2020-05-04 13:08:16 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andrés Vieira <anvieiravazquez@gmail.com>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-04 13:08:16 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-05-15 07:34:40 -03:00
|
|
|
#include <AK/Assertions.h>
|
2020-07-13 04:15:07 -03:00
|
|
|
#include <AK/NumberFormat.h>
|
2022-02-14 05:15:26 -03:00
|
|
|
#include <AK/StringUtils.h>
|
2021-03-18 17:39:23 -03:00
|
|
|
#include <LibArchive/Zip.h>
|
|
|
|
|
#include <LibCompress/Deflate.h>
|
2020-05-04 13:08:16 -03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-04-13 07:59:17 -03:00
|
|
|
#include <LibCore/Directory.h>
|
2020-05-04 13:08:16 -03:00
|
|
|
#include <LibCore/File.h>
|
2021-11-23 07:32:25 -03:00
|
|
|
#include <LibCore/MappedFile.h>
|
2022-01-03 19:24:56 -03:00
|
|
|
#include <LibCore/System.h>
|
2020-05-04 13:08:16 -03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
2021-06-13 12:00:56 -03:00
|
|
|
static bool unpack_zip_member(Archive::ZipMember zip_member, bool quiet)
|
2020-05-04 13:08:16 -03:00
|
|
|
{
|
2021-03-18 17:39:23 -03:00
|
|
|
if (zip_member.is_directory) {
|
|
|
|
|
if (mkdir(zip_member.name.characters(), 0755) < 0) {
|
|
|
|
|
perror("mkdir");
|
2020-05-04 13:08:16 -03:00
|
|
|
return false;
|
|
|
|
|
}
|
2021-06-13 12:00:56 -03:00
|
|
|
if (!quiet)
|
|
|
|
|
outln(" extracting: {}", zip_member.name);
|
2021-03-18 17:39:23 -03:00
|
|
|
return true;
|
2020-05-04 13:08:16 -03:00
|
|
|
}
|
2022-04-22 06:07:18 -03:00
|
|
|
MUST(Core::Directory::create(LexicalPath(zip_member.name).parent(), Core::Directory::CreateDirectories::Yes));
|
2021-03-18 17:39:23 -03:00
|
|
|
auto new_file = Core::File::construct(zip_member.name);
|
2021-05-12 06:26:43 -03:00
|
|
|
if (!new_file->open(Core::OpenMode::WriteOnly)) {
|
2021-03-18 17:39:23 -03:00
|
|
|
warnln("Can't write file {}: {}", zip_member.name, new_file->error_string());
|
2020-05-04 13:08:16 -03:00
|
|
|
return false;
|
2021-03-18 17:39:23 -03:00
|
|
|
}
|
2020-05-04 13:08:16 -03:00
|
|
|
|
2021-06-13 12:00:56 -03:00
|
|
|
if (!quiet)
|
|
|
|
|
outln(" extracting: {}", zip_member.name);
|
2020-05-04 13:08:16 -03:00
|
|
|
|
2021-03-18 17:39:23 -03:00
|
|
|
// TODO: verify CRC32s match!
|
|
|
|
|
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 05:08:59 -03:00
|
|
|
return false;
|
|
|
|
|
}
|
2021-03-18 17:39:23 -03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Archive::ZipCompressionMethod::Deflate: {
|
|
|
|
|
auto decompressed_data = Compress::DeflateDecompressor::decompress_all(zip_member.compressed_data);
|
|
|
|
|
if (!decompressed_data.has_value()) {
|
|
|
|
|
warnln("Failed decompressing file {}", zip_member.name);
|
2020-07-13 05:08:59 -03:00
|
|
|
return false;
|
|
|
|
|
}
|
2021-03-18 17:39:23 -03:00
|
|
|
if (decompressed_data.value().size() != zip_member.uncompressed_size) {
|
|
|
|
|
warnln("Failed decompressing file {}", zip_member.name);
|
2020-07-13 05:08:59 -03:00
|
|
|
return false;
|
|
|
|
|
}
|
2021-03-18 17:39:23 -03: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 05:08:59 -03:00
|
|
|
return false;
|
|
|
|
|
}
|
2021-03-18 17:39:23 -03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!new_file->close()) {
|
|
|
|
|
warnln("Can't close file {}: {}", zip_member.name, new_file->error_string());
|
|
|
|
|
return false;
|
2020-05-04 13:08:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 19:24:56 -03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-05-04 13:08:16 -03:00
|
|
|
{
|
2022-04-01 14:58:27 -03:00
|
|
|
char const* path;
|
2021-06-13 12:00:56 -03:00
|
|
|
bool quiet { false };
|
2021-04-25 10:35:18 -03:00
|
|
|
String output_directory_path;
|
2022-02-14 05:15:26 -03:00
|
|
|
Vector<StringView> file_filters;
|
2020-05-04 13:08:16 -03:00
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2021-06-13 11:59:16 -03:00
|
|
|
args_parser.add_option(output_directory_path, "Directory to receive the archive content", "output-directory", 'd', "path");
|
2021-06-13 12:00:56 -03:00
|
|
|
args_parser.add_option(quiet, "Be less verbose", "quiet", 'q');
|
2020-05-04 13:08:16 -03:00
|
|
|
args_parser.add_positional_argument(path, "File to unzip", "path", Core::ArgsParser::Required::Yes);
|
2022-02-14 05:15:26 -03:00
|
|
|
args_parser.add_positional_argument(file_filters, "Files or filters in the archive to extract", "files", Core::ArgsParser::Required::No);
|
2022-01-03 19:24:56 -03:00
|
|
|
args_parser.parse(arguments);
|
2020-05-04 13:08:16 -03:00
|
|
|
|
2020-07-13 04:15:07 -03:00
|
|
|
String zip_file_path { path };
|
2020-05-04 13:08:16 -03:00
|
|
|
|
2022-01-03 19:24:56 -03:00
|
|
|
struct stat st = TRY(Core::System::stat(zip_file_path));
|
2020-07-13 04:15:07 -03: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 16:14:14 -03: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 04:15:07 -03:00
|
|
|
|
2021-06-13 12:00:56 -03:00
|
|
|
if (!quiet)
|
|
|
|
|
warnln("Archive: {}", zip_file_path);
|
2020-05-04 13:08:16 -03:00
|
|
|
|
2022-01-15 16:14:14 -03:00
|
|
|
auto zip_file = Archive::Zip::try_create(input_bytes);
|
2021-03-18 17:39:23 -03:00
|
|
|
if (!zip_file.has_value()) {
|
|
|
|
|
warnln("Invalid zip file {}", zip_file_path);
|
|
|
|
|
return 1;
|
2020-05-04 13:08:16 -03:00
|
|
|
}
|
|
|
|
|
|
2021-04-25 10:35:18 -03:00
|
|
|
if (!output_directory_path.is_null()) {
|
2022-04-13 07:59:17 -03:00
|
|
|
TRY(Core::Directory::create(output_directory_path, Core::Directory::CreateDirectories::Yes));
|
2022-01-03 19:24:56 -03:00
|
|
|
TRY(Core::System::chdir(output_directory_path));
|
2021-04-25 10:35:18 -03:00
|
|
|
}
|
|
|
|
|
|
2021-03-18 17:39:23 -03:00
|
|
|
auto success = zip_file->for_each_member([&](auto zip_member) {
|
2022-02-14 05:15:26 -03: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 14:32:29 -03:00
|
|
|
auto string_filter = filter.replace("_"sv, "?"sv, ReplaceMode::All);
|
2022-02-14 05:15:26 -03:00
|
|
|
if (zip_member.name.matches(string_filter, CaseSensitivity::CaseSensitive)) {
|
|
|
|
|
keep_file = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
keep_file = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (keep_file) {
|
|
|
|
|
if (!unpack_zip_member(zip_member, quiet))
|
|
|
|
|
return IterationDecision::Break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return IterationDecision::Continue;
|
2021-03-18 17:39:23 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return success ? 0 : 1;
|
2020-05-04 13:08:16 -03:00
|
|
|
}
|