]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/tmpfile-util.c
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 _cleanup_umask_ mode_t u
= 0;
68 fd
= mkostemp(pattern
, O_CLOEXEC
);
75 int fmkostemp_safe(char *pattern
, const char *mode
, FILE **ret_f
) {
79 fd
= mkostemp_safe(pattern
);
93 int tempfn_xxxxxx(const char *p
, const char *extra
, char **ret
) {
101 if (path_equal(p
, "/"))
109 * /foo/bar/.#<extra>waldoXXXXXX
113 if (!filename_is_valid(fn
))
116 extra
= strempty(extra
);
118 t
= new(char, strlen(p
) + 2 + strlen(extra
) + 6 + 1);
122 strcpy(stpcpy(stpcpy(stpcpy(mempcpy(t
, p
, fn
- p
), ".#"), extra
), fn
), "XXXXXX");
124 *ret
= path_simplify(t
, false);
128 int tempfn_random(const char *p
, const char *extra
, char **ret
) {
138 if (path_equal(p
, "/"))
146 * /foo/bar/.#<extra>waldobaa2a261115984a9
150 if (!filename_is_valid(fn
))
153 extra
= strempty(extra
);
155 t
= new(char, strlen(p
) + 2 + strlen(extra
) + 16 + 1);
159 x
= stpcpy(stpcpy(stpcpy(mempcpy(t
, p
, fn
- p
), ".#"), extra
), fn
);
162 for (i
= 0; i
< 16; i
++) {
163 *(x
++) = hexchar(u
& 0xF);
169 *ret
= path_simplify(t
, false);
173 int tempfn_random_child(const char *p
, const char *extra
, char **ret
) {
184 * /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
193 extra
= strempty(extra
);
195 t
= new(char, strlen(p
) + 3 + strlen(extra
) + 16 + 1);
200 x
= stpcpy(stpcpy(t
, ".#"), extra
);
202 x
= stpcpy(stpcpy(stpcpy(t
, p
), "/.#"), extra
);
205 for (i
= 0; i
< 16; i
++) {
206 *(x
++) = hexchar(u
& 0xF);
212 *ret
= path_simplify(t
, false);
216 int open_tmpfile_unlinkable(const char *directory
, int flags
) {
221 r
= tmp_dir(&directory
);
224 } else if (isempty(directory
))
227 /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
229 /* Try O_TMPFILE first, if it is supported */
230 fd
= open(directory
, flags
|O_TMPFILE
|O_EXCL
, S_IRUSR
|S_IWUSR
);
234 /* Fall back to unguessable name + unlinking */
235 p
= strjoina(directory
, "/systemd-tmp-XXXXXX");
237 fd
= mkostemp_safe(p
);
246 int open_tmpfile_linkable(const char *target
, int flags
, char **ret_path
) {
247 _cleanup_free_
char *tmp
= NULL
;
253 /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
254 assert((flags
& O_EXCL
) == 0);
256 /* Creates a temporary file, that shall be renamed to "target" later. If possible, this uses O_TMPFILE – in
257 * which case "ret_path" will be returned as NULL. If not possible a the tempoary path name used is returned in
258 * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
260 fd
= open_parent(target
, O_TMPFILE
|flags
, 0640);
266 log_debug_errno(fd
, "Failed to use O_TMPFILE for %s: %m", target
);
268 r
= tempfn_random(target
, NULL
, &tmp
);
272 fd
= open(tmp
, O_CREAT
|O_EXCL
|O_NOFOLLOW
|O_NOCTTY
|flags
, 0640);
276 *ret_path
= TAKE_PTR(tmp
);
281 int link_tmpfile(int fd
, const char *path
, const char *target
) {
287 /* Moves a temporary file created with open_tmpfile() above into its final place. if "path" is NULL an fd
288 * created with O_TMPFILE is assumed, and linkat() is used. Otherwise it is assumed O_TMPFILE is not supported
289 * on the directory, and renameat2() is used instead.
291 * Note that in both cases we will not replace existing files. This is because linkat() does not support this
292 * operation currently (renameat2() does), and there is no nice way to emulate this. */
295 r
= rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, target
);
299 char proc_fd_path
[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(fd
) + 1];
301 xsprintf(proc_fd_path
, "/proc/self/fd/%i", fd
);
303 if (linkat(AT_FDCWD
, proc_fd_path
, AT_FDCWD
, target
, AT_SYMLINK_FOLLOW
) < 0)
310 int mkdtemp_malloc(const char *template, char **ret
) {
311 _cleanup_free_
char *p
= NULL
;
317 p
= strdup(template);
325 p
= strjoin(tmp
, "/XXXXXX");