beginnings of using ErrCtx
This commit is contained in:
parent
eb6003c957
commit
675125be05
@ -12,8 +12,10 @@ const ErrCtx = @This();
|
|||||||
buf: BoundedArray(u8, capacity) =
|
buf: BoundedArray(u8, capacity) =
|
||||||
BoundedArray(u8, capacity).init(0) catch unreachable,
|
BoundedArray(u8, capacity).init(0) catch unreachable,
|
||||||
overflow: bool = false,
|
overflow: bool = false,
|
||||||
|
dirty: bool = false,
|
||||||
|
|
||||||
pub fn write(self: *ErrCtx, bytes: []const u8) error{}!usize {
|
pub fn write(self: *ErrCtx, bytes: []const u8) error{}!usize {
|
||||||
|
self.dirty = true;
|
||||||
const can_add = capacity - self.buf.len;
|
const can_add = capacity - self.buf.len;
|
||||||
if (can_add == 0) return bytes.len;
|
if (can_add == 0) return bytes.len;
|
||||||
|
|
||||||
@ -32,6 +34,10 @@ pub fn writer(self: *ErrCtx) Writer {
|
|||||||
return Writer{ .context = self };
|
return Writer{ .context = self };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn err(self: *ErrCtx, comptime format: []const u8, args: anytype) void {
|
||||||
|
self.writer().print(format, args) catch unreachable;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn iterator(self: *const ErrCtx) mem.SplitIterator(u8) {
|
pub fn iterator(self: *const ErrCtx) mem.SplitIterator(u8) {
|
||||||
const slice = self.buf.constSlice();
|
const slice = self.buf.constSlice();
|
||||||
const last_byte = if (slice[slice.len - 1] == '0') slice.len - 1 else slice.len;
|
const last_byte = if (slice[slice.len - 1] == '0') slice.len - 1 else slice.len;
|
||||||
|
59
src/User.zig
59
src/User.zig
@ -46,34 +46,51 @@ pub fn clone(
|
|||||||
|
|
||||||
// fromLine accepts a line of /etc/passwd (with or without the EOL) and makes a
|
// fromLine accepts a line of /etc/passwd (with or without the EOL) and makes a
|
||||||
// User.
|
// User.
|
||||||
fn fromLine(allocator: Allocator, err_ctx: anytype, line: []const u8) error{ InvalidRecord, OutOfMemory }!User {
|
fn fromLine(allocator: Allocator, err_ctx: *ErrCtx, line: []const u8) error{ InvalidRecord, OutOfMemory }!User {
|
||||||
_ = err_ctx;
|
|
||||||
var it = mem.split(u8, line, ":");
|
var it = mem.split(u8, line, ":");
|
||||||
const name = it.next() orelse return error.InvalidRecord;
|
const name = it.next();
|
||||||
_ = it.next() orelse return error.InvalidRecord; // password
|
_ = it.next(); // password
|
||||||
const uids = it.next() orelse return error.InvalidRecord;
|
const uids = it.next();
|
||||||
const gids = it.next() orelse return error.InvalidRecord;
|
const gids = it.next();
|
||||||
const gecos = it.next() orelse return error.InvalidRecord;
|
const gecos = it.next();
|
||||||
const home = it.next() orelse return error.InvalidRecord;
|
const home = it.next();
|
||||||
const shell = it.next() orelse return error.InvalidRecord;
|
const shell = it.next();
|
||||||
|
|
||||||
|
// all fields are set
|
||||||
|
if (shell == null) {
|
||||||
|
err_ctx.err("too few user fields in line: {s}", .{line});
|
||||||
|
return error.InvalidRecord;
|
||||||
|
}
|
||||||
|
|
||||||
// the line must be exhaustive.
|
// the line must be exhaustive.
|
||||||
if (it.next() != null) return error.InvalidRecord;
|
if (it.next() != null)
|
||||||
|
return error.InvalidRecord;
|
||||||
|
|
||||||
const uid = fmt.parseInt(u32, uids, 10) catch return error.InvalidRecord;
|
const uid = fmt.parseInt(u32, uids.?, 10) catch {
|
||||||
const gid = fmt.parseInt(u32, gids, 10) catch return error.InvalidRecord;
|
err_ctx.err("bad uid: {s}", .{uids.?});
|
||||||
|
return error.InvalidRecord;
|
||||||
|
};
|
||||||
|
|
||||||
try validate.utf8(name);
|
const gid = fmt.parseInt(u32, gids.?, 10) catch {
|
||||||
try validate.utf8(gecos);
|
err_ctx.err("bad uid: {s}", .{gids.?});
|
||||||
try validate.utf8(home);
|
return error.InvalidRecord;
|
||||||
try validate.utf8(shell);
|
};
|
||||||
|
|
||||||
|
validate.utf8(name.?) catch err_ctx.err("name is invalid utf8: {s}", .{name.?});
|
||||||
|
validate.utf8(gecos.?) catch err_ctx.err("gecos is invalid utf8: {s}", .{gecos.?});
|
||||||
|
validate.utf8(home.?) catch err_ctx.err("home is invalid utf8: {s}", .{home.?});
|
||||||
|
validate.utf8(shell.?) catch err_ctx.err("shell is invalid utf8: {s}", .{shell.?});
|
||||||
|
|
||||||
|
if (err_ctx.dirty)
|
||||||
|
return error.InvalidRecord;
|
||||||
|
|
||||||
const user = User{
|
const user = User{
|
||||||
.uid = uid,
|
.uid = uid,
|
||||||
.gid = gid,
|
.gid = gid,
|
||||||
.name = name,
|
.name = name.?,
|
||||||
.gecos = gecos,
|
.gecos = gecos.?,
|
||||||
.home = home,
|
.home = home.?,
|
||||||
.shell = shell,
|
.shell = shell.?,
|
||||||
};
|
};
|
||||||
return try user.clone(allocator);
|
return try user.clone(allocator);
|
||||||
}
|
}
|
||||||
@ -124,7 +141,7 @@ pub fn deinit(self: *User, allocator: Allocator) void {
|
|||||||
self.* = undefined;
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fromReader(allocator: Allocator, err_ctx: anytype, reader: anytype) ![]User {
|
pub fn fromReader(allocator: Allocator, err_ctx: *ErrCtx, reader: anytype) ![]User {
|
||||||
var users = ArrayList(User).init(allocator);
|
var users = ArrayList(User).init(allocator);
|
||||||
errdefer {
|
errdefer {
|
||||||
for (users.items) |*user| user.deinit(allocator);
|
for (users.items) |*user| user.deinit(allocator);
|
||||||
|
@ -73,12 +73,16 @@ fn execute(
|
|||||||
defer groupFile.close();
|
defer groupFile.close();
|
||||||
|
|
||||||
var err_ctx = ErrCtx{};
|
var err_ctx = ErrCtx{};
|
||||||
|
|
||||||
var users = try User.fromReader(
|
var users = try User.fromReader(
|
||||||
allocator,
|
allocator,
|
||||||
err_ctx.writer(),
|
&err_ctx,
|
||||||
passwdFile.reader(),
|
passwdFile.reader(),
|
||||||
);
|
);
|
||||||
|
errdefer {
|
||||||
|
var it = err_ctx.iterator();
|
||||||
|
while (it.next()) |err|
|
||||||
|
std.debug.print("ERROR: {s}\n", .{err});
|
||||||
|
}
|
||||||
defer for (users) |*user| user.deinit(allocator);
|
defer for (users) |*user| user.deinit(allocator);
|
||||||
defer allocator.free(users);
|
defer allocator.free(users);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user