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 #include "bb_archive.h"
20 //config: cpio is an archival utility program used to create, modify, and
21 //config: extract contents from archives.
22 //config: cpio has 110 bytes of overheads for every stored file.
24 //config: This implementation of cpio can extract cpio archives created in the
25 //config: "newc" or "crc" format, it cannot create or modify them.
27 //config: Unless you have a specific application which requires cpio, you
28 //config: should probably say N here.
30 //config:config FEATURE_CPIO_O
31 //config: bool "Support for archive creation"
33 //config: depends on CPIO
35 //config: This implementation of cpio can create cpio archives in the "newc"
36 //config: format only.
38 //config:config FEATURE_CPIO_P
39 //config: bool "Support for passthrough mode"
41 //config: depends on FEATURE_CPIO_O
43 //config: Passthrough mode. Rarely used.
45 //applet:IF_CPIO(APPLET(cpio, BB_DIR_BIN, BB_SUID_DROP))
46 //kbuild:lib-$(CONFIG_CPIO) += cpio.o
48 //usage:#define cpio_trivial_usage
49 //usage: "[-dmvu] [-F FILE]" IF_FEATURE_CPIO_O(" [-H newc]")
50 //usage: " [-ti"IF_FEATURE_CPIO_O("o")"]" IF_FEATURE_CPIO_P(" [-p DIR]")
51 //usage: " [EXTR_FILE]..."
52 //usage:#define cpio_full_usage "\n\n"
53 //usage: "Extract or list files from a cpio archive"
54 //usage: IF_FEATURE_CPIO_O(", or"
55 //usage: "\ncreate an archive" IF_FEATURE_CPIO_P(" (-o) or copy files (-p)")
56 //usage: " using file list on stdin"
59 //usage: "\nMain operation mode:"
61 //usage: "\n -i Extract EXTR_FILEs (or all)"
62 //usage: IF_FEATURE_CPIO_O(
63 //usage: "\n -o Create (requires -H newc)"
65 //usage: IF_FEATURE_CPIO_P(
66 //usage: "\n -p DIR Copy files to DIR"
69 //usage: "\n -d Make leading directories"
70 //usage: "\n -m Preserve mtime"
71 //usage: "\n -v Verbose"
72 //usage: "\n -u Overwrite"
73 //usage: "\n -F FILE Input (-t,-i,-p) or output (-o) file"
74 //usage: IF_FEATURE_CPIO_O(
75 //usage: "\n -H newc Archive format"
78 /* GNU cpio 2.9 --help (abridged):
81 -t, --list List the archive
82 -i, --extract Extract files from an archive
83 -o, --create Create the archive
84 -p, --pass-through Copy-pass mode
86 Options valid in any mode:
87 --block-size=SIZE I/O block size = SIZE * 512 bytes
88 -B I/O block size = 5120 bytes
89 -c Use the old portable (ASCII) archive format
90 -C, --io-size=NUMBER I/O block size in bytes
91 -f, --nonmatching Only copy files that do not match given pattern
92 -F, --file=FILE Use FILE instead of standard input or output
93 -H, --format=FORMAT Use given archive FORMAT
94 -M, --message=STRING Print STRING when the end of a volume of the
95 backup media is reached
96 -n, --numeric-uid-gid If -v, show numeric UID and GID
97 --quiet Do not print the number of blocks copied
98 --rsh-command=COMMAND Use remote COMMAND instead of rsh
99 -v, --verbose Verbosely list the files processed
100 -V, --dot Print a "." for each file processed
101 -W, --warning=FLAG Control warning display: 'none','truncate','all';
102 multiple options accumulate
104 Options valid only in --extract mode:
105 -b, --swap Swap both halfwords of words and bytes of
106 halfwords in the data (equivalent to -sS)
107 -r, --rename Interactively rename files
108 -s, --swap-bytes Swap the bytes of each halfword in the files
109 -S, --swap-halfwords Swap the halfwords of each word (4 bytes)
110 --to-stdout Extract files to standard output
111 -E, --pattern-file=FILE Read additional patterns specifying filenames to
112 extract or list from FILE
113 --only-verify-crc Verify CRC's, don't actually extract the files
115 Options valid only in --create mode:
116 -A, --append Append to an existing archive
117 -O FILE File to use instead of standard output
119 Options valid only in --pass-through mode:
120 -l, --link Link files instead of copying them, when possible
122 Options valid in --extract and --create modes:
123 --absolute-filenames Do not strip file system prefix components from
125 --no-absolute-filenames Create all files relative to the current dir
127 Options valid in --create and --pass-through modes:
128 -0, --null A list of filenames is terminated by a NUL
129 -a, --reset-access-time Reset the access times of files after reading them
130 -I FILE File to use instead of standard input
131 -L, --dereference Dereference symbolic links (copy the files
132 that they point to instead of copying the links)
133 -R, --owner=[USER][:.][GROUP] Set owner of created files
135 Options valid in --extract and --pass-through modes:
136 -d, --make-directories Create leading directories where needed
137 -m, --preserve-modification-time Retain mtime when creating files
138 --no-preserve-owner Do not change the ownership of the files
139 --sparse Write files with blocks of zeros as sparse files
140 -u, --unconditional Replace all files unconditionally
144 OPT_EXTRACT
= (1 << 0),
146 OPT_NUL_TERMINATED
= (1 << 2),
147 OPT_UNCONDITIONAL
= (1 << 3),
148 OPT_VERBOSE
= (1 << 4),
149 OPT_CREATE_LEADING_DIR
= (1 << 5),
150 OPT_PRESERVE_MTIME
= (1 << 6),
151 OPT_DEREF
= (1 << 7),
154 IF_FEATURE_CPIO_O(OPTBIT_CREATE
,)
155 IF_FEATURE_CPIO_O(OPTBIT_FORMAT
,)
156 IF_FEATURE_CPIO_P(OPTBIT_PASSTHROUGH
,)
157 IF_LONG_OPTS( OPTBIT_QUIET
,)
158 IF_LONG_OPTS( OPTBIT_2STDOUT
,)
159 OPT_CREATE
= IF_FEATURE_CPIO_O((1 << OPTBIT_CREATE
)) + 0,
160 OPT_FORMAT
= IF_FEATURE_CPIO_O((1 << OPTBIT_FORMAT
)) + 0,
161 OPT_PASSTHROUGH
= IF_FEATURE_CPIO_P((1 << OPTBIT_PASSTHROUGH
)) + 0,
162 OPT_QUIET
= IF_LONG_OPTS( (1 << OPTBIT_QUIET
)) + 0,
163 OPT_2STDOUT
= IF_LONG_OPTS( (1 << OPTBIT_2STDOUT
)) + 0,
166 #define OPTION_STR "it0uvdmLF:"
168 #if ENABLE_FEATURE_CPIO_O
169 static off_t
cpio_pad4(off_t size
)
180 /* Return value will become exit code.
181 * It's ok to exit instead of return. */
182 static NOINLINE
int cpio_o(void)
184 static const char trailer
[] ALIGN1
= "TRAILER!!!";
190 struct inodes_s
*next
;
191 struct name_s
*names
;
195 struct inodes_s
*links
= NULL
;
196 off_t bytes
= 0; /* output bytes count */
203 line
= (option_mask32
& OPT_NUL_TERMINATED
)
204 ? bb_get_chunk_from_file(stdin
, NULL
)
205 : xmalloc_fgetline(stdin
);
208 /* Strip leading "./[./]..." from the filename */
210 while (name
[0] == '.' && name
[1] == '/') {
211 while (*++name
== '/')
214 if (!*name
) { /* line is empty */
218 if ((option_mask32
& OPT_DEREF
)
223 bb_simple_perror_msg_and_die(name
);
226 if (!(S_ISLNK(st
.st_mode
) || S_ISREG(st
.st_mode
)))
227 st
.st_size
= 0; /* paranoia */
229 /* Store hardlinks for later processing, dont output them */
230 if (!S_ISDIR(st
.st_mode
) && st
.st_nlink
> 1) {
234 /* Do we have this hardlink remembered? */
238 /* Not found: add new item to "links" list */
239 l
= xzalloc(sizeof(*l
));
245 if (l
->st
.st_ino
== st
.st_ino
) {
251 /* Add new name to "l->names" list */
252 n
= xmalloc(sizeof(*n
) + strlen(name
));
253 strcpy(n
->name
, name
);
261 } else { /* line == NULL: EOF */
264 /* Output hardlink's data */
266 name
= links
->names
->name
;
267 links
->names
= links
->names
->next
;
268 /* GNU cpio is reported to emit file data
269 * only for the last instance. Mimic that. */
270 if (links
->names
== NULL
)
274 /* NB: we leak links->names and/or links,
275 * this is intended (we exit soon anyway) */
277 /* If no (more) hardlinks to output,
278 * output "trailer" entry */
280 /* st.st_size == 0 is a must, but for uniformity
281 * in the output, we zero out everything */
282 memset(&st
, 0, sizeof(st
));
283 /* st.st_nlink = 1; - GNU cpio does this */
287 bytes
+= printf("070701"
288 "%08X%08X%08X%08X%08X%08X%08X"
289 "%08X%08X%08X%08X" /* GNU cpio uses uppercase hex */
290 /* strlen+1: */ "%08X"
291 /* chksum: */ "00000000" /* (only for "070702" files) */
292 /* name,NUL: */ "%s%c",
293 (unsigned)(uint32_t) st
.st_ino
,
294 (unsigned)(uint32_t) st
.st_mode
,
295 (unsigned)(uint32_t) st
.st_uid
,
296 (unsigned)(uint32_t) st
.st_gid
,
297 (unsigned)(uint32_t) st
.st_nlink
,
298 (unsigned)(uint32_t) st
.st_mtime
,
299 (unsigned)(uint32_t) st
.st_size
,
300 (unsigned)(uint32_t) major(st
.st_dev
),
301 (unsigned)(uint32_t) minor(st
.st_dev
),
302 (unsigned)(uint32_t) major(st
.st_rdev
),
303 (unsigned)(uint32_t) minor(st
.st_rdev
),
304 (unsigned)(strlen(name
) + 1),
306 bytes
= cpio_pad4(bytes
);
309 if (S_ISLNK(st
.st_mode
)) {
310 char *lpath
= xmalloc_readlink_or_warn(name
);
313 bytes
+= printf("%s", lpath
);
315 } else { /* S_ISREG */
316 int fd
= xopen(name
, O_RDONLY
);
318 /* We must abort if file got shorter too! */
319 bb_copyfd_exact_size(fd
, STDOUT_FILENO
, st
.st_size
);
323 bytes
= cpio_pad4(bytes
);
329 /* TODO: GNU cpio pads trailer to 512 bytes, do we want that? */
334 } /* end of "while (1)" */
338 int cpio_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
339 int cpio_main(int argc UNUSED_PARAM
, char **argv
)
341 archive_handle_t
*archive_handle
;
343 IF_FEATURE_CPIO_O(const char *cpio_fmt
= "";)
347 applet_long_options
=
348 "extract\0" No_argument
"i"
349 "list\0" No_argument
"t"
350 #if ENABLE_FEATURE_CPIO_O
351 "create\0" No_argument
"o"
352 "format\0" Required_argument
"H"
353 #if ENABLE_FEATURE_CPIO_P
354 "pass-through\0" No_argument
"p"
357 "verbose\0" No_argument
"v"
358 "quiet\0" No_argument
"\xff"
359 "to-stdout\0" No_argument
"\xfe"
363 archive_handle
= init_handle();
364 /* archive_handle->src_fd = STDIN_FILENO; - done by init_handle */
365 archive_handle
->ah_flags
= ARCHIVE_EXTRACT_NEWER
;
367 /* As of now we do not enforce this: */
368 /* -i,-t,-o,-p are mutually exclusive */
369 /* -u,-d,-m make sense only with -i or -p */
370 /* -L makes sense only with -o or -p */
372 #if !ENABLE_FEATURE_CPIO_O
373 opt
= getopt32(argv
, OPTION_STR
, &cpio_filename
);
375 if (opt
& OPT_FILE
) { /* -F */
376 xmove_fd(xopen(cpio_filename
, O_RDONLY
), STDIN_FILENO
);
379 opt
= getopt32(argv
, OPTION_STR
"oH:" IF_FEATURE_CPIO_P("p"), &cpio_filename
, &cpio_fmt
);
381 if ((opt
& (OPT_FILE
|OPT_CREATE
)) == OPT_FILE
) { /* -F without -o */
382 xmove_fd(xopen(cpio_filename
, O_RDONLY
), STDIN_FILENO
);
384 if (opt
& OPT_PASSTHROUGH
) {
390 if (opt
& OPT_CREATE_LEADING_DIR
)
391 mkdir(argv
[0], 0777);
392 /* Crude existence check:
393 * close(xopen(argv[0], O_RDONLY | O_DIRECTORY));
394 * We can also xopen, fstat, IS_DIR, later fchdir.
395 * This would check for existence earlier and cleaner.
396 * As it stands now, if we fail xchdir later,
397 * child dies on EPIPE, unless it caught
398 * a diffrerent problem earlier.
399 * This is good enough for now.
412 pid
= fork_or_rexec(argv
- optind
);
413 if (pid
== 0) { /* child */
415 xmove_fd(pp
.wr
, STDOUT_FILENO
);
419 USE_FOR_NOMMU(argv
[-optind
][0] &= 0x7f); /* undo fork_or_rexec() damage */
422 xmove_fd(pp
.rd
, STDIN_FILENO
);
423 //opt &= ~OPT_PASSTHROUGH;
428 if (opt
& OPT_CREATE
) {
429 if (cpio_fmt
[0] != 'n') /* we _require_ "-H newc" */
431 if (opt
& OPT_FILE
) {
432 xmove_fd(xopen(cpio_filename
, O_WRONLY
| O_CREAT
| O_TRUNC
), STDOUT_FILENO
);
440 /* One of either extract or test options must be given */
441 if ((opt
& (OPT_TEST
| OPT_EXTRACT
)) == 0) {
445 if (opt
& OPT_TEST
) {
446 /* if both extract and test options are given, ignore extract option */
448 archive_handle
->action_header
= header_list
;
450 if (opt
& OPT_EXTRACT
) {
451 archive_handle
->action_data
= data_extract_all
;
452 if (opt
& OPT_2STDOUT
)
453 archive_handle
->action_data
= data_extract_to_stdout
;
455 if (opt
& OPT_UNCONDITIONAL
) {
456 archive_handle
->ah_flags
|= ARCHIVE_UNLINK_OLD
;
457 archive_handle
->ah_flags
&= ~ARCHIVE_EXTRACT_NEWER
;
459 if (opt
& OPT_VERBOSE
) {
460 if (archive_handle
->action_header
== header_list
) {
461 archive_handle
->action_header
= header_verbose_list
;
463 archive_handle
->action_header
= header_list
;
466 if (opt
& OPT_CREATE_LEADING_DIR
) {
467 archive_handle
->ah_flags
|= ARCHIVE_CREATE_LEADING_DIRS
;
469 if (opt
& OPT_PRESERVE_MTIME
) {
470 archive_handle
->ah_flags
|= ARCHIVE_RESTORE_DATE
;
474 archive_handle
->filter
= filter_accept_list
;
475 llist_add_to(&archive_handle
->accept
, *argv
);
479 /* see get_header_cpio */
480 archive_handle
->cpio__blocks
= (off_t
)-1;
481 while (get_header_cpio(archive_handle
) == EXIT_SUCCESS
)
484 if (archive_handle
->cpio__blocks
!= (off_t
)-1
485 && !(opt
& OPT_QUIET
)
487 fprintf(stderr
, "%"OFF_FMT
"u blocks\n", archive_handle
->cpio__blocks
);