Refine and fix bugs in the Oar library
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
2a0f821e64
commit
78ae403f61
201
source/oar.cpp
201
source/oar.cpp
|
@ -3,14 +3,25 @@ export module oar;
|
||||||
import coral;
|
import coral;
|
||||||
import coral.files;
|
import coral.files;
|
||||||
|
|
||||||
constexpr coral::usize signature_length{4};
|
/**
|
||||||
|
* Length of the full magic signature at the beginning of an Oar file.
|
||||||
|
*/
|
||||||
|
constexpr coral::usize signature_length {4};
|
||||||
|
|
||||||
constexpr coral::usize signature_version_length{1};
|
/**
|
||||||
|
* Length of the magic signature at the beginning of an Oar file without the version indicator
|
||||||
|
* byte.
|
||||||
|
*/
|
||||||
|
constexpr coral::usize signature_identifier_length {signature_length - 1};
|
||||||
|
|
||||||
constexpr coral::usize signature_identifier_length{signature_length - signature_version_length};
|
/**
|
||||||
|
* Hardcoded signature magic value that this implementation of Oar expects when reading archives.
|
||||||
constexpr coral::u8 signature_magic[signature_length]{'o', 'a', 'r', 0};
|
*/
|
||||||
|
constexpr coral::u8 signature_magic[signature_length] {'o', 'a', 'r', 0};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Oar file header format.
|
||||||
|
*/
|
||||||
struct header {
|
struct header {
|
||||||
coral::u8 signature_magic[signature_length];
|
coral::u8 signature_magic[signature_length];
|
||||||
|
|
||||||
|
@ -21,6 +32,9 @@ struct header {
|
||||||
|
|
||||||
static_assert(sizeof(header) == 512);
|
static_assert(sizeof(header) == 512);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Oar file header format.
|
||||||
|
*/
|
||||||
struct entry {
|
struct entry {
|
||||||
coral::path path;
|
coral::path path;
|
||||||
|
|
||||||
|
@ -33,13 +47,29 @@ struct entry {
|
||||||
|
|
||||||
static_assert(sizeof(entry) == 512);
|
static_assert(sizeof(entry) == 512);
|
||||||
|
|
||||||
export namespace oar {
|
/**
|
||||||
struct archive_file_reader : public coral::file_reader {
|
* Archive file access interface.
|
||||||
enum class [[nodiscard]] close_result {
|
*/
|
||||||
ok,
|
struct archive_file : public coral::file_reader {
|
||||||
};
|
/**
|
||||||
|
* Results of a find operation performed on an [archive_file].
|
||||||
enum class [[nodiscard]] open_result {
|
*
|
||||||
|
* [find_result::ok] means that the find operation was successful.
|
||||||
|
*
|
||||||
|
* [find_result::io_unavailable] signals a failure to communicate with the underlying
|
||||||
|
* [coral::file_reader] for whatever reason.
|
||||||
|
*
|
||||||
|
* [find_result::archive_invalid] signals that data was read but it does not match the format
|
||||||
|
* of an Oar archive. This is typically because the underlying [coral::file_reader] is not
|
||||||
|
* reading from an Oar archive file.
|
||||||
|
*
|
||||||
|
* [find_result::archive_unsupported] signals that data was read and was formatted as expected
|
||||||
|
* for an Oar archive, however, it is from an unsupported version of the archive format.
|
||||||
|
*
|
||||||
|
* [find_result::not_found] indicates that no entry in the archive could be found that matches
|
||||||
|
* the given query.
|
||||||
|
*/
|
||||||
|
enum class [[nodiscard]] find_result {
|
||||||
ok,
|
ok,
|
||||||
io_unavailable,
|
io_unavailable,
|
||||||
archive_invalid,
|
archive_invalid,
|
||||||
|
@ -47,75 +77,64 @@ export namespace oar {
|
||||||
not_found,
|
not_found,
|
||||||
};
|
};
|
||||||
|
|
||||||
archive_file_reader(coral::file_reader * archive_reader) {
|
archive_file(coral::file_reader * archive_reader) {
|
||||||
this->archive_reader = archive_reader;
|
this->archive_reader = archive_reader;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs a lookup for a file entry matching the path `file_path` in the archive, returning
|
||||||
|
* [find_result] to indicate the result of the operation.
|
||||||
|
*/
|
||||||
|
find_result find(coral::path const & file_path) {
|
||||||
this->data_offset = 0;
|
this->data_offset = 0;
|
||||||
this->data_length = 0;
|
this->data_length = 0;
|
||||||
this->data_cursor = 0;
|
this->data_cursor = 0;
|
||||||
}
|
|
||||||
|
|
||||||
close_result close() {
|
if (!this->archive_reader->seek(0).is_ok()) return find_result::io_unavailable;
|
||||||
return close_result::ok;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_open() const {
|
constexpr coral::usize header_size {sizeof(header)};
|
||||||
return this->data_offset >= sizeof(header);
|
coral::u8 archive_header_buffer[header_size] {0};
|
||||||
}
|
|
||||||
|
|
||||||
open_result open(coral::path const & file_path) {
|
if (!this->archive_reader->read(archive_header_buffer).and_test(
|
||||||
if (this->is_open()) switch (this->close()) {
|
[](coral::usize value) -> bool { return value == header_size; }))
|
||||||
case close_result::ok: break;
|
return find_result::archive_invalid;
|
||||||
default: coral::unreachable();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this->archive_reader->seek(0).is_ok()) return open_result::io_unavailable;
|
header const * const archive_header {
|
||||||
|
reinterpret_cast<header const *>(archive_header_buffer)};
|
||||||
constexpr coral::usize header_size = sizeof(header);
|
|
||||||
coral::u8 archive_header_buffer[header_size]{0};
|
|
||||||
|
|
||||||
{
|
|
||||||
coral::expected const read_bytes{archive_reader->read(archive_header_buffer)};
|
|
||||||
|
|
||||||
if ((!read_bytes.is_ok()) || (read_bytes.value() != header_size))
|
|
||||||
return open_result::archive_invalid;
|
|
||||||
}
|
|
||||||
|
|
||||||
header const * const archive_header{reinterpret_cast<header const *>(archive_header_buffer)};
|
|
||||||
|
|
||||||
if (!coral::equals({archive_header->signature_magic, signature_identifier_length},
|
if (!coral::equals({archive_header->signature_magic, signature_identifier_length},
|
||||||
{signature_magic, signature_identifier_length})) return open_result::archive_invalid;
|
{signature_magic, signature_identifier_length})) return find_result::archive_invalid;
|
||||||
|
|
||||||
if (archive_header->signature_magic[signature_identifier_length] !=
|
if (archive_header->signature_magic[signature_identifier_length] !=
|
||||||
signature_magic[signature_identifier_length]) return open_result::archive_unsupported;
|
signature_magic[signature_identifier_length]) return find_result::archive_unsupported;
|
||||||
|
|
||||||
// Read file table.
|
// Read file table.
|
||||||
coral::u64 head{0};
|
coral::u64 head {0};
|
||||||
coral::u64 tail{archive_header->entry_count - 1};
|
coral::u64 tail {archive_header->entry_count - 1};
|
||||||
constexpr coral::usize entry_size{sizeof(entry)};
|
constexpr coral::usize entry_size {sizeof(entry)};
|
||||||
coral::u8 file_entry_buffer[entry_size]{0};
|
coral::u8 archive_entry_buffer[entry_size] {0};
|
||||||
|
|
||||||
while (head <= tail) {
|
while (head <= tail) {
|
||||||
coral::u64 const midpoint{head + ((tail - head) / 2)};
|
coral::u64 const midpoint {head + ((tail - head) / 2)};
|
||||||
|
|
||||||
if (!archive_reader->seek(header_size + (entry_size * midpoint)).is_ok())
|
if (!this->archive_reader->seek(header_size + (entry_size * midpoint)).is_ok())
|
||||||
return open_result::archive_invalid;
|
return find_result::archive_invalid;
|
||||||
|
|
||||||
{
|
if (!this->archive_reader->read(archive_entry_buffer).and_test(
|
||||||
coral::expected const read_bytes{archive_reader->read(file_entry_buffer)};
|
[](coral::usize value) -> bool { return value == entry_size; }))
|
||||||
|
return find_result::archive_invalid;
|
||||||
|
|
||||||
if ((!read_bytes.is_ok()) || (read_bytes.value() != header_size))
|
entry const * const archive_entry {
|
||||||
return open_result::archive_invalid;
|
reinterpret_cast<entry const *>(archive_entry_buffer)};
|
||||||
}
|
|
||||||
|
|
||||||
entry const * const archive_entry{reinterpret_cast<entry const *>(file_entry_buffer)};
|
coral::size const comparison {file_path.compare(archive_entry->path)};
|
||||||
coral::size const comparison{file_path.compare(archive_entry->path)};
|
|
||||||
|
|
||||||
if (comparison == 0) {
|
if (comparison == 0) {
|
||||||
this->data_offset = archive_entry->data_offset;
|
this->data_offset = archive_entry->data_offset;
|
||||||
this->data_length = archive_entry->data_length;
|
this->data_length = archive_entry->data_length;
|
||||||
this->data_cursor = archive_entry->data_offset;
|
this->data_cursor = archive_entry->data_offset;
|
||||||
|
|
||||||
return open_result::ok;
|
return find_result::ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (comparison > 0) {
|
if (comparison > 0) {
|
||||||
|
@ -125,52 +144,71 @@ export namespace oar {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return open_result::not_found;
|
return find_result::not_found;
|
||||||
}
|
}
|
||||||
|
|
||||||
coral::expected<coral::usize, coral::io_error> read(coral::slice<coral::u8> const & buffer) override {
|
/**
|
||||||
if (!this->is_open()) return coral::io_error::unavailable;
|
* Attempts to read `data.length` bytes from the file and fill `data` with it, returning the
|
||||||
|
* number of bytes actually read or a [coral::io_error] value to indicate an error occured.
|
||||||
|
*/
|
||||||
|
coral::expected<coral::usize, coral::io_error> read(coral::slice<coral::u8> const & data) override {
|
||||||
|
if (this->data_offset < sizeof(header)) return coral::io_error::unavailable;
|
||||||
|
|
||||||
if (this->archive_reader->seek(this->data_offset + this->data_cursor).is_ok())
|
coral::usize const data_tail {this->data_offset + this->data_length};
|
||||||
return coral::io_error::unavailable;
|
|
||||||
|
|
||||||
coral::expected const bytes_read{this->archive_reader->read(buffer.sliced(0,
|
if (!this->archive_reader->seek(coral::clamp(this->data_offset + this->data_cursor,
|
||||||
coral::min(buffer.length, static_cast<coral::usize>((
|
this->data_offset, data_tail)).is_ok()) return coral::io_error::unavailable;
|
||||||
this->data_offset + this->data_length) - this->data_cursor))))};
|
|
||||||
|
|
||||||
if (!bytes_read.is_ok()) this->data_cursor += bytes_read.value();
|
coral::expected const data_read {this->archive_reader->read(
|
||||||
|
data.sliced(0, coral::min(data.length, data_tail - this->data_cursor)))};
|
||||||
|
|
||||||
return bytes_read;
|
if (data_read.is_ok()) this->data_cursor += data_read.value();
|
||||||
|
|
||||||
|
return data_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts to seek to `offset` absolute position in the file, returning the new absolute
|
||||||
|
* cursor or a [coral::io_error] value to indicate an error occured.
|
||||||
|
*/
|
||||||
coral::expected<coral::u64, coral::io_error> seek(coral::u64 offset) override {
|
coral::expected<coral::u64, coral::io_error> seek(coral::u64 offset) override {
|
||||||
if (!this->is_open()) return coral::io_error::unavailable;
|
if (this->data_offset < sizeof(header)) return coral::io_error::unavailable;
|
||||||
|
|
||||||
|
this->data_cursor = offset;
|
||||||
|
|
||||||
return coral::io_error::unavailable;
|
return coral::io_error::unavailable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts to read to read the absolute file cursor position, returning it or a
|
||||||
|
* [coral::io_error] value to indicate an error occured.
|
||||||
|
*/
|
||||||
coral::expected<coral::u64, coral::io_error> tell() override {
|
coral::expected<coral::u64, coral::io_error> tell() override {
|
||||||
if (!this->is_open()) return coral::io_error::unavailable;
|
if (this->data_offset < sizeof(header)) return coral::io_error::unavailable;
|
||||||
|
|
||||||
return this->data_cursor;
|
return this->data_cursor;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
coral::file_reader * archive_reader;
|
coral::file_reader * archive_reader {nullptr};
|
||||||
|
|
||||||
coral::u64 data_offset;
|
coral::u64 data_offset {0};
|
||||||
|
|
||||||
coral::u64 data_length;
|
coral::u64 data_length {0};
|
||||||
|
|
||||||
coral::u64 data_cursor;
|
coral::u64 data_cursor {0};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export namespace oar {
|
||||||
struct archive : public coral::fs {
|
struct archive : public coral::fs {
|
||||||
archive(coral::fs * backing_fs, coral::path const & archive_path) {
|
archive(coral::fs * backing_fs, coral::path const & archive_path) {
|
||||||
this->backing_fs = backing_fs;
|
this->backing_fs = backing_fs;
|
||||||
this->archive_path = archive_path;
|
this->archive_path = archive_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Queries the archive for the [coral::fs::access_rules] and returns them.
|
||||||
|
*/
|
||||||
access_rules query_access() override {
|
access_rules query_access() override {
|
||||||
return {
|
return {
|
||||||
.can_read = true,
|
.can_read = true,
|
||||||
|
@ -178,22 +216,29 @@ export namespace oar {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts to open a readable context for reading from the archive file identified by
|
||||||
|
* `file_path`, doing nothing if the requested file could not be found.
|
||||||
|
*/
|
||||||
void read_file(coral::path const & file_path,
|
void read_file(coral::path const & file_path,
|
||||||
coral::closure<void(coral::file_reader &)> const & then) override {
|
coral::closure<void(coral::file_reader &)> const & then) override {
|
||||||
|
|
||||||
if ((this->backing_fs == nullptr) || (this->archive_path.byte_size() == 0)) return;
|
if ((this->backing_fs == nullptr) || (this->archive_path.byte_size() == 0)) return;
|
||||||
|
|
||||||
this->backing_fs->read_file(this->archive_path, [&](coral::file_reader & archive_reader) {
|
this->backing_fs->read_file(this->archive_path,
|
||||||
archive_file_reader file_reader{&archive_reader};
|
[&](coral::file_reader & archive_reader) {
|
||||||
|
archive_file file{&archive_reader};
|
||||||
|
|
||||||
if (file_reader.open(file_path) != archive_file_reader::open_result::ok) return;
|
if (file.find(file_path) != archive_file::find_result::ok) return;
|
||||||
|
|
||||||
then(file_reader);
|
then(file);
|
||||||
|
|
||||||
if (file_reader.close() != archive_file_reader::close_result::ok) return;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts to open a writable context for reading from the archive file identified by
|
||||||
|
* `file_path`, however this will always do nothing as archive file-systems are read-only.
|
||||||
|
*/
|
||||||
void write_file(coral::path const & file_path,
|
void write_file(coral::path const & file_path,
|
||||||
coral::closure<void(coral::file_writer &)> const & then) override {
|
coral::closure<void(coral::file_writer &)> const & then) override {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue