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 | * |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 8 | * |
| 9 | * Limitations: |
| 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 | * |
| 13 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 15 | #include "unarchive.h" |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 17 | #define CPIO_OPT_EXTRACT 0x01 |
| 18 | #define CPIO_OPT_TEST 0x02 |
| 19 | #define CPIO_OPT_UNCONDITIONAL 0x04 |
| 20 | #define CPIO_OPT_VERBOSE 0x08 |
| 21 | #define CPIO_OPT_FILE 0x10 |
| 22 | #define CPIO_OPT_CREATE_LEADING_DIR 0x20 |
| 23 | #define CPIO_OPT_PRESERVE_MTIME 0x40 |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 25 | int cpio_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 26 | int cpio_main(int argc, char **argv) |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 27 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 28 | archive_handle_t *archive_handle; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 29 | char *cpio_filename = NULL; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 30 | unsigned opt; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 31 | |
| 32 | /* Initialise */ |
| 33 | archive_handle = init_handle(); |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 34 | archive_handle->src_fd = STDIN_FILENO; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 35 | archive_handle->seek = seek_by_read; |
Glenn L McGrath | 4cee66d | 2003-08-28 19:12:23 +0000 | [diff] [blame] | 36 | archive_handle->flags = ARCHIVE_EXTRACT_NEWER | ARCHIVE_PRESERVE_DATE; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 38 | opt = getopt32(argv, "ituvF:dm", &cpio_filename); |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 39 | |
| 40 | /* One of either extract or test options must be given */ |
| 41 | if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) { |
| 42 | bb_show_usage(); |
| 43 | } |
| 44 | |
| 45 | if (opt & CPIO_OPT_TEST) { |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 46 | /* if both extract and test options are given, ignore extract option */ |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 47 | if (opt & CPIO_OPT_EXTRACT) { |
| 48 | opt &= ~CPIO_OPT_EXTRACT; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 49 | } |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 50 | archive_handle->action_header = header_list; |
| 51 | } |
| 52 | if (opt & CPIO_OPT_EXTRACT) { |
| 53 | archive_handle->action_data = data_extract_all; |
| 54 | } |
| 55 | if (opt & CPIO_OPT_UNCONDITIONAL) { |
| 56 | archive_handle->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL; |
| 57 | archive_handle->flags &= ~ARCHIVE_EXTRACT_NEWER; |
| 58 | } |
| 59 | if (opt & CPIO_OPT_VERBOSE) { |
| 60 | if (archive_handle->action_header == header_list) { |
| 61 | archive_handle->action_header = header_verbose_list; |
| 62 | } else { |
| 63 | archive_handle->action_header = header_list; |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 66 | if (cpio_filename) { /* CPIO_OPT_FILE */ |
Rob Landley | 86b4d64 | 2006-08-03 17:58:17 +0000 | [diff] [blame] | 67 | archive_handle->src_fd = xopen(cpio_filename, O_RDONLY); |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 68 | archive_handle->seek = seek_by_jump; |
| 69 | } |
| 70 | if (opt & CPIO_OPT_CREATE_LEADING_DIR) { |
| 71 | archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS; |
| 72 | } |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 73 | |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 74 | while (optind < argc) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 75 | archive_handle->filter = filter_accept_list; |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 76 | llist_add_to(&(archive_handle->accept), argv[optind]); |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 77 | optind++; |
| 78 | } |
| 79 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 80 | while (get_header_cpio(archive_handle) == EXIT_SUCCESS); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 81 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 82 | return EXIT_SUCCESS; |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 83 | } |