diff options
author | Colin Walters <walters@verbum.org> | 2014-12-11 21:37:43 -0500 |
---|---|---|
committer | Colin Walters <walters@verbum.org> | 2014-12-17 21:38:23 -0500 |
commit | b964ff719128775ef8d08307c9eabf77154a29d9 (patch) | |
tree | 1ab93e15de54ba3df0eedb4c310597b810a68337 | |
parent | 70c019db163b3a6d23d2efefa682daeab3b7a61f (diff) | |
download | libgsystem-b964ff719128775ef8d08307c9eabf77154a29d9.tar.gz |
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.
-rw-r--r-- | src/gsystem-file-utils.c | 44 | ||||
-rw-r--r-- | 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, |