Store CRC as apk file name

This commit is contained in:
Nullptr 2021-08-26 23:07:54 +08:00
parent 5f8159ec65
commit 113b2c93b8
2 changed files with 36 additions and 26 deletions

View File

@ -13,8 +13,6 @@ import android.util.Log;
import org.lsposed.lspatch.util.FileUtils; import org.lsposed.lspatch.util.FileUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -39,24 +37,19 @@ public class LSPAppComponentFactoryStub extends AppComponentFactory {
* This method will be called at <b>instantiateClassLoader</b> by <b>createOrUpdateClassLoaderLocked</b> * This method will be called at <b>instantiateClassLoader</b> by <b>createOrUpdateClassLoaderLocked</b>
**/ **/
private void initOriginalAppComponentFactory(ApplicationInfo aInfo) { private void initOriginalAppComponentFactory(ApplicationInfo aInfo) {
final String cacheApkPath = aInfo.dataDir + "/cache/origin_apk.bin"; final String originPath = aInfo.dataDir + "/cache/lspatch/origin/";
final String originalAppComponentFactoryClass = FileUtils.readTextFromInputStream(baseClassLoader.getResourceAsStream(ORIGINAL_APP_COMPONENT_FACTORY_ASSET_PATH)); final String originalAppComponentFactoryClass = FileUtils.readTextFromInputStream(baseClassLoader.getResourceAsStream(ORIGINAL_APP_COMPONENT_FACTORY_ASSET_PATH));
try { try {
File cacheApk = new File(cacheApkPath); String cacheApkPath;
if (cacheApk.exists()) { try (ZipFile sourceFile = new ZipFile(aInfo.sourceDir)) {
try (ZipFile sourceFile = new ZipFile(aInfo.sourceDir); cacheApkPath = originPath + sourceFile.getEntry(ORIGINAL_APK_ASSET_PATH).getCrc();
InputStream is = new FileInputStream(cacheApk)) {
var sourceCrc = sourceFile.getEntry(ORIGINAL_APK_ASSET_PATH).getCrc();
var cacheCrc = FileUtils.calculateCrc(is);
if (sourceCrc != cacheCrc) {
Log.i(TAG, "Application updated, extract original apk again");
cacheApk.delete();
}
}
} }
if (!cacheApk.exists()) { if (!Files.exists(Paths.get(cacheApkPath))) {
Log.i(TAG, "extract original apk");
FileUtils.deleteFolderIfExists(Paths.get(originPath));
Files.createDirectories(Paths.get(originPath));
try (InputStream is = baseClassLoader.getResourceAsStream(ORIGINAL_APK_ASSET_PATH)) { try (InputStream is = baseClassLoader.getResourceAsStream(ORIGINAL_APK_ASSET_PATH)) {
Files.copy(is, Paths.get(cacheApkPath)); Files.copy(is, Paths.get(cacheApkPath));
} }
@ -68,11 +61,11 @@ public class LSPAppComponentFactoryStub extends AppComponentFactory {
originalAppComponentFactory = (AppComponentFactory) appClassLoader.loadClass(originalAppComponentFactoryClass).newInstance(); originalAppComponentFactory = (AppComponentFactory) appClassLoader.loadClass(originalAppComponentFactoryClass).newInstance();
} catch (ClassNotFoundException | NullPointerException ignored) { } catch (ClassNotFoundException | NullPointerException ignored) {
if (originalAppComponentFactoryClass != null && !originalAppComponentFactoryClass.isEmpty()) if (originalAppComponentFactoryClass != null && !originalAppComponentFactoryClass.isEmpty())
Log.w(TAG, "Original AppComponentFactory not found"); Log.w(TAG, "original AppComponentFactory not found");
originalAppComponentFactory = new AppComponentFactory(); originalAppComponentFactory = new AppComponentFactory();
} }
Log.d(TAG, "Instantiate original AppComponentFactory: " + originalAppComponentFactory); Log.d(TAG, "instantiate original AppComponentFactory: " + originalAppComponentFactory);
} catch (Throwable e) { } catch (Throwable e) {
Log.e(TAG, "initOriginalAppComponentFactory", e); Log.e(TAG, "initOriginalAppComponentFactory", e);
} }

View File

@ -5,7 +5,11 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32; import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class FileUtils { public class FileUtils {
@ -22,13 +26,26 @@ public class FileUtils {
return null; return null;
} }
public static long calculateCrc(InputStream is) throws IOException { public static void deleteFolderIfExists(Path target) throws IOException {
CRC32 crcMaker = new CRC32(); if (Files.notExists(target)) return;
byte[] buffer = new byte[65536]; Files.walkFileTree(target, new SimpleFileVisitor<>() {
int bytesRead; @Override
while((bytesRead = is.read(buffer)) != -1) { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
crcMaker.update(buffer, 0, bytesRead); throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
} }
return crcMaker.getValue();
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e)
throws IOException {
if (e == null) {
Files.delete(dir);
return FileVisitResult.CONTINUE;
} else {
throw e;
}
}
});
} }
} }