Skip to content

Commit

Permalink
1. [enhancement] remove repair patch
Browse files Browse the repository at this point in the history
2. [enhancement] move crash detail and patch retry to tinker_temp directroy
  • Loading branch information
shwenzhang committed Dec 16, 2016
1 parent d372cdc commit 9919eba
Show file tree
Hide file tree
Showing 36 changed files with 298 additions and 492 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,23 @@ public DefaultPatchListener(Context context) {
* you can overwrite it
*
* @param path
* @param isUpgrade
* @return
*/
@Override
public int onPatchReceived(String path, boolean isUpgrade) {
public int onPatchReceived(String path) {

int returnCode = patchCheck(path, isUpgrade);
int returnCode = patchCheck(path);

if (returnCode == ShareConstants.ERROR_PATCH_OK) {
TinkerPatchService.runPatchService(context, path, isUpgrade);
TinkerPatchService.runPatchService(context, path);
} else {
Tinker.with(context).getLoadReporter().onLoadPatchListenerReceiveFail(new File(path), returnCode, isUpgrade);
Tinker.with(context).getLoadReporter().onLoadPatchListenerReceiveFail(new File(path), returnCode);
}
return returnCode;

}

protected int patchCheck(String path, boolean isUpgrade) {
protected int patchCheck(String path) {
Tinker manager = Tinker.with(context);
//check SharePreferences also
if (!manager.isTinkerEnabled() || !ShareTinkerInternals.isTinkerEnableWithSharedPreferences(context)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
* Created by zhangshaowen on 16/3/14.
*/
public interface PatchListener {
int onPatchReceived(String path, boolean isUpgrade);
int onPatchReceived(String path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class BsDiffPatchInternal extends BasePatchInternal {
private static final String TAG = "Tinker.BsDiffPatchInternal";

protected static boolean tryRecoverLibraryFiles(Tinker manager, ShareSecurityCheck checker, Context context,
String patchVersionDirectory, File patchFile, boolean isUpgradePatch) {
String patchVersionDirectory, File patchFile) {

if (!manager.isEnabledForNativeLib()) {
TinkerLog.w(TAG, "patch recover, library is not enabled");
Expand All @@ -55,19 +55,19 @@ protected static boolean tryRecoverLibraryFiles(Tinker manager, ShareSecurityChe
return true;
}
long begin = SystemClock.elapsedRealtime();
boolean result = patchLibraryExtractViaBsDiff(context, patchVersionDirectory, libMeta, patchFile, isUpgradePatch);
boolean result = patchLibraryExtractViaBsDiff(context, patchVersionDirectory, libMeta, patchFile);
long cost = SystemClock.elapsedRealtime() - begin;
TinkerLog.i(TAG, "recover lib result:%b, cost:%d, isUpgradePatch:%b", result, cost, isUpgradePatch);
TinkerLog.i(TAG, "recover lib result:%b, cost:%d", result, cost);
return result;
}


private static boolean patchLibraryExtractViaBsDiff(Context context, String patchVersionDirectory, String meta, File patchFile, boolean isUpgradePatch) {
private static boolean patchLibraryExtractViaBsDiff(Context context, String patchVersionDirectory, String meta, File patchFile) {
String dir = patchVersionDirectory + "/" + SO_PATH + "/";
return extractBsDiffInternals(context, dir, meta, patchFile, TYPE_Library, isUpgradePatch);
return extractBsDiffInternals(context, dir, meta, patchFile, TYPE_Library);
}

private static boolean extractBsDiffInternals(Context context, String dir, String meta, File patchFile, int type, boolean isUpgradePatch) {
private static boolean extractBsDiffInternals(Context context, String dir, String meta, File patchFile, int type) {
//parse
ArrayList<ShareBsDiffPatchInfo> patchList = new ArrayList<>();

Expand Down Expand Up @@ -110,7 +110,7 @@ private static boolean extractBsDiffInternals(Context context, String dir, Strin
final String fileMd5 = info.md5;
if (!SharePatchFileUtil.checkIfMd5Valid(fileMd5)) {
TinkerLog.w(TAG, "meta file md5 mismatch, type:%s, name: %s, md5: %s", ShareTinkerInternals.getTypeString(type), info.name, info.md5);
manager.getPatchReporter().onPatchPackageCheckFail(patchFile, isUpgradePatch, BasePatchInternal.getMetaCorruptedCode(type));
manager.getPatchReporter().onPatchPackageCheckFail(patchFile, BasePatchInternal.getMetaCorruptedCode(type));
return false;
}
String middle;
Expand Down Expand Up @@ -140,29 +140,29 @@ private static boolean extractBsDiffInternals(Context context, String dir, Strin

if (patchFileEntry == null) {
TinkerLog.w(TAG, "patch entry is null. path:" + patchRealPath);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.name, type, isUpgradePatch);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.name, type);
return false;
}

if (patchFileMd5.equals("0")) {
if (!extract(patch, patchFileEntry, extractedFile, fileMd5, false)) {
TinkerLog.w(TAG, "Failed to extract file " + extractedFile.getPath());
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.name, type, isUpgradePatch);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.name, type);
return false;
}
} else {
//we do not check the intermediate files' md5 to save time, use check whether it is 32 length
if (!SharePatchFileUtil.checkIfMd5Valid(patchFileMd5)) {
TinkerLog.w(TAG, "meta file md5 mismatch, type:%s, name: %s, md5: %s", ShareTinkerInternals.getTypeString(type), info.name, patchFileMd5);
manager.getPatchReporter().onPatchPackageCheckFail(patchFile, isUpgradePatch, BasePatchInternal.getMetaCorruptedCode(type));
manager.getPatchReporter().onPatchPackageCheckFail(patchFile, BasePatchInternal.getMetaCorruptedCode(type));
return false;
}

ZipEntry rawApkFileEntry = apk.getEntry(patchRealPath);

if (rawApkFileEntry == null) {
TinkerLog.w(TAG, "apk entry is null. path:" + patchRealPath);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.name, type, isUpgradePatch);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.name, type);
return false;
}

Expand All @@ -172,7 +172,7 @@ private static boolean extractBsDiffInternals(Context context, String dir, Strin
String rawEntryCrc = String.valueOf(rawApkFileEntry.getCrc());
if (!rawEntryCrc.equals(rawApkCrc)) {
TinkerLog.e(TAG, "apk entry %s crc is not equal, expect crc: %s, got crc: %s", patchRealPath, rawApkCrc, rawEntryCrc);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.name, type, isUpgradePatch);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.name, type);
return false;
}
InputStream oldStream = null;
Expand All @@ -189,7 +189,7 @@ private static boolean extractBsDiffInternals(Context context, String dir, Strin
//go go go bsdiff get the
if (!SharePatchFileUtil.verifyFileMd5(extractedFile, fileMd5)) {
TinkerLog.w(TAG, "Failed to recover diff file " + extractedFile.getPath());
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.name, type, isUpgradePatch);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.name, type);
SharePatchFileUtil.safeDeleteFile(extractedFile);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class DexDiffPatchInternal extends BasePatchInternal {
protected static final String TAG = "Tinker.DexDiffPatchInternal";

protected static boolean tryRecoverDexFiles(Tinker manager, ShareSecurityCheck checker, Context context,
String patchVersionDirectory, File patchFile, boolean isUpgradePatch) {
String patchVersionDirectory, File patchFile) {
if (!manager.isEnabledForDex()) {
TinkerLog.w(TAG, "patch recover, dex is not enabled");
return true;
Expand All @@ -63,16 +63,16 @@ protected static boolean tryRecoverDexFiles(Tinker manager, ShareSecurityCheck c
}

long begin = SystemClock.elapsedRealtime();
boolean result = patchDexExtractViaDexDiff(context, patchVersionDirectory, dexMeta, patchFile, isUpgradePatch);
boolean result = patchDexExtractViaDexDiff(context, patchVersionDirectory, dexMeta, patchFile);
long cost = SystemClock.elapsedRealtime() - begin;
TinkerLog.i(TAG, "recover dex result:%b, cost:%d, isUpgradePatch:%b", result, cost, isUpgradePatch);
TinkerLog.i(TAG, "recover dex result:%b, cost:%d", result, cost);
return result;
}

private static boolean patchDexExtractViaDexDiff(Context context, String patchVersionDirectory, String meta, final File patchFile, final boolean isUpgradePatch) {
private static boolean patchDexExtractViaDexDiff(Context context, String patchVersionDirectory, String meta, final File patchFile) {
String dir = patchVersionDirectory + "/" + DEX_PATH + "/";

if (!extractDexDiffInternals(context, dir, meta, patchFile, TYPE_DEX, isUpgradePatch)) {
if (!extractDexDiffInternals(context, dir, meta, patchFile, TYPE_DEX)) {
TinkerLog.w(TAG, "patch recover, extractDiffInternals fail");
return false;
}
Expand All @@ -90,18 +90,32 @@ private static boolean patchDexExtractViaDexDiff(Context context, String patchVe
optimizeDexDirectoryFile.mkdirs();
}

TinkerLog.w(TAG, "patch recover, try to optimize dex file count:%d", files.length);

boolean isSuccess = TinkerParallelDexOptimizer.optimizeAll(
files, optimizeDexDirectoryFile,
new TinkerParallelDexOptimizer.ResultCallback() {
long start;
@Override
public void onStart(File dexFile, File optimizedDir) {
start = System.currentTimeMillis();
TinkerLog.i(TAG, "start to optimize dex %s", dexFile.getPath());
}

@Override
public void onSuccess(File dexFile, File optimizedDir) {
// Do nothing.
TinkerLog.i(TAG, "success to optimize dex %s use time %d",
dexFile.getPath(), (System.currentTimeMillis() - start));
}

@Override
public void onFailed(File dexFile, File optimizedDir, Throwable thr) {
TinkerLog.i(TAG, "fail to optimize dex %s use time %d",
dexFile.getPath(), (System.currentTimeMillis() - start));

SharePatchFileUtil.safeDeleteFile(dexFile);
manager.getPatchReporter().onPatchDexOptFail(patchFile, dexFile, optimizeDexDirectory, dexFile.getName(), thr, isUpgradePatch);
manager.getPatchReporter().onPatchDexOptFail(patchFile, dexFile, optimizeDexDirectory, dexFile.getName(), thr);
}
}
);
Expand All @@ -112,7 +126,7 @@ public void onFailed(File dexFile, File optimizedDir, Throwable thr) {
return true;
}

private static boolean extractDexDiffInternals(Context context, String dir, String meta, File patchFile, int type, boolean isUpgradePatch) {
private static boolean extractDexDiffInternals(Context context, String dir, String meta, File patchFile, int type) {
//parse
ArrayList<ShareDexDiffPatchInfo> patchList = new ArrayList<>();

Expand Down Expand Up @@ -164,7 +178,7 @@ private static boolean extractDexDiffInternals(Context context, String dir, Stri

if (!SharePatchFileUtil.checkIfMd5Valid(extractedFileMd5)) {
TinkerLog.w(TAG, "meta file md5 invalid, type:%s, name: %s, md5: %s", ShareTinkerInternals.getTypeString(type), info.rawName, extractedFileMd5);
manager.getPatchReporter().onPatchPackageCheckFail(patchFile, isUpgradePatch, BasePatchInternal.getMetaCorruptedCode(type));
manager.getPatchReporter().onPatchPackageCheckFail(patchFile, BasePatchInternal.getMetaCorruptedCode(type));
return false;
}

Expand All @@ -190,14 +204,14 @@ private static boolean extractDexDiffInternals(Context context, String dir, Stri
if (oldDexCrc.equals("0")) {
if (patchFileEntry == null) {
TinkerLog.w(TAG, "patch entry is null. path:" + patchRealPath);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type, isUpgradePatch);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type);
return false;
}

//it is a new file, but maybe we need to repack the dex file
if (!extractDexFile(patch, patchFileEntry, extractedFile, info)) {
TinkerLog.w(TAG, "Failed to extract raw patch file " + extractedFile.getPath());
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type, isUpgradePatch);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type);
return false;
}
} else if (dexDiffMd5.equals("0")) {
Expand All @@ -208,15 +222,15 @@ private static boolean extractDexDiffInternals(Context context, String dir, Stri

if (rawApkFileEntry == null) {
TinkerLog.w(TAG, "apk entry is null. path:" + patchRealPath);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type, isUpgradePatch);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type);
return false;
}

//check source crc instead of md5 for faster
String rawEntryCrc = String.valueOf(rawApkFileEntry.getCrc());
if (!rawEntryCrc.equals(oldDexCrc)) {
TinkerLog.e(TAG, "apk entry %s crc is not equal, expect crc: %s, got crc: %s", patchRealPath, oldDexCrc, rawEntryCrc);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type, isUpgradePatch);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type);
return false;
}

Expand All @@ -226,41 +240,41 @@ private static boolean extractDexDiffInternals(Context context, String dir, Stri

if (!SharePatchFileUtil.verifyDexFileMd5(extractedFile, extractedFileMd5)) {
TinkerLog.w(TAG, "Failed to recover dex file when verify patched dex: " + extractedFile.getPath());
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type, isUpgradePatch);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type);
SharePatchFileUtil.safeDeleteFile(extractedFile);
return false;
}
} else {
if (patchFileEntry == null) {
TinkerLog.w(TAG, "patch entry is null. path:" + patchRealPath);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type, isUpgradePatch);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type);
return false;
}

if (!SharePatchFileUtil.checkIfMd5Valid(dexDiffMd5)) {
TinkerLog.w(TAG, "meta file md5 invalid, type:%s, name: %s, md5: %s", ShareTinkerInternals.getTypeString(type), info.rawName, dexDiffMd5);
manager.getPatchReporter().onPatchPackageCheckFail(patchFile, isUpgradePatch, BasePatchInternal.getMetaCorruptedCode(type));
manager.getPatchReporter().onPatchPackageCheckFail(patchFile, BasePatchInternal.getMetaCorruptedCode(type));
return false;
}

if (rawApkFileEntry == null) {
TinkerLog.w(TAG, "apk entry is null. path:" + patchRealPath);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type, isUpgradePatch);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type);
return false;
}
//check source crc instead of md5 for faster
String rawEntryCrc = String.valueOf(rawApkFileEntry.getCrc());
if (!rawEntryCrc.equals(oldDexCrc)) {
TinkerLog.e(TAG, "apk entry %s crc is not equal, expect crc: %s, got crc: %s", patchRealPath, oldDexCrc, rawEntryCrc);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type, isUpgradePatch);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type);
return false;
}

patchDexFile(apk, patch, rawApkFileEntry, patchFileEntry, info, extractedFile);

if (!SharePatchFileUtil.verifyDexFileMd5(extractedFile, extractedFileMd5)) {
TinkerLog.w(TAG, "Failed to recover dex file when verify patched dex: " + extractedFile.getPath());
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type, isUpgradePatch);
manager.getPatchReporter().onPatchTypeExtractFail(patchFile, extractedFile, info.rawName, type);
SharePatchFileUtil.safeDeleteFile(extractedFile);
return false;
}
Expand Down
Loading

0 comments on commit 9919eba

Please sign in to comment.