1 /*****************************************************************************
2 * file.c: configuration file handling
3 *****************************************************************************
4 * Copyright (C) 2001-2007 VLC authors and VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <errno.h> /* errno */
35 #elif defined(HAVE_USELOCALE)
42 #include <vlc_common.h>
43 #include "../libvlc.h"
44 #include <vlc_charset.h>
47 #include <vlc_modules.h>
48 #include <vlc_plugin.h>
50 #include "configuration.h"
51 #include "modules/modules.h"
53 static inline char *strdupnull (const char *src)
55 return src ? strdup (src) : NULL;
59 * Get the user's configuration file
61 static char *config_GetConfigFile( vlc_object_t *obj )
63 char *psz_file = var_CreateGetNonEmptyString( obj, "config" );
64 var_Destroy( obj, "config" );
65 if( psz_file == NULL )
67 char *psz_dir = config_GetUserDir( VLC_CONFIG_DIR );
69 if( asprintf( &psz_file, "%s" DIR_SEP CONFIG_FILE, psz_dir ) == -1 )
76 static FILE *config_OpenConfigFile( vlc_object_t *p_obj )
78 char *psz_filename = config_GetConfigFile( p_obj );
79 if( psz_filename == NULL )
82 msg_Dbg( p_obj, "opening config file (%s)", psz_filename );
84 FILE *p_stream = vlc_fopen( psz_filename, "rt" );
85 if( p_stream == NULL && errno != ENOENT )
87 msg_Err( p_obj, "cannot open config file (%s): %m",
91 #if !( defined(WIN32) || defined(__APPLE__) || defined(__OS2__) )
92 else if( p_stream == NULL && errno == ENOENT )
94 /* This is the fallback for pre XDG Base Directory
95 * Specification configs */
96 char *home = config_GetUserDir(VLC_HOME_DIR);
100 && asprintf( &psz_old, "%s/.vlc/" CONFIG_FILE,
103 p_stream = vlc_fopen( psz_old, "rt" );
106 /* Old config file found. We want to write it at the
107 * new location now. */
108 msg_Info( p_obj->p_libvlc, "Found old config file at %s. "
109 "VLC will now use %s.", psz_old, psz_filename );
111 if( asprintf(&psz_readme,"%s/.vlc/README",
114 FILE *p_readme = vlc_fopen( psz_readme, "wt" );
117 fprintf( p_readme, "The VLC media player "
118 "configuration folder has moved to comply\n"
119 "with the XDG Base Directory Specification "
120 "version 0.6. Your\nconfiguration has been "
121 "copied to the new location:\n%s\nYou can "
122 "delete this directory and all its contents.",
128 /* Remove the old configuration file so that --reset-config
129 * can work properly. Fortunately, Linux allows removing
130 * open files - with most filesystems. */
138 free( psz_filename );
143 static int64_t strtoi (const char *str)
149 l = strtoll (str, &end, 0);
153 #if (LLONG_MAX > 0x7fffffffffffffffLL)
154 if (l > 0x7fffffffffffffffLL
155 || l < -0x8000000000000000LL)
164 #undef config_LoadConfigFile
165 /*****************************************************************************
166 * config_LoadConfigFile: loads the configuration file.
167 *****************************************************************************
168 * This function is called to load the config options stored in the config
170 *****************************************************************************/
171 int config_LoadConfigFile( vlc_object_t *p_this )
175 file = config_OpenConfigFile (p_this);
179 /* Look for UTF-8 Byte Order Mark */
180 char * (*convert) (const char *) = strdupnull;
183 if ((fread (bom, 1, 3, file) != 3)
184 || memcmp (bom, "\xEF\xBB\xBF", 3))
186 convert = FromLocaleDup;
187 rewind (file); /* no BOM, rewind */
194 /* Ensure consistent number formatting... */
195 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
196 locale_t baseloc = uselocale (loc);
198 vlc_rwlock_wrlock (&config_lock);
199 while ((linelen = getline (&line, &bufsize, file)) != -1)
201 line[linelen - 1] = '\0'; /* trim newline */
203 /* Ignore comments, section and empty lines */
204 if (memchr ("#[", line[0], 3) != NULL)
207 /* look for option name */
208 const char *psz_option_name = line;
210 char *ptr = strchr (line, '=');
212 continue; /* syntax error */
215 module_config_t *item = config_FindConfig (p_this, psz_option_name);
219 const char *psz_option_value = ptr + 1;
220 switch (CONFIG_CLASS(item->i_type))
222 case CONFIG_ITEM_BOOL:
223 case CONFIG_ITEM_INTEGER:
228 l = strtoi (psz_option_value);
229 if ((l > item->max.i) || (l < item->min.i))
232 msg_Warn (p_this, "Integer value (%s) for %s: %m",
233 psz_option_value, psz_option_name);
239 case CONFIG_ITEM_FLOAT:
240 if (!*psz_option_value)
241 break; /* ignore empty option */
242 item->value.f = (float)atof (psz_option_value);
246 free ((char *)item->value.psz);
247 item->value.psz = convert (psz_option_value);
251 vlc_rwlock_unlock (&config_lock);
256 msg_Err (p_this, "error reading configuration: %m");
261 if (loc != (locale_t)0)
269 /*****************************************************************************
270 * config_CreateDir: Create configuration directory if it doesn't exist.
271 *****************************************************************************/
272 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
274 if( !psz_dirname || !*psz_dirname ) return -1;
276 if( vlc_mkdir( psz_dirname, 0700 ) == 0 )
286 /* Let's try to create the parent directory */
287 char psz_parent[strlen( psz_dirname ) + 1], *psz_end;
288 strcpy( psz_parent, psz_dirname );
290 psz_end = strrchr( psz_parent, DIR_SEP_CHAR );
291 if( psz_end && psz_end != psz_parent )
294 if( config_CreateDir( p_this, psz_parent ) == 0 )
296 if( !vlc_mkdir( psz_dirname, 0700 ) )
303 msg_Warn( p_this, "could not create %s: %m", psz_dirname );
308 config_Write (FILE *file, const char *desc, const char *type,
309 bool comment, const char *name, const char *fmt, ...)
317 if (fprintf (file, "# %s (%s)\n%s%s=", desc, vlc_gettext (type),
318 comment ? "#" : "", name) < 0)
322 ret = vfprintf (file, fmt, ap);
327 if (fputs ("\n\n", file) == EOF)
333 static int config_PrepareDir (vlc_object_t *obj)
335 char *psz_configdir = config_GetUserDir (VLC_CONFIG_DIR);
336 if (psz_configdir == NULL)
339 int ret = config_CreateDir (obj, psz_configdir);
340 free (psz_configdir);
344 #undef config_SaveConfigFile
346 * Saves the in-memory configuration into a file.
347 * @return 0 on success, -1 on error.
349 int config_SaveConfigFile (vlc_object_t *p_this)
352 if( config_PrepareDir( p_this ) )
354 msg_Err( p_this, "no configuration directory" );
359 * Save module config in file
362 char *permanent = config_GetConfigFile (p_this);
363 if (permanent == NULL)
365 if (asprintf (&temporary, "%s.%u", permanent, getpid ()) == -1)
374 /* Some users make vlcrc read-only to prevent changes.
375 * The atomic replacement scheme breaks this "feature",
376 * so we check for read-only by hand. */
377 if (stat (permanent, &st) == 0 && !(st.st_mode & S_IWUSR))
379 msg_Err (p_this, "configuration file is read-only");
384 /* Configuration lock must be taken before vlcrc serializer below. */
385 vlc_rwlock_rdlock (&config_lock);
387 /* The temporary configuration file is per-PID. Therefore this function
388 * should be serialized against itself within a given process. */
389 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
390 vlc_mutex_lock (&lock);
392 int fd = vlc_open (temporary, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
395 vlc_rwlock_unlock (&config_lock);
396 vlc_mutex_unlock (&lock);
399 FILE *file = fdopen (fd, "wt");
402 msg_Err (p_this, "cannot create configuration file: %m");
403 vlc_rwlock_unlock (&config_lock);
405 vlc_mutex_unlock (&lock);
411 "### "PACKAGE_NAME" "PACKAGE_VERSION"\n"
415 "### lines beginning with a '#' character are comments\n"
419 /* Ensure consistent number formatting... */
420 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
421 locale_t baseloc = uselocale (loc);
423 /* We would take the config lock here. But this would cause a lock
424 * inversion with the serializer above and config_AutoSaveConfigFile().
425 vlc_rwlock_rdlock (&config_lock);*/
427 /* Look for the selected module, if NULL then save everything */
428 module_t **list = module_list_get (NULL);
430 for (int i = 0; (p_parser = list[i]) != NULL; i++)
432 module_config_t *p_item, *p_end;
434 if( !p_parser->i_config_items )
437 fprintf( file, "[%s]", module_get_object (p_parser) );
438 if( p_parser->psz_longname )
439 fprintf( file, " # %s\n\n", p_parser->psz_longname );
441 fprintf( file, "\n\n" );
443 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
447 if (!CONFIG_ITEM(p_item->i_type) /* ignore hint */
448 || p_item->b_removed /* ignore deprecated option */
449 || p_item->b_unsaveable) /* ignore volatile option */
452 if (IsConfigIntegerType (p_item->i_type))
454 int64_t val = p_item->value.i;
455 config_Write (file, p_item->psz_text,
456 (CONFIG_CLASS(p_item->i_type) == CONFIG_ITEM_BOOL)
457 ? N_("boolean") : N_("integer"),
458 val == p_item->orig.i,
459 p_item->psz_name, "%"PRId64, val);
462 if (IsConfigFloatType (p_item->i_type))
464 float val = p_item->value.f;
465 config_Write (file, p_item->psz_text, N_("float"),
466 val == p_item->orig.f,
467 p_item->psz_name, "%f", val);
471 const char *psz_value = p_item->value.psz;
474 assert (IsConfigStringType (p_item->i_type));
476 modified = !!strcmp (psz_value ? psz_value : "",
477 p_item->orig.psz ? p_item->orig.psz : "");
478 config_Write (file, p_item->psz_text, N_("string"),
479 !modified, p_item->psz_name, "%s",
480 psz_value ? psz_value : "");
484 vlc_rwlock_unlock (&config_lock);
486 module_list_free (list);
487 if (loc != (locale_t)0)
494 * Flush to disk and replace atomically
496 fflush (file); /* Flush from run-time */
499 vlc_unlink (temporary);
500 vlc_mutex_unlock (&lock);
501 msg_Err (p_this, "cannot write configuration file");
505 #if defined(__APPLE__) || defined(__ANDROID__)
506 fsync (fd); /* Flush from OS */
508 fdatasync (fd); /* Flush from OS */
510 #if defined (WIN32) || defined (__OS2__)
511 /* Windows cannot (re)move open files nor overwrite existing ones */
513 vlc_unlink (permanent);
515 /* Atomically replace the file... */
516 if (vlc_rename (temporary, permanent))
517 vlc_unlink (temporary);
518 /* (...then synchronize the directory, err, TODO...) */
519 /* ...and finally close the file */
520 vlc_mutex_unlock (&lock);
521 #if !defined (WIN32) && !defined (__OS2__)
535 int config_AutoSaveConfigFile( vlc_object_t *p_this )
541 vlc_rwlock_rdlock (&config_lock);
544 /* Note: this will get the read lock recursively. Ok. */
545 ret = config_SaveConfigFile (p_this);
546 config_dirty = (ret != 0);
548 vlc_rwlock_unlock (&config_lock);