2022-02-15 10:49:03 +02:00
|
|
|
const std = @import("std");
|
|
|
|
|
2022-02-27 15:49:00 +02:00
|
|
|
const pad = @import("padding.zig");
|
|
|
|
const validate = @import("validate.zig");
|
2022-03-09 07:04:33 +02:00
|
|
|
const compress = @import("compress.zig");
|
2022-02-27 15:49:00 +02:00
|
|
|
const InvalidRecord = validate.InvalidRecord;
|
|
|
|
|
|
|
|
const mem = std.mem;
|
|
|
|
const Allocator = mem.Allocator;
|
|
|
|
const ArrayList = std.ArrayList;
|
2022-03-01 11:01:45 +02:00
|
|
|
const BufSet = std.BufSet;
|
2022-02-24 05:51:04 +02:00
|
|
|
|
2022-02-28 10:31:14 +02:00
|
|
|
pub const Group = struct {
|
|
|
|
gid: u32,
|
|
|
|
name: []const u8,
|
2022-03-01 11:01:45 +02:00
|
|
|
members: BufSet,
|
|
|
|
|
|
|
|
pub fn clone(self: *const Group, allocator: Allocator) Allocator.Error!Group {
|
2022-03-02 11:05:20 +02:00
|
|
|
var name = try allocator.dupe(u8, self.name);
|
2022-03-01 11:01:45 +02:00
|
|
|
return Group{
|
|
|
|
.gid = self.gid,
|
|
|
|
.name = name,
|
|
|
|
.members = try self.members.cloneWithAllocator(allocator),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deinit(self: *Group, allocator: Allocator) void {
|
|
|
|
allocator.free(self.name);
|
|
|
|
self.members.deinit();
|
|
|
|
self.* = undefined;
|
|
|
|
}
|
2022-02-28 10:31:14 +02:00
|
|
|
};
|
|
|
|
|
2022-03-10 18:50:40 +02:00
|
|
|
pub const GroupStored = struct {
|
2022-02-15 10:49:03 +02:00
|
|
|
gid: u32,
|
|
|
|
name: []const u8,
|
2022-03-09 07:04:33 +02:00
|
|
|
members_offset: u64,
|
2022-02-27 15:49:00 +02:00
|
|
|
};
|
|
|
|
|
2022-03-10 18:50:40 +02:00
|
|
|
pub const PackedGroup = struct {
|
|
|
|
pub const alignment_bits = 3;
|
2022-02-27 15:49:00 +02:00
|
|
|
|
|
|
|
const Inner = packed struct {
|
|
|
|
gid: u32,
|
|
|
|
groupname_len: u8,
|
|
|
|
|
|
|
|
pub fn groupnameLen(self: *const Inner) usize {
|
|
|
|
return self.groupname_len + 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inner: *const Inner,
|
|
|
|
groupdata: []const u8,
|
2022-03-09 07:04:33 +02:00
|
|
|
members_offset: u64,
|
2022-02-27 15:49:00 +02:00
|
|
|
|
|
|
|
pub const Entry = struct {
|
|
|
|
group: PackedGroup,
|
|
|
|
next: ?[]const u8,
|
|
|
|
};
|
|
|
|
|
2022-03-09 07:04:33 +02:00
|
|
|
pub fn fromBytes(bytes: []const u8) error{Overflow}!Entry {
|
2022-02-27 15:49:00 +02:00
|
|
|
const inner = mem.bytesAsValue(Inner, bytes[0..@sizeOf(Inner)]);
|
2022-03-09 07:04:33 +02:00
|
|
|
const start_blob = @sizeOf(Inner);
|
|
|
|
const end_strings = @sizeOf(Inner) + inner.groupnameLen();
|
|
|
|
const members_offset = try compress.uvarint(bytes[end_strings..]);
|
|
|
|
const end_blob = end_strings + members_offset.bytes_read;
|
|
|
|
const next_start = pad.roundUp(usize, alignment_bits, end_blob);
|
2022-02-27 15:49:00 +02:00
|
|
|
|
|
|
|
var next: ?[]const u8 = null;
|
2022-03-09 07:04:33 +02:00
|
|
|
if (next_start < bytes.len)
|
|
|
|
next = bytes[next_start..];
|
2022-02-27 15:49:00 +02:00
|
|
|
|
|
|
|
return Entry{
|
|
|
|
.group = PackedGroup{
|
|
|
|
.inner = inner,
|
2022-03-09 07:04:33 +02:00
|
|
|
.groupdata = bytes[start_blob..end_strings],
|
|
|
|
.members_offset = members_offset.value,
|
2022-02-27 15:49:00 +02:00
|
|
|
},
|
|
|
|
.next = next,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validateUtf8(s: []const u8) InvalidRecord!void {
|
2022-03-02 06:18:19 +02:00
|
|
|
if (!std.unicode.utf8ValidateSlice(s))
|
2022-02-27 15:49:00 +02:00
|
|
|
return error.InvalidRecord;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const Iterator = struct {
|
|
|
|
section: ?[]const u8,
|
|
|
|
|
2022-03-09 07:04:33 +02:00
|
|
|
pub fn next(it: *Iterator) error{Overflow}!?PackedGroup {
|
2022-02-27 15:49:00 +02:00
|
|
|
if (it.section) |section| {
|
2022-03-09 07:04:33 +02:00
|
|
|
const entry = try fromBytes(section);
|
2022-02-27 15:49:00 +02:00
|
|
|
it.section = entry.next;
|
|
|
|
return entry.group;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn iterator(section: []const u8) Iterator {
|
|
|
|
return Iterator{ .section = section };
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gid(self: *const PackedGroup) u32 {
|
|
|
|
return self.inner.gid;
|
|
|
|
}
|
|
|
|
|
2022-03-09 07:04:33 +02:00
|
|
|
pub fn membersOffset(self: *const PackedGroup) u64 {
|
|
|
|
return self.members_offset;
|
2022-02-27 15:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn name(self: *const PackedGroup) []const u8 {
|
|
|
|
return self.groupdata;
|
|
|
|
}
|
|
|
|
|
2022-03-09 07:04:33 +02:00
|
|
|
const packErr = validate.InvalidRecord || Allocator.Error || error{Overflow};
|
2022-02-27 15:49:00 +02:00
|
|
|
pub fn packTo(
|
|
|
|
arr: *ArrayList(u8),
|
2022-02-28 10:31:14 +02:00
|
|
|
group: GroupStored,
|
2022-02-27 15:49:00 +02:00
|
|
|
) packErr!void {
|
2022-03-10 18:50:40 +02:00
|
|
|
std.debug.assert(arr.items.len & 7 == 0);
|
2022-02-27 15:49:00 +02:00
|
|
|
const groupname_len = try validate.downCast(u5, group.name.len - 1);
|
|
|
|
try validate.utf8(group.name);
|
|
|
|
const inner = Inner{
|
|
|
|
.gid = group.gid,
|
|
|
|
.groupname_len = groupname_len,
|
|
|
|
};
|
|
|
|
|
|
|
|
try arr.*.appendSlice(mem.asBytes(&inner));
|
|
|
|
try arr.*.appendSlice(group.name);
|
2022-03-09 07:04:33 +02:00
|
|
|
try compress.appendUvarint(arr, group.members_offset);
|
2022-02-27 15:49:00 +02:00
|
|
|
}
|
2022-02-15 10:49:03 +02:00
|
|
|
};
|
2022-02-24 05:51:04 +02:00
|
|
|
|
|
|
|
const testing = std.testing;
|
|
|
|
|
2022-03-01 11:01:45 +02:00
|
|
|
// someMembers constructs a bufset from an allocator and a list of strings.
|
|
|
|
pub fn someMembers(
|
|
|
|
allocator: Allocator,
|
|
|
|
members: []const []const u8,
|
|
|
|
) Allocator.Error!BufSet {
|
|
|
|
var bufset = BufSet.init(allocator);
|
2022-03-05 05:33:31 +02:00
|
|
|
errdefer bufset.deinit();
|
2022-03-10 18:50:40 +02:00
|
|
|
for (members) |member|
|
2022-03-01 11:01:45 +02:00
|
|
|
try bufset.insert(member);
|
|
|
|
return bufset;
|
|
|
|
}
|
|
|
|
|
2022-02-24 05:51:04 +02:00
|
|
|
test "PackedGroup alignment" {
|
|
|
|
try testing.expectEqual(@sizeOf(PackedGroup) * 8, @bitSizeOf(PackedGroup));
|
|
|
|
}
|
2022-02-27 15:49:00 +02:00
|
|
|
|
|
|
|
test "construct PackedGroups" {
|
|
|
|
var buf = ArrayList(u8).init(testing.allocator);
|
|
|
|
defer buf.deinit();
|
|
|
|
|
2022-02-28 10:31:14 +02:00
|
|
|
const groups = [_]GroupStored{
|
|
|
|
.{
|
2022-02-27 15:49:00 +02:00
|
|
|
.gid = 1000,
|
|
|
|
.name = "sudo",
|
|
|
|
.members_offset = 1,
|
|
|
|
},
|
2022-02-28 10:31:14 +02:00
|
|
|
.{
|
2022-02-27 15:49:00 +02:00
|
|
|
.gid = std.math.maxInt(u32),
|
|
|
|
.name = "Name" ** 8, // 32
|
2022-03-09 07:04:33 +02:00
|
|
|
.members_offset = std.math.maxInt(u64),
|
2022-02-27 15:49:00 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (groups) |group| {
|
|
|
|
try PackedGroup.packTo(&buf, group);
|
2022-03-10 18:50:40 +02:00
|
|
|
try pad.arrayList(&buf, PackedGroup.alignment_bits);
|
2022-02-27 15:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var i: u29 = 0;
|
2022-03-02 06:18:19 +02:00
|
|
|
var it = PackedGroup.iterator(buf.items);
|
2022-03-09 07:04:33 +02:00
|
|
|
while (try it.next()) |group| : (i += 1) {
|
2022-03-02 06:18:19 +02:00
|
|
|
try testing.expectEqual(groups[i].gid, group.gid());
|
|
|
|
try testing.expectEqualStrings(groups[i].name, group.name());
|
|
|
|
try testing.expectEqual(groups[i].members_offset, group.membersOffset());
|
2022-02-27 15:49:00 +02:00
|
|
|
}
|
2022-03-02 06:18:19 +02:00
|
|
|
try testing.expectEqual(groups.len, i);
|
2022-02-27 15:49:00 +02:00
|
|
|
}
|
2022-03-01 11:01:45 +02:00
|
|
|
|
|
|
|
test "Group.clone" {
|
|
|
|
var allocator = testing.allocator;
|
|
|
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
|
|
|
|
var members = BufSet.init(allocator);
|
|
|
|
try members.insert("member1");
|
|
|
|
try members.insert("member2");
|
|
|
|
defer members.deinit();
|
|
|
|
|
|
|
|
var cloned = try members.cloneWithAllocator(arena.allocator());
|
|
|
|
|
|
|
|
cloned.remove("member1");
|
|
|
|
try cloned.insert("member4");
|
|
|
|
try testing.expect(members.contains("member1"));
|
|
|
|
try testing.expect(!members.contains("member4"));
|
|
|
|
|
|
|
|
try testing.expect(!cloned.contains("member1"));
|
|
|
|
try testing.expect(cloned.contains("member4"));
|
|
|
|
}
|