Add getCwd
It's nice for this to be a separate function and not just inline in `absPath`. Prepared as part ofcl/1865
, though I don't think I actually ended up using it there. Change-Id:I24d9d4a984cee0af587010baf04b3939a1c147ec
This commit is contained in:
parent
b6884388a1
commit
742303dc3a
|
@ -17,15 +17,19 @@ namespace fs = std::filesystem;
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
|
Path getCwd() {
|
||||||
|
char buf[PATH_MAX];
|
||||||
|
if (!getcwd(buf, sizeof(buf))) {
|
||||||
|
throw SysError("cannot get cwd");
|
||||||
|
}
|
||||||
|
return Path(buf);
|
||||||
|
}
|
||||||
|
|
||||||
Path absPath(Path path, std::optional<PathView> dir, bool resolveSymlinks)
|
Path absPath(Path path, std::optional<PathView> dir, bool resolveSymlinks)
|
||||||
{
|
{
|
||||||
if (path.empty() || path[0] != '/') {
|
if (path.empty() || path[0] != '/') {
|
||||||
if (!dir) {
|
if (!dir) {
|
||||||
char buf[PATH_MAX];
|
path = concatStrings(getCwd(), "/", path);
|
||||||
if (!getcwd(buf, sizeof(buf))) {
|
|
||||||
throw SysError("cannot get cwd");
|
|
||||||
}
|
|
||||||
path = concatStrings(buf, "/", path);
|
|
||||||
} else {
|
} else {
|
||||||
path = concatStrings(*dir, "/", path);
|
path = concatStrings(*dir, "/", path);
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,13 @@ namespace nix {
|
||||||
struct Sink;
|
struct Sink;
|
||||||
struct Source;
|
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
|
* @return An absolutized path, resolving paths relative to the
|
||||||
* specified directory, or the current directory otherwise. The path
|
* specified directory, or the current directory otherwise. The path
|
||||||
|
|
Loading…
Reference in a new issue