From 09de309b6eea5cd509e7b524b83384a37496f71d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Wed, 6 Jul 2022 09:26:24 +0300 Subject: [PATCH] turbo-analyze now shows something --- build.zig | 7 + ...emove_dupe_gids.awk => group-dupegids.awk} | 0 contrib/{assign0.awk => passwd-assign0.awk} | 0 src/DB.zig | 8 +- src/analyze.zig | 151 ++++++------------ 5 files changed, 58 insertions(+), 108 deletions(-) rename contrib/{remove_dupe_gids.awk => group-dupegids.awk} (100%) rename contrib/{assign0.awk => passwd-assign0.awk} (100%) diff --git a/build.zig b/build.zig index 13ad7c6..c849e83 100644 --- a/build.zig +++ b/build.zig @@ -50,6 +50,13 @@ pub fn build(b: *zbs.Builder) void { exe.install(); } + { + const exe = b.addExecutable("turbo-analyze", "src/analyze.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + exe.install(); + } + { const src_test = b.addTest("src/test_all.zig"); addCmphDeps(src_test, cmph); diff --git a/contrib/remove_dupe_gids.awk b/contrib/group-dupegids.awk similarity index 100% rename from contrib/remove_dupe_gids.awk rename to contrib/group-dupegids.awk diff --git a/contrib/assign0.awk b/contrib/passwd-assign0.awk similarity index 100% rename from contrib/assign0.awk rename to contrib/passwd-assign0.awk diff --git a/src/DB.zig b/src/DB.zig index 5a2665f..b016584 100644 --- a/src/DB.zig +++ b/src/DB.zig @@ -204,7 +204,7 @@ pub fn iov(self: *const DB) BoundedArray(os.iovec_const, DB_fields.len * 2) { return result; } -const DBNumbers = struct { +pub const DBNumbers = struct { header: u64, bdz_gid: u64, bdz_groupname: u64, @@ -223,9 +223,9 @@ const DBNumbers = struct { }; // in blocks -fn fieldLengths(header: *const Header) DBNumbers { +pub fn fieldLengths(header: *const Header) DBNumbers { return DBNumbers{ - .header = undefined, + .header = comptime DB.nblocks_n(u64, @sizeOf(Header)), .bdz_gid = header.nblocks_bdz_gid, .bdz_groupname = header.nblocks_bdz_groupname, .bdz_uid = header.nblocks_bdz_uid, @@ -244,7 +244,7 @@ fn fieldLengths(header: *const Header) DBNumbers { } // in blocks -fn fieldOffsets(lengths: DBNumbers) DBNumbers { +pub fn fieldOffsets(lengths: DBNumbers) DBNumbers { var result: DBNumbers = undefined; result.header = 0; var offset = comptime nblocks_n(u64, @sizeOf(Header)); diff --git a/src/analyze.zig b/src/analyze.zig index 7242c25..384f1eb 100644 --- a/src/analyze.zig +++ b/src/analyze.zig @@ -1,8 +1,9 @@ const std = @import("std"); const fs = std.fs; const io = std.io; -const mem = std.mem; const os = std.os; +const fmt = std.fmt; +const mem = std.mem; const heap = std.heap; const meta = std.meta; const ArrayList = std.ArrayList; @@ -14,7 +15,7 @@ const DB = @import("DB.zig"); const File = @import("File.zig"); const PackedUser = @import("PackedUser.zig"); const Header = @import("header.zig").Header; -const section_length = @import("header.zig").section_length; +const section_length_bits = @import("header.zig").section_length_bits; const usage = \\usage: turbo-analyze [options] [db.turbo] @@ -26,7 +27,8 @@ const usage = const Info = struct { fname: []const u8, - size_file: os.off_t, + size_file: []const u8, + bytes_file: os.off_t, version: meta.fieldInfo(Header, .version).field_type, endian: meta.fieldInfo(Header, .endian).field_type, ptr_size: meta.fieldInfo(Header, .ptr_size).field_type, @@ -35,65 +37,8 @@ const Info = struct { users: meta.fieldInfo(Header, .num_users).field_type, groups: meta.fieldInfo(Header, .num_groups).field_type, shells: meta.fieldInfo(Header, .num_shells).field_type, - nbytes_header: u64, - nbytes_bdz_gid: u64, - offset_bdz_gid: u64, - nbytes_bdz_groupname: u64, - offset_bdz_groupname: u64, - nbytes_bdz_uid: u64, - offset_bdz_uid: u64, - nbytes_bdz_username: u64, - offset_bdz_username: u64, - nbytes_idx_gid2group: u64, - offset_idx_gid2group: u64, - nbytes_idx_groupname2group: u64, - offset_idx_groupname2group: u64, - nbytes_idx_uid2user: u64, - offset_idx_uid2user: u64, - nbytes_idx_name2user: u64, - offset_idx_name2user: u64, - nbytes_shell_index: u64, - offset_shell_index: u64, - nbytes_shell_blob: u64, - offset_shell_blob: u64, - nbytes_groups: u64, - offset_groups: u64, - nbytes_users: u64, - offset_users: u64, - nbytes_additional_gids: u64, - offset_additional_gids: u64, }; -const template = - \\File: {[fname]s} - \\Size: {[size_file]:.2} ({[size_file]d} bytes) - \\Version: {[version}d} - \\Endian: {[endian]s} - \\Pointer size: {[ptr_size]}d - \\Buffer size in bytes for getgr: {[getgr_bufsize]d} - \\Buffer size in bytes for getpw: {[getpw_bufsize]d} - \\ - \\Users: {[users]d} - \\Groups: {[groups]d} - \\Shells: {[shells]d} - \\Sections: - \\ Name Offset Size - \\ header 0 {[nbytes_header]:.2} - \\ bdz_gid {[offset_bdz_gid]d<21} {[nbytes_bdz_gid]:.2} - \\ bdz_groupname {[offset_bdz_groupname]d<21}{[nbytes_bdz_groupname]:.2} - \\ bdz_uid {[offset_bdz_uid]d<21} {[nbytes_bdz_uid]:.2} - \\ bdz_username {[offset_bdz_username]d} {[nbytes_bdz_username]:.2} - \\ idx_gid2group {[offset_idx_group]d} {[nbytes_idx_group]:.2} - \\ idx_groupname2group {[offset_idx_group]d} {[nbytes_idx_group]:.2} - \\ idx_uid2user {[offset_idx_user]d} {[nbytes_idx_user]:.2} - \\ idx_name2user {[offset_idx_user]d} {[nbytes_idx_user]:.2} - \\ shell_index {[offset_shell_index]d} {[nbytes_shell_index]:.2} - \\ shell_blob {[offset_shell_blob]d} {[nbytes_shell_blob]:.2} - \\ groups {[offset_groups]d} {[nbytes_groups]:.2} - \\ users {[offset_users]d} {[nbytes_users]:.2} - \\ additional_gids {[offset_additional_gids]d} {[nbytes_additional_gids]:.2} -; - pub fn main() !void { // This line is here because of https://github.com/ziglang/zig/issues/7807 const argv: []const [*:0]const u8 = os.argv; @@ -110,7 +55,6 @@ fn execute( stderr: anytype, argv: []const [*:0]const u8, ) u8 { - _ = stdout; const myflags = flags.parse(argv, &[_]flags.Flag{ .{ .name = "-h", .kind = .boolean }, }) catch { @@ -162,24 +106,15 @@ fn execute( defer file.close(); const db = file.db; - // const offset_bdz_gid = @ptrToInt(db.bdz_gid.ptr) - @ptrToInt(db.header.ptr); - // const offset_bdz_groupname= @ptrToInt(db.bdz_groupname) - offset_bdz_gid; - // const offset_bdz_uid= @ptrToInt(db.bdz_uid) - offset_bdz_groupname; - // const offset_bdz_username= @ptrToInt(db.bdz_username) - offset_bdz_uid; - // const offset_idx_gid2group= @ptrToInt(db.idx_gid2group) - offset_bdz_username; - // const offset_idx_groupname2group= @ptrToInt(db.idx_groupname2group) - offset_idx_gid2group; - // const offset_idx_uid2user= @ptrToInt(db.idx_uid2user) - offset_idx_groupname2group; - // const offset_idx_name2user= @ptrToInt(db.idx_name2user) - offset_idx_uid2user; - // const offset_shell_index= @ptrToInt(db.shell_index) - offset_idx_name2user; - // const offset_shell_blob= @ptrToInt(db.shell_blob) - offset_shell_index; - // const offset_groups= @ptrToInt(db.groups) - offset_shell_blob; - // const offset_users= @ptrToInt(db.users) - offset_groups; - // const offset_groupmembers= @ptrToInt(db.groupmembers) - offset_users; - // const offset_additional_gids= @ptrToInt(db.additional_gids) - offset_groupmembers; + var scratch: [16]u8 = undefined; + const buf = fmt.bufPrint(scratch[0..], "{:.2}", .{ + fmt.fmtIntSizeBin(@intCast(u64, file_size_bytes)), + }) catch unreachable; const info = Info{ .fname = db_file, - .size_file = file_size_bytes, + .size_file = buf, + .bytes_file = file_size_bytes, .version = db.header.version, .endian = db.header.endian, .ptr_size = db.header.ptr_size, @@ -188,35 +123,43 @@ fn execute( .users = db.header.num_users, .groups = db.header.num_groups, .shells = db.header.num_shells, - .nbytes_header = section_length * DB.nblocks_n(u64, @sizeOf(Header)), - .offset_bdz_gid = 0, - .nbytes_bdz_gid = 0, - .nbytes_bdz_groupname = 0, - .offset_bdz_groupname = 0, - .nbytes_bdz_uid = 0, - .offset_bdz_uid = 0, - .nbytes_bdz_username = 0, - .offset_bdz_username = 0, - .nbytes_idx_gid2group = 0, - .offset_idx_gid2group = 0, - .nbytes_idx_groupname2group = 0, - .offset_idx_groupname2group = 0, - .nbytes_idx_uid2user = 0, - .offset_idx_uid2user = 0, - .nbytes_idx_name2user = 0, - .offset_idx_name2user = 0, - .nbytes_shell_index = 0, - .offset_shell_index = 0, - .nbytes_shell_blob = 0, - .offset_shell_blob = 0, - .nbytes_groups = 0, - .offset_groups = 0, - .nbytes_users = 0, - .offset_users = 0, - .nbytes_additional_gids = 0, - .offset_additional_gids = 0, }; - _ = info; + + const template = + \\File: {[fname]s} + \\Size: {[size_file]s} ({[bytes_file]d} bytes) + \\Version: {[version]d} + \\Endian: {[endian]s} + \\Pointer size: {[ptr_size]}d + \\Buffer size in bytes for getgr: {[getgr_bufsize]d} + \\Buffer size in bytes for getpw: {[getpw_bufsize]d} + \\ + \\Users: {[users]d} + \\Groups: {[groups]d} + \\Shells: {[shells]d} + \\Sections: + \\ Name Blocks Size Offset + \\ + ; + stdout.print(template, info) catch {}; + + var lengths = DB.fieldLengths(db.header); + var offsets = DB.fieldOffsets(lengths); + + inline for (meta.fields(DB.DBNumbers)) |field| { + const length = @field(lengths, field.name); + const size = fmt.bufPrint(scratch[0..], "{:.2}", .{ + fmt.fmtIntSizeBin(@intCast(u64, length << section_length_bits)), + }) catch unreachable; + + const offset = @field(offsets, field.name); + stdout.print(" {s:<19}{:8} {s:<12} {:8}\n", .{ + field.name, + length, + size, + offset, + }) catch {}; + } return 0; }