]> git.sesse.net Git - vlc/blob - src/config/dirs.c
a241ff57becb929f02709d3190d587d339ab708c
[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 #ifndef UNDER_CE
34 # include <direct.h>
35 #endif
36 # include <shlobj.h>
37 #else
38 # include <unistd.h>
39 # include <pwd.h>
40 #endif
41
42 #include "../libvlc.h"
43 #include "configuration.h"
44 #include <vlc_charset.h>
45 #include <vlc_configuration.h>
46
47 #include <errno.h>                                                  /* errno */
48 #include <assert.h>
49 #include <limits.h>
50
51 #if defined( WIN32 )
52 # define DIR_SHARE ""
53 #else
54 # define DIR_SHARE "share"
55 #endif
56
57
58 /**
59  * config_GetDataDir: find directory where shared data is installed
60  *
61  * @return a string (always succeeds).
62  */
63 const char *config_GetDataDir( void )
64 {
65 #if defined (WIN32) || defined(__APPLE__) || defined (SYS_BEOS)
66     static char path[PATH_MAX] = "";
67
68     if( *path == '\0' )
69     {
70         snprintf( path, sizeof( path ), "%s" DIR_SEP DIR_SHARE, psz_vlcpath );
71         path[sizeof( path ) - 1] = '\0';
72     }
73     return path;
74 #else
75     return DATA_PATH;
76 #endif
77 }
78
79 static const char *GetDir( bool b_appdata, bool b_common_appdata )
80 {
81     /* FIXME: a full memory page here - quite a waste... */
82     static char homedir[PATH_MAX] = "";
83
84 #if defined (WIN32)
85     wchar_t wdir[MAX_PATH];
86
87 # if defined (UNDER_CE)
88     /*There are some errors in cegcc headers*/
89 #undef SHGetSpecialFolderPath
90     BOOL WINAPI SHGetSpecialFolderPath(HWND,LPWSTR,int,BOOL);
91     if( SHGetSpecialFolderPath( NULL, wdir, CSIDL_APPDATA, 1 ) )
92 # else
93     /* Get the "Application Data" folder for the current user */
94     if( S_OK == SHGetFolderPathW( NULL,
95               ( b_appdata ? CSIDL_APPDATA :
96                ( b_common_appdata ? CSIDL_COMMON_APPDATA: CSIDL_PERSONAL ) )
97               | CSIDL_FLAG_CREATE,
98                                   NULL, SHGFP_TYPE_CURRENT, wdir ) )
99 # endif
100     {
101         static char appdir[PATH_MAX] = "";
102         static char comappdir[PATH_MAX] = "";
103         WideCharToMultiByte (CP_UTF8, 0, wdir, -1,
104                              b_appdata ? appdir :
105                              (b_common_appdata ? comappdir :homedir),
106                               PATH_MAX, NULL, NULL);
107         return b_appdata ? appdir : (b_common_appdata ? comappdir :homedir);
108     }
109 #else
110     (void)b_appdata;
111     (void)b_common_appdata;
112 #endif
113
114 #ifdef LIBVLC_USE_PTHREAD
115     static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
116     pthread_mutex_lock (&lock);
117 #endif
118
119     if (!*homedir)
120     {
121         const char *psz_localhome = getenv( "HOME" );
122 #if defined(HAVE_GETPWUID_R)
123         char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
124         if (psz_localhome == NULL)
125         {
126             struct passwd pw, *res;
127
128             if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
129                 psz_localhome = pw.pw_dir;
130         }
131 #endif
132         if (psz_localhome == NULL)
133             psz_localhome = getenv( "TMP" );
134         if (psz_localhome == NULL)
135             psz_localhome = "/tmp";
136
137         const char *uhomedir = FromLocale (psz_localhome);
138         strncpy (homedir, uhomedir, sizeof (homedir) - 1);
139         homedir[sizeof (homedir) - 1] = '\0';
140         LocaleFree (uhomedir);
141     }
142 #ifdef LIBVLC_USE_PTHREAD
143     pthread_mutex_unlock (&lock);
144 #endif
145     return homedir;
146 }
147
148 /**
149  * Determines the system configuration directory.
150  *
151  * @return a string (always succeeds).
152  */
153 const char *config_GetConfDir( void )
154 {
155 #if defined (WIN32)
156     return GetDir( false, true );
157 #elif defined(__APPLE__) || defined (SYS_BEOS)
158     static char path[PATH_MAX] = "";
159
160     if( *path == '\0' )
161     {
162         snprintf( path, sizeof( path ), "%s"DIR_SEP DIR_SHARE, /* FIXME: Duh? */
163                   psz_vlcpath );
164         path[sizeof( path ) - 1] = '\0';
165     }
166     return path;
167 #else
168     return SYSCONFDIR;
169 #endif
170 }
171
172 /**
173  * Get the user's home directory
174  */
175 const char *config_GetHomeDir( void )
176 {
177     return GetDir (false, false);
178 }
179
180 static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
181 {
182     char *psz_dir;
183 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
184     const char *psz_parent = GetDir (true, false);
185
186     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
187         psz_dir = NULL;
188
189     (void)xdg_name; (void)xdg_default;
190 #else
191     char var[sizeof ("XDG__HOME") + strlen (xdg_name)];
192     /* XDG Base Directory Specification - Version 0.6 */
193     snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
194
195     const char *psz_home = getenv (var);
196     psz_home = psz_home ? FromLocale (psz_home) : NULL;
197     if( psz_home )
198     {
199         if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 )
200             psz_dir = NULL;
201         LocaleFree (psz_home);
202         return psz_dir;
203     }
204
205     /* Try HOME, then fallback to non-XDG dirs */
206     psz_home = config_GetHomeDir();
207     if( asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
208         psz_dir = NULL;
209 #endif
210     return psz_dir;
211 }
212
213 /**
214  * Get the user's VLC configuration directory
215  */
216 char *config_GetUserConfDir( void )
217 {
218     return config_GetFooDir ("CONFIG", ".config");
219 }
220
221 /**
222  * Get the user's VLC data directory
223  * (used for stuff like the skins, custom lua modules, ...)
224  */
225 char *config_GetUserDataDir( void )
226 {
227     return config_GetFooDir ("DATA", ".local/share");
228 }
229
230 /**
231  * Get the user's VLC cache directory
232  * (used for stuff like the modules cache, the album art cache, ...)
233  */
234 char *config_GetCacheDir( void )
235 {
236 #if defined(__APPLE__)
237     char *psz_dir;
238     const char *psz_parent = GetDir (true, false);
239
240     if( asprintf( &psz_dir, "%s" DIR_SEP CACHES_DIR, psz_parent ) == -1 )
241         psz_dir = NULL;
242
243     return psz_dir;
244 #else
245     return config_GetFooDir ("CACHE", ".cache");
246 #endif
247 }