More file functions in libcuttlefish_utils
Move RemoveFile from the launcher to the library.
Add FileExists.
Bug: none
Test: local
Change-Id: I9033e0dbc9bc88e5326bca559eb1ef9c21a0694e
diff --git a/common/libs/utils/files.cpp b/common/libs/utils/files.cpp
index 654d971..432e8da 100644
--- a/common/libs/utils/files.cpp
+++ b/common/libs/utils/files.cpp
@@ -20,6 +20,7 @@
#include <array>
#include <climits>
+#include <cstdio>
#include <cstdlib>
#include <sys/types.h>
#include <sys/stat.h>
@@ -27,6 +28,11 @@
namespace cvd {
+bool FileExists(const std::string& path) {
+ struct stat st;
+ return stat(path.c_str(), &st) == 0;
+}
+
bool FileHasContent(const std::string& path) {
return FileSize(path) > 0;
}
@@ -67,4 +73,9 @@
return st.st_size;
}
+bool RemoveFile(const std::string& file) {
+ LOG(INFO) << "Removing " << file;
+ return remove(file.c_str()) == 0;
+}
+
} // namespace cvd
diff --git a/common/libs/utils/files.h b/common/libs/utils/files.h
index ce689ac..d05501d 100644
--- a/common/libs/utils/files.h
+++ b/common/libs/utils/files.h
@@ -20,9 +20,11 @@
#include <string>
namespace cvd {
+bool FileExists(const std::string& path);
bool FileHasContent(const std::string& path);
bool DirectoryExists(const std::string& path);
off_t FileSize(const std::string& path);
+bool RemoveFile(const std::string& file);
// The returned value may contain .. or . if these are present in the path
// argument.
diff --git a/host/commands/launch/data_image.cc b/host/commands/launch/data_image.cc
index 415a71e..9bdc421 100644
--- a/host/commands/launch/data_image.cc
+++ b/host/commands/launch/data_image.cc
@@ -11,11 +11,6 @@
const std::string kDataPolicyAlwaysCreate = "always_create";
const std::string kDataPolicyResizeUpTo= "resize_up_to";
-void RemoveFile(const std::string& file) {
- LOG(INFO) << "Removing " << file;
- remove(file.c_str());
-}
-
const int FSCK_ERROR_CORRECTED = 1;
const int FSCK_ERROR_CORRECTED_REQUIRES_REBOOT = 2;
@@ -117,7 +112,7 @@
}
if (remove) {
- RemoveFile(data_image.c_str());
+ cvd::RemoveFile(data_image.c_str());
}
if (create) {