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