cpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. CPIO_OPT_CREATE = (1 << 9) * ENABLE_FEATURE_CPIO_O,
  83. CPIO_OPT_FORMAT = (1 << 10) * ENABLE_FEATURE_CPIO_O,
  84. CPIO_OPT_PASSTHROUGH = (1 << 11) * ENABLE_FEATURE_CPIO_P,
  85. };
  86. #define OPTION_STR "it0uvdmLF:"
  87. #if ENABLE_FEATURE_CPIO_O
  88. static off_t cpio_pad4(off_t size)
  89. {
  90. int i;
  91. i = (- size) & 3;
  92. size += i;
  93. while (--i >= 0)
  94. bb_putchar('\0');
  95. return size;
  96. }
  97. /* Return value will become exit code.
  98. * It's ok to exit instead of return. */
  99. static int cpio_o(void)
  100. {
  101. static const char trailer[] ALIGN1 = "TRAILER!!!";
  102. struct name_s {
  103. struct name_s *next;
  104. char name[1];
  105. };
  106. struct inodes_s {
  107. struct inodes_s *next;
  108. struct name_s *names;
  109. struct stat st;
  110. };
  111. struct inodes_s *links = NULL;
  112. off_t bytes = 0; /* output bytes count */
  113. while (1) {
  114. const char *name;
  115. char *line;
  116. struct stat st;
  117. line = (option_mask32 & CPIO_OPT_NUL_TERMINATED)
  118. ? bb_get_chunk_from_file(stdin, NULL)
  119. : xmalloc_fgetline(stdin);
  120. if (line) {
  121. /* Strip leading "./[./]..." from the filename */
  122. name = line;
  123. while (name[0] == '.' && name[1] == '/') {
  124. while (*++name == '/')
  125. continue;
  126. }
  127. if (!*name) { /* line is empty */
  128. free(line);
  129. continue;
  130. }
  131. if ((option_mask32 & CPIO_OPT_DEREF)
  132. ? stat(name, &st)
  133. : lstat(name, &st)
  134. ) {
  135. abort_cpio_o:
  136. bb_simple_perror_msg_and_die(name);
  137. }
  138. if (!(S_ISLNK(st.st_mode) || S_ISREG(st.st_mode)))
  139. st.st_size = 0; /* paranoia */
  140. /* Store hardlinks for later processing, dont output them */
  141. if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) {
  142. struct name_s *n;
  143. struct inodes_s *l;
  144. /* Do we have this hardlink remembered? */
  145. l = links;
  146. while (1) {
  147. if (l == NULL) {
  148. /* Not found: add new item to "links" list */
  149. l = xzalloc(sizeof(*l));
  150. l->st = st;
  151. l->next = links;
  152. links = l;
  153. break;
  154. }
  155. if (l->st.st_ino == st.st_ino) {
  156. /* found */
  157. break;
  158. }
  159. l = l->next;
  160. }
  161. /* Add new name to "l->names" list */
  162. n = xmalloc(sizeof(*n) + strlen(name));
  163. strcpy(n->name, name);
  164. n->next = l->names;
  165. l->names = n;
  166. free(line);
  167. continue;
  168. }
  169. } else { /* line == NULL: EOF */
  170. next_link:
  171. if (links) {
  172. /* Output hardlink's data */
  173. st = links->st;
  174. name = links->names->name;
  175. links->names = links->names->next;
  176. /* GNU cpio is reported to emit file data
  177. * only for the last instance. Mimic that. */
  178. if (links->names == NULL)
  179. links = links->next;
  180. else
  181. st.st_size = 0;
  182. /* NB: we leak links->names and/or links,
  183. * this is intended (we exit soon anyway) */
  184. } else {
  185. /* If no (more) hardlinks to output,
  186. * output "trailer" entry */
  187. name = trailer;
  188. /* st.st_size == 0 is a must, but for uniformity
  189. * in the output, we zero out everything */
  190. memset(&st, 0, sizeof(st));
  191. /* st.st_nlink = 1; - GNU cpio does this */
  192. }
  193. }
  194. bytes += printf("070701"
  195. "%08X%08X%08X%08X%08X%08X%08X"
  196. "%08X%08X%08X%08X" /* GNU cpio uses uppercase hex */
  197. /* strlen+1: */ "%08X"
  198. /* chksum: */ "00000000" /* (only for "070702" files) */
  199. /* name,NUL: */ "%s%c",
  200. (unsigned)(uint32_t) st.st_ino,
  201. (unsigned)(uint32_t) st.st_mode,
  202. (unsigned)(uint32_t) st.st_uid,
  203. (unsigned)(uint32_t) st.st_gid,
  204. (unsigned)(uint32_t) st.st_nlink,
  205. (unsigned)(uint32_t) st.st_mtime,
  206. (unsigned)(uint32_t) st.st_size,
  207. (unsigned)(uint32_t) major(st.st_dev),
  208. (unsigned)(uint32_t) minor(st.st_dev),
  209. (unsigned)(uint32_t) major(st.st_rdev),
  210. (unsigned)(uint32_t) minor(st.st_rdev),
  211. (unsigned)(strlen(name) + 1),
  212. name, '\0');
  213. bytes = cpio_pad4(bytes);
  214. if (st.st_size) {
  215. if (S_ISLNK(st.st_mode)) {
  216. char *lpath = xmalloc_readlink_or_warn(name);
  217. if (!lpath)
  218. goto abort_cpio_o;
  219. bytes += printf("%s", lpath);
  220. free(lpath);
  221. } else { /* S_ISREG */
  222. int fd = xopen(name, O_RDONLY);
  223. fflush(stdout);
  224. /* We must abort if file got shorter too! */
  225. bb_copyfd_exact_size(fd, STDOUT_FILENO, st.st_size);
  226. bytes += st.st_size;
  227. close(fd);
  228. }
  229. bytes = cpio_pad4(bytes);
  230. }
  231. if (!line) {
  232. if (name != trailer)
  233. goto next_link;
  234. /* TODO: GNU cpio pads trailer to 512 bytes, do we want that? */
  235. return EXIT_SUCCESS;
  236. }
  237. free(line);
  238. } /* end of "while (1)" */
  239. }
  240. #endif
  241. int cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  242. int cpio_main(int argc UNUSED_PARAM, char **argv)
  243. {
  244. archive_handle_t *archive_handle;
  245. char *cpio_filename;
  246. USE_FEATURE_CPIO_O(const char *cpio_fmt = "";)
  247. unsigned opt;
  248. #if ENABLE_GETOPT_LONG
  249. applet_long_options =
  250. "extract\0" No_argument "i"
  251. "list\0" No_argument "t"
  252. #if ENABLE_FEATURE_CPIO_O
  253. "create\0" No_argument "o"
  254. "format\0" Required_argument "H"
  255. #if ENABLE_FEATURE_CPIO_P
  256. "pass-through\0" No_argument "p"
  257. #endif
  258. #endif
  259. ;
  260. #endif
  261. /* As of now we do not enforce this: */
  262. /* -i,-t,-o,-p are mutually exclusive */
  263. /* -u,-d,-m make sense only with -i or -p */
  264. /* -L makes sense only with -o or -p */
  265. #if !ENABLE_FEATURE_CPIO_O
  266. opt = getopt32(argv, OPTION_STR, &cpio_filename);
  267. #else
  268. opt = getopt32(argv, OPTION_STR "oH:" USE_FEATURE_CPIO_P("p"), &cpio_filename, &cpio_fmt);
  269. if (opt & CPIO_OPT_PASSTHROUGH) {
  270. pid_t pid;
  271. struct fd_pair pp;
  272. if (argv[optind] == NULL)
  273. bb_show_usage();
  274. if (opt & CPIO_OPT_CREATE_LEADING_DIR)
  275. mkdir(argv[optind], 0777);
  276. /* Crude existence check:
  277. * close(xopen(argv[optind], O_RDONLY | O_DIRECTORY));
  278. * We can also xopen, fstat, IS_DIR, later fchdir.
  279. * This would check for existence earlier and cleaner.
  280. * As it stands now, if we fail xchdir later,
  281. * child dies on EPIPE, unless it caught
  282. * a diffrerent problem earlier.
  283. * This is good enough for now.
  284. */
  285. #if !BB_MMU
  286. pp.rd = 3;
  287. pp.wr = 4;
  288. if (!re_execed) {
  289. close(3);
  290. close(4);
  291. xpiped_pair(pp);
  292. }
  293. #else
  294. xpiped_pair(pp);
  295. #endif
  296. pid = fork_or_rexec(argv);
  297. if (pid == 0) { /* child */
  298. close(pp.rd);
  299. xmove_fd(pp.wr, STDOUT_FILENO);
  300. goto dump;
  301. }
  302. /* parent */
  303. xchdir(argv[optind++]);
  304. close(pp.wr);
  305. xmove_fd(pp.rd, STDIN_FILENO);
  306. opt &= ~CPIO_OPT_PASSTHROUGH;
  307. opt |= CPIO_OPT_EXTRACT;
  308. goto skip;
  309. }
  310. /* -o */
  311. if (opt & CPIO_OPT_CREATE) {
  312. if (*cpio_fmt != 'n') /* we _require_ "-H newc" */
  313. bb_show_usage();
  314. if (opt & CPIO_OPT_FILE) {
  315. fclose(stdout);
  316. stdout = fopen_for_write(cpio_filename);
  317. /* Paranoia: I don't trust libc that much */
  318. xdup2(fileno(stdout), STDOUT_FILENO);
  319. }
  320. dump:
  321. return cpio_o();
  322. }
  323. skip:
  324. #endif
  325. argv += optind;
  326. archive_handle = init_handle();
  327. archive_handle->src_fd = STDIN_FILENO;
  328. archive_handle->seek = seek_by_read;
  329. archive_handle->ah_flags = ARCHIVE_EXTRACT_NEWER;
  330. /* One of either extract or test options must be given */
  331. if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) {
  332. bb_show_usage();
  333. }
  334. if (opt & CPIO_OPT_TEST) {
  335. /* if both extract and test options are given, ignore extract option */
  336. opt &= ~CPIO_OPT_EXTRACT;
  337. archive_handle->action_header = header_list;
  338. }
  339. if (opt & CPIO_OPT_EXTRACT) {
  340. archive_handle->action_data = data_extract_all;
  341. }
  342. if (opt & CPIO_OPT_UNCONDITIONAL) {
  343. archive_handle->ah_flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
  344. archive_handle->ah_flags &= ~ARCHIVE_EXTRACT_NEWER;
  345. }
  346. if (opt & CPIO_OPT_VERBOSE) {
  347. if (archive_handle->action_header == header_list) {
  348. archive_handle->action_header = header_verbose_list;
  349. } else {
  350. archive_handle->action_header = header_list;
  351. }
  352. }
  353. if (opt & CPIO_OPT_FILE) { /* -F */
  354. archive_handle->src_fd = xopen(cpio_filename, O_RDONLY);
  355. archive_handle->seek = seek_by_jump;
  356. }
  357. if (opt & CPIO_OPT_CREATE_LEADING_DIR) {
  358. archive_handle->ah_flags |= ARCHIVE_CREATE_LEADING_DIRS;
  359. }
  360. if (opt & CPIO_OPT_PRESERVE_MTIME) {
  361. archive_handle->ah_flags |= ARCHIVE_PRESERVE_DATE;
  362. }
  363. while (*argv) {
  364. archive_handle->filter = filter_accept_list;
  365. llist_add_to(&(archive_handle->accept), *argv);
  366. argv++;
  367. }
  368. /* see get_header_cpio */
  369. archive_handle->ah_priv[2] = (void*) ~(ptrdiff_t)0;
  370. while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
  371. continue;
  372. if (archive_handle->ah_priv[2] != (void*) ~(ptrdiff_t)0)
  373. printf("%lu blocks\n", (unsigned long)(ptrdiff_t)(archive_handle->ah_priv[2]));
  374. return EXIT_SUCCESS;
  375. }