diff --git a/glnx-dirfd.h b/glnx-dirfd.h index 581b3032eb73289da00694fb45285ebe9da8422e..4824bc4b3f18d1b41d51b023daed67c70de46433 100644 --- a/glnx-dirfd.h +++ b/glnx-dirfd.h @@ -83,6 +83,34 @@ char *glnx_fdrel_abspath (int dfd, void glnx_gen_temp_name (gchar *tmpl); +/** + * glnx_ensure_dir: + * @dfd: directory fd + * @path: Directory path + * @mode: Mode + * @error: Return location for a #GError, or %NULL + * + * Wrapper around mkdirat() which ignores adds #GError support, ensures that + * it retries on %EINTR, and also ignores `EEXIST`. + * + * See also `glnx_shutil_mkdir_p_at()` for recursive handling. + * + * Returns: %TRUE on success, %FALSE otherwise + */ +static inline gboolean +glnx_ensure_dir (int dfd, + const char *path, + mode_t mode, + GError **error) +{ + if (TEMP_FAILURE_RETRY (mkdirat (dfd, path, mode)) != 0) + { + if (G_UNLIKELY (errno != EEXIST)) + return glnx_throw_errno_prefix (error, "mkdirat(%s)", path); + } + return TRUE; +} + gboolean glnx_mkdtempat (int dfd, gchar *tmpl, int mode, diff --git a/glnx-shutil.c b/glnx-shutil.c index 2e02eea4534eb3a19cf83ab133c73f390882f498..9124d7a4fd45abc6761c4bb806acb576ca14d558 100644 --- a/glnx-shutil.c +++ b/glnx-shutil.c @@ -185,6 +185,8 @@ mkdir_p_at_internal (int dfd, * * Similar to g_mkdir_with_parents(), except operates relative to the * directory fd @dfd. + * + * See also glnx_ensure_dir() for a non-recursive version. */ gboolean glnx_shutil_mkdir_p_at (int dfd,