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);
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;
int open_tmpfile_unlinkable(const char *directory, int flags);
int open_tmpfile_linkable(const char *target, int flags, char **ret_path);
+int fopen_tmpfile_linkable(const char *target, int flags, char **ret_path, FILE **ret_file);
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);
static int install_loader_config(const char *esp_path) {
_cleanup_(unlink_and_freep) char *t = NULL;
_cleanup_fclose_ FILE *f = NULL;
- _cleanup_close_ int fd = -1;
const char *p;
int r;
if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
return 0;
- fd = open_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t);
- if (fd < 0)
- return log_error_errno(fd, "Failed to open \"%s\" for writing: %m", p);
-
- f = take_fdopen(&fd, "w");
- if (!f)
- return log_oom();
+ r = fopen_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t, &f);
+ if (r < 0)
+ return log_error_errno(r, "Failed to open \"%s\" for writing: %m", p);
fprintf(f, "#timeout 3\n"
"#console-mode keep\n");
fprintf(f, "default %s-*\n", arg_entry_token);
}
- r = fflush_sync_and_check(f);
- if (r < 0)
- return log_error_errno(r, "Failed to write \"%s\": %m", p);
-
- r = link_tmpfile(fileno(f), t, p);
+ r = flink_tmpfile(f, t, p);
if (r == -EEXIST)
return 0; /* Silently skip creation if the file exists now (recheck) */
if (r < 0)