int lp_config_sync(LpConfig *lpconfig){ int fd = -1; bctbx_vfs_file_t *pFile = NULL; if (lpconfig->filename==NULL) return -1; if (lpconfig->readonly) return 0; #ifndef _WIN32 /* don't create group/world-accessible files */ (void) umask(S_IRWXG | S_IRWXO); #endif pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,lpconfig->tmpfilename, "w"); lpconfig->pFile = pFile; fd = pFile->fd; if (fd == -1 ){ ms_warning("Could not write %s ! Maybe it is read-only. Configuration will not be saved.",lpconfig->filename); lpconfig->readonly=1; return -1; } ms_list_for_each2(lpconfig->sections,(void (*)(void *,void*))lp_section_write,(void *)lpconfig); bctbx_file_close(pFile); #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)); } lpconfig->modified=0; return 0; }
/** * Closes the file whose file descriptor is stored in the file handle p. * @param p sqlite3_file file handle pointer. * @return SQLITE_OK if successful, SQLITE_IOERR_CLOSE otherwise. */ static int sqlite3bctbx_Close(sqlite3_file *p){ int ret; sqlite3_bctbx_file_t *pFile = (sqlite3_bctbx_file_t*) p; ret = bctbx_file_close(pFile->pbctbx_file); if (!ret){ return SQLITE_OK; } else{ free(pFile); return SQLITE_IOERR_CLOSE ; } }
int lp_config_read_relative_file(const LpConfig *lpconfig, const char *filename, char *data, size_t max_length) { char *dup_config_file = NULL; const char *dir = NULL; char *filepath = NULL; int fd = 0; bctbx_vfs_file_t* pFile = NULL; char* realfilepath = NULL; if (lpconfig->filename == NULL) return -1; dup_config_file = ms_strdup(lpconfig->filename); dir = _lp_config_dirname(dup_config_file); filepath = ms_strdup_printf("%s/%s", dir, filename); realfilepath = lp_realpath(filepath, NULL); if(realfilepath == NULL) { ms_error("Could not resolv %s: %s", filepath, strerror(errno)); goto err; } pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,realfilepath,"r"); if (pFile !=NULL) fd = pFile->fd; if(fd == -1 ) { ms_error("Could not open %s for read.", realfilepath); goto err; } if(bctbx_file_read(pFile, data, 1, max_length) < 0){ ms_error("%s could not be loaded.", realfilepath); goto err; } bctbx_file_close(pFile); ms_free(dup_config_file); ms_free(filepath); ms_free(realfilepath); return 0; err: ms_free(dup_config_file); ms_free(filepath); if(realfilepath) ms_free(realfilepath); return -1; }
int lp_config_read_file(LpConfig *lpconfig, const char *filename){ char* path = lp_realpath(filename, NULL); int fd=-1; bctbx_vfs_file_t* pFile = bctbx_file_open(lpconfig->g_bctbx_vfs, path, "r"); fd = pFile->fd; if (fd != -1){ ms_message("Reading config information from %s", path); lp_config_parse(lpconfig, pFile); bctbx_file_close(pFile); ms_free(path); return 0; } ms_warning("Fail to open file %s",path); ms_free(path); return -1; }
void lp_config_write_relative_file(const LpConfig *lpconfig, const char *filename, const char *data) { char *dup_config_file = NULL; const char *dir = NULL; char *filepath = NULL; char *realfilepath = NULL; int fd = 0; bctbx_vfs_file_t *pFile = lpconfig->pFile; if (lpconfig->filename == NULL) return; if(strlen(data) == 0) { ms_warning("%s has not been created because there is no data to write", filename); return; } dup_config_file = ms_strdup(lpconfig->filename); dir = _lp_config_dirname(dup_config_file); filepath = ms_strdup_printf("%s/%s", dir, filename); realfilepath = lp_realpath(filepath, NULL); if(realfilepath == NULL) { ms_error("Could not resolv %s: %s", filepath, strerror(errno)); goto end; } pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,realfilepath, "w"); fd = pFile->fd; if(fd == -1) { ms_error("Could not open %s for write", realfilepath); goto end; } bctbx_file_fprintf(pFile, 0, "%s",data); bctbx_file_close(pFile); end: ms_free(dup_config_file); ms_free(filepath); if(realfilepath) ms_free(realfilepath); }
bool_t lp_config_relative_file_exists(const LpConfig *lpconfig, const char *filename) { bctbx_vfs_file_t *pFile = lpconfig->pFile; if (lpconfig->filename == NULL) { return FALSE; } else { char *conf_path = ms_strdup(lpconfig->filename); const char *dir = _lp_config_dirname(conf_path); char *filepath = ms_strdup_printf("%s/%s", dir, filename); char *realfilepath = lp_realpath(filepath, NULL); ms_free(conf_path); ms_free(filepath); if(realfilepath == NULL) return FALSE; pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,realfilepath, "r"); ms_free(realfilepath); if (pFile->fd != -1) { bctbx_file_close(pFile); } return pFile->fd > 0; } }
LpConfig *lp_config_new_with_factory(const char *config_filename, const char *factory_config_filename) { int fd; bctbx_vfs_file_t* pFile = NULL; LpConfig *lpconfig=lp_new0(LpConfig,1); lpconfig->g_bctbx_vfs = bctbx_vfs_get_default(); lpconfig->refcnt=1; if (config_filename!=NULL){ if(ortp_file_exist(config_filename) == 0) { lpconfig->filename=lp_realpath(config_filename, NULL); if(lpconfig->filename == NULL) { ms_error("Could not find the real path of %s: %s", config_filename, strerror(errno)); goto fail; } } else { lpconfig->filename = ms_strdup(config_filename); } lpconfig->tmpfilename=ortp_strdup_printf("%s.tmp",lpconfig->filename); ms_message("Using (r/w) config information from %s", lpconfig->filename); #if !defined(_WIN32) { struct stat fileStat; if ((stat(lpconfig->filename,&fileStat) == 0) && (S_ISREG(fileStat.st_mode))) { /* make existing configuration files non-group/world-accessible */ if (chmod(lpconfig->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*/ pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,lpconfig->filename, "r+"); fd = pFile->fd; lpconfig->pFile = pFile; #ifdef RENAME_REQUIRES_NONEXISTENT_NEW_PATH if (fd == -1){ pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,lpconfig->tmpfilename, "r+"); if (fd){ ms_warning("Could not open %s but %s works, app may have crashed during last sync.",lpconfig->filename,lpconfig->tmpfilename); } } #endif if (fd != -1){ lp_config_parse(lpconfig, pFile); bctbx_file_close(pFile); lpconfig->pFile = NULL; lpconfig->modified=0; } } if (factory_config_filename != NULL) { lp_config_read_file(lpconfig, factory_config_filename); } return lpconfig; fail: ms_free(lpconfig); return NULL; }