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
|
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
|
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
-
-
+
|
**
*/
#if SQLITE_TEST /* This file is used for testing only */
#include "sqlite3.h"
#include "sqliteInt.h"
#if 0 /* FIX THIS LATER */
typedef struct Testvfs Testvfs;
typedef struct TestvfsShm TestvfsShm;
typedef struct TestvfsBuffer TestvfsBuffer;
typedef struct tvfs_file tvfs_file;
struct tvfs_file {
sqlite3_file base; /* Base class. Must be first */
sqlite3_vfs *pVfs; /* the VFS */
typedef struct TestvfsFile TestvfsFile;
/*
** An open file handle.
*/
struct TestvfsFile {
sqlite3_file base; /* Base class. Must be first */
sqlite3_vfs *pVfs; /* The VFS */
TestvfsShm *pShm; /* Shared memory segment */
const char *zFilename; /* Filename */
sqlite3_file *pReal; /* The real, underlying file descriptor */
const char *zFilename; /* Filename as passed to xOpen() */
sqlite3_file *pReal; /* The real, underlying file descriptor */
Tcl_Obj *pShmId; /* Shared memory id for Tcl callbacks */
TestvfsBuffer *pShm; /* Shared memory buffer */
};
/*
** An instance of this structure is allocated for each VFS created. The
** sqlite3_vfs.pAppData field of the VFS structure registered with SQLite
** is set to point to it.
*/
struct Testvfs {
char *zName; /* Name of this VFS */
sqlite3_vfs *pParent; /* The VFS to use for file IO */
sqlite3_vfs *pVfs; /* The testvfs registered with SQLite */
Tcl_Interp *interp; /* Interpreter to run script in */
int nScript; /* Number of elements in array apScript */
Tcl_Obj **apScript; /* Script to execute */
TestvfsBuffer *pBuffer; /* List of shared buffers */
int isNoshm;
};
/*
** A shared-memory buffer.
*/
struct TestvfsBuffer {
char *zFile; /* Associated file name */
int n; /* Size of allocated buffer in bytes */
u8 *a; /* Buffer allocated using ckalloc() */
int nRef; /* Number of references to this object */
TestvfsBuffer *pNext; /* Next in linked list of all buffers */
};
/*
** A shared-memory handle returned by tvfsShmOpen().
*/
struct TestvfsShm {
Tcl_Obj *id; /* Name of this handle */
TestvfsBuffer *pBuffer; /* Underlying buffer */
};
#define PARENTVFS(x) (((Testvfs *)((x)->pAppData))->pParent)
/*
** Method declarations for tvfs_file.
** Method declarations for TestvfsFile.
*/
static int tvfsClose(sqlite3_file*);
static int tvfsRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
static int tvfsWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
static int tvfsTruncate(sqlite3_file*, sqlite3_int64 size);
static int tvfsSync(sqlite3_file*, int flags);
static int tvfsFileSize(sqlite3_file*, sqlite3_int64 *pSize);
|
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
|
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
|
-
+
+
+
+
+
+
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
|
tvfsShmClose /* xShmClose */
};
/*
** Close an tvfs-file.
*/
static int tvfsClose(sqlite3_file *pFile){
tvfs_file *p = (tvfs_file *)pFile;
TestvfsFile *p = (TestvfsFile *)pFile;
if( p->pShmId ){
Tcl_DecrRefCount(p->pShmId);
p->pShmId = 0;
}
if( pFile->pMethods ){
ckfree((char *)pFile->pMethods);
}
return sqlite3OsClose(p->pReal);
}
/*
** Read data from an tvfs-file.
*/
static int tvfsRead(
sqlite3_file *pFile,
void *zBuf,
int iAmt,
sqlite_int64 iOfst
){
tvfs_file *p = (tvfs_file *)pFile;
TestvfsFile *p = (TestvfsFile *)pFile;
return sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
}
/*
** Write data to an tvfs-file.
*/
static int tvfsWrite(
sqlite3_file *pFile,
const void *zBuf,
int iAmt,
sqlite_int64 iOfst
){
tvfs_file *p = (tvfs_file *)pFile;
TestvfsFile *p = (TestvfsFile *)pFile;
return sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
}
/*
** Truncate an tvfs-file.
*/
static int tvfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
tvfs_file *p = (tvfs_file *)pFile;
TestvfsFile *p = (TestvfsFile *)pFile;
return sqlite3OsTruncate(p->pReal, size);
}
/*
** Sync an tvfs-file.
*/
static int tvfsSync(sqlite3_file *pFile, int flags){
tvfs_file *p = (tvfs_file *)pFile;
TestvfsFile *p = (TestvfsFile *)pFile;
return sqlite3OsSync(p->pReal, flags);
}
/*
** Return the current file-size of an tvfs-file.
*/
static int tvfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
tvfs_file *p = (tvfs_file *)pFile;
TestvfsFile *p = (TestvfsFile *)pFile;
return sqlite3OsFileSize(p->pReal, pSize);
}
/*
** Lock an tvfs-file.
*/
static int tvfsLock(sqlite3_file *pFile, int eLock){
tvfs_file *p = (tvfs_file *)pFile;
TestvfsFile *p = (TestvfsFile *)pFile;
return sqlite3OsLock(p->pReal, eLock);
}
/*
** Unlock an tvfs-file.
*/
static int tvfsUnlock(sqlite3_file *pFile, int eLock){
tvfs_file *p = (tvfs_file *)pFile;
TestvfsFile *p = (TestvfsFile *)pFile;
return sqlite3OsUnlock(p->pReal, eLock);
}
/*
** Check if another file-handle holds a RESERVED lock on an tvfs-file.
*/
static int tvfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
tvfs_file *p = (tvfs_file *)pFile;
TestvfsFile *p = (TestvfsFile *)pFile;
return sqlite3OsCheckReservedLock(p->pReal, pResOut);
}
/*
** File control method. For custom operations on an tvfs-file.
*/
static int tvfsFileControl(sqlite3_file *pFile, int op, void *pArg){
tvfs_file *p = (tvfs_file *)pFile;
TestvfsFile *p = (TestvfsFile *)pFile;
return sqlite3OsFileControl(p->pReal, op, pArg);
}
/*
** Return the sector-size in bytes for an tvfs-file.
*/
static int tvfsSectorSize(sqlite3_file *pFile){
tvfs_file *p = (tvfs_file *)pFile;
TestvfsFile *p = (TestvfsFile *)pFile;
return sqlite3OsSectorSize(p->pReal);
}
/*
** Return the device characteristic flags supported by an tvfs-file.
*/
static int tvfsDeviceCharacteristics(sqlite3_file *pFile){
tvfs_file *p = (tvfs_file *)pFile;
TestvfsFile *p = (TestvfsFile *)pFile;
return sqlite3OsDeviceCharacteristics(p->pReal);
}
/*
** Open an tvfs file handle.
*/
static int tvfsOpen(
sqlite3_vfs *pVfs,
const char *zName,
sqlite3_file *pFile,
int flags,
int *pOutFlags
){
int rc;
tvfs_file *p = (tvfs_file *)pFile;
TestvfsFile *p = (TestvfsFile *)pFile;
p->pShm = 0;
p->pShmId = 0;
p->zFilename = zName;
p->pVfs = pVfs;
p->pReal = (sqlite3_file *)&p[1];
rc = sqlite3OsOpen(PARENTVFS(pVfs), zName, p->pReal, flags, pOutFlags);
if( p->pReal->pMethods ){
sqlite3_io_methods *pMethods;
pMethods = (sqlite3_io_methods *)ckalloc(sizeof(sqlite3_io_methods));
memcpy(pMethods, &tvfs_io_methods, sizeof(sqlite3_io_methods));
if( ((Testvfs *)pVfs->pAppData)->isNoshm ){
pMethods->xShmOpen = 0;
pMethods->xShmGet = 0;
pMethods->xShmSize = 0;
pMethods->xShmRelease = 0;
pMethods->xShmClose = 0;
pMethods->xShmLock = 0;
}
pFile->pMethods = &tvfs_io_methods;
pFile->pMethods = pMethods;
}
return rc;
}
/*
** Delete the file located at zPath. If the dirSync argument is true,
** ensure the file-system modifications are synced to disk before
** returning.
|
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
|
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
|
-
+
-
-
+
-
+
+
+
-
-
-
-
-
+
-
+
-
+
-
+
-
+
-
-
+
-
+
-
+
-
-
+
-
+
-
+
-
+
-
+
-
-
+
-
-
+
+
-
+
+
-
+
-
-
+
-
+
-
+
-
+
-
-
+
-
+
-
+
-
+
-
-
+
+
-
+
-
-
+
+
+
|
return 0;
}
static int tvfsShmOpen(
sqlite3_file *pFileDes
){
Testvfs *p = (Testvfs *)(pVfs->pAppData);
Testvfs *p;
int rc = SQLITE_OK; /* Return code */
Tcl_Obj *pId = 0; /* Id for this connection */
TestvfsBuffer *pBuffer; /* Buffer to open connection to */
TestvfsShm *pShm; /* New shm handle */
tvfs_file *pFd; /* The file descriptor */
TestvfsFile *pFd; /* The testvfs file structure */
pFd = (tvfs_file*)pFileDes;
pFd = (TestvfsFile*)pFileDes;
p = (Testvfs *)pFd->pVfs->pAppData;
assert( pFd->pShmId==0 && pFd->pShm==0 );
/* Evaluate the Tcl script:
**
** SCRIPT xShmOpen FILENAME
**
** If the script returns an SQLite error code other than SQLITE_OK, an
** error is returned to the caller. If it returns SQLITE_OK, the new
** connection is named "anon". Otherwise, the value returned by the
** script is used as the connection name.
*/
tvfsExecTcl(p, "xShmOpen", Tcl_NewStringObj(pFd->zFilename, -1), 0, 0);
if( tvfsResultCode(p, &rc) ){
if( rc!=SQLITE_OK ) return rc;
pId = Tcl_NewStringObj("anon", -1);
}else{
pId = Tcl_GetObjResult(p->interp);
}
Tcl_IncrRefCount(pId);
/* Allocate the TestvfsShm handle. */
pShm = (TestvfsShm *)ckalloc(sizeof(TestvfsShm));
memset(pShm, 0, sizeof(TestvfsShm));
pShm->id = pId;
pFd->pShmId = pId;
/* Search for a TestvfsBuffer. Create a new one if required. */
for(pBuffer=p->pBuffer; pBuffer; pBuffer=pBuffer->pNext){
if( 0==strcmp(zName, pBuffer->zFile) ) break;
if( 0==strcmp(pFd->zFilename, pBuffer->zFile) ) break;
}
if( !pBuffer ){
int nByte = sizeof(TestvfsBuffer) + strlen(zName) + 1;
int nByte = sizeof(TestvfsBuffer) + strlen(pFd->zFilename) + 1;
pBuffer = (TestvfsBuffer *)ckalloc(nByte);
memset(pBuffer, 0, nByte);
pBuffer->zFile = (char *)&pBuffer[1];
strcpy(pBuffer->zFile, zName);
strcpy(pBuffer->zFile, pFd->zFilename);
pBuffer->pNext = p->pBuffer;
p->pBuffer = pBuffer;
}
/* Connect the TestvfsBuffer to the new TestvfsShm handle and return. */
pBuffer->nRef++;
pShm->pBuffer = pBuffer;
pFd->pShm = pBuffer;
*pp = (sqlite3_shm *)pShm;
return SQLITE_OK;
}
static int tvfsShmSize(
sqlite3_vfs *pVfs,
sqlite3_file *pFile,
sqlite3_shm *pShmHandle,
int reqSize,
int *pNewSize
){
int rc = SQLITE_OK;
TestvfsFile *pFd = (TestvfsFile *)pFile;
Testvfs *p = (Testvfs *)(pVfs->pAppData);
Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);
TestvfsShm *pShm = (TestvfsShm *)pShmHandle;
tvfsExecTcl(p, "xShmSize",
Tcl_NewStringObj(pShm->pBuffer->zFile, -1), pShm->id, 0
Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
);
tvfsResultCode(p, &rc);
if( rc==SQLITE_OK ){
tvfsGrowBuffer(pShm, reqSize, pNewSize);
tvfsGrowBuffer(pFd, reqSize, pNewSize);
}
return rc;
}
static int tvfsShmGet(
sqlite3_vfs *pVfs,
sqlite3_file *pFile,
sqlite3_shm *pShmHandle,
int reqMapSize,
int *pMapSize,
void **pp
){
int rc = SQLITE_OK;
TestvfsFile *pFd = (TestvfsFile *)pFile;
Testvfs *p = (Testvfs *)(pVfs->pAppData);
Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);
TestvfsShm *pShm = (TestvfsShm *)pShmHandle;
tvfsExecTcl(p, "xShmGet",
Tcl_NewStringObj(pShm->pBuffer->zFile, -1), pShm->id, 0
Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
);
tvfsResultCode(p, &rc);
if( rc==SQLITE_OK ){
tvfsGrowBuffer(pShm, reqMapSize, pMapSize);
*pp = pShm->pBuffer->a;
tvfsGrowBuffer(pFd, reqMapSize, pMapSize);
*pp = pFd->pShm->a;
}
return rc;
}
static int tvfsShmRelease(sqlite3_vfs *pVfs, sqlite3_shm *pShmHandle){
static int tvfsShmRelease(sqlite3_file *pFile){
int rc = SQLITE_OK;
TestvfsFile *pFd = (TestvfsFile *)pFile;
Testvfs *p = (Testvfs *)(pVfs->pAppData);
Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);
TestvfsShm *pShm = (TestvfsShm *)pShmHandle;
tvfsExecTcl(p, "xShmRelease",
Tcl_NewStringObj(pShm->pBuffer->zFile, -1), pShm->id, 0
Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
);
tvfsResultCode(p, &rc);
return rc;
}
static int tvfsShmLock(
sqlite3_vfs *pVfs,
sqlite3_file *pFile,
sqlite3_shm *pShmHandle,
int desiredLock,
int *gotLock
){
int rc = SQLITE_OK;
TestvfsFile *pFd = (TestvfsFile *)pFile;
Testvfs *p = (Testvfs *)(pVfs->pAppData);
Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);
TestvfsShm *pShm = (TestvfsShm *)pShmHandle;
char *zLock = "";
switch( desiredLock ){
case SQLITE_SHM_READ: zLock = "READ"; break;
case SQLITE_SHM_WRITE: zLock = "WRITE"; break;
case SQLITE_SHM_CHECKPOINT: zLock = "CHECKPOINT"; break;
case SQLITE_SHM_RECOVER: zLock = "RECOVER"; break;
case SQLITE_SHM_PENDING: zLock = "PENDING"; break;
case SQLITE_SHM_UNLOCK: zLock = "UNLOCK"; break;
}
tvfsExecTcl(p, "xShmLock",
Tcl_NewStringObj(pShm->pBuffer->zFile, -1), pShm->id,
Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId,
Tcl_NewStringObj(zLock, -1)
);
tvfsResultCode(p, &rc);
if( rc==SQLITE_OK ){
*gotLock = desiredLock;
}
return rc;
}
static int tvfsShmClose(
sqlite3_vfs *pVfs,
sqlite3_file *pFile,
sqlite3_shm *pShmHandle,
int deleteFlag
){
int rc = SQLITE_OK;
TestvfsFile *pFd = (TestvfsFile *)pFile;
Testvfs *p = (Testvfs *)(pVfs->pAppData);
Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);
TestvfsShm *pShm = (TestvfsShm *)pShmHandle;
TestvfsBuffer *pBuffer = pShm->pBuffer;
TestvfsBuffer *pBuffer = pFd->pShm;
assert( pFd->pShmId && pFd->pShm );
#if 0
assert( (deleteFlag!=0)==(pBuffer->nRef==1) );
#endif
tvfsExecTcl(p, "xShmClose",
Tcl_NewStringObj(pShm->pBuffer->zFile, -1), pShm->id, 0
Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
);
tvfsResultCode(p, &rc);
pBuffer->nRef--;
if( pBuffer->nRef==0 ){
TestvfsBuffer **pp;
for(pp=&p->pBuffer; *pp!=pBuffer; pp=&((*pp)->pNext));
*pp = (*pp)->pNext;
ckfree((char *)pBuffer->a);
ckfree((char *)pBuffer);
}
Tcl_DecrRefCount(pShm->id);
ckfree((char *)pShm);
Tcl_DecrRefCount(pFd->pShmId);
pFd->pShmId = 0;
pFd->pShm = 0;
return rc;
}
static int testvfs_obj_cmd(
ClientData cd,
Tcl_Interp *interp,
|
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
|
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
|
-
-
+
|
*/
static int testvfs_cmd(
ClientData cd,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
static sqlite3_vfs tvfs_vfs = {
2, /* iVersion */
sizeof(tvfs_file), /* szOsFile */
sizeof(TestvfsFile), /* szOsFile */
0, /* mxPathname */
0, /* pNext */
0, /* zName */
0, /* pAppData */
tvfsOpen, /* xOpen */
tvfsDelete, /* xDelete */
tvfsAccess, /* xAccess */
|
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
|
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
|
-
+
-
-
-
-
-
-
-
-
-
+
|
pVfs = (sqlite3_vfs *)ckalloc(sizeof(sqlite3_vfs));
memcpy(pVfs, &tvfs_vfs, sizeof(sqlite3_vfs));
pVfs->pAppData = (void *)p;
pVfs->zName = p->zName;
pVfs->mxPathname = p->pParent->mxPathname;
pVfs->szOsFile += p->pParent->szOsFile;
p->pVfs = pVfs;
if( isNoshm ){
p->isNoshm = isNoshm;
pVfs->xShmOpen = 0;
pVfs->xShmGet = 0;
pVfs->xShmSize = 0;
pVfs->xShmRelease = 0;
pVfs->xShmClose = 0;
pVfs->xShmLock = 0;
}
Tcl_CreateObjCommand(interp, zVfs, testvfs_obj_cmd, p, testvfs_obj_del);
sqlite3_vfs_register(pVfs, 0);
return TCL_OK;
bad_args:
Tcl_WrongNumArgs(interp, 1, objv, "?-noshm? VFSNAME SCRIPT");
return TCL_ERROR;
}
#endif /* 0 */
int Sqlitetestvfs_Init(Tcl_Interp *interp){
/* Tcl_CreateObjCommand(interp, "testvfs", testvfs_cmd, 0, 0); */
Tcl_CreateObjCommand(interp, "testvfs", testvfs_cmd, 0, 0);
return TCL_OK;
}
#endif
|