Merge "Add getCwd" into main

This commit is contained in:
rebecca “wiggles” turner 2024-09-10 00:10:40 +00:00 committed by Gerrit Code Review
commit f5ae72d445
2 changed files with 16 additions and 5 deletions

View file

@ -18,15 +18,19 @@ namespace fs = std::filesystem;
namespace nix {
Path absPath(Path path, std::optional<PathView> dir, bool resolveSymlinks)
{
if (path.empty() || path[0] != '/') {
if (!dir) {
Path getCwd() {
char buf[PATH_MAX];
if (!getcwd(buf, sizeof(buf))) {
throw SysError("cannot get cwd");
}
path = concatStrings(buf, "/", path);
return Path(buf);
}
Path absPath(Path path, std::optional<PathView> dir, bool resolveSymlinks)
{
if (path.empty() || path[0] != '/') {
if (!dir) {
path = concatStrings(getCwd(), "/", path);
} else {
path = concatStrings(*dir, "/", path);
}

View file

@ -29,6 +29,13 @@ namespace nix {
struct Sink;
struct Source;
/**
* Get the current working directory.
*
* Throw an error if the current directory cannot get got.
*/
Path getCwd();
/**
* @return An absolutized path, resolving paths relative to the
* specified directory, or the current directory otherwise. The path