Skip to content

Commit 58f4bf3

Browse files
committed
Create a type to represent the *-img-*.zip files that come out of the build system
Bug: b/470448309
1 parent 6f28ea9 commit 58f4bf3

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed

base/cvd/cuttlefish/host/commands/assemble_cvd/android_build/BUILD.bazel

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,23 @@ cf_cc_library(
4444
"@abseil-cpp//absl/strings",
4545
],
4646
)
47+
48+
cf_cc_library(
49+
name = "img_zip",
50+
srcs = ["img_zip.cc"],
51+
hdrs = ["img_zip.h"],
52+
deps = [
53+
"//cuttlefish/common/libs/key_equals_value",
54+
"//cuttlefish/common/libs/utils:result",
55+
"//cuttlefish/host/commands/assemble_cvd:guest_config_cc_proto",
56+
"//cuttlefish/host/commands/assemble_cvd/android_build",
57+
"//cuttlefish/host/commands/assemble_cvd/android_build:find_build_archive",
58+
"//cuttlefish/host/libs/config:build_archive",
59+
"//cuttlefish/host/libs/config:fetcher_config",
60+
"//cuttlefish/host/libs/config:file_source",
61+
"@abseil-cpp//absl/strings",
62+
"@abseil-cpp//absl/strings:str_format",
63+
"@fmt",
64+
"@protobuf",
65+
],
66+
)
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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/commands/assemble_cvd/android_build/img_zip.h"
17+
18+
#include <functional>
19+
#include <map>
20+
#include <memory>
21+
#include <optional>
22+
#include <ostream>
23+
#include <set>
24+
#include <string>
25+
#include <string_view>
26+
#include <utility>
27+
28+
#include "absl/strings/str_cat.h"
29+
#include "absl/strings/strip.h"
30+
#include "fmt/ostream.h"
31+
#include "google/protobuf/text_format.h"
32+
33+
#include "cuttlefish/common/libs/key_equals_value/key_equals_value.h"
34+
#include "cuttlefish/common/libs/utils/result.h"
35+
#include "cuttlefish/host/commands/assemble_cvd/android_build/android_build.h"
36+
#include "cuttlefish/host/commands/assemble_cvd/android_build/find_build_archive.h"
37+
#include "cuttlefish/host/commands/assemble_cvd/proto/guest_config.pb.h"
38+
#include "cuttlefish/host/libs/config/build_archive.h"
39+
#include "cuttlefish/host/libs/config/fetcher_config.h"
40+
#include "cuttlefish/host/libs/config/file_source.h"
41+
42+
namespace cuttlefish {
43+
namespace {
44+
45+
static constexpr std::string_view kImgMatch = "-img-";
46+
static constexpr std::string_view kImgSuffix = ".img";
47+
48+
class ImgZipImpl : public AndroidBuild {
49+
public:
50+
static Result<std::unique_ptr<ImgZipImpl>> FromBuildArchive(
51+
BuildArchive archive) {
52+
std::unique_ptr<ImgZipImpl> img_zip(new ImgZipImpl(std::move(archive)));
53+
54+
CF_EXPECT(img_zip->Images());
55+
56+
return std::move(img_zip);
57+
}
58+
59+
Result<std::set<std::string, std::less<void>>> Images() override {
60+
std::set<std::string, std::less<void>> partitions;
61+
for (std::string_view member : archive_.Members()) {
62+
if (!absl::ConsumeSuffix(&member, kImgSuffix)) {
63+
continue;
64+
}
65+
absl::ConsumePrefix(&member, "/");
66+
partitions.emplace(member);
67+
}
68+
return partitions;
69+
}
70+
71+
Result<std::string> ImageFile(
72+
std::string_view name,
73+
std::optional<std::string_view> extract_dir) override {
74+
std::string member_name = absl::StrCat(name, kImgSuffix);
75+
return CF_EXPECT(archive_.MemberFilepath(member_name, extract_dir));
76+
}
77+
78+
// TODO: schuffelen - put in AndroidBuild
79+
Result<std::map<std::string, std::string>> AndroidInfoTxt() {
80+
std::string contents =
81+
CF_EXPECT(archive_.MemberContents("android-info.txt"));
82+
return CF_EXPECT(ParseKeyEqualsValue(contents));
83+
}
84+
85+
// TODO: schuffelen - put in AndroidBuild
86+
Result<config::GuestConfigFile> GuestConfigProto() {
87+
std::string contents =
88+
CF_EXPECT(archive_.MemberContents("cuttlefish-guest-config.txtpb"));
89+
90+
config::GuestConfigFile proto_config;
91+
CF_EXPECT(
92+
google::protobuf::TextFormat::ParseFromString(contents, &proto_config));
93+
94+
return proto_config;
95+
}
96+
97+
private:
98+
ImgZipImpl(BuildArchive archive) : archive_(std::move(archive)) {}
99+
100+
std::ostream& Format(std::ostream& out) const override {
101+
fmt::print(out, "ImgZip {{ {} }}", archive_);
102+
return out;
103+
}
104+
105+
BuildArchive archive_;
106+
};
107+
108+
} // namespace
109+
110+
Result<std::unique_ptr<AndroidBuild>> ImgZip(const FetcherConfig& config,
111+
FileSource source) {
112+
BuildArchive archive = CF_EXPECT(FindBuildArchive(config, source, kImgMatch));
113+
return CF_EXPECT(ImgZipImpl::FromBuildArchive(std::move(archive)));
114+
}
115+
116+
Result<std::unique_ptr<AndroidBuild>> ImgZip(const std::string& path) {
117+
BuildArchive archive = CF_EXPECT(FindBuildArchive(path, kImgMatch));
118+
return CF_EXPECT(ImgZipImpl::FromBuildArchive(std::move(archive)));
119+
}
120+
121+
} // namespace cuttlefish
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 <memory>
19+
#include <string>
20+
21+
#include "cuttlefish/host/commands/assemble_cvd/android_build/android_build.h"
22+
#include "cuttlefish/host/libs/config/fetcher_config.h"
23+
#include "cuttlefish/host/libs/config/file_source.h"
24+
25+
namespace cuttlefish {
26+
27+
/**
28+
* Represents the <build target>-img-<build id>.zip file produced by the Android
29+
* build system, possibly in an extracted form produced by `cvd fetch`.
30+
*
31+
* Expected to contain `super.img` and other `.img` files that are physical
32+
* partitions. Does not contain `.img` files for the logical partitions inside
33+
* `super`.
34+
*
35+
* Also expected to contain the `android-info.txt` file and/or the
36+
* `cuttlefish-guest-config.txtpb` file. See go/cf-guest-config-pb for more
37+
* information.
38+
*/
39+
Result<std::unique_ptr<AndroidBuild>> ImgZip(const FetcherConfig& config,
40+
FileSource source);
41+
42+
Result<std::unique_ptr<AndroidBuild>> ImgZip(const std::string& path);
43+
44+
} // namespace cuttlefish

0 commit comments

Comments
 (0)