ls: Don't do caching now that password/group file reading is more reasonable
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-05-20 22:10:01 +02:00
parent 2c798b8bf0
commit 04d074538f
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -6,54 +6,17 @@
#include <os/FileSystem.h>
#include <pwd.h>
struct UsernameCache
{
uid_t uid;
String name;
};
struct GroupCache
{
gid_t gid;
String name;
};
Vector<UsernameCache> g_user_cache;
Vector<GroupCache> g_group_cache;
Result<void> 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())
void find_user_and_group(struct stat& st, StringView& owner, StringView& group)
{
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 {};
}
Result<int> luna_main(int argc, char** argv)
@ -119,7 +82,7 @@ Result<int> 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)
{