blob: 0fbe7b8e54c76982ef8be0f30f505a0b767c803f [file] [log] [blame]
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini cpio implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 2001 by Glenn McGrath
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +00006 *
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 McGrath8f5b63e2001-06-22 09:22:06 +000024 *
25 */
26#include <fcntl.h>
27#include <getopt.h>
28#include <stdlib.h>
29#include <string.h>
30#include <unistd.h>
Glenn L McGrathef0eab52001-10-25 14:49:48 +000031#include "unarchive.h"
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000032#include "busybox.h"
33
Glenn L McGrath10b78132004-02-25 09:30:06 +000034#define CPIO_OPT_EXTRACT 0x01
35#define CPIO_OPT_TEST 0x02
36#define CPIO_OPT_UNCONDITIONAL 0x04
37#define CPIO_OPT_VERBOSE 0x08
38#define CPIO_OPT_FILE 0x10
39#define CPIO_OPT_CREATE_LEADING_DIR 0x20
40#define CPIO_OPT_PRESERVE_MTIME 0x40
41
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000042extern int cpio_main(int argc, char **argv)
43{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000044 archive_handle_t *archive_handle;
Glenn L McGrath10b78132004-02-25 09:30:06 +000045 char *cpio_filename = NULL;
46 unsigned long opt;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000047
48 /* Initialise */
49 archive_handle = init_handle();
Eric Andersen70060d22004-03-27 10:02:48 +000050 archive_handle->src_fd = STDIN_FILENO;
Glenn L McGrath237ae422002-11-03 14:05:15 +000051 archive_handle->seek = seek_by_char;
Glenn L McGrath4cee66d2003-08-28 19:12:23 +000052 archive_handle->flags = ARCHIVE_EXTRACT_NEWER | ARCHIVE_PRESERVE_DATE;
Glenn L McGrath10b78132004-02-25 09:30:06 +000053
54 opt = bb_getopt_ulflags(argc, argv, "ituvF:dm", &cpio_filename);
55
56 /* One of either extract or test options must be given */
57 if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) {
58 bb_show_usage();
59 }
60
61 if (opt & CPIO_OPT_TEST) {
62 /* if both extract and test option are given, ignore extract option */
63 if (opt & CPIO_OPT_EXTRACT) {
64 opt &= ~CPIO_OPT_EXTRACT;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000065 }
Glenn L McGrath10b78132004-02-25 09:30:06 +000066 archive_handle->action_header = header_list;
67 }
68 if (opt & CPIO_OPT_EXTRACT) {
69 archive_handle->action_data = data_extract_all;
70 }
71 if (opt & CPIO_OPT_UNCONDITIONAL) {
72 archive_handle->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
73 archive_handle->flags &= ~ARCHIVE_EXTRACT_NEWER;
74 }
75 if (opt & CPIO_OPT_VERBOSE) {
76 if (archive_handle->action_header == header_list) {
77 archive_handle->action_header = header_verbose_list;
78 } else {
79 archive_handle->action_header = header_list;
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000080 }
81 }
Glenn L McGrath10b78132004-02-25 09:30:06 +000082 if (cpio_filename) { /* CPIO_OPT_FILE */
83 archive_handle->src_fd = bb_xopen(cpio_filename, O_RDONLY);
84 archive_handle->seek = seek_by_jump;
85 }
86 if (opt & CPIO_OPT_CREATE_LEADING_DIR) {
87 archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
88 }
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000089
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000090 while (optind < argc) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000091 archive_handle->filter = filter_accept_list;
Glenn L McGrath66125c82002-12-08 00:54:33 +000092 archive_handle->accept = llist_add_to(archive_handle->accept, argv[optind]);
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000093 optind++;
94 }
95
Glenn L McGrathb72a7352002-12-10 00:17:22 +000096 while (get_header_cpio(archive_handle) == EXIT_SUCCESS);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000097
98 return(EXIT_SUCCESS);
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000099}