cpio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. #if ENABLE_FEATURE_CPIO_O
  17. static off_t cpio_pad4(off_t size)
  18. {
  19. int i;
  20. i = (- size) & 3;
  21. size += i;
  22. while (--i >= 0)
  23. bb_putchar('\0');
  24. return size;
  25. }
  26. /* Return value will become exit code.
  27. * It's ok to exit instead of return. */
  28. static int cpio_o(void)
  29. {
  30. struct name_s {
  31. struct name_s *next;
  32. char name[1];
  33. };
  34. struct inodes_s {
  35. struct inodes_s *next;
  36. struct name_s *names;
  37. struct stat st;
  38. };
  39. struct inodes_s *links = NULL;
  40. off_t bytes = 0; /* output bytes count */
  41. while (1) {
  42. const char *name;
  43. char *line;
  44. struct stat st;
  45. line = xmalloc_fgetline(stdin);
  46. if (line) {
  47. /* Strip leading "./[./]..." from the filename */
  48. name = line;
  49. while (name[0] == '.' && name[1] == '/') {
  50. while (*++name == '/')
  51. continue;
  52. }
  53. if (!*name) { /* line is empty */
  54. free(line);
  55. continue;
  56. }
  57. if (lstat(name, &st)) {
  58. abort_cpio_o:
  59. bb_simple_perror_msg_and_die(name);
  60. }
  61. if (!(S_ISLNK(st.st_mode) || S_ISREG(st.st_mode)))
  62. st.st_size = 0; /* paranoia */
  63. /* Store hardlinks for later processing, dont output them */
  64. if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) {
  65. struct name_s *n;
  66. struct inodes_s *l;
  67. /* Do we have this hardlink remembered? */
  68. l = links;
  69. while (1) {
  70. if (l == NULL) {
  71. /* Not found: add new item to "links" list */
  72. l = xzalloc(sizeof(*l));
  73. l->st = st;
  74. l->next = links;
  75. links = l;
  76. break;
  77. }
  78. if (l->st.st_ino == st.st_ino) {
  79. /* found */
  80. break;
  81. }
  82. l = l->next;
  83. }
  84. /* Add new name to "l->names" list */
  85. n = xmalloc(sizeof(*n) + strlen(name));
  86. strcpy(n->name, name);
  87. n->next = l->names;
  88. l->names = n;
  89. free(line);
  90. continue;
  91. }
  92. } else { /* line == NULL: EOF */
  93. next_link:
  94. if (links) {
  95. /* Output hardlink's data */
  96. st = links->st;
  97. name = links->names->name;
  98. links->names = links->names->next;
  99. /* GNU cpio is reported to emit file data
  100. * only for the last instance. Mimic that. */
  101. if (links->names == NULL)
  102. links = links->next;
  103. else
  104. st.st_size = 0;
  105. /* NB: we leak links->names and/or links,
  106. * this is intended (we exit soon anyway) */
  107. } else {
  108. /* If no (more) hardlinks to output,
  109. * output "trailer" entry */
  110. name = "TRAILER!!!";
  111. /* st.st_size == 0 is a must, but for uniformity
  112. * in the output, we zero out everything */
  113. memset(&st, 0, sizeof(st));
  114. /* st.st_nlink = 1; - GNU cpio does this */
  115. }
  116. }
  117. bytes += printf("070701"
  118. "%08X%08X%08X%08X%08X%08X%08X"
  119. "%08X%08X%08X%08X" /* GNU cpio uses uppercase hex */
  120. /* strlen+1: */ "%08X"
  121. /* chksum: */ "00000000" /* (only for "070702" files) */
  122. /* name,NUL: */ "%s%c",
  123. (unsigned)(uint32_t) st.st_ino,
  124. (unsigned)(uint32_t) st.st_mode,
  125. (unsigned)(uint32_t) st.st_uid,
  126. (unsigned)(uint32_t) st.st_gid,
  127. (unsigned)(uint32_t) st.st_nlink,
  128. (unsigned)(uint32_t) st.st_mtime,
  129. (unsigned)(uint32_t) st.st_size,
  130. (unsigned)(uint32_t) major(st.st_dev),
  131. (unsigned)(uint32_t) minor(st.st_dev),
  132. (unsigned)(uint32_t) major(st.st_rdev),
  133. (unsigned)(uint32_t) minor(st.st_rdev),
  134. (unsigned)(strlen(name) + 1),
  135. name, '\0');
  136. bytes = cpio_pad4(bytes);
  137. if (st.st_size) {
  138. if (S_ISLNK(st.st_mode)) {
  139. char *lpath = xmalloc_readlink_or_warn(name);
  140. if (!lpath)
  141. goto abort_cpio_o;
  142. bytes += printf("%s", lpath);
  143. free(lpath);
  144. } else { /* S_ISREG */
  145. int fd = xopen(name, O_RDONLY);
  146. fflush(stdout);
  147. /* We must abort if file got shorter too! */
  148. bb_copyfd_exact_size(fd, STDOUT_FILENO, st.st_size);
  149. bytes += st.st_size;
  150. close(fd);
  151. }
  152. bytes = cpio_pad4(bytes);
  153. }
  154. if (!line) {
  155. if (links)
  156. goto next_link;
  157. /* TODO: GNU cpio pads trailer to 512 bytes, do we want that? */
  158. return EXIT_SUCCESS;
  159. }
  160. free(line);
  161. } /* end of "while (1)" */
  162. }
  163. #endif
  164. /* GNU cpio 2.9 --help (abridged):
  165. Modes:
  166. -i, --extract Extract files from an archive
  167. -o, --create Create the archive
  168. -p, --pass-through Copy-pass mode [was ist das?!]
  169. -t, --list List the archive
  170. Options valid in any mode:
  171. --block-size=SIZE I/O block size = SIZE * 512 bytes
  172. -B I/O block size = 5120 bytes
  173. -c Use the old portable (ASCII) archive format
  174. -C, --io-size=NUMBER I/O block size in bytes
  175. -f, --nonmatching Only copy files that do not match given pattern
  176. -F, --file=FILE Use FILE instead of standard input or output
  177. -H, --format=FORMAT Use given archive FORMAT
  178. -M, --message=STRING Print STRING when the end of a volume of the
  179. backup media is reached
  180. -n, --numeric-uid-gid If -v, show numeric UID and GID
  181. --quiet Do not print the number of blocks copied
  182. --rsh-command=COMMAND Use remote COMMAND instead of rsh
  183. -v, --verbose Verbosely list the files processed
  184. -V, --dot Print a "." for each file processed
  185. -W, --warning=FLAG Control warning display: 'none','truncate','all';
  186. multiple options accumulate
  187. Options valid only in --extract mode:
  188. -b, --swap Swap both halfwords of words and bytes of
  189. halfwords in the data (equivalent to -sS)
  190. -r, --rename Interactively rename files
  191. -s, --swap-bytes Swap the bytes of each halfword in the files
  192. -S, --swap-halfwords Swap the halfwords of each word (4 bytes)
  193. --to-stdout Extract files to standard output
  194. -E, --pattern-file=FILE Read additional patterns specifying filenames to
  195. extract or list from FILE
  196. --only-verify-crc Verify CRC's, don't actually extract the files
  197. Options valid only in --create mode:
  198. -A, --append Append to an existing archive
  199. -O FILE File to use instead of standard output
  200. Options valid only in --pass-through mode:
  201. -l, --link Link files instead of copying them, when possible
  202. Options valid in --extract and --create modes:
  203. --absolute-filenames Do not strip file system prefix components from
  204. the file names
  205. --no-absolute-filenames Create all files relative to the current dir
  206. Options valid in --create and --pass-through modes:
  207. -0, --null A list of filenames is terminated by a NUL
  208. -a, --reset-access-time Reset the access times of files after reading them
  209. -I FILE File to use instead of standard input
  210. -L, --dereference Dereference symbolic links (copy the files
  211. that they point to instead of copying the links)
  212. -R, --owner=[USER][:.][GROUP] Set owner of created files
  213. Options valid in --extract and --pass-through modes:
  214. -d, --make-directories Create leading directories where needed
  215. -m, --preserve-modification-time Retain mtime when creating files
  216. --no-preserve-owner Do not change the ownership of the files
  217. --sparse Write files with blocks of zeros as sparse files
  218. -u, --unconditional Replace all files unconditionally
  219. */
  220. int cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  221. int cpio_main(int argc UNUSED_PARAM, char **argv)
  222. {
  223. archive_handle_t *archive_handle;
  224. char *cpio_filename;
  225. USE_FEATURE_CPIO_O(const char *cpio_fmt = "";)
  226. unsigned opt;
  227. enum {
  228. CPIO_OPT_EXTRACT = (1 << 0),
  229. CPIO_OPT_TEST = (1 << 1),
  230. CPIO_OPT_UNCONDITIONAL = (1 << 2),
  231. CPIO_OPT_VERBOSE = (1 << 3),
  232. CPIO_OPT_FILE = (1 << 4),
  233. CPIO_OPT_CREATE_LEADING_DIR = (1 << 5),
  234. CPIO_OPT_PRESERVE_MTIME = (1 << 6),
  235. CPIO_OPT_CREATE = (1 << 7),
  236. CPIO_OPT_FORMAT = (1 << 8),
  237. };
  238. #if ENABLE_GETOPT_LONG
  239. applet_long_options =
  240. "extract\0" No_argument "i"
  241. "list\0" No_argument "t"
  242. #if ENABLE_FEATURE_CPIO_O
  243. "create\0" No_argument "o"
  244. "format\0" Required_argument "H"
  245. #endif
  246. ;
  247. #endif
  248. /* Initialize */
  249. archive_handle = init_handle();
  250. archive_handle->src_fd = STDIN_FILENO;
  251. archive_handle->seek = seek_by_read;
  252. archive_handle->ah_flags = ARCHIVE_EXTRACT_NEWER;
  253. #if ENABLE_FEATURE_CPIO_O
  254. opt = getopt32(argv, "ituvF:dmoH:", &cpio_filename, &cpio_fmt);
  255. if (opt & CPIO_OPT_CREATE) {
  256. if (*cpio_fmt != 'n')
  257. bb_show_usage();
  258. if (opt & CPIO_OPT_FILE) {
  259. fclose(stdout);
  260. stdout = fopen_for_write(cpio_filename);
  261. /* Paranoia: I don't trust libc that much */
  262. xdup2(fileno(stdout), STDOUT_FILENO);
  263. }
  264. return cpio_o();
  265. }
  266. #else
  267. opt = getopt32(argv, "ituvF:dm", &cpio_filename);
  268. #endif
  269. argv += optind;
  270. /* One of either extract or test options must be given */
  271. if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) {
  272. bb_show_usage();
  273. }
  274. if (opt & CPIO_OPT_TEST) {
  275. /* if both extract and test options are given, ignore extract option */
  276. if (opt & CPIO_OPT_EXTRACT) {
  277. opt &= ~CPIO_OPT_EXTRACT;
  278. }
  279. archive_handle->action_header = header_list;
  280. }
  281. if (opt & CPIO_OPT_EXTRACT) {
  282. archive_handle->action_data = data_extract_all;
  283. }
  284. if (opt & CPIO_OPT_UNCONDITIONAL) {
  285. archive_handle->ah_flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
  286. archive_handle->ah_flags &= ~ARCHIVE_EXTRACT_NEWER;
  287. }
  288. if (opt & CPIO_OPT_VERBOSE) {
  289. if (archive_handle->action_header == header_list) {
  290. archive_handle->action_header = header_verbose_list;
  291. } else {
  292. archive_handle->action_header = header_list;
  293. }
  294. }
  295. if (opt & CPIO_OPT_FILE) { /* -F */
  296. archive_handle->src_fd = xopen(cpio_filename, O_RDONLY);
  297. archive_handle->seek = seek_by_jump;
  298. }
  299. if (opt & CPIO_OPT_CREATE_LEADING_DIR) {
  300. archive_handle->ah_flags |= ARCHIVE_CREATE_LEADING_DIRS;
  301. }
  302. if (opt & CPIO_OPT_PRESERVE_MTIME) {
  303. archive_handle->ah_flags |= ARCHIVE_PRESERVE_DATE;
  304. }
  305. while (*argv) {
  306. archive_handle->filter = filter_accept_list;
  307. llist_add_to(&(archive_handle->accept), *argv);
  308. argv++;
  309. }
  310. while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
  311. continue;
  312. return EXIT_SUCCESS;
  313. }