blob: 9f3044053477a25dfeb3a83097886c6fa7f5f1bc [file] [log] [blame]
Corina633099b2020-06-17 21:55:33 +01001// 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
Corina762bb252020-07-09 10:30:33 +010022#include "android-base/strings.h"
23
Corina633099b2020-06-17 21:55:33 +010024using std::string;
25
26namespace mediaprovider {
27namespace fuse {
28
Martijn Coenenfc41bbf2021-09-24 10:14:16 +020029bool containsMount(const string& path) {
Corina762bb252020-07-09 10:30:33 +010030 // 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.
Corina633099b2020-06-17 21:55:33 +010032
Corina762bb252020-07-09 10:30:33 +010033 static const string prefix = "/storage/emulated/";
34 if (!android::base::StartsWithIgnoreCase(path, prefix)) {
Corina633099b2020-06-17 21:55:33 +010035 return false;
36 }
37
Martijn Coenenfc41bbf2021-09-24 10:14:16 +020038 // 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()) {
Corina762bb252020-07-09 10:30:33 +010043 return false;
44 }
45
Martijn Coenenfc41bbf2021-09-24 10:14:16 +020046 const string& path_suffix = path.substr(pos);
47
Corina762bb252020-07-09 10:30:33 +010048 static const string android_suffix = "/Android";
49 static const string data_suffix = "/Android/data";
50 static const string obb_suffix = "/Android/obb";
51
Corina762bb252020-07-09 10:30:33 +010052 return android::base::EqualsIgnoreCase(path_suffix, android_suffix) ||
53 android::base::EqualsIgnoreCase(path_suffix, data_suffix) ||
54 android::base::EqualsIgnoreCase(path_suffix, obb_suffix);
Corina633099b2020-06-17 21:55:33 +010055}
56
57} // namespace fuse
58} // namespace mediaprovider