Corina | 633099b | 2020-06-17 21:55:33 +0100 | [diff] [blame] | 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 <string> |
| 20 | #include <vector> |
| 21 | |
Corina | 762bb25 | 2020-07-09 10:30:33 +0100 | [diff] [blame] | 22 | #include "android-base/strings.h" |
| 23 | |
Corina | 633099b | 2020-06-17 21:55:33 +0100 | [diff] [blame] | 24 | using std::string; |
| 25 | |
| 26 | namespace mediaprovider { |
| 27 | namespace fuse { |
| 28 | |
Martijn Coenen | fc41bbf | 2021-09-24 10:14:16 +0200 | [diff] [blame] | 29 | bool containsMount(const string& path) { |
Corina | 762bb25 | 2020-07-09 10:30:33 +0100 | [diff] [blame] | 30 | // This method is called from lookup, so it's called rather frequently. |
| 31 | // Hence, we avoid concatenating the strings and we use 3 separate suffixes. |
Corina | 633099b | 2020-06-17 21:55:33 +0100 | [diff] [blame] | 32 | |
Corina | 762bb25 | 2020-07-09 10:30:33 +0100 | [diff] [blame] | 33 | static const string prefix = "/storage/emulated/"; |
| 34 | if (!android::base::StartsWithIgnoreCase(path, prefix)) { |
Corina | 633099b | 2020-06-17 21:55:33 +0100 | [diff] [blame] | 35 | return false; |
| 36 | } |
| 37 | |
Martijn Coenen | fc41bbf | 2021-09-24 10:14:16 +0200 | [diff] [blame] | 38 | // Skip over the user-id by finding the next '/' |
| 39 | size_t pos = path.find_first_of("/", prefix.length()); |
| 40 | // If we can't find another '/', or the '/' immediately follows the previous, |
| 41 | // ('/storage/emulated//'), not a valid mount. |
| 42 | if (pos == std::string::npos || pos == prefix.length()) { |
Corina | 762bb25 | 2020-07-09 10:30:33 +0100 | [diff] [blame] | 43 | return false; |
| 44 | } |
| 45 | |
Martijn Coenen | fc41bbf | 2021-09-24 10:14:16 +0200 | [diff] [blame] | 46 | const string& path_suffix = path.substr(pos); |
| 47 | |
Corina | 762bb25 | 2020-07-09 10:30:33 +0100 | [diff] [blame] | 48 | static const string android_suffix = "/Android"; |
| 49 | static const string data_suffix = "/Android/data"; |
| 50 | static const string obb_suffix = "/Android/obb"; |
| 51 | |
Corina | 762bb25 | 2020-07-09 10:30:33 +0100 | [diff] [blame] | 52 | return android::base::EqualsIgnoreCase(path_suffix, android_suffix) || |
| 53 | android::base::EqualsIgnoreCase(path_suffix, data_suffix) || |
| 54 | android::base::EqualsIgnoreCase(path_suffix, obb_suffix); |
Corina | 633099b | 2020-06-17 21:55:33 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | } // namespace fuse |
| 58 | } // namespace mediaprovider |