Matthew Mitchell
Matthew Mitchell

Reputation: 5383

POSIX guaranteeing write to disk

As I understand it, if I want to synchronise data to the storage device I can use fsync() to supposedly flush all the OS output caches... but apparently it doesn't guarantee this at all, unlike the documentation tries to deceive you, and the data may not be written to the disk!

This is not very good for many purposes because it can lead to data corruption. How do I use the POSIX libraries (In a portable way if possible) to guarantee that the data has been written (as far as possible) and prevent data corruption?

There is fdatasync() but it is not implemented on OSX, so is there a better and more portable way, or does one have to implement different code on different systems? I'm also not sure if fdatasync() is good enough.

Of-course, in the worst case scenario I could forget about this and use a redundant database library that uses ACID to store the data. I don't want that.

Also I'm interested in how to ensure truncate and rename operations have definitely completed.

Thanks!

Upvotes: 2

Views: 1256

Answers (1)

jmh
jmh

Reputation: 9336

You are looking for sync. There is both a program called sync and a system call called sync (man 1 sync and man 2 sync respectively):

   #include <unistd.h>

   void sync(void);

DESCRIPTION
   sync() first commits inodes to buffers, and then buffers to disk.

So it will ensure that all pending operations (truncates, renames etc) are in fact written to the disk.

fsync does not claim to flush all output caches, but instead claims to flush all changes to a particular file descriptor to disk. It explicitly does not ensure that the directory entry is updated (in which case a call to fsync on a filedescriptor for the directory is needed).

fsyncdata is even more useless as it will not flush file metadata and instead will just ensure that the data in the file is flushed.

It is a good idea to trust the manpages. I won't say there are not mistakes, but they tend to be extremely accurate.

Upvotes: 6

Related Questions