1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #define LOG_TAG "FuseUtils"
16 
17 #include "include/libfuse_jni/FuseUtils.h"
18 
19 #include <regex>
20 #include <string>
21 #include <vector>
22 
23 #include "android-base/strings.h"
24 
25 using std::string;
26 
27 namespace mediaprovider {
28 namespace fuse {
29 
containsMount(const string & path)30 bool containsMount(const string& path) {
31     // This method is called from lookup, so it's called rather frequently.
32     // Hence, we avoid concatenating the strings and we use 3 separate suffixes.
33 
34     static const string prefix = "/storage/emulated/";
35     if (!android::base::StartsWithIgnoreCase(path, prefix)) {
36         return false;
37     }
38 
39     size_t pos = path.find_first_of('/', prefix.length());
40     if (pos == std::string::npos) {
41         return false;
42     }
43 
44     const string& path_suffix = path.substr(pos);
45 
46     static const string android_suffix = "/Android";
47     static const string data_suffix = "/Android/data";
48     static const string obb_suffix = "/Android/obb";
49 
50     return android::base::EqualsIgnoreCase(path_suffix, android_suffix) ||
51            android::base::EqualsIgnoreCase(path_suffix, data_suffix) ||
52            android::base::EqualsIgnoreCase(path_suffix, obb_suffix);
53 }
54 
getVolumeNameFromPath(const std::string & path)55 string getVolumeNameFromPath(const std::string& path) {
56     std::string volume_name = "";
57     if (!android::base::StartsWith(path, STORAGE_PREFIX)) {
58         volume_name = VOLUME_INTERNAL;
59     } else if (android::base::StartsWith(path, PRIMARY_VOLUME_PREFIX) || path == STORAGE_PREFIX) {
60         volume_name = VOLUME_EXTERNAL_PRIMARY;
61     } else {
62         // Use regex to extract volume name
63         std::regex volumeRegex(R"(/storage/([a-zA-Z0-9-]+)/)");
64         std::smatch match;
65         if (std::regex_search(path, match, volumeRegex)) {
66             volume_name = match[1].str();
67             // Convert to lowercase
68             std::transform(volume_name.begin(), volume_name.end(), volume_name.begin(), ::tolower);
69         }
70     }
71     return volume_name;
72 }
73 
74 }  // namespace fuse
75 }  // namespace mediaprovider
76