/ Check-in [966bc68e]
Login

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

Overview
Comment:The crash tests now compile, at least. But they get wrong results. The problem is fundamental and suggests I need to completely rethink how the new OS backend should work. (CVS 2792)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:966bc68e1bf4e0cc88407871c162ee3014160415
User & Date: drh 2005-11-29 18:37:16
Context
2005-11-29
19:50
Make the build work on windows again. Ticket #1544. (CVS 2793) check-in: 59bdca25 user: drh tags: trunk
18:37
The crash tests now compile, at least. But they get wrong results. The problem is fundamental and suggests I need to completely rethink how the new OS backend should work. (CVS 2792) check-in: 966bc68e user: drh tags: trunk
03:13
Make the OsFile structure opaque with its internal structure known only to the appropriate os_*.c implementation. Omit the os_unix.h and os_win.h include files. The crash tests are broken by this patch. (CVS 2791) check-in: 058f3175 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/test6.c.

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
..
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
...
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
...
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
...
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
...
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
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
...
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
** the effect on the database file of an OS crash or power failure.  This
** is used to test the ability of SQLite to recover from those situations.
*/
#if SQLITE_TEST          /* This file is used for the testing only */
#include "sqliteInt.h"
#include "os.h"
#include "tcl.h"
#if 0

/*
** A copy of the original sqlite3Io structure
*/
static struct sqlite3IoVtbl origIo;

/*
** The OsFile structure for the crash-test backend.  The pBase field
** points to an OsFile structure for the native backend.
*/
struct OsFile {
  u8 **apBlk;         /* Array of blocks that have been written to. */
  int nBlk;           /* Size of apBlock. */

  int nMaxWrite;      /* Largest offset written to. */
  char *zName;        /* File name */
  OsFile *pBase;      /* Base class */
  OsFile *pNext;      /* Next in a list of them all */
};

/*
................................................................................
  sqlite3OsLeaveMutex();
  return r;
}

/*
** A list of all open files.
*/
static OsTestFile *pAllFiles = 0;

/*
** Initialise the os_test.c specific fields of pFile.
*/
static void initFile(OsFile **pId, char const *zName){
  OsFile *pFile = *pId = sqliteMalloc(sizeof(OsFile) + strlen(zName)+1);
  pFile->nMaxWrite = 0; 

  pFile->nBlk = 0; 
  pFile->apBlk = 0; 
  pFile->zName = (char *)(&pFile[1]);
  strcpy(pFile->zName, zName);
  pFile->pBase = id;
  pFile->pNext = pAllFiles;
  pAllFiles = pFile;
}

/*
** Undo the work done by initFile. Delete the OsTestFile structure
** and unlink the structure from the pAllFiles list.
*/
static void closeFile(OsFile *id){
  OsFile *pFile = id;
  if( pFile==pAllFiles ){
    pAllFiles = pFile->pNext;
  }else{
    OsFile *p;
    for(p=pAllFiles; p->pNext!=pFile; p=p->pNext ){
      assert( p );
    }
    p->pNext = pFile->pNext;
  }
  sqliteFree(pFile);

}

/*
** Return the current seek offset from the start of the file. This
** is unix-only code.
*/
static i64 osTell(OsFile *pFile){
  return lseek(pFile->pBase->h, 0, SEEK_CUR);
}

