How do I get the default temporary directory on Mac OS X?
Asked Answered
S

4

23

I'd like to create some directories of data for some unit tests and I'd like these directories to be in the default temporary directory for the user.

I could just create a subdir under /tmp I suppose, but I don't want to make an assumption about how somebody has set up their own machine.

I'm planning on writing the test data on the fly, which is why I'd like to put this into a temporary directory.

Sheathing answered 17/12, 2008 at 12:42 Comment(1)
Also note that /tmp is not accessible in a process running sandboxed (using normal sandbox rules, as in something distributed through the Mac App Store)Unmeet
S
8

Looking back at this question there and the documentation for NSTemporaryDirectory(), if you are using 10.6 or above, then you are recommended to use the URLForDirectory:inDomain:appropriateForURL:create:error: method from NSFileManager for a more flexible method of creating directories.

And it returns a URL instead of a string path, which is another thing we're recommended to use.

Sheathing answered 21/3, 2012 at 22:17 Comment(1)
how and where exactly are you getting a temp directory. there seems to be some api issues with NSItemReplacementDirectory. NSHipster published: Actually, this method appears to be intended for moving existing temporary files to a permanent location on disk with -replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error:.Mcdade
B
39

Don't use tmpnam() or tempnam(). They are insecure (see the man page for details). Don't assume /tmp. Use NSTemporaryDirectory() in conjunction with mkdtemp(). NSTemporaryDirectory() will give you a better directory to use, however it can return nil. I've used code similar to this:

NSString * tempDir = NSTemporaryDirectory();
if (tempDir == nil)
    tempDir = @"/tmp";

NSString * template = [tempDir stringByAppendingPathComponent: @"temp.XXXXXX"];
NSLog(@"Template: %@", template);
const char * fsTemplate = [template fileSystemRepresentation];
NSMutableData * bufferData = [NSMutableData dataWithBytes: fsTemplate
                                                   length: strlen(fsTemplate)+1];
char * buffer = [bufferData mutableBytes];
NSLog(@"FS Template: %s", buffer);
char * result = mkdtemp(buffer);
NSString * temporaryDirectory = [[NSFileManager defaultManager]
        stringWithFileSystemRepresentation: buffer
                                    length: strlen(buffer)];

You can now create files inside temporaryDirectory. Remove the NSLogs for production code.

Bushnell answered 17/12, 2008 at 14:7 Comment(0)
S
8

Looking back at this question there and the documentation for NSTemporaryDirectory(), if you are using 10.6 or above, then you are recommended to use the URLForDirectory:inDomain:appropriateForURL:create:error: method from NSFileManager for a more flexible method of creating directories.

And it returns a URL instead of a string path, which is another thing we're recommended to use.

Sheathing answered 21/3, 2012 at 22:17 Comment(1)
how and where exactly are you getting a temp directory. there seems to be some api issues with NSItemReplacementDirectory. NSHipster published: Actually, this method appears to be intended for moving existing temporary files to a permanent location on disk with -replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error:.Mcdade
W
6

In Objective-C you can use NSTemporaryDirectory().

Wooden answered 17/12, 2008 at 18:35 Comment(0)
M
5

Use the tempnam(), tmpnam() or tmpfile() function.

Myceto answered 17/12, 2008 at 12:47 Comment(2)
tempnam() and tmpnam() are insecure. Use mkstemp() instead.Bushnell
Maybe not, but at least I found out about TEMPDIRSheathing

© 2022 - 2024 — McMap. All rights reserved.