fix non-packed iterator

This commit is contained in:
Motiejus Jakštys 2022-02-22 06:44:58 +02:00 committed by Motiejus Jakštys
parent ab0562950c
commit 26fbfc5f08

View File

@ -19,17 +19,33 @@ pub const PackedUser = packed struct {
// blobSize returns the length of the blob storing string values. // blobSize returns the length of the blob storing string values.
pub fn blobLength(self: *const PackedUser) usize { pub fn blobLength(self: *const PackedUser) usize {
var result: usize = self.home_len; var result: usize = self.realHomeLen();
if (!self.name_is_a_suffix) { if (!self.name_is_a_suffix) {
result += self.name_len; result += self.realNameLen();
} }
result += self.gecos_len; result += self.realGecosLen();
if (self.shell_here) { if (self.shell_here) {
result += self.shell_len_or_idx; result += self.realShellLen();
} }
return result; return result;
} }
pub fn realHomeLen(self: *const PackedUser) usize {
return self.home_len + 1;
}
pub fn realNameLen(self: *const PackedUser) usize {
return self.name_len + 1;
}
pub fn realShellLen(self: *const PackedUser) usize {
return self.shell_len_or_idx + 1;
}
pub fn realGecosLen(self: *const PackedUser) usize {
return self.gecos_len;
}
}; };
const PackedUserAlignmentBits = 3; const PackedUserAlignmentBits = 3;
@ -135,9 +151,9 @@ pub const UserReader = struct {
PackedUser, PackedUser,
it.ur.blob[it.index..endUser][0..packedUserSizeHere], it.ur.blob[it.index..endUser][0..packedUserSizeHere],
); );
const startBlob = endUser + 1; const startBlob = endUser;
const endBlob = startBlob + packedUser.blobLength(); const endBlob = startBlob + packedUser.blobLength();
it.index = pad.roundUp(usize, PackedUserAlignmentBits, endBlob + 1); it.index = pad.roundUp(usize, PackedUserAlignmentBits, endBlob);
return PackedEntry{ return PackedEntry{
.packed_user = packedUser, .packed_user = packedUser,
.blob = it.ur.blob[startBlob..endBlob], .blob = it.ur.blob[startBlob..endBlob],
@ -150,41 +166,43 @@ pub const UserReader = struct {
} }
pub const Iterator = struct { pub const Iterator = struct {
pit: *PackedIterator, pit: PackedIterator,
shellIndex: shellIndexProto, shellIndex: shellIndexProto,
pub fn next(it: *Iterator) ?User { pub fn next(it: *Iterator) ?User {
const entry = it.next() orelse return null; const entry = it.pit.next() orelse return null;
const home = entry.blob[0..entry.packed_user.home_len]; const u = entry.packed_user;
const home = entry.blob[0..u.realHomeLen()];
var name: []const u8 = undefined; var name: []const u8 = undefined;
var pos: usize = undefined; var pos: usize = undefined;
//var name_start: usize = 0; if (u.name_is_a_suffix) {
//var name_end: usize = 0; const name_start = u.realHomeLen() - u.realNameLen();
name = entry.blob[name_start..u.realHomeLen()];
if (entry.packed_user.name_is_a_suffix) { pos = u.home_len + 1;
const name_start = entry.packed_user.home_len - entry.packed_user.name_len;
name = entry.blob[name_start..entry.packed_user.home_len];
pos = entry.packed_user.home_len + 1;
} else { } else {
const name_start = entry.packed_user.home_len + 1; const name_start = u.realHomeLen();
name = entry.blob[name_start .. name_start + entry.packed_user.name_len]; name = entry.blob[name_start .. name_start + u.realNameLen()];
pos = name_start + entry.packed_user.name_len + 1; pos = name_start + u.realNameLen();
} }
const gecos = entry.blob[pos .. pos + u.realGecosLen()];
pos += u.realGecosLen();
var shell: []const u8 = undefined; var shell: []const u8 = undefined;
if (entry.packed_user.shell_here) { if (u.shell_here) {
shell = entry.blob[pos .. pos + entry.packed_user.shell_len_or_idx]; shell = entry.blob[pos .. pos + u.realShellLen()];
pos += entry.packed_user.shell_len_or_idx + 1;
} else { } else {
shell = it.shellIndex(entry.packed_user.shell_len_or_idx); shell = it.shellIndex(u.shell_len_or_idx);
} }
return User{ return User{
.uid = entry.packed_user.uid, .uid = u.uid,
.gid = entry.packed_user.gid, .gid = u.gid,
.name = undefined, .name = name,
.gecos = gecos,
.home = home, .home = home,
.shell = shell,
}; };
} }
}; };
@ -236,29 +254,34 @@ test "construct PackedUser blob" {
defer buf.deinit(); defer buf.deinit();
var writer = UserWriter.init(&buf, testShellIndex); var writer = UserWriter.init(&buf, testShellIndex);
const users = [_]User{ const users = [_]User{ User{
User{
.uid = 1000, .uid = 1000,
.gid = 1000, .gid = 1000,
.name = "vidmantas", .name = "vidmantas",
.gecos = "Vidmantas Kaminskas", .gecos = "Vidmantas Kaminskas",
.home = "/home/vidmantas", .home = "/home/vidmantas",
.shell = "/bin/bash", .shell = "/bin/bash",
}, }, User{
User{
.uid = 1001, .uid = 1001,
.gid = 1001, .gid = 1001,
.name = "svc-foo", .name = "svc-foo",
.gecos = "Service Account", .gecos = "Service Account",
.home = "/home/service1", .home = "/home/service1",
.shell = "/usr/bin/nologin", .shell = "/usr/bin/nologin",
}, }, User{
}; .uid = 1002,
.gid = 1002,
.name = "user-account",
.gecos = "",
.home = "/a",
.shell = "/bin/zsh",
} };
for (users) |user| { for (users) |user| {
try writer.appendUser(user); try writer.appendUser(user);
} }
var rd = UserReader.init(buf.items); var rd = UserReader.init(buf.items);
{
var it = rd.packedIterator(); var it = rd.packedIterator();
var i: u32 = 0; var i: u32 = 0;
while (it.next()) |entry| : (i += 1) { while (it.next()) |entry| : (i += 1) {
@ -266,3 +289,17 @@ test "construct PackedUser blob" {
try testing.expectEqual(users[i].gid, entry.packed_user.gid); try testing.expectEqual(users[i].gid, entry.packed_user.gid);
} }
} }
{
var it = rd.iterator(testShell);
var i: u32 = 0;
while (it.next()) |user| : (i += 1) {
try testing.expectEqual(users[i].uid, user.uid);
try testing.expectEqual(users[i].gid, user.gid);
try testing.expectEqualStrings(users[i].name, user.name);
try testing.expectEqualStrings(users[i].gecos, user.gecos);
try testing.expectEqualStrings(users[i].home, user.home);
try testing.expectEqualStrings(users[i].shell, user.shell);
}
}
}