/*
** Load block 'blk' into the cache of pFile.
*/
static int cacheBlock(OsFile *pFile, int blk){
  if( blk>=pFile->nBlk ){
    int n = ((pFile->nBlk * 2) + 100 + blk);
    /* if( pFile->nBlk==0 ){ printf("DIRTY %s\n", pFile->zName); } */
    pFile->apBlk = (u8 **)sqliteRealloc(pFile->apBlk, n * sizeof(u8*));
    if( !pFile->apBlk ) return SQLITE_NOMEM;
    memset(&pFile->apBlk[pFile->nBlk], 0, (n - pFile->nBlk)*sizeof(u8*));
    pFile->nBlk = n;
................................................................................
/*
** Write the cache of pFile to disk. If crash is non-zero, randomly
** skip blocks when writing. The cache is deleted before returning.
*/
static int writeCache2(OsFile *pFile, int crash){
  int i;
  int nMax = pFile->nMaxWrite;
  i64 offset;
  int rc = SQLITE_OK;

  offset = osTell(pFile);
  for(i=0; i<pFile->nBlk; i++){
    u8 *p = pFile->apBlk[i];
    if( p ){
      int skip = 0;
      int trash = 0;
      if( crash ){
        char random;
................................................................................
      sqliteFree(p);
    }
  }
  sqliteFree(pFile->apBlk);
  pFile->nBlk = 0;
  pFile->apBlk = 0;
  pFile->nMaxWrite = 0;

  if( rc==SQLITE_OK ){
    rc = origIo.xSeek(pFile->pBase, offset);
  }
  return rc;
}

/*
** Write the cache to disk.
*/
static int writeCache(OsFile *pFile){
  if( pFile->apBlk ){
    int c = crashRequired(pFile->zName);
    if( c ){
      OsTestFile *p;
#ifdef TRACE_WRITECACHE
      printf("\nCrash during sync of %s\n", pFile->zName);
#endif
      for(p=pAllFiles; p; p=p->pNext){
        writeCache2(p, 1);
      }
      exit(-1);
................................................................................
  }
  return SQLITE_OK;
}

/*
** Close the file.
*/
static int crashClose(OsFile *id){
  if( id->pAux ) return SQLITE_OK;
  if( id->isOpen ){
    /* printf("CLOSE %s (%d blocks)\n", (*id)->zName, (*id)->nBlk); */
    writeCache(id);
    origIo.xClose(&id->pBase);
  }
  closeFile(id);





  return SQLITE_OK;
}

static int crashRead(OsFile *id, void *pBuf, int amt){
  i64 offset;       /* The current offset from the start of the file */
  i64 end;          /* The byte just past the last byte read */
  int blk;            /* Block number the read starts on */
  int i;
  u8 *zCsr;
  int rc = SQLITE_OK;
  OsTestFile *pFile = (OsTestFile*)id->pAux;

  offset = osTell(pFile);
  end = offset+amt;
  blk = (offset/BLOCKSIZE);

  zCsr = (u8 *)pBuf;
  for(i=blk; i*BLOCKSIZE<end; i++){
    int off = 0;
    int len = 0;
................................................................................
      len = len - (BLOCK_OFFSET(i+1)-end);
    }

    if( i<pFile->nBlk && pFile->apBlk[i]){
      u8 *pBlk = pFile->apBlk[i];
      memcpy(zCsr, &pBlk[off], len);
    }else{
      rc = origIo.xSeek(id, BLOCK_OFFSET(i) + off);
      if( rc!=SQLITE_OK ) return rc;
      rc = origIo.xRead(id, zCsr, len);
      if( rc!=SQLITE_OK ) return rc;
    }

    zCsr += len;
  }
  assert( zCsr==&((u8 *)pBuf)[amt] );

  rc = origIo.xSeek(id, end);
  return rc;
}

static int crashWrite(OsFile *id, const void *pBuf, int amt){
  i64 offset;       /* The current offset from the start of the file */
  i64 end;          /* The byte just past the last byte written */
  int blk;            /* Block number the write starts on */
  int i;
  const u8 *zCsr;
  int rc = SQLITE_OK;
  OsTestFile *pFile =  (OsTestFile*)id->pAux;

  offset = osTell(pFile);
  end = offset+amt;
  blk = (offset/BLOCKSIZE);

  zCsr = (u8 *)pBuf;
  for(i=blk; i*BLOCKSIZE<end; i++){
    u8 *pBlk;
    int off = 0;
    int len = 0;

    /* Make sure the block is in the cache */
    rc = cacheBlock(pFile, i);
    if( rc!=SQLITE_OK ) return rc;

    /* Write into the cache */
    pBlk = pFile->apBlk[i];
    assert( pBlk );

    if( BLOCK_OFFSET(i) < offset ){
      off = offset-BLOCK_OFFSET(i);
    }
    len = BLOCKSIZE - off;
    if( BLOCK_OFFSET(i+1) > end ){
      len = len - (BLOCK_OFFSET(i+1)-end);
    }
    memcpy(&pBlk[off], zCsr, len);
    zCsr += len;
  }
  if( pFile->nMaxWrite<end ){
    pFile->nMaxWrite = end;
  }
  assert( zCsr==&((u8 *)pBuf)[amt] );

  rc = origIo.xSeek(id, end);

  return rc;
}

/*
** Sync the file. First flush the write-cache to disk, then call the
** real sync() function.
*/
static int crashSync(OsFile *id, int dataOnly){
  int rc;
  /* printf("SYNC %s (%d blocks)\n", (*id)->zName, (*id)->nBlk); */
  rc = writeCache((OsTestFile*)id->pAux);
  if( rc!=SQLITE_OK ) return rc;
  rc = origIo.xSync(id, dataOnly);
  return rc;
}

/*
** Truncate the file. Set the internal OsFile.nMaxWrite variable to the new
** file size to ensure that nothing in the write-cache past this point
** is written to disk.
*/
static int crashTruncate(OsFile *id, i64 nByte){
  OsTestFile *pFile = (OsTestFile*)id->pAux;
  pFile->nMaxWrite = nByte;
  return origIo.xTruncate(id, nByte);
}

/*
** Return the size of the file. If the cache contains a write that extended
** the file, then return this size instead of the on-disk size.
*/
static int crashFileSize(OsFile *id, i64 *pSize){
  int rc = origIo.xFileSize(id, pSize);
  OsTestFile *pFile = (OsTestFile*)id->pAux;
  if( rc==SQLITE_OK && pSize && *pSize<pFile->nMaxWrite ){
    *pSize = pFile->nMaxWrite;
  }
  return rc;
}

/*
** The three functions used to open files. All that is required is to
** initialise the os_test.c specific fields and then call the corresponding
** os_unix.c function to really open the file.
*/
static int crashOpenReadWrite(const char *zFilename, OsFile *id, int *pRdonly){
  initFile(id, zFilename);
  return origIo.xOpenReadWrite(zFilename, id, pRdonly);


}


static int crashOpenExclusive(const char *zFilename, OsFile *id, int delFlag){
  initFile(id, zFilename);

  return origIo.xOpenExclusive(zFilename, id, delFlag);


}


static int crashOpenReadOnly(const char *zFilename, OsFile *id){



  initFile(id, zFilename);































  return origIo.xOpenReadOnly(zFilename, id);
}

/*
** Make a copy of an OsFile object
*/
static void crashCopyOsFile(OsFile *pDest, OsFile *pSrc){
  *pDest = *pSrc;
  ((OsTestFile*)(pDest->pAux))->pBase = pDest;
}

/*
** tclcmd:   sqlite_crashparams DELAY CRASHFILE
**
** This procedure implements a TCL command that enables crash testing
** in testfixture.  Once enabled, crash testing cannot be disabled.
................................................................................
  zFile = Tcl_GetStringFromObj(objv[2], &nFile);
  if( nFile>=sizeof(zCrashFile)-1 ){
    Tcl_AppendResult(interp, "crash file name too big", 0);
    return TCL_ERROR;
  }
  setCrashParams(delay, zFile);
  origIo = sqlite3Io;










  sqlite3Io.xRead = crashRead;
  sqlite3Io.xWrite = crashWrite;
  sqlite3Io.xClose = crashClose;
  sqlite3Io.xSync = crashSync;
  sqlite3Io.xTruncate = crashTruncate;
  sqlite3Io.xFileSize = crashFileSize;
  sqlite3Io.xOpenReadWrite = crashOpenReadWrite;
  sqlite3Io.xOpenExclusive = crashOpenExclusive;
  sqlite3Io.xOpenReadOnly = crashOpenReadOnly;

  sqlite3Io.xSet





  return TCL_OK;
}

#endif
/*
** This procedure registers the TCL procedures defined in this file.
*/
int Sqlitetest6_Init(Tcl_Interp *interp){
#if 0
  Tcl_CreateObjCommand(interp, "sqlite3_crashparams", crashParamsObjCmd, 0, 0);
#endif
  return TCL_OK;
}

#endif /* SQLITE_TEST */







<













>







 







|




|


>




|





|


|
|









|
>



|
<

<
<
<
<
<
<
<
|







 







<


<







 







<
<
<
<










|







 







|
|
|
|
|
|

|
>
>
>
>
>










|

|







 







|

|







|










<

|










|



|












|
|


<
<
>










|
|
<









<
|
|







|
<
|
|









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



|

|
|
<







 







>
>
>
>
>
>
>
>
>
>


|



<
<
<
>
|
>
>
>
>
>



<




<

<




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
..
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
...
177
178
179
180
181
182
183

184
185

186
187
188
189
190
191
192
...
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
...
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
...
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
392
393
394

395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414

415
416
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
...
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
** the effect on the database file of an OS crash or power failure.  This
** is used to test the ability of SQLite to recover from those situations.
*/
#if SQLITE_TEST          /* This file is used for the testing only */
#include "sqliteInt.h"
#include "os.h"
#include "tcl.h"


/*
** A copy of the original sqlite3Io structure
*/
static struct sqlite3IoVtbl origIo;

/*
** The OsFile structure for the crash-test backend.  The pBase field
** points to an OsFile structure for the native backend.
*/
struct OsFile {
  u8 **apBlk;         /* Array of blocks that have been written to. */
  int nBlk;           /* Size of apBlock. */
  i64 offset;         /* Next character to be read from the file */
  int nMaxWrite;      /* Largest offset written to. */
  char *zName;        /* File name */
  OsFile *pBase;      /* Base class */
  OsFile *pNext;      /* Next in a list of them all */
};

/*
................................................................................
  sqlite3OsLeaveMutex();
  return r;
}

/*
** A list of all open files.
*/
static OsFile *pAllFiles = 0;

/*
** Initialise the os_test.c specific fields of pFile.
*/
static void initFile(OsFile **pId, char const *zName, OsFile *pBase){
  OsFile *pFile = *pId = sqliteMalloc(sizeof(OsFile) + strlen(zName)+1);
  pFile->nMaxWrite = 0; 
  pFile->offset = 0;
  pFile->nBlk = 0; 
  pFile->apBlk = 0; 
  pFile->zName = (char *)(&pFile[1]);
  strcpy(pFile->zName, zName);
  pFile->pBase = pBase;
  pFile->pNext = pAllFiles;
  pAllFiles = pFile;
}

/*
** Undo the work done by initFile. Delete the OsFile structure
** and unlink the structure from the pAllFiles list.
*/
static void closeFile(OsFile **pId){
  OsFile *pFile = *pId;
  if( pFile==pAllFiles ){
    pAllFiles = pFile->pNext;
  }else{
    OsFile *p;
    for(p=pAllFiles; p->pNext!=pFile; p=p->pNext ){
      assert( p );
    }
    p->pNext = pFile->pNext;
  }
  sqliteFree(*pId);
  *pId = 0;
}

/*
** Read block 'blk' off of the real disk file and into the cache of pFile.

*/







static int readBlockIntoCache(OsFile *pFile, int blk){
  if( blk>=pFile->nBlk ){
    int n = ((pFile->nBlk * 2) + 100 + blk);
    /* if( pFile->nBlk==0 ){ printf("DIRTY %s\n", pFile->zName); } */
    pFile->apBlk = (u8 **)sqliteRealloc(pFile->apBlk, n * sizeof(u8*));
    if( !pFile->apBlk ) return SQLITE_NOMEM;
    memset(&pFile->apBlk[pFile->nBlk], 0, (n - pFile->nBlk)*sizeof(u8*));
    pFile->nBlk = n;
................................................................................
/*
** Write the cache of pFile to disk. If crash is non-zero, randomly
** skip blocks when writing. The cache is deleted before returning.
*/
static int writeCache2(OsFile *pFile, int crash){
  int i;
  int nMax = pFile->nMaxWrite;

  int rc = SQLITE_OK;


  for(i=0; i<pFile->nBlk; i++){
    u8 *p = pFile->apBlk[i];
    if( p ){
      int skip = 0;
      int trash = 0;
      if( crash ){
        char random;
................................................................................
      sqliteFree(p);
    }
  }
  sqliteFree(pFile->apBlk);
  pFile->nBlk = 0;
  pFile->apBlk = 0;
  pFile->nMaxWrite = 0;




  return rc;
}

/*
** Write the cache to disk.
*/
static int writeCache(OsFile *pFile){
  if( pFile->apBlk ){
    int c = crashRequired(pFile->zName);
    if( c ){
      OsFile *p;
#ifdef TRACE_WRITECACHE
      printf("\nCrash during sync of %s\n", pFile->zName);
#endif
      for(p=pAllFiles; p; p=p->pNext){
        writeCache2(p, 1);
      }
      exit(-1);
................................................................................
  }
  return SQLITE_OK;
}

/*
** Close the file.
*/
static int crashClose(OsFile **pId){
  OsFile *pFile = *pId;
  if( pFile ){
    /* printf("CLOSE %s (%d blocks)\n", pFile->zName, pFile->nBlk); */
    writeCache(pFile);
    origIo.xClose(&pFile->pBase);
  }
  closeFile(pId);
  return SQLITE_OK;
}

static int crashSeek(OsFile *id, i64 offset){
  id->offset = offset;
  return SQLITE_OK;
}

static int crashRead(OsFile *id, void *pBuf, int amt){
  i64 offset;       /* The current offset from the start of the file */
  i64 end;          /* The byte just past the last byte read */
  int blk;            /* Block number the read starts on */
  int i;
  u8 *zCsr;
  int rc = SQLITE_OK;
  OsFile *pFile = id;

  offset = pFile->offset;
  end = offset+amt;
  blk = (offset/BLOCKSIZE);

  zCsr = (u8 *)pBuf;
  for(i=blk; i*BLOCKSIZE<end; i++){
    int off = 0;
    int len = 0;
................................................................................
      len = len - (BLOCK_OFFSET(i+1)-end);
    }

    if( i<pFile->nBlk && pFile->apBlk[i]){
      u8 *pBlk = pFile->apBlk[i];
      memcpy(zCsr, &pBlk[off], len);
    }else{
      rc = origIo.xSeek(id->pBase, BLOCK_OFFSET(i) + off);
      if( rc!=SQLITE_OK ) return rc;
      rc = origIo.xRead(id->pBase, zCsr, len);
      if( rc!=SQLITE_OK ) return rc;
    }

    zCsr += len;
  }
  assert( zCsr==&((u8 *)pBuf)[amt] );

  id->offset = end;
  return rc;
}

static int crashWrite(OsFile *id, const void *pBuf, int amt){
  i64 offset;       /* The current offset from the start of the file */
  i64 end;          /* The byte just past the last byte written */
  int blk;            /* Block number the write starts on */
  int i;
  const u8 *zCsr;
  int rc = SQLITE_OK;


  offset = id->offset;
  end = offset+amt;
  blk = (offset/BLOCKSIZE);

  zCsr = (u8 *)pBuf;
  for(i=blk; i*BLOCKSIZE<end; i++){
    u8 *pBlk;
    int off = 0;
    int len = 0;

    /* Make sure the block is in the cache */
    rc = readBlockIntoCache(id, i);
    if( rc!=SQLITE_OK ) return rc;

    /* Write into the cache */
    pBlk = id->apBlk[i];
    assert( pBlk );

    if( BLOCK_OFFSET(i) < offset ){
      off = offset-BLOCK_OFFSET(i);
    }
    len = BLOCKSIZE - off;
    if( BLOCK_OFFSET(i+1) > end ){
      len = len - (BLOCK_OFFSET(i+1)-end);
    }
    memcpy(&pBlk[off], zCsr, len);
    zCsr += len;
  }
  if( id->nMaxWrite<end ){
    id->nMaxWrite = end;
  }
  assert( zCsr==&((u8 *)pBuf)[amt] );


  id->offset = end;
  return rc;
}

/*
** Sync the file. First flush the write-cache to disk, then call the
** real sync() function.
*/
static int crashSync(OsFile *id, int dataOnly){
  int rc;
  /* printf("SYNC %s (%d blocks)\n", (*id)->zName, (*id)->nBlk); */
  rc = writeCache(id);
  /* if( rc!=SQLITE_OK ) return rc; rc = origIo.xSync(id->pBase, dataOnly); */

  return rc;
}

/*
** Truncate the file. Set the internal OsFile.nMaxWrite variable to the new
** file size to ensure that nothing in the write-cache past this point
** is written to disk.
*/
static int crashTruncate(OsFile *id, i64 nByte){

  id->nMaxWrite = nByte;
  return origIo.xTruncate(id->pBase, nByte);
}

/*
** Return the size of the file. If the cache contains a write that extended
** the file, then return this size instead of the on-disk size.
*/
static int crashFileSize(OsFile *id, i64 *pSize){
  int rc = origIo.xFileSize(id->pBase, pSize);

  if( rc==SQLITE_OK && pSize && *pSize<id->nMaxWrite ){
    *pSize = id->nMaxWrite;
  }
  return rc;
}

/*
** The three functions used to open files. All that is required is to
** initialise the os_test.c specific fields and then call the corresponding
** os_unix.c function to really open the file.
*/
static int crashOpenReadWrite(const char *zFilename, OsFile **pId,int *pRdonly){
  OsFile *pBase = 0;
  int rc = origIo.xOpenReadWrite(zFilename, &pBase, pRdonly);
  if( !rc ){
    initFile(pId, zFilename, pBase);
  }
  return rc;
}
static int crashOpenExclusive(const char *zFilename, OsFile **pId, int delFlag){

  OsFile *pBase = 0;
  int rc = origIo.xOpenExclusive(zFilename, &pBase, delFlag);
  if( !rc ){
    initFile(pId, zFilename, pBase);
  }
  return rc;
}
static int crashOpenReadOnly(const char *zFilename, OsFile **pId){
  OsFile *pBase = 0;
  int rc = origIo.xOpenReadOnly(zFilename, &pBase);
  if( !rc ){
    initFile(pId, zFilename, pBase);
  }
  return rc;
}

/*
** OpenDirectory and SyncDirectory are no-ops
*/
static int crashOpenDir(const char *zName, OsFile *id){
  return SQLITE_OK;
}
static int crashSyncDir(const char *zName){
  return SQLITE_OK;
}

/*
** Locking primitives are passed through into the underlying
** file descriptor.
*/
int crashLock(OsFile *id, int lockType){
  return origIo.xLock(id->pBase, lockType);
}
int crashUnlock(OsFile *id, int lockType){
  return origIo.xUnlock(id->pBase, lockType);
}
int crashCheckReservedLock(OsFile *id){
  return origIo.xCheckReservedLock(id->pBase);
}
void crashSetFullSync(OsFile *id, int setting){
  return;  /* This is a no-op */
}
int crashLockState(OsFile *id){
  return origIo.xLockState(id->pBase);
}

/*
** Return the underlying file handle.
*/
int crashFileHandle(OsFile *id){
  return origIo.xFileHandle(id->pBase);

}

/*
** tclcmd:   sqlite_crashparams DELAY CRASHFILE
**
** This procedure implements a TCL command that enables crash testing
** in testfixture.  Once enabled, crash testing cannot be disabled.
................................................................................
  zFile = Tcl_GetStringFromObj(objv[2], &nFile);
  if( nFile>=sizeof(zCrashFile)-1 ){
    Tcl_AppendResult(interp, "crash file name too big", 0);
    return TCL_ERROR;
  }
  setCrashParams(delay, zFile);
  origIo = sqlite3Io;
  /* xDelete unchanged */
  /* xFileExists unchanged */
  sqlite3Io.xOpenReadWrite = crashOpenReadWrite;
  sqlite3Io.xOpenExclusive = crashOpenExclusive;
  sqlite3Io.xOpenReadOnly = crashOpenReadOnly;
  sqlite3Io.xOpenDirectory = crashOpenDir;
  sqlite3Io.xSyncDirectory = crashSyncDir;
  /* xTempFileName unchanged */
  /* xIsDirWritable unchanged */
  sqlite3Io.xClose = crashClose;
  sqlite3Io.xRead = crashRead;
  sqlite3Io.xWrite = crashWrite;
  sqlite3Io.xSeek = crashSeek;
  sqlite3Io.xSync = crashSync;
  sqlite3Io.xTruncate = crashTruncate;
  sqlite3Io.xFileSize = crashFileSize;



  /* xFullPathname unchanged */
  sqlite3Io.xLock = crashLock;
  sqlite3Io.xUnlock = crashUnlock;
  sqlite3Io.xCheckReservedLock = crashCheckReservedLock;
  sqlite3Io.xSetFullSync = crashSetFullSync;
  sqlite3Io.xFileHandle = crashFileHandle;
  sqlite3Io.xLockState = crashLockState;
  return TCL_OK;
}


/*
** This procedure registers the TCL procedures defined in this file.
*/
int Sqlitetest6_Init(Tcl_Interp *interp){

  Tcl_CreateObjCommand(interp, "sqlite3_crashparams", crashParamsObjCmd, 0, 0);

  return TCL_OK;
}

#endif /* SQLITE_TEST */