« Netscape's "Tin Man" rocket | Main | Apple's "1984" ad: Rejected by the board »

Unix should have a newfd call

So you can open a file and then unlink() it. The fd hangs onto the file, even though the file isn't visible in the filesystem anymore. Once you close the file, it goes away.

This is kinda cool and gets gets used every so often, but much more useful would be the reverse.

Cleaning up files in the process of being created before they are renamed() into place is a pain. If you could create a new filesystem fd in the unlinked state first, and then link it into the filesystem once it was ready, all the temp file and unlink-on-error nonsense could be done away with.

newfd() would have to take a path to associate the fd with a particular filesystem, like statvfs(), but that's easy.

A lot of newbie programmer errors where a partially-written file is put into place over the existing file would be eliminated too. I bet this could have saved a lot of trouble over the years.

Kinda late to be adding calls like this though. 30+ years late.

Comments (3)

Would this work:

Put the files in a directory, like "foo.20071126120436", and then when the files are completely written there, symlink a directory "foo.latest" pointing to foo.20071126120436? (atomically)

Then "foo.latest" always points to the latest, complete copy of the files.

If you have a whole directory's worth of stuff, then that's a reasonable approach.

I just find it a little surreal how difficult it is to reliably write a file. You have to do something like


safely write file:

tempname = reliable_temp_filename()

if (!open tempname for write)
return error

if (!write buf to file) {
unlink(tempname)
close(fille)
return error
}

if (!close file) {
unlink(tempname)
return error
}

if (!rename(tempname, realname)) {
unlink(tempname)
return error
}

return success


A lot of work to write a simple little file.

It is kinda messed up, yeah.

I tend to prefer a hard link rather than a symlink in some cases (there are pluses and minuses there too). But it'd be nice if there was a clean way to do this baked right into libc.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on November 26, 2007 3:38 AM.

The previous post in this blog was Netscape's "Tin Man" rocket.

The next post in this blog is Apple's "1984" ad: Rejected by the board.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33