PackedUser is now readable

This commit is contained in:
Motiejus Jakštys 2022-02-24 17:33:27 +02:00 committed by Motiejus Jakštys
parent 0f43f8b2d0
commit 49a7d79b05

View File

@ -124,9 +124,8 @@ pub const PackedUser = struct {
// packTo packs the User record and copies it to the given byte slice. The // packTo packs the User record and copies it to the given byte slice. The
// slice must have at least maxRecordSize() bytes available. // slice must have at least maxRecordSize() bytes available.
pub fn packTo(buf1: *[]u8, user: User, shellIndexFn: shellIndexFnType) error{InvalidRecord}!void { pub fn packTo(buf1: *[]u8, user: User, shellIndexFn: shellIndexFnType) error{InvalidRecord}!void {
std.debug.print("\nbuf1.len: {d}\n", .{buf1.*.len});
var buf = buf1.*; var buf = buf1.*;
const bufStart = @ptrToInt(&buf[0]); const bufStart = @ptrToInt(buf.ptr);
const home_len = try downCast(u6, user.home.len - 1); const home_len = try downCast(u6, user.home.len - 1);
const name_len = try downCast(u5, user.name.len - 1); const name_len = try downCast(u5, user.name.len - 1);
@ -150,27 +149,32 @@ pub const PackedUser = struct {
.gecos_len = gecos_len, .gecos_len = gecos_len,
}; };
const innerBytes = mem.asBytes(&inner); const innerBytes = mem.asBytes(&inner);
buf.len += innerBytes.len +
user.home.len +
user.gecos.len;
mem.copy(u8, buf, innerBytes); mem.copy(u8, buf[0..innerBytes.len], innerBytes);
buf = buf[innerBytes.len..]; buf = buf[innerBytes.len..];
mem.copy(u8, buf, user.home); mem.copy(u8, buf[0..user.home.len], user.home);
buf = buf[user.home.len..]; buf = buf[user.home.len..];
if (!inner.name_is_a_suffix) { if (!inner.name_is_a_suffix) {
mem.copy(u8, buf, user.name); buf.len += user.name.len;
mem.copy(u8, buf[0..user.name.len], user.name);
buf = buf[user.name.len..]; buf = buf[user.name.len..];
} }
mem.copy(u8, buf, user.gecos); mem.copy(u8, buf, user.gecos);
buf = buf[user.gecos.len..]; buf = buf[user.gecos.len..];
if (inner.shell_here) { if (inner.shell_here) {
mem.copy(u8, buf, user.shell); buf.len += user.shell.len;
mem.copy(u8, buf[0..user.shell.len], user.shell);
buf = buf[user.shell.len..]; buf = buf[user.shell.len..];
} }
_ = bufStart; const bufLen = @ptrToInt(buf.ptr) - bufStart;
//const bufLen = @ptrToInt(&buf[0]) - bufStart; const padding = pad.roundUpPadding(u64, PackedUserAlignmentBits, bufLen);
//const padding = pad.roundUpPadding(u64, PackedUserAlignmentBits, bufLen); buf.len += padding;
//mem.set(u8, buf[0..padding], 0); mem.set(u8, buf[0..padding], 0);
} }
// maxSize is the maximum number of records a PackedUser can take // maxSize is the maximum number of records a PackedUser can take
@ -358,10 +362,7 @@ test "construct PackedUser section" {
.shell = "s" ** 64, .shell = "s" ** 64,
} }; } };
for (users) |user| { for (users) |user| {
std.debug.print("\nmaxSize: {d}\n", .{PackedUser.maxSize()});
try buf.ensureUnusedCapacity(PackedUser.maxSize()); try buf.ensureUnusedCapacity(PackedUser.maxSize());
buf.items[0] = 1;
std.debug.print("\nbuf.items.len: {d}\n", .{buf.items.len});
try PackedUser.packTo(&buf.items, user, testShellIndex); try PackedUser.packTo(&buf.items, user, testShellIndex);
} }