Skip to content

Commit 92a0488

Browse files
committed
Expose lower-level zip source types from BuildApi and remote_zip.h
Creating the low-level types and wrapping them before returning it gets in the way of expressing the correct ordering of IO wrappers. The new function `OpenZip(BuildApi&, Build, std::string)` now expresses the intended layering of Buffer(Cache(Remote)). Bug: b/439624498
1 parent 8a7c5ed commit 92a0488

12 files changed

Lines changed: 121 additions & 47 deletions

base/cvd/cuttlefish/host/libs/web/BUILD.bazel

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ cf_cc_library(
3737
"//cuttlefish/host/libs/web/http_client:http_json",
3838
"//cuttlefish/host/libs/zip:remote_zip",
3939
"//cuttlefish/host/libs/zip:zip_cc",
40-
"//cuttlefish/host/libs/zip:zip_file",
4140
"//libbase",
4241
"@jsoncpp",
4342
],
@@ -90,6 +89,19 @@ cf_cc_library(
9089
],
9190
)
9291

92+
cf_cc_library(
93+
name = "build_api_zip",
94+
srcs = ["build_api_zip.cc"],
95+
hdrs = ["build_api_zip.h"],
96+
deps = [
97+
"//cuttlefish/common/libs/utils:result",
98+
"//cuttlefish/host/libs/web:android_build",
99+
"//cuttlefish/host/libs/web:build_api",
100+
"//cuttlefish/host/libs/zip:buffered_zip_source",
101+
"//cuttlefish/host/libs/zip:zip_cc",
102+
],
103+
)
104+
93105
cf_cc_library(
94106
name = "build_zip_name",
95107
srcs = ["build_zip_name.cc"],
@@ -113,8 +125,8 @@ cf_cc_library(
113125
"//cuttlefish/host/libs/web:credential_source",
114126
"//cuttlefish/host/libs/web/cas:cas_downloader",
115127
"//cuttlefish/host/libs/web/http_client",
128+
"//cuttlefish/host/libs/zip:cached_zip_source",
116129
"//cuttlefish/host/libs/zip:zip_cc",
117-
"//cuttlefish/host/libs/zip:zip_file",
118130
"//libbase",
119131
"@fmt",
120132
],

base/cvd/cuttlefish/host/libs/web/android_build_api.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
#include "cuttlefish/host/libs/web/http_client/http_json.h"
5353
#include "cuttlefish/host/libs/zip/remote_zip.h"
5454
#include "cuttlefish/host/libs/zip/zip_cc.h"
55-
#include "cuttlefish/host/libs/zip/zip_file.h"
5655

5756
namespace cuttlefish {
5857
namespace {
@@ -193,31 +192,31 @@ Result<std::string> AndroidBuildApi::DownloadFileWithBackup(
193192
return DownloadTargetFile(build, target_directory, selected_artifact);
194193
}
195194

196-
Result<ReadableZip> AndroidBuildApi::OpenZipArchive(
197-
const Build& build, const std::string& archive_name) {
198-
Result<ReadableZip> res =
199-
std::visit([this, &archive_name](
200-
auto&& arg) { return OpenZipArchive(arg, archive_name); },
195+
Result<SeekableZipSource> AndroidBuildApi::FileReader(
196+
const Build& build, const std::string& artifact_name) {
197+
Result<SeekableZipSource> res =
198+
std::visit([this, &artifact_name](
199+
auto&& arg) { return FileReader(arg, artifact_name); },
201200
build);
202201
return CF_EXPECT(std::move(res));
203202
}
204203

205-
Result<ReadableZip> AndroidBuildApi::OpenZipArchive(
206-
const DeviceBuild& build, const std::string& archive_name) {
207-
std::string url = CF_EXPECT(GetArtifactDownloadUrl(build, archive_name));
204+
Result<SeekableZipSource> AndroidBuildApi::FileReader(
205+
const DeviceBuild& build, const std::string& artifact_name) {
206+
std::string url = CF_EXPECT(GetArtifactDownloadUrl(build, artifact_name));
208207
std::vector<std::string> headers = CF_EXPECT(Headers());
209-
return CF_EXPECT(ZipFromUrl(http_client, url, headers));
208+
return CF_EXPECT(ZipSourceFromUrl(http_client, url, headers));
210209
}
211210

212-
Result<ReadableZip> AndroidBuildApi::OpenZipArchive(
213-
const DirectoryBuild& build, const std::string& archive_name) {
211+
Result<SeekableZipSource> AndroidBuildApi::FileReader(
212+
const DirectoryBuild& build, const std::string& artifact_name) {
214213
for (const std::string& path : build.paths) {
215-
std::string zip_path_attempt = path + "/" + archive_name;
214+
std::string zip_path_attempt = path + "/" + artifact_name;
216215
if (FileExists(zip_path_attempt)) {
217-
return CF_EXPECT(ZipOpenRead(zip_path_attempt));
216+
return CF_EXPECT(WritableZipSource::FromFile(zip_path_attempt));
218217
}
219218
}
220-
return CF_ERRF("Could not find '{}'", archive_name);
219+
return CF_ERRF("Could not find '{}'", artifact_name);
221220
}
222221

223222
Result<std::vector<std::string>> AndroidBuildApi::Headers() {

base/cvd/cuttlefish/host/libs/web/android_build_api.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class AndroidBuildApi : public BuildApi {
5858
const std::string& artifact_name,
5959
const std::string& backup_artifact_name) override;
6060

61-
Result<ReadableZip> OpenZipArchive(const Build& build,
62-
const std::string& archive_name) override;
61+
Result<SeekableZipSource> FileReader(
62+
const Build&, const std::string& artifact_name) override;
6363

6464
private:
6565
Result<std::vector<std::string>> Headers();
@@ -105,10 +105,10 @@ class AndroidBuildApi : public BuildApi {
105105
Result<Build> GetBuild(const DeviceBuildString& build_string);
106106
Result<Build> GetBuild(const DirectoryBuildString& build_string);
107107

108-
Result<ReadableZip> OpenZipArchive(const DeviceBuild& build,
109-
const std::string& archive_name);
110-
Result<ReadableZip> OpenZipArchive(const DirectoryBuild& build,
111-
const std::string& archive_name);
108+
Result<SeekableZipSource> FileReader(const DeviceBuild&,
109+
const std::string& artifact_name);
110+
Result<SeekableZipSource> FileReader(const DirectoryBuild&,
111+
const std::string& artifact_name);
112112

113113
HttpClient& http_client;
114114
CredentialSource* credential_source;

base/cvd/cuttlefish/host/libs/web/build_api.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class BuildApi {
3838
const std::string& artifact_name,
3939
const std::string& backup_artifact_name) = 0;
4040

41-
virtual Result<ReadableZip> OpenZipArchive(const Build& build,
42-
const std::string& artifact_name) = 0;
41+
virtual Result<SeekableZipSource> FileReader(
42+
const Build&, const std::string& artifact_name) = 0;
4343
};
4444

4545
} // namespace cuttlefish
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Copyright (C) 2025 The Android Open Source Project
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#include "cuttlefish/host/libs/web/build_api_zip.h"
17+
18+
#include <string>
19+
#include <utility>
20+
21+
#include "cuttlefish/common/libs/utils/result.h"
22+
#include "cuttlefish/host/libs/web/android_build.h"
23+
#include "cuttlefish/host/libs/web/build_api.h"
24+
#include "cuttlefish/host/libs/zip/buffered_zip_source.h"
25+
#include "cuttlefish/host/libs/zip/zip_cc.h"
26+
27+
namespace cuttlefish {
28+
29+
Result<ReadableZip> OpenZip(BuildApi& build_api, const Build& build,
30+
const std::string& name) {
31+
SeekableZipSource source = CF_EXPECT(build_api.FileReader(build, name));
32+
33+
SeekableZipSource buffered =
34+
CF_EXPECT(BufferZipSource(std::move(source), 1 << 16));
35+
36+
return CF_EXPECT(ReadableZip::FromSource(std::move(buffered)));
37+
}
38+
39+
} // namespace cuttlefish
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Copyright (C) 2025 The Android Open Source Project
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#pragma once
17+
18+
#include <string>
19+
20+
#include "cuttlefish/common/libs/utils/result.h"
21+
#include "cuttlefish/host/libs/web/android_build.h"
22+
#include "cuttlefish/host/libs/web/build_api.h"
23+
#include "cuttlefish/host/libs/zip/zip_cc.h"
24+
25+
namespace cuttlefish {
26+
27+
Result<ReadableZip> OpenZip(BuildApi&, const Build&, const std::string& name);
28+
29+
} // namespace cuttlefish

base/cvd/cuttlefish/host/libs/web/caching_build_api.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
#include "cuttlefish/host/libs/web/android_build_api.h"
2929
#include "cuttlefish/host/libs/web/android_build_string.h"
3030
#include "cuttlefish/host/libs/web/build_api.h"
31+
#include "cuttlefish/host/libs/zip/cached_zip_source.h"
3132
#include "cuttlefish/host/libs/zip/zip_cc.h"
32-
#include "cuttlefish/host/libs/zip/zip_file.h"
3333

3434
namespace cuttlefish {
3535
namespace {
@@ -144,11 +144,11 @@ Result<std::string> CachingBuildApi::DownloadFileWithBackup(
144144
paths.target_backup_artifact));
145145
}
146146

147-
Result<ReadableZip> CachingBuildApi::OpenZipArchive(
148-
const Build& build, const std::string& zip_name) {
149-
// TODO: schuffelen - cache only needed zip file parts
150-
CF_EXPECT(build_api_.DownloadFile(build, cache_base_path_, zip_name));
151-
return ZipOpenRead(cache_base_path_ + "/" + zip_name);
147+
Result<SeekableZipSource> CachingBuildApi::FileReader(
148+
const Build& build, const std::string& artifact) {
149+
SeekableZipSource source = CF_EXPECT(build_api_.FileReader(build, artifact));
150+
std::string cache_path = fmt::format("{}/{}", cache_base_path_, artifact);
151+
return CF_EXPECT(CacheZipSource(std::move(source), cache_path));
152152
}
153153

154154
} // namespace cuttlefish

base/cvd/cuttlefish/host/libs/web/caching_build_api.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class CachingBuildApi : public BuildApi {
3939
const std::string& artifact_name,
4040
const std::string& backup_artifact_name) override;
4141

42-
Result<ReadableZip> OpenZipArchive(const Build& build,
43-
const std::string& zip_name) override;
42+
Result<SeekableZipSource> FileReader(const Build&,
43+
const std::string& artifact) override;
4444

4545
private:
4646
Result<bool> CanCache(const std::string& target_directory);

base/cvd/cuttlefish/host/libs/zip/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ cf_cc_library(
106106
deps = [
107107
"//cuttlefish/common/libs/utils:result",
108108
"//cuttlefish/host/libs/web/http_client",
109-
"//cuttlefish/host/libs/zip:buffered_zip_source",
110109
"//cuttlefish/host/libs/zip:zip_cc",
111110
"@abseil-cpp//absl/strings",
112111
"@fmt",

base/cvd/cuttlefish/host/libs/zip/remote_zip.cc

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
#include "cuttlefish/common/libs/utils/result.h"
3232
#include "cuttlefish/host/libs/web/http_client/http_client.h"
33-
#include "cuttlefish/host/libs/zip/buffered_zip_source.h"
3433
#include "cuttlefish/host/libs/zip/zip_cc.h"
3534

3635
namespace cuttlefish {
@@ -124,22 +123,17 @@ Result<uint64_t> GetSizeIfSupportsRangeRequests(
124123

125124
} // namespace
126125

127-
Result<ReadableZip> ZipFromUrl(HttpClient& http_client, const std::string& url,
128-
std::vector<std::string> headers) {
126+
Result<SeekableZipSource> ZipSourceFromUrl(HttpClient& http_client,
127+
const std::string& url,
128+
std::vector<std::string> headers) {
129129
uint64_t size =
130130
CF_EXPECT(GetSizeIfSupportsRangeRequests(http_client, url, headers));
131131

132132
std::unique_ptr<RemoteZip> callbacks =
133133
std::make_unique<RemoteZip>(http_client, url, size, std::move(headers));
134134
CF_EXPECT(callbacks.get());
135135

136-
SeekableZipSource source =
137-
CF_EXPECT(SeekableZipSource::FromCallbacks(std::move(callbacks)));
138-
139-
SeekableZipSource buffered =
140-
CF_EXPECT(BufferZipSource(std::move(source), 1 << 16));
141-
142-
return CF_EXPECT(ReadableZip::FromSource(std::move(buffered)));
136+
return CF_EXPECT(SeekableZipSource::FromCallbacks(std::move(callbacks)));
143137
}
144138

145139
} // namespace cuttlefish

0 commit comments

Comments
 (0)