blob: 82b3fe5ed99668118b6c9781a110cb6ebf961478 [file] [log] [blame]
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini cpio implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 2001 by Glenn McGrath
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +00008 *
9 * Limitations:
Denis Vlasenko261f2372008-04-05 00:07:46 +000010 * Doesn't check CRC's
11 * Only supports new ASCII and CRC formats
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000012 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +020014#include "bb_archive.h"
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000015
Denys Vlasenkof6beef62013-11-14 11:39:00 +010016//config:config CPIO
17//config: bool "cpio"
18//config: default y
19//config: help
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.
23//config:
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.
26//config:
27//config: Unless you have a specific application which requires cpio, you
28//config: should probably say N here.
29//config:
30//config:config FEATURE_CPIO_O
31//config: bool "Support for archive creation"
32//config: default y
33//config: depends on CPIO
34//config: help
35//config: This implementation of cpio can create cpio archives in the "newc"
36//config: format only.
37//config:
38//config:config FEATURE_CPIO_P
39//config: bool "Support for passthrough mode"
40//config: default y
41//config: depends on FEATURE_CPIO_O
42//config: help
43//config: Passthrough mode. Rarely used.
44
Denys Vlasenko36184a42013-11-14 09:54:24 +010045//applet:IF_CPIO(APPLET(cpio, BB_DIR_BIN, BB_SUID_DROP))
Denys Vlasenko66620fa2013-11-14 09:53:52 +010046//kbuild:lib-$(CONFIG_CPIO) += cpio.o
47
Denys Vlasenko1f937d62010-12-26 02:22:51 +010048//usage:#define cpio_trivial_usage
Aaro Koskinen2735bc02015-10-16 17:24:46 +020049//usage: "[-dmvu] [-F FILE] [-R USER[:GRP]]" IF_FEATURE_CPIO_O(" [-H newc]")
Denys Vlasenko1f937d62010-12-26 02:22:51 +010050//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"
57//usage: )
58//usage: "\n"
59//usage: "\nMain operation mode:"
60//usage: "\n -t List"
61//usage: "\n -i Extract EXTR_FILEs (or all)"
62//usage: IF_FEATURE_CPIO_O(
63//usage: "\n -o Create (requires -H newc)"
64//usage: )
65//usage: IF_FEATURE_CPIO_P(
66//usage: "\n -p DIR Copy files to DIR"
67//usage: )
Denys Vlasenko184b2662014-06-30 17:19:17 +020068//usage: "\nOptions:"
Denys Vlasenko1f937d62010-12-26 02:22:51 +010069//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"
Aaro Koskinen2735bc02015-10-16 17:24:46 +020074//usage: "\n -R USER[:GRP] Set owner of created files"
Denys Vlasenko1f937d62010-12-26 02:22:51 +010075//usage: IF_FEATURE_CPIO_O(
76//usage: "\n -H newc Archive format"
77//usage: )
78
Denis Vlasenko05af8322009-03-20 23:01:48 +000079/* GNU cpio 2.9 --help (abridged):
80
81 Modes:
82 -t, --list List the archive
83 -i, --extract Extract files from an archive
84 -o, --create Create the archive
Denys Vlasenko1f937d62010-12-26 02:22:51 +010085 -p, --pass-through Copy-pass mode
Denis Vlasenko05af8322009-03-20 23:01:48 +000086
87 Options valid in any mode:
88 --block-size=SIZE I/O block size = SIZE * 512 bytes
89 -B I/O block size = 5120 bytes
90 -c Use the old portable (ASCII) archive format
91 -C, --io-size=NUMBER I/O block size in bytes
92 -f, --nonmatching Only copy files that do not match given pattern
93 -F, --file=FILE Use FILE instead of standard input or output
94 -H, --format=FORMAT Use given archive FORMAT
95 -M, --message=STRING Print STRING when the end of a volume of the
96 backup media is reached
97 -n, --numeric-uid-gid If -v, show numeric UID and GID
98 --quiet Do not print the number of blocks copied
99 --rsh-command=COMMAND Use remote COMMAND instead of rsh
100 -v, --verbose Verbosely list the files processed
101 -V, --dot Print a "." for each file processed
102 -W, --warning=FLAG Control warning display: 'none','truncate','all';
103 multiple options accumulate
104
105 Options valid only in --extract mode:
106 -b, --swap Swap both halfwords of words and bytes of
107 halfwords in the data (equivalent to -sS)
108 -r, --rename Interactively rename files
109 -s, --swap-bytes Swap the bytes of each halfword in the files
110 -S, --swap-halfwords Swap the halfwords of each word (4 bytes)
111 --to-stdout Extract files to standard output
112 -E, --pattern-file=FILE Read additional patterns specifying filenames to
113 extract or list from FILE
114 --only-verify-crc Verify CRC's, don't actually extract the files
115
116 Options valid only in --create mode:
117 -A, --append Append to an existing archive
118 -O FILE File to use instead of standard output
119
120 Options valid only in --pass-through mode:
121 -l, --link Link files instead of copying them, when possible
122
123 Options valid in --extract and --create modes:
124 --absolute-filenames Do not strip file system prefix components from
125 the file names
126 --no-absolute-filenames Create all files relative to the current dir
127
128 Options valid in --create and --pass-through modes:
129 -0, --null A list of filenames is terminated by a NUL
130 -a, --reset-access-time Reset the access times of files after reading them
131 -I FILE File to use instead of standard input
132 -L, --dereference Dereference symbolic links (copy the files
133 that they point to instead of copying the links)
Aaro Koskinen2735bc02015-10-16 17:24:46 +0200134 -R, --owner=[USER][:.][GRP] Set owner of created files
Denis Vlasenko05af8322009-03-20 23:01:48 +0000135
136 Options valid in --extract and --pass-through modes:
137 -d, --make-directories Create leading directories where needed
138 -m, --preserve-modification-time Retain mtime when creating files
139 --no-preserve-owner Do not change the ownership of the files
140 --sparse Write files with blocks of zeros as sparse files
141 -u, --unconditional Replace all files unconditionally
142 */
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100143
Denis Vlasenko05af8322009-03-20 23:01:48 +0000144enum {
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100145 OPT_EXTRACT = (1 << 0),
146 OPT_TEST = (1 << 1),
147 OPT_NUL_TERMINATED = (1 << 2),
148 OPT_UNCONDITIONAL = (1 << 3),
149 OPT_VERBOSE = (1 << 4),
150 OPT_CREATE_LEADING_DIR = (1 << 5),
151 OPT_PRESERVE_MTIME = (1 << 6),
152 OPT_DEREF = (1 << 7),
153 OPT_FILE = (1 << 8),
Aaro Koskinen2735bc02015-10-16 17:24:46 +0200154 OPT_OWNER = (1 << 9),
155 OPTBIT_OWNER = 9,
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200156 IF_FEATURE_CPIO_O(OPTBIT_CREATE ,)
157 IF_FEATURE_CPIO_O(OPTBIT_FORMAT ,)
158 IF_FEATURE_CPIO_P(OPTBIT_PASSTHROUGH,)
159 IF_LONG_OPTS( OPTBIT_QUIET ,)
160 IF_LONG_OPTS( OPTBIT_2STDOUT ,)
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100161 OPT_CREATE = IF_FEATURE_CPIO_O((1 << OPTBIT_CREATE )) + 0,
162 OPT_FORMAT = IF_FEATURE_CPIO_O((1 << OPTBIT_FORMAT )) + 0,
163 OPT_PASSTHROUGH = IF_FEATURE_CPIO_P((1 << OPTBIT_PASSTHROUGH)) + 0,
164 OPT_QUIET = IF_LONG_OPTS( (1 << OPTBIT_QUIET )) + 0,
165 OPT_2STDOUT = IF_LONG_OPTS( (1 << OPTBIT_2STDOUT )) + 0,
Denis Vlasenko05af8322009-03-20 23:01:48 +0000166};
167
Aaro Koskinen2735bc02015-10-16 17:24:46 +0200168#define OPTION_STR "it0uvdmLF:R:"
169
170struct globals {
171 struct bb_uidgid_t owner_ugid;
172} FIX_ALIASING;
173#define G (*(struct globals*)&bb_common_bufsiz1)
174void BUG_cpio_globals_too_big(void);
175#define INIT_G() do { \
176 G.owner_ugid.uid = -1L; \
177 G.owner_ugid.gid = -1L; \
178} while (0)
Denis Vlasenko05af8322009-03-20 23:01:48 +0000179
Denis Vlasenko261f2372008-04-05 00:07:46 +0000180#if ENABLE_FEATURE_CPIO_O
181static off_t cpio_pad4(off_t size)
182{
183 int i;
184
185 i = (- size) & 3;
186 size += i;
187 while (--i >= 0)
188 bb_putchar('\0');
189 return size;
190}
191
192/* Return value will become exit code.
193 * It's ok to exit instead of return. */
Denys Vlasenkoa7bb3c12009-10-08 12:28:08 +0200194static NOINLINE int cpio_o(void)
Denis Vlasenko261f2372008-04-05 00:07:46 +0000195{
196 struct name_s {
197 struct name_s *next;
198 char name[1];
199 };
200 struct inodes_s {
201 struct inodes_s *next;
202 struct name_s *names;
203 struct stat st;
204 };
205
206 struct inodes_s *links = NULL;
207 off_t bytes = 0; /* output bytes count */
208
209 while (1) {
210 const char *name;
211 char *line;
212 struct stat st;
213
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100214 line = (option_mask32 & OPT_NUL_TERMINATED)
Denis Vlasenko05af8322009-03-20 23:01:48 +0000215 ? bb_get_chunk_from_file(stdin, NULL)
216 : xmalloc_fgetline(stdin);
Denis Vlasenko261f2372008-04-05 00:07:46 +0000217
218 if (line) {
219 /* Strip leading "./[./]..." from the filename */
220 name = line;
221 while (name[0] == '.' && name[1] == '/') {
222 while (*++name == '/')
223 continue;
224 }
225 if (!*name) { /* line is empty */
226 free(line);
227 continue;
228 }
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100229 if ((option_mask32 & OPT_DEREF)
Denis Vlasenko05af8322009-03-20 23:01:48 +0000230 ? stat(name, &st)
231 : lstat(name, &st)
232 ) {
Denis Vlasenko261f2372008-04-05 00:07:46 +0000233 abort_cpio_o:
234 bb_simple_perror_msg_and_die(name);
235 }
236
Aaro Koskinen2735bc02015-10-16 17:24:46 +0200237 if (G.owner_ugid.uid != (uid_t)-1L)
238 st.st_uid = G.owner_ugid.uid;
239 if (G.owner_ugid.gid != (gid_t)-1L)
240 st.st_gid = G.owner_ugid.gid;
241
Denis Vlasenko261f2372008-04-05 00:07:46 +0000242 if (!(S_ISLNK(st.st_mode) || S_ISREG(st.st_mode)))
243 st.st_size = 0; /* paranoia */
244
245 /* Store hardlinks for later processing, dont output them */
246 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) {
247 struct name_s *n;
248 struct inodes_s *l;
249
250 /* Do we have this hardlink remembered? */
251 l = links;
252 while (1) {
253 if (l == NULL) {
254 /* Not found: add new item to "links" list */
255 l = xzalloc(sizeof(*l));
256 l->st = st;
257 l->next = links;
258 links = l;
259 break;
260 }
261 if (l->st.st_ino == st.st_ino) {
262 /* found */
263 break;
264 }
265 l = l->next;
266 }
267 /* Add new name to "l->names" list */
268 n = xmalloc(sizeof(*n) + strlen(name));
269 strcpy(n->name, name);
270 n->next = l->names;
271 l->names = n;
272
273 free(line);
274 continue;
275 }
Denis Vlasenko261f2372008-04-05 00:07:46 +0000276 } else { /* line == NULL: EOF */
277 next_link:
278 if (links) {
279 /* Output hardlink's data */
280 st = links->st;
281 name = links->names->name;
282 links->names = links->names->next;
283 /* GNU cpio is reported to emit file data
284 * only for the last instance. Mimic that. */
285 if (links->names == NULL)
286 links = links->next;
287 else
288 st.st_size = 0;
289 /* NB: we leak links->names and/or links,
290 * this is intended (we exit soon anyway) */
291 } else {
292 /* If no (more) hardlinks to output,
293 * output "trailer" entry */
Aaro Koskinen2735bc02015-10-16 17:24:46 +0200294 name = cpio_TRAILER;
Denis Vlasenko261f2372008-04-05 00:07:46 +0000295 /* st.st_size == 0 is a must, but for uniformity
296 * in the output, we zero out everything */
297 memset(&st, 0, sizeof(st));
298 /* st.st_nlink = 1; - GNU cpio does this */
299 }
300 }
301
302 bytes += printf("070701"
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100303 "%08X%08X%08X%08X%08X%08X%08X"
304 "%08X%08X%08X%08X" /* GNU cpio uses uppercase hex */
Denis Vlasenko261f2372008-04-05 00:07:46 +0000305 /* strlen+1: */ "%08X"
306 /* chksum: */ "00000000" /* (only for "070702" files) */
307 /* name,NUL: */ "%s%c",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100308 (unsigned)(uint32_t) st.st_ino,
309 (unsigned)(uint32_t) st.st_mode,
310 (unsigned)(uint32_t) st.st_uid,
311 (unsigned)(uint32_t) st.st_gid,
312 (unsigned)(uint32_t) st.st_nlink,
313 (unsigned)(uint32_t) st.st_mtime,
314 (unsigned)(uint32_t) st.st_size,
315 (unsigned)(uint32_t) major(st.st_dev),
316 (unsigned)(uint32_t) minor(st.st_dev),
317 (unsigned)(uint32_t) major(st.st_rdev),
318 (unsigned)(uint32_t) minor(st.st_rdev),
319 (unsigned)(strlen(name) + 1),
320 name, '\0');
Denis Vlasenko261f2372008-04-05 00:07:46 +0000321 bytes = cpio_pad4(bytes);
322
323 if (st.st_size) {
324 if (S_ISLNK(st.st_mode)) {
325 char *lpath = xmalloc_readlink_or_warn(name);
326 if (!lpath)
327 goto abort_cpio_o;
328 bytes += printf("%s", lpath);
329 free(lpath);
330 } else { /* S_ISREG */
331 int fd = xopen(name, O_RDONLY);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100332 fflush_all();
Denis Vlasenko261f2372008-04-05 00:07:46 +0000333 /* We must abort if file got shorter too! */
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000334 bb_copyfd_exact_size(fd, STDOUT_FILENO, st.st_size);
Denis Vlasenko261f2372008-04-05 00:07:46 +0000335 bytes += st.st_size;
336 close(fd);
337 }
338 bytes = cpio_pad4(bytes);
339 }
340
341 if (!line) {
Aaro Koskinen2735bc02015-10-16 17:24:46 +0200342 if (name != cpio_TRAILER)
Denis Vlasenko261f2372008-04-05 00:07:46 +0000343 goto next_link;
344 /* TODO: GNU cpio pads trailer to 512 bytes, do we want that? */
345 return EXIT_SUCCESS;
346 }
347
348 free(line);
349 } /* end of "while (1)" */
350}
351#endif
Glenn L McGrath10b78132004-02-25 09:30:06 +0000352
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000353int cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000354int cpio_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +0000355{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000356 archive_handle_t *archive_handle;
Denis Vlasenko261f2372008-04-05 00:07:46 +0000357 char *cpio_filename;
Aaro Koskinen2735bc02015-10-16 17:24:46 +0200358 char *cpio_owner;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000359 IF_FEATURE_CPIO_O(const char *cpio_fmt = "";)
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000360 unsigned opt;
Denis Vlasenko2b407b12008-07-11 21:42:12 +0000361
Denys Vlasenkof3b92d32009-06-19 12:10:38 +0200362#if ENABLE_LONG_OPTS
Denis Vlasenko2b407b12008-07-11 21:42:12 +0000363 applet_long_options =
364 "extract\0" No_argument "i"
365 "list\0" No_argument "t"
366#if ENABLE_FEATURE_CPIO_O
367 "create\0" No_argument "o"
368 "format\0" Required_argument "H"
Denis Vlasenko83518d12009-03-20 22:17:13 +0000369#if ENABLE_FEATURE_CPIO_P
370 "pass-through\0" No_argument "p"
371#endif
Denis Vlasenko2b407b12008-07-11 21:42:12 +0000372#endif
Aaro Koskinen2735bc02015-10-16 17:24:46 +0200373 "owner\0" Required_argument "R"
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200374 "verbose\0" No_argument "v"
375 "quiet\0" No_argument "\xff"
376 "to-stdout\0" No_argument "\xfe"
Denis Vlasenko2b407b12008-07-11 21:42:12 +0000377 ;
378#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000379
Aaro Koskinen2735bc02015-10-16 17:24:46 +0200380 INIT_G();
Denys Vlasenko607f65d2010-01-09 20:23:03 +0100381 archive_handle = init_handle();
382 /* archive_handle->src_fd = STDIN_FILENO; - done by init_handle */
383 archive_handle->ah_flags = ARCHIVE_EXTRACT_NEWER;
384
Denis Vlasenko83518d12009-03-20 22:17:13 +0000385 /* As of now we do not enforce this: */
386 /* -i,-t,-o,-p are mutually exclusive */
387 /* -u,-d,-m make sense only with -i or -p */
Denis Vlasenko05af8322009-03-20 23:01:48 +0000388 /* -L makes sense only with -o or -p */
389
Denis Vlasenko83518d12009-03-20 22:17:13 +0000390#if !ENABLE_FEATURE_CPIO_O
Aaro Koskinen2735bc02015-10-16 17:24:46 +0200391 opt = getopt32(argv, OPTION_STR, &cpio_filename, &cpio_owner);
392#else
393 opt = getopt32(argv, OPTION_STR "oH:" IF_FEATURE_CPIO_P("p"),
394 &cpio_filename, &cpio_owner, &cpio_fmt);
395#endif
Denys Vlasenkoff0e8752010-05-10 04:16:43 +0200396 argv += optind;
Aaro Koskinen2735bc02015-10-16 17:24:46 +0200397 if (opt & OPT_OWNER) { /* -R */
398 parse_chown_usergroup_or_die(&G.owner_ugid, cpio_owner);
399 archive_handle->cpio__owner = G.owner_ugid;
400 }
401#if !ENABLE_FEATURE_CPIO_O
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100402 if (opt & OPT_FILE) { /* -F */
Denys Vlasenko02dd96f2010-01-09 20:25:42 +0100403 xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO);
Denys Vlasenko607f65d2010-01-09 20:23:03 +0100404 }
Denis Vlasenko83518d12009-03-20 22:17:13 +0000405#else
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100406 if ((opt & (OPT_FILE|OPT_CREATE)) == OPT_FILE) { /* -F without -o */
Denys Vlasenko607f65d2010-01-09 20:23:03 +0100407 xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO);
408 }
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100409 if (opt & OPT_PASSTHROUGH) {
Denis Vlasenko83518d12009-03-20 22:17:13 +0000410 pid_t pid;
411 struct fd_pair pp;
Glenn L McGrath10b78132004-02-25 09:30:06 +0000412
Denys Vlasenko40e5a302010-01-04 14:30:37 +0100413 if (argv[0] == NULL)
Denis Vlasenko83518d12009-03-20 22:17:13 +0000414 bb_show_usage();
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100415 if (opt & OPT_CREATE_LEADING_DIR)
Denys Vlasenko40e5a302010-01-04 14:30:37 +0100416 mkdir(argv[0], 0777);
Denis Vlasenko83518d12009-03-20 22:17:13 +0000417 /* Crude existence check:
Denys Vlasenko40e5a302010-01-04 14:30:37 +0100418 * close(xopen(argv[0], O_RDONLY | O_DIRECTORY));
Denis Vlasenko83518d12009-03-20 22:17:13 +0000419 * We can also xopen, fstat, IS_DIR, later fchdir.
420 * This would check for existence earlier and cleaner.
421 * As it stands now, if we fail xchdir later,
422 * child dies on EPIPE, unless it caught
423 * a diffrerent problem earlier.
424 * This is good enough for now.
425 */
426#if !BB_MMU
427 pp.rd = 3;
428 pp.wr = 4;
429 if (!re_execed) {
430 close(3);
431 close(4);
432 xpiped_pair(pp);
433 }
434#else
435 xpiped_pair(pp);
436#endif
Denys Vlasenko40e5a302010-01-04 14:30:37 +0100437 pid = fork_or_rexec(argv - optind);
Denis Vlasenko83518d12009-03-20 22:17:13 +0000438 if (pid == 0) { /* child */
439 close(pp.rd);
440 xmove_fd(pp.wr, STDOUT_FILENO);
441 goto dump;
442 }
443 /* parent */
Denys Vlasenkod2277e22011-11-22 17:19:26 +0100444 USE_FOR_NOMMU(argv[-optind][0] &= 0x7f); /* undo fork_or_rexec() damage */
Denys Vlasenko40e5a302010-01-04 14:30:37 +0100445 xchdir(*argv++);
Denis Vlasenko83518d12009-03-20 22:17:13 +0000446 close(pp.wr);
447 xmove_fd(pp.rd, STDIN_FILENO);
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100448 //opt &= ~OPT_PASSTHROUGH;
449 opt |= OPT_EXTRACT;
Denis Vlasenko83518d12009-03-20 22:17:13 +0000450 goto skip;
451 }
452 /* -o */
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100453 if (opt & OPT_CREATE) {
Denys Vlasenko93ac7d82010-01-09 19:56:15 +0100454 if (cpio_fmt[0] != 'n') /* we _require_ "-H newc" */
Denis Vlasenko261f2372008-04-05 00:07:46 +0000455 bb_show_usage();
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100456 if (opt & OPT_FILE) {
Denys Vlasenkoc05387d2010-10-18 02:38:27 +0200457 xmove_fd(xopen(cpio_filename, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
Denis Vlasenko261f2372008-04-05 00:07:46 +0000458 }
Denis Vlasenko83518d12009-03-20 22:17:13 +0000459 dump:
Denis Vlasenko261f2372008-04-05 00:07:46 +0000460 return cpio_o();
461 }
Denis Vlasenko83518d12009-03-20 22:17:13 +0000462 skip:
Denis Vlasenko261f2372008-04-05 00:07:46 +0000463#endif
Glenn L McGrath10b78132004-02-25 09:30:06 +0000464
465 /* One of either extract or test options must be given */
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100466 if ((opt & (OPT_TEST | OPT_EXTRACT)) == 0) {
Glenn L McGrath10b78132004-02-25 09:30:06 +0000467 bb_show_usage();
468 }
469
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100470 if (opt & OPT_TEST) {
Rob Landleyb7128c62005-09-11 01:05:30 +0000471 /* if both extract and test options are given, ignore extract option */
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100472 opt &= ~OPT_EXTRACT;
Glenn L McGrath10b78132004-02-25 09:30:06 +0000473 archive_handle->action_header = header_list;
474 }
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100475 if (opt & OPT_EXTRACT) {
Glenn L McGrath10b78132004-02-25 09:30:06 +0000476 archive_handle->action_data = data_extract_all;
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100477 if (opt & OPT_2STDOUT)
Denys Vlasenkod30b89c2009-06-26 01:55:45 +0200478 archive_handle->action_data = data_extract_to_stdout;
Glenn L McGrath10b78132004-02-25 09:30:06 +0000479 }
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100480 if (opt & OPT_UNCONDITIONAL) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200481 archive_handle->ah_flags |= ARCHIVE_UNLINK_OLD;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000482 archive_handle->ah_flags &= ~ARCHIVE_EXTRACT_NEWER;
Glenn L McGrath10b78132004-02-25 09:30:06 +0000483 }
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100484 if (opt & OPT_VERBOSE) {
Glenn L McGrath10b78132004-02-25 09:30:06 +0000485 if (archive_handle->action_header == header_list) {
486 archive_handle->action_header = header_verbose_list;
487 } else {
488 archive_handle->action_header = header_list;
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +0000489 }
490 }
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100491 if (opt & OPT_CREATE_LEADING_DIR) {
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000492 archive_handle->ah_flags |= ARCHIVE_CREATE_LEADING_DIRS;
Glenn L McGrath10b78132004-02-25 09:30:06 +0000493 }
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100494 if (opt & OPT_PRESERVE_MTIME) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200495 archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE;
Denis Vlasenkobbd55c92008-06-27 15:52:07 +0000496 }
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +0000497
Denis Vlasenko261f2372008-04-05 00:07:46 +0000498 while (*argv) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000499 archive_handle->filter = filter_accept_list;
Denys Vlasenko40e5a302010-01-04 14:30:37 +0100500 llist_add_to(&archive_handle->accept, *argv);
Denis Vlasenko261f2372008-04-05 00:07:46 +0000501 argv++;
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +0000502 }
503
Denis Vlasenkocf4dd072008-10-17 14:11:04 +0000504 /* see get_header_cpio */
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100505 archive_handle->cpio__blocks = (off_t)-1;
Denis Vlasenko261f2372008-04-05 00:07:46 +0000506 while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
507 continue;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000508
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100509 if (archive_handle->cpio__blocks != (off_t)-1
Denys Vlasenko1f937d62010-12-26 02:22:51 +0100510 && !(opt & OPT_QUIET)
Denys Vlasenko607f65d2010-01-09 20:23:03 +0100511 ) {
Matheus Izvekov4640ccc2010-04-26 13:53:57 +0200512 fprintf(stderr, "%"OFF_FMT"u blocks\n", archive_handle->cpio__blocks);
Denys Vlasenko607f65d2010-01-09 20:23:03 +0100513 }
Denis Vlasenkod83676e2008-10-17 14:03:56 +0000514
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000515 return EXIT_SUCCESS;
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +0000516}