Merge "Get rid of unneeded allocations in Extract...() APIs"
This commit is contained in:
commit
18f25d399e
1 changed files with 22 additions and 13 deletions
|
|
@ -783,12 +783,12 @@ class FileWriter : public zip_archive::Writer {
|
||||||
// block device).
|
// block device).
|
||||||
//
|
//
|
||||||
// Returns a valid FileWriter on success, |nullptr| if an error occurred.
|
// Returns a valid FileWriter on success, |nullptr| if an error occurred.
|
||||||
static std::unique_ptr<FileWriter> Create(int fd, const ZipEntry* entry) {
|
static FileWriter Create(int fd, const ZipEntry* entry) {
|
||||||
const uint32_t declared_length = entry->uncompressed_length;
|
const uint32_t declared_length = entry->uncompressed_length;
|
||||||
const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
|
const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
|
||||||
if (current_offset == -1) {
|
if (current_offset == -1) {
|
||||||
ALOGW("Zip: unable to seek to current location on fd %d: %s", fd, strerror(errno));
|
ALOGW("Zip: unable to seek to current location on fd %d: %s", fd, strerror(errno));
|
||||||
return nullptr;
|
return FileWriter{};
|
||||||
}
|
}
|
||||||
|
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
|
@ -808,7 +808,7 @@ class FileWriter : public zip_archive::Writer {
|
||||||
ALOGW("Zip: unable to allocate %" PRId64 " bytes at offset %" PRId64 ": %s",
|
ALOGW("Zip: unable to allocate %" PRId64 " bytes at offset %" PRId64 ": %s",
|
||||||
static_cast<int64_t>(declared_length), static_cast<int64_t>(current_offset),
|
static_cast<int64_t>(declared_length), static_cast<int64_t>(current_offset),
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return std::unique_ptr<FileWriter>(nullptr);
|
return FileWriter{};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif // __linux__
|
#endif // __linux__
|
||||||
|
|
@ -816,7 +816,7 @@ class FileWriter : public zip_archive::Writer {
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
if (fstat(fd, &sb) == -1) {
|
if (fstat(fd, &sb) == -1) {
|
||||||
ALOGW("Zip: unable to fstat file: %s", strerror(errno));
|
ALOGW("Zip: unable to fstat file: %s", strerror(errno));
|
||||||
return std::unique_ptr<FileWriter>(nullptr);
|
return FileWriter{};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Block device doesn't support ftruncate(2).
|
// Block device doesn't support ftruncate(2).
|
||||||
|
|
@ -825,13 +825,22 @@ class FileWriter : public zip_archive::Writer {
|
||||||
if (result == -1) {
|
if (result == -1) {
|
||||||
ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
|
ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
|
||||||
static_cast<int64_t>(declared_length + current_offset), strerror(errno));
|
static_cast<int64_t>(declared_length + current_offset), strerror(errno));
|
||||||
return std::unique_ptr<FileWriter>(nullptr);
|
return FileWriter{};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::unique_ptr<FileWriter>(new FileWriter(fd, declared_length));
|
return FileWriter(fd, declared_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FileWriter(FileWriter&& other)
|
||||||
|
: fd_(other.fd_),
|
||||||
|
declared_length_(other.declared_length_),
|
||||||
|
total_bytes_written_(other.total_bytes_written_) {
|
||||||
|
other.fd_ = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsValid() const { return fd_ != -1; }
|
||||||
|
|
||||||
virtual bool Append(uint8_t* buf, size_t buf_size) override {
|
virtual bool Append(uint8_t* buf, size_t buf_size) override {
|
||||||
if (total_bytes_written_ + buf_size > declared_length_) {
|
if (total_bytes_written_ + buf_size > declared_length_) {
|
||||||
ALOGW("Zip: Unexpected size " ZD " (declared) vs " ZD " (actual)", declared_length_,
|
ALOGW("Zip: Unexpected size " ZD " (declared) vs " ZD " (actual)", declared_length_,
|
||||||
|
|
@ -850,10 +859,10 @@ class FileWriter : public zip_archive::Writer {
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FileWriter(const int fd, const size_t declared_length)
|
explicit FileWriter(const int fd = -1, const size_t declared_length = 0)
|
||||||
: Writer(), fd_(fd), declared_length_(declared_length), total_bytes_written_(0) {}
|
: Writer(), fd_(fd), declared_length_(declared_length), total_bytes_written_(0) {}
|
||||||
|
|
||||||
const int fd_;
|
int fd_;
|
||||||
const size_t declared_length_;
|
const size_t declared_length_;
|
||||||
size_t total_bytes_written_;
|
size_t total_bytes_written_;
|
||||||
};
|
};
|
||||||
|
|
@ -1066,17 +1075,17 @@ int32_t ExtractToWriter(ZipArchiveHandle handle, ZipEntry* entry, zip_archive::W
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t ExtractToMemory(ZipArchiveHandle handle, ZipEntry* entry, uint8_t* begin, uint32_t size) {
|
int32_t ExtractToMemory(ZipArchiveHandle handle, ZipEntry* entry, uint8_t* begin, uint32_t size) {
|
||||||
std::unique_ptr<zip_archive::Writer> writer(new MemoryWriter(begin, size));
|
MemoryWriter writer(begin, size);
|
||||||
return ExtractToWriter(handle, entry, writer.get());
|
return ExtractToWriter(handle, entry, &writer);
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t ExtractEntryToFile(ZipArchiveHandle handle, ZipEntry* entry, int fd) {
|
int32_t ExtractEntryToFile(ZipArchiveHandle handle, ZipEntry* entry, int fd) {
|
||||||
std::unique_ptr<zip_archive::Writer> writer(FileWriter::Create(fd, entry));
|
auto writer = FileWriter::Create(fd, entry);
|
||||||
if (writer.get() == nullptr) {
|
if (!writer.IsValid()) {
|
||||||
return kIoError;
|
return kIoError;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ExtractToWriter(handle, entry, writer.get());
|
return ExtractToWriter(handle, entry, &writer);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* ErrorCodeString(int32_t error_code) {
|
const char* ErrorCodeString(int32_t error_code) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue