]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/tmpfile-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #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. */
53 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
61 /* This is much like mkostemp() but is subject to umask(). */
62 int mkostemp_safe(char *pattern
) {
63 _cleanup_umask_ mode_t u
= 0;
70 fd
= mkostemp(pattern
, O_CLOEXEC
);
77 int fmkostemp_safe(char *pattern
, const char *mode
, FILE **ret_f
) {
81 fd
= mkostemp_safe(pattern
);
95 int tempfn_xxxxxx(const char *p
, const char *extra
, char **ret
) {
103 if (path_equal(p
, "/"))
111 * /foo/bar/.#<extra>waldoXXXXXX
115 if (!filename_is_valid(fn
))
118 extra
= strempty(extra
);
120 t
= new(char, strlen(p
) + 2 + strlen(extra
) + 6 + 1);
124 strcpy(stpcpy(stpcpy(stpcpy(mempcpy(t
, p
, fn
- p
), ".#"), extra
), fn
), "XXXXXX");
126 *ret
= path_simplify(t
, false);
130 int tempfn_random(const char *p
, const char *extra
, char **ret
) {
140 if (path_equal(p
, "/"))
148 * /foo/bar/.#<extra>waldobaa2a261115984a9
152 if (!filename_is_valid(fn
))
155 extra
= strempty(extra
);
157 t
= new(char, strlen(p
) + 2 + strlen(extra
) + 16 + 1);
161 x
= stpcpy(stpcpy(stpcpy(mempcpy(t
, p
, fn
- p
), ".#"), extra
), fn
);
164 for (i
= 0; i
< 16; i
++) {
165 *(x
++) = hexchar(u
& 0xF);
171 *ret
= path_simplify(t
, false);
175 int tempfn_random_child(const char *p
, const char *extra
, char **ret
) {
186 * /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
195 extra
= strempty(extra
);
197 t
= new(char, strlen(p
) + 3 + strlen(extra
) + 16 + 1);
202 x
= stpcpy(stpcpy(t
, ".#"), extra
);
204 x
= stpcpy(stpcpy(stpcpy(t
, p
), "/.#"), extra
);
207 for (i
= 0; i
< 16; i
++) {
208 *(x
++) = hexchar(u
& 0xF);
214 *ret
= path_simplify(t
, false);
218 int open_tmpfile_unlinkable(const char *directory
, int flags
) {
223 r
= tmp_dir(&directory
);
226 } else if (isempty(directory
))
229 /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
231 /* Try O_TMPFILE first, if it is supported */
232 fd
= open(directory
, flags
|O_TMPFILE
|O_EXCL
, S_IRUSR
|S_IWUSR
);
236 /* Fall back to unguessable name + unlinking */
237 p
= strjoina(directory
, "/systemd-tmp-XXXXXX");
239 fd
= mkostemp_safe(p
);
248 int open_tmpfile_linkable(const char *target
, int flags
, char **ret_path
) {
249 _cleanup_free_
char *tmp
= NULL
;
255 /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
256 assert((flags
& O_EXCL
) == 0);
258 /* Creates a temporary file, that shall be renamed to "target" later. If possible, this uses O_TMPFILE – in
259 * which case "ret_path" will be returned as NULL. If not possible a the tempoary path name used is returned in
260 * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
262 fd
= open_parent(target
, O_TMPFILE
|flags
, 0640);
268 log_debug_errno(fd
, "Failed to use O_TMPFILE for %s: %m", target
);
270 r
= tempfn_random(target
, NULL
, &tmp
);
274 fd
= open(tmp
, O_CREAT
|O_EXCL
|O_NOFOLLOW
|O_NOCTTY
|flags
, 0640);
278 *ret_path
= TAKE_PTR(tmp
);
283 int link_tmpfile(int fd
, const char *path
, const char *target
) {
289 /* Moves a temporary file created with open_tmpfile() above into its final place. if "path" is NULL an fd
290 * created with O_TMPFILE is assumed, and linkat() is used. Otherwise it is assumed O_TMPFILE is not supported
291 * on the directory, and renameat2() is used instead.
293 * Note that in both cases we will not replace existing files. This is because linkat() does not support this
294 * operation currently (renameat2() does), and there is no nice way to emulate this. */
297 r
= rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, target
);
301 char proc_fd_path
[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(fd
) + 1];
303 xsprintf(proc_fd_path
, "/proc/self/fd/%i", fd
);
305 if (linkat(AT_FDCWD
, proc_fd_path
, AT_FDCWD
, target
, AT_SYMLINK_FOLLOW
) < 0)
312 int mkdtemp_malloc(const char *template, char **ret
) {
313 _cleanup_free_
char *p
= NULL
;
319 p
= strdup(template);
327 p
= strjoin(tmp
, "/XXXXXX");