Skip to content

Commit 5b0faf9

Browse files
committed
Replace FetcherConfig::AddFilesToConfig with BuildFetcherConfigMember
This function doesn't need to use any private members of the class, so it can be an external function. The difference between `AddFilesToConfig` and other member functions became more obvious when introducing mutex locking. https://embeddedartistry.com/fieldatlas/how-non-member-functions-improve-encapsulation/ Bug: b/461609135
1 parent 8b662ce commit 5b0faf9

File tree

4 files changed

+52
-49
lines changed

4 files changed

+52
-49
lines changed

base/cvd/cuttlefish/host/commands/cvd/fetch/fetch_context.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,14 @@ Result<void> FetchBuildContext::DesparseFiles(std::vector<std::string> files) {
209209

210210
Result<void> FetchBuildContext::AddFileToConfig(std::string file) {
211211
auto [build_id, build_target] = GetBuildIdAndTarget(build_);
212-
CF_EXPECT(fetch_context_.fetcher_config_.AddFilesToConfig(
213-
file_source_, build_id, build_target, {file},
214-
fetch_context_.target_directories_.root, true));
212+
213+
CvdFile config_member = CF_EXPECT(
214+
BuildFetcherConfigMember(file_source_, build_id, build_target, file,
215+
fetch_context_.target_directories_.root));
216+
217+
CF_EXPECT(fetch_context_.fetcher_config_.add_cvd_file(
218+
std::move(config_member), /* override_entry = */ true));
219+
215220
return {};
216221
}
217222

base/cvd/cuttlefish/host/commands/cvd/fetch/fetch_cvd.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,11 @@ Result<std::string> SaveConfig(FetcherConfig& config,
176176
// itself was built at.
177177
// https://android.googlesource.com/platform/build/+/979c9f3/Changes.md#build_number
178178
const std::string fetcher_path = target_directory + "/fetcher_config.json";
179-
CF_EXPECT(config.AddFilesToConfig(FileSource::GENERATED, "", "",
180-
{fetcher_path}, target_directory));
179+
180+
CvdFile config_member = CF_EXPECT(BuildFetcherConfigMember(
181+
FileSource::GENERATED, "", "", fetcher_path, target_directory));
182+
CF_EXPECT(config.add_cvd_file(std::move(config_member)));
183+
181184
config.SaveToFile(fetcher_path);
182185

183186
for (const auto& file : config.get_cvd_files()) {
@@ -365,8 +368,12 @@ Result<void> FetchChromeOsTarget(
365368
auto archive_files = CF_EXPECT(ExtractArchiveContents(
366369
archive_path, target_directories.chrome_os, keep_downloaded_archives));
367370
trace.CompletePhase("Extract");
368-
CF_EXPECT(config.AddFilesToConfig(FileSource::CHROME_OS_BUILD, "", "",
369-
archive_files, target_directories.root));
371+
for (const std::string& archive_file : archive_files) {
372+
CvdFile config_member = CF_EXPECT(
373+
BuildFetcherConfigMember(FileSource::CHROME_OS_BUILD, "", "",
374+
archive_file, target_directories.root));
375+
CF_EXPECT(config.add_cvd_file(std::move(config_member)));
376+
}
370377
return {};
371378
}
372379

base/cvd/cuttlefish/host/libs/config/fetcher_config.cpp

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ Json::Value CvdFileToJson(const CvdFile& cvd_file) {
152152
return json;
153153
}
154154

155+
Result<std::string> NormalizePath(std::string path) {
156+
CF_EXPECT(!absl::StrContains(path, ".."));
157+
while (absl::StrContains(path, "//")) {
158+
absl::StrReplaceAll({{"//", "/"}}, &path);
159+
}
160+
return path;
161+
}
162+
155163
} // namespace
156164

157165
bool FetcherConfig::add_cvd_file(const CvdFile& file, bool override_entry) {
@@ -200,42 +208,6 @@ std::string FetcherConfig::FindCvdFileWithSuffix(
200208
return "";
201209
}
202210

203-
static Result<std::string> NormalizePath(std::string path) {
204-
CF_EXPECT(!absl::StrContains(path, ".."));
205-
while (absl::StrContains(path, "//")) {
206-
absl::StrReplaceAll({{"//", "/"}}, &path);
207-
}
208-
return path;
209-
}
210-
211-
Result<void> FetcherConfig::AddFilesToConfig(
212-
FileSource purpose, const std::string& build_id,
213-
const std::string& build_target, const std::vector<std::string>& paths,
214-
const std::string& directory_prefix, bool override_entry) {
215-
// This neither acesses dictionary_ directly or locks *mutex, but it calls
216-
// `add_cvd_file` which does.
217-
218-
for (const std::string& path : paths) {
219-
std::string_view local_path(path);
220-
if (!android::base::ConsumePrefix(&local_path, directory_prefix)) {
221-
LOG(ERROR) << "Failed to remove prefix " << directory_prefix << " from "
222-
<< local_path;
223-
return {};
224-
}
225-
while (android::base::StartsWith(local_path, "/")) {
226-
android::base::ConsumePrefix(&local_path, "/");
227-
}
228-
std::string normalized = CF_EXPECT(NormalizePath(std::string(local_path)));
229-
// TODO(schuffelen): Do better for local builds here.
230-
CvdFile file(purpose, build_id, build_target, normalized);
231-
CF_EXPECT(add_cvd_file(file, override_entry),
232-
"Duplicate file \""
233-
<< file << "\", Existing file: \"" << get_cvd_files()[path]
234-
<< "\". Failed to add path \"" << path << "\"");
235-
}
236-
return {};
237-
}
238-
239211
Result<void> FetcherConfig::RemoveFileFromConfig(const std::string& path) {
240212
std::scoped_lock lock(*mutex_);
241213

@@ -249,6 +221,25 @@ Result<void> FetcherConfig::RemoveFileFromConfig(const std::string& path) {
249221
return {};
250222
}
251223

224+
Result<CvdFile> BuildFetcherConfigMember(FileSource purpose,
225+
const std::string& build_id,
226+
const std::string& build_target,
227+
const std::string& path,
228+
const std::string& directory_prefix) {
229+
std::string_view local_path(path);
230+
if (!android::base::ConsumePrefix(&local_path, directory_prefix)) {
231+
LOG(ERROR) << "Failed to remove prefix " << directory_prefix << " from "
232+
<< local_path;
233+
return {};
234+
}
235+
while (android::base::StartsWith(local_path, "/")) {
236+
android::base::ConsumePrefix(&local_path, "/");
237+
}
238+
std::string normalized = CF_EXPECT(NormalizePath(std::string(local_path)));
239+
// TODO(schuffelen): Do better for local builds here.
240+
return CvdFile(purpose, build_id, build_target, normalized);
241+
}
242+
252243
FetcherConfigs FetcherConfigs::Create(std::vector<FetcherConfig> configs) {
253244
if (configs.empty()) {
254245
configs.emplace_back();

base/cvd/cuttlefish/host/libs/config/fetcher_config.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,19 @@ class FetcherConfig {
7575

7676
std::string FindCvdFileWithSuffix(const std::string& suffix) const;
7777

78-
Result<void> AddFilesToConfig(FileSource purpose, const std::string& build_id,
79-
const std::string& build_target,
80-
const std::vector<std::string>& paths,
81-
const std::string& directory_prefix,
82-
bool override_entry = false);
83-
8478
Result<void> RemoveFileFromConfig(const std::string& path);
8579

8680
private:
8781
Json::Value dictionary_;
8882
std::unique_ptr<std::mutex> mutex_;
8983
};
9084

85+
Result<CvdFile> BuildFetcherConfigMember(FileSource purpose,
86+
const std::string& build_id,
87+
const std::string& build_target,
88+
const std::string& path,
89+
const std::string& directory_prefix);
90+
9191
class FetcherConfigs {
9292
public:
9393
static FetcherConfigs Create(std::vector<FetcherConfig> configs);

0 commit comments

Comments
 (0)