Skip to content

Commit

Permalink
[enhancement] add info log and canRead for isLegalFile judgement
Browse files Browse the repository at this point in the history
  • Loading branch information
shwenzhang committed Apr 20, 2017
1 parent 9683139 commit 40c15bb
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ protected static boolean waitAndCheckDexOptFile(File patchFile, Tinker manager)
return false;
}
if (Build.VERSION.SDK_INT >= 21) {
Throwable lastThrowable = null;
for (File file : optFiles) {
TinkerLog.i(TAG, "check dex optimizer file format: %s, size %d", file.getName(), file.length());
int returnType;
Expand All @@ -134,6 +135,7 @@ protected static boolean waitAndCheckDexOptFile(File patchFile, Tinker manager)
} catch (Throwable e) {
TinkerLog.e(TAG, "final parallel dex optimizer file %s is not elf format, return false", file.getName());
failDexFiles.add(file);
lastThrowable = e;
} finally {
if (elfFile != null) {
try {
Expand All @@ -146,8 +148,12 @@ protected static boolean waitAndCheckDexOptFile(File patchFile, Tinker manager)
}
}
if (!failDexFiles.isEmpty()) {
Throwable returnThrowable = lastThrowable == null
? new TinkerRuntimeException(ShareConstants.CHECK_DEX_OAT_FORMAT_FAIL)
: new TinkerRuntimeException(ShareConstants.CHECK_DEX_OAT_FORMAT_FAIL, lastThrowable);

manager.getPatchReporter().onPatchDexOptFail(patchFile, failDexFiles,
new TinkerRuntimeException(ShareConstants.CHECK_DEX_OAT_FORMAT_FAIL));
returnThrowable);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@


import android.content.Context;
import android.os.Looper;
import android.os.MessageQueue;

import com.tencent.tinker.lib.service.TinkerPatchService;
import com.tencent.tinker.lib.tinker.Tinker;
Expand Down Expand Up @@ -111,6 +109,7 @@ public void onLoadPatchVersionChanged(String oldVersion, String newVersion, File

/**
* After system ota, we will try to load dex with interpret mode
*
* @param type type define as following
* {@code ShareConstants.TYPE_INTERPRET_OK} it is ok, using interpret mode
* {@code ShareConstants.TYPE_INTERPRET_GET_INSTRUCTION_SET_ERROR} get instruction set from exist oat file fail
Expand Down Expand Up @@ -148,7 +147,6 @@ public void onLoadInterpret(int type, Throwable e) {
* {@code ShareConstants.TYPE_DEX} patch dex file or directory not found
* {@code ShareConstants.TYPE_LIBRARY} patch lib file or directory not found
* {@code ShareConstants.TYPE_RESOURCE} patch lib file or directory not found
*
* @param isDirectory whether is directory for the file type
*/
@Override
Expand Down Expand Up @@ -222,11 +220,11 @@ public void onLoadResult(File patchDirectory, int loadCode, long cost) {
* you can disable patch as {@link DefaultLoadReporter#onLoadException(Throwable, int)}
*
* @param e
* @param errorCode exception code
* {@code ShareConstants.ERROR_LOAD_EXCEPTION_UNKNOWN} unknown exception
* {@code ShareConstants.ERROR_LOAD_EXCEPTION_DEX} exception when load dex
* {@code ShareConstants.ERROR_LOAD_EXCEPTION_RESOURCE} exception when load resource
* {@code ShareConstants.ERROR_LOAD_EXCEPTION_UNCAUGHT} exception unCaught
* @param errorCode exception code
* {@code ShareConstants.ERROR_LOAD_EXCEPTION_UNKNOWN} unknown exception
* {@code ShareConstants.ERROR_LOAD_EXCEPTION_DEX} exception when load dex
* {@code ShareConstants.ERROR_LOAD_EXCEPTION_RESOURCE} exception when load resource
* {@code ShareConstants.ERROR_LOAD_EXCEPTION_UNCAUGHT} exception unCaught
*/
@Override
public void onLoadException(Throwable e, int errorCode) {
Expand Down Expand Up @@ -321,26 +319,24 @@ public void checkAndCleanPatch() {

}

public void retryPatch() {
public boolean retryPatch() {
final Tinker tinker = Tinker.with(context);
if (!tinker.isMainProcess()) {
return;
return false;
}
Looper.getMainLooper().myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
@Override
public boolean queueIdle() {
File patchVersionFile = tinker.getTinkerLoadResultIfPresent().patchVersionFile;
if (patchVersionFile != null) {
if (UpgradePatchRetry.getInstance(context).onPatchListenerCheck(SharePatchFileUtil.getMD5(patchVersionFile))) {
TinkerLog.i(TAG, "try to repair oat file on patch process");
TinkerInstaller.onReceiveUpgradePatch(context, patchVersionFile.getAbsolutePath());
} else {
TinkerLog.i(TAG, "repair retry exceed must max time, just clean");
checkAndCleanPatch();
}
}
return false;

File patchVersionFile = tinker.getTinkerLoadResultIfPresent().patchVersionFile;
if (patchVersionFile != null) {
if (UpgradePatchRetry.getInstance(context).onPatchListenerCheck(SharePatchFileUtil.getMD5(patchVersionFile))) {
TinkerLog.i(TAG, "try to repair oat file on patch process");
TinkerInstaller.onReceiveUpgradePatch(context, patchVersionFile.getAbsolutePath());
return true;
} else {
TinkerLog.i(TAG, "repair retry exceed must max time, just clean");
checkAndCleanPatch();
}
});
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class TinkerDexLoader {



private static File testDexFile;
private static File testOptDexFile;

private TinkerDexLoader() {
}
Expand Down Expand Up @@ -116,7 +116,7 @@ public static boolean loadTinkerJars(final TinkerApplication application, String
final Throwable[] parallelOTAThrowable = new Throwable[1];
String targetISA;
try {
targetISA = ShareOatUtil.getTestDexOatInstructionSet(directory, testDexFile);
targetISA = ShareOatUtil.getOatFileInstructionSet(testOptDexFile);
} catch (Throwable e) {
// don't ota on the front
deleteOutOfDateOATFile(directory);
Expand Down Expand Up @@ -234,17 +234,17 @@ public static boolean checkComplete(String directory, ShareSecurityCheck securit
ShareIntentUtil.setIntentReturnCode(intentResult, ShareConstants.ERROR_LOAD_PATCH_VERSION_DEX_FILE_NOT_EXIST);
return false;
}
// find test dex
if (dexFile.getName().startsWith(ShareConstants.TEST_DEX_NAME)) {
testDexFile = dexFile;
}
//check dex opt whether complete also
File dexOptFile = new File(SharePatchFileUtil.optimizedPathFor(dexFile, optimizeDexDirectoryFile));
if (!SharePatchFileUtil.isLegalFile(dexOptFile)) {
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;
}
// find test dex
if (dexOptFile.getName().startsWith(ShareConstants.TEST_DEX_NAME)) {
testOptDexFile = dexOptFile;
}
}

//if is ok, add to result intent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,17 @@ public Intent tryLoad(TinkerApplication app) {
private void tryLoadPatchFilesInternal(TinkerApplication app, Intent resultIntent) {
final int tinkerFlag = app.getTinkerFlags();

if (!ShareTinkerInternals.isTinkerEnabled(tinkerFlag)
|| ShareTinkerInternals.isInPatchProcess(app)) {
if (!ShareTinkerInternals.isTinkerEnabled(tinkerFlag)) {
Log.w(TAG, "tryLoadPatchFiles: tinker is disable, just return");
ShareIntentUtil.setIntentReturnCode(resultIntent, ShareConstants.ERROR_LOAD_DISABLE);
return;
}
if (ShareTinkerInternals.isInPatchProcess(app)) {
Log.w(TAG, "tryLoadPatchFiles: we don't load patch with :patch process itself, just return");
ShareIntentUtil.setIntentReturnCode(resultIntent, ShareConstants.ERROR_LOAD_DISABLE);
return;

}
//tinker
File patchDirectoryFile = SharePatchFileUtil.getPatchDirectory(app);
if (patchDirectoryFile == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ private ShareOatUtil() {
throw new UnsupportedOperationException();
}

public static String getTestDexOatInstructionSet(String directory, File testDexFile) throws Throwable {
String optimizeDexDirectory = directory + "/" + ShareConstants.DEFAULT_DEX_OPTIMIZE_PATH + "/";
File testDexOptFile = new File(SharePatchFileUtil.optimizedPathFor(testDexFile, new File(optimizeDexDirectory)));
return getOatFileInstructionSet(testDexOptFile);

}
/**
* Get instruction set used to generate {@code oatFile}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static String checkTinkerLastUncaughtCrash(Context context) {
}

public static final boolean isLegalFile(File file) {
return file != null && file.exists() && file.isFile() && file.length() > 0;
return file != null && file.exists() && file.canRead() && file.isFile() && file.length() > 0;
}

/**
Expand Down

0 comments on commit 40c15bb

Please sign in to comment.