Skip to content

Commit

Permalink
Fix getUserName behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
balsoft committed Feb 7, 2024
1 parent 7a49064 commit c5f8a40
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/libutil/users.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ namespace nix {
std::string getUserName(uid_t uid)
{
auto pw = getpwuid(uid);
std::string name = pw ? pw->pw_name : getEnv("USER").value_or("");
if (name.empty())
if (!pw)
throw Error("cannot figure out user name");
return name;
return pw->pw_name;
}

std::string getUserName()
{
return getUserName(getuid());
uid_t uid = getuid();
if (getpwuid(uid))
return getUserName(uid);
else {
if (auto name = getEnv("USER"))
return *name;
else
throw Error("cannot figure our user name");
}
}

std::string getGroupName(gid_t gid)
Expand Down

0 comments on commit c5f8a40

Please sign in to comment.