diff options
author | Colin Walters <walters@verbum.org> | 2015-04-17 09:30:03 -0400 |
---|---|---|
committer | Colin Walters <walters@verbum.org> | 2015-04-17 09:33:58 -0400 |
commit | 381ca54ee3a47de291d26a5db8772732fb4a9d59 (patch) | |
tree | c4e69c6ce42162cd48e39cbe0a0374d245ae9966 | |
parent | c231a3b845981d4b237e8f63ef3503e070009386 (diff) | |
download | libglnx-381ca54ee3a47de291d26a5db8772732fb4a9d59.tar.gz |
dirfd: Add API to get an absolute path from a dfd/relpath
There are a lot of APIs that still only take absolute paths, such as
librpm (and everything above it). I plan to use this in rpm-ostree to
convert temporary directories that I'm accessing fd-relative back into
absolutes until such time as fd-relative APIs are plumbed through the
stack more.
-rw-r--r-- | glnx-dirfd.c | 22 | ||||
-rw-r--r-- | glnx-dirfd.h | 3 |
2 files changed, 25 insertions, 0 deletions
diff --git a/glnx-dirfd.c b/glnx-dirfd.c index 4b7d5a0..a51eb3d 100644 --- a/glnx-dirfd.c +++ b/glnx-dirfd.c @@ -204,3 +204,25 @@ glnx_dirfd_iterator_clear (GLnxDirFdIterator *dfd_iter) (void) closedir (real_dfd_iter->d); real_dfd_iter->initialized = FALSE; } + +/** + * glnx_fdrel_abspath: + * @dfd: Directory fd + * @path: Path + * + * Turn a fd-relative pair into something that can be used for legacy + * APIs expecting absolute paths. + * + * This is Linux specific, and only valid inside this process (unless + * you set up the child process to have the exact same fd number, but + * don't try that). + */ +char * +glnx_fdrel_abspath (int dfd, + const char *path) +{ + dfd = glnx_dirfd_canonicalize (dfd); + if (dfd == AT_FDCWD) + return g_strdup (path); + return g_strdup_printf ("/proc/self/fd/%d/%s", dfd, path); +} diff --git a/glnx-dirfd.h b/glnx-dirfd.h index 585d16a..bcf7f1e 100644 --- a/glnx-dirfd.h +++ b/glnx-dirfd.h @@ -74,4 +74,7 @@ gboolean glnx_opendirat (int dfd, int *out_fd, GError **error); +char *glnx_fdrel_abspath (int dfd, + const char *path); + G_END_DECLS |