Merge "Remove stale old path from database on file rename"
diff --git a/src/com/android/providers/media/MediaProvider.java b/src/com/android/providers/media/MediaProvider.java
index d0fb1a4..3559674 100644
--- a/src/com/android/providers/media/MediaProvider.java
+++ b/src/com/android/providers/media/MediaProvider.java
@@ -1365,6 +1365,22 @@
     }
 
     /**
+     * Renames file or directory in lower file system and deletes {@code oldPath} from database.
+     */
+    private int renameUncheckedForFuse(String oldPath, String newPath) {
+        final int result = renameInLowerFs(oldPath, newPath);
+        if (result == 0) {
+            LocalCallingIdentity token = clearLocalCallingIdentity();
+            try {
+                scanFile(new File(oldPath), REASON_DEMAND);
+            } finally {
+                restoreLocalCallingIdentity(token);
+            }
+        }
+        return result;
+    }
+
+    /**
      * Rename file or directory from {@code oldPath} to {@code newPath}.
      *
      * @param oldPath path of the file or directory to be renamed.
@@ -1388,7 +1404,7 @@
         try {
             if (shouldBypassFuseRestrictions(/*forWrite*/ true, oldPath)
                     && shouldBypassFuseRestrictions(/*forWrite*/ true, newPath)) {
-                return renameInLowerFs(oldPath, newPath);
+                return renameUncheckedForFuse(oldPath, newPath);
             }
 
             // Allow legacy app without storage permission to rename files only in its external
diff --git a/tests/jni/FuseDaemonTest/src/com/android/tests/fused/FilePathAccessTest.java b/tests/jni/FuseDaemonTest/src/com/android/tests/fused/FilePathAccessTest.java
index 8c8ee92..a76ff17 100644
--- a/tests/jni/FuseDaemonTest/src/com/android/tests/fused/FilePathAccessTest.java
+++ b/tests/jni/FuseDaemonTest/src/com/android/tests/fused/FilePathAccessTest.java
@@ -911,7 +911,11 @@
             // Can create an image anywhere
             assertCanCreateFile(topLevelImageFile);
             assertCanCreateFile(imageInAnObviouslyWrongPlace);
+
+            // Put the file back in its place and let TEST_APP_A delete it
+            assertThat(otherAppImageFile.createNewFile()).isTrue();
         } finally {
+            deleteFileAsNoThrow(TEST_APP_A, otherAppImageFile.getAbsolutePath());
             otherAppImageFile.delete();
             uninstallApp(TEST_APP_A);
             denyAppOpsToUid(Process.myUid(), SYSTEM_GALERY_APPOPS);
@@ -999,11 +1003,7 @@
             // However, we can't convert it to a music file, because system gallery has full access
             // to images and video only
             assertThat(imageFile.renameTo(musicFile)).isFalse();
-
-            // Rename file back to it's original name so that the test app can clean it up
-            assertThat(imageFile.renameTo(otherAppVideoFile)).isTrue();
         } finally {
-            deleteFileAs(TEST_APP_A, otherAppVideoFile.getPath());
             uninstallApp(TEST_APP_A);
             imageFile.delete();
             videoFile.delete();
@@ -1322,9 +1322,9 @@
             assertThat(otherAppImage.createNewFile()).isTrue();
             assertThat(otherAppMusic.createNewFile()).isTrue();
         } finally {
-            otherAppPdf.delete();
-            otherAppImage.delete();
-            otherAppMusic.delete();
+            deleteFileAsNoThrow(TEST_APP_A, otherAppPdf.getAbsolutePath());
+            deleteFileAsNoThrow(TEST_APP_A, otherAppImage.getAbsolutePath());
+            deleteFileAsNoThrow(TEST_APP_A, otherAppMusic.getAbsolutePath());
             dropShellPermissionIdentity();
             uninstallApp(TEST_APP_A);
         }
@@ -1373,9 +1373,6 @@
             assertThat(pdfInObviouslyWrongPlace.exists()).isFalse();
             assertFileContent(musicFile, BYTES_DATA1);
 
-            // Rename file back to it's original name so that the test app can clean it up
-            assertThat(musicFile.renameTo(otherAppPdf)).isTrue();
-            assertThat(deleteFileAs(TEST_APP_A, otherAppPdf.getPath())).isTrue();
         } finally {
             pdf.delete();
             pdfInObviouslyWrongPlace.delete();