From b964ff719128775ef8d08307c9eabf77154a29d9 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Thu, 11 Dec 2014 21:37:43 -0500 Subject: 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(+) 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 @@ -491,6 +491,50 @@ gs_file_open_dir_fd_at (int parent_dfd, return TRUE; } +/** + * 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 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, -- cgit v1.2.1