From e14bda80851a137214ab2991e73e5a80ca9e2517 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Fri, 28 Dec 2018 13:21:52 -0800 Subject: [PATCH] Avoid unnecessary File.delete() for nonexistent directory This is a follow up CL to my previous CL [1], which aimed to delete /data/system/inputmethod/ when it's empty. One thing I overlooked was the case where the directory does not exist already. In that case, further trying to delete such a nonexistent directory simply failed hence resulted in outputting the following error message to logcat. Failed to delete the empty parent directory /data/system/inputmethod With this CL, the existence of /data/system/inputmethod/ will be checked first to see if there is anything to clean up in the first place. [1]: I2307b5e4edf7b90d2fc03138f233d6051f80cf90 b1a3fdb99f5c577227fe80298f01bfbeb01322ad Bug: 121223050 Test: Manually verified as follows 1. Build and flash aosp_taimen-userdebug into taimen 2. Wait until the device fully boots up 3. adb reboot # to avoid Bug 121259290 4. adb root 5. adb shell cat /data/system/inputmethod/subtypes.xml -> make sure the content looks as follows: 6. Open AOSP Keyboard settings 7. Go to "Appearance & Layouts" -> "Custom input styles" 8. Remove all layouts 9. adb shell cat /data/system/inputmethod/subtypes.xml -> make sure the file no longer exists 10. adb shell ls /data/system/inputmethod -> make sure the directory no longer exists 11. adb reboot 12. adb logcat -s InputMethodManagerService:* -> no error message about /data/system/inputmethod Change-Id: I0428a1ec6300ef346503c53928ddbcf5bb38aab0 --- .../server/inputmethod/InputMethodManagerService.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java index ad0886145496a..99979c18b5201 100644 --- a/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java +++ b/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java @@ -4237,11 +4237,15 @@ public class InputMethodManagerService extends IInputMethodManager.Stub ArrayMap> allSubtypes, AtomicFile subtypesFile, ArrayMap methodMap) { if (allSubtypes.isEmpty()) { + final File parentDir = subtypesFile.getBaseFile().getParentFile(); + if (parentDir == null || !parentDir.exists()) { + // Even the parent directory doesn't exist. There is nothing to clean up. + return; + } if (subtypesFile.exists()) { subtypesFile.delete(); } - final File parentDir = subtypesFile.getBaseFile().getParentFile(); - if (parentDir != null && FileUtils.listFilesOrEmpty(parentDir).length == 0) { + if (FileUtils.listFilesOrEmpty(parentDir).length == 0) { if (!parentDir.delete()) { Slog.e(TAG, "Failed to delete the empty parent directory " + parentDir); }