Skip to content

Commit b521014

Browse files
committed
Remove private-implementation code from ReadableZip
Bug: 458175394
1 parent 70033dc commit b521014

File tree

2 files changed

+26
-50
lines changed

2 files changed

+26
-50
lines changed

base/cvd/cuttlefish/host/libs/zip/zip_cc.cc

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,6 @@ std::optional<ZipCompression> CompressionFromRaw(uint16_t method) {
180180

181181
} // namespace
182182

183-
struct ReadableZip::Impl {
184-
// This may not actually be writable. The Impl class is shared between
185-
// ReadableZip and WritableZip, and only reading methods will be called from
186-
// ReadableZip.
187-
Impl(ManagedZip raw, WritableZipSource source)
188-
: raw_(std::move(raw)), source_(std::move(source)) {}
189-
190-
ManagedZip raw_;
191-
WritableZipSource source_;
192-
};
193-
194183
Result<ReadableZipSource> ReadableZipSource::FromCallbacks(
195184
std::unique_ptr<ReadableZipSourceCallback> callbacks) {
196185
CF_EXPECT(callbacks.get());
@@ -424,17 +413,17 @@ Result<ReadableZip> ReadableZip::FromSource(SeekableZipSource source) {
424413

425414
WritableZipSource fake_writable_source(std::move(source.raw_));
426415

427-
return ReadableZip(std::make_unique<Impl>(std::move(zip_ret),
428-
std::move(fake_writable_source)));
416+
return ReadableZip(std::move(zip_ret), std::move(fake_writable_source));
429417
}
430418

419+
// These have to be defined in the `.cc` file to avoid linker errors because of
420+
// bazel weirdness around cmake files.
431421
ReadableZip::ReadableZip(ReadableZip&&) = default;
432422
ReadableZip::~ReadableZip() = default;
433423
ReadableZip& ReadableZip::operator=(ReadableZip&&) = default;
434424

435425
Result<uint64_t> ReadableZip::NumEntries() {
436-
CF_EXPECT(impl_.get());
437-
zip_t* raw_zip = CF_EXPECT(impl_->raw_.get());
426+
zip_t* raw_zip = CF_EXPECT(raw_.get());
438427

439428
int64_t entries = zip_get_num_entries(raw_zip, 0);
440429
CF_EXPECT_GE(entries, 0, ZipErrorString(raw_zip));
@@ -443,8 +432,7 @@ Result<uint64_t> ReadableZip::NumEntries() {
443432
}
444433

445434
Result<std::string> ReadableZip::EntryName(uint64_t index) {
446-
CF_EXPECT(impl_.get());
447-
zip_t* raw_zip = CF_EXPECT(impl_->raw_.get());
435+
zip_t* raw_zip = CF_EXPECT(raw_.get());
448436

449437
const char* name_cstr = zip_get_name(raw_zip, index, 0);
450438
CF_EXPECT_NE(name_cstr, nullptr, ZipErrorString(raw_zip));
@@ -453,8 +441,7 @@ Result<std::string> ReadableZip::EntryName(uint64_t index) {
453441
}
454442

455443
Result<uint32_t> ReadableZip::EntryUnixAttributes(uint64_t index) {
456-
CF_EXPECT(impl_.get());
457-
zip_t* raw_zip = CF_EXPECT(impl_->raw_.get());
444+
zip_t* raw_zip = CF_EXPECT(raw_.get());
458445

459446
uint8_t opsys;
460447
uint32_t attributes;
@@ -467,8 +454,7 @@ Result<uint32_t> ReadableZip::EntryUnixAttributes(uint64_t index) {
467454
}
468455

469456
Result<SeekableZipSource> ReadableZip::GetFile(const std::string& name) {
470-
CF_EXPECT(impl_.get());
471-
zip_t* raw_zip = CF_EXPECT(impl_->raw_.get());
457+
zip_t* raw_zip = CF_EXPECT(raw_.get());
472458

473459
int64_t index = zip_name_locate(raw_zip, name.c_str(), 0);
474460
CF_EXPECT_GE(index, 0, ZipErrorString(raw_zip));
@@ -477,8 +463,7 @@ Result<SeekableZipSource> ReadableZip::GetFile(const std::string& name) {
477463
}
478464

479465
Result<SeekableZipSource> ReadableZip::GetFile(uint64_t index) {
480-
CF_EXPECT(impl_.get());
481-
zip_t* raw_zip = CF_EXPECT(impl_->raw_.get());
466+
zip_t* raw_zip = CF_EXPECT(raw_.get());
482467

483468
ManagedZipError error = NewZipError();
484469
ManagedZipSource raw_source(zip_source_zip_file_create(
@@ -489,7 +474,8 @@ Result<SeekableZipSource> ReadableZip::GetFile(uint64_t index) {
489474
return SeekableZipSource(std::move(raw_source));
490475
}
491476

492-
ReadableZip::ReadableZip(std::unique_ptr<Impl> impl) : impl_(std::move(impl)) {}
477+
ReadableZip::ReadableZip(ManagedZip raw, WritableZipSource source)
478+
: raw_(std::move(raw)), source_(std::move(source)) {}
493479

494480
Result<WritableZip> WritableZip::FromSource(
495481
WritableZipSource source, WritableZip::OpenBehavior open_behavior) {
@@ -519,18 +505,12 @@ Result<WritableZip> WritableZip::FromSource(WritableZipSource source,
519505
return CF_ERR(ZipErrorString(error.get()));
520506
}
521507

522-
return WritableZip(
523-
std::make_unique<Impl>(std::move(zip_ret), std::move(source)));
508+
return WritableZip(std::move(zip_ret), std::move(source));
524509
}
525510

526-
WritableZip::WritableZip(WritableZip&&) = default;
527-
WritableZip::~WritableZip() = default;
528-
WritableZip& WritableZip::operator=(WritableZip&&) = default;
529-
530511
Result<void> WritableZip::AddFile(const std::string& name,
531512
ReadableZipSource source) {
532-
CF_EXPECT(impl_.get());
533-
zip_t* raw_zip = CF_EXPECT(impl_->raw_.get());
513+
zip_t* raw_zip = CF_EXPECT(raw_.get());
534514

535515
zip_source_t* raw_source = CF_EXPECT(source.raw_.get());
536516

@@ -543,27 +523,24 @@ Result<void> WritableZip::AddFile(const std::string& name,
543523
}
544524

545525
Result<void> WritableZip::Finalize(WritableZip zip_cc) {
546-
CF_EXPECT(zip_cc.impl_.get());
547-
zip_t* raw_zip = CF_EXPECT(zip_cc.impl_->raw_.get());
526+
zip_t* raw_zip = CF_EXPECT(zip_cc.raw_.get());
548527

549528
CF_EXPECT_EQ(zip_close(raw_zip), 0, ZipErrorString(raw_zip));
550529

551-
zip_cc.impl_->raw_.release(); // Deleted by zip_close
530+
zip_cc.raw_.release(); // Deleted by zip_close
552531

553532
return {};
554533
}
555534

556535
Result<WritableZipSource> WritableZipSource::FromZip(WritableZip zip_cc) {
557-
CF_EXPECT(zip_cc.impl_.get());
558-
559-
WritableZipSource source = std::move(zip_cc.impl_->source_);
536+
WritableZipSource source = std::move(zip_cc.source_);
560537

561538
CF_EXPECT(WritableZip::Finalize(std::move(zip_cc)));
562539

563540
return source;
564541
}
565542

566-
WritableZip::WritableZip(std::unique_ptr<Impl> impl)
567-
: ReadableZip(std::move(impl)) {}
543+
WritableZip::WritableZip(ManagedZip raw, WritableZipSource source)
544+
: ReadableZip(std::move(raw), std::move(source)) {}
568545

569546
} // namespace cuttlefish

base/cvd/cuttlefish/host/libs/zip/zip_cc.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,11 @@ class ReadableZip {
228228
Result<SeekableZipSource> GetFile(const std::string& name);
229229
Result<SeekableZipSource> GetFile(uint64_t index);
230230

231-
private:
232-
struct Impl; // For pimpl: to avoid exposing libzip headers
233-
234-
ReadableZip(std::unique_ptr<Impl>);
231+
protected:
232+
ReadableZip(ManagedZip, WritableZipSource);
235233

236-
std::unique_ptr<Impl> impl_;
234+
ManagedZip raw_;
235+
WritableZipSource source_;
237236
};
238237

239238
class WritableZip : public ReadableZip {
@@ -245,9 +244,9 @@ class WritableZip : public ReadableZip {
245244
static Result<WritableZip> FromSource(
246245
WritableZipSource, OpenBehavior open_behavior = OpenBehavior::Truncate);
247246

248-
WritableZip(WritableZip&&);
249-
~WritableZip() override;
250-
WritableZip& operator=(WritableZip&&);
247+
WritableZip(WritableZip&&) = default;
248+
~WritableZip() override = default;
249+
WritableZip& operator=(WritableZip&&) = default;
251250

252251
/* Mutates the archive to add a file. Reading the contents of the sources that
253252
* are added is deferred until `Finalize` time. */
@@ -257,10 +256,10 @@ class WritableZip : public ReadableZip {
257256
* and does the archive encoding. */
258257
static Result<void> Finalize(WritableZip);
259258

260-
private:
259+
protected:
261260
static Result<WritableZip> FromSource(WritableZipSource, int flags);
262261

263-
WritableZip(std::unique_ptr<Impl>);
262+
WritableZip(ManagedZip, WritableZipSource);
264263
};
265264

266265
} // namespace cuttlefish

0 commit comments

Comments
 (0)