sqlite3_bctbx_vfs.c 12.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
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
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 19
*/

20
#ifdef SQLITE_STORAGE_ENABLED
21
#include "private.h"
22

23
#include "bctoolbox/charconv.h"
24
#include "sqlite3_bctbx_vfs.h"
25 26
#include <sqlite3.h>

Sandrine Avakian's avatar
Sandrine Avakian committed
27 28 29 30 31
#ifndef _WIN32_WCE
#include <errno.h>
#endif /*_WIN32_WCE*/


32
/**
33 34 35
 * 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.
36 37 38 39
 */
static int sqlite3bctbx_Close(sqlite3_file *p){

	int ret;
40
	sqlite3_bctbx_file_t *pFile = (sqlite3_bctbx_file_t*) p;
41

42
	ret = bctbx_file_close(pFile->pbctbx_file);
43
	if (!ret){
44
		return SQLITE_OK;
45 46 47 48 49 50 51 52 53
	}
	else{
		free(pFile);
		return SQLITE_IOERR_CLOSE ;
	}
}


/**
54
 * Read count bytes from the open file given by p, starting at offset and puts them in
55 56
 * the buffer pointed by buf.
 * Calls bctbx_file_read.
57
 *
58
 * @param  p  		sqlite3_file file handle pointer.
59 60 61
 * @param  buf    	buffer to write the read bytes to.
 * @param  count  	number of bytes to read
 * @param  offset 	file offset where to start reading
62
 * @return 			SQLITE_OK if read bytes equals count,
63
 *                  SQLITE_IOERR_SHORT_READ if the number of bytes read is inferior to count
64
 *                  SQLITE_IOERR_READ if an error occurred.
65 66 67
 */
static int sqlite3bctbx_Read(sqlite3_file *p, void *buf, int count, sqlite_int64 offset){
	int ret;
68
	sqlite3_bctbx_file_t *pFile = (sqlite3_bctbx_file_t*) p;
69
	if (pFile){
Benjamin REIS's avatar
Benjamin REIS committed
70
		ret = (int)bctbx_file_read(pFile->pbctbx_file, buf, (size_t)count, (off_t)offset);
71 72 73 74
		if( ret==count ){
			return SQLITE_OK;
		}
		else if( ret >= 0 ){
75
			/*fill in unread portion of buffer, as requested by sqlite3 documentation*/
Benjamin REIS's avatar
Benjamin REIS committed
76
			memset(((uint8_t*)buf) + ret, 0, (size_t)(count-ret));
77
			return SQLITE_IOERR_SHORT_READ;
78
		}else {
79

80 81 82 83 84 85 86
			return SQLITE_IOERR_READ;
		}
	}
	return SQLITE_IOERR_READ;
}

/**
87
 * Writes directly to the open file given through the p argument.
88 89
 * Calls bctbx_file_write .
 * @param  p       sqlite3_file file handle pointer.
90 91 92
 * @param  buf     Buffer containing data to write
 * @param  count   Size of data to write in bytes
 * @param  offset  File offset where to write to
93
 * @return         SQLITE_OK on success, SQLITE_IOERR_WRITE if an error occurred.
94 95
 */
static int sqlite3bctbx_Write(sqlite3_file *p, const void *buf, int count, sqlite_int64 offset){
96
	sqlite3_bctbx_file_t *pFile = (sqlite3_bctbx_file_t*) p;
97 98
	int ret;
	if (pFile ){
Benjamin REIS's avatar
Benjamin REIS committed
99
		ret = (int)bctbx_file_write(pFile->pbctbx_file, buf, (size_t)count, (off_t)offset);
100 101 102 103 104 105 106
		if(ret > 0 ) return SQLITE_OK;
		else {
			return SQLITE_IOERR_WRITE;
		}
	}
	return SQLITE_IOERR_WRITE;
}
107

