Skip to content

Commit 70033dc

Browse files
committed
Remove private-implementation logic from the ZipSource hierarchy
Bug: b/458175394
1 parent 97cbd68 commit 70033dc

File tree

2 files changed

+61
-89
lines changed

2 files changed

+61
-89
lines changed

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

Lines changed: 33 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,18 @@
2020
#include <stdint.h>
2121
#include <stdio.h>
2222

23-
#include <zip.h>
24-
2523
#include <memory>
2624
#include <optional>
2725
#include <string>
2826
#include <utility>
2927

28+
#include "zip.h"
29+
3030
#include "cuttlefish/common/libs/utils/result.h"
3131

3232
namespace cuttlefish {
3333
namespace {
3434

35-
struct ZipDeleter {
36-
void operator()(zip_error_t* error) {
37-
zip_error_fini(error);
38-
delete error;
39-
}
40-
void operator()(zip_source_t* source) { zip_source_free(source); }
41-
void operator()(zip_t* zip_ptr) { zip_discard(zip_ptr); }
42-
};
43-
44-
using ManagedZip = std::unique_ptr<zip_t, ZipDeleter>;
45-
using ManagedZipError = std::unique_ptr<zip_error_t, ZipDeleter>;
46-
using ManagedZipSource = std::unique_ptr<zip_source_t, ZipDeleter>;
47-
4835
ManagedZipError NewZipError() {
4936
ManagedZipError error(new zip_error_t);
5037
zip_error_init(error.get());
@@ -193,12 +180,6 @@ std::optional<ZipCompression> CompressionFromRaw(uint16_t method) {
193180

194181
} // namespace
195182

196-
struct ReadableZipSource::Impl {
197-
Impl(ManagedZipSource raw) : raw_(std::move(raw)) {}
198-
199-
ManagedZipSource raw_;
200-
};
201-
202183
struct ReadableZip::Impl {
203184
// This may not actually be writable. The Impl class is shared between
204185
// ReadableZip and WritableZip, and only reading methods will be called from
@@ -223,16 +204,17 @@ Result<ReadableZipSource> ReadableZipSource::FromCallbacks(
223204

224205
CF_EXPECT(source.get(), ZipErrorString(error.get()));
225206

226-
return ReadableZipSource(std::make_unique<Impl>(std::move(source)));
207+
return ReadableZipSource(std::move(source));
227208
}
228209

210+
// These have to be defined in the `.cc` file to avoid linker errors because of
211+
// bazel weirdness around cmake files.
229212
ReadableZipSource::ReadableZipSource(ReadableZipSource&&) = default;
230213
ReadableZipSource::~ReadableZipSource() = default;
231214
ReadableZipSource& ReadableZipSource::operator=(ReadableZipSource&&) = default;
232215

233216
Result<ZipStat> ReadableZipSource::Stat() {
234-
CF_EXPECT(impl_.get());
235-
zip_source_t* raw_source = CF_EXPECT(impl_->raw_.get());
217+
zip_source_t* raw_source = CF_EXPECT(raw_.get());
236218

237219
zip_stat_t raw_stat;
238220
zip_stat_init(&raw_stat);
@@ -260,16 +242,15 @@ Result<ZipStat> ReadableZipSource::Stat() {
260242
}
261243

262244
Result<ZipSourceReader> ReadableZipSource::Reader() {
263-
CF_EXPECT(impl_.get());
264-
zip_source_t* raw_source = CF_EXPECT(impl_->raw_.get());
245+
zip_source_t* raw_source = CF_EXPECT(raw_.get());
265246

266247
CF_EXPECT_EQ(zip_source_open(raw_source), 0, ZipErrorString(raw_source));
267248

268249
return ZipSourceReader(this);
269250
}
270251

271-
ReadableZipSource::ReadableZipSource(std::unique_ptr<Impl> impl)
272-
: impl_(std::move(impl)) {}
252+
ReadableZipSource::ReadableZipSource(ManagedZipSource raw)
253+
: raw_(std::move(raw)) {}
273254

274255
Result<SeekableZipSource> SeekableZipSource::FromCallbacks(
275256
std::unique_ptr<SeekableZipSourceCallback> callbacks) {
@@ -284,25 +265,19 @@ Result<SeekableZipSource> SeekableZipSource::FromCallbacks(
284265

285266
CF_EXPECT(source.get(), ZipErrorString(error.get()));
286267

287-
return SeekableZipSource(std::make_unique<Impl>(std::move(source)));
268+
return SeekableZipSource(std::move(source));
288269
}
289270

290-
SeekableZipSource::SeekableZipSource(SeekableZipSource&&) = default;
291-
SeekableZipSource::~SeekableZipSource() = default;
292-
SeekableZipSource& SeekableZipSource::operator=(SeekableZipSource&& other) =
293-
default;
294-
295271
Result<SeekingZipSourceReader> SeekableZipSource::Reader() {
296-
CF_EXPECT(impl_.get());
297-
zip_source_t* raw_source = CF_EXPECT(impl_->raw_.get());
272+
zip_source_t* raw_source = CF_EXPECT(raw_.get());
298273

299274
CF_EXPECT_EQ(zip_source_open(raw_source), 0, ZipErrorString(raw_source));
300275

301276
return SeekingZipSourceReader(this);
302277
}
303278

304-
SeekableZipSource::SeekableZipSource(std::unique_ptr<Impl> impl)
305-
: ReadableZipSource(std::move(impl)) {}
279+
SeekableZipSource::SeekableZipSource(ManagedZipSource raw)
280+
: ReadableZipSource(std::move(raw)) {}
306281

307282
Result<WritableZipSource> WritableZipSource::BorrowData(const void* data,
308283
size_t size) {
@@ -313,7 +288,7 @@ Result<WritableZipSource> WritableZipSource::BorrowData(const void* data,
313288

314289
CF_EXPECT(source.get(), ZipErrorString(error.get()));
315290

316-
return WritableZipSource(std::make_unique<Impl>(std::move(source)));
291+
return WritableZipSource(std::move(source));
317292
}
318293

319294
Result<WritableZipSource> WritableZipSource::FromFile(const std::string& path) {
@@ -323,18 +298,14 @@ Result<WritableZipSource> WritableZipSource::FromFile(const std::string& path) {
323298

324299
CF_EXPECT(source.get(), ZipErrorString(error.get()));
325300

326-
return WritableZipSource(std::make_unique<Impl>(std::move(source)));
301+
return WritableZipSource(std::move(source));
327302
}
328303

329-
WritableZipSource::WritableZipSource(WritableZipSource&&) = default;
330-
WritableZipSource::~WritableZipSource() = default;
331-
WritableZipSource& WritableZipSource::operator=(WritableZipSource&&) = default;
332-
WritableZipSource::WritableZipSource(std::unique_ptr<Impl> impl)
333-
: SeekableZipSource(std::move(impl)) {}
304+
WritableZipSource::WritableZipSource(ManagedZipSource raw)
305+
: SeekableZipSource(std::move(raw)) {}
334306

335307
Result<ZipSourceWriter> WritableZipSource::Writer() {
336-
CF_EXPECT(impl_.get());
337-
zip_source_t* raw = CF_EXPECT(impl_->raw_.get());
308+
zip_source_t* raw = CF_EXPECT(raw_.get());
338309

339310
CF_EXPECT_EQ(zip_source_begin_write(raw), 0, ZipErrorString(raw));
340311

@@ -355,14 +326,14 @@ ZipSourceReader& ZipSourceReader::operator=(ZipSourceReader&& other) {
355326
}
356327

357328
ZipSourceReader::~ZipSourceReader() {
358-
if (source_ && source_->impl_ && source_->impl_->raw_) {
359-
zip_source_close(source_->impl_->raw_.get());
329+
if (source_ && source_->raw_) {
330+
zip_source_close(source_->raw_.get());
360331
}
361332
}
362333

363334
Result<uint64_t> ZipSourceReader::Read(void* data, uint64_t length) {
364335
CF_EXPECT_NE(source_, nullptr);
365-
zip_source_t* raw_source = CF_EXPECT(source_->impl_->raw_.get());
336+
zip_source_t* raw_source = CF_EXPECT(source_->raw_.get());
366337

367338
int64_t read_res = zip_source_read(raw_source, data, length);
368339

@@ -379,8 +350,7 @@ SeekingZipSourceReader& SeekingZipSourceReader::operator=(
379350

380351
Result<void> SeekingZipSourceReader::SeekFromStart(int64_t offset) {
381352
CF_EXPECT_NE(source_, nullptr);
382-
CF_EXPECT(source_->impl_.get());
383-
zip_source_t* raw_source = CF_EXPECT(source_->impl_->raw_.get());
353+
zip_source_t* raw_source = CF_EXPECT(source_->raw_.get());
384354

385355
CF_EXPECT_EQ(zip_source_seek(raw_source, offset, SEEK_SET), 0,
386356
ZipErrorString(raw_source));
@@ -405,16 +375,15 @@ ZipSourceWriter& ZipSourceWriter::operator=(ZipSourceWriter&& other) {
405375
}
406376

407377
ZipSourceWriter::~ZipSourceWriter() {
408-
if (source_ && source_->impl_ && source_->impl_->raw_) {
409-
zip_source_rollback_write(source_->impl_->raw_.get());
378+
if (source_ && source_->raw_) {
379+
zip_source_rollback_write(source_->raw_.get());
410380
}
411381
}
412382

413383
Result<uint64_t> ZipSourceWriter::Write(void* data, uint64_t length) {
414384
CF_EXPECT_NE(data, nullptr);
415385
CF_EXPECT_NE(source_, nullptr);
416-
CF_EXPECT(source_->impl_.get());
417-
zip_source_t* raw_source = CF_EXPECT(source_->impl_->raw_.get());
386+
zip_source_t* raw_source = CF_EXPECT(source_->raw_.get());
418387

419388
int64_t written = zip_source_write(raw_source, data, length);
420389
CF_EXPECT_GE(written, 0, ZipErrorString(raw_source));
@@ -423,8 +392,7 @@ Result<uint64_t> ZipSourceWriter::Write(void* data, uint64_t length) {
423392

424393
Result<void> ZipSourceWriter::SeekFromStart(int64_t offset) {
425394
CF_EXPECT_NE(source_, nullptr);
426-
CF_EXPECT(source_->impl_.get());
427-
zip_source_t* raw_source = CF_EXPECT(source_->impl_->raw_.get());
395+
zip_source_t* raw_source = CF_EXPECT(source_->raw_.get());
428396

429397
CF_EXPECT_EQ(zip_source_seek_write(raw_source, offset, SEEK_SET), 0,
430398
ZipErrorString(raw_source));
@@ -434,16 +402,15 @@ Result<void> ZipSourceWriter::SeekFromStart(int64_t offset) {
434402

435403
Result<void> ZipSourceWriter::Finalize(ZipSourceWriter writer) {
436404
CF_EXPECT_NE(writer.source_, nullptr);
437-
CF_EXPECT(writer.source_->impl_.get());
438-
zip_source_t* raw = CF_EXPECT(writer.source_->impl_->raw_.get());
405+
zip_source_t* raw = CF_EXPECT(writer.source_->raw_.get());
439406

440407
CF_EXPECT_EQ(zip_source_commit_write(raw), 0, ZipErrorString(raw));
441408

442409
return {};
443410
}
444411

445412
Result<ReadableZip> ReadableZip::FromSource(SeekableZipSource source) {
446-
zip_source_t* source_raw = CF_EXPECT(source.impl_->raw_.get());
413+
zip_source_t* source_raw = CF_EXPECT(source.raw_.get());
447414

448415
ManagedZipError error = NewZipError();
449416
zip_source_keep(source_raw);
@@ -455,10 +422,7 @@ Result<ReadableZip> ReadableZip::FromSource(SeekableZipSource source) {
455422
return CF_ERR(ZipErrorString(error.get()));
456423
}
457424

458-
// The Impl type is shared between ReadableZip and WritableZip so it uses a
459-
// WritableZipSource type. ReadableZip won't actually call any mutating
460-
// methods on it.
461-
WritableZipSource fake_writable_source(std::move(source.impl_));
425+
WritableZipSource fake_writable_source(std::move(source.raw_));
462426

463427
return ReadableZip(std::make_unique<Impl>(std::move(zip_ret),
464428
std::move(fake_writable_source)));
@@ -522,8 +486,7 @@ Result<SeekableZipSource> ReadableZip::GetFile(uint64_t index) {
522486

523487
CF_EXPECT(raw_source.get(), ZipErrorString(error.get()));
524488

525-
return SeekableZipSource(
526-
std::make_unique<ReadableZipSource::Impl>(std::move(raw_source)));
489+
return SeekableZipSource(std::move(raw_source));
527490
}
528491

529492
ReadableZip::ReadableZip(std::unique_ptr<Impl> impl) : impl_(std::move(impl)) {}
@@ -544,8 +507,7 @@ Result<WritableZip> WritableZip::FromSource(
544507

545508
Result<WritableZip> WritableZip::FromSource(WritableZipSource source,
546509
int flags) {
547-
CF_EXPECT(source.impl_.get());
548-
zip_source_t* source_raw = CF_EXPECT(source.impl_->raw_.get());
510+
zip_source_t* source_raw = CF_EXPECT(source.raw_.get());
549511

550512
ManagedZipError error = NewZipError();
551513
zip_source_keep(source_raw);
@@ -570,13 +532,12 @@ Result<void> WritableZip::AddFile(const std::string& name,
570532
CF_EXPECT(impl_.get());
571533
zip_t* raw_zip = CF_EXPECT(impl_->raw_.get());
572534

573-
CF_EXPECT(source.impl_.get());
574-
zip_source_t* raw_source = CF_EXPECT(source.impl_->raw_.get());
535+
zip_source_t* raw_source = CF_EXPECT(source.raw_.get());
575536

576537
CF_EXPECT_GE(zip_file_add(raw_zip, name.c_str(), raw_source, 0), 0,
577538
ZipErrorString(raw_zip));
578539

579-
source.impl_->raw_.release();
540+
source.raw_.release();
580541

581542
return {};
582543
}

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

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,25 @@
2222
#include <optional>
2323
#include <string>
2424

25+
#include <zip.h>
26+
2527
#include "cuttlefish/common/libs/utils/result.h"
2628

2729
namespace cuttlefish {
2830

31+
struct ZipDeleter {
32+
void operator()(zip_error_t* error) {
33+
zip_error_fini(error);
34+
delete error;
35+
}
36+
void operator()(zip_source_t* source) { zip_source_free(source); }
37+
void operator()(zip_t* zip_ptr) { zip_discard(zip_ptr); }
38+
};
39+
40+
using ManagedZip = std::unique_ptr<zip_t, ZipDeleter>;
41+
using ManagedZipError = std::unique_ptr<zip_error_t, ZipDeleter>;
42+
using ManagedZipSource = std::unique_ptr<zip_source_t, ZipDeleter>;
43+
2944
class ReadableZipSourceCallback {
3045
public:
3146
virtual ~ReadableZipSourceCallback() = default;
@@ -65,9 +80,7 @@ struct ZipStat {
6580
class ReadableZipSource {
6681
public:
6782
friend class ReadableZip;
68-
friend class SeekableZipSource;
6983
friend class WritableZip;
70-
friend class WritableZipSource;
7184
friend class ZipSourceReader;
7285
friend class SeekingZipSourceReader;
7386
friend class ZipSourceWriter;
@@ -87,12 +100,10 @@ class ReadableZipSource {
87100
* state. Can fail. Should not outlive this instance. */
88101
Result<class ZipSourceReader> Reader();
89102

90-
private:
91-
struct Impl; // For pimpl: to avoid exposing libzip headers
92-
93-
ReadableZipSource(std::unique_ptr<Impl>);
103+
protected:
104+
ManagedZipSource raw_;
94105

95-
std::unique_ptr<Impl> impl_;
106+
ReadableZipSource(ManagedZipSource);
96107
};
97108

98109
class SeekableZipSource : public ReadableZipSource {
@@ -103,16 +114,16 @@ class SeekableZipSource : public ReadableZipSource {
103114
static Result<SeekableZipSource> FromCallbacks(
104115
std::unique_ptr<SeekableZipSourceCallback>);
105116

106-
SeekableZipSource(SeekableZipSource&&);
107-
~SeekableZipSource() override;
108-
SeekableZipSource& operator=(SeekableZipSource&&);
117+
SeekableZipSource(SeekableZipSource&&) = default;
118+
~SeekableZipSource() override = default;
119+
SeekableZipSource& operator=(SeekableZipSource&&) = default;
109120

110121
/* Returns a RAII instance that puts this instance in an "open for reading"
111122
* state. Can fail. Should not outlive this instance. */
112123
Result<class SeekingZipSourceReader> Reader();
113124

114-
private:
115-
SeekableZipSource(std::unique_ptr<Impl>);
125+
protected:
126+
SeekableZipSource(ManagedZipSource);
116127
};
117128

118129
class WritableZipSource : public SeekableZipSource {
@@ -125,17 +136,17 @@ class WritableZipSource : public SeekableZipSource {
125136
/* Data access to an in-memory buffer based on serializing a zip archive. */
126137
static Result<WritableZipSource> FromZip(class WritableZip);
127138

128-
WritableZipSource(WritableZipSource&&);
129-
virtual ~WritableZipSource();
130-
WritableZipSource& operator=(WritableZipSource&&);
139+
WritableZipSource(WritableZipSource&&) = default;
140+
virtual ~WritableZipSource() = default;
141+
WritableZipSource& operator=(WritableZipSource&&) = default;
131142

132143
/* Returns a RAII instance that puts this instance in an "open for writing"
133144
* state. Can fail. Should not outlive this instance. Cannot be used at the
134145
* same time as the `Reader()` method from superclasses. */
135146
Result<class ZipSourceWriter> Writer();
136147

137-
private:
138-
WritableZipSource(std::unique_ptr<Impl>);
148+
protected:
149+
WritableZipSource(ManagedZipSource);
139150
};
140151

141152
/* A `ReadableZipSource` in an "open for reading" state. */

0 commit comments

Comments
 (0)