aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/file-system.cc
diff options
context:
space:
mode:
authorRebecca Turner <rbt@sent.as>2024-08-26 11:20:35 -0700
committerRebecca Turner <rbt@sent.as>2024-08-26 11:22:07 -0700
commit742303dc3aa23462ba3e5e0497ae20487266adb3 (patch)
treed98d656769c4d529507bca830458ea2d4bd700e3 /src/libutil/file-system.cc
parentb6884388a1281d70bb4e5bb12e1cadd34bb832f0 (diff)
Add `getCwd`
It's nice for this to be a separate function and not just inline in `absPath`. Prepared as part of cl/1865, though I don't think I actually ended up using it there. Change-Id: I24d9d4a984cee0af587010baf04b3939a1c147ec
Diffstat (limited to 'src/libutil/file-system.cc')
-rw-r--r--src/libutil/file-system.cc14
1 files changed, 9 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);
}