diff options
author | Qyriad <qyriad@qyriad.me> | 2024-07-12 16:15:33 -0600 |
---|---|---|
committer | Qyriad <qyriad@qyriad.me> | 2024-07-15 15:26:53 -0600 |
commit | d9c51ec4e5919245dcc5e9f2f5532f4d85be5218 (patch) | |
tree | 4727d01f8fdffb5dd411645135637c91478f04a3 /src/libutil/file-system.cc | |
parent | 1eb5d22132a57487e0cb84a65ecd35814624c0e5 (diff) |
libutil: implement a realPath() utility
Just a wrapper around POSIX realpath().
Change-Id: I2593770285dbae573eace490efce5b272b00b001
Diffstat (limited to 'src/libutil/file-system.cc')
-rw-r--r-- | src/libutil/file-system.cc | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index 278e5187c..f0199d36f 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -1,4 +1,5 @@ #include <sys/time.h> +#include <cstdlib> #include <filesystem> #include <atomic> @@ -106,6 +107,24 @@ Path canonPath(PathView path, bool resolveSymlinks) return s.empty() ? "/" : std::move(s); } +Path realPath(Path const & path) +{ + // With nullptr, realpath() malloc's and returns a new c-string. + char * resolved = realpath(path.c_str(), nullptr); + int saved = errno; + if (resolved == nullptr) { + throw SysError(saved, "cannot get realpath for '%s'", path); + } + + Finally const _free([&] { free(resolved); }); + + // There's not really a from_raw_parts() for std::string. + // The copy is not a big deal. + Path ret(resolved); + + return ret; +} + void chmodPath(const Path & path, mode_t mode) { if (chmod(path.c_str(), mode) == -1) |