members is a pointer now

This commit is contained in:
Motiejus Jakštys 2022-04-19 10:29:14 +03:00 committed by Motiejus Jakštys
parent ce827a9ae6
commit 70120051c8
2 changed files with 27 additions and 44 deletions

View File

@ -261,9 +261,9 @@ fn getGroup(
buf.*[buf_offset + name.len] = 0; buf.*[buf_offset + name.len] = 0;
return CGroup{ return CGroup{
.name = buf.*[buf_offset .. buf_offset + name.len].ptr,
.gid = group.gid(), .gid = group.gid(),
.members = member_ptrs, .name = buf.*[buf_offset .. buf_offset + name.len].ptr,
.members = member_ptrs.ptr,
}; };
} }
@ -602,28 +602,28 @@ test "high-level API" {
var buf = try testing.allocator.alloc(u8, db.header.getgr_bufsize); var buf = try testing.allocator.alloc(u8, db.header.getgr_bufsize);
defer testing.allocator.free(buf); defer testing.allocator.free(buf);
const all = try db.getgrnam("all", &buf); {
try testing.expect(all != null); const doesnotexist = try db.getgrnam("doesnotexist", &buf);
try testing.expectEqual(all.?.gid, 9999); try testing.expectEqual(doesnotexist, null);
try testing.expectEqualStrings(all.?.name[0..3], "all"); const all = (try db.getgrnam("all", &buf)).?;
const members = all.?.members; try testing.expectEqual(all.gid, 9999);
try testing.expectEqualStrings(mem.sliceTo(members[0].?, 0), "Name" ** 8); try testing.expectEqualStrings(all.name[0..3], "all");
try testing.expectEqualStrings(mem.sliceTo(members[1].?, 0), "root"); try testing.expectEqual(all.name[3], 0);
try testing.expectEqualStrings(mem.sliceTo(members[2].?, 0), "svc-bar"); const members = all.members;
try testing.expectEqualStrings(mem.sliceTo(members[3].?, 0), "vidmantas"); try testing.expectEqualStrings(mem.sliceTo(members[0].?, 0), "Name" ** 8);
try testing.expectEqual(members[4], null); 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);
}
const cgroup = all.?.ptr(); {
try testing.expectEqual(cgroup.gid, all.?.gid); const doesnotexist = try db.getgrgid(42, &buf);
try testing.expectEqual(cgroup.name[0], 'a'); try testing.expectEqual(doesnotexist, null);
try testing.expectEqual(cgroup.name[1], 'l'); const all = (try db.getgrgid(9999, &buf)).?;
try testing.expectEqual(cgroup.name[2], 'l'); try testing.expectEqual(all.gid, 9999);
try testing.expectEqual(cgroup.name[3], 0); try testing.expectEqualStrings(all.name[0..3], "all");
}
const all_gid = try db.getgrgid(9999, &buf);
try testing.expect(all_gid != null);
try testing.expectEqual(all_gid.?.gid, 9999);
try testing.expectEqualStrings(all_gid.?.name[0..3], "all");
} }
test "getgr_bufsize" { test "getgr_bufsize" {

View File

@ -1,32 +1,15 @@
const meta = @import("std").meta; const meta = @import("std").meta;
// All string fields are 0-terminated, but that's not part of the type. // Name type should be 0-terminated, members should be null-terminated, but
// members field is null-terminated, but that's also not part of the type. // sentinel is not part of the type (but is always there). Adding sentinel
// Making it part of the type crashes zig compiler in pre-0.10. // crashes zig compiler in pre-0.10. https://github.com/ziglang/zig/issues/7517
// https://github.com/ziglang/zig/issues/7517
pub const CGroup = struct { pub const CGroup = struct {
gid: u32, gid: u32,
// should be [*:0]const u8 // should be [*:0]const u8
name: [*]const u8, name: [*]const u8,
// Should be [*:null]align(1) const ?[*:0]const u8 // Should be [*:null]align(1) const ?[*:0]const u8
members: []align(1) const ?[*:0]const u8, members: [*]align(1) const ?[*:0]const u8,
pub fn ptr(self: *const CGroup) CGroupPtr {
return CGroupPtr{
.gid = self.gid,
.name = self.name,
.members = @intToPtr([*]const u8, @ptrToInt(self.members.ptr)),
};
}
}; };
// size of the pointer to a single member. // size of the pointer to a single member.
pub const ptr_size = @sizeOf(meta.Child(meta.fieldInfo(CGroup, .members).field_type)); pub const ptr_size = @sizeOf(meta.Child(meta.fieldInfo(CGroup, .members).field_type));
// a workaround of not having sentinel-terminated `name` and `members`.
pub const CGroupPtr = extern struct {
gid: u32,
name: [*]const u8,
members: [*]const u8,
};