Call access(2) on all files/dirs modified by Mtp

External sdcards are accessed through /mnt/media_rw,
so access() each touched file for sdcardfs to update
its metadata. This is done for all created/deleted/renamed
folders and files.

Bug: 77849654
Test: use mtp with emulated sdcard
Change-Id: Ic8cef9dc90e9cbc9783ff45a87c481833f910665
diff --git a/media/mtp/MtpUtils.cpp b/media/mtp/MtpUtils.cpp
index 51cfd7d..8564576 100644
--- a/media/mtp/MtpUtils.cpp
+++ b/media/mtp/MtpUtils.cpp
@@ -36,6 +36,13 @@
 
 constexpr unsigned long FILE_COPY_SIZE = 262144;
 
+static void access_ok(const char *path) {
+    if (access(path, F_OK) == -1) {
+        // Ignore. Failure could be common in cases of delete where
+        // the metadata was updated through other paths.
+    }
+}
+
 /*
 DateTime strings follow a compatible subset of the definition found in ISO 8601, and
 take the form of a Unicode string formatted as: "YYYYMMDDThhmmss.s". In this
@@ -101,6 +108,7 @@
     } else {
         chown((const char *)path, getuid(), FILE_GROUP);
     }
+    access_ok(path);
     return ret;
 }
 
@@ -181,6 +189,7 @@
     LOG(DEBUG) << "Copied a file with MTP. Time: " << diff.count() << " s, Size: " << length <<
         ", Rate: " << ((double) length) / diff.count() << " bytes/s";
     chown(toPath, getuid(), FILE_GROUP);
+    access_ok(toPath);
     return ret == -1 ? -1 : 0;
 }
 
@@ -212,6 +221,7 @@
         } else {
             success = unlink(childPath.c_str());
         }
+        access_ok(childPath.c_str());
         if (success == -1)
             PLOG(ERROR) << "Deleting path " << childPath << " failed";
     }
@@ -236,7 +246,22 @@
     }
     if (success == -1)
         PLOG(ERROR) << "Deleting path " << path << " failed";
+    access_ok(path);
     return success == 0;
 }
 
+int renameTo(const char *oldPath, const char *newPath) {
+    int ret = rename(oldPath, newPath);
+    access_ok(oldPath);
+    access_ok(newPath);
+    return ret;
+}
+
+// Calls access(2) on the path to update underlying filesystems,
+// then closes the fd.
+void closeObjFd(int fd, const char *path) {
+    close(fd);
+    access_ok(path);
+}
+
 }  // namespace android