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); const userOffset = try math.cast(u32, blob.items.len);
std.debug.assert(userOffset & 7 == 0); std.debug.assert(userOffset & 7 == 0);
idx2offset[i] = userOffset; idx2offset[i] = userOffset;
try userImport.PackedUserHash.packTo( try userImport.PackedUser.packTo(
&blob, &blob,
user, user,
gids.idx2offset[i], gids.idx2offset[i],
shells.indices, shells.indices,
); );
try pad.arrayList(&blob, userImport.PackedUserHash.alignment_bits); try pad.arrayList(&blob, userImport.PackedUser.alignment_bits);
} }
return UsersSection{ return UsersSection{
.idx2offset = idx2offset, .idx2offset = idx2offset,

View File

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