summaryrefslogtreecommitdiffstats
path: root/src/basic/tmpfile-util.c
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2022-09-26 11:59:21 +0200
committerDaan De Meyer <daan.j.demeyer@gmail.com>2022-11-09 11:14:10 +0100
commite624d4cf0791ffb4aafb1078f0d16a8930d088c1 (patch)
treeebca14b52ee6ed69c4b5d19c64da56f5d7652c66 /src/basic/tmpfile-util.c
parenttmpfile-util: Introduce mkdtemp_open() (diff)
downloadsystemd-e624d4cf0791ffb4aafb1078f0d16a8930d088c1.tar.xz
systemd-e624d4cf0791ffb4aafb1078f0d16a8930d088c1.zip
tmpfile-util: Add fopen_temporary_at()
Diffstat (limited to 'src/basic/tmpfile-util.c')
-rw-r--r--src/basic/tmpfile-util.c18
1 files changed, 9 insertions, 9 deletions
diff --git a/src/basic/tmpfile-util.c b/src/basic/tmpfile-util.c
index f3f9062121..e0e58472d3 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);