blob: dd109293da0a4f20d700486d237d18f1a506c50c [file] [log] [blame]
Fairphone ODM25c12f52023-12-15 17:24:06 +08001// Copyright 2017 The Chromium Authors
Martin Stjernholmc15e7e42020-12-02 22:50:53 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef THIRD_PARTY_ZLIB_GOOGLE_ZIP_WRITER_H_
6#define THIRD_PARTY_ZLIB_GOOGLE_ZIP_WRITER_H_
7
8#include <memory>
9#include <vector>
10
11#include "base/files/file_path.h"
satayev499be972022-05-13 15:05:39 +000012#include "base/time/time.h"
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000013#include "build/build_config.h"
14#include "third_party/zlib/google/zip.h"
15
16#if defined(USE_SYSTEM_MINIZIP)
17#include <minizip/unzip.h>
18#include <minizip/zip.h>
19#else
20#include "third_party/zlib/contrib/minizip/unzip.h"
21#include "third_party/zlib/contrib/minizip/zip.h"
22#endif
23
24namespace zip {
25namespace internal {
26
27// A class used to write entries to a ZIP file and buffering the reading of
28// files to limit the number of calls to the FileAccessor. This is for
29// performance reasons as these calls may be expensive when IPC based).
30// This class is so far internal and only used by zip.cc, but could be made
31// public if needed.
satayev499be972022-05-13 15:05:39 +000032//
33// All methods returning a bool return true on success and false on error.
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000034class ZipWriter {
35 public:
satayev499be972022-05-13 15:05:39 +000036// Creates a writer that will write a ZIP file to |zip_file_fd| or |zip_file|
37// and which entries are relative to |file_accessor|'s source directory.
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000038// All file reads are performed using |file_accessor|.
satayev499be972022-05-13 15:05:39 +000039#if defined(OS_POSIX) || defined(OS_FUCHSIA)
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000040 static std::unique_ptr<ZipWriter> CreateWithFd(int zip_file_fd,
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000041 FileAccessor* file_accessor);
42#endif
satayev499be972022-05-13 15:05:39 +000043
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000044 static std::unique_ptr<ZipWriter> Create(const base::FilePath& zip_file,
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000045 FileAccessor* file_accessor);
satayev499be972022-05-13 15:05:39 +000046
47 ZipWriter(const ZipWriter&) = delete;
48 ZipWriter& operator=(const ZipWriter&) = delete;
49
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000050 ~ZipWriter();
51
satayev499be972022-05-13 15:05:39 +000052 // Sets the optional progress callback. The callback is called once for each
53 // time |period|. The final callback is always called when the ZIP operation
54 // completes.
55 void SetProgressCallback(ProgressCallback callback, base::TimeDelta period) {
56 progress_callback_ = std::move(callback);
57 progress_period_ = std::move(period);
58 }
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000059
satayev499be972022-05-13 15:05:39 +000060 // Should ignore missing files and directories?
61 void ContinueOnError(bool continue_on_error) {
62 continue_on_error_ = continue_on_error;
63 }
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000064
satayev499be972022-05-13 15:05:39 +000065 // Sets the recursive flag, indicating whether the contents of subdirectories
66 // should be included.
67 void SetRecursive(bool b) { recursive_ = b; }
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000068
satayev499be972022-05-13 15:05:39 +000069 // Sets the filter callback.
70 void SetFilterCallback(FilterCallback callback) {
71 filter_callback_ = std::move(callback);
72 }
73
74 // Adds the contents of a directory. If the recursive flag is set, the
75 // contents of subdirectories are also added.
76 bool AddDirectoryContents(const base::FilePath& path);
77
78 // Adds the entries at |paths| to the ZIP file. These can be a mixed bag of
79 // files and directories. If the recursive flag is set, the contents of
80 // subdirectories is also added.
81 bool AddMixedEntries(Paths paths);
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000082
83 // Closes the ZIP file.
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000084 bool Close();
85
satayev499be972022-05-13 15:05:39 +000086 private:
87 // Takes ownership of |zip_file|.
88 ZipWriter(zipFile zip_file, FileAccessor* file_accessor);
89
90 // Regularly called during processing to check whether zipping should continue
91 // or should be cancelled.
92 bool ShouldContinue();
93
94 // Adds file content to currently open file entry.
95 bool AddFileContent(const base::FilePath& path, base::File file);
96
97 // Adds a file entry (including file contents).
98 bool AddFileEntry(const base::FilePath& path, base::File file);
99
100 // Adds file entries. All the paths should be existing files.
101 bool AddFileEntries(Paths paths);
102
103 // Adds a directory entry. If the recursive flag is set, the contents of this
104 // directory are also added.
105 bool AddDirectoryEntry(const base::FilePath& path);
106
107 // Adds directory entries. All the paths should be existing directories. If
108 // the recursive flag is set, the contents of these directories are also
109 // added.
110 bool AddDirectoryEntries(Paths paths);
111
112 // Opens a file or directory entry.
113 bool OpenNewFileEntry(const base::FilePath& path,
114 bool is_directory,
115 base::Time last_modified);
116
117 // Closes the currently open entry.
118 bool CloseNewFileEntry();
119
120 // Filters entries.
121 void Filter(std::vector<base::FilePath>* paths);
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000122
123 // The actual zip file.
124 zipFile zip_file_;
125
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000126 // Abstraction over file access methods used to read files.
satayev499be972022-05-13 15:05:39 +0000127 FileAccessor* const file_accessor_;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000128
satayev499be972022-05-13 15:05:39 +0000129 // Progress stats.
130 Progress progress_;
131
132 // Optional progress callback.
133 ProgressCallback progress_callback_;
134
135 // Optional progress reporting period.
136 base::TimeDelta progress_period_;
137
138 // Next time to report progress.
139 base::TimeTicks next_progress_report_time_ = base::TimeTicks::Now();
140
141 // Filter used to exclude files from the ZIP file.
142 FilterCallback filter_callback_;
143
144 // Should recursively add directories?
145 bool recursive_ = false;
146
147 // Should ignore missing files and directories?
148 bool continue_on_error_ = false;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000149};
150
151} // namespace internal
152} // namespace zip
153
satayev499be972022-05-13 15:05:39 +0000154#endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_WRITER_H_