Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * I/O utility functions |
3 | | * |
4 | | * This file is part of mpv. |
5 | | * |
6 | | * mpv is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * mpv is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | #include <assert.h> |
21 | | #include <errno.h> |
22 | | #include <stdint.h> |
23 | | #include <stdlib.h> |
24 | | #include <sys/types.h> |
25 | | #include <limits.h> |
26 | | |
27 | | #include "mpv_talloc.h" |
28 | | #include "config.h" |
29 | | #include "common/common.h" |
30 | | #include "misc/random.h" |
31 | | #include "misc/io_utils.h" |
32 | | #include "osdep/io.h" |
33 | | |
34 | | int mp_mkostemps(char *template, int suffixlen, int flags) |
35 | 0 | { |
36 | 0 | size_t len = strlen(template); |
37 | 0 | char *t = len >= 6 + suffixlen ? &template[len - (6 + suffixlen)] : NULL; |
38 | 0 | if (!t || strncmp(t, "XXXXXX", 6) != 0) { |
39 | 0 | errno = EINVAL; |
40 | 0 | return -1; |
41 | 0 | } |
42 | | |
43 | 0 | mp_rand_state s = mp_rand_seed(0); |
44 | 0 | for (size_t fuckshit = 0; fuckshit < UINT32_MAX; fuckshit++) { |
45 | | // Using a random value may make it require fewer iterations (even if |
46 | | // not truly random; just a counter would be sufficient). |
47 | 0 | size_t fuckmess = mp_rand_next(&s); |
48 | 0 | char crap[7] = ""; |
49 | 0 | mp_tprintf_buf(crap, sizeof(crap), "%06zx", fuckmess); |
50 | 0 | memcpy(t, crap, 6); |
51 | |
|
52 | 0 | int res = open(template, O_RDWR | O_CREAT | O_EXCL | flags, 0600); |
53 | 0 | if (res >= 0 || errno != EEXIST) |
54 | 0 | return res; |
55 | 0 | } |
56 | | |
57 | 0 | errno = EEXIST; |
58 | 0 | return -1; |
59 | 0 | } |
60 | | |
61 | | bool mp_save_to_file(const char *filepath, const void *data, size_t size) |
62 | 0 | { |
63 | 0 | mp_assert(filepath && data && size); |
64 | | |
65 | 0 | bool result = false; |
66 | 0 | char *tmp = talloc_asprintf(NULL, "%sXXXXXX", filepath); |
67 | 0 | int fd = mp_mkostemps(tmp, 0, O_CLOEXEC); |
68 | 0 | if (fd < 0) |
69 | 0 | goto done; |
70 | 0 | FILE *cache = fdopen(fd, "wb"); |
71 | 0 | if (!cache) { |
72 | 0 | close(fd); |
73 | 0 | unlink(tmp); |
74 | 0 | goto done; |
75 | 0 | } |
76 | 0 | size_t written = fwrite(data, size, 1, cache); |
77 | 0 | int ret = fclose(cache); |
78 | 0 | if (written > 0 && !ret) { |
79 | 0 | ret = rename(tmp, filepath); |
80 | 0 | result = !ret; |
81 | 0 | } else { |
82 | 0 | unlink(tmp); |
83 | 0 | } |
84 | |
|
85 | 0 | done: |
86 | 0 | talloc_free(tmp); |
87 | 0 | return result; |
88 | 0 | } |