1

I need to create a directory full of data files. These files are are temporary, user-private files.

While the files are temporary, and it's ok if they are deleted by the system, there's a lot of them and I'd like to reuse them if I can. Furthermore, I expect other applications (running as the same user) to access/create these files too. This means I need a standard protocol that will work across different programming languages.

Since I know someone will suggest it, I do not want to use '/tmp' or '%TEMP% or the java system property java.io.tmpdir. These directories are system-scoped. I'm also very interested in finding platform-standard or platform-approved methods over just finding a solution that works.

I'm trying to collect a list of where to locate these files for various platforms. Below is what I have so far, but I would really appreciate additional platforms and/or validation of what I've already found.

Drew
  • 521
  • 3
  • 17
  • The environment variable %temp% in modern Windows (I think XP SP3 and up) is in the "documents and settings" or "User" directory. TEMP and TMP = %USERPROFILE%\AppData\Local\Temp on the 2 machines I just looked at. – JimR Apr 09 '12 at 22:43

3 Answers3

2

On Mac OS X, you have a couple of options:

$TMPDIR is actually user-scoped. It's a directory within /var/folders but. within there, there's a user-specific directory hierarchy. The specific directory pointed to by $TMPDIR doesn't allow reading by anybody but the user. This is what the Cocoa function NSTemporaryDirectory() returns.

You can also use ~/Library/Caches/CompanyOrProductName. The system will not clean this out spontaneously, but it won't be included in Time Machine backups and the user can feel free to nuke it at will.

You should not use ~/Library/CompanyName. Perhaps you were thinking of ~/Library/Application Support/CompanyName? That might be appropriate but isn't usually for temporary stuff.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
2
  • If you are running Solaris and you don't mind these files to be lost where the OS reboots, you can store the files in /tmp/.<username>-<company>/ and change the directory permissions to protect it.
  • If you want these temporary files to survive a reboot, use /var/tmp/.<username>-<company>/ or ~/.<company>
  • If the user's home directories are remotely mounted and shared between various machines and you want your temporary files to be also shared the same way, you can use ~/.<company>/.

Of course, .username-company and .company are just suggestions. You can use any name that wouldn't clash with other applications and users.

~/.<company> is by far the most common choice, like for example ~/.mozilla, ~/.openoffice.org, ~/.adobe, ~/.mysql, ~/.netbeans, ~/.thunderbird, ~/.VirtualBox and the likes.

In any case, on Solaris and probably most other Unix and Unix like systems, there are no other user writable directories defined by the standard than the home directory, /tmp and /var/tmp.

jlliagre
  • 29,783
  • 6
  • 61
  • 72
0

This is the best list I've found. If you have any corrections/suggestions, please let me know.

On Windows:

  1. Create a directory "CompanyName" in the "local app data" directory. Find this via:

    1. The system environment variable LOCALAPPDATA, or
    2. Query the registry by executing the following command line:

      reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Local AppData
      

On MacOSX:

  1. $TMPDIR or Cocoa function NSTemporaryDirectory()
    • Cleared automatically every three days
  2. mkdir -m 700 ~/Library/Caches/CompanyOrProductName
    • Not removed by system automatically (fine for my use case)
    • Not backed up by TimeMachine
  3. mkdir -m 700 ~/Library/Application Support/CompanyOrProductName
    • Not removed by system automatically (fine for my use case)
    • Backed up by TimeMachine

On Linux:

  1. mkdir -m 700 ~/.company-name

On Solaris / OpenSolaris:

  1. ?

References:

  • Cocoa with Love explains the three MacOSX options above and also discusses several security issues.
Drew
  • 521
  • 3
  • 17