From 80a9cce27ceaeeb3c8002c17ce638ed45410d3e6 Mon Sep 17 00:00:00 2001 From: William Glover Date: Mon, 17 Aug 2020 11:27:36 +0100 Subject: [PATCH] Fix for issue #190 Remove beginning of path before creating serviceId to avoid 'path too long' error. Remove fileExistsAtPath check before removing file, as it is not reliable. If the file does not exist then let the remove function fail and carry on. --- LiquidCore/src/ios/API/LCMicroService.m | 44 +++++++++++-------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/LiquidCore/src/ios/API/LCMicroService.m b/LiquidCore/src/ios/API/LCMicroService.m index 1f6635fa87..8fca999bb5 100644 --- a/LiquidCore/src/ios/API/LCMicroService.m +++ b/LiquidCore/src/ios/API/LCMicroService.m @@ -40,11 +40,14 @@ + (id) serviceFromInstanceId:(NSString*)instanceId + (void) uninstall:(NSURL *)serviceURI { NSString *serviceId; - NSRange comp = [[serviceURI absoluteString] rangeOfString:@"/" options:NSBackwardsSearch]; + NSString *serviceString = [serviceURI absoluteString]; + NSString *bundleString = NSBundle.mainBundle.bundleURL.absoluteString; + serviceString = [serviceString stringByReplacingOccurrencesOfString:bundleString withString:@""]; + NSRange comp = [serviceString rangeOfString:@"/" options:NSBackwardsSearch]; if (comp.location != NSNotFound) { - serviceId = [[serviceURI absoluteString] substringToIndex:comp.location]; + serviceId = [serviceString substringToIndex:comp.location]; } else { - serviceId = [serviceURI absoluteString]; + serviceId = serviceString; } serviceId = [serviceId stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLHostAllowedCharacterSet]]; @@ -146,12 +149,15 @@ - (id) initWithURL:(NSURL*)serviceURI delegate:(id)deleg self = [super init]; if (self) { _serviceURI = serviceURI; - NSRange comp = [[serviceURI absoluteString] rangeOfString:@"/" options:NSBackwardsSearch]; + NSString *serviceString = [serviceURI absoluteString]; + NSString *bundleString = NSBundle.mainBundle.bundleURL.absoluteString; + serviceString = [serviceString stringByReplacingOccurrencesOfString:bundleString withString:@""]; + NSRange comp = [serviceString rangeOfString:@"/" options:NSBackwardsSearch]; if (comp.location != NSNotFound) { - serviceId_ = [[serviceURI absoluteString] substringToIndex:comp.location]; - module_ = [[serviceURI absoluteString] substringFromIndex:comp.location+1]; + serviceId_ = [serviceString substringToIndex:comp.location]; + module_ = [serviceString substringFromIndex:comp.location+1]; } else { - serviceId_ = [serviceURI absoluteString]; + serviceId_ = serviceString; module_ = serviceId_; } comp = [module_ rangeOfString:@"?"]; @@ -187,12 +193,8 @@ - (NSError*) fetchService if ([self.serviceURI isFileURL]) { // Symlink file for speed - if ([fileManager fileExistsAtPath:localPath]) { - [fileManager removeItemAtPath:localPath error:&error]; - } - if (error == nil) { - [fileManager createSymbolicLinkAtURL:[NSURL fileURLWithPath:localPath] withDestinationURL:self.serviceURI error:&error]; - } + [fileManager removeItemAtPath:localPath error:nil]; + [fileManager createSymbolicLinkAtURL:[NSURL fileURLWithPath:localPath] withDestinationURL:self.serviceURI error:&error]; self.fetched = true; } else { NSDate* lastModified = nil; @@ -231,9 +233,7 @@ - (NSError*) fetchService NSHTTPURLResponse *http = (NSHTTPURLResponse*)response; error = e; if (error == nil && http.statusCode == 200) { - if ([[NSFileManager defaultManager] fileExistsAtPath:localPath]) { - [[NSFileManager defaultManager] removeItemAtPath:localPath error:nil]; - } + [[NSFileManager defaultManager] removeItemAtPath:localPath error:nil]; [[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:localPath] error:nil]; @@ -247,14 +247,10 @@ - (NSError*) fetchService NSBundle *bundle = [NSBundle mainBundle]; NSURL *bundleURL = [[bundle resourceURL] URLByAppendingPathComponent:@"LiquidCore.bundle"]; NSURL *fileURL = [NSBundle URLForResource:bundleName withExtension:@"js" subdirectory:nil inBundleWithURL:bundleURL]; - if ([[NSFileManager defaultManager] fileExistsAtPath:localPath]) { - [[NSFileManager defaultManager] removeItemAtPath:localPath error:&error]; - } - if (error == nil) { - [[NSFileManager defaultManager] copyItemAtURL:fileURL - toURL:[NSURL fileURLWithPath:localPath] - error:&error]; - } + [[NSFileManager defaultManager] removeItemAtPath:localPath error:nil]; + [[NSFileManager defaultManager] copyItemAtURL:fileURL + toURL:[NSURL fileURLWithPath:localPath] + error:&error]; if (error == nil) { self.fetched = true; return;