Coverage Report

Created: 2026-11-30 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mpv/osdep/path-unix.c
Line
Count
Source
1
/*
2
 * This file is part of mpv.
3
 *
4
 * mpv is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * mpv is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
#include <string.h>
19
20
#include "misc/bstr.h"
21
#include "options/path.h"
22
#include "osdep/io.h"
23
#include "osdep/threads.h"
24
#include "path.h"
25
26
#include "config.h"
27
28
static mp_once path_init_once = MP_STATIC_ONCE_INITIALIZER;
29
30
0
#define CONF_MAX 512
31
static char mpv_home[CONF_MAX];
32
static char old_home[CONF_MAX];
33
static char mpv_cache[CONF_MAX];
34
static char old_cache[CONF_MAX];
35
static char mpv_desktop[CONF_MAX];
36
static char mpv_state[CONF_MAX];
37
0
#define MKPATH(BUF, ...) (snprintf((BUF), CONF_MAX, __VA_ARGS__) >= CONF_MAX)
38
39
static void path_init(void)
40
0
{
41
0
    char *home = getenv("HOME");
42
0
    char *xdg_cache = getenv("XDG_CACHE_HOME");
43
0
    char *xdg_config = getenv("XDG_CONFIG_HOME");
44
0
    char *xdg_state = getenv("XDG_STATE_HOME");
45
46
0
    bool err = false;
47
0
    if (xdg_config && xdg_config[0] == '/') {
48
0
        err = err || MKPATH(mpv_home, "%s/mpv", xdg_config);
49
0
    } else if (home && home[0]) {
50
0
        err = err || MKPATH(mpv_home, "%s/.config/mpv", home);
51
0
    }
52
53
    // Maintain compatibility with old ~/.mpv
54
0
    if (home && home[0]) {
55
0
        err = err || MKPATH(old_home, "%s/.mpv", home);
56
0
        err = err || MKPATH(old_cache, "%s/.mpv/cache", home);
57
0
    }
58
59
0
    if (xdg_cache && xdg_cache[0] == '/') {
60
0
        err = err || MKPATH(mpv_cache, "%s/mpv", xdg_cache);
61
0
    } else if (home && home[0]) {
62
0
        err = err || MKPATH(mpv_cache, "%s/.cache/mpv", home);
63
0
    }
64
65
0
    if (xdg_state && xdg_state[0] == '/') {
66
0
        err = err || MKPATH(mpv_state, "%s/mpv", xdg_state);
67
0
    } else if (home && home[0]) {
68
0
        err = err || MKPATH(mpv_state, "%s/.local/state/mpv", home);
69
0
    }
70
71
0
    char xdg_user_dirs[CONF_MAX] = {0};
72
0
    if (xdg_config && xdg_config[0] == '/') {
73
0
        err = err || MKPATH(xdg_user_dirs, "%s/user-dirs.dirs", xdg_config);
74
0
    } else if (home && home[0]) {
75
0
        err = err || MKPATH(xdg_user_dirs, "%s/.config/user-dirs.dirs", home);
76
0
    }
77
78
    // Attempt to read user-dirs for XDG_DESKTOP_DIR
79
0
    mpv_desktop[0] = '\0';
80
0
    if (mp_path_exists(xdg_user_dirs)) {
81
0
        char line[4096];
82
0
        FILE *user_dirs = fopen(xdg_user_dirs, "r");
83
0
        if (!user_dirs)
84
0
            goto skip_user_dirs;
85
0
        while (fgets(line, sizeof(line), user_dirs)) {
86
0
            bstr data = bstr0(line);
87
0
            if (bstr_eatstart0(&data, "XDG_DESKTOP_DIR=")) {
88
0
                bstr value = bstr_strip_linebreaks(data);
89
0
                if (bstr_eatstart0(&value, "\"") && bstr_eatend0(&value, "\"")) {
90
0
                    bool home_prefix = bstr_eatstart0(&value, "$HOME/");
91
0
                    if (home_prefix) {
92
0
                        err = err || MKPATH(mpv_desktop, "%s/%.*s", home, BSTR_P(value));
93
0
                    } else {
94
0
                        err = err || MKPATH(mpv_desktop, "%.*s", BSTR_P(value));
95
0
                    }
96
0
                }
97
0
                break;
98
0
            }
99
0
        }
100
0
        fclose(user_dirs);
101
0
    }
102
103
0
skip_user_dirs:
104
0
    if (!mpv_desktop[0] && home && home[0])
105
0
        err = err || MKPATH(mpv_desktop, "%s/%s", home, "Desktop");
106
107
    // If the old ~/.mpv exists, and the XDG config dir doesn't, use the old
108
    // config dir only. Also do not use any other XDG directories.
109
0
    if (mp_path_exists(old_home) && !mp_path_exists(mpv_home)) {
110
0
        err = err || MKPATH(mpv_home, "%s", old_home);
111
0
        err = err || MKPATH(mpv_cache, "%s", old_cache);
112
0
        err = err || MKPATH(mpv_state, "%s", old_home);
113
0
        old_home[0] = '\0';
114
0
        old_cache[0] = '\0';
115
0
    }
116
117
0
    if (err) {
118
0
        fprintf(stderr, "Config dir exceeds %d bytes\n", CONF_MAX);
119
0
        abort();
120
0
    }
121
0
}
122
123
const char *mp_get_platform_path_unix(void *talloc_ctx, const char *type)
124
0
{
125
0
    mp_exec_once(&path_init_once, path_init);
126
0
    if (strcmp(type, "home") == 0)
127
0
        return mpv_home;
128
0
    if (strcmp(type, "old_home") == 0)
129
0
        return old_home;
130
0
    if (strcmp(type, "cache") == 0)
131
0
        return mpv_cache;
132
0
    if (strcmp(type, "state") == 0)
133
0
        return mpv_state;
134
0
    if (strcmp(type, "global") == 0)
135
0
        return MPV_CONFDIR;
136
0
    if (strcmp(type, "desktop") == 0)
137
0
        return mpv_desktop;
138
0
    return NULL;
139
0
}