blob: af70f1df3476ab6b30cc0bd43a6e684e0700cb9f [file] [log] [blame]
Elliott Hughes55fd2932017-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>
18#include <error.h>
19#include <fcntl.h>
Elliott Hughes5f8b3092019-04-08 12:39:20 -070020#include <fnmatch.h>
Elliott Hughes55fd2932017-05-28 22:59:04 -070021#include <getopt.h>
22#include <inttypes.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <time.h>
28#include <unistd.h>
29
30#include <set>
31#include <string>
32
33#include <android-base/file.h>
34#include <android-base/strings.h>
35#include <ziparchive/zip_archive.h>
36
Elliott Hughesd5095252019-10-28 21:35:52 -070037using android::base::EndsWith;
38using android::base::StartsWith;
39
Elliott Hughes55fd2932017-05-28 22:59:04 -070040enum OverwriteMode {
41 kAlways,
42 kNever,
43 kPrompt,
44};
45
Elliott Hughesd5095252019-10-28 21:35:52 -070046enum Role {
47 kUnzip,
48 kZipinfo,
49};
50
51static Role role;
Elliott Hughes55fd2932017-05-28 22:59:04 -070052static OverwriteMode overwrite_mode = kPrompt;
Elliott Hughes26724132019-10-25 09:57:58 -070053static bool flag_1 = false;
Elliott Hughes55fd2932017-05-28 22:59:04 -070054static const char* flag_d = nullptr;
55static bool flag_l = false;
56static bool flag_p = false;
57static bool flag_q = false;
58static bool flag_v = false;
Elliott Hughes26724132019-10-25 09:57:58 -070059static bool flag_x = false;
Elliott Hughes55fd2932017-05-28 22:59:04 -070060static const char* archive_name = nullptr;
61static std::set<std::string> includes;
62static std::set<std::string> excludes;
63static uint64_t total_uncompressed_length = 0;
64static uint64_t total_compressed_length = 0;
65static size_t file_count = 0;
66
Elliott Hughes5f8b3092019-04-08 12:39:20 -070067static bool ShouldInclude(const std::string& name) {
68 // Explicitly excluded?
69 if (!excludes.empty()) {
70 for (const auto& exclude : excludes) {
71 if (!fnmatch(exclude.c_str(), name.c_str(), 0)) return false;
72 }
73 }
74
75 // Implicitly included?
76 if (includes.empty()) return true;
77
78 // Explicitly included?
79 for (const auto& include : includes) {
80 if (!fnmatch(include.c_str(), name.c_str(), 0)) return true;
81 }
Elliott Hughes55fd2932017-05-28 22:59:04 -070082 return false;
83}
84
85static bool MakeDirectoryHierarchy(const std::string& path) {
86 // stat rather than lstat because a symbolic link to a directory is fine too.
87 struct stat sb;
88 if (stat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode)) return true;
89
90 // Ensure the parent directories exist first.
91 if (!MakeDirectoryHierarchy(android::base::Dirname(path))) return false;
92
93 // Then try to create this directory.
94 return (mkdir(path.c_str(), 0777) != -1);
95}
96
97static int CompressionRatio(int64_t uncompressed, int64_t compressed) {
98 if (uncompressed == 0) return 0;
Andreas Gampe964b95c2019-04-05 13:48:02 -070099 return static_cast<int>((100LL * (uncompressed - compressed)) / uncompressed);
Elliott Hughes55fd2932017-05-28 22:59:04 -0700100}
101
Elliott Hughes26724132019-10-25 09:57:58 -0700102static void MaybeShowHeader(ZipArchiveHandle zah) {
Elliott Hughesd5095252019-10-28 21:35:52 -0700103 if (role == kUnzip) {
Elliott Hughes26724132019-10-25 09:57:58 -0700104 // unzip has three formats.
105 if (!flag_q) printf("Archive: %s\n", archive_name);
106 if (flag_v) {
107 printf(
108 " Length Method Size Cmpr Date Time CRC-32 Name\n"
109 "-------- ------ ------- ---- ---------- ----- -------- ----\n");
110 } else if (flag_l) {
111 printf(
112 " Length Date Time Name\n"
113 "--------- ---------- ----- ----\n");
114 }
115 } else {
116 // zipinfo.
117 if (!flag_1 && includes.empty() && excludes.empty()) {
118 ZipArchiveInfo info{GetArchiveInfo(zah)};
119 printf("Archive: %s\n", archive_name);
120 printf("Zip file size: %" PRId64 " bytes, number of entries: %zu\n", info.archive_size,
121 info.entry_count);
122 }
Elliott Hughes55fd2932017-05-28 22:59:04 -0700123 }
124}
125
126static void MaybeShowFooter() {
Elliott Hughesd5095252019-10-28 21:35:52 -0700127 if (role == kUnzip) {
Elliott Hughes26724132019-10-25 09:57:58 -0700128 if (flag_v) {
129 printf(
130 "-------- ------- --- -------\n"
131 "%8" PRId64 " %8" PRId64 " %3d%% %zu file%s\n",
132 total_uncompressed_length, total_compressed_length,
133 CompressionRatio(total_uncompressed_length, total_compressed_length), file_count,
134 (file_count == 1) ? "" : "s");
135 } else if (flag_l) {
136 printf(
137 "--------- -------\n"
138 "%9" PRId64 " %zu file%s\n",
139 total_uncompressed_length, file_count, (file_count == 1) ? "" : "s");
140 }
141 } else {
142 if (!flag_1 && includes.empty() && excludes.empty()) {
143 printf("%zu files, %" PRId64 " bytes uncompressed, %" PRId64 " bytes compressed: %3d%%\n",
144 file_count, total_uncompressed_length, total_compressed_length,
145 CompressionRatio(total_uncompressed_length, total_compressed_length));
146 }
Elliott Hughes55fd2932017-05-28 22:59:04 -0700147 }
148}
149
150static bool PromptOverwrite(const std::string& dst) {
151 // TODO: [r]ename not implemented because it doesn't seem useful.
152 printf("replace %s? [y]es, [n]o, [A]ll, [N]one: ", dst.c_str());
153 fflush(stdout);
154 while (true) {
155 char* line = nullptr;
156 size_t n;
157 if (getline(&line, &n, stdin) == -1) {
158 error(1, 0, "(EOF/read error; assuming [N]one...)");
159 overwrite_mode = kNever;
160 return false;
161 }
162 if (n == 0) continue;
163 char cmd = line[0];
164 free(line);
165 switch (cmd) {
166 case 'y':
167 return true;
168 case 'n':
169 return false;
170 case 'A':
171 overwrite_mode = kAlways;
172 return true;
173 case 'N':
174 overwrite_mode = kNever;
175 return false;
176 }
177 }
178}
179
180static void ExtractToPipe(ZipArchiveHandle zah, ZipEntry& entry, const std::string& name) {
181 // We need to extract to memory because ExtractEntryToFile insists on
182 // being able to seek and truncate, and you can't do that with stdout.
183 uint8_t* buffer = new uint8_t[entry.uncompressed_length];
184 int err = ExtractToMemory(zah, &entry, buffer, entry.uncompressed_length);
185 if (err < 0) {
186 error(1, 0, "failed to extract %s: %s", name.c_str(), ErrorCodeString(err));
187 }
188 if (!android::base::WriteFully(1, buffer, entry.uncompressed_length)) {
189 error(1, errno, "failed to write %s to stdout", name.c_str());
190 }
191 delete[] buffer;
192}
193
194static void ExtractOne(ZipArchiveHandle zah, ZipEntry& entry, const std::string& name) {
195 // Bad filename?
Elliott Hughesd5095252019-10-28 21:35:52 -0700196 if (StartsWith(name, "/") || StartsWith(name, "../") || name.find("/../") != std::string::npos) {
Elliott Hughes55fd2932017-05-28 22:59:04 -0700197 error(1, 0, "bad filename %s", name.c_str());
198 }
199
200 // Where are we actually extracting to (for human-readable output)?
201 std::string dst;
202 if (flag_d) {
203 dst = flag_d;
Elliott Hughesd5095252019-10-28 21:35:52 -0700204 if (!EndsWith(dst, "/")) dst += '/';
Elliott Hughes55fd2932017-05-28 22:59:04 -0700205 }
206 dst += name;
207
208 // Ensure the directory hierarchy exists.
209 if (!MakeDirectoryHierarchy(android::base::Dirname(name))) {
210 error(1, errno, "couldn't create directory hierarchy for %s", dst.c_str());
211 }
212
213 // An entry in a zip file can just be a directory itself.
Elliott Hughesd5095252019-10-28 21:35:52 -0700214 if (EndsWith(name, "/")) {
Elliott Hughes55fd2932017-05-28 22:59:04 -0700215 if (mkdir(name.c_str(), entry.unix_mode) == -1) {
216 // If the directory already exists, that's fine.
217 if (errno == EEXIST) {
218 struct stat sb;
219 if (stat(name.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode)) return;
220 }
221 error(1, errno, "couldn't extract directory %s", dst.c_str());
222 }
223 return;
224 }
225
226 // Create the file.
227 int fd = open(name.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC | O_EXCL, entry.unix_mode);
228 if (fd == -1 && errno == EEXIST) {
229 if (overwrite_mode == kNever) return;
230 if (overwrite_mode == kPrompt && !PromptOverwrite(dst)) return;
231 // Either overwrite_mode is kAlways or the user consented to this specific case.
232 fd = open(name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, entry.unix_mode);
233 }
234 if (fd == -1) error(1, errno, "couldn't create file %s", dst.c_str());
235
236 // Actually extract into the file.
237 if (!flag_q) printf(" inflating: %s\n", dst.c_str());
238 int err = ExtractEntryToFile(zah, &entry, fd);
239 if (err < 0) error(1, 0, "failed to extract %s: %s", dst.c_str(), ErrorCodeString(err));
240 close(fd);
241}
242
243static void ListOne(const ZipEntry& entry, const std::string& name) {
244 tm t = entry.GetModificationTime();
245 char time[32];
246 snprintf(time, sizeof(time), "%04d-%02d-%02d %02d:%02d", t.tm_year + 1900, t.tm_mon + 1,
247 t.tm_mday, t.tm_hour, t.tm_min);
248 if (flag_v) {
249 printf("%8d %s %7d %3d%% %s %08x %s\n", entry.uncompressed_length,
250 (entry.method == kCompressStored) ? "Stored" : "Defl:N", entry.compressed_length,
251 CompressionRatio(entry.uncompressed_length, entry.compressed_length), time, entry.crc32,
252 name.c_str());
253 } else {
254 printf("%9d %s %s\n", entry.uncompressed_length, time, name.c_str());
255 }
256}
257
Elliott Hughes26724132019-10-25 09:57:58 -0700258static void InfoOne(const ZipEntry& entry, const std::string& name) {
259 if (flag_1) {
260 // "android-ndk-r19b/sources/android/NOTICE"
261 printf("%s\n", name.c_str());
262 return;
263 }
264
265 int version = entry.version_made_by & 0xff;
266 int os = (entry.version_made_by >> 8) & 0xff;
267
Elliott Hughesd5095252019-10-28 21:35:52 -0700268 // TODO: Support suid/sgid? Non-Unix/non-FAT host file system attributes?
269 const char* src_fs = "???";
270 char mode[] = "??? ";
271 if (os == 0) {
272 src_fs = "fat";
273 // https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
274 int attrs = entry.external_file_attributes & 0xff;
275 mode[0] = (attrs & 0x10) ? 'd' : '-';
276 mode[1] = 'r';
277 mode[2] = (attrs & 0x01) ? '-' : 'w';
278 // The man page also mentions ".btm", but that seems to be obsolete?
279 mode[3] = EndsWith(name, ".exe") || EndsWith(name, ".com") || EndsWith(name, ".bat") ||
280 EndsWith(name, ".cmd")
281 ? 'x'
282 : '-';
283 mode[4] = (attrs & 0x20) ? 'a' : '-';
284 mode[5] = (attrs & 0x02) ? 'h' : '-';
285 mode[6] = (attrs & 0x04) ? 's' : '-';
286 } else if (os == 3) {
287 src_fs = "unx";
Elliott Hughes26724132019-10-25 09:57:58 -0700288 mode[0] = S_ISDIR(entry.unix_mode) ? 'd' : (S_ISREG(entry.unix_mode) ? '-' : '?');
289 mode[1] = entry.unix_mode & S_IRUSR ? 'r' : '-';
290 mode[2] = entry.unix_mode & S_IWUSR ? 'w' : '-';
291 mode[3] = entry.unix_mode & S_IXUSR ? 'x' : '-';
292 mode[4] = entry.unix_mode & S_IRGRP ? 'r' : '-';
293 mode[5] = entry.unix_mode & S_IWGRP ? 'w' : '-';
294 mode[6] = entry.unix_mode & S_IXGRP ? 'x' : '-';
295 mode[7] = entry.unix_mode & S_IROTH ? 'r' : '-';
296 mode[8] = entry.unix_mode & S_IWOTH ? 'w' : '-';
297 mode[9] = entry.unix_mode & S_IXOTH ? 'x' : '-';
298 }
299
Elliott Hughesd5095252019-10-28 21:35:52 -0700300 char method[5] = "stor";
301 if (entry.method == kCompressDeflated) {
302 snprintf(method, sizeof(method), "def%c", "NXFS"[(entry.gpbf >> 1) & 0x3]);
303 }
304
Elliott Hughes26724132019-10-25 09:57:58 -0700305 // TODO: zipinfo (unlike unzip) sometimes uses time zone?
306 // TODO: this uses 4-digit years because we're not barbarians unless interoperability forces it.
307 tm t = entry.GetModificationTime();
308 char time[32];
309 snprintf(time, sizeof(time), "%04d-%02d-%02d %02d:%02d", t.tm_year + 1900, t.tm_mon + 1,
310 t.tm_mday, t.tm_hour, t.tm_min);
311
312 // "-rw-r--r-- 3.0 unx 577 t- defX 19-Feb-12 16:09 android-ndk-r19b/sources/android/NOTICE"
Elliott Hughesd5095252019-10-28 21:35:52 -0700313 printf("%s %2d.%d %s %8d %c%c %s %s %s\n", mode, version / 10, version % 10, src_fs,
314 entry.uncompressed_length, entry.is_text ? 't' : 'b',
315 entry.has_data_descriptor ? 'X' : 'x', method, time, name.c_str());
Elliott Hughes26724132019-10-25 09:57:58 -0700316}
317
Elliott Hughes55fd2932017-05-28 22:59:04 -0700318static void ProcessOne(ZipArchiveHandle zah, ZipEntry& entry, const std::string& name) {
Elliott Hughesd5095252019-10-28 21:35:52 -0700319 if (role == kUnzip) {
Elliott Hughes26724132019-10-25 09:57:58 -0700320 if (flag_l || flag_v) {
321 // -l or -lv or -lq or -v.
322 ListOne(entry, name);
Elliott Hughes55fd2932017-05-28 22:59:04 -0700323 } else {
Elliott Hughes26724132019-10-25 09:57:58 -0700324 // Actually extract.
325 if (flag_p) {
326 ExtractToPipe(zah, entry, name);
327 } else {
328 ExtractOne(zah, entry, name);
329 }
Elliott Hughes55fd2932017-05-28 22:59:04 -0700330 }
Elliott Hughes26724132019-10-25 09:57:58 -0700331 } else {
332 // zipinfo or zipinfo -1.
333 InfoOne(entry, name);
Elliott Hughes55fd2932017-05-28 22:59:04 -0700334 }
335 total_uncompressed_length += entry.uncompressed_length;
336 total_compressed_length += entry.compressed_length;
337 ++file_count;
338}
339
340static void ProcessAll(ZipArchiveHandle zah) {
Elliott Hughes26724132019-10-25 09:57:58 -0700341 MaybeShowHeader(zah);
Elliott Hughes55fd2932017-05-28 22:59:04 -0700342
343 // libziparchive iteration order doesn't match the central directory.
344 // We could sort, but that would cost extra and wouldn't match either.
345 void* cookie;
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700346 int err = StartIteration(zah, &cookie);
Elliott Hughes55fd2932017-05-28 22:59:04 -0700347 if (err != 0) {
348 error(1, 0, "couldn't iterate %s: %s", archive_name, ErrorCodeString(err));
349 }
350
351 ZipEntry entry;
Elliott Hughese06a8082019-05-22 18:56:41 -0700352 std::string name;
353 while ((err = Next(cookie, &entry, &name)) >= 0) {
Elliott Hughes5f8b3092019-04-08 12:39:20 -0700354 if (ShouldInclude(name)) ProcessOne(zah, entry, name);
Elliott Hughes55fd2932017-05-28 22:59:04 -0700355 }
356
357 if (err < -1) error(1, 0, "failed iterating %s: %s", archive_name, ErrorCodeString(err));
358 EndIteration(cookie);
359
360 MaybeShowFooter();
361}
362
363static void ShowHelp(bool full) {
Elliott Hughesd5095252019-10-28 21:35:52 -0700364 if (role == kUnzip) {
Elliott Hughes26724132019-10-25 09:57:58 -0700365 fprintf(full ? stdout : stderr, "usage: unzip [-d DIR] [-lnopqv] ZIP [FILE...] [-x FILE...]\n");
366 if (!full) exit(EXIT_FAILURE);
Elliott Hughes55fd2932017-05-28 22:59:04 -0700367
Elliott Hughes26724132019-10-25 09:57:58 -0700368 printf(
369 "\n"
370 "Extract FILEs from ZIP archive. Default is all files. Both the include and\n"
371 "exclude (-x) lists use shell glob patterns.\n"
372 "\n"
373 "-d DIR Extract into DIR\n"
374 "-l List contents (-lq excludes archive name, -lv is verbose)\n"
375 "-n Never overwrite files (default: prompt)\n"
376 "-o Always overwrite files\n"
377 "-p Pipe to stdout\n"
378 "-q Quiet\n"
379 "-v List contents verbosely\n"
380 "-x FILE Exclude files\n");
381 } else {
382 fprintf(full ? stdout : stderr, "usage: zipinfo [-1] ZIP [FILE...] [-x FILE...]\n");
383 if (!full) exit(EXIT_FAILURE);
384
385 printf(
386 "\n"
387 "Show information about FILEs from ZIP archive. Default is all files.\n"
388 "Both the include and exclude (-x) lists use shell glob patterns.\n"
389 "\n"
390 "-1 Show filenames only, one per line\n"
391 "-x FILE Exclude files\n");
392 }
Elliott Hughes55fd2932017-05-28 22:59:04 -0700393 exit(EXIT_SUCCESS);
394}
395
Elliott Hughes26724132019-10-25 09:57:58 -0700396static void HandleCommonOption(int opt) {
397 switch (opt) {
398 case 'h':
399 ShowHelp(true);
400 break;
401 case 'x':
402 flag_x = true;
403 break;
404 case 1:
405 // -x swallows all following arguments, so we use '-' in the getopt
406 // string and collect files here.
407 if (!archive_name) {
408 archive_name = optarg;
409 } else if (flag_x) {
410 excludes.insert(optarg);
411 } else {
412 includes.insert(optarg);
413 }
414 break;
415 default:
416 ShowHelp(false);
417 break;
418 }
419}
420
Elliott Hughes55fd2932017-05-28 22:59:04 -0700421int main(int argc, char* argv[]) {
Elliott Hughesd5095252019-10-28 21:35:52 -0700422 // Who am I, and what am I doing?
423 const char* base = basename(argv[0]);
424 if (!strcmp(base, "ziptool") && argc > 1) return main(argc - 1, argv + 1);
425 if (!strcmp(base, "unzip")) {
426 role = kUnzip;
427 } else if (!strcmp(base, "zipinfo")) {
428 role = kZipinfo;
429 } else {
430 error(1, 0, "run as ziptool with unzip or zipinfo as the first argument, or symlink");
431 }
432
433 static const struct option opts[] = {
Elliott Hughes55fd2932017-05-28 22:59:04 -0700434 {"help", no_argument, 0, 'h'},
435 };
Elliott Hughes26724132019-10-25 09:57:58 -0700436
Elliott Hughesd5095252019-10-28 21:35:52 -0700437 if (role == kUnzip) {
Elliott Hughesd3aee662019-10-29 20:47:16 -0700438 // `unzip -Z` is "zipinfo mode", so in that case just restart...
439 if (argc > 1 && !strcmp(argv[1], "-Z")) {
440 argv[1] = const_cast<char*>("zipinfo");
441 return main(argc - 1, argv + 1);
442 }
443
Elliott Hughes26724132019-10-25 09:57:58 -0700444 int opt;
445 while ((opt = getopt_long(argc, argv, "-d:hlnopqvx", opts, nullptr)) != -1) {
446 switch (opt) {
447 case 'd':
448 flag_d = optarg;
449 break;
450 case 'l':
451 flag_l = true;
452 break;
453 case 'n':
454 overwrite_mode = kNever;
455 break;
456 case 'o':
457 overwrite_mode = kAlways;
458 break;
459 case 'p':
460 flag_p = flag_q = true;
461 break;
462 case 'q':
463 flag_q = true;
464 break;
465 case 'v':
466 flag_v = true;
467 break;
468 default:
469 HandleCommonOption(opt);
470 break;
471 }
472 }
473 } else {
474 int opt;
475 while ((opt = getopt_long(argc, argv, "-1hx", opts, nullptr)) != -1) {
476 switch (opt) {
477 case '1':
478 flag_1 = true;
479 break;
480 default:
481 HandleCommonOption(opt);
482 break;
483 }
Elliott Hughes55fd2932017-05-28 22:59:04 -0700484 }
485 }
486
487 if (!archive_name) error(1, 0, "missing archive filename");
488
489 // We can't support "-" to unzip from stdin because libziparchive relies on mmap.
490 ZipArchiveHandle zah;
491 int32_t err;
492 if ((err = OpenArchive(archive_name, &zah)) != 0) {
493 error(1, 0, "couldn't open %s: %s", archive_name, ErrorCodeString(err));
494 }
495
496 // Implement -d by changing into that directory.
497 // We'll create implicit directories based on paths in the zip file, but we
498 // require that the -d directory already exists.
499 if (flag_d && chdir(flag_d) == -1) error(1, errno, "couldn't chdir to %s", flag_d);
500
501 ProcessAll(zah);
502
503 CloseArchive(zah);
504 return 0;
505}