Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

Commit

Permalink
Update HCPInstallationWorker.m
Browse files Browse the repository at this point in the history
  • Loading branch information
bbreijer authored Sep 8, 2016
1 parent 379ecb5 commit f451382
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions src/ios/Updater/HCPInstallationWorker.m
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,19 @@ - (BOOL)isUpdateValid:(NSError **)error {

NSArray *updateFileList = _manifestDiff.updateFileList;
for (HCPManifestFile *updatedFile in updateFileList) {
NSURL *fileLocalURL = [_newReleaseFS.downloadFolder URLByAppendingPathComponent:updatedFile.name isDirectory:NO];
if (![_fileManager fileExistsAtPath:fileLocalURL.path]) {
errorMsg = [NSString stringWithFormat:@"Update validation error! File not found: %@", updatedFile.name];
break;
}

NSString *fileMD5 = [[NSData dataWithContentsOfURL:fileLocalURL] md5];
if (![fileMD5 isEqualToString:updatedFile.md5Hash]) {
errorMsg = [NSString stringWithFormat:@"Update validation error! File's %@ hash %@ doesnt match the hash %@ from manifest file", updatedFile.name, fileMD5, updatedFile.md5Hash];
break;
// Force the release of the memory allocated to calculate the MD5 hash
@autoreleasepool {
NSURL *fileLocalURL = [_newReleaseFS.downloadFolder URLByAppendingPathComponent:updatedFile.name isDirectory:NO];
if (![_fileManager fileExistsAtPath:fileLocalURL.path]) {
errorMsg = [NSString stringWithFormat:@"Update validation error! File not found: %@", updatedFile.name];
break;
}

NSString *fileMD5 = [[NSData dataWithContentsOfURL:fileLocalURL] md5];
if (![fileMD5 isEqualToString:updatedFile.md5Hash]) {
errorMsg = [NSString stringWithFormat:@"Update validation error! File's %@ hash %@ doesnt match the hash %@ from manifest file", updatedFile.name, fileMD5, updatedFile.md5Hash];
break;
}
}
}

Expand Down Expand Up @@ -243,32 +246,35 @@ - (BOOL)moveDownloadedFilesToWwwFolder:(NSError **)error {
NSArray *updatedFiles = _manifestDiff.updateFileList;
NSString *errorMsg = nil;
for (HCPManifestFile *manifestFile in updatedFiles) {
// determine paths to the file in installation and www folders
NSURL *pathInInstallationFolder = [_newReleaseFS.downloadFolder URLByAppendingPathComponent:manifestFile.name];
NSURL *pathInWwwFolder = [_newReleaseFS.wwwFolder URLByAppendingPathComponent:manifestFile.name];

// if file already exists in www folder - remove it before copying
if ([fileManager fileExistsAtPath:pathInWwwFolder.path] && ![fileManager removeItemAtURL:pathInWwwFolder error:error]) {
errorMsg = [NSString stringWithFormat:@"Failed to delete old version of the file %@ : %@. Installation failed",
manifestFile.name, [(*error) underlyingErrorLocalizedDesription]];
break;
}

// if needed - create subfolders for the new file
NSURL *parentDirectoryPathInWwwFolder = [pathInWwwFolder URLByDeletingLastPathComponent];
if (![fileManager fileExistsAtPath:parentDirectoryPathInWwwFolder.path]) {
if (![fileManager createDirectoryAtPath:parentDirectoryPathInWwwFolder.path withIntermediateDirectories:YES attributes:nil error:error]) {
errorMsg = [NSString stringWithFormat:@"Failed to create folder structure for file %@ : %@. Installation failed.",
// Force the release of the memory allocated during file copy
@autoreleasepool {
// determine paths to the file in installation and www folders
NSURL *pathInInstallationFolder = [_newReleaseFS.downloadFolder URLByAppendingPathComponent:manifestFile.name];
NSURL *pathInWwwFolder = [_newReleaseFS.wwwFolder URLByAppendingPathComponent:manifestFile.name];

// if file already exists in www folder - remove it before copying
if ([fileManager fileExistsAtPath:pathInWwwFolder.path] && ![fileManager removeItemAtURL:pathInWwwFolder error:error]) {
errorMsg = [NSString stringWithFormat:@"Failed to delete old version of the file %@ : %@. Installation failed",
manifestFile.name, [(*error) underlyingErrorLocalizedDesription]];
break;
}

// if needed - create subfolders for the new file
NSURL *parentDirectoryPathInWwwFolder = [pathInWwwFolder URLByDeletingLastPathComponent];
if (![fileManager fileExistsAtPath:parentDirectoryPathInWwwFolder.path]) {
if (![fileManager createDirectoryAtPath:parentDirectoryPathInWwwFolder.path withIntermediateDirectories:YES attributes:nil error:error]) {
errorMsg = [NSString stringWithFormat:@"Failed to create folder structure for file %@ : %@. Installation failed.",
manifestFile.name, [(*error) underlyingErrorLocalizedDesription]];
break;
}
}

// copy new file into www folder
if (![fileManager moveItemAtURL:pathInInstallationFolder toURL:pathInWwwFolder error:error]) {
errorMsg = [NSString stringWithFormat:@"Failed to copy file %@ into www folder: %@. Installation failed.",
manifestFile.name, [(*error) underlyingErrorLocalizedDesription]];
break;
}
}

// copy new file into www folder
if (![fileManager moveItemAtURL:pathInInstallationFolder toURL:pathInWwwFolder error:error]) {
errorMsg = [NSString stringWithFormat:@"Failed to copy file %@ into www folder: %@. Installation failed.",
manifestFile.name, [(*error) underlyingErrorLocalizedDesription]];
break;
}
}

Expand Down

0 comments on commit f451382

Please sign in to comment.