diff --git a/src/header.zig b/src/header.zig index c49278d..16102bd 100644 --- a/src/header.zig +++ b/src/header.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const shells = @import("shell.zig"); +const shell = @import("shell.zig"); const HeaderSize = @sizeOf(Header); const Magic = [4]u8{ 0xf0, 0x9f, 0xa4, 0xb7 }; @@ -43,7 +43,7 @@ const Header = packed struct { if (self.bom != Bom) { return error.InvalidBom; } - if (self.num_shells > shells.MaxShells) { + if (self.num_shells > shell.MaxShells) { return error.TooManyShells; } @@ -116,7 +116,7 @@ test "header pack, unpack and validation" { { var header = goodHeader; - header.num_shells = shells.MaxShells + 1; + header.num_shells = shell.MaxShells + 1; try testing.expectError(error.TooManyShells, Header.init(header.asArray())); } diff --git a/src/user.zig b/src/user.zig index e611a1e..a5c42f3 100644 --- a/src/user.zig +++ b/src/user.zig @@ -1,4 +1,7 @@ const std = @import("std"); +const shell = @import("shell.zig"); + +const Allocator = std.mem.Allocator; pub const PackedUserSize = @sizeOf(PackedUser); pub const PackedUser = packed struct { @@ -13,6 +16,53 @@ pub const PackedUser = packed struct { gecos_len: u8, }; +pub const User = struct { + uid: u32, + gid: u32, + name: []const u8, + gecos: []const u8, + home: []const u8, + shell: []const u8, +}; + +// UserWriter accepts a naive User struct and returns a PackedUser +pub const UserWriter = struct { + allocator: Allocator, + + pub fn init(allocator: Allocator) UserWriter { + return UserWriter{ + .allocator = allocator, + }; + } + + pub fn fromUser(user: User, shellw: shell.ShellWriter) !PackedUser { + var shell_here: u1 = undefined; + var shell_len_or_place: u6 = undefined; + if (shellw.getIndex(user.shell)) |idx| { + shell_here = false; + shell_len_or_place = idx; + } else { + shell_here = true; + shell_len_or_place = user.shell.len; + } + + var puser = PackedUser{ + .uid = user.uid, + .gid = user.gid, + .additional_gids_offset = 0, // second pass + .shell_here = shell_here, + .shell_len_or_place = shell_len_or_place, + .homedir_len = undefined, + .username_is_a_suffix = undefined, + .username_offset_or_len = undefined, + .gecos_len = undefined, + }; + + _ = shellw; + return puser; + } +}; + const testing = std.testing; test "PackedUser is byte-aligned" {