Debian Codegraph @

src/basic/tmpfile-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "errno-util.h"
8 #include "fd-util.h"
9 #include "fileio.h"
10 #include "fs-util.h"
11 #include "log.h"
12 #include "path-util.h"
13 #include "random-util.h"
14 #include "string-util.h"
15 #include "sync-util.h"
16 #include "tmpfile-util.h"
17 #include "umask-util.h"
18
19 static int fopen_temporary_internal(int dir_fd, const char *path, FILE **ret_file) {
20 _cleanup_fclose_ FILE *f = NULL;
21 _cleanup_close_ int fd = -EBADF;
22 int r;
23
24 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
25 assert(path);
26
27 fd = openat(dir_fd, path, O_CLOEXEC|O_NOCTTY|O_RDWR|O_CREAT|O_EXCL, 0600);
28 if (fd < 0)
29 return -errno;
30
31 /* This assumes that returned FILE object is short-lived and used within the same single-threaded
32 * context and never shared externally, hence locking is not necessary. */
33
34 r = take_fdopen_unlocked(&fd, "w", &f);
35 if (r < 0) {
36 (void) unlinkat(dir_fd, path, 0);
37 return r;
38 }
39
40 if (ret_file)
41 *ret_file = TAKE_PTR(f);
42
43 return 0;
44 }
45
46 int fopen_temporary_at(int dir_fd, const char *path, FILE **ret_file, char **ret_path) {
47 _cleanup_free_ char *t = NULL;
48 int r;
49
50 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
51 assert(path);
52
53 r = tempfn_random(path, NULL, &t);
54 if (r < 0)
55 return r;
56
57 r = fopen_temporary_internal(dir_fd, t, ret_file);
58 if (r < 0)
59 return r;
60
61 if (ret_path)
62 *ret_path = TAKE_PTR(t);
63
64 return 0;
65 }
66
67 int fopen_temporary_child_at(int dir_fd, const char *path, FILE **ret_file, char **ret_path) {
68 _cleanup_free_ char *t = NULL;
69 int r;
70
71 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
72
73 if (!path) {
74 r = tmp_dir(&path);
75 if (r < 0)
76 return r;
77 }
78
79 r = tempfn_random_child(path, NULL, &t);
80 if (r < 0)
81 return r;
82
83 r = fopen_temporary_internal(dir_fd, t, ret_file);
84 if (r < 0)
85 return r;
86
87 if (ret_path)
88 *ret_path = TAKE_PTR(t);
89
90 return 0;
91 }
92
93 /* This is much like mkostemp() but is subject to umask(). */
94 int mkostemp_safe(char *pattern) {
95 assert(pattern);
96 BLOCK_WITH_UMASK(0077);
97 return RET_NERRNO(mkostemp(pattern, O_CLOEXEC));
98 }
99
100 int fmkostemp_safe(char *pattern, const char *mode, FILE **ret_f) {
101 _cleanup_close_ int fd = -EBADF;
102 FILE *f;
103
104 fd = mkostemp_safe(pattern);
105 if (fd < 0)
106 return fd;
107
108 f = take_fdopen(&fd, mode);
109 if (!f)
110 return -errno;
111
112 *ret_f = f;
113 return 0;
114 }
115
116 void unlink_tempfilep(char (*p)[]) {
117 assert(p);
118
119 /* If the file is created with mkstemp(), it will (almost always) change the suffix.
120 * Treat this as a sign that the file was successfully created. We ignore both the rare case
121 * where the original suffix is used and unlink failures. */
122
123 if (!endswith(*p, ".XXXXXX"))
124 (void) unlink(*p);
125 }
126
127 static int tempfn_build(const char *p, const char *pre, const char *post, bool child, char **ret) {
128 _cleanup_free_ char *d = NULL, *fn = NULL, *nf = NULL, *result = NULL;
129 size_t len_pre, len_post, len_add;
130 int r;
131
132 assert(p);
133 assert(ret);
134
135 /*
136 * Turns this:
137 * /foo/bar/waldo
138 *
139 * Into this :
140 * /foo/bar/waldo/.#<pre><post> (child == true)
141 * /foo/bar/.#<pre>waldo<post> (child == false)
142 */
143
144 if (pre && strchr(pre, '/'))
145 return -EINVAL;
146
147 if (post && strchr(post, '/'))
148 return -EINVAL;
149
150 len_pre = strlen_ptr(pre);
151 len_post = strlen_ptr(post);
152 /* NAME_MAX is counted *without* the trailing NUL byte. */
153 if (len_pre > NAME_MAX - STRLEN(".#") ||
154 len_post > NAME_MAX - STRLEN(".#") - len_pre)
155 return -EINVAL;
156
157 len_add = len_pre + len_post + STRLEN(".#");
158
159 if (child) {
160 d = strdup(p);
161 if (!d)
162 return -ENOMEM;
163 } else {
164 r = path_split_prefix_filename(p, &d, &fn);
165 if (r < 0)
166 return r;
167
168 /* Truncate the filename if it would become too long after mangling. */
169 strshorten(fn, NAME_MAX - len_add);
170 }
171
172 nf = strjoin(".#", strempty(pre), strempty(fn), strempty(post));
173 if (!nf)
174 return -ENOMEM;
175
176 if (d) {
177 if (!path_extend(&d, nf))
178 return -ENOMEM;
179
180 result = path_simplify(TAKE_PTR(d));
181 } else
182 result = TAKE_PTR(nf);
183
184 if (!path_is_valid(result)) /* New path is not valid? (Maybe because too long?) Refuse. */
185 return -EINVAL;
186
187 *ret = TAKE_PTR(result);
188 return 0;
189 }
190
191 int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
192 /*
193 * Turns this:
194 * /foo/bar/waldo
195 *
196 * Into this:
197 * /foo/bar/.#<extra>waldoXXXXXX
198 */
199
200 return tempfn_build(p, extra, "XXXXXX", /* child= */ false, ret);
201 }
202
203 int tempfn_random(const char *p, const char *extra, char **ret) {
204 _cleanup_free_ char *s = NULL;
205
206 assert(p);
207 assert(ret);
208
209 /*
210 * Turns this:
211 * /foo/bar/waldo
212 *
213 * Into this:
214 * /foo/bar/.#<extra>waldobaa2a261115984a9
215 */
216
217 if (asprintf(&s, "%016" PRIx64, random_u64()) < 0)
218 return -ENOMEM;
219
220 return tempfn_build(p, extra, s, /* child= */ false, ret);
221 }
222
223 int tempfn_random_child(const char *p, const char *extra, char **ret) {
224 _cleanup_free_ char *s = NULL;
225 int r;
226
227 assert(ret);
228
229 /* Turns this:
230 * /foo/bar/waldo
231 * Into this:
232 * /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
233 */
234
235 if (!p) {
236 r = tmp_dir(&p);
237 if (r < 0)
238 return r;
239 }
240
241 if (asprintf(&s, "%016" PRIx64, random_u64()) < 0)
242 return -ENOMEM;
243
244 return tempfn_build(p, extra, s, /* child= */ true, ret);
245 }
246
247 int open_tmpfile_unlinkable(const char *directory, int flags) {
248 int r;
249
250 if (!directory) {
251 r = tmp_dir(&directory);
252 if (r < 0)
253 return r;
254 } else if (isempty(directory))
255 return -EINVAL;
256
257 /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
258
259 /* Try O_TMPFILE first, if it is supported */
260 int fd = open(directory, flags|O_TMPFILE|O_EXCL, S_IRUSR|S_IWUSR);
261 if (fd >= 0)
262 return fd;
263
264 /* Fall back to unguessable name + unlinking */
265 _cleanup_free_ char *p = path_join(directory, "/systemd-tmp-XXXXXX");
266 if (!p)
267 return -ENOMEM;
268
269 fd = mkostemp_safe(p);
270 if (fd < 0)
271 return fd;
272
273 (void) unlink(p);
274
275 return fd;
276 }
277
278 int open_tmpfile_linkable_at(int dir_fd, const char *target, int flags, char **ret_path) {
279 int r;
280
281 assert(target);
282 assert(ret_path);
283
284 /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
285 assert((flags & O_EXCL) == 0);
286
287 /* Creates a temporary file, that shall be renamed to "target" later. If possible, this uses O_TMPFILE – in
288 * which case "ret_path" will be returned as NULL. If not possible the temporary path name used is returned in
289 * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
290
291 int fd = open_parent_at(dir_fd, target, O_TMPFILE|flags, 0640);
292 if (fd >= 0) {
293 *ret_path = NULL;
294 return fd;
295 }
296
297 if (!ERRNO_IS_NEG_NOT_SUPPORTED(fd))
298 log_debug_errno(fd, "Failed to use O_TMPFILE for %s: %m", target);
299
300 _cleanup_free_ char *tmp = NULL;
301 r = tempfn_random(target, NULL, &tmp);
302 if (r < 0)
303 return r;
304
305 fd = openat(dir_fd, tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|flags, 0640);
306 if (fd < 0)
307 return -errno;
308
309 *ret_path = TAKE_PTR(tmp);
310
311 return fd;
312 }
313
314 int fopen_tmpfile_linkable_at(int dir_fd, const char *target, int flags, char **ret_path, FILE **ret_file) {
315 _cleanup_free_ char *path = NULL;
316 _cleanup_fclose_ FILE *f = NULL;
317 _cleanup_close_ int fd = -EBADF;
318
319 assert(target);
320 assert(ret_file);
321 assert(ret_path);
322
323 fd = open_tmpfile_linkable_at(dir_fd, target, flags, &path);
324 if (fd < 0)
325 return fd;
326
327 f = take_fdopen(&fd, "w");
328 if (!f)
329 return -ENOMEM;
330
331 *ret_path = TAKE_PTR(path);
332 *ret_file = TAKE_PTR(f);
333 return 0;
334 }
335
336 int link_tmpfile_at(int fd, int dir_fd, const char *path, const char *target, LinkTmpfileFlags flags) {
337 int r;
338
339 assert(fd >= 0);
340 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
341 assert(target);
342
343 /* Moves a temporary file created with open_tmpfile() above into its final place. If "path" is NULL
344 * an fd created with O_TMPFILE is assumed, and linkat() is used. Otherwise it is assumed O_TMPFILE
345 * is not supported on the directory, and renameat2() is used instead. */
346
347 if (FLAGS_SET(flags, LINK_TMPFILE_SYNC) && fsync(fd) < 0)
348 return -errno;
349
350 if (path) {
351 if (FLAGS_SET(flags, LINK_TMPFILE_REPLACE))
352 r = RET_NERRNO(renameat(dir_fd, path, dir_fd, target));
353 else
354 r = rename_noreplace(dir_fd, path, dir_fd, target);
355 } else {
356 if (FLAGS_SET(flags, LINK_TMPFILE_REPLACE))
357 r = linkat_replace(fd, /* oldpath= */ NULL, dir_fd, target);
358 else
359 r = link_fd(fd, dir_fd, target);
360 }
361 if (r < 0)
362 return r;
363
364 if (FLAGS_SET(flags, LINK_TMPFILE_SYNC)) {
365 r = fsync_full(fd);
366 if (r < 0)
367 return r;
368 }
369
370 return 0;
371 }
372
373 int flink_tmpfile_at(FILE *f, int dir_fd, const char *path, const char *target, LinkTmpfileFlags flags) {
374 int fd, r;
375
376 assert(f);
377 assert(target);
378
379 fd = fileno(f);
380 if (fd < 0) /* Not all FILE* objects encapsulate fds */
381 return -EBADF;
382
383 r = fflush_and_check(f);
384 if (r < 0)
385 return r;
386
387 return link_tmpfile_at(fd, dir_fd, path, target, flags);
388 }
389
390 int mkdtemp_malloc(const char *template, char **ret) {
391 _cleanup_free_ char *p = NULL;
392 int r;
393
394 assert(ret);
395
396 if (template)
397 p = strdup(template);
398 else {
399 const char *tmp;
400
401 r = tmp_dir(&tmp);
402 if (r < 0)
403 return r;
404
405 p = path_join(tmp, "XXXXXX");
406 }
407 if (!p)
408 return -ENOMEM;
409
410 if (!mkdtemp(p))
411 return -errno;
412
413 *ret = TAKE_PTR(p);
414 return 0;
415 }
416
417 int mkdtemp_open(const char *template, int flags, char **ret) {
418 _cleanup_free_ char *p = NULL;
419 int fd, r;
420
421 r = mkdtemp_malloc(template, &p);
422 if (r < 0)
423 return r;
424
425 fd = RET_NERRNO(open(p, O_DIRECTORY|O_CLOEXEC|flags));
426 if (fd < 0) {
427 (void) rmdir(p);
428 return fd;
429 }
430
431 if (ret)
432 *ret = TAKE_PTR(p);
433
434 return fd;
435 }
436
437 void cleanup_tmpfile_data_done(struct cleanup_tmpfile_data *d) {
438 assert(d);
439
440 if (!d->dir_fd ||
441 (*d->dir_fd < 0 && *d->dir_fd != AT_FDCWD) ||
442 !d->filename ||
443 !*d->filename)
444 return;
445
446 PROTECT_ERRNO;
447
448 (void) unlinkat(*d->dir_fd, *d->filename, /* flags= */ 0);
449 d->dir_fd = NULL;
450 d->filename = NULL;
451 }