Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 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 | |
| 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. |
Zimuzo Ezeozue | ebac1c8 | 2020-02-21 19:41:33 +0000 | [diff] [blame] | 44 | void node::BuildPathForNodeRecursive(bool safe, const node* node, std::stringstream* path) const { |
| 45 | if (node->parent_) { |
| 46 | BuildPathForNodeRecursive(safe, node->parent_, path); |
| 47 | } |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 48 | |
Zimuzo Ezeozue | ebac1c8 | 2020-02-21 19:41:33 +0000 | [diff] [blame] | 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 | } |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | std::string node::BuildPath() const { |
| 62 | std::lock_guard<std::recursive_mutex> guard(*lock_); |
Zimuzo Ezeozue | ebac1c8 | 2020-02-21 19:41:33 +0000 | [diff] [blame] | 63 | std::stringstream path; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 64 | |
Zimuzo Ezeozue | ebac1c8 | 2020-02-21 19:41:33 +0000 | [diff] [blame] | 65 | BuildPathForNodeRecursive(false, this, &path); |
| 66 | return path.str(); |
| 67 | } |
| 68 | |
| 69 | 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(); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 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) { |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 88 | node = node->LookupChildByName(segment, false /* acquire */); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 89 | if (!node) { |
| 90 | return nullptr; |
| 91 | } |
| 92 | } |
| 93 | return node; |
| 94 | } |
| 95 | |
Biswarup Pal | 93f4ec1 | 2021-02-15 13:39:36 +0000 | [diff] [blame] | 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 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 114 | void node::DeleteTree(node* tree) { |
| 115 | std::lock_guard<std::recursive_mutex> guard(*tree->lock_); |
| 116 | |
| 117 | if (tree) { |
Narayan Kamath | fc867dd | 2020-02-19 13:45:29 +0000 | [diff] [blame] | 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) { |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 123 | DeleteTree(child); |
| 124 | } |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 125 | |
Narayan Kamath | fc867dd | 2020-02-19 13:45:29 +0000 | [diff] [blame] | 126 | CHECK(tree->children_.empty()); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 127 | delete tree; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | } // namespace fuse |
| 132 | } // namespace mediaprovider |