cpio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini cpio implementation for busybox
  4. *
  5. * Copyright (C) 2001 by Glenn McGrath
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. *
  9. * Limitations:
  10. * Doesn't check CRC's
  11. * Only supports new ASCII and CRC formats
  12. *
  13. */
  14. #include "libbb.h"
  15. #include "unarchive.h"
  16. /* GNU cpio 2.9 --help (abridged):
  17. Modes:
  18. -t, --list List the archive
  19. -i, --extract Extract files from an archive
  20. -o, --create Create the archive
  21. -p, --pass-through Copy-pass mode [was ist das?!]
  22. Options valid in any mode:
  23. --block-size=SIZE I/O block size = SIZE * 512 bytes
  24. -B I/O block size = 5120 bytes
  25. -c Use the old portable (ASCII) archive format
  26. -C, --io-size=NUMBER I/O block size in bytes
  27. -f, --nonmatching Only copy files that do not match given pattern
  28. -F, --file=FILE Use FILE instead of standard input or output
  29. -H, --format=FORMAT Use given archive FORMAT
  30. -M, --message=STRING Print STRING when the end of a volume of the
  31. backup media is reached
  32. -n, --numeric-uid-gid If -v, show numeric UID and GID
  33. --quiet Do not print the number of blocks copied
  34. --rsh-command=COMMAND Use remote COMMAND instead of rsh
  35. -v, --verbose Verbosely list the files processed
  36. -V, --dot Print a "." for each file processed
  37. -W, --warning=FLAG Control warning display: 'none','truncate','all';
  38. multiple options accumulate
  39. Options valid only in --extract mode:
  40. -b, --swap Swap both halfwords of words and bytes of
  41. halfwords in the data (equivalent to -sS)
  42. -r, --rename Interactively rename files
  43. -s, --swap-bytes Swap the bytes of each halfword in the files
  44. -S, --swap-halfwords Swap the halfwords of each word (4 bytes)
  45. --to-stdout Extract files to standard output
  46. -E, --pattern-file=FILE Read additional patterns specifying filenames to
  47. extract or list from FILE
  48. --only-verify-crc Verify CRC's, don't actually extract the files
  49. Options valid only in --create mode:
  50. -A, --append Append to an existing archive
  51. -O FILE File to use instead of standard output
  52. Options valid only in --pass-through mode:
  53. -l, --link Link files instead of copying them, when possible
  54. Options valid in --extract and --create modes:
  55. --absolute-filenames Do not strip file system prefix components from
  56. the file names
  57. --no-absolute-filenames Create all files relative to the current dir
  58. Options valid in --create and --pass-through modes:
  59. -0, --null A list of filenames is terminated by a NUL
  60. -a, --reset-access-time Reset the access times of files after reading them
  61. -I FILE File to use instead of standard input
  62. -L, --dereference Dereference symbolic links (copy the files
  63. that they point to instead of copying the links)
  64. -R, --owner=[USER][:.][GROUP] Set owner of created files
  65. Options valid in --extract and --pass-through modes:
  66. -d, --make-directories Create leading directories where needed
  67. -m, --preserve-modification-time Retain mtime when creating files
  68. --no-preserve-owner Do not change the ownership of the files
  69. --sparse Write files with blocks of zeros as sparse files
  70. -u, --unconditional Replace all files unconditionally
  71. */
  72. enum {
  73. CPIO_OPT_EXTRACT = (1 << 0),
  74. CPIO_OPT_TEST = (1 << 1),
  75. CPIO_OPT_NUL_TERMINATED = (1 << 2),
  76. CPIO_OPT_UNCONDITIONAL = (1 << 3),
  77. CPIO_OPT_VERBOSE = (1 << 4),
  78. CPIO_OPT_CREATE_LEADING_DIR = (1 << 5),
  79. CPIO_OPT_PRESERVE_MTIME = (1 << 6),
  80. CPIO_OPT_DEREF = (1 << 7),
  81. CPIO_OPT_FILE = (1 << 8),
  82. OPTBIT_FILE = 8,
  83. IF_FEATURE_CPIO_O(OPTBIT_CREATE ,)
  84. IF_FEATURE_CPIO_O(OPTBIT_FORMAT ,)
  85. IF_FEATURE_CPIO_P(OPTBIT_PASSTHROUGH,)
  86. IF_LONG_OPTS( OPTBIT_QUIET ,)
  87. IF_LONG_OPTS( OPTBIT_2STDOUT ,)
  88. CPIO_OPT_CREATE = IF_FEATURE_CPIO_O((1 << OPTBIT_CREATE )) + 0,
  89. CPIO_OPT_FORMAT = IF_FEATURE_CPIO_O((1 << OPTBIT_FORMAT )) + 0,
  90. CPIO_OPT_PASSTHROUGH = IF_FEATURE_CPIO_P((1 << OPTBIT_PASSTHROUGH)) + 0,
  91. CPIO_OPT_QUIET = IF_LONG_OPTS( (1 << OPTBIT_QUIET )) + 0,
  92. CPIO_OPT_2STDOUT = IF_LONG_OPTS( (1 << OPTBIT_2STDOUT )) + 0,
  93. };
  94. #define OPTION_STR "it0uvdmLF:"
  95. #if ENABLE_FEATURE_CPIO_O
  96. static off_t cpio_pad4(off_t size)
  97. {
  98. int i;
  99. i = (- size) & 3;
  100. size += i;
  101. while (--i >= 0)
  102. bb_putchar('\0');
  103. return size;
  104. }
  105. /* Return value will become exit code.
  106. * It's ok to exit instead of return. */
  107. static NOINLINE int cpio_o(void)
  108. {
  109. static const char trailer[] ALIGN1 = "TRAILER!!!";
  110. struct name_s {
  111. struct name_s *next;
  112. char name[1];
  113. };
  114. struct inodes_s {
  115. struct inodes_s *next;
  116. struct name_s *names;
  117. struct stat st;
  118. };
  119. struct inodes_s *links = NULL;
  120. off_t bytes = 0; /* output bytes count */
  121. while (1) {
  122. const char *name;
  123. char *line;
  124. struct stat st;
  125. line = (option_mask32 & CPIO_OPT_NUL_TERMINATED)
  126. ? bb_get_chunk_from_file(stdin, NULL)
  127. : xmalloc_fgetline(stdin);
  128. if (line) {
  129. /* Strip leading "./[./]..." from the filename */
  130. name = line;
  131. while (name[0] == '.' && name[1] == '/') {
  132. while (*++name == '/')
  133. continue;
  134. }
  135. if (!*name) { /* line is empty */
  136. free(line);
  137. continue;
  138. }
  139. if ((option_mask32 & CPIO_OPT_DEREF)
  140. ? stat(name, &st)
  141. : lstat(name, &st)
  142. ) {
  143. abort_cpio_o:
  144. bb_simple_perror_msg_and_die(name);
  145. }
  146. if (!(S_ISLNK(st.st_mode) || S_ISREG(st.st_mode)))
  147. st.st_size = 0; /* paranoia */
  148. /* Store hardlinks for later processing, dont output them */
  149. if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) {
  150. struct name_s *n;
  151. struct inodes_s *l;
  152. /* Do we have this hardlink remembered? */
  153. l = links;
  154. while (1) {
  155. if (l == NULL) {
  156. /* Not found: add new item to "links" list */
  157. l = xzalloc(sizeof(*l));
  158. l->st = st;
  159. l->next = links;
  160. links = l;
  161. break;
  162. }
  163. if (l->st.st_ino == st.st_ino) {
  164. /* found */
  165. break;
  166. }
  167. l = l->next;
  168. }
  169. /* Add new name to "l->names" list */
  170. n = xmalloc(sizeof(*n) + strlen(name));
  171. strcpy(n->name, name);
  172. n->next = l->names;
  173. l->names = n;
  174. free(line);
  175. continue;
  176. }
  177. } else { /* line == NULL: EOF */
  178. next_link:
  179. if (links) {
  180. /* Output hardlink's data */
  181. st = links->st;
  182. name = links->names->name;
  183. links->names = links->names->next;
  184. /* GNU cpio is reported to emit file data
  185. * only for the last instance. Mimic that. */
  186. if (links->names == NULL)
  187. links = links->next;
  188. else
  189. st.st_size = 0;
  190. /* NB: we leak links->names and/or links,
  191. * this is intended (we exit soon anyway) */
  192. } else {
  193. /* If no (more) hardlinks to output,
  194. * output "trailer" entry */
  195. name = trailer;
  196. /* st.st_size == 0 is a must, but for uniformity
  197. * in the output, we zero out everything */
  198. memset(&st, 0, sizeof(st));
  199. /* st.st_nlink = 1; - GNU cpio does this */
  200. }
  201. }
  202. bytes += printf("070701"
  203. "%08X%08X%08X%08X%08X%08X%08X"
  204. "%08X%08X%08X%08X" /* GNU cpio uses uppercase hex */
  205. /* strlen+1: */ "%08X"
  206. /* chksum: */ "00000000" /* (only for "070702" files) */
  207. /* name,NUL: */ "%s%c",
  208. (unsigned)(uint32_t) st.st_ino,
  209. (unsigned)(uint32_t) st.st_mode,
  210. (unsigned)(uint32_t) st.st_uid,
  211. (unsigned)(uint32_t) st.st_gid,
  212. (unsigned)(uint32_t) st.st_nlink,
  213. (unsigned)(uint32_t) st.st_mtime,
  214. (unsigned)(uint32_t) st.st_size,
  215. (unsigned)(uint32_t) major(st.st_dev),
  216. (unsigned)(uint32_t) minor(st.st_dev),
  217. (unsigned)(uint32_t) major(st.st_rdev),
  218. (unsigned)(uint32_t) minor(st.st_rdev),
  219. (unsigned)(strlen(name) + 1),
  220. name, '\0');
  221. bytes = cpio_pad4(bytes);
  222. if (st.st_size) {
  223. if (S_ISLNK(st.st_mode)) {
  224. char *lpath = xmalloc_readlink_or_warn(name);
  225. if (!lpath)
  226. goto abort_cpio_o;
  227. bytes += printf("%s", lpath);
  228. free(lpath);
  229. } else { /* S_ISREG */
  230. int fd = xopen(name, O_RDONLY);
  231. fflush_all();
  232. /* We must abort if file got shorter too! */
  233. bb_copyfd_exact_size(fd, STDOUT_FILENO, st.st_size);
  234. bytes += st.st_size;
  235. close(fd);
  236. }
  237. bytes = cpio_pad4(bytes);
  238. }
  239. if (!line) {
  240. if (name != trailer)
  241. goto next_link;
  242. /* TODO: GNU cpio pads trailer to 512 bytes, do we want that? */
  243. return EXIT_SUCCESS;
  244. }
  245. free(line);
  246. } /* end of "while (1)" */
  247. }
  248. #endif
  249. int cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  250. int cpio_main(int argc UNUSED_PARAM, char **argv)
  251. {
  252. archive_handle_t *archive_handle;
  253. char *cpio_filename;
  254. IF_FEATURE_CPIO_O(const char *cpio_fmt = "";)
  255. unsigned opt;
  256. #if ENABLE_LONG_OPTS
  257. applet_long_options =
  258. "extract\0" No_argument "i"
  259. "list\0" No_argument "t"
  260. #if ENABLE_FEATURE_CPIO_O
  261. "create\0" No_argument "o"
  262. "format\0" Required_argument "H"
  263. #if ENABLE_FEATURE_CPIO_P
  264. "pass-through\0" No_argument "p"
  265. #endif
  266. #endif
  267. "verbose\0" No_argument "v"
  268. "quiet\0" No_argument "\xff"
  269. "to-stdout\0" No_argument "\xfe"
  270. ;
  271. #endif
  272. archive_handle = init_handle();
  273. /* archive_handle->src_fd = STDIN_FILENO; - done by init_handle */
  274. archive_handle->ah_flags = ARCHIVE_EXTRACT_NEWER;
  275. /* As of now we do not enforce this: */
  276. /* -i,-t,-o,-p are mutually exclusive */
  277. /* -u,-d,-m make sense only with -i or -p */
  278. /* -L makes sense only with -o or -p */
  279. #if !ENABLE_FEATURE_CPIO_O
  280. /* no parameters */
  281. opt_complementary = "=0";
  282. opt = getopt32(argv, OPTION_STR, &cpio_filename);
  283. if (opt & CPIO_OPT_FILE) { /* -F */
  284. xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO);
  285. }
  286. #else
  287. /* _exactly_ one parameter for -p, thus <= 1 param if -p is allowed */
  288. opt_complementary = ENABLE_FEATURE_CPIO_P ? "?1" : "=0";
  289. opt = getopt32(argv, OPTION_STR "oH:" IF_FEATURE_CPIO_P("p"), &cpio_filename, &cpio_fmt);
  290. argv += optind;
  291. if ((opt & (CPIO_OPT_FILE|CPIO_OPT_CREATE)) == CPIO_OPT_FILE) { /* -F without -o */
  292. xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO);
  293. }
  294. if (opt & CPIO_OPT_PASSTHROUGH) {
  295. pid_t pid;
  296. struct fd_pair pp;
  297. if (argv[0] == NULL)
  298. bb_show_usage();
  299. if (opt & CPIO_OPT_CREATE_LEADING_DIR)
  300. mkdir(argv[0], 0777);
  301. /* Crude existence check:
  302. * close(xopen(argv[0], O_RDONLY | O_DIRECTORY));
  303. * We can also xopen, fstat, IS_DIR, later fchdir.
  304. * This would check for existence earlier and cleaner.
  305. * As it stands now, if we fail xchdir later,
  306. * child dies on EPIPE, unless it caught
  307. * a diffrerent problem earlier.
  308. * This is good enough for now.
  309. */
  310. #if !BB_MMU
  311. pp.rd = 3;
  312. pp.wr = 4;
  313. if (!re_execed) {
  314. close(3);
  315. close(4);
  316. xpiped_pair(pp);
  317. }
  318. #else
  319. xpiped_pair(pp);
  320. #endif
  321. pid = fork_or_rexec(argv - optind);
  322. if (pid == 0) { /* child */
  323. close(pp.rd);
  324. xmove_fd(pp.wr, STDOUT_FILENO);
  325. goto dump;
  326. }
  327. /* parent */
  328. xchdir(*argv++);
  329. close(pp.wr);
  330. xmove_fd(pp.rd, STDIN_FILENO);
  331. //opt &= ~CPIO_OPT_PASSTHROUGH;
  332. opt |= CPIO_OPT_EXTRACT;
  333. goto skip;
  334. }
  335. /* -o */
  336. if (opt & CPIO_OPT_CREATE) {
  337. if (cpio_fmt[0] != 'n') /* we _require_ "-H newc" */
  338. bb_show_usage();
  339. if (opt & CPIO_OPT_FILE) {
  340. xmove_fd(xopen3(cpio_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666), STDOUT_FILENO);
  341. }
  342. dump:
  343. return cpio_o();
  344. }
  345. skip:
  346. #endif
  347. /* One of either extract or test options must be given */
  348. if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) {
  349. bb_show_usage();
  350. }
  351. if (opt & CPIO_OPT_TEST) {
  352. /* if both extract and test options are given, ignore extract option */
  353. opt &= ~CPIO_OPT_EXTRACT;
  354. archive_handle->action_header = header_list;
  355. }
  356. if (opt & CPIO_OPT_EXTRACT) {
  357. archive_handle->action_data = data_extract_all;
  358. if (opt & CPIO_OPT_2STDOUT)
  359. archive_handle->action_data = data_extract_to_stdout;
  360. }
  361. if (opt & CPIO_OPT_UNCONDITIONAL) {
  362. archive_handle->ah_flags |= ARCHIVE_UNLINK_OLD;
  363. archive_handle->ah_flags &= ~ARCHIVE_EXTRACT_NEWER;
  364. }
  365. if (opt & CPIO_OPT_VERBOSE) {
  366. if (archive_handle->action_header == header_list) {
  367. archive_handle->action_header = header_verbose_list;
  368. } else {
  369. archive_handle->action_header = header_list;
  370. }
  371. }
  372. if (opt & CPIO_OPT_CREATE_LEADING_DIR) {
  373. archive_handle->ah_flags |= ARCHIVE_CREATE_LEADING_DIRS;
  374. }
  375. if (opt & CPIO_OPT_PRESERVE_MTIME) {
  376. archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE;
  377. }
  378. while (*argv) {
  379. archive_handle->filter = filter_accept_list;
  380. llist_add_to(&archive_handle->accept, *argv);
  381. argv++;
  382. }
  383. /* see get_header_cpio */
  384. archive_handle->cpio__blocks = (off_t)-1;
  385. while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
  386. continue;
  387. if (archive_handle->cpio__blocks != (off_t)-1
  388. && !(opt & CPIO_OPT_QUIET)
  389. ) {
  390. fprintf(stderr, "%"OFF_FMT"u blocks\n", archive_handle->cpio__blocks);
  391. }
  392. return EXIT_SUCCESS;
  393. }