zip_main.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // Copyright 2015 The Bazel Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Zip / Unzip file using ijar zip implementation.
  16. //
  17. // Note that this Zip implementation intentionally don't compute CRC-32
  18. // because it is useless computation for jar because Java doesn't care.
  19. // CRC-32 of all files in the zip file will be set to 0.
  20. //
  21. #include <errno.h>
  22. #include <limits.h>
  23. #include <stdint.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <memory>
  28. #include <set>
  29. #include <string>
  30. #include "third_party/ijar/platform_utils.h"
  31. #include "third_party/ijar/zip.h"
  32. namespace devtools_ijar {
  33. //
  34. // A ZipExtractorProcessor that extract files in the ZIP file.
  35. //
  36. class UnzipProcessor : public ZipExtractorProcessor {
  37. public:
  38. // Create a processor who will extract the given files (or all files if NULL)
  39. // into output_root if "extract" is set to true and will print the list of
  40. // files and their unix modes if "verbose" is set to true.
  41. UnzipProcessor(const char *output_root, char **files, bool verbose,
  42. bool extract, bool flatten)
  43. : output_root_(output_root),
  44. verbose_(verbose),
  45. extract_(extract),
  46. flatten_(flatten) {
  47. if (files != NULL) {
  48. for (int i = 0; files[i] != NULL; i++) {
  49. file_names.insert(std::string(files[i]));
  50. }
  51. }
  52. }
  53. virtual ~UnzipProcessor() {}
  54. virtual void Process(const char* filename, const u4 attr,
  55. const u1* data, const size_t size);
  56. virtual bool Accept(const char* filename, const u4 attr) {
  57. // All entry files are accepted by default.
  58. if (file_names.empty()) {
  59. return true;
  60. } else {
  61. // If users have specified file entries, only accept those files.
  62. return file_names.count(std::string(filename)) == 1;
  63. }
  64. }
  65. private:
  66. const char *output_root_;
  67. const bool verbose_;
  68. const bool extract_;
  69. const bool flatten_;
  70. std::set<std::string> file_names;
  71. };
  72. // Concatene 2 path, path1 and path2, using / as a directory separator and
  73. // putting the result in "out". "size" specify the size of the output buffer. If
  74. // the result would overflow the output buffer, print an error message and
  75. // return false.
  76. bool concat_path(char *out, const size_t size, const char *path1,
  77. const char *path2) {
  78. int len1 = strlen(path1);
  79. size_t l = len1;
  80. strncpy(out, path1, size - 1);
  81. out[size - 1] = 0;
  82. if (l < size - 1 && path1[len1] != '/' && path2[0] != '/') {
  83. out[l] = '/';
  84. l++;
  85. out[l] = 0;
  86. }
  87. if (l >= size - 1) {
  88. fprintf(stderr, "paths too long to concat: %s + %s", path1, path2);
  89. return false;
  90. }
  91. strncat(out, path2, size - 1 - l);
  92. return true;
  93. }
  94. void UnzipProcessor::Process(const char* filename, const u4 attr,
  95. const u1* data, const size_t size) {
  96. mode_t perm = zipattr_to_perm(attr);
  97. bool isdir = zipattr_is_dir(attr);
  98. const char *output_file_name = filename;
  99. if (attr == 0) {
  100. // Fallback when the external attribute is not set.
  101. isdir = filename[strlen(filename)-1] == '/';
  102. perm = 0777;
  103. }
  104. if (flatten_) {
  105. if (isdir) {
  106. return;
  107. }
  108. const char *p = strrchr(filename, '/');
  109. if (p != NULL) {
  110. output_file_name = p + 1;
  111. }
  112. }
  113. if (verbose_) {
  114. printf("%c %o %s\n", isdir ? 'd' : 'f', perm, output_file_name);
  115. }
  116. if (extract_) {
  117. char path[PATH_MAX];
  118. if (!concat_path(path, sizeof(path), output_root_, output_file_name) ||
  119. !make_dirs(path, perm) ||
  120. (!isdir && !write_file(path, perm, data, size))) {
  121. abort();
  122. }
  123. }
  124. }
  125. // Get the basename of path and store it in output. output_size
  126. // is the size of the output buffer.
  127. void basename(const char *path, char *output, size_t output_size) {
  128. const char *pointer = strrchr(path, '/');
  129. if (pointer == NULL) {
  130. pointer = path;
  131. } else {
  132. pointer++; // Skip the leading slash.
  133. }
  134. strncpy(output, pointer, output_size - 1);
  135. output[output_size - 1] = 0;
  136. }
  137. // Execute the extraction (or just listing if just v is provided)
  138. int extract(char *zipfile, char *exdir, char **files, bool verbose,
  139. bool extract, bool flatten) {
  140. std::string cwd = get_cwd();
  141. if (cwd.empty()) {
  142. return -1;
  143. }
  144. char output_root[PATH_MAX + 1];
  145. if (exdir != NULL) {
  146. if (!concat_path(output_root, sizeof(output_root), cwd.c_str(), exdir)) {
  147. return -1;
  148. }
  149. } else if (cwd.length() >= sizeof(output_root)) {
  150. fprintf(stderr, "current working directory path too long");
  151. return -1;
  152. } else {
  153. memcpy(output_root, cwd.c_str(), cwd.length() + 1);
  154. }
  155. UnzipProcessor processor(output_root, files, verbose, extract, flatten);
  156. std::unique_ptr<ZipExtractor> extractor(ZipExtractor::Create(zipfile,
  157. &processor));
  158. if (extractor == NULL) {
  159. fprintf(stderr, "Unable to open zip file %s: %s.\n", zipfile,
  160. strerror(errno));
  161. return -1;
  162. }
  163. if (extractor->ProcessAll() < 0) {
  164. fprintf(stderr, "%s.\n", extractor->GetError());
  165. return -1;
  166. }
  167. return 0;
  168. }
  169. // add a file to the zip
  170. int add_file(std::unique_ptr<ZipBuilder> const &builder, char *file,
  171. char *zip_path, bool flatten, bool verbose, bool compress) {
  172. Stat file_stat = {0, 0666, false};
  173. if (file != NULL) {
  174. if (!stat_file(file, &file_stat)) {
  175. fprintf(stderr, "Cannot stat file %s: %s\n", file, strerror(errno));
  176. return -1;
  177. }
  178. }
  179. char *final_path = zip_path != NULL ? zip_path : file;
  180. bool isdir = file_stat.is_directory;
  181. if (flatten && isdir) {
  182. return 0;
  183. }
  184. // Compute the path, flattening it if requested
  185. char path[PATH_MAX];
  186. size_t len = strlen(final_path);
  187. if (len > PATH_MAX) {
  188. fprintf(stderr, "Path too long: %s.\n", final_path);
  189. return -1;
  190. }
  191. if (flatten) {
  192. basename(final_path, path, PATH_MAX);
  193. } else {
  194. strncpy(path, final_path, PATH_MAX);
  195. path[PATH_MAX - 1] = 0;
  196. if (isdir && len < PATH_MAX - 1) {
  197. // Add the trailing slash for folders
  198. path[len] = '/';
  199. path[len + 1] = 0;
  200. }
  201. }
  202. if (verbose) {
  203. mode_t perm = file_stat.file_mode & 0777;
  204. printf("%c %o %s\n", isdir ? 'd' : 'f', perm, path);
  205. }
  206. u1 *buffer = builder->NewFile(path, stat_to_zipattr(file_stat));
  207. if (isdir || file_stat.total_size == 0) {
  208. builder->FinishFile(0);
  209. } else {
  210. if (!read_file(file, buffer, file_stat.total_size)) {
  211. return -1;
  212. }
  213. builder->FinishFile(file_stat.total_size, compress, true);
  214. }
  215. return 0;
  216. }
  217. // Read a list of files separated by newlines. The resulting array can be
  218. // freed using the free method.
  219. char **read_filelist(char *filename) {
  220. Stat file_stat;
  221. if (!stat_file(filename, &file_stat)) {
  222. fprintf(stderr, "Cannot stat file %s: %s\n", filename, strerror(errno));
  223. return NULL;
  224. }
  225. char *data = static_cast<char *>(malloc(file_stat.total_size));
  226. if (!read_file(filename, data, file_stat.total_size)) {
  227. return NULL;
  228. }
  229. int nb_entries = 1;
  230. for (int i = 0; i < file_stat.total_size; i++) {
  231. if (data[i] == '\n') {
  232. nb_entries++;
  233. }
  234. }
  235. size_t sizeof_array = sizeof(char *) * (nb_entries + 1);
  236. void *result = malloc(sizeof_array + file_stat.total_size + 1);
  237. // copy the content
  238. char **filelist = static_cast<char **>(result);
  239. char *content = static_cast<char *>(result) + sizeof_array;
  240. memcpy(content, data, file_stat.total_size);
  241. content[file_stat.total_size] = '\0';
  242. free(data);
  243. // Create the corresponding array
  244. int j = 1;
  245. filelist[0] = content;
  246. for (int i = 0; i < file_stat.total_size; i++) {
  247. if (content[i] == '\n') {
  248. content[i] = 0;
  249. if (i + 1 < file_stat.total_size) {
  250. filelist[j] = content + i + 1;
  251. j++;
  252. }
  253. }
  254. }
  255. filelist[j] = NULL;
  256. return filelist;
  257. }
  258. // return real paths of the files
  259. char **parse_filelist(char *zipfile, char **file_entries, int nb_entries,
  260. bool flatten) {
  261. // no need to free since the path lists should live until the end of the
  262. // program
  263. char **files = static_cast<char **>(malloc(sizeof(char *) * nb_entries));
  264. char **zip_paths = file_entries;
  265. for (int i = 0; i < nb_entries; i++) {
  266. char *p_eq = strchr(file_entries[i], '=');
  267. if (p_eq != NULL) {
  268. if (flatten) {
  269. fprintf(stderr, "Unable to create zip file %s: %s.\n", zipfile,
  270. "= can't be used with flatten");
  271. free(files);
  272. return NULL;
  273. }
  274. if (p_eq == file_entries[i]) {
  275. fprintf(stderr, "Unable to create zip file %s: %s.\n", zipfile,
  276. "A zip path should be given before =");
  277. free(files);
  278. return NULL;
  279. }
  280. *p_eq = '\0';
  281. files[i] = p_eq + 1;
  282. if (files[i][0] == '\0') {
  283. files[i] = NULL;
  284. }
  285. } else {
  286. files[i] = file_entries[i];
  287. zip_paths[i] = NULL;
  288. }
  289. }
  290. return files;
  291. }
  292. // Execute the create operation
  293. int create(char *zipfile, char **file_entries, bool flatten, bool verbose,
  294. bool compress) {
  295. int nb_entries = 0;
  296. while (file_entries[nb_entries] != NULL) {
  297. nb_entries++;
  298. }
  299. char **zip_paths = file_entries;
  300. char **files = parse_filelist(zipfile, file_entries, nb_entries, flatten);
  301. if (files == NULL) {
  302. return -1;
  303. }
  304. u8 size = ZipBuilder::EstimateSize(files, zip_paths, nb_entries);
  305. if (size == 0) {
  306. return -1;
  307. }
  308. std::unique_ptr<ZipBuilder> builder(ZipBuilder::Create(zipfile, size));
  309. if (builder == NULL) {
  310. fprintf(stderr, "Unable to create zip file %s: %s.\n",
  311. zipfile, strerror(errno));
  312. return -1;
  313. }
  314. for (int i = 0; i < nb_entries; i++) {
  315. if (add_file(builder, files[i], zip_paths[i], flatten, verbose, compress) <
  316. 0) {
  317. return -1;
  318. }
  319. }
  320. if (builder->Finish() < 0) {
  321. fprintf(stderr, "%s\n", builder->GetError());
  322. return -1;
  323. }
  324. return 0;
  325. }
  326. } // namespace devtools_ijar
  327. //
  328. // main method
  329. //
  330. static void usage(char *progname) {
  331. fprintf(stderr,
  332. "Usage: %s [vxc[fC]] x.zip [-d exdir] [[zip_path1=]file1 ... "
  333. "[zip_pathn=]filen]\n",
  334. progname);
  335. fprintf(stderr, " v verbose - list all file in x.zip\n");
  336. fprintf(stderr,
  337. " x extract - extract files in x.zip to current directory, or "
  338. " an optional directory relative to the current directory "
  339. " specified through -d option\n");
  340. fprintf(stderr, " c create - add files to x.zip\n");
  341. fprintf(stderr,
  342. " f flatten - flatten files to use with create or "
  343. "extract operation\n");
  344. fprintf(stderr,
  345. " C compress - compress files when using the create operation\n");
  346. fprintf(stderr, "x and c cannot be used in the same command-line.\n");
  347. fprintf(stderr,
  348. "\nFor every file, a path in the zip can be specified. Examples:\n");
  349. fprintf(stderr,
  350. " zipper c x.zip a/b/__init__.py= # Add an empty file at "
  351. "a/b/__init__.py\n");
  352. fprintf(stderr,
  353. " zipper c x.zip a/b/main.py=foo/bar/bin.py # Add file "
  354. "foo/bar/bin.py at a/b/main.py\n");
  355. fprintf(stderr,
  356. "\nIf the zip path is not specified, it is assumed to be the file "
  357. "path.\n");
  358. exit(1);
  359. }
  360. int main(int argc, char **argv) {
  361. bool extract = false;
  362. bool verbose = false;
  363. bool create = false;
  364. bool compress = false;
  365. bool flatten = false;
  366. if (argc < 3) {
  367. usage(argv[0]);
  368. }
  369. for (int i = 0; argv[1][i] != 0; i++) {
  370. switch (argv[1][i]) {
  371. case 'x':
  372. extract = true;
  373. break;
  374. case 'v':
  375. verbose = true;
  376. break;
  377. case 'c':
  378. create = true;
  379. break;
  380. case 'f':
  381. flatten = true;
  382. break;
  383. case 'C':
  384. compress = true;
  385. break;
  386. default:
  387. usage(argv[0]);
  388. }
  389. }
  390. // x and c cannot be used in the same command-line.
  391. if (create && extract) {
  392. usage(argv[0]);
  393. }
  394. // Calculate the argument index of the first entry file.
  395. int filelist_start_index;
  396. if (argc > 3 && strcmp(argv[3], "-d") == 0) {
  397. filelist_start_index = 5;
  398. } else {
  399. filelist_start_index = 3;
  400. }
  401. char** filelist = NULL;
  402. // We have one option file. Read and extract the content.
  403. if (argc == filelist_start_index + 1 &&
  404. argv[filelist_start_index][0] == '@') {
  405. char* filelist_name = argv[filelist_start_index];
  406. filelist = devtools_ijar::read_filelist(filelist_name + 1);
  407. if (filelist == NULL) {
  408. fprintf(stderr, "Can't read file list %s: %s.\n", filelist_name,
  409. strerror(errno));
  410. return -1;
  411. }
  412. // We have more than one files. Assume that they are all file entries.
  413. } else if (argc >= filelist_start_index + 1) {
  414. filelist = argv + filelist_start_index;
  415. } else {
  416. // There are no entry files specified. This is forbidden if we are creating
  417. // a zip file.
  418. if (create) {
  419. fprintf(stderr, "Can't create zip without input files specified.");
  420. return -1;
  421. }
  422. }
  423. if (create) {
  424. // Create a zip
  425. return devtools_ijar::create(argv[2], filelist, flatten, verbose, compress);
  426. } else {
  427. char* exdir = NULL;
  428. if (argc > 3 && strcmp(argv[3], "-d") == 0) {
  429. exdir = argv[4];
  430. }
  431. // Extraction / list mode
  432. return devtools_ijar::extract(argv[2], exdir, filelist, verbose, extract,
  433. flatten);
  434. }
  435. }