Compare commits

..

No commits in common. "fce7698b444dfa7195da9a0832f4b7491c652286" and "13fffacd98ea6aade51cff5c14e7bc17ac3bcdbd" have entirely different histories.

3 changed files with 77 additions and 89 deletions

View File

@ -382,13 +382,6 @@ export namespace coral {
} }
} }
/**
* Invokes the `apply` procedure if the optional is not empty, otherwise having no side-effects.
*/
void and_then(closure<void(element &)> const & apply) {
if (this->has_value()) apply(**this);
}
/** /**
* Returns `true` if the optional contains a value, otherwise `false`. * Returns `true` if the optional contains a value, otherwise `false`.
*/ */

View File

@ -145,46 +145,41 @@ export namespace coral {
*/ */
struct fs { struct fs {
/** /**
* Errors that may occur while trying to walk a file tree. * Descriptor of the various rules that the file-system enforces over access to its files.
*
* [walk_error::end_of_walk] reports that there are no more paths left in the file tree that to traverse.
*
* [walk_error::io_unavailable] indicates that an implementation-defined I/O error has occured during traversal
* of the file tree and failed to recover the next file tree path as a result.
*/ */
enum class walk_error { struct access_rules {
end_of_walk, bool can_read;
io_unavailable,
bool can_write;
}; };
/** enum class [[nodiscard]] walk_result {
* Supplier of file paths from a file tree walking context created by calling [walk_files]. ok,
* not_implemented,
* Each subsequent invocation will produce either a valid path in the file tree from `target_path` or a access_denied,
* [walk_error], with [walk_error::end_of_walk] signalling that there are no more paths left in the file tree to not_found,
* traverse. See [walk_error] for more details on potential errors. io_error,
*/ };
using walker = closure<expected<path, walk_error>()>;
virtual ~fs() {}; virtual ~fs() {};
virtual walk_result walk_files(path const & target_path, closure<bool(path const &)> const & apply) {
return walk_result::not_implemented;
}
/** /**
* Attempts to read the file in `target_path`, calling `then` if it was successfully opened for reading and * Queries the file-system for its global [access_rules], returning them.
* passing the [file_reader] context along. */
virtual access_rules query_access() = 0;
/**
* Attempts to read the file in `file_path`, calling `then` if it was successfully opened for reading.
*/ */
virtual void read_file(path const & target_path, closure<void(file_reader &)> const & then) {} virtual void read_file(path const & target_path, closure<void(file_reader &)> const & then) {}
/** /**
* Attempts to walk the file tree from `target_path`, calling `then` if it was successfully opened for walking * Attempts to write the file in the file system located at `file_path`, calling `then` if it was successfully
* and passing the [walker] context along. * opened for writing.
*
* See [walker] for more information on how to use it to traverse the file tree.
*/
virtual void walk_files(path const & target_path, closure<void(walker const &)> const & then) {}
/**
* Attempts to write the file in the file system located at `target_path`, calling `then` if it was successfully
* opened for writing and passing the [file_writer] context along.
*/ */
virtual void write_file(path const & target_path, closure<void(file_writer &)> const & then) {} virtual void write_file(path const & target_path, closure<void(file_writer &)> const & then) {}
}; };

View File

