Fixing read bug and documenting.

This commit is contained in:
Sandrine Avakian 2016-05-16 16:08:41 +02:00
parent bdee27db9f
commit ae7e1b7b0f
3 changed files with 196 additions and 86 deletions

View file

@ -937,8 +937,7 @@ void lp_config_write_relative_file(const LpConfig *lpconfig, const char *filenam
ms_error("Could not open %s for write", realfilepath);
goto end;
}
fprintf(pFile->file, "%s", data);
bctbx_file_fprintf(pFile, 0, "%s",data);
bctbx_file_close(pFile);
end:

View file

@ -1,28 +1,40 @@
/*
sqlite3_bctbx_vfs.c
Copyright (C) 2016 Belledonne Communications SARL
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "sqlite3_bctbx_vfs.h"
#include <errno.h>
#include <sqlite3.h>
/**
* Closes the file .
* Sets the error errno in the argument pErrSrvd after allocating it
* if an error occurrred.
* @param pFile File handle pointer.
* @param pErrSvd Pointer holding the errno value in case an error occurred.
* @return 0 if successful, -1 otherwise.
* 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){
/*The file descriptor is not dup'ed, and will be closed when the stream created by fdopen() is closed
The fclose() function flushes the stream pointed to by fp (writing any buffered output data using fflush(3))
and closes the underlying file descriptor. */
int ret;
sqlite3_bctbx_file *pFile = (sqlite3_bctbx_file*) p;
ret = close(pFile->bctbx_file.fd);
if (!ret){
// close(pFile->fd);
//free(p);
//p = NULL;
return SQLITE_OK;
}
else{
@ -34,11 +46,11 @@ static int sqlite3bctbx_Close(sqlite3_file *p){
/**
* Read count bytes from the open file given by p, starting at offset.
* Calls bctbx_vfs bcRead : the error type is set in pErrSvd if
* something wrong happened.
* Read count bytes from the open file given by p, starting at offset and puts them in
* the buffer pointed by buf.
* Calls bctbx_file_read.
*
* @param p File handle pointer.
* @param p sqlite3_file file handle pointer.
* @param buf buffer to write the read bytes to.
* @param count number of bytes to read
* @param offset file offset where to start reading
@ -79,9 +91,8 @@ static int sqlite3bctbx_Read(sqlite3_file *p, void *buf, int count, sqlite_int64
/**
* Writes directly to the open file given through the p argument.
* Calls bctbx_vfs bcWrite : the error type is set in pErrSvd if
* something went wrong.
* @param p File handle pointer.
* Calls bctbx_file_write .
* @param p sqlite3_file file handle pointer.
* @param buf Buffer containing data to write
* @param count Size of data to write in bytes
* @param offset File offset where to write to
@ -110,7 +121,7 @@ static int sqlite3bctbx_Write(sqlite3_file *p, const void *buf, int count, sqlit
/**
* Saves the file size associated with the file handle p into the argument pSize.
* @param p File handle pointer.
* @param p sqlite3_file file handle pointer.
* @return SQLITE_OK if read bytes equals count,
* SQLITE_IOERR_FSTAT if the file size returned is negative
* SQLITE_ERROR if an error occurred.
@ -134,10 +145,17 @@ static int sqlite3bctbx_FileSize(sqlite3_file *p, sqlite_int64 *pSize){
}
/************************ PLACE HOLDER FUNCTIONS ***********************/
/** These functions were implemented to please the SQLite VFS
implementation. Some of them are just stubs, some do a very limited job. */
/**
* [sqlite3bctbx_DeviceCharacteristics description]
* @param p [description]
* @return [description]
* Returns the device characteristics for the file. Stub function
* to fill the SQLite VFS function pointer structure .
* @param p sqlite3_file file handle pointer.
* @return value 4096.
*/
static int sqlite3bctbx_DeviceCharacteristics(sqlite3_file *p){
int rc = 0x00001000;
@ -145,11 +163,11 @@ static int sqlite3bctbx_DeviceCharacteristics(sqlite3_file *p){
}
/**
* [sqlite3bctbx_FileControl description]
* @param p File handle pointer.
* @param op [description]
* @param pArg [description]
* @return [description]
* Stub function for information and control over the open file.
* @param p sqlite3_file file handle pointer.
* @param op operation
* @param pArg unused
* @return SQLITE_OK on success, SALITE_NOTFOUND otherwise.
*/
static int sqlite3bctbx_FileControl(sqlite3_file *p, int op, void *pArg){
if (op == SQLITE_FCNTL_MMAP_SIZE) return SQLITE_OK;
@ -158,10 +176,11 @@ static int sqlite3bctbx_FileControl(sqlite3_file *p, int op, void *pArg){
}
/**
* [sqlite3bctbx_nolockCheckReservedLock description]
* @param pUnused [description]
* @param pResOut [description]
* @return [description]
* The lock file mechanism is not used with this VFS : checking
* the reserved lock is always OK.
* @param pUnused sqlite3_file file handle pointer.
* @param pResOut set to 0 since there is no lock mechanism.
* @return SQLITE_OK
*/
static int sqlite3bctbx_nolockCheckReservedLock(sqlite3_file *pUnused, int *pResOut){
*pResOut = 0;
@ -169,20 +188,22 @@ static int sqlite3bctbx_nolockCheckReservedLock(sqlite3_file *pUnused, int *pRes
}
/**
* [sqlite3bctbx_nolockLock description]
* @param pUnused [description]
* @param unused [description]
* @return [description]
* The lock file mechanism is not used with this VFS : locking the file
* is always OK.
* @param pUnused sqlite3_file file handle pointer.
* @param unused unused
* @return SQLITE_OK
*/
static int sqlite3bctbx_nolockLock(sqlite3_file *pUnused, int unused){
return SQLITE_OK;
}
/**
* [sqlite3bctbx_nolockUnlock description]
* @param pUnused [description]
* @param unused [description]
* @return [description]
* The lock file mechanism is not used with this VFS : unlocking the file
* is always OK.
* @param pUnused sqlite3_file file handle pointer.
* @param unused unused
* @return SQLITE_OK
*/
static int sqlite3bctbx_nolockUnlock(sqlite3_file *pUnused, int unused){
return SQLITE_OK;
@ -190,10 +211,11 @@ static int sqlite3bctbx_nolockUnlock(sqlite3_file *pUnused, int unused){
/**
* Simple sync the file contents to the persistent media.
* @param p File handle pointer.
* @param flags [description]
* @return [description]
* Simply sync the file contents given through the file handle p
* to the persistent media.
* @param p sqlite3_file file handle pointer.
* @param flags unused
* @return SQLITE_OK on success, SLITE_IOERR_FSYNC if an error occurred.
*/
static int sqlite3bctbx_Sync(sqlite3_file *p, int flags){
sqlite3_bctbx_file *pFile = (sqlite3_bctbx_file*)p;
@ -204,8 +226,13 @@ static int sqlite3bctbx_Sync(sqlite3_file *p, int flags){
}
/**
* [sqlite3bctbx_Open description]
* Opens the file fName and populates the structure pointed by p
* with the necessary io_methods
* Methods not implemented for version 1 : xTruncate, xSectorSize
*
* @param pVfs [description]
* @param fName [description]
* @param mode [description]
@ -214,44 +241,41 @@ static int sqlite3bctbx_Sync(sqlite3_file *p, int flags){
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,
sqlite3bctbx_Close, /* xClose */
sqlite3bctbx_Read, /* xRead */
sqlite3bctbx_Write, /* xWrite */
0,
sqlite3bctbx_Close, /* xClose */
sqlite3bctbx_Read, /* xRead */
sqlite3bctbx_Write, /* xWrite */
0, /*xTruncate*/
sqlite3bctbx_Sync,
sqlite3bctbx_FileSize, /* xFileSize */
sqlite3bctbx_FileSize,
sqlite3bctbx_nolockLock,
sqlite3bctbx_nolockUnlock,
sqlite3bctbx_nolockCheckReservedLock,
sqlite3bctbx_FileControl,
0,
0, /*xSectorSize*/
sqlite3bctbx_DeviceCharacteristics,
};
sqlite3_bctbx_file * pFile = (sqlite3_bctbx_file*)p;
sqlite3_bctbx_file * pFile = (sqlite3_bctbx_file*)p; /*File handle sqlite3_bctbx_file*/
sqlite_int64 size; /* File size */
int openFlags = 0;
/*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|O_NOFOLLOW);
if( flags&SQLITE_OPEN_CREATE ) openFlags |= O_CREAT;
if( flags&SQLITE_OPEN_READONLY ) openFlags |= O_RDONLY;
if( flags&SQLITE_OPEN_READWRITE ) openFlags |= O_RDWR;
pFile->bctbx_file.fd = open(fName, openFlags, S_IRUSR | S_IWUSR);
if( pFile->bctbx_file.fd < 0 ){
if( pFile->bctbx_file.fd == -1 ){
return SQLITE_CANTOPEN;
}
// pFile->bctbx_file.file = fdopen(pFile->bctbx_file.fd, mode);
// if( pFile->bctbx_file.file == NULL ){
// return SQLITE_CANTOPEN;
// }
if( pOutFlags ){
*pOutFlags = flags;
}
@ -259,35 +283,99 @@ static int sqlite3bctbx_Open(sqlite3_vfs *pVfs, const char *fName, sqlite3_file
pFile->bctbx_file.pMethods = get_bcio();
pFile->bctbx_file.filename = (char*)fName;
pFile->base.pMethods->xFileSize(p, &size);
pFile->bctbx_file.size = size;
return SQLITE_OK;
}
/*
** This function returns a pointer to the VFS implemented in this file.
** To make the VFS available to SQLite:
**
** sqlite3_vfs_register(sqlite3_demovfs(), 0);
*/
/**
* Returns a sqlite3_vfs pointer to the VFS implemented in this file.
* Methods not implemented:
* xDelete
* xAccess
* xFullPathname
* xDlOpen
* xDlError
* xDlSym
* xDlClose
* xRandomness
* xSleep
* xCurrentTime , xCurrentTimeInt64,
* xGetLastError
* xGetSystemCall
* xSetSystemCall
* xNextSystemCall
* To make the VFS available to SQLite
* @return Pointer to bctbx_vfs.
*/
sqlite3_vfs *sqlite3_bctbx_vfs_create(void){
static sqlite3_vfs bctbx_vfs = {
1, /* iVersion */
sizeof(sqlite3_bctbx_file), /* szOsFile */
MAXPATHNAME, /* mxPathname */
0, /* pNext */
"sql3_bctbx_vfs", /* zName */
0, /* pAppData */
sqlite3bctbx_Open, /* xOpen */
0, /* xDelete */
0, /* xAccess */
0, /* xFullPathname */
0, /* xDlOpen */
0, /* xDlError */
0, /* xDlSym */
0, /* xDlClose */
0, /* xRandomness */
0, /* xSleep */
0, /* xCurrentTime */
1, /* iVersion */
sizeof(sqlite3_bctbx_file), /* szOsFile */
MAXPATHNAME, /* mxPathname */
0, /* pNext */
"sqlite3bctbx_vfs", /* zName */
0, /* pAppData */
sqlite3bctbx_Open, /* xOpen */
0, /* xDelete */
0, /* xAccess */
0, /* xFullPathname */
0, /* xDlOpen */
0, /* xDlError */
0, /* xDlSym */
0, /* xDlClose */
0, /* xRandomness */
0, /* xSleep */
0, /* xCurrentTime */
};
return &bctbx_vfs;
}
/**
* Registers sqlite3bctbx_vfs to SQLite VFS. If makeDefault is 1,
* the VFS will be used by default.
* Methods not implemented by sqlite3_bctbx_vfs are initialized to the one
* used by the unix-none VFS where all locking file operations are no-ops.
* @param makeDefault set to 1 to make the newly registered VFS be the default one, set to 0 instead.
*/
void sqlite3_bctbx_vfs_register( int makeDefault){
sqlite3_vfs* pVfsToUse = sqlite3_bctbx_vfs_create();
sqlite3_vfs* pDefault = sqlite3_vfs_find("unix-none");
pVfsToUse->xAccess = pDefault->xAccess;
pVfsToUse->xCurrentTime = pDefault->xCurrentTime;
pVfsToUse->xCurrentTimeInt64 = pDefault->xCurrentTimeInt64;
pVfsToUse->xFullPathname = pDefault->xFullPathname;
pVfsToUse->xDelete = pDefault->xDelete;
pVfsToUse->xSleep = pDefault->xSleep;
pVfsToUse->xRandomness = pDefault->xRandomness;
pVfsToUse->xGetLastError = pDefault->xGetLastError; /* Not implemented by sqlite3 :place holder */
pVfsToUse->xGetSystemCall = pDefault->xGetSystemCall;
pVfsToUse->xSetSystemCall = pDefault->xSetSystemCall;
pVfsToUse->xNextSystemCall = pDefault->xNextSystemCall;
sqlite3_vfs_register(pVfsToUse, makeDefault);
}
/**
* Unregisters sqlite3bctbx_vfs from SQLite.
*/
void sqlite3_bctbx_vfs_unregister(void)
{
sqlite3_vfs* pVfs = sqlite3_vfs_find("sqlite3bctbx_vfs");
sqlite3_vfs_unregister(pVfs);
}

View file

@ -1,3 +1,22 @@
/*
sqlite3_bctbx_vfs.h
Copyright (C) 2016 Belledonne Communications SARL
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
@ -17,6 +36,7 @@
/**
* sqlite3_bctbx_file VFS file structure.
*/
typedef struct sqlite3_bctbx_file sqlite3_bctbx_file;
struct sqlite3_bctbx_file {
@ -27,18 +47,21 @@ struct sqlite3_bctbx_file {
/**
* Very simple VFS structure based on sqlite3_vfs.
* Only the Open function is implemented,
*/
typedef struct sqlite3_bctbx_vfs sqlite3_bctbx_vfs;
struct sqlite3_bctbx_vfs {
sqlite3_bctbx_vfs *pNext; /* Next registered VFS */
const char *vfsName; /* Virtual file system name */
int (*xOpen)(sqlite3_vfs* pVfs, const char *fName, sqlite3_file *pFile,int flags, int *pOutFlags);
// int (*xDelete)(bc_vfs*, const char *vfsName, int syncDir);
// int (*xFullPathname)(bc_vfs*, const char *vfsName, int nOut, char *zOut);
};
/****************************************************
VFS API to register this VFS to sqlite3 VFS
*****************************************************/
sqlite3_vfs *sqlite3_bctbx_vfs_create(void);
//int bctbx_file_read(bc_vfs_file* pFile, void *buf, int count, uint64_t offset);
//int bctbx_file_close(bc_vfs_file* pFile);
//bc_vfs_file* bctbx_file_open(sqlite3_vfs* pVfs, const char *fName, const char* mode);
void sqlite3_bctbx_vfs_register(int makeDefault);
void sqlite3_bctbx_vfs_unregister(void);