tar.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2020, Peter Elliott <pelliott@ualberta.ca>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/LogStream.h>
  27. #include <AK/Vector.h>
  28. #include <LibCompress/Gzip.h>
  29. #include <LibCore/ArgsParser.h>
  30. #include <LibCore/FileStream.h>
  31. #include <LibTar/TarStream.h>
  32. #include <fcntl.h>
  33. #include <stdio.h>
  34. #include <sys/stat.h>
  35. constexpr size_t buffer_size = 4096;
  36. int main(int argc, char** argv)
  37. {
  38. bool create = false;
  39. bool extract = false;
  40. bool list = false;
  41. bool verbose = false;
  42. bool gzip = false;
  43. const char* archive_file = nullptr;
  44. Vector<const char*> paths;
  45. Core::ArgsParser args_parser;
  46. args_parser.add_option(create, "Create archive", "create", 'c');
  47. args_parser.add_option(extract, "Extract archive", "extract", 'x');
  48. args_parser.add_option(list, "List contents", "list", 't');
  49. args_parser.add_option(verbose, "Print paths", "verbose", 'v');
  50. args_parser.add_option(gzip, "compress or uncompress file using gzip", "gzip", 'z');
  51. args_parser.add_option(archive_file, "Archive file", "file", 'f', "FILE");
  52. args_parser.add_positional_argument(paths, "Paths", "PATHS", Core::ArgsParser::Required::No);
  53. args_parser.parse(argc, argv);
  54. if (create + extract + list != 1) {
  55. warnln("exactly one of -c, -x, and -t can be used");
  56. return 1;
  57. }
  58. if (list || extract) {
  59. auto file = Core::File::stdin();
  60. if (archive_file) {
  61. auto maybe_file = Core::File::open(archive_file, Core::IODevice::OpenMode::ReadOnly);
  62. if (maybe_file.is_error()) {
  63. warnln("Core::File::open: {}", maybe_file.error());
  64. return 1;
  65. }
  66. file = maybe_file.value();
  67. }
  68. Core::InputFileStream file_stream(file);
  69. Compress::GzipDecompressor gzip_stream(file_stream);
  70. InputStream& file_input_stream = file_stream;
  71. InputStream& gzip_input_stream = gzip_stream;
  72. Tar::TarStream tar_stream((gzip) ? gzip_input_stream : file_input_stream);
  73. if (!tar_stream.valid()) {
  74. warn() << "the provided file is not a well-formatted ustar file";
  75. return 1;
  76. }
  77. for (; !tar_stream.finished(); tar_stream.advance()) {
  78. if (list || verbose)
  79. out() << tar_stream.header().file_name();
  80. if (extract) {
  81. Tar::TarFileStream file_stream = tar_stream.file_contents();
  82. const Tar::Header& header = tar_stream.header();
  83. switch (header.type_flag()) {
  84. case Tar::NormalFile:
  85. case Tar::AlternateNormalFile: {
  86. int fd = open(String(header.file_name()).characters(), O_CREAT | O_WRONLY, header.mode());
  87. if (fd < 0) {
  88. perror("open");
  89. return 1;
  90. }
  91. Array<u8, buffer_size> buffer;
  92. size_t nread;
  93. while ((nread = file_stream.read(buffer)) > 0) {
  94. if (write(fd, buffer.data(), nread) < 0) {
  95. perror("write");
  96. return 1;
  97. }
  98. }
  99. close(fd);
  100. break;
  101. }
  102. case Tar::Directory: {
  103. if (mkdir(String(header.file_name()).characters(), header.mode())) {
  104. perror("mkdir");
  105. return 1;
  106. }
  107. break;
  108. }
  109. default:
  110. // FIXME: Implement other file types
  111. ASSERT_NOT_REACHED();
  112. }
  113. }
  114. }
  115. file_stream.close();
  116. return 0;
  117. }
  118. // FIXME: Implement other operations.
  119. ASSERT_NOT_REACHED();
  120. return 0;
  121. }