Skip to content

Commit 5c6ae7b

Browse files
adelva1984cjreynol
authored andcommitted
[skip ci] Merge "Handle errors from WalkDirectory" into main
GitOrigin-RevId: 4a9b51c
1 parent 2bfc63f commit 5c6ae7b

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

staging/common/libs/utils/files.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -577,15 +577,19 @@ std::string FindImage(const std::string& search_path,
577577
return "";
578578
}
579579

580-
std::string FindFile(const std::string& path, const std::string& target_name) {
580+
Result<std::string> FindFile(const std::string& path,
581+
const std::string& target_name) {
581582
std::string ret;
582-
WalkDirectory(path,
583-
[&ret, &target_name](const std::string& filename) mutable {
584-
if (android::base::Basename(filename) == target_name) {
585-
ret = filename;
586-
}
587-
return true;
588-
});
583+
auto res = WalkDirectory(
584+
path, [&ret, &target_name](const std::string& filename) mutable {
585+
if (android::base::Basename(filename) == target_name) {
586+
ret = filename;
587+
}
588+
return true;
589+
});
590+
if (!res.ok()) {
591+
return "";
592+
}
589593
return ret;
590594
}
591595

@@ -600,7 +604,10 @@ Result<void> WalkDirectory(
600604
file_path.append(filename);
601605
callback(file_path);
602606
if (DirectoryExists(file_path)) {
603-
WalkDirectory(file_path, callback);
607+
auto res = WalkDirectory(file_path, callback);
608+
if (!res.ok()) {
609+
return res;
610+
}
604611
}
605612
}
606613
return {};

staging/common/libs/utils/files.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ FileSizes SparseFileSizes(const std::string& path);
8383

8484
// Find file with name |target_name| under directory |path|, return path to
8585
// found file(if any)
86-
std::string FindFile(const std::string& path, const std::string& target_name);
86+
Result<std::string> FindFile(const std::string& path,
87+
const std::string& target_name);
8788

8889
Result<void> WalkDirectory(
8990
const std::string& dir,

staging/host/commands/assemble_cvd/vendor_dlkm_utils.cc

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ bool WriteLinesToFile(const Container& lines, const char* path) {
7474
}
7575

7676
// Generate a filesystem_config.txt for all files in |fs_root|
77-
bool WriteFsConfig(const char* output_path, const std::string& fs_root,
78-
const std::string& mount_point) {
77+
Result<bool> WriteFsConfig(const char* output_path, const std::string& fs_root,
78+
const std::string& mount_point) {
7979
android::base::unique_fd fd(
8080
open(output_path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644));
8181
if (!fd.ok()) {
@@ -87,8 +87,8 @@ bool WriteFsConfig(const char* output_path, const std::string& fs_root,
8787
PLOG(ERROR) << "Failed to write to " << output_path;
8888
return false;
8989
}
90-
WalkDirectory(fs_root, [&fd, &output_path, &mount_point,
91-
&fs_root](const std::string& file_path) {
90+
auto res = WalkDirectory(fs_root, [&fd, &output_path, &mount_point,
91+
&fs_root](const std::string& file_path) {
9292
const auto filename = file_path.substr(
9393
fs_root.back() == '/' ? fs_root.size() : fs_root.size() + 1);
9494
std::string fs_context = " 0 0 644 capabilities=0x0\n";
@@ -102,6 +102,9 @@ bool WriteFsConfig(const char* output_path, const std::string& fs_root,
102102
}
103103
return true;
104104
});
105+
if (!res.ok()) {
106+
return false;
107+
}
105108
return true;
106109
}
107110

@@ -427,8 +430,13 @@ bool SplitRamdiskModules(const std::string& ramdisk_path,
427430
CHECK(ret.ok()) << ret.error().FormatForEnv();
428431
ret = EnsureDirectoryExists(system_modules_dir);
429432
UnpackRamdisk(ramdisk_path, ramdisk_stage_dir);
430-
const auto module_load_file =
431-
android::base::Trim(FindFile(ramdisk_stage_dir.c_str(), "modules.load"));
433+
auto res = FindFile(ramdisk_stage_dir.c_str(), "modules.load");
434+
if (!res) {
435+
LOG(ERROR) << "Failed to find modules.dep file in input ramdisk "
436+
<< ramdisk_path;
437+
return false;
438+
}
439+
const auto module_load_file = android::base::Trim(res.value());
432440
if (module_load_file.empty()) {
433441
LOG(ERROR) << "Failed to find modules.dep file in input ramdisk "
434442
<< ramdisk_path;

staging/host/commands/assemble_cvd/vendor_dlkm_utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ bool SplitRamdiskModules(const std::string& ramdisk_path,
2626
const std::string& vendor_dlkm_build_dir,
2727
const std::string& system_dlkm_build_dir);
2828

29-
bool WriteFsConfig(const char* output_path, const std::string& fs_root,
30-
const std::string& mount_point);
29+
Result<bool> WriteFsConfig(const char* output_path, const std::string& fs_root,
30+
const std::string& mount_point);
3131

3232
Result<void> RepackSuperWithPartition(const std::string& superimg_path,
3333
const std::string& image_path,

0 commit comments

Comments
 (0)