@ -220,42 +220,69 @@ export namespace oar {
this->archive_path = archive_path; this->archive_path = archive_path;
} }
/** walk_result walk_files(path const & target_path, closure<bool(path const &)> const & apply) override {
* See [fs::walk_files]. bool not_found {false};
*/ bool has_io_error {false};
void walk_files(path const & target_path, closure<void(walker const &)> const & then) override {
this->backing_fs->read_file(this->archive_path, [&](file_reader & archive_reader) { this->backing_fs->read_file(this->archive_path, [&](file_reader & archive_reader) {
entry archive_entry{&archive_reader}; entry archive_entry{&archive_reader};
if (archive_entry.find(entry_kind::directory, target_path) != entry::find_result::ok) return; if (archive_entry.find(entry_kind::directory, target_path) != entry::find_result::ok) {
not_found = true;
then([&]() -> expected<path, walk_error> { return;
}
for (;;) {
constexpr usize path_size {sizeof(path)}; constexpr usize path_size {sizeof(path)};
u8 path_buffer[path_size] {0}; u8 path_buffer[path_size] {0};
expected const data_read {archive_entry.read(path_buffer)};
// Read verify integrity. if (data_read.is_error()) {
{ has_io_error = true;
expected const data_read {archive_entry.read(path_buffer)};
if (data_read.is_error()) return walk_error::io_unavailable; return;
}
switch (*data_read.ok()) { if (usize const data_read_value {*data_read.ok()}; data_read_value != path_size) {
case path_size: break; if (data_read_value != 0) has_io_error = true;
case 0: return walk_error::end_of_walk;
default: return walk_error::io_unavailable; return;
}
} }
// Verify existence of zero terminator in path. // Verify existence of zero terminator in path.
if (!coral::find_last(path_buffer, 0).has_value()) return walk_error::io_unavailable; if (!coral::find_last(path_buffer, 0).has_value()) {
has_io_error = true;
return {*reinterpret_cast<path const *>(path_buffer)}; return;
}); }
if (archive_entry.read(path_buffer).map<bool>(coral::equality_predicate(path_size)).ok_or(false))
if (!apply(*reinterpret_cast<path const *>(path_buffer))) return;
}
}); });
if (not_found) return walk_result::not_found;
if (has_io_error) return walk_result::io_error;
return walk_result::ok;
} }
/** /**
* See [fs::read_file]. * Queries the archive for the [fs::access_rules] and returns them.
*/
access_rules query_access() override {
return {
.can_read = true,
.can_write = false,
};
}
/**
* 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(path const & file_path, closure<void(file_reader &)> const & then) override { void read_file(path const & file_path, closure<void(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;
@ -290,43 +317,17 @@ export namespace oar {
// Walk input dir to create blocks for all files needed. // Walk input dir to create blocks for all files needed.
{ {
bool has_io_error {false}; bool has_memory {true};
bool is_out_of_memory {false};
input_fs.walk_files(input_path, [&](fs::walker const & walk) { if (input_fs.walk_files(input_path, [&](path const & entry_path) -> bool {
coral::expected walked_path {walk()}; has_memory = archive_blocks.push({.layout = {.path = entry_path}}) == coral::push_result::ok;
while (walked_path.is_ok()) { return !has_memory;
is_out_of_memory = archive_blocks.push({.layout = {
.path = *walked_path.ok()
}}) != coral::push_result::ok;
if (is_out_of_memory) return; file_count += 1;
}) != fs::walk_result::ok) return bundle_result::io_error;
file_count += 1; if (!has_memory) return bundle_result::out_of_memory;
walked_path = walk();
}
walked_path.error().and_then([&](fs::walk_error walk_error) {
switch (walk_error) {
case fs::walk_error::io_unavailable: {
has_io_error = true;
return;
}
case fs::walk_error::end_of_walk: {
has_io_error = true;
return;
}
}
});
});
if (has_io_error) return bundle_result::io_error;
if (is_out_of_memory) return bundle_result::out_of_memory;
if (file_count > coral::u32_max) return bundle_result::too_many_files; if (file_count > coral::u32_max) return bundle_result::too_many_files;
} }
@ -340,7 +341,6 @@ export namespace oar {
coral::copy(archive_header.layout.signature, signature_magic); coral::copy(archive_header.layout.signature, signature_magic);
// This was safety-checked during the initial file tree walk step.
archive_header.layout.entry_count = static_cast<coral::u32>(file_count); archive_header.layout.entry_count = static_cast<coral::u32>(file_count);
if (!archive_writer.write(archive_header.bytes).map<bool>(header::is_sizeof).ok_or(false)) { if (!archive_writer.write(archive_header.bytes).map<bool>(header::is_sizeof).ok_or(false)) {