1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "third_party/zlib/google/zip_writer.h"
6
7 #include "base/files/file.h"
8 #include "base/strings/string_util.h"
9 #include "third_party/zlib/google/zip_internal.h"
10
11 namespace zip {
12 namespace internal {
13
14 namespace {
15
16 // Numbers of pending entries that trigger writting them to the ZIP file.
17 constexpr size_t kMaxPendingEntriesCount = 50;
18
AddFileContentToZip(zipFile zip_file,base::File file,const base::FilePath & file_path)19 bool AddFileContentToZip(zipFile zip_file,
20 base::File file,
21 const base::FilePath& file_path) {
22 int num_bytes;
23 char buf[zip::internal::kZipBufSize];
24 do {
25 num_bytes = file.ReadAtCurrentPos(buf, zip::internal::kZipBufSize);
26
27 if (num_bytes > 0) {
28 if (zipWriteInFileInZip(zip_file, buf, num_bytes) != ZIP_OK) {
29 DLOG(ERROR) << "Could not write data to zip for path "
30 << file_path.value();
31 return false;
32 }
33 }
34 } while (num_bytes > 0);
35
36 return true;
37 }
38
OpenNewFileEntry(zipFile zip_file,const base::FilePath & path,bool is_directory,base::Time last_modified)39 bool OpenNewFileEntry(zipFile zip_file,
40 const base::FilePath& path,
41 bool is_directory,
42 base::Time last_modified) {
43 std::string str_path = path.AsUTF8Unsafe();
44 #if defined(OS_WIN)
45 base::ReplaceSubstringsAfterOffset(&str_path, 0u, "\\", "/");
46 #endif
47 if (is_directory)
48 str_path += "/";
49
50 return zip::internal::ZipOpenNewFileInZip(zip_file, str_path, last_modified);
51 }
52
CloseNewFileEntry(zipFile zip_file)53 bool CloseNewFileEntry(zipFile zip_file) {
54 return zipCloseFileInZip(zip_file) == ZIP_OK;
55 }
56
AddFileEntryToZip(zipFile zip_file,const base::FilePath & path,base::File file)57 bool AddFileEntryToZip(zipFile zip_file,
58 const base::FilePath& path,
59 base::File file) {
60 base::File::Info file_info;
61 if (!file.GetInfo(&file_info))
62 return false;
63
64 if (!OpenNewFileEntry(zip_file, path, /*is_directory=*/false,
65 file_info.last_modified))
66 return false;
67
68 bool success = AddFileContentToZip(zip_file, std::move(file), path);
69 if (!CloseNewFileEntry(zip_file))
70 return false;
71
72 return success;
73 }
74
AddDirectoryEntryToZip(zipFile zip_file,const base::FilePath & path,base::Time last_modified)75 bool AddDirectoryEntryToZip(zipFile zip_file,
76 const base::FilePath& path,
77 base::Time last_modified) {
78 return OpenNewFileEntry(zip_file, path, /*is_directory=*/true,
79 last_modified) &&
80 CloseNewFileEntry(zip_file);
81 }
82
83 } // namespace
84
85 #if defined(OS_POSIX)
86 // static
CreateWithFd(int zip_file_fd,const base::FilePath & root_dir,FileAccessor * file_accessor)87 std::unique_ptr<ZipWriter> ZipWriter::CreateWithFd(
88 int zip_file_fd,
89 const base::FilePath& root_dir,
90 FileAccessor* file_accessor) {
91 DCHECK(zip_file_fd != base::kInvalidPlatformFile);
92 zipFile zip_file =
93 internal::OpenFdForZipping(zip_file_fd, APPEND_STATUS_CREATE);
94 if (!zip_file) {
95 DLOG(ERROR) << "Couldn't create ZIP file for FD " << zip_file_fd;
96 return nullptr;
97 }
98 return std::unique_ptr<ZipWriter>(
99 new ZipWriter(zip_file, root_dir, file_accessor));
100 }
101 #endif
102
103 // static
Create(const base::FilePath & zip_file_path,const base::FilePath & root_dir,FileAccessor * file_accessor)104 std::unique_ptr<ZipWriter> ZipWriter::Create(
105 const base::FilePath& zip_file_path,
106 const base::FilePath& root_dir,
107 FileAccessor* file_accessor) {
108 DCHECK(!zip_file_path.empty());
109 zipFile zip_file = internal::OpenForZipping(zip_file_path.AsUTF8Unsafe(),
110 APPEND_STATUS_CREATE);
111 if (!zip_file) {
112 DLOG(ERROR) << "Couldn't create ZIP file at path " << zip_file_path;
113 return nullptr;
114 }
115 return std::unique_ptr<ZipWriter>(
116 new ZipWriter(zip_file, root_dir, file_accessor));
117 }
118
ZipWriter(zipFile zip_file,const base::FilePath & root_dir,FileAccessor * file_accessor)119 ZipWriter::ZipWriter(zipFile zip_file,
120 const base::FilePath& root_dir,
121 FileAccessor* file_accessor)
122 : zip_file_(zip_file), root_dir_(root_dir), file_accessor_(file_accessor) {}
123
~ZipWriter()124 ZipWriter::~ZipWriter() {
125 DCHECK(pending_entries_.empty());
126 }
127
WriteEntries(const std::vector<base::FilePath> & paths)128 bool ZipWriter::WriteEntries(const std::vector<base::FilePath>& paths) {
129 return AddEntries(paths) && Close();
130 }
131
AddEntries(const std::vector<base::FilePath> & paths)132 bool ZipWriter::AddEntries(const std::vector<base::FilePath>& paths) {
133 DCHECK(zip_file_);
134 pending_entries_.insert(pending_entries_.end(), paths.begin(), paths.end());
135 return FlushEntriesIfNeeded(/*force=*/false);
136 }
137
Close()138 bool ZipWriter::Close() {
139 bool success = FlushEntriesIfNeeded(/*force=*/true) &&
140 zipClose(zip_file_, nullptr) == ZIP_OK;
141 zip_file_ = nullptr;
142 return success;
143 }
144
FlushEntriesIfNeeded(bool force)145 bool ZipWriter::FlushEntriesIfNeeded(bool force) {
146 if (pending_entries_.size() < kMaxPendingEntriesCount && !force)
147 return true;
148
149 while (pending_entries_.size() >= kMaxPendingEntriesCount ||
150 (force && !pending_entries_.empty())) {
151 size_t entry_count =
152 std::min(pending_entries_.size(), kMaxPendingEntriesCount);
153 std::vector<base::FilePath> relative_paths;
154 std::vector<base::FilePath> absolute_paths;
155 relative_paths.insert(relative_paths.begin(), pending_entries_.begin(),
156 pending_entries_.begin() + entry_count);
157 for (auto iter = pending_entries_.begin();
158 iter != pending_entries_.begin() + entry_count; ++iter) {
159 // The FileAccessor requires absolute paths.
160 absolute_paths.push_back(root_dir_.Append(*iter));
161 }
162 pending_entries_.erase(pending_entries_.begin(),
163 pending_entries_.begin() + entry_count);
164
165 // We don't know which paths are files and which ones are directories, and
166 // we want to avoid making a call to file_accessor_ for each entry. Open the
167 // files instead, invalid files are returned for directories.
168 std::vector<base::File> files =
169 file_accessor_->OpenFilesForReading(absolute_paths);
170 DCHECK_EQ(files.size(), relative_paths.size());
171 for (size_t i = 0; i < files.size(); i++) {
172 const base::FilePath& relative_path = relative_paths[i];
173 const base::FilePath& absolute_path = absolute_paths[i];
174 base::File file = std::move(files[i]);
175 if (file.IsValid()) {
176 if (!AddFileEntryToZip(zip_file_, relative_path, std::move(file))) {
177 LOG(ERROR) << "Failed to write file " << relative_path.value()
178 << " to ZIP file.";
179 return false;
180 }
181 } else {
182 // Missing file or directory case.
183 base::Time last_modified =
184 file_accessor_->GetLastModifiedTime(absolute_path);
185 if (last_modified.is_null()) {
186 LOG(ERROR) << "Failed to write entry " << relative_path.value()
187 << " to ZIP file.";
188 return false;
189 }
190 DCHECK(file_accessor_->DirectoryExists(absolute_path));
191 if (!AddDirectoryEntryToZip(zip_file_, relative_path, last_modified)) {
192 LOG(ERROR) << "Failed to write directory " << relative_path.value()
193 << " to ZIP file.";
194 return false;
195 }
196 }
197 }
198 }
199 return true;
200 }
201
202 } // namespace internal
203 } // namespace zip
204