Skip to content

Commit cb8b209

Browse files
committed
Create an IO type for reading composite disk images
Bug: b/495556334
1 parent cc4142f commit cb8b209

3 files changed

Lines changed: 123 additions & 0 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ cf_cc_library(
3333
],
3434
)
3535

36+
cf_cc_library(
37+
name = "composite_io",
38+
srcs = ["composite_io.cc"],
39+
hdrs = ["composite_io.h"],
40+
deps = [
41+
"//cuttlefish/host/libs/image_aggregator:cdisk_spec_cc_proto",
42+
"//cuttlefish/host/libs/image_aggregator:composite_disk",
43+
"//cuttlefish/io",
44+
"//cuttlefish/io:fake_seek",
45+
"//cuttlefish/io:filesystem",
46+
"//cuttlefish/result:expect",
47+
"//cuttlefish/result:result_type",
48+
],
49+
)
50+
3651
cf_cc_library(
3752
name = "disk_image",
3853
hdrs = ["disk_image.h"],
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (C) 2026 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/image_aggregator/composite_io.h"
17+
18+
#include <stdint.h>
19+
20+
#include "cuttlefish/host/libs/image_aggregator/composite_disk.h"
21+
#include "cuttlefish/io/fake_seek.h"
22+
#include "cuttlefish/io/filesystem.h"
23+
#include "cuttlefish/result/expect.h"
24+
#include "cuttlefish/result/result_type.h"
25+
26+
namespace cuttlefish {
27+
28+
/* static */ Result<CompositeDiskReaderIo> CompositeDiskReaderIo::Create(
29+
CompositeDiskImage composite, ReadFilesystem& filesystem) {
30+
std::map<uint64_t, std::unique_ptr<ReaderSeeker>> offset_to_file;
31+
for (const ComponentDisk& member : composite.GetCompositeDisk().component_disks()) {
32+
std::unique_ptr<ReaderSeeker> file =
33+
CF_EXPECT(filesystem.OpenReadOnly(member.file_path()));
34+
CF_EXPECT(file.get());
35+
offset_to_file[member.offset()] = std::move(file);
36+
}
37+
CF_EXPECT_EQ(offset_to_file.count(0), 1, "No initial member");
38+
return CompositeDiskReaderIo(std::move(composite), std::move(offset_to_file));
39+
}
40+
41+
CompositeDiskReaderIo::CompositeDiskReaderIo(
42+
CompositeDiskImage composite, std::map<uint64_t, std::unique_ptr<ReaderSeeker>> offset_to_file)
43+
: ReaderFakeSeeker(composite.GetCompositeDisk().length()),
44+
composite_(std::move(composite)),
45+
offset_to_file_(std::move(offset_to_file)) {}
46+
47+
Result<uint64_t> CompositeDiskReaderIo::PRead(void* buf, uint64_t count, uint64_t offset) const {
48+
const uint64_t full_length = composite_.GetCompositeDisk().length();
49+
auto it = offset_to_file_.upper_bound(offset);
50+
uint64_t read_end = it == offset_to_file_.end() ? full_length : it->first;
51+
CF_EXPECT(it != offset_to_file_.begin());
52+
it--;
53+
CF_EXPECT(it != offset_to_file_.end());
54+
CF_EXPECT_GE(offset, it->first);
55+
if (read_end < offset) {
56+
return 0;
57+
}
58+
if (offset + count > read_end) {
59+
count = read_end - offset;
60+
}
61+
CF_EXPECT(it->second.get());
62+
return CF_EXPECT(it->second->PRead(buf, count, offset - it->first));
63+
}
64+
65+
} // namespace cuttlefish
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) 2019 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 <stdint.h>
19+
20+
#include "cuttlefish/host/libs/image_aggregator/composite_disk.h"
21+
#include "cuttlefish/io/fake_seek.h"
22+
#include "cuttlefish/io/filesystem.h"
23+
#include "cuttlefish/result/result_type.h"
24+
25+
namespace cuttlefish {
26+
27+
class CompositeDiskReaderIo : public ReaderFakeSeeker {
28+
public:
29+
static Result<CompositeDiskReaderIo> Create(CompositeDiskImage, ReadFilesystem&);
30+
31+
CompositeDiskReaderIo(CompositeDiskReaderIo&&) = default;
32+
~CompositeDiskReaderIo() = default;
33+
CompositeDiskReaderIo& operator=(CompositeDiskReaderIo&&) = default;
34+
35+
Result<uint64_t> PRead(void* buf, uint64_t count, uint64_t offset) const override;
36+
private:
37+
CompositeDiskReaderIo(CompositeDiskImage, std::map<uint64_t, std::unique_ptr<ReaderSeeker>>);
38+
39+
CompositeDiskImage composite_;
40+
std::map<uint64_t, std::unique_ptr<ReaderSeeker>> offset_to_file_;
41+
};
42+
43+
} // namespace cuttlefish

0 commit comments

Comments
 (0)