cpio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 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(stdout);
  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. /* As of now we do not enforce this: */
  273. /* -i,-t,-o,-p are mutually exclusive */
  274. /* -u,-d,-m make sense only with -i or -p */
  275. /* -L makes sense only with -o or -p */
  276. #if !ENABLE_FEATURE_CPIO_O
  277. opt = getopt32(argv, OPTION_STR, &cpio_filename);
  278. #else
  279. opt = getopt32(argv, OPTION_STR "oH:" IF_FEATURE_CPIO_P("p"), &cpio_filename, &cpio_fmt);
  280. if (opt & CPIO_OPT_PASSTHROUGH) {
  281. pid_t pid;
  282. struct fd_pair pp;
  283. if (argv[optind] == NULL)
  284. bb_show_usage();
  285. if (opt & CPIO_OPT_CREATE_LEADING_DIR)
  286. mkdir(argv[optind], 0777);
  287. /* Crude existence check:
  288. * close(xopen(argv[optind], O_RDONLY | O_DIRECTORY));
  289. * We can also xopen, fstat, IS_DIR, later fchdir.
  290. * This would check for existence earlier and cleaner.
  291. * As it stands now, if we fail xchdir later,
  292. * child dies on EPIPE, unless it caught
  293. * a diffrerent problem earlier.
  294. * This is good enough for now.
  295. */
  296. #if !BB_MMU
  297. pp.rd = 3;
  298. pp.wr = 4;
  299. if (!re_execed) {
  300. close(3);
  301. close(4);
  302. xpiped_pair(pp);
  303. }
  304. #else
  305. xpiped_pair(pp);
  306. #endif
  307. pid = fork_or_rexec(argv);
  308. if (pid == 0) { /* child */
  309. close(pp.rd);
  310. xmove_fd(pp.wr, STDOUT_FILENO);
  311. goto dump;
  312. }
  313. /* parent */
  314. xchdir(argv[optind++]);
  315. close(pp.wr);
  316. xmove_fd(pp.rd, STDIN_FILENO);
  317. opt &= ~CPIO_OPT_PASSTHROUGH;
  318. opt |= CPIO_OPT_EXTRACT;
  319. goto skip;
  320. }
  321. /* -o */
  322. if (opt & CPIO_OPT_CREATE) {
  323. if (*cpio_fmt != 'n') /* we _require_ "-H newc" */
  324. bb_show_usage();
  325. if (opt & CPIO_OPT_FILE) {
  326. fclose(stdout);
  327. stdout = fopen_for_write(cpio_filename);
  328. /* Paranoia: I don't trust libc that much */
  329. xdup2(fileno(stdout), STDOUT_FILENO);
  330. }
  331. dump:
  332. return cpio_o();
  333. }
  334. skip:
  335. #endif
  336. argv += optind;
  337. archive_handle = init_handle();
  338. archive_handle->src_fd = STDIN_FILENO;
  339. archive_handle->seek = seek_by_read;
  340. archive_handle->ah_flags = ARCHIVE_EXTRACT_NEWER;
  341. /* One of either extract or test options must be given */
  342. if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) {
  343. bb_show_usage();
  344. }
  345. if (opt & CPIO_OPT_TEST) {
  346. /* if both extract and test options are given, ignore extract option */
  347. opt &= ~CPIO_OPT_EXTRACT;
  348. archive_handle->action_header = header_list;
  349. }
  350. if (opt & CPIO_OPT_EXTRACT) {
  351. archive_handle->action_data = data_extract_all;
  352. if (opt & CPIO_OPT_2STDOUT)
  353. archive_handle->action_data = data_extract_to_stdout;
  354. }
  355. if (opt & CPIO_OPT_UNCONDITIONAL) {
  356. archive_handle->ah_flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
  357. archive_handle->ah_flags &= ~ARCHIVE_EXTRACT_NEWER;
  358. }
  359. if (opt & CPIO_OPT_VERBOSE) {
  360. if (archive_handle->action_header == header_list) {
  361. archive_handle->action_header = header_verbose_list;
  362. } else {
  363. archive_handle->action_header = header_list;
  364. }
  365. }
  366. if (opt & CPIO_OPT_FILE) { /* -F */
  367. archive_handle->src_fd = xopen(cpio_filename, O_RDONLY);
  368. archive_handle->seek = seek_by_jump;
  369. }
  370. if (opt & CPIO_OPT_CREATE_LEADING_DIR) {
  371. archive_handle->ah_flags |= ARCHIVE_CREATE_LEADING_DIRS;
  372. }
  373. if (opt & CPIO_OPT_PRESERVE_MTIME) {
  374. archive_handle->ah_flags |= ARCHIVE_PRESERVE_DATE;
  375. }
  376. while (*argv) {
  377. archive_handle->filter = filter_accept_list;
  378. llist_add_to(&(archive_handle->accept), *argv);
  379. argv++;
  380. }
  381. /* see get_header_cpio */
  382. archive_handle->ah_priv[2] = (void*) ~(ptrdiff_t)0;
  383. while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
  384. continue;
  385. if (archive_handle->ah_priv[2] != (void*) ~(ptrdiff_t)0
  386. && !(opt & CPIO_OPT_QUIET)
  387. )
  388. printf("%lu blocks\n", (unsigned long)(ptrdiff_t)(archive_handle->ah_priv[2]));
  389. return EXIT_SUCCESS;
  390. }