diff options
author | rebecca “wiggles” turner <rbt@sent.as> | 2024-09-10 00:10:40 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@localhost> | 2024-09-10 00:10:40 +0000 |
commit | f5ae72d44552d9d75e3826c7e59403cbdb13d43a (patch) | |
tree | 02ea0a45ce3c7d18d18653d64d5ebf65ccdd2662 /src | |
parent | 8f7ab26f96ec025db3f922fc50f5184167662596 (diff) | |
parent | 742303dc3aa23462ba3e5e0497ae20487266adb3 (diff) |
Merge "Add `getCwd`" into main
Diffstat (limited to 'src')
-rw-r--r-- | src/libutil/file-system.cc | 14 | ||||
-rw-r--r-- | src/libutil/file-system.hh | 7 |
2 files changed, 16 insertions, 5 deletions
diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index 8c69c9864..1d3eba58f 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -18,15 +18,19 @@ namespace fs = std::filesystem; 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) { if (path.empty() || path[0] != '/') { if (!dir) { - char buf[PATH_MAX]; - if (!getcwd(buf, sizeof(buf))) { - throw SysError("cannot get cwd"); - } - path = concatStrings(buf, "/", path); + path = concatStrings(getCwd(), "/", path); } else { path = concatStrings(*dir, "/", path); } diff --git a/src/libutil/file-system.hh b/src/libutil/file-system.hh index 0a54d1a3b..d95e8eba5 100644 --- a/src/libutil/file-system.hh +++ b/src/libutil/file-system.hh @@ -30,6 +30,13 @@ 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 * is also canonicalised. |