Skip to content

Commit ccd3d95

Browse files
committed
RemoveFile returns Result and doesn't log
Logging from a library function is not ideal. This change also moves all logging of failure to remove files to the call sites.
1 parent 58840df commit ccd3d95

File tree

12 files changed

+43
-25
lines changed

12 files changed

+43
-25
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,13 @@ Result<std::string> RenameFile(const std::string& current_filepath,
490490
return target_filepath;
491491
}
492492

493-
bool RemoveFile(const std::string& file) {
493+
Result<void> RemoveFile(const std::string& file) {
494494
LOG(DEBUG) << "Removing file " << file;
495-
if (remove(file.c_str()) == 0) {
496-
return true;
495+
if (remove(file.c_str()) != 0) {
496+
return CF_ERRF("Failed to remove file '{}' : {}", file,
497+
StrError(errno));
497498
}
498-
LOG(ERROR) << "Failed to remove file " << file << " : "
499-
<< std::strerror(errno);
500-
return false;
499+
return {};
501500
}
502501

503502
std::string ReadFile(const std::string& file) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Result<bool> IsDirectoryEmpty(const std::string& path);
6666
Result<void> RecursivelyRemoveDirectory(const std::string& path);
6767
bool Copy(const std::string& from, const std::string& to);
6868
off_t FileSize(const std::string& path);
69-
bool RemoveFile(const std::string& file);
69+
Result<void> RemoveFile(const std::string& file);
7070
Result<std::string> RenameFile(const std::string& current_filepath,
7171
const std::string& target_filepath);
7272
std::string ReadFile(const std::string& file);

base/cvd/cuttlefish/host/commands/assemble_cvd/boot_config.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ Result<void> PrepareBootEnvImage(
203203
"Unable to delete the old env image");
204204
LOG(DEBUG) << "Updated bootloader environment image.";
205205
} else {
206-
RemoveFile(tmp_boot_env_image_path);
206+
if (Result<void> res = RemoveFile(tmp_boot_env_image_path); !res.ok()) {
207+
LOG(ERROR) << res.error().FormatForEnv();
208+
}
207209
}
208210

209211
return {};

base/cvd/cuttlefish/host/commands/assemble_cvd/boot_image_utils.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ bool DeleteTmpFileIfNotChanged(const std::string& tmp_file, const std::string& c
103103
LOG(DEBUG) << "Updated " << current_file;
104104
} else {
105105
LOG(DEBUG) << "Didn't update " << current_file;
106-
RemoveFile(tmp_file);
106+
if (Result<void> res = RemoveFile(tmp_file); !res.ok()) {
107+
LOG(ERROR) << res.error().FormatForEnv();
108+
}
107109
}
108110

109111
return true;

base/cvd/cuttlefish/host/commands/cvd/instances/instance_manager.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,9 @@ Result<bool> InstanceManager::RemoveInstanceGroup(LocalInstanceGroup group) {
189189
if (instance.id() == 0) {
190190
continue;
191191
}
192-
auto remove_res = lock_manager_.RemoveLockFile(instance.id());
193-
if (!remove_res.ok()) {
192+
if (auto res = lock_manager_.RemoveLockFile(instance.id()); !res.ok()) {
194193
LOG(ERROR) << "Failed to remove instance id lock: "
195-
<< remove_res.error().FormatForEnv();
194+
<< res.error().FormatForEnv();
196195
}
197196
}
198197
CF_EXPECT(RemoveGroupDirectory(group));
@@ -257,8 +256,14 @@ Result<void> InstanceManager::Clear() {
257256
<< res.error().FormatForEnv();
258257
}
259258
}
260-
RemoveFile(group.HomeDir() + "/cuttlefish_runtime");
261-
RemoveFile(group.HomeDir() + config_json_name);
259+
std::string runtime_link = group.HomeDir() + "/cuttlefish_runtime";
260+
if (Result<void> res = RemoveFile(runtime_link);!res.ok()) {
261+
LOG(ERROR) << res.error().FormatForEnv();
262+
}
263+
std::string config_link = group.HomeDir() + config_json_name;
264+
if (Result<void> res = RemoveFile(config_link);!res.ok()) {
265+
LOG(ERROR) << res.error().FormatForEnv();
266+
}
262267
RemoveGroupDirectory(group);
263268
}
264269
std::cerr << "Stopped all known instances\n";

