3
0

cpio.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini cpio implementation for busybox
  4. *
  5. * Copyright (C) 2001 by Glenn McGrath
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. *
  9. * Limitations:
  10. * Doesn't check CRC's
  11. * Only supports new ASCII and CRC formats
  12. *
  13. */
  14. #include <fcntl.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include "unarchive.h"
  19. #include "busybox.h"
  20. #define CPIO_OPT_EXTRACT 0x01
  21. #define CPIO_OPT_TEST 0x02
  22. #define CPIO_OPT_UNCONDITIONAL 0x04
  23. #define CPIO_OPT_VERBOSE 0x08
  24. #define CPIO_OPT_FILE 0x10
  25. #define CPIO_OPT_CREATE_LEADING_DIR 0x20
  26. #define CPIO_OPT_PRESERVE_MTIME 0x40
  27. int cpio_main(int argc, char **argv)
  28. {
  29. archive_handle_t *archive_handle;
  30. char *cpio_filename = NULL;
  31. unsigned opt;
  32. /* Initialise */
  33. archive_handle = init_handle();
  34. archive_handle->src_fd = STDIN_FILENO;
  35. archive_handle->seek = seek_by_read;
  36. archive_handle->flags = ARCHIVE_EXTRACT_NEWER | ARCHIVE_PRESERVE_DATE;
  37. opt = getopt32(argc, argv, "ituvF:dm", &cpio_filename);
  38. /* One of either extract or test options must be given */
  39. if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) {
  40. bb_show_usage();
  41. }
  42. if (opt & CPIO_OPT_TEST) {
  43. /* if both extract and test options are given, ignore extract option */
  44. if (opt & CPIO_OPT_EXTRACT) {
  45. opt &= ~CPIO_OPT_EXTRACT;
  46. }
  47. archive_handle->action_header = header_list;
  48. }
  49. if (opt & CPIO_OPT_EXTRACT) {
  50. archive_handle->action_data = data_extract_all;
  51. }
  52. if (opt & CPIO_OPT_UNCONDITIONAL) {
  53. archive_handle->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
  54. archive_handle->flags &= ~ARCHIVE_EXTRACT_NEWER;
  55. }
  56. if (opt & CPIO_OPT_VERBOSE) {
  57. if (archive_handle->action_header == header_list) {
  58. archive_handle->action_header = header_verbose_list;
  59. } else {
  60. archive_handle->action_header = header_list;
  61. }
  62. }
  63. if (cpio_filename) { /* CPIO_OPT_FILE */
  64. archive_handle->src_fd = xopen(cpio_filename, O_RDONLY);
  65. archive_handle->seek = seek_by_jump;
  66. }
  67. if (opt & CPIO_OPT_CREATE_LEADING_DIR) {
  68. archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
  69. }
  70. while (optind < argc) {
  71. archive_handle->filter = filter_accept_list;
  72. llist_add_to(&(archive_handle->accept), argv[optind]);
  73. optind++;
  74. }
  75. while (get_header_cpio(archive_handle) == EXIT_SUCCESS);
  76. return EXIT_SUCCESS;
  77. }