From d184192b9f6c4000f1cf780b446c97dac8697844 Mon Sep 17 00:00:00 2001 From: Simon Morlat Date: Wed, 15 Oct 2014 15:08:29 +0200 Subject: [PATCH] fix linphonerc renaming for windows --- coreapi/lpconfig.c | 49 +++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/coreapi/lpconfig.c b/coreapi/lpconfig.c index 5c556acc7..a940fb696 100644 --- a/coreapi/lpconfig.c +++ b/coreapi/lpconfig.c @@ -49,7 +49,9 @@ #include #endif - +#ifdef WIN32 +#define RENAME_REQUIRES_NONEXISTENT_NEW_PATH 1 +#endif #define lp_new0(type,n) (type*)calloc(sizeof(type),n) @@ -351,22 +353,33 @@ LpConfig *lp_config_new_with_factory(const char *config_filename, const char *fa ms_message("Using (r/w) config information from %s", config_filename); lpconfig->filename=ortp_strdup(config_filename); lpconfig->tmpfilename=ortp_strdup_printf("%s.tmp",config_filename); - lpconfig->file=fopen(config_filename,"r+"); + +#if !defined(WIN32) + { + struct stat fileStat; + if ((stat(config_filename,&fileStat) == 0) && (S_ISREG(fileStat.st_mode))) { + /* make existing configuration files non-group/world-accessible */ + if (chmod(config_filename, S_IRUSR | S_IWUSR) == -1) { + ms_warning("unable to correct permissions on " + "configuration file: %s", strerror(errno)); + } + } + } +#endif /*WIN32*/ + /*open with r+ to check if we can write on it later*/ + lpconfig->file=fopen(lpconfig->filename,"r+"); +#ifdef RENAME_REQUIRES_NONEXISTENT_NEW_PATH + if (lpconfig->file==NULL){ + lpconfig->file=fopen(lpconfig->tmpfilename,"r+"); + if (lpconfig->file){ + ms_warning("Could not open %s but %s works, app may have crashed during last sync.",lpconfig->filename,lpconfig->tmpfilename); + } + } +#endif if (lpconfig->file!=NULL){ lp_config_parse(lpconfig,lpconfig->file); fclose(lpconfig->file); -#if !defined(WIN32) - { - struct stat fileStat; - if ((stat(config_filename,&fileStat) == 0) && (S_ISREG(fileStat.st_mode))) { - /* make existing configuration files non-group/world-accessible */ - if (chmod(config_filename, S_IRUSR | S_IWUSR) == -1) { - ms_warning("unable to correct permissions on " - "configuration file: %s", strerror(errno)); - } - } - } -#endif /*WIN32*/ + lpconfig->file=NULL; lpconfig->modified=0; } @@ -398,6 +411,7 @@ void lp_item_set_value(LpItem *item, const char *value){ static void _lp_config_destroy(LpConfig *lpconfig){ if (lpconfig->filename!=NULL) ortp_free(lpconfig->filename); + if (lpconfig->tmpfilename) ortp_free(lpconfig->tmpfilename); ms_list_for_each(lpconfig->sections,(void (*)(void*))lp_section_destroy); ms_list_free(lpconfig->sections); free(lpconfig); @@ -593,6 +607,13 @@ int lp_config_sync(LpConfig *lpconfig){ } ms_list_for_each2(lpconfig->sections,(void (*)(void *,void*))lp_section_write,(void *)file); fclose(file); +#ifdef RENAME_REQUIRES_NONEXISTENT_NEW_PATH + /* On windows, rename() does not accept that the newpath is an existing file, while it is accepted on Unix. + * As a result, we are forced to first delete the linphonerc file, and then rename.*/ + if (remove(lpconfig->filename)!=0){ + ms_error("Cannot remove %s: %s",lpconfig->filename, strerror(errno)); + } +#endif if (rename(lpconfig->tmpfilename,lpconfig->filename)!=0){ ms_error("Cannot rename %s into %s: %s",lpconfig->tmpfilename,lpconfig->filename,strerror(errno)); }