1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:17:35 +00:00

LibArchive: Port TarFileStream to Core::Stream

This commit is contained in:
Tim Schumacher 2022-11-28 15:57:44 +01:00 committed by Andreas Kling
parent 6e29619dcb
commit 71d1d9e2b5
3 changed files with 26 additions and 53 deletions

View file

@ -17,29 +17,24 @@ TarFileStream::TarFileStream(TarInputStream& tar_stream)
{
}
size_t TarFileStream::read(Bytes bytes)
ErrorOr<Bytes> TarFileStream::read(Bytes bytes)
{
// verify that the stream has not advanced
// Verify that the stream has not advanced.
VERIFY(m_tar_stream.m_generation == m_generation);
if (has_any_error())
return 0;
auto header_size_or_error = m_tar_stream.header().size();
if (header_size_or_error.is_error())
return 0;
auto header_size = header_size_or_error.release_value();
auto header_size = TRY(m_tar_stream.header().size());
auto to_read = min(bytes.size(), header_size - m_tar_stream.m_file_offset);
auto nread = m_tar_stream.m_stream.read(bytes.trim(to_read));
m_tar_stream.m_file_offset += nread;
return nread;
return bytes.slice(0, nread);
}
bool TarFileStream::unreliable_eof() const
bool TarFileStream::is_eof() const
{
// verify that the stream has not advanced
// Verify that the stream has not advanced.
VERIFY(m_tar_stream.m_generation == m_generation);
auto header_size_or_error = m_tar_stream.header().size();
@ -51,34 +46,10 @@ bool TarFileStream::unreliable_eof() const
|| m_tar_stream.m_file_offset >= header_size;
}
bool TarFileStream::read_or_error(Bytes bytes)
ErrorOr<size_t> TarFileStream::write(ReadonlyBytes)
{
// verify that the stream has not advanced
VERIFY(m_tar_stream.m_generation == m_generation);
if (read(bytes) < bytes.size()) {
set_fatal_error();
return false;
}
return true;
}
bool TarFileStream::discard_or_error(size_t count)
{
// verify that the stream has not advanced
VERIFY(m_tar_stream.m_generation == m_generation);
auto header_size_or_error = m_tar_stream.header().size();
if (header_size_or_error.is_error())
return false;
auto header_size = header_size_or_error.release_value();
if (count > header_size - m_tar_stream.m_file_offset) {
return false;
}
m_tar_stream.m_file_offset += count;
return m_tar_stream.m_stream.discard_or_error(count);
// This is purely for wrapping and representing file contents in an archive.
VERIFY_NOT_REACHED();
}
TarInputStream::TarInputStream(InputStream& stream)

View file

@ -8,20 +8,20 @@
#pragma once
#include <AK/Span.h>
#include <AK/Stream.h>
#include <LibArchive/Tar.h>
#include <LibCore/Stream.h>
namespace Archive {
class TarInputStream;
class TarFileStream : public InputStream {
class TarFileStream : public Core::Stream::Stream {
public:
size_t read(Bytes) override;
bool unreliable_eof() const override;
bool read_or_error(Bytes) override;
bool discard_or_error(size_t count) override;
virtual ErrorOr<Bytes> read(Bytes) override;
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
virtual bool is_eof() const override;
virtual bool is_open() const override { return true; };
virtual void close() override {};
private:
TarFileStream(TarInputStream& stream);
@ -77,7 +77,7 @@ inline ErrorOr<void> TarInputStream::for_each_extended_header(F func)
auto header_size = TRY(header().size());
ByteBuffer file_contents_buffer = TRY(ByteBuffer::create_zeroed(header_size));
VERIFY(file_stream.read(file_contents_buffer) == header_size);
VERIFY(TRY(file_stream.read(file_contents_buffer)).size() == header_size);
StringView file_contents { file_contents_buffer };

View file

@ -136,10 +136,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
StringBuilder long_name;
Array<u8, buffer_size> buffer;
size_t bytes_read;
while ((bytes_read = file_stream.read(buffer)) > 0)
long_name.append(reinterpret_cast<char*>(buffer.data()), bytes_read);
while (!file_stream.is_eof()) {
auto slice = TRY(file_stream.read(buffer));
long_name.append(reinterpret_cast<char*>(slice.data()), slice.size());
}
local_overrides.set("path", long_name.to_string());
TRY(tar_stream.advance());
@ -171,9 +172,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
int fd = TRY(Core::System::open(absolute_path, O_CREAT | O_WRONLY, header_mode));
Array<u8, buffer_size> buffer;
size_t bytes_read;
while ((bytes_read = file_stream.read(buffer)) > 0)
TRY(Core::System::write(fd, buffer.span().slice(0, bytes_read)));
while (!file_stream.is_eof()) {
auto slice = TRY(file_stream.read(buffer));
TRY(Core::System::write(fd, slice));
}
TRY(Core::System::close(fd));
break;