tar.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (c) 2020, Peter Elliott <pelliott@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "LibCore/Directory.h"
  7. #include <AK/Assertions.h>
  8. #include <AK/LexicalPath.h>
  9. #include <AK/Span.h>
  10. #include <AK/Vector.h>
  11. #include <LibArchive/TarStream.h>
  12. #include <LibCompress/Gzip.h>
  13. #include <LibCore/ArgsParser.h>
  14. #include <LibCore/DirIterator.h>
  15. #include <LibCore/File.h>
  16. #include <LibCore/FileStream.h>
  17. #include <LibCore/System.h>
  18. #include <LibMain/Main.h>
  19. #include <fcntl.h>
  20. #include <stdio.h>
  21. #include <sys/stat.h>
  22. #include <unistd.h>
  23. constexpr size_t buffer_size = 4096;
  24. ErrorOr<int> serenity_main(Main::Arguments arguments)
  25. {
  26. bool create = false;
  27. bool extract = false;
  28. bool list = false;
  29. bool verbose = false;
  30. bool gzip = false;
  31. bool no_auto_compress = false;
  32. StringView archive_file;
  33. bool dereference;
  34. StringView directory;
  35. Vector<String> paths;
  36. Core::ArgsParser args_parser;
  37. args_parser.add_option(create, "Create archive", "create", 'c');
  38. args_parser.add_option(extract, "Extract archive", "extract", 'x');
  39. args_parser.add_option(list, "List contents", "list", 't');
  40. args_parser.add_option(verbose, "Print paths", "verbose", 'v');
  41. args_parser.add_option(gzip, "Compress or decompress file using gzip", "gzip", 'z');
  42. args_parser.add_option(no_auto_compress, "Do not use the archive suffix to select the compression algorithm", "no-auto-compress", 0);
  43. args_parser.add_option(directory, "Directory to extract to/create from", "directory", 'C', "DIRECTORY");
  44. args_parser.add_option(archive_file, "Archive file", "file", 'f', "FILE");
  45. args_parser.add_option(dereference, "Follow symlinks", "dereference", 'h');
  46. args_parser.add_positional_argument(paths, "Paths", "PATHS", Core::ArgsParser::Required::No);
  47. args_parser.parse(arguments);
  48. if (create + extract + list != 1) {
  49. warnln("exactly one of -c, -x, and -t can be used");
  50. return 1;
  51. }
  52. if (!no_auto_compress && !archive_file.is_empty()) {
  53. if (archive_file.ends_with(".gz"sv) || archive_file.ends_with(".tgz"sv))
  54. gzip = true;
  55. }
  56. if (list || extract) {
  57. auto file = Core::File::standard_input();
  58. if (!archive_file.is_empty())
  59. file = TRY(Core::File::open(archive_file, Core::OpenMode::ReadOnly));
  60. if (!directory.is_empty())
  61. TRY(Core::System::chdir(directory));
  62. Core::InputFileStream file_stream(file);
  63. Compress::GzipDecompressor gzip_stream(file_stream);
  64. InputStream& file_input_stream = file_stream;
  65. InputStream& gzip_input_stream = gzip_stream;
  66. Archive::TarInputStream tar_stream((gzip) ? gzip_input_stream : file_input_stream);
  67. // FIXME: implement ErrorOr<TarInputStream>?
  68. if (!tar_stream.valid()) {
  69. warnln("the provided file is not a well-formatted ustar file");
  70. return 1;
  71. }
  72. HashMap<String, String> global_overrides;
  73. HashMap<String, String> local_overrides;
  74. auto get_override = [&](StringView key) -> Optional<String> {
  75. Optional<String> maybe_local = local_overrides.get(key);
  76. if (maybe_local.has_value())
  77. return maybe_local;
  78. Optional<String> maybe_global = global_overrides.get(key);
  79. if (maybe_global.has_value())
  80. return maybe_global;
  81. return {};
  82. };
  83. while (!tar_stream.finished()) {
  84. Archive::TarFileHeader const& header = tar_stream.header();
  85. // Handle meta-entries earlier to avoid consuming the file content stream.
  86. if (header.content_is_like_extended_header()) {
  87. switch (header.type_flag()) {
  88. case Archive::TarFileType::GlobalExtendedHeader: {
  89. TRY(tar_stream.for_each_extended_header([&](StringView key, StringView value) {
  90. if (value.length() == 0)
  91. global_overrides.remove(key);
  92. else
  93. global_overrides.set(key, value);
  94. }));
  95. break;
  96. }
  97. case Archive::TarFileType::ExtendedHeader: {
  98. TRY(tar_stream.for_each_extended_header([&](StringView key, StringView value) {
  99. local_overrides.set(key, value);
  100. }));
  101. break;
  102. }
  103. default:
  104. warnln("Unknown extended header type '{}' of {}", (char)header.type_flag(), header.filename());
  105. VERIFY_NOT_REACHED();
  106. }
  107. TRY(tar_stream.advance());
  108. continue;
  109. }
  110. Archive::TarFileStream file_stream = tar_stream.file_contents();
  111. // Handle other header types that don't just have an effect on extraction.
  112. switch (header.type_flag()) {
  113. case Archive::TarFileType::LongName: {
  114. StringBuilder long_name;
  115. Array<u8, buffer_size> buffer;
  116. size_t bytes_read;
  117. while ((bytes_read = file_stream.read(buffer)) > 0)
  118. long_name.append(reinterpret_cast<char*>(buffer.data()), bytes_read);
  119. local_overrides.set("path", long_name.to_string());
  120. TRY(tar_stream.advance());
  121. continue;
  122. }
  123. default:
  124. // None of the relevant headers, so continue as normal.
  125. break;
  126. }
  127. LexicalPath path = LexicalPath(header.filename());
  128. if (!header.prefix().is_empty())
  129. path = path.prepend(header.prefix());
  130. String filename = get_override("path"sv).value_or(path.string());
  131. if (list || verbose)
  132. outln("{}", filename);
  133. if (extract) {
  134. String absolute_path = Core::File::absolute_path(filename);
  135. auto parent_path = LexicalPath(absolute_path).parent();
  136. auto header_mode = TRY(header.mode());
  137. switch (header.type_flag()) {
  138. case Archive::TarFileType::NormalFile:
  139. case Archive::TarFileType::AlternateNormalFile: {
  140. MUST(Core::Directory::create(parent_path, Core::Directory::CreateDirectories::Yes));
  141. int fd = TRY(Core::System::open(absolute_path, O_CREAT | O_WRONLY, header_mode));
  142. Array<u8, buffer_size> buffer;
  143. size_t bytes_read;
  144. while ((bytes_read = file_stream.read(buffer)) > 0)
  145. TRY(Core::System::write(fd, buffer.span().slice(0, bytes_read)));
  146. TRY(Core::System::close(fd));
  147. break;
  148. }
  149. case Archive::TarFileType::SymLink: {
  150. MUST(Core::Directory::create(parent_path, Core::Directory::CreateDirectories::Yes));
  151. TRY(Core::System::symlink(header.link_name(), absolute_path));
  152. break;
  153. }
  154. case Archive::TarFileType::Directory: {
  155. MUST(Core::Directory::create(parent_path, Core::Directory::CreateDirectories::Yes));
  156. auto result_or_error = Core::System::mkdir(absolute_path, header_mode);
  157. if (result_or_error.is_error() && result_or_error.error().code() != EEXIST)
  158. return result_or_error.error();
  159. break;
  160. }
  161. default:
  162. // FIXME: Implement other file types
  163. warnln("file type '{}' of {} is not yet supported", (char)header.type_flag(), header.filename());
  164. VERIFY_NOT_REACHED();
  165. }
  166. }
  167. // Non-global headers should be cleared after every file.
  168. local_overrides.clear();
  169. TRY(tar_stream.advance());
  170. }
  171. file_stream.close();
  172. return 0;
  173. }
  174. if (create) {
  175. if (paths.size() == 0) {
  176. warnln("you must provide at least one path to be archived");
  177. return 1;
  178. }
  179. auto file = Core::File::standard_output();
  180. if (!archive_file.is_empty())
  181. file = TRY(Core::File::open(archive_file, Core::OpenMode::WriteOnly));
  182. if (!directory.is_empty())
  183. TRY(Core::System::chdir(directory));
  184. Core::OutputFileStream file_stream(file);
  185. Compress::GzipCompressor gzip_stream(file_stream);
  186. OutputStream& file_output_stream = file_stream;
  187. OutputStream& gzip_output_stream = gzip_stream;
  188. Archive::TarOutputStream tar_stream((gzip) ? gzip_output_stream : file_output_stream);
  189. auto add_file = [&](String path) -> ErrorOr<void> {
  190. auto file = Core::File::construct(path);
  191. if (!file->open(Core::OpenMode::ReadOnly)) {
  192. warnln("Failed to open {}: {}", path, file->error_string());
  193. return {};
  194. }
  195. auto statbuf = TRY(Core::System::lstat(path));
  196. auto canonicalized_path = LexicalPath::canonicalized_path(path);
  197. tar_stream.add_file(canonicalized_path, statbuf.st_mode, file->read_all());
  198. if (verbose)
  199. outln("{}", canonicalized_path);
  200. return {};
  201. };
  202. auto add_link = [&](String path) -> ErrorOr<void> {
  203. auto statbuf = TRY(Core::System::lstat(path));
  204. auto canonicalized_path = LexicalPath::canonicalized_path(path);
  205. tar_stream.add_link(canonicalized_path, statbuf.st_mode, TRY(Core::System::readlink(path)));
  206. if (verbose)
  207. outln("{}", canonicalized_path);
  208. return {};
  209. };
  210. auto add_directory = [&](String path, auto handle_directory) -> ErrorOr<void> {
  211. auto statbuf = TRY(Core::System::lstat(path));
  212. auto canonicalized_path = LexicalPath::canonicalized_path(path);
  213. tar_stream.add_directory(canonicalized_path, statbuf.st_mode);
  214. if (verbose)
  215. outln("{}", canonicalized_path);
  216. Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir);
  217. while (it.has_next()) {
  218. auto child_path = it.next_full_path();
  219. if (!dereference && Core::File::is_link(child_path)) {
  220. TRY(add_link(child_path));
  221. } else if (!Core::File::is_directory(child_path)) {
  222. TRY(add_file(child_path));
  223. } else {
  224. TRY(handle_directory(child_path, handle_directory));
  225. }
  226. }
  227. return {};
  228. };
  229. for (auto const& path : paths) {
  230. if (Core::File::is_directory(path)) {
  231. TRY(add_directory(path, add_directory));
  232. } else {
  233. TRY(add_file(path));
  234. }
  235. }
  236. tar_stream.finish();
  237. return 0;
  238. }
  239. return 0;
  240. }