storing getgr_max and getpw_max

This commit is contained in:
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, group2users: []const []const u32,
user2groups: []const []const u32, user2groups: []const []const u32,
getgr_max: usize,
getpw_max: usize,
pub fn init( pub fn init(
baseAllocator: Allocator, baseAllocator: Allocator,
usersConst: []const User, usersConst: []const User,
@ -38,12 +41,18 @@ pub fn init(
var allocator = arena.allocator(); var allocator = arena.allocator();
errdefer arena.deinit(); errdefer arena.deinit();
var users_arr = try allocator.alloc(User, usersConst.len);
var groups_arr = try allocator.alloc(Group, groupsConst.len); var groups_arr = try allocator.alloc(Group, groupsConst.len);
for (usersConst) |*user, i| var users_arr = try allocator.alloc(User, usersConst.len);
users_arr[i] = try user.clone(allocator); var getgr_max: usize = 0;
for (groupsConst) |*group, i| for (groupsConst) |*group, i| {
groups_arr[i] = try group.clone(allocator); 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(User, users_arr, {}, cmpUser);
sort.sort(Group, groups_arr, {}, cmpGroup); sort.sort(Group, groups_arr, {}, cmpGroup);
@ -114,6 +123,8 @@ pub fn init(
.name2group = name2group, .name2group = name2group,
.group2users = group2users, .group2users = group2users,
.user2groups = user2groups_final, .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_users = nblocks(u64, users.blob),
.nblocks_groupmembers = nblocks(u64, groupmembers.blob), .nblocks_groupmembers = nblocks(u64, groupmembers.blob),
.nblocks_additional_gids = nblocks(u64, additional_gids.blob), .nblocks_additional_gids = nblocks(u64, additional_gids.blob),
.getgr_max = 0, .getgr_max = corpus.getgr_max,
.getpw_max = 0, .getpw_max = corpus.getpw_max,
}; };
return DB{ return DB{

View File

@ -54,6 +54,13 @@ pub fn deinit(self: *Group, allocator: Allocator) void {
self.* = undefined; 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; const testing = std.testing;
test "Group.clone" { test "Group.clone" {

View File

@ -103,6 +103,14 @@ fn strlen(self: *const User) usize {
self.shell.len; 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 { pub fn deinit(self: *User, allocator: Allocator) void {
const slice = self.home.ptr[0..self.strlen()]; const slice = self.home.ptr[0..self.strlen()];
allocator.free(slice); allocator.free(slice);

View File

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