diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-12-26 02:22:51 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-12-26 02:22:51 +0100 |
commit | 1f937d64686d9a71c9325f2e9b23eba235a9255e (patch) | |
tree | 3e29f011ed2cb42a3da8ba751b392cb961779a82 | |
parent | e4e911e7124fca9116ca14c31f125b687d0ae57e (diff) | |
download | busybox-w32-1f937d64686d9a71c9325f2e9b23eba235a9255e.tar.gz busybox-w32-1f937d64686d9a71c9325f2e9b23eba235a9255e.tar.bz2 busybox-w32-1f937d64686d9a71c9325f2e9b23eba235a9255e.zip |
cpio: allow cpio -i to take params - names of files to extract
Also, improve help text
function old new delta
packed_usage 28028 28035 +7
cpio_main 542 532 -10
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 7/-10) Total: -3 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | archival/cpio.c | 105 | ||||
-rw-r--r-- | include/usage.src.h | 29 |
2 files changed, 66 insertions, 68 deletions
diff --git a/archival/cpio.c b/archival/cpio.c index 6ab268821..c746a71fa 100644 --- a/archival/cpio.c +++ b/archival/cpio.c | |||
@@ -14,13 +14,43 @@ | |||
14 | #include "libbb.h" | 14 | #include "libbb.h" |
15 | #include "archive.h" | 15 | #include "archive.h" |
16 | 16 | ||
17 | //usage:#define cpio_trivial_usage | ||
18 | //usage: "[-dmvu] [-F FILE]" IF_FEATURE_CPIO_O(" [-H newc]") | ||
19 | //usage: " [-ti"IF_FEATURE_CPIO_O("o")"]" IF_FEATURE_CPIO_P(" [-p DIR]") | ||
20 | //usage: " [EXTR_FILE]..." | ||
21 | //usage:#define cpio_full_usage "\n\n" | ||
22 | //usage: "Extract or list files from a cpio archive" | ||
23 | //usage: IF_FEATURE_CPIO_O(", or" | ||
24 | //usage: "\ncreate an archive" IF_FEATURE_CPIO_P(" (-o) or copy files (-p)") | ||
25 | //usage: " using file list on stdin" | ||
26 | //usage: ) | ||
27 | //usage: "\n" | ||
28 | //usage: "\nMain operation mode:" | ||
29 | //usage: "\n -t List" | ||
30 | //usage: "\n -i Extract EXTR_FILEs (or all)" | ||
31 | //usage: IF_FEATURE_CPIO_O( | ||
32 | //usage: "\n -o Create (requires -H newc)" | ||
33 | //usage: ) | ||
34 | //usage: IF_FEATURE_CPIO_P( | ||
35 | //usage: "\n -p DIR Copy files to DIR" | ||
36 | //usage: ) | ||
37 | //usage: "\nOptions:" | ||
38 | //usage: "\n -d Make leading directories" | ||
39 | //usage: "\n -m Preserve mtime" | ||
40 | //usage: "\n -v Verbose" | ||
41 | //usage: "\n -u Overwrite" | ||
42 | //usage: "\n -F FILE Input (-t,-i,-p) or output (-o) file" | ||
43 | //usage: IF_FEATURE_CPIO_O( | ||
44 | //usage: "\n -H newc Archive format" | ||
45 | //usage: ) | ||
46 | |||
17 | /* GNU cpio 2.9 --help (abridged): | 47 | /* GNU cpio 2.9 --help (abridged): |
18 | 48 | ||
19 | Modes: | 49 | Modes: |
20 | -t, --list List the archive | 50 | -t, --list List the archive |
21 | -i, --extract Extract files from an archive | 51 | -i, --extract Extract files from an archive |
22 | -o, --create Create the archive | 52 | -o, --create Create the archive |
23 | -p, --pass-through Copy-pass mode [was ist das?!] | 53 | -p, --pass-through Copy-pass mode |
24 | 54 | ||
25 | Options valid in any mode: | 55 | Options valid in any mode: |
26 | --block-size=SIZE I/O block size = SIZE * 512 bytes | 56 | --block-size=SIZE I/O block size = SIZE * 512 bytes |
@@ -78,27 +108,28 @@ | |||
78 | --sparse Write files with blocks of zeros as sparse files | 108 | --sparse Write files with blocks of zeros as sparse files |
79 | -u, --unconditional Replace all files unconditionally | 109 | -u, --unconditional Replace all files unconditionally |
80 | */ | 110 | */ |
111 | |||
81 | enum { | 112 | enum { |
82 | CPIO_OPT_EXTRACT = (1 << 0), | 113 | OPT_EXTRACT = (1 << 0), |
83 | CPIO_OPT_TEST = (1 << 1), | 114 | OPT_TEST = (1 << 1), |
84 | CPIO_OPT_NUL_TERMINATED = (1 << 2), | 115 | OPT_NUL_TERMINATED = (1 << 2), |
85 | CPIO_OPT_UNCONDITIONAL = (1 << 3), | 116 | OPT_UNCONDITIONAL = (1 << 3), |
86 | CPIO_OPT_VERBOSE = (1 << 4), | 117 | OPT_VERBOSE = (1 << 4), |
87 | CPIO_OPT_CREATE_LEADING_DIR = (1 << 5), | 118 | OPT_CREATE_LEADING_DIR = (1 << 5), |
88 | CPIO_OPT_PRESERVE_MTIME = (1 << 6), | 119 | OPT_PRESERVE_MTIME = (1 << 6), |
89 | CPIO_OPT_DEREF = (1 << 7), | 120 | OPT_DEREF = (1 << 7), |
90 | CPIO_OPT_FILE = (1 << 8), | 121 | OPT_FILE = (1 << 8), |
91 | OPTBIT_FILE = 8, | 122 | OPTBIT_FILE = 8, |
92 | IF_FEATURE_CPIO_O(OPTBIT_CREATE ,) | 123 | IF_FEATURE_CPIO_O(OPTBIT_CREATE ,) |
93 | IF_FEATURE_CPIO_O(OPTBIT_FORMAT ,) | 124 | IF_FEATURE_CPIO_O(OPTBIT_FORMAT ,) |
94 | IF_FEATURE_CPIO_P(OPTBIT_PASSTHROUGH,) | 125 | IF_FEATURE_CPIO_P(OPTBIT_PASSTHROUGH,) |
95 | IF_LONG_OPTS( OPTBIT_QUIET ,) | 126 | IF_LONG_OPTS( OPTBIT_QUIET ,) |
96 | IF_LONG_OPTS( OPTBIT_2STDOUT ,) | 127 | IF_LONG_OPTS( OPTBIT_2STDOUT ,) |
97 | CPIO_OPT_CREATE = IF_FEATURE_CPIO_O((1 << OPTBIT_CREATE )) + 0, | 128 | OPT_CREATE = IF_FEATURE_CPIO_O((1 << OPTBIT_CREATE )) + 0, |
98 | CPIO_OPT_FORMAT = IF_FEATURE_CPIO_O((1 << OPTBIT_FORMAT )) + 0, | 129 | OPT_FORMAT = IF_FEATURE_CPIO_O((1 << OPTBIT_FORMAT )) + 0, |
99 | CPIO_OPT_PASSTHROUGH = IF_FEATURE_CPIO_P((1 << OPTBIT_PASSTHROUGH)) + 0, | 130 | OPT_PASSTHROUGH = IF_FEATURE_CPIO_P((1 << OPTBIT_PASSTHROUGH)) + 0, |
100 | CPIO_OPT_QUIET = IF_LONG_OPTS( (1 << OPTBIT_QUIET )) + 0, | 131 | OPT_QUIET = IF_LONG_OPTS( (1 << OPTBIT_QUIET )) + 0, |
101 | CPIO_OPT_2STDOUT = IF_LONG_OPTS( (1 << OPTBIT_2STDOUT )) + 0, | 132 | OPT_2STDOUT = IF_LONG_OPTS( (1 << OPTBIT_2STDOUT )) + 0, |
102 | }; | 133 | }; |
103 | 134 | ||
104 | #define OPTION_STR "it0uvdmLF:" | 135 | #define OPTION_STR "it0uvdmLF:" |
@@ -138,7 +169,7 @@ static NOINLINE int cpio_o(void) | |||
138 | char *line; | 169 | char *line; |
139 | struct stat st; | 170 | struct stat st; |
140 | 171 | ||
141 | line = (option_mask32 & CPIO_OPT_NUL_TERMINATED) | 172 | line = (option_mask32 & OPT_NUL_TERMINATED) |
142 | ? bb_get_chunk_from_file(stdin, NULL) | 173 | ? bb_get_chunk_from_file(stdin, NULL) |
143 | : xmalloc_fgetline(stdin); | 174 | : xmalloc_fgetline(stdin); |
144 | 175 | ||
@@ -153,7 +184,7 @@ static NOINLINE int cpio_o(void) | |||
153 | free(line); | 184 | free(line); |
154 | continue; | 185 | continue; |
155 | } | 186 | } |
156 | if ((option_mask32 & CPIO_OPT_DEREF) | 187 | if ((option_mask32 & OPT_DEREF) |
157 | ? stat(name, &st) | 188 | ? stat(name, &st) |
158 | : lstat(name, &st) | 189 | : lstat(name, &st) |
159 | ) { | 190 | ) { |
@@ -308,28 +339,24 @@ int cpio_main(int argc UNUSED_PARAM, char **argv) | |||
308 | /* -L makes sense only with -o or -p */ | 339 | /* -L makes sense only with -o or -p */ |
309 | 340 | ||
310 | #if !ENABLE_FEATURE_CPIO_O | 341 | #if !ENABLE_FEATURE_CPIO_O |
311 | /* no parameters */ | ||
312 | opt_complementary = "=0"; | ||
313 | opt = getopt32(argv, OPTION_STR, &cpio_filename); | 342 | opt = getopt32(argv, OPTION_STR, &cpio_filename); |
314 | argv += optind; | 343 | argv += optind; |
315 | if (opt & CPIO_OPT_FILE) { /* -F */ | 344 | if (opt & OPT_FILE) { /* -F */ |
316 | xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO); | 345 | xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO); |
317 | } | 346 | } |
318 | #else | 347 | #else |
319 | /* _exactly_ one parameter for -p, thus <= 1 param if -p is allowed */ | ||
320 | opt_complementary = ENABLE_FEATURE_CPIO_P ? "?1" : "=0"; | ||
321 | opt = getopt32(argv, OPTION_STR "oH:" IF_FEATURE_CPIO_P("p"), &cpio_filename, &cpio_fmt); | 348 | opt = getopt32(argv, OPTION_STR "oH:" IF_FEATURE_CPIO_P("p"), &cpio_filename, &cpio_fmt); |
322 | argv += optind; | 349 | argv += optind; |
323 | if ((opt & (CPIO_OPT_FILE|CPIO_OPT_CREATE)) == CPIO_OPT_FILE) { /* -F without -o */ | 350 | if ((opt & (OPT_FILE|OPT_CREATE)) == OPT_FILE) { /* -F without -o */ |
324 | xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO); | 351 | xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO); |
325 | } | 352 | } |
326 | if (opt & CPIO_OPT_PASSTHROUGH) { | 353 | if (opt & OPT_PASSTHROUGH) { |
327 | pid_t pid; | 354 | pid_t pid; |
328 | struct fd_pair pp; | 355 | struct fd_pair pp; |
329 | 356 | ||
330 | if (argv[0] == NULL) | 357 | if (argv[0] == NULL) |
331 | bb_show_usage(); | 358 | bb_show_usage(); |
332 | if (opt & CPIO_OPT_CREATE_LEADING_DIR) | 359 | if (opt & OPT_CREATE_LEADING_DIR) |
333 | mkdir(argv[0], 0777); | 360 | mkdir(argv[0], 0777); |
334 | /* Crude existence check: | 361 | /* Crude existence check: |
335 | * close(xopen(argv[0], O_RDONLY | O_DIRECTORY)); | 362 | * close(xopen(argv[0], O_RDONLY | O_DIRECTORY)); |
@@ -361,15 +388,15 @@ int cpio_main(int argc UNUSED_PARAM, char **argv) | |||
361 | xchdir(*argv++); | 388 | xchdir(*argv++); |
362 | close(pp.wr); | 389 | close(pp.wr); |
363 | xmove_fd(pp.rd, STDIN_FILENO); | 390 | xmove_fd(pp.rd, STDIN_FILENO); |
364 | //opt &= ~CPIO_OPT_PASSTHROUGH; | 391 | //opt &= ~OPT_PASSTHROUGH; |
365 | opt |= CPIO_OPT_EXTRACT; | 392 | opt |= OPT_EXTRACT; |
366 | goto skip; | 393 | goto skip; |
367 | } | 394 | } |
368 | /* -o */ | 395 | /* -o */ |
369 | if (opt & CPIO_OPT_CREATE) { | 396 | if (opt & OPT_CREATE) { |
370 | if (cpio_fmt[0] != 'n') /* we _require_ "-H newc" */ | 397 | if (cpio_fmt[0] != 'n') /* we _require_ "-H newc" */ |
371 | bb_show_usage(); | 398 | bb_show_usage(); |
372 | if (opt & CPIO_OPT_FILE) { | 399 | if (opt & OPT_FILE) { |
373 | xmove_fd(xopen(cpio_filename, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO); | 400 | xmove_fd(xopen(cpio_filename, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO); |
374 | } | 401 | } |
375 | dump: | 402 | dump: |
@@ -379,35 +406,35 @@ int cpio_main(int argc UNUSED_PARAM, char **argv) | |||
379 | #endif | 406 | #endif |
380 | 407 | ||
381 | /* One of either extract or test options must be given */ | 408 | /* One of either extract or test options must be given */ |
382 | if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) { | 409 | if ((opt & (OPT_TEST | OPT_EXTRACT)) == 0) { |
383 | bb_show_usage(); | 410 | bb_show_usage(); |
384 | } | 411 | } |
385 | 412 | ||
386 | if (opt & CPIO_OPT_TEST) { | 413 | if (opt & OPT_TEST) { |
387 | /* if both extract and test options are given, ignore extract option */ | 414 | /* if both extract and test options are given, ignore extract option */ |
388 | opt &= ~CPIO_OPT_EXTRACT; | 415 | opt &= ~OPT_EXTRACT; |
389 | archive_handle->action_header = header_list; | 416 | archive_handle->action_header = header_list; |
390 | } | 417 | } |
391 | if (opt & CPIO_OPT_EXTRACT) { | 418 | if (opt & OPT_EXTRACT) { |
392 | archive_handle->action_data = data_extract_all; | 419 | archive_handle->action_data = data_extract_all; |
393 | if (opt & CPIO_OPT_2STDOUT) | 420 | if (opt & OPT_2STDOUT) |
394 | archive_handle->action_data = data_extract_to_stdout; | 421 | archive_handle->action_data = data_extract_to_stdout; |
395 | } | 422 | } |
396 | if (opt & CPIO_OPT_UNCONDITIONAL) { | 423 | if (opt & OPT_UNCONDITIONAL) { |
397 | archive_handle->ah_flags |= ARCHIVE_UNLINK_OLD; | 424 | archive_handle->ah_flags |= ARCHIVE_UNLINK_OLD; |
398 | archive_handle->ah_flags &= ~ARCHIVE_EXTRACT_NEWER; | 425 | archive_handle->ah_flags &= ~ARCHIVE_EXTRACT_NEWER; |
399 | } | 426 | } |
400 | if (opt & CPIO_OPT_VERBOSE) { | 427 | if (opt & OPT_VERBOSE) { |
401 | if (archive_handle->action_header == header_list) { | 428 | if (archive_handle->action_header == header_list) { |
402 | archive_handle->action_header = header_verbose_list; | 429 | archive_handle->action_header = header_verbose_list; |
403 | } else { | 430 | } else { |
404 | archive_handle->action_header = header_list; | 431 | archive_handle->action_header = header_list; |
405 | } | 432 | } |
406 | } | 433 | } |
407 | if (opt & CPIO_OPT_CREATE_LEADING_DIR) { | 434 | if (opt & OPT_CREATE_LEADING_DIR) { |
408 | archive_handle->ah_flags |= ARCHIVE_CREATE_LEADING_DIRS; | 435 | archive_handle->ah_flags |= ARCHIVE_CREATE_LEADING_DIRS; |
409 | } | 436 | } |
410 | if (opt & CPIO_OPT_PRESERVE_MTIME) { | 437 | if (opt & OPT_PRESERVE_MTIME) { |
411 | archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE; | 438 | archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE; |
412 | } | 439 | } |
413 | 440 | ||
@@ -423,7 +450,7 @@ int cpio_main(int argc UNUSED_PARAM, char **argv) | |||
423 | continue; | 450 | continue; |
424 | 451 | ||
425 | if (archive_handle->cpio__blocks != (off_t)-1 | 452 | if (archive_handle->cpio__blocks != (off_t)-1 |
426 | && !(opt & CPIO_OPT_QUIET) | 453 | && !(opt & OPT_QUIET) |
427 | ) { | 454 | ) { |
428 | fprintf(stderr, "%"OFF_FMT"u blocks\n", archive_handle->cpio__blocks); | 455 | fprintf(stderr, "%"OFF_FMT"u blocks\n", archive_handle->cpio__blocks); |
429 | } | 456 | } |
diff --git a/include/usage.src.h b/include/usage.src.h index 4640338e6..69a9fa159 100644 --- a/include/usage.src.h +++ b/include/usage.src.h | |||
@@ -592,35 +592,6 @@ INSERT | |||
592 | "\n -i Prompt before overwrite" \ | 592 | "\n -i Prompt before overwrite" \ |
593 | "\n -l,-s Create (sym)links" \ | 593 | "\n -l,-s Create (sym)links" \ |
594 | 594 | ||
595 | #define cpio_trivial_usage \ | ||
596 | "[-dmvu] [-F FILE]" IF_FEATURE_CPIO_O(" [-H newc]") \ | ||
597 | " [-ti"IF_FEATURE_CPIO_O("o")"]" IF_FEATURE_CPIO_P(" [-p DIR]") | ||
598 | #define cpio_full_usage "\n\n" \ | ||
599 | "Extract or list files from a cpio archive" \ | ||
600 | IF_FEATURE_CPIO_O(", or" \ | ||
601 | "\ncreate an archive" IF_FEATURE_CPIO_P(" (-o) or copy files (-p)") \ | ||
602 | " using file list on stdin" \ | ||
603 | ) \ | ||
604 | "\n" \ | ||
605 | "\nMain operation mode:" \ | ||
606 | "\n -t List" \ | ||
607 | "\n -i Extract" \ | ||
608 | IF_FEATURE_CPIO_O( \ | ||
609 | "\n -o Create (requires -H newc)" \ | ||
610 | ) \ | ||
611 | IF_FEATURE_CPIO_P( \ | ||
612 | "\n -p DIR Copy files to DIR" \ | ||
613 | ) \ | ||
614 | "\nOptions:" \ | ||
615 | "\n -d Make leading directories" \ | ||
616 | "\n -m Preserve mtime" \ | ||
617 | "\n -v Verbose" \ | ||
618 | "\n -u Overwrite" \ | ||
619 | "\n -F FILE Input (-t,-i,-p) or output (-o) file" \ | ||
620 | IF_FEATURE_CPIO_O( \ | ||
621 | "\n -H newc Archive format" \ | ||
622 | ) \ | ||
623 | |||
624 | #define crond_trivial_usage \ | 595 | #define crond_trivial_usage \ |
625 | "-fbS -l N " IF_FEATURE_CROND_D("-d N ") "-L LOGFILE -c DIR" | 596 | "-fbS -l N " IF_FEATURE_CROND_D("-d N ") "-L LOGFILE -c DIR" |
626 | #define crond_full_usage "\n\n" \ | 597 | #define crond_full_usage "\n\n" \ |