Skip to content

Commit

Permalink
1. [enhancement] check dex opt file when patch
Browse files Browse the repository at this point in the history
2. [enhancement] check if there is old patch during clean patch in DefaultloadReporter
  • Loading branch information
shwenzhang committed Dec 22, 2016
1 parent 3589f16 commit f906518
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ public void onFailed(File dexFile, File optimizedDir, Throwable thr) {
long start = System.currentTimeMillis();
DexFile.loadDex(file.getAbsolutePath(), outputPathName, 0);
TinkerLog.i(TAG, "success single dex optimize file, path: %s, use time: %d", file.getPath(), (System.currentTimeMillis() - start));
if (!SharePatchFileUtil.isLegalFile(outputFile)) {
manager.getPatchReporter()
.onPatchDexOptFail(patchFile, file, optimizeDexDirectory,
file.getName(), new TinkerRuntimeException("dexOpt file:" + outputPathName + " is not exist"));
return false;
}
}
} catch (Throwable e) {
TinkerLog.e(TAG, "dex optimize or load failed, path:" + file.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ private static boolean extractResourceDiffInternals(Context context, String dir,
throw new TinkerRuntimeException("zipEntry is null when get from oldApk");
}
String name = zipEntry.getName();
if (name.contains("../")) {
continue;
}
if (ShareResPatchInfo.checkFileInPattern(resPatchInfo.patterns, name)) {
//won't contain in add set.
if (!resPatchInfo.deleteRes.contains(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@

import com.tencent.tinker.lib.service.TinkerPatchService;
import com.tencent.tinker.lib.tinker.Tinker;
import com.tencent.tinker.lib.tinker.TinkerLoadResult;
import com.tencent.tinker.lib.util.TinkerLog;
import com.tencent.tinker.loader.shareutil.ShareConstants;
import com.tencent.tinker.loader.shareutil.SharePatchFileUtil;
import com.tencent.tinker.loader.shareutil.SharePatchInfo;
import com.tencent.tinker.loader.shareutil.ShareTinkerInternals;

import java.io.File;
Expand Down Expand Up @@ -87,7 +89,7 @@ public void onLoadPatchVersionChanged(String oldVersion, String newVersion, File
if (!Tinker.with(context).isMainProcess()) {
return;
}
TinkerLog.i(TAG, "try kill all other process");
TinkerLog.i(TAG, "onLoadPatchVersionChanged, try kill all other process");
//kill all other process to ensure that all process's code is the same.
ShareTinkerInternals.killAllOtherProcess(context);

Expand Down Expand Up @@ -123,7 +125,7 @@ public void onLoadFileNotFound(File file, int fileType, boolean isDirectory) {
TinkerLog.i(TAG, "patch loadReporter onLoadFileNotFound: patch file not found: %s, fileType:%d, isDirectory:%b",
file.getAbsolutePath(), fileType, isDirectory);

Tinker.with(context).cleanPatch();
checkAndCleanPatch();
}

/**
Expand All @@ -142,7 +144,7 @@ public void onLoadFileNotFound(File file, int fileType, boolean isDirectory) {
public void onLoadFileMd5Mismatch(File file, int fileType) {
TinkerLog.i(TAG, "patch load Reporter onLoadFileMd5Mismatch: patch file md5 mismatch file: %s, fileType:%d", file.getAbsolutePath(), fileType);
//clean patch for safety
Tinker.with(context).cleanPatch();
checkAndCleanPatch();
}

/**
Expand All @@ -159,7 +161,7 @@ public void onLoadPatchInfoCorrupted(String oldVersion, String newVersion, File
TinkerLog.i(TAG, "patch loadReporter onLoadPatchInfoCorrupted: patch info file damage: %s, from version: %s to version: %s",
patchInfoFile.getAbsolutePath(), oldVersion, newVersion);

Tinker.with(context).cleanPatch();
checkAndCleanPatch();
}

/**
Expand Down Expand Up @@ -228,7 +230,7 @@ public void onLoadException(Throwable e, int errorCode) {
TinkerLog.printErrStackTrace(TAG, e, "tinker load exception");

Tinker.with(context).setTinkerDisable();
Tinker.with(context).cleanPatch();
checkAndCleanPatch();
}
/**
* check patch signature, TINKER_ID and meta files
Expand All @@ -250,6 +252,31 @@ public void onLoadException(Throwable e, int errorCode) {
public void onLoadPackageCheckFail(File patchFile, int errorCode) {
TinkerLog.i(TAG, "patch loadReporter onLoadPackageCheckFail: "
+ "load patch package check fail file path:%s, errorCode:%d", patchFile.getAbsolutePath(), errorCode);
Tinker.with(context).cleanPatch();
checkAndCleanPatch();
}

/**
* other process may have installed old patch version,
* if we try to clean patch, we should kill other process first
*/
private void checkAndCleanPatch() {
Tinker tinker = Tinker.with(context);
//only main process can load a new patch
if (tinker.isMainProcess()) {
TinkerLoadResult tinkerLoadResult = tinker.getTinkerLoadResultIfPresent();
//if versionChange and the old patch version is not ""
if (tinkerLoadResult.versionChanged) {
SharePatchInfo sharePatchInfo = tinkerLoadResult.patchInfo;
if (sharePatchInfo != null && !ShareTinkerInternals.isNullOrNil(sharePatchInfo.oldVersion)) {
TinkerLog.w(TAG, "checkAndCleanPatch, oldVersion %s is not null, try kill all other process",
sharePatchInfo.oldVersion);

ShareTinkerInternals.killAllOtherProcess(context);
}
}

}
tinker.cleanPatch();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,7 @@ public boolean parseTinkerResult(Context context, Intent intentResult) {

if (isMainProcess && versionChanged) {
//change the old version to new
patchInfo.oldVersion = currentVersion;
tinker.getLoadReporter().onLoadPatchVersionChanged(oldVersion, newVersion, patchDirectory, patchVersionDirectory.getName());

}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,8 @@ public static boolean checkComplete(String directory, ShareSecurityCheck securit
ShareIntentUtil.setIntentReturnCode(intentResult, ShareConstants.ERROR_LOAD_PATCH_VERSION_DEX_DIRECTORY_NOT_EXIST);
return false;
}
//don't check oat file
// String optimizeDexDirectory = directory + "/" + DEX_OPTIMIZE_PATH + "/";
// File optimizeDexDirectoryFile = new File(optimizeDexDirectory);
String optimizeDexDirectory = directory + "/" + DEX_OPTIMIZE_PATH + "/";
File optimizeDexDirectoryFile = new File(optimizeDexDirectory);

//fast check whether there is any dex files missing
for (String name : dexes.keySet()) {
Expand All @@ -211,13 +210,13 @@ public static boolean checkComplete(String directory, ShareSecurityCheck securit
ShareIntentUtil.setIntentReturnCode(intentResult, ShareConstants.ERROR_LOAD_PATCH_VERSION_DEX_FILE_NOT_EXIST);
return false;
}
// //check dex opt whether complete also
// File dexOptFile = new File(SharePatchFileUtil.optimizedPathFor(dexFile, optimizeDexDirectoryFile));
// if (!dexOptFile.exists()) {
// intentResult.putExtra(ShareIntentUtil.INTENT_PATCH_MISSING_DEX_PATH, dexOptFile.getAbsolutePath());
// ShareIntentUtil.setIntentReturnCode(intentResult, ShareConstants.ERROR_LOAD_PATCH_VERSION_DEX_OPT_FILE_NOT_EXIST);
// return false;
// }
//check dex opt whether complete also
File dexOptFile = new File(SharePatchFileUtil.optimizedPathFor(dexFile, optimizeDexDirectoryFile));
if (!dexOptFile.exists()) {
intentResult.putExtra(ShareIntentUtil.INTENT_PATCH_MISSING_DEX_PATH, dexOptFile.getAbsolutePath());
ShareIntentUtil.setIntentReturnCode(intentResult, ShareConstants.ERROR_LOAD_PATCH_VERSION_DEX_OPT_FILE_NOT_EXIST);
return false;
}
}

//if is ok, add to result intent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ public static void setTinkerDisableWithSharedPreferences(Context context) {
* @return
*/
public static boolean isTinkerEnableWithSharedPreferences(Context context) {
if (context == null) {
return false;
}
SharedPreferences sp = context.getSharedPreferences(ShareConstants.TINKER_SHARE_PREFERENCE_CONFIG, Context.MODE_MULTI_PROCESS);
return sp.getBoolean(ShareConstants.TINKER_ENABLE_CONFIG, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ public static String genResOutputFile(File output, File newZipFile, Configuratio
);
}
String name = zipEntry.getName();
if (name.contains("../")) {
continue;
}
if (Utils.checkFileInPattern(config.mResFilePattern, name)) {
//won't contain in add set.
if (!deletedSet.contains(name)
Expand Down

0 comments on commit f906518

Please sign in to comment.