by _wmd · 12y ago · 0 comments
In other words:
1. fd = open("super-safe-file.tmp", O_CREAT|O_RDWR);
2. write(fd, "super-safe-data", 15);
3. close(fd);
4. rename("super-safe-file.tmp", "super-safe-file");
5. (kernel flushes file and directory metadata to disk)
6. CRASH
7. Machine reboots, "super-safe-data" exists, but no longer contains any data, since file data itself was never flushed.
8. Tears are shed, programmers are fired, backups are restored
However, HN runs on FreeBSD, and my understanding is that the combination of soft-updates + journalling there actually do provide atomic rename, even in the case of catastrophic failure. McKusick talks about it here: http://www.mckusick.com/softdep/suj.pdf
Also, just to anchor the discussion a bit, the HN code does use the "write foo.tmp; mv foo.tmp foo" trick all over the place. (Or at least, the most recent version of news.arc I've seen does.)
https://github.com/wting/hackernews/blob/master/arc.arc#L841