diff options
author | Joshua Pawlicki <waffles@google.com> | 2018-02-06 20:24:51 +0000 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2018-02-06 20:24:51 +0000 |
commit | e31b50314a69ef0eb461fc988982268146a3a5d7 (patch) | |
tree | 0b129121b2ce3396b4090e233f7aa4c7693f9f35 /google/zip_reader.h | |
parent | 0ef6628e3fbedd342e491941fe3f7f1e67b4fda3 (diff) | |
download | zlib-e31b50314a69ef0eb461fc988982268146a3a5d7.tar.gz |
Change unzipping library to support cross-process delegates.
This prepares for servicification of the unzipping library, where the
library will not have direct access to the filesystem.
Bug: 792066
Change-Id: I696dd8ef0936f22dc637e078bd8bba565e854ead
Reviewed-on: https://chromium-review.googlesource.com/860996
Commit-Queue: Joshua Pawlicki <waffles@chromium.org>
Reviewed-by: Satoru Takabayashi <satorux@chromium.org>
Reviewed-by: Jay Civelli <jcivelli@chromium.org>
Reviewed-by: Ilya Sherman <isherman@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#534778}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 5d19ee7988a3f97118f3ff791acf77f3eeed891c
Diffstat (limited to 'google/zip_reader.h')
-rw-r--r-- | google/zip_reader.h | 86 |
1 files changed, 48 insertions, 38 deletions
diff --git a/google/zip_reader.h b/google/zip_reader.h index 0464acd..cd24075 100644 --- a/google/zip_reader.h +++ b/google/zip_reader.h @@ -39,6 +39,9 @@ class WriterDelegate { // Invoked to write the next chunk of data. Return false on failure to cancel // extraction. virtual bool WriteBytes(const char* data, int num_bytes) = 0; + + // Sets the last-modified time of the data. + virtual void SetTimeModified(const base::Time& time) = 0; }; // This class is used for reading zip files. A typical use case of this @@ -49,16 +52,16 @@ class WriterDelegate { // reader.Open(zip_file_path); // while (reader.HasMore()) { // reader.OpenCurrentEntryInZip(); -// reader.ExtractCurrentEntryToDirectory(output_directory_path); +// const base::FilePath& entry_path = +// reader.current_entry_info()->file_path(); +// auto writer = CreateFilePathWriterDelegate(extract_dir, entry_path); +// reader.ExtractCurrentEntry(writer, std::numeric_limits<uint64_t>::max()); // reader.AdvanceToNextEntry(); // } // // For simplicity, error checking is omitted in the example code above. The // production code should check return values from all of these functions. // -// This calls can also be used for random access of contents in a zip file -// using LocateAndOpenEntry(). -// class ZipReader { public: // A callback that is called when the operation is successful. @@ -154,27 +157,12 @@ class ZipReader { // state is reset automatically as needed. bool OpenCurrentEntryInZip(); - // Locates an entry in the zip file and opens it. Returns true on - // success. This function internally calls OpenCurrentEntryInZip() on - // success. On failure, current_entry_info() becomes NULL. - bool LocateAndOpenEntry(const base::FilePath& path_in_zip); - // Extracts |num_bytes_to_extract| bytes of the current entry to |delegate|, // starting from the beginning of the entry. Return value specifies whether // the entire file was extracted. bool ExtractCurrentEntry(WriterDelegate* delegate, uint64_t num_bytes_to_extract) const; - // Extracts the current entry to the given output file path. If the - // current file is a directory, just creates a directory - // instead. Returns true on success. OpenCurrentEntryInZip() must be - // called beforehand. - // - // This function preserves the timestamp of the original entry. If that - // timestamp is not valid, the timestamp will be set to the current time. - bool ExtractCurrentEntryToFilePath( - const base::FilePath& output_file_path) const; - // Asynchronously extracts the current entry to the given output file path. // If the current entry is a directory it just creates the directory // synchronously instead. OpenCurrentEntryInZip() must be called beforehand. @@ -187,24 +175,6 @@ class ZipReader { const FailureCallback& failure_callback, const ProgressCallback& progress_callback); - // Extracts the current entry to the given output directory path using - // ExtractCurrentEntryToFilePath(). Sub directories are created as needed - // based on the file path of the current entry. For example, if the file - // path in zip is "foo/bar.txt", and the output directory is "output", - // "output/foo/bar.txt" will be created. - // - // Returns true on success. OpenCurrentEntryInZip() must be called - // beforehand. - // - // This function preserves the timestamp of the original entry. If that - // timestamp is not valid, the timestamp will be set to the current time. - bool ExtractCurrentEntryIntoDirectory( - const base::FilePath& output_directory_path) const; - - // Extracts the current entry by writing directly to a platform file. - // Does not close the file. Returns true on success. - bool ExtractCurrentEntryToFile(base::File* file) const; - // Extracts the current entry into memory. If the current entry is a // directory, the |output| parameter is set to the empty string. If the // current entry is a file, the |output| parameter is filled with its @@ -258,8 +228,14 @@ class ZipReader { // A writer delegate that writes to a given File. class FileWriterDelegate : public WriterDelegate { public: + // Constructs a FileWriterDelegate that manipulates |file|. The delegate will + // not own |file|, therefore the caller must guarantee |file| will outlive the + // delegate. explicit FileWriterDelegate(base::File* file); + // Constructs a FileWriterDelegate that takes ownership of |file|. + explicit FileWriterDelegate(std::unique_ptr<base::File> file); + // Truncates the file to the number of bytes written. ~FileWriterDelegate() override; @@ -272,13 +248,47 @@ class FileWriterDelegate : public WriterDelegate { // if not all bytes could be written. bool WriteBytes(const char* data, int num_bytes) override; + // Sets the last-modified time of the data. + void SetTimeModified(const base::Time& time) override; + private: + // The file the delegate modifies. base::File* file_; - int64_t file_length_; + + // The delegate can optionally own the file it modifies, in which case + // owned_file_ is set and file_ is an alias for owned_file_. + std::unique_ptr<base::File> owned_file_; + + int64_t file_length_ = 0; DISALLOW_COPY_AND_ASSIGN(FileWriterDelegate); }; +// A writer delegate that writes a file at a given path. +class FilePathWriterDelegate : public WriterDelegate { + public: + explicit FilePathWriterDelegate(const base::FilePath& output_file_path); + ~FilePathWriterDelegate() override; + + // WriterDelegate methods: + + // Creates the output file and any necessary intermediate directories. + bool PrepareOutput() override; + + // Writes |num_bytes| bytes of |data| to the file, returning false if not all + // bytes could be written. + bool WriteBytes(const char* data, int num_bytes) override; + + // Sets the last-modified time of the data. + void SetTimeModified(const base::Time& time) override; + + private: + base::FilePath output_file_path_; + base::File file_; + + DISALLOW_COPY_AND_ASSIGN(FilePathWriterDelegate); +}; + } // namespace zip #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ |