1 /* vi: set sw=4 ts=4: */
3 * Mini cpio implementation for busybox
5 * Copyright (C) 2001 by Glenn McGrath
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 * Only supports new ASCII and CRC formats
14 //config: bool "cpio (14 kb)"
17 //config: cpio is an archival utility program used to create, modify, and
18 //config: extract contents from archives.
19 //config: cpio has 110 bytes of overheads for every stored file.
21 //config: This implementation of cpio can extract cpio archives created in the
22 //config: "newc" or "crc" format.
24 //config: Unless you have a specific application which requires cpio, you
25 //config: should probably say N here.
27 //config:config FEATURE_CPIO_O
28 //config: bool "Support archive creation"
30 //config: depends on CPIO
32 //config: This implementation of cpio can create cpio archives in the "newc"
33 //config: format only.
35 //config:config FEATURE_CPIO_P
36 //config: bool "Support passthrough mode"
38 //config: depends on FEATURE_CPIO_O
40 //config: Passthrough mode. Rarely used.
42 //applet:IF_CPIO(APPLET(cpio, BB_DIR_BIN, BB_SUID_DROP))
44 //kbuild:lib-$(CONFIG_CPIO) += cpio.o
46 //usage:#define cpio_trivial_usage
47 //usage: "[-dmvu] [-F FILE] [-R USER[:GRP]]" IF_FEATURE_CPIO_O(" [-H newc]")
48 //usage: " [-ti"IF_FEATURE_CPIO_O("o")"]" IF_FEATURE_CPIO_P(" [-p DIR]")
49 //usage: " [EXTR_FILE]..."
50 //usage:#define cpio_full_usage "\n\n"
51 //usage: "Extract (-i) or list (-t) files from a cpio archive"
52 //usage: IF_FEATURE_CPIO_O(", or"
53 //usage: "\ntake file list from stdin and create an archive (-o)"
54 //usage: IF_FEATURE_CPIO_P(" or copy files (-p)")
57 //usage: "\nMain operation mode:"
59 //usage: "\n -i Extract EXTR_FILEs (or all)"
60 //usage: IF_FEATURE_CPIO_O(
61 //usage: "\n -o Create (requires -H newc)"
63 //usage: IF_FEATURE_CPIO_P(
64 //usage: "\n -p DIR Copy files to DIR"
67 //usage: "\n -d Make leading directories"
68 //usage: "\n -m Preserve mtime"
69 //usage: "\n -v Verbose"
70 //usage: "\n -u Overwrite"
71 //usage: "\n -F FILE Input (-t,-i,-p) or output (-o) file"
72 //usage: "\n -R USER[:GRP] Set owner of created files"
73 //usage: IF_FEATURE_CPIO_O(
74 //usage: "\n -H newc Archive format"
77 /* GNU cpio 2.9 --help (abridged):
80 -t, --list List the archive
81 -i, --extract Extract files from an archive
82 -o, --create Create the archive
83 -p, --pass-through Copy-pass mode
85 Options valid in any mode:
86 --block-size=SIZE I/O block size = SIZE * 512 bytes
87 -B I/O block size = 5120 bytes
88 -c Use the old portable (ASCII) archive format
89 -C, --io-size=NUMBER I/O block size in bytes
90 -f, --nonmatching Only copy files that do not match given pattern
91 -F, --file=FILE Use FILE instead of standard input or output
92 -H, --format=FORMAT Use given archive FORMAT
93 -M, --message=STRING Print STRING when the end of a volume of the
94 backup media is reached
95 -n, --numeric-uid-gid If -v, show numeric UID and GID
96 --quiet Do not print the number of blocks copied
97 --rsh-command=COMMAND Use remote COMMAND instead of rsh
98 -v, --verbose Verbosely list the files processed
99 -V, --dot Print a "." for each file processed
100 -W, --warning=FLAG Control warning display: 'none','truncate','all';
101 multiple options accumulate
103 Options valid only in --extract mode:
104 -b, --swap Swap both halfwords of words and bytes of
105 halfwords in the data (equivalent to -sS)
106 -r, --rename Interactively rename files
107 -s, --swap-bytes Swap the bytes of each halfword in the files
108 -S, --swap-halfwords Swap the halfwords of each word (4 bytes)
109 --to-stdout Extract files to standard output
110 -E, --pattern-file=FILE Read additional patterns specifying filenames to
111 extract or list from FILE
112 --only-verify-crc Verify CRC's, don't actually extract the files
114 Options valid only in --create mode:
115 -A, --append Append to an existing archive
116 -O FILE File to use instead of standard output
118 Options valid only in --pass-through mode:
119 -l, --link Link files instead of copying them, when possible
121 Options valid in --extract and --create modes:
122 --absolute-filenames Do not strip file system prefix components from
124 --no-absolute-filenames Create all files relative to the current dir
126 Options valid in --create and --pass-through modes:
127 -0, --null A list of filenames is terminated by a NUL
128 -a, --reset-access-time Reset the access times of files after reading them
129 -I FILE File to use instead of standard input
130 -L, --dereference Dereference symbolic links (copy the files
131 that they point to instead of copying the links)
132 -R, --owner=[USER][:.][GRP] Set owner of created files
134 Options valid in --extract and --pass-through modes:
135 -d, --make-directories Create leading directories where needed
136 -m, --preserve-modification-time Retain mtime when creating files
137 --no-preserve-owner Do not change the ownership of the files
138 --sparse Write files with blocks of zeros as sparse files
139 -u, --unconditional Replace all files unconditionally
143 #include "common_bufsiz.h"
144 #include "bb_archive.h"
147 OPT_EXTRACT = (1 << 0),
149 OPT_NUL_TERMINATED = (1 << 2),
150 OPT_UNCONDITIONAL = (1 << 3),
151 OPT_VERBOSE = (1 << 4),
152 OPT_CREATE_LEADING_DIR = (1 << 5),
153 OPT_PRESERVE_MTIME = (1 << 6),
154 OPT_DEREF = (1 << 7),
156 OPT_OWNER = (1 << 9),
158 IF_FEATURE_CPIO_O(OPTBIT_CREATE ,)
159 IF_FEATURE_CPIO_O(OPTBIT_FORMAT ,)
160 IF_FEATURE_CPIO_P(OPTBIT_PASSTHROUGH,)
161 IF_LONG_OPTS( OPTBIT_QUIET ,)
162 IF_LONG_OPTS( OPTBIT_2STDOUT ,)
163 OPT_CREATE = IF_FEATURE_CPIO_O((1 << OPTBIT_CREATE )) + 0,
164 OPT_FORMAT = IF_FEATURE_CPIO_O((1 << OPTBIT_FORMAT )) + 0,
165 OPT_PASSTHROUGH = IF_FEATURE_CPIO_P((1 << OPTBIT_PASSTHROUGH)) + 0,
166 OPT_QUIET = IF_LONG_OPTS( (1 << OPTBIT_QUIET )) + 0,
167 OPT_2STDOUT = IF_LONG_OPTS( (1 << OPTBIT_2STDOUT )) + 0,
170 #define OPTION_STR "it0uvdmLF:R:"
173 struct bb_uidgid_t owner_ugid;
175 #define G (*(struct globals*)bb_common_bufsiz1)
176 void BUG_cpio_globals_too_big(void);
177 #define INIT_G() do { \
178 setup_common_bufsiz(); \
179 G.owner_ugid.uid = -1L; \
180 G.owner_ugid.gid = -1L; \
183 #if ENABLE_FEATURE_CPIO_O
184 static off_t cpio_pad4(off_t size)
195 /* Return value will become exit code.
196 * It's ok to exit instead of return. */
197 static NOINLINE int cpio_o(void)
204 struct inodes_s *next;
205 struct name_s *names;
209 struct inodes_s *links = NULL;
210 off_t bytes = 0; /* output bytes count */
217 line = (option_mask32 & OPT_NUL_TERMINATED)
218 ? bb_get_chunk_from_file(stdin, NULL)
219 : xmalloc_fgetline(stdin);
222 /* Strip leading "./[./]..." from the filename */
224 while (name[0] == '.' && name[1] == '/') {
225 while (*++name == '/')
228 if (!*name) { /* line is empty */
232 if ((option_mask32 & OPT_DEREF)
237 bb_simple_perror_msg_and_die(name);
240 if (G.owner_ugid.uid != (uid_t)-1L)
241 st.st_uid = G.owner_ugid.uid;
242 if (G.owner_ugid.gid != (gid_t)-1L)
243 st.st_gid = G.owner_ugid.gid;
245 if (!(S_ISLNK(st.st_mode) || S_ISREG(st.st_mode)))
246 st.st_size = 0; /* paranoia */
248 /* Store hardlinks for later processing, dont output them */
249 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) {
253 /* Do we have this hardlink remembered? */
257 /* Not found: add new item to "links" list */
258 l = xzalloc(sizeof(*l));
264 if (l->st.st_ino == st.st_ino) {
270 /* Add new name to "l->names" list */
271 n = xmalloc(sizeof(*n) + strlen(name));
272 strcpy(n->name, name);
279 } else { /* line == NULL: EOF */
282 /* Output hardlink's data */
284 name = links->names->name;
285 links->names = links->names->next;
286 /* GNU cpio is reported to emit file data
287 * only for the last instance. Mimic that. */
288 if (links->names == NULL)
292 /* NB: we leak links->names and/or links,
293 * this is intended (we exit soon anyway) */
295 /* If no (more) hardlinks to output,
296 * output "trailer" entry */
298 /* st.st_size == 0 is a must, but for uniformity
299 * in the output, we zero out everything */
300 memset(&st, 0, sizeof(st));
301 /* st.st_nlink = 1; - GNU cpio does this */
305 bytes += printf("070701"
306 "%08X%08X%08X%08X%08X%08X%08X"
307 "%08X%08X%08X%08X" /* GNU cpio uses uppercase hex */
308 /* strlen+1: */ "%08X"
309 /* chksum: */ "00000000" /* (only for "070702" files) */
310 /* name,NUL: */ "%s%c",
311 (unsigned)(uint32_t) st.st_ino,
312 (unsigned)(uint32_t) st.st_mode,
313 (unsigned)(uint32_t) st.st_uid,
314 (unsigned)(uint32_t) st.st_gid,
315 (unsigned)(uint32_t) st.st_nlink,
316 (unsigned)(uint32_t) st.st_mtime,
317 (unsigned)(uint32_t) st.st_size,
318 (unsigned)(uint32_t) major(st.st_dev),
319 (unsigned)(uint32_t) minor(st.st_dev),
320 (unsigned)(uint32_t) major(st.st_rdev),
321 (unsigned)(uint32_t) minor(st.st_rdev),
322 (unsigned)(strlen(name) + 1),
324 bytes = cpio_pad4(bytes);
327 if (S_ISLNK(st.st_mode)) {
328 char *lpath = xmalloc_readlink_or_warn(name);
331 bytes += printf("%s", lpath);
333 } else { /* S_ISREG */
334 int fd = xopen(name, O_RDONLY);
336 /* We must abort if file got shorter too! */
337 bb_copyfd_exact_size(fd, STDOUT_FILENO, st.st_size);
341 bytes = cpio_pad4(bytes);
345 if (name != cpio_TRAILER)
347 /* TODO: GNU cpio pads trailer to 512 bytes, do we want that? */
352 } /* end of "while (1)" */
356 int cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
357 int cpio_main(int argc UNUSED_PARAM, char **argv)
359 archive_handle_t *archive_handle;
362 IF_FEATURE_CPIO_O(const char *cpio_fmt = "";)
365 const char *long_opts =
366 "extract\0" No_argument "i"
367 "list\0" No_argument "t"
368 #if ENABLE_FEATURE_CPIO_O
369 "create\0" No_argument "o"
370 "format\0" Required_argument "H"
371 #if ENABLE_FEATURE_CPIO_P
372 "pass-through\0" No_argument "p"
375 "owner\0" Required_argument "R"
376 "verbose\0" No_argument "v"
377 "quiet\0" No_argument "\xff"
378 "to-stdout\0" No_argument "\xfe"
383 archive_handle = init_handle();
384 /* archive_handle->src_fd = STDIN_FILENO; - done by init_handle */
385 archive_handle->ah_flags = ARCHIVE_EXTRACT_NEWER;
387 /* As of now we do not enforce this: */
388 /* -i,-t,-o,-p are mutually exclusive */
389 /* -u,-d,-m make sense only with -i or -p */
390 /* -L makes sense only with -o or -p */
392 #if !ENABLE_FEATURE_CPIO_O
393 opt = getopt32long(argv, OPTION_STR, long_opts, &cpio_filename, &cpio_owner);
395 opt = getopt32long(argv, OPTION_STR "oH:" IF_FEATURE_CPIO_P("p"), long_opts,
396 &cpio_filename, &cpio_owner, &cpio_fmt);
399 if (opt & OPT_OWNER) { /* -R */
400 parse_chown_usergroup_or_die(&G.owner_ugid, cpio_owner);
401 archive_handle->cpio__owner = G.owner_ugid;
403 #if !ENABLE_FEATURE_CPIO_O
404 if (opt & OPT_FILE) { /* -F */
405 xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO);
408 if ((opt & (OPT_FILE|OPT_CREATE)) == OPT_FILE) { /* -F without -o */
409 xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO);
411 if (opt & OPT_PASSTHROUGH) {
417 if (opt & OPT_CREATE_LEADING_DIR)
418 mkdir(argv[0], 0777);
419 /* Crude existence check:
420 * close(xopen(argv[0], O_RDONLY | O_DIRECTORY));
421 * We can also xopen, fstat, IS_DIR, later fchdir.
422 * This would check for existence earlier and cleaner.
423 * As it stands now, if we fail xchdir later,
424 * child dies on EPIPE, unless it caught
425 * a diffrerent problem earlier.
426 * This is good enough for now.
439 pid = fork_or_rexec(argv - optind);
440 if (pid == 0) { /* child */
442 xmove_fd(pp.wr, STDOUT_FILENO);
446 USE_FOR_NOMMU(argv[-optind][0] &= 0x7f); /* undo fork_or_rexec() damage */
449 xmove_fd(pp.rd, STDIN_FILENO);
450 //opt &= ~OPT_PASSTHROUGH;
455 if (opt & OPT_CREATE) {
456 if (cpio_fmt[0] != 'n') /* we _require_ "-H newc" */
458 if (opt & OPT_FILE) {
459 xmove_fd(xopen(cpio_filename, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
467 /* One of either extract or test options must be given */
468 if ((opt & (OPT_TEST | OPT_EXTRACT)) == 0) {
472 if (opt & OPT_TEST) {
473 /* if both extract and test options are given, ignore extract option */
475 archive_handle->action_header = header_list;
477 if (opt & OPT_EXTRACT) {
478 archive_handle->action_data = data_extract_all;
479 if (opt & OPT_2STDOUT)
480 archive_handle->action_data = data_extract_to_stdout;
482 if (opt & OPT_UNCONDITIONAL) {
483 archive_handle->ah_flags |= ARCHIVE_UNLINK_OLD;
484 archive_handle->ah_flags &= ~ARCHIVE_EXTRACT_NEWER;
486 if (opt & OPT_VERBOSE) {
487 if (archive_handle->action_header == header_list) {
488 archive_handle->action_header = header_verbose_list;
490 archive_handle->action_header = header_list;
493 if (opt & OPT_CREATE_LEADING_DIR) {
494 archive_handle->ah_flags |= ARCHIVE_CREATE_LEADING_DIRS;
496 if (opt & OPT_PRESERVE_MTIME) {
497 archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE;
501 archive_handle->filter = filter_accept_list;
502 llist_add_to(&archive_handle->accept, *argv);
506 /* see get_header_cpio */
507 archive_handle->cpio__blocks = (off_t)-1;
508 while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
511 if (archive_handle->cpio__blocks != (off_t)-1
512 && !(opt & OPT_QUIET)
514 fprintf(stderr, "%"OFF_FMT"u blocks\n", archive_handle->cpio__blocks);