From 923248d681230186cd7adf94aeeeeea82d40eda3 Mon Sep 17 00:00:00 2001 From: "A. Cody Schuffelen" Date: Tue, 17 Nov 2026 14:07:15 -0800 Subject: [PATCH] Implement libzip sources in terms of seekers There were no implementations of readers, and the zip callbacks interface was redundant with our own IO types. Additionally, improved the buffering wrapper to always return as much data as it can, rather than dropping the buffer on any seek. --- base/cvd/cuttlefish/host/libs/zip/BUILD.bazel | 10 +- .../host/libs/zip/buffered_zip_source.cc | 142 ++++++++---------- .../host/libs/zip/buffered_zip_source.h | 4 +- .../host/libs/zip/buffered_zip_source_test.cc | 3 +- .../host/libs/zip/libzip_cc/BUILD.bazel | 9 +- .../libs/zip/libzip_cc/readable_source.cc | 82 ---------- .../host/libs/zip/libzip_cc/readable_source.h | 10 -- .../libs/zip/libzip_cc/seekable_source.cc | 104 ++++++++----- .../host/libs/zip/libzip_cc/seekable_source.h | 10 +- .../host/libs/zip/libzip_cc/source_callback.h | 41 ----- .../cuttlefish/host/libs/zip/remote_zip.cc | 60 +++----- base/cvd/cuttlefish/io/length.cc | 8 +- base/cvd/cuttlefish/io/length.h | 8 +- 13 files changed, 184 insertions(+), 307 deletions(-) delete mode 100644 base/cvd/cuttlefish/host/libs/zip/libzip_cc/source_callback.h diff --git a/base/cvd/cuttlefish/host/libs/zip/BUILD.bazel b/base/cvd/cuttlefish/host/libs/zip/BUILD.bazel index d59a7d0acbd..8e5e90253a7 100644 --- a/base/cvd/cuttlefish/host/libs/zip/BUILD.bazel +++ b/base/cvd/cuttlefish/host/libs/zip/BUILD.bazel @@ -11,11 +11,13 @@ cf_cc_library( deps = [ "//cuttlefish/host/libs/zip/libzip_cc:archive", "//cuttlefish/host/libs/zip/libzip_cc:seekable_source", - "//cuttlefish/host/libs/zip/libzip_cc:source_callback", + "//cuttlefish/io", + "//cuttlefish/io:fake_seek", "//cuttlefish/io:length", + "//cuttlefish/io:read_exact", "//cuttlefish/io:reader_seeker", + "//cuttlefish/io:visitable", "//cuttlefish/result", - "@abseil-cpp//absl/log", ], ) @@ -40,7 +42,9 @@ cf_cc_library( "//cuttlefish/host/libs/web/http_client", "//cuttlefish/host/libs/web/http_client:http_probe", "//cuttlefish/host/libs/zip/libzip_cc:seekable_source", - "//cuttlefish/host/libs/zip/libzip_cc:source_callback", + "//cuttlefish/io", + "//cuttlefish/io:fake_seek", + "//cuttlefish/io:visitable", "//cuttlefish/result", "@abseil-cpp//absl/log", "@fmt", diff --git a/base/cvd/cuttlefish/host/libs/zip/buffered_zip_source.cc b/base/cvd/cuttlefish/host/libs/zip/buffered_zip_source.cc index 6a82f36a8bc..82dfd0ec8ed 100644 --- a/base/cvd/cuttlefish/host/libs/zip/buffered_zip_source.cc +++ b/base/cvd/cuttlefish/host/libs/zip/buffered_zip_source.cc @@ -21,123 +21,111 @@ #include #include +#include +#include #include #include -#include "absl/log/log.h" - #include "cuttlefish/host/libs/zip/libzip_cc/archive.h" #include "cuttlefish/host/libs/zip/libzip_cc/seekable_source.h" -#include "cuttlefish/host/libs/zip/libzip_cc/source_callback.h" +#include "cuttlefish/io/fake_seek.h" +#include "cuttlefish/io/io.h" #include "cuttlefish/io/length.h" +#include "cuttlefish/io/read_exact.h" #include "cuttlefish/io/reader_seeker.h" +#include "cuttlefish/io/visitable.h" #include "cuttlefish/result/result.h" namespace cuttlefish { namespace { -class BufferedZipSourceCallbacks : public SeekableZipSourceCallback { +class BufferedZipSourceCallbacks : public ReaderFakeSeeker { public: static Result> Create( - std::unique_ptr data_provider, size_t buffer_size) { + std::unique_ptr data_provider, uint64_t buffer_size) { CF_EXPECT(data_provider.get()); + + uint64_t backing_size = CF_EXPECT(Length(*data_provider)); + buffer_size = std::min(buffer_size, backing_size); + std::unique_ptr callbacks( - new BufferedZipSourceCallbacks(buffer_size)); + new BufferedZipSourceCallbacks(buffer_size, backing_size)); - callbacks->size_ = CF_EXPECT(Length(*data_provider)); callbacks->data_provider_ = std::move(data_provider); return callbacks; } - bool Close() override { - offset_ = 0; - buffer_remaining_ = 0; - offset_in_buffer_ = 0; - buffer_remaining_ = 0; - - return true; + Result Visit(IoVisitor& visitor) override { + CF_EXPECT(visitor.Accept(*this)); + return {}; } - bool Open() override { - offset_ = 0; - buffer_remaining_ = 0; - offset_in_buffer_ = 0; - buffer_remaining_ = 0; - return data_provider_->SeekSet(0).has_value(); + Result PRead(void* data, uint64_t len, + uint64_t offset) const override { + BufferedZipSourceCallbacks* mut = + const_cast(this); + return CF_EXPECT(mut->PReadMut(data, len, offset)); } - int64_t Read(char* data, uint64_t len) override { - if (len > buffer_.size()) { - buffer_remaining_ = 0; - if (!data_provider_->SeekSet(offset_).has_value()) { - return false; - } - VLOG(1) << "Bypassing buffer, reading " << len; - if (Result read_len = data_provider_->Read(data, len); - read_len.has_value()) { - offset_ += *read_len; - return *read_len; - } else { - return -1; - } - } - if (buffer_remaining_ > 0) { - size_t to_read = std::min(len, buffer_remaining_); - memcpy(data, &buffer_[offset_in_buffer_], to_read); - buffer_remaining_ -= to_read; - offset_in_buffer_ += to_read; - offset_ += to_read; - return to_read; - } - uint64_t buffer_fill = buffer_.size(); - if (offset_ + buffer_fill > size_) { - buffer_fill = size_ - offset_; - } - if (buffer_fill == 0) { + + private: + explicit BufferedZipSourceCallbacks(uint64_t buffer_size, + uint64_t backing_size) + : ReaderFakeSeeker(backing_size), + buffer_(buffer_size), + backing_size_(backing_size) {} + + Result PReadMut(void* data, uint64_t len, uint64_t offset) { + if (offset >= backing_size_ || len == 0) { return 0; } - if (!data_provider_->SeekSet(offset_).has_value()) { - return -1; + len = std::min(len, backing_size_ - offset); + if (len > buffer_.size()) { + return CF_EXPECT(data_provider_->PRead(data, len, offset)); } - VLOG(1) << "Filling buffer with " << buffer_fill; - Result data_provider_read = - data_provider_->Read(buffer_.data(), buffer_fill); - if (!data_provider_read.has_value()) { - return -1; + { // If data can be read from the buffer, can share read access + std::shared_lock lock(mutex_); + if (uint64_t buf_read = ReadBufLocked(data, len, offset); buf_read > 0) { + return buf_read; + } } - buffer_remaining_ = *data_provider_read; - offset_in_buffer_ = 0; - return Read(data, len); - } - uint64_t Size() override { return size_; } - bool SetOffset(int64_t new_offset) override { - if (new_offset >= offset_ && new_offset < offset_ + buffer_remaining_) { - offset_in_buffer_ += new_offset - offset_; - buffer_remaining_ -= new_offset - offset_; - } else { - buffer_remaining_ = 0; + { // If we need to load new data, get exclusive access to fill the buffer + std::lock_guard lock(mutex_); + // Another thread may have loaded data we want between the unlock / lock + if (uint64_t buf_rd = ReadBufLocked(data, len, offset); buf_rd > 0) { + return buf_rd; + } // We need new data. + buf_begin_ = std::min(backing_size_ - buffer_.size(), offset); + has_data_ = false; // If PReadExact fails, assume buffer_ is corrupted. + CF_EXPECT(PReadExact(*data_provider_, buffer_.data(), buffer_.size(), + buf_begin_)); + has_data_ = true; + return ReadBufLocked(data, len, offset); } - offset_ = new_offset; - return true; } - int64_t Offset() override { return offset_; } - private: - explicit BufferedZipSourceCallbacks(size_t buffer_size) - : buffer_(buffer_size) {} + uint64_t ReadBufLocked(void* data, uint64_t len, uint64_t off) { + if (!has_data_ || off < buf_begin_ || off >= buf_begin_ + buffer_.size()) { + return 0; + } + uint64_t buf_offset = off - buf_begin_; + uint64_t buf_read_len = std::min(len, buffer_.size() - buf_offset); + memcpy(data, &buffer_[buf_offset], buf_read_len); + return buf_read_len; + } std::unique_ptr data_provider_; + std::shared_mutex mutex_; + bool has_data_ = false; std::vector buffer_; - uint64_t size_ = 0; - int64_t offset_ = 0; - size_t offset_in_buffer_ = 0; - size_t buffer_remaining_ = 0; + uint64_t backing_size_ = 0; + uint64_t buf_begin_ = 0; }; } // namespace Result BufferZipSource( - std::unique_ptr data_provider, size_t buffer_size) { + std::unique_ptr data_provider, uint64_t buffer_size) { std::unique_ptr callbacks = CF_EXPECT(BufferedZipSourceCallbacks::Create(std::move(data_provider), buffer_size)); diff --git a/base/cvd/cuttlefish/host/libs/zip/buffered_zip_source.h b/base/cvd/cuttlefish/host/libs/zip/buffered_zip_source.h index 30c3d1df19b..58631b5f79d 100644 --- a/base/cvd/cuttlefish/host/libs/zip/buffered_zip_source.h +++ b/base/cvd/cuttlefish/host/libs/zip/buffered_zip_source.h @@ -15,7 +15,7 @@ #pragma once -#include +#include #include @@ -27,7 +27,7 @@ namespace cuttlefish { Result BufferZipSource(std::unique_ptr, - size_t buffer_size); + uint64_t buffer_size); Result BufferAndOpenZip(std::unique_ptr); diff --git a/base/cvd/cuttlefish/host/libs/zip/buffered_zip_source_test.cc b/base/cvd/cuttlefish/host/libs/zip/buffered_zip_source_test.cc index d03f88feb98..b40ac5efc37 100644 --- a/base/cvd/cuttlefish/host/libs/zip/buffered_zip_source_test.cc +++ b/base/cvd/cuttlefish/host/libs/zip/buffered_zip_source_test.cc @@ -16,6 +16,7 @@ #include "cuttlefish/host/libs/zip/buffered_zip_source.h" #include +#include #include #include @@ -57,7 +58,7 @@ TEST(BufferedZipSourceTest, ManySmallReads) { size_t read_offset = 0; while (true) { char* ptr = &data_out[read_offset]; - size_t amount_read = CF_ASSERT(reader.Read(ptr, kPrimeReadSize)); + uint64_t amount_read = CF_ASSERT(reader.Read(ptr, kPrimeReadSize)); if (amount_read == 0) { break; } diff --git a/base/cvd/cuttlefish/host/libs/zip/libzip_cc/BUILD.bazel b/base/cvd/cuttlefish/host/libs/zip/libzip_cc/BUILD.bazel index 8f79ac139b1..578b1a63b5e 100644 --- a/base/cvd/cuttlefish/host/libs/zip/libzip_cc/BUILD.bazel +++ b/base/cvd/cuttlefish/host/libs/zip/libzip_cc/BUILD.bazel @@ -56,7 +56,6 @@ cf_cc_library( deps = [ "//cuttlefish/host/libs/zip/libzip_cc:error", "//cuttlefish/host/libs/zip/libzip_cc:managed", - "//cuttlefish/host/libs/zip/libzip_cc:source_callback", "//cuttlefish/host/libs/zip/libzip_cc:stat", "//cuttlefish/io", "//cuttlefish/result", @@ -74,12 +73,13 @@ cf_cc_library( "//cuttlefish/host/libs/zip/libzip_cc:error", "//cuttlefish/host/libs/zip/libzip_cc:managed", "//cuttlefish/host/libs/zip/libzip_cc:readable_source", - "//cuttlefish/host/libs/zip/libzip_cc:source_callback", "//cuttlefish/io", "//cuttlefish/io:fake_pread_pwrite", + "//cuttlefish/io:length", "//cuttlefish/io:reader_seeker", "//cuttlefish/io:visitable", "//cuttlefish/result", + "@abseil-cpp//absl/base:no_destructor", "@libzip", ], ) @@ -89,11 +89,6 @@ cf_cc_library( hdrs = ["stat.h"], ) -cf_cc_library( - name = "source_callback", - hdrs = ["source_callback.h"], -) - cf_cc_library( name = "writable_source", srcs = ["writable_source.cc"], diff --git a/base/cvd/cuttlefish/host/libs/zip/libzip_cc/readable_source.cc b/base/cvd/cuttlefish/host/libs/zip/libzip_cc/readable_source.cc index c1cdaafe8c0..7198a7c157b 100644 --- a/base/cvd/cuttlefish/host/libs/zip/libzip_cc/readable_source.cc +++ b/base/cvd/cuttlefish/host/libs/zip/libzip_cc/readable_source.cc @@ -19,7 +19,6 @@ #include #include -#include #include #include #include @@ -29,7 +28,6 @@ #include "cuttlefish/host/libs/zip/libzip_cc/error.h" #include "cuttlefish/host/libs/zip/libzip_cc/managed.h" -#include "cuttlefish/host/libs/zip/libzip_cc/source_callback.h" #include "cuttlefish/host/libs/zip/libzip_cc/stat.h" #include "cuttlefish/io/io.h" #include "cuttlefish/result/result.h" @@ -37,27 +35,6 @@ namespace cuttlefish { namespace { -struct ReadableCallbackSource { - ReadableCallbackSource(std::unique_ptr callbacks) - : callbacks_(std::move(callbacks)), error_(NewZipError()) {} - - std::unique_ptr callbacks_; - ManagedZipError error_; -}; - -// https://libzip.org/documentation/zip_source_function.html -int64_t ReadableZipSourceCallbackFn(void* userdata, void* data, uint64_t len, - zip_source_cmd_t cmd) { - ReadableCallbackSource* source = - reinterpret_cast(userdata); - if (cmd == ZIP_SOURCE_FREE) { - delete source; - return 0; - } - return HandleCallback(*source->callbacks_, source->error_.get(), data, len, - cmd); -} - std::optional CompressionFromRaw(uint16_t method) { switch (method) { case ZIP_CM_DEFAULT: @@ -79,65 +56,6 @@ std::optional CompressionFromRaw(uint16_t method) { } // namespace -int64_t HandleCallback(ReadableZipSourceCallback& callbacks, zip_error_t* error, - void* data, uint64_t len, zip_source_cmd_t cmd) { - switch (cmd) { - case ZIP_SOURCE_CLOSE: { - bool callback_res = callbacks.Close(); - if (!callback_res) { - zip_error_set(error, ZIP_ER_CLOSE, errno); - } - return callback_res ? 0 : -1; - } - case ZIP_SOURCE_ERROR: - return zip_error_to_data(error, data, len); - case ZIP_SOURCE_OPEN: { - bool callback_res = callbacks.Open(); - if (!callback_res) { - zip_error_set(error, ZIP_ER_OPEN, errno); - } - return callback_res ? 0 : -1; - } - case ZIP_SOURCE_READ: { - int64_t callback_res = callbacks.Read(reinterpret_cast(data), len); - if (callback_res < 0) { - zip_error_set(error, ZIP_ER_READ, errno); - } - return callback_res; - } - case ZIP_SOURCE_STAT: { - zip_stat_t* stat_out = ZIP_SOURCE_GET_ARGS(zip_stat_t, data, len, error); - zip_stat_init(stat_out); - stat_out->valid = ZIP_STAT_SIZE; - stat_out->size = callbacks.Size(); - return 0; - } - case ZIP_SOURCE_SUPPORTS: - return zip_source_make_command_bitmap( - ZIP_SOURCE_CLOSE, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_OPEN, - ZIP_SOURCE_READ, ZIP_SOURCE_STAT, ZIP_SOURCE_SUPPORTS, -1); - default: - zip_error_set(error, ZIP_ER_OPNOTSUPP, EINVAL); - return -1; - } -} - -Result ReadableZipSource::FromCallbacks( - std::unique_ptr callbacks) { - CF_EXPECT(callbacks.get()); - - std::unique_ptr wrapped_source = - std::make_unique(std::move(callbacks)); - - ManagedZipError error = NewZipError(); - ManagedZipSource source(zip_source_function_create( - ReadableZipSourceCallbackFn, wrapped_source.release(), error.get())); - - CF_EXPECT(source.get(), ZipErrorString(error.get())); - - return ReadableZipSource(std::move(source)); -} - // These have to be defined in the `.cc` file to avoid linker errors because of // bazel weirdness around cmake files. ReadableZipSource::ReadableZipSource(ReadableZipSource&&) = default; diff --git a/base/cvd/cuttlefish/host/libs/zip/libzip_cc/readable_source.h b/base/cvd/cuttlefish/host/libs/zip/libzip_cc/readable_source.h index 9d4540c4f5b..3af2d7057e6 100644 --- a/base/cvd/cuttlefish/host/libs/zip/libzip_cc/readable_source.h +++ b/base/cvd/cuttlefish/host/libs/zip/libzip_cc/readable_source.h @@ -18,22 +18,15 @@ #include #include -#include #include -#include "zip.h" - #include "cuttlefish/host/libs/zip/libzip_cc/managed.h" -#include "cuttlefish/host/libs/zip/libzip_cc/source_callback.h" #include "cuttlefish/host/libs/zip/libzip_cc/stat.h" #include "cuttlefish/io/io.h" #include "cuttlefish/result/result.h" namespace cuttlefish { -int64_t HandleCallback(ReadableZipSourceCallback& callbacks, zip_error_t* error, - void* data, uint64_t len, zip_source_cmd_t cmd); - class ReadableZipSource { public: friend class ReadableZip; @@ -42,9 +35,6 @@ class ReadableZipSource { friend class SeekingZipSourceReader; friend class ZipSourceWriter; - static Result FromCallbacks( - std::unique_ptr); - // Can be safely called with a subclass type. ReadableZipSource(ReadableZipSource&&); virtual ~ReadableZipSource(); diff --git a/base/cvd/cuttlefish/host/libs/zip/libzip_cc/seekable_source.cc b/base/cvd/cuttlefish/host/libs/zip/libzip_cc/seekable_source.cc index 2ae0956b694..8d3958a6cc0 100644 --- a/base/cvd/cuttlefish/host/libs/zip/libzip_cc/seekable_source.cc +++ b/base/cvd/cuttlefish/host/libs/zip/libzip_cc/seekable_source.cc @@ -20,18 +20,20 @@ #include #include +#include #include #include #include +#include "absl/base/no_destructor.h" #include "zip.h" #include "cuttlefish/host/libs/zip/libzip_cc/error.h" #include "cuttlefish/host/libs/zip/libzip_cc/managed.h" #include "cuttlefish/host/libs/zip/libzip_cc/readable_source.h" -#include "cuttlefish/host/libs/zip/libzip_cc/source_callback.h" #include "cuttlefish/io/fake_pread_pwrite.h" #include "cuttlefish/io/io.h" +#include "cuttlefish/io/length.h" #include "cuttlefish/io/reader_seeker.h" #include "cuttlefish/io/visitable.h" #include "cuttlefish/result/result.h" @@ -39,24 +41,39 @@ namespace cuttlefish { namespace { -struct SeekableCallbackSource { - SeekableCallbackSource(std::unique_ptr callbacks) +struct CallbackUserdata { + CallbackUserdata(std::unique_ptr callbacks) : callbacks_(std::move(callbacks)), error_(NewZipError()) {} - std::unique_ptr callbacks_; + std::unique_ptr callbacks_; ManagedZipError error_; }; -int64_t HandleCallback(SeekableZipSourceCallback& callbacks, zip_error_t* error, - void* data, uint64_t len, zip_source_cmd_t cmd) { +Result HandleCallback(ReaderSeeker& callbacks, zip_error_t* error, + void* data, uint64_t len, + zip_source_cmd_t cmd) { switch (cmd) { + case ZIP_SOURCE_CLOSE: + case ZIP_SOURCE_OPEN: + CF_EXPECT(callbacks.SeekSet(0)); + return 0; + case ZIP_SOURCE_READ: + return CF_EXPECT(callbacks.Read(reinterpret_cast(data), len)); + case ZIP_SOURCE_STAT: { + zip_stat_t* stat_out = ZIP_SOURCE_GET_ARGS(zip_stat_t, data, len, error); + CF_EXPECTV(stat_out != nullptr, EINVAL, "invalid zip_stat_t"); + zip_stat_init(stat_out); + stat_out->valid = ZIP_STAT_SIZE; + stat_out->size = CF_EXPECT(Length(callbacks)); + return sizeof(zip_stat_t); + } case ZIP_SOURCE_SEEK: { - int64_t new_offset = zip_source_seek_compute_offset( - callbacks.Offset(), callbacks.Size(), data, len, error); - if (!callbacks.SetOffset(new_offset)) { - zip_error_set(error, ZIP_ER_SEEK, errno); - return -1; - } + int64_t current_offset = CF_EXPECT(callbacks.SeekCur(0)); + uint64_t size = CF_EXPECT(Length(callbacks)); + int64_t new_offset = zip_source_seek_compute_offset(current_offset, size, + data, len, error); + CF_EXPECTV(new_offset >= 0, EINVAL, "invalid offset"); + CF_EXPECT(callbacks.SeekSet(new_offset)); return 0; } case ZIP_SOURCE_SUPPORTS: @@ -65,24 +82,43 @@ int64_t HandleCallback(SeekableZipSourceCallback& callbacks, zip_error_t* error, ZIP_SOURCE_READ, ZIP_SOURCE_SEEK, ZIP_SOURCE_STAT, ZIP_SOURCE_SUPPORTS, ZIP_SOURCE_TELL, -1); case ZIP_SOURCE_TELL: - return callbacks.Offset(); + return CF_EXPECT(callbacks.SeekCur(0)); default: - return HandleCallback(static_cast(callbacks), - error, data, len, cmd); + return CF_ERRVF(EOPNOTSUPP, "Unknown operation {}", int(cmd)); } } // https://libzip.org/documentation/zip_source_function.html int64_t SeekableZipSourceCallbackFn(void* userdata, void* data, uint64_t len, zip_source_cmd_t cmd) { - SeekableCallbackSource* source = - reinterpret_cast(userdata); + CallbackUserdata* source = reinterpret_cast(userdata); if (cmd == ZIP_SOURCE_FREE) { delete source; return 0; + } else if (cmd == ZIP_SOURCE_ERROR) { + return zip_error_to_data(source->error_.get(), data, len); + } + zip_error_set(source->error_.get(), ZIP_ER_OK, 0); + // TODO(schuffelen): Hang on to this value to propagate to wrapper functions + Result res = + HandleCallback(*source->callbacks_, source->error_.get(), data, len, cmd); + if (res.has_value()) { + return res.value(); + } else if (res.error().Value() == EOPNOTSUPP) { + zip_error_set(source->error_.get(), ZIP_ER_OPNOTSUPP, EOPNOTSUPP); + return -1; } - return HandleCallback(*source->callbacks_, source->error_.get(), data, len, - cmd); + static absl::NoDestructor> kCodeMap({ + {ZIP_SOURCE_CLOSE, ZIP_ER_CLOSE}, + {ZIP_SOURCE_OPEN, ZIP_ER_OPEN}, + {ZIP_SOURCE_READ, ZIP_ER_READ}, + {ZIP_SOURCE_SEEK, ZIP_ER_SEEK}, + {ZIP_SOURCE_TELL, ZIP_ER_TELL}, + }); + auto err_it = kCodeMap->find(cmd); + int zip_err_code = err_it == kCodeMap->end() ? ZIP_ER_INVAL : err_it->second; + zip_error_set(source->error_.get(), zip_err_code, *res.error()); + return -1; } class ZipSourceAsReaderSeekerImpl : public ReaderSeeker { @@ -125,18 +161,20 @@ class ZipSourceAsReaderSeekerImpl : public ReaderSeeker { } // namespace Result SeekableZipSource::FromCallbacks( - std::unique_ptr callbacks) { + std::unique_ptr callbacks) { CF_EXPECT(callbacks.get()); - std::unique_ptr wrapped_source = - std::make_unique(std::move(callbacks)); + std::unique_ptr userdata = + std::make_unique(std::move(callbacks)); ManagedZipError error = NewZipError(); ManagedZipSource source(zip_source_function_create( - SeekableZipSourceCallbackFn, wrapped_source.release(), error.get())); + SeekableZipSourceCallbackFn, userdata.get(), error.get())); CF_EXPECT(source.get(), ZipErrorString(error.get())); + userdata.release(); // Owned by source if zip_source_function_create succeeds + // Even though `release` is here, the `delete` path is with ZIP_SOURCE_FREE. return SeekableZipSource(std::move(source)); } @@ -151,12 +189,6 @@ Result SeekableZipSource::Reader() { SeekableZipSource::SeekableZipSource(ManagedZipSource raw) : ReadableZipSource(std::move(raw)) {} -SeekingZipSourceReader::SeekingZipSourceReader(SeekingZipSourceReader&&) = - default; -SeekingZipSourceReader::~SeekingZipSourceReader() = default; -SeekingZipSourceReader& SeekingZipSourceReader::operator=( - SeekingZipSourceReader&&) = default; - Result SeekingZipSourceReader::Visit(IoVisitor& visitor) { CF_EXPECT(visitor.Accept(static_cast(*this))); return {}; @@ -188,13 +220,17 @@ Result SeekingZipSourceReader::Seek(int64_t offset, int whence) { zip_source_t* raw_source = CF_EXPECTVF(source_->raw_.get(), EBADF, "Seek({}, {}) failed", offset, whence); - CF_EXPECTVF(zip_source_seek(raw_source, offset, whence) == 0, - ZipErrno(raw_source), "zip_source_seek(..., {}, {}) failed: {}", - offset, whence, ZipErrorString(raw_source)); + if (zip_source_seek(raw_source, offset, whence) != 0) { + return CF_ERRVF(ZipErrno(raw_source), + "zip_source_seek(..., {}, {}) failed: {}", offset, whence, + ZipErrorString(raw_source)); + } int64_t tell = zip_source_tell(raw_source); - CF_EXPECTVF(tell >= 0, ZipErrno(raw_source), - "zip_source_tell(...) failed: {}", ZipErrorString(raw_source)); + if (tell < 0) { + return CF_ERRVF(ZipErrno(raw_source), "zip_source_tell(...) failed: {}", + ZipErrorString(raw_source)); + } return tell; } diff --git a/base/cvd/cuttlefish/host/libs/zip/libzip_cc/seekable_source.h b/base/cvd/cuttlefish/host/libs/zip/libzip_cc/seekable_source.h index 3f2c88ef2ed..e8c9d4c917f 100644 --- a/base/cvd/cuttlefish/host/libs/zip/libzip_cc/seekable_source.h +++ b/base/cvd/cuttlefish/host/libs/zip/libzip_cc/seekable_source.h @@ -22,7 +22,6 @@ #include "cuttlefish/host/libs/zip/libzip_cc/managed.h" #include "cuttlefish/host/libs/zip/libzip_cc/readable_source.h" -#include "cuttlefish/host/libs/zip/libzip_cc/source_callback.h" #include "cuttlefish/io/io.h" #include "cuttlefish/io/reader_seeker.h" #include "cuttlefish/result/result.h" @@ -34,8 +33,7 @@ class SeekableZipSource : public ReadableZipSource { friend class ReadableZip; friend class WritableZipSource; - static Result FromCallbacks( - std::unique_ptr); + static Result FromCallbacks(std::unique_ptr); SeekableZipSource(SeekableZipSource&&) = default; ~SeekableZipSource() override = default; @@ -54,9 +52,9 @@ class SeekingZipSourceReader : public ZipSourceReader, public ReaderSeeker { public: friend class SeekableZipSource; - SeekingZipSourceReader(SeekingZipSourceReader&&); - ~SeekingZipSourceReader() override; - SeekingZipSourceReader& operator=(SeekingZipSourceReader&&); + SeekingZipSourceReader(SeekingZipSourceReader&&) = default; + ~SeekingZipSourceReader() override = default; + SeekingZipSourceReader& operator=(SeekingZipSourceReader&&) = default; Result Visit(IoVisitor&) override; diff --git a/base/cvd/cuttlefish/host/libs/zip/libzip_cc/source_callback.h b/base/cvd/cuttlefish/host/libs/zip/libzip_cc/source_callback.h deleted file mode 100644 index dbadb1ca932..00000000000 --- a/base/cvd/cuttlefish/host/libs/zip/libzip_cc/source_callback.h +++ /dev/null @@ -1,41 +0,0 @@ -// -// Copyright (C) 2025 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 - -namespace cuttlefish { - -class ReadableZipSourceCallback { - public: - virtual ~ReadableZipSourceCallback() = default; - - virtual bool Close() = 0; - virtual bool Open() = 0; - virtual int64_t Read(char* data, uint64_t len) = 0; - virtual uint64_t Size() = 0; -}; - -/* Callback interface to provide file data to libzip. */ -class SeekableZipSourceCallback : public ReadableZipSourceCallback { - public: - virtual ~SeekableZipSourceCallback() = default; - - virtual bool SetOffset(int64_t offset) = 0; - virtual int64_t Offset() = 0; -}; - -} // namespace cuttlefish diff --git a/base/cvd/cuttlefish/host/libs/zip/remote_zip.cc b/base/cvd/cuttlefish/host/libs/zip/remote_zip.cc index bbc0d8beed5..0abb9d356f8 100644 --- a/base/cvd/cuttlefish/host/libs/zip/remote_zip.cc +++ b/base/cvd/cuttlefish/host/libs/zip/remote_zip.cc @@ -33,81 +33,69 @@ #include "cuttlefish/host/libs/web/http_client/http_client.h" #include "cuttlefish/host/libs/web/http_client/http_probe.h" #include "cuttlefish/host/libs/zip/libzip_cc/seekable_source.h" -#include "cuttlefish/host/libs/zip/libzip_cc/source_callback.h" +#include "cuttlefish/io/fake_seek.h" +#include "cuttlefish/io/io.h" +#include "cuttlefish/io/visitable.h" #include "cuttlefish/result/result.h" namespace cuttlefish { namespace { -class RemoteZip : public SeekableZipSourceCallback { +class RemoteZip : public ReaderFakeSeeker { public: RemoteZip(HttpClient& http_client, std::string url, uint64_t size, std::vector headers) - : http_client_(http_client), + : ReaderFakeSeeker(size), + http_client_(http_client), url_(std::move(url)), size_(size), headers_(std::move(headers)) {} - bool Close() override { return true; } - bool Open() override { - offset_ = 0; - return true; + Result Visit(IoVisitor& visitor) override { + CF_EXPECT(visitor.Accept(*this)); + return {}; } - int64_t Read(char* zip_data, uint64_t zip_len) override { - if (zip_len == 0 || offset_ >= size_) { + + Result PRead(void* data, uint64_t len, + uint64_t offset) const override { + if (len == 0 || offset >= size_) { return 0; } - zip_len = std::min(zip_len, size_ - offset_); + len = std::min(len, size_ - offset); uint64_t already_read = 0; - auto cb = [&already_read, &zip_data, &zip_len]( - char* http_data, size_t http_len) -> Result { + auto cb = [&already_read, &data, &len](char* http_data, + size_t http_len) -> Result { if (http_data == nullptr) { already_read = 0; return {}; } - CF_EXPECT_LE(http_len + already_read, zip_len); - memcpy(zip_data + already_read, http_data, http_len); + CF_EXPECT_LE(http_len + already_read, len); + memcpy(reinterpret_cast(data) + already_read, http_data, http_len); already_read += http_len; return {}; }; std::vector headers = headers_; headers.push_back( - fmt::format("Range: bytes={}-{}", offset_, offset_ + zip_len - 1)); + fmt::format("Range: bytes={}-{}", offset, offset + len - 1)); VLOG(1) << "Requesting " << headers.back(); HttpRequest request = { .method = HttpMethod::kGet, .url = url_, .headers = headers, }; - Result> res = - http_client_.DownloadToCallback(request, cb); - if (!res.has_value() || !res->HttpSuccess() || already_read != zip_len) { - if (!res.has_value()) { - LOG(ERROR) << res.error(); - } else if (!res->HttpSuccess()) { - LOG(ERROR) << "HTTP code: " << res->http_code; - } else { - LOG(ERROR) << already_read << " != " << zip_len; - } - errno = EIO; - return -1; - } - offset_ += already_read; + HttpResponse res = + CF_EXPECTV(http_client_.DownloadToCallback(request, cb), EIO, + "HTTP library issue"); + CF_EXPECTVF(res.HttpSuccess(), EIO, "unexpected code: {}", res.http_code); + CF_EXPECTVF(already_read == len, EIO, "{} != {}", already_read, len); return already_read; } - bool SetOffset(int64_t offset) override { - offset_ = offset; - return true; - } - int64_t Offset() override { return offset_; } - uint64_t Size() override { return size_; } private: HttpClient& http_client_; std::string url_; - uint64_t offset_ = 0; uint64_t size_ = 0; std::vector headers_; }; diff --git a/base/cvd/cuttlefish/io/length.cc b/base/cvd/cuttlefish/io/length.cc index f91f822b2e3..15af96d41b6 100644 --- a/base/cvd/cuttlefish/io/length.cc +++ b/base/cvd/cuttlefish/io/length.cc @@ -25,22 +25,22 @@ namespace cuttlefish { -Result Length(Seeker& seeker) { +Result Length(Seeker& seeker) { uint64_t current_pos = CF_EXPECT(seeker.SeekCur(0)); uint64_t end = CF_EXPECT(seeker.SeekEnd(0)); CF_EXPECT(seeker.SeekSet(current_pos)); return end; } -Result Length(Seeker&& seeker) { +Result Length(Seeker&& seeker) { return CF_EXPECT(Length(static_cast(seeker))); } -Result Length(ReaderWriterSeeker& seeker) { +Result Length(ReaderWriterSeeker& seeker) { return CF_EXPECT(Length(static_cast(seeker))); } -Result Length(ReaderWriterSeeker&& seeker) { +Result Length(ReaderWriterSeeker&& seeker) { return CF_EXPECT(Length(static_cast(seeker))); } diff --git a/base/cvd/cuttlefish/io/length.h b/base/cvd/cuttlefish/io/length.h index c64727339ea..7e7608a71c5 100644 --- a/base/cvd/cuttlefish/io/length.h +++ b/base/cvd/cuttlefish/io/length.h @@ -23,9 +23,9 @@ namespace cuttlefish { -Result Length(Seeker&); -Result Length(Seeker&&); -Result Length(ReaderWriterSeeker&); -Result Length(ReaderWriterSeeker&&); +Result Length(Seeker&); +Result Length(Seeker&&); +Result Length(ReaderWriterSeeker&); +Result Length(ReaderWriterSeeker&&); } // namespace cuttlefish