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
      b1a3fdb99f

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:
         <subtypes>
           <imi id="com.android.inputmethod.latin/.LatinIME">
             <subtype ....>
             <subtype ....>
           </imi/>
         </subtypes>
  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
This commit is contained in:
Yohei Yukawa
2018-12-28 13:21:52 -08:00
parent ade742872c
commit e14bda8085

View File

@@ -4237,11 +4237,15 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
ArrayMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
ArrayMap<String, InputMethodInfo> 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);
}