2021-03-22 21:13:27 +00:00
|
|
|
/*
|
2021-04-22 20:40:43 +00:00
|
|
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
2021-03-22 21:13:27 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-03-22 21:13:27 +00:00
|
|
|
*/
|
|
|
|
|
2023-02-08 17:34:27 +00:00
|
|
|
#include <AK/DOSPackedTime.h>
|
2021-03-22 21:13:27 +00:00
|
|
|
#include <AK/LexicalPath.h>
|
|
|
|
#include <LibArchive/Zip.h>
|
|
|
|
#include <LibCompress/Deflate.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-08 17:34:27 +00:00
|
|
|
#include <LibCore/DateTime.h>
|
2021-03-22 21:13:27 +00:00
|
|
|
#include <LibCore/DirIterator.h>
|
2023-02-09 02:02:46 +00:00
|
|
|
#include <LibCore/File.h>
|
2022-01-17 21:26:48 +00:00
|
|
|
#include <LibCore/System.h>
|
2021-03-22 21:13:27 +00:00
|
|
|
#include <LibCrypto/Checksum/CRC32.h>
|
2023-03-21 15:35:30 +00:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2021-03-22 21:13:27 +00:00
|
|
|
|
2022-01-03 09:10:34 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-03-22 21:13:27 +00:00
|
|
|
{
|
2022-07-11 20:42:03 +00:00
|
|
|
StringView zip_path;
|
2021-11-26 21:32:37 +00:00
|
|
|
Vector<StringView> source_paths;
|
2021-03-22 21:13:27 +00: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 09:10:34 +00:00
|
|
|
args_parser.parse(arguments);
|
2021-03-22 21:13:27 +00:00
|
|
|
|
2022-01-17 21:26:48 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath wpath cpath"));
|
|
|
|
|
|
|
|
auto cwd = TRY(Core::System::getcwd());
|
2022-07-11 17:32:29 +00:00
|
|
|
TRY(Core::System::unveil(LexicalPath::absolute_path(cwd, zip_path), "wc"sv));
|
2022-01-17 21:26:48 +00:00
|
|
|
for (auto const& source_path : source_paths) {
|
2022-07-11 17:32:29 +00:00
|
|
|
TRY(Core::System::unveil(LexicalPath::absolute_path(cwd, source_path), "r"sv));
|
2022-01-17 21:26:48 +00:00
|
|
|
}
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString zip_file_path { zip_path };
|
2023-03-21 15:35:30 +00:00
|
|
|
if (FileSystem::exists(zip_file_path)) {
|
2021-03-22 21:13:27 +00:00
|
|
|
if (force) {
|
|
|
|
outln("{} already exists, overwriting...", zip_file_path);
|
|
|
|
} else {
|
|
|
|
warnln("{} already exists, aborting!", zip_file_path);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
outln("Archive: {}", zip_file_path);
|
2023-02-09 02:02:46 +00:00
|
|
|
auto file_stream = TRY(Core::File::open(zip_file_path, Core::File::OpenMode::Write));
|
2022-12-25 14:33:30 +00:00
|
|
|
Archive::ZipOutputStream zip_stream(move(file_stream));
|
2021-03-22 21:13:27 +00:00
|
|
|
|
2022-12-25 15:14:05 +00:00
|
|
|
auto add_file = [&](DeprecatedString path) -> ErrorOr<void> {
|
2021-03-22 21:13:27 +00:00
|
|
|
auto canonicalized_path = LexicalPath::canonicalized_path(path);
|
2023-02-09 02:02:46 +00:00
|
|
|
auto file = TRY(Core::File::open(path, Core::File::OpenMode::Read));
|
2022-12-25 15:14:05 +00:00
|
|
|
auto file_buffer = TRY(file->read_until_eof());
|
2021-03-22 21:13:27 +00:00
|
|
|
Archive::ZipMember member {};
|
2022-12-22 14:21:13 +00:00
|
|
|
member.name = TRY(String::from_deprecated_string(canonicalized_path));
|
2021-03-22 21:13:27 +00:00
|
|
|
|
2023-02-08 17:34:27 +00:00
|
|
|
auto stat = TRY(Core::System::fstat(file->fd()));
|
|
|
|
auto date = Core::DateTime::from_timestamp(stat.st_mtim.tv_sec);
|
|
|
|
member.modification_date = to_packed_dos_date(date.year(), date.month(), date.day());
|
|
|
|
member.modification_time = to_packed_dos_time(date.hour(), date.minute(), date.second());
|
|
|
|
|
2021-03-22 21:13:27 +00:00
|
|
|
auto deflate_buffer = Compress::DeflateCompressor::compress_all(file_buffer);
|
2022-12-27 12:49:42 +00:00
|
|
|
if (!deflate_buffer.is_error() && deflate_buffer.value().size() < file_buffer.size()) {
|
2021-03-22 21:13:27 +00:00
|
|
|
member.compressed_data = deflate_buffer.value().bytes();
|
|
|
|
member.compression_method = Archive::ZipCompressionMethod::Deflate;
|
|
|
|
auto compression_ratio = (double)deflate_buffer.value().size() / file_buffer.size();
|
|
|
|
outln(" adding: {} (deflated {}%)", canonicalized_path, (int)(compression_ratio * 100));
|
|
|
|
} else {
|
|
|
|
member.compressed_data = file_buffer.bytes();
|
|
|
|
member.compression_method = Archive::ZipCompressionMethod::Store;
|
|
|
|
outln(" adding: {} (stored 0%)", canonicalized_path);
|
|
|
|
}
|
|
|
|
member.uncompressed_size = file_buffer.size();
|
|
|
|
Crypto::Checksum::CRC32 checksum { file_buffer.bytes() };
|
|
|
|
member.crc32 = checksum.digest();
|
|
|
|
member.is_directory = false;
|
2022-12-25 14:33:30 +00:00
|
|
|
return zip_stream.add_member(member);
|
2021-03-22 21:13:27 +00:00
|
|
|
};
|
|
|
|
|
2022-12-25 14:33:30 +00:00
|
|
|
auto add_directory = [&](DeprecatedString path, auto handle_directory) -> ErrorOr<void> {
|
2022-12-04 18:02:33 +00:00
|
|
|
auto canonicalized_path = DeprecatedString::formatted("{}/", LexicalPath::canonicalized_path(path));
|
2021-03-22 21:13:27 +00:00
|
|
|
Archive::ZipMember member {};
|
2022-12-22 14:21:13 +00:00
|
|
|
member.name = TRY(String::from_deprecated_string(canonicalized_path));
|
2021-03-22 21:13:27 +00:00
|
|
|
member.compressed_data = {};
|
|
|
|
member.compression_method = Archive::ZipCompressionMethod::Store;
|
|
|
|
member.uncompressed_size = 0;
|
|
|
|
member.crc32 = 0;
|
|
|
|
member.is_directory = true;
|
2023-02-08 17:34:27 +00:00
|
|
|
|
|
|
|
auto stat = TRY(Core::System::stat(canonicalized_path));
|
|
|
|
auto date = Core::DateTime::from_timestamp(stat.st_mtim.tv_sec);
|
|
|
|
member.modification_date = to_packed_dos_date(date.year(), date.month(), date.day());
|
|
|
|
member.modification_time = to_packed_dos_time(date.hour(), date.minute(), date.second());
|
|
|
|
|
2022-12-25 14:33:30 +00:00
|
|
|
TRY(zip_stream.add_member(member));
|
2021-03-22 21:13:27 +00:00
|
|
|
outln(" adding: {} (stored 0%)", canonicalized_path);
|
|
|
|
|
|
|
|
if (!recurse)
|
2022-12-25 14:33:30 +00:00
|
|
|
return {};
|
2021-03-22 21:13:27 +00:00
|
|
|
|
|
|
|
Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir);
|
|
|
|
while (it.has_next()) {
|
|
|
|
auto child_path = it.next_full_path();
|
2023-03-21 15:35:30 +00:00
|
|
|
if (FileSystem::is_link(child_path))
|
2022-12-25 14:33:30 +00:00
|
|
|
return {};
|
2023-03-21 15:35:30 +00:00
|
|
|
if (!FileSystem::is_directory(child_path)) {
|
2022-12-25 15:14:05 +00:00
|
|
|
auto result = add_file(child_path);
|
|
|
|
if (result.is_error())
|
|
|
|
warnln("Couldn't add file '{}': {}", child_path, result.error());
|
|
|
|
} else {
|
2022-12-25 14:33:30 +00: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 15:14:05 +00:00
|
|
|
}
|
2021-03-22 21:13:27 +00:00
|
|
|
}
|
2022-12-25 14:33:30 +00:00
|
|
|
return {};
|
2021-03-22 21:13:27 +00:00
|
|
|
};
|
|
|
|
|
2021-04-29 09:28:01 +00:00
|
|
|
for (auto const& source_path : source_paths) {
|
2023-03-21 15:35:30 +00:00
|
|
|
if (FileSystem::is_directory(source_path)) {
|
2022-12-25 14:33:30 +00:00
|
|
|
auto result = add_directory(source_path, add_directory);
|
|
|
|
if (result.is_error())
|
|
|
|
warnln("Couldn't add directory '{}': {}", source_path, result.error());
|
2021-03-22 21:13:27 +00:00
|
|
|
} else {
|
2022-12-25 15:14:05 +00:00
|
|
|
auto result = add_file(source_path);
|
|
|
|
if (result.is_error())
|
|
|
|
warnln("Couldn't add file '{}': {}", source_path, result.error());
|
2021-03-22 21:13:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-25 14:33:30 +00:00
|
|
|
TRY(zip_stream.finish());
|
2021-03-22 21:13:27 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|