Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: third_party/zlib/google/zip.cc

Issue 2023703002: Beginning work on GN build (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Really add //build. Add dart_bootstrap rule. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/zlib/google/zip.h ('k') | third_party/zlib/google/zip.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 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.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/bind.h"
11 #include "base/files/file.h"
12 #include "base/files/file_enumerator.h"
13 #include "base/logging.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/string_util.h"
16 #include "build/build_config.h"
17 #include "third_party/zlib/google/zip_internal.h"
18 #include "third_party/zlib/google/zip_reader.h"
19
20 #if defined(USE_SYSTEM_MINIZIP)
21 #include <minizip/unzip.h>
22 #include <minizip/zip.h>
23 #else
24 #include "third_party/zlib/contrib/minizip/unzip.h"
25 #include "third_party/zlib/contrib/minizip/zip.h"
26 #endif
27
28 namespace {
29
30 bool AddFileToZip(zipFile zip_file, const base::FilePath& src_dir) {
31 base::File file(src_dir, base::File::FLAG_OPEN | base::File::FLAG_READ);
32 if (!file.IsValid()) {
33 DLOG(ERROR) << "Could not open file for path " << src_dir.value();
34 return false;
35 }
36
37 int num_bytes;
38 char buf[zip::internal::kZipBufSize];
39 do {
40 num_bytes = file.ReadAtCurrentPos(buf, zip::internal::kZipBufSize);
41 if (num_bytes > 0) {
42 if (ZIP_OK != zipWriteInFileInZip(zip_file, buf, num_bytes)) {
43 DLOG(ERROR) << "Could not write data to zip for path "
44 << src_dir.value();
45 return false;
46 }
47 }
48 } while (num_bytes > 0);
49
50 return true;
51 }
52
53 bool AddEntryToZip(zipFile zip_file, const base::FilePath& path,
54 const base::FilePath& root_path) {
55 base::FilePath relative_path;
56 bool result = root_path.AppendRelativePath(path, &relative_path);
57 DCHECK(result);
58 std::string str_path = relative_path.AsUTF8Unsafe();
59 #if defined(OS_WIN)
60 base::ReplaceSubstringsAfterOffset(&str_path, 0u, "\\", "/");
61 #endif
62
63 bool is_directory = base::DirectoryExists(path);
64 if (is_directory)
65 str_path += "/";
66
67 zip_fileinfo file_info = zip::internal::GetFileInfoForZipping(path);
68 if (!zip::internal::ZipOpenNewFileInZip(zip_file, str_path, &file_info))
69 return false;
70
71 bool success = true;
72 if (!is_directory) {
73 success = AddFileToZip(zip_file, path);
74 }
75
76 if (ZIP_OK != zipCloseFileInZip(zip_file)) {
77 DLOG(ERROR) << "Could not close zip file entry " << str_path;
78 return false;
79 }
80
81 return success;
82 }
83
84 bool ExcludeNoFilesFilter(const base::FilePath& file_path) {
85 return true;
86 }
87
88 bool ExcludeHiddenFilesFilter(const base::FilePath& file_path) {
89 return file_path.BaseName().value()[0] != '.';
90 }
91
92 } // namespace
93
94 namespace zip {
95
96 bool Unzip(const base::FilePath& src_file, const base::FilePath& dest_dir) {
97 ZipReader reader;
98 if (!reader.Open(src_file)) {
99 DLOG(WARNING) << "Failed to open " << src_file.value();
100 return false;
101 }
102 while (reader.HasMore()) {
103 if (!reader.OpenCurrentEntryInZip()) {
104 DLOG(WARNING) << "Failed to open the current file in zip";
105 return false;
106 }
107 if (reader.current_entry_info()->is_unsafe()) {
108 DLOG(WARNING) << "Found an unsafe file in zip "
109 << reader.current_entry_info()->file_path().value();
110 return false;
111 }
112 if (!reader.ExtractCurrentEntryIntoDirectory(dest_dir)) {
113 DLOG(WARNING) << "Failed to extract "
114 << reader.current_entry_info()->file_path().value();
115 return false;
116 }
117 if (!reader.AdvanceToNextEntry()) {
118 DLOG(WARNING) << "Failed to advance to the next file";
119 return false;
120 }
121 }
122 return true;
123 }
124
125 bool ZipWithFilterCallback(const base::FilePath& src_dir,
126 const base::FilePath& dest_file,
127 const FilterCallback& filter_cb) {
128 DCHECK(base::DirectoryExists(src_dir));
129
130 zipFile zip_file = internal::OpenForZipping(dest_file.AsUTF8Unsafe(),
131 APPEND_STATUS_CREATE);
132
133 if (!zip_file) {
134 DLOG(WARNING) << "couldn't create file " << dest_file.value();
135 return false;
136 }
137
138 bool success = true;
139 base::FileEnumerator file_enumerator(src_dir, true /* recursive */,
140 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
141 for (base::FilePath path = file_enumerator.Next(); !path.value().empty();
142 path = file_enumerator.Next()) {
143 if (!filter_cb.Run(path)) {
144 continue;
145 }
146
147 if (!AddEntryToZip(zip_file, path, src_dir)) {
148 success = false;
149 break;
150 }
151 }
152
153 if (ZIP_OK != zipClose(zip_file, NULL)) {
154 DLOG(ERROR) << "Error closing zip file " << dest_file.value();
155 return false;
156 }
157
158 return success;
159 }
160
161 bool Zip(const base::FilePath& src_dir, const base::FilePath& dest_file,
162 bool include_hidden_files) {
163 if (include_hidden_files) {
164 return ZipWithFilterCallback(
165 src_dir, dest_file, base::Bind(&ExcludeNoFilesFilter));
166 } else {
167 return ZipWithFilterCallback(
168 src_dir, dest_file, base::Bind(&ExcludeHiddenFilesFilter));
169 }
170 }
171
172 #if defined(OS_POSIX)
173 bool ZipFiles(const base::FilePath& src_dir,
174 const std::vector<base::FilePath>& src_relative_paths,
175 int dest_fd) {
176 DCHECK(base::DirectoryExists(src_dir));
177 zipFile zip_file = internal::OpenFdForZipping(dest_fd, APPEND_STATUS_CREATE);
178
179 if (!zip_file) {
180 DLOG(ERROR) << "couldn't create file for fd " << dest_fd;
181 return false;
182 }
183
184 bool success = true;
185 for (std::vector<base::FilePath>::const_iterator iter =
186 src_relative_paths.begin();
187 iter != src_relative_paths.end(); ++iter) {
188 const base::FilePath& path = src_dir.Append(*iter);
189 if (!AddEntryToZip(zip_file, path, src_dir)) {
190 // TODO(hshi): clean up the partial zip file when error occurs.
191 success = false;
192 break;
193 }
194 }
195
196 if (ZIP_OK != zipClose(zip_file, NULL)) {
197 DLOG(ERROR) << "Error closing zip file for fd " << dest_fd;
198 success = false;
199 }
200
201 return success;
202 }
203 #endif // defined(OS_POSIX)
204
205 } // namespace zip
OLDNEW
« no previous file with comments | « third_party/zlib/google/zip.h ('k') | third_party/zlib/google/zip.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698