formatting

This commit is contained in:
Motiejus Jakštys 2022-02-25 15:20:35 +02:00 committed by Motiejus Jakštys
parent 22c61d1207
commit 87b4b81a48

View File

@ -2,10 +2,8 @@ const std = @import("std");
const pad = @import("padding.zig");
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const math = std.math;
const mem = std.mem;
const Allocator = mem.Allocator;
// ShellIndexFn is a function prototype that, given a shell's index (in
// global shell section), will return a shell string. Matches ShelReader.get.
@ -24,7 +22,7 @@ pub const User = struct {
pub const PackedUser = struct {
const AlignmentBits = 3;
const shellIndexFnType = fn ([]const u8) ?u6;
const shellIndexFn = fn ([]const u8) ?u6;
const InnerSize = @divExact(@bitSizeOf(Inner), 8);
const Inner = packed struct {
@ -125,7 +123,8 @@ pub const PackedUser = struct {
};
}
fn downCast(comptime T: type, n: u64) error{InvalidRecord}!T {
pub const errInvalid = error{InvalidRecord};
fn downCast(comptime T: type, n: u64) errInvalid!T {
return std.math.cast(T, n) catch |err| switch (err) {
error.Overflow => {
return error.InvalidRecord;
@ -133,7 +132,7 @@ pub const PackedUser = struct {
};
}
fn validateUtf8(s: []const u8) error{InvalidRecord}!void {
fn validateUtf8(s: []const u8) errInvalid!void {
if (!std.unicode.utf8ValidateSlice(s)) {
return error.InvalidRecord;
}
@ -142,11 +141,7 @@ pub const PackedUser = struct {
// packTo packs the User record and copies it to the given byte slice. The
// slice must have at least maxRecordSize() bytes available.
// The slice is passed as a pointer, so it can be mutated.
pub fn packTo(
buf: *[]u8,
user: User,
shellIndexFn: shellIndexFnType,
) error{InvalidRecord}!void {
pub fn packTo(buf: *[]u8, user: User, idxFn: shellIndexFn) errInvalid!void {
// function arguments are consts. We need to mutate the underlying
// slice, so passing it via pointer instead.
const home_len = try downCast(u6, user.home.len - 1);
@ -163,10 +158,10 @@ pub const PackedUser = struct {
.uid = user.uid,
.gid = user.gid,
.additional_gids_offset = std.math.maxInt(u29),
.shell_here = shellIndexFn(user.shell) == null,
.shell_len_or_idx = shellIndexFn(user.shell) orelse shell_len,
.shell_here = idxFn(user.shell) == null,
.shell_len_or_idx = idxFn(user.shell) orelse shell_len,
.home_len = home_len,
.name_is_a_suffix = std.mem.endsWith(u8, user.home, user.name),
.name_is_a_suffix = mem.endsWith(u8, user.home, user.name),
.name_len = name_len,
.gecos_len = gecos_len,
};
@ -272,6 +267,7 @@ pub const Iterator = struct {
};
const testing = std.testing;
const ArrayList = std.ArrayList;
test "PackedUser internal and external alignment" {
// External padding (AlignmentBits) must be higher or equal to
@ -284,9 +280,9 @@ test "PackedUser internal and external alignment" {
}
fn testShellIndex(shell: []const u8) ?u6 {
if (std.mem.eql(u8, shell, "/bin/bash")) {
if (mem.eql(u8, shell, "/bin/bash")) {
return 0;
} else if (std.mem.eql(u8, shell, "/bin/zsh")) {
} else if (mem.eql(u8, shell, "/bin/zsh")) {
return 1;
}
return null;
@ -320,7 +316,7 @@ test "construct PackedUser section" {
.shell = "/usr/bin/nologin",
}, User{
.uid = 0,
.gid = math.maxInt(u32),
.gid = std.math.maxInt(u32),
.name = "Name" ** 8,
.gecos = "Gecos" ** 51,
.home = "Home" ** 16,
@ -352,12 +348,14 @@ test "construct PackedUser section" {
}
test "PackedUser.maxSize()" {
// TODO(motiejus) try using a slice that points to an array in stack.
// As of writing, I am getting a stack smashing error.
var buf = try ArrayList(u8).initCapacity(testing.allocator, PackedUser.maxSize());
defer buf.deinit();
const largeUser = User{
.uid = math.maxInt(u32),
.gid = math.maxInt(u32),
.uid = std.math.maxInt(u32),
.gid = std.math.maxInt(u32),
.name = "Name" ** 8, // 32
.gecos = "Gecos" ** 51, // 255
.home = "Home" ** 16, // 64