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 a whole book full of them. It's available for iBooks and Kindle, plus a direct download in PDF and ePub format. It's also available in paper for the old-fashioned. Click here for more information.

Comments:

avium at 2005-01-25 01:04:00:
What’s an atomic directory replace operation supposed to do?

mikeash at 2005-01-25 01:07:00:
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.

Name:
Web site:
Comment:
Formatting: <i> <b> <blockquote> <code>. URLs are automatically hyperlinked.
Hosted at DigitalOcean.