cpio.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini cpio implementation for busybox
  4. *
  5. * Copyright (C) 2001 by Glenn McGrath
  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
  24. *
  25. */
  26. #include <fcntl.h>
  27. #include <getopt.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include "unarchive.h"
  32. #include "busybox.h"
  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
  38. #define CPIO_OPT_CREATE_LEADING_DIR 0x20
  39. #define CPIO_OPT_PRESERVE_MTIME 0x40
  40. extern int cpio_main(int argc, char **argv)
  41. {
  42. archive_handle_t *archive_handle;
  43. char *cpio_filename = NULL;
  44. unsigned long opt;
  45. /* Initialise */
  46. archive_handle = init_handle();
  47. archive_handle->src_fd = STDIN_FILENO;
  48. archive_handle->seek = seek_by_char;
  49. archive_handle->flags = ARCHIVE_EXTRACT_NEWER | ARCHIVE_PRESERVE_DATE;
  50. opt = bb_getopt_ulflags(argc, argv, "ituvF:dm", &cpio_filename);
  51. /* One of either extract or test options must be given */
  52. if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) {
  53. bb_show_usage();
  54. }
  55. if (opt & CPIO_OPT_TEST) {
  56. /* if both extract and test option are given, ignore extract option */
  57. if (opt & CPIO_OPT_EXTRACT) {
  58. opt &= ~CPIO_OPT_EXTRACT;
  59. }
  60. archive_handle->action_header = header_list;
  61. }
  62. if (opt & CPIO_OPT_EXTRACT) {
  63. archive_handle->action_data = data_extract_all;
  64. }
  65. if (opt & CPIO_OPT_UNCONDITIONAL) {
  66. archive_handle->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
  67. archive_handle->flags &= ~ARCHIVE_EXTRACT_NEWER;
  68. }
  69. if (opt & CPIO_OPT_VERBOSE) {
  70. if (archive_handle->action_header == header_list) {
  71. archive_handle->action_header = header_verbose_list;
  72. } else {
  73. archive_handle->action_header = header_list;
  74. }
  75. }
  76. if (cpio_filename) { /* CPIO_OPT_FILE */
  77. archive_handle->src_fd = bb_xopen(cpio_filename, O_RDONLY);
  78. archive_handle->seek = seek_by_jump;
  79. }
  80. if (opt & CPIO_OPT_CREATE_LEADING_DIR) {
  81. archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
  82. }
  83. while (optind < argc) {
  84. archive_handle->filter = filter_accept_list;
  85. archive_handle->accept = llist_add_to(archive_handle->accept, argv[optind]);
  86. optind++;
  87. }
  88. while (get_header_cpio(archive_handle) == EXIT_SUCCESS);
  89. return(EXIT_SUCCESS);
  90. }