tar.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2020, Peter Elliott <pelliott@ualberta.ca>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/LexicalPath.h>
  8. #include <AK/Span.h>
  9. #include <AK/Vector.h>
  10. #include <LibArchive/TarStream.h>
  11. #include <LibCompress/Gzip.h>
  12. #include <LibCore/ArgsParser.h>
  13. #include <LibCore/DirIterator.h>
  14. #include <LibCore/FileStream.h>
  15. #include <fcntl.h>
  16. #include <stdio.h>
  17. #include <sys/stat.h>
  18. #include <unistd.h>
  19. constexpr size_t buffer_size = 4096;
  20. int main(int argc, char** argv)
  21. {
  22. bool create = false;
  23. bool extract = false;
  24. bool list = false;
  25. bool verbose = false;
  26. bool gzip = false;
  27. const char* archive_file = nullptr;
  28. Vector<const char*> paths;
  29. Core::ArgsParser args_parser;
  30. args_parser.add_option(create, "Create archive", "create", 'c');
  31. args_parser.add_option(extract, "Extract archive", "extract", 'x');
  32. args_parser.add_option(list, "List contents", "list", 't');
  33. args_parser.add_option(verbose, "Print paths", "verbose", 'v');
  34. args_parser.add_option(gzip, "compress or uncompress file using gzip", "gzip", 'z');
  35. args_parser.add_option(archive_file, "Archive file", "file", 'f', "FILE");
  36. args_parser.add_positional_argument(paths, "Paths", "PATHS", Core::ArgsParser::Required::No);
  37. args_parser.parse(argc, argv);
  38. if (create + extract + list != 1) {
  39. warnln("exactly one of -c, -x, and -t can be used");
  40. return 1;
  41. }
  42. if (list || extract) {
  43. auto file = Core::File::standard_input();
  44. if (archive_file) {
  45. auto maybe_file = Core::File::open(archive_file, Core::OpenMode::ReadOnly);
  46. if (maybe_file.is_error()) {
  47. warnln("Core::File::open: {}", maybe_file.error());
  48. return 1;
  49. }
  50. file = maybe_file.value();
  51. }
  52. Core::InputFileStream file_stream(file);
  53. Compress::GzipDecompressor gzip_stream(file_stream);
  54. InputStream& file_input_stream = file_stream;
  55. InputStream& gzip_input_stream = gzip_stream;
  56. Archive::TarInputStream tar_stream((gzip) ? gzip_input_stream : file_input_stream);
  57. if (!tar_stream.valid()) {
  58. warnln("the provided file is not a well-formatted ustar file");
  59. return 1;
  60. }
  61. for (; !tar_stream.finished(); tar_stream.advance()) {
  62. if (list || verbose)
  63. outln("{}", tar_stream.header().filename());
  64. if (extract) {
  65. Archive::TarFileStream file_stream = tar_stream.file_contents();
  66. const Archive::TarFileHeader& header = tar_stream.header();
  67. switch (header.type_flag()) {
  68. case Archive::TarFileType::NormalFile:
  69. case Archive::TarFileType::AlternateNormalFile: {
  70. int fd = open(String(header.filename()).characters(), O_CREAT | O_WRONLY, header.mode());
  71. if (fd < 0) {
  72. perror("open");
  73. return 1;
  74. }
  75. Array<u8, buffer_size> buffer;
  76. size_t nread;
  77. while ((nread = file_stream.read(buffer)) > 0) {
  78. if (write(fd, buffer.data(), nread) < 0) {
  79. perror("write");
  80. return 1;
  81. }
  82. }
  83. close(fd);
  84. break;
  85. }
  86. case Archive::TarFileType::Directory: {
  87. if (mkdir(String(header.filename()).characters(), header.mode())) {
  88. perror("mkdir");
  89. return 1;
  90. }
  91. break;
  92. }
  93. default:
  94. // FIXME: Implement other file types
  95. VERIFY_NOT_REACHED();
  96. }
  97. }
  98. }
  99. file_stream.close();
  100. return 0;
  101. }
  102. if (create) {
  103. if (paths.size() == 0) {
  104. warnln("you must provide at least one path to be archived");
  105. return 1;
  106. }
  107. auto file = Core::File::standard_output();
  108. if (archive_file) {
  109. auto maybe_file = Core::File::open(archive_file, Core::OpenMode::WriteOnly);
  110. if (maybe_file.is_error()) {
  111. warnln("Core::File::open: {}", maybe_file.error());
  112. return 1;
  113. }
  114. file = maybe_file.value();
  115. }
  116. Core::OutputFileStream file_stream(file);
  117. Compress::GzipCompressor gzip_stream(file_stream);
  118. OutputStream& file_output_stream = file_stream;
  119. OutputStream& gzip_output_stream = gzip_stream;
  120. Archive::TarOutputStream tar_stream((gzip) ? gzip_output_stream : file_output_stream);
  121. auto add_file = [&](String path) {
  122. auto file = Core::File::construct(path);
  123. if (!file->open(Core::OpenMode::ReadOnly)) {
  124. warnln("Failed to open {}: {}", path, file->error_string());
  125. return;
  126. }
  127. struct stat statbuf;
  128. if (lstat(path.characters(), &statbuf) < 0) {
  129. warnln("Failed stating {}", path);
  130. return;
  131. }
  132. auto canonicalized_path = LexicalPath::canonicalized_path(path);
  133. tar_stream.add_file(canonicalized_path, statbuf.st_mode, file->read_all());
  134. if (verbose)
  135. outln("{}", canonicalized_path);
  136. };
  137. auto add_directory = [&](String path, auto handle_directory) -> void {
  138. struct stat statbuf;
  139. if (lstat(path.characters(), &statbuf) < 0) {
  140. warnln("Failed stating {}", path);
  141. return;
  142. }
  143. auto canonicalized_path = LexicalPath::canonicalized_path(path);
  144. tar_stream.add_directory(canonicalized_path, statbuf.st_mode);
  145. if (verbose)
  146. outln("{}", canonicalized_path);
  147. Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir);
  148. while (it.has_next()) {
  149. auto child_path = it.next_full_path();
  150. if (!Core::File::is_directory(child_path)) {
  151. add_file(child_path);
  152. } else {
  153. handle_directory(child_path, handle_directory);
  154. }
  155. }
  156. };
  157. for (auto const& path : paths) {
  158. if (Core::File::is_directory(path)) {
  159. add_directory(path, add_directory);
  160. } else {
  161. add_file(path);
  162. }
  163. }
  164. tar_stream.finish();
  165. return 0;
  166. }
  167. return 0;
  168. }