blob: 8afd8f15102503f60f6da2568907d7ad1e33bea3 [file] [log] [blame]
Elliott Hughes6702cef2017-05-28 22:59:04 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
Elliott Hughes6702cef2017-05-28 22:59:04 -070018#include <fcntl.h>
Elliott Hughes57e03122019-04-08 12:39:20 -070019#include <fnmatch.h>
Elliott Hughes6702cef2017-05-28 22:59:04 -070020#include <getopt.h>
21#include <inttypes.h>
Elliott Hughesd9835562019-11-04 19:27:33 -080022#include <libgen.h>
Elliott Hughes9b4703a2019-11-03 08:30:33 -080023#include <stdarg.h>
Elliott Hughes6702cef2017-05-28 22:59:04 -070024#include <stdio.h>
25#include <stdlib.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <time.h>
29#include <unistd.h>
30
31#include <set>
32#include <string>
33
34#include <android-base/file.h>
35#include <android-base/strings.h>
36#include <ziparchive/zip_archive.h>
Elliott Hughesb75c9f32020-09-25 13:30:37 -070037#include <zlib.h>
Elliott Hughes6702cef2017-05-28 22:59:04 -070038
Elliott Hughes344113b2019-10-28 21:35:52 -070039using android::base::EndsWith;
40using android::base::StartsWith;
41
Elliott Hughes6702cef2017-05-28 22:59:04 -070042enum OverwriteMode {
43 kAlways,
44 kNever,
45 kPrompt,
46};
47
Elliott Hughes344113b2019-10-28 21:35:52 -070048enum Role {
49 kUnzip,
50 kZipinfo,
51};
52
53static Role role;
Elliott Hughes6702cef2017-05-28 22:59:04 -070054static OverwriteMode overwrite_mode = kPrompt;
Elliott Hughesdcd3f922019-10-25 09:57:58 -070055static bool flag_1 = false;
Elliott Hughes00725992019-11-15 15:07:00 -080056static std::string flag_d;
Elliott Hughesc9089902020-05-28 17:13:37 -070057static bool flag_j = false;
Elliott Hughes6702cef2017-05-28 22:59:04 -070058static bool flag_l = false;
59static bool flag_p = false;
60static bool flag_q = false;
Elliott Hughesb75c9f32020-09-25 13:30:37 -070061static bool flag_t = false;
Elliott Hughes6702cef2017-05-28 22:59:04 -070062static bool flag_v = false;
Elliott Hughesdcd3f922019-10-25 09:57:58 -070063static bool flag_x = false;
Elliott Hughes6702cef2017-05-28 22:59:04 -070064static const char* archive_name = nullptr;
65static std::set<std::string> includes;
66static std::set<std::string> excludes;
67static uint64_t total_uncompressed_length = 0;
68static uint64_t total_compressed_length = 0;
69static size_t file_count = 0;
Elliott Hughesb75c9f32020-09-25 13:30:37 -070070static size_t bad_crc_count = 0;
Elliott Hughes6702cef2017-05-28 22:59:04 -070071
Elliott Hughes9b4703a2019-11-03 08:30:33 -080072static const char* g_progname;
Elliott Hughesb75c9f32020-09-25 13:30:37 -070073static int g_exit_code = 0;
Elliott Hughes9b4703a2019-11-03 08:30:33 -080074
75static void die(int error, const char* fmt, ...) {
76 va_list ap;
77
78 va_start(ap, fmt);
79 fprintf(stderr, "%s: ", g_progname);
80 vfprintf(stderr, fmt, ap);
81 if (error != 0) fprintf(stderr, ": %s", strerror(error));
82 fprintf(stderr, "\n");
83 va_end(ap);
84 exit(1);
85}
86
Elliott Hughes57e03122019-04-08 12:39:20 -070087static bool ShouldInclude(const std::string& name) {
88 // Explicitly excluded?
89 if (!excludes.empty()) {
90 for (const auto& exclude : excludes) {
91 if (!fnmatch(exclude.c_str(), name.c_str(), 0)) return false;
92 }
93 }
94
95 // Implicitly included?
96 if (includes.empty()) return true;
97
98 // Explicitly included?
99 for (const auto& include : includes) {
100 if (!fnmatch(include.c_str(), name.c_str(), 0)) return true;
101 }
Elliott Hughes6702cef2017-05-28 22:59:04 -0700102 return false;
103}
104
105static bool MakeDirectoryHierarchy(const std::string& path) {
106 // stat rather than lstat because a symbolic link to a directory is fine too.
107 struct stat sb;
108 if (stat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode)) return true;
109
110 // Ensure the parent directories exist first.
111 if (!MakeDirectoryHierarchy(android::base::Dirname(path))) return false;
112
113 // Then try to create this directory.
114 return (mkdir(path.c_str(), 0777) != -1);
115}
116
Elliott Hughesa14f3632019-11-11 16:50:55 -0800117static float CompressionRatio(int64_t uncompressed, int64_t compressed) {
Elliott Hughes6702cef2017-05-28 22:59:04 -0700118 if (uncompressed == 0) return 0;
Nick Desaulniersac673162019-11-13 13:01:48 -0800119 return static_cast<float>(100LL * (uncompressed - compressed)) /
120 static_cast<float>(uncompressed);
Elliott Hughes6702cef2017-05-28 22:59:04 -0700121}
122
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700123static void MaybeShowHeader(ZipArchiveHandle zah) {
Elliott Hughes344113b2019-10-28 21:35:52 -0700124 if (role == kUnzip) {
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700125 // unzip has three formats.
126 if (!flag_q) printf("Archive: %s\n", archive_name);
127 if (flag_v) {
128 printf(
129 " Length Method Size Cmpr Date Time CRC-32 Name\n"
130 "-------- ------ ------- ---- ---------- ----- -------- ----\n");
131 } else if (flag_l) {
132 printf(
133 " Length Date Time Name\n"
134 "--------- ---------- ----- ----\n");
135 }
136 } else {
137 // zipinfo.
138 if (!flag_1 && includes.empty() && excludes.empty()) {
139 ZipArchiveInfo info{GetArchiveInfo(zah)};
140 printf("Archive: %s\n", archive_name);
Tianjie Xu53a7ca02020-03-11 11:59:10 -0700141 printf("Zip file size: %" PRId64 " bytes, number of entries: %" PRIu64 "\n",
142 info.archive_size, info.entry_count);
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700143 }
Elliott Hughes6702cef2017-05-28 22:59:04 -0700144 }
145}
146
147static void MaybeShowFooter() {
Elliott Hughes344113b2019-10-28 21:35:52 -0700148 if (role == kUnzip) {
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700149 if (flag_v) {
150 printf(
151 "-------- ------- --- -------\n"
Elliott Hughesa14f3632019-11-11 16:50:55 -0800152 "%8" PRId64 " %8" PRId64 " %3.0f%% %zu file%s\n",
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700153 total_uncompressed_length, total_compressed_length,
154 CompressionRatio(total_uncompressed_length, total_compressed_length), file_count,
155 (file_count == 1) ? "" : "s");
156 } else if (flag_l) {
157 printf(
158 "--------- -------\n"
159 "%9" PRId64 " %zu file%s\n",
160 total_uncompressed_length, file_count, (file_count == 1) ? "" : "s");
Elliott Hughesb75c9f32020-09-25 13:30:37 -0700161 } else if (flag_t) {
162 if (bad_crc_count != 0) {
163 printf("At least one error was detected in %s.\n", archive_name);
164 } else {
165 printf("No errors detected in ");
166 if (includes.empty() && excludes.empty()) {
167 printf("compressed data of %s.\n", archive_name);
168 } else {
169 printf("%s for the %zu file%s tested.\n", archive_name,
170 file_count, file_count == 1 ? "" : "s");
171 }
172 }
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700173 }
174 } else {
175 if (!flag_1 && includes.empty() && excludes.empty()) {
Elliott Hughesa14f3632019-11-11 16:50:55 -0800176 printf("%zu files, %" PRId64 " bytes uncompressed, %" PRId64 " bytes compressed: %.1f%%\n",
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700177 file_count, total_uncompressed_length, total_compressed_length,
178 CompressionRatio(total_uncompressed_length, total_compressed_length));
179 }
Elliott Hughes6702cef2017-05-28 22:59:04 -0700180 }
181}
182
183static bool PromptOverwrite(const std::string& dst) {
184 // TODO: [r]ename not implemented because it doesn't seem useful.
185 printf("replace %s? [y]es, [n]o, [A]ll, [N]one: ", dst.c_str());
186 fflush(stdout);
187 while (true) {
188 char* line = nullptr;
189 size_t n;
190 if (getline(&line, &n, stdin) == -1) {
Elliott Hughes9b4703a2019-11-03 08:30:33 -0800191 die(0, "(EOF/read error; assuming [N]one...)");
Elliott Hughes6702cef2017-05-28 22:59:04 -0700192 overwrite_mode = kNever;
193 return false;
194 }
195 if (n == 0) continue;
196 char cmd = line[0];
197 free(line);
198 switch (cmd) {
199 case 'y':
200 return true;
201 case 'n':
202 return false;
203 case 'A':
204 overwrite_mode = kAlways;
205 return true;
206 case 'N':
207 overwrite_mode = kNever;
208 return false;
209 }
210 }
211}
212
Elliott Hughesb75c9f32020-09-25 13:30:37 -0700213class TestWriter : public zip_archive::Writer {
214 public:
215 bool Append(uint8_t* buf, size_t size) {
216 crc = static_cast<uint32_t>(crc32(crc, reinterpret_cast<const Bytef*>(buf),
217 static_cast<uInt>(size)));
218 return true;
219 }
220 uint32_t crc = 0;
221};
222
223static void TestOne(ZipArchiveHandle zah, const ZipEntry64& entry, const std::string& name) {
224 if (!flag_q) printf(" testing: %-24s ", name.c_str());
225 TestWriter writer;
226 int err = ExtractToWriter(zah, &entry, &writer);
227 if (err < 0) {
228 die(0, "failed to extract %s: %s", name.c_str(), ErrorCodeString(err));
229 }
230 if (writer.crc == entry.crc32) {
231 if (!flag_q) printf("OK\n");
232 } else {
233 if (flag_q) printf("%-23s ", name.c_str());
234 printf("bad CRC %08" PRIx32 " (should be %08" PRIx32 ")\n", writer.crc, entry.crc32);
235 bad_crc_count++;
236 g_exit_code = 2;
237 }
238}
239
Tianjie26ee1db2020-04-01 23:08:34 -0700240static void ExtractToPipe(ZipArchiveHandle zah, const ZipEntry64& entry, const std::string& name) {
Elliott Hughes6702cef2017-05-28 22:59:04 -0700241 // We need to extract to memory because ExtractEntryToFile insists on
242 // being able to seek and truncate, and you can't do that with stdout.
Tianjie26ee1db2020-04-01 23:08:34 -0700243 if (entry.uncompressed_length > SIZE_MAX) {
244 die(0, "entry size %" PRIu64 " is too large to extract.", entry.uncompressed_length);
245 }
246 auto uncompressed_length = static_cast<size_t>(entry.uncompressed_length);
247 uint8_t* buffer = new uint8_t[uncompressed_length];
248 int err = ExtractToMemory(zah, &entry, buffer, uncompressed_length);
Elliott Hughes6702cef2017-05-28 22:59:04 -0700249 if (err < 0) {
Elliott Hughes9b4703a2019-11-03 08:30:33 -0800250 die(0, "failed to extract %s: %s", name.c_str(), ErrorCodeString(err));
Elliott Hughes6702cef2017-05-28 22:59:04 -0700251 }
Tianjie26ee1db2020-04-01 23:08:34 -0700252 if (!android::base::WriteFully(1, buffer, uncompressed_length)) {
Elliott Hughes9b4703a2019-11-03 08:30:33 -0800253 die(errno, "failed to write %s to stdout", name.c_str());
Elliott Hughes6702cef2017-05-28 22:59:04 -0700254 }
255 delete[] buffer;
256}
257
Elliott Hughesc9089902020-05-28 17:13:37 -0700258static void ExtractOne(ZipArchiveHandle zah, const ZipEntry64& entry, std::string name) {
Elliott Hughes6702cef2017-05-28 22:59:04 -0700259 // Bad filename?
Elliott Hughes344113b2019-10-28 21:35:52 -0700260 if (StartsWith(name, "/") || StartsWith(name, "../") || name.find("/../") != std::string::npos) {
Elliott Hughes9b4703a2019-11-03 08:30:33 -0800261 die(0, "bad filename %s", name.c_str());
Elliott Hughes6702cef2017-05-28 22:59:04 -0700262 }
263
Elliott Hughesc9089902020-05-28 17:13:37 -0700264 // Junk the path if we were asked to.
265 if (flag_j) name = android::base::Basename(name);
266
Elliott Hughes6702cef2017-05-28 22:59:04 -0700267 // Where are we actually extracting to (for human-readable output)?
Elliott Hughes00725992019-11-15 15:07:00 -0800268 // flag_d is the empty string if -d wasn't used, or has a trailing '/'
269 // otherwise.
270 std::string dst = flag_d + name;
Elliott Hughes6702cef2017-05-28 22:59:04 -0700271
272 // Ensure the directory hierarchy exists.
273 if (!MakeDirectoryHierarchy(android::base::Dirname(name))) {
Elliott Hughes9b4703a2019-11-03 08:30:33 -0800274 die(errno, "couldn't create directory hierarchy for %s", dst.c_str());
Elliott Hughes6702cef2017-05-28 22:59:04 -0700275 }
276
277 // An entry in a zip file can just be a directory itself.
Elliott Hughes344113b2019-10-28 21:35:52 -0700278 if (EndsWith(name, "/")) {
Elliott Hughes6702cef2017-05-28 22:59:04 -0700279 if (mkdir(name.c_str(), entry.unix_mode) == -1) {
280 // If the directory already exists, that's fine.
281 if (errno == EEXIST) {
282 struct stat sb;
283 if (stat(name.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode)) return;
284 }
Elliott Hughes9b4703a2019-11-03 08:30:33 -0800285 die(errno, "couldn't extract directory %s", dst.c_str());
Elliott Hughes6702cef2017-05-28 22:59:04 -0700286 }
287 return;
288 }
289
290 // Create the file.
291 int fd = open(name.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC | O_EXCL, entry.unix_mode);
292 if (fd == -1 && errno == EEXIST) {
293 if (overwrite_mode == kNever) return;
294 if (overwrite_mode == kPrompt && !PromptOverwrite(dst)) return;
295 // Either overwrite_mode is kAlways or the user consented to this specific case.
296 fd = open(name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, entry.unix_mode);
297 }
Elliott Hughes9b4703a2019-11-03 08:30:33 -0800298 if (fd == -1) die(errno, "couldn't create file %s", dst.c_str());
Elliott Hughes6702cef2017-05-28 22:59:04 -0700299
300 // Actually extract into the file.
301 if (!flag_q) printf(" inflating: %s\n", dst.c_str());
302 int err = ExtractEntryToFile(zah, &entry, fd);
Elliott Hughes9b4703a2019-11-03 08:30:33 -0800303 if (err < 0) die(0, "failed to extract %s: %s", dst.c_str(), ErrorCodeString(err));
Elliott Hughes6702cef2017-05-28 22:59:04 -0700304 close(fd);
305}
306
Tianjie26ee1db2020-04-01 23:08:34 -0700307static void ListOne(const ZipEntry64& entry, const std::string& name) {
Elliott Hughes6702cef2017-05-28 22:59:04 -0700308 tm t = entry.GetModificationTime();
309 char time[32];
310 snprintf(time, sizeof(time), "%04d-%02d-%02d %02d:%02d", t.tm_year + 1900, t.tm_mon + 1,
311 t.tm_mday, t.tm_hour, t.tm_min);
312 if (flag_v) {
Elliott Hughesf9faaa72020-04-17 16:01:16 -0700313 printf("%8" PRIu64 " %s %8" PRIu64 " %3.0f%% %s %08x %s\n", entry.uncompressed_length,
Elliott Hughes6702cef2017-05-28 22:59:04 -0700314 (entry.method == kCompressStored) ? "Stored" : "Defl:N", entry.compressed_length,
315 CompressionRatio(entry.uncompressed_length, entry.compressed_length), time, entry.crc32,
316 name.c_str());
317 } else {
Elliott Hughesf9faaa72020-04-17 16:01:16 -0700318 printf("%9" PRIu64 " %s %s\n", entry.uncompressed_length, time, name.c_str());
Elliott Hughes6702cef2017-05-28 22:59:04 -0700319 }
320}
321
Tianjie26ee1db2020-04-01 23:08:34 -0700322static void InfoOne(const ZipEntry64& entry, const std::string& name) {
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700323 if (flag_1) {
324 // "android-ndk-r19b/sources/android/NOTICE"
325 printf("%s\n", name.c_str());
326 return;
327 }
328
329 int version = entry.version_made_by & 0xff;
330 int os = (entry.version_made_by >> 8) & 0xff;
331
Elliott Hughes344113b2019-10-28 21:35:52 -0700332 // TODO: Support suid/sgid? Non-Unix/non-FAT host file system attributes?
333 const char* src_fs = "???";
334 char mode[] = "??? ";
335 if (os == 0) {
336 src_fs = "fat";
337 // https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
338 int attrs = entry.external_file_attributes & 0xff;
339 mode[0] = (attrs & 0x10) ? 'd' : '-';
340 mode[1] = 'r';
341 mode[2] = (attrs & 0x01) ? '-' : 'w';
342 // The man page also mentions ".btm", but that seems to be obsolete?
343 mode[3] = EndsWith(name, ".exe") || EndsWith(name, ".com") || EndsWith(name, ".bat") ||
344 EndsWith(name, ".cmd")
345 ? 'x'
346 : '-';
347 mode[4] = (attrs & 0x20) ? 'a' : '-';
348 mode[5] = (attrs & 0x02) ? 'h' : '-';
349 mode[6] = (attrs & 0x04) ? 's' : '-';
350 } else if (os == 3) {
351 src_fs = "unx";
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700352 mode[0] = S_ISDIR(entry.unix_mode) ? 'd' : (S_ISREG(entry.unix_mode) ? '-' : '?');
353 mode[1] = entry.unix_mode & S_IRUSR ? 'r' : '-';
354 mode[2] = entry.unix_mode & S_IWUSR ? 'w' : '-';
355 mode[3] = entry.unix_mode & S_IXUSR ? 'x' : '-';
356 mode[4] = entry.unix_mode & S_IRGRP ? 'r' : '-';
357 mode[5] = entry.unix_mode & S_IWGRP ? 'w' : '-';
358 mode[6] = entry.unix_mode & S_IXGRP ? 'x' : '-';
359 mode[7] = entry.unix_mode & S_IROTH ? 'r' : '-';
360 mode[8] = entry.unix_mode & S_IWOTH ? 'w' : '-';
361 mode[9] = entry.unix_mode & S_IXOTH ? 'x' : '-';
362 }
363
Elliott Hughes344113b2019-10-28 21:35:52 -0700364 char method[5] = "stor";
365 if (entry.method == kCompressDeflated) {
366 snprintf(method, sizeof(method), "def%c", "NXFS"[(entry.gpbf >> 1) & 0x3]);
367 }
368
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700369 // TODO: zipinfo (unlike unzip) sometimes uses time zone?
370 // TODO: this uses 4-digit years because we're not barbarians unless interoperability forces it.
371 tm t = entry.GetModificationTime();
372 char time[32];
373 snprintf(time, sizeof(time), "%04d-%02d-%02d %02d:%02d", t.tm_year + 1900, t.tm_mon + 1,
374 t.tm_mday, t.tm_hour, t.tm_min);
375
376 // "-rw-r--r-- 3.0 unx 577 t- defX 19-Feb-12 16:09 android-ndk-r19b/sources/android/NOTICE"
Tianjie26ee1db2020-04-01 23:08:34 -0700377 printf("%s %2d.%d %s %8" PRIu64 " %c%c %s %s %s\n", mode, version / 10, version % 10, src_fs,
Elliott Hughes344113b2019-10-28 21:35:52 -0700378 entry.uncompressed_length, entry.is_text ? 't' : 'b',
379 entry.has_data_descriptor ? 'X' : 'x', method, time, name.c_str());
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700380}
381
Tianjie26ee1db2020-04-01 23:08:34 -0700382static void ProcessOne(ZipArchiveHandle zah, const ZipEntry64& entry, const std::string& name) {
Elliott Hughes344113b2019-10-28 21:35:52 -0700383 if (role == kUnzip) {
Elliott Hughesb75c9f32020-09-25 13:30:37 -0700384 if (flag_t) {
385 // -t.
386 TestOne(zah, entry, name);
387 } else if (flag_l || flag_v) {
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700388 // -l or -lv or -lq or -v.
389 ListOne(entry, name);
Elliott Hughes6702cef2017-05-28 22:59:04 -0700390 } else {
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700391 // Actually extract.
392 if (flag_p) {
393 ExtractToPipe(zah, entry, name);
394 } else {
395 ExtractOne(zah, entry, name);
396 }
Elliott Hughes6702cef2017-05-28 22:59:04 -0700397 }
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700398 } else {
399 // zipinfo or zipinfo -1.
400 InfoOne(entry, name);
Elliott Hughes6702cef2017-05-28 22:59:04 -0700401 }
402 total_uncompressed_length += entry.uncompressed_length;
403 total_compressed_length += entry.compressed_length;
404 ++file_count;
405}
406
407static void ProcessAll(ZipArchiveHandle zah) {
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700408 MaybeShowHeader(zah);
Elliott Hughes6702cef2017-05-28 22:59:04 -0700409
410 // libziparchive iteration order doesn't match the central directory.
411 // We could sort, but that would cost extra and wouldn't match either.
412 void* cookie;
Elliott Hughes4e78d052019-05-08 10:44:06 -0700413 int err = StartIteration(zah, &cookie);
Elliott Hughes6702cef2017-05-28 22:59:04 -0700414 if (err != 0) {
Elliott Hughes9b4703a2019-11-03 08:30:33 -0800415 die(0, "couldn't iterate %s: %s", archive_name, ErrorCodeString(err));
Elliott Hughes6702cef2017-05-28 22:59:04 -0700416 }
417
Tianjie26ee1db2020-04-01 23:08:34 -0700418 ZipEntry64 entry;
Elliott Hughes21f9ab02019-05-22 18:56:41 -0700419 std::string name;
420 while ((err = Next(cookie, &entry, &name)) >= 0) {
Elliott Hughes57e03122019-04-08 12:39:20 -0700421 if (ShouldInclude(name)) ProcessOne(zah, entry, name);
Elliott Hughes6702cef2017-05-28 22:59:04 -0700422 }
423
Elliott Hughes9b4703a2019-11-03 08:30:33 -0800424 if (err < -1) die(0, "failed iterating %s: %s", archive_name, ErrorCodeString(err));
Elliott Hughes6702cef2017-05-28 22:59:04 -0700425 EndIteration(cookie);
426
427 MaybeShowFooter();
428}
429
430static void ShowHelp(bool full) {
Elliott Hughes344113b2019-10-28 21:35:52 -0700431 if (role == kUnzip) {
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700432 fprintf(full ? stdout : stderr, "usage: unzip [-d DIR] [-lnopqv] ZIP [FILE...] [-x FILE...]\n");
433 if (!full) exit(EXIT_FAILURE);
Elliott Hughes6702cef2017-05-28 22:59:04 -0700434
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700435 printf(
436 "\n"
437 "Extract FILEs from ZIP archive. Default is all files. Both the include and\n"
438 "exclude (-x) lists use shell glob patterns.\n"
439 "\n"
440 "-d DIR Extract into DIR\n"
Elliott Hughesc9089902020-05-28 17:13:37 -0700441 "-j Junk (ignore) file paths\n"
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700442 "-l List contents (-lq excludes archive name, -lv is verbose)\n"
443 "-n Never overwrite files (default: prompt)\n"
444 "-o Always overwrite files\n"
445 "-p Pipe to stdout\n"
446 "-q Quiet\n"
Elliott Hughesb75c9f32020-09-25 13:30:37 -0700447 "-t Test compressed data (do not extract)\n"
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700448 "-v List contents verbosely\n"
449 "-x FILE Exclude files\n");
450 } else {
451 fprintf(full ? stdout : stderr, "usage: zipinfo [-1] ZIP [FILE...] [-x FILE...]\n");
452 if (!full) exit(EXIT_FAILURE);
453
454 printf(
455 "\n"
456 "Show information about FILEs from ZIP archive. Default is all files.\n"
457 "Both the include and exclude (-x) lists use shell glob patterns.\n"
458 "\n"
459 "-1 Show filenames only, one per line\n"
460 "-x FILE Exclude files\n");
461 }
Elliott Hughes6702cef2017-05-28 22:59:04 -0700462 exit(EXIT_SUCCESS);
463}
464
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700465static void HandleCommonOption(int opt) {
466 switch (opt) {
467 case 'h':
468 ShowHelp(true);
469 break;
470 case 'x':
471 flag_x = true;
472 break;
473 case 1:
474 // -x swallows all following arguments, so we use '-' in the getopt
475 // string and collect files here.
476 if (!archive_name) {
477 archive_name = optarg;
478 } else if (flag_x) {
479 excludes.insert(optarg);
480 } else {
481 includes.insert(optarg);
482 }
483 break;
484 default:
485 ShowHelp(false);
486 break;
487 }
488}
489
Elliott Hughes6702cef2017-05-28 22:59:04 -0700490int main(int argc, char* argv[]) {
Elliott Hughes344113b2019-10-28 21:35:52 -0700491 // Who am I, and what am I doing?
Elliott Hughes9b4703a2019-11-03 08:30:33 -0800492 g_progname = basename(argv[0]);
493 if (!strcmp(g_progname, "ziptool") && argc > 1) return main(argc - 1, argv + 1);
494 if (!strcmp(g_progname, "unzip")) {
Elliott Hughes344113b2019-10-28 21:35:52 -0700495 role = kUnzip;
Elliott Hughes9b4703a2019-11-03 08:30:33 -0800496 } else if (!strcmp(g_progname, "zipinfo")) {
Elliott Hughes344113b2019-10-28 21:35:52 -0700497 role = kZipinfo;
498 } else {
Elliott Hughes9b4703a2019-11-03 08:30:33 -0800499 die(0, "run as ziptool with unzip or zipinfo as the first argument, or symlink");
Elliott Hughes344113b2019-10-28 21:35:52 -0700500 }
501
502 static const struct option opts[] = {
Elliott Hughes6702cef2017-05-28 22:59:04 -0700503 {"help", no_argument, 0, 'h'},
Elliott Hughes8373d682019-11-16 11:18:50 -0800504 {},
Elliott Hughes6702cef2017-05-28 22:59:04 -0700505 };
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700506
Elliott Hughes344113b2019-10-28 21:35:52 -0700507 if (role == kUnzip) {
Elliott Hughesa688bed2019-10-29 20:47:16 -0700508 // `unzip -Z` is "zipinfo mode", so in that case just restart...
509 if (argc > 1 && !strcmp(argv[1], "-Z")) {
510 argv[1] = const_cast<char*>("zipinfo");
511 return main(argc - 1, argv + 1);
512 }
513
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700514 int opt;
Elliott Hughesb75c9f32020-09-25 13:30:37 -0700515 while ((opt = getopt_long(argc, argv, "-Dd:hjlnopqtvx", opts, nullptr)) != -1) {
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700516 switch (opt) {
Elliott Hughesb75c9f32020-09-25 13:30:37 -0700517 case 'D':
518 // Undocumented and ignored, since we never use the times from the zip
519 // file when creating files or directories. Moreover, libziparchive
520 // only looks at the DOS last modified date anyway. There's no code to
521 // use the GMT modification/access times in the extra field at the
522 // moment.
523 break;
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700524 case 'd':
525 flag_d = optarg;
Elliott Hughes00725992019-11-15 15:07:00 -0800526 if (!EndsWith(flag_d, "/")) flag_d += '/';
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700527 break;
Elliott Hughesc9089902020-05-28 17:13:37 -0700528 case 'j':
529 flag_j = true;
530 break;
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700531 case 'l':
532 flag_l = true;
533 break;
534 case 'n':
535 overwrite_mode = kNever;
536 break;
537 case 'o':
538 overwrite_mode = kAlways;
539 break;
540 case 'p':
541 flag_p = flag_q = true;
542 break;
543 case 'q':
544 flag_q = true;
545 break;
Elliott Hughesb75c9f32020-09-25 13:30:37 -0700546 case 't':
547 flag_t = true;
548 break;
Elliott Hughesdcd3f922019-10-25 09:57:58 -0700549 case 'v':
550 flag_v = true;
551 break;
552 default:
553 HandleCommonOption(opt);
554 break;
555 }
556 }
557 } else {
558 int opt;
559 while ((opt = getopt_long(argc, argv, "-1hx", opts, nullptr)) != -1) {
560 switch (opt) {
561 case '1':
562 flag_1 = true;
563 break;
564 default:
565 HandleCommonOption(opt);
566 break;
567 }
Elliott Hughes6702cef2017-05-28 22:59:04 -0700568 }
569 }
570
Elliott Hughes9b4703a2019-11-03 08:30:33 -0800571 if (!archive_name) die(0, "missing archive filename");
Elliott Hughes6702cef2017-05-28 22:59:04 -0700572
573 // We can't support "-" to unzip from stdin because libziparchive relies on mmap.
574 ZipArchiveHandle zah;
575 int32_t err;
576 if ((err = OpenArchive(archive_name, &zah)) != 0) {
Elliott Hughes9b4703a2019-11-03 08:30:33 -0800577 die(0, "couldn't open %s: %s", archive_name, ErrorCodeString(err));
Elliott Hughes6702cef2017-05-28 22:59:04 -0700578 }
579
580 // Implement -d by changing into that directory.
Elliott Hughes00725992019-11-15 15:07:00 -0800581 // We'll create implicit directories based on paths in the zip file, and we'll create
582 // the -d directory itself, but we require that *parents* of the -d directory already exists.
583 // This is pretty arbitrary, but it's the behavior of the original unzip.
584 if (!flag_d.empty()) {
585 if (mkdir(flag_d.c_str(), 0777) == -1 && errno != EEXIST) {
586 die(errno, "couldn't created %s", flag_d.c_str());
587 }
588 if (chdir(flag_d.c_str()) == -1) {
589 die(errno, "couldn't chdir to %s", flag_d.c_str());
590 }
591 }
Elliott Hughes6702cef2017-05-28 22:59:04 -0700592
593 ProcessAll(zah);
594
595 CloseArchive(zah);
Elliott Hughesb75c9f32020-09-25 13:30:37 -0700596 return g_exit_code;
Elliott Hughes6702cef2017-05-28 22:59:04 -0700597}