]> git.sesse.net Git - vlc/blobdiff - src/config/file.c
Don't try to lock locked mutexes under valgrind
[vlc] / src / config / file.c
index 08848ddb7014bb1f112a90f8e9f41830ee3b2ccd..d6cc24e75a3f8db66c8da7978cfbf903e85f0c10 100644 (file)
@@ -25,7 +25,7 @@
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
 #include "../libvlc.h"
 #include "vlc_charset.h"
 #include "vlc_keys.h"
 #include <errno.h>                                                  /* errno */
 #include <assert.h>
 #include <limits.h>
+#ifdef __APPLE__
+#   include <xlocale.h>
+#else
+#include <locale.h>
+#endif
 
 #include "configuration.h"
 #include "modules/modules.h"
@@ -84,7 +89,7 @@ static FILE *config_OpenConfigFile( vlc_object_t *p_obj, const char *mode )
          * Specification configs */
         char *psz_old;
         if( asprintf( &psz_old, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE,
-                  p_obj->p_libvlc->psz_homedir ) != -1 )
+                      config_GetHomeDir() ) != -1 )
         {
             p_stream = utf8_fopen( psz_old, mode );
             if( p_stream )
@@ -95,7 +100,7 @@ static FILE *config_OpenConfigFile( vlc_object_t *p_obj, const char *mode )
                           "VLC will now use %s.", psz_old, psz_filename );
                 char *psz_readme;
                 if( asprintf(&psz_readme,"%s"DIR_SEP CONFIG_DIR DIR_SEP"README",
-                              p_obj->p_libvlc->psz_homedir ) != -1 )
+                              config_GetHomeDir() ) != -1 )
                 {
                     FILE *p_readme = utf8_fopen( psz_readme, "wt" );
                     if( p_readme )
@@ -153,7 +158,6 @@ static int strtoi (const char *str)
 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
 {
     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
-    vlc_list_t *p_list;
     FILE *file;
 
     file = config_OpenConfigFile (p_this, "rt");
@@ -164,7 +168,7 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
     vlc_mutex_lock( &priv->config_lock );
 
     /* Look for the selected module, if NULL then save everything */
-    p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
+    module_t **list = module_list_get (NULL);
 
     /* Look for UTF-8 Byte Order Mark */
     char * (*convert) (const char *) = strdupnull;
@@ -181,6 +185,10 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
     char line[1024], section[1022];
     section[0] = '\0';
 
+    /* Ensure consistent number formatting... */
+    locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
+    locale_t baseloc = uselocale (loc);
+
     while (fgets (line, 1024, file) != NULL)
     {
         /* Ignore comments and empty lines */
@@ -206,9 +214,9 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
             if ((psz_module_name == NULL)
              || (strcmp (psz_module_name, section) == 0))
             {
-                for (int i = 0; i < p_list->i_count; i++)
+                for (int i = 0; list[i]; i++)
                 {
-                    module_t *m = (module_t *)p_list->p_values[i].p_object;
+                    module_t *m = list[i];
 
                     if ((strcmp (section, m->psz_object_name) == 0)
                      && (m->i_config_items > 0)) /* ignore config-less modules */
@@ -272,7 +280,7 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
                 case CONFIG_ITEM_FLOAT:
                     if( !*psz_option_value )
                         break;                    /* ignore empty option */
-                    p_item->value.f = (float)i18n_atof( psz_option_value);
+                    p_item->value.f = (float)atof (psz_option_value);
                     p_item->saved.f = p_item->value.f;
                     break;
 
@@ -308,7 +316,12 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
     }
     fclose (file);
 
-    vlc_list_release( p_list );
+    module_list_free (list);
+    if (loc != (locale_t)0)
+    {
+        uselocale (baseloc);
+        freelocale (loc);
+    }
 
     vlc_mutex_unlock( &priv->config_lock );
     return 0;
@@ -319,7 +332,7 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
  *****************************************************************************/
 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
 {
-    if( !psz_dirname && !*psz_dirname ) return -1;
+    if( !psz_dirname || !*psz_dirname ) return -1;
 
     if( utf8_mkdir( psz_dirname, 0700 ) == 0 )
         return 0;
@@ -402,7 +415,6 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
 {
     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
     module_t *p_parser;
-    vlc_list_t *p_list;
     FILE *file;
     char p_line[1024], *p_index2;
     int i_sizebuf = 0;
@@ -446,7 +458,7 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
     p_bigbuffer[0] = 0;
 
     /* List all available modules */
-    p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
+    module_t **list = module_list_get (NULL);
 
     /* backup file into memory, we only need to backup the sections we won't
      * save later on */
@@ -457,10 +469,8 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
         {
 
             /* we found a section, check if we need to do a backup */
-            for( i_index = 0; i_index < p_list->i_count; i_index++ )
+            for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ )
             {
-                p_parser = (module_t *)p_list->p_values[i_index].p_object ;
-
                 if( ((p_index2 - &p_line[1])
                        == (int)strlen(p_parser->psz_object_name) )
                     && !memcmp( &p_line[1], p_parser->psz_object_name,
@@ -474,7 +484,7 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
                 }
             }
 
-            if( i_index == p_list->i_count )
+            if( list[i_index] == NULL )
             {
                 /* we don't have this section in our list so we need to back
                  * it up */
@@ -512,7 +522,7 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
     file = config_OpenConfigFile (p_this, "wt");
     if( !file )
     {
-        vlc_list_release( p_list );
+        module_list_free (list);
         free( p_bigbuffer );
         vlc_mutex_unlock( &priv->config_lock );
         return -1;
@@ -521,11 +531,14 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
     fprintf( file, "\xEF\xBB\xBF###\n###  " COPYRIGHT_MESSAGE "\n###\n\n"
        "###\n### lines beginning with a '#' character are comments\n###\n\n" );
 
+    /* Ensure consistent number formatting... */
+    locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
+    locale_t baseloc = uselocale (loc);
+
     /* Look for the selected module, if NULL then save everything */
-    for( i_index = 0; i_index < p_list->i_count; i_index++ )
+    for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ )
     {
         module_config_t *p_item, *p_end;
-        p_parser = (module_t *)p_list->p_values[i_index].p_object ;
 
         if( psz_module_name && strcmp( psz_module_name,
                                        p_parser->psz_object_name ) )
@@ -626,7 +639,12 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
         }
     }
 
-    vlc_list_release( p_list );
+    module_list_free (list);
+    if (loc != (locale_t)0)
+    {
+        uselocale (baseloc);
+        freelocale (loc);
+    }
 
     /*
      * Restore old settings from the config in file
@@ -643,18 +661,17 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
 int config_AutoSaveConfigFile( vlc_object_t *p_this )
 {
     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
-    vlc_list_t *p_list;
-    int i_index, i_count;
+    size_t i_index;
+    bool done;
 
     assert( p_this );
 
     /* Check if there's anything to save */
     vlc_mutex_lock( &priv->config_lock );
-    p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
-    i_count = p_list->i_count;
-    for( i_index = 0; i_index < i_count; i_index++ )
+    module_t **list = module_list_get (NULL);
+    for( i_index = 0; list[i_index]; i_index++ )
     {
-        module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object ;
+        module_t *p_parser = list[i_index];
         module_config_t *p_item, *p_end;
 
         if( !p_parser->i_config_items ) continue;
@@ -667,11 +684,11 @@ int config_AutoSaveConfigFile( vlc_object_t *p_this )
         }
         if( p_item < p_end ) break;
     }
-    vlc_list_release( p_list );
+    done = list[i_index] == NULL;
+    module_list_free (list);
     vlc_mutex_unlock( &priv->config_lock );
 
-    if( i_index == i_count ) return VLC_SUCCESS;
-    return SaveConfigFile( p_this, 0, true );
+    return done ? VLC_SUCCESS : SaveConfigFile( p_this, NULL, true );
 }
 
 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
@@ -691,7 +708,7 @@ char *config_GetCustomConfigFile( libvlc_int_t *p_libvlc )
         {
             /* This is incomplete: we should also support the ~cmassiot/ syntax */
             char *psz_buf;
-            if( asprintf( &psz_buf, "%s/%s", p_libvlc->psz_homedir,
+            if( asprintf( &psz_buf, "%s/%s", config_GetHomeDir(),
                           psz_configfile + 2 ) == -1 )
             {
                 free( psz_configfile );