Locating the macOS tmp directory

From Free Pascal wiki
Revision as of 09:01, 26 March 2021 by Trev (talk | contribs) (→‎See also: New section)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

English (en)

macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

Overview

To locate the current user's temporary directory in macOS you should use the native macOS Foundation function NSTemporaryDirectory(). This directory is unique for each user, the user is guaranteed to have write permissions to it, and it is in a hashed location which is not predictable in advance so that it is safe from security issues associated with predictable locations. It is also guaranteed to work when your application has been sandboxed.

You should be aware that this directory is cleaned out automatically every 3 days but otherwise persists between application launches and reboots.

Determining where to store files

Before we go any further, let's recap where the Apple Guidelines indicate your application should store its files:

  • Use the /Applications or /Applications/Utilities directory for the application bundle. The application bundle should contain everything: libraries, dependencies, help, every file that the application needs to run except those created by the application itself. If the application bundle is copied to another machine's /Applications or /Applications/Utilities directory, it should be able to run. Installing to these folders requires Admin privileges. The data in these folders is backed up by Time Machine.
  • Use the ~/Applications directory should Admin privileges not be available. This is the standard location for a single user application. This directory should not be expected to exist. The application bundle should contain everything: libraries, dependencies, help, every file that the application needs to run except those created by the application itself. If the application bundle is copied to another machine's /Applications or /Applications/Utilities directory, it should be able to run. This data is backed up by Time Machine.
  • Use the Application Support directory (this data is backed up by Time Machine), appending your <bundle_ID>, for:
    • Resource and data files that your application creates and manages for the user. You might use this directory to store application state information, computed or downloaded data, or even user created data that you manage on behalf of the user.
    • Autosave files.
  • Use the Caches directory (this is not backed up by Time Machine), appending your <bundle_ID>, for cached data files or any files that your application can recreate easily.
  • Use CFPreferences to read and write your application's preferences. This will automatically write preferences to the appropriate location and read them from the appropriate location. This data is backed up by Time Machine.
  • Use the application Resources directory (this is backed up by Time Machine) for your application-supplied image files, sound files, icon files and other unchanging data files necessary for your application's operation.
  • Use NSTemporaryDirectory (this is not backed up by Time Machine) to store temporary files that you intend to use immediately for some ongoing operation but then plan to discard later. Delete temporary files as soon as you are done with them.

Storing temporary files

How to locate the user's unique temporary directory, display it and write a temporary file to it is demonstrated by the code below:

...
{$modeswitch objectivec1} 

interface

Uses
  ...
  CocoaAll;
...

procedure TmpFile;
var
  tmpFile: TextFile;
  filePath: String;
begin
  ShowMessage(NSTemporaryDirectory.UTF8String);

  filePath := NSTemporaryDirectory.Utf8String + 'test.tmp';

  AssignFile(TmpFile, filePath);
  Try
    Rewrite(TmpFile);
    Writeln(TmpFile, 'This is my test tmp file.');
  Finally
    CloseFile(TmpFile);
  End;
end;

Why not use GetTempDir?

It should be noted that Free Pascal's GetTempDir function (from SysUtils) uses the value of the macOS TMPDIR environment variable to determine the path to where the current user's temporary files should be stored. This is a possible security issue because the TMPDIR environment variable is easily compromised.

See also