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 */
43 #include "configuration.h"
44 #include "modules/modules.h"
46 static char *ConfigKeyToString( int );
48 static inline char *strdupnull (const char *src)
50 return src ? strdup (src) : NULL;
54 * Get the user's configuration file
56 static char *config_GetConfigFile( vlc_object_t *obj )
58 char *psz_file = config_GetPsz( obj, "config" );
59 if( psz_file == NULL )
61 char *psz_dir = config_GetUserConfDir();
63 if( asprintf( &psz_file, "%s" DIR_SEP CONFIG_FILE, psz_dir ) == -1 )
70 static FILE *config_OpenConfigFile( vlc_object_t *p_obj )
72 char *psz_filename = config_GetConfigFile( p_obj );
73 if( psz_filename == NULL )
76 msg_Dbg( p_obj, "opening config file (%s)", psz_filename );
78 FILE *p_stream = utf8_fopen( psz_filename, "rt" );
79 if( p_stream == NULL && errno != ENOENT )
81 msg_Err( p_obj, "cannot open config file (%s): %m",
85 #if !( defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS) )
86 else if( p_stream == NULL && errno == ENOENT )
88 /* This is the fallback for pre XDG Base Directory
89 * Specification configs */
91 if( asprintf( &psz_old, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE,
92 config_GetHomeDir() ) != -1 )
94 p_stream = utf8_fopen( psz_old, "rt" );
97 /* Old config file found. We want to write it at the
98 * new location now. */
99 msg_Info( p_obj->p_libvlc, "Found old config file at %s. "
100 "VLC will now use %s.", psz_old, psz_filename );
102 if( asprintf(&psz_readme,"%s"DIR_SEP CONFIG_DIR DIR_SEP"README",
103 config_GetHomeDir() ) != -1 )
105 FILE *p_readme = utf8_fopen( psz_readme, "wt" );
108 fprintf( p_readme, "The VLC media player "
109 "configuration folder has moved to comply\n"
110 "with the XDG Base Directory Specification "
111 "version 0.6. Your\nconfiguration has been "
112 "copied to the new location:\n%s\nYou can "
113 "delete this directory and all its contents.",
128 static int strtoi (const char *str)
134 l = strtol (str, &end, 0);
138 if ((l > INT_MAX) || (l < INT_MIN))
147 /*****************************************************************************
148 * config_LoadConfigFile: loads the configuration file.
149 *****************************************************************************
150 * This function is called to load the config options stored in the config
152 *****************************************************************************/
153 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
157 file = config_OpenConfigFile (p_this);
161 /* Look for the selected module, if NULL then save everything */
162 module_t **list = module_list_get (NULL);
164 /* Look for UTF-8 Byte Order Mark */
165 char * (*convert) (const char *) = strdupnull;
168 if ((fread (bom, 1, 3, file) != 3)
169 || memcmp (bom, "\xEF\xBB\xBF", 3))
171 convert = FromLocaleDup;
172 rewind (file); /* no BOM, rewind */
175 module_t *module = NULL;
176 char line[1024], section[1022];
179 /* Ensure consistent number formatting... */
180 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
181 locale_t baseloc = uselocale (loc);
183 while (fgets (line, 1024, file) != NULL)
185 /* Ignore comments and empty lines */
196 char *ptr = strchr (line, ']');
198 continue; /* syntax error; */
201 /* New section ( = a given module) */
202 strcpy (section, line + 1);
205 if ((psz_module_name == NULL)
206 || (strcmp (psz_module_name, section) == 0))
208 for (int i = 0; list[i]; i++)
210 module_t *m = list[i];
212 if ((strcmp (section, m->psz_object_name) == 0)
213 && (m->i_config_items > 0)) /* ignore config-less modules */
216 if (psz_module_name != NULL)
218 "loading config for module \"%s\"",
229 continue; /* no need to parse if there is no matching module */
231 char *ptr = strchr (line, '\n');
235 /* look for option name */
236 const char *psz_option_name = line;
238 ptr = strchr (line, '=');
240 continue; /* syntax error */
243 const char *psz_option_value = ptr + 1;
245 /* try to match this option with one of the module's options */
246 for (size_t i = 0; i < module->confsize; i++)
248 module_config_t *p_item = module->p_config + i;
250 if ((p_item->i_type & CONFIG_HINT)
251 || strcmp (p_item->psz_name, psz_option_name))
257 vlc_mutex_lock( p_item->p_lock );
258 switch( p_item->i_type )
260 case CONFIG_ITEM_BOOL:
261 case CONFIG_ITEM_INTEGER:
263 long l = strtoi (psz_option_value);
265 msg_Warn (p_this, "Integer value (%s) for %s: %m",
266 psz_option_value, psz_option_name);
268 p_item->saved.i = p_item->value.i = (int)l;
272 case CONFIG_ITEM_FLOAT:
273 if( !*psz_option_value )
274 break; /* ignore empty option */
275 p_item->value.f = (float)atof (psz_option_value);
276 p_item->saved.f = p_item->value.f;
279 case CONFIG_ITEM_KEY:
280 if( !*psz_option_value )
281 break; /* ignore empty option */
282 p_item->value.i = ConfigStringToKey(psz_option_value);
283 p_item->saved.i = p_item->value.i;
287 /* free old string */
288 free( (char*) p_item->value.psz );
289 free( (char*) p_item->saved.psz );
291 p_item->value.psz = convert (psz_option_value);
292 p_item->saved.psz = strdupnull (p_item->value.psz);
295 vlc_mutex_unlock( p_item->p_lock );
302 msg_Err (p_this, "error reading configuration: %m");
307 module_list_free (list);
308 if (loc != (locale_t)0)
316 /*****************************************************************************
317 * config_CreateDir: Create configuration directory if it doesn't exist.
318 *****************************************************************************/
319 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
321 if( !psz_dirname || !*psz_dirname ) return -1;
323 if( utf8_mkdir( psz_dirname, 0700 ) == 0 )
333 /* Let's try to create the parent directory */
334 char psz_parent[strlen( psz_dirname ) + 1], *psz_end;
335 strcpy( psz_parent, psz_dirname );
337 psz_end = strrchr( psz_parent, DIR_SEP_CHAR );
338 if( psz_end && psz_end != psz_parent )
341 if( config_CreateDir( p_this, psz_parent ) == 0 )
343 if( !utf8_mkdir( psz_dirname, 0700 ) )
350 msg_Err( p_this, "could not create %s: %m", psz_dirname );
355 config_Write (FILE *file, const char *type, const char *desc,
356 bool comment, const char *name, const char *fmt, ...)
364 if (fprintf (file, "# %s (%s)\n%s%s=", desc, vlc_gettext (type),
365 comment ? "#" : "", name) < 0)
369 ret = vfprintf (file, fmt, ap);
374 if (fputs ("\n\n", file) == EOF)
380 static int config_PrepareDir (vlc_object_t *obj)
382 char *psz_configdir = config_GetUserConfDir ();
383 if (psz_configdir == NULL) /* XXX: This should never happen */
386 int ret = config_CreateDir (obj, psz_configdir);
387 free (psz_configdir);
391 /*****************************************************************************
392 * config_SaveConfigFile: Save a module's config options.
393 *****************************************************************************
394 * This will save the specified module's config options to the config file.
395 * If psz_module_name is NULL then we save all the modules config options.
396 * It's no use to save the config options that kept their default values, so
397 * we'll try to be a bit clever here.
399 * When we save we mustn't delete the config options of the modules that
400 * haven't been loaded. So we cannot just create a new config file with the
401 * config structures we've got in memory.
402 * I don't really know how to deal with this nicely, so I will use a completly
404 * I will load the config file in memory, but skipping all the sections of the
405 * modules we want to save. Then I will create a brand new file, dump the file
406 * loaded in memory and then append the sections of the modules we want to
409 *****************************************************************************/
410 static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
415 char *permanent = NULL, *temporary = NULL;
416 char p_line[1024], *p_index2;
417 unsigned long i_sizebuf = 0;
418 char *p_bigbuffer = NULL, *p_index;
422 if( config_PrepareDir( p_this ) )
424 msg_Err( p_this, "no configuration directory" );
428 file = config_OpenConfigFile( p_this );
431 /* look for file size */
432 fseek( file, 0L, SEEK_END );
433 i_sizebuf = ftell( file );
434 fseek( file, 0L, SEEK_SET );
435 if( i_sizebuf >= LONG_MAX )
439 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
444 /* List all available modules */
445 module_t **list = module_list_get (NULL);
447 /* backup file into memory, we only need to backup the sections we won't
450 while( file && fgets( p_line, 1024, file ) )
452 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
455 /* we found a section, check if we need to do a backup */
456 for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ )
458 if( ((p_index2 - &p_line[1])
459 == (int)strlen(p_parser->psz_object_name) )
460 && !memcmp( &p_line[1], p_parser->psz_object_name,
461 strlen(p_parser->psz_object_name) ) )
463 if( !psz_module_name )
465 else if( !strcmp( psz_module_name,
466 p_parser->psz_object_name ) )
471 if( list[i_index] == NULL )
473 /* we don't have this section in our list so we need to back
477 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
490 /* save line if requested and line is valid (doesn't begin with a
491 * space, tab, or eol) */
492 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
493 && (p_line[0] != '\t') )
495 strcpy( p_index, p_line );
496 p_index += strlen( p_line );
504 * Save module config in file
506 permanent = config_GetConfigFile (p_this);
509 module_list_free (list);
513 if (asprintf (&temporary, "%s.%u", permanent, getpid ()) == -1)
516 module_list_free (list);
520 /* The temporary configuration file is per-PID. Therefore SaveConfigFile()
521 * should be serialized against itself within a given process. */
522 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
523 vlc_mutex_lock (&lock);
525 int fd = utf8_open (temporary, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
528 vlc_mutex_unlock (&lock);
529 module_list_free (list);
532 file = fdopen (fd, "wt");
536 vlc_mutex_unlock (&lock);
537 module_list_free (list);
541 fprintf( file, "\xEF\xBB\xBF###\n### " COPYRIGHT_MESSAGE "\n###\n\n"
542 "###\n### lines beginning with a '#' character are comments\n###\n\n" );
544 /* Ensure consistent number formatting... */
545 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
546 locale_t baseloc = uselocale (loc);
548 /* Look for the selected module, if NULL then save everything */
549 for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ )
551 module_config_t *p_item, *p_end;
553 if( psz_module_name && strcmp( psz_module_name,
554 p_parser->psz_object_name ) )
557 if( !p_parser->i_config_items )
560 if( psz_module_name )
561 msg_Dbg( p_this, "saving config for module \"%s\"",
562 p_parser->psz_object_name );
564 fprintf( file, "[%s]", p_parser->psz_object_name );
565 if( p_parser->psz_longname )
566 fprintf( file, " # %s\n\n", p_parser->psz_longname );
568 fprintf( file, "\n\n" );
570 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
574 if ((p_item->i_type & CONFIG_HINT) /* ignore hint */
575 || p_item->b_removed /* ignore deprecated option */
576 || p_item->b_unsaveable) /* ignore volatile option */
579 vlc_mutex_lock (p_item->p_lock);
581 /* Do not save the new value in the configuration file
582 * if doing an autosave, and the item is not an "autosaved" one. */
583 bool b_retain = b_autosave && !p_item->b_autosave;
585 if (IsConfigIntegerType (p_item->i_type))
587 int val = b_retain ? p_item->saved.i : p_item->value.i;
588 if (p_item->i_type == CONFIG_ITEM_KEY)
590 char *psz_key = ConfigKeyToString (val);
591 config_Write (file, p_item->psz_text, N_("key"),
592 val == p_item->orig.i,
593 p_item->psz_name, "%s",
594 psz_key ? psz_key : "");
598 config_Write (file, p_item->psz_text,
599 (p_item->i_type == CONFIG_ITEM_BOOL)
600 ? N_("boolean") : N_("integer"),
601 val == p_item->orig.i,
602 p_item->psz_name, "%d", val);
603 p_item->saved.i = val;
606 if (IsConfigFloatType (p_item->i_type))
608 float val = b_retain ? p_item->saved.f : p_item->value.f;
609 config_Write (file, p_item->psz_text, N_("float"),
610 val == p_item->orig.f,
611 p_item->psz_name, "%f", val);
612 p_item->saved.f = val;
616 const char *psz_value = b_retain ? p_item->saved.psz
620 assert (IsConfigStringType (p_item->i_type));
622 if (b_retain && (psz_value == NULL)) /* FIXME: hack */
623 psz_value = p_item->orig.psz;
627 ? ((p_item->orig.psz != NULL)
628 ? (strcmp (psz_value, p_item->orig.psz) != 0)
630 : (p_item->orig.psz != NULL);
632 config_Write (file, p_item->psz_text, N_("string"),
633 !modified, p_item->psz_name, "%s",
634 psz_value ? psz_value : "");
639 free ((char *)p_item->saved.psz);
640 if( (psz_value && p_item->orig.psz &&
641 strcmp( psz_value, p_item->orig.psz )) ||
642 !psz_value || !p_item->orig.psz)
643 p_item->saved.psz = strdupnull (psz_value);
645 p_item->saved.psz = NULL;
650 p_item->b_dirty = false;
651 vlc_mutex_unlock (p_item->p_lock);
655 module_list_free (list);
656 if (loc != (locale_t)0)
663 * Restore old settings from the config in file
665 fputs( p_bigbuffer, file );
669 * Flush to disk and replace atomically
671 fflush (file); /* Flush from run-time */
673 fdatasync (fd); /* Flush from OS */
674 /* Atomically replace the file... */
675 rename (temporary, permanent);
676 /* (...then synchronize the directory, err, TODO...) */
677 /* ...and finally close the file */
678 vlc_mutex_unlock (&lock);
682 /* Windows cannot remove open files nor overwrite existing ones */
684 rename (temporary, permanent);
685 vlc_mutex_unlock (&lock);
701 int config_AutoSaveConfigFile( vlc_object_t *p_this )
708 /* Check if there's anything to save */
709 module_t **list = module_list_get (NULL);
710 for( i_index = 0; list[i_index] && !save; i_index++ )
712 module_t *p_parser = list[i_index];
713 module_config_t *p_item, *p_end;
715 if( !p_parser->i_config_items ) continue;
717 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
718 p_item < p_end && !save;
721 vlc_mutex_lock (p_item->p_lock);
722 save = p_item->b_autosave && p_item->b_dirty;
723 vlc_mutex_unlock (p_item->p_lock);
726 module_list_free (list);
728 return save ? VLC_SUCCESS : SaveConfigFile( p_this, NULL, true );
731 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
733 return SaveConfigFile( p_this, psz_module_name, false );
736 int ConfigStringToKey( const char *psz_key )
740 const char *psz_parser = strchr( psz_key, '-' );
741 while( psz_parser && psz_parser != psz_key )
743 for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
745 if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
746 strlen( vlc_modifiers[i].psz_key_string ) ) )
748 i_key |= vlc_modifiers[i].i_key_code;
751 psz_key = psz_parser + 1;
752 psz_parser = strchr( psz_key, '-' );
754 for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
756 if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
758 i_key |= vlc_keys[i].i_key_code;
765 char *ConfigKeyToString( int i_key )
767 char *psz_key = malloc( 100 );
778 for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
781 if( i_key & vlc_modifiers[index].i_key_code )
783 p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
786 for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
789 if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
791 p += sprintf( p, "%s", vlc_keys[index].psz_key_string );