]> git.sesse.net Git - vlc/blob - src/config/dirs.c
2661eb41fc975f7b92a3f478dbfa15be22362744
[vlc] / src / config / dirs.c
1 /*****************************************************************************
2  * dirs.c: directories configuration
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * Copyright © 2007-2008 Rémi Denis-Courmont
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29
30 #if defined( WIN32 )
31 # define _WIN32_IE IE5
32 # include <w32api.h>
33 # include <direct.h>
34 # include <shlobj.h>
35 #else
36 # include <unistd.h>
37 # include <pwd.h>
38 #endif
39
40 #include "../libvlc.h"
41 #include "configuration.h"
42 #include <vlc_charset.h>
43 #include <vlc_configuration.h>
44
45 #include <errno.h>                                                  /* errno */
46 #include <assert.h>
47 #include <limits.h>
48
49 #if defined( WIN32 )
50 # define DIR_SHARE ""
51 #else
52 # define DIR_SHARE "share"
53 #endif
54
55 /**
56  * config_GetDataDir: find directory where shared data is installed
57  *
58  * @return a string (always succeeds).
59  */
60 const char *config_GetDataDir( void )
61 {
62 #if defined (WIN32) || defined(__APPLE__) || defined (SYS_BEOS)
63     static char path[PATH_MAX] = "";
64
65     if( *path == '\0' )
66     {
67         snprintf( path, sizeof( path ), "%s" DIR_SEP DIR_SHARE,
68                   vlc_global()->psz_vlcpath );
69         path[sizeof( path ) - 1] = '\0';
70     }
71     return path;
72 #else
73     return DATA_PATH;
74 #endif
75 }
76
77 /**
78  * Determines the system configuration directory.
79  *
80  * @return a string (always succeeds).
81  */
82 const char *config_GetConfDir( void )
83 {
84 #if defined (WIN32) || defined(__APPLE__) || defined (SYS_BEOS)
85     static char path[PATH_MAX] = "";
86
87     if( *path == '\0' )
88     {
89         snprintf( path, sizeof( path ), "%s"DIR_SEP DIR_SHARE, /* FIXME: Duh? */
90                   vlc_global()->psz_vlcpath );
91         path[sizeof( path ) - 1] = '\0';
92     }
93     return path;
94 #else
95     return SYSCONFDIR;
96 #endif
97 }
98
99 static const char *GetDir( bool b_appdata )
100 {
101     /* FIXME: a full memory page here - quite a waste... */
102     static char homedir[PATH_MAX] = "";
103
104 #if defined (WIN32)
105     wchar_t wdir[MAX_PATH];
106
107 # if defined (UNDER_CE)
108     if( SHGetSpecialFolderPath( NULL, wdir, CSIDL_APPDATA, 1 ) )
109 # else
110     /* Get the "Application Data" folder for the current user */
111     if( S_OK == SHGetFolderPathW( NULL,
112               (b_appdata ? CSIDL_APPDATA : CSIDL_PERSONAL) | CSIDL_FLAG_CREATE,
113                                   NULL, SHGFP_TYPE_CURRENT, wdir ) )
114 # endif
115     {
116         static char appdir[PATH_MAX] = "";
117         WideCharToMultiByte (CP_UTF8, 0, wdir, -1,
118                              b_appdata ? appdir : homedir, PATH_MAX,
119                              NULL, NULL);
120         return b_appdata ? appdir : homedir;
121     }
122 #else
123     (void)b_appdata;
124 #endif
125
126 #ifdef LIBVLC_USE_PTHREAD
127     static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
128     pthread_mutex_lock (&lock);
129 #endif
130
131     if (!*homedir)
132     {
133         const char *psz_localhome = getenv( "HOME" );
134 #if defined(HAVE_GETPWUID_R)
135         char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
136         if (psz_localhome == NULL)
137         {
138             struct passwd pw, *res;
139
140             if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
141                 psz_localhome = pw.pw_dir;
142         }
143 #endif
144         if (psz_localhome == NULL)
145             psz_localhome = getenv( "TMP" );
146         if (psz_localhome == NULL)
147             psz_localhome = "/tmp";
148
149         const char *uhomedir = FromLocale (psz_localhome);
150         strncpy (homedir, uhomedir, sizeof (homedir) - 1);
151         homedir[sizeof (homedir) - 1] = '\0';
152         LocaleFree (uhomedir);
153     }
154 #ifdef LIBVLC_USE_PTHREAD
155     pthread_mutex_unlock (&lock);
156 #endif
157     return homedir;
158 }
159
160 /**
161  * Get the user's home directory
162  */
163 const char *config_GetHomeDir( void )
164 {
165     return GetDir (false);
166 }
167
168 static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
169 {
170     char *psz_dir;
171 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
172     const char *psz_parent = GetDir (true);
173
174     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
175         psz_dir = NULL;
176
177     (void)xdg_name; (void)xdg_default;
178 #else
179     char var[sizeof ("XDG__HOME") + strlen (xdg_name)];
180     /* XDG Base Directory Specification - Version 0.6 */
181     snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
182
183     const char *psz_home = getenv (var);
184     psz_home = psz_home ? FromLocale (psz_home) : NULL;
185     if( psz_home )
186     {
187         if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 )
188             psz_dir = NULL;
189         LocaleFree (psz_home);
190         return psz_dir;
191     }
192
193     /* Try HOME, then fallback to non-XDG dirs */
194     psz_home = config_GetHomeDir();
195     if( asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
196         psz_dir = NULL;
197 #endif
198     return psz_dir;
199 }
200
201 /**
202  * Get the user's VLC configuration directory
203  */
204 char *config_GetUserConfDir( void )
205 {
206     return config_GetFooDir ("CONFIG", ".config");
207 }
208
209 /**
210  * Get the user's VLC data directory
211  * (used for stuff like the skins, custom lua modules, ...)
212  */
213 char *config_GetUserDataDir( void )
214 {
215     return config_GetFooDir ("DATA", ".local/share");
216 }
217
218 /**
219  * Get the user's VLC cache directory
220  * (used for stuff like the modules cache, the album art cache, ...)
221  */
222 char *config_GetCacheDir( void )
223 {
224     return config_GetFooDir ("CACHE", ".cache");
225 }