Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini cpio implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 2001 by Glenn McGrath |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 8 | * |
| 9 | * Limitations: |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 10 | * Doesn't check CRC's |
| 11 | * Only supports new ASCII and CRC formats |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 12 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
Denys Vlasenko | d184a72 | 2011-09-22 12:45:14 +0200 | [diff] [blame] | 14 | #include "bb_archive.h" |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 15 | |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 16 | //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 Vlasenko | 36184a4 | 2013-11-14 09:54:24 +0100 | [diff] [blame] | 45 | //applet:IF_CPIO(APPLET(cpio, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 66620fa | 2013-11-14 09:53:52 +0100 | [diff] [blame] | 46 | //kbuild:lib-$(CONFIG_CPIO) += cpio.o |
| 47 | |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 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" |
| 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 Vlasenko | 184b266 | 2014-06-30 17:19:17 +0200 | [diff] [blame] | 68 | //usage: "\nOptions:" |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 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" |
| 76 | //usage: ) |
| 77 | |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 78 | /* GNU cpio 2.9 --help (abridged): |
| 79 | |
| 80 | Modes: |
| 81 | -t, --list List the archive |
| 82 | -i, --extract Extract files from an archive |
| 83 | -o, --create Create the archive |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 84 | -p, --pass-through Copy-pass mode |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 85 | |
| 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 |
| 103 | |
| 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 |
| 114 | |
| 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 |
| 118 | |
| 119 | Options valid only in --pass-through mode: |
| 120 | -l, --link Link files instead of copying them, when possible |
| 121 | |
| 122 | Options valid in --extract and --create modes: |
| 123 | --absolute-filenames Do not strip file system prefix components from |
| 124 | the file names |
| 125 | --no-absolute-filenames Create all files relative to the current dir |
| 126 | |
| 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 |
| 134 | |
| 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 |
| 141 | */ |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 142 | |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 143 | enum { |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 144 | OPT_EXTRACT = (1 << 0), |
| 145 | OPT_TEST = (1 << 1), |
| 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), |
| 152 | OPT_FILE = (1 << 8), |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 153 | OPTBIT_FILE = 8, |
| 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 ,) |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 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, |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | #define OPTION_STR "it0uvdmLF:" |
| 167 | |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 168 | #if ENABLE_FEATURE_CPIO_O |
| 169 | static off_t cpio_pad4(off_t size) |
| 170 | { |
| 171 | int i; |
| 172 | |
| 173 | i = (- size) & 3; |
| 174 | size += i; |
| 175 | while (--i >= 0) |
| 176 | bb_putchar('\0'); |
| 177 | return size; |
| 178 | } |
| 179 | |
| 180 | /* Return value will become exit code. |
| 181 | * It's ok to exit instead of return. */ |
Denys Vlasenko | a7bb3c1 | 2009-10-08 12:28:08 +0200 | [diff] [blame] | 182 | static NOINLINE int cpio_o(void) |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 183 | { |
Denis Vlasenko | c503dde | 2008-10-26 19:55:20 +0000 | [diff] [blame] | 184 | static const char trailer[] ALIGN1 = "TRAILER!!!"; |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 185 | struct name_s { |
| 186 | struct name_s *next; |
| 187 | char name[1]; |
| 188 | }; |
| 189 | struct inodes_s { |
| 190 | struct inodes_s *next; |
| 191 | struct name_s *names; |
| 192 | struct stat st; |
| 193 | }; |
| 194 | |
| 195 | struct inodes_s *links = NULL; |
| 196 | off_t bytes = 0; /* output bytes count */ |
| 197 | |
| 198 | while (1) { |
| 199 | const char *name; |
| 200 | char *line; |
| 201 | struct stat st; |
| 202 | |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 203 | line = (option_mask32 & OPT_NUL_TERMINATED) |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 204 | ? bb_get_chunk_from_file(stdin, NULL) |
| 205 | : xmalloc_fgetline(stdin); |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 206 | |
| 207 | if (line) { |
| 208 | /* Strip leading "./[./]..." from the filename */ |
| 209 | name = line; |
| 210 | while (name[0] == '.' && name[1] == '/') { |
| 211 | while (*++name == '/') |
| 212 | continue; |
| 213 | } |
| 214 | if (!*name) { /* line is empty */ |
| 215 | free(line); |
| 216 | continue; |
| 217 | } |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 218 | if ((option_mask32 & OPT_DEREF) |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 219 | ? stat(name, &st) |
| 220 | : lstat(name, &st) |
| 221 | ) { |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 222 | abort_cpio_o: |
| 223 | bb_simple_perror_msg_and_die(name); |
| 224 | } |
| 225 | |
| 226 | if (!(S_ISLNK(st.st_mode) || S_ISREG(st.st_mode))) |
| 227 | st.st_size = 0; /* paranoia */ |
| 228 | |
| 229 | /* Store hardlinks for later processing, dont output them */ |
| 230 | if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) { |
| 231 | struct name_s *n; |
| 232 | struct inodes_s *l; |
| 233 | |
| 234 | /* Do we have this hardlink remembered? */ |
| 235 | l = links; |
| 236 | while (1) { |
| 237 | if (l == NULL) { |
| 238 | /* Not found: add new item to "links" list */ |
| 239 | l = xzalloc(sizeof(*l)); |
| 240 | l->st = st; |
| 241 | l->next = links; |
| 242 | links = l; |
| 243 | break; |
| 244 | } |
| 245 | if (l->st.st_ino == st.st_ino) { |
| 246 | /* found */ |
| 247 | break; |
| 248 | } |
| 249 | l = l->next; |
| 250 | } |
| 251 | /* Add new name to "l->names" list */ |
| 252 | n = xmalloc(sizeof(*n) + strlen(name)); |
| 253 | strcpy(n->name, name); |
| 254 | n->next = l->names; |
| 255 | l->names = n; |
| 256 | |
| 257 | free(line); |
| 258 | continue; |
| 259 | } |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 260 | } else { /* line == NULL: EOF */ |
| 261 | next_link: |
| 262 | if (links) { |
| 263 | /* Output hardlink's data */ |
| 264 | st = links->st; |
| 265 | name = links->names->name; |
| 266 | links->names = links->names->next; |
| 267 | /* GNU cpio is reported to emit file data |
| 268 | * only for the last instance. Mimic that. */ |
| 269 | if (links->names == NULL) |
| 270 | links = links->next; |
| 271 | else |
| 272 | st.st_size = 0; |
| 273 | /* NB: we leak links->names and/or links, |
| 274 | * this is intended (we exit soon anyway) */ |
| 275 | } else { |
| 276 | /* If no (more) hardlinks to output, |
| 277 | * output "trailer" entry */ |
Denis Vlasenko | c503dde | 2008-10-26 19:55:20 +0000 | [diff] [blame] | 278 | name = trailer; |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 279 | /* st.st_size == 0 is a must, but for uniformity |
| 280 | * in the output, we zero out everything */ |
| 281 | memset(&st, 0, sizeof(st)); |
| 282 | /* st.st_nlink = 1; - GNU cpio does this */ |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | bytes += printf("070701" |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 287 | "%08X%08X%08X%08X%08X%08X%08X" |
| 288 | "%08X%08X%08X%08X" /* GNU cpio uses uppercase hex */ |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 289 | /* strlen+1: */ "%08X" |
| 290 | /* chksum: */ "00000000" /* (only for "070702" files) */ |
| 291 | /* name,NUL: */ "%s%c", |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 292 | (unsigned)(uint32_t) st.st_ino, |
| 293 | (unsigned)(uint32_t) st.st_mode, |
| 294 | (unsigned)(uint32_t) st.st_uid, |
| 295 | (unsigned)(uint32_t) st.st_gid, |
| 296 | (unsigned)(uint32_t) st.st_nlink, |
| 297 | (unsigned)(uint32_t) st.st_mtime, |
| 298 | (unsigned)(uint32_t) st.st_size, |
| 299 | (unsigned)(uint32_t) major(st.st_dev), |
| 300 | (unsigned)(uint32_t) minor(st.st_dev), |
| 301 | (unsigned)(uint32_t) major(st.st_rdev), |
| 302 | (unsigned)(uint32_t) minor(st.st_rdev), |
| 303 | (unsigned)(strlen(name) + 1), |
| 304 | name, '\0'); |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 305 | bytes = cpio_pad4(bytes); |
| 306 | |
| 307 | if (st.st_size) { |
| 308 | if (S_ISLNK(st.st_mode)) { |
| 309 | char *lpath = xmalloc_readlink_or_warn(name); |
| 310 | if (!lpath) |
| 311 | goto abort_cpio_o; |
| 312 | bytes += printf("%s", lpath); |
| 313 | free(lpath); |
| 314 | } else { /* S_ISREG */ |
| 315 | int fd = xopen(name, O_RDONLY); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 316 | fflush_all(); |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 317 | /* We must abort if file got shorter too! */ |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 318 | bb_copyfd_exact_size(fd, STDOUT_FILENO, st.st_size); |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 319 | bytes += st.st_size; |
| 320 | close(fd); |
| 321 | } |
| 322 | bytes = cpio_pad4(bytes); |
| 323 | } |
| 324 | |
| 325 | if (!line) { |
Denis Vlasenko | c503dde | 2008-10-26 19:55:20 +0000 | [diff] [blame] | 326 | if (name != trailer) |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 327 | goto next_link; |
| 328 | /* TODO: GNU cpio pads trailer to 512 bytes, do we want that? */ |
| 329 | return EXIT_SUCCESS; |
| 330 | } |
| 331 | |
| 332 | free(line); |
| 333 | } /* end of "while (1)" */ |
| 334 | } |
| 335 | #endif |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 336 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 337 | int cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 338 | int cpio_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 339 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 340 | archive_handle_t *archive_handle; |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 341 | char *cpio_filename; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 342 | IF_FEATURE_CPIO_O(const char *cpio_fmt = "";) |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 343 | unsigned opt; |
Denis Vlasenko | 2b407b1 | 2008-07-11 21:42:12 +0000 | [diff] [blame] | 344 | |
Denys Vlasenko | f3b92d3 | 2009-06-19 12:10:38 +0200 | [diff] [blame] | 345 | #if ENABLE_LONG_OPTS |
Denis Vlasenko | 2b407b1 | 2008-07-11 21:42:12 +0000 | [diff] [blame] | 346 | applet_long_options = |
| 347 | "extract\0" No_argument "i" |
| 348 | "list\0" No_argument "t" |
| 349 | #if ENABLE_FEATURE_CPIO_O |
| 350 | "create\0" No_argument "o" |
| 351 | "format\0" Required_argument "H" |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 352 | #if ENABLE_FEATURE_CPIO_P |
| 353 | "pass-through\0" No_argument "p" |
| 354 | #endif |
Denis Vlasenko | 2b407b1 | 2008-07-11 21:42:12 +0000 | [diff] [blame] | 355 | #endif |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 356 | "verbose\0" No_argument "v" |
| 357 | "quiet\0" No_argument "\xff" |
| 358 | "to-stdout\0" No_argument "\xfe" |
Denis Vlasenko | 2b407b1 | 2008-07-11 21:42:12 +0000 | [diff] [blame] | 359 | ; |
| 360 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 361 | |
Denys Vlasenko | 607f65d | 2010-01-09 20:23:03 +0100 | [diff] [blame] | 362 | archive_handle = init_handle(); |
| 363 | /* archive_handle->src_fd = STDIN_FILENO; - done by init_handle */ |
| 364 | archive_handle->ah_flags = ARCHIVE_EXTRACT_NEWER; |
| 365 | |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 366 | /* As of now we do not enforce this: */ |
| 367 | /* -i,-t,-o,-p are mutually exclusive */ |
| 368 | /* -u,-d,-m make sense only with -i or -p */ |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 369 | /* -L makes sense only with -o or -p */ |
| 370 | |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 371 | #if !ENABLE_FEATURE_CPIO_O |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 372 | opt = getopt32(argv, OPTION_STR, &cpio_filename); |
Denys Vlasenko | ff0e875 | 2010-05-10 04:16:43 +0200 | [diff] [blame] | 373 | argv += optind; |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 374 | if (opt & OPT_FILE) { /* -F */ |
Denys Vlasenko | 02dd96f | 2010-01-09 20:25:42 +0100 | [diff] [blame] | 375 | xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO); |
Denys Vlasenko | 607f65d | 2010-01-09 20:23:03 +0100 | [diff] [blame] | 376 | } |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 377 | #else |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 378 | opt = getopt32(argv, OPTION_STR "oH:" IF_FEATURE_CPIO_P("p"), &cpio_filename, &cpio_fmt); |
Denys Vlasenko | 40e5a30 | 2010-01-04 14:30:37 +0100 | [diff] [blame] | 379 | argv += optind; |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 380 | if ((opt & (OPT_FILE|OPT_CREATE)) == OPT_FILE) { /* -F without -o */ |
Denys Vlasenko | 607f65d | 2010-01-09 20:23:03 +0100 | [diff] [blame] | 381 | xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO); |
| 382 | } |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 383 | if (opt & OPT_PASSTHROUGH) { |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 384 | pid_t pid; |
| 385 | struct fd_pair pp; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 386 | |
Denys Vlasenko | 40e5a30 | 2010-01-04 14:30:37 +0100 | [diff] [blame] | 387 | if (argv[0] == NULL) |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 388 | bb_show_usage(); |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 389 | if (opt & OPT_CREATE_LEADING_DIR) |
Denys Vlasenko | 40e5a30 | 2010-01-04 14:30:37 +0100 | [diff] [blame] | 390 | mkdir(argv[0], 0777); |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 391 | /* Crude existence check: |
Denys Vlasenko | 40e5a30 | 2010-01-04 14:30:37 +0100 | [diff] [blame] | 392 | * close(xopen(argv[0], O_RDONLY | O_DIRECTORY)); |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 393 | * We can also xopen, fstat, IS_DIR, later fchdir. |
| 394 | * This would check for existence earlier and cleaner. |
| 395 | * As it stands now, if we fail xchdir later, |
| 396 | * child dies on EPIPE, unless it caught |
| 397 | * a diffrerent problem earlier. |
| 398 | * This is good enough for now. |
| 399 | */ |
| 400 | #if !BB_MMU |
| 401 | pp.rd = 3; |
| 402 | pp.wr = 4; |
| 403 | if (!re_execed) { |
| 404 | close(3); |
| 405 | close(4); |
| 406 | xpiped_pair(pp); |
| 407 | } |
| 408 | #else |
| 409 | xpiped_pair(pp); |
| 410 | #endif |
Denys Vlasenko | 40e5a30 | 2010-01-04 14:30:37 +0100 | [diff] [blame] | 411 | pid = fork_or_rexec(argv - optind); |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 412 | if (pid == 0) { /* child */ |
| 413 | close(pp.rd); |
| 414 | xmove_fd(pp.wr, STDOUT_FILENO); |
| 415 | goto dump; |
| 416 | } |
| 417 | /* parent */ |
Denys Vlasenko | d2277e2 | 2011-11-22 17:19:26 +0100 | [diff] [blame] | 418 | USE_FOR_NOMMU(argv[-optind][0] &= 0x7f); /* undo fork_or_rexec() damage */ |
Denys Vlasenko | 40e5a30 | 2010-01-04 14:30:37 +0100 | [diff] [blame] | 419 | xchdir(*argv++); |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 420 | close(pp.wr); |
| 421 | xmove_fd(pp.rd, STDIN_FILENO); |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 422 | //opt &= ~OPT_PASSTHROUGH; |
| 423 | opt |= OPT_EXTRACT; |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 424 | goto skip; |
| 425 | } |
| 426 | /* -o */ |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 427 | if (opt & OPT_CREATE) { |
Denys Vlasenko | 93ac7d8 | 2010-01-09 19:56:15 +0100 | [diff] [blame] | 428 | if (cpio_fmt[0] != 'n') /* we _require_ "-H newc" */ |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 429 | bb_show_usage(); |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 430 | if (opt & OPT_FILE) { |
Denys Vlasenko | c05387d | 2010-10-18 02:38:27 +0200 | [diff] [blame] | 431 | xmove_fd(xopen(cpio_filename, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO); |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 432 | } |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 433 | dump: |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 434 | return cpio_o(); |
| 435 | } |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 436 | skip: |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 437 | #endif |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 438 | |
| 439 | /* One of either extract or test options must be given */ |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 440 | if ((opt & (OPT_TEST | OPT_EXTRACT)) == 0) { |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 441 | bb_show_usage(); |
| 442 | } |
| 443 | |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 444 | if (opt & OPT_TEST) { |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 445 | /* if both extract and test options are given, ignore extract option */ |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 446 | opt &= ~OPT_EXTRACT; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 447 | archive_handle->action_header = header_list; |
| 448 | } |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 449 | if (opt & OPT_EXTRACT) { |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 450 | archive_handle->action_data = data_extract_all; |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 451 | if (opt & OPT_2STDOUT) |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 452 | archive_handle->action_data = data_extract_to_stdout; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 453 | } |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 454 | if (opt & OPT_UNCONDITIONAL) { |
Denys Vlasenko | d57d626 | 2009-09-17 02:43:14 +0200 | [diff] [blame] | 455 | archive_handle->ah_flags |= ARCHIVE_UNLINK_OLD; |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 456 | archive_handle->ah_flags &= ~ARCHIVE_EXTRACT_NEWER; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 457 | } |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 458 | if (opt & OPT_VERBOSE) { |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 459 | if (archive_handle->action_header == header_list) { |
| 460 | archive_handle->action_header = header_verbose_list; |
| 461 | } else { |
| 462 | archive_handle->action_header = header_list; |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 465 | if (opt & OPT_CREATE_LEADING_DIR) { |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 466 | archive_handle->ah_flags |= ARCHIVE_CREATE_LEADING_DIRS; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 467 | } |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 468 | if (opt & OPT_PRESERVE_MTIME) { |
Denys Vlasenko | d57d626 | 2009-09-17 02:43:14 +0200 | [diff] [blame] | 469 | archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE; |
Denis Vlasenko | bbd55c9 | 2008-06-27 15:52:07 +0000 | [diff] [blame] | 470 | } |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 471 | |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 472 | while (*argv) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 473 | archive_handle->filter = filter_accept_list; |
Denys Vlasenko | 40e5a30 | 2010-01-04 14:30:37 +0100 | [diff] [blame] | 474 | llist_add_to(&archive_handle->accept, *argv); |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 475 | argv++; |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Denis Vlasenko | cf4dd07 | 2008-10-17 14:11:04 +0000 | [diff] [blame] | 478 | /* see get_header_cpio */ |
Denys Vlasenko | aa4977d | 2010-01-06 10:53:17 +0100 | [diff] [blame] | 479 | archive_handle->cpio__blocks = (off_t)-1; |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 480 | while (get_header_cpio(archive_handle) == EXIT_SUCCESS) |
| 481 | continue; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 482 | |
Denys Vlasenko | aa4977d | 2010-01-06 10:53:17 +0100 | [diff] [blame] | 483 | if (archive_handle->cpio__blocks != (off_t)-1 |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 484 | && !(opt & OPT_QUIET) |
Denys Vlasenko | 607f65d | 2010-01-09 20:23:03 +0100 | [diff] [blame] | 485 | ) { |
Matheus Izvekov | 4640ccc | 2010-04-26 13:53:57 +0200 | [diff] [blame] | 486 | fprintf(stderr, "%"OFF_FMT"u blocks\n", archive_handle->cpio__blocks); |
Denys Vlasenko | 607f65d | 2010-01-09 20:23:03 +0100 | [diff] [blame] | 487 | } |
Denis Vlasenko | d83676e | 2008-10-17 14:03:56 +0000 | [diff] [blame] | 488 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 489 | return EXIT_SUCCESS; |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 490 | } |