Skip to content

Commit 3f99afc

Browse files
committed
Close streams for forward-locked apps
Forward-locked apps aren't very prevalent, but it needed to be restructured to make sure both streams and ZipFile objects are closed. Change-Id: I41f863224fecd24069e525e9ce3738de8237bd5e
1 parent a94afeb commit 3f99afc

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

services/java/com/android/server/pm/PackageManagerService.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
import java.util.zip.ZipOutputStream;
140140

141141
import libcore.io.ErrnoException;
142+
import libcore.io.IoUtils;
142143
import libcore.io.Libcore;
143144

144145
/**
@@ -6979,9 +6980,8 @@ private int setPermissionsLI(PackageParser.Package newPackage) {
69796980
} catch (IOException e) {
69806981
Slog.e(TAG, "Couldn't create a new zip file for the public parts of a" +
69816982
" forward-locked app.");
6983+
destResourceFile.delete();
69826984
return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
6983-
} finally {
6984-
//TODO clean up the extracted public files
69856985
}
69866986
retCode = mInstaller.setForwardLockPerm(getApkName(newPackage.mPath),
69876987
newPackage.applicationInfo.uid);
@@ -7023,38 +7023,34 @@ private void extractPublicFiles(PackageParser.Package newPackage,
70237023
File publicZipFile) throws IOException {
70247024
final FileOutputStream fstr = new FileOutputStream(publicZipFile);
70257025
final ZipOutputStream publicZipOutStream = new ZipOutputStream(fstr);
7026-
final ZipFile privateZip = new ZipFile(newPackage.mPath);
7027-
7028-
// Copy manifest, resources.arsc and res directory to public zip
7026+
try {
7027+
final ZipFile privateZip = new ZipFile(newPackage.mPath);
7028+
try {
7029+
// Copy manifest, resources.arsc and res directory to public zip
70297030

7030-
final Enumeration<? extends ZipEntry> privateZipEntries = privateZip.entries();
7031-
while (privateZipEntries.hasMoreElements()) {
7032-
final ZipEntry zipEntry = privateZipEntries.nextElement();
7033-
final String zipEntryName = zipEntry.getName();
7034-
if ("AndroidManifest.xml".equals(zipEntryName)
7035-
|| "resources.arsc".equals(zipEntryName)
7036-
|| zipEntryName.startsWith("res/")) {
7037-
try {
7038-
copyZipEntry(zipEntry, privateZip, publicZipOutStream);
7039-
} catch (IOException e) {
7040-
try {
7041-
publicZipOutStream.close();
7042-
throw e;
7043-
} finally {
7044-
publicZipFile.delete();
7031+
final Enumeration<? extends ZipEntry> privateZipEntries = privateZip.entries();
7032+
while (privateZipEntries.hasMoreElements()) {
7033+
final ZipEntry zipEntry = privateZipEntries.nextElement();
7034+
final String zipEntryName = zipEntry.getName();
7035+
if ("AndroidManifest.xml".equals(zipEntryName)
7036+
|| "resources.arsc".equals(zipEntryName)
7037+
|| zipEntryName.startsWith("res/")) {
7038+
copyZipEntry(zipEntry, privateZip, publicZipOutStream);
70457039
}
70467040
}
7041+
} finally {
7042+
try { privateZip.close(); } catch (IOException e) { }
70477043
}
7048-
}
70497044

7050-
publicZipOutStream.finish();
7051-
publicZipOutStream.flush();
7052-
FileUtils.sync(fstr);
7053-
publicZipOutStream.close();
7054-
FileUtils.setPermissions(
7055-
publicZipFile.getAbsolutePath(),
7056-
FileUtils.S_IRUSR|FileUtils.S_IWUSR|FileUtils.S_IRGRP|FileUtils.S_IROTH,
7057-
-1, -1);
7045+
publicZipOutStream.finish();
7046+
publicZipOutStream.flush();
7047+
FileUtils.sync(fstr);
7048+
publicZipOutStream.close();
7049+
FileUtils.setPermissions(publicZipFile.getAbsolutePath(), FileUtils.S_IRUSR
7050+
| FileUtils.S_IWUSR | FileUtils.S_IRGRP | FileUtils.S_IROTH, -1, -1);
7051+
} finally {
7052+
IoUtils.closeQuietly(publicZipOutStream);
7053+
}
70587054
}
70597055

70607056
private static void copyZipEntry(ZipEntry zipEntry,
@@ -7073,11 +7069,15 @@ private static void copyZipEntry(ZipEntry zipEntry,
70737069
}
70747070
outZipStream.putNextEntry(newEntry);
70757071

7076-
InputStream data = inZipFile.getInputStream(zipEntry);
7077-
while ((num = data.read(buffer)) > 0) {
7078-
outZipStream.write(buffer, 0, num);
7072+
final InputStream data = inZipFile.getInputStream(zipEntry);
7073+
try {
7074+
while ((num = data.read(buffer)) > 0) {
7075+
outZipStream.write(buffer, 0, num);
7076+
}
7077+
outZipStream.flush();
7078+
} finally {
7079+
IoUtils.closeQuietly(data);
70797080
}
7080-
outZipStream.flush();
70817081
}
70827082

70837083
private void deleteTempPackageFiles() {

0 commit comments

Comments
 (0)