ona/source/oar.cpp

254 lines
7.3 KiB
C++
Raw Normal View History

2023-02-19 17:43:30 +01:00
export module oar;
2023-02-19 17:50:29 +01:00
import coral;
import coral.files;
2023-02-19 17:43:30 +01:00
2023-02-24 01:35:36 +01:00
/**
* Length of the full magic signature at the beginning of an Oar file.
*/
constexpr coral::usize signature_length {4};
/**
* 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};
/**
* Hardcoded signature magic value that this implementation of Oar expects when reading archives.
*/
constexpr coral::u8 signature_magic[signature_length] {'o', 'a', 'r', 0};
/**
* Oar file header format.
*/
struct header {
coral::u8 signature_magic[signature_length];
coral::u32 entry_count;
coral::u8 padding[504];
};
static_assert(sizeof(header) == 512);
2023-02-24 01:35:36 +01:00
/**
* Oar file header format.
*/
struct entry {
coral::path path;
coral::u64 data_offset;
coral::u64 data_length;
coral::u8 padding[240];
};
static_assert(sizeof(entry) == 512);
2023-02-24 01:35:36 +01:00
/**
* Archive file access interface.
*/
struct archive_file : public coral::file_reader {
/**
* Results of a find operation performed on an [archive_file].
*
* [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,
io_unavailable,
archive_invalid,
archive_unsupported,
not_found,
};
2023-02-24 01:35:36 +01:00
archive_file(coral::file_reader * archive_reader) {
this->archive_reader = archive_reader;
}
2023-02-24 01:35:36 +01:00
/**
* 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_length = 0;
this->data_cursor = 0;
2023-02-24 01:35:36 +01:00
if (!this->archive_reader->seek(0).is_ok()) return find_result::io_unavailable;
2023-02-24 01:35:36 +01:00
constexpr coral::usize header_size {sizeof(header)};
coral::u8 archive_header_buffer[header_size] {0};
2023-02-24 01:35:36 +01:00
if (!this->archive_reader->read(archive_header_buffer).and_test(
[](coral::usize value) -> bool { return value == header_size; }))
return find_result::archive_invalid;
2023-02-24 01:35:36 +01:00
header const * const archive_header {
reinterpret_cast<header const *>(archive_header_buffer)};
2023-02-24 01:35:36 +01:00
if (!coral::equals({archive_header->signature_magic, signature_identifier_length},
{signature_magic, signature_identifier_length})) return find_result::archive_invalid;
2023-02-24 01:35:36 +01:00
if (archive_header->signature_magic[signature_identifier_length] !=
signature_magic[signature_identifier_length]) return find_result::archive_unsupported;
2023-02-24 01:35:36 +01:00
// Read file table.
coral::u64 head {0};
coral::u64 tail {archive_header->entry_count - 1};
constexpr coral::usize entry_size {sizeof(entry)};
coral::u8 archive_entry_buffer[entry_size] {0};
2023-02-24 01:35:36 +01:00
while (head <= tail) {
coral::u64 const midpoint {head + ((tail - head) / 2)};
2023-02-24 01:35:36 +01:00
if (!this->archive_reader->seek(header_size + (entry_size * midpoint)).is_ok())
return find_result::archive_invalid;
2023-02-24 01:35:36 +01:00
if (!this->archive_reader->read(archive_entry_buffer).and_test(
[](coral::usize value) -> bool { return value == entry_size; }))
return find_result::archive_invalid;
2023-02-19 17:43:30 +01:00
2023-02-24 01:35:36 +01:00
entry const * const archive_entry {
reinterpret_cast<entry const *>(archive_entry_buffer)};
2023-02-19 17:43:30 +01:00
2023-02-24 01:35:36 +01:00
coral::size const comparison {file_path.compare(archive_entry->path)};
2023-02-19 17:43:30 +01:00
2023-02-24 01:35:36 +01:00
if (comparison == 0) {
this->data_offset = archive_entry->data_offset;
this->data_length = archive_entry->data_length;
this->data_cursor = archive_entry->data_offset;
2023-02-24 01:35:36 +01:00
return find_result::ok;
}
2023-02-24 01:35:36 +01:00
if (comparison > 0) {
head = (midpoint + 1);
} else {
tail = (midpoint - 1);
}
}
2023-02-24 01:35:36 +01:00
return find_result::not_found;
}
2023-02-24 01:35:36 +01:00
/**
* 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;
2023-02-24 01:35:36 +01:00
coral::usize const data_tail {this->data_offset + this->data_length};
2023-02-24 01:35:36 +01:00
if (!this->archive_reader->seek(coral::clamp(this->data_offset + this->data_cursor,
this->data_offset, data_tail)).is_ok()) return coral::io_error::unavailable;
2023-02-24 01:35:36 +01:00
coral::expected const data_read {this->archive_reader->read(
data.sliced(0, coral::min(data.length, data_tail - this->data_cursor)))};
2023-02-24 01:35:36 +01:00
if (data_read.is_ok()) this->data_cursor += data_read.value();
2023-02-24 01:35:36 +01:00
return data_read;
}
2023-02-24 01:35:36 +01:00
/**
* 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 {
if (this->data_offset < sizeof(header)) return coral::io_error::unavailable;
2023-02-24 01:35:36 +01:00
this->data_cursor = offset;
2023-02-24 01:35:36 +01:00
return coral::io_error::unavailable;
}
2023-02-24 01:35:36 +01:00
/**
* 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 {
if (this->data_offset < sizeof(header)) return coral::io_error::unavailable;
2023-02-24 01:35:36 +01:00
return this->data_cursor;
}
2023-02-24 01:35:36 +01:00
private:
coral::file_reader * archive_reader {nullptr};
2023-02-19 17:43:30 +01:00
2023-02-24 01:35:36 +01:00
coral::u64 data_offset {0};
2023-02-19 17:43:30 +01:00
2023-02-24 01:35:36 +01:00
coral::u64 data_length {0};
2023-02-19 17:43:30 +01:00
2023-02-24 01:35:36 +01:00
coral::u64 data_cursor {0};
};
2023-02-19 17:43:30 +01:00
2023-02-24 01:35:36 +01:00
export namespace oar {
2023-02-19 17:50:29 +01:00
struct archive : public coral::fs {
archive(coral::fs * backing_fs, coral::path const & archive_path) {
this->backing_fs = backing_fs;
this->archive_path = archive_path;
2023-02-19 17:43:30 +01:00
}
2023-02-24 01:35:36 +01:00
/**
* Queries the archive for the [coral::fs::access_rules] and returns them.
*/
2023-02-23 15:02:17 +01:00
access_rules query_access() override {
return {
.can_read = true,
.can_write = false,
};
}
2023-02-24 01:35:36 +01:00
/**
* 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.
*/
2023-02-19 18:16:43 +01:00
void read_file(coral::path const & file_path,
coral::closure<void(coral::file_reader &)> const & then) override {
2023-02-24 01:35:36 +01:00
if ((this->backing_fs == nullptr) || (this->archive_path.byte_size() == 0)) return;
2023-02-24 01:35:36 +01:00
this->backing_fs->read_file(this->archive_path,
[&](coral::file_reader & archive_reader) {
archive_file file{&archive_reader};
2023-02-24 01:35:36 +01:00
if (file.find(file_path) != archive_file::find_result::ok) return;
2023-02-24 01:35:36 +01:00
then(file);
});
}
2023-02-19 17:43:30 +01:00
2023-02-24 01:35:36 +01:00
/**
* 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.
*/
2023-02-19 18:16:43 +01:00
void write_file(coral::path const & file_path,
coral::closure<void(coral::file_writer &)> const & then) override {
2023-02-24 01:35:36 +01:00
// Read-only file system.
}
private:
coral::fs * backing_fs;
coral::path archive_path;
2023-02-19 17:43:30 +01:00
};
}