Skip to content

Commit 8183287

Browse files
authored
Robustified the pref file URL creation. (#512)
I made the Branch preference directory path URL more robust. I added: More checks in code for failure and better recovery. I added _Nonnull for compiler level checking of these code paths. A worst case backup plan if normal Branch prefs directory creation fails. n.b.: Storyboards were updated so that Xcode 7 could be tested. Since we don't know the underlying problem this fix may not help but certainly won't hurt. I tested this code on iOS 7/8/9/10 on iOS devices, and with Xcode 7 and 8. I tested the worst case option by forcing failure in the normal code path with the debugger.
1 parent 89a5f67 commit 8183287

File tree

4 files changed

+204
-181
lines changed

4 files changed

+204
-181
lines changed

Branch-SDK/Branch-SDK/BNCPreferenceHelper.m

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -717,35 +717,64 @@ + (NSString *)prefsFile_deprecated {
717717
return path;
718718
}
719719

720-
+ (NSURL*) URLForBranchDirectory {
720+
+ (NSURL* _Nonnull) URLForBranchDirectory {
721+
NSSearchPathDirectory kSearchDirectories[] = {
722+
NSApplicationSupportDirectory,
723+
NSCachesDirectory,
724+
NSDocumentDirectory,
725+
};
726+
727+
#define _countof(array) (sizeof(array)/sizeof(array[0]))
728+
729+
for (NSSearchPathDirectory directory = 0; directory < _countof(kSearchDirectories); directory++) {
730+
NSURL *URL = [self createDirectoryForBranchURLWithPath:kSearchDirectories[directory]];
731+
if (URL) return URL;
732+
}
733+
734+
#undef _countof
735+
736+
// Worst case backup plan:
737+
NSString *path = [@"~/Library/io.branch" stringByExpandingTildeInPath];
738+
NSURL *branchURL = [NSURL fileURLWithPath:path isDirectory:YES];
739+
NSFileManager *fileManager = [NSFileManager defaultManager];
721740
NSError *error = nil;
722-
NSURL *URL =
723-
[[NSFileManager defaultManager]
724-
URLForDirectory:NSApplicationSupportDirectory
725-
inDomain:NSUserDomainMask
726-
appropriateForURL:nil
727-
create:YES
741+
BOOL success =
742+
[fileManager
743+
createDirectoryAtURL:branchURL
744+
withIntermediateDirectories:YES
745+
attributes:nil
728746
error:&error];
729-
if (error) {
730-
NSLog(@"Error creating URLForPrefsDirectory: %@.", error);
731-
return nil;
747+
if (!success) {
748+
NSLog(@"Worst case CreateBranchURL error: %@ URL: %@.", error, branchURL);
732749
}
733-
URL = [URL URLByAppendingPathComponent:@"io.branch"];
734-
[[NSFileManager defaultManager]
735-
createDirectoryAtURL:URL
736-
withIntermediateDirectories:YES
737-
attributes:nil
738-
error:&error];
739-
if (error) {
740-
NSLog(@"Error creating URLForPrefsDirectory: %@.", error);
741-
return nil;
750+
return branchURL;
751+
}
752+
753+
+ (NSURL* _Null_unspecified) createDirectoryForBranchURLWithPath:(NSSearchPathDirectory)directory {
754+
NSFileManager *fileManager = [NSFileManager defaultManager];
755+
NSArray *URLs = [fileManager URLsForDirectory:directory inDomains:NSUserDomainMask | NSLocalDomainMask];
756+
757+
for (NSURL *URL in URLs) {
758+
NSError *error = nil;
759+
NSURL *branchURL = [URL URLByAppendingPathComponent:@"io.branch" isDirectory:YES];
760+
BOOL success =
761+
[fileManager
762+
createDirectoryAtURL:branchURL
763+
withIntermediateDirectories:YES
764+
attributes:nil
765+
error:&error];
766+
if (success) {
767+
return branchURL;
768+
} else {
769+
NSLog(@"CreateBranchURL error: %@ URL: %@.", error, branchURL);
770+
}
742771
}
743-
return URL;
772+
return nil;
744773
}
745774

746-
+ (NSURL*) URLForPrefsFile {
775+
+ (NSURL* _Nonnull) URLForPrefsFile {
747776
NSURL *URL = [self URLForBranchDirectory];
748-
URL = [URL URLByAppendingPathComponent:BRANCH_PREFS_FILE];
777+
URL = [URL URLByAppendingPathComponent:BRANCH_PREFS_FILE isDirectory:NO];
749778
return URL;
750779
}
751780

Branch-SDK/Branch-SDK/BNCServerRequestQueue.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,9 @@ + (NSString *)queueFile_deprecated {
291291
return path;
292292
}
293293

294-
+ (NSURL*) URLForQueueFile {
294+
+ (NSURL* _Nonnull) URLForQueueFile {
295295
NSURL *URL = [BNCPreferenceHelper URLForBranchDirectory];
296-
URL = [URL URLByAppendingPathComponent:BRANCH_QUEUE_FILE];
296+
URL = [URL URLByAppendingPathComponent:BRANCH_QUEUE_FILE isDirectory:NO];
297297
return URL;
298298
}
299299

0 commit comments

Comments
 (0)