Change containsMount() to look for any user-id.

On devices with app cloning, we may be responsible for storage paths
that represent a different user-id than the one that we are running as.
Change containsMount() to accept any user-id.

Bug: 200134631
Test: TEST_MAPPING, atest FuseUtilsTest
Change-Id: I4387beeed5925b9e43c4f45d0f5e9ed2f6a196d5
Merged-In: I4387beeed5925b9e43c4f45d0f5e9ed2f6a196d5
diff --git a/jni/FuseUtils.cpp b/jni/FuseUtils.cpp
index 7829888..9f30440 100644
--- a/jni/FuseUtils.cpp
+++ b/jni/FuseUtils.cpp
@@ -26,7 +26,7 @@
 namespace mediaprovider {
 namespace fuse {
 
-bool containsMount(const string& path, const string& userid) {
+bool containsMount(const string& path) {
     // This method is called from lookup, so it's called rather frequently.
     // Hence, we avoid concatenating the strings and we use 3 separate suffixes.
 
@@ -35,16 +35,20 @@
         return false;
     }
 
-    const string& rest_of_path = path.substr(prefix.length());
-    if (!android::base::StartsWithIgnoreCase(rest_of_path, userid)) {
+    // Skip over the user-id by finding the next '/'
+    size_t pos = path.find_first_of("/", prefix.length());
+    // If we can't find another '/', or the '/' immediately follows the previous,
+    // ('/storage/emulated//'), not a valid mount.
+    if (pos == std::string::npos || pos == prefix.length()) {
         return false;
     }
 
+    const string& path_suffix = path.substr(pos);
+
     static const string android_suffix = "/Android";
     static const string data_suffix = "/Android/data";
     static const string obb_suffix = "/Android/obb";
 
-    const string& path_suffix = rest_of_path.substr(userid.length());
     return android::base::EqualsIgnoreCase(path_suffix, android_suffix) ||
            android::base::EqualsIgnoreCase(path_suffix, data_suffix) ||
            android::base::EqualsIgnoreCase(path_suffix, obb_suffix);