1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-18 10:21:03 +03:00

Change the signature of sqlite3PagerPagecount() so that it can return an error code. (CVS 5195)

FossilOrigin-Name: e9f01c01866d302d81bf9ebc484ea6351cbc0f60
This commit is contained in:
danielk1977
2008-06-07 08:58:22 +00:00
parent 8162054b44
commit ad0132dff1
8 changed files with 85 additions and 63 deletions

View File

@@ -9,7 +9,7 @@
** May you share freely, never taking more than you give.
**
*************************************************************************
** $Id: btree.c,v 1.458 2008/05/09 16:57:51 danielk1977 Exp $
** $Id: btree.c,v 1.459 2008/06/07 08:58:22 danielk1977 Exp $
**
** This file implements a external (disk-based) database using BTrees.
** See the header comment on "btreeInt.h" for additional information.
@@ -1654,15 +1654,14 @@ static int lockBtree(BtShared *pBt){
/* Do some checking to help insure the file we opened really is
** a valid database file.
*/
rc = SQLITE_NOTADB;
nPage = sqlite3PagerPagecount(pBt->pPager);
if( nPage<0 ){
rc = SQLITE_IOERR;
rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
if( rc!=SQLITE_OK ){
goto page1_init_failed;
}else if( nPage>0 ){
int pageSize;
int usableSize;
u8 *page1 = pPage1->aData;
rc = SQLITE_NOTADB;
if( memcmp(page1, zMagicHeader, 16)!=0 ){
goto page1_init_failed;
}
@@ -1800,9 +1799,13 @@ static int newDatabase(BtShared *pBt){
MemPage *pP1;
unsigned char *data;
int rc;
int nPage;
assert( sqlite3_mutex_held(pBt->mutex) );
if( sqlite3PagerPagecount(pBt->pPager)>0 ) return SQLITE_OK;
rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
if( rc!=SQLITE_OK || nPage>0 ){
return rc;
}
pP1 = pBt->pPage1;
assert( pP1!=0 );
data = pP1->aData;
@@ -2143,6 +2146,13 @@ static int relocatePage(
return rc;
}
static int pagerPagecount(Pager *pPager){
int rc;
int nPage;
rc = sqlite3PagerPagecount(pPager, &nPage);
return (rc==SQLITE_OK?nPage:-1);
}
/* Forward declaration required by incrVacuumStep(). */
static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
@@ -2168,7 +2178,7 @@ static int incrVacuumStep(BtShared *pBt, Pgno nFin){
assert( sqlite3_mutex_held(pBt->mutex) );
iLastPg = pBt->nTrunc;
if( iLastPg==0 ){
iLastPg = sqlite3PagerPagecount(pBt->pPager);
iLastPg = pagerPagecount(pBt->pPager);
}
if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
@@ -2301,7 +2311,7 @@ static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){
Pgno nFree;
Pgno nPtrmap;
const int pgsz = pBt->pageSize;
Pgno nOrig = sqlite3PagerPagecount(pBt->pPager);
int nOrig = pagerPagecount(pBt->pPager);
if( PTRMAP_ISPAGE(pBt, nOrig) ){
return SQLITE_CORRUPT_BKPT;
@@ -2722,7 +2732,7 @@ static int btreeCursor(
}
}
pCur->pgnoRoot = (Pgno)iTable;
if( iTable==1 && sqlite3PagerPagecount(pBt->pPager)==0 ){
if( iTable==1 && pagerPagecount(pBt->pPager)==0 ){
rc = SQLITE_EMPTY;
goto create_cursor_exception;
}
@@ -2977,7 +2987,7 @@ static int getOverflowPage(
iGuess++;
}
if( iGuess<=sqlite3PagerPagecount(pBt->pPager) ){
if( iGuess<=pagerPagecount(pBt->pPager) ){
rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
if( rc!=SQLITE_OK ){
return rc;
@@ -3962,7 +3972,7 @@ static int allocateBtreePage(
** the entire-list will be searched for that page.
*/
#ifndef SQLITE_OMIT_AUTOVACUUM
if( exact && nearby<=sqlite3PagerPagecount(pBt->pPager) ){
if( exact && nearby<=pagerPagecount(pBt->pPager) ){
u8 eType;
assert( nearby>0 );
assert( pBt->autoVacuum );
@@ -4098,7 +4108,9 @@ static int allocateBtreePage(
iPage = get4byte(&aData[8+closest*4]);
if( !searchList || iPage==nearby ){
*pPgno = iPage;
if( *pPgno>sqlite3PagerPagecount(pBt->pPager) ){
int nPage;
nPage = pagerPagecount(pBt->pPager);
if( *pPgno>nPage ){
/* Free page off the end of the file */
rc = SQLITE_CORRUPT_BKPT;
goto end_allocate_page;
@@ -4127,7 +4139,8 @@ static int allocateBtreePage(
}else{
/* There are no pages on the freelist, so create a new page at the
** end of the file */
*pPgno = sqlite3PagerPagecount(pBt->pPager) + 1;
int nPage = pagerPagecount(pBt->pPager);
*pPgno = nPage + 1;
#ifndef SQLITE_OMIT_AUTOVACUUM
if( pBt->nTrunc ){
@@ -4281,7 +4294,7 @@ static int clearCell(MemPage *pPage, unsigned char *pCell){
assert( ovflPgno==0 || nOvfl>0 );
while( nOvfl-- ){
MemPage *pOvfl;
if( ovflPgno==0 || ovflPgno>sqlite3PagerPagecount(pBt->pPager) ){
if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt->pPager) ){
return SQLITE_CORRUPT_BKPT;
}
@@ -5425,7 +5438,7 @@ static int balance_shallower(MemPage *pPage){
*/
pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
assert( pgnoChild>0 );
assert( pgnoChild<=sqlite3PagerPagecount(pPage->pBt->pPager) );
assert( pgnoChild<=pagerPagecount(pPage->pBt->pPager) );
rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0);
if( rc ) goto end_shallow_balance;
if( pPage->pgno==1 ){
@@ -6003,7 +6016,7 @@ static int clearDatabasePage(
int i;
assert( sqlite3_mutex_held(pBt->mutex) );
if( pgno>sqlite3PagerPagecount(pBt->pPager) ){
if( pgno>pagerPagecount(pBt->pPager) ){
return SQLITE_CORRUPT_BKPT;
}
@@ -6659,7 +6672,7 @@ char *sqlite3BtreeIntegrityCheck(
}
sCheck.pBt = pBt;
sCheck.pPager = pBt->pPager;
sCheck.nPage = sqlite3PagerPagecount(sCheck.pPager);
sCheck.nPage = pagerPagecount(sCheck.pPager);
sCheck.mxErr = mxErr;
sCheck.nErr = 0;
*pnErr = 0;
@@ -6820,8 +6833,8 @@ static int btreeCopyFile(Btree *pTo, Btree *pFrom){
return SQLITE_BUSY;
}
nToPage = sqlite3PagerPagecount(pBtTo->pPager);
nFromPage = sqlite3PagerPagecount(pBtFrom->pPager);
nToPage = pagerPagecount(pBtTo->pPager);
nFromPage = pagerPagecount(pBtFrom->pPager);
iSkip = PENDING_BYTE_PAGE(pBtTo);
/* Variable nNewPage is the number of pages required to store the

View File

@@ -18,7 +18,7 @@
** file simultaneously, or one process from reading the database while
** another is writing.
**
** @(#) $Id: pager.c,v 1.455 2008/06/07 05:19:38 danielk1977 Exp $
** @(#) $Id: pager.c,v 1.456 2008/06/07 08:58:22 danielk1977 Exp $
*/
#ifndef SQLITE_OMIT_DISKIO
#include "sqliteInt.h"
@@ -2461,7 +2461,7 @@ int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
if( mxPage>0 ){
pPager->mxPgno = mxPage;
}
sqlite3PagerPagecount(pPager);
sqlite3PagerPagecount(pPager, 0);
return pPager->mxPgno;
}
@@ -2522,12 +2522,12 @@ int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
** file is 4096 bytes, 5 is returned instead of 4.
*/
int sqlite3PagerPagecount(Pager *pPager){
int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
i64 n = 0;
int rc;
assert( pPager!=0 );
if( pPager->errCode ){
return -1;
return pPager->errCode;
}
if( pPager->dbSize>=0 ){
n = pPager->dbSize;
@@ -2538,7 +2538,7 @@ int sqlite3PagerPagecount(Pager *pPager){
pPager->nRef++;
pager_error(pPager, rc);
pPager->nRef--;
return -1;
return rc;
}
if( n>0 && n<pPager->pageSize ){
n = 1;
@@ -2555,7 +2555,10 @@ int sqlite3PagerPagecount(Pager *pPager){
if( n>pPager->mxPgno ){
pPager->mxPgno = n;
}
return n;
if( pnPage ){
*pnPage = n;
}
return SQLITE_OK;
}
@@ -2697,7 +2700,7 @@ static int pager_wait_on_lock(Pager *pPager, int locktype){
int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
int rc;
assert( pPager->state>=PAGER_SHARED || MEMDB );
sqlite3PagerPagecount(pPager);
sqlite3PagerPagecount(pPager, 0);
if( pPager->errCode ){
rc = pPager->errCode;
return rc;
@@ -3148,12 +3151,11 @@ static int hasHotJournal(Pager *pPager){
}
if( rc==SQLITE_OK && exists && !locked ){
int nPage = sqlite3PagerPagecount(pPager);
if( nPage==0 ){
int nPage;
rc = sqlite3PagerPagecount(pPager, &nPage);
if( rc==SQLITE_OK && nPage==0 ){
sqlite3OsDelete(pVfs, pPager->zJournal, 0);
exists = 0;
}else if( nPage<0 ){
rc = SQLITE_IOERR;
}
}
@@ -3553,7 +3555,7 @@ static int pagerSharedLock(Pager *pPager){
** it can be neglected.
*/
char dbFileVers[sizeof(pPager->dbFileVers)];
sqlite3PagerPagecount(pPager);
sqlite3PagerPagecount(pPager, 0);
if( pPager->errCode ){
rc = pPager->errCode;
@@ -3789,9 +3791,8 @@ static int pagerAcquire(
if( pPager->nExtra>0 ){
memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
}
nMax = sqlite3PagerPagecount(pPager);
if( pPager->errCode ){
rc = pPager->errCode;
rc = sqlite3PagerPagecount(pPager, &nMax);
if( rc!=SQLITE_OK ){
sqlite3PagerUnref(pPg);
return rc;
}
@@ -3949,7 +3950,7 @@ static int pager_open_journal(Pager *pPager){
assert( pPager->state>=PAGER_RESERVED );
assert( pPager->useJournal );
assert( pPager->pInJournal==0 );
sqlite3PagerPagecount(pPager);
sqlite3PagerPagecount(pPager, 0);
pagerLeave(pPager);
pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
pagerEnter(pPager);
@@ -4079,7 +4080,7 @@ int sqlite3PagerBegin(DbPage *pPg, int exFlag){
assert( pPager->nRec==0 );
assert( pPager->origDbSize==0 );
assert( pPager->pInJournal==0 );
sqlite3PagerPagecount(pPager);
sqlite3PagerPagecount(pPager, 0);
pagerLeave(pPager);
pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
pagerEnter(pPager);
@@ -4365,7 +4366,7 @@ int sqlite3PagerWrite(DbPage *pDbPage){
*/
pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
nPageCount = sqlite3PagerPagecount(pPager);
sqlite3PagerPagecount(pPager, (int *)&nPageCount);
if( pPg->pgno>nPageCount ){
nPage = (pPg->pgno - pg1)+1;
}else if( (pg1+nPagePerSector-1)>nPageCount ){

View File

@@ -13,7 +13,7 @@
** subsystem. The page cache subsystem reads and writes a file a page
** at a time and provides a journal for rollback.
**
** @(#) $Id: pager.h,v 1.75 2008/06/06 11:11:26 danielk1977 Exp $
** @(#) $Id: pager.h,v 1.76 2008/06/07 08:58:22 danielk1977 Exp $
*/
#ifndef _PAGER_H_
@@ -85,7 +85,7 @@ DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
int sqlite3PagerRef(DbPage*);
int sqlite3PagerUnref(DbPage*);
int sqlite3PagerWrite(DbPage*);
int sqlite3PagerPagecount(Pager*);
int sqlite3PagerPagecount(Pager*, int*);
int sqlite3PagerTruncate(Pager*,Pgno);
int sqlite3PagerBegin(DbPage*, int exFlag);
int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, Pgno, int);

View File

@@ -13,7 +13,7 @@
** is not included in the SQLite library. It is used for automated
** testing of the SQLite library.
**
** $Id: test2.c,v 1.57 2008/03/21 16:45:48 drh Exp $
** $Id: test2.c,v 1.58 2008/06/07 08:58:22 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "tcl.h"
@@ -305,13 +305,15 @@ static int pager_pagecount(
){
Pager *pPager;
char zBuf[100];
int nPage;
if( argc!=2 ){
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" ID\"", 0);
return TCL_ERROR;
}
pPager = sqlite3TextToPtr(argv[1]);
sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",sqlite3PagerPagecount(pPager));
sqlite3PagerPagecount(pPager, &nPage);
sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nPage);
Tcl_AppendResult(interp, zBuf, 0);
return TCL_OK;
}

View File

@@ -43,7 +43,7 @@
** in this file for details. If in doubt, do not deviate from existing
** commenting and indentation practices when changing or adding code.
**
** $Id: vdbe.c,v 1.747 2008/06/06 15:04:37 drh Exp $
** $Id: vdbe.c,v 1.748 2008/06/07 08:58:22 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
@@ -489,8 +489,17 @@ static void registerTrace(FILE *out, int iReg, Mem *p){
#ifdef SQLITE_DEBUG
static int fileExists(sqlite3 *db, const char *zFile){
int res;
int rc = sqlite3OsAccess(db->pVfs, zFile, SQLITE_ACCESS_EXISTS, &res);
int res = 0;
int rc = SQLITE_OK;
#ifdef SQLITE_TEST
/* If we are currently testing IO errors, then do not call OsAccess() to
** test for the presence of zFile. This is because any IO error that
** occurs here will not be reported, causing the test to fail.
*/
extern int sqlite3_io_error_pending;
if( sqlite3_io_error_pending<=0 )
#endif
rc = sqlite3OsAccess(db->pVfs, zFile, SQLITE_ACCESS_EXISTS, &res);
return (res && rc==SQLITE_OK);
}
#endif
@@ -2636,7 +2645,7 @@ case OP_OpenWrite: {
case SQLITE_OK: {
int flags = sqlite3BtreeFlags(pCur->pCursor);
/* Sanity checking. Only the lower four bits of the flags byte should
** be used. Bit 3 (mask 0x08) is unpreditable. The lower 3 bits
** be used. Bit 3 (mask 0x08) is unpredictable. The lower 3 bits
** (mask 0x07) should be either 5 (intkey+leafdata for tables) or
** 2 (zerodata for indices). If these conditions are not met it can
** only mean that we are dealing with a corrupt database file
@@ -4817,10 +4826,8 @@ case OP_Pagecount: { /* out2-prerelease */
int nPage;
Pager *pPager = sqlite3BtreePager(db->aDb[p1].pBt);
nPage = sqlite3PagerPagecount(pPager);
if( nPage<0 ){
rc = SQLITE_IOERR;
}else{
rc = sqlite3PagerPagecount(pPager, &nPage);
if( rc==SQLITE_OK ){
pOut->flags = MEM_Int;
pOut->u.i = nPage;
}