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