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> |
| 18 | #include <error.h> |
| 19 | #include <fcntl.h> |
Elliott Hughes | 5f8b309 | 2019-04-08 12:39:20 -0700 | [diff] [blame] | 20 | #include <fnmatch.h> |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 21 | #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 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 | 5f8b309 | 2019-04-08 12:39:20 -0700 | [diff] [blame] | 67 | static 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 Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | |
| 85 | static 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 | |
| 97 | static int CompressionRatio(int64_t uncompressed, int64_t compressed) { |
| 98 | if (uncompressed == 0) return 0; |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 99 | return static_cast<int>((100LL * (uncompressed - compressed)) / uncompressed); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 102 | static void MaybeShowHeader(ZipArchiveHandle zah) { |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame^] | 103 | if (role == kUnzip) { |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 104 | // 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 Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | static void MaybeShowFooter() { |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame^] | 127 | if (role == kUnzip) { |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 128 | 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 Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | static 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 | |
| 180 | static 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 | |
| 194 | static void ExtractOne(ZipArchiveHandle zah, ZipEntry& entry, const std::string& name) { |
| 195 | // Bad filename? |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame^] | 196 | if (StartsWith(name, "/") || StartsWith(name, "../") || name.find("/../") != std::string::npos) { |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 197 | 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 Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame^] | 204 | if (!EndsWith(dst, "/")) dst += '/'; |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 205 | } |
| 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 Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame^] | 214 | if (EndsWith(name, "/")) { |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 215 | 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 | |
| 243 | static 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 Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 258 | static 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 Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame^] | 268 | // 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 Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 288 | 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 Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame^] | 300 | char method[5] = "stor"; |
| 301 | if (entry.method == kCompressDeflated) { |
| 302 | snprintf(method, sizeof(method), "def%c", "NXFS"[(entry.gpbf >> 1) & 0x3]); |
| 303 | } |
| 304 | |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 305 | // 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 Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame^] | 313 | 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 Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 318 | static void ProcessOne(ZipArchiveHandle zah, ZipEntry& entry, const std::string& name) { |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame^] | 319 | if (role == kUnzip) { |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 320 | if (flag_l || flag_v) { |
| 321 | // -l or -lv or -lq or -v. |
| 322 | ListOne(entry, name); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 323 | } else { |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 324 | // Actually extract. |
| 325 | if (flag_p) { |
| 326 | ExtractToPipe(zah, entry, name); |
| 327 | } else { |
| 328 | ExtractOne(zah, entry, name); |
| 329 | } |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 330 | } |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 331 | } else { |
| 332 | // zipinfo or zipinfo -1. |
| 333 | InfoOne(entry, name); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 334 | } |
| 335 | total_uncompressed_length += entry.uncompressed_length; |
| 336 | total_compressed_length += entry.compressed_length; |
| 337 | ++file_count; |
| 338 | } |
| 339 | |
| 340 | static void ProcessAll(ZipArchiveHandle zah) { |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 341 | MaybeShowHeader(zah); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 342 | |
| 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 Hughes | a22ac0f | 2019-05-08 10:44:06 -0700 | [diff] [blame] | 346 | int err = StartIteration(zah, &cookie); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 347 | if (err != 0) { |
| 348 | error(1, 0, "couldn't iterate %s: %s", archive_name, ErrorCodeString(err)); |
| 349 | } |
| 350 | |
| 351 | ZipEntry entry; |
Elliott Hughes | e06a808 | 2019-05-22 18:56:41 -0700 | [diff] [blame] | 352 | std::string name; |
| 353 | while ((err = Next(cookie, &entry, &name)) >= 0) { |
Elliott Hughes | 5f8b309 | 2019-04-08 12:39:20 -0700 | [diff] [blame] | 354 | if (ShouldInclude(name)) ProcessOne(zah, entry, name); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | if (err < -1) error(1, 0, "failed iterating %s: %s", archive_name, ErrorCodeString(err)); |
| 358 | EndIteration(cookie); |
| 359 | |
| 360 | MaybeShowFooter(); |
| 361 | } |
| 362 | |
| 363 | static void ShowHelp(bool full) { |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame^] | 364 | if (role == kUnzip) { |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 365 | fprintf(full ? stdout : stderr, "usage: unzip [-d DIR] [-lnopqv] ZIP [FILE...] [-x FILE...]\n"); |
| 366 | if (!full) exit(EXIT_FAILURE); |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 367 | |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 368 | 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 Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 393 | exit(EXIT_SUCCESS); |
| 394 | } |
| 395 | |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 396 | static 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 Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 421 | int main(int argc, char* argv[]) { |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame^] | 422 | // 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 Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 434 | {"help", no_argument, 0, 'h'}, |
| 435 | }; |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 436 | |
Elliott Hughes | d509525 | 2019-10-28 21:35:52 -0700 | [diff] [blame^] | 437 | if (role == kUnzip) { |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 438 | int opt; |
| 439 | while ((opt = getopt_long(argc, argv, "-d:hlnopqvx", opts, nullptr)) != -1) { |
| 440 | switch (opt) { |
| 441 | case 'd': |
| 442 | flag_d = optarg; |
| 443 | break; |
| 444 | case 'l': |
| 445 | flag_l = true; |
| 446 | break; |
| 447 | case 'n': |
| 448 | overwrite_mode = kNever; |
| 449 | break; |
| 450 | case 'o': |
| 451 | overwrite_mode = kAlways; |
| 452 | break; |
| 453 | case 'p': |
| 454 | flag_p = flag_q = true; |
| 455 | break; |
| 456 | case 'q': |
| 457 | flag_q = true; |
| 458 | break; |
| 459 | case 'v': |
| 460 | flag_v = true; |
| 461 | break; |
| 462 | default: |
| 463 | HandleCommonOption(opt); |
| 464 | break; |
| 465 | } |
| 466 | } |
| 467 | } else { |
| 468 | int opt; |
| 469 | while ((opt = getopt_long(argc, argv, "-1hx", opts, nullptr)) != -1) { |
| 470 | switch (opt) { |
| 471 | case '1': |
| 472 | flag_1 = true; |
| 473 | break; |
| 474 | default: |
| 475 | HandleCommonOption(opt); |
| 476 | break; |
| 477 | } |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
| 481 | if (!archive_name) error(1, 0, "missing archive filename"); |
| 482 | |
| 483 | // We can't support "-" to unzip from stdin because libziparchive relies on mmap. |
| 484 | ZipArchiveHandle zah; |
| 485 | int32_t err; |
| 486 | if ((err = OpenArchive(archive_name, &zah)) != 0) { |
| 487 | error(1, 0, "couldn't open %s: %s", archive_name, ErrorCodeString(err)); |
| 488 | } |
| 489 | |
| 490 | // Implement -d by changing into that directory. |
| 491 | // We'll create implicit directories based on paths in the zip file, but we |
| 492 | // require that the -d directory already exists. |
| 493 | if (flag_d && chdir(flag_d) == -1) error(1, errno, "couldn't chdir to %s", flag_d); |
| 494 | |
| 495 | ProcessAll(zah); |
| 496 | |
| 497 | CloseArchive(zah); |
| 498 | return 0; |
| 499 | } |