Skip to content
Open
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
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/common/libs/fs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ cf_cc_library(
deps = [
"//cuttlefish/common/libs/fs:file_instance",
"//cuttlefish/common/libs/utils:environment",
"//cuttlefish/posix:stat",
"//cuttlefish/posix:strerror",
"//cuttlefish/result",
"@abseil-cpp//absl/log",
Expand Down
4 changes: 2 additions & 2 deletions base/cvd/cuttlefish/common/libs/fs/unique_fd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "fmt/format.h"

#include "cuttlefish/common/libs/utils/known_paths.h"
#include "cuttlefish/posix/stat.h"
#include "cuttlefish/posix/strerror.h"
#include "cuttlefish/result/result.h"

Expand Down Expand Up @@ -210,8 +211,7 @@ UniqueFd UniqueFd::Creat(const std::string& path, mode_t mode) {
}

Result<UniqueFd> UniqueFd::Fifo(const std::string& path, mode_t mode) {
struct stat st{};
if (TEMP_FAILURE_RETRY(stat(path.c_str(), &st)) == 0) {
if (Stat(path).has_value()) {
CF_EXPECTF(TEMP_FAILURE_RETRY(remove(path.c_str())) == 0,
"Failed to delete old file at '{}': '{}'", path,
::cuttlefish::StrError(errno));
Expand Down
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/common/libs/utils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ cf_cc_library(
"//cuttlefish/io:string",
"//cuttlefish/posix:realpath",
"//cuttlefish/posix:rename",
"//cuttlefish/posix:stat",
"//cuttlefish/posix:strerror",
"//cuttlefish/result",
"//libbase",
Expand Down
43 changes: 15 additions & 28 deletions base/cvd/cuttlefish/common/libs/utils/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "cuttlefish/io/string.h"
#include "cuttlefish/posix/realpath.h"
#include "cuttlefish/posix/rename.h"
#include "cuttlefish/posix/stat.h"
#include "cuttlefish/posix/strerror.h"
#include "cuttlefish/result/result.h"

Expand Down Expand Up @@ -224,19 +225,12 @@ std::string AbsolutePath(std::string_view path) {
}

off_t FileSize(const std::string& path) {
struct stat st{};
if (stat(path.c_str(), &st) == -1) {
return 0;
}
return st.st_size;
static auto get_size = [](const struct stat& st) { return st.st_size; };
return Stat(path).transform(get_size).value_or(0);
}

Result<uid_t> FileOwner(const std::string& path) {
struct stat st{};
if (stat(path.c_str(), &st) == -1) {
return CF_ERRF("Failed to stat file '{}' : {}", path, StrError(errno));
}
return st.st_uid;
return CF_EXPECT(Stat(path)).st_uid;
}

bool MakeFileExecutable(const std::string& path) {
Expand All @@ -246,11 +240,7 @@ bool MakeFileExecutable(const std::string& path) {

Result<std::chrono::system_clock::time_point> FileModificationTime(
const std::string& path) {
struct stat st;
CF_EXPECTF(stat(path.c_str(), &st) == 0,
"stat() failed retrieving file modification time on \"{}\" with "
"error: {}",
path, strerror(errno));
struct stat st = CF_EXPECT(Stat(path));
#ifdef __linux__
std::chrono::seconds seconds(st.st_mtim.tv_sec);
#elif defined(__APPLE__)
Expand Down Expand Up @@ -383,8 +373,8 @@ FileSizes SparseFileSizes(const std::string& path) {
}

bool FileIsSocket(const std::string& path) {
struct stat st{};
return stat(path.c_str(), &st) == 0 && S_ISSOCK(st.st_mode);
static auto sock = [](const struct stat& st) { return S_ISSOCK(st.st_mode); };
return Stat(path).transform(sock).value_or(false);
}

Result<std::string> FindFile(const std::string& path,
Expand Down Expand Up @@ -439,22 +429,19 @@ Result<std::string> Search(const std::vector<std::string>& path,

Result<SharedFD> CreateOrReuseAndDrainFifo(const std::string& path,
mode_t mode) {
struct stat st{};
bool existed = false;
if (TEMP_FAILURE_RETRY(stat(path.c_str(), &st)) != 0) {
CF_EXPECTF(TEMP_FAILURE_RETRY(mkfifo(path.c_str(), mode)) == 0,
"Failed to mkfifo('{}', {:o}): {}", path, mode,
::cuttlefish::StrError(errno));
} else {
CF_EXPECTF(S_ISFIFO(st.st_mode), "File at '{}' exists but is not a FIFO",
Result<struct stat> st = Stat(path);
if (st.has_value()) {
CF_EXPECTF(S_ISFIFO(st->st_mode), "File at '{}' exists but is not a FIFO",
path);
existed = true;
} else {
CF_EXPECTF(TEMP_FAILURE_RETRY(mkfifo(path.c_str(), mode)) == 0,
"Failed to mkfifo('{}', {:o}): {}", path, mode, StrError(errno));
}

auto ret = SharedFD::Open(path, O_RDWR);
SharedFD ret = SharedFD::Open(path, O_RDWR);
CF_EXPECTF(ret->IsOpen(), "Failed to open '{}': '{}'", path, ret->StrError());

if (existed) {
if (st.has_value()) {
int flags = ret->Fcntl(F_GETFL, 0);
if (flags >= 0) {
ret->Fcntl(F_SETFL, flags | O_NONBLOCK);
Expand Down
5 changes: 3 additions & 2 deletions base/cvd/cuttlefish/files/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cf_cc_library(
hdrs = ["are_hard_linked.h"],
deps = [
"//cuttlefish/files:file_device_id",
"//cuttlefish/posix:strerror",
"//cuttlefish/posix:stat",
"//cuttlefish/result:expect",
"//cuttlefish/result:result_type",
],
Expand All @@ -35,6 +35,7 @@ cf_cc_library(
hdrs = ["copy_with_attributes.h"],
deps = [
"//cuttlefish/files:copy",
"//cuttlefish/posix:stat",
"//cuttlefish/posix:strerror",
"//cuttlefish/result:expect",
"//cuttlefish/result:result_type",
Expand Down Expand Up @@ -62,7 +63,7 @@ cf_cc_library(
srcs = ["file_device_id.cc"],
hdrs = ["file_device_id.h"],
deps = [
"//cuttlefish/posix:strerror",
"//cuttlefish/posix:stat",
"//cuttlefish/result:expect",
"//cuttlefish/result:result_type",
],
Expand Down
11 changes: 2 additions & 9 deletions base/cvd/cuttlefish/files/are_hard_linked.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,21 @@

#include "cuttlefish/files/are_hard_linked.h"

#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <string>

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

namespace cuttlefish {
namespace {

Result<ino_t> FileInodeNumber(const std::string& path) {
struct stat out;
CF_EXPECTF(
stat(path.c_str(), &out) == 0,
"stat() failed trying to retrieve inode num information for \"{}\" "
"with error: {}",
path, StrError(errno));
return out.st_ino;
return CF_EXPECT(Stat(path)).st_ino;
}

} // namespace
Expand Down
8 changes: 4 additions & 4 deletions base/cvd/cuttlefish/files/copy_with_attributes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <string>

#include "cuttlefish/files/copy.h"
#include "cuttlefish/posix/stat.h"
#include "cuttlefish/posix/strerror.h"
#include "cuttlefish/result/expect.h"
#include "cuttlefish/result/result_type.h"
Expand All @@ -31,10 +33,8 @@ namespace cuttlefish {
Result<void> CopyWithAttributes(const std::string& from,
const std::string& to) {
CF_EXPECTF(Copy(from, to), "Failed to copy '{}' to '{}'", from, to);
struct stat st;
CF_EXPECTF(stat(from.c_str(), &st) >= 0, "Failed to stat '{}': {}", from,
StrError(errno));
CF_EXPECTF(chmod(to.c_str(), st.st_mode) >= 0, "Failed to chmod '{}': {}", to,
mode_t mode = CF_EXPECT(Stat(from)).st_mode;
CF_EXPECTF(chmod(to.c_str(), mode) >= 0, "Failed to chmod '{}': {}", to,
StrError(errno));
return {};
}
Expand Down
11 changes: 2 additions & 9 deletions base/cvd/cuttlefish/files/file_device_id.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,19 @@

#include "cuttlefish/files/file_device_id.h"

#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <string>

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

namespace cuttlefish {

Result<dev_t> FileDeviceId(const std::string& path) {
struct stat out;
CF_EXPECTF(
stat(path.c_str(), &out) == 0,
"stat() failed trying to retrieve device ID information for \"{}\" "
"with error: {}",
path, StrError(errno));
return out.st_dev;
return CF_EXPECT(Stat(path)).st_dev;
}

} // namespace cuttlefish
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 @@ -182,6 +182,7 @@ cf_cc_library(
"//cuttlefish/common/libs/utils:in_sandbox",
"//cuttlefish/files:directory_contents",
"//cuttlefish/host/libs/config:config_utils",
"//cuttlefish/posix:stat",
"//cuttlefish/posix:strerror",
"//cuttlefish/process:proc_file_utils",
"//cuttlefish/result",
Expand Down
7 changes: 4 additions & 3 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/stat.h"
#include "cuttlefish/posix/strerror.h"
#include "cuttlefish/process/proc_file_utils.h"
#include "cuttlefish/result/result.h"
Expand Down Expand Up @@ -149,14 +150,14 @@ Result<void> CleanPriorFiles(const std::vector<std::string>& paths,
std::set<std::string> prior_dirs;
std::set<std::string> prior_files;
for (const auto& path : paths) {
struct stat statbuf;
if (stat(path.c_str(), &statbuf) < 0) {
Result<struct stat> statbuf = Stat(path);
if (!statbuf.has_value()) {
if (errno == ENOENT) {
continue; // it doesn't exist yet, so there is no work to do
}
return CF_ERRNO("Could not stat \"" << path << "\"");
}
bool is_directory = (statbuf.st_mode & S_IFMT) == S_IFDIR;
bool is_directory = (statbuf->st_mode & S_IFMT) == S_IFDIR;
(is_directory ? prior_dirs : prior_files).emplace(path);
}
VLOG(0) << fmt::format("Prior dirs: {}", fmt::join(prior_dirs, ", "));
Expand Down
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/host/commands/cvdalloc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ cf_cc_library(
depend_on_what_you_use_enabled = False,
include_cleaner_enabled = False,
deps = [
"//cuttlefish/posix:stat",
"//cuttlefish/posix:strerror",
"//cuttlefish/result",
"//libbase",
Expand Down
8 changes: 3 additions & 5 deletions base/cvd/cuttlefish/host/commands/cvdalloc/privilege.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#include <errno.h>
#include <fcntl.h>
#include <string.h>
#if defined(__linux__)
#include <linux/capability.h>
#include <linux/prctl.h>
Expand All @@ -31,6 +30,7 @@

#include "absl/log/log.h"

#include "cuttlefish/posix/stat.h"
#include "cuttlefish/posix/strerror.h"

namespace cuttlefish {
Expand Down Expand Up @@ -87,11 +87,9 @@ static int SetAmbientCapabilities() {
#endif

Result<void> ValidateCvdallocBinary(std::string_view path) {
struct stat st;
int r = stat(path.data(), &st);
CF_EXPECTF(r == 0, "Could not stat the cvdalloc binary at '{}': '{}'", path,
StrError(errno));
struct stat st = CF_EXPECT(Stat(path));
#if defined(__linux__)
(void)st;
/* Try and determine if the cvdalloc binary has any capabilities. */
struct vfs_cap_data cap;
ssize_t s = getxattr(path.data(), XATTR_NAME_CAPS, &cap, sizeof(cap));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ cf_cc_library(
"//cuttlefish/host/libs/config:known_paths",
"//cuttlefish/host/libs/feature",
"//cuttlefish/host/libs/vm_manager",
"//cuttlefish/posix:strerror",
"//cuttlefish/posix:stat",
"//cuttlefish/process:command",
"//cuttlefish/process:subprocess",
"//cuttlefish/result",
Expand Down
9 changes: 2 additions & 7 deletions base/cvd/cuttlefish/host/commands/run_cvd/launch/cvdalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@

#include "cuttlefish/host/commands/run_cvd/launch/cvdalloc.h"

#include <errno.h>
#include <sys/socket.h>
#include <sys/stat.h>

#include <chrono>
#include <mutex>
Expand All @@ -39,7 +37,7 @@
#include "cuttlefish/host/libs/feature/command_source.h"
#include "cuttlefish/host/libs/feature/feature.h"
#include "cuttlefish/host/libs/vm_manager/vm_manager.h"
#include "cuttlefish/posix/strerror.h"
#include "cuttlefish/posix/stat.h"
#include "cuttlefish/process/command.h"
#include "cuttlefish/process/subprocess.h"
#include "cuttlefish/result/result.h"
Expand Down Expand Up @@ -103,10 +101,7 @@ Result<void> Cvdalloc::ResultSetup() {
}

Result<void> Cvdalloc::BinaryIsValid(std::string_view path) {
struct stat st; // NOLINT(misc-include-cleaner): sys/stat.h
int r = stat(path.data(), &st);
CF_EXPECT(r == 0, "Could not stat the cvdalloc binary at "
<< path << ": " << StrError(errno));
CF_EXPECT(Stat(path));
CF_EXPECT(ValidateCvdallocBinary(path));
return {};
}
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 @@ -19,6 +19,7 @@ cf_cc_library(
"//cuttlefish/common/libs/utils:files",
"//cuttlefish/common/libs/utils:users",
"//cuttlefish/posix:rename",
"//cuttlefish/posix:stat",
"//cuttlefish/posix:strerror",
"//cuttlefish/result",
"//libbase",
Expand Down
7 changes: 4 additions & 3 deletions base/cvd/cuttlefish/host/libs/directories/xdg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "cuttlefish/common/libs/utils/files.h"
#include "cuttlefish/common/libs/utils/users.h"
#include "cuttlefish/posix/rename.h"
#include "cuttlefish/posix/stat.h"
#include "cuttlefish/posix/strerror.h"
#include "cuttlefish/result/result.h"

Expand Down Expand Up @@ -134,12 +135,12 @@ Result<std::string> ReadCvdDataFile(std::string_view path) {
Result<std::vector<std::string>> FindCvdDataFiles(std::string_view path) {
std::vector<std::string> results;
for (const std::string& dir : CF_EXPECT(CvdDataDirs())) {
struct stat statbuf;
std::string test_path = fmt::format("{}/{}", dir, path);
if (stat(test_path.c_str(), &statbuf) != 0) {
Result<struct stat> statbuf = Stat(test_path);
if (!statbuf.has_value()) {
continue;
}
if (!S_ISDIR(statbuf.st_mode)) {
if (!S_ISDIR(statbuf->st_mode)) {
results.emplace_back(std::move(test_path));
continue;
}
Expand Down
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/io/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ cf_cc_library(
"//cuttlefish/io",
"//cuttlefish/io:filesystem",
"//cuttlefish/io:shared_fd",
"//cuttlefish/posix:stat",
"//cuttlefish/posix:strerror",
"//cuttlefish/result:expect",
"//cuttlefish/result:result_type",
Expand Down
Loading
Loading