Safe File Update (atomic)

What is the recommended way for atomic (complete) file data updates in FreeBSD?

The non-atomic variant is like:
Code:
// old state
int fd = open("", O_CREAT | O_TRUNC | O_WRONLY)
write(fd, ...); // 0+ times
fsync(fd); // optional
close(fd);
// new state

Note this is more about kernal internals then userspace.
 
Under Linux (and I believe most other Un*xen), the recommended way is to write to another file in the same partition, and then use the fact that rename(2) or link(2) guarantee that when the destination argument already exists, it will always point to either the old file, or the new one, even if the system crashes in between.
 
Back
Top