Skip to content
Merged
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
47 changes: 36 additions & 11 deletions base/cvd/cuttlefish/host/libs/web/caching_build_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <string>
#include <utility>

#include <android-base/file.h>
#include <fmt/core.h>
#include <fmt/format.h>
#include "absl/log/log.h"
Expand Down Expand Up @@ -45,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<CachingPaths> 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;
}
Expand Down Expand Up @@ -92,12 +98,18 @@ Result<Build> CachingBuildApi::GetBuild(const BuildString& build_string) {
Result<std::string> 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;
}
LOG(INFO) << "Linking " << paths.cache_artifact << " to "
<< paths.target_artifact;
return CF_EXPECT(CreateHardLink(paths.cache_artifact, paths.target_artifact,
kOverwriteExistingFile));
}
Expand All @@ -106,24 +118,37 @@ Result<std::string> 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)) {
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)) {
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)) {
LOG(INFO) << "Linking " << paths.cache_artifact << " to "
<< paths.target_artifact;
return CF_EXPECT(CreateHardLink(paths.cache_artifact, paths.target_artifact,
kOverwriteExistingFile));
}
LOG(INFO) << "Linking " << paths.cache_backup_artifact << " to "
<< paths.target_backup_artifact;
return CF_EXPECT(CreateHardLink(paths.cache_backup_artifact,
paths.target_backup_artifact));
}
Expand Down
Loading