gui: macos: use float for rate
[vlc.git] / src / config / file.c
blobe6b10e5c028ebd9724afd43aaa16feb3b56296b8
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 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include <errno.h> /* errno */
28 #include <assert.h>
29 #include <limits.h>
30 #include <fcntl.h>
31 #include <sys/stat.h>
32 #ifdef __APPLE__
33 # include <xlocale.h>
34 #elif defined(HAVE_USELOCALE)
35 #include <locale.h>
36 #endif
37 #include <unistd.h>
39 #include <vlc_common.h>
40 #include "../libvlc.h"
41 #include <vlc_charset.h>
42 #include <vlc_fs.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;
55 /**
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 )
67 psz_file = NULL;
68 free( psz_dir );
70 return psz_file;
73 static FILE *config_OpenConfigFile( vlc_object_t *p_obj )
75 char *psz_filename = config_GetConfigFile( p_obj );
76 if( psz_filename == NULL )
77 return 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);
94 char *psz_old;
96 if( home != NULL
97 && asprintf( &psz_old, "%s/.vlc/" CONFIG_FILE,
98 home ) != -1 )
100 p_stream = vlc_fopen( psz_old, "rt" );
101 if( p_stream )
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 );
107 char *psz_readme;
108 if( asprintf(&psz_readme,"%s/.vlc/README",
109 home ) != -1 )
111 FILE *p_readme = vlc_fopen( psz_readme, "wt" );
112 if( p_readme )
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.",
120 psz_filename);
121 fclose( p_readme );
123 free( psz_readme );
125 /* Remove the old configuration file so that --reset-config
126 * can work properly. Fortunately, Linux allows removing
127 * open files - with most filesystems. */
128 unlink( psz_old );
130 free( psz_old );
132 free( home );
134 #endif
135 free( psz_filename );
136 return p_stream;
139 static int64_t vlc_strtoi (const char *str)
141 char *end;
142 long long l;
144 errno = 0;
145 l = strtoll (str, &end, 0);
147 if (!errno)
149 #if (LLONG_MAX > 0x7fffffffffffffffLL)
150 if (l > 0x7fffffffffffffffLL
151 || l < -0x8000000000000000LL)
152 errno = ERANGE;
153 #endif
154 if (*end)
155 errno = EINVAL;
157 return l;
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
165 * file.
166 *****************************************************************************/
167 int config_LoadConfigFile( vlc_object_t *p_this )
169 FILE *file;
171 file = config_OpenConfigFile (p_this);
172 if (file == NULL)
173 return VLC_EGENERIC;
175 /* Skip UTF-8 Byte Order Mark if present */
176 char bom[3];
177 if (fread (bom, 1, 3, file) != 3 || memcmp (bom, "\xEF\xBB\xBF", 3))
178 rewind (file); /* no BOM, rewind */
180 char *line = NULL;
181 size_t bufsize;
182 ssize_t linelen;
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)
195 continue;
197 /* look for option name */
198 const char *psz_option_name = line;
200 char *ptr = strchr (line, '=');
201 if (ptr == NULL)
202 continue; /* syntax error */
203 *ptr = '\0';
205 module_config_t *item = config_FindConfig(psz_option_name);
206 if (item == NULL)
207 continue;
209 /* Reject values of options that are unsaveable */
210 if (item->b_unsaveable)
211 continue;
212 /* Ignore options that are obsolete */
213 if (item->b_removed)
214 continue;
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:
222 int64_t l;
224 errno = 0;
225 l = vlc_strtoi (psz_option_value);
226 if ((l > item->max.i) || (l < item->min.i))
227 errno = ERANGE;
228 if (errno)
229 msg_Warn (p_this, "Integer value (%s) for %s: %s",
230 psz_option_value, psz_option_name,
231 vlc_strerror_c(errno));
232 else
233 item->value.i = l;
234 break;
237 case CONFIG_ITEM_FLOAT:
238 if (!*psz_option_value)
239 break; /* ignore empty option */
240 item->value.f = (float)atof (psz_option_value);
241 break;
243 default:
244 free (item->value.psz);
245 item->value.psz = strdupnull (psz_option_value);
246 break;
249 vlc_rwlock_unlock (&config_lock);
250 free (line);
252 if (ferror (file))
254 msg_Err (p_this, "error reading configuration: %s",
255 vlc_strerror_c(errno));
256 clearerr (file);
258 fclose (file);
260 if (loc != (locale_t)0)
262 uselocale (baseloc);
263 freelocale (loc);
265 return 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 )
276 return 0;
278 switch( errno )
280 case EEXIST:
281 return 0;
283 case ENOENT:
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 )
292 *psz_end = '\0';
293 if( config_CreateDir( p_this, psz_parent ) == 0 )
295 if( !vlc_mkdir( psz_dirname, 0700 ) )
296 return 0;
302 msg_Warn( p_this, "could not create %s: %s", psz_dirname,
303 vlc_strerror_c(errno) );
304 return -1;
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, ...)
311 va_list ap;
312 int ret;
314 if (desc == NULL)
315 desc = "?";
317 if (fprintf (file, "# %s (%s)\n%s%s=", desc, vlc_gettext (type),
318 comment ? "#" : "", name) < 0)
319 return -1;
321 va_start (ap, fmt);
322 ret = vfprintf (file, fmt, ap);
323 va_end (ap);
324 if (ret < 0)
325 return -1;
327 if (fputs ("\n\n", file) == EOF)
328 return -1;
329 return 0;
333 static int config_PrepareDir (vlc_object_t *obj)
335 char *psz_configdir = config_GetUserDir (VLC_CONFIG_DIR);
336 if (psz_configdir == NULL)
337 return -1;
339 int ret = config_CreateDir (obj, psz_configdir);
340 free (psz_configdir);
341 return ret;
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" );
355 return -1;
359 * Save module config in file
361 char *temporary;
362 char *permanent = config_GetConfigFile (p_this);
363 if (permanent == NULL)
364 return -1;
365 if (asprintf (&temporary, "%s.%u", permanent, getpid ()) == -1)
367 free (permanent);
368 return -1;
370 else
372 struct stat st;
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");
380 goto error;
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);
393 if (fd == -1)
395 vlc_rwlock_unlock (&config_lock);
396 vlc_mutex_unlock (&lock);
397 goto error;
399 FILE *file = fdopen (fd, "wt");
400 if (file == NULL)
402 msg_Err (p_this, "cannot create configuration file: %s",
403 vlc_strerror_c(errno));
404 vlc_rwlock_unlock (&config_lock);
405 vlc_close (fd);
406 vlc_mutex_unlock (&lock);
407 goto error;
410 fprintf( file,
411 "\xEF\xBB\xBF###\n"
412 "### "PACKAGE_NAME" "PACKAGE_VERSION"\n"
413 "###\n"
414 "\n"
415 "###\n"
416 "### lines beginning with a '#' character are comments\n"
417 "###\n"
418 "\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)
435 continue;
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 );
440 else
441 fprintf( file, "\n\n" );
443 for (p_item = p->conf.items, p_end = p_item + p->conf.size;
444 p_item < p_end;
445 p_item++)
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 */
450 continue;
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);
461 else
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);
469 else
471 const char *psz_value = p_item->value.psz;
472 bool modified;
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)
488 uselocale (baseloc);
489 freelocale (loc);
493 * Flush to disk and replace atomically
495 fflush (file); /* Flush from run-time */
496 if (ferror (file))
498 vlc_unlink (temporary);
499 vlc_mutex_unlock (&lock);
500 msg_Err (p_this, "cannot write configuration file");
501 fclose (file);
502 goto error;
504 fdatasync (fd); /* Flush from OS */
505 #if defined (_WIN32) || defined (__OS2__)
506 /* Windows cannot (re)move open files nor overwrite existing ones */
507 fclose (file);
508 vlc_unlink (permanent);
509 #endif
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__)
517 fclose (file);
518 #endif
520 free (temporary);
521 free (permanent);
522 return 0;
524 error:
525 free (temporary);
526 free (permanent);
527 return -1;
530 int config_AutoSaveConfigFile( vlc_object_t *p_this )
532 int ret = 0;
534 assert( p_this );
536 vlc_rwlock_rdlock (&config_lock);
537 if (config_dirty)
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);
545 return ret;