/tmp on OS X

Under Linux and most other Un*x systems, you can rely on the directory /tmp being the system scratch space. As such, you can pretty much use /tmp without fear.

On OS X, each user has its own scratch space. Use the NSTemporaryDirectory() function to retrieve the directory name.

Here is a small bit of Objective C example code.

#import 

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// print the current user's scratch directory.
NSLog(NSTemporaryDirectory());

[pool drain];
return 0;
}

Update 2009-07-27:
On the command line, the TMPDIR environment variable holds the same value that NSTemporaryDirectory() returns.

4 thoughts on “/tmp on OS X

  1. Harley Pebley

    That’s cool that each user has their own /tmp space. Hope as sys admin you can specify where it is (like /home). Once found out the hard way /tmp should not be a mount point on / (the default, at least on the dist I was on). Since then I always give it its own partition.

  2. James Reuben Knowles Post author

    “Once found out the hard way /tmp should not be a mount point on /”

    You, too, eh? War story time. 🙂

    I haven’t dug into system internals enough to know if there’s a simple way to tell OS X to use a separate partition for temp directories.

    Interestingly, /etc /tmp and /var are all symlinks into /private, which appears to be marked with extended attributes. I don’t know enough about BSD-specific intricacies to know what’s going on, but the per-user configurability make me suspect that union filesystem features may be used — but that’s pure speculation.

  3. Sid

    James, on my iMac, there is an issue related to NSTemporaryDirectory. The issue reads something like this:

    “Could not get Darwin_user_dir, falling back to NSTemporaryDirectory()”

    Because of this, I couldn’t run the Mac OS. I am frustrated, and have no clue whatsoever on how to fix this. Any help?

  4. James Reuben Knowles Post author

    Sid,

    Ouch! That sounds nasty. It sounds like something got corrupted somewhere. Unfortunately it sounds like something completely different than what this blog post is about, and I’m not sure what might be the matter, off hand.

    I’d try the Apple forums for starters. If you have your install disks still, you can try doing a recovery also.

    I wish I could be more helpful. Mysterious errors that defy Google happen. Regardless, I wish you my sincere wishes for a speedy resolution.

Leave a Reply