Serenity Operating System
at master 140 lines 6.1 kB view raw
1/* 2 * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <AK/DOSPackedTime.h> 8#include <AK/LexicalPath.h> 9#include <LibArchive/Zip.h> 10#include <LibCompress/Deflate.h> 11#include <LibCore/ArgsParser.h> 12#include <LibCore/DateTime.h> 13#include <LibCore/DeprecatedFile.h> 14#include <LibCore/DirIterator.h> 15#include <LibCore/File.h> 16#include <LibCore/System.h> 17#include <LibCrypto/Checksum/CRC32.h> 18 19ErrorOr<int> serenity_main(Main::Arguments arguments) 20{ 21 StringView zip_path; 22 Vector<StringView> source_paths; 23 bool recurse = false; 24 bool force = false; 25 26 Core::ArgsParser args_parser; 27 args_parser.add_positional_argument(zip_path, "Zip file path", "zipfile", Core::ArgsParser::Required::Yes); 28 args_parser.add_positional_argument(source_paths, "Input files to be archived", "files", Core::ArgsParser::Required::Yes); 29 args_parser.add_option(recurse, "Travel the directory structure recursively", "recurse-paths", 'r'); 30 args_parser.add_option(force, "Overwrite existing zip file", "force", 'f'); 31 args_parser.parse(arguments); 32 33 TRY(Core::System::pledge("stdio rpath wpath cpath")); 34 35 auto cwd = TRY(Core::System::getcwd()); 36 TRY(Core::System::unveil(LexicalPath::absolute_path(cwd, zip_path), "wc"sv)); 37 for (auto const& source_path : source_paths) { 38 TRY(Core::System::unveil(LexicalPath::absolute_path(cwd, source_path), "r"sv)); 39 } 40 TRY(Core::System::unveil(nullptr, nullptr)); 41 42 DeprecatedString zip_file_path { zip_path }; 43 if (Core::DeprecatedFile::exists(zip_file_path)) { 44 if (force) { 45 outln("{} already exists, overwriting...", zip_file_path); 46 } else { 47 warnln("{} already exists, aborting!", zip_file_path); 48 return 1; 49 } 50 } 51 52 outln("Archive: {}", zip_file_path); 53 auto file_stream = TRY(Core::File::open(zip_file_path, Core::File::OpenMode::Write)); 54 Archive::ZipOutputStream zip_stream(move(file_stream)); 55 56 auto add_file = [&](DeprecatedString path) -> ErrorOr<void> { 57 auto canonicalized_path = LexicalPath::canonicalized_path(path); 58 auto file = TRY(Core::File::open(path, Core::File::OpenMode::Read)); 59 auto file_buffer = TRY(file->read_until_eof()); 60 Archive::ZipMember member {}; 61 member.name = TRY(String::from_deprecated_string(canonicalized_path)); 62 63 auto stat = TRY(Core::System::fstat(file->fd())); 64 auto date = Core::DateTime::from_timestamp(stat.st_mtim.tv_sec); 65 member.modification_date = to_packed_dos_date(date.year(), date.month(), date.day()); 66 member.modification_time = to_packed_dos_time(date.hour(), date.minute(), date.second()); 67 68 auto deflate_buffer = Compress::DeflateCompressor::compress_all(file_buffer); 69 if (!deflate_buffer.is_error() && deflate_buffer.value().size() < file_buffer.size()) { 70 member.compressed_data = deflate_buffer.value().bytes(); 71 member.compression_method = Archive::ZipCompressionMethod::Deflate; 72 auto compression_ratio = (double)deflate_buffer.value().size() / file_buffer.size(); 73 outln(" adding: {} (deflated {}%)", canonicalized_path, (int)(compression_ratio * 100)); 74 } else { 75 member.compressed_data = file_buffer.bytes(); 76 member.compression_method = Archive::ZipCompressionMethod::Store; 77 outln(" adding: {} (stored 0%)", canonicalized_path); 78 } 79 member.uncompressed_size = file_buffer.size(); 80 Crypto::Checksum::CRC32 checksum { file_buffer.bytes() }; 81 member.crc32 = checksum.digest(); 82 member.is_directory = false; 83 return zip_stream.add_member(member); 84 }; 85 86 auto add_directory = [&](DeprecatedString path, auto handle_directory) -> ErrorOr<void> { 87 auto canonicalized_path = DeprecatedString::formatted("{}/", LexicalPath::canonicalized_path(path)); 88 Archive::ZipMember member {}; 89 member.name = TRY(String::from_deprecated_string(canonicalized_path)); 90 member.compressed_data = {}; 91 member.compression_method = Archive::ZipCompressionMethod::Store; 92 member.uncompressed_size = 0; 93 member.crc32 = 0; 94 member.is_directory = true; 95 96 auto stat = TRY(Core::System::stat(canonicalized_path)); 97 auto date = Core::DateTime::from_timestamp(stat.st_mtim.tv_sec); 98 member.modification_date = to_packed_dos_date(date.year(), date.month(), date.day()); 99 member.modification_time = to_packed_dos_time(date.hour(), date.minute(), date.second()); 100 101 TRY(zip_stream.add_member(member)); 102 outln(" adding: {} (stored 0%)", canonicalized_path); 103 104 if (!recurse) 105 return {}; 106 107 Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir); 108 while (it.has_next()) { 109 auto child_path = it.next_full_path(); 110 if (Core::DeprecatedFile::is_link(child_path)) 111 return {}; 112 if (!Core::DeprecatedFile::is_directory(child_path)) { 113 auto result = add_file(child_path); 114 if (result.is_error()) 115 warnln("Couldn't add file '{}': {}", child_path, result.error()); 116 } else { 117 auto result = handle_directory(child_path, handle_directory); 118 if (result.is_error()) 119 warnln("Couldn't add directory '{}': {}", child_path, result.error()); 120 } 121 } 122 return {}; 123 }; 124 125 for (auto const& source_path : source_paths) { 126 if (Core::DeprecatedFile::is_directory(source_path)) { 127 auto result = add_directory(source_path, add_directory); 128 if (result.is_error()) 129 warnln("Couldn't add directory '{}': {}", source_path, result.error()); 130 } else { 131 auto result = add_file(source_path); 132 if (result.is_error()) 133 warnln("Couldn't add file '{}': {}", source_path, result.error()); 134 } 135 } 136 137 TRY(zip_stream.finish()); 138 139 return 0; 140}