]> git.sesse.net Git - vlc/blob - src/config/dirs.c
Split directory handling and core configuration
[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/vlc.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 /**
50  * config_GetDataDir: find directory where shared data is installed
51  *
52  * @return a string (always succeeds).
53  */
54 const char *config_GetDataDir( void )
55 {
56 #if defined (WIN32) || defined (UNDER_CE)
57     return vlc_global()->psz_vlcpath;
58 #elif defined(__APPLE__) || defined (SYS_BEOS)
59     static char path[PATH_MAX] = "";
60
61     if( *path == '\0' )
62     {
63         snprintf( path, sizeof( path ), "%s/share",
64                   vlc_global()->psz_vlcpath );
65         path[sizeof( path ) - 1] = '\0';
66     }
67     return path;
68 #else
69     return DATA_PATH;
70 #endif
71 }
72
73 /**
74  * Determines the system configuration directory.
75  *
76  * @return a string (always succeeds).
77  */
78 const char *config_GetConfDir( void )
79 {
80 #if defined (WIN32) || defined (UNDER_CE)
81     return vlc_global()->psz_vlcpath;
82 #elif defined(__APPLE__) || defined (SYS_BEOS)
83     static char path[PATH_MAX] = "";
84
85     if( *path == '\0' )
86     {
87         snprintf( path, sizeof( path ), "%s/share", /* FIXME: Duh? */
88                   vlc_global()->psz_vlcpath );
89         path[sizeof( path ) - 1] = '\0';
90     }
91     return path;
92 #else
93     return SYSCONFDIR;
94 #endif
95 }
96
97 static char *GetDir( bool b_appdata )
98 {
99     const char *psz_localhome = NULL;
100
101 #if defined(WIN32) && !defined(UNDER_CE)
102     wchar_t whomedir[MAX_PATH];
103     /* Get the "Application Data" folder for the current user */
104     if( S_OK == SHGetFolderPathW( NULL,
105               (b_appdata ? CSIDL_APPDATA : CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
106                                   NULL, SHGFP_TYPE_CURRENT, whomedir ) )
107         return FromWide( whomedir );
108
109 #elif defined(UNDER_CE)
110     (void)b_appdata;
111 #ifndef CSIDL_APPDATA
112 #   define CSIDL_APPDATA 0x1A
113 #endif
114
115     wchar_t whomedir[MAX_PATH];
116
117     /* get the "Application Data" folder for the current user */
118     if( SHGetSpecialFolderPath( NULL, whomedir, CSIDL_APPDATA, 1 ) )
119         return FromWide( whomedir );
120 #else
121     (void)b_appdata;
122 #endif
123
124     psz_localhome = getenv( "HOME" );
125 #if defined(HAVE_GETPWUID_R)
126     char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
127     if( psz_localhome == NULL )
128     {
129         struct passwd pw, *res;
130
131         if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
132             psz_localhome = pw.pw_dir;
133     }
134 #endif
135     if (psz_localhome == NULL)
136         psz_localhome = getenv( "TMP" );
137     if (psz_localhome == NULL)
138         psz_localhome = "/tmp";
139
140     return FromLocaleDup( psz_localhome );
141 }
142
143 /**
144  * Get the user's home directory
145  */
146 char *config_GetHomeDir( void )
147 {
148     return GetDir( false );
149 }
150
151 static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
152 {
153     char *psz_dir;
154 #if defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS)
155     char *psz_parent = GetDir (true);
156
157     if( asprintf( &psz_dir, "%s" DIR_SEP CONFIG_DIR, psz_parent ) == -1 )
158         psz_dir = NULL;
159
160     free (psz_parent);
161     (void)xdg_name; (void)xdg_default;
162 #else
163     char var[sizeof ("XDG__HOME") + strlen (xdg_name)];
164
165     /* XDG Base Directory Specification - Version 0.6 */
166     snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
167     char *psz_home = getenv( var );
168     psz_home = psz_home ? FromLocaleDup( psz_home ) : NULL;
169     if( psz_home )
170     {
171         if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 )
172             psz_dir = NULL;
173         goto out;
174     }
175
176     /* Try HOME, then fallback to non-XDG dirs */
177     psz_home = config_GetHomeDir();
178     if( asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
179         psz_dir = NULL;
180
181 out:
182     free (psz_home);
183 #endif
184     return psz_dir;
185 }
186
187 /**
188  * Get the user's VLC configuration directory
189  */
190 char *config_GetUserConfDir( void )
191 {
192     return config_GetFooDir ("CONFIG", ".config");
193 }
194
195 /**
196  * Get the user's VLC data directory
197  * (used for stuff like the skins, custom lua modules, ...)
198  */
199 char *config_GetUserDataDir( void )
200 {
201     return config_GetFooDir ("DATA", ".local/share");
202 }
203
204 /**
205  * Get the user's VLC cache directory
206  * (used for stuff like the modules cache, the album art cache, ...)
207  */
208 char *config_GetCacheDir( void )
209 {
210     return config_GetFooDir ("CACHE", ".cache");
211 }