|
20 | 20 | #include <sys/statvfs.h>
|
21 | 21 | #include <unistd.h>
|
22 | 22 |
|
23 |
| -#include <algorithm> |
24 | 23 | #include <cerrno>
|
25 | 24 | #include <cstddef>
|
26 | 25 | #include <cstdint>
|
@@ -231,7 +230,7 @@ absl::Status Mounts::Remove(absl::string_view path) {
|
231 | 230 | return absl::NotFoundError(
|
232 | 231 | absl::StrCat("Path does not exist in mounts: ", path));
|
233 | 232 | }
|
234 |
| - curtree = it->second.mutable_sub_tree(); |
| 233 | + curtree = &it->second; |
235 | 234 | }
|
236 | 235 | curtree->clear_node();
|
237 | 236 | curtree->clear_entries();
|
@@ -281,29 +280,25 @@ absl::Status Mounts::Insert(absl::string_view path,
|
281 | 280 |
|
282 | 281 | std::vector<absl::string_view> parts =
|
283 | 282 | absl::StrSplit(absl::StripPrefix(fixed_path, "/"), '/');
|
| 283 | + std::string final_part(parts.back()); |
| 284 | + parts.pop_back(); |
284 | 285 |
|
285 | 286 | MountTree* curtree = &mount_tree_;
|
286 |
| - int i = 0; |
287 |
| - while (true) { |
288 |
| - std::string part(parts[i]); |
289 |
| - auto [it, did_insert] = curtree->mutable_entries()->insert( |
290 |
| - {std::string(part), MountTree::MountMapEntry()}); |
291 |
| - auto& entry = it->second; |
292 |
| - if (did_insert) { |
293 |
| - entry.set_index(++mount_index_); |
294 |
| - } |
295 |
| - curtree = entry.mutable_sub_tree(); |
296 |
| - if (i == parts.size() - 1) { // Final part |
297 |
| - break; |
298 |
| - } |
299 |
| - ++i; |
| 287 | + for (absl::string_view part : parts) { |
| 288 | + curtree = &(curtree->mutable_entries() |
| 289 | + ->insert({std::string(part), MountTree()}) |
| 290 | + .first->second); |
300 | 291 | if (curtree->has_node() && curtree->node().has_file_node()) {
|
301 | 292 | return absl::FailedPreconditionError(
|
302 | 293 | absl::StrCat("Cannot insert ", path,
|
303 | 294 | " since a file is mounted as a parent directory"));
|
304 | 295 | }
|
305 | 296 | }
|
306 | 297 |
|
| 298 | + curtree = &(curtree->mutable_entries() |
| 299 | + ->insert({final_part, MountTree()}) |
| 300 | + .first->second); |
| 301 | + |
307 | 302 | if (curtree->has_node()) {
|
308 | 303 | if (internal::IsEquivalentNode(curtree->node(), new_node)) {
|
309 | 304 | SAPI_RAW_LOG(INFO, "Inserting %s with the same value twice",
|
@@ -379,7 +374,7 @@ absl::StatusOr<std::string> Mounts::ResolvePath(absl::string_view path) const {
|
379 | 374 | }
|
380 | 375 | return absl::NotFoundError("Path could not be resolved in the mounts");
|
381 | 376 | }
|
382 |
| - curtree = &it->second.sub_tree(); |
| 377 | + curtree = &it->second; |
383 | 378 | tail = parts.second;
|
384 | 379 | }
|
385 | 380 | switch (curtree->node().node_case()) {
|
@@ -692,21 +687,6 @@ void MountWithDefaults(const std::string& source, const std::string& target,
|
692 | 687 | }
|
693 | 688 | }
|
694 | 689 |
|
695 |
| -using MapEntry = std::pair<const std::string, MountTree::MountMapEntry>; |
696 |
| - |
697 |
| -std::vector<const MapEntry*> GetSortedEntries(const MountTree& tree) { |
698 |
| - std::vector<const MapEntry*> ordered_entries; |
699 |
| - ordered_entries.reserve(tree.entries_size()); |
700 |
| - for (const auto& entry : tree.entries()) { |
701 |
| - ordered_entries.push_back(&entry); |
702 |
| - } |
703 |
| - std::sort(ordered_entries.begin(), ordered_entries.end(), |
704 |
| - [](const MapEntry* a, const MapEntry* b) { |
705 |
| - return a->second.index() < b->second.index(); |
706 |
| - }); |
707 |
| - return ordered_entries; |
708 |
| -} |
709 |
| - |
710 | 690 | // Traverses the MountTree to create all required files and perform the mounts.
|
711 | 691 | void CreateMounts(const MountTree& tree, const std::string& path,
|
712 | 692 | bool create_backing_files) {
|
@@ -768,10 +748,9 @@ void CreateMounts(const MountTree& tree, const std::string& path,
|
768 | 748 | }
|
769 | 749 |
|
770 | 750 | // Traverse the subtrees.
|
771 |
| - for (const MapEntry* entry : GetSortedEntries(tree)) { |
772 |
| - auto& [key, value] = *entry; |
773 |
| - std::string new_path = sapi::file::JoinPath(path, key); |
774 |
| - CreateMounts(value.sub_tree(), new_path, create_backing_files); |
| 751 | + for (const auto& kv : tree.entries()) { |
| 752 | + std::string new_path = sapi::file::JoinPath(path, kv.first); |
| 753 | + CreateMounts(kv.second, new_path, create_backing_files); |
775 | 754 | }
|
776 | 755 | }
|
777 | 756 |
|
@@ -802,10 +781,9 @@ void RecursivelyListMountsImpl(const MountTree& tree,
|
802 | 781 | absl::StrCat("tmpfs: ", node.tmpfs_node().tmpfs_options()));
|
803 | 782 | }
|
804 | 783 |
|
805 |
| - for (const MapEntry* entry : GetSortedEntries(tree)) { |
806 |
| - auto& [key, value] = *entry; |
807 |
| - RecursivelyListMountsImpl(value.sub_tree(), |
808 |
| - absl::StrCat(tree_path, "/", key), |
| 784 | + for (const auto& subentry : tree.entries()) { |
| 785 | + RecursivelyListMountsImpl(subentry.second, |
| 786 | + absl::StrCat(tree_path, "/", subentry.first), |
809 | 787 | outside_entries, inside_entries);
|
810 | 788 | }
|
811 | 789 | }
|
|
0 commit comments