Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion base/cvd/cuttlefish/host/libs/image_aggregator/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@protobuf//bazel:cc_proto_library.bzl", "cc_proto_library")
load("@protobuf//bazel:proto_library.bzl", "proto_library")
load("//cuttlefish/bazel:rules.bzl", "COPTS", "cf_cc_library")
load("//cuttlefish/bazel:rules.bzl", "COPTS", "cf_cc_library", "cf_cc_test")

package(
default_visibility = ["//:android_cuttlefish"],
Expand Down Expand Up @@ -33,6 +33,40 @@ cf_cc_library(
],
)

cf_cc_library(
name = "composite_io",
srcs = ["composite_io.cc"],
hdrs = ["composite_io.h"],
deps = [
"//cuttlefish/host/libs/image_aggregator:cdisk_spec_cc_proto",
"//cuttlefish/host/libs/image_aggregator:composite_disk",
"//cuttlefish/io",
"//cuttlefish/io:fake_seek",
"//cuttlefish/io:filesystem",
"//cuttlefish/result:expect",
"//cuttlefish/result:result_type",
],
)

cf_cc_test(
name = "composite_io_test",
srcs = ["composite_io_test.cc"],
deps = [
"//cuttlefish/host/libs/image_aggregator:cdisk_spec_cc_proto",
"//cuttlefish/host/libs/image_aggregator:composite_disk",
"//cuttlefish/host/libs/image_aggregator:composite_io",
"//cuttlefish/io",
"//cuttlefish/io:filesystem",
"//cuttlefish/io:in_memory",
"//cuttlefish/io:string",
"//cuttlefish/result:result_matchers",
"//cuttlefish/result:result_type",
"@abseil-cpp//absl/strings",
"@googletest//:gtest",
"@googletest//:gtest_main",
],
)

cf_cc_library(
name = "disk_image",
hdrs = ["disk_image.h"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace cuttlefish {
/** File representing a virtual disk made of separate component files. */
class CompositeDiskImage : public DiskImage {
public:
CompositeDiskImage(CompositeDisk);

static Result<CompositeDiskImage> OpenExisting(Reader&);
static Result<CompositeDiskImage> OpenExisting(const std::string& path);

Expand All @@ -51,7 +53,6 @@ class CompositeDiskImage : public DiskImage {
}

private:
CompositeDiskImage(CompositeDisk);
CompositeDisk cdisk_;
};

Expand Down
65 changes: 65 additions & 0 deletions base/cvd/cuttlefish/host/libs/image_aggregator/composite_io.cc
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 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrap to 80-chars

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
43 changes: 43 additions & 0 deletions base/cvd/cuttlefish/host/libs/image_aggregator/composite_io.h
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)));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/io/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ cf_cc_library(
hdrs = ["string.h"],
deps = [
"//cuttlefish/io",
"//cuttlefish/io:write_exact",
"//cuttlefish/result:expect",
"//cuttlefish/result:result_type",
],
Expand Down
6 changes: 6 additions & 0 deletions base/cvd/cuttlefish/io/string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <string>

#include "cuttlefish/io/io.h"
#include "cuttlefish/io/write_exact.h"
#include "cuttlefish/result/expect.h"
#include "cuttlefish/result/result_type.h"

Expand All @@ -37,4 +38,9 @@ Result<std::string> ReadToString(Reader& reader, size_t buffer_size) {
return out.str();
}

Result<void> WriteString(Writer& writer, std::string_view str) {
CF_EXPECT(WriteExact(writer, str.data(), str.size()));
return {};
}

} // namespace cuttlefish
4 changes: 3 additions & 1 deletion base/cvd/cuttlefish/io/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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
Loading