Difference between revisions of "Locating the macOS tmp directory"

From Lazarus wiki
Jump to navigationJump to search
m (→‎Determining where to store files: Added TM note to tmp dir location)
m (→‎Storing temporary files: Resolved compiler uninitialised var warning)
Line 44: Line 44:
 
   StrSize  : CFIndex;
 
   StrSize  : CFIndex;
 
begin
 
begin
 +
  StrSize := 0;
 +
 
   if cfStr = nil then
 
   if cfStr = nil then
 
     begin
 
     begin

Revision as of 23:52, 14 December 2019

macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

English (en)

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 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 (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

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

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

Uses
 ...
 SysUtils,
 CocoaAll,
 ...;

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

  if cfStr = nil then
    begin
    Result := '';
    Exit;
    end;

   {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}
    begin
    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;
end;  {CFStrToAnsiStr}

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

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);
var
  tmpFile: TextFile;
  filePath: String;
begin
  ShowMessage(NSStrToAnsiStr(NSTemporaryDirectory));

  filePath := (NSStrToAnsiStr(NSTemporaryDirectory) + PathDelim + '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.