TarStream: Refactor the API to get rid of that awful method in Result
All checks were successful
continuous-integration/drone/push Build is passing
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:
parent
6cf042e65e
commit
82b555cf5c
@ -55,7 +55,7 @@ Result<void> init()
|
|||||||
Scheduler::init();
|
Scheduler::init();
|
||||||
|
|
||||||
TarStream::Entry entry;
|
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)
|
if (entry.type == TarStream::EntryType::RegularFile)
|
||||||
{
|
{
|
||||||
|
@ -115,14 +115,6 @@ template <typename T> class Result
|
|||||||
return m_value.try_move_value(ref);
|
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()
|
T release_value()
|
||||||
{
|
{
|
||||||
expect(has_value(), "Result::release_value() called on a Result that holds an error");
|
expect(has_value(), "Result::release_value() called on a Result that holds an error");
|
||||||
|
@ -29,7 +29,7 @@ class TarStream
|
|||||||
|
|
||||||
void initialize(void* base, usize size);
|
void initialize(void* base, usize size);
|
||||||
|
|
||||||
Result<Entry> read_next_entry();
|
Result<bool> read_next_entry(Entry& out);
|
||||||
|
|
||||||
void rewind();
|
void rewind();
|
||||||
|
|
||||||
@ -58,9 +58,9 @@ class TarStream
|
|||||||
char prefix[155];
|
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);
|
Result<Entry> parse_header(const TarHeader* hdr);
|
||||||
|
|
||||||
void* m_base;
|
void* m_base;
|
||||||
|
@ -22,13 +22,17 @@ void TarStream::rewind()
|
|||||||
m_offset = 0;
|
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));
|
memcpy(out, m_pos, sizeof(TarHeader));
|
||||||
m_pos = offset_ptr(m_pos, 512);
|
m_pos = offset_ptr(m_pos, 512);
|
||||||
m_offset += 512;
|
m_offset += 512;
|
||||||
return {};
|
success = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<TarStream::Entry> TarStream::parse_header(const TarStream::TarHeader* hdr)
|
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;
|
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;
|
success = false;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
TRY(read_header(out));
|
read_header(out, success);
|
||||||
|
if (!success) return;
|
||||||
if (!memcmp(out->magic, "ustar", 5))
|
if (!memcmp(out->magic, "ustar", 5))
|
||||||
{
|
{
|
||||||
success = true;
|
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
|
Result<bool> TarStream::read_next_entry(Entry& out)
|
||||||
// clunky to me...
|
|
||||||
Result<TarStream::Entry> TarStream::read_next_entry()
|
|
||||||
{
|
{
|
||||||
bool success;
|
bool success;
|
||||||
TarHeader header;
|
TarHeader header;
|
||||||
auto rc = find_valid_header(&header, success);
|
find_valid_header(&header, success);
|
||||||
if (!success) return rc.release_error();
|
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
|
usize TarStream::read_contents(const Entry& entry, void* buf, usize offset, usize length) const
|
||||||
|
Loading…
Reference in New Issue
Block a user