[zip] Change const char* to string_view in ZipWriter

This would allow adding entries from one zip archive into
a new one without copying, directly from a ZipString object

Change-Id: I52f91008f497e798e044c43f57a6481cf4bec36d
This commit is contained in:
Yurii Zubrytskyi 2019-06-17 14:26:30 -07:00
parent 716ba5d91d
commit 2b283118a0
2 changed files with 10 additions and 9 deletions

View File

@ -21,6 +21,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
#include "android-base/macros.h" #include "android-base/macros.h"
@ -101,7 +102,7 @@ class ZipWriter {
* Subsequent calls to WriteBytes(const void*, size_t) will add data to this entry. * 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. * 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 * Starts a new zip entry with the given path and flags, where the
@ -111,17 +112,17 @@ class ZipWriter {
* Subsequent calls to WriteBytes(const void*, size_t) will add data to this entry. * 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. * 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. * 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. * 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. * Writes bytes to the zip file for the previously started zip entry.

View File

@ -130,7 +130,7 @@ int32_t ZipWriter::HandleError(int32_t error_code) {
return 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; uint32_t alignment = 0;
if (flags & kAlign32) { if (flags & kAlign32) {
flags &= ~kAlign32; flags &= ~kAlign32;
@ -139,11 +139,11 @@ int32_t ZipWriter::StartEntry(const char* path, size_t flags) {
return StartAlignedEntryWithTime(path, flags, time_t(), alignment); 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); 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; uint32_t alignment = 0;
if (flags & kAlign32) { if (flags & kAlign32) {
flags &= ~kAlign32; flags &= ~kAlign32;
@ -198,7 +198,7 @@ static void CopyFromFileEntry(const ZipWriter::FileEntry& src, bool use_data_des
dst->extra_field_length = src.padding_length; 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) { uint32_t alignment) {
if (state_ != State::kWritingZip) { if (state_ != State::kWritingZip) {
return kInvalidState; return kInvalidState;
@ -265,7 +265,7 @@ int32_t ZipWriter::StartAlignedEntryWithTime(const char* path, size_t flags, tim
return HandleError(kIoError); 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); return HandleError(kIoError);
} }