blob: e17a9e88abe16885b10cef0535291b10c6cbfdad [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 Ezeozue67db40c2020-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 Ezeozue67db40c2020-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 Ezeozue67db40c2020-02-21 19:41:33 +000063 std::stringstream path;
Narayan Kamathaef84a12020-01-02 15:20:13 +000064
Zimuzo Ezeozue67db40c2020-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
96void node::DeleteTree(node* tree) {
97 std::lock_guard<std::recursive_mutex> guard(*tree->lock_);
98
99 if (tree) {
Narayan Kamath568f17a2020-02-19 13:45:29 +0000100 // Make a copy of the list of children because calling Delete tree
101 // will modify the list of children, which will cause issues while
102 // iterating over them.
103 std::vector<node*> children(tree->children_.begin(), tree->children_.end());
104 for (node* child : children) {
Narayan Kamathaef84a12020-01-02 15:20:13 +0000105 DeleteTree(child);
106 }
Narayan Kamathaef84a12020-01-02 15:20:13 +0000107
Narayan Kamath568f17a2020-02-19 13:45:29 +0000108 CHECK(tree->children_.empty());
Narayan Kamathaef84a12020-01-02 15:20:13 +0000109 delete tree;
110 }
111}
112
113} // namespace fuse
114} // namespace mediaprovider