示例#1
0
int lp_config_read_relative_file(const LpConfig *lpconfig, const char *filename, char *data, size_t max_length) {
	char *dir;
	char *filepath;
	FILE *file;
	char* realfilepath;
	if (lpconfig->filename == NULL) return -1;
	dir = _lp_config_dirname(lpconfig->filename);
	filepath = ms_strdup_printf("%s/%s", dir, filename);
	realfilepath = lp_realpath(filepath, NULL);
	file = fopen(realfilepath, "r");
	if(file != NULL) {
		if(fread(data, 1, max_length, file)<=0) {
			ms_error("%s could not be loaded. %s", realfilepath, strerror(errno));
			goto err;
		}
		fclose(file);
	} else {
		ms_error("Could not open %s for read. %s", realfilepath, strerror(errno));
		goto err;
	}
	ms_free(dir);
	ms_free(filepath);
	ms_free(realfilepath);
	return 0;

err:
	ms_free(dir);
	ms_free(filepath);
	ms_free(realfilepath);
	return -1;
}
示例#2
0
LpConfig *lp_config_new_with_factory(const char *config_filename, const char *factory_config_filename) {
	LpConfig *lpconfig=lp_new0(LpConfig,1);
	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*/
		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);

			lpconfig->file=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;
}
示例#3
0
int lp_config_read_file(LpConfig *lpconfig, const char *filename){
	char* path = lp_realpath(filename, NULL);
	FILE* f=fopen(path,"r");
	if (f!=NULL){
		ms_message("Reading config information from %s", path);
		lp_config_parse(lpconfig,f);
		fclose(f);
		return 0;
	}
	ms_warning("Fail to open file %s",path);
	ms_free(path);
	return -1;
}
示例#4
0
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;
}
示例#5
0
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;
}
示例#6
0
bool_t lp_config_relative_file_exists(const LpConfig *lpconfig, const char *filename) {
	if (lpconfig->filename == NULL) {
		return FALSE;
	} else {
		char *dir = _lp_config_dirname(lpconfig->filename);
		char *filepath = ms_strdup_printf("%s/%s", dir, filename);
		char *realfilepath = lp_realpath(filepath, NULL);
		FILE *file = fopen(realfilepath, "r");
		ms_free(dir);
		ms_free(filepath);
		ms_free(realfilepath);
		if (file) {
			fclose(file);
		}
		return file != NULL;
	}
}
示例#7
0
void lp_config_write_relative_file(const LpConfig *lpconfig, const char *filename, const char *data) {
	if (lpconfig->filename == NULL) return;
	if(strlen(data) > 0) {
		char *dir = _lp_config_dirname(lpconfig->filename);
		char *filepath = ms_strdup_printf("%s/%s", dir, filename);
		char *realfilepath = lp_realpath(filepath, NULL);
		FILE *file = fopen(realfilepath, "w");
		if(file != NULL) {
			fprintf(file, "%s", data);
			fclose(file);
		} else {
			ms_error("Could not open %s for write", realfilepath);
		}
		ms_free(dir);
		ms_free(filepath);
		ms_free(realfilepath);
	} else {
		ms_warning("%s has not been created because there is no data to write", filename);
	}
}
示例#8
0
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);
}
示例#9
0
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;
	}
}