From 04d074538fc3d96e2e2ce77e842f9304595ee187 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 20 May 2023 22:10:01 +0200 Subject: [PATCH] ls: Don't do caching now that password/group file reading is more reasonable --- apps/ls.cpp | 57 ++++++++++------------------------------------------- 1 file changed, 10 insertions(+), 47 deletions(-) diff --git a/apps/ls.cpp b/apps/ls.cpp index ee89ca82..b0bca358 100644 --- a/apps/ls.cpp +++ b/apps/ls.cpp @@ -6,54 +6,17 @@ #include #include -struct UsernameCache +void find_user_and_group(struct stat& st, StringView& owner, StringView& group) { - uid_t uid; - String name; -}; + auto* pw = getpwuid(st.st_uid); + if (!pw) owner = "???"; + else + owner = pw->pw_name; -struct GroupCache -{ - gid_t gid; - String name; -}; - -Vector g_user_cache; -Vector g_group_cache; - -Result find_user_and_group(struct stat& st, StringView& owner, StringView& group) -{ - for (const auto& user : g_user_cache) - { - if (user.uid == st.st_uid) owner = user.name.view(); - } - if (owner.is_empty()) - { - auto* pw = getpwuid(st.st_uid); - if (!pw) owner = "???"; - else - owner = pw->pw_name; - - auto name = TRY(String::from_string_view(owner)); - TRY(g_user_cache.try_append({ st.st_uid, move(name) })); - } - - for (const auto& grp : g_group_cache) - { - if (grp.gid == st.st_gid) group = grp.name.view(); - } - if (group.is_empty()) - { - auto* grp = getgrgid(st.st_gid); - if (!grp) group = "???"; - else - group = grp->gr_name; - - auto name = TRY(String::from_string_view(group)); - TRY(g_group_cache.try_append({ st.st_gid, move(name) })); - } - - return {}; + auto* grp = getgrgid(st.st_gid); + if (!grp) group = "???"; + else + group = grp->gr_name; } Result luna_main(int argc, char** argv) @@ -119,7 +82,7 @@ Result luna_main(int argc, char** argv) StringView owner; StringView group; - TRY(find_user_and_group(st, owner, group)); + find_user_and_group(st, owner, group); if (!human_readable && !si) {