diff options
author | Tim Schumacher <timschumi@gmx.de> | 2022-12-14 01:36:04 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-12-14 15:20:37 +0000 |
commit | 69de7096255166d699b5593463b76f5e2129a48e (patch) | |
tree | 742082ca4b65d1dd12b41eafd831d02fd80e59fc /Userland/Libraries/LibArchive | |
parent | 1ec646ab940e3f0f5e4dfdb18762ba46c9735c91 (diff) | |
download | serenity-69de7096255166d699b5593463b76f5e2129a48e.zip |
LibArchive: Do some error propagation while packing tar files
Diffstat (limited to 'Userland/Libraries/LibArchive')
-rw-r--r-- | Userland/Libraries/LibArchive/TarStream.cpp | 30 | ||||
-rw-r--r-- | Userland/Libraries/LibArchive/TarStream.h | 8 |
2 files changed, 21 insertions, 17 deletions
diff --git a/Userland/Libraries/LibArchive/TarStream.cpp b/Userland/Libraries/LibArchive/TarStream.cpp index f7b5d5ebde..fcac3c8a25 100644 --- a/Userland/Libraries/LibArchive/TarStream.cpp +++ b/Userland/Libraries/LibArchive/TarStream.cpp @@ -142,7 +142,7 @@ TarOutputStream::TarOutputStream(Core::Stream::Handle<Core::Stream::Stream> stre { } -void TarOutputStream::add_directory(DeprecatedString const& path, mode_t mode) +ErrorOr<void> TarOutputStream::add_directory(DeprecatedString const& path, mode_t mode) { VERIFY(!m_finished); TarFileHeader header {}; @@ -153,12 +153,13 @@ void TarOutputStream::add_directory(DeprecatedString const& path, mode_t mode) header.set_magic(gnu_magic); header.set_version(gnu_version); header.calculate_checksum(); - MUST(m_stream->write_entire_buffer(Bytes { &header, sizeof(header) })); + TRY(m_stream->write_entire_buffer(Bytes { &header, sizeof(header) })); u8 padding[block_size] = { 0 }; - MUST(m_stream->write_entire_buffer(Bytes { &padding, block_size - sizeof(header) })); + TRY(m_stream->write_entire_buffer(Bytes { &padding, block_size - sizeof(header) })); + return {}; } -void TarOutputStream::add_file(DeprecatedString const& path, mode_t mode, ReadonlyBytes bytes) +ErrorOr<void> TarOutputStream::add_file(DeprecatedString const& path, mode_t mode, ReadonlyBytes bytes) { VERIFY(!m_finished); TarFileHeader header {}; @@ -169,17 +170,18 @@ void TarOutputStream::add_file(DeprecatedString const& path, mode_t mode, Readon header.set_magic(gnu_magic); header.set_version(gnu_version); header.calculate_checksum(); - MUST(m_stream->write_entire_buffer(ReadonlyBytes { &header, sizeof(header) })); + TRY(m_stream->write_entire_buffer(ReadonlyBytes { &header, sizeof(header) })); constexpr Array<u8, block_size> padding { 0 }; - MUST(m_stream->write_entire_buffer(ReadonlyBytes { &padding, block_size - sizeof(header) })); + TRY(m_stream->write_entire_buffer(ReadonlyBytes { &padding, block_size - sizeof(header) })); size_t n_written = 0; while (n_written < bytes.size()) { n_written += MUST(m_stream->write(bytes.slice(n_written, min(bytes.size() - n_written, block_size)))); } - MUST(m_stream->write_entire_buffer(ReadonlyBytes { &padding, block_size - (n_written % block_size) })); + TRY(m_stream->write_entire_buffer(ReadonlyBytes { &padding, block_size - (n_written % block_size) })); + return {}; } -void TarOutputStream::add_link(DeprecatedString const& path, mode_t mode, StringView link_name) +ErrorOr<void> TarOutputStream::add_link(DeprecatedString const& path, mode_t mode, StringView link_name) { VERIFY(!m_finished); TarFileHeader header {}; @@ -191,19 +193,21 @@ void TarOutputStream::add_link(DeprecatedString const& path, mode_t mode, String header.set_version(gnu_version); header.set_link_name(link_name); header.calculate_checksum(); - MUST(m_stream->write_entire_buffer(Bytes { &header, sizeof(header) })); + TRY(m_stream->write_entire_buffer(Bytes { &header, sizeof(header) })); u8 padding[block_size] = { 0 }; - MUST(m_stream->write_entire_buffer(Bytes { &padding, block_size - sizeof(header) })); + TRY(m_stream->write_entire_buffer(Bytes { &padding, block_size - sizeof(header) })); + return {}; } -void TarOutputStream::finish() +ErrorOr<void> TarOutputStream::finish() { VERIFY(!m_finished); constexpr Array<u8, block_size> padding { 0 }; // 2 empty records that are used to signify the end of the archive. - MUST(m_stream->write_entire_buffer(ReadonlyBytes { &padding, block_size })); - MUST(m_stream->write_entire_buffer(ReadonlyBytes { &padding, block_size })); + TRY(m_stream->write_entire_buffer(ReadonlyBytes { &padding, block_size })); + TRY(m_stream->write_entire_buffer(ReadonlyBytes { &padding, block_size })); m_finished = true; + return {}; } } diff --git a/Userland/Libraries/LibArchive/TarStream.h b/Userland/Libraries/LibArchive/TarStream.h index 5d62d0d19a..cd571e94da 100644 --- a/Userland/Libraries/LibArchive/TarStream.h +++ b/Userland/Libraries/LibArchive/TarStream.h @@ -59,10 +59,10 @@ private: class TarOutputStream { public: TarOutputStream(Core::Stream::Handle<Core::Stream::Stream>); - void add_file(DeprecatedString const& path, mode_t, ReadonlyBytes); - void add_link(DeprecatedString const& path, mode_t, StringView); - void add_directory(DeprecatedString const& path, mode_t); - void finish(); + ErrorOr<void> add_file(DeprecatedString const& path, mode_t, ReadonlyBytes); + ErrorOr<void> add_link(DeprecatedString const& path, mode_t, StringView); + ErrorOr<void> add_directory(DeprecatedString const& path, mode_t); + ErrorOr<void> finish(); private: Core::Stream::Handle<Core::Stream::Stream> m_stream; |