Utilities: Remove zip and unzip

They are not even being compiled
This commit is contained in:
circl 2024-06-23 14:40:26 +02:00 committed by Andrew Kaster
parent 84ed7e0a63
commit e5018d2a7a
Notes: sideshowbarker 2024-07-17 08:38:37 +09:00
3 changed files with 0 additions and 317 deletions

2
.github/CODEOWNERS vendored
View file

@ -24,7 +24,5 @@
/Userland/Utilities/gzip.cpp @timschumi
/Userland/Utilities/lzcat.cpp @timschumi
/Userland/Utilities/tar.cpp @timschumi
/Userland/Utilities/unzip.cpp @timschumi
/Userland/Utilities/wasm.cpp @alimpfard
/Userland/Utilities/xzcat.cpp @timschumi
/Userland/Utilities/zip.cpp @timschumi

View file

@ -1,203 +0,0 @@
/*
* Copyright (c) 2020, Andrés Vieira <anvieiravazquez@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Assertions.h>
#include <AK/DOSPackedTime.h>
#include <AK/NumberFormat.h>
#include <AK/StringUtils.h>
#include <LibArchive/Zip.h>
#include <LibCompress/Deflate.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/DateTime.h>
#include <LibCore/Directory.h>
#include <LibCore/File.h>
#include <LibCore/MappedFile.h>
#include <LibCore/System.h>
#include <LibCrypto/Checksum/CRC32.h>
#include <LibFileSystem/FileSystem.h>
#include <sys/stat.h>
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.seconds_since_epoch());
struct utimbuf buf {
.actime = seconds,
.modtime = seconds
};
return Core::System::utime(zip_member.name, buf);
}
static bool unpack_zip_member(Archive::ZipMember zip_member, bool quiet)
{
if (zip_member.is_directory) {
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());
return false;
}
if (!quiet)
outln(" extracting: {}", zip_member.name);
return true;
}
MUST(Core::Directory::create(LexicalPath(zip_member.name.to_byte_string()).parent(), Core::Directory::CreateDirectories::Yes));
auto new_file_or_error = Core::File::open(zip_member.name.to_byte_string(), Core::File::OpenMode::Write);
if (new_file_or_error.is_error()) {
warnln("Can't write file {}: {}", zip_member.name, new_file_or_error.release_error());
return false;
}
auto new_file = new_file_or_error.release_value();
if (!quiet)
outln(" extracting: {}", zip_member.name);
Crypto::Checksum::CRC32 checksum;
switch (zip_member.compression_method) {
case Archive::ZipCompressionMethod::Store: {
if (auto maybe_error = new_file->write_until_depleted(zip_member.compressed_data); maybe_error.is_error()) {
warnln("Can't write file contents in {}: {}", zip_member.name, maybe_error.release_error());
return false;
}
checksum.update({ zip_member.compressed_data.data(), zip_member.compressed_data.size() });
break;
}
case Archive::ZipCompressionMethod::Deflate: {
auto decompressed_data = Compress::DeflateDecompressor::decompress_all(zip_member.compressed_data);
if (decompressed_data.is_error()) {
warnln("Failed decompressing file {}: {}", zip_member.name, decompressed_data.error());
return false;
}
if (decompressed_data.value().size() != zip_member.uncompressed_size) {
warnln("Failed decompressing file {}", zip_member.name);
return false;
}
if (auto maybe_error = new_file->write_until_depleted(decompressed_data.value()); maybe_error.is_error()) {
warnln("Can't write file contents in {}: {}", zip_member.name, maybe_error.release_error());
return false;
}
checksum.update(decompressed_data.value());
break;
}
default:
VERIFY_NOT_REACHED();
}
if (adjust_modification_time(zip_member).is_error()) {
warnln("Failed setting modification_time for file {}", zip_member.name);
return false;
}
new_file->close();
if (checksum.digest() != zip_member.crc32) {
warnln("Failed decompressing file {}: CRC32 mismatch", zip_member.name);
MUST(FileSystem::remove(zip_member.name, FileSystem::RecursionMode::Disallowed));
return false;
}
return true;
}
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
StringView zip_file_path;
bool quiet { false };
bool list_files { false };
StringView output_directory_path;
Vector<StringView> file_filters;
Core::ArgsParser args_parser;
args_parser.add_option(list_files, "Only list files in the archive", "list", 'l');
args_parser.add_option(output_directory_path, "Directory to receive the archive content", "output-directory", 'd', "path");
args_parser.add_option(quiet, "Be less verbose", "quiet", 'q');
args_parser.add_positional_argument(zip_file_path, "File to unzip", "path", Core::ArgsParser::Required::Yes);
args_parser.add_positional_argument(file_filters, "Files or filters in the archive to extract", "files", Core::ArgsParser::Required::No);
args_parser.parse(arguments);
struct stat st = TRY(Core::System::stat(zip_file_path));
// 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.
OwnPtr<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();
}
if (!quiet)
warnln("Archive: {}", zip_file_path);
auto zip_file = Archive::Zip::try_create(input_bytes);
if (!zip_file.has_value()) {
warnln("Invalid zip file {}", zip_file_path);
return 1;
}
if (!output_directory_path.is_null()) {
TRY(Core::Directory::create(output_directory_path, Core::Directory::CreateDirectories::Yes));
TRY(Core::System::chdir(output_directory_path));
}
if (list_files) {
outln(" Length Date Time Name");
outln("--------- ---------- -------- ----");
TRY(zip_file->for_each_member([&](auto zip_member) -> ErrorOr<IterationDecision> {
auto time = time_from_packed_dos(zip_member.modification_date, zip_member.modification_time);
auto time_str = TRY(Core::DateTime::from_timestamp(time.seconds_since_epoch()).to_string());
outln("{:>9} {} {}", zip_member.uncompressed_size, time_str, zip_member.name);
return IterationDecision::Continue;
}));
auto statistics = TRY(zip_file->calculate_statistics());
outln("--------- ----");
outln("{:>9} {} files", statistics.total_uncompressed_bytes(), statistics.member_count());
return 0;
}
Vector<Archive::ZipMember> zip_directories;
auto success = TRY(zip_file->for_each_member([&](auto zip_member) {
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)
auto string_filter = filter.replace("_"sv, "?"sv, ReplaceMode::All);
if (zip_member.name.bytes_as_string_view().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;
if (zip_member.is_directory)
zip_directories.append(zip_member);
}
return IterationDecision::Continue;
}));
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;
}
}
return success ? 0 : 1;
}

View file

@ -1,112 +0,0 @@
/*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/LexicalPath.h>
#include <LibArchive/Zip.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/DateTime.h>
#include <LibCore/DirIterator.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibFileSystem/FileSystem.h>
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
StringView zip_path;
Vector<StringView> source_paths;
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');
args_parser.parse(arguments);
TRY(Core::System::pledge("stdio rpath wpath cpath"));
auto cwd = TRY(Core::System::getcwd());
TRY(Core::System::unveil(LexicalPath::absolute_path(cwd, zip_path), "wc"sv));
for (auto const& source_path : source_paths) {
TRY(Core::System::unveil(LexicalPath::absolute_path(cwd, source_path), "r"sv));
}
TRY(Core::System::unveil(nullptr, nullptr));
if (FileSystem::exists(zip_path)) {
if (force) {
outln("{} already exists, overwriting...", zip_path);
} else {
warnln("{} already exists, aborting!", zip_path);
return 1;
}
}
outln("Archive: {}", zip_path);
auto file_stream = TRY(Core::File::open(zip_path, Core::File::OpenMode::Write));
Archive::ZipOutputStream zip_stream(move(file_stream));
auto add_file = [&](StringView path) -> ErrorOr<void> {
auto canonicalized_path = TRY(String::from_byte_string(LexicalPath::canonicalized_path(path)));
auto file = TRY(Core::File::open(path, Core::File::OpenMode::Read));
auto stat = TRY(Core::System::fstat(file->fd()));
auto date = Core::DateTime::from_timestamp(stat.st_mtim.tv_sec);
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));
} else {
outln(" adding: {} (stored)", canonicalized_path);
}
return {};
};
auto add_directory = [&](StringView path, auto handle_directory) -> ErrorOr<void> {
auto canonicalized_path = TRY(String::formatted("{}/", LexicalPath::canonicalized_path(path)));
auto stat = TRY(Core::System::stat(path));
auto date = Core::DateTime::from_timestamp(stat.st_mtim.tv_sec);
TRY(zip_stream.add_directory(canonicalized_path, date));
if (!recurse)
return {};
Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir);
while (it.has_next()) {
auto child_path = it.next_full_path();
if (FileSystem::is_link(child_path))
return {};
if (!FileSystem::is_directory(child_path)) {
auto result = add_file(child_path);
if (result.is_error())
warnln("Couldn't add file '{}': {}", child_path, result.error());
} else {
auto result = handle_directory(child_path, handle_directory);
if (result.is_error())
warnln("Couldn't add directory '{}': {}", child_path, result.error());
}
}
return {};
};
for (auto const& source_path : source_paths) {
if (FileSystem::is_directory(source_path)) {
auto result = add_directory(source_path, add_directory);
if (result.is_error())
warnln("Couldn't add directory '{}': {}", source_path, result.error());
} else {
auto result = add_file(source_path);
if (result.is_error())
warnln("Couldn't add file '{}': {}", source_path, result.error());
}
}
TRY(zip_stream.finish());
return 0;
}