Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions base/cvd/cuttlefish/common/libs/utils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ cf_cc_library(
deps = [
":environment",
"//cuttlefish/common/libs/fs",
"//cuttlefish/common/libs/utils:contains",
"//cuttlefish/common/libs/utils:in_sandbox",
"//cuttlefish/common/libs/utils:users",
"//cuttlefish/posix:rename",
Expand All @@ -130,23 +129,11 @@ cf_cc_library(
],
)

cf_cc_library(
name = "files_test_helper",
testonly = 1,
srcs = ["files_test_helper.cpp"],
hdrs = ["files_test_helper.h"],
deps = [
"//cuttlefish/common/libs/utils:files",
"@googletest//:gtest",
],
)

cf_cc_test(
name = "files_test",
srcs = ["files_test.cpp"],
deps = [
"//cuttlefish/common/libs/utils:files",
"//cuttlefish/common/libs/utils:files_test_helper",
"//cuttlefish/result",
"//cuttlefish/result:result_matchers",
"//libbase",
Expand Down
84 changes: 0 additions & 84 deletions base/cvd/cuttlefish/common/libs/utils/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#include <ios>
#include <iosfwd>
#include <memory>
#include <numeric>
#include <string>
#include <string_view>
#include <vector>
Expand All @@ -57,14 +56,12 @@
#include "absl/strings/str_split.h"
#include "absl/log/check.h"
#include "absl/log/log.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "fmt/format.h"

#include "cuttlefish/common/libs/fs/shared_buf.h"
#include "cuttlefish/common/libs/fs/shared_fd.h"
#include "cuttlefish/common/libs/utils/contains.h"
#include "cuttlefish/common/libs/utils/environment.h"
#include "cuttlefish/common/libs/utils/in_sandbox.h"
#include "cuttlefish/common/libs/utils/users.h"
Expand Down Expand Up @@ -340,10 +337,6 @@ Result<void> RecursivelyRemoveDirectory(const std::string& path) {
return {};
}

namespace {

} // namespace

bool Copy(const std::string& from, const std::string& to) {
SharedFD fd_from = SharedFD::Open(from, O_RDONLY);
SharedFD fd_to = SharedFD::Open(to, O_WRONLY | O_CREAT | O_TRUNC, 0644);
Expand Down Expand Up @@ -638,83 +631,6 @@ Result<void> WalkDirectory(const std::string& dir,
return {};
}

namespace {
Comment thread
jemoreira marked this conversation as resolved.

std::vector<std::string> FoldPath(std::vector<std::string> elements,
std::string token) {
static constexpr std::array kIgnored = {".", "..", ""};
if (token == ".." && !elements.empty()) {
elements.pop_back();
} else if (!Contains(kIgnored, token)) {
elements.emplace_back(token);
}
return elements;
}

Result<std::vector<std::string>> CalculatePrefix(
const InputPathForm& path_info) {
const auto& path = path_info.path_to_convert;
std::string working_dir;
if (path_info.current_working_dir) {
working_dir = *path_info.current_working_dir;
} else {
working_dir = CurrentDirectory();
}
std::vector<std::string> prefix;
if (path == "~" || absl::StartsWith(path, "~/")) {
const auto home_dir =
path_info.home_dir.value_or(CF_EXPECT(SystemWideUserHome()));
prefix = absl::StrSplit(home_dir, '/', absl::SkipEmpty());
} else if (!absl::StartsWith(path, "/")) {
prefix = absl::StrSplit(working_dir, '/', absl::SkipEmpty());
}
return prefix;
}

} // namespace

Result<std::string> EmulateAbsolutePath(const InputPathForm& path_info) {
const auto& path = path_info.path_to_convert;
std::string working_dir;
if (path_info.current_working_dir) {
working_dir = *path_info.current_working_dir;
} else {
working_dir = CurrentDirectory();
}
CF_EXPECT(absl::StartsWith(working_dir, "/"),
"Current working directory should be given in an absolute path.");

if (path.empty()) {
LOG(ERROR) << "The requested path to convert an absolute path is empty.";
return "";
}

auto prefix = CF_EXPECT(CalculatePrefix(path_info));
std::vector<std::string> components;
components.insert(components.end(), prefix.begin(), prefix.end());
std::vector<std::string_view> tokens = absl::StrSplit(path, '/', absl::SkipEmpty());
// remove first ~
if (!tokens.empty() && tokens[0] == "~") {
tokens.erase(tokens.begin());
}
components.insert(components.end(), tokens.begin(), tokens.end());

std::string combined = absl::StrJoin(components, "/");
CF_EXPECTF(!Contains(components, "~"),
"~ is not allowed in the middle of the path: {}", combined);

auto processed_tokens = std::accumulate(components.begin(), components.end(),
std::vector<std::string>{}, FoldPath);

const auto processed_path = "/" + absl::StrJoin(processed_tokens, "/");

if (path_info.follow_symlink && FileExists(processed_path)) {
return CF_EXPECTF(RealPath(processed_path),
"Failed to effectively conduct readpath -f {}", processed_path);
}
return processed_path;
}

std::vector<std::string> Path(const std::string& env_name) {
// TODO: Assumes a SUS system. Elsewhere we may need to change the delimiter.
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html
Expand Down
28 changes: 0 additions & 28 deletions base/cvd/cuttlefish/common/libs/utils/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include <chrono>
#include <functional>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
Expand Down Expand Up @@ -106,33 +105,6 @@ using WalkDirectoryCallback = std::function<Result<void>(const std::string&)>;
Result<void> WalkDirectory(const std::string& dir,
const WalkDirectoryCallback& callback);

// parameter to EmulateAbsolutePath
struct InputPathForm {
/** If nullopt, uses the process' current working dir
* But if there is no preceding .. or ., this field is not used.
*/
std::optional<std::string> current_working_dir;
/** If nullopt, use SystemWideUserHome()
* But, if there's no preceding ~, this field is not used.
*/
std::optional<std::string> home_dir;
std::string path_to_convert;
bool follow_symlink;
};

/**
* Returns emulated absolute path with a different process'/thread's
* context.
*
* This is useful when daemon(0, 0)-started server process wants to
* figure out a relative path that came from its client.
*
* The call mostly succeeds. It fails only if:
* home_dir isn't given so supposed to relies on the local SystemWideUserHome()
* but SystemWideUserHome() call fails.
*/
Result<std::string> EmulateAbsolutePath(const InputPathForm& path_info);

std::vector<std::string> Path(const std::string& env_name = "PATH");

Result<std::string> Search(const std::vector<std::string>& path,
Expand Down
84 changes: 0 additions & 84 deletions base/cvd/cuttlefish/common/libs/utils/files_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "absl/cleanup/cleanup.h"
#include "absl/strings/str_cat.h"

#include "cuttlefish/common/libs/utils/files_test_helper.h"
#include "cuttlefish/result/result.h"
#include "cuttlefish/result/result_matchers.h"

Expand Down Expand Up @@ -99,89 +98,6 @@ TEST_F(FilesTests, MoveDirectoryContents) {
EXPECT_THAT(FileExists(dst_dir_ + "/sub_dir/file2.txt"), IsTrue());
}

TEST_P(EmulateAbsolutePathBase, NoHomeNoPwd) {
const bool follow_symlink = false;
auto emulated_absolute_path =
EmulateAbsolutePath({.current_working_dir = std::nullopt,
.home_dir = std::nullopt,
.path_to_convert = input_path_,
.follow_symlink = follow_symlink});

ASSERT_TRUE(emulated_absolute_path.ok())
<< emulated_absolute_path.error().Trace();
ASSERT_EQ(*emulated_absolute_path, expected_path_);
}

INSTANTIATE_TEST_SUITE_P(
CommonUtilsTest, EmulateAbsolutePathBase,
testing::Values(InputOutput{.path_to_convert_ = "/", .expected_ = "/"},
InputOutput{.path_to_convert_ = "", .expected_ = ""},
InputOutput{.path_to_convert_ = "/a/b/c/",
.expected_ = "/a/b/c"},
InputOutput{.path_to_convert_ = "/a", .expected_ = "/a"}));

TEST_P(EmulateAbsolutePathWithPwd, NoHomeYesPwd) {
const bool follow_symlink = false;
auto emulated_absolute_path =
EmulateAbsolutePath({.current_working_dir = current_dir_,
.home_dir = "/a/b/c",
.path_to_convert = input_path_,
.follow_symlink = follow_symlink});

ASSERT_TRUE(emulated_absolute_path.ok())
<< emulated_absolute_path.error().Trace();
ASSERT_EQ(*emulated_absolute_path, expected_path_);
}

INSTANTIATE_TEST_SUITE_P(
CommonUtilsTest, EmulateAbsolutePathWithPwd,
testing::Values(InputOutput{.path_to_convert_ = "",
.working_dir_ = "/x/y/z",
.expected_ = ""},
InputOutput{.path_to_convert_ = "a",
.working_dir_ = "/x/y/z",
.expected_ = "/x/y/z/a"},
InputOutput{.path_to_convert_ = ".",
.working_dir_ = "/x/y/z",
.expected_ = "/x/y/z"},
InputOutput{.path_to_convert_ = "..",
.working_dir_ = "/x/y/z",
.expected_ = "/x/y"},
InputOutput{.path_to_convert_ = "./k/../../t/./q",
.working_dir_ = "/x/y/z",
.expected_ = "/x/y/t/q"}));

TEST_P(EmulateAbsolutePathWithHome, YesHomeNoPwd) {
const bool follow_symlink = false;
auto emulated_absolute_path =
EmulateAbsolutePath({.current_working_dir = std::nullopt,
.home_dir = home_dir_,
.path_to_convert = input_path_,
.follow_symlink = follow_symlink});

ASSERT_TRUE(emulated_absolute_path.ok())
<< emulated_absolute_path.error().Trace();
ASSERT_EQ(*emulated_absolute_path, expected_path_);
}

INSTANTIATE_TEST_SUITE_P(
CommonUtilsTest, EmulateAbsolutePathWithHome,
testing::Values(InputOutput{.path_to_convert_ = "~",
.home_dir_ = "/x/y/z",
.expected_ = "/x/y/z"},
InputOutput{.path_to_convert_ = "~/a",
.home_dir_ = "/x/y/z",
.expected_ = "/x/y/z/a"},
InputOutput{.path_to_convert_ = "~/.",
.home_dir_ = "/x/y/z",
.expected_ = "/x/y/z"},
InputOutput{.path_to_convert_ = "~/..",
.home_dir_ = "/x/y/z",
.expected_ = "/x/y"},
InputOutput{.path_to_convert_ = "~/k/../../t/./q",
.home_dir_ = "/x/y/z",
.expected_ = "/x/y/t/q"}));

TEST(FilesTest, PathWithCustomEnv) {
const std::string env_name = "TEST_PATH";
const std::string dir1 = "/foo/bar";
Expand Down
37 changes: 0 additions & 37 deletions base/cvd/cuttlefish/common/libs/utils/files_test_helper.cpp

This file was deleted.

60 changes: 0 additions & 60 deletions base/cvd/cuttlefish/common/libs/utils/files_test_helper.h

This file was deleted.

Loading
Loading