Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cvd cache #834

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
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
63 changes: 45 additions & 18 deletions base/cvd/cuttlefish/common/libs/utils/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <array>
#include <cerrno>
#include <chrono>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -76,6 +77,8 @@

namespace cuttlefish {

static constexpr char kWhitespaceCharacters[] = " \n\t\r\v\f";

bool FileExists(const std::string& path, bool follow_symlinks) {
struct stat st {};
return (follow_symlinks ? stat : lstat)(path.c_str(), &st) == 0;
Expand Down Expand Up @@ -226,6 +229,25 @@ Result<std::vector<std::string>> DirectoryContents(const std::string& path) {
return ret;
}

Result<std::vector<std::string>> DirectoryContentsByModTimeDesc(
const std::string& path) {
std::vector<std::string> contents =
CF_EXPECTF(DirectoryContents(path),
"Failure retrieving contents of directory at \"{}\"", path);

auto not_self_or_parent_directory = [](std::string filename) {
return filename != "." && filename != "..";
};
std::vector<std::string> filtered;
std::copy_if(contents.begin(), contents.end(), std::back_inserter(filtered),
not_self_or_parent_directory);

std::sort(filtered.begin(), filtered.end(), [](std::string a, std::string b) {
return FileModificationTime(a) > FileModificationTime(b);
});
return filtered;
}

bool DirectoryExists(const std::string& path, bool follow_symlinks) {
struct stat st {};
if ((follow_symlinks ? stat : lstat)(path.c_str(), &st) == -1) {
Expand Down Expand Up @@ -613,35 +635,40 @@ bool FileIsSocket(const std::string& path) {
}

// return unit determined by the `--block-size` argument
Result<long> GetDiskUsage(const std::string& path,
const std::string& size_arg) {
Result<std::size_t> GetDiskUsage(const std::string& path,
const std::string& size_arg) {
Command du_cmd("du");
du_cmd.AddParameter("-s"); // summarize, only output total
du_cmd.AddParameter(
"--apparent-size"); // apparent size rather than device usage
du_cmd.AddParameter("--block-size=" + size_arg);
du_cmd.AddParameter(path);
SharedFD read_fd;
SharedFD write_fd;
SharedFD::Pipe(&read_fd, &write_fd);
du_cmd.RedirectStdIO(Subprocess::StdIOChannel::kStdOut, write_fd);
auto subprocess = du_cmd.Start();
std::array<char, 1024> text_output{};
const auto bytes_read = read_fd->Read(text_output.data(), text_output.size());
CF_EXPECTF(bytes_read > 0, "Failed to read from pipe: {}", strerror(errno));
std::move(subprocess).Wait();
int result;
CF_EXPECTF(android::base::ParseInt(text_output.data(), &result),
"Failure parsing \"{}\" to integer.", text_output.data());

std::string out;
std::string err;
int return_code = RunWithManagedStdio(std::move(du_cmd), nullptr, &out, &err);
CF_EXPECT(return_code == 0, "Failed to run `du` command");
CF_EXPECT(out.empty() == false, "No output read from `du` command");
std::vector<std::string> split_out =
android::base::Tokenize(out, kWhitespaceCharacters);
CF_EXPECTF(split_out.empty() == false,
"No valid output read from `du` command in \"{}\"", out);
std::string total = split_out.front();

std::size_t result;
CF_EXPECTF(android::base::ParseUint(total, &result),
"Failure parsing \"{}\" to integer.", total);
return result;
}

Result<long> GetDiskUsageBytes(const std::string& path) {
return GetDiskUsage(path, "1");
Result<std::size_t> GetDiskUsageBytes(const std::string& path) {
return CF_EXPECTF(GetDiskUsage(path, "1"),
"Unable to determine disk usage of file \"{}\"", path);
}

Result<long> GetDiskUsageGigabytes(const std::string& path) {
return GetDiskUsage(path, "1G");
Result<std::size_t> GetDiskUsageGigabytes(const std::string& path) {
return CF_EXPECTF(GetDiskUsage(path, "1G"),
"Unable to determine disk usage of file \"{}\"", path);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions base/cvd/cuttlefish/common/libs/utils/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <sys/types.h>

#include <chrono>
#include <cstddef>
#include <optional>
#include <string>
#include <vector>
Expand Down Expand Up @@ -50,6 +51,9 @@ Result<void> MoveDirectoryContents(const std::string& source,
const std::string& destination);
bool FileHasContent(const std::string& path);
Result<std::vector<std::string>> DirectoryContents(const std::string& path);
// does not include self-reference or parent reference ("." or "..")
Result<std::vector<std::string>> DirectoryContentsByModTimeDesc(
const std::string& path);
bool DirectoryExists(const std::string& path, bool follow_symlinks = true);
inline bool IsDirectory(const std::string& path) {
return DirectoryExists(path);
Expand Down Expand Up @@ -78,8 +82,8 @@ std::string cpp_basename(const std::string& str);
bool FileIsSocket(const std::string& path);
// Get disk usage of a path. If this path is a directory, disk usage will
// account for all files under this folder(recursively).
Result<long> GetDiskUsageBytes(const std::string& path);
Result<long> GetDiskUsageGigabytes(const std::string& path);
Result<std::size_t> GetDiskUsageBytes(const std::string& path);
Result<std::size_t> GetDiskUsageGigabytes(const std::string& path);

// acloud related API
std::string FindImage(const std::string& search_path,
Expand Down
21 changes: 21 additions & 0 deletions base/cvd/cuttlefish/common/libs/utils/flag_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <algorithm>
#include <cerrno>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <functional>
Expand All @@ -34,6 +35,7 @@

#include <android-base/logging.h>
#include <android-base/parsebool.h>
#include <android-base/parseint.h>
#include <android-base/scopeguard.h>
#include <android-base/strings.h>
#include <fmt/format.h>
Expand Down Expand Up @@ -566,6 +568,25 @@ Flag GflagsCompatFlag(const std::string& name, int32_t& value) {
return GflagsCompatNumericFlagGeneric(name, value);
}

template <typename T>
static Flag GflagsCompatUnsignedNumericFlagGeneric(const std::string& name,
T& value) {
return GflagsCompatFlag(name)
.Getter([&value]() { return std::to_string(value); })
.Setter([&value](const FlagMatch& match) -> Result<void> {
T result;
CF_EXPECTF(android::base::ParseUint<T>(match.value, &result),
"Failed to parse \"{}\" as an unsigned integer",
match.value);
value = result;
return {};
});
}

Flag GflagsCompatFlag(const std::string& name, std::size_t& value) {
return GflagsCompatUnsignedNumericFlagGeneric(name, value);
}

Flag GflagsCompatFlag(const std::string& name, bool& value) {
return GflagsCompatBoolFlagBase(name)
.Getter([&value]() { return fmt::format("{}", value); })
Expand Down
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/common/libs/utils/flag_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ Flag UnexpectedArgumentGuard();
Flag GflagsCompatFlag(const std::string& name);
Flag GflagsCompatFlag(const std::string& name, std::string& value);
Flag GflagsCompatFlag(const std::string& name, std::int32_t& value);
Flag GflagsCompatFlag(const std::string& name, std::size_t& value);
Flag GflagsCompatFlag(const std::string& name, bool& value);
Flag GflagsCompatFlag(const std::string& name, std::vector<std::string>& value);
Flag GflagsCompatFlag(const std::string& name, std::vector<bool>& value,
Expand Down
21 changes: 21 additions & 0 deletions base/cvd/cuttlefish/common/libs/utils/tee_logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ TeeLogger::TeeLogger(const std::vector<SeverityTarget>& destinations,
const std::string& prefix)
: destinations_(destinations), prefix_(prefix) {}

ScopedTeeLogger::ScopedTeeLogger(TeeLogger tee_logger)
// Set the android logger to full verbosity, the tee_logger will choose
// whether to write each line.
: scoped_severity_(android::base::VERBOSE) {
old_logger_ = android::base::SetLogger(tee_logger);
}

ScopedTeeLogger::~ScopedTeeLogger() {
// restore the previous logger
android::base::SetLogger(std::move(old_logger_));
}

// Copied from system/libbase/logging_splitters.h
static std::pair<int, int> CountSizeAndNewLines(const char* message) {
int size = 0;
Expand Down Expand Up @@ -273,6 +285,15 @@ static std::vector<SeverityTarget> SeverityTargetsForFiles(
return log_severities;
}

TeeLogger LogToStderr(
const std::string& log_prefix, MetadataLevel stderr_level,
std::optional<android::base::LogSeverity> stderr_severity) {
std::vector<SeverityTarget> log_severities{
SeverityTarget{stderr_severity ? *stderr_severity : ConsoleSeverity(),
SharedFD::Dup(/* stderr */ 2), stderr_level}};
return TeeLogger(log_severities, log_prefix);
}

TeeLogger LogToFiles(const std::vector<std::string>& files,
const std::string& log_prefix) {
return TeeLogger(SeverityTargetsForFiles(files), log_prefix);
Expand Down
14 changes: 14 additions & 0 deletions base/cvd/cuttlefish/common/libs/utils/tee_logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ class TeeLogger {
std::string prefix_;
};

class ScopedTeeLogger {
public:
ScopedTeeLogger(TeeLogger tee_logger);
~ScopedTeeLogger();

private:
android::base::LogFunction old_logger_;
android::base::ScopedLogSeverity scoped_severity_;
};

TeeLogger LogToStderr(
const std::string& log_prefix = "",
MetadataLevel stderr_level = MetadataLevel::ONLY_MESSAGE,
std::optional<android::base::LogSeverity> stderr_severity = std::nullopt);
TeeLogger LogToFiles(const std::vector<std::string>& files,
const std::string& log_prefix = "");
TeeLogger LogToStderrAndFiles(
Expand Down
2 changes: 2 additions & 0 deletions base/cvd/cuttlefish/host/commands/cvd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ cc_library(
"cli/commands/acloud_mixsuperimage.cpp",
"cli/commands/acloud_translator.cpp",
"cli/commands/bugreport.cpp",
"cli/commands/cache.cpp",
"cli/commands/clear.cpp",
"cli/commands/cmd_list.cpp",
"cli/commands/create.cpp",
Expand Down Expand Up @@ -154,6 +155,7 @@ cc_library(
"cli/commands/acloud_mixsuperimage.h",
"cli/commands/acloud_translator.h",
"cli/commands/bugreport.h",
"cli/commands/cache.h",
"cli/commands/clear.h",
"cli/commands/cmd_list.h",
"cli/commands/create.h",
Expand Down
Loading