Skip to content

Commit a5469f3

Browse files
cblichmanncopybara-github
authored andcommitted
Automated rollback of commit 2ba4460.
PiperOrigin-RevId: 681393675 Change-Id: If591c8f4813c4630f75785fb36bc1c33f62bff1d
1 parent 2ba4460 commit a5469f3

File tree

5 files changed

+21
-72
lines changed

5 files changed

+21
-72
lines changed

sandboxed_api/sandbox2/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,6 @@ cc_test(
728728
copts = sapi_platform_copts(),
729729
data = ["//sandboxed_api/sandbox2/testcases:minimal_dynamic"],
730730
deps = [
731-
":mount_tree_cc_proto",
732731
":mounts",
733732
"//sandboxed_api:testing",
734733
"//sandboxed_api/util:file_base",

sandboxed_api/sandbox2/mount_tree.proto

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,7 @@ message MountTree {
5656
}
5757

5858
// The entries are mappings from the next path component to the subtree.
59-
message MountMapEntry {
60-
MountTree sub_tree = 1;
61-
// Helps to keep a stable and deterministic order of mounts. Protobuf makes
62-
// no guarantees about the order of map entries and may even randomize in
63-
// certain build configurations.
64-
int64 index = 2;
65-
}
66-
map<string, MountMapEntry> entries = 1;
59+
map<string, MountTree> entries = 1;
6760

6861
// The node of the current path. If not set, we'll just create a directory at
6962
// this position.

sandboxed_api/sandbox2/mounts.cc

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <sys/statvfs.h>
2121
#include <unistd.h>
2222

23-
#include <algorithm>
2423
#include <cerrno>
2524
#include <cstddef>
2625
#include <cstdint>
@@ -231,7 +230,7 @@ absl::Status Mounts::Remove(absl::string_view path) {
231230
return absl::NotFoundError(
232231
absl::StrCat("Path does not exist in mounts: ", path));
233232
}
234-
curtree = it->second.mutable_sub_tree();
233+
curtree = &it->second;
235234
}
236235
curtree->clear_node();
237236
curtree->clear_entries();
@@ -281,29 +280,25 @@ absl::Status Mounts::Insert(absl::string_view path,
281280

282281
std::vector<absl::string_view> parts =
283282
absl::StrSplit(absl::StripPrefix(fixed_path, "/"), '/');
283+
std::string final_part(parts.back());
284+
parts.pop_back();
284285

285286
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);
300291
if (curtree->has_node() && curtree->node().has_file_node()) {
301292
return absl::FailedPreconditionError(
302293
absl::StrCat("Cannot insert ", path,
303294
" since a file is mounted as a parent directory"));
304295
}
305296
}
306297

298+
curtree = &(curtree->mutable_entries()
299+
->insert({final_part, MountTree()})
300+
.first->second);
301+
307302
if (curtree->has_node()) {
308303
if (internal::IsEquivalentNode(curtree->node(), new_node)) {
309304
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 {
379374
}
380375
return absl::NotFoundError("Path could not be resolved in the mounts");
381376
}
382-
curtree = &it->second.sub_tree();
377+
curtree = &it->second;
383378
tail = parts.second;
384379
}
385380
switch (curtree->node().node_case()) {
@@ -692,21 +687,6 @@ void MountWithDefaults(const std::string& source, const std::string& target,
692687
}
693688
}
694689

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-
710690
// Traverses the MountTree to create all required files and perform the mounts.
711691
void CreateMounts(const MountTree& tree, const std::string& path,
712692
bool create_backing_files) {
@@ -768,10 +748,9 @@ void CreateMounts(const MountTree& tree, const std::string& path,
768748
}
769749

770750
// 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);
775754
}
776755
}
777756

@@ -802,10 +781,9 @@ void RecursivelyListMountsImpl(const MountTree& tree,
802781
absl::StrCat("tmpfs: ", node.tmpfs_node().tmpfs_options()));
803782
}
804783

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),
809787
outside_entries, inside_entries);
810788
}
811789
}

sandboxed_api/sandbox2/mounts.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#define SANDBOXED_API_SANDBOX2_MOUNTTREE_H_
1717

1818
#include <cstddef>
19-
#include <cstdint>
2019
#include <string>
2120
#include <utility>
2221
#include <vector>
@@ -34,7 +33,6 @@ bool IsSameFile(const std::string& path1, const std::string& path2);
3433
bool IsWritable(const MountTree::Node& node);
3534
bool HasSameTarget(const MountTree::Node& n1, const MountTree::Node& n2);
3635
bool IsEquivalentNode(const MountTree::Node& n1, const MountTree::Node& n2);
37-
3836
} // namespace internal
3937

4038
class Mounts {
@@ -99,10 +97,11 @@ class Mounts {
9997
absl::StatusOr<std::string> ResolvePath(absl::string_view path) const;
10098

10199
private:
100+
friend class MountTreeTest;
101+
102102
absl::Status Insert(absl::string_view path, const MountTree::Node& node);
103103

104104
MountTree mount_tree_;
105-
int64_t mount_index_ = 0; // Used to keep track of the mount insertion order
106105
};
107106

108107
} // namespace sandbox2

sandboxed_api/sandbox2/mounts_test.cc

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "absl/status/status.h"
2626
#include "absl/strings/match.h"
2727
#include "absl/strings/str_cat.h"
28-
#include "sandboxed_api/sandbox2/mount_tree.pb.h"
2928
#include "sandboxed_api/testing.h"
3029
#include "sandboxed_api/util/path.h"
3130
#include "sandboxed_api/util/status_matchers.h"
@@ -41,7 +40,6 @@ using ::sapi::GetTestSourcePath;
4140
using ::sapi::GetTestTempPath;
4241
using ::sapi::IsOk;
4342
using ::sapi::StatusIs;
44-
using ::testing::ElementsAre;
4543
using ::testing::Eq;
4644
using ::testing::StrEq;
4745
using ::testing::UnorderedElementsAreArray;
@@ -427,24 +425,6 @@ TEST(MountTreeTest, TestNodeEquivalence) {
427425
EXPECT_FALSE(internal::IsEquivalentNode(nodes[6], nodes[0]));
428426
}
429427

430-
TEST(MountTreeTest, Order) {
431-
Mounts mounts;
432-
ASSERT_THAT(mounts.AddDirectoryAt("/A", "/a"), IsOk());
433-
ASSERT_THAT(mounts.AddDirectoryAt("/B", "/d/b"), IsOk());
434-
ASSERT_THAT(mounts.AddDirectoryAt("/C/D/E", "/d/c/e/f/h"), IsOk());
435-
ASSERT_THAT(mounts.AddFileAt("/J/G/H", "/d/c/e/f/h/j"), IsOk());
436-
ASSERT_THAT(mounts.AddDirectoryAt("/K/L/M", "/d/c/e/f/h/k"), IsOk());
437-
438-
std::vector<std::string> inside;
439-
std::vector<std::string> outside;
440-
mounts.RecursivelyListMounts(&inside, &outside);
441-
442-
EXPECT_THAT(inside,
443-
ElementsAre("/A/", "/B/", "/C/D/E/", "/J/G/H", "/K/L/M/"));
444-
EXPECT_THAT(outside, ElementsAre("R /a/", "R /d/b/", "R /d/c/e/f/h/",
445-
"R /d/c/e/f/h/j", "R /d/c/e/f/h/k/"));
446-
}
447-
448428
TEST(MountsResolvePathTest, Files) {
449429
Mounts mounts;
450430
ASSERT_THAT(mounts.AddFileAt("/A", "/a"), IsOk());

0 commit comments

Comments
 (0)