blob: 11b575eaee567da10ccc18f0e417a0d7a57a5fca [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>
Elliott Hughes55fd2932017-05-28 22:59:04 -070018#include <fcntl.h>
Elliott Hughes5f8b3092019-04-08 12:39:20 -070019#include <fnmatch.h>
Elliott Hughes55fd2932017-05-28 22:59:04 -070020#include <getopt.h>
21#include <inttypes.h>
Elliott Hughesf1b255a2019-11-04 19:27:33 -080022#include <libgen.h>
Elliott Hughesbcd81062019-11-03 08:30:33 -080023#include <stdarg.h>
Elliott Hughes55fd2932017-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>
37
Elliott Hughesd5095252019-10-28 21:35:52 -070038using android::base::EndsWith;
39using android::base::StartsWith;
40
Elliott Hughes55fd2932017-05-28 22:59:04 -070041enum OverwriteMode {
42 kAlways,
43 kNever,
44 kPrompt,
45};
46
Elliott Hughesd5095252019-10-28 21:35:52 -070047enum Role {
48 kUnzip,
49 kZipinfo,
50};
51
52static Role role;
Elliott Hughes55fd2932017-05-28 22:59:04 -070053static OverwriteMode overwrite_mode = kPrompt;
Elliott Hughes26724132019-10-25 09:57:58 -070054static bool flag_1 = false;
Elliott Hughes55fd2932017-05-28 22:59:04 -070055static const char* flag_d = nullptr;
56static bool flag_l = false;
57static bool flag_p = false;
58static bool flag_q = false;
59static bool flag_v = false;
Elliott Hughes26724132019-10-25 09:57:58 -070060static bool flag_x = false;
Elliott Hughes55fd2932017-05-28 22:59:04 -070061static const char* archive_name = nullptr;
62static std::set<std::string> includes;
63static std::set<std::string> excludes;
64static uint64_t total_uncompressed_length = 0;
65static uint64_t total_compressed_length = 0;
66static size_t file_count = 0;
67
Elliott Hughesbcd81062019-11-03 08:30:33 -080068static const char* g_progname;
69
70static void die(int error, const char* fmt, ...) {
71 va_list ap;
72
73 va_start(ap, fmt);
74 fprintf(stderr, "%s: ", g_progname);
75 vfprintf(stderr, fmt, ap);
76 if (error != 0) fprintf(stderr, ": %s", strerror(error));
77 fprintf(stderr, "\n");
78 va_end(ap);
79 exit(1);
80}
81
Elliott Hughes5f8b3092019-04-08 12:39:20 -070082static bool ShouldInclude(const std::string& name) {
83 // Explicitly excluded?
84 if (!excludes.empty()) {
85 for (const auto& exclude : excludes) {
86 if (!fnmatch(exclude.c_str(), name.c_str(), 0)) return false;
87 }
88 }
89
90 // Implicitly included?
91 if (includes.empty()) return true;
92
93 // Explicitly included?
94 for (const auto& include : includes) {
95 if (!fnmatch(include.c_str(), name.c_str(), 0)) return true;
96 }
Elliott Hughes55fd2932017-05-28 22:59:04 -070097 return false;
98}
99
100static bool MakeDirectoryHierarchy(const std::string& path) {
101 // stat rather than lstat because a symbolic link to a directory is fine too.
102 struct stat sb;
103 if (stat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode)) return true;
104
105 // Ensure the parent directories exist first.
106 if (!MakeDirectoryHierarchy(android::base::Dirname(path))) return false;
107
108 // Then try to create this directory.
109 return (mkdir(path.c_str(), 0777) != -1);
110}
111
Elliott Hughes00875972019-11-11 16:50:55 -0800112static float CompressionRatio(int64_t uncompressed, int64_t compressed) {
Elliott Hughes55fd2932017-05-28 22:59:04 -0700113 if (uncompressed == 0) return 0;
Nick Desaulniers4e7507f2019-11-13 13:01:48 -0800114 return static_cast<float>(100LL * (uncompressed - compressed)) /
115 static_cast<float>(uncompressed);
Elliott Hughes55fd2932017-05-28 22:59:04 -0700116}
117
Elliott Hughes26724132019-10-25 09:57:58 -0700118static void MaybeShowHeader(ZipArchiveHandle zah) {
Elliott Hughesd5095252019-10-28 21:35:52 -0700119 if (role == kUnzip) {
Elliott Hughes26724132019-10-25 09:57:58 -0700120 // unzip has three formats.
121 if (!flag_q) printf("Archive: %s\n", archive_name);
122 if (flag_v) {
123 printf(
124 " Length Method Size Cmpr Date Time CRC-32 Name\n"
125 "-------- ------ ------- ---- ---------- ----- -------- ----\n");
126 } else if (flag_l) {
127 printf(
128 " Length Date Time Name\n"
129 "--------- ---------- ----- ----\n");
130 }
131 } else {
132 // zipinfo.
133 if (!flag_1 && includes.empty() && excludes.empty()) {
134 ZipArchiveInfo info{GetArchiveInfo(zah)};
135 printf("Archive: %s\n", archive_name);
136 printf("Zip file size: %" PRId64 " bytes, number of entries: %zu\n", info.archive_size,
137 info.entry_count);
138 }
Elliott Hughes55fd2932017-05-28 22:59:04 -0700139 }
140}
141
142static void MaybeShowFooter() {
Elliott Hughesd5095252019-10-28 21:35:52 -0700143 if (role == kUnzip) {
Elliott Hughes26724132019-10-25 09:57:58 -0700144 if (flag_v) {
145 printf(
146 "-------- ------- --- -------\n"
Elliott Hughes00875972019-11-11 16:50:55 -0800147 "%8" PRId64 " %8" PRId64 " %3.0f%% %zu file%s\n",
Elliott Hughes26724132019-10-25 09:57:58 -0700148 total_uncompressed_length, total_compressed_length,
149 CompressionRatio(total_uncompressed_length, total_compressed_length), file_count,
150 (file_count == 1) ? "" : "s");
151 } else if (flag_l) {
152 printf(
153 "--------- -------\n"
154 "%9" PRId64 " %zu file%s\n",
155 total_uncompressed_length, file_count, (file_count == 1) ? "" : "s");
156 }
157 } else {
158 if (!flag_1 && includes.empty() && excludes.empty()) {
Elliott Hughes00875972019-11-11 16:50:55 -0800159 printf("%zu files, %" PRId64 " bytes uncompressed, %" PRId64 " bytes compressed: %.1f%%\n",
Elliott Hughes26724132019-10-25 09:57:58 -0700160 file_count, total_uncompressed_length, total_compressed_length,
161 CompressionRatio(total_uncompressed_length, total_compressed_length));
162 }
Elliott Hughes55fd2932017-05-28 22:59:04 -0700163 }
164}
165
166static bool PromptOverwrite(const std::string& dst) {
167 // TODO: [r]ename not implemented because it doesn't seem useful.
168 printf("replace %s? [y]es, [n]o, [A]ll, [N]one: ", dst.c_str());
169 fflush(stdout);
170 while (true) {
171 char* line = nullptr;
172 size_t n;
173 if (getline(&line, &n, stdin) == -1) {
Elliott Hughesbcd81062019-11-03 08:30:33 -0800174 die(0, "(EOF/read error; assuming [N]one...)");
Elliott Hughes55fd2932017-05-28 22:59:04 -0700175 overwrite_mode = kNever;
176 return false;
177 }
178 if (n == 0) continue;
179 char cmd = line[0];
180 free(line);
181 switch (cmd) {
182 case 'y':
183 return true;
184 case 'n':
185 return false;
186 case 'A':
187 overwrite_mode = kAlways;
188 return true;
189 case 'N':
190 overwrite_mode = kNever;
191 return false;
192 }
193 }
194}
195
196static void ExtractToPipe(ZipArchiveHandle zah, ZipEntry& entry, const std::string& name) {
197 // We need to extract to memory because ExtractEntryToFile insists on
198 // being able to seek and truncate, and you can't do that with stdout.
199 uint8_t* buffer = new uint8_t[entry.uncompressed_length];
200 int err = ExtractToMemory(zah, &entry, buffer, entry.uncompressed_length);
201 if (err < 0) {
Elliott Hughesbcd81062019-11-03 08:30:33 -0800202 die(0, "failed to extract %s: %s", name.c_str(), ErrorCodeString(err));
Elliott Hughes55fd2932017-05-28 22:59:04 -0700203 }
204 if (!android::base::WriteFully(1, buffer, entry.uncompressed_length)) {
Elliott Hughesbcd81062019-11-03 08:30:33 -0800205 die(errno, "failed to write %s to stdout", name.c_str());
Elliott Hughes55fd2932017-05-28 22:59:04 -0700206 }
207 delete[] buffer;
208}
209
210static void ExtractOne(ZipArchiveHandle zah, ZipEntry& entry, const std::string& name) {
211 // Bad filename?
Elliott Hughesd5095252019-10-28 21:35:52 -0700212 if (StartsWith(name, "/") || StartsWith(name, "../") || name.find("/../") != std::string::npos) {
Elliott Hughesbcd81062019-11-03 08:30:33 -0800213 die(0, "bad filename %s", name.c_str());
Elliott Hughes55fd2932017-05-28 22:59:04 -0700214 }
215
216 // Where are we actually extracting to (for human-readable output)?
217 std::string dst;
218 if (flag_d) {
219 dst = flag_d;
Elliott Hughesd5095252019-10-28 21:35:52 -0700220 if (!EndsWith(dst, "/")) dst += '/';
Elliott Hughes55fd2932017-05-28 22:59:04 -0700221 }
222 dst += name;
223
224 // Ensure the directory hierarchy exists.
225 if (!MakeDirectoryHierarchy(android::base::Dirname(name))) {
Elliott Hughesbcd81062019-11-03 08:30:33 -0800226 die(errno, "couldn't create directory hierarchy for %s", dst.c_str());
Elliott Hughes55fd2932017-05-28 22:59:04 -0700227 }
228
229 // An entry in a zip file can just be a directory itself.
Elliott Hughesd5095252019-10-28 21:35:52 -0700230 if (EndsWith(name, "/")) {
Elliott Hughes55fd2932017-05-28 22:59:04 -0700231 if (mkdir(name.c_str(), entry.unix_mode) == -1) {
232 // If the directory already exists, that's fine.
233 if (errno == EEXIST) {
234 struct stat sb;
235 if (stat(name.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode)) return;
236 }
Elliott Hughesbcd81062019-11-03 08:30:33 -0800237 die(errno, "couldn't extract directory %s", dst.c_str());
Elliott Hughes55fd2932017-05-28 22:59:04 -0700238 }
239 return;
240 }
241
242 // Create the file.
243 int fd = open(name.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC | O_EXCL, entry.unix_mode);
244 if (fd == -1 && errno == EEXIST) {
245 if (overwrite_mode == kNever) return;
246 if (overwrite_mode == kPrompt && !PromptOverwrite(dst)) return;
247 // Either overwrite_mode is kAlways or the user consented to this specific case.
248 fd = open(name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, entry.unix_mode);
249 }
Elliott Hughesbcd81062019-11-03 08:30:33 -0800250 if (fd == -1) die(errno, "couldn't create file %s", dst.c_str());
Elliott Hughes55fd2932017-05-28 22:59:04 -0700251
252 // Actually extract into the file.
253 if (!flag_q) printf(" inflating: %s\n", dst.c_str());
254 int err = ExtractEntryToFile(zah, &entry, fd);
Elliott Hughesbcd81062019-11-03 08:30:33 -0800255 if (err < 0) die(0, "failed to extract %s: %s", dst.c_str(), ErrorCodeString(err));
Elliott Hughes55fd2932017-05-28 22:59:04 -0700256 close(fd);
257}
258
259static void ListOne(const ZipEntry& entry, const std::string& name) {
260 tm t = entry.GetModificationTime();
261 char time[32];
262 snprintf(time, sizeof(time), "%04d-%02d-%02d %02d:%02d", t.tm_year + 1900, t.tm_mon + 1,
263 t.tm_mday, t.tm_hour, t.tm_min);
264 if (flag_v) {
Elliott Hughes00875972019-11-11 16:50:55 -0800265 printf("%8d %s %7d %3.0f%% %s %08x %s\n", entry.uncompressed_length,
Elliott Hughes55fd2932017-05-28 22:59:04 -0700266 (entry.method == kCompressStored) ? "Stored" : "Defl:N", entry.compressed_length,
267 CompressionRatio(entry.uncompressed_length, entry.compressed_length), time, entry.crc32,
268 name.c_str());
269 } else {
270 printf("%9d %s %s\n", entry.uncompressed_length, time, name.c_str());
271 }
272}
273
Elliott Hughes26724132019-10-25 09:57:58 -0700274static void InfoOne(const ZipEntry& entry, const std::string& name) {
275 if (flag_1) {
276 // "android-ndk-r19b/sources/android/NOTICE"
277 printf("%s\n", name.c_str());
278 return;
279 }
280
281 int version = entry.version_made_by & 0xff;
282 int os = (entry.version_made_by >> 8) & 0xff;
283
Elliott Hughesd5095252019-10-28 21:35:52 -0700284 // TODO: Support suid/sgid? Non-Unix/non-FAT host file system attributes?
285 const char* src_fs = "???";
286 char mode[] = "??? ";
287 if (os == 0) {
288 src_fs = "fat";
289 // https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
290 int attrs = entry.external_file_attributes & 0xff;
291 mode[0] = (attrs & 0x10) ? 'd' : '-';
292 mode[1] = 'r';
293 mode[2] = (attrs & 0x01) ? '-' : 'w';
294 // The man page also mentions ".btm", but that seems to be obsolete?
295 mode[3] = EndsWith(name, ".exe") || EndsWith(name, ".com") || EndsWith(name, ".bat") ||
296 EndsWith(name, ".cmd")
297 ? 'x'
298 : '-';
299 mode[4] = (attrs & 0x20) ? 'a' : '-';
300 mode[5] = (attrs & 0x02) ? 'h' : '-';
301 mode[6] = (attrs & 0x04) ? 's' : '-';
302 } else if (os == 3) {
303 src_fs = "unx";
Elliott Hughes26724132019-10-25 09:57:58 -0700304 mode[0] = S_ISDIR(entry.unix_mode) ? 'd' : (S_ISREG(entry.unix_mode) ? '-' : '?');
305 mode[1] = entry.unix_mode & S_IRUSR ? 'r' : '-';
306 mode[2] = entry.unix_mode & S_IWUSR ? 'w' : '-';
307 mode[3] = entry.unix_mode & S_IXUSR ? 'x' : '-';
308 mode[4] = entry.unix_mode & S_IRGRP ? 'r' : '-';
309 mode[5] = entry.unix_mode & S_IWGRP ? 'w' : '-';
310 mode[6] = entry.unix_mode & S_IXGRP ? 'x' : '-';
311 mode[7] = entry.unix_mode & S_IROTH ? 'r' : '-';
312 mode[8] = entry.unix_mode & S_IWOTH ? 'w' : '-';
313 mode[9] = entry.unix_mode & S_IXOTH ? 'x' : '-';
314 }
315
Elliott Hughesd5095252019-10-28 21:35:52 -0700316 char method[5] = "stor";
317 if (entry.method == kCompressDeflated) {
318 snprintf(method, sizeof(method), "def%c", "NXFS"[(entry.gpbf >> 1) & 0x3]);
319 }
320
Elliott Hughes26724132019-10-25 09:57:58 -0700321 // TODO: zipinfo (unlike unzip) sometimes uses time zone?
322 // TODO: this uses 4-digit years because we're not barbarians unless interoperability forces it.
323 tm t = entry.GetModificationTime();
324 char time[32];
325 snprintf(time, sizeof(time), "%04d-%02d-%02d %02d:%02d", t.tm_year + 1900, t.tm_mon + 1,
326 t.tm_mday, t.tm_hour, t.tm_min);
327
328 // "-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 -0700329 printf("%s %2d.%d %s %8d %c%c %s %s %s\n", mode, version / 10, version % 10, src_fs,
330 entry.uncompressed_length, entry.is_text ? 't' : 'b',
331 entry.has_data_descriptor ? 'X' : 'x', method, time, name.c_str());
Elliott Hughes26724132019-10-25 09:57:58 -0700332}
333
Elliott Hughes55fd2932017-05-28 22:59:04 -0700334static void ProcessOne(ZipArchiveHandle zah, ZipEntry& entry, const std::string& name) {
Elliott Hughesd5095252019-10-28 21:35:52 -0700335 if (role == kUnzip) {
Elliott Hughes26724132019-10-25 09:57:58 -0700336 if (flag_l || flag_v) {
337 // -l or -lv or -lq or -v.
338 ListOne(entry, name);
Elliott Hughes55fd2932017-05-28 22:59:04 -0700339 } else {
Elliott Hughes26724132019-10-25 09:57:58 -0700340 // Actually extract.
341 if (flag_p) {
342 ExtractToPipe(zah, entry, name);
343 } else {
344 ExtractOne(zah, entry, name);
345 }
Elliott Hughes55fd2932017-05-28 22:59:04 -0700346 }
Elliott Hughes26724132019-10-25 09:57:58 -0700347 } else {
348 // zipinfo or zipinfo -1.
349 InfoOne(entry, name);
Elliott Hughes55fd2932017-05-28 22:59:04 -0700350 }
351 total_uncompressed_length += entry.uncompressed_length;
352 total_compressed_length += entry.compressed_length;
353 ++file_count;
354}
355
356static void ProcessAll(ZipArchiveHandle zah) {
Elliott Hughes26724132019-10-25 09:57:58 -0700357 MaybeShowHeader(zah);
Elliott Hughes55fd2932017-05-28 22:59:04 -0700358
359 // libziparchive iteration order doesn't match the central directory.
360 // We could sort, but that would cost extra and wouldn't match either.
361 void* cookie;
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700362 int err = StartIteration(zah, &cookie);
Elliott Hughes55fd2932017-05-28 22:59:04 -0700363 if (err != 0) {
Elliott Hughesbcd81062019-11-03 08:30:33 -0800364 die(0, "couldn't iterate %s: %s", archive_name, ErrorCodeString(err));
Elliott Hughes55fd2932017-05-28 22:59:04 -0700365 }
366
367 ZipEntry entry;
Elliott Hughese06a8082019-05-22 18:56:41 -0700368 std::string name;
369 while ((err = Next(cookie, &entry, &name)) >= 0) {
Elliott Hughes5f8b3092019-04-08 12:39:20 -0700370 if (ShouldInclude(name)) ProcessOne(zah, entry, name);
Elliott Hughes55fd2932017-05-28 22:59:04 -0700371 }
372
Elliott Hughesbcd81062019-11-03 08:30:33 -0800373 if (err < -1) die(0, "failed iterating %s: %s", archive_name, ErrorCodeString(err));
Elliott Hughes55fd2932017-05-28 22:59:04 -0700374 EndIteration(cookie);
375
376 MaybeShowFooter();
377}
378
379static void ShowHelp(bool full) {
Elliott Hughesd5095252019-10-28 21:35:52 -0700380 if (role == kUnzip) {
Elliott Hughes26724132019-10-25 09:57:58 -0700381 fprintf(full ? stdout : stderr, "usage: unzip [-d DIR] [-lnopqv] ZIP [FILE...] [-x FILE...]\n");
382 if (!full) exit(EXIT_FAILURE);
Elliott Hughes55fd2932017-05-28 22:59:04 -0700383
Elliott Hughes26724132019-10-25 09:57:58 -0700384 printf(
385 "\n"
386 "Extract FILEs from ZIP archive. Default is all files. Both the include and\n"
387 "exclude (-x) lists use shell glob patterns.\n"
388 "\n"
389 "-d DIR Extract into DIR\n"
390 "-l List contents (-lq excludes archive name, -lv is verbose)\n"
391 "-n Never overwrite files (default: prompt)\n"
392 "-o Always overwrite files\n"
393 "-p Pipe to stdout\n"
394 "-q Quiet\n"
395 "-v List contents verbosely\n"
396 "-x FILE Exclude files\n");
397 } else {
398 fprintf(full ? stdout : stderr, "usage: zipinfo [-1] ZIP [FILE...] [-x FILE...]\n");
399 if (!full) exit(EXIT_FAILURE);
400
401 printf(
402 "\n"
403 "Show information about FILEs from ZIP archive. Default is all files.\n"
404 "Both the include and exclude (-x) lists use shell glob patterns.\n"
405 "\n"
406 "-1 Show filenames only, one per line\n"
407 "-x FILE Exclude files\n");
408 }
Elliott Hughes55fd2932017-05-28 22:59:04 -0700409 exit(EXIT_SUCCESS);
410}
411
Elliott Hughes26724132019-10-25 09:57:58 -0700412static void HandleCommonOption(int opt) {
413 switch (opt) {
414 case 'h':
415 ShowHelp(true);
416 break;
417 case 'x':
418 flag_x = true;
419 break;
420 case 1:
421 // -x swallows all following arguments, so we use '-' in the getopt
422 // string and collect files here.
423 if (!archive_name) {
424 archive_name = optarg;
425 } else if (flag_x) {
426 excludes.insert(optarg);
427 } else {
428 includes.insert(optarg);
429 }
430 break;
431 default:
432 ShowHelp(false);
433 break;
434 }
435}
436
Elliott Hughes55fd2932017-05-28 22:59:04 -0700437int main(int argc, char* argv[]) {
Elliott Hughesd5095252019-10-28 21:35:52 -0700438 // Who am I, and what am I doing?
Elliott Hughesbcd81062019-11-03 08:30:33 -0800439 g_progname = basename(argv[0]);
440 if (!strcmp(g_progname, "ziptool") && argc > 1) return main(argc - 1, argv + 1);
441 if (!strcmp(g_progname, "unzip")) {
Elliott Hughesd5095252019-10-28 21:35:52 -0700442 role = kUnzip;
Elliott Hughesbcd81062019-11-03 08:30:33 -0800443 } else if (!strcmp(g_progname, "zipinfo")) {
Elliott Hughesd5095252019-10-28 21:35:52 -0700444 role = kZipinfo;
445 } else {
Elliott Hughesbcd81062019-11-03 08:30:33 -0800446 die(0, "run as ziptool with unzip or zipinfo as the first argument, or symlink");
Elliott Hughesd5095252019-10-28 21:35:52 -0700447 }
448
449 static const struct option opts[] = {
Elliott Hughes55fd2932017-05-28 22:59:04 -0700450 {"help", no_argument, 0, 'h'},
Elliott Hughes2ab5a702019-11-16 11:18:50 -0800451 {},
Elliott Hughes55fd2932017-05-28 22:59:04 -0700452 };
Elliott Hughes26724132019-10-25 09:57:58 -0700453
Elliott Hughesd5095252019-10-28 21:35:52 -0700454 if (role == kUnzip) {
Elliott Hughesd3aee662019-10-29 20:47:16 -0700455 // `unzip -Z` is "zipinfo mode", so in that case just restart...
456 if (argc > 1 && !strcmp(argv[1], "-Z")) {
457 argv[1] = const_cast<char*>("zipinfo");
458 return main(argc - 1, argv + 1);
459 }
460
Elliott Hughes26724132019-10-25 09:57:58 -0700461 int opt;
462 while ((opt = getopt_long(argc, argv, "-d:hlnopqvx", opts, nullptr)) != -1) {
463 switch (opt) {
464 case 'd':
465 flag_d = optarg;
466 break;
467 case 'l':
468 flag_l = true;
469 break;
470 case 'n':
471 overwrite_mode = kNever;
472 break;
473 case 'o':
474 overwrite_mode = kAlways;
475 break;
476 case 'p':
477 flag_p = flag_q = true;
478 break;
479 case 'q':
480 flag_q = true;
481 break;
482 case 'v':
483 flag_v = true;
484 break;
485 default:
486 HandleCommonOption(opt);
487 break;
488 }
489 }
490 } else {
491 int opt;
492 while ((opt = getopt_long(argc, argv, "-1hx", opts, nullptr)) != -1) {
493 switch (opt) {
494 case '1':
495 flag_1 = true;
496 break;
497 default:
498 HandleCommonOption(opt);
499 break;
500 }
Elliott Hughes55fd2932017-05-28 22:59:04 -0700501 }
502 }
503
Elliott Hughesbcd81062019-11-03 08:30:33 -0800504 if (!archive_name) die(0, "missing archive filename");
Elliott Hughes55fd2932017-05-28 22:59:04 -0700505
506 // We can't support "-" to unzip from stdin because libziparchive relies on mmap.
507 ZipArchiveHandle zah;
508 int32_t err;
509 if ((err = OpenArchive(archive_name, &zah)) != 0) {
Elliott Hughesbcd81062019-11-03 08:30:33 -0800510 die(0, "couldn't open %s: %s", archive_name, ErrorCodeString(err));
Elliott Hughes55fd2932017-05-28 22:59:04 -0700511 }
512
513 // Implement -d by changing into that directory.
514 // We'll create implicit directories based on paths in the zip file, but we
515 // require that the -d directory already exists.
Elliott Hughesbcd81062019-11-03 08:30:33 -0800516 if (flag_d && chdir(flag_d) == -1) die(errno, "couldn't chdir to %s", flag_d);
Elliott Hughes55fd2932017-05-28 22:59:04 -0700517
518 ProcessAll(zah);
519
520 CloseArchive(zah);
521 return 0;
522}