summaryrefslogtreecommitdiff
path: root/glnx-dirfd.c
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2015-04-17 09:30:03 -0400
committerColin Walters <walters@verbum.org>2015-04-17 09:33:58 -0400
commit381ca54ee3a47de291d26a5db8772732fb4a9d59 (patch)
treec4e69c6ce42162cd48e39cbe0a0374d245ae9966 /glnx-dirfd.c
parentc231a3b845981d4b237e8f63ef3503e070009386 (diff)
downloadlibglnx-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.
Diffstat (limited to 'glnx-dirfd.c')
-rw-r--r--glnx-dirfd.c22
1 files changed, 22 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);
+}