File: sqlite3_bctbx_vfs.c

package info (click to toggle)
linphone 5.3.105-6
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 57,048 kB
  • sloc: cpp: 166,867; ansic: 102,939; python: 8,280; java: 4,406; sh: 1,040; xml: 1,023; makefile: 777; perl: 377; objc: 190; php: 88; javascript: 38; cs: 38
file content (391 lines) | stat: -rw-r--r-- 12,861 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 * Copyright (c) 2010-2022 Belledonne Communications SARL.
 *
 * This file is part of Liblinphone
 * (see https://gitlab.linphone.org/BC/public/liblinphone).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "bctoolbox/charconv.h"
#include <bctoolbox/defs.h>

#include "sqlite3_bctbx_vfs.h"
#include <sqlite3.h>

#ifndef _WIN32_WCE
#include <errno.h>
#endif /*_WIN32_WCE*/

#include "private.h"

/**
 * 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;
	}
}

/**
 * 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  		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
 * @return 			SQLITE_OK if read bytes equals count,
 *                  SQLITE_IOERR_SHORT_READ if the number of bytes read is inferior to count
 *                  SQLITE_IOERR_READ if an error occurred.
 */
static int sqlite3bctbx_Read(sqlite3_file *p, void *buf, int count, sqlite_int64 offset) {
	int ret;
	sqlite3_bctbx_file_t *pFile = (sqlite3_bctbx_file_t *)p;
	if (pFile) {
		ret = (int)bctbx_file_read(pFile->pbctbx_file, buf, (size_t)count, (off_t)offset);
		if (ret == count) {
			return SQLITE_OK;
		} else if (ret >= 0) {
			/*fill in unread portion of buffer, as requested by sqlite3 documentation*/
			memset(((uint8_t *)buf) + ret, 0, (size_t)(count - ret));
			return SQLITE_IOERR_SHORT_READ;
		} else {

			return SQLITE_IOERR_READ;
		}
	}
	return SQLITE_IOERR_READ;
}

/**
 * Writes directly to the open file given through the p argument.
 * 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
 * @return         SQLITE_OK on success, SQLITE_IOERR_WRITE if an error occurred.
 */
static int sqlite3bctbx_Write(sqlite3_file *p, const void *buf, int count, sqlite_int64 offset) {
	sqlite3_bctbx_file_t *pFile = (sqlite3_bctbx_file_t *)p;
	int ret;
	if (pFile) {
		ret = (int)bctbx_file_write(pFile->pbctbx_file, buf, (size_t)count, (off_t)offset);
		if (ret > 0) return SQLITE_OK;
		else {
			return SQLITE_IOERR_WRITE;
		}
	}
	return SQLITE_IOERR_WRITE;
}

/**
 * TRuncates or extends a file depending on the size provided.
 * @param  p    sqlite3_file file handle pointer.
 * @param  size New file size.
 * @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.
 */
static int sqlite3bctbx_Truncate(sqlite3_file *p, sqlite_int64 size) {
	int rc;
	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;
}

/**
 * Saves the file size associated with the file handle p into the argument pSize.
 * @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.
 */
static int sqlite3bctbx_FileSize(sqlite3_file *p, sqlite_int64 *pSize) {

	int64_t rc; /* Return code from fstat() call */
	sqlite3_bctbx_file_t *pFile = (sqlite3_bctbx_file_t *)p;
	if (pFile->pbctbx_file) {
		rc = bctbx_file_size(pFile->pbctbx_file);
		if (rc < 0) {
			return SQLITE_IOERR_FSTAT;
		}
		if (pSize) {
			*pSize = rc;
			return SQLITE_OK;
		}
	}
	return SQLITE_ERROR;
}

/************************ 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. */

/**
 * 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(BCTBX_UNUSED(sqlite3_file *p)) {
	int rc = 0x00001000;
	return rc;
}

/**
 * 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(BCTBX_UNUSED(sqlite3_file *p), BCTBX_UNUSED(int op), BCTBX_UNUSED(void *pArg)) {
#ifdef SQLITE_FCNTL_MMAP_SIZE
	if (op == SQLITE_FCNTL_MMAP_SIZE) return SQLITE_OK;
#endif
	return SQLITE_NOTFOUND;
}

/**
 * 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(BCTBX_UNUSED(sqlite3_file *pUnused), int *pResOut) {
	*pResOut = 0;
	return SQLITE_OK;
}

/**
 * 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(BCTBX_UNUSED(sqlite3_file *pUnused), BCTBX_UNUSED(int unused)) {
	return SQLITE_OK;
}

/**
 * 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(BCTBX_UNUSED(sqlite3_file *pUnused), BCTBX_UNUSED(int unused)) {
	return SQLITE_OK;
}

/**
 * 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, BCTBX_UNUSED(int flags)) {
	sqlite3_bctbx_file_t *pFile = (sqlite3_bctbx_file_t *)p;
	int ret = bctbx_file_sync(pFile->pbctbx_file);
	return (ret == BCTBX_VFS_OK ? SQLITE_OK : SQLITE_IOERR_FSYNC);
}

/************************ END OF PLACE HOLDER FUNCTIONS ***********************/

/**
 * 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(BCTBX_UNUSED(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 */
	    sqlite3bctbx_Truncate, /* xTruncate */
	    sqlite3bctbx_Sync,
	    sqlite3bctbx_FileSize,
	    sqlite3bctbx_nolockLock,
	    sqlite3bctbx_nolockUnlock,
	    sqlite3bctbx_nolockCheckReservedLock,
	    sqlite3bctbx_FileControl,
	    NULL, /* xSectorSize */
	    sqlite3bctbx_DeviceCharacteristics
	    /*other function points follows, all NULL but not present in all sqlite3 versions.*/
	};

	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
	/* sqlite3 requires an UTF-8 encoded path, but UTF-8 may not be the
	 * process current locale. */
	wFname = bctbx_utf8_to_locale(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;
}

