From ff60569c193ef2d43f1cd886bc0d945a7fa1a69e Mon Sep 17 00:00:00 2001 From: jagghoshal-blip Date: Wed, 21 Jan 2026 16:00:40 -0800 Subject: [PATCH 1/2] Enhance caching logic with directory checks - Add directory existence checks for target artifact paths. - The `ConstructCachePaths` function now returns a `Result` and handles the creation of all required directories, including the build cache and the parent directories for target artifacts. This centralizes directory management within the path construction logic. - Add cache hits/misses logging --- .../cuttlefish/host/libs/web/caching_build_api.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/base/cvd/cuttlefish/host/libs/web/caching_build_api.cpp b/base/cvd/cuttlefish/host/libs/web/caching_build_api.cpp index c0f40b8870d..41c478363f7 100644 --- a/base/cvd/cuttlefish/host/libs/web/caching_build_api.cpp +++ b/base/cvd/cuttlefish/host/libs/web/caching_build_api.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include #include "absl/log/log.h" @@ -98,6 +99,8 @@ Result CachingBuildApi::DownloadFile( if (!FileExists(paths.cache_artifact)) { CF_EXPECT(build_api_.DownloadFile(build, paths.build_cache, artifact_name)); } + CF_EXPECT(EnsureDirectoryExists( + std::string(android::base::Dirname(paths.target_artifact)))); return CF_EXPECT(CreateHardLink(paths.cache_artifact, paths.target_artifact, kOverwriteExistingFile)); } @@ -110,10 +113,14 @@ Result CachingBuildApi::DownloadFileWithBackup( artifact_name, backup_artifact_name); CF_EXPECT(EnsureDirectoryExists(paths.build_cache)); if (FileExists(paths.cache_artifact)) { + CF_EXPECT(EnsureDirectoryExists( + std::string(android::base::Dirname(paths.target_artifact)))); return CF_EXPECT(CreateHardLink(paths.cache_artifact, paths.target_artifact, kOverwriteExistingFile)); } if (FileExists(paths.cache_backup_artifact)) { + CF_EXPECT(EnsureDirectoryExists( + std::string(android::base::Dirname(paths.target_backup_artifact)))); return CF_EXPECT(CreateHardLink(paths.cache_backup_artifact, paths.target_backup_artifact, kOverwriteExistingFile)); @@ -121,9 +128,13 @@ Result CachingBuildApi::DownloadFileWithBackup( const auto artifact_filepath = CF_EXPECT(build_api_.DownloadFileWithBackup( build, paths.build_cache, artifact_name, backup_artifact_name)); if (absl::EndsWith(artifact_filepath, artifact_name)) { + CF_EXPECT(EnsureDirectoryExists( + std::string(android::base::Dirname(paths.target_artifact)))); return CF_EXPECT(CreateHardLink(paths.cache_artifact, paths.target_artifact, kOverwriteExistingFile)); } + CF_EXPECT(EnsureDirectoryExists( + std::string(android::base::Dirname(paths.target_backup_artifact)))); return CF_EXPECT(CreateHardLink(paths.cache_backup_artifact, paths.target_backup_artifact)); } From 9bb0f41353e1e8b686fc269ea8629e31b3a9fa0f Mon Sep 17 00:00:00 2001 From: jagghoshal-blip Date: Thu, 22 Jan 2026 17:36:48 +0000 Subject: [PATCH 2/2] Enhance caching logic with directory checks - Add directory existence check during caching - The `ConstructCachePaths` function now returns a `Result` and handles the creation of all required directories, including the build cache and the parent directories for target artifacts. This centralizes directory management within the path construction logic. - Add cache hit/miss logging Bug: 441779367 --- .../host/libs/web/caching_build_api.cpp | 56 ++++++++++++------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/base/cvd/cuttlefish/host/libs/web/caching_build_api.cpp b/base/cvd/cuttlefish/host/libs/web/caching_build_api.cpp index 41c478363f7..fed75ebf8ad 100644 --- a/base/cvd/cuttlefish/host/libs/web/caching_build_api.cpp +++ b/base/cvd/cuttlefish/host/libs/web/caching_build_api.cpp @@ -46,22 +46,27 @@ struct CachingPaths { std::string cache_backup_artifact; }; -CachingPaths ConstructCachePaths(const std::string& cache_base, - const Build& build, - const std::string& target_directory, - const std::string& artifact, - const std::string& backup_artifact = "") { +Result ConstructCachePaths(const std::string& cache_base, + const Build& build, + const std::string& target_directory, + const std::string& artifact, + const std::string& backup_artifact = "") { const auto [id, target] = GetBuildIdAndTarget(build); auto result = CachingPaths{ .build_cache = fmt::format("{}/{}/{}", cache_base, id, target), .target_artifact = ConstructTargetFilepath(target_directory, artifact), }; result.cache_artifact = ConstructTargetFilepath(result.build_cache, artifact); + CF_EXPECT(EnsureDirectoryExists(result.build_cache)); + CF_EXPECT( + EnsureDirectoryExists(android::base::Dirname(result.target_artifact))); if (!backup_artifact.empty()) { result.target_backup_artifact = ConstructTargetFilepath(target_directory, backup_artifact); result.cache_backup_artifact = ConstructTargetFilepath(result.build_cache, backup_artifact); + CF_EXPECT(EnsureDirectoryExists( + android::base::Dirname(result.target_backup_artifact))); } return result; } @@ -93,14 +98,18 @@ Result CachingBuildApi::GetBuild(const BuildString& build_string) { Result CachingBuildApi::DownloadFile( const Build& build, const std::string& target_directory, const std::string& artifact_name) { - const auto paths = ConstructCachePaths(cache_base_path_, build, - target_directory, artifact_name); - CF_EXPECT(EnsureDirectoryExists(paths.build_cache)); + const auto paths = CF_EXPECT(ConstructCachePaths(cache_base_path_, build, + target_directory, artifact_name)); if (!FileExists(paths.cache_artifact)) { + LOG(INFO) << artifact_name << " not in cache. Downloading to " + << paths.build_cache; CF_EXPECT(build_api_.DownloadFile(build, paths.build_cache, artifact_name)); + } else { + LOG(INFO) << "Found " << artifact_name << " in cache at " + << paths.cache_artifact; } - CF_EXPECT(EnsureDirectoryExists( - std::string(android::base::Dirname(paths.target_artifact)))); + LOG(INFO) << "Linking " << paths.cache_artifact << " to " + << paths.target_artifact; return CF_EXPECT(CreateHardLink(paths.cache_artifact, paths.target_artifact, kOverwriteExistingFile)); } @@ -109,32 +118,37 @@ Result CachingBuildApi::DownloadFileWithBackup( const Build& build, const std::string& target_directory, const std::string& artifact_name, const std::string& backup_artifact_name) { const auto paths = - ConstructCachePaths(cache_base_path_, build, target_directory, - artifact_name, backup_artifact_name); - CF_EXPECT(EnsureDirectoryExists(paths.build_cache)); + CF_EXPECT(ConstructCachePaths(cache_base_path_, build, target_directory, + artifact_name, backup_artifact_name)); if (FileExists(paths.cache_artifact)) { - CF_EXPECT(EnsureDirectoryExists( - std::string(android::base::Dirname(paths.target_artifact)))); + LOG(INFO) << "Found " << artifact_name << " in cache at " + << paths.cache_artifact; + LOG(INFO) << "Linking " << paths.cache_artifact << " to " + << paths.target_artifact; return CF_EXPECT(CreateHardLink(paths.cache_artifact, paths.target_artifact, kOverwriteExistingFile)); } if (FileExists(paths.cache_backup_artifact)) { - CF_EXPECT(EnsureDirectoryExists( - std::string(android::base::Dirname(paths.target_backup_artifact)))); + LOG(INFO) << "Found " << backup_artifact_name << " in cache at " + << paths.cache_backup_artifact; + LOG(INFO) << "Linking " << paths.cache_backup_artifact << " to " + << paths.target_backup_artifact; return CF_EXPECT(CreateHardLink(paths.cache_backup_artifact, paths.target_backup_artifact, kOverwriteExistingFile)); } + LOG(INFO) << artifact_name << " and " << backup_artifact_name + << " not in cache. Downloading to " << paths.build_cache; const auto artifact_filepath = CF_EXPECT(build_api_.DownloadFileWithBackup( build, paths.build_cache, artifact_name, backup_artifact_name)); if (absl::EndsWith(artifact_filepath, artifact_name)) { - CF_EXPECT(EnsureDirectoryExists( - std::string(android::base::Dirname(paths.target_artifact)))); + LOG(INFO) << "Linking " << paths.cache_artifact << " to " + << paths.target_artifact; return CF_EXPECT(CreateHardLink(paths.cache_artifact, paths.target_artifact, kOverwriteExistingFile)); } - CF_EXPECT(EnsureDirectoryExists( - std::string(android::base::Dirname(paths.target_backup_artifact)))); + LOG(INFO) << "Linking " << paths.cache_backup_artifact << " to " + << paths.target_backup_artifact; return CF_EXPECT(CreateHardLink(paths.cache_backup_artifact, paths.target_backup_artifact)); }