2020-05-04 16:08:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andrés Vieira <anvieiravazquez@gmail.com>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-04 16:08:16 +00:00
|
|
|
*/
|
|
|
|
|
2021-05-15 10:34:40 +00:00
|
|
|
#include <AK/Assertions.h>
|
2023-02-08 17:34:27 +00:00
|
|
|
#include <AK/DOSPackedTime.h>
|
2020-07-13 07:15:07 +00:00
|
|
|
#include <AK/NumberFormat.h>
|
2022-02-14 08:15:26 +00:00
|
|
|
#include <AK/StringUtils.h>
|
2021-03-18 20:39:23 +00:00
|
|
|
#include <LibArchive/Zip.h>
|
|
|
|
#include <LibCompress/Deflate.h>
|
2020-05-04 16:08:16 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-08 20:08:01 +00:00
|
|
|
#include <LibCore/DeprecatedFile.h>
|
2022-04-13 10:59:17 +00:00
|
|
|
#include <LibCore/Directory.h>
|
2021-11-23 10:32:25 +00:00
|
|
|
#include <LibCore/MappedFile.h>
|
2022-01-03 22:24:56 +00:00
|
|
|
#include <LibCore/System.h>
|
2022-12-18 10:48:20 +00:00
|
|
|
#include <LibCrypto/Checksum/CRC32.h>
|
2020-05-04 16:08:16 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2023-02-08 17:34:27 +00:00
|
|
|
static ErrorOr<void> adjust_modification_time(Archive::ZipMember const& zip_member)
|
|
|
|
{
|
|
|
|
auto time = time_from_packed_dos(zip_member.modification_date, zip_member.modification_time);
|
|
|
|
auto seconds = static_cast<time_t>(time.to_seconds());
|
|
|
|
struct utimbuf buf {
|
|
|
|
.actime = seconds,
|
|
|
|
.modtime = seconds
|
|
|
|
};
|
|
|
|
|
|
|
|
return Core::System::utime(zip_member.name, buf);
|
|
|
|
}
|
|
|
|
|
2021-06-13 15:00:56 +00:00
|
|
|
static bool unpack_zip_member(Archive::ZipMember zip_member, bool quiet)
|
2020-05-04 16:08:16 +00:00
|
|
|
{
|
2021-03-18 20:39:23 +00:00
|
|
|
if (zip_member.is_directory) {
|
2022-12-22 14:21:13 +00:00
|
|
|
if (auto maybe_error = Core::System::mkdir(zip_member.name, 0755); maybe_error.is_error()) {
|
|
|
|
warnln("Failed to create directory '{}': {}", zip_member.name, maybe_error.error());
|
2020-05-04 16:08:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-06-13 15:00:56 +00:00
|
|
|
if (!quiet)
|
|
|
|
outln(" extracting: {}", zip_member.name);
|
2021-03-18 20:39:23 +00:00
|
|
|
return true;
|
2020-05-04 16:08:16 +00:00
|
|
|
}
|
2022-12-22 14:21:13 +00:00
|
|
|
MUST(Core::Directory::create(LexicalPath(zip_member.name.to_deprecated_string()).parent(), Core::Directory::CreateDirectories::Yes));
|
2023-02-08 20:08:01 +00:00
|
|
|
auto new_file = Core::DeprecatedFile::construct(zip_member.name.to_deprecated_string());
|
2021-05-12 09:26:43 +00:00
|
|
|
if (!new_file->open(Core::OpenMode::WriteOnly)) {
|
2021-03-18 20:39:23 +00:00
|
|
|
warnln("Can't write file {}: {}", zip_member.name, new_file->error_string());
|
2020-05-04 16:08:16 +00:00
|
|
|
return false;
|
2021-03-18 20:39:23 +00:00
|
|
|
}
|
2020-05-04 16:08:16 +00:00
|
|
|
|
2021-06-13 15:00:56 +00:00
|
|
|
if (!quiet)
|
|
|
|
outln(" extracting: {}", zip_member.name);
|
2020-05-04 16:08:16 +00:00
|
|
|
|
2022-12-18 10:48:20 +00:00
|
|
|
Crypto::Checksum::CRC32 checksum;
|
2021-03-18 20:39:23 +00:00
|
|
|
switch (zip_member.compression_method) {
|
|
|
|
case Archive::ZipCompressionMethod::Store: {
|
|
|
|
if (!new_file->write(zip_member.compressed_data.data(), zip_member.compressed_data.size())) {
|
|
|
|
warnln("Can't write file contents in {}: {}", zip_member.name, new_file->error_string());
|
2020-07-13 08:08:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-12-18 10:48:20 +00:00
|
|
|
checksum.update({ zip_member.compressed_data.data(), zip_member.compressed_data.size() });
|
2021-03-18 20:39:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Archive::ZipCompressionMethod::Deflate: {
|
|
|
|
auto decompressed_data = Compress::DeflateDecompressor::decompress_all(zip_member.compressed_data);
|
2022-12-02 21:01:44 +00:00
|
|
|
if (decompressed_data.is_error()) {
|
|
|
|
warnln("Failed decompressing file {}: {}", zip_member.name, decompressed_data.error());
|
2020-07-13 08:08:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-18 20:39:23 +00:00
|
|
|
if (decompressed_data.value().size() != zip_member.uncompressed_size) {
|
|
|
|
warnln("Failed decompressing file {}", zip_member.name);
|
2020-07-13 08:08:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-18 20:39:23 +00:00
|
|
|
if (!new_file->write(decompressed_data.value().data(), decompressed_data.value().size())) {
|
|
|
|
warnln("Can't write file contents in {}: {}", zip_member.name, new_file->error_string());
|
2020-07-13 08:08:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-12-18 10:48:20 +00:00
|
|
|
checksum.update({ decompressed_data.value().data(), decompressed_data.value().size() });
|
2021-03-18 20:39:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2023-02-08 17:34:27 +00:00
|
|
|
if (adjust_modification_time(zip_member).is_error()) {
|
|
|
|
warnln("Failed setting modification_time for file {}", zip_member.name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-18 20:39:23 +00:00
|
|
|
if (!new_file->close()) {
|
|
|
|
warnln("Can't close file {}: {}", zip_member.name, new_file->error_string());
|
|
|
|
return false;
|
2020-05-04 16:08:16 +00:00
|
|
|
}
|
|
|
|
|
2022-12-18 10:48:20 +00:00
|
|
|
if (checksum.digest() != zip_member.crc32) {
|
|
|
|
warnln("Failed decompressing file {}: CRC32 mismatch", zip_member.name);
|
2023-02-08 20:08:01 +00:00
|
|
|
MUST(Core::DeprecatedFile::remove(zip_member.name, Core::DeprecatedFile::RecursionMode::Disallowed));
|
2022-12-18 10:48:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-04 16:08:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-03 22:24:56 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-05-04 16:08:16 +00:00
|
|
|
{
|
2022-12-18 10:59:02 +00:00
|
|
|
StringView zip_file_path;
|
2021-06-13 15:00:56 +00:00
|
|
|
bool quiet { false };
|
2022-12-18 10:59:02 +00:00
|
|
|
StringView output_directory_path;
|
2022-02-14 08:15:26 +00:00
|
|
|
Vector<StringView> file_filters;
|
2020-05-04 16:08:16 +00:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2021-06-13 14:59:16 +00:00
|
|
|
args_parser.add_option(output_directory_path, "Directory to receive the archive content", "output-directory", 'd', "path");
|
2021-06-13 15:00:56 +00:00
|
|
|
args_parser.add_option(quiet, "Be less verbose", "quiet", 'q');
|
2022-12-18 10:59:02 +00:00
|
|
|
args_parser.add_positional_argument(zip_file_path, "File to unzip", "path", Core::ArgsParser::Required::Yes);
|
2022-02-14 08:15:26 +00:00
|
|
|
args_parser.add_positional_argument(file_filters, "Files or filters in the archive to extract", "files", Core::ArgsParser::Required::No);
|
2022-01-03 22:24:56 +00:00
|
|
|
args_parser.parse(arguments);
|
2020-05-04 16:08:16 +00:00
|
|
|
|
2022-01-03 22:24:56 +00:00
|
|
|
struct stat st = TRY(Core::System::stat(zip_file_path));
|
2020-07-13 07:15:07 +00:00
|
|
|
|
|
|
|
// FIXME: Map file chunk-by-chunk once we have mmap() with offset.
|
|
|
|
// This will require mapping some parts then unmapping them repeatedly,
|
|
|
|
// but it would be significantly faster and less syscall heavy than seek()/read() at every read.
|
2022-01-15 19:14:14 +00:00
|
|
|
RefPtr<Core::MappedFile> mapped_file;
|
|
|
|
ReadonlyBytes input_bytes;
|
|
|
|
if (st.st_size > 0) {
|
|
|
|
mapped_file = TRY(Core::MappedFile::map(zip_file_path));
|
|
|
|
input_bytes = mapped_file->bytes();
|
|
|
|
}
|
2020-07-13 07:15:07 +00:00
|
|
|
|
2021-06-13 15:00:56 +00:00
|
|
|
if (!quiet)
|
|
|
|
warnln("Archive: {}", zip_file_path);
|
2020-05-04 16:08:16 +00:00
|
|
|
|
2022-01-15 19:14:14 +00:00
|
|
|
auto zip_file = Archive::Zip::try_create(input_bytes);
|
2021-03-18 20:39:23 +00:00
|
|
|
if (!zip_file.has_value()) {
|
|
|
|
warnln("Invalid zip file {}", zip_file_path);
|
|
|
|
return 1;
|
2020-05-04 16:08:16 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 13:35:18 +00:00
|
|
|
if (!output_directory_path.is_null()) {
|
2022-04-13 10:59:17 +00:00
|
|
|
TRY(Core::Directory::create(output_directory_path, Core::Directory::CreateDirectories::Yes));
|
2022-01-03 22:24:56 +00:00
|
|
|
TRY(Core::System::chdir(output_directory_path));
|
2021-04-25 13:35:18 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 17:34:27 +00:00
|
|
|
Vector<Archive::ZipMember> zip_directories;
|
|
|
|
|
2022-12-22 14:21:13 +00:00
|
|
|
auto success = TRY(zip_file->for_each_member([&](auto zip_member) {
|
2022-02-14 08:15:26 +00:00
|
|
|
bool keep_file = false;
|
|
|
|
|
|
|
|
if (!file_filters.is_empty()) {
|
|
|
|
for (auto& filter : file_filters) {
|
|
|
|
// Convert underscore wildcards (usual unzip convention) to question marks (as used by StringUtils)
|
2022-07-11 17:32:29 +00:00
|
|
|
auto string_filter = filter.replace("_"sv, "?"sv, ReplaceMode::All);
|
2022-12-22 14:21:13 +00:00
|
|
|
if (zip_member.name.bytes_as_string_view().matches(string_filter, CaseSensitivity::CaseSensitive)) {
|
2022-02-14 08:15:26 +00:00
|
|
|
keep_file = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
keep_file = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keep_file) {
|
|
|
|
if (!unpack_zip_member(zip_member, quiet))
|
|
|
|
return IterationDecision::Break;
|
2023-02-08 17:34:27 +00:00
|
|
|
if (zip_member.is_directory)
|
|
|
|
zip_directories.append(zip_member);
|
2022-02-14 08:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return IterationDecision::Continue;
|
2022-12-22 14:21:13 +00:00
|
|
|
}));
|
2021-03-18 20:39:23 +00:00
|
|
|
|
2023-02-08 17:34:27 +00:00
|
|
|
if (!success) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& directory : zip_directories) {
|
|
|
|
if (adjust_modification_time(directory).is_error()) {
|
|
|
|
warnln("Failed setting modification time for directory {}", directory.name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 20:39:23 +00:00
|
|
|
return success ? 0 : 1;
|
2020-05-04 16:08:16 +00:00
|
|
|
}
|