Monday, March 09, 2009

fsync




Ext4 data loss - The importance of doing things right when writing to files is going to be critical with some of the new filesystems out there.

Do this:


open and read file ~/.kde/foo/bar/baz
fd = open("~/.kde/foo/bar/baz.new", O_WRONLY|O_TRUNC|O_CREAT)
write(fd, buf-of-new-contents-of-file, size-of-new-contents-of-file)
fsync(fd) --- and check the error return from the fsync
close(fd)
rename("~/.kde/foo/bar/baz", "~/.kde/foo/bar/baz~") --- this is optional
rename("~/.kde/foo/bar/baz.new", "~/.kde/foo/bar/baz")