tar.cpp 7.3 KB

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