/**
 * Opens the file fName and populates the structure pointed by p
 * with the necessary io_methods
 * Methods not implemented for version 1 : xTruncate, xSectorSize.
 * Initializes some fields in the p structure, some of which where already
 * initialized by SQLite.
 * @param  pVfs      sqlite3_vfs VFS pointer.
 * @param  fName     filename
 * @param  p         file handle pointer
 * @param  flags     db file access flags
 * @param  pOutFlags flags used by SQLite
 * @return           SQLITE_CANTOPEN on error, SQLITE_OK on success.
 */
static  int sqlite3bctbx_Open(sqlite3_vfs *pVfs, const char *fName, sqlite3_file *p, int flags, int *pOutFlags ){
	static const sqlite3_io_methods sqlite3_bctbx_io = {
		1,										/* iVersion         Structure version number */
		sqlite3bctbx_Close,                 	/* xClose */
		sqlite3bctbx_Read,                  	/* xRead */
		sqlite3bctbx_Write,                 	/* xWrite */
		0,										/*xTruncate*/
		sqlite3bctbx_Sync,
		sqlite3bctbx_FileSize,
		sqlite3bctbx_nolockLock,
		sqlite3bctbx_nolockUnlock,
		sqlite3bctbx_nolockCheckReservedLock,
		sqlite3bctbx_FileControl,
		0,										/*xSectorSize*/
		sqlite3bctbx_DeviceCharacteristics,
	};

	sqlite3_bctbx_file_t * pFile = (sqlite3_bctbx_file_t*)p; /*File handle sqlite3_bctbx_file_t*/
	int openFlags = 0;
	char* wFname;

	/*returns error if filename is empty or file handle not initialized*/
	if (pFile == NULL || fName == NULL){
		return SQLITE_IOERR;
	}
	
	/* Set flags  to open the file with */
	if( flags&SQLITE_OPEN_EXCLUSIVE ) openFlags  |= O_EXCL;
	if( flags&SQLITE_OPEN_CREATE )    openFlags |= O_CREAT;
	if( flags&SQLITE_OPEN_READONLY )  openFlags |= O_RDONLY;
	if( flags&SQLITE_OPEN_READWRITE ) openFlags |= O_RDWR;

#if defined(_WIN32)
	openFlags |= O_BINARY;
#endif
	wFname = ConvertFromUtf8Filename(fName);
	if (wFname != NULL) {
		pFile->pbctbx_file = bctbx_file_open2(bctbx_vfs_get_default(), wFname, openFlags);
		bctbx_free(wFname);
	} else {
		pFile->pbctbx_file = NULL;
	}
	
	if( pFile->pbctbx_file == NULL){
		return SQLITE_CANTOPEN;
	}

	if( pOutFlags ){
    	*pOutFlags = flags;
  	}
	pFile->base.pMethods = &sqlite3_bctbx_io;

	return SQLITE_OK;
}
Beispiel #2
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;
}