merge IOSAD-150
authorDirk Zimmermann <dz@pep.security>
Wed, 09 Oct 2019 17:11:38 +0200
changeset 1313b5e95747cf96
parent 1299 1a8cd3285b52
parent 1312 b0127139f080
child 1314 0051489a83a3
child 1316 0fdd4fa74274
child 1318 bdceebc31019
merge IOSAD-150
     1.1 --- a/pEpObjCAdapter/PEPObjCAdapter.m	Wed Oct 02 09:27:09 2019 +0200
     1.2 +++ b/pEpObjCAdapter/PEPObjCAdapter.m	Wed Oct 09 17:11:38 2019 +0200
     1.3 @@ -19,6 +19,11 @@
     1.4  
     1.5  const PEP_decrypt_flags PEP_decrypt_flag_none = 0x0;
     1.6  
     1.7 +/**
     1.8 + The pEp part of the home directory (where pEp is supposed to store data).
     1.9 + */
    1.10 +static NSString * const s_pEpHomeComponent = @"pEp_home";
    1.11 +
    1.12  #if TARGET_OS_IPHONE
    1.13  // marked for iOS to think about what we want on macOS
    1.14  const char* _Nullable perMachineDirectory = NULL;
    1.15 @@ -74,27 +79,15 @@
    1.16  }
    1.17  
    1.18  /**
    1.19 - Looks up the application support directory and creates an app-specific subdirectory under it.
    1.20 + Looks up (and creates if necessary) a pEp directory under "Application Support".
    1.21  
    1.22 - Directories derived from it:
    1.23 -
    1.24 - * $HOME (the engine uses that).
    1.25 - * The engine's per_user_directory (which is placed under $HOME).
    1.26 - * The engine's per_machine_directory (for iOS).
    1.27 -
    1.28 - @return A URL pointing to as app-specific directory under the OS defined
    1.29 -         application support directory for the current user.
    1.30 + @return A URL pointing a pEp directory under "Application Support".
    1.31   */
    1.32 -+ (NSURL *)createApplicationDirectory
    1.33 ++ (NSURL *)createApplicationDirectoryOSX
    1.34  {
    1.35 -    NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
    1.36 -    if (!bundleID) {
    1.37 -        // This can happen in unit tests
    1.38 -        bundleID = @"test";
    1.39 -    }
    1.40      NSFileManager *fm = [NSFileManager defaultManager];
    1.41      NSURL *dirPath = nil;
    1.42 -    
    1.43 +
    1.44      // Find the application support directory in the home directory.
    1.45      NSArray *appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
    1.46                                          inDomains:NSUserDomainMask];
    1.47 @@ -103,8 +96,8 @@
    1.48          // Append the bundle ID to the URL for the
    1.49          // Application Support directory.
    1.50          // Mainly needed for OS X, but doesn't do any harm on iOS
    1.51 -        dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:bundleID];
    1.52 -        
    1.53 +        dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:s_pEpHomeComponent];
    1.54 +
    1.55          // If the directory does not exist, this method creates it.
    1.56          // This method is only available in OS X v10.7 and iOS 5.0 or later.
    1.57          NSError *theError = nil;
    1.58 @@ -115,11 +108,67 @@
    1.59              return nil;
    1.60          }
    1.61      }
    1.62 -    
    1.63 +
    1.64      return dirPath;
    1.65  }
    1.66  
    1.67  /**
    1.68 + Looks up the shared directory for pEp apps under iOS and makes sure it exists.
    1.69 +
    1.70 + @return A URL pointing a pEp directory in the app container.
    1.71 +*/
    1.72 ++ (NSURL *)createApplicationDirectoryiOS
    1.73 +{
    1.74 +    NSString *appGroupId = @"group.security.pep.pep4ios";
    1.75 +    NSFileManager *fm = [NSFileManager defaultManager];
    1.76 +    NSURL *containerUrl = [fm containerURLForSecurityApplicationGroupIdentifier:appGroupId];
    1.77 +    NSLog(@"containerUrl '%@'", containerUrl);
    1.78 +
    1.79 +    if (containerUrl == nil) {
    1.80 +        // Will happen when running tests, so fall back.
    1.81 +        NSArray *appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
    1.82 +                                            inDomains:NSUserDomainMask];
    1.83 +        containerUrl = [appSupportDir lastObject];
    1.84 +    }
    1.85 +
    1.86 +    if (containerUrl == nil) {
    1.87 +        NSLog(@"ERROR: No app container, no application support directory.");
    1.88 +    }
    1.89 +
    1.90 +    NSURL *dirPath = [containerUrl URLByAppendingPathComponent:s_pEpHomeComponent];
    1.91 +
    1.92 +    // If the directory does not exist, this method creates it.
    1.93 +    NSError *theError = nil;
    1.94 +    if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES
    1.95 +                       attributes:nil error:&theError]) {
    1.96 +        NSLog(@"ERROR: Could not create pEp home directory, directly writing to app container instead.");
    1.97 +    }
    1.98 +
    1.99 +    return dirPath;
   1.100 +}
   1.101 +
   1.102 +/**
   1.103 + Looks up the shared directory for pEp apps under iOS and makes sure it exists.
   1.104 +
   1.105 + Derived settings:
   1.106 +
   1.107 + * $HOME (the engine uses that).
   1.108 + * The engine's per_user_directory (which is placed under $HOME).
   1.109 + * The engine's per_machine_directory (see [PEPObjCAdapter setPerMachineDirectory:]).
   1.110 +
   1.111 + @return A URL pointing to as app-specific directory under the OS defined
   1.112 + application support directory for the current user.
   1.113 + */
   1.114 ++ (NSURL *)createApplicationDirectory
   1.115 +{
   1.116 +#if TARGET_OS_IPHONE
   1.117 +    return [self createApplicationDirectoryiOS];
   1.118 +#else
   1.119 +    return [self createApplicationDirectoryOSX];
   1.120 +#endif
   1.121 +}
   1.122 +
   1.123 +/**
   1.124   Sets the directory that will be fed into the engine's per_machine_directory.
   1.125  
   1.126   Does not handle macOS. For macOS, either PER_MACHINE_DIRECTORY has to be defined