1 /*****************************************************************************
2 * file.c: configuration file handling
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
28 #include <vlc_common.h>
29 #include "../libvlc.h"
30 #include "vlc_charset.h"
33 #include <errno.h> /* errno */
44 #include "configuration.h"
45 #include "modules/modules.h"
47 static char *ConfigKeyToString( int );
49 static inline char *strdupnull (const char *src)
51 return src ? strdup (src) : NULL;
55 * Get the user's configuration file
57 static char *config_GetConfigFile( vlc_object_t *obj )
59 char *psz_file = config_GetPsz( obj, "config" );
60 if( psz_file == NULL )
62 char *psz_dir = config_GetUserConfDir();
64 if( asprintf( &psz_file, "%s" DIR_SEP CONFIG_FILE, psz_dir ) == -1 )
71 static FILE *config_OpenConfigFile( vlc_object_t *p_obj )
73 char *psz_filename = config_GetConfigFile( p_obj );
74 if( psz_filename == NULL )
77 msg_Dbg( p_obj, "opening config file (%s)", psz_filename );
79 FILE *p_stream = utf8_fopen( psz_filename, "rt" );
80 if( p_stream == NULL && errno != ENOENT )
82 msg_Err( p_obj, "cannot open config file (%s): %m",
86 #if !( defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS) )
87 else if( p_stream == NULL && errno == ENOENT )
89 /* This is the fallback for pre XDG Base Directory
90 * Specification configs */
92 if( asprintf( &psz_old, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE,
93 config_GetHomeDir() ) != -1 )
95 p_stream = utf8_fopen( psz_old, "rt" );
98 /* Old config file found. We want to write it at the
99 * new location now. */
100 msg_Info( p_obj->p_libvlc, "Found old config file at %s. "
101 "VLC will now use %s.", psz_old, psz_filename );
103 if( asprintf(&psz_readme,"%s"DIR_SEP CONFIG_DIR DIR_SEP"README",
104 config_GetHomeDir() ) != -1 )
106 FILE *p_readme = utf8_fopen( psz_readme, "wt" );
109 fprintf( p_readme, "The VLC media player "
110 "configuration folder has moved to comply\n"
111 "with the XDG Base Directory Specification "
112 "version 0.6. Your\nconfiguration has been "
113 "copied to the new location:\n%s\nYou can "
114 "delete this directory and all its contents.",
125 free( psz_filename );
130 static int strtoi (const char *str)
136 l = strtol (str, &end, 0);
140 if ((l > INT_MAX) || (l < INT_MIN))
149 /*****************************************************************************
150 * config_LoadConfigFile: loads the configuration file.
151 *****************************************************************************
152 * This function is called to load the config options stored in the config
154 *****************************************************************************/
155 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
159 file = config_OpenConfigFile (p_this);
163 /* Look for the selected module, if NULL then save everything */
164 module_t **list = module_list_get (NULL);
166 /* Look for UTF-8 Byte Order Mark */
167 char * (*convert) (const char *) = strdupnull;
170 if ((fread (bom, 1, 3, file) != 3)
171 || memcmp (bom, "\xEF\xBB\xBF", 3))
173 convert = FromLocaleDup;
174 rewind (file); /* no BOM, rewind */
177 module_t *module = NULL;
178 char line[1024], section[1022];
181 /* Ensure consistent number formatting... */
182 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
183 locale_t baseloc = uselocale (loc);
185 while (fgets (line, 1024, file) != NULL)
187 /* Ignore comments and empty lines */
198 char *ptr = strchr (line, ']');
200 continue; /* syntax error; */
203 /* New section ( = a given module) */
204 strcpy (section, line + 1);
207 if ((psz_module_name == NULL)
208 || (strcmp (psz_module_name, section) == 0))
210 for (int i = 0; list[i]; i++)
212 module_t *m = list[i];
214 if ((strcmp (section, m->psz_object_name) == 0)
215 && (m->i_config_items > 0)) /* ignore config-less modules */
218 if (psz_module_name != NULL)
220 "loading config for module \"%s\"",
231 continue; /* no need to parse if there is no matching module */
233 char *ptr = strchr (line, '\n');
237 /* look for option name */
238 const char *psz_option_name = line;
240 ptr = strchr (line, '=');
242 continue; /* syntax error */
245 const char *psz_option_value = ptr + 1;
247 /* try to match this option with one of the module's options */
248 for (size_t i = 0; i < module->confsize; i++)
250 module_config_t *p_item = module->p_config + i;
252 if ((p_item->i_type & CONFIG_HINT)
253 || strcmp (p_item->psz_name, psz_option_name))
259 vlc_mutex_lock( p_item->p_lock );
260 switch( p_item->i_type )
262 case CONFIG_ITEM_BOOL:
263 case CONFIG_ITEM_INTEGER:
265 long l = strtoi (psz_option_value);
267 msg_Warn (p_this, "Integer value (%s) for %s: %m",
268 psz_option_value, psz_option_name);
270 p_item->saved.i = p_item->value.i = (int)l;
274 case CONFIG_ITEM_FLOAT:
275 if( !*psz_option_value )
276 break; /* ignore empty option */
277 p_item->value.f = (float)atof (psz_option_value);
278 p_item->saved.f = p_item->value.f;
281 case CONFIG_ITEM_KEY:
282 if( !*psz_option_value )
283 break; /* ignore empty option */
284 p_item->value.i = ConfigStringToKey(psz_option_value);
285 p_item->saved.i = p_item->value.i;
289 /* free old string */
290 free( (char*) p_item->value.psz );
291 free( (char*) p_item->saved.psz );
293 p_item->value.psz = convert (psz_option_value);
294 p_item->saved.psz = strdupnull (p_item->value.psz);
297 vlc_mutex_unlock( p_item->p_lock );
304 msg_Err (p_this, "error reading configuration: %m");
309 module_list_free (list);
310 if (loc != (locale_t)0)
318 /*****************************************************************************
319 * config_CreateDir: Create configuration directory if it doesn't exist.
320 *****************************************************************************/
321 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
323 if( !psz_dirname || !*psz_dirname ) return -1;
325 if( utf8_mkdir( psz_dirname, 0700 ) == 0 )
335 /* Let's try to create the parent directory */
336 char psz_parent[strlen( psz_dirname ) + 1], *psz_end;
337 strcpy( psz_parent, psz_dirname );
339 psz_end = strrchr( psz_parent, DIR_SEP_CHAR );
340 if( psz_end && psz_end != psz_parent )
343 if( config_CreateDir( p_this, psz_parent ) == 0 )
345 if( !utf8_mkdir( psz_dirname, 0700 ) )
352 msg_Err( p_this, "could not create %s: %m", psz_dirname );
357 config_Write (FILE *file, const char *type, const char *desc,
358 bool comment, const char *name, const char *fmt, ...)
366 if (fprintf (file, "# %s (%s)\n%s%s=", desc, vlc_gettext (type),
367 comment ? "#" : "", name) < 0)
371 ret = vfprintf (file, fmt, ap);
376 if (fputs ("\n\n", file) == EOF)
382 static int config_PrepareDir (vlc_object_t *obj)
384 char *psz_configdir = config_GetUserConfDir ();
385 if (psz_configdir == NULL) /* XXX: This should never happen */
388 int ret = config_CreateDir (obj, psz_configdir);
389 free (psz_configdir);
393 /*****************************************************************************
394 * config_SaveConfigFile: Save a module's config options.
395 *****************************************************************************
396 * This will save the specified module's config options to the config file.
397 * If psz_module_name is NULL then we save all the modules config options.
398 * It's no use to save the config options that kept their default values, so
399 * we'll try to be a bit clever here.
401 * When we save we mustn't delete the config options of the modules that
402 * haven't been loaded. So we cannot just create a new config file with the
403 * config structures we've got in memory.
404 * I don't really know how to deal with this nicely, so I will use a completly
406 * I will load the config file in memory, but skipping all the sections of the
407 * modules we want to save. Then I will create a brand new file, dump the file
408 * loaded in memory and then append the sections of the modules we want to
411 *****************************************************************************/
412 static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
417 char *permanent = NULL, *temporary = NULL;
418 char p_line[1024], *p_index2;
419 unsigned long i_sizebuf = 0;
420 char *p_bigbuffer = NULL, *p_index;
424 if( config_PrepareDir( p_this ) )
426 msg_Err( p_this, "no configuration directory" );
430 file = config_OpenConfigFile( p_this );
433 /* look for file size */
434 fseek( file, 0L, SEEK_END );
435 i_sizebuf = ftell( file );
436 fseek( file, 0L, SEEK_SET );
437 if( i_sizebuf >= LONG_MAX )
441 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
446 /* List all available modules */
447 module_t **list = module_list_get (NULL);
449 /* backup file into memory, we only need to backup the sections we won't
452 while( file && fgets( p_line, 1024, file ) )
454 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
457 /* we found a section, check if we need to do a backup */
458 for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ )
460 if( ((p_index2 - &p_line[1])
461 == (int)strlen(p_parser->psz_object_name) )
462 && !memcmp( &p_line[1], p_parser->psz_object_name,
463 strlen(p_parser->psz_object_name) ) )
465 if( !psz_module_name )
467 else if( !strcmp( psz_module_name,
468 p_parser->psz_object_name ) )
473 if( list[i_index] == NULL )
475 /* we don't have this section in our list so we need to back
479 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
492 /* save line if requested and line is valid (doesn't begin with a
493 * space, tab, or eol) */
494 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
495 && (p_line[0] != '\t') )
497 strcpy( p_index, p_line );
498 p_index += strlen( p_line );
506 * Save module config in file
508 permanent = config_GetConfigFile (p_this);
511 module_list_free (list);
515 if (asprintf (&temporary, "%s.%u", permanent, getpid ()) == -1)
518 module_list_free (list);
522 /* The temporary configuration file is per-PID. Therefore SaveConfigFile()
523 * should be serialized against itself within a given process. */
524 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
525 vlc_mutex_lock (&lock);
527 int fd = utf8_open (temporary, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
530 vlc_mutex_unlock (&lock);
531 module_list_free (list);
534 file = fdopen (fd, "wt");
538 vlc_mutex_unlock (&lock);
539 module_list_free (list);
543 fprintf( file, "\xEF\xBB\xBF###\n### " COPYRIGHT_MESSAGE "\n###\n\n"
544 "###\n### lines beginning with a '#' character are comments\n###\n\n" );
546 /* Ensure consistent number formatting... */
547 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
548 locale_t baseloc = uselocale (loc);
550 /* Look for the selected module, if NULL then save everything */
551 for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ )
553 module_config_t *p_item, *p_end;
555 if( psz_module_name && strcmp( psz_module_name,
556 p_parser->psz_object_name ) )
559 if( !p_parser->i_config_items )
562 if( psz_module_name )
563 msg_Dbg( p_this, "saving config for module \"%s\"",
564 p_parser->psz_object_name );
566 fprintf( file, "[%s]", p_parser->psz_object_name );
567 if( p_parser->psz_longname )
568 fprintf( file, " # %s\n\n", p_parser->psz_longname );
570 fprintf( file, "\n\n" );
572 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
576 if ((p_item->i_type & CONFIG_HINT) /* ignore hint */
577 || p_item->b_removed /* ignore deprecated option */
578 || p_item->b_unsaveable) /* ignore volatile option */
581 vlc_mutex_lock (p_item->p_lock);
583 /* Do not save the new value in the configuration file
584 * if doing an autosave, and the item is not an "autosaved" one. */
585 bool b_retain = b_autosave && !p_item->b_autosave;
587 if (IsConfigIntegerType (p_item->i_type))
589 int val = b_retain ? p_item->saved.i : p_item->value.i;
590 if (p_item->i_type == CONFIG_ITEM_KEY)
592 char *psz_key = ConfigKeyToString (val);
593 config_Write (file, p_item->psz_text, N_("key"),
594 val == p_item->orig.i,
595 p_item->psz_name, "%s",
596 psz_key ? psz_key : "");
600 config_Write (file, p_item->psz_text,
601 (p_item->i_type == CONFIG_ITEM_BOOL)
602 ? N_("boolean") : N_("integer"),
603 val == p_item->orig.i,
604 p_item->psz_name, "%d", val);
605 p_item->saved.i = val;
608 if (IsConfigFloatType (p_item->i_type))
610 float val = b_retain ? p_item->saved.f : p_item->value.f;
611 config_Write (file, p_item->psz_text, N_("float"),
612 val == p_item->orig.f,
613 p_item->psz_name, "%f", val);
614 p_item->saved.f = val;
618 const char *psz_value = b_retain ? p_item->saved.psz
622 assert (IsConfigStringType (p_item->i_type));
624 if (b_retain && (psz_value == NULL)) /* FIXME: hack */
625 psz_value = p_item->orig.psz;
629 ? ((p_item->orig.psz != NULL)
630 ? (strcmp (psz_value, p_item->orig.psz) != 0)
632 : (p_item->orig.psz != NULL);
634 config_Write (file, p_item->psz_text, N_("string"),
635 !modified, p_item->psz_name, "%s",
636 psz_value ? psz_value : "");
641 free ((char *)p_item->saved.psz);
642 if( (psz_value && p_item->orig.psz &&
643 strcmp( psz_value, p_item->orig.psz )) ||
644 !psz_value || !p_item->orig.psz)
645 p_item->saved.psz = strdupnull (psz_value);
647 p_item->saved.psz = NULL;
652 p_item->b_dirty = false;
653 vlc_mutex_unlock (p_item->p_lock);
657 module_list_free (list);
658 if (loc != (locale_t)0)
665 * Restore old settings from the config in file
667 fputs( p_bigbuffer, file );
671 * Flush to disk and replace atomically
673 fflush (file); /* Flush from run-time */
675 fdatasync (fd); /* Flush from OS */
676 /* Atomically replace the file... */
677 rename (temporary, permanent);
678 /* (...then synchronize the directory, err, TODO...) */
679 /* ...and finally close the file */
680 vlc_mutex_unlock (&lock);
684 /* Windows cannot remove open files nor overwrite existing ones */
685 utf8_unlink (permanent);
686 rename (temporary, permanent);
687 vlc_mutex_unlock (&lock);
703 int config_AutoSaveConfigFile( vlc_object_t *p_this )
710 /* Check if there's anything to save */
711 module_t **list = module_list_get (NULL);
712 for( i_index = 0; list[i_index] && !save; i_index++ )
714 module_t *p_parser = list[i_index];
715 module_config_t *p_item, *p_end;
717 if( !p_parser->i_config_items ) continue;
719 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
720 p_item < p_end && !save;
723 vlc_mutex_lock (p_item->p_lock);
724 save = p_item->b_autosave && p_item->b_dirty;
725 vlc_mutex_unlock (p_item->p_lock);
728 module_list_free (list);
730 return save ? VLC_SUCCESS : SaveConfigFile( p_this, NULL, true );
733 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
735 return SaveConfigFile( p_this, psz_module_name, false );
738 int ConfigStringToKey( const char *psz_key )
742 const char *psz_parser = strchr( psz_key, '-' );
743 while( psz_parser && psz_parser != psz_key )
745 for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
747 if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
748 strlen( vlc_modifiers[i].psz_key_string ) ) )
750 i_key |= vlc_modifiers[i].i_key_code;
753 psz_key = psz_parser + 1;
754 psz_parser = strchr( psz_key, '-' );
756 for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
758 if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
760 i_key |= vlc_keys[i].i_key_code;
767 char *ConfigKeyToString( int i_key )
769 char *psz_key = malloc( 100 );
780 for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
783 if( i_key & vlc_modifiers[index].i_key_code )
785 p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
788 for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
791 if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
793 p += sprintf( p, "%s", vlc_keys[index].psz_key_string );