/ Check-in [25e72f81]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Modify the VFS in test_vfs.c to match the refactoring of the xShmXXX methods.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | wal-refactor
Files: files | file ages | folders
SHA1: 25e72f81561575051c63e9bf5d2c8e76f9fcf5c6
User & Date: dan 2010-05-13 06:19:37
Context
2010-05-13
07:08
Fix for a segfault that can follow a malloc failure. check-in: 3cab9022 user: dan tags: wal-refactor
06:19
Modify the VFS in test_vfs.c to match the refactoring of the xShmXXX methods. check-in: 25e72f81 user: dan tags: wal-refactor
2010-05-12
18:30
Fix for the test_devsym.c test module for the VFS-SHM refactoring. check-in: 49d62933 user: drh tags: wal-refactor
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/test_vfs.c.

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
..
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
...
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
...
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
...
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431


432
433
434
435
436
437
438
...
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
...
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
...
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
**
*/
#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 */
  TestvfsShm *pShm;         /* Shared memory segment */
  const char *zFilename;    /* Filename */
  sqlite3_file *pReal;      /* The real, underlying file descriptor */


};


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

};

/*
** 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.
*/
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);
................................................................................
  tvfsShmClose                    /* xShmClose */
};

/*
** Close an tvfs-file.
*/
static int tvfsClose(sqlite3_file *pFile){
  tvfs_file *p = (tvfs_file *)pFile;







  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;
  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;
  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;
  return sqlite3OsTruncate(p->pReal, size);
}

/*
** Sync an tvfs-file.
*/
static int tvfsSync(sqlite3_file *pFile, int flags){
  tvfs_file *p = (tvfs_file *)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;
  return sqlite3OsFileSize(p->pReal, pSize);
}

/*
** Lock an tvfs-file.
*/
static int tvfsLock(sqlite3_file *pFile, int eLock){
  tvfs_file *p = (tvfs_file *)pFile;
  return sqlite3OsLock(p->pReal, eLock);
}

/*
** Unlock an tvfs-file.
*/
static int tvfsUnlock(sqlite3_file *pFile, int eLock){
  tvfs_file *p = (tvfs_file *)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;
  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;
  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;
  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;
  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;
  p->pShm = 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 ){











    pFile->pMethods = &tvfs_io_methods;
  }

  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.
................................................................................
/*
** Return the current time as a Julian Day number in *pTimeOut.
*/
static int tvfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
  return PARENTVFS(pVfs)->xCurrentTime(PARENTVFS(pVfs), pTimeOut);
}

static void tvfsGrowBuffer(TestvfsShm *pShm, int reqSize, int *pNewSize){
  TestvfsBuffer *pBuffer = pShm->pBuffer;
  if( reqSize>pBuffer->n ){
    pBuffer->a = (u8 *)ckrealloc((char *)pBuffer->a, reqSize);
    memset(&pBuffer->a[pBuffer->n], 0x55, reqSize-pBuffer->n);
    pBuffer->n = reqSize;
  }
  *pNewSize = pBuffer->n;
}
................................................................................

  return 0;
}

static int tvfsShmOpen(
  sqlite3_file *pFileDes
){
  Testvfs *p = (Testvfs *)(pVfs->pAppData);
  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 */

  pFd = (tvfs_file*)pFileDes;



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

  /* 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( !pBuffer ){
    int nByte = sizeof(TestvfsBuffer) + strlen(zName) + 1;
    pBuffer = (TestvfsBuffer *)ckalloc(nByte);
    memset(pBuffer, 0, nByte);
    pBuffer->zFile = (char *)&pBuffer[1];
    strcpy(pBuffer->zFile, zName);
    pBuffer->pNext = p->pBuffer;
    p->pBuffer = pBuffer;
  }

  /* Connect the TestvfsBuffer to the new TestvfsShm handle and return. */
  pBuffer->nRef++;
  pShm->pBuffer = pBuffer;
  *pp = (sqlite3_shm *)pShm;
  return SQLITE_OK;
}

