TarStream: Refactor the API to get rid of that awful method in Result
All checks were successful
continuous-integration/drone/push Build is passing

That method being try_set_value_with_specific_error()
This commit is contained in:
apio 2023-01-11 17:30:53 +01:00
parent 6cf042e65e
commit 82b555cf5c
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 21 additions and 24 deletions

View File

@ -55,7 +55,7 @@ Result<void> init()
Scheduler::init();
TarStream::Entry entry;
while (TRY(g_initrd.read_next_entry().try_set_value_with_specific_error(entry, 0)))
while (TRY(g_initrd.read_next_entry(entry)))
{
if (entry.type == TarStream::EntryType::RegularFile)
{

View File

@ -115,14 +115,6 @@ template <typename T> class Result
return m_value.try_move_value(ref);
}
// FIXME: Please remove this.
Result<bool> try_set_value_with_specific_error(T& ref, int error)
{
if (has_error() && m_error != error) return release_error();
return m_value.try_set_value(ref);
}
T release_value()
{
expect(has_value(), "Result::release_value() called on a Result that holds an error");

View File

@ -29,7 +29,7 @@ class TarStream
void initialize(void* base, usize size);
Result<Entry> read_next_entry();
Result<bool> read_next_entry(Entry& out);
void rewind();
@ -58,9 +58,9 @@ class TarStream
char prefix[155];
};
Result<void> find_valid_header(TarHeader* out, bool& success);
void find_valid_header(TarHeader* out, bool& success);
Result<void> read_header(TarHeader* out);
void read_header(TarHeader* out, bool& success);
Result<Entry> parse_header(const TarHeader* hdr);
void* m_base;

View File

@ -22,13 +22,17 @@ void TarStream::rewind()
m_offset = 0;
}
Result<void> TarStream::read_header(TarHeader* out)
void TarStream::read_header(TarHeader* out, bool& success)
{
if ((m_offset + 512) > m_size) return err(0);
if ((m_offset + 512) > m_size)
{
success = false;
return;
};
memcpy(out, m_pos, sizeof(TarHeader));
m_pos = offset_ptr(m_pos, 512);
m_offset += 512;
return {};
success = true;
}
Result<TarStream::Entry> TarStream::parse_header(const TarStream::TarHeader* hdr)
@ -61,30 +65,31 @@ Result<TarStream::Entry> TarStream::parse_header(const TarStream::TarHeader* hdr
return entry;
}
Result<void> TarStream::find_valid_header(TarStream::TarHeader* out, bool& success)
void TarStream::find_valid_header(TarStream::TarHeader* out, bool& success)
{
success = false;
while (true)
{
TRY(read_header(out));
read_header(out, success);
if (!success) return;
if (!memcmp(out->magic, "ustar", 5))
{
success = true;
return {};
return;
}
}
}
// FIXME: How do we know whether an error is an error or just EOF? Returning an error code of 0 as EOF seems a bit
// clunky to me...
Result<TarStream::Entry> TarStream::read_next_entry()
Result<bool> TarStream::read_next_entry(Entry& out)
{
bool success;
TarHeader header;
auto rc = find_valid_header(&header, success);
if (!success) return rc.release_error();
find_valid_header(&header, success);
if (!success) return false;
return parse_header(&header);
out = TRY(parse_header(&header));
return true;
}
usize TarStream::read_contents(const Entry& entry, void* buf, usize offset, usize length) const