2022-06-20 10:35:29 +00:00
|
|
|
//
|
|
|
|
// SandboxBroker.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Christopher Snowhill on 6/20/22.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
#import "SandboxBroker.h"
|
|
|
|
|
|
|
|
#import "Logging.h"
|
|
|
|
|
|
|
|
#import "Cog-Swift.h"
|
|
|
|
|
|
|
|
#import "PlaylistController.h"
|
|
|
|
|
2022-06-21 07:01:07 +00:00
|
|
|
static SandboxBroker *kSharedSandboxBroker = nil;
|
2022-06-20 10:35:29 +00:00
|
|
|
|
|
|
|
@interface SandboxEntry : NSObject {
|
|
|
|
SandboxToken *_token;
|
|
|
|
NSInteger _refCount;
|
|
|
|
NSURL *_secureUrl;
|
2022-06-27 06:52:38 +00:00
|
|
|
NSString *_path;
|
2022-06-29 18:56:50 +00:00
|
|
|
BOOL _isFolder;
|
2022-06-20 10:35:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
@property(readonly) SandboxToken *token;
|
|
|
|
|
|
|
|
@property NSURL *secureUrl;
|
|
|
|
|
|
|
|
@property(readonly) NSString *path;
|
|
|
|
|
|
|
|
@property NSInteger refCount;
|
|
|
|
|
2022-06-29 18:56:50 +00:00
|
|
|
@property(readonly) BOOL isFolder;
|
|
|
|
|
2022-06-20 10:35:29 +00:00
|
|
|
- (id)initWithToken:(SandboxToken *)token;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SandboxEntry
|
|
|
|
- (id)initWithToken:(SandboxToken *)token {
|
|
|
|
SandboxEntry *obj = [super init];
|
|
|
|
if(obj) {
|
|
|
|
obj->_refCount = 1;
|
|
|
|
obj->_secureUrl = nil;
|
|
|
|
obj->_token = token;
|
2022-06-27 06:52:38 +00:00
|
|
|
obj->_path = token.path;
|
2022-06-29 18:56:50 +00:00
|
|
|
obj->_isFolder = token.folder;
|
2022-06-27 06:52:38 +00:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2022-06-20 10:35:29 +00:00
|
|
|
- (NSInteger)refCount {
|
|
|
|
return _refCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setRefCount:(NSInteger)refCount {
|
|
|
|
_refCount = refCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSURL *)secureUrl {
|
|
|
|
return _secureUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setSecureUrl:(NSURL *)url {
|
|
|
|
_secureUrl = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (SandboxToken *)token {
|
|
|
|
return _token;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)path {
|
2022-06-27 06:52:38 +00:00
|
|
|
return _path;
|
2022-06-20 10:35:29 +00:00
|
|
|
}
|
2022-06-29 18:56:50 +00:00
|
|
|
|
|
|
|
- (BOOL)isFolder {
|
|
|
|
return _isFolder;
|
|
|
|
}
|
2022-06-20 10:35:29 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SandboxBroker
|
|
|
|
|
|
|
|
+ (id)sharedSandboxBroker {
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
2022-06-21 07:01:07 +00:00
|
|
|
kSharedSandboxBroker = [[self alloc] init];
|
2022-06-20 10:35:29 +00:00
|
|
|
});
|
2022-06-21 07:01:07 +00:00
|
|
|
return kSharedSandboxBroker;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSPersistentContainer *)sharedPersistentContainer {
|
|
|
|
return [NSClassFromString(@"PlaylistController") sharedPersistentContainer];
|
2022-06-20 10:35:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 09:45:45 +00:00
|
|
|
+ (NSURL *)urlWithoutFragment:(NSURL *)url {
|
|
|
|
if(![url isFileURL]) return url;
|
|
|
|
|
|
|
|
NSString *s = [url path];
|
|
|
|
|
2022-06-30 02:39:13 +00:00
|
|
|
NSRange fragmentRange = [s rangeOfString:@"#"
|
|
|
|
options:NSBackwardsSearch];
|
2022-06-21 09:45:45 +00:00
|
|
|
|
2022-06-30 02:39:13 +00:00
|
|
|
if(fragmentRange.location != NSNotFound) {
|
2022-06-29 18:56:50 +00:00
|
|
|
// Chop the fragment.
|
2022-06-30 02:39:13 +00:00
|
|
|
NSString *newURLString = [s substringToIndex:fragmentRange.location];
|
2022-06-21 09:45:45 +00:00
|
|
|
|
2022-06-29 18:56:50 +00:00
|
|
|
return [NSURL fileURLWithPath:newURLString];
|
|
|
|
} else {
|
|
|
|
return url;
|
|
|
|
}
|
2022-06-21 09:45:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 10:35:29 +00:00
|
|
|
- (id)init {
|
|
|
|
id _self = [super init];
|
|
|
|
if(_self) {
|
|
|
|
storage = [[NSMutableArray alloc] init];
|
|
|
|
}
|
|
|
|
|
|
|
|
return _self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)shutdown {
|
|
|
|
for(SandboxEntry *obj in storage) {
|
|
|
|
if([obj secureUrl]) {
|
|
|
|
[[obj secureUrl] stopAccessingSecurityScopedResource];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
+ (BOOL)isPath:(NSURL *)path aSubdirectoryOf:(NSURL *)directory {
|
|
|
|
NSArray *pathComponents = [path pathComponents];
|
|
|
|
NSArray *directoryComponents = [directory pathComponents];
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
if([pathComponents count] < [directoryComponents count])
|
|
|
|
return NO;
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
for(size_t i = 0; i < [directoryComponents count]; ++i) {
|
|
|
|
if(![pathComponents[i] isEqualToString:directoryComponents[i]])
|
|
|
|
return NO;
|
|
|
|
}
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
return YES;
|
|
|
|
}
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
- (SandboxEntry *)recursivePathTest:(NSURL *)url {
|
2022-06-24 21:58:56 +00:00
|
|
|
SandboxEntry *ret = nil;
|
2022-06-24 06:35:26 +00:00
|
|
|
|
2022-06-24 21:58:56 +00:00
|
|
|
NSPersistentContainer *pc = [SandboxBroker sharedPersistentContainer];
|
2022-06-29 18:56:50 +00:00
|
|
|
|
|
|
|
NSPredicate *folderPredicate = [NSPredicate predicateWithFormat:@"folder == NO"];
|
|
|
|
NSPredicate *filePredicate = [NSPredicate predicateWithFormat:@"path == %@", [url path]];
|
|
|
|
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[folderPredicate, filePredicate]];
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2022-06-24 21:58:56 +00:00
|
|
|
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"SandboxToken"];
|
2022-06-29 18:56:50 +00:00
|
|
|
request.predicate = predicate;
|
|
|
|
|
2022-06-24 21:58:56 +00:00
|
|
|
NSError *error = nil;
|
|
|
|
NSArray *results = [pc.viewContext executeFetchRequest:request error:&error];
|
|
|
|
if(results && [results count] > 0) {
|
2022-06-29 18:56:50 +00:00
|
|
|
ret = [[SandboxEntry alloc] initWithToken:results[0]];
|
|
|
|
}
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2022-06-29 18:56:50 +00:00
|
|
|
if(!ret) {
|
|
|
|
predicate = [NSPredicate predicateWithFormat:@"folder == YES"];
|
|
|
|
|
|
|
|
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"path.length" ascending:NO];
|
|
|
|
|
|
|
|
request = [NSFetchRequest fetchRequestWithEntityName:@"SandboxToken"];
|
|
|
|
request.sortDescriptors = @[sortDescriptor];
|
|
|
|
request.predicate = predicate;
|
|
|
|
|
|
|
|
error = nil;
|
|
|
|
results = [pc.viewContext executeFetchRequest:request error:&error];
|
|
|
|
if(results) results = [results copy];
|
|
|
|
|
|
|
|
if(results && [results count] > 0) {
|
|
|
|
for(SandboxToken *token in results) {
|
|
|
|
if(token.path && [SandboxBroker isPath:url aSubdirectoryOf:[NSURL fileURLWithPath:token.path]]) {
|
|
|
|
SandboxEntry *entry = [[SandboxEntry alloc] initWithToken:token];
|
|
|
|
|
|
|
|
ret = entry;
|
|
|
|
break;
|
|
|
|
}
|
2022-06-24 06:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-24 21:58:56 +00:00
|
|
|
}
|
2022-06-24 06:35:26 +00:00
|
|
|
|
|
|
|
if(ret) {
|
|
|
|
BOOL isStale;
|
|
|
|
NSError *err = nil;
|
|
|
|
NSURL *secureUrl = [NSURL URLByResolvingBookmarkData:ret.token.bookmark options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:&isStale error:&err];
|
|
|
|
if(!secureUrl && err) {
|
|
|
|
ALog(@"Failed to access bookmark for URL: %@, error: %@", ret.token.path, [err localizedDescription]);
|
|
|
|
return nil;
|
|
|
|
}
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2022-06-24 06:35:26 +00:00
|
|
|
ret.secureUrl = secureUrl;
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2022-06-24 06:35:26 +00:00
|
|
|
return ret;
|
2022-06-20 10:35:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
return nil;
|
2022-06-20 10:35:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 02:41:03 +00:00
|
|
|
static inline void dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_block_t block) {
|
|
|
|
if(dispatch_queue_get_label(queue) == dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)) {
|
|
|
|
block();
|
|
|
|
} else {
|
|
|
|
dispatch_sync(queue, block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 06:13:42 +00:00
|
|
|
- (void)addFolderIfMissing:(NSURL *)folderUrl {
|
|
|
|
if(![folderUrl isFileURL]) return;
|
|
|
|
|
|
|
|
@synchronized (self) {
|
|
|
|
SandboxEntry *_entry = nil;
|
|
|
|
|
|
|
|
for(SandboxEntry *entry in storage) {
|
2022-06-29 18:56:50 +00:00
|
|
|
if(entry.path && entry.isFolder && [SandboxBroker isPath:folderUrl aSubdirectoryOf:[NSURL fileURLWithPath:entry.path]]) {
|
2022-06-29 06:13:42 +00:00
|
|
|
_entry = entry;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_entry) {
|
|
|
|
_entry = [self recursivePathTest:folderUrl];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_entry) {
|
|
|
|
NSError *err = nil;
|
|
|
|
NSData *bookmark = [folderUrl bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:&err];
|
|
|
|
if(!bookmark && err) {
|
|
|
|
ALog(@"Failed to add bookmark for URL: %@, with error: %@", folderUrl, [err localizedDescription]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-30 02:41:03 +00:00
|
|
|
dispatch_sync_reentrant(dispatch_get_main_queue(), ^{
|
|
|
|
NSPersistentContainer *pc = [NSClassFromString(@"PlaylistController") sharedPersistentContainer];
|
2022-06-29 06:13:42 +00:00
|
|
|
|
2022-06-30 02:41:03 +00:00
|
|
|
SandboxToken *token = [NSEntityDescription insertNewObjectForEntityForName:@"SandboxToken" inManagedObjectContext:pc.viewContext];
|
2022-06-29 06:13:42 +00:00
|
|
|
|
2022-06-30 02:41:03 +00:00
|
|
|
if(token) {
|
|
|
|
token.path = [folderUrl path];
|
|
|
|
token.bookmark = bookmark;
|
2022-06-29 06:13:42 +00:00
|
|
|
}
|
2022-06-30 02:41:03 +00:00
|
|
|
});
|
2022-06-29 06:13:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 18:56:50 +00:00
|
|
|
- (void)addFileIfMissing:(NSURL *)fileUrl {
|
|
|
|
if(![fileUrl isFileURL]) return;
|
|
|
|
|
|
|
|
NSURL *url = [SandboxBroker urlWithoutFragment:fileUrl];
|
|
|
|
|
|
|
|
@synchronized (self) {
|
|
|
|
SandboxEntry *_entry = nil;
|
|
|
|
|
|
|
|
for(SandboxEntry *entry in storage) {
|
|
|
|
if(entry.path) {
|
|
|
|
if((entry.isFolder && [SandboxBroker isPath:url aSubdirectoryOf:[NSURL fileURLWithPath:entry.path]]) ||
|
|
|
|
(!entry.isFolder && [url isEqualTo:[NSURL fileURLWithPath:entry.path]])) {
|
|
|
|
_entry = entry;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_entry) {
|
|
|
|
_entry = [self recursivePathTest:url];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_entry) {
|
|
|
|
NSError *err = nil;
|
|
|
|
NSData *bookmark = [fileUrl bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:&err];
|
|
|
|
if(!bookmark && err) {
|
|
|
|
ALog(@"Failed to add bookmark for URL: %@, with error: %@", url, [err localizedDescription]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSPersistentContainer *pc = [NSClassFromString(@"PlaylistController") sharedPersistentContainer];
|
|
|
|
|
2022-06-30 02:41:03 +00:00
|
|
|
dispatch_sync_reentrant(dispatch_get_main_queue(), ^{
|
|
|
|
SandboxToken *token = [NSEntityDescription insertNewObjectForEntityForName:@"SandboxToken" inManagedObjectContext:pc.viewContext];
|
2022-06-29 18:56:50 +00:00
|
|
|
|
2022-06-30 02:41:03 +00:00
|
|
|
if(token) {
|
|
|
|
token.path = [url path];
|
|
|
|
token.bookmark = bookmark;
|
|
|
|
token.folder = NO;
|
2022-06-29 18:56:50 +00:00
|
|
|
}
|
2022-06-30 02:41:03 +00:00
|
|
|
});
|
2022-06-29 18:56:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 05:37:11 +00:00
|
|
|
- (void)requestFolderForFile:(NSURL *)fileUrl {
|
|
|
|
if(![fileUrl isFileURL]) return;
|
|
|
|
NSURL *folderUrl = [fileUrl URLByDeletingLastPathComponent];
|
|
|
|
|
|
|
|
@synchronized(self) {
|
|
|
|
SandboxEntry *_entry = nil;
|
|
|
|
|
|
|
|
for(SandboxEntry *entry in storage) {
|
|
|
|
if(entry.path && entry.isFolder && [SandboxBroker isPath:folderUrl aSubdirectoryOf:[NSURL fileURLWithPath:entry.path]]) {
|
|
|
|
_entry = entry;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_entry) {
|
|
|
|
_entry = [self recursivePathTest:folderUrl];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_entry) {
|
|
|
|
dispatch_sync_reentrant(dispatch_get_main_queue(), ^{
|
|
|
|
NSOpenPanel *panel = [NSOpenPanel openPanel];
|
|
|
|
[panel setAllowsMultipleSelection:NO];
|
|
|
|
[panel setCanChooseDirectories:YES];
|
|
|
|
[panel setCanChooseFiles:NO];
|
|
|
|
[panel setFloatingPanel:YES];
|
|
|
|
[panel setDirectoryURL:folderUrl];
|
|
|
|
[panel setTitle:@"Open to grant access to container folder"];
|
|
|
|
NSInteger result = [panel runModal];
|
|
|
|
if(result == NSModalResponseOK) {
|
|
|
|
NSURL *folderUrl = [panel URL];
|
|
|
|
NSError *err = nil;
|
|
|
|
NSData *bookmark = [folderUrl bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:&err];
|
|
|
|
if(!bookmark && err) {
|
|
|
|
ALog(@"Failed to add bookmark for URL: %@, with error: %@", folderUrl, [err localizedDescription]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSPersistentContainer *pc = [NSClassFromString(@"PlaylistController") sharedPersistentContainer];
|
|
|
|
|
|
|
|
SandboxToken *token = [NSEntityDescription insertNewObjectForEntityForName:@"SandboxToken" inManagedObjectContext:pc.viewContext];
|
|
|
|
|
|
|
|
if(token) {
|
|
|
|
token.path = [folderUrl path];
|
|
|
|
token.bookmark = bookmark;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
- (const void *)beginFolderAccess:(NSURL *)fileUrl {
|
2022-06-29 18:56:50 +00:00
|
|
|
NSURL *folderUrl = [SandboxBroker urlWithoutFragment:fileUrl];
|
2022-06-21 05:10:43 +00:00
|
|
|
if(![folderUrl isFileURL]) return NULL;
|
|
|
|
|
2022-06-24 07:29:50 +00:00
|
|
|
SandboxEntry *_entry = nil;
|
2022-06-29 18:56:50 +00:00
|
|
|
|
|
|
|
NSString *sandboxPath = [folderUrl path];
|
2022-06-20 10:35:29 +00:00
|
|
|
|
|
|
|
@synchronized(self) {
|
2022-06-24 07:29:50 +00:00
|
|
|
for(SandboxEntry *entry in storage) {
|
2022-06-29 18:56:50 +00:00
|
|
|
if(entry.path) {
|
|
|
|
if((entry.isFolder && [SandboxBroker isPath:folderUrl aSubdirectoryOf:[NSURL fileURLWithPath:entry.path]]) ||
|
|
|
|
(!entry.isFolder && [entry.path isEqualToString:sandboxPath])) {
|
|
|
|
entry.refCount += 1;
|
|
|
|
_entry = entry;
|
|
|
|
break;
|
|
|
|
}
|
2022-06-24 07:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_entry) {
|
|
|
|
_entry = [self recursivePathTest:folderUrl];
|
|
|
|
}
|
2022-06-21 05:10:43 +00:00
|
|
|
|
2022-06-27 08:00:11 +00:00
|
|
|
if(_entry) {
|
|
|
|
[storage addObject:_entry];
|
2022-06-24 07:29:50 +00:00
|
|
|
|
2022-06-27 08:00:11 +00:00
|
|
|
if(_entry.secureUrl) {
|
|
|
|
[_entry.secureUrl startAccessingSecurityScopedResource];
|
|
|
|
}
|
2022-06-24 07:29:50 +00:00
|
|
|
|
2022-06-27 08:00:11 +00:00
|
|
|
return CFBridgingRetain(_entry);
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-06-20 10:35:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
- (void)endFolderAccess:(const void *)handle {
|
|
|
|
if(!handle) return;
|
|
|
|
SandboxEntry *entry = CFBridgingRelease(handle);
|
|
|
|
if(!entry) return;
|
2022-06-20 10:35:29 +00:00
|
|
|
|
|
|
|
@synchronized(self) {
|
2022-06-21 05:10:43 +00:00
|
|
|
if(entry.refCount > 1) {
|
|
|
|
entry.refCount -= 1;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if(entry.secureUrl) {
|
|
|
|
[entry.secureUrl stopAccessingSecurityScopedResource];
|
|
|
|
entry.secureUrl = nil;
|
|
|
|
}
|
|
|
|
entry.refCount = 0;
|
|
|
|
|
|
|
|
[storage removeObject:entry];
|
|
|
|
}
|
2022-06-20 10:35:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-24 07:29:50 +00:00
|
|
|
- (BOOL)areAllPathsSafe:(NSArray *)urls {
|
|
|
|
for(NSURL *url in urls) {
|
|
|
|
if(![self recursivePathTest:url]) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2022-06-20 10:35:29 +00:00
|
|
|
@end
|