Skip to content

Commit

Permalink
Missing argument in p/invoke to retrieve username.
Browse files Browse the repository at this point in the history
  • Loading branch information
carlossanlop committed May 27, 2022
1 parent 2d3bff8 commit 1dd8c07
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ internal unsafe struct Passwd
}

/// <summary>
/// Gets the user name associated to the current user's UID.
/// Gets the user name associated to the specified UID.
/// </summary>
/// <returns>On success, return a string with the user name. On failure, returns an empty string.</returns>
internal static unsafe string GetUserNameFromPasswd()
/// <param name="uid">The user ID.</param>
/// <returns>On success, return a string with the user name associated to the specified UID. On failure, returns an empty string.</returns>
internal static unsafe string GetUserNameFromPasswd(uint uid)
{
// First try with a buffer that should suffice for 99% of cases.
string? username;
const int BufLen = Interop.Sys.Passwd.InitialBufferSize;
byte* stackBuf = stackalloc byte[BufLen];
if (TryGetUserNameFromPasswd(stackBuf, BufLen, out username))
if (TryGetUserNameFromPasswd(uid, stackBuf, BufLen, out username))
{
return username ?? string.Empty;
}
Expand All @@ -48,19 +49,19 @@ internal static unsafe string GetUserNameFromPasswd()
byte[] heapBuf = new byte[lastBufLen];
fixed (byte* buf = &heapBuf[0])
{
if (TryGetUserNameFromPasswd(buf, heapBuf.Length, out username))
if (TryGetUserNameFromPasswd(uid, buf, heapBuf.Length, out username))
{
return username ?? string.Empty;
}
}
}
}

private static unsafe bool TryGetUserNameFromPasswd(byte* buf, int bufLen, out string? username)
private static unsafe bool TryGetUserNameFromPasswd(uint uid, byte* buf, int bufLen, out string? username)
{
// Call getpwuid_r to get the passwd struct
Interop.Sys.Passwd passwd;
int error = Interop.Sys.GetPwUidR(Interop.Sys.GetEUid(), out passwd, buf, bufLen);
int error = Interop.Sys.GetPwUidR(uid, out passwd, buf, bufLen);

// If the call succeeds, give back the user name retrieved
if (error == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ partial void ReadFileFromDiskAndWriteToArchiveStreamAsEntry(string fullPath, str
entry._header._uid = (int)status.Uid;
if (!_userIdentifiers.TryGetValue(status.Uid, out string? uName))
{
uName = Interop.Sys.GetUserNameFromPasswd();
uName = Interop.Sys.GetUserNameFromPasswd(status.Uid);
_userIdentifiers.Add(status.Uid, uName);
}
entry._header._uName = uName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ partial void VerifyPlatformSpecificMetadata(string filePath, TarEntry entry)
if (entry is PosixTarEntry posix)
{
string gname = Interop.Sys.GetGroupName(status.Gid);
string uname = Interop.Sys.GetUserNameFromPasswd();
string uname = Interop.Sys.GetUserNameFromPasswd(status.Uid);

Assert.Equal(gname, posix.GroupName);
Assert.Equal(uname, posix.UserName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static string MachineName
}
}

public static string UserName => Interop.Sys.GetUserNameFromPasswd();
public static string UserName => Interop.Sys.GetUserNameFromPasswd(Interop.Sys.GetEUid());

[MethodImplAttribute(MethodImplOptions.NoInlining)] // Avoid inlining PInvoke frame into the hot path
private static int GetProcessId() => Interop.Sys.GetPid();
Expand Down

0 comments on commit 1dd8c07

Please sign in to comment.