Skip to content

Commit fbe4349

Browse files
committed
Stop using EmulateAbsolutePath
EmulateAbsolutePath was needed for the cvd server as the client may have been executing in a different directory and with a different $HOME. The function handled "~" at the begining of the path, which the cvd command shouldn't do as that's the shell's responsibility. Without the special handling for "~" and with all callers just using the current working directory, calls to the function were doing exactly the same as `AbsolutePath` or `RealPath` (with `follow_symlinks` as true or false respectively).
1 parent 83a4f26 commit fbe4349

10 files changed

Lines changed: 4 additions & 336 deletions

File tree

base/cvd/cuttlefish/common/libs/utils/BUILD.bazel

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ cf_cc_library(
115115
deps = [
116116
":environment",
117117
"//cuttlefish/common/libs/fs",
118-
"//cuttlefish/common/libs/utils:contains",
119118
"//cuttlefish/common/libs/utils:in_sandbox",
120119
"//cuttlefish/common/libs/utils:users",
121120
"//cuttlefish/posix:rename",
@@ -130,23 +129,11 @@ cf_cc_library(
130129
],
131130
)
132131

133-
cf_cc_library(
134-
name = "files_test_helper",
135-
testonly = 1,
136-
srcs = ["files_test_helper.cpp"],
137-
hdrs = ["files_test_helper.h"],
138-
deps = [
139-
"//cuttlefish/common/libs/utils:files",
140-
"@googletest//:gtest",
141-
],
142-
)
143-
144132
cf_cc_test(
145133
name = "files_test",
146134
srcs = ["files_test.cpp"],
147135
deps = [
148136
"//cuttlefish/common/libs/utils:files",
149-
"//cuttlefish/common/libs/utils:files_test_helper",
150137
"//cuttlefish/result",
151138
"//cuttlefish/result:result_matchers",
152139
"//libbase",

base/cvd/cuttlefish/common/libs/utils/files.cpp

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
#include <ios>
4747
#include <iosfwd>
4848
#include <memory>
49-
#include <numeric>
5049
#include <string>
5150
#include <string_view>
5251
#include <vector>
@@ -57,14 +56,12 @@
5756
#include "absl/strings/str_split.h"
5857
#include "absl/log/check.h"
5958
#include "absl/log/log.h"
60-
#include "absl/strings/match.h"
6159
#include "absl/strings/str_cat.h"
6260
#include "absl/strings/str_join.h"
6361
#include "fmt/format.h"
6462

6563
#include "cuttlefish/common/libs/fs/shared_buf.h"
6664
#include "cuttlefish/common/libs/fs/shared_fd.h"
67-
#include "cuttlefish/common/libs/utils/contains.h"
6865
#include "cuttlefish/common/libs/utils/environment.h"
6966
#include "cuttlefish/common/libs/utils/in_sandbox.h"
7067
#include "cuttlefish/common/libs/utils/users.h"
@@ -340,10 +337,6 @@ Result<void> RecursivelyRemoveDirectory(const std::string& path) {
340337
return {};
341338
}
342339

343-
namespace {
344-
345-
} // namespace
346-
347340
bool Copy(const std::string& from, const std::string& to) {
348341
SharedFD fd_from = SharedFD::Open(from, O_RDONLY);
349342
SharedFD fd_to = SharedFD::Open(to, O_WRONLY | O_CREAT | O_TRUNC, 0644);
@@ -638,83 +631,6 @@ Result<void> WalkDirectory(const std::string& dir,
638631
return {};
639632
}
640633

641-
namespace {
642-
643-
std::vector<std::string> FoldPath(std::vector<std::string> elements,
644-
std::string token) {
645-
static constexpr std::array kIgnored = {".", "..", ""};
646-
if (token == ".." && !elements.empty()) {
647-
elements.pop_back();
648-
} else if (!Contains(kIgnored, token)) {
649-
elements.emplace_back(token);
650-
}
651-
return elements;
652-
}
653-
654-
Result<std::vector<std::string>> CalculatePrefix(
655-
const InputPathForm& path_info) {
656-
const auto& path = path_info.path_to_convert;
657-
std::string working_dir;
658-
if (path_info.current_working_dir) {
659-
working_dir = *path_info.current_working_dir;
660-
} else {
661-
working_dir = CurrentDirectory();
662-
}
663-
std::vector<std::string> prefix;
664-
if (path == "~" || absl::StartsWith(path, "~/")) {
665-
const auto home_dir =
666-
path_info.home_dir.value_or(CF_EXPECT(SystemWideUserHome()));
667-
prefix = absl::StrSplit(home_dir, '/', absl::SkipEmpty());
668-
} else if (!absl::StartsWith(path, "/")) {
669-
prefix = absl::StrSplit(working_dir, '/', absl::SkipEmpty());
670-
}
671-
return prefix;
672-
}
673-
674-
} // namespace
675-
676-
Result<std::string> EmulateAbsolutePath(const InputPathForm& path_info) {
677-
const auto& path = path_info.path_to_convert;
678-
std::string working_dir;
679-
if (path_info.current_working_dir) {
680-
working_dir = *path_info.current_working_dir;
681-
} else {
682-
working_dir = CurrentDirectory();
683-
}
684-
CF_EXPECT(absl::StartsWith(working_dir, "/"),
685-
"Current working directory should be given in an absolute path.");
686-
687-
if (path.empty()) {
688-
LOG(ERROR) << "The requested path to convert an absolute path is empty.";
689-
return "";
690-
}
691-
692-
auto prefix = CF_EXPECT(CalculatePrefix(path_info));
693-
std::vector<std::string> components;
694-
components.insert(components.end(), prefix.begin(), prefix.end());
695-
std::vector<std::string_view> tokens = absl::StrSplit(path, '/', absl::SkipEmpty());
696-
// remove first ~
697-
if (!tokens.empty() && tokens[0] == "~") {
698-
tokens.erase(tokens.begin());
699-
}
700-
components.insert(components.end(), tokens.begin(), tokens.end());
701-
702-
std::string combined = absl::StrJoin(components, "/");
703-
CF_EXPECTF(!Contains(components, "~"),
704-
"~ is not allowed in the middle of the path: {}", combined);
705-
706-
auto processed_tokens = std::accumulate(components.begin(), components.end(),
707-
std::vector<std::string>{}, FoldPath);
708-
709-
const auto processed_path = "/" + absl::StrJoin(processed_tokens, "/");
710-
711-
if (path_info.follow_symlink && FileExists(processed_path)) {
712-
return CF_EXPECTF(RealPath(processed_path),
713-
"Failed to effectively conduct readpath -f {}", processed_path);
714-
}
715-
return processed_path;
716-
}
717-
718634
std::vector<std::string> Path(const std::string& env_name) {
719635
// TODO: Assumes a SUS system. Elsewhere we may need to change the delimiter.
720636
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html

base/cvd/cuttlefish/common/libs/utils/files.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#include <chrono>
2222
#include <functional>
23-
#include <optional>
2423
#include <string>
2524
#include <string_view>
2625
#include <vector>
@@ -106,33 +105,6 @@ using WalkDirectoryCallback = std::function<Result<void>(const std::string&)>;
106105
Result<void> WalkDirectory(const std::string& dir,
107106
const WalkDirectoryCallback& callback);
108107

109-
// parameter to EmulateAbsolutePath
110-
struct InputPathForm {
111-
/** If nullopt, uses the process' current working dir
112-
* But if there is no preceding .. or ., this field is not used.
113-
*/
114-
std::optional<std::string> current_working_dir;
115-
/** If nullopt, use SystemWideUserHome()
116-
* But, if there's no preceding ~, this field is not used.
117-
*/
118-
std::optional<std::string> home_dir;
119-
std::string path_to_convert;
120-
bool follow_symlink;
121-
};
122-
123-
/**
124-
* Returns emulated absolute path with a different process'/thread's
125-
* context.
126-
*
127-
* This is useful when daemon(0, 0)-started server process wants to
128-
* figure out a relative path that came from its client.
129-
*
130-
* The call mostly succeeds. It fails only if:
131-
* home_dir isn't given so supposed to relies on the local SystemWideUserHome()
132-
* but SystemWideUserHome() call fails.
133-
*/
134-
Result<std::string> EmulateAbsolutePath(const InputPathForm& path_info);
135-
136108
std::vector<std::string> Path(const std::string& env_name = "PATH");
137109

138110
Result<std::string> Search(const std::vector<std::string>& path,

base/cvd/cuttlefish/common/libs/utils/files_test.cpp

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "absl/cleanup/cleanup.h"
2525
#include "absl/strings/str_cat.h"
2626

27-
#include "cuttlefish/common/libs/utils/files_test_helper.h"
2827
#include "cuttlefish/result/result.h"
2928
#include "cuttlefish/result/result_matchers.h"
3029

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

102-
TEST_P(EmulateAbsolutePathBase, NoHomeNoPwd) {
103-
const bool follow_symlink = false;
104-
auto emulated_absolute_path =
105-
EmulateAbsolutePath({.current_working_dir = std::nullopt,
106-
.home_dir = std::nullopt,
107-
.path_to_convert = input_path_,
108-
.follow_symlink = follow_symlink});
109-
110-
ASSERT_TRUE(emulated_absolute_path.ok())
111-
<< emulated_absolute_path.error().Trace();
112-
ASSERT_EQ(*emulated_absolute_path, expected_path_);
113-
}
114-
115-
INSTANTIATE_TEST_SUITE_P(
116-
CommonUtilsTest, EmulateAbsolutePathBase,
117-
testing::Values(InputOutput{.path_to_convert_ = "/", .expected_ = "/"},
118-
InputOutput{.path_to_convert_ = "", .expected_ = ""},
119-
InputOutput{.path_to_convert_ = "/a/b/c/",
120-
.expected_ = "/a/b/c"},
121-
InputOutput{.path_to_convert_ = "/a", .expected_ = "/a"}));
122-
123-
TEST_P(EmulateAbsolutePathWithPwd, NoHomeYesPwd) {
124-
const bool follow_symlink = false;
125-
auto emulated_absolute_path =
126-
EmulateAbsolutePath({.current_working_dir = current_dir_,
127-
.home_dir = "/a/b/c",
128-
.path_to_convert = input_path_,
129-
.follow_symlink = follow_symlink});
130-
131-
ASSERT_TRUE(emulated_absolute_path.ok())
132-
<< emulated_absolute_path.error().Trace();
133-
ASSERT_EQ(*emulated_absolute_path, expected_path_);
134-
}
135-
136-
INSTANTIATE_TEST_SUITE_P(
137-
CommonUtilsTest, EmulateAbsolutePathWithPwd,
138-
testing::Values(InputOutput{.path_to_convert_ = "",
139-
.working_dir_ = "/x/y/z",
140-
.expected_ = ""},
141-
InputOutput{.path_to_convert_ = "a",
142-
.working_dir_ = "/x/y/z",
143-
.expected_ = "/x/y/z/a"},
144-
InputOutput{.path_to_convert_ = ".",
145-
.working_dir_ = "/x/y/z",
146-
.expected_ = "/x/y/z"},
147-
InputOutput{.path_to_convert_ = "..",
148-
.working_dir_ = "/x/y/z",
149-
.expected_ = "/x/y"},
150-
InputOutput{.path_to_convert_ = "./k/../../t/./q",
151-
.working_dir_ = "/x/y/z",
152-
.expected_ = "/x/y/t/q"}));
153-
154-
TEST_P(EmulateAbsolutePathWithHome, YesHomeNoPwd) {
155-
const bool follow_symlink = false;
156-
auto emulated_absolute_path =
157-
EmulateAbsolutePath({.current_working_dir = std::nullopt,
158-
.home_dir = home_dir_,
159-
.path_to_convert = input_path_,
160-
.follow_symlink = follow_symlink});
161-
162-
ASSERT_TRUE(emulated_absolute_path.ok())
163-
<< emulated_absolute_path.error().Trace();
164-
ASSERT_EQ(*emulated_absolute_path, expected_path_);
165-
}
166-
167-
INSTANTIATE_TEST_SUITE_P(
168-
CommonUtilsTest, EmulateAbsolutePathWithHome,
169-
testing::Values(InputOutput{.path_to_convert_ = "~",
170-
.home_dir_ = "/x/y/z",
171-
.expected_ = "/x/y/z"},
172-
InputOutput{.path_to_convert_ = "~/a",
173-
.home_dir_ = "/x/y/z",
174-
.expected_ = "/x/y/z/a"},
175-
InputOutput{.path_to_convert_ = "~/.",
176-
.home_dir_ = "/x/y/z",
177-
.expected_ = "/x/y/z"},
178-
InputOutput{.path_to_convert_ = "~/..",
179-
.home_dir_ = "/x/y/z",
180-
.expected_ = "/x/y"},
181-
InputOutput{.path_to_convert_ = "~/k/../../t/./q",
182-
.home_dir_ = "/x/y/z",
183-
.expected_ = "/x/y/t/q"}));
184-
185101
TEST(FilesTest, PathWithCustomEnv) {
186102
const std::string env_name = "TEST_PATH";
187103
const std::string dir1 = "/foo/bar";

base/cvd/cuttlefish/common/libs/utils/files_test_helper.cpp

Lines changed: 0 additions & 37 deletions
This file was deleted.

base/cvd/cuttlefish/common/libs/utils/files_test_helper.h

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)