[libglnx] dirfd: Add API to get an absolute path from a dfd/relpath



commit 381ca54ee3a47de291d26a5db8772732fb4a9d59
Author: Colin Walters <walters verbum org>
Date:   Fri Apr 17 09:30:03 2015 -0400

    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.

 glnx-dirfd.c |   22 ++++++++++++++++++++++
 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


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]