merge IOS-1784
authorDirk Zimmermann <dz@pep.security>
Tue, 10 Sep 2019 17:13:37 +0200
changeset 1233b64d0b1b0796
parent 1213 7e79b40b50f7
parent 1232 f4c15a3f522b
child 1234 4e7e5af6608d
child 1235 f459eaced8cc
merge IOS-1784
     1.1 --- a/pEpObjCAdapter/PEPObjCAdapter.m	Wed Sep 04 13:01:54 2019 +0200
     1.2 +++ b/pEpObjCAdapter/PEPObjCAdapter.m	Tue Sep 10 17:13:37 2019 +0200
     1.3 @@ -60,7 +60,12 @@
     1.4  + (void)initialize
     1.5  {
     1.6      s_homeURL = [self createApplicationDirectory];
     1.7 -    [self setPerMachineDirectory:s_homeURL]; // Important, defines $HOME for the engine
     1.8 +
     1.9 +    // The engine will put its per_user_directory under this directory.
    1.10 +    setenv("HOME", [[s_homeURL path] cStringUsingEncoding:NSUTF8StringEncoding], 1);
    1.11 +
    1.12 +    // This sets the engine's per_machine_directory under iOS.
    1.13 +    [self setPerMachineDirectory:s_homeURL];
    1.14  }
    1.15  
    1.16  + (NSURL *)homeURL
    1.17 @@ -68,6 +73,18 @@
    1.18      return s_homeURL;
    1.19  }
    1.20  
    1.21 +/**
    1.22 + Looks up the application support directory and creates an app-specific subdirectory under it.
    1.23 +
    1.24 + Directories derived from it:
    1.25 +
    1.26 + * $HOME (the engine uses that).
    1.27 + * The engine's per_user_directory (which is placed under $HOME).
    1.28 + * The engine's per_machine_directory (for iOS).
    1.29 +
    1.30 + @return A URL pointing to as app-specific directory under the OS defined
    1.31 +         application support directory for the current user.
    1.32 + */
    1.33  + (NSURL *)createApplicationDirectory
    1.34  {
    1.35      NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
    1.36 @@ -102,16 +119,21 @@
    1.37      return dirPath;
    1.38  }
    1.39  
    1.40 -+ (void)setPerMachineDirectory:(NSURL *)homeDir
    1.41 +/**
    1.42 + Sets the directory that will be fed into the engine's per_machine_directory.
    1.43 +
    1.44 + Does not handle macOS. For macOS, either PER_MACHINE_DIRECTORY has to be defined
    1.45 + (if constant), or this method has to be extended to handle it.
    1.46 +
    1.47 + @param perMachineDir The url to use as the per_machine_directory directory.
    1.48 + */
    1.49 ++ (void)setPerMachineDirectory:(NSURL *)perMachineDir
    1.50  {
    1.51  #if TARGET_OS_IPHONE
    1.52 -    // Set HOME, which is also important for the perUser directory later
    1.53 -    setenv("HOME", [[homeDir path] cStringUsingEncoding:NSUTF8StringEncoding], 1);
    1.54 -
    1.55      if (perMachineDirectory) {
    1.56          free((void *) perMachineDirectory);
    1.57      }
    1.58 -    perMachineDirectory = strdup([homeDir path].UTF8String);
    1.59 +    perMachineDirectory = strdup([perMachineDir path].UTF8String);
    1.60  #endif
    1.61  }
    1.62  
    1.63 @@ -167,4 +189,15 @@
    1.64      [PEPObjCAdapter setupTrustWordsDB:[NSBundle mainBundle]];
    1.65  }
    1.66  
    1.67 +
    1.68 ++ (NSString * _Nonnull)perUserDirectoryString
    1.69 +{
    1.70 +    return [NSString stringWithCString:per_user_directory() encoding:NSUTF8StringEncoding];
    1.71 +}
    1.72 +
    1.73 ++ (NSString * _Nonnull)perMachineDirectoryString
    1.74 +{
    1.75 +    return [NSString stringWithCString:per_machine_directory() encoding:NSUTF8StringEncoding];
    1.76 +}
    1.77 +
    1.78  @end
     2.1 --- a/pEpObjCAdapterFramework/PEPObjCAdapter.h	Wed Sep 04 13:01:54 2019 +0200
     2.2 +++ b/pEpObjCAdapterFramework/PEPObjCAdapter.h	Tue Sep 10 17:13:37 2019 +0200
     2.3 @@ -37,4 +37,19 @@
     2.4  + (void)setupTrustWordsDB;
     2.5  + (void)setupTrustWordsDB:(NSBundle * _Nonnull)rootBundle;
     2.6  
     2.7 +/**
     2.8 + The directory where pEp stores user-specific data.
     2.9 +
    2.10 + @return An NSString denoting the directory where user-specific data gets stored by the engine.
    2.11 + */
    2.12 ++ (NSString * _Nonnull)perUserDirectoryString;
    2.13 +
    2.14 +/**
    2.15 + The directory where pEp stores data for all users on this machine.
    2.16 +
    2.17 + @return An NSString denoting the directory where global data (for all users of this machine
    2.18 +         or device) gets stored by the engine.
    2.19 + */
    2.20 ++ (NSString * _Nonnull)perMachineDirectoryString;
    2.21 +
    2.22  @end
     3.1 --- a/pEpObjCTests/TestUtils/PEPTestUtils.h	Wed Sep 04 13:01:54 2019 +0200
     3.2 +++ b/pEpObjCTests/TestUtils/PEPTestUtils.h	Tue Sep 10 17:13:37 2019 +0200
     3.3 @@ -46,8 +46,6 @@
     3.4  
     3.5  + (void)cleanUp;
     3.6  
     3.7 -+ (NSArray *)pEpWorkFiles;
     3.8 -
     3.9  @end
    3.10  
    3.11  NS_ASSUME_NONNULL_END
     4.1 --- a/pEpObjCTests/TestUtils/PEPTestUtils.m	Wed Sep 04 13:01:54 2019 +0200
     4.2 +++ b/pEpObjCTests/TestUtils/PEPTestUtils.m	Tue Sep 10 17:13:37 2019 +0200
     4.3 @@ -116,64 +116,20 @@
     4.4      return message;
     4.5  }
     4.6  
     4.7 -+ (NSArray *)pEpWorkFiles;
     4.8 -{
     4.9 -    // Only files whose content is affected by tests.
    4.10 -    NSString *home = [[[NSProcessInfo processInfo]environment]objectForKey:@"HOME"];
    4.11 -
    4.12 -    NSArray *baseNames = @[@".pEp_keys", @".pEp_management"];
    4.13 -    NSArray *baseEndings = @[@"db", @"db-shm", @"db-wal"];
    4.14 -
    4.15 -    NSMutableArray *result = [NSMutableArray array];
    4.16 -
    4.17 -    for (NSString *base in baseNames) {
    4.18 -        for (NSString *ending in baseEndings) {
    4.19 -            NSString *filename = [NSString stringWithFormat:@"%@.%@", base, ending];
    4.20 -            [result addObject:[home stringByAppendingPathComponent:filename]];
    4.21 -        }
    4.22 -    }
    4.23 -
    4.24 -    return result;
    4.25 -}
    4.26 -
    4.27  + (void)cleanUp
    4.28  {
    4.29 -    // This triggers setting HOME und GPGHOME in the adapter.
    4.30 -    // Important for tests which do a cleanup on test start.
    4.31 -    [PEPObjCAdapter homeURL];
    4.32 -
    4.33      [PEPSession cleanup];
    4.34  
    4.35 -    for (id path in [self pEpWorkFiles]) {
    4.36 -        [self delFilePath:path];
    4.37 -    }
    4.38 -}
    4.39 +    NSString *homeString = [PEPObjCAdapter perUserDirectoryString];
    4.40 +    NSURL *homeUrl = [NSURL fileURLWithPath:homeString isDirectory:YES];
    4.41  
    4.42 -#pragma mark - PRIVATE
    4.43 -
    4.44 -+ (void)delFilePath:(NSString *)filePath
    4.45 -{
    4.46      NSFileManager *fileManager = [NSFileManager defaultManager];
    4.47 -    NSError *error = nil;
    4.48 -    if ([fileManager fileExistsAtPath:filePath]) {
    4.49 -        BOOL success = [fileManager removeItemAtPath:filePath error:&error];
    4.50 -        if (!success) {
    4.51 -            NSLog(@"Error: %@", [error localizedDescription]);
    4.52 -        }
    4.53 -    }
    4.54 -}
    4.55 -
    4.56 -+ (void)undelFileWithPath:(NSString *)path backup:(NSString *)backup;
    4.57 -{
    4.58 -    NSParameterAssert(backup);
    4.59 -    NSFileManager *fileManager = [NSFileManager defaultManager];
    4.60 -    NSString* bpath = [path stringByAppendingString:backup];
    4.61 -    BOOL fileExists = [fileManager fileExistsAtPath:bpath];
    4.62 -    if (fileExists) {
    4.63 +    NSDirectoryEnumerator<NSString *> *enumerator = [fileManager enumeratorAtPath:homeString];
    4.64 +    for (NSString *path in enumerator) {
    4.65 +        NSURL *fileUrl = [NSURL fileURLWithPath:path isDirectory:NO relativeToURL:homeUrl];
    4.66          NSError *error = nil;
    4.67 -        BOOL success = [fileManager moveItemAtPath:bpath toPath:path error:&error];
    4.68 -        if (!success) {
    4.69 -            NSLog(@"Error: %@", [error localizedDescription]);
    4.70 +        if (![fileManager removeItemAtURL:fileUrl error:&error]) {
    4.71 +            NSLog(@"Error deleting '%@': %@", fileUrl, [error localizedDescription]);
    4.72          }
    4.73      }
    4.74  }