From: Daan De Meyer Date: Sat, 24 Sep 2022 15:10:40 +0000 (+0200) Subject: tmpfile-util: Introduce mkdtemp_open() X-Git-Tag: v253-rc1~561^2~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=86e69a44b4432c05cc5c6e9af912db14b8fbea72;p=thirdparty%2Fsystemd.git tmpfile-util: Introduce mkdtemp_open() --- diff --git a/src/basic/tmpfile-util.c b/src/basic/tmpfile-util.c index 34d3016ba93..f3f9062121e 100644 --- a/src/basic/tmpfile-util.c +++ b/src/basic/tmpfile-util.c @@ -358,3 +358,23 @@ int mkdtemp_malloc(const char *template, char **ret) { *ret = TAKE_PTR(p); return 0; } + +int mkdtemp_open(const char *template, int flags, char **ret) { + _cleanup_free_ char *p = NULL; + int fd, r; + + r = mkdtemp_malloc(template, &p); + if (r < 0) + return r; + + fd = RET_NERRNO(open(p, O_DIRECTORY|O_CLOEXEC|flags)); + if (fd < 0) { + (void) rmdir(p); + return fd; + } + + if (ret) + *ret = TAKE_PTR(p); + + return fd; +} diff --git a/src/basic/tmpfile-util.h b/src/basic/tmpfile-util.h index 610cbaf87e7..96e37cc5783 100644 --- a/src/basic/tmpfile-util.h +++ b/src/basic/tmpfile-util.h @@ -19,3 +19,4 @@ int link_tmpfile(int fd, const char *path, const char *target); int flink_tmpfile(FILE *f, const char *path, const char *target); int mkdtemp_malloc(const char *template, char **ret); +int mkdtemp_open(const char *template, int flags, char **ret);