0000: 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
0010: 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
0020: 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
0030: 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
0040: 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
0050: 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
0060: 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
0070: 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
0080: 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
0090: 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
00a0: 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
00b0: 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
00c0: 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
00d0: 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
00e0: 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
00f0: 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
0100: 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
0110: 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
0120: 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0170: 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 ******.** This i
0180: 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 s the implementa
0190: 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 65 tion of the page
01a0: 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d cache subsystem
01b0: 20 6f 72 20 22 70 61 67 65 72 22 2e 0a 2a 2a 20 or "pager"..**
01c0: 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 69 73 .** The pager is
01d0: 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 used to access
01e0: 61 20 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 a database disk
01f0: 66 69 6c 65 2e 20 20 49 74 20 69 6d 70 6c 65 6d file. It implem
0200: 65 6e 74 73 0a 2a 2a 20 61 74 6f 6d 69 63 20 63 ents.** atomic c
0210: 6f 6d 6d 69 74 20 61 6e 64 20 72 6f 6c 6c 62 61 ommit and rollba
0220: 63 6b 20 74 68 72 6f 75 67 68 20 74 68 65 20 75 ck through the u
0230: 73 65 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 se of a journal
0240: 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 file that.** is
0250: 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68 separate from th
0260: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
0270: 20 20 54 68 65 20 70 61 67 65 72 20 61 6c 73 6f The pager also
0280: 20 69 6d 70 6c 65 6d 65 6e 74 73 20 66 69 6c 65 implements file
0290: 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 70 .** locking to p
02a0: 72 65 76 65 6e 74 20 74 77 6f 20 70 72 6f 63 65 revent two proce
02b0: 73 73 65 73 20 66 72 6f 6d 20 77 72 69 74 69 6e sses from writin
02c0: 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 g the same datab
02d0: 61 73 65 0a 2a 2a 20 66 69 6c 65 20 73 69 6d 75 ase.** file simu
02e0: 6c 74 61 6e 65 6f 75 73 6c 79 2c 20 6f 72 20 6f ltaneously, or o
02f0: 6e 65 20 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 ne process from
0300: 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 reading the data
0310: 62 61 73 65 20 77 68 69 6c 65 0a 2a 2a 20 61 6e base while.** an
0320: 6f 74 68 65 72 20 69 73 20 77 72 69 74 69 6e 67 other is writing
0330: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
0340: 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a ITE_OMIT_DISKIO.
0350: 23 69 6e 63 6c 75 64 65 20 22 73 71 6c 69 74 65 #include "sqlite
0360: 49 6e 74 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 Int.h".#include
0370: 22 77 61 6c 2e 68 22 0a 0a 2f 2a 0a 2a 2a 2a 2a "wal.h"../*.****
0380: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0390: 20 4e 4f 54 45 53 20 4f 4e 20 54 48 45 20 44 45 NOTES ON THE DE
03a0: 53 49 47 4e 20 4f 46 20 54 48 45 20 50 41 47 45 SIGN OF THE PAGE
03b0: 52 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a R **************
03c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
03d0: 20 57 69 74 68 69 6e 20 74 68 69 73 20 63 6f 6d Within this com
03e0: 6d 65 6e 74 20 62 6c 6f 63 6b 2c 20 61 20 70 61 ment block, a pa
03f0: 67 65 20 69 73 20 64 65 65 6d 65 64 20 74 6f 20 ge is deemed to
0400: 68 61 76 65 20 62 65 65 6e 20 73 79 6e 63 65 64 have been synced
0410: 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c .** automaticall
0420: 79 20 61 73 20 73 6f 6f 6e 20 61 73 20 69 74 20 y as soon as it
0430: 69 73 20 77 72 69 74 74 65 6e 20 77 68 65 6e 20 is written when
0440: 50 52 41 47 4d 41 20 73 79 6e 63 68 72 6f 6e 6f PRAGMA synchrono
0450: 75 73 3d 4f 46 46 2e 0a 2a 2a 20 4f 74 68 65 72 us=OFF..** Other
0460: 77 69 73 65 2c 20 74 68 65 20 70 61 67 65 20 69 wise, the page i
0470: 73 20 6e 6f 74 20 73 79 6e 63 65 64 20 75 6e 74 s not synced unt
0480: 69 6c 20 74 68 65 20 78 53 79 6e 63 20 6d 65 74 il the xSync met
0490: 68 6f 64 20 6f 66 20 74 68 65 20 56 46 53 0a 2a hod of the VFS.*
04a0: 2a 20 69 73 20 63 61 6c 6c 65 64 20 73 75 63 63 * is called succ
04b0: 65 73 73 66 75 6c 6c 79 20 6f 6e 20 74 68 65 20 essfully on the
04c0: 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 file containing
04d0: 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 the page..**.**
04e0: 44 65 66 69 6e 69 74 69 6f 6e 3a 20 20 41 20 70 Definition: A p
04f0: 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 age of the datab
0500: 61 73 65 20 66 69 6c 65 20 69 73 20 73 61 69 64 ase file is said
0510: 20 74 6f 20 62 65 20 22 6f 76 65 72 77 72 69 74 to be "overwrit
0520: 65 61 62 6c 65 22 20 69 66 0a 2a 2a 20 6f 6e 65 eable" if.** one
0530: 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 or more of the
0540: 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 following are tr
0550: 75 65 20 61 62 6f 75 74 20 74 68 65 20 70 61 67 ue about the pag
0560: 65 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 28 61 e:.** .** (a
0570: 29 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 ) The original
0580: 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 content of the p
0590: 61 67 65 20 61 73 20 69 74 20 77 61 73 20 61 74 age as it was at
05a0: 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
05b0: 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 f.** th
05c0: 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 e transaction ha
05d0: 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 69 s been written i
05e0: 6e 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b nto the rollback
05f0: 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 journal and.**
0600: 20 20 20 20 20 20 20 20 20 73 79 6e 63 65 64 2e synced.
0610: 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 28 62 29 20 .** .** (b)
0620: 20 54 68 65 20 70 61 67 65 20 77 61 73 20 61 20 The page was a
0630: 66 72 65 65 6c 69 73 74 20 6c 65 61 66 20 70 61 freelist leaf pa
0640: 67 65 20 61 74 20 74 68 65 20 73 74 61 72 74 20 ge at the start
0650: 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 of the transacti
0660: 6f 6e 2e 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 28 on..** .** (
0670: 63 29 20 20 54 68 65 20 70 61 67 65 20 6e 75 6d c) The page num
0680: 62 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74 ber is greater t
0690: 68 61 6e 20 74 68 65 20 6c 61 72 67 65 73 74 20 han the largest
06a0: 70 61 67 65 20 74 68 61 74 20 65 78 69 73 74 65 page that existe
06b0: 64 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 d in.**
06c0: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
06d0: 6c 65 20 61 74 20 74 68 65 20 73 74 61 72 74 20 le at the start
06e0: 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 of the transacti
06f0: 6f 6e 2e 0a 2a 2a 20 0a 2a 2a 20 28 31 29 20 41 on..** .** (1) A
0700: 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 page of the dat
0710: 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6e 65 abase file is ne
0720: 76 65 72 20 6f 76 65 72 77 72 69 74 74 65 6e 20 ver overwritten
0730: 75 6e 6c 65 73 73 20 6f 6e 65 20 6f 66 20 74 68 unless one of th
0740: 65 0a 2a 2a 20 20 20 20 20 66 6f 6c 6c 6f 77 69 e.** followi
0750: 6e 67 20 61 72 65 20 74 72 75 65 3a 0a 2a 2a 20 ng are true:.**
0760: 0a 2a 2a 20 20 20 20 20 28 61 29 20 54 68 65 20 .** (a) The
0770: 70 61 67 65 20 61 6e 64 20 61 6c 6c 20 6f 74 68 page and all oth
0780: 65 72 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 er pages on the
0790: 73 61 6d 65 20 73 65 63 74 6f 72 20 61 72 65 20 same sector are
07a0: 6f 76 65 72 77 72 69 74 65 61 62 6c 65 2e 0a 2a overwriteable..*
07b0: 2a 20 0a 2a 2a 20 20 20 20 20 28 62 29 20 54 68 * .** (b) Th
07c0: 65 20 61 74 6f 6d 69 63 20 70 61 67 65 20 77 72 e atomic page wr
07d0: 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ite optimization
07e0: 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 61 6e 64 is enabled, and
07f0: 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20 20 the entire.**
0800: 20 20 20 20 20 20 20 74 72 61 6e 73 61 63 74 69 transacti
0810: 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 on other than th
0820: 65 20 75 70 64 61 74 65 20 6f 66 20 74 68 65 20 e update of the
0830: 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 65 71 75 transaction sequ
0840: 65 6e 63 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 ence.**
0850: 6e 75 6d 62 65 72 20 63 6f 6e 73 69 73 74 73 20 number consists
0860: 6f 66 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 of a single page
0870: 20 63 68 61 6e 67 65 2e 0a 2a 2a 20 0a 2a 2a 20 change..** .**
0880: 28 32 29 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 (2) The content
0890: 6f 66 20 61 20 70 61 67 65 20 77 72 69 74 74 65 of a page writte
08a0: 6e 20 69 6e 74 6f 20 74 68 65 20 72 6f 6c 6c 62 n into the rollb
08b0: 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 65 78 61 63 ack journal exac
08c0: 74 6c 79 20 6d 61 74 63 68 65 73 0a 2a 2a 20 20 tly matches.**
08d0: 20 20 20 62 6f 74 68 20 74 68 65 20 63 6f 6e 74 both the cont
08e0: 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 ent in the datab
08f0: 61 73 65 20 77 68 65 6e 20 74 68 65 20 72 6f 6c ase when the rol
0900: 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 77 61 lback journal wa
0910: 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 20 20 20 s written.**
0920: 20 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 and the content
0930: 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
0940: 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e at the beginnin
0950: 67 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 g of the current
0960: 0a 2a 2a 20 20 20 20 20 74 72 61 6e 73 61 63 74 .** transact
0970: 69 6f 6e 2e 0a 2a 2a 20 0a 2a 2a 20 28 33 29 20 ion..** .** (3)
0980: 57 72 69 74 65 73 20 74 6f 20 74 68 65 20 64 61 Writes to the da
0990: 74 61 62 61 73 65 20 66 69 6c 65 20 61 72 65 20 tabase file are
09a0: 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 an integer multi
09b0: 70 6c 65 20 6f 66 20 74 68 65 20 70 61 67 65 20 ple of the page
09c0: 73 69 7a 65 0a 2a 2a 20 20 20 20 20 69 6e 20 6c size.** in l
09d0: 65 6e 67 74 68 20 61 6e 64 20 61 72 65 20 61 6c ength and are al
09e0: 69 67 6e 65 64 20 74 6f 20 61 20 70 61 67 65 20 igned to a page
09f0: 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 20 0a 2a 2a boundary..** .**
0a00: 20 28 34 29 20 52 65 61 64 73 20 66 72 6f 6d 20 (4) Reads from
0a10: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
0a20: 65 20 61 72 65 20 65 69 74 68 65 72 20 61 6c 69 e are either ali
0a30: 67 6e 65 64 20 6f 6e 20 61 20 70 61 67 65 20 62 gned on a page b
0a40: 6f 75 6e 64 61 72 79 20 61 6e 64 0a 2a 2a 20 20 oundary and.**
0a50: 20 20 20 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 an integer mu
0a60: 6c 74 69 70 6c 65 20 6f 66 20 74 68 65 20 70 61 ltiple of the pa
0a70: 67 65 20 73 69 7a 65 20 69 6e 20 6c 65 6e 67 74 ge size in lengt
0a80: 68 20 6f 72 20 61 72 65 20 74 61 6b 65 6e 20 66 h or are taken f
0a90: 72 6f 6d 20 74 68 65 0a 2a 2a 20 20 20 20 20 66 rom the.** f
0aa0: 69 72 73 74 20 31 30 30 20 62 79 74 65 73 20 6f irst 100 bytes o
0ab0: 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
0ac0: 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 28 35 29 20 ile..** .** (5)
0ad0: 41 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 74 68 All writes to th
0ae0: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
0af0: 61 72 65 20 73 79 6e 63 65 64 20 70 72 69 6f 72 are synced prior
0b00: 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b to the rollback
0b10: 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 journal.**
0b20: 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2c 20 74 being deleted, t
0b30: 72 75 6e 63 61 74 65 64 2c 20 6f 72 20 7a 65 72 runcated, or zer
0b40: 6f 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 28 36 29 20 oed..** .** (6)
0b50: 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 If a master jour
0b60: 6e 61 6c 20 66 69 6c 65 20 69 73 20 75 73 65 64 nal file is used
0b70: 2c 20 74 68 65 6e 20 61 6c 6c 20 77 72 69 74 65 , then all write
0b80: 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 s to the databas
0b90: 65 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 61 72 e file.** ar
0ba0: 65 20 73 79 6e 63 65 64 20 70 72 69 6f 72 20 74 e synced prior t
0bb0: 6f 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 o the master jou
0bc0: 72 6e 61 6c 20 62 65 69 6e 67 20 64 65 6c 65 74 rnal being delet
0bd0: 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 44 65 66 69 6e ed..** .** Defin
0be0: 69 74 69 6f 6e 3a 20 54 77 6f 20 64 61 74 61 62 ition: Two datab
0bf0: 61 73 65 73 20 28 6f 72 20 74 68 65 20 73 61 6d ases (or the sam
0c00: 65 20 64 61 74 61 62 61 73 65 20 61 74 20 74 77 e database at tw
0c10: 6f 20 70 6f 69 6e 74 73 20 69 74 20 74 69 6d 65 o points it time
0c20: 29 0a 2a 2a 20 61 72 65 20 73 61 69 64 20 74 6f ).** are said to
0c30: 20 62 65 20 22 6c 6f 67 69 63 61 6c 6c 79 20 65 be "logically e
0c40: 71 75 69 76 61 6c 65 6e 74 22 20 69 66 20 74 68 quivalent" if th
0c50: 65 79 20 67 69 76 65 20 74 68 65 20 73 61 6d 65 ey give the same
0c60: 20 61 6e 73 77 65 72 20 74 6f 0a 2a 2a 20 61 6c answer to.** al
0c70: 6c 20 71 75 65 72 69 65 73 2e 20 20 4e 6f 74 65 l queries. Note
0c80: 20 69 6e 20 70 61 72 74 69 63 75 6c 61 72 20 74 in particular t
0c90: 68 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f he the content o
0ca0: 66 20 66 72 65 65 6c 69 73 74 20 6c 65 61 66 0a f freelist leaf.
0cb0: 2a 2a 20 70 61 67 65 73 20 63 61 6e 20 62 65 20 ** pages can be
0cc0: 63 68 61 6e 67 65 64 20 61 72 62 69 74 61 72 69 changed arbitari
0cd0: 6c 79 20 77 69 74 68 6f 75 74 20 65 66 66 65 63 ly without effec
0ce0: 74 69 6e 67 20 74 68 65 20 6c 6f 67 69 63 61 6c ting the logical
0cf0: 20 65 71 75 69 76 61 6c 65 6e 63 65 0a 2a 2a 20 equivalence.**
0d00: 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e of the database.
0d10: 0a 2a 2a 20 0a 2a 2a 20 28 37 29 20 41 74 20 61 .** .** (7) At a
0d20: 6e 79 20 74 69 6d 65 2c 20 69 66 20 61 6e 79 20 ny time, if any
0d30: 73 75 62 73 65 74 2c 20 69 6e 63 6c 75 64 69 6e subset, includin
0d40: 67 20 74 68 65 20 65 6d 70 74 79 20 73 65 74 20 g the empty set
0d50: 61 6e 64 20 74 68 65 20 74 6f 74 61 6c 20 73 65 and the total se
0d60: 74 2c 0a 2a 2a 20 20 20 20 20 6f 66 20 74 68 65 t,.** of the
0d70: 20 75 6e 73 79 6e 63 65 64 20 63 68 61 6e 67 65 unsynced change
0d80: 73 20 74 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 20 s to a rollback
0d90: 6a 6f 75 72 6e 61 6c 20 61 72 65 20 72 65 6d 6f journal are remo
0da0: 76 65 64 20 61 6e 64 20 74 68 65 20 0a 2a 2a 20 ved and the .**
0db0: 20 20 20 20 6a 6f 75 72 6e 61 6c 20 69 73 20 72 journal is r
0dc0: 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65 20 olled back, the
0dd0: 72 65 73 75 6c 74 69 6e 67 20 64 61 74 61 62 61 resulting databa
0de0: 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 se file will be
0df0: 6c 6f 67 69 63 61 6c 0a 2a 2a 20 20 20 20 20 65 logical.** e
0e00: 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 74 68 65 quivalent to the
0e10: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
0e20: 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 t the beginning
0e30: 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 of the transacti
0e40: 6f 6e 2e 0a 2a 2a 20 0a 2a 2a 20 28 38 29 20 57 on..** .** (8) W
0e50: 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f hen a transactio
0e60: 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b n is rolled back
0e70: 2c 20 74 68 65 20 78 54 72 75 6e 63 61 74 65 20 , the xTruncate
0e80: 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 56 46 method of the VF
0e90: 53 0a 2a 2a 20 20 20 20 20 69 73 20 63 61 6c 6c S.** is call
0ea0: 65 64 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 ed to restore th
0eb0: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
0ec0: 74 6f 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65 to the same size
0ed0: 20 69 74 20 77 61 73 20 61 74 0a 2a 2a 20 20 20 it was at.**
0ee0: 20 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 the beginning
0ef0: 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 of the transacti
0f00: 6f 6e 2e 20 20 28 49 6e 20 73 6f 6d 65 20 56 46 on. (In some VF
0f10: 53 65 73 2c 20 74 68 65 20 78 54 72 75 6e 63 61 Ses, the xTrunca
0f20: 74 65 0a 2a 2a 20 20 20 20 20 6d 65 74 68 6f 64 te.** method
0f30: 20 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 62 75 74 is a no-op, but
0f40: 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 63 that does not c
0f50: 68 61 6e 67 65 20 74 68 65 20 66 61 63 74 20 74 hange the fact t
0f60: 68 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a he SQLite will.*
0f70: 2a 20 20 20 20 20 69 6e 76 6f 6b 65 20 69 74 2e * invoke it.
0f80: 29 0a 2a 2a 20 0a 2a 2a 20 28 39 29 20 57 68 65 ).** .** (9) Whe
0f90: 6e 65 76 65 72 20 74 68 65 20 64 61 74 61 62 61 never the databa
0fa0: 73 65 20 66 69 6c 65 20 69 73 20 6d 6f 64 69 66 se file is modif
0fb0: 69 65 64 2c 20 61 74 20 6c 65 61 73 74 20 6f 6e ied, at least on
0fc0: 65 20 62 69 74 20 69 6e 20 74 68 65 20 72 61 6e e bit in the ran
0fd0: 67 65 0a 2a 2a 20 20 20 20 20 6f 66 20 62 79 74 ge.** of byt
0fe0: 65 73 20 66 72 6f 6d 20 32 34 20 74 68 72 6f 75 es from 24 throu
0ff0: 67 68 20 33 39 20 69 6e 63 6c 75 73 69 76 65 20 gh 39 inclusive
1000: 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 20 will be changed
1010: 70 72 69 6f 72 20 74 6f 20 72 65 6c 65 61 73 69 prior to releasi
1020: 6e 67 0a 2a 2a 20 20 20 20 20 74 68 65 20 45 58 ng.** the EX
1030: 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 0a 2a 2a CLUSIVE lock..**
1040: 0a 2a 2a 20 28 31 30 29 20 54 68 65 20 70 61 74 .** (10) The pat
1050: 74 65 72 6e 20 6f 66 20 62 69 74 73 20 69 6e 20 tern of bits in
1060: 62 79 74 65 73 20 32 34 20 74 68 72 6f 75 67 68 bytes 24 through
1070: 20 33 39 20 73 68 61 6c 6c 20 6e 6f 74 20 72 65 39 shall not re
1080: 70 65 61 74 20 69 6e 20 6c 65 73 73 0a 2a 2a 20 peat in less.**
1090: 20 20 20 20 20 74 68 61 6e 20 6f 6e 65 20 62 69 than one bi
10a0: 6c 6c 69 6f 6e 20 74 72 61 6e 73 61 63 74 69 6f llion transactio
10b0: 6e 73 2e 0a 2a 2a 0a 2a 2a 20 28 31 31 29 20 41 ns..**.** (11) A
10c0: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
10d0: 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 61 74 s well-formed at
10e0: 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 61 the beginning a
10f0: 6e 64 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 nd at the conclu
1100: 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 6f 66 20 sion.** of
1110: 65 76 65 72 79 20 74 72 61 6e 73 61 63 74 69 6f every transactio
1120: 6e 2e 0a 2a 2a 0a 2a 2a 20 28 31 32 29 20 41 6e n..**.** (12) An
1130: 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 EXCLUSIVE lock
1140: 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 is held on the d
1150: 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 65 atabase file whe
1160: 6e 20 77 72 69 74 69 6e 67 20 74 6f 0a 2a 2a 20 n writing to.**
1170: 20 20 20 20 20 74 68 65 20 64 61 74 61 62 61 73 the databas
1180: 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 28 31 e file..**.** (1
1190: 33 29 20 41 20 53 48 41 52 45 44 20 6c 6f 63 6b 3) A SHARED lock
11a0: 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 is held on the
11b0: 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 database file wh
11c0: 69 6c 65 20 72 65 61 64 69 6e 67 20 61 6e 79 0a ile reading any.
11d0: 2a 2a 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 20 ** content
11e0: 6f 75 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 out of the datab
11f0: 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 0a 2f 2a ase file..*/../*
1200: 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 74 .** Macros for t
1210: 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67 2e 20 roubleshooting.
1220: 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 Normally turned
1230: 20 6f 66 66 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e off.*/.#if 0.in
1240: 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 t sqlite3PagerTr
1250: 61 63 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20 ace=1; /* True
1260: 74 6f 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e to enable tracin
1270: 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c g */.#define sql
1280: 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 20 ite3DebugPrintf
1290: 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 50 printf.#define P
12a0: 41 47 45 52 54 52 41 43 45 28 58 29 20 20 20 20 AGERTRACE(X)
12b0: 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 if( sqlite3Page
12c0: 72 54 72 61 63 65 20 29 7b 20 73 71 6c 69 74 65 rTrace ){ sqlite
12d0: 33 44 65 62 75 67 50 72 69 6e 74 66 20 58 3b 20 3DebugPrintf X;
12e0: 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 }.#else.#define
12f0: 50 41 47 45 52 54 52 41 43 45 28 58 29 0a 23 65 PAGERTRACE(X).#e
1300: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
1310: 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 6d 61 following two ma
1320: 63 72 6f 73 20 61 72 65 20 75 73 65 64 20 77 69 cros are used wi
1330: 74 68 69 6e 20 74 68 65 20 50 41 47 45 52 54 52 thin the PAGERTR
1340: 41 43 45 28 29 20 6d 61 63 72 6f 73 20 61 62 6f ACE() macros abo
1350: 76 65 0a 2a 2a 20 74 6f 20 70 72 69 6e 74 20 6f ve.** to print o
1360: 75 74 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 ut file-descript
1370: 6f 72 73 2e 20 0a 2a 2a 0a 2a 2a 20 50 41 47 45 ors. .**.** PAGE
1380: 52 49 44 28 29 20 74 61 6b 65 73 20 61 20 70 6f RID() takes a po
1390: 69 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 72 inter to a Pager
13a0: 20 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61 struct as its a
13b0: 72 67 75 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 rgument. The.**
13c0: 61 73 73 6f 63 69 61 74 65 64 20 66 69 6c 65 2d associated file-
13d0: 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 72 65 descriptor is re
13e0: 74 75 72 6e 65 64 2e 20 46 49 4c 45 48 41 4e 44 turned. FILEHAND
13f0: 4c 45 49 44 28 29 20 74 61 6b 65 73 20 61 6e 20 LEID() takes an
1400: 73 71 6c 69 74 65 33 5f 66 69 6c 65 0a 2a 2a 20 sqlite3_file.**
1410: 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61 72 struct as its ar
1420: 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 gument..*/.#defi
1430: 6e 65 20 50 41 47 45 52 49 44 28 70 29 20 28 28 ne PAGERID(p) ((
1440: 69 6e 74 29 28 70 2d 3e 66 64 29 29 0a 23 64 65 int)(p->fd)).#de
1450: 66 69 6e 65 20 46 49 4c 45 48 41 4e 44 4c 45 49 fine FILEHANDLEI
1460: 44 28 66 64 29 20 28 28 69 6e 74 29 66 64 29 0a D(fd) ((int)fd).
1470: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 ./*.** The page
1480: 63 61 63 68 65 20 61 73 20 61 20 77 68 6f 6c 65 cache as a whole
1490: 20 69 73 20 61 6c 77 61 79 73 20 69 6e 20 6f 6e is always in on
14a0: 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
14b0: 6e 67 0a 2a 2a 20 73 74 61 74 65 73 3a 0a 2a 2a ng.** states:.**
14c0: 0a 2a 2a 20 20 20 50 41 47 45 52 5f 55 4e 4c 4f .** PAGER_UNLO
14d0: 43 4b 20 20 20 20 20 20 20 20 54 68 65 20 70 61 CK The pa
14e0: 67 65 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20 ge cache is not
14f0: 63 75 72 72 65 6e 74 6c 79 20 72 65 61 64 69 6e currently readin
1500: 67 20 6f 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 g or .**
1510: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 w
1520: 72 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 riting the datab
1530: 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 72 65 ase file. There
1540: 20 69 73 20 6e 6f 0a 2a 2a 20 20 20 20 20 20 20 is no.**
1550: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1560: 64 61 74 61 20 68 65 6c 64 20 69 6e 20 6d 65 6d data held in mem
1570: 6f 72 79 2e 20 20 54 68 69 73 20 69 73 20 74 68 ory. This is th
1580: 65 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 20 20 20 e initial.**
1590: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15a0: 20 20 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 state..**.**
15b0: 20 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 20 PAGER_SHARED
15c0: 20 20 20 20 20 20 54 68 65 20 70 61 67 65 20 63 The page c
15d0: 61 63 68 65 20 69 73 20 72 65 61 64 69 6e 67 20 ache is reading
15e0: 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a the database..**
15f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1600: 20 20 20 20 20 20 20 57 72 69 74 69 6e 67 20 69 Writing i
1610: 73 20 6e 6f 74 20 70 65 72 6d 69 74 74 65 64 2e s not permitted.
1620: 20 20 54 68 65 72 65 20 63 61 6e 20 62 65 0a 2a There can be.*
1630: 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
1640: 20 20 20 20 20 20 20 20 6d 75 6c 74 69 70 6c 65 multiple
1650: 20 72 65 61 64 65 72 73 20 61 63 63 65 73 73 69 readers accessi
1660: 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 ng the same data
1670: 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 base.**
1680: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69 fi
1690: 6c 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 le at the same t
16a0: 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 ime..**.** PAG
16b0: 45 52 5f 52 45 53 45 52 56 45 44 20 20 20 20 20 ER_RESERVED
16c0: 20 54 68 69 73 20 70 72 6f 63 65 73 73 20 68 61 This process ha
16d0: 73 20 72 65 73 65 72 76 65 64 20 74 68 65 20 64 s reserved the d
16e0: 61 74 61 62 61 73 65 20 66 6f 72 20 77 72 69 74 atabase for writ
16f0: 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ing.**
1700: 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75 74 but
1710: 20 68 61 73 20 6e 6f 74 20 79 65 74 20 6d 61 64 has not yet mad
1720: 65 20 61 6e 79 20 63 68 61 6e 67 65 73 2e 20 20 e any changes.
1730: 4f 6e 6c 79 20 6f 6e 65 20 70 72 6f 63 65 73 73 Only one process
1740: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1750: 20 20 20 20 20 20 20 20 20 20 61 74 20 61 20 74 at a t
1760: 69 6d 65 20 63 61 6e 20 72 65 73 65 72 76 65 20 ime can reserve
1770: 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 the database. T
1780: 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 20 he original.**
1790: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17a0: 20 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69 database fi
17b0: 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 le has not been
17c0: 6d 6f 64 69 66 69 65 64 20 73 6f 20 6f 74 68 65 modified so othe
17d0: 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 r.**
17e0: 20 20 20 20 20 20 20 20 20 20 20 70 72 6f 63 65 proce
17f0: 73 73 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 sses may still b
1800: 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 6f 6e e reading the on
1810: 2d 64 69 73 6b 0a 2a 2a 20 20 20 20 20 20 20 20 -disk.**
1820: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
1830: 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
1840: 0a 2a 2a 20 20 20 50 41 47 45 52 5f 45 58 43 4c .** PAGER_EXCL
1850: 55 53 49 56 45 20 20 20 20 20 54 68 65 20 70 61 USIVE The pa
1860: 67 65 20 63 61 63 68 65 20 69 73 20 77 72 69 74 ge cache is writ
1870: 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 ing the database
1880: 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 ..**
1890: 20 20 20 20 20 20 20 20 20 20 20 41 63 63 65 73 Acces
18a0: 73 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 s is exclusive.
18b0: 20 4e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 No other proces
18c0: 73 65 73 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 ses or.**
18d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18e0: 74 68 72 65 61 64 73 20 63 61 6e 20 62 65 20 72 threads can be r
18f0: 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e eading or writin
1900: 67 20 77 68 69 6c 65 20 6f 6e 65 0a 2a 2a 20 20 g while one.**
1910: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1920: 20 20 20 20 20 70 72 6f 63 65 73 73 20 69 73 20 process is
1930: 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 writing..**.**
1940: 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20 PAGER_SYNCED
1950: 20 20 20 20 20 54 68 65 20 70 61 67 65 72 20 6d The pager m
1960: 6f 76 65 73 20 74 6f 20 74 68 69 73 20 73 74 61 oves to this sta
1970: 74 65 20 66 72 6f 6d 20 50 41 47 45 52 5f 45 58 te from PAGER_EX
1980: 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 20 CLUSIVE.**
1990: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19a0: 20 61 66 74 65 72 20 61 6c 6c 20 64 69 72 74 79 after all dirty
19b0: 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e pages have been
19c0: 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a written to the.
19d0: 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
19e0: 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 databas
19f0: 65 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 66 e file and the f
1a00: 69 6c 65 20 68 61 73 20 62 65 65 6e 20 73 79 6e ile has been syn
1a10: 63 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 ced to.**
1a20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a30: 64 69 73 6b 2e 20 41 6c 6c 20 74 68 61 74 20 72 disk. All that r
1a40: 65 6d 61 69 6e 73 20 74 6f 20 64 6f 20 69 73 20 emains to do is
1a50: 74 6f 20 72 65 6d 6f 76 65 20 6f 72 0a 2a 2a 20 to remove or.**
1a60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a70: 20 20 20 20 20 20 74 72 75 6e 63 61 74 65 20 74 truncate t
1a80: 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1a90: 61 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 and the transact
1aa0: 69 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 ion .**
1ab0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 69 wi
1ac0: 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 2e ll be committed.
1ad0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 .**.** The page
1ae0: 63 61 63 68 65 20 63 6f 6d 65 73 20 75 70 20 69 cache comes up i
1af0: 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 n PAGER_UNLOCK.
1b00: 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 The first time
1b10: 61 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 a.** sqlite3Page
1b20: 72 47 65 74 28 29 20 6f 63 63 75 72 73 2c 20 74 rGet() occurs, t
1b30: 68 65 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 he state transit
1b40: 69 6f 6e 73 20 74 6f 20 50 41 47 45 52 5f 53 48 ions to PAGER_SH
1b50: 41 52 45 44 2e 0a 2a 2a 20 41 66 74 65 72 20 61 ARED..** After a
1b60: 6c 6c 20 70 61 67 65 73 20 68 61 76 65 20 62 65 ll pages have be
1b70: 65 6e 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e en released usin
1b80: 67 20 73 71 6c 69 74 65 5f 70 61 67 65 5f 75 6e g sqlite_page_un
1b90: 72 65 66 28 29 2c 0a 2a 2a 20 74 68 65 20 73 74 ref(),.** the st
1ba0: 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 ate transitions
1bb0: 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 55 4e back to PAGER_UN
1bc0: 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73 74 LOCK. The first
1bd0: 20 74 69 6d 65 0a 2a 2a 20 74 68 61 74 20 73 71 time.** that sq
1be0: 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
1bf0: 29 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 ) is called, the
1c00: 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f state transitio
1c10: 6e 73 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f 52 ns to.** PAGER_R
1c20: 45 53 45 52 56 45 44 2e 20 20 28 4e 6f 74 65 20 ESERVED. (Note
1c30: 74 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 65 that sqlite3Page
1c40: 72 57 72 69 74 65 28 29 20 63 61 6e 20 6f 6e 6c rWrite() can onl
1c50: 79 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 6f y be.** called o
1c60: 6e 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 n an outstanding
1c70: 20 70 61 67 65 20 77 68 69 63 68 20 6d 65 61 6e page which mean
1c80: 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72 s that the pager
1c90: 20 6d 75 73 74 0a 2a 2a 20 62 65 20 69 6e 20 50 must.** be in P
1ca0: 41 47 45 52 5f 53 48 41 52 45 44 20 62 65 66 6f AGER_SHARED befo
1cb0: 72 65 20 69 74 20 74 72 61 6e 73 69 74 69 6f 6e re it transition
1cc0: 73 20 74 6f 20 50 41 47 45 52 5f 52 45 53 45 52 s to PAGER_RESER
1cd0: 56 45 44 2e 29 0a 2a 2a 20 50 41 47 45 52 5f 52 VED.).** PAGER_R
1ce0: 45 53 45 52 56 45 44 20 6d 65 61 6e 73 20 74 68 ESERVED means th
1cf0: 61 74 20 74 68 65 72 65 20 69 73 20 61 6e 20 6f at there is an o
1d00: 70 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 pen rollback jou
1d10: 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 74 72 61 rnal..** The tra
1d20: 6e 73 69 74 69 6f 6e 20 74 6f 20 50 41 47 45 52 nsition to PAGER
1d30: 5f 45 58 43 4c 55 53 49 56 45 20 6f 63 63 75 72 _EXCLUSIVE occur
1d40: 73 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61 s before any cha
1d50: 6e 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 nges.** are made
1d60: 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
1d70: 20 66 69 6c 65 2c 20 74 68 6f 75 67 68 20 77 72 file, though wr
1d80: 69 74 65 73 20 74 6f 20 74 68 65 20 72 6f 6c 6c ites to the roll
1d90: 62 61 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 back.** journal
1da0: 6f 63 63 75 72 73 20 77 69 74 68 20 6a 75 73 74 occurs with just
1db0: 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e PAGER_RESERVED.
1dc0: 20 20 41 66 74 65 72 20 61 6e 20 73 71 6c 69 74 After an sqlit
1dd0: 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 e3PagerRollback(
1de0: 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 50 ).** or sqlite3P
1df0: 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 agerCommitPhaseT
1e00: 77 6f 28 29 2c 20 74 68 65 20 73 74 61 74 65 20 wo(), the state
1e10: 63 61 6e 20 67 6f 20 62 61 63 6b 20 74 6f 20 50 can go back to P
1e20: 41 47 45 52 5f 53 48 41 52 45 44 2c 0a 2a 2a 20 AGER_SHARED,.**
1e30: 6f 72 20 69 74 20 63 61 6e 20 73 74 61 79 20 61 or it can stay a
1e40: 74 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 t PAGER_EXCLUSIV
1e50: 45 20 69 66 20 77 65 20 61 72 65 20 69 6e 20 65 E if we are in e
1e60: 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 20 xclusive access
1e70: 6d 6f 64 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 mode..*/.#define
1e80: 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 PAGER_UNLOCK
1e90: 20 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41 47 0.#define PAG
1ea0: 45 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 31 ER_SHARED 1
1eb0: 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 53 48 /* same as SH
1ec0: 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 ARED_LOCK */.#de
1ed0: 66 69 6e 65 20 50 41 47 45 52 5f 52 45 53 45 52 fine PAGER_RESER
1ee0: 56 45 44 20 20 20 20 32 20 20 20 2f 2a 20 73 61 VED 2 /* sa
1ef0: 6d 65 20 61 73 20 52 45 53 45 52 56 45 44 5f 4c me as RESERVED_L
1f00: 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 OCK */.#define P
1f10: 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20 AGER_EXCLUSIVE
1f20: 20 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 4 /* same as
1f30: 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 2a EXCLUSIVE_LOCK *
1f40: 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f /.#define PAGER_
1f50: 53 59 4e 43 45 44 20 20 20 20 20 20 35 0a 0a 2f SYNCED 5../
1f60: 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 75 73 65 *.** A macro use
1f70: 64 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74 d for invoking t
1f80: 68 65 20 63 6f 64 65 63 20 69 66 20 74 68 65 72 he codec if ther
1f90: 65 20 69 73 20 6f 6e 65 0a 2a 2f 0a 23 69 66 64 e is one.*/.#ifd
1fa0: 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f ef SQLITE_HAS_CO
1fb0: 44 45 43 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 DEC.# define COD
1fc0: 45 43 31 28 50 2c 44 2c 4e 2c 58 2c 45 29 20 5c EC1(P,D,N,X,E) \
1fd0: 0a 20 20 20 20 69 66 28 20 50 2d 3e 78 43 6f 64 . if( P->xCod
1fe0: 65 63 20 26 26 20 50 2d 3e 78 43 6f 64 65 63 28 ec && P->xCodec(
1ff0: 50 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e 2c 58 29 P->pCodec,D,N,X)
2000: 3d 3d 30 20 29 7b 20 45 3b 20 7d 0a 23 20 64 65 ==0 ){ E; }.# de
2010: 66 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c fine CODEC2(P,D,
2020: 4e 2c 58 2c 45 2c 4f 29 20 5c 0a 20 20 20 20 69 N,X,E,O) \. i
2030: 66 28 20 50 2d 3e 78 43 6f 64 65 63 3d 3d 30 20 f( P->xCodec==0
2040: 29 7b 20 4f 3d 28 63 68 61 72 2a 29 44 3b 20 7d ){ O=(char*)D; }
2050: 65 6c 73 65 20 5c 0a 20 20 20 20 69 66 28 20 28 else \. if( (
2060: 4f 3d 28 63 68 61 72 2a 29 28 50 2d 3e 78 43 6f O=(char*)(P->xCo
2070: 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 2c 44 2c dec(P->pCodec,D,
2080: 4e 2c 58 29 29 29 3d 3d 30 20 29 7b 20 45 3b 20 N,X)))==0 ){ E;
2090: 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 }.#else.# define
20a0: 20 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 2c CODEC1(P,D,N,X,
20b0: 45 29 20 20 20 2f 2a 20 4e 4f 2d 4f 50 20 2a 2f E) /* NO-OP */
20c0: 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 32 .# define CODEC2
20d0: 28 50 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 20 4f 3d (P,D,N,X,E,O) O=
20e0: 28 63 68 61 72 2a 29 44 0a 23 65 6e 64 69 66 0a (char*)D.#endif.
20f0: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d ./*.** The maxim
2100: 75 6d 20 61 6c 6c 6f 77 65 64 20 73 65 63 74 6f um allowed secto
2110: 72 20 73 69 7a 65 2e 20 36 34 4b 69 42 2e 20 49 r size. 64KiB. I
2120: 66 20 74 68 65 20 78 53 65 63 74 6f 72 73 69 7a f the xSectorsiz
2130: 65 28 29 20 6d 65 74 68 6f 64 20 0a 2a 2a 20 72 e() method .** r
2140: 65 74 75 72 6e 73 20 61 20 76 61 6c 75 65 20 6c eturns a value l
2150: 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 73 2c arger than this,
2160: 20 74 68 65 6e 20 4d 41 58 5f 53 45 43 54 4f 52 then MAX_SECTOR
2170: 5f 53 49 5a 45 20 69 73 20 75 73 65 64 20 69 6e _SIZE is used in
2180: 73 74 65 61 64 2e 0a 2a 2a 20 54 68 69 73 20 63 stead..** This c
2190: 6f 75 6c 64 20 63 6f 6e 63 65 69 76 61 62 6c 79 ould conceivably
21a0: 20 63 61 75 73 65 20 63 6f 72 72 75 70 74 69 6f cause corruptio
21b0: 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 70 6f n following a po
21c0: 77 65 72 20 66 61 69 6c 75 72 65 20 6f 6e 0a 2a wer failure on.*
21d0: 2a 20 73 75 63 68 20 61 20 73 79 73 74 65 6d 2e * such a system.
21e0: 20 54 68 69 73 20 69 73 20 63 75 72 72 65 6e 74 This is current
21f0: 6c 79 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 ly an undocument
2200: 65 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64 65 ed limit..*/.#de
2210: 66 69 6e 65 20 4d 41 58 5f 53 45 43 54 4f 52 5f fine MAX_SECTOR_
2220: 53 49 5a 45 20 30 78 31 30 30 30 30 0a 0a 2f 2a SIZE 0x10000../*
2230: 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
2240: 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
2250: 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c structure is al
2260: 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 located for each
2270: 20 61 63 74 69 76 65 0a 2a 2a 20 73 61 76 65 70 active.** savep
2280: 6f 69 6e 74 20 61 6e 64 20 73 74 61 74 65 6d 65 oint and stateme
2290: 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 nt transaction i
22a0: 6e 20 74 68 65 20 73 79 73 74 65 6d 2e 20 41 6c n the system. Al
22b0: 6c 20 73 75 63 68 20 73 74 72 75 63 74 75 72 65 l such structure
22c0: 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 20 s.** are stored
22d0: 69 6e 20 74 68 65 20 50 61 67 65 72 2e 61 53 61 in the Pager.aSa
22e0: 76 65 70 6f 69 6e 74 5b 5d 20 61 72 72 61 79 2c vepoint[] array,
22f0: 20 77 68 69 63 68 20 69 73 20 61 6c 6c 6f 63 61 which is alloca
2300: 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65 73 69 7a ted and.** resiz
2310: 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 ed using sqlite3
2320: 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a Realloc()..**.**
2330: 20 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69 6e When a savepoin
2340: 74 20 69 73 20 63 72 65 61 74 65 64 2c 20 74 68 t is created, th
2350: 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 e PagerSavepoint
2360: 2e 69 48 64 72 4f 66 66 73 65 74 20 66 69 65 6c .iHdrOffset fiel
2370: 64 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 d is.** set to 0
2380: 2e 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 2d 68 . If a journal-h
2390: 65 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e eader is written
23a0: 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 6a into the main j
23b0: 6f 75 72 6e 61 6c 20 77 68 69 6c 65 0a 2a 2a 20 ournal while.**
23c0: 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 69 73 the savepoint is
23d0: 20 61 63 74 69 76 65 2c 20 74 68 65 6e 20 69 48 active, then iH
23e0: 64 72 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 drOffset is set
23f0: 74 6f 20 74 68 65 20 62 79 74 65 20 6f 66 66 73 to the byte offs
2400: 65 74 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 et .** immediate
2410: 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 ly following the
2420: 20 6c 61 73 74 20 6a 6f 75 72 6e 61 6c 20 72 65 last journal re
2430: 63 6f 72 64 20 77 72 69 74 74 65 6e 20 69 6e 74 cord written int
2440: 6f 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f o the main.** jo
2450: 75 72 6e 61 6c 20 62 65 66 6f 72 65 20 74 68 65 urnal before the
2460: 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 2e journal-header.
2470: 20 54 68 69 73 20 69 73 20 72 65 71 75 69 72 65 This is require
2480: 64 20 64 75 72 69 6e 67 20 73 61 76 65 70 6f 69 d during savepoi
2490: 6e 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 28 nt.** rollback (
24a0: 73 65 65 20 70 61 67 65 72 50 6c 61 79 62 61 63 see pagerPlaybac
24b0: 6b 53 61 76 65 70 6f 69 6e 74 28 29 29 2e 0a 2a kSavepoint())..*
24c0: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
24d0: 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 PagerSavepoint
24e0: 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 3b 0a PagerSavepoint;.
24f0: 73 74 72 75 63 74 20 50 61 67 65 72 53 61 76 65 struct PagerSave
2500: 70 6f 69 6e 74 20 7b 0a 20 20 69 36 34 20 69 4f point {. i64 iO
2510: 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 ffset;
2520: 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 69 /* Starti
2530: 6e 67 20 6f 66 66 73 65 74 20 69 6e 20 6d 61 69 ng offset in mai
2540: 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 n journal */. i
2550: 36 34 20 69 48 64 72 4f 66 66 73 65 74 3b 20 20 64 iHdrOffset;
2560: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
2570: 65 65 20 61 62 6f 76 65 20 2a 2f 0a 20 20 42 69 ee above */. Bi
2580: 74 76 65 63 20 2a 70 49 6e 53 61 76 65 70 6f 69 tvec *pInSavepoi
2590: 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 65 nt; /* Se
25a0: 74 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 t of pages in th
25b0: 69 73 20 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a is savepoint */.
25c0: 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 20 Pgno nOrig;
25d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
25e0: 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 75 6d 62 65 * Original numbe
25f0: 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 66 69 r of pages in fi
2600: 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 53 75 le */. Pgno iSu
2610: 62 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 bRec;
2620: 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
2630: 20 66 69 72 73 74 20 72 65 63 6f 72 64 20 69 6e first record in
2640: 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a sub-journal */.
2650: 20 20 75 33 32 20 61 57 61 6c 44 61 74 61 5b 57 u32 aWalData[W
2660: 41 4c 5f 53 41 56 45 50 4f 49 4e 54 5f 4e 44 41 AL_SAVEPOINT_NDA
2670: 54 41 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 57 TA]; /* W
2680: 41 4c 20 73 61 76 65 70 6f 69 6e 74 20 63 6f 6e AL savepoint con
2690: 74 65 78 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a text */.};../*.*
26a0: 2a 20 41 20 6f 70 65 6e 20 70 61 67 65 20 63 61 * A open page ca
26b0: 63 68 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e che is an instan
26c0: 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
26d0: 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ing structure..*
26e0: 2a 0a 2a 2a 20 65 72 72 43 6f 64 65 0a 2a 2a 0a *.** errCode.**.
26f0: 2a 2a 20 20 20 50 61 67 65 72 2e 65 72 72 43 6f ** Pager.errCo
2700: 64 65 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f de may be set to
2710: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53 SQLITE_IOERR, S
2720: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2c 20 6f QLITE_CORRUPT, o
2730: 72 0a 2a 2a 20 20 20 6f 72 20 53 51 4c 49 54 45 r.** or SQLITE
2740: 5f 46 55 4c 4c 2e 20 4f 6e 63 65 20 6f 6e 65 20 _FULL. Once one
2750: 6f 66 20 74 68 65 20 66 69 72 73 74 20 74 68 72 of the first thr
2760: 65 65 20 65 72 72 6f 72 73 20 6f 63 63 75 72 73 ee errors occurs
2770: 2c 20 69 74 20 70 65 72 73 69 73 74 73 0a 2a 2a , it persists.**
2780: 20 20 20 61 6e 64 20 69 73 20 72 65 74 75 72 6e and is return
2790: 65 64 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 ed as the result
27a0: 20 6f 66 20 65 76 65 72 79 20 6d 61 6a 6f 72 20 of every major
27b0: 70 61 67 65 72 20 41 50 49 20 63 61 6c 6c 2e 20 pager API call.
27c0: 20 54 68 65 0a 2a 2a 20 20 20 53 51 4c 49 54 45 The.** SQLITE
27d0: 5f 46 55 4c 4c 20 72 65 74 75 72 6e 20 63 6f 64 _FULL return cod
27e0: 65 20 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69 e is slightly di
27f0: 66 66 65 72 65 6e 74 2e 20 49 74 20 70 65 72 73 fferent. It pers
2800: 69 73 74 73 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 ists only until
2810: 74 68 65 0a 2a 2a 20 20 20 6e 65 78 74 20 73 75 the.** next su
2820: 63 63 65 73 73 66 75 6c 20 72 6f 6c 6c 62 61 63 ccessful rollbac
2830: 6b 20 69 73 20 70 65 72 66 6f 72 6d 65 64 20 6f k is performed o
2840: 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 n the pager cach
2850: 65 2e 20 41 6c 73 6f 2c 0a 2a 2a 20 20 20 53 51 e. Also,.** SQ
2860: 4c 49 54 45 5f 46 55 4c 4c 20 64 6f 65 73 20 6e LITE_FULL does n
2870: 6f 74 20 61 66 66 65 63 74 20 74 68 65 20 73 71 ot affect the sq
2880: 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 lite3PagerGet()
2890: 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 and sqlite3Pager
28a0: 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 20 20 41 50 Lookup().** AP
28b0: 49 73 2c 20 74 68 65 79 20 6d 61 79 20 73 74 69 Is, they may sti
28c0: 6c 6c 20 62 65 20 75 73 65 64 20 73 75 63 63 65 ll be used succe
28d0: 73 73 66 75 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 64 ssfully..**.** d
28e0: 62 53 69 7a 65 56 61 6c 69 64 2c 20 64 62 53 69 bSizeValid, dbSi
28f0: 7a 65 2c 20 64 62 4f 72 69 67 53 69 7a 65 2c 20 ze, dbOrigSize,
2900: 64 62 46 69 6c 65 53 69 7a 65 0a 2a 2a 0a 2a 2a dbFileSize.**.**
2910: 20 20 20 4d 61 6e 61 67 69 6e 67 20 74 68 65 20 Managing the
2920: 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 size of the data
2930: 62 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67 base file in pag
2940: 65 73 20 69 73 20 61 20 6c 69 74 74 6c 65 20 63 es is a little c
2950: 6f 6d 70 6c 69 63 61 74 65 64 2e 0a 2a 2a 20 20 omplicated..**
2960: 20 54 68 65 20 76 61 72 69 61 62 6c 65 20 50 61 The variable Pa
2970: 67 65 72 2e 64 62 53 69 7a 65 20 63 6f 6e 74 61 ger.dbSize conta
2980: 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ins the number o
2990: 66 20 70 61 67 65 73 20 74 68 61 74 20 74 68 65 f pages that the
29a0: 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 69 database.** i
29b0: 6d 61 67 65 20 63 75 72 72 65 6e 74 6c 79 20 63 mage currently c
29c0: 6f 6e 74 61 69 6e 73 2e 20 41 73 20 74 68 65 20 ontains. As the
29d0: 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 67 database image g
29e0: 72 6f 77 73 20 6f 72 20 73 68 72 69 6e 6b 73 20 rows or shrinks
29f0: 74 68 69 73 0a 2a 2a 20 20 20 76 61 72 69 61 62 this.** variab
2a00: 6c 65 20 69 73 20 75 70 64 61 74 65 64 2e 20 54 le is updated. T
2a10: 68 65 20 76 61 72 69 61 62 6c 65 20 50 61 67 65 he variable Page
2a20: 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 63 6f 6e r.dbFileSize con
2a30: 74 61 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 tains the number
2a40: 0a 2a 2a 20 20 20 6f 66 20 70 61 67 65 73 20 69 .** of pages i
2a50: 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
2a60: 69 6c 65 2e 20 54 68 69 73 20 6d 61 79 20 62 65 ile. This may be
2a70: 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 different from
2a80: 50 61 67 65 72 2e 64 62 53 69 7a 65 0a 2a 2a 20 Pager.dbSize.**
2a90: 20 20 69 66 20 73 6f 6d 65 20 70 61 67 65 73 20 if some pages
2aa0: 68 61 76 65 20 62 65 65 6e 20 61 70 70 65 6e 64 have been append
2ab0: 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ed to the databa
2ac0: 73 65 20 69 6d 61 67 65 20 62 75 74 20 6e 6f 74 se image but not
2ad0: 20 79 65 74 20 77 72 69 74 74 65 6e 0a 2a 2a 20 yet written.**
2ae0: 20 20 6f 75 74 20 66 72 6f 6d 20 74 68 65 20 63 out from the c
2af0: 61 63 68 65 20 74 6f 20 74 68 65 20 61 63 74 75 ache to the actu
2b00: 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e al file on disk.
2b10: 20 4f 72 20 69 66 20 74 68 65 20 69 6d 61 67 65 Or if the image
2b20: 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 20 20 74 has been.** t
2b30: 72 75 6e 63 61 74 65 64 20 62 79 20 61 6e 20 69 runcated by an i
2b40: 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 61 63 75 75 ncremental-vacuu
2b50: 6d 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 65 m operation. The
2b60: 20 50 61 67 65 72 2e 64 62 4f 72 69 67 53 69 7a Pager.dbOrigSiz
2b70: 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 20 20 e variable.**
2b80: 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 75 6d contains the num
2b90: 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
2ba0: 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 the database ima
2bb0: 67 65 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 ge when the curr
2bc0: 65 6e 74 0a 2a 2a 20 20 20 74 72 61 6e 73 61 63 ent.** transac
2bd0: 74 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 2e tion was opened.
2be0: 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 The contents of
2bf0: 20 61 6c 6c 20 74 68 72 65 65 20 6f 66 20 74 68 all three of th
2c00: 65 73 65 20 76 61 72 69 61 62 6c 65 73 20 69 73 ese variables is
2c10: 0a 2a 2a 20 20 20 6f 6e 6c 79 20 67 75 61 72 61 .** only guara
2c20: 6e 74 65 65 64 20 74 6f 20 62 65 20 63 6f 72 72 nteed to be corr
2c30: 65 63 74 20 69 66 20 74 68 65 20 62 6f 6f 6c 65 ect if the boole
2c40: 61 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 56 an Pager.dbSizeV
2c50: 61 6c 69 64 20 69 73 20 74 72 75 65 2e 0a 2a 2a alid is true..**
2c60: 0a 2a 2a 20 20 20 54 4f 44 4f 3a 20 55 6e 64 65 .** TODO: Unde
2c70: 72 20 77 68 61 74 20 63 6f 6e 64 69 74 69 6f 6e r what condition
2c80: 73 20 69 73 20 64 62 53 69 7a 65 56 61 6c 69 64 s is dbSizeValid
2c90: 20 73 65 74 3f 20 43 6c 65 61 72 65 64 3f 0a 2a set? Cleared?.*
2ca0: 2a 0a 2a 2a 20 63 68 61 6e 67 65 43 6f 75 6e 74 *.** changeCount
2cb0: 44 6f 6e 65 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 Done.**.** Thi
2cc0: 73 20 62 6f 6f 6c 65 61 6e 20 76 61 72 69 61 62 s boolean variab
2cd0: 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 le is used to ma
2ce0: 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 ke sure that the
2cf0: 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 change-counter
2d00: 0a 2a 2a 20 20 20 28 74 68 65 20 34 2d 62 79 74 .** (the 4-byt
2d10: 65 20 68 65 61 64 65 72 20 66 69 65 6c 64 20 61 e header field a
2d20: 74 20 62 79 74 65 20 6f 66 66 73 65 74 20 32 34 t byte offset 24
2d30: 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
2d40: 20 66 69 6c 65 29 20 69 73 20 0a 2a 2a 20 20 20 file) is .**
2d50: 6e 6f 74 20 75 70 64 61 74 65 64 20 6d 6f 72 65 not updated more
2d60: 20 6f 66 74 65 6e 20 74 68 61 6e 20 6e 65 63 65 often than nece
2d70: 73 73 61 72 79 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 ssary. .**.**
2d80: 49 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 It is set to tru
2d90: 65 20 77 68 65 6e 20 74 68 65 20 63 68 61 6e 67 e when the chang
2da0: 65 2d 63 6f 75 6e 74 65 72 20 66 69 65 6c 64 20 e-counter field
2db0: 69 73 20 75 70 64 61 74 65 64 2c 20 77 68 69 63 is updated, whic
2dc0: 68 20 0a 2a 2a 20 20 20 63 61 6e 20 6f 6e 6c 79 h .** can only
2dd0: 20 68 61 70 70 65 6e 20 69 66 20 61 6e 20 65 78 happen if an ex
2de0: 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 clusive lock is
2df0: 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61 held on the data
2e00: 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 base file..**
2e10: 49 74 20 69 73 20 63 6c 65 61 72 65 64 20 28 73 It is cleared (s
2e20: 65 74 20 74 6f 20 66 61 6c 73 65 29 20 77 68 65 et to false) whe
2e30: 6e 65 76 65 72 20 61 6e 20 65 78 63 6c 75 73 69 never an exclusi
2e40: 76 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a 20 20 ve lock is .**
2e50: 20 72 65 6c 69 6e 71 75 69 73 68 65 64 20 6f 6e relinquished on
2e60: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
2e70: 6c 65 2e 20 45 61 63 68 20 74 69 6d 65 20 61 20 le. Each time a
2e80: 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 transaction is c
2e90: 6f 6d 6d 69 74 74 65 64 2c 0a 2a 2a 20 20 20 54 ommitted,.** T
2ea0: 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f he changeCountDo
2eb0: 6e 65 20 66 6c 61 67 20 69 73 20 69 6e 73 70 65 ne flag is inspe
2ec0: 63 74 65 64 2e 20 49 66 20 69 74 20 69 73 20 74 cted. If it is t
2ed0: 72 75 65 2c 20 74 68 65 20 77 6f 72 6b 20 6f 66 rue, the work of
2ee0: 0a 2a 2a 20 20 20 75 70 64 61 74 69 6e 67 20 74 .** updating t
2ef0: 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 he change-counte
2f00: 72 20 69 73 20 6f 6d 69 74 74 65 64 20 66 6f 72 r is omitted for
2f10: 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 the current tra
2f20: 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nsaction..**.**
2f30: 20 20 54 68 69 73 20 6d 65 63 68 61 6e 69 73 6d This mechanism
2f40: 20 6d 65 61 6e 73 20 74 68 61 74 20 77 68 65 6e means that when
2f50: 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c running in excl
2f60: 75 73 69 76 65 20 6d 6f 64 65 2c 20 61 20 63 6f usive mode, a co
2f70: 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 20 20 6e nnection .** n
2f80: 65 65 64 20 6f 6e 6c 79 20 75 70 64 61 74 65 20 eed only update
2f90: 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
2fa0: 65 72 20 6f 6e 63 65 2c 20 66 6f 72 20 74 68 65 er once, for the
2fb0: 20 66 69 72 73 74 20 74 72 61 6e 73 61 63 74 69 first transacti
2fc0: 6f 6e 0a 2a 2a 20 20 20 63 6f 6d 6d 69 74 74 65 on.** committe
2fd0: 64 2e 0a 2a 2a 0a 2a 2a 20 64 62 4d 6f 64 69 66 d..**.** dbModif
2fe0: 69 65 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 ied.**.** The
2ff0: 64 62 4d 6f 64 69 66 69 65 64 20 66 6c 61 67 20 dbModified flag
3000: 69 73 20 73 65 74 20 77 68 65 6e 65 76 65 72 20 is set whenever
3010: 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 a database page
3020: 69 73 20 64 69 72 74 69 65 64 2e 0a 2a 2a 20 20 is dirtied..**
3030: 20 49 74 20 69 73 20 63 6c 65 61 72 65 64 20 61 It is cleared a
3040: 74 20 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63 t the end of eac
3050: 68 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a h transaction..*
3060: 2a 0a 2a 2a 20 20 20 49 74 20 69 73 20 75 73 65 *.** It is use
3070: 64 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e d when committin
3080: 67 20 6f 72 20 6f 74 68 65 72 77 69 73 65 20 65 g or otherwise e
3090: 6e 64 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74 nding a transact
30a0: 69 6f 6e 2e 20 49 66 0a 2a 2a 20 20 20 74 68 65 ion. If.** the
30b0: 20 64 62 4d 6f 64 69 66 69 65 64 20 66 6c 61 67 dbModified flag
30c0: 20 69 73 20 63 6c 65 61 72 20 74 68 65 6e 20 6c is clear then l
30d0: 65 73 73 20 77 6f 72 6b 20 68 61 73 20 74 6f 20 ess work has to
30e0: 62 65 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 6a be done..**.** j
30f0: 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 0a 2a 2a ournalStarted.**
3100: 0a 2a 2a 20 20 20 54 68 69 73 20 66 6c 61 67 20 .** This flag
3110: 69 73 20 73 65 74 20 77 68 65 6e 65 76 65 72 20 is set whenever
3120: 74 68 65 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 the the main jou
3130: 72 6e 61 6c 20 69 73 20 6f 70 65 6e 65 64 20 61 rnal is opened a
3140: 6e 64 0a 2a 2a 20 20 20 69 6e 69 74 69 61 6c 69 nd.** initiali
3150: 7a 65 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 zed.**.** The
3160: 70 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 66 6c point of this fl
3170: 61 67 20 69 73 20 74 68 61 74 20 69 74 20 6d 75 ag is that it mu
3180: 73 74 20 62 65 20 73 65 74 20 61 66 74 65 72 20 st be set after
3190: 74 68 65 20 0a 2a 2a 20 20 20 66 69 72 73 74 20 the .** first
31a0: 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 journal header i
31b0: 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 n a journal file
31c0: 20 68 61 73 20 62 65 65 6e 20 73 79 6e 63 65 64 has been synced
31d0: 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 20 20 20 41 to disk..** A
31e0: 66 74 65 72 20 74 68 69 73 20 68 61 73 20 68 61 fter this has ha
31f0: 70 70 65 6e 65 64 2c 20 6e 65 77 20 70 61 67 65 ppened, new page
3200: 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 s appended to th
3210: 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 20 e database .**
3220: 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 68 65 do not need the
3230: 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 PGHDR_NEED_SYNC
3240: 20 66 6c 61 67 20 73 65 74 2c 20 61 73 20 74 68 flag set, as th
3250: 65 79 20 64 6f 20 6e 6f 74 20 6e 65 65 64 0a 2a ey do not need.*
3260: 2a 20 20 20 74 6f 20 77 61 69 74 20 66 6f 72 20 * to wait for
3270: 61 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 20 62 a journal sync b
3280: 65 66 6f 72 65 20 74 68 65 79 20 63 61 6e 20 62 efore they can b
3290: 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f e written out to
32a0: 0a 2a 2a 20 20 20 74 68 65 20 64 61 74 61 62 61 .** the databa
32b0: 73 65 20 66 69 6c 65 20 28 73 65 65 20 66 75 6e se file (see fun
32c0: 63 74 69 6f 6e 20 70 61 67 65 72 5f 77 72 69 74 ction pager_writ
32d0: 65 28 29 29 2e 0a 2a 2a 20 20 20 0a 2a 2a 20 73 e())..** .** s
32e0: 65 74 4d 61 73 74 65 72 0a 2a 2a 0a 2a 2a 20 20 etMaster.**.**
32f0: 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 This variable i
3300: 73 20 75 73 65 64 20 74 6f 20 65 6e 73 75 72 65 s used to ensure
3310: 20 74 68 61 74 20 74 68 65 20 6d 61 73 74 65 72 that the master
3320: 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 journal file na
3330: 6d 65 0a 2a 2a 20 20 20 28 69 66 20 61 6e 79 29 me.** (if any)
3340: 20 69 73 20 6f 6e 6c 79 20 77 72 69 74 74 65 6e is only written
3350: 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 into the journa
3360: 6c 20 66 69 6c 65 20 6f 6e 63 65 2e 0a 2a 2a 0a l file once..**.
3370: 2a 2a 20 20 20 57 68 65 6e 20 63 6f 6d 6d 69 74 ** When commit
3380: 74 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 ting a transacti
3390: 6f 6e 2c 20 74 68 65 20 6d 61 73 74 65 72 20 6a on, the master j
33a0: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
33b0: 20 28 69 66 20 61 6e 79 29 0a 2a 2a 20 20 20 6d (if any).** m
33c0: 61 79 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e ay be written in
33d0: 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
33e0: 69 6c 65 20 77 68 69 6c 65 20 74 68 65 20 70 61 ile while the pa
33f0: 67 65 72 20 69 73 20 73 74 69 6c 6c 20 69 6e 0a ger is still in.
3400: 2a 2a 20 20 20 50 41 47 45 52 5f 52 45 53 45 52 ** PAGER_RESER
3410: 56 45 44 20 73 74 61 74 65 20 28 73 65 65 20 43 VED state (see C
3420: 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 ommitPhaseOne()
3430: 66 6f 72 20 74 68 65 20 61 63 74 69 6f 6e 29 2e for the action).
3440: 20 49 74 0a 2a 2a 20 20 20 74 68 65 6e 20 61 74 It.** then at
3450: 74 65 6d 70 74 73 20 74 6f 20 75 70 67 72 61 64 tempts to upgrad
3460: 65 20 74 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 e to an exclusiv
3470: 65 20 6c 6f 63 6b 2e 20 49 66 20 74 68 69 73 20 e lock. If this
3480: 61 74 74 65 6d 70 74 0a 2a 2a 20 20 20 66 61 69 attempt.** fai
3490: 6c 73 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f ls, then SQLITE_
34a0: 42 55 53 59 20 6d 61 79 20 62 65 20 72 65 74 75 BUSY may be retu
34b0: 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 rned to the user
34c0: 20 61 6e 64 20 74 68 65 20 75 73 65 72 0a 2a 2a and the user.**
34d0: 20 20 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74 may attempt t
34e0: 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 o commit the tra
34f0: 6e 73 61 63 74 69 6f 6e 20 61 67 61 69 6e 20 6c nsaction again l
3500: 61 74 65 72 20 28 63 61 6c 6c 69 6e 67 0a 2a 2a ater (calling.**
3510: 20 20 20 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e CommitPhaseOn
3520: 65 28 29 20 61 67 61 69 6e 29 2e 20 54 68 69 73 e() again). This
3530: 20 66 6c 61 67 20 69 73 20 75 73 65 64 20 74 6f flag is used to
3540: 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 ensure that the
3550: 20 0a 2a 2a 20 20 20 6d 61 73 74 65 72 20 6a 6f .** master jo
3560: 75 72 6e 61 6c 20 6e 61 6d 65 20 69 73 20 6f 6e urnal name is on
3570: 6c 79 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 ly written to th
3580: 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 e journal file t
3590: 68 65 20 66 69 72 73 74 0a 2a 2a 20 20 20 74 69 he first.** ti
35a0: 6d 65 20 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e me CommitPhaseOn
35b0: 65 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a e() is called..*
35c0: 2a 0a 2a 2a 20 64 6f 4e 6f 74 53 79 6e 63 0a 2a *.** doNotSync.*
35d0: 2a 0a 2a 2a 20 20 20 57 68 65 6e 20 65 6e 61 62 *.** When enab
35e0: 6c 65 64 2c 20 63 61 63 68 65 20 73 70 69 6c 6c led, cache spill
35f0: 73 20 61 72 65 20 70 72 6f 68 69 62 69 74 65 64 s are prohibited
3600: 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c and the journal
3610: 20 66 69 6c 65 20 63 61 6e 6e 6f 74 0a 2a 2a 20 file cannot.**
3620: 20 20 62 65 20 73 79 6e 63 65 64 2e 20 20 54 68 be synced. Th
3630: 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 is variable is s
3640: 65 74 20 61 6e 64 20 63 6c 65 61 72 65 64 20 62 et and cleared b
3650: 79 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 y sqlite3PagerWr
3660: 69 74 65 28 29 20 0a 2a 2a 20 20 20 69 6e 20 6f ite() .** in o
3670: 72 64 65 72 20 74 6f 20 70 72 65 76 65 6e 74 20 rder to prevent
3680: 61 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 20 66 a journal sync f
3690: 72 6f 6d 20 68 61 70 70 65 6e 69 6e 67 20 69 6e rom happening in
36a0: 20 62 65 74 77 65 65 6e 20 74 68 65 0a 2a 2a 20 between the.**
36b0: 20 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 6f 66 journalling of
36c0: 20 74 77 6f 20 70 61 67 65 73 20 6f 6e 20 74 68 two pages on th
36d0: 65 20 73 61 6d 65 20 73 65 63 74 6f 72 2e 0a 2a e same sector..*
36e0: 2a 0a 2a 2a 20 6e 65 65 64 53 79 6e 63 0a 2a 2a *.** needSync.**
36f0: 0a 2a 2a 20 20 20 54 4f 44 4f 3a 20 49 74 20 6d .** TODO: It m
3700: 69 67 68 74 20 62 65 20 65 61 73 69 65 72 20 74 ight be easier t
3710: 6f 20 73 65 74 20 74 68 69 73 20 76 61 72 69 61 o set this varia
3720: 62 6c 65 20 69 6e 20 77 72 69 74 65 4a 6f 75 72 ble in writeJour
3730: 6e 61 6c 48 64 72 28 29 0a 2a 2a 20 20 20 61 6e nalHdr().** an
3740: 64 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 d writeMasterJou
3750: 72 6e 61 6c 28 29 20 6f 6e 6c 79 2e 20 43 68 61 rnal() only. Cha
3760: 6e 67 65 20 69 74 73 20 6d 65 61 6e 69 6e 67 20 nge its meaning
3770: 74 6f 20 22 75 6e 73 79 6e 63 65 64 20 64 61 74 to "unsynced dat
3780: 61 0a 2a 2a 20 20 20 68 61 73 20 62 65 65 6e 20 a.** has been
3790: 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a written to the j
37a0: 6f 75 72 6e 61 6c 22 2e 0a 2a 2a 0a 2a 2a 20 73 ournal"..**.** s
37b0: 75 62 6a 49 6e 4d 65 6d 6f 72 79 0a 2a 2a 0a 2a ubjInMemory.**.*
37c0: 2a 20 20 20 54 68 69 73 20 69 73 20 61 20 62 6f * This is a bo
37d0: 6f 6c 65 61 6e 20 76 61 72 69 61 62 6c 65 2e 20 olean variable.
37e0: 49 66 20 74 72 75 65 2c 20 74 68 65 6e 20 61 6e If true, then an
37f0: 79 20 72 65 71 75 69 72 65 64 20 73 75 62 2d 6a y required sub-j
3800: 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 69 73 20 6f ournal.** is o
3810: 70 65 6e 65 64 20 61 73 20 61 6e 20 69 6e 2d 6d pened as an in-m
3820: 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 emory journal fi
3830: 6c 65 2e 20 49 66 20 66 61 6c 73 65 2c 20 74 68 le. If false, th
3840: 65 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 en in-memory.**
3850: 20 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 61 sub-journals a
3860: 72 65 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72 re only used for
3870: 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 72 in-memory pager
3880: 20 66 69 6c 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 files..*/.struc
3890: 74 20 50 61 67 65 72 20 7b 0a 20 20 73 71 6c 69 t Pager {. sqli
38a0: 74 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 te3_vfs *pVfs;
38b0: 20 20 20 20 20 20 20 20 2f 2a 20 4f 53 20 66 75 /* OS fu
38c0: 6e 63 74 69 6f 6e 73 20 74 6f 20 75 73 65 20 66 nctions to use f
38d0: 6f 72 20 49 4f 20 2a 2f 0a 20 20 75 38 20 65 78 or IO */. u8 ex
38e0: 63 6c 75 73 69 76 65 4d 6f 64 65 3b 20 20 20 20 clusiveMode;
38f0: 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 /* Boolea
3900: 6e 2e 20 54 72 75 65 20 69 66 20 6c 6f 63 6b 69 n. True if locki
3910: 6e 67 5f 6d 6f 64 65 3d 3d 45 58 43 4c 55 53 49 ng_mode==EXCLUSI
3920: 56 45 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e VE */. u8 journ
3930: 61 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 alMode;
3940: 20 20 20 20 2f 2a 20 4f 6e 20 6f 66 20 74 68 65 /* On of the
3950: 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f PAGER_JOURNALMO
3960: 44 45 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 DE_* values */.
3970: 20 75 38 20 75 73 65 4a 6f 75 72 6e 61 6c 3b 20 u8 useJournal;
3980: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
3990: 55 73 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a Use a rollback j
39a0: 6f 75 72 6e 61 6c 20 6f 6e 20 74 68 69 73 20 66 ournal on this f
39b0: 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6e 6f 52 65 ile */. u8 noRe
39c0: 61 64 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 adlock;
39d0: 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 /* Do not b
39e0: 6f 74 68 65 72 20 74 6f 20 6f 62 74 61 69 6e 20 other to obtain
39f0: 72 65 61 64 6c 6f 63 6b 73 20 2a 2f 0a 20 20 75 readlocks */. u
3a00: 38 20 6e 6f 53 79 6e 63 3b 20 20 20 20 20 20 20 8 noSync;
3a10: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f /* Do
3a20: 20 6e 6f 74 20 73 79 6e 63 20 74 68 65 20 6a 6f not sync the jo
3a30: 75 72 6e 61 6c 20 69 66 20 74 72 75 65 20 2a 2f urnal if true */
3a40: 0a 20 20 75 38 20 66 75 6c 6c 53 79 6e 63 3b 20 . u8 fullSync;
3a50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
3a60: 2a 20 44 6f 20 65 78 74 72 61 20 73 79 6e 63 73 * Do extra syncs
3a70: 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
3a80: 66 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 20 2a for robustness *
3a90: 2f 0a 20 20 75 38 20 73 79 6e 63 5f 66 6c 61 67 /. u8 sync_flag
3aa0: 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
3ab0: 2f 2a 20 4f 6e 65 20 6f 66 20 53 59 4e 43 5f 4e /* One of SYNC_N
3ac0: 4f 52 4d 41 4c 20 6f 72 20 53 59 4e 43 5f 46 55 ORMAL or SYNC_FU
3ad0: 4c 4c 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70 46 LL */. u8 tempF
3ae0: 69 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ile;
3af0: 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 /* zFilename
3b00: 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 is a temporary
3b10: 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 72 65 61 file */. u8 rea
3b20: 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 dOnly;
3b30: 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f /* True fo
3b40: 72 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 r a read-only da
3b50: 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 6d tabase */. u8 m
3b60: 65 6d 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 emDb;
3b70: 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
3b80: 74 6f 20 69 6e 68 69 62 69 74 20 61 6c 6c 20 66 to inhibit all f
3b90: 69 6c 65 20 49 2f 4f 20 2a 2f 0a 0a 20 20 2f 2a ile I/O */.. /*
3ba0: 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 The following b
3bb0: 6c 6f 63 6b 20 63 6f 6e 74 61 69 6e 73 20 74 68 lock contains th
3bc0: 6f 73 65 20 63 6c 61 73 73 20 6d 65 6d 62 65 72 ose class member
3bd0: 73 20 74 68 61 74 20 61 72 65 20 64 79 6e 61 6d s that are dynam
3be0: 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 6d 6f 64 69 ically. ** modi
3bf0: 66 69 65 64 20 64 75 72 69 6e 67 20 6e 6f 72 6d fied during norm
3c00: 61 6c 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 54 al operations. T
3c10: 68 65 20 6f 74 68 65 72 20 76 61 72 69 61 62 6c he other variabl
3c20: 65 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 es in this struc
3c30: 74 75 72 65 0a 20 20 2a 2a 20 61 72 65 20 65 69 ture. ** are ei
3c40: 74 68 65 72 20 63 6f 6e 73 74 61 6e 74 20 74 68 ther constant th
3c50: 72 6f 75 67 68 6f 75 74 20 74 68 65 20 6c 69 66 roughout the lif
3c60: 65 74 69 6d 65 20 6f 66 20 74 68 65 20 70 61 67 etime of the pag
3c70: 65 72 2c 20 6f 72 20 65 6c 73 65 0a 20 20 2a 2a er, or else. **
3c80: 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 63 used to store c
3c90: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 70 61 72 onfiguration par
3ca0: 61 6d 65 74 65 72 73 20 74 68 61 74 20 61 66 66 ameters that aff
3cb0: 65 63 74 20 74 68 65 20 77 61 79 20 74 68 65 20 ect the way the
3cc0: 70 61 67 65 72 20 0a 20 20 2a 2a 20 6f 70 65 72 pager . ** oper
3cd0: 61 74 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 ates.. **. **
3ce0: 54 68 65 20 27 73 74 61 74 65 27 20 76 61 72 69 The 'state' vari
3cf0: 61 62 6c 65 20 69 73 20 64 65 73 63 72 69 62 65 able is describe
3d00: 64 20 69 6e 20 6d 6f 72 65 20 64 65 74 61 69 6c d in more detail
3d10: 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 0a along with the.
3d20: 20 20 2a 2a 20 64 65 73 63 72 69 70 74 69 6f 6e ** description
3d30: 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 s of the values
3d40: 69 74 20 6d 61 79 20 74 61 6b 65 20 2d 20 50 41 it may take - PA
3d50: 47 45 52 5f 55 4e 4c 4f 43 4b 20 65 74 63 2e 20 GER_UNLOCK etc.
3d60: 4d 61 6e 79 20 6f 66 20 74 68 65 0a 20 20 2a 2a Many of the. **
3d70: 20 6f 74 68 65 72 20 76 61 72 69 61 62 6c 65 73 other variables
3d80: 20 69 6e 20 74 68 69 73 20 62 6c 6f 63 6b 20 61 in this block a
3d90: 72 65 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 re described in
3da0: 74 68 65 20 63 6f 6d 6d 65 6e 74 20 64 69 72 65 the comment dire
3db0: 63 74 6c 79 20 0a 20 20 2a 2a 20 61 62 6f 76 65 ctly . ** above
3dc0: 20 74 68 69 73 20 63 6c 61 73 73 20 64 65 66 69 this class defi
3dd0: 6e 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 75 nition.. */. u
3de0: 38 20 73 74 61 74 65 3b 20 20 20 20 20 20 20 20 8 state;
3df0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 41 /* PA
3e00: 47 45 52 5f 55 4e 4c 4f 43 4b 2c 20 5f 53 48 41 GER_UNLOCK, _SHA
3e10: 52 45 44 2c 20 5f 52 45 53 45 52 56 45 44 2c 20 RED, _RESERVED,
3e20: 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 64 62 4d etc. */. u8 dbM
3e30: 6f 64 69 66 69 65 64 3b 20 20 20 20 20 20 20 20 odified;
3e40: 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
3e50: 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 63 there are any c
3e60: 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 44 62 hanges to the Db
3e70: 20 2a 2f 0a 20 20 75 38 20 6e 65 65 64 53 79 6e */. u8 needSyn
3e80: 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
3e90: 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 /* True if an
3ea0: 66 73 79 6e 63 28 29 20 69 73 20 6e 65 65 64 65 fsync() is neede
3eb0: 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c d on the journal
3ec0: 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c */. u8 journal
3ed0: 53 74 61 72 74 65 64 3b 20 20 20 20 20 20 20 20 Started;
3ee0: 20 20 2f 2a 20 54 72 75 65 20 69 66 20 68 65 61 /* True if hea
3ef0: 64 65 72 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 69 der of journal i
3f00: 73 20 73 79 6e 63 65 64 20 2a 2f 0a 20 20 75 38 s synced */. u8
3f10: 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 changeCountDone
3f20: 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 ; /* Set
3f30: 20 61 66 74 65 72 20 69 6e 63 72 65 6d 65 6e 74 after increment
3f40: 69 6e 67 20 74 68 65 20 63 68 61 6e 67 65 2d 63 ing the change-c
3f50: 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 73 ounter */. u8 s
3f60: 65 74 4d 61 73 74 65 72 3b 20 20 20 20 20 20 20 etMaster;
3f70: 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
3f80: 69 66 20 61 20 6d 2d 6a 20 6e 61 6d 65 20 68 61 if a m-j name ha
3f90: 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 s been written t
3fa0: 6f 20 6a 72 6e 6c 20 2a 2f 0a 20 20 75 38 20 64 o jrnl */. u8 d
3fb0: 6f 4e 6f 74 53 79 6e 63 3b 20 20 20 20 20 20 20 oNotSync;
3fc0: 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 /* Boole
3fd0: 61 6e 2e 20 57 68 69 6c 65 20 74 72 75 65 2c 20 an. While true,
3fe0: 64 6f 20 6e 6f 74 20 73 70 69 6c 6c 20 74 68 65 do not spill the
3ff0: 20 63 61 63 68 65 20 2a 2f 0a 20 20 75 38 20 64 cache */. u8 d
4000: 62 53 69 7a 65 56 61 6c 69 64 3b 20 20 20 20 20 bSizeValid;
4010: 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 77 /* Set w
4020: 68 65 6e 20 64 62 53 69 7a 65 20 69 73 20 63 6f hen dbSize is co
4030: 72 72 65 63 74 20 2a 2f 0a 20 20 75 38 20 73 75 rrect */. u8 su
4040: 62 6a 49 6e 4d 65 6d 6f 72 79 3b 20 20 20 20 20 bjInMemory;
4050: 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 /* True t
4060: 6f 20 75 73 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 o use in-memory
4070: 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a sub-journals */.
4080: 20 20 50 67 6e 6f 20 64 62 53 69 7a 65 3b 20 20 Pgno dbSize;
4090: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
40a0: 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
40b0: 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
40c0: 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 4f 72 69 */. Pgno dbOri
40d0: 67 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 gSize;
40e0: 20 20 2f 2a 20 64 62 53 69 7a 65 20 62 65 66 6f /* dbSize befo
40f0: 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 re the current t
4100: 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 ransaction */.
4110: 50 67 6e 6f 20 64 62 46 69 6c 65 53 69 7a 65 3b Pgno dbFileSize;
4120: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
4130: 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
4140: 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
4150: 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 ile */. int err
4160: 43 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 Code;
4170: 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 73 /* One of s
4180: 65 76 65 72 61 6c 20 6b 69 6e 64 73 20 6f 66 20 everal kinds of
4190: 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 errors */. int
41a0: 6e 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 nRec;
41b0: 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 73 /* Pages
41c0: 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 73 69 6e 63 journalled sinc
41d0: 65 20 6c 61 73 74 20 6a 2d 68 65 61 64 65 72 20 e last j-header
41e0: 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 75 33 32 written */. u32
41f0: 20 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20 cksumInit;
4200: 20 20 20 20 20 20 20 20 20 2f 2a 20 51 75 61 73 /* Quas
4210: 69 2d 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 61 i-random value a
4220: 64 64 65 64 20 74 6f 20 65 76 65 72 79 20 63 68 dded to every ch
4230: 65 63 6b 73 75 6d 20 2a 2f 0a 20 20 75 33 32 20 ecksum */. u32
4240: 6e 53 75 62 52 65 63 3b 20 20 20 20 20 20 20 20 nSubRec;
4250: 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
4260: 72 20 6f 66 20 72 65 63 6f 72 64 73 20 77 72 69 r of records wri
4270: 74 74 65 6e 20 74 6f 20 73 75 62 2d 6a 6f 75 72 tten to sub-jour
4280: 6e 61 6c 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 nal */. Bitvec
4290: 2a 70 49 6e 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 *pInJournal;
42a0: 20 20 20 20 20 2f 2a 20 4f 6e 65 20 62 69 74 20 /* One bit
42b0: 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 69 6e for each page in
42c0: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
42d0: 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f le */. sqlite3_
42e0: 66 69 6c 65 20 2a 66 64 3b 20 20 20 20 20 20 20 file *fd;
42f0: 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 /* File desc
4300: 72 69 70 74 6f 72 20 66 6f 72 20 64 61 74 61 62 riptor for datab
4310: 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ase */. sqlite3
4320: 5f 66 69 6c 65 20 2a 6a 66 64 3b 20 20 20 20 20 _file *jfd;
4330: 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 /* File des
4340: 63 72 69 70 74 6f 72 20 66 6f 72 20 6d 61 69 6e criptor for main
4350: 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 73 71 journal */. sq
4360: 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 6a 66 64 lite3_file *sjfd
4370: 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c ; /* Fil
4380: 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 e descriptor for
4390: 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a sub-journal */.
43a0: 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 4f 66 66 i64 journalOff
43b0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
43c0: 20 43 75 72 72 65 6e 74 20 77 72 69 74 65 20 6f Current write o
43d0: 66 66 73 65 74 20 69 6e 20 74 68 65 20 6a 6f 75 ffset in the jou
43e0: 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 rnal file */. i
43f0: 36 34 20 6a 6f 75 72 6e 61 6c 48 64 72 3b 20 20 64 journalHdr;
4400: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 /* By
4410: 74 65 20 6f 66 66 73 65 74 20 74 6f 20 70 72 65 te offset to pre
4420: 76 69 6f 75 73 20 6a 6f 75 72 6e 61 6c 20 68 65 vious journal he
4430: 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20 6a 6f ader */. i64 jo
4440: 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 urnalSizeLimit;
4450: 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6c 69 /* Size li
4460: 6d 69 74 20 66 6f 72 20 70 65 72 73 69 73 74 65 mit for persiste
4470: 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 nt journal files
4480: 20 2a 2f 0a 20 20 50 61 67 65 72 53 61 76 65 70 */. PagerSavep
4490: 6f 69 6e 74 20 2a 61 53 61 76 65 70 6f 69 6e 74 oint *aSavepoint
44a0: 3b 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 61 63 ; /* Array of ac
44b0: 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 tive savepoints
44c0: 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 6f */. int nSavepo
44d0: 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 int;
44e0: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c /* Number of el
44f0: 65 6d 65 6e 74 73 20 69 6e 20 61 53 61 76 65 70 ements in aSavep
4500: 6f 69 6e 74 5b 5d 20 2a 2f 0a 20 20 63 68 61 72 oint[] */. char
4510: 20 64 62 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b dbFileVers[16];
4520: 20 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 /* Chang
4530: 65 73 20 77 68 65 6e 65 76 65 72 20 64 61 74 61 es whenever data
4540: 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 base file change
4550: 73 20 2a 2f 0a 20 20 75 33 32 20 73 65 63 74 6f s */. u32 secto
4560: 72 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 rSize;
4570: 20 20 20 2f 2a 20 41 73 73 75 6d 65 64 20 73 65 /* Assumed se
4580: 63 74 6f 72 20 73 69 7a 65 20 64 75 72 69 6e 67 ctor size during
4590: 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20 rollback */..
45a0: 75 31 36 20 6e 45 78 74 72 61 3b 20 20 20 20 20 u16 nExtra;
45b0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
45c0: 64 64 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 dd this many byt
45d0: 65 73 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 es to each in-me
45e0: 6d 6f 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 mory page */. i
45f0: 31 36 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20 16 nReserve;
4600: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
4610: 6d 62 65 72 20 6f 66 20 75 6e 75 73 65 64 20 62 mber of unused b
4620: 79 74 65 73 20 61 74 20 65 6e 64 20 6f 66 20 65 ytes at end of e
4630: 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 33 ach page */. u3
4640: 32 20 76 66 73 46 6c 61 67 73 3b 20 20 20 20 20 2 vfsFlags;
4650: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 /* Fla
4660: 67 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 76 gs for sqlite3_v
4670: 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 fs.xOpen() */.
4680: 69 6e 74 20 70 61 67 65 53 69 7a 65 3b 20 20 20 int pageSize;
4690: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
46a0: 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
46b0: 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 n a page */. Pg
46c0: 6e 6f 20 6d 78 50 67 6e 6f 3b 20 20 20 20 20 20 no mxPgno;
46d0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 /* Max
46e0: 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 69 7a imum allowed siz
46f0: 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
4700: 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69 e */. char *zFi
4710: 6c 65 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 lename;
4720: 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
4730: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
4740: 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 */. char *zJour
4750: 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 nal;
4760: 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
4770: 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a journal file */.
4780: 20 20 69 6e 74 20 28 2a 78 42 75 73 79 48 61 6e int (*xBusyHan
4790: 64 6c 65 72 29 28 76 6f 69 64 2a 29 3b 20 2f 2a dler)(void*); /*
47a0: 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c Function to cal
47b0: 6c 20 77 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 l when busy */.
47c0: 20 76 6f 69 64 20 2a 70 42 75 73 79 48 61 6e 64 void *pBusyHand
47d0: 6c 65 72 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 lerArg; /*
47e0: 43 6f 6e 74 65 78 74 20 61 72 67 75 6d 65 6e 74 Context argument
47f0: 20 66 6f 72 20 78 42 75 73 79 48 61 6e 64 6c 65 for xBusyHandle
4800: 72 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 r */.#ifdef SQLI
4810: 54 45 5f 54 45 53 54 0a 20 20 69 6e 74 20 6e 48 TE_TEST. int nH
4820: 69 74 2c 20 6e 4d 69 73 73 3b 20 20 20 20 20 20 it, nMiss;
4830: 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 68 /* Cache h
4840: 69 74 73 20 61 6e 64 20 6d 69 73 73 69 6e 67 20 its and missing
4850: 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 61 64 2c 20 */. int nRead,
4860: 6e 57 72 69 74 65 3b 20 20 20 20 20 20 20 20 20 nWrite;
4870: 20 2f 2a 20 44 61 74 61 62 61 73 65 20 70 61 67 /* Database pag
4880: 65 73 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20 es read/written
4890: 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 */.#endif. void
48a0: 20 28 2a 78 52 65 69 6e 69 74 65 72 29 28 44 62 (*xReiniter)(Db
48b0: 50 61 67 65 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20 Page*); /* Call
48c0: 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 this routine whe
48d0: 6e 20 72 65 6c 6f 61 64 69 6e 67 20 70 61 67 65 n reloading page
48e0: 73 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 s */.#ifdef SQLI
48f0: 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 76 TE_HAS_CODEC. v
4900: 6f 69 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 oid *(*xCodec)(v
4910: 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c oid*,void*,Pgno,
4920: 69 6e 74 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 int); /* Routine
4930: 20 66 6f 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67 for en/decoding
4940: 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 data */. void
4950: 28 2a 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 (*xCodecSizeChng
4960: 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29 )(void*,int,int)
4970: 3b 20 2f 2a 20 4e 6f 74 69 66 79 20 6f 66 20 70 ; /* Notify of p
4980: 61 67 65 20 73 69 7a 65 20 63 68 61 6e 67 65 73 age size changes
4990: 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f */. void (*xCo
49a0: 64 65 63 46 72 65 65 29 28 76 6f 69 64 2a 29 3b decFree)(void*);
49b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
49c0: 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 Destructor for t
49d0: 68 65 20 63 6f 64 65 63 20 2a 2f 0a 20 20 76 6f he codec */. vo
49e0: 69 64 20 2a 70 43 6f 64 65 63 3b 20 20 20 20 20 id *pCodec;
49f0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
4a00: 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 st argument to x
4a10: 43 6f 64 65 63 2e 2e 2e 20 6d 65 74 68 6f 64 73 Codec... methods
4a20: 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 68 61 */.#endif. cha
4a30: 72 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 20 r *pTmpSpace;
4a40: 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
4a50: 72 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 r.pageSize bytes
4a60: 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74 6d of space for tm
4a70: 70 20 75 73 65 20 2a 2f 0a 20 20 50 43 61 63 68 p use */. PCach
4a80: 65 20 2a 70 50 43 61 63 68 65 3b 20 20 20 20 20 e *pPCache;
4a90: 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
4aa0: 72 20 74 6f 20 70 61 67 65 20 63 61 63 68 65 20 r to page cache
4ab0: 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 object */. sqli
4ac0: 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 te3_backup *pBac
4ad0: 6b 75 70 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 kup; /* Point
4ae0: 65 72 20 74 6f 20 6c 69 73 74 20 6f 66 20 6f 6e er to list of on
4af0: 67 6f 69 6e 67 20 62 61 63 6b 75 70 20 70 72 6f going backup pro
4b00: 63 65 73 73 65 73 20 2a 2f 0a 23 69 66 6e 64 65 cesses */.#ifnde
4b10: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 f SQLITE_OMIT_WA
4b20: 4c 0a 20 20 57 61 6c 20 2a 70 57 61 6c 3b 20 20 L. Wal *pWal;
4b30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4b40: 2f 2a 20 57 72 69 74 65 2d 61 68 65 61 64 20 6c /* Write-ahead l
4b50: 6f 67 20 75 73 65 64 20 62 79 20 22 6a 6f 75 72 og used by "jour
4b60: 6e 61 6c 5f 6d 6f 64 65 3d 77 61 6c 22 20 2a 2f nal_mode=wal" */
4b70: 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a .#endif.};../*.*
4b80: 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
4b90: 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 global variables
4ba0: 20 68 6f 6c 64 20 63 6f 75 6e 74 65 72 73 20 75 hold counters u
4bb0: 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 sed for.** testi
4bc0: 6e 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 ng purposes only
4bd0: 2e 20 20 54 68 65 73 65 20 76 61 72 69 61 62 6c . These variabl
4be0: 65 73 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 es do not exist
4bf0: 69 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 65 73 74 in.** a non-test
4c00: 69 6e 67 20 62 75 69 6c 64 2e 20 20 54 68 65 73 ing build. Thes
4c10: 65 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 e variables are
4c20: 6e 6f 74 20 74 68 72 65 61 64 2d 73 61 66 65 2e not thread-safe.
4c30: 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
4c40: 45 5f 54 45 53 54 0a 69 6e 74 20 73 71 6c 69 74 E_TEST.int sqlit
4c50: 65 33 5f 70 61 67 65 72 5f 72 65 61 64 64 62 5f e3_pager_readdb_
4c60: 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a count = 0; /*
4c70: 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 Number of full
4c80: 70 61 67 65 73 20 72 65 61 64 20 66 72 6f 6d 20 pages read from
4c90: 44 42 20 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 DB */.int sqlite
4ca0: 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 3_pager_writedb_
4cb0: 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20 count = 0; /*
4cc0: 4e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 Number of full p
4cd0: 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 ages written to
4ce0: 44 42 20 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 DB */.int sqlite
4cf0: 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f 63 3_pager_writej_c
4d00: 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 ount = 0; /*
4d10: 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
4d20: 77 72 69 74 74 65 6e 20 74 6f 20 6a 6f 75 72 6e written to journ
4d30: 61 6c 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50 al */.# define P
4d40: 41 47 45 52 5f 49 4e 43 52 28 76 29 20 20 76 2b AGER_INCR(v) v+
4d50: 2b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 +.#else.# define
4d60: 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29 0a 23 PAGER_INCR(v).#
4d70: 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4a endif..../*.** J
4d80: 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 62 65 67 ournal files beg
4d90: 69 6e 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c in with the foll
4da0: 6f 77 69 6e 67 20 6d 61 67 69 63 20 73 74 72 69 owing magic stri
4db0: 6e 67 2e 20 20 54 68 65 20 64 61 74 61 0a 2a 2a ng. The data.**
4dc0: 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 was obtained fr
4dd0: 6f 6d 20 2f 64 65 76 2f 72 61 6e 64 6f 6d 2e 20 om /dev/random.
4de0: 20 49 74 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 It is used only
4df0: 20 61 73 20 61 20 73 61 6e 69 74 79 20 63 68 65 as a sanity che
4e00: 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 ck..**.** Since
4e10: 76 65 72 73 69 6f 6e 20 32 2e 38 2e 30 2c 20 74 version 2.8.0, t
4e20: 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 he journal forma
4e30: 74 20 63 6f 6e 74 61 69 6e 73 20 61 64 64 69 74 t contains addit
4e40: 69 6f 6e 61 6c 20 73 61 6e 69 74 79 0a 2a 2a 20 ional sanity.**
4e50: 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 checking informa
4e60: 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 70 6f tion. If the po
4e70: 77 65 72 20 66 61 69 6c 73 20 77 68 69 6c 65 20 wer fails while
4e80: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 the journal is b
4e90: 65 69 6e 67 0a 2a 2a 20 77 72 69 74 74 65 6e 2c eing.** written,
4ea0: 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 20 67 61 72 semi-random gar
4eb0: 62 61 67 65 20 64 61 74 61 20 6d 69 67 68 74 20 bage data might
4ec0: 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 6a 6f appear in the jo
4ed0: 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 61 66 urnal.** file af
4ee0: 74 65 72 20 70 6f 77 65 72 20 69 73 20 72 65 73 ter power is res
4ef0: 74 6f 72 65 64 2e 20 20 49 66 20 61 6e 20 61 74 tored. If an at
4f00: 74 65 6d 70 74 20 69 73 20 74 68 65 6e 20 6d 61 tempt is then ma
4f10: 64 65 0a 2a 2a 20 74 6f 20 72 6f 6c 6c 20 74 68 de.** to roll th
4f20: 65 20 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2c 20 e journal back,
4f30: 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 75 the database cou
4f40: 6c 64 20 62 65 20 63 6f 72 72 75 70 74 65 64 2e ld be corrupted.
4f50: 20 20 54 68 65 20 61 64 64 69 74 69 6f 6e 61 6c The additional
4f60: 0a 2a 2a 20 73 61 6e 69 74 79 20 63 68 65 63 6b .** sanity check
4f70: 69 6e 67 20 64 61 74 61 20 69 73 20 61 6e 20 61 ing data is an a
4f80: 74 74 65 6d 70 74 20 74 6f 20 64 69 73 63 6f 76 ttempt to discov
4f90: 65 72 20 74 68 65 20 67 61 72 62 61 67 65 20 69 er the garbage i
4fa0: 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c n the.** journal
4fb0: 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a and ignore it..
4fc0: 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6e 69 74 79 **.** The sanity
4fd0: 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d checking inform
4fe0: 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6e 65 ation for the ne
4ff0: 77 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 w journal format
5000: 20 63 6f 6e 73 69 73 74 73 0a 2a 2a 20 6f 66 20 consists.** of
5010: 61 20 33 32 2d 62 69 74 20 63 68 65 63 6b 73 75 a 32-bit checksu
5020: 6d 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 6f m on each page o
5030: 66 20 64 61 74 61 2e 20 20 54 68 65 20 63 68 65 f data. The che
5040: 63 6b 73 75 6d 20 63 6f 76 65 72 73 20 62 6f 74 cksum covers bot
5050: 68 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e 75 h.** the page nu
5060: 6d 62 65 72 20 61 6e 64 20 74 68 65 20 70 50 61 mber and the pPa
5070: 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 ger->pageSize by
5080: 74 65 73 20 6f 66 20 64 61 74 61 20 66 6f 72 20 tes of data for
5090: 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20 54 68 69 the page..** Thi
50a0: 73 20 63 6b 73 75 6d 20 69 73 20 69 6e 69 74 69 s cksum is initi
50b0: 61 6c 69 7a 65 64 20 74 6f 20 61 20 33 32 2d 62 alized to a 32-b
50c0: 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 it random value
50d0: 74 68 61 74 20 61 70 70 65 61 72 73 20 69 6e 20 that appears in
50e0: 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 the.** journal f
50f0: 69 6c 65 20 72 69 67 68 74 20 61 66 74 65 72 20 ile right after
5100: 74 68 65 20 68 65 61 64 65 72 2e 20 20 54 68 65 the header. The
5110: 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c 69 random initiali
5120: 7a 65 72 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 zer is important
5130: 2c 0a 2a 2a 20 62 65 63 61 75 73 65 20 67 61 72 ,.** because gar
5140: 62 61 67 65 20 64 61 74 61 20 74 68 61 74 20 61 bage data that a
5150: 70 70 65 61 72 73 20 61 74 20 74 68 65 20 65 6e ppears at the en
5160: 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 69 d of a journal i
5170: 73 20 6c 69 6b 65 6c 79 0a 2a 2a 20 64 61 74 61 s likely.** data
5180: 20 74 68 61 74 20 77 61 73 20 6f 6e 63 65 20 69 that was once i
5190: 6e 20 6f 74 68 65 72 20 66 69 6c 65 73 20 74 68 n other files th
51a0: 61 74 20 68 61 76 65 20 6e 6f 77 20 62 65 65 6e at have now been
51b0: 20 64 65 6c 65 74 65 64 2e 20 20 49 66 20 74 68 deleted. If th
51c0: 65 0a 2a 2a 20 67 61 72 62 61 67 65 20 64 61 74 e.** garbage dat
51d0: 61 20 63 61 6d 65 20 66 72 6f 6d 20 61 6e 20 6f a came from an o
51e0: 62 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 bsolete journal
51f0: 66 69 6c 65 2c 20 74 68 65 20 63 68 65 63 6b 73 file, the checks
5200: 75 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20 ums might.** be
5210: 63 6f 72 72 65 63 74 2e 20 20 42 75 74 20 62 79 correct. But by
5220: 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 initializing th
5230: 65 20 63 68 65 63 6b 73 75 6d 20 74 6f 20 72 61 e checksum to ra
5240: 6e 64 6f 6d 20 76 61 6c 75 65 20 77 68 69 63 68 ndom value which
5250: 0a 2a 2a 20 69 73 20 64 69 66 66 65 72 65 6e 74 .** is different
5260: 20 66 6f 72 20 65 76 65 72 79 20 6a 6f 75 72 6e for every journ
5270: 61 6c 2c 20 77 65 20 6d 69 6e 69 6d 69 7a 65 20 al, we minimize
5280: 74 68 61 74 20 72 69 73 6b 2e 0a 2a 2f 0a 73 74 that risk..*/.st
5290: 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 atic const unsig
52a0: 6e 65 64 20 63 68 61 72 20 61 4a 6f 75 72 6e 61 ned char aJourna
52b0: 6c 4d 61 67 69 63 5b 5d 20 3d 20 7b 0a 20 20 30 lMagic[] = {. 0
52c0: 78 64 39 2c 20 30 78 64 35 2c 20 30 78 30 35 2c xd9, 0xd5, 0x05,
52d0: 20 30 78 66 39 2c 20 30 78 32 30 2c 20 30 78 61 0xf9, 0x20, 0xa
52e0: 31 2c 20 30 78 36 33 2c 20 30 78 64 37 2c 0a 7d 1, 0x63, 0xd7,.}
52f0: 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 69 7a ;../*.** The siz
5300: 65 20 6f 66 20 74 68 65 20 6f 66 20 65 61 63 68 e of the of each
5310: 20 70 61 67 65 20 72 65 63 6f 72 64 20 69 6e 20 page record in
5320: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 67 the journal is g
5330: 69 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 iven by.** the f
5340: 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 2e 0a ollowing macro..
5350: 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e */.#define JOURN
5360: 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 AL_PG_SZ(pPager)
5370: 20 20 28 28 70 50 61 67 65 72 2d 3e 70 61 67 65 ((pPager->page
5380: 53 69 7a 65 29 20 2b 20 38 29 0a 0a 2f 2a 0a 2a Size) + 8)../*.*
5390: 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 * The journal he
53a0: 61 64 65 72 20 73 69 7a 65 20 66 6f 72 20 74 68 ader size for th
53b0: 69 73 20 70 61 67 65 72 2e 20 54 68 69 73 20 69 is pager. This i
53c0: 73 20 75 73 75 61 6c 6c 79 20 74 68 65 20 73 61 s usually the sa
53d0: 6d 65 20 0a 2a 2a 20 73 69 7a 65 20 61 73 20 61 me .** size as a
53e0: 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 single disk sec
53f0: 74 6f 72 2e 20 53 65 65 20 61 6c 73 6f 20 73 65 tor. See also se
5400: 74 53 65 63 74 6f 72 53 69 7a 65 28 29 2e 0a 2a tSectorSize()..*
5410: 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 /.#define JOURNA
5420: 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
5430: 20 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 (pPager->sector
5440: 53 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 Size)../*.** The
5450: 20 6d 61 63 72 6f 20 4d 45 4d 44 42 20 69 73 20 macro MEMDB is
5460: 74 72 75 65 20 69 66 20 77 65 20 61 72 65 20 64 true if we are d
5470: 65 61 6c 69 6e 67 20 77 69 74 68 20 61 6e 20 69 ealing with an i
5480: 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
5490: 65 2e 0a 2a 2a 20 57 65 20 64 6f 20 74 68 69 73 e..** We do this
54a0: 20 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20 74 as a macro so t
54b0: 68 61 74 20 69 66 20 74 68 65 20 53 51 4c 49 54 hat if the SQLIT
54c0: 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 E_OMIT_MEMORYDB
54d0: 6d 61 63 72 6f 20 69 73 20 73 65 74 2c 0a 2a 2a macro is set,.**
54e0: 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 4d 45 the value of ME
54f0: 4d 44 42 20 77 69 6c 6c 20 62 65 20 61 20 63 6f MDB will be a co
5500: 6e 73 74 61 6e 74 20 61 6e 64 20 74 68 65 20 63 nstant and the c
5510: 6f 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6f 70 74 ompiler will opt
5520: 69 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20 63 6f 64 imize.** out cod
5530: 65 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 65 76 e that would nev
5540: 65 72 20 65 78 65 63 75 74 65 2e 0a 2a 2f 0a 23 er execute..*/.#
5550: 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
5560: 54 5f 4d 45 4d 4f 52 59 44 42 0a 23 20 64 65 66 T_MEMORYDB.# def
5570: 69 6e 65 20 4d 45 4d 44 42 20 30 0a 23 65 6c 73 ine MEMDB 0.#els
5580: 65 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44 42 e.# define MEMDB
5590: 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 0a 23 pPager->memDb.#
55a0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
55b0: 20 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c 20 70 maximum legal p
55c0: 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 28 32 age number is (2
55d0: 5e 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65 ^31 - 1)..*/.#de
55e0: 66 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 5f 50 fine PAGER_MAX_P
55f0: 47 4e 4f 20 32 31 34 37 34 38 33 36 34 37 0a 0a GNO 2147483647..
5600: 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 20 0a #ifndef NDEBUG .
5610: 2f 2a 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a /*.** Usage:.**.
5620: 2a 2a 20 20 20 61 73 73 65 72 74 28 20 61 73 73 ** assert( ass
5630: 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 ert_pager_state(
5640: 70 50 61 67 65 72 29 20 29 3b 0a 2a 2f 0a 73 74 pPager) );.*/.st
5650: 61 74 69 63 20 69 6e 74 20 61 73 73 65 72 74 5f atic int assert_
5660: 70 61 67 65 72 5f 73 74 61 74 65 28 50 61 67 65 pager_state(Page
5670: 72 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20 20 2f r *pPager){.. /
5680: 2a 20 41 20 74 65 6d 70 2d 66 69 6c 65 20 69 73 * A temp-file is
5690: 20 61 6c 77 61 79 73 20 69 6e 20 50 41 47 45 52 always in PAGER
56a0: 5f 45 58 43 4c 55 53 49 56 45 20 6f 72 20 50 41 _EXCLUSIVE or PA
56b0: 47 45 52 5f 53 59 4e 43 45 44 20 73 74 61 74 65 GER_SYNCED state
56c0: 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 . */. assert( p
56d0: 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3d Pager->tempFile=
56e0: 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 =0 || pPager->st
56f0: 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 ate>=PAGER_EXCLU
5700: 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 SIVE );.. /* Th
5710: 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e e changeCountDon
5720: 65 20 66 6c 61 67 20 69 73 20 61 6c 77 61 79 73 e flag is always
5730: 20 73 65 74 20 66 6f 72 20 74 65 6d 70 2d 66 69 set for temp-fi
5740: 6c 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 les */. assert(
5750: 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c pPager->tempFil
5760: 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e e==0 || pPager->
5770: 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 changeCountDone
5780: 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a );.. return 1;.
5790: 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
57a0: 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 69 Return true if i
57b0: 74 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 t is necessary t
57c0: 6f 20 77 72 69 74 65 20 70 61 67 65 20 2a 70 50 o write page *pP
57d0: 67 20 69 6e 74 6f 20 74 68 65 20 73 75 62 2d 6a g into the sub-j
57e0: 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20 70 61 67 ournal..** A pag
57f0: 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 77 72 e needs to be wr
5800: 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 73 itten into the s
5810: 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 74 68 ub-journal if th
5820: 65 72 65 20 65 78 69 73 74 73 20 6f 6e 65 0a 2a ere exists one.*
5830: 2a 20 6f 72 20 6d 6f 72 65 20 6f 70 65 6e 20 73 * or more open s
5840: 61 76 65 70 6f 69 6e 74 73 20 66 6f 72 20 77 68 avepoints for wh
5850: 69 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 ich:.**.** * T
5860: 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 he page-number i
5870: 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 s less than or e
5880: 71 75 61 6c 20 74 6f 20 50 61 67 65 72 53 61 76 qual to PagerSav
5890: 65 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c 20 61 6e epoint.nOrig, an
58a0: 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 62 69 74 d.** * The bit
58b0: 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
58c0: 6f 20 74 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 o the page-numbe
58d0: 72 20 69 73 20 6e 6f 74 20 73 65 74 20 69 6e 0a r is not set in.
58e0: 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65 ** PagerSave
58f0: 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 point.pInSavepoi
5900: 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e nt..*/.static in
5910: 74 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 t subjRequiresPa
5920: 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a ge(PgHdr *pPg){.
5930: 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 Pgno pgno = pP
5940: 67 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 g->pgno;. Pager
5950: 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e *pPager = pPg->
5960: 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 69 3b pPager;. int i;
5970: 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 . for(i=0; i<pP
5980: 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 ager->nSavepoint
5990: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 ; i++){. Page
59a0: 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 rSavepoint *p =
59b0: 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f &pPager->aSavepo
59c0: 69 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 int[i];. if(
59d0: 70 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e 6f 20 26 p->nOrig>=pgno &
59e0: 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 69 74 76 & 0==sqlite3Bitv
59f0: 65 63 54 65 73 74 28 70 2d 3e 70 49 6e 53 61 76 ecTest(p->pInSav
5a00: 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 20 29 7b epoint, pgno) ){
5a10: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
5a20: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
5a30: 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
5a40: 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 Return true if t
5a50: 68 65 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 he page is alrea
5a60: 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 dy in the journa
5a70: 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 l file..*/.stati
5a80: 63 20 69 6e 74 20 70 61 67 65 49 6e 4a 6f 75 72 c int pageInJour
5a90: 6e 61 6c 28 50 67 48 64 72 20 2a 70 50 67 29 7b nal(PgHdr *pPg){
5aa0: 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
5ab0: 33 42 69 74 76 65 63 54 65 73 74 28 70 50 67 2d 3BitvecTest(pPg-
5ac0: 3e 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 >pPager->pInJour
5ad0: 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b nal, pPg->pgno);
5ae0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 .}../*.** Read a
5af0: 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 32-bit integer
5b00: 66 72 6f 6d 20 74 68 65 20 67 69 76 65 6e 20 66 from the given f
5b10: 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 ile descriptor.
5b20: 20 53 74 6f 72 65 20 74 68 65 20 69 6e 74 65 67 Store the integ
5b30: 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 72 65 er.** that is re
5b40: 61 64 20 69 6e 20 2a 70 52 65 73 2e 20 20 52 65 ad in *pRes. Re
5b50: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 turn SQLITE_OK i
5b60: 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 f everything wor
5b70: 6b 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a 20 65 72 ked, or an.** er
5b80: 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d 65 ror code is some
5b90: 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 thing goes wrong
5ba0: 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 61 6c 75 ..**.** All valu
5bb0: 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e es are stored on
5bc0: 20 64 69 73 6b 20 61 73 20 62 69 67 2d 65 6e 64 disk as big-end
5bd0: 69 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ian..*/.static i
5be0: 6e 74 20 72 65 61 64 33 32 62 69 74 73 28 73 71 nt read32bits(sq
5bf0: 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 lite3_file *fd,
5c00: 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 i64 offset, u32
5c10: 2a 70 52 65 73 29 7b 0a 20 20 75 6e 73 69 67 6e *pRes){. unsign
5c20: 65 64 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20 ed char ac[4];.
5c30: 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 int rc = sqlite
5c40: 33 4f 73 52 65 61 64 28 66 64 2c 20 61 63 2c 20 3OsRead(fd, ac,
5c50: 73 69 7a 65 6f 66 28 61 63 29 2c 20 6f 66 66 73 sizeof(ac), offs
5c60: 65 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 et);. if( rc==S
5c70: 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
5c80: 2a 70 52 65 73 20 3d 20 73 71 6c 69 74 65 33 47 *pRes = sqlite3G
5c90: 65 74 34 62 79 74 65 28 61 63 29 3b 0a 20 20 7d et4byte(ac);. }
5ca0: 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
5cb0: 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 33 ./*.** Write a 3
5cc0: 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 6e 2-bit integer in
5cd0: 74 6f 20 61 20 73 74 72 69 6e 67 20 62 75 66 66 to a string buff
5ce0: 65 72 20 69 6e 20 62 69 67 2d 65 6e 64 69 61 6e er in big-endian
5cf0: 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a byte order..*/.
5d00: 23 64 65 66 69 6e 65 20 70 75 74 33 32 62 69 74 #define put32bit
5d10: 73 28 41 2c 42 29 20 20 73 71 6c 69 74 65 33 50 s(A,B) sqlite3P
5d20: 75 74 34 62 79 74 65 28 28 75 38 2a 29 41 2c 42 ut4byte((u8*)A,B
5d30: 29 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 )../*.** Write a
5d40: 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 32-bit integer
5d50: 69 6e 74 6f 20 74 68 65 20 67 69 76 65 6e 20 66 into the given f
5d60: 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 ile descriptor.
5d70: 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
5d80: 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 K.** on success
5d90: 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 or an error code
5da0: 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f is something go
5db0: 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 es wrong..*/.sta
5dc0: 74 69 63 20 69 6e 74 20 77 72 69 74 65 33 32 62 tic int write32b
5dd0: 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 its(sqlite3_file
5de0: 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 65 74 *fd, i64 offset
5df0: 2c 20 75 33 32 20 76 61 6c 29 7b 0a 20 20 63 68 , u32 val){. ch
5e00: 61 72 20 61 63 5b 34 5d 3b 0a 20 20 70 75 74 33 ar ac[4];. put3
5e10: 32 62 69 74 73 28 61 63 2c 20 76 61 6c 29 3b 0a 2bits(ac, val);.
5e20: 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
5e30: 4f 73 57 72 69 74 65 28 66 64 2c 20 61 63 2c 20 OsWrite(fd, ac,
5e40: 34 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 0a 2f 4, offset);.}../
5e50: 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e *.** The argumen
5e60: 74 20 74 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 t to this macro
5e70: 69 73 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 is a file descri
5e80: 70 74 6f 72 20 28 74 79 70 65 20 73 71 6c 69 74 ptor (type sqlit
5e90: 65 33 5f 66 69 6c 65 2a 29 2e 0a 2a 2a 20 52 65 e3_file*)..** Re
5ea0: 74 75 72 6e 20 30 20 69 66 20 69 74 20 69 73 20 turn 0 if it is
5eb0: 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 6e 6f 6e not open, or non
5ec0: 2d 7a 65 72 6f 20 28 62 75 74 20 6e 6f 74 20 31 -zero (but not 1
5ed0: 29 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a ) if it is..**.*
5ee0: 2a 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61 * This is so tha
5ef0: 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61 t expressions ca
5f00: 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 73 3a n be written as:
5f10: 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 69 73 4f .**.** if( isO
5f20: 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
5f30: 20 29 7b 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 69 6e ){ ....**.** in
5f40: 73 74 65 61 64 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 stead of.**.**
5f50: 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 66 64 if( pPager->jfd
5f60: 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 20 2e 2e ->pMethods ){ ..
5f70: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69 73 4f ..*/.#define isO
5f80: 70 65 6e 28 70 46 64 29 20 28 28 70 46 64 29 2d pen(pFd) ((pFd)-
5f90: 3e 70 4d 65 74 68 6f 64 73 29 0a 0a 2f 2a 0a 2a >pMethods)../*.*
5fa0: 2a 20 49 66 20 66 69 6c 65 20 70 46 64 20 69 73 * If file pFd is
5fb0: 20 6f 70 65 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 open, call sqli
5fc0: 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 20 6f 6e te3OsUnlock() on
5fd0: 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 it..*/.static i
5fe0: 6e 74 20 6f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 nt osUnlock(sqli
5ff0: 74 65 33 5f 66 69 6c 65 20 2a 70 46 64 2c 20 69 te3_file *pFd, i
6000: 6e 74 20 65 4c 6f 63 6b 29 7b 0a 20 20 69 66 28 nt eLock){. if(
6010: 20 21 69 73 4f 70 65 6e 28 70 46 64 29 20 29 7b !isOpen(pFd) ){
6020: 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
6030: 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 TE_OK;. }. ret
6040: 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c urn sqlite3OsUnl
6050: 6f 63 6b 28 70 46 64 2c 20 65 4c 6f 63 6b 29 3b ock(pFd, eLock);
6060: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
6070: 75 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d 69 6e unction determin
6080: 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f es whether or no
6090: 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 t the atomic-wri
60a0: 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a te optimization.
60b0: 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 ** can be used w
60c0: 69 74 68 20 74 68 69 73 20 70 61 67 65 72 2e 20 ith this pager.
60d0: 54 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e The optimization
60e0: 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 66 3a can be used if:
60f0: 0a 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68 65 20 .**.** (a) the
6100: 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 value returned b
6110: 79 20 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 y OsDeviceCharac
6120: 74 65 72 69 73 74 69 63 73 28 29 20 69 6e 64 69 teristics() indi
6130: 63 61 74 65 73 20 74 68 61 74 0a 2a 2a 20 20 20 cates that.**
6140: 20 20 20 61 20 64 61 74 61 62 61 73 65 20 70 61 a database pa
6150: 67 65 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 ge may be writte
6160: 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20 61 6e n atomically, an
6170: 64 0a 2a 2a 20 20 28 62 29 20 74 68 65 20 76 61 d.** (b) the va
6180: 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 lue returned by
6190: 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 20 69 OsSectorSize() i
61a0: 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 s less than or e
61b0: 71 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74 6f 20 qual.** to
61c0: 74 68 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a the page size..*
61d0: 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a *.** The optimiz
61e0: 61 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 61 6c ation is also al
61f0: 77 61 79 73 20 65 6e 61 62 6c 65 64 20 66 6f 72 ways enabled for
6200: 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 temporary files
6210: 2e 20 49 74 20 69 73 0a 2a 2a 20 61 6e 20 65 72 . It is.** an er
6220: 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 ror to call this
6230: 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 70 50 61 function if pPa
6240: 67 65 72 20 69 73 20 6f 70 65 6e 65 64 20 6f 6e ger is opened on
6250: 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a an in-memory.**
6260: 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a database..**.**
6270: 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 If the optimiza
6280: 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 75 tion cannot be u
6290: 73 65 64 2c 20 30 20 69 73 20 72 65 74 75 72 6e sed, 0 is return
62a0: 65 64 2e 20 49 66 20 69 74 20 63 61 6e 20 62 65 ed. If it can be
62b0: 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 used,.** then t
62c0: 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
62d0: 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 d is the size of
62e0: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
62f0: 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 63 6f 6e e when it.** con
6300: 74 61 69 6e 73 20 72 6f 6c 6c 62 61 63 6b 20 64 tains rollback d
6310: 61 74 61 20 66 6f 72 20 65 78 61 63 74 6c 79 20 ata for exactly
6320: 6f 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 one page..*/.#if
6330: 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
6340: 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 73 E_ATOMIC_WRITE.s
6350: 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 42 75 tatic int jrnlBu
6360: 66 66 65 72 53 69 7a 65 28 50 61 67 65 72 20 2a fferSize(Pager *
6370: 70 50 61 67 65 72 29 7b 0a 20 20 61 73 73 65 72 pPager){. asser
6380: 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 69 t( !MEMDB );. i
6390: 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 f( !pPager->temp
63a0: 46 69 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 File ){. int
63b0: 64 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 dc;
63c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
63d0: 20 44 65 76 69 63 65 20 63 68 61 72 61 63 74 65 Device characte
63e0: 72 69 73 74 69 63 73 20 2a 2f 0a 20 20 20 20 69 ristics */. i
63f0: 6e 74 20 6e 53 65 63 74 6f 72 3b 20 20 20 20 20 nt nSector;
6400: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6410: 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 /* Sector size
6420: 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 50 61 67 */. int szPag
6430: 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
6440: 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
6450: 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 20 20 61 73 size */.. as
6460: 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 sert( isOpen(pPa
6470: 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 20 20 ger->fd) );.
6480: 64 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 dc = sqlite3OsDe
6490: 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
64a0: 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b ics(pPager->fd);
64b0: 0a 20 20 20 20 6e 53 65 63 74 6f 72 20 3d 20 70 . nSector = p
64c0: 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a Pager->sectorSiz
64d0: 65 3b 0a 20 20 20 20 73 7a 50 61 67 65 20 3d 20 e;. szPage =
64e0: 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
64f0: 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 53 51 ;.. assert(SQ
6500: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
6510: 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b C512==(512>>8));
6520: 0a 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 . assert(SQLI
6530: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 TE_IOCAP_ATOMIC6
6540: 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b 4K==(65536>>8));
6550: 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 64 63 26 . if( 0==(dc&
6560: 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 (SQLITE_IOCAP_AT
6570: 4f 4d 49 43 7c 28 73 7a 50 61 67 65 3e 3e 38 29 OMIC|(szPage>>8)
6580: 29 20 7c 7c 20 6e 53 65 63 74 6f 72 3e 73 7a 50 ) || nSector>szP
6590: 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 age) ){. re
65a0: 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 turn 0;. }.
65b0: 7d 0a 0a 20 20 72 65 74 75 72 6e 20 4a 4f 55 52 }.. return JOUR
65c0: 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
65d0: 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f r) + JOURNAL_PG_
65e0: 53 5a 28 70 50 61 67 65 72 29 3b 0a 7d 0a 23 65 SZ(pPager);.}.#e
65f0: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 ndif../*.** If S
6600: 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 QLITE_CHECK_PAGE
6610: 53 20 69 73 20 64 65 66 69 6e 65 64 20 74 68 65 S is defined the
6620: 6e 20 77 65 20 64 6f 20 73 6f 6d 65 20 73 61 6e n we do some san
6630: 69 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 ity checking.**
6640: 6f 6e 20 74 68 65 20 63 61 63 68 65 20 75 73 69 on the cache usi
6650: 6e 67 20 61 20 68 61 73 68 20 66 75 6e 63 74 69 ng a hash functi
6660: 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 on. This is use
6670: 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a d for testing.**
6680: 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f and debugging o
6690: 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 nly..*/.#ifdef S
66a0: 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 QLITE_CHECK_PAGE
66b0: 53 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 S./*.** Return a
66c0: 20 33 32 2d 62 69 74 20 68 61 73 68 20 6f 66 20 32-bit hash of
66d0: 74 68 65 20 70 61 67 65 20 64 61 74 61 20 66 6f the page data fo
66e0: 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 r pPage..*/.stat
66f0: 69 63 20 75 33 32 20 70 61 67 65 72 5f 64 61 74 ic u32 pager_dat
6700: 61 68 61 73 68 28 69 6e 74 20 6e 42 79 74 65 2c ahash(int nByte,
6710: 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
6720: 70 44 61 74 61 29 7b 0a 20 20 75 33 32 20 68 61 pData){. u32 ha
6730: 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b sh = 0;. int i;
6740: 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 . for(i=0; i<nB
6750: 79 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 68 yte; i++){. h
6760: 61 73 68 20 3d 20 28 68 61 73 68 2a 31 30 33 39 ash = (hash*1039
6770: 29 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a 20 20 ) + pData[i];.
6780: 7d 0a 20 20 72 65 74 75 72 6e 20 68 61 73 68 3b }. return hash;
6790: 0a 7d 0a 73 74 61 74 69 63 20 75 33 32 20 70 61 .}.static u32 pa
67a0: 67 65 72 5f 70 61 67 65 68 61 73 68 28 50 67 48 ger_pagehash(PgH
67b0: 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 65 dr *pPage){. re
67c0: 74 75 72 6e 20 70 61 67 65 72 5f 64 61 74 61 68 turn pager_datah
67d0: 61 73 68 28 70 50 61 67 65 2d 3e 70 50 61 67 65 ash(pPage->pPage
67e0: 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 75 6e r->pageSize, (un
67f0: 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 50 signed char *)pP
6800: 61 67 65 2d 3e 70 44 61 74 61 29 3b 0a 7d 0a 73 age->pData);.}.s
6810: 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 tatic void pager
6820: 5f 73 65 74 5f 70 61 67 65 68 61 73 68 28 50 67 _set_pagehash(Pg
6830: 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 70 Hdr *pPage){. p
6840: 50 61 67 65 2d 3e 70 61 67 65 48 61 73 68 20 3d Page->pageHash =
6850: 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 pager_pagehash(
6860: 70 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pPage);.}../*.**
6870: 20 54 68 65 20 43 48 45 43 4b 5f 50 41 47 45 20 The CHECK_PAGE
6880: 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 20 50 67 macro takes a Pg
6890: 48 64 72 2a 20 61 73 20 61 6e 20 61 72 67 75 6d Hdr* as an argum
68a0: 65 6e 74 2e 20 49 66 20 53 51 4c 49 54 45 5f 43 ent. If SQLITE_C
68b0: 48 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20 69 73 HECK_PAGES.** is
68c0: 20 64 65 66 69 6e 65 64 2c 20 61 6e 64 20 4e 44 defined, and ND
68d0: 45 42 55 47 20 69 73 20 6e 6f 74 20 64 65 66 69 EBUG is not defi
68e0: 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28 29 ned, an assert()
68f0: 20 73 74 61 74 65 6d 65 6e 74 20 63 68 65 63 6b statement check
6900: 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 61 s.** that the pa
6910: 67 65 20 69 73 20 65 69 74 68 65 72 20 64 69 72 ge is either dir
6920: 74 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61 74 63 ty or still matc
6930: 68 65 73 20 74 68 65 20 63 61 6c 63 75 6c 61 74 hes the calculat
6940: 65 64 20 70 61 67 65 2d 68 61 73 68 2e 0a 2a 2f ed page-hash..*/
6950: 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 50 .#define CHECK_P
6960: 41 47 45 28 78 29 20 63 68 65 63 6b 50 61 67 65 AGE(x) checkPage
6970: 28 78 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 (x).static void
6980: 63 68 65 63 6b 50 61 67 65 28 50 67 48 64 72 20 checkPage(PgHdr
6990: 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a *pPg){. Pager *
69a0: 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 pPager = pPg->pP
69b0: 61 67 65 72 3b 0a 20 20 61 73 73 65 72 74 28 20 ager;. assert(
69c0: 21 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 7c !pPg->pageHash |
69d0: 7c 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 | pPager->errCod
69e0: 65 0a 20 20 20 20 20 20 7c 7c 20 28 70 50 67 2d e. || (pPg-
69f0: 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 >flags&PGHDR_DIR
6a00: 54 59 29 20 7c 7c 20 70 50 67 2d 3e 70 61 67 65 TY) || pPg->page
6a10: 48 61 73 68 3d 3d 70 61 67 65 72 5f 70 61 67 65 Hash==pager_page
6a20: 68 61 73 68 28 70 50 67 29 20 29 3b 0a 7d 0a 0a hash(pPg) );.}..
6a30: 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 70 61 #else.#define pa
6a40: 67 65 72 5f 64 61 74 61 68 61 73 68 28 58 2c 59 ger_datahash(X,Y
6a50: 29 20 20 30 0a 23 64 65 66 69 6e 65 20 70 61 67 ) 0.#define pag
6a60: 65 72 5f 70 61 67 65 68 61 73 68 28 58 29 20 20 er_pagehash(X)
6a70: 30 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 0.#define CHECK_
6a80: 50 41 47 45 28 78 29 0a 23 65 6e 64 69 66 20 20 PAGE(x).#endif
6a90: 2f 2a 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f /* SQLITE_CHECK_
6aa0: 50 41 47 45 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 PAGES */../*.**
6ab0: 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c When this is cal
6ac0: 6c 65 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 led the journal
6ad0: 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70 file for pager p
6ae0: 50 61 67 65 72 20 6d 75 73 74 20 62 65 20 6f 70 Pager must be op
6af0: 65 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 en..** This func
6b00: 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f tion attempts to
6b10: 20 72 65 61 64 20 61 20 6d 61 73 74 65 72 20 6a read a master j
6b20: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
6b30: 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 65 6e from the .** en
6b40: 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e d of the file an
6b50: 64 2c 20 69 66 20 73 75 63 63 65 73 73 66 75 6c d, if successful
6b60: 2c 20 63 6f 70 69 65 73 20 69 74 20 69 6e 74 6f , copies it into
6b70: 20 6d 65 6d 6f 72 79 20 73 75 70 70 6c 69 65 64 memory supplied
6b80: 20 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c .** by the call
6b90: 65 72 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 er. See comments
6ba0: 20 61 62 6f 76 65 20 77 72 69 74 65 4d 61 73 74 above writeMast
6bb0: 65 72 4a 6f 75 72 6e 61 6c 28 29 20 66 6f 72 20 erJournal() for
6bc0: 74 68 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 75 73 the format.** us
6bd0: 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 6d 61 ed to store a ma
6be0: 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
6bf0: 65 20 6e 61 6d 65 20 61 74 20 74 68 65 20 65 6e e name at the en
6c00: 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 d of a journal f
6c10: 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 74 ile..**.** zMast
6c20: 65 72 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f er must point to
6c30: 20 61 20 62 75 66 66 65 72 20 6f 66 20 61 74 20 a buffer of at
6c40: 6c 65 61 73 74 20 6e 4d 61 73 74 65 72 20 62 79 least nMaster by
6c50: 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 tes allocated by
6c60: 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 .** the caller.
6c70: 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 73 This should be s
6c80: 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 qlite3_vfs.mxPat
6c90: 68 6e 61 6d 65 2b 31 20 28 74 6f 20 65 6e 73 75 hname+1 (to ensu
6ca0: 72 65 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 65 re there is.** e
6cb0: 6e 6f 75 67 68 20 73 70 61 63 65 20 74 6f 20 77 nough space to w
6cc0: 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 rite the master
6cd0: 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 29 2e 20 49 journal name). I
6ce0: 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 f the master jou
6cf0: 72 6e 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69 6e 20 rnal.** name in
6d00: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c the journal is l
6d10: 6f 6e 67 65 72 20 74 68 61 6e 20 6e 4d 61 73 74 onger than nMast
6d20: 65 72 20 62 79 74 65 73 20 28 69 6e 63 6c 75 64 er bytes (includ
6d30: 69 6e 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 ing a.** nul-ter
6d40: 6d 69 6e 61 74 6f 72 29 2c 20 74 68 65 6e 20 74 minator), then t
6d50: 68 69 73 20 69 73 20 68 61 6e 64 6c 65 64 20 61 his is handled a
6d60: 73 20 69 66 20 6e 6f 20 6d 61 73 74 65 72 20 6a s if no master j
6d70: 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a 20 77 ournal name.** w
6d80: 65 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 74 ere present in t
6d90: 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a he journal..**.*
6da0: 2a 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f * If a master jo
6db0: 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 urnal file name
6dc0: 69 73 20 70 72 65 73 65 6e 74 20 61 74 20 74 68 is present at th
6dd0: 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75 e end of the jou
6de0: 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2c 20 74 68 rnal.** file, th
6df0: 65 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 en it is copied
6e00: 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 into the buffer
6e10: 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d pointed to by zM
6e20: 61 73 74 65 72 2e 20 41 0a 2a 2a 20 6e 75 6c 2d aster. A.** nul-
6e30: 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 20 terminator byte
6e40: 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 is appended to t
6e50: 68 65 20 62 75 66 66 65 72 20 66 6f 6c 6c 6f 77 he buffer follow
6e60: 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 0a 2a ing the master.*
6e70: 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e * journal file n
6e80: 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 ame..**.** If it
6e90: 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 74 is determined t
6ea0: 68 61 74 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f hat no master jo
6eb0: 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 urnal file name
6ec0: 69 73 20 70 72 65 73 65 6e 74 20 0a 2a 2a 20 7a is present .** z
6ed0: 4d 61 73 74 65 72 5b 30 5d 20 69 73 20 73 65 74 Master[0] is set
6ee0: 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 to 0 and SQLITE
6ef0: 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a _OK returned..**
6f00: 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 .** If an error
6f10: 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 61 occurs while rea
6f20: 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a 6f ding from the jo
6f30: 75 72 6e 61 6c 20 66 69 6c 65 2c 20 61 6e 20 53 urnal file, an S
6f40: 51 4c 69 74 65 0a 2a 2a 20 65 72 72 6f 72 20 63 QLite.** error c
6f50: 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
6f60: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 .*/.static int r
6f70: 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c eadMasterJournal
6f80: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
6f90: 4a 72 6e 6c 2c 20 63 68 61 72 20 2a 7a 4d 61 73 Jrnl, char *zMas
6fa0: 74 65 72 2c 20 75 33 32 20 6e 4d 61 73 74 65 72 ter, u32 nMaster
6fb0: 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
6fc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6fd0: 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
6fe0: 2f 0a 20 20 75 33 32 20 6c 65 6e 3b 20 20 20 20 /. u32 len;
6ff0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
7000: 2a 20 4c 65 6e 67 74 68 20 69 6e 20 62 79 74 65 * Length in byte
7010: 73 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 s of master jour
7020: 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 36 nal name */. i6
7030: 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 4 szJ;
7040: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 /* Tota
7050: 6c 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 l size in bytes
7060: 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 of journal file
7070: 70 4a 72 6e 6c 20 2a 2f 0a 20 20 75 33 32 20 63 pJrnl */. u32 c
7080: 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 ksum;
7090: 20 20 20 20 20 20 2f 2a 20 4d 4a 20 63 68 65 63 /* MJ chec
70a0: 6b 73 75 6d 20 76 61 6c 75 65 20 72 65 61 64 20 ksum value read
70b0: 66 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a from journal */.
70c0: 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 u32 u;
70d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
70e0: 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f Unsigned loop co
70f0: 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67 unter */. unsig
7100: 6e 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b ned char aMagic[
7110: 38 5d 3b 20 20 20 2f 2a 20 41 20 62 75 66 66 65 8]; /* A buffe
7120: 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 r to hold the ma
7130: 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 gic header */.
7140: 7a 4d 61 73 74 65 72 5b 30 5d 20 3d 20 27 5c 30 zMaster[0] = '\0
7150: 27 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 ';.. if( SQLITE
7160: 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 _OK!=(rc = sqlit
7170: 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4a 72 e3OsFileSize(pJr
7180: 6e 6c 2c 20 26 73 7a 4a 29 29 0a 20 20 20 7c 7c nl, &szJ)). ||
7190: 20 73 7a 4a 3c 31 36 0a 20 20 20 7c 7c 20 53 51 szJ<16. || SQ
71a0: 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 LITE_OK!=(rc = r
71b0: 65 61 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c ead32bits(pJrnl,
71c0: 20 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e 29 29 0a szJ-16, &len)).
71d0: 20 20 20 7c 7c 20 6c 65 6e 3e 3d 6e 4d 61 73 74 || len>=nMast
71e0: 65 72 20 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 er . || SQLITE
71f0: 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 _OK!=(rc = read3
7200: 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2bits(pJrnl, szJ
7210: 2d 31 32 2c 20 26 63 6b 73 75 6d 29 29 0a 20 20 -12, &cksum)).
7220: 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 || SQLITE_OK!=(
7230: 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 rc = sqlite3OsRe
7240: 61 64 28 70 4a 72 6e 6c 2c 20 61 4d 61 67 69 63 ad(pJrnl, aMagic
7250: 2c 20 38 2c 20 73 7a 4a 2d 38 29 29 0a 20 20 20 , 8, szJ-8)).
7260: 7c 7c 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 || memcmp(aMagic
7270: 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c , aJournalMagic,
7280: 20 38 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 8). || SQLITE
7290: 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 _OK!=(rc = sqlit
72a0: 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20 e3OsRead(pJrnl,
72b0: 7a 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 73 7a zMaster, len, sz
72c0: 4a 2d 31 36 2d 6c 65 6e 29 29 0a 20 20 29 7b 0a J-16-len)). ){.
72d0: 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
72e0: 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 }.. /* See if
72f0: 74 68 65 20 63 68 65 63 6b 73 75 6d 20 6d 61 74 the checksum mat
7300: 63 68 65 73 20 74 68 65 20 6d 61 73 74 65 72 20 ches the master
7310: 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a journal name */.
7320: 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65 6e for(u=0; u<len
7330: 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 ; u++){. cksu
7340: 6d 20 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d 3b m -= zMaster[u];
7350: 0a 20 20 7d 0a 20 20 69 66 28 20 63 6b 73 75 6d . }. if( cksum
7360: 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 ){. /* If th
7370: 65 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 73 6e e checksum doesn
7380: 27 74 20 61 64 64 20 75 70 2c 20 74 68 65 6e 20 't add up, then
7390: 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 one or more of t
73a0: 68 65 20 64 69 73 6b 20 73 65 63 74 6f 72 73 0a he disk sectors.
73b0: 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e ** containin
73c0: 67 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 g the master jou
73d0: 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 rnal filename is
73e0: 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73 corrupted. This
73f0: 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 64 65 means. ** de
7400: 66 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 62 61 finitely roll ba
7410: 63 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65 74 75 ck, so just retu
7420: 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 rn SQLITE_OK and
7430: 20 72 65 70 6f 72 74 20 61 20 28 6e 75 6c 29 0a report a (nul).
7440: 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f ** master-jo
7450: 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a urnal filename..
7460: 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d */. len =
7470: 20 30 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 0;. }. zMaste
7480: 72 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 r[len] = '\0';.
7490: 20 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
74a0: 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
74b0: 52 65 74 75 72 6e 20 74 68 65 20 6f 66 66 73 65 Return the offse
74c0: 74 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 t of the sector
74d0: 62 6f 75 6e 64 61 72 79 20 61 74 20 6f 72 20 69 boundary at or i
74e0: 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a 2a 20 66 mmediately .** f
74f0: 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 76 61 6c ollowing the val
7500: 75 65 20 69 6e 20 70 50 61 67 65 72 2d 3e 6a 6f ue in pPager->jo
7510: 75 72 6e 61 6c 4f 66 66 2c 20 61 73 73 75 6d 69 urnalOff, assumi
7520: 6e 67 20 61 20 73 65 63 74 6f 72 20 0a 2a 2a 20 ng a sector .**
7530: 73 69 7a 65 20 6f 66 20 70 50 61 67 65 72 2d 3e size of pPager->
7540: 73 65 63 74 6f 72 53 69 7a 65 20 62 79 74 65 73 sectorSize bytes
7550: 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 20 66 6f 72 20 ..**.** i.e for
7560: 61 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 a sector size of
7570: 20 35 31 32 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 61 512:.**.** Pa
7580: 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 20 ger.journalOff
7590: 20 20 20 20 20 20 20 20 52 65 74 75 72 6e 20 76 Return v
75a0: 61 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d alue.** ------
75b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
75c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
75d0: 2d 0a 2a 2a 20 20 20 30 20 20 20 20 20 20 20 20 -.** 0
75e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
75f0: 20 30 0a 2a 2a 20 20 20 35 31 32 20 20 20 20 20 0.** 512
7600: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7610: 20 20 35 31 32 0a 2a 2a 20 20 20 31 30 30 20 20 512.** 100
7620: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7630: 20 20 20 20 20 35 31 32 0a 2a 2a 20 20 20 32 30 512.** 20
7640: 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 00
7650: 20 20 20 20 20 20 20 20 32 30 34 38 0a 2a 2a 20 2048.**
7660: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 6a .*/.static i64 j
7670: 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 ournalHdrOffset(
7680: 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
7690: 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 30 i64 offset = 0
76a0: 3b 0a 20 20 69 36 34 20 63 20 3d 20 70 50 61 67 ;. i64 c = pPag
76b0: 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a er->journalOff;.
76c0: 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 6f if( c ){. o
76d0: 66 66 73 65 74 20 3d 20 28 28 63 2d 31 29 2f 4a ffset = ((c-1)/J
76e0: 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
76f0: 61 67 65 72 29 20 2b 20 31 29 20 2a 20 4a 4f 55 ager) + 1) * JOU
7700: 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
7710: 65 72 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 er);. }. asser
7720: 74 28 20 6f 66 66 73 65 74 25 4a 4f 55 52 4e 41 t( offset%JOURNA
7730: 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
7740: 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
7750: 20 6f 66 66 73 65 74 3e 3d 63 20 29 3b 0a 20 20 offset>=c );.
7760: 61 73 73 65 72 74 28 20 28 6f 66 66 73 65 74 2d assert( (offset-
7770: 63 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 c)<JOURNAL_HDR_S
7780: 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 72 Z(pPager) );. r
7790: 65 74 75 72 6e 20 6f 66 66 73 65 74 3b 0a 7d 0a eturn offset;.}.
77a0: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e ./*.** The journ
77b0: 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 al file must be
77c0: 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66 open when this f
77d0: 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
77e0: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 d..**.** This fu
77f0: 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f nction is a no-o
7800: 70 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c p if the journal
7810: 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 file has not be
7820: 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a en written to.**
7830: 20 77 69 74 68 69 6e 20 74 68 65 20 63 75 72 72 within the curr
7840: 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
7850: 28 69 2e 65 2e 20 69 66 20 50 61 67 65 72 2e 6a (i.e. if Pager.j
7860: 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 29 2e 0a 2a ournalOff==0)..*
7870: 2a 0a 2a 2a 20 49 66 20 64 6f 54 72 75 6e 63 61 *.** If doTrunca
7880: 74 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f te is non-zero o
7890: 72 20 74 68 65 20 50 61 67 65 72 2e 6a 6f 75 72 r the Pager.jour
78a0: 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 76 61 72 nalSizeLimit var
78b0: 69 61 62 6c 65 20 69 73 0a 2a 2a 20 73 65 74 20 iable is.** set
78c0: 74 6f 20 30 2c 20 74 68 65 6e 20 74 72 75 6e 63 to 0, then trunc
78d0: 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ate the journal
78e0: 66 69 6c 65 20 74 6f 20 7a 65 72 6f 20 62 79 74 file to zero byt
78f0: 65 73 20 69 6e 20 73 69 7a 65 2e 20 4f 74 68 65 es in size. Othe
7900: 72 77 69 73 65 2c 0a 2a 2a 20 7a 65 72 6f 20 74 rwise,.** zero t
7910: 68 65 20 32 38 2d 62 79 74 65 20 68 65 61 64 65 he 28-byte heade
7920: 72 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f r at the start o
7930: 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
7940: 6c 65 2e 20 49 6e 20 65 69 74 68 65 72 20 63 61 le. In either ca
7950: 73 65 2c 20 0a 2a 2a 20 69 66 20 74 68 65 20 70 se, .** if the p
7960: 61 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 6e ager is not in n
7970: 6f 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 73 79 6e o-sync mode, syn
7980: 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 c the journal fi
7990: 6c 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 0a le immediately .
79a0: 2a 2a 20 61 66 74 65 72 20 77 72 69 74 69 6e 67 ** after writing
79b0: 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 69 or truncating i
79c0: 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 61 67 65 t..**.** If Page
79d0: 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d r.journalSizeLim
79e0: 69 74 20 69 73 20 73 65 74 20 74 6f 20 61 20 70 it is set to a p
79f0: 6f 73 69 74 69 76 65 2c 20 6e 6f 6e 2d 7a 65 72 ositive, non-zer
7a00: 6f 20 76 61 6c 75 65 2c 20 61 6e 64 0a 2a 2a 20 o value, and.**
7a10: 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 72 following the tr
7a20: 75 6e 63 61 74 69 6f 6e 20 6f 72 20 7a 65 72 6f uncation or zero
7a30: 69 6e 67 20 64 65 73 63 72 69 62 65 64 20 61 62 ing described ab
7a40: 6f 76 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 ove the size of
7a50: 74 68 65 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 the .** journal
7a60: 66 69 6c 65 20 69 6e 20 62 79 74 65 73 20 69 73 file in bytes is
7a70: 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 larger than thi
7a80: 73 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 72 s value, then tr
7a90: 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 6a 6f uncate the.** jo
7aa0: 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 50 61 urnal file to Pa
7ab0: 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c ger.journalSizeL
7ac0: 69 6d 69 74 20 62 79 74 65 73 2e 20 54 68 65 20 imit bytes. The
7ad0: 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 journal file doe
7ae0: 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f s.** not need to
7af0: 20 62 65 20 73 79 6e 63 65 64 20 66 6f 6c 6c 6f be synced follo
7b00: 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61 74 wing this operat
7b10: 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ion..**.** If an
7b20: 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 IO error occurs
7b30: 2c 20 61 62 61 6e 64 6f 6e 20 70 72 6f 63 65 73 , abandon proces
7b40: 73 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 20 sing and return
7b50: 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 the IO error cod
7b60: 65 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c e..** Otherwise,
7b70: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
7b80: 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 K..*/.static int
7b90: 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 zeroJournalHdr(
7ba0: 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
7bb0: 6e 74 20 64 6f 54 72 75 6e 63 61 74 65 29 7b 0a nt doTruncate){.
7bc0: 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
7bd0: 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 E_OK;
7be0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7bf0: 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
7c00: 64 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 de */. assert(
7c10: 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
7c20: 66 64 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 fd) );. if( pPa
7c30: 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
7c40: 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 36 34 ){. const i64
7c50: 20 69 4c 69 6d 69 74 20 3d 20 70 50 61 67 65 72 iLimit = pPager
7c60: 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d ->journalSizeLim
7c70: 69 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 it; /* Local
7c80: 63 61 63 68 65 20 6f 66 20 6a 73 6c 20 2a 2f 0a cache of jsl */.
7c90: 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a . IOTRACE(("J
7ca0: 5a 45 52 4f 48 44 52 20 25 70 5c 6e 22 2c 20 70 ZEROHDR %p\n", p
7cb0: 50 61 67 65 72 29 29 0a 20 20 20 20 69 66 28 20 Pager)). if(
7cc0: 64 6f 54 72 75 6e 63 61 74 65 20 7c 7c 20 69 4c doTruncate || iL
7cd0: 69 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 imit==0 ){.
7ce0: 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 rc = sqlite3OsT
7cf0: 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e runcate(pPager->
7d00: 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c jfd, 0);. }el
7d10: 73 65 7b 0a 20 20 20 20 20 20 73 74 61 74 69 63 se{. static
7d20: 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 65 72 6f const char zero
7d30: 48 64 72 5b 32 38 5d 20 3d 20 7b 30 7d 3b 0a 20 Hdr[28] = {0};.
7d40: 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
7d50: 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3OsWrite(pPager-
7d60: 3e 6a 66 64 2c 20 7a 65 72 6f 48 64 72 2c 20 73 >jfd, zeroHdr, s
7d70: 69 7a 65 6f 66 28 7a 65 72 6f 48 64 72 29 2c 20 izeof(zeroHdr),
7d80: 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 0);. }. if
7d90: 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
7da0: 26 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 && !pPager->noSy
7db0: 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d nc ){. rc =
7dc0: 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 sqlite3OsSync(p
7dd0: 50 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c 49 Pager->jfd, SQLI
7de0: 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 TE_SYNC_DATAONLY
7df0: 7c 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c |pPager->sync_fl
7e00: 61 67 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 ags);. }..
7e10: 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e /* At this poin
7e20: 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f t the transactio
7e30: 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 n is committed b
7e40: 75 74 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 ut the write loc
7e50: 6b 20 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 69 k . ** is sti
7e60: 6c 6c 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 66 ll held on the f
7e70: 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 ile. If there is
7e80: 20 61 20 73 69 7a 65 20 6c 69 6d 69 74 20 63 6f a size limit co
7e90: 6e 66 69 67 75 72 65 64 20 66 6f 72 20 0a 20 20 nfigured for .
7ea0: 20 20 2a 2a 20 74 68 65 20 70 65 72 73 69 73 74 ** the persist
7eb0: 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 ent journal and
7ec0: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
7ed0: 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 75 currently consu
7ee0: 6d 65 73 20 6d 6f 72 65 0a 20 20 20 20 2a 2a 20 mes more. **
7ef0: 73 70 61 63 65 20 74 68 61 6e 20 74 68 61 74 20 space than that
7f00: 6c 69 6d 69 74 20 61 6c 6c 6f 77 73 20 66 6f 72 limit allows for
7f10: 2c 20 74 72 75 6e 63 61 74 65 20 69 74 20 6e 6f , truncate it no
7f20: 77 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e w. There is no n
7f30: 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 73 79 eed. ** to sy
7f40: 6e 63 20 74 68 65 20 66 69 6c 65 20 66 6f 6c 6c nc the file foll
7f50: 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61 owing this opera
7f60: 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tion.. */.
7f70: 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
7f80: 4f 4b 20 26 26 20 69 4c 69 6d 69 74 3e 30 20 29 OK && iLimit>0 )
7f90: 7b 0a 20 20 20 20 20 20 69 36 34 20 73 7a 3b 0a {. i64 sz;.
7fa0: 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
7fb0: 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 e3OsFileSize(pPa
7fc0: 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 29 3b 0a ger->jfd, &sz);.
7fd0: 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
7fe0: 4c 49 54 45 5f 4f 4b 20 26 26 20 73 7a 3e 69 4c LITE_OK && sz>iL
7ff0: 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 imit ){.
8000: 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 rc = sqlite3OsTr
8010: 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a uncate(pPager->j
8020: 66 64 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 fd, iLimit);.
8030: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
8040: 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
8050: 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c *.** The journal
8060: 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 file must be op
8070: 65 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 en when this rou
8080: 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 tine is called.
8090: 41 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 68 65 61 A journal.** hea
80a0: 64 65 72 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 der (JOURNAL_HDR
80b0: 5f 53 5a 20 62 79 74 65 73 29 20 69 73 20 77 72 _SZ bytes) is wr
80c0: 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a itten into the j
80d0: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 74 20 74 ournal file at t
80e0: 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f he.** current lo
80f0: 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 cation..**.** Th
8100: 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 74 68 65 e format for the
8110: 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
8120: 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a is as follows:.*
8130: 2a 20 2d 20 38 20 62 79 74 65 73 3a 20 4d 61 67 * - 8 bytes: Mag
8140: 69 63 20 69 64 65 6e 74 69 66 79 69 6e 67 20 6a ic identifying j
8150: 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 2e 0a 2a ournal format..*
8160: 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 4e 75 6d * - 4 bytes: Num
8170: 62 65 72 20 6f 66 20 72 65 63 6f 72 64 73 20 69 ber of records i
8180: 6e 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 2d 31 n journal, or -1
8190: 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 69 73 no-sync mode is
81a0: 20 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 on..** - 4 byte
81b0: 73 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 s: Random number
81c0: 20 75 73 65 64 20 66 6f 72 20 70 61 67 65 20 68 used for page h
81d0: 61 73 68 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 ash..** - 4 byte
81e0: 73 3a 20 49 6e 69 74 69 61 6c 20 64 61 74 61 62 s: Initial datab
81f0: 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a ase page count..
8200: 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 53 65 ** - 4 bytes: Se
8210: 63 74 6f 72 20 73 69 7a 65 20 75 73 65 64 20 62 ctor size used b
8220: 79 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 y the process th
8230: 61 74 20 77 72 6f 74 65 20 74 68 69 73 20 6a 6f at wrote this jo
8240: 75 72 6e 61 6c 2e 0a 2a 2a 20 2d 20 34 20 62 79 urnal..** - 4 by
8250: 74 65 73 3a 20 44 61 74 61 62 61 73 65 20 70 61 tes: Database pa
8260: 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 0a 2a 2a 20 ge size..** .**
8270: 46 6f 6c 6c 6f 77 65 64 20 62 79 20 28 4a 4f 55 Followed by (JOU
8280: 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 RNAL_HDR_SZ - 28
8290: 29 20 62 79 74 65 73 20 6f 66 20 75 6e 75 73 65 ) bytes of unuse
82a0: 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 d space..*/.stat
82b0: 69 63 20 69 6e 74 20 77 72 69 74 65 4a 6f 75 72 ic int writeJour
82c0: 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 50 nalHdr(Pager *pP
82d0: 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 ager){. int rc
82e0: 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
82f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
8300: 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
8310: 20 63 68 61 72 20 2a 7a 48 65 61 64 65 72 20 3d char *zHeader =
8320: 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 pPager->pTmpSpa
8330: 63 65 3b 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 ce; /* Temporar
8340: 79 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20 y space used to
8350: 62 75 69 6c 64 20 68 65 61 64 65 72 20 2a 2f 0a build header */.
8360: 20 20 75 33 32 20 6e 48 65 61 64 65 72 20 3d 20 u32 nHeader =
8370: 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
8380: 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 ; /* Size of
8390: 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 buffer pointed
83a0: 74 6f 20 62 79 20 7a 48 65 61 64 65 72 20 2a 2f to by zHeader */
83b0: 0a 20 20 75 33 32 20 6e 57 72 69 74 65 3b 20 20 . u32 nWrite;
83c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
83d0: 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 /* Bytes
83e0: 6f 66 20 68 65 61 64 65 72 20 73 65 63 74 6f 72 of header sector
83f0: 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e written */. in
8400: 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 t ii;
8410: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8420: 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
8430: 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 r */.. assert(
8440: 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
8450: 66 64 29 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a fd) ); /* J
8460: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 ournal file must
8470: 20 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 be open. */..
8480: 69 66 28 20 6e 48 65 61 64 65 72 3e 4a 4f 55 52 if( nHeader>JOUR
8490: 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
84a0: 72 29 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 r) ){. nHeade
84b0: 72 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f r = JOURNAL_HDR_
84c0: 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a SZ(pPager);. }.
84d0: 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 . /* If there a
84e0: 72 65 20 61 63 74 69 76 65 20 73 61 76 65 70 6f re active savepo
84f0: 69 6e 74 73 20 61 6e 64 20 61 6e 79 20 6f 66 20 ints and any of
8500: 74 68 65 6d 20 77 65 72 65 20 63 72 65 61 74 65 them were create
8510: 64 20 0a 20 20 2a 2a 20 73 69 6e 63 65 20 74 68 d . ** since th
8520: 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 6a 6f e most recent jo
8530: 75 72 6e 61 6c 20 68 65 61 64 65 72 20 77 61 73 urnal header was
8540: 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65 written, update
8550: 20 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 the . ** Pager
8560: 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 Savepoint.iHdrOf
8570: 66 73 65 74 20 66 69 65 6c 64 73 20 6e 6f 77 2e fset fields now.
8580: 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 . */. for(ii=0
8590: 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 ; ii<pPager->nSa
85a0: 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a vepoint; ii++){.
85b0: 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
85c0: 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 aSavepoint[ii].i
85d0: 48 64 72 4f 66 66 73 65 74 3d 3d 30 20 29 7b 0a HdrOffset==0 ){.
85e0: 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 pPager->aS
85f0: 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 64 avepoint[ii].iHd
8600: 72 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 rOffset = pPager
8610: 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 ->journalOff;.
8620: 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 }. }.. pPage
8630: 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 r->journalHdr =
8640: 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
8650: 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f ff = journalHdrO
8660: 66 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 0a ffset(pPager);..
8670: 20 20 2f 2a 20 0a 20 20 2a 2a 20 57 72 69 74 65 /* . ** Write
8680: 20 74 68 65 20 6e 52 65 63 20 46 69 65 6c 64 20 the nRec Field
8690: 2d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 - the number of
86a0: 70 61 67 65 20 72 65 63 6f 72 64 73 20 74 68 61 page records tha
86b0: 74 20 66 6f 6c 6c 6f 77 20 74 68 69 73 0a 20 20 t follow this.
86c0: 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 ** journal heade
86d0: 72 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 7a 65 72 r. Normally, zer
86e0: 6f 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 o is written to
86f0: 74 68 69 73 20 76 61 6c 75 65 20 61 74 20 74 68 this value at th
8700: 69 73 20 74 69 6d 65 2e 0a 20 20 2a 2a 20 41 66 is time.. ** Af
8710: 74 65 72 20 74 68 65 20 72 65 63 6f 72 64 73 20 ter the records
8720: 61 72 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 are added to the
8730: 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 74 68 journal (and th
8740: 65 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 65 64 e journal synced
8750: 2c 20 0a 20 20 2a 2a 20 69 66 20 69 6e 20 66 75 , . ** if in fu
8760: 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 29 2c 20 74 ll-sync mode), t
8770: 68 65 20 7a 65 72 6f 20 69 73 20 6f 76 65 72 77 he zero is overw
8780: 72 69 74 74 65 6e 20 77 69 74 68 20 74 68 65 20 ritten with the
8790: 74 72 75 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a true number. **
87a0: 20 6f 66 20 72 65 63 6f 72 64 73 20 28 73 65 65 of records (see
87b0: 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 29 29 2e syncJournal()).
87c0: 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 66 61 73 . **. ** A fas
87d0: 74 65 72 20 61 6c 74 65 72 6e 61 74 69 76 65 20 ter alternative
87e0: 69 73 20 74 6f 20 77 72 69 74 65 20 30 78 46 46 is to write 0xFF
87f0: 46 46 46 46 46 46 20 74 6f 20 74 68 65 20 6e 52 FFFFFF to the nR
8800: 65 63 20 66 69 65 6c 64 2e 20 57 68 65 6e 0a 20 ec field. When.
8810: 20 2a 2a 20 72 65 61 64 69 6e 67 20 74 68 65 20 ** reading the
8820: 6a 6f 75 72 6e 61 6c 20 74 68 69 73 20 76 61 6c journal this val
8830: 75 65 20 74 65 6c 6c 73 20 53 51 4c 69 74 65 20 ue tells SQLite
8840: 74 6f 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 to assume that t
8850: 68 65 0a 20 20 2a 2a 20 72 65 73 74 20 6f 66 20 he. ** rest of
8860: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
8870: 20 63 6f 6e 74 61 69 6e 73 20 76 61 6c 69 64 20 contains valid
8880: 70 61 67 65 20 72 65 63 6f 72 64 73 2e 20 54 68 page records. Th
8890: 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 0a 20 20 is assumption.
88a0: 2a 2a 20 69 73 20 64 61 6e 67 65 72 6f 75 73 2c ** is dangerous,
88b0: 20 61 73 20 69 66 20 61 20 66 61 69 6c 75 72 65 as if a failure
88c0: 20 6f 63 63 75 72 72 65 64 20 77 68 69 6c 73 74 occurred whilst
88d0: 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 writing to the
88e0: 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c journal. ** fil
88f0: 65 20 69 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e e it may contain
8900: 20 73 6f 6d 65 20 67 61 72 62 61 67 65 20 64 61 some garbage da
8910: 74 61 2e 20 54 68 65 72 65 20 61 72 65 20 74 77 ta. There are tw
8920: 6f 20 73 63 65 6e 61 72 69 6f 73 0a 20 20 2a 2a o scenarios. **
8930: 20 77 68 65 72 65 20 74 68 69 73 20 72 69 73 6b where this risk
8940: 20 63 61 6e 20 62 65 20 69 67 6e 6f 72 65 64 3a can be ignored:
8950: 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 . **. ** * W
8960: 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 69 73 hen the pager is
8970: 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 in no-sync mode
8980: 2e 20 43 6f 72 72 75 70 74 69 6f 6e 20 63 61 6e . Corruption can
8990: 20 66 6f 6c 6c 6f 77 20 61 0a 20 20 2a 2a 20 20 follow a. **
89a0: 20 20 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 power failure
89b0: 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e in this case an
89c0: 79 77 61 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 yway.. **. **
89d0: 20 20 2a 20 57 68 65 6e 20 74 68 65 20 53 51 4c * When the SQL
89e0: 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 ITE_IOCAP_SAFE_A
89f0: 50 50 45 4e 44 20 66 6c 61 67 20 69 73 20 73 65 PPEND flag is se
8a00: 74 2e 20 54 68 69 73 20 67 75 61 72 61 6e 74 65 t. This guarante
8a10: 65 73 0a 20 20 2a 2a 20 20 20 20 20 74 68 61 74 es. ** that
8a20: 20 67 61 72 62 61 67 65 20 64 61 74 61 20 69 73 garbage data is
8a30: 20 6e 65 76 65 72 20 61 70 70 65 6e 64 65 64 20 never appended
8a40: 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
8a50: 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 ile.. */. asse
8a60: 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 rt( isOpen(pPage
8a70: 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 72 r->fd) || pPager
8a80: 2d 3e 6e 6f 53 79 6e 63 20 29 3b 0a 20 20 69 66 ->noSync );. if
8a90: 28 20 28 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e ( (pPager->noSyn
8aa0: 63 29 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 6a c) || (pPager->j
8ab0: 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 ournalMode==PAGE
8ac0: 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 R_JOURNALMODE_ME
8ad0: 4d 4f 52 59 29 0a 20 20 20 7c 7c 20 28 73 71 6c MORY). || (sql
8ae0: 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 ite3OsDeviceChar
8af0: 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67 acteristics(pPag
8b00: 65 72 2d 3e 66 64 29 26 53 51 4c 49 54 45 5f 49 er->fd)&SQLITE_I
8b10: 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 OCAP_SAFE_APPEND
8b20: 29 20 0a 20 20 29 7b 0a 20 20 20 20 6d 65 6d 63 ) . ){. memc
8b30: 70 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f 75 py(zHeader, aJou
8b40: 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f rnalMagic, sizeo
8b50: 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 f(aJournalMagic)
8b60: 29 3b 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 );. put32bits
8b70: 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 (&zHeader[sizeof
8b80: 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d (aJournalMagic)]
8b90: 2c 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 , 0xffffffff);.
8ba0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 73 }else{. mems
8bb0: 65 74 28 7a 48 65 61 64 65 72 2c 20 30 2c 20 73 et(zHeader, 0, s
8bc0: 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 izeof(aJournalMa
8bd0: 67 69 63 29 2b 34 29 3b 0a 20 20 7d 0a 0a 20 20 gic)+4);. }..
8be0: 2f 2a 20 54 68 65 20 72 61 6e 64 6f 6d 20 63 68 /* The random ch
8bf0: 65 63 6b 2d 68 61 73 68 20 69 6e 69 74 69 61 6c eck-hash initial
8c00: 69 73 65 72 20 2a 2f 20 0a 20 20 73 71 6c 69 74 iser */ . sqlit
8c10: 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 e3_randomness(si
8c20: 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 63 6b 73 zeof(pPager->cks
8c30: 75 6d 49 6e 69 74 29 2c 20 26 70 50 61 67 65 72 umInit), &pPager
8c40: 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 ->cksumInit);.
8c50: 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 put32bits(&zHead
8c60: 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e er[sizeof(aJourn
8c70: 61 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 70 50 61 alMagic)+4], pPa
8c80: 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b ger->cksumInit);
8c90: 0a 20 20 2f 2a 20 54 68 65 20 69 6e 69 74 69 61 . /* The initia
8ca0: 6c 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 l database size
8cb0: 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 */. put32bits(&
8cc0: 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 zHeader[sizeof(a
8cd0: 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 38 5d JournalMagic)+8]
8ce0: 2c 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 , pPager->dbOrig
8cf0: 53 69 7a 65 29 3b 0a 20 20 2f 2a 20 54 68 65 20 Size);. /* The
8d00: 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 73 assumed sector s
8d10: 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70 72 6f ize for this pro
8d20: 63 65 73 73 20 2a 2f 0a 20 20 70 75 74 33 32 62 cess */. put32b
8d30: 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a its(&zHeader[siz
8d40: 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 eof(aJournalMagi
8d50: 63 29 2b 31 32 5d 2c 20 70 50 61 67 65 72 2d 3e c)+12], pPager->
8d60: 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 0a 20 20 sectorSize);..
8d70: 2f 2a 20 54 68 65 20 70 61 67 65 20 73 69 7a 65 /* The page size
8d80: 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 */. put32bits(
8d90: 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 &zHeader[sizeof(
8da0: 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 aJournalMagic)+1
8db0: 36 5d 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 6], pPager->page
8dc0: 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 Size);.. /* Ini
8dd0: 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 74 61 tializing the ta
8de0: 69 6c 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 il of the buffer
8df0: 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 is not necessar
8e00: 79 2e 20 20 45 76 65 72 79 74 68 69 6e 67 0a 20 y. Everything.
8e10: 20 2a 2a 20 77 6f 72 6b 73 20 66 69 6e 64 20 69 ** works find i
8e20: 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
8e30: 6d 65 6d 73 65 74 28 29 20 69 73 20 6f 6d 69 74 memset() is omit
8e40: 74 65 64 2e 20 20 42 75 74 20 69 6e 69 74 69 61 ted. But initia
8e50: 6c 69 7a 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 lizing. ** the
8e60: 6d 65 6d 6f 72 79 20 70 72 65 76 65 6e 74 73 20 memory prevents
8e70: 76 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20 63 6f valgrind from co
8e80: 6d 70 6c 61 69 6e 69 6e 67 2c 20 73 6f 20 77 65 mplaining, so we
8e90: 20 61 72 65 20 77 69 6c 6c 69 6e 67 20 74 6f 0a are willing to.
8ea0: 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 70 65 ** take the pe
8eb0: 72 66 6f 72 6d 61 6e 63 65 20 68 69 74 2e 0a 20 rformance hit..
8ec0: 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 7a 48 */. memset(&zH
8ed0: 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f eader[sizeof(aJo
8ee0: 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 5d 2c urnalMagic)+20],
8ef0: 20 30 2c 0a 20 20 20 20 20 20 20 20 20 6e 48 65 0,. nHe
8f00: 61 64 65 72 2d 28 73 69 7a 65 6f 66 28 61 4a 6f ader-(sizeof(aJo
8f10: 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 29 29 urnalMagic)+20))
8f20: 3b 0a 0a 20 20 2f 2a 20 49 6e 20 74 68 65 6f 72 ;.. /* In theor
8f30: 79 2c 20 69 74 20 69 73 20 6f 6e 6c 79 20 6e 65 y, it is only ne
8f40: 63 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 cessary to write
8f50: 20 74 68 65 20 32 38 20 62 79 74 65 73 20 74 68 the 28 bytes th
8f60: 61 74 20 74 68 65 20 0a 20 20 2a 2a 20 6a 6f 75 at the . ** jou
8f70: 72 6e 61 6c 20 68 65 61 64 65 72 20 63 6f 6e 73 rnal header cons
8f80: 75 6d 65 73 20 74 6f 20 74 68 65 20 6a 6f 75 72 umes to the jour
8f90: 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e 20 54 nal file here. T
8fa0: 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 hen increment th
8fb0: 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 2e 6a 6f e . ** Pager.jo
8fc0: 75 72 6e 61 6c 4f 66 66 20 76 61 72 69 61 62 6c urnalOff variabl
8fd0: 65 20 62 79 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 e by JOURNAL_HDR
8fe0: 5f 53 5a 20 73 6f 20 74 68 61 74 20 74 68 65 20 _SZ so that the
8ff0: 6e 65 78 74 20 0a 20 20 2a 2a 20 72 65 63 6f 72 next . ** recor
9000: 64 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 d is written to
9010: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 the following se
9020: 63 74 6f 72 20 28 6c 65 61 76 69 6e 67 20 61 20 ctor (leaving a
9030: 67 61 70 20 69 6e 20 74 68 65 20 66 69 6c 65 0a gap in the file.
9040: 20 20 2a 2a 20 74 68 61 74 20 77 69 6c 6c 20 62 ** that will b
9050: 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 66 69 6c e implicitly fil
9060: 6c 65 64 20 69 6e 20 62 79 20 74 68 65 20 4f 53 led in by the OS
9070: 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 48 6f 77 ).. **. ** How
9080: 65 76 65 72 20 69 74 20 68 61 73 20 62 65 65 6e ever it has been
9090: 20 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 74 discovered that
90a0: 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 on some systems
90b0: 20 74 68 69 73 20 70 61 74 74 65 72 6e 20 63 61 this pattern ca
90c0: 6e 20 0a 20 20 2a 2a 20 62 65 20 73 69 67 6e 69 n . ** be signi
90d0: 66 69 63 61 6e 74 6c 79 20 73 6c 6f 77 65 72 20 ficantly slower
90e0: 74 68 61 6e 20 63 6f 6e 74 69 67 75 6f 75 73 6c than contiguousl
90f0: 79 20 77 72 69 74 69 6e 67 20 64 61 74 61 20 74 y writing data t
9100: 6f 20 74 68 65 20 66 69 6c 65 2c 0a 20 20 2a 2a o the file,. **
9110: 20 65 76 65 6e 20 69 66 20 74 68 61 74 20 6d 65 even if that me
9120: 61 6e 73 20 65 78 70 6c 69 63 69 74 6c 79 20 77 ans explicitly w
9130: 72 69 74 69 6e 67 20 64 61 74 61 20 74 6f 20 74 riting data to t
9140: 68 65 20 62 6c 6f 63 6b 20 6f 66 20 0a 20 20 2a he block of . *
9150: 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 * (JOURNAL_HDR_S
9160: 5a 20 2d 20 32 38 29 20 62 79 74 65 73 20 74 68 Z - 28) bytes th
9170: 61 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 75 at will not be u
9180: 73 65 64 2e 20 53 6f 20 74 68 61 74 20 69 73 20 sed. So that is
9190: 77 68 61 74 0a 20 20 2a 2a 20 69 73 20 64 6f 6e what. ** is don
91a0: 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 e. . **. ** Th
91b0: 65 20 6c 6f 6f 70 20 69 73 20 72 65 71 75 69 72 e loop is requir
91c0: 65 64 20 68 65 72 65 20 69 6e 20 63 61 73 65 20 ed here in case
91d0: 74 68 65 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 the sector-size
91e0: 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 is larger than t
91f0: 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 he . ** databas
9200: 65 20 70 61 67 65 20 73 69 7a 65 2e 20 53 69 6e e page size. Sin
9210: 63 65 20 74 68 65 20 7a 48 65 61 64 65 72 20 62 ce the zHeader b
9220: 75 66 66 65 72 20 69 73 20 6f 6e 6c 79 20 50 61 uffer is only Pa
9230: 67 65 72 2e 70 61 67 65 53 69 7a 65 0a 20 20 2a ger.pageSize. *
9240: 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c * bytes in size,
9250: 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 63 more than one c
9260: 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 all to sqlite3Os
9270: 57 72 69 74 65 28 29 20 6d 61 79 20 62 65 20 72 Write() may be r
9280: 65 71 75 69 72 65 64 0a 20 20 2a 2a 20 74 6f 20 equired. ** to
9290: 70 6f 70 75 6c 61 74 65 20 74 68 65 20 65 6e 74 populate the ent
92a0: 69 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 ire journal head
92b0: 65 72 20 73 65 63 74 6f 72 2e 0a 20 20 2a 2f 20 er sector.. */
92c0: 0a 20 20 66 6f 72 28 6e 57 72 69 74 65 3d 30 3b . for(nWrite=0;
92d0: 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 26 26 rc==SQLITE_OK&&
92e0: 6e 57 72 69 74 65 3c 4a 4f 55 52 4e 41 4c 5f 48 nWrite<JOURNAL_H
92f0: 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 20 6e DR_SZ(pPager); n
9300: 57 72 69 74 65 2b 3d 6e 48 65 61 64 65 72 29 7b Write+=nHeader){
9310: 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a . IOTRACE(("J
9320: 48 44 52 20 25 70 20 25 6c 6c 64 20 25 64 5c 6e HDR %p %lld %d\n
9330: 22 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 ", pPager, pPage
9340: 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2c 20 6e r->journalHdr, n
9350: 48 65 61 64 65 72 29 29 0a 20 20 20 20 72 63 20 Header)). rc
9360: 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 = sqlite3OsWrite
9370: 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 48 (pPager->jfd, zH
9380: 65 61 64 65 72 2c 20 6e 48 65 61 64 65 72 2c 20 eader, nHeader,
9390: 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
93a0: 66 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ff);. assert(
93b0: 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
93c0: 48 64 72 20 3c 3d 20 70 50 61 67 65 72 2d 3e 6a Hdr <= pPager->j
93d0: 6f 75 72 6e 61 6c 4f 66 66 20 29 3b 0a 20 20 20 ournalOff );.
93e0: 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
93f0: 4f 66 66 20 2b 3d 20 6e 48 65 61 64 65 72 3b 0a Off += nHeader;.
9400: 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 }.. return rc
9410: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a ;.}../*.** The j
9420: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 ournal file must
9430: 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 be open when th
9440: 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20 41 20 is is called. A
9450: 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 journal header f
9460: 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 4e 41 4c 5f ile.** (JOURNAL_
9470: 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 69 73 HDR_SZ bytes) is
9480: 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 63 read from the c
9490: 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 urrent location
94a0: 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a in the journal.*
94b0: 2a 20 66 69 6c 65 2e 20 54 68 65 20 63 75 72 72 * file. The curr
94c0: 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 ent location in
94d0: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
94e0: 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20 is given by.**
94f0: 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
9500: 66 66 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 ff. See comments
9510: 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e 20 above function
9520: 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 writeJournalHdr(
9530: 29 20 66 6f 72 0a 2a 2a 20 61 20 64 65 73 63 72 ) for.** a descr
9540: 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 6a 6f iption of the jo
9550: 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 6f 72 urnal header for
9560: 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 mat..**.** If th
9570: 65 20 68 65 61 64 65 72 20 69 73 20 72 65 61 64 e header is read
9580: 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 2a successfully, *
9590: 70 4e 52 65 63 20 69 73 20 73 65 74 20 74 6f 20 pNRec is set to
95a0: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a the number of.**
95b0: 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 6f page records fo
95c0: 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 68 65 61 llowing this hea
95d0: 64 65 72 20 61 6e 64 20 2a 70 44 62 53 69 7a 65 der and *pDbSize
95e0: 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 73 is set to the s
95f0: 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 ize of the.** da
9600: 74 61 62 61 73 65 20 62 65 66 6f 72 65 20 74 68 tabase before th
9610: 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 e transaction be
9620: 67 61 6e 2c 20 69 6e 20 70 61 67 65 73 2e 20 41 gan, in pages. A
9630: 6c 73 6f 2c 20 70 50 61 67 65 72 2d 3e 63 6b 73 lso, pPager->cks
9640: 75 6d 49 6e 69 74 0a 2a 2a 20 69 73 20 73 65 74 umInit.** is set
9650: 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 72 65 to the value re
9660: 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 ad from the jour
9670: 6e 61 6c 20 68 65 61 64 65 72 2e 20 53 51 4c 49 nal header. SQLI
9680: 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
9690: 64 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73 d.** in this cas
96a0: 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
96b0: 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 journal header f
96c0: 69 6c 65 20 61 70 70 65 61 72 73 20 74 6f 20 62 ile appears to b
96d0: 65 20 63 6f 72 72 75 70 74 65 64 2c 20 53 51 4c e corrupted, SQL
96e0: 49 54 45 5f 44 4f 4e 45 20 69 73 0a 2a 2a 20 72 ITE_DONE is.** r
96f0: 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 4e 52 eturned and *pNR
9700: 65 63 20 61 6e 64 20 2a 50 44 62 53 69 7a 65 20 ec and *PDbSize
9710: 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 are undefined.
9720: 49 66 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 If JOURNAL_HDR_S
9730: 5a 20 62 79 74 65 73 0a 2a 2a 20 63 61 6e 6e 6f Z bytes.** canno
9740: 74 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74 t be read from t
9750: 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
9760: 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 an error code is
9770: 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 returned..*/.st
9780: 61 74 69 63 20 69 6e 74 20 72 65 61 64 4a 6f 75 atic int readJou
9790: 72 6e 61 6c 48 64 72 28 0a 20 20 50 61 67 65 72 rnalHdr(. Pager
97a0: 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 *pPager,
97b0: 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 /* Pager
97c0: 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 object */. int
97d0: 20 69 73 48 6f 74 2c 0a 20 20 69 36 34 20 6a 6f isHot,. i64 jo
97e0: 75 72 6e 61 6c 53 69 7a 65 2c 20 20 20 20 20 20 urnalSize,
97f0: 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
9800: 66 20 74 68 65 20 6f 70 65 6e 20 6a 6f 75 72 6e f the open journ
9810: 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 al file in bytes
9820: 20 2a 2f 0a 20 20 75 33 32 20 2a 70 4e 52 65 63 */. u32 *pNRec
9830: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
9840: 20 20 20 2f 2a 20 4f 55 54 3a 20 56 61 6c 75 65 /* OUT: Value
9850: 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6e read from the n
9860: 52 65 63 20 66 69 65 6c 64 20 2a 2f 0a 20 20 75 Rec field */. u
9870: 33 32 20 2a 70 44 62 53 69 7a 65 20 20 20 20 20 32 *pDbSize
9880: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
9890: 55 54 3a 20 56 61 6c 75 65 20 6f 66 20 6f 72 69 UT: Value of ori
98a0: 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 73 ginal database s
98b0: 69 7a 65 20 66 69 65 6c 64 20 2a 2f 0a 29 7b 0a ize field */.){.
98c0: 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
98d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
98e0: 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
98f0: 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
9900: 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20 20 20 aMagic[8];
9910: 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 /* A buffer to h
9920: 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 20 68 65 old the magic he
9930: 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20 69 48 ader */. i64 iH
9940: 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 drOff;
9950: 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
9960: 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 of journal head
9970: 65 72 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f er being read */
9980: 0a 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 .. assert( isOp
9990: 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
99a0: 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f 75 72 6e ); /* Journ
99b0: 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 al file must be
99c0: 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 2f 2a 20 41 open. */.. /* A
99d0: 64 76 61 6e 63 65 20 50 61 67 65 72 2e 6a 6f 75 dvance Pager.jou
99e0: 72 6e 61 6c 4f 66 66 20 74 6f 20 74 68 65 20 73 rnalOff to the s
99f0: 74 61 72 74 20 6f 66 20 74 68 65 20 6e 65 78 74 tart of the next
9a00: 20 73 65 63 74 6f 72 2e 20 49 66 20 74 68 65 0a sector. If the.
9a10: 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ** journal fil
9a20: 65 20 69 73 20 74 6f 6f 20 73 6d 61 6c 6c 20 66 e is too small f
9a30: 6f 72 20 74 68 65 72 65 20 74 6f 20 62 65 20 61 or there to be a
9a40: 20 68 65 61 64 65 72 20 73 74 6f 72 65 64 20 61 header stored a
9a50: 74 20 74 68 69 73 0a 20 20 2a 2a 20 70 6f 69 6e t this. ** poin
9a60: 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 t, return SQLITE
9a70: 5f 44 4f 4e 45 2e 0a 20 20 2a 2f 0a 20 20 70 50 _DONE.. */. pP
9a80: 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
9a90: 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 = journalHdrOff
9aa0: 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 69 set(pPager);. i
9ab0: 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
9ac0: 61 6c 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 48 44 alOff+JOURNAL_HD
9ad0: 52 5f 53 5a 28 70 50 61 67 65 72 29 20 3e 20 6a R_SZ(pPager) > j
9ae0: 6f 75 72 6e 61 6c 53 69 7a 65 20 29 7b 0a 20 20 ournalSize ){.
9af0: 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
9b00: 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 48 64 72 DONE;. }. iHdr
9b10: 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f Off = pPager->jo
9b20: 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 urnalOff;.. /*
9b30: 52 65 61 64 20 69 6e 20 74 68 65 20 66 69 72 73 Read in the firs
9b40: 74 20 38 20 62 79 74 65 73 20 6f 66 20 74 68 65 t 8 bytes of the
9b50: 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e journal header.
9b60: 20 49 66 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 If they do not
9b70: 6d 61 74 63 68 0a 20 20 2a 2a 20 74 68 65 20 20 match. ** the
9b80: 6d 61 67 69 63 20 73 74 72 69 6e 67 20 66 6f 75 magic string fou
9b90: 6e 64 20 61 74 20 74 68 65 20 73 74 61 72 74 20 nd at the start
9ba0: 6f 66 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 of each journal
9bb0: 68 65 61 64 65 72 2c 20 72 65 74 75 72 6e 0a 20 header, return.
9bc0: 20 2a 2a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e ** SQLITE_DONE.
9bd0: 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 If an IO error
9be0: 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 61 occurs, return a
9bf0: 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 4f 74 n error code. Ot
9c00: 68 65 72 77 69 73 65 2c 0a 20 20 2a 2a 20 70 72 herwise,. ** pr
9c10: 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 oceed.. */. if
9c20: 28 20 69 73 48 6f 74 20 7c 7c 20 69 48 64 72 4f ( isHot || iHdrO
9c30: 66 66 21 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 ff!=pPager->jour
9c40: 6e 61 6c 48 64 72 20 29 7b 0a 20 20 20 20 72 63 nalHdr ){. rc
9c50: 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 = sqlite3OsRead
9c60: 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61 4d (pPager->jfd, aM
9c70: 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4d 61 agic, sizeof(aMa
9c80: 67 69 63 29 2c 20 69 48 64 72 4f 66 66 29 3b 0a gic), iHdrOff);.
9c90: 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
9ca0: 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
9cb0: 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d }. if( mem
9cc0: 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 cmp(aMagic, aJou
9cd0: 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f rnalMagic, sizeo
9ce0: 66 28 61 4d 61 67 69 63 29 29 21 3d 30 20 29 7b f(aMagic))!=0 ){
9cf0: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
9d00: 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d LITE_DONE;. }
9d10: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 . }.. /* Read
9d20: 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20 the first three
9d30: 33 32 2d 62 69 74 20 66 69 65 6c 64 73 20 6f 66 32-bit fields of
9d40: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 the journal hea
9d50: 64 65 72 3a 20 54 68 65 20 6e 52 65 63 0a 20 20 der: The nRec.
9d60: 2a 2a 20 66 69 65 6c 64 2c 20 74 68 65 20 63 68 ** field, the ch
9d70: 65 63 6b 73 75 6d 2d 69 6e 69 74 69 61 6c 69 7a ecksum-initializ
9d80: 65 72 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 er and the datab
9d90: 61 73 65 20 73 69 7a 65 20 61 74 20 74 68 65 20 ase size at the
9da0: 73 74 61 72 74 0a 20 20 2a 2a 20 6f 66 20 74 68 start. ** of th
9db0: 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 52 e transaction. R
9dc0: 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 eturn an error c
9dd0: 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 ode if anything
9de0: 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 20 20 2a 2f goes wrong.. */
9df0: 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b . if( SQLITE_OK
9e00: 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 !=(rc = read32bi
9e10: 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ts(pPager->jfd,
9e20: 69 48 64 72 4f 66 66 2b 38 2c 20 70 4e 52 65 63 iHdrOff+8, pNRec
9e30: 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f )). || SQLITE_
9e40: 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 OK!=(rc = read32
9e50: 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
9e60: 2c 20 69 48 64 72 4f 66 66 2b 31 32 2c 20 26 70 , iHdrOff+12, &p
9e70: 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 Pager->cksumInit
9e80: 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f )). || SQLITE_
9e90: 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 OK!=(rc = read32
9ea0: 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
9eb0: 2c 20 69 48 64 72 4f 66 66 2b 31 36 2c 20 70 44 , iHdrOff+16, pD
9ec0: 62 53 69 7a 65 29 29 0a 20 20 29 7b 0a 20 20 20 bSize)). ){.
9ed0: 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
9ee0: 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a . if( pPager->j
9ef0: 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a ournalOff==0 ){.
9f00: 20 20 20 20 75 33 32 20 69 50 61 67 65 53 69 7a u32 iPageSiz
9f10: 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
9f20: 20 2f 2a 20 50 61 67 65 2d 73 69 7a 65 20 66 69 /* Page-size fi
9f30: 65 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 eld of journal h
9f40: 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 33 32 eader */. u32
9f50: 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 iSectorSize;
9f60: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 /* Sec
9f70: 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f tor-size field o
9f80: 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 f journal header
9f90: 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 50 61 67 */. u16 iPag
9fa0: 65 53 69 7a 65 31 36 3b 20 20 20 20 20 20 20 20 eSize16;
9fb0: 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 /* Copy of
9fc0: 69 50 61 67 65 53 69 7a 65 20 69 6e 20 31 36 2d iPageSize in 16-
9fd0: 62 69 74 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a bit variable */.
9fe0: 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 . /* Read the
9ff0: 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 page-size and s
a000: 65 63 74 6f 72 2d 73 69 7a 65 20 6a 6f 75 72 6e ector-size journ
a010: 61 6c 20 68 65 61 64 65 72 20 66 69 65 6c 64 73 al header fields
a020: 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c . */. if( SQL
a030: 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 ITE_OK!=(rc = re
a040: 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d ad32bits(pPager-
a050: 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 32 30 >jfd, iHdrOff+20
a060: 2c 20 26 69 53 65 63 74 6f 72 53 69 7a 65 29 29 , &iSectorSize))
a070: 0a 20 20 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f . || SQLITE_
a080: 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 OK!=(rc = read32
a090: 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
a0a0: 2c 20 69 48 64 72 4f 66 66 2b 32 34 2c 20 26 69 , iHdrOff+24, &i
a0b0: 50 61 67 65 53 69 7a 65 29 29 0a 20 20 20 20 29 PageSize)). )
a0c0: 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 {. return r
a0d0: 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a c;. }.. /*
a0e0: 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 Check that the
a0f0: 76 61 6c 75 65 73 20 72 65 61 64 20 66 72 6f 6d values read from
a100: 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 the page-size a
a110: 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 66 nd sector-size f
a120: 69 65 6c 64 73 0a 20 20 20 20 2a 2a 20 61 72 65 ields. ** are
a130: 20 77 69 74 68 69 6e 20 72 61 6e 67 65 2e 20 54 within range. T
a140: 6f 20 62 65 20 27 69 6e 20 72 61 6e 67 65 27 2c o be 'in range',
a150: 20 62 6f 74 68 20 76 61 6c 75 65 73 20 6e 65 65 both values nee
a160: 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 72 0a d to be a power.
a170: 20 20 20 20 2a 2a 20 6f 66 20 74 77 6f 20 67 72 ** of two gr
a180: 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 eater than or eq
a190: 75 61 6c 20 74 6f 20 35 31 32 20 6f 72 20 33 32 ual to 512 or 32
a1a0: 2c 20 61 6e 64 20 6e 6f 74 20 67 72 65 61 74 65 , and not greate
a1b0: 72 20 74 68 61 6e 20 74 68 65 69 72 20 0a 20 20 r than their .
a1c0: 20 20 2a 2a 20 72 65 73 70 65 63 74 69 76 65 20 ** respective
a1d0: 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 6d 61 78 compile time max
a1e0: 69 6d 75 6d 20 6c 69 6d 69 74 73 2e 0a 20 20 20 imum limits..
a1f0: 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 50 61 67 */. if( iPag
a200: 65 53 69 7a 65 3c 35 31 32 20 20 20 20 20 20 20 eSize<512
a210: 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 69 53 || iS
a220: 65 63 74 6f 72 53 69 7a 65 3c 33 32 0a 20 20 20 ectorSize<32.
a230: 20 20 7c 7c 20 69 50 61 67 65 53 69 7a 65 3e 53 || iPageSize>S
a240: 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 QLITE_MAX_PAGE_S
a250: 49 5a 45 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 IZE || iSectorSi
a260: 7a 65 3e 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 ze>MAX_SECTOR_SI
a270: 5a 45 0a 20 20 20 20 20 7c 7c 20 28 28 69 50 61 ZE. || ((iPa
a280: 67 65 53 69 7a 65 2d 31 29 26 69 50 61 67 65 53 geSize-1)&iPageS
a290: 69 7a 65 29 21 3d 30 20 20 20 7c 7c 20 28 28 69 ize)!=0 || ((i
a2a0: 53 65 63 74 6f 72 53 69 7a 65 2d 31 29 26 69 53 SectorSize-1)&iS
a2b0: 65 63 74 6f 72 53 69 7a 65 29 21 3d 30 20 0a 20 ectorSize)!=0 .
a2c0: 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 ){. /* I
a2d0: 66 20 74 68 65 20 65 69 74 68 65 72 20 74 68 65 f the either the
a2e0: 20 70 61 67 65 2d 73 69 7a 65 20 6f 72 20 73 65 page-size or se
a2f0: 63 74 6f 72 2d 73 69 7a 65 20 69 6e 20 74 68 65 ctor-size in the
a300: 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 journal-header
a310: 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 69 6e 76 is . ** inv
a320: 61 6c 69 64 2c 20 74 68 65 6e 20 74 68 65 20 70 alid, then the p
a330: 72 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f 74 rocess that wrot
a340: 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 e the journal-he
a350: 61 64 65 72 20 6d 75 73 74 20 68 61 76 65 20 0a ader must have .
a360: 20 20 20 20 20 20 2a 2a 20 63 72 61 73 68 65 64 ** crashed
a370: 20 62 65 66 6f 72 65 20 74 68 65 20 68 65 61 64 before the head
a380: 65 72 20 77 61 73 20 73 79 6e 63 65 64 2e 20 49 er was synced. I
a390: 6e 20 74 68 69 73 20 63 61 73 65 20 73 74 6f 70 n this case stop
a3a0: 20 72 65 61 64 69 6e 67 20 0a 20 20 20 20 20 20 reading .
a3b0: 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ** the journal f
a3c0: 69 6c 65 20 68 65 72 65 2e 0a 20 20 20 20 20 20 ile here..
a3d0: 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 */. return
a3e0: 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 SQLITE_DONE;.
a3f0: 20 7d 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 }.. /* Updat
a400: 65 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 e the page-size
a410: 74 6f 20 6d 61 74 63 68 20 74 68 65 20 76 61 6c to match the val
a420: 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 ue read from the
a430: 20 6a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 2a journal. . *
a440: 2a 20 55 73 65 20 61 20 74 65 73 74 63 61 73 65 * Use a testcase
a450: 28 29 20 6d 61 63 72 6f 20 74 6f 20 6d 61 6b 65 () macro to make
a460: 20 73 75 72 65 20 74 68 61 74 20 6d 61 6c 6c 6f sure that mallo
a470: 63 20 66 61 69 6c 75 72 65 20 77 69 74 68 69 6e c failure within
a480: 20 0a 20 20 20 20 2a 2a 20 50 61 67 65 72 53 65 . ** PagerSe
a490: 74 50 61 67 65 73 69 7a 65 28 29 20 69 73 20 74 tPagesize() is t
a4a0: 65 73 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 ested.. */.
a4b0: 20 20 69 50 61 67 65 53 69 7a 65 31 36 20 3d 20 iPageSize16 =
a4c0: 28 75 31 36 29 69 50 61 67 65 53 69 7a 65 3b 0a (u16)iPageSize;.
a4d0: 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
a4e0: 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 PagerSetPagesize
a4f0: 28 70 50 61 67 65 72 2c 20 26 69 50 61 67 65 53 (pPager, &iPageS
a500: 69 7a 65 31 36 2c 20 2d 31 29 3b 0a 20 20 20 20 ize16, -1);.
a510: 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 testcase( rc!=SQ
a520: 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 61 LITE_OK );. a
a530: 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
a540: 45 5f 4f 4b 20 7c 7c 20 69 50 61 67 65 53 69 7a E_OK || iPageSiz
a550: 65 31 36 3d 3d 28 75 31 36 29 69 50 61 67 65 53 e16==(u16)iPageS
a560: 69 7a 65 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 55 ize );.. /* U
a570: 70 64 61 74 65 20 74 68 65 20 61 73 73 75 6d 65 pdate the assume
a580: 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 74 6f d sector-size to
a590: 20 6d 61 74 63 68 20 74 68 65 20 76 61 6c 75 65 match the value
a5a0: 20 75 73 65 64 20 62 79 20 0a 20 20 20 20 2a 2a used by . **
a5b0: 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 61 the process tha
a5c0: 74 20 63 72 65 61 74 65 64 20 74 68 69 73 20 6a t created this j
a5d0: 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 69 73 20 ournal. If this
a5e0: 6a 6f 75 72 6e 61 6c 20 77 61 73 0a 20 20 20 20 journal was.
a5f0: 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 61 20 ** created by a
a600: 70 72 6f 63 65 73 73 20 6f 74 68 65 72 20 74 68 process other th
a610: 61 6e 20 74 68 69 73 20 6f 6e 65 2c 20 74 68 65 an this one, the
a620: 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 20 n this routine.
a630: 20 20 20 2a 2a 20 69 73 20 62 65 69 6e 67 20 63 ** is being c
a640: 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 alled from withi
a650: 6e 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b n pager_playback
a660: 28 29 2e 20 54 68 65 20 6c 6f 63 61 6c 20 76 61 (). The local va
a670: 6c 75 65 0a 20 20 20 20 2a 2a 20 6f 66 20 50 61 lue. ** of Pa
a680: 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 69 ger.sectorSize i
a690: 73 20 72 65 73 74 6f 72 65 64 20 61 74 20 74 68 s restored at th
a6a0: 65 20 65 6e 64 20 6f 66 20 74 68 61 74 20 72 6f e end of that ro
a6b0: 75 74 69 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 utine.. */.
a6c0: 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 pPager->sector
a6d0: 53 69 7a 65 20 3d 20 69 53 65 63 74 6f 72 53 69 Size = iSectorSi
a6e0: 7a 65 3b 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 ze;. }.. pPage
a6f0: 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d r->journalOff +=
a700: 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 JOURNAL_HDR_SZ(
a710: 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72 pPager);. retur
a720: 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 n rc;.}.../*.**
a730: 57 72 69 74 65 20 74 68 65 20 73 75 70 70 6c 69 Write the suppli
a740: 65 64 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 ed master journa
a750: 6c 20 6e 61 6d 65 20 69 6e 74 6f 20 74 68 65 20 l name into the
a760: 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 journal file for
a770: 20 70 61 67 65 72 0a 2a 2a 20 70 50 61 67 65 72 pager.** pPager
a780: 20 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 at the current
a790: 6c 6f 63 61 74 69 6f 6e 2e 20 54 68 65 20 6d 61 location. The ma
a7a0: 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster journal nam
a7b0: 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 6c 61 e must be the la
a7c0: 73 74 0a 2a 2a 20 74 68 69 6e 67 20 77 72 69 74 st.** thing writ
a7d0: 74 65 6e 20 74 6f 20 61 20 6a 6f 75 72 6e 61 6c ten to a journal
a7e0: 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 70 61 file. If the pa
a7f0: 67 65 72 20 69 73 20 69 6e 20 66 75 6c 6c 2d 73 ger is in full-s
a800: 79 6e 63 20 6d 6f 64 65 2c 20 74 68 65 0a 2a 2a ync mode, the.**
a810: 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 journal file de
a820: 73 63 72 69 70 74 6f 72 20 69 73 20 61 64 76 61 scriptor is adva
a830: 6e 63 65 64 20 74 6f 20 74 68 65 20 6e 65 78 74 nced to the next
a840: 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 sector boundary
a850: 20 62 65 66 6f 72 65 0a 2a 2a 20 61 6e 79 74 68 before.** anyth
a860: 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e 2e 20 ing is written.
a870: 54 68 65 20 66 6f 72 6d 61 74 20 69 73 3a 0a 2a The format is:.*
a880: 2a 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 73 *.** + 4 bytes
a890: 3a 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 2e : PAGER_MJ_PGNO.
a8a0: 0a 2a 2a 20 20 20 2b 20 4e 20 62 79 74 65 73 3a .** + N bytes:
a8b0: 20 4d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 Master journal
a8c0: 66 69 6c 65 6e 61 6d 65 20 69 6e 20 75 74 66 2d filename in utf-
a8d0: 38 2e 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 8..** + 4 byte
a8e0: 73 3a 20 4e 20 28 6c 65 6e 67 74 68 20 6f 66 20 s: N (length of
a8f0: 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
a900: 61 6d 65 20 69 6e 20 62 79 74 65 73 2c 20 6e 6f ame in bytes, no
a910: 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 29 nul-terminator)
a920: 2e 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 73 ..** + 4 bytes
a930: 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c : Master journal
a940: 20 6e 61 6d 65 20 63 68 65 63 6b 73 75 6d 2e 0a name checksum..
a950: 2a 2a 20 20 20 2b 20 38 20 62 79 74 65 73 3a 20 ** + 8 bytes:
a960: 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 2e aJournalMagic[].
a970: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 73 74 65 .**.** The maste
a980: 72 20 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 63 r journal page c
a990: 68 65 63 6b 73 75 6d 20 69 73 20 74 68 65 20 73 hecksum is the s
a9a0: 75 6d 20 6f 66 20 74 68 65 20 62 79 74 65 73 20 um of the bytes
a9b0: 69 6e 20 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a in the master.**
a9c0: 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2c 20 77 journal name, w
a9d0: 68 65 72 65 20 65 61 63 68 20 62 79 74 65 20 69 here each byte i
a9e0: 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 s interpreted as
a9f0: 20 61 20 73 69 67 6e 65 64 20 38 2d 62 69 74 20 a signed 8-bit
aa00: 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 integer..**.** I
aa10: 66 20 7a 4d 61 73 74 65 72 20 69 73 20 61 20 4e f zMaster is a N
aa20: 55 4c 4c 20 70 6f 69 6e 74 65 72 20 28 6f 63 63 ULL pointer (occ
aa30: 75 72 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 urs for a single
aa40: 20 64 61 74 61 62 61 73 65 20 74 72 61 6e 73 61 database transa
aa50: 63 74 69 6f 6e 29 2c 20 0a 2a 2a 20 74 68 69 73 ction), .** this
aa60: 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 call is a no-op
aa70: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
aa80: 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e writeMasterJourn
aa90: 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 al(Pager *pPager
aaa0: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d , const char *zM
aab0: 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 aster){. int rc
aac0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
aad0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
aae0: 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 turn code */. i
aaf0: 6e 74 20 6e 4d 61 73 74 65 72 3b 20 20 20 20 20 nt nMaster;
ab00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ab10: 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 /* Length of str
ab20: 69 6e 67 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 20 ing zMaster */.
ab30: 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20 20 i64 iHdrOff;
ab40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ab50: 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 /* Offset of h
ab60: 65 61 64 65 72 20 69 6e 20 6a 6f 75 72 6e 61 6c eader in journal
ab70: 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a file */. i64 j
ab80: 72 6e 6c 53 69 7a 65 3b 20 20 20 20 20 20 20 20 rnlSize;
ab90: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
aba0: 69 7a 65 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 ize of journal f
abb0: 69 6c 65 20 6f 6e 20 64 69 73 6b 20 2a 2f 0a 20 ile on disk */.
abc0: 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 30 3b 20 u32 cksum = 0;
abd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
abe0: 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 6f 66 /* Checksum of
abf0: 20 73 74 72 69 6e 67 20 7a 4d 61 73 74 65 72 20 string zMaster
ac00: 2a 2f 0a 0a 20 20 69 66 28 20 21 7a 4d 61 73 74 */.. if( !zMast
ac10: 65 72 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 65 er || pPager->se
ac20: 74 4d 61 73 74 65 72 0a 20 20 20 7c 7c 20 70 50 tMaster. || pP
ac30: 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
ac40: 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
ac50: 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 MODE_MEMORY .
ac60: 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e || pPager->journ
ac70: 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
ac80: 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 URNALMODE_OFF .
ac90: 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
aca0: 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
acb0: 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 pPager->setMaste
acc0: 72 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 28 r = 1;. assert(
acd0: 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
ace0: 6a 66 64 29 20 29 3b 0a 20 20 61 73 73 65 72 74 jfd) );. assert
acf0: 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
ad00: 6c 48 64 72 20 3c 3d 20 70 50 61 67 65 72 2d 3e lHdr <= pPager->
ad10: 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 3b 0a 0a 20 journalOff );..
ad20: 20 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68 /* Calculate th
ad30: 65 20 6c 65 6e 67 74 68 20 69 6e 20 62 79 74 65 e length in byte
ad40: 73 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b 73 s and the checks
ad50: 75 6d 20 6f 66 20 7a 4d 61 73 74 65 72 20 2a 2f um of zMaster */
ad60: 0a 20 20 66 6f 72 28 6e 4d 61 73 74 65 72 3d 30 . for(nMaster=0
ad70: 3b 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 74 65 ; zMaster[nMaste
ad80: 72 5d 3b 20 6e 4d 61 73 74 65 72 2b 2b 29 7b 0a r]; nMaster++){.
ad90: 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 7a 4d 61 cksum += zMa
ada0: 73 74 65 72 5b 6e 4d 61 73 74 65 72 5d 3b 0a 20 ster[nMaster];.
adb0: 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 69 6e 20 66 }.. /* If in f
adc0: 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 61 ull-sync mode, a
add0: 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 dvance to the ne
ade0: 78 74 20 64 69 73 6b 20 73 65 63 74 6f 72 20 62 xt disk sector b
adf0: 65 66 6f 72 65 20 77 72 69 74 69 6e 67 0a 20 20 efore writing.
ae00: 2a 2a 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f ** the master jo
ae10: 75 72 6e 61 6c 20 6e 61 6d 65 2e 20 54 68 69 73 urnal name. This
ae20: 20 69 73 20 69 6e 20 63 61 73 65 20 74 68 65 20 is in case the
ae30: 70 72 65 76 69 6f 75 73 20 70 61 67 65 20 77 72 previous page wr
ae40: 69 74 74 65 6e 20 74 6f 0a 20 20 2a 2a 20 74 68 itten to. ** th
ae50: 65 20 6a 6f 75 72 6e 61 6c 20 68 61 73 20 61 6c e journal has al
ae60: 72 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 ready been synce
ae70: 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 d.. */. if( pP
ae80: 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 29 ager->fullSync )
ae90: 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f {. pPager->jo
aea0: 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e urnalOff = journ
aeb0: 61 6c 48 64 72 4f 66 66 73 65 74 28 70 50 61 67 alHdrOffset(pPag
aec0: 65 72 29 3b 0a 20 20 7d 0a 20 20 69 48 64 72 4f er);. }. iHdrO
aed0: 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 ff = pPager->jou
aee0: 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 57 rnalOff;.. /* W
aef0: 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 rite the master
af00: 6a 6f 75 72 6e 61 6c 20 64 61 74 61 20 74 6f 20 journal data to
af10: 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a the end of the j
af20: 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 0a ournal file. If.
af30: 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 ** an error oc
af40: 63 75 72 73 2c 20 72 65 74 75 72 6e 20 74 68 65 curs, return the
af50: 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 74 error code to t
af60: 68 65 20 63 61 6c 6c 65 72 2e 0a 20 20 2a 2f 0a he caller.. */.
af70: 20 20 69 66 28 20 28 30 20 21 3d 20 28 72 63 20 if( (0 != (rc
af80: 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 = write32bits(pP
af90: 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f ager->jfd, iHdrO
afa0: 66 66 2c 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e ff, PAGER_MJ_PGN
afb0: 4f 28 70 50 61 67 65 72 29 29 29 29 0a 20 20 20 O(pPager)))).
afc0: 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 || (0 != (rc = s
afd0: 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 qlite3OsWrite(pP
afe0: 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 ager->jfd, zMast
aff0: 65 72 2c 20 6e 4d 61 73 74 65 72 2c 20 69 48 64 er, nMaster, iHd
b000: 72 4f 66 66 2b 34 29 29 29 0a 20 20 20 7c 7c 20 rOff+4))). ||
b010: 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74 (0 != (rc = writ
b020: 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e e32bits(pPager->
b030: 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e jfd, iHdrOff+4+n
b040: 4d 61 73 74 65 72 2c 20 6e 4d 61 73 74 65 72 29 Master, nMaster)
b050: 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 )). || (0 != (
b060: 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 rc = write32bits
b070: 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 (pPager->jfd, iH
b080: 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2b drOff+4+nMaster+
b090: 34 2c 20 63 6b 73 75 6d 29 29 29 0a 20 20 20 7c 4, cksum))). |
b0a0: 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 | (0 != (rc = sq
b0b0: 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 lite3OsWrite(pPa
b0c0: 67 65 72 2d 3e 6a 66 64 2c 20 61 4a 6f 75 72 6e ger->jfd, aJourn
b0d0: 61 6c 4d 61 67 69 63 2c 20 38 2c 20 69 48 64 72 alMagic, 8, iHdr
b0e0: 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 38 29 Off+4+nMaster+8)
b0f0: 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 )). ){. retu
b100: 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 61 rn rc;. }. pPa
b110: 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
b120: 2b 3d 20 28 6e 4d 61 73 74 65 72 2b 32 30 29 3b += (nMaster+20);
b130: 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 . pPager->needS
b140: 79 6e 63 20 3d 20 21 70 50 61 67 65 72 2d 3e 6e ync = !pPager->n
b150: 6f 53 79 6e 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 oSync;.. /* If
b160: 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
b170: 70 65 72 69 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 peristent-journa
b180: 6c 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 l mode, then the
b190: 20 70 68 79 73 69 63 61 6c 20 0a 20 20 2a 2a 20 physical . **
b1a0: 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 6d 61 79 journal-file may
b1b0: 20 65 78 74 65 6e 64 20 70 61 73 74 20 74 68 65 extend past the
b1c0: 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 74 end of the mast
b1d0: 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a er-journal name.
b1e0: 20 20 2a 2a 20 61 6e 64 20 38 20 62 79 74 65 73 ** and 8 bytes
b1f0: 20 6f 66 20 6d 61 67 69 63 20 64 61 74 61 20 6a of magic data j
b200: 75 73 74 20 77 72 69 74 74 65 6e 20 74 6f 20 74 ust written to t
b210: 68 65 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 he file. This is
b220: 20 0a 20 20 2a 2a 20 64 61 6e 67 65 72 6f 75 73 . ** dangerous
b230: 20 62 65 63 61 75 73 65 20 74 68 65 20 63 6f 64 because the cod
b240: 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 e to rollback a
b250: 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 hot-journal file
b260: 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 . ** will not b
b270: 65 20 61 62 6c 65 20 74 6f 20 66 69 6e 64 20 74 e able to find t
b280: 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 he master-journa
b290: 6c 20 6e 61 6d 65 20 74 6f 20 64 65 74 65 72 6d l name to determ
b2a0: 69 6e 65 20 0a 20 20 2a 2a 20 77 68 65 74 68 65 ine . ** whethe
b2b0: 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f 75 r or not the jou
b2c0: 72 6e 61 6c 20 69 73 20 68 6f 74 2e 20 0a 20 20 rnal is hot. .
b2d0: 2a 2a 0a 20 20 2a 2a 20 45 61 73 69 65 73 74 20 **. ** Easiest
b2e0: 74 68 69 6e 67 20 74 6f 20 64 6f 20 69 6e 20 74 thing to do in t
b2f0: 68 69 73 20 73 63 65 6e 61 72 69 6f 20 69 73 20 his scenario is
b300: 74 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 to truncate the
b310: 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 journal . ** fi
b320: 6c 65 20 74 6f 20 74 68 65 20 72 65 71 75 69 72 le to the requir
b330: 65 64 20 73 69 7a 65 2e 0a 20 20 2a 2f 20 0a 20 ed size.. */ .
b340: 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d if( SQLITE_OK==
b350: 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 (rc = sqlite3OsF
b360: 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e ileSize(pPager->
b370: 6a 66 64 2c 20 26 6a 72 6e 6c 53 69 7a 65 29 29 jfd, &jrnlSize))
b380: 0a 20 20 20 26 26 20 6a 72 6e 6c 53 69 7a 65 3e . && jrnlSize>
b390: 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
b3a0: 66 66 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d ff. ){. rc =
b3b0: 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 sqlite3OsTrunca
b3c0: 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 te(pPager->jfd,
b3d0: 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
b3e0: 66 66 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ff);. }. retur
b3f0: 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 n rc;.}../*.** F
b400: 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20 74 68 ind a page in th
b410: 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69 76 e hash table giv
b420: 65 6e 20 69 74 73 20 70 61 67 65 20 6e 75 6d 62 en its page numb
b430: 65 72 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 er. Return.** a
b440: 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 pointer to the p
b450: 61 67 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 age or NULL if t
b460: 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 67 he requested pag
b470: 65 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 6c 72 e is not .** alr
b480: 65 61 64 79 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a eady in memory..
b490: 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 */.static PgHdr
b4a0: 2a 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 50 61 *pager_lookup(Pa
b4b0: 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e ger *pPager, Pgn
b4c0: 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 o pgno){. PgHdr
b4d0: 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 *p;
b4e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
b4f0: 52 65 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f 0a Return value */.
b500: 0a 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 . /* It is not
b510: 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 61 20 63 possible for a c
b520: 61 6c 6c 20 74 6f 20 50 63 61 63 68 65 46 65 74 all to PcacheFet
b530: 63 68 28 29 20 77 69 74 68 20 63 72 65 61 74 65 ch() with create
b540: 46 6c 61 67 3d 3d 30 20 74 6f 0a 20 20 2a 2a 20 Flag==0 to. **
b550: 66 61 69 6c 2c 20 73 69 6e 63 65 20 6e 6f 20 61 fail, since no a
b560: 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 ttempt to alloca
b570: 74 65 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 te dynamic memor
b580: 79 20 77 69 6c 6c 20 62 65 20 6d 61 64 65 2e 0a y will be made..
b590: 20 20 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71 6c */. (void)sql
b5a0: 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28 ite3PcacheFetch(
b5b0: 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c pPager->pPCache,
b5c0: 20 70 67 6e 6f 2c 20 30 2c 20 26 70 29 3b 0a 20 pgno, 0, &p);.
b5d0: 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
b5e0: 0a 2a 2a 20 55 6e 6c 65 73 73 20 74 68 65 20 70 .** Unless the p
b5f0: 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f 72 ager is in error
b600: 2d 73 74 61 74 65 2c 20 64 69 73 63 61 72 64 20 -state, discard
b610: 61 6c 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 all in-memory pa
b620: 67 65 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20 70 ges. If.** the p
b630: 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f 72 ager is in error
b640: 2d 73 74 61 74 65 2c 20 74 68 65 6e 20 74 68 69 -state, then thi
b650: 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f s call is a no-o
b660: 70 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 57 p..**.** TODO: W
b670: 68 79 20 63 61 6e 20 77 65 20 6e 6f 74 20 72 65 hy can we not re
b680: 73 65 74 20 74 68 65 20 70 61 67 65 72 20 77 68 set the pager wh
b690: 69 6c 65 20 69 6e 20 65 72 72 6f 72 20 73 74 61 ile in error sta
b6a0: 74 65 3f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f te?.*/.static vo
b6b0: 69 64 20 70 61 67 65 72 5f 72 65 73 65 74 28 50 id pager_reset(P
b6c0: 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
b6d0: 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d if( SQLITE_OK==
b6e0: 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
b6f0: 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 61 ){. sqlite3Ba
b700: 63 6b 75 70 52 65 73 74 61 72 74 28 70 50 61 67 ckupRestart(pPag
b710: 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b 0a 20 20 er->pBackup);.
b720: 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 sqlite3PcacheC
b730: 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70 50 43 lear(pPager->pPC
b740: 61 63 68 65 29 3b 0a 20 20 20 20 70 50 61 67 65 ache);. pPage
b750: 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d r->dbSizeValid =
b760: 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 0;. }.}../*.**
b770: 20 46 72 65 65 20 61 6c 6c 20 73 74 72 75 63 74 Free all struct
b780: 75 72 65 73 20 69 6e 20 74 68 65 20 50 61 67 65 ures in the Page
b790: 72 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 r.aSavepoint[] a
b7a0: 72 72 61 79 20 61 6e 64 20 73 65 74 20 62 6f 74 rray and set bot
b7b0: 68 0a 2a 2a 20 50 61 67 65 72 2e 61 53 61 76 65 h.** Pager.aSave
b7c0: 70 6f 69 6e 74 20 61 6e 64 20 50 61 67 65 72 2e point and Pager.
b7d0: 6e 53 61 76 65 70 6f 69 6e 74 20 74 6f 20 7a 65 nSavepoint to ze
b7e0: 72 6f 2e 20 43 6c 6f 73 65 20 74 68 65 20 73 75 ro. Close the su
b7f0: 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 66 20 b-journal.** if
b800: 69 74 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 it is open and t
b810: 68 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 he pager is not
b820: 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 in exclusive mod
b830: 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
b840: 64 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 d releaseAllSave
b850: 70 6f 69 6e 74 73 28 50 61 67 65 72 20 2a 70 50 points(Pager *pP
b860: 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 69 69 3b ager){. int ii;
b870: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
b880: 2a 20 49 74 65 72 61 74 6f 72 20 66 6f 72 20 6c * Iterator for l
b890: 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20 50 ooping through P
b8a0: 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 ager.aSavepoint
b8b0: 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 */. for(ii=0; i
b8c0: 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 i<pPager->nSavep
b8d0: 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 oint; ii++){.
b8e0: 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 sqlite3BitvecDe
b8f0: 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 61 53 stroy(pPager->aS
b900: 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49 6e avepoint[ii].pIn
b910: 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a Savepoint);. }.
b920: 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65 if( !pPager->e
b930: 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 xclusiveMode ||
b940: 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 sqlite3IsMemJour
b950: 6e 61 6c 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 nal(pPager->sjfd
b960: 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) ){. sqlite3
b970: 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e OsClose(pPager->
b980: 73 6a 66 64 29 3b 0a 20 20 7d 0a 20 20 73 71 6c sjfd);. }. sql
b990: 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65 72 ite3_free(pPager
b9a0: 2d 3e 61 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 ->aSavepoint);.
b9b0: 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f pPager->aSavepo
b9c0: 69 6e 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 int = 0;. pPage
b9d0: 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 r->nSavepoint =
b9e0: 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53 75 0;. pPager->nSu
b9f0: 62 52 65 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a bRec = 0;.}../*.
ba00: 2a 2a 20 53 65 74 20 74 68 65 20 62 69 74 20 6e ** Set the bit n
ba10: 75 6d 62 65 72 20 70 67 6e 6f 20 69 6e 20 74 68 umber pgno in th
ba20: 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 e PagerSavepoint
ba30: 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 0a 2a .pInSavepoint .*
ba40: 2a 20 62 69 74 76 65 63 73 20 6f 66 20 61 6c 6c * bitvecs of all
ba50: 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 open savepoints
ba60: 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f . Return SQLITE_
ba70: 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75 6c OK if successful
ba80: 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f .** or SQLITE_NO
ba90: 4d 45 4d 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 MEM if a malloc
baa0: 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 2e 0a failure occurs..
bab0: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 */.static int ad
bac0: 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 dToSavepointBitv
bad0: 65 63 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 ecs(Pager *pPage
bae0: 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 r, Pgno pgno){.
baf0: 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 int ii;
bb00: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
bb10: 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
bb20: 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
bb30: 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 OK; /* Res
bb40: 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 66 ult code */.. f
bb50: 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 67 or(ii=0; ii<pPag
bb60: 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 er->nSavepoint;
bb70: 69 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 72 ii++){. Pager
bb80: 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 26 Savepoint *p = &
bb90: 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 pPager->aSavepoi
bba0: 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 nt[ii];. if(
bbb0: 70 67 6e 6f 3c 3d 70 2d 3e 6e 4f 72 69 67 20 29 pgno<=p->nOrig )
bbc0: 7b 0a 20 20 20 20 20 20 72 63 20 7c 3d 20 73 71 {. rc |= sq
bbd0: 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70 lite3BitvecSet(p
bbe0: 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20 ->pInSavepoint,
bbf0: 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 74 65 73 pgno);. tes
bc00: 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 tcase( rc==SQLIT
bc10: 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 E_NOMEM );.
bc20: 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
bc30: 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 ITE_OK || rc==SQ
bc40: 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 LITE_NOMEM );.
bc50: 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
bc60: 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
bc70: 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 69 turn true if thi
bc80: 73 20 70 61 67 65 72 20 75 73 65 73 20 61 20 77 s pager uses a w
bc90: 72 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 20 69 rite-ahead log i
bca0: 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 75 73 nstead of the us
bcb0: 75 61 6c 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 ual.** rollback
bcc0: 6a 6f 75 72 6e 61 6c 2e 20 4f 74 68 65 72 77 69 journal. Otherwi
bcd0: 73 65 20 66 61 6c 73 65 2e 0a 2a 2f 0a 23 69 66 se false..*/.#if
bce0: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
bcf0: 5f 57 41 4c 0a 73 74 61 74 69 63 20 69 6e 74 20 _WAL.static int
bd00: 70 61 67 65 72 55 73 65 57 61 6c 28 50 61 67 65 pagerUseWal(Page
bd10: 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 r *pPager){. re
bd20: 74 75 72 6e 20 28 70 50 61 67 65 72 2d 3e 70 57 turn (pPager->pW
bd30: 61 6c 21 3d 30 29 3b 0a 7d 0a 23 65 6c 73 65 0a al!=0);.}.#else.
bd40: 23 20 64 65 66 69 6e 65 20 70 61 67 65 72 55 73 # define pagerUs
bd50: 65 57 61 6c 28 78 29 20 30 0a 23 20 64 65 66 69 eWal(x) 0.# defi
bd60: 6e 65 20 70 61 67 65 72 52 6f 6c 6c 62 61 63 6b ne pagerRollback
bd70: 57 61 6c 28 78 29 20 30 0a 23 20 64 65 66 69 6e Wal(x) 0.# defin
bd80: 65 20 70 61 67 65 72 57 61 6c 46 72 61 6d 65 73 e pagerWalFrames
bd90: 28 76 2c 77 2c 78 2c 79 2c 7a 29 20 30 0a 23 20 (v,w,x,y,z) 0.#
bda0: 64 65 66 69 6e 65 20 70 61 67 65 72 4f 70 65 6e define pagerOpen
bdb0: 57 61 6c 49 66 50 72 65 73 65 6e 74 28 7a 29 20 WalIfPresent(z)
bdc0: 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 64 65 66 69 SQLITE_OK.# defi
bdd0: 6e 65 20 70 61 67 65 72 42 65 67 69 6e 52 65 61 ne pagerBeginRea
bde0: 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 7a 29 20 dTransaction(z)
bdf0: 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 SQLITE_OK.#endif
be00: 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 74 ../*.** Unlock t
be10: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
be20: 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
be30: 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 is a no-op if th
be40: 65 20 70 61 67 65 72 0a 2a 2a 20 69 73 20 69 6e e pager.** is in
be50: 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e exclusive mode.
be60: 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 .**.** If the pa
be70: 67 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ger is currently
be80: 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 2c in error state,
be90: 20 64 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e discard the con
bea0: 74 65 6e 74 73 20 6f 66 20 0a 2a 2a 20 74 68 65 tents of .** the
beb0: 20 63 61 63 68 65 20 61 6e 64 20 72 65 73 65 74 cache and reset
bec0: 20 74 68 65 20 50 61 67 65 72 20 73 74 72 75 63 the Pager struc
bed0: 74 75 72 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 ture internal st
bee0: 61 74 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 ate. If there is
bef0: 0a 2a 2a 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 .** an open jour
bf00: 6e 61 6c 2d 66 69 6c 65 2c 20 74 68 65 6e 20 74 nal-file, then t
bf10: 68 65 20 6e 65 78 74 20 74 69 6d 65 20 61 20 73 he next time a s
bf20: 68 61 72 65 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 hared-lock is ob
bf30: 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 tained.** on the
bf40: 20 70 61 67 65 72 20 66 69 6c 65 20 28 62 79 20 pager file (by
bf50: 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 this or any othe
bf60: 72 20 70 72 6f 63 65 73 73 29 2c 20 69 74 20 77 r process), it w
bf70: 69 6c 6c 20 62 65 0a 2a 2a 20 74 72 65 61 74 65 ill be.** treate
bf80: 64 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e d as a hot-journ
bf90: 61 6c 20 61 6e 64 20 72 6f 6c 6c 65 64 20 62 61 al and rolled ba
bfa0: 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ck..*/.static vo
bfb0: 69 64 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 id pager_unlock(
bfc0: 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
bfd0: 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65 if( !pPager->e
bfe0: 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a xclusiveMode ){.
bff0: 20 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c int rc = SQL
c000: 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 ITE_OK;
c010: 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
c020: 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 63 20 3d */. int iDc =
c030: 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
c040: 66 64 29 3f 73 71 6c 69 74 65 33 4f 73 44 65 76 fd)?sqlite3OsDev
c050: 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
c060: 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3a 30 cs(pPager->fd):0
c070: 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 ;.. /* Always
c080: 20 63 6c 6f 73 65 20 74 68 65 20 6a 6f 75 72 6e close the journ
c090: 61 6c 20 66 69 6c 65 20 77 68 65 6e 20 64 72 6f al file when dro
c0a0: 70 70 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 pping the databa
c0b0: 73 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2a 20 se lock.. **
c0c0: 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 6f 74 68 Otherwise, anoth
c0d0: 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 er connection wi
c0e0: 74 68 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d th journal_mode=
c0f0: 64 65 6c 65 74 65 20 6d 69 67 68 74 0a 20 20 20 delete might.
c100: 20 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 66 ** delete the f
c110: 69 6c 65 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 ile out from und
c120: 65 72 20 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 er us.. */.
c130: 20 20 61 73 73 65 72 74 28 20 28 50 41 47 45 52 assert( (PAGER
c140: 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d _JOURNALMODE_MEM
c150: 4f 52 59 20 20 20 26 20 35 29 21 3d 31 20 29 3b ORY & 5)!=1 );
c160: 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 50 41 . assert( (PA
c170: 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
c180: 4f 46 46 20 20 20 20 20 20 26 20 35 29 21 3d 31 OFF & 5)!=1
c190: 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
c1a0: 28 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f (PAGER_JOURNALMO
c1b0: 44 45 5f 57 41 4c 20 20 20 20 20 20 26 20 35 29 DE_WAL & 5)
c1c0: 21 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 !=1 );. asser
c1d0: 74 28 20 28 50 41 47 45 52 5f 4a 4f 55 52 4e 41 t( (PAGER_JOURNA
c1e0: 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20 20 20 26 LMODE_DELETE &
c1f0: 20 35 29 21 3d 31 20 29 3b 0a 20 20 20 20 61 73 5)!=1 );. as
c200: 73 65 72 74 28 20 28 50 41 47 45 52 5f 4a 4f 55 sert( (PAGER_JOU
c210: 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 RNALMODE_TRUNCAT
c220: 45 20 26 20 35 29 3d 3d 31 20 29 3b 0a 20 20 20 E & 5)==1 );.
c230: 20 61 73 73 65 72 74 28 20 28 50 41 47 45 52 5f assert( (PAGER_
c240: 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 JOURNALMODE_PERS
c250: 49 53 54 20 20 26 20 35 29 3d 3d 31 20 29 3b 0a IST & 5)==1 );.
c260: 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44 63 20 if( 0==(iDc
c270: 26 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 55 & SQLITE_IOCAP_U
c280: 4e 44 45 4c 45 54 41 42 4c 45 5f 57 48 45 4e 5f NDELETABLE_WHEN_
c290: 4f 50 45 4e 29 0a 20 20 20 20 20 7c 7c 20 31 21 OPEN). || 1!
c2a0: 3d 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 =(pPager->journa
c2b0: 6c 4d 6f 64 65 20 26 20 35 29 0a 20 20 20 20 29 lMode & 5). )
c2c0: 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f {. sqlite3O
c2d0: 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a sClose(pPager->j
c2e0: 66 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 fd);. }..
c2f0: 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 sqlite3BitvecDes
c300: 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 6e troy(pPager->pIn
c310: 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 70 50 Journal);. pP
c320: 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
c330: 20 3d 20 30 3b 0a 20 20 20 20 72 65 6c 65 61 73 = 0;. releas
c340: 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 eAllSavepoints(p
c350: 50 61 67 65 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 Pager);.. /*
c360: 49 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 75 If the file is u
c370: 6e 6c 6f 63 6b 65 64 2c 20 73 6f 6d 65 62 6f 64 nlocked, somebod
c380: 79 20 65 6c 73 65 20 6d 69 67 68 74 20 63 68 61 y else might cha
c390: 6e 67 65 20 69 74 2e 20 54 68 65 0a 20 20 20 20 nge it. The.
c3a0: 2a 2a 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 ** values stored
c3b0: 20 69 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 in Pager.dbSize
c3c0: 20 65 74 63 2e 20 6d 69 67 68 74 20 62 65 63 6f etc. might beco
c3d0: 6d 65 20 69 6e 76 61 6c 69 64 20 69 66 0a 20 20 me invalid if.
c3e0: 20 20 2a 2a 20 74 68 69 73 20 68 61 70 70 65 6e ** this happen
c3f0: 73 2e 20 20 4f 6e 65 20 63 61 6e 20 61 72 67 75 s. One can argu
c400: 65 20 74 68 61 74 20 74 68 69 73 20 64 6f 65 73 e that this does
c410: 6e 27 74 20 6e 65 65 64 20 74 6f 20 62 65 20 63 n't need to be c
c420: 6c 65 61 72 65 64 0a 20 20 20 20 2a 2a 20 75 6e leared. ** un
c430: 74 69 6c 20 74 68 65 20 63 68 61 6e 67 65 2d 63 til the change-c
c440: 6f 75 6e 74 65 72 20 63 68 65 63 6b 20 66 61 69 ounter check fai
c450: 6c 73 20 69 6e 20 50 61 67 65 72 53 68 61 72 65 ls in PagerShare
c460: 64 4c 6f 63 6b 28 29 2e 0a 20 20 20 20 2a 2a 20 dLock().. **
c470: 43 6c 65 61 72 69 6e 67 20 74 68 65 20 70 61 67 Clearing the pag
c480: 65 20 73 69 7a 65 20 63 61 63 68 65 20 68 65 72 e size cache her
c490: 65 20 69 73 20 62 65 69 6e 67 20 63 6f 6e 73 65 e is being conse
c4a0: 72 76 61 74 69 76 65 2e 0a 20 20 20 20 2a 2f 0a rvative.. */.
c4b0: 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 pPager->dbSi
c4c0: 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 0a 20 20 zeValid = 0;..
c4d0: 20 20 69 66 28 20 70 61 67 65 72 55 73 65 57 61 if( pagerUseWa
c4e0: 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 l(pPager) ){.
c4f0: 20 20 20 73 71 6c 69 74 65 33 57 61 6c 45 6e 64 sqlite3WalEnd
c500: 52 65 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 ReadTransaction(
c510: 70 50 61 67 65 72 2d 3e 70 57 61 6c 29 3b 0a 20 pPager->pWal);.
c520: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
c530: 72 63 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 rc = osUnlock(pP
c540: 61 67 65 72 2d 3e 66 64 2c 20 4e 4f 5f 4c 4f 43 ager->fd, NO_LOC
c550: 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 K);. }. if
c560: 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 70 50 ( rc ){. pP
c570: 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 ager->errCode =
c580: 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 49 4f rc;. }. IO
c590: 54 52 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20 25 TRACE(("UNLOCK %
c5a0: 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 0a p\n", pPager))..
c5b0: 20 20 20 20 2f 2a 20 49 66 20 50 61 67 65 72 2e /* If Pager.
c5c0: 65 72 72 43 6f 64 65 20 69 73 20 73 65 74 2c 20 errCode is set,
c5d0: 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
c5e0: 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20 the pager cache
c5f0: 63 61 6e 6e 6f 74 20 62 65 0a 20 20 20 20 2a 2a cannot be. **
c600: 20 74 72 75 73 74 65 64 2e 20 4e 6f 77 20 74 68 trusted. Now th
c610: 61 74 20 74 68 65 20 70 61 67 65 72 20 66 69 6c at the pager fil
c620: 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 e is unlocked, t
c630: 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
c640: 68 65 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 20 he. ** cache
c650: 63 61 6e 20 62 65 20 64 69 73 63 61 72 64 65 64 can be discarded
c660: 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63 and the error c
c670: 6f 64 65 20 73 61 66 65 6c 79 20 63 6c 65 61 72 ode safely clear
c680: 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ed.. */. i
c690: 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f f( pPager->errCo
c6a0: 64 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 de ){. if(
c6b0: 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
c6c0: 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d . pPager-
c6d0: 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 >errCode = SQLIT
c6e0: 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 E_OK;. }.
c6f0: 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 pager_reset(
c700: 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 0a pPager);. }..
c710: 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e pPager->chan
c720: 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 30 3b geCountDone = 0;
c730: 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 . pPager->sta
c740: 74 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 te = PAGER_UNLOC
c750: 4b 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 K;. pPager->d
c760: 62 4d 6f 64 69 66 69 65 64 20 3d 20 30 3b 0a 20 bModified = 0;.
c770: 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 }.}../*.** This
c780: 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 function should
c790: 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 be called when
c7a0: 61 6e 20 49 4f 45 52 52 2c 20 43 4f 52 52 55 50 an IOERR, CORRUP
c7b0: 54 20 6f 72 20 46 55 4c 4c 20 65 72 72 6f 72 0a T or FULL error.
c7c0: 2a 2a 20 6d 61 79 20 68 61 76 65 20 6f 63 63 75 ** may have occu
c7d0: 72 72 65 64 2e 20 54 68 65 20 66 69 72 73 74 20 rred. The first
c7e0: 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f argument is a po
c7f0: 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 inter to the pag
c800: 65 72 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 er .** structure
c810: 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 74 68 65 , the second the
c820: 20 65 72 72 6f 72 2d 63 6f 64 65 20 61 62 6f 75 error-code abou
c830: 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 t to be returned
c840: 20 62 79 20 61 20 70 61 67 65 72 20 0a 2a 2a 20 by a pager .**
c850: 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 API function. Th
c860: 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
c870: 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 is a copy of th
c880: 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
c890: 74 20 0a 2a 2a 20 74 6f 20 74 68 69 73 20 66 75 t .** to this fu
c8a0: 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 nction. .**.** I
c8b0: 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 f the second arg
c8c0: 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 54 45 5f ument is SQLITE_
c8d0: 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f IOERR, SQLITE_CO
c8e0: 52 52 55 50 54 2c 20 6f 72 20 53 51 4c 49 54 45 RRUPT, or SQLITE
c8f0: 5f 46 55 4c 4c 0a 2a 2a 20 74 68 65 20 65 72 72 _FULL.** the err
c900: 6f 72 20 62 65 63 6f 6d 65 73 20 70 65 72 73 69 or becomes persi
c910: 73 74 65 6e 74 2e 20 55 6e 74 69 6c 20 74 68 65 stent. Until the
c920: 20 70 65 72 73 69 73 74 65 6e 20 65 72 72 6f 72 persisten error
c930: 20 69 73 20 63 6c 65 61 72 65 64 2c 0a 2a 2a 20 is cleared,.**
c940: 73 75 62 73 65 71 75 65 6e 74 20 41 50 49 20 63 subsequent API c
c950: 61 6c 6c 73 20 6f 6e 20 74 68 69 73 20 50 61 67 alls on this Pag
c960: 65 72 20 77 69 6c 6c 20 69 6d 6d 65 64 69 61 74 er will immediat
c970: 65 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20 73 ely return the s
c980: 61 6d 65 20 0a 2a 2a 20 65 72 72 6f 72 20 63 6f ame .** error co
c990: 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 65 72 73 de..**.** A pers
c9a0: 69 73 74 65 6e 74 20 65 72 72 6f 72 20 69 6e 64 istent error ind
c9b0: 69 63 61 74 65 73 20 74 68 61 74 20 74 68 65 20 icates that the
c9c0: 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
c9d0: 70 61 67 65 72 2d 63 61 63 68 65 20 0a 2a 2a 20 pager-cache .**
c9e0: 63 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 65 cannot be truste
c9f0: 64 2e 20 54 68 69 73 20 73 74 61 74 65 20 63 61 d. This state ca
ca00: 6e 20 62 65 20 63 6c 65 61 72 65 64 20 62 79 20 n be cleared by
ca10: 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 73 63 61 completely disca
ca20: 72 64 69 6e 67 20 0a 2a 2a 20 74 68 65 20 63 6f rding .** the co
ca30: 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 ntents of the pa
ca40: 67 65 72 2d 63 61 63 68 65 2e 20 49 66 20 61 20 ger-cache. If a
ca50: 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 transaction was
ca60: 61 63 74 69 76 65 20 77 68 65 6e 0a 2a 2a 20 74 active when.** t
ca70: 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 65 72 he persistent er
ca80: 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 74 68 ror occurred, th
ca90: 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 en the rollback
caa0: 6a 6f 75 72 6e 61 6c 20 6d 61 79 20 6e 65 65 64 journal may need
cab0: 0a 2a 2a 20 74 6f 20 62 65 20 72 65 70 6c 61 79 .** to be replay
cac0: 65 64 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 ed to restore th
cad0: 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
cae0: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
caf0: 28 61 73 20 69 66 0a 2a 2a 20 69 74 20 77 65 72 (as if.** it wer
cb00: 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 29 e a hot-journal)
cb10: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
cb20: 70 61 67 65 72 5f 65 72 72 6f 72 28 50 61 67 65 pager_error(Page
cb30: 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 72 r *pPager, int r
cb40: 63 29 7b 0a 20 20 69 6e 74 20 72 63 32 20 3d 20 c){. int rc2 =
cb50: 72 63 20 26 20 30 78 66 66 3b 0a 20 20 61 73 73 rc & 0xff;. ass
cb60: 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
cb70: 4f 4b 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b 0a OK || !MEMDB );.
cb80: 20 20 61 73 73 65 72 74 28 0a 20 20 20 20 20 20 assert(.
cb90: 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
cba0: 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c ==SQLITE_FULL ||
cbb0: 0a 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e . pPager->
cbc0: 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f errCode==SQLITE_
cbd0: 4f 4b 20 7c 7c 0a 20 20 20 20 20 20 20 28 70 50 OK ||. (pP
cbe0: 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 20 ager->errCode &
cbf0: 30 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 0xff)==SQLITE_IO
cc00: 45 52 52 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 ERR. );. if( r
cc10: 63 32 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 c2==SQLITE_FULL
cc20: 7c 7c 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 49 || rc2==SQLITE_I
cc30: 4f 45 52 52 20 29 7b 0a 20 20 20 20 70 50 61 67 OERR ){. pPag
cc40: 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 er->errCode = rc
cc50: 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
cc60: 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 c;.}../*.** Exec
cc70: 75 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 ute a rollback i
cc80: 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 f a transaction
cc90: 69 73 20 61 63 74 69 76 65 20 61 6e 64 20 75 6e is active and un
cca0: 6c 6f 63 6b 20 74 68 65 20 0a 2a 2a 20 64 61 74 lock the .** dat
ccb0: 61 62 61 73 65 20 66 69 6c 65 2e 20 0a 2a 2a 0a abase file. .**.
ccc0: 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 ** If the pager
ccd0: 68 61 73 20 61 6c 72 65 61 64 79 20 65 6e 74 65 has already ente
cce0: 72 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 74 red the error st
ccf0: 61 74 65 2c 20 64 6f 20 6e 6f 74 20 61 74 74 65 ate, do not atte
cd00: 6d 70 74 20 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c mpt .** the roll
cd10: 62 61 63 6b 20 61 74 20 74 68 69 73 20 74 69 6d back at this tim
cd20: 65 2e 20 49 6e 73 74 65 61 64 2c 20 70 61 67 65 e. Instead, page
cd30: 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 63 61 r_unlock() is ca
cd40: 6c 6c 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61 6c lled. The.** cal
cd50: 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c 6f 63 l to pager_unloc
cd60: 6b 28 29 20 77 69 6c 6c 20 64 69 73 63 61 72 64 k() will discard
cd70: 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 all in-memory p
cd80: 61 67 65 73 2c 20 75 6e 6c 6f 63 6b 0a 2a 2a 20 ages, unlock.**
cd90: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
cda0: 65 20 61 6e 64 20 63 6c 65 61 72 20 74 68 65 20 e and clear the
cdb0: 65 72 72 6f 72 20 73 74 61 74 65 2e 20 49 66 20 error state. If
cdc0: 74 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 0a this means that.
cdd0: 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20 68 6f ** there is a ho
cde0: 74 2d 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 69 t-journal left i
cdf0: 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 n the file-syste
ce00: 6d 2c 20 74 68 65 20 6e 65 78 74 20 63 6f 6e 6e m, the next conn
ce10: 65 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 6f 62 74 ection.** to obt
ce20: 61 69 6e 20 61 20 73 68 61 72 65 64 20 6c 6f 63 ain a shared loc
ce30: 6b 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 28 k on the pager (
ce40: 77 68 69 63 68 20 6d 61 79 20 62 65 20 74 68 69 which may be thi
ce50: 73 20 6f 6e 65 29 20 77 69 6c 6c 0a 2a 2a 20 72 s one) will.** r
ce60: 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a oll it back..**.
ce70: 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 ** If the pager
ce80: 68 61 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 has not already
ce90: 65 6e 74 65 72 65 64 20 74 68 65 20 65 72 72 6f entered the erro
cea0: 72 20 73 74 61 74 65 2c 20 62 75 74 20 61 6e 20 r state, but an
ceb0: 49 4f 20 6f 72 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 IO or.** malloc
cec0: 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 error occurs dur
ced0: 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 ing a rollback,
cee0: 74 68 65 6e 20 74 68 69 73 20 77 69 6c 6c 20 69 then this will i
cef0: 74 73 65 6c 66 20 63 61 75 73 65 20 0a 2a 2a 20 tself cause .**
cf00: 74 68 65 20 70 61 67 65 72 20 74 6f 20 65 6e 74 the pager to ent
cf10: 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 er the error sta
cf20: 74 65 2e 20 57 68 69 63 68 20 77 69 6c 6c 20 62 te. Which will b
cf30: 65 20 63 6c 65 61 72 65 64 20 62 79 20 74 68 65 e cleared by the
cf40: 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 .** call to page
cf50: 72 5f 75 6e 6c 6f 63 6b 28 29 2c 20 61 73 20 64 r_unlock(), as d
cf60: 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e 0a escribed above..
cf70: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
cf80: 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c agerUnlockAndRol
cf90: 6c 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50 61 lback(Pager *pPa
cfa0: 67 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 67 ger){. if( pPag
cfb0: 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c er->errCode==SQL
cfc0: 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 ITE_OK && pPager
cfd0: 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 ->state>=PAGER_R
cfe0: 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 73 ESERVED ){. s
cff0: 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
d000: 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 73 nMalloc();. s
d010: 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 qlite3PagerRollb
d020: 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 ack(pPager);.
d030: 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
d040: 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 7d 0a 20 nMalloc();. }.
d050: 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 pager_unlock(pP
d060: 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ager);.}../*.**
d070: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 64 This routine end
d080: 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e s a transaction.
d090: 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 A transaction i
d0a0: 73 20 75 73 75 61 6c 6c 79 20 65 6e 64 65 64 20 s usually ended
d0b0: 62 79 20 0a 2a 2a 20 65 69 74 68 65 72 20 61 20 by .** either a
d0c0: 43 4f 4d 4d 49 54 20 6f 72 20 61 20 52 4f 4c 4c COMMIT or a ROLL
d0d0: 42 41 43 4b 20 6f 70 65 72 61 74 69 6f 6e 2e 20 BACK operation.
d0e0: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 79 This routine may
d0f0: 20 62 65 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 61 be called .** a
d100: 66 74 65 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 fter rollback of
d110: 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 20 a hot-journal,
d120: 6f 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f or if an error o
d130: 63 63 75 72 73 20 77 68 69 6c 65 20 6f 70 65 6e ccurs while open
d140: 69 6e 67 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e ing.** the journ
d150: 61 6c 20 66 69 6c 65 20 6f 72 20 77 72 69 74 69 al file or writi
d160: 6e 67 20 74 68 65 20 76 65 72 79 20 66 69 72 73 ng the very firs
d170: 74 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 t journal-header
d180: 20 6f 66 20 61 0a 2a 2a 20 64 61 74 61 62 61 73 of a.** databas
d190: 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a e transaction..*
d1a0: 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 * .** If the pag
d1b0: 65 72 20 69 73 20 69 6e 20 50 41 47 45 52 5f 53 er is in PAGER_S
d1c0: 48 41 52 45 44 20 6f 72 20 50 41 47 45 52 5f 55 HARED or PAGER_U
d1d0: 4e 4c 4f 43 4b 20 73 74 61 74 65 20 77 68 65 6e NLOCK state when
d1e0: 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 this.** routine
d1f0: 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 is called, it i
d200: 73 20 61 20 6e 6f 2d 6f 70 20 28 72 65 74 75 72 s a no-op (retur
d210: 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 29 2e 0a 2a ns SQLITE_OK)..*
d220: 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 *.** Otherwise,
d230: 61 6e 79 20 61 63 74 69 76 65 20 73 61 76 65 70 any active savep
d240: 6f 69 6e 74 73 20 61 72 65 20 72 65 6c 65 61 73 oints are releas
d250: 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
d260: 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
d270: 20 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74 20 69 open, then it i
d280: 73 20 22 66 69 6e 61 6c 69 7a 65 64 22 2e 20 4f s "finalized". O
d290: 6e 63 65 20 61 20 6a 6f 75 72 6e 61 6c 20 0a 2a nce a journal .*
d2a0: 2a 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 * file has been
d2b0: 66 69 6e 61 6c 69 7a 65 64 20 69 74 20 69 73 20 finalized it is
d2c0: 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 not possible to
d2d0: 75 73 65 20 69 74 20 74 6f 20 72 6f 6c 6c 20 62 use it to roll b
d2e0: 61 63 6b 20 61 20 0a 2a 2a 20 74 72 61 6e 73 61 ack a .** transa
d2f0: 63 74 69 6f 6e 2e 20 4e 6f 72 20 77 69 6c 6c 20 ction. Nor will
d300: 69 74 20 62 65 20 63 6f 6e 73 69 64 65 72 65 64 it be considered
d310: 20 74 6f 20 62 65 20 61 20 68 6f 74 2d 6a 6f 75 to be a hot-jou
d320: 72 6e 61 6c 20 62 79 20 74 68 69 73 0a 2a 2a 20 rnal by this.**
d330: 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 64 61 74 or any other dat
d340: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
d350: 2e 20 45 78 61 63 74 6c 79 20 68 6f 77 20 61 20 . Exactly how a
d360: 6a 6f 75 72 6e 61 6c 20 69 73 20 66 69 6e 61 6c journal is final
d370: 69 7a 65 64 0a 2a 2a 20 64 65 70 65 6e 64 73 20 ized.** depends
d380: 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f on whether or no
d390: 74 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 t the pager is r
d3a0: 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 unning in exclus
d3b0: 69 76 65 20 6d 6f 64 65 20 61 6e 64 0a 2a 2a 20 ive mode and.**
d3c0: 74 68 65 20 63 75 72 72 65 6e 74 20 6a 6f 75 72 the current jour
d3d0: 6e 61 6c 2d 6d 6f 64 65 20 28 50 61 67 65 72 2e nal-mode (Pager.
d3e0: 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 76 61 6c 75 journalMode valu
d3f0: 65 29 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a e), as follows:.
d400: 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d **.** journalM
d410: 6f 64 65 3d 3d 4d 45 4d 4f 52 59 0a 2a 2a 20 20 ode==MEMORY.**
d420: 20 20 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 Journal file
d430: 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 73 69 descriptor is si
d440: 6d 70 6c 79 20 63 6c 6f 73 65 64 2e 20 54 68 69 mply closed. Thi
d450: 73 20 64 65 73 74 72 6f 79 73 20 61 6e 20 0a 2a s destroys an .*
d460: 2a 20 20 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 * in-memory
d470: 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 20 journal..**.**
d480: 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 54 52 journalMode==TR
d490: 55 4e 43 41 54 45 0a 2a 2a 20 20 20 20 20 4a 6f UNCATE.** Jo
d4a0: 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 74 72 urnal file is tr
d4b0: 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 6f 20 uncated to zero
d4c0: 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a bytes in size..*
d4d0: 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f *.** journalMo
d4e0: 64 65 3d 3d 50 45 52 53 49 53 54 0a 2a 2a 20 20 de==PERSIST.**
d4f0: 20 20 20 54 68 65 20 66 69 72 73 74 20 32 38 20 The first 28
d500: 62 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f 75 bytes of the jou
d510: 72 6e 61 6c 20 66 69 6c 65 20 61 72 65 20 7a 65 rnal file are ze
d520: 72 6f 65 64 2e 20 54 68 69 73 20 69 6e 76 61 6c roed. This inval
d530: 69 64 61 74 65 73 0a 2a 2a 20 20 20 20 20 74 68 idates.** th
d540: 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 e first journal
d550: 68 65 61 64 65 72 20 69 6e 20 74 68 65 20 66 69 header in the fi
d560: 6c 65 2c 20 61 6e 64 20 68 65 6e 63 65 20 74 68 le, and hence th
d570: 65 20 65 6e 74 69 72 65 20 6a 6f 75 72 6e 61 6c e entire journal
d580: 0a 2a 2a 20 20 20 20 20 66 69 6c 65 2e 20 41 6e .** file. An
d590: 20 69 6e 76 61 6c 69 64 20 6a 6f 75 72 6e 61 6c invalid journal
d5a0: 20 66 69 6c 65 20 63 61 6e 6e 6f 74 20 62 65 20 file cannot be
d5b0: 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a rolled back..**.
d5c0: 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ** journalMode
d5d0: 3d 3d 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20 ==DELETE.**
d5e0: 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 The journal file
d5f0: 20 69 73 20 63 6c 6f 73 65 64 20 61 6e 64 20 64 is closed and d
d600: 65 6c 65 74 65 64 20 75 73 69 6e 67 20 73 71 6c eleted using sql
d610: 69 74 65 33 4f 73 44 65 6c 65 74 65 28 29 2e 0a ite3OsDelete()..
d620: 2a 2a 0a 2a 2a 20 20 20 20 20 49 66 20 74 68 65 **.** If the
d630: 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e pager is runnin
d640: 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d g in exclusive m
d650: 6f 64 65 2c 20 74 68 69 73 20 6d 65 74 68 6f 64 ode, this method
d660: 20 6f 66 20 66 69 6e 61 6c 69 7a 69 6e 67 0a 2a of finalizing.*
d670: 2a 20 20 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 * the journa
d680: 6c 20 66 69 6c 65 20 69 73 20 6e 65 76 65 72 20 l file is never
d690: 75 73 65 64 2e 20 49 6e 73 74 65 61 64 2c 20 69 used. Instead, i
d6a0: 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 4d 6f 64 f the journalMod
d6b0: 65 20 69 73 0a 2a 2a 20 20 20 20 20 44 45 4c 45 e is.** DELE
d6c0: 54 45 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 TE and the pager
d6d0: 20 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 is in exclusive
d6e0: 20 6d 6f 64 65 2c 20 74 68 65 20 6d 65 74 68 6f mode, the metho
d6f0: 64 20 64 65 73 63 72 69 62 65 64 20 75 6e 64 65 d described unde
d700: 72 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c r.** journal
d710: 4d 6f 64 65 3d 3d 50 45 52 53 49 53 54 20 69 73 Mode==PERSIST is
d720: 20 75 73 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a used instead..*
d730: 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 6a *.** After the j
d740: 6f 75 72 6e 61 6c 20 69 73 20 66 69 6e 61 6c 69 ournal is finali
d750: 7a 65 64 2c 20 69 66 20 72 75 6e 6e 69 6e 67 20 zed, if running
d760: 69 6e 20 6e 6f 6e 2d 65 78 63 6c 75 73 69 76 65 in non-exclusive
d770: 20 6d 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 70 61 mode, the.** pa
d780: 67 65 72 20 6d 6f 76 65 73 20 74 6f 20 50 41 47 ger moves to PAG
d790: 45 52 5f 53 48 41 52 45 44 20 73 74 61 74 65 20 ER_SHARED state
d7a0: 28 61 6e 64 20 64 6f 77 6e 67 72 61 64 65 73 20 (and downgrades
d7b0: 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a the lock on the.
d7c0: 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
d7d0: 20 61 63 63 6f 72 64 69 6e 67 6c 79 29 2e 0a 2a accordingly)..*
d7e0: 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 *.** If the page
d7f0: 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 r is running in
d800: 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 61 exclusive mode a
d810: 6e 64 20 69 73 20 69 6e 20 50 41 47 45 52 5f 53 nd is in PAGER_S
d820: 59 4e 43 45 44 20 73 74 61 74 65 2c 0a 2a 2a 20 YNCED state,.**
d830: 69 74 20 6d 6f 76 65 73 20 74 6f 20 50 41 47 45 it moves to PAGE
d840: 52 5f 45 58 43 4c 55 53 49 56 45 2e 20 4e 6f 20 R_EXCLUSIVE. No
d850: 6c 6f 63 6b 73 20 61 72 65 20 64 6f 77 6e 67 72 locks are downgr
d860: 61 64 65 64 20 77 68 65 6e 20 72 75 6e 6e 69 6e aded when runnin
d870: 67 20 69 6e 0a 2a 2a 20 65 78 63 6c 75 73 69 76 g in.** exclusiv
d880: 65 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 e mode..**.** SQ
d890: 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
d8a0: 6e 65 64 20 69 66 20 6e 6f 20 65 72 72 6f 72 20 ned if no error
d8b0: 6f 63 63 75 72 73 2e 20 49 66 20 61 6e 20 65 72 occurs. If an er
d8c0: 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e ror occurs durin
d8d0: 67 0a 2a 2a 20 61 6e 79 20 6f 66 20 74 68 65 20 g.** any of the
d8e0: 49 4f 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 6f IO operations to
d8f0: 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f finalize the jo
d900: 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 75 6e urnal file or un
d910: 6c 6f 63 6b 20 74 68 65 0a 2a 2a 20 64 61 74 61 lock the.** data
d920: 62 61 73 65 20 74 68 65 6e 20 74 68 65 20 49 4f base then the IO
d930: 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
d940: 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 75 eturned to the u
d950: 73 65 72 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 ser. If the .**
d960: 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 66 69 6e operation to fin
d970: 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 alize the journa
d980: 6c 20 66 69 6c 65 20 66 61 69 6c 73 2c 20 74 68 l file fails, th
d990: 65 6e 20 74 68 65 20 63 6f 64 65 20 73 74 69 6c en the code stil
d9a0: 6c 0a 2a 2a 20 74 72 69 65 73 20 74 6f 20 75 6e l.** tries to un
d9b0: 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 lock the databas
d9c0: 65 20 66 69 6c 65 20 69 66 20 6e 6f 74 20 69 6e e file if not in
d9d0: 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e exclusive mode.
d9e0: 20 49 66 20 74 68 65 0a 2a 2a 20 75 6e 6c 6f 63 If the.** unloc
d9f0: 6b 20 6f 70 65 72 61 74 69 6f 6e 20 66 61 69 6c k operation fail
da00: 73 20 61 73 20 77 65 6c 6c 2c 20 74 68 65 6e 20 s as well, then
da10: 74 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 the first error
da20: 63 6f 64 65 20 72 65 6c 61 74 65 64 0a 2a 2a 20 code related.**
da30: 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 72 72 to the first err
da40: 6f 72 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 28 or encountered (
da50: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6e 61 the journal fina
da60: 6c 69 7a 61 74 69 6f 6e 20 6f 6e 65 29 20 69 73 lization one) is
da70: 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f .** returned..*/
da80: 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 .static int page
da90: 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f r_end_transactio
daa0: 6e 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c n(Pager *pPager,
dab0: 20 69 6e 74 20 68 61 73 4d 61 73 74 65 72 29 7b int hasMaster){
dac0: 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
dad0: 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 45 TE_OK; /* E
dae0: 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 6a rror code from j
daf0: 6f 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74 ournal finalizat
db00: 69 6f 6e 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f ion operation */
db10: 0a 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51 4c . int rc2 = SQL
db20: 49 54 45 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 45 ITE_OK; /* E
db30: 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 64 rror code from d
db40: 62 20 66 69 6c 65 20 75 6e 6c 6f 63 6b 20 6f 70 b file unlock op
db50: 65 72 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 eration */.. if
db60: 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c ( pPager->state<
db70: 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 PAGER_RESERVED )
db80: 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
db90: 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 ITE_OK;. }. re
dba0: 6c 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e leaseAllSavepoin
dbb0: 74 73 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 61 ts(pPager);.. a
dbc0: 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
dbd0: 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 ager->jfd) || pP
dbe0: 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
dbf0: 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 73 4f ==0 );. if( isO
dc00: 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
dc10: 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
dc20: 21 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 !pagerUseWal(pPa
dc30: 67 65 72 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 ger) );.. /*
dc40: 46 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 Finalize the jou
dc50: 72 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 rnal file. */.
dc60: 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4d if( sqlite3IsM
dc70: 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 emJournal(pPager
dc80: 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 ->jfd) ){.
dc90: 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
dca0: 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 journalMode==PAG
dcb0: 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d ER_JOURNALMODE_M
dcc0: 45 4d 4f 52 59 20 29 3b 0a 20 20 20 20 20 20 73 EMORY );. s
dcd0: 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 qlite3OsClose(pP
dce0: 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 ager->jfd);.
dcf0: 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 }else if( pPager
dd00: 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
dd10: 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
dd20: 5f 54 52 55 4e 43 41 54 45 20 29 7b 0a 20 20 20 _TRUNCATE ){.
dd30: 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a if( pPager->j
dd40: 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a ournalOff==0 ){.
dd50: 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
dd60: 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 65 ITE_OK;. }e
dd70: 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 lse{. rc
dd80: 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 = sqlite3OsTrunc
dd90: 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ate(pPager->jfd,
dda0: 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0);. }.
ddb0: 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
ddc0: 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 alOff = 0;.
ddd0: 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
dde0: 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 Started = 0;.
ddf0: 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 }else if( pPage
de00: 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d r->journalMode==
de10: 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
de20: 45 5f 50 45 52 53 49 53 54 0a 20 20 20 20 20 20 E_PERSIST.
de30: 7c 7c 20 28 70 50 61 67 65 72 2d 3e 65 78 63 6c || (pPager->excl
de40: 75 73 69 76 65 4d 6f 64 65 20 26 26 20 70 50 61 usiveMode && pPa
de50: 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
de60: 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d !=PAGER_JOURNALM
de70: 4f 44 45 5f 57 41 4c 29 0a 20 20 20 20 29 7b 0a ODE_WAL). ){.
de80: 20 20 20 20 20 20 72 63 20 3d 20 7a 65 72 6f 4a rc = zeroJ
de90: 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 ournalHdr(pPager
dea0: 2c 20 68 61 73 4d 61 73 74 65 72 29 3b 0a 20 20 , hasMaster);.
deb0: 20 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72 28 pager_error(
dec0: 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 pPager, rc);.
ded0: 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
dee0: 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 alOff = 0;.
def0: 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
df00: 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 Started = 0;.
df10: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a }else{. /*
df20: 20 54 68 69 73 20 62 72 61 6e 63 68 20 6d 61 79 This branch may
df30: 20 62 65 20 65 78 65 63 75 74 65 64 20 77 69 74 be executed wit
df40: 68 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4d h Pager.journalM
df50: 6f 64 65 3d 3d 4d 45 4d 4f 52 59 20 69 66 0a 20 ode==MEMORY if.
df60: 20 20 20 20 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f ** a hot-jo
df70: 75 72 6e 61 6c 20 77 61 73 20 6a 75 73 74 20 72 urnal was just r
df80: 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 6e 20 74 olled back. In t
df90: 68 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f 75 his case the jou
dfa0: 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20 66 69 rnal. ** fi
dfb0: 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f le should be clo
dfc0: 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65 64 2e sed and deleted.
dfd0: 20 49 66 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 If this connect
dfe0: 69 6f 6e 20 77 72 69 74 65 73 20 74 6f 0a 20 20 ion writes to.
dff0: 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 ** the datab
e000: 61 73 65 20 66 69 6c 65 2c 20 69 74 20 77 69 6c ase file, it wil
e010: 6c 20 64 6f 20 73 6f 20 75 73 69 6e 67 20 61 6e l do so using an
e020: 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e in-memory journ
e030: 61 6c 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 al. . */.
e040: 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
e050: 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d er->journalMode=
e060: 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
e070: 44 45 5f 44 45 4c 45 54 45 20 0a 20 20 20 20 20 DE_DELETE .
e080: 20 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d || pPager-
e090: 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 >journalMode==PA
e0a0: 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
e0b0: 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 20 20 MEMORY .
e0c0: 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f || pPager->jo
e0d0: 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 urnalMode==PAGER
e0e0: 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c _JOURNALMODE_WAL
e0f0: 20 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 . );.
e100: 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
e110: 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 pPager->jfd);.
e120: 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d if( !pPager-
e130: 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 >tempFile ){.
e140: 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
e150: 33 4f 73 44 65 6c 65 74 65 28 70 50 61 67 65 72 3OsDelete(pPager
e160: 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e ->pVfs, pPager->
e170: 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 zJournal, 0);.
e180: 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 }. }..#if
e190: 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b def SQLITE_CHECK
e1a0: 5f 50 41 47 45 53 0a 20 20 20 20 73 71 6c 69 74 _PAGES. sqlit
e1b0: 65 33 50 63 61 63 68 65 49 74 65 72 61 74 65 44 e3PcacheIterateD
e1c0: 69 72 74 79 28 70 50 61 67 65 72 2d 3e 70 50 43 irty(pPager->pPC
e1d0: 61 63 68 65 2c 20 70 61 67 65 72 5f 73 65 74 5f ache, pager_set_
e1e0: 70 61 67 65 68 61 73 68 29 3b 0a 23 65 6e 64 69 pagehash);.#endi
e1f0: 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 f. }. sqlite3B
e200: 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 itvecDestroy(pPa
e210: 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 ger->pInJournal)
e220: 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a ;. pPager->pInJ
e230: 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 70 50 ournal = 0;. pP
e240: 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a ager->nRec = 0;.
e250: 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 sqlite3PcacheC
e260: 6c 65 61 6e 41 6c 6c 28 70 50 61 67 65 72 2d 3e leanAll(pPager->
e270: 70 50 43 61 63 68 65 29 3b 0a 0a 20 20 69 66 28 pPCache);.. if(
e280: 20 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 pagerUseWal(pPa
e290: 67 65 72 29 20 29 7b 0a 20 20 20 20 72 63 32 20 ger) ){. rc2
e2a0: 3d 20 73 71 6c 69 74 65 33 57 61 6c 45 6e 64 57 = sqlite3WalEndW
e2b0: 72 69 74 65 54 72 61 6e 73 61 63 74 69 6f 6e 28 riteTransaction(
e2c0: 70 50 61 67 65 72 2d 3e 70 57 61 6c 29 3b 0a 20 pPager->pWal);.
e2d0: 20 20 20 61 73 73 65 72 74 28 20 72 63 32 3d 3d assert( rc2==
e2e0: 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 SQLITE_OK );.
e2f0: 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d pPager->state =
e300: 20 50 41 47 45 52 5f 53 48 41 52 45 44 3b 0a 0a PAGER_SHARED;..
e310: 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f /* If the co
e320: 6e 6e 65 63 74 69 6f 6e 20 77 61 73 20 69 6e 20 nnection was in
e330: 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 65 78 63 locking_mode=exc
e340: 6c 75 73 69 76 65 20 6d 6f 64 65 20 62 75 74 20 lusive mode but
e350: 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 2c 0a 20 20 is no longer,.
e360: 20 20 2a 2a 20 64 72 6f 70 20 74 68 65 20 45 58 ** drop the EX
e370: 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 68 65 6c CLUSIVE lock hel
e380: 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 d on the databas
e390: 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 e file.. */.
e3a0: 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e if( !pPager->
e3b0: 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 26 26 exclusiveMode &&
e3c0: 20 73 71 6c 69 74 65 33 57 61 6c 45 78 63 6c 75 sqlite3WalExclu
e3d0: 73 69 76 65 4d 6f 64 65 28 70 50 61 67 65 72 2d siveMode(pPager-
e3e0: 3e 70 57 61 6c 2c 20 30 29 20 29 7b 0a 20 20 20 >pWal, 0) ){.
e3f0: 20 20 20 72 63 32 20 3d 20 6f 73 55 6e 6c 6f 63 rc2 = osUnloc
e400: 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 53 48 k(pPager->fd, SH
e410: 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 ARED_LOCK);.
e420: 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 }. }else if( !p
e430: 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 Pager->exclusive
e440: 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 32 20 Mode ){. rc2
e450: 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 = osUnlock(pPage
e460: 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f r->fd, SHARED_LO
e470: 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d CK);. pPager-
e480: 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53 >state = PAGER_S
e490: 48 41 52 45 44 3b 0a 20 20 20 20 70 50 61 67 65 HARED;. pPage
e4a0: 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f r->changeCountDo
e4b0: 6e 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 ne = 0;. }else
e4c0: 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 if( pPager->stat
e4d0: 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 e==PAGER_SYNCED
e4e0: 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 ){. pPager->s
e4f0: 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43 tate = PAGER_EXC
e500: 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20 20 70 50 LUSIVE;. }. pP
e510: 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 ager->setMaster
e520: 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e = 0;. pPager->n
e530: 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 70 eedSync = 0;. p
e540: 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 Pager->dbModifie
e550: 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 4f 44 d = 0;.. /* TOD
e560: 4f 3a 20 49 73 20 74 68 69 73 20 6f 70 74 69 6d O: Is this optim
e570: 61 6c 3f 20 57 68 79 20 69 73 20 74 68 65 20 64 al? Why is the d
e580: 62 20 73 69 7a 65 20 69 6e 76 61 6c 69 64 61 74 b size invalidat
e590: 65 64 20 68 65 72 65 20 0a 20 20 2a 2a 20 77 68 ed here . ** wh
e5a0: 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 en the database
e5b0: 66 69 6c 65 20 69 73 20 6e 6f 74 20 75 6e 6c 6f file is not unlo
e5c0: 63 6b 65 64 3f 20 2a 2f 0a 20 20 70 50 61 67 65 cked? */. pPage
e5d0: 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d 20 r->dbOrigSize =
e5e0: 30 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 0;. sqlite3Pcac
e5f0: 68 65 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 heTruncate(pPage
e600: 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 50 61 67 r->pPCache, pPag
e610: 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 69 er->dbSize);. i
e620: 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 f( !MEMDB ){.
e630: 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 pPager->dbSizeV
e640: 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 alid = 0;. }..
e650: 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c return (rc==SQL
e660: 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63 29 3b 0a ITE_OK?rc2:rc);.
e670: 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 }../*.** Paramet
e680: 65 72 20 61 44 61 74 61 20 6d 75 73 74 20 70 6f er aData must po
e690: 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 20 int to a buffer
e6a0: 6f 66 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 of pPager->pageS
e6b0: 69 7a 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 ize bytes.** of
e6c0: 64 61 74 61 2e 20 43 6f 6d 70 75 74 65 20 61 6e data. Compute an
e6d0: 64 20 72 65 74 75 72 6e 20 61 20 63 68 65 63 6b d return a check
e6e0: 73 75 6d 20 62 61 73 65 64 20 6f 6e 74 20 74 68 sum based ont th
e6f0: 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
e700: 65 20 0a 2a 2a 20 70 61 67 65 20 6f 66 20 64 61 e .** page of da
e710: 74 61 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 ta and the curre
e720: 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 50 61 67 nt value of pPag
e730: 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 2e 0a 2a er->cksumInit..*
e740: 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74 *.** This is not
e750: 20 61 20 72 65 61 6c 20 63 68 65 63 6b 73 75 6d a real checksum
e760: 2e 20 49 74 20 69 73 20 72 65 61 6c 6c 79 20 6a . It is really j
e770: 75 73 74 20 74 68 65 20 73 75 6d 20 6f 66 20 74 ust the sum of t
e780: 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 69 6e he .** random in
e790: 69 74 69 61 6c 20 76 61 6c 75 65 20 28 70 50 61 itial value (pPa
e7a0: 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 20 ger->cksumInit)
e7b0: 61 6e 64 20 65 76 65 72 79 20 32 30 30 74 68 20 and every 200th
e7c0: 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 70 byte.** of the p
e7d0: 61 67 65 20 64 61 74 61 2c 20 73 74 61 72 74 69 age data, starti
e7e0: 6e 67 20 77 69 74 68 20 62 79 74 65 20 6f 66 66 ng with byte off
e7f0: 73 65 74 20 28 70 50 61 67 65 72 2d 3e 70 61 67 set (pPager->pag
e800: 65 53 69 7a 65 25 32 30 30 29 2e 0a 2a 2a 20 45 eSize%200)..** E
e810: 61 63 68 20 62 79 74 65 20 69 73 20 69 6e 74 65 ach byte is inte
e820: 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 38 2d rpreted as an 8-
e830: 62 69 74 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 bit unsigned int
e840: 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e eger..**.** Chan
e850: 67 69 6e 67 20 74 68 65 20 66 6f 72 6d 75 6c 61 ging the formula
e860: 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 used to compute
e870: 20 74 68 69 73 20 63 68 65 63 6b 73 75 6d 20 72 this checksum r
e880: 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a 2a 2a 20 esults in an.**
e890: 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 6a 6f 75 incompatible jou
e8a0: 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 rnal file format
e8b0: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6a 6f 75 72 6e ..**.** If journ
e8c0: 61 6c 20 63 6f 72 72 75 70 74 69 6f 6e 20 6f 63 al corruption oc
e8d0: 63 75 72 73 20 64 75 65 20 74 6f 20 61 20 70 6f curs due to a po
e8e0: 77 65 72 20 66 61 69 6c 75 72 65 2c 20 74 68 65 wer failure, the
e8f0: 20 6d 6f 73 74 20 6c 69 6b 65 6c 79 20 0a 2a 2a most likely .**
e900: 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 68 61 scenario is tha
e910: 74 20 6f 6e 65 20 65 6e 64 20 6f 72 20 74 68 65 t one end or the
e920: 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 72 65 other of the re
e930: 63 6f 72 64 20 77 69 6c 6c 20 62 65 20 63 68 61 cord will be cha
e940: 6e 67 65 64 2e 20 0a 2a 2a 20 49 74 20 69 73 20 nged. .** It is
e950: 6d 75 63 68 20 6c 65 73 73 20 6c 69 6b 65 6c 79 much less likely
e960: 20 74 68 61 74 20 74 68 65 20 74 77 6f 20 65 6e that the two en
e970: 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 ds of the journa
e980: 6c 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 l record will be
e990: 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 6e 64 20 .** correct and
e9a0: 74 68 65 20 6d 69 64 64 6c 65 20 62 65 20 63 6f the middle be co
e9b0: 72 72 75 70 74 2e 20 20 54 68 75 73 2c 20 74 68 rrupt. Thus, th
e9c0: 69 73 20 22 63 68 65 63 6b 73 75 6d 22 20 73 63 is "checksum" sc
e9d0: 68 65 6d 65 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 heme,.** though
e9e0: 66 61 73 74 20 61 6e 64 20 73 69 6d 70 6c 65 2c fast and simple,
e9f0: 20 63 61 74 63 68 65 73 20 74 68 65 20 6d 6f 73 catches the mos
ea00: 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69 6e 64 20 tly likely kind
ea10: 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a of corruption..*
ea20: 2f 0a 73 74 61 74 69 63 20 75 33 32 20 70 61 67 /.static u32 pag
ea30: 65 72 5f 63 6b 73 75 6d 28 50 61 67 65 72 20 2a er_cksum(Pager *
ea40: 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 75 38 pPager, const u8
ea50: 20 2a 61 44 61 74 61 29 7b 0a 20 20 75 33 32 20 *aData){. u32
ea60: 63 6b 73 75 6d 20 3d 20 70 50 61 67 65 72 2d 3e cksum = pPager->
ea70: 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20 20 cksumInit;
ea80: 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 76 /* Checksum v
ea90: 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a alue to return *
eaa0: 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 50 61 67 /. int i = pPag
eab0: 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d 32 30 30 er->pageSize-200
eac0: 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f ; /* Lo
ead0: 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
eae0: 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20 20 while( i>0 ){.
eaf0: 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44 61 74 61 cksum += aData
eb00: 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d 20 32 30 [i];. i -= 20
eb10: 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
eb20: 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 cksum;.}../*.**
eb30: 52 65 61 64 20 61 20 73 69 6e 67 6c 65 20 70 61 Read a single pa
eb40: 67 65 20 66 72 6f 6d 20 65 69 74 68 65 72 20 74 ge from either t
eb50: 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
eb60: 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d (if isMainJrnl==
eb70: 31 29 20 6f 72 0a 2a 2a 20 66 72 6f 6d 20 74 68 1) or.** from th
eb80: 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 28 69 e sub-journal (i
eb90: 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 30 29 f isMainJrnl==0)
eba0: 20 61 6e 64 20 70 6c 61 79 62 61 63 6b 20 74 68 and playback th
ebb0: 61 74 20 70 61 67 65 2e 0a 2a 2a 20 54 68 65 20 at page..** The
ebc0: 70 61 67 65 20 62 65 67 69 6e 73 20 61 74 20 6f page begins at o
ebd0: 66 66 73 65 74 20 2a 70 4f 66 66 73 65 74 20 69 ffset *pOffset i
ebe0: 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 54 68 nto the file. Th
ebf0: 65 20 2a 70 4f 66 66 73 65 74 0a 2a 2a 20 76 61 e *pOffset.** va
ec00: 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 65 64 lue is increased
ec10: 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 to the start of
ec20: 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 the next page i
ec30: 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a n the journal..*
ec40: 2a 0a 2a 2a 20 54 68 65 20 69 73 4d 61 69 6e 4a *.** The isMainJ
ec50: 72 6e 6c 20 66 6c 61 67 20 69 73 20 74 72 75 65 rnl flag is true
ec60: 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 if this is the
ec70: 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f main rollback jo
ec80: 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 66 61 6c urnal and.** fal
ec90: 73 65 20 66 6f 72 20 74 68 65 20 73 74 61 74 65 se for the state
eca0: 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 ment journal. T
ecb0: 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b he main rollback
ecc0: 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73 0a 2a 2a journal uses.**
ecd0: 20 63 68 65 63 6b 73 75 6d 73 20 2d 20 74 68 65 checksums - the
ece0: 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e statement journ
ecf0: 61 6c 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a al does not..**.
ed00: 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 6e ** If the page n
ed10: 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 umber of the pag
ed20: 65 20 72 65 63 6f 72 64 20 72 65 61 64 20 66 72 e record read fr
ed30: 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 om the (sub-)jou
ed40: 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 rnal file.** is
ed50: 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 greater than the
ed60: 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f current value o
ed70: 66 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 f Pager.dbSize,
ed80: 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 20 69 73 then playback is
ed90: 0a 2a 2a 20 73 6b 69 70 70 65 64 20 61 6e 64 20 .** skipped and
eda0: 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
edb0: 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 urned..**.** If
edc0: 70 44 6f 6e 65 20 69 73 20 6e 6f 74 20 4e 55 4c pDone is not NUL
edd0: 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 61 20 L, then it is a
ede0: 72 65 63 6f 72 64 20 6f 66 20 70 61 67 65 73 20 record of pages
edf0: 74 68 61 74 20 68 61 76 65 20 61 6c 72 65 61 64 that have alread
ee00: 79 0a 2a 2a 20 62 65 65 6e 20 70 6c 61 79 65 64 y.** been played
ee10: 20 62 61 63 6b 2e 20 20 49 66 20 74 68 65 20 70 back. If the p
ee20: 61 67 65 20 61 74 20 2a 70 4f 66 66 73 65 74 20 age at *pOffset
ee30: 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
ee40: 20 70 6c 61 79 65 64 20 62 61 63 6b 0a 2a 2a 20 played back.**
ee50: 28 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f (if the correspo
ee60: 6e 64 69 6e 67 20 70 44 6f 6e 65 20 62 69 74 20 nding pDone bit
ee70: 69 73 20 73 65 74 29 20 74 68 65 6e 20 73 6b 69 is set) then ski
ee80: 70 20 74 68 65 20 70 6c 61 79 62 61 63 6b 2e 0a p the playback..
ee90: 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 ** Make sure the
eea0: 20 70 44 6f 6e 65 20 62 69 74 20 63 6f 72 72 65 pDone bit corre
eeb0: 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 sponding to the
eec0: 2a 70 4f 66 66 73 65 74 20 70 61 67 65 20 69 73 *pOffset page is
eed0: 20 73 65 74 0a 2a 2a 20 70 72 69 6f 72 20 74 6f set.** prior to
eee0: 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a returning..**.*
eef0: 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 72 65 * If the page re
ef00: 63 6f 72 64 20 69 73 20 73 75 63 63 65 73 73 66 cord is successf
ef10: 75 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20 74 ully read from t
ef20: 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c he (sub-)journal
ef30: 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 70 6c 61 file.** and pla
ef40: 79 65 64 20 62 61 63 6b 2c 20 74 68 65 6e 20 53 yed back, then S
ef50: 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
ef60: 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 rned. If an IO e
ef70: 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a 2a 20 77 rror occurs.** w
ef80: 68 69 6c 65 20 72 65 61 64 69 6e 67 20 74 68 65 hile reading the
ef90: 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 74 68 65 record from the
efa0: 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 (sub-)journal f
efb0: 69 6c 65 20 6f 72 20 77 68 69 6c 65 20 77 72 69 ile or while wri
efc0: 74 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 65 20 64 ting.** to the d
efd0: 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 atabase file, th
efe0: 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 en the IO error
eff0: 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
f000: 2e 20 49 66 20 64 61 74 61 0a 2a 2a 20 69 73 20 . If data.** is
f010: 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65 61 successfully rea
f020: 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62 2d d from the (sub-
f030: 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 75 )journal file bu
f040: 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 0a t appears to be.
f050: 2a 2a 20 63 6f 72 72 75 70 74 65 64 2c 20 53 51 ** corrupted, SQ
f060: 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65 74 LITE_DONE is ret
f070: 75 72 6e 65 64 2e 20 44 61 74 61 20 69 73 20 63 urned. Data is c
f080: 6f 6e 73 69 64 65 72 65 64 20 63 6f 72 72 75 70 onsidered corrup
f090: 74 65 64 20 69 6e 0a 2a 2a 20 74 77 6f 20 63 69 ted in.** two ci
f0a0: 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a 2a 2a 20 rcumstances:.**
f0b0: 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 72 .** * If the r
f0c0: 65 63 6f 72 64 20 70 61 67 65 2d 6e 75 6d 62 65 ecord page-numbe
f0d0: 72 20 69 73 20 69 6c 6c 65 67 61 6c 20 28 30 20 r is illegal (0
f0e0: 6f 72 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f or PAGER_MJ_PGNO
f0f0: 29 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20 49 66 20 ), or.** * If
f100: 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 62 65 the record is be
f110: 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 ing rolled back
f120: 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f from the main jo
f130: 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 20 urnal file.**
f140: 20 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b 73 and the checks
f150: 75 6d 20 66 69 65 6c 64 20 64 6f 65 73 20 6e 6f um field does no
f160: 74 20 6d 61 74 63 68 20 74 68 65 20 72 65 63 6f t match the reco
f170: 72 64 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a rd content..**.*
f180: 2a 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68 65 * Neither of the
f190: 73 65 20 74 77 6f 20 73 63 65 6e 61 72 69 6f 73 se two scenarios
f1a0: 20 61 72 65 20 70 6f 73 73 69 62 6c 65 20 64 75 are possible du
f1b0: 72 69 6e 67 20 61 20 73 61 76 65 70 6f 69 6e 74 ring a savepoint
f1c0: 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a rollback..**.**
f1d0: 20 49 66 20 74 68 69 73 20 69 73 20 61 20 73 61 If this is a sa
f1e0: 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b vepoint rollback
f1f0: 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 20 6d 61 , then memory ma
f200: 79 20 68 61 76 65 20 74 6f 20 62 65 20 64 79 6e y have to be dyn
f210: 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f amically.** allo
f220: 63 61 74 65 64 20 62 79 20 74 68 69 73 20 66 75 cated by this fu
f230: 6e 63 74 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 nction. If this
f240: 69 73 20 74 68 65 20 63 61 73 65 20 61 6e 64 20 is the case and
f250: 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 an allocation fa
f260: 69 6c 73 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e ils,.** SQLITE_N
f270: 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 OMEM is returned
f280: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
f290: 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f pager_playback_o
f2a0: 6e 65 5f 70 61 67 65 28 0a 20 20 50 61 67 65 72 ne_page(. Pager
f2b0: 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 *pPager,
f2c0: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
f2d0: 70 61 67 65 72 20 62 65 69 6e 67 20 70 6c 61 79 pager being play
f2e0: 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 36 34 ed back */. i64
f2f0: 20 2a 70 4f 66 66 73 65 74 2c 20 20 20 20 20 20 *pOffset,
f300: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 /* Of
f310: 66 73 65 74 20 6f 66 20 72 65 63 6f 72 64 20 74 fset of record t
f320: 6f 20 70 6c 61 79 62 61 63 6b 20 2a 2f 0a 20 20 o playback */.
f330: 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 2c 20 20 Bitvec *pDone,
f340: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f350: 20 42 69 74 76 65 63 20 6f 66 20 70 61 67 65 73 Bitvec of pages
f360: 20 61 6c 72 65 61 64 79 20 70 6c 61 79 65 64 20 already played
f370: 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 73 back */. int is
f380: 4d 61 69 6e 4a 72 6e 6c 2c 20 20 20 20 20 20 20 MainJrnl,
f390: 20 20 20 20 20 20 20 20 2f 2a 20 31 20 2d 3e 20 /* 1 ->
f3a0: 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 2e 20 30 20 main journal. 0
f3b0: 2d 3e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 -> sub-journal.
f3c0: 2a 2f 0a 20 20 69 6e 74 20 69 73 53 61 76 65 70 */. int isSavep
f3d0: 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 nt
f3e0: 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 /* True for a
f3f0: 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 savepoint rollb
f400: 61 63 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ack */.){. int
f410: 72 63 3b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 rc;. PgHdr *pPg
f420: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
f430: 20 20 20 20 2f 2a 20 41 6e 20 65 78 69 73 74 69 /* An existi
f440: 6e 67 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 ng page in the c
f450: 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 ache */. Pgno p
f460: 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 gno;
f470: 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 /* The p
f480: 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 age number of a
f490: 70 61 67 65 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 page in journal
f4a0: 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20 */. u32 cksum;
f4b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f4c0: 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 75 /* Checksum u
f4d0: 73 65 64 20 66 6f 72 20 73 61 6e 69 74 79 20 63 sed for sanity c
f4e0: 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 63 68 61 hecking */. cha
f4f0: 72 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 r *aData;
f500: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 /* Te
f510: 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 20 mporary storage
f520: 66 6f 72 20 74 68 65 20 70 61 67 65 20 2a 2f 0a for the page */.
f530: 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
f540: 6a 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 jfd;
f550: 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 /* The file desc
f560: 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65 20 6a riptor for the j
f570: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 ournal file */.
f580: 20 69 6e 74 20 69 73 53 79 6e 63 65 64 3b 20 20 int isSynced;
f590: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
f5a0: 2a 20 54 72 75 65 20 69 66 20 6a 6f 75 72 6e 61 * True if journa
f5b0: 6c 20 70 61 67 65 20 69 73 20 73 79 6e 63 65 64 l page is synced
f5c0: 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 28 */.. assert( (
f5d0: 69 73 4d 61 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d isMainJrnl&~1)==
f5e0: 30 20 29 3b 20 20 20 20 20 20 2f 2a 20 69 73 4d 0 ); /* isM
f5f0: 61 69 6e 4a 72 6e 6c 20 69 73 20 30 20 6f 72 20 ainJrnl is 0 or
f600: 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 1 */. assert( (
f610: 69 73 53 61 76 65 70 6e 74 26 7e 31 29 3d 3d 30 isSavepnt&~1)==0
f620: 20 29 3b 20 20 20 20 20 20 20 2f 2a 20 69 73 53 ); /* isS
f630: 61 76 65 70 6e 74 20 69 73 20 30 20 6f 72 20 31 avepnt is 0 or 1
f640: 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 */. assert( is
f650: 4d 61 69 6e 4a 72 6e 6c 20 7c 7c 20 70 44 6f 6e MainJrnl || pDon
f660: 65 20 29 3b 20 20 20 20 20 2f 2a 20 70 44 6f 6e e ); /* pDon
f670: 65 20 61 6c 77 61 79 73 20 75 73 65 64 20 6f 6e e always used on
f680: 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f sub-journals */
f690: 0a 20 20 61 73 73 65 72 74 28 20 69 73 53 61 76 . assert( isSav
f6a0: 65 70 6e 74 20 7c 7c 20 70 44 6f 6e 65 3d 3d 30 epnt || pDone==0
f6b0: 20 29 3b 20 20 20 2f 2a 20 70 44 6f 6e 65 20 6e ); /* pDone n
f6c0: 65 76 65 72 20 75 73 65 64 20 6f 6e 20 6e 6f 6e ever used on non
f6d0: 2d 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 -savepoint */..
f6e0: 20 61 44 61 74 61 20 3d 20 70 50 61 67 65 72 2d aData = pPager-
f6f0: 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 61 73 >pTmpSpace;. as
f700: 73 65 72 74 28 20 61 44 61 74 61 20 29 3b 20 20 sert( aData );
f710: 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 73 /* Temp s
f720: 74 6f 72 61 67 65 20 6d 75 73 74 20 68 61 76 65 torage must have
f730: 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c already been al
f740: 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 61 73 73 located */. ass
f750: 65 72 74 28 20 70 61 67 65 72 55 73 65 57 61 6c ert( pagerUseWal
f760: 28 70 50 61 67 65 72 29 3d 3d 30 20 7c 7c 20 28 (pPager)==0 || (
f770: 21 69 73 4d 61 69 6e 4a 72 6e 6c 20 26 26 20 69 !isMainJrnl && i
f780: 73 53 61 76 65 70 6e 74 29 20 29 3b 0a 0a 20 20 sSavepnt) );..
f790: 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65 /* Read the page
f7a0: 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67 65 number and page
f7b0: 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 6a data from the j
f7c0: 6f 75 72 6e 61 6c 20 6f 72 20 73 75 62 2d 6a 6f ournal or sub-jo
f7d0: 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 2e urnal. ** file.
f7e0: 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 Return an error
f7f0: 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c code to the cal
f800: 6c 65 72 20 69 66 20 61 6e 20 49 4f 20 65 72 72 ler if an IO err
f810: 6f 72 20 6f 63 63 75 72 73 2e 0a 20 20 2a 2f 0a or occurs.. */.
f820: 20 20 6a 66 64 20 3d 20 69 73 4d 61 69 6e 4a 72 jfd = isMainJr
f830: 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e 6a 66 64 nl ? pPager->jfd
f840: 20 3a 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 3b : pPager->sjfd;
f850: 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 . rc = read32bi
f860: 74 73 28 6a 66 64 2c 20 2a 70 4f 66 66 73 65 74 ts(jfd, *pOffset
f870: 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 , &pgno);. if(
f880: 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
f890: 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 return rc;. rc
f8a0: 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 = sqlite3OsRead(
f8b0: 6a 66 64 2c 20 28 75 38 2a 29 61 44 61 74 61 2c jfd, (u8*)aData,
f8c0: 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
f8d0: 65 2c 20 28 2a 70 4f 66 66 73 65 74 29 2b 34 29 e, (*pOffset)+4)
f8e0: 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
f8f0: 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 TE_OK ) return r
f900: 63 3b 0a 20 20 2a 70 4f 66 66 73 65 74 20 2b 3d c;. *pOffset +=
f910: 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
f920: 65 20 2b 20 34 20 2b 20 69 73 4d 61 69 6e 4a 72 e + 4 + isMainJr
f930: 6e 6c 2a 34 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 nl*4;.. /* Sani
f940: 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 ty checking on t
f950: 68 65 20 70 61 67 65 2e 20 20 54 68 69 73 20 69 he page. This i
f960: 73 20 6d 6f 72 65 20 69 6d 70 6f 72 74 61 6e 74 s more important
f970: 20 74 68 61 74 20 49 20 6f 72 69 67 69 6e 61 6c that I original
f980: 6c 79 0a 20 20 2a 2a 20 74 68 6f 75 67 68 74 2e ly. ** thought.
f990: 20 20 49 66 20 61 20 70 6f 77 65 72 20 66 61 69 If a power fai
f9a0: 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c lure occurs whil
f9b0: 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 e the journal is
f9c0: 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 0a being written,.
f9d0: 20 20 2a 2a 20 69 74 20 63 6f 75 6c 64 20 63 61 ** it could ca
f9e0: 75 73 65 20 69 6e 76 61 6c 69 64 20 64 61 74 61 use invalid data
f9f0: 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 to be written i
fa00: 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e nto the journal.
fa10: 20 20 57 65 20 6e 65 65 64 20 74 6f 0a 20 20 2a We need to. *
fa20: 2a 20 64 65 74 65 63 74 20 74 68 69 73 20 69 6e * detect this in
fa30: 76 61 6c 69 64 20 64 61 74 61 20 28 77 69 74 68 valid data (with
fa40: 20 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74 high probabilit
fa50: 79 29 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 y) and ignore it
fa60: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 67 6e .. */. if( pgn
fa70: 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 o==0 || pgno==PA
fa80: 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 GER_MJ_PGNO(pPag
fa90: 65 72 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 er) ){. asser
faa0: 74 28 20 21 69 73 53 61 76 65 70 6e 74 20 29 3b t( !isSavepnt );
fab0: 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
fac0: 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 TE_DONE;. }. i
fad0: 66 28 20 70 67 6e 6f 3e 28 50 67 6e 6f 29 70 50 f( pgno>(Pgno)pP
fae0: 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 7c 7c 20 ager->dbSize ||
faf0: 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 sqlite3BitvecTes
fb00: 74 28 70 44 6f 6e 65 2c 20 70 67 6e 6f 29 20 29 t(pDone, pgno) )
fb10: 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
fb20: 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 ITE_OK;. }. if
fb30: 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 29 7b 0a ( isMainJrnl ){.
fb40: 20 20 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 rc = read32b
fb50: 69 74 73 28 6a 66 64 2c 20 28 2a 70 4f 66 66 73 its(jfd, (*pOffs
fb60: 65 74 29 2d 34 2c 20 26 63 6b 73 75 6d 29 3b 0a et)-4, &cksum);.
fb70: 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
fb80: 75 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20 urn rc;. if(
fb90: 21 69 73 53 61 76 65 70 6e 74 20 26 26 20 70 61 !isSavepnt && pa
fba0: 67 65 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 ger_cksum(pPager
fbb0: 2c 20 28 75 38 2a 29 61 44 61 74 61 29 21 3d 63 , (u8*)aData)!=c
fbc0: 6b 73 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 65 ksum ){. re
fbd0: 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 turn SQLITE_DONE
fbe0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 ;. }. }.. i
fbf0: 66 28 20 70 44 6f 6e 65 20 26 26 20 28 72 63 20 f( pDone && (rc
fc00: 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 = sqlite3BitvecS
fc10: 65 74 28 70 44 6f 6e 65 2c 20 70 67 6e 6f 29 29 et(pDone, pgno))
fc20: 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
fc30: 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
fc40: 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 }.. assert( pPa
fc50: 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 ger->state==PAGE
fc60: 52 5f 52 45 53 45 52 56 45 44 20 7c 7c 20 70 50 R_RESERVED || pP
fc70: 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
fc80: 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a ER_EXCLUSIVE );.
fc90: 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 . /* If the pag
fca0: 65 72 20 69 73 20 69 6e 20 52 45 53 45 52 56 45 er is in RESERVE
fcb0: 44 20 73 74 61 74 65 2c 20 74 68 65 6e 20 74 68 D state, then th
fcc0: 65 72 65 20 6d 75 73 74 20 62 65 20 61 20 63 6f ere must be a co
fcd0: 70 79 20 6f 66 20 74 68 69 73 0a 20 20 2a 2a 20 py of this. **
fce0: 70 61 67 65 20 69 6e 20 74 68 65 20 70 61 67 65 page in the page
fcf0: 72 20 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73 r cache. In this
fd00: 20 63 61 73 65 20 6a 75 73 74 20 75 70 64 61 74 case just updat
fd10: 65 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 e the pager cach
fd20: 65 2c 0a 20 20 2a 2a 20 6e 6f 74 20 74 68 65 20 e,. ** not the
fd30: 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 database file. T
fd40: 68 65 20 70 61 67 65 20 69 73 20 6c 65 66 74 20 he page is left
fd50: 6d 61 72 6b 65 64 20 64 69 72 74 79 20 69 6e 20 marked dirty in
fd60: 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2a 0a this case.. **.
fd70: 20 20 2a 2a 20 41 6e 20 65 78 63 65 70 74 69 6f ** An exceptio
fd80: 6e 20 74 6f 20 74 68 65 20 61 62 6f 76 65 20 72 n to the above r
fd90: 75 6c 65 3a 20 49 66 20 74 68 65 20 64 61 74 61 ule: If the data
fda0: 62 61 73 65 20 69 73 20 69 6e 20 6e 6f 2d 73 79 base is in no-sy
fdb0: 6e 63 20 6d 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 nc mode. ** and
fdc0: 20 61 20 70 61 67 65 20 69 73 20 6d 6f 76 65 64 a page is moved
fdd0: 20 64 75 72 69 6e 67 20 61 6e 20 69 6e 63 72 65 during an incre
fde0: 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 74 68 mental vacuum th
fdf0: 65 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79 0a en the page may.
fe00: 20 20 2a 2a 20 6e 6f 74 20 62 65 20 69 6e 20 74 ** not be in t
fe10: 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 he pager cache.
fe20: 4c 61 74 65 72 3a 20 69 66 20 61 20 6d 61 6c 6c Later: if a mall
fe30: 6f 63 28 29 20 6f 72 20 49 4f 20 65 72 72 6f 72 oc() or IO error
fe40: 20 6f 63 63 75 72 73 0a 20 20 2a 2a 20 64 75 72 occurs. ** dur
fe50: 69 6e 67 20 61 20 4d 6f 76 65 70 61 67 65 28 29 ing a Movepage()
fe60: 20 63 61 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 call, then the
fe70: 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 page may not be
fe80: 69 6e 20 74 68 65 20 63 61 63 68 65 0a 20 20 2a in the cache. *
fe90: 2a 20 65 69 74 68 65 72 2e 20 53 6f 20 74 68 65 * either. So the
fea0: 20 63 6f 6e 64 69 74 69 6f 6e 20 64 65 73 63 72 condition descr
feb0: 69 62 65 64 20 69 6e 20 74 68 65 20 61 62 6f 76 ibed in the abov
fec0: 65 20 70 61 72 61 67 72 61 70 68 20 69 73 20 6e e paragraph is n
fed0: 6f 74 0a 20 20 2a 2a 20 61 73 73 65 72 74 28 29 ot. ** assert()
fee0: 61 62 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 able.. **. **
fef0: 49 66 20 69 6e 20 45 58 43 4c 55 53 49 56 45 20 If in EXCLUSIVE
ff00: 73 74 61 74 65 2c 20 74 68 65 6e 20 77 65 20 75 state, then we u
ff10: 70 64 61 74 65 20 74 68 65 20 70 61 67 65 72 20 pdate the pager
ff20: 63 61 63 68 65 20 69 66 20 69 74 20 65 78 69 73 cache if it exis
ff30: 74 73 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 ts. ** and the
ff40: 6d 61 69 6e 20 66 69 6c 65 2e 20 54 68 65 20 70 main file. The p
ff50: 61 67 65 20 69 73 20 74 68 65 6e 20 6d 61 72 6b age is then mark
ff60: 65 64 20 6e 6f 74 20 64 69 72 74 79 2e 0a 20 20 ed not dirty..
ff70: 2a 2a 0a 20 20 2a 2a 20 54 69 63 6b 65 74 20 23 **. ** Ticket #
ff80: 31 31 37 31 3a 20 20 54 68 65 20 73 74 61 74 65 1171: The state
ff90: 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 ment journal mig
ffa0: 68 74 20 63 6f 6e 74 61 69 6e 20 70 61 67 65 20 ht contain page
ffb0: 63 6f 6e 74 65 6e 74 20 74 68 61 74 20 69 73 0a content that is.
ffc0: 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 66 ** different f
ffd0: 72 6f 6d 20 74 68 65 20 70 61 67 65 20 63 6f 6e rom the page con
ffe0: 74 65 6e 74 20 61 74 20 74 68 65 20 73 74 61 72 tent at the star
fff0: 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 t of the transac
10000 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 69 73 20 tion.. ** This
10010 6f 63 63 75 72 73 20 77 68 65 6e 20 61 20 70 61 occurs when a pa
10020 67 65 20 69 73 20 63 68 61 6e 67 65 64 20 70 72 ge is changed pr
10030 69 6f 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 ior to the start
10040 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 0a of a statement.
10050 20 20 2a 2a 20 74 68 65 6e 20 63 68 61 6e 67 65 ** then change
10060 64 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 d again within t
10070 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 57 he statement. W
10080 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b hen rolling back
10090 20 73 75 63 68 20 61 0a 20 20 2a 2a 20 73 74 61 such a. ** sta
100a0 74 65 6d 65 6e 74 20 77 65 20 6d 75 73 74 20 6e tement we must n
100b0 6f 74 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 ot write to the
100c0 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 original databas
100d0 65 20 75 6e 6c 65 73 73 20 77 65 20 6b 6e 6f 77 e unless we know
100e0 0a 20 20 2a 2a 20 66 6f 72 20 63 65 72 74 61 69 . ** for certai
100f0 6e 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c 20 n that original
10100 70 61 67 65 20 63 6f 6e 74 65 6e 74 73 20 61 72 page contents ar
10110 65 20 73 79 6e 63 65 64 20 69 6e 74 6f 20 74 68 e synced into th
10120 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a e main rollback.
10130 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f ** journal. O
10140 74 68 65 72 77 69 73 65 2c 20 61 20 70 6f 77 65 therwise, a powe
10150 72 20 6c 6f 73 73 20 6d 69 67 68 74 20 6c 65 61 r loss might lea
10160 76 65 20 6d 6f 64 69 66 69 65 64 20 64 61 74 61 ve modified data
10170 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 in the. ** dat
10180 61 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 6f abase file witho
10190 75 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 ut an entry in t
101a0 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 he rollback jour
101b0 6e 61 6c 20 74 68 61 74 20 63 61 6e 0a 20 20 2a nal that can. *
101c0 2a 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 * restore the da
101d0 74 61 62 61 73 65 20 74 6f 20 69 74 73 20 6f 72 tabase to its or
101e0 69 67 69 6e 61 6c 20 66 6f 72 6d 2e 20 20 54 77 iginal form. Tw
101f0 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 20 6d 75 73 o conditions mus
10200 74 20 62 65 0a 20 20 2a 2a 20 6d 65 74 20 62 65 t be. ** met be
10210 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20 fore writing to
10220 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
10230 65 73 2e 20 28 31 29 20 74 68 65 20 64 61 74 61 es. (1) the data
10240 62 61 73 65 20 6d 75 73 74 20 62 65 0a 20 20 2a base must be. *
10250 2a 20 6c 6f 63 6b 65 64 2e 20 20 28 32 29 20 77 * locked. (2) w
10260 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 e know that the
10270 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63 6f original page co
10280 6e 74 65 6e 74 20 69 73 20 66 75 6c 6c 79 20 73 ntent is fully s
10290 79 6e 63 65 64 0a 20 20 2a 2a 20 69 6e 20 74 68 ynced. ** in th
102a0 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 65 e main journal e
102b0 69 74 68 65 72 20 62 65 63 61 75 73 65 20 74 68 ither because th
102c0 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e e page is not in
102d0 20 63 61 63 68 65 20 6f 72 20 65 6c 73 65 0a 20 cache or else.
102e0 20 2a 2a 20 74 68 65 20 70 61 67 65 20 69 73 20 ** the page is
102f0 6d 61 72 6b 65 64 20 61 73 20 6e 65 65 64 53 79 marked as needSy
10300 6e 63 3d 3d 30 2e 0a 20 20 2a 2a 0a 20 20 2a 2a nc==0.. **. **
10310 20 32 30 30 38 2d 30 34 2d 31 34 3a 20 20 57 68 2008-04-14: Wh
10320 65 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f en attempting to
10330 20 76 61 63 75 75 6d 20 61 20 63 6f 72 72 75 70 vacuum a corrup
10340 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c t database file,
10350 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f 73 73 it. ** is poss
10360 69 62 6c 65 20 74 6f 20 66 61 69 6c 20 61 20 73 ible to fail a s
10370 74 61 74 65 6d 65 6e 74 20 6f 6e 20 61 20 64 61 tatement on a da
10380 74 61 62 61 73 65 20 74 68 61 74 20 64 6f 65 73 tabase that does
10390 20 6e 6f 74 20 79 65 74 20 65 78 69 73 74 2e 0a not yet exist..
103a0 20 20 2a 2a 20 44 6f 20 6e 6f 74 20 61 74 74 65 ** Do not atte
103b0 6d 70 74 20 74 6f 20 77 72 69 74 65 20 69 66 20 mpt to write if
103c0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 database file ha
103d0 73 20 6e 65 76 65 72 20 62 65 65 6e 20 6f 70 65 s never been ope
103e0 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ned.. */. if(
103f0 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 pagerUseWal(pPag
10400 65 72 29 20 29 7b 0a 20 20 20 20 70 50 67 20 3d er) ){. pPg =
10410 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
10420 20 70 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f 6f pPg = pager_loo
10430 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e 6f kup(pPager, pgno
10440 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 );. }. assert(
10450 20 70 50 67 20 7c 7c 20 21 4d 45 4d 44 42 20 29 pPg || !MEMDB )
10460 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 ;. PAGERTRACE((
10470 22 50 4c 41 59 42 41 43 4b 20 25 64 20 70 61 67 "PLAYBACK %d pag
10480 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29 20 e %d hash(%08x)
10490 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 %s\n",.
104a0 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 PAGERID(pPager
104b0 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 64 ), pgno, pager_d
104c0 61 74 61 68 61 73 68 28 70 50 61 67 65 72 2d 3e atahash(pPager->
104d0 70 61 67 65 53 69 7a 65 2c 20 28 75 38 2a 29 61 pageSize, (u8*)a
104e0 44 61 74 61 29 2c 0a 20 20 20 20 20 20 20 20 20 Data),.
104f0 20 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 3f 22 6d (isMainJrnl?"m
10500 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22 3a 22 73 75 ain-journal":"su
10510 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a 20 20 29 29 b-journal"). ))
10520 3b 0a 20 20 69 66 28 20 69 73 4d 61 69 6e 4a 72 ;. if( isMainJr
10530 6e 6c 20 29 7b 0a 20 20 20 20 69 73 53 79 6e 63 nl ){. isSync
10540 65 64 20 3d 20 70 50 61 67 65 72 2d 3e 6e 6f 53 ed = pPager->noS
10550 79 6e 63 20 7c 7c 20 28 2a 70 4f 66 66 73 65 74 ync || (*pOffset
10560 20 3c 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 <= pPager->jour
10570 6e 61 6c 48 64 72 29 3b 0a 20 20 7d 65 6c 73 65 nalHdr);. }else
10580 7b 0a 20 20 20 20 69 73 53 79 6e 63 65 64 20 3d {. isSynced =
10590 20 28 70 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d 28 (pPg==0 || 0==(
105a0 70 50 67 2d 3e 66 6c 61 67 73 20 26 20 50 47 48 pPg->flags & PGH
105b0 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 3b 0a DR_NEED_SYNC));.
105c0 20 20 7d 0a 20 20 69 66 28 20 28 70 50 61 67 65 }. if( (pPage
105d0 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f r->state>=PAGER_
105e0 45 58 43 4c 55 53 49 56 45 29 0a 20 20 20 26 26 EXCLUSIVE). &&
105f0 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
10600 66 64 29 0a 20 20 20 26 26 20 69 73 53 79 6e 63 fd). && isSync
10610 65 64 0a 20 20 29 7b 0a 20 20 20 20 69 36 34 20 ed. ){. i64
10620 6f 66 73 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a ofst = (pgno-1)*
10630 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 (i64)pPager->pag
10640 65 53 69 7a 65 3b 0a 20 20 20 20 74 65 73 74 63 eSize;. testc
10650 61 73 65 28 20 21 69 73 53 61 76 65 70 6e 74 20 ase( !isSavepnt
10660 26 26 20 70 50 67 21 3d 30 20 26 26 20 28 70 50 && pPg!=0 && (pP
10670 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
10680 45 45 44 5f 53 59 4e 43 29 21 3d 30 20 29 3b 0a EED_SYNC)!=0 );.
10690 20 20 20 20 61 73 73 65 72 74 28 20 21 70 61 67 assert( !pag
106a0 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 erUseWal(pPager)
106b0 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c );. rc = sql
106c0 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 ite3OsWrite(pPag
106d0 65 72 2d 3e 66 64 2c 20 28 75 38 2a 29 61 44 61 er->fd, (u8*)aDa
106e0 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 ta, pPager->page
106f0 53 69 7a 65 2c 20 6f 66 73 74 29 3b 0a 20 20 20 Size, ofst);.
10700 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 if( pgno>pPager
10710 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a ->dbFileSize ){.
10720 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 pPager->db
10730 46 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b FileSize = pgno;
10740 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
10750 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 29 Pager->pBackup )
10760 7b 0a 20 20 20 20 20 20 43 4f 44 45 43 31 28 70 {. CODEC1(p
10770 50 61 67 65 72 2c 20 61 44 61 74 61 2c 20 70 67 Pager, aData, pg
10780 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c 49 54 45 no, 3, rc=SQLITE
10790 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 20 20 73 _NOMEM);. s
107a0 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 qlite3BackupUpda
107b0 74 65 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b te(pPager->pBack
107c0 75 70 2c 20 70 67 6e 6f 2c 20 28 75 38 2a 29 61 up, pgno, (u8*)a
107d0 44 61 74 61 29 3b 0a 20 20 20 20 20 20 43 4f 44 Data);. COD
107e0 45 43 32 28 70 50 61 67 65 72 2c 20 61 44 61 74 EC2(pPager, aDat
107f0 61 2c 20 70 67 6e 6f 2c 20 37 2c 20 72 63 3d 53 a, pgno, 7, rc=S
10800 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 61 44 61 QLITE_NOMEM, aDa
10810 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c ta);. }. }el
10820 73 65 20 69 66 28 20 21 69 73 4d 61 69 6e 4a 72 se if( !isMainJr
10830 6e 6c 20 26 26 20 70 50 67 3d 3d 30 20 29 7b 0a nl && pPg==0 ){.
10840 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 /* If this i
10850 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 s a rollback of
10860 61 20 73 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 a savepoint and
10870 64 61 74 61 20 77 61 73 20 6e 6f 74 20 77 72 69 data was not wri
10880 74 74 65 6e 20 74 6f 0a 20 20 20 20 2a 2a 20 74 tten to. ** t
10890 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 he database and
108a0 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 the page is not
108b0 69 6e 2d 6d 65 6d 6f 72 79 2c 20 74 68 65 72 65 in-memory, there
108c0 20 69 73 20 61 20 70 6f 74 65 6e 74 69 61 6c 0a is a potential.
108d0 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 2e 20 ** problem.
108e0 57 68 65 6e 20 74 68 65 20 70 61 67 65 20 69 73 When the page is
108f0 20 6e 65 78 74 20 66 65 74 63 68 65 64 20 62 79 next fetched by
10900 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 79 65 the b-tree laye
10910 72 2c 20 69 74 20 0a 20 20 20 20 2a 2a 20 77 69 r, it . ** wi
10920 6c 6c 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 ll be read from
10930 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
10940 65 2c 20 77 68 69 63 68 20 6d 61 79 20 6f 72 20 e, which may or
10950 6d 61 79 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 may not be .
10960 2a 2a 20 63 75 72 72 65 6e 74 2e 20 0a 20 20 20 ** current. .
10970 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 72 65 **. ** There
10980 20 61 72 65 20 61 20 63 6f 75 70 6c 65 20 6f 66 are a couple of
10990 20 64 69 66 66 65 72 65 6e 74 20 77 61 79 73 20 different ways
109a0 74 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 2e this can happen.
109b0 20 41 6c 6c 20 61 72 65 20 71 75 69 74 65 0a 20 All are quite.
109c0 20 20 20 2a 2a 20 6f 62 73 63 75 72 65 2e 20 57 ** obscure. W
109d0 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 73 hen running in s
109e0 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c ynchronous mode,
109f0 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 68 this can only h
10a00 61 70 70 65 6e 20 0a 20 20 20 20 2a 2a 20 69 66 appen . ** if
10a10 20 74 68 65 20 70 61 67 65 20 69 73 20 6f 6e 20 the page is on
10a20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 61 74 the free-list at
10a30 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 the start of th
10a40 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 e transaction, t
10a50 68 65 6e 0a 20 20 20 20 2a 2a 20 70 6f 70 75 6c hen. ** popul
10a60 61 74 65 64 2c 20 74 68 65 6e 20 6d 6f 76 65 64 ated, then moved
10a70 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 50 61 using sqlite3Pa
10a80 67 65 72 4d 6f 76 65 70 61 67 65 28 29 2e 0a 20 gerMovepage()..
10a90 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 **. ** The
10aa0 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 74 6f 20 solution is to
10ab0 61 64 64 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 add an in-memory
10ac0 20 70 61 67 65 20 74 6f 20 74 68 65 20 63 61 63 page to the cac
10ad0 68 65 20 63 6f 6e 74 61 69 6e 69 6e 67 0a 20 20 he containing.
10ae0 20 20 2a 2a 20 74 68 65 20 64 61 74 61 20 6a 75 ** the data ju
10af0 73 74 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 st read from the
10b00 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 4d 61 sub-journal. Ma
10b10 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 64 rk the page as d
10b20 69 72 74 79 20 0a 20 20 20 20 2a 2a 20 61 6e 64 irty . ** and
10b30 20 69 66 20 74 68 65 20 70 61 67 65 72 20 72 65 if the pager re
10b40 71 75 69 72 65 73 20 61 20 6a 6f 75 72 6e 61 6c quires a journal
10b50 2d 73 79 6e 63 2c 20 74 68 65 6e 20 6d 61 72 6b -sync, then mark
10b60 20 74 68 65 20 70 61 67 65 20 61 73 20 0a 20 20 the page as .
10b70 20 20 2a 2a 20 72 65 71 75 69 72 69 6e 67 20 61 ** requiring a
10b80 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 62 65 journal-sync be
10b90 66 6f 72 65 20 69 74 20 69 73 20 77 72 69 74 74 fore it is writt
10ba0 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 en.. */. a
10bb0 73 73 65 72 74 28 20 69 73 53 61 76 65 70 6e 74 ssert( isSavepnt
10bc0 20 29 3b 0a 20 20 20 20 69 66 28 20 28 72 63 20 );. if( (rc
10bd0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 = sqlite3PagerAc
10be0 71 75 69 72 65 28 70 50 61 67 65 72 2c 20 70 67 quire(pPager, pg
10bf0 6e 6f 2c 20 26 70 50 67 2c 20 31 29 29 21 3d 53 no, &pPg, 1))!=S
10c00 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
10c10 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
10c20 20 7d 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 }. pPg->flag
10c30 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 s &= ~PGHDR_NEED
10c40 5f 52 45 41 44 3b 0a 20 20 20 20 73 71 6c 69 74 _READ;. sqlit
10c50 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 e3PcacheMakeDirt
10c60 79 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 69 66 y(pPg);. }. if
10c70 28 20 70 50 67 20 29 7b 0a 20 20 20 20 2f 2a 20 ( pPg ){. /*
10c80 4e 6f 20 70 61 67 65 20 73 68 6f 75 6c 64 20 65 No page should e
10c90 76 65 72 20 62 65 20 65 78 70 6c 69 63 69 74 6c ver be explicitl
10ca0 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 y rolled back th
10cb0 61 74 20 69 73 20 69 6e 20 75 73 65 2c 20 65 78 at is in use, ex
10cc0 63 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20 cept. ** for
10cd0 70 61 67 65 20 31 20 77 68 69 63 68 20 69 73 20 page 1 which is
10ce0 68 65 6c 64 20 69 6e 20 75 73 65 20 69 6e 20 6f held in use in o
10cf0 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 rder to keep the
10d00 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 lock on the.
10d10 20 2a 2a 20 64 61 74 61 62 61 73 65 20 61 63 74 ** database act
10d20 69 76 65 2e 20 48 6f 77 65 76 65 72 20 73 75 63 ive. However suc
10d30 68 20 61 20 70 61 67 65 20 6d 61 79 20 62 65 20 h a page may be
10d40 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73 20 61 rolled back as a
10d50 20 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 6f result. ** o
10d60 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 72 f an internal er
10d70 72 6f 72 20 72 65 73 75 6c 74 69 6e 67 20 69 6e ror resulting in
10d80 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 63 61 an automatic ca
10d90 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c ll to. ** sql
10da0 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 ite3PagerRollbac
10db0 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 k().. */.
10dc0 76 6f 69 64 20 2a 70 44 61 74 61 3b 0a 20 20 20 void *pData;.
10dd0 20 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 pData = pPg->pD
10de0 61 74 61 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 ata;. memcpy(
10df0 70 44 61 74 61 2c 20 28 75 38 2a 29 61 44 61 74 pData, (u8*)aDat
10e00 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 a, pPager->pageS
10e10 69 7a 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 ize);. pPager
10e20 2d 3e 78 52 65 69 6e 69 74 65 72 28 70 50 67 29 ->xReiniter(pPg)
10e30 3b 0a 20 20 20 20 69 66 28 20 69 73 4d 61 69 6e ;. if( isMain
10e40 4a 72 6e 6c 20 26 26 20 28 21 69 73 53 61 76 65 Jrnl && (!isSave
10e50 70 6e 74 20 7c 7c 20 2a 70 4f 66 66 73 65 74 3c pnt || *pOffset<
10e60 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c =pPager->journal
10e70 48 64 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a Hdr) ){. /*
10e80 20 49 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 If the contents
10e90 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 77 65 of this page we
10ea0 72 65 20 6a 75 73 74 20 72 65 73 74 6f 72 65 64 re just restored
10eb0 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 0a from the main .
10ec0 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c ** journal
10ed0 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 73 20 file, then its
10ee0 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 content must be
10ef0 61 73 20 74 68 65 79 20 77 65 72 65 20 77 68 65 as they were whe
10f00 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 n the . **
10f10 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 transaction was
10f20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 49 6e first opened. In
10f30 20 74 68 69 73 20 63 61 73 65 20 77 65 20 63 61 this case we ca
10f40 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 0a n mark the page.
10f50 20 20 20 20 20 20 2a 2a 20 61 73 20 63 6c 65 61 ** as clea
10f60 6e 2c 20 73 69 6e 63 65 20 74 68 65 72 65 20 77 n, since there w
10f70 69 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20 74 ill be no need t
10f80 6f 20 77 72 69 74 65 20 69 74 20 6f 75 74 20 74 o write it out t
10f90 6f 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 o the. ** d
10fa0 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 2a atabase.. *
10fb0 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 *. ** There
10fc0 20 69 73 20 6f 6e 65 20 65 78 63 65 70 74 69 6f is one exceptio
10fd0 6e 20 74 6f 20 74 68 69 73 20 72 75 6c 65 2e 20 n to this rule.
10fe0 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 62 If the page is b
10ff0 65 69 6e 67 20 72 6f 6c 6c 65 64 0a 20 20 20 20 eing rolled.
11000 20 20 2a 2a 20 62 61 63 6b 20 61 73 20 70 61 72 ** back as par
11010 74 20 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 t of a savepoint
11020 20 28 6f 72 20 73 74 61 74 65 6d 65 6e 74 29 20 (or statement)
11030 72 6f 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 61 6e rollback from an
11040 20 0a 20 20 20 20 20 20 2a 2a 20 75 6e 73 79 6e . ** unsyn
11050 63 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 ced portion of t
11060 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
11070 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 file, then it is
11080 20 6e 6f 74 20 73 61 66 65 0a 20 20 20 20 20 20 not safe.
11090 2a 2a 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70 ** to mark the p
110a0 61 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 54 68 age as clean. Th
110b0 69 73 20 69 73 20 62 65 63 61 75 73 65 20 6d 61 is is because ma
110c0 72 6b 69 6e 67 20 74 68 65 20 70 61 67 65 20 61 rking the page a
110d0 73 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 6e s. ** clean
110e0 20 77 69 6c 6c 20 63 6c 65 61 72 20 74 68 65 20 will clear the
110f0 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 PGHDR_NEED_SYNC
11100 66 6c 61 67 2e 20 53 69 6e 63 65 20 74 68 65 20 flag. Since the
11110 70 61 67 65 20 69 73 0a 20 20 20 20 20 20 2a 2a page is. **
11120 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
11130 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 72 65 journal file (re
11140 63 6f 72 64 65 64 20 69 6e 20 50 61 67 65 72 2e corded in Pager.
11150 70 49 6e 4a 6f 75 72 6e 61 6c 29 20 61 6e 64 0a pInJournal) and.
11160 20 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 ** the PGH
11170 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 DR_NEED_SYNC fla
11180 67 20 69 73 20 63 6c 65 61 72 65 64 2c 20 69 66 g is cleared, if
11190 20 74 68 65 20 70 61 67 65 20 69 73 20 77 72 69 the page is wri
111a0 74 74 65 6e 20 74 6f 0a 20 20 20 20 20 20 2a 2a tten to. **
111b0 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 again within th
111c0 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 is transaction,
111d0 69 74 20 77 69 6c 6c 20 62 65 20 6d 61 72 6b 65 it will be marke
111e0 64 20 61 73 20 64 69 72 74 79 20 62 75 74 0a 20 d as dirty but.
111f0 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 ** the PGHD
11200 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 R_NEED_SYNC flag
11210 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 73 65 74 will not be set
11220 2e 20 49 74 20 63 6f 75 6c 64 20 74 68 65 6e 20 . It could then
11230 70 6f 74 65 6e 74 69 61 6c 6c 79 0a 20 20 20 20 potentially.
11240 20 20 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20 ** be written
11250 6f 75 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 out into the dat
11260 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 abase file befor
11270 65 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 e its journal fi
11280 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 67 6d le. ** segm
11290 65 6e 74 20 69 73 20 73 79 6e 63 65 64 2e 20 49 ent is synced. I
112a0 66 20 61 20 63 72 61 73 68 20 6f 63 63 75 72 73 f a crash occurs
112b0 20 64 75 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f during or follo
112c0 77 69 6e 67 20 74 68 69 73 2c 0a 20 20 20 20 20 wing this,.
112d0 20 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72 ** database cor
112e0 72 75 70 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75 ruption may ensu
112f0 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 e.. */.
11300 20 20 61 73 73 65 72 74 28 20 21 70 61 67 65 72 assert( !pager
11310 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 UseWal(pPager) )
11320 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 ;. sqlite3P
11330 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 70 cacheMakeClean(p
11340 50 67 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 Pg);. }.#ifde
11350 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 f SQLITE_CHECK_P
11360 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e 70 61 AGES. pPg->pa
11370 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 geHash = pager_p
11380 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a 23 65 agehash(pPg);.#e
11390 6e 64 69 66 0a 20 20 20 20 2f 2a 20 49 66 20 74 ndif. /* If t
113a0 68 69 73 20 77 61 73 20 70 61 67 65 20 31 2c 20 his was page 1,
113b0 74 68 65 6e 20 72 65 73 74 6f 72 65 20 74 68 65 then restore the
113c0 20 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e value of Pager.
113d0 64 62 46 69 6c 65 56 65 72 73 2e 0a 20 20 20 20 dbFileVers..
113e0 2a 2a 20 44 6f 20 74 68 69 73 20 62 65 66 6f 72 ** Do this befor
113f0 65 20 61 6e 79 20 64 65 63 6f 64 69 6e 67 2e 20 e any decoding.
11400 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3d */. if( pgno=
11410 3d 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 =1 ){. memc
11420 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 py(&pPager->dbFi
11430 6c 65 56 65 72 73 2c 20 26 28 28 75 38 2a 29 70 leVers, &((u8*)p
11440 44 61 74 61 29 5b 32 34 5d 2c 73 69 7a 65 6f 66 Data)[24],sizeof
11450 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 (pPager->dbFileV
11460 65 72 73 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 ers));. }..
11470 20 20 2f 2a 20 44 65 63 6f 64 65 20 74 68 65 20 /* Decode the
11480 70 61 67 65 20 6a 75 73 74 20 72 65 61 64 20 66 page just read f
11490 72 6f 6d 20 64 69 73 6b 20 2a 2f 0a 20 20 20 20 rom disk */.
114a0 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 CODEC1(pPager, p
114b0 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c Data, pPg->pgno,
114c0 20 33 2c 20 72 63 3d 53 51 4c 49 54 45 5f 4e 4f 3, rc=SQLITE_NO
114d0 4d 45 4d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 MEM);. sqlite
114e0 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 70 3PcacheRelease(p
114f0 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 Pg);. }. retur
11500 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 n rc;.}../*.** P
11510 61 72 61 6d 65 74 65 72 20 7a 4d 61 73 74 65 72 arameter zMaster
11520 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 is the name of
11530 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
11540 20 66 69 6c 65 2e 20 41 20 73 69 6e 67 6c 65 20 file. A single
11550 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 journal.** file
11560 74 68 61 74 20 72 65 66 65 72 72 65 64 20 74 6f that referred to
11570 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
11580 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 6a 75 73 nal file has jus
11590 74 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61 t been rolled ba
115a0 63 6b 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ck..** This rout
115b0 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 69 74 ine checks if it
115c0 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 is possible to
115d0 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 delete the maste
115e0 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 0a r journal file,.
115f0 2a 2a 20 61 6e 64 20 64 6f 65 73 20 73 6f 20 69 ** and does so i
11600 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 41 f it is..**.** A
11610 72 67 75 6d 65 6e 74 20 7a 4d 61 73 74 65 72 20 rgument zMaster
11620 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20 50 61 67 may point to Pag
11630 65 72 2e 70 54 6d 70 53 70 61 63 65 2e 20 53 6f er.pTmpSpace. So
11640 20 74 68 61 74 20 62 75 66 66 65 72 20 69 73 20 that buffer is
11650 6e 6f 74 20 0a 2a 2a 20 61 76 61 69 6c 61 62 6c not .** availabl
11660 65 20 66 6f 72 20 75 73 65 20 77 69 74 68 69 6e e for use within
11670 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a this function..
11680 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 6d 61 73 **.** When a mas
11690 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
116a0 20 69 73 20 63 72 65 61 74 65 64 2c 20 69 74 20 is created, it
116b0 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 is populated wit
116c0 68 20 74 68 65 20 6e 61 6d 65 73 20 0a 2a 2a 20 h the names .**
116d0 6f 66 20 61 6c 6c 20 6f 66 20 69 74 73 20 63 68 of all of its ch
116e0 69 6c 64 20 6a 6f 75 72 6e 61 6c 73 2c 20 6f 6e ild journals, on
116f0 65 20 61 66 74 65 72 20 61 6e 6f 74 68 65 72 2c e after another,
11700 20 66 6f 72 6d 61 74 74 65 64 20 61 73 20 75 74 formatted as ut
11710 66 2d 38 20 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 f-8 .** encoded
11720 74 65 78 74 2e 20 54 68 65 20 65 6e 64 20 6f 66 text. The end of
11730 20 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 each child jour
11740 6e 61 6c 20 66 69 6c 65 20 69 73 20 6d 61 72 6b nal file is mark
11750 65 64 20 77 69 74 68 20 61 20 0a 2a 2a 20 6e 75 ed with a .** nu
11760 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 l-terminator byt
11770 65 20 28 30 78 30 30 29 2e 20 69 2e 65 2e 20 74 e (0x00). i.e. t
11780 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e he entire conten
11790 74 73 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a ts of a master j
117a0 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 66 ournal.** file f
117b0 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e or a transaction
117c0 20 69 6e 76 6f 6c 76 69 6e 67 20 74 77 6f 20 64 involving two d
117d0 61 74 61 62 61 73 65 73 20 6d 69 67 68 74 20 62 atabases might b
117e0 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 2f 68 6f 6d e:.**.** "/hom
117f0 65 2f 62 69 6c 6c 2f 61 2e 64 62 2d 6a 6f 75 72 e/bill/a.db-jour
11800 6e 61 6c 5c 78 30 30 2f 68 6f 6d 65 2f 62 69 6c nal\x00/home/bil
11810 6c 2f 62 2e 64 62 2d 6a 6f 75 72 6e 61 6c 5c 78 l/b.db-journal\x
11820 30 30 22 0a 2a 2a 0a 2a 2a 20 41 20 6d 61 73 74 00".**.** A mast
11830 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
11840 6d 61 79 20 6f 6e 6c 79 20 62 65 20 64 65 6c 65 may only be dele
11850 74 65 64 20 6f 6e 63 65 20 61 6c 6c 20 6f 66 20 ted once all of
11860 69 74 73 20 63 68 69 6c 64 20 0a 2a 2a 20 6a 6f its child .** jo
11870 75 72 6e 61 6c 73 20 68 61 76 65 20 62 65 65 6e urnals have been
11880 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a rolled back..**
11890 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
118a0 6e 20 72 65 61 64 73 20 74 68 65 20 63 6f 6e 74 n reads the cont
118b0 65 6e 74 73 20 6f 66 20 74 68 65 20 6d 61 73 74 ents of the mast
118c0 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er-journal file
118d0 69 6e 74 6f 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 into .** memory
118e0 61 6e 64 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 and loops throug
118f0 68 20 65 61 63 68 20 6f 66 20 74 68 65 20 63 68 h each of the ch
11900 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 ild journal name
11910 73 2e 20 46 6f 72 0a 2a 2a 20 65 61 63 68 20 63 s. For.** each c
11920 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 hild journal, it
11930 20 63 68 65 63 6b 73 20 69 66 3a 0a 2a 2a 0a 2a checks if:.**.*
11940 2a 20 20 20 2a 20 69 66 20 74 68 65 20 63 68 69 * * if the chi
11950 6c 64 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73 74 ld journal exist
11960 73 2c 20 61 6e 64 20 69 66 20 73 6f 0a 2a 2a 20 s, and if so.**
11970 20 20 2a 20 69 66 20 74 68 65 20 63 68 69 6c 64 * if the child
11980 20 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74 61 69 6e journal contain
11990 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f s a reference to
119a0 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
119b0 0a 2a 2a 20 20 20 20 20 66 69 6c 65 20 7a 4d 61 .** file zMa
119c0 73 74 65 72 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 ster.**.** If a
119d0 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 61 child journal ca
119e0 6e 20 62 65 20 66 6f 75 6e 64 20 74 68 61 74 20 n be found that
119f0 6d 61 74 63 68 65 73 20 62 6f 74 68 20 6f 66 20 matches both of
11a00 74 68 65 20 63 72 69 74 65 72 69 61 0a 2a 2a 20 the criteria.**
11a10 61 62 6f 76 65 2c 20 74 68 69 73 20 66 75 6e 63 above, this func
11a20 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 77 69 74 tion returns wit
11a30 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 hout doing anyth
11a40 69 6e 67 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ing. Otherwise,
11a50 69 66 0a 2a 2a 20 6e 6f 20 73 75 63 68 20 63 68 if.** no such ch
11a60 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 61 6e 20 ild journal can
11a70 62 65 20 66 6f 75 6e 64 2c 20 66 69 6c 65 20 7a be found, file z
11a80 4d 61 73 74 65 72 20 69 73 20 64 65 6c 65 74 65 Master is delete
11a90 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 66 69 d from.** the fi
11aa0 6c 65 2d 73 79 73 74 65 6d 20 75 73 69 6e 67 20 le-system using
11ab0 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 sqlite3OsDelete(
11ac0 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 )..**.** If an I
11ad0 4f 20 65 72 72 6f 72 20 77 69 74 68 69 6e 20 74 O error within t
11ae0 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 6e his function, an
11af0 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
11b00 65 74 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a 2a eturned. This.**
11b10 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 function alloca
11b20 74 65 73 20 6d 65 6d 6f 72 79 20 62 79 20 63 61 tes memory by ca
11b30 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 6c lling sqlite3Mal
11b40 6c 6f 63 28 29 2e 20 49 66 20 61 6e 20 61 6c 6c loc(). If an all
11b50 6f 63 61 74 69 6f 6e 0a 2a 2a 20 66 61 69 6c 73 ocation.** fails
11b60 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 , SQLITE_NOMEM i
11b70 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 s returned. Othe
11b80 72 77 69 73 65 2c 20 69 66 20 6e 6f 20 49 4f 20 rwise, if no IO
11b90 6f 72 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 or malloc errors
11ba0 20 0a 2a 2a 20 6f 63 63 75 72 2c 20 53 51 4c 49 .** occur, SQLI
11bb0 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
11bc0 64 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 54 d..**.** TODO: T
11bd0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c his function all
11be0 6f 63 61 74 65 73 20 61 20 73 69 6e 67 6c 65 20 ocates a single
11bf0 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 block of memory
11c00 74 6f 20 6c 6f 61 64 0a 2a 2a 20 74 68 65 20 65 to load.** the e
11c10 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f ntire contents o
11c20 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 f the master jou
11c30 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 rnal file. This
11c40 63 6f 75 6c 64 20 62 65 0a 2a 2a 20 61 20 63 6f could be.** a co
11c50 75 70 6c 65 20 6f 66 20 6b 69 6c 6f 62 79 74 65 uple of kilobyte
11c60 73 20 6f 72 20 73 6f 20 2d 20 70 6f 74 65 6e 74 s or so - potent
11c70 69 61 6c 6c 79 20 6c 61 72 67 65 72 20 74 68 61 ially larger tha
11c80 6e 20 74 68 65 20 70 61 67 65 20 0a 2a 2a 20 73 n the page .** s
11c90 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ize..*/.static i
11ca0 6e 74 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 nt pager_delmast
11cb0 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 er(Pager *pPager
11cc0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d , const char *zM
11cd0 61 73 74 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 aster){. sqlite
11ce0 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50 3_vfs *pVfs = pP
11cf0 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 6e ager->pVfs;. in
11d00 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
11d10 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
11d20 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 n code */. sqli
11d30 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 te3_file *pMaste
11d40 72 3b 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 r; /* Malloc'
11d50 64 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c d master-journal
11d60 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
11d70 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 */. sqlite3_fi
11d80 6c 65 20 2a 70 4a 6f 75 72 6e 61 6c 3b 20 20 20 le *pJournal;
11d90 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 63 68 69 6c /* Malloc'd chil
11da0 64 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 d-journal file d
11db0 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 63 escriptor */. c
11dc0 68 61 72 20 2a 7a 4d 61 73 74 65 72 4a 6f 75 72 har *zMasterJour
11dd0 6e 61 6c 20 3d 20 30 3b 20 2f 2a 20 43 6f 6e 74 nal = 0; /* Cont
11de0 65 6e 74 73 20 6f 66 20 6d 61 73 74 65 72 20 6a ents of master j
11df0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 ournal file */.
11e00 20 69 36 34 20 6e 4d 61 73 74 65 72 4a 6f 75 72 i64 nMasterJour
11e10 6e 61 6c 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 nal; /* Si
11e20 7a 65 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 ze of master jou
11e30 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 rnal file */..
11e40 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 /* Allocate spac
11e50 65 20 66 6f 72 20 62 6f 74 68 20 74 68 65 20 70 e for both the p
11e60 4a 6f 75 72 6e 61 6c 20 61 6e 64 20 70 4d 61 73 Journal and pMas
11e70 74 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 70 ter file descrip
11e80 74 6f 72 73 2e 0a 20 20 2a 2a 20 49 66 20 73 75 tors.. ** If su
11e90 63 63 65 73 73 66 75 6c 2c 20 6f 70 65 6e 20 74 ccessful, open t
11ea0 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
11eb0 6c 20 66 69 6c 65 20 66 6f 72 20 72 65 61 64 69 l file for readi
11ec0 6e 67 2e 0a 20 20 2a 2f 0a 20 20 70 4d 61 73 74 ng.. */. pMast
11ed0 65 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 er = (sqlite3_fi
11ee0 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c le *)sqlite3Mall
11ef0 6f 63 5a 65 72 6f 28 70 56 66 73 2d 3e 73 7a 4f ocZero(pVfs->szO
11f00 73 46 69 6c 65 20 2a 20 32 29 3b 0a 20 20 70 4a sFile * 2);. pJ
11f10 6f 75 72 6e 61 6c 20 3d 20 28 73 71 6c 69 74 65 ournal = (sqlite
11f20 33 5f 66 69 6c 65 20 2a 29 28 28 28 75 38 20 2a 3_file *)(((u8 *
11f30 29 70 4d 61 73 74 65 72 29 20 2b 20 70 56 66 73 )pMaster) + pVfs
11f40 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20 20 69 ->szOsFile);. i
11f50 66 28 20 21 70 4d 61 73 74 65 72 20 29 7b 0a 20 f( !pMaster ){.
11f60 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
11f70 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 OMEM;. }else{.
11f80 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c 61 const int fla
11f90 67 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45 gs = (SQLITE_OPE
11fa0 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 N_READONLY|SQLIT
11fb0 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f E_OPEN_MASTER_JO
11fc0 55 52 4e 41 4c 29 3b 0a 20 20 20 20 72 63 20 3d URNAL);. rc =
11fd0 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 sqlite3OsOpen(p
11fe0 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 70 4d Vfs, zMaster, pM
11ff0 61 73 74 65 72 2c 20 66 6c 61 67 73 2c 20 30 29 aster, flags, 0)
12000 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d ;. }. if( rc!=
12010 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
12020 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a delmaster_out;.
12030 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f . rc = sqlite3O
12040 73 46 69 6c 65 53 69 7a 65 28 70 4d 61 73 74 65 sFileSize(pMaste
12050 72 2c 20 26 6e 4d 61 73 74 65 72 4a 6f 75 72 6e r, &nMasterJourn
12060 61 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 al);. if( rc!=S
12070 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 QLITE_OK ) goto
12080 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 0a delmaster_out;..
12090 20 20 69 66 28 20 6e 4d 61 73 74 65 72 4a 6f 75 if( nMasterJou
120a0 72 6e 61 6c 3e 30 20 29 7b 0a 20 20 20 20 63 68 rnal>0 ){. ch
120b0 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 0a 20 20 ar *zJournal;.
120c0 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 50 char *zMasterP
120d0 74 72 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 tr = 0;. int
120e0 6e 4d 61 73 74 65 72 50 74 72 20 3d 20 70 56 66 nMasterPtr = pVf
120f0 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 3b s->mxPathname+1;
12100 0a 0a 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 .. /* Load th
12110 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72 20 e entire master
12120 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 journal file int
12130 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 o space obtained
12140 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 73 71 6c from. ** sql
12150 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e ite3_malloc() an
12160 64 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 d pointed to by
12170 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2e 20 zMasterJournal.
12180 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 4d 61 73 . */. zMas
12190 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c terJournal = sql
121a0 69 74 65 33 4d 61 6c 6c 6f 63 28 28 69 6e 74 29 ite3Malloc((int)
121b0 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 2b nMasterJournal +
121c0 20 6e 4d 61 73 74 65 72 50 74 72 20 2b 20 31 29 nMasterPtr + 1)
121d0 3b 0a 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 ;. if( !zMast
121e0 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 erJournal ){.
121f0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
12200 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f OMEM;. goto
12210 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a delmaster_out;.
12220 20 20 20 20 7d 0a 20 20 20 20 7a 4d 61 73 74 65 }. zMaste
12230 72 50 74 72 20 3d 20 26 7a 4d 61 73 74 65 72 4a rPtr = &zMasterJ
12240 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a 6f ournal[nMasterJo
12250 75 72 6e 61 6c 2b 31 5d 3b 0a 20 20 20 20 72 63 urnal+1];. rc
12260 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 = sqlite3OsRead
12270 28 70 4d 61 73 74 65 72 2c 20 7a 4d 61 73 74 65 (pMaster, zMaste
12280 72 4a 6f 75 72 6e 61 6c 2c 20 28 69 6e 74 29 6e rJournal, (int)n
12290 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 30 MasterJournal, 0
122a0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
122b0 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 QLITE_OK ) goto
122c0 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 delmaster_out;.
122d0 20 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 zMasterJourna
122e0 6c 5b 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c l[nMasterJournal
122f0 5d 20 3d 20 30 3b 0a 0a 20 20 20 20 7a 4a 6f 75 ] = 0;.. zJou
12300 72 6e 61 6c 20 3d 20 7a 4d 61 73 74 65 72 4a 6f rnal = zMasterJo
12310 75 72 6e 61 6c 3b 0a 20 20 20 20 77 68 69 6c 65 urnal;. while
12320 28 20 28 7a 4a 6f 75 72 6e 61 6c 2d 7a 4d 61 73 ( (zJournal-zMas
12330 74 65 72 4a 6f 75 72 6e 61 6c 29 3c 6e 4d 61 73 terJournal)<nMas
12340 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 terJournal ){.
12350 20 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b 0a int exists;.
12360 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
12370 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c e3OsAccess(pVfs,
12380 20 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54 zJournal, SQLIT
12390 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c E_ACCESS_EXISTS,
123a0 20 26 65 78 69 73 74 73 29 3b 0a 20 20 20 20 20 &exists);.
123b0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
123c0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
123d0 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 to delmaster_out
123e0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
123f0 69 66 28 20 65 78 69 73 74 73 20 29 7b 0a 20 20 if( exists ){.
12400 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 /* One of
12410 74 68 65 20 6a 6f 75 72 6e 61 6c 73 20 70 6f 69 the journals poi
12420 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 6d nted to by the m
12430 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 65 78 aster journal ex
12440 69 73 74 73 2e 0a 20 20 20 20 20 20 20 20 2a 2a ists.. **
12450 20 4f 70 65 6e 20 69 74 20 61 6e 64 20 63 68 65 Open it and che
12460 63 6b 20 69 66 20 69 74 20 70 6f 69 6e 74 73 20 ck if it points
12470 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f at the master jo
12480 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 20 20 urnal. If.
12490 20 20 2a 2a 20 73 6f 2c 20 72 65 74 75 72 6e 20 ** so, return
124a0 77 69 74 68 6f 75 74 20 64 65 6c 65 74 69 6e 67 without deleting
124b0 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
124c0 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 nal file..
124d0 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 */. int
124e0 20 63 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 c;. int
124f0 66 6c 61 67 73 20 3d 20 28 53 51 4c 49 54 45 5f flags = (SQLITE_
12500 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 OPEN_READONLY|SQ
12510 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a LITE_OPEN_MAIN_J
12520 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 20 20 20 OURNAL);.
12530 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f rc = sqlite3OsO
12540 70 65 6e 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e pen(pVfs, zJourn
12550 61 6c 2c 20 70 4a 6f 75 72 6e 61 6c 2c 20 66 6c al, pJournal, fl
12560 61 67 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 ags, 0);.
12570 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
12580 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
12590 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f goto delmaster_o
125a0 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 ut;. }..
125b0 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 rc = read
125c0 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 4a MasterJournal(pJ
125d0 6f 75 72 6e 61 6c 2c 20 7a 4d 61 73 74 65 72 50 ournal, zMasterP
125e0 74 72 2c 20 6e 4d 61 73 74 65 72 50 74 72 29 3b tr, nMasterPtr);
125f0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
12600 4f 73 43 6c 6f 73 65 28 70 4a 6f 75 72 6e 61 6c OsClose(pJournal
12610 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
12620 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
12630 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 goto d
12640 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 elmaster_out;.
12650 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 }..
12660 20 63 20 3d 20 7a 4d 61 73 74 65 72 50 74 72 5b c = zMasterPtr[
12670 30 5d 21 3d 30 20 26 26 20 73 74 72 63 6d 70 28 0]!=0 && strcmp(
12680 7a 4d 61 73 74 65 72 50 74 72 2c 20 7a 4d 61 73 zMasterPtr, zMas
12690 74 65 72 29 3d 3d 30 3b 0a 20 20 20 20 20 20 20 ter)==0;.
126a0 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 20 20 if( c ){.
126b0 20 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20 61 /* We have a
126c0 20 6d 61 74 63 68 2e 20 44 6f 20 6e 6f 74 20 64 match. Do not d
126d0 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 elete the master
126e0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 2a journal file. *
126f0 2f 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f /. goto
12700 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a delmaster_out;.
12710 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
12720 7d 0a 20 20 20 20 20 20 7a 4a 6f 75 72 6e 61 6c }. zJournal
12730 20 2b 3d 20 28 73 71 6c 69 74 65 33 53 74 72 6c += (sqlite3Strl
12740 65 6e 33 30 28 7a 4a 6f 75 72 6e 61 6c 29 2b 31 en30(zJournal)+1
12750 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a );. }. }. .
12760 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
12770 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 Delete(pVfs, zMa
12780 73 74 65 72 2c 20 30 29 3b 0a 0a 64 65 6c 6d 61 ster, 0);..delma
12790 73 74 65 72 5f 6f 75 74 3a 0a 20 20 69 66 28 20 ster_out:. if(
127a0 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 29 zMasterJournal )
127b0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 {. sqlite3_fr
127c0 65 65 28 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 ee(zMasterJourna
127d0 6c 29 3b 0a 20 20 7d 20 20 0a 20 20 69 66 28 20 l);. } . if(
127e0 70 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 73 pMaster ){. s
127f0 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4d qlite3OsClose(pM
12800 61 73 74 65 72 29 3b 0a 20 20 20 20 61 73 73 65 aster);. asse
12810 72 74 28 20 21 69 73 4f 70 65 6e 28 70 4a 6f 75 rt( !isOpen(pJou
12820 72 6e 61 6c 29 20 29 3b 0a 20 20 7d 0a 20 20 73 rnal) );. }. s
12830 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4d 61 73 qlite3_free(pMas
12840 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 ter);. return r
12850 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 c;.}.../*.** Thi
12860 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 s function is us
12870 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 ed to change the
12880 20 61 63 74 75 61 6c 20 73 69 7a 65 20 6f 66 20 actual size of
12890 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a the database .**
128a0 20 66 69 6c 65 20 69 6e 20 74 68 65 20 66 69 6c file in the fil
128b0 65 2d 73 79 73 74 65 6d 2e 20 54 68 69 73 20 6f e-system. This o
128c0 6e 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e nly happens when
128d0 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 72 committing a tr
128e0 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 6f 72 ansaction,.** or
128f0 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 rolling back a
12900 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 69 6e 63 transaction (inc
12910 6c 75 64 69 6e 67 20 72 6f 6c 6c 69 6e 67 20 62 luding rolling b
12920 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 ack a hot-journa
12930 6c 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 l)..**.** If the
12940 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 main database f
12950 69 6c 65 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c ile is not open,
12960 20 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65 or an exclusive
12970 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 0a 2a 2a 20 lock is not.**
12980 68 65 6c 64 2c 20 74 68 69 73 20 66 75 6e 63 74 held, this funct
12990 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 ion is a no-op.
129a0 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 73 Otherwise, the s
129b0 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ize of the file
129c0 69 73 0a 2a 2a 20 63 68 61 6e 67 65 64 20 74 6f is.** changed to
129d0 20 6e 50 61 67 65 20 70 61 67 65 73 20 28 6e 50 nPage pages (nP
129e0 61 67 65 2a 70 50 61 67 65 72 2d 3e 70 61 67 65 age*pPager->page
129f0 53 69 7a 65 20 62 79 74 65 73 29 2e 20 49 66 20 Size bytes). If
12a00 74 68 65 20 66 69 6c 65 0a 2a 2a 20 6f 6e 20 64 the file.** on d
12a10 69 73 6b 20 69 73 20 63 75 72 72 65 6e 74 6c 79 isk is currently
12a20 20 6c 61 72 67 65 72 20 74 68 61 6e 20 6e 50 61 larger than nPa
12a30 67 65 20 70 61 67 65 73 2c 20 74 68 65 6e 20 75 ge pages, then u
12a40 73 65 20 74 68 65 20 56 46 53 0a 2a 2a 20 78 54 se the VFS.** xT
12a50 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68 6f 64 runcate() method
12a60 20 74 6f 20 74 72 75 6e 63 61 74 65 20 69 74 2e to truncate it.
12a70 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 74 20 6d 69 .**.** Or, it mi
12a80 67 68 74 20 6d 69 67 68 74 20 62 65 20 74 68 65 ght might be the
12a90 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20 66 case that the f
12aa0 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 73 ile on disk is s
12ab0 6d 61 6c 6c 65 72 20 74 68 61 6e 20 0a 2a 2a 20 maller than .**
12ac0 6e 50 61 67 65 20 70 61 67 65 73 2e 20 53 6f 6d nPage pages. Som
12ad0 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 e operating syst
12ae0 65 6d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f em implementatio
12af0 6e 73 20 63 61 6e 20 67 65 74 20 63 6f 6e 66 75 ns can get confu
12b00 73 65 64 20 69 66 20 0a 2a 2a 20 79 6f 75 20 74 sed if .** you t
12b10 72 79 20 74 6f 20 74 72 75 6e 63 61 74 65 20 61 ry to truncate a
12b20 20 66 69 6c 65 20 74 6f 20 73 6f 6d 65 20 73 69 file to some si
12b30 7a 65 20 74 68 61 74 20 69 73 20 6c 61 72 67 65 ze that is large
12b40 72 20 74 68 61 6e 20 69 74 20 0a 2a 2a 20 63 75 r than it .** cu
12b50 72 72 65 6e 74 6c 79 20 69 73 2c 20 73 6f 20 64 rrently is, so d
12b60 65 74 65 63 74 20 74 68 69 73 20 63 61 73 65 20 etect this case
12b70 61 6e 64 20 77 72 69 74 65 20 61 20 73 69 6e 67 and write a sing
12b80 6c 65 20 7a 65 72 6f 20 62 79 74 65 20 74 6f 20 le zero byte to
12b90 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 .** the end of t
12ba0 68 65 20 6e 65 77 20 66 69 6c 65 20 69 6e 73 74 he new file inst
12bb0 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 ead..**.** If su
12bc0 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e ccessful, return
12bd0 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61 SQLITE_OK. If a
12be0 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 n IO error occur
12bf0 73 20 77 68 69 6c 65 20 6d 6f 64 69 66 79 69 6e s while modifyin
12c00 67 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 g.** the databas
12c10 65 20 66 69 6c 65 2c 20 72 65 74 75 72 6e 20 74 e file, return t
12c20 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f he error code to
12c30 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a the caller..*/.
12c40 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
12c50 5f 74 72 75 6e 63 61 74 65 28 50 61 67 65 72 20 _truncate(Pager
12c60 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 6e 50 *pPager, Pgno nP
12c70 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d age){. int rc =
12c80 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
12c90 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
12ca0 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 =PAGER_EXCLUSIVE
12cb0 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 && isOpen(pPage
12cc0 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 69 36 r->fd) ){. i6
12cd0 34 20 63 75 72 72 65 6e 74 53 69 7a 65 2c 20 6e 4 currentSize, n
12ce0 65 77 53 69 7a 65 3b 0a 20 20 20 20 2f 2a 20 54 ewSize;. /* T
12cf0 4f 44 4f 3a 20 49 73 20 69 74 20 73 61 66 65 20 ODO: Is it safe
12d00 74 6f 20 75 73 65 20 50 61 67 65 72 2e 64 62 46 to use Pager.dbF
12d10 69 6c 65 53 69 7a 65 20 68 65 72 65 3f 20 2a 2f ileSize here? */
12d20 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
12d30 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 3OsFileSize(pPag
12d40 65 72 2d 3e 66 64 2c 20 26 63 75 72 72 65 6e 74 er->fd, ¤t
12d50 53 69 7a 65 29 3b 0a 20 20 20 20 6e 65 77 53 69 Size);. newSi
12d60 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 ze = pPager->pag
12d70 65 53 69 7a 65 2a 28 69 36 34 29 6e 50 61 67 65 eSize*(i64)nPage
12d80 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
12d90 4c 49 54 45 5f 4f 4b 20 26 26 20 63 75 72 72 65 LITE_OK && curre
12da0 6e 74 53 69 7a 65 21 3d 6e 65 77 53 69 7a 65 20 ntSize!=newSize
12db0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63 75 72 ){. if( cur
12dc0 72 65 6e 74 53 69 7a 65 3e 6e 65 77 53 69 7a 65 rentSize>newSize
12dd0 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
12de0 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 sqlite3OsTrunca
12df0 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 6e te(pPager->fd, n
12e00 65 77 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d ewSize);. }
12e10 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 else{. rc
12e20 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
12e30 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 22 22 e(pPager->fd, ""
12e40 2c 20 31 2c 20 6e 65 77 53 69 7a 65 2d 31 29 3b , 1, newSize-1);
12e50 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
12e60 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
12e70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 67 ){. pPag
12e80 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d er->dbFileSize =
12e90 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a nPage;. }.
12ea0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
12eb0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
12ec0 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 Set the value of
12ed0 20 74 68 65 20 50 61 67 65 72 2e 73 65 63 74 6f the Pager.secto
12ee0 72 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 66 rSize variable f
12ef0 6f 72 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 or the given.**
12f00 70 61 67 65 72 20 62 61 73 65 64 20 6f 6e 20 74 pager based on t
12f10 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
12f20 64 20 62 79 20 74 68 65 20 78 53 65 63 74 6f 72 d by the xSector
12f30 53 69 7a 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f Size method.** o
12f40 66 20 74 68 65 20 6f 70 65 6e 20 64 61 74 61 62 f the open datab
12f50 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 73 65 ase file. The se
12f60 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 ctor size will b
12f70 65 20 75 73 65 64 20 75 73 65 64 20 0a 2a 2a 20 e used used .**
12f80 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 to determine the
12f90 20 73 69 7a 65 20 61 6e 64 20 61 6c 69 67 6e 6d size and alignm
12fa0 65 6e 74 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 ent of journal h
12fb0 65 61 64 65 72 20 61 6e 64 20 0a 2a 2a 20 6d 61 eader and .** ma
12fc0 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 ster journal poi
12fd0 6e 74 65 72 73 20 77 69 74 68 69 6e 20 63 72 65 nters within cre
12fe0 61 74 65 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ated journal fil
12ff0 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 65 es..**.** For te
13000 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 74 68 mporary files th
13010 65 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 e effective sect
13020 6f 72 20 73 69 7a 65 20 69 73 20 61 6c 77 61 79 or size is alway
13030 73 20 35 31 32 20 62 79 74 65 73 2e 0a 2a 2a 0a s 512 bytes..**.
13040 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 6f ** Otherwise, fo
13050 72 20 6e 6f 6e 2d 74 65 6d 70 6f 72 61 72 79 20 r non-temporary
13060 66 69 6c 65 73 2c 20 74 68 65 20 65 66 66 65 63 files, the effec
13070 74 69 76 65 20 73 65 63 74 6f 72 20 73 69 7a 65 tive sector size
13080 20 69 73 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 is.** the value
13090 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 returned by the
130a0 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29 20 6d xSectorSize() m
130b0 65 74 68 6f 64 20 72 6f 75 6e 64 65 64 20 75 70 ethod rounded up
130c0 20 74 6f 20 33 32 20 69 66 0a 2a 2a 20 69 74 20 to 32 if.** it
130d0 69 73 20 6c 65 73 73 20 74 68 61 6e 20 33 32 2c is less than 32,
130e0 20 6f 72 20 72 6f 75 6e 64 65 64 20 64 6f 77 6e or rounded down
130f0 20 74 6f 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 to MAX_SECTOR_S
13100 49 5a 45 20 69 66 20 69 74 0a 2a 2a 20 69 73 20 IZE if it.** is
13110 67 72 65 61 74 65 72 20 74 68 61 6e 20 4d 41 58 greater than MAX
13120 5f 53 45 43 54 4f 52 5f 53 49 5a 45 2e 0a 2a 2f _SECTOR_SIZE..*/
13130 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 .static void set
13140 53 65 63 74 6f 72 53 69 7a 65 28 50 61 67 65 72 SectorSize(Pager
13150 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 61 73 73 *pPager){. ass
13160 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 ert( isOpen(pPag
13170 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 er->fd) || pPage
13180 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 0a r->tempFile );..
13190 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 74 if( !pPager->t
131a0 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 2f empFile ){. /
131b0 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 64 6f * Sector size do
131c0 65 73 6e 27 74 20 6d 61 74 74 65 72 20 66 6f 72 esn't matter for
131d0 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 temporary files
131e0 2e 20 41 6c 73 6f 2c 20 74 68 65 20 66 69 6c 65 . Also, the file
131f0 0a 20 20 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 . ** may not
13200 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 have been opened
13210 20 79 65 74 2c 20 69 6e 20 77 68 69 63 68 20 63 yet, in which c
13220 61 73 65 20 74 68 65 20 4f 73 53 65 63 74 6f 72 ase the OsSector
13230 53 69 7a 65 28 29 0a 20 20 20 20 2a 2a 20 63 61 Size(). ** ca
13240 6c 6c 20 77 69 6c 6c 20 73 65 67 66 61 75 6c 74 ll will segfault
13250 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 .. */. pPa
13260 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 ger->sectorSize
13270 3d 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f = sqlite3OsSecto
13280 72 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 rSize(pPager->fd
13290 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 );. }. if( pPa
132a0 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3c ger->sectorSize<
132b0 33 32 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 32 ){. pPager
132c0 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 35 ->sectorSize = 5
132d0 31 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 12;. }. if( pP
132e0 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 ager->sectorSize
132f0 3e 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 >MAX_SECTOR_SIZE
13300 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
13310 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3e MAX_SECTOR_SIZE>
13320 3d 35 31 32 20 29 3b 0a 20 20 20 20 70 50 61 67 =512 );. pPag
13330 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d er->sectorSize =
13340 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 MAX_SECTOR_SIZE
13350 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 ;. }.}../*.** P
13360 6c 61 79 62 61 63 6b 20 74 68 65 20 6a 6f 75 72 layback the jour
13370 6e 61 6c 20 61 6e 64 20 74 68 75 73 20 72 65 73 nal and thus res
13380 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 tore the databas
13390 65 20 66 69 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 e file to.** the
133a0 20 73 74 61 74 65 20 69 74 20 77 61 73 20 69 6e state it was in
133b0 20 62 65 66 6f 72 65 20 77 65 20 73 74 61 72 74 before we start
133c0 65 64 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65 ed making change
133d0 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a s. .**.** The j
133e0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d ournal file form
133f0 61 74 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 at is as follows
13400 3a 20 0a 2a 2a 0a 2a 2a 20 20 28 31 29 20 20 38 : .**.** (1) 8
13410 20 62 79 74 65 20 70 72 65 66 69 78 2e 20 20 41 byte prefix. A
13420 20 63 6f 70 79 20 6f 66 20 61 4a 6f 75 72 6e 61 copy of aJourna
13430 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a 20 20 28 32 lMagic[]..** (2
13440 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e ) 4 byte big-en
13450 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 dian integer whi
13460 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ch is the number
13470 20 6f 66 20 76 61 6c 69 64 20 70 61 67 65 20 72 of valid page r
13480 65 63 6f 72 64 73 0a 2a 2a 20 20 20 20 20 20 20 ecords.**
13490 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 in the journal.
134a0 20 49 66 20 74 68 69 73 20 76 61 6c 75 65 20 69 If this value i
134b0 73 20 30 78 66 66 66 66 66 66 66 66 2c 20 74 68 s 0xffffffff, th
134c0 65 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a en compute the.*
134d0 2a 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f * number o
134e0 66 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 f page records f
134f0 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 rom the journal
13500 73 69 7a 65 2e 0a 2a 2a 20 20 28 33 29 20 20 34 size..** (3) 4
13510 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e byte big-endian
13520 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 integer which i
13530 73 20 74 68 65 20 69 6e 69 74 69 61 6c 20 76 61 s the initial va
13540 6c 75 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 lue for the .**
13550 20 20 20 20 20 20 73 61 6e 69 74 79 20 63 68 65 sanity che
13560 63 6b 73 75 6d 2e 0a 2a 2a 20 20 28 34 29 20 20 cksum..** (4)
13570 34 20 62 79 74 65 20 69 6e 74 65 67 65 72 20 77 4 byte integer w
13580 68 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 hich is the numb
13590 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 74 er of pages to t
135a0 72 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 20 runcate the.**
135b0 20 20 20 20 20 64 61 74 61 62 61 73 65 20 74 6f database to
135c0 20 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 during a rollba
135d0 63 6b 2e 0a 2a 2a 20 20 28 35 29 20 20 34 20 62 ck..** (5) 4 b
135e0 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 yte big-endian i
135f0 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 nteger which is
13600 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 2e the sector size.
13610 20 20 54 68 65 20 68 65 61 64 65 72 0a 2a 2a 20 The header.**
13620 20 20 20 20 20 20 69 73 20 74 68 69 73 20 6d 61 is this ma
13630 6e 79 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 ny bytes in size
13640 2e 0a 2a 2a 20 20 28 36 29 20 20 34 20 62 79 74 ..** (6) 4 byt
13650 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 e big-endian int
13660 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 eger which is th
13670 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 e page size..**
13680 20 28 37 29 20 20 7a 65 72 6f 20 70 61 64 64 69 (7) zero paddi
13690 6e 67 20 6f 75 74 20 74 6f 20 74 68 65 20 6e 65 ng out to the ne
136a0 78 74 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 0a xt sector size..
136b0 2a 2a 20 20 28 38 29 20 20 5a 65 72 6f 20 6f 72 ** (8) Zero or
136c0 20 6d 6f 72 65 20 70 61 67 65 73 20 69 6e 73 74 more pages inst
136d0 61 6e 63 65 73 2c 20 65 61 63 68 20 61 73 20 66 ances, each as f
136e0 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20 ollows:.**
136f0 20 20 2b 20 20 34 20 62 79 74 65 20 70 61 67 65 + 4 byte page
13700 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 number..**
13710 20 20 20 2b 20 20 70 50 61 67 65 72 2d 3e 70 61 + pPager->pa
13720 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 geSize bytes of
13730 64 61 74 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20 data..**
13740 2b 20 20 34 20 62 79 74 65 20 63 68 65 63 6b 73 + 4 byte checks
13750 75 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 um.**.** When we
13760 20 73 70 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f speak of the jo
13770 75 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 77 65 urnal header, we
13780 20 6d 65 61 6e 20 74 68 65 20 66 69 72 73 74 20 mean the first
13790 37 20 69 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 7 items above..*
137a0 2a 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20 * Each entry in
137b0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 the journal is a
137c0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
137d0 65 20 38 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a e 8th item..**.*
137e0 2a 20 43 61 6c 6c 20 74 68 65 20 76 61 6c 75 65 * Call the value
137f0 20 66 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 from the second
13800 20 62 75 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20 bullet "nRec".
13810 20 6e 52 65 63 20 69 73 20 74 68 65 20 6e 75 6d nRec is the num
13820 62 65 72 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 ber of.** valid
13830 70 61 67 65 20 65 6e 74 72 69 65 73 20 69 6e 20 page entries in
13840 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e the journal. In
13850 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 79 6f 75 most cases, you
13860 20 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 can compute the
13870 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 .** value of nRe
13880 63 20 66 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 c from the size
13890 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
138a0 69 6c 65 2e 20 20 42 75 74 20 69 66 20 61 20 70 ile. But if a p
138b0 6f 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20 ower.** failure
138c0 6f 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 74 occurred while t
138d0 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 he journal was b
138e0 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 69 74 eing written, it
138f0 20 63 6f 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a could be the.**
13900 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20 73 case that the s
13910 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e ize of the journ
13920 61 6c 20 66 69 6c 65 20 68 61 64 20 61 6c 72 65 al file had alre
13930 61 64 79 20 62 65 65 6e 20 69 6e 63 72 65 61 73 ady been increas
13940 65 64 20 62 75 74 0a 2a 2a 20 74 68 65 20 65 78 ed but.** the ex
13950 74 72 61 20 65 6e 74 72 69 65 73 20 68 61 64 20 tra entries had
13960 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 69 74 20 not yet made it
13970 73 61 66 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20 safely to disk.
13980 20 49 6e 20 73 75 63 68 20 61 20 63 61 73 65 2c In such a case,
13990 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 .** the value of
139a0 20 6e 52 65 63 20 63 6f 6d 70 75 74 65 64 20 66 nRec computed f
139b0 72 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 69 7a rom the file siz
139c0 65 20 77 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c e would be too l
139d0 61 72 67 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 arge. For.** th
139e0 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c at reason, we al
139f0 77 61 79 73 20 75 73 65 20 74 68 65 20 6e 52 65 ways use the nRe
13a00 63 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 68 c value in the h
13a10 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 eader..**.** If
13a20 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 the nRec value i
13a30 73 20 30 78 66 66 66 66 66 66 66 66 20 69 74 20 s 0xffffffff it
13a40 6d 65 61 6e 73 20 74 68 61 74 20 6e 52 65 63 20 means that nRec
13a50 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 should be comput
13a60 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 ed.** from the f
13a70 69 6c 65 20 73 69 7a 65 2e 20 20 54 68 69 73 20 ile size. This
13a80 76 61 6c 75 65 20 69 73 20 75 73 65 64 20 77 68 value is used wh
13a90 65 6e 20 74 68 65 20 75 73 65 72 20 73 65 6c 65 en the user sele
13aa0 63 74 73 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 cts the.** no-sy
13ab0 6e 63 20 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68 nc option for th
13ac0 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f e journal. A po
13ad0 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c wer failure coul
13ae0 64 20 6c 65 61 64 20 74 6f 20 63 6f 72 72 75 70 d lead to corrup
13af0 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20 tion.** in this
13b00 63 61 73 65 2e 20 20 42 75 74 20 66 6f 72 20 74 case. But for t
13b10 68 69 6e 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f hings like tempo
13b20 72 61 72 79 20 74 61 62 6c 65 20 28 77 68 69 63 rary table (whic
13b30 68 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c h will be.** del
13b40 65 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 6f eted when the po
13b50 77 65 72 20 69 73 20 72 65 73 74 6f 72 65 64 29 wer is restored)
13b60 20 77 65 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 we don't care.
13b70 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 .**.** If the f
13b80 69 6c 65 20 6f 70 65 6e 65 64 20 61 73 20 74 68 ile opened as th
13b90 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
13ba0 73 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 s not a well-for
13bb0 6d 65 64 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 med.** journal f
13bc0 69 6c 65 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 ile then all pag
13bd0 65 73 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 es up to the fir
13be0 73 74 20 63 6f 72 72 75 70 74 65 64 20 70 61 67 st corrupted pag
13bf0 65 20 61 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 e are rolled.**
13c00 62 61 63 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65 back (or no page
13c10 73 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c s if the journal
13c20 20 68 65 61 64 65 72 20 69 73 20 63 6f 72 72 75 header is corru
13c30 70 74 65 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e pted). The journ
13c40 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68 al file.** is th
13c50 65 6e 20 64 65 6c 65 74 65 64 20 61 6e 64 20 53 en deleted and S
13c60 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 QLITE_OK returne
13c70 64 2c 20 6a 75 73 74 20 61 73 20 69 66 20 6e 6f d, just as if no
13c80 20 63 6f 72 72 75 70 74 69 6f 6e 20 68 61 64 0a corruption had.
13c90 2a 2a 20 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 ** been encounte
13ca0 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e red..**.** If an
13cb0 20 49 2f 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 I/O or malloc()
13cc0 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 error occurs, t
13cd0 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 he journal-file
13ce0 69 73 20 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a is not deleted.*
13cf0 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 * and an error c
13d00 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
13d10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 48 6f 74 .**.** The isHot
13d20 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64 69 63 parameter indic
13d30 61 74 65 73 20 74 68 61 74 20 77 65 20 61 72 65 ates that we are
13d40 20 74 72 79 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 trying to rollb
13d50 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a ack a journal.**
13d60 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 61 that might be a
13d70 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f hot journal. O
13d80 72 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74 r, it could be t
13d90 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hat the journal
13da0 69 73 20 0a 2a 2a 20 70 72 65 73 65 72 76 65 64 is .** preserved
13db0 20 62 65 63 61 75 73 65 20 6f 66 20 4a 4f 55 52 because of JOUR
13dc0 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 NALMODE_PERSIST
13dd0 6f 72 20 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 or JOURNALMODE_T
13de0 52 55 4e 43 41 54 45 2e 0a 2a 2a 20 49 66 20 74 RUNCATE..** If t
13df0 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 61 6c 6c he journal reall
13e00 79 20 69 73 20 68 6f 74 2c 20 72 65 73 65 74 20 y is hot, reset
13e10 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20 the pager cache
13e20 70 72 69 6f 72 20 72 6f 6c 6c 69 6e 67 0a 2a 2a prior rolling.**
13e30 20 62 61 63 6b 20 61 6e 79 20 63 6f 6e 74 65 6e back any conten
13e40 74 2e 20 20 49 66 20 74 68 65 20 6a 6f 75 72 6e t. If the journ
13e50 61 6c 20 69 73 20 6d 65 72 65 6c 79 20 70 65 72 al is merely per
13e60 73 69 73 74 65 6e 74 2c 20 6e 6f 20 72 65 73 65 sistent, no rese
13e70 74 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64 2e 0a t is.** needed..
13e80 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
13e90 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 50 61 67 ger_playback(Pag
13ea0 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
13eb0 69 73 48 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65 isHot){. sqlite
13ec0 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50 3_vfs *pVfs = pP
13ed0 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36 ager->pVfs;. i6
13ee0 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 4 szJ;
13ef0 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
13f00 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
13f10 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 le in bytes */.
13f20 20 75 33 32 20 6e 52 65 63 3b 20 20 20 20 20 20 u32 nRec;
13f30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
13f40 62 65 72 20 6f 66 20 52 65 63 6f 72 64 73 20 69 ber of Records i
13f50 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f n the journal */
13f60 0a 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 . u32 u;
13f70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 /* U
13f80 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 nsigned loop cou
13f90 6e 74 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d nter */. Pgno m
13fa0 78 50 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 xPg = 0;
13fb0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 /* Size of th
13fc0 65 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 20 e original file
13fd0 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e in pages */. in
13fe0 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
13ff0 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 /* Result
14000 20 63 6f 64 65 20 6f 66 20 61 20 73 75 62 72 6f code of a subro
14010 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 utine */. int r
14020 65 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 es = 1;
14030 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 /* Value ret
14040 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 urned by sqlite3
14050 4f 73 41 63 63 65 73 73 28 29 20 2a 2f 0a 20 20 OsAccess() */.
14060 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 char *zMaster =
14070 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 0; /* Name
14080 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e of master journ
14090 61 6c 20 66 69 6c 65 20 69 66 20 61 6e 79 20 2a al file if any *
140a0 2f 0a 20 20 69 6e 74 20 6e 65 65 64 50 61 67 65 /. int needPage
140b0 72 52 65 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 rReset; /*
140c0 54 72 75 65 20 74 6f 20 72 65 73 65 74 20 70 61 True to reset pa
140d0 67 65 20 70 72 69 6f 72 20 74 6f 20 66 69 72 73 ge prior to firs
140e0 74 20 70 61 67 65 20 72 6f 6c 6c 62 61 63 6b 20 t page rollback
140f0 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 */.. /* Figure
14100 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 out how many rec
14110 6f 72 64 73 20 61 72 65 20 69 6e 20 74 68 65 20 ords are in the
14120 6a 6f 75 72 6e 61 6c 2e 20 20 41 62 6f 72 74 20 journal. Abort
14130 65 61 72 6c 79 20 69 66 0a 20 20 2a 2a 20 74 68 early if. ** th
14140 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 65 6d 70 e journal is emp
14150 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ty.. */. asser
14160 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
14170 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 72 63 20 3d ->jfd) );. rc =
14180 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 sqlite3OsFileSi
14190 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ze(pPager->jfd,
141a0 26 73 7a 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 &szJ);. if( rc!
141b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a =SQLITE_OK || sz
141c0 4a 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f J==0 ){. goto
141d0 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 end_playback;.
141e0 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 }.. /* Read th
141f0 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
14200 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 6a name from the j
14210 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 ournal, if it is
14220 20 70 72 65 73 65 6e 74 2e 0a 20 20 2a 2a 20 49 present.. ** I
14230 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e f a master journ
14240 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 al file name is
14250 73 70 65 63 69 66 69 65 64 2c 20 62 75 74 20 74 specified, but t
14260 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 0a 20 he file is not.
14270 20 2a 2a 20 70 72 65 73 65 6e 74 20 6f 6e 20 64 ** present on d
14280 69 73 6b 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f isk, then the jo
14290 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 68 6f 74 urnal is not hot
142a0 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 and does not ne
142b0 65 64 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70 6c ed to be. ** pl
142c0 61 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a 2a 0a ayed back.. **.
142d0 20 20 2a 2a 20 54 4f 44 4f 3a 20 54 65 63 68 6e ** TODO: Techn
142e0 69 63 61 6c 6c 79 20 74 68 65 20 66 6f 6c 6c 6f ically the follo
142f0 77 69 6e 67 20 69 73 20 61 6e 20 65 72 72 6f 72 wing is an error
14300 20 62 65 63 61 75 73 65 20 69 74 20 61 73 73 75 because it assu
14310 6d 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 62 75 mes that. ** bu
14320 66 66 65 72 20 50 61 67 65 72 2e 70 54 6d 70 53 ffer Pager.pTmpS
14330 70 61 63 65 20 69 73 20 28 6d 78 50 61 74 68 6e pace is (mxPathn
14340 61 6d 65 2b 31 29 20 62 79 74 65 73 20 6f 72 20 ame+1) bytes or
14350 6c 61 72 67 65 72 2e 20 69 2e 65 2e 20 74 68 61 larger. i.e. tha
14360 74 0a 20 20 2a 2a 20 28 70 50 61 67 65 72 2d 3e t. ** (pPager->
14370 70 61 67 65 53 69 7a 65 20 3e 3d 20 70 50 61 67 pageSize >= pPag
14380 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 er->pVfs->mxPath
14390 6e 61 6d 65 2b 31 29 2e 20 55 73 69 6e 67 20 6f name+1). Using o
143a0 73 5f 75 6e 69 78 2e 63 2c 0a 20 20 2a 2a 20 20 s_unix.c,. **
143b0 6d 78 50 61 74 68 6e 61 6d 65 20 69 73 20 35 31 mxPathname is 51
143c0 32 2c 20 77 68 69 63 68 20 69 73 20 74 68 65 20 2, which is the
143d0 73 61 6d 65 20 61 73 20 74 68 65 20 6d 69 6e 69 same as the mini
143e0 6d 75 6d 20 61 6c 6c 6f 77 61 62 6c 65 20 76 61 mum allowable va
143f0 6c 75 65 0a 20 20 2a 2a 20 66 6f 72 20 70 61 67 lue. ** for pag
14400 65 53 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 7a 4d eSize.. */. zM
14410 61 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e aster = pPager->
14420 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 72 63 20 pTmpSpace;. rc
14430 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 = readMasterJour
14440 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c nal(pPager->jfd,
14450 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72 zMaster, pPager
14460 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 ->pVfs->mxPathna
14470 6d 65 2b 31 29 3b 0a 20 20 69 66 28 20 72 63 3d me+1);. if( rc=
14480 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d =SQLITE_OK && zM
14490 61 73 74 65 72 5b 30 5d 20 29 7b 0a 20 20 20 20 aster[0] ){.
144a0 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 rc = sqlite3OsAc
144b0 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 cess(pVfs, zMast
144c0 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 er, SQLITE_ACCES
144d0 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 3b S_EXISTS, &res);
144e0 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 20 3d . }. zMaster =
144f0 20 30 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 0;. if( rc!=SQ
14500 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 72 65 73 20 LITE_OK || !res
14510 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f ){. goto end_
14520 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a 20 20 playback;. }.
14530 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
14540 66 66 20 3d 20 30 3b 0a 20 20 6e 65 65 64 50 61 ff = 0;. needPa
14550 67 65 72 52 65 73 65 74 20 3d 20 69 73 48 6f 74 gerReset = isHot
14560 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f ;.. /* This loo
14570 70 20 74 65 72 6d 69 6e 61 74 65 73 20 65 69 74 p terminates eit
14580 68 65 72 20 77 68 65 6e 20 61 20 72 65 61 64 4a her when a readJ
14590 6f 75 72 6e 61 6c 48 64 72 28 29 20 6f 72 20 0a ournalHdr() or .
145a0 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 ** pager_playb
145b0 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 63 ack_one_page() c
145c0 61 6c 6c 20 72 65 74 75 72 6e 73 20 53 51 4c 49 all returns SQLI
145d0 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 49 4f TE_DONE or an IO
145e0 20 65 72 72 6f 72 20 0a 20 20 2a 2a 20 6f 63 63 error . ** occ
145f0 75 72 73 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 urs. . */. whi
14600 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 2f 2a 20 le( 1 ){. /*
14610 52 65 61 64 20 74 68 65 20 6e 65 78 74 20 6a 6f Read the next jo
14620 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 72 6f urnal header fro
14630 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 m the journal fi
14640 6c 65 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 le. If there ar
14650 65 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 65 6e 6f e. ** not eno
14660 75 67 68 20 62 79 74 65 73 20 6c 65 66 74 20 69 ugh bytes left i
14670 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
14680 6c 65 20 66 6f 72 20 61 20 63 6f 6d 70 6c 65 74 le for a complet
14690 65 20 68 65 61 64 65 72 2c 20 6f 72 0a 20 20 20 e header, or.
146a0 20 2a 2a 20 69 74 20 69 73 20 63 6f 72 72 75 70 ** it is corrup
146b0 74 65 64 2c 20 74 68 65 6e 20 61 20 70 72 6f 63 ted, then a proc
146c0 65 73 73 20 6d 75 73 74 20 6f 66 20 66 61 69 6c ess must of fail
146d0 65 64 20 77 68 69 6c 65 20 77 72 69 74 69 6e 67 ed while writing
146e0 20 69 74 2e 0a 20 20 20 20 2a 2a 20 54 68 69 73 it.. ** This
146f0 20 69 6e 64 69 63 61 74 65 73 20 6e 6f 74 68 69 indicates nothi
14700 6e 67 20 6d 6f 72 65 20 6e 65 65 64 73 20 74 6f ng more needs to
14710 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e be rolled back.
14720 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
14730 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 readJournalHdr(
14740 70 50 61 67 65 72 2c 20 69 73 48 6f 74 2c 20 73 pPager, isHot, s
14750 7a 4a 2c 20 26 6e 52 65 63 2c 20 26 6d 78 50 67 zJ, &nRec, &mxPg
14760 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
14770 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 0a 20 20 20 QLITE_OK ){ .
14780 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
14790 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 E_DONE ){.
147a0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
147b0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
147c0 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 goto end_playbac
147d0 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a k;. }.. /*
147e0 20 49 66 20 6e 52 65 63 20 69 73 20 30 78 66 66 If nRec is 0xff
147f0 66 66 66 66 66 66 2c 20 74 68 65 6e 20 74 68 69 ffffff, then thi
14800 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 63 72 s journal was cr
14810 65 61 74 65 64 20 62 79 20 61 20 70 72 6f 63 65 eated by a proce
14820 73 73 0a 20 20 20 20 2a 2a 20 77 6f 72 6b 69 6e ss. ** workin
14830 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 g in no-sync mod
14840 65 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 e. This means th
14850 61 74 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 at the rest of t
14860 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a he journal. *
14870 2a 20 66 69 6c 65 20 63 6f 6e 73 69 73 74 73 20 * file consists
14880 6f 66 20 70 61 67 65 73 2c 20 74 68 65 72 65 20 of pages, there
14890 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6a 6f 75 72 are no more jour
148a0 6e 61 6c 20 68 65 61 64 65 72 73 2e 20 43 6f 6d nal headers. Com
148b0 70 75 74 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 pute. ** the
148c0 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 62 61 value of nRec ba
148d0 73 65 64 20 6f 6e 20 74 68 69 73 20 61 73 73 75 sed on this assu
148e0 6d 70 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 mption.. */.
148f0 20 20 20 69 66 28 20 6e 52 65 63 3d 3d 30 78 66 if( nRec==0xf
14900 66 66 66 66 66 66 66 20 29 7b 0a 20 20 20 20 20 fffffff ){.
14910 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
14920 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 >journalOff==JOU
14930 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
14940 65 72 29 20 29 3b 0a 20 20 20 20 20 20 6e 52 65 er) );. nRe
14950 63 20 3d 20 28 69 6e 74 29 28 28 73 7a 4a 20 2d c = (int)((szJ -
14960 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 JOURNAL_HDR_SZ(
14970 70 50 61 67 65 72 29 29 2f 4a 4f 55 52 4e 41 4c pPager))/JOURNAL
14980 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 3b _PG_SZ(pPager));
14990 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 . }.. /* I
149a0 66 20 6e 52 65 63 20 69 73 20 30 20 61 6e 64 20 f nRec is 0 and
149b0 74 68 69 73 20 72 6f 6c 6c 62 61 63 6b 20 69 73 this rollback is
149c0 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f of a transactio
149d0 6e 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69 n created by thi
149e0 73 0a 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 s. ** process
149f0 20 61 6e 64 20 69 66 20 74 68 69 73 20 69 73 20 and if this is
14a00 74 68 65 20 66 69 6e 61 6c 20 68 65 61 64 65 72 the final header
14a10 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c in the journal,
14a20 20 74 68 65 6e 20 69 74 20 6d 65 61 6e 73 0a 20 then it means.
14a30 20 20 20 2a 2a 20 74 68 61 74 20 74 68 69 73 20 ** that this
14a40 70 61 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 part of the jour
14a50 6e 61 6c 20 77 61 73 20 62 65 69 6e 67 20 66 69 nal was being fi
14a60 6c 6c 65 64 20 62 75 74 20 68 61 73 20 6e 6f 74 lled but has not
14a70 20 79 65 74 20 62 65 65 6e 0a 20 20 20 20 2a 2a yet been. **
14a80 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e synced to disk.
14a90 20 20 43 6f 6d 70 75 74 65 20 74 68 65 20 6e 75 Compute the nu
14aa0 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 62 61 mber of pages ba
14ab0 73 65 64 20 6f 6e 20 74 68 65 20 72 65 6d 61 69 sed on the remai
14ac0 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 73 69 7a 65 ning. ** size
14ad0 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 of the file..
14ae0 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 **. ** The
14af0 74 68 69 72 64 20 74 65 72 6d 20 6f 66 20 74 68 third term of th
14b00 65 20 74 65 73 74 20 77 61 73 20 61 64 64 65 64 e test was added
14b10 20 74 6f 20 66 69 78 20 74 69 63 6b 65 74 20 23 to fix ticket #
14b20 32 35 36 35 2e 0a 20 20 20 20 2a 2a 20 57 68 65 2565.. ** Whe
14b30 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 n rolling back a
14b40 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 2c 20 6e 52 hot journal, nR
14b50 65 63 3d 3d 30 20 61 6c 77 61 79 73 20 6d 65 61 ec==0 always mea
14b60 6e 73 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 ns that the next
14b70 0a 20 20 20 20 2a 2a 20 63 68 75 6e 6b 20 6f 66 . ** chunk of
14b80 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 63 6f 6e the journal con
14b90 74 61 69 6e 73 20 7a 65 72 6f 20 70 61 67 65 73 tains zero pages
14ba0 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 to be rolled ba
14bb0 63 6b 2e 20 20 42 75 74 0a 20 20 20 20 2a 2a 20 ck. But. **
14bc0 77 68 65 6e 20 64 6f 69 6e 67 20 61 20 52 4f 4c when doing a ROL
14bd0 4c 42 41 43 4b 20 61 6e 64 20 74 68 65 20 6e 52 LBACK and the nR
14be0 65 63 3d 3d 30 20 63 68 75 6e 6b 20 69 73 20 74 ec==0 chunk is t
14bf0 68 65 20 6c 61 73 74 20 63 68 75 6e 6b 20 69 6e he last chunk in
14c00 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 . ** the jour
14c10 6e 61 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 nal, it means th
14c20 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d at the journal m
14c30 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 61 64 64 ight contain add
14c40 69 74 69 6f 6e 61 6c 0a 20 20 20 20 2a 2a 20 70 itional. ** p
14c50 61 67 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 ages that need t
14c60 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b o be rolled back
14c70 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 6e 75 and that the nu
14c80 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 0a 20 mber of pages .
14c90 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 ** should be
14ca0 63 6f 6d 70 75 74 65 64 20 62 61 73 65 64 20 6f computed based o
14cb0 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
14cc0 6c 65 20 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a le size.. */.
14cd0 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d 30 20 if( nRec==0
14ce0 26 26 20 21 69 73 48 6f 74 20 26 26 0a 20 20 20 && !isHot &&.
14cf0 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 pPager->jou
14d00 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f rnalHdr+JOURNAL_
14d10 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d HDR_SZ(pPager)==
14d20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
14d30 66 66 20 29 7b 0a 20 20 20 20 20 20 6e 52 65 63 ff ){. nRec
14d40 20 3d 20 28 69 6e 74 29 28 28 73 7a 4a 20 2d 20 = (int)((szJ -
14d50 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
14d60 66 66 29 20 2f 20 4a 4f 55 52 4e 41 4c 5f 50 47 ff) / JOURNAL_PG
14d70 5f 53 5a 28 70 50 61 67 65 72 29 29 3b 0a 20 20 _SZ(pPager));.
14d80 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 }.. /* If t
14d90 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 his is the first
14da0 20 68 65 61 64 65 72 20 72 65 61 64 20 66 72 6f header read fro
14db0 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 m the journal, t
14dc0 72 75 6e 63 61 74 65 20 74 68 65 0a 20 20 20 20 runcate the.
14dd0 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
14de0 20 62 61 63 6b 20 74 6f 20 69 74 73 20 6f 72 69 back to its ori
14df0 67 69 6e 61 6c 20 73 69 7a 65 2e 0a 20 20 20 20 ginal size..
14e00 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 */. if( pPage
14e10 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a r->journalOff==J
14e20 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
14e30 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 72 ager) ){. r
14e40 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61 c = pager_trunca
14e50 74 65 28 70 50 61 67 65 72 2c 20 6d 78 50 67 29 te(pPager, mxPg)
14e60 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
14e70 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
14e80 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c goto end_pl
14e90 61 79 62 61 63 6b 3b 0a 20 20 20 20 20 20 7d 0a ayback;. }.
14ea0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 pPager->db
14eb0 53 69 7a 65 20 3d 20 6d 78 50 67 3b 0a 20 20 20 Size = mxPg;.
14ec0 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 }.. /* Copy
14ed0 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 6f original pages o
14ee0 75 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 ut of the journa
14ef0 6c 20 61 6e 64 20 62 61 63 6b 20 69 6e 74 6f 20 l and back into
14f00 74 68 65 20 0a 20 20 20 20 2a 2a 20 64 61 74 61 the . ** data
14f10 62 61 73 65 20 66 69 6c 65 20 61 6e 64 2f 6f 72 base file and/or
14f20 20 70 61 67 65 20 63 61 63 68 65 2e 0a 20 20 20 page cache..
14f30 20 2a 2f 0a 20 20 20 20 66 6f 72 28 75 3d 30 3b */. for(u=0;
14f40 20 75 3c 6e 52 65 63 3b 20 75 2b 2b 29 7b 0a 20 u<nRec; u++){.
14f50 20 20 20 20 20 69 66 28 20 6e 65 65 64 50 61 67 if( needPag
14f60 65 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20 erReset ){.
14f70 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 pager_reset(p
14f80 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 Pager);.
14f90 6e 65 65 64 50 61 67 65 72 52 65 73 65 74 20 3d needPagerReset =
14fa0 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
14fb0 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 rc = pager_pla
14fc0 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 yback_one_page(p
14fd0 50 61 67 65 72 2c 26 70 50 61 67 65 72 2d 3e 6a Pager,&pPager->j
14fe0 6f 75 72 6e 61 6c 4f 66 66 2c 30 2c 31 2c 30 29 ournalOff,0,1,0)
14ff0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
15000 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
15010 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
15020 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 ITE_DONE ){.
15030 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
15040 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 E_OK;.
15050 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
15060 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 20 20 20 20 ff = szJ;.
15070 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
15080 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d }else if( rc=
15090 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 =SQLITE_IOERR_SH
150a0 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 ORT_READ ){.
150b0 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
150c0 6a 6f 75 72 6e 61 6c 20 68 61 73 20 62 65 65 6e journal has been
150d0 20 74 72 75 6e 63 61 74 65 64 2c 20 73 69 6d 70 truncated, simp
150e0 6c 79 20 73 74 6f 70 20 72 65 61 64 69 6e 67 20 ly stop reading
150f0 61 6e 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a and. **
15100 20 70 72 6f 63 65 73 73 69 6e 67 20 74 68 65 20 processing the
15110 6a 6f 75 72 6e 61 6c 2e 20 54 68 69 73 20 6d 69 journal. This mi
15120 67 68 74 20 68 61 70 70 65 6e 20 69 66 20 74 68 ght happen if th
15130 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 0a 20 20 e journal was.
15140 20 20 20 20 20 20 20 20 2a 2a 20 6e 6f 74 20 63 ** not c
15150 6f 6d 70 6c 65 74 65 6c 79 20 77 72 69 74 74 65 ompletely writte
15160 6e 20 61 6e 64 20 73 79 6e 63 65 64 20 70 72 69 n and synced pri
15170 6f 72 20 74 6f 20 61 20 63 72 61 73 68 2e 20 20 or to a crash.
15180 49 6e 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 In that.
15190 20 20 2a 2a 20 63 61 73 65 2c 20 74 68 65 20 64 ** case, the d
151a0 61 74 61 62 61 73 65 20 73 68 6f 75 6c 64 20 68 atabase should h
151b0 61 76 65 20 6e 65 76 65 72 20 62 65 65 6e 20 77 ave never been w
151c0 72 69 74 74 65 6e 20 69 6e 20 74 68 65 0a 20 20 ritten in the.
151d0 20 20 20 20 20 20 20 20 2a 2a 20 66 69 72 73 74 ** first
151e0 20 70 6c 61 63 65 20 73 6f 20 69 74 20 69 73 20 place so it is
151f0 4f 4b 20 74 6f 20 73 69 6d 70 6c 79 20 61 62 61 OK to simply aba
15200 6e 64 6f 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 ndon the rollbac
15210 6b 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 k. */.
15220 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
15230 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 goto e
15240 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 nd_playback;.
15250 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
15260 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 /* If we a
15270 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 72 6f 6c re unable to rol
15280 6c 62 61 63 6b 2c 20 71 75 69 74 20 61 6e 64 20 lback, quit and
15290 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 return the error
152a0 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f . ** co
152b0 64 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 63 de. This will c
152c0 61 75 73 65 20 74 68 65 20 70 61 67 65 72 20 74 ause the pager t
152d0 6f 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f o enter the erro
152e0 72 20 73 74 61 74 65 0a 20 20 20 20 20 20 20 20 r state.
152f0 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 6e 6f 20 ** so that no
15300 66 75 72 74 68 65 72 20 68 61 72 6d 20 77 69 6c further harm wil
15310 6c 20 62 65 20 64 6f 6e 65 2e 20 20 50 65 72 68 l be done. Perh
15320 61 70 73 20 74 68 65 20 6e 65 78 74 0a 20 20 20 aps the next.
15330 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 ** proces
15340 73 20 74 6f 20 63 6f 6d 65 20 61 6c 6f 6e 67 20 s to come along
15350 77 69 6c 6c 20 62 65 20 61 62 6c 65 20 74 6f 20 will be able to
15360 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 64 61 74 rollback the dat
15370 61 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20 abase..
15380 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 67 6f */. go
15390 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b to end_playback;
153a0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
153b0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f }. }. }. /
153c0 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 *NOTREACHED*/.
153d0 61 73 73 65 72 74 28 20 30 20 29 3b 0a 0a 65 6e assert( 0 );..en
153e0 64 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 2f 2a d_playback:. /*
153f0 20 46 6f 6c 6c 6f 77 69 6e 67 20 61 20 72 6f 6c Following a rol
15400 6c 62 61 63 6b 2c 20 74 68 65 20 64 61 74 61 62 lback, the datab
15410 61 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 ase file should
15420 62 65 20 62 61 63 6b 20 69 6e 20 69 74 73 20 6f be back in its o
15430 72 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 73 74 61 riginal. ** sta
15440 74 65 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 te prior to the
15450 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 start of the tra
15460 6e 73 61 63 74 69 6f 6e 2c 20 73 6f 20 69 6e 76 nsaction, so inv
15470 6f 6b 65 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c oke the. ** SQL
15480 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 ITE_FCNTL_DB_UNC
15490 48 41 4e 47 45 44 20 66 69 6c 65 2d 63 6f 6e 74 HANGED file-cont
154a0 72 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20 64 69 rol method to di
154b0 73 61 62 6c 65 20 74 68 65 0a 20 20 2a 2a 20 61 sable the. ** a
154c0 73 73 65 72 74 69 6f 6e 20 74 68 61 74 20 74 68 ssertion that th
154d0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f e transaction co
154e0 75 6e 74 65 72 20 77 61 73 20 6d 6f 64 69 66 69 unter was modifi
154f0 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ed.. */. asser
15500 74 28 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 66 t(. pPager->f
15510 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 20 7c d->pMethods==0 |
15520 7c 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 |. sqlite3OsF
15530 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 61 67 65 ileControl(pPage
15540 72 2d 3e 66 64 2c 53 51 4c 49 54 45 5f 46 43 4e r->fd,SQLITE_FCN
15550 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 2c TL_DB_UNCHANGED,
15560 30 29 3e 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 0)>=SQLITE_OK.
15570 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 );.. /* If this
15580 20 70 6c 61 79 62 61 63 6b 20 69 73 20 68 61 70 playback is hap
15590 70 65 6e 69 6e 67 20 61 75 74 6f 6d 61 74 69 63 pening automatic
155a0 61 6c 6c 79 20 61 73 20 61 20 72 65 73 75 6c 74 ally as a result
155b0 20 6f 66 20 61 6e 20 49 4f 20 6f 72 20 0a 20 20 of an IO or .
155c0 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 ** malloc error
155d0 74 68 61 74 20 6f 63 63 75 72 72 65 64 20 61 66 that occurred af
155e0 74 65 72 20 74 68 65 20 63 68 61 6e 67 65 2d 63 ter the change-c
155f0 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64 61 74 ounter was updat
15600 65 64 20 62 75 74 20 0a 20 20 2a 2a 20 62 65 66 ed but . ** bef
15610 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 ore the transact
15620 69 6f 6e 20 77 61 73 20 63 6f 6d 6d 69 74 74 65 ion was committe
15630 64 2c 20 74 68 65 6e 20 74 68 65 20 63 68 61 6e d, then the chan
15640 67 65 2d 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2a ge-counter . **
15650 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 6d 61 modification ma
15660 79 20 6a 75 73 74 20 68 61 76 65 20 62 65 65 6e y just have been
15670 20 72 65 76 65 72 74 65 64 2e 20 49 66 20 74 68 reverted. If th
15680 69 73 20 68 61 70 70 65 6e 73 20 69 6e 20 65 78 is happens in ex
15690 63 6c 75 73 69 76 65 20 0a 20 20 2a 2a 20 6d 6f clusive . ** mo
156a0 64 65 2c 20 74 68 65 6e 20 73 75 62 73 65 71 75 de, then subsequ
156b0 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 ent transactions
156c0 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 performed by th
156d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 6c e connection wil
156e0 6c 20 6e 6f 74 0a 20 20 2a 2a 20 75 70 64 61 74 l not. ** updat
156f0 65 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 e the change-cou
15700 6e 74 65 72 20 61 74 20 61 6c 6c 2e 20 54 68 69 nter at all. Thi
15710 73 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 63 61 s may lead to ca
15720 63 68 65 20 69 6e 63 6f 6e 73 69 73 74 65 6e 63 che inconsistenc
15730 79 0a 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 y. ** problems
15740 66 6f 72 20 6f 74 68 65 72 20 70 72 6f 63 65 73 for other proces
15750 73 65 73 20 61 74 20 73 6f 6d 65 20 70 6f 69 6e ses at some poin
15760 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e t in the future.
15770 20 53 6f 2c 20 6a 75 73 74 0a 20 20 2a 2a 20 69 So, just. ** i
15780 6e 20 63 61 73 65 20 74 68 69 73 20 68 61 73 20 n case this has
15790 68 61 70 70 65 6e 65 64 2c 20 63 6c 65 61 72 20 happened, clear
157a0 74 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 the changeCountD
157b0 6f 6e 65 20 66 6c 61 67 20 6e 6f 77 2e 0a 20 20 one flag now..
157c0 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 63 68 61 */. pPager->cha
157d0 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 ngeCountDone = p
157e0 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b Pager->tempFile;
157f0 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 .. if( rc==SQLI
15800 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 7a 4d 61 TE_OK ){. zMa
15810 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 ster = pPager->p
15820 54 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 72 63 TmpSpace;. rc
15830 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 = readMasterJou
15840 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 rnal(pPager->jfd
15850 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 , zMaster, pPage
15860 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e r->pVfs->mxPathn
15870 61 6d 65 2b 31 29 3b 0a 20 20 20 20 74 65 73 74 ame+1);. test
15880 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 case( rc!=SQLITE
15890 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 _OK );. }. if(
158a0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
158b0 26 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 & pPager->noSync
158c0 3d 3d 30 20 26 26 20 70 50 61 67 65 72 2d 3e 73 ==0 && pPager->s
158d0 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c tate>=PAGER_EXCL
158e0 55 53 49 56 45 20 29 7b 0a 20 20 20 20 72 63 20 USIVE ){. rc
158f0 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 = sqlite3OsSync(
15900 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 pPager->fd, pPag
15910 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b er->sync_flags);
15920 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 . }. if( rc==S
15930 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 QLITE_OK && pPag
15940 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 26 26 er->noSync==0 &&
15950 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d pPager->state>=
15960 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 PAGER_EXCLUSIVE
15970 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
15980 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 te3OsSync(pPager
15990 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 ->fd, pPager->sy
159a0 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 7d 0a 20 nc_flags);. }.
159b0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
159c0 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 OK ){. rc = p
159d0 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 ager_end_transac
159e0 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 7a 4d 61 tion(pPager, zMa
159f0 73 74 65 72 5b 30 5d 21 3d 27 5c 30 27 29 3b 0a ster[0]!='\0');.
15a00 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 testcase( rc
15a10 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 !=SQLITE_OK );.
15a20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c }. if( rc==SQL
15a30 49 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 65 ITE_OK && zMaste
15a40 72 5b 30 5d 20 26 26 20 72 65 73 20 29 7b 0a 20 r[0] && res ){.
15a50 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 77 /* If there w
15a60 61 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 as a master jour
15a70 6e 61 6c 20 61 6e 64 20 74 68 69 73 20 72 6f 75 nal and this rou
15a80 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e tine will return
15a90 20 73 75 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a success,. **
15aa0 20 73 65 65 20 69 66 20 69 74 20 69 73 20 70 6f see if it is po
15ab0 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 ssible to delete
15ac0 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
15ad0 6e 61 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 nal.. */.
15ae0 72 63 20 3d 20 70 61 67 65 72 5f 64 65 6c 6d 61 rc = pager_delma
15af0 73 74 65 72 28 70 50 61 67 65 72 2c 20 7a 4d 61 ster(pPager, zMa
15b00 73 74 65 72 29 3b 0a 20 20 20 20 74 65 73 74 63 ster);. testc
15b10 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ase( rc!=SQLITE_
15b20 4f 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 OK );. }.. /*
15b30 54 68 65 20 50 61 67 65 72 2e 73 65 63 74 6f 72 The Pager.sector
15b40 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 6d 61 Size variable ma
15b50 79 20 68 61 76 65 20 62 65 65 6e 20 75 70 64 61 y have been upda
15b60 74 65 64 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e ted while rollin
15b70 67 0a 20 20 2a 2a 20 62 61 63 6b 20 61 20 6a 6f g. ** back a jo
15b80 75 72 6e 61 6c 20 63 72 65 61 74 65 64 20 62 79 urnal created by
15b90 20 61 20 70 72 6f 63 65 73 73 20 77 69 74 68 20 a process with
15ba0 61 20 64 69 66 66 65 72 65 6e 74 20 73 65 63 74 a different sect
15bb0 6f 72 20 73 69 7a 65 0a 20 20 2a 2a 20 76 61 6c or size. ** val
15bc0 75 65 2e 20 52 65 73 65 74 20 69 74 20 74 6f 20 ue. Reset it to
15bd0 74 68 65 20 63 6f 72 72 65 63 74 20 76 61 6c 75 the correct valu
15be0 65 20 66 6f 72 20 74 68 69 73 20 70 72 6f 63 65 e for this proce
15bf0 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 65 74 53 65 ss.. */. setSe
15c00 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 ctorSize(pPager)
15c10 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
15c20 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 .../*.** Read th
15c30 65 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 70 61 e content for pa
15c40 67 65 20 70 50 67 20 6f 75 74 20 6f 66 20 74 68 ge pPg out of th
15c50 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
15c60 61 6e 64 20 69 6e 74 6f 20 0a 2a 2a 20 70 50 67 and into .** pPg
15c70 2d 3e 70 44 61 74 61 2e 20 41 20 73 68 61 72 65 ->pData. A share
15c80 64 20 6c 6f 63 6b 20 6f 72 20 67 72 65 61 74 65 d lock or greate
15c90 72 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 6f r must be held o
15ca0 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a n the database.*
15cb0 2a 20 66 69 6c 65 20 62 65 66 6f 72 65 20 74 68 * file before th
15cc0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
15cd0 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 alled..**.** If
15ce0 70 61 67 65 20 31 20 69 73 20 72 65 61 64 2c 20 page 1 is read,
15cf0 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 6f then the value o
15d00 66 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 f Pager.dbFileVe
15d10 72 73 5b 5d 20 69 73 20 73 65 74 20 74 6f 0a 2a rs[] is set to.*
15d20 2a 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 * the value read
15d30 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 from the databa
15d40 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 se file..**.** I
15d50 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 f an IO error oc
15d60 63 75 72 73 2c 20 74 68 65 6e 20 74 68 65 20 49 curs, then the I
15d70 4f 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 O error is retur
15d80 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 ned to the calle
15d90 72 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c r..** Otherwise,
15da0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
15db0 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 turned..*/.stati
15dc0 63 20 69 6e 74 20 72 65 61 64 44 62 50 61 67 65 c int readDbPage
15dd0 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 (PgHdr *pPg){.
15de0 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 Pager *pPager =
15df0 70 50 67 2d 3e 70 50 61 67 65 72 3b 20 2f 2a 20 pPg->pPager; /*
15e00 50 61 67 65 72 20 6f 62 6a 65 63 74 20 61 73 73 Pager object ass
15e10 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 61 67 ociated with pag
15e20 65 20 70 50 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 e pPg */. Pgno
15e30 70 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f pgno = pPg->pgno
15e40 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 ; /* Page
15e50 6e 75 6d 62 65 72 20 74 6f 20 72 65 61 64 20 2a number to read *
15e60 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c /. int rc = SQL
15e70 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 ITE_OK;
15e80 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
15e90 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e 57 61 6c */. int isInWal
15ea0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
15eb0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 67 /* True if pag
15ec0 65 20 69 73 20 69 6e 20 6c 6f 67 20 66 69 6c 65 e is in log file
15ed0 20 2a 2f 0a 20 20 69 6e 74 20 70 67 73 7a 20 3d */. int pgsz =
15ee0 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
15ef0 65 3b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 e; /* Number of
15f00 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f bytes to read */
15f10 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
15f20 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 er->state>=PAGER
15f30 5f 53 48 41 52 45 44 20 26 26 20 21 4d 45 4d 44 _SHARED && !MEMD
15f40 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 B );. assert( i
15f50 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 sOpen(pPager->fd
15f60 29 20 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 ) );.. if( NEVE
15f70 52 28 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 R(!isOpen(pPager
15f80 2d 3e 66 64 29 29 20 29 7b 0a 20 20 20 20 61 73 ->fd)) ){. as
15f90 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65 sert( pPager->te
15fa0 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 6d 65 mpFile );. me
15fb0 6d 73 65 74 28 70 50 67 2d 3e 70 44 61 74 61 2c mset(pPg->pData,
15fc0 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 0, pPager->page
15fd0 53 69 7a 65 29 3b 0a 20 20 20 20 72 65 74 75 72 Size);. retur
15fe0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
15ff0 0a 0a 20 20 69 66 28 20 70 61 67 65 72 55 73 65 .. if( pagerUse
16000 57 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 Wal(pPager) ){.
16010 20 20 20 2f 2a 20 54 72 79 20 74 6f 20 70 75 6c /* Try to pul
16020 6c 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 l the page from
16030 74 68 65 20 77 72 69 74 65 2d 61 68 65 61 64 20 the write-ahead
16040 6c 6f 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d log. */. rc =
16050 20 73 71 6c 69 74 65 33 57 61 6c 52 65 61 64 28 sqlite3WalRead(
16060 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 70 67 pPager->pWal, pg
16070 6e 6f 2c 20 26 69 73 49 6e 57 61 6c 2c 20 70 67 no, &isInWal, pg
16080 73 7a 2c 20 70 50 67 2d 3e 70 44 61 74 61 29 3b sz, pPg->pData);
16090 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 . }. if( rc==S
160a0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 69 73 49 QLITE_OK && !isI
160b0 6e 57 61 6c 20 29 7b 0a 20 20 20 20 69 36 34 20 nWal ){. i64
160c0 69 4f 66 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d iOffset = (pgno-
160d0 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 1)*(i64)pPager->
160e0 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 pageSize;. rc
160f0 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 = sqlite3OsRead
16100 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 67 (pPager->fd, pPg
16110 2d 3e 70 44 61 74 61 2c 20 70 67 73 7a 2c 20 69 ->pData, pgsz, i
16120 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 69 66 28 Offset);. if(
16130 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 rc==SQLITE_IOER
16140 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a R_SHORT_READ ){.
16150 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
16160 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a E_OK;. }. }.
16170 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 . if( pgno==1 )
16180 7b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a {. if( rc ){.
16190 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
161a0 72 65 61 64 20 69 73 20 75 6e 73 75 63 63 65 73 read is unsucces
161b0 73 66 75 6c 2c 20 73 65 74 20 74 68 65 20 64 62 sful, set the db
161c0 46 69 6c 65 56 65 72 73 5b 5d 20 74 6f 20 73 6f FileVers[] to so
161d0 6d 65 74 68 69 6e 67 0a 20 20 20 20 20 20 2a 2a mething. **
161e0 20 74 68 61 74 20 77 69 6c 6c 20 6e 65 76 65 72 that will never
161f0 20 62 65 20 61 20 76 61 6c 69 64 20 66 69 6c 65 be a valid file
16200 20 76 65 72 73 69 6f 6e 2e 20 20 64 62 46 69 6c version. dbFil
16210 65 56 65 72 73 5b 5d 20 69 73 20 61 20 63 6f 70 eVers[] is a cop
16220 79 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 62 79 y. ** of by
16230 74 65 73 20 32 34 2e 2e 33 39 20 6f 66 20 74 68 tes 24..39 of th
16240 65 20 64 61 74 61 62 61 73 65 2e 20 20 42 79 74 e database. Byt
16250 65 73 20 32 38 2e 2e 33 31 20 73 68 6f 75 6c 64 es 28..31 should
16260 20 61 6c 77 61 79 73 20 62 65 0a 20 20 20 20 20 always be.
16270 20 2a 2a 20 7a 65 72 6f 20 6f 72 20 74 68 65 20 ** zero or the
16280 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 size of the data
16290 62 61 73 65 20 69 6e 20 70 61 67 65 2e 20 42 79 base in page. By
162a0 74 65 73 20 33 32 2e 2e 33 35 20 61 6e 64 20 33 tes 32..35 and 3
162b0 35 2e 2e 33 39 0a 20 20 20 20 20 20 2a 2a 20 73 5..39. ** s
162c0 68 6f 75 6c 64 20 62 65 20 70 61 67 65 20 6e 75 hould be page nu
162d0 6d 62 65 72 73 20 77 68 69 63 68 20 61 72 65 20 mbers which are
162e0 6e 65 76 65 72 20 30 78 66 66 66 66 66 66 66 66 never 0xffffffff
162f0 2e 20 20 53 6f 20 66 69 6c 6c 69 6e 67 0a 20 20 . So filling.
16300 20 20 20 20 2a 2a 20 70 50 61 67 65 72 2d 3e 64 ** pPager->d
16310 62 46 69 6c 65 56 65 72 73 5b 5d 20 77 69 74 68 bFileVers[] with
16320 20 61 6c 6c 20 30 78 66 66 20 62 79 74 65 73 20 all 0xff bytes
16330 73 68 6f 75 6c 64 20 73 75 66 66 69 63 65 2e 0a should suffice..
16340 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
16350 2a 20 46 6f 72 20 61 6e 20 65 6e 63 72 79 70 74 * For an encrypt
16360 65 64 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 ed database, the
16370 20 73 69 74 75 61 74 69 6f 6e 20 69 73 20 6d 6f situation is mo
16380 72 65 20 63 6f 6d 70 6c 65 78 3a 20 20 62 79 74 re complex: byt
16390 65 73 0a 20 20 20 20 20 20 2a 2a 20 32 34 2e 2e es. ** 24..
163a0 33 39 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 39 of the databa
163b0 73 65 20 61 72 65 20 77 68 69 74 65 20 6e 6f 69 se are white noi
163c0 73 65 2e 20 20 42 75 74 20 74 68 65 20 70 72 6f se. But the pro
163d0 62 61 62 69 6c 69 74 79 20 6f 66 0a 20 20 20 20 bability of.
163e0 20 20 2a 2a 20 77 68 69 74 65 20 6e 6f 69 73 69 ** white noisi
163f0 6e 67 20 65 71 75 61 6c 69 6e 67 20 31 36 20 62 ng equaling 16 b
16400 79 74 65 73 20 6f 66 20 30 78 66 66 20 69 73 20 ytes of 0xff is
16410 76 61 6e 69 73 68 69 6e 67 6c 79 20 73 6d 61 6c vanishingly smal
16420 6c 20 73 6f 0a 20 20 20 20 20 20 2a 2a 20 77 65 l so. ** we
16430 20 73 68 6f 75 6c 64 20 73 74 69 6c 6c 20 62 65 should still be
16440 20 6f 6b 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ok.. */.
16450 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 67 65 memset(pPage
16460 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 30 r->dbFileVers, 0
16470 78 66 66 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 xff, sizeof(pPag
16480 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 er->dbFileVers))
16490 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
164a0 20 20 20 75 38 20 2a 64 62 46 69 6c 65 56 65 72 u8 *dbFileVer
164b0 73 20 3d 20 26 28 28 75 38 2a 29 70 50 67 2d 3e s = &((u8*)pPg->
164c0 70 44 61 74 61 29 5b 32 34 5d 3b 0a 20 20 20 20 pData)[24];.
164d0 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 memcpy(&pPager
164e0 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 64 62 ->dbFileVers, db
164f0 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 FileVers, sizeof
16500 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 (pPager->dbFileV
16510 65 72 73 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ers));. }. }
16520 0a 20 20 43 4f 44 45 43 31 28 70 50 61 67 65 72 . CODEC1(pPager
16530 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c 20 70 67 , pPg->pData, pg
16540 6e 6f 2c 20 33 2c 20 72 63 20 3d 20 53 51 4c 49 no, 3, rc = SQLI
16550 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 0a 20 20 50 41 TE_NOMEM);.. PA
16560 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 GER_INCR(sqlite3
16570 5f 70 61 67 65 72 5f 72 65 61 64 64 62 5f 63 6f _pager_readdb_co
16580 75 6e 74 29 3b 0a 20 20 50 41 47 45 52 5f 49 4e unt);. PAGER_IN
16590 43 52 28 70 50 61 67 65 72 2d 3e 6e 52 65 61 64 CR(pPager->nRead
165a0 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 50 );. IOTRACE(("P
165b0 47 49 4e 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 GIN %p %d\n", pP
165c0 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 ager, pgno));.
165d0 50 41 47 45 52 54 52 41 43 45 28 28 22 46 45 54 PAGERTRACE(("FET
165e0 43 48 20 25 64 20 70 61 67 65 20 25 64 20 68 61 CH %d page %d ha
165f0 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 sh(%08x)\n",.
16600 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 PAGE
16610 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 67 6e RID(pPager), pgn
16620 6f 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 o, pager_pagehas
16630 68 28 70 50 67 29 29 29 3b 0a 0a 20 20 72 65 74 h(pPg)));.. ret
16640 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 urn rc;.}..#ifnd
16650 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 ef SQLITE_OMIT_W
16660 41 4c 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 AL./*.** This fu
16670 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 nction is invoke
16680 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 d once for each
16690 70 61 67 65 20 74 68 61 74 20 68 61 73 20 61 6c page that has al
166a0 72 65 61 64 79 20 62 65 65 6e 20 0a 2a 2a 20 77 ready been .** w
166b0 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
166c0 6c 6f 67 20 66 69 6c 65 20 77 68 65 6e 20 61 20 log file when a
166d0 57 41 4c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 WAL transaction
166e0 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a is rolled back..
166f0 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 69 50 67 ** Parameter iPg
16700 20 69 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d is the page num
16710 62 65 72 20 6f 66 20 73 61 69 64 20 70 61 67 65 ber of said page
16720 2e 20 54 68 65 20 70 43 74 78 20 61 72 67 75 6d . The pCtx argum
16730 65 6e 74 20 0a 2a 2a 20 69 73 20 61 63 74 75 61 ent .** is actua
16740 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74 6f lly a pointer to
16750 20 74 68 65 20 50 61 67 65 72 20 73 74 72 75 63 the Pager struc
16760 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 ture..**.** If p
16770 61 67 65 20 69 50 67 20 69 73 20 70 72 65 73 65 age iPg is prese
16780 6e 74 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c nt in the cache,
16790 20 61 6e 64 20 68 61 73 20 6e 6f 20 6f 75 74 73 and has no outs
167a0 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 tanding referenc
167b0 65 73 2c 0a 2a 2a 20 69 74 20 69 73 20 64 69 73 es,.** it is dis
167c0 63 61 72 64 65 64 2e 20 4f 74 68 65 72 77 69 73 carded. Otherwis
167d0 65 2c 20 69 66 20 74 68 65 72 65 20 61 72 65 20 e, if there are
167e0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 75 74 73 one or more outs
167f0 74 61 6e 64 69 6e 67 0a 2a 2a 20 72 65 66 65 72 tanding.** refer
16800 65 6e 63 65 73 2c 20 74 68 65 20 70 61 67 65 20 ences, the page
16810 63 6f 6e 74 65 6e 74 20 69 73 20 72 65 6c 6f 61 content is reloa
16820 64 65 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 ded from the dat
16830 61 62 61 73 65 2e 20 49 66 20 74 68 65 0a 2a 2a abase. If the.**
16840 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 6c 6f attempt to relo
16850 61 64 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 ad content from
16860 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
16870 72 65 71 75 69 72 65 64 20 61 6e 64 20 66 61 69 required and fai
16880 6c 73 2c 20 0a 2a 2a 20 72 65 74 75 72 6e 20 61 ls, .** return a
16890 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 n SQLite error c
168a0 6f 64 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ode. Otherwise,
168b0 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 SQLITE_OK..*/.st
168c0 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 55 6e atic int pagerUn
168d0 64 6f 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20 doCallback(void
168e0 2a 70 43 74 78 2c 20 50 67 6e 6f 20 69 50 67 29 *pCtx, Pgno iPg)
168f0 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
16900 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 ITE_OK;. Pager
16910 2a 70 50 61 67 65 72 20 3d 20 28 50 61 67 65 72 *pPager = (Pager
16920 20 2a 29 70 43 74 78 3b 0a 20 20 50 67 48 64 72 *)pCtx;. PgHdr
16930 20 2a 70 50 67 3b 0a 0a 20 20 70 50 67 20 3d 20 *pPg;.. pPg =
16940 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b sqlite3PagerLook
16950 75 70 28 70 50 61 67 65 72 2c 20 69 50 67 29 3b up(pPager, iPg);
16960 0a 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 . if( pPg ){.
16970 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 63 61 if( sqlite3Pca
16980 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74 28 chePageRefcount(
16990 70 50 67 29 3d 3d 31 20 29 7b 0a 20 20 20 20 20 pPg)==1 ){.
169a0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 sqlite3PcacheDr
169b0 6f 70 28 70 50 67 29 3b 0a 20 20 20 20 7d 65 6c op(pPg);. }el
169c0 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 se{. rc = r
169d0 65 61 64 44 62 50 61 67 65 28 70 50 67 29 3b 0a eadDbPage(pPg);.
169e0 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
169f0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
16a00 20 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e pPager->xRein
16a10 69 74 65 72 28 70 50 67 29 3b 0a 20 20 20 20 20 iter(pPg);.
16a20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
16a30 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 29 3b PagerUnref(pPg);
16a40 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
16a50 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 66 20 61 20 Normally, if a
16a60 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 transaction is r
16a70 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 61 6e 79 20 olled back, any
16a80 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73 65 73 backup processes
16a90 20 61 72 65 0a 20 20 2a 2a 20 75 70 64 61 74 65 are. ** update
16aa0 64 20 61 73 20 64 61 74 61 20 69 73 20 63 6f 70 d as data is cop
16ab0 69 65 64 20 6f 75 74 20 6f 66 20 74 68 65 20 72 ied out of the r
16ac0 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 ollback journal
16ad0 61 6e 64 20 69 6e 74 6f 20 74 68 65 0a 20 20 2a and into the. *
16ae0 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68 69 73 * database. This
16af0 20 69 73 20 6e 6f 74 20 67 65 6e 65 72 61 6c 6c is not generall
16b00 79 20 70 6f 73 73 69 62 6c 65 20 77 69 74 68 20 y possible with
16b10 61 20 57 41 4c 20 64 61 74 61 62 61 73 65 2c 20 a WAL database,
16b20 61 73 0a 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b as. ** rollback
16b30 20 69 6e 76 6f 6c 76 65 73 20 73 69 6d 70 6c 79 involves simply
16b40 20 74 72 75 6e 63 61 74 69 6e 67 20 74 68 65 20 truncating the
16b50 6c 6f 67 20 66 69 6c 65 2e 20 54 68 65 72 65 66 log file. Theref
16b60 6f 72 65 2c 20 69 66 20 6f 6e 65 0a 20 20 2a 2a ore, if one. **
16b70 20 6f 72 20 6d 6f 72 65 20 66 72 61 6d 65 73 20 or more frames
16b80 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 have already bee
16b90 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 n written to the
16ba0 20 6c 6f 67 20 28 61 6e 64 20 74 68 65 72 65 66 log (and theref
16bb0 6f 72 65 20 0a 20 20 2a 2a 20 61 6c 73 6f 20 63 ore . ** also c
16bc0 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 62 opied into the b
16bd0 61 63 6b 75 70 20 64 61 74 61 62 61 73 65 73 29 ackup databases)
16be0 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 69 73 as part of this
16bf0 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 transaction,.
16c00 2a 2a 20 74 68 65 20 62 61 63 6b 75 70 73 20 6d ** the backups m
16c10 75 73 74 20 62 65 20 72 65 73 74 61 72 74 65 64 ust be restarted
16c20 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .. */. sqlite3
16c30 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 70 50 BackupRestart(pP
16c40 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b 0a ager->pBackup);.
16c50 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
16c60 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
16c70 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 tion is called t
16c80 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 74 72 61 o rollback a tra
16c90 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 61 20 57 41 nsaction on a WA
16ca0 4c 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 L database..*/.s
16cb0 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 52 tatic int pagerR
16cc0 6f 6c 6c 62 61 63 6b 57 61 6c 28 50 61 67 65 72 ollbackWal(Pager
16cd0 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 *pPager){. int
16ce0 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
16cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16d00 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 Return Code */.
16d10 20 50 67 48 64 72 20 2a 70 4c 69 73 74 3b 20 20 PgHdr *pList;
16d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16d30 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 72 74 /* List of dirt
16d40 79 20 70 61 67 65 73 20 74 6f 20 72 65 76 65 72 y pages to rever
16d50 74 20 2a 2f 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 t */.. /* For a
16d60 6c 6c 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 ll pages in the
16d70 63 61 63 68 65 20 74 68 61 74 20 61 72 65 20 63 cache that are c
16d80 75 72 72 65 6e 74 6c 79 20 64 69 72 74 79 20 6f urrently dirty o
16d90 72 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 20 r have already.
16da0 20 2a 2a 20 62 65 65 6e 20 77 72 69 74 74 65 6e ** been written
16db0 20 28 62 75 74 20 6e 6f 74 20 63 6f 6d 6d 69 74 (but not commit
16dc0 74 65 64 29 20 74 6f 20 74 68 65 20 6c 6f 67 20 ted) to the log
16dd0 66 69 6c 65 2c 20 64 6f 20 6f 6e 65 20 6f 66 20 file, do one of
16de0 74 68 65 20 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 the . ** follow
16df0 69 6e 67 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 ing:. **. **
16e00 20 2b 20 44 69 73 63 61 72 64 20 74 68 65 20 63 + Discard the c
16e10 61 63 68 65 64 20 70 61 67 65 20 28 69 66 20 72 ached page (if r
16e20 65 66 63 6f 75 6e 74 3d 3d 30 29 2c 20 6f 72 0a efcount==0), or.
16e30 20 20 2a 2a 20 20 20 2b 20 52 65 6c 6f 61 64 20 ** + Reload
16e40 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 66 72 6f page content fro
16e50 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 m the database (
16e60 69 66 20 72 65 66 63 6f 75 6e 74 3e 30 29 2e 0a if refcount>0)..
16e70 20 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 */. pPager->d
16e80 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e bSize = pPager->
16e90 64 62 4f 72 69 67 53 69 7a 65 3b 0a 20 20 72 63 dbOrigSize;. rc
16ea0 20 3d 20 73 71 6c 69 74 65 33 57 61 6c 55 6e 64 = sqlite3WalUnd
16eb0 6f 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 o(pPager->pWal,
16ec0 70 61 67 65 72 55 6e 64 6f 43 61 6c 6c 62 61 63 pagerUndoCallbac
16ed0 6b 2c 20 28 76 6f 69 64 20 2a 29 70 50 61 67 65 k, (void *)pPage
16ee0 72 29 3b 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 r);. pList = sq
16ef0 6c 69 74 65 33 50 63 61 63 68 65 44 69 72 74 79 lite3PcacheDirty
16f00 4c 69 73 74 28 70 50 61 67 65 72 2d 3e 70 50 43 List(pPager->pPC
16f10 61 63 68 65 29 3b 0a 20 20 77 68 69 6c 65 28 20 ache);. while(
16f20 70 4c 69 73 74 20 26 26 20 72 63 3d 3d 53 51 4c pList && rc==SQL
16f30 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 50 67 ITE_OK ){. Pg
16f40 48 64 72 20 2a 70 4e 65 78 74 20 3d 20 70 4c 69 Hdr *pNext = pLi
16f50 73 74 2d 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 st->pDirty;.
16f60 72 63 20 3d 20 70 61 67 65 72 55 6e 64 6f 43 61 rc = pagerUndoCa
16f70 6c 6c 62 61 63 6b 28 28 76 6f 69 64 20 2a 29 70 llback((void *)p
16f80 50 61 67 65 72 2c 20 70 4c 69 73 74 2d 3e 70 67 Pager, pList->pg
16f90 6e 6f 29 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d no);. pList =
16fa0 20 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20 20 72 pNext;. }.. r
16fb0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
16fc0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
16fd0 20 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72 is a wrapper ar
16fe0 6f 75 6e 64 20 73 71 6c 69 74 65 33 57 61 6c 46 ound sqlite3WalF
16ff0 72 61 6d 65 73 28 29 2e 20 41 73 20 77 65 6c 6c rames(). As well
17000 20 61 73 20 6c 6f 67 67 69 6e 67 0a 2a 2a 20 74 as logging.** t
17010 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
17020 68 65 20 6c 69 73 74 20 6f 66 20 70 61 67 65 73 he list of pages
17030 20 68 65 61 64 65 64 20 62 79 20 70 4c 69 73 74 headed by pList
17040 20 28 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 (connected by p
17050 44 69 72 74 79 29 2c 0a 2a 2a 20 74 68 69 73 20 Dirty),.** this
17060 66 75 6e 63 74 69 6f 6e 20 6e 6f 74 69 66 69 65 function notifie
17070 73 20 61 6e 79 20 61 63 74 69 76 65 20 62 61 63 s any active bac
17080 6b 75 70 20 70 72 6f 63 65 73 73 65 73 20 74 68 kup processes th
17090 61 74 20 74 68 65 20 70 61 67 65 73 20 68 61 76 at the pages hav
170a0 65 0a 2a 2a 20 63 68 61 6e 67 65 64 2e 20 0a 2a e.** changed. .*
170b0 2f 20 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 / .static int pa
170c0 67 65 72 57 61 6c 46 72 61 6d 65 73 28 0a 20 20 gerWalFrames(.
170d0 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 Pager *pPager,
170e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
170f0 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 /* Pager object
17100 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 4c 69 73 */. PgHdr *pLis
17110 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t,
17120 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 /* List of
17130 66 72 61 6d 65 73 20 74 6f 20 6c 6f 67 20 2a 2f frames to log */
17140 0a 20 20 50 67 6e 6f 20 6e 54 72 75 6e 63 61 74 . Pgno nTruncat
17150 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
17160 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 73 /* Database s
17170 69 7a 65 20 61 66 74 65 72 20 74 68 69 73 20 63 ize after this c
17180 6f 6d 6d 69 74 20 2a 2f 0a 20 20 69 6e 74 20 69 ommit */. int i
17190 73 43 6f 6d 6d 69 74 2c 20 20 20 20 20 20 20 20 sCommit,
171a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
171b0 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20 ue if this is a
171c0 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20 69 6e 74 20 commit */. int
171d0 73 79 6e 63 5f 66 6c 61 67 73 20 20 20 20 20 20 sync_flags
171e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
171f0 6c 61 67 73 20 74 6f 20 70 61 73 73 20 74 6f 20 lags to pass to
17200 4f 73 53 79 6e 63 28 29 20 28 6f 72 20 30 29 20 OsSync() (or 0)
17210 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 */.){. int rc;
17220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17230 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
17240 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 61 73 73 n code */.. ass
17250 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 57 61 ert( pPager->pWa
17260 6c 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 l );. rc = sqli
17270 74 65 33 57 61 6c 46 72 61 6d 65 73 28 70 50 61 te3WalFrames(pPa
17280 67 65 72 2d 3e 70 57 61 6c 2c 20 0a 20 20 20 20 ger->pWal, .
17290 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 pPager->pageSi
172a0 7a 65 2c 20 70 4c 69 73 74 2c 20 6e 54 72 75 6e ze, pList, nTrun
172b0 63 61 74 65 2c 20 69 73 43 6f 6d 6d 69 74 2c 20 cate, isCommit,
172c0 73 79 6e 63 5f 66 6c 61 67 73 0a 20 20 29 3b 0a sync_flags. );.
172d0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
172e0 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 70 _OK && pPager->p
172f0 42 61 63 6b 75 70 20 29 7b 0a 20 20 20 20 50 67 Backup ){. Pg
17300 48 64 72 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 Hdr *p;. for(
17310 70 3d 70 4c 69 73 74 3b 20 70 3b 20 70 3d 70 2d p=pList; p; p=p-
17320 3e 70 44 69 72 74 79 29 7b 0a 20 20 20 20 20 20 >pDirty){.
17330 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 sqlite3BackupUpd
17340 61 74 65 28 70 50 61 67 65 72 2d 3e 70 42 61 63 ate(pPager->pBac
17350 6b 75 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 28 75 kup, p->pgno, (u
17360 38 20 2a 29 70 2d 3e 70 44 61 74 61 29 3b 0a 20 8 *)p->pData);.
17370 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
17380 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 n rc;.}../*.** B
17390 65 67 69 6e 20 61 20 72 65 61 64 20 74 72 61 6e egin a read tran
173a0 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 57 saction on the W
173b0 41 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 AL..**.** This r
173c0 6f 75 74 69 6e 65 20 75 73 65 64 20 74 6f 20 62 outine used to b
173d0 65 20 63 61 6c 6c 65 64 20 22 70 61 67 65 72 4f e called "pagerO
173e0 70 65 6e 53 6e 61 70 73 68 6f 74 28 29 22 20 62 penSnapshot()" b
173f0 65 63 61 75 73 65 20 69 74 20 65 73 73 65 6e 74 ecause it essent
17400 69 61 6c 6c 79 0a 2a 2a 20 6d 61 6b 65 73 20 61 ially.** makes a
17410 20 73 6e 61 70 73 68 6f 74 20 6f 66 20 74 68 65 snapshot of the
17420 20 64 61 74 61 62 61 73 65 20 61 74 20 74 68 65 database at the
17430 20 63 75 72 72 65 6e 74 20 70 6f 69 6e 74 20 69 current point i
17440 6e 20 74 69 6d 65 20 61 6e 64 20 70 72 65 73 65 n time and prese
17450 72 76 65 73 0a 2a 2a 20 74 68 61 74 20 73 6e 61 rves.** that sna
17460 70 73 68 6f 74 20 66 6f 72 20 75 73 65 20 62 79 pshot for use by
17470 20 74 68 65 20 72 65 61 64 65 72 20 69 6e 20 73 the reader in s
17480 70 69 74 65 20 6f 66 20 63 6f 6e 63 75 72 72 65 pite of concurre
17490 6e 74 6c 79 20 63 68 61 6e 67 65 73 20 62 79 0a ntly changes by.
174a0 2a 2a 20 6f 74 68 65 72 20 77 72 69 74 65 72 73 ** other writers
174b0 20 6f 72 20 63 68 65 63 6b 70 6f 69 6e 74 65 72 or checkpointer
174c0 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
174d0 20 70 61 67 65 72 42 65 67 69 6e 52 65 61 64 54 pagerBeginReadT
174e0 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 67 65 72 ransaction(Pager
174f0 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 *pPager){. int
17500 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
17510 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17520 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
17530 20 69 6e 74 20 63 68 61 6e 67 65 64 20 3d 20 30 int changed = 0
17540 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
17550 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 63 68 /* True if cach
17560 65 20 6d 75 73 74 20 62 65 20 72 65 73 65 74 20 e must be reset
17570 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 61 */.. assert( pa
17580 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 gerUseWal(pPager
17590 29 20 29 3b 0a 0a 20 20 2f 2a 20 73 71 6c 69 74 ) );.. /* sqlit
175a0 65 33 57 61 6c 45 6e 64 52 65 61 64 54 72 61 6e e3WalEndReadTran
175b0 73 61 63 74 69 6f 6e 28 29 20 77 61 73 20 6e 6f saction() was no
175c0 74 20 63 61 6c 6c 65 64 20 66 6f 72 20 74 68 65 t called for the
175d0 20 70 72 65 76 69 6f 75 73 0a 20 20 2a 2a 20 74 previous. ** t
175e0 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 6c 6f ransaction in lo
175f0 63 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 cking_mode=EXCLU
17600 53 49 56 45 2e 20 20 53 6f 20 63 61 6c 6c 20 69 SIVE. So call i
17610 74 20 6e 6f 77 2e 20 20 49 66 20 77 65 0a 20 20 t now. If we.
17620 2a 2a 20 61 72 65 20 69 6e 20 6c 6f 63 6b 69 6e ** are in lockin
17630 67 5f 6d 6f 64 65 3d 4e 4f 52 4d 41 4c 20 61 6e g_mode=NORMAL an
17640 64 20 45 6e 64 52 65 61 64 28 29 20 77 61 73 20 d EndRead() was
17650 70 72 65 76 69 6f 75 73 6c 79 20 63 61 6c 6c 65 previously calle
17660 64 2c 0a 20 20 2a 2a 20 74 68 65 20 64 75 70 6c d,. ** the dupl
17670 69 63 61 74 65 20 63 61 6c 6c 20 69 73 20 68 61 icate call is ha
17680 72 6d 6c 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 rmless.. */. s
17690 71 6c 69 74 65 33 57 61 6c 45 6e 64 52 65 61 64 qlite3WalEndRead
176a0 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 Transaction(pPag
176b0 65 72 2d 3e 70 57 61 6c 29 3b 0a 0a 20 20 72 63 er->pWal);.. rc
176c0 20 3d 20 73 71 6c 69 74 65 33 57 61 6c 42 65 67 = sqlite3WalBeg
176d0 69 6e 52 65 61 64 54 72 61 6e 73 61 63 74 69 6f inReadTransactio
176e0 6e 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 n(pPager->pWal,
176f0 26 63 68 61 6e 67 65 64 29 3b 0a 20 20 69 66 28 &changed);. if(
17700 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
17710 7b 0a 20 20 20 20 69 6e 74 20 64 75 6d 6d 79 3b {. int dummy;
17720 0a 20 20 20 20 69 66 28 20 63 68 61 6e 67 65 64 . if( changed
17730 20 29 7b 0a 20 20 20 20 20 20 70 61 67 65 72 5f ){. pager_
17740 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 reset(pPager);.
17750 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
17760 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 7c 7c 20 ger->errCode ||
17770 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 pPager->dbSizeVa
17780 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 0a lid==0 );. }.
17790 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
177a0 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 PagerPagecount(p
177b0 50 61 67 65 72 2c 20 26 64 75 6d 6d 79 29 3b 0a Pager, &dummy);.
177c0 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73 74 }. pPager->st
177d0 61 74 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52 ate = PAGER_SHAR
177e0 45 44 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 ED;.. return rc
177f0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b ;.}../*.** Check
17800 20 69 66 20 74 68 65 20 2a 2d 77 61 6c 20 66 69 if the *-wal fi
17810 6c 65 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f le that correspo
17820 6e 64 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 nds to the datab
17830 61 73 65 20 6f 70 65 6e 65 64 20 62 79 20 70 50 ase opened by pP
17840 61 67 65 72 0a 2a 2a 20 65 78 69 73 74 73 2e 20 ager.** exists.
17850 41 73 73 75 6d 69 6e 67 20 6e 6f 20 65 72 72 6f Assuming no erro
17860 72 20 6f 63 63 75 72 73 2c 20 73 65 74 20 2a 70 r occurs, set *p
17870 45 78 69 73 74 73 20 74 6f 20 31 20 69 66 20 74 Exists to 1 if t
17880 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 2c 0a he file exists,.
17890 2a 2a 20 6f 72 20 30 20 6f 74 68 65 72 77 69 73 ** or 0 otherwis
178a0 65 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c e and return SQL
178b0 49 54 45 5f 4f 4b 2e 20 49 66 20 61 6e 20 49 4f ITE_OK. If an IO
178c0 20 6f 72 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 or OOM error oc
178d0 63 75 72 73 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 curs, return.**
178e0 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 an SQLite error
178f0 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 code..*/.static
17900 69 6e 74 20 70 61 67 65 72 48 61 73 57 41 4c 28 int pagerHasWAL(
17910 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
17920 6e 74 20 2a 70 45 78 69 73 74 73 29 7b 0a 20 20 nt *pExists){.
17930 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
17940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17950 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
17960 2f 0a 20 20 63 68 61 72 20 2a 7a 57 61 6c 3b 20 /. char *zWal;
17970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17980 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
17990 68 65 20 57 41 4c 20 66 69 6c 65 20 2a 2f 0a 0a he WAL file */..
179a0 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65 assert( !pPage
179b0 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 r->tempFile );.
179c0 20 7a 57 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f zWal = sqlite3_
179d0 6d 70 72 69 6e 74 66 28 22 25 73 2d 77 61 6c 22 mprintf("%s-wal"
179e0 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e , pPager->zFilen
179f0 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 7a 57 61 ame);. if( !zWa
17a00 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 l ){. rc = SQ
17a10 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 LITE_NOMEM;. }e
17a20 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 lse{. rc = sq
17a30 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 50 lite3OsAccess(pP
17a40 61 67 65 72 2d 3e 70 56 66 73 2c 20 7a 57 61 6c ager->pVfs, zWal
17a50 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f , SQLITE_ACCESS_
17a60 45 58 49 53 54 53 2c 20 70 45 78 69 73 74 73 29 EXISTS, pExists)
17a70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 ;. sqlite3_fr
17a80 65 65 28 7a 57 61 6c 29 3b 0a 20 20 7d 0a 20 20 ee(zWal);. }.
17a90 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
17aa0 0a 2a 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 .** Check if the
17ab0 20 2a 2d 77 61 6c 20 66 69 6c 65 20 74 68 61 74 *-wal file that
17ac0 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 corresponds to
17ad0 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 70 65 the database ope
17ae0 6e 65 64 20 62 79 20 70 50 61 67 65 72 0a 2a 2a ned by pPager.**
17af0 20 65 78 69 73 74 73 2e 20 49 66 20 69 74 20 64 exists. If it d
17b00 6f 65 73 2c 20 6f 70 65 6e 20 74 68 65 20 70 61 oes, open the pa
17b10 67 65 72 20 69 6e 20 57 41 4c 20 6d 6f 64 65 2e ger in WAL mode.
17b20 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 6e Otherwise, if n
17b30 6f 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 o error.** occur
17b40 73 2c 20 6d 61 6b 65 20 73 75 72 65 20 50 61 67 s, make sure Pag
17b50 65 72 2e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 69 er.journalMode i
17b60 73 20 6e 6f 74 20 73 65 74 20 74 6f 20 50 41 47 s not set to PAG
17b70 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 ER_JOURNALMODE_W
17b80 41 4c 2e 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 AL..** If an IO
17b90 6f 72 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 or OOM error occ
17ba0 75 72 73 2c 20 72 65 74 75 72 6e 20 61 6e 20 53 urs, return an S
17bb0 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 QLite error code
17bc0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 57 ..**.** If the W
17bd0 41 4c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 AL file is opene
17be0 64 2c 20 61 6c 73 6f 20 6f 70 65 6e 20 61 20 73 d, also open a s
17bf0 6e 61 70 73 68 6f 74 20 28 72 65 61 64 20 74 72 napshot (read tr
17c00 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a ansaction)..**.*
17c10 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 * The caller mus
17c20 74 20 68 6f 6c 64 20 61 20 53 48 41 52 45 44 20 t hold a SHARED
17c30 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
17c40 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 61 6c base file to cal
17c50 6c 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 l this.** functi
17c60 6f 6e 2e 20 42 65 63 61 75 73 65 20 61 6e 20 45 on. Because an E
17c70 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e XCLUSIVE lock on
17c80 20 74 68 65 20 64 62 20 66 69 6c 65 20 69 73 20 the db file is
17c90 72 65 71 75 69 72 65 64 20 74 6f 20 64 65 6c 65 required to dele
17ca0 74 65 20 0a 2a 2a 20 61 20 57 41 4c 2c 20 74 68 te .** a WAL, th
17cb0 69 73 20 65 6e 73 75 72 65 73 20 74 68 65 72 65 is ensures there
17cc0 20 69 73 20 6e 6f 20 72 61 63 65 20 63 6f 6e 64 is no race cond
17cd0 69 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 74 68 ition between th
17ce0 65 20 78 41 63 63 65 73 73 28 29 20 0a 2a 2a 20 e xAccess() .**
17cf0 62 65 6c 6f 77 20 61 6e 64 20 61 6e 20 78 44 65 below and an xDe
17d00 6c 65 74 65 28 29 20 62 65 69 6e 67 20 65 78 65 lete() being exe
17d10 63 75 74 65 64 20 62 79 20 73 6f 6d 65 20 6f 74 cuted by some ot
17d20 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a her connection..
17d30 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
17d40 67 65 72 4f 70 65 6e 57 61 6c 49 66 50 72 65 73 gerOpenWalIfPres
17d50 65 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 ent(Pager *pPage
17d60 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 r){. int rc = S
17d70 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
17d80 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c !pPager->tempFil
17d90 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 73 57 e ){. int isW
17da0 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 al;
17db0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
17dc0 66 20 57 41 4c 20 66 69 6c 65 20 65 78 69 73 74 f WAL file exist
17dd0 73 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 s */. rc = pa
17de0 67 65 72 48 61 73 57 41 4c 28 70 50 61 67 65 72 gerHasWAL(pPager
17df0 2c 20 26 69 73 57 61 6c 29 3b 0a 20 20 20 20 69 , &isWal);. i
17e00 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
17e10 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 ){. if( is
17e20 57 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 Wal ){. p
17e30 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 ager_reset(pPage
17e40 72 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d r);. rc =
17e50 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 sqlite3PagerOpe
17e60 6e 57 61 6c 28 70 50 61 67 65 72 2c 20 30 29 3b nWal(pPager, 0);
17e70 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d . if( rc=
17e80 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
17e90 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 rc = pag
17ea0 65 72 42 65 67 69 6e 52 65 61 64 54 72 61 6e 73 erBeginReadTrans
17eb0 61 63 74 69 6f 6e 28 70 50 61 67 65 72 29 3b 0a action(pPager);.
17ec0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
17ed0 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 }else if( pPager
17ee0 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
17ef0 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
17f00 5f 57 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 _WAL ){.
17f10 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
17f20 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 ode = PAGER_JOUR
17f30 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 3b 0a NALMODE_DELETE;.
17f40 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
17f50 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
17f60 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 .#endif../*.** P
17f70 6c 61 79 62 61 63 6b 20 73 61 76 65 70 6f 69 6e layback savepoin
17f80 74 20 70 53 61 76 65 70 6f 69 6e 74 2e 20 4f 72 t pSavepoint. Or
17f90 2c 20 69 66 20 70 53 61 76 65 70 6f 69 6e 74 3d , if pSavepoint=
17fa0 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 6c 61 79 =NULL, then play
17fb0 62 61 63 6b 0a 2a 2a 20 74 68 65 20 65 6e 74 69 back.** the enti
17fc0 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 re master journa
17fd0 6c 20 66 69 6c 65 2e 20 54 68 65 20 63 61 73 65 l file. The case
17fe0 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 4c pSavepoint==NUL
17ff0 4c 20 6f 63 63 75 72 73 20 77 68 65 6e 20 0a 2a L occurs when .*
18000 2a 20 61 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 * a ROLLBACK TO
18010 63 6f 6d 6d 61 6e 64 20 69 73 20 69 6e 76 6f 6b command is invok
18020 65 64 20 6f 6e 20 61 20 53 41 56 45 50 4f 49 4e ed on a SAVEPOIN
18030 54 20 74 68 61 74 20 69 73 20 61 20 74 72 61 6e T that is a tran
18040 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 73 61 76 65 saction .** save
18050 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 point..**.** Whe
18060 6e 20 70 53 61 76 65 70 6f 69 6e 74 20 69 73 20 n pSavepoint is
18070 6e 6f 74 20 4e 55 4c 4c 20 28 6d 65 61 6e 69 6e not NULL (meanin
18080 67 20 61 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 g a non-transact
18090 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 73 ion savepoint is
180a0 20 0a 2a 2a 20 62 65 69 6e 67 20 72 6f 6c 6c 65 .** being rolle
180b0 64 20 62 61 63 6b 29 2c 20 74 68 65 6e 20 74 68 d back), then th
180c0 65 20 72 6f 6c 6c 62 61 63 6b 20 63 6f 6e 73 69 e rollback consi
180d0 73 74 73 20 6f 66 20 75 70 20 74 6f 20 74 68 72 sts of up to thr
180e0 65 65 20 73 74 61 67 65 73 2c 0a 2a 2a 20 70 65 ee stages,.** pe
180f0 72 66 6f 72 6d 65 64 20 69 6e 20 74 68 65 20 6f rformed in the o
18100 72 64 65 72 20 73 70 65 63 69 66 69 65 64 3a 0a rder specified:.
18110 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 67 65 73 20 **.** * Pages
18120 61 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20 are played back
18130 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f from the main jo
18140 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61 urnal starting a
18150 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 20 6f 66 t byte.** of
18160 66 73 65 74 20 50 61 67 65 72 53 61 76 65 70 6f fset PagerSavepo
18170 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 20 int.iOffset and
18180 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 0a 2a continuing to .*
18190 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65 70 * PagerSavep
181a0 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 2c oint.iHdrOffset,
181b0 20 6f 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f or to the end o
181c0 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e f the main journ
181d0 61 6c 0a 2a 2a 20 20 20 20 20 66 69 6c 65 20 69 al.** file i
181e0 66 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 f PagerSavepoint
181f0 2e 69 48 64 72 4f 66 66 73 65 74 20 69 73 20 7a .iHdrOffset is z
18200 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 ero..**.** * I
18210 66 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 f PagerSavepoint
18220 2e 69 48 64 72 4f 66 66 73 65 74 20 69 73 20 6e .iHdrOffset is n
18230 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 70 61 ot zero, then pa
18240 67 65 73 20 61 72 65 20 70 6c 61 79 65 64 0a 2a ges are played.*
18250 2a 20 20 20 20 20 62 61 63 6b 20 73 74 61 72 74 * back start
18260 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 ing from the jou
18270 72 6e 61 6c 20 68 65 61 64 65 72 20 69 6d 6d 65 rnal header imme
18280 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e diately followin
18290 67 20 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 g .** PagerS
182a0 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 avepoint.iHdrOff
182b0 73 65 74 20 74 6f 20 74 68 65 20 65 6e 64 20 6f set to the end o
182c0 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e f the main journ
182d0 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 al file..**.**
182e0 20 2a 20 50 61 67 65 73 20 61 72 65 20 74 68 65 * Pages are the
182f0 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66 72 n played back fr
18300 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e om the sub-journ
18310 61 6c 20 66 69 6c 65 2c 20 73 74 61 72 74 69 6e al file, startin
18320 67 0a 2a 2a 20 20 20 20 20 77 69 74 68 20 74 68 g.** with th
18330 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 e PagerSavepoint
18340 2e 69 53 75 62 52 65 63 20 61 6e 64 20 63 6f 6e .iSubRec and con
18350 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 20 65 tinuing to the e
18360 6e 64 20 6f 66 0a 2a 2a 20 20 20 20 20 74 68 65 nd of.** the
18370 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a journal file..*
18380 2a 0a 2a 2a 20 54 68 72 6f 75 67 68 6f 75 74 20 *.** Throughout
18390 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 70 72 6f the rollback pro
183a0 63 65 73 73 2c 20 65 61 63 68 20 74 69 6d 65 20 cess, each time
183b0 61 20 70 61 67 65 20 69 73 20 72 6f 6c 6c 65 64 a page is rolled
183c0 20 62 61 63 6b 2c 20 74 68 65 0a 2a 2a 20 63 6f back, the.** co
183d0 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 rresponding bit
183e0 69 73 20 73 65 74 20 69 6e 20 61 20 62 69 74 76 is set in a bitv
183f0 65 63 20 73 74 72 75 63 74 75 72 65 20 28 76 61 ec structure (va
18400 72 69 61 62 6c 65 20 70 44 6f 6e 65 20 69 6e 20 riable pDone in
18410 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 the.** implement
18420 61 74 69 6f 6e 20 62 65 6c 6f 77 29 2e 20 54 68 ation below). Th
18430 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e is is used to en
18440 73 75 72 65 20 74 68 61 74 20 61 20 70 61 67 65 sure that a page
18450 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 72 6f 6c 6c is only.** roll
18460 65 64 20 62 61 63 6b 20 74 68 65 20 66 69 72 73 ed back the firs
18470 74 20 74 69 6d 65 20 69 74 20 69 73 20 65 6e 63 t time it is enc
18480 6f 75 6e 74 65 72 65 64 20 69 6e 20 65 69 74 68 ountered in eith
18490 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a er journal..**.*
184a0 2a 20 49 66 20 70 53 61 76 65 70 6f 69 6e 74 20 * If pSavepoint
184b0 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 61 is NULL, then pa
184c0 67 65 73 20 61 72 65 20 6f 6e 6c 79 20 70 6c 61 ges are only pla
184d0 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 yed back from th
184e0 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 e main.** journa
184f0 6c 20 66 69 6c 65 2e 20 54 68 65 72 65 20 69 73 l file. There is
18500 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 61 20 62 no need for a b
18510 69 74 76 65 63 20 69 6e 20 74 68 69 73 20 63 61 itvec in this ca
18520 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 69 74 se..**.** In eit
18530 68 65 72 20 63 61 73 65 2c 20 62 65 66 6f 72 65 her case, before
18540 20 70 6c 61 79 62 61 63 6b 20 63 6f 6d 6d 65 6e playback commen
18550 63 65 73 20 74 68 65 20 50 61 67 65 72 2e 64 62 ces the Pager.db
18560 53 69 7a 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a Size variable.**
18570 20 69 73 20 72 65 73 65 74 20 74 6f 20 74 68 65 is reset to the
18580 20 76 61 6c 75 65 20 74 68 61 74 20 69 74 20 68 value that it h
18590 65 6c 64 20 61 74 20 74 68 65 20 73 74 61 72 74 eld at the start
185a0 20 6f 66 20 74 68 65 20 73 61 76 65 70 6f 69 6e of the savepoin
185b0 74 20 0a 2a 2a 20 28 6f 72 20 74 72 61 6e 73 61 t .** (or transa
185c0 63 74 69 6f 6e 29 2e 20 4e 6f 20 70 61 67 65 20 ction). No page
185d0 77 69 74 68 20 61 20 70 61 67 65 2d 6e 75 6d 62 with a page-numb
185e0 65 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 er greater than
185f0 74 68 69 73 20 76 61 6c 75 65 0a 2a 2a 20 69 73 this value.** is
18600 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 49 66 played back. If
18610 20 6f 6e 65 20 69 73 20 65 6e 63 6f 75 6e 74 65 one is encounte
18620 72 65 64 20 69 74 20 69 73 20 73 69 6d 70 6c 79 red it is simply
18630 20 73 6b 69 70 70 65 64 2e 0a 2a 2f 0a 73 74 61 skipped..*/.sta
18640 74 69 63 20 69 6e 74 20 70 61 67 65 72 50 6c 61 tic int pagerPla
18650 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28 50 ybackSavepoint(P
18660 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 61 ager *pPager, Pa
18670 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 gerSavepoint *pS
18680 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 36 34 avepoint){. i64
18690 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 szJ;
186a0 20 20 20 20 20 20 2f 2a 20 45 66 66 65 63 74 69 /* Effecti
186b0 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6d ve size of the m
186c0 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 ain journal */.
186d0 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20 20 i64 iHdrOff;
186e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 /* End
186f0 20 6f 66 20 66 69 72 73 74 20 73 65 67 6d 65 6e of first segmen
18700 74 20 6f 66 20 6d 61 69 6e 2d 6a 6f 75 72 6e 61 t of main-journa
18710 6c 20 72 65 63 6f 72 64 73 20 2a 2f 0a 20 20 69 l records */. i
18720 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
18730 4b 3b 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 K; /* Retur
18740 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 69 74 76 n code */. Bitv
18750 65 63 20 2a 70 44 6f 6e 65 20 3d 20 30 3b 20 20 ec *pDone = 0;
18760 20 20 20 20 20 2f 2a 20 42 69 74 76 65 63 20 74 /* Bitvec t
18770 6f 20 65 6e 73 75 72 65 20 70 61 67 65 73 20 70 o ensure pages p
18780 6c 61 79 65 64 20 62 61 63 6b 20 6f 6e 6c 79 20 layed back only
18790 6f 6e 63 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 once */.. asser
187a0 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 t( pPager->state
187b0 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 >=PAGER_SHARED )
187c0 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 ;.. /* Allocate
187d0 20 61 20 62 69 74 76 65 63 20 74 6f 20 75 73 65 a bitvec to use
187e0 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 65 to store the se
187f0 74 20 6f 66 20 70 61 67 65 73 20 72 6f 6c 6c 65 t of pages rolle
18800 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 66 28 20 d back */. if(
18810 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 pSavepoint ){.
18820 20 20 70 44 6f 6e 65 20 3d 20 73 71 6c 69 74 65 pDone = sqlite
18830 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70 53 3BitvecCreate(pS
18840 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67 29 avepoint->nOrig)
18850 3b 0a 20 20 20 20 69 66 28 20 21 70 44 6f 6e 65 ;. if( !pDone
18860 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
18870 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
18880 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 }. }.. /* S
18890 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 et the database
188a0 73 69 7a 65 20 62 61 63 6b 20 74 6f 20 74 68 65 size back to the
188b0 20 76 61 6c 75 65 20 69 74 20 77 61 73 20 62 65 value it was be
188c0 66 6f 72 65 20 74 68 65 20 73 61 76 65 70 6f 69 fore the savepoi
188d0 6e 74 20 0a 20 20 2a 2a 20 62 65 69 6e 67 20 72 nt . ** being r
188e0 65 76 65 72 74 65 64 20 77 61 73 20 6f 70 65 6e everted was open
188f0 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 ed.. */. pPage
18900 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 53 61 76 r->dbSize = pSav
18910 65 70 6f 69 6e 74 20 3f 20 70 53 61 76 65 70 6f epoint ? pSavepo
18920 69 6e 74 2d 3e 6e 4f 72 69 67 20 3a 20 70 50 61 int->nOrig : pPa
18930 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b ger->dbOrigSize;
18940 0a 0a 20 20 69 66 28 20 21 70 53 61 76 65 70 6f .. if( !pSavepo
18950 69 6e 74 20 26 26 20 70 61 67 65 72 55 73 65 57 int && pagerUseW
18960 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 al(pPager) ){.
18970 20 20 72 65 74 75 72 6e 20 70 61 67 65 72 52 6f return pagerRo
18980 6c 6c 62 61 63 6b 57 61 6c 28 70 50 61 67 65 72 llbackWal(pPager
18990 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 73 65 );. }.. /* Use
189a0 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
189b0 4f 66 66 20 61 73 20 74 68 65 20 65 66 66 65 63 Off as the effec
189c0 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 tive size of the
189d0 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a 20 main rollback.
189e0 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 ** journal. Th
189f0 65 20 61 63 74 75 61 6c 20 66 69 6c 65 20 6d 69 e actual file mi
18a00 67 68 74 20 62 65 20 6c 61 72 67 65 72 20 74 68 ght be larger th
18a10 61 6e 20 74 68 69 73 20 69 6e 0a 20 20 2a 2a 20 an this in. **
18a20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
18a30 45 5f 54 52 55 4e 43 41 54 45 20 6f 72 20 50 41 E_TRUNCATE or PA
18a40 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
18a50 50 45 52 53 49 53 54 2e 20 20 42 75 74 20 61 6e PERSIST. But an
18a60 79 74 68 69 6e 67 0a 20 20 2a 2a 20 70 61 73 74 ything. ** past
18a70 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
18a80 4f 66 66 20 69 73 20 6f 66 66 2d 6c 69 6d 69 74 Off is off-limit
18a90 73 20 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a 20 20 s to us.. */.
18aa0 73 7a 4a 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f szJ = pPager->jo
18ab0 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 61 73 73 65 urnalOff;. asse
18ac0 72 74 28 20 70 61 67 65 72 55 73 65 57 61 6c 28 rt( pagerUseWal(
18ad0 70 50 61 67 65 72 29 3d 3d 30 20 7c 7c 20 73 7a pPager)==0 || sz
18ae0 4a 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 42 65 J==0 );.. /* Be
18af0 67 69 6e 20 62 79 20 72 6f 6c 6c 69 6e 67 20 62 gin by rolling b
18b00 61 63 6b 20 72 65 63 6f 72 64 73 20 66 72 6f 6d ack records from
18b10 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 the main journa
18b20 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a 20 20 l starting at.
18b30 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e ** PagerSavepoin
18b40 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 20 63 6f t.iOffset and co
18b50 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 20 ntinuing to the
18b60 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 next journal hea
18b70 64 65 72 2e 0a 20 20 2a 2a 20 54 68 65 72 65 20 der.. ** There
18b80 6d 69 67 68 74 20 62 65 20 72 65 63 6f 72 64 73 might be records
18b90 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 in the main jou
18ba0 72 6e 61 6c 20 74 68 61 74 20 68 61 76 65 20 61 rnal that have a
18bb0 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a page number. *
18bc0 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 * greater than t
18bd0 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 62 he current datab
18be0 61 73 65 20 73 69 7a 65 20 28 70 50 61 67 65 72 ase size (pPager
18bf0 2d 3e 64 62 53 69 7a 65 29 20 62 75 74 20 74 68 ->dbSize) but th
18c00 6f 73 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 ose. ** will be
18c10 20 73 6b 69 70 70 65 64 20 61 75 74 6f 6d 61 74 skipped automat
18c20 69 63 61 6c 6c 79 2e 20 20 50 61 67 65 73 20 61 ically. Pages a
18c30 72 65 20 61 64 64 65 64 20 74 6f 20 70 44 6f 6e re added to pDon
18c40 65 20 61 73 20 74 68 65 79 0a 20 20 2a 2a 20 61 e as they. ** a
18c50 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a re played back..
18c60 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65 */. if( pSave
18c70 70 6f 69 6e 74 20 26 26 20 21 70 61 67 65 72 55 point && !pagerU
18c80 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 7b seWal(pPager) ){
18c90 0a 20 20 20 20 69 48 64 72 4f 66 66 20 3d 20 70 . iHdrOff = p
18ca0 53 61 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72 4f Savepoint->iHdrO
18cb0 66 66 73 65 74 20 3f 20 70 53 61 76 65 70 6f 69 ffset ? pSavepoi
18cc0 6e 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 20 3a nt->iHdrOffset :
18cd0 20 73 7a 4a 3b 0a 20 20 20 20 70 50 61 67 65 72 szJ;. pPager
18ce0 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 70 ->journalOff = p
18cf0 53 61 76 65 70 6f 69 6e 74 2d 3e 69 4f 66 66 73 Savepoint->iOffs
18d00 65 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 72 et;. while( r
18d10 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
18d20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
18d30 66 66 3c 69 48 64 72 4f 66 66 20 29 7b 0a 20 20 ff<iHdrOff ){.
18d40 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 rc = pager_p
18d50 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 layback_one_page
18d60 28 70 50 61 67 65 72 2c 20 26 70 50 61 67 65 72 (pPager, &pPager
18d70 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 44 ->journalOff, pD
18d80 6f 6e 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 one, 1, 1);.
18d90 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 }. assert( rc
18da0 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b !=SQLITE_DONE );
18db0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 . }else{. pP
18dc0 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
18dd0 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 = 0;. }.. /*
18de0 43 6f 6e 74 69 6e 75 65 20 72 6f 6c 6c 69 6e 67 Continue rolling
18df0 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20 6f 75 back records ou
18e00 74 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f t of the main jo
18e10 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61 urnal starting a
18e20 74 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 t. ** the first
18e30 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
18e40 73 65 65 6e 20 61 6e 64 20 63 6f 6e 74 69 6e 75 seen and continu
18e50 69 6e 67 20 75 6e 74 69 6c 20 74 68 65 20 65 66 ing until the ef
18e60 66 65 63 74 69 76 65 20 65 6e 64 0a 20 20 2a 2a fective end. **
18e70 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 of the main jou
18e80 72 6e 61 6c 20 66 69 6c 65 2e 20 20 43 6f 6e 74 rnal file. Cont
18e90 69 6e 75 65 20 74 6f 20 73 6b 69 70 20 6f 75 74 inue to skip out
18ea0 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 73 20 -of-range pages
18eb0 61 6e 64 0a 20 20 2a 2a 20 63 6f 6e 74 69 6e 75 and. ** continu
18ec0 65 20 61 64 64 69 6e 67 20 70 61 67 65 73 20 72 e adding pages r
18ed0 6f 6c 6c 65 64 20 62 61 63 6b 20 74 6f 20 70 44 olled back to pD
18ee0 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c one.. */. whil
18ef0 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b e( rc==SQLITE_OK
18f00 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 && pPager->jour
18f10 6e 61 6c 4f 66 66 3c 73 7a 4a 20 29 7b 0a 20 20 nalOff<szJ ){.
18f20 20 20 75 33 32 20 69 69 3b 20 20 20 20 20 20 20 u32 ii;
18f30 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
18f40 6e 74 65 72 20 2a 2f 0a 20 20 20 20 75 33 32 20 nter */. u32
18f50 6e 4a 52 65 63 20 3d 20 30 3b 20 20 20 20 20 2f nJRec = 0; /
18f60 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4a 6f 75 72 * Number of Jour
18f70 6e 61 6c 20 52 65 63 6f 72 64 73 20 2a 2f 0a 20 nal Records */.
18f80 20 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a 20 20 u32 dummy;.
18f90 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e rc = readJourn
18fa0 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 30 2c alHdr(pPager, 0,
18fb0 20 73 7a 4a 2c 20 26 6e 4a 52 65 63 2c 20 26 64 szJ, &nJRec, &d
18fc0 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72 ummy);. asser
18fd0 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f t( rc!=SQLITE_DO
18fe0 4e 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 0a 20 20 NE );.. /*.
18ff0 20 20 2a 2a 20 54 68 65 20 22 70 50 61 67 65 72 ** The "pPager
19000 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 ->journalHdr+JOU
19010 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
19020 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 er)==pPager->jou
19030 72 6e 61 6c 4f 66 66 22 0a 20 20 20 20 2a 2a 20 rnalOff". **
19040 74 65 73 74 20 69 73 20 72 65 6c 61 74 65 64 20 test is related
19050 74 6f 20 74 69 63 6b 65 74 20 23 32 35 36 35 2e to ticket #2565.
19060 20 20 53 65 65 20 74 68 65 20 64 69 73 63 75 73 See the discus
19070 73 69 6f 6e 20 69 6e 20 74 68 65 0a 20 20 20 20 sion in the.
19080 2a 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 ** pager_playbac
19090 6b 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 k() function for
190a0 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f additional info
190b0 72 6d 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a rmation.. */.
190c0 20 20 20 20 69 66 28 20 6e 4a 52 65 63 3d 3d 30 if( nJRec==0
190d0 20 0a 20 20 20 20 20 26 26 20 70 50 61 67 65 72 . && pPager
190e0 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 ->journalHdr+JOU
190f0 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
19100 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 er)==pPager->jou
19110 72 6e 61 6c 4f 66 66 0a 20 20 20 20 29 7b 0a 20 rnalOff. ){.
19120 20 20 20 20 20 6e 4a 52 65 63 20 3d 20 28 75 33 nJRec = (u3
19130 32 29 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2)((szJ - pPager
19140 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 2f 4a 4f ->journalOff)/JO
19150 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 URNAL_PG_SZ(pPag
19160 65 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 er));. }.
19170 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 for(ii=0; rc==SQ
19180 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 4a LITE_OK && ii<nJ
19190 52 65 63 20 26 26 20 70 50 61 67 65 72 2d 3e 6a Rec && pPager->j
191a0 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 3b 20 69 ournalOff<szJ; i
191b0 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63 20 3d i++){. rc =
191c0 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f pager_playback_
191d0 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c one_page(pPager,
191e0 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 &pPager->journa
191f0 6c 4f 66 66 2c 20 70 44 6f 6e 65 2c 20 31 2c 20 lOff, pDone, 1,
19200 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 1);. }. as
19210 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
19220 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 20 20 61 _DONE );. }. a
19230 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
19240 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e E_OK || pPager->
19250 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 73 7a 4a 20 journalOff==szJ
19260 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 );.. /* Finally
19270 2c 20 20 72 6f 6c 6c 62 61 63 6b 20 70 61 67 65 , rollback page
19280 73 20 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a s from the sub-j
19290 6f 75 72 6e 61 6c 2e 20 20 50 61 67 65 20 74 68 ournal. Page th
192a0 61 74 20 77 65 72 65 0a 20 20 2a 2a 20 70 72 65 at were. ** pre
192b0 76 69 6f 75 73 6c 79 20 72 6f 6c 6c 65 64 20 62 viously rolled b
192c0 61 63 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6d ack out of the m
192d0 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 ain journal (and
192e0 20 61 72 65 20 68 65 6e 63 65 20 69 6e 20 70 44 are hence in pD
192f0 6f 6e 65 29 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 one). ** will b
19300 65 20 73 6b 69 70 70 65 64 2e 20 20 4f 75 74 2d e skipped. Out-
19310 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 73 20 61 of-range pages a
19320 72 65 20 61 6c 73 6f 20 73 6b 69 70 70 65 64 2e re also skipped.
19330 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 . */. if( pSav
19340 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 75 33 epoint ){. u3
19350 32 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 2 ii;
19360 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
19370 20 2a 2f 0a 20 20 20 20 69 36 34 20 6f 66 66 73 */. i64 offs
19380 65 74 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d et = pSavepoint-
19390 3e 69 53 75 62 52 65 63 2a 28 34 2b 70 50 61 67 >iSubRec*(4+pPag
193a0 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a er->pageSize);..
193b0 20 20 20 20 69 66 28 20 70 61 67 65 72 55 73 65 if( pagerUse
193c0 57 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 Wal(pPager) ){.
193d0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
193e0 33 57 61 6c 53 61 76 65 70 6f 69 6e 74 55 6e 64 3WalSavepointUnd
193f0 6f 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 o(pPager->pWal,
19400 70 53 61 76 65 70 6f 69 6e 74 2d 3e 61 57 61 6c pSavepoint->aWal
19410 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Data);. }.
19420 20 66 6f 72 28 69 69 3d 70 53 61 76 65 70 6f 69 for(ii=pSavepoi
19430 6e 74 2d 3e 69 53 75 62 52 65 63 3b 20 72 63 3d nt->iSubRec; rc=
19440 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 =SQLITE_OK && ii
19450 3c 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 <pPager->nSubRec
19460 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 ; ii++){. a
19470 73 73 65 72 74 28 20 6f 66 66 73 65 74 3d 3d 69 ssert( offset==i
19480 69 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 i*(4+pPager->pag
19490 65 53 69 7a 65 29 20 29 3b 0a 20 20 20 20 20 20 eSize) );.
194a0 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 rc = pager_playb
194b0 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 ack_one_page(pPa
194c0 67 65 72 2c 20 26 6f 66 66 73 65 74 2c 20 70 44 ger, &offset, pD
194d0 6f 6e 65 2c 20 30 2c 20 31 29 3b 0a 20 20 20 20 one, 0, 1);.
194e0 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 }. assert( rc
194f0 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b !=SQLITE_DONE );
19500 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 42 . }.. sqlite3B
19510 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 44 6f itvecDestroy(pDo
19520 6e 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 ne);. if( rc==S
19530 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
19540 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
19550 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 7d 0a 0a 20 ff = szJ;. }..
19560 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
19570 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
19580 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
19590 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 f in-memory page
195a0 73 20 74 68 61 74 20 61 72 65 20 61 6c 6c 6f 77 s that are allow
195b0 65 64 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 ed..*/.void sqli
195c0 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65 te3PagerSetCache
195d0 73 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 size(Pager *pPag
195e0 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b er, int mxPage){
195f0 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 . sqlite3Pcache
19600 53 65 74 43 61 63 68 65 73 69 7a 65 28 70 50 61 SetCachesize(pPa
19610 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 6d 78 ger->pPCache, mx
19620 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Page);.}../*.**
19630 41 64 6a 75 73 74 20 74 68 65 20 72 6f 62 75 73 Adjust the robus
19640 74 6e 65 73 73 20 6f 66 20 74 68 65 20 64 61 74 tness of the dat
19650 61 62 61 73 65 20 74 6f 20 64 61 6d 61 67 65 20 abase to damage
19660 64 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 due to OS crashe
19670 73 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 61 s.** or power fa
19680 69 6c 75 72 65 73 20 62 79 20 63 68 61 6e 67 69 ilures by changi
19690 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ng the number of
196a0 20 73 79 6e 63 73 28 29 73 20 77 68 65 6e 20 77 syncs()s when w
196b0 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 72 6f riting.** the ro
196c0 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 llback journal.
196d0 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65 There are three
196e0 20 6c 65 76 65 6c 73 3a 0a 2a 2a 0a 2a 2a 20 20 levels:.**.**
196f0 20 20 4f 46 46 20 20 20 20 20 20 20 73 71 6c 69 OFF sqli
19700 74 65 33 4f 73 53 79 6e 63 28 29 20 69 73 20 6e te3OsSync() is n
19710 65 76 65 72 20 63 61 6c 6c 65 64 2e 20 20 54 68 ever called. Th
19720 69 73 20 69 73 20 74 68 65 20 64 65 66 61 75 6c is is the defaul
19730 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
19740 20 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 for temporary
19750 61 6e 64 20 74 72 61 6e 73 69 65 6e 74 20 66 69 and transient fi
19760 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 4f les..**.** NO
19770 52 4d 41 4c 20 20 20 20 54 68 65 20 6a 6f 75 72 RMAL The jour
19780 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 6f 6e nal is synced on
19790 63 65 20 62 65 66 6f 72 65 20 77 72 69 74 65 73 ce before writes
197a0 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a begin on the.**
197b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61 da
197c0 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 69 73 tabase. This is
197d0 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 65 71 75 61 normally adequa
197e0 74 65 20 70 72 6f 74 65 63 74 69 6f 6e 2c 20 62 te protection, b
197f0 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ut.**
19800 20 20 20 69 74 20 69 73 20 74 68 65 6f 72 65 74 it is theoret
19810 69 63 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 2c ically possible,
19820 20 74 68 6f 75 67 68 20 76 65 72 79 20 75 6e 6c though very unl
19830 69 6b 65 6c 79 2c 0a 2a 2a 20 20 20 20 20 20 20 ikely,.**
19840 20 20 20 20 20 20 20 74 68 61 74 20 61 6e 20 69 that an i
19850 6e 6f 70 65 72 74 75 6e 65 20 70 6f 77 65 72 20 nopertune power
19860 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c 65 failure could le
19870 61 76 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a ave the journal.
19880 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
19890 69 6e 20 61 20 73 74 61 74 65 20 77 68 69 63 68 in a state which
198a0 20 77 6f 75 6c 64 20 63 61 75 73 65 20 64 61 6d would cause dam
198b0 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 age to the datab
198c0 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ase.**
198d0 20 20 20 20 77 68 65 6e 20 69 74 20 69 73 20 72 when it is r
198e0 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a olled back..**.*
198f0 2a 20 20 20 20 46 55 4c 4c 20 20 20 20 20 20 54 * FULL T
19900 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 he journal is sy
19910 6e 63 65 64 20 74 77 69 63 65 20 62 65 66 6f 72 nced twice befor
19920 65 20 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f e writes begin o
19930 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 n the.**
19940 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 28 database (
19950 77 69 74 68 20 73 6f 6d 65 20 61 64 64 69 74 69 with some additi
19960 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
19970 20 2d 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c - the nRec fiel
19980 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 d.**
19990 20 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c of the journal
199a0 20 68 65 61 64 65 72 20 2d 20 62 65 69 6e 67 20 header - being
199b0 77 72 69 74 74 65 6e 20 69 6e 20 62 65 74 77 65 written in betwe
199c0 65 6e 20 74 68 65 20 74 77 6f 0a 2a 2a 20 20 20 en the two.**
199d0 20 20 20 20 20 20 20 20 20 20 20 73 79 6e 63 73 syncs
199e0 29 2e 20 20 49 66 20 77 65 20 61 73 73 75 6d 65 ). If we assume
199f0 20 74 68 61 74 20 77 72 69 74 69 6e 67 20 61 0a that writing a.
19a00 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
19a10 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 single disk sect
19a20 6f 72 20 69 73 20 61 74 6f 6d 69 63 2c 20 74 68 or is atomic, th
19a30 65 6e 20 74 68 69 73 20 6d 6f 64 65 20 70 72 6f en this mode pro
19a40 76 69 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 vides.**
19a50 20 20 20 20 20 20 61 73 73 75 72 61 6e 63 65 20 assurance
19a60 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c that the journal
19a70 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f 72 will not be cor
19a80 72 75 70 74 65 64 20 74 6f 20 74 68 65 0a 2a 2a rupted to the.**
19a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6f po
19aa0 69 6e 74 20 6f 66 20 63 61 75 73 69 6e 67 20 64 int of causing d
19ab0 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 amage to the dat
19ac0 61 62 61 73 65 20 64 75 72 69 6e 67 20 72 6f 6c abase during rol
19ad0 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4e 75 6d lback..**.** Num
19ae0 65 72 69 63 20 76 61 6c 75 65 73 20 61 73 73 6f eric values asso
19af0 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 73 ciated with thes
19b00 65 20 73 74 61 74 65 73 20 61 72 65 20 4f 46 46 e states are OFF
19b10 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c 0a 2a ==1, NORMAL=2,.*
19b20 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a 2a 2f * and FULL=3..*/
19b30 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
19b40 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d OMIT_PAGER_PRAGM
19b50 41 53 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 50 AS.void sqlite3P
19b60 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 65 76 agerSetSafetyLev
19b70 65 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 el(Pager *pPager
19b80 2c 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 , int level, int
19b90 20 62 46 75 6c 6c 46 73 79 6e 63 29 7b 0a 20 20 bFullFsync){.
19ba0 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d pPager->noSync =
19bb0 20 20 28 6c 65 76 65 6c 3d 3d 31 20 7c 7c 20 70 (level==1 || p
19bc0 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 Pager->tempFile)
19bd0 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d ?1:0;. pPager-
19be0 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 28 6c 65 76 >fullSync = (lev
19bf0 65 6c 3d 3d 33 20 26 26 20 21 70 50 61 67 65 72 el==3 && !pPager
19c00 2d 3e 74 65 6d 70 46 69 6c 65 29 20 3f 31 3a 30 ->tempFile) ?1:0
19c10 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 ;. pPager->sync
19c20 5f 66 6c 61 67 73 20 3d 20 28 62 46 75 6c 6c 46 _flags = (bFullF
19c30 73 79 6e 63 3f 53 51 4c 49 54 45 5f 53 59 4e 43 sync?SQLITE_SYNC
19c40 5f 46 55 4c 4c 3a 53 51 4c 49 54 45 5f 53 59 4e _FULL:SQLITE_SYN
19c50 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 69 66 28 C_NORMAL);. if(
19c60 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 pPager->noSync
19c70 29 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 ) pPager->needSy
19c80 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 nc = 0;.}.#endif
19c90 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
19ca0 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 owing global var
19cb0 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 iable is increme
19cc0 6e 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 nted whenever th
19cd0 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 61 74 74 e library.** att
19ce0 65 6d 70 74 73 20 74 6f 20 6f 70 65 6e 20 61 20 empts to open a
19cf0 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 temporary file.
19d00 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f This informatio
19d10 6e 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a n is used for.**
19d20 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 testing and ana
19d30 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a 2f lysis only. .*/
19d40 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
19d50 45 53 54 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f EST.int sqlite3_
19d60 6f 70 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 20 3d opentemp_count =
19d70 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 0;.#endif../*.*
19d80 2a 20 4f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 * Open a tempora
19d90 72 79 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 ry file..**.** W
19da0 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 64 65 rite the file de
19db0 73 63 72 69 70 74 6f 72 20 69 6e 74 6f 20 2a 70 scriptor into *p
19dc0 46 69 6c 65 2e 20 52 65 74 75 72 6e 20 53 51 4c File. Return SQL
19dd0 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 ITE_OK on succes
19de0 73 20 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 s .** or some ot
19df0 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 69 her error code i
19e00 66 20 77 65 20 66 61 69 6c 2e 20 54 68 65 20 4f f we fail. The O
19e10 53 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 S will automatic
19e20 61 6c 6c 79 20 0a 2a 2a 20 64 65 6c 65 74 65 20 ally .** delete
19e30 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69 the temporary fi
19e40 6c 65 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c le when it is cl
19e50 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 osed..**.** The
19e60 66 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f 20 flags passed to
19e70 74 68 65 20 56 46 53 20 6c 61 79 65 72 20 78 4f the VFS layer xO
19e80 70 65 6e 28 29 20 63 61 6c 6c 20 61 72 65 20 74 pen() call are t
19e90 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 0a 2a hose specified.*
19ea0 2a 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 76 * by parameter v
19eb0 66 73 46 6c 61 67 73 20 4f 52 65 64 20 77 69 74 fsFlags ORed wit
19ec0 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a h the following:
19ed0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 .**.** SQLIT
19ee0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
19ef0 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f .** SQLITE_O
19f00 50 45 4e 5f 43 52 45 41 54 45 0a 2a 2a 20 20 20 PEN_CREATE.**
19f10 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 SQLITE_OPEN_EX
19f20 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 53 CLUSIVE.** S
19f30 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 QLITE_OPEN_DELET
19f40 45 4f 4e 43 4c 4f 53 45 0a 2a 2f 0a 73 74 61 74 EONCLOSE.*/.stat
19f50 69 63 20 69 6e 74 20 70 61 67 65 72 4f 70 65 6e ic int pagerOpen
19f60 74 65 6d 70 28 0a 20 20 50 61 67 65 72 20 2a 70 temp(. Pager *p
19f70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 2f 2a Pager, /*
19f80 20 54 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 The pager objec
19f90 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 t */. sqlite3_f
19fa0 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 2f 2a 20 ile *pFile, /*
19fb0 57 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 64 Write the file d
19fc0 65 73 63 72 69 70 74 6f 72 20 68 65 72 65 20 2a escriptor here *
19fd0 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 /. int vfsFlags
19fe0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 /* Fla
19ff0 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 gs passed throug
1a000 68 20 74 6f 20 74 68 65 20 56 46 53 20 2a 2f 0a h to the VFS */.
1a010 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
1a020 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
1a030 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 23 69 turn code */..#i
1a040 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
1a050 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 . sqlite3_opent
1a060 65 6d 70 5f 63 6f 75 6e 74 2b 2b 3b 20 20 2f 2a emp_count++; /*
1a070 20 55 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e Used for testin
1a080 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f g and analysis o
1a090 6e 6c 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 nly */.#endif..
1a0a0 20 76 66 73 46 6c 61 67 73 20 7c 3d 20 20 53 51 vfsFlags |= SQ
1a0b0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
1a0c0 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 ITE | SQLITE_OPE
1a0d0 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20 20 20 20 N_CREATE |.
1a0e0 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 SQLITE_OP
1a0f0 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 20 53 EN_EXCLUSIVE | S
1a100 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 QLITE_OPEN_DELET
1a110 45 4f 4e 43 4c 4f 53 45 3b 0a 20 20 72 63 20 3d EONCLOSE;. rc =
1a120 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 sqlite3OsOpen(p
1a130 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 30 2c 20 Pager->pVfs, 0,
1a140 70 46 69 6c 65 2c 20 76 66 73 46 6c 61 67 73 2c pFile, vfsFlags,
1a150 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 0);. assert( r
1a160 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c!=SQLITE_OK ||
1a170 69 73 4f 70 65 6e 28 70 46 69 6c 65 29 20 29 3b isOpen(pFile) );
1a180 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1a190 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 ./*.** Set the b
1a1a0 75 73 79 20 68 61 6e 64 6c 65 72 20 66 75 6e 63 usy handler func
1a1b0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tion..**.** The
1a1c0 70 61 67 65 72 20 69 6e 76 6f 6b 65 73 20 74 68 pager invokes th
1a1d0 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 e busy-handler i
1a1e0 66 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 f sqlite3OsLock(
1a1f0 29 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53 51 ) returns .** SQ
1a200 4c 49 54 45 5f 42 55 53 59 20 77 68 65 6e 20 74 LITE_BUSY when t
1a210 72 79 69 6e 67 20 74 6f 20 75 70 67 72 61 64 65 rying to upgrade
1a220 20 66 72 6f 6d 20 6e 6f 2d 6c 6f 63 6b 20 74 6f from no-lock to
1a230 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 0a a SHARED lock,.
1a240 2a 2a 20 6f 72 20 77 68 65 6e 20 74 72 79 69 6e ** or when tryin
1a250 67 20 74 6f 20 75 70 67 72 61 64 65 20 66 72 6f g to upgrade fro
1a260 6d 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 m a RESERVED loc
1a270 6b 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 k to an EXCLUSIV
1a280 45 20 0a 2a 2a 20 6c 6f 63 6b 2e 20 49 74 20 64 E .** lock. It d
1a290 6f 65 73 20 2a 6e 6f 74 2a 20 69 6e 76 6f 6b 65 oes *not* invoke
1a2a0 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
1a2b0 72 20 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67 r when upgrading
1a2c0 20 66 72 6f 6d 0a 2a 2a 20 53 48 41 52 45 44 20 from.** SHARED
1a2d0 74 6f 20 52 45 53 45 52 56 45 44 2c 20 6f 72 20 to RESERVED, or
1a2e0 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67 20 66 when upgrading f
1a2f0 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 45 58 rom SHARED to EX
1a300 43 4c 55 53 49 56 45 0a 2a 2a 20 28 77 68 69 63 CLUSIVE.** (whic
1a310 68 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 h occurs during
1a320 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c hot-journal roll
1a330 62 61 63 6b 29 2e 20 53 75 6d 6d 61 72 79 3a 0a back). Summary:.
1a340 2a 2a 0a 2a 2a 20 20 20 54 72 61 6e 73 69 74 69 **.** Transiti
1a350 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 on
1a360 20 20 20 20 20 20 20 20 20 20 7c 20 49 6e 76 6f | Invo
1a370 6b 65 73 20 78 42 75 73 79 48 61 6e 64 6c 65 72 kes xBusyHandler
1a380 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d .** ----------
1a390 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1a3a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1a3b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a --------------.*
1a3c0 2a 20 20 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 * NO_LOCK
1a3d0 20 20 2d 3e 20 53 48 41 52 45 44 5f 4c 4f 43 4b -> SHARED_LOCK
1a3e0 20 20 20 20 20 20 7c 20 59 65 73 0a 2a 2a 20 20 | Yes.**
1a3f0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d SHARED_LOCK -
1a400 3e 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 > RESERVED_LOCK
1a410 20 20 20 7c 20 4e 6f 0a 2a 2a 20 20 20 53 48 41 | No.** SHA
1a420 52 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 45 58 RED_LOCK -> EX
1a430 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c CLUSIVE_LOCK |
1a440 20 4e 6f 0a 2a 2a 20 20 20 52 45 53 45 52 56 45 No.** RESERVE
1a450 44 5f 4c 4f 43 4b 20 2d 3e 20 45 58 43 4c 55 53 D_LOCK -> EXCLUS
1a460 49 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 59 65 73 IVE_LOCK | Yes
1a470 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 .**.** If the bu
1a480 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 sy-handler callb
1a490 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d ack returns non-
1a4a0 7a 65 72 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69 zero, the lock i
1a4b0 73 20 0a 2a 2a 20 72 65 74 72 69 65 64 2e 20 49 s .** retried. I
1a4c0 66 20 69 74 20 72 65 74 75 72 6e 73 20 7a 65 72 f it returns zer
1a4d0 6f 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c 49 o, then the SQLI
1a4e0 54 45 5f 42 55 53 59 20 65 72 72 6f 72 20 69 73 TE_BUSY error is
1a4f0 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 74 6f 20 .** returned to
1a500 74 68 65 20 63 61 6c 6c 65 72 20 6f 66 20 74 68 the caller of th
1a510 65 20 70 61 67 65 72 20 41 50 49 20 66 75 6e 63 e pager API func
1a520 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 tion..*/.void sq
1a530 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73 lite3PagerSetBus
1a540 79 68 61 6e 64 6c 65 72 28 0a 20 20 50 61 67 65 yhandler(. Page
1a550 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 r *pPager,
1a560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a570 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 /* Pager object
1a580 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 */. int (*xBus
1a590 79 48 61 6e 64 6c 65 72 29 28 76 6f 69 64 20 2a yHandler)(void *
1a5a0 29 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f ), /* Po
1a5b0 69 6e 74 65 72 20 74 6f 20 62 75 73 79 2d 68 61 inter to busy-ha
1a5c0 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a ndler function *
1a5d0 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 73 79 48 /. void *pBusyH
1a5e0 61 6e 64 6c 65 72 41 72 67 20 20 20 20 20 20 20 andlerArg
1a5f0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 /* Argu
1a600 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f 20 ment to pass to
1a610 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a xBusyHandler */.
1a620 29 7b 20 20 0a 20 20 70 50 61 67 65 72 2d 3e 78 ){ . pPager->x
1a630 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 78 42 BusyHandler = xB
1a640 75 73 79 48 61 6e 64 6c 65 72 3b 0a 20 20 70 50 usyHandler;. pP
1a650 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c ager->pBusyHandl
1a660 65 72 41 72 67 20 3d 20 70 42 75 73 79 48 61 6e erArg = pBusyHan
1a670 64 6c 65 72 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a dlerArg;.}../*.*
1a680 2a 20 52 65 70 6f 72 74 20 74 68 65 20 63 75 72 * Report the cur
1a690 72 65 6e 74 20 70 61 67 65 20 73 69 7a 65 20 61 rent page size a
1a6a0 6e 64 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 nd number of res
1a6b0 65 72 76 65 64 20 62 79 74 65 73 20 62 61 63 6b erved bytes back
1a6c0 0a 2a 2a 20 74 6f 20 74 68 65 20 63 6f 64 65 63 .** to the codec
1a6d0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
1a6e0 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 73 74 61 TE_HAS_CODEC.sta
1a6f0 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 52 65 tic void pagerRe
1a700 70 6f 72 74 53 69 7a 65 28 50 61 67 65 72 20 2a portSize(Pager *
1a710 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 70 pPager){. if( p
1a720 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 53 69 7a Pager->xCodecSiz
1a730 65 43 68 6e 67 20 29 7b 0a 20 20 20 20 70 50 61 eChng ){. pPa
1a740 67 65 72 2d 3e 78 43 6f 64 65 63 53 69 7a 65 43 ger->xCodecSizeC
1a750 68 6e 67 28 70 50 61 67 65 72 2d 3e 70 43 6f 64 hng(pPager->pCod
1a760 65 63 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 ec, pPager->page
1a770 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 20 Size,.
1a780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a790 20 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 6e 52 (int)pPager->nR
1a7a0 65 73 65 72 76 65 29 3b 0a 20 20 7d 0a 7d 0a 23 eserve);. }.}.#
1a7b0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 70 61 else.# define pa
1a7c0 67 65 72 52 65 70 6f 72 74 53 69 7a 65 28 58 29 gerReportSize(X)
1a7d0 20 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 20 69 66 /* No-op if
1a7e0 20 77 65 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f we do not suppo
1a7f0 72 74 20 61 20 63 6f 64 65 63 20 2a 2f 0a 23 65 rt a codec */.#e
1a800 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e ndif../*.** Chan
1a810 67 65 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 ge the page size
1a820 20 75 73 65 64 20 62 79 20 74 68 65 20 50 61 67 used by the Pag
1a830 65 72 20 6f 62 6a 65 63 74 2e 20 54 68 65 20 6e er object. The n
1a840 65 77 20 70 61 67 65 20 73 69 7a 65 20 0a 2a 2a ew page size .**
1a850 20 69 73 20 70 61 73 73 65 64 20 69 6e 20 2a 70 is passed in *p
1a860 50 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 PageSize..**.**
1a870 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 If the pager is
1a880 69 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 in the error sta
1a890 74 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e te when this fun
1a8a0 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c ction is called,
1a8b0 20 69 74 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f it.** is a no-o
1a8c0 70 2e 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 p. The value ret
1a8d0 75 72 6e 65 64 20 69 73 20 74 68 65 20 65 72 72 urned is the err
1a8e0 6f 72 20 73 74 61 74 65 20 65 72 72 6f 72 20 63 or state error c
1a8f0 6f 64 65 20 28 69 2e 65 2e 20 0a 2a 2a 20 6f 6e ode (i.e. .** on
1a900 65 20 6f 66 20 53 51 4c 49 54 45 5f 49 4f 45 52 e of SQLITE_IOER
1a910 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 R, SQLITE_CORRUP
1a920 54 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c T or SQLITE_FULL
1a930 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 )..**.** Otherwi
1a940 73 65 2c 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 se, if all of th
1a950 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 e following are
1a960 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 true:.**.** *
1a970 74 68 65 20 6e 65 77 20 70 61 67 65 20 73 69 7a the new page siz
1a980 65 20 28 76 61 6c 75 65 20 6f 66 20 2a 70 50 61 e (value of *pPa
1a990 67 65 53 69 7a 65 29 20 69 73 20 76 61 6c 69 64 geSize) is valid
1a9a0 20 28 61 20 70 6f 77 65 72 20 0a 2a 2a 20 20 20 (a power .**
1a9b0 20 20 6f 66 20 74 77 6f 20 62 65 74 77 65 65 6e of two between
1a9c0 20 35 31 32 20 61 6e 64 20 53 51 4c 49 54 45 5f 512 and SQLITE_
1a9d0 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 2c 20 69 MAX_PAGE_SIZE, i
1a9e0 6e 63 6c 75 73 69 76 65 29 2c 20 61 6e 64 0a 2a nclusive), and.*
1a9f0 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 72 65 20 61 *.** * there a
1aa00 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e re no outstandin
1aa10 67 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 g page reference
1aa20 73 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a s, and.**.** *
1aa30 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
1aa40 20 65 69 74 68 65 72 20 6e 6f 74 20 61 6e 20 69 either not an i
1aa50 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
1aa60 65 20 6f 72 20 69 74 20 69 73 0a 2a 2a 20 20 20 e or it is.**
1aa70 20 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 an in-memory d
1aa80 61 74 61 62 61 73 65 20 74 68 61 74 20 63 75 72 atabase that cur
1aa90 72 65 6e 74 6c 79 20 63 6f 6e 73 69 73 74 73 20 rently consists
1aaa0 6f 66 20 7a 65 72 6f 20 70 61 67 65 73 2e 0a 2a of zero pages..*
1aab0 2a 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 70 61 *.** then the pa
1aac0 67 65 72 20 6f 62 6a 65 63 74 20 70 61 67 65 20 ger object page
1aad0 73 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 2a size is set to *
1aae0 70 50 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a pPageSize..**.**
1aaf0 20 49 66 20 74 68 65 20 70 61 67 65 20 73 69 7a If the page siz
1ab00 65 20 69 73 20 63 68 61 6e 67 65 64 2c 20 74 68 e is changed, th
1ab10 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
1ab20 20 75 73 65 73 20 73 71 6c 69 74 65 33 50 61 67 uses sqlite3Pag
1ab30 65 72 4d 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 74 erMalloc() .** t
1ab40 6f 20 6f 62 74 61 69 6e 20 61 20 6e 65 77 20 50 o obtain a new P
1ab50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65 20 62 ager.pTmpSpace b
1ab60 75 66 66 65 72 2e 20 49 66 20 74 68 69 73 20 61 uffer. If this a
1ab70 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 llocation attemp
1ab80 74 20 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c t .** fails, SQL
1ab90 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 ITE_NOMEM is ret
1aba0 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 70 61 urned and the pa
1abb0 67 65 20 73 69 7a 65 20 72 65 6d 61 69 6e 73 20 ge size remains
1abc0 75 6e 63 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 unchanged. .** I
1abd0 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 61 73 65 n all other case
1abe0 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 s, SQLITE_OK is
1abf0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
1ac00 49 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 If the page size
1ac10 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 2c is not changed,
1ac20 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65 20 either because
1ac30 6f 6e 65 20 6f 66 20 74 68 65 20 65 6e 75 6d 65 one of the enume
1ac40 72 61 74 65 64 0a 2a 2a 20 63 6f 6e 64 69 74 69 rated.** conditi
1ac50 6f 6e 73 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 ons above is not
1ac60 20 74 72 75 65 2c 20 74 68 65 20 70 61 67 65 72 true, the pager
1ac70 20 77 61 73 20 69 6e 20 65 72 72 6f 72 20 73 74 was in error st
1ac80 61 74 65 20 77 68 65 6e 20 74 68 69 73 0a 2a 2a ate when this.**
1ac90 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 function was ca
1aca0 6c 6c 65 64 2c 20 6f 72 20 62 65 63 61 75 73 65 lled, or because
1acb0 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
1acc0 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 20 66 cation attempt f
1acd0 61 69 6c 65 64 2c 20 0a 2a 2a 20 74 68 65 6e 20 ailed, .** then
1ace0 2a 70 50 61 67 65 53 69 7a 65 20 69 73 20 73 65 *pPageSize is se
1acf0 74 20 74 6f 20 74 68 65 20 6f 6c 64 2c 20 72 65 t to the old, re
1ad00 74 61 69 6e 65 64 20 70 61 67 65 20 73 69 7a 65 tained page size
1ad10 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
1ad20 67 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 g..*/.int sqlite
1ad30 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 3PagerSetPagesiz
1ad40 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c e(Pager *pPager,
1ad50 20 75 31 36 20 2a 70 50 61 67 65 53 69 7a 65 2c u16 *pPageSize,
1ad60 20 69 6e 74 20 6e 52 65 73 65 72 76 65 29 7b 0a int nReserve){.
1ad70 20 20 69 6e 74 20 72 63 20 3d 20 70 50 61 67 65 int rc = pPage
1ad80 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 69 r->errCode;.. i
1ad90 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1ada0 20 29 7b 0a 20 20 20 20 75 31 36 20 70 61 67 65 ){. u16 page
1adb0 53 69 7a 65 20 3d 20 2a 70 50 61 67 65 53 69 7a Size = *pPageSiz
1adc0 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e;. assert( p
1add0 61 67 65 53 69 7a 65 3d 3d 30 20 7c 7c 20 28 70 ageSize==0 || (p
1ade0 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 ageSize>=512 &&
1adf0 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 pageSize<=SQLITE
1ae00 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 20 _MAX_PAGE_SIZE)
1ae10 29 3b 0a 20 20 20 20 69 66 28 20 28 70 50 61 67 );. if( (pPag
1ae20 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 7c 7c 20 er->memDb==0 ||
1ae30 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d pPager->dbSize==
1ae40 30 29 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 0). && sqlit
1ae50 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 e3PcacheRefCount
1ae60 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
1ae70 29 3d 3d 30 20 0a 20 20 20 20 20 26 26 20 70 61 )==0 . && pa
1ae80 67 65 53 69 7a 65 20 26 26 20 70 61 67 65 53 69 geSize && pageSi
1ae90 7a 65 21 3d 70 50 61 67 65 72 2d 3e 70 61 67 65 ze!=pPager->page
1aea0 53 69 7a 65 20 0a 20 20 20 20 29 7b 0a 20 20 20 Size . ){.
1aeb0 20 20 20 63 68 61 72 20 2a 70 4e 65 77 20 3d 20 char *pNew =
1aec0 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50 (char *)sqlite3P
1aed0 61 67 65 4d 61 6c 6c 6f 63 28 70 61 67 65 53 69 ageMalloc(pageSi
1aee0 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 ze);. if( !
1aef0 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 pNew ){.
1af00 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
1af10 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a M;. }else{.
1af20 20 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 pager_re
1af30 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 set(pPager);.
1af40 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 61 67 pPager->pag
1af50 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 eSize = pageSize
1af60 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1af70 33 50 61 67 65 46 72 65 65 28 70 50 61 67 65 72 3PageFree(pPager
1af80 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 ->pTmpSpace);.
1af90 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 54 pPager->pT
1afa0 6d 70 53 70 61 63 65 20 3d 20 70 4e 65 77 3b 0a mpSpace = pNew;.
1afb0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 sqlite3P
1afc0 63 61 63 68 65 53 65 74 50 61 67 65 53 69 7a 65 cacheSetPageSize
1afd0 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
1afe0 2c 20 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 , pageSize);.
1aff0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2a }. }. *
1b000 70 50 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36 pPageSize = (u16
1b010 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a )pPager->pageSiz
1b020 65 3b 0a 20 20 20 20 69 66 28 20 6e 52 65 73 65 e;. if( nRese
1b030 72 76 65 3c 30 20 29 20 6e 52 65 73 65 72 76 65 rve<0 ) nReserve
1b040 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 = pPager->nRese
1b050 72 76 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 rve;. assert(
1b060 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 26 26 20 nReserve>=0 &&
1b070 6e 52 65 73 65 72 76 65 3c 31 30 30 30 20 29 3b nReserve<1000 );
1b080 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 . pPager->nRe
1b090 73 65 72 76 65 20 3d 20 28 69 31 36 29 6e 52 65 serve = (i16)nRe
1b0a0 73 65 72 76 65 3b 0a 20 20 20 20 70 61 67 65 72 serve;. pager
1b0b0 52 65 70 6f 72 74 53 69 7a 65 28 70 50 61 67 65 ReportSize(pPage
1b0c0 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e r);. }. return
1b0d0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
1b0e0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
1b0f0 6f 20 74 68 65 20 22 74 65 6d 70 6f 72 61 72 79 o the "temporary
1b100 20 70 61 67 65 22 20 62 75 66 66 65 72 20 68 65 page" buffer he
1b110 6c 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a ld internally.**
1b120 20 62 79 20 74 68 65 20 70 61 67 65 72 2e 20 20 by the pager.
1b130 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 This is a buffer
1b140 20 74 68 61 74 20 69 73 20 62 69 67 20 65 6e 6f that is big eno
1b150 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a ugh to hold the.
1b160 2a 2a 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e ** entire conten
1b170 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 t of a database
1b180 70 61 67 65 2e 20 20 54 68 69 73 20 62 75 66 66 page. This buff
1b190 65 72 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 er is used inter
1b1a0 6e 61 6c 6c 79 0a 2a 2a 20 64 75 72 69 6e 67 20 nally.** during
1b1b0 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 77 69 6c rollback and wil
1b1c0 6c 20 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e l be overwritten
1b1d0 20 77 68 65 6e 65 76 65 72 20 61 20 72 6f 6c 6c whenever a roll
1b1e0 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 back.** occurs.
1b1f0 20 42 75 74 20 6f 74 68 65 72 20 6d 6f 64 75 6c But other modul
1b200 65 73 20 61 72 65 20 66 72 65 65 20 74 6f 20 75 es are free to u
1b210 73 65 20 69 74 20 74 6f 6f 2c 20 61 73 20 6c 6f se it too, as lo
1b220 6e 67 20 61 73 0a 2a 2a 20 6e 6f 20 72 6f 6c 6c ng as.** no roll
1b230 62 61 63 6b 73 20 61 72 65 20 68 61 70 70 65 6e backs are happen
1b240 69 6e 67 2e 0a 2a 2f 0a 76 6f 69 64 20 2a 73 71 ing..*/.void *sq
1b250 6c 69 74 65 33 50 61 67 65 72 54 65 6d 70 53 70 lite3PagerTempSp
1b260 61 63 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ace(Pager *pPage
1b270 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 r){. return pPa
1b280 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a ger->pTmpSpace;.
1b290 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 }../*.** Attempt
1b2a0 20 74 6f 20 73 65 74 20 74 68 65 20 6d 61 78 69 to set the maxi
1b2b0 6d 75 6d 20 64 61 74 61 62 61 73 65 20 70 61 67 mum database pag
1b2c0 65 20 63 6f 75 6e 74 20 69 66 20 6d 78 50 61 67 e count if mxPag
1b2d0 65 20 69 73 20 70 6f 73 69 74 69 76 65 2e 20 0a e is positive. .
1b2e0 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 ** Make no chang
1b2f0 65 73 20 69 66 20 6d 78 50 61 67 65 20 69 73 20 es if mxPage is
1b300 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 zero or negative
1b310 2e 20 20 41 6e 64 20 6e 65 76 65 72 20 72 65 64 . And never red
1b320 75 63 65 20 74 68 65 0a 2a 2a 20 6d 61 78 69 6d uce the.** maxim
1b330 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 62 65 um page count be
1b340 6c 6f 77 20 74 68 65 20 63 75 72 72 65 6e 74 20 low the current
1b350 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 size of the data
1b360 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 61 base..**.** Rega
1b370 72 64 6c 65 73 73 20 6f 66 20 6d 78 50 61 67 65 rdless of mxPage
1b380 2c 20 72 65 74 75 72 6e 20 74 68 65 20 63 75 72 , return the cur
1b390 72 65 6e 74 20 6d 61 78 69 6d 75 6d 20 70 61 67 rent maximum pag
1b3a0 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 69 6e 74 20 e count..*/.int
1b3b0 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61 78 50 sqlite3PagerMaxP
1b3c0 61 67 65 43 6f 75 6e 74 28 50 61 67 65 72 20 2a ageCount(Pager *
1b3d0 70 50 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 pPager, int mxPa
1b3e0 67 65 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 ge){. int nPage
1b3f0 3b 0a 20 20 69 66 28 20 6d 78 50 61 67 65 3e 30 ;. if( mxPage>0
1b400 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
1b410 6d 78 50 67 6e 6f 20 3d 20 6d 78 50 61 67 65 3b mxPgno = mxPage;
1b420 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 . }. if( pPage
1b430 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f r->state!=PAGER_
1b440 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 73 71 UNLOCK ){. sq
1b450 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f lite3PagerPageco
1b460 75 6e 74 28 70 50 61 67 65 72 2c 20 26 6e 50 61 unt(pPager, &nPa
1b470 67 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ge);. assert(
1b480 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 3e pPager->mxPgno>
1b490 3d 6e 50 61 67 65 20 29 3b 0a 20 20 7d 0a 20 20 =nPage );. }.
1b4a0 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6d return pPager->m
1b4b0 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 xPgno;.}../*.**
1b4c0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 The following se
1b4d0 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 72 t of routines ar
1b4e0 65 20 75 73 65 64 20 74 6f 20 64 69 73 61 62 6c e used to disabl
1b4f0 65 20 74 68 65 20 73 69 6d 75 6c 61 74 65 64 0a e the simulated.
1b500 2a 2a 20 49 2f 4f 20 65 72 72 6f 72 20 6d 65 63 ** I/O error mec
1b510 68 61 6e 69 73 6d 2e 20 20 54 68 65 73 65 20 72 hanism. These r
1b520 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 outines are used
1b530 20 74 6f 20 61 76 6f 69 64 20 73 69 6d 75 6c 61 to avoid simula
1b540 74 65 64 0a 2a 2a 20 65 72 72 6f 72 73 20 69 6e ted.** errors in
1b550 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 77 65 places where we
1b560 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f do not care abo
1b570 75 74 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a ut errors..**.**
1b580 20 55 6e 6c 65 73 73 20 2d 44 53 51 4c 49 54 45 Unless -DSQLITE
1b590 5f 54 45 53 54 3d 31 20 69 73 20 75 73 65 64 2c _TEST=1 is used,
1b5a0 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
1b5b0 61 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a are all no-ops.*
1b5c0 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20 6e * and generate n
1b5d0 6f 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64 65 o code..*/.#ifde
1b5e0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 65 78 f SQLITE_TEST.ex
1b5f0 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 tern int sqlite3
1b600 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e _io_error_pendin
1b610 67 3b 0a 65 78 74 65 72 6e 20 69 6e 74 20 73 71 g;.extern int sq
1b620 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 lite3_io_error_h
1b630 69 74 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 73 it;.static int s
1b640 61 76 65 64 5f 63 6e 74 3b 0a 76 6f 69 64 20 64 aved_cnt;.void d
1b650 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 isable_simulated
1b660 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 _io_errors(void)
1b670 7b 0a 20 20 73 61 76 65 64 5f 63 6e 74 20 3d 20 {. saved_cnt =
1b680 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
1b690 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 73 71 6c 69 _pending;. sqli
1b6a0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e te3_io_error_pen
1b6b0 64 69 6e 67 20 3d 20 2d 31 3b 0a 7d 0a 76 6f 69 ding = -1;.}.voi
1b6c0 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 d enable_simulat
1b6d0 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 ed_io_errors(voi
1b6e0 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f d){. sqlite3_io
1b6f0 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d _error_pending =
1b700 20 73 61 76 65 64 5f 63 6e 74 3b 0a 7d 0a 23 65 saved_cnt;.}.#e
1b710 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 64 69 73 lse.# define dis
1b720 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 able_simulated_i
1b730 6f 5f 65 72 72 6f 72 73 28 29 0a 23 20 64 65 66 o_errors().# def
1b740 69 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c ine enable_simul
1b750 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 ated_io_errors()
1b760 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
1b770 65 61 64 20 74 68 65 20 66 69 72 73 74 20 4e 20 ead the first N
1b780 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62 bytes from the b
1b790 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 eginning of the
1b7a0 66 69 6c 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 file into memory
1b7b0 0a 2a 2a 20 74 68 61 74 20 70 44 65 73 74 20 70 .** that pDest p
1b7c0 6f 69 6e 74 73 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a oints to. .**.**
1b7d0 20 49 66 20 74 68 65 20 70 61 67 65 72 20 77 61 If the pager wa
1b7e0 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 74 72 s opened on a tr
1b7f0 61 6e 73 69 65 6e 74 20 66 69 6c 65 20 28 7a 46 ansient file (zF
1b800 69 6c 65 6e 61 6d 65 3d 3d 22 22 29 2c 20 6f 72 ilename==""), or
1b810 0a 2a 2a 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 .** opened on a
1b820 66 69 6c 65 20 6c 65 73 73 20 74 68 61 6e 20 4e file less than N
1b830 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 bytes in size,
1b840 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 the output buffe
1b850 72 20 69 73 0a 2a 2a 20 7a 65 72 6f 65 64 20 61 r is.** zeroed a
1b860 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 nd SQLITE_OK ret
1b870 75 72 6e 65 64 2e 20 54 68 65 20 72 61 74 69 6f urned. The ratio
1b880 6e 61 6c 65 20 66 6f 72 20 74 68 69 73 20 69 73 nale for this is
1b890 20 74 68 61 74 20 74 68 69 73 20 0a 2a 2a 20 66 that this .** f
1b8a0 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 unction is used
1b8b0 74 6f 20 72 65 61 64 20 64 61 74 61 62 61 73 65 to read database
1b8c0 20 68 65 61 64 65 72 73 2c 20 61 6e 64 20 61 20 headers, and a
1b8d0 6e 65 77 20 74 72 61 6e 73 69 65 6e 74 20 6f 72 new transient or
1b8e0 0a 2a 2a 20 7a 65 72 6f 20 73 69 7a 65 64 20 64 .** zero sized d
1b8f0 61 74 61 62 61 73 65 20 68 61 73 20 61 20 68 65 atabase has a he
1b900 61 64 65 72 20 74 68 61 6e 20 63 6f 6e 73 69 73 ader than consis
1b910 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 66 20 7a ts entirely of z
1b920 65 72 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 eroes..**.** If
1b930 61 6e 79 20 49 4f 20 65 72 72 6f 72 20 61 70 61 any IO error apa
1b940 72 74 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 49 rt from SQLITE_I
1b950 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 OERR_SHORT_READ
1b960 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 0a is encountered,.
1b970 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 ** the error cod
1b980 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f e is returned to
1b990 20 74 68 65 20 63 61 6c 6c 65 72 20 61 6e 64 20 the caller and
1b9a0 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
1b9b0 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75 the.** output bu
1b9c0 66 66 65 72 20 75 6e 64 65 66 69 6e 65 64 2e 0a ffer undefined..
1b9d0 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 61 */.int sqlite3Pa
1b9e0 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 gerReadFileheade
1b9f0 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c r(Pager *pPager,
1ba00 20 69 6e 74 20 4e 2c 20 75 6e 73 69 67 6e 65 64 int N, unsigned
1ba10 20 63 68 61 72 20 2a 70 44 65 73 74 29 7b 0a 20 char *pDest){.
1ba20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1ba30 5f 4f 4b 3b 0a 20 20 6d 65 6d 73 65 74 28 70 44 _OK;. memset(pD
1ba40 65 73 74 2c 20 30 2c 20 4e 29 3b 0a 20 20 61 73 est, 0, N);. as
1ba50 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 sert( isOpen(pPa
1ba60 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 ger->fd) || pPag
1ba70 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a er->tempFile );.
1ba80 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 . /* This routi
1ba90 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 ne is only calle
1baa0 64 20 62 79 20 62 74 72 65 65 20 69 6d 6d 65 64 d by btree immed
1bab0 69 61 74 65 6c 79 20 61 66 74 65 72 20 63 72 65 iately after cre
1bac0 61 74 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 50 ating. ** the P
1bad0 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 20 54 68 ager object. Th
1bae0 65 72 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e ere has not been
1baf0 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 an opportunity
1bb00 74 6f 20 74 72 61 6e 73 69 74 69 6f 6e 0a 20 20 to transition.
1bb10 2a 2a 20 74 6f 20 57 41 4c 20 6d 6f 64 65 20 79 ** to WAL mode y
1bb20 65 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 et.. */. asser
1bb30 74 28 20 21 70 61 67 65 72 55 73 65 57 61 6c 28 t( !pagerUseWal(
1bb40 70 50 61 67 65 72 29 20 29 3b 0a 23 69 66 20 30 pPager) );.#if 0
1bb50 0a 20 20 69 66 28 20 70 61 67 65 72 55 73 65 57 . if( pagerUseW
1bb60 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 al(pPager) ){.
1bb70 20 20 69 6e 74 20 69 73 49 6e 57 61 6c 20 3d 20 int isInWal =
1bb80 30 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 0;. rc = sqli
1bb90 74 65 33 57 61 6c 52 65 61 64 28 70 50 61 67 65 te3WalRead(pPage
1bba0 72 2d 3e 70 57 61 6c 2c 20 31 2c 20 26 69 73 49 r->pWal, 1, &isI
1bbb0 6e 57 61 6c 2c 20 4e 2c 20 70 44 65 73 74 29 3b nWal, N, pDest);
1bbc0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1bbd0 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 49 6e 57 61 ITE_OK || isInWa
1bbe0 6c 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 l ){. retur
1bbf0 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a n rc;. }. }.
1bc00 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 69 73 #endif.. if( is
1bc10 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
1bc20 20 29 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 ){. IOTRACE(
1bc30 28 22 44 42 48 44 52 20 25 70 20 30 20 25 64 5c ("DBHDR %p 0 %d\
1bc40 6e 22 2c 20 70 50 61 67 65 72 2c 20 4e 29 29 0a n", pPager, N)).
1bc50 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1bc60 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 OsRead(pPager->f
1bc70 64 2c 20 70 44 65 73 74 2c 20 4e 2c 20 30 29 3b d, pDest, N, 0);
1bc80 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
1bc90 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f ITE_IOERR_SHORT_
1bca0 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 72 63 READ ){. rc
1bcb0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1bcc0 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1bcd0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
1bce0 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e turn the total n
1bcf0 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
1bd00 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
1bd10 69 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 0a ile associated .
1bd20 2a 2a 20 77 69 74 68 20 70 50 61 67 65 72 2e 20 ** with pPager.
1bd30 4e 6f 72 6d 61 6c 6c 79 2c 20 74 68 69 73 20 69 Normally, this i
1bd40 73 20 63 61 6c 63 75 6c 61 74 65 64 20 61 73 20 s calculated as
1bd50 28 3c 64 62 20 66 69 6c 65 20 73 69 7a 65 3e 2f (<db file size>/
1bd60 3c 70 61 67 65 2d 73 69 7a 65 3e 29 2e 0a 2a 2a <page-size>)..**
1bd70 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 65 However, if the
1bd80 20 66 69 6c 65 20 69 73 20 62 65 74 77 65 65 6e file is between
1bd90 20 31 20 61 6e 64 20 3c 70 61 67 65 2d 73 69 7a 1 and <page-siz
1bda0 65 3e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 e> bytes in size
1bdb0 2c 20 74 68 65 6e 20 0a 2a 2a 20 74 68 69 73 20 , then .** this
1bdc0 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20 is considered a
1bdd0 31 20 70 61 67 65 20 66 69 6c 65 2e 0a 2a 2a 0a 1 page file..**.
1bde0 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 ** If the pager
1bdf0 69 73 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 is in error stat
1be00 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 e when this func
1be10 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 tion is called,
1be20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65 72 72 6f then the.** erro
1be30 72 20 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f r state error co
1be40 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 de is returned a
1be50 6e 64 20 2a 70 6e 50 61 67 65 20 6c 65 66 74 20 nd *pnPage left
1be60 75 6e 63 68 61 6e 67 65 64 2e 20 4f 72 2c 0a 2a unchanged. Or,.*
1be70 2a 20 69 66 20 74 68 65 20 66 69 6c 65 20 73 79 * if the file sy
1be80 73 74 65 6d 20 68 61 73 20 74 6f 20 62 65 20 71 stem has to be q
1be90 75 65 72 69 65 64 20 66 6f 72 20 74 68 65 20 73 ueried for the s
1bea0 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ize of the file
1beb0 61 6e 64 0a 2a 2a 20 74 68 65 20 71 75 65 72 79 and.** the query
1bec0 20 61 74 74 65 6d 70 74 20 72 65 74 75 72 6e 73 attempt returns
1bed0 20 61 6e 20 49 4f 20 65 72 72 6f 72 2c 20 74 68 an IO error, th
1bee0 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 e IO error code
1bef0 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 is returned.** a
1bf00 6e 64 20 2a 70 6e 50 61 67 65 20 69 73 20 6c 65 nd *pnPage is le
1bf10 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a ft unchanged..**
1bf20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 .** Otherwise, i
1bf30 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 f everything is
1bf40 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 6e successful, then
1bf50 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
1bf60 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 2a 70 turned.** and *p
1bf70 6e 50 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 nPage is set to
1bf80 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
1bf90 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 ges in the datab
1bfa0 61 73 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 ase..*/.int sqli
1bfb0 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e te3PagerPagecoun
1bfc0 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c t(Pager *pPager,
1bfd0 20 69 6e 74 20 2a 70 6e 50 61 67 65 29 7b 0a 20 int *pnPage){.
1bfe0 20 50 67 6e 6f 20 6e 50 61 67 65 20 3d 20 30 3b Pgno nPage = 0;
1bff0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 /* Va
1c000 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 76 69 lue to return vi
1c010 61 20 2a 70 6e 50 61 67 65 20 2a 2f 0a 0a 20 20 a *pnPage */..
1c020 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 /* Determine the
1c030 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
1c040 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 53 74 in the file. St
1c050 6f 72 65 20 74 68 69 73 20 69 6e 20 6e 50 61 67 ore this in nPag
1c060 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 e. */. if( pPag
1c070 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 er->dbSizeValid
1c080 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 70 ){. nPage = p
1c090 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 Pager->dbSize;.
1c0a0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 }else{. int
1c0b0 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
1c0c0 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 72 65 74 /* Error ret
1c0d0 75 72 6e 65 64 20 62 79 20 4f 73 46 69 6c 65 53 urned by OsFileS
1c0e0 69 7a 65 28 29 20 2a 2f 0a 20 20 20 20 69 36 34 ize() */. i64
1c0f0 20 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 n = 0;
1c100 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 73 69 7a /* File siz
1c110 65 20 69 6e 20 62 79 74 65 73 20 72 65 74 75 72 e in bytes retur
1c120 6e 65 64 20 62 79 20 4f 73 46 69 6c 65 53 69 7a ned by OsFileSiz
1c130 65 28 29 20 2a 2f 0a 0a 20 20 20 20 69 66 28 20 e() */.. if(
1c140 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 pagerUseWal(pPag
1c150 65 72 29 20 26 26 20 70 50 61 67 65 72 2d 3e 73 er) && pPager->s
1c160 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f tate!=PAGER_UNLO
1c170 43 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 CK ){. sqli
1c180 74 65 33 57 61 6c 44 62 73 69 7a 65 28 70 50 61 te3WalDbsize(pPa
1c190 67 65 72 2d 3e 70 57 61 6c 2c 20 26 6e 50 61 67 ger->pWal, &nPag
1c1a0 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 e);. }.. i
1c1b0 66 28 20 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 f( nPage==0 ){.
1c1c0 20 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f assert( isO
1c1d0 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
1c1e0 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 || pPager->tempF
1c1f0 69 6c 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28 ile );. if(
1c200 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
1c210 66 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 fd) ){. i
1c220 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 f( SQLITE_OK!=(r
1c230 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c c = sqlite3OsFil
1c240 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 eSize(pPager->fd
1c250 2c 20 26 6e 29 29 20 29 7b 0a 20 20 20 20 20 20 , &n)) ){.
1c260 20 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72 28 pager_error(
1c270 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 pPager, rc);.
1c280 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
1c290 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1c2a0 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6e 3e }. if( n>
1c2b0 30 20 26 26 20 6e 3c 70 50 61 67 65 72 2d 3e 70 0 && n<pPager->p
1c2c0 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 ageSize ){.
1c2d0 20 20 20 6e 50 61 67 65 20 3d 20 31 3b 0a 20 20 nPage = 1;.
1c2e0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1c2f0 20 20 20 6e 50 61 67 65 20 3d 20 28 50 67 6e 6f nPage = (Pgno
1c300 29 28 6e 20 2f 20 70 50 61 67 65 72 2d 3e 70 61 )(n / pPager->pa
1c310 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d geSize);. }
1c320 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
1c330 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 Pager->state!=PA
1c340 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 GER_UNLOCK ){.
1c350 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 pPager->dbSi
1c360 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 ze = nPage;.
1c370 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 pPager->dbFile
1c380 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 Size = nPage;.
1c390 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 pPager->dbSi
1c3a0 7a 65 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20 zeValid = 1;.
1c3b0 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
1c3c0 74 68 65 20 63 75 72 72 65 6e 74 20 6e 75 6d 62 the current numb
1c3d0 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 er of pages in t
1c3e0 68 65 20 66 69 6c 65 20 69 73 20 67 72 65 61 74 he file is great
1c3f0 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20 2a er than the . *
1c400 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 6d 61 78 * configured max
1c410 69 6d 75 6d 20 70 61 67 65 72 20 6e 75 6d 62 65 imum pager numbe
1c420 72 2c 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 r, increase the
1c430 61 6c 6c 6f 77 65 64 20 6c 69 6d 69 74 20 73 6f allowed limit so
1c440 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 66 . ** that the f
1c450 69 6c 65 20 63 61 6e 20 62 65 20 72 65 61 64 2e ile can be read.
1c460 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 50 61 67 . */. if( nPag
1c470 65 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f e>pPager->mxPgno
1c480 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
1c490 6d 78 50 67 6e 6f 20 3d 20 28 50 67 6e 6f 29 6e mxPgno = (Pgno)n
1c4a0 50 61 67 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 Page;. }.. /*
1c4b0 53 65 74 20 74 68 65 20 6f 75 74 70 75 74 20 76 Set the output v
1c4c0 61 72 69 61 62 6c 65 20 61 6e 64 20 72 65 74 75 ariable and retu
1c4d0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 2a 2f 0a rn SQLITE_OK */.
1c4e0 20 20 2a 70 6e 50 61 67 65 20 3d 20 6e 50 61 67 *pnPage = nPag
1c4f0 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 e;. return SQLI
1c500 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a TE_OK;.}.../*.**
1c510 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e 20 61 Try to obtain a
1c520 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 6c 6f lock of type lo
1c530 63 6b 74 79 70 65 20 6f 6e 20 74 68 65 20 64 61 cktype on the da
1c540 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 0a tabase file. If.
1c550 2a 2a 20 61 20 73 69 6d 69 6c 61 72 20 6f 72 20 ** a similar or
1c560 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 69 73 20 greater lock is
1c570 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74 68 already held, th
1c580 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
1c590 20 6e 6f 2d 6f 70 0a 2a 2a 20 28 72 65 74 75 72 no-op.** (retur
1c5a0 6e 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 20 69 ning SQLITE_OK i
1c5b0 6d 6d 65 64 69 61 74 65 6c 79 29 2e 0a 2a 2a 0a mmediately)..**.
1c5c0 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 74 ** Otherwise, at
1c5d0 74 65 6d 70 74 20 74 6f 20 6f 62 74 61 69 6e 20 tempt to obtain
1c5e0 74 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 73 the lock using s
1c5f0 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 qlite3OsLock().
1c600 49 6e 76 6f 6b 65 20 0a 2a 2a 20 74 68 65 20 62 Invoke .** the b
1c610 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 usy callback if
1c620 74 68 65 20 6c 6f 63 6b 20 69 73 20 63 75 72 72 the lock is curr
1c630 65 6e 74 6c 79 20 6e 6f 74 20 61 76 61 69 6c 61 ently not availa
1c640 62 6c 65 2e 20 52 65 70 65 61 74 20 0a 2a 2a 20 ble. Repeat .**
1c650 75 6e 74 69 6c 20 74 68 65 20 62 75 73 79 20 63 until the busy c
1c660 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 allback returns
1c670 66 61 6c 73 65 20 6f 72 20 75 6e 74 69 6c 20 74 false or until t
1c680 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 0a 2a he attempt to .*
1c690 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 6f 63 * obtain the loc
1c6a0 6b 20 73 75 63 63 65 65 64 73 2e 0a 2a 2a 0a 2a k succeeds..**.*
1c6b0 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f * Return SQLITE_
1c6c0 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e OK on success an
1c6d0 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 d an error code
1c6e0 69 66 20 77 65 20 63 61 6e 6e 6f 74 20 6f 62 74 if we cannot obt
1c6f0 61 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e ain.** the lock.
1c700 20 49 66 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 If the lock is
1c710 6f 62 74 61 69 6e 65 64 20 73 75 63 63 65 73 73 obtained success
1c720 66 75 6c 6c 79 2c 20 73 65 74 20 74 68 65 20 50 fully, set the P
1c730 61 67 65 72 2e 73 74 61 74 65 20 0a 2a 2a 20 76 ager.state .** v
1c740 61 72 69 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 74 ariable to lockt
1c750 79 70 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 ype before retur
1c760 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ning..*/.static
1c770 69 6e 74 20 70 61 67 65 72 5f 77 61 69 74 5f 6f int pager_wait_o
1c780 6e 5f 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 n_lock(Pager *pP
1c790 61 67 65 72 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 ager, int lockty
1c7a0 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 pe){. int rc;
1c7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c7c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1c7d0 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 eturn code */..
1c7e0 20 2f 2a 20 54 68 65 20 4f 53 20 6c 6f 63 6b 20 /* The OS lock
1c7f0 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20 74 values must be t
1c800 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 50 he same as the P
1c810 61 67 65 72 20 6c 6f 63 6b 20 76 61 6c 75 65 73 ager lock values
1c820 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 50 41 */. assert( PA
1c830 47 45 52 5f 53 48 41 52 45 44 3d 3d 53 48 41 52 GER_SHARED==SHAR
1c840 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 ED_LOCK );. ass
1c850 65 72 74 28 20 50 41 47 45 52 5f 52 45 53 45 52 ert( PAGER_RESER
1c860 56 45 44 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f VED==RESERVED_LO
1c870 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 CK );. assert(
1c880 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3d PAGER_EXCLUSIVE=
1c890 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
1c8a0 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
1c8b0 66 69 6c 65 20 69 73 20 63 75 72 72 65 6e 74 6c file is currentl
1c8c0 79 20 75 6e 6c 6f 63 6b 65 64 20 74 68 65 6e 20 y unlocked then
1c8d0 74 68 65 20 73 69 7a 65 20 6d 75 73 74 20 62 65 the size must be
1c8e0 20 75 6e 6b 6e 6f 77 6e 2e 20 49 74 0a 20 20 2a unknown. It. *
1c8f0 2a 20 6d 75 73 74 20 6e 6f 74 20 68 61 76 65 20 * must not have
1c900 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 61 74 been modified at
1c910 20 74 68 69 73 20 70 6f 69 6e 74 2e 0a 20 20 2a this point.. *
1c920 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 /. assert( pPag
1c930 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 er->state>=PAGER
1c940 5f 53 48 41 52 45 44 20 7c 7c 20 70 50 61 67 65 _SHARED || pPage
1c950 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 3d 3d r->dbSizeValid==
1c960 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1c970 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
1c980 47 45 52 5f 53 48 41 52 45 44 20 7c 7c 20 70 50 GER_SHARED || pP
1c990 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 ager->dbModified
1c9a0 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 ==0 );.. /* Che
1c9b0 63 6b 20 74 68 61 74 20 74 68 69 73 20 69 73 20 ck that this is
1c9c0 65 69 74 68 65 72 20 61 20 6e 6f 2d 6f 70 20 28 either a no-op (
1c9d0 62 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75 because the requ
1c9e0 65 73 74 65 64 20 6c 6f 63 6b 20 69 73 20 0a 20 ested lock is .
1c9f0 20 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 ** already held
1ca00 2c 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 , or one of the
1ca10 74 72 61 6e 73 69 73 74 69 6f 6e 73 20 74 68 61 transistions tha
1ca20 74 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64 6c t the busy-handl
1ca30 65 72 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 69 er. ** may be i
1ca40 6e 76 6f 6b 65 64 20 64 75 72 69 6e 67 2c 20 61 nvoked during, a
1ca50 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 ccording to the
1ca60 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 0a 20 20 comment above.
1ca70 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 ** sqlite3PagerS
1ca80 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 29 2e etBusyhandler().
1ca90 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
1caa0 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d (pPager->state>=
1cab0 6c 6f 63 6b 74 79 70 65 29 0a 20 20 20 20 20 20 locktype).
1cac0 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 73 74 61 || (pPager->sta
1cad0 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b te==PAGER_UNLOCK
1cae0 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 50 41 && locktype==PA
1caf0 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20 20 GER_SHARED).
1cb00 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 73 || (pPager->s
1cb10 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45 tate==PAGER_RESE
1cb20 52 56 45 44 20 26 26 20 6c 6f 63 6b 74 79 70 65 RVED && locktype
1cb30 3d 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 ==PAGER_EXCLUSIV
1cb40 45 29 0a 20 20 29 3b 0a 0a 20 20 69 66 28 20 70 E). );.. if( p
1cb50 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 6c 6f Pager->state>=lo
1cb60 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 63 cktype ){. rc
1cb70 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1cb80 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 20 7b 0a }else{. do {.
1cb90 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1cba0 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d e3OsLock(pPager-
1cbb0 3e 66 64 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a >fd, locktype);.
1cbc0 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d }while( rc==
1cbd0 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70 SQLITE_BUSY && p
1cbe0 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 Pager->xBusyHand
1cbf0 6c 65 72 28 70 50 61 67 65 72 2d 3e 70 42 75 73 ler(pPager->pBus
1cc00 79 48 61 6e 64 6c 65 72 41 72 67 29 20 29 3b 0a yHandlerArg) );.
1cc10 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1cc20 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 TE_OK ){. p
1cc30 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 28 Pager->state = (
1cc40 75 38 29 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 u8)locktype;.
1cc50 20 20 20 49 4f 54 52 41 43 45 28 28 22 4c 4f 43 IOTRACE(("LOC
1cc60 4b 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 K %p %d\n", pPag
1cc70 65 72 2c 20 6c 6f 63 6b 74 79 70 65 29 29 0a 20 er, locktype)).
1cc80 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
1cc90 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 n rc;.}../*.** F
1cca0 75 6e 63 74 69 6f 6e 20 61 73 73 65 72 74 54 72 unction assertTr
1ccb0 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 uncateConstraint
1ccc0 28 70 50 61 67 65 72 29 20 63 68 65 63 6b 73 20 (pPager) checks
1ccd0 74 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 that one of the
1cce0 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 .** following is
1ccf0 20 74 72 75 65 20 66 6f 72 20 61 6c 6c 20 64 69 true for all di
1cd00 72 74 79 20 70 61 67 65 73 20 63 75 72 72 65 6e rty pages curren
1cd10 74 6c 79 20 69 6e 20 74 68 65 20 70 61 67 65 2d tly in the page-
1cd20 63 61 63 68 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 cache:.**.** a
1cd30 29 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 ) The page numbe
1cd40 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f r is less than o
1cd50 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 73 r equal to the s
1cd60 69 7a 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 20 ize of the .**
1cd70 20 20 20 20 63 75 72 72 65 6e 74 20 64 61 74 61 current data
1cd80 62 61 73 65 20 69 6d 61 67 65 2c 20 69 6e 20 70 base image, in p
1cd90 61 67 65 73 2c 20 4f 52 0a 2a 2a 0a 2a 2a 20 20 ages, OR.**.**
1cda0 20 62 29 20 69 66 20 74 68 65 20 70 61 67 65 20 b) if the page
1cdb0 63 6f 6e 74 65 6e 74 20 77 65 72 65 20 77 72 69 content were wri
1cdc0 74 74 65 6e 20 61 74 20 74 68 69 73 20 74 69 6d tten at this tim
1cdd0 65 2c 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74 0a e, it would not.
1cde0 2a 2a 20 20 20 20 20 20 62 65 20 6e 65 63 65 73 ** be neces
1cdf0 73 61 72 79 20 74 6f 20 77 72 69 74 65 20 74 68 sary to write th
1ce00 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e e current conten
1ce10 74 20 6f 75 74 20 74 6f 20 74 68 65 20 73 75 62 t out to the sub
1ce20 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 -journal.**
1ce30 20 28 61 73 20 64 65 74 65 72 6d 69 6e 65 64 20 (as determined
1ce40 62 79 20 66 75 6e 63 74 69 6f 6e 20 73 75 62 6a by function subj
1ce50 52 65 71 75 69 72 65 73 50 61 67 65 28 29 29 2e RequiresPage()).
1ce60 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f .**.** If the co
1ce70 6e 64 69 74 69 6f 6e 20 61 73 73 65 72 74 65 64 ndition asserted
1ce80 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f by this functio
1ce90 6e 20 77 65 72 65 20 6e 6f 74 20 74 72 75 65 2c n were not true,
1cea0 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 69 72 74 and the.** dirt
1ceb0 79 20 70 61 67 65 20 77 65 72 65 20 74 6f 20 62 y page were to b
1cec0 65 20 64 69 73 63 61 72 64 65 64 20 66 72 6f 6d e discarded from
1ced0 20 74 68 65 20 63 61 63 68 65 20 76 69 61 20 74 the cache via t
1cee0 68 65 20 70 61 67 65 72 53 74 72 65 73 73 28 29 he pagerStress()
1cef0 0a 2a 2a 20 72 6f 75 74 69 6e 65 2c 20 70 61 67 .** routine, pag
1cf00 65 72 53 74 72 65 73 73 28 29 20 77 6f 75 6c 64 erStress() would
1cf10 20 6e 6f 74 20 77 72 69 74 65 20 74 68 65 20 63 not write the c
1cf20 75 72 72 65 6e 74 20 70 61 67 65 20 63 6f 6e 74 urrent page cont
1cf30 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 61 ent to.** the da
1cf40 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 tabase file. If
1cf50 61 20 73 61 76 65 70 6f 69 6e 74 20 74 72 61 6e a savepoint tran
1cf60 73 61 63 74 69 6f 6e 20 77 65 72 65 20 72 6f 6c saction were rol
1cf70 6c 65 64 20 62 61 63 6b 20 61 66 74 65 72 0a 2a led back after.*
1cf80 2a 20 74 68 69 73 20 68 61 70 70 65 6e 65 64 2c * this happened,
1cf90 20 74 68 65 20 63 6f 72 72 65 63 74 20 62 65 68 the correct beh
1cfa0 61 76 69 6f 75 72 20 77 6f 75 6c 64 20 62 65 20 aviour would be
1cfb0 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 63 to restore the c
1cfc0 75 72 72 65 6e 74 0a 2a 2a 20 63 6f 6e 74 65 6e urrent.** conten
1cfd0 74 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20 48 t of the page. H
1cfe0 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 74 68 owever, since th
1cff0 69 73 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f is content is no
1d000 74 20 70 72 65 73 65 6e 74 20 69 6e 20 65 69 74 t present in eit
1d010 68 65 72 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 her.** the datab
1d020 61 73 65 20 66 69 6c 65 20 6f 72 20 74 68 65 20 ase file or the
1d030 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 72 portion of the r
1d040 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 ollback journal
1d050 61 6e 64 20 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 and .** sub-jour
1d060 6e 61 6c 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 nal rolled back
1d070 74 68 65 20 63 6f 6e 74 65 6e 74 20 63 6f 75 6c the content coul
1d080 64 20 6e 6f 74 20 62 65 20 72 65 73 74 6f 72 65 d not be restore
1d090 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 61 74 d and the.** dat
1d0a0 61 62 61 73 65 20 69 6d 61 67 65 20 77 6f 75 6c abase image woul
1d0b0 64 20 62 65 63 6f 6d 65 20 63 6f 72 72 75 70 74 d become corrupt
1d0c0 2e 20 49 74 20 69 73 20 74 68 65 72 65 66 6f 72 . It is therefor
1d0d0 65 20 66 6f 72 74 75 6e 61 74 65 20 74 68 61 74 e fortunate that
1d0e0 20 0a 2a 2a 20 74 68 69 73 20 63 69 72 63 75 6d .** this circum
1d0f0 73 74 61 6e 63 65 20 63 61 6e 6e 6f 74 20 61 72 stance cannot ar
1d100 69 73 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 ise..*/.#if defi
1d110 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
1d120 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 ).static void as
1d130 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 sertTruncateCons
1d140 74 72 61 69 6e 74 43 62 28 50 67 48 64 72 20 2a traintCb(PgHdr *
1d150 70 50 67 29 7b 0a 20 20 61 73 73 65 72 74 28 20 pPg){. assert(
1d160 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 pPg->flags&PGHDR
1d170 5f 44 49 52 54 59 20 29 3b 0a 20 20 61 73 73 65 _DIRTY );. asse
1d180 72 74 28 20 21 73 75 62 6a 52 65 71 75 69 72 65 rt( !subjRequire
1d190 73 50 61 67 65 28 70 50 67 29 20 7c 7c 20 70 50 sPage(pPg) || pP
1d1a0 67 2d 3e 70 67 6e 6f 3c 3d 70 50 67 2d 3e 70 50 g->pgno<=pPg->pP
1d1b0 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 29 3b 0a ager->dbSize );.
1d1c0 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 }.static void as
1d1d0 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 sertTruncateCons
1d1e0 74 72 61 69 6e 74 28 50 61 67 65 72 20 2a 70 50 traint(Pager *pP
1d1f0 61 67 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 ager){. sqlite3
1d200 50 63 61 63 68 65 49 74 65 72 61 74 65 44 69 72 PcacheIterateDir
1d210 74 79 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ty(pPager->pPCac
1d220 68 65 2c 20 61 73 73 65 72 74 54 72 75 6e 63 61 he, assertTrunca
1d230 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 62 29 3b teConstraintCb);
1d240 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e .}.#else.# defin
1d250 65 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 e assertTruncate
1d260 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 67 65 Constraint(pPage
1d270 72 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a r).#endif../*.**
1d280 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 69 6e Truncate the in
1d290 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
1d2a0 20 66 69 6c 65 20 69 6d 61 67 65 20 74 6f 20 6e file image to n
1d2b0 50 61 67 65 20 70 61 67 65 73 2e 20 54 68 69 73 Page pages. This
1d2c0 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 64 6f .** function do
1d2d0 65 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 es not actually
1d2e0 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 modify the datab
1d2f0 61 73 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b ase file on disk
1d300 2e 20 49 74 20 0a 2a 2a 20 6a 75 73 74 20 73 65 . It .** just se
1d310 74 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 ts the internal
1d320 73 74 61 74 65 20 6f 66 20 74 68 65 20 70 61 67 state of the pag
1d330 65 72 20 6f 62 6a 65 63 74 20 73 6f 20 74 68 61 er object so tha
1d340 74 20 74 68 65 20 0a 2a 2a 20 74 72 75 6e 63 61 t the .** trunca
1d350 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 64 6f 6e tion will be don
1d360 65 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 e when the curre
1d370 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 nt transaction i
1d380 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2f 0a s committed..*/.
1d390 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
1d3a0 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 50 rTruncateImage(P
1d3b0 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 ager *pPager, Pg
1d3c0 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 61 73 73 no nPage){. ass
1d3d0 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 ert( pPager->dbS
1d3e0 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 61 73 izeValid );. as
1d3f0 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 sert( pPager->db
1d400 53 69 7a 65 3e 3d 6e 50 61 67 65 20 29 3b 0a 20 Size>=nPage );.
1d410 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
1d420 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 >state>=PAGER_RE
1d430 53 45 52 56 45 44 20 29 3b 0a 20 20 70 50 61 67 SERVED );. pPag
1d440 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61 er->dbSize = nPa
1d450 67 65 3b 0a 20 20 61 73 73 65 72 74 54 72 75 6e ge;. assertTrun
1d460 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 cateConstraint(p
1d470 50 61 67 65 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a Pager);.}.../*.*
1d480 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
1d490 69 73 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 is called before
1d4a0 20 61 74 74 65 6d 70 74 69 6e 67 20 61 20 68 6f attempting a ho
1d4b0 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 t-journal rollba
1d4c0 63 6b 2e 20 49 74 0a 2a 2a 20 73 79 6e 63 73 20 ck. It.** syncs
1d4d0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
1d4e0 20 74 6f 20 64 69 73 6b 2c 20 74 68 65 6e 20 73 to disk, then s
1d4f0 65 74 73 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 ets pPager->jour
1d500 6e 61 6c 48 64 72 20 74 6f 20 74 68 65 0a 2a 2a nalHdr to the.**
1d510 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 size of the jou
1d520 72 6e 61 6c 20 66 69 6c 65 20 73 6f 20 74 68 61 rnal file so tha
1d530 74 20 74 68 65 20 70 61 67 65 72 5f 70 6c 61 79 t the pager_play
1d540 62 61 63 6b 28 29 20 72 6f 75 74 69 6e 65 20 6b back() routine k
1d550 6e 6f 77 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 nows.** that the
1d560 20 65 6e 74 69 72 65 20 6a 6f 75 72 6e 61 6c 20 entire journal
1d570 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 73 79 file has been sy
1d580 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 79 6e 63 nced..**.** Sync
1d590 69 6e 67 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 ing a hot-journa
1d5a0 6c 20 74 6f 20 64 69 73 6b 20 62 65 66 6f 72 65 l to disk before
1d5b0 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 72 attempting to r
1d5c0 6f 6c 6c 20 69 74 20 62 61 63 6b 20 65 6e 73 75 oll it back ensu
1d5d0 72 65 73 20 0a 2a 2a 20 74 68 61 74 20 69 66 20 res .** that if
1d5e0 61 20 70 6f 77 65 72 2d 66 61 69 6c 75 72 65 20 a power-failure
1d5f0 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 occurs during th
1d600 65 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20 e rollback, the
1d610 70 72 6f 63 65 73 73 20 74 68 61 74 0a 2a 2a 20 process that.**
1d620 61 74 74 65 6d 70 74 73 20 72 6f 6c 6c 62 61 63 attempts rollbac
1d630 6b 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 73 74 k following syst
1d640 65 6d 20 72 65 63 6f 76 65 72 79 20 73 65 65 73 em recovery sees
1d650 20 74 68 65 20 73 61 6d 65 20 6a 6f 75 72 6e 61 the same journa
1d660 6c 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 73 20 l.** content as
1d670 74 68 69 73 20 70 72 6f 63 65 73 73 2e 0a 2a 2a this process..**
1d680 0a 2a 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e .** If everythin
1d690 67 20 67 6f 65 73 20 61 73 20 70 6c 61 6e 6e 65 g goes as planne
1d6a0 64 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 d, SQLITE_OK is
1d6b0 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 returned. Otherw
1d6c0 69 73 65 2c 20 0a 2a 2a 20 61 6e 20 53 51 4c 69 ise, .** an SQLi
1d6d0 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a te error code..*
1d6e0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
1d6f0 65 72 53 79 6e 63 48 6f 74 4a 6f 75 72 6e 61 6c erSyncHotJournal
1d700 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
1d710 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1d720 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21 70 50 TE_OK;. if( !pP
1d730 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a ager->noSync ){.
1d740 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1d750 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a OsSync(pPager->j
1d760 66 64 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f fd, SQLITE_SYNC_
1d770 4e 4f 52 4d 41 4c 29 3b 0a 20 20 7d 0a 20 20 69 NORMAL);. }. i
1d780 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1d790 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
1d7a0 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 ite3OsFileSize(p
1d7b0 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 70 50 61 Pager->jfd, &pPa
1d7c0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 29 ger->journalHdr)
1d7d0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1d7e0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 c;.}../*.** Shut
1d7f0 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 down the page ca
1d800 63 68 65 2e 20 20 46 72 65 65 20 61 6c 6c 20 6d che. Free all m
1d810 65 6d 6f 72 79 20 61 6e 64 20 63 6c 6f 73 65 20 emory and close
1d820 61 6c 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a all files..**.**
1d830 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f If a transactio
1d840 6e 20 77 61 73 20 69 6e 20 70 72 6f 67 72 65 73 n was in progres
1d850 73 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 s when this rout
1d860 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 ine is called, t
1d870 68 61 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 hat.** transacti
1d880 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 on is rolled bac
1d890 6b 2e 20 20 41 6c 6c 20 6f 75 74 73 74 61 6e 64 k. All outstand
1d8a0 69 6e 67 20 70 61 67 65 73 20 61 72 65 20 69 6e ing pages are in
1d8b0 76 61 6c 69 64 61 74 65 64 0a 2a 2a 20 61 6e 64 validated.** and
1d8c0 20 74 68 65 69 72 20 6d 65 6d 6f 72 79 20 69 73 their memory is
1d8d0 20 66 72 65 65 64 2e 20 20 41 6e 79 20 61 74 74 freed. Any att
1d8e0 65 6d 70 74 20 74 6f 20 75 73 65 20 61 20 70 61 empt to use a pa
1d8f0 67 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a ge associated.**
1d900 20 77 69 74 68 20 74 68 69 73 20 70 61 67 65 20 with this page
1d910 63 61 63 68 65 20 61 66 74 65 72 20 74 68 69 73 cache after this
1d920 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
1d930 73 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 0a 2a 2a s will likely.**
1d940 20 72 65 73 75 6c 74 20 69 6e 20 61 20 63 6f 72 result in a cor
1d950 65 64 75 6d 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 edump..**.** Thi
1d960 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 s function alway
1d970 73 20 73 75 63 63 65 65 64 73 2e 20 49 66 20 61 s succeeds. If a
1d980 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1d990 61 63 74 69 76 65 20 61 6e 20 61 74 74 65 6d 70 active an attemp
1d9a0 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 t.** is made to
1d9b0 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 49 66 roll it back. If
1d9c0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
1d9d0 20 64 75 72 69 6e 67 20 74 68 65 20 72 6f 6c 6c during the roll
1d9e0 62 61 63 6b 20 0a 2a 2a 20 61 20 68 6f 74 20 6a back .** a hot j
1d9f0 6f 75 72 6e 61 6c 20 6d 61 79 20 62 65 20 6c 65 ournal may be le
1da00 66 74 20 69 6e 20 74 68 65 20 66 69 6c 65 73 79 ft in the filesy
1da10 73 74 65 6d 20 62 75 74 20 6e 6f 20 65 72 72 6f stem but no erro
1da20 72 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a r is returned.**
1da30 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a to the caller..
1da40 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 61 */.int sqlite3Pa
1da50 67 65 72 43 6c 6f 73 65 28 50 61 67 65 72 20 2a gerClose(Pager *
1da60 70 50 61 67 65 72 29 7b 0a 20 20 75 38 20 2a 70 pPager){. u8 *p
1da70 54 6d 70 20 3d 20 28 75 38 20 2a 29 70 50 61 67 Tmp = (u8 *)pPag
1da80 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 0a er->pTmpSpace;..
1da90 20 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 disable_simula
1daa0 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b ted_io_errors();
1dab0 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 . sqlite3BeginB
1dac0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 enignMalloc();.
1dad0 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
1dae0 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e = 0;. pPager->
1daf0 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 exclusiveMode =
1db00 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 0;.#ifndef SQLIT
1db10 45 5f 4f 4d 49 54 5f 57 41 4c 0a 20 20 73 71 6c E_OMIT_WAL. sql
1db20 69 74 65 33 57 61 6c 43 6c 6f 73 65 28 70 50 61 ite3WalClose(pPa
1db30 67 65 72 2d 3e 70 57 61 6c 2c 0a 20 20 20 20 28 ger->pWal,. (
1db40 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3f pPager->noSync ?
1db50 20 30 20 3a 20 70 50 61 67 65 72 2d 3e 73 79 6e 0 : pPager->syn
1db60 63 5f 66 6c 61 67 73 29 2c 20 0a 20 20 20 20 70 c_flags), . p
1db70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c Pager->pageSize,
1db80 20 70 54 6d 70 0a 20 20 29 3b 0a 20 20 70 50 61 pTmp. );. pPa
1db90 67 65 72 2d 3e 70 57 61 6c 20 3d 20 30 3b 0a 23 ger->pWal = 0;.#
1dba0 65 6e 64 69 66 0a 20 20 70 61 67 65 72 5f 72 65 endif. pager_re
1dbb0 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 69 set(pPager);. i
1dbc0 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 f( MEMDB ){.
1dbd0 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 pager_unlock(pPa
1dbe0 67 65 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ger);. }else{.
1dbf0 20 20 20 2f 2a 20 53 65 74 20 50 61 67 65 72 2e /* Set Pager.
1dc00 6a 6f 75 72 6e 61 6c 48 64 72 20 74 6f 20 2d 31 journalHdr to -1
1dc10 20 66 6f 72 20 74 68 65 20 62 65 6e 65 66 69 74 for the benefit
1dc20 20 6f 66 20 74 68 65 20 70 61 67 65 72 5f 70 6c of the pager_pl
1dc30 61 79 62 61 63 6b 28 29 20 0a 20 20 20 20 2a 2a ayback() . **
1dc40 20 63 61 6c 6c 20 77 68 69 63 68 20 6d 61 79 20 call which may
1dc50 62 65 20 6d 61 64 65 20 66 72 6f 6d 20 77 69 74 be made from wit
1dc60 68 69 6e 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 hin pagerUnlockA
1dc70 6e 64 52 6f 6c 6c 62 61 63 6b 28 29 2e 20 49 66 ndRollback(). If
1dc80 20 69 74 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f it. ** is no
1dc90 74 20 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 75 t -1, then the u
1dca0 6e 73 79 6e 63 65 64 20 70 6f 72 74 69 6f 6e 20 nsynced portion
1dcb0 6f 66 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e of an open journ
1dcc0 61 6c 20 66 69 6c 65 20 6d 61 79 0a 20 20 20 20 al file may.
1dcd0 2a 2a 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 ** be played bac
1dce0 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 k into the datab
1dcf0 61 73 65 2e 20 49 66 20 61 20 70 6f 77 65 72 20 ase. If a power
1dd00 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77 failure occurs w
1dd10 68 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 hile. ** this
1dd20 20 69 73 20 68 61 70 70 65 6e 69 6e 67 2c 20 74 is happening, t
1dd30 68 65 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 he database may
1dd40 62 65 63 6f 6d 65 20 63 6f 72 72 75 70 74 2e 0a become corrupt..
1dd50 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 */. if( i
1dd60 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
1dd70 64 29 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 d) ){. pPag
1dd80 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 61 er->errCode = pa
1dd90 67 65 72 53 79 6e 63 48 6f 74 4a 6f 75 72 6e 61 gerSyncHotJourna
1dda0 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d l(pPager);. }
1ddb0 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b . pagerUnlock
1ddc0 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 AndRollback(pPag
1ddd0 65 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 er);. }. sqlit
1dde0 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f e3EndBenignMallo
1ddf0 63 28 29 3b 0a 20 20 65 6e 61 62 6c 65 5f 73 69 c();. enable_si
1de00 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 mulated_io_error
1de10 73 28 29 3b 0a 20 20 50 41 47 45 52 54 52 41 43 s();. PAGERTRAC
1de20 45 28 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c E(("CLOSE %d\n",
1de30 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 PAGERID(pPager)
1de40 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 ));. IOTRACE(("
1de50 43 4c 4f 53 45 20 25 70 5c 6e 22 2c 20 70 50 61 CLOSE %p\n", pPa
1de60 67 65 72 29 29 0a 20 20 73 71 6c 69 74 65 33 4f ger)). sqlite3O
1de70 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a sClose(pPager->j
1de80 66 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 fd);. sqlite3Os
1de90 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 66 64 Close(pPager->fd
1dea0 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 );. sqlite3Page
1deb0 46 72 65 65 28 70 54 6d 70 29 3b 0a 20 20 73 71 Free(pTmp);. sq
1dec0 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 lite3PcacheClose
1ded0 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
1dee0 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );..#ifdef SQLIT
1def0 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69 66 E_HAS_CODEC. if
1df00 28 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 ( pPager->xCodec
1df10 46 72 65 65 20 29 20 70 50 61 67 65 72 2d 3e 78 Free ) pPager->x
1df20 43 6f 64 65 63 46 72 65 65 28 70 50 61 67 65 72 CodecFree(pPager
1df30 2d 3e 70 43 6f 64 65 63 29 3b 0a 23 65 6e 64 69 ->pCodec);.#endi
1df40 66 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 f.. assert( !pP
1df50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 ager->aSavepoint
1df60 20 26 26 20 21 70 50 61 67 65 72 2d 3e 70 49 6e && !pPager->pIn
1df70 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 Journal );. ass
1df80 65 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 ert( !isOpen(pPa
1df90 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 21 69 73 ger->jfd) && !is
1dfa0 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 Open(pPager->sjf
1dfb0 64 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 d) );.. sqlite3
1dfc0 5f 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 _free(pPager);.
1dfd0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1dfe0 4b 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e K;.}..#if !defin
1dff0 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 ed(NDEBUG) || de
1e000 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 fined(SQLITE_TES
1e010 54 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 T)./*.** Return
1e020 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 the page number
1e030 66 6f 72 20 70 61 67 65 20 70 50 67 2e 0a 2a 2f for page pPg..*/
1e040 0a 50 67 6e 6f 20 73 71 6c 69 74 65 33 50 61 67 .Pgno sqlite3Pag
1e050 65 72 50 61 67 65 6e 75 6d 62 65 72 28 44 62 50 erPagenumber(DbP
1e060 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 age *pPg){. ret
1e070 75 72 6e 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 7d urn pPg->pgno;.}
1e080 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 .#endif../*.** I
1e090 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 ncrement the ref
1e0a0 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 erence count for
1e0b0 20 70 61 67 65 20 70 50 67 2e 0a 2a 2f 0a 76 6f page pPg..*/.vo
1e0c0 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 id sqlite3PagerR
1e0d0 65 66 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b ef(DbPage *pPg){
1e0e0 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 . sqlite3Pcache
1e0f0 52 65 66 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a 0a Ref(pPg);.}../*.
1e100 2a 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 ** Sync the jour
1e110 6e 61 6c 2e 20 49 6e 20 6f 74 68 65 72 20 77 6f nal. In other wo
1e120 72 64 73 2c 20 6d 61 6b 65 20 73 75 72 65 20 61 rds, make sure a
1e130 6c 6c 20 74 68 65 20 70 61 67 65 73 20 74 68 61 ll the pages tha
1e140 74 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 77 t have.** been w
1e150 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f ritten to the jo
1e160 75 72 6e 61 6c 20 68 61 76 65 20 61 63 74 75 61 urnal have actua
1e170 6c 6c 79 20 72 65 61 63 68 65 64 20 74 68 65 20 lly reached the
1e180 73 75 72 66 61 63 65 20 6f 66 20 74 68 65 0a 2a surface of the.*
1e190 2a 20 64 69 73 6b 20 61 6e 64 20 63 61 6e 20 62 * disk and can b
1e1a0 65 20 72 65 73 74 6f 72 65 64 20 69 6e 20 74 68 e restored in th
1e1b0 65 20 65 76 65 6e 74 20 6f 66 20 61 20 68 6f 74 e event of a hot
1e1c0 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 -journal rollbac
1e1d0 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 k..**.** If the
1e1e0 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 Pager.needSync f
1e1f0 6c 61 67 20 69 73 20 6e 6f 74 20 73 65 74 2c 20 lag is not set,
1e200 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 then this functi
1e210 6f 6e 20 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 on is a.** no-op
1e220 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 . Otherwise, the
1e230 20 61 63 74 69 6f 6e 73 20 72 65 71 75 69 72 65 actions require
1e240 64 20 64 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 d depend on the
1e250 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 0a 2a 2a 20 journal-mode.**
1e260 61 6e 64 20 74 68 65 20 64 65 76 69 63 65 20 63 and the device c
1e270 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 6f haracteristics o
1e280 66 20 74 68 65 20 74 68 65 20 66 69 6c 65 2d 73 f the the file-s
1e290 79 73 74 65 6d 2c 20 61 73 20 66 6f 6c 6c 6f 77 ystem, as follow
1e2a0 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 s:.**.** * If
1e2b0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
1e2c0 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 is an in-memory
1e2d0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6e journal file, n
1e2e0 6f 20 61 63 74 69 6f 6e 20 6e 65 65 64 0a 2a 2a o action need.**
1e2f0 20 20 20 20 20 62 65 20 74 61 6b 65 6e 2e 0a 2a be taken..*
1e300 2a 0a 2a 2a 20 20 20 2a 20 4f 74 68 65 72 77 69 *.** * Otherwi
1e310 73 65 2c 20 69 66 20 74 68 65 20 64 65 76 69 63 se, if the devic
1e320 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f e does not suppo
1e330 72 74 20 74 68 65 20 53 41 46 45 5f 41 50 50 45 rt the SAFE_APPE
1e340 4e 44 20 70 72 6f 70 65 72 74 79 2c 0a 2a 2a 20 ND property,.**
1e350 20 20 20 20 74 68 65 6e 20 74 68 65 20 6e 52 65 then the nRe
1e360 63 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 6d c field of the m
1e370 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 77 72 69 ost recently wri
1e380 74 74 65 6e 20 6a 6f 75 72 6e 61 6c 20 68 65 61 tten journal hea
1e390 64 65 72 0a 2a 2a 20 20 20 20 20 69 73 20 75 70 der.** is up
1e3a0 64 61 74 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e dated to contain
1e3b0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6a the number of j
1e3c0 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 73 20 74 ournal records t
1e3d0 68 61 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 hat have.**
1e3e0 62 65 65 6e 20 77 72 69 74 74 65 6e 20 66 6f 6c been written fol
1e3f0 6c 6f 77 69 6e 67 20 69 74 2e 20 49 66 20 74 68 lowing it. If th
1e400 65 20 70 61 67 65 72 20 69 73 20 6f 70 65 72 61 e pager is opera
1e410 74 69 6e 67 20 69 6e 20 66 75 6c 6c 2d 73 79 6e ting in full-syn
1e420 63 0a 2a 2a 20 20 20 20 20 6d 6f 64 65 2c 20 74 c.** mode, t
1e430 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hen the journal
1e440 66 69 6c 65 20 69 73 20 73 79 6e 63 65 64 20 62 file is synced b
1e450 65 66 6f 72 65 20 74 68 69 73 20 66 69 65 6c 64 efore this field
1e460 20 69 73 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a is updated..**.
1e470 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 64 65 ** * If the de
1e480 76 69 63 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 vice does not su
1e490 70 70 6f 72 74 20 74 68 65 20 53 45 51 55 45 4e pport the SEQUEN
1e4a0 54 49 41 4c 20 70 72 6f 70 65 72 74 79 2c 20 74 TIAL property, t
1e4b0 68 65 6e 20 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 hen .** jour
1e4c0 6e 61 6c 20 66 69 6c 65 20 69 73 20 73 79 6e 63 nal file is sync
1e4d0 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 6e ed..**.** Or, in
1e4e0 20 70 73 65 75 64 6f 2d 63 6f 64 65 3a 0a 2a 2a pseudo-code:.**
1e4f0 0a 2a 2a 20 20 20 69 66 28 20 4e 4f 54 20 3c 69 .** if( NOT <i
1e500 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c n-memory journal
1e510 3e 20 29 7b 0a 2a 2a 20 20 20 20 20 69 66 28 20 > ){.** if(
1e520 4e 4f 54 20 53 41 46 45 5f 41 50 50 45 4e 44 20 NOT SAFE_APPEND
1e530 29 7b 0a 2a 2a 20 20 20 20 20 20 20 69 66 28 20 ){.** if(
1e540 3c 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 3e <full-sync mode>
1e550 20 29 20 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 ) xSync(<journa
1e560 6c 20 66 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 20 l file>);.**
1e570 20 20 20 3c 75 70 64 61 74 65 20 6e 52 65 63 20 <update nRec
1e580 66 69 65 6c 64 3e 0a 2a 2a 20 20 20 20 20 7d 20 field>.** }
1e590 0a 2a 2a 20 20 20 20 20 69 66 28 20 4e 4f 54 20 .** if( NOT
1e5a0 53 45 51 55 45 4e 54 49 41 4c 20 29 20 78 53 79 SEQUENTIAL ) xSy
1e5b0 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 nc(<journal file
1e5c0 3e 29 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a >);.** }.**.**
1e5d0 20 54 68 65 20 50 61 67 65 72 2e 6e 65 65 64 53 The Pager.needS
1e5e0 79 6e 63 20 66 6c 61 67 20 69 73 20 6e 65 76 65 ync flag is neve
1e5f0 72 20 62 65 20 73 65 74 20 66 6f 72 20 74 65 6d r be set for tem
1e600 70 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 6f 72 porary files, or
1e610 20 61 6e 79 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 any.** file ope
1e620 72 61 74 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e rating in no-syn
1e630 63 20 6d 6f 64 65 20 28 50 61 67 65 72 2e 6e 6f c mode (Pager.no
1e640 53 79 6e 63 20 73 65 74 20 74 6f 20 6e 6f 6e 2d Sync set to non-
1e650 7a 65 72 6f 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 zero)..**.** If
1e660 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 69 73 successful, this
1e670 20 72 6f 75 74 69 6e 65 20 63 6c 65 61 72 73 20 routine clears
1e680 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 the PGHDR_NEED_S
1e690 59 4e 43 20 66 6c 61 67 20 6f 66 20 65 76 65 72 YNC flag of ever
1e6a0 79 20 0a 2a 2a 20 70 61 67 65 20 63 75 72 72 65 y .** page curre
1e6b0 6e 74 6c 79 20 68 65 6c 64 20 69 6e 20 6d 65 6d ntly held in mem
1e6c0 6f 72 79 20 62 65 66 6f 72 65 20 72 65 74 75 72 ory before retur
1e6d0 6e 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 ning SQLITE_OK.
1e6e0 49 66 20 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f If an IO.** erro
1e6f0 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 r is encountered
1e700 2c 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 , then the IO er
1e710 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
1e720 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c rned to the call
1e730 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e er..*/.static in
1e740 74 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 t syncJournal(Pa
1e750 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
1e760 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 if( pPager->need
1e770 53 79 6e 63 20 29 7b 0a 20 20 20 20 61 73 73 65 Sync ){. asse
1e780 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d rt( !pPager->tem
1e790 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 pFile );. if(
1e7a0 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
1e7b0 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode!=PAGER_JOUR
1e7c0 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 NALMODE_MEMORY )
1e7d0 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b 20 {. int rc;
1e7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e800 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
1e810 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 const int i
1e820 44 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 Dc = sqlite3OsDe
1e830 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
1e840 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b ics(pPager->fd);
1e850 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
1e860 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
1e870 64 29 20 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 d) );.. if(
1e880 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 0==(iDc&SQLITE_
1e890 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e IOCAP_SAFE_APPEN
1e8a0 44 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a D) ){. /*
1e8b0 20 54 68 69 73 20 62 6c 6f 63 6b 20 64 65 61 6c This block deal
1e8c0 73 20 77 69 74 68 20 61 6e 20 6f 62 73 63 75 72 s with an obscur
1e8d0 65 20 70 72 6f 62 6c 65 6d 2e 20 49 66 20 74 68 e problem. If th
1e8e0 65 20 6c 61 73 74 20 63 6f 6e 6e 65 63 74 69 6f e last connectio
1e8f0 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 n. ** tha
1e900 74 20 77 72 6f 74 65 20 74 6f 20 74 68 69 73 20 t wrote to this
1e910 64 61 74 61 62 61 73 65 20 77 61 73 20 6f 70 65 database was ope
1e920 72 61 74 69 6e 67 20 69 6e 20 70 65 72 73 69 73 rating in persis
1e930 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 0a 20 20 20 tent-journal.
1e940 20 20 20 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 ** mode, th
1e950 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 en the journal f
1e960 69 6c 65 20 6d 61 79 20 61 74 20 74 68 69 73 20 ile may at this
1e970 70 6f 69 6e 74 20 61 63 74 75 61 6c 6c 79 20 62 point actually b
1e980 65 20 6c 61 72 67 65 72 0a 20 20 20 20 20 20 20 e larger.
1e990 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 2e 6a ** than Pager.j
1e9a0 6f 75 72 6e 61 6c 4f 66 66 20 62 79 74 65 73 2e ournalOff bytes.
1e9b0 20 49 66 20 74 68 65 20 6e 65 78 74 20 74 68 69 If the next thi
1e9c0 6e 67 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 ng in the journa
1e9d0 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 69 6c l. ** fil
1e9e0 65 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 e happens to be
1e9f0 61 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 a journal-header
1ea00 20 28 77 72 69 74 74 65 6e 20 61 73 20 70 61 72 (written as par
1ea10 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 t of the.
1ea20 20 2a 2a 20 70 72 65 76 69 6f 75 73 20 63 6f 6e ** previous con
1ea30 6e 65 63 74 69 6f 6e 27 73 20 74 72 61 6e 73 61 nection's transa
1ea40 63 74 69 6f 6e 29 2c 20 61 6e 64 20 61 20 63 72 ction), and a cr
1ea50 61 73 68 20 6f 72 20 70 6f 77 65 72 2d 66 61 69 ash or power-fai
1ea60 6c 75 72 65 20 0a 20 20 20 20 20 20 20 20 2a 2a lure . **
1ea70 20 6f 63 63 75 72 73 20 61 66 74 65 72 20 6e 52 occurs after nR
1ea80 65 63 20 69 73 20 75 70 64 61 74 65 64 20 62 75 ec is updated bu
1ea90 74 20 62 65 66 6f 72 65 20 74 68 69 73 20 63 6f t before this co
1eaa0 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74 65 73 20 nnection writes
1eab0 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 74 . ** anyt
1eac0 68 69 6e 67 20 65 6c 73 65 20 74 6f 20 74 68 65 hing else to the
1ead0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f journal file (o
1eae0 72 20 63 6f 6d 6d 69 74 73 2f 72 6f 6c 6c 73 20 r commits/rolls
1eaf0 62 61 63 6b 20 69 74 73 20 0a 20 20 20 20 20 20 back its .
1eb00 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
1eb10 29 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 6d ), then SQLite m
1eb20 61 79 20 62 65 63 6f 6d 65 20 63 6f 6e 66 75 73 ay become confus
1eb30 65 64 20 77 68 65 6e 20 64 6f 69 6e 67 20 74 68 ed when doing th
1eb40 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 68 6f e . ** ho
1eb50 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 t-journal rollba
1eb60 63 6b 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 63 ck following rec
1eb70 6f 76 65 72 79 2e 20 49 74 20 6d 61 79 20 72 6f overy. It may ro
1eb80 6c 6c 20 62 61 63 6b 20 61 6c 6c 0a 20 20 20 20 ll back all.
1eb90 20 20 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 63 ** of this c
1eba0 6f 6e 6e 65 63 74 69 6f 6e 73 20 64 61 74 61 2c onnections data,
1ebb0 20 74 68 65 6e 20 70 72 6f 63 65 65 64 20 74 6f then proceed to
1ebc0 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 74 68 rolling back th
1ebd0 65 20 6f 6c 64 2c 0a 20 20 20 20 20 20 20 20 2a e old,. *
1ebe0 2a 20 6f 75 74 2d 6f 66 2d 64 61 74 65 20 64 61 * out-of-date da
1ebf0 74 61 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 ta that follows
1ec00 69 74 2e 20 44 61 74 61 62 61 73 65 20 63 6f 72 it. Database cor
1ec10 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 ruption..
1ec20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 **. ** T
1ec30 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 o work around th
1ec40 69 73 2c 20 69 66 20 74 68 65 20 6a 6f 75 72 6e is, if the journ
1ec50 61 6c 20 66 69 6c 65 20 64 6f 65 73 20 61 70 70 al file does app
1ec60 65 61 72 20 74 6f 20 63 6f 6e 74 61 69 6e 0a 20 ear to contain.
1ec70 20 20 20 20 20 20 20 2a 2a 20 61 20 76 61 6c 69 ** a vali
1ec80 64 20 68 65 61 64 65 72 20 66 6f 6c 6c 6f 77 69 d header followi
1ec90 6e 67 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c ng Pager.journal
1eca0 4f 66 66 2c 20 74 68 65 6e 20 77 72 69 74 65 20 Off, then write
1ecb0 61 20 30 78 30 30 0a 20 20 20 20 20 20 20 20 2a a 0x00. *
1ecc0 2a 20 62 79 74 65 20 74 6f 20 74 68 65 20 73 74 * byte to the st
1ecd0 61 72 74 20 6f 66 20 69 74 20 74 6f 20 70 72 65 art of it to pre
1ece0 76 65 6e 74 20 69 74 20 66 72 6f 6d 20 62 65 69 vent it from bei
1ecf0 6e 67 20 72 65 63 6f 67 6e 69 7a 65 64 2e 0a 20 ng recognized..
1ed00 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 **.
1ed10 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 20 69 4e ** Variable iN
1ed20 65 78 74 48 64 72 4f 66 66 73 65 74 20 69 73 20 extHdrOffset is
1ed30 73 65 74 20 74 6f 20 74 68 65 20 6f 66 66 73 65 set to the offse
1ed40 74 20 61 74 20 77 68 69 63 68 20 74 68 69 73 0a t at which this.
1ed50 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 62 6c ** probl
1ed60 65 6d 61 74 69 63 20 68 65 61 64 65 72 20 77 69 ematic header wi
1ed70 6c 6c 20 6f 63 63 75 72 2c 20 69 66 20 69 74 20 ll occur, if it
1ed80 65 78 69 73 74 73 2e 20 61 4d 61 67 69 63 20 69 exists. aMagic i
1ed90 73 20 75 73 65 64 20 0a 20 20 20 20 20 20 20 20 s used .
1eda0 2a 2a 20 61 73 20 61 20 74 65 6d 70 6f 72 61 72 ** as a temporar
1edb0 79 20 62 75 66 66 65 72 20 74 6f 20 69 6e 73 70 y buffer to insp
1edc0 65 63 74 20 74 68 65 20 66 69 72 73 74 20 63 6f ect the first co
1edd0 75 70 6c 65 20 6f 66 20 62 79 74 65 73 20 6f 66 uple of bytes of
1ede0 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 . ** the
1edf0 70 6f 74 65 6e 74 69 61 6c 20 6a 6f 75 72 6e 61 potential journa
1ee00 6c 20 68 65 61 64 65 72 2e 0a 20 20 20 20 20 20 l header..
1ee10 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 36 34 */. i64
1ee20 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 3b iNextHdrOffset;
1ee30 0a 20 20 20 20 20 20 20 20 75 38 20 61 4d 61 67 . u8 aMag
1ee40 69 63 5b 38 5d 3b 0a 20 20 20 20 20 20 20 20 75 ic[8];. u
1ee50 38 20 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 8 zHeader[sizeof
1ee60 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b (aJournalMagic)+
1ee70 34 5d 3b 0a 0a 20 20 20 20 20 20 20 20 6d 65 6d 4];.. mem
1ee80 63 70 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f cpy(zHeader, aJo
1ee90 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 urnalMagic, size
1eea0 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 of(aJournalMagic
1eeb0 29 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 33 ));. put3
1eec0 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 2bits(&zHeader[s
1eed0 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 izeof(aJournalMa
1eee0 67 69 63 29 5d 2c 20 70 50 61 67 65 72 2d 3e 6e gic)], pPager->n
1eef0 52 65 63 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 Rec);.. i
1ef00 4e 65 78 74 48 64 72 4f 66 66 73 65 74 20 3d 20 NextHdrOffset =
1ef10 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 journalHdrOffset
1ef20 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 (pPager);.
1ef30 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
1ef40 52 65 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 Read(pPager->jfd
1ef50 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 69 4e 65 , aMagic, 8, iNe
1ef60 78 74 48 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 xtHdrOffset);.
1ef70 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
1ef80 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 6d 65 LITE_OK && 0==me
1ef90 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f mcmp(aMagic, aJo
1efa0 75 72 6e 61 6c 4d 61 67 69 63 2c 20 38 29 20 29 urnalMagic, 8) )
1efb0 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74 {. stat
1efc0 69 63 20 63 6f 6e 73 74 20 75 38 20 7a 65 72 6f ic const u8 zero
1efd0 62 79 74 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 byte = 0;.
1efe0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1eff0 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e OsWrite(pPager->
1f000 6a 66 64 2c 20 26 7a 65 72 6f 62 79 74 65 2c 20 jfd, &zerobyte,
1f010 31 2c 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 1, iNextHdrOffse
1f020 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 t);. }.
1f030 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1f040 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 LITE_OK && rc!=S
1f050 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 QLITE_IOERR_SHOR
1f060 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 T_READ ){.
1f070 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1f080 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 }..
1f090 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e /* Write the n
1f0a0 52 65 63 20 76 61 6c 75 65 20 69 6e 74 6f 20 74 Rec value into t
1f0b0 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1f0c0 68 65 61 64 65 72 2e 20 49 66 20 69 6e 0a 20 20 header. If in.
1f0d0 20 20 20 20 20 20 2a 2a 20 66 75 6c 6c 2d 73 79 ** full-sy
1f0e0 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c 20 nchronous mode,
1f0f0 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c sync the journal
1f100 20 66 69 72 73 74 2e 20 54 68 69 73 20 65 6e 73 first. This ens
1f110 75 72 65 73 20 74 68 61 74 0a 20 20 20 20 20 20 ures that.
1f120 20 20 2a 2a 20 61 6c 6c 20 64 61 74 61 20 68 61 ** all data ha
1f130 73 20 72 65 61 6c 6c 79 20 68 69 74 20 74 68 65 s really hit the
1f140 20 64 69 73 6b 20 62 65 66 6f 72 65 20 6e 52 65 disk before nRe
1f150 63 20 69 73 20 75 70 64 61 74 65 64 20 74 6f 20 c is updated to
1f160 6d 61 72 6b 0a 20 20 20 20 20 20 20 20 2a 2a 20 mark. **
1f170 69 74 20 61 73 20 61 20 63 61 6e 64 69 64 61 74 it as a candidat
1f180 65 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a e for rollback..
1f190 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
1f1a0 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f ** This is no
1f1b0 74 20 72 65 71 75 69 72 65 64 20 69 66 20 74 68 t required if th
1f1c0 65 20 70 65 72 73 69 73 74 65 6e 74 20 6d 65 64 e persistent med
1f1d0 69 61 20 73 75 70 70 6f 72 74 73 20 74 68 65 0a ia supports the.
1f1e0 20 20 20 20 20 20 20 20 2a 2a 20 53 41 46 45 5f ** SAFE_
1f1f0 41 50 50 45 4e 44 20 70 72 6f 70 65 72 74 79 2e APPEND property.
1f200 20 42 65 63 61 75 73 65 20 69 6e 20 74 68 69 73 Because in this
1f210 20 63 61 73 65 20 69 74 20 69 73 20 6e 6f 74 20 case it is not
1f220 70 6f 73 73 69 62 6c 65 20 0a 20 20 20 20 20 20 possible .
1f230 20 20 2a 2a 20 66 6f 72 20 67 61 72 62 61 67 65 ** for garbage
1f240 20 64 61 74 61 20 74 6f 20 62 65 20 61 70 70 65 data to be appe
1f250 6e 64 65 64 20 74 6f 20 74 68 65 20 66 69 6c 65 nded to the file
1f260 2c 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 , the nRec field
1f270 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 70 . ** is p
1f280 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 30 78 opulated with 0x
1f290 46 46 46 46 46 46 46 46 20 77 68 65 6e 20 74 68 FFFFFFFF when th
1f2a0 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
1f2b0 20 69 73 20 77 72 69 74 74 65 6e 0a 20 20 20 20 is written.
1f2c0 20 20 20 20 2a 2a 20 61 6e 64 20 6e 65 76 65 72 ** and never
1f2d0 20 6e 65 65 64 73 20 74 6f 20 62 65 20 75 70 64 needs to be upd
1f2e0 61 74 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f ated.. */
1f2f0 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 . if( pPa
1f300 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 26 26 ger->fullSync &&
1f310 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 0==(iDc&SQLITE_
1f320 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c IOCAP_SEQUENTIAL
1f330 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50 ) ){. P
1f340 41 47 45 52 54 52 41 43 45 28 28 22 53 59 4e 43 AGERTRACE(("SYNC
1f350 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 64 5c 6e journal of %d\n
1f360 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 ", PAGERID(pPage
1f370 72 29 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 r)));.
1f380 49 4f 54 52 41 43 45 28 28 22 4a 53 59 4e 43 20 IOTRACE(("JSYNC
1f390 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a %p\n", pPager)).
1f3a0 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
1f3b0 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 qlite3OsSync(pPa
1f3c0 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 ger->jfd, pPager
1f3d0 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 ->sync_flags);.
1f3e0 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 if( rc!
1f3f0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 =SQLITE_OK ) ret
1f400 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 urn rc;.
1f410 7d 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 }. IOTRAC
1f420 45 28 28 22 4a 48 44 52 20 25 70 20 25 6c 6c 64 E(("JHDR %p %lld
1f430 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 61 \n", pPager, pPa
1f440 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 29 ger->journalHdr)
1f450 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
1f460 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 0a sqlite3OsWrite(.
1f470 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 67 pPag
1f480 65 72 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 er->jfd, zHeader
1f490 2c 20 73 69 7a 65 6f 66 28 7a 48 65 61 64 65 72 , sizeof(zHeader
1f4a0 29 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e ), pPager->journ
1f4b0 61 6c 48 64 72 0a 20 20 20 20 20 20 20 20 29 3b alHdr. );
1f4c0 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
1f4d0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 =SQLITE_OK ) ret
1f4e0 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
1f4f0 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44 if( 0==(iD
1f500 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 c&SQLITE_IOCAP_S
1f510 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 EQUENTIAL) ){.
1f520 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 PAGERTRACE
1f530 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 (("SYNC journal
1f540 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 of %d\n", PAGERI
1f550 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 D(pPager)));.
1f560 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a IOTRACE(("J
1f570 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 SYNC %p\n", pPag
1f580 65 72 29 29 0a 20 20 20 20 20 20 20 20 72 63 20 er)). rc
1f590 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 = sqlite3OsSync(
1f5a0 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 pPager->jfd, pPa
1f5b0 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 7c ger->sync_flags|
1f5c0 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 50 61 . (pPa
1f5d0 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 3d ger->sync_flags=
1f5e0 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c =SQLITE_SYNC_FUL
1f5f0 4c 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 L?SQLITE_SYNC_DA
1f600 54 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 20 20 TAONLY:0).
1f610 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 );. if(
1f620 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1f630 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
1f640 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f }. }.. /
1f650 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 * The journal fi
1f660 6c 65 20 77 61 73 20 6a 75 73 74 20 73 75 63 63 le was just succ
1f670 65 73 73 66 75 6c 6c 79 20 73 79 6e 63 65 64 2e essfully synced.
1f680 20 53 65 74 20 50 61 67 65 72 2e 6e 65 65 64 53 Set Pager.needS
1f690 79 6e 63 20 0a 20 20 20 20 2a 2a 20 74 6f 20 7a ync . ** to z
1f6a0 65 72 6f 20 61 6e 64 20 63 6c 65 61 72 20 74 68 ero and clear th
1f6b0 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e e PGHDR_NEED_SYN
1f6c0 43 20 66 6c 61 67 20 6f 6e 20 61 6c 6c 20 70 61 C flag on all pa
1f6d0 67 65 73 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 gess.. */.
1f6e0 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
1f6f0 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 c = 0;. pPage
1f700 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 r->journalStarte
1f710 64 20 3d 20 31 3b 0a 20 20 20 20 70 50 61 67 65 d = 1;. pPage
1f720 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 r->journalHdr =
1f730 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
1f740 66 66 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 ff;. sqlite3P
1f750 63 61 63 68 65 43 6c 65 61 72 53 79 6e 63 46 6c cacheClearSyncFl
1f760 61 67 73 28 70 50 61 67 65 72 2d 3e 70 50 43 61 ags(pPager->pPCa
1f770 63 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 che);. }.. ret
1f780 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1f790 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 ../*.** The argu
1f7a0 6d 65 6e 74 20 69 73 20 74 68 65 20 66 69 72 73 ment is the firs
1f7b0 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 t in a linked li
1f7c0 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 st of dirty page
1f7d0 73 20 63 6f 6e 6e 65 63 74 65 64 0a 2a 2a 20 62 s connected.** b
1f7e0 79 20 74 68 65 20 50 67 48 64 72 2e 70 44 69 72 y the PgHdr.pDir
1f7f0 74 79 20 70 6f 69 6e 74 65 72 2e 20 54 68 69 73 ty pointer. This
1f800 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 73 function writes
1f810 20 65 61 63 68 20 6f 6e 65 20 6f 66 20 74 68 65 each one of the
1f820 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 .** in-memory pa
1f830 67 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 ges in the list
1f840 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
1f850 66 69 6c 65 2e 20 54 68 65 20 61 72 67 75 6d 65 file. The argume
1f860 6e 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 4c nt may.** be NUL
1f870 4c 2c 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 L, representing
1f880 61 6e 20 65 6d 70 74 79 20 6c 69 73 74 2e 20 49 an empty list. I
1f890 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 69 73 n this case this
1f8a0 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 function is.**
1f8b0 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 a no-op..**.** T
1f8c0 68 65 20 70 61 67 65 72 20 6d 75 73 74 20 68 6f he pager must ho
1f8d0 6c 64 20 61 74 20 6c 65 61 73 74 20 61 20 52 45 ld at least a RE
1f8e0 53 45 52 56 45 44 20 6c 6f 63 6b 20 77 68 65 6e SERVED lock when
1f8f0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a this function.*
1f900 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20 42 65 66 * is called. Bef
1f910 6f 72 65 20 77 72 69 74 69 6e 67 20 61 6e 79 74 ore writing anyt
1f920 68 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 hing to the data
1f930 62 61 73 65 20 66 69 6c 65 2c 20 74 68 69 73 20 base file, this
1f940 6c 6f 63 6b 0a 2a 2a 20 69 73 20 75 70 67 72 61 lock.** is upgra
1f950 64 65 64 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 ded to an EXCLUS
1f960 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 IVE lock. If the
1f970 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20 lock cannot be
1f980 6f 62 74 61 69 6e 65 64 2c 0a 2a 2a 20 53 51 4c obtained,.** SQL
1f990 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 ITE_BUSY is retu
1f9a0 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 rned and no data
1f9b0 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 is written to t
1f9c0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1f9d0 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 ..** .** If the
1f9e0 70 61 67 65 72 20 69 73 20 61 20 74 65 6d 70 2d pager is a temp-
1f9f0 66 69 6c 65 20 70 61 67 65 72 20 61 6e 64 20 74 file pager and t
1fa00 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 2d 73 he actual file-s
1fa10 79 73 74 65 6d 20 66 69 6c 65 0a 2a 2a 20 69 73 ystem file.** is
1fa20 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e 2c 20 69 not yet open, i
1fa30 74 20 69 73 20 63 72 65 61 74 65 64 20 61 6e 64 t is created and
1fa40 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 61 opened before a
1fa50 6e 79 20 64 61 74 61 20 69 73 20 0a 2a 2a 20 77 ny data is .** w
1fa60 72 69 74 74 65 6e 20 6f 75 74 2e 0a 2a 2a 0a 2a ritten out..**.*
1fa70 2a 20 4f 6e 63 65 20 74 68 65 20 6c 6f 63 6b 20 * Once the lock
1fa80 68 61 73 20 62 65 65 6e 20 75 70 67 72 61 64 65 has been upgrade
1fa90 64 20 61 6e 64 2c 20 69 66 20 6e 65 63 65 73 73 d and, if necess
1faa0 61 72 79 2c 20 74 68 65 20 66 69 6c 65 20 6f 70 ary, the file op
1fab0 65 6e 65 64 2c 0a 2a 2a 20 74 68 65 20 70 61 67 ened,.** the pag
1fac0 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f es are written o
1fad0 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ut to the databa
1fae0 73 65 20 66 69 6c 65 20 69 6e 20 6c 69 73 74 20 se file in list
1faf0 6f 72 64 65 72 2e 20 57 72 69 74 69 6e 67 0a 2a order. Writing.*
1fb00 2a 20 61 20 70 61 67 65 20 69 73 20 73 6b 69 70 * a page is skip
1fb10 70 65 64 20 69 66 20 69 74 20 6d 65 65 74 73 20 ped if it meets
1fb20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 66 6f either of the fo
1fb30 6c 6c 6f 77 69 6e 67 20 63 72 69 74 65 72 69 61 llowing criteria
1fb40 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 :.**.** * The
1fb50 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 67 page number is g
1fb60 72 65 61 74 65 72 20 74 68 61 6e 20 50 61 67 65 reater than Page
1fb70 72 2e 64 62 53 69 7a 65 2c 20 6f 72 0a 2a 2a 20 r.dbSize, or.**
1fb80 20 20 2a 20 54 68 65 20 50 47 48 44 52 5f 44 4f * The PGHDR_DO
1fb90 4e 54 5f 57 52 49 54 45 20 66 6c 61 67 20 69 73 NT_WRITE flag is
1fba0 20 73 65 74 20 6f 6e 20 74 68 65 20 70 61 67 65 set on the page
1fbb0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 69 74 69 ..**.** If writi
1fbc0 6e 67 20 6f 75 74 20 61 20 70 61 67 65 20 63 61 ng out a page ca
1fbd0 75 73 65 73 20 74 68 65 20 64 61 74 61 62 61 73 uses the databas
1fbe0 65 20 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20 e file to grow,
1fbf0 50 61 67 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 Pager.dbFileSize
1fc00 0a 2a 2a 20 69 73 20 75 70 64 61 74 65 64 20 61 .** is updated a
1fc10 63 63 6f 72 64 69 6e 67 6c 79 2e 20 49 66 20 70 ccordingly. If p
1fc20 61 67 65 20 31 20 69 73 20 77 72 69 74 74 65 6e age 1 is written
1fc30 20 6f 75 74 2c 20 74 68 65 6e 20 74 68 65 20 76 out, then the v
1fc40 61 6c 75 65 20 63 61 63 68 65 64 0a 2a 2a 20 69 alue cached.** i
1fc50 6e 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 n Pager.dbFileVe
1fc60 72 73 5b 5d 20 69 73 20 75 70 64 61 74 65 64 20 rs[] is updated
1fc70 74 6f 20 6d 61 74 63 68 20 74 68 65 20 6e 65 77 to match the new
1fc80 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e value stored in
1fc90 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
1fca0 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 file..**.** If
1fcb0 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 everything is su
1fcc0 63 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 ccessful, SQLITE
1fcd0 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e _OK is returned.
1fce0 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 If an IO error
1fcf0 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 61 6e 20 49 .** occurs, an I
1fd00 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 O error code is
1fd10 72 65 74 75 72 6e 65 64 2e 20 4f 72 2c 20 69 66 returned. Or, if
1fd20 20 74 68 65 20 45 58 43 4c 55 53 49 56 45 20 6c the EXCLUSIVE l
1fd30 6f 63 6b 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 ock cannot.** be
1fd40 20 6f 62 74 61 69 6e 65 64 2c 20 53 51 4c 49 54 obtained, SQLIT
1fd50 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e E_BUSY is return
1fd60 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
1fd70 74 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 t pager_write_pa
1fd80 67 65 6c 69 73 74 28 50 67 48 64 72 20 2a 70 4c gelist(PgHdr *pL
1fd90 69 73 74 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 ist){. Pager *p
1fda0 50 61 67 65 72 3b 20 20 20 20 20 20 20 20 20 20 Pager;
1fdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1fdc0 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a Pager object */.
1fdd0 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
1fde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fdf0 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
1fe00 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 69 66 28 20 code */.. if(
1fe10 4e 45 56 45 52 28 70 4c 69 73 74 3d 3d 30 29 20 NEVER(pList==0)
1fe20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
1fe30 4f 4b 3b 0a 20 20 70 50 61 67 65 72 20 3d 20 70 OK;. pPager = p
1fe40 4c 69 73 74 2d 3e 70 50 61 67 65 72 3b 0a 0a 20 List->pPager;..
1fe50 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e /* At this poin
1fe60 74 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 65 t there may be e
1fe70 69 74 68 65 72 20 61 20 52 45 53 45 52 56 45 44 ither a RESERVED
1fe80 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f or EXCLUSIVE lo
1fe90 63 6b 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 ck on the. ** d
1fea0 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 atabase file. If
1feb0 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 there is alread
1fec0 79 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c y an EXCLUSIVE l
1fed0 6f 63 6b 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 ock, the followi
1fee0 6e 67 0a 20 20 2a 2a 20 63 61 6c 6c 20 69 73 20 ng. ** call is
1fef0 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 a no-op.. **.
1ff00 2a 2a 20 4d 6f 76 69 6e 67 20 74 68 65 20 6c 6f ** Moving the lo
1ff10 63 6b 20 66 72 6f 6d 20 52 45 53 45 52 56 45 44 ck from RESERVED
1ff20 20 74 6f 20 45 58 43 4c 55 53 49 56 45 20 61 63 to EXCLUSIVE ac
1ff30 74 75 61 6c 6c 79 20 69 6e 76 6f 6c 76 65 73 20 tually involves
1ff40 67 6f 69 6e 67 0a 20 20 2a 2a 20 74 68 72 6f 75 going. ** throu
1ff50 67 68 20 61 6e 20 69 6e 74 65 72 6d 65 64 69 61 gh an intermedia
1ff60 74 65 20 73 74 61 74 65 20 50 45 4e 44 49 4e 47 te state PENDING
1ff70 2e 20 20 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f . A PENDING lo
1ff80 63 6b 20 70 72 65 76 65 6e 74 73 20 6e 65 77 0a ck prevents new.
1ff90 20 20 2a 2a 20 72 65 61 64 65 72 73 20 66 72 6f ** readers fro
1ffa0 6d 20 61 74 74 61 63 68 69 6e 67 20 74 6f 20 74 m attaching to t
1ffb0 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20 he database but
1ffc0 69 73 20 75 6e 73 75 66 66 69 63 69 65 6e 74 20 is unsufficient
1ffd0 66 6f 72 20 75 73 20 74 6f 0a 20 20 2a 2a 20 77 for us to. ** w
1ffe0 72 69 74 65 2e 20 20 54 68 65 20 69 64 65 61 20 rite. The idea
1fff0 6f 66 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 of a PENDING loc
20000 6b 20 69 73 20 74 6f 20 70 72 65 76 65 6e 74 20 k is to prevent
20010 6e 65 77 20 72 65 61 64 65 72 73 20 66 72 6f 6d new readers from
20020 0a 20 20 2a 2a 20 63 6f 6d 69 6e 67 20 69 6e 20 . ** coming in
20030 77 68 69 6c 65 20 77 65 20 77 61 69 74 20 66 6f while we wait fo
20040 72 20 65 78 69 73 74 69 6e 67 20 72 65 61 64 65 r existing reade
20050 72 73 20 74 6f 20 63 6c 65 61 72 2e 0a 20 20 2a rs to clear.. *
20060 2a 0a 20 20 2a 2a 20 57 68 69 6c 65 20 74 68 65 *. ** While the
20070 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 65 pager is in the
20080 20 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2c RESERVED state,
20090 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 the original da
200a0 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 2a 2a tabase file. **
200b0 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 6e is unchanged an
200c0 64 20 77 65 20 63 61 6e 20 72 6f 6c 6c 62 61 63 d we can rollbac
200d0 6b 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 k without having
200e0 20 74 6f 20 70 6c 61 79 62 61 63 6b 20 74 68 65 to playback the
200f0 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 69 6e . ** journal in
20100 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 to the original
20110 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
20120 4f 6e 63 65 20 77 65 20 74 72 61 6e 73 69 74 69 Once we transiti
20130 6f 6e 20 74 6f 0a 20 20 2a 2a 20 45 58 43 4c 55 on to. ** EXCLU
20140 53 49 56 45 2c 20 69 74 20 6d 65 61 6e 73 20 74 SIVE, it means t
20150 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
20160 20 68 61 73 20 62 65 65 6e 20 63 68 61 6e 67 65 has been change
20170 64 20 61 6e 64 20 61 6e 79 20 72 6f 6c 6c 62 61 d and any rollba
20180 63 6b 0a 20 20 2a 2a 20 77 69 6c 6c 20 72 65 71 ck. ** will req
20190 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 20 70 uire a journal p
201a0 6c 61 79 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 layback.. */.
201b0 61 73 73 65 72 74 28 20 21 70 61 67 65 72 55 73 assert( !pagerUs
201c0 65 57 61 6c 28 70 4c 69 73 74 2d 3e 70 50 61 67 eWal(pList->pPag
201d0 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 er) );. assert(
201e0 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d pPager->state>=
201f0 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 PAGER_RESERVED )
20200 3b 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 ;. rc = pager_w
20210 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 ait_on_lock(pPag
20220 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f er, EXCLUSIVE_LO
20230 43 4b 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 CK);.. /* If th
20240 65 20 66 69 6c 65 20 69 73 20 61 20 74 65 6d 70 e file is a temp
20250 2d 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 79 65 -file has not ye
20260 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c 20 6f t been opened, o
20270 70 65 6e 20 69 74 20 6e 6f 77 2e 20 49 74 0a 20 pen it now. It.
20280 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 ** is not possi
20290 62 6c 65 20 66 6f 72 20 72 63 20 74 6f 20 62 65 ble for rc to be
202a0 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 other than SQLI
202b0 54 45 5f 4f 4b 20 69 66 20 74 68 69 73 20 62 72 TE_OK if this br
202c0 61 6e 63 68 0a 20 20 2a 2a 20 69 73 20 74 61 6b anch. ** is tak
202d0 65 6e 2c 20 61 73 20 70 61 67 65 72 5f 77 61 69 en, as pager_wai
202e0 74 5f 6f 6e 5f 6c 6f 63 6b 28 29 20 69 73 20 61 t_on_lock() is a
202f0 20 6e 6f 2d 6f 70 20 66 6f 72 20 74 65 6d 70 2d no-op for temp-
20300 66 69 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 files.. */. if
20310 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 ( !isOpen(pPager
20320 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 61 73 73 ->fd) ){. ass
20330 65 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65 6d ert( pPager->tem
20340 70 46 69 6c 65 20 26 26 20 72 63 3d 3d 53 51 4c pFile && rc==SQL
20350 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 63 ITE_OK );. rc
20360 20 3d 20 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 = pagerOpentemp
20370 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d (pPager, pPager-
20380 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 76 66 73 >fd, pPager->vfs
20390 46 6c 61 67 73 29 3b 0a 20 20 7d 0a 0a 20 20 2f Flags);. }.. /
203a0 2a 20 42 65 66 6f 72 65 20 74 68 65 20 66 69 72 * Before the fir
203b0 73 74 20 77 72 69 74 65 2c 20 67 69 76 65 20 74 st write, give t
203c0 68 65 20 56 46 53 20 61 20 68 69 6e 74 20 6f 66 he VFS a hint of
203d0 20 77 68 61 74 20 74 68 65 20 66 69 6e 61 6c 0a what the final.
203e0 20 20 2a 2a 20 66 69 6c 65 20 73 69 7a 65 20 77 ** file size w
203f0 69 6c 6c 20 62 65 2e 0a 20 20 2a 2f 0a 20 20 69 ill be.. */. i
20400 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a f( pPager->dbSiz
20410 65 20 3e 20 28 70 50 61 67 65 72 2d 3e 64 62 4f e > (pPager->dbO
20420 72 69 67 53 69 7a 65 2b 31 29 20 26 26 20 69 73 rigSize+1) && is
20430 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
20440 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
20450 69 6e 74 36 34 20 73 7a 46 69 6c 65 20 3d 20 70 int64 szFile = p
20460 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 Pager->pageSize
20470 2a 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 * (sqlite3_int64
20480 29 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b )pPager->dbSize;
20490 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69 . sqlite3OsFi
204a0 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 61 67 65 72 leControl(pPager
204b0 2d 3e 66 64 2c 20 53 51 4c 49 54 45 5f 46 43 4e ->fd, SQLITE_FCN
204c0 54 4c 5f 53 49 5a 45 5f 48 49 4e 54 2c 20 26 73 TL_SIZE_HINT, &s
204d0 7a 46 69 6c 65 29 3b 0a 20 20 7d 0a 0a 20 20 77 zFile);. }.. w
204e0 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 hile( rc==SQLITE
204f0 5f 4f 4b 20 26 26 20 70 4c 69 73 74 20 29 7b 0a _OK && pList ){.
20500 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 Pgno pgno =
20510 70 4c 69 73 74 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 pList->pgno;..
20520 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 /* If there ar
20530 65 20 64 69 72 74 79 20 70 61 67 65 73 20 69 6e e dirty pages in
20540 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20 the page cache
20550 77 69 74 68 20 70 61 67 65 20 6e 75 6d 62 65 72 with page number
20560 73 20 67 72 65 61 74 65 72 0a 20 20 20 20 2a 2a s greater. **
20570 20 74 68 61 6e 20 50 61 67 65 72 2e 64 62 53 69 than Pager.dbSi
20580 7a 65 2c 20 74 68 69 73 20 6d 65 61 6e 73 20 73 ze, this means s
20590 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 qlite3PagerTrunc
205a0 61 74 65 49 6d 61 67 65 28 29 20 77 61 73 20 63 ateImage() was c
205b0 61 6c 6c 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 alled to. **
205c0 6d 61 6b 65 20 74 68 65 20 66 69 6c 65 20 73 6d make the file sm
205d0 61 6c 6c 65 72 20 28 70 72 65 73 75 6d 61 62 6c aller (presumabl
205e0 79 20 62 79 20 61 75 74 6f 2d 76 61 63 75 75 6d y by auto-vacuum
205f0 20 63 6f 64 65 29 2e 20 44 6f 20 6e 6f 74 20 77 code). Do not w
20600 72 69 74 65 0a 20 20 20 20 2a 2a 20 61 6e 79 20 rite. ** any
20610 73 75 63 68 20 70 61 67 65 73 20 74 6f 20 74 68 such pages to th
20620 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 e file.. **.
20630 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 64 6f 20 6e ** Also, do n
20640 6f 74 20 77 72 69 74 65 20 6f 75 74 20 61 6e 79 ot write out any
20650 20 70 61 67 65 20 74 68 61 74 20 68 61 73 20 74 page that has t
20660 68 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 he PGHDR_DONT_WR
20670 49 54 45 20 66 6c 61 67 0a 20 20 20 20 2a 2a 20 ITE flag. **
20680 73 65 74 20 28 73 65 74 20 62 79 20 73 71 6c 69 set (set by sqli
20690 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 69 74 te3PagerDontWrit
206a0 65 28 29 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 e()).. */.
206b0 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67 65 if( pgno<=pPage
206c0 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 30 3d 3d r->dbSize && 0==
206d0 28 70 4c 69 73 74 2d 3e 66 6c 61 67 73 26 50 47 (pList->flags&PG
206e0 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 29 20 HDR_DONT_WRITE)
206f0 29 7b 0a 20 20 20 20 20 20 69 36 34 20 6f 66 66 ){. i64 off
20700 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 set = (pgno-1)*(
20710 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65 i64)pPager->page
20720 53 69 7a 65 3b 20 20 20 2f 2a 20 4f 66 66 73 65 Size; /* Offse
20730 74 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 t to write */.
20740 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 3b char *pData;
20750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20770 20 20 20 2f 2a 20 44 61 74 61 20 74 6f 20 77 72 /* Data to wr
20780 69 74 65 20 2a 2f 20 20 20 20 0a 0a 20 20 20 20 ite */ ..
20790 20 20 2f 2a 20 45 6e 63 6f 64 65 20 74 68 65 20 /* Encode the
207a0 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 database */.
207b0 20 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c CODEC2(pPager,
207c0 20 70 4c 69 73 74 2d 3e 70 44 61 74 61 2c 20 70 pList->pData, p
207d0 67 6e 6f 2c 20 36 2c 20 72 65 74 75 72 6e 20 53 gno, 6, return S
207e0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 QLITE_NOMEM, pDa
207f0 74 61 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 57 ta);.. /* W
20800 72 69 74 65 20 6f 75 74 20 74 68 65 20 70 61 67 rite out the pag
20810 65 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20 20 e data. */.
20820 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 rc = sqlite3OsW
20830 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c rite(pPager->fd,
20840 20 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e pData, pPager->
20850 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 74 pageSize, offset
20860 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 );.. /* If
20870 70 61 67 65 20 31 20 77 61 73 20 6a 75 73 74 20 page 1 was just
20880 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65 20 written, update
20890 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 Pager.dbFileVers
208a0 20 74 6f 20 6d 61 74 63 68 0a 20 20 20 20 20 20 to match.
208b0 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6e 6f 77 ** the value now
208c0 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64 stored in the d
208d0 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 atabase file. If
208e0 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 0a 20 writing this .
208f0 20 20 20 20 20 2a 2a 20 70 61 67 65 20 63 61 75 ** page cau
20900 73 65 64 20 74 68 65 20 64 61 74 61 62 61 73 65 sed the database
20910 20 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20 75 file to grow, u
20920 70 64 61 74 65 20 64 62 46 69 6c 65 53 69 7a 65 pdate dbFileSize
20930 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . . */.
20940 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b if( pgno==1 ){
20950 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 . memcpy(
20960 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 &pPager->dbFileV
20970 65 72 73 2c 20 26 70 44 61 74 61 5b 32 34 5d 2c ers, &pData[24],
20980 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e sizeof(pPager->
20990 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 dbFileVers));.
209a0 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
209b0 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 46 pgno>pPager->dbF
209c0 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 ileSize ){.
209d0 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c pPager->dbFil
209e0 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20 20 eSize = pgno;.
209f0 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 }.. /*
20a00 55 70 64 61 74 65 20 61 6e 79 20 62 61 63 6b 75 Update any backu
20a10 70 20 6f 62 6a 65 63 74 73 20 63 6f 70 79 69 6e p objects copyin
20a20 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f g the contents o
20a30 66 20 74 68 69 73 20 70 61 67 65 72 2e 20 2a 2f f this pager. */
20a40 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 61 . sqlite3Ba
20a50 63 6b 75 70 55 70 64 61 74 65 28 70 50 61 67 65 ckupUpdate(pPage
20a60 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67 6e 6f r->pBackup, pgno
20a70 2c 20 28 75 38 2a 29 70 4c 69 73 74 2d 3e 70 44 , (u8*)pList->pD
20a80 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 50 41 47 ata);.. PAG
20a90 45 52 54 52 41 43 45 28 28 22 53 54 4f 52 45 20 ERTRACE(("STORE
20aa0 25 64 20 70 61 67 65 20 25 64 20 68 61 73 68 28 %d page %d hash(
20ab0 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 %08x)\n",.
20ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47 PAG
20ad0 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 67 ERID(pPager), pg
20ae0 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 no, pager_pageha
20af0 73 68 28 70 4c 69 73 74 29 29 29 3b 0a 20 20 20 sh(pList)));.
20b00 20 20 20 49 4f 54 52 41 43 45 28 28 22 50 47 4f IOTRACE(("PGO
20b10 55 54 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 UT %p %d\n", pPa
20b20 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 ger, pgno));.
20b30 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 PAGER_INCR(sq
20b40 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 lite3_pager_writ
20b50 65 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 edb_count);.
20b60 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 PAGER_INCR(pPa
20b70 67 65 72 2d 3e 6e 57 72 69 74 65 29 3b 0a 20 20 ger->nWrite);.
20b80 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 50 }else{. P
20b90 41 47 45 52 54 52 41 43 45 28 28 22 4e 4f 53 54 AGERTRACE(("NOST
20ba0 4f 52 45 20 25 64 20 70 61 67 65 20 25 64 5c 6e ORE %d page %d\n
20bb0 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 ", PAGERID(pPage
20bc0 72 29 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20 r), pgno));.
20bd0 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f }.#ifdef SQLITE_
20be0 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 CHECK_PAGES.
20bf0 70 4c 69 73 74 2d 3e 70 61 67 65 48 61 73 68 20 pList->pageHash
20c00 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 = pager_pagehash
20c10 28 70 4c 69 73 74 29 3b 0a 23 65 6e 64 69 66 0a (pList);.#endif.
20c20 20 20 20 20 70 4c 69 73 74 20 3d 20 70 4c 69 73 pList = pLis
20c30 74 2d 3e 70 44 69 72 74 79 3b 0a 20 20 7d 0a 0a t->pDirty;. }..
20c40 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
20c50 2f 2a 0a 2a 2a 20 45 6e 73 75 72 65 20 74 68 61 /*.** Ensure tha
20c60 74 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 t the sub-journa
20c70 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 2e 20 l file is open.
20c80 49 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 If it is already
20c90 20 6f 70 65 6e 2c 20 74 68 69 73 20 0a 2a 2a 20 open, this .**
20ca0 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f function is a no
20cb0 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 -op..**.** SQLIT
20cc0 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
20cd0 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 67 if everything g
20ce0 6f 65 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f oes according to
20cf0 20 70 6c 61 6e 2e 20 41 6e 20 0a 2a 2a 20 53 51 plan. An .** SQ
20d00 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 20 65 LITE_IOERR_XXX e
20d10 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
20d20 75 72 6e 65 64 20 69 66 20 61 20 63 61 6c 6c 20 urned if a call
20d30 74 6f 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e to sqlite3OsOpen
20d40 28 29 20 0a 2a 2a 20 66 61 69 6c 73 2e 0a 2a 2f () .** fails..*/
20d50 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65 6e .static int open
20d60 53 75 62 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 SubJournal(Pager
20d70 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 *pPager){. int
20d80 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
20d90 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 . if( !isOpen(p
20da0 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a Pager->sjfd) ){.
20db0 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
20dc0 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 journalMode==PAG
20dd0 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d ER_JOURNALMODE_M
20de0 45 4d 4f 52 59 20 7c 7c 20 70 50 61 67 65 72 2d EMORY || pPager-
20df0 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 29 7b >subjInMemory ){
20e00 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 . sqlite3Me
20e10 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 mJournalOpen(pPa
20e20 67 65 72 2d 3e 73 6a 66 64 29 3b 0a 20 20 20 20 ger->sjfd);.
20e30 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
20e40 3d 20 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 = pagerOpentemp(
20e50 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e pPager, pPager->
20e60 73 6a 66 64 2c 20 53 51 4c 49 54 45 5f 4f 50 45 sjfd, SQLITE_OPE
20e70 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 29 3b 0a 20 N_SUBJOURNAL);.
20e80 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
20e90 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 n rc;.}../*.** A
20ea0 70 70 65 6e 64 20 61 20 72 65 63 6f 72 64 20 6f ppend a record o
20eb0 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 f the current st
20ec0 61 74 65 20 6f 66 20 70 61 67 65 20 70 50 67 20 ate of page pPg
20ed0 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e to the sub-journ
20ee0 61 6c 2e 20 0a 2a 2a 20 49 74 20 69 73 20 74 68 al. .** It is th
20ef0 65 20 63 61 6c 6c 65 72 73 20 72 65 73 70 6f 6e e callers respon
20f00 73 69 62 69 6c 69 74 79 20 74 6f 20 75 73 65 20 sibility to use
20f10 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 subjRequiresPage
20f20 28 29 20 74 6f 20 63 68 65 63 6b 20 0a 2a 2a 20 () to check .**
20f30 74 68 61 74 20 69 74 20 69 73 20 72 65 61 6c 6c that it is reall
20f40 79 20 72 65 71 75 69 72 65 64 20 62 65 66 6f 72 y required befor
20f50 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 e calling this f
20f60 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 unction..**.** I
20f70 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 65 f successful, se
20f80 74 20 74 68 65 20 62 69 74 20 63 6f 72 72 65 73 t the bit corres
20f90 70 6f 6e 64 69 6e 67 20 74 6f 20 70 50 67 2d 3e ponding to pPg->
20fa0 70 67 6e 6f 20 69 6e 20 74 68 65 20 62 69 74 76 pgno in the bitv
20fb0 65 63 73 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 6f ecs.** for all o
20fc0 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 62 pen savepoints b
20fd0 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e efore returning.
20fe0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
20ff0 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c tion returns SQL
21000 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 ITE_OK if everyt
21010 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 hing is successf
21020 75 6c 2c 20 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 ul, an IO.** err
21030 6f 72 20 63 6f 64 65 20 69 66 20 74 68 65 20 61 or code if the a
21040 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 ttempt to write
21050 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e to the sub-journ
21060 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a al fails, or .**
21070 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 SQLITE_NOMEM if
21080 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 a malloc fails
21090 77 68 69 6c 65 20 73 65 74 74 69 6e 67 20 61 20 while setting a
210a0 62 69 74 20 69 6e 20 61 20 73 61 76 65 70 6f 69 bit in a savepoi
210b0 6e 74 0a 2a 2a 20 62 69 74 76 65 63 2e 0a 2a 2f nt.** bitvec..*/
210c0 0a 73 74 61 74 69 63 20 69 6e 74 20 73 75 62 6a .static int subj
210d0 6f 75 72 6e 61 6c 50 61 67 65 28 50 67 48 64 72 ournalPage(PgHdr
210e0 20 2a 70 50 67 29 7b 0a 20 20 69 6e 74 20 72 63 *pPg){. int rc
210f0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
21100 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 Pager *pPager =
21110 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 pPg->pPager;. i
21120 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
21130 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f alMode!=PAGER_JO
21140 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b URNALMODE_OFF ){
21150 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 .. /* Open th
21160 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2c 20 69 e sub-journal, i
21170 66 20 69 74 20 68 61 73 20 6e 6f 74 20 61 6c 72 f it has not alr
21180 65 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65 64 eady been opened
21190 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
211a0 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e pPager->useJourn
211b0 61 6c 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 al );. assert
211c0 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
211d0 3e 6a 66 64 29 20 7c 7c 20 70 61 67 65 72 55 73 >jfd) || pagerUs
211e0 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 3b 0a eWal(pPager) );.
211f0 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 assert( isOp
21200 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 en(pPager->sjfd)
21210 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 || pPager->nSub
21220 52 65 63 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 Rec==0 );. as
21230 73 65 72 74 28 20 70 61 67 65 72 55 73 65 57 61 sert( pagerUseWa
21240 6c 28 70 50 61 67 65 72 29 20 0a 20 20 20 20 20 l(pPager) .
21250 20 20 20 20 7c 7c 20 70 61 67 65 49 6e 4a 6f 75 || pageInJou
21260 72 6e 61 6c 28 70 50 67 29 20 0a 20 20 20 20 20 rnal(pPg) .
21270 20 20 20 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f || pPg->pgno
21280 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 >pPager->dbOrigS
21290 69 7a 65 20 0a 20 20 20 20 29 3b 0a 20 20 20 20 ize . );.
212a0 72 63 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72 rc = openSubJour
212b0 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 nal(pPager);..
212c0 20 20 2f 2a 20 49 66 20 74 68 65 20 73 75 62 2d /* If the sub-
212d0 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6f 70 65 6e journal was open
212e0 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 ed successfully
212f0 28 6f 72 20 77 61 73 20 61 6c 72 65 61 64 79 20 (or was already
21300 6f 70 65 6e 29 2c 0a 20 20 20 20 2a 2a 20 77 72 open),. ** wr
21310 69 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ite the journal
21320 72 65 63 6f 72 64 20 69 6e 74 6f 20 74 68 65 20 record into the
21330 66 69 6c 65 2e 20 20 2a 2f 0a 20 20 20 20 69 66 file. */. if
21340 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
21350 29 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 70 ){. void *p
21360 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 74 Data = pPg->pDat
21370 61 3b 0a 20 20 20 20 20 20 69 36 34 20 6f 66 66 a;. i64 off
21380 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53 set = pPager->nS
21390 75 62 52 65 63 2a 28 34 2b 70 50 61 67 65 72 2d ubRec*(4+pPager-
213a0 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 >pageSize);.
213b0 20 20 63 68 61 72 20 2a 70 44 61 74 61 32 3b 0a char *pData2;.
213c0 20 20 0a 20 20 20 20 20 20 43 4f 44 45 43 32 28 . CODEC2(
213d0 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 pPager, pData, p
213e0 50 67 2d 3e 70 67 6e 6f 2c 20 37 2c 20 72 65 74 Pg->pgno, 7, ret
213f0 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
21400 2c 20 70 44 61 74 61 32 29 3b 0a 20 20 20 20 20 , pData2);.
21410 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53 54 PAGERTRACE(("ST
21420 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 MT-JOURNAL %d pa
21430 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 ge %d\n", PAGERI
21440 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e D(pPager), pPg->
21450 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 72 63 pgno));. rc
21460 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 = write32bits(p
21470 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 6f 66 66 Pager->sjfd, off
21480 73 65 74 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b set, pPg->pgno);
21490 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
214a0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
214b0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
214c0 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e OsWrite(pPager->
214d0 73 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50 sjfd, pData2, pP
214e0 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 ager->pageSize,
214f0 6f 66 66 73 65 74 2b 34 29 3b 0a 20 20 20 20 20 offset+4);.
21500 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 }. }. }. i
21510 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
21520 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
21530 6e 53 75 62 52 65 63 2b 2b 3b 0a 20 20 20 20 61 nSubRec++;. a
21540 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e ssert( pPager->n
21550 53 61 76 65 70 6f 69 6e 74 3e 30 20 29 3b 0a 20 Savepoint>0 );.
21560 20 20 20 72 63 20 3d 20 61 64 64 54 6f 53 61 76 rc = addToSav
21570 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 70 50 epointBitvecs(pP
21580 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 ager, pPg->pgno)
21590 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
215a0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
215b0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
215c0 6c 65 64 20 62 79 20 74 68 65 20 70 63 61 63 68 led by the pcach
215d0 65 20 6c 61 79 65 72 20 77 68 65 6e 20 69 74 20 e layer when it
215e0 68 61 73 20 72 65 61 63 68 65 64 20 73 6f 6d 65 has reached some
215f0 0a 2a 2a 20 73 6f 66 74 20 6d 65 6d 6f 72 79 20 .** soft memory
21600 6c 69 6d 69 74 2e 20 54 68 65 20 66 69 72 73 74 limit. The first
21610 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 argument is a p
21620 6f 69 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 ointer to a Page
21630 72 20 6f 62 6a 65 63 74 0a 2a 2a 20 28 63 61 73 r object.** (cas
21640 74 20 61 73 20 61 20 76 6f 69 64 2a 29 2e 20 54 t as a void*). T
21650 68 65 20 70 61 67 65 72 20 69 73 20 61 6c 77 61 he pager is alwa
21660 79 73 20 27 70 75 72 67 65 61 62 6c 65 27 20 28 ys 'purgeable' (
21670 6e 6f 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 not an in-memory
21680 0a 2a 2a 20 64 61 74 61 62 61 73 65 29 2e 20 54 .** database). T
21690 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
216a0 6e 74 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 nt is a referenc
216b0 65 20 74 6f 20 61 20 70 61 67 65 20 74 68 61 74 e to a page that
216c0 20 69 73 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c is .** currentl
216d0 79 20 64 69 72 74 79 20 62 75 74 20 68 61 73 20 y dirty but has
216e0 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 no outstanding r
216f0 65 66 65 72 65 6e 63 65 73 2e 20 54 68 65 20 70 eferences. The p
21700 61 67 65 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 age.** is always
21710 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
21720 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a 65 63 the Pager objec
21730 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 t passed as the
21740 66 69 72 73 74 20 0a 2a 2a 20 61 72 67 75 6d 65 first .** argume
21750 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f nt..**.** The jo
21760 62 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 b of this functi
21770 6f 6e 20 69 73 20 74 6f 20 6d 61 6b 65 20 70 50 on is to make pP
21780 67 20 63 6c 65 61 6e 20 62 79 20 77 72 69 74 69 g clean by writi
21790 6e 67 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 0a ng its contents.
217a0 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 ** out to the da
217b0 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69 66 20 tabase file, if
217c0 70 6f 73 73 69 62 6c 65 2e 20 54 68 69 73 20 6d possible. This m
217d0 61 79 20 69 6e 76 6f 6c 76 65 20 73 79 6e 63 69 ay involve synci
217e0 6e 67 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 ng the.** journa
217f0 6c 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 l file. .**.** I
21800 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 71 f successful, sq
21810 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 lite3PcacheMakeC
21820 6c 65 61 6e 28 29 20 69 73 20 63 61 6c 6c 65 64 lean() is called
21830 20 6f 6e 20 74 68 65 20 70 61 67 65 20 61 6e 64 on the page and
21840 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 .** SQLITE_OK re
21850 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f turned. If an IO
21860 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 error occurs wh
21870 69 6c 65 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 ile trying to ma
21880 6b 65 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 63 ke the.** page c
21890 6c 65 61 6e 2c 20 74 68 65 20 49 4f 20 65 72 72 lean, the IO err
218a0 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
218b0 6e 65 64 2e 20 49 66 20 74 68 65 20 70 61 67 65 ned. If the page
218c0 20 63 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20 6d 61 cannot be.** ma
218d0 64 65 20 63 6c 65 61 6e 20 66 6f 72 20 73 6f 6d de clean for som
218e0 65 20 6f 74 68 65 72 20 72 65 61 73 6f 6e 2c 20 e other reason,
218f0 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 but no error occ
21900 75 72 73 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 urs, then SQLITE
21910 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e _OK.** is return
21920 65 64 20 62 79 20 73 71 6c 69 74 65 33 50 63 61 ed by sqlite3Pca
21930 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 69 cheMakeClean() i
21940 73 20 6e 6f 74 20 63 61 6c 6c 65 64 2e 0a 2a 2f s not called..*/
21950 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 .static int page
21960 72 53 74 72 65 73 73 28 76 6f 69 64 20 2a 70 2c rStress(void *p,
21970 20 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 PgHdr *pPg){.
21980 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 Pager *pPager =
21990 28 50 61 67 65 72 20 2a 29 70 3b 0a 20 20 69 6e (Pager *)p;. in
219a0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
219b0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 ;.. assert( pPg
219c0 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67 65 72 ->pPager==pPager
219d0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
219e0 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 g->flags&PGHDR_D
219f0 49 52 54 59 20 29 3b 0a 0a 20 20 70 50 67 2d 3e IRTY );.. pPg->
21a00 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 69 66 pDirty = 0;. if
21a10 28 20 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 ( pagerUseWal(pP
21a20 61 67 65 72 29 20 29 7b 0a 20 20 20 20 2f 2a 20 ager) ){. /*
21a30 57 72 69 74 65 20 61 20 73 69 6e 67 6c 65 20 66 Write a single f
21a40 72 61 6d 65 20 66 6f 72 20 74 68 69 73 20 70 61 rame for this pa
21a50 67 65 20 74 6f 20 74 68 65 20 6c 6f 67 2e 20 2a ge to the log. *
21a60 2f 0a 20 20 20 20 69 66 28 20 73 75 62 6a 52 65 /. if( subjRe
21a70 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 20 quiresPage(pPg)
21a80 29 7b 20 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){ . rc = s
21a90 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 ubjournalPage(pP
21aa0 67 29 3b 20 0a 20 20 20 20 7d 0a 20 20 20 20 69 g); . }. i
21ab0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
21ac0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 ){. rc = p
21ad0 61 67 65 72 57 61 6c 46 72 61 6d 65 73 28 70 50 agerWalFrames(pP
21ae0 61 67 65 72 2c 20 70 50 67 2c 20 30 2c 20 30 2c ager, pPg, 0, 0,
21af0 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 0);. }. }el
21b00 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 se{. /* The d
21b10 6f 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20 69 73 oNotSync flag is
21b20 20 73 65 74 20 62 79 20 74 68 65 20 73 71 6c 69 set by the sqli
21b30 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 te3PagerWrite()
21b40 66 75 6e 63 74 69 6f 6e 20 77 68 69 6c 65 20 69 function while i
21b50 74 0a 20 20 20 20 2a 2a 20 69 73 20 6a 6f 75 72 t. ** is jour
21b60 6e 61 6c 6c 69 6e 67 20 61 20 73 65 74 20 6f 66 nalling a set of
21b70 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 64 61 74 two or more dat
21b80 61 62 61 73 65 20 70 61 67 65 73 20 74 68 61 74 abase pages that
21b90 20 61 72 65 20 73 74 6f 72 65 64 0a 20 20 20 20 are stored.
21ba0 2a 2a 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 ** on the same d
21bb0 69 73 6b 20 73 65 63 74 6f 72 2e 20 53 79 6e 63 isk sector. Sync
21bc0 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ing the journal
21bd0 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 77 is not allowed w
21be0 68 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 hile. ** this
21bf0 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20 61 73 is happening as
21c00 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 it is important
21c10 20 74 68 61 74 20 61 6c 6c 20 6d 65 6d 62 65 72 that all member
21c20 73 20 6f 66 20 73 75 63 68 20 61 0a 20 20 20 20 s of such a.
21c30 2a 2a 20 73 65 74 20 6f 66 20 70 61 67 65 73 20 ** set of pages
21c40 61 72 65 20 73 79 6e 63 65 64 20 74 6f 20 64 69 are synced to di
21c50 73 6b 20 74 6f 67 65 74 68 65 72 2e 20 53 6f 2c sk together. So,
21c60 20 69 66 20 74 68 65 20 70 61 67 65 20 74 68 69 if the page thi
21c70 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 2a s function. *
21c80 2a 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20 6d * is trying to m
21c90 61 6b 65 20 63 6c 65 61 6e 20 77 69 6c 6c 20 72 ake clean will r
21ca0 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c equire a journal
21cb0 20 73 79 6e 63 20 61 6e 64 20 74 68 65 20 64 6f sync and the do
21cc0 4e 6f 74 53 79 6e 63 0a 20 20 20 20 2a 2a 20 66 NotSync. ** f
21cd0 6c 61 67 20 69 73 20 73 65 74 2c 20 72 65 74 75 lag is set, retu
21ce0 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 rn without doing
21cf0 20 61 6e 79 74 68 69 6e 67 2e 20 54 68 65 20 70 anything. The p
21d00 63 61 63 68 65 20 6c 61 79 65 72 20 77 69 6c 6c cache layer will
21d10 0a 20 20 20 20 2a 2a 20 6a 75 73 74 20 68 61 76 . ** just hav
21d20 65 20 74 6f 20 67 6f 20 61 68 65 61 64 20 61 6e e to go ahead an
21d30 64 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 d allocate a new
21d40 20 70 61 67 65 20 62 75 66 66 65 72 20 69 6e 73 page buffer ins
21d50 74 65 61 64 20 6f 66 0a 20 20 20 20 2a 2a 20 72 tead of. ** r
21d60 65 75 73 69 6e 67 20 70 50 67 2e 0a 20 20 20 20 eusing pPg..
21d70 2a 2a 0a 20 20 20 20 2a 2a 20 53 69 6d 69 6c 61 **. ** Simila
21d80 72 6c 79 2c 20 69 66 20 74 68 65 20 70 61 67 65 rly, if the page
21d90 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 65 6e r has already en
21da0 74 65 72 65 64 20 74 68 65 20 65 72 72 6f 72 20 tered the error
21db0 73 74 61 74 65 2c 20 64 6f 20 6e 6f 74 0a 20 20 state, do not.
21dc0 20 20 2a 2a 20 74 72 79 20 74 6f 20 77 72 69 74 ** try to writ
21dd0 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f e the contents o
21de0 66 20 70 50 67 20 74 6f 20 64 69 73 6b 2e 0a 20 f pPg to disk..
21df0 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 4e 45 */. if( NE
21e00 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43 VER(pPager->errC
21e10 6f 64 65 29 0a 20 20 20 20 20 7c 7c 20 28 70 50 ode). || (pP
21e20 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 ager->doNotSync
21e30 26 26 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 && pPg->flags&PG
21e40 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 0a 20 HDR_NEED_SYNC).
21e50 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ){. retu
21e60 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
21e70 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 53 79 }. . /* Sy
21e80 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 nc the journal f
21e90 69 6c 65 20 69 66 20 72 65 71 75 69 72 65 64 2e ile if required.
21ea0 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 67 2d */. if( pPg-
21eb0 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
21ec0 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 20 20 D_SYNC ){.
21ed0 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c rc = syncJournal
21ee0 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 (pPager);.
21ef0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
21f00 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 66 75 6c K && pPager->ful
21f10 6c 53 79 6e 63 20 26 26 20 0a 20 20 20 20 20 20 lSync && .
21f20 20 20 21 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 !(pPager->jour
21f30 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
21f40 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 OURNALMODE_MEMOR
21f50 59 29 20 26 26 0a 20 20 20 20 20 20 20 20 21 28 Y) &&. !(
21f60 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 sqlite3OsDeviceC
21f70 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 haracteristics(p
21f80 50 61 67 65 72 2d 3e 66 64 29 26 53 51 4c 49 54 Pager->fd)&SQLIT
21f90 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 E_IOCAP_SAFE_APP
21fa0 45 4e 44 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 END). ){.
21fb0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 pPager->nR
21fc0 65 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 ec = 0;.
21fd0 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e 61 rc = writeJourna
21fe0 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a 20 20 lHdr(pPager);.
21ff0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 }. }. .
22000 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 /* If the pag
22010 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 69 73 e number of this
22020 20 70 61 67 65 20 69 73 20 6c 61 72 67 65 72 20 page is larger
22030 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 than the current
22040 20 73 69 7a 65 20 6f 66 0a 20 20 20 20 2a 2a 20 size of. **
22050 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 the database ima
22060 67 65 2c 20 69 74 20 6d 61 79 20 6e 65 65 64 20 ge, it may need
22070 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f to be written to
22080 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
22090 2e 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 .. ** This is
220a0 20 62 65 63 61 75 73 65 20 74 68 65 20 63 61 6c because the cal
220b0 6c 20 74 6f 20 70 61 67 65 72 5f 77 72 69 74 65 l to pager_write
220c0 5f 70 61 67 65 6c 69 73 74 28 29 20 62 65 6c 6f _pagelist() belo
220d0 77 20 77 69 6c 6c 20 6e 6f 74 0a 20 20 20 20 2a w will not. *
220e0 2a 20 61 63 74 75 61 6c 6c 79 20 77 72 69 74 65 * actually write
220f0 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 6c data to the fil
22100 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a e in this case..
22110 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6f **. ** Co
22120 6e 73 69 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f nsider the follo
22130 77 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66 wing sequence of
22140 20 65 76 65 6e 74 73 3a 0a 20 20 20 20 2a 2a 0a events:. **.
22150 20 20 20 20 2a 2a 20 20 20 42 45 47 49 4e 3b 0a ** BEGIN;.
22160 20 20 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 72 ** <jour
22170 6e 61 6c 20 70 61 67 65 20 58 3e 0a 20 20 20 20 nal page X>.
22180 2a 2a 20 20 20 20 20 3c 6d 6f 64 69 66 79 20 70 ** <modify p
22190 61 67 65 20 58 3e 0a 20 20 20 20 2a 2a 20 20 20 age X>. **
221a0 20 20 53 41 56 45 50 4f 49 4e 54 20 73 70 3b 0a SAVEPOINT sp;.
221b0 20 20 20 20 2a 2a 20 20 20 20 20 20 20 3c 73 68 ** <sh
221c0 72 69 6e 6b 20 64 61 74 61 62 61 73 65 20 66 69 rink database fi
221d0 6c 65 20 74 6f 20 59 20 70 61 67 65 73 3e 0a 20 le to Y pages>.
221e0 20 20 20 2a 2a 20 20 20 20 20 20 20 70 61 67 65 ** page
221f0 72 53 74 72 65 73 73 28 70 61 67 65 20 58 29 0a rStress(page X).
22200 20 20 20 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 ** ROLLB
22210 41 43 4b 20 54 4f 20 73 70 3b 0a 20 20 20 20 2a ACK TO sp;. *
22220 2a 0a 20 20 20 20 2a 2a 20 49 66 20 28 58 3e 59 *. ** If (X>Y
22230 29 2c 20 74 68 65 6e 20 77 68 65 6e 20 70 61 67 ), then when pag
22240 65 72 53 74 72 65 73 73 20 69 73 20 63 61 6c 6c erStress is call
22250 65 64 20 70 61 67 65 20 58 20 77 69 6c 6c 20 6e ed page X will n
22260 6f 74 20 62 65 20 77 72 69 74 74 65 6e 0a 20 20 ot be written.
22270 20 20 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65 20 ** out to the
22280 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 62 database file, b
22290 75 74 20 77 69 6c 6c 20 62 65 20 64 72 6f 70 70 ut will be dropp
222a0 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 ed from the cach
222b0 65 2e 20 54 68 65 6e 2c 0a 20 20 20 20 2a 2a 20 e. Then,. **
222c0 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 52 following the "R
222d0 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20 73 OLLBACK TO sp" s
222e0 74 61 74 65 6d 65 6e 74 2c 20 72 65 61 64 69 6e tatement, readin
222f0 67 20 70 61 67 65 20 58 20 77 69 6c 6c 20 72 65 g page X will re
22300 61 64 0a 20 20 20 20 2a 2a 20 64 61 74 61 20 66 ad. ** data f
22310 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
22320 20 66 69 6c 65 2e 20 54 68 69 73 20 77 69 6c 6c file. This will
22330 20 62 65 20 74 68 65 20 63 6f 70 79 20 6f 66 20 be the copy of
22340 70 61 67 65 20 58 20 61 73 20 69 74 0a 20 20 20 page X as it.
22350 20 2a 2a 20 77 61 73 20 77 68 65 6e 20 74 68 65 ** was when the
22360 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 transaction sta
22370 72 74 65 64 2c 20 6e 6f 74 20 61 73 20 69 74 20 rted, not as it
22380 77 61 73 20 77 68 65 6e 20 22 53 41 56 45 50 4f was when "SAVEPO
22390 49 4e 54 20 73 70 22 0a 20 20 20 20 2a 2a 20 77 INT sp". ** w
223a0 61 73 20 65 78 65 63 75 74 65 64 2e 0a 20 20 20 as executed..
223b0 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 **. ** The s
223c0 6f 6c 75 74 69 6f 6e 20 69 73 20 74 6f 20 77 72 olution is to wr
223d0 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ite the current
223e0 64 61 74 61 20 66 6f 72 20 70 61 67 65 20 58 20 data for page X
223f0 69 6e 74 6f 20 74 68 65 20 0a 20 20 20 20 2a 2a into the . **
22400 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c sub-journal fil
22410 65 20 6e 6f 77 20 28 69 66 20 69 74 20 69 73 20 e now (if it is
22420 6e 6f 74 20 61 6c 72 65 61 64 79 20 74 68 65 72 not already ther
22430 65 29 2c 20 73 6f 20 74 68 61 74 20 69 74 20 77 e), so that it w
22440 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65 20 72 65 ill. ** be re
22450 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 63 75 stored to its cu
22460 72 72 65 6e 74 20 76 61 6c 75 65 20 77 68 65 6e rrent value when
22470 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 54 the "ROLLBACK T
22480 4f 20 73 70 22 20 69 73 20 0a 20 20 20 20 2a 2a O sp" is . **
22490 20 65 78 65 63 75 74 65 64 2e 0a 20 20 20 20 2a executed.. *
224a0 2f 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 /. if( NEVER(
224b0 0a 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c . rc==SQL
224c0 49 54 45 5f 4f 4b 20 26 26 20 70 50 67 2d 3e 70 ITE_OK && pPg->p
224d0 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 53 69 gno>pPager->dbSi
224e0 7a 65 20 26 26 20 73 75 62 6a 52 65 71 75 69 72 ze && subjRequir
224f0 65 73 50 61 67 65 28 70 50 67 29 0a 20 20 20 20 esPage(pPg).
22500 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 ) ){. rc =
22510 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 subjournalPage(p
22520 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 Pg);. }. .
22530 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 /* Write the c
22540 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 ontents of the p
22550 61 67 65 20 6f 75 74 20 74 6f 20 74 68 65 20 64 age out to the d
22560 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f atabase file. */
22570 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
22580 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
22590 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 rc = pager_write
225a0 5f 70 61 67 65 6c 69 73 74 28 70 50 67 29 3b 0a _pagelist(pPg);.
225b0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
225c0 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 Mark the page as
225d0 20 63 6c 65 61 6e 2e 20 2a 2f 0a 20 20 69 66 28 clean. */. if(
225e0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
225f0 7b 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45 {. PAGERTRACE
22600 28 28 22 53 54 52 45 53 53 20 25 64 20 70 61 67 (("STRESS %d pag
22610 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 e %d\n", PAGERID
22620 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 (pPager), pPg->p
22630 67 6e 6f 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 gno));. sqlit
22640 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 e3PcacheMakeClea
22650 6e 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 n(pPg);. }.. r
22660 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 72 6f eturn pager_erro
22670 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 7d r(pPager, rc);.}
22680 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 .../*.** Allocat
22690 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 e and initialize
226a0 20 61 20 6e 65 77 20 50 61 67 65 72 20 6f 62 6a a new Pager obj
226b0 65 63 74 20 61 6e 64 20 70 75 74 20 61 20 70 6f ect and put a po
226c0 69 6e 74 65 72 20 74 6f 20 69 74 0a 2a 2a 20 69 inter to it.** i
226d0 6e 20 2a 70 70 50 61 67 65 72 2e 20 54 68 65 20 n *ppPager. The
226e0 70 61 67 65 72 20 73 68 6f 75 6c 64 20 65 76 65 pager should eve
226f0 6e 74 75 61 6c 6c 79 20 62 65 20 66 72 65 65 64 ntually be freed
22700 20 62 79 20 70 61 73 73 69 6e 67 20 69 74 0a 2a by passing it.*
22710 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 * to sqlite3Page
22720 72 43 6c 6f 73 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 rClose()..**.**
22730 54 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 61 72 The zFilename ar
22740 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 70 61 gument is the pa
22750 74 68 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 th to the databa
22760 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 2e se file to open.
22770 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 .** If zFilename
22780 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 61 20 is NULL then a
22790 72 61 6e 64 6f 6d 6c 79 2d 6e 61 6d 65 64 20 74 randomly-named t
227a0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 69 73 emporary file is
227b0 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64 20 created.** and
227c0 75 73 65 64 20 61 73 20 74 68 65 20 66 69 6c 65 used as the file
227d0 20 74 6f 20 62 65 20 63 61 63 68 65 64 2e 20 54 to be cached. T
227e0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 61 emporary files a
227f0 72 65 20 62 65 20 64 65 6c 65 74 65 64 0a 2a 2a re be deleted.**
22800 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77 automatically w
22810 68 65 6e 20 74 68 65 79 20 61 72 65 20 63 6c 6f hen they are clo
22820 73 65 64 2e 20 49 66 20 7a 46 69 6c 65 6e 61 6d sed. If zFilenam
22830 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 e is ":memory:"
22840 74 68 65 6e 20 0a 2a 2a 20 61 6c 6c 20 69 6e 66 then .** all inf
22850 6f 72 6d 61 74 69 6f 6e 20 69 73 20 68 65 6c 64 ormation is held
22860 20 69 6e 20 63 61 63 68 65 2e 20 49 74 20 69 73 in cache. It is
22870 20 6e 65 76 65 72 20 77 72 69 74 74 65 6e 20 74 never written t
22880 6f 20 64 69 73 6b 2e 20 0a 2a 2a 20 54 68 69 73 o disk. .** This
22890 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
228a0 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e 2d implement an in-
228b0 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e memory database.
228c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 45 78 74 72 .**.** The nExtr
228d0 61 20 70 61 72 61 6d 65 74 65 72 20 73 70 65 63 a parameter spec
228e0 69 66 69 65 73 20 74 68 65 20 6e 75 6d 62 65 72 ifies the number
228f0 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 of bytes of spa
22900 63 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 ce allocated.**
22910 61 6c 6f 6e 67 20 77 69 74 68 20 65 61 63 68 20 along with each
22920 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 20 page reference.
22930 54 68 69 73 20 73 70 61 63 65 20 69 73 20 61 76 This space is av
22940 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 65 20 75 ailable to the u
22950 73 65 72 0a 2a 2a 20 76 69 61 20 74 68 65 20 73 ser.** via the s
22960 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 qlite3PagerGetEx
22970 74 72 61 28 29 20 41 50 49 2e 0a 2a 2a 0a 2a 2a tra() API..**.**
22980 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d The flags argum
22990 65 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 ent is used to s
229a0 70 65 63 69 66 79 20 70 72 6f 70 65 72 74 69 65 pecify propertie
229b0 73 20 74 68 61 74 20 61 66 66 65 63 74 20 74 68 s that affect th
229c0 65 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 6f e.** operation o
229d0 66 20 74 68 65 20 70 61 67 65 72 2e 20 49 74 20 f the pager. It
229e0 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 should be passed
229f0 20 73 6f 6d 65 20 62 69 74 77 69 73 65 20 63 6f some bitwise co
22a00 6d 62 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 mbination.** of
22a10 74 68 65 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a the PAGER_OMIT_J
22a20 4f 55 52 4e 41 4c 20 61 6e 64 20 50 41 47 45 52 OURNAL and PAGER
22a30 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 66 6c 61 _NO_READLOCK fla
22a40 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 66 gs..**.** The vf
22a50 73 46 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 sFlags parameter
22a60 20 69 73 20 61 20 62 69 74 6d 61 73 6b 20 74 6f is a bitmask to
22a70 20 70 61 73 73 20 74 6f 20 74 68 65 20 66 6c 61 pass to the fla
22a80 67 73 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 gs parameter.**
22a90 6f 66 20 74 68 65 20 78 4f 70 65 6e 28 29 20 6d of the xOpen() m
22aa0 65 74 68 6f 64 20 6f 66 20 74 68 65 20 73 75 70 ethod of the sup
22ab0 70 6c 69 65 64 20 56 46 53 20 77 68 65 6e 20 6f plied VFS when o
22ac0 70 65 6e 69 6e 67 20 66 69 6c 65 73 2e 20 0a 2a pening files. .*
22ad0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 *.** If the page
22ae0 72 20 6f 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f r object is allo
22af0 63 61 74 65 64 20 61 6e 64 20 74 68 65 20 73 70 cated and the sp
22b00 65 63 69 66 69 65 64 20 66 69 6c 65 20 6f 70 65 ecified file ope
22b10 6e 65 64 20 0a 2a 2a 20 73 75 63 63 65 73 73 66 ned .** successf
22b20 75 6c 6c 79 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 ully, SQLITE_OK
22b30 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 is returned and
22b40 2a 70 70 50 61 67 65 72 20 73 65 74 20 74 6f 20 *ppPager set to
22b50 70 6f 69 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 point to.** the
22b60 6e 65 77 20 70 61 67 65 72 20 6f 62 6a 65 63 74 new pager object
22b70 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 . If an error oc
22b80 63 75 72 73 2c 20 2a 70 70 50 61 67 65 72 20 69 curs, *ppPager i
22b90 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 0a 2a 2a s set to NULL.**
22ba0 20 61 6e 64 20 65 72 72 6f 72 20 63 6f 64 65 20 and error code
22bb0 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 20 66 returned. This f
22bc0 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 unction may retu
22bd0 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a rn SQLITE_NOMEM.
22be0 2a 2a 20 28 73 71 6c 69 74 65 33 4d 61 6c 6c 6f ** (sqlite3Mallo
22bf0 63 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61 c() is used to a
22c00 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 29 2c llocate memory),
22c10 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e SQLITE_CANTOPEN
22c20 20 6f 72 20 0a 2a 2a 20 76 61 72 69 6f 75 73 20 or .** various
22c30 53 51 4c 49 54 45 5f 49 4f 5f 58 58 58 20 65 72 SQLITE_IO_XXX er
22c40 72 6f 72 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c rors..*/.int sql
22c50 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 0a 20 ite3PagerOpen(.
22c60 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
22c70 66 73 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 fs, /* The
22c80 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 virtual file sy
22c90 73 74 65 6d 20 74 6f 20 75 73 65 20 2a 2f 0a 20 stem to use */.
22ca0 20 50 61 67 65 72 20 2a 2a 70 70 50 61 67 65 72 Pager **ppPager
22cb0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 , /* OUT
22cc0 3a 20 52 65 74 75 72 6e 20 74 68 65 20 50 61 67 : Return the Pag
22cd0 65 72 20 73 74 72 75 63 74 75 72 65 20 68 65 72 er structure her
22ce0 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 e */. const cha
22cf0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 r *zFilename,
22d00 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 /* Name of the d
22d10 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 atabase file to
22d20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 open */. int nE
22d30 78 74 72 61 2c 20 20 20 20 20 20 20 20 20 20 20 xtra,
22d40 20 20 20 2f 2a 20 45 78 74 72 61 20 62 79 74 65 /* Extra byte
22d50 73 20 61 70 70 65 6e 64 20 74 6f 20 65 61 63 68 s append to each
22d60 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 in-memory page
22d70 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 */. int flags,
22d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
22d90 20 66 6c 61 67 73 20 63 6f 6e 74 72 6f 6c 6c 69 flags controlli
22da0 6e 67 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a ng this file */.
22db0 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 2c 20 int vfsFlags,
22dc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c /* fl
22dd0 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 ags passed throu
22de0 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 gh to sqlite3_vf
22df0 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 76 s.xOpen() */. v
22e00 6f 69 64 20 28 2a 78 52 65 69 6e 69 74 29 28 44 oid (*xReinit)(D
22e10 62 50 61 67 65 2a 29 20 2f 2a 20 46 75 6e 63 74 bPage*) /* Funct
22e20 69 6f 6e 20 74 6f 20 72 65 69 6e 69 74 69 61 6c ion to reinitial
22e30 69 7a 65 20 70 61 67 65 73 20 2a 2f 0a 29 7b 0a ize pages */.){.
22e40 20 20 75 38 20 2a 70 50 74 72 3b 0a 20 20 50 61 u8 *pPtr;. Pa
22e50 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 30 3b ger *pPager = 0;
22e60 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 /* Pager
22e70 6f 62 6a 65 63 74 20 74 6f 20 61 6c 6c 6f 63 61 object to alloca
22e80 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f te and return */
22e90 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
22ea0 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 52 TE_OK; /* R
22eb0 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
22ec0 69 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d 20 30 int tempFile = 0
22ed0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 ; /* True
22ee0 20 66 6f 72 20 74 65 6d 70 20 66 69 6c 65 73 20 for temp files
22ef0 28 69 6e 63 6c 2e 20 69 6e 2d 6d 65 6d 6f 72 79 (incl. in-memory
22f00 20 66 69 6c 65 73 29 20 2a 2f 0a 20 20 69 6e 74 files) */. int
22f10 20 6d 65 6d 44 62 20 3d 20 30 3b 20 20 20 20 20 memDb = 0;
22f20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
22f30 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d 6d this is an in-m
22f40 65 6d 6f 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20 emory file */.
22f50 69 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d 20 30 int readOnly = 0
22f60 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 ; /* True
22f70 20 69 66 20 74 68 69 73 20 69 73 20 61 20 72 65 if this is a re
22f80 61 64 2d 6f 6e 6c 79 20 66 69 6c 65 20 2a 2f 0a ad-only file */.
22f90 20 20 69 6e 74 20 6a 6f 75 72 6e 61 6c 46 69 6c int journalFil
22fa0 65 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 42 79 eSize; /* By
22fb0 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 tes to allocate
22fc0 66 6f 72 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c for each journal
22fd0 20 66 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a fd */. char *z
22fe0 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20 20 20 Pathname = 0;
22ff0 20 20 2f 2a 20 46 75 6c 6c 20 70 61 74 68 20 74 /* Full path t
23000 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 o database file
23010 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 68 6e 61 */. int nPathna
23020 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a me = 0; /*
23030 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
23040 20 69 6e 20 7a 50 61 74 68 6e 61 6d 65 20 2a 2f in zPathname */
23050 0a 20 20 69 6e 74 20 75 73 65 4a 6f 75 72 6e 61 . int useJourna
23060 6c 20 3d 20 28 66 6c 61 67 73 20 26 20 50 41 47 l = (flags & PAG
23070 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 29 ER_OMIT_JOURNAL)
23080 3d 3d 30 3b 20 2f 2a 20 46 61 6c 73 65 20 74 6f ==0; /* False to
23090 20 6f 6d 69 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f omit journal */
230a0 0a 20 20 69 6e 74 20 6e 6f 52 65 61 64 6c 6f 63 . int noReadloc
230b0 6b 20 3d 20 28 66 6c 61 67 73 20 26 20 50 41 47 k = (flags & PAG
230c0 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 29 21 ER_NO_READLOCK)!
230d0 3d 30 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 =0; /* True to
230e0 6f 6d 69 74 20 72 65 61 64 2d 6c 6f 63 6b 20 2a omit read-lock *
230f0 2f 0a 20 20 69 6e 74 20 70 63 61 63 68 65 53 69 /. int pcacheSi
23100 7a 65 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 ze = sqlite3Pcac
23110 68 65 53 69 7a 65 28 29 3b 20 20 20 20 20 20 20 heSize();
23120 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 6c 6c 6f /* Bytes to allo
23130 63 61 74 65 20 66 6f 72 20 50 43 61 63 68 65 20 cate for PCache
23140 2a 2f 0a 20 20 75 31 36 20 73 7a 50 61 67 65 44 */. u16 szPageD
23150 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 flt = SQLITE_DEF
23160 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 20 AULT_PAGE_SIZE;
23170 20 2f 2a 20 44 65 66 61 75 6c 74 20 70 61 67 65 /* Default page
23180 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 2f 2a 20 46 size */.. /* F
23190 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 75 igure out how mu
231a0 63 68 20 73 70 61 63 65 20 69 73 20 72 65 71 75 ch space is requ
231b0 69 72 65 64 20 66 6f 72 20 65 61 63 68 20 6a 6f ired for each jo
231c0 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e 64 6c urnal file-handl
231d0 65 0a 20 20 2a 2a 20 28 74 68 65 72 65 20 61 72 e. ** (there ar
231e0 65 20 74 77 6f 20 6f 66 20 74 68 65 6d 2c 20 74 e two of them, t
231f0 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
23200 61 6e 64 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 and the sub-jour
23210 6e 61 6c 29 2e 20 54 68 69 73 0a 20 20 2a 2a 20 nal). This. **
23220 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 73 is the maximum s
23230 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f pace required fo
23240 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a r an in-memory j
23250 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64 ournal file hand
23260 6c 65 20 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 le . ** and a r
23270 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c 20 66 egular journal f
23280 69 6c 65 2d 68 61 6e 64 6c 65 2e 20 4e 6f 74 65 ile-handle. Note
23290 20 74 68 61 74 20 61 20 22 72 65 67 75 6c 61 72 that a "regular
232a0 20 6a 6f 75 72 6e 61 6c 2d 68 61 6e 64 6c 65 22 journal-handle"
232b0 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 61 20 77 . ** may be a w
232c0 72 61 70 70 65 72 20 63 61 70 61 62 6c 65 20 6f rapper capable o
232d0 66 20 63 61 63 68 69 6e 67 20 74 68 65 20 66 69 f caching the fi
232e0 72 73 74 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 rst portion of t
232f0 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 he journal. **
23300 66 69 6c 65 20 69 6e 20 6d 65 6d 6f 72 79 20 74 file in memory t
23310 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 o implement the
23320 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 atomic-write opt
23330 69 6d 69 7a 61 74 69 6f 6e 20 28 73 65 65 20 0a imization (see .
23340 20 20 2a 2a 20 73 6f 75 72 63 65 20 66 69 6c 65 ** source file
23350 20 6a 6f 75 72 6e 61 6c 2e 63 29 2e 0a 20 20 2a journal.c).. *
23360 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4a /. if( sqlite3J
23370 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 ournalSize(pVfs)
23380 3e 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e >sqlite3MemJourn
23390 61 6c 53 69 7a 65 28 29 20 29 7b 0a 20 20 20 20 alSize() ){.
233a0 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 journalFileSize
233b0 3d 20 52 4f 55 4e 44 38 28 73 71 6c 69 74 65 33 = ROUND8(sqlite3
233c0 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 JournalSize(pVfs
233d0 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ));. }else{.
233e0 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 journalFileSize
233f0 20 3d 20 52 4f 55 4e 44 38 28 73 71 6c 69 74 65 = ROUND8(sqlite
23400 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 3MemJournalSize(
23410 29 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 ));. }.. /* Se
23420 74 20 74 68 65 20 6f 75 74 70 75 74 20 76 61 72 t the output var
23430 69 61 62 6c 65 20 74 6f 20 4e 55 4c 4c 20 69 6e iable to NULL in
23440 20 63 61 73 65 20 61 6e 20 65 72 72 6f 72 20 6f case an error o
23450 63 63 75 72 73 2e 20 2a 2f 0a 20 20 2a 70 70 50 ccurs. */. *ppP
23460 61 67 65 72 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 ager = 0;.. /*
23470 43 6f 6d 70 75 74 65 20 61 6e 64 20 73 74 6f 72 Compute and stor
23480 65 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e e the full pathn
23490 61 6d 65 20 69 6e 20 61 6e 20 61 6c 6c 6f 63 61 ame in an alloca
234a0 74 65 64 20 62 75 66 66 65 72 20 70 6f 69 6e 74 ted buffer point
234b0 65 64 0a 20 20 2a 2a 20 74 6f 20 62 79 20 7a 50 ed. ** to by zP
234c0 61 74 68 6e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 athname, length
234d0 6e 50 61 74 68 6e 61 6d 65 2e 20 4f 72 2c 20 69 nPathname. Or, i
234e0 66 20 74 68 69 73 20 69 73 20 61 20 74 65 6d 70 f this is a temp
234f0 6f 72 61 72 79 20 66 69 6c 65 2c 0a 20 20 2a 2a orary file,. **
23500 20 6c 65 61 76 65 20 62 6f 74 68 20 6e 50 61 74 leave both nPat
23510 68 6e 61 6d 65 20 61 6e 64 20 7a 50 61 74 68 6e hname and zPathn
23520 61 6d 65 20 73 65 74 20 74 6f 20 30 2e 0a 20 20 ame set to 0..
23530 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 */. if( zFilena
23540 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b me && zFilename[
23550 30 5d 20 29 7b 0a 20 20 20 20 6e 50 61 74 68 6e 0] ){. nPathn
23560 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 ame = pVfs->mxPa
23570 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 7a 50 thname+1;. zP
23580 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 athname = sqlite
23590 33 4d 61 6c 6c 6f 63 28 6e 50 61 74 68 6e 61 6d 3Malloc(nPathnam
235a0 65 2a 32 29 3b 0a 20 20 20 20 69 66 28 20 7a 50 e*2);. if( zP
235b0 61 74 68 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 athname==0 ){.
235c0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
235d0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 23 E_NOMEM;. }.#
235e0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
235f0 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 IT_MEMORYDB.
23600 69 66 28 20 73 74 72 63 6d 70 28 7a 46 69 6c 65 if( strcmp(zFile
23610 6e 61 6d 65 2c 22 3a 6d 65 6d 6f 72 79 3a 22 29 name,":memory:")
23620 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d ==0 ){. mem
23630 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 7a 50 Db = 1;. zP
23640 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b 0a athname[0] = 0;.
23650 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 }else.#endif
23660 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 7a 50 61 . {. zPa
23670 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b 20 2f thname[0] = 0; /
23680 2a 20 4d 61 6b 65 20 73 75 72 65 20 69 6e 69 74 * Make sure init
23690 69 61 6c 69 7a 65 64 20 65 76 65 6e 20 69 66 20 ialized even if
236a0 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 20 66 FullPathname() f
236b0 61 69 6c 73 20 2a 2f 0a 20 20 20 20 20 20 72 63 ails */. rc
236c0 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c = sqlite3OsFull
236d0 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a Pathname(pVfs, z
236e0 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74 68 6e Filename, nPathn
236f0 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 29 3b ame, zPathname);
23700 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 50 61 74 . }.. nPat
23710 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 hname = sqlite3S
23720 74 72 6c 65 6e 33 30 28 7a 50 61 74 68 6e 61 6d trlen30(zPathnam
23730 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d e);. if( rc==
23740 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 50 61 SQLITE_OK && nPa
23750 74 68 6e 61 6d 65 2b 38 3e 70 56 66 73 2d 3e 6d thname+8>pVfs->m
23760 78 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 xPathname ){.
23770 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 /* This branc
23780 68 20 69 73 20 74 61 6b 65 6e 20 77 68 65 6e 20 h is taken when
23790 74 68 65 20 6a 6f 75 72 6e 61 6c 20 70 61 74 68 the journal path
237a0 20 72 65 71 75 69 72 65 64 20 62 79 0a 20 20 20 required by.
237b0 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 ** the databa
237c0 73 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 se being opened
237d0 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 74 68 61 will be more tha
237e0 6e 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 n pVfs->mxPathna
237f0 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 me. ** byte
23800 73 20 69 6e 20 6c 65 6e 67 74 68 2e 20 54 68 69 s in length. Thi
23810 73 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 s means the data
23820 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 6f base cannot be o
23830 70 65 6e 65 64 2c 0a 20 20 20 20 20 20 2a 2a 20 pened,. **
23840 61 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 as it will not b
23850 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6f 70 e possible to op
23860 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 en the journal f
23870 69 6c 65 20 6f 72 20 65 76 65 6e 0a 20 20 20 20 ile or even.
23880 20 20 2a 2a 20 63 68 65 63 6b 20 66 6f 72 20 61 ** check for a
23890 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 65 66 hot-journal bef
238a0 6f 72 65 20 72 65 61 64 69 6e 67 2e 0a 20 20 20 ore reading..
238b0 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d */. rc =
238c0 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e SQLITE_CANTOPEN
238d0 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 _BKPT;. }.
238e0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
238f0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 OK ){. sqli
23900 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 te3_free(zPathna
23910 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 me);. retur
23920 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a n rc;. }. }.
23930 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d . /* Allocate m
23940 65 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 50 61 emory for the Pa
23950 67 65 72 20 73 74 72 75 63 74 75 72 65 2c 20 50 ger structure, P
23960 43 61 63 68 65 20 6f 62 6a 65 63 74 2c 20 74 68 Cache object, th
23970 65 0a 20 20 2a 2a 20 74 68 72 65 65 20 66 69 6c e. ** three fil
23980 65 20 64 65 73 63 72 69 70 74 6f 72 73 2c 20 74 e descriptors, t
23990 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
239a0 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 6a 6f name and the jo
239b0 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 urnal . ** file
239c0 20 6e 61 6d 65 2e 20 54 68 65 20 6c 61 79 6f 75 name. The layou
239d0 74 20 69 6e 20 6d 65 6d 6f 72 79 20 69 73 20 61 t in memory is a
239e0 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a s follows:. **.
239f0 20 20 2a 2a 20 20 20 20 20 50 61 67 65 72 20 6f ** Pager o
23a00 62 6a 65 63 74 20 20 20 20 20 20 20 20 20 20 20 bject
23a10 20 20 20 20 20 20 20 20 20 28 73 69 7a 65 6f 66 (sizeof
23a20 28 50 61 67 65 72 29 20 62 79 74 65 73 29 0a 20 (Pager) bytes).
23a30 20 2a 2a 20 20 20 20 20 50 43 61 63 68 65 20 6f ** PCache o
23a40 62 6a 65 63 74 20 20 20 20 20 20 20 20 20 20 20 bject
23a50 20 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 (sqlite3
23a60 50 63 61 63 68 65 53 69 7a 65 28 29 20 62 79 74 PcacheSize() byt
23a70 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 44 61 74 es). ** Dat
23a80 61 62 61 73 65 20 66 69 6c 65 20 68 61 6e 64 6c abase file handl
23a90 65 20 20 20 20 20 20 20 20 20 20 20 20 28 70 56 e (pV
23aa0 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 62 79 74 fs->szOsFile byt
23ab0 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 53 75 62 es). ** Sub
23ac0 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 -journal file ha
23ad0 6e 64 6c 65 20 20 20 20 20 20 20 20 20 28 6a 6f ndle (jo
23ae0 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 62 79 urnalFileSize by
23af0 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 4d 61 tes). ** Ma
23b00 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 in journal file
23b10 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 28 6a handle (j
23b20 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 62 ournalFileSize b
23b30 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 44 ytes). ** D
23b40 61 74 61 62 61 73 65 20 66 69 6c 65 20 6e 61 6d atabase file nam
23b50 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 e (
23b60 6e 50 61 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 nPathname+1 byte
23b70 73 29 0a 20 20 2a 2a 20 20 20 20 20 4a 6f 75 72 s). ** Jour
23b80 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 20 20 nal file name
23b90 20 20 20 20 20 20 20 20 20 20 20 20 28 6e 50 61 (nPa
23ba0 74 68 6e 61 6d 65 2b 38 2b 31 20 62 79 74 65 73 thname+8+1 bytes
23bb0 29 0a 20 20 2a 2f 0a 20 20 70 50 74 72 20 3d 20 ). */. pPtr =
23bc0 28 75 38 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c (u8 *)sqlite3Mal
23bd0 6c 6f 63 5a 65 72 6f 28 0a 20 20 20 20 52 4f 55 locZero(. ROU
23be0 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 50 61 67 ND8(sizeof(*pPag
23bf0 65 72 29 29 20 2b 20 20 20 20 20 20 2f 2a 20 50 er)) + /* P
23c00 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 2a ager structure *
23c10 2f 0a 20 20 20 20 52 4f 55 4e 44 38 28 70 63 61 /. ROUND8(pca
23c20 63 68 65 53 69 7a 65 29 20 2b 20 20 20 20 20 20 cheSize) +
23c30 20 20 20 20 20 2f 2a 20 50 43 61 63 68 65 20 6f /* PCache o
23c40 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 52 4f 55 bject */. ROU
23c50 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 ND8(pVfs->szOsFi
23c60 6c 65 29 20 2b 20 20 20 20 20 20 20 2f 2a 20 54 le) + /* T
23c70 68 65 20 6d 61 69 6e 20 64 62 20 66 69 6c 65 20 he main db file
23c80 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 */. journalFi
23c90 6c 65 53 69 7a 65 20 2a 20 32 20 2b 20 20 20 20 leSize * 2 +
23ca0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 77 6f /* The two
23cb0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a journal files *
23cc0 2f 20 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 / . nPathname
23cd0 20 2b 20 31 20 2b 20 20 20 20 20 20 20 20 20 20 + 1 +
23ce0 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 /* zFilena
23cf0 6d 65 20 2a 2f 0a 20 20 20 20 6e 50 61 74 68 6e me */. nPathn
23d00 61 6d 65 20 2b 20 38 20 2b 20 31 20 20 20 20 20 ame + 8 + 1
23d10 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4a 6f 75 /* zJou
23d20 72 6e 61 6c 20 2a 2f 0a 20 20 29 3b 0a 20 20 61 rnal */. );. a
23d30 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 ssert( EIGHT_BYT
23d40 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 53 51 4c 49 E_ALIGNMENT(SQLI
23d50 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 6a 6f TE_INT_TO_PTR(jo
23d60 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 29 20 urnalFileSize))
23d70 29 3b 0a 20 20 69 66 28 20 21 70 50 74 72 20 29 );. if( !pPtr )
23d80 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 {. sqlite3_fr
23d90 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 ee(zPathname);.
23da0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
23db0 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 50 _NOMEM;. }. pP
23dc0 61 67 65 72 20 3d 20 20 20 20 20 20 20 20 20 20 ager =
23dd0 20 20 20 20 28 50 61 67 65 72 2a 29 28 70 50 74 (Pager*)(pPt
23de0 72 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 50 r);. pPager->pP
23df0 43 61 63 68 65 20 3d 20 20 20 20 28 50 43 61 63 Cache = (PCac
23e00 68 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 he*)(pPtr += ROU
23e10 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 50 61 67 ND8(sizeof(*pPag
23e20 65 72 29 29 29 3b 0a 20 20 70 50 61 67 65 72 2d er)));. pPager-
23e30 3e 66 64 20 3d 20 20 20 28 73 71 6c 69 74 65 33 >fd = (sqlite3
23e40 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 _file*)(pPtr +=
23e50 52 4f 55 4e 44 38 28 70 63 61 63 68 65 53 69 7a ROUND8(pcacheSiz
23e60 65 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 e));. pPager->s
23e70 6a 66 64 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 jfd = (sqlite3_f
23e80 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f ile*)(pPtr += RO
23e90 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f 73 46 UND8(pVfs->szOsF
23ea0 69 6c 65 29 29 3b 0a 20 20 70 50 61 67 65 72 2d ile));. pPager-
23eb0 3e 6a 66 64 20 3d 20 20 28 73 71 6c 69 74 65 33 >jfd = (sqlite3
23ec0 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 _file*)(pPtr +=
23ed0 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 journalFileSize)
23ee0 3b 0a 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c ;. pPager->zFil
23ef0 65 6e 61 6d 65 20 3d 20 20 20 20 28 63 68 61 72 ename = (char
23f00 2a 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75 72 6e *)(pPtr += journ
23f10 61 6c 46 69 6c 65 53 69 7a 65 29 3b 0a 20 20 61 alFileSize);. a
23f20 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 ssert( EIGHT_BYT
23f30 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 50 61 67 E_ALIGNMENT(pPag
23f40 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 2f er->jfd) );.. /
23f50 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 50 61 * Fill in the Pa
23f60 67 65 72 2e 7a 46 69 6c 65 6e 61 6d 65 20 61 6e ger.zFilename an
23f70 64 20 50 61 67 65 72 2e 7a 4a 6f 75 72 6e 61 6c d Pager.zJournal
23f80 20 62 75 66 66 65 72 73 2c 20 69 66 20 72 65 71 buffers, if req
23f90 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 uired. */. if(
23fa0 7a 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 zPathname ){.
23fb0 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 pPager->zJourna
23fc0 6c 20 3d 20 20 20 28 63 68 61 72 2a 29 28 70 50 l = (char*)(pP
23fd0 74 72 20 2b 3d 20 6e 50 61 74 68 6e 61 6d 65 20 tr += nPathname
23fe0 2b 20 31 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 + 1);. memcpy
23ff0 28 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 (pPager->zFilena
24000 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6e me, zPathname, n
24010 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d Pathname);. m
24020 65 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a 4a emcpy(pPager->zJ
24030 6f 75 72 6e 61 6c 2c 20 7a 50 61 74 68 6e 61 6d ournal, zPathnam
24040 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 e, nPathname);.
24050 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 memcpy(&pPage
24060 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 6e 50 61 74 r->zJournal[nPat
24070 68 6e 61 6d 65 5d 2c 20 22 2d 6a 6f 75 72 6e 61 hname], "-journa
24080 6c 22 2c 20 38 29 3b 0a 20 20 20 20 69 66 28 20 l", 8);. if(
24090 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d pPager->zFilenam
240a0 65 5b 30 5d 3d 3d 30 20 29 20 70 50 61 67 65 72 e[0]==0 ) pPager
240b0 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 30 5d 20 3d 20 ->zJournal[0] =
240c0 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 0;. sqlite3_f
240d0 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a ree(zPathname);.
240e0 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 70 56 }. pPager->pV
240f0 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 70 50 61 fs = pVfs;. pPa
24100 67 65 72 2d 3e 76 66 73 46 6c 61 67 73 20 3d 20 ger->vfsFlags =
24110 76 66 73 46 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 vfsFlags;.. /*
24120 4f 70 65 6e 20 74 68 65 20 70 61 67 65 72 20 66 Open the pager f
24130 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ile.. */. if(
24140 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 zFilename && zFi
24150 6c 65 6e 61 6d 65 5b 30 5d 20 26 26 20 21 6d 65 lename[0] && !me
24160 6d 44 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 mDb ){. int f
24170 6f 75 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 out = 0;
24180 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
24190 46 53 20 66 6c 61 67 73 20 72 65 74 75 72 6e 65 FS flags returne
241a0 64 20 62 79 20 78 4f 70 65 6e 28 29 20 2a 2f 0a d by xOpen() */.
241b0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
241c0 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 OsOpen(pVfs, pPa
241d0 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 ger->zFilename,
241e0 70 50 61 67 65 72 2d 3e 66 64 2c 20 76 66 73 46 pPager->fd, vfsF
241f0 6c 61 67 73 2c 20 26 66 6f 75 74 29 3b 0a 20 20 lags, &fout);.
24200 20 20 72 65 61 64 4f 6e 6c 79 20 3d 20 28 66 6f readOnly = (fo
24210 75 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 ut&SQLITE_OPEN_R
24220 45 41 44 4f 4e 4c 59 29 3b 0a 0a 20 20 20 20 2f EADONLY);.. /
24230 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 77 61 * If the file wa
24240 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f s successfully o
24250 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 2f 77 pened for read/w
24260 72 69 74 65 20 61 63 63 65 73 73 2c 0a 20 20 20 rite access,.
24270 20 2a 2a 20 63 68 6f 6f 73 65 20 61 20 64 65 66 ** choose a def
24280 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 69 ault page size i
24290 6e 20 63 61 73 65 20 77 65 20 68 61 76 65 20 74 n case we have t
242a0 6f 20 63 72 65 61 74 65 20 74 68 65 0a 20 20 20 o create the.
242b0 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c ** database fil
242c0 65 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 70 e. The default p
242d0 61 67 65 20 73 69 7a 65 20 69 73 20 74 68 65 20 age size is the
242e0 6d 61 78 69 6d 75 6d 20 6f 66 3a 0a 20 20 20 20 maximum of:.
242f0 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 53 **. ** + S
24300 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 QLITE_DEFAULT_PA
24310 47 45 5f 53 49 5a 45 2c 0a 20 20 20 20 2a 2a 20 GE_SIZE,. **
24320 20 20 20 2b 20 54 68 65 20 76 61 6c 75 65 20 72 + The value r
24330 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 eturned by sqlit
24340 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 e3OsSectorSize()
24350 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 . ** + The
24360 20 6c 61 72 67 65 73 74 20 70 61 67 65 20 73 69 largest page si
24370 7a 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 77 ze that can be w
24380 72 69 74 74 65 6e 20 61 74 6f 6d 69 63 61 6c 6c ritten atomicall
24390 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 y.. */. if
243a0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
243b0 26 26 20 21 72 65 61 64 4f 6e 6c 79 20 29 7b 0a && !readOnly ){.
243c0 20 20 20 20 20 20 73 65 74 53 65 63 74 6f 72 53 setSectorS
243d0 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 ize(pPager);.
243e0 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 assert(SQLITE
243f0 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 _DEFAULT_PAGE_SI
24400 5a 45 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 44 ZE<=SQLITE_MAX_D
24410 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
24420 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 7a 50 );. if( szP
24430 61 67 65 44 66 6c 74 3c 70 50 61 67 65 72 2d 3e ageDflt<pPager->
24440 73 65 63 74 6f 72 53 69 7a 65 20 29 7b 0a 20 20 sectorSize ){.
24450 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 if( pPager
24460 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3e 53 51 4c ->sectorSize>SQL
24470 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f ITE_MAX_DEFAULT_
24480 50 41 47 45 5f 53 49 5a 45 20 29 7b 0a 20 20 20 PAGE_SIZE ){.
24490 20 20 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c szPageDfl
244a0 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 t = SQLITE_MAX_D
244b0 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
244c0 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
244d0 0a 20 20 20 20 20 20 20 20 20 20 73 7a 50 61 67 . szPag
244e0 65 44 66 6c 74 20 3d 20 28 75 31 36 29 70 50 61 eDflt = (u16)pPa
244f0 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3b ger->sectorSize;
24500 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
24510 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }.#ifdef SQLITE
24520 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 _ENABLE_ATOMIC_W
24530 52 49 54 45 0a 20 20 20 20 20 20 7b 0a 20 20 20 RITE. {.
24540 20 20 20 20 20 69 6e 74 20 69 44 63 20 3d 20 73 int iDc = s
24550 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 qlite3OsDeviceCh
24560 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 aracteristics(pP
24570 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20 ager->fd);.
24580 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 int ii;.
24590 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 assert(SQLITE
245a0 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 _IOCAP_ATOMIC512
245b0 3d 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 ==(512>>8));.
245c0 20 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 assert(SQLI
245d0 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 TE_IOCAP_ATOMIC6
245e0 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b 4K==(65536>>8));
245f0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
24600 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 SQLITE_MAX_DEFAU
24610 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c 3d 36 35 LT_PAGE_SIZE<=65
24620 35 33 36 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 536);. fo
24630 72 28 69 69 3d 73 7a 50 61 67 65 44 66 6c 74 3b r(ii=szPageDflt;
24640 20 69 69 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f ii<=SQLITE_MAX_
24650 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
24660 45 3b 20 69 69 3d 69 69 2a 32 29 7b 0a 20 20 20 E; ii=ii*2){.
24670 20 20 20 20 20 20 20 69 66 28 20 69 44 63 26 28 if( iDc&(
24680 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
24690 4d 49 43 7c 28 69 69 3e 3e 38 29 29 20 29 7b 0a MIC|(ii>>8)) ){.
246a0 20 20 20 20 20 20 20 20 20 20 20 20 73 7a 50 61 szPa
246b0 67 65 44 66 6c 74 20 3d 20 69 69 3b 0a 20 20 20 geDflt = ii;.
246c0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
246d0 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 }. }.#endi
246e0 66 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b f. }. }else{
246f0 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74 65 6d . /* If a tem
24700 70 6f 72 61 72 79 20 66 69 6c 65 20 69 73 20 72 porary file is r
24710 65 71 75 65 73 74 65 64 2c 20 69 74 20 69 73 20 equested, it is
24720 6e 6f 74 20 6f 70 65 6e 65 64 20 69 6d 6d 65 64 not opened immed
24730 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 49 iately.. ** I
24740 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20 61 n this case we a
24750 63 63 65 70 74 20 74 68 65 20 64 65 66 61 75 6c ccept the defaul
24760 74 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64 20 t page size and
24770 64 65 6c 61 79 20 61 63 74 75 61 6c 6c 79 0a 20 delay actually.
24780 20 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 74 68 ** opening th
24790 65 20 66 69 6c 65 20 75 6e 74 69 6c 20 74 68 65 e file until the
247a0 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 4f first call to O
247b0 73 57 72 69 74 65 28 29 2e 0a 20 20 20 20 2a 2a sWrite().. **
247c0 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 62 72 61 . ** This bra
247d0 6e 63 68 20 69 73 20 61 6c 73 6f 20 72 75 6e 20 nch is also run
247e0 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 for an in-memory
247f0 20 64 61 74 61 62 61 73 65 2e 20 41 6e 20 69 6e database. An in
24800 2d 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 64 -memory. ** d
24810 61 74 61 62 61 73 65 20 69 73 20 74 68 65 20 73 atabase is the s
24820 61 6d 65 20 61 73 20 61 20 74 65 6d 70 2d 66 69 ame as a temp-fi
24830 6c 65 20 74 68 61 74 20 69 73 20 6e 65 76 65 72 le that is never
24840 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 0a written out to.
24850 20 20 20 20 2a 2a 20 64 69 73 6b 20 61 6e 64 20 ** disk and
24860 75 73 65 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 uses an in-memor
24870 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e y rollback journ
24880 61 6c 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 al.. */ .
24890 74 65 6d 70 46 69 6c 65 20 3d 20 31 3b 0a 20 20 tempFile = 1;.
248a0 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 pPager->state
248b0 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 = PAGER_EXCLUSIV
248c0 45 3b 0a 20 20 20 20 72 65 61 64 4f 6e 6c 79 20 E;. readOnly
248d0 3d 20 28 76 66 73 46 6c 61 67 73 26 53 51 4c 49 = (vfsFlags&SQLI
248e0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
248f0 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 );. }.. /* The
24900 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 following call
24910 74 6f 20 50 61 67 65 72 53 65 74 50 61 67 65 73 to PagerSetPages
24920 69 7a 65 28 29 20 73 65 72 76 65 73 20 74 6f 20 ize() serves to
24930 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 set the value of
24940 20 0a 20 20 2a 2a 20 50 61 67 65 72 2e 70 61 67 . ** Pager.pag
24950 65 53 69 7a 65 20 61 6e 64 20 74 6f 20 61 6c 6c eSize and to all
24960 6f 63 61 74 65 20 74 68 65 20 50 61 67 65 72 2e ocate the Pager.
24970 70 54 6d 70 53 70 61 63 65 20 62 75 66 66 65 72 pTmpSpace buffer
24980 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d .. */. if( rc=
24990 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
249a0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
249b0 2d 3e 6d 65 6d 44 62 3d 3d 30 20 29 3b 0a 20 20 ->memDb==0 );.
249c0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
249d0 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 gerSetPagesize(p
249e0 50 61 67 65 72 2c 20 26 73 7a 50 61 67 65 44 66 Pager, &szPageDf
249f0 6c 74 2c 20 2d 31 29 3b 0a 20 20 20 20 74 65 73 lt, -1);. tes
24a00 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 tcase( rc!=SQLIT
24a10 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f E_OK );. }.. /
24a20 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 * If an error oc
24a30 63 75 72 72 65 64 20 69 6e 20 65 69 74 68 65 72 curred in either
24a40 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 73 20 61 of the blocks a
24a50 62 6f 76 65 2c 20 66 72 65 65 20 74 68 65 20 0a bove, free the .
24a60 20 20 2a 2a 20 50 61 67 65 72 20 73 74 72 75 63 ** Pager struc
24a70 74 75 72 65 20 61 6e 64 20 63 6c 6f 73 65 20 74 ture and close t
24a80 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 he file.. */.
24a90 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
24aa0 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 K ){. assert(
24ab0 20 21 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 !pPager->pTmpSp
24ac0 61 63 65 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ace );. sqlit
24ad0 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 e3OsClose(pPager
24ae0 2d 3e 66 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 ->fd);. sqlit
24af0 65 33 5f 66 72 65 65 28 70 50 61 67 65 72 29 3b e3_free(pPager);
24b00 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
24b10 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 }.. /* Initia
24b20 6c 69 7a 65 20 74 68 65 20 50 43 61 63 68 65 20 lize the PCache
24b30 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 61 73 73 object. */. ass
24b40 65 72 74 28 20 6e 45 78 74 72 61 3c 31 30 30 30 ert( nExtra<1000
24b50 20 29 3b 0a 20 20 6e 45 78 74 72 61 20 3d 20 52 );. nExtra = R
24b60 4f 55 4e 44 38 28 6e 45 78 74 72 61 29 3b 0a 20 OUND8(nExtra);.
24b70 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70 sqlite3PcacheOp
24b80 65 6e 28 73 7a 50 61 67 65 44 66 6c 74 2c 20 6e en(szPageDflt, n
24b90 45 78 74 72 61 2c 20 21 6d 65 6d 44 62 2c 0a 20 Extra, !memDb,.
24ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
24bb0 20 20 20 21 6d 65 6d 44 62 3f 70 61 67 65 72 53 !memDb?pagerS
24bc0 74 72 65 73 73 3a 30 2c 20 28 76 6f 69 64 20 2a tress:0, (void *
24bd0 29 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d )pPager, pPager-
24be0 3e 70 50 43 61 63 68 65 29 3b 0a 0a 20 20 50 41 >pPCache);.. PA
24bf0 47 45 52 54 52 41 43 45 28 28 22 4f 50 45 4e 20 GERTRACE(("OPEN
24c00 25 64 20 25 73 5c 6e 22 2c 20 46 49 4c 45 48 41 %d %s\n", FILEHA
24c10 4e 44 4c 45 49 44 28 70 50 61 67 65 72 2d 3e 66 NDLEID(pPager->f
24c20 64 29 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c d), pPager->zFil
24c30 65 6e 61 6d 65 29 29 3b 0a 20 20 49 4f 54 52 41 ename));. IOTRA
24c40 43 45 28 28 22 4f 50 45 4e 20 25 70 20 25 73 5c CE(("OPEN %p %s\
24c50 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 n", pPager, pPag
24c60 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 0a er->zFilename)).
24c70 0a 20 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f . pPager->useJo
24c80 75 72 6e 61 6c 20 3d 20 28 75 38 29 75 73 65 4a urnal = (u8)useJ
24c90 6f 75 72 6e 61 6c 3b 0a 20 20 70 50 61 67 65 72 ournal;. pPager
24ca0 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 ->noReadlock = (
24cb0 6e 6f 52 65 61 64 6c 6f 63 6b 20 26 26 20 72 65 noReadlock && re
24cc0 61 64 4f 6e 6c 79 29 20 3f 31 3a 30 3b 0a 20 20 adOnly) ?1:0;.
24cd0 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f /* pPager->stmtO
24ce0 70 65 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a pen = 0; */. /*
24cf0 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 pPager->stmtInU
24d00 73 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 se = 0; */. /*
24d10 70 50 61 67 65 72 2d 3e 6e 52 65 66 20 3d 20 30 pPager->nRef = 0
24d20 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 ; */. pPager->d
24d30 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 28 75 38 bSizeValid = (u8
24d40 29 6d 65 6d 44 62 3b 0a 20 20 2f 2a 20 70 50 61 )memDb;. /* pPa
24d50 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 20 3d 20 ger->stmtSize =
24d60 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 0; */. /* pPage
24d70 72 2d 3e 73 74 6d 74 4a 53 69 7a 65 20 3d 20 30 r->stmtJSize = 0
24d80 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 ; */. /* pPager
24d90 2d 3e 6e 50 61 67 65 20 3d 20 30 3b 20 2a 2f 0a ->nPage = 0; */.
24da0 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f pPager->mxPgno
24db0 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 = SQLITE_MAX_PA
24dc0 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 2f 2a 20 70 GE_COUNT;. /* p
24dd0 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 Pager->state = P
24de0 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b 20 2a 2f 0a AGER_UNLOCK; */.
24df0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
24e00 2d 3e 73 74 61 74 65 20 3d 3d 20 28 74 65 6d 70 ->state == (temp
24e10 46 69 6c 65 20 3f 20 50 41 47 45 52 5f 45 58 43 File ? PAGER_EXC
24e20 4c 55 53 49 56 45 20 3a 20 50 41 47 45 52 5f 55 LUSIVE : PAGER_U
24e30 4e 4c 4f 43 4b 29 20 29 3b 0a 20 20 2f 2a 20 70 NLOCK) );. /* p
24e40 50 61 67 65 72 2d 3e 65 72 72 4d 61 73 6b 20 3d Pager->errMask =
24e50 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 0; */. pPager-
24e60 3e 74 65 6d 70 46 69 6c 65 20 3d 20 28 75 38 29 >tempFile = (u8)
24e70 74 65 6d 70 46 69 6c 65 3b 0a 20 20 61 73 73 65 tempFile;. asse
24e80 72 74 28 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 rt( tempFile==PA
24e90 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
24ea0 4e 4f 52 4d 41 4c 20 0a 20 20 20 20 20 20 20 20 NORMAL .
24eb0 20 20 7c 7c 20 74 65 6d 70 46 69 6c 65 3d 3d 50 || tempFile==P
24ec0 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
24ed0 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 _EXCLUSIVE );.
24ee0 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f assert( PAGER_LO
24ef0 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 CKINGMODE_EXCLUS
24f00 49 56 45 3d 3d 31 20 29 3b 0a 20 20 70 50 61 67 IVE==1 );. pPag
24f10 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 er->exclusiveMod
24f20 65 20 3d 20 28 75 38 29 74 65 6d 70 46 69 6c 65 e = (u8)tempFile
24f30 3b 20 0a 20 20 70 50 61 67 65 72 2d 3e 63 68 61 ; . pPager->cha
24f40 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 ngeCountDone = p
24f50 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b Pager->tempFile;
24f60 0a 20 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 . pPager->memDb
24f70 20 3d 20 28 75 38 29 6d 65 6d 44 62 3b 0a 20 20 = (u8)memDb;.
24f80 70 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 pPager->readOnly
24f90 20 3d 20 28 75 38 29 72 65 61 64 4f 6e 6c 79 3b = (u8)readOnly;
24fa0 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 65 . /* pPager->ne
24fb0 65 64 53 79 6e 63 20 3d 20 30 3b 20 2a 2f 0a 20 edSync = 0; */.
24fc0 20 61 73 73 65 72 74 28 20 75 73 65 4a 6f 75 72 assert( useJour
24fd0 6e 61 6c 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 nal || pPager->t
24fe0 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 70 50 61 empFile );. pPa
24ff0 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 70 50 ger->noSync = pP
25000 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a ager->tempFile;.
25010 20 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 pPager->fullSy
25020 6e 63 20 3d 20 70 50 61 67 65 72 2d 3e 6e 6f 53 nc = pPager->noS
25030 79 6e 63 20 3f 30 3a 31 3b 0a 20 20 70 50 61 67 ync ?0:1;. pPag
25040 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d er->sync_flags =
25050 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 SQLITE_SYNC_NOR
25060 4d 41 4c 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 MAL;. /* pPager
25070 2d 3e 70 46 69 72 73 74 20 3d 20 30 3b 20 2a 2f ->pFirst = 0; */
25080 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 . /* pPager->pF
25090 69 72 73 74 53 79 6e 63 65 64 20 3d 20 30 3b 20 irstSynced = 0;
250a0 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e */. /* pPager->
250b0 70 4c 61 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 pLast = 0; */.
250c0 70 50 61 67 65 72 2d 3e 6e 45 78 74 72 61 20 3d pPager->nExtra =
250d0 20 28 75 31 36 29 6e 45 78 74 72 61 3b 0a 20 20 (u16)nExtra;.
250e0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 pPager->journalS
250f0 69 7a 65 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 izeLimit = SQLIT
25100 45 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 E_DEFAULT_JOURNA
25110 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20 20 L_SIZE_LIMIT;.
25120 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
25130 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 74 65 Pager->fd) || te
25140 6d 70 46 69 6c 65 20 29 3b 0a 20 20 73 65 74 53 mpFile );. setS
25150 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 ectorSize(pPager
25160 29 3b 0a 20 20 69 66 28 20 21 75 73 65 4a 6f 75 );. if( !useJou
25170 72 6e 61 6c 20 29 7b 0a 20 20 20 20 70 50 61 67 rnal ){. pPag
25180 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 er->journalMode
25190 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d = PAGER_JOURNALM
251a0 4f 44 45 5f 4f 46 46 3b 0a 20 20 7d 65 6c 73 65 ODE_OFF;. }else
251b0 20 69 66 28 20 6d 65 6d 44 62 20 29 7b 0a 20 20 if( memDb ){.
251c0 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
251d0 6c 4d 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f lMode = PAGER_JO
251e0 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 URNALMODE_MEMORY
251f0 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70 50 61 67 65 ;. }. /* pPage
25200 72 2d 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 20 r->xBusyHandler
25210 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 = 0; */. /* pPa
25220 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 ger->pBusyHandle
25230 72 41 72 67 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 rArg = 0; */. p
25240 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 Pager->xReiniter
25250 20 3d 20 78 52 65 69 6e 69 74 3b 0a 20 20 2f 2a = xReinit;. /*
25260 20 6d 65 6d 73 65 74 28 70 50 61 67 65 72 2d 3e memset(pPager->
25270 61 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 aHash, 0, sizeof
25280 28 70 50 61 67 65 72 2d 3e 61 48 61 73 68 29 29 (pPager->aHash))
25290 3b 20 2a 2f 0a 0a 20 20 2a 70 70 50 61 67 65 72 ; */.. *ppPager
252a0 20 3d 20 70 50 61 67 65 72 3b 0a 20 20 72 65 74 = pPager;. ret
252b0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
252c0 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 ..../*.** This f
252d0 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
252e0 64 20 61 66 74 65 72 20 74 72 61 6e 73 69 74 69 d after transiti
252f0 6f 6e 69 6e 67 20 66 72 6f 6d 20 50 41 47 45 52 oning from PAGER
25300 5f 55 4e 4c 4f 43 4b 20 74 6f 0a 2a 2a 20 50 41 _UNLOCK to.** PA
25310 47 45 52 5f 53 48 41 52 45 44 20 73 74 61 74 65 GER_SHARED state
25320 2e 20 49 74 20 74 65 73 74 73 20 69 66 20 74 68 . It tests if th
25330 65 72 65 20 69 73 20 61 20 68 6f 74 20 6a 6f 75 ere is a hot jou
25340 72 6e 61 6c 20 70 72 65 73 65 6e 74 20 69 6e 0a rnal present in.
25350 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 ** the file-syst
25360 65 6d 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e em for the given
25370 20 70 61 67 65 72 2e 20 41 20 68 6f 74 20 6a 6f pager. A hot jo
25380 75 72 6e 61 6c 20 69 73 20 6f 6e 65 20 74 68 61 urnal is one tha
25390 74 20 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 t .** needs to b
253a0 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 41 e played back. A
253b0 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 ccording to this
253c0 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 68 6f 74 function, a hot
253d0 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 -journal.** file
253e0 20 65 78 69 73 74 73 20 69 66 20 74 68 65 20 66 exists if the f
253f0 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74 65 72 69 ollowing criteri
25400 61 20 61 72 65 20 6d 65 74 3a 0a 2a 2a 0a 2a 2a a are met:.**.**
25410 20 20 20 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c * The journal
25420 20 66 69 6c 65 20 65 78 69 73 74 73 20 69 6e 20 file exists in
25430 74 68 65 20 66 69 6c 65 20 73 79 73 74 65 6d 2c the file system,
25440 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 4e 6f 20 70 and.** * No p
25450 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 61 20 52 rocess holds a R
25460 45 53 45 52 56 45 44 20 6f 72 20 67 72 65 61 74 ESERVED or great
25470 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 er lock on the d
25480 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 61 6e atabase file, an
25490 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 74 d.** * The dat
254a0 61 62 61 73 65 20 66 69 6c 65 20 69 74 73 65 6c abase file itsel
254b0 66 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 f is greater tha
254c0 6e 20 30 20 62 79 74 65 73 20 69 6e 20 73 69 7a n 0 bytes in siz
254d0 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 e, and.** * Th
254e0 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 e first byte of
254f0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
25500 20 65 78 69 73 74 73 20 61 6e 64 20 69 73 20 6e exists and is n
25510 6f 74 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 49 ot 0x00..**.** I
25520 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 f the current si
25530 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
25540 73 65 20 66 69 6c 65 20 69 73 20 30 20 62 75 74 se file is 0 but
25550 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a a journal file.
25560 2a 2a 20 65 78 69 73 74 73 2c 20 74 68 61 74 20 ** exists, that
25570 69 73 20 70 72 6f 62 61 62 6c 79 20 61 6e 20 6f is probably an o
25580 6c 64 20 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 ld journal left
25590 6f 76 65 72 20 66 72 6f 6d 20 61 20 70 72 69 6f over from a prio
255a0 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77 69 r.** database wi
255b0 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 th the same name
255c0 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
255d0 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
255e0 69 73 0a 2a 2a 20 6a 75 73 74 20 64 65 6c 65 74 is.** just delet
255f0 65 64 20 75 73 69 6e 67 20 4f 73 44 65 6c 65 74 ed using OsDelet
25600 65 2c 20 2a 70 45 78 69 73 74 73 20 69 73 20 73 e, *pExists is s
25610 65 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 et to 0 and SQLI
25620 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75 TE_OK.** is retu
25630 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rned..**.** This
25640 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f routine does no
25650 74 20 63 68 65 63 6b 20 69 66 20 74 68 65 72 65 t check if there
25660 20 69 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 is a master jou
25670 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 0a 2a 2a rnal filename.**
25680 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
25690 68 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 he file. If ther
256a0 65 20 69 73 2c 20 61 6e 64 20 74 68 61 74 20 6d e is, and that m
256b0 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
256c0 6c 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65 le.** does not e
256d0 78 69 73 74 2c 20 74 68 65 6e 20 74 68 65 20 6a xist, then the j
256e0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e ournal file is n
256f0 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 2e 20 49 ot really hot. I
25700 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73 65 20 74 n this.** case t
25710 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
25720 20 72 65 74 75 72 6e 20 61 20 66 61 6c 73 65 2d return a false-
25730 70 6f 73 69 74 69 76 65 2e 20 54 68 65 20 70 61 positive. The pa
25740 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 0a 2a ger_playback().*
25750 2a 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 * routine will d
25760 69 73 63 6f 76 65 72 20 74 68 61 74 20 74 68 65 iscover that the
25770 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
25780 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 20 not really hot
25790 61 6e 64 20 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 and .** will not
257a0 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 0a roll it back. .
257b0 2a 2a 0a 2a 2a 20 49 66 20 61 20 68 6f 74 2d 6a **.** If a hot-j
257c0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 66 ournal file is f
257d0 6f 75 6e 64 20 74 6f 20 65 78 69 73 74 2c 20 2a ound to exist, *
257e0 70 45 78 69 73 74 73 20 69 73 20 73 65 74 20 74 pExists is set t
257f0 6f 20 31 20 61 6e 64 20 0a 2a 2a 20 53 51 4c 49 o 1 and .** SQLI
25800 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 TE_OK returned.
25810 49 66 20 6e 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61 If no hot-journa
25820 6c 20 66 69 6c 65 20 69 73 20 70 72 65 73 65 6e l file is presen
25830 74 2c 20 2a 70 45 78 69 73 74 73 20 69 73 0a 2a t, *pExists is.*
25840 2a 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53 * set to 0 and S
25850 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 QLITE_OK returne
25860 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f d. If an IO erro
25870 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 r occurs while t
25880 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 64 65 74 65 rying.** to dete
25890 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 rmine whether or
258a0 20 6e 6f 74 20 61 20 68 6f 74 2d 6a 6f 75 72 6e not a hot-journ
258b0 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20 al file exists,
258c0 74 68 65 20 49 4f 20 65 72 72 6f 72 0a 2a 2a 20 the IO error.**
258d0 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
258e0 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f and the value o
258f0 66 20 2a 70 45 78 69 73 74 73 20 69 73 20 75 6e f *pExists is un
25900 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 defined..*/.stat
25910 69 63 20 69 6e 74 20 68 61 73 48 6f 74 4a 6f 75 ic int hasHotJou
25920 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 rnal(Pager *pPag
25930 65 72 2c 20 69 6e 74 20 2a 70 45 78 69 73 74 73 er, int *pExists
25940 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 ){. sqlite3_vfs
25950 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 * const pVfs =
25960 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 pPager->pVfs;.
25970 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
25980 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a OK; /*
25990 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
259a0 20 20 69 6e 74 20 65 78 69 73 74 73 20 3d 20 31 int exists = 1
259b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
259c0 2f 2a 20 54 72 75 65 20 69 66 20 61 20 6a 6f 75 /* True if a jou
259d0 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70 72 65 rnal file is pre
259e0 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6a 72 sent */. int jr
259f0 6e 6c 4f 70 65 6e 20 3d 20 21 21 69 73 4f 70 65 nlOpen = !!isOpe
25a00 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a n(pPager->jfd);.
25a10 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
25a20 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 r!=0 );. assert
25a30 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 ( pPager->useJou
25a40 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 rnal );. assert
25a50 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
25a60 3e 66 64 29 20 29 3b 0a 20 20 61 73 73 65 72 74 >fd) );. assert
25a70 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 ( pPager->state
25a80 3c 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 <= PAGER_SHARED
25a90 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6a 72 6e );. assert( jrn
25aa0 6c 4f 70 65 6e 3d 3d 30 20 7c 7c 20 28 20 73 71 lOpen==0 || ( sq
25ab0 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 lite3OsDeviceCha
25ac0 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 racteristics(pPa
25ad0 67 65 72 2d 3e 6a 66 64 29 20 26 0a 20 20 20 20 ger->jfd) &.
25ae0 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 55 4e 44 SQLITE_IOCAP_UND
25af0 45 4c 45 54 41 42 4c 45 5f 57 48 45 4e 5f 4f 50 ELETABLE_WHEN_OP
25b00 45 4e 0a 20 20 29 29 3b 0a 0a 20 20 2a 70 45 78 EN. ));.. *pEx
25b10 69 73 74 73 20 3d 20 30 3b 0a 20 20 69 66 28 20 ists = 0;. if(
25b20 21 6a 72 6e 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 !jrnlOpen ){.
25b30 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 rc = sqlite3OsA
25b40 63 63 65 73 73 28 70 56 66 73 2c 20 70 50 61 67 ccess(pVfs, pPag
25b50 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 er->zJournal, SQ
25b60 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 LITE_ACCESS_EXIS
25b70 54 53 2c 20 26 65 78 69 73 74 73 29 3b 0a 20 20 TS, &exists);.
25b80 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 }. if( rc==SQLI
25b90 54 45 5f 4f 4b 20 26 26 20 65 78 69 73 74 73 20 TE_OK && exists
25ba0 29 7b 0a 20 20 20 20 69 6e 74 20 6c 6f 63 6b 65 ){. int locke
25bb0 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
25bc0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 6f /* True if so
25bd0 6d 65 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 me process holds
25be0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
25bf0 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 52 61 63 65 */.. /* Race
25c00 20 63 6f 6e 64 69 74 69 6f 6e 20 68 65 72 65 3a condition here:
25c10 20 20 41 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 Another proces
25c20 73 20 6d 69 67 68 74 20 68 61 76 65 20 62 65 65 s might have bee
25c30 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 20 20 n holding the.
25c40 20 20 2a 2a 20 74 68 65 20 52 45 53 45 52 56 45 ** the RESERVE
25c50 44 20 6c 6f 63 6b 20 61 6e 64 20 68 61 76 65 20 D lock and have
25c60 61 20 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e 20 61 a journal open a
25c70 74 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 41 t the sqlite3OsA
25c80 63 63 65 73 73 28 29 20 0a 20 20 20 20 2a 2a 20 ccess() . **
25c90 63 61 6c 6c 20 61 62 6f 76 65 2c 20 62 75 74 20 call above, but
25ca0 74 68 65 6e 20 64 65 6c 65 74 65 20 74 68 65 20 then delete the
25cb0 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 64 72 6f 70 journal and drop
25cc0 20 74 68 65 20 6c 6f 63 6b 20 62 65 66 6f 72 65 the lock before
25cd0 0a 20 20 20 20 2a 2a 20 77 65 20 67 65 74 20 74 . ** we get t
25ce0 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 o the following
25cf0 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 sqlite3OsCheckRe
25d00 73 65 72 76 65 64 4c 6f 63 6b 28 29 20 63 61 6c servedLock() cal
25d10 6c 2e 20 20 49 66 20 74 68 61 74 0a 20 20 20 20 l. If that.
25d20 2a 2a 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 ** is the case,
25d30 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 this routine mig
25d40 68 74 20 74 68 69 6e 6b 20 74 68 65 72 65 20 69 ht think there i
25d50 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 s a hot journal
25d60 77 68 65 6e 0a 20 20 20 20 2a 2a 20 69 6e 20 66 when. ** in f
25d70 61 63 74 20 74 68 65 72 65 20 69 73 20 6e 6f 6e act there is non
25d80 65 2e 20 20 54 68 69 73 20 72 65 73 75 6c 74 73 e. This results
25d90 20 69 6e 20 61 20 66 61 6c 73 65 2d 70 6f 73 69 in a false-posi
25da0 74 69 76 65 20 77 68 69 63 68 20 77 69 6c 6c 0a tive which will.
25db0 20 20 20 20 2a 2a 20 62 65 20 64 65 61 6c 74 20 ** be dealt
25dc0 77 69 74 68 20 62 79 20 74 68 65 20 70 6c 61 79 with by the play
25dd0 62 61 63 6b 20 72 6f 75 74 69 6e 65 2e 20 20 54 back routine. T
25de0 69 63 6b 65 74 20 23 33 38 38 33 2e 0a 20 20 20 icket #3883..
25df0 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c */. rc = sql
25e00 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 ite3OsCheckReser
25e10 76 65 64 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e vedLock(pPager->
25e20 66 64 2c 20 26 6c 6f 63 6b 65 64 29 3b 0a 20 20 fd, &locked);.
25e30 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
25e40 5f 4f 4b 20 26 26 20 21 6c 6f 63 6b 65 64 20 29 _OK && !locked )
25e50 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 {. int nPag
25e60 65 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 e;.. /* Che
25e70 63 6b 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 ck the size of t
25e80 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
25e90 2e 20 49 66 20 69 74 20 63 6f 6e 73 69 73 74 73 . If it consists
25ea0 20 6f 66 20 30 20 70 61 67 65 73 2c 0a 20 20 20 of 0 pages,.
25eb0 20 20 20 2a 2a 20 74 68 65 6e 20 64 65 6c 65 74 ** then delet
25ec0 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 e the journal fi
25ed0 6c 65 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 le. See the head
25ee0 65 72 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 er comment above
25ef0 20 66 6f 72 20 0a 20 20 20 20 20 20 2a 2a 20 74 for . ** t
25f00 68 65 20 72 65 61 73 6f 6e 69 6e 67 20 68 65 72 he reasoning her
25f10 65 2e 20 20 44 65 6c 65 74 65 20 74 68 65 20 6f e. Delete the o
25f20 62 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 bsolete journal
25f30 66 69 6c 65 20 75 6e 64 65 72 0a 20 20 20 20 20 file under.
25f40 20 2a 2a 20 61 20 52 45 53 45 52 56 45 44 20 6c ** a RESERVED l
25f50 6f 63 6b 20 74 6f 20 61 76 6f 69 64 20 72 61 63 ock to avoid rac
25f60 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 6e 64 e conditions and
25f70 20 74 6f 20 61 76 6f 69 64 20 76 69 6f 6c 61 74 to avoid violat
25f80 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 5b 48 33 ing. ** [H3
25f90 33 30 32 30 5d 2e 0a 20 20 20 20 20 20 2a 2f 0a 3020].. */.
25fa0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
25fb0 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
25fc0 28 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 (pPager, &nPage)
25fd0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
25fe0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
25ff0 20 20 20 20 20 69 66 28 20 6e 50 61 67 65 3d 3d if( nPage==
26000 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 0 ){. s
26010 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
26020 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 nMalloc();.
26030 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
26040 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 OsLock(pPager->f
26050 64 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b d, RESERVED_LOCK
26060 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a )==SQLITE_OK ){.
26070 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
26080 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 te3OsDelete(pVfs
26090 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e , pPager->zJourn
260a0 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 al, 0);.
260b0 20 20 20 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c sqlite3OsUnl
260c0 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ock(pPager->fd,
260d0 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 SHARED_LOCK);.
260e0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
260f0 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 sqlite3EndBe
26100 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
26110 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
26120 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f /* The jo
26130 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 urnal file exist
26140 73 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 63 s and no other c
26150 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 61 20 onnection has a
26160 72 65 73 65 72 76 65 64 0a 20 20 20 20 20 20 20 reserved.
26170 20 20 20 2a 2a 20 6f 72 20 67 72 65 61 74 65 72 ** or greater
26180 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
26190 61 62 61 73 65 20 66 69 6c 65 2e 20 4e 6f 77 20 abase file. Now
261a0 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 72 65 check that there
261b0 20 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a is. **
261c0 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6e 6f at least one no
261d0 6e 2d 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 n-zero bytes at
261e0 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 the start of the
261f0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 journal file..
26200 20 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20 74 ** If t
26210 68 65 72 65 20 69 73 2c 20 74 68 65 6e 20 77 65 here is, then we
26220 20 63 6f 6e 73 69 64 65 72 20 74 68 69 73 20 6a consider this j
26230 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 68 6f 74 ournal to be hot
26240 2e 20 49 66 20 6e 6f 74 2c 20 0a 20 20 20 20 20 . If not, .
26250 20 20 20 20 20 2a 2a 20 69 74 20 63 61 6e 20 62 ** it can b
26260 65 20 69 67 6e 6f 72 65 64 2e 0a 20 20 20 20 20 e ignored..
26270 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
26280 20 20 69 66 28 20 21 6a 72 6e 6c 4f 70 65 6e 20 if( !jrnlOpen
26290 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 ){. i
262a0 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f 50 nt f = SQLITE_OP
262b0 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 EN_READONLY|SQLI
262c0 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 TE_OPEN_MAIN_JOU
262d0 52 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 RNAL;.
262e0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
262f0 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 Open(pVfs, pPage
26300 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 r->zJournal, pPa
26310 67 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 29 ger->jfd, f, &f)
26320 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
26330 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
26340 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
26350 20 20 20 20 20 20 20 20 20 75 38 20 66 69 72 73 u8 firs
26360 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 t = 0;.
26370 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
26380 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 sRead(pPager->jf
26390 64 2c 20 28 76 6f 69 64 20 2a 29 26 66 69 72 73 d, (void *)&firs
263a0 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 t, 1, 0);.
263b0 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
263c0 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 LITE_IOERR_SHORT
263d0 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 20 _READ ){.
263e0 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
263f0 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 TE_OK;.
26400 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 }.
26410 20 69 66 28 20 21 6a 72 6e 6c 4f 70 65 6e 20 29 if( !jrnlOpen )
26420 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
26430 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 sqlite3OsClose(p
26440 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 Pager->jfd);.
26450 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
26460 20 20 20 20 20 20 20 2a 70 45 78 69 73 74 73 20 *pExists
26470 3d 20 28 66 69 72 73 74 21 3d 30 29 3b 0a 20 20 = (first!=0);.
26480 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 }else if
26490 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 41 4e ( rc==SQLITE_CAN
264a0 54 4f 50 45 4e 20 29 7b 0a 20 20 20 20 20 20 20 TOPEN ){.
264b0 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 63 61 /* If we ca
264c0 6e 6e 6f 74 20 6f 70 65 6e 20 74 68 65 20 72 6f nnot open the ro
264d0 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 llback journal f
264e0 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 ile in order to
264f0 73 65 65 20 69 66 0a 20 20 20 20 20 20 20 20 20 see if.
26500 20 20 20 2a 2a 20 69 74 73 20 68 61 73 20 61 20 ** its has a
26510 7a 65 72 6f 20 68 65 61 64 65 72 2c 20 74 68 61 zero header, tha
26520 74 20 6d 69 67 68 74 20 62 65 20 64 75 65 20 74 t might be due t
26530 6f 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 2c 20 o an I/O error,
26540 6f 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a or. *
26550 2a 20 69 74 20 6d 69 67 68 74 20 62 65 20 64 75 * it might be du
26560 65 20 74 6f 20 74 68 65 20 72 61 63 65 20 63 6f e to the race co
26570 6e 64 69 74 69 6f 6e 20 64 65 73 63 72 69 62 65 ndition describe
26580 64 20 61 62 6f 76 65 20 61 6e 64 20 69 6e 0a 20 d above and in.
26590 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 69 ** ti
265a0 63 6b 65 74 20 23 33 38 38 33 2e 20 20 45 69 74 cket #3883. Eit
265b0 68 65 72 20 77 61 79 2c 20 61 73 73 75 6d 65 20 her way, assume
265c0 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c that the journal
265d0 20 69 73 20 68 6f 74 2e 0a 20 20 20 20 20 20 20 is hot..
265e0 20 20 20 20 20 2a 2a 20 54 68 69 73 20 6d 69 67 ** This mig
265f0 68 74 20 62 65 20 61 20 66 61 6c 73 65 20 70 6f ht be a false po
26600 73 69 74 69 76 65 2e 20 20 42 75 74 20 69 66 20 sitive. But if
26610 69 74 20 69 73 2c 20 74 68 65 6e 20 74 68 65 0a it is, then the.
26620 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 ** a
26630 75 74 6f 6d 61 74 69 63 20 6a 6f 75 72 6e 61 6c utomatic journal
26640 20 70 6c 61 79 62 61 63 6b 20 61 6e 64 20 72 65 playback and re
26650 63 6f 76 65 72 79 20 6d 65 63 68 61 6e 69 73 6d covery mechanism
26660 20 77 69 6c 6c 20 64 65 61 6c 0a 20 20 20 20 20 will deal.
26670 20 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 69 ** with i
26680 74 20 75 6e 64 65 72 20 61 6e 20 45 58 43 4c 55 t under an EXCLU
26690 53 49 56 45 20 6c 6f 63 6b 20 77 68 65 72 65 20 SIVE lock where
266a0 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 we do not need t
266b0 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a o. **
266c0 20 77 6f 72 72 79 20 73 6f 20 6d 75 63 68 20 77 worry so much w
266d0 69 74 68 20 72 61 63 65 20 63 6f 6e 64 69 74 69 ith race conditi
266e0 6f 6e 73 2e 0a 20 20 20 20 20 20 20 20 20 20 20 ons..
266f0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
26700 2a 70 45 78 69 73 74 73 20 3d 20 31 3b 0a 20 20 *pExists = 1;.
26710 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
26720 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
26730 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
26740 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
26750 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }.. return rc;.
26760 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
26770 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
26780 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 73 68 61 to obtain a sha
26790 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 red lock on the
267a0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
267b0 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 * It is illegal
267c0 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 to call sqlite3P
267d0 61 67 65 72 41 63 71 75 69 72 65 28 29 20 75 6e agerAcquire() un
267e0 74 69 6c 20 61 66 74 65 72 20 74 68 69 73 20 66 til after this f
267f0 75 6e 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 unction.** has b
26800 65 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 een successfully
26810 20 63 61 6c 6c 65 64 2e 20 49 66 20 61 20 73 68 called. If a sh
26820 61 72 65 64 2d 6c 6f 63 6b 20 69 73 20 61 6c 72 ared-lock is alr
26830 65 61 64 79 20 68 65 6c 64 20 77 68 65 6e 0a 2a eady held when.*
26840 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * this function
26850 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 is called, it is
26860 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 a no-op..**.**
26870 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 The following op
26880 65 72 61 74 69 6f 6e 73 20 61 72 65 20 61 6c 73 erations are als
26890 6f 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 o performed by t
268a0 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a his function..**
268b0 0a 2a 2a 20 20 20 31 29 20 49 66 20 74 68 65 20 .** 1) If the
268c0 70 61 67 65 72 20 69 73 20 63 75 72 72 65 6e 74 pager is current
268d0 6c 79 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f ly in PAGER_UNLO
268e0 43 4b 20 73 74 61 74 65 20 28 6e 6f 20 6c 6f 63 CK state (no loc
268f0 6b 20 68 65 6c 64 0a 2a 2a 20 20 20 20 20 20 6f k held.** o
26900 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
26910 69 6c 65 29 2c 20 74 68 65 6e 20 61 6e 20 61 74 ile), then an at
26920 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f tempt is made to
26930 20 6f 62 74 61 69 6e 20 61 0a 2a 2a 20 20 20 20 obtain a.**
26940 20 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 6f 6e SHARED lock on
26950 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
26960 6c 65 2e 20 49 6d 6d 65 64 69 61 74 65 6c 79 20 le. Immediately
26970 61 66 74 65 72 20 6f 62 74 61 69 6e 69 6e 67 0a after obtaining.
26980 2a 2a 20 20 20 20 20 20 74 68 65 20 53 48 41 52 ** the SHAR
26990 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 66 69 6c ED lock, the fil
269a0 65 2d 73 79 73 74 65 6d 20 69 73 20 63 68 65 63 e-system is chec
269b0 6b 65 64 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f ked for a hot-jo
269c0 75 72 6e 61 6c 2c 0a 2a 2a 20 20 20 20 20 20 77 urnal,.** w
269d0 68 69 63 68 20 69 73 20 70 6c 61 79 65 64 20 62 hich is played b
269e0 61 63 6b 20 69 66 20 70 72 65 73 65 6e 74 2e 20 ack if present.
269f0 46 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79 20 68 6f Following any ho
26a00 74 2d 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 t-journal .**
26a10 20 20 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 rollback, the
26a20 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
26a30 20 63 61 63 68 65 20 61 72 65 20 76 61 6c 69 64 cache are valid
26a40 61 74 65 64 20 62 79 20 63 68 65 63 6b 69 6e 67 ated by checking
26a50 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 27 63 68 .** the 'ch
26a60 61 6e 67 65 2d 63 6f 75 6e 74 65 72 27 20 66 69 ange-counter' fi
26a70 65 6c 64 20 6f 66 20 74 68 65 20 64 61 74 61 62 eld of the datab
26a80 61 73 65 20 66 69 6c 65 20 68 65 61 64 65 72 20 ase file header
26a90 61 6e 64 0a 2a 2a 20 20 20 20 20 20 64 69 73 63 and.** disc
26aa0 61 72 64 65 64 20 69 66 20 74 68 65 79 20 61 72 arded if they ar
26ab0 65 20 66 6f 75 6e 64 20 74 6f 20 62 65 20 69 6e e found to be in
26ac0 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 valid..**.** 2
26ad0 29 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 ) If the pager i
26ae0 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 s running in exc
26af0 6c 75 73 69 76 65 2d 6d 6f 64 65 2c 20 61 6e 64 lusive-mode, and
26b00 20 74 68 65 72 65 20 61 72 65 20 63 75 72 72 65 there are curre
26b10 6e 74 6c 79 0a 2a 2a 20 20 20 20 20 20 6e 6f 20 ntly.** no
26b20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 outstanding refe
26b30 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 70 61 rences to any pa
26b40 67 65 73 2c 20 61 6e 64 20 69 73 20 69 6e 20 74 ges, and is in t
26b50 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 0a he error state,.
26b60 2a 2a 20 20 20 20 20 20 74 68 65 6e 20 61 6e 20 ** then an
26b70 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 attempt is made
26b80 74 6f 20 63 6c 65 61 72 20 74 68 65 20 65 72 72 to clear the err
26b90 6f 72 20 73 74 61 74 65 20 62 79 20 64 69 73 63 or state by disc
26ba0 61 72 64 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74 arding.** t
26bb0 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
26bc0 68 65 20 70 61 67 65 20 63 61 63 68 65 20 61 6e he page cache an
26bd0 64 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 d rolling back a
26be0 6e 79 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 0a ny open journal.
26bf0 2a 2a 20 20 20 20 20 20 66 69 6c 65 2e 0a 2a 2a ** file..**
26c00 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 72 61 .** If the opera
26c10 74 69 6f 6e 20 64 65 73 63 72 69 62 65 64 20 62 tion described b
26c20 79 20 28 32 29 20 61 62 6f 76 65 20 69 73 20 6e y (2) above is n
26c30 6f 74 20 61 74 74 65 6d 70 74 65 64 2c 20 61 6e ot attempted, an
26c40 64 20 69 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 d if the.** page
26c50 72 20 69 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 r is in an error
26c60 20 73 74 61 74 65 20 6f 74 68 65 72 20 74 68 61 state other tha
26c70 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 77 68 n SQLITE_FULL wh
26c80 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 en this is calle
26c90 64 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 d,.** the error
26ca0 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 state error code
26cb0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 74 is returned. It
26cc0 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f is permitted to
26cd0 20 72 65 61 64 20 74 68 65 0a 2a 2a 20 64 61 74 read the.** dat
26ce0 61 62 61 73 65 20 77 68 65 6e 20 69 6e 20 53 51 abase when in SQ
26cf0 4c 49 54 45 5f 46 55 4c 4c 20 65 72 72 6f 72 20 LITE_FULL error
26d00 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 state..**.** Oth
26d10 65 72 77 69 73 65 2c 20 69 66 20 65 76 65 72 79 erwise, if every
26d20 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 thing is success
26d30 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 ful, SQLITE_OK i
26d40 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 s returned. If a
26d50 6e 0a 2a 2a 20 49 4f 20 65 72 72 6f 72 20 6f 63 n.** IO error oc
26d60 63 75 72 73 20 77 68 69 6c 65 20 6c 6f 63 6b 69 curs while locki
26d70 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2c ng the database,
26d80 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 61 20 checking for a
26d90 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 hot-journal.** f
26da0 69 6c 65 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 ile or rolling b
26db0 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 ack a journal fi
26dc0 6c 65 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 le, the IO error
26dd0 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
26de0 64 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 d..*/.int sqlite
26df0 33 50 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 3PagerSharedLock
26e00 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
26e10 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
26e20 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 TE_OK;
26e30 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
26e40 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 code */. int is
26e50 45 72 72 6f 72 52 65 73 65 74 20 3d 20 30 3b 20 ErrorReset = 0;
26e60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
26e70 54 72 75 65 20 69 66 20 72 65 63 6f 76 65 72 69 True if recoveri
26e80 6e 67 20 66 72 6f 6d 20 65 72 72 6f 72 20 73 74 ng from error st
26e90 61 74 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 ate */.. /* Thi
26ea0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c s routine is onl
26eb0 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 62 2d y called from b-
26ec0 74 72 65 65 20 61 6e 64 20 6f 6e 6c 79 20 77 68 tree and only wh
26ed0 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f 0a en there are no.
26ee0 20 20 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 ** outstanding
26ef0 20 70 61 67 65 73 20 2a 2f 0a 20 20 61 73 73 65 pages */. asse
26f00 72 74 28 20 73 71 6c 69 74 65 33 50 63 61 63 68 rt( sqlite3Pcach
26f10 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 eRefCount(pPager
26f20 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 29 3b ->pPCache)==0 );
26f30 0a 20 20 69 66 28 20 4e 45 56 45 52 28 4d 45 4d . if( NEVER(MEM
26f40 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 DB && pPager->er
26f50 72 43 6f 64 65 29 20 29 7b 20 72 65 74 75 72 6e rCode) ){ return
26f60 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
26f70 3b 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 ; }.. /* If thi
26f80 73 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e s database is in
26f90 20 61 6e 20 65 72 72 6f 72 2d 73 74 61 74 65 2c an error-state,
26fa0 20 6e 6f 77 20 69 73 20 61 20 63 68 61 6e 63 65 now is a chance
26fb0 20 74 6f 20 63 6c 65 61 72 0a 20 20 2a 2a 20 74 to clear. ** t
26fc0 68 65 20 65 72 72 6f 72 2e 20 44 69 73 63 61 72 he error. Discar
26fd0 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f d the contents o
26fe0 66 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 f the pager-cach
26ff0 65 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 0a 20 e and rollback.
27000 20 2a 2a 20 61 6e 79 20 68 6f 74 20 6a 6f 75 72 ** any hot jour
27010 6e 61 6c 20 69 6e 20 74 68 65 20 66 69 6c 65 2d nal in the file-
27020 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 69 system.. */. i
27030 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f f( pPager->errCo
27040 64 65 20 29 7b 0a 20 20 20 20 69 66 28 20 69 73 de ){. if( is
27050 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
27060 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f ) || pPager->zJo
27070 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 69 urnal ){. i
27080 73 45 72 72 6f 72 52 65 73 65 74 20 3d 20 31 3b sErrorReset = 1;
27090 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 . }. pPage
270a0 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c r->errCode = SQL
270b0 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 70 61 67 65 ITE_OK;. page
270c0 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b r_reset(pPager);
270d0 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 61 67 65 . }.. if( page
270e0 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 rUseWal(pPager)
270f0 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 ){. rc = page
27100 72 42 65 67 69 6e 52 65 61 64 54 72 61 6e 73 61 rBeginReadTransa
27110 63 74 69 6f 6e 28 70 50 61 67 65 72 29 3b 0a 20 ction(pPager);.
27120 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 }else if( pPage
27130 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f r->state==PAGER_
27140 55 4e 4c 4f 43 4b 20 7c 7c 20 69 73 45 72 72 6f UNLOCK || isErro
27150 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 73 71 rReset ){. sq
27160 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 6f 6e 73 lite3_vfs * cons
27170 74 20 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d t pVfs = pPager-
27180 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 69 >pVfs;. int i
27190 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b sHotJournal = 0;
271a0 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 . assert( !ME
271b0 4d 44 42 20 29 3b 0a 20 20 20 20 61 73 73 65 72 MDB );. asser
271c0 74 28 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 t( sqlite3Pcache
271d0 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d RefCount(pPager-
271e0 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 29 3b 0a >pPCache)==0 );.
271f0 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
27200 6e 6f 52 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 noReadlock ){.
27210 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
27220 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a er->readOnly );.
27230 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 pPager->st
27240 61 74 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52 ate = PAGER_SHAR
27250 45 44 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ED;. }else{.
27260 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
27270 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 wait_on_lock(pPa
27280 67 65 72 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b ger, SHARED_LOCK
27290 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
272a0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
272b0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
272c0 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 ager->state==PAG
272d0 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20 ER_UNLOCK );.
272e0 20 20 20 20 20 72 65 74 75 72 6e 20 70 61 67 65 return page
272f0 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 r_error(pPager,
27300 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc);. }.
27310 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
27320 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 53 48 Pager->state>=SH
27330 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 ARED_LOCK );..
27340 20 20 2f 2a 20 49 66 20 61 20 6a 6f 75 72 6e 61 /* If a journa
27350 6c 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20 61 l file exists, a
27360 6e 64 20 74 68 65 72 65 20 69 73 20 6e 6f 20 52 nd there is no R
27370 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 ESERVED lock on
27380 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 the. ** datab
27390 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 ase file, then i
273a0 74 20 65 69 74 68 65 72 20 6e 65 65 64 73 20 74 t either needs t
273b0 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b o be played back
273c0 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 or deleted..
273d0 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73 45 */. if( !isE
273e0 72 72 6f 72 52 65 73 65 74 20 29 7b 0a 20 20 20 rrorReset ){.
273f0 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
27400 72 2d 3e 73 74 61 74 65 20 3c 3d 20 50 41 47 45 r->state <= PAGE
27410 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20 20 20 R_SHARED );.
27420 20 20 72 63 20 3d 20 68 61 73 48 6f 74 4a 6f 75 rc = hasHotJou
27430 72 6e 61 6c 28 70 50 61 67 65 72 2c 20 26 69 73 rnal(pPager, &is
27440 48 6f 74 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 HotJournal);.
27450 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
27460 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
27470 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 goto failed;.
27480 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
27490 66 28 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 f( isErrorReset
274a0 7c 7c 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 || isHotJournal
274b0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 20 ){. /* Get
274c0 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
274d0 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
274e0 65 20 66 69 6c 65 2e 20 41 74 20 74 68 69 73 20 e file. At this
274f0 70 6f 69 6e 74 20 69 74 20 69 73 0a 20 20 20 20 point it is.
27500 20 20 2a 2a 20 69 6d 70 6f 72 74 61 6e 74 20 74 ** important t
27510 68 61 74 20 61 20 52 45 53 45 52 56 45 44 20 6c hat a RESERVED l
27520 6f 63 6b 20 69 73 20 6e 6f 74 20 6f 62 74 61 69 ock is not obtai
27530 6e 65 64 20 6f 6e 20 74 68 65 20 77 61 79 20 74 ned on the way t
27540 6f 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 45 o the. ** E
27550 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 XCLUSIVE lock. I
27560 66 20 69 74 20 77 65 72 65 2c 20 61 6e 6f 74 68 f it were, anoth
27570 65 72 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 er process might
27580 20 6f 70 65 6e 20 74 68 65 0a 20 20 20 20 20 20 open the.
27590 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
275a0 2c 20 64 65 74 65 63 74 20 74 68 65 20 52 45 53 , detect the RES
275b0 45 52 56 45 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 ERVED lock, and
275c0 63 6f 6e 63 6c 75 64 65 20 74 68 61 74 20 74 68 conclude that th
275d0 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 e. ** datab
275e0 61 73 65 20 69 73 20 73 61 66 65 20 74 6f 20 72 ase is safe to r
275f0 65 61 64 20 77 68 69 6c 65 20 74 68 69 73 20 70 ead while this p
27600 72 6f 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 rocess is still
27610 72 6f 6c 6c 69 6e 67 20 74 68 65 20 0a 20 20 20 rolling the .
27620 20 20 20 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 ** hot-journa
27630 6c 20 62 61 63 6b 2e 0a 20 20 20 20 20 20 2a 2a l back.. **
27640 20 0a 20 20 20 20 20 20 2a 2a 20 42 65 63 61 75 . ** Becau
27650 73 65 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 se the intermedi
27660 61 74 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 ate RESERVED loc
27670 6b 20 69 73 20 6e 6f 74 20 72 65 71 75 65 73 74 k is not request
27680 65 64 2c 20 61 6e 79 0a 20 20 20 20 20 20 2a 2a ed, any. **
27690 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 61 other process a
276a0 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 61 63 63 ttempting to acc
276b0 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 ess the database
276c0 20 66 69 6c 65 20 77 69 6c 6c 20 67 65 74 20 74 file will get t
276d0 6f 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 69 73 o . ** this
276e0 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f point in the co
276f0 64 65 20 61 6e 64 20 66 61 69 6c 20 74 6f 20 6f de and fail to o
27700 62 74 61 69 6e 20 69 74 73 20 6f 77 6e 20 45 58 btain its own EX
27710 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 0a 20 20 CLUSIVE lock .
27720 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64 61 ** on the da
27730 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 tabase file..
27740 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
27750 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 45 58 pPager->state<EX
27760 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a CLUSIVE_LOCK ){.
27770 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
27780 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 ite3OsLock(pPage
27790 72 2d 3e 66 64 2c 20 45 58 43 4c 55 53 49 56 45 r->fd, EXCLUSIVE
277a0 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 _LOCK);.
277b0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
277c0 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
277d0 63 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28 c = pager_error(
277e0 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 pPager, rc);.
277f0 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c goto fail
27800 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ed;. }.
27810 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 pPager->st
27820 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c ate = PAGER_EXCL
27830 55 53 49 56 45 3b 0a 20 20 20 20 20 20 7d 0a 20 USIVE;. }.
27840 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 . /* Open t
27850 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 he journal for r
27860 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 ead/write access
27870 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 . This is becaus
27880 65 20 69 6e 20 0a 20 20 20 20 20 20 2a 2a 20 65 e in . ** e
27890 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 xclusive-access
278a0 6d 6f 64 65 20 74 68 65 20 66 69 6c 65 20 64 65 mode the file de
278b0 73 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65 scriptor will be
278c0 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e 64 0a 20 kept open and.
278d0 20 20 20 20 20 2a 2a 20 70 6f 73 73 69 62 6c 79 ** possibly
278e0 20 75 73 65 64 20 66 6f 72 20 61 20 74 72 61 6e used for a tran
278f0 73 61 63 74 69 6f 6e 20 6c 61 74 65 72 20 6f 6e saction later on
27900 2e 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d . On some system
27910 73 2c 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 s, the. **
27920 4f 73 54 72 75 6e 63 61 74 65 28 29 20 63 61 6c OsTruncate() cal
27930 6c 20 75 73 65 64 20 69 6e 20 65 78 63 6c 75 73 l used in exclus
27940 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 ive-access mode
27950 61 6c 73 6f 20 72 65 71 75 69 72 65 73 0a 20 20 also requires.
27960 20 20 20 20 2a 2a 20 61 20 72 65 61 64 2f 77 72 ** a read/wr
27970 69 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e ite file handle.
27980 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
27990 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 if( !isOpen(pPag
279a0 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 er->jfd) ){.
279b0 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 int res;.
279c0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
279d0 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 70 3OsAccess(pVfs,p
279e0 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c Pager->zJournal,
279f0 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
27a00 49 53 54 53 2c 26 72 65 73 29 3b 0a 20 20 20 20 ISTS,&res);.
27a10 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
27a20 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
27a30 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 if( res ){.
27a40 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 6f int fo
27a50 75 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 ut = 0;.
27a60 20 20 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49 int f = SQLI
27a70 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
27a80 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 E|SQLITE_OPEN_MA
27a90 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 IN_JOURNAL;.
27aa0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
27ab0 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c !pPager->tempFil
27ac0 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 e );.
27ad0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f rc = sqlite3OsO
27ae0 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 pen(pVfs, pPager
27af0 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 ->zJournal, pPag
27b00 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 6f 75 er->jfd, f, &fou
27b10 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 t);.
27b20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 assert( rc!=SQLI
27b30 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 TE_OK || isOpen(
27b40 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a pPager->jfd) );.
27b50 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
27b60 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
27b70 20 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 45 fout&SQLITE_OPE
27b80 4e 5f 52 45 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 N_READONLY ){.
27b90 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
27ba0 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e SQLITE_CANTOPEN
27bb0 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 _BKPT;.
27bc0 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c sqlite3OsCl
27bd0 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 ose(pPager->jfd)
27be0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
27bf0 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
27c00 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 . /*
27c10 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 64 If the journal d
27c20 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 69 oes not exist, i
27c30 74 20 75 73 75 61 6c 6c 79 20 6d 65 61 6e 73 20 t usually means
27c40 74 68 61 74 20 73 6f 6d 65 20 0a 20 20 20 20 20 that some .
27c50 20 20 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 ** other
27c60 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 6e 61 67 connection manag
27c70 65 64 20 74 6f 20 67 65 74 20 69 6e 20 61 6e 64 ed to get in and
27c80 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 20 62 65 roll it back be
27c90 66 6f 72 65 20 0a 20 20 20 20 20 20 20 20 20 20 fore .
27ca0 20 20 2a 2a 20 74 68 69 73 20 63 6f 6e 6e 65 63 ** this connec
27cb0 74 69 6f 6e 20 6f 62 74 61 69 6e 65 64 20 74 68 tion obtained th
27cc0 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b e exclusive lock
27cd0 20 61 62 6f 76 65 2e 20 4f 72 2c 20 69 74 20 0a above. Or, it .
27ce0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6d ** m
27cf0 61 79 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 ay mean that the
27d00 20 70 61 67 65 72 20 77 61 73 20 69 6e 20 74 68 pager was in th
27d10 65 20 65 72 72 6f 72 2d 73 74 61 74 65 20 77 68 e error-state wh
27d20 65 6e 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 en this.
27d30 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 ** function
27d40 77 61 73 20 63 61 6c 6c 65 64 20 61 6e 64 20 74 was called and t
27d50 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
27d60 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 does not exist.
27d70 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
27d80 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 rc = pager_end_t
27d90 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 ransaction(pPage
27da0 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 r, 0);.
27db0 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
27dc0 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 }. if( r
27dd0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
27de0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 goto fai
27df0 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 led;. }..
27e00 20 20 20 20 2f 2a 20 52 65 73 65 74 20 74 68 65 /* Reset the
27e10 20 6a 6f 75 72 6e 61 6c 20 73 74 61 74 75 73 20 journal status
27e20 66 69 65 6c 64 73 20 74 6f 20 69 6e 64 69 63 61 fields to indica
27e30 74 65 73 20 74 68 61 74 20 77 65 20 68 61 76 65 tes that we have
27e40 20 6e 6f 0a 20 20 20 20 20 20 2a 2a 20 72 6f 6c no. ** rol
27e50 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 61 74 lback journal at
27e60 20 74 68 69 73 20 74 69 6d 65 2e 20 2a 2f 0a 20 this time. */.
27e70 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 pPager->jou
27e80 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b rnalStarted = 0;
27e90 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a . pPager->j
27ea0 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 ournalOff = 0;.
27eb0 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 74 pPager->set
27ec0 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 Master = 0;.
27ed0 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
27ee0 6c 48 64 72 20 3d 20 30 3b 0a 20 0a 20 20 20 20 lHdr = 0;. .
27ef0 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 /* Make sure t
27f00 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
27f10 68 61 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20 has been synced
27f20 74 6f 20 64 69 73 6b 2e 20 2a 2f 0a 20 0a 20 20 to disk. */. .
27f30 20 20 20 20 2f 2a 20 50 6c 61 79 62 61 63 6b 20 /* Playback
27f40 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20 6a and delete the j
27f50 6f 75 72 6e 61 6c 2e 20 20 44 72 6f 70 20 74 68 ournal. Drop th
27f60 65 20 64 61 74 61 62 61 73 65 20 77 72 69 74 65 e database write
27f70 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 63 6b 20 61 . ** lock a
27f80 6e 64 20 72 65 61 63 71 75 69 72 65 20 74 68 65 nd reacquire the
27f90 20 72 65 61 64 20 6c 6f 63 6b 2e 20 50 75 72 67 read lock. Purg
27fa0 65 20 74 68 65 20 63 61 63 68 65 20 62 65 66 6f e the cache befo
27fb0 72 65 0a 20 20 20 20 20 20 2a 2a 20 70 6c 61 79 re. ** play
27fc0 69 6e 67 20 62 61 63 6b 20 74 68 65 20 68 6f 74 ing back the hot
27fd0 2d 6a 6f 75 72 6e 61 6c 20 73 6f 20 74 68 61 74 -journal so that
27fe0 20 77 65 20 64 6f 6e 27 74 20 65 6e 64 20 75 70 we don't end up
27ff0 20 77 69 74 68 0a 20 20 20 20 20 20 2a 2a 20 61 with. ** a
28000 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 63 n inconsistent c
28010 61 63 68 65 2e 20 20 53 79 6e 63 20 74 68 65 20 ache. Sync the
28020 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f hot journal befo
28030 72 65 20 70 6c 61 79 69 6e 67 0a 20 20 20 20 20 re playing.
28040 20 2a 2a 20 69 74 20 62 61 63 6b 20 73 69 6e 63 ** it back sinc
28050 65 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 e the process th
28060 61 74 20 63 72 61 73 68 65 64 20 61 6e 64 20 6c at crashed and l
28070 65 66 74 20 74 68 65 20 68 6f 74 20 6a 6f 75 72 eft the hot jour
28080 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f nal. ** pro
28090 62 61 62 6c 79 20 64 69 64 20 6e 6f 74 20 73 79 bably did not sy
280a0 6e 63 20 69 74 20 61 6e 64 20 77 65 20 61 72 65 nc it and we are
280b0 20 72 65 71 75 69 72 65 64 20 74 6f 20 61 6c 77 required to alw
280c0 61 79 73 20 73 79 6e 63 0a 20 20 20 20 20 20 2a ays sync. *
280d0 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 62 65 * the journal be
280e0 66 6f 72 65 20 70 6c 61 79 69 6e 67 20 69 74 20 fore playing it
280f0 62 61 63 6b 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 back.. */.
28100 20 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 if( isOpen(
28110 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a pPager->jfd) ){.
28120 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 rc = pag
28130 65 72 53 79 6e 63 48 6f 74 4a 6f 75 72 6e 61 6c erSyncHotJournal
28140 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 (pPager);.
28150 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
28160 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
28170 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 rc = pager_play
28180 62 61 63 6b 28 70 50 61 67 65 72 2c 20 31 29 3b back(pPager, 1);
28190 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
281a0 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
281b0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
281c0 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72 rc = pager_err
281d0 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a or(pPager, rc);.
281e0 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 goto f
281f0 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d ailed;. }
28200 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
28210 73 73 65 72 74 28 20 28 70 50 61 67 65 72 2d 3e ssert( (pPager->
28220 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 41 state==PAGER_SHA
28230 52 45 44 29 0a 20 20 20 20 20 20 20 20 20 20 20 RED).
28240 7c 7c 20 28 70 50 61 67 65 72 2d 3e 65 78 63 6c || (pPager->excl
28250 75 73 69 76 65 4d 6f 64 65 20 26 26 20 70 50 61 usiveMode && pPa
28260 67 65 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 52 ger->state>PAGER
28270 5f 53 48 41 52 45 44 29 0a 20 20 20 20 20 20 29 _SHARED). )
28280 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 ;. }.. if(
28290 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 pPager->pBackup
282a0 20 7c 7c 20 73 71 6c 69 74 65 33 50 63 61 63 68 || sqlite3Pcach
282b0 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 ePagecount(pPage
282c0 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20 29 7b r->pPCache)>0 ){
282d0 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 68 . /* The sh
282e0 61 72 65 64 2d 6c 6f 63 6b 20 68 61 73 20 6a 75 ared-lock has ju
282f0 73 74 20 62 65 65 6e 20 61 63 71 75 69 72 65 64 st been acquired
28300 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
28310 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 file. ** a
28320 6e 64 20 74 68 65 72 65 20 61 72 65 20 61 6c 72 nd there are alr
28330 65 61 64 79 20 70 61 67 65 73 20 69 6e 20 74 68 eady pages in th
28340 65 20 63 61 63 68 65 20 28 66 72 6f 6d 20 61 20 e cache (from a
28350 70 72 65 76 69 6f 75 73 0a 20 20 20 20 20 20 2a previous. *
28360 2a 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 * read or write
28370 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 20 43 transaction). C
28380 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 heck to see if t
28390 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 he database.
283a0 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f ** has been mo
283b0 64 69 66 69 65 64 2e 20 20 49 66 20 74 68 65 20 dified. If the
283c0 64 61 74 61 62 61 73 65 20 68 61 73 20 63 68 61 database has cha
283d0 6e 67 65 64 2c 20 66 6c 75 73 68 20 74 68 65 0a nged, flush the.
283e0 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 0a ** cache..
283f0 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
28400 2a 20 44 61 74 61 62 61 73 65 20 63 68 61 6e 67 * Database chang
28410 65 73 20 69 73 20 64 65 74 65 63 74 65 64 20 62 es is detected b
28420 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 35 20 y looking at 15
28430 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 67 0a bytes beginning.
28440 20 20 20 20 20 20 2a 2a 20 61 74 20 6f 66 66 73 ** at offs
28450 65 74 20 32 34 20 69 6e 74 6f 20 74 68 65 20 66 et 24 into the f
28460 69 6c 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 ile. The first
28470 34 20 6f 66 20 74 68 65 73 65 20 31 36 20 62 79 4 of these 16 by
28480 74 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a 2a tes are. **
28490 20 61 20 33 32 2d 62 69 74 20 63 6f 75 6e 74 65 a 32-bit counte
284a0 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d r that is increm
284b0 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 68 20 ented with each
284c0 63 68 61 6e 67 65 2e 20 20 54 68 65 0a 20 20 20 change. The.
284d0 20 20 20 2a 2a 20 6f 74 68 65 72 20 62 79 74 65 ** other byte
284e0 73 20 63 68 61 6e 67 65 20 72 61 6e 64 6f 6d 6c s change randoml
284f0 79 20 77 69 74 68 20 65 61 63 68 20 66 69 6c 65 y with each file
28500 20 63 68 61 6e 67 65 20 77 68 65 6e 0a 20 20 20 change when.
28510 20 20 20 2a 2a 20 61 20 63 6f 64 65 63 20 69 73 ** a codec is
28520 20 69 6e 20 75 73 65 2e 0a 20 20 20 20 20 20 2a in use.. *
28530 2a 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 * . ** Ther
28540 65 20 69 73 20 61 20 76 61 6e 69 73 68 69 6e 67 e is a vanishing
28550 6c 79 20 73 6d 61 6c 6c 20 63 68 61 6e 63 65 20 ly small chance
28560 74 68 61 74 20 61 20 63 68 61 6e 67 65 20 77 69 that a change wi
28570 6c 6c 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 20 ll not be .
28580 20 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 20 54 ** detected. T
28590 68 65 20 63 68 61 6e 63 65 20 6f 66 20 61 6e 20 he chance of an
285a0 75 6e 64 65 74 65 63 74 65 64 20 63 68 61 6e 67 undetected chang
285b0 65 20 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68 e is so small th
285c0 61 74 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 63 at. ** it c
285d0 61 6e 20 62 65 20 6e 65 67 6c 65 63 74 65 64 2e an be neglected.
285e0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
285f0 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 int nPage;.
28600 20 63 68 61 72 20 64 62 46 69 6c 65 56 65 72 73 char dbFileVers
28610 5b 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e [sizeof(pPager->
28620 64 62 46 69 6c 65 56 65 72 73 29 5d 3b 0a 20 20 dbFileVers)];.
28630 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
28640 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 Pagecount(pPager
28650 2c 20 26 6e 50 61 67 65 29 3b 0a 0a 20 20 20 20 , &nPage);..
28660 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 if( pPager->er
28670 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20 rCode ){.
28680 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 rc = pPager->er
28690 72 43 6f 64 65 3b 0a 20 20 20 20 20 20 20 20 67 rCode;. g
286a0 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 oto failed;.
286b0 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 6e }.. if( n
286c0 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 Page>0 ){.
286d0 20 20 49 4f 54 52 41 43 45 28 28 22 43 4b 56 45 IOTRACE(("CKVE
286e0 52 53 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 RS %p %d\n", pPa
286f0 67 65 72 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 ger, sizeof(dbFi
28700 6c 65 56 65 72 73 29 29 29 3b 0a 20 20 20 20 20 leVers)));.
28710 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
28720 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 sRead(pPager->fd
28730 2c 20 26 64 62 46 69 6c 65 56 65 72 73 2c 20 73 , &dbFileVers, s
28740 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 izeof(dbFileVers
28750 29 2c 20 32 34 29 3b 0a 20 20 20 20 20 20 20 20 ), 24);.
28760 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
28770 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 K ){. g
28780 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 oto failed;.
28790 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
287a0 65 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 e{. memse
287b0 74 28 64 62 46 69 6c 65 56 65 72 73 2c 20 30 2c t(dbFileVers, 0,
287c0 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 sizeof(dbFileVe
287d0 72 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 rs));. }..
287e0 20 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 if( memcmp(
287f0 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 pPager->dbFileVe
28800 72 73 2c 20 64 62 46 69 6c 65 56 65 72 73 2c 20 rs, dbFileVers,
28810 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 sizeof(dbFileVer
28820 73 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 s))!=0 ){.
28830 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 pager_reset(pP
28840 61 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ager);. }.
28850 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
28860 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 pPager->exclusi
28870 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 72 veMode || pPager
28880 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 ->state==PAGER_S
28890 48 41 52 45 44 20 29 3b 0a 0a 20 20 20 20 2f 2a HARED );.. /*
288a0 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 57 If there is a W
288b0 41 4c 20 66 69 6c 65 20 69 6e 20 74 68 65 20 66 AL file in the f
288c0 69 6c 65 2d 73 79 73 74 65 6d 2c 20 6f 70 65 6e ile-system, open
288d0 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 69 this database i
288e0 6e 20 57 41 4c 0a 20 20 20 20 2a 2a 20 6d 6f 64 n WAL. ** mod
288f0 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 e. Otherwise, th
28900 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 e following func
28910 74 69 6f 6e 20 63 61 6c 6c 20 69 73 20 61 20 6e tion call is a n
28920 6f 2d 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 o-op.. */.
28930 20 72 63 20 3d 20 70 61 67 65 72 4f 70 65 6e 57 rc = pagerOpenW
28940 61 6c 49 66 50 72 65 73 65 6e 74 28 70 50 61 67 alIfPresent(pPag
28950 65 72 29 3b 0a 20 20 7d 0a 0a 20 66 61 69 6c 65 er);. }.. faile
28960 64 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c d:. if( rc!=SQL
28970 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a ITE_OK ){. /*
28980 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 pager_unlock()
28990 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 65 is a no-op for e
289a0 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 61 6e xclusive mode an
289b0 64 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 d in-memory data
289c0 62 61 73 65 73 2e 20 2a 2f 0a 20 20 20 20 70 61 bases. */. pa
289d0 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 ger_unlock(pPage
289e0 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e r);. }. return
289f0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 rc;.}../*.** If
28a00 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 the reference c
28a10 6f 75 6e 74 20 68 61 73 20 72 65 61 63 68 65 64 ount has reached
28a20 20 7a 65 72 6f 2c 20 72 6f 6c 6c 62 61 63 6b 20 zero, rollback
28a30 61 6e 79 20 61 63 74 69 76 65 0a 2a 2a 20 74 72 any active.** tr
28a40 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 75 6e ansaction and un
28a50 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 72 2e 0a lock the pager..
28a60 2a 2a 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 6e **.** Except, in
28a70 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58 locking_mode=EX
28a80 43 4c 55 53 49 56 45 20 77 68 65 6e 20 74 68 65 CLUSIVE when the
28a90 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 74 6f re is nothing to
28aa0 20 69 6e 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 in.** the rollb
28ab0 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 ack journal, the
28ac0 20 75 6e 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 70 unlock is not p
28ad0 65 72 66 6f 72 6d 65 64 20 61 6e 64 20 74 68 65 erformed and the
28ae0 72 65 20 69 73 0a 2a 2a 20 6e 6f 74 68 69 6e 67 re is.** nothing
28af0 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 73 6f to rollback, so
28b00 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
28b10 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 20 0a 73 74 a no-op..*/ .st
28b20 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 55 atic void pagerU
28b30 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 50 61 nlockIfUnused(Pa
28b40 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
28b50 69 66 28 20 28 73 71 6c 69 74 65 33 50 63 61 63 if( (sqlite3Pcac
28b60 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 heRefCount(pPage
28b70 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 29 0a r->pPCache)==0).
28b80 20 20 20 26 26 20 28 21 70 50 61 67 65 72 2d 3e && (!pPager->
28b90 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c exclusiveMode ||
28ba0 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
28bb0 4f 66 66 3e 30 29 20 0a 20 20 29 7b 0a 20 20 20 Off>0) . ){.
28bc0 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 pagerUnlockAndR
28bd0 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b ollback(pPager);
28be0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 . }.}../*.** Ac
28bf0 71 75 69 72 65 20 61 20 72 65 66 65 72 65 6e 63 quire a referenc
28c00 65 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 e to page number
28c10 20 70 67 6e 6f 20 69 6e 20 70 61 67 65 72 20 70 pgno in pager p
28c20 50 61 67 65 72 20 28 61 20 70 61 67 65 0a 2a 2a Pager (a page.**
28c30 20 72 65 66 65 72 65 6e 63 65 20 68 61 73 20 74 reference has t
28c40 79 70 65 20 44 62 50 61 67 65 2a 29 2e 20 49 66 ype DbPage*). If
28c50 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 72 the requested r
28c60 65 66 65 72 65 6e 63 65 20 69 73 20 0a 2a 2a 20 eference is .**
28c70 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 62 74 successfully obt
28c80 61 69 6e 65 64 2c 20 69 74 20 69 73 20 63 6f 70 ained, it is cop
28c90 69 65 64 20 74 6f 20 2a 70 70 50 61 67 65 20 61 ied to *ppPage a
28ca0 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 nd SQLITE_OK ret
28cb0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 urned..**.** If
28cc0 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 the requested pa
28cd0 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e ge is already in
28ce0 20 74 68 65 20 63 61 63 68 65 2c 20 69 74 20 69 the cache, it i
28cf0 73 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 20 s returned. .**
28d00 4f 74 68 65 72 77 69 73 65 2c 20 61 20 6e 65 77 Otherwise, a new
28d10 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 page object is
28d20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f allocated and po
28d30 70 75 6c 61 74 65 64 20 77 69 74 68 20 64 61 74 pulated with dat
28d40 61 0a 2a 2a 20 72 65 61 64 20 66 72 6f 6d 20 74 a.** read from t
28d50 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
28d60 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 2c . In some cases,
28d70 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75 the pcache modu
28d80 6c 65 20 6d 61 79 0a 2a 2a 20 63 68 6f 6f 73 65 le may.** choose
28d90 20 6e 6f 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 not to allocate
28da0 20 61 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 a new page obje
28db0 63 74 20 61 6e 64 20 6d 61 79 20 72 65 75 73 65 ct and may reuse
28dc0 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 an existing.**
28dd0 6f 62 6a 65 63 74 20 77 69 74 68 20 6e 6f 20 6f object with no o
28de0 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 utstanding refer
28df0 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ences..**.** The
28e00 20 65 78 74 72 61 20 64 61 74 61 20 61 70 70 65 extra data appe
28e10 6e 64 65 64 20 74 6f 20 61 20 70 61 67 65 20 69 nded to a page i
28e20 73 20 61 6c 77 61 79 73 20 69 6e 69 74 69 61 6c s always initial
28e30 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73 20 74 68 ized to zeros th
28e40 65 20 0a 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 e .** first time
28e50 20 61 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65 a page is loade
28e60 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e 20 49 d into memory. I
28e70 66 20 74 68 65 20 70 61 67 65 20 72 65 71 75 65 f the page reque
28e80 73 74 65 64 20 69 73 20 0a 2a 2a 20 61 6c 72 65 sted is .** alre
28e90 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 ady in the cache
28ea0 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
28eb0 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 ion is called, t
28ec0 68 65 6e 20 74 68 65 20 65 78 74 72 61 0a 2a 2a hen the extra.**
28ed0 20 64 61 74 61 20 69 73 20 6c 65 66 74 20 61 73 data is left as
28ee0 20 69 74 20 77 61 73 20 77 68 65 6e 20 74 68 65 it was when the
28ef0 20 70 61 67 65 20 6f 62 6a 65 63 74 20 77 61 73 page object was
28f00 20 6c 61 73 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a last used..**.*
28f10 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
28f20 65 20 69 6d 61 67 65 20 69 73 20 73 6d 61 6c 6c e image is small
28f30 65 72 20 74 68 61 6e 20 74 68 65 20 72 65 71 75 er than the requ
28f40 65 73 74 65 64 20 70 61 67 65 20 6f 72 20 69 66 ested page or if
28f50 20 61 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 a .** non-zero
28f60 76 61 6c 75 65 20 69 73 20 70 61 73 73 65 64 20 value is passed
28f70 61 73 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 as the noContent
28f80 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20 74 parameter and t
28f90 68 65 20 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 he .** requested
28fa0 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 page is not alr
28fb0 65 61 64 79 20 73 74 6f 72 65 64 20 69 6e 20 74 eady stored in t
28fc0 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e 20 6e he cache, then n
28fd0 6f 20 0a 2a 2a 20 61 63 74 75 61 6c 20 64 69 73 o .** actual dis
28fe0 6b 20 72 65 61 64 20 6f 63 63 75 72 73 2e 20 49 k read occurs. I
28ff0 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 n this case the
29000 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20 memory image of
29010 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 69 73 20 the .** page is
29020 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 initialized to a
29030 6c 6c 20 7a 65 72 6f 73 2e 20 0a 2a 2a 0a 2a 2a ll zeros. .**.**
29040 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 If noContent is
29050 20 74 72 75 65 2c 20 69 74 20 6d 65 61 6e 73 20 true, it means
29060 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 63 that we do not c
29070 61 72 65 20 61 62 6f 75 74 20 74 68 65 20 63 6f are about the co
29080 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 ntents.** of the
29090 20 70 61 67 65 2e 20 54 68 69 73 20 6f 63 63 75 page. This occu
290a0 72 73 20 69 6e 20 74 77 6f 20 73 65 70 65 72 61 rs in two sepera
290b0 74 65 20 73 63 65 6e 61 72 69 6f 73 3a 0a 2a 2a te scenarios:.**
290c0 0a 2a 2a 20 20 20 61 29 20 57 68 65 6e 20 72 65 .** a) When re
290d0 61 64 69 6e 67 20 61 20 66 72 65 65 2d 6c 69 73 ading a free-lis
290e0 74 20 6c 65 61 66 20 70 61 67 65 20 66 72 6f 6d t leaf page from
290f0 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 61 the database, a
29100 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 62 29 20 57 68 nd.**.** b) Wh
29110 65 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 20 69 en a savepoint i
29120 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 s being rolled b
29130 61 63 6b 20 61 6e 64 20 77 65 20 6e 65 65 64 20 ack and we need
29140 74 6f 20 6c 6f 61 64 0a 2a 2a 20 20 20 20 20 20 to load.**
29150 61 20 6e 65 77 20 70 61 67 65 20 69 6e 74 6f 20 a new page into
29160 74 68 65 20 63 61 63 68 65 20 74 6f 20 62 65 20 the cache to be
29170 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 filled with the
29180 64 61 74 61 20 72 65 61 64 0a 2a 2a 20 20 20 20 data read.**
29190 20 20 66 72 6f 6d 20 74 68 65 20 73 61 76 65 70 from the savep
291a0 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a oint journal..**
291b0 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 .** If noContent
291c0 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 is true, then t
291d0 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 he data returned
291e0 20 69 73 20 7a 65 72 6f 65 64 20 69 6e 73 74 65 is zeroed inste
291f0 61 64 20 6f 66 0a 2a 2a 20 62 65 69 6e 67 20 72 ad of.** being r
29200 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 ead from the dat
29210 61 62 61 73 65 2e 20 41 64 64 69 74 69 6f 6e 61 abase. Additiona
29220 6c 6c 79 2c 20 74 68 65 20 62 69 74 73 20 63 6f lly, the bits co
29230 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 rresponding.** t
29240 6f 20 70 67 6e 6f 20 69 6e 20 50 61 67 65 72 2e o pgno in Pager.
29250 70 49 6e 4a 6f 75 72 6e 61 6c 20 28 62 69 74 76 pInJournal (bitv
29260 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c 72 65 ec of pages alre
29270 61 64 79 20 77 72 69 74 74 65 6e 20 74 6f 20 74 ady written to t
29280 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 he.** journal fi
29290 6c 65 29 20 61 6e 64 20 74 68 65 20 50 61 67 65 le) and the Page
292a0 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 rSavepoint.pInSa
292b0 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73 20 vepoint bitvecs
292c0 6f 66 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 73 of any open.** s
292d0 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73 65 avepoints are se
292e0 74 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69 66 t. This means if
292f0 20 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 64 the page is mad
29300 65 20 77 72 69 74 61 62 6c 65 20 61 74 20 61 6e e writable at an
29310 79 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e 20 74 68 y.** point in th
29320 65 20 66 75 74 75 72 65 2c 20 75 73 69 6e 67 20 e future, using
29330 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 a call to sqlite
29340 33 50 61 67 65 72 57 72 69 74 65 28 29 2c 20 69 3PagerWrite(), i
29350 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 77 ts contents.** w
29360 69 6c 6c 20 6e 6f 74 20 62 65 20 6a 6f 75 72 6e ill not be journ
29370 61 6c 65 64 2e 20 54 68 69 73 20 73 61 76 65 73 aled. This saves
29380 20 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 IO..**.** The a
29390 63 71 75 69 73 69 74 69 6f 6e 20 6d 69 67 68 74 cquisition might
293a0 20 66 61 69 6c 20 66 6f 72 20 73 65 76 65 72 61 fail for severa
293b0 6c 20 72 65 61 73 6f 6e 73 2e 20 20 49 6e 20 61 l reasons. In a
293c0 6c 6c 20 63 61 73 65 73 2c 0a 2a 2a 20 61 6e 20 ll cases,.** an
293d0 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f appropriate erro
293e0 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
293f0 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65 20 69 ed and *ppPage i
29400 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a s set to NULL..*
29410 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 *.** See also sq
29420 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 lite3PagerLookup
29430 28 29 2e 20 20 42 6f 74 68 20 74 68 69 73 20 72 (). Both this r
29440 6f 75 74 69 6e 65 20 61 6e 64 20 4c 6f 6f 6b 75 outine and Looku
29450 70 28 29 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 p() attempt.** t
29460 6f 20 66 69 6e 64 20 61 20 70 61 67 65 20 69 6e o find a page in
29470 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 the in-memory c
29480 61 63 68 65 20 66 69 72 73 74 2e 20 20 49 66 20 ache first. If
29490 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 the page is not
294a0 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 6d 65 already.** in me
294b0 6d 6f 72 79 2c 20 74 68 69 73 20 72 6f 75 74 69 mory, this routi
294c0 6e 65 20 67 6f 65 73 20 74 6f 20 64 69 73 6b 20 ne goes to disk
294d0 74 6f 20 72 65 61 64 20 69 74 20 69 6e 20 77 68 to read it in wh
294e0 65 72 65 61 73 20 4c 6f 6f 6b 75 70 28 29 0a 2a ereas Lookup().*
294f0 2a 20 6a 75 73 74 20 72 65 74 75 72 6e 73 20 30 * just returns 0
29500 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
29510 61 63 71 75 69 72 65 73 20 61 20 72 65 61 64 2d acquires a read-
29520 6c 6f 63 6b 20 74 68 65 20 66 69 72 73 74 20 74 lock the first t
29530 69 6d 65 20 69 74 0a 2a 2a 20 68 61 73 20 74 6f ime it.** has to
29540 20 67 6f 20 74 6f 20 64 69 73 6b 2c 20 61 6e 64 go to disk, and
29550 20 63 6f 75 6c 64 20 61 6c 73 6f 20 70 6c 61 79 could also play
29560 62 61 63 6b 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 back an old jour
29570 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61 72 79 nal if necessary
29580 2e 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f 6f 6b 75 ..** Since Looku
29590 70 28 29 20 6e 65 76 65 72 20 67 6f 65 73 20 74 p() never goes t
295a0 6f 20 64 69 73 6b 2c 20 69 74 20 6e 65 76 65 72 o disk, it never
295b0 20 68 61 73 20 74 6f 20 64 65 61 6c 20 77 69 74 has to deal wit
295c0 68 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72 20 6a 6f h locks.** or jo
295d0 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a urnal files..*/.
295e0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
295f0 41 63 71 75 69 72 65 28 0a 20 20 50 61 67 65 72 Acquire(. Pager
29600 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 2f *pPager, /
29610 2a 20 54 68 65 20 70 61 67 65 72 20 6f 70 65 6e * The pager open
29620 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
29630 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 file */. Pgno
29640 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 2f pgno, /
29650 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f * Page number to
29660 20 66 65 74 63 68 20 2a 2f 0a 20 20 44 62 50 61 fetch */. DbPa
29670 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 ge **ppPage,
29680 2f 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 /* Write a point
29690 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 er to the page h
296a0 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 ere */. int noC
296b0 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 2f 2a 20 ontent /*
296c0 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 72 65 Do not bother re
296d0 61 64 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66 72 ading content fr
296e0 6f 6d 20 64 69 73 6b 20 69 66 20 74 72 75 65 20 om disk if true
296f0 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a */.){. int rc;.
29700 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a 20 PgHdr *pPg;..
29710 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74 5f assert( assert_
29720 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 pager_state(pPag
29730 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 er) );. assert(
29740 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 50 pPager->state>P
29750 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 0a AGER_UNLOCK );..
29760 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29 7b if( pgno==0 ){
29770 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
29780 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
29790 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 . }.. /* If th
297a0 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 e pager is in th
297b0 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 72 e error state, r
297c0 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 eturn an error i
297d0 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0a 20 20 2a mmediately. . *
297e0 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 71 * Otherwise, req
297f0 75 65 73 74 20 74 68 65 20 70 61 67 65 20 66 72 uest the page fr
29800 6f 6d 20 74 68 65 20 50 43 61 63 68 65 20 6c 61 om the PCache la
29810 79 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 yer. */. if( pP
29820 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 ager->errCode!=S
29830 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 QLITE_OK && pPag
29840 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c er->errCode!=SQL
29850 49 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 ITE_FULL ){.
29860 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 rc = pPager->err
29870 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Code;. }else{.
29880 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
29890 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67 65 cacheFetch(pPage
298a0 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e 6f r->pPCache, pgno
298b0 2c 20 31 2c 20 70 70 50 61 67 65 29 3b 0a 20 20 , 1, ppPage);.
298c0 7d 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c }.. if( rc!=SQL
298d0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a ITE_OK ){. /*
298e0 20 45 69 74 68 65 72 20 74 68 65 20 63 61 6c 6c Either the call
298f0 20 74 6f 20 73 71 6c 69 74 65 33 50 63 61 63 68 to sqlite3Pcach
29900 65 46 65 74 63 68 28 29 20 72 65 74 75 72 6e 65 eFetch() returne
29910 64 20 61 6e 20 65 72 72 6f 72 20 6f 72 20 74 68 d an error or th
29920 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 20 77 e. ** pager w
29930 61 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 as already in th
29940 65 20 65 72 72 6f 72 2d 73 74 61 74 65 20 77 68 e error-state wh
29950 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
29960 20 77 61 73 20 63 61 6c 6c 65 64 2e 0a 20 20 20 was called..
29970 20 2a 2a 20 53 65 74 20 70 50 67 20 74 6f 20 30 ** Set pPg to 0
29980 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20 74 68 65 and jump to the
29990 20 65 78 63 65 70 74 69 6f 6e 20 68 61 6e 64 6c exception handl
299a0 65 72 2e 20 20 2a 2f 0a 20 20 20 20 70 50 67 20 er. */. pPg
299b0 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 70 61 = 0;. goto pa
299c0 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b ger_acquire_err;
299d0 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 . }. assert( (
299e0 2a 70 70 50 61 67 65 29 2d 3e 70 67 6e 6f 3d 3d *ppPage)->pgno==
299f0 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 pgno );. assert
29a00 28 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 ( (*ppPage)->pPa
29a10 67 65 72 3d 3d 70 50 61 67 65 72 20 7c 7c 20 28 ger==pPager || (
29a20 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65 72 *ppPage)->pPager
29a30 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 28 2a ==0 );.. if( (*
29a40 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65 72 20 ppPage)->pPager
29a50 26 26 20 21 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b && !noContent ){
29a60 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 . /* In this
29a70 63 61 73 65 20 74 68 65 20 70 63 61 63 68 65 20 case the pcache
29a80 61 6c 72 65 61 64 79 20 63 6f 6e 74 61 69 6e 73 already contains
29a90 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 an initialized
29aa0 63 6f 70 79 20 6f 66 0a 20 20 20 20 2a 2a 20 74 copy of. ** t
29ab0 68 65 20 70 61 67 65 2e 20 52 65 74 75 72 6e 20 he page. Return
29ac0 77 69 74 68 6f 75 74 20 66 75 72 74 68 65 72 20 without further
29ad0 61 64 6f 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 ado. */. ass
29ae0 65 72 74 28 20 70 67 6e 6f 3c 3d 50 41 47 45 52 ert( pgno<=PAGER
29af0 5f 4d 41 58 5f 50 47 4e 4f 20 26 26 20 70 67 6e _MAX_PGNO && pgn
29b00 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f o!=PAGER_MJ_PGNO
29b10 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 (pPager) );.
29b20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 PAGER_INCR(pPage
29b30 72 2d 3e 6e 48 69 74 29 3b 0a 20 20 20 20 72 65 r->nHit);. re
29b40 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
29b50 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
29b60 20 54 68 65 20 70 61 67 65 72 20 63 61 63 68 65 The pager cache
29b70 20 68 61 73 20 63 72 65 61 74 65 64 20 61 20 6e has created a n
29b80 65 77 20 70 61 67 65 2e 20 49 74 73 20 63 6f 6e ew page. Its con
29b90 74 65 6e 74 20 6e 65 65 64 73 20 74 6f 20 0a 20 tent needs to .
29ba0 20 20 20 2a 2a 20 62 65 20 69 6e 69 74 69 61 6c ** be initial
29bb0 69 7a 65 64 2e 20 20 2a 2f 0a 20 20 20 20 69 6e ized. */. in
29bc0 74 20 6e 4d 61 78 3b 0a 0a 20 20 20 20 50 41 47 t nMax;.. PAG
29bd0 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e ER_INCR(pPager->
29be0 6e 4d 69 73 73 29 3b 0a 20 20 20 20 70 50 67 20 nMiss);. pPg
29bf0 3d 20 2a 70 70 50 61 67 65 3b 0a 20 20 20 20 70 = *ppPage;. p
29c00 50 67 2d 3e 70 50 61 67 65 72 20 3d 20 70 50 61 Pg->pPager = pPa
29c10 67 65 72 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 ger;.. /* The
29c20 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 6e 75 maximum page nu
29c30 6d 62 65 72 20 69 73 20 32 5e 33 31 2e 20 52 65 mber is 2^31. Re
29c40 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
29c50 55 50 54 20 69 66 20 61 20 70 61 67 65 0a 20 20 UPT if a page.
29c60 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67 72 65 61 ** number grea
29c70 74 65 72 20 74 68 61 6e 20 74 68 69 73 2c 20 6f ter than this, o
29c80 72 20 74 68 65 20 75 6e 75 73 65 64 20 6c 6f 63 r the unused loc
29c90 6b 69 6e 67 2d 70 61 67 65 2c 20 69 73 20 72 65 king-page, is re
29ca0 71 75 65 73 74 65 64 2e 20 2a 2f 0a 20 20 20 20 quested. */.
29cb0 69 66 28 20 70 67 6e 6f 3e 50 41 47 45 52 5f 4d if( pgno>PAGER_M
29cc0 41 58 5f 50 47 4e 4f 20 7c 7c 20 70 67 6e 6f 3d AX_PGNO || pgno=
29cd0 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 =PAGER_MJ_PGNO(p
29ce0 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 Pager) ){.
29cf0 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 rc = SQLITE_CORR
29d00 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
29d10 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69 goto pager_acqui
29d20 72 65 5f 65 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 re_err;. }..
29d30 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
29d40 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 agerPagecount(pP
29d50 61 67 65 72 2c 20 26 6e 4d 61 78 29 3b 0a 20 20 ager, &nMax);.
29d60 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
29d70 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 _OK ){. got
29d80 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f o pager_acquire_
29d90 65 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 err;. }..
29da0 69 66 28 20 4d 45 4d 44 42 20 7c 7c 20 6e 4d 61 if( MEMDB || nMa
29db0 78 3c 28 69 6e 74 29 70 67 6e 6f 20 7c 7c 20 6e x<(int)pgno || n
29dc0 6f 43 6f 6e 74 65 6e 74 20 7c 7c 20 21 69 73 4f oContent || !isO
29dd0 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
29de0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e ){. if( pgn
29df0 6f 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f o>pPager->mxPgno
29e00 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
29e10 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 SQLITE_FULL;.
29e20 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 72 goto pager
29e30 5f 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 _acquire_err;.
29e40 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
29e50 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 noContent ){.
29e60 20 20 20 20 20 2f 2a 20 46 61 69 6c 75 72 65 20 /* Failure
29e70 74 6f 20 73 65 74 20 74 68 65 20 62 69 74 73 20 to set the bits
29e80 69 6e 20 74 68 65 20 49 6e 4a 6f 75 72 6e 61 6c in the InJournal
29e90 20 62 69 74 2d 76 65 63 74 6f 72 73 20 69 73 20 bit-vectors is
29ea0 62 65 6e 69 67 6e 2e 0a 20 20 20 20 20 20 20 20 benign..
29eb0 2a 2a 20 49 74 20 6d 65 72 65 6c 79 20 6d 65 61 ** It merely mea
29ec0 6e 73 20 74 68 61 74 20 77 65 20 6d 69 67 68 74 ns that we might
29ed0 20 64 6f 20 73 6f 6d 65 20 65 78 74 72 61 20 77 do some extra w
29ee0 6f 72 6b 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 61 ork to journal a
29ef0 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 . ** pag
29f00 65 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 e that does not
29f10 6e 65 65 64 20 74 6f 20 62 65 20 6a 6f 75 72 6e need to be journ
29f20 61 6c 65 64 2e 20 20 4e 65 76 65 72 74 68 65 6c aled. Neverthel
29f30 65 73 73 2c 20 62 65 20 73 75 72 65 20 0a 20 20 ess, be sure .
29f40 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 65 73 74 ** to test
29f50 20 74 68 65 20 63 61 73 65 20 77 68 65 72 65 20 the case where
29f60 61 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 6f a malloc error o
29f70 63 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 ccurs while tryi
29f80 6e 67 20 74 6f 20 73 65 74 20 0a 20 20 20 20 20 ng to set .
29f90 20 20 20 2a 2a 20 61 20 62 69 74 20 69 6e 20 61 ** a bit in a
29fa0 20 62 69 74 20 76 65 63 74 6f 72 2e 0a 20 20 20 bit vector..
29fb0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
29fc0 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 sqlite3BeginBeni
29fd0 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 gnMalloc();.
29fe0 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 if( pgno<=pP
29ff0 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 ager->dbOrigSize
2a000 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 54 45 ){. TE
2a010 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 73 STONLY( rc = ) s
2a020 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
2a030 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
2a040 61 6c 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 al, pgno);.
2a050 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
2a060 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
2a070 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
2a080 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 TESTONLY( r
2a090 63 20 3d 20 29 20 61 64 64 54 6f 53 61 76 65 70 c = ) addToSavep
2a0a0 6f 69 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 ointBitvecs(pPag
2a0b0 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 er, pgno);.
2a0c0 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d testcase( rc=
2a0d0 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b =SQLITE_NOMEM );
2a0e0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
2a0f0 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 EndBenignMalloc(
2a100 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
2a110 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e 70 44 61 memset(pPg->pDa
2a120 74 61 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 ta, 0, pPager->p
2a130 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 ageSize);.
2a140 49 4f 54 52 41 43 45 28 28 22 5a 45 52 4f 20 25 IOTRACE(("ZERO %
2a150 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c p %d\n", pPager,
2a160 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 65 6c pgno));. }el
2a170 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 se{. assert
2a180 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70 ( pPg->pPager==p
2a190 50 61 67 65 72 20 29 3b 0a 20 20 20 20 20 20 72 Pager );. r
2a1a0 63 20 3d 20 72 65 61 64 44 62 50 61 67 65 28 70 c = readDbPage(p
2a1b0 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 Pg);. if( r
2a1c0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
2a1d0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 goto pag
2a1e0 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b 0a er_acquire_err;.
2a1f0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 }. }.#i
2a200 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 fdef SQLITE_CHEC
2a210 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67 2d K_PAGES. pPg-
2a220 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 >pageHash = page
2a230 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 3b r_pagehash(pPg);
2a240 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 72 .#endif. }.. r
2a250 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
2a260 0a 0a 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f ..pager_acquire_
2a270 65 72 72 3a 0a 20 20 61 73 73 65 72 74 28 20 72 err:. assert( r
2a280 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a c!=SQLITE_OK );.
2a290 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 if( pPg ){.
2a2a0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 sqlite3PcacheDr
2a2b0 6f 70 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 70 op(pPg);. }. p
2a2c0 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 agerUnlockIfUnus
2a2d0 65 64 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 2a ed(pPager);.. *
2a2e0 70 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 72 65 ppPage = 0;. re
2a2f0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
2a300 2a 20 41 63 71 75 69 72 65 20 61 20 70 61 67 65 * Acquire a page
2a310 20 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 if it is alread
2a320 79 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f y in the in-memo
2a330 72 79 20 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a ry cache. Do.**
2a340 20 6e 6f 74 20 72 65 61 64 20 74 68 65 20 70 61 not read the pa
2a350 67 65 20 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 ge from disk. R
2a360 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
2a370 74 6f 20 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 to the page,.**
2a380 6f 72 20 30 20 69 66 20 74 68 65 20 70 61 67 65 or 0 if the page
2a390 20 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 is not in cache
2a3a0 2e 20 41 6c 73 6f 2c 20 72 65 74 75 72 6e 20 30 . Also, return 0
2a3b0 20 69 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 if the .** page
2a3c0 72 20 69 73 20 69 6e 20 50 41 47 45 52 5f 55 4e r is in PAGER_UN
2a3d0 4c 4f 43 4b 20 73 74 61 74 65 20 77 68 65 6e 20 LOCK state when
2a3e0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
2a3f0 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 6f 72 20 69 called,.** or i
2a400 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 f the pager is i
2a410 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 n an error state
2a420 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 other than SQLI
2a430 54 45 5f 46 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 TE_FULL..**.** S
2a440 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 ee also sqlite3P
2a450 61 67 65 72 47 65 74 28 29 2e 20 20 54 68 65 20 agerGet(). The
2a460 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 difference betwe
2a470 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a en this routine.
2a480 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 50 61 ** and sqlite3Pa
2a490 67 65 72 47 65 74 28 29 20 69 73 20 74 68 61 74 gerGet() is that
2a4a0 20 5f 67 65 74 28 29 20 77 69 6c 6c 20 67 6f 20 _get() will go
2a4b0 74 6f 20 74 68 65 20 64 69 73 6b 20 61 6e 64 20 to the disk and
2a4c0 72 65 61 64 0a 2a 2a 20 69 6e 20 74 68 65 20 70 read.** in the p
2a4d0 61 67 65 20 69 66 20 74 68 65 20 70 61 67 65 20 age if the page
2a4e0 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 is not already i
2a4f0 6e 20 63 61 63 68 65 2e 20 20 54 68 69 73 20 72 n cache. This r
2a500 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e outine.** return
2a510 73 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 70 61 s NULL if the pa
2a520 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 ge is not in cac
2a530 68 65 20 6f 72 20 69 66 20 61 20 64 69 73 6b 20 he or if a disk
2a540 49 2f 4f 20 65 72 72 6f 72 20 0a 2a 2a 20 68 61 I/O error .** ha
2a550 73 20 65 76 65 72 20 68 61 70 70 65 6e 65 64 2e s ever happened.
2a560 0a 2a 2f 0a 44 62 50 61 67 65 20 2a 73 71 6c 69 .*/.DbPage *sqli
2a570 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50 te3PagerLookup(P
2a580 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 ager *pPager, Pg
2a590 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 no pgno){. PgHd
2a5a0 72 20 2a 70 50 67 20 3d 20 30 3b 0a 20 20 61 73 r *pPg = 0;. as
2a5b0 73 65 72 74 28 20 70 50 61 67 65 72 21 3d 30 20 sert( pPager!=0
2a5c0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 67 6e );. assert( pgn
2a5d0 6f 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 o!=0 );. assert
2a5e0 28 20 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 ( pPager->pPCach
2a5f0 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 e!=0 );. assert
2a600 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 ( pPager->state
2a610 3e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 > PAGER_UNLOCK )
2a620 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 ;. sqlite3Pcach
2a630 65 46 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 eFetch(pPager->p
2a640 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 30 2c PCache, pgno, 0,
2a650 20 26 70 50 67 29 3b 0a 20 20 72 65 74 75 72 6e &pPg);. return
2a660 20 70 50 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 pPg;.}../*.** R
2a670 65 6c 65 61 73 65 20 61 20 70 61 67 65 20 72 65 elease a page re
2a680 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 ference..**.** I
2a690 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 f the number of
2a6a0 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 references to th
2a6b0 65 20 70 61 67 65 20 64 72 6f 70 20 74 6f 20 7a e page drop to z
2a6c0 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a ero, then the.**
2a6d0 20 70 61 67 65 20 69 73 20 61 64 64 65 64 20 74 page is added t
2a6e0 6f 20 74 68 65 20 4c 52 55 20 6c 69 73 74 2e 20 o the LRU list.
2a6f0 20 57 68 65 6e 20 61 6c 6c 20 72 65 66 65 72 65 When all refere
2a700 6e 63 65 73 20 74 6f 20 61 6c 6c 20 70 61 67 65 nces to all page
2a710 73 0a 2a 2a 20 61 72 65 20 72 65 6c 65 61 73 65 s.** are release
2a720 64 2c 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 d, a rollback oc
2a730 63 75 72 73 20 61 6e 64 20 74 68 65 20 6c 6f 63 curs and the loc
2a740 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
2a750 65 20 69 73 0a 2a 2a 20 72 65 6d 6f 76 65 64 2e e is.** removed.
2a760 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 .*/.void sqlite3
2a770 50 61 67 65 72 55 6e 72 65 66 28 44 62 50 61 67 PagerUnref(DbPag
2a780 65 20 2a 70 50 67 29 7b 0a 20 20 69 66 28 20 70 e *pPg){. if( p
2a790 50 67 20 29 7b 0a 20 20 20 20 50 61 67 65 72 20 Pg ){. Pager
2a7a0 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
2a7b0 50 61 67 65 72 3b 0a 20 20 20 20 73 71 6c 69 74 Pager;. sqlit
2a7c0 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 e3PcacheRelease(
2a7d0 70 50 67 29 3b 0a 20 20 20 20 70 61 67 65 72 55 pPg);. pagerU
2a7e0 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 70 50 nlockIfUnused(pP
2a7f0 61 67 65 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ager);. }.}../*
2a800 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
2a810 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 74 20 74 n is called at t
2a820 68 65 20 73 74 61 72 74 20 6f 66 20 65 76 65 72 he start of ever
2a830 79 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 y write transact
2a840 69 6f 6e 2e 0a 2a 2a 20 54 68 65 72 65 20 6d 75 ion..** There mu
2a850 73 74 20 61 6c 72 65 61 64 79 20 62 65 20 61 20 st already be a
2a860 52 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c RESERVED or EXCL
2a870 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 USIVE lock on th
2a880 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 66 e database .** f
2a890 69 6c 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f ile when this ro
2a8a0 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e utine is called.
2a8b0 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 .**.** Open the
2a8c0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 journal file for
2a8d0 20 70 61 67 65 72 20 70 50 61 67 65 72 20 61 6e pager pPager an
2a8e0 64 20 77 72 69 74 65 20 61 20 6a 6f 75 72 6e 61 d write a journa
2a8f0 6c 20 68 65 61 64 65 72 0a 2a 2a 20 74 6f 20 74 l header.** to t
2a900 68 65 20 73 74 61 72 74 20 6f 66 20 69 74 2e 20 he start of it.
2a910 49 66 20 74 68 65 72 65 20 61 72 65 20 61 63 74 If there are act
2a920 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 2c 20 ive savepoints,
2a930 6f 70 65 6e 20 74 68 65 20 73 75 62 2d 6a 6f 75 open the sub-jou
2a940 72 6e 61 6c 0a 2a 2a 20 61 73 20 77 65 6c 6c 2e rnal.** as well.
2a950 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
2a960 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e s only used when
2a970 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
2a980 65 20 69 73 20 62 65 69 6e 67 20 0a 2a 2a 20 6f e is being .** o
2a990 70 65 6e 65 64 20 74 6f 20 77 72 69 74 65 20 61 pened to write a
2a9a0 20 72 6f 6c 6c 62 61 63 6b 20 6c 6f 67 20 66 6f rollback log fo
2a9b0 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e r a transaction.
2a9c0 20 49 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 It is not used
2a9d0 0a 2a 2a 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 .** when opening
2a9e0 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 66 a hot journal f
2a9f0 69 6c 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 ile to roll it b
2aa00 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ack..**.** If th
2aa10 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
2aa20 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 28 s already open (
2aa30 61 73 20 69 74 20 6d 61 79 20 62 65 20 69 6e 20 as it may be in
2aa40 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 29 2c exclusive mode),
2aa50 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 66 75 .** then this fu
2aa60 6e 63 74 69 6f 6e 20 6a 75 73 74 20 77 72 69 74 nction just writ
2aa70 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 68 65 61 es a journal hea
2aa80 64 65 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 der to the start
2aa90 20 6f 66 20 74 68 65 0a 2a 2a 20 61 6c 72 65 61 of the.** alrea
2aaa0 64 79 20 6f 70 65 6e 20 66 69 6c 65 2e 20 0a 2a dy open file. .*
2aab0 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f 72 20 *.** Whether or
2aac0 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 not the journal
2aad0 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 62 file is opened b
2aae0 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c y this function,
2aaf0 20 74 68 65 0a 2a 2a 20 50 61 67 65 72 2e 70 49 the.** Pager.pI
2ab00 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 76 65 63 20 nJournal bitvec
2ab10 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c structure is all
2ab20 6f 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 ocated..**.** Re
2ab30 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 turn SQLITE_OK i
2ab40 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 f everything is
2ab50 73 75 63 63 65 73 73 66 75 6c 2e 20 4f 74 68 65 successful. Othe
2ab60 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20 0a 2a rwise, return .*
2ab70 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 * SQLITE_NOMEM i
2ab80 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f f the attempt to
2ab90 20 61 6c 6c 6f 63 61 74 65 20 50 61 67 65 72 2e allocate Pager.
2aba0 70 49 6e 4a 6f 75 72 6e 61 6c 20 66 61 69 6c 73 pInJournal fails
2abb0 2c 20 6f 72 20 0a 2a 2a 20 61 6e 20 49 4f 20 65 , or .** an IO e
2abc0 72 72 6f 72 20 63 6f 64 65 20 69 66 20 6f 70 65 rror code if ope
2abd0 6e 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 ning or writing
2abe0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
2abf0 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 fails..*/.stati
2ac00 63 20 69 6e 74 20 70 61 67 65 72 5f 6f 70 65 6e c int pager_open
2ac10 5f 6a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a _journal(Pager *
2ac20 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 pPager){. int r
2ac30 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
2ac40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ac50 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
2ac60 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 code */. int nP
2ac70 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
2ac80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ac90 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
2aca0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
2acb0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
2acc0 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 const pVfs = pP
2acd0 61 67 65 72 2d 3e 70 56 66 73 3b 20 20 20 2f 2a ager->pVfs; /*
2ace0 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 Local cache of
2acf0 76 66 73 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 0a vfs pointer */..
2ad00 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
2ad10 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 ->state>=PAGER_R
2ad20 45 53 45 52 56 45 44 20 29 3b 0a 20 20 61 73 73 ESERVED );. ass
2ad30 65 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73 65 ert( pPager->use
2ad40 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 Journal );. ass
2ad50 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 ert( pPager->jou
2ad60 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f rnalMode!=PAGER_
2ad70 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 JOURNALMODE_OFF
2ad80 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
2ad90 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d ger->pInJournal=
2ada0 3d 30 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 =0 );. . /* If
2adb0 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
2adc0 65 72 72 6f 72 20 73 74 61 74 65 2c 20 74 68 69 error state, thi
2add0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 s function is a
2ade0 6e 6f 2d 6f 70 2e 20 20 42 75 74 20 6f 6e 0a 20 no-op. But on.
2adf0 20 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 68 61 ** the other ha
2ae00 6e 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 nd, this routine
2ae10 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 is never called
2ae20 20 69 66 20 77 65 20 61 72 65 20 61 6c 72 65 61 if we are alrea
2ae30 64 79 20 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 72 dy in. ** an er
2ae40 72 6f 72 20 73 74 61 74 65 2e 20 2a 2f 0a 20 20 ror state. */.
2ae50 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 if( NEVER(pPager
2ae60 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 72 65 74 ->errCode) ) ret
2ae70 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 urn pPager->errC
2ae80 6f 64 65 3b 0a 0a 20 20 74 65 73 74 63 61 73 65 ode;.. testcase
2ae90 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 ( pPager->dbSize
2aea0 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 72 63 Valid==0 );. rc
2aeb0 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 = sqlite3PagerP
2aec0 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c agecount(pPager,
2aed0 20 26 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20 &nPage);. if(
2aee0 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
2aef0 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 pPager->pInJou
2af00 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 rnal = sqlite3Bi
2af10 74 76 65 63 43 72 65 61 74 65 28 6e 50 61 67 65 tvecCreate(nPage
2af20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d );. if( pPager-
2af30 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 >pInJournal==0 )
2af40 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
2af50 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a ITE_NOMEM;. }..
2af60 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f /* Open the jo
2af70 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 69 74 urnal file if it
2af80 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 is not already
2af90 6f 70 65 6e 2e 20 2a 2f 0a 20 20 69 66 28 20 21 open. */. if( !
2afa0 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
2afb0 66 64 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 fd) ){. if( p
2afc0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
2afd0 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
2afe0 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a LMODE_MEMORY ){.
2aff0 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d sqlite3Mem
2b000 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 JournalOpen(pPag
2b010 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 7d 65 er->jfd);. }e
2b020 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 lse{. const
2b030 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 20 20 20 int flags =
2b040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2b050 2a 20 56 46 53 20 66 6c 61 67 73 20 74 6f 20 6f * VFS flags to o
2b060 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 pen journal file
2b070 20 2a 2f 0a 20 20 20 20 20 20 20 20 53 51 4c 49 */. SQLI
2b080 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
2b090 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 E|SQLITE_OPEN_CR
2b0a0 45 41 54 45 7c 0a 20 20 20 20 20 20 20 20 28 70 EATE|. (p
2b0b0 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
2b0c0 3f 20 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 ? . (SQ
2b0d0 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 LITE_OPEN_DELETE
2b0e0 4f 4e 43 4c 4f 53 45 7c 53 51 4c 49 54 45 5f 4f ONCLOSE|SQLITE_O
2b0f0 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c PEN_TEMP_JOURNAL
2b100 29 3a 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 ):. (SQ
2b110 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a LITE_OPEN_MAIN_J
2b120 4f 55 52 4e 41 4c 29 0a 20 20 20 20 20 20 20 20 OURNAL).
2b130 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 );.#ifdef SQLITE
2b140 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 _ENABLE_ATOMIC_W
2b150 52 49 54 45 0a 20 20 20 20 20 20 72 63 20 3d 20 RITE. rc =
2b160 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 sqlite3JournalOp
2b170 65 6e 28 0a 20 20 20 20 20 20 20 20 20 20 70 56 en(. pV
2b180 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 fs, pPager->zJou
2b190 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 rnal, pPager->jf
2b1a0 64 2c 20 66 6c 61 67 73 2c 20 6a 72 6e 6c 42 75 d, flags, jrnlBu
2b1b0 66 66 65 72 53 69 7a 65 28 70 50 61 67 65 72 29 fferSize(pPager)
2b1c0 0a 20 20 20 20 20 20 29 3b 0a 23 65 6c 73 65 0a . );.#else.
2b1d0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
2b1e0 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 e3OsOpen(pVfs, p
2b1f0 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c Pager->zJournal,
2b200 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c pPager->jfd, fl
2b210 61 67 73 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a ags, 0);.#endif.
2b220 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
2b230 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
2b240 7c 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 || isOpen(pPager
2b250 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 7d 0a 0a 0a ->jfd) );. }...
2b260 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 66 /* Write the f
2b270 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 irst journal hea
2b280 64 65 72 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e der to the journ
2b290 61 6c 20 66 69 6c 65 20 61 6e 64 20 6f 70 65 6e al file and open
2b2a0 20 0a 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a . ** the sub-j
2b2b0 6f 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 ournal if necess
2b2c0 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ary.. */. if(
2b2d0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
2b2e0 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 43 68 . /* TODO: Ch
2b2f0 65 63 6b 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 eck if all of th
2b300 65 73 65 20 61 72 65 20 72 65 61 6c 6c 79 20 72 ese are really r
2b310 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 20 20 equired. */.
2b320 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 pPager->dbOrigSi
2b330 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 ze = pPager->dbS
2b340 69 7a 65 3b 0a 20 20 20 20 70 50 61 67 65 72 2d ize;. pPager-
2b350 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 >journalStarted
2b360 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d = 0;. pPager-
2b370 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 >needSync = 0;.
2b380 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 pPager->nRec
2b390 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d = 0;. pPager-
2b3a0 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b >journalOff = 0;
2b3b0 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 74 . pPager->set
2b3c0 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 Master = 0;.
2b3d0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 pPager->journalH
2b3e0 64 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d dr = 0;. rc =
2b3f0 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 writeJournalHdr
2b400 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 (pPager);. }..
2b410 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
2b420 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 OK ){. sqlite
2b430 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 3BitvecDestroy(p
2b440 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
2b450 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e l);. pPager->
2b460 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a pInJournal = 0;.
2b470 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
2b480 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 .}../*.** Begin
2b490 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 a write-transact
2b4a0 69 6f 6e 20 6f 6e 20 74 68 65 20 73 70 65 63 69 ion on the speci
2b4b0 66 69 65 64 20 70 61 67 65 72 20 6f 62 6a 65 63 fied pager objec
2b4c0 74 2e 20 49 66 20 61 20 0a 2a 2a 20 77 72 69 74 t. If a .** writ
2b4d0 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 e-transaction ha
2b4e0 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6f s already been o
2b4f0 70 65 6e 65 64 2c 20 74 68 69 73 20 66 75 6e 63 pened, this func
2b500 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tion is a no-op.
2b510 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 .**.** If the ex
2b520 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 69 73 Flag argument is
2b530 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 61 63 71 false, then acq
2b540 75 69 72 65 20 61 74 20 6c 65 61 73 74 20 61 20 uire at least a
2b550 52 45 53 45 52 56 45 44 0a 2a 2a 20 6c 6f 63 6b RESERVED.** lock
2b560 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
2b570 20 66 69 6c 65 2e 20 49 66 20 65 78 46 6c 61 67 file. If exFlag
2b580 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 61 is true, then a
2b590 63 71 75 69 72 65 20 61 74 20 6c 65 61 73 74 0a cquire at least.
2b5a0 2a 2a 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 ** an EXCLUSIVE
2b5b0 6c 6f 63 6b 2e 20 49 66 20 73 75 63 68 20 61 20 lock. If such a
2b5c0 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 lock is already
2b5d0 68 65 6c 64 2c 20 6e 6f 20 6c 6f 63 6b 69 6e 67 held, no locking
2b5e0 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 6e .** functions n
2b5f0 65 65 64 20 62 65 20 63 61 6c 6c 65 64 2e 0a 2a eed be called..*
2b600 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 *.** If this is
2b610 6e 6f 74 20 61 20 74 65 6d 70 6f 72 61 72 79 20 not a temporary
2b620 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c or in-memory fil
2b630 65 20 61 6e 64 2c 20 74 68 65 20 6a 6f 75 72 6e e and, the journ
2b640 61 6c 20 66 69 6c 65 20 69 73 20 0a 2a 2a 20 6f al file is .** o
2b650 70 65 6e 65 64 20 69 66 20 69 74 20 68 61 73 20 pened if it has
2b660 6e 6f 74 20 62 65 65 6e 20 61 6c 72 65 61 64 79 not been already
2b670 2e 20 46 6f 72 20 61 20 74 65 6d 70 6f 72 61 72 . For a temporar
2b680 79 20 66 69 6c 65 2c 20 74 68 65 20 6f 70 65 6e y file, the open
2b690 69 6e 67 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6a ing .** of the j
2b6a0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 64 ournal file is d
2b6b0 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 74 68 eferred until th
2b6c0 65 72 65 20 69 73 20 61 6e 20 61 63 74 75 61 6c ere is an actual
2b6d0 20 6e 65 65 64 20 74 6f 20 0a 2a 2a 20 77 72 69 need to .** wri
2b6e0 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 te to the journa
2b6f0 6c 2e 20 54 4f 44 4f 3a 20 57 68 79 20 68 61 6e l. TODO: Why han
2b700 64 6c 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69 dle temporary fi
2b710 6c 65 73 20 64 69 66 66 65 72 65 6e 74 6c 79 3f les differently?
2b720 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f .**.** If the jo
2b730 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 urnal file is op
2b740 65 6e 65 64 20 28 6f 72 20 69 66 20 69 74 20 69 ened (or if it i
2b750 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 29 2c s already open),
2b760 20 74 68 65 6e 20 61 0a 2a 2a 20 6a 6f 75 72 6e then a.** journ
2b770 61 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72 69 al-header is wri
2b780 74 74 65 6e 20 74 6f 20 74 68 65 20 73 74 61 72 tten to the star
2b790 74 20 6f 66 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 t of it..**.** I
2b7a0 66 20 74 68 65 20 73 75 62 6a 49 6e 4d 65 6d 6f f the subjInMemo
2b7b0 72 79 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e ry argument is n
2b7c0 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 6e on-zero, then an
2b7d0 79 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6f 70 y sub-journal op
2b7e0 65 6e 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 74 ened.** within t
2b7f0 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 his transaction
2b800 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 61 will be opened a
2b810 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 s an in-memory f
2b820 69 6c 65 2e 20 54 68 69 73 0a 2a 2a 20 68 61 73 ile. This.** has
2b830 20 6e 6f 20 65 66 66 65 63 74 20 69 66 20 74 68 no effect if th
2b840 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 e sub-journal is
2b850 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 20 already opened
2b860 28 61 73 20 69 74 20 6d 61 79 20 62 65 20 77 68 (as it may be wh
2b870 65 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 69 6e en.** running in
2b880 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 29 exclusive mode)
2b890 20 6f 72 20 69 66 20 74 68 65 20 74 72 61 6e 73 or if the trans
2b8a0 61 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 action does not
2b8b0 72 65 71 75 69 72 65 20 61 0a 2a 2a 20 73 75 62 require a.** sub
2b8c0 2d 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 65 -journal. If the
2b8d0 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 72 subjInMemory ar
2b8e0 67 75 6d 65 6e 74 20 69 73 20 7a 65 72 6f 2c 20 gument is zero,
2b8f0 74 68 65 6e 20 61 6e 79 20 72 65 71 75 69 72 65 then any require
2b900 64 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c d.** sub-journal
2b910 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 is implemented
2b920 69 6e 2d 6d 65 6d 6f 72 79 20 69 66 20 70 50 61 in-memory if pPa
2b930 67 65 72 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d ger is an in-mem
2b940 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 0a 2a ory database, .*
2b950 2a 20 6f 72 20 75 73 69 6e 67 20 61 20 74 65 6d * or using a tem
2b960 70 6f 72 61 72 79 20 66 69 6c 65 20 6f 74 68 65 porary file othe
2b970 72 77 69 73 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 rwise..*/.int sq
2b980 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 lite3PagerBegin(
2b990 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
2b9a0 6e 74 20 65 78 46 6c 61 67 2c 20 69 6e 74 20 73 nt exFlag, int s
2b9b0 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 7b 0a 20 20 ubjInMemory){.
2b9c0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
2b9d0 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 OK;. assert( pP
2b9e0 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 ager->state!=PAG
2b9f0 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 70 ER_UNLOCK );. p
2ba00 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d Pager->subjInMem
2ba10 6f 72 79 20 3d 20 28 75 38 29 73 75 62 6a 49 6e ory = (u8)subjIn
2ba20 4d 65 6d 6f 72 79 3b 0a 0a 20 20 69 66 28 20 70 Memory;.. if( p
2ba30 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 Pager->state==PA
2ba40 47 45 52 5f 53 48 41 52 45 44 20 29 7b 0a 20 20 GER_SHARED ){.
2ba50 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
2ba60 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 ->pInJournal==0
2ba70 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 );. assert( !
2ba80 4d 45 4d 44 42 20 26 26 20 21 70 50 61 67 65 72 MEMDB && !pPager
2ba90 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 0a 20 ->tempFile );..
2baa0 20 20 20 69 66 28 20 70 61 67 65 72 55 73 65 57 if( pagerUseW
2bab0 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 al(pPager) ){.
2bac0 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 /* If the pa
2bad0 67 65 72 20 69 73 20 63 6f 6e 66 69 67 75 72 65 ger is configure
2bae0 64 20 74 6f 20 75 73 65 20 6c 6f 63 6b 69 6e 67 d to use locking
2baf0 5f 6d 6f 64 65 3d 65 78 63 6c 75 73 69 76 65 2c _mode=exclusive,
2bb00 20 61 6e 64 20 61 6e 0a 20 20 20 20 20 20 2a 2a and an. **
2bb10 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 exclusive lock
2bb20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
2bb30 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 is not already h
2bb40 65 6c 64 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e eld, obtain it n
2bb50 6f 77 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ow.. */.
2bb60 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 if( pPager->e
2bb70 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 26 26 20 xclusiveMode &&
2bb80 73 71 6c 69 74 65 33 57 61 6c 45 78 63 6c 75 73 sqlite3WalExclus
2bb90 69 76 65 4d 6f 64 65 28 70 50 61 67 65 72 2d 3e iveMode(pPager->
2bba0 70 57 61 6c 2c 20 2d 31 29 20 29 7b 0a 20 20 20 pWal, -1) ){.
2bbb0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
2bbc0 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 3OsLock(pPager->
2bbd0 66 64 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f fd, EXCLUSIVE_LO
2bbe0 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 CK);. pPa
2bbf0 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 ger->state = PAG
2bc00 45 52 5f 53 48 41 52 45 44 3b 0a 20 20 20 20 20 ER_SHARED;.
2bc10 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
2bc20 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
2bc30 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
2bc40 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 }. s
2bc50 71 6c 69 74 65 33 57 61 6c 45 78 63 6c 75 73 69 qlite3WalExclusi
2bc60 76 65 4d 6f 64 65 28 70 50 61 67 65 72 2d 3e 70 veMode(pPager->p
2bc70 57 61 6c 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d Wal, 1);. }
2bc80 0a 0a 20 20 20 20 20 20 2f 2a 20 47 72 61 62 20 .. /* Grab
2bc90 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f the write lock o
2bca0 6e 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 2e 20 n the log file.
2bcb0 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 75 If successful, u
2bcc0 70 67 72 61 64 65 20 74 6f 0a 20 20 20 20 20 20 pgrade to.
2bcd0 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 ** PAGER_RESERVE
2bce0 44 20 73 74 61 74 65 2e 20 4f 74 68 65 72 77 69 D state. Otherwi
2bcf0 73 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 se, return an er
2bd00 72 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20 ror code to the
2bd10 63 61 6c 6c 65 72 2e 0a 20 20 20 20 20 20 2a 2a caller.. **
2bd20 20 54 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 The busy-handle
2bd30 72 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 r is not invoked
2bd40 20 69 66 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e if another conn
2bd50 65 63 74 69 6f 6e 20 61 6c 72 65 61 64 79 0a 20 ection already.
2bd60 20 20 20 20 20 2a 2a 20 68 6f 6c 64 73 20 74 68 ** holds th
2bd70 65 20 77 72 69 74 65 2d 6c 6f 63 6b 2e 20 49 66 e write-lock. If
2bd80 20 70 6f 73 73 69 62 6c 65 2c 20 74 68 65 20 75 possible, the u
2bd90 70 70 65 72 20 6c 61 79 65 72 20 77 69 6c 6c 20 pper layer will
2bda0 63 61 6c 6c 20 69 74 2e 0a 20 20 20 20 20 20 2a call it.. *
2bdb0 2a 0a 20 20 20 20 20 20 2a 2a 20 57 41 4c 20 6d *. ** WAL m
2bdc0 6f 64 65 20 73 65 74 73 20 50 61 67 65 72 2e 73 ode sets Pager.s
2bdd0 74 61 74 65 20 74 6f 20 50 41 47 45 52 5f 52 45 tate to PAGER_RE
2bde0 53 45 52 56 45 44 20 77 68 65 6e 20 69 74 20 68 SERVED when it h
2bdf0 61 73 20 61 6e 20 6f 70 65 6e 0a 20 20 20 20 20 as an open.
2be00 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c ** transaction,
2be10 20 62 75 74 20 6e 65 76 65 72 20 74 6f 20 50 41 but never to PA
2be20 47 45 52 5f 45 58 43 4c 55 53 49 56 45 2e 20 54 GER_EXCLUSIVE. T
2be30 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 69 his is because i
2be40 6e 20 0a 20 20 20 20 20 20 2a 2a 20 50 41 47 45 n . ** PAGE
2be50 52 5f 45 58 43 4c 55 53 49 56 45 20 73 74 61 74 R_EXCLUSIVE stat
2be60 65 20 74 68 65 20 63 6f 64 65 20 74 6f 20 72 6f e the code to ro
2be70 6c 6c 20 62 61 63 6b 20 73 61 76 65 70 6f 69 6e ll back savepoin
2be80 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 0a 20 t transactions.
2be90 20 20 20 20 20 2a 2a 20 6d 61 79 20 63 6f 70 79 ** may copy
2bea0 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 73 data from the s
2beb0 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 6e 74 6f 20 ub-journal into
2bec0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
2bed0 65 20 61 73 20 77 65 6c 6c 0a 20 20 20 20 20 20 e as well.
2bee0 2a 2a 20 61 73 20 69 6e 74 6f 20 74 68 65 20 70 ** as into the p
2bef0 61 67 65 20 63 61 63 68 65 2e 20 57 68 69 63 68 age cache. Which
2bf00 20 77 6f 75 6c 64 20 62 65 20 69 6e 63 6f 72 72 would be incorr
2bf10 65 63 74 20 69 6e 20 57 41 4c 20 6d 6f 64 65 2e ect in WAL mode.
2bf20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
2bf30 72 63 20 3d 20 73 71 6c 69 74 65 33 57 61 6c 42 rc = sqlite3WalB
2bf40 65 67 69 6e 57 72 69 74 65 54 72 61 6e 73 61 63 eginWriteTransac
2bf50 74 69 6f 6e 28 70 50 61 67 65 72 2d 3e 70 57 61 tion(pPager->pWa
2bf60 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 l);. if( rc
2bf70 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
2bf80 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 pPager->d
2bf90 62 4f 72 69 67 53 69 7a 65 20 3d 20 70 50 61 67 bOrigSize = pPag
2bfa0 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 20 er->dbSize;.
2bfb0 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 pPager->stat
2bfc0 65 20 3d 20 50 41 47 45 52 5f 52 45 53 45 52 56 e = PAGER_RESERV
2bfd0 45 44 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 ED;. pPag
2bfe0 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d er->journalOff =
2bff0 20 30 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 0;. }..
2c000 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 assert( rc!=S
2c010 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 QLITE_OK || pPag
2c020 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 er->state==PAGER
2c030 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 20 _RESERVED );.
2c040 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
2c050 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 QLITE_OK || pPag
2c060 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 er->state==PAGER
2c070 5f 53 48 41 52 45 44 20 29 3b 0a 20 20 20 20 7d _SHARED );. }
2c080 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4f else{. /* O
2c090 62 74 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 btain a RESERVED
2c0a0 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
2c0b0 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 abase file. If t
2c0c0 68 65 20 65 78 46 6c 61 67 20 70 61 72 61 6d 65 he exFlag parame
2c0d0 74 65 72 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 ter. ** is
2c0e0 74 72 75 65 2c 20 74 68 65 6e 20 69 6d 6d 65 64 true, then immed
2c0f0 69 61 74 65 6c 79 20 75 70 67 72 61 64 65 20 74 iately upgrade t
2c100 68 69 73 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 his to an EXCLUS
2c110 49 56 45 20 6c 6f 63 6b 2e 20 54 68 65 0a 20 20 IVE lock. The.
2c120 20 20 20 20 2a 2a 20 62 75 73 79 2d 68 61 6e 64 ** busy-hand
2c130 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 63 61 6e ler callback can
2c140 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 75 70 be used when up
2c150 67 72 61 64 69 6e 67 20 74 6f 20 74 68 65 20 45 grading to the E
2c160 58 43 4c 55 53 49 56 45 0a 20 20 20 20 20 20 2a XCLUSIVE. *
2c170 2a 20 6c 6f 63 6b 2c 20 62 75 74 20 6e 6f 74 20 * lock, but not
2c180 77 68 65 6e 20 6f 62 74 61 69 6e 69 6e 67 20 74 when obtaining t
2c190 68 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b he RESERVED lock
2c1a0 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
2c1b0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c rc = sqlite3OsL
2c1c0 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ock(pPager->fd,
2c1d0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 3b 0a RESERVED_LOCK);.
2c1e0 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
2c1f0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
2c200 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 pPager->state
2c210 20 3d 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 = PAGER_RESERVE
2c220 44 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 D;. if( e
2c230 78 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 xFlag ){.
2c240 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 rc = pager_wa
2c250 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 it_on_lock(pPage
2c260 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 r, EXCLUSIVE_LOC
2c270 4b 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 K);. }.
2c280 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
2c290 20 2f 2a 20 4e 6f 20 6e 65 65 64 20 74 6f 20 6f /* No need to o
2c2a0 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 pen the journal
2c2b0 66 69 6c 65 20 61 74 20 74 68 69 73 20 74 69 6d file at this tim
2c2c0 65 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 0a 20 e. It will be.
2c2d0 20 20 20 2a 2a 20 6f 70 65 6e 65 64 20 62 65 66 ** opened bef
2c2e0 6f 72 65 20 69 74 20 69 73 20 77 72 69 74 74 65 ore it is writte
2c2f0 6e 20 74 6f 2e 20 20 49 66 20 77 65 20 64 65 66 n to. If we def
2c300 65 72 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 6a er opening the j
2c310 6f 75 72 6e 61 6c 2c 0a 20 20 20 20 2a 2a 20 77 ournal,. ** w
2c320 65 20 6d 69 67 68 74 20 73 61 76 65 20 74 68 65 e might save the
2c330 20 77 6f 72 6b 20 6f 66 20 63 72 65 61 74 69 6e work of creatin
2c340 67 20 61 20 66 69 6c 65 20 69 66 20 74 68 65 20 g a file if the
2c350 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 transaction.
2c360 2a 2a 20 65 6e 64 73 20 75 70 20 62 65 69 6e 67 ** ends up being
2c370 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20 2a 2f a no-op.. */
2c380 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 4f . }else if( isO
2c390 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
2c3a0 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 && pPager->jour
2c3b0 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 nalOff==0 ){.
2c3c0 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 /* This happens
2c3d0 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 when the pager
2c3e0 77 61 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 was in exclusive
2c3f0 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 74 68 65 -access mode the
2c400 20 6c 61 73 74 0a 20 20 20 20 2a 2a 20 74 69 6d last. ** tim
2c410 65 20 61 20 28 72 65 61 64 20 6f 72 20 77 72 69 e a (read or wri
2c420 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 te) transaction
2c430 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 was successfully
2c440 20 63 6f 6e 63 6c 75 64 65 64 0a 20 20 20 20 2a concluded. *
2c450 2a 20 62 79 20 74 68 69 73 20 63 6f 6e 6e 65 63 * by this connec
2c460 74 69 6f 6e 2e 20 49 6e 73 74 65 61 64 20 6f 66 tion. Instead of
2c470 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6a 6f deleting the jo
2c480 75 72 6e 61 6c 20 66 69 6c 65 20 69 74 20 77 61 urnal file it wa
2c490 73 20 0a 20 20 20 20 2a 2a 20 6b 65 70 74 20 6f s . ** kept o
2c4a0 70 65 6e 20 61 6e 64 20 65 69 74 68 65 72 20 77 pen and either w
2c4b0 61 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 as truncated to
2c4c0 30 20 62 79 74 65 73 20 6f 72 20 69 74 73 20 68 0 bytes or its h
2c4d0 65 61 64 65 72 20 77 61 73 0a 20 20 20 20 2a 2a eader was. **
2c4e0 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 overwritten wit
2c4f0 68 20 7a 65 72 6f 73 2e 0a 20 20 20 20 2a 2f 0a h zeros.. */.
2c500 20 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65 assert( page
2c510 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 3d rUseWal(pPager)=
2c520 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =0 );. assert
2c530 28 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 3d 3d ( pPager->nRec==
2c540 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
2c550 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 pPager->dbOrigS
2c560 69 7a 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 ize==0 );. as
2c570 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49 sert( pPager->pI
2c580 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 nJournal==0 );.
2c590 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 6f 70 rc = pager_op
2c5a0 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65 en_journal(pPage
2c5b0 72 29 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 r);. }.. PAGER
2c5c0 54 52 41 43 45 28 28 22 54 52 41 4e 53 41 43 54 TRACE(("TRANSACT
2c5d0 49 4f 4e 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 ION %d\n", PAGER
2c5e0 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 ID(pPager)));.
2c5f0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
2c600 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 K ){. assert(
2c610 20 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 !pPager->dbModi
2c620 66 69 65 64 20 29 3b 0a 20 20 20 20 2f 2a 20 49 fied );. /* I
2c630 67 6e 6f 72 65 20 61 6e 79 20 49 4f 20 65 72 72 gnore any IO err
2c640 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 77 or that occurs w
2c650 69 74 68 69 6e 20 70 61 67 65 72 5f 65 6e 64 5f ithin pager_end_
2c660 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 2e 20 54 transaction(). T
2c670 68 65 0a 20 20 20 20 2a 2a 20 70 75 72 70 6f 73 he. ** purpos
2c680 65 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 20 69 e of this call i
2c690 73 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 69 s to reset the i
2c6a0 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 nternal state of
2c6b0 20 74 68 65 20 70 61 67 65 72 0a 20 20 20 20 2a the pager. *
2c6c0 2a 20 73 75 62 2d 73 79 73 74 65 6d 2e 20 49 74 * sub-system. It
2c6d0 20 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 doesn't matter
2c6e0 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 if the journal-f
2c6f0 69 6c 65 20 69 73 20 6e 6f 74 20 70 72 6f 70 65 ile is not prope
2c700 72 6c 79 0a 20 20 20 20 2a 2a 20 66 69 6e 61 6c rly. ** final
2c710 69 7a 65 64 20 61 74 20 74 68 69 73 20 70 6f 69 ized at this poi
2c720 6e 74 20 28 73 69 6e 63 65 20 69 74 20 69 73 20 nt (since it is
2c730 6e 6f 74 20 61 20 76 61 6c 69 64 20 6a 6f 75 72 not a valid jour
2c740 6e 61 6c 20 66 69 6c 65 20 61 6e 79 77 61 79 29 nal file anyway)
2c750 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 61 67 .. */. pag
2c760 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 er_end_transacti
2c770 6f 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 on(pPager, 0);.
2c780 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
2c790 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 }../*.** Mark a
2c7a0 73 69 6e 67 6c 65 20 64 61 74 61 20 70 61 67 65 single data page
2c7b0 20 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54 as writeable. T
2c7c0 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74 he page is writt
2c7d0 65 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 en into the .**
2c7e0 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 main journal or
2c7f0 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 20 72 sub-journal as r
2c800 65 71 75 69 72 65 64 2e 20 49 66 20 74 68 65 20 equired. If the
2c810 70 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 page is written
2c820 69 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 into.** one of t
2c830 68 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 he journals, the
2c840 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 corresponding b
2c850 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 it is set in the
2c860 20 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f .** Pager.pInJo
2c870 75 72 6e 61 6c 20 62 69 74 76 65 63 20 61 6e 64 urnal bitvec and
2c880 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f the PagerSavepo
2c890 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 int.pInSavepoint
2c8a0 20 62 69 74 76 65 63 73 0a 2a 2a 20 6f 66 20 61 bitvecs.** of a
2c8b0 6e 79 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e ny open savepoin
2c8c0 74 73 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 ts as appropriat
2c8d0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
2c8e0 20 70 61 67 65 72 5f 77 72 69 74 65 28 50 67 48 pager_write(PgH
2c8f0 64 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69 64 dr *pPg){. void
2c900 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 *pData = pPg->p
2c910 44 61 74 61 3b 0a 20 20 50 61 67 65 72 20 2a 70 Data;. Pager *p
2c920 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 Pager = pPg->pPa
2c930 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ger;. int rc =
2c940 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a SQLITE_OK;.. /*
2c950 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
2c960 20 6e 6f 74 20 63 61 6c 6c 65 64 20 75 6e 6c 65 not called unle
2c970 73 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e ss a transaction
2c980 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
2c990 6e 0a 20 20 2a 2a 20 73 74 61 72 74 65 64 2e 0a n. ** started..
2c9a0 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
2c9b0 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
2c9c0 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a GER_RESERVED );.
2c9d0 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f . /* If an erro
2c9e0 72 20 68 61 73 20 62 65 65 6e 20 70 72 65 76 69 r has been previ
2c9f0 6f 75 73 6c 79 20 64 65 74 65 63 74 65 64 2c 20 ously detected,
2ca00 72 65 70 6f 72 74 20 74 68 65 20 73 61 6d 65 20 report the same
2ca10 65 72 72 6f 72 0a 20 20 2a 2a 20 61 67 61 69 6e error. ** again
2ca20 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 .. */. if( NEV
2ca30 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f ER(pPager->errCo
2ca40 64 65 29 20 29 20 20 72 65 74 75 72 6e 20 70 50 de) ) return pP
2ca50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a ager->errCode;..
2ca60 20 20 2f 2a 20 48 69 67 68 65 72 2d 6c 65 76 65 /* Higher-leve
2ca70 6c 20 72 6f 75 74 69 6e 65 73 20 6e 65 76 65 72 l routines never
2ca80 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 call this funct
2ca90 69 6f 6e 20 69 66 20 64 61 74 61 62 61 73 65 20 ion if database
2caa0 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 77 72 69 74 is not. ** writ
2cab0 61 62 6c 65 2e 20 20 42 75 74 20 63 68 65 63 6b able. But check
2cac0 20 61 6e 79 77 61 79 2c 20 6a 75 73 74 20 66 6f anyway, just fo
2cad0 72 20 72 6f 62 75 73 74 6e 65 73 73 2e 20 2a 2f r robustness. */
2cae0 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 . if( NEVER(pPa
2caf0 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29 ger->readOnly) )
2cb00 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 return SQLITE_P
2cb10 45 52 4d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ERM;.. assert(
2cb20 21 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 !pPager->setMast
2cb30 65 72 20 29 3b 0a 0a 20 20 43 48 45 43 4b 5f 50 er );.. CHECK_P
2cb40 41 47 45 28 70 50 67 29 3b 0a 0a 20 20 2f 2a 20 AGE(pPg);.. /*
2cb50 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 Mark the page as
2cb60 20 64 69 72 74 79 2e 20 20 49 66 20 74 68 65 20 dirty. If the
2cb70 70 61 67 65 20 68 61 73 20 61 6c 72 65 61 64 79 page has already
2cb80 20 62 65 65 6e 20 77 72 69 74 74 65 6e 0a 20 20 been written.
2cb90 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 ** to the journa
2cba0 6c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 72 65 l then we can re
2cbb0 74 75 72 6e 20 72 69 67 68 74 20 61 77 61 79 2e turn right away.
2cbc0 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 . */. sqlite3P
2cbd0 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 cacheMakeDirty(p
2cbe0 50 67 29 3b 0a 20 20 69 66 28 20 70 61 67 65 49 Pg);. if( pageI
2cbf0 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26 nJournal(pPg) &&
2cc00 20 21 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 !subjRequiresPa
2cc10 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20 20 61 ge(pPg) ){. a
2cc20 73 73 65 72 74 28 20 21 70 61 67 65 72 55 73 65 ssert( !pagerUse
2cc30 57 61 6c 28 70 50 61 67 65 72 29 20 29 3b 0a 20 Wal(pPager) );.
2cc40 20 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 pPager->dbMod
2cc50 69 66 69 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c ified = 1;. }el
2cc60 73 65 7b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 se{.. /* If w
2cc70 65 20 67 65 74 20 74 68 69 73 20 66 61 72 2c 20 e get this far,
2cc80 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 it means that th
2cc90 65 20 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20 e page needs to
2cca0 62 65 0a 20 20 20 20 2a 2a 20 77 72 69 74 74 65 be. ** writte
2ccb0 6e 20 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 n to the transac
2ccc0 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 tion journal or
2ccd0 74 68 65 20 63 6b 65 63 6b 70 6f 69 6e 74 20 6a the ckeckpoint j
2cce0 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 6f 72 ournal. ** or
2ccf0 20 62 6f 74 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 both.. **.
2cd00 20 20 2a 2a 20 48 69 67 68 65 72 20 6c 65 76 65 ** Higher leve
2cd10 6c 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c l routines shoul
2cd20 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 73 d have already s
2cd30 74 61 72 74 65 64 20 61 20 74 72 61 6e 73 61 63 tarted a transac
2cd40 74 69 6f 6e 2c 0a 20 20 20 20 2a 2a 20 77 68 69 tion,. ** whi
2cd50 63 68 20 6d 65 61 6e 73 20 74 68 65 79 20 68 61 ch means they ha
2cd60 76 65 20 61 63 71 75 69 72 65 64 20 74 68 65 20 ve acquired the
2cd70 6e 65 63 65 73 73 61 72 79 20 6c 6f 63 6b 73 20 necessary locks
2cd80 62 75 74 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b but the rollback
2cd90 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 . ** journal
2cda0 6d 69 67 68 74 20 6e 6f 74 20 79 65 74 20 62 65 might not yet be
2cdb0 20 6f 70 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 open.. */.
2cdc0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
2cdd0 67 65 72 42 65 67 69 6e 28 70 50 61 67 65 72 2c gerBegin(pPager,
2cde0 20 30 2c 20 70 50 61 67 65 72 2d 3e 73 75 62 6a 0, pPager->subj
2cdf0 49 6e 4d 65 6d 6f 72 79 29 3b 0a 20 20 20 20 69 InMemory);. i
2ce00 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
2ce10 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
2ce20 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 rc;. }. i
2ce30 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 f( !isOpen(pPage
2ce40 72 2d 3e 6a 66 64 29 20 0a 20 20 20 20 20 26 26 r->jfd) . &&
2ce50 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
2ce60 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode!=PAGER_JOUR
2ce70 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 20 NALMODE_OFF .
2ce80 20 20 26 26 20 21 70 61 67 65 72 55 73 65 57 61 && !pagerUseWa
2ce90 6c 28 70 50 61 67 65 72 29 0a 20 20 20 20 29 7b l(pPager). ){
2cea0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
2ceb0 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 Pager->useJourna
2cec0 6c 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 l );. rc =
2ced0 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e pager_open_journ
2cee0 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 al(pPager);.
2cef0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
2cf00 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b _OK ) return rc;
2cf10 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 . }. pPage
2cf20 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 r->dbModified =
2cf30 31 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 54 68 65 1;. . /* The
2cf40 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 transaction jou
2cf50 72 6e 61 6c 20 6e 6f 77 20 65 78 69 73 74 73 20 rnal now exists
2cf60 61 6e 64 20 77 65 20 68 61 76 65 20 61 20 52 45 and we have a RE
2cf70 53 45 52 56 45 44 20 6f 72 20 61 6e 0a 20 20 20 SERVED or an.
2cf80 20 2a 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f ** EXCLUSIVE lo
2cf90 63 6b 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64 ck on the main d
2cfa0 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 57 atabase file. W
2cfb0 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 rite the current
2cfc0 20 70 61 67 65 20 74 6f 0a 20 20 20 20 2a 2a 20 page to. **
2cfd0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
2cfe0 6a 6f 75 72 6e 61 6c 20 69 66 20 69 74 20 69 73 journal if it is
2cff0 20 6e 6f 74 20 74 68 65 72 65 20 61 6c 72 65 61 not there alrea
2d000 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 dy.. */. i
2d010 66 28 20 21 70 61 67 65 49 6e 4a 6f 75 72 6e 61 f( !pageInJourna
2d020 6c 28 70 50 67 29 20 26 26 20 69 73 4f 70 65 6e l(pPg) && isOpen
2d030 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b (pPager->jfd) ){
2d040 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 . assert( !
2d050 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 pagerUseWal(pPag
2d060 65 72 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 er) );. if(
2d070 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 pPg->pgno<=pPag
2d080 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 er->dbOrigSize )
2d090 7b 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 6b {. u32 ck
2d0a0 73 75 6d 3b 0a 20 20 20 20 20 20 20 20 63 68 61 sum;. cha
2d0b0 72 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 r *pData2;..
2d0c0 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 /* We should
2d0d0 20 6e 65 76 65 72 20 77 72 69 74 65 20 74 6f 20 never write to
2d0e0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
2d0f0 20 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 20 the page that.
2d100 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 ** contai
2d110 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ns the database
2d120 6c 6f 63 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c locks. The foll
2d130 6f 77 69 6e 67 20 61 73 73 65 72 74 20 76 65 72 owing assert ver
2d140 69 66 69 65 73 0a 20 20 20 20 20 20 20 20 2a 2a ifies. **
2d150 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e that we do not.
2d160 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 */. asse
2d170 72 74 28 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 rt( pPg->pgno!=P
2d180 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 AGER_MJ_PGNO(pPa
2d190 67 65 72 29 20 29 3b 0a 0a 20 20 20 20 20 20 20 ger) );..
2d1a0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
2d1b0 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3c 3d 20 70 >journalHdr <= p
2d1c0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
2d1d0 66 20 29 3b 0a 20 20 20 20 20 20 20 20 43 4f 44 f );. COD
2d1e0 45 43 32 28 70 50 61 67 65 72 2c 20 70 44 61 74 EC2(pPager, pDat
2d1f0 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 37 2c a, pPg->pgno, 7,
2d200 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
2d210 4f 4d 45 4d 2c 20 70 44 61 74 61 32 29 3b 0a 20 OMEM, pData2);.
2d220 20 20 20 20 20 20 20 63 6b 73 75 6d 20 3d 20 70 cksum = p
2d230 61 67 65 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 ager_cksum(pPage
2d240 72 2c 20 28 75 38 2a 29 70 44 61 74 61 32 29 3b r, (u8*)pData2);
2d250 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 . rc = wr
2d260 69 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 ite32bits(pPager
2d270 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a ->jfd, pPager->j
2d280 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e ournalOff, pPg->
2d290 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 pgno);. i
2d2a0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
2d2b0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
2d2c0 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
2d2d0 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 e(pPager->jfd, p
2d2e0 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 Data2, pPager->p
2d2f0 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 ageSize,.
2d300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2d310 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a pPager->j
2d320 6f 75 72 6e 61 6c 4f 66 66 20 2b 20 34 29 3b 0a ournalOff + 4);.
2d330 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 pPager
2d340 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 ->journalOff +=
2d350 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
2d360 2b 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 +4;. }.
2d370 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
2d380 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
2d390 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 33 rc = write3
2d3a0 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 2bits(pPager->jf
2d3b0 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e d, pPager->journ
2d3c0 61 6c 4f 66 66 2c 20 63 6b 73 75 6d 29 3b 0a 20 alOff, cksum);.
2d3d0 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d pPager-
2d3e0 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 >journalOff += 4
2d3f0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
2d400 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 4f IOTRACE(("JO
2d410 55 54 20 25 70 20 25 64 20 25 6c 6c 64 20 25 64 UT %p %d %lld %d
2d420 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 \n", pPager, pPg
2d430 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 ->pgno, .
2d440 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 pPager
2d450 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 ->journalOff, pP
2d460 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 29 ager->pageSize))
2d470 3b 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 5f ;. PAGER_
2d480 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 INCR(sqlite3_pag
2d490 65 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 29 er_writej_count)
2d4a0 3b 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54 ;. PAGERT
2d4b0 52 41 43 45 28 28 22 4a 4f 55 52 4e 41 4c 20 25 RACE(("JOURNAL %
2d4c0 64 20 70 61 67 65 20 25 64 20 6e 65 65 64 53 79 d page %d needSy
2d4d0 6e 63 3d 25 64 20 68 61 73 68 28 25 30 38 78 29 nc=%d hash(%08x)
2d4e0 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
2d4f0 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 PAGERID(pPager
2d500 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 ), pPg->pgno, .
2d510 20 20 20 20 20 20 20 20 20 20 20 20 28 28 70 50 ((pP
2d520 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
2d530 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29 2c 20 EED_SYNC)?1:0),
2d540 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 pager_pagehash(p
2d550 50 67 29 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 Pg)));..
2d560 2f 2a 20 45 76 65 6e 20 69 66 20 61 6e 20 49 4f /* Even if an IO
2d570 20 6f 72 20 64 69 73 6b 66 75 6c 6c 20 65 72 72 or diskfull err
2d580 6f 72 20 6f 63 63 75 72 72 65 64 20 77 68 69 6c or occurred whil
2d590 65 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 74 68 e journalling th
2d5a0 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 e. ** pag
2d5b0 65 20 69 6e 20 74 68 65 20 62 6c 6f 63 6b 20 61 e in the block a
2d5c0 62 6f 76 65 2c 20 73 65 74 20 74 68 65 20 6e 65 bove, set the ne
2d5d0 65 64 2d 73 79 6e 63 20 66 6c 61 67 20 66 6f 72 ed-sync flag for
2d5e0 20 74 68 65 20 70 61 67 65 2e 0a 20 20 20 20 20 the page..
2d5f0 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c ** Otherwise,
2d600 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 when the transa
2d610 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 ction is rolled
2d620 62 61 63 6b 2c 20 74 68 65 20 6c 6f 67 69 63 20 back, the logic
2d630 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6c in. ** pl
2d640 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 ayback_one_page(
2d650 29 20 77 69 6c 6c 20 74 68 69 6e 6b 20 74 68 61 ) will think tha
2d660 74 20 74 68 65 20 70 61 67 65 20 6e 65 65 64 73 t the page needs
2d670 20 74 6f 20 62 65 20 72 65 73 74 6f 72 65 64 0a to be restored.
2d680 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 ** in th
2d690 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
2d6a0 20 41 6e 64 20 69 66 20 61 6e 20 49 4f 20 65 72 And if an IO er
2d6b0 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 ror occurs while
2d6c0 20 64 6f 69 6e 67 20 73 6f 2c 0a 20 20 20 20 20 doing so,.
2d6d0 20 20 20 2a 2a 20 74 68 65 6e 20 63 6f 72 72 75 ** then corru
2d6e0 70 74 69 6f 6e 20 6d 61 79 20 66 6f 6c 6c 6f 77 ption may follow
2d6f0 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
2d700 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 if( !pPager
2d710 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 ->noSync ){.
2d720 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 pPg->flags
2d730 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 |= PGHDR_NEED_S
2d740 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 20 70 YNC;. p
2d750 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 Pager->needSync
2d760 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a = 1;. }..
2d770 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 72 /* An er
2d780 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 ror has occurred
2d790 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 writing to the
2d7a0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 journal file. Th
2d7b0 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 e . ** tr
2d7c0 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 ansaction will b
2d7d0 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 79 e rolled back by
2d7e0 20 74 68 65 20 6c 61 79 65 72 20 61 62 6f 76 65 the layer above
2d7f0 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
2d800 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
2d810 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
2d820 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
2d830 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 }..
2d840 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 2b 2b pPager->nRec++
2d850 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
2d860 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 ( pPager->pInJou
2d870 72 6e 61 6c 21 3d 30 20 29 3b 0a 20 20 20 20 20 rnal!=0 );.
2d880 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
2d890 69 74 76 65 63 53 65 74 28 70 50 61 67 65 72 2d itvecSet(pPager-
2d8a0 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 >pInJournal, pPg
2d8b0 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 ->pgno);.
2d8c0 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 testcase( rc==S
2d8d0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 QLITE_NOMEM );.
2d8e0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 assert( r
2d8f0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c==SQLITE_OK ||
2d900 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
2d910 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 7c );. rc |
2d920 3d 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 = addToSavepoint
2d930 42 69 74 76 65 63 73 28 70 50 61 67 65 72 2c 20 Bitvecs(pPager,
2d940 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 pPg->pgno);.
2d950 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
2d960 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
2d970 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
2d980 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 QLITE_NOMEM );.
2d990 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
2d9a0 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rc;. }.
2d9b0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
2d9c0 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e if( !pPager->
2d9d0 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 26 journalStarted &
2d9e0 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e & !pPager->noSyn
2d9f0 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 c ){. p
2da00 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 Pg->flags |= PGH
2da10 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 DR_NEED_SYNC;.
2da20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
2da30 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 needSync = 1;.
2da40 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
2da50 50 41 47 45 52 54 52 41 43 45 28 28 22 41 50 50 PAGERTRACE(("APP
2da60 45 4e 44 20 25 64 20 70 61 67 65 20 25 64 20 6e END %d page %d n
2da70 65 65 64 53 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20 eedSync=%d\n",.
2da80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 P
2da90 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 AGERID(pPager),
2daa0 70 50 67 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 pPg->pgno,.
2dab0 20 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d ((pPg-
2dac0 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
2dad0 44 5f 53 59 4e 43 29 3f 31 3a 30 29 29 29 3b 0a D_SYNC)?1:0)));.
2dae0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
2daf0 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 . /* If the s
2db00 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c tatement journal
2db10 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 is open and the
2db20 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 page is not in
2db30 69 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 it,. ** then
2db40 77 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e write the curren
2db50 74 20 70 61 67 65 20 74 6f 20 74 68 65 20 73 74 t page to the st
2db60 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e atement journal.
2db70 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 20 20 Note that.
2db80 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 ** the statement
2db90 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 journal format
2dba0 64 69 66 66 65 72 73 20 66 72 6f 6d 20 74 68 65 differs from the
2dbb0 20 73 74 61 6e 64 61 72 64 20 6a 6f 75 72 6e 61 standard journa
2dbc0 6c 20 66 6f 72 6d 61 74 0a 20 20 20 20 2a 2a 20 l format. **
2dbd0 69 6e 20 74 68 61 74 20 69 74 20 6f 6d 69 74 73 in that it omits
2dbe0 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 20 61 the checksums a
2dbf0 6e 64 20 74 68 65 20 68 65 61 64 65 72 2e 0a 20 nd the header..
2dc00 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 75 */. if( su
2dc10 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70 bjRequiresPage(p
2dc20 50 67 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 Pg) ){. rc
2dc30 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 = subjournalPage
2dc40 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d (pPg);. }. }
2dc50 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 .. /* Update th
2dc60 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 e database size
2dc70 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f and return.. */
2dc80 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
2dc90 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f r->state>=PAGER_
2dca0 53 48 41 52 45 44 20 29 3b 0a 20 20 69 66 28 20 SHARED );. if(
2dcb0 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3c 70 pPager->dbSize<p
2dcc0 50 67 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 Pg->pgno ){.
2dcd0 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d pPager->dbSize =
2dce0 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 7d 0a pPg->pgno;. }.
2dcf0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
2dd00 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 64 61 74 /*.** Mark a dat
2dd10 61 20 70 61 67 65 20 61 73 20 77 72 69 74 65 61 a page as writea
2dd20 62 6c 65 2e 20 54 68 69 73 20 72 6f 75 74 69 6e ble. This routin
2dd30 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 e must be called
2dd40 20 62 65 66 6f 72 65 20 0a 2a 2a 20 6d 61 6b 69 before .** maki
2dd50 6e 67 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20 ng changes to a
2dd60 70 61 67 65 2e 20 54 68 65 20 63 61 6c 6c 65 72 page. The caller
2dd70 20 6d 75 73 74 20 63 68 65 63 6b 20 74 68 65 20 must check the
2dd80 72 65 74 75 72 6e 20 76 61 6c 75 65 20 0a 2a 2a return value .**
2dd90 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f of this functio
2dda0 6e 20 61 6e 64 20 62 65 20 63 61 72 65 66 75 6c n and be careful
2ddb0 20 6e 6f 74 20 74 6f 20 63 68 61 6e 67 65 20 61 not to change a
2ddc0 6e 79 20 70 61 67 65 20 64 61 74 61 20 75 6e 6c ny page data unl
2ddd0 65 73 73 20 0a 2a 2a 20 74 68 69 73 20 72 6f 75 ess .** this rou
2dde0 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c tine returns SQL
2ddf0 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 ITE_OK..**.** Th
2de00 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 e difference bet
2de10 77 65 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 ween this functi
2de20 6f 6e 20 61 6e 64 20 70 61 67 65 72 5f 77 72 69 on and pager_wri
2de30 74 65 28 29 20 69 73 20 74 68 61 74 20 74 68 69 te() is that thi
2de40 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c s.** function al
2de50 73 6f 20 64 65 61 6c 73 20 77 69 74 68 20 74 68 so deals with th
2de60 65 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 77 e special case w
2de70 68 65 72 65 20 32 20 6f 72 20 6d 6f 72 65 20 70 here 2 or more p
2de80 61 67 65 73 0a 2a 2a 20 66 69 74 20 6f 6e 20 61 ages.** fit on a
2de90 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 single disk sec
2dea0 74 6f 72 2e 20 49 6e 20 74 68 69 73 20 63 61 73 tor. In this cas
2deb0 65 20 61 6c 6c 20 63 6f 2d 72 65 73 69 64 65 6e e all co-residen
2dec0 74 20 70 61 67 65 73 0a 2a 2a 20 6d 75 73 74 20 t pages.** must
2ded0 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 have been writte
2dee0 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c n to the journal
2def0 20 66 69 6c 65 20 62 65 66 6f 72 65 20 72 65 74 file before ret
2df00 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 urning..**.** If
2df10 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
2df20 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f , SQLITE_NOMEM o
2df30 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f r an IO error co
2df40 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a de is returned.*
2df50 2a 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 * as appropriate
2df60 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c . Otherwise, SQL
2df70 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 69 6e 74 20 73 ITE_OK..*/.int s
2df80 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
2df90 28 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 (DbPage *pDbPage
2dfa0 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
2dfb0 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 50 67 48 64 LITE_OK;.. PgHd
2dfc0 72 20 2a 70 50 67 20 3d 20 70 44 62 50 61 67 65 r *pPg = pDbPage
2dfd0 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 ;. Pager *pPage
2dfe0 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b r = pPg->pPager;
2dff0 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 50 65 72 . Pgno nPagePer
2e000 53 65 63 74 6f 72 20 3d 20 28 70 50 61 67 65 72 Sector = (pPager
2e010 2d 3e 73 65 63 74 6f 72 53 69 7a 65 2f 70 50 61 ->sectorSize/pPa
2e020 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a ger->pageSize);.
2e030 0a 20 20 69 66 28 20 6e 50 61 67 65 50 65 72 53 . if( nPagePerS
2e040 65 63 74 6f 72 3e 31 20 29 7b 0a 20 20 20 20 50 ector>1 ){. P
2e050 67 6e 6f 20 6e 50 61 67 65 43 6f 75 6e 74 3b 20 gno nPageCount;
2e060 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 /* Tota
2e070 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 l number of page
2e080 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 s in database fi
2e090 6c 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 70 le */. Pgno p
2e0a0 67 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 g1;
2e0b0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 /* First pag
2e0c0 65 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 e of the sector
2e0d0 70 50 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6f pPg is located o
2e0e0 6e 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 n. */. int nP
2e0f0 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
2e100 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
2e110 20 70 61 67 65 73 20 73 74 61 72 74 69 6e 67 20 pages starting
2e120 61 74 20 70 67 31 20 74 6f 20 6a 6f 75 72 6e 61 at pg1 to journa
2e130 6c 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b l */. int ii;
2e140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2e150 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
2e160 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65 er */. int ne
2e170 65 64 53 79 6e 63 20 3d 20 30 3b 20 20 20 20 20 edSync = 0;
2e180 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 /* True if a
2e190 6e 79 20 70 61 67 65 20 68 61 73 20 50 47 48 44 ny page has PGHD
2e1a0 52 5f 4e 45 45 44 5f 53 59 4e 43 20 2a 2f 0a 0a R_NEED_SYNC */..
2e1b0 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 64 /* Set the d
2e1c0 6f 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20 74 6f oNotSync flag to
2e1d0 20 31 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 1. This is beca
2e1e0 75 73 65 20 77 65 20 63 61 6e 6e 6f 74 20 61 6c use we cannot al
2e1f0 6c 6f 77 20 61 20 6a 6f 75 72 6e 61 6c 0a 20 20 low a journal.
2e200 20 20 2a 2a 20 68 65 61 64 65 72 20 74 6f 20 62 ** header to b
2e210 65 20 77 72 69 74 74 65 6e 20 62 65 74 77 65 65 e written betwee
2e220 6e 20 74 68 65 20 70 61 67 65 73 20 6a 6f 75 72 n the pages jour
2e230 6e 61 6c 65 64 20 62 79 20 74 68 69 73 20 66 75 naled by this fu
2e240 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 nction.. */.
2e250 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 assert( !MEMD
2e260 42 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 B );. assert(
2e270 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 pPager->doNotSy
2e280 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 nc==0 );. pPa
2e290 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 3d ger->doNotSync =
2e2a0 20 31 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 1;.. /* This
2e2b0 20 74 72 69 63 6b 20 61 73 73 75 6d 65 73 20 74 trick assumes t
2e2c0 68 61 74 20 62 6f 74 68 20 74 68 65 20 70 61 67 hat both the pag
2e2d0 65 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f e-size and secto
2e2e0 72 2d 73 69 7a 65 20 61 72 65 0a 20 20 20 20 2a r-size are. *
2e2f0 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 70 6f 77 * an integer pow
2e300 65 72 20 6f 66 20 32 2e 20 49 74 20 73 65 74 73 er of 2. It sets
2e310 20 76 61 72 69 61 62 6c 65 20 70 67 31 20 74 6f variable pg1 to
2e320 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 0a the identifier.
2e330 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 69 ** of the fi
2e340 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 rst page of the
2e350 73 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c 6f sector pPg is lo
2e360 63 61 74 65 64 20 6f 6e 2e 0a 20 20 20 20 2a 2f cated on.. */
2e370 0a 20 20 20 20 70 67 31 20 3d 20 28 28 70 50 67 . pg1 = ((pPg
2e380 2d 3e 70 67 6e 6f 2d 31 29 20 26 20 7e 28 6e 50 ->pgno-1) & ~(nP
2e390 61 67 65 50 65 72 53 65 63 74 6f 72 2d 31 29 29 agePerSector-1))
2e3a0 20 2b 20 31 3b 0a 0a 20 20 20 20 72 63 20 3d 20 + 1;.. rc =
2e3b0 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
2e3c0 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 28 69 count(pPager, (i
2e3d0 6e 74 20 2a 29 26 6e 50 61 67 65 43 6f 75 6e 74 nt *)&nPageCount
2e3e0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 );. if( rc )
2e3f0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 69 return rc;. i
2e400 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3e 6e 50 61 f( pPg->pgno>nPa
2e410 67 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 geCount ){.
2e420 20 6e 50 61 67 65 20 3d 20 28 70 50 67 2d 3e 70 nPage = (pPg->p
2e430 67 6e 6f 20 2d 20 70 67 31 29 2b 31 3b 0a 20 20 gno - pg1)+1;.
2e440 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 67 31 }else if( (pg1
2e450 2b 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 2d +nPagePerSector-
2e460 31 29 3e 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b 1)>nPageCount ){
2e470 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 6e . nPage = n
2e480 50 61 67 65 43 6f 75 6e 74 2b 31 2d 70 67 31 3b PageCount+1-pg1;
2e490 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
2e4a0 20 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 50 nPage = nPageP
2e4b0 65 72 53 65 63 74 6f 72 3b 0a 20 20 20 20 7d 0a erSector;. }.
2e4c0 20 20 20 20 61 73 73 65 72 74 28 6e 50 61 67 65 assert(nPage
2e4d0 3e 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 >0);. assert(
2e4e0 70 67 31 3c 3d 70 50 67 2d 3e 70 67 6e 6f 29 3b pg1<=pPg->pgno);
2e4f0 0a 20 20 20 20 61 73 73 65 72 74 28 28 70 67 31 . assert((pg1
2e500 2b 6e 50 61 67 65 29 3e 70 50 67 2d 3e 70 67 6e +nPage)>pPg->pgn
2e510 6f 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 69 3d o);.. for(ii=
2e520 30 3b 20 69 69 3c 6e 50 61 67 65 20 26 26 20 72 0; ii<nPage && r
2e530 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 69 c==SQLITE_OK; ii
2e540 2b 2b 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 ++){. Pgno
2e550 70 67 20 3d 20 70 67 31 2b 69 69 3b 0a 20 20 20 pg = pg1+ii;.
2e560 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 3b PgHdr *pPage;
2e570 0a 20 20 20 20 20 20 69 66 28 20 70 67 3d 3d 70 . if( pg==p
2e580 50 67 2d 3e 70 67 6e 6f 20 7c 7c 20 21 73 71 6c Pg->pgno || !sql
2e590 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 ite3BitvecTest(p
2e5a0 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
2e5b0 6c 2c 20 70 67 29 20 29 7b 0a 20 20 20 20 20 20 l, pg) ){.
2e5c0 20 20 69 66 28 20 70 67 21 3d 50 41 47 45 52 5f if( pg!=PAGER_
2e5d0 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 MJ_PGNO(pPager)
2e5e0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
2e5f0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
2e600 74 28 70 50 61 67 65 72 2c 20 70 67 2c 20 26 70 t(pPager, pg, &p
2e610 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Page);.
2e620 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
2e630 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
2e640 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 rc = pager_wri
2e650 74 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 te(pPage);.
2e660 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 if( pPage
2e670 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 ->flags&PGHDR_NE
2e680 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 20 ED_SYNC ){.
2e690 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e needSyn
2e6a0 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 c = 1;.
2e6b0 20 20 20 20 20 61 73 73 65 72 74 28 70 50 61 67 assert(pPag
2e6c0 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b 0a 20 er->needSync);.
2e6d0 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
2e6e0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
2e6f0 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 PagerUnref(pPage
2e700 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 );. }.
2e710 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
2e720 65 6c 73 65 20 69 66 28 20 28 70 50 61 67 65 20 else if( (pPage
2e730 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 = pager_lookup(p
2e740 50 61 67 65 72 2c 20 70 67 29 29 21 3d 30 20 29 Pager, pg))!=0 )
2e750 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 {. if( pP
2e760 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 age->flags&PGHDR
2e770 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 _NEED_SYNC ){.
2e780 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 needSync
2e790 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 1;. }.
2e7a0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 sqlite3P
2e7b0 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 agerUnref(pPage)
2e7c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
2e7d0 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 . /* If the P
2e7e0 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 GHDR_NEED_SYNC f
2e7f0 6c 61 67 20 69 73 20 73 65 74 20 66 6f 72 20 61 lag is set for a
2e800 6e 79 20 6f 66 20 74 68 65 20 6e 50 61 67 65 20 ny of the nPage
2e810 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 74 pages . ** st
2e820 61 72 74 69 6e 67 20 61 74 20 70 67 31 2c 20 74 arting at pg1, t
2e830 68 65 6e 20 69 74 20 6e 65 65 64 73 20 74 6f 20 hen it needs to
2e840 62 65 20 73 65 74 20 66 6f 72 20 61 6c 6c 20 6f be set for all o
2e850 66 20 74 68 65 6d 2e 20 42 65 63 61 75 73 65 0a f them. Because.
2e860 20 20 20 20 2a 2a 20 77 72 69 74 69 6e 67 20 74 ** writing t
2e870 6f 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 6e o any of these n
2e880 50 61 67 65 20 70 61 67 65 73 20 6d 61 79 20 64 Page pages may d
2e890 61 6d 61 67 65 20 74 68 65 20 6f 74 68 65 72 73 amage the others
2e8a0 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 6a 6f 75 , the. ** jou
2e8b0 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 63 rnal file must c
2e8c0 6f 6e 74 61 69 6e 20 73 79 6e 63 28 29 65 64 20 ontain sync()ed
2e8d0 63 6f 70 69 65 73 20 6f 66 20 61 6c 6c 20 6f 66 copies of all of
2e8e0 20 74 68 65 6d 0a 20 20 20 20 2a 2a 20 62 65 66 them. ** bef
2e8f0 6f 72 65 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 ore any of them
2e900 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 6f can be written o
2e910 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ut to the databa
2e920 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a se file.. */.
2e930 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
2e940 54 45 5f 4f 4b 20 26 26 20 6e 65 65 64 53 79 6e TE_OK && needSyn
2e950 63 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 c ){. asser
2e960 74 28 20 21 4d 45 4d 44 42 20 26 26 20 70 50 61 t( !MEMDB && pPa
2e970 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 29 ger->noSync==0 )
2e980 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 ;. for(ii=0
2e990 3b 20 69 69 3c 6e 50 61 67 65 3b 20 69 69 2b 2b ; ii<nPage; ii++
2e9a0 29 7b 0a 20 20 20 20 20 20 20 20 50 67 48 64 72 ){. PgHdr
2e9b0 20 2a 70 50 61 67 65 20 3d 20 70 61 67 65 72 5f *pPage = pager_
2e9c0 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 lookup(pPager, p
2e9d0 67 31 2b 69 69 29 3b 0a 20 20 20 20 20 20 20 20 g1+ii);.
2e9e0 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 if( pPage ){.
2e9f0 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e 66 6c pPage->fl
2ea00 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 ags |= PGHDR_NEE
2ea10 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 D_SYNC;.
2ea20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
2ea30 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 ref(pPage);.
2ea40 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
2ea50 20 20 20 20 61 73 73 65 72 74 28 70 50 61 67 65 assert(pPage
2ea60 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b 0a 20 20 r->needSync);.
2ea70 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 }.. assert(
2ea80 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 pPager->doNotSy
2ea90 6e 63 3d 3d 31 20 29 3b 0a 20 20 20 20 70 50 61 nc==1 );. pPa
2eaa0 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 3d ger->doNotSync =
2eab0 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
2eac0 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 rc = pager_writ
2ead0 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a e(pDbPage);. }.
2eae0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
2eaf0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 /*.** Return TRU
2eb00 45 20 69 66 20 74 68 65 20 70 61 67 65 20 67 69 E if the page gi
2eb10 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 6d ven in the argum
2eb20 65 6e 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 ent was previous
2eb30 6c 79 20 70 61 73 73 65 64 0a 2a 2a 20 74 6f 20 ly passed.** to
2eb40 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
2eb50 65 28 29 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 e(). In other w
2eb60 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54 52 55 ords, return TRU
2eb70 45 20 69 66 20 69 74 20 69 73 20 6f 6b 0a 2a 2a E if it is ok.**
2eb80 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 63 to change the c
2eb90 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 ontent of the pa
2eba0 67 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e ge..*/.#ifndef N
2ebb0 44 45 42 55 47 0a 69 6e 74 20 73 71 6c 69 74 65 DEBUG.int sqlite
2ebc0 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
2ebd0 65 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a e(DbPage *pPg){.
2ebe0 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 66 6c return pPg->fl
2ebf0 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 3b ags&PGHDR_DIRTY;
2ec00 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
2ec10 20 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 A call to this
2ec20 72 6f 75 74 69 6e 65 20 74 65 6c 6c 73 20 74 68 routine tells th
2ec30 65 20 70 61 67 65 72 20 74 68 61 74 20 69 74 20 e pager that it
2ec40 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 is not necessary
2ec50 20 74 6f 0a 2a 2a 20 77 72 69 74 65 20 74 68 65 to.** write the
2ec60 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 information on
2ec70 70 61 67 65 20 70 50 67 20 62 61 63 6b 20 74 6f page pPg back to
2ec80 20 74 68 65 20 64 69 73 6b 2c 20 65 76 65 6e 20 the disk, even
2ec90 74 68 6f 75 67 68 0a 2a 2a 20 74 68 61 74 20 70 though.** that p
2eca0 61 67 65 20 6d 69 67 68 74 20 62 65 20 6d 61 72 age might be mar
2ecb0 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 20 54 ked as dirty. T
2ecc0 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f 72 his happens, for
2ecd0 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a 2a example, when.*
2ece0 2a 20 74 68 65 20 70 61 67 65 20 68 61 73 20 62 * the page has b
2ecf0 65 65 6e 20 61 64 64 65 64 20 61 73 20 61 20 6c een added as a l
2ed00 65 61 66 20 6f 66 20 74 68 65 20 66 72 65 65 6c eaf of the freel
2ed10 69 73 74 20 61 6e 64 20 73 6f 20 69 74 73 0a 2a ist and so its.*
2ed20 2a 20 63 6f 6e 74 65 6e 74 20 6e 6f 20 6c 6f 6e * content no lon
2ed30 67 65 72 20 6d 61 74 74 65 72 73 2e 0a 2a 2a 0a ger matters..**.
2ed40 2a 2a 20 54 68 65 20 6f 76 65 72 6c 79 69 6e 67 ** The overlying
2ed50 20 73 6f 66 74 77 61 72 65 20 6c 61 79 65 72 20 software layer
2ed60 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 calls this routi
2ed70 6e 65 20 77 68 65 6e 20 61 6c 6c 20 6f 66 20 74 ne when all of t
2ed80 68 65 20 64 61 74 61 0a 2a 2a 20 6f 6e 20 74 68 he data.** on th
2ed90 65 20 67 69 76 65 6e 20 70 61 67 65 20 69 73 20 e given page is
2eda0 75 6e 75 73 65 64 2e 20 54 68 65 20 70 61 67 65 unused. The page
2edb0 72 20 6d 61 72 6b 73 20 74 68 65 20 70 61 67 65 r marks the page
2edc0 20 61 73 20 63 6c 65 61 6e 20 73 6f 0a 2a 2a 20 as clean so.**
2edd0 74 68 61 74 20 69 74 20 64 6f 65 73 20 6e 6f 74 that it does not
2ede0 20 67 65 74 20 77 72 69 74 74 65 6e 20 74 6f 20 get written to
2edf0 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 54 65 73 74 disk..**.** Test
2ee00 73 20 73 68 6f 77 20 74 68 61 74 20 74 68 69 73 s show that this
2ee10 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63 61 optimization ca
2ee20 6e 20 71 75 61 64 72 75 70 6c 65 20 74 68 65 20 n quadruple the
2ee30 73 70 65 65 64 20 6f 66 20 6c 61 72 67 65 20 0a speed of large .
2ee40 2a 2a 20 44 45 4c 45 54 45 20 6f 70 65 72 61 74 ** DELETE operat
2ee50 69 6f 6e 73 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 ions..*/.void sq
2ee60 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 lite3PagerDontWr
2ee70 69 74 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b ite(PgHdr *pPg){
2ee80 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
2ee90 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a = pPg->pPager;.
2eea0 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67 if( (pPg->flag
2eeb0 73 26 50 47 48 44 52 5f 44 49 52 54 59 29 20 26 s&PGHDR_DIRTY) &
2eec0 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 & pPager->nSavep
2eed0 6f 69 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 50 oint==0 ){. P
2eee0 41 47 45 52 54 52 41 43 45 28 28 22 44 4f 4e 54 AGERTRACE(("DONT
2eef0 5f 57 52 49 54 45 20 70 61 67 65 20 25 64 20 6f _WRITE page %d o
2ef00 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d 3e 70 67 f %d\n", pPg->pg
2ef10 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 no, PAGERID(pPag
2ef20 65 72 29 29 29 3b 0a 20 20 20 20 49 4f 54 52 41 er)));. IOTRA
2ef30 43 45 28 28 22 43 4c 45 41 4e 20 25 70 20 25 64 CE(("CLEAN %p %d
2ef40 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 \n", pPager, pPg
2ef50 2d 3e 70 67 6e 6f 29 29 0a 20 20 20 20 70 50 67 ->pgno)). pPg
2ef60 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 ->flags |= PGHDR
2ef70 5f 44 4f 4e 54 5f 57 52 49 54 45 3b 0a 23 69 66 _DONT_WRITE;.#if
2ef80 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b def SQLITE_CHECK
2ef90 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e _PAGES. pPg->
2efa0 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 pageHash = pager
2efb0 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a _pagehash(pPg);.
2efc0 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a #endif. }.}../*
2efd0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
2efe0 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 69 6e is called to in
2eff0 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 crement the valu
2f000 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
2f010 65 20 66 69 6c 65 20 0a 2a 2a 20 63 68 61 6e 67 e file .** chang
2f020 65 2d 63 6f 75 6e 74 65 72 2c 20 73 74 6f 72 65 e-counter, store
2f030 64 20 61 73 20 61 20 34 2d 62 79 74 65 20 62 69 d as a 4-byte bi
2f040 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 g-endian integer
2f050 20 73 74 61 72 74 69 6e 67 20 61 74 20 0a 2a 2a starting at .**
2f060 20 62 79 74 65 20 6f 66 66 73 65 74 20 32 34 20 byte offset 24
2f070 6f 66 20 74 68 65 20 70 61 67 65 72 20 66 69 6c of the pager fil
2f080 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
2f090 69 73 44 69 72 65 63 74 4d 6f 64 65 20 66 6c 61 isDirectMode fla
2f0a0 67 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 g is zero, then
2f0b0 74 68 69 73 20 69 73 20 64 6f 6e 65 20 62 79 20 this is done by
2f0c0 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 calling .** sqli
2f0d0 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 te3PagerWrite()
2f0e0 6f 6e 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 on page 1, then
2f0f0 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 63 6f modifying the co
2f100 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a ntents of the.**
2f110 20 70 61 67 65 20 64 61 74 61 2e 20 49 6e 20 74 page data. In t
2f120 68 69 73 20 63 61 73 65 20 74 68 65 20 66 69 6c his case the fil
2f130 65 20 77 69 6c 6c 20 62 65 20 75 70 64 61 74 65 e will be update
2f140 64 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 d when the curre
2f150 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f nt.** transactio
2f160 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a n is committed..
2f170 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 44 69 72 65 **.** The isDire
2f180 63 74 4d 6f 64 65 20 66 6c 61 67 20 6d 61 79 20 ctMode flag may
2f190 6f 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f only be non-zero
2f1a0 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 if the library
2f1b0 77 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 was compiled.**
2f1c0 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f with the SQLITE_
2f1d0 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 ENABLE_ATOMIC_WR
2f1e0 49 54 45 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 ITE macro define
2f1f0 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c d. In this case,
2f200 0a 2a 2a 20 69 66 20 69 73 44 69 72 65 63 74 20 .** if isDirect
2f210 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
2f220 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
2f230 69 6c 65 20 69 73 20 75 70 64 61 74 65 64 20 64 ile is updated d
2f240 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 77 72 irectly.** by wr
2f250 69 74 69 6e 67 20 61 6e 20 75 70 64 61 74 65 64 iting an updated
2f260 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 67 65 version of page
2f270 20 31 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20 1 using a call
2f280 74 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 to the .** sqlit
2f290 65 33 4f 73 57 72 69 74 65 28 29 20 66 75 6e 63 e3OsWrite() func
2f2a0 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
2f2b0 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 int pager_incr_c
2f2c0 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61 67 hangecounter(Pag
2f2d0 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
2f2e0 69 73 44 69 72 65 63 74 4d 6f 64 65 29 7b 0a 20 isDirectMode){.
2f2f0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
2f300 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 44 65 63 6c 61 _OK;.. /* Decla
2f310 72 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a re and initializ
2f320 65 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 e constant integ
2f330 65 72 20 27 69 73 44 69 72 65 63 74 27 2e 20 49 er 'isDirect'. I
2f340 66 20 74 68 65 0a 20 20 2a 2a 20 61 74 6f 6d 69 f the. ** atomi
2f350 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 c-write optimiza
2f360 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 20 tion is enabled
2f370 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2c 20 74 in this build, t
2f380 68 65 6e 20 69 73 44 69 72 65 63 74 0a 20 20 2a hen isDirect. *
2f390 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 * is initialized
2f3a0 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70 61 to the value pa
2f3b0 73 73 65 64 20 61 73 20 74 68 65 20 69 73 44 69 ssed as the isDi
2f3c0 72 65 63 74 4d 6f 64 65 20 70 61 72 61 6d 65 74 rectMode paramet
2f3d0 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 69 73 20 er. ** to this
2f3e0 66 75 6e 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 function. Otherw
2f3f0 69 73 65 2c 20 69 74 20 69 73 20 61 6c 77 61 79 ise, it is alway
2f400 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 20 s set to zero..
2f410 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 69 64 65 **. ** The ide
2f420 61 20 69 73 20 74 68 61 74 20 69 66 20 74 68 65 a is that if the
2f430 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 atomic-write op
2f440 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 6e 6f timization is no
2f450 74 0a 20 20 2a 2a 20 65 6e 61 62 6c 65 64 20 61 t. ** enabled a
2f460 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 t compile time,
2f470 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 61 6e the compiler can
2f480 20 6f 6d 69 74 20 74 68 65 20 74 65 73 74 73 20 omit the tests
2f490 6f 66 0a 20 20 2a 2a 20 27 69 73 44 69 72 65 63 of. ** 'isDirec
2f4a0 74 27 20 62 65 6c 6f 77 2c 20 61 73 20 77 65 6c t' below, as wel
2f4b0 6c 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 20 65 l as the block e
2f4c0 6e 63 6c 6f 73 65 64 20 69 6e 20 74 68 65 0a 20 nclosed in the.
2f4d0 20 2a 2a 20 22 69 66 28 20 69 73 44 69 72 65 63 ** "if( isDirec
2f4e0 74 20 29 22 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a t )" condition..
2f4f0 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c */.#ifndef SQL
2f500 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 ITE_ENABLE_ATOMI
2f510 43 5f 57 52 49 54 45 0a 23 20 64 65 66 69 6e 65 C_WRITE.# define
2f520 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 30 0a 20 DIRECT_MODE 0.
2f530 20 61 73 73 65 72 74 28 20 69 73 44 69 72 65 63 assert( isDirec
2f540 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a 20 20 55 4e tMode==0 );. UN
2f550 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 USED_PARAMETER(i
2f560 73 44 69 72 65 63 74 4d 6f 64 65 29 3b 0a 23 65 sDirectMode);.#e
2f570 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 44 49 52 lse.# define DIR
2f580 45 43 54 5f 4d 4f 44 45 20 69 73 44 69 72 65 63 ECT_MODE isDirec
2f590 74 4d 6f 64 65 0a 23 65 6e 64 69 66 0a 0a 20 20 tMode.#endif..
2f5a0 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
2f5b0 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 state>=PAGER_RES
2f5c0 45 52 56 45 44 20 29 3b 0a 20 20 69 66 28 20 21 ERVED );. if( !
2f5d0 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f pPager->changeCo
2f5e0 75 6e 74 44 6f 6e 65 20 26 26 20 70 50 61 67 65 untDone && pPage
2f5f0 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29 7b 0a 20 r->dbSize>0 ){.
2f600 20 20 20 50 67 48 64 72 20 2a 70 50 67 48 64 72 PgHdr *pPgHdr
2f610 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
2f620 20 2f 2a 20 52 65 66 65 72 65 6e 63 65 20 74 6f /* Reference to
2f630 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 20 20 75 page 1 */. u
2f640 33 32 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 32 change_counte
2f650 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 r; /*
2f660 49 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 6f 66 Initial value of
2f670 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 change-counter
2f680 66 69 65 6c 64 20 2a 2f 0a 0a 20 20 20 20 61 73 field */.. as
2f690 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74 sert( !pPager->t
2f6a0 65 6d 70 46 69 6c 65 20 26 26 20 69 73 4f 70 65 empFile && isOpe
2f6b0 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b n(pPager->fd) );
2f6c0 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 .. /* Open pa
2f6d0 67 65 20 31 20 6f 66 20 74 68 65 20 66 69 6c 65 ge 1 of the file
2f6e0 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 2a 2f for writing. */
2f6f0 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
2f700 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 3PagerGet(pPager
2f710 2c 20 31 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 , 1, &pPgHdr);.
2f720 20 20 20 61 73 73 65 72 74 28 20 70 50 67 48 64 assert( pPgHd
2f730 72 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 r==0 || rc==SQLI
2f740 54 45 5f 4f 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a TE_OK );.. /*
2f750 20 49 66 20 70 61 67 65 20 6f 6e 65 20 77 61 73 If page one was
2f760 20 66 65 74 63 68 65 64 20 73 75 63 63 65 73 73 fetched success
2f770 66 75 6c 6c 79 2c 20 61 6e 64 20 74 68 69 73 20 fully, and this
2f780 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 0a function is not.
2f790 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 ** operating
2f7a0 20 69 6e 20 64 69 72 65 63 74 2d 6d 6f 64 65 2c in direct-mode,
2f7b0 20 6d 61 6b 65 20 70 61 67 65 20 31 20 77 72 69 make page 1 wri
2f7c0 74 61 62 6c 65 2e 20 20 57 68 65 6e 20 6e 6f 74 table. When not
2f7d0 20 69 6e 20 0a 20 20 20 20 2a 2a 20 64 69 72 65 in . ** dire
2f7e0 63 74 20 6d 6f 64 65 2c 20 70 61 67 65 20 31 20 ct mode, page 1
2f7f0 69 73 20 61 6c 77 61 79 73 20 68 65 6c 64 20 69 is always held i
2f800 6e 20 63 61 63 68 65 20 61 6e 64 20 68 65 6e 63 n cache and henc
2f810 65 20 74 68 65 20 50 61 67 65 72 47 65 74 28 29 e the PagerGet()
2f820 0a 20 20 20 20 2a 2a 20 61 62 6f 76 65 20 69 73 . ** above is
2f830 20 61 6c 77 61 79 73 20 73 75 63 63 65 73 73 66 always successf
2f840 75 6c 20 2d 20 68 65 6e 63 65 20 74 68 65 20 41 ul - hence the A
2f850 4c 57 41 59 53 20 6f 6e 20 72 63 3d 3d 53 51 4c LWAYS on rc==SQL
2f860 49 54 45 5f 4f 4b 2e 0a 20 20 20 20 2a 2f 0a 20 ITE_OK.. */.
2f870 20 20 20 69 66 28 20 21 44 49 52 45 43 54 5f 4d if( !DIRECT_M
2f880 4f 44 45 20 26 26 20 41 4c 57 41 59 53 28 72 63 ODE && ALWAYS(rc
2f890 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a ==SQLITE_OK) ){.
2f8a0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
2f8b0 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67 e3PagerWrite(pPg
2f8c0 48 64 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 Hdr);. }..
2f8d0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
2f8e0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 OK ){. /* I
2f8f0 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61 6c ncrement the val
2f900 75 65 20 6a 75 73 74 20 72 65 61 64 20 61 6e 64 ue just read and
2f910 20 77 72 69 74 65 20 69 74 20 62 61 63 6b 20 74 write it back t
2f920 6f 20 62 79 74 65 20 32 34 2e 20 2a 2f 0a 20 20 o byte 24. */.
2f930 20 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 change_count
2f940 65 72 20 3d 20 73 71 6c 69 74 65 33 47 65 74 34 er = sqlite3Get4
2f950 62 79 74 65 28 28 75 38 2a 29 70 50 61 67 65 72 byte((u8*)pPager
2f960 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 3b 0a 20 ->dbFileVers);.
2f970 20 20 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e change_coun
2f980 74 65 72 2b 2b 3b 0a 20 20 20 20 20 20 70 75 74 ter++;. put
2f990 33 32 62 69 74 73 28 28 28 63 68 61 72 2a 29 70 32bits(((char*)p
2f9a0 50 67 48 64 72 2d 3e 70 44 61 74 61 29 2b 32 34 PgHdr->pData)+24
2f9b0 2c 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 , change_counter
2f9c0 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 41 6c 73 );.. /* Als
2f9d0 6f 20 73 74 6f 72 65 20 74 68 65 20 53 51 4c 69 o store the SQLi
2f9e0 74 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 te version numbe
2f9f0 72 20 69 6e 20 62 79 74 65 73 20 39 36 2e 2e 39 r in bytes 96..9
2fa00 39 20 61 6e 64 20 69 6e 0a 20 20 20 20 20 20 2a 9 and in. *
2fa10 2a 20 62 79 74 65 73 20 39 32 2e 2e 39 35 20 73 * bytes 92..95 s
2fa20 74 6f 72 65 20 74 68 65 20 63 68 61 6e 67 65 20 tore the change
2fa30 63 6f 75 6e 74 65 72 20 66 6f 72 20 77 68 69 63 counter for whic
2fa40 68 20 74 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 h the version nu
2fa50 6d 62 65 72 0a 20 20 20 20 20 20 2a 2a 20 69 73 mber. ** is
2fa60 20 76 61 6c 69 64 2e 20 2a 2f 0a 20 20 20 20 20 valid. */.
2fa70 20 70 75 74 33 32 62 69 74 73 28 28 28 63 68 61 put32bits(((cha
2fa80 72 2a 29 70 50 67 48 64 72 2d 3e 70 44 61 74 61 r*)pPgHdr->pData
2fa90 29 2b 39 32 2c 20 63 68 61 6e 67 65 5f 63 6f 75 )+92, change_cou
2faa0 6e 74 65 72 29 3b 0a 20 20 20 20 20 20 70 75 74 nter);. put
2fab0 33 32 62 69 74 73 28 28 28 63 68 61 72 2a 29 70 32bits(((char*)p
2fac0 50 67 48 64 72 2d 3e 70 44 61 74 61 29 2b 39 36 PgHdr->pData)+96
2fad0 2c 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e , SQLITE_VERSION
2fae0 5f 4e 55 4d 42 45 52 29 3b 0a 0a 20 20 20 20 20 _NUMBER);..
2faf0 20 2f 2a 20 49 66 20 72 75 6e 6e 69 6e 67 20 69 /* If running i
2fb00 6e 20 64 69 72 65 63 74 20 6d 6f 64 65 2c 20 77 n direct mode, w
2fb10 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 rite the content
2fb20 73 20 6f 66 20 70 61 67 65 20 31 20 74 6f 20 74 s of page 1 to t
2fb30 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 he file. */.
2fb40 20 20 69 66 28 20 44 49 52 45 43 54 5f 4d 4f 44 if( DIRECT_MOD
2fb50 45 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e E ){. con
2fb60 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 20 3d 20 st void *zBuf =
2fb70 70 50 67 48 64 72 2d 3e 70 44 61 74 61 3b 0a 20 pPgHdr->pData;.
2fb80 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
2fb90 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a Pager->dbFileSiz
2fba0 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 e>0 );. r
2fbb0 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
2fbc0 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 7a te(pPager->fd, z
2fbd0 42 75 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 Buf, pPager->pag
2fbe0 65 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20 eSize, 0);.
2fbf0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
2fc00 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
2fc10 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 pPager->change
2fc20 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 CountDone = 1;.
2fc30 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
2fc40 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 50 else{. pP
2fc50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e ager->changeCoun
2fc60 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20 20 tDone = 1;.
2fc70 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a }. }.. /*
2fc80 20 52 65 6c 65 61 73 65 20 74 68 65 20 70 61 67 Release the pag
2fc90 65 20 72 65 66 65 72 65 6e 63 65 2e 20 2a 2f 0a e reference. */.
2fca0 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
2fcb0 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0a 20 Unref(pPgHdr);.
2fcc0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
2fcd0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 }../*.** Sync th
2fce0 65 20 70 61 67 65 72 20 66 69 6c 65 20 74 6f 20 e pager file to
2fcf0 64 69 73 6b 2e 20 54 68 69 73 20 69 73 20 61 20 disk. This is a
2fd00 6e 6f 2d 6f 70 20 66 6f 72 20 69 6e 2d 6d 65 6d no-op for in-mem
2fd10 6f 72 79 20 66 69 6c 65 73 0a 2a 2a 20 6f 72 20 ory files.** or
2fd20 70 61 67 65 73 20 77 69 74 68 20 74 68 65 20 50 pages with the P
2fd30 61 67 65 72 2e 6e 6f 53 79 6e 63 20 66 6c 61 67 ager.noSync flag
2fd40 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 set..**.** If s
2fd50 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 63 61 uccessful, or ca
2fd60 6c 6c 65 64 20 6f 6e 20 61 20 70 61 67 65 72 20 lled on a pager
2fd70 66 6f 72 20 77 68 69 63 68 20 69 74 20 69 73 20 for which it is
2fd80 61 20 6e 6f 2d 6f 70 2c 20 74 68 69 73 0a 2a 2a a no-op, this.**
2fd90 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
2fda0 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 s SQLITE_OK. Oth
2fdb0 65 72 77 69 73 65 2c 20 61 6e 20 49 4f 20 65 72 erwise, an IO er
2fdc0 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
2fdd0 72 6e 65 64 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c rned..*/.int sql
2fde0 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 50 61 ite3PagerSync(Pa
2fdf0 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
2fe00 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
2fe10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2fe20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
2fe30 6f 64 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ode */. assert(
2fe40 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 69 66 28 !MEMDB );. if(
2fe50 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 pPager->noSync
2fe60 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
2fe70 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a TE_OK;. }else{.
2fe80 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
2fe90 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 OsSync(pPager->f
2fea0 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f d, pPager->sync_
2feb0 66 6c 61 67 73 29 3b 0a 20 20 7d 0a 20 20 72 65 flags);. }. re
2fec0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
2fed0 2a 20 53 79 6e 63 20 74 68 65 20 64 61 74 61 62 * Sync the datab
2fee0 61 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65 ase file for the
2fef0 20 70 61 67 65 72 20 70 50 61 67 65 72 2e 20 7a pager pPager. z
2ff00 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f Master points to
2ff10 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 the name.** of
2ff20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
2ff30 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c file that shoul
2ff40 64 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 d be written int
2ff50 6f 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c o the individual
2ff60 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 .** journal file
2ff70 2e 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 62 65 . zMaster may be
2ff80 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 20 NULL, which is
2ff90 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 6e interpreted as n
2ffa0 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 o master.** jour
2ffb0 6e 61 6c 20 28 61 20 73 69 6e 67 6c 65 20 64 61 nal (a single da
2ffc0 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 tabase transacti
2ffd0 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 on)..**.** This
2ffe0 72 6f 75 74 69 6e 65 20 65 6e 73 75 72 65 73 20 routine ensures
2fff0 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 that:.**.** *
30000 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c The database fil
30010 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
30020 20 69 73 20 75 70 64 61 74 65 64 2c 0a 2a 2a 20 is updated,.**
30030 20 20 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 * the journal
30040 69 73 20 73 79 6e 63 65 64 20 28 75 6e 6c 65 73 is synced (unles
30050 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 s the atomic-wri
30060 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 te optimization
30070 69 73 20 75 73 65 64 29 2c 0a 2a 2a 20 20 20 2a is used),.** *
30080 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 all dirty pages
30090 20 61 72 65 20 77 72 69 74 74 65 6e 20 74 6f 20 are written to
300a0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
300b0 65 2c 20 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 e, .** * the d
300c0 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 atabase file is
300d0 74 72 75 6e 63 61 74 65 64 20 28 69 66 20 72 65 truncated (if re
300e0 71 75 69 72 65 64 29 2c 20 61 6e 64 0a 2a 2a 20 quired), and.**
300f0 20 20 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 * the database
30100 20 66 69 6c 65 20 73 79 6e 63 65 64 2e 20 0a 2a file synced. .*
30110 2a 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 74 68 *.** The only th
30120 69 6e 67 20 74 68 61 74 20 72 65 6d 61 69 6e 73 ing that remains
30130 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 74 to commit the t
30140 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 74 6f ransaction is to
30150 20 66 69 6e 61 6c 69 7a 65 20 0a 2a 2a 20 28 64 finalize .** (d
30160 65 6c 65 74 65 2c 20 74 72 75 6e 63 61 74 65 20 elete, truncate
30170 6f 72 20 7a 65 72 6f 20 74 68 65 20 66 69 72 73 or zero the firs
30180 74 20 70 61 72 74 20 6f 66 29 20 74 68 65 20 6a t part of) the j
30190 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20 ournal file (or
301a0 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 6d .** delete the m
301b0 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
301c0 6c 65 20 69 66 20 73 70 65 63 69 66 69 65 64 29 le if specified)
301d0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
301e0 74 20 69 66 20 7a 4d 61 73 74 65 72 3d 3d 4e 55 t if zMaster==NU
301f0 4c 4c 2c 20 74 68 69 73 20 64 6f 65 73 20 6e 6f LL, this does no
30200 74 20 6f 76 65 72 77 72 69 74 65 20 61 20 70 72 t overwrite a pr
30210 65 76 69 6f 75 73 20 76 61 6c 75 65 0a 2a 2a 20 evious value.**
30220 70 61 73 73 65 64 20 74 6f 20 61 6e 20 73 71 6c passed to an sql
30230 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 ite3PagerCommitP
30240 68 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 2e 0a haseOne() call..
30250 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6e **.** If the fin
30260 61 6c 20 70 61 72 61 6d 65 74 65 72 20 2d 20 6e al parameter - n
30270 6f 53 79 6e 63 20 2d 20 69 73 20 74 72 75 65 2c oSync - is true,
30280 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 then the databa
30290 73 65 20 66 69 6c 65 20 69 74 73 65 6c 66 0a 2a se file itself.*
302a0 2a 20 69 73 20 6e 6f 74 20 73 79 6e 63 65 64 2e * is not synced.
302b0 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 The caller must
302c0 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 call sqlite3Pag
302d0 65 72 53 79 6e 63 28 29 20 64 69 72 65 63 74 6c erSync() directl
302e0 79 20 74 6f 0a 2a 2a 20 73 79 6e 63 20 74 68 65 y to.** sync the
302f0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 database file b
30300 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 43 6f efore calling Co
30310 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 74 mmitPhaseTwo() t
30320 6f 20 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20 o delete the.**
30330 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 journal file in
30340 74 68 69 73 20 63 61 73 65 2e 0a 2a 2f 0a 69 6e this case..*/.in
30350 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f t sqlite3PagerCo
30360 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 0a 20 20 mmitPhaseOne(.
30370 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 Pager *pPager,
30380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
30390 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 /* Pager object
303a0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
303b0 2a 7a 4d 61 73 74 65 72 2c 20 20 20 20 20 20 20 *zMaster,
303c0 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 4e /* If not N
303d0 55 4c 4c 2c 20 74 68 65 20 6d 61 73 74 65 72 20 ULL, the master
303e0 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a journal name */.
303f0 20 20 69 6e 74 20 6e 6f 53 79 6e 63 20 20 20 20 int noSync
30400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
30410 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 /* True to omi
30420 74 20 74 68 65 20 78 53 79 6e 63 20 6f 6e 20 74 t the xSync on t
30430 68 65 20 64 62 20 66 69 6c 65 20 2a 2f 0a 29 7b he db file */.){
30440 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
30450 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 TE_OK;
30460 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
30470 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 64 e */.. /* The d
30480 62 4f 72 69 67 53 69 7a 65 20 69 73 20 6e 65 76 bOrigSize is nev
30490 65 72 20 73 65 74 20 69 66 20 6a 6f 75 72 6e 61 er set if journa
304a0 6c 5f 6d 6f 64 65 3d 4f 46 46 20 2a 2f 0a 20 20 l_mode=OFF */.
304b0 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
304c0 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 journalMode!=PAG
304d0 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f ER_JOURNALMODE_O
304e0 46 46 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 FF || pPager->db
304f0 4f 72 69 67 53 69 7a 65 3d 3d 30 20 29 3b 0a 0a OrigSize==0 );..
30500 20 20 2f 2a 20 49 66 20 61 20 70 72 69 6f 72 20 /* If a prior
30510 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 error occurred,
30520 72 65 70 6f 72 74 20 74 68 61 74 20 65 72 72 6f report that erro
30530 72 20 61 67 61 69 6e 2e 20 2a 2f 0a 20 20 69 66 r again. */. if
30540 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e ( NEVER(pPager->
30550 65 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 72 errCode) ) retur
30560 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 n pPager->errCod
30570 65 3b 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 e;.. PAGERTRACE
30580 28 28 22 44 41 54 41 42 41 53 45 20 53 59 4e 43 (("DATABASE SYNC
30590 3a 20 46 69 6c 65 3d 25 73 20 7a 4d 61 73 74 65 : File=%s zMaste
305a0 72 3d 25 73 20 6e 53 69 7a 65 3d 25 64 5c 6e 22 r=%s nSize=%d\n"
305b0 2c 20 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d , . pPager-
305c0 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a 4d 61 73 >zFilename, zMas
305d0 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 64 62 53 ter, pPager->dbS
305e0 69 7a 65 29 29 3b 0a 0a 20 20 69 66 28 20 4d 45 ize));.. if( ME
305f0 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 64 MDB && pPager->d
30600 62 4d 6f 64 69 66 69 65 64 20 29 7b 0a 20 20 20 bModified ){.
30610 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 /* If this is a
30620 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 62 2c 20 n in-memory db,
30630 6f 72 20 6e 6f 20 70 61 67 65 73 20 68 61 76 65 or no pages have
30640 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f been written to
30650 2c 20 6f 72 20 74 68 69 73 0a 20 20 20 20 2a 2a , or this. **
30660 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 61 6c function has al
30670 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 ready been calle
30680 64 2c 20 69 74 20 69 73 20 6d 6f 73 74 6c 79 20 d, it is mostly
30690 61 20 6e 6f 2d 6f 70 2e 20 20 48 6f 77 65 76 65 a no-op. Howeve
306a0 72 2c 20 61 6e 79 0a 20 20 20 20 2a 2a 20 62 61 r, any. ** ba
306b0 63 6b 75 70 20 69 6e 20 70 72 6f 67 72 65 73 73 ckup in progress
306c0 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 73 needs to be res
306d0 74 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 tarted.. */.
306e0 20 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 sqlite3Backup
306f0 52 65 73 74 61 72 74 28 70 50 61 67 65 72 2d 3e Restart(pPager->
30700 70 42 61 63 6b 75 70 29 3b 0a 20 20 7d 65 6c 73 pBackup);. }els
30710 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 e if( pPager->st
30720 61 74 65 21 3d 50 41 47 45 52 5f 53 59 4e 43 45 ate!=PAGER_SYNCE
30730 44 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62 4d D && pPager->dbM
30740 6f 64 69 66 69 65 64 20 29 7b 0a 20 20 20 20 69 odified ){. i
30750 66 28 20 70 61 67 65 72 55 73 65 57 61 6c 28 70 f( pagerUseWal(p
30760 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 Pager) ){.
30770 50 67 48 64 72 20 2a 70 4c 69 73 74 20 3d 20 73 PgHdr *pList = s
30780 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72 74 qlite3PcacheDirt
30790 79 4c 69 73 74 28 70 50 61 67 65 72 2d 3e 70 50 yList(pPager->pP
307a0 43 61 63 68 65 29 3b 0a 20 20 20 20 20 20 69 66 Cache);. if
307b0 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 ( pList ){.
307c0 20 20 20 72 63 20 3d 20 70 61 67 65 72 57 61 6c rc = pagerWal
307d0 46 72 61 6d 65 73 28 70 50 61 67 65 72 2c 20 70 Frames(pPager, p
307e0 4c 69 73 74 2c 20 70 50 61 67 65 72 2d 3e 64 62 List, pPager->db
307f0 53 69 7a 65 2c 20 31 2c 20 0a 20 20 20 20 20 20 Size, 1, .
30800 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 66 (pPager->f
30810 75 6c 6c 53 79 6e 63 20 3f 20 70 50 61 67 65 72 ullSync ? pPager
30820 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3a 20 30 ->sync_flags : 0
30830 29 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 ). );.
30840 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 }. if( r
30850 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
30860 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 sqlite3P
30870 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 cacheCleanAll(pP
30880 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a ager->pPCache);.
30890 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
308a0 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 e{. /* The
308b0 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 following block
308c0 75 70 64 61 74 65 73 20 74 68 65 20 63 68 61 6e updates the chan
308d0 67 65 2d 63 6f 75 6e 74 65 72 2e 20 45 78 61 63 ge-counter. Exac
308e0 74 6c 79 20 68 6f 77 20 69 74 0a 20 20 20 20 20 tly how it.
308f0 20 2a 2a 20 64 6f 65 73 20 74 68 69 73 20 64 65 ** does this de
30900 70 65 6e 64 73 20 6f 6e 20 77 68 65 74 68 65 72 pends on whether
30910 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74 6f 6d or not the atom
30920 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69 6d 69 ic-update optimi
30930 7a 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 zation. **
30940 77 61 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63 was enabled at c
30950 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 61 6e 64 ompile time, and
30960 20 69 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 if this transac
30970 74 69 6f 6e 20 6d 65 65 74 73 20 74 68 65 20 0a tion meets the .
30980 20 20 20 20 20 20 2a 2a 20 72 75 6e 74 69 6d 65 ** runtime
30990 20 63 72 69 74 65 72 69 61 20 74 6f 20 75 73 65 criteria to use
309a0 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 3a 20 the operation:
309b0 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
309c0 2a 2a 20 20 20 20 2a 20 54 68 65 20 66 69 6c 65 ** * The file
309d0 2d 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 -system supports
309e0 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 the atomic-writ
309f0 65 20 70 72 6f 70 65 72 74 79 20 66 6f 72 0a 20 e property for.
30a00 20 20 20 20 20 2a 2a 20 20 20 20 20 20 62 6c 6f ** blo
30a10 63 6b 73 20 6f 66 20 73 69 7a 65 20 70 61 67 65 cks of size page
30a20 2d 73 69 7a 65 2c 20 61 6e 64 20 0a 20 20 20 20 -size, and .
30a30 20 20 2a 2a 20 20 20 20 2a 20 54 68 69 73 20 63 ** * This c
30a40 6f 6d 6d 69 74 20 69 73 20 6e 6f 74 20 70 61 72 ommit is not par
30a50 74 20 6f 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c t of a multi-fil
30a60 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 e transaction, a
30a70 6e 64 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 2a nd. ** *
30a80 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 70 61 67 Exactly one pag
30a90 65 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 e has been modif
30aa0 69 65 64 20 61 6e 64 20 73 74 6f 72 65 20 69 6e ied and store in
30ab0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
30ac0 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 e.. **.
30ad0 20 20 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 ** If the opti
30ae0 6d 69 7a 61 74 69 6f 6e 20 77 61 73 20 6e 6f 74 mization was not
30af0 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 enabled at comp
30b00 69 6c 65 20 74 69 6d 65 2c 20 74 68 65 6e 20 74 ile time, then t
30b10 68 65 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 he. ** page
30b20 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 r_incr_changecou
30b30 6e 74 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 nter() function
30b40 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 75 70 64 is called to upd
30b50 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 0a 20 ate the change.
30b60 20 20 20 20 20 2a 2a 20 63 6f 75 6e 74 65 72 20 ** counter
30b70 69 6e 20 27 69 6e 64 69 72 65 63 74 2d 6d 6f 64 in 'indirect-mod
30b80 65 27 2e 20 49 66 20 74 68 65 20 6f 70 74 69 6d e'. If the optim
30b90 69 7a 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 69 ization is compi
30ba0 6c 65 64 20 69 6e 20 62 75 74 0a 20 20 20 20 20 led in but.
30bb0 20 2a 2a 20 69 73 20 6e 6f 74 20 61 70 70 6c 69 ** is not appli
30bc0 63 61 62 6c 65 20 74 6f 20 74 68 69 73 20 74 72 cable to this tr
30bd0 61 6e 73 61 63 74 69 6f 6e 2c 20 63 61 6c 6c 20 ansaction, call
30be0 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 sqlite3JournalCr
30bf0 65 61 74 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 eate(). **
30c00 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 to make sure the
30c10 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 journal file ha
30c20 73 20 61 63 74 75 61 6c 6c 79 20 62 65 65 6e 20 s actually been
30c30 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20 63 61 created, then ca
30c40 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 ll. ** page
30c50 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 r_incr_changecou
30c60 6e 74 65 72 28 29 20 74 6f 20 75 70 64 61 74 65 nter() to update
30c70 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e the change-coun
30c80 74 65 72 20 69 6e 20 69 6e 64 69 72 65 63 74 0a ter in indirect.
30c90 20 20 20 20 20 20 2a 2a 20 6d 6f 64 65 2e 20 0a ** mode. .
30ca0 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
30cb0 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 * Otherwise, if
30cc0 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e the optimization
30cd0 20 69 73 20 62 6f 74 68 20 65 6e 61 62 6c 65 64 is both enabled
30ce0 20 61 6e 64 20 61 70 70 6c 69 63 61 62 6c 65 2c and applicable,
30cf0 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 . ** then c
30d00 61 6c 6c 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 all pager_incr_c
30d10 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74 hangecounter() t
30d20 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 o update the cha
30d30 6e 67 65 2d 63 6f 75 6e 74 65 72 0a 20 20 20 20 nge-counter.
30d40 20 20 2a 2a 20 69 6e 20 27 64 69 72 65 63 74 27 ** in 'direct'
30d50 20 6d 6f 64 65 2e 20 49 6e 20 74 68 69 73 20 63 mode. In this c
30d60 61 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ase the journal
30d70 66 69 6c 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 file will never
30d80 62 65 0a 20 20 20 20 20 20 2a 2a 20 63 72 65 61 be. ** crea
30d90 74 65 64 20 66 6f 72 20 74 68 69 73 20 74 72 61 ted for this tra
30da0 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 nsaction..
30db0 2a 2f 0a 20 20 23 69 66 64 65 66 20 53 51 4c 49 */. #ifdef SQLI
30dc0 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 TE_ENABLE_ATOMIC
30dd0 5f 57 52 49 54 45 0a 20 20 20 20 20 20 50 67 48 _WRITE. PgH
30de0 64 72 20 2a 70 50 67 3b 0a 20 20 20 20 20 20 61 dr *pPg;. a
30df0 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
30e00 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 ager->jfd) || pP
30e10 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
30e20 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
30e30 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20 20 20 MODE_OFF );.
30e40 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 26 if( !zMaster &
30e50 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d & isOpen(pPager-
30e60 3e 6a 66 64 29 20 0a 20 20 20 20 20 20 20 26 26 >jfd) . &&
30e70 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
30e80 4f 66 66 3d 3d 6a 72 6e 6c 42 75 66 66 65 72 53 Off==jrnlBufferS
30e90 69 7a 65 28 70 50 61 67 65 72 29 20 0a 20 20 20 ize(pPager) .
30ea0 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e 64 && pPager->d
30eb0 62 53 69 7a 65 3e 3d 70 50 61 67 65 72 2d 3e 64 bSize>=pPager->d
30ec0 62 46 69 6c 65 53 69 7a 65 0a 20 20 20 20 20 20 bFileSize.
30ed0 20 26 26 20 28 30 3d 3d 28 70 50 67 20 3d 20 73 && (0==(pPg = s
30ee0 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72 74 qlite3PcacheDirt
30ef0 79 4c 69 73 74 28 70 50 61 67 65 72 2d 3e 70 50 yList(pPager->pP
30f00 43 61 63 68 65 29 29 20 7c 7c 20 30 3d 3d 70 50 Cache)) || 0==pP
30f10 67 2d 3e 70 44 69 72 74 79 29 0a 20 20 20 20 20 g->pDirty).
30f20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 55 ){. /* U
30f30 70 64 61 74 65 20 74 68 65 20 64 62 20 66 69 6c pdate the db fil
30f40 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 e change counter
30f50 20 76 69 61 20 74 68 65 20 64 69 72 65 63 74 2d via the direct-
30f60 77 72 69 74 65 20 6d 65 74 68 6f 64 2e 20 54 68 write method. Th
30f70 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 6f e . ** fo
30f80 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 77 69 6c llowing call wil
30f90 6c 20 6d 6f 64 69 66 79 20 74 68 65 20 69 6e 2d l modify the in-
30fa0 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 memory represent
30fb0 61 74 69 6f 6e 20 6f 66 20 70 61 67 65 20 31 20 ation of page 1
30fc0 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 69 . ** to i
30fd0 6e 63 6c 75 64 65 20 74 68 65 20 75 70 64 61 74 nclude the updat
30fe0 65 64 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 ed change counte
30ff0 72 20 61 6e 64 20 74 68 65 6e 20 77 72 69 74 65 r and then write
31000 20 70 61 67 65 20 31 20 0a 20 20 20 20 20 20 20 page 1 .
31010 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20 ** directly to
31020 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
31030 65 2e 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 e. Because of th
31040 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 0a e atomic-write .
31050 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 70 65 ** prope
31060 72 74 79 20 6f 66 20 74 68 65 20 68 6f 73 74 20 rty of the host
31070 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 69 file-system, thi
31080 73 20 69 73 20 73 61 66 65 2e 0a 20 20 20 20 20 s is safe..
31090 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 */. rc
310a0 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 = pager_incr_ch
310b0 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 angecounter(pPag
310c0 65 72 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 65 er, 1);. }e
310d0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 lse{. rc
310e0 3d 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c = sqlite3Journal
310f0 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e 6a Create(pPager->j
31100 66 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 fd);. if(
31110 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
31120 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
31130 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e pager_incr_chan
31140 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72 gecounter(pPager
31150 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a , 0);. }.
31160 20 20 20 20 20 20 7d 0a 20 20 23 65 6c 73 65 0a }. #else.
31170 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
31180 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e _incr_changecoun
31190 74 65 72 28 70 50 61 67 65 72 2c 20 30 29 3b 0a ter(pPager, 0);.
311a0 20 20 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 #endif. i
311b0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
311c0 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 ) goto commit_p
311d0 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 hase_one_exit;.
311e0 20 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 . /* If th
311f0 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 is transaction h
31200 61 73 20 6d 61 64 65 20 74 68 65 20 64 61 74 61 as made the data
31210 62 61 73 65 20 73 6d 61 6c 6c 65 72 2c 20 74 68 base smaller, th
31220 65 6e 20 61 6c 6c 20 70 61 67 65 73 0a 20 20 20 en all pages.
31230 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 69 73 63 ** being disc
31240 61 72 64 65 64 20 62 79 20 74 68 65 20 74 72 75 arded by the tru
31250 6e 63 61 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 ncation must be
31260 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a written to the j
31270 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20 ournal. **
31280 66 69 6c 65 2e 20 54 68 69 73 20 63 61 6e 20 6f file. This can o
31290 6e 6c 79 20 68 61 70 70 65 6e 20 69 6e 20 61 75 nly happen in au
312a0 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 2e 0a to-vacuum mode..
312b0 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
312c0 2a 20 42 65 66 6f 72 65 20 72 65 61 64 69 6e 67 * Before reading
312d0 20 74 68 65 20 70 61 67 65 73 20 77 69 74 68 20 the pages with
312e0 70 61 67 65 20 6e 75 6d 62 65 72 73 20 6c 61 72 page numbers lar
312f0 67 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20 ger than the .
31300 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 76 ** current v
31310 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62 alue of Pager.db
31320 53 69 7a 65 2c 20 73 65 74 20 64 62 53 69 7a 65 Size, set dbSize
31330 20 62 61 63 6b 20 74 6f 20 74 68 65 20 76 61 6c back to the val
31340 75 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74 ue. ** that
31350 20 69 74 20 74 6f 6f 6b 20 61 74 20 74 68 65 20 it took at the
31360 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 start of the tra
31370 6e 73 61 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 nsaction. Otherw
31380 69 73 65 2c 20 74 68 65 0a 20 20 20 20 20 20 2a ise, the. *
31390 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 * calls to sqlit
313a0 65 33 50 61 67 65 72 47 65 74 28 29 20 72 65 74 e3PagerGet() ret
313b0 75 72 6e 20 7a 65 72 6f 65 64 20 70 61 67 65 73 urn zeroed pages
313c0 20 69 6e 73 74 65 61 64 20 6f 66 20 0a 20 20 20 instead of .
313d0 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 64 61 ** reading da
313e0 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 ta from the data
313f0 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20 base file..
31400 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 57 68 65 **. ** Whe
31410 6e 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 3d n journal_mode==
31420 4f 46 46 20 74 68 65 20 64 62 4f 72 69 67 53 69 OFF the dbOrigSi
31430 7a 65 20 69 73 20 61 6c 77 61 79 73 20 7a 65 72 ze is always zer
31440 6f 2c 20 73 6f 20 74 68 69 73 0a 20 20 20 20 20 o, so this.
31450 20 2a 2a 20 62 6c 6f 63 6b 20 6e 65 76 65 72 20 ** block never
31460 72 75 6e 73 20 69 66 20 6a 6f 75 72 6e 61 6c 5f runs if journal_
31470 6d 6f 64 65 3d 4f 46 46 2e 0a 20 20 20 20 20 20 mode=OFF..
31480 2a 2f 0a 20 20 23 69 66 6e 64 65 66 20 53 51 4c */. #ifndef SQL
31490 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
314a0 55 55 4d 0a 20 20 20 20 20 20 69 66 28 20 70 50 UUM. if( pP
314b0 61 67 65 72 2d 3e 64 62 53 69 7a 65 3c 70 50 61 ager->dbSize<pPa
314c0 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 ger->dbOrigSize
314d0 0a 20 20 20 20 20 20 20 26 26 20 41 4c 57 41 59 . && ALWAY
314e0 53 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 S(pPager->journa
314f0 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 lMode!=PAGER_JOU
31500 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 29 0a 20 20 RNALMODE_OFF).
31510 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 50 ){. P
31520 67 6e 6f 20 69 3b 20 20 20 20 20 20 20 20 20 20 gno i;
31530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
31540 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 /* Iter
31550 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f ator variable */
31560 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 50 . const P
31570 67 6e 6f 20 69 53 6b 69 70 20 3d 20 50 41 47 45 gno iSkip = PAGE
31580 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 R_MJ_PGNO(pPager
31590 29 3b 20 2f 2a 20 50 65 6e 64 69 6e 67 20 6c 6f ); /* Pending lo
315a0 63 6b 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 ck page */.
315b0 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 64 62 const Pgno db
315c0 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 Size = pPager->d
315d0 62 53 69 7a 65 3b 20 20 20 20 20 20 20 2f 2a 20 bSize; /*
315e0 44 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 73 Database image s
315f0 69 7a 65 20 2a 2f 20 0a 20 20 20 20 20 20 20 20 ize */ .
31600 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d pPager->dbSize =
31610 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 pPager->dbOrigS
31620 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 ize;. for
31630 28 20 69 3d 64 62 53 69 7a 65 2b 31 3b 20 69 3c ( i=dbSize+1; i<
31640 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 =pPager->dbOrigS
31650 69 7a 65 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 ize; i++ ){.
31660 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 if( !sqlit
31670 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50 61 e3BitvecTest(pPa
31680 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c ger->pInJournal,
31690 20 69 29 20 26 26 20 69 21 3d 69 53 6b 69 70 20 i) && i!=iSkip
316a0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 50 ){. P
316b0 67 48 64 72 20 2a 70 50 61 67 65 3b 20 20 20 20 gHdr *pPage;
316c0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
316d0 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 to journal */.
316e0 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
316f0 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
31700 70 50 61 67 65 72 2c 20 69 2c 20 26 70 50 61 67 pPager, i, &pPag
31710 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 e);.
31720 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
31730 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f K ) goto commit_
31740 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a phase_one_exit;.
31750 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
31760 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
31770 74 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 te(pPage);.
31780 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 sqlite3Pa
31790 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b gerUnref(pPage);
317a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
317b0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
317c0 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 goto commit_pha
317d0 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 se_one_exit;.
317e0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
317f0 20 7d 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 }. pPage
31800 72 2d 3e 64 62 53 69 7a 65 20 3d 20 64 62 53 69 r->dbSize = dbSi
31810 7a 65 3b 0a 20 20 20 20 20 20 7d 20 0a 20 20 23 ze;. } . #
31820 65 6e 64 69 66 0a 20 20 0a 20 20 20 20 20 20 2f endif. . /
31830 2a 20 57 72 69 74 65 20 74 68 65 20 6d 61 73 74 * Write the mast
31840 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 er journal name
31850 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c into the journal
31860 20 66 69 6c 65 2e 20 49 66 20 61 20 6d 61 73 74 file. If a mast
31870 65 72 20 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 er . ** jou
31880 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 68 rnal file name h
31890 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
318a0 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a written to the j
318b0 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 0a 20 20 ournal file, .
318c0 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 7a 4d 61 ** or if zMa
318d0 73 74 65 72 20 69 73 20 4e 55 4c 4c 20 28 6e 6f ster is NULL (no
318e0 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 29 master journal)
318f0 2c 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c 6c , then this call
31900 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 is a no-op..
31910 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d */. rc =
31920 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 writeMasterJour
31930 6e 61 6c 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 nal(pPager, zMas
31940 74 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ter);. if(
31950 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
31960 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 goto commit_phas
31970 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 0a 20 e_one_exit;. .
31980 20 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 /* Sync the
31990 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 journal file. I
319a0 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 75 70 64 f the atomic-upd
319b0 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ate optimization
319c0 20 69 73 20 62 65 69 6e 67 0a 20 20 20 20 20 20 is being.
319d0 2a 2a 20 75 73 65 64 2c 20 74 68 69 73 20 63 61 ** used, this ca
319e0 6c 6c 20 77 69 6c 6c 20 6e 6f 74 20 63 72 65 61 ll will not crea
319f0 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 te the journal f
31a00 69 6c 65 20 6f 72 20 70 65 72 66 6f 72 6d 20 61 ile or perform a
31a10 6e 79 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 6c ny. ** real
31a20 20 49 4f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 IO.. */.
31a30 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 rc = syncJou
31a40 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 rnal(pPager);.
31a50 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
31a60 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d TE_OK ) goto com
31a70 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 mit_phase_one_ex
31a80 69 74 3b 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 it;. . /*
31a90 57 72 69 74 65 20 61 6c 6c 20 64 69 72 74 79 20 Write all dirty
31aa0 70 61 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 pages to the dat
31ab0 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 abase file. */.
31ac0 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
31ad0 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28 73 write_pagelist(s
31ae0 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72 74 qlite3PcacheDirt
31af0 79 4c 69 73 74 28 70 50 61 67 65 72 2d 3e 70 50 yList(pPager->pP
31b00 43 61 63 68 65 29 29 3b 0a 20 20 20 20 20 20 69 Cache));. i
31b10 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
31b20 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
31b30 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 rt( rc!=SQLITE_I
31b40 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b 0a OERR_BLOCKED );.
31b50 20 20 20 20 20 20 20 20 67 6f 74 6f 20 63 6f 6d goto com
31b60 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 mit_phase_one_ex
31b70 69 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 it;. }.
31b80 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 sqlite3PcacheC
31b90 6c 65 61 6e 41 6c 6c 28 70 50 61 67 65 72 2d 3e leanAll(pPager->
31ba0 70 50 43 61 63 68 65 29 3b 0a 20 20 0a 20 20 20 pPCache);. .
31bb0 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c /* If the fil
31bc0 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f 74 e on disk is not
31bd0 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65 20 61 the same size a
31be0 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 s the database i
31bf0 6d 61 67 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 mage,. ** t
31c00 68 65 6e 20 75 73 65 20 70 61 67 65 72 5f 74 72 hen use pager_tr
31c10 75 6e 63 61 74 65 20 74 6f 20 67 72 6f 77 20 6f uncate to grow o
31c20 72 20 73 68 72 69 6e 6b 20 74 68 65 20 66 69 6c r shrink the fil
31c30 65 20 68 65 72 65 2e 0a 20 20 20 20 20 20 2a 2f e here.. */
31c40 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 . if( pPage
31c50 72 2d 3e 64 62 53 69 7a 65 21 3d 70 50 61 67 65 r->dbSize!=pPage
31c60 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 7b r->dbFileSize ){
31c70 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 6e 4e . Pgno nN
31c80 65 77 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 ew = pPager->dbS
31c90 69 7a 65 20 2d 20 28 70 50 61 67 65 72 2d 3e 64 ize - (pPager->d
31ca0 62 53 69 7a 65 3d 3d 50 41 47 45 52 5f 4d 4a 5f bSize==PAGER_MJ_
31cb0 50 47 4e 4f 28 70 50 61 67 65 72 29 29 3b 0a 20 PGNO(pPager));.
31cc0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
31cd0 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
31ce0 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b GER_EXCLUSIVE );
31cf0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 . rc = pa
31d00 67 65 72 5f 74 72 75 6e 63 61 74 65 28 70 50 61 ger_truncate(pPa
31d10 67 65 72 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 ger, nNew);.
31d20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
31d30 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d TE_OK ) goto com
31d40 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 mit_phase_one_ex
31d50 69 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 it;. }. .
31d60 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c /* Finally,
31d70 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 sync the databa
31d80 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 se file. */.
31d90 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e if( !pPager->n
31da0 6f 53 79 6e 63 20 26 26 20 21 6e 6f 53 79 6e 63 oSync && !noSync
31db0 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
31dc0 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 sqlite3OsSync(p
31dd0 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 Pager->fd, pPage
31de0 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a r->sync_flags);.
31df0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 49 4f }. IO
31e00 54 52 41 43 45 28 28 22 44 42 53 59 4e 43 20 25 TRACE(("DBSYNC %
31e10 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 p\n", pPager)).
31e20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 72 }.. pPager
31e30 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f ->state = PAGER_
31e40 53 59 4e 43 45 44 3b 0a 20 20 7d 0a 0a 63 6f 6d SYNCED;. }..com
31e50 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 mit_phase_one_ex
31e60 69 74 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b it:. return rc;
31e70 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 .}.../*.** When
31e80 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
31e90 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 64 61 74 called, the dat
31ea0 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 62 abase file has b
31eb0 65 65 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a een completely.*
31ec0 2a 20 75 70 64 61 74 65 64 20 74 6f 20 72 65 66 * updated to ref
31ed0 6c 65 63 74 20 74 68 65 20 63 68 61 6e 67 65 73 lect the changes
31ee0 20 6d 61 64 65 20 62 79 20 74 68 65 20 63 75 72 made by the cur
31ef0 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e rent transaction
31f00 20 61 6e 64 0a 2a 2a 20 73 79 6e 63 65 64 20 74 and.** synced t
31f10 6f 20 64 69 73 6b 2e 20 54 68 65 20 6a 6f 75 72 o disk. The jour
31f20 6e 61 6c 20 66 69 6c 65 20 73 74 69 6c 6c 20 65 nal file still e
31f30 78 69 73 74 73 20 69 6e 20 74 68 65 20 66 69 6c xists in the fil
31f40 65 2d 73 79 73 74 65 6d 20 0a 2a 2a 20 74 68 6f e-system .** tho
31f50 75 67 68 2c 20 61 6e 64 20 69 66 20 61 20 66 61 ugh, and if a fa
31f60 69 6c 75 72 65 20 6f 63 63 75 72 73 20 61 74 20 ilure occurs at
31f70 74 68 69 73 20 70 6f 69 6e 74 20 69 74 20 77 69 this point it wi
31f80 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a ll eventually.**
31f90 20 62 65 20 75 73 65 64 20 61 73 20 61 20 68 6f be used as a ho
31fa0 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 t-journal and th
31fb0 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 e current transa
31fc0 63 74 69 6f 6e 20 72 6f 6c 6c 65 64 20 62 61 63 ction rolled bac
31fd0 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 k..**.** This fu
31fe0 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 nction finalizes
31ff0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
32000 65 2c 20 65 69 74 68 65 72 20 62 79 20 64 65 6c e, either by del
32010 65 74 69 6e 67 2c 20 0a 2a 2a 20 74 72 75 6e 63 eting, .** trunc
32020 61 74 69 6e 67 20 6f 72 20 70 61 72 74 69 61 6c ating or partial
32030 6c 79 20 7a 65 72 6f 69 6e 67 20 69 74 2c 20 73 ly zeroing it, s
32040 6f 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 o that it cannot
32050 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 66 6f 72 be used .** for
32060 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c hot-journal rol
32070 6c 62 61 63 6b 2e 20 4f 6e 63 65 20 74 68 69 73 lback. Once this
32080 20 69 73 20 64 6f 6e 65 20 74 68 65 20 74 72 61 is done the tra
32090 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 nsaction is.** i
320a0 72 72 65 76 6f 63 61 62 6c 79 20 63 6f 6d 6d 69 rrevocably commi
320b0 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 tted..**.** If a
320c0 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
320d0 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 an IO error code
320e0 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
320f0 20 74 68 65 20 70 61 67 65 72 0a 2a 2a 20 6d 6f the pager.** mo
32100 76 65 73 20 69 6e 74 6f 20 74 68 65 20 65 72 72 ves into the err
32110 6f 72 20 73 74 61 74 65 2e 20 4f 74 68 65 72 77 or state. Otherw
32120 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 ise, SQLITE_OK i
32130 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 69 s returned..*/.i
32140 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 nt sqlite3PagerC
32150 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50 61 ommitPhaseTwo(Pa
32160 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
32170 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
32180 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 OK;
32190 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
321a0 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 ode */.. /* Thi
321b0 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 s routine should
321c0 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 69 not be called i
321d0 66 20 61 20 70 72 69 6f 72 20 65 72 72 6f 72 20 f a prior error
321e0 68 61 73 20 6f 63 63 75 72 72 65 64 2e 0a 20 20 has occurred..
321f0 2a 2a 20 42 75 74 20 69 66 20 28 64 75 65 20 74 ** But if (due t
32200 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 o a coding error
32210 20 65 6c 73 65 77 68 65 72 65 20 69 6e 20 74 68 elsewhere in th
32220 65 20 73 79 73 74 65 6d 29 20 69 74 20 64 6f 65 e system) it doe
32230 73 20 67 65 74 0a 20 20 2a 2a 20 63 61 6c 6c 65 s get. ** calle
32240 64 2c 20 6a 75 73 74 20 72 65 74 75 72 6e 20 74 d, just return t
32250 68 65 20 73 61 6d 65 20 65 72 72 6f 72 20 63 6f he same error co
32260 64 65 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 de without doing
32270 20 61 6e 79 74 68 69 6e 67 2e 20 2a 2f 0a 20 20 anything. */.
32280 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 if( NEVER(pPager
32290 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 72 65 74 ->errCode) ) ret
322a0 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 urn pPager->errC
322b0 6f 64 65 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 ode;.. /* This
322c0 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 function should
322d0 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 69 66 not be called if
322e0 20 74 68 65 20 70 61 67 65 72 20 69 73 20 6e 6f the pager is no
322f0 74 20 69 6e 20 61 74 20 6c 65 61 73 74 0a 20 20 t in at least.
32300 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 ** PAGER_RESERVE
32310 44 20 73 74 61 74 65 2e 20 41 6e 64 20 69 6e 64 D state. And ind
32320 65 65 64 20 53 51 4c 69 74 65 20 6e 65 76 65 72 eed SQLite never
32330 20 64 6f 65 73 20 74 68 69 73 2e 20 42 75 74 20 does this. But
32340 69 74 20 69 73 0a 20 20 2a 2a 20 6e 69 63 65 20 it is. ** nice
32350 74 6f 20 68 61 76 65 20 74 68 69 73 20 64 65 66 to have this def
32360 65 6e 73 69 76 65 20 74 65 73 74 20 68 65 72 65 ensive test here
32370 20 61 6e 79 77 61 79 2e 0a 20 20 2a 2f 0a 20 20 anyway.. */.
32380 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 if( NEVER(pPager
32390 2d 3e 73 74 61 74 65 3c 50 41 47 45 52 5f 52 45 ->state<PAGER_RE
323a0 53 45 52 56 45 44 29 20 29 20 72 65 74 75 72 6e SERVED) ) return
323b0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 0a SQLITE_ERROR;..
323c0 20 20 2f 2a 20 41 6e 20 6f 70 74 69 6d 69 7a 61 /* An optimiza
323d0 74 69 6f 6e 2e 20 49 66 20 74 68 65 20 64 61 74 tion. If the dat
323e0 61 62 61 73 65 20 77 61 73 20 6e 6f 74 20 61 63 abase was not ac
323f0 74 75 61 6c 6c 79 20 6d 6f 64 69 66 69 65 64 20 tually modified
32400 64 75 72 69 6e 67 0a 20 20 2a 2a 20 74 68 69 73 during. ** this
32410 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 transaction, th
32420 65 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 e pager is runni
32430 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d ng in exclusive-
32440 6d 6f 64 65 20 61 6e 64 20 69 73 0a 20 20 2a 2a mode and is. **
32450 20 75 73 69 6e 67 20 70 65 72 73 69 73 74 65 6e using persisten
32460 74 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 6e t journals, then
32470 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
32480 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a s a no-op.. **.
32490 20 20 2a 2a 20 54 68 65 20 73 74 61 72 74 20 6f ** The start o
324a0 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
324b0 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e le currently con
324c0 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 6a tains a single j
324d0 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 ournal . ** hea
324e0 64 65 72 20 77 69 74 68 20 74 68 65 20 6e 52 65 der with the nRe
324f0 63 20 66 69 65 6c 64 20 73 65 74 20 74 6f 20 30 c field set to 0
32500 2e 20 49 66 20 73 75 63 68 20 61 20 6a 6f 75 72 . If such a jour
32510 6e 61 6c 20 69 73 20 75 73 65 64 20 61 73 0a 20 nal is used as.
32520 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 ** a hot-journa
32530 6c 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a 6f 75 l during hot-jou
32540 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 30 rnal rollback, 0
32550 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 62 65 changes will be
32560 20 6d 61 64 65 0a 20 20 2a 2a 20 74 6f 20 74 68 made. ** to th
32570 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
32580 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 So there is no
32590 6e 65 65 64 20 74 6f 20 7a 65 72 6f 20 74 68 65 need to zero the
325a0 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 journal . ** h
325b0 65 61 64 65 72 2e 20 53 69 6e 63 65 20 74 68 65 eader. Since the
325c0 20 70 61 67 65 72 20 69 73 20 69 6e 20 65 78 63 pager is in exc
325d0 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 lusive mode, the
325e0 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 re is no need.
325f0 2a 2a 20 74 6f 20 64 72 6f 70 20 61 6e 79 20 6c ** to drop any l
32600 6f 63 6b 73 20 65 69 74 68 65 72 2e 0a 20 20 2a ocks either.. *
32610 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e /. if( pPager->
32620 64 62 4d 6f 64 69 66 69 65 64 3d 3d 30 20 26 26 dbModified==0 &&
32630 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 pPager->exclusi
32640 76 65 4d 6f 64 65 20 0a 20 20 20 26 26 20 70 50 veMode . && pP
32650 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
32660 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
32670 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20 29 MODE_PERSIST. )
32680 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 {. assert( pP
32690 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
326a0 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a ==JOURNAL_HDR_SZ
326b0 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 (pPager) );.
326c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
326d0 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 ;. }.. PAGERTR
326e0 41 43 45 28 28 22 43 4f 4d 4d 49 54 20 25 64 5c ACE(("COMMIT %d\
326f0 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 n", PAGERID(pPag
32700 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28 er)));. assert(
32710 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d pPager->state==
32720 50 41 47 45 52 5f 53 59 4e 43 45 44 20 7c 7c 20 PAGER_SYNCED ||
32730 4d 45 4d 44 42 20 7c 7c 20 21 70 50 61 67 65 72 MEMDB || !pPager
32740 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 3b 0a ->dbModified );.
32750 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 rc = pager_end
32760 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 _transaction(pPa
32770 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 ger, pPager->set
32780 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74 75 72 Master);. retur
32790 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 n pager_error(pP
327a0 61 67 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a ager, rc);.}../*
327b0 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6c 6c .** Rollback all
327c0 20 63 68 61 6e 67 65 73 2e 20 54 68 65 20 64 61 changes. The da
327d0 74 61 62 61 73 65 20 66 61 6c 6c 73 20 62 61 63 tabase falls bac
327e0 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52 45 k to PAGER_SHARE
327f0 44 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 D mode..**.** Th
32800 69 73 20 66 75 6e 63 74 69 6f 6e 20 70 65 72 66 is function perf
32810 6f 72 6d 73 20 74 77 6f 20 74 61 73 6b 73 3a 0a orms two tasks:.
32820 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 74 20 72 6f **.** 1) It ro
32830 6c 6c 73 20 62 61 63 6b 20 74 68 65 20 6a 6f 75 lls back the jou
32840 72 6e 61 6c 20 66 69 6c 65 2c 20 72 65 73 74 6f rnal file, resto
32850 72 69 6e 67 20 61 6c 6c 20 64 61 74 61 62 61 73 ring all databas
32860 65 20 66 69 6c 65 20 61 6e 64 20 0a 2a 2a 20 20 e file and .**
32870 20 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 in-memory ca
32880 63 68 65 20 70 61 67 65 73 20 74 6f 20 74 68 65 che pages to the
32890 20 73 74 61 74 65 20 74 68 65 79 20 77 65 72 65 state they were
328a0 20 69 6e 20 77 68 65 6e 20 74 68 65 20 74 72 61 in when the tra
328b0 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 nsaction.**
328c0 20 77 61 73 20 6f 70 65 6e 65 64 2c 20 61 6e 64 was opened, and
328d0 0a 2a 2a 20 20 20 32 29 20 49 74 20 66 69 6e 61 .** 2) It fina
328e0 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e 61 lizes the journa
328f0 6c 20 66 69 6c 65 2c 20 73 6f 20 74 68 61 74 20 l file, so that
32900 69 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 66 it is not used f
32910 6f 72 20 68 6f 74 0a 2a 2a 20 20 20 20 20 20 72 or hot.** r
32920 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6e 79 20 70 ollback at any p
32930 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 oint in the futu
32940 72 65 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 65 63 re..**.** subjec
32950 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 t to the followi
32960 6e 67 20 71 75 61 6c 69 66 69 63 61 74 69 6f 6e ng qualification
32970 73 3a 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74 68 s:.**.** * If th
32980 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
32990 73 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e 20 77 s not yet open w
329a0 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
329b0 6e 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 n is called,.**
329c0 20 20 74 68 65 6e 20 6f 6e 6c 79 20 28 32 29 20 then only (2)
329d0 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 49 6e is performed. In
329e0 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 this case there
329f0 20 69 73 20 6e 6f 20 6a 6f 75 72 6e 61 6c 20 66 is no journal f
32a00 69 6c 65 0a 2a 2a 20 20 20 74 6f 20 72 6f 6c 6c ile.** to roll
32a10 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 back..**.** * I
32a20 66 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 f in an error st
32a30 61 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 ate other than S
32a40 51 4c 49 54 45 5f 46 55 4c 4c 2c 20 74 68 65 6e QLITE_FULL, then
32a50 20 74 61 73 6b 20 28 31 29 20 69 73 20 0a 2a 2a task (1) is .**
32a60 20 20 20 70 65 72 66 6f 72 6d 65 64 2e 20 49 66 performed. If
32a70 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 61 73 successful, tas
32a80 6b 20 28 32 29 2e 20 52 65 67 61 72 64 6c 65 73 k (2). Regardles
32a90 73 20 6f 66 20 74 68 65 20 6f 75 74 63 6f 6d 65 s of the outcome
32aa0 0a 2a 2a 20 20 20 6f 66 20 65 69 74 68 65 72 2c .** of either,
32ab0 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
32ac0 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
32ad0 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 eturned to the c
32ae0 61 6c 6c 65 72 0a 2a 2a 20 20 20 28 69 2e 65 2e aller.** (i.e.
32af0 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 49 either SQLITE_I
32b00 4f 45 52 52 20 6f 72 20 53 51 4c 49 54 45 5f 43 OERR or SQLITE_C
32b10 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20 2a ORRUPT)..**.** *
32b20 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 If the pager is
32b30 20 69 6e 20 50 41 47 45 52 5f 52 45 53 45 52 56 in PAGER_RESERV
32b40 45 44 20 73 74 61 74 65 2c 20 74 68 65 6e 20 61 ED state, then a
32b50 74 74 65 6d 70 74 20 28 31 29 2e 20 57 68 65 74 ttempt (1). Whet
32b60 68 65 72 0a 2a 2a 20 20 20 6f 72 20 6e 6f 74 20 her.** or not
32b70 28 31 29 20 69 73 20 73 75 63 63 75 73 73 66 75 (1) is succussfu
32b80 6c 2c 20 61 6c 73 6f 20 61 74 74 65 6d 70 74 20 l, also attempt
32b90 28 32 29 2e 20 49 66 20 73 75 63 63 65 73 73 66 (2). If successf
32ba0 75 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 ul, return.**
32bb0 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 SQLITE_OK. Other
32bc0 77 69 73 65 2c 20 65 6e 74 65 72 20 74 68 65 20 wise, enter the
32bd0 65 72 72 6f 72 20 73 74 61 74 65 20 61 6e 64 20 error state and
32be0 72 65 74 75 72 6e 20 74 68 65 20 66 69 72 73 74 return the first
32bf0 20 0a 2a 2a 20 20 20 65 72 72 6f 72 20 63 6f 64 .** error cod
32c00 65 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 20 0a e encountered. .
32c10 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69 73 20 **.** In this
32c20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f case there is no
32c30 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 chance that the
32c40 20 64 61 74 61 62 61 73 65 20 77 61 73 20 77 72 database was wr
32c50 69 74 74 65 6e 20 74 6f 2e 20 0a 2a 2a 20 20 20 itten to. .**
32c60 53 6f 20 69 73 20 73 61 66 65 20 74 6f 20 66 69 So is safe to fi
32c70 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e nalize the journ
32c80 61 6c 20 66 69 6c 65 20 65 76 65 6e 20 69 66 20 al file even if
32c90 74 68 65 20 70 6c 61 79 62 61 63 6b 20 0a 2a 2a the playback .**
32ca0 20 20 20 28 6f 70 65 72 61 74 69 6f 6e 20 31 29 (operation 1)
32cb0 20 66 61 69 6c 65 64 2e 20 48 6f 77 65 76 65 72 failed. However
32cc0 20 74 68 65 20 70 61 67 65 72 20 6d 75 73 74 20 the pager must
32cd0 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 enter the error
32ce0 73 74 61 74 65 0a 2a 2a 20 20 20 61 73 20 74 68 state.** as th
32cf0 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
32d00 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 e in-memory cach
32d10 65 20 61 72 65 20 6e 6f 77 20 73 75 73 70 65 63 e are now suspec
32d20 74 2e 0a 2a 2a 0a 2a 2a 20 2a 20 46 69 6e 61 6c t..**.** * Final
32d30 6c 79 2c 20 69 66 20 69 6e 20 50 41 47 45 52 5f ly, if in PAGER_
32d40 45 58 43 4c 55 53 49 56 45 20 73 74 61 74 65 2c EXCLUSIVE state,
32d50 20 74 68 65 6e 20 61 74 74 65 6d 70 74 20 28 31 then attempt (1
32d60 29 2e 20 4f 6e 6c 79 0a 2a 2a 20 20 20 61 74 74 ). Only.** att
32d70 65 6d 70 74 20 28 32 29 20 69 66 20 28 31 29 20 empt (2) if (1)
32d80 69 73 20 73 75 63 63 65 73 73 66 75 6c 2e 20 52 is successful. R
32d90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
32da0 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a if successful,.*
32db0 2a 20 20 20 6f 74 68 65 72 77 69 73 65 20 65 6e * otherwise en
32dc0 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 ter the error st
32dd0 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 74 ate and return t
32de0 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 66 72 he error code fr
32df0 6f 6d 20 74 68 65 20 0a 2a 2a 20 20 20 66 61 69 om the .** fai
32e00 6c 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e 0a ling operation..
32e10 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69 73 20 **.** In this
32e20 63 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73 case the databas
32e30 65 20 66 69 6c 65 20 6d 61 79 20 68 61 76 65 20 e file may have
32e40 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 2e been written to.
32e50 20 53 6f 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 So if the.**
32e60 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 74 69 playback operati
32e70 6f 6e 20 64 69 64 20 6e 6f 74 20 73 75 63 63 65 on did not succe
32e80 65 64 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74 20 ed it would not
32e90 62 65 20 73 61 66 65 20 74 6f 20 66 69 6e 61 6c be safe to final
32ea0 69 7a 65 0a 2a 2a 20 20 20 74 68 65 20 6a 6f 75 ize.** the jou
32eb0 72 6e 61 6c 20 66 69 6c 65 2e 20 49 74 20 6e 65 rnal file. It ne
32ec0 65 64 73 20 74 6f 20 62 65 20 6c 65 66 74 20 69 eds to be left i
32ed0 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 n the file-syste
32ee0 6d 20 73 6f 20 74 68 61 74 0a 2a 2a 20 20 20 73 m so that.** s
32ef0 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 ome other proces
32f00 73 20 63 61 6e 20 75 73 65 20 69 74 20 74 6f 20 s can use it to
32f10 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 61 restore the data
32f20 62 61 73 65 20 73 74 61 74 65 20 28 62 79 0a 2a base state (by.*
32f30 2a 20 20 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 * hot-journal
32f40 72 6f 6c 6c 62 61 63 6b 29 2e 0a 2a 2f 0a 69 6e rollback)..*/.in
32f50 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f t sqlite3PagerRo
32f60 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50 llback(Pager *pP
32f70 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 ager){. int rc
32f80 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
32f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
32fa0 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
32fb0 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 52 PAGERTRACE(("R
32fc0 4f 4c 4c 42 41 43 4b 20 25 64 5c 6e 22 2c 20 50 OLLBACK %d\n", P
32fd0 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 AGERID(pPager)))
32fe0 3b 0a 20 20 69 66 28 20 70 61 67 65 72 55 73 65 ;. if( pagerUse
32ff0 57 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 Wal(pPager) ){.
33000 20 20 20 69 6e 74 20 72 63 32 3b 0a 0a 20 20 20 int rc2;..
33010 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
33020 65 72 53 61 76 65 70 6f 69 6e 74 28 70 50 61 67 erSavepoint(pPag
33030 65 72 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f er, SAVEPOINT_RO
33040 4c 4c 42 41 43 4b 2c 20 2d 31 29 3b 0a 20 20 20 LLBACK, -1);.
33050 20 72 63 32 20 3d 20 70 61 67 65 72 5f 65 6e 64 rc2 = pager_end
33060 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 _transaction(pPa
33070 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 ger, pPager->set
33080 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28 Master);. if(
33090 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
330a0 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 72 rc = rc2;. r
330b0 63 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28 c = pager_error(
330c0 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 7d pPager, rc);. }
330d0 65 6c 73 65 20 69 66 28 20 21 70 50 61 67 65 72 else if( !pPager
330e0 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 7c 7c 20 ->dbModified ||
330f0 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e !isOpen(pPager->
33100 6a 66 64 29 20 29 7b 0a 20 20 20 20 72 63 20 3d jfd) ){. rc =
33110 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 pager_end_trans
33120 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 action(pPager, p
33130 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 Pager->setMaster
33140 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 );. }else if( p
33150 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 Pager->errCode &
33160 26 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 & pPager->errCod
33170 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 e!=SQLITE_FULL )
33180 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 {. if( pPager
33190 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 ->state>=PAGER_E
331a0 58 43 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20 XCLUSIVE ){.
331b0 20 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b pager_playback
331c0 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 (pPager, 0);.
331d0 20 7d 0a 20 20 20 20 72 63 20 3d 20 70 50 61 67 }. rc = pPag
331e0 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d er->errCode;. }
331f0 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 50 else{. if( pP
33200 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 ager->state==PAG
33210 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20 ER_RESERVED ){.
33220 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 int rc2;.
33230 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 rc = pager_p
33240 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 layback(pPager,
33250 30 29 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20 0);. rc2 =
33260 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 pager_end_transa
33270 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 50 ction(pPager, pP
33280 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 29 ager->setMaster)
33290 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
332a0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
332b0 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 rc = rc2;.
332c0 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
332d0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 {. rc = pag
332e0 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 er_playback(pPag
332f0 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 er, 0);. }..
33300 20 20 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b if( !MEMDB ){
33310 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 . pPager->d
33320 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a bSizeValid = 0;.
33330 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 }.. /* If
33340 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
33350 20 64 75 72 69 6e 67 20 61 20 52 4f 4c 4c 42 41 during a ROLLBA
33360 43 4b 2c 20 77 65 20 63 61 6e 20 6e 6f 20 6c 6f CK, we can no lo
33370 6e 67 65 72 20 74 72 75 73 74 20 74 68 65 20 70 nger trust the p
33380 61 67 65 72 0a 20 20 20 20 2a 2a 20 63 61 63 68 ager. ** cach
33390 65 2e 20 53 6f 20 63 61 6c 6c 20 70 61 67 65 72 e. So call pager
333a0 5f 65 72 72 6f 72 28 29 20 6f 6e 20 74 68 65 20 _error() on the
333b0 77 61 79 20 6f 75 74 20 74 6f 20 6d 61 6b 65 20 way out to make
333c0 61 6e 79 20 65 72 72 6f 72 20 0a 20 20 20 20 2a any error . *
333d0 2a 20 70 65 72 73 69 73 74 65 6e 74 2e 0a 20 20 * persistent..
333e0 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 */. rc = pa
333f0 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 ger_error(pPager
33400 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 , rc);. }. ret
33410 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
33420 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 Return TRUE if
33430 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
33440 65 20 69 73 20 6f 70 65 6e 65 64 20 72 65 61 64 e is opened read
33450 2d 6f 6e 6c 79 2e 20 20 52 65 74 75 72 6e 20 46 -only. Return F
33460 41 4c 53 45 0a 2a 2a 20 69 66 20 74 68 65 20 64 ALSE.** if the d
33470 61 74 61 62 61 73 65 20 69 73 20 28 69 6e 20 74 atabase is (in t
33480 68 65 6f 72 79 29 20 77 72 69 74 61 62 6c 65 2e heory) writable.
33490 0a 2a 2f 0a 75 38 20 73 71 6c 69 74 65 33 50 61 .*/.u8 sqlite3Pa
334a0 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 gerIsreadonly(Pa
334b0 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
334c0 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 72 return pPager->r
334d0 65 61 64 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a 0a 2a eadOnly;.}../*.*
334e0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
334f0 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 ber of reference
33500 73 20 74 6f 20 74 68 65 20 70 61 67 65 72 2e 0a s to the pager..
33510 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 61 */.int sqlite3Pa
33520 67 65 72 52 65 66 63 6f 75 6e 74 28 50 61 67 65 gerRefcount(Page
33530 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 r *pPager){. re
33540 74 75 72 6e 20 73 71 6c 69 74 65 33 50 63 61 63 turn sqlite3Pcac
33550 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 heRefCount(pPage
33560 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 7d 0a 0a r->pPCache);.}..
33570 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
33580 20 61 70 70 72 6f 78 69 6d 61 74 65 20 6e 75 6d approximate num
33590 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
335a0 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 memory currently
335b0 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 .** used by the
335c0 70 61 67 65 72 20 61 6e 64 20 69 74 73 20 61 73 pager and its as
335d0 73 6f 63 69 61 74 65 64 20 63 61 63 68 65 2e 0a sociated cache..
335e0 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 61 */.int sqlite3Pa
335f0 67 65 72 4d 65 6d 55 73 65 64 28 50 61 67 65 72 gerMemUsed(Pager
33600 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 *pPager){. int
33610 20 70 65 72 50 61 67 65 53 69 7a 65 20 3d 20 70 perPageSize = p
33620 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 Pager->pageSize
33630 2b 20 70 50 61 67 65 72 2d 3e 6e 45 78 74 72 61 + pPager->nExtra
33640 20 2b 20 32 30 3b 0a 20 20 72 65 74 75 72 6e 20 + 20;. return
33650 70 65 72 50 61 67 65 53 69 7a 65 2a 73 71 6c 69 perPageSize*sqli
33660 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 te3PcachePagecou
33670 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 nt(pPager->pPCac
33680 68 65 29 0a 20 20 20 20 20 20 20 20 20 20 20 2b he). +
33690 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 sqlite3MallocSi
336a0 7a 65 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f ze(pPager);.}../
336b0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
336c0 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 number of refere
336d0 6e 63 65 73 20 74 6f 20 74 68 65 20 73 70 65 63 nces to the spec
336e0 69 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 69 ified page..*/.i
336f0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 nt sqlite3PagerP
33700 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 50 61 ageRefcount(DbPa
33710 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 65 ge *pPage){. re
33720 74 75 72 6e 20 73 71 6c 69 74 65 33 50 63 61 63 turn sqlite3Pcac
33730 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 hePageRefcount(p
33740 50 61 67 65 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 Page);.}..#ifdef
33750 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a SQLITE_TEST./*.
33760 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
33770 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 is used for test
33780 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 ing and analysis
33790 20 6f 6e 6c 79 2e 0a 2a 2f 0a 69 6e 74 20 2a 73 only..*/.int *s
337a0 71 6c 69 74 65 33 50 61 67 65 72 53 74 61 74 73 qlite3PagerStats
337b0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
337c0 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 61 5b . static int a[
337d0 31 31 5d 3b 0a 20 20 61 5b 30 5d 20 3d 20 73 71 11];. a[0] = sq
337e0 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f lite3PcacheRefCo
337f0 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 unt(pPager->pPCa
33800 63 68 65 29 3b 0a 20 20 61 5b 31 5d 20 3d 20 73 che);. a[1] = s
33810 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 qlite3PcachePage
33820 63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 count(pPager->pP
33830 43 61 63 68 65 29 3b 0a 20 20 61 5b 32 5d 20 3d Cache);. a[2] =
33840 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 sqlite3PcacheGe
33850 74 43 61 63 68 65 73 69 7a 65 28 70 50 61 67 65 tCachesize(pPage
33860 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 61 r->pPCache);. a
33870 5b 33 5d 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 [3] = pPager->db
33880 53 69 7a 65 56 61 6c 69 64 20 3f 20 28 69 6e 74 SizeValid ? (int
33890 29 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 ) pPager->dbSize
338a0 20 3a 20 2d 31 3b 0a 20 20 61 5b 34 5d 20 3d 20 : -1;. a[4] =
338b0 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3b 0a 20 pPager->state;.
338c0 20 61 5b 35 5d 20 3d 20 70 50 61 67 65 72 2d 3e a[5] = pPager->
338d0 65 72 72 43 6f 64 65 3b 0a 20 20 61 5b 36 5d 20 errCode;. a[6]
338e0 3d 20 70 50 61 67 65 72 2d 3e 6e 48 69 74 3b 0a = pPager->nHit;.
338f0 20 20 61 5b 37 5d 20 3d 20 70 50 61 67 65 72 2d a[7] = pPager-
33900 3e 6e 4d 69 73 73 3b 0a 20 20 61 5b 38 5d 20 3d >nMiss;. a[8] =
33910 20 30 3b 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 0; /* Used to
33920 62 65 20 70 50 61 67 65 72 2d 3e 6e 4f 76 66 6c be pPager->nOvfl
33930 20 2a 2f 0a 20 20 61 5b 39 5d 20 3d 20 70 50 61 */. a[9] = pPa
33940 67 65 72 2d 3e 6e 52 65 61 64 3b 0a 20 20 61 5b ger->nRead;. a[
33950 31 30 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 57 10] = pPager->nW
33960 72 69 74 65 3b 0a 20 20 72 65 74 75 72 6e 20 61 rite;. return a
33970 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
33980 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 * Return true if
33990 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d 6d this is an in-m
339a0 65 6d 6f 72 79 20 70 61 67 65 72 2e 0a 2a 2f 0a emory pager..*/.
339b0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
339c0 49 73 4d 65 6d 64 62 28 50 61 67 65 72 20 2a 70 IsMemdb(Pager *p
339d0 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e Pager){. return
339e0 20 4d 45 4d 44 42 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a MEMDB;.}../*.**
339f0 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 72 Check that ther
33a00 65 20 61 72 65 20 61 74 20 6c 65 61 73 74 20 6e e are at least n
33a10 53 61 76 65 70 6f 69 6e 74 20 73 61 76 65 70 6f Savepoint savepo
33a20 69 6e 74 73 20 6f 70 65 6e 2e 20 49 66 20 74 68 ints open. If th
33a30 65 72 65 20 61 72 65 0a 2a 2a 20 63 75 72 72 65 ere are.** curre
33a40 6e 74 6c 79 20 6c 65 73 73 20 74 68 61 6e 20 6e ntly less than n
33a50 53 61 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e 2c Savepoints open,
33a60 20 74 68 65 6e 20 6f 70 65 6e 20 6f 6e 65 20 6f then open one o
33a70 72 20 6d 6f 72 65 20 73 61 76 65 70 6f 69 6e 74 r more savepoint
33a80 73 0a 2a 2a 20 74 6f 20 6d 61 6b 65 20 75 70 20 s.** to make up
33a90 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 2e 20 the difference.
33aa0 49 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 If the number of
33ab0 20 73 61 76 65 70 6f 69 6e 74 73 20 69 73 20 61 savepoints is a
33ac0 6c 72 65 61 64 79 0a 2a 2a 20 65 71 75 61 6c 20 lready.** equal
33ad0 74 6f 20 6e 53 61 76 65 70 6f 69 6e 74 2c 20 74 to nSavepoint, t
33ae0 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
33af0 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a n is a no-op..**
33b00 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 .** If a memory
33b10 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 allocation fails
33b20 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 , SQLITE_NOMEM i
33b30 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 s returned. If a
33b40 6e 20 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 n error .** occu
33b50 72 73 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67 rs while opening
33b60 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
33b70 20 66 69 6c 65 2c 20 74 68 65 6e 20 61 6e 20 49 file, then an I
33b80 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 0a O error code is.
33b90 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 ** returned. Oth
33ba0 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f erwise, SQLITE_O
33bb0 4b 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 K..*/.int sqlite
33bc0 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 3PagerOpenSavepo
33bd0 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 int(Pager *pPage
33be0 72 2c 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e r, int nSavepoin
33bf0 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 t){. int rc = S
33c00 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 QLITE_OK;
33c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
33c20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
33c30 2f 0a 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 /. int nCurrent
33c40 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 = pPager->nSave
33c50 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a point; /*
33c60 20 43 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 Current number
33c70 6f 66 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f of savepoints */
33c80 0a 0a 20 20 69 66 28 20 6e 53 61 76 65 70 6f 69 .. if( nSavepoi
33c90 6e 74 3e 6e 43 75 72 72 65 6e 74 20 26 26 20 70 nt>nCurrent && p
33ca0 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 Pager->useJourna
33cb0 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b l ){. int ii;
33cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
33cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
33ce0 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 /* Iterator var
33cf0 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 50 61 67 iable */. Pag
33d00 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 61 4e 65 erSavepoint *aNe
33d10 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 w;
33d20 20 20 20 20 20 2f 2a 20 4e 65 77 20 50 61 67 65 /* New Page
33d30 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 61 72 72 r.aSavepoint arr
33d40 61 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 ay */. int nP
33d50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
33d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
33d70 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64 61 74 /* Size of dat
33d80 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 abase file */..
33d90 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
33da0 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 agerPagecount(pP
33db0 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 ager, &nPage);.
33dc0 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
33dd0 72 6e 20 72 63 3b 0a 0a 20 20 20 20 2f 2a 20 47 rn rc;.. /* G
33de0 72 6f 77 20 74 68 65 20 50 61 67 65 72 2e 61 53 row the Pager.aS
33df0 61 76 65 70 6f 69 6e 74 20 61 72 72 61 79 20 75 avepoint array u
33e00 73 69 6e 67 20 72 65 61 6c 6c 6f 63 28 29 2e 20 sing realloc().
33e10 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f Return SQLITE_NO
33e20 4d 45 4d 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 MEM. ** if th
33e30 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 e allocation fai
33e40 6c 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 7a ls. Otherwise, z
33e50 65 72 6f 20 74 68 65 20 6e 65 77 20 70 6f 72 74 ero the new port
33e60 69 6f 6e 20 69 6e 20 63 61 73 65 20 61 20 0a 20 ion in case a .
33e70 20 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 ** malloc fai
33e80 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c lure occurs whil
33e90 65 20 70 6f 70 75 6c 61 74 69 6e 67 20 69 74 20 e populating it
33ea0 69 6e 20 74 68 65 20 66 6f 72 28 2e 2e 2e 29 20 in the for(...)
33eb0 6c 6f 6f 70 20 62 65 6c 6f 77 2e 0a 20 20 20 20 loop below..
33ec0 2a 2f 0a 20 20 20 20 61 4e 65 77 20 3d 20 28 50 */. aNew = (P
33ed0 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 29 agerSavepoint *)
33ee0 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 0a sqlite3Realloc(.
33ef0 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
33f00 61 53 61 76 65 70 6f 69 6e 74 2c 20 73 69 7a 65 aSavepoint, size
33f10 6f 66 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e of(PagerSavepoin
33f20 74 29 2a 6e 53 61 76 65 70 6f 69 6e 74 0a 20 20 t)*nSavepoint.
33f30 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21 61 4e );. if( !aN
33f40 65 77 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ew ){. retu
33f50 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
33f60 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 . }. memse
33f70 74 28 26 61 4e 65 77 5b 6e 43 75 72 72 65 6e 74 t(&aNew[nCurrent
33f80 5d 2c 20 30 2c 20 28 6e 53 61 76 65 70 6f 69 6e ], 0, (nSavepoin
33f90 74 2d 6e 43 75 72 72 65 6e 74 29 20 2a 20 73 69 t-nCurrent) * si
33fa0 7a 65 6f 66 28 50 61 67 65 72 53 61 76 65 70 6f zeof(PagerSavepo
33fb0 69 6e 74 29 29 3b 0a 20 20 20 20 70 50 61 67 65 int));. pPage
33fc0 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 3d 20 r->aSavepoint =
33fd0 61 4e 65 77 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f aNew;.. /* Po
33fe0 70 75 6c 61 74 65 20 74 68 65 20 50 61 67 65 72 pulate the Pager
33ff0 53 61 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 Savepoint struct
34000 75 72 65 73 20 6a 75 73 74 20 61 6c 6c 6f 63 61 ures just alloca
34010 74 65 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 ted. */. for(
34020 69 69 3d 6e 43 75 72 72 65 6e 74 3b 20 69 69 3c ii=nCurrent; ii<
34030 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b nSavepoint; ii++
34040 29 7b 0a 20 20 20 20 20 20 61 4e 65 77 5b 69 69 ){. aNew[ii
34050 5d 2e 6e 4f 72 69 67 20 3d 20 6e 50 61 67 65 3b ].nOrig = nPage;
34060 0a 20 20 20 20 20 20 69 66 28 20 69 73 4f 70 65 . if( isOpe
34070 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 n(pPager->jfd) &
34080 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 & pPager->journa
34090 6c 4f 66 66 3e 30 20 29 7b 0a 20 20 20 20 20 20 lOff>0 ){.
340a0 20 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 73 aNew[ii].iOffs
340b0 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 et = pPager->jou
340c0 72 6e 61 6c 4f 66 66 3b 0a 20 20 20 20 20 20 7d rnalOff;. }
340d0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 4e else{. aN
340e0 65 77 5b 69 69 5d 2e 69 4f 66 66 73 65 74 20 3d ew[ii].iOffset =
340f0 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 JOURNAL_HDR_SZ(
34100 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 7d pPager);. }
34110 0a 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e . aNew[ii].
34120 69 53 75 62 52 65 63 20 3d 20 70 50 61 67 65 72 iSubRec = pPager
34130 2d 3e 6e 53 75 62 52 65 63 3b 0a 20 20 20 20 20 ->nSubRec;.
34140 20 61 4e 65 77 5b 69 69 5d 2e 70 49 6e 53 61 76 aNew[ii].pInSav
34150 65 70 6f 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 epoint = sqlite3
34160 42 69 74 76 65 63 43 72 65 61 74 65 28 6e 50 61 BitvecCreate(nPa
34170 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 ge);. if( !
34180 61 4e 65 77 5b 69 69 5d 2e 70 49 6e 53 61 76 65 aNew[ii].pInSave
34190 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 point ){.
341a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
341b0 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 OMEM;. }.
341c0 20 20 20 20 69 66 28 20 70 61 67 65 72 55 73 65 if( pagerUse
341d0 57 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 Wal(pPager) ){.
341e0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 61 sqlite3Wa
341f0 6c 53 61 76 65 70 6f 69 6e 74 28 70 50 61 67 65 lSavepoint(pPage
34200 72 2d 3e 70 57 61 6c 2c 20 61 4e 65 77 5b 69 69 r->pWal, aNew[ii
34210 5d 2e 61 57 61 6c 44 61 74 61 29 3b 0a 20 20 20 ].aWalData);.
34220 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 }. pPage
34230 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 r->nSavepoint =
34240 69 69 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ii+1;. }.
34250 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
34260 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 6e 53 61 76 nSavepoint==nSav
34270 65 70 6f 69 6e 74 20 29 3b 0a 20 20 20 20 61 73 epoint );. as
34280 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 sertTruncateCons
34290 74 72 61 69 6e 74 28 70 50 61 67 65 72 29 3b 0a traint(pPager);.
342a0 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 }.. return rc
342b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
342c0 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
342d0 65 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f ed to rollback o
342e0 72 20 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 r release (commi
342f0 74 29 20 61 20 73 61 76 65 70 6f 69 6e 74 2e 0a t) a savepoint..
34300 2a 2a 20 54 68 65 20 73 61 76 65 70 6f 69 6e 74 ** The savepoint
34310 20 74 6f 20 72 65 6c 65 61 73 65 20 6f 72 20 72 to release or r
34320 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 20 6e 6f 74 ollback need not
34330 20 62 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 be the most rec
34340 65 6e 74 6c 79 20 0a 2a 2a 20 63 72 65 61 74 65 ently .** create
34350 64 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a d savepoint..**.
34360 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 6f 70 20 ** Parameter op
34370 69 73 20 61 6c 77 61 79 73 20 65 69 74 68 65 72 is always either
34380 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 SAVEPOINT_ROLLB
34390 41 43 4b 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 ACK or SAVEPOINT
343a0 5f 52 45 4c 45 41 53 45 2e 0a 2a 2a 20 49 66 20 _RELEASE..** If
343b0 69 74 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f it is SAVEPOINT_
343c0 52 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 72 65 RELEASE, then re
343d0 6c 65 61 73 65 20 61 6e 64 20 64 65 73 74 72 6f lease and destro
343e0 79 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 y the savepoint
343f0 77 69 74 68 0a 2a 2a 20 69 6e 64 65 78 20 69 53 with.** index iS
34400 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 69 74 20 avepoint. If it
34410 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c is SAVEPOINT_ROL
34420 4c 42 41 43 4b 2c 20 74 68 65 6e 20 72 6f 6c 6c LBACK, then roll
34430 62 61 63 6b 20 61 6c 6c 20 63 68 61 6e 67 65 73 back all changes
34440 0a 2a 2a 20 74 68 61 74 20 68 61 76 65 20 6f 63 .** that have oc
34450 63 75 72 72 65 64 20 73 69 6e 63 65 20 74 68 65 curred since the
34460 20 73 70 65 63 69 66 69 65 64 20 73 61 76 65 70 specified savep
34470 6f 69 6e 74 20 77 61 73 20 63 72 65 61 74 65 64 oint was created
34480 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 76 65 ..**.** The save
34490 70 6f 69 6e 74 20 74 6f 20 72 6f 6c 6c 62 61 63 point to rollbac
344a0 6b 20 6f 72 20 72 65 6c 65 61 73 65 20 69 73 20 k or release is
344b0 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 70 61 identified by pa
344c0 72 61 6d 65 74 65 72 20 0a 2a 2a 20 69 53 61 76 rameter .** iSav
344d0 65 70 6f 69 6e 74 2e 20 41 20 76 61 6c 75 65 20 epoint. A value
344e0 6f 66 20 30 20 6d 65 61 6e 73 20 74 6f 20 6f 70 of 0 means to op
344f0 65 72 61 74 65 20 6f 6e 20 74 68 65 20 6f 75 74 erate on the out
34500 65 72 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 ermost savepoint
34510 0a 2a 2a 20 28 74 68 65 20 66 69 72 73 74 20 63 .** (the first c
34520 72 65 61 74 65 64 29 2e 20 41 20 76 61 6c 75 65 reated). A value
34530 20 6f 66 20 28 50 61 67 65 72 2e 6e 53 61 76 65 of (Pager.nSave
34540 70 6f 69 6e 74 2d 31 29 20 6d 65 61 6e 73 20 6f point-1) means o
34550 70 65 72 61 74 65 0a 2a 2a 20 6f 6e 20 74 68 65 perate.** on the
34560 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 most recently c
34570 72 65 61 74 65 64 20 73 61 76 65 70 6f 69 6e 74 reated savepoint
34580 2e 20 49 66 20 69 53 61 76 65 70 6f 69 6e 74 20 . If iSavepoint
34590 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a is greater than.
345a0 2a 2a 20 28 50 61 67 65 72 2e 6e 53 61 76 65 70 ** (Pager.nSavep
345b0 6f 69 6e 74 2d 31 29 2c 20 74 68 65 6e 20 74 68 oint-1), then th
345c0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
345d0 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 no-op..**.** If
345e0 20 61 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 a negative valu
345f0 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 74 e is passed to t
34600 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 his function, th
34610 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a en the current.*
34620 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 * transaction is
34630 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 rolled back. Th
34640 69 73 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 is is different
34650 74 6f 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 to calling .** s
34660 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 qlite3PagerRollb
34670 61 63 6b 28 29 20 62 65 63 61 75 73 65 20 74 68 ack() because th
34680 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 is function does
34690 20 6e 6f 74 20 74 65 72 6d 69 6e 61 74 65 0a 2a not terminate.*
346a0 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f * the transactio
346b0 6e 20 6f 72 20 75 6e 6c 6f 63 6b 20 74 68 65 20 n or unlock the
346c0 64 61 74 61 62 61 73 65 2c 20 69 74 20 6a 75 73 database, it jus
346d0 74 20 72 65 73 74 6f 72 65 73 20 74 68 65 20 0a t restores the .
346e0 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 ** contents of t
346f0 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 he database to i
34700 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 ts original stat
34710 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 79 e. .**.** In any
34720 20 63 61 73 65 2c 20 61 6c 6c 20 73 61 76 65 70 case, all savep
34730 6f 69 6e 74 73 20 77 69 74 68 20 61 6e 20 69 6e oints with an in
34740 64 65 78 20 67 72 65 61 74 65 72 20 74 68 61 6e dex greater than
34750 20 69 53 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 iSavepoint .**
34760 61 72 65 20 64 65 73 74 72 6f 79 65 64 2e 20 49 are destroyed. I
34770 66 20 74 68 69 73 20 69 73 20 61 20 72 65 6c 65 f this is a rele
34780 61 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 6f ase operation (o
34790 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c p==SAVEPOINT_REL
347a0 45 41 53 45 29 2c 0a 2a 2a 20 74 68 65 6e 20 73 EASE),.** then s
347b0 61 76 65 70 6f 69 6e 74 20 69 53 61 76 65 70 6f avepoint iSavepo
347c0 69 6e 74 20 69 73 20 61 6c 73 6f 20 64 65 73 74 int is also dest
347d0 72 6f 79 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 royed..**.** Thi
347e0 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 s function may r
347f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
34800 45 4d 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 EM if a memory a
34810 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c llocation fails,
34820 0a 2a 2a 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 .** or an IO err
34830 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 20 49 4f or code if an IO
34840 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 error occurs wh
34850 69 6c 65 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b ile rolling back
34860 20 61 20 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 a .** savepoint
34870 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 73 20 6f . If no errors o
34880 63 63 75 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 ccur, SQLITE_OK
34890 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 20 is returned..*/
348a0 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 .int sqlite3Page
348b0 72 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 rSavepoint(Pager
348c0 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 *pPager, int op
348d0 2c 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 , int iSavepoint
348e0 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
348f0 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 LITE_OK;.. asse
34900 72 74 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e rt( op==SAVEPOIN
34910 54 5f 52 45 4c 45 41 53 45 20 7c 7c 20 6f 70 3d T_RELEASE || op=
34920 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 =SAVEPOINT_ROLLB
34930 41 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ACK );. assert(
34940 20 69 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 7c iSavepoint>=0 |
34950 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f | op==SAVEPOINT_
34960 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 0a 20 20 69 ROLLBACK );.. i
34970 66 28 20 69 53 61 76 65 70 6f 69 6e 74 3c 70 50 f( iSavepoint<pP
34980 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 ager->nSavepoint
34990 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 ){. int ii;
349a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 /* It
349b0 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 erator variable
349c0 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 4e 65 77 3b */. int nNew;
349d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
349e0 62 65 72 20 6f 66 20 72 65 6d 61 69 6e 69 6e 67 ber of remaining
349f0 20 73 61 76 65 70 6f 69 6e 74 73 20 61 66 74 65 savepoints afte
34a00 72 20 74 68 69 73 20 6f 70 2e 20 2a 2f 0a 0a 20 r this op. */..
34a10 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 /* Figure out
34a20 20 68 6f 77 20 6d 61 6e 79 20 73 61 76 65 70 6f how many savepo
34a30 69 6e 74 73 20 77 69 6c 6c 20 73 74 69 6c 6c 20 ints will still
34a40 62 65 20 61 63 74 69 76 65 20 61 66 74 65 72 20 be active after
34a50 74 68 69 73 0a 20 20 20 20 2a 2a 20 6f 70 65 72 this. ** oper
34a60 61 74 69 6f 6e 2e 20 53 74 6f 72 65 20 74 68 69 ation. Store thi
34a70 73 20 76 61 6c 75 65 20 69 6e 20 6e 4e 65 77 2e s value in nNew.
34a80 20 54 68 65 6e 20 66 72 65 65 20 72 65 73 6f 75 Then free resou
34a90 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 rces associated
34aa0 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 61 6e 79 . ** with any
34ab0 20 73 61 76 65 70 6f 69 6e 74 73 20 74 68 61 74 savepoints that
34ac0 20 61 72 65 20 64 65 73 74 72 6f 79 65 64 20 62 are destroyed b
34ad0 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e y this operation
34ae0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 4e 65 .. */. nNe
34af0 77 20 3d 20 69 53 61 76 65 70 6f 69 6e 74 20 2b w = iSavepoint +
34b00 20 28 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e (( op==SAVEPOIN
34b10 54 5f 52 45 4c 45 41 53 45 20 29 20 3f 20 30 20 T_RELEASE ) ? 0
34b20 3a 20 31 29 3b 0a 20 20 20 20 66 6f 72 28 69 69 : 1);. for(ii
34b30 3d 6e 4e 65 77 3b 20 69 69 3c 70 50 61 67 65 72 =nNew; ii<pPager
34b40 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 ->nSavepoint; ii
34b50 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ++){. sqlit
34b60 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 e3BitvecDestroy(
34b70 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 pPager->aSavepoi
34b80 6e 74 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f nt[ii].pInSavepo
34b90 69 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 int);. }.
34ba0 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 pPager->nSavepoi
34bb0 6e 74 20 3d 20 6e 4e 65 77 3b 0a 0a 20 20 20 20 nt = nNew;..
34bc0 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 /* If this is a
34bd0 72 65 6c 65 61 73 65 20 6f 66 20 74 68 65 20 6f release of the o
34be0 75 74 65 72 6d 6f 73 74 20 73 61 76 65 70 6f 69 utermost savepoi
34bf0 6e 74 2c 20 74 72 75 6e 63 61 74 65 20 0a 20 20 nt, truncate .
34c00 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75 ** the sub-jou
34c10 72 6e 61 6c 20 74 6f 20 7a 65 72 6f 20 62 79 74 rnal to zero byt
34c20 65 73 20 69 6e 20 73 69 7a 65 2e 20 2a 2f 0a 20 es in size. */.
34c30 20 20 20 69 66 28 20 6f 70 3d 3d 53 41 56 45 50 if( op==SAVEP
34c40 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b 0a OINT_RELEASE ){.
34c50 20 20 20 20 20 20 69 66 28 20 6e 4e 65 77 3d 3d if( nNew==
34c60 30 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 0 && isOpen(pPag
34c70 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 er->sjfd) ){.
34c80 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 74 72 75 /* Only tru
34c90 6e 63 61 74 65 20 69 66 20 69 74 20 69 73 20 61 ncate if it is a
34ca0 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 75 62 2d n in-memory sub-
34cb0 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 20 20 journal. */.
34cc0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 if( sqlite3I
34cd0 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 sMemJournal(pPag
34ce0 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 er->sjfd) ){.
34cf0 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
34d00 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 te3OsTruncate(pP
34d10 61 67 65 72 2d 3e 73 6a 66 64 2c 20 30 29 3b 0a ager->sjfd, 0);.
34d20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
34d30 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
34d40 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
34d50 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 75 pPager->nSu
34d60 62 52 65 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 bRec = 0;.
34d70 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 45 }. }. /* E
34d80 6c 73 65 20 74 68 69 73 20 69 73 20 61 20 72 6f lse this is a ro
34d90 6c 6c 62 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e llback operation
34da0 2c 20 70 6c 61 79 62 61 63 6b 20 74 68 65 20 73 , playback the s
34db0 70 65 63 69 66 69 65 64 20 73 61 76 65 70 6f 69 pecified savepoi
34dc0 6e 74 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 nt.. ** If th
34dd0 69 73 20 69 73 20 61 20 74 65 6d 70 2d 66 69 6c is is a temp-fil
34de0 65 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c e, it is possibl
34df0 65 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e e that the journ
34e00 61 6c 20 66 69 6c 65 20 68 61 73 0a 20 20 20 20 al file has.
34e10 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 ** not yet been
34e20 6f 70 65 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 opened. In this
34e30 63 61 73 65 20 74 68 65 72 65 20 68 61 76 65 20 case there have
34e40 62 65 65 6e 20 6e 6f 20 63 68 61 6e 67 65 73 20 been no changes
34e50 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 to. ** the da
34e60 74 61 62 61 73 65 20 66 69 6c 65 2c 20 73 6f 20 tabase file, so
34e70 74 68 65 20 70 6c 61 79 62 61 63 6b 20 6f 70 65 the playback ope
34e80 72 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 73 6b ration can be sk
34e90 69 70 70 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 ipped.. */.
34ea0 20 20 65 6c 73 65 20 69 66 28 20 70 61 67 65 72 else if( pager
34eb0 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 7c UseWal(pPager) |
34ec0 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d | isOpen(pPager-
34ed0 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 50 >jfd) ){. P
34ee0 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 agerSavepoint *p
34ef0 53 61 76 65 70 6f 69 6e 74 20 3d 20 28 6e 4e 65 Savepoint = (nNe
34f00 77 3d 3d 30 29 3f 30 3a 26 70 50 61 67 65 72 2d w==0)?0:&pPager-
34f10 3e 61 53 61 76 65 70 6f 69 6e 74 5b 6e 4e 65 77 >aSavepoint[nNew
34f20 2d 31 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 -1];. rc =
34f30 70 61 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76 pagerPlaybackSav
34f40 65 70 6f 69 6e 74 28 70 50 61 67 65 72 2c 20 70 epoint(pPager, p
34f50 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 Savepoint);.
34f60 20 20 61 73 73 65 72 74 28 72 63 21 3d 53 51 4c assert(rc!=SQL
34f70 49 54 45 5f 44 4f 4e 45 29 3b 0a 20 20 20 20 7d ITE_DONE);. }
34f80 0a 20 20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e . . }. return
34f90 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
34fa0 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 turn the full pa
34fb0 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 thname of the da
34fc0 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a tabase file..*/.
34fd0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
34fe0 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 te3PagerFilename
34ff0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
35000 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 . return pPager
35010 2d 3e 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a ->zFilename;.}..
35020 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
35030 20 56 46 53 20 73 74 72 75 63 74 75 72 65 20 66 VFS structure f
35040 6f 72 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f or the pager..*/
35050 0a 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 .const sqlite3_v
35060 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 fs *sqlite3Pager
35070 56 66 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 Vfs(Pager *pPage
35080 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 r){. return pPa
35090 67 65 72 2d 3e 70 56 66 73 3b 0a 7d 0a 0a 2f 2a ger->pVfs;.}../*
350a0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 .** Return the f
350b0 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 ile handle for t
350c0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
350d0 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 associated.** w
350e0 69 74 68 20 74 68 65 20 70 61 67 65 72 2e 20 20 ith the pager.
350f0 54 68 69 73 20 6d 69 67 68 74 20 72 65 74 75 72 This might retur
35100 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 66 69 n NULL if the fi
35110 6c 65 20 68 61 73 0a 2a 2a 20 6e 6f 74 20 79 65 le has.** not ye
35120 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 2a t been opened..*
35130 2f 0a 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a /.sqlite3_file *
35140 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 sqlite3PagerFile
35150 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
35160 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 . return pPager
35170 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ->fd;.}../*.** R
35180 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 eturn the full p
35190 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a athname of the j
351a0 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a ournal file..*/.
351b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
351c0 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 6e te3PagerJournaln
351d0 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ame(Pager *pPage
351e0 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 r){. return pPa
351f0 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a 7d ger->zJournal;.}
35200 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
35210 72 75 65 20 69 66 20 66 73 79 6e 63 28 29 20 63 rue if fsync() c
35220 61 6c 6c 73 20 61 72 65 20 64 69 73 61 62 6c 65 alls are disable
35230 64 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 d for this pager
35240 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0a . Return FALSE.
35250 2a 2a 20 69 66 20 66 73 79 6e 63 28 29 73 20 61 ** if fsync()s a
35260 72 65 20 65 78 65 63 75 74 65 64 20 6e 6f 72 6d re executed norm
35270 61 6c 6c 79 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c ally..*/.int sql
35280 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 ite3PagerNosync(
35290 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
352a0 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
352b0 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23 69 66 64 >noSync;.}..#ifd
352c0 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f ef SQLITE_HAS_CO
352d0 44 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 DEC./*.** Set or
352e0 20 72 65 74 72 69 65 76 65 20 74 68 65 20 63 6f retrieve the co
352f0 64 65 63 20 66 6f 72 20 74 68 69 73 20 70 61 67 dec for this pag
35300 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 er.*/.static voi
35310 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 d sqlite3PagerSe
35320 74 43 6f 64 65 63 28 0a 20 20 50 61 67 65 72 20 tCodec(. Pager
35330 2a 70 50 61 67 65 72 2c 0a 20 20 76 6f 69 64 20 *pPager,. void
35340 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 2a *(*xCodec)(void*
35350 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 ,void*,Pgno,int)
35360 2c 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 ,. void (*xCode
35370 63 53 69 7a 65 43 68 6e 67 29 28 76 6f 69 64 2a cSizeChng)(void*
35380 2c 69 6e 74 2c 69 6e 74 29 2c 0a 20 20 76 6f 69 ,int,int),. voi
35390 64 20 28 2a 78 43 6f 64 65 63 46 72 65 65 29 28 d (*xCodecFree)(
353a0 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 20 2a void*),. void *
353b0 70 43 6f 64 65 63 0a 29 7b 0a 20 20 69 66 28 20 pCodec.){. if(
353c0 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 pPager->xCodecFr
353d0 65 65 20 29 20 70 50 61 67 65 72 2d 3e 78 43 6f ee ) pPager->xCo
353e0 64 65 63 46 72 65 65 28 70 50 61 67 65 72 2d 3e decFree(pPager->
353f0 70 43 6f 64 65 63 29 3b 0a 20 20 70 50 61 67 65 pCodec);. pPage
35400 72 2d 3e 78 43 6f 64 65 63 20 3d 20 70 50 61 67 r->xCodec = pPag
35410 65 72 2d 3e 6d 65 6d 44 62 20 3f 20 30 20 3a 20 er->memDb ? 0 :
35420 78 43 6f 64 65 63 3b 0a 20 20 70 50 61 67 65 72 xCodec;. pPager
35430 2d 3e 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 ->xCodecSizeChng
35440 20 3d 20 78 43 6f 64 65 63 53 69 7a 65 43 68 6e = xCodecSizeChn
35450 67 3b 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f g;. pPager->xCo
35460 64 65 63 46 72 65 65 20 3d 20 78 43 6f 64 65 63 decFree = xCodec
35470 46 72 65 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e Free;. pPager->
35480 70 43 6f 64 65 63 20 3d 20 70 43 6f 64 65 63 3b pCodec = pCodec;
35490 0a 20 20 70 61 67 65 72 52 65 70 6f 72 74 53 69 . pagerReportSi
354a0 7a 65 28 70 50 61 67 65 72 29 3b 0a 7d 0a 73 74 ze(pPager);.}.st
354b0 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 atic void *sqlit
354c0 65 33 50 61 67 65 72 47 65 74 43 6f 64 65 63 28 e3PagerGetCodec(
354d0 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
354e0 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
354f0 3e 70 43 6f 64 65 63 3b 0a 7d 0a 23 65 6e 64 69 >pCodec;.}.#endi
35500 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f..#ifndef SQLIT
35510 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
35520 4d 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 M./*.** Move the
35530 20 70 61 67 65 20 70 50 67 20 74 6f 20 6c 6f 63 page pPg to loc
35540 61 74 69 6f 6e 20 70 67 6e 6f 20 69 6e 20 74 68 ation pgno in th
35550 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 e file..**.** Th
35560 65 72 65 20 6d 75 73 74 20 62 65 20 6e 6f 20 72 ere must be no r
35570 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 eferences to the
35580 20 70 61 67 65 20 70 72 65 76 69 6f 75 73 6c 79 page previously
35590 20 6c 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20 70 located at.** p
355a0 67 6e 6f 20 28 77 68 69 63 68 20 77 65 20 63 61 gno (which we ca
355b0 6c 6c 20 70 50 67 4f 6c 64 29 20 74 68 6f 75 67 ll pPgOld) thoug
355c0 68 20 74 68 61 74 20 70 61 67 65 20 69 73 20 61 h that page is a
355d0 6c 6c 6f 77 65 64 20 74 6f 20 62 65 0a 2a 2a 20 llowed to be.**
355e0 69 6e 20 63 61 63 68 65 2e 20 20 49 66 20 74 68 in cache. If th
355f0 65 20 70 61 67 65 20 70 72 65 76 69 6f 75 73 6c e page previousl
35600 79 20 6c 6f 63 61 74 65 64 20 61 74 20 70 67 6e y located at pgn
35610 6f 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 o is not already
35620 0a 2a 2a 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 .** in the rollb
35630 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 ack journal, it
35640 69 73 20 6e 6f 74 20 70 75 74 20 74 68 65 72 65 is not put there
35650 20 62 79 20 62 79 20 74 68 69 73 20 72 6f 75 74 by by this rout
35660 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 ine..**.** Refer
35670 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 ences to the pag
35680 65 20 70 50 67 20 72 65 6d 61 69 6e 20 76 61 6c e pPg remain val
35690 69 64 2e 20 55 70 64 61 74 69 6e 67 20 61 6e 79 id. Updating any
356a0 0a 2a 2a 20 6d 65 74 61 2d 64 61 74 61 20 61 73 .** meta-data as
356b0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 50 sociated with pP
356c0 67 20 28 69 2e 65 2e 20 64 61 74 61 20 73 74 6f g (i.e. data sto
356d0 72 65 64 20 69 6e 20 74 68 65 20 6e 45 78 74 72 red in the nExtr
356e0 61 20 62 79 74 65 73 0a 2a 2a 20 61 6c 6c 6f 63 a bytes.** alloc
356f0 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 ated along with
35700 74 68 65 20 70 61 67 65 29 20 69 73 20 74 68 65 the page) is the
35710 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 responsibility
35720 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a of the caller..*
35730 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69 *.** A transacti
35740 6f 6e 20 6d 75 73 74 20 62 65 20 61 63 74 69 76 on must be activ
35750 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 e when this rout
35760 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 49 ine is called. I
35770 74 20 75 73 65 64 20 74 6f 20 62 65 0a 2a 2a 20 t used to be.**
35780 72 65 71 75 69 72 65 64 20 74 68 61 74 20 61 20 required that a
35790 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
357a0 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 61 63 ction was not ac
357b0 74 69 76 65 2c 20 62 75 74 20 74 68 69 73 20 72 tive, but this r
357c0 65 73 74 72 69 63 74 69 6f 6e 0a 2a 2a 20 68 61 estriction.** ha
357d0 73 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 20 28 s been removed (
357e0 43 52 45 41 54 45 20 49 4e 44 45 58 20 6e 65 65 CREATE INDEX nee
357f0 64 73 20 74 6f 20 6d 6f 76 65 20 61 20 70 61 67 ds to move a pag
35800 65 20 77 68 65 6e 20 61 20 73 74 61 74 65 6d 65 e when a stateme
35810 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f nt.** transactio
35820 6e 20 69 73 20 61 63 74 69 76 65 29 2e 0a 2a 2a n is active)..**
35830 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 75 72 74 .** If the fourt
35840 68 20 61 72 67 75 6d 65 6e 74 2c 20 69 73 43 6f h argument, isCo
35850 6d 6d 69 74 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72 mmit, is non-zer
35860 6f 2c 20 74 68 65 6e 20 74 68 69 73 20 70 61 67 o, then this pag
35870 65 20 69 73 20 62 65 69 6e 67 0a 2a 2a 20 6d 6f e is being.** mo
35880 76 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 ved as part of a
35890 20 64 61 74 61 62 61 73 65 20 72 65 6f 72 67 61 database reorga
358a0 6e 69 7a 61 74 69 6f 6e 20 6a 75 73 74 20 62 65 nization just be
358b0 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 fore the transac
358c0 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 62 65 69 6e tion .** is bein
358d0 67 20 63 6f 6d 6d 69 74 74 65 64 2e 20 49 6e 20 g committed. In
358e0 74 68 69 73 20 63 61 73 65 2c 20 69 74 20 69 73 this case, it is
358f0 20 67 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 guaranteed that
35900 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61 the database pa
35910 67 65 20 0a 2a 2a 20 70 50 67 20 72 65 66 65 72 ge .** pPg refer
35920 73 20 74 6f 20 77 69 6c 6c 20 6e 6f 74 20 62 65 s to will not be
35930 20 77 72 69 74 74 65 6e 20 74 6f 20 61 67 61 69 written to agai
35940 6e 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 72 n within this tr
35950 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a ansaction..**.**
35960 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d This function m
35970 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ay return SQLITE
35980 5f 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f 20 _NOMEM or an IO
35990 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e error code if an
359a0 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 error.** occurs
359b0 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 . Otherwise, it
359c0 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f returns SQLITE_O
359d0 4b 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 K..*/.int sqlite
359e0 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 50 3PagerMovepage(P
359f0 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 44 62 ager *pPager, Db
35a00 50 61 67 65 20 2a 70 50 67 2c 20 50 67 6e 6f 20 Page *pPg, Pgno
35a10 70 67 6e 6f 2c 20 69 6e 74 20 69 73 43 6f 6d 6d pgno, int isComm
35a20 69 74 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 it){. PgHdr *pP
35a30 67 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 gOld;
35a40 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 /* The page
35a50 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65 being overwritte
35a60 6e 2e 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 65 65 n. */. Pgno nee
35a70 64 53 79 6e 63 50 67 6e 6f 20 3d 20 30 3b 20 20 dSyncPgno = 0;
35a80 20 20 20 20 20 2f 2a 20 4f 6c 64 20 76 61 6c 75 /* Old valu
35a90 65 20 6f 66 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 e of pPg->pgno,
35aa0 69 66 20 73 79 6e 63 20 69 73 20 72 65 71 75 69 if sync is requi
35ab0 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b red */. int rc;
35ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
35ad0 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
35ae0 63 6f 64 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6f code */. Pgno o
35af0 72 69 67 50 67 6e 6f 3b 20 20 20 20 20 20 20 20 rigPgno;
35b00 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 /* The or
35b10 69 67 69 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62 iginal page numb
35b20 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 er */.. assert(
35b30 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a pPg->nRef>0 );.
35b40 0a 20 20 2f 2a 20 49 6e 20 6f 72 64 65 72 20 74 . /* In order t
35b50 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c o be able to rol
35b60 6c 62 61 63 6b 2c 20 61 6e 20 69 6e 2d 6d 65 6d lback, an in-mem
35b70 6f 72 79 20 64 61 74 61 62 61 73 65 20 6d 75 73 ory database mus
35b80 74 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 74 t journal. ** t
35b90 68 65 20 70 61 67 65 20 77 65 20 61 72 65 20 6d he page we are m
35ba0 6f 76 69 6e 67 20 66 72 6f 6d 2e 0a 20 20 2a 2f oving from.. */
35bb0 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a . if( MEMDB ){.
35bc0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
35bd0 50 61 67 65 72 57 72 69 74 65 28 70 50 67 29 3b PagerWrite(pPg);
35be0 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 . if( rc ) re
35bf0 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 turn rc;. }..
35c00 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 62 /* If the page b
35c10 65 69 6e 67 20 6d 6f 76 65 64 20 69 73 20 64 69 eing moved is di
35c20 72 74 79 20 61 6e 64 20 68 61 73 20 6e 6f 74 20 rty and has not
35c30 62 65 65 6e 20 73 61 76 65 64 20 62 79 20 74 68 been saved by th
35c40 65 20 6c 61 74 65 73 74 0a 20 20 2a 2a 20 73 61 e latest. ** sa
35c50 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 73 61 vepoint, then sa
35c60 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 ve the current c
35c70 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 ontents of the p
35c80 61 67 65 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 age into the .
35c90 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6e ** sub-journal n
35ca0 6f 77 2e 20 54 68 69 73 20 69 73 20 72 65 71 75 ow. This is requ
35cb0 69 72 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 74 ired to handle t
35cc0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 63 65 he following sce
35cd0 6e 61 72 69 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a nario:. **. **
35ce0 20 20 20 42 45 47 49 4e 3b 0a 20 20 2a 2a 20 20 BEGIN;. **
35cf0 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 61 67 65 <journal page
35d00 20 58 2c 20 74 68 65 6e 20 6d 6f 64 69 66 79 20 X, then modify
35d10 69 74 20 69 6e 20 6d 65 6d 6f 72 79 3e 0a 20 20 it in memory>.
35d20 2a 2a 20 20 20 20 20 53 41 56 45 50 4f 49 4e 54 ** SAVEPOINT
35d30 20 6f 6e 65 3b 0a 20 20 2a 2a 20 20 20 20 20 20 one;. **
35d40 20 3c 4d 6f 76 65 20 70 61 67 65 20 58 20 74 6f <Move page X to
35d50 20 6c 6f 63 61 74 69 6f 6e 20 59 3e 0a 20 20 2a location Y>. *
35d60 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 20 54 * ROLLBACK T
35d70 4f 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20 2a 2a O one;. **. **
35d80 20 49 66 20 70 61 67 65 20 58 20 77 65 72 65 20 If page X were
35d90 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f 20 74 not written to t
35da0 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 68 he sub-journal h
35db0 65 72 65 2c 20 69 74 20 77 6f 75 6c 64 20 6e 6f ere, it would no
35dc0 74 0a 20 20 2a 2a 20 62 65 20 70 6f 73 73 69 62 t. ** be possib
35dd0 6c 65 20 74 6f 20 72 65 73 74 6f 72 65 20 69 74 le to restore it
35de0 73 20 63 6f 6e 74 65 6e 74 73 20 77 68 65 6e 20 s contents when
35df0 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 54 4f the "ROLLBACK TO
35e00 20 6f 6e 65 22 0a 20 20 2a 2a 20 73 74 61 74 65 one". ** state
35e10 6d 65 6e 74 20 77 65 72 65 20 69 73 20 70 72 6f ment were is pro
35e20 63 65 73 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a cessed.. **. *
35e30 2a 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 * subjournalPage
35e40 28 29 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 61 () may need to a
35e50 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 74 6f llocate space to
35e60 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f store pPg->pgno
35e70 20 69 6e 74 6f 0a 20 20 2a 2a 20 6f 6e 65 20 6f into. ** one o
35e80 72 20 6d 6f 72 65 20 73 61 76 65 70 6f 69 6e 74 r more savepoint
35e90 20 62 69 74 76 65 63 73 2e 20 54 68 69 73 20 69 bitvecs. This i
35ea0 73 20 74 68 65 20 72 65 61 73 6f 6e 20 74 68 69 s the reason thi
35eb0 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 s function. **
35ec0 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 may return SQLIT
35ed0 45 5f 4e 4f 4d 45 4d 2e 0a 20 20 2a 2f 0a 20 20 E_NOMEM.. */.
35ee0 69 66 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 if( pPg->flags&P
35ef0 47 48 44 52 5f 44 49 52 54 59 0a 20 20 20 26 26 GHDR_DIRTY. &&
35f00 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 subjRequiresPag
35f10 65 28 70 50 67 29 0a 20 20 20 26 26 20 53 51 4c e(pPg). && SQL
35f20 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 75 ITE_OK!=(rc = su
35f30 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 67 bjournalPage(pPg
35f40 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 )). ){. retu
35f50 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 50 41 rn rc;. }.. PA
35f60 47 45 52 54 52 41 43 45 28 28 22 4d 4f 56 45 20 GERTRACE(("MOVE
35f70 25 64 20 70 61 67 65 20 25 64 20 28 6e 65 65 64 %d page %d (need
35f80 53 79 6e 63 3d 25 64 29 20 6d 6f 76 65 73 20 74 Sync=%d) moves t
35f90 6f 20 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 o %d\n", .
35fa0 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c PAGERID(pPager),
35fb0 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 28 70 50 67 pPg->pgno, (pPg
35fc0 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 ->flags&PGHDR_NE
35fd0 45 44 5f 53 59 4e 43 29 3f 31 3a 30 2c 20 70 67 ED_SYNC)?1:0, pg
35fe0 6e 6f 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 no));. IOTRACE(
35ff0 28 22 4d 4f 56 45 20 25 70 20 25 64 20 25 64 5c ("MOVE %p %d %d\
36000 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d n", pPager, pPg-
36010 3e 70 67 6e 6f 2c 20 70 67 6e 6f 29 29 0a 0a 20 >pgno, pgno))..
36020 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e /* If the journ
36030 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 al needs to be s
36040 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 70 ync()ed before p
36050 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 63 61 age pPg->pgno ca
36060 6e 0a 20 20 2a 2a 20 62 65 20 77 72 69 74 74 65 n. ** be writte
36070 6e 20 74 6f 2c 20 73 74 6f 72 65 20 70 50 67 2d n to, store pPg-
36080 3e 70 67 6e 6f 20 69 6e 20 6c 6f 63 61 6c 20 76 >pgno in local v
36090 61 72 69 61 62 6c 65 20 6e 65 65 64 53 79 6e 63 ariable needSync
360a0 50 67 6e 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 Pgno.. **. **
360b0 49 66 20 74 68 65 20 69 73 43 6f 6d 6d 69 74 20 If the isCommit
360c0 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 flag is set, the
360d0 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f re is no need to
360e0 20 72 65 6d 65 6d 62 65 72 20 74 68 61 74 0a 20 remember that.
360f0 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ** the journal
36100 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 needs to be sync
36110 28 29 65 64 20 62 65 66 6f 72 65 20 64 61 74 61 ()ed before data
36120 62 61 73 65 20 70 61 67 65 20 70 50 67 2d 3e 70 base page pPg->p
36130 67 6e 6f 20 0a 20 20 2a 2a 20 63 61 6e 20 62 65 gno . ** can be
36140 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 54 68 65 written to. The
36150 20 63 61 6c 6c 65 72 20 68 61 73 20 61 6c 72 65 caller has alre
36160 61 64 79 20 70 72 6f 6d 69 73 65 64 20 6e 6f 74 ady promised not
36170 20 74 6f 20 77 72 69 74 65 20 74 6f 20 69 74 2e to write to it.
36180 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 50 67 . */. if( (pPg
36190 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 ->flags&PGHDR_NE
361a0 45 44 5f 53 59 4e 43 29 20 26 26 20 21 69 73 43 ED_SYNC) && !isC
361b0 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 6e 65 65 ommit ){. nee
361c0 64 53 79 6e 63 50 67 6e 6f 20 3d 20 70 50 67 2d dSyncPgno = pPg-
361d0 3e 70 67 6e 6f 3b 0a 20 20 20 20 61 73 73 65 72 >pgno;. asser
361e0 74 28 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c t( pageInJournal
361f0 28 70 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70 67 (pPg) || pPg->pg
36200 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 no>pPager->dbOri
36210 67 53 69 7a 65 20 29 3b 0a 20 20 20 20 61 73 73 gSize );. ass
36220 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 ert( pPg->flags&
36230 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 20 PGHDR_DIRTY );.
36240 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
36250 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 3b 0a 20 r->needSync );.
36260 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
36270 63 61 63 68 65 20 63 6f 6e 74 61 69 6e 73 20 61 cache contains a
36280 20 70 61 67 65 20 77 69 74 68 20 70 61 67 65 2d page with page-
36290 6e 75 6d 62 65 72 20 70 67 6e 6f 2c 20 72 65 6d number pgno, rem
362a0 6f 76 65 20 69 74 0a 20 20 2a 2a 20 66 72 6f 6d ove it. ** from
362b0 20 69 74 73 20 68 61 73 68 20 63 68 61 69 6e 2e its hash chain.
362c0 20 41 6c 73 6f 2c 20 69 66 20 74 68 65 20 50 67 Also, if the Pg
362d0 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 77 61 73 Hdr.needSync was
362e0 20 73 65 74 20 66 6f 72 20 0a 20 20 2a 2a 20 70 set for . ** p
362f0 61 67 65 20 70 67 6e 6f 20 62 65 66 6f 72 65 20 age pgno before
36300 74 68 65 20 27 6d 6f 76 65 27 20 6f 70 65 72 61 the 'move' opera
36310 74 69 6f 6e 2c 20 69 74 20 6e 65 65 64 73 20 74 tion, it needs t
36320 6f 20 62 65 20 72 65 74 61 69 6e 65 64 20 0a 20 o be retained .
36330 20 2a 2a 20 66 6f 72 20 74 68 65 20 70 61 67 65 ** for the page
36340 20 6d 6f 76 65 64 20 74 68 65 72 65 2e 0a 20 20 moved there..
36350 2a 2f 0a 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 */. pPg->flags
36360 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 53 &= ~PGHDR_NEED_S
36370 59 4e 43 3b 0a 20 20 70 50 67 4f 6c 64 20 3d 20 YNC;. pPgOld =
36380 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 pager_lookup(pPa
36390 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 61 73 ger, pgno);. as
363a0 73 65 72 74 28 20 21 70 50 67 4f 6c 64 20 7c 7c sert( !pPgOld ||
363b0 20 70 50 67 4f 6c 64 2d 3e 6e 52 65 66 3d 3d 31 pPgOld->nRef==1
363c0 20 29 3b 0a 20 20 69 66 28 20 70 50 67 4f 6c 64 );. if( pPgOld
363d0 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 ){. pPg->fla
363e0 67 73 20 7c 3d 20 28 70 50 67 4f 6c 64 2d 3e 66 gs |= (pPgOld->f
363f0 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f lags&PGHDR_NEED_
36400 53 59 4e 43 29 3b 0a 20 20 20 20 69 66 28 20 4d SYNC);. if( M
36410 45 4d 44 42 20 29 7b 0a 20 20 20 20 20 20 2f 2a EMDB ){. /*
36420 20 44 6f 20 6e 6f 74 20 64 69 73 63 61 72 64 20 Do not discard
36430 70 61 67 65 73 20 66 72 6f 6d 20 61 6e 20 69 6e pages from an in
36440 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
36450 20 73 69 6e 63 65 20 77 65 20 6d 69 67 68 74 0a since we might.
36460 20 20 20 20 20 20 2a 2a 20 6e 65 65 64 20 74 6f ** need to
36470 20 72 6f 6c 6c 62 61 63 6b 20 6c 61 74 65 72 2e rollback later.
36480 20 20 4a 75 73 74 20 6d 6f 76 65 20 74 68 65 20 Just move the
36490 70 61 67 65 20 6f 75 74 20 6f 66 20 74 68 65 20 page out of the
364a0 77 61 79 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 way. */. as
364b0 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 sert( pPager->db
364c0 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 SizeValid );.
364d0 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 sqlite3Pcache
364e0 4d 6f 76 65 28 70 50 67 4f 6c 64 2c 20 70 50 61 Move(pPgOld, pPa
364f0 67 65 72 2d 3e 64 62 53 69 7a 65 2b 31 29 3b 0a ger->dbSize+1);.
36500 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
36510 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 sqlite3PcacheDr
36520 6f 70 28 70 50 67 4f 6c 64 29 3b 0a 20 20 20 20 op(pPgOld);.
36530 7d 0a 20 20 7d 0a 0a 20 20 6f 72 69 67 50 67 6e }. }.. origPgn
36540 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 o = pPg->pgno;.
36550 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f sqlite3PcacheMo
36560 76 65 28 70 50 67 2c 20 70 67 6e 6f 29 3b 0a 20 ve(pPg, pgno);.
36570 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 sqlite3PcacheMa
36580 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20 keDirty(pPg);.
36590 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 pPager->dbModifi
365a0 65 64 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e ed = 1;.. if( n
365b0 65 65 64 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20 eedSyncPgno ){.
365c0 20 20 20 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e /* If needSyn
365d0 63 50 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72 cPgno is non-zer
365e0 6f 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 o, then the jour
365f0 6e 61 6c 20 66 69 6c 65 20 6e 65 65 64 73 20 74 nal file needs t
36600 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e o be . ** syn
36610 63 28 29 65 64 20 62 65 66 6f 72 65 20 61 6e 79 c()ed before any
36620 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e data is written
36630 20 74 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c to database fil
36640 65 20 70 61 67 65 20 6e 65 65 64 53 79 6e 63 50 e page needSyncP
36650 67 6e 6f 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72 gno.. ** Curr
36660 65 6e 74 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70 ently, no such p
36670 61 67 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 age exists in th
36680 65 20 70 61 67 65 2d 63 61 63 68 65 20 61 6e 64 e page-cache and
36690 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 22 69 73 the . ** "is
366a0 20 6a 6f 75 72 6e 61 6c 65 64 22 20 62 69 74 76 journaled" bitv
366b0 65 63 20 66 6c 61 67 20 68 61 73 20 62 65 65 6e ec flag has been
366c0 20 73 65 74 2e 20 54 68 69 73 20 6e 65 65 64 73 set. This needs
366d0 20 74 6f 20 62 65 20 72 65 6d 65 64 69 65 64 20 to be remedied
366e0 62 79 0a 20 20 20 20 2a 2a 20 6c 6f 61 64 69 6e by. ** loadin
366f0 67 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 g the page into
36700 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 the pager-cache
36710 61 6e 64 20 73 65 74 74 69 6e 67 20 74 68 65 20 and setting the
36720 50 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 0a PgHdr.needSync .
36730 20 20 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 20 ** flag..
36740 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 **. ** If th
36750 65 20 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 e attempt to loa
36760 64 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 d the page into
36770 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 66 the page-cache f
36780 61 69 6c 73 2c 20 28 64 75 65 0a 20 20 20 20 2a ails, (due. *
36790 2a 20 74 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 * to a malloc()
367a0 6f 72 20 49 4f 20 66 61 69 6c 75 72 65 29 2c 20 or IO failure),
367b0 63 6c 65 61 72 20 74 68 65 20 62 69 74 20 69 6e clear the bit in
367c0 20 74 68 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b the pInJournal[
367d0 5d 0a 20 20 20 20 2a 2a 20 61 72 72 61 79 2e 20 ]. ** array.
367e0 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 Otherwise, if th
367f0 65 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65 64 e page is loaded
36800 20 61 6e 64 20 77 72 69 74 74 65 6e 20 61 67 61 and written aga
36810 69 6e 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 69 in in. ** thi
36820 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 s transaction, i
36830 74 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e t may be written
36840 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
36850 20 66 69 6c 65 20 62 65 66 6f 72 65 0a 20 20 20 file before.
36860 20 2a 2a 20 69 74 20 69 73 20 73 79 6e 63 65 64 ** it is synced
36870 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 into the journa
36880 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 77 61 79 l file. This way
36890 2c 20 69 74 20 6d 61 79 20 65 6e 64 20 75 70 20 , it may end up
368a0 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f in. ** the jo
368b0 75 72 6e 61 6c 20 66 69 6c 65 20 74 77 69 63 65 urnal file twice
368c0 2c 20 62 75 74 20 74 68 61 74 20 69 73 20 6e 6f , but that is no
368d0 74 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 20 t a problem..
368e0 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 **. ** The s
368f0 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 qlite3PagerGet()
36900 20 63 61 6c 6c 20 6d 61 79 20 63 61 75 73 65 20 call may cause
36910 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 the journal to s
36920 79 6e 63 2e 20 53 6f 20 6d 61 6b 65 0a 20 20 20 ync. So make.
36930 20 2a 2a 20 73 75 72 65 20 74 68 65 20 50 61 67 ** sure the Pag
36940 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 er.needSync flag
36950 20 69 73 20 73 65 74 20 74 6f 6f 2e 0a 20 20 20 is set too..
36960 20 2a 2f 0a 20 20 20 20 50 67 48 64 72 20 2a 70 */. PgHdr *p
36970 50 67 48 64 72 3b 0a 20 20 20 20 61 73 73 65 72 PgHdr;. asser
36980 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 t( pPager->needS
36990 79 6e 63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 ync );. rc =
369a0 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
369b0 70 50 61 67 65 72 2c 20 6e 65 65 64 53 79 6e 63 pPager, needSync
369c0 50 67 6e 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a Pgno, &pPgHdr);.
369d0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
369e0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 TE_OK ){. i
369f0 66 28 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 3c f( needSyncPgno<
36a00 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 =pPager->dbOrigS
36a10 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61 ize ){. a
36a20 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 ssert( pPager->p
36a30 54 6d 70 53 70 61 63 65 21 3d 30 20 29 3b 0a 20 TmpSpace!=0 );.
36a40 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 sqlite3Bi
36a50 74 76 65 63 43 6c 65 61 72 28 70 50 61 67 65 72 tvecClear(pPager
36a60 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 6e 65 ->pInJournal, ne
36a70 65 64 53 79 6e 63 50 67 6e 6f 2c 20 70 50 61 67 edSyncPgno, pPag
36a80 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a er->pTmpSpace);.
36a90 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 }. re
36aa0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
36ab0 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 pPager->needS
36ac0 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 61 73 73 ync = 1;. ass
36ad0 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 ert( pPager->noS
36ae0 79 6e 63 3d 3d 30 20 26 26 20 21 4d 45 4d 44 42 ync==0 && !MEMDB
36af0 20 29 3b 0a 20 20 20 20 70 50 67 48 64 72 2d 3e );. pPgHdr->
36b00 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e flags |= PGHDR_N
36b10 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 73 71 EED_SYNC;. sq
36b20 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 lite3PcacheMakeD
36b30 69 72 74 79 28 70 50 67 48 64 72 29 3b 0a 20 20 irty(pPgHdr);.
36b40 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
36b50 72 65 66 28 70 50 67 48 64 72 29 3b 0a 20 20 7d ref(pPgHdr);. }
36b60 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 6f 72 20 .. /*. ** For
36b70 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 an in-memory dat
36b80 61 62 61 73 65 2c 20 6d 61 6b 65 20 73 75 72 65 abase, make sure
36b90 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 the original pa
36ba0 67 65 20 63 6f 6e 74 69 6e 75 65 73 0a 20 20 2a ge continues. *
36bb0 2a 20 74 6f 20 65 78 69 73 74 2c 20 69 6e 20 63 * to exist, in c
36bc0 61 73 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 ase the transact
36bd0 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 72 6f 6c ion needs to rol
36be0 6c 20 62 61 63 6b 2e 20 20 55 73 65 20 70 50 67 l back. Use pPg
36bf0 4f 6c 64 0a 20 20 2a 2a 20 61 73 20 74 68 65 20 Old. ** as the
36c00 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 73 69 original page si
36c10 6e 63 65 20 69 74 20 68 61 73 20 61 6c 72 65 61 nce it has alrea
36c20 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 dy been allocate
36c30 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4d 45 d.. */. if( ME
36c40 4d 44 42 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 MDB ){. sqlit
36c50 65 33 50 63 61 63 68 65 4d 6f 76 65 28 70 50 67 e3PcacheMove(pPg
36c60 4f 6c 64 2c 20 6f 72 69 67 50 67 6e 6f 29 3b 0a Old, origPgno);.
36c70 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
36c80 55 6e 72 65 66 28 70 50 67 4f 6c 64 29 3b 0a 20 Unref(pPgOld);.
36c90 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }.. return SQL
36ca0 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 ITE_OK;.}.#endif
36cb0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
36cc0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
36cd0 64 61 74 61 20 66 6f 72 20 74 68 65 20 73 70 65 data for the spe
36ce0 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a cified page..*/.
36cf0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 void *sqlite3Pag
36d00 65 72 47 65 74 44 61 74 61 28 44 62 50 61 67 65 erGetData(DbPage
36d10 20 2a 70 50 67 29 7b 0a 20 20 61 73 73 65 72 74 *pPg){. assert
36d20 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 7c 7c ( pPg->nRef>0 ||
36d30 20 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 6d 65 pPg->pPager->me
36d40 6d 44 62 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 mDb );. return
36d50 70 50 67 2d 3e 70 44 61 74 61 3b 0a 7d 0a 0a 2f pPg->pData;.}../
36d60 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f *.** Return a po
36d70 69 6e 74 65 72 20 74 6f 20 74 68 65 20 50 61 67 inter to the Pag
36d80 65 72 2e 6e 45 78 74 72 61 20 62 79 74 65 73 20 er.nExtra bytes
36d90 6f 66 20 22 65 78 74 72 61 22 20 73 70 61 63 65 of "extra" space
36da0 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 .** allocated a
36db0 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 73 70 long with the sp
36dc0 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f ecified page..*/
36dd0 0a 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 .void *sqlite3Pa
36de0 67 65 72 47 65 74 45 78 74 72 61 28 44 62 50 61 gerGetExtra(DbPa
36df0 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 ge *pPg){. retu
36e00 72 6e 20 70 50 67 2d 3e 70 45 78 74 72 61 3b 0a rn pPg->pExtra;.
36e10 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 }../*.** Get/set
36e20 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 the locking-mod
36e30 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 e for this pager
36e40 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f 64 . Parameter eMod
36e50 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 0a 2a 2a e must be one.**
36e60 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e of PAGER_LOCKIN
36e70 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 50 41 47 GMODE_QUERY, PAG
36e80 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e ER_LOCKINGMODE_N
36e90 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20 50 41 47 ORMAL or .** PAG
36ea0 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 ER_LOCKINGMODE_E
36eb0 58 43 4c 55 53 49 56 45 2e 20 49 66 20 74 68 65 XCLUSIVE. If the
36ec0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f parameter is no
36ed0 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e 0a 2a t _QUERY, then.*
36ee0 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f * the locking-mo
36ef0 64 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 de is set to the
36f00 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65 64 value specified
36f10 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 ..**.** The retu
36f20 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 65 69 rned value is ei
36f30 74 68 65 72 20 50 41 47 45 52 5f 4c 4f 43 4b 49 ther PAGER_LOCKI
36f40 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f 72 NGMODE_NORMAL or
36f50 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e .** PAGER_LOCKIN
36f60 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 2c GMODE_EXCLUSIVE,
36f70 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 indicating the
36f80 63 75 72 72 65 6e 74 20 28 70 6f 73 73 69 62 6c current (possibl
36f90 79 20 75 70 64 61 74 65 64 29 0a 2a 2a 20 6c 6f y updated).** lo
36fa0 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a 2f 0a 69 cking-mode..*/.i
36fb0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c nt sqlite3PagerL
36fc0 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65 72 ockingMode(Pager
36fd0 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 4d *pPager, int eM
36fe0 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 ode){. assert(
36ff0 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 eMode==PAGER_LOC
37000 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 0a 20 KINGMODE_QUERY.
37010 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d || eM
37020 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 ode==PAGER_LOCKI
37030 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a 20 20 NGMODE_NORMAL.
37040 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f || eMo
37050 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e de==PAGER_LOCKIN
37060 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 GMODE_EXCLUSIVE
37070 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 );. assert( PAG
37080 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 ER_LOCKINGMODE_Q
37090 55 45 52 59 3c 30 20 29 3b 0a 20 20 61 73 73 65 UERY<0 );. asse
370a0 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e rt( PAGER_LOCKIN
370b0 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d 30 20 GMODE_NORMAL>=0
370c0 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 && PAGER_LOCKING
370d0 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3e 3d MODE_EXCLUSIVE>=
370e0 30 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f 64 65 0 );. if( eMode
370f0 3e 3d 30 20 26 26 20 21 70 50 61 67 65 72 2d 3e >=0 && !pPager->
37100 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 tempFile ){.
37110 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
37120 65 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f 64 eMode = (u8)eMod
37130 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 e;. }. return
37140 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 65 78 63 (int)pPager->exc
37150 6c 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a 0a 2f lusiveMode;.}../
37160 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6a 6f 75 *.** Set the jou
37170 72 6e 61 6c 2d 6d 6f 64 65 20 66 6f 72 20 74 68 rnal-mode for th
37180 69 73 20 70 61 67 65 72 2e 20 50 61 72 61 6d 65 is pager. Parame
37190 74 65 72 20 65 4d 6f 64 65 20 6d 75 73 74 20 62 ter eMode must b
371a0 65 20 6f 6e 65 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20 e one of:.**.**
371b0 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c PAGER_JOURNAL
371c0 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 2a 2a 20 20 MODE_DELETE.**
371d0 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d PAGER_JOURNALM
371e0 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a 2a 2a 20 ODE_TRUNCATE.**
371f0 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c PAGER_JOURNAL
37200 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 2a 2a 20 MODE_PERSIST.**
37210 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c PAGER_JOURNAL
37220 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 20 20 20 20 50 MODE_OFF.** P
37230 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
37240 5f 4d 45 4d 4f 52 59 0a 2a 2a 20 20 20 20 50 41 _MEMORY.** PA
37250 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
37260 57 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f WAL.**.** The jo
37270 75 72 6e 61 6c 6d 6f 64 65 20 69 73 20 73 65 74 urnalmode is set
37280 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 73 70 to the value sp
37290 65 63 69 66 69 65 64 20 69 66 20 74 68 65 20 63 ecified if the c
372a0 68 61 6e 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 hange is allowed
372b0 2e 0a 2a 2a 20 54 68 65 20 63 68 61 6e 67 65 20 ..** The change
372c0 6d 61 79 20 62 65 20 64 69 73 61 6c 6c 6f 77 65 may be disallowe
372d0 64 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 d for the follow
372e0 69 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 2a 2a 0a ing reasons:.**.
372f0 2a 2a 20 20 20 2a 20 20 41 6e 20 69 6e 2d 6d 65 ** * An in-me
37300 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 63 61 mory database ca
37310 6e 20 6f 6e 6c 79 20 68 61 76 65 20 69 74 73 20 n only have its
37320 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 73 65 74 journal_mode set
37330 20 74 6f 20 5f 4f 46 46 0a 2a 2a 20 20 20 20 20 to _OFF.**
37340 20 6f 72 20 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a or _MEMORY..**.
37350 2a 2a 20 20 20 2a 20 20 54 65 6d 70 6f 72 61 72 ** * Temporar
37360 79 20 64 61 74 61 62 61 73 65 73 20 63 61 6e 6e y databases cann
37370 6f 74 20 68 61 76 65 20 5f 57 41 4c 20 6a 6f 75 ot have _WAL jou
37380 72 6e 61 6c 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 rnalmode..**.**
37390 54 68 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64 The returned ind
373a0 69 63 61 74 65 20 74 68 65 20 63 75 72 72 65 6e icate the curren
373b0 74 20 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 t (possibly upda
373c0 74 65 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 ted) journal-mod
373d0 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 e..*/.int sqlite
373e0 33 50 61 67 65 72 53 65 74 4a 6f 75 72 6e 61 6c 3PagerSetJournal
373f0 4d 6f 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 Mode(Pager *pPag
37400 65 72 2c 20 69 6e 74 20 65 4d 6f 64 65 29 7b 0a er, int eMode){.
37410 20 20 75 38 20 65 4f 6c 64 20 3d 20 70 50 61 67 u8 eOld = pPag
37420 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b er->journalMode;
37430 20 20 20 20 2f 2a 20 50 72 69 6f 72 20 6a 6f 75 /* Prior jou
37440 72 6e 61 6c 6d 6f 64 65 20 2a 2f 0a 0a 20 20 2f rnalmode */.. /
37450 2a 20 54 68 65 20 65 4d 6f 64 65 20 70 61 72 61 * The eMode para
37460 6d 65 74 65 72 20 69 73 20 61 6c 77 61 79 73 20 meter is always
37470 76 61 6c 69 64 20 2a 2f 0a 20 20 61 73 73 65 72 valid */. asser
37480 74 28 20 20 20 20 20 20 65 4d 6f 64 65 3d 3d 50 t( eMode==P
37490 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
374a0 5f 44 45 4c 45 54 45 0a 20 20 20 20 20 20 20 20 _DELETE.
374b0 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 || eMode==PA
374c0 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
374d0 54 52 55 4e 43 41 54 45 0a 20 20 20 20 20 20 20 TRUNCATE.
374e0 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 || eMode==P
374f0 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
37500 5f 50 45 52 53 49 53 54 0a 20 20 20 20 20 20 20 _PERSIST.
37510 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 || eMode==P
37520 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
37530 5f 4f 46 46 20 0a 20 20 20 20 20 20 20 20 20 20 _OFF .
37540 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 || eMode==PAGE
37550 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 R_JOURNALMODE_WA
37560 4c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c L . |
37570 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a | eMode==PAGER_J
37580 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 OURNALMODE_MEMOR
37590 59 20 29 3b 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f Y );.. /* Do no
375a0 74 20 61 6c 6c 6f 77 20 74 68 65 20 6a 6f 75 72 t allow the jour
375b0 6e 61 6c 6d 6f 64 65 20 6f 66 20 61 20 54 45 4d nalmode of a TEM
375c0 50 20 64 61 74 61 62 61 73 65 20 74 6f 20 62 65 P database to be
375d0 20 63 68 61 6e 67 65 64 20 74 6f 20 57 41 4c 0a changed to WAL.
375e0 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 */. if( pPage
375f0 72 2d 3e 74 65 6d 70 46 69 6c 65 20 26 26 20 65 r->tempFile && e
37600 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
37610 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 29 7b 0a 20 NALMODE_WAL ){.
37620 20 20 20 61 73 73 65 72 74 28 20 65 4f 6c 64 21 assert( eOld!
37630 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
37640 44 45 5f 57 41 4c 20 29 3b 0a 20 20 20 20 65 4d DE_WAL );. eM
37650 6f 64 65 20 3d 20 65 4f 6c 64 3b 0a 20 20 7d 0a ode = eOld;. }.
37660 0a 20 20 2f 2a 20 44 6f 20 61 6c 6c 6f 77 20 74 . /* Do allow t
37670 68 65 20 6a 6f 75 72 6e 61 6c 6d 6f 64 65 20 6f he journalmode o
37680 66 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 f an in-memory d
37690 61 74 61 62 61 73 65 20 74 6f 20 62 65 20 73 65 atabase to be se
376a0 74 20 74 6f 0a 20 20 2a 2a 20 61 6e 79 74 68 69 t to. ** anythi
376b0 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 4d 45 ng other than ME
376c0 4d 4f 52 59 20 6f 72 20 4f 46 46 0a 20 20 2a 2f MORY or OFF. */
376d0 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a . if( MEMDB ){.
376e0 20 20 20 20 61 73 73 65 72 74 28 20 65 4f 6c 64 assert( eOld
376f0 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
37700 4f 44 45 5f 4d 45 4d 4f 52 59 20 7c 7c 20 65 4f ODE_MEMORY || eO
37710 6c 64 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 ld==PAGER_JOURNA
37720 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20 20 LMODE_OFF );.
37730 20 69 66 28 20 65 4d 6f 64 65 21 3d 50 41 47 45 if( eMode!=PAGE
37740 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 R_JOURNALMODE_ME
37750 4d 4f 52 59 20 26 26 20 65 4d 6f 64 65 21 3d 50 MORY && eMode!=P
37760 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
37770 5f 4f 46 46 20 29 7b 0a 20 20 20 20 20 20 65 4d _OFF ){. eM
37780 6f 64 65 20 3d 20 65 4f 6c 64 3b 0a 20 20 20 20 ode = eOld;.
37790 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 4d 6f }. }.. if( eMo
377a0 64 65 21 3d 65 4f 6c 64 20 29 7b 0a 20 20 20 20 de!=eOld ){.
377b0 2f 2a 20 57 68 65 6e 20 63 68 61 6e 67 69 6e 67 /* When changing
377c0 20 62 65 74 77 65 65 6e 20 72 6f 6c 6c 62 61 63 between rollbac
377d0 6b 20 6d 6f 64 65 73 2c 20 63 6c 6f 73 65 20 74 k modes, close t
377e0 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
377f0 70 72 69 6f 72 0a 20 20 20 20 2a 2a 20 74 6f 20 prior. ** to
37800 74 68 65 20 63 68 61 6e 67 65 2e 20 20 42 75 74 the change. But
37810 20 77 68 65 6e 20 63 68 61 6e 67 69 6e 67 20 66 when changing f
37820 72 6f 6d 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6d rom a rollback m
37830 6f 64 65 20 74 6f 20 57 41 4c 2c 20 6b 65 65 70 ode to WAL, keep
37840 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 . ** the jour
37850 6e 61 6c 20 6f 70 65 6e 20 73 69 6e 63 65 20 74 nal open since t
37860 68 65 72 65 20 69 73 20 61 20 72 6f 6c 6c 62 61 here is a rollba
37870 63 6b 2d 73 74 79 6c 65 20 74 72 61 6e 73 61 63 ck-style transac
37880 74 69 6f 6e 20 69 6e 20 70 6c 61 79 0a 20 20 20 tion in play.
37890 20 2a 2a 20 75 73 65 64 20 74 6f 20 63 6f 6e 76 ** used to conv
378a0 65 72 74 20 74 68 65 20 76 65 72 73 69 6f 6e 20 ert the version
378b0 6e 75 6d 62 65 72 73 20 69 6e 20 74 68 65 20 62 numbers in the b
378c0 74 72 65 65 20 68 65 61 64 65 72 2e 0a 20 20 20 tree header..
378d0 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 73 4f 70 */. if( isOp
378e0 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
378f0 26 26 20 65 4d 6f 64 65 21 3d 50 41 47 45 52 5f && eMode!=PAGER_
37900 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 JOURNALMODE_WAL
37910 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
37920 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e OsClose(pPager->
37930 6a 66 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 jfd);. }..
37940 20 2f 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6a /* Change the j
37950 6f 75 72 6e 61 6c 20 6d 6f 64 65 2e 20 2a 2f 0a ournal mode. */.
37960 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
37970 6e 61 6c 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d nalMode = (u8)eM
37980 6f 64 65 3b 0a 0a 20 20 20 20 2f 2a 20 57 68 65 ode;.. /* Whe
37990 6e 20 74 72 61 6e 73 69 73 74 69 6f 6e 69 6e 67 n transistioning
379a0 20 66 72 6f 6d 20 54 52 55 4e 43 41 54 45 20 6f from TRUNCATE o
379b0 72 20 50 45 52 53 49 53 54 20 74 6f 20 61 6e 79 r PERSIST to any
379c0 20 6f 74 68 65 72 20 6a 6f 75 72 6e 61 6c 0a 20 other journal.
379d0 20 20 20 2a 2a 20 6d 6f 64 65 20 65 78 63 65 70 ** mode excep
379e0 74 20 57 41 4c 20 28 61 6e 64 20 77 65 20 61 72 t WAL (and we ar
379f0 65 20 6e 6f 74 20 69 6e 20 6c 6f 63 6b 69 6e 67 e not in locking
37a00 5f 6d 6f 64 65 3d 45 58 43 4c 55 53 49 56 45 29 _mode=EXCLUSIVE)
37a10 20 74 68 65 6e 20 0a 20 20 20 20 2a 2a 20 64 65 then . ** de
37a20 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c lete the journal
37a30 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 file.. */.
37a40 20 20 61 73 73 65 72 74 28 20 28 50 41 47 45 52 assert( (PAGER
37a50 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 _JOURNALMODE_TRU
37a60 4e 43 41 54 45 20 26 20 35 29 3d 3d 31 20 29 3b NCATE & 5)==1 );
37a70 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 50 41 . assert( (PA
37a80 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
37a90 50 45 52 53 49 53 54 20 26 20 35 29 3d 3d 31 20 PERSIST & 5)==1
37aa0 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 );. assert( (
37ab0 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
37ac0 45 5f 44 45 4c 45 54 45 20 26 20 35 29 3d 3d 30 E_DELETE & 5)==0
37ad0 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
37ae0 28 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f (PAGER_JOURNALMO
37af0 44 45 5f 4d 45 4d 4f 52 59 20 26 20 35 29 3d 3d DE_MEMORY & 5)==
37b00 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 4 );. assert(
37b10 20 28 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d (PAGER_JOURNALM
37b20 4f 44 45 5f 4f 46 46 20 26 20 35 29 3d 3d 30 20 ODE_OFF & 5)==0
37b30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 );. assert( (
37b40 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
37b50 45 5f 57 41 4c 20 26 20 35 29 3d 3d 35 20 29 3b E_WAL & 5)==5 );
37b60 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 .. assert( is
37b70 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
37b80 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 78 63 6c || pPager->excl
37b90 75 73 69 76 65 4d 6f 64 65 20 29 3b 0a 20 20 20 usiveMode );.
37ba0 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 if( !pPager->ex
37bb0 63 6c 75 73 69 76 65 4d 6f 64 65 20 26 26 20 28 clusiveMode && (
37bc0 65 4f 6c 64 20 26 20 35 29 3d 3d 31 20 26 26 20 eOld & 5)==1 &&
37bd0 28 65 4d 6f 64 65 20 26 20 31 29 3d 3d 30 20 29 (eMode & 1)==0 )
37be0 7b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 {.. /* In t
37bf0 68 69 73 20 63 61 73 65 20 77 65 20 77 6f 75 6c his case we woul
37c00 64 20 6c 69 6b 65 20 74 6f 20 64 65 6c 65 74 65 d like to delete
37c10 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
37c20 65 2e 20 49 66 20 69 74 20 69 73 0a 20 20 20 20 e. If it is.
37c30 20 20 2a 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c ** not possibl
37c40 65 2c 20 74 68 65 6e 20 74 68 61 74 20 69 73 20 e, then that is
37c50 6e 6f 74 20 61 20 70 72 6f 62 6c 65 6d 2e 20 44 not a problem. D
37c60 65 6c 65 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 eleting the jour
37c70 6e 61 6c 20 66 69 6c 65 0a 20 20 20 20 20 20 2a nal file. *
37c80 2a 20 68 65 72 65 20 69 73 20 61 6e 20 6f 70 74 * here is an opt
37c90 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 6c 79 2e 0a imization only..
37ca0 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
37cb0 2a 20 42 65 66 6f 72 65 20 64 65 6c 65 74 69 6e * Before deletin
37cc0 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 g the journal fi
37cd0 6c 65 2c 20 6f 62 74 61 69 6e 20 61 20 52 45 53 le, obtain a RES
37ce0 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 ERVED lock on th
37cf0 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 e. ** datab
37d00 61 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 65 ase file. This e
37d10 6e 73 75 72 65 73 20 74 68 61 74 20 74 68 65 20 nsures that the
37d20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
37d30 6e 6f 74 20 64 65 6c 65 74 65 64 0a 20 20 20 20 not deleted.
37d40 20 20 2a 2a 20 77 68 69 6c 65 20 69 74 20 69 73 ** while it is
37d50 20 69 6e 20 75 73 65 20 62 79 20 73 6f 6d 65 20 in use by some
37d60 6f 74 68 65 72 20 63 6c 69 65 6e 74 2e 0a 20 20 other client..
37d70 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 */. int
37d80 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
37d90 0a 20 20 20 20 20 20 69 6e 74 20 73 74 61 74 65 . int state
37da0 20 3d 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 = pPager->state
37db0 3b 0a 20 20 20 20 20 20 69 66 28 20 73 74 61 74 ;. if( stat
37dc0 65 3c 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 e<PAGER_SHARED )
37dd0 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
37de0 71 6c 69 74 65 33 50 61 67 65 72 53 68 61 72 65 qlite3PagerShare
37df0 64 4c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 dLock(pPager);.
37e00 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
37e10 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d pPager->state==
37e20 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 7b 0a PAGER_SHARED ){.
37e30 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
37e40 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc==SQLITE_OK );
37e50 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
37e60 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 lite3OsLock(pPag
37e70 65 72 2d 3e 66 64 2c 20 52 45 53 45 52 56 45 44 er->fd, RESERVED
37e80 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a _LOCK);. }.
37e90 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
37ea0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
37eb0 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 sqlite3OsDele
37ec0 74 65 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c te(pPager->pVfs,
37ed0 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 pPager->zJourna
37ee0 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 l, 0);. }.
37ef0 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
37f00 49 54 45 5f 4f 4b 20 26 26 20 73 74 61 74 65 3d ITE_OK && state=
37f10 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 7b =PAGER_SHARED ){
37f20 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
37f30 4f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d OsUnlock(pPager-
37f40 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b >fd, SHARED_LOCK
37f50 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 );. }else i
37f60 66 28 20 73 74 61 74 65 3d 3d 50 41 47 45 52 5f f( state==PAGER_
37f70 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 UNLOCK ){.
37f80 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 pager_unlock(p
37f90 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a Pager);. }.
37fa0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 74 assert( st
37fb0 61 74 65 3d 3d 70 50 61 67 65 72 2d 3e 73 74 61 ate==pPager->sta
37fc0 74 65 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a te );. }. }.
37fd0 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 . /* Return the
37fe0 20 6e 65 77 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 new journal mod
37ff0 65 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 69 e */. return (i
38000 6e 74 29 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e nt)pPager->journ
38010 61 6c 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a alMode;.}../*.**
38020 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 Return the curr
38030 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 ent journal mode
38040 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
38050 50 61 67 65 72 47 65 74 4a 6f 75 72 6e 61 6c 4d PagerGetJournalM
38060 6f 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ode(Pager *pPage
38070 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e r){. return (in
38080 74 29 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 t)pPager->journa
38090 6c 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 lMode;.}../*.**
380a0 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 Return TRUE if t
380b0 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 61 he pager is in a
380c0 20 73 74 61 74 65 20 77 68 65 72 65 20 69 74 20 state where it
380d0 69 73 20 4f 4b 20 74 6f 20 63 68 61 6e 67 65 20 is OK to change
380e0 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 6d 6f the.** journalmo
380f0 64 65 2e 20 20 4a 6f 75 72 6e 61 6c 6d 6f 64 65 de. Journalmode
38100 20 63 68 61 6e 67 65 73 20 63 61 6e 20 6f 6e 6c changes can onl
38110 79 20 68 61 70 70 65 6e 20 77 68 65 6e 20 74 68 y happen when th
38120 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 e database.** is
38130 20 75 6e 6d 6f 64 69 66 69 65 64 2e 0a 2a 2f 0a unmodified..*/.
38140 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
38150 4f 6b 54 6f 43 68 61 6e 67 65 4a 6f 75 72 6e 61 OkToChangeJourna
38160 6c 4d 6f 64 65 28 50 61 67 65 72 20 2a 70 50 61 lMode(Pager *pPa
38170 67 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 67 ger){. if( pPag
38180 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 er->dbModified )
38190 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 return 0;. if(
381a0 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
381b0 6a 66 64 29 20 26 26 20 70 50 61 67 65 72 2d 3e jfd) && pPager->
381c0 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 20 29 20 72 journalOff>0 ) r
381d0 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 eturn 0;. retur
381e0 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 n 1;.}../*.** Ge
381f0 74 2f 73 65 74 20 74 68 65 20 73 69 7a 65 2d 6c t/set the size-l
38200 69 6d 69 74 20 75 73 65 64 20 66 6f 72 20 70 65 imit used for pe
38210 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c rsistent journal
38220 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 files..**.** Se
38230 74 74 69 6e 67 20 74 68 65 20 73 69 7a 65 20 6c tting the size l
38240 69 6d 69 74 20 74 6f 20 2d 31 20 6d 65 61 6e 73 imit to -1 means
38250 20 6e 6f 20 6c 69 6d 69 74 20 69 73 20 65 6e 66 no limit is enf
38260 6f 72 63 65 64 2e 0a 2a 2a 20 41 6e 20 61 74 74 orced..** An att
38270 65 6d 70 74 20 74 6f 20 73 65 74 20 61 20 6c 69 empt to set a li
38280 6d 69 74 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e mit smaller than
38290 20 2d 31 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a -1 is a no-op..
382a0 2a 2f 0a 69 36 34 20 73 71 6c 69 74 65 33 50 61 */.i64 sqlite3Pa
382b0 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 gerJournalSizeLi
382c0 6d 69 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 mit(Pager *pPage
382d0 72 2c 20 69 36 34 20 69 4c 69 6d 69 74 29 7b 0a r, i64 iLimit){.
382e0 20 20 69 66 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 if( iLimit>=-1
382f0 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
38300 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 journalSizeLimit
38310 20 3d 20 69 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 = iLimit;. }.
38320 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e return pPager->
38330 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 journalSizeLimit
38340 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
38350 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
38360 68 65 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b he pPager->pBack
38370 75 70 20 76 61 72 69 61 62 6c 65 2e 20 54 68 65 up variable. The
38380 20 62 61 63 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a backup module.*
38390 2a 20 69 6e 20 62 61 63 6b 75 70 2e 63 20 6d 61 * in backup.c ma
383a0 69 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 intains the cont
383b0 65 6e 74 20 6f 66 20 74 68 69 73 20 76 61 72 69 ent of this vari
383c0 61 62 6c 65 2e 20 54 68 69 73 20 6d 6f 64 75 6c able. This modul
383d0 65 0a 2a 2a 20 75 73 65 73 20 69 74 20 6f 70 61 e.** uses it opa
383e0 71 75 65 6c 79 20 61 73 20 61 6e 20 61 72 67 75 quely as an argu
383f0 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 42 ment to sqlite3B
38400 61 63 6b 75 70 52 65 73 74 61 72 74 28 29 20 61 ackupRestart() a
38410 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 42 61 63 nd.** sqlite3Bac
38420 6b 75 70 55 70 64 61 74 65 28 29 20 6f 6e 6c 79 kupUpdate() only
38430 2e 0a 2a 2f 0a 73 71 6c 69 74 65 33 5f 62 61 63 ..*/.sqlite3_bac
38440 6b 75 70 20 2a 2a 73 71 6c 69 74 65 33 50 61 67 kup **sqlite3Pag
38450 65 72 42 61 63 6b 75 70 50 74 72 28 50 61 67 65 erBackupPtr(Page
38460 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 r *pPager){. re
38470 74 75 72 6e 20 26 70 50 61 67 65 72 2d 3e 70 42 turn &pPager->pB
38480 61 63 6b 75 70 3b 0a 7d 0a 0a 23 69 66 6e 64 65 ackup;.}..#ifnde
38490 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 f SQLITE_OMIT_WA
384a0 4c 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e L./*.** This fun
384b0 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
384c0 77 68 65 6e 20 74 68 65 20 75 73 65 72 20 69 6e when the user in
384d0 76 6f 6b 65 73 20 22 50 52 41 47 4d 41 20 63 68 vokes "PRAGMA ch
384e0 65 63 6b 70 6f 69 6e 74 22 2e 0a 2a 2f 0a 69 6e eckpoint"..*/.in
384f0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 68 t sqlite3PagerCh
38500 65 63 6b 70 6f 69 6e 74 28 50 61 67 65 72 20 2a eckpoint(Pager *
38510 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 pPager){. int r
38520 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
38530 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 57 61 if( pPager->pWa
38540 6c 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a 42 75 l ){. u8 *zBu
38550 66 20 3d 20 28 75 38 20 2a 29 70 50 61 67 65 72 f = (u8 *)pPager
38560 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20 ->pTmpSpace;.
38570 20 72 63 20 3d 20 73 71 6c 69 74 65 33 57 61 6c rc = sqlite3Wal
38580 43 68 65 63 6b 70 6f 69 6e 74 28 70 50 61 67 65 Checkpoint(pPage
38590 72 2d 3e 70 57 61 6c 2c 0a 20 20 20 20 20 20 20 r->pWal,.
385a0 20 28 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 (pPager->noSync
385b0 20 3f 20 30 20 3a 20 70 50 61 67 65 72 2d 3e 73 ? 0 : pPager->s
385c0 79 6e 63 5f 66 6c 61 67 73 29 2c 0a 20 20 20 20 ync_flags),.
385d0 20 20 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65 pPager->page
385e0 53 69 7a 65 2c 20 7a 42 75 66 0a 20 20 20 20 29 Size, zBuf. )
385f0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
38600 63 3b 0a 7d 0a 0a 69 6e 74 20 73 71 6c 69 74 65 c;.}..int sqlite
38610 33 50 61 67 65 72 57 61 6c 43 61 6c 6c 62 61 63 3PagerWalCallbac
38620 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 k(Pager *pPager)
38630 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
38640 65 33 57 61 6c 43 61 6c 6c 62 61 63 6b 28 70 50 e3WalCallback(pP
38650 61 67 65 72 2d 3e 70 57 61 6c 29 3b 0a 7d 0a 0a ager->pWal);.}..
38660 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 /*.** Return tru
38670 65 20 69 66 20 74 68 65 20 75 6e 64 65 72 6c 79 e if the underly
38680 69 6e 67 20 56 46 53 20 66 6f 72 20 74 68 65 20 ing VFS for the
38690 67 69 76 65 6e 20 70 61 67 65 72 20 73 75 70 70 given pager supp
386a0 6f 72 74 73 20 74 68 65 0a 2a 2a 20 70 72 69 6d orts the.** prim
386b0 69 74 69 76 65 73 20 6e 65 63 65 73 73 61 72 79 itives necessary
386c0 20 66 6f 72 20 77 72 69 74 65 2d 61 68 65 61 64 for write-ahead
386d0 20 6c 6f 67 67 69 6e 67 2e 0a 2a 2f 0a 69 6e 74 logging..*/.int
386e0 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 61 6c sqlite3PagerWal
386f0 53 75 70 70 6f 72 74 65 64 28 50 61 67 65 72 20 Supported(Pager
38700 2a 70 50 61 67 65 72 2