forked from google/android-cuttlefish
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaching_build_api.cpp
More file actions
129 lines (112 loc) · 4.67 KB
/
Copy pathcaching_build_api.cpp
File metadata and controls
129 lines (112 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// Copyright (C) 2024 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "cuttlefish/host/libs/web/caching_build_api.h"
#include <string>
#include <utility>
#include <android-base/file.h>
#include <fmt/core.h>
#include <fmt/format.h>
#include "absl/log/log.h"
#include "cuttlefish/common/libs/utils/files.h"
#include "cuttlefish/host/libs/web/android_build.h"
#include "cuttlefish/host/libs/web/android_build_api.h"
#include "cuttlefish/host/libs/web/android_build_string.h"
#include "cuttlefish/host/libs/web/build_api.h"
#include "cuttlefish/host/libs/zip/cached_zip_source.h"
#include "cuttlefish/host/libs/zip/libzip_cc/seekable_source.h"
#include "cuttlefish/result/result.h"
namespace cuttlefish {
namespace {
constexpr bool kOverwriteExistingFile = true;
struct CachingPaths {
std::string build_cache;
std::string target_artifact;
std::string cache_artifact;
std::string target_backup_artifact;
std::string cache_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;
}
bool IsInCache(const std::string& filepath) {
const bool exists = FileExists(filepath);
if (exists) {
VLOG(1) << "Found \"" << filepath << "\" in cache";
} else {
VLOG(1) << "\"" << filepath << "\" not in cache";
}
return exists;
}
} // namespace
bool CanCache(const std::string& target_directory,
const std::string& cache_base_path) {
const Result<bool> result = CanHardLink(target_directory, cache_base_path);
if (result.ok() && *result) {
return true;
}
if (!result.ok()) {
LOG(ERROR) << "Error during hard link check: " << result.error();
}
LOG(WARNING)
<< "Caching disabled, unable to hard link between fetch directory \""
<< target_directory << "\" and cache directory \"" << cache_base_path
<< "\"";
return false;
}
CachingBuildApi::CachingBuildApi(BuildApi& build_api,
std::string cache_base_path)
: build_api_(build_api), cache_base_path_(std::move(cache_base_path)) {};
Result<Build> CachingBuildApi::GetBuild(const BuildString& build_string) {
return CF_EXPECT(build_api_.GetBuild(build_string));
}
Result<std::string> CachingBuildApi::DownloadFile(
const Build& build, const std::string& target_directory,
const std::string& artifact_name) {
const auto paths = CF_EXPECT(ConstructCachePaths(cache_base_path_, build,
target_directory, artifact_name));
if (!IsInCache(paths.cache_artifact)) {
CF_EXPECT(build_api_.DownloadFile(build, paths.build_cache, artifact_name));
}
return CF_EXPECT(CreateHardLink(paths.cache_artifact, paths.target_artifact,
kOverwriteExistingFile));
}
Result<SeekableZipSource> CachingBuildApi::FileReader(
const Build& build, const std::string& artifact) {
SeekableZipSource source = CF_EXPECT(build_api_.FileReader(build, artifact));
std::string cache_path = fmt::format("{}/{}", cache_base_path_, artifact);
return CF_EXPECT(CacheZipSource(std::move(source), cache_path));
}
} // namespace cuttlefish