1
Fork 0

storing getgr_max and getpw_max

main
Motiejus Jakštys 2022-04-18 13:11:20 +03:00 committed by Motiejus Jakštys
parent 71f5284399
commit c6bd3f5cb8
5 changed files with 34 additions and 8 deletions

View File

@ -26,6 +26,9 @@ name2group: StringHashMap(u32),
group2users: []const []const u32,
user2groups: []const []const u32,
getgr_max: usize,
getpw_max: usize,
pub fn init(
baseAllocator: Allocator,
usersConst: []const User,
@ -38,12 +41,18 @@ pub fn init(
var allocator = arena.allocator();
errdefer arena.deinit();
var users_arr = try allocator.alloc(User, usersConst.len);
var groups_arr = try allocator.alloc(Group, groupsConst.len);
for (usersConst) |*user, i|
users_arr[i] = try user.clone(allocator);
for (groupsConst) |*group, i|
var users_arr = try allocator.alloc(User, usersConst.len);
var getgr_max: usize = 0;
for (groupsConst) |*group, i| {
groups_arr[i] = try group.clone(allocator);
getgr_max = math.max(getgr_max, groups_arr[i].strlenZ());
}
var getpw_max: usize = 0;
for (usersConst) |*user, i| {
users_arr[i] = try user.clone(allocator);
getpw_max = math.max(getpw_max, users_arr[i].strlenZ());
}
sort.sort(User, users_arr, {}, cmpUser);
sort.sort(Group, groups_arr, {}, cmpGroup);
@ -114,6 +123,8 @@ pub fn init(
.name2group = name2group,
.group2users = group2users,
.user2groups = user2groups_final,
.getgr_max = getgr_max,
.getpw_max = getpw_max,
};
}

View File

@ -119,8 +119,8 @@ pub fn fromCorpus(
.nblocks_users = nblocks(u64, users.blob),
.nblocks_groupmembers = nblocks(u64, groupmembers.blob),
.nblocks_additional_gids = nblocks(u64, additional_gids.blob),
.getgr_max = 0,
.getpw_max = 0,
.getgr_max = corpus.getgr_max,
.getpw_max = corpus.getpw_max,
};
return DB{

View File

@ -54,6 +54,13 @@ pub fn deinit(self: *Group, allocator: Allocator) void {
self.* = undefined;
}
// buffer size in bytes if all strings were zero-terminated.
pub fn strlenZ(self: *const Group) usize {
return self._buf.len +
self.members.len + // each membername sentinel
1; // name sentinel
}
const testing = std.testing;
test "Group.clone" {

View File

@ -103,6 +103,14 @@ fn strlen(self: *const User) usize {
self.shell.len;
}
// length of all string-data fields, assuming they are zero-terminated.
// Includes one character for "password".
pub fn strlenZ(self: *const User) usize {
return self.strlen() +
4 + // '\0' of name, gecos, home and shell
2; // password: 'x\0'
}
pub fn deinit(self: *User, allocator: Allocator) void {
const slice = self.home.ptr[0..self.strlen()];
allocator.free(slice);

View File

@ -92,8 +92,8 @@ test "header pack and unpack" {
.nblocks_users = 0,
.nblocks_groupmembers = 0,
.nblocks_additional_gids = 1,
.getgr_max = 0,
.getpw_max = 0,
.getgr_max = 16,
.getpw_max = 32,
};
const bytes = mem.asBytes(&header1);