108 109 110 111
/**
 * TRuncates or extends a file depending on the size provided.
 * @param  p    sqlite3_file file handle pointer.
 * @param  size New file size.
112 113
 * @return  SQLITE_OK on success, SQLITE_IOERR_TRUNCATE if an error occurred during truncate,
 *          SQLITE_ERROR if ther was a problem on the file descriptor.
114 115
 */
static int sqlite3bctbx_Truncate(sqlite3_file *p, sqlite_int64 size){
116
	int rc;
117 118 119 120 121 122 123 124 125 126 127 128 129
	sqlite3_bctbx_file_t *pFile = (sqlite3_bctbx_file_t*) p;
	if (pFile->pbctbx_file){
		rc = bctbx_file_truncate(pFile->pbctbx_file, size);
		if( rc < 0 ) {
			return SQLITE_IOERR_TRUNCATE;
		}
		if (rc == 0){
			return SQLITE_OK;
		}
	}
	return SQLITE_ERROR;

}
130 131

/**
132
 * Saves the file size associated with the file handle p into the argument pSize.
133
 * @param  p 	sqlite3_file file handle pointer.
134 135 136
 * @return 		SQLITE_OK if read bytes equals count,
 *              SQLITE_IOERR_FSTAT if the file size returned is negative
 *              SQLITE_ERROR if an error occurred.
137 138 139
 */
static int sqlite3bctbx_FileSize(sqlite3_file *p, sqlite_int64 *pSize){

140
	int64_t rc;                         /* Return code from fstat() call */
141
	sqlite3_bctbx_file_t *pFile = (sqlite3_bctbx_file_t*) p;
142 143
	if (pFile->pbctbx_file){
		rc = bctbx_file_size(pFile->pbctbx_file);
144 145 146 147 148 149
		if( rc < 0 ) {
			return SQLITE_IOERR_FSTAT;
		}
		if (pSize){
			*pSize = rc;
			return SQLITE_OK;
150
		}
151 152 153 154 155 156
	}
	return SQLITE_ERROR;


}

157 158

/************************ PLACE HOLDER FUNCTIONS ***********************/
159
/** These functions were implemented to please the SQLite VFS
160 161 162
implementation. Some of them are just stubs, some do a very limited job. */


163
/**
164 165 166 167
 * 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.
168 169 170 171 172 173 174
 */
static int sqlite3bctbx_DeviceCharacteristics(sqlite3_file *p){
	int rc = 0x00001000;
	return rc;
}

/**
175 176 177 178 179
 * 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.
180 181
 */
static int sqlite3bctbx_FileControl(sqlite3_file *p, int op, void *pArg){
Simon Morlat's avatar
Simon Morlat committed
182
#ifdef SQLITE_FCNTL_MMAP_SIZE
183
	if (op == SQLITE_FCNTL_MMAP_SIZE) return SQLITE_OK;
Simon Morlat's avatar
Simon Morlat committed
184
#endif
185 186 187 188 189
	return SQLITE_NOTFOUND;

}

/**
190 191 192 193 194
 * 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
195 196 197 198 199 200 201
 */
static int sqlite3bctbx_nolockCheckReservedLock(sqlite3_file *pUnused, int *pResOut){
	*pResOut = 0;
	return SQLITE_OK;
}

/**
202
 * The lock file mechanism is not used with this VFS : locking the file
203 204 205 206
 * is always OK.
 * @param  pUnused sqlite3_file file handle pointer.
 * @param  unused  unused
 * @return         SQLITE_OK
207 208 209 210 211 212
 */
static int sqlite3bctbx_nolockLock(sqlite3_file *pUnused, int unused){
	return SQLITE_OK;
}

/**
213 214 215 216 217
 * 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
218 219 220 221 222 223
 */
static int sqlite3bctbx_nolockUnlock(sqlite3_file *pUnused, int unused){
	return SQLITE_OK;
}


224
/**
225 226 227 228 229
 * 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.
230 231
 */
static int sqlite3bctbx_Sync(sqlite3_file *p, int flags){
232
	sqlite3_bctbx_file_t *pFile = (sqlite3_bctbx_file_t*)p;
233
#if _WIN32
234
	int ret;
235
	ret = FlushFileBuffers((HANDLE)_get_osfhandle(pFile->pbctbx_file->fd));
236
	return (ret!=0 ? SQLITE_OK : SQLITE_IOERR_FSYNC);
237 238 239 240
#else
	int rc = fsync(pFile->pbctbx_file->fd);
	return (rc==0 ? SQLITE_OK : SQLITE_IOERR_FSYNC);
#endif
241 242
}

Sandrine Avakian's avatar
Sandrine Avakian committed
243
/************************ END OF PLACE HOLDER FUNCTIONS ***********************/
244 245

/**
246 247
 * Opens the file fName and populates the structure pointed by p
 * with the necessary io_methods
248
 * Methods not implemented for version 1 : xTruncate, xSectorSize.
249 250 251 252 253
 * 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
254
 * @param  flags     db file access flags
255 256
 * @param  pOutFlags flags used by SQLite
 * @return           SQLITE_CANTOPEN on error, SQLITE_OK on success.
257 258 259
 */
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 = {
260
		1,										/* iVersion         Structure version number */
261 262 263
		sqlite3bctbx_Close,                 	/* xClose */
		sqlite3bctbx_Read,                  	/* xRead */
		sqlite3bctbx_Write,                 	/* xWrite */
264
		sqlite3bctbx_Truncate,					/* xTruncate */
265
		sqlite3bctbx_Sync,
266
		sqlite3bctbx_FileSize,
267 268 269 270
		sqlite3bctbx_nolockLock,
		sqlite3bctbx_nolockUnlock,
		sqlite3bctbx_nolockCheckReservedLock,
		sqlite3bctbx_FileControl,
271
		NULL,									/* xSectorSize */
272 273
		sqlite3bctbx_DeviceCharacteristics
		/*other function points follows, all NULL but not present in all sqlite3 versions.*/
274
	};
275

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

280
	/*returns error if filename is empty or file handle not initialized*/
281 282 283
	if (pFile == NULL || fName == NULL){
		return SQLITE_IOERR;
	}
284

285
	/* Set flags  to open the file with */
Sandrine Avakian's avatar
Sandrine Avakian committed
286
	if( flags&SQLITE_OPEN_EXCLUSIVE ) openFlags  |= O_EXCL;
287 288 289
	if( flags&SQLITE_OPEN_CREATE )    openFlags |= O_CREAT;
	if( flags&SQLITE_OPEN_READONLY )  openFlags |= O_RDONLY;
	if( flags&SQLITE_OPEN_READWRITE ) openFlags |= O_RDWR;
290

Sandrine Avakian's avatar
Sandrine Avakian committed
291 292 293
#if defined(_WIN32)
	openFlags |= O_BINARY;
#endif
294
	wFname = bctbx_utf8_to_locale(fName);
295 296 297 298 299 300
	if (wFname != NULL) {
		pFile->pbctbx_file = bctbx_file_open2(bctbx_vfs_get_default(), wFname, openFlags);
		bctbx_free(wFname);
	} else {
		pFile->pbctbx_file = NULL;
	}
301

302
	if( pFile->pbctbx_file == NULL){
303 304
		return SQLITE_CANTOPEN;
	}
305

306
	if( pOutFlags ){
307
    	*pOutFlags = flags;
308 309
  	}
	pFile->base.pMethods = &sqlite3_bctbx_io;
310

311 312 313
	return SQLITE_OK;
}

314

315

316 317
sqlite3_vfs *sqlite3_bctbx_vfs_create(void){
  static sqlite3_vfs bctbx_vfs = {
318 319 320 321 322 323 324 325 326
    1,								/* iVersion */
    sizeof(sqlite3_bctbx_file_t),	/* szOsFile */
    MAXPATHNAME,					/* mxPathname */
    NULL,							/* pNext */
    LINPHONE_SQLITE3_VFS,			/* zName */
    NULL,							/* pAppData */
    sqlite3bctbx_Open,				/* xOpen */
    NULL,							/* xDelete */
    NULL,							/* xAccess */
327
    NULL							/* xFullPathname */
328 329 330 331
  };
  return &bctbx_vfs;
}

