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 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | * Limitations: |
| 22 | * Doesn't check CRC's |
| 23 | * Only supports new ASCII and CRC formats |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 24 | * |
| 25 | */ |
| 26 | #include <fcntl.h> |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <unistd.h> |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 30 | #include "unarchive.h" |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 31 | #include "busybox.h" |
| 32 | |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 33 | #define CPIO_OPT_EXTRACT 0x01 |
| 34 | #define CPIO_OPT_TEST 0x02 |
| 35 | #define CPIO_OPT_UNCONDITIONAL 0x04 |
| 36 | #define CPIO_OPT_VERBOSE 0x08 |
| 37 | #define CPIO_OPT_FILE 0x10 |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 38 | #define CPIO_OPT_CREATE_LEADING_DIR 0x20 |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 39 | #define CPIO_OPT_PRESERVE_MTIME 0x40 |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 40 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame^] | 41 | int cpio_main(int argc, char **argv) |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 42 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 43 | archive_handle_t *archive_handle; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 44 | char *cpio_filename = NULL; |
| 45 | unsigned long opt; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 46 | |
| 47 | /* Initialise */ |
| 48 | archive_handle = init_handle(); |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 49 | archive_handle->src_fd = STDIN_FILENO; |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 50 | archive_handle->seek = seek_by_char; |
Glenn L McGrath | 4cee66d | 2003-08-28 19:12:23 +0000 | [diff] [blame] | 51 | archive_handle->flags = ARCHIVE_EXTRACT_NEWER | ARCHIVE_PRESERVE_DATE; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 52 | |
| 53 | opt = bb_getopt_ulflags(argc, argv, "ituvF:dm", &cpio_filename); |
| 54 | |
| 55 | /* One of either extract or test options must be given */ |
| 56 | if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) { |
| 57 | bb_show_usage(); |
| 58 | } |
| 59 | |
| 60 | if (opt & CPIO_OPT_TEST) { |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 61 | /* if both extract and test options are given, ignore extract option */ |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 62 | if (opt & CPIO_OPT_EXTRACT) { |
| 63 | opt &= ~CPIO_OPT_EXTRACT; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 64 | } |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 65 | archive_handle->action_header = header_list; |
| 66 | } |
| 67 | if (opt & CPIO_OPT_EXTRACT) { |
| 68 | archive_handle->action_data = data_extract_all; |
| 69 | } |
| 70 | if (opt & CPIO_OPT_UNCONDITIONAL) { |
| 71 | archive_handle->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL; |
| 72 | archive_handle->flags &= ~ARCHIVE_EXTRACT_NEWER; |
| 73 | } |
| 74 | if (opt & CPIO_OPT_VERBOSE) { |
| 75 | if (archive_handle->action_header == header_list) { |
| 76 | archive_handle->action_header = header_verbose_list; |
| 77 | } else { |
| 78 | archive_handle->action_header = header_list; |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 81 | if (cpio_filename) { /* CPIO_OPT_FILE */ |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 82 | archive_handle->src_fd = bb_xopen(cpio_filename, O_RDONLY); |
| 83 | archive_handle->seek = seek_by_jump; |
| 84 | } |
| 85 | if (opt & CPIO_OPT_CREATE_LEADING_DIR) { |
| 86 | archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS; |
| 87 | } |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 88 | |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 89 | while (optind < argc) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 90 | archive_handle->filter = filter_accept_list; |
Glenn L McGrath | 66125c8 | 2002-12-08 00:54:33 +0000 | [diff] [blame] | 91 | archive_handle->accept = llist_add_to(archive_handle->accept, argv[optind]); |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 92 | optind++; |
| 93 | } |
| 94 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 95 | while (get_header_cpio(archive_handle) == EXIT_SUCCESS); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 96 | |
| 97 | return(EXIT_SUCCESS); |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 98 | } |