Difference between revisions of "Locating the macOS tmp directory"

From Lazarus wiki
(New page content for macOS :-)
m (Fix template)
Line 1: Line 1:
{{Platform only|macOS}}
{{Platform only|macOS}}
== Overview ==
== Overview ==

Revision as of 08:26, 13 December 2019


This article applies to macOS only.

See also: Multiplatform Programming Guide

English (en)


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 Application Support directory (this 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 writes preferences to the appropriate location and read them from the appropriate location. This data is backed up by Time Machine.
  • Use NSTemporaryDirectory 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

The following string support functions are needed for our use of NSTemporaryDirectory():

// String support functions (source https://macpgmr.github.io)


function CFStrToAnsiStr(cfStr    : CFStringRef;
                        encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1): AnsiString;
 {Convert CFString to AnsiString.
  If encoding is not specified, encode using CP1252.}
  StrPtr   : Pointer;
  StrRange : CFRange;
  StrSize  : CFIndex;
  if cfStr = nil then
    Result := '';

   {First try the optimized function}
  StrPtr := CFStringGetCStringPtr(cfStr, encoding);
  if StrPtr <> nil then  {Succeeded?}
    Result := PChar(StrPtr)
  else  {Use slower approach - see comments in CFString.pas}
    StrRange.location := 0;
    StrRange.length := CFStringGetLength(cfStr);

     {Determine how long resulting string will be}
    CFStringGetBytes(cfStr, StrRange, encoding, Ord('?'),
                     False, nil, 0, StrSize);
    SetLength(Result, StrSize);  {Expand string to needed length}

    if StrSize > 0 then  {Convert string?}
      CFStringGetBytes(cfStr, StrRange, encoding, Ord('?'),
                       False, @Result[1], StrSize, StrSize);
end;  {CFStrToAnsiStr}

function NSStrToAnsiStr(aNSStr   : NSString;
                        encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1): AnsiString;
 {Convert NSString to AnsiString.
  If encoding is not specified, encode using CP1252.}
   {Note NSString and CFStringRef are interchangable}
  Result := CFStrToAnsiStr(CFStringRef(aNSStr), encoding);

With those string support functions in place, you can now locate the user's unique temporary directory, display it and write a file to it as demonstrated by the code below:

procedure TForm1.FormShow(Sender: TObject);
  tmpFile: TextFile;
  filePath: String;

  filePath := (NSStrToAnsiStr(NSTemporaryDirectory) + PathDelim + 'test.tmp');

  AssignFile(TmpFile, filePath);
    Writeln(TmpFile, 'This is my test tmp file');

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.