Commit 5f910193 authored by Anand K. Mistry's avatar Anand K. Mistry Committed by Commit Bot

Use base::File::Whence for VolumeArchive::Seek.

Better type safety, and helps minimise changes in the eventual minizip
uprev.

BUG=889703

Change-Id: Ie05f7fcf2d83d792326b728a0cb54d3a4b628b10
Reviewed-on: https://chromium-review.googlesource.com/c/1273996
Commit-Queue: Anand Mistry <amistry@chromium.org>
Reviewed-by: default avatarLuciano Pacheco <lucmult@chromium.org>
Cr-Commit-Position: refs/heads/master@{#598845}
parent 86da6573
......@@ -11,6 +11,7 @@
#include <limits>
#include <utility>
#include "base/files/file.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "ppapi/cpp/logging.h"
......@@ -73,7 +74,7 @@ void* CustomArchiveOpen(void* archive,
int64_t DynamicCache(VolumeArchiveMinizip* archive, int64_t unzip_size) {
int64_t offset = archive->reader()->offset();
if (archive->reader()->Seek(static_cast<int64_t>(offset),
ZLIB_FILEFUNC_SEEK_SET) < 0) {
base::File::FROM_BEGIN) < 0) {
return -1 /* Error */;
}
......@@ -98,7 +99,7 @@ int64_t DynamicCache(VolumeArchiveMinizip* archive, int64_t unzip_size) {
} while (left_length > 0);
if (archive->reader()->Seek(static_cast<int64_t>(offset),
ZLIB_FILEFUNC_SEEK_SET) < 0) {
base::File::FROM_BEGIN) < 0) {
return -1 /* Error */;
}
archive->dynamic_cache_size_ = bytes_to_read - left_length;
......@@ -122,7 +123,7 @@ uint32_t CustomArchiveRead(void* archive,
memcpy(buffer, archive_minizip->static_cache_.get() + offset_in_cache,
size);
if (archive_minizip->reader()->Seek(static_cast<int64_t>(size),
ZLIB_FILEFUNC_SEEK_CUR) < 0) {
base::File::FROM_CURRENT) < 0) {
return -1 /* Error */;
}
return size;
......@@ -154,7 +155,7 @@ uint32_t CustomArchiveRead(void* archive,
unzip_buffer_pointer += copy_length;
left_length -= copy_length;
if (archive_minizip->reader()->Seek(static_cast<int64_t>(copy_length),
ZLIB_FILEFUNC_SEEK_CUR) < 0) {
base::File::FROM_CURRENT) < 0) {
return -1 /* Error */;
}
} while (left_length > 0);
......@@ -182,8 +183,24 @@ long CustomArchiveSeek(void* archive,
VolumeArchiveMinizip* archive_minizip =
static_cast<VolumeArchiveMinizip*>(archive);
long return_value = static_cast<long>(archive_minizip->reader()->Seek(
static_cast<int64_t>(offset), static_cast<int64_t>(origin)));
base::File::Whence whence;
switch (origin) {
case ZLIB_FILEFUNC_SEEK_SET:
whence = base::File::FROM_BEGIN;
break;
case ZLIB_FILEFUNC_SEEK_CUR:
whence = base::File::FROM_CURRENT;
break;
case ZLIB_FILEFUNC_SEEK_END:
whence = base::File::FROM_END;
break;
default:
NOTREACHED();
return -1;
}
long return_value = static_cast<long>(
archive_minizip->reader()->Seek(static_cast<int64_t>(offset), whence));
if (return_value >= 0)
return 0 /* Success */;
return -1 /* Error */;
......@@ -251,7 +268,7 @@ bool VolumeArchiveMinizip::Init(const std::string& encoding) {
int64_t left_length = static_cache_size_;
static_cache_offset_ = std::max(reader()->archive_size() - static_cache_size_,
static_cast<int64_t>(0));
if (reader()->Seek(static_cache_offset_, ZLIB_FILEFUNC_SEEK_SET) < 0) {
if (reader()->Seek(static_cache_offset_, base::File::FROM_BEGIN) < 0) {
set_error_message(kArchiveOpenError);
return false /* Error */;
}
......@@ -264,7 +281,7 @@ bool VolumeArchiveMinizip::Init(const std::string& encoding) {
} while (left_length > 0);
// Set the offset to the original position.
if (reader()->Seek(previous_offset, ZLIB_FILEFUNC_SEEK_SET) < 0) {
if (reader()->Seek(previous_offset, base::File::FROM_BEGIN) < 0) {
set_error_message(kArchiveOpenError);
return false /* Error */;
}
......
......@@ -9,6 +9,8 @@
#include <memory>
#include <string>
#include "base/files/file.h"
// Defines a reader for archive volumes. This class is used by minizip
// for custom reads.
class VolumeReader {
......@@ -30,7 +32,7 @@ class VolumeReader {
// Tries to seek to offset from whence. Returns the resulting offset location
// or -1 in case of errors. Similar to
// http://www.cplusplus.com/reference/cstdio/fseek/
virtual int64_t Seek(int64_t offset, int whence) = 0;
virtual int64_t Seek(int64_t offset, base::File::Whence whence) = 0;
// Fetches a passphrase for reading. If the passphrase is not available it
// returns nullptr.
......
......@@ -7,6 +7,7 @@
#include <algorithm>
#include <limits>
#include "base/files/file.h"
#include "chrome/browser/resources/chromeos/zip_archiver/cpp/javascript_requestor_interface.h"
#include "ppapi/cpp/logging.h"
#include "third_party/minizip/src/unzip.h"
......@@ -177,18 +178,19 @@ int64_t VolumeReaderJavaScriptStream::Read(int64_t bytes_to_read,
return bytes_read;
}
int64_t VolumeReaderJavaScriptStream::Seek(int64_t offset, int whence) {
int64_t VolumeReaderJavaScriptStream::Seek(int64_t offset,
base::File::Whence whence) {
base::AutoLock al(shared_state_lock_);
int64_t new_offset = offset_;
switch (whence) {
case ZLIB_FILEFUNC_SEEK_SET:
case base::File::FROM_BEGIN:
new_offset = offset;
break;
case ZLIB_FILEFUNC_SEEK_CUR:
case base::File::FROM_CURRENT:
new_offset += offset;
break;
case ZLIB_FILEFUNC_SEEK_END:
case base::File::FROM_END:
new_offset = archive_size_ + offset;
break;
default:
......
......@@ -63,7 +63,7 @@ class VolumeReaderJavaScriptStream : public VolumeReader {
int64_t Read(int64_t bytes_to_read, const void** destination_buffer) override;
// See volume_reader.h for description.
int64_t Seek(int64_t offset, int whence) override;
int64_t Seek(int64_t offset, base::File::Whence whence) override;
// Sets the request Id to be used by the reader.
void SetRequestId(const std::string& request_id);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment