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 along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <errno.h> /* errno */
39 #include <vlc_common.h>
40 #include "../libvlc.h"
41 #include "vlc_charset.h"
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_GetUserDir( VLC_CONFIG_DIR );
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 */
91 char *home = config_GetUserDir(VLC_HOME_DIR);
95 && asprintf( &psz_old, "%s/.vlc/" CONFIG_FILE,
98 p_stream = utf8_fopen( psz_old, "rt" );
101 /* Old config file found. We want to write it at the
102 * new location now. */
103 msg_Info( p_obj->p_libvlc, "Found old config file at %s. "
104 "VLC will now use %s.", psz_old, psz_filename );
106 if( asprintf(&psz_readme,"%s/.vlc/README",
109 FILE *p_readme = utf8_fopen( psz_readme, "wt" );
112 fprintf( p_readme, "The VLC media player "
113 "configuration folder has moved to comply\n"
114 "with the XDG Base Directory Specification "
115 "version 0.6. Your\nconfiguration has been "
116 "copied to the new location:\n%s\nYou can "
117 "delete this directory and all its contents.",
129 free( psz_filename );
134 static int strtoi (const char *str)
140 l = strtol (str, &end, 0);
144 if ((l > INT_MAX) || (l < INT_MIN))
153 /*****************************************************************************
154 * config_LoadConfigFile: loads the configuration file.
155 *****************************************************************************
156 * This function is called to load the config options stored in the config
158 *****************************************************************************/
159 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
163 file = config_OpenConfigFile (p_this);
167 /* Look for the selected module, if NULL then save everything */
168 module_t **list = module_list_get (NULL);
170 /* Look for UTF-8 Byte Order Mark */
171 char * (*convert) (const char *) = strdupnull;
174 if ((fread (bom, 1, 3, file) != 3)
175 || memcmp (bom, "\xEF\xBB\xBF", 3))
177 convert = FromLocaleDup;
178 rewind (file); /* no BOM, rewind */
181 module_t *module = NULL;
182 char line[1024], section[1022];
185 /* Ensure consistent number formatting... */
186 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
187 locale_t baseloc = uselocale (loc);
189 while (fgets (line, 1024, file) != NULL)
191 /* Ignore comments and empty lines */
202 char *ptr = strchr (line, ']');
204 continue; /* syntax error; */
207 /* New section ( = a given module) */
208 strcpy (section, line + 1);
211 if ((psz_module_name == NULL)
212 || (strcmp (psz_module_name, section) == 0))
214 for (int i = 0; list[i]; i++)
216 module_t *m = list[i];
218 if ((strcmp (section, m->psz_object_name) == 0)
219 && (m->i_config_items > 0)) /* ignore config-less modules */
222 if (psz_module_name != NULL)
224 "loading config for module \"%s\"",
235 continue; /* no need to parse if there is no matching module */
237 char *ptr = strchr (line, '\n');
241 /* look for option name */
242 const char *psz_option_name = line;
244 ptr = strchr (line, '=');
246 continue; /* syntax error */
249 const char *psz_option_value = ptr + 1;
251 /* try to match this option with one of the module's options */
252 for (size_t i = 0; i < module->confsize; i++)
254 module_config_t *p_item = module->p_config + i;
256 if ((p_item->i_type & CONFIG_HINT)
257 || strcmp (p_item->psz_name, psz_option_name))
263 vlc_mutex_lock( p_item->p_lock );
264 switch( p_item->i_type )
266 case CONFIG_ITEM_BOOL:
267 case CONFIG_ITEM_INTEGER:
269 long l = strtoi (psz_option_value);
271 msg_Warn (p_this, "Integer value (%s) for %s: %m",
272 psz_option_value, psz_option_name);
274 p_item->saved.i = p_item->value.i = (int)l;
278 case CONFIG_ITEM_FLOAT:
279 if( !*psz_option_value )
280 break; /* ignore empty option */
281 p_item->value.f = (float)atof (psz_option_value);
282 p_item->saved.f = p_item->value.f;
285 case CONFIG_ITEM_KEY:
286 if( !*psz_option_value )
287 break; /* ignore empty option */
288 p_item->value.i = ConfigStringToKey(psz_option_value);
289 p_item->saved.i = p_item->value.i;
293 /* free old string */
294 free( (char*) p_item->value.psz );
295 free( (char*) p_item->saved.psz );
297 p_item->value.psz = convert (psz_option_value);
298 p_item->saved.psz = strdupnull (p_item->value.psz);
301 vlc_mutex_unlock( p_item->p_lock );
308 msg_Err (p_this, "error reading configuration: %m");
313 module_list_free (list);
314 if (loc != (locale_t)0)
322 /*****************************************************************************
323 * config_CreateDir: Create configuration directory if it doesn't exist.
324 *****************************************************************************/
325 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
327 if( !psz_dirname || !*psz_dirname ) return -1;
329 if( utf8_mkdir( psz_dirname, 0700 ) == 0 )
339 /* Let's try to create the parent directory */
340 char psz_parent[strlen( psz_dirname ) + 1], *psz_end;
341 strcpy( psz_parent, psz_dirname );
343 psz_end = strrchr( psz_parent, DIR_SEP_CHAR );
344 if( psz_end && psz_end != psz_parent )
347 if( config_CreateDir( p_this, psz_parent ) == 0 )
349 if( !utf8_mkdir( psz_dirname, 0700 ) )
356 msg_Err( p_this, "could not create %s: %m", psz_dirname );
361 config_Write (FILE *file, const char *desc, const char *type,
362 bool comment, const char *name, const char *fmt, ...)
370 if (fprintf (file, "# %s (%s)\n%s%s=", desc, vlc_gettext (type),
371 comment ? "#" : "", name) < 0)
375 ret = vfprintf (file, fmt, ap);
380 if (fputs ("\n\n", file) == EOF)
386 static int config_PrepareDir (vlc_object_t *obj)
388 char *psz_configdir = config_GetUserDir (VLC_CONFIG_DIR);
389 if (psz_configdir == NULL)
392 int ret = config_CreateDir (obj, psz_configdir);
393 free (psz_configdir);
397 /*****************************************************************************
398 * config_SaveConfigFile: Save a module's config options.
399 *****************************************************************************
400 * This will save the specified module's config options to the config file.
401 * If psz_module_name is NULL then we save all the modules config options.
402 * It's no use to save the config options that kept their default values, so
403 * we'll try to be a bit clever here.
405 * When we save we mustn't delete the config options of the modules that
406 * haven't been loaded. So we cannot just create a new config file with the
407 * config structures we've got in memory.
408 * I don't really know how to deal with this nicely, so I will use a completly
410 * I will load the config file in memory, but skipping all the sections of the
411 * modules we want to save. Then I will create a brand new file, dump the file
412 * loaded in memory and then append the sections of the modules we want to
415 *****************************************************************************/
416 static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
421 char *permanent = NULL, *temporary = NULL;
422 char p_line[1024], *p_index2;
423 unsigned long i_sizebuf = 0;
424 char *p_bigbuffer = NULL, *p_index;
428 if( config_PrepareDir( p_this ) )
430 msg_Err( p_this, "no configuration directory" );
434 file = config_OpenConfigFile( p_this );
439 /* Some users make vlcrc read-only to prevent changes.
440 * The atomic replacement scheme breaks this "feature",
441 * so we check for read-only by hand. */
442 if (fstat (fileno (file), &st)
443 || !(st.st_mode & S_IWUSR))
445 msg_Err (p_this, "configuration file is read-only");
448 i_sizebuf = ( st.st_size < LONG_MAX ) ? st.st_size : 0;
451 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
456 /* List all available modules */
457 module_t **list = module_list_get (NULL);
459 /* backup file into memory, we only need to backup the sections we won't
462 while( file && fgets( p_line, 1024, file ) )
464 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
467 /* we found a section, check if we need to do a backup */
468 for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ )
470 if( ((p_index2 - &p_line[1])
471 == (int)strlen(p_parser->psz_object_name) )
472 && !memcmp( &p_line[1], p_parser->psz_object_name,
473 strlen(p_parser->psz_object_name) ) )
475 if( !psz_module_name )
477 else if( !strcmp( psz_module_name,
478 p_parser->psz_object_name ) )
483 if( list[i_index] == NULL )
485 /* we don't have this section in our list so we need to back
489 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
502 /* save line if requested and line is valid (doesn't begin with a
503 * space, tab, or eol) */
504 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
505 && (p_line[0] != '\t') )
507 strcpy( p_index, p_line );
508 p_index += strlen( p_line );
516 * Save module config in file
518 permanent = config_GetConfigFile (p_this);
521 module_list_free (list);
525 if (asprintf (&temporary, "%s.%u", permanent, getpid ()) == -1)
528 module_list_free (list);
532 /* The temporary configuration file is per-PID. Therefore SaveConfigFile()
533 * should be serialized against itself within a given process. */
534 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
535 vlc_mutex_lock (&lock);
537 int fd = utf8_open (temporary, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
540 vlc_mutex_unlock (&lock);
541 module_list_free (list);
544 file = fdopen (fd, "wt");
548 vlc_mutex_unlock (&lock);
549 module_list_free (list);
553 fprintf( file, "\xEF\xBB\xBF###\n### " COPYRIGHT_MESSAGE "\n###\n\n"
554 "###\n### lines beginning with a '#' character are comments\n###\n\n" );
556 /* Ensure consistent number formatting... */
557 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
558 locale_t baseloc = uselocale (loc);
560 /* Look for the selected module, if NULL then save everything */
561 for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ )
563 module_config_t *p_item, *p_end;
565 if( psz_module_name && strcmp( psz_module_name,
566 p_parser->psz_object_name ) )
569 if( !p_parser->i_config_items )
572 if( psz_module_name )
573 msg_Dbg( p_this, "saving config for module \"%s\"",
574 p_parser->psz_object_name );
576 fprintf( file, "[%s]", p_parser->psz_object_name );
577 if( p_parser->psz_longname )
578 fprintf( file, " # %s\n\n", p_parser->psz_longname );
580 fprintf( file, "\n\n" );
582 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
586 if ((p_item->i_type & CONFIG_HINT) /* ignore hint */
587 || p_item->b_removed /* ignore deprecated option */
588 || p_item->b_unsaveable) /* ignore volatile option */
591 vlc_mutex_lock (p_item->p_lock);
593 /* Do not save the new value in the configuration file
594 * if doing an autosave, and the item is not an "autosaved" one. */
595 bool b_retain = b_autosave && !p_item->b_autosave;
597 if (IsConfigIntegerType (p_item->i_type))
599 int val = b_retain ? p_item->saved.i : p_item->value.i;
600 if (p_item->i_type == CONFIG_ITEM_KEY)
602 char *psz_key = ConfigKeyToString (val);
603 config_Write (file, p_item->psz_text, N_("key"),
604 val == p_item->orig.i,
605 p_item->psz_name, "%s",
606 psz_key ? psz_key : "");
610 config_Write (file, p_item->psz_text,
611 (p_item->i_type == CONFIG_ITEM_BOOL)
612 ? N_("boolean") : N_("integer"),
613 val == p_item->orig.i,
614 p_item->psz_name, "%d", val);
615 p_item->saved.i = val;
618 if (IsConfigFloatType (p_item->i_type))
620 float val = b_retain ? p_item->saved.f : p_item->value.f;
621 config_Write (file, p_item->psz_text, N_("float"),
622 val == p_item->orig.f,
623 p_item->psz_name, "%f", val);
624 p_item->saved.f = val;
628 const char *psz_value = b_retain ? p_item->saved.psz
632 assert (IsConfigStringType (p_item->i_type));
634 if (b_retain && (psz_value == NULL)) /* FIXME: hack */
635 psz_value = p_item->orig.psz;
639 ? ((p_item->orig.psz != NULL)
640 ? (strcmp (psz_value, p_item->orig.psz) != 0)
642 : (p_item->orig.psz != NULL);
644 config_Write (file, p_item->psz_text, N_("string"),
645 !modified, p_item->psz_name, "%s",
646 psz_value ? psz_value : "");
651 free ((char *)p_item->saved.psz);
652 if( (psz_value && p_item->orig.psz &&
653 strcmp( psz_value, p_item->orig.psz )) ||
654 !psz_value || !p_item->orig.psz)
655 p_item->saved.psz = strdupnull (psz_value);
657 p_item->saved.psz = NULL;
662 p_item->b_dirty = false;
663 vlc_mutex_unlock (p_item->p_lock);
667 module_list_free (list);
668 if (loc != (locale_t)0)
675 * Restore old settings from the config in file
677 fputs( p_bigbuffer, file );
681 * Flush to disk and replace atomically
683 fflush (file); /* Flush from run-time */
685 fdatasync (fd); /* Flush from OS */
686 /* Atomically replace the file... */
687 if (utf8_rename (temporary, permanent))
688 utf8_unlink (temporary);
689 /* (...then synchronize the directory, err, TODO...) */
690 /* ...and finally close the file */
691 vlc_mutex_unlock (&lock);
695 /* Windows cannot remove open files nor overwrite existing ones */
696 utf8_unlink (permanent);
697 if (utf8_rename (temporary, permanent))
698 utf8_unlink (temporary);
699 vlc_mutex_unlock (&lock);
715 int config_AutoSaveConfigFile( vlc_object_t *p_this )
722 /* Check if there's anything to save */
723 module_t **list = module_list_get (NULL);
724 for( i_index = 0; list[i_index] && !save; i_index++ )
726 module_t *p_parser = list[i_index];
727 module_config_t *p_item, *p_end;
729 if( !p_parser->i_config_items ) continue;
731 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
732 p_item < p_end && !save;
735 vlc_mutex_lock (p_item->p_lock);
736 save = p_item->b_autosave && p_item->b_dirty;
737 vlc_mutex_unlock (p_item->p_lock);
740 module_list_free (list);
742 return save ? VLC_SUCCESS : SaveConfigFile( p_this, NULL, true );
745 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
747 return SaveConfigFile( p_this, psz_module_name, false );
750 int ConfigStringToKey( const char *psz_key )
754 const char *psz_parser = strchr( psz_key, '-' );
755 while( psz_parser && psz_parser != psz_key )
757 for( i = 0; i < vlc_num_modifiers; ++i )
759 if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
760 strlen( vlc_modifiers[i].psz_key_string ) ) )
762 i_key |= vlc_modifiers[i].i_key_code;
765 psz_key = psz_parser + 1;
766 psz_parser = strchr( psz_key, '-' );
768 for( i = 0; i < vlc_num_keys; ++i )
770 if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
772 i_key |= vlc_keys[i].i_key_code;
779 char *ConfigKeyToString( int i_key )
781 // Worst case appears to be 45 characters:
782 // "Command-Meta-Ctrl-Shift-Alt-Browser Favorites"
784 char *psz_key = malloc( keylen );
795 for( index = 0; index < vlc_num_modifiers; ++index )
797 if( i_key & vlc_modifiers[index].i_key_code )
799 p += snprintf( p, keylen-(psz_key-p), "%s-",
800 vlc_modifiers[index].psz_key_string );
803 for( index = 0; index < vlc_num_keys; ++index )
805 if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
807 p += snprintf( p, keylen-(psz_key-p), "%s",
808 vlc_keys[index].psz_key_string );