@@ -8,7 +8,7 @@ const Types = @import("../types.zig");
pub const User = @This();
pub const TYPE_PREFIX = "users";
const USER_VERSION: usize = 0;
const USER_VERSION: usize = 1;
var datad: std.fs.Dir = undefined;
pub fn init(_: []const u8) !void {}
@@ -16,13 +16,39 @@ pub fn initType(stor: Types.Storage) !void {
datad = stor;
}
pub fn readVersioned(a: Allocator, file: std.fs.File) !User {
mtls_fp: [40]u8 = .{0} ** 40,
not_before: i64,
not_after: i64,
username: []const u8,
pub fn findMTLSFingerprint(a: Allocator, fp: []const u8) !User {
if (fp.len != 40) return error.InvalidFingerprint;
const file = try openFile(fp);
return readFile(a, file);
}
pub fn open(a: Allocator, username: []const u8) !User {
for (username) |c| if (!std.ascii.isLower(c)) return error.InvalidUsername;
const ufile = try openFile(username);
return try readFile(a, ufile);
}
pub fn commit(self: User) !User {
const file = try openFile(self.mtls_fp);
const w = file.writer().any();
try self.writeOut(w);
}
fn readVersioned(a: Allocator, file: std.fs.File) !User {
var reader = file.reader();
const ver: usize = try reader.readInt(usize, endian);
switch (ver) {
0 => {
return User{
.mtls_fp = try reader.readBytesNoEof(40),
.not_before = std.math.minInt(i64),
.not_after = std.math.maxInt(i64),
.username = try reader.readUntilDelimiterAlloc(a, 0, 0xFFF),
};
},
@@ -30,10 +56,13 @@ pub fn readVersioned(a: Allocator, file: std.fs.File) !User {
}
}
mtls_fp: [40]u8 = .{0} ** 40,
username: []const u8,
fn openFile(fp: []const u8) !std.fs.File {
var buf: [2048]u8 = undefined;
const filename = try std.fmt.bufPrint(&buf, "{s}.fp", .{fp});
return try datad.createFile(filename, .{ .read = true, .truncate = false });
}
pub fn readFile(a: Allocator, file: std.fs.File) !User {
fn readFile(a: Allocator, file: std.fs.File) !User {
defer file.close();
return readVersioned(a, file);
}
@@ -47,20 +76,6 @@ pub fn writeOut(_: User) !void {
}
pub fn new() !User {
// TODO implement ln username -> fp
return error.NotImplemnted;
}
pub fn findMTLSFingerprint(a: Allocator, fp: []const u8) !User {
if (fp.len != 40) return error.InvalidFingerprint;
var fpfbuf: [43]u8 = undefined;
const fname = try std.fmt.bufPrint(&fpfbuf, "{s}.fp", .{fp});
const file = datad.openFile(fname, .{}) catch return error.UserNotFound;
return User.readFile(a, file);
}
pub fn open(a: Allocator, username: []const u8) !User {
for (username) |c| if (!std.ascii.isLower(c)) return error.InvalidUsername;
const ufile = datad.openFile(username, .{}) catch return error.UserNotFound;
return try User.readFile(a, ufile);
}