blob: 31e49709693ab2e1327059be7fefae5f0572ca72 [file] [log] [blame]
Narayan Kamathaef84a12020-01-02 15:20:13 +00001/*
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
19static 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
40namespace mediaprovider {
41namespace fuse {
42
43// Assumes that |node| has at least one child.
Zimuzo Ezeozueebac1c82020-02-21 19:41:33 +000044void node::BuildPathForNodeRecursive(bool safe, const node* node, std::stringstream* path) const {
45 if (node->parent_) {
46 BuildPathForNodeRecursive(safe, node->parent_, path);
47 }
Narayan Kamathaef84a12020-01-02 15:20:13 +000048
Zimuzo Ezeozueebac1c82020-02-21 19:41:33 +000049 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 }
Narayan Kamathaef84a12020-01-02 15:20:13 +000059}
60
61std::string node::BuildPath() const {
62 std::lock_guard<std::recursive_mutex> guard(*lock_);
Zimuzo Ezeozueebac1c82020-02-21 19:41:33 +000063 std::stringstream path;
Narayan Kamathaef84a12020-01-02 15:20:13 +000064
Zimuzo Ezeozueebac1c82020-02-21 19:41:33 +000065 BuildPathForNodeRecursive(false, this, &path);
66 return path.str();
67}
68
69std::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();
Narayan Kamathaef84a12020-01-02 15:20:13 +000075}
76
77const 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) {
Narayan Kamatheca34252020-02-11 13:08:37 +000088 node = node->LookupChildByName(segment, false /* acquire */);
Narayan Kamathaef84a12020-01-02 15:20:13 +000089 if (!node) {
90 return nullptr;
91 }
92 }
93 return node;
94}
95
Biswarup Pal93f4ec12021-02-15 13:39:36 +000096const 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
Narayan Kamathaef84a12020-01-02 15:20:13 +0000114void node::DeleteTree(node* tree) {
115 std::lock_guard<std::recursive_mutex> guard(*tree->lock_);
116
117 if (tree) {
Narayan Kamathfc867dd2020-02-19 13:45:29 +0000118 // 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) {
Narayan Kamathaef84a12020-01-02 15:20:13 +0000123 DeleteTree(child);
124 }
Narayan Kamathaef84a12020-01-02 15:20:13 +0000125
Narayan Kamathfc867dd2020-02-19 13:45:29 +0000126 CHECK(tree->children_.empty());
Narayan Kamathaef84a12020-01-02 15:20:13 +0000127 delete tree;
128 }
129}
130
131} // namespace fuse
132} // namespace mediaprovider