2022-03-23 22:14:48 +02:00
|
|
|
const std = @import("std");
|
|
|
|
const os = std.os;
|
|
|
|
const mem = std.mem;
|
|
|
|
const math = std.math;
|
|
|
|
const meta = std.meta;
|
|
|
|
const sort = std.sort;
|
|
|
|
const assert = std.debug.assert;
|
|
|
|
const Allocator = std.mem.Allocator;
|
|
|
|
const ArrayList = std.ArrayList;
|
|
|
|
const AutoHashMap = std.AutoHashMap;
|
|
|
|
const BoundedArray = std.BoundedArray;
|
|
|
|
|
2022-07-04 06:09:03 +03:00
|
|
|
const ErrCtx = @import("ErrCtx.zig");
|
2022-03-23 22:14:48 +02:00
|
|
|
const Corpus = @import("Corpus.zig");
|
|
|
|
const pad = @import("padding.zig");
|
|
|
|
const compress = @import("compress.zig");
|
2022-03-25 14:31:56 +02:00
|
|
|
const Group = @import("Group.zig");
|
2022-04-19 10:46:02 +03:00
|
|
|
const CGroup = Group.CGroup;
|
2022-03-25 14:31:56 +02:00
|
|
|
const PackedGroup = @import("PackedGroup.zig");
|
|
|
|
const GroupStored = PackedGroup.GroupStored;
|
2022-04-19 13:17:23 +03:00
|
|
|
const User = @import("User.zig");
|
|
|
|
const CUser = User.CUser;
|
|
|
|
const PackedUser = @import("PackedUser.zig");
|
2022-03-23 22:14:48 +02:00
|
|
|
const ShellSections = @import("shell.zig").ShellWriter.ShellSections;
|
|
|
|
const ShellReader = @import("shell.zig").ShellReader;
|
|
|
|
const ShellWriter = @import("shell.zig").ShellWriter;
|
2022-03-25 14:31:56 +02:00
|
|
|
const InvalidHeader = @import("header.zig").Invalid;
|
2022-03-23 22:14:48 +02:00
|
|
|
const Header = @import("header.zig").Header;
|
|
|
|
const max_shells = @import("shell.zig").max_shells;
|
|
|
|
const section_length_bits = @import("header.zig").section_length_bits;
|
|
|
|
const section_length = @import("header.zig").section_length;
|
|
|
|
const cmph = @import("cmph.zig");
|
|
|
|
const bdz = @import("bdz.zig");
|
|
|
|
|
|
|
|
const zeroes = &[_]u8{0} ** section_length;
|
|
|
|
|
|
|
|
const DB = @This();
|
|
|
|
// All sections, as they end up in the DB. Order is important.
|
2022-03-25 14:31:56 +02:00
|
|
|
header: *const Header,
|
2022-03-23 22:14:48 +02:00
|
|
|
bdz_gid: []const u8,
|
|
|
|
bdz_groupname: []const u8,
|
|
|
|
bdz_uid: []const u8,
|
|
|
|
bdz_username: []const u8,
|
|
|
|
idx_gid2group: []const u32,
|
|
|
|
idx_groupname2group: []const u32,
|
|
|
|
idx_uid2user: []const u32,
|
|
|
|
idx_name2user: []const u32,
|
|
|
|
shell_index: []const u16,
|
|
|
|
shell_blob: []const u8,
|
|
|
|
groups: []const u8,
|
|
|
|
users: []const u8,
|
|
|
|
groupmembers: []const u8,
|
|
|
|
additional_gids: []const u8,
|
|
|
|
|
|
|
|
pub fn fromCorpus(
|
|
|
|
allocator: Allocator,
|
|
|
|
corpus: *const Corpus,
|
2022-07-04 06:09:03 +03:00
|
|
|
err: *ErrCtx,
|
2022-03-23 22:14:48 +02:00
|
|
|
) error{ OutOfMemory, InvalidRecord, TooMany }!DB {
|
2022-07-04 06:09:03 +03:00
|
|
|
_ = err;
|
|
|
|
|
2022-03-23 22:14:48 +02:00
|
|
|
const gids = corpus.groups.items(.gid);
|
|
|
|
const gnames = corpus.groups.items(.name);
|
|
|
|
const uids = corpus.users.items(.uid);
|
|
|
|
const unames = corpus.users.items(.name);
|
|
|
|
|
2022-03-25 14:31:56 +02:00
|
|
|
const bdz_gid = try cmph.packU32(allocator, gids);
|
2022-03-23 22:14:48 +02:00
|
|
|
errdefer allocator.free(bdz_gid);
|
|
|
|
|
2022-03-25 14:31:56 +02:00
|
|
|
const bdz_groupname = try cmph.packStr(allocator, gnames);
|
2022-03-23 22:14:48 +02:00
|
|
|
errdefer allocator.free(bdz_groupname);
|
|
|
|
|
2022-03-25 14:31:56 +02:00
|
|
|
const bdz_uid = try cmph.packU32(allocator, uids);
|
2022-03-23 22:14:48 +02:00
|
|
|
errdefer allocator.free(bdz_uid);
|
|
|
|
|
|
|
|
const bdz_username = try cmph.packStr(allocator, unames);
|
|
|
|
errdefer allocator.free(bdz_username);
|
|
|
|
|
|
|
|
var shell = try shellSections(allocator, corpus);
|
|
|
|
defer shell.deinit();
|
|
|
|
|
2022-04-19 13:26:42 +03:00
|
|
|
const shell_index = try allocator.dupe(u16, shell.index.constSlice());
|
|
|
|
errdefer allocator.free(shell_index);
|
|
|
|
|
|
|
|
const shell_blob = try allocator.dupe(u8, shell.blob.constSlice());
|
|
|
|
errdefer allocator.free(shell_blob);
|
|
|
|
|
2022-03-25 14:31:56 +02:00
|
|
|
const additional_gids = try additionalGids(allocator, corpus);
|
2022-03-23 22:14:48 +02:00
|
|
|
errdefer allocator.free(additional_gids.blob);
|
2022-03-25 14:31:56 +02:00
|
|
|
defer allocator.free(additional_gids.idx2offset);
|
2022-03-23 22:14:48 +02:00
|
|
|
|
2022-03-25 14:31:56 +02:00
|
|
|
const users = try usersSection(allocator, corpus, &additional_gids, &shell);
|
2022-03-23 22:14:48 +02:00
|
|
|
errdefer allocator.free(users.blob);
|
2022-03-25 14:31:56 +02:00
|
|
|
defer allocator.free(users.idx2offset);
|
2022-03-23 22:14:48 +02:00
|
|
|
|
2022-03-25 14:31:56 +02:00
|
|
|
const groupmembers = try groupMembers(allocator, corpus, users.idx2offset);
|
2022-03-23 22:14:48 +02:00
|
|
|
errdefer allocator.free(groupmembers.blob);
|
2022-03-25 14:31:56 +02:00
|
|
|
defer allocator.free(groupmembers.idx2offset);
|
2022-03-23 22:14:48 +02:00
|
|
|
|
2022-03-25 14:31:56 +02:00
|
|
|
const groups = try groupsSection(allocator, corpus, groupmembers.idx2offset);
|
2022-03-23 22:14:48 +02:00
|
|
|
errdefer allocator.free(groups.blob);
|
2022-03-25 14:31:56 +02:00
|
|
|
defer allocator.free(groups.idx2offset);
|
2022-03-23 22:14:48 +02:00
|
|
|
|
2022-03-25 14:31:56 +02:00
|
|
|
const idx_gid2group = try bdzIdx(u32, allocator, bdz_gid, gids, groups.idx2offset);
|
2022-03-23 22:14:48 +02:00
|
|
|
errdefer allocator.free(idx_gid2group);
|
|
|
|
|
2022-03-25 14:31:56 +02:00
|
|
|
const idx_groupname2group = try bdzIdx([]const u8, allocator, bdz_groupname, gnames, groups.idx2offset);
|
2022-03-23 22:14:48 +02:00
|
|
|
errdefer allocator.free(idx_groupname2group);
|
|
|
|
|
2022-03-25 14:31:56 +02:00
|
|
|
const idx_uid2user = try bdzIdx(u32, allocator, bdz_uid, uids, users.idx2offset);
|
2022-03-23 22:14:48 +02:00
|
|
|
errdefer allocator.free(idx_uid2user);
|
|
|
|
|
2022-03-25 14:31:56 +02:00
|
|
|
const idx_name2user = try bdzIdx([]const u8, allocator, bdz_username, unames, users.idx2offset);
|
2022-03-23 22:14:48 +02:00
|
|
|
errdefer allocator.free(idx_name2user);
|
|
|
|
|
2022-03-25 14:31:56 +02:00
|
|
|
const header = try allocator.create(Header);
|
|
|
|
errdefer allocator.destroy(header);
|
|
|
|
|
|
|
|
header.* = Header{
|
2022-03-23 22:14:48 +02:00
|
|
|
.nblocks_shell_blob = nblocks(u8, shell.blob.constSlice()),
|
|
|
|
.num_shells = shell.len,
|
|
|
|
.num_groups = groups.len,
|
|
|
|
.num_users = users.len,
|
|
|
|
.nblocks_bdz_gid = nblocks(u32, bdz_gid),
|
|
|
|
.nblocks_bdz_groupname = nblocks(u32, bdz_groupname),
|
|
|
|
.nblocks_bdz_uid = nblocks(u32, bdz_uid),
|
|
|
|
.nblocks_bdz_username = nblocks(u32, bdz_username),
|
|
|
|
.nblocks_groups = nblocks(u64, groups.blob),
|
|
|
|
.nblocks_users = nblocks(u64, users.blob),
|
|
|
|
.nblocks_groupmembers = nblocks(u64, groupmembers.blob),
|
|
|
|
.nblocks_additional_gids = nblocks(u64, additional_gids.blob),
|
2022-04-19 06:11:37 +03:00
|
|
|
.getgr_bufsize = corpus.getgr_bufsize,
|
|
|
|
.getpw_bufsize = corpus.getpw_bufsize,
|
2022-03-23 22:14:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return DB{
|
|
|
|
.header = header,
|
|
|
|
.bdz_gid = bdz_gid,
|
|
|
|
.bdz_groupname = bdz_groupname,
|
|
|
|
.bdz_uid = bdz_uid,
|
|
|
|
.bdz_username = bdz_username,
|
|
|
|
.idx_gid2group = idx_gid2group,
|
|
|
|
.idx_groupname2group = idx_groupname2group,
|
|
|
|
.idx_uid2user = idx_uid2user,
|
|
|
|
.idx_name2user = idx_name2user,
|
2022-04-19 13:26:42 +03:00
|
|
|
.shell_index = shell_index,
|
|
|
|
.shell_blob = shell_blob,
|
2022-03-23 22:14:48 +02:00
|
|
|
.groups = groups.blob,
|
|
|
|
.users = users.blob,
|
|
|
|
.groupmembers = groupmembers.blob,
|
|
|
|
.additional_gids = additional_gids.blob,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-21 09:30:39 +03:00
|
|
|
pub fn getgrBufsize(self: *const DB) usize {
|
|
|
|
return self.header.getgr_bufsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn getpwBufsize(self: *const DB) usize {
|
|
|
|
return self.header.getpw_bufsize;
|
|
|
|
}
|
|
|
|
|
2022-03-25 14:31:56 +02:00
|
|
|
pub fn deinit(self: *DB, allocator: Allocator) void {
|
|
|
|
allocator.destroy(self.header);
|
|
|
|
allocator.free(self.bdz_gid);
|
|
|
|
allocator.free(self.bdz_groupname);
|
|
|
|
allocator.free(self.bdz_uid);
|
|
|
|
allocator.free(self.bdz_username);
|
|
|
|
allocator.free(self.idx_gid2group);
|
|
|
|
allocator.free(self.idx_groupname2group);
|
|
|
|
allocator.free(self.idx_uid2user);
|
|
|
|
allocator.free(self.idx_name2user);
|
2022-04-19 13:26:42 +03:00
|
|
|
allocator.free(self.shell_index);
|
|
|
|
allocator.free(self.shell_blob);
|
2022-03-25 14:31:56 +02:00
|
|
|
allocator.free(self.groups);
|
|
|
|
allocator.free(self.users);
|
|
|
|
allocator.free(self.groupmembers);
|
|
|
|
allocator.free(self.additional_gids);
|
|
|
|
self.* = undefined;
|
|
|
|
}
|
|
|
|
|
2022-03-23 22:14:48 +02:00
|
|
|
const DB_fields = meta.fields(DB);
|
|
|
|
pub fn iov(self: *const DB) BoundedArray(os.iovec_const, DB_fields.len * 2) {
|
|
|
|
var result = BoundedArray(os.iovec_const, DB_fields.len * 2).init(0) catch unreachable;
|
|
|
|
inline for (DB_fields) |field| {
|
|
|
|
comptime assertDefinedLayout(field.field_type);
|
|
|
|
const value = @field(self, field.name);
|
|
|
|
const bytes: []const u8 = switch (@TypeOf(value)) {
|
2022-03-25 14:31:56 +02:00
|
|
|
*const Header => mem.asBytes(value),
|
2022-03-23 22:14:48 +02:00
|
|
|
else => mem.sliceAsBytes(value),
|
|
|
|
};
|
|
|
|
result.appendAssumeCapacity(os.iovec_const{
|
|
|
|
.iov_base = bytes.ptr,
|
|
|
|
.iov_len = bytes.len,
|
|
|
|
});
|
|
|
|
const padding = pad.until(usize, section_length_bits, bytes.len);
|
|
|
|
if (padding != 0)
|
|
|
|
result.appendAssumeCapacity(.{
|
|
|
|
.iov_base = zeroes,
|
|
|
|
.iov_len = padding,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-07-06 09:26:24 +03:00
|
|
|
pub const DBNumbers = struct {
|
2022-07-06 08:42:15 +03:00
|
|
|
header: u64,
|
|
|
|
bdz_gid: u64,
|
|
|
|
bdz_groupname: u64,
|
|
|
|
bdz_uid: u64,
|
|
|
|
bdz_username: u64,
|
|
|
|
idx_gid2group: u64,
|
|
|
|
idx_groupname2group: u64,
|
|
|
|
idx_uid2user: u64,
|
|
|
|
idx_name2user: u64,
|
|
|
|
shell_index: u64,
|
|
|
|
shell_blob: u64,
|
|
|
|
groups: u64,
|
|
|
|
users: u64,
|
|
|
|
groupmembers: u64,
|
|
|
|
additional_gids: u64,
|
|
|
|
};
|
|
|
|
|
|
|
|
// in blocks
|
2022-07-06 09:26:24 +03:00
|
|
|
pub fn fieldLengths(header: *const Header) DBNumbers {
|
2022-07-06 08:42:15 +03:00
|
|
|
return DBNumbers{
|
2022-07-06 09:26:24 +03:00
|
|
|
.header = comptime DB.nblocks_n(u64, @sizeOf(Header)),
|
2022-07-06 08:42:15 +03:00
|
|
|
.bdz_gid = header.nblocks_bdz_gid,
|
|
|
|
.bdz_groupname = header.nblocks_bdz_groupname,
|
|
|
|
.bdz_uid = header.nblocks_bdz_uid,
|
|
|
|
.bdz_username = header.nblocks_bdz_username,
|
|
|
|
.idx_gid2group = nblocks_n(u32, header.num_groups * 4),
|
|
|
|
.idx_groupname2group = nblocks_n(u32, header.num_groups * 4),
|
|
|
|
.idx_uid2user = nblocks_n(u32, header.num_users * 4),
|
|
|
|
.idx_name2user = nblocks_n(u32, header.num_users * 4),
|
|
|
|
.shell_index = nblocks_n(u16, header.num_shells * 2),
|
|
|
|
.shell_blob = header.nblocks_shell_blob,
|
|
|
|
.groups = header.nblocks_groups,
|
|
|
|
.users = header.nblocks_users,
|
|
|
|
.groupmembers = header.nblocks_groupmembers,
|
|
|
|
.additional_gids = header.nblocks_additional_gids,
|
2022-03-23 22:14:48 +02:00
|
|
|
};
|
2022-07-06 08:42:15 +03:00
|
|
|
}
|
2022-03-23 22:14:48 +02:00
|
|
|
|
2022-07-06 08:42:15 +03:00
|
|
|
// in blocks
|
2022-07-06 09:26:24 +03:00
|
|
|
pub fn fieldOffsets(lengths: DBNumbers) DBNumbers {
|
2022-07-06 08:42:15 +03:00
|
|
|
var result: DBNumbers = undefined;
|
|
|
|
result.header = 0;
|
2022-03-25 14:31:56 +02:00
|
|
|
var offset = comptime nblocks_n(u64, @sizeOf(Header));
|
2022-07-06 13:29:36 +03:00
|
|
|
inline for (DB_fields[0..]) |field, i| {
|
|
|
|
comptime {
|
|
|
|
assert(mem.eql(u8, field.name, meta.fields(DBNumbers)[i].name));
|
|
|
|
if (mem.eql(u8, field.name, "header")) continue;
|
|
|
|
}
|
|
|
|
|
2022-07-06 08:42:15 +03:00
|
|
|
@field(result, field.name) = offset;
|
|
|
|
offset += @field(lengths, field.name);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fromBytes(buf: []align(8) const u8) InvalidHeader!DB {
|
|
|
|
const header = try Header.fromBytes(buf[0..@sizeOf(Header)]);
|
|
|
|
const lengths = fieldLengths(header);
|
|
|
|
const offsets = fieldOffsets(lengths);
|
|
|
|
|
|
|
|
var result: DB = undefined;
|
|
|
|
result.header = header;
|
|
|
|
inline for (DB_fields[1..]) |field| {
|
|
|
|
const start_block = @field(offsets, field.name);
|
|
|
|
const end = (start_block + @field(lengths, field.name)) << section_length_bits;
|
|
|
|
const start = start_block << section_length_bits;
|
2022-03-25 14:31:56 +02:00
|
|
|
const slice_type = meta.Child(field.field_type);
|
|
|
|
const value = mem.bytesAsSlice(slice_type, buf[start..end]);
|
2022-03-23 22:14:48 +02:00
|
|
|
@field(result, field.name) = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-07-12 12:44:42 +03:00
|
|
|
pub fn packCGroupNoMembers(group: *const PackedGroup, buf: []u8) error{BufferTooSmall}!CGroup {
|
2022-07-08 08:40:24 +03:00
|
|
|
// first word in buf will be a pointer to null. that probably can be
|
|
|
|
// simplified by writing (0)**word_size to the buffer, but let's pretend
|
|
|
|
// type safety for a moment.
|
|
|
|
const name_start = @sizeOf(?[*:0]const u8);
|
|
|
|
if (name_start > buf.len) return error.BufferTooSmall;
|
|
|
|
var member_ptrs = mem.bytesAsSlice(?[*:0]const u8, buf[0..name_start]);
|
|
|
|
member_ptrs[0] = null;
|
|
|
|
|
|
|
|
// write name
|
|
|
|
const name = group.name();
|
|
|
|
if (name_start + name.len + 1 > buf.len) return error.BufferTooSmall;
|
|
|
|
mem.copy(u8, buf[name_start..], name);
|
|
|
|
buf[name_start + name.len] = 0;
|
|
|
|
|
|
|
|
return CGroup{
|
|
|
|
.gid = group.gid(),
|
|
|
|
.name = buf[name_start .. name_start + name.len :0].ptr,
|
|
|
|
.members = member_ptrs.ptr,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-12 12:44:42 +03:00
|
|
|
// The pointer to `arr` gets changed on second iteration, and I don't know why.
|
|
|
|
// Leaving this for better times.
|
|
|
|
//const GroupMembersIter = struct {
|
|
|
|
// vit: compress.VarintSliceIterator,
|
|
|
|
// it: compress.DeltaDecompressionIterator,
|
|
|
|
// total: usize,
|
|
|
|
// arr: []const u8,
|
|
|
|
//
|
|
|
|
// pub fn nextMust(self: *GroupMembersIter) ?u64 {
|
|
|
|
// return self.it.nextMust();
|
|
|
|
// }
|
|
|
|
//};
|
|
|
|
//
|
2022-07-11 15:35:46 +03:00
|
|
|
//pub fn groupMembersIter(members_slice: []const u8) GroupMembersIter {
|
2022-07-12 12:44:42 +03:00
|
|
|
// std.debug.print("members_slice.ptr={*}\n", .{members_slice.ptr});
|
2022-07-11 15:38:32 +03:00
|
|
|
// var vit = compress.varintSliceIteratorMust(members_slice);
|
2022-07-12 12:44:42 +03:00
|
|
|
// var it = compress.deltaDecompressionIterator(&vit);
|
2022-07-11 15:35:46 +03:00
|
|
|
// return GroupMembersIter{
|
2022-07-12 12:44:42 +03:00
|
|
|
// .arr = members_slice,
|
|
|
|
// .vit = vit,
|
|
|
|
// .it = it,
|
|
|
|
// .total = vit.remaining,
|
2022-07-11 15:35:46 +03:00
|
|
|
// };
|
|
|
|
//}
|
|
|
|
|
2022-04-18 19:35:26 +03:00
|
|
|
// dumps PackedGroup to []u8 and returns a CGroup.
|
2022-07-12 12:44:42 +03:00
|
|
|
pub fn packCGroup(self: *const DB, group: *const PackedGroup, buf: []u8) error{BufferTooSmall}!CGroup {
|
2022-04-18 19:35:26 +03:00
|
|
|
const members_slice = self.groupmembers[group.members_offset..];
|
2022-07-11 15:38:32 +03:00
|
|
|
var vit = compress.varintSliceIteratorMust(members_slice);
|
2022-04-08 15:05:56 +03:00
|
|
|
const num_members = vit.remaining;
|
2022-04-07 12:23:50 +03:00
|
|
|
|
2022-04-18 19:35:26 +03:00
|
|
|
const ptr_end = @sizeOf(?[*:0]const u8) * (num_members + 1);
|
2022-07-12 12:44:42 +03:00
|
|
|
if (ptr_end > buf.len)
|
|
|
|
return error.BufferTooSmall;
|
2022-07-04 05:44:42 +03:00
|
|
|
var member_ptrs = mem.bytesAsSlice(?[*:0]const u8, buf[0..ptr_end]);
|
2022-04-18 19:35:26 +03:00
|
|
|
member_ptrs[member_ptrs.len - 1] = null;
|
|
|
|
var buf_offset: usize = ptr_end;
|
2022-04-17 08:58:57 +03:00
|
|
|
|
2022-07-09 13:00:45 +03:00
|
|
|
var it = compress.deltaDecompressionIterator(&vit);
|
2022-07-11 15:35:46 +03:00
|
|
|
|
2022-04-07 12:23:50 +03:00
|
|
|
var i: usize = 0;
|
|
|
|
while (it.nextMust()) |member_offset| : (i += 1) {
|
2022-04-17 08:58:57 +03:00
|
|
|
const entry = PackedUser.fromBytes(self.users[member_offset << 3 ..]);
|
2022-04-18 19:35:26 +03:00
|
|
|
const start = buf_offset;
|
2022-04-07 12:23:50 +03:00
|
|
|
const name = entry.user.name();
|
2022-07-12 12:44:42 +03:00
|
|
|
if (buf_offset + name.len + 1 > buf.len)
|
|
|
|
return error.BufferTooSmall;
|
2022-07-04 05:44:42 +03:00
|
|
|
mem.copy(u8, buf[buf_offset..], name);
|
2022-04-18 19:35:26 +03:00
|
|
|
buf_offset += name.len;
|
2022-07-04 05:44:42 +03:00
|
|
|
buf[buf_offset] = 0;
|
2022-04-18 19:35:26 +03:00
|
|
|
buf_offset += 1;
|
|
|
|
|
2022-04-17 08:58:57 +03:00
|
|
|
// TODO: arr[i] = buf[...] triggers a bug in zig pre-0.10
|
2022-07-04 05:44:42 +03:00
|
|
|
const terminated = buf[start .. buf_offset - 1 :0];
|
2022-04-18 19:35:26 +03:00
|
|
|
member_ptrs[i] = terminated;
|
2022-04-07 12:23:50 +03:00
|
|
|
}
|
2022-04-18 19:35:26 +03:00
|
|
|
|
|
|
|
const name = group.name();
|
2022-07-06 16:54:43 +03:00
|
|
|
if (buf_offset + name.len + 1 > buf.len) return error.BufferTooSmall;
|
2022-07-04 05:44:42 +03:00
|
|
|
mem.copy(u8, buf[buf_offset..], name);
|
|
|
|
buf[buf_offset + name.len] = 0;
|
2022-04-18 19:35:26 +03:00
|
|
|
|
|
|
|
return CGroup{
|
|
|
|
.gid = group.gid(),
|
2022-07-07 07:00:13 +03:00
|
|
|
.name = buf[buf_offset .. buf_offset + name.len :0].ptr,
|
2022-04-19 10:29:14 +03:00
|
|
|
.members = member_ptrs.ptr,
|
2022-04-18 19:35:26 +03:00
|
|
|
};
|
2022-04-07 12:23:50 +03:00
|
|
|
}
|
|
|
|
|
2022-07-11 15:35:46 +03:00
|
|
|
pub fn getGroupByName(self: *const DB, name: []const u8) ?PackedGroup {
|
|
|
|
const idx = bdz.search(self.bdz_groupname, name);
|
|
|
|
if (idx >= self.header.num_groups) return null;
|
|
|
|
const offset = self.idx_groupname2group[idx];
|
|
|
|
const nbits = PackedGroup.alignment_bits;
|
|
|
|
const group = PackedGroup.fromBytes(self.groups[offset << nbits ..]).group;
|
|
|
|
if (!mem.eql(u8, name, group.name())) return null;
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn getGroupByGid(self: *const DB, gid: u32) ?PackedGroup {
|
|
|
|
const idx = bdz.search_u32(self.bdz_gid, gid);
|
|
|
|
if (idx >= self.header.num_groups) return null;
|
|
|
|
const offset = self.idx_gid2group[idx];
|
|
|
|
const nbits = PackedGroup.alignment_bits;
|
|
|
|
const group = PackedGroup.fromBytes(self.groups[offset << nbits ..]).group;
|
|
|
|
if (gid != group.gid()) return null;
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
2022-04-19 07:18:41 +03:00
|
|
|
// get a CGroup entry by name.
|
2022-07-08 08:40:24 +03:00
|
|
|
pub fn getgrnam(
|
|
|
|
self: *const DB,
|
|
|
|
name: []const u8,
|
|
|
|
buf: []u8,
|
|
|
|
omit_members: bool,
|
|
|
|
) error{BufferTooSmall}!?CGroup {
|
2022-07-11 15:35:46 +03:00
|
|
|
const group = self.getGroupByName(name) orelse return null;
|
|
|
|
if (omit_members)
|
2022-07-12 12:44:42 +03:00
|
|
|
return try packCGroupNoMembers(&group, buf)
|
2022-07-08 08:40:24 +03:00
|
|
|
else
|
2022-07-12 12:44:42 +03:00
|
|
|
return try self.packCGroup(&group, buf);
|
2022-03-26 12:06:07 +02:00
|
|
|
}
|
|
|
|
|
2022-04-19 10:08:37 +03:00
|
|
|
// get a CGroup entry by it's gid.
|
2022-07-08 08:40:24 +03:00
|
|
|
pub fn getgrgid(
|
|
|
|
self: *const DB,
|
|
|
|
gid: u32,
|
|
|
|
buf: []u8,
|
|
|
|
omit_members: bool,
|
|
|
|
) error{BufferTooSmall}!?CGroup {
|
2022-07-11 15:35:46 +03:00
|
|
|
const group = self.getGroupByGid(gid) orelse return null;
|
|
|
|
if (omit_members)
|
2022-07-12 12:44:42 +03:00
|
|
|
return try packCGroupNoMembers(&group, buf)
|
2022-07-08 08:40:24 +03:00
|
|
|
else
|
2022-07-12 12:44:42 +03:00
|
|
|
return try self.packCGroup(&group, buf);
|
2022-04-19 10:08:37 +03:00
|
|
|
}
|
|
|
|
|
2022-07-06 16:54:43 +03:00
|
|
|
fn pushStr(str: []const u8, buf: []u8, offset: *usize) [*:0]const u8 {
|
2022-04-19 13:17:23 +03:00
|
|
|
const start = offset.*;
|
2022-07-07 06:54:27 +03:00
|
|
|
mem.copy(u8, buf[start..], str);
|
|
|
|
buf[start + str.len] = 0;
|
2022-04-19 13:17:23 +03:00
|
|
|
offset.* += str.len + 1;
|
2022-07-07 07:00:13 +03:00
|
|
|
return buf[start .. start + str.len :0].ptr;
|
2022-04-19 13:17:23 +03:00
|
|
|
}
|
|
|
|
|
2022-07-11 11:11:25 +03:00
|
|
|
pub fn shellReader(self: *const DB) ShellReader {
|
|
|
|
return ShellReader{
|
2022-04-19 13:17:23 +03:00
|
|
|
.index = self.shell_index,
|
|
|
|
.blob = self.shell_blob,
|
|
|
|
};
|
2022-07-11 11:11:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn writeUser(self: *const DB, user: PackedUser, buf: []u8) error{BufferTooSmall}!CUser {
|
2022-04-19 13:17:23 +03:00
|
|
|
const name = user.name();
|
|
|
|
const gecos = user.gecos();
|
|
|
|
const home = user.home();
|
2022-07-11 11:11:25 +03:00
|
|
|
const shell = user.shell(self.shellReader());
|
2022-04-19 13:17:23 +03:00
|
|
|
|
|
|
|
const strlen =
|
|
|
|
name.len + 1 +
|
|
|
|
gecos.len + 1 +
|
|
|
|
home.len + 1 +
|
|
|
|
shell.len + 1;
|
2022-07-06 13:53:34 +03:00
|
|
|
if (strlen > buf.len) return error.BufferTooSmall;
|
2022-04-19 13:17:23 +03:00
|
|
|
|
|
|
|
var offset: usize = 0;
|
|
|
|
const pw_name = pushStr(name, buf, &offset);
|
|
|
|
const pw_gecos = pushStr(gecos, buf, &offset);
|
|
|
|
const pw_dir = pushStr(home, buf, &offset);
|
|
|
|
const pw_shell = pushStr(shell, buf, &offset);
|
|
|
|
|
|
|
|
return CUser{
|
|
|
|
.pw_name = pw_name,
|
|
|
|
.pw_uid = user.uid(),
|
|
|
|
.pw_gid = user.gid(),
|
|
|
|
.pw_gecos = pw_gecos,
|
|
|
|
.pw_dir = pw_dir,
|
|
|
|
.pw_shell = pw_shell,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-11 11:11:25 +03:00
|
|
|
pub fn getUserByName(self: *const DB, name: []const u8) ?PackedUser {
|
2022-04-19 13:17:23 +03:00
|
|
|
const idx = bdz.search(self.bdz_username, name);
|
2022-04-20 06:04:34 +03:00
|
|
|
// bdz may return a hash that's bigger than the number of users
|
|
|
|
if (idx >= self.header.num_users) return null;
|
2022-04-19 13:17:23 +03:00
|
|
|
const offset = self.idx_name2user[idx];
|
|
|
|
const nbits = PackedUser.alignment_bits;
|
|
|
|
const user = PackedUser.fromBytes(self.users[offset << nbits ..]).user;
|
|
|
|
if (!mem.eql(u8, name, user.name())) return null;
|
2022-07-08 06:29:42 +03:00
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a CUser entry by name.
|
|
|
|
pub fn getpwnam(self: *const DB, name: []const u8, buf: []u8) error{BufferTooSmall}!?CUser {
|
2022-07-11 11:11:25 +03:00
|
|
|
const user = self.getUserByName(name) orelse return null;
|
2022-07-08 06:29:42 +03:00
|
|
|
return try self.writeUser(user, buf);
|
2022-04-19 13:17:23 +03:00
|
|
|
}
|
|
|
|
|
2022-07-11 11:11:25 +03:00
|
|
|
pub fn getUserByUid(self: *const DB, uid: u32) ?PackedUser {
|
2022-04-19 13:50:29 +03:00
|
|
|
const idx = bdz.search_u32(self.bdz_uid, uid);
|
2022-04-20 06:04:34 +03:00
|
|
|
if (idx >= self.header.num_users) return null;
|
2022-04-19 13:50:29 +03:00
|
|
|
const offset = self.idx_uid2user[idx];
|
|
|
|
const nbits = PackedUser.alignment_bits;
|
|
|
|
const user = PackedUser.fromBytes(self.users[offset << nbits ..]).user;
|
|
|
|
if (uid != user.uid()) return null;
|
2022-07-11 11:11:25 +03:00
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a CUser entry by uid.
|
|
|
|
pub fn getpwuid(self: *const DB, uid: u32, buf: []u8) error{BufferTooSmall}!?CUser {
|
|
|
|
const user = self.getUserByUid(uid) orelse return null;
|
2022-07-08 06:29:42 +03:00
|
|
|
return try self.writeUser(user, buf);
|
2022-04-19 13:50:29 +03:00
|
|
|
}
|
|
|
|
|
2022-03-23 22:14:48 +02:00
|
|
|
fn shellSections(
|
|
|
|
allocator: Allocator,
|
|
|
|
corpus: *const Corpus,
|
|
|
|
) error{OutOfMemory}!ShellSections {
|
|
|
|
var popcon = ShellWriter.init(allocator);
|
2022-04-19 13:17:23 +03:00
|
|
|
errdefer popcon.deinit();
|
2022-03-23 22:14:48 +02:00
|
|
|
for (corpus.users.items(.shell)) |shell|
|
|
|
|
try popcon.put(shell);
|
|
|
|
return popcon.toOwnedSections(max_shells);
|
|
|
|
}
|
|
|
|
|
|
|
|
const AdditionalGids = struct {
|
|
|
|
// user index -> offset in blob
|
|
|
|
idx2offset: []const u64,
|
|
|
|
// compressed user gids blob. A blob contains N <= users.len items,
|
|
|
|
// an item is:
|
|
|
|
// len: varint
|
|
|
|
// gid: [varint]varint,
|
|
|
|
// ... and the gid list is delta-compressed.
|
|
|
|
blob: []const u8,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn additionalGids(
|
|
|
|
allocator: Allocator,
|
|
|
|
corpus: *const Corpus,
|
|
|
|
) error{OutOfMemory}!AdditionalGids {
|
|
|
|
var blob = ArrayList(u8).init(allocator);
|
|
|
|
errdefer blob.deinit();
|
|
|
|
var idx2offset = try allocator.alloc(u64, corpus.users.len);
|
|
|
|
errdefer allocator.free(idx2offset);
|
|
|
|
|
|
|
|
// zero'th entry is empty, so groupless users can refer to it.
|
|
|
|
try compress.appendUvarint(&blob, 0);
|
|
|
|
|
|
|
|
var scratch = try allocator.alloc(u32, 256);
|
2022-05-16 19:23:14 +03:00
|
|
|
var scratch_allocated: bool = true;
|
|
|
|
defer if (scratch_allocated) allocator.free(scratch);
|
2022-03-23 22:14:48 +02:00
|
|
|
for (corpus.user2groups) |usergroups, user_idx| {
|
|
|
|
if (usergroups.len == 0) {
|
|
|
|
idx2offset[user_idx] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
idx2offset[user_idx] = blob.items.len;
|
2022-05-16 19:23:14 +03:00
|
|
|
if (scratch.len < usergroups.len) {
|
|
|
|
allocator.free(scratch);
|
|
|
|
scratch_allocated = false;
|
|
|
|
scratch = try allocator.alloc(u32, usergroups.len);
|
|
|
|
scratch_allocated = true;
|
|
|
|
}
|
2022-03-23 22:14:48 +02:00
|
|
|
scratch.len = usergroups.len;
|
|
|
|
const corpusGids = corpus.groups.items(.gid);
|
|
|
|
for (usergroups) |group_idx, i|
|
|
|
|
scratch[i] = corpusGids[group_idx];
|
|
|
|
compress.deltaCompress(u32, scratch) catch |err| switch (err) {
|
|
|
|
error.NotSorted => unreachable,
|
|
|
|
};
|
|
|
|
try compress.appendUvarint(&blob, usergroups.len);
|
|
|
|
for (scratch) |gid|
|
|
|
|
try compress.appendUvarint(&blob, gid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return AdditionalGids{
|
|
|
|
.idx2offset = idx2offset,
|
|
|
|
.blob = blob.toOwnedSlice(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const UsersSection = struct {
|
|
|
|
// number of users in this section
|
|
|
|
len: u32,
|
|
|
|
// user index -> offset in blob
|
|
|
|
idx2offset: []const u32,
|
|
|
|
blob: []const u8,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn usersSection(
|
|
|
|
allocator: Allocator,
|
|
|
|
corpus: *const Corpus,
|
|
|
|
gids: *const AdditionalGids,
|
|
|
|
shells: *const ShellSections,
|
|
|
|
) error{ OutOfMemory, InvalidRecord, TooMany }!UsersSection {
|
|
|
|
var idx2offset = try allocator.alloc(u32, corpus.users.len);
|
|
|
|
errdefer allocator.free(idx2offset);
|
|
|
|
// as of writing each user takes 12 bytes + blobs + padding, padded to
|
|
|
|
// 8 bytes. 24 is an optimistic lower bound for an average record size.
|
|
|
|
var blob = try ArrayList(u8).initCapacity(allocator, 24 * corpus.users.len);
|
|
|
|
errdefer blob.deinit();
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < corpus.users.len) : (i += 1) {
|
|
|
|
// TODO: this is inefficient by calling `.slice()` on every iteration
|
|
|
|
const user = corpus.users.get(i);
|
2022-06-05 23:11:13 +03:00
|
|
|
const user_offset = math.cast(u35, blob.items.len) orelse return error.TooMany;
|
2022-03-23 22:14:48 +02:00
|
|
|
assert(user_offset & 7 == 0);
|
|
|
|
idx2offset[i] = @truncate(u32, user_offset >> 3);
|
|
|
|
try PackedUser.packTo(
|
|
|
|
&blob,
|
|
|
|
user,
|
|
|
|
gids.idx2offset[i],
|
|
|
|
shells.shell2idx,
|
|
|
|
);
|
|
|
|
try pad.arrayList(&blob, PackedUser.alignment_bits);
|
|
|
|
}
|
|
|
|
return UsersSection{
|
|
|
|
.len = @intCast(u32, corpus.users.len),
|
|
|
|
.idx2offset = idx2offset,
|
|
|
|
.blob = blob.toOwnedSlice(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const GroupMembers = struct {
|
|
|
|
// group index to it's offset in blob
|
|
|
|
idx2offset: []const u64,
|
|
|
|
// members are delta-varint encoded byte-offsets to the user struct
|
|
|
|
blob: []const u8,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn groupMembers(
|
|
|
|
allocator: Allocator,
|
|
|
|
corpus: *const Corpus,
|
|
|
|
user2offset: []const u32,
|
|
|
|
) error{OutOfMemory}!GroupMembers {
|
|
|
|
var idx2offset = try allocator.alloc(u64, corpus.groups.len);
|
|
|
|
errdefer allocator.free(idx2offset);
|
|
|
|
var blob = ArrayList(u8).init(allocator);
|
|
|
|
errdefer blob.deinit();
|
|
|
|
// zero'th entry is empty, so empty groups can refer to it
|
|
|
|
try compress.appendUvarint(&blob, 0);
|
|
|
|
|
|
|
|
var scratch = try ArrayList(u32).initCapacity(allocator, 1024);
|
|
|
|
defer scratch.deinit();
|
|
|
|
|
|
|
|
for (corpus.group2users) |members, group_idx| {
|
|
|
|
if (members.len == 0) {
|
|
|
|
idx2offset[group_idx] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
idx2offset[group_idx] = blob.items.len;
|
|
|
|
try scratch.ensureTotalCapacity(members.len);
|
|
|
|
scratch.items.len = members.len;
|
|
|
|
for (members) |user_idx, i|
|
|
|
|
scratch.items[i] = user2offset[user_idx];
|
|
|
|
|
|
|
|
compress.deltaCompress(u32, scratch.items) catch |err| switch (err) {
|
|
|
|
error.NotSorted => unreachable,
|
|
|
|
};
|
|
|
|
try compress.appendUvarint(&blob, members.len);
|
|
|
|
for (scratch.items) |elem|
|
|
|
|
try compress.appendUvarint(&blob, elem);
|
|
|
|
}
|
|
|
|
return GroupMembers{
|
|
|
|
.idx2offset = idx2offset,
|
|
|
|
.blob = blob.toOwnedSlice(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const GroupsSection = struct {
|
|
|
|
// number of groups in this section
|
|
|
|
len: u32,
|
|
|
|
// group index -> offset in blob
|
|
|
|
idx2offset: []const u32,
|
|
|
|
blob: []const u8,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn groupsSection(
|
|
|
|
allocator: Allocator,
|
|
|
|
corpus: *const Corpus,
|
|
|
|
members_offset: []const u64,
|
|
|
|
) error{ OutOfMemory, InvalidRecord }!GroupsSection {
|
|
|
|
var idx2offset = try allocator.alloc(u32, corpus.groups.len);
|
|
|
|
errdefer allocator.free(idx2offset);
|
|
|
|
|
|
|
|
var blob = try ArrayList(u8).initCapacity(allocator, 8 * corpus.groups.len);
|
|
|
|
errdefer blob.deinit();
|
|
|
|
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < corpus.groups.len) : (i += 1) {
|
|
|
|
// TODO: this is inefficient; it's calling `.slice()` on every iteration
|
|
|
|
const group = corpus.groups.get(i);
|
|
|
|
const group_offset = @intCast(u32, blob.items.len);
|
|
|
|
assert(group_offset & 7 == 0);
|
|
|
|
idx2offset[i] = @truncate(u32, group_offset >> 3);
|
|
|
|
const group_stored = GroupStored{
|
|
|
|
.gid = group.gid,
|
|
|
|
.name = group.name,
|
|
|
|
.members_offset = members_offset[i],
|
|
|
|
};
|
|
|
|
try PackedGroup.packTo(&blob, group_stored);
|
|
|
|
try pad.arrayList(&blob, PackedGroup.alignment_bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GroupsSection{
|
|
|
|
.len = @intCast(u32, corpus.groups.len),
|
|
|
|
.idx2offset = idx2offset,
|
|
|
|
.blob = blob.toOwnedSlice(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-09 13:00:45 +03:00
|
|
|
pub fn userGids(self: *const DB, offset: u64) compress.DeltaDecompressionIterator {
|
2022-07-11 15:38:32 +03:00
|
|
|
var vit = compress.varintSliceIteratorMust(self.additional_gids[offset..]);
|
2022-07-09 13:00:45 +03:00
|
|
|
return compress.deltaDecompressionIterator(&vit);
|
|
|
|
}
|
|
|
|
|
2022-03-23 22:14:48 +02:00
|
|
|
// creates a bdz index using packed_mphf.
|
|
|
|
// hash = bdz_search(packed_mphf, keys[i]);
|
|
|
|
// result[hash] = idx2offset[i];
|
|
|
|
fn bdzIdx(
|
|
|
|
comptime T: type,
|
|
|
|
allocator: Allocator,
|
|
|
|
packed_mphf: []const u8,
|
|
|
|
keys: []const T,
|
|
|
|
idx2offset: []const u32,
|
|
|
|
) error{OutOfMemory}![]const u32 {
|
2022-03-25 14:31:56 +02:00
|
|
|
const search_fn = switch (T) {
|
|
|
|
u32 => bdz.search_u32,
|
|
|
|
[]const u8 => bdz.search,
|
|
|
|
else => unreachable,
|
2022-03-23 22:14:48 +02:00
|
|
|
};
|
|
|
|
assert(keys.len <= math.maxInt(u32));
|
|
|
|
var result = try allocator.alloc(u32, keys.len);
|
2022-04-17 08:58:57 +03:00
|
|
|
errdefer allocator.free(result);
|
2022-03-23 22:14:48 +02:00
|
|
|
for (keys) |key, i|
|
|
|
|
result[search_fn(packed_mphf, key)] = idx2offset[i];
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nblocks_n returns how many blocks a given number of bytes will take
|
2022-07-05 06:02:54 +03:00
|
|
|
pub fn nblocks_n(comptime T: type, nbytes: usize) T {
|
2022-03-23 22:14:48 +02:00
|
|
|
const B = switch (T) {
|
|
|
|
u8 => u14,
|
2022-03-25 14:31:56 +02:00
|
|
|
u16 => u22,
|
2022-03-23 22:14:48 +02:00
|
|
|
u32 => u38,
|
|
|
|
u64 => u70,
|
2022-03-25 14:31:56 +02:00
|
|
|
else => @compileError("got " ++ @typeName(T) ++ ", only u8, u32 and u64 are supported"),
|
2022-03-23 22:14:48 +02:00
|
|
|
};
|
|
|
|
const upper = pad.roundUp(B, section_length_bits, @intCast(B, nbytes));
|
|
|
|
assert(upper & (section_length - 1) == 0);
|
2022-03-25 14:31:56 +02:00
|
|
|
return @truncate(T, upper >> section_length_bits);
|
2022-03-23 22:14:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// nblocks returns how many blocks a particular slice will take.
|
|
|
|
fn nblocks(comptime T: type, arr: []const u8) T {
|
|
|
|
return nblocks_n(T, arr.len);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assertDefinedLayout(comptime T: type) void {
|
|
|
|
return switch (T) {
|
2022-04-19 06:11:37 +03:00
|
|
|
u4, u8, u16, u32, u64 => {},
|
2022-03-23 22:14:48 +02:00
|
|
|
else => switch (@typeInfo(T)) {
|
2022-03-25 14:31:56 +02:00
|
|
|
.Array => assertDefinedLayout(meta.Elem(T)),
|
|
|
|
.Pointer => |info| assertDefinedLayout(info.child),
|
2022-03-23 22:14:48 +02:00
|
|
|
.Enum => assertDefinedLayout(meta.Tag(T)),
|
|
|
|
.Struct => {
|
|
|
|
if (meta.containerLayout(T) == .Auto)
|
|
|
|
@compileError("layout of " ++ @typeName(T) ++ " is undefined");
|
|
|
|
for (meta.fields(T)) |field|
|
|
|
|
assertDefinedLayout(field.field_type);
|
|
|
|
},
|
|
|
|
else => @compileError("unexpected type " ++ @typeName(T)),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const testing = std.testing;
|
|
|
|
|
2022-07-12 12:59:47 +03:00
|
|
|
test "DB read/write via iovec" {
|
2022-03-23 22:14:48 +02:00
|
|
|
const allocator = testing.allocator;
|
|
|
|
var corpus = try Corpus.testCorpus(allocator);
|
|
|
|
defer corpus.deinit();
|
|
|
|
|
2022-07-04 06:09:03 +03:00
|
|
|
var errc = ErrCtx{};
|
|
|
|
var db = try DB.fromCorpus(allocator, &corpus, &errc);
|
2022-03-23 22:14:48 +02:00
|
|
|
defer db.deinit(allocator);
|
|
|
|
|
|
|
|
const fd = try os.memfd_create("test_turbonss_db", 0);
|
|
|
|
defer os.close(fd);
|
|
|
|
|
2022-03-25 14:31:56 +02:00
|
|
|
const len = try os.writev(fd, db.iov().constSlice());
|
|
|
|
const buf = try os.mmap(null, len, os.PROT.READ, os.MAP.SHARED, fd, 0);
|
|
|
|
const db2 = try fromBytes(buf);
|
|
|
|
try testing.expectEqual(corpus.groups.len, db.header.num_groups);
|
|
|
|
try testing.expectEqual(corpus.users.len, db.header.num_users);
|
|
|
|
try testing.expectEqual(db.header.num_groups, db2.header.num_groups);
|
|
|
|
try testing.expectEqual(db.header.num_users, db2.header.num_users);
|
|
|
|
const num_groups = db2.header.num_groups;
|
|
|
|
const num_users = db2.header.num_users;
|
|
|
|
|
|
|
|
try testing.expectEqualSlices(u32, db.idx_gid2group, db2.idx_gid2group[0..num_groups]);
|
|
|
|
try testing.expectEqualSlices(u32, db.idx_uid2user, db2.idx_uid2user[0..num_users]);
|
2022-07-04 06:09:03 +03:00
|
|
|
try testing.expectEqualStrings("", errc.unwrap().constSlice());
|
2022-03-23 22:14:48 +02:00
|
|
|
}
|
|
|
|
|
2022-07-12 12:59:47 +03:00
|
|
|
test "DB getgrnam/getgrgid" {
|
2022-04-19 07:18:41 +03:00
|
|
|
var corpus = try Corpus.testCorpus(testing.allocator);
|
2022-04-07 12:23:50 +03:00
|
|
|
defer corpus.deinit();
|
2022-07-04 06:09:03 +03:00
|
|
|
var errc = ErrCtx{};
|
|
|
|
var db = try DB.fromCorpus(testing.allocator, &corpus, &errc);
|
2022-04-19 07:18:41 +03:00
|
|
|
defer db.deinit(testing.allocator);
|
2022-04-21 09:30:39 +03:00
|
|
|
var buf = try testing.allocator.alloc(u8, db.getgrBufsize());
|
2022-04-19 07:18:41 +03:00
|
|
|
defer testing.allocator.free(buf);
|
2022-04-07 12:23:50 +03:00
|
|
|
|
2022-04-19 10:29:14 +03:00
|
|
|
{
|
2022-07-08 08:40:24 +03:00
|
|
|
try testing.expectEqual(try db.getgrnam("doesnotexist", buf, false), null);
|
|
|
|
const all = (try db.getgrnam("all", buf, false)).?;
|
2022-04-19 10:29:14 +03:00
|
|
|
try testing.expectEqual(all.gid, 9999);
|
2022-04-19 14:11:05 +03:00
|
|
|
try testing.expectEqualStrings(all.name[0..4], "all\x00");
|
2022-04-19 10:29:14 +03:00
|
|
|
const members = all.members;
|
|
|
|
try testing.expectEqualStrings(mem.sliceTo(members[0].?, 0), "Name" ** 8);
|
|
|
|
try testing.expectEqualStrings(mem.sliceTo(members[1].?, 0), "root");
|
|
|
|
try testing.expectEqualStrings(mem.sliceTo(members[2].?, 0), "svc-bar");
|
|
|
|
try testing.expectEqualStrings(mem.sliceTo(members[3].?, 0), "vidmantas");
|
|
|
|
try testing.expectEqual(members[4], null);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-07-08 08:40:24 +03:00
|
|
|
const all = (try db.getgrnam("all", buf, true)).?;
|
|
|
|
try testing.expectEqual(all.gid, 9999);
|
|
|
|
try testing.expectEqualStrings(all.name[0..4], "all\x00");
|
|
|
|
try testing.expectEqual(all.members[0], null);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
try testing.expectEqual(try db.getgrgid(42, buf, false), null);
|
|
|
|
const all = (try db.getgrgid(9999, buf, false)).?;
|
2022-04-19 10:29:14 +03:00
|
|
|
try testing.expectEqual(all.gid, 9999);
|
|
|
|
try testing.expectEqualStrings(all.name[0..3], "all");
|
|
|
|
}
|
2022-07-04 06:09:03 +03:00
|
|
|
try testing.expectEqualStrings("", errc.unwrap().constSlice());
|
2022-04-19 07:18:41 +03:00
|
|
|
|
2022-07-08 08:40:24 +03:00
|
|
|
_ = try db.getgrnam("all", buf, false);
|
2022-04-19 07:18:41 +03:00
|
|
|
buf.len -= 1;
|
2022-07-08 08:40:24 +03:00
|
|
|
try testing.expectError(error.BufferTooSmall, db.getgrnam("all", buf, false));
|
2022-04-07 12:23:50 +03:00
|
|
|
}
|
|
|
|
|
2022-07-12 12:59:47 +03:00
|
|
|
test "DB getpwnam/getpwuid" {
|
2022-04-19 13:50:29 +03:00
|
|
|
var corpus = try Corpus.testCorpus(testing.allocator);
|
|
|
|
defer corpus.deinit();
|
2022-07-04 06:09:03 +03:00
|
|
|
var errc = ErrCtx{};
|
|
|
|
var db = try DB.fromCorpus(testing.allocator, &corpus, &errc);
|
2022-04-19 13:50:29 +03:00
|
|
|
defer db.deinit(testing.allocator);
|
2022-04-21 09:30:39 +03:00
|
|
|
var buf = try testing.allocator.alloc(u8, db.getpwBufsize());
|
2022-04-19 13:50:29 +03:00
|
|
|
defer testing.allocator.free(buf);
|
|
|
|
|
|
|
|
{
|
2022-07-04 05:44:42 +03:00
|
|
|
try testing.expectEqual(try db.getpwnam("doesnotexist", buf), null);
|
|
|
|
const vidmantas = (try db.getpwnam("vidmantas", buf)).?;
|
2022-04-19 13:50:29 +03:00
|
|
|
try testing.expectEqual(vidmantas.pw_uid, 128);
|
|
|
|
try testing.expectEqual(vidmantas.pw_gid, 128);
|
2022-04-19 14:11:05 +03:00
|
|
|
try testing.expectEqualStrings(vidmantas.pw_name[0..10], "vidmantas\x00");
|
|
|
|
try testing.expectEqualStrings(vidmantas.pw_gecos[0..20], "Vidmantas Kaminskas\x00");
|
|
|
|
try testing.expectEqualStrings(vidmantas.pw_dir[0..16], "/home/vidmantas\x00");
|
2022-04-19 13:50:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-07-04 05:44:42 +03:00
|
|
|
try testing.expectEqual(try db.getpwuid(123456, buf), null);
|
|
|
|
const vidmantas = (try db.getpwuid(128, buf)).?;
|
2022-04-19 13:50:29 +03:00
|
|
|
try testing.expectEqual(vidmantas.pw_uid, 128);
|
|
|
|
try testing.expectEqual(vidmantas.pw_gid, 128);
|
2022-04-19 14:11:05 +03:00
|
|
|
try testing.expectEqualStrings(vidmantas.pw_name[0..10], "vidmantas\x00");
|
2022-04-19 13:50:29 +03:00
|
|
|
}
|
2022-07-04 06:09:03 +03:00
|
|
|
try testing.expectEqualStrings("", errc.unwrap().constSlice());
|
2022-04-19 13:50:29 +03:00
|
|
|
|
2022-07-04 05:44:42 +03:00
|
|
|
const long = try db.getpwnam("Name" ** 8, buf);
|
2022-04-19 14:11:05 +03:00
|
|
|
try testing.expectEqualStrings(long.?.pw_name[0..33], "Name" ** 8 ++ "\x00");
|
2022-04-19 13:50:29 +03:00
|
|
|
buf.len -= 1;
|
2022-07-06 13:53:34 +03:00
|
|
|
try testing.expectError(error.BufferTooSmall, db.getpwnam("Name" ** 8, buf));
|
2022-04-19 13:17:23 +03:00
|
|
|
}
|
|
|
|
|
2022-07-12 12:59:47 +03:00
|
|
|
test "DB additionalGids" {
|
2022-03-23 22:14:48 +02:00
|
|
|
const allocator = testing.allocator;
|
|
|
|
var corpus = try Corpus.testCorpus(allocator);
|
|
|
|
defer corpus.deinit();
|
|
|
|
|
|
|
|
var additional_gids = try additionalGids(allocator, &corpus);
|
|
|
|
defer allocator.free(additional_gids.idx2offset);
|
|
|
|
defer allocator.free(additional_gids.blob);
|
|
|
|
|
|
|
|
var user_idx: usize = 0;
|
|
|
|
while (user_idx < corpus.users.len) : (user_idx += 1) {
|
|
|
|
const groups = corpus.user2groups[user_idx];
|
|
|
|
const offset = additional_gids.idx2offset[user_idx];
|
|
|
|
if (groups.len == 0) {
|
|
|
|
try testing.expect(offset == 0);
|
|
|
|
continue;
|
|
|
|
}
|
2022-07-11 15:38:32 +03:00
|
|
|
var vit = try compress.varintSliceIterator(additional_gids.blob[offset..]);
|
2022-07-09 13:00:45 +03:00
|
|
|
var it = compress.deltaDecompressionIterator(&vit);
|
2022-03-23 22:14:48 +02:00
|
|
|
try testing.expectEqual(it.remaining(), groups.len);
|
|
|
|
var i: u64 = 0;
|
|
|
|
const corpusGids = corpus.groups.items(.gid);
|
|
|
|
while (try it.next()) |gid| : (i += 1) {
|
|
|
|
try testing.expectEqual(gid, corpusGids[groups[i]]);
|
|
|
|
}
|
|
|
|
try testing.expectEqual(i, groups.len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 12:59:47 +03:00
|
|
|
test "DB pack gids" {
|
2022-03-23 22:14:48 +02:00
|
|
|
const allocator = testing.allocator;
|
|
|
|
var corpus = try Corpus.testCorpus(allocator);
|
|
|
|
defer corpus.deinit();
|
|
|
|
|
|
|
|
const cmph_gid = try cmph.packU32(allocator, corpus.groups.items(.gid));
|
|
|
|
defer allocator.free(cmph_gid);
|
|
|
|
|
|
|
|
const k1 = bdz.search_u32(cmph_gid, 0);
|
|
|
|
const k2 = bdz.search_u32(cmph_gid, 128);
|
|
|
|
const k3 = bdz.search_u32(cmph_gid, 9999);
|
|
|
|
const k4 = bdz.search_u32(cmph_gid, 100000);
|
|
|
|
var hashes = &[_]u32{ k1, k2, k3, k4 };
|
|
|
|
sort.sort(u32, hashes, {}, comptime sort.asc(u32));
|
|
|
|
for (hashes) |hash, i|
|
|
|
|
try testing.expectEqual(i, hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
const hash_offsets = &[_]u32{ 0, 10, 20, 30 };
|
|
|
|
|
|
|
|
fn expectUsedHashes(allocator: Allocator, arr: []const u32) !void {
|
|
|
|
var used = AutoHashMap(u32, void).init(allocator);
|
|
|
|
defer used.deinit();
|
|
|
|
|
|
|
|
for (arr) |elem|
|
|
|
|
try used.putNoClobber(elem, {});
|
|
|
|
for (hash_offsets) |item|
|
|
|
|
try testing.expect(used.get(item) != null);
|
|
|
|
}
|
|
|
|
|
2022-07-12 12:59:47 +03:00
|
|
|
test "DB bdzIdx on u32" {
|
2022-03-23 22:14:48 +02:00
|
|
|
const keys = [_]u32{ 42, 1, 2, 3 };
|
|
|
|
const mphf = try cmph.packU32(testing.allocator, keys[0..]);
|
|
|
|
defer testing.allocator.free(mphf);
|
|
|
|
var result = try bdzIdx(u32, testing.allocator, mphf, keys[0..], hash_offsets);
|
|
|
|
defer testing.allocator.free(result);
|
|
|
|
try expectUsedHashes(testing.allocator, result);
|
|
|
|
}
|
|
|
|
|
2022-07-12 12:59:47 +03:00
|
|
|
test "DB bdzIdx on str" {
|
2022-03-23 22:14:48 +02:00
|
|
|
const keys = [_][]const u8{ "42", "1", "2", "3" };
|
|
|
|
const mphf = try cmph.packStr(testing.allocator, keys[0..]);
|
|
|
|
defer testing.allocator.free(mphf);
|
|
|
|
var result = try bdzIdx([]const u8, testing.allocator, mphf, keys[0..], hash_offsets);
|
|
|
|
defer testing.allocator.free(result);
|
|
|
|
try expectUsedHashes(testing.allocator, result);
|
|
|
|
}
|
|
|
|
|
2022-07-12 12:59:47 +03:00
|
|
|
test "DB nblocks" {
|
2022-03-23 22:14:48 +02:00
|
|
|
const tests = .{
|
|
|
|
.{ 0, &[_]u8{} },
|
|
|
|
.{ 1, &[_]u8{ 1, 2, 42 } },
|
|
|
|
.{ 1, &[_]u8{1} ** 63 },
|
|
|
|
.{ 1, &[_]u8{1} ** 64 },
|
|
|
|
.{ 2, &[_]u8{1} ** 65 },
|
|
|
|
.{ 255, &[_]u8{1} ** (255 * 64) },
|
|
|
|
};
|
|
|
|
|
|
|
|
inline for (tests) |tt| {
|
|
|
|
try testing.expectEqual(nblocks(u8, tt[1]), tt[0]);
|
|
|
|
try testing.expectEqual(nblocks(u32, tt[1]), tt[0]);
|
|
|
|
try testing.expectEqual(nblocks(u64, tt[1]), tt[0]);
|
|
|
|
}
|
|
|
|
}
|