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. |
| 44 | void node::BuildPathForNodeRecursive(node* node, std::string* path) { |
| 45 | if (node->parent_) BuildPathForNodeRecursive(node->parent_, path); |
| 46 | |
| 47 | (*path) += node->GetName() + "/"; |
| 48 | } |
| 49 | |
| 50 | std::string node::BuildPath() const { |
| 51 | std::lock_guard<std::recursive_mutex> guard(*lock_); |
| 52 | std::string path; |
| 53 | |
| 54 | path.reserve(PATH_MAX); |
| 55 | if (parent_) BuildPathForNodeRecursive(parent_, &path); |
| 56 | path += name_; |
| 57 | return path; |
| 58 | } |
| 59 | |
| 60 | const node* node::LookupAbsolutePath(const node* root, const std::string& absolute_path) { |
| 61 | if (absolute_path.find(root->GetName()) != 0) { |
| 62 | return nullptr; |
| 63 | } |
| 64 | |
| 65 | std::vector<std::string> segments = GetPathSegments(root->GetName().size(), absolute_path); |
| 66 | |
| 67 | std::lock_guard<std::recursive_mutex> guard(*root->lock_); |
| 68 | |
| 69 | const node* node = root; |
| 70 | for (const std::string& segment : segments) { |
| 71 | node = node->LookupChildByName(segment); |
| 72 | if (!node) { |
| 73 | return nullptr; |
| 74 | } |
| 75 | } |
| 76 | return node; |
| 77 | } |
| 78 | |
| 79 | void node::DeleteTree(node* tree) { |
| 80 | std::lock_guard<std::recursive_mutex> guard(*tree->lock_); |
| 81 | |
| 82 | if (tree) { |
| 83 | for (node* child : tree->children_) { |
| 84 | DeleteTree(child); |
| 85 | } |
| 86 | tree->children_.clear(); |
| 87 | |
| 88 | LOG(DEBUG) << "DELETE node " << tree->GetName(); |
| 89 | tree->RemoveFromParent(); |
| 90 | delete tree; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | } // namespace fuse |
| 95 | } // namespace mediaprovider |