tar.cpp 12 KB

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