2020-10-02 05:24:24 +00:00
|
|
|
/*
|
2021-09-01 02:32:46 +00:00
|
|
|
* Copyright (c) 2020, Peter Elliott <pelliott@serenityos.org>
|
2020-10-02 05:24:24 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-10-02 05:24:24 +00:00
|
|
|
*/
|
|
|
|
|
2021-05-15 10:34:40 +00:00
|
|
|
#include <AK/Assertions.h>
|
2021-03-12 23:45:23 +00:00
|
|
|
#include <AK/LexicalPath.h>
|
|
|
|
#include <AK/Span.h>
|
2020-10-02 05:24:24 +00:00
|
|
|
#include <AK/Vector.h>
|
2021-03-18 14:22:05 +00:00
|
|
|
#include <LibArchive/TarStream.h>
|
2020-10-03 18:59:41 +00:00
|
|
|
#include <LibCompress/Gzip.h>
|
2020-10-02 05:24:24 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-03-12 23:45:23 +00:00
|
|
|
#include <LibCore/DirIterator.h>
|
2021-11-09 23:27:01 +00:00
|
|
|
#include <LibCore/File.h>
|
2020-10-02 05:24:24 +00:00
|
|
|
#include <LibCore/FileStream.h>
|
2022-02-27 22:12:33 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2020-10-02 05:24:24 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/stat.h>
|
2021-03-12 16:29:37 +00:00
|
|
|
#include <unistd.h>
|
2020-10-02 05:24:24 +00:00
|
|
|
|
|
|
|
constexpr size_t buffer_size = 4096;
|
|
|
|
|
2022-02-27 22:12:33 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-10-02 05:24:24 +00:00
|
|
|
{
|
|
|
|
bool create = false;
|
|
|
|
bool extract = false;
|
|
|
|
bool list = false;
|
|
|
|
bool verbose = false;
|
2020-10-03 18:59:41 +00:00
|
|
|
bool gzip = false;
|
2022-02-17 22:35:11 +00:00
|
|
|
bool no_auto_compress = false;
|
|
|
|
StringView archive_file;
|
2022-01-02 20:42:25 +00:00
|
|
|
const char* directory = nullptr;
|
2020-10-02 05:24:24 +00:00
|
|
|
Vector<const char*> paths;
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_option(create, "Create archive", "create", 'c');
|
|
|
|
args_parser.add_option(extract, "Extract archive", "extract", 'x');
|
|
|
|
args_parser.add_option(list, "List contents", "list", 't');
|
|
|
|
args_parser.add_option(verbose, "Print paths", "verbose", 'v');
|
2022-01-02 20:42:25 +00:00
|
|
|
args_parser.add_option(gzip, "Compress or decompress file using gzip", "gzip", 'z');
|
2022-02-17 22:35:11 +00:00
|
|
|
args_parser.add_option(no_auto_compress, "Do not use the archive suffix to select the compression algorithm", "no-auto-compress", 0);
|
2022-01-02 20:42:25 +00:00
|
|
|
args_parser.add_option(directory, "Directory to extract to/create from", "directory", 'C', "DIRECTORY");
|
2020-10-02 05:24:24 +00:00
|
|
|
args_parser.add_option(archive_file, "Archive file", "file", 'f', "FILE");
|
|
|
|
args_parser.add_positional_argument(paths, "Paths", "PATHS", Core::ArgsParser::Required::No);
|
2022-02-27 22:12:33 +00:00
|
|
|
args_parser.parse(arguments);
|
2020-10-02 05:24:24 +00:00
|
|
|
|
|
|
|
if (create + extract + list != 1) {
|
2020-10-04 13:35:43 +00:00
|
|
|
warnln("exactly one of -c, -x, and -t can be used");
|
2020-10-02 05:24:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-02-17 22:35:11 +00:00
|
|
|
if (!no_auto_compress && !archive_file.is_empty()) {
|
|
|
|
if (archive_file.ends_with(".gz"sv) || archive_file.ends_with(".tgz"sv))
|
|
|
|
gzip = true;
|
|
|
|
}
|
|
|
|
|
2020-10-02 05:24:24 +00:00
|
|
|
if (list || extract) {
|
2020-12-22 22:37:11 +00:00
|
|
|
auto file = Core::File::standard_input();
|
2020-10-02 05:24:24 +00:00
|
|
|
|
2022-02-27 22:12:33 +00:00
|
|
|
if (!archive_file.is_empty())
|
|
|
|
file = TRY(Core::File::open(archive_file, Core::OpenMode::ReadOnly));
|
2020-10-03 18:59:41 +00:00
|
|
|
|
2022-02-27 22:12:33 +00:00
|
|
|
if (directory)
|
|
|
|
TRY(Core::System::chdir(directory));
|
2022-01-02 20:42:25 +00:00
|
|
|
|
2020-10-03 18:59:41 +00:00
|
|
|
Core::InputFileStream file_stream(file);
|
|
|
|
Compress::GzipDecompressor gzip_stream(file_stream);
|
|
|
|
|
|
|
|
InputStream& file_input_stream = file_stream;
|
|
|
|
InputStream& gzip_input_stream = gzip_stream;
|
2021-03-18 14:22:05 +00:00
|
|
|
Archive::TarInputStream tar_stream((gzip) ? gzip_input_stream : file_input_stream);
|
2022-02-27 22:12:33 +00:00
|
|
|
// FIXME: implement ErrorOr<TarInputStream>?
|
2020-10-03 18:59:41 +00:00
|
|
|
if (!tar_stream.valid()) {
|
2020-10-25 16:46:16 +00:00
|
|
|
warnln("the provided file is not a well-formatted ustar file");
|
2020-10-03 18:59:41 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2020-10-02 05:24:24 +00:00
|
|
|
for (; !tar_stream.finished(); tar_stream.advance()) {
|
2022-03-04 15:57:16 +00:00
|
|
|
const Archive::TarFileHeader& header = tar_stream.header();
|
|
|
|
|
|
|
|
LexicalPath path = LexicalPath(header.filename());
|
|
|
|
if (!header.prefix().is_empty())
|
|
|
|
path = path.prepend(header.prefix());
|
|
|
|
String filename = path.string();
|
|
|
|
|
2020-10-02 05:24:24 +00:00
|
|
|
if (list || verbose)
|
2022-03-04 15:57:16 +00:00
|
|
|
outln("{}", filename);
|
2020-10-02 05:24:24 +00:00
|
|
|
|
|
|
|
if (extract) {
|
2021-03-18 14:22:05 +00:00
|
|
|
Archive::TarFileStream file_stream = tar_stream.file_contents();
|
2020-10-02 05:24:24 +00:00
|
|
|
|
2022-03-04 15:57:16 +00:00
|
|
|
String absolute_path = Core::File::absolute_path(filename);
|
2021-11-21 18:56:48 +00:00
|
|
|
|
2020-10-02 05:24:24 +00:00
|
|
|
switch (header.type_flag()) {
|
2021-03-18 14:22:05 +00:00
|
|
|
case Archive::TarFileType::NormalFile:
|
|
|
|
case Archive::TarFileType::AlternateNormalFile: {
|
2021-11-09 23:27:01 +00:00
|
|
|
Core::File::ensure_parent_directories(absolute_path);
|
|
|
|
|
2022-02-27 22:12:33 +00:00
|
|
|
int fd = TRY(Core::System::open(absolute_path, O_CREAT | O_WRONLY, header.mode()));
|
2020-10-02 05:24:24 +00:00
|
|
|
|
|
|
|
Array<u8, buffer_size> buffer;
|
2022-03-05 11:55:45 +00:00
|
|
|
size_t bytes_read;
|
|
|
|
while ((bytes_read = file_stream.read(buffer)) > 0)
|
|
|
|
TRY(Core::System::write(fd, buffer.span().slice(0, bytes_read)));
|
2022-02-27 22:12:33 +00:00
|
|
|
|
|
|
|
TRY(Core::System::close(fd));
|
2020-10-02 05:24:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-11-22 20:17:24 +00:00
|
|
|
case Archive::TarFileType::SymLink: {
|
|
|
|
Core::File::ensure_parent_directories(absolute_path);
|
|
|
|
|
2022-02-27 22:12:33 +00:00
|
|
|
TRY(Core::System::symlink(header.link_name(), absolute_path));
|
2021-11-22 20:17:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-03-18 14:22:05 +00:00
|
|
|
case Archive::TarFileType::Directory: {
|
2021-11-09 23:27:01 +00:00
|
|
|
Core::File::ensure_parent_directories(absolute_path);
|
|
|
|
|
2022-02-27 22:12:33 +00:00
|
|
|
auto result_or_error = Core::System::mkdir(absolute_path, header.mode());
|
|
|
|
if (result_or_error.is_error() && result_or_error.error().code() != EEXIST)
|
|
|
|
return result_or_error.error();
|
2020-10-02 05:24:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-05-22 16:53:22 +00:00
|
|
|
case Archive::TarFileType::GlobalExtendedHeader: {
|
|
|
|
dbgln("ignoring global extended header: {}", header.filename());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Archive::TarFileType::ExtendedHeader: {
|
|
|
|
dbgln("ignoring extended header: {}", header.filename());
|
|
|
|
break;
|
|
|
|
}
|
2020-10-02 05:24:24 +00:00
|
|
|
default:
|
|
|
|
// FIXME: Implement other file types
|
2021-05-22 16:53:22 +00:00
|
|
|
warnln("file type '{}' of {} is not yet supported", (char)header.type_flag(), header.filename());
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-10-02 05:24:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-03 18:59:41 +00:00
|
|
|
file_stream.close();
|
2022-02-27 22:12:33 +00:00
|
|
|
|
2020-10-02 05:24:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-12 23:45:23 +00:00
|
|
|
if (create) {
|
|
|
|
if (paths.size() == 0) {
|
|
|
|
warnln("you must provide at least one path to be archived");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto file = Core::File::standard_output();
|
|
|
|
|
2022-02-27 22:12:33 +00:00
|
|
|
if (!archive_file.is_empty())
|
|
|
|
file = TRY(Core::File::open(archive_file, Core::OpenMode::WriteOnly));
|
2021-03-12 23:45:23 +00:00
|
|
|
|
2022-02-27 22:12:33 +00:00
|
|
|
if (directory)
|
|
|
|
TRY(Core::System::chdir(directory));
|
2022-01-02 20:42:25 +00:00
|
|
|
|
2021-03-12 23:45:23 +00:00
|
|
|
Core::OutputFileStream file_stream(file);
|
|
|
|
Compress::GzipCompressor gzip_stream(file_stream);
|
|
|
|
|
|
|
|
OutputStream& file_output_stream = file_stream;
|
|
|
|
OutputStream& gzip_output_stream = gzip_stream;
|
2021-03-18 14:22:05 +00:00
|
|
|
Archive::TarOutputStream tar_stream((gzip) ? gzip_output_stream : file_output_stream);
|
2021-03-12 23:45:23 +00:00
|
|
|
|
2022-02-27 22:12:33 +00:00
|
|
|
auto add_file = [&](String path) -> ErrorOr<void> {
|
2021-03-12 23:45:23 +00:00
|
|
|
auto file = Core::File::construct(path);
|
2021-05-12 09:26:43 +00:00
|
|
|
if (!file->open(Core::OpenMode::ReadOnly)) {
|
2021-03-12 23:45:23 +00:00
|
|
|
warnln("Failed to open {}: {}", path, file->error_string());
|
2022-02-27 22:12:33 +00:00
|
|
|
return {};
|
2021-03-12 23:45:23 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 22:12:33 +00:00
|
|
|
auto statbuf_or_error = Core::System::lstat(path);
|
|
|
|
if (statbuf_or_error.is_error())
|
|
|
|
return statbuf_or_error.error();
|
|
|
|
|
|
|
|
auto statbuf = statbuf_or_error.value();
|
2021-03-12 23:45:23 +00:00
|
|
|
auto canonicalized_path = LexicalPath::canonicalized_path(path);
|
|
|
|
tar_stream.add_file(canonicalized_path, statbuf.st_mode, file->read_all());
|
|
|
|
if (verbose)
|
|
|
|
outln("{}", canonicalized_path);
|
2022-02-27 22:12:33 +00:00
|
|
|
|
|
|
|
return {};
|
2021-03-12 23:45:23 +00:00
|
|
|
};
|
|
|
|
|
2022-02-27 22:12:33 +00:00
|
|
|
auto add_directory = [&](String path, auto handle_directory) -> ErrorOr<void> {
|
|
|
|
auto statbuf_or_error = Core::System::lstat(path);
|
|
|
|
if (statbuf_or_error.is_error())
|
|
|
|
return statbuf_or_error.error();
|
|
|
|
|
|
|
|
auto statbuf = statbuf_or_error.value();
|
2021-03-12 23:45:23 +00:00
|
|
|
auto canonicalized_path = LexicalPath::canonicalized_path(path);
|
|
|
|
tar_stream.add_directory(canonicalized_path, statbuf.st_mode);
|
|
|
|
if (verbose)
|
|
|
|
outln("{}", canonicalized_path);
|
|
|
|
|
|
|
|
Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir);
|
|
|
|
while (it.has_next()) {
|
|
|
|
auto child_path = it.next_full_path();
|
|
|
|
if (!Core::File::is_directory(child_path)) {
|
2022-02-27 22:12:33 +00:00
|
|
|
TRY(add_file(child_path));
|
2021-03-12 23:45:23 +00:00
|
|
|
} else {
|
2022-02-27 22:12:33 +00:00
|
|
|
TRY(handle_directory(child_path, handle_directory));
|
2021-03-12 23:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-27 22:12:33 +00:00
|
|
|
|
|
|
|
return {};
|
2021-03-12 23:45:23 +00:00
|
|
|
};
|
|
|
|
|
2021-04-29 09:28:01 +00:00
|
|
|
for (auto const& path : paths) {
|
2021-03-12 23:45:23 +00:00
|
|
|
if (Core::File::is_directory(path)) {
|
2022-02-27 22:12:33 +00:00
|
|
|
TRY(add_directory(path, add_directory));
|
2021-03-12 23:45:23 +00:00
|
|
|
} else {
|
2022-02-27 22:12:33 +00:00
|
|
|
TRY(add_file(path));
|
2021-03-12 23:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tar_stream.finish();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-10-02 05:24:24 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|