1
Fork 0
turbonss/src/group.zig

203 lines
5.7 KiB
Zig
Raw Normal View History

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
};
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
};
pub const PackedGroup = struct {
pub const alignment_bits = 3;
2022-02-27 15:49:00 +02:00
const Inner = packed struct {
gid: u32,
padding: u3 = 0,
groupname_len: u5,
2022-02-27 15:49:00 +02:00
pub fn groupnameLen(self: *const Inner) usize {
return @as(usize, self.groupname_len) + 1;
2022-02-27 15:49:00 +02:00
}
};
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 {
std.debug.assert(arr.items.len & 7 == 0);
2022-02-27 15:49:00 +02:00
try validate.utf8(group.name);
const len = try validate.downCast(u5, group.name.len - 1);
const inner = Inner{ .gid = group.gid, .groupname_len = len };
2022-02-27 15:49:00 +02:00
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();
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-03-18 06:52:21 +02:00
GroupStored{
2022-02-27 15:49:00 +02:00
.gid = 1000,
.name = "sudo",
.members_offset = 1,
},
2022-03-18 06:52:21 +02:00
GroupStored{
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);
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"));
}