36 lines
838 B
Zig
36 lines
838 B
Zig
const std = @import("std");
|
|
const os = std.os;
|
|
|
|
const DB = @import("DB.zig");
|
|
const InvalidHeader = @import("header.zig").Invalid;
|
|
|
|
const File = @This();
|
|
|
|
db: DB,
|
|
ptr: []align(4096) const u8,
|
|
|
|
pub const Error = os.OpenError || os.FStatError || os.MMapError || InvalidHeader;
|
|
pub fn open(fname: []const u8) Error!File {
|
|
const fd = try os.open(fname, os.O.RDONLY, 0);
|
|
var fd_open = true;
|
|
errdefer {
|
|
if (fd_open) os.close(fd);
|
|
}
|
|
|
|
const st = try os.fstat(fd);
|
|
const size = @intCast(u64, st.size);
|
|
const ptr = try os.mmap(null, size, os.PROT.READ, os.MAP.SHARED, fd, 0);
|
|
errdefer os.munmap(ptr);
|
|
|
|
os.close(fd);
|
|
fd_open = false;
|
|
|
|
const db = try DB.fromBytes(ptr);
|
|
return File{ .db = db, .ptr = ptr };
|
|
}
|
|
|
|
pub fn close(self: *File) void {
|
|
os.munmap(self.ptr);
|
|
self.* = undefined;
|
|
}
|