LibArchive: Do some error propagation while packing tar files

This commit is contained in:
Tim Schumacher 2022-12-14 01:36:04 +01:00 committed by Linus Groh
parent 1ec646ab94
commit 69de709625
3 changed files with 25 additions and 21 deletions

View file

@ -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 {};
}
}

View file

@ -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;

View file

@ -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;
}