2021-03-23 00:13:27 +03:00
|
|
|
/*
|
2021-04-22 23:40:43 +03:00
|
|
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
2021-03-23 00:13:27 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-03-23 00:13:27 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/LexicalPath.h>
|
|
|
|
#include <LibArchive/Zip.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-08 20:34:27 +03:00
|
|
|
#include <LibCore/DateTime.h>
|
2021-03-23 00:13:27 +03:00
|
|
|
#include <LibCore/DirIterator.h>
|
2023-02-09 05:02:46 +03:00
|
|
|
#include <LibCore/File.h>
|
2022-01-18 00:26:48 +03:00
|
|
|
#include <LibCore/System.h>
|
2023-03-21 18:35:30 +03:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2021-03-23 00:13:27 +03:00
|
|
|
|
2022-01-03 12:10:34 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-03-23 00:13:27 +03:00
|
|
|
{
|
2022-07-11 23:42:03 +03:00
|
|
|
StringView zip_path;
|
2021-11-27 00:32:37 +03:00
|
|
|
Vector<StringView> source_paths;
|
2021-03-23 00:13:27 +03:00
|
|
|
bool recurse = false;
|
|
|
|
bool force = false;
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_positional_argument(zip_path, "Zip file path", "zipfile", Core::ArgsParser::Required::Yes);
|
|
|
|
args_parser.add_positional_argument(source_paths, "Input files to be archived", "files", Core::ArgsParser::Required::Yes);
|
|
|
|
args_parser.add_option(recurse, "Travel the directory structure recursively", "recurse-paths", 'r');
|
|
|
|
args_parser.add_option(force, "Overwrite existing zip file", "force", 'f');
|
2022-01-03 12:10:34 +03:00
|
|
|
args_parser.parse(arguments);
|
2021-03-23 00:13:27 +03:00
|
|
|
|
2022-01-18 00:26:48 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath wpath cpath"));
|
|
|
|
|
|
|
|
auto cwd = TRY(Core::System::getcwd());
|
2022-07-11 20:32:29 +03:00
|
|
|
TRY(Core::System::unveil(LexicalPath::absolute_path(cwd, zip_path), "wc"sv));
|
2022-01-18 00:26:48 +03:00
|
|
|
for (auto const& source_path : source_paths) {
|
2022-07-11 20:32:29 +03:00
|
|
|
TRY(Core::System::unveil(LexicalPath::absolute_path(cwd, source_path), "r"sv));
|
2022-01-18 00:26:48 +03:00
|
|
|
}
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
|
|
|
|
2023-06-02 19:36:52 +03:00
|
|
|
if (FileSystem::exists(zip_path)) {
|
2021-03-23 00:13:27 +03:00
|
|
|
if (force) {
|
2023-06-02 19:36:52 +03:00
|
|
|
outln("{} already exists, overwriting...", zip_path);
|
2021-03-23 00:13:27 +03:00
|
|
|
} else {
|
2023-06-02 19:36:52 +03:00
|
|
|
warnln("{} already exists, aborting!", zip_path);
|
2021-03-23 00:13:27 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 19:36:52 +03:00
|
|
|
outln("Archive: {}", zip_path);
|
|
|
|
auto file_stream = TRY(Core::File::open(zip_path, Core::File::OpenMode::Write));
|
2022-12-25 17:33:30 +03:00
|
|
|
Archive::ZipOutputStream zip_stream(move(file_stream));
|
2021-03-23 00:13:27 +03:00
|
|
|
|
2023-06-02 19:36:52 +03:00
|
|
|
auto add_file = [&](StringView path) -> ErrorOr<void> {
|
2023-12-16 17:19:34 +03:00
|
|
|
auto canonicalized_path = TRY(String::from_byte_string(LexicalPath::canonicalized_path(path)));
|
2021-03-23 00:13:27 +03:00
|
|
|
|
2023-06-02 19:33:07 +03:00
|
|
|
auto file = TRY(Core::File::open(path, Core::File::OpenMode::Read));
|
2023-02-08 20:34:27 +03:00
|
|
|
auto stat = TRY(Core::System::fstat(file->fd()));
|
|
|
|
auto date = Core::DateTime::from_timestamp(stat.st_mtim.tv_sec);
|
|
|
|
|
2023-06-02 19:33:07 +03:00
|
|
|
auto information = TRY(zip_stream.add_member_from_stream(canonicalized_path, *file, date));
|
|
|
|
if (information.compression_ratio < 1.f) {
|
|
|
|
outln(" adding: {} (deflated {}%)", canonicalized_path, (int)(information.compression_ratio * 100));
|
2021-03-23 00:13:27 +03:00
|
|
|
} else {
|
2023-06-02 19:33:07 +03:00
|
|
|
outln(" adding: {} (stored)", canonicalized_path);
|
2021-03-23 00:13:27 +03:00
|
|
|
}
|
2023-06-02 19:33:07 +03:00
|
|
|
|
|
|
|
return {};
|
2021-03-23 00:13:27 +03:00
|
|
|
};
|
|
|
|
|
2023-06-02 19:36:52 +03:00
|
|
|
auto add_directory = [&](StringView path, auto handle_directory) -> ErrorOr<void> {
|
2023-06-02 19:33:07 +03:00
|
|
|
auto canonicalized_path = TRY(String::formatted("{}/", LexicalPath::canonicalized_path(path)));
|
2023-02-08 20:34:27 +03:00
|
|
|
|
2023-06-02 19:33:07 +03:00
|
|
|
auto stat = TRY(Core::System::stat(path));
|
2023-02-08 20:34:27 +03:00
|
|
|
auto date = Core::DateTime::from_timestamp(stat.st_mtim.tv_sec);
|
2023-06-02 19:33:07 +03:00
|
|
|
TRY(zip_stream.add_directory(canonicalized_path, date));
|
2021-03-23 00:13:27 +03:00
|
|
|
|
|
|
|
if (!recurse)
|
2022-12-25 17:33:30 +03:00
|
|
|
return {};
|
2021-03-23 00:13:27 +03:00
|
|
|
|
|
|
|
Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir);
|
|
|
|
while (it.has_next()) {
|
|
|
|
auto child_path = it.next_full_path();
|
2023-03-21 18:35:30 +03:00
|
|
|
if (FileSystem::is_link(child_path))
|
2022-12-25 17:33:30 +03:00
|
|
|
return {};
|
2023-03-21 18:35:30 +03:00
|
|
|
if (!FileSystem::is_directory(child_path)) {
|
2022-12-25 18:14:05 +03:00
|
|
|
auto result = add_file(child_path);
|
|
|
|
if (result.is_error())
|
|
|
|
warnln("Couldn't add file '{}': {}", child_path, result.error());
|
|
|
|
} else {
|
2022-12-25 17:33:30 +03:00
|
|
|
auto result = handle_directory(child_path, handle_directory);
|
|
|
|
if (result.is_error())
|
|
|
|
warnln("Couldn't add directory '{}': {}", child_path, result.error());
|
2022-12-25 18:14:05 +03:00
|
|
|
}
|
2021-03-23 00:13:27 +03:00
|
|
|
}
|
2022-12-25 17:33:30 +03:00
|
|
|
return {};
|
2021-03-23 00:13:27 +03:00
|
|
|
};
|
|
|
|
|
2021-04-29 12:28:01 +03:00
|
|
|
for (auto const& source_path : source_paths) {
|
2023-03-21 18:35:30 +03:00
|
|
|
if (FileSystem::is_directory(source_path)) {
|
2022-12-25 17:33:30 +03:00
|
|
|
auto result = add_directory(source_path, add_directory);
|
|
|
|
if (result.is_error())
|
|
|
|
warnln("Couldn't add directory '{}': {}", source_path, result.error());
|
2021-03-23 00:13:27 +03:00
|
|
|
} else {
|
2022-12-25 18:14:05 +03:00
|
|
|
auto result = add_file(source_path);
|
|
|
|
if (result.is_error())
|
|
|
|
warnln("Couldn't add file '{}': {}", source_path, result.error());
|
2021-03-23 00:13:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-25 17:33:30 +03:00
|
|
|
TRY(zip_stream.finish());
|
2021-03-23 00:13:27 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|