Move DirExists and FileHasContent to utils library
Bug: 110478603
Test: local & gce
Change-Id: I0c04877698a586c0723fe6c6f896f3e85973af0c
diff --git a/common/libs/utils/Android.bp b/common/libs/utils/Android.bp
index 0e41180..56c18e3 100644
--- a/common/libs/utils/Android.bp
+++ b/common/libs/utils/Android.bp
@@ -19,6 +19,7 @@
"subprocess.cpp",
"environment.cpp",
"size_utils.cpp",
+ "files.cpp",
],
header_libs: [
"cuttlefish_glog",
diff --git a/common/libs/utils/files.cpp b/common/libs/utils/files.cpp
new file mode 100644
index 0000000..f1bcea7
--- /dev/null
+++ b/common/libs/utils/files.cpp
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "common/libs/utils/files.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+namespace cvd {
+
+bool FileHasContent(const std::string& path) {
+ struct stat st;
+ if (stat(path.c_str(), &st) == -1)
+ return false;
+ if (st.st_size == 0)
+ return false;
+ return true;
+}
+
+bool DirectoryExists(const std::string& path) {
+ struct stat st;
+ if (stat(path.c_str(), &st) == -1) return false;
+ if ((st.st_mode & S_IFMT) != S_IFDIR) return false;
+ return true;
+}
+} // namespace cvd
diff --git a/common/libs/utils/files.h b/common/libs/utils/files.h
new file mode 100644
index 0000000..5229b3a
--- /dev/null
+++ b/common/libs/utils/files.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include <string>
+
+namespace cvd {
+bool FileHasContent(const std::string& path);
+bool DirectoryExists(const std::string& path);
+} // namespace cvd
diff --git a/host/commands/launch/main.cc b/host/commands/launch/main.cc
index 6ef18bb..e6c0335 100644
--- a/host/commands/launch/main.cc
+++ b/host/commands/launch/main.cc
@@ -38,6 +38,7 @@
#include "common/libs/fs/shared_select.h"
#include "common/libs/strings/str_split.h"
#include "common/libs/utils/environment.h"
+#include "common/libs/utils/files.h"
#include "common/libs/utils/subprocess.h"
#include "common/libs/utils/size_utils.h"
#include "common/vsoc/lib/vsoc_memory.h"
@@ -237,24 +238,6 @@
KernelLogMonitor& operator=(const KernelLogMonitor&) = delete;
};
-bool DirExists(const char* path) {
- struct stat 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;
- return true;
-}
-
void CreateBlankImage(
const std::string& image, int image_mb, const std::string& image_fmt) {
LOG(INFO) << "Creating " << image;
@@ -272,7 +255,7 @@
}
bool ApplyDataImagePolicy(const char* data_image) {
- bool data_exists = FileHasContent(data_image);
+ bool data_exists = cvd::FileHasContent(data_image);
bool remove{};
bool create{};
@@ -316,7 +299,7 @@
}
bool EnsureDirExists(const std::string& dir) {
- if (!DirExists(dir.c_str())) {
+ if (!cvd::DirectoryExists(dir.c_str())) {
LOG(INFO) << "Setting up " << dir;
if (mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
if (errno == EACCES) {
@@ -457,7 +440,7 @@
for (const auto& file :
{FLAGS_system_image, FLAGS_vendor_image, FLAGS_cache_image,
FLAGS_data_image, FLAGS_boot_image}) {
- if (!FileHasContent(file.c_str())) {
+ if (!cvd::FileHasContent(file.c_str())) {
LOG(FATAL) << "File not found: " << file;
return false;
}