1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
10 #include "hexdecoct.h"
12 #include "memfd-util.h"
13 #include "missing_fcntl.h"
14 #include "missing_syscall.h"
15 #include "path-util.h"
16 #include "process-util.h"
17 #include "random-util.h"
18 #include "stdio-util.h"
19 #include "string-util.h"
20 #include "tmpfile-util.h"
21 #include "umask-util.h"
23 int fopen_temporary(const char *path
, FILE **_f
, char **_temp_path
) {
32 r
= tempfn_xxxxxx(path
, NULL
, &t
);
36 fd
= mkostemp_safe(t
);
42 /* This assumes that returned FILE object is short-lived and used within the same single-threaded
43 * context and never shared externally, hence locking is not necessary. */
45 r
= fdopen_unlocked(fd
, "w", &f
);
59 /* This is much like mkostemp() but is subject to umask(). */
60 int mkostemp_safe(char *pattern
) {
61 _unused_ _cleanup_umask_ mode_t u
= umask(0077);
66 fd
= mkostemp(pattern
, O_CLOEXEC
);
73 int fmkostemp_safe(char *pattern
, const char *mode
, FILE **ret_f
) {
77 fd
= mkostemp_safe(pattern
);
91 int tempfn_xxxxxx(const char *p
, const char *extra
, char **ret
) {
99 if (path_equal(p
, "/"))
107 * /foo/bar/.#<extra>waldoXXXXXX
111 if (!filename_is_valid(fn
))
114 extra
= strempty(extra
);
116 t
= new(char, strlen(p
) + 2 + strlen(extra
) + 6 + 1);
120 strcpy(stpcpy(stpcpy(stpcpy(mempcpy(t
, p
, fn
- p
), ".#"), extra
), fn
), "XXXXXX");
122 *ret
= path_simplify(t
, false);
126 int tempfn_random(const char *p
, const char *extra
, char **ret
) {
136 if (path_equal(p
, "/"))
144 * /foo/bar/.#<extra>waldobaa2a261115984a9
148 if (!filename_is_valid(fn
))
151 extra
= strempty(extra
);
153 t
= new(char, strlen(p
) + 2 + strlen(extra
) + 16 + 1);
157 x
= stpcpy(stpcpy(stpcpy(mempcpy(t
, p
, fn
- p
), ".#"), extra
), fn
);
160 for (i
= 0; i
< 16; i
++) {
161 *(x
++) = hexchar(u
& 0xF);
167 *ret
= path_simplify(t
, false);
171 int tempfn_random_child(const char *p
, const char *extra
, char **ret
) {
182 * /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
191 extra
= strempty(extra
);
193 t
= new(char, strlen(p
) + 3 + strlen(extra
) + 16 + 1);
198 x
= stpcpy(stpcpy(t
, ".#"), extra
);
200 x
= stpcpy(stpcpy(stpcpy(t
, p
), "/.#"), extra
);
203 for (i
= 0; i
< 16; i
++) {
204 *(x
++) = hexchar(u
& 0xF);
210 *ret
= path_simplify(t
, false);
214 int open_tmpfile_unlinkable(const char *directory
, int flags
) {
219 r
= tmp_dir(&directory
);
222 } else if (isempty(directory
))
225 /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
227 /* Try O_TMPFILE first, if it is supported */
228 fd
= open(directory
, flags
|O_TMPFILE
|O_EXCL
, S_IRUSR
|S_IWUSR
);
232 /* Fall back to unguessable name + unlinking */
233 p
= strjoina(directory
, "/systemd-tmp-XXXXXX");
235 fd
= mkostemp_safe(p
);
244 int open_tmpfile_linkable(const char *target
, int flags
, char **ret_path
) {
245 _cleanup_free_
char *tmp
= NULL
;
251 /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
252 assert((flags
& O_EXCL
) == 0);
254 /* Creates a temporary file, that shall be renamed to "target" later. If possible, this uses O_TMPFILE – in
255 * which case "ret_path" will be returned as NULL. If not possible a the tempoary path name used is returned in
256 * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
258 fd
= open_parent(target
, O_TMPFILE
|flags
, 0640);
264 log_debug_errno(fd
, "Failed to use O_TMPFILE for %s: %m", target
);
266 r
= tempfn_random(target
, NULL
, &tmp
);
270 fd
= open(tmp
, O_CREAT
|O_EXCL
|O_NOFOLLOW
|O_NOCTTY
|flags
, 0640);
274 *ret_path
= TAKE_PTR(tmp
);
279 int link_tmpfile(int fd
, const char *path
, const char *target
) {
285 /* Moves a temporary file created with open_tmpfile() above into its final place. if "path" is NULL an fd
286 * created with O_TMPFILE is assumed, and linkat() is used. Otherwise it is assumed O_TMPFILE is not supported
287 * on the directory, and renameat2() is used instead.
289 * Note that in both cases we will not replace existing files. This is because linkat() does not support this
290 * operation currently (renameat2() does), and there is no nice way to emulate this. */
293 r
= rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, target
);
297 char proc_fd_path
[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(fd
) + 1];
299 xsprintf(proc_fd_path
, "/proc/self/fd/%i", fd
);
301 if (linkat(AT_FDCWD
, proc_fd_path
, AT_FDCWD
, target
, AT_SYMLINK_FOLLOW
) < 0)
308 int mkdtemp_malloc(const char *template, char **ret
) {
309 _cleanup_free_
char *p
= NULL
;
315 p
= strdup(template);
323 p
= path_join(tmp
, "XXXXXX");