1
Fork 0
turbonss/src/group.zig

204 lines
5.3 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");
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 {
var name = try allocator.alloc(u8, self.name.len);
mem.copy(u8, name, self.name);
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
};
const GroupStored = struct {
2022-02-15 10:49:03 +02:00
gid: u32,
name: []const u8,
2022-02-27 15:49:00 +02:00
members_offset: u32,
};
const PackedGroup = struct {
const alignmentBits = 3;
const Inner = packed struct {
gid: u32,
members_offset: u32,
groupname_len: u8,
pub fn groupnameLen(self: *const Inner) usize {
return self.groupname_len + 1;
}
};
inner: *const Inner,
groupdata: []const u8,
pub const Entry = struct {
group: PackedGroup,
next: ?[]const u8,
};
pub fn fromBytes(bytes: []const u8) Entry {
const inner = mem.bytesAsValue(Inner, bytes[0..@sizeOf(Inner)]);
const endBlob = @sizeOf(Inner) + inner.groupnameLen();
const nextStart = pad.roundUp(usize, alignmentBits, endBlob);
var next: ?[]const u8 = null;
2022-03-02 06:18:19 +02:00
if (nextStart < bytes.len)
2022-02-27 15:49:00 +02:00
next = bytes[nextStart..];
return Entry{
.group = PackedGroup{
.inner = inner,
.groupdata = bytes[@sizeOf(Inner)..endBlob],
},
.next = next,
};
}
const packErr = validate.InvalidRecord || Allocator.Error;
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,
pub fn next(it: *Iterator) ?PackedGroup {
if (it.section) |section| {
const entry = fromBytes(section);
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;
}
pub fn membersOffset(self: *const PackedGroup) u32 {
return self.inner.members_offset;
}
pub fn name(self: *const PackedGroup) []const u8 {
return self.groupdata;
}
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 {
const groupname_len = try validate.downCast(u5, group.name.len - 1);
try validate.utf8(group.name);
const inner = Inner{
.gid = group.gid,
.members_offset = group.members_offset,
.groupname_len = groupname_len,
};
try arr.*.appendSlice(mem.asBytes(&inner));
try arr.*.appendSlice(group.name);
try pad.arrayList(arr, alignmentBits);
}
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);
for (members) |member| {
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
.members_offset = std.math.maxInt(u32),
},
};
for (groups) |group| {
try PackedGroup.packTo(&buf, group);
}
var i: u29 = 0;
2022-03-02 06:18:19 +02:00
var it = PackedGroup.iterator(buf.items);
while (it.next()) |group| : (i += 1) {
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"));
}