diff options
author | Karol Kosek <krkk@serenityos.org> | 2022-12-25 15:33:30 +0100 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2022-12-27 07:24:07 +0330 |
commit | 8238f926fda6dbdaa7131a55965e76179d82f8ba (patch) | |
tree | 6002640b78d6e6a6a6363a11109552d113cab6da /Userland | |
parent | 012645bdf954229fbc4fc9c0b2c6f290cc1330fc (diff) | |
download | serenity-8238f926fda6dbdaa7131a55965e76179d82f8ba.zip |
LibArchive+Utilities: Port ZipOutputStream to Core::Stream
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibArchive/Zip.cpp | 21 | ||||
-rw-r--r-- | Userland/Libraries/LibArchive/Zip.h | 120 | ||||
-rw-r--r-- | Userland/Utilities/zip.cpp | 29 |
3 files changed, 90 insertions, 80 deletions
diff --git a/Userland/Libraries/LibArchive/Zip.cpp b/Userland/Libraries/LibArchive/Zip.cpp index bf842d9a0d..45af7cd9ef 100644 --- a/Userland/Libraries/LibArchive/Zip.cpp +++ b/Userland/Libraries/LibArchive/Zip.cpp @@ -9,11 +9,6 @@ namespace Archive { -OutputStream& operator<<(OutputStream& stream, ZipCompressionMethod method) -{ - return stream << to_underlying(method); -} - bool Zip::find_end_of_central_directory_offset(ReadonlyBytes buffer, size_t& offset) { for (size_t backwards_offset = 0; backwards_offset <= UINT16_MAX; backwards_offset++) // the file may have a trailing comment of an arbitrary 16 bit length @@ -108,8 +103,8 @@ bool Zip::for_each_member(Function<IterationDecision(ZipMember const&)> callback return true; } -ZipOutputStream::ZipOutputStream(OutputStream& stream) - : m_stream(stream) +ZipOutputStream::ZipOutputStream(NonnullOwnPtr<Core::Stream::Stream> stream) + : m_stream(move(stream)) { } @@ -119,12 +114,12 @@ static u16 minimum_version_needed(ZipCompressionMethod method) return method == ZipCompressionMethod::Deflate ? 20 : 10; } -void ZipOutputStream::add_member(ZipMember const& member) +ErrorOr<void> ZipOutputStream::add_member(ZipMember const& member) { VERIFY(!m_finished); VERIFY(member.name.length() <= UINT16_MAX); VERIFY(member.compressed_data.size() <= UINT32_MAX); - m_members.append(member); + TRY(m_members.try_append(member)); LocalFileHeader local_file_header { .minimum_version = minimum_version_needed(member.compression_method), @@ -141,10 +136,10 @@ void ZipOutputStream::add_member(ZipMember const& member) .extra_data = nullptr, .compressed_data = member.compressed_data.data(), }; - local_file_header.write(m_stream); + return local_file_header.write(*m_stream); } -void ZipOutputStream::finish() +ErrorOr<void> ZipOutputStream::finish() { VERIFY(!m_finished); m_finished = true; @@ -175,7 +170,7 @@ void ZipOutputStream::finish() .comment = nullptr, }; file_header_offset += sizeof(LocalFileHeader::signature) + (sizeof(LocalFileHeader) - (sizeof(u8*) * 3)) + member.name.length() + member.compressed_data.size(); - central_directory_record.write(m_stream); + TRY(central_directory_record.write(*m_stream)); central_directory_size += central_directory_record.size(); } @@ -189,7 +184,7 @@ void ZipOutputStream::finish() .comment_length = 0, .comment = nullptr, }; - end_of_central_directory.write(m_stream); + return end_of_central_directory.write(*m_stream); } } diff --git a/Userland/Libraries/LibArchive/Zip.h b/Userland/Libraries/LibArchive/Zip.h index 704b46e2cb..4ce3572c53 100644 --- a/Userland/Libraries/LibArchive/Zip.h +++ b/Userland/Libraries/LibArchive/Zip.h @@ -11,8 +11,8 @@ #include <AK/DeprecatedString.h> #include <AK/Function.h> #include <AK/IterationDecision.h> -#include <AK/Stream.h> #include <AK/Vector.h> +#include <LibCore/Stream.h> #include <string.h> namespace Archive { @@ -55,18 +55,23 @@ struct [[gnu::packed]] EndOfCentralDirectory { return true; } - void write(OutputStream& stream) const + ErrorOr<void> write(Core::Stream::Stream& stream) const { - stream.write_or_error(signature); - stream << disk_number; - stream << central_directory_start_disk; - stream << disk_records_count; - stream << total_records_count; - stream << central_directory_size; - stream << central_directory_offset; - stream << comment_length; + auto write_value = [&stream](auto value) { + return stream.write_entire_buffer({ &value, sizeof(value) }); + }; + + TRY(stream.write_entire_buffer(signature)); + TRY(write_value(disk_number)); + TRY(write_value(central_directory_start_disk)); + TRY(write_value(disk_records_count)); + TRY(write_value(total_records_count)); + TRY(write_value(central_directory_size)); + TRY(write_value(central_directory_offset)); + TRY(write_value(comment_length)); if (comment_length > 0) - stream.write_or_error({ comment, comment_length }); + TRY(stream.write_entire_buffer({ comment, comment_length })); + return {}; } }; @@ -100,8 +105,6 @@ union ZipGeneralPurposeFlags { }; static_assert(sizeof(ZipGeneralPurposeFlags) == sizeof(u16)); -OutputStream& operator<<(OutputStream& stream, ZipCompressionMethod method); - struct [[gnu::packed]] CentralDirectoryRecord { static constexpr Array<u8, signature_length> signature = { 0x50, 0x4b, 0x01, 0x02 }; // 'PK\x01\x02' @@ -138,31 +141,36 @@ struct [[gnu::packed]] CentralDirectoryRecord { return true; } - void write(OutputStream& stream) const + ErrorOr<void> write(Core::Stream::Stream& stream) const { - stream.write_or_error(signature); - stream << made_by_version; - stream << minimum_version; - stream << general_purpose_flags.flags; - stream << compression_method; - stream << modification_time; - stream << modification_date; - stream << crc32; - stream << compressed_size; - stream << uncompressed_size; - stream << name_length; - stream << extra_data_length; - stream << comment_length; - stream << start_disk; - stream << internal_attributes; - stream << external_attributes; - stream << local_file_header_offset; + auto write_value = [&stream](auto value) { + return stream.write_entire_buffer({ &value, sizeof(value) }); + }; + + TRY(stream.write_entire_buffer(signature)); + TRY(write_value(made_by_version)); + TRY(write_value(minimum_version)); + TRY(write_value(general_purpose_flags.flags)); + TRY(write_value(compression_method)); + TRY(write_value(modification_time)); + TRY(write_value(modification_date)); + TRY(write_value(crc32)); + TRY(write_value(compressed_size)); + TRY(write_value(uncompressed_size)); + TRY(write_value(name_length)); + TRY(write_value(extra_data_length)); + TRY(write_value(comment_length)); + TRY(write_value(start_disk)); + TRY(write_value(internal_attributes)); + TRY(write_value(external_attributes)); + TRY(write_value(local_file_header_offset)); if (name_length > 0) - stream.write_or_error({ name, name_length }); + TRY(stream.write_entire_buffer({ name, name_length })); if (extra_data_length > 0) - stream.write_or_error({ extra_data, extra_data_length }); + TRY(stream.write_entire_buffer({ extra_data, extra_data_length })); if (comment_length > 0) - stream.write_or_error({ comment, comment_length }); + TRY(stream.write_entire_buffer({ comment, comment_length })); + return {}; } [[nodiscard]] size_t size() const @@ -202,25 +210,30 @@ struct [[gnu::packed]] LocalFileHeader { return true; } - void write(OutputStream& stream) const + ErrorOr<void> write(Core::Stream::Stream& stream) const { - stream.write_or_error(signature); - stream << minimum_version; - stream << general_purpose_flags.flags; - stream << compression_method; - stream << modification_time; - stream << modification_date; - stream << crc32; - stream << compressed_size; - stream << uncompressed_size; - stream << name_length; - stream << extra_data_length; + auto write_value = [&stream](auto value) { + return stream.write_entire_buffer({ &value, sizeof(value) }); + }; + + TRY(stream.write_entire_buffer(signature)); + TRY(write_value(minimum_version)); + TRY(write_value(general_purpose_flags.flags)); + TRY(write_value(compression_method)); + TRY(write_value(modification_time)); + TRY(write_value(modification_date)); + TRY(write_value(crc32)); + TRY(write_value(compressed_size)); + TRY(write_value(uncompressed_size)); + TRY(write_value(name_length)); + TRY(write_value(extra_data_length)); if (name_length > 0) - stream.write_or_error({ name, name_length }); + TRY(stream.write_entire_buffer({ name, name_length })); if (extra_data_length > 0) - stream.write_or_error({ extra_data, extra_data_length }); + TRY(stream.write_entire_buffer({ extra_data, extra_data_length })); if (compressed_size > 0) - stream.write_or_error({ compressed_data, compressed_size }); + TRY(stream.write_entire_buffer({ compressed_data, compressed_size })); + return {}; } }; @@ -254,12 +267,13 @@ private: class ZipOutputStream { public: - ZipOutputStream(OutputStream&); - void add_member(ZipMember const&); - void finish(); + ZipOutputStream(NonnullOwnPtr<Core::Stream::Stream>); + + ErrorOr<void> add_member(ZipMember const&); + ErrorOr<void> finish(); private: - OutputStream& m_stream; + NonnullOwnPtr<Core::Stream::Stream> m_stream; Vector<ZipMember> m_members; bool m_finished { false }; diff --git a/Userland/Utilities/zip.cpp b/Userland/Utilities/zip.cpp index 5caebca2de..b04dc9bd32 100644 --- a/Userland/Utilities/zip.cpp +++ b/Userland/Utilities/zip.cpp @@ -10,7 +10,6 @@ #include <LibCore/ArgsParser.h> #include <LibCore/DirIterator.h> #include <LibCore/File.h> -#include <LibCore/FileStream.h> #include <LibCore/Stream.h> #include <LibCore/System.h> #include <LibCrypto/Checksum/CRC32.h> @@ -48,11 +47,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) } } - auto file_stream = TRY(Core::OutputFileStream::open(zip_file_path)); - outln("Archive: {}", zip_file_path); - - Archive::ZipOutputStream zip_stream { file_stream }; + auto file_stream = TRY(Core::Stream::File::open(zip_file_path, Core::Stream::OpenMode::Write)); + Archive::ZipOutputStream zip_stream(move(file_stream)); auto add_file = [&](DeprecatedString path) -> ErrorOr<void> { auto canonicalized_path = LexicalPath::canonicalized_path(path); @@ -76,11 +73,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) Crypto::Checksum::CRC32 checksum { file_buffer.bytes() }; member.crc32 = checksum.digest(); member.is_directory = false; - zip_stream.add_member(member); - return {}; + return zip_stream.add_member(member); }; - auto add_directory = [&](DeprecatedString path, auto handle_directory) -> void { + auto add_directory = [&](DeprecatedString path, auto handle_directory) -> ErrorOr<void> { auto canonicalized_path = DeprecatedString::formatted("{}/", LexicalPath::canonicalized_path(path)); Archive::ZipMember member {}; member.name = canonicalized_path; @@ -89,30 +85,35 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) member.uncompressed_size = 0; member.crc32 = 0; member.is_directory = true; - zip_stream.add_member(member); + TRY(zip_stream.add_member(member)); outln(" adding: {} (stored 0%)", canonicalized_path); if (!recurse) - return; + return {}; Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir); while (it.has_next()) { auto child_path = it.next_full_path(); if (Core::File::is_link(child_path)) - return; + return {}; if (!Core::File::is_directory(child_path)) { auto result = add_file(child_path); if (result.is_error()) warnln("Couldn't add file '{}': {}", child_path, result.error()); } else { - handle_directory(child_path, handle_directory); + 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 (Core::File::is_directory(source_path)) { - add_directory(source_path, add_directory); + 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()) @@ -120,7 +121,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) } } - zip_stream.finish(); + TRY(zip_stream.finish()); return 0; } |