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
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
2c798b8bf0
commit
04d074538f
41
apps/ls.cpp
41
apps/ls.cpp
@ -6,54 +6,17 @@
|
|||||||
#include <os/FileSystem.h>
|
#include <os/FileSystem.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
|
|
||||||
struct UsernameCache
|
void find_user_and_group(struct stat& st, StringView& owner, StringView& group)
|
||||||
{
|
|
||||||
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())
|
|
||||||
{
|
{
|
||||||
auto* pw = getpwuid(st.st_uid);
|
auto* pw = getpwuid(st.st_uid);
|
||||||
if (!pw) owner = "???";
|
if (!pw) owner = "???";
|
||||||
else
|
else
|
||||||
owner = pw->pw_name;
|
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);
|
auto* grp = getgrgid(st.st_gid);
|
||||||
if (!grp) group = "???";
|
if (!grp) group = "???";
|
||||||
else
|
else
|
||||||
group = grp->gr_name;
|
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)
|
Result<int> luna_main(int argc, char** argv)
|
||||||
@ -119,7 +82,7 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
StringView owner;
|
StringView owner;
|
||||||
StringView group;
|
StringView group;
|
||||||
|
|
||||||
TRY(find_user_and_group(st, owner, group));
|
find_user_and_group(st, owner, group);
|
||||||
|
|
||||||
if (!human_readable && !si)
|
if (!human_readable && !si)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user