Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 18 | #include <fcntl.h> |
Elliott Hughes | 5f8b309 | 2019-04-08 12:39:20 -0700 | [diff] [blame] | 19 | #include <fnmatch.h> |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 20 | #include <getopt.h> |
| 21 | #include <inttypes.h> |
Elliott Hughes | f1b255a | 2019-11-04 19:27:33 -0800 | [diff] [blame^] | 22 | #include <libgen.h> |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 23 | #include <stdarg.h> |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 24 | #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 Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 38 | using android::base::EndsWith; |
| 39 | using android::base::StartsWith; |
| 40 | |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 41 | enum OverwriteMode { |
| 42 | kAlways, |
| 43 | kNever, |
| 44 | kPrompt, |
| 45 | }; |
| 46 | |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 47 | enum Role { |
| 48 | kUnzip, |
| 49 | kZipinfo, |
| 50 | }; |
| 51 | |
| 52 | static Role role; |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 53 | static OverwriteMode overwrite_mode = kPrompt; |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 54 | static bool flag_1 = false; |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 55 | static const char* flag_d = nullptr; |
| 56 | static bool flag_l = false; |
| 57 | static bool flag_p = false; |
| 58 | static bool flag_q = false; |
| 59 | static bool flag_v = false; |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 60 | static bool flag_x = false; |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 61 | static const char* archive_name = nullptr; |
| 62 | static std::set<std::string> includes; |
| 63 | static std::set<std::string> excludes; |
| 64 | static uint64_t total_uncompressed_length = 0; |
| 65 | static uint64_t total_compressed_length = 0; |
| 66 | static size_t file_count = 0; |
| 67 | |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 68 | static const char* g_progname; |
| 69 | |
| 70 | static 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 Hughes | 5f8b309 | 2019-04-08 12:39:20 -0700 | [diff] [blame] | 82 | static 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 Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | |
| 100 | static 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 | |
| 112 | static int CompressionRatio(int64_t uncompressed, int64_t compressed) { |
| 113 | if (uncompressed == 0) return 0; |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 114 | return static_cast<int>((100LL * (uncompressed - compressed)) / uncompressed); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 117 | static void MaybeShowHeader(ZipArchiveHandle zah) { |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 118 | if (role == kUnzip) { |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 119 | // unzip has three formats. |
| 120 | if (!flag_q) printf("Archive: %s\n", archive_name); |
| 121 | if (flag_v) { |
| 122 | printf( |
| 123 | " Length Method Size Cmpr Date Time CRC-32 Name\n" |
| 124 | "-------- ------ ------- ---- ---------- ----- -------- ----\n"); |
| 125 | } else if (flag_l) { |
| 126 | printf( |
| 127 | " Length Date Time Name\n" |
| 128 | "--------- ---------- ----- ----\n"); |
| 129 | } |
| 130 | } else { |
| 131 | // zipinfo. |
| 132 | if (!flag_1 && includes.empty() && excludes.empty()) { |
| 133 | ZipArchiveInfo info{GetArchiveInfo(zah)}; |
| 134 | printf("Archive: %s\n", archive_name); |
| 135 | printf("Zip file size: %" PRId64 " bytes, number of entries: %zu\n", info.archive_size, |
| 136 | info.entry_count); |
| 137 | } |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | static void MaybeShowFooter() { |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 142 | if (role == kUnzip) { |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 143 | if (flag_v) { |
| 144 | printf( |
| 145 | "-------- ------- --- -------\n" |
| 146 | "%8" PRId64 " %8" PRId64 " %3d%% %zu file%s\n", |
| 147 | total_uncompressed_length, total_compressed_length, |
| 148 | CompressionRatio(total_uncompressed_length, total_compressed_length), file_count, |
| 149 | (file_count == 1) ? "" : "s"); |
| 150 | } else if (flag_l) { |
| 151 | printf( |
| 152 | "--------- -------\n" |
| 153 | "%9" PRId64 " %zu file%s\n", |
| 154 | total_uncompressed_length, file_count, (file_count == 1) ? "" : "s"); |
| 155 | } |
| 156 | } else { |
| 157 | if (!flag_1 && includes.empty() && excludes.empty()) { |
| 158 | printf("%zu files, %" PRId64 " bytes uncompressed, %" PRId64 " bytes compressed: %3d%%\n", |
| 159 | file_count, total_uncompressed_length, total_compressed_length, |
| 160 | CompressionRatio(total_uncompressed_length, total_compressed_length)); |
| 161 | } |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | static bool PromptOverwrite(const std::string& dst) { |
| 166 | // TODO: [r]ename not implemented because it doesn't seem useful. |
| 167 | printf("replace %s? [y]es, [n]o, [A]ll, [N]one: ", dst.c_str()); |
| 168 | fflush(stdout); |
| 169 | while (true) { |
| 170 | char* line = nullptr; |
| 171 | size_t n; |
| 172 | if (getline(&line, &n, stdin) == -1) { |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 173 | die(0, "(EOF/read error; assuming [N]one...)"); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 174 | overwrite_mode = kNever; |
| 175 | return false; |
| 176 | } |
| 177 | if (n == 0) continue; |
| 178 | char cmd = line[0]; |
| 179 | free(line); |
| 180 | switch (cmd) { |
| 181 | case 'y': |
| 182 | return true; |
| 183 | case 'n': |
| 184 | return false; |
| 185 | case 'A': |
| 186 | overwrite_mode = kAlways; |
| 187 | return true; |
| 188 | case 'N': |
| 189 | overwrite_mode = kNever; |
| 190 | return false; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | static void ExtractToPipe(ZipArchiveHandle zah, ZipEntry& entry, const std::string& name) { |
| 196 | // We need to extract to memory because ExtractEntryToFile insists on |
| 197 | // being able to seek and truncate, and you can't do that with stdout. |
| 198 | uint8_t* buffer = new uint8_t[entry.uncompressed_length]; |
| 199 | int err = ExtractToMemory(zah, &entry, buffer, entry.uncompressed_length); |
| 200 | if (err < 0) { |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 201 | die(0, "failed to extract %s: %s", name.c_str(), ErrorCodeString(err)); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 202 | } |
| 203 | if (!android::base::WriteFully(1, buffer, entry.uncompressed_length)) { |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 204 | die(errno, "failed to write %s to stdout", name.c_str()); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 205 | } |
| 206 | delete[] buffer; |
| 207 | } |
| 208 | |
| 209 | static void ExtractOne(ZipArchiveHandle zah, ZipEntry& entry, const std::string& name) { |
| 210 | // Bad filename? |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 211 | if (StartsWith(name, "/") || StartsWith(name, "../") || name.find("/../") != std::string::npos) { |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 212 | die(0, "bad filename %s", name.c_str()); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // Where are we actually extracting to (for human-readable output)? |
| 216 | std::string dst; |
| 217 | if (flag_d) { |
| 218 | dst = flag_d; |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 219 | if (!EndsWith(dst, "/")) dst += '/'; |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 220 | } |
| 221 | dst += name; |
| 222 | |
| 223 | // Ensure the directory hierarchy exists. |
| 224 | if (!MakeDirectoryHierarchy(android::base::Dirname(name))) { |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 225 | die(errno, "couldn't create directory hierarchy for %s", dst.c_str()); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // An entry in a zip file can just be a directory itself. |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 229 | if (EndsWith(name, "/")) { |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 230 | if (mkdir(name.c_str(), entry.unix_mode) == -1) { |
| 231 | // If the directory already exists, that's fine. |
| 232 | if (errno == EEXIST) { |
| 233 | struct stat sb; |
| 234 | if (stat(name.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode)) return; |
| 235 | } |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 236 | die(errno, "couldn't extract directory %s", dst.c_str()); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 237 | } |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | // Create the file. |
| 242 | int fd = open(name.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC | O_EXCL, entry.unix_mode); |
| 243 | if (fd == -1 && errno == EEXIST) { |
| 244 | if (overwrite_mode == kNever) return; |
| 245 | if (overwrite_mode == kPrompt && !PromptOverwrite(dst)) return; |
| 246 | // Either overwrite_mode is kAlways or the user consented to this specific case. |
| 247 | fd = open(name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, entry.unix_mode); |
| 248 | } |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 249 | if (fd == -1) die(errno, "couldn't create file %s", dst.c_str()); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 250 | |
| 251 | // Actually extract into the file. |
| 252 | if (!flag_q) printf(" inflating: %s\n", dst.c_str()); |
| 253 | int err = ExtractEntryToFile(zah, &entry, fd); |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 254 | if (err < 0) die(0, "failed to extract %s: %s", dst.c_str(), ErrorCodeString(err)); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 255 | close(fd); |
| 256 | } |
| 257 | |
| 258 | static void ListOne(const ZipEntry& entry, const std::string& name) { |
| 259 | tm t = entry.GetModificationTime(); |
| 260 | char time[32]; |
| 261 | snprintf(time, sizeof(time), "%04d-%02d-%02d %02d:%02d", t.tm_year + 1900, t.tm_mon + 1, |
| 262 | t.tm_mday, t.tm_hour, t.tm_min); |
| 263 | if (flag_v) { |
| 264 | printf("%8d %s %7d %3d%% %s %08x %s\n", entry.uncompressed_length, |
| 265 | (entry.method == kCompressStored) ? "Stored" : "Defl:N", entry.compressed_length, |
| 266 | CompressionRatio(entry.uncompressed_length, entry.compressed_length), time, entry.crc32, |
| 267 | name.c_str()); |
| 268 | } else { |
| 269 | printf("%9d %s %s\n", entry.uncompressed_length, time, name.c_str()); |
| 270 | } |
| 271 | } |
| 272 | |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 273 | static void InfoOne(const ZipEntry& entry, const std::string& name) { |
| 274 | if (flag_1) { |
| 275 | // "android-ndk-r19b/sources/android/NOTICE" |
| 276 | printf("%s\n", name.c_str()); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | int version = entry.version_made_by & 0xff; |
| 281 | int os = (entry.version_made_by >> 8) & 0xff; |
| 282 | |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 283 | // TODO: Support suid/sgid? Non-Unix/non-FAT host file system attributes? |
| 284 | const char* src_fs = "???"; |
| 285 | char mode[] = "??? "; |
| 286 | if (os == 0) { |
| 287 | src_fs = "fat"; |
| 288 | // https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants |
| 289 | int attrs = entry.external_file_attributes & 0xff; |
| 290 | mode[0] = (attrs & 0x10) ? 'd' : '-'; |
| 291 | mode[1] = 'r'; |
| 292 | mode[2] = (attrs & 0x01) ? '-' : 'w'; |
| 293 | // The man page also mentions ".btm", but that seems to be obsolete? |
| 294 | mode[3] = EndsWith(name, ".exe") || EndsWith(name, ".com") || EndsWith(name, ".bat") || |
| 295 | EndsWith(name, ".cmd") |
| 296 | ? 'x' |
| 297 | : '-'; |
| 298 | mode[4] = (attrs & 0x20) ? 'a' : '-'; |
| 299 | mode[5] = (attrs & 0x02) ? 'h' : '-'; |
| 300 | mode[6] = (attrs & 0x04) ? 's' : '-'; |
| 301 | } else if (os == 3) { |
| 302 | src_fs = "unx"; |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 303 | mode[0] = S_ISDIR(entry.unix_mode) ? 'd' : (S_ISREG(entry.unix_mode) ? '-' : '?'); |
| 304 | mode[1] = entry.unix_mode & S_IRUSR ? 'r' : '-'; |
| 305 | mode[2] = entry.unix_mode & S_IWUSR ? 'w' : '-'; |
| 306 | mode[3] = entry.unix_mode & S_IXUSR ? 'x' : '-'; |
| 307 | mode[4] = entry.unix_mode & S_IRGRP ? 'r' : '-'; |
| 308 | mode[5] = entry.unix_mode & S_IWGRP ? 'w' : '-'; |
| 309 | mode[6] = entry.unix_mode & S_IXGRP ? 'x' : '-'; |
| 310 | mode[7] = entry.unix_mode & S_IROTH ? 'r' : '-'; |
| 311 | mode[8] = entry.unix_mode & S_IWOTH ? 'w' : '-'; |
| 312 | mode[9] = entry.unix_mode & S_IXOTH ? 'x' : '-'; |
| 313 | } |
| 314 | |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 315 | char method[5] = "stor"; |
| 316 | if (entry.method == kCompressDeflated) { |
| 317 | snprintf(method, sizeof(method), "def%c", "NXFS"[(entry.gpbf >> 1) & 0x3]); |
| 318 | } |
| 319 | |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 320 | // TODO: zipinfo (unlike unzip) sometimes uses time zone? |
| 321 | // TODO: this uses 4-digit years because we're not barbarians unless interoperability forces it. |
| 322 | tm t = entry.GetModificationTime(); |
| 323 | char time[32]; |
| 324 | snprintf(time, sizeof(time), "%04d-%02d-%02d %02d:%02d", t.tm_year + 1900, t.tm_mon + 1, |
| 325 | t.tm_mday, t.tm_hour, t.tm_min); |
| 326 | |
| 327 | // "-rw-r--r-- 3.0 unx 577 t- defX 19-Feb-12 16:09 android-ndk-r19b/sources/android/NOTICE" |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 328 | printf("%s %2d.%d %s %8d %c%c %s %s %s\n", mode, version / 10, version % 10, src_fs, |
| 329 | entry.uncompressed_length, entry.is_text ? 't' : 'b', |
| 330 | entry.has_data_descriptor ? 'X' : 'x', method, time, name.c_str()); |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 333 | static void ProcessOne(ZipArchiveHandle zah, ZipEntry& entry, const std::string& name) { |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 334 | if (role == kUnzip) { |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 335 | if (flag_l || flag_v) { |
| 336 | // -l or -lv or -lq or -v. |
| 337 | ListOne(entry, name); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 338 | } else { |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 339 | // Actually extract. |
| 340 | if (flag_p) { |
| 341 | ExtractToPipe(zah, entry, name); |
| 342 | } else { |
| 343 | ExtractOne(zah, entry, name); |
| 344 | } |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 345 | } |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 346 | } else { |
| 347 | // zipinfo or zipinfo -1. |
| 348 | InfoOne(entry, name); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 349 | } |
| 350 | total_uncompressed_length += entry.uncompressed_length; |
| 351 | total_compressed_length += entry.compressed_length; |
| 352 | ++file_count; |
| 353 | } |
| 354 | |
| 355 | static void ProcessAll(ZipArchiveHandle zah) { |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 356 | MaybeShowHeader(zah); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 357 | |
| 358 | // libziparchive iteration order doesn't match the central directory. |
| 359 | // We could sort, but that would cost extra and wouldn't match either. |
| 360 | void* cookie; |
Elliott Hughes | a22ac0f | 2019-05-08 10:44:06 -0700 | [diff] [blame] | 361 | int err = StartIteration(zah, &cookie); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 362 | if (err != 0) { |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 363 | die(0, "couldn't iterate %s: %s", archive_name, ErrorCodeString(err)); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | ZipEntry entry; |
Elliott Hughes | e06a808 | 2019-05-22 18:56:41 -0700 | [diff] [blame] | 367 | std::string name; |
| 368 | while ((err = Next(cookie, &entry, &name)) >= 0) { |
Elliott Hughes | 5f8b309 | 2019-04-08 12:39:20 -0700 | [diff] [blame] | 369 | if (ShouldInclude(name)) ProcessOne(zah, entry, name); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 372 | if (err < -1) die(0, "failed iterating %s: %s", archive_name, ErrorCodeString(err)); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 373 | EndIteration(cookie); |
| 374 | |
| 375 | MaybeShowFooter(); |
| 376 | } |
| 377 | |
| 378 | static void ShowHelp(bool full) { |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 379 | if (role == kUnzip) { |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 380 | fprintf(full ? stdout : stderr, "usage: unzip [-d DIR] [-lnopqv] ZIP [FILE...] [-x FILE...]\n"); |
| 381 | if (!full) exit(EXIT_FAILURE); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 382 | |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 383 | printf( |
| 384 | "\n" |
| 385 | "Extract FILEs from ZIP archive. Default is all files. Both the include and\n" |
| 386 | "exclude (-x) lists use shell glob patterns.\n" |
| 387 | "\n" |
| 388 | "-d DIR Extract into DIR\n" |
| 389 | "-l List contents (-lq excludes archive name, -lv is verbose)\n" |
| 390 | "-n Never overwrite files (default: prompt)\n" |
| 391 | "-o Always overwrite files\n" |
| 392 | "-p Pipe to stdout\n" |
| 393 | "-q Quiet\n" |
| 394 | "-v List contents verbosely\n" |
| 395 | "-x FILE Exclude files\n"); |
| 396 | } else { |
| 397 | fprintf(full ? stdout : stderr, "usage: zipinfo [-1] ZIP [FILE...] [-x FILE...]\n"); |
| 398 | if (!full) exit(EXIT_FAILURE); |
| 399 | |
| 400 | printf( |
| 401 | "\n" |
| 402 | "Show information about FILEs from ZIP archive. Default is all files.\n" |
| 403 | "Both the include and exclude (-x) lists use shell glob patterns.\n" |
| 404 | "\n" |
| 405 | "-1 Show filenames only, one per line\n" |
| 406 | "-x FILE Exclude files\n"); |
| 407 | } |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 408 | exit(EXIT_SUCCESS); |
| 409 | } |
| 410 | |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 411 | static void HandleCommonOption(int opt) { |
| 412 | switch (opt) { |
| 413 | case 'h': |
| 414 | ShowHelp(true); |
| 415 | break; |
| 416 | case 'x': |
| 417 | flag_x = true; |
| 418 | break; |
| 419 | case 1: |
| 420 | // -x swallows all following arguments, so we use '-' in the getopt |
| 421 | // string and collect files here. |
| 422 | if (!archive_name) { |
| 423 | archive_name = optarg; |
| 424 | } else if (flag_x) { |
| 425 | excludes.insert(optarg); |
| 426 | } else { |
| 427 | includes.insert(optarg); |
| 428 | } |
| 429 | break; |
| 430 | default: |
| 431 | ShowHelp(false); |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 436 | int main(int argc, char* argv[]) { |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 437 | // Who am I, and what am I doing? |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 438 | g_progname = basename(argv[0]); |
| 439 | if (!strcmp(g_progname, "ziptool") && argc > 1) return main(argc - 1, argv + 1); |
| 440 | if (!strcmp(g_progname, "unzip")) { |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 441 | role = kUnzip; |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 442 | } else if (!strcmp(g_progname, "zipinfo")) { |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 443 | role = kZipinfo; |
| 444 | } else { |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 445 | die(0, "run as ziptool with unzip or zipinfo as the first argument, or symlink"); |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static const struct option opts[] = { |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 449 | {"help", no_argument, 0, 'h'}, |
| 450 | }; |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 451 | |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 452 | if (role == kUnzip) { |
Elliott Hughes | d3aee66 | 2019-10-29 20:47:16 -0700 | [diff] [blame] | 453 | // `unzip -Z` is "zipinfo mode", so in that case just restart... |
| 454 | if (argc > 1 && !strcmp(argv[1], "-Z")) { |
| 455 | argv[1] = const_cast<char*>("zipinfo"); |
| 456 | return main(argc - 1, argv + 1); |
| 457 | } |
| 458 | |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 459 | int opt; |
| 460 | while ((opt = getopt_long(argc, argv, "-d:hlnopqvx", opts, nullptr)) != -1) { |
| 461 | switch (opt) { |
| 462 | case 'd': |
| 463 | flag_d = optarg; |
| 464 | break; |
| 465 | case 'l': |
| 466 | flag_l = true; |
| 467 | break; |
| 468 | case 'n': |
| 469 | overwrite_mode = kNever; |
| 470 | break; |
| 471 | case 'o': |
| 472 | overwrite_mode = kAlways; |
| 473 | break; |
| 474 | case 'p': |
| 475 | flag_p = flag_q = true; |
| 476 | break; |
| 477 | case 'q': |
| 478 | flag_q = true; |
| 479 | break; |
| 480 | case 'v': |
| 481 | flag_v = true; |
| 482 | break; |
| 483 | default: |
| 484 | HandleCommonOption(opt); |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | } else { |
| 489 | int opt; |
| 490 | while ((opt = getopt_long(argc, argv, "-1hx", opts, nullptr)) != -1) { |
| 491 | switch (opt) { |
| 492 | case '1': |
| 493 | flag_1 = true; |
| 494 | break; |
| 495 | default: |
| 496 | HandleCommonOption(opt); |
| 497 | break; |
| 498 | } |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 502 | if (!archive_name) die(0, "missing archive filename"); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 503 | |
| 504 | // We can't support "-" to unzip from stdin because libziparchive relies on mmap. |
| 505 | ZipArchiveHandle zah; |
| 506 | int32_t err; |
| 507 | if ((err = OpenArchive(archive_name, &zah)) != 0) { |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 508 | die(0, "couldn't open %s: %s", archive_name, ErrorCodeString(err)); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | // Implement -d by changing into that directory. |
| 512 | // We'll create implicit directories based on paths in the zip file, but we |
| 513 | // require that the -d directory already exists. |
Elliott Hughes | bcd8106 | 2019-11-03 08:30:33 -0800 | [diff] [blame] | 514 | if (flag_d && chdir(flag_d) == -1) die(errno, "couldn't chdir to %s", flag_d); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 515 | |
| 516 | ProcessAll(zah); |
| 517 | |
| 518 | CloseArchive(zah); |
| 519 | return 0; |
| 520 | } |