summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorTim Schumacher <timschumi@gmx.de>2022-12-14 01:36:04 +0100
committerLinus Groh <mail@linusgroh.de>2022-12-14 15:20:37 +0000
commit69de7096255166d699b5593463b76f5e2129a48e (patch)
tree742082ca4b65d1dd12b41eafd831d02fd80e59fc /Userland
parent1ec646ab940e3f0f5e4dfdb18762ba46c9735c91 (diff)
downloadserenity-69de7096255166d699b5593463b76f5e2129a48e.zip
LibArchive: Do some error propagation while packing tar files
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibArchive/TarStream.cpp30
-rw-r--r--Userland/Libraries/LibArchive/TarStream.h8
-rw-r--r--Userland/Utilities/tar.cpp8
3 files changed, 25 insertions, 21 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;
diff --git a/Userland/Utilities/tar.cpp b/Userland/Utilities/tar.cpp
index f1527670d5..4a19f83f9d 100644
--- a/Userland/Utilities/tar.cpp
+++ b/Userland/Utilities/tar.cpp
@@ -229,7 +229,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto statbuf = TRY(Core::System::lstat(path));
auto canonicalized_path = LexicalPath::canonicalized_path(path);
- tar_stream.add_file(canonicalized_path, statbuf.st_mode, file->read_all());
+ TRY(tar_stream.add_file(canonicalized_path, statbuf.st_mode, file->read_all()));
if (verbose)
outln("{}", canonicalized_path);
@@ -240,7 +240,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto statbuf = TRY(Core::System::lstat(path));
auto canonicalized_path = LexicalPath::canonicalized_path(path);
- tar_stream.add_link(canonicalized_path, statbuf.st_mode, TRY(Core::System::readlink(path)));
+ TRY(tar_stream.add_link(canonicalized_path, statbuf.st_mode, TRY(Core::System::readlink(path))));
if (verbose)
outln("{}", canonicalized_path);
@@ -251,7 +251,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto statbuf = TRY(Core::System::lstat(path));
auto canonicalized_path = LexicalPath::canonicalized_path(path);
- tar_stream.add_directory(canonicalized_path, statbuf.st_mode);
+ TRY(tar_stream.add_directory(canonicalized_path, statbuf.st_mode));
if (verbose)
outln("{}", canonicalized_path);
@@ -278,7 +278,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
}
}
- tar_stream.finish();
+ TRY(tar_stream.finish());
return 0;
}