From 7bb4e1099e9a6d776159a7698e845f817105480e Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Thu, 11 Feb 2016 21:18:00 -0800 Subject: [PATCH 1/3] fix getCurrentPackage --- CodePush.js | 10 ++++---- CodePush.m | 1 + CodePushPackage.m | 23 ++++++++++++------- .../microsoft/codepush/react/CodePush.java | 4 ++++ .../codepush/react/CodePushPackage.java | 10 ++++++-- 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/CodePush.js b/CodePush.js index 206f7a2..baf1c57 100644 --- a/CodePush.js +++ b/CodePush.js @@ -27,7 +27,7 @@ async function checkForUpdate(deploymentKey = null) { */ const config = deploymentKey ? { ...nativeConfig, ...{ deploymentKey } } : nativeConfig; const sdk = getPromisifiedSdk(requestFetchAdapter, config); - + // Use dynamically overridden getCurrentPackage() during tests. const localPackage = await module.exports.getCurrentPackage(); @@ -57,7 +57,7 @@ async function checkForUpdate(deploymentKey = null) { * bug in the server, but we're adding this check just to double-check that the * client app is resilient to a potential issue with the update check. */ - if (!update || update.updateAppVersion || (update.packageHash === localPackage.packageHash)) { + if (!update || update.updateAppVersion || localPackage && (update.packageHash === localPackage.packageHash)) { if (update && update.updateAppVersion) { log("An update is available but it is targeting a newer binary version than you are currently running."); } @@ -87,8 +87,10 @@ const getConfiguration = (() => { async function getCurrentPackage() { const localPackage = await NativeCodePush.getCurrentPackage(); - localPackage.failedInstall = await NativeCodePush.isFailedUpdate(localPackage.packageHash); - localPackage.isFirstRun = await NativeCodePush.isFirstRun(localPackage.packageHash); + if (localPackage) { + localPackage.failedInstall = await NativeCodePush.isFailedUpdate(localPackage.packageHash); + localPackage.isFirstRun = await NativeCodePush.isFirstRun(localPackage.packageHash); + } return localPackage; } diff --git a/CodePush.m b/CodePush.m index 95f6a9d..192a350 100644 --- a/CodePush.m +++ b/CodePush.m @@ -462,6 +462,7 @@ RCT_EXPORT_METHOD(getCurrentPackage:(RCTPromiseResolveBlock)resolve if (error) { reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error); + return; } // Add the "isPending" virtual property to the package at this point, so that diff --git a/CodePushPackage.m b/CodePushPackage.m index 8798d5a..07bffcb 100644 --- a/CodePushPackage.m +++ b/CodePushPackage.m @@ -142,7 +142,7 @@ NSString * const UnzippedFolderName = @"unzipped"; NSString *folderPath = [CodePushPackage getCurrentPackageFolderPath:error]; if (!*error) { if (!folderPath) { - return [NSDictionary dictionary]; + return nil; } NSString *packagePath = [folderPath stringByAppendingPathComponent:@"app.json"]; @@ -159,7 +159,7 @@ NSString * const UnzippedFolderName = @"unzipped"; } } - return NULL; + return nil; } + (NSDictionary *)getPackage:(NSString *)packageHash @@ -204,15 +204,22 @@ NSString * const UnzippedFolderName = @"unzipped"; failCallback:(void (^)(NSError *err))failCallback { NSString *newPackageFolderPath = [self getPackageFolderPath:updatePackage[@"packageHash"]]; - NSError *error = nil; + NSError *error; - if (![[NSFileManager defaultManager] fileExistsAtPath:newPackageFolderPath]) { - [[NSFileManager defaultManager] createDirectoryAtPath:newPackageFolderPath + if (![[NSFileManager defaultManager] fileExistsAtPath:[self getCodePushPath]]) { + [[NSFileManager defaultManager] createDirectoryAtPath:[self getCodePushPath] withIntermediateDirectories:YES attributes:nil error:&error]; } + if ([[NSFileManager defaultManager] fileExistsAtPath:newPackageFolderPath]) { + // This removes any stale data in newPackageFolderPath that could have been left + // uncleared due to a crash or error during the download or install process. + [[NSFileManager defaultManager] removeItemAtPath:newPackageFolderPath + error:&error]; + } + if (error) { return failCallback(error); } @@ -259,9 +266,9 @@ NSString * const UnzippedFolderName = @"unzipped"; return; } - [CodePushPackage copyEntriesInFolder:currentPackageFolderPath - destFolder:newPackageFolderPath - error:&error]; + [[NSFileManager defaultManager] copyItemAtPath:currentPackageFolderPath + toPath:newPackageFolderPath + error:&error]; if (error) { failCallback(error); return; diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java index 8d07647..b6dc518 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java @@ -411,6 +411,10 @@ public class CodePush { @Override protected Void doInBackground(Object... params) { WritableMap currentPackage = codePushPackage.getCurrentPackage(); + if (currentPackage == null) { + promise.resolve(""); + return null; + } Boolean isPendingUpdate = false; diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java index 714d459..3a9b550 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java @@ -129,14 +129,14 @@ public class CodePushPackage { public WritableMap getCurrentPackage() { String folderPath = getCurrentPackageFolderPath(); if (folderPath == null) { - return new WritableNativeMap(); + return null; } String packagePath = CodePushUtils.appendPathComponent(folderPath, PACKAGE_FILE_NAME); try { return CodePushUtils.getWritableMapFromFile(packagePath); } catch (IOException e) { - // There is no current package. + // Should not happen unless the update metadata was somehow deleted. return null; } } @@ -155,6 +155,12 @@ public class CodePushPackage { DownloadProgressCallback progressCallback) throws IOException { String newPackageFolderPath = getPackageFolderPath(CodePushUtils.tryGetString(updatePackage, PACKAGE_HASH_KEY)); + if (FileUtils.fileAtPathExists(newPackageFolderPath)) { + // This removes any stale data in newPackageFolderPath that could have been left + // uncleared due to a crash or error during the download or install process. + FileUtils.deleteDirectoryAtPath(newPackageFolderPath); + } + String downloadUrlString = CodePushUtils.tryGetString(updatePackage, DOWNLOAD_URL_KEY); URL downloadUrl = null; HttpURLConnection connection = null; From 434baeb55f55f294d2a41ab82da9e4fea9a18aff Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Fri, 12 Feb 2016 11:14:02 -0800 Subject: [PATCH 2/3] else if --- CodePushPackage.m | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/CodePushPackage.m b/CodePushPackage.m index 07bffcb..a03e83b 100644 --- a/CodePushPackage.m +++ b/CodePushPackage.m @@ -211,9 +211,7 @@ NSString * const UnzippedFolderName = @"unzipped"; withIntermediateDirectories:YES attributes:nil error:&error]; - } - - if ([[NSFileManager defaultManager] fileExistsAtPath:newPackageFolderPath]) { + } else if ([[NSFileManager defaultManager] fileExistsAtPath:newPackageFolderPath]) { // This removes any stale data in newPackageFolderPath that could have been left // uncleared due to a crash or error during the download or install process. [[NSFileManager defaultManager] removeItemAtPath:newPackageFolderPath From 446dc97c162c016e572f3fe2ccd1c4748aa0e51a Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Fri, 12 Feb 2016 11:19:39 -0800 Subject: [PATCH 3/3] else if --- CodePushPackage.m | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/CodePushPackage.m b/CodePushPackage.m index a03e83b..52c4805 100644 --- a/CodePushPackage.m +++ b/CodePushPackage.m @@ -206,16 +206,16 @@ NSString * const UnzippedFolderName = @"unzipped"; NSString *newPackageFolderPath = [self getPackageFolderPath:updatePackage[@"packageHash"]]; NSError *error; - if (![[NSFileManager defaultManager] fileExistsAtPath:[self getCodePushPath]]) { - [[NSFileManager defaultManager] createDirectoryAtPath:[self getCodePushPath] - withIntermediateDirectories:YES - attributes:nil - error:&error]; - } else if ([[NSFileManager defaultManager] fileExistsAtPath:newPackageFolderPath]) { + if ([[NSFileManager defaultManager] fileExistsAtPath:newPackageFolderPath]) { // This removes any stale data in newPackageFolderPath that could have been left // uncleared due to a crash or error during the download or install process. [[NSFileManager defaultManager] removeItemAtPath:newPackageFolderPath error:&error]; + } else if (![[NSFileManager defaultManager] fileExistsAtPath:[self getCodePushPath]]) { + [[NSFileManager defaultManager] createDirectoryAtPath:[self getCodePushPath] + withIntermediateDirectories:YES + attributes:nil + error:&error]; } if (error) {