filling packed user

This commit is contained in:
Motiejus Jakštys 2022-02-18 20:29:45 +02:00 committed by Motiejus Jakštys
parent eacfc08592
commit da727113e5
2 changed files with 53 additions and 3 deletions

View File

@ -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()));
}

View File

@ -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" {