1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specic language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "node-inl.h"
18
GetPathSegments(int segment_start,const std::string & path)19 static std::vector<std::string> GetPathSegments(int segment_start, const std::string& path) {
20 std::vector<std::string> segments;
21 int segment_end = path.find_first_of('/', segment_start);
22
23 while (segment_end != std::string::npos) {
24 if (segment_end == segment_start) {
25 // First character is '/' ignore
26 segment_end = path.find_first_of('/', ++segment_start);
27 continue;
28 }
29
30 segments.push_back(path.substr(segment_start, segment_end - segment_start));
31 segment_start = segment_end + 1;
32 segment_end = path.find_first_of('/', segment_start);
33 }
34 if (segment_start < path.size()) {
35 segments.push_back(path.substr(segment_start));
36 }
37 return segments;
38 }
39
40 namespace mediaprovider {
41 namespace fuse {
42
43 // Assumes that |node| has at least one child.
BuildPathForNodeRecursive(bool safe,const node * node,std::stringstream * path) const44 void node::BuildPathForNodeRecursive(bool safe, const node* node, std::stringstream* path) const {
45 if (node->parent_) {
46 BuildPathForNodeRecursive(safe, node->parent_, path);
47 }
48
49 if (safe && node->parent_) {
50 (*path) << reinterpret_cast<uintptr_t>(node);
51 } else {
52 (*path) << node->GetName();
53 }
54
55 if (node != this) {
56 // Must not add a '/' to the last segment
57 (*path) << "/";
58 }
59 }
60
BuildPath() const61 std::string node::BuildPath() const {
62 std::lock_guard<std::recursive_mutex> guard(*lock_);
63 std::stringstream path;
64
65 BuildPathForNodeRecursive(false, this, &path);
66 return path.str();
67 }
68
BuildSafePath() const69 std::string node::BuildSafePath() const {
70 std::lock_guard<std::recursive_mutex> guard(*lock_);
71 std::stringstream path;
72
73 BuildPathForNodeRecursive(true, this, &path);
74 return path.str();
75 }
76
LookupAbsolutePath(const node * root,const std::string & absolute_path)77 const node* node::LookupAbsolutePath(const node* root, const std::string& absolute_path) {
78 if (absolute_path.find(root->GetName()) != 0) {
79 return nullptr;
80 }
81
82 std::vector<std::string> segments = GetPathSegments(root->GetName().size(), absolute_path);
83
84 std::lock_guard<std::recursive_mutex> guard(*root->lock_);
85
86 const node* node = root;
87 for (const std::string& segment : segments) {
88 node = node->LookupChildByName(segment, false /* acquire */);
89 if (!node) {
90 return nullptr;
91 }
92 }
93 return node;
94 }
95
LookupInode(const node * root,ino_t ino)96 const node* node::LookupInode(const node* root, ino_t ino) {
97 CHECK(root);
98
99 std::lock_guard<std::recursive_mutex> guard(*root->lock_);
100
101 if ((root->ino_ == ino) && !root->deleted_ && !(root->handles_.empty())) {
102 return root;
103 }
104
105 for (node* child : root->children_) {
106 const node* node = LookupInode(child, ino);
107 if (node) {
108 return node;
109 }
110 }
111 return nullptr;
112 }
113
DeleteTree(node * tree)114 void node::DeleteTree(node* tree) {
115 std::lock_guard<std::recursive_mutex> guard(*tree->lock_);
116
117 if (tree) {
118 // Make a copy of the list of children because calling Delete tree
119 // will modify the list of children, which will cause issues while
120 // iterating over them.
121 std::vector<node*> children(tree->children_.begin(), tree->children_.end());
122 for (node* child : children) {
123 DeleteTree(child);
124 }
125
126 CHECK(tree->children_.empty());
127 delete tree;
128 }
129 }
130
131 } // namespace fuse
132 } // namespace mediaprovider
133