Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion base/cvd/cuttlefish/files/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ cf_cc_library(
srcs = ["directory_contents.cc"],
hdrs = ["directory_contents.h"],
deps = [
"//cuttlefish/posix:open_dir",
"//cuttlefish/result:expect",
"//cuttlefish/result:result_type",
],
Expand Down Expand Up @@ -90,7 +91,7 @@ cf_cc_library(
srcs = ["is_directory_empty.cc"],
hdrs = ["is_directory_empty.h"],
deps = [
"//cuttlefish/posix:strerror",
"//cuttlefish/posix:open_dir",
"//cuttlefish/result:expect",
"//cuttlefish/result:result_type",
],
Expand Down
4 changes: 2 additions & 2 deletions base/cvd/cuttlefish/files/directory_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
#include <string>
#include <vector>

#include "cuttlefish/posix/open_dir.h"
#include "cuttlefish/result/expect.h"
#include "cuttlefish/result/result_type.h"

namespace cuttlefish {

Result<std::vector<std::string>> DirectoryContents(const std::string& path) {
std::vector<std::string> ret;
std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(path.c_str()), closedir);
CF_EXPECTF(dir != nullptr, "Could not read from dir \"{}\"", path);
std::unique_ptr<DIR, CloseDir> dir = CF_EXPECT(OpenDir(path));
struct dirent* ent{};
while ((ent = readdir(dir.get()))) {
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
Expand Down
6 changes: 2 additions & 4 deletions base/cvd/cuttlefish/files/is_directory_empty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@
#include "cuttlefish/files/is_directory_empty.h"

#include <dirent.h>
#include <errno.h>

#include <memory>
#include <string>

#include "cuttlefish/posix/strerror.h"
#include "cuttlefish/posix/open_dir.h"
#include "cuttlefish/result/expect.h"
#include "cuttlefish/result/result_type.h"

namespace cuttlefish {

Result<bool> IsDirectoryEmpty(const std::string& path) {
std::unique_ptr<DIR, int (*)(DIR*)> direc(opendir(path.c_str()), closedir);
CF_EXPECTF(direc.get(), "opendir('{}') failed: {}", path, StrError(errno));
std::unique_ptr<DIR, CloseDir> direc = CF_EXPECT(OpenDir(path));

int cnt = 0;
while (::readdir(direc.get())) {
Expand Down
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/host/commands/assemble_cvd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ cf_cc_library(
"//cuttlefish/common/libs/utils:in_sandbox",
"//cuttlefish/files:directory_contents",
"//cuttlefish/host/libs/config:config_utils",
"//cuttlefish/posix:open_dir",
"//cuttlefish/posix:stat",
"//cuttlefish/posix:strerror",
"//cuttlefish/process:proc_file_utils",
Expand Down
6 changes: 2 additions & 4 deletions base/cvd/cuttlefish/host/commands/assemble_cvd/clean.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "cuttlefish/common/libs/utils/in_sandbox.h"
#include "cuttlefish/files/directory_contents.h"
#include "cuttlefish/host/libs/config/config_utils.h"
#include "cuttlefish/posix/open_dir.h"
#include "cuttlefish/posix/stat.h"
#include "cuttlefish/posix/strerror.h"
#include "cuttlefish/process/proc_file_utils.h"
Expand Down Expand Up @@ -66,10 +67,7 @@ Result<void> CleanPriorFiles(const std::string& path,
}
return {};
}
std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(path.c_str()), closedir);
if (!dir) {
return CF_ERRNO("Could not clean \"" << path << "\"");
}
std::unique_ptr<DIR, CloseDir> dir = CF_EXPECT(OpenDir(path));
for (auto entity = readdir(dir.get()); entity != nullptr;
entity = readdir(dir.get())) {
std::string entity_name(entity->d_name);
Expand Down
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/host/commands/stop/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ cf_cc_binary(
"//cuttlefish/flag_parser",
"//cuttlefish/host/libs/command_util",
"//cuttlefish/host/libs/config:cuttlefish_config",
"//cuttlefish/posix:open_dir",
"//cuttlefish/posix:strerror",
"//cuttlefish/result",
"@abseil-cpp//absl/log",
Expand Down
10 changes: 5 additions & 5 deletions base/cvd/cuttlefish/host/commands/stop/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "cuttlefish/host/libs/command_util/runner/defs.h"
#include "cuttlefish/host/libs/command_util/util.h"
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
#include "cuttlefish/posix/open_dir.h"
#include "cuttlefish/posix/strerror.h"
#include "cuttlefish/result/result.h"

Expand All @@ -63,14 +64,13 @@ std::set<std::string> FallbackDirs() {
std::string parent_path = StringFromEnv("HOME", ".");
paths.insert(parent_path + "/cuttlefish_assembly");

std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(parent_path.c_str()),
closedir);
if (!dir) {
Result<std::unique_ptr<DIR, CloseDir>> dir = OpenDir(parent_path);
if (!dir.has_value()) {
return paths;
}

for (auto entity = readdir(dir.get()); entity != nullptr;
entity = readdir(dir.get())) {
for (auto entity = readdir(dir->get()); entity != nullptr;
entity = readdir(dir->get())) {
std::string subdir(entity->d_name);
if (!absl::StartsWith(subdir, "cuttlefish_runtime.")) {
continue;
Expand Down
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/host/libs/directories/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ cf_cc_library(
"//cuttlefish/common/libs/utils:environment",
"//cuttlefish/common/libs/utils:files",
"//cuttlefish/common/libs/utils:users",
"//cuttlefish/posix:open_dir",
"//cuttlefish/posix:rename",
"//cuttlefish/posix:stat",
"//cuttlefish/posix:strerror",
Expand Down
5 changes: 2 additions & 3 deletions base/cvd/cuttlefish/host/libs/directories/xdg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "cuttlefish/common/libs/utils/environment.h"
#include "cuttlefish/common/libs/utils/files.h"
#include "cuttlefish/common/libs/utils/users.h"
#include "cuttlefish/posix/open_dir.h"
#include "cuttlefish/posix/rename.h"
#include "cuttlefish/posix/stat.h"
#include "cuttlefish/posix/strerror.h"
Expand Down Expand Up @@ -144,9 +145,7 @@ Result<std::vector<std::string>> FindCvdDataFiles(std::string_view path) {
results.emplace_back(std::move(test_path));
continue;
}
std::unique_ptr<DIR, int (*)(DIR*)> dir_iter(opendir(test_path.c_str()),
closedir);
CF_EXPECTF(dir_iter.get(), "Failed to open '{}'", path);
std::unique_ptr<DIR, CloseDir> dir_iter = CF_EXPECT(OpenDir(test_path));
dirent* entry;
while ((entry = readdir(dir_iter.get())) != nullptr) {
std::string entry_name(entry->d_name);
Expand Down
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/host/libs/web/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ cf_cc_library(
"//cuttlefish/host/libs/zip:remote_zip",
"//cuttlefish/host/libs/zip/libzip_cc:seekable_source",
"//cuttlefish/host/libs/zip/libzip_cc:writable_source",
"//cuttlefish/posix:open_dir",
"//cuttlefish/posix:symlink",
"//cuttlefish/result",
"//libbase",
Expand Down
8 changes: 2 additions & 6 deletions base/cvd/cuttlefish/host/libs/web/android_build_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include "cuttlefish/host/libs/zip/libzip_cc/seekable_source.h"
#include "cuttlefish/host/libs/zip/libzip_cc/writable_source.h"
#include "cuttlefish/host/libs/zip/remote_zip.h"
#include "cuttlefish/posix/open_dir.h"
#include "cuttlefish/posix/symlink.h"
#include "cuttlefish/result/result.h"

Expand All @@ -70,10 +71,6 @@ bool StatusIsTerminal(const std::string& status) {
return terminal_statuses.count(status) > 0;
}

struct CloseDir {
void operator()(DIR* dir) { closedir(dir); }
};

Result<Json::Value> GetResponseJson(const HttpResponse<Json::Value>& response,
const bool allow_redirect = false) {
// debug information in error responses floods stderr with too much text
Expand Down Expand Up @@ -363,8 +360,7 @@ Result<std::unordered_set<std::string>> AndroidBuildApi::Artifacts(
const DirectoryBuild& build, const std::vector<std::string>&) {
std::unordered_set<std::string> artifacts;
for (const auto& path : build.paths) {
auto dir = std::unique_ptr<DIR, CloseDir>(opendir(path.c_str()));
CF_EXPECT(dir != nullptr, "Could not read files from \"" << path << "\"");
std::unique_ptr<DIR, CloseDir> dir = CF_EXPECT(OpenDir(path));
for (auto entity = readdir(dir.get()); entity != nullptr;
entity = readdir(dir.get())) {
artifacts.emplace(std::string(entity->d_name));
Expand Down
11 changes: 11 additions & 0 deletions base/cvd/cuttlefish/posix/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ package(
default_visibility = ["//:android_cuttlefish"],
)

cf_cc_library(
name = "open_dir",
srcs = ["open_dir.cc"],
hdrs = ["open_dir.h"],
deps = [
"//cuttlefish/posix:strerror",
"//cuttlefish/result:expect",
"//cuttlefish/result:result_type",
],
)

cf_cc_library(
name = "rename",
srcs = ["rename.cc"],
Expand Down
51 changes: 51 additions & 0 deletions base/cvd/cuttlefish/posix/open_dir.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// Copyright (C) 2026 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/posix/open_dir.h"

#include <dirent.h>
#include <errno.h>

#include <memory>
#include <string>
#include <string_view>

#include "cuttlefish/posix/strerror.h"
#include "cuttlefish/result/expect.h"
#include "cuttlefish/result/result_type.h"

namespace cuttlefish {

void CloseDir::operator()(DIR* dir) {
if (dir) {
closedir(dir);
}
}

Result<std::unique_ptr<DIR, CloseDir>> OpenDir(const char* path) {
std::unique_ptr<DIR, CloseDir> ret(opendir(path));
CF_EXPECTF(ret.get(), "opendir('{}') failed: {}'", path, StrError(errno));
return ret;
}

Result<std::unique_ptr<DIR, CloseDir>> OpenDir(const std::string& path) {
return CF_EXPECT(OpenDir(path.c_str()));
}

Result<std::unique_ptr<DIR, CloseDir>> OpenDir(std::string_view path) {
return CF_EXPECT(OpenDir(std::string(path)));
}

} // namespace cuttlefish
36 changes: 36 additions & 0 deletions base/cvd/cuttlefish/posix/open_dir.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Copyright (C) 2026 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 <dirent.h>

#include <memory>
#include <string>
#include <string_view>

#include "cuttlefish/result/result_type.h"

namespace cuttlefish {

struct CloseDir {
void operator()(DIR* dir);
};

Result<std::unique_ptr<DIR, CloseDir>> OpenDir(const char*);
Result<std::unique_ptr<DIR, CloseDir>> OpenDir(const std::string&);
Result<std::unique_ptr<DIR, CloseDir>> OpenDir(std::string_view);

} // namespace cuttlefish
Loading