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;
|
|
|
|
};
|
|
|
|
|
|
|
|
@property(readonly) SandboxToken *token;
|
|
|
|
|
|
|
|
@property NSURL *secureUrl;
|
|
|
|
|
|
|
|
@property(readonly) NSString *path;
|
|
|
|
|
|
|
|
@property NSInteger refCount;
|
|
|
|
|
|
|
|
- (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;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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 {
|
|
|
|
return _token.path;
|
|
|
|
}
|
|
|
|
@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;
|
|
|
|
|
|
|
|
NSNumber *isDirectory;
|
|
|
|
|
|
|
|
BOOL success = [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil];
|
|
|
|
|
|
|
|
if(success && [isDirectory boolValue]) return url;
|
|
|
|
|
|
|
|
NSString *s = [url path];
|
|
|
|
|
|
|
|
NSString *lastComponent = [url lastPathComponent];
|
|
|
|
|
|
|
|
// Find that last component in the string from the end to make sure
|
|
|
|
// to get the last one
|
|
|
|
NSRange fragmentRange = [s rangeOfString:lastComponent
|
|
|
|
options:NSBackwardsSearch];
|
|
|
|
|
|
|
|
// Chop the fragment.
|
|
|
|
NSString *newURLString = [s substringToIndex:fragmentRange.location + fragmentRange.length];
|
|
|
|
|
|
|
|
return [NSURL fileURLWithPath:newURLString];
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
for(SandboxEntry *entry in storage) {
|
2022-06-21 23:12:28 +00:00
|
|
|
if(entry.path && [SandboxBroker isPath:url aSubdirectoryOf:[NSURL fileURLWithPath:entry.path]]) {
|
2022-06-21 05:10:43 +00:00
|
|
|
entry.refCount += 1;
|
|
|
|
return entry;
|
2022-06-20 10:35:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 07:01:07 +00:00
|
|
|
NSPersistentContainer *pc = [SandboxBroker sharedPersistentContainer];
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"path.length" ascending:NO];
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"SandboxToken"];
|
|
|
|
request.sortDescriptors = @[sortDescriptor];
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
NSError *error = nil;
|
|
|
|
NSArray *results = [pc.viewContext executeFetchRequest:request error:&error];
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
if(results && [results count] > 0) {
|
|
|
|
for(SandboxToken *token in results) {
|
2022-06-21 23:12:28 +00:00
|
|
|
if(token.path && [SandboxBroker isPath:url aSubdirectoryOf:[NSURL fileURLWithPath:token.path]]) {
|
2022-06-21 05:10:43 +00:00
|
|
|
SandboxEntry *entry = [[SandboxEntry alloc] initWithToken:token];
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
BOOL isStale;
|
|
|
|
NSError *err = nil;
|
|
|
|
NSURL *secureUrl = [NSURL URLByResolvingBookmarkData:token.bookmark options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:&isStale error:&err];
|
|
|
|
if(!secureUrl && err) {
|
|
|
|
ALog(@"Failed to access bookmark for URL: %@, error: %@", token.path, [err localizedDescription]);
|
|
|
|
return nil;
|
|
|
|
}
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
entry.secureUrl = secureUrl;
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2022-06-21 23:12:28 +00:00
|
|
|
[storage addObject:entry];
|
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
[secureUrl startAccessingSecurityScopedResource];
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
return entry;
|
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-21 05:10:43 +00:00
|
|
|
- (const void *)beginFolderAccess:(NSURL *)fileUrl {
|
2022-06-21 09:45:45 +00:00
|
|
|
NSURL *folderUrl = [[SandboxBroker urlWithoutFragment:fileUrl] URLByDeletingLastPathComponent];
|
2022-06-21 05:10:43 +00:00
|
|
|
if(![folderUrl isFileURL]) return NULL;
|
|
|
|
|
|
|
|
SandboxEntry *entry;
|
2022-06-20 10:35:29 +00:00
|
|
|
|
|
|
|
@synchronized(self) {
|
2022-06-21 05:10:43 +00:00
|
|
|
entry = [self recursivePathTest:folderUrl];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(entry) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|