From: Daan De Meyer Date: Mon, 26 Sep 2022 09:59:21 +0000 (+0200) Subject: tmpfile-util: Add fopen_temporary_at() X-Git-Tag: v253-rc1~561^2~8 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e624d4cf0791ffb4aafb1078f0d16a8930d088c1;p=thirdparty%2Fsystemd.git tmpfile-util: Add fopen_temporary_at() --- diff --git a/src/basic/tmpfile-util.c b/src/basic/tmpfile-util.c index f3f9062121e..e0e58472d37 100644 --- a/src/basic/tmpfile-util.c +++ b/src/basic/tmpfile-util.c @@ -19,14 +19,14 @@ #include "tmpfile-util.h" #include "umask-util.h" -int fopen_temporary(const char *path, FILE **ret_f, char **ret_temp_path) { +int fopen_temporary_at(int dir_fd, const char *path, FILE **ret_file, char **ret_temp_path) { _cleanup_fclose_ FILE *f = NULL; _cleanup_free_ char *t = NULL; _cleanup_close_ int fd = -1; int r; if (path) { - r = tempfn_xxxxxx(path, NULL, &t); + r = tempfn_random(path, NULL, &t); if (r < 0) return r; } else { @@ -36,12 +36,12 @@ int fopen_temporary(const char *path, FILE **ret_f, char **ret_temp_path) { if (r < 0) return r; - t = path_join(d, "XXXXXX"); - if (!t) - return -ENOMEM; + r = tempfn_random_child(d, NULL, &t); + if (r < 0) + return r; } - fd = mkostemp_safe(t); + fd = openat(dir_fd, t, O_CLOEXEC|O_NOCTTY|O_RDWR|O_CREAT|O_EXCL, 0600); if (fd < 0) return -errno; @@ -50,12 +50,12 @@ int fopen_temporary(const char *path, FILE **ret_f, char **ret_temp_path) { r = take_fdopen_unlocked(&fd, "w", &f); if (r < 0) { - (void) unlink(t); + (void) unlinkat(dir_fd, t, 0); return r; } - if (ret_f) - *ret_f = TAKE_PTR(f); + if (ret_file) + *ret_file = TAKE_PTR(f); if (ret_temp_path) *ret_temp_path = TAKE_PTR(t); diff --git a/src/basic/tmpfile-util.h b/src/basic/tmpfile-util.h index 96e37cc5783..4af28b9da34 100644 --- a/src/basic/tmpfile-util.h +++ b/src/basic/tmpfile-util.h @@ -1,9 +1,13 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include #include -int fopen_temporary(const char *path, FILE **_f, char **_temp_path); +int fopen_temporary_at(int dir_fd, const char *path, FILE **ret_file, char **ret_path); +static inline int fopen_temporary(const char *path, FILE **ret_file, char **ret_path) { + return fopen_temporary_at(AT_FDCWD, path, ret_file, ret_path); +} int mkostemp_safe(char *pattern); int fmkostemp_safe(char *pattern, const char *mode, FILE**_f);