Developer Guidelines for Using /tmp ?
apple Verified ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Is there an Apple document anywhere with an official guidelines for using /tmp?
I know Apple started (explicitly) encouraging developers to use /tmp under Leopard because it's ignored by Time Machine but I can't find any actual definition of what a "temporary" file is or how long it can be guaranteed to exist in /tmp. Does the system regularly flush /tmp files using something like cron or luanchd as a part of system maintain?
I think it is a good idea but I don't want someone to leave the application open and possibly have a support file pulled out from underneath it.
Thanks,
=Tod
5 replies
Sign in or join Beachball Help to reply.
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Tod Kuykendall wrote:
Is there an Apple document anywhere with an official guidelines for using /tmp?I don't think Apple's guidelines are any different than the standard /tmp guidelines for UNIX. That being said, I don't know if they are written down anywhere.
I wouldn't use /tmp at all if there were another option. Most UNIX programmers try to avoid it because it is is usually a tiny partition on a local file system. Often, if you need a temp file, you need lots of space. Other times, you need a temp file but you want it to be fast. A common tactic is to create temp files and then hard link them into place when you are done. That usually won't work if using /tmp because the final destination is a different file system.
I know Apple started (explicitly) encouraging developers to use /tmp under Leopard because it's ignored by Time Machine but I can't find any actual definition of what a "temporary" file is or how long it can be guaranteed to exist in /tmp. Does the system regularly flush /tmp files using something like cron or luanchd as a part of system maintain?
They usually go away at reboot. I don't know if it is shutdown or start up. /tmp is a good place for UNIX sockets, but not much else.
I think it is a good idea but I don't want someone to leave the application open and possibly have a support file pulled out from underneath it.
Nothing is going to get pulled out from underneath. I don't know the details of when the flush takes place, but I know it won't touch any open files. Otherwise, some system services (UNIX sockets are on my mind these days) wouldn't work. I wouldn't close any file in /tmp and later expect it to be there. It usually will be there, but it isn't guaranteed.
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
+I don't think Apple's guidelines are any different than the standard /tmp guidelines for UNIX. That being said, I don't know if they are written down anywhere.+
Yeah, that's kind of as far as I got. Of course everyone uses /tmp for make installs and builds and all that on the Unix side but I don't see any explicit guidelines on what is expected or appropriate.
Essentially what I'm doing is downloading files and putting them into an IKImageBrowserView. Because it's a viewer app I consider all the files to be scratch or temporary data and expect the all of them to be thrown away. A user would have to explicitly save a copy of one of the viewed files to keep it locally after quitting. For now I'm simply writing the files into an array in memory but obviously under heavy usage this becomes a problem.
IKImageBrowserDataSource can supply a filepath instead so I was considering writing the files to a temporary folder and then deleting the folder on shutdown, but where to write the folder becomes an issue. Creating a folder in the programs user/preferences folder seems to the best combo of hidden but self contained space but I hesitate to do this. If the program ever exited abnormally the files would remain there. Obviously I'd double-check at start-up and blow away any old folders and start over, but it still seems sketchy somehow.
Your point about the open files is taken, though, because these would be "closed" files to system but the BrowserView would still be referencing them for redraw so /tmp is probably out. I just liked the idea that if I left any extraneous folders/files they would get cleared by the system clearing /tmp if the user didn't ever start-up my app again.
Any other ideas appreciated,
=Tod
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
That's what "Application Support" and "Caches" are for.
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Tod Kuykendall wrote:
Yeah, that's kind of as far as I got. Of course everyone uses /tmp for make installs and builds and all that on the Unix side but I don't see any explicit guidelines on what is expected or appropriate.I've never incorporated /tmp into any kind of workflow. There are better ways to do that. Normally I do things in Perl and Perl supports some tricks to create temporary files that automatically get deleted when the script exits. This works using the C function tmpnam(). It creates a file, opens it, and returns a pointer to it. Then it unlinks it. When you close the file (either explicitly or when your process dies) the file goes away for good.
Your point about the open files is taken, though, because these would be "closed" files to system but the BrowserView would still be referencing them for redraw so /tmp is probably out. I just liked the idea that if I left any extraneous folders/files they would get cleared by the system clearing /tmp if the user didn't ever start-up my app again.
I think it is perfectly safe to use /tmp in that manner. I was just speculating because I don't really know. I think there is some cron task that deletes files that haven't been used after some period. The safest strategy is still to maintain your own temporary files.
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
You're right - I think ~/Library/Caches is the correct destination for this kind of usage. It is even mentioned explicitly alongside /tmp in the developer doc urging developers to use non-Time Machine monitored folders. I must have mentally skipped over that part while I was reading it... I blame my *nix background for being /tmp-centric.
Using a user specific folder versus a systemwide folder also avoids any multiple user issues like fast user switching or files left over from one users session and another opening the program later from a different account. Unlikely, yes, but something to keep in mind - you would need to session_ID the folder name in /tmp to avoid fast-switched users accessing each others data.
In case anyone else is interested this the only Apple document I was able to find that addresses the issue directly: http://developer.apple.com/documentation/MacOSX/Conceptual/BPFileSystem/Articles /WhereToPutFiles.html
Reading it I just found out there is actually an NSTemporaryDirectory command. Huh, there's always more to Cocoa than you knew.Thanks guys,
=Tod