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 | */ |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 13 | //config:config CPIO |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 14 | //config: bool "cpio (14 kb)" |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 15 | //config: default y |
| 16 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 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. |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 20 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 21 | //config: This implementation of cpio can extract cpio archives created in the |
| 22 | //config: "newc" or "crc" format. |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 23 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 24 | //config: Unless you have a specific application which requires cpio, you |
| 25 | //config: should probably say N here. |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 26 | //config: |
| 27 | //config:config FEATURE_CPIO_O |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 28 | //config: bool "Support archive creation" |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 29 | //config: default y |
| 30 | //config: depends on CPIO |
| 31 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 32 | //config: This implementation of cpio can create cpio archives in the "newc" |
| 33 | //config: format only. |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 34 | //config: |
| 35 | //config:config FEATURE_CPIO_P |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 36 | //config: bool "Support passthrough mode" |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 37 | //config: default y |
| 38 | //config: depends on FEATURE_CPIO_O |
| 39 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 40 | //config: Passthrough mode. Rarely used. |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 41 | |
Denys Vlasenko | 36184a4 | 2013-11-14 09:54:24 +0100 | [diff] [blame] | 42 | //applet:IF_CPIO(APPLET(cpio, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 0c4dbd4 | 2017-09-18 16:28:43 +0200 | [diff] [blame] | 43 | |
Denys Vlasenko | 66620fa | 2013-11-14 09:53:52 +0100 | [diff] [blame] | 44 | //kbuild:lib-$(CONFIG_CPIO) += cpio.o |
| 45 | |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 46 | //usage:#define cpio_trivial_usage |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 47 | //usage: "[-dmvu] [-F FILE] [-R USER[:GRP]]" IF_FEATURE_CPIO_O(" [-H newc]") |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 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" |
Denys Vlasenko | d2f4241 | 2016-07-08 12:52:24 +0200 | [diff] [blame] | 51 | //usage: "Extract (-i) or list (-t) files from a cpio archive" |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 52 | //usage: IF_FEATURE_CPIO_O(", or" |
Denys Vlasenko | d2f4241 | 2016-07-08 12:52:24 +0200 | [diff] [blame] | 53 | //usage: "\ntake file list from stdin and create an archive (-o)" |
| 54 | //usage: IF_FEATURE_CPIO_P(" or copy files (-p)") |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 55 | //usage: ) |
| 56 | //usage: "\n" |
| 57 | //usage: "\nMain operation mode:" |
| 58 | //usage: "\n -t List" |
| 59 | //usage: "\n -i Extract EXTR_FILEs (or all)" |
| 60 | //usage: IF_FEATURE_CPIO_O( |
| 61 | //usage: "\n -o Create (requires -H newc)" |
| 62 | //usage: ) |
| 63 | //usage: IF_FEATURE_CPIO_P( |
| 64 | //usage: "\n -p DIR Copy files to DIR" |
| 65 | //usage: ) |
Denys Vlasenko | 184b266 | 2014-06-30 17:19:17 +0200 | [diff] [blame] | 66 | //usage: "\nOptions:" |
Denys Vlasenko | 4d0e2d5 | 2018-07-06 20:49:08 +0200 | [diff] [blame] | 67 | //usage: IF_FEATURE_CPIO_O( |
| 68 | //usage: "\n -H newc Archive format" |
| 69 | //usage: ) |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 70 | //usage: "\n -d Make leading directories" |
| 71 | //usage: "\n -m Preserve mtime" |
| 72 | //usage: "\n -v Verbose" |
| 73 | //usage: "\n -u Overwrite" |
| 74 | //usage: "\n -F FILE Input (-t,-i,-p) or output (-o) file" |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 75 | //usage: "\n -R USER[:GRP] Set owner of created files" |
Denys Vlasenko | 4d0e2d5 | 2018-07-06 20:49:08 +0200 | [diff] [blame] | 76 | //usage: "\n -L Dereference symlinks" |
| 77 | //usage: "\n -0 Input is separated by NULs" |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 78 | |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 79 | /* 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 Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 85 | -p, --pass-through Copy-pass mode |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 86 | |
| 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 Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 134 | -R, --owner=[USER][:.][GRP] Set owner of created files |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 135 | |
| 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 Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 143 | |
Denys Vlasenko | 0c4dbd4 | 2017-09-18 16:28:43 +0200 | [diff] [blame] | 144 | #include "libbb.h" |
| 145 | #include "common_bufsiz.h" |
| 146 | #include "bb_archive.h" |
| 147 | |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 148 | enum { |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 149 | OPT_EXTRACT = (1 << 0), |
| 150 | OPT_TEST = (1 << 1), |
| 151 | OPT_NUL_TERMINATED = (1 << 2), |
| 152 | OPT_UNCONDITIONAL = (1 << 3), |
| 153 | OPT_VERBOSE = (1 << 4), |
| 154 | OPT_CREATE_LEADING_DIR = (1 << 5), |
| 155 | OPT_PRESERVE_MTIME = (1 << 6), |
| 156 | OPT_DEREF = (1 << 7), |
| 157 | OPT_FILE = (1 << 8), |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 158 | OPT_OWNER = (1 << 9), |
| 159 | OPTBIT_OWNER = 9, |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 160 | IF_FEATURE_CPIO_O(OPTBIT_CREATE ,) |
| 161 | IF_FEATURE_CPIO_O(OPTBIT_FORMAT ,) |
| 162 | IF_FEATURE_CPIO_P(OPTBIT_PASSTHROUGH,) |
| 163 | IF_LONG_OPTS( OPTBIT_QUIET ,) |
| 164 | IF_LONG_OPTS( OPTBIT_2STDOUT ,) |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 165 | OPT_CREATE = IF_FEATURE_CPIO_O((1 << OPTBIT_CREATE )) + 0, |
| 166 | OPT_FORMAT = IF_FEATURE_CPIO_O((1 << OPTBIT_FORMAT )) + 0, |
| 167 | OPT_PASSTHROUGH = IF_FEATURE_CPIO_P((1 << OPTBIT_PASSTHROUGH)) + 0, |
| 168 | OPT_QUIET = IF_LONG_OPTS( (1 << OPTBIT_QUIET )) + 0, |
| 169 | OPT_2STDOUT = IF_LONG_OPTS( (1 << OPTBIT_2STDOUT )) + 0, |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 172 | #define OPTION_STR "it0uvdmLF:R:" |
| 173 | |
| 174 | struct globals { |
| 175 | struct bb_uidgid_t owner_ugid; |
| 176 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 177 | #define G (*(struct globals*)bb_common_bufsiz1) |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 178 | void BUG_cpio_globals_too_big(void); |
| 179 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 180 | setup_common_bufsiz(); \ |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 181 | G.owner_ugid.uid = -1L; \ |
| 182 | G.owner_ugid.gid = -1L; \ |
| 183 | } while (0) |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 184 | |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 185 | #if ENABLE_FEATURE_CPIO_O |
| 186 | static off_t cpio_pad4(off_t size) |
| 187 | { |
| 188 | int i; |
| 189 | |
| 190 | i = (- size) & 3; |
| 191 | size += i; |
| 192 | while (--i >= 0) |
| 193 | bb_putchar('\0'); |
| 194 | return size; |
| 195 | } |
| 196 | |
| 197 | /* Return value will become exit code. |
| 198 | * It's ok to exit instead of return. */ |
Denys Vlasenko | a7bb3c1 | 2009-10-08 12:28:08 +0200 | [diff] [blame] | 199 | static NOINLINE int cpio_o(void) |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 200 | { |
| 201 | struct name_s { |
| 202 | struct name_s *next; |
| 203 | char name[1]; |
| 204 | }; |
| 205 | struct inodes_s { |
| 206 | struct inodes_s *next; |
| 207 | struct name_s *names; |
| 208 | struct stat st; |
| 209 | }; |
| 210 | |
| 211 | struct inodes_s *links = NULL; |
| 212 | off_t bytes = 0; /* output bytes count */ |
| 213 | |
| 214 | while (1) { |
| 215 | const char *name; |
| 216 | char *line; |
| 217 | struct stat st; |
| 218 | |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 219 | line = (option_mask32 & OPT_NUL_TERMINATED) |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 220 | ? bb_get_chunk_from_file(stdin, NULL) |
| 221 | : xmalloc_fgetline(stdin); |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 222 | |
| 223 | if (line) { |
| 224 | /* Strip leading "./[./]..." from the filename */ |
| 225 | name = line; |
| 226 | while (name[0] == '.' && name[1] == '/') { |
| 227 | while (*++name == '/') |
| 228 | continue; |
| 229 | } |
| 230 | if (!*name) { /* line is empty */ |
| 231 | free(line); |
| 232 | continue; |
| 233 | } |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 234 | if ((option_mask32 & OPT_DEREF) |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 235 | ? stat(name, &st) |
| 236 | : lstat(name, &st) |
| 237 | ) { |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 238 | abort_cpio_o: |
| 239 | bb_simple_perror_msg_and_die(name); |
| 240 | } |
| 241 | |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 242 | if (G.owner_ugid.uid != (uid_t)-1L) |
| 243 | st.st_uid = G.owner_ugid.uid; |
| 244 | if (G.owner_ugid.gid != (gid_t)-1L) |
| 245 | st.st_gid = G.owner_ugid.gid; |
| 246 | |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 247 | if (!(S_ISLNK(st.st_mode) || S_ISREG(st.st_mode))) |
| 248 | st.st_size = 0; /* paranoia */ |
| 249 | |
| 250 | /* Store hardlinks for later processing, dont output them */ |
| 251 | if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) { |
| 252 | struct name_s *n; |
| 253 | struct inodes_s *l; |
| 254 | |
| 255 | /* Do we have this hardlink remembered? */ |
| 256 | l = links; |
| 257 | while (1) { |
| 258 | if (l == NULL) { |
| 259 | /* Not found: add new item to "links" list */ |
| 260 | l = xzalloc(sizeof(*l)); |
| 261 | l->st = st; |
| 262 | l->next = links; |
| 263 | links = l; |
| 264 | break; |
| 265 | } |
| 266 | if (l->st.st_ino == st.st_ino) { |
| 267 | /* found */ |
| 268 | break; |
| 269 | } |
| 270 | l = l->next; |
| 271 | } |
| 272 | /* Add new name to "l->names" list */ |
| 273 | n = xmalloc(sizeof(*n) + strlen(name)); |
| 274 | strcpy(n->name, name); |
| 275 | n->next = l->names; |
| 276 | l->names = n; |
| 277 | |
| 278 | free(line); |
| 279 | continue; |
| 280 | } |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 281 | } else { /* line == NULL: EOF */ |
| 282 | next_link: |
| 283 | if (links) { |
| 284 | /* Output hardlink's data */ |
| 285 | st = links->st; |
| 286 | name = links->names->name; |
| 287 | links->names = links->names->next; |
| 288 | /* GNU cpio is reported to emit file data |
| 289 | * only for the last instance. Mimic that. */ |
| 290 | if (links->names == NULL) |
| 291 | links = links->next; |
| 292 | else |
| 293 | st.st_size = 0; |
| 294 | /* NB: we leak links->names and/or links, |
| 295 | * this is intended (we exit soon anyway) */ |
| 296 | } else { |
| 297 | /* If no (more) hardlinks to output, |
| 298 | * output "trailer" entry */ |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 299 | name = cpio_TRAILER; |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 300 | /* st.st_size == 0 is a must, but for uniformity |
| 301 | * in the output, we zero out everything */ |
| 302 | memset(&st, 0, sizeof(st)); |
| 303 | /* st.st_nlink = 1; - GNU cpio does this */ |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | bytes += printf("070701" |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 308 | "%08X%08X%08X%08X%08X%08X%08X" |
| 309 | "%08X%08X%08X%08X" /* GNU cpio uses uppercase hex */ |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 310 | /* strlen+1: */ "%08X" |
| 311 | /* chksum: */ "00000000" /* (only for "070702" files) */ |
| 312 | /* name,NUL: */ "%s%c", |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 313 | (unsigned)(uint32_t) st.st_ino, |
| 314 | (unsigned)(uint32_t) st.st_mode, |
| 315 | (unsigned)(uint32_t) st.st_uid, |
| 316 | (unsigned)(uint32_t) st.st_gid, |
| 317 | (unsigned)(uint32_t) st.st_nlink, |
| 318 | (unsigned)(uint32_t) st.st_mtime, |
| 319 | (unsigned)(uint32_t) st.st_size, |
| 320 | (unsigned)(uint32_t) major(st.st_dev), |
| 321 | (unsigned)(uint32_t) minor(st.st_dev), |
| 322 | (unsigned)(uint32_t) major(st.st_rdev), |
| 323 | (unsigned)(uint32_t) minor(st.st_rdev), |
| 324 | (unsigned)(strlen(name) + 1), |
| 325 | name, '\0'); |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 326 | bytes = cpio_pad4(bytes); |
| 327 | |
| 328 | if (st.st_size) { |
| 329 | if (S_ISLNK(st.st_mode)) { |
| 330 | char *lpath = xmalloc_readlink_or_warn(name); |
| 331 | if (!lpath) |
| 332 | goto abort_cpio_o; |
| 333 | bytes += printf("%s", lpath); |
| 334 | free(lpath); |
| 335 | } else { /* S_ISREG */ |
| 336 | int fd = xopen(name, O_RDONLY); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 337 | fflush_all(); |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 338 | /* We must abort if file got shorter too! */ |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 339 | bb_copyfd_exact_size(fd, STDOUT_FILENO, st.st_size); |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 340 | bytes += st.st_size; |
| 341 | close(fd); |
| 342 | } |
| 343 | bytes = cpio_pad4(bytes); |
| 344 | } |
| 345 | |
| 346 | if (!line) { |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 347 | if (name != cpio_TRAILER) |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 348 | goto next_link; |
| 349 | /* TODO: GNU cpio pads trailer to 512 bytes, do we want that? */ |
| 350 | return EXIT_SUCCESS; |
| 351 | } |
| 352 | |
| 353 | free(line); |
| 354 | } /* end of "while (1)" */ |
| 355 | } |
| 356 | #endif |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 357 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 358 | int cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 359 | int cpio_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 360 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 361 | archive_handle_t *archive_handle; |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 362 | char *cpio_filename; |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 363 | char *cpio_owner; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 364 | IF_FEATURE_CPIO_O(const char *cpio_fmt = "";) |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 365 | unsigned opt; |
Denys Vlasenko | f3b92d3 | 2009-06-19 12:10:38 +0200 | [diff] [blame] | 366 | #if ENABLE_LONG_OPTS |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 367 | const char *long_opts = |
Denis Vlasenko | 2b407b1 | 2008-07-11 21:42:12 +0000 | [diff] [blame] | 368 | "extract\0" No_argument "i" |
| 369 | "list\0" No_argument "t" |
| 370 | #if ENABLE_FEATURE_CPIO_O |
| 371 | "create\0" No_argument "o" |
| 372 | "format\0" Required_argument "H" |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 373 | #if ENABLE_FEATURE_CPIO_P |
| 374 | "pass-through\0" No_argument "p" |
| 375 | #endif |
Denis Vlasenko | 2b407b1 | 2008-07-11 21:42:12 +0000 | [diff] [blame] | 376 | #endif |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 377 | "owner\0" Required_argument "R" |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 378 | "verbose\0" No_argument "v" |
Denys Vlasenko | 9be4d4f | 2018-07-06 20:50:30 +0200 | [diff] [blame] | 379 | "null\0" No_argument "0" |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 380 | "quiet\0" No_argument "\xff" |
| 381 | "to-stdout\0" No_argument "\xfe" |
Denis Vlasenko | 2b407b1 | 2008-07-11 21:42:12 +0000 | [diff] [blame] | 382 | ; |
| 383 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 384 | |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 385 | INIT_G(); |
Denys Vlasenko | 607f65d | 2010-01-09 20:23:03 +0100 | [diff] [blame] | 386 | archive_handle = init_handle(); |
| 387 | /* archive_handle->src_fd = STDIN_FILENO; - done by init_handle */ |
| 388 | archive_handle->ah_flags = ARCHIVE_EXTRACT_NEWER; |
| 389 | |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 390 | /* As of now we do not enforce this: */ |
| 391 | /* -i,-t,-o,-p are mutually exclusive */ |
| 392 | /* -u,-d,-m make sense only with -i or -p */ |
Denis Vlasenko | 05af832 | 2009-03-20 23:01:48 +0000 | [diff] [blame] | 393 | /* -L makes sense only with -o or -p */ |
| 394 | |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 395 | #if !ENABLE_FEATURE_CPIO_O |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 396 | opt = getopt32long(argv, OPTION_STR, long_opts, &cpio_filename, &cpio_owner); |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 397 | #else |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 398 | opt = getopt32long(argv, OPTION_STR "oH:" IF_FEATURE_CPIO_P("p"), long_opts, |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 399 | &cpio_filename, &cpio_owner, &cpio_fmt); |
| 400 | #endif |
Denys Vlasenko | ff0e875 | 2010-05-10 04:16:43 +0200 | [diff] [blame] | 401 | argv += optind; |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 402 | if (opt & OPT_OWNER) { /* -R */ |
| 403 | parse_chown_usergroup_or_die(&G.owner_ugid, cpio_owner); |
| 404 | archive_handle->cpio__owner = G.owner_ugid; |
| 405 | } |
| 406 | #if !ENABLE_FEATURE_CPIO_O |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 407 | if (opt & OPT_FILE) { /* -F */ |
Denys Vlasenko | 02dd96f | 2010-01-09 20:25:42 +0100 | [diff] [blame] | 408 | xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO); |
Denys Vlasenko | 607f65d | 2010-01-09 20:23:03 +0100 | [diff] [blame] | 409 | } |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 410 | #else |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 411 | if ((opt & (OPT_FILE|OPT_CREATE)) == OPT_FILE) { /* -F without -o */ |
Denys Vlasenko | 607f65d | 2010-01-09 20:23:03 +0100 | [diff] [blame] | 412 | xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO); |
| 413 | } |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 414 | if (opt & OPT_PASSTHROUGH) { |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 415 | pid_t pid; |
| 416 | struct fd_pair pp; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 417 | |
Denys Vlasenko | 40e5a30 | 2010-01-04 14:30:37 +0100 | [diff] [blame] | 418 | if (argv[0] == NULL) |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 419 | bb_show_usage(); |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 420 | if (opt & OPT_CREATE_LEADING_DIR) |
Denys Vlasenko | 40e5a30 | 2010-01-04 14:30:37 +0100 | [diff] [blame] | 421 | mkdir(argv[0], 0777); |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 422 | /* Crude existence check: |
Denys Vlasenko | 40e5a30 | 2010-01-04 14:30:37 +0100 | [diff] [blame] | 423 | * close(xopen(argv[0], O_RDONLY | O_DIRECTORY)); |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 424 | * We can also xopen, fstat, IS_DIR, later fchdir. |
| 425 | * This would check for existence earlier and cleaner. |
| 426 | * As it stands now, if we fail xchdir later, |
| 427 | * child dies on EPIPE, unless it caught |
| 428 | * a diffrerent problem earlier. |
| 429 | * This is good enough for now. |
| 430 | */ |
| 431 | #if !BB_MMU |
| 432 | pp.rd = 3; |
| 433 | pp.wr = 4; |
| 434 | if (!re_execed) { |
| 435 | close(3); |
| 436 | close(4); |
| 437 | xpiped_pair(pp); |
| 438 | } |
| 439 | #else |
| 440 | xpiped_pair(pp); |
| 441 | #endif |
Denys Vlasenko | 40e5a30 | 2010-01-04 14:30:37 +0100 | [diff] [blame] | 442 | pid = fork_or_rexec(argv - optind); |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 443 | if (pid == 0) { /* child */ |
| 444 | close(pp.rd); |
| 445 | xmove_fd(pp.wr, STDOUT_FILENO); |
| 446 | goto dump; |
| 447 | } |
| 448 | /* parent */ |
Denys Vlasenko | d2277e2 | 2011-11-22 17:19:26 +0100 | [diff] [blame] | 449 | USE_FOR_NOMMU(argv[-optind][0] &= 0x7f); /* undo fork_or_rexec() damage */ |
Denys Vlasenko | 40e5a30 | 2010-01-04 14:30:37 +0100 | [diff] [blame] | 450 | xchdir(*argv++); |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 451 | close(pp.wr); |
| 452 | xmove_fd(pp.rd, STDIN_FILENO); |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 453 | //opt &= ~OPT_PASSTHROUGH; |
| 454 | opt |= OPT_EXTRACT; |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 455 | goto skip; |
| 456 | } |
| 457 | /* -o */ |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 458 | if (opt & OPT_CREATE) { |
Denys Vlasenko | 93ac7d8 | 2010-01-09 19:56:15 +0100 | [diff] [blame] | 459 | if (cpio_fmt[0] != 'n') /* we _require_ "-H newc" */ |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 460 | bb_show_usage(); |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 461 | if (opt & OPT_FILE) { |
Denys Vlasenko | c05387d | 2010-10-18 02:38:27 +0200 | [diff] [blame] | 462 | 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] | 463 | } |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 464 | dump: |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 465 | return cpio_o(); |
| 466 | } |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 467 | skip: |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 468 | #endif |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 469 | |
| 470 | /* One of either extract or test options must be given */ |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 471 | if ((opt & (OPT_TEST | OPT_EXTRACT)) == 0) { |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 472 | bb_show_usage(); |
| 473 | } |
| 474 | |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 475 | if (opt & OPT_TEST) { |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 476 | /* if both extract and test options are given, ignore extract option */ |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 477 | opt &= ~OPT_EXTRACT; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 478 | archive_handle->action_header = header_list; |
| 479 | } |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 480 | if (opt & OPT_EXTRACT) { |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 481 | archive_handle->action_data = data_extract_all; |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 482 | if (opt & OPT_2STDOUT) |
Denys Vlasenko | d30b89c | 2009-06-26 01:55:45 +0200 | [diff] [blame] | 483 | archive_handle->action_data = data_extract_to_stdout; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 484 | } |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 485 | if (opt & OPT_UNCONDITIONAL) { |
Denys Vlasenko | d57d626 | 2009-09-17 02:43:14 +0200 | [diff] [blame] | 486 | archive_handle->ah_flags |= ARCHIVE_UNLINK_OLD; |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 487 | archive_handle->ah_flags &= ~ARCHIVE_EXTRACT_NEWER; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 488 | } |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 489 | if (opt & OPT_VERBOSE) { |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 490 | if (archive_handle->action_header == header_list) { |
| 491 | archive_handle->action_header = header_verbose_list; |
| 492 | } else { |
| 493 | archive_handle->action_header = header_list; |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 496 | if (opt & OPT_CREATE_LEADING_DIR) { |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 497 | archive_handle->ah_flags |= ARCHIVE_CREATE_LEADING_DIRS; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 498 | } |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 499 | if (opt & OPT_PRESERVE_MTIME) { |
Denys Vlasenko | d57d626 | 2009-09-17 02:43:14 +0200 | [diff] [blame] | 500 | archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE; |
Denis Vlasenko | bbd55c9 | 2008-06-27 15:52:07 +0000 | [diff] [blame] | 501 | } |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 502 | |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 503 | while (*argv) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 504 | archive_handle->filter = filter_accept_list; |
Denys Vlasenko | 40e5a30 | 2010-01-04 14:30:37 +0100 | [diff] [blame] | 505 | llist_add_to(&archive_handle->accept, *argv); |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 506 | argv++; |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Denis Vlasenko | cf4dd07 | 2008-10-17 14:11:04 +0000 | [diff] [blame] | 509 | /* see get_header_cpio */ |
Denys Vlasenko | aa4977d | 2010-01-06 10:53:17 +0100 | [diff] [blame] | 510 | archive_handle->cpio__blocks = (off_t)-1; |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 511 | while (get_header_cpio(archive_handle) == EXIT_SUCCESS) |
| 512 | continue; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 513 | |
Harald van Dijk | 8c24af9 | 2018-05-22 17:34:31 +0200 | [diff] [blame] | 514 | create_links_from_list(archive_handle->link_placeholders); |
Natanael Copa | d950322 | 2018-03-30 20:18:12 +0200 | [diff] [blame] | 515 | |
Denys Vlasenko | aa4977d | 2010-01-06 10:53:17 +0100 | [diff] [blame] | 516 | if (archive_handle->cpio__blocks != (off_t)-1 |
Denys Vlasenko | 1f937d6 | 2010-12-26 02:22:51 +0100 | [diff] [blame] | 517 | && !(opt & OPT_QUIET) |
Denys Vlasenko | 607f65d | 2010-01-09 20:23:03 +0100 | [diff] [blame] | 518 | ) { |
Matheus Izvekov | 4640ccc | 2010-04-26 13:53:57 +0200 | [diff] [blame] | 519 | fprintf(stderr, "%"OFF_FMT"u blocks\n", archive_handle->cpio__blocks); |
Denys Vlasenko | 607f65d | 2010-01-09 20:23:03 +0100 | [diff] [blame] | 520 | } |
Denis Vlasenko | d83676e | 2008-10-17 14:03:56 +0000 | [diff] [blame] | 521 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 522 | return EXIT_SUCCESS; |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 523 | } |