2022-10-09 23:10:13 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const sys = @import("./sys.zig");
|
|
|
|
|
|
|
|
///
|
|
|
|
/// An entry block of an Oar archive file.
|
|
|
|
///
|
|
|
|
/// Typically, following this block in memory is the file data it holds the meta-information for.
|
|
|
|
///
|
|
|
|
pub const Entry = extern struct {
|
2022-10-11 01:15:19 +01:00
|
|
|
signature: [3]u8 = signature_magic,
|
2022-10-11 01:03:02 +01:00
|
|
|
revision: u8,
|
2022-10-11 01:15:19 +01:00
|
|
|
name_length: u8 = 0,
|
2022-10-09 23:10:13 +01:00
|
|
|
name_buffer: [255]u8 = std.mem.zeroes([255]u8),
|
|
|
|
file_size: u64,
|
2022-10-11 01:15:19 +01:00
|
|
|
padding: [244]u8 = std.mem.zeroes([244]u8),
|
2022-10-11 01:03:02 +01:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Returns `true` if `entry` correctly identifies itself as a valid Oar entry, otherwise
|
|
|
|
/// `false`.
|
|
|
|
///
|
|
|
|
pub fn isValid(entry: Entry) bool {
|
2022-10-11 01:15:19 +01:00
|
|
|
return std.mem.eql(u8, &entry.signature, signature_magic[0 ..]);
|
2022-10-11 01:03:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Attempts to read an [Entry] from `file_access` at its current cursor position.
|
|
|
|
///
|
|
|
|
/// Returns the read [Entry] value or `null` if the end of file is reached before completing the
|
|
|
|
/// read.
|
|
|
|
///
|
|
|
|
pub fn read(file_access: *sys.FileAccess) sys.FileAccess.Error!?Entry {
|
|
|
|
var entry = std.mem.zeroes(Entry);
|
|
|
|
const origin = try file_access.queryCursor();
|
|
|
|
|
|
|
|
if ((try file_access.read(std.mem.asBytes(&entry))) != @sizeOf(Entry)) {
|
|
|
|
try file_access.seek(origin);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Magic identifier used to validate [Entry] data.
|
|
|
|
///
|
2022-10-11 01:15:19 +01:00
|
|
|
const signature_magic = [3]u8{'o', 'a', 'r'};
|
2022-10-09 23:10:13 +01:00
|
|
|
};
|