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