/ Check-in [0d258956]
Login

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

Overview
Comment:Modify test_journal.c to verify the page data being written to the journal file. (CVS 6123)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:0d258956f8971c0af7853b836a7d6e7f3a800c37
User & Date: danielk1977 2009-01-06 17:52:44
Context
2009-01-06
18:21
Reduce the number of paths in btreeCopyFile(). (CVS 6124) check-in: df2c285c user: danielk1977 tags: trunk
17:52
Modify test_journal.c to verify the page data being written to the journal file. (CVS 6123) check-in: 0d258956 user: danielk1977 tags: trunk
15:58
Cleanup of the PAGERTRACE macro. Other comment changes in pager.c. (CVS 6122) check-in: ee7b4b60 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/test_journal.c.

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
..
43
44
45
46
47
48
49




50
51
52
53
54
55
56
...
132
133
134
135
136
137
138

139

140
141
142
143
144
145
146
...
184
185
186
187
188
189
190






















191
192
193
194
195
196
197
...
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
...
298
299
300
301
302
303
304
305
306
307
308
309

310




311

312
313
314
315
316
317
318
...
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
...
469
470
471
472
473
474
475

476
477
478
479
480
481
482
** bug in SQLite - writing data to a database file page when:
**
**   a) the original page data is not stored in a synced portion of the
**      journal file, and
**   b) the page was not a free-list leaf page when the transaction was
**      first opened.
**
** $Id: test_journal.c,v 1.5 2009/01/06 14:34:35 danielk1977 Exp $
*/
#if SQLITE_TEST          /* This file is used for testing only */

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

/*
................................................................................
  int flags;               /* Flags the file was opened with */

  /* The following are only used by database file file handles */
  int eLock;               /* Current lock held on the file */
  u32 nPage;               /* Size of file in pages when transaction started */
  u32 nPagesize;           /* Page size when transaction started */
  Bitvec *pWritable;       /* Bitvec of pages that may be written to the file */





  jt_file *pNext;          /* All files are stored in a linked list */
  sqlite3_file *pReal;     /* The file handle for the underlying vfs */
};

/*
** Method declarations for jt_file.
................................................................................
  sqlite3_vfs *pVfs;
  jt_file *pList;
};
static struct JtGlobal g = {0, 0};

static void closeTransaction(jt_file *p){
  sqlite3BitvecDestroy(p->pWritable);

  p->pWritable = 0;

}

/*
** Close an jt-file.
*/
static int jtClose(sqlite3_file *pFile){
  jt_file **pp;
................................................................................
  return pMain;
}


static u32 decodeUint32(const unsigned char *z){
  return (z[0]<<24) + (z[1]<<16) + (z[2]<<8) + z[3];
}























