blob: 59ae60c77267563d8677a6b36378adf21f896b2c [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 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +00008 *
9 * Limitations:
10 * Doesn't check CRC's
11 * Only supports new ASCII and CRC formats
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000012 *
13 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000014#include "libbb.h"
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000015#include "unarchive.h"
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000016
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000017#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 McGrath10b78132004-02-25 09:30:06 +000024
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000025int cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000026int cpio_main(int argc, char **argv)
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000027{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000028 archive_handle_t *archive_handle;
Glenn L McGrath10b78132004-02-25 09:30:06 +000029 char *cpio_filename = NULL;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000030 unsigned opt;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000031
32 /* Initialise */
33 archive_handle = init_handle();
Eric Andersen70060d22004-03-27 10:02:48 +000034 archive_handle->src_fd = STDIN_FILENO;
Denis Vlasenko13858992006-10-08 12:49:22 +000035 archive_handle->seek = seek_by_read;
Glenn L McGrath4cee66d2003-08-28 19:12:23 +000036 archive_handle->flags = ARCHIVE_EXTRACT_NEWER | ARCHIVE_PRESERVE_DATE;
Glenn L McGrath10b78132004-02-25 09:30:06 +000037
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000038 opt = getopt32(argv, "ituvF:dm", &cpio_filename);
Glenn L McGrath10b78132004-02-25 09:30:06 +000039
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 Landleyb7128c62005-09-11 01:05:30 +000046 /* if both extract and test options are given, ignore extract option */
Glenn L McGrath10b78132004-02-25 09:30:06 +000047 if (opt & CPIO_OPT_EXTRACT) {
48 opt &= ~CPIO_OPT_EXTRACT;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000049 }
Glenn L McGrath10b78132004-02-25 09:30:06 +000050 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 McGrath8f5b63e2001-06-22 09:22:06 +000064 }
65 }
Rob Landleyb7128c62005-09-11 01:05:30 +000066 if (cpio_filename) { /* CPIO_OPT_FILE */
Rob Landley86b4d642006-08-03 17:58:17 +000067 archive_handle->src_fd = xopen(cpio_filename, O_RDONLY);
Glenn L McGrath10b78132004-02-25 09:30:06 +000068 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 McGrath8f5b63e2001-06-22 09:22:06 +000073
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000074 while (optind < argc) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000075 archive_handle->filter = filter_accept_list;
Rob Landley8bb50782006-05-26 23:44:51 +000076 llist_add_to(&(archive_handle->accept), argv[optind]);
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000077 optind++;
78 }
79
Glenn L McGrathb72a7352002-12-10 00:17:22 +000080 while (get_header_cpio(archive_handle) == EXIT_SUCCESS);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000081
Denis Vlasenko079f8af2006-11-27 16:49:31 +000082 return EXIT_SUCCESS;
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000083}