static int tvfsShmSize(
  sqlite3_vfs *pVfs,
  sqlite3_shm *pShmHandle,
  int reqSize,
  int *pNewSize
){
  int rc = SQLITE_OK;

  Testvfs *p = (Testvfs *)(pVfs->pAppData);
  TestvfsShm *pShm = (TestvfsShm *)pShmHandle;

  tvfsExecTcl(p, "xShmSize", 
      Tcl_NewStringObj(pShm->pBuffer->zFile, -1), pShm->id, 0
  );
  tvfsResultCode(p, &rc);
  if( rc==SQLITE_OK ){
    tvfsGrowBuffer(pShm, reqSize, pNewSize);
  }
  return rc;
}

static int tvfsShmGet(
  sqlite3_vfs *pVfs,
  sqlite3_shm *pShmHandle, 
  int reqMapSize, 
  int *pMapSize, 
  void **pp
){
  int rc = SQLITE_OK;

  Testvfs *p = (Testvfs *)(pVfs->pAppData);
  TestvfsShm *pShm = (TestvfsShm *)pShmHandle;

  tvfsExecTcl(p, "xShmGet", 
      Tcl_NewStringObj(pShm->pBuffer->zFile, -1), pShm->id, 0
  );
  tvfsResultCode(p, &rc);
  if( rc==SQLITE_OK ){
    tvfsGrowBuffer(pShm, reqMapSize, pMapSize);
    *pp = pShm->pBuffer->a;
  }
  return rc;
}

static int tvfsShmRelease(sqlite3_vfs *pVfs, sqlite3_shm *pShmHandle){
  int rc = SQLITE_OK;

  Testvfs *p = (Testvfs *)(pVfs->pAppData);
  TestvfsShm *pShm = (TestvfsShm *)pShmHandle;

  tvfsExecTcl(p, "xShmRelease", 
      Tcl_NewStringObj(pShm->pBuffer->zFile, -1), pShm->id, 0
  );
  tvfsResultCode(p, &rc);

  return rc;
}

static int tvfsShmLock(
  sqlite3_vfs *pVfs,
  sqlite3_shm *pShmHandle,
  int desiredLock,
  int *gotLock
){
  int rc = SQLITE_OK;
  Testvfs *p = (Testvfs *)(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(zLock, -1)
  );
  tvfsResultCode(p, &rc);
  if( rc==SQLITE_OK ){
    *gotLock = desiredLock;
  }

  return rc;
}

static int tvfsShmClose(
  sqlite3_vfs *pVfs, 
  sqlite3_shm *pShmHandle, 
  int deleteFlag
){
  int rc = SQLITE_OK;

  Testvfs *p = (Testvfs *)(pVfs->pAppData);
  TestvfsShm *pShm = (TestvfsShm *)pShmHandle;
  TestvfsBuffer *pBuffer = pShm->pBuffer;


#if 0
  assert( (deleteFlag!=0)==(pBuffer->nRef==1) );
#endif

  tvfsExecTcl(p, "xShmClose", 
      Tcl_NewStringObj(pShm->pBuffer->zFile, -1), pShm->id, 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);


  return rc;
}

static int testvfs_obj_cmd(
  ClientData cd,
  Tcl_Interp *interp,
................................................................................
*/
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 */
    0,                            /* mxPathname */
    0,                            /* pNext */
    0,                            /* zName */
    0,                            /* pAppData */
    tvfsOpen,                     /* xOpen */
    tvfsDelete,                   /* xDelete */
    tvfsAccess,                   /* xAccess */
................................................................................
  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 ){
    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); */
  return TCL_OK;
}

#endif







<
<



|
>
>
>
>
|
|
|
<
|
|
>
>







 







>













<
<
<
<
<
<
<
<





|







 







|
>
>
>
>
>
>
>












|












|







|







|







|







|







|







|







|







|







|







 







|

>





>
>
>
>
>
>
>
>
>
>
>
|

