Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f2aa5ec2 authored by Narayan Kamath's avatar Narayan Kamath Committed by android-build-merger
Browse files

Merge "Get rid of unneeded allocations in Extract...() APIs" am: 18f25d39 am: 64bf32d9

am: 780105e6

Change-Id: I71782554db964d33820a4ee68ae766a05c39266f
parents faaa0d71 780105e6
Loading
Loading
Loading
Loading
+22 −13
Original line number Original line Diff line number Diff line
@@ -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 FileWriter(fd, declared_length);
  }
  }


    return std::unique_ptr<FileWriter>(new 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) {