Skip to content

Commit

Permalink
Allow renaming filesystems
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed Sep 27, 2020
1 parent f126b23 commit fba8a02
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 83 deletions.
25 changes: 1 addition & 24 deletions app/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,7 @@ static void ios_handle_die(const char *msg) {
@implementation AppDelegate

- (int)boot {
NSFileManager *manager = [NSFileManager defaultManager];
NSURL *container = ContainerURL();
NSURL *roots = [container URLByAppendingPathComponent:@"roots"];
NSURL *root = [roots URLByAppendingPathComponent:Roots.instance.defaultRoot];

#if 0
// copy the files to the app container so I can more easily get them out
NSURL *documents = [NSFileManager.defaultManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0];
[NSFileManager.defaultManager removeItemAtURL:[documents URLByAppendingPathComponent:@"roots copy"] error:nil];
[NSFileManager.defaultManager copyItemAtURL:[container URLByAppendingPathComponent:@"roots"]
toURL:[documents URLByAppendingPathComponent:@"roots copy"]
error:nil];
#endif

if (![manager fileExistsAtPath:root.path]) {
NSURL *alpineMaster = [NSBundle.mainBundle URLForResource:@"alpine" withExtension:nil];
NSError *error = nil;
[manager copyItemAtURL:alpineMaster toURL:root error:&error];
if (error != nil) {
NSLog(@"%@", error);
exit(1);
}
}
root = [root URLByAppendingPathComponent:@"data"];
NSURL *root = [[Roots.instance rootUrl:Roots.instance.defaultRoot] URLByAppendingPathComponent:@"data"];
int err = mount_root(&fakefs, root.fileSystemRepresentation);
if (err < 0)
return err;
Expand Down
2 changes: 2 additions & 0 deletions app/Roots.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ NS_ASSUME_NONNULL_BEGIN

@property (readonly) NSOrderedSet<NSString *> *roots;
@property NSString *defaultRoot;
- (NSURL *)rootUrl:(NSString *)name;
- (BOOL)importRootFromArchive:(NSURL *)archive name:(NSString *)name error:(NSError **)error progressReporter:(id<ProgressReporter> _Nullable)progress;
- (BOOL)exportRootNamed:(NSString *)name toArchive:(NSURL *)archive error:(NSError **)error progressReporter:(id<ProgressReporter> _Nullable)progress;
- (BOOL)destroyRootNamed:(NSString *)name error:(NSError **)error;
- (BOOL)renameRoot:(NSString *)name toName:(NSString *)newName error:(NSError **)error;

@end

Expand Down
33 changes: 27 additions & 6 deletions app/Roots.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
}
}

- (NSURL *)rootUrl:(NSString *)name {
return [RootsDir() URLByAppendingPathComponent:name];
}

- (void)syncFileProviderDomains {
if (self.updatingDomains) {
self.domainsNeedUpdate = YES;
Expand Down Expand Up @@ -119,7 +123,7 @@ void root_progress_callback(void *cookie, double progress, const char *message,
- (BOOL)importRootFromArchive:(NSURL *)archive name:(NSString *)name error:(NSError **)error progressReporter:(id<ProgressReporter> _Nullable)progress {
NSAssert(![self.roots containsObject:name], @"root already exists: %@", name);
struct fakefsify_error fs_err;
NSURL *destination = [RootsDir() URLByAppendingPathComponent:name];
NSURL *destination = [self rootUrl:name];
NSURL *tempDestination = [NSFileManager.defaultManager.temporaryDirectory
URLByAppendingPathComponent:[NSProcessInfo.processInfo globallyUniqueString]];
if (tempDestination == nil)
Expand Down Expand Up @@ -156,7 +160,7 @@ - (BOOL)importRootFromArchive:(NSURL *)archive name:(NSString *)name error:(NSEr
- (BOOL)exportRootNamed:(NSString *)name toArchive:(NSURL *)archive error:(NSError **)error progressReporter:(id<ProgressReporter> _Nullable)progress {
NSAssert([self.roots containsObject:name], @"trying to export a root that doesn't exist: %@", name);
struct fakefsify_error fs_err;
if (!fakefs_export([RootsDir() URLByAppendingPathComponent:name].fileSystemRepresentation,
if (!fakefs_export([self rootUrl:name].fileSystemRepresentation,
archive.fileSystemRepresentation,
&fs_err, (struct progress) {(__bridge void *) progress, root_progress_callback})) {
// TODO: dedup with above method
Expand All @@ -175,18 +179,35 @@ - (BOOL)exportRootNamed:(NSString *)name toArchive:(NSURL *)archive error:(NSErr
}

- (BOOL)destroyRootNamed:(NSString *)name error:(NSError **)error {
if (name == self.defaultRoot) {
*error = [NSError errorWithDomain:@"iSH" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Cannot delete the default root"}];
if ([name isEqualToString:self.defaultRoot]) {
*error = [NSError errorWithDomain:@"iSH" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Cannot delete the default filesystem"}];
return NO;
}
NSAssert([self.roots containsObject:name], @"root does not exist: %@", name);
NSURL *rootUrl = [RootsDir() URLByAppendingPathComponent:name];
if (![NSFileManager.defaultManager removeItemAtURL:rootUrl error:error])
if (![NSFileManager.defaultManager removeItemAtURL:[self rootUrl:name] error:error])
return NO;
[[self mutableOrderedSetValueForKey:@"roots"] removeObject:name];
return YES;
}

- (BOOL)renameRoot:(NSString *)name toName:(NSString *)newName error:(NSError **)error {
if (name.length == 0) {
*error = [NSError errorWithDomain:@"iSH" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Filesystem name can't be empty"}];
return NO;
}
if ([name isEqualToString:self.defaultRoot]) {
*error = [NSError errorWithDomain:@"iSH" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Cannot rename the default filesystem"}];
return NO;
}
NSAssert([self.roots containsObject:name], @"root does not exist: %@", name);

if (![NSFileManager.defaultManager moveItemAtURL:[self rootUrl:name] toURL:[self rootUrl:newName] error:error])
return NO;
NSUInteger index = [self.roots indexOfObject:name];
[[self mutableOrderedSetValueForKey:@"roots"] replaceObjectAtIndex:index withObject:newName];
return YES;
}

+ (instancetype)instance {
static Roots *instance;
static dispatch_once_t token;
Expand Down
Loading

0 comments on commit fba8a02

Please sign in to comment.