-
Notifications
You must be signed in to change notification settings - Fork 204
Create an IO type that can read composite disk files #2304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright (C) 2026 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/image_aggregator/composite_io.h" | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| #include "cuttlefish/host/libs/image_aggregator/composite_disk.h" | ||
| #include "cuttlefish/io/fake_seek.h" | ||
| #include "cuttlefish/io/filesystem.h" | ||
| #include "cuttlefish/result/expect.h" | ||
| #include "cuttlefish/result/result_type.h" | ||
|
|
||
| namespace cuttlefish { | ||
|
|
||
| /* static */ Result<CompositeDiskReaderIo> CompositeDiskReaderIo::Create( | ||
| CompositeDiskImage composite, ReadFilesystem& filesystem) { | ||
| std::map<uint64_t, std::unique_ptr<ReaderSeeker>> offset_to_file; | ||
| for (const ComponentDisk& member : composite.GetCompositeDisk().component_disks()) { | ||
| std::unique_ptr<ReaderSeeker> file = | ||
| CF_EXPECT(filesystem.OpenReadOnly(member.file_path())); | ||
| CF_EXPECT(file.get()); | ||
| offset_to_file[member.offset()] = std::move(file); | ||
| } | ||
| CF_EXPECT_EQ(offset_to_file.count(0), 1, "No initial member"); | ||
| return CompositeDiskReaderIo(std::move(composite), std::move(offset_to_file)); | ||
| } | ||
|
|
||
| CompositeDiskReaderIo::CompositeDiskReaderIo( | ||
| CompositeDiskImage composite, std::map<uint64_t, std::unique_ptr<ReaderSeeker>> offset_to_file) | ||
| : ReaderFakeSeeker(composite.GetCompositeDisk().length()), | ||
| composite_(std::move(composite)), | ||
| offset_to_file_(std::move(offset_to_file)) {} | ||
|
|
||
| Result<uint64_t> CompositeDiskReaderIo::PRead(void* buf, uint64_t count, uint64_t offset) const { | ||
| const uint64_t full_length = composite_.GetCompositeDisk().length(); | ||
| auto it = offset_to_file_.upper_bound(offset); | ||
| uint64_t read_end = it == offset_to_file_.end() ? full_length : it->first; | ||
| CF_EXPECT(it != offset_to_file_.begin()); | ||
| it--; | ||
| CF_EXPECT(it != offset_to_file_.end()); | ||
| CF_EXPECT_GE(offset, it->first); | ||
| if (read_end < offset) { | ||
| return 0; | ||
| } | ||
| if (offset + count > read_end) { | ||
| count = read_end - offset; | ||
| } | ||
| CF_EXPECT(it->second.get()); | ||
| return CF_EXPECT(it->second->PRead(buf, count, offset - it->first)); | ||
| } | ||
|
|
||
| } // namespace cuttlefish | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Copyright (C) 2019 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. | ||
| */ | ||
| #pragma once | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| #include "cuttlefish/host/libs/image_aggregator/composite_disk.h" | ||
| #include "cuttlefish/io/fake_seek.h" | ||
| #include "cuttlefish/io/filesystem.h" | ||
| #include "cuttlefish/result/result_type.h" | ||
|
|
||
| namespace cuttlefish { | ||
|
|
||
| class CompositeDiskReaderIo : public ReaderFakeSeeker { | ||
| public: | ||
| static Result<CompositeDiskReaderIo> Create(CompositeDiskImage, ReadFilesystem&); | ||
|
|
||
| CompositeDiskReaderIo(CompositeDiskReaderIo&&) = default; | ||
| ~CompositeDiskReaderIo() = default; | ||
| CompositeDiskReaderIo& operator=(CompositeDiskReaderIo&&) = default; | ||
|
|
||
| Result<uint64_t> PRead(void* buf, uint64_t count, uint64_t offset) const override; | ||
| private: | ||
| CompositeDiskReaderIo(CompositeDiskImage, std::map<uint64_t, std::unique_ptr<ReaderSeeker>>); | ||
|
|
||
| CompositeDiskImage composite_; | ||
| std::map<uint64_t, std::unique_ptr<ReaderSeeker>> offset_to_file_; | ||
| }; | ||
|
|
||
| } // namespace cuttlefish |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // | ||
| // Copyright (C) 2026 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/image_aggregator/composite_io.h" | ||
|
|
||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| #include "absl/strings/str_cat.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| #include "cuttlefish/io/io.h" | ||
| #include "cuttlefish/host/libs/image_aggregator/composite_disk.h" | ||
| #include "cuttlefish/io/filesystem.h" | ||
| #include "cuttlefish/io/in_memory.h" | ||
| #include "cuttlefish/io/string.h" | ||
| #include "cuttlefish/result/result_matchers.h" | ||
| #include "cuttlefish/result/result_type.h" | ||
|
|
||
| namespace cuttlefish { | ||
| namespace { | ||
|
|
||
| TEST(CompositeIoTest, SeekValue) { | ||
| std::unique_ptr<ReadWriteFilesystem> fs = InMemoryFilesystem(); | ||
| ASSERT_NE(fs.get(), nullptr); | ||
|
|
||
| static constexpr std::string_view kAPath = "a"; | ||
| static constexpr std::string_view kAContent = "hello "; | ||
| Result<std::unique_ptr<ReaderWriterSeeker>> file_a = fs->CreateFile(kAPath); | ||
| ASSERT_THAT(file_a, IsOk()); | ||
| ASSERT_NE(file_a->get(), nullptr); | ||
| ASSERT_THAT(WriteString(**file_a, kAContent), IsOk()); | ||
|
|
||
| static constexpr std::string_view kBPath = "b"; | ||
| static constexpr std::string_view kBContent = "world"; | ||
| Result<std::unique_ptr<ReaderWriterSeeker>> file_b = fs->CreateFile(kBPath); | ||
| ASSERT_THAT(file_b, IsOk()); | ||
| ASSERT_NE(file_b->get(), nullptr); | ||
| ASSERT_THAT(WriteString(**file_b, kBContent), IsOk()); | ||
|
|
||
| CompositeDisk disk; | ||
| disk.set_length(kAContent.size() + kBContent.size()); | ||
|
|
||
| ComponentDisk& member_a = *disk.add_component_disks(); | ||
| member_a.set_file_path(kAPath); | ||
| member_a.set_offset(0); | ||
|
|
||
| ComponentDisk& member_b = *disk.add_component_disks(); | ||
| member_b.set_file_path(kBPath); | ||
| member_b.set_offset(kAContent.size()); | ||
|
|
||
| CompositeDiskImage image(std::move(disk)); | ||
|
|
||
| Result<CompositeDiskReaderIo> io = | ||
| CompositeDiskReaderIo::Create(std::move(image), *fs); | ||
| ASSERT_THAT(io, IsOk()); | ||
|
|
||
| ASSERT_THAT(ReadToString(*io), | ||
| IsOkAndValue(absl::StrCat(kAContent, kBContent))); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create a string variable at around l47 somewhere that has the absl::StrCat and call it expected_content instead. This will make the expectation pop out a little more when reading. |
||
| } | ||
|
|
||
| } // namespace | ||
| } // namespace cuttlefish | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,15 +15,17 @@ | |
|
|
||
| #pragma once | ||
|
|
||
| #include <stdint.h> | ||
| #include <stddef.h> | ||
|
|
||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| #include "cuttlefish/io/io.h" | ||
| #include "cuttlefish/result/result_type.h" | ||
|
|
||
| namespace cuttlefish { | ||
|
|
||
| Result<std::string> ReadToString(Reader&, size_t buffer_size = 1 << 16); | ||
| Result<void> WriteString(Writer&, std::string_view); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing to do for now here, and I think the style guide is a little quiet on the matter, but we should consider whether we want to standardize on including or omitting parameter names in header files. We're already inconsistent. There however seems to be a lean in existing code for named parameters, though.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do have a linter rule enabled validating that parameter names are the same in the source file and header file when they are present, if that convinces you either way. |
||
|
|
||
| } // namespace cuttlefish | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrap to 80-chars