Skip to content

Commit

Permalink
[zip] Change const char* to string_view in ZipWriter
Browse files Browse the repository at this point in the history
This would allow adding entries from one zip archive into
a new one without copying, directly from a ZipString object

Change-Id: I52f91008f497e798e044c43f57a6481cf4bec36d
  • Loading branch information
Yurii Zubrytskyi committed Jun 19, 2019
1 parent 716ba5d commit 2b28311
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
9 changes: 5 additions & 4 deletions libziparchive/include/ziparchive/zip_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <memory>
#include <string>
#include <string_view>
#include <vector>

#include "android-base/macros.h"
Expand Down Expand Up @@ -101,7 +102,7 @@ class ZipWriter {
* Subsequent calls to WriteBytes(const void*, size_t) will add data to this entry.
* Returns 0 on success, and an error value < 0 on failure.
*/
int32_t StartEntry(const char* path, size_t flags);
int32_t StartEntry(std::string_view path, size_t flags);

/**
* Starts a new zip entry with the given path and flags, where the
Expand All @@ -111,17 +112,17 @@ class ZipWriter {
* Subsequent calls to WriteBytes(const void*, size_t) will add data to this entry.
* Returns 0 on success, and an error value < 0 on failure.
*/
int32_t StartAlignedEntry(const char* path, size_t flags, uint32_t alignment);
int32_t StartAlignedEntry(std::string_view path, size_t flags, uint32_t alignment);

/**
* Same as StartEntry(const char*, size_t), but sets a last modified time for the entry.
*/
int32_t StartEntryWithTime(const char* path, size_t flags, time_t time);
int32_t StartEntryWithTime(std::string_view path, size_t flags, time_t time);

/**
* Same as StartAlignedEntry(const char*, size_t), but sets a last modified time for the entry.
*/
int32_t StartAlignedEntryWithTime(const char* path, size_t flags, time_t time, uint32_t alignment);
int32_t StartAlignedEntryWithTime(std::string_view path, size_t flags, time_t time, uint32_t alignment);

/**
* Writes bytes to the zip file for the previously started zip entry.
Expand Down
10 changes: 5 additions & 5 deletions libziparchive/zip_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ int32_t ZipWriter::HandleError(int32_t error_code) {
return error_code;
}

int32_t ZipWriter::StartEntry(const char* path, size_t flags) {
int32_t ZipWriter::StartEntry(std::string_view path, size_t flags) {
uint32_t alignment = 0;
if (flags & kAlign32) {
flags &= ~kAlign32;
Expand All @@ -139,11 +139,11 @@ int32_t ZipWriter::StartEntry(const char* path, size_t flags) {
return StartAlignedEntryWithTime(path, flags, time_t(), alignment);
}

int32_t ZipWriter::StartAlignedEntry(const char* path, size_t flags, uint32_t alignment) {
int32_t ZipWriter::StartAlignedEntry(std::string_view path, size_t flags, uint32_t alignment) {
return StartAlignedEntryWithTime(path, flags, time_t(), alignment);
}

int32_t ZipWriter::StartEntryWithTime(const char* path, size_t flags, time_t time) {
int32_t ZipWriter::StartEntryWithTime(std::string_view path, size_t flags, time_t time) {
uint32_t alignment = 0;
if (flags & kAlign32) {
flags &= ~kAlign32;
Expand Down Expand Up @@ -198,7 +198,7 @@ static void CopyFromFileEntry(const ZipWriter::FileEntry& src, bool use_data_des
dst->extra_field_length = src.padding_length;
}

int32_t ZipWriter::StartAlignedEntryWithTime(const char* path, size_t flags, time_t time,
int32_t ZipWriter::StartAlignedEntryWithTime(std::string_view path, size_t flags, time_t time,
uint32_t alignment) {
if (state_ != State::kWritingZip) {
return kInvalidState;
Expand Down Expand Up @@ -265,7 +265,7 @@ int32_t ZipWriter::StartAlignedEntryWithTime(const char* path, size_t flags, tim
return HandleError(kIoError);
}

if (fwrite(path, sizeof(*path), file_entry.path.size(), file_) != file_entry.path.size()) {
if (fwrite(path.data(), 1, path.size(), file_) != path.size()) {
return HandleError(kIoError);
}

Expand Down

0 comments on commit 2b28311

Please sign in to comment.