diff --git a/base/cvd/cuttlefish/common/libs/utils/BUILD.bazel b/base/cvd/cuttlefish/common/libs/utils/BUILD.bazel index 6cd6074620b..1ed003e9558 100644 --- a/base/cvd/cuttlefish/common/libs/utils/BUILD.bazel +++ b/base/cvd/cuttlefish/common/libs/utils/BUILD.bazel @@ -139,6 +139,7 @@ cf_cc_test( "//cuttlefish/common/libs/utils:files", "//cuttlefish/files:are_hard_linked", "//cuttlefish/files:file_exists", + "//cuttlefish/files:is_directory_empty", "//cuttlefish/result", "//cuttlefish/result:result_matchers", "//libbase", diff --git a/base/cvd/cuttlefish/common/libs/utils/files.cpp b/base/cvd/cuttlefish/common/libs/utils/files.cpp index 203f813ba16..46ea6c4c5ef 100644 --- a/base/cvd/cuttlefish/common/libs/utils/files.cpp +++ b/base/cvd/cuttlefish/common/libs/utils/files.cpp @@ -19,7 +19,6 @@ #ifdef __linux__ #include #endif -#include #include #include #include @@ -34,7 +33,6 @@ #include #include #include -#include #include #include #include @@ -191,20 +189,6 @@ bool CanAccess(const std::string& path, const int mode) { return access(path.c_str(), mode) == 0; } -Result IsDirectoryEmpty(const std::string& path) { - std::unique_ptr direc(opendir(path.c_str()), closedir); - CF_EXPECTF(direc.get(), "opendir('{}') failed: {}", path, StrError(errno)); - - int cnt = 0; - while (::readdir(direc.get())) { - cnt++; - if (cnt > 2) { - return false; - } - } - return true; -} - std::string AbsolutePath(std::string_view path) { if (path.empty()) { return {}; diff --git a/base/cvd/cuttlefish/common/libs/utils/files.h b/base/cvd/cuttlefish/common/libs/utils/files.h index e2ed23cff55..cd1c56f54d6 100644 --- a/base/cvd/cuttlefish/common/libs/utils/files.h +++ b/base/cvd/cuttlefish/common/libs/utils/files.h @@ -51,7 +51,6 @@ Result EnsureDirectoryExists(const std::string& directory_path, Result ChangeGroup(const std::string& path, const std::string& group_name); bool CanAccess(const std::string& path, int mode); -Result IsDirectoryEmpty(const std::string& path); off_t FileSize(const std::string& path); Result RemoveFile(const std::string& file); Result RenameFile(const std::string& current_filepath, diff --git a/base/cvd/cuttlefish/common/libs/utils/files_test.cpp b/base/cvd/cuttlefish/common/libs/utils/files_test.cpp index 975e4e796f6..626c14f905f 100644 --- a/base/cvd/cuttlefish/common/libs/utils/files_test.cpp +++ b/base/cvd/cuttlefish/common/libs/utils/files_test.cpp @@ -30,6 +30,7 @@ #include "cuttlefish/files/are_hard_linked.h" #include "cuttlefish/files/file_exists.h" +#include "cuttlefish/files/is_directory_empty.h" #include "cuttlefish/result/result.h" #include "cuttlefish/result/result_matchers.h" diff --git a/base/cvd/cuttlefish/files/BUILD.bazel b/base/cvd/cuttlefish/files/BUILD.bazel index 78dfbd204c7..320ead40feb 100644 --- a/base/cvd/cuttlefish/files/BUILD.bazel +++ b/base/cvd/cuttlefish/files/BUILD.bazel @@ -62,6 +62,17 @@ cf_cc_library( hdrs = ["file_exists.h"], ) +cf_cc_library( + name = "is_directory_empty", + srcs = ["is_directory_empty.cc"], + hdrs = ["is_directory_empty.h"], + deps = [ + "//cuttlefish/posix:strerror", + "//cuttlefish/result:expect", + "//cuttlefish/result:result_type", + ], +) + cf_cc_library( name = "is_symlink", srcs = ["is_symlink.cc"], diff --git a/base/cvd/cuttlefish/files/is_directory_empty.cc b/base/cvd/cuttlefish/files/is_directory_empty.cc new file mode 100644 index 00000000000..75f86157a51 --- /dev/null +++ b/base/cvd/cuttlefish/files/is_directory_empty.cc @@ -0,0 +1,45 @@ +/* + * 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 "cuttlefish/files/is_directory_empty.h" + +#include +#include + +#include +#include + +#include "cuttlefish/posix/strerror.h" +#include "cuttlefish/result/expect.h" +#include "cuttlefish/result/result_type.h" + +namespace cuttlefish { + +Result IsDirectoryEmpty(const std::string& path) { + std::unique_ptr direc(opendir(path.c_str()), closedir); + CF_EXPECTF(direc.get(), "opendir('{}') failed: {}", path, StrError(errno)); + + int cnt = 0; + while (::readdir(direc.get())) { + cnt++; + if (cnt > 2) { + return false; + } + } + return true; +} + +} // namespace cuttlefish diff --git a/base/cvd/cuttlefish/files/is_directory_empty.h b/base/cvd/cuttlefish/files/is_directory_empty.h new file mode 100644 index 00000000000..4bfdc63c36f --- /dev/null +++ b/base/cvd/cuttlefish/files/is_directory_empty.h @@ -0,0 +1,26 @@ +/* + * 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 + +#include "cuttlefish/result/result_type.h" + +namespace cuttlefish { + +Result IsDirectoryEmpty(const std::string& path); + +} // namespace cuttlefish diff --git a/base/cvd/cuttlefish/host/commands/assemble_cvd/BUILD.bazel b/base/cvd/cuttlefish/host/commands/assemble_cvd/BUILD.bazel index b4853e1d16a..4d7e9234988 100644 --- a/base/cvd/cuttlefish/host/commands/assemble_cvd/BUILD.bazel +++ b/base/cvd/cuttlefish/host/commands/assemble_cvd/BUILD.bazel @@ -34,6 +34,7 @@ cf_cc_binary( "//cuttlefish/files:directory_contents", "//cuttlefish/files:directory_exists", "//cuttlefish/files:file_exists", + "//cuttlefish/files:is_directory_empty", "//cuttlefish/files:recursively_remove_directory", "//cuttlefish/flag_parser", "//cuttlefish/host/commands/assemble_cvd:assemble_cvd_flags", @@ -321,6 +322,7 @@ cf_cc_library( "//cuttlefish/common/libs/utils:network", "//cuttlefish/files:directory_exists", "//cuttlefish/files:file_exists", + "//cuttlefish/files:is_directory_empty", "//cuttlefish/host/commands/assemble_cvd:alloc", "//cuttlefish/host/commands/assemble_cvd:assemble_cvd_flags", "//cuttlefish/host/commands/assemble_cvd:disk_image_flags_vectorization", diff --git a/base/cvd/cuttlefish/host/commands/assemble_cvd/assemble_cvd.cc b/base/cvd/cuttlefish/host/commands/assemble_cvd/assemble_cvd.cc index 38aaff0c049..ad0a58c04ee 100644 --- a/base/cvd/cuttlefish/host/commands/assemble_cvd/assemble_cvd.cc +++ b/base/cvd/cuttlefish/host/commands/assemble_cvd/assemble_cvd.cc @@ -51,6 +51,7 @@ #include "cuttlefish/files/directory_contents.h" #include "cuttlefish/files/directory_exists.h" #include "cuttlefish/files/file_exists.h" +#include "cuttlefish/files/is_directory_empty.h" #include "cuttlefish/files/recursively_remove_directory.h" #include "cuttlefish/flag_parser/flag.h" #include "cuttlefish/flag_parser/gflags_compat.h" diff --git a/base/cvd/cuttlefish/host/commands/assemble_cvd/flags.cc b/base/cvd/cuttlefish/host/commands/assemble_cvd/flags.cc index 223d17ac986..2a1d7183d4d 100644 --- a/base/cvd/cuttlefish/host/commands/assemble_cvd/flags.cc +++ b/base/cvd/cuttlefish/host/commands/assemble_cvd/flags.cc @@ -54,6 +54,7 @@ #include "cuttlefish/common/libs/utils/network.h" #include "cuttlefish/files/directory_exists.h" #include "cuttlefish/files/file_exists.h" +#include "cuttlefish/files/is_directory_empty.h" #include "cuttlefish/host/commands/assemble_cvd/alloc.h" #include "cuttlefish/host/commands/assemble_cvd/assemble_cvd_flags.h" #include "cuttlefish/host/commands/assemble_cvd/disk_image_flags_vectorization.h" diff --git a/base/cvd/cuttlefish/host/commands/cvd/utils/BUILD.bazel b/base/cvd/cuttlefish/host/commands/cvd/utils/BUILD.bazel index 9c8f2e27b38..cdc56da3a3a 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/utils/BUILD.bazel +++ b/base/cvd/cuttlefish/host/commands/cvd/utils/BUILD.bazel @@ -17,6 +17,7 @@ cf_cc_library( "//cuttlefish/common/libs/utils:files", "//cuttlefish/files:directory_exists", "//cuttlefish/files:file_exists", + "//cuttlefish/files:is_directory_empty", "//cuttlefish/host/libs/config:config_utils", "//cuttlefish/result", "@abseil-cpp//absl/strings", diff --git a/base/cvd/cuttlefish/host/commands/cvd/utils/common.cpp b/base/cvd/cuttlefish/host/commands/cvd/utils/common.cpp index 82b08f2a46a..179d07d4234 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/utils/common.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/utils/common.cpp @@ -27,6 +27,7 @@ #include "cuttlefish/common/libs/utils/files.h" #include "cuttlefish/files/directory_exists.h" #include "cuttlefish/files/file_exists.h" +#include "cuttlefish/files/is_directory_empty.h" #include "cuttlefish/host/libs/config/config_utils.h" #include "cuttlefish/result/result.h"