Append mode for NonBlockingFileIO?

openFile only seems to provide read and write modes, with the curious omission of append (O_APPEND).

I'm planning to work around this by providing the following argument to openFile: flags: append ? .posix(flags: O_APPEND, mode: 0) : .default.

Any reason this isn't provided out of the box?

Yeah, but it's not a terribly good one. The short answer is that .read and .write are different from append. Read and write (what NIO calls modes) form (along with read-write mode) a set of options that you must pass exactly one of to the open syscall. That is, open requires exactly one of O_RDONLY, O_WRONLY, and O_RDWR.

Notice that none of those are O_APPEND. This is because O_APPEND is a modifying flag: it modifies the behaviour of writes. But it doesn't replace write mode: you have to set O_APPEND and a write mode.

With that said, I agree with you: while this API is pedantically correct, it's not all that helpful, and an append mode would certainly ease the user interaction with the API. We already have an issue for it: NonBlockingFileIO should have an append mode for open · Issue #1779 · apple/swift-nio · GitHub. We'd love it if someone in the community felt motivated to add it.

1 Like

Seems like swift-system has a different approach which supports append:

Has there been discussion on replacing some of this infrastructure (or at least making it more compatible) with swift-system?

I don't think Swift system's approach is meaningfully different to ours: FileDescriptor.AccessMode is essentially identical to NIO's NIOFileHandle.Mode, and does not include .append. The difference is only that there's a nice .append spelling, instead of forcing you to write O_APPEND. We could certainly add a .append helper to NIOFileHandle.Flags to improve the spelling.

As to reconciling the two, in the fullness of time much of what we have here should be completely unnecessary and subsumed by other frameworks. In the short term, I'd love to reconcile with Swift System but for backward compatibility reasons we'll need to carry the current implementation for the foreseeable future.

As a gift to you: Provide NonBlockingFileIO helpers that use Swift System · Issue #1957 · apple/swift-nio · GitHub

3 Likes