diff --git a/base/cvd/cuttlefish/host/commands/cvd/cache/cache.h b/base/cvd/cuttlefish/host/commands/cvd/cache/cache.h index 85f027657e0..e7287fe8c8d 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/cache/cache.h +++ b/base/cvd/cuttlefish/host/commands/cvd/cache/cache.h @@ -23,6 +23,8 @@ namespace cuttlefish { +inline constexpr int kDefaultCacheSizeGB = 25; + Result<std::string> EmptyCache(const std::string& cache_directory); Result<std::string> GetCacheInfo(const std::string& cache_directory, bool json_formatted); diff --git a/base/cvd/cuttlefish/host/commands/cvd/cli/commands/cache.cpp b/base/cvd/cuttlefish/host/commands/cvd/cli/commands/cache.cpp index fb978d49230..1dd625138a2 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/cli/commands/cache.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/cli/commands/cache.cpp @@ -39,8 +39,6 @@ namespace cuttlefish { namespace { -constexpr int kDefaultCacheSizeGB = 25; - constexpr char kSummaryHelpText[] = "Manage the files cached by cvd"; enum class Action { diff --git a/base/cvd/cuttlefish/host/commands/cvd/cli/commands/fetch.cpp b/base/cvd/cuttlefish/host/commands/cvd/cli/commands/fetch.cpp index bfc47636735..9c2843b26a6 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/cli/commands/fetch.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/cli/commands/fetch.cpp @@ -29,10 +29,12 @@ #include "common/libs/utils/result.h" #include "common/libs/utils/subprocess.h" #include "common/libs/utils/tee_logging.h" +#include "host/commands/cvd/cache/cache.h" #include "host/commands/cvd/cli/commands/command_handler.h" #include "host/commands/cvd/cli/types.h" #include "host/commands/cvd/fetch/fetch_cvd.h" #include "host/commands/cvd/fetch/fetch_cvd_parser.h" +#include "host/commands/cvd/utils/common.h" namespace cuttlefish { @@ -60,7 +62,13 @@ Result<void> CvdFetchCommandHandler::Handle(const CommandRequest& request) { ScopedTeeLogger logger( LogToStderrAndFiles({log_file}, "", metadata_level, flags.verbosity)); - CF_EXPECT(FetchCvdMain(flags)); + Result<void> result = FetchCvdMain(flags); + if (flags.build_api_flags.enable_caching) { + LOG(INFO) << "Checking if fetch cache needs to be pruned"; + CF_EXPECT( + PruneCache(PerUserCacheDir(), flags.build_api_flags.max_cache_size_GB)); + } + CF_EXPECT(std::move(result)); return {}; } diff --git a/base/cvd/cuttlefish/host/commands/cvd/fetch/fetch_cvd_parser.cc b/base/cvd/cuttlefish/host/commands/cvd/fetch/fetch_cvd_parser.cc index a4da9c442a2..87bc471afd8 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/fetch/fetch_cvd_parser.cc +++ b/base/cvd/cuttlefish/host/commands/cvd/fetch/fetch_cvd_parser.cc @@ -119,6 +119,11 @@ std::vector<Flag> GetFlagsVector(FetchFlags& fetch_flags, flags.emplace_back( GflagsCompatFlag("enable_caching", build_api_flags.enable_caching) .Help("Whether to enable local fetch file caching or not")); + flags.emplace_back( + GflagsCompatFlag("max_cache_size_GB", build_api_flags.max_cache_size_GB) + .Help("Max allowed size(in gigabytes) of the local fetch file cache. " + " If the cache grows beyond this size it will be pruned after " + "the fetches complete.")); CredentialFlags& credential_flags = build_api_flags.credential_flags; flags.emplace_back( diff --git a/base/cvd/cuttlefish/host/commands/cvd/fetch/fetch_cvd_parser.h b/base/cvd/cuttlefish/host/commands/cvd/fetch/fetch_cvd_parser.h index ccb5c61fca0..c615856e2fb 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/fetch/fetch_cvd_parser.h +++ b/base/cvd/cuttlefish/host/commands/cvd/fetch/fetch_cvd_parser.h @@ -16,6 +16,7 @@ #pragma once #include <chrono> +#include <cstddef> #include <optional> #include <string> #include <vector> @@ -23,6 +24,7 @@ #include <android-base/logging.h> #include "common/libs/utils/result.h" +#include "host/commands/cvd/cache/cache.h" #include "host/libs/web/android_build_api.h" #include "host/libs/web/android_build_string.h" #include "host/libs/web/cas/cas_downloader.h" @@ -69,6 +71,7 @@ struct BuildApiFlags { bool external_dns_resolver = kDefaultExternalDnsResolver; std::string api_base_url = kAndroidBuildServiceUrl; bool enable_caching = kDefaultEnableCaching; + std::size_t max_cache_size_GB = kDefaultCacheSizeGB; CasDownloaderFlags cas_downloader_flags; };