Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(568)

Side by Side Diff: third_party/sqlite/sqlite-src-3080704/src/pager.c

Issue 883353008: [sql] Import reference version of SQLite 3.8.7.4. (Closed) Base URL: http://chromium.googlesource.com/chromium/src.git@master
Patch Set: Hold back encoding change which is messing up patch. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 ** 2001 September 15 2 ** 2001 September 15
3 ** 3 **
4 ** The author disclaims copyright to this source code. In place of 4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing: 5 ** a legal notice, here is a blessing:
6 ** 6 **
7 ** May you do good and not evil. 7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others. 8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give. 9 ** May you share freely, never taking more than you give.
10 ** 10 **
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 ** first 100 bytes of the database file. 68 ** first 100 bytes of the database file.
69 ** 69 **
70 ** (5) All writes to the database file are synced prior to the rollback journal 70 ** (5) All writes to the database file are synced prior to the rollback journal
71 ** being deleted, truncated, or zeroed. 71 ** being deleted, truncated, or zeroed.
72 ** 72 **
73 ** (6) If a master journal file is used, then all writes to the database file 73 ** (6) If a master journal file is used, then all writes to the database file
74 ** are synced prior to the master journal being deleted. 74 ** are synced prior to the master journal being deleted.
75 ** 75 **
76 ** Definition: Two databases (or the same database at two points it time) 76 ** Definition: Two databases (or the same database at two points it time)
77 ** are said to be "logically equivalent" if they give the same answer to 77 ** are said to be "logically equivalent" if they give the same answer to
78 ** all queries. Note in particular the the content of freelist leaf 78 ** all queries. Note in particular the content of freelist leaf
79 ** pages can be changed arbitarily without effecting the logical equivalence 79 ** pages can be changed arbitrarily without affecting the logical equivalence
80 ** of the database. 80 ** of the database.
81 ** 81 **
82 ** (7) At any time, if any subset, including the empty set and the total set, 82 ** (7) At any time, if any subset, including the empty set and the total set,
83 ** of the unsynced changes to a rollback journal are removed and the 83 ** of the unsynced changes to a rollback journal are removed and the
84 ** journal is rolled back, the resulting database file will be logical 84 ** journal is rolled back, the resulting database file will be logically
85 ** equivalent to the database file at the beginning of the transaction. 85 ** equivalent to the database file at the beginning of the transaction.
86 ** 86 **
87 ** (8) When a transaction is rolled back, the xTruncate method of the VFS 87 ** (8) When a transaction is rolled back, the xTruncate method of the VFS
88 ** is called to restore the database file to the same size it was at 88 ** is called to restore the database file to the same size it was at
89 ** the beginning of the transaction. (In some VFSes, the xTruncate 89 ** the beginning of the transaction. (In some VFSes, the xTruncate
90 ** method is a no-op, but that does not change the fact the SQLite will 90 ** method is a no-op, but that does not change the fact the SQLite will
91 ** invoke it.) 91 ** invoke it.)
92 ** 92 **
93 ** (9) Whenever the database file is modified, at least one bit in the range 93 ** (9) Whenever the database file is modified, at least one bit in the range
94 ** of bytes from 24 through 39 inclusive will be changed prior to releasing 94 ** of bytes from 24 through 39 inclusive will be changed prior to releasing
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD 266 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
267 ** state after the entire transaction has been successfully written into the 267 ** state after the entire transaction has been successfully written into the
268 ** database file. In this state the transaction may be committed simply 268 ** database file. In this state the transaction may be committed simply
269 ** by finalizing the journal file. Once in WRITER_FINISHED state, it is 269 ** by finalizing the journal file. Once in WRITER_FINISHED state, it is
270 ** not possible to modify the database further. At this point, the upper 270 ** not possible to modify the database further. At this point, the upper
271 ** layer must either commit or rollback the transaction. 271 ** layer must either commit or rollback the transaction.
272 ** 272 **
273 ** * A write transaction is active. 273 ** * A write transaction is active.
274 ** * An EXCLUSIVE or greater lock is held on the database file. 274 ** * An EXCLUSIVE or greater lock is held on the database file.
275 ** * All writing and syncing of journal and database data has finished. 275 ** * All writing and syncing of journal and database data has finished.
276 ** If no error occured, all that remains is to finalize the journal to 276 ** If no error occurred, all that remains is to finalize the journal to
277 ** commit the transaction. If an error did occur, the caller will need 277 ** commit the transaction. If an error did occur, the caller will need
278 ** to rollback the transaction. 278 ** to rollback the transaction.
279 ** 279 **
280 ** ERROR: 280 ** ERROR:
281 ** 281 **
282 ** The ERROR state is entered when an IO or disk-full error (including 282 ** The ERROR state is entered when an IO or disk-full error (including
283 ** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it 283 ** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
284 ** difficult to be sure that the in-memory pager state (cache contents, 284 ** difficult to be sure that the in-memory pager state (cache contents,
285 ** db size etc.) are consistent with the contents of the file-system. 285 ** db size etc.) are consistent with the contents of the file-system.
286 ** 286 **
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 ** VFS call is successful. This way, the Pager.eLock variable may be set 371 ** VFS call is successful. This way, the Pager.eLock variable may be set
372 ** to a less exclusive (lower) value than the lock that is actually held 372 ** to a less exclusive (lower) value than the lock that is actually held
373 ** at the system level, but it is never set to a more exclusive value. 373 ** at the system level, but it is never set to a more exclusive value.
374 ** 374 **
375 ** This is usually safe. If an xUnlock fails or appears to fail, there may 375 ** This is usually safe. If an xUnlock fails or appears to fail, there may
376 ** be a few redundant xLock() calls or a lock may be held for longer than 376 ** be a few redundant xLock() calls or a lock may be held for longer than
377 ** required, but nothing really goes wrong. 377 ** required, but nothing really goes wrong.
378 ** 378 **
379 ** The exception is when the database file is unlocked as the pager moves 379 ** The exception is when the database file is unlocked as the pager moves
380 ** from ERROR to OPEN state. At this point there may be a hot-journal file 380 ** from ERROR to OPEN state. At this point there may be a hot-journal file
381 ** in the file-system that needs to be rolled back (as part of a OPEN->SHARED 381 ** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
382 ** transition, by the same pager or any other). If the call to xUnlock() 382 ** transition, by the same pager or any other). If the call to xUnlock()
383 ** fails at this point and the pager is left holding an EXCLUSIVE lock, this 383 ** fails at this point and the pager is left holding an EXCLUSIVE lock, this
384 ** can confuse the call to xCheckReservedLock() call made later as part 384 ** can confuse the call to xCheckReservedLock() call made later as part
385 ** of hot-journal detection. 385 ** of hot-journal detection.
386 ** 386 **
387 ** xCheckReservedLock() is defined as returning true "if there is a RESERVED 387 ** xCheckReservedLock() is defined as returning true "if there is a RESERVED
388 ** lock held by this process or any others". So xCheckReservedLock may 388 ** lock held by this process or any others". So xCheckReservedLock may
389 ** return true because the caller itself is holding an EXCLUSIVE lock (but 389 ** return true because the caller itself is holding an EXCLUSIVE lock (but
390 ** doesn't know it because of a previous error in xUnlock). If this happens 390 ** doesn't know it because of a previous error in xUnlock). If this happens
391 ** a hot-journal may be mistaken for a journal being created by an active 391 ** a hot-journal may be mistaken for a journal being created by an active
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 i64 iHdrOffset; /* See above */ 447 i64 iHdrOffset; /* See above */
448 Bitvec *pInSavepoint; /* Set of pages in this savepoint */ 448 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
449 Pgno nOrig; /* Original number of pages in file */ 449 Pgno nOrig; /* Original number of pages in file */
450 Pgno iSubRec; /* Index of first record in sub-journal */ 450 Pgno iSubRec; /* Index of first record in sub-journal */
451 #ifndef SQLITE_OMIT_WAL 451 #ifndef SQLITE_OMIT_WAL
452 u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */ 452 u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */
453 #endif 453 #endif
454 }; 454 };
455 455
456 /* 456 /*
457 ** A open page cache is an instance of struct Pager. A description of 457 ** Bits of the Pager.doNotSpill flag. See further description below.
458 */
459 #define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */
460 #define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */
461 #define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */
462
463 /*
464 ** An open page cache is an instance of struct Pager. A description of
458 ** some of the more important member variables follows: 465 ** some of the more important member variables follows:
459 ** 466 **
460 ** eState 467 ** eState
461 ** 468 **
462 ** The current 'state' of the pager object. See the comment and state 469 ** The current 'state' of the pager object. See the comment and state
463 ** diagram above for a description of the pager state. 470 ** diagram above for a description of the pager state.
464 ** 471 **
465 ** eLock 472 ** eLock
466 ** 473 **
467 ** For a real on-disk database, the current lock held on the database file - 474 ** For a real on-disk database, the current lock held on the database file -
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 ** Journal files that contain master journal pointers cannot be finalized 519 ** Journal files that contain master journal pointers cannot be finalized
513 ** simply by overwriting the first journal-header with zeroes, as the 520 ** simply by overwriting the first journal-header with zeroes, as the
514 ** master journal pointer could interfere with hot-journal rollback of any 521 ** master journal pointer could interfere with hot-journal rollback of any
515 ** subsequently interrupted transaction that reuses the journal file. 522 ** subsequently interrupted transaction that reuses the journal file.
516 ** 523 **
517 ** The flag is cleared as soon as the journal file is finalized (either 524 ** The flag is cleared as soon as the journal file is finalized (either
518 ** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the 525 ** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
519 ** journal file from being successfully finalized, the setMaster flag 526 ** journal file from being successfully finalized, the setMaster flag
520 ** is cleared anyway (and the pager will move to ERROR state). 527 ** is cleared anyway (and the pager will move to ERROR state).
521 ** 528 **
522 ** doNotSpill, doNotSyncSpill 529 ** doNotSpill
523 ** 530 **
524 ** These two boolean variables control the behaviour of cache-spills 531 ** This variables control the behavior of cache-spills (calls made by
525 ** (calls made by the pcache module to the pagerStress() routine to 532 ** the pcache module to the pagerStress() routine to write cached data
526 ** write cached data to the file-system in order to free up memory). 533 ** to the file-system in order to free up memory).
527 ** 534 **
528 ** When doNotSpill is non-zero, writing to the database from pagerStress() 535 ** When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
529 ** is disabled altogether. This is done in a very obscure case that 536 ** writing to the database from pagerStress() is disabled altogether.
537 ** The SPILLFLAG_ROLLBACK case is done in a very obscure case that
530 ** comes up during savepoint rollback that requires the pcache module 538 ** comes up during savepoint rollback that requires the pcache module
531 ** to allocate a new page to prevent the journal file from being written 539 ** to allocate a new page to prevent the journal file from being written
532 ** while it is being traversed by code in pager_playback(). 540 ** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF
541 ** case is a user preference.
533 ** 542 **
534 ** If doNotSyncSpill is non-zero, writing to the database from pagerStress() 543 ** If the SPILLFLAG_NOSYNC bit is set, writing to the database from pagerStres s()
535 ** is permitted, but syncing the journal file is not. This flag is set 544 ** is permitted, but syncing the journal file is not. This flag is set
536 ** by sqlite3PagerWrite() when the file-system sector-size is larger than 545 ** by sqlite3PagerWrite() when the file-system sector-size is larger than
537 ** the database page-size in order to prevent a journal sync from happening 546 ** the database page-size in order to prevent a journal sync from happening
538 ** in between the journalling of two pages on the same sector. 547 ** in between the journalling of two pages on the same sector.
539 ** 548 **
540 ** subjInMemory 549 ** subjInMemory
541 ** 550 **
542 ** This is a boolean variable. If true, then any required sub-journal 551 ** This is a boolean variable. If true, then any required sub-journal
543 ** is opened as an in-memory journal file. If false, then in-memory 552 ** is opened as an in-memory journal file. If false, then in-memory
544 ** sub-journals are only used for in-memory pager files. 553 ** sub-journals are only used for in-memory pager files.
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
605 ** The Pager.errCode variable is only ever used in PAGER_ERROR state. It 614 ** The Pager.errCode variable is only ever used in PAGER_ERROR state. It
606 ** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode 615 ** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
607 ** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX 616 ** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
608 ** sub-codes. 617 ** sub-codes.
609 */ 618 */
610 struct Pager { 619 struct Pager {
611 sqlite3_vfs *pVfs; /* OS functions to use for IO */ 620 sqlite3_vfs *pVfs; /* OS functions to use for IO */
612 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */ 621 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
613 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */ 622 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
614 u8 useJournal; /* Use a rollback journal on this file */ 623 u8 useJournal; /* Use a rollback journal on this file */
615 u8 noReadlock; /* Do not bother to obtain readlocks */
616 u8 noSync; /* Do not sync the journal if true */ 624 u8 noSync; /* Do not sync the journal if true */
617 u8 fullSync; /* Do extra syncs of the journal for robustness */ 625 u8 fullSync; /* Do extra syncs of the journal for robustness */
618 u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */ 626 u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
627 u8 walSyncFlags; /* SYNC_NORMAL or SYNC_FULL for wal writes */
619 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */ 628 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
620 u8 tempFile; /* zFilename is a temporary file */ 629 u8 tempFile; /* zFilename is a temporary or immutable file */
630 u8 noLock; /* Do not lock (except in WAL mode) */
621 u8 readOnly; /* True for a read-only database */ 631 u8 readOnly; /* True for a read-only database */
622 u8 memDb; /* True to inhibit all file I/O */ 632 u8 memDb; /* True to inhibit all file I/O */
623 633
624 /************************************************************************** 634 /**************************************************************************
625 ** The following block contains those class members that change during 635 ** The following block contains those class members that change during
626 ** routine opertion. Class members not in this block are either fixed 636 ** routine operation. Class members not in this block are either fixed
627 ** when the pager is first created or else only change when there is a 637 ** when the pager is first created or else only change when there is a
628 ** significant mode change (such as changing the page_size, locking_mode, 638 ** significant mode change (such as changing the page_size, locking_mode,
629 ** or the journal_mode). From another view, these class members describe 639 ** or the journal_mode). From another view, these class members describe
630 ** the "state" of the pager, while other class members describe the 640 ** the "state" of the pager, while other class members describe the
631 ** "configuration" of the pager. 641 ** "configuration" of the pager.
632 */ 642 */
633 u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */ 643 u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
634 u8 eLock; /* Current lock held on database file */ 644 u8 eLock; /* Current lock held on database file */
635 u8 changeCountDone; /* Set after incrementing the change-counter */ 645 u8 changeCountDone; /* Set after incrementing the change-counter */
636 u8 setMaster; /* True if a m-j name has been written to jrnl */ 646 u8 setMaster; /* True if a m-j name has been written to jrnl */
637 u8 doNotSpill; /* Do not spill the cache when non-zero */ 647 u8 doNotSpill; /* Do not spill the cache when non-zero */
638 u8 doNotSyncSpill; /* Do not do a spill that requires jrnl sync */
639 u8 subjInMemory; /* True to use in-memory sub-journals */ 648 u8 subjInMemory; /* True to use in-memory sub-journals */
640 Pgno dbSize; /* Number of pages in the database */ 649 Pgno dbSize; /* Number of pages in the database */
641 Pgno dbOrigSize; /* dbSize before the current transaction */ 650 Pgno dbOrigSize; /* dbSize before the current transaction */
642 Pgno dbFileSize; /* Number of pages in the database file */ 651 Pgno dbFileSize; /* Number of pages in the database file */
643 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */ 652 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
644 int errCode; /* One of several kinds of errors */ 653 int errCode; /* One of several kinds of errors */
645 int nRec; /* Pages journalled since last j-header written */ 654 int nRec; /* Pages journalled since last j-header written */
646 u32 cksumInit; /* Quasi-random value added to every checksum */ 655 u32 cksumInit; /* Quasi-random value added to every checksum */
647 u32 nSubRec; /* Number of records written to sub-journal */ 656 u32 nSubRec; /* Number of records written to sub-journal */
648 Bitvec *pInJournal; /* One bit for each page in the database file */ 657 Bitvec *pInJournal; /* One bit for each page in the database file */
649 sqlite3_file *fd; /* File descriptor for database */ 658 sqlite3_file *fd; /* File descriptor for database */
650 sqlite3_file *jfd; /* File descriptor for main journal */ 659 sqlite3_file *jfd; /* File descriptor for main journal */
651 sqlite3_file *sjfd; /* File descriptor for sub-journal */ 660 sqlite3_file *sjfd; /* File descriptor for sub-journal */
652 i64 journalOff; /* Current write offset in the journal file */ 661 i64 journalOff; /* Current write offset in the journal file */
653 i64 journalHdr; /* Byte offset to previous journal header */ 662 i64 journalHdr; /* Byte offset to previous journal header */
654 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */ 663 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
655 PagerSavepoint *aSavepoint; /* Array of active savepoints */ 664 PagerSavepoint *aSavepoint; /* Array of active savepoints */
656 int nSavepoint; /* Number of elements in aSavepoint[] */ 665 int nSavepoint; /* Number of elements in aSavepoint[] */
657 char dbFileVers[16]; /* Changes whenever database file changes */ 666 char dbFileVers[16]; /* Changes whenever database file changes */
667
668 u8 bUseFetch; /* True to use xFetch() */
669 int nMmapOut; /* Number of mmap pages currently outstanding */
670 sqlite3_int64 szMmap; /* Desired maximum mmap size */
671 PgHdr *pMmapFreelist; /* List of free mmap page headers (pDirty) */
658 /* 672 /*
659 ** End of the routinely-changing class members 673 ** End of the routinely-changing class members
660 ***************************************************************************/ 674 ***************************************************************************/
661 675
662 u16 nExtra; /* Add this many bytes to each in-memory page */ 676 u16 nExtra; /* Add this many bytes to each in-memory page */
663 i16 nReserve; /* Number of unused bytes at end of each page */ 677 i16 nReserve; /* Number of unused bytes at end of each page */
664 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */ 678 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
665 u32 sectorSize; /* Assumed sector size during rollback */ 679 u32 sectorSize; /* Assumed sector size during rollback */
666 int pageSize; /* Number of bytes in a page */ 680 int pageSize; /* Number of bytes in a page */
667 Pgno mxPgno; /* Maximum allowed size of the database */ 681 Pgno mxPgno; /* Maximum allowed size of the database */
668 i64 journalSizeLimit; /* Size limit for persistent journal files */ 682 i64 journalSizeLimit; /* Size limit for persistent journal files */
669 char *zFilename; /* Name of the database file */ 683 char *zFilename; /* Name of the database file */
670 char *zJournal; /* Name of the journal file */ 684 char *zJournal; /* Name of the journal file */
671 int (*xBusyHandler)(void*); /* Function to call when busy */ 685 int (*xBusyHandler)(void*); /* Function to call when busy */
672 void *pBusyHandlerArg; /* Context argument for xBusyHandler */ 686 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
687 int aStat[3]; /* Total cache hits, misses and writes */
673 #ifdef SQLITE_TEST 688 #ifdef SQLITE_TEST
674 int nHit, nMiss; /* Cache hits and missing */ 689 int nRead; /* Database pages read */
675 int nRead, nWrite; /* Database pages read/written */
676 #endif 690 #endif
677 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */ 691 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
678 #ifdef SQLITE_HAS_CODEC 692 #ifdef SQLITE_HAS_CODEC
679 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */ 693 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
680 void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */ 694 void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
681 void (*xCodecFree)(void*); /* Destructor for the codec */ 695 void (*xCodecFree)(void*); /* Destructor for the codec */
682 void *pCodec; /* First argument to xCodec... methods */ 696 void *pCodec; /* First argument to xCodec... methods */
683 #endif 697 #endif
684 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */ 698 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
685 PCache *pPCache; /* Pointer to page cache object */ 699 PCache *pPCache; /* Pointer to page cache object */
686 #ifndef SQLITE_OMIT_WAL 700 #ifndef SQLITE_OMIT_WAL
687 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */ 701 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
688 char *zWal; /* File name for write-ahead log */ 702 char *zWal; /* File name for write-ahead log */
689 #endif 703 #endif
690 }; 704 };
691 705
692 /* 706 /*
707 ** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
708 ** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS
709 ** or CACHE_WRITE to sqlite3_db_status().
710 */
711 #define PAGER_STAT_HIT 0
712 #define PAGER_STAT_MISS 1
713 #define PAGER_STAT_WRITE 2
714
715 /*
693 ** The following global variables hold counters used for 716 ** The following global variables hold counters used for
694 ** testing purposes only. These variables do not exist in 717 ** testing purposes only. These variables do not exist in
695 ** a non-testing build. These variables are not thread-safe. 718 ** a non-testing build. These variables are not thread-safe.
696 */ 719 */
697 #ifdef SQLITE_TEST 720 #ifdef SQLITE_TEST
698 int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */ 721 int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
699 int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */ 722 int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
700 int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */ 723 int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
701 # define PAGER_INCR(v) v++ 724 # define PAGER_INCR(v) v++
702 #else 725 #else
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
750 ** the value of MEMDB will be a constant and the compiler will optimize 773 ** the value of MEMDB will be a constant and the compiler will optimize
751 ** out code that would never execute. 774 ** out code that would never execute.
752 */ 775 */
753 #ifdef SQLITE_OMIT_MEMORYDB 776 #ifdef SQLITE_OMIT_MEMORYDB
754 # define MEMDB 0 777 # define MEMDB 0
755 #else 778 #else
756 # define MEMDB pPager->memDb 779 # define MEMDB pPager->memDb
757 #endif 780 #endif
758 781
759 /* 782 /*
783 ** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
784 ** interfaces to access the database using memory-mapped I/O.
785 */
786 #if SQLITE_MAX_MMAP_SIZE>0
787 # define USEFETCH(x) ((x)->bUseFetch)
788 #else
789 # define USEFETCH(x) 0
790 #endif
791
792 /*
760 ** The maximum legal page number is (2^31 - 1). 793 ** The maximum legal page number is (2^31 - 1).
761 */ 794 */
762 #define PAGER_MAX_PGNO 2147483647 795 #define PAGER_MAX_PGNO 2147483647
763 796
764 /* 797 /*
765 ** The argument to this macro is a file descriptor (type sqlite3_file*). 798 ** The argument to this macro is a file descriptor (type sqlite3_file*).
766 ** Return 0 if it is not open, or non-zero (but not 1) if it is. 799 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
767 ** 800 **
768 ** This is so that expressions can be written as: 801 ** This is so that expressions can be written as:
769 ** 802 **
770 ** if( isOpen(pPager->jfd) ){ ... 803 ** if( isOpen(pPager->jfd) ){ ...
771 ** 804 **
772 ** instead of 805 ** instead of
773 ** 806 **
774 ** if( pPager->jfd->pMethods ){ ... 807 ** if( pPager->jfd->pMethods ){ ...
775 */ 808 */
776 #define isOpen(pFd) ((pFd)->pMethods) 809 #define isOpen(pFd) ((pFd)->pMethods)
777 810
778 /* 811 /*
779 ** Return true if this pager uses a write-ahead log instead of the usual 812 ** Return true if this pager uses a write-ahead log instead of the usual
780 ** rollback journal. Otherwise false. 813 ** rollback journal. Otherwise false.
781 */ 814 */
782 #ifndef SQLITE_OMIT_WAL 815 #ifndef SQLITE_OMIT_WAL
783 static int pagerUseWal(Pager *pPager){ 816 static int pagerUseWal(Pager *pPager){
784 return (pPager->pWal!=0); 817 return (pPager->pWal!=0);
785 } 818 }
786 #else 819 #else
787 # define pagerUseWal(x) 0 820 # define pagerUseWal(x) 0
788 # define pagerRollbackWal(x) 0 821 # define pagerRollbackWal(x) 0
789 # define pagerWalFrames(v,w,x,y,z) 0 822 # define pagerWalFrames(v,w,x,y) 0
790 # define pagerOpenWalIfPresent(z) SQLITE_OK 823 # define pagerOpenWalIfPresent(z) SQLITE_OK
791 # define pagerBeginReadTransaction(z) SQLITE_OK 824 # define pagerBeginReadTransaction(z) SQLITE_OK
792 #endif 825 #endif
793 826
794 #ifndef NDEBUG 827 #ifndef NDEBUG
795 /* 828 /*
796 ** Usage: 829 ** Usage:
797 ** 830 **
798 ** assert( assert_pager_state(pPager) ); 831 ** assert( assert_pager_state(pPager) );
799 ** 832 **
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
852 switch( p->eState ){ 885 switch( p->eState ){
853 case PAGER_OPEN: 886 case PAGER_OPEN:
854 assert( !MEMDB ); 887 assert( !MEMDB );
855 assert( pPager->errCode==SQLITE_OK ); 888 assert( pPager->errCode==SQLITE_OK );
856 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile ); 889 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
857 break; 890 break;
858 891
859 case PAGER_READER: 892 case PAGER_READER:
860 assert( pPager->errCode==SQLITE_OK ); 893 assert( pPager->errCode==SQLITE_OK );
861 assert( p->eLock!=UNKNOWN_LOCK ); 894 assert( p->eLock!=UNKNOWN_LOCK );
862 assert( p->eLock>=SHARED_LOCK || p->noReadlock ); 895 assert( p->eLock>=SHARED_LOCK );
863 break; 896 break;
864 897
865 case PAGER_WRITER_LOCKED: 898 case PAGER_WRITER_LOCKED:
866 assert( p->eLock!=UNKNOWN_LOCK ); 899 assert( p->eLock!=UNKNOWN_LOCK );
867 assert( pPager->errCode==SQLITE_OK ); 900 assert( pPager->errCode==SQLITE_OK );
868 if( !pagerUseWal(pPager) ){ 901 if( !pagerUseWal(pPager) ){
869 assert( p->eLock>=RESERVED_LOCK ); 902 assert( p->eLock>=RESERVED_LOCK );
870 } 903 }
871 assert( pPager->dbSize==pPager->dbOrigSize ); 904 assert( pPager->dbSize==pPager->dbOrigSize );
872 assert( pPager->dbOrigSize==pPager->dbFileSize ); 905 assert( pPager->dbOrigSize==pPager->dbFileSize );
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
983 /* 1016 /*
984 ** Return true if it is necessary to write page *pPg into the sub-journal. 1017 ** Return true if it is necessary to write page *pPg into the sub-journal.
985 ** A page needs to be written into the sub-journal if there exists one 1018 ** A page needs to be written into the sub-journal if there exists one
986 ** or more open savepoints for which: 1019 ** or more open savepoints for which:
987 ** 1020 **
988 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and 1021 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
989 ** * The bit corresponding to the page-number is not set in 1022 ** * The bit corresponding to the page-number is not set in
990 ** PagerSavepoint.pInSavepoint. 1023 ** PagerSavepoint.pInSavepoint.
991 */ 1024 */
992 static int subjRequiresPage(PgHdr *pPg){ 1025 static int subjRequiresPage(PgHdr *pPg){
1026 Pager *pPager = pPg->pPager;
1027 PagerSavepoint *p;
993 Pgno pgno = pPg->pgno; 1028 Pgno pgno = pPg->pgno;
994 Pager *pPager = pPg->pPager;
995 int i; 1029 int i;
996 for(i=0; i<pPager->nSavepoint; i++){ 1030 for(i=0; i<pPager->nSavepoint; i++){
997 PagerSavepoint *p = &pPager->aSavepoint[i]; 1031 p = &pPager->aSavepoint[i];
998 if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){ 1032 if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
999 return 1; 1033 return 1;
1000 } 1034 }
1001 } 1035 }
1002 return 0; 1036 return 0;
1003 } 1037 }
1004 1038
1005 /* 1039 /*
1006 ** Return true if the page is already in the journal file. 1040 ** Return true if the page is already in the journal file.
1007 */ 1041 */
1008 static int pageInJournal(PgHdr *pPg){ 1042 static int pageInJournal(Pager *pPager, PgHdr *pPg){
1009 return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno); 1043 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
1010 } 1044 }
1011 1045
1012 /* 1046 /*
1013 ** Read a 32-bit integer from the given file descriptor. Store the integer 1047 ** Read a 32-bit integer from the given file descriptor. Store the integer
1014 ** that is read in *pRes. Return SQLITE_OK if everything worked, or an 1048 ** that is read in *pRes. Return SQLITE_OK if everything worked, or an
1015 ** error code is something goes wrong. 1049 ** error code is something goes wrong.
1016 ** 1050 **
1017 ** All values are stored on disk as big-endian. 1051 ** All values are stored on disk as big-endian.
1018 */ 1052 */
1019 static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){ 1053 static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1051 ** UNKNOWN_LOCK for an explanation of this. 1085 ** UNKNOWN_LOCK for an explanation of this.
1052 */ 1086 */
1053 static int pagerUnlockDb(Pager *pPager, int eLock){ 1087 static int pagerUnlockDb(Pager *pPager, int eLock){
1054 int rc = SQLITE_OK; 1088 int rc = SQLITE_OK;
1055 1089
1056 assert( !pPager->exclusiveMode || pPager->eLock==eLock ); 1090 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
1057 assert( eLock==NO_LOCK || eLock==SHARED_LOCK ); 1091 assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
1058 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 ); 1092 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
1059 if( isOpen(pPager->fd) ){ 1093 if( isOpen(pPager->fd) ){
1060 assert( pPager->eLock>=eLock ); 1094 assert( pPager->eLock>=eLock );
1061 rc = sqlite3OsUnlock(pPager->fd, eLock); 1095 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
1062 if( pPager->eLock!=UNKNOWN_LOCK ){ 1096 if( pPager->eLock!=UNKNOWN_LOCK ){
1063 pPager->eLock = (u8)eLock; 1097 pPager->eLock = (u8)eLock;
1064 } 1098 }
1065 IOTRACE(("UNLOCK %p %d\n", pPager, eLock)) 1099 IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
1066 } 1100 }
1067 return rc; 1101 return rc;
1068 } 1102 }
1069 1103
1070 /* 1104 /*
1071 ** Lock the database file to level eLock, which must be either SHARED_LOCK, 1105 ** Lock the database file to level eLock, which must be either SHARED_LOCK,
1072 ** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the 1106 ** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
1073 ** Pager.eLock variable to the new locking state. 1107 ** Pager.eLock variable to the new locking state.
1074 ** 1108 **
1075 ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is 1109 ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
1076 ** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK. 1110 ** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
1077 ** See the comment above the #define of UNKNOWN_LOCK for an explanation 1111 ** See the comment above the #define of UNKNOWN_LOCK for an explanation
1078 ** of this. 1112 ** of this.
1079 */ 1113 */
1080 static int pagerLockDb(Pager *pPager, int eLock){ 1114 static int pagerLockDb(Pager *pPager, int eLock){
1081 int rc = SQLITE_OK; 1115 int rc = SQLITE_OK;
1082 1116
1083 assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK ); 1117 assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
1084 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){ 1118 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
1085 rc = sqlite3OsLock(pPager->fd, eLock); 1119 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
1086 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){ 1120 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
1087 pPager->eLock = (u8)eLock; 1121 pPager->eLock = (u8)eLock;
1088 IOTRACE(("LOCK %p %d\n", pPager, eLock)) 1122 IOTRACE(("LOCK %p %d\n", pPager, eLock))
1089 } 1123 }
1090 } 1124 }
1091 return rc; 1125 return rc;
1092 } 1126 }
1093 1127
1094 /* 1128 /*
1095 ** This function determines whether or not the atomic-write optimization 1129 ** This function determines whether or not the atomic-write optimization
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
1206 i64 szJ; /* Total size in bytes of journal file pJrnl */ 1240 i64 szJ; /* Total size in bytes of journal file pJrnl */
1207 u32 cksum; /* MJ checksum value read from journal */ 1241 u32 cksum; /* MJ checksum value read from journal */
1208 u32 u; /* Unsigned loop counter */ 1242 u32 u; /* Unsigned loop counter */
1209 unsigned char aMagic[8]; /* A buffer to hold the magic header */ 1243 unsigned char aMagic[8]; /* A buffer to hold the magic header */
1210 zMaster[0] = '\0'; 1244 zMaster[0] = '\0';
1211 1245
1212 if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ)) 1246 if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
1213 || szJ<16 1247 || szJ<16
1214 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len)) 1248 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
1215 || len>=nMaster 1249 || len>=nMaster
1250 || len==0
1216 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum)) 1251 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
1217 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8)) 1252 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
1218 || memcmp(aMagic, aJournalMagic, 8) 1253 || memcmp(aMagic, aJournalMagic, 8)
1219 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len)) 1254 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
1220 ){ 1255 ){
1221 return rc; 1256 return rc;
1222 } 1257 }
1223 1258
1224 /* See if the checksum matches the master journal name */ 1259 /* See if the checksum matches the master journal name */
1225 for(u=0; u<len; u++){ 1260 for(u=0; u<len; u++){
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
1383 assert( isOpen(pPager->fd) || pPager->noSync ); 1418 assert( isOpen(pPager->fd) || pPager->noSync );
1384 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY) 1419 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
1385 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) 1420 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
1386 ){ 1421 ){
1387 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic)); 1422 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
1388 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff); 1423 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
1389 }else{ 1424 }else{
1390 memset(zHeader, 0, sizeof(aJournalMagic)+4); 1425 memset(zHeader, 0, sizeof(aJournalMagic)+4);
1391 } 1426 }
1392 1427
1393 /* The random check-hash initialiser */ 1428 /* The random check-hash initializer */
1394 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit); 1429 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
1395 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit); 1430 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
1396 /* The initial database size */ 1431 /* The initial database size */
1397 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize); 1432 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
1398 /* The assumed sector size for this process */ 1433 /* The assumed sector size for this process */
1399 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize); 1434 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
1400 1435
1401 /* The page size */ 1436 /* The page size */
1402 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize); 1437 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
1403 1438
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
1583 int nMaster; /* Length of string zMaster */ 1618 int nMaster; /* Length of string zMaster */
1584 i64 iHdrOff; /* Offset of header in journal file */ 1619 i64 iHdrOff; /* Offset of header in journal file */
1585 i64 jrnlSize; /* Size of journal file on disk */ 1620 i64 jrnlSize; /* Size of journal file on disk */
1586 u32 cksum = 0; /* Checksum of string zMaster */ 1621 u32 cksum = 0; /* Checksum of string zMaster */
1587 1622
1588 assert( pPager->setMaster==0 ); 1623 assert( pPager->setMaster==0 );
1589 assert( !pagerUseWal(pPager) ); 1624 assert( !pagerUseWal(pPager) );
1590 1625
1591 if( !zMaster 1626 if( !zMaster
1592 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 1627 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
1593 || pPager->journalMode==PAGER_JOURNALMODE_OFF 1628 || !isOpen(pPager->jfd)
1594 ){ 1629 ){
1595 return SQLITE_OK; 1630 return SQLITE_OK;
1596 } 1631 }
1597 pPager->setMaster = 1; 1632 pPager->setMaster = 1;
1598 assert( isOpen(pPager->jfd) );
1599 assert( pPager->journalHdr <= pPager->journalOff ); 1633 assert( pPager->journalHdr <= pPager->journalOff );
1600 1634
1601 /* Calculate the length in bytes and the checksum of zMaster */ 1635 /* Calculate the length in bytes and the checksum of zMaster */
1602 for(nMaster=0; zMaster[nMaster]; nMaster++){ 1636 for(nMaster=0; zMaster[nMaster]; nMaster++){
1603 cksum += zMaster[nMaster]; 1637 cksum += zMaster[nMaster];
1604 } 1638 }
1605 1639
1606 /* If in full-sync mode, advance to the next disk sector before writing 1640 /* If in full-sync mode, advance to the next disk sector before writing
1607 ** the master journal name. This is in case the previous page written to 1641 ** the master journal name. This is in case the previous page written to
1608 ** the journal has already been synced. 1642 ** the journal has already been synced.
(...skipping 28 matching lines...) Expand all
1637 */ 1671 */
1638 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize)) 1672 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
1639 && jrnlSize>pPager->journalOff 1673 && jrnlSize>pPager->journalOff
1640 ){ 1674 ){
1641 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff); 1675 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
1642 } 1676 }
1643 return rc; 1677 return rc;
1644 } 1678 }
1645 1679
1646 /* 1680 /*
1647 ** Find a page in the hash table given its page number. Return
1648 ** a pointer to the page or NULL if the requested page is not
1649 ** already in memory.
1650 */
1651 static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
1652 PgHdr *p; /* Return value */
1653
1654 /* It is not possible for a call to PcacheFetch() with createFlag==0 to
1655 ** fail, since no attempt to allocate dynamic memory will be made.
1656 */
1657 (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
1658 return p;
1659 }
1660
1661 /*
1662 ** Discard the entire contents of the in-memory page-cache. 1681 ** Discard the entire contents of the in-memory page-cache.
1663 */ 1682 */
1664 static void pager_reset(Pager *pPager){ 1683 static void pager_reset(Pager *pPager){
1665 sqlite3BackupRestart(pPager->pBackup); 1684 sqlite3BackupRestart(pPager->pBackup);
1666 sqlite3PcacheClear(pPager->pPCache); 1685 sqlite3PcacheClear(pPager->pPCache);
1667 } 1686 }
1668 1687
1669 /* 1688 /*
1670 ** Free all structures in the Pager.aSavepoint[] array and set both 1689 ** Free all structures in the Pager.aSavepoint[] array and set both
1671 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal 1690 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
1781 ** trusted. Now that there are no outstanding references to the pager, 1800 ** trusted. Now that there are no outstanding references to the pager,
1782 ** it can safely move back to PAGER_OPEN state. This happens in both 1801 ** it can safely move back to PAGER_OPEN state. This happens in both
1783 ** normal and exclusive-locking mode. 1802 ** normal and exclusive-locking mode.
1784 */ 1803 */
1785 if( pPager->errCode ){ 1804 if( pPager->errCode ){
1786 assert( !MEMDB ); 1805 assert( !MEMDB );
1787 pager_reset(pPager); 1806 pager_reset(pPager);
1788 pPager->changeCountDone = pPager->tempFile; 1807 pPager->changeCountDone = pPager->tempFile;
1789 pPager->eState = PAGER_OPEN; 1808 pPager->eState = PAGER_OPEN;
1790 pPager->errCode = SQLITE_OK; 1809 pPager->errCode = SQLITE_OK;
1810 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
1791 } 1811 }
1792 1812
1793 pPager->journalOff = 0; 1813 pPager->journalOff = 0;
1794 pPager->journalHdr = 0; 1814 pPager->journalHdr = 0;
1795 pPager->setMaster = 0; 1815 pPager->setMaster = 0;
1796 } 1816 }
1797 1817
1798 /* 1818 /*
1799 ** This function is called whenever an IOERR or FULL error that requires 1819 ** This function is called whenever an IOERR or FULL error that requires
1800 ** the pager to transition into the ERROR state may ahve occurred. 1820 ** the pager to transition into the ERROR state may ahve occurred.
(...skipping 21 matching lines...) Expand all
1822 pPager->errCode==SQLITE_OK || 1842 pPager->errCode==SQLITE_OK ||
1823 (pPager->errCode & 0xff)==SQLITE_IOERR 1843 (pPager->errCode & 0xff)==SQLITE_IOERR
1824 ); 1844 );
1825 if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){ 1845 if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
1826 pPager->errCode = rc; 1846 pPager->errCode = rc;
1827 pPager->eState = PAGER_ERROR; 1847 pPager->eState = PAGER_ERROR;
1828 } 1848 }
1829 return rc; 1849 return rc;
1830 } 1850 }
1831 1851
1852 static int pager_truncate(Pager *pPager, Pgno nPage);
1853
1832 /* 1854 /*
1833 ** This routine ends a transaction. A transaction is usually ended by 1855 ** This routine ends a transaction. A transaction is usually ended by
1834 ** either a COMMIT or a ROLLBACK operation. This routine may be called 1856 ** either a COMMIT or a ROLLBACK operation. This routine may be called
1835 ** after rollback of a hot-journal, or if an error occurs while opening 1857 ** after rollback of a hot-journal, or if an error occurs while opening
1836 ** the journal file or writing the very first journal-header of a 1858 ** the journal file or writing the very first journal-header of a
1837 ** database transaction. 1859 ** database transaction.
1838 ** 1860 **
1839 ** This routine is never called in PAGER_ERROR state. If it is called 1861 ** This routine is never called in PAGER_ERROR state. If it is called
1840 ** in PAGER_NONE or PAGER_SHARED state and the lock held is less 1862 ** in PAGER_NONE or PAGER_SHARED state and the lock held is less
1841 ** exclusive than a RESERVED lock, it is a no-op. 1863 ** exclusive than a RESERVED lock, it is a no-op.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1875 ** 1897 **
1876 ** SQLITE_OK is returned if no error occurs. If an error occurs during 1898 ** SQLITE_OK is returned if no error occurs. If an error occurs during
1877 ** any of the IO operations to finalize the journal file or unlock the 1899 ** any of the IO operations to finalize the journal file or unlock the
1878 ** database then the IO error code is returned to the user. If the 1900 ** database then the IO error code is returned to the user. If the
1879 ** operation to finalize the journal file fails, then the code still 1901 ** operation to finalize the journal file fails, then the code still
1880 ** tries to unlock the database file if not in exclusive mode. If the 1902 ** tries to unlock the database file if not in exclusive mode. If the
1881 ** unlock operation fails as well, then the first error code related 1903 ** unlock operation fails as well, then the first error code related
1882 ** to the first error encountered (the journal finalization one) is 1904 ** to the first error encountered (the journal finalization one) is
1883 ** returned. 1905 ** returned.
1884 */ 1906 */
1885 static int pager_end_transaction(Pager *pPager, int hasMaster){ 1907 static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){
1886 int rc = SQLITE_OK; /* Error code from journal finalization operation */ 1908 int rc = SQLITE_OK; /* Error code from journal finalization operation */
1887 int rc2 = SQLITE_OK; /* Error code from db file unlock operation */ 1909 int rc2 = SQLITE_OK; /* Error code from db file unlock operation */
1888 1910
1889 /* Do nothing if the pager does not have an open write transaction 1911 /* Do nothing if the pager does not have an open write transaction
1890 ** or at least a RESERVED lock. This function may be called when there 1912 ** or at least a RESERVED lock. This function may be called when there
1891 ** is no write-transaction active but a RESERVED or greater lock is 1913 ** is no write-transaction active but a RESERVED or greater lock is
1892 ** held under two circumstances: 1914 ** held under two circumstances:
1893 ** 1915 **
1894 ** 1. After a successful hot-journal rollback, it is called with 1916 ** 1. After a successful hot-journal rollback, it is called with
1895 ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK. 1917 ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
(...skipping 16 matching lines...) Expand all
1912 1934
1913 /* Finalize the journal file. */ 1935 /* Finalize the journal file. */
1914 if( sqlite3IsMemJournal(pPager->jfd) ){ 1936 if( sqlite3IsMemJournal(pPager->jfd) ){
1915 assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); 1937 assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
1916 sqlite3OsClose(pPager->jfd); 1938 sqlite3OsClose(pPager->jfd);
1917 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){ 1939 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
1918 if( pPager->journalOff==0 ){ 1940 if( pPager->journalOff==0 ){
1919 rc = SQLITE_OK; 1941 rc = SQLITE_OK;
1920 }else{ 1942 }else{
1921 rc = sqlite3OsTruncate(pPager->jfd, 0); 1943 rc = sqlite3OsTruncate(pPager->jfd, 0);
1944 if( rc==SQLITE_OK && pPager->fullSync ){
1945 /* Make sure the new file size is written into the inode right away.
1946 ** Otherwise the journal might resurrect following a power loss and
1947 ** cause the last transaction to roll back. See
1948 ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773
1949 */
1950 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
1951 }
1922 } 1952 }
1923 pPager->journalOff = 0; 1953 pPager->journalOff = 0;
1924 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST 1954 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
1925 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL) 1955 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
1926 ){ 1956 ){
1927 rc = zeroJournalHdr(pPager, hasMaster); 1957 rc = zeroJournalHdr(pPager, hasMaster);
1928 pPager->journalOff = 0; 1958 pPager->journalOff = 0;
1929 }else{ 1959 }else{
1930 /* This branch may be executed with Pager.journalMode==MEMORY if 1960 /* This branch may be executed with Pager.journalMode==MEMORY if
1931 ** a hot-journal was just rolled back. In this case the journal 1961 ** a hot-journal was just rolled back. In this case the journal
1932 ** file should be closed and deleted. If this connection writes to 1962 ** file should be closed and deleted. If this connection writes to
1933 ** the database file, it will do so using an in-memory journal. 1963 ** the database file, it will do so using an in-memory journal.
1934 */ 1964 */
1965 int bDelete = (!pPager->tempFile && sqlite3JournalExists(pPager->jfd));
1935 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE 1966 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
1936 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 1967 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
1937 || pPager->journalMode==PAGER_JOURNALMODE_WAL 1968 || pPager->journalMode==PAGER_JOURNALMODE_WAL
1938 ); 1969 );
1939 sqlite3OsClose(pPager->jfd); 1970 sqlite3OsClose(pPager->jfd);
1940 if( !pPager->tempFile ){ 1971 if( bDelete ){
1941 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); 1972 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
1942 } 1973 }
1943 } 1974 }
1944 } 1975 }
1945 1976
1946 #ifdef SQLITE_CHECK_PAGES 1977 #ifdef SQLITE_CHECK_PAGES
1947 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash); 1978 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
1948 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){ 1979 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
1949 PgHdr *p = pager_lookup(pPager, 1); 1980 PgHdr *p = sqlite3PagerLookup(pPager, 1);
1950 if( p ){ 1981 if( p ){
1951 p->pageHash = 0; 1982 p->pageHash = 0;
1952 sqlite3PagerUnref(p); 1983 sqlite3PagerUnrefNotNull(p);
1953 } 1984 }
1954 } 1985 }
1955 #endif 1986 #endif
1956 1987
1957 sqlite3BitvecDestroy(pPager->pInJournal); 1988 sqlite3BitvecDestroy(pPager->pInJournal);
1958 pPager->pInJournal = 0; 1989 pPager->pInJournal = 0;
1959 pPager->nRec = 0; 1990 pPager->nRec = 0;
1960 sqlite3PcacheCleanAll(pPager->pPCache); 1991 sqlite3PcacheCleanAll(pPager->pPCache);
1961 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize); 1992 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
1962 1993
1963 if( pagerUseWal(pPager) ){ 1994 if( pagerUseWal(pPager) ){
1964 /* Drop the WAL write-lock, if any. Also, if the connection was in 1995 /* Drop the WAL write-lock, if any. Also, if the connection was in
1965 ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE 1996 ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
1966 ** lock held on the database file. 1997 ** lock held on the database file.
1967 */ 1998 */
1968 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal); 1999 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
1969 assert( rc2==SQLITE_OK ); 2000 assert( rc2==SQLITE_OK );
2001 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
2002 /* This branch is taken when committing a transaction in rollback-journal
2003 ** mode if the database file on disk is larger than the database image.
2004 ** At this point the journal has been finalized and the transaction
2005 ** successfully committed, but the EXCLUSIVE lock is still held on the
2006 ** file. So it is safe to truncate the database file to its minimum
2007 ** required size. */
2008 assert( pPager->eLock==EXCLUSIVE_LOCK );
2009 rc = pager_truncate(pPager, pPager->dbSize);
1970 } 2010 }
2011
2012 if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){
2013 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
2014 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
2015 }
2016
1971 if( !pPager->exclusiveMode 2017 if( !pPager->exclusiveMode
1972 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0)) 2018 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
1973 ){ 2019 ){
1974 rc2 = pagerUnlockDb(pPager, SHARED_LOCK); 2020 rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
1975 pPager->changeCountDone = 0; 2021 pPager->changeCountDone = 0;
1976 } 2022 }
1977 pPager->eState = PAGER_READER; 2023 pPager->eState = PAGER_READER;
1978 pPager->setMaster = 0; 2024 pPager->setMaster = 0;
1979 2025
1980 return (rc==SQLITE_OK?rc2:rc); 2026 return (rc==SQLITE_OK?rc2:rc);
(...skipping 18 matching lines...) Expand all
1999 */ 2045 */
2000 static void pagerUnlockAndRollback(Pager *pPager){ 2046 static void pagerUnlockAndRollback(Pager *pPager){
2001 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){ 2047 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
2002 assert( assert_pager_state(pPager) ); 2048 assert( assert_pager_state(pPager) );
2003 if( pPager->eState>=PAGER_WRITER_LOCKED ){ 2049 if( pPager->eState>=PAGER_WRITER_LOCKED ){
2004 sqlite3BeginBenignMalloc(); 2050 sqlite3BeginBenignMalloc();
2005 sqlite3PagerRollback(pPager); 2051 sqlite3PagerRollback(pPager);
2006 sqlite3EndBenignMalloc(); 2052 sqlite3EndBenignMalloc();
2007 }else if( !pPager->exclusiveMode ){ 2053 }else if( !pPager->exclusiveMode ){
2008 assert( pPager->eState==PAGER_READER ); 2054 assert( pPager->eState==PAGER_READER );
2009 pager_end_transaction(pPager, 0); 2055 pager_end_transaction(pPager, 0, 0);
2010 } 2056 }
2011 } 2057 }
2012 pager_unlock(pPager); 2058 pager_unlock(pPager);
2013 } 2059 }
2014 2060
2015 /* 2061 /*
2016 ** Parameter aData must point to a buffer of pPager->pageSize bytes 2062 ** Parameter aData must point to a buffer of pPager->pageSize bytes
2017 ** of data. Compute and return a checksum based ont the contents of the 2063 ** of data. Compute and return a checksum based ont the contents of the
2018 ** page of data and the current value of pPager->cksumInit. 2064 ** page of data and the current value of pPager->cksumInit.
2019 ** 2065 **
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
2203 ** in the main journal either because the page is not in cache or else 2249 ** in the main journal either because the page is not in cache or else
2204 ** the page is marked as needSync==0. 2250 ** the page is marked as needSync==0.
2205 ** 2251 **
2206 ** 2008-04-14: When attempting to vacuum a corrupt database file, it 2252 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
2207 ** is possible to fail a statement on a database that does not yet exist. 2253 ** is possible to fail a statement on a database that does not yet exist.
2208 ** Do not attempt to write if database file has never been opened. 2254 ** Do not attempt to write if database file has never been opened.
2209 */ 2255 */
2210 if( pagerUseWal(pPager) ){ 2256 if( pagerUseWal(pPager) ){
2211 pPg = 0; 2257 pPg = 0;
2212 }else{ 2258 }else{
2213 pPg = pager_lookup(pPager, pgno); 2259 pPg = sqlite3PagerLookup(pPager, pgno);
2214 } 2260 }
2215 assert( pPg || !MEMDB ); 2261 assert( pPg || !MEMDB );
2216 assert( pPager->eState!=PAGER_OPEN || pPg==0 ); 2262 assert( pPager->eState!=PAGER_OPEN || pPg==0 );
2217 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n", 2263 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
2218 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData), 2264 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
2219 (isMainJrnl?"main-journal":"sub-journal") 2265 (isMainJrnl?"main-journal":"sub-journal")
2220 )); 2266 ));
2221 if( isMainJrnl ){ 2267 if( isMainJrnl ){
2222 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr); 2268 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
2223 }else{ 2269 }else{
2224 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC)); 2270 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
2225 } 2271 }
2226 if( isOpen(pPager->fd) 2272 if( isOpen(pPager->fd)
2227 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) 2273 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
2228 && isSynced 2274 && isSynced
2229 ){ 2275 ){
2230 i64 ofst = (pgno-1)*(i64)pPager->pageSize; 2276 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
2231 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 ); 2277 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
2232 assert( !pagerUseWal(pPager) ); 2278 assert( !pagerUseWal(pPager) );
2233 rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst); 2279 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
2234 if( pgno>pPager->dbFileSize ){ 2280 if( pgno>pPager->dbFileSize ){
2235 pPager->dbFileSize = pgno; 2281 pPager->dbFileSize = pgno;
2236 } 2282 }
2237 if( pPager->pBackup ){ 2283 if( pPager->pBackup ){
2238 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM); 2284 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
2239 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData); 2285 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
2240 CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData); 2286 CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
2241 } 2287 }
2242 }else if( !isMainJrnl && pPg==0 ){ 2288 }else if( !isMainJrnl && pPg==0 ){
2243 /* If this is a rollback of a savepoint and data was not written to 2289 /* If this is a rollback of a savepoint and data was not written to
2244 ** the database and the page is not in-memory, there is a potential 2290 ** the database and the page is not in-memory, there is a potential
2245 ** problem. When the page is next fetched by the b-tree layer, it 2291 ** problem. When the page is next fetched by the b-tree layer, it
2246 ** will be read from the database file, which may or may not be 2292 ** will be read from the database file, which may or may not be
2247 ** current. 2293 ** current.
2248 ** 2294 **
2249 ** There are a couple of different ways this can happen. All are quite 2295 ** There are a couple of different ways this can happen. All are quite
2250 ** obscure. When running in synchronous mode, this can only happen 2296 ** obscure. When running in synchronous mode, this can only happen
2251 ** if the page is on the free-list at the start of the transaction, then 2297 ** if the page is on the free-list at the start of the transaction, then
2252 ** populated, then moved using sqlite3PagerMovepage(). 2298 ** populated, then moved using sqlite3PagerMovepage().
2253 ** 2299 **
2254 ** The solution is to add an in-memory page to the cache containing 2300 ** The solution is to add an in-memory page to the cache containing
2255 ** the data just read from the sub-journal. Mark the page as dirty 2301 ** the data just read from the sub-journal. Mark the page as dirty
2256 ** and if the pager requires a journal-sync, then mark the page as 2302 ** and if the pager requires a journal-sync, then mark the page as
2257 ** requiring a journal-sync before it is written. 2303 ** requiring a journal-sync before it is written.
2258 */ 2304 */
2259 assert( isSavepnt ); 2305 assert( isSavepnt );
2260 assert( pPager->doNotSpill==0 ); 2306 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
2261 pPager->doNotSpill++; 2307 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
2262 rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1); 2308 rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
2263 assert( pPager->doNotSpill==1 ); 2309 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
2264 pPager->doNotSpill--; 2310 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
2265 if( rc!=SQLITE_OK ) return rc; 2311 if( rc!=SQLITE_OK ) return rc;
2266 pPg->flags &= ~PGHDR_NEED_READ; 2312 pPg->flags &= ~PGHDR_NEED_READ;
2267 sqlite3PcacheMakeDirty(pPg); 2313 sqlite3PcacheMakeDirty(pPg);
2268 } 2314 }
2269 if( pPg ){ 2315 if( pPg ){
2270 /* No page should ever be explicitly rolled back that is in use, except 2316 /* No page should ever be explicitly rolled back that is in use, except
2271 ** for page 1 which is held in use in order to keep the lock on the 2317 ** for page 1 which is held in use in order to keep the lock on the
2272 ** database active. However such a page may be rolled back as a result 2318 ** database active. However such a page may be rolled back as a result
2273 ** of an internal error resulting in an automatic call to 2319 ** of an internal error resulting in an automatic call to
2274 ** sqlite3PagerRollback(). 2320 ** sqlite3PagerRollback().
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
2383 if( rc!=SQLITE_OK ) goto delmaster_out; 2429 if( rc!=SQLITE_OK ) goto delmaster_out;
2384 2430
2385 /* Load the entire master journal file into space obtained from 2431 /* Load the entire master journal file into space obtained from
2386 ** sqlite3_malloc() and pointed to by zMasterJournal. Also obtain 2432 ** sqlite3_malloc() and pointed to by zMasterJournal. Also obtain
2387 ** sufficient space (in zMasterPtr) to hold the names of master 2433 ** sufficient space (in zMasterPtr) to hold the names of master
2388 ** journal files extracted from regular rollback-journals. 2434 ** journal files extracted from regular rollback-journals.
2389 */ 2435 */
2390 rc = sqlite3OsFileSize(pMaster, &nMasterJournal); 2436 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
2391 if( rc!=SQLITE_OK ) goto delmaster_out; 2437 if( rc!=SQLITE_OK ) goto delmaster_out;
2392 nMasterPtr = pVfs->mxPathname+1; 2438 nMasterPtr = pVfs->mxPathname+1;
2393 zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1); 2439 zMasterJournal = sqlite3Malloc(nMasterJournal + nMasterPtr + 1);
2394 if( !zMasterJournal ){ 2440 if( !zMasterJournal ){
2395 rc = SQLITE_NOMEM; 2441 rc = SQLITE_NOMEM;
2396 goto delmaster_out; 2442 goto delmaster_out;
2397 } 2443 }
2398 zMasterPtr = &zMasterJournal[nMasterJournal+1]; 2444 zMasterPtr = &zMasterJournal[nMasterJournal+1];
2399 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0); 2445 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
2400 if( rc!=SQLITE_OK ) goto delmaster_out; 2446 if( rc!=SQLITE_OK ) goto delmaster_out;
2401 zMasterJournal[nMasterJournal] = 0; 2447 zMasterJournal[nMasterJournal] = 0;
2402 2448
2403 zJournal = zMasterJournal; 2449 zJournal = zMasterJournal;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
2452 ** This function is used to change the actual size of the database 2498 ** This function is used to change the actual size of the database
2453 ** file in the file-system. This only happens when committing a transaction, 2499 ** file in the file-system. This only happens when committing a transaction,
2454 ** or rolling back a transaction (including rolling back a hot-journal). 2500 ** or rolling back a transaction (including rolling back a hot-journal).
2455 ** 2501 **
2456 ** If the main database file is not open, or the pager is not in either 2502 ** If the main database file is not open, or the pager is not in either
2457 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size 2503 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
2458 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes). 2504 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
2459 ** If the file on disk is currently larger than nPage pages, then use the VFS 2505 ** If the file on disk is currently larger than nPage pages, then use the VFS
2460 ** xTruncate() method to truncate it. 2506 ** xTruncate() method to truncate it.
2461 ** 2507 **
2462 ** Or, it might might be the case that the file on disk is smaller than 2508 ** Or, it might be the case that the file on disk is smaller than
2463 ** nPage pages. Some operating system implementations can get confused if 2509 ** nPage pages. Some operating system implementations can get confused if
2464 ** you try to truncate a file to some size that is larger than it 2510 ** you try to truncate a file to some size that is larger than it
2465 ** currently is, so detect this case and write a single zero byte to 2511 ** currently is, so detect this case and write a single zero byte to
2466 ** the end of the new file instead. 2512 ** the end of the new file instead.
2467 ** 2513 **
2468 ** If successful, return SQLITE_OK. If an IO error occurs while modifying 2514 ** If successful, return SQLITE_OK. If an IO error occurs while modifying
2469 ** the database file, return the error code to the caller. 2515 ** the database file, return the error code to the caller.
2470 */ 2516 */
2471 static int pager_truncate(Pager *pPager, Pgno nPage){ 2517 static int pager_truncate(Pager *pPager, Pgno nPage){
2472 int rc = SQLITE_OK; 2518 int rc = SQLITE_OK;
2473 assert( pPager->eState!=PAGER_ERROR ); 2519 assert( pPager->eState!=PAGER_ERROR );
2474 assert( pPager->eState!=PAGER_READER ); 2520 assert( pPager->eState!=PAGER_READER );
2475 2521
2476 if( isOpen(pPager->fd) 2522 if( isOpen(pPager->fd)
2477 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) 2523 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
2478 ){ 2524 ){
2479 i64 currentSize, newSize; 2525 i64 currentSize, newSize;
2480 int szPage = pPager->pageSize; 2526 int szPage = pPager->pageSize;
2481 assert( pPager->eLock==EXCLUSIVE_LOCK ); 2527 assert( pPager->eLock==EXCLUSIVE_LOCK );
2482 /* TODO: Is it safe to use Pager.dbFileSize here? */ 2528 /* TODO: Is it safe to use Pager.dbFileSize here? */
2483 rc = sqlite3OsFileSize(pPager->fd, &currentSize); 2529 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
2484 newSize = szPage*(i64)nPage; 2530 newSize = szPage*(i64)nPage;
2485 if( rc==SQLITE_OK && currentSize!=newSize ){ 2531 if( rc==SQLITE_OK && currentSize!=newSize ){
2486 if( currentSize>newSize ){ 2532 if( currentSize>newSize ){
2487 rc = sqlite3OsTruncate(pPager->fd, newSize); 2533 rc = sqlite3OsTruncate(pPager->fd, newSize);
2488 }else{ 2534 }else if( (currentSize+szPage)<=newSize ){
2489 char *pTmp = pPager->pTmpSpace; 2535 char *pTmp = pPager->pTmpSpace;
2490 memset(pTmp, 0, szPage); 2536 memset(pTmp, 0, szPage);
2491 testcase( (newSize-szPage) < currentSize );
2492 testcase( (newSize-szPage) == currentSize ); 2537 testcase( (newSize-szPage) == currentSize );
2493 testcase( (newSize-szPage) > currentSize ); 2538 testcase( (newSize-szPage) > currentSize );
2494 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage); 2539 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
2495 } 2540 }
2496 if( rc==SQLITE_OK ){ 2541 if( rc==SQLITE_OK ){
2497 pPager->dbFileSize = nPage; 2542 pPager->dbFileSize = nPage;
2498 } 2543 }
2499 } 2544 }
2500 } 2545 }
2501 return rc; 2546 return rc;
2502 } 2547 }
2503 2548
2504 /* 2549 /*
2550 ** Return a sanitized version of the sector-size of OS file pFile. The
2551 ** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
2552 */
2553 int sqlite3SectorSize(sqlite3_file *pFile){
2554 int iRet = sqlite3OsSectorSize(pFile);
2555 if( iRet<32 ){
2556 iRet = 512;
2557 }else if( iRet>MAX_SECTOR_SIZE ){
2558 assert( MAX_SECTOR_SIZE>=512 );
2559 iRet = MAX_SECTOR_SIZE;
2560 }
2561 return iRet;
2562 }
2563
2564 /*
2505 ** Set the value of the Pager.sectorSize variable for the given 2565 ** Set the value of the Pager.sectorSize variable for the given
2506 ** pager based on the value returned by the xSectorSize method 2566 ** pager based on the value returned by the xSectorSize method
2507 ** of the open database file. The sector size will be used used 2567 ** of the open database file. The sector size will be used
2508 ** to determine the size and alignment of journal header and 2568 ** to determine the size and alignment of journal header and
2509 ** master journal pointers within created journal files. 2569 ** master journal pointers within created journal files.
2510 ** 2570 **
2511 ** For temporary files the effective sector size is always 512 bytes. 2571 ** For temporary files the effective sector size is always 512 bytes.
2512 ** 2572 **
2513 ** Otherwise, for non-temporary files, the effective sector size is 2573 ** Otherwise, for non-temporary files, the effective sector size is
2514 ** the value returned by the xSectorSize() method rounded up to 32 if 2574 ** the value returned by the xSectorSize() method rounded up to 32 if
2515 ** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it 2575 ** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
2516 ** is greater than MAX_SECTOR_SIZE. 2576 ** is greater than MAX_SECTOR_SIZE.
2577 **
2578 ** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
2579 ** the effective sector size to its minimum value (512). The purpose of
2580 ** pPager->sectorSize is to define the "blast radius" of bytes that
2581 ** might change if a crash occurs while writing to a single byte in
2582 ** that range. But with POWERSAFE_OVERWRITE, the blast radius is zero
2583 ** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
2584 ** size. For backwards compatibility of the rollback journal file format,
2585 ** we cannot reduce the effective sector size below 512.
2517 */ 2586 */
2518 static void setSectorSize(Pager *pPager){ 2587 static void setSectorSize(Pager *pPager){
2519 assert( isOpen(pPager->fd) || pPager->tempFile ); 2588 assert( isOpen(pPager->fd) || pPager->tempFile );
2520 2589
2521 if( !pPager->tempFile ){ 2590 if( pPager->tempFile
2591 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
2592 SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
2593 ){
2522 /* Sector size doesn't matter for temporary files. Also, the file 2594 /* Sector size doesn't matter for temporary files. Also, the file
2523 ** may not have been opened yet, in which case the OsSectorSize() 2595 ** may not have been opened yet, in which case the OsSectorSize()
2524 ** call will segfault. 2596 ** call will segfault. */
2525 */
2526 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
2527 }
2528 if( pPager->sectorSize<32 ){
2529 pPager->sectorSize = 512; 2597 pPager->sectorSize = 512;
2530 } 2598 }else{
2531 if( pPager->sectorSize>MAX_SECTOR_SIZE ){ 2599 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
2532 assert( MAX_SECTOR_SIZE>=512 );
2533 pPager->sectorSize = MAX_SECTOR_SIZE;
2534 } 2600 }
2535 } 2601 }
2536 2602
2537 /* 2603 /*
2538 ** Playback the journal and thus restore the database file to 2604 ** Playback the journal and thus restore the database file to
2539 ** the state it was in before we started making changes. 2605 ** the state it was in before we started making changes.
2540 ** 2606 **
2541 ** The journal file format is as follows: 2607 ** The journal file format is as follows:
2542 ** 2608 **
2543 ** (1) 8 byte prefix. A copy of aJournalMagic[]. 2609 ** (1) 8 byte prefix. A copy of aJournalMagic[].
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
2594 static int pager_playback(Pager *pPager, int isHot){ 2660 static int pager_playback(Pager *pPager, int isHot){
2595 sqlite3_vfs *pVfs = pPager->pVfs; 2661 sqlite3_vfs *pVfs = pPager->pVfs;
2596 i64 szJ; /* Size of the journal file in bytes */ 2662 i64 szJ; /* Size of the journal file in bytes */
2597 u32 nRec; /* Number of Records in the journal */ 2663 u32 nRec; /* Number of Records in the journal */
2598 u32 u; /* Unsigned loop counter */ 2664 u32 u; /* Unsigned loop counter */
2599 Pgno mxPg = 0; /* Size of the original file in pages */ 2665 Pgno mxPg = 0; /* Size of the original file in pages */
2600 int rc; /* Result code of a subroutine */ 2666 int rc; /* Result code of a subroutine */
2601 int res = 1; /* Value returned by sqlite3OsAccess() */ 2667 int res = 1; /* Value returned by sqlite3OsAccess() */
2602 char *zMaster = 0; /* Name of master journal file if any */ 2668 char *zMaster = 0; /* Name of master journal file if any */
2603 int needPagerReset; /* True to reset page prior to first page rollback */ 2669 int needPagerReset; /* True to reset page prior to first page rollback */
2670 int nPlayback = 0; /* Total number of pages restored from journal */
2604 2671
2605 /* Figure out how many records are in the journal. Abort early if 2672 /* Figure out how many records are in the journal. Abort early if
2606 ** the journal is empty. 2673 ** the journal is empty.
2607 */ 2674 */
2608 assert( isOpen(pPager->jfd) ); 2675 assert( isOpen(pPager->jfd) );
2609 rc = sqlite3OsFileSize(pPager->jfd, &szJ); 2676 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
2610 if( rc!=SQLITE_OK ){ 2677 if( rc!=SQLITE_OK ){
2611 goto end_playback; 2678 goto end_playback;
2612 } 2679 }
2613 2680
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
2694 2761
2695 /* Copy original pages out of the journal and back into the 2762 /* Copy original pages out of the journal and back into the
2696 ** database file and/or page cache. 2763 ** database file and/or page cache.
2697 */ 2764 */
2698 for(u=0; u<nRec; u++){ 2765 for(u=0; u<nRec; u++){
2699 if( needPagerReset ){ 2766 if( needPagerReset ){
2700 pager_reset(pPager); 2767 pager_reset(pPager);
2701 needPagerReset = 0; 2768 needPagerReset = 0;
2702 } 2769 }
2703 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0); 2770 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
2704 if( rc!=SQLITE_OK ){ 2771 if( rc==SQLITE_OK ){
2772 nPlayback++;
2773 }else{
2705 if( rc==SQLITE_DONE ){ 2774 if( rc==SQLITE_DONE ){
2706 rc = SQLITE_OK;
2707 pPager->journalOff = szJ; 2775 pPager->journalOff = szJ;
2708 break; 2776 break;
2709 }else if( rc==SQLITE_IOERR_SHORT_READ ){ 2777 }else if( rc==SQLITE_IOERR_SHORT_READ ){
2710 /* If the journal has been truncated, simply stop reading and 2778 /* If the journal has been truncated, simply stop reading and
2711 ** processing the journal. This might happen if the journal was 2779 ** processing the journal. This might happen if the journal was
2712 ** not completely written and synced prior to a crash. In that 2780 ** not completely written and synced prior to a crash. In that
2713 ** case, the database should have never been written in the 2781 ** case, the database should have never been written in the
2714 ** first place so it is OK to simply abandon the rollback. */ 2782 ** first place so it is OK to simply abandon the rollback. */
2715 rc = SQLITE_OK; 2783 rc = SQLITE_OK;
2716 goto end_playback; 2784 goto end_playback;
(...skipping 10 matching lines...) Expand all
2727 } 2795 }
2728 /*NOTREACHED*/ 2796 /*NOTREACHED*/
2729 assert( 0 ); 2797 assert( 0 );
2730 2798
2731 end_playback: 2799 end_playback:
2732 /* Following a rollback, the database file should be back in its original 2800 /* Following a rollback, the database file should be back in its original
2733 ** state prior to the start of the transaction, so invoke the 2801 ** state prior to the start of the transaction, so invoke the
2734 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the 2802 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
2735 ** assertion that the transaction counter was modified. 2803 ** assertion that the transaction counter was modified.
2736 */ 2804 */
2737 assert( 2805 #ifdef SQLITE_DEBUG
2738 pPager->fd->pMethods==0 || 2806 if( pPager->fd->pMethods ){
2739 sqlite3OsFileControl(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0)>=SQLITE_OK 2807 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
2740 ); 2808 }
2809 #endif
2741 2810
2742 /* If this playback is happening automatically as a result of an IO or 2811 /* If this playback is happening automatically as a result of an IO or
2743 ** malloc error that occurred after the change-counter was updated but 2812 ** malloc error that occurred after the change-counter was updated but
2744 ** before the transaction was committed, then the change-counter 2813 ** before the transaction was committed, then the change-counter
2745 ** modification may just have been reverted. If this happens in exclusive 2814 ** modification may just have been reverted. If this happens in exclusive
2746 ** mode, then subsequent transactions performed by the connection will not 2815 ** mode, then subsequent transactions performed by the connection will not
2747 ** update the change-counter at all. This may lead to cache inconsistency 2816 ** update the change-counter at all. This may lead to cache inconsistency
2748 ** problems for other processes at some point in the future. So, just 2817 ** problems for other processes at some point in the future. So, just
2749 ** in case this has happened, clear the changeCountDone flag now. 2818 ** in case this has happened, clear the changeCountDone flag now.
2750 */ 2819 */
2751 pPager->changeCountDone = pPager->tempFile; 2820 pPager->changeCountDone = pPager->tempFile;
2752 2821
2753 if( rc==SQLITE_OK ){ 2822 if( rc==SQLITE_OK ){
2754 zMaster = pPager->pTmpSpace; 2823 zMaster = pPager->pTmpSpace;
2755 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); 2824 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
2756 testcase( rc!=SQLITE_OK ); 2825 testcase( rc!=SQLITE_OK );
2757 } 2826 }
2758 if( rc==SQLITE_OK 2827 if( rc==SQLITE_OK
2759 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) 2828 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
2760 ){ 2829 ){
2761 rc = sqlite3PagerSync(pPager); 2830 rc = sqlite3PagerSync(pPager, 0);
2762 } 2831 }
2763 if( rc==SQLITE_OK ){ 2832 if( rc==SQLITE_OK ){
2764 rc = pager_end_transaction(pPager, zMaster[0]!='\0'); 2833 rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0);
2765 testcase( rc!=SQLITE_OK ); 2834 testcase( rc!=SQLITE_OK );
2766 } 2835 }
2767 if( rc==SQLITE_OK && zMaster[0] && res ){ 2836 if( rc==SQLITE_OK && zMaster[0] && res ){
2768 /* If there was a master journal and this routine will return success, 2837 /* If there was a master journal and this routine will return success,
2769 ** see if it is possible to delete the master journal. 2838 ** see if it is possible to delete the master journal.
2770 */ 2839 */
2771 rc = pager_delmaster(pPager, zMaster); 2840 rc = pager_delmaster(pPager, zMaster);
2772 testcase( rc!=SQLITE_OK ); 2841 testcase( rc!=SQLITE_OK );
2773 } 2842 }
2843 if( isHot && nPlayback ){
2844 sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, "recovered %d pages from %s",
2845 nPlayback, pPager->zJournal);
2846 }
2774 2847
2775 /* The Pager.sectorSize variable may have been updated while rolling 2848 /* The Pager.sectorSize variable may have been updated while rolling
2776 ** back a journal created by a process with a different sector size 2849 ** back a journal created by a process with a different sector size
2777 ** value. Reset it to the correct value for this process. 2850 ** value. Reset it to the correct value for this process.
2778 */ 2851 */
2779 setSectorSize(pPager); 2852 setSectorSize(pPager);
2780 return rc; 2853 return rc;
2781 } 2854 }
2782 2855
2783 2856
2784 /* 2857 /*
2785 ** Read the content for page pPg out of the database file and into 2858 ** Read the content for page pPg out of the database file and into
2786 ** pPg->pData. A shared lock or greater must be held on the database 2859 ** pPg->pData. A shared lock or greater must be held on the database
2787 ** file before this function is called. 2860 ** file before this function is called.
2788 ** 2861 **
2789 ** If page 1 is read, then the value of Pager.dbFileVers[] is set to 2862 ** If page 1 is read, then the value of Pager.dbFileVers[] is set to
2790 ** the value read from the database file. 2863 ** the value read from the database file.
2791 ** 2864 **
2792 ** If an IO error occurs, then the IO error is returned to the caller. 2865 ** If an IO error occurs, then the IO error is returned to the caller.
2793 ** Otherwise, SQLITE_OK is returned. 2866 ** Otherwise, SQLITE_OK is returned.
2794 */ 2867 */
2795 static int readDbPage(PgHdr *pPg){ 2868 static int readDbPage(PgHdr *pPg, u32 iFrame){
2796 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */ 2869 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
2797 Pgno pgno = pPg->pgno; /* Page number to read */ 2870 Pgno pgno = pPg->pgno; /* Page number to read */
2798 int rc = SQLITE_OK; /* Return code */ 2871 int rc = SQLITE_OK; /* Return code */
2799 int isInWal = 0; /* True if page is in log file */
2800 int pgsz = pPager->pageSize; /* Number of bytes to read */ 2872 int pgsz = pPager->pageSize; /* Number of bytes to read */
2801 2873
2802 assert( pPager->eState>=PAGER_READER && !MEMDB ); 2874 assert( pPager->eState>=PAGER_READER && !MEMDB );
2803 assert( isOpen(pPager->fd) ); 2875 assert( isOpen(pPager->fd) );
2804 2876
2805 if( NEVER(!isOpen(pPager->fd)) ){ 2877 #ifndef SQLITE_OMIT_WAL
2806 assert( pPager->tempFile ); 2878 if( iFrame ){
2807 memset(pPg->pData, 0, pPager->pageSize);
2808 return SQLITE_OK;
2809 }
2810
2811 if( pagerUseWal(pPager) ){
2812 /* Try to pull the page from the write-ahead log. */ 2879 /* Try to pull the page from the write-ahead log. */
2813 rc = sqlite3WalRead(pPager->pWal, pgno, &isInWal, pgsz, pPg->pData); 2880 rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData);
2814 } 2881 }else
2815 if( rc==SQLITE_OK && !isInWal ){ 2882 #endif
2883 {
2816 i64 iOffset = (pgno-1)*(i64)pPager->pageSize; 2884 i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
2817 rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset); 2885 rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
2818 if( rc==SQLITE_IOERR_SHORT_READ ){ 2886 if( rc==SQLITE_IOERR_SHORT_READ ){
2819 rc = SQLITE_OK; 2887 rc = SQLITE_OK;
2820 } 2888 }
2821 } 2889 }
2822 2890
2823 if( pgno==1 ){ 2891 if( pgno==1 ){
2824 if( rc ){ 2892 if( rc ){
2825 /* If the read is unsuccessful, set the dbFileVers[] to something 2893 /* If the read is unsuccessful, set the dbFileVers[] to something
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
2884 ** it is discarded. Otherwise, if there are one or more outstanding 2952 ** it is discarded. Otherwise, if there are one or more outstanding
2885 ** references, the page content is reloaded from the database. If the 2953 ** references, the page content is reloaded from the database. If the
2886 ** attempt to reload content from the database is required and fails, 2954 ** attempt to reload content from the database is required and fails,
2887 ** return an SQLite error code. Otherwise, SQLITE_OK. 2955 ** return an SQLite error code. Otherwise, SQLITE_OK.
2888 */ 2956 */
2889 static int pagerUndoCallback(void *pCtx, Pgno iPg){ 2957 static int pagerUndoCallback(void *pCtx, Pgno iPg){
2890 int rc = SQLITE_OK; 2958 int rc = SQLITE_OK;
2891 Pager *pPager = (Pager *)pCtx; 2959 Pager *pPager = (Pager *)pCtx;
2892 PgHdr *pPg; 2960 PgHdr *pPg;
2893 2961
2962 assert( pagerUseWal(pPager) );
2894 pPg = sqlite3PagerLookup(pPager, iPg); 2963 pPg = sqlite3PagerLookup(pPager, iPg);
2895 if( pPg ){ 2964 if( pPg ){
2896 if( sqlite3PcachePageRefcount(pPg)==1 ){ 2965 if( sqlite3PcachePageRefcount(pPg)==1 ){
2897 sqlite3PcacheDrop(pPg); 2966 sqlite3PcacheDrop(pPg);
2898 }else{ 2967 }else{
2899 rc = readDbPage(pPg); 2968 u32 iFrame = 0;
2969 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
2970 if( rc==SQLITE_OK ){
2971 rc = readDbPage(pPg, iFrame);
2972 }
2900 if( rc==SQLITE_OK ){ 2973 if( rc==SQLITE_OK ){
2901 pPager->xReiniter(pPg); 2974 pPager->xReiniter(pPg);
2902 } 2975 }
2903 sqlite3PagerUnref(pPg); 2976 sqlite3PagerUnrefNotNull(pPg);
2904 } 2977 }
2905 } 2978 }
2906 2979
2907 /* Normally, if a transaction is rolled back, any backup processes are 2980 /* Normally, if a transaction is rolled back, any backup processes are
2908 ** updated as data is copied out of the rollback journal and into the 2981 ** updated as data is copied out of the rollback journal and into the
2909 ** database. This is not generally possible with a WAL database, as 2982 ** database. This is not generally possible with a WAL database, as
2910 ** rollback involves simply truncating the log file. Therefore, if one 2983 ** rollback involves simply truncating the log file. Therefore, if one
2911 ** or more frames have already been written to the log (and therefore 2984 ** or more frames have already been written to the log (and therefore
2912 ** also copied into the backup databases) as part of this transaction, 2985 ** also copied into the backup databases) as part of this transaction,
2913 ** the backups must be restarted. 2986 ** the backups must be restarted.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
2949 ** this function notifies any active backup processes that the pages have 3022 ** this function notifies any active backup processes that the pages have
2950 ** changed. 3023 ** changed.
2951 ** 3024 **
2952 ** The list of pages passed into this routine is always sorted by page number. 3025 ** The list of pages passed into this routine is always sorted by page number.
2953 ** Hence, if page 1 appears anywhere on the list, it will be the first page. 3026 ** Hence, if page 1 appears anywhere on the list, it will be the first page.
2954 */ 3027 */
2955 static int pagerWalFrames( 3028 static int pagerWalFrames(
2956 Pager *pPager, /* Pager object */ 3029 Pager *pPager, /* Pager object */
2957 PgHdr *pList, /* List of frames to log */ 3030 PgHdr *pList, /* List of frames to log */
2958 Pgno nTruncate, /* Database size after this commit */ 3031 Pgno nTruncate, /* Database size after this commit */
2959 int isCommit, /* True if this is a commit */ 3032 int isCommit /* True if this is a commit */
2960 int syncFlags /* Flags to pass to OsSync() (or 0) */
2961 ){ 3033 ){
2962 int rc; /* Return code */ 3034 int rc; /* Return code */
3035 int nList; /* Number of pages in pList */
2963 #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES) 3036 #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
2964 PgHdr *p; /* For looping over pages */ 3037 PgHdr *p; /* For looping over pages */
2965 #endif 3038 #endif
2966 3039
2967 assert( pPager->pWal ); 3040 assert( pPager->pWal );
3041 assert( pList );
2968 #ifdef SQLITE_DEBUG 3042 #ifdef SQLITE_DEBUG
2969 /* Verify that the page list is in accending order */ 3043 /* Verify that the page list is in accending order */
2970 for(p=pList; p && p->pDirty; p=p->pDirty){ 3044 for(p=pList; p && p->pDirty; p=p->pDirty){
2971 assert( p->pgno < p->pDirty->pgno ); 3045 assert( p->pgno < p->pDirty->pgno );
2972 } 3046 }
2973 #endif 3047 #endif
2974 3048
3049 assert( pList->pDirty==0 || isCommit );
2975 if( isCommit ){ 3050 if( isCommit ){
2976 /* If a WAL transaction is being committed, there is no point in writing 3051 /* If a WAL transaction is being committed, there is no point in writing
2977 ** any pages with page numbers greater than nTruncate into the WAL file. 3052 ** any pages with page numbers greater than nTruncate into the WAL file.
2978 ** They will never be read by any client. So remove them from the pDirty 3053 ** They will never be read by any client. So remove them from the pDirty
2979 ** list here. */ 3054 ** list here. */
2980 PgHdr *p; 3055 PgHdr *p;
2981 PgHdr **ppNext = &pList; 3056 PgHdr **ppNext = &pList;
2982 for(p=pList; (*ppNext = p); p=p->pDirty){ 3057 nList = 0;
2983 if( p->pgno<=nTruncate ) ppNext = &p->pDirty; 3058 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
3059 if( p->pgno<=nTruncate ){
3060 ppNext = &p->pDirty;
3061 nList++;
3062 }
2984 } 3063 }
2985 assert( pList ); 3064 assert( pList );
3065 }else{
3066 nList = 1;
2986 } 3067 }
3068 pPager->aStat[PAGER_STAT_WRITE] += nList;
2987 3069
2988 if( pList->pgno==1 ) pager_write_changecounter(pList); 3070 if( pList->pgno==1 ) pager_write_changecounter(pList);
2989 rc = sqlite3WalFrames(pPager->pWal, 3071 rc = sqlite3WalFrames(pPager->pWal,
2990 pPager->pageSize, pList, nTruncate, isCommit, syncFlags 3072 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
2991 ); 3073 );
2992 if( rc==SQLITE_OK && pPager->pBackup ){ 3074 if( rc==SQLITE_OK && pPager->pBackup ){
2993 PgHdr *p; 3075 PgHdr *p;
2994 for(p=pList; p; p=p->pDirty){ 3076 for(p=pList; p; p=p->pDirty){
2995 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData); 3077 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
2996 } 3078 }
2997 } 3079 }
2998 3080
2999 #ifdef SQLITE_CHECK_PAGES 3081 #ifdef SQLITE_CHECK_PAGES
3000 pList = sqlite3PcacheDirtyList(pPager->pPCache); 3082 pList = sqlite3PcacheDirtyList(pPager->pPCache);
(...skipping 23 matching lines...) Expand all
3024 /* sqlite3WalEndReadTransaction() was not called for the previous 3106 /* sqlite3WalEndReadTransaction() was not called for the previous
3025 ** transaction in locking_mode=EXCLUSIVE. So call it now. If we 3107 ** transaction in locking_mode=EXCLUSIVE. So call it now. If we
3026 ** are in locking_mode=NORMAL and EndRead() was previously called, 3108 ** are in locking_mode=NORMAL and EndRead() was previously called,
3027 ** the duplicate call is harmless. 3109 ** the duplicate call is harmless.
3028 */ 3110 */
3029 sqlite3WalEndReadTransaction(pPager->pWal); 3111 sqlite3WalEndReadTransaction(pPager->pWal);
3030 3112
3031 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed); 3113 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
3032 if( rc!=SQLITE_OK || changed ){ 3114 if( rc!=SQLITE_OK || changed ){
3033 pager_reset(pPager); 3115 pager_reset(pPager);
3116 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
3034 } 3117 }
3035 3118
3036 return rc; 3119 return rc;
3037 } 3120 }
3038 #endif 3121 #endif
3039 3122
3040 /* 3123 /*
3041 ** This function is called as part of the transition from PAGER_OPEN 3124 ** This function is called as part of the transition from PAGER_OPEN
3042 ** to PAGER_READER state to determine the size of the database file 3125 ** to PAGER_READER state to determine the size of the database file
3043 ** in pages (assuming the page size currently stored in Pager.pageSize). 3126 ** in pages (assuming the page size currently stored in Pager.pageSize).
3044 ** 3127 **
3045 ** If no error occurs, SQLITE_OK is returned and the size of the database 3128 ** If no error occurs, SQLITE_OK is returned and the size of the database
3046 ** in pages is stored in *pnPage. Otherwise, an error code (perhaps 3129 ** in pages is stored in *pnPage. Otherwise, an error code (perhaps
3047 ** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified. 3130 ** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
3048 */ 3131 */
3049 static int pagerPagecount(Pager *pPager, Pgno *pnPage){ 3132 static int pagerPagecount(Pager *pPager, Pgno *pnPage){
3050 Pgno nPage; /* Value to return via *pnPage */ 3133 Pgno nPage; /* Value to return via *pnPage */
3051 3134
3052 /* Query the WAL sub-system for the database size. The WalDbsize() 3135 /* Query the WAL sub-system for the database size. The WalDbsize()
3053 ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or 3136 ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
3054 ** if the database size is not available. The database size is not 3137 ** if the database size is not available. The database size is not
3055 ** available from the WAL sub-system if the log file is empty or 3138 ** available from the WAL sub-system if the log file is empty or
3056 ** contains no valid committed transactions. 3139 ** contains no valid committed transactions.
3057 */ 3140 */
3058 assert( pPager->eState==PAGER_OPEN ); 3141 assert( pPager->eState==PAGER_OPEN );
3059 assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock ); 3142 assert( pPager->eLock>=SHARED_LOCK );
3060 nPage = sqlite3WalDbsize(pPager->pWal); 3143 nPage = sqlite3WalDbsize(pPager->pWal);
3061 3144
3062 /* If the database size was not available from the WAL sub-system, 3145 /* If the database size was not available from the WAL sub-system,
3063 ** determine it based on the size of the database file. If the size 3146 ** determine it based on the size of the database file. If the size
3064 ** of the database file is not an integer multiple of the page-size, 3147 ** of the database file is not an integer multiple of the page-size,
3065 ** round down to the nearest page. Except, any file larger than 0 3148 ** round down to the nearest page. Except, any file larger than 0
3066 ** bytes in size is considered to contain at least one page. 3149 ** bytes in size is considered to contain at least one page.
3067 */ 3150 */
3068 if( nPage==0 ){ 3151 if( nPage==0 ){
3069 i64 n = 0; /* Size of db file in bytes */ 3152 i64 n = 0; /* Size of db file in bytes */
3070 assert( isOpen(pPager->fd) || pPager->tempFile ); 3153 assert( isOpen(pPager->fd) || pPager->tempFile );
3071 if( isOpen(pPager->fd) ){ 3154 if( isOpen(pPager->fd) ){
3072 int rc = sqlite3OsFileSize(pPager->fd, &n); 3155 int rc = sqlite3OsFileSize(pPager->fd, &n);
3073 if( rc!=SQLITE_OK ){ 3156 if( rc!=SQLITE_OK ){
3074 return rc; 3157 return rc;
3075 } 3158 }
3076 } 3159 }
3077 nPage = (Pgno)(n / pPager->pageSize); 3160 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
3078 if( nPage==0 && n>0 ){
3079 nPage = 1;
3080 }
3081 } 3161 }
3082 3162
3083 /* If the current number of pages in the file is greater than the 3163 /* If the current number of pages in the file is greater than the
3084 ** configured maximum pager number, increase the allowed limit so 3164 ** configured maximum pager number, increase the allowed limit so
3085 ** that the file can be read. 3165 ** that the file can be read.
3086 */ 3166 */
3087 if( nPage>pPager->mxPgno ){ 3167 if( nPage>pPager->mxPgno ){
3088 pPager->mxPgno = (Pgno)nPage; 3168 pPager->mxPgno = (Pgno)nPage;
3089 } 3169 }
3090 3170
(...skipping 16 matching lines...) Expand all
3107 ** 3187 **
3108 ** The caller must hold a SHARED lock on the database file to call this 3188 ** The caller must hold a SHARED lock on the database file to call this
3109 ** function. Because an EXCLUSIVE lock on the db file is required to delete 3189 ** function. Because an EXCLUSIVE lock on the db file is required to delete
3110 ** a WAL on a none-empty database, this ensures there is no race condition 3190 ** a WAL on a none-empty database, this ensures there is no race condition
3111 ** between the xAccess() below and an xDelete() being executed by some 3191 ** between the xAccess() below and an xDelete() being executed by some
3112 ** other connection. 3192 ** other connection.
3113 */ 3193 */
3114 static int pagerOpenWalIfPresent(Pager *pPager){ 3194 static int pagerOpenWalIfPresent(Pager *pPager){
3115 int rc = SQLITE_OK; 3195 int rc = SQLITE_OK;
3116 assert( pPager->eState==PAGER_OPEN ); 3196 assert( pPager->eState==PAGER_OPEN );
3117 assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock ); 3197 assert( pPager->eLock>=SHARED_LOCK );
3118 3198
3119 if( !pPager->tempFile ){ 3199 if( !pPager->tempFile ){
3120 int isWal; /* True if WAL file exists */ 3200 int isWal; /* True if WAL file exists */
3121 Pgno nPage; /* Size of the database file */ 3201 Pgno nPage; /* Size of the database file */
3122 3202
3123 rc = pagerPagecount(pPager, &nPage); 3203 rc = pagerPagecount(pPager, &nPage);
3124 if( rc ) return rc; 3204 if( rc ) return rc;
3125 if( nPage==0 ){ 3205 if( nPage==0 ){
3126 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0); 3206 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
3207 if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK;
3127 isWal = 0; 3208 isWal = 0;
3128 }else{ 3209 }else{
3129 rc = sqlite3OsAccess( 3210 rc = sqlite3OsAccess(
3130 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal 3211 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
3131 ); 3212 );
3132 } 3213 }
3133 if( rc==SQLITE_OK ){ 3214 if( rc==SQLITE_OK ){
3134 if( isWal ){ 3215 if( isWal ){
3135 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 ); 3216 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
3136 rc = sqlite3PagerOpenWal(pPager, 0); 3217 rc = sqlite3PagerOpenWal(pPager, 0);
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
3260 assert( rc!=SQLITE_DONE ); 3341 assert( rc!=SQLITE_DONE );
3261 } 3342 }
3262 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ ); 3343 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
3263 3344
3264 /* Finally, rollback pages from the sub-journal. Page that were 3345 /* Finally, rollback pages from the sub-journal. Page that were
3265 ** previously rolled back out of the main journal (and are hence in pDone) 3346 ** previously rolled back out of the main journal (and are hence in pDone)
3266 ** will be skipped. Out-of-range pages are also skipped. 3347 ** will be skipped. Out-of-range pages are also skipped.
3267 */ 3348 */
3268 if( pSavepoint ){ 3349 if( pSavepoint ){
3269 u32 ii; /* Loop counter */ 3350 u32 ii; /* Loop counter */
3270 i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize); 3351 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
3271 3352
3272 if( pagerUseWal(pPager) ){ 3353 if( pagerUseWal(pPager) ){
3273 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData); 3354 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
3274 } 3355 }
3275 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){ 3356 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
3276 assert( offset==ii*(4+pPager->pageSize) ); 3357 assert( offset==(i64)ii*(4+pPager->pageSize) );
3277 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1); 3358 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
3278 } 3359 }
3279 assert( rc!=SQLITE_DONE ); 3360 assert( rc!=SQLITE_DONE );
3280 } 3361 }
3281 3362
3282 sqlite3BitvecDestroy(pDone); 3363 sqlite3BitvecDestroy(pDone);
3283 if( rc==SQLITE_OK ){ 3364 if( rc==SQLITE_OK ){
3284 pPager->journalOff = szJ; 3365 pPager->journalOff = szJ;
3285 } 3366 }
3286 3367
3287 return rc; 3368 return rc;
3288 } 3369 }
3289 3370
3290 /* 3371 /*
3291 ** Change the maximum number of in-memory pages that are allowed. 3372 ** Change the maximum number of in-memory pages that are allowed.
3292 */ 3373 */
3293 void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){ 3374 void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
3294 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage); 3375 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
3295 } 3376 }
3296 3377
3297 /* 3378 /*
3298 ** Adjust the robustness of the database to damage due to OS crashes 3379 ** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
3299 ** or power failures by changing the number of syncs()s when writing 3380 */
3300 ** the rollback journal. There are three levels: 3381 static void pagerFixMaplimit(Pager *pPager){
3382 #if SQLITE_MAX_MMAP_SIZE>0
3383 sqlite3_file *fd = pPager->fd;
3384 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
3385 sqlite3_int64 sz;
3386 sz = pPager->szMmap;
3387 pPager->bUseFetch = (sz>0);
3388 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
3389 }
3390 #endif
3391 }
3392
3393 /*
3394 ** Change the maximum size of any memory mapping made of the database file.
3395 */
3396 void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
3397 pPager->szMmap = szMmap;
3398 pagerFixMaplimit(pPager);
3399 }
3400
3401 /*
3402 ** Free as much memory as possible from the pager.
3403 */
3404 void sqlite3PagerShrink(Pager *pPager){
3405 sqlite3PcacheShrink(pPager->pPCache);
3406 }
3407
3408 /*
3409 ** Adjust settings of the pager to those specified in the pgFlags parameter.
3410 **
3411 ** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
3412 ** of the database to damage due to OS crashes or power failures by
3413 ** changing the number of syncs()s when writing the journals.
3414 ** There are three levels:
3301 ** 3415 **
3302 ** OFF sqlite3OsSync() is never called. This is the default 3416 ** OFF sqlite3OsSync() is never called. This is the default
3303 ** for temporary and transient files. 3417 ** for temporary and transient files.
3304 ** 3418 **
3305 ** NORMAL The journal is synced once before writes begin on the 3419 ** NORMAL The journal is synced once before writes begin on the
3306 ** database. This is normally adequate protection, but 3420 ** database. This is normally adequate protection, but
3307 ** it is theoretically possible, though very unlikely, 3421 ** it is theoretically possible, though very unlikely,
3308 ** that an inopertune power failure could leave the journal 3422 ** that an inopertune power failure could leave the journal
3309 ** in a state which would cause damage to the database 3423 ** in a state which would cause damage to the database
3310 ** when it is rolled back. 3424 ** when it is rolled back.
(...skipping 20 matching lines...) Expand all
3331 ** using fcntl(F_FULLFSYNC). SQLITE_SYNC_NORMAL means to do an 3445 ** using fcntl(F_FULLFSYNC). SQLITE_SYNC_NORMAL means to do an
3332 ** ordinary fsync() call. There is no difference between SQLITE_SYNC_FULL 3446 ** ordinary fsync() call. There is no difference between SQLITE_SYNC_FULL
3333 ** and SQLITE_SYNC_NORMAL on platforms other than MacOSX. But the 3447 ** and SQLITE_SYNC_NORMAL on platforms other than MacOSX. But the
3334 ** synchronous=FULL versus synchronous=NORMAL setting determines when 3448 ** synchronous=FULL versus synchronous=NORMAL setting determines when
3335 ** the xSync primitive is called and is relevant to all platforms. 3449 ** the xSync primitive is called and is relevant to all platforms.
3336 ** 3450 **
3337 ** Numeric values associated with these states are OFF==1, NORMAL=2, 3451 ** Numeric values associated with these states are OFF==1, NORMAL=2,
3338 ** and FULL=3. 3452 ** and FULL=3.
3339 */ 3453 */
3340 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 3454 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
3341 void sqlite3PagerSetSafetyLevel( 3455 void sqlite3PagerSetFlags(
3342 Pager *pPager, /* The pager to set safety level for */ 3456 Pager *pPager, /* The pager to set safety level for */
3343 int level, /* PRAGMA synchronous. 1=OFF, 2=NORMAL, 3=FULL */ 3457 unsigned pgFlags /* Various flags */
3344 int bFullFsync, /* PRAGMA fullfsync */
3345 int bCkptFullFsync /* PRAGMA checkpoint_fullfsync */
3346 ){ 3458 ){
3459 unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
3347 assert( level>=1 && level<=3 ); 3460 assert( level>=1 && level<=3 );
3348 pPager->noSync = (level==1 || pPager->tempFile) ?1:0; 3461 pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
3349 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0; 3462 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
3350 if( pPager->noSync ){ 3463 if( pPager->noSync ){
3351 pPager->syncFlags = 0; 3464 pPager->syncFlags = 0;
3352 pPager->ckptSyncFlags = 0; 3465 pPager->ckptSyncFlags = 0;
3353 }else if( bFullFsync ){ 3466 }else if( pgFlags & PAGER_FULLFSYNC ){
3354 pPager->syncFlags = SQLITE_SYNC_FULL; 3467 pPager->syncFlags = SQLITE_SYNC_FULL;
3355 pPager->ckptSyncFlags = SQLITE_SYNC_FULL; 3468 pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
3356 }else if( bCkptFullFsync ){ 3469 }else if( pgFlags & PAGER_CKPT_FULLFSYNC ){
3357 pPager->syncFlags = SQLITE_SYNC_NORMAL; 3470 pPager->syncFlags = SQLITE_SYNC_NORMAL;
3358 pPager->ckptSyncFlags = SQLITE_SYNC_FULL; 3471 pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
3359 }else{ 3472 }else{
3360 pPager->syncFlags = SQLITE_SYNC_NORMAL; 3473 pPager->syncFlags = SQLITE_SYNC_NORMAL;
3361 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL; 3474 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
3362 } 3475 }
3476 pPager->walSyncFlags = pPager->syncFlags;
3477 if( pPager->fullSync ){
3478 pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
3479 }
3480 if( pgFlags & PAGER_CACHESPILL ){
3481 pPager->doNotSpill &= ~SPILLFLAG_OFF;
3482 }else{
3483 pPager->doNotSpill |= SPILLFLAG_OFF;
3484 }
3363 } 3485 }
3364 #endif 3486 #endif
3365 3487
3366 /* 3488 /*
3367 ** The following global variable is incremented whenever the library 3489 ** The following global variable is incremented whenever the library
3368 ** attempts to open a temporary file. This information is used for 3490 ** attempts to open a temporary file. This information is used for
3369 ** testing and analysis only. 3491 ** testing and analysis only.
3370 */ 3492 */
3371 #ifdef SQLITE_TEST 3493 #ifdef SQLITE_TEST
3372 int sqlite3_opentemp_count = 0; 3494 int sqlite3_opentemp_count = 0;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
3423 ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes 3545 ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
3424 ** 3546 **
3425 ** If the busy-handler callback returns non-zero, the lock is 3547 ** If the busy-handler callback returns non-zero, the lock is
3426 ** retried. If it returns zero, then the SQLITE_BUSY error is 3548 ** retried. If it returns zero, then the SQLITE_BUSY error is
3427 ** returned to the caller of the pager API function. 3549 ** returned to the caller of the pager API function.
3428 */ 3550 */
3429 void sqlite3PagerSetBusyhandler( 3551 void sqlite3PagerSetBusyhandler(
3430 Pager *pPager, /* Pager object */ 3552 Pager *pPager, /* Pager object */
3431 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */ 3553 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
3432 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */ 3554 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
3433 ){ 3555 ){
3434 pPager->xBusyHandler = xBusyHandler; 3556 pPager->xBusyHandler = xBusyHandler;
3435 pPager->pBusyHandlerArg = pBusyHandlerArg; 3557 pPager->pBusyHandlerArg = pBusyHandlerArg;
3558
3559 if( isOpen(pPager->fd) ){
3560 void **ap = (void **)&pPager->xBusyHandler;
3561 assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
3562 assert( ap[1]==pBusyHandlerArg );
3563 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
3564 }
3436 } 3565 }
3437 3566
3438 /* 3567 /*
3439 ** Change the page size used by the Pager object. The new page size 3568 ** Change the page size used by the Pager object. The new page size
3440 ** is passed in *pPageSize. 3569 ** is passed in *pPageSize.
3441 ** 3570 **
3442 ** If the pager is in the error state when this function is called, it 3571 ** If the pager is in the error state when this function is called, it
3443 ** is a no-op. The value returned is the error state error code (i.e. 3572 ** is a no-op. The value returned is the error state error code (i.e.
3444 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL). 3573 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
3445 ** 3574 **
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
3490 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){ 3619 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
3491 rc = sqlite3OsFileSize(pPager->fd, &nByte); 3620 rc = sqlite3OsFileSize(pPager->fd, &nByte);
3492 } 3621 }
3493 if( rc==SQLITE_OK ){ 3622 if( rc==SQLITE_OK ){
3494 pNew = (char *)sqlite3PageMalloc(pageSize); 3623 pNew = (char *)sqlite3PageMalloc(pageSize);
3495 if( !pNew ) rc = SQLITE_NOMEM; 3624 if( !pNew ) rc = SQLITE_NOMEM;
3496 } 3625 }
3497 3626
3498 if( rc==SQLITE_OK ){ 3627 if( rc==SQLITE_OK ){
3499 pager_reset(pPager); 3628 pager_reset(pPager);
3500 pPager->dbSize = (Pgno)(nByte/pageSize); 3629 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
3501 pPager->pageSize = pageSize; 3630 }
3631 if( rc==SQLITE_OK ){
3502 sqlite3PageFree(pPager->pTmpSpace); 3632 sqlite3PageFree(pPager->pTmpSpace);
3503 pPager->pTmpSpace = pNew; 3633 pPager->pTmpSpace = pNew;
3504 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize); 3634 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
3635 pPager->pageSize = pageSize;
3636 }else{
3637 sqlite3PageFree(pNew);
3505 } 3638 }
3506 } 3639 }
3507 3640
3508 *pPageSize = pPager->pageSize; 3641 *pPageSize = pPager->pageSize;
3509 if( rc==SQLITE_OK ){ 3642 if( rc==SQLITE_OK ){
3510 if( nReserve<0 ) nReserve = pPager->nReserve; 3643 if( nReserve<0 ) nReserve = pPager->nReserve;
3511 assert( nReserve>=0 && nReserve<1000 ); 3644 assert( nReserve>=0 && nReserve<1000 );
3512 pPager->nReserve = (i16)nReserve; 3645 pPager->nReserve = (i16)nReserve;
3513 pagerReportSize(pPager); 3646 pagerReportSize(pPager);
3647 pagerFixMaplimit(pPager);
3514 } 3648 }
3515 return rc; 3649 return rc;
3516 } 3650 }
3517 3651
3518 /* 3652 /*
3519 ** Return a pointer to the "temporary page" buffer held internally 3653 ** Return a pointer to the "temporary page" buffer held internally
3520 ** by the pager. This is a buffer that is big enough to hold the 3654 ** by the pager. This is a buffer that is big enough to hold the
3521 ** entire content of a database page. This buffer is used internally 3655 ** entire content of a database page. This buffer is used internally
3522 ** during rollback and will be overwritten whenever a rollback 3656 ** during rollback and will be overwritten whenever a rollback
3523 ** occurs. But other modules are free to use it too, as long as 3657 ** occurs. But other modules are free to use it too, as long as
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
3627 ** obtain the lock succeeds. 3761 ** obtain the lock succeeds.
3628 ** 3762 **
3629 ** Return SQLITE_OK on success and an error code if we cannot obtain 3763 ** Return SQLITE_OK on success and an error code if we cannot obtain
3630 ** the lock. If the lock is obtained successfully, set the Pager.state 3764 ** the lock. If the lock is obtained successfully, set the Pager.state
3631 ** variable to locktype before returning. 3765 ** variable to locktype before returning.
3632 */ 3766 */
3633 static int pager_wait_on_lock(Pager *pPager, int locktype){ 3767 static int pager_wait_on_lock(Pager *pPager, int locktype){
3634 int rc; /* Return code */ 3768 int rc; /* Return code */
3635 3769
3636 /* Check that this is either a no-op (because the requested lock is 3770 /* Check that this is either a no-op (because the requested lock is
3637 ** already held, or one of the transistions that the busy-handler 3771 ** already held), or one of the transitions that the busy-handler
3638 ** may be invoked during, according to the comment above 3772 ** may be invoked during, according to the comment above
3639 ** sqlite3PagerSetBusyhandler(). 3773 ** sqlite3PagerSetBusyhandler().
3640 */ 3774 */
3641 assert( (pPager->eLock>=locktype) 3775 assert( (pPager->eLock>=locktype)
3642 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK) 3776 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
3643 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK) 3777 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
3644 ); 3778 );
3645 3779
3646 do { 3780 do {
3647 rc = pagerLockDb(pPager, locktype); 3781 rc = pagerLockDb(pPager, locktype);
3648 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) ); 3782 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
3649 return rc; 3783 return rc;
3650 } 3784 }
3651 3785
3652 /* 3786 /*
3653 ** Function assertTruncateConstraint(pPager) checks that one of the 3787 ** Function assertTruncateConstraint(pPager) checks that one of the
3654 ** following is true for all dirty pages currently in the page-cache: 3788 ** following is true for all dirty pages currently in the page-cache:
3655 ** 3789 **
3656 ** a) The page number is less than or equal to the size of the 3790 ** a) The page number is less than or equal to the size of the
3657 ** current database image, in pages, OR 3791 ** current database image, in pages, OR
3658 ** 3792 **
3659 ** b) if the page content were written at this time, it would not 3793 ** b) if the page content were written at this time, it would not
3660 ** be necessary to write the current content out to the sub-journal 3794 ** be necessary to write the current content out to the sub-journal
3661 ** (as determined by function subjRequiresPage()). 3795 ** (as determined by function subjRequiresPage()).
3662 ** 3796 **
3663 ** If the condition asserted by this function were not true, and the 3797 ** If the condition asserted by this function were not true, and the
3664 ** dirty page were to be discarded from the cache via the pagerStress() 3798 ** dirty page were to be discarded from the cache via the pagerStress()
3665 ** routine, pagerStress() would not write the current page content to 3799 ** routine, pagerStress() would not write the current page content to
3666 ** the database file. If a savepoint transaction were rolled back after 3800 ** the database file. If a savepoint transaction were rolled back after
3667 ** this happened, the correct behaviour would be to restore the current 3801 ** this happened, the correct behavior would be to restore the current
3668 ** content of the page. However, since this content is not present in either 3802 ** content of the page. However, since this content is not present in either
3669 ** the database file or the portion of the rollback journal and 3803 ** the database file or the portion of the rollback journal and
3670 ** sub-journal rolled back the content could not be restored and the 3804 ** sub-journal rolled back the content could not be restored and the
3671 ** database image would become corrupt. It is therefore fortunate that 3805 ** database image would become corrupt. It is therefore fortunate that
3672 ** this circumstance cannot arise. 3806 ** this circumstance cannot arise.
3673 */ 3807 */
3674 #if defined(SQLITE_DEBUG) 3808 #if defined(SQLITE_DEBUG)
3675 static void assertTruncateConstraintCb(PgHdr *pPg){ 3809 static void assertTruncateConstraintCb(PgHdr *pPg){
3676 assert( pPg->flags&PGHDR_DIRTY ); 3810 assert( pPg->flags&PGHDR_DIRTY );
3677 assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize ); 3811 assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
3678 } 3812 }
3679 static void assertTruncateConstraint(Pager *pPager){ 3813 static void assertTruncateConstraint(Pager *pPager){
3680 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb); 3814 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
3681 } 3815 }
3682 #else 3816 #else
3683 # define assertTruncateConstraint(pPager) 3817 # define assertTruncateConstraint(pPager)
3684 #endif 3818 #endif
3685 3819
3686 /* 3820 /*
3687 ** Truncate the in-memory database file image to nPage pages. This 3821 ** Truncate the in-memory database file image to nPage pages. This
3688 ** function does not actually modify the database file on disk. It 3822 ** function does not actually modify the database file on disk. It
3689 ** just sets the internal state of the pager object so that the 3823 ** just sets the internal state of the pager object so that the
3690 ** truncation will be done when the current transaction is committed. 3824 ** truncation will be done when the current transaction is committed.
3825 **
3826 ** This function is only called right before committing a transaction.
3827 ** Once this function has been called, the transaction must either be
3828 ** rolled back or committed. It is not safe to call this function and
3829 ** then continue writing to the database.
3691 */ 3830 */
3692 void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){ 3831 void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
3693 assert( pPager->dbSize>=nPage ); 3832 assert( pPager->dbSize>=nPage );
3694 assert( pPager->eState>=PAGER_WRITER_CACHEMOD ); 3833 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
3695 pPager->dbSize = nPage; 3834 pPager->dbSize = nPage;
3696 assertTruncateConstraint(pPager); 3835
3836 /* At one point the code here called assertTruncateConstraint() to
3837 ** ensure that all pages being truncated away by this operation are,
3838 ** if one or more savepoints are open, present in the savepoint
3839 ** journal so that they can be restored if the savepoint is rolled
3840 ** back. This is no longer necessary as this function is now only
3841 ** called right before committing a transaction. So although the
3842 ** Pager object may still have open savepoints (Pager.nSavepoint!=0),
3843 ** they cannot be rolled back. So the assertTruncateConstraint() call
3844 ** is no longer correct. */
3697 } 3845 }
3698 3846
3699 3847
3700 /* 3848 /*
3701 ** This function is called before attempting a hot-journal rollback. It 3849 ** This function is called before attempting a hot-journal rollback. It
3702 ** syncs the journal file to disk, then sets pPager->journalHdr to the 3850 ** syncs the journal file to disk, then sets pPager->journalHdr to the
3703 ** size of the journal file so that the pager_playback() routine knows 3851 ** size of the journal file so that the pager_playback() routine knows
3704 ** that the entire journal file has been synced. 3852 ** that the entire journal file has been synced.
3705 ** 3853 **
3706 ** Syncing a hot-journal to disk before attempting to roll it back ensures 3854 ** Syncing a hot-journal to disk before attempting to roll it back ensures
3707 ** that if a power-failure occurs during the rollback, the process that 3855 ** that if a power-failure occurs during the rollback, the process that
3708 ** attempts rollback following system recovery sees the same journal 3856 ** attempts rollback following system recovery sees the same journal
3709 ** content as this process. 3857 ** content as this process.
3710 ** 3858 **
3711 ** If everything goes as planned, SQLITE_OK is returned. Otherwise, 3859 ** If everything goes as planned, SQLITE_OK is returned. Otherwise,
3712 ** an SQLite error code. 3860 ** an SQLite error code.
3713 */ 3861 */
3714 static int pagerSyncHotJournal(Pager *pPager){ 3862 static int pagerSyncHotJournal(Pager *pPager){
3715 int rc = SQLITE_OK; 3863 int rc = SQLITE_OK;
3716 if( !pPager->noSync ){ 3864 if( !pPager->noSync ){
3717 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL); 3865 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
3718 } 3866 }
3719 if( rc==SQLITE_OK ){ 3867 if( rc==SQLITE_OK ){
3720 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr); 3868 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
3721 } 3869 }
3722 return rc; 3870 return rc;
3723 } 3871 }
3724 3872
3725 /* 3873 /*
3874 ** Obtain a reference to a memory mapped page object for page number pgno.
3875 ** The new object will use the pointer pData, obtained from xFetch().
3876 ** If successful, set *ppPage to point to the new page reference
3877 ** and return SQLITE_OK. Otherwise, return an SQLite error code and set
3878 ** *ppPage to zero.
3879 **
3880 ** Page references obtained by calling this function should be released
3881 ** by calling pagerReleaseMapPage().
3882 */
3883 static int pagerAcquireMapPage(
3884 Pager *pPager, /* Pager object */
3885 Pgno pgno, /* Page number */
3886 void *pData, /* xFetch()'d data for this page */
3887 PgHdr **ppPage /* OUT: Acquired page object */
3888 ){
3889 PgHdr *p; /* Memory mapped page to return */
3890
3891 if( pPager->pMmapFreelist ){
3892 *ppPage = p = pPager->pMmapFreelist;
3893 pPager->pMmapFreelist = p->pDirty;
3894 p->pDirty = 0;
3895 memset(p->pExtra, 0, pPager->nExtra);
3896 }else{
3897 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
3898 if( p==0 ){
3899 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
3900 return SQLITE_NOMEM;
3901 }
3902 p->pExtra = (void *)&p[1];
3903 p->flags = PGHDR_MMAP;
3904 p->nRef = 1;
3905 p->pPager = pPager;
3906 }
3907
3908 assert( p->pExtra==(void *)&p[1] );
3909 assert( p->pPage==0 );
3910 assert( p->flags==PGHDR_MMAP );
3911 assert( p->pPager==pPager );
3912 assert( p->nRef==1 );
3913
3914 p->pgno = pgno;
3915 p->pData = pData;
3916 pPager->nMmapOut++;
3917
3918 return SQLITE_OK;
3919 }
3920
3921 /*
3922 ** Release a reference to page pPg. pPg must have been returned by an
3923 ** earlier call to pagerAcquireMapPage().
3924 */
3925 static void pagerReleaseMapPage(PgHdr *pPg){
3926 Pager *pPager = pPg->pPager;
3927 pPager->nMmapOut--;
3928 pPg->pDirty = pPager->pMmapFreelist;
3929 pPager->pMmapFreelist = pPg;
3930
3931 assert( pPager->fd->pMethods->iVersion>=3 );
3932 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
3933 }
3934
3935 /*
3936 ** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
3937 */
3938 static void pagerFreeMapHdrs(Pager *pPager){
3939 PgHdr *p;
3940 PgHdr *pNext;
3941 for(p=pPager->pMmapFreelist; p; p=pNext){
3942 pNext = p->pDirty;
3943 sqlite3_free(p);
3944 }
3945 }
3946
3947
3948 /*
3726 ** Shutdown the page cache. Free all memory and close all files. 3949 ** Shutdown the page cache. Free all memory and close all files.
3727 ** 3950 **
3728 ** If a transaction was in progress when this routine is called, that 3951 ** If a transaction was in progress when this routine is called, that
3729 ** transaction is rolled back. All outstanding pages are invalidated 3952 ** transaction is rolled back. All outstanding pages are invalidated
3730 ** and their memory is freed. Any attempt to use a page associated 3953 ** and their memory is freed. Any attempt to use a page associated
3731 ** with this page cache after this function returns will likely 3954 ** with this page cache after this function returns will likely
3732 ** result in a coredump. 3955 ** result in a coredump.
3733 ** 3956 **
3734 ** This function always succeeds. If a transaction is active an attempt 3957 ** This function always succeeds. If a transaction is active an attempt
3735 ** is made to roll it back. If an error occurs during the rollback 3958 ** is made to roll it back. If an error occurs during the rollback
3736 ** a hot journal may be left in the filesystem but no error is returned 3959 ** a hot journal may be left in the filesystem but no error is returned
3737 ** to the caller. 3960 ** to the caller.
3738 */ 3961 */
3739 int sqlite3PagerClose(Pager *pPager){ 3962 int sqlite3PagerClose(Pager *pPager){
3740 u8 *pTmp = (u8 *)pPager->pTmpSpace; 3963 u8 *pTmp = (u8 *)pPager->pTmpSpace;
3741 3964
3965 assert( assert_pager_state(pPager) );
3742 disable_simulated_io_errors(); 3966 disable_simulated_io_errors();
3743 sqlite3BeginBenignMalloc(); 3967 sqlite3BeginBenignMalloc();
3968 pagerFreeMapHdrs(pPager);
3744 /* pPager->errCode = 0; */ 3969 /* pPager->errCode = 0; */
3745 pPager->exclusiveMode = 0; 3970 pPager->exclusiveMode = 0;
3746 #ifndef SQLITE_OMIT_WAL 3971 #ifndef SQLITE_OMIT_WAL
3747 sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp); 3972 sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
3748 pPager->pWal = 0; 3973 pPager->pWal = 0;
3749 #endif 3974 #endif
3750 pager_reset(pPager); 3975 pager_reset(pPager);
3751 if( MEMDB ){ 3976 if( MEMDB ){
3752 pager_unlock(pPager); 3977 pager_unlock(pPager);
3753 }else{ 3978 }else{
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
3803 sqlite3PcacheRef(pPg); 4028 sqlite3PcacheRef(pPg);
3804 } 4029 }
3805 4030
3806 /* 4031 /*
3807 ** Sync the journal. In other words, make sure all the pages that have 4032 ** Sync the journal. In other words, make sure all the pages that have
3808 ** been written to the journal have actually reached the surface of the 4033 ** been written to the journal have actually reached the surface of the
3809 ** disk and can be restored in the event of a hot-journal rollback. 4034 ** disk and can be restored in the event of a hot-journal rollback.
3810 ** 4035 **
3811 ** If the Pager.noSync flag is set, then this function is a no-op. 4036 ** If the Pager.noSync flag is set, then this function is a no-op.
3812 ** Otherwise, the actions required depend on the journal-mode and the 4037 ** Otherwise, the actions required depend on the journal-mode and the
3813 ** device characteristics of the the file-system, as follows: 4038 ** device characteristics of the file-system, as follows:
3814 ** 4039 **
3815 ** * If the journal file is an in-memory journal file, no action need 4040 ** * If the journal file is an in-memory journal file, no action need
3816 ** be taken. 4041 ** be taken.
3817 ** 4042 **
3818 ** * Otherwise, if the device does not support the SAFE_APPEND property, 4043 ** * Otherwise, if the device does not support the SAFE_APPEND property,
3819 ** then the nRec field of the most recently written journal header 4044 ** then the nRec field of the most recently written journal header
3820 ** is updated to contain the number of journal records that have 4045 ** is updated to contain the number of journal records that have
3821 ** been written following it. If the pager is operating in full-sync 4046 ** been written following it. If the pager is operating in full-sync
3822 ** mode, then the journal file is synced before this field is updated. 4047 ** mode, then the journal file is synced before this field is updated.
3823 ** 4048 **
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
3995 */ 4220 */
3996 if( !isOpen(pPager->fd) ){ 4221 if( !isOpen(pPager->fd) ){
3997 assert( pPager->tempFile && rc==SQLITE_OK ); 4222 assert( pPager->tempFile && rc==SQLITE_OK );
3998 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags); 4223 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
3999 } 4224 }
4000 4225
4001 /* Before the first write, give the VFS a hint of what the final 4226 /* Before the first write, give the VFS a hint of what the final
4002 ** file size will be. 4227 ** file size will be.
4003 */ 4228 */
4004 assert( rc!=SQLITE_OK || isOpen(pPager->fd) ); 4229 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
4005 if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){ 4230 if( rc==SQLITE_OK
4231 && pPager->dbHintSize<pPager->dbSize
4232 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
4233 ){
4006 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize; 4234 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
4007 sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile); 4235 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
4008 pPager->dbHintSize = pPager->dbSize; 4236 pPager->dbHintSize = pPager->dbSize;
4009 } 4237 }
4010 4238
4011 while( rc==SQLITE_OK && pList ){ 4239 while( rc==SQLITE_OK && pList ){
4012 Pgno pgno = pList->pgno; 4240 Pgno pgno = pList->pgno;
4013 4241
4014 /* If there are dirty pages in the page cache with page numbers greater 4242 /* If there are dirty pages in the page cache with page numbers greater
4015 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to 4243 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
4016 ** make the file smaller (presumably by auto-vacuum code). Do not write 4244 ** make the file smaller (presumably by auto-vacuum code). Do not write
4017 ** any such pages to the file. 4245 ** any such pages to the file.
(...skipping 17 matching lines...) Expand all
4035 /* If page 1 was just written, update Pager.dbFileVers to match 4263 /* If page 1 was just written, update Pager.dbFileVers to match
4036 ** the value now stored in the database file. If writing this 4264 ** the value now stored in the database file. If writing this
4037 ** page caused the database file to grow, update dbFileSize. 4265 ** page caused the database file to grow, update dbFileSize.
4038 */ 4266 */
4039 if( pgno==1 ){ 4267 if( pgno==1 ){
4040 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers)); 4268 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
4041 } 4269 }
4042 if( pgno>pPager->dbFileSize ){ 4270 if( pgno>pPager->dbFileSize ){
4043 pPager->dbFileSize = pgno; 4271 pPager->dbFileSize = pgno;
4044 } 4272 }
4273 pPager->aStat[PAGER_STAT_WRITE]++;
4045 4274
4046 /* Update any backup objects copying the contents of this pager. */ 4275 /* Update any backup objects copying the contents of this pager. */
4047 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData); 4276 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
4048 4277
4049 PAGERTRACE(("STORE %d page %d hash(%08x)\n", 4278 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
4050 PAGERID(pPager), pgno, pager_pagehash(pList))); 4279 PAGERID(pPager), pgno, pager_pagehash(pList)));
4051 IOTRACE(("PGOUT %p %d\n", pPager, pgno)); 4280 IOTRACE(("PGOUT %p %d\n", pPager, pgno));
4052 PAGER_INCR(sqlite3_pager_writedb_count); 4281 PAGER_INCR(sqlite3_pager_writedb_count);
4053 PAGER_INCR(pPager->nWrite);
4054 }else{ 4282 }else{
4055 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno)); 4283 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
4056 } 4284 }
4057 pager_set_pagehash(pList); 4285 pager_set_pagehash(pList);
4058 pList = pList->pDirty; 4286 pList = pList->pDirty;
4059 } 4287 }
4060 4288
4061 return rc; 4289 return rc;
4062 } 4290 }
4063 4291
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
4097 static int subjournalPage(PgHdr *pPg){ 4325 static int subjournalPage(PgHdr *pPg){
4098 int rc = SQLITE_OK; 4326 int rc = SQLITE_OK;
4099 Pager *pPager = pPg->pPager; 4327 Pager *pPager = pPg->pPager;
4100 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ 4328 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
4101 4329
4102 /* Open the sub-journal, if it has not already been opened */ 4330 /* Open the sub-journal, if it has not already been opened */
4103 assert( pPager->useJournal ); 4331 assert( pPager->useJournal );
4104 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) ); 4332 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
4105 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 ); 4333 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
4106 assert( pagerUseWal(pPager) 4334 assert( pagerUseWal(pPager)
4107 || pageInJournal(pPg) 4335 || pageInJournal(pPager, pPg)
4108 || pPg->pgno>pPager->dbOrigSize 4336 || pPg->pgno>pPager->dbOrigSize
4109 ); 4337 );
4110 rc = openSubJournal(pPager); 4338 rc = openSubJournal(pPager);
4111 4339
4112 /* If the sub-journal was opened successfully (or was already open), 4340 /* If the sub-journal was opened successfully (or was already open),
4113 ** write the journal record into the file. */ 4341 ** write the journal record into the file. */
4114 if( rc==SQLITE_OK ){ 4342 if( rc==SQLITE_OK ){
4115 void *pData = pPg->pData; 4343 void *pData = pPg->pData;
4116 i64 offset = pPager->nSubRec*(4+pPager->pageSize); 4344 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
4117 char *pData2; 4345 char *pData2;
4118 4346
4119 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2); 4347 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
4120 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno)); 4348 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
4121 rc = write32bits(pPager->sjfd, offset, pPg->pgno); 4349 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
4122 if( rc==SQLITE_OK ){ 4350 if( rc==SQLITE_OK ){
4123 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4); 4351 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
4124 } 4352 }
4125 } 4353 }
4126 } 4354 }
(...skipping 24 matching lines...) Expand all
4151 ** made clean for some other reason, but no error occurs, then SQLITE_OK 4379 ** made clean for some other reason, but no error occurs, then SQLITE_OK
4152 ** is returned by sqlite3PcacheMakeClean() is not called. 4380 ** is returned by sqlite3PcacheMakeClean() is not called.
4153 */ 4381 */
4154 static int pagerStress(void *p, PgHdr *pPg){ 4382 static int pagerStress(void *p, PgHdr *pPg){
4155 Pager *pPager = (Pager *)p; 4383 Pager *pPager = (Pager *)p;
4156 int rc = SQLITE_OK; 4384 int rc = SQLITE_OK;
4157 4385
4158 assert( pPg->pPager==pPager ); 4386 assert( pPg->pPager==pPager );
4159 assert( pPg->flags&PGHDR_DIRTY ); 4387 assert( pPg->flags&PGHDR_DIRTY );
4160 4388
4161 /* The doNotSyncSpill flag is set during times when doing a sync of 4389 /* The doNotSpill NOSYNC bit is set during times when doing a sync of
4162 ** journal (and adding a new header) is not allowed. This occurs 4390 ** journal (and adding a new header) is not allowed. This occurs
4163 ** during calls to sqlite3PagerWrite() while trying to journal multiple 4391 ** during calls to sqlite3PagerWrite() while trying to journal multiple
4164 ** pages belonging to the same sector. 4392 ** pages belonging to the same sector.
4165 ** 4393 **
4166 ** The doNotSpill flag inhibits all cache spilling regardless of whether 4394 ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
4167 ** or not a sync is required. This is set during a rollback. 4395 ** regardless of whether or not a sync is required. This is set during
4396 ** a rollback or by user request, respectively.
4168 ** 4397 **
4169 ** Spilling is also prohibited when in an error state since that could 4398 ** Spilling is also prohibited when in an error state since that could
4170 ** lead to database corruption. In the current implementaton it 4399 ** lead to database corruption. In the current implementation it
4171 ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1 4400 ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3
4172 ** while in the error state, hence it is impossible for this routine to 4401 ** while in the error state, hence it is impossible for this routine to
4173 ** be called in the error state. Nevertheless, we include a NEVER() 4402 ** be called in the error state. Nevertheless, we include a NEVER()
4174 ** test for the error state as a safeguard against future changes. 4403 ** test for the error state as a safeguard against future changes.
4175 */ 4404 */
4176 if( NEVER(pPager->errCode) ) return SQLITE_OK; 4405 if( NEVER(pPager->errCode) ) return SQLITE_OK;
4177 if( pPager->doNotSpill ) return SQLITE_OK; 4406 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
4178 if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){ 4407 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
4408 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
4409 if( pPager->doNotSpill
4410 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
4411 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
4412 ){
4179 return SQLITE_OK; 4413 return SQLITE_OK;
4180 } 4414 }
4181 4415
4182 pPg->pDirty = 0; 4416 pPg->pDirty = 0;
4183 if( pagerUseWal(pPager) ){ 4417 if( pagerUseWal(pPager) ){
4184 /* Write a single frame for this page to the log. */ 4418 /* Write a single frame for this page to the log. */
4185 if( subjRequiresPage(pPg) ){ 4419 if( subjRequiresPage(pPg) ){
4186 rc = subjournalPage(pPg); 4420 rc = subjournalPage(pPg);
4187 } 4421 }
4188 if( rc==SQLITE_OK ){ 4422 if( rc==SQLITE_OK ){
4189 rc = pagerWalFrames(pPager, pPg, 0, 0, 0); 4423 rc = pagerWalFrames(pPager, pPg, 0, 0);
4190 } 4424 }
4191 }else{ 4425 }else{
4192 4426
4193 /* Sync the journal file if required. */ 4427 /* Sync the journal file if required. */
4194 if( pPg->flags&PGHDR_NEED_SYNC 4428 if( pPg->flags&PGHDR_NEED_SYNC
4195 || pPager->eState==PAGER_WRITER_CACHEMOD 4429 || pPager->eState==PAGER_WRITER_CACHEMOD
4196 ){ 4430 ){
4197 rc = syncJournal(pPager, 1); 4431 rc = syncJournal(pPager, 1);
4198 } 4432 }
4199 4433
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
4258 ** automatically when they are closed. If zFilename is ":memory:" then 4492 ** automatically when they are closed. If zFilename is ":memory:" then
4259 ** all information is held in cache. It is never written to disk. 4493 ** all information is held in cache. It is never written to disk.
4260 ** This can be used to implement an in-memory database. 4494 ** This can be used to implement an in-memory database.
4261 ** 4495 **
4262 ** The nExtra parameter specifies the number of bytes of space allocated 4496 ** The nExtra parameter specifies the number of bytes of space allocated
4263 ** along with each page reference. This space is available to the user 4497 ** along with each page reference. This space is available to the user
4264 ** via the sqlite3PagerGetExtra() API. 4498 ** via the sqlite3PagerGetExtra() API.
4265 ** 4499 **
4266 ** The flags argument is used to specify properties that affect the 4500 ** The flags argument is used to specify properties that affect the
4267 ** operation of the pager. It should be passed some bitwise combination 4501 ** operation of the pager. It should be passed some bitwise combination
4268 ** of the PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK flags. 4502 ** of the PAGER_* flags.
4269 ** 4503 **
4270 ** The vfsFlags parameter is a bitmask to pass to the flags parameter 4504 ** The vfsFlags parameter is a bitmask to pass to the flags parameter
4271 ** of the xOpen() method of the supplied VFS when opening files. 4505 ** of the xOpen() method of the supplied VFS when opening files.
4272 ** 4506 **
4273 ** If the pager object is allocated and the specified file opened 4507 ** If the pager object is allocated and the specified file opened
4274 ** successfully, SQLITE_OK is returned and *ppPager set to point to 4508 ** successfully, SQLITE_OK is returned and *ppPager set to point to
4275 ** the new pager object. If an error occurs, *ppPager is set to NULL 4509 ** the new pager object. If an error occurs, *ppPager is set to NULL
4276 ** and error code returned. This function may return SQLITE_NOMEM 4510 ** and error code returned. This function may return SQLITE_NOMEM
4277 ** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or 4511 ** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
4278 ** various SQLITE_IO_XXX errors. 4512 ** various SQLITE_IO_XXX errors.
(...skipping 10 matching lines...) Expand all
4289 u8 *pPtr; 4523 u8 *pPtr;
4290 Pager *pPager = 0; /* Pager object to allocate and return */ 4524 Pager *pPager = 0; /* Pager object to allocate and return */
4291 int rc = SQLITE_OK; /* Return code */ 4525 int rc = SQLITE_OK; /* Return code */
4292 int tempFile = 0; /* True for temp files (incl. in-memory files) */ 4526 int tempFile = 0; /* True for temp files (incl. in-memory files) */
4293 int memDb = 0; /* True if this is an in-memory file */ 4527 int memDb = 0; /* True if this is an in-memory file */
4294 int readOnly = 0; /* True if this is a read-only file */ 4528 int readOnly = 0; /* True if this is a read-only file */
4295 int journalFileSize; /* Bytes to allocate for each journal fd */ 4529 int journalFileSize; /* Bytes to allocate for each journal fd */
4296 char *zPathname = 0; /* Full path to database file */ 4530 char *zPathname = 0; /* Full path to database file */
4297 int nPathname = 0; /* Number of bytes in zPathname */ 4531 int nPathname = 0; /* Number of bytes in zPathname */
4298 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */ 4532 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
4299 int noReadlock = (flags & PAGER_NO_READLOCK)!=0; /* True to omit read-lock */
4300 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */ 4533 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
4301 u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */ 4534 u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */
4535 const char *zUri = 0; /* URI args to copy */
4536 int nUri = 0; /* Number of bytes of URI args at *zUri */
4302 4537
4303 /* Figure out how much space is required for each journal file-handle 4538 /* Figure out how much space is required for each journal file-handle
4304 ** (there are two of them, the main journal and the sub-journal). This 4539 ** (there are two of them, the main journal and the sub-journal). This
4305 ** is the maximum space required for an in-memory journal file handle 4540 ** is the maximum space required for an in-memory journal file handle
4306 ** and a regular journal file-handle. Note that a "regular journal-handle" 4541 ** and a regular journal file-handle. Note that a "regular journal-handle"
4307 ** may be a wrapper capable of caching the first portion of the journal 4542 ** may be a wrapper capable of caching the first portion of the journal
4308 ** file in memory to implement the atomic-write optimization (see 4543 ** file in memory to implement the atomic-write optimization (see
4309 ** source file journal.c). 4544 ** source file journal.c).
4310 */ 4545 */
4311 if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){ 4546 if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
4312 journalFileSize = ROUND8(sqlite3JournalSize(pVfs)); 4547 journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
4313 }else{ 4548 }else{
4314 journalFileSize = ROUND8(sqlite3MemJournalSize()); 4549 journalFileSize = ROUND8(sqlite3MemJournalSize());
4315 } 4550 }
4316 4551
4317 /* Set the output variable to NULL in case an error occurs. */ 4552 /* Set the output variable to NULL in case an error occurs. */
4318 *ppPager = 0; 4553 *ppPager = 0;
4319 4554
4320 #ifndef SQLITE_OMIT_MEMORYDB 4555 #ifndef SQLITE_OMIT_MEMORYDB
4321 if( flags & PAGER_MEMORY ){ 4556 if( flags & PAGER_MEMORY ){
4322 memDb = 1; 4557 memDb = 1;
4323 zFilename = 0; 4558 if( zFilename && zFilename[0] ){
4559 zPathname = sqlite3DbStrDup(0, zFilename);
4560 if( zPathname==0 ) return SQLITE_NOMEM;
4561 nPathname = sqlite3Strlen30(zPathname);
4562 zFilename = 0;
4563 }
4324 } 4564 }
4325 #endif 4565 #endif
4326 4566
4327 /* Compute and store the full pathname in an allocated buffer pointed 4567 /* Compute and store the full pathname in an allocated buffer pointed
4328 ** to by zPathname, length nPathname. Or, if this is a temporary file, 4568 ** to by zPathname, length nPathname. Or, if this is a temporary file,
4329 ** leave both nPathname and zPathname set to 0. 4569 ** leave both nPathname and zPathname set to 0.
4330 */ 4570 */
4331 if( zFilename && zFilename[0] ){ 4571 if( zFilename && zFilename[0] ){
4572 const char *z;
4332 nPathname = pVfs->mxPathname+1; 4573 nPathname = pVfs->mxPathname+1;
4333 zPathname = sqlite3Malloc(nPathname*2); 4574 zPathname = sqlite3DbMallocRaw(0, nPathname*2);
4334 if( zPathname==0 ){ 4575 if( zPathname==0 ){
4335 return SQLITE_NOMEM; 4576 return SQLITE_NOMEM;
4336 } 4577 }
4337 zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */ 4578 zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
4338 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname); 4579 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
4339 nPathname = sqlite3Strlen30(zPathname); 4580 nPathname = sqlite3Strlen30(zPathname);
4581 z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
4582 while( *z ){
4583 z += sqlite3Strlen30(z)+1;
4584 z += sqlite3Strlen30(z)+1;
4585 }
4586 nUri = (int)(&z[1] - zUri);
4587 assert( nUri>=0 );
4340 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){ 4588 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
4341 /* This branch is taken when the journal path required by 4589 /* This branch is taken when the journal path required by
4342 ** the database being opened will be more than pVfs->mxPathname 4590 ** the database being opened will be more than pVfs->mxPathname
4343 ** bytes in length. This means the database cannot be opened, 4591 ** bytes in length. This means the database cannot be opened,
4344 ** as it will not be possible to open the journal file or even 4592 ** as it will not be possible to open the journal file or even
4345 ** check for a hot-journal before reading. 4593 ** check for a hot-journal before reading.
4346 */ 4594 */
4347 rc = SQLITE_CANTOPEN_BKPT; 4595 rc = SQLITE_CANTOPEN_BKPT;
4348 } 4596 }
4349 if( rc!=SQLITE_OK ){ 4597 if( rc!=SQLITE_OK ){
4350 sqlite3_free(zPathname); 4598 sqlite3DbFree(0, zPathname);
4351 return rc; 4599 return rc;
4352 } 4600 }
4353 } 4601 }
4354 4602
4355 /* Allocate memory for the Pager structure, PCache object, the 4603 /* Allocate memory for the Pager structure, PCache object, the
4356 ** three file descriptors, the database file name and the journal 4604 ** three file descriptors, the database file name and the journal
4357 ** file name. The layout in memory is as follows: 4605 ** file name. The layout in memory is as follows:
4358 ** 4606 **
4359 ** Pager object (sizeof(Pager) bytes) 4607 ** Pager object (sizeof(Pager) bytes)
4360 ** PCache object (sqlite3PcacheSize() bytes) 4608 ** PCache object (sqlite3PcacheSize() bytes)
4361 ** Database file handle (pVfs->szOsFile bytes) 4609 ** Database file handle (pVfs->szOsFile bytes)
4362 ** Sub-journal file handle (journalFileSize bytes) 4610 ** Sub-journal file handle (journalFileSize bytes)
4363 ** Main journal file handle (journalFileSize bytes) 4611 ** Main journal file handle (journalFileSize bytes)
4364 ** Database file name (nPathname+1 bytes) 4612 ** Database file name (nPathname+1 bytes)
4365 ** Journal file name (nPathname+8+1 bytes) 4613 ** Journal file name (nPathname+8+1 bytes)
4366 */ 4614 */
4367 pPtr = (u8 *)sqlite3MallocZero( 4615 pPtr = (u8 *)sqlite3MallocZero(
4368 ROUND8(sizeof(*pPager)) + /* Pager structure */ 4616 ROUND8(sizeof(*pPager)) + /* Pager structure */
4369 ROUND8(pcacheSize) + /* PCache object */ 4617 ROUND8(pcacheSize) + /* PCache object */
4370 ROUND8(pVfs->szOsFile) + /* The main db file */ 4618 ROUND8(pVfs->szOsFile) + /* The main db file */
4371 journalFileSize * 2 + /* The two journal files */ 4619 journalFileSize * 2 + /* The two journal files */
4372 nPathname + 1 + /* zFilename */ 4620 nPathname + 1 + nUri + /* zFilename */
4373 nPathname + 8 + 1 /* zJournal */ 4621 nPathname + 8 + 2 /* zJournal */
4374 #ifndef SQLITE_OMIT_WAL 4622 #ifndef SQLITE_OMIT_WAL
4375 + nPathname + 4 + 1 /* zWal */ 4623 + nPathname + 4 + 2 /* zWal */
4376 #endif 4624 #endif
4377 ); 4625 );
4378 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) ); 4626 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
4379 if( !pPtr ){ 4627 if( !pPtr ){
4380 sqlite3_free(zPathname); 4628 sqlite3DbFree(0, zPathname);
4381 return SQLITE_NOMEM; 4629 return SQLITE_NOMEM;
4382 } 4630 }
4383 pPager = (Pager*)(pPtr); 4631 pPager = (Pager*)(pPtr);
4384 pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager))); 4632 pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
4385 pPager->fd = (sqlite3_file*)(pPtr += ROUND8(pcacheSize)); 4633 pPager->fd = (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
4386 pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile)); 4634 pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
4387 pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize); 4635 pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize);
4388 pPager->zFilename = (char*)(pPtr += journalFileSize); 4636 pPager->zFilename = (char*)(pPtr += journalFileSize);
4389 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) ); 4637 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
4390 4638
4391 /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */ 4639 /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
4392 if( zPathname ){ 4640 if( zPathname ){
4393 assert( nPathname>0 ); 4641 assert( nPathname>0 );
4394 pPager->zJournal = (char*)(pPtr += nPathname + 1); 4642 pPager->zJournal = (char*)(pPtr += nPathname + 1 + nUri);
4395 memcpy(pPager->zFilename, zPathname, nPathname); 4643 memcpy(pPager->zFilename, zPathname, nPathname);
4644 if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
4396 memcpy(pPager->zJournal, zPathname, nPathname); 4645 memcpy(pPager->zJournal, zPathname, nPathname);
4397 memcpy(&pPager->zJournal[nPathname], "-journal", 8); 4646 memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2);
4647 sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
4398 #ifndef SQLITE_OMIT_WAL 4648 #ifndef SQLITE_OMIT_WAL
4399 pPager->zWal = &pPager->zJournal[nPathname+8+1]; 4649 pPager->zWal = &pPager->zJournal[nPathname+8+1];
4400 memcpy(pPager->zWal, zPathname, nPathname); 4650 memcpy(pPager->zWal, zPathname, nPathname);
4401 memcpy(&pPager->zWal[nPathname], "-wal", 4); 4651 memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
4652 sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
4402 #endif 4653 #endif
4403 sqlite3_free(zPathname); 4654 sqlite3DbFree(0, zPathname);
4404 } 4655 }
4405 pPager->pVfs = pVfs; 4656 pPager->pVfs = pVfs;
4406 pPager->vfsFlags = vfsFlags; 4657 pPager->vfsFlags = vfsFlags;
4407 4658
4408 /* Open the pager file. 4659 /* Open the pager file.
4409 */ 4660 */
4410 if( zFilename && zFilename[0] ){ 4661 if( zFilename && zFilename[0] ){
4411 int fout = 0; /* VFS flags returned by xOpen() */ 4662 int fout = 0; /* VFS flags returned by xOpen() */
4412 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout); 4663 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
4413 assert( !memDb ); 4664 assert( !memDb );
4414 readOnly = (fout&SQLITE_OPEN_READONLY); 4665 readOnly = (fout&SQLITE_OPEN_READONLY);
4415 4666
4416 /* If the file was successfully opened for read/write access, 4667 /* If the file was successfully opened for read/write access,
4417 ** choose a default page size in case we have to create the 4668 ** choose a default page size in case we have to create the
4418 ** database file. The default page size is the maximum of: 4669 ** database file. The default page size is the maximum of:
4419 ** 4670 **
4420 ** + SQLITE_DEFAULT_PAGE_SIZE, 4671 ** + SQLITE_DEFAULT_PAGE_SIZE,
4421 ** + The value returned by sqlite3OsSectorSize() 4672 ** + The value returned by sqlite3OsSectorSize()
4422 ** + The largest page size that can be written atomically. 4673 ** + The largest page size that can be written atomically.
4423 */ 4674 */
4424 if( rc==SQLITE_OK && !readOnly ){ 4675 if( rc==SQLITE_OK ){
4425 setSectorSize(pPager); 4676 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
4426 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE); 4677 if( !readOnly ){
4427 if( szPageDflt<pPager->sectorSize ){ 4678 setSectorSize(pPager);
4428 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){ 4679 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
4429 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE; 4680 if( szPageDflt<pPager->sectorSize ){
4430 }else{ 4681 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
4431 szPageDflt = (u32)pPager->sectorSize; 4682 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
4432 } 4683 }else{
4433 } 4684 szPageDflt = (u32)pPager->sectorSize;
4434 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
4435 {
4436 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
4437 int ii;
4438 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
4439 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
4440 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
4441 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
4442 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
4443 szPageDflt = ii;
4444 } 4685 }
4445 } 4686 }
4687 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
4688 {
4689 int ii;
4690 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
4691 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
4692 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
4693 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
4694 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
4695 szPageDflt = ii;
4696 }
4697 }
4698 }
4699 #endif
4446 } 4700 }
4447 #endif 4701 pPager->noLock = sqlite3_uri_boolean(zFilename, "nolock", 0);
4702 if( (iDc & SQLITE_IOCAP_IMMUTABLE)!=0
4703 || sqlite3_uri_boolean(zFilename, "immutable", 0) ){
4704 vfsFlags |= SQLITE_OPEN_READONLY;
4705 goto act_like_temp_file;
4706 }
4448 } 4707 }
4449 }else{ 4708 }else{
4450 /* If a temporary file is requested, it is not opened immediately. 4709 /* If a temporary file is requested, it is not opened immediately.
4451 ** In this case we accept the default page size and delay actually 4710 ** In this case we accept the default page size and delay actually
4452 ** opening the file until the first call to OsWrite(). 4711 ** opening the file until the first call to OsWrite().
4453 ** 4712 **
4454 ** This branch is also run for an in-memory database. An in-memory 4713 ** This branch is also run for an in-memory database. An in-memory
4455 ** database is the same as a temp-file that is never written out to 4714 ** database is the same as a temp-file that is never written out to
4456 ** disk and uses an in-memory rollback journal. 4715 ** disk and uses an in-memory rollback journal.
4716 **
4717 ** This branch also runs for files marked as immutable.
4457 */ 4718 */
4719 act_like_temp_file:
4458 tempFile = 1; 4720 tempFile = 1;
4459 pPager->eState = PAGER_READER; 4721 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
4460 pPager->eLock = EXCLUSIVE_LOCK; 4722 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE locking mo de */
4723 pPager->noLock = 1; /* Do no locking */
4461 readOnly = (vfsFlags&SQLITE_OPEN_READONLY); 4724 readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
4462 } 4725 }
4463 4726
4464 /* The following call to PagerSetPagesize() serves to set the value of 4727 /* The following call to PagerSetPagesize() serves to set the value of
4465 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer. 4728 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
4466 */ 4729 */
4467 if( rc==SQLITE_OK ){ 4730 if( rc==SQLITE_OK ){
4468 assert( pPager->memDb==0 ); 4731 assert( pPager->memDb==0 );
4469 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1); 4732 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
4470 testcase( rc!=SQLITE_OK ); 4733 testcase( rc!=SQLITE_OK );
4471 } 4734 }
4472 4735
4473 /* If an error occurred in either of the blocks above, free the 4736 /* Initialize the PCache object. */
4474 ** Pager structure and close the file. 4737 if( rc==SQLITE_OK ){
4738 assert( nExtra<1000 );
4739 nExtra = ROUND8(nExtra);
4740 rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
4741 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache );
4742 }
4743
4744 /* If an error occurred above, free the Pager structure and close the file.
4475 */ 4745 */
4476 if( rc!=SQLITE_OK ){ 4746 if( rc!=SQLITE_OK ){
4477 assert( !pPager->pTmpSpace );
4478 sqlite3OsClose(pPager->fd); 4747 sqlite3OsClose(pPager->fd);
4748 sqlite3PageFree(pPager->pTmpSpace);
4479 sqlite3_free(pPager); 4749 sqlite3_free(pPager);
4480 return rc; 4750 return rc;
4481 } 4751 }
4482 4752
4483 /* Initialize the PCache object. */
4484 assert( nExtra<1000 );
4485 nExtra = ROUND8(nExtra);
4486 sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
4487 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
4488
4489 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename)); 4753 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
4490 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename)) 4754 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
4491 4755
4492 pPager->useJournal = (u8)useJournal; 4756 pPager->useJournal = (u8)useJournal;
4493 pPager->noReadlock = (noReadlock && readOnly) ?1:0;
4494 /* pPager->stmtOpen = 0; */ 4757 /* pPager->stmtOpen = 0; */
4495 /* pPager->stmtInUse = 0; */ 4758 /* pPager->stmtInUse = 0; */
4496 /* pPager->nRef = 0; */ 4759 /* pPager->nRef = 0; */
4497 /* pPager->stmtSize = 0; */ 4760 /* pPager->stmtSize = 0; */
4498 /* pPager->stmtJSize = 0; */ 4761 /* pPager->stmtJSize = 0; */
4499 /* pPager->nPage = 0; */ 4762 /* pPager->nPage = 0; */
4500 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT; 4763 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
4501 /* pPager->state = PAGER_UNLOCK; */ 4764 /* pPager->state = PAGER_UNLOCK; */
4502 #if 0
4503 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
4504 #endif
4505 /* pPager->errMask = 0; */ 4765 /* pPager->errMask = 0; */
4506 pPager->tempFile = (u8)tempFile; 4766 pPager->tempFile = (u8)tempFile;
4507 assert( tempFile==PAGER_LOCKINGMODE_NORMAL 4767 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
4508 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE ); 4768 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
4509 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 ); 4769 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
4510 pPager->exclusiveMode = (u8)tempFile; 4770 pPager->exclusiveMode = (u8)tempFile;
4511 pPager->changeCountDone = pPager->tempFile; 4771 pPager->changeCountDone = pPager->tempFile;
4512 pPager->memDb = (u8)memDb; 4772 pPager->memDb = (u8)memDb;
4513 pPager->readOnly = (u8)readOnly; 4773 pPager->readOnly = (u8)readOnly;
4514 assert( useJournal || pPager->tempFile ); 4774 assert( useJournal || pPager->tempFile );
4515 pPager->noSync = pPager->tempFile; 4775 pPager->noSync = pPager->tempFile;
4516 pPager->fullSync = pPager->noSync ?0:1; 4776 if( pPager->noSync ){
4517 pPager->syncFlags = pPager->noSync ? 0 : SQLITE_SYNC_NORMAL; 4777 assert( pPager->fullSync==0 );
4518 pPager->ckptSyncFlags = pPager->syncFlags; 4778 assert( pPager->syncFlags==0 );
4779 assert( pPager->walSyncFlags==0 );
4780 assert( pPager->ckptSyncFlags==0 );
4781 }else{
4782 pPager->fullSync = 1;
4783 pPager->syncFlags = SQLITE_SYNC_NORMAL;
4784 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
4785 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
4786 }
4519 /* pPager->pFirst = 0; */ 4787 /* pPager->pFirst = 0; */
4520 /* pPager->pFirstSynced = 0; */ 4788 /* pPager->pFirstSynced = 0; */
4521 /* pPager->pLast = 0; */ 4789 /* pPager->pLast = 0; */
4522 pPager->nExtra = (u16)nExtra; 4790 pPager->nExtra = (u16)nExtra;
4523 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT; 4791 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
4524 assert( isOpen(pPager->fd) || tempFile ); 4792 assert( isOpen(pPager->fd) || tempFile );
4525 setSectorSize(pPager); 4793 setSectorSize(pPager);
4526 if( !useJournal ){ 4794 if( !useJournal ){
4527 pPager->journalMode = PAGER_JOURNALMODE_OFF; 4795 pPager->journalMode = PAGER_JOURNALMODE_OFF;
4528 }else if( memDb ){ 4796 }else if( memDb ){
4529 pPager->journalMode = PAGER_JOURNALMODE_MEMORY; 4797 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
4530 } 4798 }
4531 /* pPager->xBusyHandler = 0; */ 4799 /* pPager->xBusyHandler = 0; */
4532 /* pPager->pBusyHandlerArg = 0; */ 4800 /* pPager->pBusyHandlerArg = 0; */
4533 pPager->xReiniter = xReinit; 4801 pPager->xReiniter = xReinit;
4534 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ 4802 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
4803 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
4535 4804
4536 *ppPager = pPager; 4805 *ppPager = pPager;
4537 return SQLITE_OK; 4806 return SQLITE_OK;
4538 } 4807 }
4539 4808
4540 4809
4810 /* Verify that the database file has not be deleted or renamed out from
4811 ** under the pager. Return SQLITE_OK if the database is still were it ought
4812 ** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
4813 ** code from sqlite3OsAccess()) if the database has gone missing.
4814 */
4815 static int databaseIsUnmoved(Pager *pPager){
4816 int bHasMoved = 0;
4817 int rc;
4818
4819 if( pPager->tempFile ) return SQLITE_OK;
4820 if( pPager->dbSize==0 ) return SQLITE_OK;
4821 assert( pPager->zFilename && pPager->zFilename[0] );
4822 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
4823 if( rc==SQLITE_NOTFOUND ){
4824 /* If the HAS_MOVED file-control is unimplemented, assume that the file
4825 ** has not been moved. That is the historical behavior of SQLite: prior to
4826 ** version 3.8.3, it never checked */
4827 rc = SQLITE_OK;
4828 }else if( rc==SQLITE_OK && bHasMoved ){
4829 rc = SQLITE_READONLY_DBMOVED;
4830 }
4831 return rc;
4832 }
4833
4541 4834
4542 /* 4835 /*
4543 ** This function is called after transitioning from PAGER_UNLOCK to 4836 ** This function is called after transitioning from PAGER_UNLOCK to
4544 ** PAGER_SHARED state. It tests if there is a hot journal present in 4837 ** PAGER_SHARED state. It tests if there is a hot journal present in
4545 ** the file-system for the given pager. A hot journal is one that 4838 ** the file-system for the given pager. A hot journal is one that
4546 ** needs to be played back. According to this function, a hot-journal 4839 ** needs to be played back. According to this function, a hot-journal
4547 ** file exists if the following criteria are met: 4840 ** file exists if the following criteria are met:
4548 ** 4841 **
4549 ** * The journal file exists in the file system, and 4842 ** * The journal file exists in the file system, and
4550 ** * No process holds a RESERVED or greater lock on the database file, and 4843 ** * No process holds a RESERVED or greater lock on the database file, and
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
4596 ** call above, but then delete the journal and drop the lock before 4889 ** call above, but then delete the journal and drop the lock before
4597 ** we get to the following sqlite3OsCheckReservedLock() call. If that 4890 ** we get to the following sqlite3OsCheckReservedLock() call. If that
4598 ** is the case, this routine might think there is a hot journal when 4891 ** is the case, this routine might think there is a hot journal when
4599 ** in fact there is none. This results in a false-positive which will 4892 ** in fact there is none. This results in a false-positive which will
4600 ** be dealt with by the playback routine. Ticket #3883. 4893 ** be dealt with by the playback routine. Ticket #3883.
4601 */ 4894 */
4602 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked); 4895 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
4603 if( rc==SQLITE_OK && !locked ){ 4896 if( rc==SQLITE_OK && !locked ){
4604 Pgno nPage; /* Number of pages in database file */ 4897 Pgno nPage; /* Number of pages in database file */
4605 4898
4606 /* Check the size of the database file. If it consists of 0 pages,
4607 ** then delete the journal file. See the header comment above for
4608 ** the reasoning here. Delete the obsolete journal file under
4609 ** a RESERVED lock to avoid race conditions and to avoid violating
4610 ** [H33020].
4611 */
4612 rc = pagerPagecount(pPager, &nPage); 4899 rc = pagerPagecount(pPager, &nPage);
4613 if( rc==SQLITE_OK ){ 4900 if( rc==SQLITE_OK ){
4614 if( nPage==0 ){ 4901 /* If the database is zero pages in size, that means that either (1) the
4902 ** journal is a remnant from a prior database with the same name where
4903 ** the database file but not the journal was deleted, or (2) the initial
4904 ** transaction that populates a new database is being rolled back.
4905 ** In either case, the journal file can be deleted. However, take care
4906 ** not to delete the journal file if it is already open due to
4907 ** journal_mode=PERSIST.
4908 */
4909 if( nPage==0 && !jrnlOpen ){
4615 sqlite3BeginBenignMalloc(); 4910 sqlite3BeginBenignMalloc();
4616 if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){ 4911 if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
4617 sqlite3OsDelete(pVfs, pPager->zJournal, 0); 4912 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
4618 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK); 4913 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
4619 } 4914 }
4620 sqlite3EndBenignMalloc(); 4915 sqlite3EndBenignMalloc();
4621 }else{ 4916 }else{
4622 /* The journal file exists and no other connection has a reserved 4917 /* The journal file exists and no other connection has a reserved
4623 ** or greater lock on the database file. Now check that there is 4918 ** or greater lock on the database file. Now check that there is
4624 ** at least one non-zero bytes at the start of the journal file. 4919 ** at least one non-zero bytes at the start of the journal file.
4625 ** If there is, then we consider this journal to be hot. If not, 4920 ** If there is, then we consider this journal to be hot. If not,
4626 ** it can be ignored. 4921 ** it can be ignored.
4627 */ 4922 */
4628 if( !jrnlOpen ){ 4923 if( !jrnlOpen ){
4629 int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL; 4924 int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
4630 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f); 4925 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
4631 } 4926 }
4632 if( rc==SQLITE_OK ){ 4927 if( rc==SQLITE_OK ){
4633 u8 first = 0; 4928 u8 first = 0;
4634 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0); 4929 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
4635 if( rc==SQLITE_IOERR_SHORT_READ ){ 4930 if( rc==SQLITE_IOERR_SHORT_READ ){
4636 rc = SQLITE_OK; 4931 rc = SQLITE_OK;
4637 } 4932 }
4638 if( !jrnlOpen ){ 4933 if( !jrnlOpen ){
4639 sqlite3OsClose(pPager->jfd); 4934 sqlite3OsClose(pPager->jfd);
4640 } 4935 }
4641 *pExists = (first!=0); 4936 *pExists = (first!=0);
4642 }else if( rc==SQLITE_CANTOPEN ){ 4937 }else if( rc==SQLITE_CANTOPEN ){
4643 /* If we cannot open the rollback journal file in order to see if 4938 /* If we cannot open the rollback journal file in order to see if
4644 ** its has a zero header, that might be due to an I/O error, or 4939 ** it has a zero header, that might be due to an I/O error, or
4645 ** it might be due to the race condition described above and in 4940 ** it might be due to the race condition described above and in
4646 ** ticket #3883. Either way, assume that the journal is hot. 4941 ** ticket #3883. Either way, assume that the journal is hot.
4647 ** This might be a false positive. But if it is, then the 4942 ** This might be a false positive. But if it is, then the
4648 ** automatic journal playback and recovery mechanism will deal 4943 ** automatic journal playback and recovery mechanism will deal
4649 ** with it under an EXCLUSIVE lock where we do not need to 4944 ** with it under an EXCLUSIVE lock where we do not need to
4650 ** worry so much with race conditions. 4945 ** worry so much with race conditions.
4651 */ 4946 */
4652 *pExists = 1; 4947 *pExists = 1;
4653 rc = SQLITE_OK; 4948 rc = SQLITE_OK;
4654 } 4949 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
4697 */ 4992 */
4698 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 ); 4993 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
4699 assert( assert_pager_state(pPager) ); 4994 assert( assert_pager_state(pPager) );
4700 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER ); 4995 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
4701 if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; } 4996 if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
4702 4997
4703 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){ 4998 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
4704 int bHotJournal = 1; /* True if there exists a hot journal-file */ 4999 int bHotJournal = 1; /* True if there exists a hot journal-file */
4705 5000
4706 assert( !MEMDB ); 5001 assert( !MEMDB );
4707 assert( pPager->noReadlock==0 || pPager->readOnly );
4708 5002
4709 if( pPager->noReadlock==0 ){ 5003 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
4710 rc = pager_wait_on_lock(pPager, SHARED_LOCK); 5004 if( rc!=SQLITE_OK ){
4711 if( rc!=SQLITE_OK ){ 5005 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
4712 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK ); 5006 goto failed;
4713 goto failed;
4714 }
4715 } 5007 }
4716 5008
4717 /* If a journal file exists, and there is no RESERVED lock on the 5009 /* If a journal file exists, and there is no RESERVED lock on the
4718 ** database file, then it either needs to be played back or deleted. 5010 ** database file, then it either needs to be played back or deleted.
4719 */ 5011 */
4720 if( pPager->eLock<=SHARED_LOCK ){ 5012 if( pPager->eLock<=SHARED_LOCK ){
4721 rc = hasHotJournal(pPager, &bHotJournal); 5013 rc = hasHotJournal(pPager, &bHotJournal);
4722 } 5014 }
4723 if( rc!=SQLITE_OK ){ 5015 if( rc!=SQLITE_OK ){
4724 goto failed; 5016 goto failed;
4725 } 5017 }
4726 if( bHotJournal ){ 5018 if( bHotJournal ){
5019 if( pPager->readOnly ){
5020 rc = SQLITE_READONLY_ROLLBACK;
5021 goto failed;
5022 }
5023
4727 /* Get an EXCLUSIVE lock on the database file. At this point it is 5024 /* Get an EXCLUSIVE lock on the database file. At this point it is
4728 ** important that a RESERVED lock is not obtained on the way to the 5025 ** important that a RESERVED lock is not obtained on the way to the
4729 ** EXCLUSIVE lock. If it were, another process might open the 5026 ** EXCLUSIVE lock. If it were, another process might open the
4730 ** database file, detect the RESERVED lock, and conclude that the 5027 ** database file, detect the RESERVED lock, and conclude that the
4731 ** database is safe to read while this process is still rolling the 5028 ** database is safe to read while this process is still rolling the
4732 ** hot-journal back. 5029 ** hot-journal back.
4733 ** 5030 **
4734 ** Because the intermediate RESERVED lock is not requested, any 5031 ** Because the intermediate RESERVED lock is not requested, any
4735 ** other process attempting to access the database file will get to 5032 ** other process attempting to access the database file will get to
4736 ** this point in the code and fail to obtain its own EXCLUSIVE lock 5033 ** this point in the code and fail to obtain its own EXCLUSIVE lock
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
4814 pager_error(pPager, rc); 5111 pager_error(pPager, rc);
4815 goto failed; 5112 goto failed;
4816 } 5113 }
4817 5114
4818 assert( pPager->eState==PAGER_OPEN ); 5115 assert( pPager->eState==PAGER_OPEN );
4819 assert( (pPager->eLock==SHARED_LOCK) 5116 assert( (pPager->eLock==SHARED_LOCK)
4820 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK) 5117 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
4821 ); 5118 );
4822 } 5119 }
4823 5120
4824 if( !pPager->tempFile 5121 if( !pPager->tempFile && (
4825 && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0) 5122 pPager->pBackup
4826 ){ 5123 || sqlite3PcachePagecount(pPager->pPCache)>0
5124 || USEFETCH(pPager)
5125 )){
4827 /* The shared-lock has just been acquired on the database file 5126 /* The shared-lock has just been acquired on the database file
4828 ** and there are already pages in the cache (from a previous 5127 ** and there are already pages in the cache (from a previous
4829 ** read or write transaction). Check to see if the database 5128 ** read or write transaction). Check to see if the database
4830 ** has been modified. If the database has changed, flush the 5129 ** has been modified. If the database has changed, flush the
4831 ** cache. 5130 ** cache.
4832 ** 5131 **
4833 ** Database changes is detected by looking at 15 bytes beginning 5132 ** Database changes is detected by looking at 15 bytes beginning
4834 ** at offset 24 into the file. The first 4 of these 16 bytes are 5133 ** at offset 24 into the file. The first 4 of these 16 bytes are
4835 ** a 32-bit counter that is incremented with each change. The 5134 ** a 32-bit counter that is incremented with each change. The
4836 ** other bytes change randomly with each file change when 5135 ** other bytes change randomly with each file change when
4837 ** a codec is in use. 5136 ** a codec is in use.
4838 ** 5137 **
4839 ** There is a vanishingly small chance that a change will not be 5138 ** There is a vanishingly small chance that a change will not be
4840 ** detected. The chance of an undetected change is so small that 5139 ** detected. The chance of an undetected change is so small that
4841 ** it can be neglected. 5140 ** it can be neglected.
4842 */ 5141 */
4843 Pgno nPage = 0; 5142 Pgno nPage = 0;
4844 char dbFileVers[sizeof(pPager->dbFileVers)]; 5143 char dbFileVers[sizeof(pPager->dbFileVers)];
4845 5144
4846 rc = pagerPagecount(pPager, &nPage); 5145 rc = pagerPagecount(pPager, &nPage);
4847 if( rc ) goto failed; 5146 if( rc ) goto failed;
4848 5147
4849 if( nPage>0 ){ 5148 if( nPage>0 ){
4850 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); 5149 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
4851 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); 5150 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
4852 if( rc!=SQLITE_OK ){ 5151 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
4853 goto failed; 5152 goto failed;
4854 } 5153 }
4855 }else{ 5154 }else{
4856 memset(dbFileVers, 0, sizeof(dbFileVers)); 5155 memset(dbFileVers, 0, sizeof(dbFileVers));
4857 } 5156 }
4858 5157
4859 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){ 5158 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
4860 pager_reset(pPager); 5159 pager_reset(pPager);
5160
5161 /* Unmap the database file. It is possible that external processes
5162 ** may have truncated the database file and then extended it back
5163 ** to its original size while this process was not holding a lock.
5164 ** In this case there may exist a Pager.pMap mapping that appears
5165 ** to be the right size but is not actually valid. Avoid this
5166 ** possibility by unmapping the db here. */
5167 if( USEFETCH(pPager) ){
5168 sqlite3OsUnfetch(pPager->fd, 0, 0);
5169 }
4861 } 5170 }
4862 } 5171 }
4863 5172
4864 /* If there is a WAL file in the file-system, open this database in WAL 5173 /* If there is a WAL file in the file-system, open this database in WAL
4865 ** mode. Otherwise, the following function call is a no-op. 5174 ** mode. Otherwise, the following function call is a no-op.
4866 */ 5175 */
4867 rc = pagerOpenWalIfPresent(pPager); 5176 rc = pagerOpenWalIfPresent(pPager);
4868 #ifndef SQLITE_OMIT_WAL 5177 #ifndef SQLITE_OMIT_WAL
4869 assert( pPager->pWal==0 || rc==SQLITE_OK ); 5178 assert( pPager->pWal==0 || rc==SQLITE_OK );
4870 #endif 5179 #endif
(...skipping 21 matching lines...) Expand all
4892 5201
4893 /* 5202 /*
4894 ** If the reference count has reached zero, rollback any active 5203 ** If the reference count has reached zero, rollback any active
4895 ** transaction and unlock the pager. 5204 ** transaction and unlock the pager.
4896 ** 5205 **
4897 ** Except, in locking_mode=EXCLUSIVE when there is nothing to in 5206 ** Except, in locking_mode=EXCLUSIVE when there is nothing to in
4898 ** the rollback journal, the unlock is not performed and there is 5207 ** the rollback journal, the unlock is not performed and there is
4899 ** nothing to rollback, so this routine is a no-op. 5208 ** nothing to rollback, so this routine is a no-op.
4900 */ 5209 */
4901 static void pagerUnlockIfUnused(Pager *pPager){ 5210 static void pagerUnlockIfUnused(Pager *pPager){
4902 if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){ 5211 if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
4903 pagerUnlockAndRollback(pPager); 5212 pagerUnlockAndRollback(pPager);
4904 } 5213 }
4905 } 5214 }
4906 5215
4907 /* 5216 /*
4908 ** Acquire a reference to page number pgno in pager pPager (a page 5217 ** Acquire a reference to page number pgno in pager pPager (a page
4909 ** reference has type DbPage*). If the requested reference is 5218 ** reference has type DbPage*). If the requested reference is
4910 ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned. 5219 ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
4911 ** 5220 **
4912 ** If the requested page is already in the cache, it is returned. 5221 ** If the requested page is already in the cache, it is returned.
4913 ** Otherwise, a new page object is allocated and populated with data 5222 ** Otherwise, a new page object is allocated and populated with data
4914 ** read from the database file. In some cases, the pcache module may 5223 ** read from the database file. In some cases, the pcache module may
4915 ** choose not to allocate a new page object and may reuse an existing 5224 ** choose not to allocate a new page object and may reuse an existing
4916 ** object with no outstanding references. 5225 ** object with no outstanding references.
4917 ** 5226 **
4918 ** The extra data appended to a page is always initialized to zeros the 5227 ** The extra data appended to a page is always initialized to zeros the
4919 ** first time a page is loaded into memory. If the page requested is 5228 ** first time a page is loaded into memory. If the page requested is
4920 ** already in the cache when this function is called, then the extra 5229 ** already in the cache when this function is called, then the extra
4921 ** data is left as it was when the page object was last used. 5230 ** data is left as it was when the page object was last used.
4922 ** 5231 **
4923 ** If the database image is smaller than the requested page or if a 5232 ** If the database image is smaller than the requested page or if a
4924 ** non-zero value is passed as the noContent parameter and the 5233 ** non-zero value is passed as the noContent parameter and the
4925 ** requested page is not already stored in the cache, then no 5234 ** requested page is not already stored in the cache, then no
4926 ** actual disk read occurs. In this case the memory image of the 5235 ** actual disk read occurs. In this case the memory image of the
4927 ** page is initialized to all zeros. 5236 ** page is initialized to all zeros.
4928 ** 5237 **
4929 ** If noContent is true, it means that we do not care about the contents 5238 ** If noContent is true, it means that we do not care about the contents
4930 ** of the page. This occurs in two seperate scenarios: 5239 ** of the page. This occurs in two scenarios:
4931 ** 5240 **
4932 ** a) When reading a free-list leaf page from the database, and 5241 ** a) When reading a free-list leaf page from the database, and
4933 ** 5242 **
4934 ** b) When a savepoint is being rolled back and we need to load 5243 ** b) When a savepoint is being rolled back and we need to load
4935 ** a new page into the cache to be filled with the data read 5244 ** a new page into the cache to be filled with the data read
4936 ** from the savepoint journal. 5245 ** from the savepoint journal.
4937 ** 5246 **
4938 ** If noContent is true, then the data returned is zeroed instead of 5247 ** If noContent is true, then the data returned is zeroed instead of
4939 ** being read from the database. Additionally, the bits corresponding 5248 ** being read from the database. Additionally, the bits corresponding
4940 ** to pgno in Pager.pInJournal (bitvec of pages already written to the 5249 ** to pgno in Pager.pInJournal (bitvec of pages already written to the
(...skipping 10 matching lines...) Expand all
4951 ** in memory, this routine goes to disk to read it in whereas Lookup() 5260 ** in memory, this routine goes to disk to read it in whereas Lookup()
4952 ** just returns 0. This routine acquires a read-lock the first time it 5261 ** just returns 0. This routine acquires a read-lock the first time it
4953 ** has to go to disk, and could also playback an old journal if necessary. 5262 ** has to go to disk, and could also playback an old journal if necessary.
4954 ** Since Lookup() never goes to disk, it never has to deal with locks 5263 ** Since Lookup() never goes to disk, it never has to deal with locks
4955 ** or journal files. 5264 ** or journal files.
4956 */ 5265 */
4957 int sqlite3PagerAcquire( 5266 int sqlite3PagerAcquire(
4958 Pager *pPager, /* The pager open on the database file */ 5267 Pager *pPager, /* The pager open on the database file */
4959 Pgno pgno, /* Page number to fetch */ 5268 Pgno pgno, /* Page number to fetch */
4960 DbPage **ppPage, /* Write a pointer to the page here */ 5269 DbPage **ppPage, /* Write a pointer to the page here */
4961 int noContent /* Do not bother reading content from disk if true */ 5270 int flags /* PAGER_GET_XXX flags */
4962 ){ 5271 ){
4963 int rc; 5272 int rc = SQLITE_OK;
4964 PgHdr *pPg; 5273 PgHdr *pPg = 0;
5274 u32 iFrame = 0; /* Frame to read from WAL file */
5275 const int noContent = (flags & PAGER_GET_NOCONTENT);
5276
5277 /* It is acceptable to use a read-only (mmap) page for any page except
5278 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
5279 ** flag was specified by the caller. And so long as the db is not a
5280 ** temporary or in-memory database. */
5281 const int bMmapOk = (pgno!=1 && USEFETCH(pPager)
5282 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
5283 #ifdef SQLITE_HAS_CODEC
5284 && pPager->xCodec==0
5285 #endif
5286 );
4965 5287
4966 assert( pPager->eState>=PAGER_READER ); 5288 assert( pPager->eState>=PAGER_READER );
4967 assert( assert_pager_state(pPager) ); 5289 assert( assert_pager_state(pPager) );
5290 assert( noContent==0 || bMmapOk==0 );
4968 5291
4969 if( pgno==0 ){ 5292 if( pgno==0 ){
4970 return SQLITE_CORRUPT_BKPT; 5293 return SQLITE_CORRUPT_BKPT;
4971 } 5294 }
4972 5295
4973 /* If the pager is in the error state, return an error immediately. 5296 /* If the pager is in the error state, return an error immediately.
4974 ** Otherwise, request the page from the PCache layer. */ 5297 ** Otherwise, request the page from the PCache layer. */
4975 if( pPager->errCode!=SQLITE_OK ){ 5298 if( pPager->errCode!=SQLITE_OK ){
4976 rc = pPager->errCode; 5299 rc = pPager->errCode;
4977 }else{ 5300 }else{
4978 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage); 5301 if( bMmapOk && pagerUseWal(pPager) ){
5302 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
5303 if( rc!=SQLITE_OK ) goto pager_acquire_err;
5304 }
5305
5306 if( bMmapOk && iFrame==0 ){
5307 void *pData = 0;
5308
5309 rc = sqlite3OsFetch(pPager->fd,
5310 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
5311 );
5312
5313 if( rc==SQLITE_OK && pData ){
5314 if( pPager->eState>PAGER_READER ){
5315 pPg = sqlite3PagerLookup(pPager, pgno);
5316 }
5317 if( pPg==0 ){
5318 rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
5319 }else{
5320 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
5321 }
5322 if( pPg ){
5323 assert( rc==SQLITE_OK );
5324 *ppPage = pPg;
5325 return SQLITE_OK;
5326 }
5327 }
5328 if( rc!=SQLITE_OK ){
5329 goto pager_acquire_err;
5330 }
5331 }
5332
5333 {
5334 sqlite3_pcache_page *pBase;
5335 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
5336 if( pBase==0 ){
5337 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
5338 if( rc!=SQLITE_OK ) goto pager_acquire_err;
5339 }
5340 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
5341 if( pPg==0 ) rc = SQLITE_NOMEM;
5342 }
4979 } 5343 }
4980 5344
4981 if( rc!=SQLITE_OK ){ 5345 if( rc!=SQLITE_OK ){
4982 /* Either the call to sqlite3PcacheFetch() returned an error or the 5346 /* Either the call to sqlite3PcacheFetch() returned an error or the
4983 ** pager was already in the error-state when this function was called. 5347 ** pager was already in the error-state when this function was called.
4984 ** Set pPg to 0 and jump to the exception handler. */ 5348 ** Set pPg to 0 and jump to the exception handler. */
4985 pPg = 0; 5349 pPg = 0;
4986 goto pager_acquire_err; 5350 goto pager_acquire_err;
4987 } 5351 }
4988 assert( (*ppPage)->pgno==pgno ); 5352 assert( (*ppPage)->pgno==pgno );
4989 assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 ); 5353 assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
4990 5354
4991 if( (*ppPage)->pPager && !noContent ){ 5355 if( (*ppPage)->pPager && !noContent ){
4992 /* In this case the pcache already contains an initialized copy of 5356 /* In this case the pcache already contains an initialized copy of
4993 ** the page. Return without further ado. */ 5357 ** the page. Return without further ado. */
4994 assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) ); 5358 assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
4995 PAGER_INCR(pPager->nHit); 5359 pPager->aStat[PAGER_STAT_HIT]++;
4996 return SQLITE_OK; 5360 return SQLITE_OK;
4997 5361
4998 }else{ 5362 }else{
4999 /* The pager cache has created a new page. Its content needs to 5363 /* The pager cache has created a new page. Its content needs to
5000 ** be initialized. */ 5364 ** be initialized. */
5001 5365
5002 PAGER_INCR(pPager->nMiss);
5003 pPg = *ppPage; 5366 pPg = *ppPage;
5004 pPg->pPager = pPager; 5367 pPg->pPager = pPager;
5005 5368
5006 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page 5369 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
5007 ** number greater than this, or the unused locking-page, is requested. */ 5370 ** number greater than this, or the unused locking-page, is requested. */
5008 if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){ 5371 if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
5009 rc = SQLITE_CORRUPT_BKPT; 5372 rc = SQLITE_CORRUPT_BKPT;
5010 goto pager_acquire_err; 5373 goto pager_acquire_err;
5011 } 5374 }
5012 5375
(...skipping 14 matching lines...) Expand all
5027 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno); 5390 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
5028 testcase( rc==SQLITE_NOMEM ); 5391 testcase( rc==SQLITE_NOMEM );
5029 } 5392 }
5030 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno); 5393 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
5031 testcase( rc==SQLITE_NOMEM ); 5394 testcase( rc==SQLITE_NOMEM );
5032 sqlite3EndBenignMalloc(); 5395 sqlite3EndBenignMalloc();
5033 } 5396 }
5034 memset(pPg->pData, 0, pPager->pageSize); 5397 memset(pPg->pData, 0, pPager->pageSize);
5035 IOTRACE(("ZERO %p %d\n", pPager, pgno)); 5398 IOTRACE(("ZERO %p %d\n", pPager, pgno));
5036 }else{ 5399 }else{
5400 if( pagerUseWal(pPager) && bMmapOk==0 ){
5401 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
5402 if( rc!=SQLITE_OK ) goto pager_acquire_err;
5403 }
5037 assert( pPg->pPager==pPager ); 5404 assert( pPg->pPager==pPager );
5038 rc = readDbPage(pPg); 5405 pPager->aStat[PAGER_STAT_MISS]++;
5406 rc = readDbPage(pPg, iFrame);
5039 if( rc!=SQLITE_OK ){ 5407 if( rc!=SQLITE_OK ){
5040 goto pager_acquire_err; 5408 goto pager_acquire_err;
5041 } 5409 }
5042 } 5410 }
5043 pager_set_pagehash(pPg); 5411 pager_set_pagehash(pPg);
5044 } 5412 }
5045 5413
5046 return SQLITE_OK; 5414 return SQLITE_OK;
5047 5415
5048 pager_acquire_err: 5416 pager_acquire_err:
(...skipping 12 matching lines...) Expand all
5061 ** not read the page from disk. Return a pointer to the page, 5429 ** not read the page from disk. Return a pointer to the page,
5062 ** or 0 if the page is not in cache. 5430 ** or 0 if the page is not in cache.
5063 ** 5431 **
5064 ** See also sqlite3PagerGet(). The difference between this routine 5432 ** See also sqlite3PagerGet(). The difference between this routine
5065 ** and sqlite3PagerGet() is that _get() will go to the disk and read 5433 ** and sqlite3PagerGet() is that _get() will go to the disk and read
5066 ** in the page if the page is not already in cache. This routine 5434 ** in the page if the page is not already in cache. This routine
5067 ** returns NULL if the page is not in cache or if a disk I/O error 5435 ** returns NULL if the page is not in cache or if a disk I/O error
5068 ** has ever happened. 5436 ** has ever happened.
5069 */ 5437 */
5070 DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){ 5438 DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
5071 PgHdr *pPg = 0; 5439 sqlite3_pcache_page *pPage;
5072 assert( pPager!=0 ); 5440 assert( pPager!=0 );
5073 assert( pgno!=0 ); 5441 assert( pgno!=0 );
5074 assert( pPager->pPCache!=0 ); 5442 assert( pPager->pPCache!=0 );
5075 assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR ); 5443 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
5076 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg); 5444 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
5077 return pPg;
5078 } 5445 }
5079 5446
5080 /* 5447 /*
5081 ** Release a page reference. 5448 ** Release a page reference.
5082 ** 5449 **
5083 ** If the number of references to the page drop to zero, then the 5450 ** If the number of references to the page drop to zero, then the
5084 ** page is added to the LRU list. When all references to all pages 5451 ** page is added to the LRU list. When all references to all pages
5085 ** are released, a rollback occurs and the lock on the database is 5452 ** are released, a rollback occurs and the lock on the database is
5086 ** removed. 5453 ** removed.
5087 */ 5454 */
5455 void sqlite3PagerUnrefNotNull(DbPage *pPg){
5456 Pager *pPager;
5457 assert( pPg!=0 );
5458 pPager = pPg->pPager;
5459 if( pPg->flags & PGHDR_MMAP ){
5460 pagerReleaseMapPage(pPg);
5461 }else{
5462 sqlite3PcacheRelease(pPg);
5463 }
5464 pagerUnlockIfUnused(pPager);
5465 }
5088 void sqlite3PagerUnref(DbPage *pPg){ 5466 void sqlite3PagerUnref(DbPage *pPg){
5089 if( pPg ){ 5467 if( pPg ) sqlite3PagerUnrefNotNull(pPg);
5090 Pager *pPager = pPg->pPager;
5091 sqlite3PcacheRelease(pPg);
5092 pagerUnlockIfUnused(pPager);
5093 }
5094 } 5468 }
5095 5469
5096 #if defined(__APPLE__)
5097 /*
5098 ** Create and return a CFURLRef given a cstring containing the path to a file.
5099 */
5100 static CFURLRef create_cfurl_from_cstring(const char* filePath){
5101 CFStringRef urlString = CFStringCreateWithFileSystemRepresentation(
5102 kCFAllocatorDefault, filePath);
5103 CFURLRef urlRef = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
5104 urlString, kCFURLPOSIXPathStyle, FALSE);
5105 CFRelease(urlString);
5106 return urlRef;
5107 }
5108 #endif
5109
5110 /* 5470 /*
5111 ** This function is called at the start of every write transaction. 5471 ** This function is called at the start of every write transaction.
5112 ** There must already be a RESERVED or EXCLUSIVE lock on the database 5472 ** There must already be a RESERVED or EXCLUSIVE lock on the database
5113 ** file when this routine is called. 5473 ** file when this routine is called.
5114 ** 5474 **
5115 ** Open the journal file for pager pPager and write a journal header 5475 ** Open the journal file for pager pPager and write a journal header
5116 ** to the start of it. If there are active savepoints, open the sub-journal 5476 ** to the start of it. If there are active savepoints, open the sub-journal
5117 ** as well. This function is only used when the journal file is being 5477 ** as well. This function is only used when the journal file is being
5118 ** opened to write a rollback log for a transaction. It is not used 5478 ** opened to write a rollback log for a transaction. It is not used
5119 ** when opening a hot journal file to roll it back. 5479 ** when opening a hot journal file to roll it back.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
5152 if( !isOpen(pPager->jfd) ){ 5512 if( !isOpen(pPager->jfd) ){
5153 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){ 5513 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
5154 sqlite3MemJournalOpen(pPager->jfd); 5514 sqlite3MemJournalOpen(pPager->jfd);
5155 }else{ 5515 }else{
5156 const int flags = /* VFS flags to open journal file */ 5516 const int flags = /* VFS flags to open journal file */
5157 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| 5517 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
5158 (pPager->tempFile ? 5518 (pPager->tempFile ?
5159 (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL): 5519 (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
5160 (SQLITE_OPEN_MAIN_JOURNAL) 5520 (SQLITE_OPEN_MAIN_JOURNAL)
5161 ); 5521 );
5162 #ifdef SQLITE_ENABLE_ATOMIC_WRITE 5522
5163 rc = sqlite3JournalOpen( 5523 /* Verify that the database still has the same name as it did when
5164 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager) 5524 ** it was originally opened. */
5165 ); 5525 rc = databaseIsUnmoved(pPager);
5166 #else 5526 if( rc==SQLITE_OK ){
5167 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0); 5527 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
5168 #endif 5528 rc = sqlite3JournalOpen(
5169 #if defined(__APPLE__) 5529 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
5170 /* Set the TimeMachine exclusion metadata for the journal if it has 5530 );
5171 ** been set for the database. Only do this for unix-type vfs 5531 #else
5172 ** implementations. */ 5532 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
5173 if( rc==SQLITE_OK && pPager->zFilename!=NULL 5533 #endif
5174 && strlen(pPager->zFilename)>0
5175 && strncmp(pVfs->zName, "unix", 4)==0
5176 && ( pVfs->zName[4]=='-' || pVfs->zName[4]=='\0' ) ){
5177 CFURLRef database = create_cfurl_from_cstring(pPager->zFilename);
5178 if( CSBackupIsItemExcluded(database, NULL) ){
5179 CFURLRef journal = create_cfurl_from_cstring(pPager->zJournal);
5180 /* Ignore errors from the following exclusion call. */
5181 CSBackupSetItemExcluded(journal, TRUE, FALSE);
5182 CFRelease(journal);
5183 }
5184 CFRelease(database);
5185 } 5534 }
5186 #endif
5187 } 5535 }
5188 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) ); 5536 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
5189 } 5537 }
5190 5538
5191 5539
5192 /* Write the first journal header to the journal file and open 5540 /* Write the first journal header to the journal file and open
5193 ** the sub-journal if necessary. 5541 ** the sub-journal if necessary.
5194 */ 5542 */
5195 if( rc==SQLITE_OK ){ 5543 if( rc==SQLITE_OK ){
5196 /* TODO: Check if all of these are really required. */ 5544 /* TODO: Check if all of these are really required. */
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
5297 } 5645 }
5298 5646
5299 /* 5647 /*
5300 ** Mark a single data page as writeable. The page is written into the 5648 ** Mark a single data page as writeable. The page is written into the
5301 ** main journal or sub-journal as required. If the page is written into 5649 ** main journal or sub-journal as required. If the page is written into
5302 ** one of the journals, the corresponding bit is set in the 5650 ** one of the journals, the corresponding bit is set in the
5303 ** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs 5651 ** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
5304 ** of any open savepoints as appropriate. 5652 ** of any open savepoints as appropriate.
5305 */ 5653 */
5306 static int pager_write(PgHdr *pPg){ 5654 static int pager_write(PgHdr *pPg){
5307 void *pData = pPg->pData;
5308 Pager *pPager = pPg->pPager; 5655 Pager *pPager = pPg->pPager;
5309 int rc = SQLITE_OK; 5656 int rc = SQLITE_OK;
5657 int inJournal;
5310 5658
5311 /* This routine is not called unless a write-transaction has already 5659 /* This routine is not called unless a write-transaction has already
5312 ** been started. The journal file may or may not be open at this point. 5660 ** been started. The journal file may or may not be open at this point.
5313 ** It is never called in the ERROR state. 5661 ** It is never called in the ERROR state.
5314 */ 5662 */
5315 assert( pPager->eState==PAGER_WRITER_LOCKED 5663 assert( pPager->eState==PAGER_WRITER_LOCKED
5316 || pPager->eState==PAGER_WRITER_CACHEMOD 5664 || pPager->eState==PAGER_WRITER_CACHEMOD
5317 || pPager->eState==PAGER_WRITER_DBMOD 5665 || pPager->eState==PAGER_WRITER_DBMOD
5318 ); 5666 );
5319 assert( assert_pager_state(pPager) ); 5667 assert( assert_pager_state(pPager) );
5320 5668 assert( pPager->errCode==0 );
5321 /* If an error has been previously detected, report the same error 5669 assert( pPager->readOnly==0 );
5322 ** again. This should not happen, but the check provides robustness. */
5323 if( NEVER(pPager->errCode) ) return pPager->errCode;
5324
5325 /* Higher-level routines never call this function if database is not
5326 ** writable. But check anyway, just for robustness. */
5327 if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
5328 5670
5329 CHECK_PAGE(pPg); 5671 CHECK_PAGE(pPg);
5330 5672
5331 /* The journal file needs to be opened. Higher level routines have already 5673 /* The journal file needs to be opened. Higher level routines have already
5332 ** obtained the necessary locks to begin the write-transaction, but the 5674 ** obtained the necessary locks to begin the write-transaction, but the
5333 ** rollback journal might not yet be open. Open it now if this is the case. 5675 ** rollback journal might not yet be open. Open it now if this is the case.
5334 ** 5676 **
5335 ** This is done before calling sqlite3PcacheMakeDirty() on the page. 5677 ** This is done before calling sqlite3PcacheMakeDirty() on the page.
5336 ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then 5678 ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
5337 ** an error might occur and the pager would end up in WRITER_LOCKED state 5679 ** an error might occur and the pager would end up in WRITER_LOCKED state
5338 ** with pages marked as dirty in the cache. 5680 ** with pages marked as dirty in the cache.
5339 */ 5681 */
5340 if( pPager->eState==PAGER_WRITER_LOCKED ){ 5682 if( pPager->eState==PAGER_WRITER_LOCKED ){
5341 rc = pager_open_journal(pPager); 5683 rc = pager_open_journal(pPager);
5342 if( rc!=SQLITE_OK ) return rc; 5684 if( rc!=SQLITE_OK ) return rc;
5343 } 5685 }
5344 assert( pPager->eState>=PAGER_WRITER_CACHEMOD ); 5686 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
5345 assert( assert_pager_state(pPager) ); 5687 assert( assert_pager_state(pPager) );
5346 5688
5347 /* Mark the page as dirty. If the page has already been written 5689 /* Mark the page as dirty. If the page has already been written
5348 ** to the journal then we can return right away. 5690 ** to the journal then we can return right away.
5349 */ 5691 */
5350 sqlite3PcacheMakeDirty(pPg); 5692 sqlite3PcacheMakeDirty(pPg);
5351 if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){ 5693 inJournal = pageInJournal(pPager, pPg);
5694 if( inJournal && (pPager->nSavepoint==0 || !subjRequiresPage(pPg)) ){
5352 assert( !pagerUseWal(pPager) ); 5695 assert( !pagerUseWal(pPager) );
5353 }else{ 5696 }else{
5354 5697
5355 /* The transaction journal now exists and we have a RESERVED or an 5698 /* The transaction journal now exists and we have a RESERVED or an
5356 ** EXCLUSIVE lock on the main database file. Write the current page to 5699 ** EXCLUSIVE lock on the main database file. Write the current page to
5357 ** the transaction journal if it is not there already. 5700 ** the transaction journal if it is not there already.
5358 */ 5701 */
5359 if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){ 5702 if( !inJournal && !pagerUseWal(pPager) ){
5360 assert( pagerUseWal(pPager)==0 ); 5703 assert( pagerUseWal(pPager)==0 );
5361 if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){ 5704 if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
5362 u32 cksum; 5705 u32 cksum;
5363 char *pData2; 5706 char *pData2;
5364 i64 iOff = pPager->journalOff; 5707 i64 iOff = pPager->journalOff;
5365 5708
5366 /* We should never write to the journal file the page that 5709 /* We should never write to the journal file the page that
5367 ** contains the database locks. The following assert verifies 5710 ** contains the database locks. The following assert verifies
5368 ** that we do not. */ 5711 ** that we do not. */
5369 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); 5712 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
5370 5713
5371 assert( pPager->journalHdr<=pPager->journalOff ); 5714 assert( pPager->journalHdr<=pPager->journalOff );
5372 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2); 5715 CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
5373 cksum = pager_cksum(pPager, (u8*)pData2); 5716 cksum = pager_cksum(pPager, (u8*)pData2);
5374 5717
5375 /* Even if an IO or diskfull error occurs while journalling the 5718 /* Even if an IO or diskfull error occurs while journalling the
5376 ** page in the block above, set the need-sync flag for the page. 5719 ** page in the block above, set the need-sync flag for the page.
5377 ** Otherwise, when the transaction is rolled back, the logic in 5720 ** Otherwise, when the transaction is rolled back, the logic in
5378 ** playback_one_page() will think that the page needs to be restored 5721 ** playback_one_page() will think that the page needs to be restored
5379 ** in the database file. And if an IO error occurs while doing so, 5722 ** in the database file. And if an IO error occurs while doing so,
5380 ** then corruption may follow. 5723 ** then corruption may follow.
5381 */ 5724 */
5382 pPg->flags |= PGHDR_NEED_SYNC; 5725 pPg->flags |= PGHDR_NEED_SYNC;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
5414 PAGERID(pPager), pPg->pgno, 5757 PAGERID(pPager), pPg->pgno,
5415 ((pPg->flags&PGHDR_NEED_SYNC)?1:0))); 5758 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
5416 } 5759 }
5417 } 5760 }
5418 5761
5419 /* If the statement journal is open and the page is not in it, 5762 /* If the statement journal is open and the page is not in it,
5420 ** then write the current page to the statement journal. Note that 5763 ** then write the current page to the statement journal. Note that
5421 ** the statement journal format differs from the standard journal format 5764 ** the statement journal format differs from the standard journal format
5422 ** in that it omits the checksums and the header. 5765 ** in that it omits the checksums and the header.
5423 */ 5766 */
5424 if( subjRequiresPage(pPg) ){ 5767 if( pPager->nSavepoint>0 && subjRequiresPage(pPg) ){
5425 rc = subjournalPage(pPg); 5768 rc = subjournalPage(pPg);
5426 } 5769 }
5427 } 5770 }
5428 5771
5429 /* Update the database size and return. 5772 /* Update the database size and return.
5430 */ 5773 */
5431 if( pPager->dbSize<pPg->pgno ){ 5774 if( pPager->dbSize<pPg->pgno ){
5432 pPager->dbSize = pPg->pgno; 5775 pPager->dbSize = pPg->pgno;
5433 } 5776 }
5434 return rc; 5777 return rc;
5435 } 5778 }
5436 5779
5437 /* 5780 /*
5781 ** This is a variant of sqlite3PagerWrite() that runs when the sector size
5782 ** is larger than the page size. SQLite makes the (reasonable) assumption that
5783 ** all bytes of a sector are written together by hardware. Hence, all bytes of
5784 ** a sector need to be journalled in case of a power loss in the middle of
5785 ** a write.
5786 **
5787 ** Usually, the sector size is less than or equal to the page size, in which
5788 ** case pages can be individually written. This routine only runs in the except ional
5789 ** case where the page size is smaller than the sector size.
5790 */
5791 static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
5792 int rc = SQLITE_OK; /* Return code */
5793 Pgno nPageCount; /* Total number of pages in database file */
5794 Pgno pg1; /* First page of the sector pPg is located on. */
5795 int nPage = 0; /* Number of pages starting at pg1 to journal * /
5796 int ii; /* Loop counter */
5797 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
5798 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
5799 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
5800
5801 /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
5802 ** a journal header to be written between the pages journaled by
5803 ** this function.
5804 */
5805 assert( !MEMDB );
5806 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
5807 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
5808
5809 /* This trick assumes that both the page-size and sector-size are
5810 ** an integer power of 2. It sets variable pg1 to the identifier
5811 ** of the first page of the sector pPg is located on.
5812 */
5813 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
5814
5815 nPageCount = pPager->dbSize;
5816 if( pPg->pgno>nPageCount ){
5817 nPage = (pPg->pgno - pg1)+1;
5818 }else if( (pg1+nPagePerSector-1)>nPageCount ){
5819 nPage = nPageCount+1-pg1;
5820 }else{
5821 nPage = nPagePerSector;
5822 }
5823 assert(nPage>0);
5824 assert(pg1<=pPg->pgno);
5825 assert((pg1+nPage)>pPg->pgno);
5826
5827 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
5828 Pgno pg = pg1+ii;
5829 PgHdr *pPage;
5830 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
5831 if( pg!=PAGER_MJ_PGNO(pPager) ){
5832 rc = sqlite3PagerGet(pPager, pg, &pPage);
5833 if( rc==SQLITE_OK ){
5834 rc = pager_write(pPage);
5835 if( pPage->flags&PGHDR_NEED_SYNC ){
5836 needSync = 1;
5837 }
5838 sqlite3PagerUnrefNotNull(pPage);
5839 }
5840 }
5841 }else if( (pPage = sqlite3PagerLookup(pPager, pg))!=0 ){
5842 if( pPage->flags&PGHDR_NEED_SYNC ){
5843 needSync = 1;
5844 }
5845 sqlite3PagerUnrefNotNull(pPage);
5846 }
5847 }
5848
5849 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
5850 ** starting at pg1, then it needs to be set for all of them. Because
5851 ** writing to any of these nPage pages may damage the others, the
5852 ** journal file must contain sync()ed copies of all of them
5853 ** before any of them can be written out to the database file.
5854 */
5855 if( rc==SQLITE_OK && needSync ){
5856 assert( !MEMDB );
5857 for(ii=0; ii<nPage; ii++){
5858 PgHdr *pPage = sqlite3PagerLookup(pPager, pg1+ii);
5859 if( pPage ){
5860 pPage->flags |= PGHDR_NEED_SYNC;
5861 sqlite3PagerUnrefNotNull(pPage);
5862 }
5863 }
5864 }
5865
5866 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
5867 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
5868 return rc;
5869 }
5870
5871 /*
5438 ** Mark a data page as writeable. This routine must be called before 5872 ** Mark a data page as writeable. This routine must be called before
5439 ** making changes to a page. The caller must check the return value 5873 ** making changes to a page. The caller must check the return value
5440 ** of this function and be careful not to change any page data unless 5874 ** of this function and be careful not to change any page data unless
5441 ** this routine returns SQLITE_OK. 5875 ** this routine returns SQLITE_OK.
5442 ** 5876 **
5443 ** The difference between this function and pager_write() is that this 5877 ** The difference between this function and pager_write() is that this
5444 ** function also deals with the special case where 2 or more pages 5878 ** function also deals with the special case where 2 or more pages
5445 ** fit on a single disk sector. In this case all co-resident pages 5879 ** fit on a single disk sector. In this case all co-resident pages
5446 ** must have been written to the journal file before returning. 5880 ** must have been written to the journal file before returning.
5447 ** 5881 **
5448 ** If an error occurs, SQLITE_NOMEM or an IO error code is returned 5882 ** If an error occurs, SQLITE_NOMEM or an IO error code is returned
5449 ** as appropriate. Otherwise, SQLITE_OK. 5883 ** as appropriate. Otherwise, SQLITE_OK.
5450 */ 5884 */
5451 int sqlite3PagerWrite(DbPage *pDbPage){ 5885 int sqlite3PagerWrite(PgHdr *pPg){
5452 int rc = SQLITE_OK; 5886 assert( (pPg->flags & PGHDR_MMAP)==0 );
5453 5887 assert( pPg->pPager->eState>=PAGER_WRITER_LOCKED );
5454 PgHdr *pPg = pDbPage; 5888 assert( pPg->pPager->eState!=PAGER_ERROR );
5455 Pager *pPager = pPg->pPager; 5889 assert( assert_pager_state(pPg->pPager) );
5456 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize); 5890 if( pPg->pPager->sectorSize > (u32)pPg->pPager->pageSize ){
5457 5891 return pagerWriteLargeSector(pPg);
5458 assert( pPager->eState>=PAGER_WRITER_LOCKED );
5459 assert( pPager->eState!=PAGER_ERROR );
5460 assert( assert_pager_state(pPager) );
5461
5462 if( nPagePerSector>1 ){
5463 Pgno nPageCount; /* Total number of pages in database file */
5464 Pgno pg1; /* First page of the sector pPg is located on. */
5465 int nPage = 0; /* Number of pages starting at pg1 to journal */
5466 int ii; /* Loop counter */
5467 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
5468
5469 /* Set the doNotSyncSpill flag to 1. This is because we cannot allow
5470 ** a journal header to be written between the pages journaled by
5471 ** this function.
5472 */
5473 assert( !MEMDB );
5474 assert( pPager->doNotSyncSpill==0 );
5475 pPager->doNotSyncSpill++;
5476
5477 /* This trick assumes that both the page-size and sector-size are
5478 ** an integer power of 2. It sets variable pg1 to the identifier
5479 ** of the first page of the sector pPg is located on.
5480 */
5481 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
5482
5483 nPageCount = pPager->dbSize;
5484 if( pPg->pgno>nPageCount ){
5485 nPage = (pPg->pgno - pg1)+1;
5486 }else if( (pg1+nPagePerSector-1)>nPageCount ){
5487 nPage = nPageCount+1-pg1;
5488 }else{
5489 nPage = nPagePerSector;
5490 }
5491 assert(nPage>0);
5492 assert(pg1<=pPg->pgno);
5493 assert((pg1+nPage)>pPg->pgno);
5494
5495 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
5496 Pgno pg = pg1+ii;
5497 PgHdr *pPage;
5498 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
5499 if( pg!=PAGER_MJ_PGNO(pPager) ){
5500 rc = sqlite3PagerGet(pPager, pg, &pPage);
5501 if( rc==SQLITE_OK ){
5502 rc = pager_write(pPage);
5503 if( pPage->flags&PGHDR_NEED_SYNC ){
5504 needSync = 1;
5505 }
5506 sqlite3PagerUnref(pPage);
5507 }
5508 }
5509 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
5510 if( pPage->flags&PGHDR_NEED_SYNC ){
5511 needSync = 1;
5512 }
5513 sqlite3PagerUnref(pPage);
5514 }
5515 }
5516
5517 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
5518 ** starting at pg1, then it needs to be set for all of them. Because
5519 ** writing to any of these nPage pages may damage the others, the
5520 ** journal file must contain sync()ed copies of all of them
5521 ** before any of them can be written out to the database file.
5522 */
5523 if( rc==SQLITE_OK && needSync ){
5524 assert( !MEMDB );
5525 for(ii=0; ii<nPage; ii++){
5526 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
5527 if( pPage ){
5528 pPage->flags |= PGHDR_NEED_SYNC;
5529 sqlite3PagerUnref(pPage);
5530 }
5531 }
5532 }
5533
5534 assert( pPager->doNotSyncSpill==1 );
5535 pPager->doNotSyncSpill--;
5536 }else{ 5892 }else{
5537 rc = pager_write(pDbPage); 5893 return pager_write(pPg);
5538 } 5894 }
5539 return rc;
5540 } 5895 }
5541 5896
5542 /* 5897 /*
5543 ** Return TRUE if the page given in the argument was previously passed 5898 ** Return TRUE if the page given in the argument was previously passed
5544 ** to sqlite3PagerWrite(). In other words, return TRUE if it is ok 5899 ** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
5545 ** to change the content of the page. 5900 ** to change the content of the page.
5546 */ 5901 */
5547 #ifndef NDEBUG 5902 #ifndef NDEBUG
5548 int sqlite3PagerIswriteable(DbPage *pPg){ 5903 int sqlite3PagerIswriteable(DbPage *pPg){
5549 return pPg->flags&PGHDR_DIRTY; 5904 return pPg->flags&PGHDR_DIRTY;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
5615 ** "if( isDirect )" condition. 5970 ** "if( isDirect )" condition.
5616 */ 5971 */
5617 #ifndef SQLITE_ENABLE_ATOMIC_WRITE 5972 #ifndef SQLITE_ENABLE_ATOMIC_WRITE
5618 # define DIRECT_MODE 0 5973 # define DIRECT_MODE 0
5619 assert( isDirectMode==0 ); 5974 assert( isDirectMode==0 );
5620 UNUSED_PARAMETER(isDirectMode); 5975 UNUSED_PARAMETER(isDirectMode);
5621 #else 5976 #else
5622 # define DIRECT_MODE isDirectMode 5977 # define DIRECT_MODE isDirectMode
5623 #endif 5978 #endif
5624 5979
5625 if( !pPager->changeCountDone && pPager->dbSize>0 ){ 5980 if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
5626 PgHdr *pPgHdr; /* Reference to page 1 */ 5981 PgHdr *pPgHdr; /* Reference to page 1 */
5627 5982
5628 assert( !pPager->tempFile && isOpen(pPager->fd) ); 5983 assert( !pPager->tempFile && isOpen(pPager->fd) );
5629 5984
5630 /* Open page 1 of the file for writing. */ 5985 /* Open page 1 of the file for writing. */
5631 rc = sqlite3PagerGet(pPager, 1, &pPgHdr); 5986 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
5632 assert( pPgHdr==0 || rc==SQLITE_OK ); 5987 assert( pPgHdr==0 || rc==SQLITE_OK );
5633 5988
5634 /* If page one was fetched successfully, and this function is not 5989 /* If page one was fetched successfully, and this function is not
5635 ** operating in direct-mode, make page 1 writable. When not in 5990 ** operating in direct-mode, make page 1 writable. When not in
5636 ** direct mode, page 1 is always held in cache and hence the PagerGet() 5991 ** direct mode, page 1 is always held in cache and hence the PagerGet()
5637 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK. 5992 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
5638 */ 5993 */
5639 if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){ 5994 if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
5640 rc = sqlite3PagerWrite(pPgHdr); 5995 rc = sqlite3PagerWrite(pPgHdr);
5641 } 5996 }
5642 5997
5643 if( rc==SQLITE_OK ){ 5998 if( rc==SQLITE_OK ){
5644 /* Actually do the update of the change counter */ 5999 /* Actually do the update of the change counter */
5645 pager_write_changecounter(pPgHdr); 6000 pager_write_changecounter(pPgHdr);
5646 6001
5647 /* If running in direct mode, write the contents of page 1 to the file. */ 6002 /* If running in direct mode, write the contents of page 1 to the file. */
5648 if( DIRECT_MODE ){ 6003 if( DIRECT_MODE ){
5649 const void *zBuf; 6004 const void *zBuf;
5650 assert( pPager->dbFileSize>0 ); 6005 assert( pPager->dbFileSize>0 );
5651 CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf); 6006 CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
5652 if( rc==SQLITE_OK ){ 6007 if( rc==SQLITE_OK ){
5653 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); 6008 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
6009 pPager->aStat[PAGER_STAT_WRITE]++;
5654 } 6010 }
5655 if( rc==SQLITE_OK ){ 6011 if( rc==SQLITE_OK ){
6012 /* Update the pager's copy of the change-counter. Otherwise, the
6013 ** next time a read transaction is opened the cache will be
6014 ** flushed (as the change-counter values will not match). */
6015 const void *pCopy = (const void *)&((const char *)zBuf)[24];
6016 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
5656 pPager->changeCountDone = 1; 6017 pPager->changeCountDone = 1;
5657 } 6018 }
5658 }else{ 6019 }else{
5659 pPager->changeCountDone = 1; 6020 pPager->changeCountDone = 1;
5660 } 6021 }
5661 } 6022 }
5662 6023
5663 /* Release the page reference. */ 6024 /* Release the page reference. */
5664 sqlite3PagerUnref(pPgHdr); 6025 sqlite3PagerUnref(pPgHdr);
5665 } 6026 }
5666 return rc; 6027 return rc;
5667 } 6028 }
5668 6029
5669 /* 6030 /*
5670 ** Sync the database file to disk. This is a no-op for in-memory databases 6031 ** Sync the database file to disk. This is a no-op for in-memory databases
5671 ** or pages with the Pager.noSync flag set. 6032 ** or pages with the Pager.noSync flag set.
5672 ** 6033 **
5673 ** If successful, or if called on a pager for which it is a no-op, this 6034 ** If successful, or if called on a pager for which it is a no-op, this
5674 ** function returns SQLITE_OK. Otherwise, an IO error code is returned. 6035 ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
5675 */ 6036 */
5676 int sqlite3PagerSync(Pager *pPager){ 6037 int sqlite3PagerSync(Pager *pPager, const char *zMaster){
5677 int rc = SQLITE_OK; 6038 int rc = SQLITE_OK;
5678 if( !pPager->noSync ){ 6039
6040 if( isOpen(pPager->fd) ){
6041 void *pArg = (void*)zMaster;
6042 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
6043 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
6044 }
6045 if( rc==SQLITE_OK && !pPager->noSync ){
5679 assert( !MEMDB ); 6046 assert( !MEMDB );
5680 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags); 6047 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
5681 }else if( isOpen(pPager->fd) ){
5682 assert( !MEMDB );
5683 sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, (void *)&rc);
5684 } 6048 }
5685 return rc; 6049 return rc;
5686 } 6050 }
5687 6051
5688 /* 6052 /*
5689 ** This function may only be called while a write-transaction is active in 6053 ** This function may only be called while a write-transaction is active in
5690 ** rollback. If the connection is in WAL mode, this call is a no-op. 6054 ** rollback. If the connection is in WAL mode, this call is a no-op.
5691 ** Otherwise, if the connection does not already have an EXCLUSIVE lock on 6055 ** Otherwise, if the connection does not already have an EXCLUSIVE lock on
5692 ** the database file, an attempt is made to obtain one. 6056 ** the database file, an attempt is made to obtain one.
5693 ** 6057 **
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
5768 if( pagerUseWal(pPager) ){ 6132 if( pagerUseWal(pPager) ){
5769 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache); 6133 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
5770 PgHdr *pPageOne = 0; 6134 PgHdr *pPageOne = 0;
5771 if( pList==0 ){ 6135 if( pList==0 ){
5772 /* Must have at least one page for the WAL commit flag. 6136 /* Must have at least one page for the WAL commit flag.
5773 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */ 6137 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
5774 rc = sqlite3PagerGet(pPager, 1, &pPageOne); 6138 rc = sqlite3PagerGet(pPager, 1, &pPageOne);
5775 pList = pPageOne; 6139 pList = pPageOne;
5776 pList->pDirty = 0; 6140 pList->pDirty = 0;
5777 } 6141 }
5778 assert( pList!=0 || rc!=SQLITE_OK ); 6142 assert( rc==SQLITE_OK );
5779 if( pList ){ 6143 if( ALWAYS(pList) ){
5780 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1, 6144 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
5781 (pPager->fullSync ? pPager->syncFlags : 0)
5782 );
5783 } 6145 }
5784 sqlite3PagerUnref(pPageOne); 6146 sqlite3PagerUnref(pPageOne);
5785 if( rc==SQLITE_OK ){ 6147 if( rc==SQLITE_OK ){
5786 sqlite3PcacheCleanAll(pPager->pPCache); 6148 sqlite3PcacheCleanAll(pPager->pPCache);
5787 } 6149 }
5788 }else{ 6150 }else{
5789 /* The following block updates the change-counter. Exactly how it 6151 /* The following block updates the change-counter. Exactly how it
5790 ** does this depends on whether or not the atomic-update optimization 6152 ** does this depends on whether or not the atomic-update optimization
5791 ** was enabled at compile time, and if this transaction meets the 6153 ** was enabled at compile time, and if this transaction meets the
5792 ** runtime criteria to use the operation: 6154 ** runtime criteria to use the operation:
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
5831 rc = sqlite3JournalCreate(pPager->jfd); 6193 rc = sqlite3JournalCreate(pPager->jfd);
5832 if( rc==SQLITE_OK ){ 6194 if( rc==SQLITE_OK ){
5833 rc = pager_incr_changecounter(pPager, 0); 6195 rc = pager_incr_changecounter(pPager, 0);
5834 } 6196 }
5835 } 6197 }
5836 #else 6198 #else
5837 rc = pager_incr_changecounter(pPager, 0); 6199 rc = pager_incr_changecounter(pPager, 0);
5838 #endif 6200 #endif
5839 if( rc!=SQLITE_OK ) goto commit_phase_one_exit; 6201 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
5840 6202
5841 /* If this transaction has made the database smaller, then all pages
5842 ** being discarded by the truncation must be written to the journal
5843 ** file. This can only happen in auto-vacuum mode.
5844 **
5845 ** Before reading the pages with page numbers larger than the
5846 ** current value of Pager.dbSize, set dbSize back to the value
5847 ** that it took at the start of the transaction. Otherwise, the
5848 ** calls to sqlite3PagerGet() return zeroed pages instead of
5849 ** reading data from the database file.
5850 */
5851 #ifndef SQLITE_OMIT_AUTOVACUUM
5852 if( pPager->dbSize<pPager->dbOrigSize
5853 && pPager->journalMode!=PAGER_JOURNALMODE_OFF
5854 ){
5855 Pgno i; /* Iterator variable */
5856 const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
5857 const Pgno dbSize = pPager->dbSize; /* Database image size */
5858 pPager->dbSize = pPager->dbOrigSize;
5859 for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
5860 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
5861 PgHdr *pPage; /* Page to journal */
5862 rc = sqlite3PagerGet(pPager, i, &pPage);
5863 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
5864 rc = sqlite3PagerWrite(pPage);
5865 sqlite3PagerUnref(pPage);
5866 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
5867 }
5868 }
5869 pPager->dbSize = dbSize;
5870 }
5871 #endif
5872
5873 /* Write the master journal name into the journal file. If a master 6203 /* Write the master journal name into the journal file. If a master
5874 ** journal file name has already been written to the journal file, 6204 ** journal file name has already been written to the journal file,
5875 ** or if zMaster is NULL (no master journal), then this call is a no-op. 6205 ** or if zMaster is NULL (no master journal), then this call is a no-op.
5876 */ 6206 */
5877 rc = writeMasterJournal(pPager, zMaster); 6207 rc = writeMasterJournal(pPager, zMaster);
5878 if( rc!=SQLITE_OK ) goto commit_phase_one_exit; 6208 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
5879 6209
5880 /* Sync the journal file and write all dirty pages to the database. 6210 /* Sync the journal file and write all dirty pages to the database.
5881 ** If the atomic-update optimization is being used, this sync will not 6211 ** If the atomic-update optimization is being used, this sync will not
5882 ** create the journal file or perform any real IO. 6212 ** create the journal file or perform any real IO.
5883 ** 6213 **
5884 ** Because the change-counter page was just modified, unless the 6214 ** Because the change-counter page was just modified, unless the
5885 ** atomic-update optimization is used it is almost certain that the 6215 ** atomic-update optimization is used it is almost certain that the
5886 ** journal requires a sync here. However, in locking_mode=exclusive 6216 ** journal requires a sync here. However, in locking_mode=exclusive
5887 ** on a system under memory pressure it is just possible that this is 6217 ** on a system under memory pressure it is just possible that this is
5888 ** not the case. In this case it is likely enough that the redundant 6218 ** not the case. In this case it is likely enough that the redundant
5889 ** xSync() call will be changed to a no-op by the OS anyhow. 6219 ** xSync() call will be changed to a no-op by the OS anyhow.
5890 */ 6220 */
5891 rc = syncJournal(pPager, 0); 6221 rc = syncJournal(pPager, 0);
5892 if( rc!=SQLITE_OK ) goto commit_phase_one_exit; 6222 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
5893 6223
5894 rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache)); 6224 rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
5895 if( rc!=SQLITE_OK ){ 6225 if( rc!=SQLITE_OK ){
5896 assert( rc!=SQLITE_IOERR_BLOCKED ); 6226 assert( rc!=SQLITE_IOERR_BLOCKED );
5897 goto commit_phase_one_exit; 6227 goto commit_phase_one_exit;
5898 } 6228 }
5899 sqlite3PcacheCleanAll(pPager->pPCache); 6229 sqlite3PcacheCleanAll(pPager->pPCache);
5900 6230
5901 /* If the file on disk is not the same size as the database image, 6231 /* If the file on disk is smaller than the database image, use
5902 ** then use pager_truncate to grow or shrink the file here. 6232 ** pager_truncate to grow the file here. This can happen if the database
5903 */ 6233 ** image was extended as part of the current transaction and then the
5904 if( pPager->dbSize!=pPager->dbFileSize ){ 6234 ** last page in the db image moved to the free-list. In this case the
6235 ** last page is never written out to disk, leaving the database file
6236 ** undersized. Fix this now if it is the case. */
6237 if( pPager->dbSize>pPager->dbFileSize ){
5905 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager)); 6238 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
5906 assert( pPager->eState==PAGER_WRITER_DBMOD ); 6239 assert( pPager->eState==PAGER_WRITER_DBMOD );
5907 rc = pager_truncate(pPager, nNew); 6240 rc = pager_truncate(pPager, nNew);
5908 if( rc!=SQLITE_OK ) goto commit_phase_one_exit; 6241 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
5909 } 6242 }
5910 6243
5911 /* Finally, sync the database file. */ 6244 /* Finally, sync the database file. */
5912 if( !noSync ){ 6245 if( !noSync ){
5913 rc = sqlite3PagerSync(pPager); 6246 rc = sqlite3PagerSync(pPager, zMaster);
5914 } 6247 }
5915 IOTRACE(("DBSYNC %p\n", pPager)) 6248 IOTRACE(("DBSYNC %p\n", pPager))
5916 } 6249 }
5917 } 6250 }
5918 6251
5919 commit_phase_one_exit: 6252 commit_phase_one_exit:
5920 if( rc==SQLITE_OK && !pagerUseWal(pPager) ){ 6253 if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
5921 pPager->eState = PAGER_WRITER_FINISHED; 6254 pPager->eState = PAGER_WRITER_FINISHED;
5922 } 6255 }
5923 return rc; 6256 return rc;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
5967 if( pPager->eState==PAGER_WRITER_LOCKED 6300 if( pPager->eState==PAGER_WRITER_LOCKED
5968 && pPager->exclusiveMode 6301 && pPager->exclusiveMode
5969 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST 6302 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
5970 ){ 6303 ){
5971 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff ); 6304 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
5972 pPager->eState = PAGER_READER; 6305 pPager->eState = PAGER_READER;
5973 return SQLITE_OK; 6306 return SQLITE_OK;
5974 } 6307 }
5975 6308
5976 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager))); 6309 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
5977 rc = pager_end_transaction(pPager, pPager->setMaster); 6310 rc = pager_end_transaction(pPager, pPager->setMaster, 1);
5978 return pager_error(pPager, rc); 6311 return pager_error(pPager, rc);
5979 } 6312 }
5980 6313
5981 /* 6314 /*
5982 ** If a write transaction is open, then all changes made within the 6315 ** If a write transaction is open, then all changes made within the
5983 ** transaction are reverted and the current write-transaction is closed. 6316 ** transaction are reverted and the current write-transaction is closed.
5984 ** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR 6317 ** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
5985 ** state if an error occurs. 6318 ** state if an error occurs.
5986 ** 6319 **
5987 ** If the pager is already in PAGER_ERROR state when this function is called, 6320 ** If the pager is already in PAGER_ERROR state when this function is called,
(...skipping 24 matching lines...) Expand all
6012 ** the pager is already in the ERROR state, the rollback is not 6345 ** the pager is already in the ERROR state, the rollback is not
6013 ** attempted here. Instead, the error code is returned to the caller. 6346 ** attempted here. Instead, the error code is returned to the caller.
6014 */ 6347 */
6015 assert( assert_pager_state(pPager) ); 6348 assert( assert_pager_state(pPager) );
6016 if( pPager->eState==PAGER_ERROR ) return pPager->errCode; 6349 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
6017 if( pPager->eState<=PAGER_READER ) return SQLITE_OK; 6350 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
6018 6351
6019 if( pagerUseWal(pPager) ){ 6352 if( pagerUseWal(pPager) ){
6020 int rc2; 6353 int rc2;
6021 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1); 6354 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
6022 rc2 = pager_end_transaction(pPager, pPager->setMaster); 6355 rc2 = pager_end_transaction(pPager, pPager->setMaster, 0);
6023 if( rc==SQLITE_OK ) rc = rc2; 6356 if( rc==SQLITE_OK ) rc = rc2;
6024 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){ 6357 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
6025 int eState = pPager->eState; 6358 int eState = pPager->eState;
6026 rc = pager_end_transaction(pPager, 0); 6359 rc = pager_end_transaction(pPager, 0, 0);
6027 if( !MEMDB && eState>PAGER_WRITER_LOCKED ){ 6360 if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
6028 /* This can happen using journal_mode=off. Move the pager to the error 6361 /* This can happen using journal_mode=off. Move the pager to the error
6029 ** state to indicate that the contents of the cache may not be trusted. 6362 ** state to indicate that the contents of the cache may not be trusted.
6030 ** Any active readers will get SQLITE_ABORT. 6363 ** Any active readers will get SQLITE_ABORT.
6031 */ 6364 */
6032 pPager->errCode = SQLITE_ABORT; 6365 pPager->errCode = SQLITE_ABORT;
6033 pPager->eState = PAGER_ERROR; 6366 pPager->eState = PAGER_ERROR;
6034 return rc; 6367 return rc;
6035 } 6368 }
6036 }else{ 6369 }else{
6037 rc = pager_playback(pPager, 0); 6370 rc = pager_playback(pPager, 0);
6038 } 6371 }
6039 6372
6040 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK ); 6373 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
6041 assert( rc==SQLITE_OK || rc==SQLITE_FULL || (rc&0xFF)==SQLITE_IOERR ); 6374 assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
6375 || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR
6376 || rc==SQLITE_CANTOPEN
6377 );
6042 6378
6043 /* If an error occurs during a ROLLBACK, we can no longer trust the pager 6379 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
6044 ** cache. So call pager_error() on the way out to make any error persistent. 6380 ** cache. So call pager_error() on the way out to make any error persistent.
6045 */ 6381 */
6046 return pager_error(pPager, rc); 6382 return pager_error(pPager, rc);
6047 } 6383 }
6048 6384
6049 /* 6385 /*
6050 ** Return TRUE if the database file is opened read-only. Return FALSE 6386 ** Return TRUE if the database file is opened read-only. Return FALSE
6051 ** if the database is (in theory) writable. 6387 ** if the database is (in theory) writable.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
6085 ** This routine is used for testing and analysis only. 6421 ** This routine is used for testing and analysis only.
6086 */ 6422 */
6087 int *sqlite3PagerStats(Pager *pPager){ 6423 int *sqlite3PagerStats(Pager *pPager){
6088 static int a[11]; 6424 static int a[11];
6089 a[0] = sqlite3PcacheRefCount(pPager->pPCache); 6425 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
6090 a[1] = sqlite3PcachePagecount(pPager->pPCache); 6426 a[1] = sqlite3PcachePagecount(pPager->pPCache);
6091 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache); 6427 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
6092 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize; 6428 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
6093 a[4] = pPager->eState; 6429 a[4] = pPager->eState;
6094 a[5] = pPager->errCode; 6430 a[5] = pPager->errCode;
6095 a[6] = pPager->nHit; 6431 a[6] = pPager->aStat[PAGER_STAT_HIT];
6096 a[7] = pPager->nMiss; 6432 a[7] = pPager->aStat[PAGER_STAT_MISS];
6097 a[8] = 0; /* Used to be pPager->nOvfl */ 6433 a[8] = 0; /* Used to be pPager->nOvfl */
6098 a[9] = pPager->nRead; 6434 a[9] = pPager->nRead;
6099 a[10] = pPager->nWrite; 6435 a[10] = pPager->aStat[PAGER_STAT_WRITE];
6100 return a; 6436 return a;
6101 } 6437 }
6102 #endif 6438 #endif
6103 6439
6104 /* 6440 /*
6441 ** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
6442 ** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
6443 ** current cache hit or miss count, according to the value of eStat. If the
6444 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
6445 ** returning.
6446 */
6447 void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
6448
6449 assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
6450 || eStat==SQLITE_DBSTATUS_CACHE_MISS
6451 || eStat==SQLITE_DBSTATUS_CACHE_WRITE
6452 );
6453
6454 assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
6455 assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
6456 assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
6457
6458 *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
6459 if( reset ){
6460 pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
6461 }
6462 }
6463
6464 /*
6105 ** Return true if this is an in-memory pager. 6465 ** Return true if this is an in-memory pager.
6106 */ 6466 */
6107 int sqlite3PagerIsMemdb(Pager *pPager){ 6467 int sqlite3PagerIsMemdb(Pager *pPager){
6108 return MEMDB; 6468 return MEMDB;
6109 } 6469 }
6110 6470
6111 /* 6471 /*
6112 ** Check that there are at least nSavepoint savepoints open. If there are 6472 ** Check that there are at least nSavepoint savepoints open. If there are
6113 ** currently less than nSavepoints open, then open one or more savepoints 6473 ** currently less than nSavepoints open, then open one or more savepoints
6114 ** to make up the difference. If the number of savepoints is already 6474 ** to make up the difference. If the number of savepoints is already
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
6239 rc = pagerPlaybackSavepoint(pPager, pSavepoint); 6599 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
6240 assert(rc!=SQLITE_DONE); 6600 assert(rc!=SQLITE_DONE);
6241 } 6601 }
6242 } 6602 }
6243 6603
6244 return rc; 6604 return rc;
6245 } 6605 }
6246 6606
6247 /* 6607 /*
6248 ** Return the full pathname of the database file. 6608 ** Return the full pathname of the database file.
6609 **
6610 ** Except, if the pager is in-memory only, then return an empty string if
6611 ** nullIfMemDb is true. This routine is called with nullIfMemDb==1 when
6612 ** used to report the filename to the user, for compatibility with legacy
6613 ** behavior. But when the Btree needs to know the filename for matching to
6614 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
6615 ** participate in shared-cache.
6249 */ 6616 */
6250 const char *sqlite3PagerFilename(Pager *pPager){ 6617 const char *sqlite3PagerFilename(Pager *pPager, int nullIfMemDb){
6251 return pPager->zFilename; 6618 return (nullIfMemDb && pPager->memDb) ? "" : pPager->zFilename;
6252 } 6619 }
6253 6620
6254 /* 6621 /*
6255 ** Return the VFS structure for the pager. 6622 ** Return the VFS structure for the pager.
6256 */ 6623 */
6257 const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){ 6624 const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
6258 return pPager->pVfs; 6625 return pPager->pVfs;
6259 } 6626 }
6260 6627
6261 /* 6628 /*
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
6296 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec); 6663 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
6297 pPager->xCodec = pPager->memDb ? 0 : xCodec; 6664 pPager->xCodec = pPager->memDb ? 0 : xCodec;
6298 pPager->xCodecSizeChng = xCodecSizeChng; 6665 pPager->xCodecSizeChng = xCodecSizeChng;
6299 pPager->xCodecFree = xCodecFree; 6666 pPager->xCodecFree = xCodecFree;
6300 pPager->pCodec = pCodec; 6667 pPager->pCodec = pCodec;
6301 pagerReportSize(pPager); 6668 pagerReportSize(pPager);
6302 } 6669 }
6303 void *sqlite3PagerGetCodec(Pager *pPager){ 6670 void *sqlite3PagerGetCodec(Pager *pPager){
6304 return pPager->pCodec; 6671 return pPager->pCodec;
6305 } 6672 }
6306 #endif 6673
6674 /*
6675 ** This function is called by the wal module when writing page content
6676 ** into the log file.
6677 **
6678 ** This function returns a pointer to a buffer containing the encrypted
6679 ** page content. If a malloc fails, this function may return NULL.
6680 */
6681 void *sqlite3PagerCodec(PgHdr *pPg){
6682 void *aData = 0;
6683 CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
6684 return aData;
6685 }
6686
6687 /*
6688 ** Return the current pager state
6689 */
6690 int sqlite3PagerState(Pager *pPager){
6691 return pPager->eState;
6692 }
6693 #endif /* SQLITE_HAS_CODEC */
6307 6694
6308 #ifndef SQLITE_OMIT_AUTOVACUUM 6695 #ifndef SQLITE_OMIT_AUTOVACUUM
6309 /* 6696 /*
6310 ** Move the page pPg to location pgno in the file. 6697 ** Move the page pPg to location pgno in the file.
6311 ** 6698 **
6312 ** There must be no references to the page previously located at 6699 ** There must be no references to the page previously located at
6313 ** pgno (which we call pPgOld) though that page is allowed to be 6700 ** pgno (which we call pPgOld) though that page is allowed to be
6314 ** in cache. If the page previously located at pgno is not already 6701 ** in cache. If the page previously located at pgno is not already
6315 ** in the rollback journal, it is not put there by by this routine. 6702 ** in the rollback journal, it is not put there by by this routine.
6316 ** 6703 **
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
6382 6769
6383 /* If the journal needs to be sync()ed before page pPg->pgno can 6770 /* If the journal needs to be sync()ed before page pPg->pgno can
6384 ** be written to, store pPg->pgno in local variable needSyncPgno. 6771 ** be written to, store pPg->pgno in local variable needSyncPgno.
6385 ** 6772 **
6386 ** If the isCommit flag is set, there is no need to remember that 6773 ** If the isCommit flag is set, there is no need to remember that
6387 ** the journal needs to be sync()ed before database page pPg->pgno 6774 ** the journal needs to be sync()ed before database page pPg->pgno
6388 ** can be written to. The caller has already promised not to write to it. 6775 ** can be written to. The caller has already promised not to write to it.
6389 */ 6776 */
6390 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){ 6777 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
6391 needSyncPgno = pPg->pgno; 6778 needSyncPgno = pPg->pgno;
6392 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize ); 6779 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
6780 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
6393 assert( pPg->flags&PGHDR_DIRTY ); 6781 assert( pPg->flags&PGHDR_DIRTY );
6394 } 6782 }
6395 6783
6396 /* If the cache contains a page with page-number pgno, remove it 6784 /* If the cache contains a page with page-number pgno, remove it
6397 ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for 6785 ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
6398 ** page pgno before the 'move' operation, it needs to be retained 6786 ** page pgno before the 'move' operation, it needs to be retained
6399 ** for the page moved there. 6787 ** for the page moved there.
6400 */ 6788 */
6401 pPg->flags &= ~PGHDR_NEED_SYNC; 6789 pPg->flags &= ~PGHDR_NEED_SYNC;
6402 pPgOld = pager_lookup(pPager, pgno); 6790 pPgOld = sqlite3PagerLookup(pPager, pgno);
6403 assert( !pPgOld || pPgOld->nRef==1 ); 6791 assert( !pPgOld || pPgOld->nRef==1 );
6404 if( pPgOld ){ 6792 if( pPgOld ){
6405 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC); 6793 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
6406 if( MEMDB ){ 6794 if( MEMDB ){
6407 /* Do not discard pages from an in-memory database since we might 6795 /* Do not discard pages from an in-memory database since we might
6408 ** need to rollback later. Just move the page out of the way. */ 6796 ** need to rollback later. Just move the page out of the way. */
6409 sqlite3PcacheMove(pPgOld, pPager->dbSize+1); 6797 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
6410 }else{ 6798 }else{
6411 sqlite3PcacheDrop(pPgOld); 6799 sqlite3PcacheDrop(pPgOld);
6412 } 6800 }
6413 } 6801 }
6414 6802
6415 origPgno = pPg->pgno; 6803 origPgno = pPg->pgno;
6416 sqlite3PcacheMove(pPg, pgno); 6804 sqlite3PcacheMove(pPg, pgno);
6417 sqlite3PcacheMakeDirty(pPg); 6805 sqlite3PcacheMakeDirty(pPg);
6418 6806
6419 /* For an in-memory database, make sure the original page continues 6807 /* For an in-memory database, make sure the original page continues
6420 ** to exist, in case the transaction needs to roll back. Use pPgOld 6808 ** to exist, in case the transaction needs to roll back. Use pPgOld
6421 ** as the original page since it has already been allocated. 6809 ** as the original page since it has already been allocated.
6422 */ 6810 */
6423 if( MEMDB ){ 6811 if( MEMDB ){
6424 assert( pPgOld ); 6812 assert( pPgOld );
6425 sqlite3PcacheMove(pPgOld, origPgno); 6813 sqlite3PcacheMove(pPgOld, origPgno);
6426 sqlite3PagerUnref(pPgOld); 6814 sqlite3PagerUnrefNotNull(pPgOld);
6427 } 6815 }
6428 6816
6429 if( needSyncPgno ){ 6817 if( needSyncPgno ){
6430 /* If needSyncPgno is non-zero, then the journal file needs to be 6818 /* If needSyncPgno is non-zero, then the journal file needs to be
6431 ** sync()ed before any data is written to database file page needSyncPgno. 6819 ** sync()ed before any data is written to database file page needSyncPgno.
6432 ** Currently, no such page exists in the page-cache and the 6820 ** Currently, no such page exists in the page-cache and the
6433 ** "is journaled" bitvec flag has been set. This needs to be remedied by 6821 ** "is journaled" bitvec flag has been set. This needs to be remedied by
6434 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC 6822 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
6435 ** flag. 6823 ** flag.
6436 ** 6824 **
6437 ** If the attempt to load the page into the page-cache fails, (due 6825 ** If the attempt to load the page into the page-cache fails, (due
6438 ** to a malloc() or IO failure), clear the bit in the pInJournal[] 6826 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
6439 ** array. Otherwise, if the page is loaded and written again in 6827 ** array. Otherwise, if the page is loaded and written again in
6440 ** this transaction, it may be written to the database file before 6828 ** this transaction, it may be written to the database file before
6441 ** it is synced into the journal file. This way, it may end up in 6829 ** it is synced into the journal file. This way, it may end up in
6442 ** the journal file twice, but that is not a problem. 6830 ** the journal file twice, but that is not a problem.
6443 */ 6831 */
6444 PgHdr *pPgHdr; 6832 PgHdr *pPgHdr;
6445 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr); 6833 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
6446 if( rc!=SQLITE_OK ){ 6834 if( rc!=SQLITE_OK ){
6447 if( needSyncPgno<=pPager->dbOrigSize ){ 6835 if( needSyncPgno<=pPager->dbOrigSize ){
6448 assert( pPager->pTmpSpace!=0 ); 6836 assert( pPager->pTmpSpace!=0 );
6449 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace); 6837 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
6450 } 6838 }
6451 return rc; 6839 return rc;
6452 } 6840 }
6453 pPgHdr->flags |= PGHDR_NEED_SYNC; 6841 pPgHdr->flags |= PGHDR_NEED_SYNC;
6454 sqlite3PcacheMakeDirty(pPgHdr); 6842 sqlite3PcacheMakeDirty(pPgHdr);
6455 sqlite3PagerUnref(pPgHdr); 6843 sqlite3PagerUnrefNotNull(pPgHdr);
6456 } 6844 }
6457 6845
6458 return SQLITE_OK; 6846 return SQLITE_OK;
6459 } 6847 }
6460 #endif 6848 #endif
6461 6849
6462 /* 6850 /*
6463 ** Return a pointer to the data for the specified page. 6851 ** Return a pointer to the data for the specified page.
6464 */ 6852 */
6465 void *sqlite3PagerGetData(DbPage *pPg){ 6853 void *sqlite3PagerGetData(DbPage *pPg){
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
6632 7020
6633 /* 7021 /*
6634 ** Get/set the size-limit used for persistent journal files. 7022 ** Get/set the size-limit used for persistent journal files.
6635 ** 7023 **
6636 ** Setting the size limit to -1 means no limit is enforced. 7024 ** Setting the size limit to -1 means no limit is enforced.
6637 ** An attempt to set a limit smaller than -1 is a no-op. 7025 ** An attempt to set a limit smaller than -1 is a no-op.
6638 */ 7026 */
6639 i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){ 7027 i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
6640 if( iLimit>=-1 ){ 7028 if( iLimit>=-1 ){
6641 pPager->journalSizeLimit = iLimit; 7029 pPager->journalSizeLimit = iLimit;
7030 sqlite3WalLimit(pPager->pWal, iLimit);
6642 } 7031 }
6643 return pPager->journalSizeLimit; 7032 return pPager->journalSizeLimit;
6644 } 7033 }
6645 7034
6646 /* 7035 /*
6647 ** Return a pointer to the pPager->pBackup variable. The backup module 7036 ** Return a pointer to the pPager->pBackup variable. The backup module
6648 ** in backup.c maintains the content of this variable. This module 7037 ** in backup.c maintains the content of this variable. This module
6649 ** uses it opaquely as an argument to sqlite3BackupRestart() and 7038 ** uses it opaquely as an argument to sqlite3BackupRestart() and
6650 ** sqlite3BackupUpdate() only. 7039 ** sqlite3BackupUpdate() only.
6651 */ 7040 */
6652 sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){ 7041 sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
6653 return &pPager->pBackup; 7042 return &pPager->pBackup;
6654 } 7043 }
6655 7044
7045 #ifndef SQLITE_OMIT_VACUUM
7046 /*
7047 ** Unless this is an in-memory or temporary database, clear the pager cache.
7048 */
7049 void sqlite3PagerClearCache(Pager *pPager){
7050 if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
7051 }
7052 #endif
7053
6656 #ifndef SQLITE_OMIT_WAL 7054 #ifndef SQLITE_OMIT_WAL
6657 /* 7055 /*
6658 ** This function is called when the user invokes "PRAGMA wal_checkpoint", 7056 ** This function is called when the user invokes "PRAGMA wal_checkpoint",
6659 ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint() 7057 ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
6660 ** or wal_blocking_checkpoint() API functions. 7058 ** or wal_blocking_checkpoint() API functions.
6661 ** 7059 **
6662 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. 7060 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
6663 */ 7061 */
6664 int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){ 7062 int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
6665 int rc = SQLITE_OK; 7063 int rc = SQLITE_OK;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
6707 /* 7105 /*
6708 ** Call sqlite3WalOpen() to open the WAL handle. If the pager is in 7106 ** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
6709 ** exclusive-locking mode when this function is called, take an EXCLUSIVE 7107 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
6710 ** lock on the database file and use heap-memory to store the wal-index 7108 ** lock on the database file and use heap-memory to store the wal-index
6711 ** in. Otherwise, use the normal shared-memory. 7109 ** in. Otherwise, use the normal shared-memory.
6712 */ 7110 */
6713 static int pagerOpenWal(Pager *pPager){ 7111 static int pagerOpenWal(Pager *pPager){
6714 int rc = SQLITE_OK; 7112 int rc = SQLITE_OK;
6715 7113
6716 assert( pPager->pWal==0 && pPager->tempFile==0 ); 7114 assert( pPager->pWal==0 && pPager->tempFile==0 );
6717 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK || pPager- >noReadlock); 7115 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
6718 7116
6719 /* If the pager is already in exclusive-mode, the WAL module will use 7117 /* If the pager is already in exclusive-mode, the WAL module will use
6720 ** heap-memory for the wal-index instead of the VFS shared-memory 7118 ** heap-memory for the wal-index instead of the VFS shared-memory
6721 ** implementation. Take the exclusive lock now, before opening the WAL 7119 ** implementation. Take the exclusive lock now, before opening the WAL
6722 ** file, to make sure this is safe. 7120 ** file, to make sure this is safe.
6723 */ 7121 */
6724 if( pPager->exclusiveMode ){ 7122 if( pPager->exclusiveMode ){
6725 rc = pagerExclusiveLock(pPager); 7123 rc = pagerExclusiveLock(pPager);
6726 } 7124 }
6727 7125
6728 /* Open the connection to the log file. If this operation fails, 7126 /* Open the connection to the log file. If this operation fails,
6729 ** (e.g. due to malloc() failure), return an error code. 7127 ** (e.g. due to malloc() failure), return an error code.
6730 */ 7128 */
6731 if( rc==SQLITE_OK ){ 7129 if( rc==SQLITE_OK ){
6732 rc = sqlite3WalOpen(pPager->pVfs, 7130 rc = sqlite3WalOpen(pPager->pVfs,
6733 pPager->fd, pPager->zWal, pPager->exclusiveMode, &pPager->pWal 7131 pPager->fd, pPager->zWal, pPager->exclusiveMode,
7132 pPager->journalSizeLimit, &pPager->pWal
6734 ); 7133 );
6735 } 7134 }
7135 pagerFixMaplimit(pPager);
6736 7136
6737 return rc; 7137 return rc;
6738 } 7138 }
6739 7139
6740 7140
6741 /* 7141 /*
6742 ** The caller must be holding a SHARED lock on the database file to call 7142 ** The caller must be holding a SHARED lock on the database file to call
6743 ** this function. 7143 ** this function.
6744 ** 7144 **
6745 ** If the pager passed as the first argument is open on a real database 7145 ** If the pager passed as the first argument is open on a real database
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
6816 7216
6817 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on 7217 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
6818 ** the database file, the log and log-summary files will be deleted. 7218 ** the database file, the log and log-summary files will be deleted.
6819 */ 7219 */
6820 if( rc==SQLITE_OK && pPager->pWal ){ 7220 if( rc==SQLITE_OK && pPager->pWal ){
6821 rc = pagerExclusiveLock(pPager); 7221 rc = pagerExclusiveLock(pPager);
6822 if( rc==SQLITE_OK ){ 7222 if( rc==SQLITE_OK ){
6823 rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, 7223 rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
6824 pPager->pageSize, (u8*)pPager->pTmpSpace); 7224 pPager->pageSize, (u8*)pPager->pTmpSpace);
6825 pPager->pWal = 0; 7225 pPager->pWal = 0;
7226 pagerFixMaplimit(pPager);
6826 } 7227 }
6827 } 7228 }
6828 return rc; 7229 return rc;
6829 } 7230 }
6830 7231
6831 #ifdef SQLITE_HAS_CODEC
6832 /*
6833 ** This function is called by the wal module when writing page content
6834 ** into the log file.
6835 **
6836 ** This function returns a pointer to a buffer containing the encrypted
6837 ** page content. If a malloc fails, this function may return NULL.
6838 */
6839 void *sqlite3PagerCodec(PgHdr *pPg){
6840 void *aData = 0;
6841 CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
6842 return aData;
6843 }
6844 #endif /* SQLITE_HAS_CODEC */
6845
6846 #endif /* !SQLITE_OMIT_WAL */ 7232 #endif /* !SQLITE_OMIT_WAL */
6847 7233
7234 #ifdef SQLITE_ENABLE_ZIPVFS
7235 /*
7236 ** A read-lock must be held on the pager when this function is called. If
7237 ** the pager is in WAL mode and the WAL file currently contains one or more
7238 ** frames, return the size in bytes of the page images stored within the
7239 ** WAL frames. Otherwise, if this is not a WAL database or the WAL file
7240 ** is empty, return 0.
7241 */
7242 int sqlite3PagerWalFramesize(Pager *pPager){
7243 assert( pPager->eState>=PAGER_READER );
7244 return sqlite3WalFramesize(pPager->pWal);
7245 }
7246 #endif
7247
6848 #endif /* SQLITE_OMIT_DISKIO */ 7248 #endif /* SQLITE_OMIT_DISKIO */
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3080704/src/pager.h ('k') | third_party/sqlite/sqlite-src-3080704/src/parse.y » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698