Clean up overloading of FileExists
FileExists is used for directories and also returns false for 0-length
files. Both behaviors are a bit surprising, so split it into:
FileHasContent
DirExists
Test: Local build
Bug: 110097226
Change-Id: Ic6caf67a0c9194a44a75a5b5f1cb2a427312d8b4
Merged-In: Ic6caf67a0c9194a44a75a5b5f1cb2a427312d8b4
(cherry picked from commit 85b72a547709b77617d0861302ed3f9acdd6288d)
diff --git a/host/commands/launch/main.cc b/host/commands/launch/main.cc
index 3f7bd4d..d304d5e 100644
--- a/host/commands/launch/main.cc
+++ b/host/commands/launch/main.cc
@@ -294,9 +294,18 @@
}
}
-bool FileExists(const char* path) {
+bool DirExists(const char* path) {
struct stat st;
- if (stat(path, &st))
+ if (stat(path, &st) == -1)
+ return false;
+ if ((st.st_mode & S_IFMT) != S_IFDIR)
+ return false;
+ return true;
+}
+
+bool FileHasContent(const char* path) {
+ struct stat st;
+ if (stat(path, &st) == -1)
return false;
if (st.st_size == 0)
return false;
@@ -327,7 +336,7 @@
}
bool ApplyDataImagePolicy(const char* data_image) {
- bool data_exists = FileExists(data_image);
+ bool data_exists = FileHasContent(data_image);
bool remove{};
bool create{};
@@ -371,7 +380,7 @@
}
bool EnsureDirExists(const char* dir) {
- if (!FileExists(dir)) {
+ if (!DirExists(dir)) {
LOG(INFO) << "Setting up " << dir;
if (mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
if (errno == EACCES) {
@@ -494,7 +503,7 @@
}
if (FLAGS_initrd.empty()) {
FLAGS_initrd = FLAGS_system_image_dir + "/ramdisk.img";
- if (!FileExists(FLAGS_initrd.c_str())) {
+ if (!FileHasContent(FLAGS_initrd.c_str())) {
LOG(WARNING) << "No ramdisk.img found; assuming system-as-root build";
FLAGS_initrd.clear();
}
@@ -518,7 +527,7 @@
for (const auto& file :
{FLAGS_system_image, FLAGS_vendor_image, FLAGS_cache_image, FLAGS_kernel,
FLAGS_data_image, FLAGS_kernel_command_line}) {
- if (!FileExists(file.c_str())) {
+ if (!FileHasContent(file.c_str())) {
LOG(FATAL) << "File not found: " << file;
return false;
}