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, const char *mode )
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, mode );
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 && mode[0] == 'r' )
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, mode );
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 )
155 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
158 file = config_OpenConfigFile (p_this, "rt");
162 /* Acquire config file lock */
163 vlc_mutex_lock( &priv->config_lock );
165 /* Look for the selected module, if NULL then save everything */
166 module_t **list = module_list_get (NULL);
168 /* Look for UTF-8 Byte Order Mark */
169 char * (*convert) (const char *) = strdupnull;
172 if ((fread (bom, 1, 3, file) != 3)
173 || memcmp (bom, "\xEF\xBB\xBF", 3))
175 convert = FromLocaleDup;
176 rewind (file); /* no BOM, rewind */
179 module_t *module = NULL;
180 char line[1024], section[1022];
183 /* Ensure consistent number formatting... */
184 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
185 locale_t baseloc = uselocale (loc);
187 while (fgets (line, 1024, file) != NULL)
189 /* Ignore comments and empty lines */
200 char *ptr = strchr (line, ']');
202 continue; /* syntax error; */
205 /* New section ( = a given module) */
206 strcpy (section, line + 1);
209 if ((psz_module_name == NULL)
210 || (strcmp (psz_module_name, section) == 0))
212 for (int i = 0; list[i]; i++)
214 module_t *m = list[i];
216 if ((strcmp (section, m->psz_object_name) == 0)
217 && (m->i_config_items > 0)) /* ignore config-less modules */
220 if (psz_module_name != NULL)
222 "loading config for module \"%s\"",
233 continue; /* no need to parse if there is no matching module */
235 char *ptr = strchr (line, '\n');
239 /* look for option name */
240 const char *psz_option_name = line;
242 ptr = strchr (line, '=');
244 continue; /* syntax error */
247 const char *psz_option_value = ptr + 1;
249 /* try to match this option with one of the module's options */
250 for (size_t i = 0; i < module->confsize; i++)
252 module_config_t *p_item = module->p_config + i;
254 if ((p_item->i_type & CONFIG_HINT)
255 || strcmp (p_item->psz_name, psz_option_name))
261 switch( p_item->i_type )
263 case CONFIG_ITEM_BOOL:
264 case CONFIG_ITEM_INTEGER:
266 long l = strtoi (psz_option_value);
268 msg_Warn (p_this, "Integer value (%s) for %s: %m",
269 psz_option_value, psz_option_name);
271 p_item->saved.i = p_item->value.i = (int)l;
275 case CONFIG_ITEM_FLOAT:
276 if( !*psz_option_value )
277 break; /* ignore empty option */
278 p_item->value.f = (float)atof (psz_option_value);
279 p_item->saved.f = p_item->value.f;
282 case CONFIG_ITEM_KEY:
283 if( !*psz_option_value )
284 break; /* ignore empty option */
285 p_item->value.i = ConfigStringToKey(psz_option_value);
286 p_item->saved.i = p_item->value.i;
290 vlc_mutex_lock( p_item->p_lock );
292 /* free old string */
293 free( (char*) p_item->value.psz );
294 free( (char*) p_item->saved.psz );
296 p_item->value.psz = convert (psz_option_value);
297 p_item->saved.psz = strdupnull (p_item->value.psz);
299 vlc_mutex_unlock( p_item->p_lock );
309 msg_Err (p_this, "error reading configuration: %m");
314 module_list_free (list);
315 if (loc != (locale_t)0)
321 vlc_mutex_unlock( &priv->config_lock );
325 /*****************************************************************************
326 * config_CreateDir: Create configuration directory if it doesn't exist.
327 *****************************************************************************/
328 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
330 if( !psz_dirname || !*psz_dirname ) return -1;
332 if( utf8_mkdir( psz_dirname, 0700 ) == 0 )
342 /* Let's try to create the parent directory */
343 char psz_parent[strlen( psz_dirname ) + 1], *psz_end;
344 strcpy( psz_parent, psz_dirname );
346 psz_end = strrchr( psz_parent, DIR_SEP_CHAR );
347 if( psz_end && psz_end != psz_parent )
350 if( config_CreateDir( p_this, psz_parent ) == 0 )
352 if( !utf8_mkdir( psz_dirname, 0700 ) )
359 msg_Err( p_this, "could not create %s: %m", psz_dirname );
364 config_Write (FILE *file, const char *type, const char *desc,
365 bool comment, const char *name, const char *fmt, ...)
373 if (fprintf (file, "# %s (%s)\n%s%s=", desc, vlc_gettext (type),
374 comment ? "#" : "", name) < 0)
378 ret = vfprintf (file, fmt, ap);
383 if (fputs ("\n\n", file) == EOF)
389 static int config_PrepareDir (vlc_object_t *obj)
391 char *psz_configdir = config_GetUserConfDir ();
392 if (psz_configdir == NULL) /* XXX: This should never happen */
395 int ret = config_CreateDir (obj, psz_configdir);
396 free (psz_configdir);
400 /*****************************************************************************
401 * config_SaveConfigFile: Save a module's config options.
402 *****************************************************************************
403 * This will save the specified module's config options to the config file.
404 * If psz_module_name is NULL then we save all the modules config options.
405 * It's no use to save the config options that kept their default values, so
406 * we'll try to be a bit clever here.
408 * When we save we mustn't delete the config options of the modules that
409 * haven't been loaded. So we cannot just create a new config file with the
410 * config structures we've got in memory.
411 * I don't really know how to deal with this nicely, so I will use a completly
413 * I will load the config file in memory, but skipping all the sections of the
414 * modules we want to save. Then I will create a brand new file, dump the file
415 * loaded in memory and then append the sections of the modules we want to
418 *****************************************************************************/
419 static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
422 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
425 char *permanent = NULL, *temporary = NULL;
426 char p_line[1024], *p_index2;
427 unsigned long i_sizebuf = 0;
428 char *p_bigbuffer = NULL, *p_index;
432 /* Acquire config file lock */
433 vlc_mutex_lock( &priv->config_lock );
435 if( config_PrepareDir( p_this ) )
437 msg_Err( p_this, "no configuration directory" );
441 file = config_OpenConfigFile( p_this, "rt" );
444 /* look for file size */
445 fseek( file, 0L, SEEK_END );
446 i_sizebuf = ftell( file );
447 fseek( file, 0L, SEEK_SET );
448 if( i_sizebuf >= LONG_MAX )
452 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
457 /* List all available modules */
458 module_t **list = module_list_get (NULL);
460 /* backup file into memory, we only need to backup the sections we won't
463 while( file && fgets( p_line, 1024, file ) )
465 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
468 /* we found a section, check if we need to do a backup */
469 for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ )
471 if( ((p_index2 - &p_line[1])
472 == (int)strlen(p_parser->psz_object_name) )
473 && !memcmp( &p_line[1], p_parser->psz_object_name,
474 strlen(p_parser->psz_object_name) ) )
476 if( !psz_module_name )
478 else if( !strcmp( psz_module_name,
479 p_parser->psz_object_name ) )
484 if( list[i_index] == NULL )
486 /* we don't have this section in our list so we need to back
490 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
503 /* save line if requested and line is valid (doesn't begin with a
504 * space, tab, or eol) */
505 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
506 && (p_line[0] != '\t') )
508 strcpy( p_index, p_line );
509 p_index += strlen( p_line );
517 * Save module config in file
519 permanent = config_GetConfigFile (p_this);
522 module_list_free (list);
526 if (asprintf (&temporary, "%s.%u", permanent, getpid ()) == -1)
529 module_list_free (list);
533 int fd = utf8_open (temporary, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
536 module_list_free (list);
539 file = fdopen (fd, "wt");
543 module_list_free (list);
547 fprintf( file, "\xEF\xBB\xBF###\n### " COPYRIGHT_MESSAGE "\n###\n\n"
548 "###\n### lines beginning with a '#' character are comments\n###\n\n" );
550 /* Ensure consistent number formatting... */
551 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
552 locale_t baseloc = uselocale (loc);
554 /* Look for the selected module, if NULL then save everything */
555 for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ )
557 module_config_t *p_item, *p_end;
559 if( psz_module_name && strcmp( psz_module_name,
560 p_parser->psz_object_name ) )
563 if( !p_parser->i_config_items )
566 if( psz_module_name )
567 msg_Dbg( p_this, "saving config for module \"%s\"",
568 p_parser->psz_object_name );
570 fprintf( file, "[%s]", p_parser->psz_object_name );
571 if( p_parser->psz_longname )
572 fprintf( file, " # %s\n\n", p_parser->psz_longname );
574 fprintf( file, "\n\n" );
576 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
580 /* Do not save the new value in the configuration file
581 * if doing an autosave, and the item is not an "autosaved" one. */
582 bool b_retain = b_autosave && !p_item->b_autosave;
584 if ((p_item->i_type & CONFIG_HINT) /* ignore hint */
585 || p_item->b_removed /* ignore deprecated option */
586 || p_item->b_unsaveable) /* ignore volatile option */
589 if (IsConfigIntegerType (p_item->i_type))
591 int val = b_retain ? p_item->saved.i : p_item->value.i;
592 if (p_item->i_type == CONFIG_ITEM_KEY)
594 char *psz_key = ConfigKeyToString (val);
595 config_Write (file, p_item->psz_text, N_("key"),
596 val == p_item->orig.i,
597 p_item->psz_name, "%s",
598 psz_key ? psz_key : "");
602 config_Write (file, p_item->psz_text,
603 (p_item->i_type == CONFIG_ITEM_BOOL)
604 ? N_("boolean") : N_("integer"),
605 val == p_item->orig.i,
606 p_item->psz_name, "%d", val);
607 p_item->saved.i = val;
610 if (IsConfigFloatType (p_item->i_type))
612 float val = b_retain ? p_item->saved.f : p_item->value.f;
613 config_Write (file, p_item->psz_text, N_("float"),
614 val == p_item->orig.f,
615 p_item->psz_name, "%f", val);
616 p_item->saved.f = val;
620 const char *psz_value = b_retain ? p_item->saved.psz
624 assert (IsConfigStringType (p_item->i_type));
626 if (b_retain && (psz_value == NULL)) /* FIXME: hack */
627 psz_value = p_item->orig.psz;
631 ? ((p_item->orig.psz != NULL)
632 ? (strcmp (psz_value, p_item->orig.psz) != 0)
634 : (p_item->orig.psz != NULL);
636 config_Write (file, p_item->psz_text, N_("string"),
637 !modified, p_item->psz_name, "%s",
638 psz_value ? psz_value : "");
643 free ((char *)p_item->saved.psz);
644 if( (psz_value && p_item->orig.psz &&
645 strcmp( psz_value, p_item->orig.psz )) ||
646 !psz_value || !p_item->orig.psz)
647 p_item->saved.psz = strdupnull (psz_value);
649 p_item->saved.psz = NULL;
654 p_item->b_dirty = false;
658 module_list_free (list);
659 if (loc != (locale_t)0)
666 * Restore old settings from the config in file
668 fputs( p_bigbuffer, file );
672 * Flush to disk and replace atomically
674 fflush (file); /* Flush from run-time */
676 fdatasync (fd); /* Flush from OS */
677 /* Atomically replace the file... */
678 rename (temporary, permanent);
679 /* (...then synchronize the directory, err, TODO...) */
680 /* ...and finally close the file */
682 vlc_mutex_unlock (&priv->config_lock);
685 /* Windows cannot remove open files nor overwrite existing ones */
687 rename (temporary, permanent);
697 vlc_mutex_unlock( &priv->config_lock );
704 int config_AutoSaveConfigFile( vlc_object_t *p_this )
706 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
712 /* Check if there's anything to save */
713 vlc_mutex_lock( &priv->config_lock );
714 module_t **list = module_list_get (NULL);
715 for( i_index = 0; list[i_index]; i_index++ )
717 module_t *p_parser = list[i_index];
718 module_config_t *p_item, *p_end;
720 if( !p_parser->i_config_items ) continue;
722 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
726 if( p_item->b_autosave && p_item->b_dirty ) break;
728 if( p_item < p_end ) break;
730 done = list[i_index] == NULL;
731 module_list_free (list);
732 vlc_mutex_unlock( &priv->config_lock );
734 return done ? VLC_SUCCESS : SaveConfigFile( p_this, NULL, true );
737 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
739 return SaveConfigFile( p_this, psz_module_name, false );
742 int ConfigStringToKey( const char *psz_key )
746 const char *psz_parser = strchr( psz_key, '-' );
747 while( psz_parser && psz_parser != psz_key )
749 for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
751 if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
752 strlen( vlc_modifiers[i].psz_key_string ) ) )
754 i_key |= vlc_modifiers[i].i_key_code;
757 psz_key = psz_parser + 1;
758 psz_parser = strchr( psz_key, '-' );
760 for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
762 if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
764 i_key |= vlc_keys[i].i_key_code;
771 char *ConfigKeyToString( int i_key )
773 char *psz_key = malloc( 100 );
784 for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
787 if( i_key & vlc_modifiers[index].i_key_code )
789 p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
792 for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
795 if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
797 p += sprintf( p, "%s", vlc_keys[index].psz_key_string );