members is a pointer now
This commit is contained in:
parent
ce827a9ae6
commit
70120051c8
36
lib/DB.zig
36
lib/DB.zig
@ -261,9 +261,9 @@ fn getGroup(
|
||||
buf.*[buf_offset + name.len] = 0;
|
||||
|
||||
return CGroup{
|
||||
.name = buf.*[buf_offset .. buf_offset + name.len].ptr,
|
||||
.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);
|
||||
defer testing.allocator.free(buf);
|
||||
|
||||
const all = try db.getgrnam("all", &buf);
|
||||
try testing.expect(all != null);
|
||||
try testing.expectEqual(all.?.gid, 9999);
|
||||
try testing.expectEqualStrings(all.?.name[0..3], "all");
|
||||
const members = all.?.members;
|
||||
{
|
||||
const doesnotexist = try db.getgrnam("doesnotexist", &buf);
|
||||
try testing.expectEqual(doesnotexist, null);
|
||||
const all = (try db.getgrnam("all", &buf)).?;
|
||||
try testing.expectEqual(all.gid, 9999);
|
||||
try testing.expectEqualStrings(all.name[0..3], "all");
|
||||
try testing.expectEqual(all.name[3], 0);
|
||||
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);
|
||||
}
|
||||
|
||||
const cgroup = all.?.ptr();
|
||||
try testing.expectEqual(cgroup.gid, all.?.gid);
|
||||
try testing.expectEqual(cgroup.name[0], 'a');
|
||||
try testing.expectEqual(cgroup.name[1], 'l');
|
||||
try testing.expectEqual(cgroup.name[2], 'l');
|
||||
try testing.expectEqual(cgroup.name[3], 0);
|
||||
|
||||
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");
|
||||
{
|
||||
const doesnotexist = try db.getgrgid(42, &buf);
|
||||
try testing.expectEqual(doesnotexist, null);
|
||||
const all = (try db.getgrgid(9999, &buf)).?;
|
||||
try testing.expectEqual(all.gid, 9999);
|
||||
try testing.expectEqualStrings(all.name[0..3], "all");
|
||||
}
|
||||
}
|
||||
|
||||
test "getgr_bufsize" {
|
||||
|
@ -1,32 +1,15 @@
|
||||
const meta = @import("std").meta;
|
||||
|
||||
// All string fields are 0-terminated, but that's not part of the type.
|
||||
// members field is null-terminated, but that's also not part of the type.
|
||||
// Making it part of the type crashes zig compiler in pre-0.10.
|
||||
// https://github.com/ziglang/zig/issues/7517
|
||||
|
||||
// Name type should be 0-terminated, members should be null-terminated, but
|
||||
// sentinel is not part of the type (but is always there). Adding sentinel
|
||||
// crashes zig compiler in pre-0.10. https://github.com/ziglang/zig/issues/7517
|
||||
pub const CGroup = struct {
|
||||
gid: u32,
|
||||
// should be [*:0]const u8
|
||||
name: [*]const u8,
|
||||
// Should be [*:null]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)),
|
||||
};
|
||||
}
|
||||
members: [*]align(1) const ?[*:0]const u8,
|
||||
};
|
||||
|
||||
// size of the pointer to a single member.
|
||||
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,
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user