turbo-getent group implementation (no tests)

This commit is contained in:
Motiejus Jakštys 2022-07-12 12:55:46 +03:00
parent d2407685a0
commit c4e8f1f02c

View File

@ -5,6 +5,7 @@ const mem = std.mem;
const fmt = std.fmt;
const ArrayList = std.ArrayList;
const compress = @import("compress.zig");
const flags = @import("flags.zig");
const DB = @import("DB.zig");
const File = @import("File.zig");
@ -148,8 +149,29 @@ fn group(stdout: anytype, db: *const DB, keys: []const [*:0]const u8) u8 {
continue;
};
// not converting to Group to avoid memory allocations.
// not converting to Group to save a few memory allocations.
stdout.print("{s}:x:{d}:", .{ g.name(), g.gid() }) catch return 3;
// TODO: move member iteration from here and DB.packCGroup
// to a common place.
const members_slice = db.groupmembers[g.members_offset..];
var vit = compress.varintSliceIteratorMust(members_slice);
var it = compress.deltaDecompressionIterator(&vit);
// lines will be buffered, but flushed on every EOL.
var line_writer = io.BufferedWriter(65536, @TypeOf(stdout)){
.unbuffered_writer = stdout,
};
var i: usize = 0;
while (it.nextMust()) |member_offset| : (i += 1) {
const puser = PackedUser.fromBytes(db.users[member_offset << 3 ..]);
const name = puser.user.name();
if (i != 0)
_ = line_writer.write(",") catch return 3;
_ = line_writer.write(name) catch return 3;
}
_ = line_writer.write("\n") catch return 3;
line_writer.flush() catch return 3;
}
return if (some_notfound) 2 else 0;
@ -163,7 +185,7 @@ fn group_all(stdout: anytype, db: *const DB) u8 {
const testing = std.testing;
test "passwd" {
test "turbo-getent passwd" {
var tf = try File.TestDB.init(testing.allocator);
defer tf.deinit();
var stdout = ArrayList(u8).init(testing.allocator);
@ -193,7 +215,7 @@ test "passwd" {
}
}
test "passwd_all" {
test "turbo-getent passwd_all" {
var tf = try File.TestDB.init(testing.allocator);
defer tf.deinit();
var stdout = ArrayList(u8).init(testing.allocator);