>







 







|
|







 







|



<
|

|
>
>







 







<
<
<
<
|



|


|



|






|
<




|
<




>
|
<


|



|





|
<





>
|
<


|



|
|




|

>
|
<


|







|
<




|
|











|











|
<



>
|
<
|

>





|











|
|
>







 







<


|







 







|
<
<
<
<
<
<
<










<


|




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
..
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
...
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
...
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
...
433
434
435
436
437
438
439
440
441
442
443

444
445
446
447
448
449
450
451
452
453
454
455
...
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
...
708
709
710
711
712
713
714

715
716
717
718
719
720
721
722
723
724
...
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
**
*/
#if SQLITE_TEST          /* This file is used for testing only */

#include "sqlite3.h"
#include "sqliteInt.h"



typedef struct Testvfs Testvfs;
typedef struct TestvfsShm TestvfsShm;
typedef struct TestvfsBuffer TestvfsBuffer;
typedef struct TestvfsFile TestvfsFile;

/*
** An open file handle.
*/
struct TestvfsFile {
  sqlite3_file base;              /* Base class.  Must be first */
  sqlite3_vfs *pVfs;              /* The VFS */

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










#define PARENTVFS(x) (((Testvfs *)((x)->pAppData))->pParent)


/*
** 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);
................................................................................
  tvfsShmClose                    /* xShmClose */
};

/*
** Close an tvfs-file.
*/
static int tvfsClose(sqlite3_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
){
  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
){
  TestvfsFile *p = (TestvfsFile *)pFile;
  return sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
}

/*
** Truncate an tvfs-file.
*/
static int tvfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
  TestvfsFile *p = (TestvfsFile *)pFile;
  return sqlite3OsTruncate(p->pReal, size);
}

/*
** Sync an tvfs-file.
*/
static int tvfsSync(sqlite3_file *pFile, int flags){
  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){
  TestvfsFile *p = (TestvfsFile *)pFile;
  return sqlite3OsFileSize(p->pReal, pSize);
}

/*
** Lock an tvfs-file.
*/
static int tvfsLock(sqlite3_file *pFile, int eLock){
  TestvfsFile *p = (TestvfsFile *)pFile;
  return sqlite3OsLock(p->pReal, eLock);
}

/*
** Unlock an tvfs-file.
*/
static int tvfsUnlock(sqlite3_file *pFile, int eLock){
  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){
  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){
  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){
  TestvfsFile *p = (TestvfsFile *)pFile;
  return sqlite3OsSectorSize(p->pReal);
}

/*
** Return the device characteristic flags supported by an tvfs-file.
*/
static int tvfsDeviceCharacteristics(sqlite3_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;
  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 = 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.
................................................................................
/*
** Return the current time as a Julian Day number in *pTimeOut.
*/
static int tvfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
  return PARENTVFS(pVfs)->xCurrentTime(PARENTVFS(pVfs), pTimeOut);
}

static void tvfsGrowBuffer(TestvfsFile *pFd, int reqSize, int *pNewSize){
  TestvfsBuffer *pBuffer = pFd->pShm;
  if( reqSize>pBuffer->n ){
    pBuffer->a = (u8 *)ckrealloc((char *)pBuffer->a, reqSize);
    memset(&pBuffer->a[pBuffer->n], 0x55, reqSize-pBuffer->n);
    pBuffer->n = reqSize;
  }
  *pNewSize = pBuffer->n;
}
................................................................................

  return 0;
}

