Line | Count | Source |
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 | | static int mp_mkostemps_mode(char *template, int suffixlen, int flags, mode_t mode) |
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, mode); |
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 | | int mp_mkostemps(char *template, int suffixlen, int flags) |
62 | 0 | { |
63 | 0 | return mp_mkostemps_mode(template, suffixlen, flags, 0600); |
64 | 0 | } |
65 | | |
66 | | bool mp_save_to_file(const char *filepath, const void *data, size_t size) |
67 | 0 | { |
68 | 0 | mp_assert(filepath); |
69 | | |
70 | 0 | bool result = false; |
71 | 0 | char *tmp = talloc_asprintf(NULL, "%sXXXXXX", filepath); |
72 | 0 | int fd = mp_mkostemps_mode(tmp, 0, O_CLOEXEC, 0666); |
73 | 0 | if (fd < 0) |
74 | 0 | goto done; |
75 | 0 | FILE *fs = fdopen(fd, "wb"); |
76 | 0 | if (!fs) { |
77 | 0 | close(fd); |
78 | 0 | unlink(tmp); |
79 | 0 | goto done; |
80 | 0 | } |
81 | 0 | size_t written = (data && size) ? fwrite(data, size, 1, fs) : 1; |
82 | 0 | int ret = fclose(fs); |
83 | 0 | if (written > 0 && !ret) { |
84 | 0 | ret = rename(tmp, filepath); |
85 | 0 | result = !ret; |
86 | 0 | } else { |
87 | 0 | unlink(tmp); |
88 | 0 | } |
89 | |
|
90 | 0 | done: |
91 | 0 | talloc_free(tmp); |
92 | 0 | return result; |
93 | 0 | } |