/ OSX / utilities / SecKnownFilePaths.m
SecKnownFilePaths.m
 1  
 2  #import <Foundation/Foundation.h>
 3  #import "SecKnownFilePaths.h"
 4  #import "OSX/utilities/SecCFRelease.h"
 5  #ifdef DARLING
 6  #import <CoreFoundation/CFPriv.h>
 7  #endif
 8  
 9  // This file is separate from SecFileLocation.c because it has a global variable.
10  // We need exactly one of those per address space, so it needs to live in the Security framework.
11  static CFURLRef sCustomHomeURL = NULL;
12  
13  CFURLRef SecCopyHomeURL(void)
14  {
15      // This returns a CFURLRef so that it can be passed as the second parameter
16      // to CFURLCreateCopyAppendingPathComponent
17  
18      CFURLRef homeURL = sCustomHomeURL;
19      if (homeURL) {
20          CFRetain(homeURL);
21      } else {
22  #ifdef DARLING
23          // ported from an older version of Security
24          //
25          // i'm not sure how Apple is convincing the compiler that CFCopyHomeDirectoryURL is available on macOS
26          // because there's nothing new in the public headers to indicate that the function has suddenly become
27          // available on macOS, nor is there any indication in the Xcode build files that this code is being
28          // compiled for Catalyst for macOS
29          //
30          // maybe they're just not using compiler availability warnings/errors
31          //
32          // either way, this should work fine and provide the same behavior as Apple's code
33          homeURL = CFCopyHomeDirectoryURLForUser(NULL);
34  #else
35          homeURL = CFCopyHomeDirectoryURL();
36  #endif
37      }
38  
39      return homeURL;
40  }
41  
42  CFURLRef SecCopyBaseFilesURL(bool system)
43  {
44      CFURLRef baseURL = sCustomHomeURL;
45      if (baseURL) {
46          CFRetain(baseURL);
47      } else {
48  #if TARGET_OS_OSX
49          if (system) {
50              baseURL = CFURLCreateWithFileSystemPath(NULL, CFSTR("/"), kCFURLPOSIXPathStyle, true);
51          } else {
52              baseURL = SecCopyHomeURL();
53          }
54  #elif TARGET_OS_SIMULATOR
55          baseURL = SecCopyHomeURL();
56  #else
57          baseURL = CFURLCreateWithFileSystemPath(NULL, CFSTR("/"), kCFURLPOSIXPathStyle, true);
58  #endif
59      }
60      return baseURL;
61  }
62  
63  void SecSetCustomHomeURL(CFURLRef url)
64  {
65      sCustomHomeURL = CFRetainSafe(url);
66  }
67  
68  void SecSetCustomHomeURLString(CFStringRef home_path)
69  {
70      CFReleaseNull(sCustomHomeURL);
71      if (home_path) {
72          sCustomHomeURL = CFURLCreateWithFileSystemPath(NULL, home_path, kCFURLPOSIXPathStyle, true);
73      }
74  }