/*
* Mini cpio implementation for busybox
*
- * Copyright (C) 2001 by Glenn McGrath
+ * Copyright (C) 2001 by Glenn McGrath
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
*/
#include <fcntl.h>
-#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include "unarchive.h"
#include "busybox.h"
-extern int cpio_main(int argc, char **argv)
+#define CPIO_OPT_EXTRACT 0x01
+#define CPIO_OPT_TEST 0x02
+#define CPIO_OPT_UNCONDITIONAL 0x04
+#define CPIO_OPT_VERBOSE 0x08
+#define CPIO_OPT_FILE 0x10
+#define CPIO_OPT_CREATE_LEADING_DIR 0x20
+#define CPIO_OPT_PRESERVE_MTIME 0x40
+
+int cpio_main(int argc, char **argv)
{
- FILE *src_stream = stdin;
- char **extract_names = NULL;
- int extract_function = 0;
- int num_of_entries = 0;
- int opt = 0;
- mode_t oldmask = 0;
+ archive_handle_t *archive_handle;
+ char *cpio_filename = NULL;
+ unsigned long opt;
- while ((opt = getopt(argc, argv, "idmuvtF:")) != -1) {
- switch (opt) {
- case 'i': // extract
- extract_function |= extract_all_to_fs;
- break;
- case 'd': // create _leading_ directories
- extract_function |= extract_create_leading_dirs;
- oldmask = umask(077); /* Make make_directory act like GNU cpio */
- break;
- case 'm': // preserve modification time
- extract_function |= extract_preserve_date;
- break;
- case 'v': // verbosly list files
- extract_function |= extract_verbose_list;
- break;
- case 'u': // unconditional
- extract_function |= extract_unconditional;
- break;
- case 't': // list files
- extract_function |= extract_list;
- break;
- case 'F':
- src_stream = xfopen(optarg, "r");
- break;
- default:
- show_usage();
- }
- }
+ /* Initialise */
+ archive_handle = init_handle();
+ archive_handle->src_fd = STDIN_FILENO;
+ archive_handle->seek = seek_by_char;
+ archive_handle->flags = ARCHIVE_EXTRACT_NEWER | ARCHIVE_PRESERVE_DATE;
+
+ opt = bb_getopt_ulflags(argc, argv, "ituvF:dm", &cpio_filename);
- if (extract_function & extract_all_to_fs && extract_function & extract_list) {
- extract_function ^= extract_all_to_fs; /* If specify t, don't extract*/
+ /* One of either extract or test options must be given */
+ if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) {
+ bb_show_usage();
}
- if (extract_function & extract_all_to_fs && extract_function & extract_verbose_list) { /* The meaning of v changes on extract */
- extract_function ^= extract_verbose_list;
- extract_function |= extract_list;
+ if (opt & CPIO_OPT_TEST) {
+ /* if both extract and test options are given, ignore extract option */
+ if (opt & CPIO_OPT_EXTRACT) {
+ opt &= ~CPIO_OPT_EXTRACT;
+ }
+ archive_handle->action_header = header_list;
+ }
+ if (opt & CPIO_OPT_EXTRACT) {
+ archive_handle->action_data = data_extract_all;
+ }
+ if (opt & CPIO_OPT_UNCONDITIONAL) {
+ archive_handle->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
+ archive_handle->flags &= ~ARCHIVE_EXTRACT_NEWER;
+ }
+ if (opt & CPIO_OPT_VERBOSE) {
+ if (archive_handle->action_header == header_list) {
+ archive_handle->action_header = header_verbose_list;
+ } else {
+ archive_handle->action_header = header_list;
+ }
+ }
+ if (cpio_filename) { /* CPIO_OPT_FILE */
+ archive_handle->src_fd = bb_xopen(cpio_filename, O_RDONLY);
+ archive_handle->seek = seek_by_jump;
+ }
+ if (opt & CPIO_OPT_CREATE_LEADING_DIR) {
+ archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
}
- extract_names = malloc(4);
while (optind < argc) {
- num_of_entries++;
- *extract_names = realloc(*extract_names, num_of_entries);
- extract_names[num_of_entries - 1] = xstrdup(argv[optind]);
+ archive_handle->filter = filter_accept_list;
+ llist_add_to(&(archive_handle->accept), argv[optind]);
optind++;
}
- unarchive(src_stream, stdout, &get_header_cpio, extract_function, "./", extract_names);
- if (oldmask) umask(oldmask); /* Restore umask if we changed it */
- return EXIT_SUCCESS;
-}
+ while (get_header_cpio(archive_handle) == EXIT_SUCCESS);
+ return(EXIT_SUCCESS);
+}