1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-09 12:58:26 +03:00

tmpfile-util: Add fopen_temporary_at()

This commit is contained in:
Daan De Meyer 2022-09-26 11:59:21 +02:00
parent 86e69a44b4
commit e624d4cf07
2 changed files with 14 additions and 10 deletions

View File

@ -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);

View File

@ -1,9 +1,13 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <fcntl.h>
#include <stdio.h>
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);