ls but with io_uring

Fix crash with --long on macos #8

merged opened by reykjalin.org targeting main

It turns out macOS does several things that go against the assumptions currently built into lsr:

  1. The /etc/passwd file has an extensive comment at the top of the file; several lines starting with #.
  2. macOS (and apparently some other operating systems) allow negative uids and gids.

So, the changes here:

  1. Allow for comments in /etc/passwd; and
  2. Use i33 instead of u32, posix.uid_t and posix.gid_t when parsing and using uids and gids.
Labels

None yet.

Participants 2
AT URI
at://did:plc:ph2n2oeirk7t5zjmxwfkhojk/sh.tangled.repo.pull/3lopi2qdbog22
+16 -4
Diff #0
+16 -4
src/main.zig
··· 636 636 }; 637 637 638 638 const User = struct { 639 - uid: posix.uid_t, 639 + uid: if (builtin.os.tag == .macos) i33 else posix.uid_t, 640 640 name: []const u8, 641 641 642 642 fn lessThan(_: void, lhs: User, rhs: User) bool { ··· 645 645 }; 646 646 647 647 const Group = struct { 648 - gid: posix.gid_t, 648 + gid: if (builtin.os.tag == .macos) i33 else posix.gid_t, 649 649 name: []const u8, 650 650 651 651 fn lessThan(_: void, lhs: Group, rhs: Group) bool { ··· 905 905 // <name>:<throwaway>:<uid><...garbage> 906 906 while (lines.next()) |line| { 907 907 if (line.len == 0) continue; 908 + if (std.mem.startsWith(u8, line, "#")) continue; 909 + 908 910 var iter = std.mem.splitScalar(u8, line, ':'); 909 911 const name = iter.first(); 910 912 _ = iter.next(); ··· 912 914 913 915 const user: User = .{ 914 916 .name = name, 915 - .uid = try std.fmt.parseInt(u32, uid, 10), 917 + .uid = try std.fmt.parseInt( 918 + if (builtin.os.tag == .macos) i33 else u32, 919 + uid, 920 + 10, 921 + ), 916 922 }; 917 923 918 924 cmd.users.appendAssumeCapacity(user); ··· 947 953 // <name>:<throwaway>:<uid><...garbage> 948 954 while (lines.next()) |line| { 949 955 if (line.len == 0) continue; 956 + if (std.mem.startsWith(u8, line, "#")) continue; 957 + 950 958 var iter = std.mem.splitScalar(u8, line, ':'); 951 959 const name = iter.first(); 952 960 _ = iter.next(); ··· 954 962 955 963 const group: Group = .{ 956 964 .name = name, 957 - .gid = try std.fmt.parseInt(u32, gid, 10), 965 + .gid = try std.fmt.parseInt( 966 + if (builtin.os.tag == .macos) i33 else u32, 967 + gid, 968 + 10, 969 + ), 958 970 }; 959 971 960 972 cmd.groups.appendAssumeCapacity(group);

Submissions

sign up or login to add to the discussion
reykjalin.org submitted #0
reykjalin.org

I chose i33 here just so we wouldn't have to include a comptime if/else check everywhere we're doing something with a uid or gid. Since u32 fits into i33 things just seemlessly work.

I don't think the structs are packed, so maybe we should bump this up to i64? Don't know what makes the most sense here, but i33 seems pretty good so far.

rockorager.dev

Thanks for the fixes! I think sticking with i33 is the right call.

pull request successfully merged