cpio.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 "libbb.h"
  15. #include "unarchive.h"
  16. #define CPIO_OPT_EXTRACT 0x01
  17. #define CPIO_OPT_TEST 0x02
  18. #define CPIO_OPT_UNCONDITIONAL 0x04
  19. #define CPIO_OPT_VERBOSE 0x08
  20. #define CPIO_OPT_FILE 0x10
  21. #define CPIO_OPT_CREATE_LEADING_DIR 0x20
  22. #define CPIO_OPT_PRESERVE_MTIME 0x40
  23. int cpio_main(int argc, char **argv);
  24. int cpio_main(int argc, char **argv)
  25. {
  26. archive_handle_t *archive_handle;
  27. char *cpio_filename = NULL;
  28. unsigned opt;
  29. /* Initialise */
  30. archive_handle = init_handle();
  31. archive_handle->src_fd = STDIN_FILENO;
  32. archive_handle->seek = seek_by_read;
  33. archive_handle->flags = ARCHIVE_EXTRACT_NEWER | ARCHIVE_PRESERVE_DATE;
  34. opt = getopt32(argc, argv, "ituvF:dm", &cpio_filename);
  35. /* One of either extract or test options must be given */
  36. if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) {
  37. bb_show_usage();
  38. }
  39. if (opt & CPIO_OPT_TEST) {
  40. /* if both extract and test options are given, ignore extract option */
  41. if (opt & CPIO_OPT_EXTRACT) {
  42. opt &= ~CPIO_OPT_EXTRACT;
  43. }
  44. archive_handle->action_header = header_list;
  45. }
  46. if (opt & CPIO_OPT_EXTRACT) {
  47. archive_handle->action_data = data_extract_all;
  48. }
  49. if (opt & CPIO_OPT_UNCONDITIONAL) {
  50. archive_handle->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
  51. archive_handle->flags &= ~ARCHIVE_EXTRACT_NEWER;
  52. }
  53. if (opt & CPIO_OPT_VERBOSE) {
  54. if (archive_handle->action_header == header_list) {
  55. archive_handle->action_header = header_verbose_list;
  56. } else {
  57. archive_handle->action_header = header_list;
  58. }
  59. }
  60. if (cpio_filename) { /* CPIO_OPT_FILE */
  61. archive_handle->src_fd = xopen(cpio_filename, O_RDONLY);
  62. archive_handle->seek = seek_by_jump;
  63. }
  64. if (opt & CPIO_OPT_CREATE_LEADING_DIR) {
  65. archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
  66. }
  67. while (optind < argc) {
  68. archive_handle->filter = filter_accept_list;
  69. llist_add_to(&(archive_handle->accept), argv[optind]);
  70. optind++;
  71. }
  72. while (get_header_cpio(archive_handle) == EXIT_SUCCESS);
  73. return EXIT_SUCCESS;
  74. }