base/cvd/cuttlefish/host/commands/cvd/instances/stop.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,12 @@ Result<void> DeleteLockFile(const GroupProcInfo& group_info) {
153153
lock_file_path_stream << lock_file_prefix << id << ".lock";
154154
auto lock_file_path = lock_file_path_stream.str();
155155
if (FileExists(lock_file_path) && !DirectoryExists(lock_file_path)) {
156-
if (RemoveFile(lock_file_path)) {
156+
if (Result<void> res = RemoveFile(lock_file_path); res.ok()) {
157157
LOG(DEBUG) << "Reset the lock file: " << lock_file_path;
158158
} else {
159159
all_success = false;
160-
LOG(ERROR) << "Failed to remove the lock file: " << lock_file_path;
160+
LOG(ERROR) << "Failed to remove the lock file '" << lock_file_path
161+
<< "': " << res.error().FormatForEnv();
161162
}
162163
}
163164
}
@@ -210,8 +211,8 @@ Result<void> DeleteAllOwnedInstanceLocks() {
210211
<< "' because it's not owned by current user";
211212
continue;
212213
}
213-
if (!RemoveFile(lock_file_path)) {
214-
LOG(ERROR) << "Failed to remove '" << lock_file_path << "'";
214+
if (Result<void> res = RemoveFile(lock_file_path); !res.ok()) {
215+
LOG(ERROR) << res.error().FormatForEnv();
215216
}
216217
}
217218
return {};

base/cvd/cuttlefish/host/commands/host_bugreport/main.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ Result<void> CvdHostBugreportMain(int argc, char** argv) {
213213

214214
LogError(WritableZip::Finalize(std::move(archive)));
215215

216-
if (!RemoveFile(log_filename)) {
217-
LOG(INFO) << "Failed to remove host bug report log file: " << log_filename;
216+
if (Result<void> res = RemoveFile(log_filename); !res.ok()) {
217+
LOG(INFO) << "Failed to remove host bug report log file '" << log_filename
218+
<< "': " << res.error().FormatForEnv();
218219
}
219220

220221
return {};

base/cvd/cuttlefish/host/commands/mkenvimage_slim/mkenvimage_slim.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ Result<int> MkenvimageSlimMain(int argc, char** argv) {
7979
return CF_ERR("Couldn't open the output file " + FLAGS_output_path);
8080
} else if (FLAGS_env_size !=
8181
WriteAll(output_fd, (char*)env_buffer.data(), FLAGS_env_size)) {
82-
RemoveFile(FLAGS_output_path);
82+
if (Result<void> res = RemoveFile(FLAGS_output_path); !res.ok()) {
83+
LOG(ERROR) << res.error().FormatForEnv();
84+
}
8385
return CF_ERR("Couldn't complete write to " + FLAGS_output_path);
8486
}
8587

base/cvd/cuttlefish/host/commands/run_cvd/server_loop_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ void ServerLoopImpl::RestartRunCvd(int notification_fd) {
415415
// undesired behavior. Always try to delete the file "restore" if a restart is
416416
// requested.
417417
if (IsRestoring(config_)) {
418-
CHECK(RemoveFile(config_.AssemblyPath("restore")));
418+
CHECK(RemoveFile(config_.AssemblyPath("restore")).ok());
419419
}
420420
auto config_path = config_.AssemblyPath("cuttlefish_config.json");
421421
auto followup_stdin = SharedFD::MemfdCreate("pseudo_stdin");

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ Result<void> InitializeDataImage(
217217
LOG(DEBUG) << instance.data_image() << " exists. Not creating it.";
218218
return {};
219219
case DataImageAction::kCreateBlankImage: {
220-
RemoveFile(instance.new_data_image());
220+
if (Result<void> res = RemoveFile(instance.new_data_image()); !res.ok()) {
221+
LOG(ERROR) << res.error().FormatForEnv();
222+
}
221223
CF_EXPECT(instance.blank_data_image_mb() != 0,
222224
"Expected `-blank_data_image_mb` to be set for "
223225
"image creation.");

0 commit comments

Comments
 (0)