summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Kohsuke Yatoh <kyatoh@google.com> 2021-04-11 15:13:49 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2021-04-11 15:13:49 +0000
commitd62afd949e89dd3058c244f68a295fd44cd06ccd (patch)
treebe001813a8f646531b69e7fba2e8e360d9727285
parent9ac9fe53bfc0491de3c3b7ebe756a8b3311dc416 (diff)
parent2dd8bdc64501be87f0b0f6abb956ffdfd95fc819 (diff)
Merge "Close files in FontManagerShellCommand." into sc-dev
-rw-r--r--services/core/java/com/android/server/graphics/fonts/FontManagerShellCommand.java55
1 files changed, 30 insertions, 25 deletions
diff --git a/services/core/java/com/android/server/graphics/fonts/FontManagerShellCommand.java b/services/core/java/com/android/server/graphics/fonts/FontManagerShellCommand.java
index cf9a79f69f19..5457ef2c77a9 100644
--- a/services/core/java/com/android/server/graphics/fonts/FontManagerShellCommand.java
+++ b/services/core/java/com/android/server/graphics/fonts/FontManagerShellCommand.java
@@ -317,40 +317,45 @@ public class FontManagerShellCommand extends ShellCommand {
"Signature file argument is required.");
}
- // TODO: close fontFd and sigFd.
- ParcelFileDescriptor fontFd = shell.openFileForSystem(fontPath, "r");
- if (fontFd == null) {
- throw new SystemFontException(
- FontManager.RESULT_ERROR_FAILED_TO_OPEN_FONT_FILE,
- "Failed to open font file");
- }
-
- ParcelFileDescriptor sigFd = shell.openFileForSystem(signaturePath, "r");
- if (sigFd == null) {
- throw new SystemFontException(
- FontManager.RESULT_ERROR_FAILED_TO_OPEN_SIGNATURE_FILE,
- "Failed to open signature file");
- }
+ try (ParcelFileDescriptor fontFd = shell.openFileForSystem(fontPath, "r");
+ ParcelFileDescriptor sigFd = shell.openFileForSystem(signaturePath, "r")) {
+ if (fontFd == null) {
+ throw new SystemFontException(
+ FontManager.RESULT_ERROR_FAILED_TO_OPEN_FONT_FILE,
+ "Failed to open font file");
+ }
- try (FileInputStream sigFis = new FileInputStream(sigFd.getFileDescriptor())) {
- int len = sigFis.available();
- if (len > MAX_SIGNATURE_FILE_SIZE_BYTES) {
+ if (sigFd == null) {
throw new SystemFontException(
- FontManager.RESULT_ERROR_SIGNATURE_TOO_LARGE,
- "Signature file is too large");
+ FontManager.RESULT_ERROR_FAILED_TO_OPEN_SIGNATURE_FILE,
+ "Failed to open signature file");
}
- byte[] signature = new byte[len];
- if (sigFis.read(signature, 0, len) != len) {
+
+ byte[] signature;
+ try (FileInputStream sigFis = new FileInputStream(sigFd.getFileDescriptor())) {
+ int len = sigFis.available();
+ if (len > MAX_SIGNATURE_FILE_SIZE_BYTES) {
+ throw new SystemFontException(
+ FontManager.RESULT_ERROR_SIGNATURE_TOO_LARGE,
+ "Signature file is too large");
+ }
+ signature = new byte[len];
+ if (sigFis.read(signature, 0, len) != len) {
+ throw new SystemFontException(
+ FontManager.RESULT_ERROR_INVALID_SIGNATURE_FILE,
+ "Invalid read length");
+ }
+ } catch (IOException e) {
throw new SystemFontException(
FontManager.RESULT_ERROR_INVALID_SIGNATURE_FILE,
- "Invalid read length");
+ "Failed to read signature file.", e);
}
mService.update(
-1, Collections.singletonList(new FontUpdateRequest(fontFd, signature)));
} catch (IOException e) {
- throw new SystemFontException(
- FontManager.RESULT_ERROR_INVALID_SIGNATURE_FILE,
- "Failed to read signature file.", e);
+ // We should reach here only when close() threw IOException.
+ // shell.openFileForSystem() and FontManagerService.update() don't throw IOException.
+ Slog.w(TAG, "Error while closing files", e);
}
shell.getOutPrintWriter().println("Success"); // TODO: Output more details.