sqlite3_vfs *sqlite3_bctbx_vfs_create(void) {
	static sqlite3_vfs bctbx_vfs = {
	    1,                            /* iVersion */
	    sizeof(sqlite3_bctbx_file_t), /* szOsFile */
	    MAXPATHNAME,                  /* mxPathname */
	    NULL,                         /* pNext */
	    BCTBX_SQLITE3_VFS,            /* zName */
	    NULL,                         /* pAppData */
	    sqlite3bctbx_Open,            /* xOpen */
	    NULL,                         /* xDelete */
	    NULL,                         /* xAccess */
	    NULL                          /* xFullPathname */
	};
	return &bctbx_vfs;
}

/*static int sqlite3bctbx_winFullPathname(
                                        sqlite3_vfs *pVfs,            // Pointer to vfs object
                                        const char *zRelative,        // Possibly relative input path
                                        int nFull,                    // Size of output buffer in bytes
                                        char *zFull){
    //LPWSTR zTemp;
    //DWORD nByte;
    // If this path name begins with "/X:", where "X" is any alphabetic
    // character, discard the initial "/" from the pathname.
    //
    //if (zRelative[0] == '/' && sqlite3Isalpha(zRelative[1]) && zRelative[2] == ':'){
    //	zRelative++;
    //}

     nByte = GetFullPathNameW((LPCWSTR)zRelative, 0, 0, 0);
     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;
     }
    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
    return SQLITE_OK;
}*/

void sqlite3_bctbx_vfs_register(int makeDefault) {
	sqlite3_vfs *pVfsToUse = sqlite3_bctbx_vfs_create();
#if _WIN32
	sqlite3_vfs *pDefault = sqlite3_vfs_find("win32");
#else
	sqlite3_vfs *pDefault = sqlite3_vfs_find("unix-none");
#endif
	pVfsToUse->xCurrentTime = pDefault->xCurrentTime;

	pVfsToUse->xAccess = pDefault->xAccess;
	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 */
	/*Functions below should not be a problem sincve we are declaring ourselves
	 in version 1 */

	/* used in version 2
	xCurrentTimeInt64;*/
	/* used in version 3
	xGetSystemCall
	xSetSystemCall
	xNextSystemCall*/

	sqlite3_vfs_register(pVfsToUse, makeDefault);
}

void sqlite3_bctbx_vfs_unregister(void) {
	sqlite3_vfs *pVfs = sqlite3_vfs_find(BCTBX_SQLITE3_VFS);
	sqlite3_vfs_unregister(pVfs);
}