Skip to content

Commit

Permalink
[Installer] Fix installing some signed APK files
Browse files Browse the repository at this point in the history
Some signed APKs could not be installed due size mismatch.

Signed-off-by: Muntashir Al-Islam <[email protected]>
  • Loading branch information
MuntashirAkon committed Sep 7, 2023
1 parent 12c288a commit 61a51f5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import io.github.muntashirakon.AppManager.settings.Prefs;
import io.github.muntashirakon.AppManager.utils.ArrayUtils;
import io.github.muntashirakon.AppManager.utils.ContextUtils;
import io.github.muntashirakon.AppManager.utils.DigestUtils;
import io.github.muntashirakon.AppManager.utils.FileUtils;
import io.github.muntashirakon.AppManager.utils.LangUtils;
import io.github.muntashirakon.io.IoUtils;
Expand Down Expand Up @@ -707,6 +708,18 @@ public long getFileSize() {
else throw new RuntimeException("Neither zipEntry nor source is defined.");
}

/**
* Get size of the entry or {@code -1} if unavailable
*/
@WorkerThread
public long getFileSize(boolean signed) {
try {
return (signed ? getSignedFile() : getRealCachedFile()).length();
} catch (IOException e) {
return -1;
}
}

@WorkerThread
public File getFile(boolean signed) throws IOException {
return signed ? getSignedFile() : getRealCachedFile();
Expand Down Expand Up @@ -734,12 +747,14 @@ private File getSignedFile() throws IOException {
mIdsigFile = mFileCache.createCachedFile("idsig");
signer.setIdsigFile(mIdsigFile);
}
if (signer.sign(realFile, mSignedFile, -1, zipAlign)) {
if (Signer.verify(sigSchemes, mSignedFile, mIdsigFile)) {
return mSignedFile;
}
if (signer.sign(realFile, mSignedFile, -1, zipAlign)
&& Signer.verify(sigSchemes, mSignedFile, mIdsigFile)) {
DigestUtils.getHexDigest(DigestUtils.SHA_256, mSignedFile);
return mSignedFile;
}
throw new IOException("Failed to sign " + realFile);
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new IOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,9 @@ public boolean install(@NonNull ApkFile apkFile, @NonNull List<String> selectedS
Log.d(TAG, "Install: selected entries: %s", selectedSplitIds);
// Write apk files
for (ApkFile.Entry entry : selectedEntries) {
long entrySize = entry.getFileSize(options.isSignApkFiles());
try (InputStream apkInputStream = entry.getInputStream(options.isSignApkFiles());
OutputStream apkOutputStream = mSession.openWrite(entry.getFileName(), 0, entry.getFileSize())) {
OutputStream apkOutputStream = mSession.openWrite(entry.getFileName(), 0, entrySize)) {
IoUtils.copy(apkInputStream, apkOutputStream, totalSize, progressHandler);
mSession.fsync(apkOutputStream);
Log.d(TAG, "Install: copied entry %s", entry.name);
Expand Down

0 comments on commit 61a51f5

Please sign in to comment.