|
| 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 |
0 commit comments