Skip to content

Commit 881728e

Browse files
committed
Use FileSource to filter searching FetcherConfig for files
This reduces the chance of false positives when searching for the `kernel` and `initramfs.img` files. This is relevant because the presence of the `kernel` file is used as a signal to distinguish whether `cvd fetch` was called with the `--kernel_build` argument. Bug: b/462235783
1 parent 05fdabf commit 881728e

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

base/cvd/cuttlefish/host/commands/assemble_cvd/flags/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ cf_cc_library(
154154
deps = [
155155
"//cuttlefish/host/commands/assemble_cvd:flags_defaults",
156156
"//cuttlefish/host/libs/config:fetcher_config",
157+
"//cuttlefish/host/libs/config:file_source",
157158
"//libbase",
158159
"@gflags",
159160
],
@@ -166,6 +167,7 @@ cf_cc_library(
166167
deps = [
167168
"//cuttlefish/host/commands/assemble_cvd:flags_defaults",
168169
"//cuttlefish/host/libs/config:fetcher_config",
170+
"//cuttlefish/host/libs/config:file_source",
169171
"//libbase",
170172
"@gflags",
171173
],

base/cvd/cuttlefish/host/commands/assemble_cvd/flags/initramfs_path.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ InitramfsPathFlag InitramfsPathFlag::FromGlobalGflags(
3939
std::vector<std::string> initramfs_paths;
4040
if (flag_info.is_default) {
4141
for (size_t i = 0; i < fetcher_configs.Size(); ++i) {
42-
initramfs_paths.emplace_back(
43-
fetcher_configs.ForInstance(i).FindCvdFileWithSuffix(
44-
"initramfs.img"));
42+
const FetcherConfig& fetcher_config = fetcher_configs.ForInstance(i);
43+
initramfs_paths.emplace_back(fetcher_config.FindCvdFileWithSuffix(
44+
FileSource::KERNEL_BUILD, "initramfs.img"));
4545
}
4646
} else {
4747
initramfs_paths = android::base::Split(flag_info.current_value, ",");

base/cvd/cuttlefish/host/commands/assemble_cvd/flags/kernel_path.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "cuttlefish/host/commands/assemble_cvd/flags_defaults.h"
2727
#include "cuttlefish/host/libs/config/fetcher_config.h"
28+
#include "cuttlefish/host/libs/config/file_source.h"
2829

2930
DEFINE_string(kernel_path, CF_DEFAULTS_KERNEL_PATH,
3031
"Path to the kernel. Overrides the one from the boot image");
@@ -40,8 +41,9 @@ KernelPathFlag KernelPathFlag::FromGlobalGflags(
4041

4142
if (flag_info.is_default) {
4243
for (size_t i = 0; i < fetcher_configs.Size(); ++i) {
43-
kernel_paths.emplace_back(
44-
fetcher_configs.ForInstance(i).FindCvdFileWithSuffix("kernel"));
44+
const FetcherConfig& fetcher_config = fetcher_configs.ForInstance(i);
45+
kernel_paths.emplace_back(fetcher_config.FindCvdFileWithSuffix(
46+
FileSource::KERNEL_BUILD, "kernel"));
4547
}
4648
} else {
4749
kernel_paths = android::base::Split(flag_info.current_value, ",");

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,23 @@ std::map<std::string, CvdFile> FetcherConfig::get_cvd_files() const {
212212
}
213213

214214
std::string FetcherConfig::FindCvdFileWithSuffix(
215-
const std::string& suffix) const {
215+
FileSource source, std::string_view suffix) const {
216216
std::scoped_lock lock(*mutex_);
217217

218218
if (!dictionary_.isMember(kCvdFiles)) {
219219
return {};
220220
}
221-
const auto& json_files = dictionary_[kCvdFiles];
221+
const Json::Value& json_files = dictionary_[kCvdFiles];
222222
for (auto it = json_files.begin(); it != json_files.end(); it++) {
223-
const auto& file = it.key().asString();
224-
if (absl::EndsWith(file, suffix)) {
225-
return file;
223+
const std::string& file = it.key().asString();
224+
if (!absl::EndsWith(file, suffix)) {
225+
continue;
226+
}
227+
CvdFile parsed = JsonToCvdFile(file, *it);
228+
if (parsed.source != source) {
229+
continue;
226230
}
231+
return file;
227232
}
228233
LOG(DEBUG) << "Could not find file ending in " << suffix;
229234
return "";

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <mutex>
2121
#include <ostream>
2222
#include <string>
23+
#include <string_view>
2324
#include <vector>
2425

2526
#include "json/value.h"
@@ -79,7 +80,8 @@ class FetcherConfig {
7980
bool add_cvd_file(const CvdFile& file, bool override_entry = false);
8081
std::map<std::string, CvdFile> get_cvd_files() const;
8182

82-
std::string FindCvdFileWithSuffix(const std::string& suffix) const;
83+
std::string FindCvdFileWithSuffix(FileSource source,
84+
std::string_view suffix) const;
8385

8486
Result<void> RemoveFileFromConfig(const std::string& path);
8587

0 commit comments

Comments
 (0)