static int tvfsShmOpen(
  sqlite3_file *pFileDes
){
  Testvfs *p;
  int rc = SQLITE_OK;             /* Return code */
  Tcl_Obj *pId = 0;               /* Id for this connection */
  TestvfsBuffer *pBuffer;         /* Buffer to open connection to */

  TestvfsFile *pFd;               /* The testvfs file structure */

  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
................................................................................
  if( tvfsResultCode(p, &rc) ){
    if( rc!=SQLITE_OK ) return rc;
    pId = Tcl_NewStringObj("anon", -1);
  }else{
    pId = Tcl_GetObjResult(p->interp);
  }
  Tcl_IncrRefCount(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(pFd->zFilename, pBuffer->zFile) ) break;
  }
  if( !pBuffer ){
    int nByte = sizeof(TestvfsBuffer) + strlen(pFd->zFilename) + 1;
    pBuffer = (TestvfsBuffer *)ckalloc(nByte);
    memset(pBuffer, 0, nByte);
    pBuffer->zFile = (char *)&pBuffer[1];
    strcpy(pBuffer->zFile, pFd->zFilename);
    pBuffer->pNext = p->pBuffer;
    p->pBuffer = pBuffer;
  }

  /* Connect the TestvfsBuffer to the new TestvfsShm handle and return. */
  pBuffer->nRef++;
  pFd->pShm = pBuffer;

  return SQLITE_OK;
}

static int tvfsShmSize(
  sqlite3_file *pFile,

  int reqSize,
  int *pNewSize
){
  int rc = SQLITE_OK;
  TestvfsFile *pFd = (TestvfsFile *)pFile;
  Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);


  tvfsExecTcl(p, "xShmSize", 
      Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
  );
  tvfsResultCode(p, &rc);
  if( rc==SQLITE_OK ){
    tvfsGrowBuffer(pFd, reqSize, pNewSize);
  }
  return rc;
}

static int tvfsShmGet(
  sqlite3_file *pFile, 

  int reqMapSize, 
  int *pMapSize, 
  void **pp
){
  int rc = SQLITE_OK;
  TestvfsFile *pFd = (TestvfsFile *)pFile;
  Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);


  tvfsExecTcl(p, "xShmGet", 
      Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
  );
  tvfsResultCode(p, &rc);
  if( rc==SQLITE_OK ){
    tvfsGrowBuffer(pFd, reqMapSize, pMapSize);
    *pp = pFd->pShm->a;
  }
  return rc;
}

static int tvfsShmRelease(sqlite3_file *pFile){
  int rc = SQLITE_OK;
  TestvfsFile *pFd = (TestvfsFile *)pFile;
  Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);


  tvfsExecTcl(p, "xShmRelease", 
      Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
  );
  tvfsResultCode(p, &rc);

  return rc;
}

static int tvfsShmLock(
  sqlite3_file *pFile,

  int desiredLock,
  int *gotLock
){
  int rc = SQLITE_OK;
  TestvfsFile *pFd = (TestvfsFile *)pFile;
  Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);
  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(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_file *pFile,

  int deleteFlag
){
  int rc = SQLITE_OK;
  TestvfsFile *pFd = (TestvfsFile *)pFile;
  Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData);

  TestvfsBuffer *pBuffer = pFd->pShm;

  assert( pFd->pShmId && pFd->pShm );
#if 0
  assert( (deleteFlag!=0)==(pBuffer->nRef==1) );
#endif

  tvfsExecTcl(p, "xShmClose", 
      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(pFd->pShmId);
  pFd->pShmId = 0;
  pFd->pShm = 0;

  return rc;
}

static int testvfs_obj_cmd(
  ClientData cd,
  Tcl_Interp *interp,
................................................................................
*/
static int testvfs_cmd(
  ClientData cd,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){

  static sqlite3_vfs tvfs_vfs = {
    2,                            /* iVersion */
    sizeof(TestvfsFile),            /* szOsFile */
    0,                            /* mxPathname */
    0,                            /* pNext */
    0,                            /* zName */
    0,                            /* pAppData */
    tvfsOpen,                     /* xOpen */
    tvfsDelete,                   /* xDelete */
    tvfsAccess,                   /* xAccess */
................................................................................
  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;
  p->isNoshm = isNoshm;








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


int Sqlitetestvfs_Init(Tcl_Interp *interp){
  Tcl_CreateObjCommand(interp, "testvfs", testvfs_cmd, 0, 0);
  return TCL_OK;
}

#endif