332
/*static int sqlite3bctbx_winFullPathname(
Sylvain Berfini's avatar
Sylvain Berfini committed
333 334 335
										sqlite3_vfs *pVfs,            // Pointer to vfs object
										const char *zRelative,        // Possibly relative input path
										int nFull,                    // Size of output buffer in bytes
336 337 338
										char *zFull){
	//LPWSTR zTemp;
	//DWORD nByte;
Sylvain Berfini's avatar
Sylvain Berfini committed
339 340 341
	// If this path name begins with "/X:", where "X" is any alphabetic
	// character, discard the initial "/" from the pathname.
	//
342 343 344 345
	//if (zRelative[0] == '/' && sqlite3Isalpha(zRelative[1]) && zRelative[2] == ':'){
	//	zRelative++;
	//}

Sylvain Berfini's avatar
Sylvain Berfini committed
346
	 nByte = GetFullPathNameW((LPCWSTR)zRelative, 0, 0, 0);
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
	 if (nByte == 0){
		return SQLITE_CANTOPEN_FULLPATH;
	 }
	 nByte += 3;
	 zTemp = bctbx_malloc(nByte*sizeof(zTemp[0]));
	 memset(zTemp, 0, nByte*sizeof(zTemp[0]));
	 if (zTemp == 0){
		return SQLITE_IOERR_NOMEM;
	 }
	 nByte = GetFullPathNameW((LPCWSTR)zRelative, nByte, zTemp, 0);
	 if (nByte == 0){
		bctbx_free(zTemp);
		return SQLITE_CANTOPEN_FULLPATH;
	 }
	 if (zTemp){
		sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zTemp);
		bctbx_free(zTemp);
		return SQLITE_OK;
	 }
	 else{
		return SQLITE_IOERR_NOMEM;
Sylvain Berfini's avatar
Sylvain Berfini committed
368
	 }
369 370
	sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
	return SQLITE_OK;
371
}*/
372

Sandrine Avakian's avatar
Sandrine Avakian committed
373

374 375 376

void sqlite3_bctbx_vfs_register( int makeDefault){
	sqlite3_vfs* pVfsToUse = sqlite3_bctbx_vfs_create();
377 378 379
	#if _WIN32
	sqlite3_vfs* pDefault = sqlite3_vfs_find("win32");
	#else
380
	sqlite3_vfs* pDefault = sqlite3_vfs_find("unix-none");
381
	#endif
382
	pVfsToUse->xCurrentTime = pDefault->xCurrentTime;
383

384
	pVfsToUse->xAccess =  pDefault->xAccess;
385
	pVfsToUse->xFullPathname = pDefault->xFullPathname;
Sandrine Avakian's avatar
Sandrine Avakian committed
386

387 388 389 390
	pVfsToUse->xDelete = pDefault->xDelete;
	pVfsToUse->xSleep = pDefault->xSleep;
	pVfsToUse->xRandomness = pDefault->xRandomness;
	pVfsToUse->xGetLastError = pDefault->xGetLastError; /* Not implemented by sqlite3 :place holder */
391 392
	/*Functions below should not be a problem sincve we are declaring ourselves
	 in version 1 */
393 394

	/* used in version 2
395
	xCurrentTimeInt64;*/
396
	/* used in version 3
397 398 399
	xGetSystemCall
	xSetSystemCall
	xNextSystemCall*/
400 401 402 403 404 405 406 407

	sqlite3_vfs_register(pVfsToUse, makeDefault);

}


void sqlite3_bctbx_vfs_unregister(void)
{
408
	sqlite3_vfs* pVfs = sqlite3_vfs_find(LINPHONE_SQLITE3_VFS);
409 410 411
	sqlite3_vfs_unregister(pVfs);
}

412
#endif /*SQLITE_STORAGE_ENABLED*/