[libgsystem] fsutils: Add two small APIs for opendirat()



commit b964ff719128775ef8d08307c9eabf77154a29d9
Author: Colin Walters <walters verbum org>
Date:   Thu Dec 11 21:37:43 2014 -0500

    fsutils: Add two small APIs for opendirat()
    
    We already had gs_file_open_dir_fd, but it didn't support O_NOFOLLOW,
    which is quite useful.
    
    Also add a raw function wrapping the core bits we need to opendir(),
    also taking a follow boolean.

 src/gsystem-file-utils.c |   44 ++++++++++++++++++++++++++++++++++++++++++++
 src/gsystem-file-utils.h |   10 ++++++++++
 2 files changed, 54 insertions(+), 0 deletions(-)
---
diff --git a/src/gsystem-file-utils.c b/src/gsystem-file-utils.c
index c157d1d..30ccc9d 100644
--- a/src/gsystem-file-utils.c
+++ b/src/gsystem-file-utils.c
@@ -492,6 +492,50 @@ gs_file_open_dir_fd_at (int            parent_dfd,
 }
 
 /**
+ * gs_opendirat_with_errno:
+ * @dfd: File descriptor for origin directory
+ * @name: Pathname, relative to @dfd
+ * @follow: Whether or not to follow symbolic links
+ *
+ * Use openat() to open a directory, using a standard set of flags.
+ * This function sets errno.
+ */
+int
+gs_opendirat_with_errno (int dfd, const char *path, gboolean follow)
+{
+  int flags = O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOCTTY;
+  if (!follow)
+    flags |= O_NOFOLLOW;
+  return openat (dfd, path, flags);
+}
+
+/**
+ * gs_opendirat:
+ * @dfd: File descriptor for origin directory
+ * @path: Pathname, relative to @dfd
+ * @follow: Whether or not to follow symbolic links
+ * @error: Error
+ *
+ * Use openat() to open a directory, using a standard set of flags.
+ */
+gboolean
+gs_opendirat (int             dfd,
+              const char     *path,
+              gboolean        follow,
+              int            *out_fd,
+              GError        **error)
+{
+  int ret = gs_opendirat_with_errno (dfd, path, follow);
+  if (ret == -1)
+    {
+      _set_error_from_errno ("openat", error);
+      return FALSE;
+    }
+  *out_fd = ret;
+  return TRUE;
+}
+
+/**
  * gs_file_open_in_tmpdir_at:
  * @tmpdir_fd: Directory to place temporary file
  * @mode: Default mode (will be affected by umask)
diff --git a/src/gsystem-file-utils.h b/src/gsystem-file-utils.h
index d792c81..82b7070 100644
--- a/src/gsystem-file-utils.h
+++ b/src/gsystem-file-utils.h
@@ -81,6 +81,16 @@ gboolean gs_file_open_dir_fd_at (int            parent_dfd,
                                  GCancellable  *cancellable,
                                  GError       **error);
 
+int gs_opendirat_with_errno (int           dfd,
+                             const char   *path,
+                             gboolean      follow);
+gboolean gs_opendirat (int             dfd,
+                       const char     *path,
+                       gboolean        follow,
+                       int            *out_fd,
+                       GError        **error);
+
+
 gboolean gs_file_open_in_tmpdir_at (int                tmpdir_fd,
                                     int                mode,
                                     char             **out_name,


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