mutable/immutable PackedUser
This commit is contained in:
parent
f4c4dc535c
commit
b4c7cba627
78
src/user.zig
78
src/user.zig
|
@ -20,7 +20,13 @@ pub const User = struct {
|
||||||
shell: []const u8,
|
shell: []const u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const PackedUser = struct {
|
pub const PackedUserMut = packedUser(false);
|
||||||
|
pub const PackedUserConst = packedUser(true);
|
||||||
|
|
||||||
|
fn packedUser(immutable: bool) type {
|
||||||
|
return struct {
|
||||||
|
const Self = @This();
|
||||||
|
|
||||||
const AlignmentBits = 3;
|
const AlignmentBits = 3;
|
||||||
const shellIndexFn = fn ([]const u8) ?u6;
|
const shellIndexFn = fn ([]const u8) ?u6;
|
||||||
|
|
||||||
|
@ -87,17 +93,20 @@ pub const PackedUser = struct {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const Bytes = if (immutable) []const u8 else []u8;
|
||||||
|
const InnerPointer = if (immutable) *const Inner else *Inner;
|
||||||
|
|
||||||
// PackedUser does not allocate; it re-interprets the "bytes" blob field.
|
// PackedUser does not allocate; it re-interprets the "bytes" blob field.
|
||||||
// Both of those fields are pointers to "our representation" of that field.
|
// Both of those fields are pointers to "our representation" of that field.
|
||||||
inner: *const Inner,
|
inner: InnerPointer,
|
||||||
userdata: []const u8,
|
userdata: Bytes,
|
||||||
|
|
||||||
pub const Entry = struct {
|
pub const Entry = struct {
|
||||||
user: PackedUser,
|
user: Self,
|
||||||
next: ?[]const u8,
|
next: ?Bytes,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn fromBytes(bytes: []const u8) Entry {
|
pub fn fromBytes(bytes: Bytes) Entry {
|
||||||
const inner = std.mem.bytesAsValue(
|
const inner = std.mem.bytesAsValue(
|
||||||
Inner,
|
Inner,
|
||||||
// Should use InnerSize instead of sizeOf, see
|
// Should use InnerSize instead of sizeOf, see
|
||||||
|
@ -109,13 +118,13 @@ pub const PackedUser = struct {
|
||||||
const endBlob = startBlob + inner.blobLength();
|
const endBlob = startBlob + inner.blobLength();
|
||||||
|
|
||||||
const nextStart = pad.roundUp(usize, AlignmentBits, endBlob);
|
const nextStart = pad.roundUp(usize, AlignmentBits, endBlob);
|
||||||
var next: ?[]const u8 = null;
|
var next: ?Bytes = null;
|
||||||
if (nextStart < bytes.len) {
|
if (nextStart < bytes.len) {
|
||||||
next = bytes[nextStart..];
|
next = bytes[nextStart..];
|
||||||
}
|
}
|
||||||
|
|
||||||
return Entry{
|
return Entry{
|
||||||
.user = PackedUser{
|
.user = Self{
|
||||||
.inner = inner,
|
.inner = inner,
|
||||||
.userdata = bytes[startBlob..endBlob],
|
.userdata = bytes[startBlob..endBlob],
|
||||||
},
|
},
|
||||||
|
@ -202,31 +211,31 @@ pub const PackedUser = struct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uid(self: *const PackedUser) u32 {
|
pub fn uid(self: Self) u32 {
|
||||||
return self.inner.uid;
|
return self.inner.uid;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn gid(self: *const PackedUser) u32 {
|
pub fn gid(self: Self) u32 {
|
||||||
return self.inner.gid;
|
return self.inner.gid;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn home(self: *const PackedUser) []const u8 {
|
pub fn home(self: Self) []const u8 {
|
||||||
return self.userdata[0..self.inner.homeLen()];
|
return self.userdata[0..self.inner.homeLen()];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(self: *const PackedUser) []const u8 {
|
pub fn name(self: Self) []const u8 {
|
||||||
const name_pos = self.inner.nameStart();
|
const name_pos = self.inner.nameStart();
|
||||||
const name_len = self.inner.nameLen();
|
const name_len = self.inner.nameLen();
|
||||||
return self.userdata[name_pos .. name_pos + name_len];
|
return self.userdata[name_pos .. name_pos + name_len];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn gecos(self: *const PackedUser) []const u8 {
|
pub fn gecos(self: Self) []const u8 {
|
||||||
const gecos_pos = self.inner.gecosStart();
|
const gecos_pos = self.inner.gecosStart();
|
||||||
const gecos_len = self.inner.gecosLen();
|
const gecos_len = self.inner.gecosLen();
|
||||||
return self.userdata[gecos_pos .. gecos_pos + gecos_len];
|
return self.userdata[gecos_pos .. gecos_pos + gecos_len];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn shell(self: *const PackedUser, idxFn: ShellIndexFn) []const u8 {
|
pub fn shell(self: Self, idxFn: ShellIndexFn) []const u8 {
|
||||||
if (self.inner.shell_here) {
|
if (self.inner.shell_here) {
|
||||||
const shell_pos = self.inner.maybeShellStart();
|
const shell_pos = self.inner.maybeShellStart();
|
||||||
const shell_len = self.inner.shellLen();
|
const shell_len = self.inner.shellLen();
|
||||||
|
@ -234,28 +243,29 @@ pub const PackedUser = struct {
|
||||||
}
|
}
|
||||||
return idxFn(self.inner.shell_len_or_idx);
|
return idxFn(self.inner.shell_len_or_idx);
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
pub fn userIterator(section: []const u8, idxFn: ShellIndexFn) Iterator {
|
pub const Iterator = struct {
|
||||||
return Iterator{
|
section: ?Bytes,
|
||||||
.section = section,
|
|
||||||
.shellIndex = idxFn,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const Iterator = struct {
|
|
||||||
section: ?[]const u8,
|
|
||||||
shellIndex: ShellIndexFn,
|
shellIndex: ShellIndexFn,
|
||||||
|
|
||||||
pub fn next(it: *Iterator) ?PackedUser {
|
pub fn next(it: *Iterator) ?Self {
|
||||||
if (it.section) |section| {
|
if (it.section) |section| {
|
||||||
const entry = PackedUser.fromBytes(section);
|
const entry = Self.fromBytes(section);
|
||||||
it.section = entry.next;
|
it.section = entry.next;
|
||||||
return entry.user;
|
return entry.user;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub fn iterator(section: Bytes, idxFn: ShellIndexFn) Iterator {
|
||||||
|
return Iterator{
|
||||||
|
.section = section,
|
||||||
|
.shellIndex = idxFn,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const testing = std.testing;
|
const testing = std.testing;
|
||||||
const ArrayList = std.ArrayList;
|
const ArrayList = std.ArrayList;
|
||||||
|
@ -267,7 +277,7 @@ test "PackedUser internal and external alignment" {
|
||||||
// cannot be converted from/to [@bitSizeOf(PackedUser)/8]u8;
|
// cannot be converted from/to [@bitSizeOf(PackedUser)/8]u8;
|
||||||
// asBytes/bytesAsValue use @sizeOf, which is larger. Now we are putting no
|
// asBytes/bytesAsValue use @sizeOf, which is larger. Now we are putting no
|
||||||
// more than 1, but it probably could be higher.
|
// more than 1, but it probably could be higher.
|
||||||
try testing.expect(@sizeOf(PackedUser) * 8 - @bitSizeOf(PackedUser) <= 8);
|
try testing.expect(@sizeOf(PackedUserMut.Inner) * 8 - @bitSizeOf(PackedUserMut.Inner) <= 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn testShellIndex(shell: []const u8) ?u6 {
|
fn testShellIndex(shell: []const u8) ?u6 {
|
||||||
|
@ -321,11 +331,11 @@ test "construct PackedUser section" {
|
||||||
.shell = "/",
|
.shell = "/",
|
||||||
} };
|
} };
|
||||||
for (users) |user| {
|
for (users) |user| {
|
||||||
try buf.ensureUnusedCapacity(PackedUser.maxSize());
|
try buf.ensureUnusedCapacity(PackedUserMut.maxSize());
|
||||||
try PackedUser.packTo(&buf, user, testShellIndex);
|
try PackedUserMut.packTo(&buf, user, testShellIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
var it = userIterator(buf.items, testShell);
|
var it = PackedUserMut.iterator(buf.items, testShell);
|
||||||
var i: u32 = 0;
|
var i: u32 = 0;
|
||||||
while (it.next()) |user| : (i += 1) {
|
while (it.next()) |user| : (i += 1) {
|
||||||
try testing.expectEqual(users[i].uid, user.uid());
|
try testing.expectEqual(users[i].uid, user.uid());
|
||||||
|
@ -341,7 +351,7 @@ test "construct PackedUser section" {
|
||||||
test "PackedUser.maxSize()" {
|
test "PackedUser.maxSize()" {
|
||||||
// TODO(motiejus) try using a slice that points to an array in stack.
|
// TODO(motiejus) try using a slice that points to an array in stack.
|
||||||
// As of writing, I am getting a stack smashing error.
|
// As of writing, I am getting a stack smashing error.
|
||||||
var buf = try ArrayList(u8).initCapacity(testing.allocator, PackedUser.maxSize());
|
var buf = try ArrayList(u8).initCapacity(testing.allocator, PackedUserConst.maxSize());
|
||||||
defer buf.deinit();
|
defer buf.deinit();
|
||||||
|
|
||||||
const largeUser = User{
|
const largeUser = User{
|
||||||
|
@ -352,6 +362,6 @@ test "PackedUser.maxSize()" {
|
||||||
.home = "Home" ** 16, // 64
|
.home = "Home" ** 16, // 64
|
||||||
.shell = "She.LllL" ** 8, // 64
|
.shell = "She.LllL" ** 8, // 64
|
||||||
};
|
};
|
||||||
try PackedUser.packTo(&buf, largeUser, testShellIndex);
|
try PackedUserConst.packTo(&buf, largeUser, testShellIndex);
|
||||||
try testing.expectEqual(PackedUser.maxSize(), buf.items.len);
|
try testing.expectEqual(PackedUserConst.maxSize(), buf.items.len);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue