1 /*****************************************************************************
2 * file.c: configuration file handling
3 *****************************************************************************
4 * Copyright (C) 2001-2007 VLC authors and VideoLAN
6 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
27 #include <errno.h> /* errno */
34 #elif defined(HAVE_USELOCALE)
39 #include <vlc_common.h>
40 #include "../libvlc.h"
41 #include <vlc_charset.h>
43 #include <vlc_actions.h>
44 #include <vlc_modules.h>
45 #include <vlc_plugin.h>
47 #include "configuration.h"
48 #include "modules/modules.h"
50 static inline char *strdupnull (const char *src
)
52 return src
? strdup (src
) : NULL
;
56 * Get the user's configuration file
58 static char *config_GetConfigFile( vlc_object_t
*obj
)
60 char *psz_file
= var_CreateGetNonEmptyString( obj
, "config" );
61 var_Destroy( obj
, "config" );
62 if( psz_file
== NULL
)
64 char *psz_dir
= config_GetUserDir( VLC_CONFIG_DIR
);
66 if( asprintf( &psz_file
, "%s" DIR_SEP CONFIG_FILE
, psz_dir
) == -1 )
73 static FILE *config_OpenConfigFile( vlc_object_t
*p_obj
)
75 char *psz_filename
= config_GetConfigFile( p_obj
);
76 if( psz_filename
== NULL
)
79 msg_Dbg( p_obj
, "opening config file (%s)", psz_filename
);
81 FILE *p_stream
= vlc_fopen( psz_filename
, "rt" );
82 if( p_stream
== NULL
&& errno
!= ENOENT
)
84 msg_Err( p_obj
, "cannot open config file (%s): %s",
85 psz_filename
, vlc_strerror_c(errno
) );
88 #if !( defined(_WIN32) || defined(__APPLE__) || defined(__OS2__) )
89 else if( p_stream
== NULL
&& errno
== ENOENT
)
91 /* This is the fallback for pre XDG Base Directory
92 * Specification configs */
93 char *home
= config_GetUserDir(VLC_HOME_DIR
);
97 && asprintf( &psz_old
, "%s/.vlc/" CONFIG_FILE
,
100 p_stream
= vlc_fopen( psz_old
, "rt" );
103 /* Old config file found. We want to write it at the
104 * new location now. */
105 msg_Info( p_obj
, "Found old config file at %s. "
106 "VLC will now use %s.", psz_old
, psz_filename
);
108 if( asprintf(&psz_readme
,"%s/.vlc/README",
111 FILE *p_readme
= vlc_fopen( psz_readme
, "wt" );
114 fprintf( p_readme
, "The VLC media player "
115 "configuration folder has moved to comply\n"
116 "with the XDG Base Directory Specification "
117 "version 0.6. Your\nconfiguration has been "
118 "copied to the new location:\n%s\nYou can "
119 "delete this directory and all its contents.",
125 /* Remove the old configuration file so that --reset-config
126 * can work properly. Fortunately, Linux allows removing
127 * open files - with most filesystems. */
135 free( psz_filename
);
139 static int64_t vlc_strtoi (const char *str
)
145 l
= strtoll (str
, &end
, 0);
149 #if (LLONG_MAX > 0x7fffffffffffffffLL)
150 if (l
> 0x7fffffffffffffffLL
151 || l
< -0x8000000000000000LL
)
160 #undef config_LoadConfigFile
161 /*****************************************************************************
162 * config_LoadConfigFile: loads the configuration file.
163 *****************************************************************************
164 * This function is called to load the config options stored in the config
166 *****************************************************************************/
167 int config_LoadConfigFile( vlc_object_t
*p_this
)
171 file
= config_OpenConfigFile (p_this
);
175 /* Skip UTF-8 Byte Order Mark if present */
177 if (fread (bom
, 1, 3, file
) != 3 || memcmp (bom
, "\xEF\xBB\xBF", 3))
178 rewind (file
); /* no BOM, rewind */
184 /* Ensure consistent number formatting... */
185 locale_t loc
= newlocale (LC_NUMERIC_MASK
, "C", NULL
);
186 locale_t baseloc
= uselocale (loc
);
188 vlc_rwlock_wrlock (&config_lock
);
189 while ((linelen
= getline (&line
, &bufsize
, file
)) != -1)
191 line
[linelen
- 1] = '\0'; /* trim newline */
193 /* Ignore comments, section and empty lines */
194 if (memchr ("#[", line
[0], 3) != NULL
)
197 /* look for option name */
198 const char *psz_option_name
= line
;
200 char *ptr
= strchr (line
, '=');
202 continue; /* syntax error */
205 module_config_t
*item
= config_FindConfig(psz_option_name
);
209 /* Reject values of options that are unsaveable */
210 if (item
->b_unsaveable
)
212 /* Ignore options that are obsolete */
216 const char *psz_option_value
= ptr
+ 1;
217 switch (CONFIG_CLASS(item
->i_type
))
219 case CONFIG_ITEM_BOOL
:
220 case CONFIG_ITEM_INTEGER
:
225 l
= vlc_strtoi (psz_option_value
);
226 if ((l
> item
->max
.i
) || (l
< item
->min
.i
))
229 msg_Warn (p_this
, "Integer value (%s) for %s: %s",
230 psz_option_value
, psz_option_name
,
231 vlc_strerror_c(errno
));
237 case CONFIG_ITEM_FLOAT
:
238 if (!*psz_option_value
)
239 break; /* ignore empty option */
240 item
->value
.f
= (float)atof (psz_option_value
);
244 free (item
->value
.psz
);
245 item
->value
.psz
= strdupnull (psz_option_value
);
249 vlc_rwlock_unlock (&config_lock
);
254 msg_Err (p_this
, "error reading configuration: %s",
255 vlc_strerror_c(errno
));
260 if (loc
!= (locale_t
)0)
268 /*****************************************************************************
269 * config_CreateDir: Create configuration directory if it doesn't exist.
270 *****************************************************************************/
271 int config_CreateDir( vlc_object_t
*p_this
, const char *psz_dirname
)
273 if( !psz_dirname
|| !*psz_dirname
) return -1;
275 if( vlc_mkdir( psz_dirname
, 0700 ) == 0 )
285 /* Let's try to create the parent directory */
286 char psz_parent
[strlen( psz_dirname
) + 1], *psz_end
;
287 strcpy( psz_parent
, psz_dirname
);
289 psz_end
= strrchr( psz_parent
, DIR_SEP_CHAR
);
290 if( psz_end
&& psz_end
!= psz_parent
)
293 if( config_CreateDir( p_this
, psz_parent
) == 0 )
295 if( !vlc_mkdir( psz_dirname
, 0700 ) )
302 msg_Warn( p_this
, "could not create %s: %s", psz_dirname
,
303 vlc_strerror_c(errno
) );
307 VLC_FORMAT(6, 7) static int
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: %s",
403 vlc_strerror_c(errno
));
404 vlc_rwlock_unlock (&config_lock
);
406 vlc_mutex_unlock (&lock
);
412 "### "PACKAGE_NAME
" "PACKAGE_VERSION
"\n"
416 "### lines beginning with a '#' character are comments\n"
420 /* Ensure consistent number formatting... */
421 locale_t loc
= newlocale (LC_NUMERIC_MASK
, "C", NULL
);
422 locale_t baseloc
= uselocale (loc
);
424 /* We would take the config lock here. But this would cause a lock
425 * inversion with the serializer above and config_AutoSaveConfigFile().
426 vlc_rwlock_rdlock (&config_lock);*/
428 /* Look for the selected module, if NULL then save everything */
429 for (vlc_plugin_t
*p
= vlc_plugins
; p
!= NULL
; p
= p
->next
)
431 module_t
*p_parser
= p
->module
;
432 module_config_t
*p_item
, *p_end
;
434 if (p
->conf
.count
== 0)
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
->conf
.items
, p_end
= p_item
+ p
->conf
.size
;
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 if (loc
!= (locale_t
)0)
493 * Flush to disk and replace atomically
495 fflush (file
); /* Flush from run-time */
498 vlc_unlink (temporary
);
499 vlc_mutex_unlock (&lock
);
500 msg_Err (p_this
, "cannot write configuration file");
504 fdatasync (fd
); /* Flush from OS */
505 #if defined (_WIN32) || defined (__OS2__)
506 /* Windows cannot (re)move open files nor overwrite existing ones */
508 vlc_unlink (permanent
);
510 /* Atomically replace the file... */
511 if (vlc_rename (temporary
, permanent
))
512 vlc_unlink (temporary
);
513 /* (...then synchronize the directory, err, TODO...) */
514 /* ...and finally close the file */
515 vlc_mutex_unlock (&lock
);
516 #if !defined (_WIN32) && !defined (__OS2__)
530 int config_AutoSaveConfigFile( vlc_object_t
*p_this
)
536 vlc_rwlock_rdlock (&config_lock
);
539 /* Note: this will get the read lock recursively. Ok. */
540 ret
= config_SaveConfigFile (p_this
);
541 config_dirty
= (ret
!= 0);
543 vlc_rwlock_unlock (&config_lock
);