]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/tmpfile-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
10 #include "memfd-util.h"
11 #include "missing_fcntl.h"
12 #include "missing_syscall.h"
13 #include "path-util.h"
14 #include "process-util.h"
15 #include "random-util.h"
16 #include "stdio-util.h"
17 #include "string-util.h"
18 #include "tmpfile-util.h"
19 #include "umask-util.h"
21 int fopen_temporary(const char *path
, FILE **_f
, char **_temp_path
) {
30 r
= tempfn_xxxxxx(path
, NULL
, &t
);
34 fd
= mkostemp_safe(t
);
54 /* This is much like mkostemp() but is subject to umask(). */
55 int mkostemp_safe(char *pattern
) {
56 _cleanup_umask_ mode_t u
= 0;
63 fd
= mkostemp(pattern
, O_CLOEXEC
);
70 int fmkostemp_safe(char *pattern
, const char *mode
, FILE **ret_f
) {
74 fd
= mkostemp_safe(pattern
);
88 int tempfn_xxxxxx(const char *p
, const char *extra
, char **ret
) {
96 if (path_equal(p
, "/"))
104 * /foo/bar/.#<extra>waldoXXXXXX
108 if (!filename_is_valid(fn
))
111 extra
= strempty(extra
);
113 t
= new(char, strlen(p
) + 2 + strlen(extra
) + 6 + 1);
117 strcpy(stpcpy(stpcpy(stpcpy(mempcpy(t
, p
, fn
- p
), ".#"), extra
), fn
), "XXXXXX");
119 *ret
= path_simplify(t
, false);
123 int tempfn_random(const char *p
, const char *extra
, char **ret
) {
133 if (path_equal(p
, "/"))
141 * /foo/bar/.#<extra>waldobaa2a261115984a9
145 if (!filename_is_valid(fn
))
148 extra
= strempty(extra
);
150 t
= new(char, strlen(p
) + 2 + strlen(extra
) + 16 + 1);
154 x
= stpcpy(stpcpy(stpcpy(mempcpy(t
, p
, fn
- p
), ".#"), extra
), fn
);
157 for (i
= 0; i
< 16; i
++) {
158 *(x
++) = hexchar(u
& 0xF);
164 *ret
= path_simplify(t
, false);
168 int tempfn_random_child(const char *p
, const char *extra
, char **ret
) {
179 * /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
188 extra
= strempty(extra
);
190 t
= new(char, strlen(p
) + 3 + strlen(extra
) + 16 + 1);
195 x
= stpcpy(stpcpy(t
, ".#"), extra
);
197 x
= stpcpy(stpcpy(stpcpy(t
, p
), "/.#"), extra
);
200 for (i
= 0; i
< 16; i
++) {
201 *(x
++) = hexchar(u
& 0xF);
207 *ret
= path_simplify(t
, false);
211 int open_tmpfile_unlinkable(const char *directory
, int flags
) {
216 r
= tmp_dir(&directory
);
219 } else if (isempty(directory
))
222 /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
224 /* Try O_TMPFILE first, if it is supported */
225 fd
= open(directory
, flags
|O_TMPFILE
|O_EXCL
, S_IRUSR
|S_IWUSR
);
229 /* Fall back to unguessable name + unlinking */
230 p
= strjoina(directory
, "/systemd-tmp-XXXXXX");
232 fd
= mkostemp_safe(p
);
241 int open_tmpfile_linkable(const char *target
, int flags
, char **ret_path
) {
242 _cleanup_free_
char *tmp
= NULL
;
248 /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
249 assert((flags
& O_EXCL
) == 0);
251 /* Creates a temporary file, that shall be renamed to "target" later. If possible, this uses O_TMPFILE – in
252 * which case "ret_path" will be returned as NULL. If not possible a the tempoary path name used is returned in
253 * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
255 fd
= open_parent(target
, O_TMPFILE
|flags
, 0640);
261 log_debug_errno(fd
, "Failed to use O_TMPFILE for %s: %m", target
);
263 r
= tempfn_random(target
, NULL
, &tmp
);
267 fd
= open(tmp
, O_CREAT
|O_EXCL
|O_NOFOLLOW
|O_NOCTTY
|flags
, 0640);
271 *ret_path
= TAKE_PTR(tmp
);
276 int link_tmpfile(int fd
, const char *path
, const char *target
) {
282 /* Moves a temporary file created with open_tmpfile() above into its final place. if "path" is NULL an fd
283 * created with O_TMPFILE is assumed, and linkat() is used. Otherwise it is assumed O_TMPFILE is not supported
284 * on the directory, and renameat2() is used instead.
286 * Note that in both cases we will not replace existing files. This is because linkat() does not support this
287 * operation currently (renameat2() does), and there is no nice way to emulate this. */
290 r
= rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, target
);
294 char proc_fd_path
[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(fd
) + 1];
296 xsprintf(proc_fd_path
, "/proc/self/fd/%i", fd
);
298 if (linkat(AT_FDCWD
, proc_fd_path
, AT_FDCWD
, target
, AT_SYMLINK_FOLLOW
) < 0)
305 int mkdtemp_malloc(const char *template, char **ret
) {
306 _cleanup_free_
char *p
= NULL
;
312 p
= strdup(template);
320 p
= strjoin(tmp
, "/XXXXXX");