| 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 |  | 
|  | 37 | enum OverwriteMode { | 
|  | 38 | kAlways, | 
|  | 39 | kNever, | 
|  | 40 | kPrompt, | 
|  | 41 | }; | 
|  | 42 |  | 
|  | 43 | static OverwriteMode overwrite_mode = kPrompt; | 
|  | 44 | static const char* flag_d = nullptr; | 
|  | 45 | static bool flag_l = false; | 
|  | 46 | static bool flag_p = false; | 
|  | 47 | static bool flag_q = false; | 
|  | 48 | static bool flag_v = false; | 
|  | 49 | static const char* archive_name = nullptr; | 
|  | 50 | static std::set<std::string> includes; | 
|  | 51 | static std::set<std::string> excludes; | 
|  | 52 | static uint64_t total_uncompressed_length = 0; | 
|  | 53 | static uint64_t total_compressed_length = 0; | 
|  | 54 | static size_t file_count = 0; | 
|  | 55 |  | 
| Elliott Hughes | 5f8b309 | 2019-04-08 12:39:20 -0700 | [diff] [blame] | 56 | static bool ShouldInclude(const std::string& name) { | 
|  | 57 | // Explicitly excluded? | 
|  | 58 | if (!excludes.empty()) { | 
|  | 59 | for (const auto& exclude : excludes) { | 
|  | 60 | if (!fnmatch(exclude.c_str(), name.c_str(), 0)) return false; | 
|  | 61 | } | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | // Implicitly included? | 
|  | 65 | if (includes.empty()) return true; | 
|  | 66 |  | 
|  | 67 | // Explicitly included? | 
|  | 68 | for (const auto& include : includes) { | 
|  | 69 | if (!fnmatch(include.c_str(), name.c_str(), 0)) return true; | 
|  | 70 | } | 
| Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 71 | return false; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | static bool MakeDirectoryHierarchy(const std::string& path) { | 
|  | 75 | // stat rather than lstat because a symbolic link to a directory is fine too. | 
|  | 76 | struct stat sb; | 
|  | 77 | if (stat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode)) return true; | 
|  | 78 |  | 
|  | 79 | // Ensure the parent directories exist first. | 
|  | 80 | if (!MakeDirectoryHierarchy(android::base::Dirname(path))) return false; | 
|  | 81 |  | 
|  | 82 | // Then try to create this directory. | 
|  | 83 | return (mkdir(path.c_str(), 0777) != -1); | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | static int CompressionRatio(int64_t uncompressed, int64_t compressed) { | 
|  | 87 | if (uncompressed == 0) return 0; | 
| Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 88 | return static_cast<int>((100LL * (uncompressed - compressed)) / uncompressed); | 
| Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 89 | } | 
|  | 90 |  | 
|  | 91 | static void MaybeShowHeader() { | 
|  | 92 | if (!flag_q) printf("Archive:  %s\n", archive_name); | 
|  | 93 | if (flag_v) { | 
|  | 94 | printf( | 
|  | 95 | " Length   Method    Size  Cmpr    Date    Time   CRC-32   Name\n" | 
|  | 96 | "--------  ------  ------- ---- ---------- ----- --------  ----\n"); | 
|  | 97 | } else if (flag_l) { | 
|  | 98 | printf( | 
|  | 99 | "  Length      Date    Time    Name\n" | 
|  | 100 | "---------  ---------- -----   ----\n"); | 
|  | 101 | } | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | static void MaybeShowFooter() { | 
|  | 105 | if (flag_v) { | 
|  | 106 | printf( | 
|  | 107 | "--------          -------  ---                            -------\n" | 
|  | 108 | "%8" PRId64 "         %8" PRId64 " %3d%%                            %zu file%s\n", | 
|  | 109 | total_uncompressed_length, total_compressed_length, | 
|  | 110 | CompressionRatio(total_uncompressed_length, total_compressed_length), file_count, | 
|  | 111 | (file_count == 1) ? "" : "s"); | 
|  | 112 | } else if (flag_l) { | 
|  | 113 | printf( | 
|  | 114 | "---------                     -------\n" | 
|  | 115 | "%9" PRId64 "                     %zu file%s\n", | 
|  | 116 | total_uncompressed_length, file_count, (file_count == 1) ? "" : "s"); | 
|  | 117 | } | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | static bool PromptOverwrite(const std::string& dst) { | 
|  | 121 | // TODO: [r]ename not implemented because it doesn't seem useful. | 
|  | 122 | printf("replace %s? [y]es, [n]o, [A]ll, [N]one: ", dst.c_str()); | 
|  | 123 | fflush(stdout); | 
|  | 124 | while (true) { | 
|  | 125 | char* line = nullptr; | 
|  | 126 | size_t n; | 
|  | 127 | if (getline(&line, &n, stdin) == -1) { | 
|  | 128 | error(1, 0, "(EOF/read error; assuming [N]one...)"); | 
|  | 129 | overwrite_mode = kNever; | 
|  | 130 | return false; | 
|  | 131 | } | 
|  | 132 | if (n == 0) continue; | 
|  | 133 | char cmd = line[0]; | 
|  | 134 | free(line); | 
|  | 135 | switch (cmd) { | 
|  | 136 | case 'y': | 
|  | 137 | return true; | 
|  | 138 | case 'n': | 
|  | 139 | return false; | 
|  | 140 | case 'A': | 
|  | 141 | overwrite_mode = kAlways; | 
|  | 142 | return true; | 
|  | 143 | case 'N': | 
|  | 144 | overwrite_mode = kNever; | 
|  | 145 | return false; | 
|  | 146 | } | 
|  | 147 | } | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | static void ExtractToPipe(ZipArchiveHandle zah, ZipEntry& entry, const std::string& name) { | 
|  | 151 | // We need to extract to memory because ExtractEntryToFile insists on | 
|  | 152 | // being able to seek and truncate, and you can't do that with stdout. | 
|  | 153 | uint8_t* buffer = new uint8_t[entry.uncompressed_length]; | 
|  | 154 | int err = ExtractToMemory(zah, &entry, buffer, entry.uncompressed_length); | 
|  | 155 | if (err < 0) { | 
|  | 156 | error(1, 0, "failed to extract %s: %s", name.c_str(), ErrorCodeString(err)); | 
|  | 157 | } | 
|  | 158 | if (!android::base::WriteFully(1, buffer, entry.uncompressed_length)) { | 
|  | 159 | error(1, errno, "failed to write %s to stdout", name.c_str()); | 
|  | 160 | } | 
|  | 161 | delete[] buffer; | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | static void ExtractOne(ZipArchiveHandle zah, ZipEntry& entry, const std::string& name) { | 
|  | 165 | // Bad filename? | 
|  | 166 | if (android::base::StartsWith(name, "/") || android::base::StartsWith(name, "../") || | 
|  | 167 | name.find("/../") != std::string::npos) { | 
|  | 168 | error(1, 0, "bad filename %s", name.c_str()); | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | // Where are we actually extracting to (for human-readable output)? | 
|  | 172 | std::string dst; | 
|  | 173 | if (flag_d) { | 
|  | 174 | dst = flag_d; | 
|  | 175 | if (!android::base::EndsWith(dst, "/")) dst += '/'; | 
|  | 176 | } | 
|  | 177 | dst += name; | 
|  | 178 |  | 
|  | 179 | // Ensure the directory hierarchy exists. | 
|  | 180 | if (!MakeDirectoryHierarchy(android::base::Dirname(name))) { | 
|  | 181 | error(1, errno, "couldn't create directory hierarchy for %s", dst.c_str()); | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | // An entry in a zip file can just be a directory itself. | 
|  | 185 | if (android::base::EndsWith(name, "/")) { | 
|  | 186 | if (mkdir(name.c_str(), entry.unix_mode) == -1) { | 
|  | 187 | // If the directory already exists, that's fine. | 
|  | 188 | if (errno == EEXIST) { | 
|  | 189 | struct stat sb; | 
|  | 190 | if (stat(name.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode)) return; | 
|  | 191 | } | 
|  | 192 | error(1, errno, "couldn't extract directory %s", dst.c_str()); | 
|  | 193 | } | 
|  | 194 | return; | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | // Create the file. | 
|  | 198 | int fd = open(name.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC | O_EXCL, entry.unix_mode); | 
|  | 199 | if (fd == -1 && errno == EEXIST) { | 
|  | 200 | if (overwrite_mode == kNever) return; | 
|  | 201 | if (overwrite_mode == kPrompt && !PromptOverwrite(dst)) return; | 
|  | 202 | // Either overwrite_mode is kAlways or the user consented to this specific case. | 
|  | 203 | fd = open(name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, entry.unix_mode); | 
|  | 204 | } | 
|  | 205 | if (fd == -1) error(1, errno, "couldn't create file %s", dst.c_str()); | 
|  | 206 |  | 
|  | 207 | // Actually extract into the file. | 
|  | 208 | if (!flag_q) printf("  inflating: %s\n", dst.c_str()); | 
|  | 209 | int err = ExtractEntryToFile(zah, &entry, fd); | 
|  | 210 | if (err < 0) error(1, 0, "failed to extract %s: %s", dst.c_str(), ErrorCodeString(err)); | 
|  | 211 | close(fd); | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | static void ListOne(const ZipEntry& entry, const std::string& name) { | 
|  | 215 | tm t = entry.GetModificationTime(); | 
|  | 216 | char time[32]; | 
|  | 217 | snprintf(time, sizeof(time), "%04d-%02d-%02d %02d:%02d", t.tm_year + 1900, t.tm_mon + 1, | 
|  | 218 | t.tm_mday, t.tm_hour, t.tm_min); | 
|  | 219 | if (flag_v) { | 
|  | 220 | printf("%8d  %s  %7d %3d%% %s %08x  %s\n", entry.uncompressed_length, | 
|  | 221 | (entry.method == kCompressStored) ? "Stored" : "Defl:N", entry.compressed_length, | 
|  | 222 | CompressionRatio(entry.uncompressed_length, entry.compressed_length), time, entry.crc32, | 
|  | 223 | name.c_str()); | 
|  | 224 | } else { | 
|  | 225 | printf("%9d  %s   %s\n", entry.uncompressed_length, time, name.c_str()); | 
|  | 226 | } | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | static void ProcessOne(ZipArchiveHandle zah, ZipEntry& entry, const std::string& name) { | 
|  | 230 | if (flag_l || flag_v) { | 
|  | 231 | // -l or -lv or -lq or -v. | 
|  | 232 | ListOne(entry, name); | 
|  | 233 | } else { | 
|  | 234 | // Actually extract. | 
|  | 235 | if (flag_p) { | 
|  | 236 | ExtractToPipe(zah, entry, name); | 
|  | 237 | } else { | 
|  | 238 | ExtractOne(zah, entry, name); | 
|  | 239 | } | 
|  | 240 | } | 
|  | 241 | total_uncompressed_length += entry.uncompressed_length; | 
|  | 242 | total_compressed_length += entry.compressed_length; | 
|  | 243 | ++file_count; | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 | static void ProcessAll(ZipArchiveHandle zah) { | 
|  | 247 | MaybeShowHeader(); | 
|  | 248 |  | 
|  | 249 | // libziparchive iteration order doesn't match the central directory. | 
|  | 250 | // We could sort, but that would cost extra and wouldn't match either. | 
|  | 251 | void* cookie; | 
| Elliott Hughes | a22ac0f | 2019-05-08 10:44:06 -0700 | [diff] [blame] | 252 | int err = StartIteration(zah, &cookie); | 
| Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 253 | if (err != 0) { | 
|  | 254 | error(1, 0, "couldn't iterate %s: %s", archive_name, ErrorCodeString(err)); | 
|  | 255 | } | 
|  | 256 |  | 
|  | 257 | ZipEntry entry; | 
| Elliott Hughes | e06a808 | 2019-05-22 18:56:41 -0700 | [diff] [blame] | 258 | std::string name; | 
|  | 259 | while ((err = Next(cookie, &entry, &name)) >= 0) { | 
| Elliott Hughes | 5f8b309 | 2019-04-08 12:39:20 -0700 | [diff] [blame] | 260 | if (ShouldInclude(name)) ProcessOne(zah, entry, name); | 
| Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 261 | } | 
|  | 262 |  | 
|  | 263 | if (err < -1) error(1, 0, "failed iterating %s: %s", archive_name, ErrorCodeString(err)); | 
|  | 264 | EndIteration(cookie); | 
|  | 265 |  | 
|  | 266 | MaybeShowFooter(); | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | static void ShowHelp(bool full) { | 
|  | 270 | fprintf(full ? stdout : stderr, "usage: unzip [-d DIR] [-lnopqv] ZIP [FILE...] [-x FILE...]\n"); | 
|  | 271 | if (!full) exit(EXIT_FAILURE); | 
|  | 272 |  | 
|  | 273 | printf( | 
|  | 274 | "\n" | 
| Elliott Hughes | 5f8b309 | 2019-04-08 12:39:20 -0700 | [diff] [blame] | 275 | "Extract FILEs from ZIP archive. Default is all files. Both the include and\n" | 
|  | 276 | "exclude (-x) lists use shell glob patterns.\n" | 
| Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 277 | "\n" | 
|  | 278 | "-d DIR	Extract into DIR\n" | 
|  | 279 | "-l	List contents (-lq excludes archive name, -lv is verbose)\n" | 
|  | 280 | "-n	Never overwrite files (default: prompt)\n" | 
|  | 281 | "-o	Always overwrite files\n" | 
|  | 282 | "-p	Pipe to stdout\n" | 
|  | 283 | "-q	Quiet\n" | 
|  | 284 | "-v	List contents verbosely\n" | 
|  | 285 | "-x FILE	Exclude files\n"); | 
|  | 286 | exit(EXIT_SUCCESS); | 
|  | 287 | } | 
|  | 288 |  | 
|  | 289 | int main(int argc, char* argv[]) { | 
|  | 290 | static struct option opts[] = { | 
|  | 291 | {"help", no_argument, 0, 'h'}, | 
|  | 292 | }; | 
|  | 293 | bool saw_x = false; | 
|  | 294 | int opt; | 
|  | 295 | while ((opt = getopt_long(argc, argv, "-d:hlnopqvx", opts, nullptr)) != -1) { | 
|  | 296 | switch (opt) { | 
|  | 297 | case 'd': | 
|  | 298 | flag_d = optarg; | 
|  | 299 | break; | 
|  | 300 | case 'h': | 
|  | 301 | ShowHelp(true); | 
|  | 302 | break; | 
|  | 303 | case 'l': | 
|  | 304 | flag_l = true; | 
|  | 305 | break; | 
|  | 306 | case 'n': | 
|  | 307 | overwrite_mode = kNever; | 
|  | 308 | break; | 
|  | 309 | case 'o': | 
|  | 310 | overwrite_mode = kAlways; | 
|  | 311 | break; | 
|  | 312 | case 'p': | 
|  | 313 | flag_p = flag_q = true; | 
|  | 314 | break; | 
|  | 315 | case 'q': | 
|  | 316 | flag_q = true; | 
|  | 317 | break; | 
|  | 318 | case 'v': | 
|  | 319 | flag_v = true; | 
|  | 320 | break; | 
|  | 321 | case 'x': | 
|  | 322 | saw_x = true; | 
|  | 323 | break; | 
|  | 324 | case 1: | 
|  | 325 | // -x swallows all following arguments, so we use '-' in the getopt | 
|  | 326 | // string and collect files here. | 
|  | 327 | if (!archive_name) { | 
|  | 328 | archive_name = optarg; | 
|  | 329 | } else if (saw_x) { | 
|  | 330 | excludes.insert(optarg); | 
|  | 331 | } else { | 
|  | 332 | includes.insert(optarg); | 
|  | 333 | } | 
|  | 334 | break; | 
|  | 335 | default: | 
|  | 336 | ShowHelp(false); | 
|  | 337 | } | 
|  | 338 | } | 
|  | 339 |  | 
|  | 340 | if (!archive_name) error(1, 0, "missing archive filename"); | 
|  | 341 |  | 
|  | 342 | // We can't support "-" to unzip from stdin because libziparchive relies on mmap. | 
|  | 343 | ZipArchiveHandle zah; | 
|  | 344 | int32_t err; | 
|  | 345 | if ((err = OpenArchive(archive_name, &zah)) != 0) { | 
|  | 346 | error(1, 0, "couldn't open %s: %s", archive_name, ErrorCodeString(err)); | 
|  | 347 | } | 
|  | 348 |  | 
|  | 349 | // Implement -d by changing into that directory. | 
|  | 350 | // We'll create implicit directories based on paths in the zip file, but we | 
|  | 351 | // require that the -d directory already exists. | 
|  | 352 | if (flag_d && chdir(flag_d) == -1) error(1, errno, "couldn't chdir to %s", flag_d); | 
|  | 353 |  | 
|  | 354 | ProcessAll(zah); | 
|  | 355 |  | 
|  | 356 | CloseArchive(zah); | 
|  | 357 | return 0; | 
|  | 358 | } |