📄 pager.c
字号:
** If the journal header file appears to be corrupted, SQLITE_DONE is** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes** cannot be read from the journal file an error code is returned.*/static int readJournalHdr( Pager *pPager, i64 journalSize, u32 *pNRec, u32 *pDbSize){ int rc; unsigned char aMagic[8]; /* A buffer to hold the magic header */ i64 jrnlOff; seekJournalHdr(pPager); if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){ return SQLITE_DONE; } jrnlOff = pPager->journalOff; rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff); if( rc ) return rc; jrnlOff += sizeof(aMagic); if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){ return SQLITE_DONE; } rc = read32bits(pPager->jfd, jrnlOff, pNRec); if( rc ) return rc; rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit); if( rc ) return rc; rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize); if( rc ) return rc; /* Update the assumed sector-size to match the value used by ** the process that created this journal. If this journal was ** created by a process other than this one, then this routine ** is being called from within pager_playback(). The local value ** of Pager.sectorSize is restored at the end of that routine. */ rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize); if( rc ) return rc; pPager->journalOff += JOURNAL_HDR_SZ(pPager); return SQLITE_OK;}/*** Write the supplied master journal name into the journal file for pager** pPager at the current location. The master journal name must be the last** thing written to a journal file. If the pager is in full-sync mode, the** journal file descriptor is advanced to the next sector boundary before** anything is written. The format is:**** + 4 bytes: PAGER_MJ_PGNO.** + N bytes: length of master journal name.** + 4 bytes: N** + 4 bytes: Master journal name checksum.** + 8 bytes: aJournalMagic[].**** The master journal page checksum is the sum of the bytes in the master** journal name.**** If zMaster is a NULL pointer (occurs for a single database transaction), ** this call is a no-op.*/static int writeMasterJournal(Pager *pPager, const char *zMaster){ int rc; int len; int i; i64 jrnlOff; u32 cksum = 0; char zBuf[sizeof(aJournalMagic)+2*4]; if( !zMaster || pPager->setMaster) return SQLITE_OK; pPager->setMaster = 1; len = strlen(zMaster); for(i=0; i<len; i++){ cksum += zMaster[i]; } /* If in full-sync mode, advance to the next disk sector before writing ** the master journal name. This is in case the previous page written to ** the journal has already been synced. */ if( pPager->fullSync ){ seekJournalHdr(pPager); } jrnlOff = pPager->journalOff; pPager->journalOff += (len+20); rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager)); if( rc!=SQLITE_OK ) return rc; jrnlOff += 4; rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff); if( rc!=SQLITE_OK ) return rc; jrnlOff += len; put32bits(zBuf, len); put32bits(&zBuf[4], cksum); memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic)); rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff); pPager->needSync = !pPager->noSync; return rc;}/*** Add or remove a page from the list of all pages that are in the** statement journal.**** The Pager keeps a separate list of pages that are currently in** the statement journal. This helps the sqlite3PagerStmtCommit()** routine run MUCH faster for the common case where there are many** pages in memory but only a few are in the statement journal.*/static void page_add_to_stmt_list(PgHdr *pPg){ Pager *pPager = pPg->pPager; PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); assert( MEMDB ); if( !pHist->inStmt ){ assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 ); if( pPager->pStmt ){ PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg; } pHist->pNextStmt = pPager->pStmt; pPager->pStmt = pPg; pHist->inStmt = 1; }}/*** Find a page in the hash table given its page number. Return** a pointer to the page or NULL if not found.*/static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){ PgHdr *p; if( pPager->aHash==0 ) return 0; p = pPager->aHash[pgno & (pPager->nHash-1)]; while( p && p->pgno!=pgno ){ p = p->pNextHash; } return p;}/*** Clear the in-memory cache. This routine** sets the state of the pager back to what it was when it was first** opened. Any outstanding pages are invalidated and subsequent attempts** to access those pages will likely result in a coredump.*/static void pager_reset(Pager *pPager){ PgHdr *pPg, *pNext; if( pPager->errCode ) return; for(pPg=pPager->pAll; pPg; pPg=pNext){ IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno)); PAGER_INCR(sqlite3_pager_pgfree_count); pNext = pPg->pNextAll; lruListRemove(pPg); sqlite3_free(pPg); } assert(pPager->lru.pFirst==0); assert(pPager->lru.pFirstSynced==0); assert(pPager->lru.pLast==0); pPager->pStmt = 0; pPager->pAll = 0; pPager->pDirty = 0; pPager->nHash = 0; sqlite3_free(pPager->aHash); pPager->nPage = 0; pPager->aHash = 0; pPager->nRef = 0;}/*** Unlock the database file. **** If the pager is currently in error state, discard the contents of ** the cache and reset the Pager structure internal state. If there is** an open journal-file, then the next time a shared-lock is obtained** on the pager file (by this or any other process), it will be** treated as a hot-journal and rolled back.*/static void pager_unlock(Pager *pPager){ if( !pPager->exclusiveMode ){ if( !MEMDB ){ if( pPager->fd->pMethods ){ osUnlock(pPager->fd, NO_LOCK); } pPager->dbSize = -1; IOTRACE(("UNLOCK %p\n", pPager)) /* If Pager.errCode is set, the contents of the pager cache cannot be ** trusted. Now that the pager file is unlocked, the contents of the ** cache can be discarded and the error code safely cleared. */ if( pPager->errCode ){ pPager->errCode = SQLITE_OK; pager_reset(pPager); if( pPager->stmtOpen ){ sqlite3OsClose(pPager->stfd); sqlite3_free(pPager->aInStmt); pPager->aInStmt = 0; } if( pPager->journalOpen ){ sqlite3OsClose(pPager->jfd); pPager->journalOpen = 0; sqlite3_free(pPager->aInJournal); pPager->aInJournal = 0; } pPager->stmtOpen = 0; pPager->stmtInUse = 0; pPager->journalOff = 0; pPager->journalStarted = 0; pPager->stmtAutoopen = 0; pPager->origDbSize = 0; } } if( !MEMDB || pPager->errCode==SQLITE_OK ){ pPager->state = PAGER_UNLOCK; pPager->changeCountDone = 0; } }}/*** Execute a rollback if a transaction is active and unlock the ** database file. If the pager has already entered the error state, ** do not attempt the rollback.*/static void pagerUnlockAndRollback(Pager *p){ assert( p->state>=PAGER_RESERVED || p->journalOpen==0 ); if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){ sqlite3PagerRollback(p); } pager_unlock(p); assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) ); assert( p->errCode || !p->stmtOpen || p->exclusiveMode );}/*** This routine ends a transaction. A transaction is ended by either** a COMMIT or a ROLLBACK.**** When this routine is called, the pager has the journal file open and** a RESERVED or EXCLUSIVE lock on the database. This routine will release** the database lock and acquires a SHARED lock in its place if that is** the appropriate thing to do. Release locks usually is appropriate,** unless we are in exclusive access mode or unless this is a ** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.**** The journal file is either deleted or truncated.**** TODO: Consider keeping the journal file open for temporary databases.** This might give a performance improvement on windows where opening** a file is an expensive operation.*/static int pager_end_transaction(Pager *pPager){ PgHdr *pPg; int rc = SQLITE_OK; int rc2 = SQLITE_OK; assert( !MEMDB ); if( pPager->state<PAGER_RESERVED ){ return SQLITE_OK; } sqlite3PagerStmtCommit(pPager); if( pPager->stmtOpen && !pPager->exclusiveMode ){ sqlite3OsClose(pPager->stfd); pPager->stmtOpen = 0; } if( pPager->journalOpen ){ if( pPager->exclusiveMode && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){; pPager->journalOff = 0; pPager->journalStarted = 0; }else{ sqlite3OsClose(pPager->jfd); pPager->journalOpen = 0; if( rc==SQLITE_OK ){ rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); } } sqlite3_free( pPager->aInJournal ); pPager->aInJournal = 0; for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ pPg->inJournal = 0; pPg->dirty = 0; pPg->needSync = 0; pPg->alwaysRollback = 0;#ifdef SQLITE_CHECK_PAGES pPg->pageHash = pager_pagehash(pPg);#endif } pPager->pDirty = 0; pPager->dirtyCache = 0; pPager->nRec = 0; }else{ assert( pPager->aInJournal==0 ); assert( pPager->dirtyCache==0 || pPager->useJournal==0 ); } if( !pPager->exclusiveMode ){ rc2 = osUnlock(pPager->fd, SHARED_LOCK); pPager->state = PAGER_SHARED; }else if( pPager->state==PAGER_SYNCED ){ pPager->state = PAGER_EXCLUSIVE; } pPager->origDbSize = 0; pPager->setMaster = 0; pPager->needSync = 0; lruListSetFirstSynced(pPager); pPager->dbSize = -1; return (rc==SQLITE_OK?rc2:rc);}/*** Compute and return a checksum for the page of data.**** This is not a real checksum. It is really just the sum of the ** random initial value and the page number. We experimented with** a checksum of the entire data, but that was found to be too slow.**** Note that the page number is stored at the beginning of data and** the checksum is stored at the end. This is important. If journal** corruption occurs due to a power failure, the most likely scenario** is that one end or the other of the record will be changed. It is** much less likely that the two ends of the journal record will be** correct and the middle be corrupt. Thus, this "checksum" scheme,** though fast and simple, catches the mostly likely kind of corruption.**** FIX ME: Consider adding every 200th (or so) byte of the data to the** checksum. That way if a single page spans 3 or more disk sectors and** only the middle sector is corrupt, we will still have a reasonable** chance of failing the checksum and thus detecting the problem.*/static u32 pager_cksum(Pager *pPager, const u8 *aData){ u32 cksum = pPager->cksumInit; int i = pPager->pageSize-200; while( i>0 ){ cksum += aData[i]; i -= 200; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -