diff options
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 631cf076b..124a0aef7 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -17,15 +17,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 e49323e84..23685ee08 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. |