mikeash.com: just this guy, you know?

Posted at 2005-01-24 00:00 | RSS feed (Full text feed) | Blog Index
Next article: Dashboard Rant
Tags: content-free humor
Name/comment conflict
by Mike Ash  

Going through some code, I came across a function called AtomicReplaceDirectory. The very first line of this function was a comment that said:

note, despite the name, this is *not* atomic.

Isn't it great when the name and the comments are at odds?

It's not quite as bad as it sounds; it turns out that an atomic directory replace operation is impossible to perform on Mac OS X (and pretty much any other OS/FS combination). This function performs a best effort at something that approximates an atomic directory replacement, so its name is reasonable in the end.

Did you enjoy this article? I'm selling whole books full of them! Volumes II and III are now out! They're available as ePub, PDF, print, and on iBooks and Kindle. Click here for more information.


What’s an atomic directory replace operation supposed to do?
Atomic means that the operation is indivisible. In other words, you either get the complete original directory or the complete new directory, but never anything else, even if the dog trips over the power cord in the middle.

Atomic file swaps are usually pretty easy. You get the files on the same disk, make a system call, the OS swaps the pointers around and you’re good. No matter what happens, you’ll either get the old file still there or you’ll get the new file. You’ll never get a corrupted or empty file. This technique doesn’t work for directories, though, so you just have to approximate it.

Comments RSS feed for this page

Add your thoughts, post a comment:

Spam and off-topic posts will be deleted without notice. Culprits may be publicly humiliated at my sole discretion.

The Answer to the Ultimate Question of Life, the Universe, and Everything?
Formatting: <i> <b> <blockquote> <code>.
NOTE: Due to an increase in spam, URLs are forbidden! Please provide search terms or fragment your URLs so they don't look like URLs.
Hosted at DigitalOcean.