3
0

cpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 (GNU cpio) 2.9 help (abridged):
  165. Main operation mode:
  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. Operation modifiers 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 to the given NUMBER 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. Operation modifiers 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. Operation modifiers valid only in --create mode:
  198. -A, --append Append to an existing archive
  199. -O FILE File to use instead of standard output
  200. Operation modifiers valid only in --pass-through mode:
  201. -l, --link Link files instead of copying them, when possible
  202. Operation modifiers 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. Operation modifiers 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. Operation modifiers valid in --extract and --pass-through modes:
  214. -d, --make-directories Create leading directories where needed
  215. -m, --preserve-modification-time
  216. Retain previous file modification times when
  217. creating files
  218. --no-preserve-owner Do not change the ownership of the files
  219. --sparse Write files with blocks of zeros as sparse files
  220. -u, --unconditional Replace all files unconditionally
  221. */
  222. int cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  223. int cpio_main(int argc ATTRIBUTE_UNUSED, char **argv)
  224. {
  225. archive_handle_t *archive_handle;
  226. char *cpio_filename;
  227. USE_FEATURE_CPIO_O(const char *cpio_fmt = "";)
  228. unsigned opt;
  229. enum {
  230. CPIO_OPT_EXTRACT = (1 << 0),
  231. CPIO_OPT_TEST = (1 << 1),
  232. CPIO_OPT_UNCONDITIONAL = (1 << 2),
  233. CPIO_OPT_VERBOSE = (1 << 3),
  234. CPIO_OPT_FILE = (1 << 4),
  235. CPIO_OPT_CREATE_LEADING_DIR = (1 << 5),
  236. CPIO_OPT_PRESERVE_MTIME = (1 << 6),
  237. CPIO_OPT_CREATE = (1 << 7),
  238. CPIO_OPT_FORMAT = (1 << 8),
  239. };
  240. #if ENABLE_GETOPT_LONG && ENABLE_DESKTOP
  241. applet_long_options =
  242. "extract\0" No_argument "i"
  243. "list\0" No_argument "t"
  244. #if ENABLE_FEATURE_CPIO_O
  245. "create\0" No_argument "o"
  246. "format\0" Required_argument "H"
  247. #endif
  248. ;
  249. #endif
  250. /* Initialize */
  251. archive_handle = init_handle();
  252. archive_handle->src_fd = STDIN_FILENO;
  253. archive_handle->seek = seek_by_read;
  254. archive_handle->flags = ARCHIVE_EXTRACT_NEWER;
  255. #if ENABLE_FEATURE_CPIO_O
  256. opt = getopt32(argv, "ituvF:dmoH:", &cpio_filename, &cpio_fmt);
  257. if (opt & CPIO_OPT_CREATE) {
  258. if (*cpio_fmt != 'n')
  259. bb_show_usage();
  260. if (opt & CPIO_OPT_FILE) {
  261. fclose(stdout);
  262. stdout = fopen(cpio_filename, "w");
  263. /* Paranoia: I don't trust libc that much */
  264. xdup2(fileno(stdout), STDOUT_FILENO);
  265. }
  266. return cpio_o();
  267. }
  268. #else
  269. opt = getopt32(argv, "ituvF:dm", &cpio_filename);
  270. #endif
  271. argv += optind;
  272. /* One of either extract or test options must be given */
  273. if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) {
  274. bb_show_usage();
  275. }
  276. if (opt & CPIO_OPT_TEST) {
  277. /* if both extract and test options are given, ignore extract option */
  278. if (opt & CPIO_OPT_EXTRACT) {
  279. opt &= ~CPIO_OPT_EXTRACT;
  280. }
  281. archive_handle->action_header = header_list;
  282. }
  283. if (opt & CPIO_OPT_EXTRACT) {
  284. archive_handle->action_data = data_extract_all;
  285. }
  286. if (opt & CPIO_OPT_UNCONDITIONAL) {
  287. archive_handle->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
  288. archive_handle->flags &= ~ARCHIVE_EXTRACT_NEWER;
  289. }
  290. if (opt & CPIO_OPT_VERBOSE) {
  291. if (archive_handle->action_header == header_list) {
  292. archive_handle->action_header = header_verbose_list;
  293. } else {
  294. archive_handle->action_header = header_list;
  295. }
  296. }
  297. if (opt & CPIO_OPT_FILE) { /* -F */
  298. archive_handle->src_fd = xopen(cpio_filename, O_RDONLY);
  299. archive_handle->seek = seek_by_jump;
  300. }
  301. if (opt & CPIO_OPT_CREATE_LEADING_DIR) {
  302. archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
  303. }
  304. if (opt & CPIO_OPT_PRESERVE_MTIME) {
  305. archive_handle->flags |= ARCHIVE_PRESERVE_DATE;
  306. }
  307. while (*argv) {
  308. archive_handle->filter = filter_accept_list;
  309. llist_add_to(&(archive_handle->accept), *argv);
  310. argv++;
  311. }
  312. while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
  313. continue;
  314. return EXIT_SUCCESS;
  315. }