[Mono-list] Does FileMode.Append have O_APPEND semantics on Unix?
Jonathan Pryor
jonpryor at vt.edu
Wed May 3 07:08:57 EDT 2006
On Wed, 2006-05-03 at 08:43 +0200, Jaroslaw Kowalski wrote:
> My question is: is FileMode.Append supposed to have O_APPEND semantics on
> Unix?
Sadly, no. Mono's System.IO layer is layered upon io-layer, which
exports a Win32 API. FileMode.Append is mapped to OPEN_ALWAYS for the
CreateFile() call, and OPEN_ALWAYS is mapped to O_CREAT on Unix.
O_APPEND never enters the picture.
> If not, what's the best way to open file with this append semantics in
> mono? I've seen you have MonoIO class, but I'm not sure how it can be used.
MonoIO is how managed code interacts with the runtime io-layer, which
uses Win32 API calls for its implementation. You can't get append
semantics with MonoIO.
If you really want O_APPEND, you can always use Mono.Unix.UnixStream
within Mono.Posix.dll:
int fd = Mono.Unix.Native.Syscall.open ("path",
Mono.Unix.Native.OpenFlags.O_APPEND);
Stream s = new UnixStream (fd);
// Write to `s'...
However, using O_APPEND isn't necessarily a panacea -- the open(2) man
page mentions:
O_APPEND may lead to corrupted files on NFS file systems if
more than one process appends data to a file at once. This is
because NFS does not support appending to a file, so the client
kernel has to simulate it, which can’t be done without a race
condition.
So using your existing named-mutex solution may be the better solution,
at least if your users will potentially use NFS.
- Jon
More information about the Mono-list
mailing list