Skip to content

Commit

Permalink
Mounts: Simplify insertion logic
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 685626737
Change-Id: I4de44d777eb787c46432e2b8c7a73e6e33476b90
  • Loading branch information
cblichmann authored and copybara-github committed Oct 14, 2024
1 parent e092278 commit ccf962c
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions sandboxed_api/sandbox2/mounts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -280,25 +280,21 @@ absl::Status Mounts::Insert(absl::string_view path,

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

MountTree* curtree = &mount_tree_;
for (absl::string_view part : parts) {
curtree = &(curtree->mutable_entries()
->insert({std::string(part), MountTree()})
.first->second);
for (int i = 0; true; ++i) {
auto it = curtree->mutable_entries()->emplace(parts[i], MountTree()).first;
curtree = &it->second;
if (i == parts.size() - 1) { // Final part
break;
}
if (curtree->has_node() && curtree->node().has_file_node()) {
return absl::FailedPreconditionError(
absl::StrCat("Cannot insert ", path,
" since a file is mounted as a parent directory"));
}
}

curtree = &(curtree->mutable_entries()
->insert({final_part, MountTree()})
.first->second);

if (curtree->has_node()) {
if (internal::IsEquivalentNode(curtree->node(), new_node)) {
SAPI_RAW_LOG(INFO, "Inserting %s with the same value twice",
Expand Down Expand Up @@ -586,8 +582,7 @@ uint64_t GetMountFlagsFor(const std::string& path) {
}

std::string MountFlagsToString(uint64_t flags) {
#define SAPI_MAP(x) \
{ x, #x }
#define SAPI_MAP(x) {x, #x}
static constexpr std::pair<uint64_t, absl::string_view> kMap[] = {
SAPI_MAP(MS_RDONLY), SAPI_MAP(MS_NOSUID),
SAPI_MAP(MS_NODEV), SAPI_MAP(MS_NOEXEC),
Expand Down

0 comments on commit ccf962c

Please sign in to comment.