in reply to Writing to a file atomically without renaming
No, not really. Not in UNIX systems at least. Of course, the word "atomic" is somewhat vague, you may want to define more precisely what you want.
Here are the alternatives. You can use file locking, but that's atomic only if the other program reading the file uses them too. You might hear of mandatory locking, but that doesn't really make a write atomic, as it could interrupt You can use link instead of rename, but I doubt that would help. You can always write a single byte atomically to a file, which is enough if you just want to change a flag. You can write to datagram sockets atomically (with the maximum size possibly restricted), but that doesn't substitutes real files. You can somehow make sure that all other services that could read the file are stopped, like going into single user mode and running only the process that writes the file; this will make the write practically atomic, but that's probably not what you want either.
You could create a mandatory lock or lease on the file, thus making it sure that no-one can open the file while you are doing the read; but someone can still have the file opened before you do that, and there is no way to tell if this is the case if it's some other user. If you are sure that only (non-setid) programs runnnig under your uid can have the file open, than this can be feasable.
So, the situation is like this: you can only do an atomic write if either
|
|---|