summaryrefslogtreecommitdiff
path: root/src/basic/tmpfile-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2022-03-17 18:18:04 +0100
committerLennart Poettering <lennart@poettering.net>2022-03-22 00:34:48 +0100
commita5b30e156a0e01d01ef49298d40149930031633f (patch)
tree39b2e2e2deee325816f4228da67fae606021381e /src/basic/tmpfile-util.c
parentf6ad0282c9b9260076e74195e35a4c0c278537c7 (diff)
downloadsystemd-a5b30e156a0e01d01ef49298d40149930031633f.tar.gz
bootctl: generalize open_tmpfile_linkable() use a bit
We want FILE* here, instead of a plain fd. Let's generalize this in tmpfile-util.c, so we can reuse it later easily.
Diffstat (limited to 'src/basic/tmpfile-util.c')
-rw-r--r--src/basic/tmpfile-util.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/basic/tmpfile-util.c b/src/basic/tmpfile-util.c
index cf3bbad1c4..e0a338c163 100644
--- a/src/basic/tmpfile-util.c
+++ b/src/basic/tmpfile-util.c
@@ -275,6 +275,28 @@ int open_tmpfile_linkable(const char *target, int flags, char **ret_path) {
return fd;
}
+int fopen_tmpfile_linkable(const char *target, int flags, char **ret_path, FILE **ret_file) {
+ _cleanup_free_ char *path = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
+ _cleanup_close_ int fd = -1;
+
+ assert(target);
+ assert(ret_file);
+ assert(ret_path);
+
+ fd = open_tmpfile_linkable(target, flags, &path);
+ if (fd < 0)
+ return fd;
+
+ f = take_fdopen(&fd, "w");
+ if (!f)
+ return -ENOMEM;
+
+ *ret_path = TAKE_PTR(path);
+ *ret_file = TAKE_PTR(f);
+ return 0;
+}
+
int link_tmpfile(int fd, const char *path, const char *target) {
assert(fd >= 0);
assert(target);
@@ -292,6 +314,23 @@ int link_tmpfile(int fd, const char *path, const char *target) {
return RET_NERRNO(linkat(AT_FDCWD, FORMAT_PROC_FD_PATH(fd), AT_FDCWD, target, AT_SYMLINK_FOLLOW));
}
+int flink_tmpfile(FILE *f, const char *path, const char *target) {
+ int fd, r;
+
+ assert(f);
+ assert(target);
+
+ fd = fileno(f);
+ if (fd < 0) /* Not all FILE* objects encapsulate fds */
+ return -EBADF;
+
+ r = fflush_sync_and_check(f);
+ if (r < 0)
+ return r;
+
+ return link_tmpfile(fd, path, target);
+}
+
int mkdtemp_malloc(const char *template, char **ret) {
_cleanup_free_ char *p = NULL;
int r;