From c2ef7d0e61ce9bc764a8ca1fbccf719efc3ab46b Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Sun, 26 Jun 2022 02:56:44 -0700 Subject: [PATCH] [Sandbox] Compare to the actual user paths Remove the sandbox reference, because the user will add folders outside the sandbox, and we have entitlements to access these folders. Signed-off-by: Christopher Snowhill --- FileTree/FileTreeDataSource.m | 16 ++++++++----- Preferences/Preferences/PathSuggester.m | 30 ++++++++++++------------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/FileTree/FileTreeDataSource.m b/FileTree/FileTreeDataSource.m index ac329dd06..3eea73def 100644 --- a/FileTree/FileTreeDataSource.m +++ b/FileTree/FileTreeDataSource.m @@ -19,12 +19,18 @@ static void *kFileTreeDataSourceContext = &kFileTreeDataSourceContext; +// XXX this is only for reference, we have the entitlement for the path anyway +static NSURL *pathEscape(NSString *path) { + NSString *componentsToRemove = [NSString stringWithFormat:@"Library/Containers/%@/Data/", [[NSBundle mainBundle] bundleIdentifier]]; + NSRange rangeOfMatch = [path rangeOfString:componentsToRemove]; + if(rangeOfMatch.location != NSNotFound) + path = [path stringByReplacingCharactersInRange:rangeOfMatch withString:@""]; + return [NSURL fileURLWithPath:path]; +} + static NSURL *defaultMusicDirectory(void) { - return [[NSFileManager defaultManager] URLForDirectory:NSMusicDirectory - inDomain:NSUserDomainMask - appropriateForURL:nil - create:NO - error:nil]; + NSString *path = [NSSearchPathForDirectoriesInDomains(NSMusicDirectory, NSUserDomainMask, YES) lastObject]; + return pathEscape(path); } @interface FileTreeDataSource () diff --git a/Preferences/Preferences/PathSuggester.m b/Preferences/Preferences/PathSuggester.m index 1391c4066..a55b9e7d4 100644 --- a/Preferences/Preferences/PathSuggester.m +++ b/Preferences/Preferences/PathSuggester.m @@ -18,28 +18,28 @@ @property(nonatomic) NSURL *_Nullable url; @end +// XXX this is only for comparison, not "escaping the sandbox" +static NSURL *pathEscape(NSString *path) { + NSString *componentsToRemove = [NSString stringWithFormat:@"Library/Containers/%@/Data/", [[NSBundle mainBundle] bundleIdentifier]]; + NSRange rangeOfMatch = [path rangeOfString:componentsToRemove]; + if(rangeOfMatch.location != NSNotFound) + path = [path stringByReplacingCharactersInRange:rangeOfMatch withString:@""]; + return [NSURL fileURLWithPath:path]; +} + static NSURL *defaultMusicDirectory(void) { - return [[NSFileManager defaultManager] URLForDirectory:NSMusicDirectory - inDomain:NSUserDomainMask - appropriateForURL:nil - create:NO - error:nil]; + NSString *path = [NSSearchPathForDirectoriesInDomains(NSMusicDirectory, NSUserDomainMask, YES) lastObject]; + return pathEscape(path); } static NSURL *defaultDownloadsDirectory(void) { - return [[NSFileManager defaultManager] URLForDirectory:NSDownloadsDirectory - inDomain:NSUserDomainMask - appropriateForURL:nil - create:NO - error:nil]; + NSString *path = [NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES) lastObject]; + return pathEscape(path); } static NSURL *defaultMoviesDirectory(void) { - return [[NSFileManager defaultManager] URLForDirectory:NSMoviesDirectory - inDomain:NSUserDomainMask - appropriateForURL:nil - create:NO - error:nil]; + NSString *path = [NSSearchPathForDirectoriesInDomains(NSMoviesDirectory, NSUserDomainMask, YES) lastObject]; + return pathEscape(path); } @interface PathItem : NSObject