static int readFreelist(jt_file *pMain){
  int rc;
  sqlite3_file *p = pMain->pReal;
  sqlite3_int64 iSize;

  rc = sqlite3OsFileSize(p, &iSize);
................................................................................
static int jtWrite(
  sqlite3_file *pFile, 
  const void *zBuf, 
  int iAmt, 
  sqlite_int64 iOfst
){
  jt_file *p = (jt_file *)pFile;
  if( p->flags&SQLITE_OPEN_MAIN_JOURNAL && iOfst==0 ){

    jt_file *pMain = locateDatabaseHandle(p->zName);
    assert( pMain );

    if( decodeJournalHdr(zBuf, 0, &pMain->nPage, 0, &pMain->nPagesize) ){
      /* Zeroing the first journal-file header. This is the end of a
      ** transaction. */
      closeTransaction(pMain);
    }else{
      /* Writing the first journal header to a journal file. This happens
      ** when a transaction is first started.  */
      int rc;
      pMain->pWritable = sqlite3BitvecCreate(pMain->nPage);


      if( !pMain->pWritable ){
	return SQLITE_IOERR_NOMEM;
      }
      rc = readFreelist(pMain);
      if( rc!=SQLITE_OK ){
        return rc;
      }








    }
  }

  if( p->flags&SQLITE_OPEN_MAIN_DB && p->pWritable && iAmt==p->nPagesize ){
    u32 pgno = iOfst/p->nPagesize + 1;
    assert( pgno>p->nPage || sqlite3BitvecTest(p->pWritable, pgno) );
  }
................................................................................

/*
** The first argument to this function is a handle open on a journal file.
** This function reads the journal file and adds the page number for each
** page in the journal to the Bitvec object passed as the second argument.
*/
static int readJournalFile(jt_file *p, jt_file *pMain){
  int rc;
  unsigned char zBuf[28];
  sqlite3_file *pReal = p->pReal;
  sqlite3_int64 iOff = 0;
  sqlite3_int64 iSize = 0;






  rc = sqlite3OsFileSize(p->pReal, &iSize);

  while( rc==SQLITE_OK && iOff<iSize ){
    u32 nRec, nPage, nSector, nPagesize;
    u32 ii;
    rc = sqlite3OsRead(pReal, zBuf, 28, iOff);
    if( rc!=SQLITE_OK 
     || decodeJournalHdr(zBuf, &nRec, &nPage, &nSector, &nPagesize) 
    ){
................................................................................
    }
    iOff += nSector;
    if( nRec==0 ){
      /* A trick. There might be another journal-header immediately 
      ** following this one. In this case, 0 records means 0 records, 
      ** not "read until the end of the file". See also ticket #2565.
      */
      if( iSize>=(nRec+nSector) ){
        rc = sqlite3OsRead(pReal, zBuf, 28, iOff);
        if( rc!=SQLITE_OK || 0==decodeJournalHdr(zBuf, 0, 0, 0, 0) ){
          continue;
        }
      }
      nRec = (iSize - iOff)/(pMain->nPagesize + 8);
    }
    for(ii=0; rc==SQLITE_OK && ii<nRec && iOff<iSize; ii++){
      u32 pgno;
      rc = sqlite3OsRead(pReal, zBuf, 4, iOff);
      if( rc==SQLITE_OK ){
        pgno = decodeUint32(zBuf);
        iOff += (8 + pMain->nPagesize);
        if( pgno>0 && pgno<=pMain->nPage ){











          sqlite3BitvecSet(pMain->pWritable, pgno);
        }

      }
    }

    iOff = ((iOff + (nSector-1)) / nSector) * nSector;
  }

finish_rjf:

  if( rc==SQLITE_IOERR_SHORT_READ ){
    rc = SQLITE_OK;
  }
  return rc;
}

/*
................................................................................
  if( rc==SQLITE_OK ){
    pFile->pMethods = &jt_io_methods;
    p->eLock = 0;
    p->zName = zName;
    p->flags = flags;
    p->pNext = 0;
    p->pWritable = 0;

    if( zName ){
      p->pNext = g.pList;
      g.pList = p;
    }
  }
  return rc;
}







|







 







>
>
>
>







 







>

>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







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







 







|




>

>
>
>
>
|
>







 







|





|






<

>
>
>
>
>
>
>
>
>
>
>


>







>







 







>







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
..
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
...
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
...
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
...
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
...
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
...
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
** bug in SQLite - writing data to a database file page when:
**
**   a) the original page data is not stored in a synced portion of the
**      journal file, and
**   b) the page was not a free-list leaf page when the transaction was
**      first opened.
**
** $Id: test_journal.c,v 1.6 2009/01/06 17:52:44 danielk1977 Exp $
*/
#if SQLITE_TEST          /* This file is used for testing only */

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

/*
................................................................................
  int flags;               /* Flags the file was opened with */

  /* The following are only used by database file file handles */
  int eLock;               /* Current lock held on the file */
  u32 nPage;               /* Size of file in pages when transaction started */
  u32 nPagesize;           /* Page size when transaction started */
  Bitvec *pWritable;       /* Bitvec of pages that may be written to the file */
  u32 *aCksum;             /* Checksum for first nPage pages */

  /* Only used by journal file-handles */
  sqlite3_int64 iMaxOff;   /* Maximum offset written to this transaction */

  jt_file *pNext;          /* All files are stored in a linked list */
  sqlite3_file *pReal;     /* The file handle for the underlying vfs */
};

/*
** Method declarations for jt_file.
................................................................................
  sqlite3_vfs *pVfs;
  jt_file *pList;
};
static struct JtGlobal g = {0, 0};

static void closeTransaction(jt_file *p){
  sqlite3BitvecDestroy(p->pWritable);
  sqlite3_free(p->aCksum);
  p->pWritable = 0;
  p->aCksum = 0;
}

/*
** Close an jt-file.
*/
static int jtClose(sqlite3_file *pFile){
  jt_file **pp;
................................................................................
  return pMain;
}


static u32 decodeUint32(const unsigned char *z){
  return (z[0]<<24) + (z[1]<<16) + (z[2]<<8) + z[3];
}

static int calculateCksums(jt_file *pMain){
  int rc = SQLITE_OK;
  int ii, jj;
  unsigned char *aData = sqlite3_malloc(pMain->nPagesize);
  if( !aData ){
    return SQLITE_IOERR_NOMEM;
  }

  for(ii=0; rc==SQLITE_OK && ii<pMain->nPage; ii++){
    u32 cksum = 0;
    sqlite_int64 iOff = (sqlite3_int64)(pMain->nPagesize) * (sqlite3_int64)ii;
    rc = sqlite3OsRead(pMain->pReal, aData, pMain->nPagesize, iOff);
    for(jj=0; jj<pMain->nPagesize; jj++){
      cksum = cksum + aData[jj] + (cksum<<3);
    }
    pMain->aCksum[ii] = cksum;
  }
  sqlite3_free(aData);

  return rc;
}

static int readFreelist(jt_file *pMain){
  int rc;
  sqlite3_file *p = pMain->pReal;
  sqlite3_int64 iSize;

  rc = sqlite3OsFileSize(p, &iSize);
................................................................................
static int jtWrite(
  sqlite3_file *pFile, 
  const void *zBuf, 
  int iAmt, 
  sqlite_int64 iOfst
){
  jt_file *p = (jt_file *)pFile;
  if( p->flags&SQLITE_OPEN_MAIN_JOURNAL ){
    if( iOfst==0 ){
      jt_file *pMain = locateDatabaseHandle(p->zName);
      assert( pMain );
  
      if( decodeJournalHdr(zBuf, 0, &pMain->nPage, 0, &pMain->nPagesize) ){
        /* Zeroing the first journal-file header. This is the end of a
        ** transaction. */
        closeTransaction(pMain);
      }else{
        /* Writing the first journal header to a journal file. This happens
        ** when a transaction is first started.  */
        int rc;
        pMain->pWritable = sqlite3BitvecCreate(pMain->nPage);
        pMain->aCksum = sqlite3_malloc(sizeof(u32) * (pMain->nPage + 1));
        p->iMaxOff = 0;
        if( !pMain->pWritable || !pMain->aCksum ){
  	return SQLITE_IOERR_NOMEM;
        }
        rc = readFreelist(pMain);
        if( rc!=SQLITE_OK ){
          return rc;
        }
        rc = calculateCksums(pMain);
        if( rc!=SQLITE_OK ){
          return rc;
        }
      }
    }
    if( p->iMaxOff<(iOfst + iAmt) ){
      p->iMaxOff = iOfst + iAmt;
    }
  }

  if( p->flags&SQLITE_OPEN_MAIN_DB && p->pWritable && iAmt==p->nPagesize ){
    u32 pgno = iOfst/p->nPagesize + 1;
    assert( pgno>p->nPage || sqlite3BitvecTest(p->pWritable, pgno) );
  }
................................................................................

/*
** The first argument to this function is a handle open on a journal file.
** This function reads the journal file and adds the page number for each
** page in the journal to the Bitvec object passed as the second argument.
*/
static int readJournalFile(jt_file *p, jt_file *pMain){
  int rc = SQLITE_OK;
  unsigned char zBuf[28];
  sqlite3_file *pReal = p->pReal;
  sqlite3_int64 iOff = 0;
  sqlite3_int64 iSize = 0;
  unsigned char *aPage;

  aPage = sqlite3_malloc(pMain->nPagesize);
  if( !aPage ){
    return SQLITE_IOERR_NOMEM;
  }
  /* rc = sqlite3OsFileSize(p->pReal, &iSize); */
  iSize = p->iMaxOff;
  while( rc==SQLITE_OK && iOff<iSize ){
    u32 nRec, nPage, nSector, nPagesize;
    u32 ii;
    rc = sqlite3OsRead(pReal, zBuf, 28, iOff);
    if( rc!=SQLITE_OK 
     || decodeJournalHdr(zBuf, &nRec, &nPage, &nSector, &nPagesize) 
    ){
................................................................................
    }
    iOff += nSector;
    if( nRec==0 ){
      /* A trick. There might be another journal-header immediately 
      ** following this one. In this case, 0 records means 0 records, 
      ** not "read until the end of the file". See also ticket #2565.
      */
      if( iSize>=(iOff+nSector) ){
        rc = sqlite3OsRead(pReal, zBuf, 28, iOff);
        if( rc!=SQLITE_OK || 0==decodeJournalHdr(zBuf, 0, 0, 0, 0) ){
          continue;
        }
      }
      nRec = (iSize-iOff) / (pMain->nPagesize+8);
    }
    for(ii=0; rc==SQLITE_OK && ii<nRec && iOff<iSize; ii++){
      u32 pgno;
      rc = sqlite3OsRead(pReal, zBuf, 4, iOff);
      if( rc==SQLITE_OK ){
        pgno = decodeUint32(zBuf);

        if( pgno>0 && pgno<=pMain->nPage ){
          if( 0==sqlite3BitvecTest(pMain->pWritable, pgno) ){
            rc = sqlite3OsRead(pReal, aPage, pMain->nPagesize, iOff+4);
            if( rc==SQLITE_OK ){
              int jj;
              u32 cksum = 0;
              for(jj=0; jj<pMain->nPagesize; jj++){
                cksum = cksum + aPage[jj] + (cksum<<3);
              }
              assert( cksum==pMain->aCksum[pgno-1] );
            }
          }
          sqlite3BitvecSet(pMain->pWritable, pgno);
        }
        iOff += (8 + pMain->nPagesize);
      }
    }

    iOff = ((iOff + (nSector-1)) / nSector) * nSector;
  }

finish_rjf:
  sqlite3_free(aPage);
  if( rc==SQLITE_IOERR_SHORT_READ ){
    rc = SQLITE_OK;
  }
  return rc;
}

/*
................................................................................
  if( rc==SQLITE_OK ){
    pFile->pMethods = &jt_io_methods;
    p->eLock = 0;
    p->zName = zName;
    p->flags = flags;
    p->pNext = 0;
    p->pWritable = 0;
    p->aCksum = 0;
    if( zName ){
      p->pNext = g.pList;
      g.pList = p;
    }
  }
  return rc;
}