* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
-#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>
#include <fcntl.h>
+#include <sys/stat.h>
#ifdef __APPLE__
# include <xlocale.h>
#else
#include <locale.h>
#endif
+#include <vlc_common.h>
+#include "../libvlc.h"
+#include "vlc_charset.h"
+#include "vlc_keys.h"
+
#include "configuration.h"
#include "modules/modules.h"
return psz_file;
}
-static FILE *config_OpenConfigFile( vlc_object_t *p_obj, const char *mode )
+static FILE *config_OpenConfigFile( vlc_object_t *p_obj )
{
char *psz_filename = config_GetConfigFile( p_obj );
if( psz_filename == NULL )
msg_Dbg( p_obj, "opening config file (%s)", psz_filename );
- FILE *p_stream = utf8_fopen( psz_filename, mode );
+ FILE *p_stream = utf8_fopen( psz_filename, "rt" );
if( p_stream == NULL && errno != ENOENT )
{
msg_Err( p_obj, "cannot open config file (%s): %m",
}
#if !( defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS) )
- else if( p_stream == NULL && errno == ENOENT && mode[0] == 'r' )
+ else if( p_stream == NULL && errno == ENOENT )
{
/* This is the fallback for pre XDG Base Directory
* Specification configs */
if( asprintf( &psz_old, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE,
config_GetHomeDir() ) != -1 )
{
- p_stream = utf8_fopen( psz_old, mode );
+ p_stream = utf8_fopen( psz_old, "rt" );
if( p_stream )
{
/* Old config file found. We want to write it at the
}
}
#endif
+ free( psz_filename );
return p_stream;
}
*****************************************************************************/
int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
{
- libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
FILE *file;
- file = config_OpenConfigFile (p_this, "rt");
+ file = config_OpenConfigFile (p_this);
if (file == NULL)
return VLC_EGENERIC;
- /* Acquire config file lock */
- vlc_mutex_lock( &priv->config_lock );
-
/* Look for the selected module, if NULL then save everything */
module_t **list = module_list_get (NULL);
uselocale (baseloc);
freelocale (loc);
}
-
- vlc_mutex_unlock( &priv->config_lock );
return 0;
}
static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
bool b_autosave )
{
- libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
module_t *p_parser;
FILE *file = NULL;
char *permanent = NULL, *temporary = NULL;
bool b_backup;
int i_index;
- /* Acquire config file lock */
- vlc_mutex_lock( &priv->config_lock );
-
if( config_PrepareDir( p_this ) )
{
msg_Err( p_this, "no configuration directory" );
goto error;
}
- file = config_OpenConfigFile( p_this, "rt" );
+ file = config_OpenConfigFile( p_this );
if( file != NULL )
{
/* look for file size */
goto error;
}
- if (asprintf (&temporary, "%s.%u", permanent, getpid ()) == -1)
+ if (asprintf (&temporary, "%s.%u", permanent,
+#ifdef UNDER_CE
+ GetCurrentProcessId ()
+#else
+ getpid ()
+#endif
+ ) == -1)
{
temporary = NULL;
module_list_free (list);
goto error;
}
+ /* The temporary configuration file is per-PID. Therefore SaveConfigFile()
+ * should be serialized against itself within a given process. */
+ static vlc_mutex_t lock = VLC_STATIC_MUTEX;
+ vlc_mutex_lock (&lock);
+
int fd = utf8_open (temporary, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
if (fd == -1)
{
+ vlc_mutex_unlock (&lock);
module_list_free (list);
goto error;
}
if (file == NULL)
{
close (fd);
+ vlc_mutex_unlock (&lock);
module_list_free (list);
goto error;
}
#ifndef WIN32
fdatasync (fd); /* Flush from OS */
/* Atomically replace the file... */
- rename (temporary, permanent);
+ utf8_rename (temporary, permanent);
/* (...then synchronize the directory, err, TODO...) */
/* ...and finally close the file */
+ vlc_mutex_unlock (&lock);
#endif
- vlc_mutex_unlock (&priv->config_lock);
fclose (file);
#ifdef WIN32
/* Windows cannot remove open files nor overwrite existing ones */
- remove (permanent);
- rename (temporary, permanent);
+ utf8_unlink (permanent);
+ utf8_rename (temporary, permanent);
+ vlc_mutex_unlock (&lock);
#endif
free (temporary);
error:
if( file )
fclose( file );
- vlc_mutex_unlock( &priv->config_lock );
free (temporary);
free (permanent);
free( p_bigbuffer );
int config_AutoSaveConfigFile( vlc_object_t *p_this )
{
- libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
size_t i_index;
bool save = false;
assert( p_this );
/* Check if there's anything to save */
- vlc_mutex_lock( &priv->config_lock );
module_t **list = module_list_get (NULL);
for( i_index = 0; list[i_index] && !save; i_index++ )
{
}
}
module_list_free (list);
- vlc_mutex_unlock( &priv->config_lock );
return save ? VLC_SUCCESS : SaveConfigFile( p_this, NULL, true );
}
int ConfigStringToKey( const char *psz_key )
{
int i_key = 0;
- unsigned int i;
+ size_t i;
const char *psz_parser = strchr( psz_key, '-' );
while( psz_parser && psz_parser != psz_key )
{
- for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
+ for( i = 0; i < vlc_num_modifiers; ++i )
{
if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
strlen( vlc_modifiers[i].psz_key_string ) ) )
psz_key = psz_parser + 1;
psz_parser = strchr( psz_key, '-' );
}
- for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
+ for( i = 0; i < vlc_num_keys; ++i )
{
if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
{
char *ConfigKeyToString( int i_key )
{
- char *psz_key = malloc( 100 );
+ // Worst case appears to be 45 characters:
+ // "Command-Meta-Ctrl-Shift-Alt-Browser Favorites"
+ enum { keylen=64 };
+ char *psz_key = malloc( keylen );
char *p;
size_t index;
*psz_key = '\0';
p = psz_key;
- for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
- index++ )
+ for( index = 0; index < vlc_num_modifiers; ++index )
{
if( i_key & vlc_modifiers[index].i_key_code )
{
- p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
+ p += snprintf( p, keylen-(psz_key-p), "%s-",
+ vlc_modifiers[index].psz_key_string );
}
}
- for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
- index++)
+ for( index = 0; index < vlc_num_keys; ++index )
{
if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
{
- p += sprintf( p, "%s", vlc_keys[index].psz_key_string );
+ p += snprintf( p, keylen-(psz_key-p), "%s",
+ vlc_keys[index].psz_key_string );
break;
}
}