1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-18 00:12:43 +01:00
nix/src/libutil/windows/users.cc
John Ericson 39b2a399ad Start building the scheduler for Windows
Building derivations is a lot harder, but the downloading goals is
portable enough.

The "common channel" code is due to Volth. I wonder if there is a way we
can factor it out into separate functions / files to avoid some
within-function CPP.

Co-authored-by: volth <volth@volth.com>
2024-05-10 20:23:59 -04:00

52 lines
1.1 KiB
C++

#include "util.hh"
#include "users.hh"
#include "environment-variables.hh"
#include "file-system.hh"
#include "windows-error.hh"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
namespace nix {
using namespace nix::windows;
std::string getUserName()
{
// Get the required buffer size
DWORD size = 0;
if (!GetUserNameA(nullptr, &size)) {
auto lastError = GetLastError();
if (lastError != ERROR_INSUFFICIENT_BUFFER)
throw WinError(lastError, "cannot figure out size of user name");
}
std::string name;
// Allocate a buffer of sufficient size
//
// - 1 because no need for null byte
name.resize(size - 1);
// Retrieve the username
if (!GetUserNameA(&name[0], &size))
throw WinError("cannot figure out user name");
return name;
}
Path getHome()
{
static Path homeDir = []()
{
Path homeDir = getEnv("USERPROFILE").value_or("C:\\Users\\Default");
assert(!homeDir.empty());
return canonPath(homeDir);
}();
return homeDir;
}
bool isRootUser() {
return false;
}
}