Coverage Report

Created: 2026-09-28 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mpv/options/path.c
Line
Count
Source
1
/*
2
 * This file is part of mpv.
3
 *
4
 * Get path to config dir/file.
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 <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#include <stdbool.h>
25
#include <sys/types.h>
26
#include <sys/stat.h>
27
#include <errno.h>
28
29
#include "config.h"
30
31
#include "misc/path_utils.h"
32
#include "common/common.h"
33
#include "common/global.h"
34
#include "common/msg.h"
35
#include "options/options.h"
36
#include "options/path.h"
37
#include "mpv_talloc.h"
38
#include "osdep/io.h"
39
#include "osdep/path.h"
40
#include "misc/ctype.h"
41
42
// In order of decreasing priority: the first has highest priority.
43
static const mp_get_platform_path_cb path_resolvers[] = {
44
#if HAVE_COCOA
45
    mp_get_platform_path_mac,
46
#endif
47
#if HAVE_DARWIN
48
    mp_get_platform_path_darwin,
49
#elif !defined(_WIN32) || defined(__CYGWIN__)
50
    mp_get_platform_path_unix,
51
#endif
52
#if HAVE_UWP
53
    mp_get_platform_path_uwp,
54
#elif defined(_WIN32)
55
    mp_get_platform_path_win,
56
#endif
57
};
58
59
// from highest (most preferred) to lowest priority
60
static const char *const config_dirs[] = {
61
    "home",
62
    "old_home",
63
    "osxbundle",
64
    "exe_dir",
65
    "global",
66
};
67
68
// Return a platform specific path using a path type as defined in osdep/path.h.
69
// Keep in mind that the only way to free the return value is freeing talloc_ctx
70
// (or its children), as this function can return a statically allocated string.
71
static const char *mp_get_platform_path(void *talloc_ctx,
72
                                        struct mpv_global *global,
73
                                        const char *type)
74
3.91M
{
75
3.91M
    mp_assert(talloc_ctx);
76
77
3.91M
    if (global->configdir) {
78
        // Return NULL for all platform paths if --no-config is passed
79
3.88M
        if (!global->configdir[0])
80
3.88M
            return NULL;
81
82
        // force all others to NULL, only first returns the path
83
25.2k
        for (int n = 0; n < MP_ARRAY_SIZE(config_dirs); n++) {
84
24.8k
            if (strcmp(config_dirs[n], type) == 0)
85
8.50k
                return (n == 0) ? global->configdir : NULL;
86
24.8k
        }
87
8.89k
    }
88
89
    // Return the native config path if the platform doesn't support the
90
    // type we are trying to fetch.
91
26.8k
    const char *fallback_type = NULL;
92
26.8k
    if (!strcmp(type, "cache") || !strcmp(type, "state"))
93
4.50k
        fallback_type = "home";
94
95
34.7k
    for (int n = 0; n < MP_ARRAY_SIZE(path_resolvers); n++) {
96
26.8k
        const char *path = path_resolvers[n](talloc_ctx, type);
97
26.8k
        if (path && path[0])
98
18.9k
            return path;
99
26.8k
    }
100
101
7.91k
    if (fallback_type) {
102
0
        mp_assert(strcmp(fallback_type, type) != 0);
103
0
        return mp_get_platform_path(talloc_ctx, global, fallback_type);
104
0
    }
105
7.91k
    return NULL;
106
7.91k
}
107
108
void mp_init_paths(struct mpv_global *global, struct MPOpts *opts)
109
126k
{
110
126k
    TA_FREEP(&global->configdir);
111
112
126k
    const char *force_configdir = getenv("MPV_HOME");
113
126k
    if (opts->force_configdir && opts->force_configdir[0])
114
94
        force_configdir = opts->force_configdir;
115
126k
    if (!opts->load_config)
116
126k
        force_configdir = "";
117
118
126k
    global->configdir = mp_get_user_path(global, global, force_configdir);
119
126k
}
120
121
char *mp_find_user_file(void *talloc_ctx, struct mpv_global *global,
122
                        const char *type, const char *filename)
123
692k
{
124
692k
    void *tmp = talloc_new(NULL);
125
692k
    char *res = (char *)mp_get_platform_path(tmp, global, type);
126
692k
    if (res)
127
4.67k
        res = mp_path_join(talloc_ctx, res, filename);
128
692k
    talloc_free(tmp);
129
692k
    MP_DBG(global, "%s path: '%s' -> '%s'\n", type, filename, res ? res : "-");
130
692k
    return res;
131
692k
}
132
133
static char **mp_find_all_config_files_limited(void *talloc_ctx,
134
                                               struct mpv_global *global,
135
                                               int max_files,
136
                                               const char *filename)
137
592k
{
138
592k
    char **ret = talloc_array(talloc_ctx, char*, 2); // 2 preallocated
139
592k
    int num_ret = 0;
140
141
3.55M
    for (int i = 0; i < MP_ARRAY_SIZE(config_dirs); i++) {
142
2.96M
        const char *dir = mp_get_platform_path(ret, global, config_dirs[i]);
143
2.96M
        bstr s = bstr0(filename);
144
2.97M
        while (dir && num_ret < max_files && s.len) {
145
11.4k
            bstr fn;
146
11.4k
            bstr_split_tok(s, "|", &fn, &s);
147
148
11.4k
            char *file = mp_path_join_bstr(ret, bstr0(dir), fn);
149
11.4k
            if (mp_path_exists(file)) {
150
806
                MP_DBG(global, "config path: '%.*s' -> '%s'\n",
151
806
                        BSTR_P(fn), file);
152
806
                MP_TARRAY_APPEND(NULL, ret, num_ret, file);
153
10.6k
            } else {
154
10.6k
                MP_DBG(global, "config path: '%.*s' -/-> '%s'\n",
155
10.6k
                        BSTR_P(fn), file);
156
10.6k
            }
157
11.4k
        }
158
2.96M
    }
159
160
592k
    MP_TARRAY_GROW(NULL, ret, num_ret);
161
592k
    ret[num_ret] = NULL;
162
163
592k
    for (int n = 0; n < num_ret / 2; n++)
164
0
        MPSWAP(char*, ret[n], ret[num_ret - n - 1]);
165
592k
    return ret;
166
592k
}
167
168
char **mp_find_all_config_files(void *talloc_ctx, struct mpv_global *global,
169
                                const char *filename)
170
496k
{
171
496k
    return mp_find_all_config_files_limited(talloc_ctx, global, 64, filename);
172
496k
}
173
174
char *mp_find_config_file(void *talloc_ctx, struct mpv_global *global,
175
                          const char *filename)
176
95.7k
{
177
95.7k
    char **l = mp_find_all_config_files_limited(talloc_ctx, global, 1, filename);
178
95.7k
    char *r = l && l[0] ? talloc_steal(talloc_ctx, l[0]) : NULL;
179
95.7k
    talloc_free(l);
180
95.7k
    return r;
181
95.7k
}
182
183
char *mp_get_user_path(void *talloc_ctx, struct mpv_global *global,
184
                       const char *path)
185
1.47M
{
186
1.47M
    if (!path)
187
620k
        return NULL;
188
853k
    char *res = NULL;
189
853k
    bstr bpath = bstr0(path);
190
853k
    if (bstr_eatstart0(&bpath, "~")) {
191
        // parse to "~" <prefix> "/" <rest>
192
265k
        bstr prefix, rest;
193
265k
        if (bstr_split_tok(bpath, "/", &prefix, &rest)) {
194
263k
            const char *rest0 = rest.start; // ok in this case
195
263k
            if (bstr_equals0(prefix, "~")) {
196
6.92k
                res = mp_find_config_file(talloc_ctx, global, rest0);
197
6.92k
                if (!res) {
198
6.12k
                    void *tmp = talloc_new(NULL);
199
6.12k
                    const char *p = mp_get_platform_path(tmp, global, "home");
200
6.12k
                    res = mp_path_join_bstr(talloc_ctx, bstr0(p), rest);
201
6.12k
                    talloc_free(tmp);
202
6.12k
                }
203
256k
            } else if (bstr_equals0(prefix, "")) {
204
422
                char *home = getenv("HOME");
205
422
                if (!home)
206
0
                    home = getenv("USERPROFILE");
207
422
                res = mp_path_join_bstr(talloc_ctx, bstr0(home), rest);
208
256k
            } else if (bstr_eatstart0(&prefix, "~")) {
209
254k
                void *tmp = talloc_new(NULL);
210
254k
                char type[80];
211
254k
                snprintf(type, sizeof(type), "%.*s", BSTR_P(prefix));
212
254k
                const char *p = mp_get_platform_path(tmp, global, type);
213
254k
                res = mp_path_join_bstr(talloc_ctx, bstr0(p), rest);
214
254k
                talloc_free(tmp);
215
254k
            }
216
263k
        }
217
265k
    }
218
853k
    if (!res) {
219
590k
        res = talloc_strdup(talloc_ctx, path);
220
590k
    } else {
221
262k
        MP_DBG(global, "user path: '%s' -> '%s'\n", path, res);
222
262k
    }
223
853k
    return res;
224
1.47M
}
225
226
char *mp_normalize_user_path(void *talloc_ctx, struct mpv_global *global,
227
                             const char *path)
228
10.0k
{
229
10.0k
    char *expanded = mp_get_user_path(NULL, global, path);
230
10.0k
    char *normalized = mp_normalize_path(talloc_ctx, expanded);
231
10.0k
    talloc_free(expanded);
232
10.0k
    return normalized;
233
10.0k
}
234
235
void mp_mk_user_dir(struct mpv_global *global, const char *type, char *subdir)
236
126k
{
237
126k
    char *dir = mp_find_user_file(NULL, global, type, subdir);
238
126k
    if (dir)
239
416
        mp_mkdirp(dir);
240
126k
    talloc_free(dir);
241
126k
}