1
Fork 0

replace PackedUserHash with simple PackedUser

passing hashes is OK
This commit is contained in:
Motiejus Jakštys 2022-03-10 18:59:07 +02:00 committed by Motiejus Jakštys
parent c78a2c0def
commit 1751f6c996
2 changed files with 196 additions and 203 deletions

View File

@ -251,13 +251,13 @@ pub fn usersSection(
const userOffset = try math.cast(u32, blob.items.len);
std.debug.assert(userOffset & 7 == 0);
idx2offset[i] = userOffset;
try userImport.PackedUserHash.packTo(
try userImport.PackedUser.packTo(
&blob,
user,
gids.idx2offset[i],
shells.indices,
);
try pad.arrayList(&blob, userImport.PackedUserHash.alignment_bits);
try pad.arrayList(&blob, userImport.PackedUser.alignment_bits);
}
return UsersSection{
.idx2offset = idx2offset,

View File

@ -10,6 +10,7 @@ const mem = std.mem;
const math = std.math;
const Allocator = mem.Allocator;
const ArrayList = std.ArrayList;
const StringHashMap = std.StringHashMap;
// Idx2ShellProto is a function prototype that, given a shell's index (in
// global shell section), will return a shell string. Matches ShellReader.get.
@ -78,10 +79,7 @@ pub fn Shell2Index(T: type) type {
};
}
pub const PackedUserHash = packedUser(std.StringHashMap(u6));
fn packedUser(comptime ShellIndexType: type) type {
return struct {
pub const PackedUser = struct {
const Self = @This();
pub const alignment_bits = 3;
@ -205,7 +203,7 @@ fn packedUser(comptime ShellIndexType: type) type {
arr: *ArrayList(u8),
user: User,
additional_gids_offset: u64,
idxFn: ShellIndexType,
idxFn: StringHashMap(u6),
) error{ InvalidRecord, OutOfMemory }!void {
std.debug.assert(arr.items.len & 7 == 0);
// function arguments are consts. We need to mutate the underlying
@ -279,30 +277,23 @@ fn packedUser(comptime ShellIndexType: type) type {
}
return idxFn(self.inner.shell_len_or_idx);
}
};
}
};
const testing = std.testing;
test "PackedUser internal and external alignment" {
try testing.expectEqual(
@sizeOf(PackedUserHash.Inner) * 8,
@bitSizeOf(PackedUserHash.Inner),
@sizeOf(PackedUser.Inner) * 8,
@bitSizeOf(PackedUser.Inner),
);
}
const TestShellIndex = struct {
pub fn get(_: *const TestShellIndex, shell: []const u8) ?u6 {
if (mem.eql(u8, shell, "/bin/bash")) {
return 0;
} else if (mem.eql(u8, shell, "/bin/zsh")) {
return 1;
}
return null;
}
};
const PackedUserTest = packedUser(TestShellIndex);
fn testShellIndex(allocator: Allocator) StringHashMap(u6) {
var result = StringHashMap(u6).init(allocator);
result.put("/bin/bash", 0) catch unreachable;
result.put("/bin/zsh", 1) catch unreachable;
return result;
}
fn testShell(index: u6) []const u8 {
return switch (index) {
@ -345,13 +336,15 @@ test "construct PackedUser section" {
.home = "/",
.shell = "/",
} };
var shellIndex = testShellIndex(testing.allocator);
defer shellIndex.deinit();
for (users) |user| {
try PackedUserTest.packTo(&buf, user, math.maxInt(u64), TestShellIndex{});
try pad.arrayList(&buf, PackedUserHash.alignment_bits);
try PackedUser.packTo(&buf, user, math.maxInt(u64), shellIndex);
try pad.arrayList(&buf, PackedUser.alignment_bits);
}
var i: u29 = 0;
var it1 = PackedUserTest.iterator(buf.items, testShell);
var it1 = PackedUser.iterator(buf.items, testShell);
while (try it1.next()) |user| : (i += 1) {
try testing.expectEqual(users[i].uid, user.uid());
try testing.expectEqual(users[i].gid, user.gid());