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