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 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 ..**.** @(#) $Id
0340: 3a 20 70 61 67 65 72 2e 63 2c 76 20 31 2e 36 32 : pager.c,v 1.62
0350: 36 20 32 30 30 39 2f 30 37 2f 32 35 20 31 39 3a 6 2009/07/25 19:
0360: 33 31 3a 33 32 20 64 72 68 20 45 78 70 20 24 0a 31:32 drh Exp $.
0370: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
0380: 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 23 69 E_OMIT_DISKIO.#i
0390: 6e 63 6c 75 64 65 20 22 73 71 6c 69 74 65 49 6e nclude "sqliteIn
03a0: 74 2e 68 22 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 t.h"../*.** Macr
03b0: 6f 73 20 66 6f 72 20 74 72 6f 75 62 6c 65 73 68 os for troublesh
03c0: 6f 6f 74 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c ooting. Normall
03d0: 79 20 74 75 72 6e 65 64 20 6f 66 66 0a 2a 2f 0a y turned off.*/.
03e0: 23 69 66 20 30 0a 69 6e 74 20 73 71 6c 69 74 65 #if 0.int sqlite
03f0: 33 50 61 67 65 72 54 72 61 63 65 3d 31 3b 20 20 3PagerTrace=1;
0400: 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c /* True to enabl
0410: 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a 23 64 65 e tracing */.#de
0420: 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65 62 75 fine sqlite3Debu
0430: 67 50 72 69 6e 74 66 20 70 72 69 6e 74 66 0a 23 gPrintf printf.#
0440: 64 65 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 define PAGERTRAC
0450: 45 28 58 29 20 20 20 20 20 69 66 28 20 73 71 6c E(X) if( sql
0460: 69 74 65 33 50 61 67 65 72 54 72 61 63 65 20 29 ite3PagerTrace )
0470: 7b 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 { sqlite3DebugPr
0480: 69 6e 74 66 20 58 3b 20 7d 0a 23 65 6c 73 65 0a intf X; }.#else.
0490: 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52 41 #define PAGERTRA
04a0: 43 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a CE(X).#endif../*
04b0: 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
04c0: 67 20 74 77 6f 20 6d 61 63 72 6f 73 20 61 72 65 g two macros are
04d0: 20 75 73 65 64 20 77 69 74 68 69 6e 20 74 68 65 used within the
04e0: 20 50 41 47 45 52 54 52 41 43 45 28 29 20 6d 61 PAGERTRACE() ma
04f0: 63 72 6f 73 20 61 62 6f 76 65 0a 2a 2a 20 74 6f cros above.** to
0500: 20 70 72 69 6e 74 20 6f 75 74 20 66 69 6c 65 2d print out file-
0510: 64 65 73 63 72 69 70 74 6f 72 73 2e 20 0a 2a 2a descriptors. .**
0520: 0a 2a 2a 20 50 41 47 45 52 49 44 28 29 20 74 61 .** PAGERID() ta
0530: 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f kes a pointer to
0540: 20 61 20 50 61 67 65 72 20 73 74 72 75 63 74 20 a Pager struct
0550: 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e 74 2e as its argument.
0560: 20 54 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 74 The.** associat
0570: 65 64 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 ed file-descript
0580: 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 or is returned.
0590: 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 29 20 74 FILEHANDLEID() t
05a0: 61 6b 65 73 20 61 6e 20 73 71 6c 69 74 65 33 5f akes an sqlite3_
05b0: 66 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 20 61 file.** struct a
05c0: 73 20 69 74 73 20 61 72 67 75 6d 65 6e 74 2e 0a s its argument..
05d0: 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 */.#define PAGER
05e0: 49 44 28 70 29 20 28 28 69 6e 74 29 28 70 2d 3e ID(p) ((int)(p->
05f0: 66 64 29 29 0a 23 64 65 66 69 6e 65 20 46 49 4c fd)).#define FIL
0600: 45 48 41 4e 44 4c 45 49 44 28 66 64 29 20 28 28 EHANDLEID(fd) ((
0610: 69 6e 74 29 66 64 29 0a 0a 2f 2a 0a 2a 2a 20 54 int)fd)../*.** T
0620: 68 65 20 70 61 67 65 20 63 61 63 68 65 20 61 73 he page cache as
0630: 20 61 20 77 68 6f 6c 65 20 69 73 20 61 6c 77 61 a whole is alwa
0640: 79 73 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 ys in one of the
0650: 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 following.** st
0660: 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 41 ates:.**.** PA
0670: 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 GER_UNLOCK
0680: 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 The page cache
0690: 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
06a0: 79 20 72 65 61 64 69 6e 67 20 6f 72 20 0a 2a 2a y reading or .**
06b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
06c0: 20 20 20 20 20 20 20 77 72 69 74 69 6e 67 20 74 writing t
06d0: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
06e0: 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 0a 2a . There is no.*
06f0: 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
0700: 20 20 20 20 20 20 20 20 64 61 74 61 20 68 65 6c data hel
0710: 64 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 54 68 d in memory. Th
0720: 69 73 20 69 73 20 74 68 65 20 69 6e 69 74 69 61 is is the initia
0730: 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 l.**
0740: 20 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65 state
0750: 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f ..**.** PAGER_
0760: 53 48 41 52 45 44 20 20 20 20 20 20 20 20 54 68 SHARED Th
0770: 65 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 e page cache is
0780: 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 reading the data
0790: 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20 base..**
07a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 57 W
07b0: 72 69 74 69 6e 67 20 69 73 20 6e 6f 74 20 70 65 riting is not pe
07c0: 72 6d 69 74 74 65 64 2e 20 20 54 68 65 72 65 20 rmitted. There
07d0: 63 61 6e 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 can be.**
07e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
07f0: 6d 75 6c 74 69 70 6c 65 20 72 65 61 64 65 72 73 multiple readers
0800: 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 accessing the s
0810: 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 ame database.**
0820: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0830: 20 20 20 20 20 20 66 69 6c 65 20 61 74 20 74 68 file at th
0840: 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a 0a e same time..**.
0850: 2a 2a 20 20 20 50 41 47 45 52 5f 52 45 53 45 52 ** PAGER_RESER
0860: 56 45 44 20 20 20 20 20 20 54 68 69 73 20 70 72 VED This pr
0870: 6f 63 65 73 73 20 68 61 73 20 72 65 73 65 72 76 ocess has reserv
0880: 65 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ed the database
0890: 66 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a 20 20 for writing.**
08a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
08b0: 20 20 20 20 20 62 75 74 20 68 61 73 20 6e 6f 74 but has not
08c0: 20 79 65 74 20 6d 61 64 65 20 61 6e 79 20 63 68 yet made any ch
08d0: 61 6e 67 65 73 2e 20 20 4f 6e 6c 79 20 6f 6e 65 anges. Only one
08e0: 20 70 72 6f 63 65 73 73 0a 2a 2a 20 20 20 20 20 process.**
08f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0900: 20 20 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20 at a time can
0910: 72 65 73 65 72 76 65 20 74 68 65 20 64 61 74 61 reserve the data
0920: 62 61 73 65 2e 20 20 54 68 65 20 6f 72 69 67 69 base. The origi
0930: 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 nal.**
0940: 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 dat
0950: 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 6e abase file has n
0960: 6f 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 ot been modified
0970: 20 73 6f 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20 so other.**
0980: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0990: 20 20 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 processes may
09a0: 20 73 74 69 6c 6c 20 62 65 20 72 65 61 64 69 6e still be readin
09b0: 67 20 74 68 65 20 6f 6e 2d 64 69 73 6b 0a 2a 2a g the on-disk.**
09c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
09d0: 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 database
09e0: 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 file..**.** PA
09f0: 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20 20 GER_EXCLUSIVE
0a00: 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 The page cache
0a10: 20 69 73 20 77 72 69 74 69 6e 67 20 74 68 65 20 is writing the
0a20: 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20 database..**
0a30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0a40: 20 20 20 41 63 63 65 73 73 20 69 73 20 65 78 63 Access is exc
0a50: 6c 75 73 69 76 65 2e 20 20 4e 6f 20 6f 74 68 65 lusive. No othe
0a60: 72 20 70 72 6f 63 65 73 73 65 73 20 6f 72 0a 2a r processes or.*
0a70: 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
0a80: 20 20 20 20 20 20 20 20 74 68 72 65 61 64 73 20 threads
0a90: 63 61 6e 20 62 65 20 72 65 61 64 69 6e 67 20 6f can be reading o
0aa0: 72 20 77 72 69 74 69 6e 67 20 77 68 69 6c 65 20 r writing while
0ab0: 6f 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 one.**
0ac0: 20 20 20 20 20 20 20 20 20 20 20 20 20 70 72 6f pro
0ad0: 63 65 73 73 20 69 73 20 77 72 69 74 69 6e 67 2e cess is writing.
0ae0: 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 53 .**.** PAGER_S
0af0: 59 4e 43 45 44 20 20 20 20 20 20 20 20 54 68 65 YNCED The
0b00: 20 70 61 67 65 72 20 6d 6f 76 65 73 20 74 6f 20 pager moves to
0b10: 74 68 69 73 20 73 74 61 74 65 20 66 72 6f 6d 20 this state from
0b20: 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 0a PAGER_EXCLUSIVE.
0b30: 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
0b40: 20 20 20 20 20 20 20 20 20 61 66 74 65 72 20 61 after a
0b50: 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 68 ll dirty pages h
0b60: 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e ave been written
0b70: 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 to the.**
0b80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0b90: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
0ba0: 6e 64 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 nd the file has
0bb0: 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f 0a 2a been synced to.*
0bc0: 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
0bd0: 20 20 20 20 20 20 20 20 64 69 73 6b 2e 20 41 6c disk. Al
0be0: 6c 20 74 68 61 74 20 72 65 6d 61 69 6e 73 20 74 l that remains t
0bf0: 6f 20 64 6f 20 69 73 20 74 6f 20 72 65 6d 6f 76 o do is to remov
0c00: 65 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 e or.**
0c10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 72 tr
0c20: 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e uncate the journ
0c30: 61 6c 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 al file and the
0c40: 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 transaction .**
0c50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0c60: 20 20 20 20 20 20 77 69 6c 6c 20 62 65 20 63 6f will be co
0c70: 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 mmitted..**.** T
0c80: 68 65 20 70 61 67 65 20 63 61 63 68 65 20 63 6f he page cache co
0c90: 6d 65 73 20 75 70 20 69 6e 20 50 41 47 45 52 5f mes up in PAGER_
0ca0: 55 4e 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 UNLOCK. The fir
0cb0: 73 74 20 74 69 6d 65 20 61 0a 2a 2a 20 73 71 6c st time a.** sql
0cc0: 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 6f ite3PagerGet() o
0cd0: 63 63 75 72 73 2c 20 74 68 65 20 73 74 61 74 65 ccurs, the state
0ce0: 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20 transitions to
0cf0: 50 41 47 45 52 5f 53 48 41 52 45 44 2e 0a 2a 2a PAGER_SHARED..**
0d00: 20 41 66 74 65 72 20 61 6c 6c 20 70 61 67 65 73 After all pages
0d10: 20 68 61 76 65 20 62 65 65 6e 20 72 65 6c 65 61 have been relea
0d20: 73 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 sed using sqlite
0d30: 5f 70 61 67 65 5f 75 6e 72 65 66 28 29 2c 0a 2a _page_unref(),.*
0d40: 2a 20 74 68 65 20 73 74 61 74 65 20 74 72 61 6e * the state tran
0d50: 73 69 74 69 6f 6e 73 20 62 61 63 6b 20 74 6f 20 sitions back to
0d60: 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54 PAGER_UNLOCK. T
0d70: 68 65 20 66 69 72 73 74 20 74 69 6d 65 0a 2a 2a he first time.**
0d80: 20 74 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 that sqlite3Pag
0d90: 65 72 57 72 69 74 65 28 29 20 69 73 20 63 61 6c erWrite() is cal
0da0: 6c 65 64 2c 20 74 68 65 20 73 74 61 74 65 20 74 led, the state t
0db0: 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 0a 2a 2a ransitions to.**
0dc0: 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e PAGER_RESERVED.
0dd0: 20 20 28 4e 6f 74 65 20 74 68 61 74 20 73 71 6c (Note that sql
0de0: 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 ite3PagerWrite()
0df0: 20 63 61 6e 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20 can only be.**
0e00: 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 6f 75 74 called on an out
0e10: 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 77 68 standing page wh
0e20: 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 74 ich means that t
0e30: 68 65 20 70 61 67 65 72 20 6d 75 73 74 0a 2a 2a he pager must.**
0e40: 20 62 65 20 69 6e 20 50 41 47 45 52 5f 53 48 41 be in PAGER_SHA
0e50: 52 45 44 20 62 65 66 6f 72 65 20 69 74 20 74 72 RED before it tr
0e60: 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20 50 41 47 ansitions to PAG
0e70: 45 52 5f 52 45 53 45 52 56 45 44 2e 29 0a 2a 2a ER_RESERVED.).**
0e80: 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 PAGER_RESERVED
0e90: 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 72 65 means that there
0ea0: 20 69 73 20 61 6e 20 6f 70 65 6e 20 72 6f 6c 6c is an open roll
0eb0: 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a back journal..**
0ec0: 20 54 68 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 The transition
0ed0: 74 6f 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 to PAGER_EXCLUSI
0ee0: 56 45 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65 VE occurs before
0ef0: 20 61 6e 79 20 63 68 61 6e 67 65 73 0a 2a 2a 20 any changes.**
0f00: 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 are made to the
0f10: 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 database file, t
0f20: 68 6f 75 67 68 20 77 72 69 74 65 73 20 74 6f 20 hough writes to
0f30: 74 68 65 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 the rollback.**
0f40: 6a 6f 75 72 6e 61 6c 20 6f 63 63 75 72 73 20 77 journal occurs w
0f50: 69 74 68 20 6a 75 73 74 20 50 41 47 45 52 5f 52 ith just PAGER_R
0f60: 45 53 45 52 56 45 44 2e 20 20 41 66 74 65 72 20 ESERVED. After
0f70: 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 an sqlite3PagerR
0f80: 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 6f 72 20 ollback().** or
0f90: 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d sqlite3PagerComm
0fa0: 69 74 50 68 61 73 65 54 77 6f 28 29 2c 20 74 68 itPhaseTwo(), th
0fb0: 65 20 73 74 61 74 65 20 63 61 6e 20 67 6f 20 62 e state can go b
0fc0: 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41 ack to PAGER_SHA
0fd0: 52 45 44 2c 0a 2a 2a 20 6f 72 20 69 74 20 63 61 RED,.** or it ca
0fe0: 6e 20 73 74 61 79 20 61 74 20 50 41 47 45 52 5f n stay at PAGER_
0ff0: 45 58 43 4c 55 53 49 56 45 20 69 66 20 77 65 20 EXCLUSIVE if we
1000: 61 72 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65 are in exclusive
1010: 20 61 63 63 65 73 73 20 6d 6f 64 65 2e 0a 2a 2f access mode..*/
1020: 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 55 .#define PAGER_U
1030: 4e 4c 4f 43 4b 20 20 20 20 20 20 30 0a 23 64 65 NLOCK 0.#de
1040: 66 69 6e 65 20 50 41 47 45 52 5f 53 48 41 52 45 fine PAGER_SHARE
1050: 44 20 20 20 20 20 20 31 20 20 20 2f 2a 20 73 61 D 1 /* sa
1060: 6d 65 20 61 73 20 53 48 41 52 45 44 5f 4c 4f 43 me as SHARED_LOC
1070: 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 K */.#define PAG
1080: 45 52 5f 52 45 53 45 52 56 45 44 20 20 20 20 32 ER_RESERVED 2
1090: 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 52 45 /* same as RE
10a0: 53 45 52 56 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 SERVED_LOCK */.#
10b0: 64 65 66 69 6e 65 20 50 41 47 45 52 5f 45 58 43 define PAGER_EXC
10c0: 4c 55 53 49 56 45 20 20 20 34 20 20 20 2f 2a 20 LUSIVE 4 /*
10d0: 73 61 6d 65 20 61 73 20 45 58 43 4c 55 53 49 56 same as EXCLUSIV
10e0: 45 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e E_LOCK */.#defin
10f0: 65 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 e PAGER_SYNCED
1100: 20 20 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 5../*.** A m
1110: 61 63 72 6f 20 75 73 65 64 20 66 6f 72 20 69 6e acro used for in
1120: 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f 64 65 63 voking the codec
1130: 20 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 if there is one
1140: 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
1150: 45 5f 48 41 53 5f 43 4f 44 45 43 0a 23 20 64 65 E_HAS_CODEC.# de
1160: 66 69 6e 65 20 43 4f 44 45 43 31 28 50 2c 44 2c fine CODEC1(P,D,
1170: 4e 2c 58 2c 45 29 20 5c 0a 20 20 20 20 69 66 28 N,X,E) \. if(
1180: 20 50 2d 3e 78 43 6f 64 65 63 20 26 26 20 50 2d P->xCodec && P-
1190: 3e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 >xCodec(P->pCode
11a0: 63 2c 44 2c 4e 2c 58 29 3d 3d 30 20 29 7b 20 45 c,D,N,X)==0 ){ E
11b0: 3b 20 7d 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 ; }.# define COD
11c0: 45 43 32 28 50 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 EC2(P,D,N,X,E,O)
11d0: 20 5c 0a 20 20 20 20 69 66 28 20 50 2d 3e 78 43 \. if( P->xC
11e0: 6f 64 65 63 3d 3d 30 20 29 7b 20 4f 3d 28 63 68 odec==0 ){ O=(ch
11f0: 61 72 2a 29 44 3b 20 7d 65 6c 73 65 20 5c 0a 20 ar*)D; }else \.
1200: 20 20 20 69 66 28 20 28 4f 3d 28 63 68 61 72 2a if( (O=(char*
1210: 29 28 50 2d 3e 78 43 6f 64 65 63 28 50 2d 3e 70 )(P->xCodec(P->p
1220: 43 6f 64 65 63 2c 44 2c 4e 2c 58 29 29 29 3d 3d Codec,D,N,X)))==
1230: 30 20 29 7b 20 45 3b 20 7d 0a 23 65 6c 73 65 0a 0 ){ E; }.#else.
1240: 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 31 28 # define CODEC1(
1250: 50 2c 44 2c 4e 2c 58 2c 45 29 20 20 20 2f 2a 20 P,D,N,X,E) /*
1260: 4e 4f 2d 4f 50 20 2a 2f 0a 23 20 64 65 66 69 6e NO-OP */.# defin
1270: 65 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 e CODEC2(P,D,N,X
1280: 2c 45 2c 4f 29 20 4f 3d 28 63 68 61 72 2a 29 44 ,E,O) O=(char*)D
1290: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
12a0: 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 he maximum allow
12b0: 65 64 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 20 ed sector size.
12c0: 31 36 4d 42 2e 20 49 66 20 74 68 65 20 78 53 65 16MB. If the xSe
12d0: 63 74 6f 72 73 69 7a 65 28 29 20 6d 65 74 68 6f ctorsize() metho
12e0: 64 20 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 d .** returns a
12f0: 76 61 6c 75 65 20 6c 61 72 67 65 72 20 74 68 61 value larger tha
1300: 6e 20 74 68 69 73 2c 20 74 68 65 6e 20 4d 41 58 n this, then MAX
1310: 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 73 20 _SECTOR_SIZE is
1320: 75 73 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a used instead..**
1330: 20 54 68 69 73 20 63 6f 75 6c 64 20 63 6f 6e 63 This could conc
1340: 65 69 76 61 62 6c 79 20 63 61 75 73 65 20 63 6f eivably cause co
1350: 72 72 75 70 74 69 6f 6e 20 66 6f 6c 6c 6f 77 69 rruption followi
1360: 6e 67 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 ng a power failu
1370: 72 65 20 6f 6e 0a 2a 2a 20 73 75 63 68 20 61 20 re on.** such a
1380: 73 79 73 74 65 6d 2e 20 54 68 69 73 20 69 73 20 system. This is
1390: 63 75 72 72 65 6e 74 6c 79 20 61 6e 20 75 6e 64 currently an und
13a0: 6f 63 75 6d 65 6e 74 65 64 20 6c 69 6d 69 74 2e ocumented limit.
13b0: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 58 5f .*/.#define MAX_
13c0: 53 45 43 54 4f 52 5f 53 49 5a 45 20 30 78 30 31 SECTOR_SIZE 0x01
13d0: 30 30 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 00000../*.** An
13e0: 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
13f0: 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
1400: 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ure is allocated
1410: 20 66 6f 72 20 65 61 63 68 20 61 63 74 69 76 65 for each active
1420: 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 20 61 6e .** savepoint an
1430: 64 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e d statement tran
1440: 73 61 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 73 saction in the s
1450: 79 73 74 65 6d 2e 20 41 6c 6c 20 73 75 63 68 20 ystem. All such
1460: 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 61 72 structures.** ar
1470: 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 e stored in the
1480: 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 Pager.aSavepoint
1490: 5b 5d 20 61 72 72 61 79 2c 20 77 68 69 63 68 20 [] array, which
14a0: 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 is allocated and
14b0: 0a 2a 2a 20 72 65 73 69 7a 65 64 20 75 73 69 6e .** resized usin
14c0: 67 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 g sqlite3Realloc
14d0: 28 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 ()..**.** When a
14e0: 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 63 72 savepoint is cr
14f0: 65 61 74 65 64 2c 20 74 68 65 20 50 61 67 65 72 eated, the Pager
1500: 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 Savepoint.iHdrOf
1510: 66 73 65 74 20 66 69 65 6c 64 20 69 73 0a 2a 2a fset field is.**
1520: 20 73 65 74 20 74 6f 20 30 2e 20 49 66 20 61 20 set to 0. If a
1530: 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 69 journal-header i
1540: 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 s written into t
1550: 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
1560: 77 68 69 6c 65 0a 2a 2a 20 74 68 65 20 73 61 76 while.** the sav
1570: 65 70 6f 69 6e 74 20 69 73 20 61 63 74 69 76 65 epoint is active
1580: 2c 20 74 68 65 6e 20 69 48 64 72 4f 66 66 73 65 , then iHdrOffse
1590: 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 t is set to the
15a0: 62 79 74 65 20 6f 66 66 73 65 74 20 0a 2a 2a 20 byte offset .**
15b0: 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c immediately foll
15c0: 6f 77 69 6e 67 20 74 68 65 20 6c 61 73 74 20 6a owing the last j
15d0: 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 20 77 72 ournal record wr
15e0: 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6d itten into the m
15f0: 61 69 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 62 ain.** journal b
1600: 65 66 6f 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 efore the journa
1610: 6c 2d 68 65 61 64 65 72 2e 20 54 68 69 73 20 69 l-header. This i
1620: 73 20 72 65 71 75 69 72 65 64 20 64 75 72 69 6e s required durin
1630: 67 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 72 g savepoint.** r
1640: 6f 6c 6c 62 61 63 6b 20 28 73 65 65 20 70 61 67 ollback (see pag
1650: 65 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f erPlaybackSavepo
1660: 69 6e 74 28 29 29 2e 0a 2a 2f 0a 74 79 70 65 64 int())..*/.typed
1670: 65 66 20 73 74 72 75 63 74 20 50 61 67 65 72 53 ef struct PagerS
1680: 61 76 65 70 6f 69 6e 74 20 50 61 67 65 72 53 61 avepoint PagerSa
1690: 76 65 70 6f 69 6e 74 3b 0a 73 74 72 75 63 74 20 vepoint;.struct
16a0: 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 7b PagerSavepoint {
16b0: 0a 20 20 69 36 34 20 69 4f 66 66 73 65 74 3b 20 . i64 iOffset;
16c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16d0: 2f 2a 20 53 74 61 72 74 69 6e 67 20 6f 66 66 73 /* Starting offs
16e0: 65 74 20 69 6e 20 6d 61 69 6e 20 6a 6f 75 72 6e et in main journ
16f0: 61 6c 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 al */. i64 iHdr
1700: 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 Offset;
1710: 20 20 20 20 20 2f 2a 20 53 65 65 20 61 62 6f 76 /* See abov
1720: 65 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 e */. Bitvec *p
1730: 49 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 InSavepoint;
1740: 20 20 20 20 2f 2a 20 53 65 74 20 6f 66 20 70 61 /* Set of pa
1750: 67 65 73 20 69 6e 20 74 68 69 73 20 73 61 76 65 ges in this save
1760: 70 6f 69 6e 74 20 2a 2f 0a 20 20 50 67 6e 6f 20 point */. Pgno
1770: 6e 4f 72 69 67 3b 20 20 20 20 20 20 20 20 20 20 nOrig;
1780: 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 /* Origi
1790: 6e 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 nal number of pa
17a0: 67 65 73 20 69 6e 20 66 69 6c 65 20 2a 2f 0a 20 ges in file */.
17b0: 20 50 67 6e 6f 20 69 53 75 62 52 65 63 3b 20 20 Pgno iSubRec;
17c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17d0: 20 49 6e 64 65 78 20 6f 66 20 66 69 72 73 74 20 Index of first
17e0: 72 65 63 6f 72 64 20 69 6e 20 73 75 62 2d 6a 6f record in sub-jo
17f0: 75 72 6e 61 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a urnal */.};../*.
1800: 2a 2a 20 41 20 6f 70 65 6e 20 70 61 67 65 20 63 ** A open page c
1810: 61 63 68 65 20 69 73 20 61 6e 20 69 6e 73 74 61 ache is an insta
1820: 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
1830: 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a wing structure..
1840: 2a 2a 0a 2a 2a 20 65 72 72 43 6f 64 65 0a 2a 2a **.** errCode.**
1850: 0a 2a 2a 20 20 20 50 61 67 65 72 2e 65 72 72 43 .** Pager.errC
1860: 6f 64 65 20 6d 61 79 20 62 65 20 73 65 74 20 74 ode may be set t
1870: 6f 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 o SQLITE_IOERR,
1880: 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2c 20 SQLITE_CORRUPT,
1890: 6f 72 0a 2a 2a 20 20 20 6f 72 20 53 51 4c 49 54 or.** or SQLIT
18a0: 45 5f 46 55 4c 4c 2e 20 4f 6e 63 65 20 6f 6e 65 E_FULL. Once one
18b0: 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74 68 of the first th
18c0: 72 65 65 20 65 72 72 6f 72 73 20 6f 63 63 75 72 ree errors occur
18d0: 73 2c 20 69 74 20 70 65 72 73 69 73 74 73 0a 2a s, it persists.*
18e0: 2a 20 20 20 61 6e 64 20 69 73 20 72 65 74 75 72 * and is retur
18f0: 6e 65 64 20 61 73 20 74 68 65 20 72 65 73 75 6c ned as the resul
1900: 74 20 6f 66 20 65 76 65 72 79 20 6d 61 6a 6f 72 t of every major
1910: 20 70 61 67 65 72 20 41 50 49 20 63 61 6c 6c 2e pager API call.
1920: 20 20 54 68 65 0a 2a 2a 20 20 20 53 51 4c 49 54 The.** SQLIT
1930: 45 5f 46 55 4c 4c 20 72 65 74 75 72 6e 20 63 6f E_FULL return co
1940: 64 65 20 69 73 20 73 6c 69 67 68 74 6c 79 20 64 de is slightly d
1950: 69 66 66 65 72 65 6e 74 2e 20 49 74 20 70 65 72 ifferent. It per
1960: 73 69 73 74 73 20 6f 6e 6c 79 20 75 6e 74 69 6c sists only until
1970: 20 74 68 65 0a 2a 2a 20 20 20 6e 65 78 74 20 73 the.** next s
1980: 75 63 63 65 73 73 66 75 6c 20 72 6f 6c 6c 62 61 uccessful rollba
1990: 63 6b 20 69 73 20 70 65 72 66 6f 72 6d 65 64 20 ck is performed
19a0: 6f 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 on the pager cac
19b0: 68 65 2e 20 41 6c 73 6f 2c 0a 2a 2a 20 20 20 53 he. Also,.** S
19c0: 51 4c 49 54 45 5f 46 55 4c 4c 20 64 6f 65 73 20 QLITE_FULL does
19d0: 6e 6f 74 20 61 66 66 65 63 74 20 74 68 65 20 73 not affect the s
19e0: 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 qlite3PagerGet()
19f0: 20 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67 65 and sqlite3Page
1a00: 72 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 20 20 41 rLookup().** A
1a10: 50 49 73 2c 20 74 68 65 79 20 6d 61 79 20 73 74 PIs, they may st
1a20: 69 6c 6c 20 62 65 20 75 73 65 64 20 73 75 63 63 ill be used succ
1a30: 65 73 73 66 75 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 essfully..**.**
1a40: 64 62 53 69 7a 65 56 61 6c 69 64 2c 20 64 62 53 dbSizeValid, dbS
1a50: 69 7a 65 2c 20 64 62 4f 72 69 67 53 69 7a 65 2c ize, dbOrigSize,
1a60: 20 64 62 46 69 6c 65 53 69 7a 65 0a 2a 2a 0a 2a dbFileSize.**.*
1a70: 2a 20 20 20 4d 61 6e 61 67 69 6e 67 20 74 68 65 * Managing the
1a80: 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 size of the dat
1a90: 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61 abase file in pa
1aa0: 67 65 73 20 69 73 20 61 20 6c 69 74 74 6c 65 20 ges is a little
1ab0: 63 6f 6d 70 6c 69 63 61 74 65 64 2e 0a 2a 2a 20 complicated..**
1ac0: 20 20 54 68 65 20 76 61 72 69 61 62 6c 65 20 50 The variable P
1ad0: 61 67 65 72 2e 64 62 53 69 7a 65 20 63 6f 6e 74 ager.dbSize cont
1ae0: 61 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 ains the number
1af0: 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 74 68 of pages that th
1b00: 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 e database.**
1b10: 69 6d 61 67 65 20 63 75 72 72 65 6e 74 6c 79 20 image currently
1b20: 63 6f 6e 74 61 69 6e 73 2e 20 41 73 20 74 68 65 contains. As the
1b30: 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 database image
1b40: 67 72 6f 77 73 20 6f 72 20 73 68 72 69 6e 6b 73 grows or shrinks
1b50: 20 74 68 69 73 0a 2a 2a 20 20 20 76 61 72 69 61 this.** varia
1b60: 62 6c 65 20 69 73 20 75 70 64 61 74 65 64 2e 20 ble is updated.
1b70: 54 68 65 20 76 61 72 69 61 62 6c 65 20 50 61 67 The variable Pag
1b80: 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 63 6f er.dbFileSize co
1b90: 6e 74 61 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 ntains the numbe
1ba0: 72 0a 2a 2a 20 20 20 6f 66 20 70 61 67 65 73 20 r.** of pages
1bb0: 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
1bc0: 66 69 6c 65 2e 20 54 68 69 73 20 6d 61 79 20 62 file. This may b
1bd0: 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d e different from
1be0: 20 50 61 67 65 72 2e 64 62 53 69 7a 65 0a 2a 2a Pager.dbSize.**
1bf0: 20 20 20 69 66 20 73 6f 6d 65 20 70 61 67 65 73 if some pages
1c00: 20 68 61 76 65 20 62 65 65 6e 20 61 70 70 65 6e have been appen
1c10: 64 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 ded to the datab
1c20: 61 73 65 20 69 6d 61 67 65 20 62 75 74 20 6e 6f ase image but no
1c30: 74 20 79 65 74 20 77 72 69 74 74 65 6e 0a 2a 2a t yet written.**
1c40: 20 20 20 6f 75 74 20 66 72 6f 6d 20 74 68 65 20 out from the
1c50: 63 61 63 68 65 20 74 6f 20 74 68 65 20 61 63 74 cache to the act
1c60: 75 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b ual file on disk
1c70: 2e 20 4f 72 20 69 66 20 74 68 65 20 69 6d 61 67 . Or if the imag
1c80: 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 20 20 e has been.**
1c90: 74 72 75 6e 63 61 74 65 64 20 62 79 20 61 6e 20 truncated by an
1ca0: 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 61 63 75 incremental-vacu
1cb0: 75 6d 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 um operation. Th
1cc0: 65 20 50 61 67 65 72 2e 64 62 4f 72 69 67 53 69 e Pager.dbOrigSi
1cd0: 7a 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 20 ze variable.**
1ce0: 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 75 contains the nu
1cf0: 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
1d00: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d the database im
1d10: 61 67 65 20 77 68 65 6e 20 74 68 65 20 63 75 72 age when the cur
1d20: 72 65 6e 74 0a 2a 2a 20 20 20 74 72 61 6e 73 61 rent.** transa
1d30: 63 74 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 ction was opened
1d40: 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f . The contents o
1d50: 66 20 61 6c 6c 20 74 68 72 65 65 20 6f 66 20 74 f all three of t
1d60: 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 20 69 hese variables i
1d70: 73 0a 2a 2a 20 20 20 6f 6e 6c 79 20 67 75 61 72 s.** only guar
1d80: 61 6e 74 65 65 64 20 74 6f 20 62 65 20 63 6f 72 anteed to be cor
1d90: 72 65 63 74 20 69 66 20 74 68 65 20 62 6f 6f 6c rect if the bool
1da0: 65 61 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 ean Pager.dbSize
1db0: 56 61 6c 69 64 20 69 73 20 74 72 75 65 2e 0a 2a Valid is true..*
1dc0: 2a 0a 2a 2a 20 20 20 54 4f 44 4f 3a 20 55 6e 64 *.** TODO: Und
1dd0: 65 72 20 77 68 61 74 20 63 6f 6e 64 69 74 69 6f er what conditio
1de0: 6e 73 20 69 73 20 64 62 53 69 7a 65 56 61 6c 69 ns is dbSizeVali
1df0: 64 20 73 65 74 3f 20 43 6c 65 61 72 65 64 3f 0a d set? Cleared?.
1e00: 2a 2a 0a 2a 2a 20 63 68 61 6e 67 65 43 6f 75 6e **.** changeCoun
1e10: 74 44 6f 6e 65 0a 2a 2a 0a 2a 2a 20 20 20 54 68 tDone.**.** Th
1e20: 69 73 20 62 6f 6f 6c 65 61 6e 20 76 61 72 69 61 is boolean varia
1e30: 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d ble is used to m
1e40: 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 ake sure that th
1e50: 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
1e60: 20 0a 2a 2a 20 20 20 28 74 68 65 20 34 2d 62 79 .** (the 4-by
1e70: 74 65 20 68 65 61 64 65 72 20 66 69 65 6c 64 20 te header field
1e80: 61 74 20 62 79 74 65 20 6f 66 66 73 65 74 20 32 at byte offset 2
1e90: 34 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 4 of the databas
1ea0: 65 20 66 69 6c 65 29 20 69 73 20 0a 2a 2a 20 20 e file) is .**
1eb0: 20 6e 6f 74 20 75 70 64 61 74 65 64 20 6d 6f 72 not updated mor
1ec0: 65 20 6f 66 74 65 6e 20 74 68 61 6e 20 6e 65 63 e often than nec
1ed0: 65 73 73 61 72 79 2e 20 0a 2a 2a 0a 2a 2a 20 20 essary. .**.**
1ee0: 20 49 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 It is set to tr
1ef0: 75 65 20 77 68 65 6e 20 74 68 65 20 63 68 61 6e ue when the chan
1f00: 67 65 2d 63 6f 75 6e 74 65 72 20 66 69 65 6c 64 ge-counter field
1f10: 20 69 73 20 75 70 64 61 74 65 64 2c 20 77 68 69 is updated, whi
1f20: 63 68 20 0a 2a 2a 20 20 20 63 61 6e 20 6f 6e 6c ch .** can onl
1f30: 79 20 68 61 70 70 65 6e 20 69 66 20 61 6e 20 65 y happen if an e
1f40: 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 xclusive lock is
1f50: 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 held on the dat
1f60: 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 20 abase file..**
1f70: 20 49 74 20 69 73 20 63 6c 65 61 72 65 64 20 28 It is cleared (
1f80: 73 65 74 20 74 6f 20 66 61 6c 73 65 29 20 77 68 set to false) wh
1f90: 65 6e 65 76 65 72 20 61 6e 20 65 78 63 6c 75 73 enever an exclus
1fa0: 69 76 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a 20 ive lock is .**
1fb0: 20 20 72 65 6c 69 6e 71 75 69 73 68 65 64 20 6f relinquished o
1fc0: 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
1fd0: 69 6c 65 2e 20 45 61 63 68 20 74 69 6d 65 20 61 ile. Each time a
1fe0: 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1ff0: 63 6f 6d 6d 69 74 74 65 64 2c 0a 2a 2a 20 20 20 committed,.**
2000: 54 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 The changeCountD
2010: 6f 6e 65 20 66 6c 61 67 20 69 73 20 69 6e 73 70 one flag is insp
2020: 65 63 74 65 64 2e 20 49 66 20 69 74 20 69 73 20 ected. If it is
2030: 74 72 75 65 2c 20 74 68 65 20 77 6f 72 6b 20 6f true, the work o
2040: 66 0a 2a 2a 20 20 20 75 70 64 61 74 69 6e 67 20 f.** updating
2050: 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
2060: 65 72 20 69 73 20 6f 6d 69 74 74 65 64 20 66 6f er is omitted fo
2070: 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 r the current tr
2080: 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a ansaction..**.**
2090: 20 20 20 54 68 69 73 20 6d 65 63 68 61 6e 69 73 This mechanis
20a0: 6d 20 6d 65 61 6e 73 20 74 68 61 74 20 77 68 65 m means that whe
20b0: 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 n running in exc
20c0: 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 61 20 63 lusive mode, a c
20d0: 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 20 20 onnection .**
20e0: 6e 65 65 64 20 6f 6e 6c 79 20 75 70 64 61 74 65 need only update
20f0: 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e the change-coun
2100: 74 65 72 20 6f 6e 63 65 2c 20 66 6f 72 20 74 68 ter once, for th
2110: 65 20 66 69 72 73 74 20 74 72 61 6e 73 61 63 74 e first transact
2120: 69 6f 6e 0a 2a 2a 20 20 20 63 6f 6d 6d 69 74 74 ion.** committ
2130: 65 64 2e 0a 2a 2a 0a 2a 2a 20 64 62 4d 6f 64 69 ed..**.** dbModi
2140: 66 69 65 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 fied.**.** The
2150: 20 64 62 4d 6f 64 69 66 69 65 64 20 66 6c 61 67 dbModified flag
2160: 20 69 73 20 73 65 74 20 77 68 65 6e 65 76 65 72 is set whenever
2170: 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 a database page
2180: 20 69 73 20 64 69 72 74 69 65 64 2e 0a 2a 2a 20 is dirtied..**
2190: 20 20 49 74 20 69 73 20 63 6c 65 61 72 65 64 20 It is cleared
21a0: 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 65 61 at the end of ea
21b0: 63 68 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a ch transaction..
21c0: 2a 2a 0a 2a 2a 20 20 20 49 74 20 69 73 20 75 73 **.** It is us
21d0: 65 64 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 ed when committi
21e0: 6e 67 20 6f 72 20 6f 74 68 65 72 77 69 73 65 20 ng or otherwise
21f0: 65 6e 64 69 6e 67 20 61 20 74 72 61 6e 73 61 63 ending a transac
2200: 74 69 6f 6e 2e 20 49 66 0a 2a 2a 20 20 20 74 68 tion. If.** th
2210: 65 20 64 62 4d 6f 64 69 66 69 65 64 20 66 6c 61 e dbModified fla
2220: 67 20 69 73 20 63 6c 65 61 72 20 74 68 65 6e 20 g is clear then
2230: 6c 65 73 73 20 77 6f 72 6b 20 68 61 73 20 74 6f less work has to
2240: 20 62 65 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 be done..**.**
2250: 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 0a 2a journalStarted.*
2260: 2a 0a 2a 2a 20 20 20 54 68 69 73 20 66 6c 61 67 *.** This flag
2270: 20 69 73 20 73 65 74 20 77 68 65 6e 65 76 65 72 is set whenever
2280: 20 74 68 65 20 74 68 65 20 6d 61 69 6e 20 6a 6f the the main jo
2290: 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 2e urnal is synced.
22a0: 20 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 70 6f .**.** The po
22b0: 69 6e 74 20 6f 66 20 74 68 69 73 20 66 6c 61 67 int of this flag
22c0: 20 69 73 20 74 68 61 74 20 69 74 20 6d 75 73 74 is that it must
22d0: 20 62 65 20 73 65 74 20 61 66 74 65 72 20 74 68 be set after th
22e0: 65 20 0a 2a 2a 20 20 20 66 69 72 73 74 20 6a 6f e .** first jo
22f0: 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 6e 20 urnal header in
2300: 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 a journal file h
2310: 61 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20 74 as been synced t
2320: 6f 20 64 69 73 6b 2e 0a 2a 2a 20 20 20 41 66 74 o disk..** Aft
2330: 65 72 20 74 68 69 73 20 68 61 73 20 68 61 70 70 er this has happ
2340: 65 6e 65 64 2c 20 6e 65 77 20 70 61 67 65 73 20 ened, new pages
2350: 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 appended to the
2360: 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 20 20 64 database .** d
2370: 6f 20 6e 6f 74 20 6e 65 65 64 20 74 68 65 20 50 o not need the P
2380: 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 GHDR_NEED_SYNC f
2390: 6c 61 67 20 73 65 74 2c 20 61 73 20 74 68 65 79 lag set, as they
23a0: 20 64 6f 20 6e 6f 74 20 6e 65 65 64 0a 2a 2a 20 do not need.**
23b0: 20 20 74 6f 20 77 61 69 74 20 66 6f 72 20 61 20 to wait for a
23c0: 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 20 62 65 66 journal sync bef
23d0: 6f 72 65 20 74 68 65 79 20 63 61 6e 20 62 65 20 ore they can be
23e0: 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 0a 2a written out to.*
23f0: 2a 20 20 20 74 68 65 20 64 61 74 61 62 61 73 65 * the database
2400: 20 66 69 6c 65 20 28 73 65 65 20 66 75 6e 63 74 file (see funct
2410: 69 6f 6e 20 70 61 67 65 72 5f 77 72 69 74 65 28 ion pager_write(
2420: 29 29 2e 0a 2a 2a 20 20 20 0a 2a 2a 20 73 65 74 ))..** .** set
2430: 4d 61 73 74 65 72 0a 2a 2a 0a 2a 2a 20 20 20 54 Master.**.** T
2440: 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 his variable is
2450: 75 73 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 used to ensure t
2460: 68 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 6a hat the master j
2470: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
2480: 0a 2a 2a 20 20 20 28 69 66 20 61 6e 79 29 20 69 .** (if any) i
2490: 73 20 6f 6e 6c 79 20 77 72 69 74 74 65 6e 20 69 s only written i
24a0: 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 nto the journal
24b0: 66 69 6c 65 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a file once..**.**
24c0: 20 20 20 57 68 65 6e 20 63 6f 6d 6d 69 74 74 69 When committi
24d0: 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e ng a transaction
24e0: 2c 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 , the master jou
24f0: 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 28 rnal file name (
2500: 69 66 20 61 6e 79 29 0a 2a 2a 20 20 20 6d 61 79 if any).** may
2510: 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f be written into
2520: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
2530: 65 20 77 68 69 6c 65 20 74 68 65 20 70 61 67 65 e while the page
2540: 72 20 69 73 20 73 74 69 6c 6c 20 69 6e 0a 2a 2a r is still in.**
2550: 20 20 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 PAGER_RESERVE
2560: 44 20 73 74 61 74 65 20 28 73 65 65 20 43 6f 6d D state (see Com
2570: 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 66 6f mitPhaseOne() fo
2580: 72 20 74 68 65 20 61 63 74 69 6f 6e 29 2e 20 49 r the action). I
2590: 74 0a 2a 2a 20 20 20 74 68 65 6e 20 61 74 74 65 t.** then atte
25a0: 6d 70 74 73 20 74 6f 20 75 70 67 72 61 64 65 20 mpts to upgrade
25b0: 74 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 to an exclusive
25c0: 6c 6f 63 6b 2e 20 49 66 20 74 68 69 73 20 61 74 lock. If this at
25d0: 74 65 6d 70 74 0a 2a 2a 20 20 20 66 61 69 6c 73 tempt.** fails
25e0: 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 42 55 , then SQLITE_BU
25f0: 53 59 20 6d 61 79 20 62 65 20 72 65 74 75 72 6e SY may be return
2600: 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 20 61 ed to the user a
2610: 6e 64 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 20 nd the user.**
2620: 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74 6f 20 may attempt to
2630: 63 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 commit the trans
2640: 61 63 74 69 6f 6e 20 61 67 61 69 6e 20 6c 61 74 action again lat
2650: 65 72 20 28 63 61 6c 6c 69 6e 67 0a 2a 2a 20 20 er (calling.**
2660: 20 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 CommitPhaseOne(
2670: 29 20 61 67 61 69 6e 29 2e 20 54 68 69 73 20 66 ) again). This f
2680: 6c 61 67 20 69 73 20 75 73 65 64 20 74 6f 20 65 lag is used to e
2690: 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 0a nsure that the .
26a0: 2a 2a 20 20 20 6d 61 73 74 65 72 20 6a 6f 75 72 ** master jour
26b0: 6e 61 6c 20 6e 61 6d 65 20 69 73 20 6f 6e 6c 79 nal name is only
26c0: 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
26d0: 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 65 journal file the
26e0: 20 66 69 72 73 74 0a 2a 2a 20 20 20 74 69 6d 65 first.** time
26f0: 20 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 CommitPhaseOne(
2700: 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a ) is called..**.
2710: 2a 2a 20 64 6f 4e 6f 74 53 79 6e 63 0a 2a 2a 0a ** doNotSync.**.
2720: 2a 2a 20 20 20 54 68 69 73 20 76 61 72 69 61 62 ** This variab
2730: 6c 65 20 69 73 20 73 65 74 20 61 6e 64 20 63 6c le is set and cl
2740: 65 61 72 65 64 20 62 79 20 73 71 6c 69 74 65 33 eared by sqlite3
2750: 50 61 67 65 72 57 72 69 74 65 28 29 2e 0a 2a 2a PagerWrite()..**
2760: 0a 2a 2a 20 6e 65 65 64 53 79 6e 63 0a 2a 2a 0a .** needSync.**.
2770: 2a 2a 20 20 20 54 4f 44 4f 3a 20 49 74 20 6d 69 ** TODO: It mi
2780: 67 68 74 20 62 65 20 65 61 73 69 65 72 20 74 6f ght be easier to
2790: 20 73 65 74 20 74 68 69 73 20 76 61 72 69 61 62 set this variab
27a0: 6c 65 20 69 6e 20 77 72 69 74 65 4a 6f 75 72 6e le in writeJourn
27b0: 61 6c 48 64 72 28 29 0a 2a 2a 20 20 20 61 6e 64 alHdr().** and
27c0: 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 writeMasterJour
27d0: 6e 61 6c 28 29 20 6f 6e 6c 79 2e 20 43 68 61 6e nal() only. Chan
27e0: 67 65 20 69 74 73 20 6d 65 61 6e 69 6e 67 20 74 ge its meaning t
27f0: 6f 20 22 75 6e 73 79 6e 63 65 64 20 64 61 74 61 o "unsynced data
2800: 0a 2a 2a 20 20 20 68 61 73 20 62 65 65 6e 20 77 .** has been w
2810: 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f ritten to the jo
2820: 75 72 6e 61 6c 22 2e 0a 2a 2a 0a 2a 2a 20 73 75 urnal"..**.** su
2830: 62 6a 49 6e 4d 65 6d 6f 72 79 0a 2a 2a 0a 2a 2a bjInMemory.**.**
2840: 20 20 20 54 68 69 73 20 69 73 20 61 20 62 6f 6f This is a boo
2850: 6c 65 61 6e 20 76 61 72 69 61 62 6c 65 2e 20 49 lean variable. I
2860: 66 20 74 72 75 65 2c 20 74 68 65 6e 20 61 6e 79 f true, then any
2870: 20 72 65 71 75 69 72 65 64 20 73 75 62 2d 6a 6f required sub-jo
2880: 75 72 6e 61 6c 0a 2a 2a 20 20 20 69 73 20 6f 70 urnal.** is op
2890: 65 6e 65 64 20 61 73 20 61 6e 20 69 6e 2d 6d 65 ened as an in-me
28a0: 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c mory journal fil
28b0: 65 2e 20 49 66 20 66 61 6c 73 65 2c 20 74 68 65 e. If false, the
28c0: 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 20 n in-memory.**
28d0: 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 61 72 sub-journals ar
28e0: 65 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72 20 e only used for
28f0: 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 72 20 in-memory pager
2900: 66 69 6c 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 files..*/.struct
2910: 20 50 61 67 65 72 20 7b 0a 20 20 73 71 6c 69 74 Pager {. sqlit
2920: 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 e3_vfs *pVfs;
2930: 20 20 20 20 20 20 20 2f 2a 20 4f 53 20 66 75 6e /* OS fun
2940: 63 74 69 6f 6e 73 20 74 6f 20 75 73 65 20 66 6f ctions to use fo
2950: 72 20 49 4f 20 2a 2f 0a 20 20 75 38 20 65 78 63 r IO */. u8 exc
2960: 6c 75 73 69 76 65 4d 6f 64 65 3b 20 20 20 20 20 lusiveMode;
2970: 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e /* Boolean
2980: 2e 20 54 72 75 65 20 69 66 20 6c 6f 63 6b 69 6e . True if lockin
2990: 67 5f 6d 6f 64 65 3d 3d 45 58 43 4c 55 53 49 56 g_mode==EXCLUSIV
29a0: 45 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 E */. u8 journa
29b0: 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 lMode;
29c0: 20 20 20 2f 2a 20 4f 6e 20 6f 66 20 74 68 65 20 /* On of the
29d0: 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
29e0: 45 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 E_* values */.
29f0: 75 38 20 75 73 65 4a 6f 75 72 6e 61 6c 3b 20 20 u8 useJournal;
2a00: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 /* U
2a10: 73 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f se a rollback jo
2a20: 75 72 6e 61 6c 20 6f 6e 20 74 68 69 73 20 66 69 urnal on this fi
2a30: 6c 65 20 2a 2f 0a 20 20 75 38 20 6e 6f 52 65 61 le */. u8 noRea
2a40: 64 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 dlock;
2a50: 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f /* Do not bo
2a60: 74 68 65 72 20 74 6f 20 6f 62 74 61 69 6e 20 72 ther to obtain r
2a70: 65 61 64 6c 6f 63 6b 73 20 2a 2f 0a 20 20 75 38 eadlocks */. u8
2a80: 20 6e 6f 53 79 6e 63 3b 20 20 20 20 20 20 20 20 noSync;
2a90: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 /* Do
2aa0: 6e 6f 74 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 not sync the jou
2ab0: 72 6e 61 6c 20 69 66 20 74 72 75 65 20 2a 2f 0a rnal if true */.
2ac0: 20 20 75 38 20 66 75 6c 6c 53 79 6e 63 3b 20 20 u8 fullSync;
2ad0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
2ae0: 20 44 6f 20 65 78 74 72 61 20 73 79 6e 63 73 20 Do extra syncs
2af0: 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
2b00: 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 20 2a 2f or robustness */
2b10: 0a 20 20 75 38 20 73 79 6e 63 5f 66 6c 61 67 73 . u8 sync_flags
2b20: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
2b30: 2a 20 4f 6e 65 20 6f 66 20 53 59 4e 43 5f 4e 4f * One of SYNC_NO
2b40: 52 4d 41 4c 20 6f 72 20 53 59 4e 43 5f 46 55 4c RMAL or SYNC_FUL
2b50: 4c 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70 46 69 L */. u8 tempFi
2b60: 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 le;
2b70: 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 /* zFilename
2b80: 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 is a temporary f
2b90: 69 6c 65 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 ile */. u8 read
2ba0: 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20 Only;
2bb0: 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 /* True for
2bc0: 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 a read-only dat
2bd0: 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 6d 65 abase */. u8 me
2be0: 6d 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 mDb;
2bf0: 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 /* True t
2c00: 6f 20 69 6e 68 69 62 69 74 20 61 6c 6c 20 66 69 o inhibit all fi
2c10: 6c 65 20 49 2f 4f 20 2a 2f 0a 0a 20 20 2f 2a 20 le I/O */.. /*
2c20: 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c The following bl
2c30: 6f 63 6b 20 63 6f 6e 74 61 69 6e 73 20 74 68 6f ock contains tho
2c40: 73 65 20 63 6c 61 73 73 20 6d 65 6d 62 65 72 73 se class members
2c50: 20 74 68 61 74 20 61 72 65 20 64 79 6e 61 6d 69 that are dynami
2c60: 63 61 6c 6c 79 0a 20 20 2a 2a 20 6d 6f 64 69 66 cally. ** modif
2c70: 69 65 64 20 64 75 72 69 6e 67 20 6e 6f 72 6d 61 ied during norma
2c80: 6c 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 54 68 l operations. Th
2c90: 65 20 6f 74 68 65 72 20 76 61 72 69 61 62 6c 65 e other variable
2ca0: 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 s in this struct
2cb0: 75 72 65 0a 20 20 2a 2a 20 61 72 65 20 65 69 74 ure. ** are eit
2cc0: 68 65 72 20 63 6f 6e 73 74 61 6e 74 20 74 68 72 her constant thr
2cd0: 6f 75 67 68 6f 75 74 20 74 68 65 20 6c 69 66 65 oughout the life
2ce0: 74 69 6d 65 20 6f 66 20 74 68 65 20 70 61 67 65 time of the page
2cf0: 72 2c 20 6f 72 20 65 6c 73 65 0a 20 20 2a 2a 20 r, or else. **
2d00: 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 63 6f used to store co
2d10: 6e 66 69 67 75 72 61 74 69 6f 6e 20 70 61 72 61 nfiguration para
2d20: 6d 65 74 65 72 73 20 74 68 61 74 20 61 66 66 65 meters that affe
2d30: 63 74 20 74 68 65 20 77 61 79 20 74 68 65 20 70 ct the way the p
2d40: 61 67 65 72 20 0a 20 20 2a 2a 20 6f 70 65 72 61 ager . ** opera
2d50: 74 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 tes.. **. ** T
2d60: 68 65 20 27 73 74 61 74 65 27 20 76 61 72 69 61 he 'state' varia
2d70: 62 6c 65 20 69 73 20 64 65 73 63 72 69 62 65 64 ble is described
2d80: 20 69 6e 20 6d 6f 72 65 20 64 65 74 61 69 6c 20 in more detail
2d90: 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 0a 20 along with the.
2da0: 20 2a 2a 20 64 65 73 63 72 69 70 74 69 6f 6e 73 ** descriptions
2db0: 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 of the values i
2dc0: 74 20 6d 61 79 20 74 61 6b 65 20 2d 20 50 41 47 t may take - PAG
2dd0: 45 52 5f 55 4e 4c 4f 43 4b 20 65 74 63 2e 20 4d ER_UNLOCK etc. M
2de0: 61 6e 79 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 any of the. **
2df0: 6f 74 68 65 72 20 76 61 72 69 61 62 6c 65 73 20 other variables
2e00: 69 6e 20 74 68 69 73 20 62 6c 6f 63 6b 20 61 72 in this block ar
2e10: 65 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 74 e described in t
2e20: 68 65 20 63 6f 6d 6d 65 6e 74 20 64 69 72 65 63 he comment direc
2e30: 74 6c 79 20 0a 20 20 2a 2a 20 61 62 6f 76 65 20 tly . ** above
2e40: 74 68 69 73 20 63 6c 61 73 73 20 64 65 66 69 6e this class defin
2e50: 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 75 38 ition.. */. u8
2e60: 20 73 74 61 74 65 3b 20 20 20 20 20 20 20 20 20 state;
2e70: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 41 47 /* PAG
2e80: 45 52 5f 55 4e 4c 4f 43 4b 2c 20 5f 53 48 41 52 ER_UNLOCK, _SHAR
2e90: 45 44 2c 20 5f 52 45 53 45 52 56 45 44 2c 20 65 ED, _RESERVED, e
2ea0: 74 63 2e 20 2a 2f 0a 20 20 75 38 20 64 62 4d 6f tc. */. u8 dbMo
2eb0: 64 69 66 69 65 64 3b 20 20 20 20 20 20 20 20 20 dified;
2ec0: 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
2ed0: 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 63 68 there are any ch
2ee0: 61 6e 67 65 73 20 74 6f 20 74 68 65 20 44 62 20 anges to the Db
2ef0: 2a 2f 0a 20 20 75 38 20 6e 65 65 64 53 79 6e 63 */. u8 needSync
2f00: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
2f10: 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 66 /* True if an f
2f20: 73 79 6e 63 28 29 20 69 73 20 6e 65 65 64 65 64 sync() is needed
2f30: 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 on the journal
2f40: 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c 53 */. u8 journalS
2f50: 74 61 72 74 65 64 3b 20 20 20 20 20 20 20 20 20 tarted;
2f60: 20 2f 2a 20 54 72 75 65 20 69 66 20 68 65 61 64 /* True if head
2f70: 65 72 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 69 73 er of journal is
2f80: 20 73 79 6e 63 65 64 20 2a 2f 0a 20 20 75 38 20 synced */. u8
2f90: 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 3b changeCountDone;
2fa0: 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 /* Set
2fb0: 61 66 74 65 72 20 69 6e 63 72 65 6d 65 6e 74 69 after incrementi
2fc0: 6e 67 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f ng the change-co
2fd0: 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 73 65 unter */. u8 se
2fe0: 74 4d 61 73 74 65 72 3b 20 20 20 20 20 20 20 20 tMaster;
2ff0: 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
3000: 66 20 61 20 6d 2d 6a 20 6e 61 6d 65 20 68 61 73 f a m-j name has
3010: 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f been written to
3020: 20 6a 72 6e 6c 20 2a 2f 0a 20 20 75 38 20 64 6f jrnl */. u8 do
3030: 4e 6f 74 53 79 6e 63 3b 20 20 20 20 20 20 20 20 NotSync;
3040: 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 /* Boolea
3050: 6e 2e 20 57 68 69 6c 65 20 74 72 75 65 2c 20 64 n. While true, d
3060: 6f 20 6e 6f 74 20 73 70 69 6c 6c 20 74 68 65 20 o not spill the
3070: 63 61 63 68 65 20 2a 2f 0a 20 20 75 38 20 64 62 cache */. u8 db
3080: 53 69 7a 65 56 61 6c 69 64 3b 20 20 20 20 20 20 SizeValid;
3090: 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 77 68 /* Set wh
30a0: 65 6e 20 64 62 53 69 7a 65 20 69 73 20 63 6f 72 en dbSize is cor
30b0: 72 65 63 74 20 2a 2f 0a 20 20 75 38 20 73 75 62 rect */. u8 sub
30c0: 6a 49 6e 4d 65 6d 6f 72 79 3b 20 20 20 20 20 20 jInMemory;
30d0: 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f /* True to
30e0: 20 75 73 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 use in-memory s
30f0: 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a 20 ub-journals */.
3100: 20 50 67 6e 6f 20 64 62 53 69 7a 65 3b 20 20 20 Pgno dbSize;
3110: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
3120: 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
3130: 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
3140: 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 4f 72 69 67 */. Pgno dbOrig
3150: 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 Size;
3160: 20 2f 2a 20 64 62 53 69 7a 65 20 62 65 66 6f 72 /* dbSize befor
3170: 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 e the current tr
3180: 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 50 ansaction */. P
3190: 67 6e 6f 20 64 62 46 69 6c 65 53 69 7a 65 3b 20 gno dbFileSize;
31a0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
31b0: 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
31c0: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
31d0: 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 le */. int errC
31e0: 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ode;
31f0: 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 73 65 /* One of se
3200: 76 65 72 61 6c 20 6b 69 6e 64 73 20 6f 66 20 65 veral kinds of e
3210: 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e rrors */. int n
3220: 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 Rec;
3230: 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 73 20 /* Pages
3240: 6a 6f 75 72 6e 61 6c 6c 65 64 20 73 69 6e 63 65 journalled since
3250: 20 6c 61 73 74 20 6a 2d 68 65 61 64 65 72 20 77 last j-header w
3260: 72 69 74 74 65 6e 20 2a 2f 0a 20 20 75 33 32 20 ritten */. u32
3270: 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20 20 cksumInit;
3280: 20 20 20 20 20 20 20 20 2f 2a 20 51 75 61 73 69 /* Quasi
3290: 2d 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 61 64 -random value ad
32a0: 64 65 64 20 74 6f 20 65 76 65 72 79 20 63 68 65 ded to every che
32b0: 63 6b 73 75 6d 20 2a 2f 0a 20 20 75 33 32 20 6e cksum */. u32 n
32c0: 53 75 62 52 65 63 3b 20 20 20 20 20 20 20 20 20 SubRec;
32d0: 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
32e0: 20 6f 66 20 72 65 63 6f 72 64 73 20 77 72 69 74 of records writ
32f0: 74 65 6e 20 74 6f 20 73 75 62 2d 6a 6f 75 72 6e ten to sub-journ
3300: 61 6c 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a al */. Bitvec *
3310: 70 49 6e 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 pInJournal;
3320: 20 20 20 20 2f 2a 20 4f 6e 65 20 62 69 74 20 66 /* One bit f
3330: 6f 72 20 65 61 63 68 20 70 61 67 65 20 69 6e 20 or each page in
3340: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
3350: 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 e */. sqlite3_f
3360: 69 6c 65 20 2a 66 64 3b 20 20 20 20 20 20 20 20 ile *fd;
3370: 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 /* File descr
3380: 69 70 74 6f 72 20 66 6f 72 20 64 61 74 61 62 61 iptor for databa
3390: 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f se */. sqlite3_
33a0: 66 69 6c 65 20 2a 6a 66 64 3b 20 20 20 20 20 20 file *jfd;
33b0: 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 /* File desc
33c0: 72 69 70 74 6f 72 20 66 6f 72 20 6d 61 69 6e 20 riptor for main
33d0: 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 73 71 6c journal */. sql
33e0: 69 74 65 33 5f 66 69 6c 65 20 2a 73 6a 66 64 3b ite3_file *sjfd;
33f0: 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 /* File
3400: 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 descriptor for
3410: 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 sub-journal */.
3420: 20 69 36 34 20 6a 6f 75 72 6e 61 6c 4f 66 66 3b i64 journalOff;
3430: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
3440: 43 75 72 72 65 6e 74 20 77 72 69 74 65 20 6f 66 Current write of
3450: 66 73 65 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 fset in the jour
3460: 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 nal file */. i6
3470: 34 20 6a 6f 75 72 6e 61 6c 48 64 72 3b 20 20 20 4 journalHdr;
3480: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 /* Byt
3490: 65 20 6f 66 66 73 65 74 20 74 6f 20 70 72 65 76 e offset to prev
34a0: 69 6f 75 73 20 6a 6f 75 72 6e 61 6c 20 68 65 61 ious journal hea
34b0: 64 65 72 20 2a 2f 0a 20 20 50 61 67 65 72 53 61 der */. PagerSa
34c0: 76 65 70 6f 69 6e 74 20 2a 61 53 61 76 65 70 6f vepoint *aSavepo
34d0: 69 6e 74 3b 20 2f 2a 20 41 72 72 61 79 20 6f 66 int; /* Array of
34e0: 20 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e active savepoin
34f0: 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 76 ts */. int nSav
3500: 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20 epoint;
3510: 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
3520: 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61 53 61 elements in aSa
3530: 76 65 70 6f 69 6e 74 5b 5d 20 2a 2f 0a 20 20 63 vepoint[] */. c
3540: 68 61 72 20 64 62 46 69 6c 65 56 65 72 73 5b 31 har dbFileVers[1
3550: 36 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 68 6]; /* Ch
3560: 61 6e 67 65 73 20 77 68 65 6e 65 76 65 72 20 64 anges whenever d
3570: 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 68 61 atabase file cha
3580: 6e 67 65 73 20 2a 2f 0a 20 20 75 33 32 20 73 65 nges */. u32 se
3590: 63 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20 ctorSize;
35a0: 20 20 20 20 20 20 2f 2a 20 41 73 73 75 6d 65 64 /* Assumed
35b0: 20 73 65 63 74 6f 72 20 73 69 7a 65 20 64 75 72 sector size dur
35c0: 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a ing rollback */.
35d0: 0a 20 20 75 31 36 20 6e 45 78 74 72 61 3b 20 20 . u16 nExtra;
35e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
35f0: 2a 20 41 64 64 20 74 68 69 73 20 6d 61 6e 79 20 * Add this many
3600: 62 79 74 65 73 20 74 6f 20 65 61 63 68 20 69 6e bytes to each in
3610: 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 2a 2f 0a -memory page */.
3620: 20 20 69 31 36 20 6e 52 65 73 65 72 76 65 3b 20 i16 nReserve;
3630: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
3640: 20 4e 75 6d 62 65 72 20 6f 66 20 75 6e 75 73 65 Number of unuse
3650: 64 20 62 79 74 65 73 20 61 74 20 65 6e 64 20 6f d bytes at end o
3660: 66 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 f each page */.
3670: 20 75 33 32 20 76 66 73 46 6c 61 67 73 3b 20 20 u32 vfsFlags;
3680: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
3690: 46 6c 61 67 73 20 66 6f 72 20 73 71 6c 69 74 65 Flags for sqlite
36a0: 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 3_vfs.xOpen() */
36b0: 0a 20 20 69 6e 74 20 70 61 67 65 53 69 7a 65 3b . int pageSize;
36c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
36d0: 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
36e0: 73 20 69 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 s in a page */.
36f0: 20 50 67 6e 6f 20 6d 78 50 67 6e 6f 3b 20 20 20 Pgno mxPgno;
3700: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
3710: 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 Maximum allowed
3720: 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 size of the data
3730: 62 61 73 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a base */. char *
3740: 7a 46 69 6c 65 6e 61 6d 65 3b 20 20 20 20 20 20 zFilename;
3750: 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
3760: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
3770: 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4a le */. char *zJ
3780: 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 ournal;
3790: 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
37a0: 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
37b0: 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 79 */. int (*xBusy
37c0: 48 61 6e 64 6c 65 72 29 28 76 6f 69 64 2a 29 3b Handler)(void*);
37d0: 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 /* Function to
37e0: 63 61 6c 6c 20 77 68 65 6e 20 62 75 73 79 20 2a call when busy *
37f0: 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 73 79 48 /. void *pBusyH
3800: 61 6e 64 6c 65 72 41 72 67 3b 20 20 20 20 20 20 andlerArg;
3810: 2f 2a 20 43 6f 6e 74 65 78 74 20 61 72 67 75 6d /* Context argum
3820: 65 6e 74 20 66 6f 72 20 78 42 75 73 79 48 61 6e ent for xBusyHan
3830: 64 6c 65 72 20 2a 2f 0a 23 69 66 64 65 66 20 53 dler */.#ifdef S
3840: 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 6e 74 QLITE_TEST. int
3850: 20 6e 48 69 74 2c 20 6e 4d 69 73 73 3b 20 20 20 nHit, nMiss;
3860: 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 /* Cach
3870: 65 20 68 69 74 73 20 61 6e 64 20 6d 69 73 73 69 e hits and missi
3880: 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 61 ng */. int nRea
3890: 64 2c 20 6e 57 72 69 74 65 3b 20 20 20 20 20 20 d, nWrite;
38a0: 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 /* Database
38b0: 70 61 67 65 73 20 72 65 61 64 2f 77 72 69 74 74 pages read/writt
38c0: 65 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 76 en */.#endif. v
38d0: 6f 69 64 20 28 2a 78 52 65 69 6e 69 74 65 72 29 oid (*xReiniter)
38e0: 28 44 62 50 61 67 65 2a 29 3b 20 2f 2a 20 43 61 (DbPage*); /* Ca
38f0: 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ll this routine
3900: 77 68 65 6e 20 72 65 6c 6f 61 64 69 6e 67 20 70 when reloading p
3910: 61 67 65 73 20 2a 2f 0a 23 69 66 64 65 66 20 53 ages */.#ifdef S
3920: 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a QLITE_HAS_CODEC.
3930: 20 20 76 6f 69 64 20 2a 28 2a 78 43 6f 64 65 63 void *(*xCodec
3940: 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 )(void*,void*,Pg
3950: 6e 6f 2c 69 6e 74 29 3b 20 2f 2a 20 52 6f 75 74 no,int); /* Rout
3960: 69 6e 65 20 66 6f 72 20 65 6e 2f 64 65 63 6f 64 ine for en/decod
3970: 69 6e 67 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f ing data */. vo
3980: 69 64 20 28 2a 78 43 6f 64 65 63 53 69 7a 65 43 id (*xCodecSizeC
3990: 68 6e 67 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 69 hng)(void*,int,i
39a0: 6e 74 29 3b 20 2f 2a 20 4e 6f 74 69 66 79 20 6f nt); /* Notify o
39b0: 66 20 70 61 67 65 20 73 69 7a 65 20 63 68 61 6e f page size chan
39c0: 67 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a ges */. void (*
39d0: 78 43 6f 64 65 63 46 72 65 65 29 28 76 6f 69 64 xCodecFree)(void
39e0: 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 *);
39f0: 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f /* Destructor fo
3a00: 72 20 74 68 65 20 63 6f 64 65 63 20 2a 2f 0a 20 r the codec */.
3a10: 20 76 6f 69 64 20 2a 70 43 6f 64 65 63 3b 20 20 void *pCodec;
3a20: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
3a30: 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 First argument t
3a40: 6f 20 78 43 6f 64 65 63 2e 2e 2e 20 6d 65 74 68 o xCodec... meth
3a50: 6f 64 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 ods */.#endif.
3a60: 63 68 61 72 20 2a 70 54 6d 70 53 70 61 63 65 3b char *pTmpSpace;
3a70: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
3a80: 61 67 65 72 2e 70 61 67 65 53 69 7a 65 20 62 79 ager.pageSize by
3a90: 74 65 73 20 6f 66 20 73 70 61 63 65 20 66 6f 72 tes of space for
3aa0: 20 74 6d 70 20 75 73 65 20 2a 2f 0a 20 20 69 36 tmp use */. i6
3ab0: 34 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 4 journalSizeLim
3ac0: 69 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a it; /* Siz
3ad0: 65 20 6c 69 6d 69 74 20 66 6f 72 20 70 65 72 73 e limit for pers
3ae0: 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 istent journal f
3af0: 69 6c 65 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 iles */. PCache
3b00: 20 2a 70 50 43 61 63 68 65 3b 20 20 20 20 20 20 *pPCache;
3b10: 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
3b20: 20 74 6f 20 70 61 67 65 20 63 61 63 68 65 20 6f to page cache o
3b30: 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 bject */. sqlit
3b40: 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 6b e3_backup *pBack
3b50: 75 70 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 up; /* Pointe
3b60: 72 20 74 6f 20 6c 69 73 74 20 6f 66 20 6f 6e 67 r to list of ong
3b70: 6f 69 6e 67 20 62 61 63 6b 75 70 20 70 72 6f 63 oing backup proc
3b80: 65 73 73 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a esses */.};../*.
3b90: 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
3ba0: 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
3bb0: 73 20 68 6f 6c 64 20 63 6f 75 6e 74 65 72 73 20 s hold counters
3bc0: 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 used for.** test
3bd0: 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c ing purposes onl
3be0: 79 2e 20 20 54 68 65 73 65 20 76 61 72 69 61 62 y. These variab
3bf0: 6c 65 73 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 les do not exist
3c00: 20 69 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 65 73 in.** a non-tes
3c10: 74 69 6e 67 20 62 75 69 6c 64 2e 20 20 54 68 65 ting build. The
3c20: 73 65 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 se variables are
3c30: 20 6e 6f 74 20 74 68 72 65 61 64 2d 73 61 66 65 not thread-safe
3c40: 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
3c50: 54 45 5f 54 45 53 54 0a 69 6e 74 20 73 71 6c 69 TE_TEST.int sqli
3c60: 74 65 33 5f 70 61 67 65 72 5f 72 65 61 64 64 62 te3_pager_readdb
3c70: 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f _count = 0; /
3c80: 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c * Number of full
3c90: 20 70 61 67 65 73 20 72 65 61 64 20 66 72 6f 6d pages read from
3ca0: 20 44 42 20 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 DB */.int sqlit
3cb0: 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 e3_pager_writedb
3cc0: 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 2f 2a _count = 0; /*
3cd0: 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 Number of full
3ce0: 70 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f pages written to
3cf0: 20 44 42 20 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 DB */.int sqlit
3d00: 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f e3_pager_writej_
3d10: 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a count = 0; /*
3d20: 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
3d30: 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 6f 75 72 written to jour
3d40: 6e 61 6c 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 nal */.# define
3d50: 50 41 47 45 52 5f 49 4e 43 52 28 76 29 20 20 76 PAGER_INCR(v) v
3d60: 2b 2b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e ++.#else.# defin
3d70: 65 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29 0a e PAGER_INCR(v).
3d80: 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 #endif..../*.**
3d90: 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 62 65 Journal files be
3da0: 67 69 6e 20 77 69 74 68 20 74 68 65 20 66 6f 6c gin with the fol
3db0: 6c 6f 77 69 6e 67 20 6d 61 67 69 63 20 73 74 72 lowing magic str
3dc0: 69 6e 67 2e 20 20 54 68 65 20 64 61 74 61 0a 2a ing. The data.*
3dd0: 2a 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20 66 * was obtained f
3de0: 72 6f 6d 20 2f 64 65 76 2f 72 61 6e 64 6f 6d 2e rom /dev/random.
3df0: 20 20 49 74 20 69 73 20 75 73 65 64 20 6f 6e 6c It is used onl
3e00: 79 20 61 73 20 61 20 73 61 6e 69 74 79 20 63 68 y as a sanity ch
3e10: 65 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 eck..**.** Since
3e20: 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 30 2c 20 version 2.8.0,
3e30: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d the journal form
3e40: 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 64 64 69 at contains addi
3e50: 74 69 6f 6e 61 6c 20 73 61 6e 69 74 79 0a 2a 2a tional sanity.**
3e60: 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d checking inform
3e70: 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 70 ation. If the p
3e80: 6f 77 65 72 20 66 61 69 6c 73 20 77 68 69 6c 65 ower fails while
3e90: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
3ea0: 62 65 69 6e 67 0a 2a 2a 20 77 72 69 74 74 65 6e being.** written
3eb0: 2c 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 20 67 61 , semi-random ga
3ec0: 72 62 61 67 65 20 64 61 74 61 20 6d 69 67 68 74 rbage data might
3ed0: 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 6a appear in the j
3ee0: 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 61 ournal.** file a
3ef0: 66 74 65 72 20 70 6f 77 65 72 20 69 73 20 72 65 fter power is re
3f00: 73 74 6f 72 65 64 2e 20 20 49 66 20 61 6e 20 61 stored. If an a
3f10: 74 74 65 6d 70 74 20 69 73 20 74 68 65 6e 20 6d ttempt is then m
3f20: 61 64 65 0a 2a 2a 20 74 6f 20 72 6f 6c 6c 20 74 ade.** to roll t
3f30: 68 65 20 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2c he journal back,
3f40: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
3f50: 75 6c 64 20 62 65 20 63 6f 72 72 75 70 74 65 64 uld be corrupted
3f60: 2e 20 20 54 68 65 20 61 64 64 69 74 69 6f 6e 61 . The additiona
3f70: 6c 0a 2a 2a 20 73 61 6e 69 74 79 20 63 68 65 63 l.** sanity chec
3f80: 6b 69 6e 67 20 64 61 74 61 20 69 73 20 61 6e 20 king data is an
3f90: 61 74 74 65 6d 70 74 20 74 6f 20 64 69 73 63 6f attempt to disco
3fa0: 76 65 72 20 74 68 65 20 67 61 72 62 61 67 65 20 ver the garbage
3fb0: 69 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 in the.** journa
3fc0: 6c 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e l and ignore it.
3fd0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6e 69 74 .**.** The sanit
3fe0: 79 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 y checking infor
3ff0: 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6e mation for the n
4000: 65 77 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 ew journal forma
4010: 74 20 63 6f 6e 73 69 73 74 73 0a 2a 2a 20 6f 66 t consists.** of
4020: 20 61 20 33 32 2d 62 69 74 20 63 68 65 63 6b 73 a 32-bit checks
4030: 75 6d 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 um on each page
4040: 6f 66 20 64 61 74 61 2e 20 20 54 68 65 20 63 68 of data. The ch
4050: 65 63 6b 73 75 6d 20 63 6f 76 65 72 73 20 62 6f ecksum covers bo
4060: 74 68 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e th.** the page n
4070: 75 6d 62 65 72 20 61 6e 64 20 74 68 65 20 70 50 umber and the pP
4080: 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 ager->pageSize b
4090: 79 74 65 73 20 6f 66 20 64 61 74 61 20 66 6f 72 ytes of data for
40a0: 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20 54 68 the page..** Th
40b0: 69 73 20 63 6b 73 75 6d 20 69 73 20 69 6e 69 74 is cksum is init
40c0: 69 61 6c 69 7a 65 64 20 74 6f 20 61 20 33 32 2d ialized to a 32-
40d0: 62 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 bit random value
40e0: 20 74 68 61 74 20 61 70 70 65 61 72 73 20 69 6e that appears in
40f0: 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 the.** journal
4100: 66 69 6c 65 20 72 69 67 68 74 20 61 66 74 65 72 file right after
4110: 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 54 68 the header. Th
4120: 65 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c e random initial
4130: 69 7a 65 72 20 69 73 20 69 6d 70 6f 72 74 61 6e izer is importan
4140: 74 2c 0a 2a 2a 20 62 65 63 61 75 73 65 20 67 61 t,.** because ga
4150: 72 62 61 67 65 20 64 61 74 61 20 74 68 61 74 20 rbage data that
4160: 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20 65 appears at the e
4170: 6e 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 nd of a journal
4180: 69 73 20 6c 69 6b 65 6c 79 0a 2a 2a 20 64 61 74 is likely.** dat
4190: 61 20 74 68 61 74 20 77 61 73 20 6f 6e 63 65 20 a that was once
41a0: 69 6e 20 6f 74 68 65 72 20 66 69 6c 65 73 20 74 in other files t
41b0: 68 61 74 20 68 61 76 65 20 6e 6f 77 20 62 65 65 hat have now bee
41c0: 6e 20 64 65 6c 65 74 65 64 2e 20 20 49 66 20 74 n deleted. If t
41d0: 68 65 0a 2a 2a 20 67 61 72 62 61 67 65 20 64 61 he.** garbage da
41e0: 74 61 20 63 61 6d 65 20 66 72 6f 6d 20 61 6e 20 ta came from an
41f0: 6f 62 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c obsolete journal
4200: 20 66 69 6c 65 2c 20 74 68 65 20 63 68 65 63 6b file, the check
4210: 73 75 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 62 65 sums might.** be
4220: 20 63 6f 72 72 65 63 74 2e 20 20 42 75 74 20 62 correct. But b
4230: 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 y initializing t
4240: 68 65 20 63 68 65 63 6b 73 75 6d 20 74 6f 20 72 he checksum to r
4250: 61 6e 64 6f 6d 20 76 61 6c 75 65 20 77 68 69 63 andom value whic
4260: 68 0a 2a 2a 20 69 73 20 64 69 66 66 65 72 65 6e h.** is differen
4270: 74 20 66 6f 72 20 65 76 65 72 79 20 6a 6f 75 72 t for every jour
4280: 6e 61 6c 2c 20 77 65 20 6d 69 6e 69 6d 69 7a 65 nal, we minimize
4290: 20 74 68 61 74 20 72 69 73 6b 2e 0a 2a 2f 0a 73 that risk..*/.s
42a0: 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 tatic const unsi
42b0: 67 6e 65 64 20 63 68 61 72 20 61 4a 6f 75 72 6e gned char aJourn
42c0: 61 6c 4d 61 67 69 63 5b 5d 20 3d 20 7b 0a 20 20 alMagic[] = {.
42d0: 30 78 64 39 2c 20 30 78 64 35 2c 20 30 78 30 35 0xd9, 0xd5, 0x05
42e0: 2c 20 30 78 66 39 2c 20 30 78 32 30 2c 20 30 78 , 0xf9, 0x20, 0x
42f0: 61 31 2c 20 30 78 36 33 2c 20 30 78 64 37 2c 0a a1, 0x63, 0xd7,.
4300: 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 69 };../*.** The si
4310: 7a 65 20 6f 66 20 74 68 65 20 6f 66 20 65 61 63 ze of the of eac
4320: 68 20 70 61 67 65 20 72 65 63 6f 72 64 20 69 6e h page record in
4330: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
4340: 67 69 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20 given by.** the
4350: 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 2e following macro.
4360: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 .*/.#define JOUR
4370: 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 NAL_PG_SZ(pPager
4380: 29 20 20 28 28 70 50 61 67 65 72 2d 3e 70 61 67 ) ((pPager->pag
4390: 65 53 69 7a 65 29 20 2b 20 38 29 0a 0a 2f 2a 0a eSize) + 8)../*.
43a0: 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 68 ** The journal h
43b0: 65 61 64 65 72 20 73 69 7a 65 20 66 6f 72 20 74 eader size for t
43c0: 68 69 73 20 70 61 67 65 72 2e 20 54 68 69 73 20 his pager. This
43d0: 69 73 20 75 73 75 61 6c 6c 79 20 74 68 65 20 73 is usually the s
43e0: 61 6d 65 20 0a 2a 2a 20 73 69 7a 65 20 61 73 20 ame .** size as
43f0: 61 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 a single disk se
4400: 63 74 6f 72 2e 20 53 65 65 20 61 6c 73 6f 20 73 ctor. See also s
4410: 65 74 53 65 63 74 6f 72 53 69 7a 65 28 29 2e 0a etSectorSize()..
4420: 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e */.#define JOURN
4430: 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
4440: 29 20 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f ) (pPager->secto
4450: 72 53 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 rSize)../*.** Th
4460: 65 20 6d 61 63 72 6f 20 4d 45 4d 44 42 20 69 73 e macro MEMDB is
4470: 20 74 72 75 65 20 69 66 20 77 65 20 61 72 65 20 true if we are
4480: 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 6e 20 dealing with an
4490: 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
44a0: 73 65 2e 0a 2a 2a 20 57 65 20 64 6f 20 74 68 69 se..** We do thi
44b0: 73 20 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20 s as a macro so
44c0: 74 68 61 74 20 69 66 20 74 68 65 20 53 51 4c 49 that if the SQLI
44d0: 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 TE_OMIT_MEMORYDB
44e0: 20 6d 61 63 72 6f 20 69 73 20 73 65 74 2c 0a 2a macro is set,.*
44f0: 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 4d * the value of M
4500: 45 4d 44 42 20 77 69 6c 6c 20 62 65 20 61 20 63 EMDB will be a c
4510: 6f 6e 73 74 61 6e 74 20 61 6e 64 20 74 68 65 20 onstant and the
4520: 63 6f 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6f 70 compiler will op
4530: 74 69 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20 63 6f timize.** out co
4540: 64 65 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 65 de that would ne
4550: 76 65 72 20 65 78 65 63 75 74 65 2e 0a 2a 2f 0a ver execute..*/.
4560: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
4570: 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 23 20 64 65 IT_MEMORYDB.# de
4580: 66 69 6e 65 20 4d 45 4d 44 42 20 30 0a 23 65 6c fine MEMDB 0.#el
4590: 73 65 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44 se.# define MEMD
45a0: 42 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 0a B pPager->memDb.
45b0: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
45c0: 65 20 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c 20 e maximum legal
45d0: 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 28 page number is (
45e0: 32 5e 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64 2^31 - 1)..*/.#d
45f0: 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 5f efine PAGER_MAX_
4600: 50 47 4e 4f 20 32 31 34 37 34 38 33 36 34 37 0a PGNO 2147483647.
4610: 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 20 .#ifndef NDEBUG
4620: 0a 2f 2a 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a ./*.** Usage:.**
4630: 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 61 73 .** assert( as
4640: 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 sert_pager_state
4650: 28 70 50 61 67 65 72 29 20 29 3b 0a 2a 2f 0a 73 (pPager) );.*/.s
4660: 74 61 74 69 63 20 69 6e 74 20 61 73 73 65 72 74 tatic int assert
4670: 5f 70 61 67 65 72 5f 73 74 61 74 65 28 50 61 67 _pager_state(Pag
4680: 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20 20 er *pPager){..
4690: 2f 2a 20 41 20 74 65 6d 70 2d 66 69 6c 65 20 69 /* A temp-file i
46a0: 73 20 61 6c 77 61 79 73 20 69 6e 20 50 41 47 45 s always in PAGE
46b0: 52 5f 45 58 43 4c 55 53 49 56 45 20 6f 72 20 50 R_EXCLUSIVE or P
46c0: 41 47 45 52 5f 53 59 4e 43 45 44 20 73 74 61 74 AGER_SYNCED stat
46d0: 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 e. */. assert(
46e0: 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
46f0: 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 ==0 || pPager->s
4700: 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c tate>=PAGER_EXCL
4710: 55 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 54 USIVE );.. /* T
4720: 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f he changeCountDo
4730: 6e 65 20 66 6c 61 67 20 69 73 20 61 6c 77 61 79 ne flag is alway
4740: 73 20 73 65 74 20 66 6f 72 20 74 65 6d 70 2d 66 s set for temp-f
4750: 69 6c 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 iles */. assert
4760: 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 ( pPager->tempFi
4770: 6c 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d le==0 || pPager-
4780: 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 >changeCountDone
4790: 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b );.. return 1;
47a0: 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
47b0: 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
47c0: 69 74 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 it is necessary
47d0: 74 6f 20 77 72 69 74 65 20 70 61 67 65 20 2a 70 to write page *p
47e0: 50 67 20 69 6e 74 6f 20 74 68 65 20 73 75 62 2d Pg into the sub-
47f0: 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20 70 61 journal..** A pa
4800: 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 77 ge needs to be w
4810: 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
4820: 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 74 sub-journal if t
4830: 68 65 72 65 20 65 78 69 73 74 73 20 6f 6e 65 0a here exists one.
4840: 2a 2a 20 6f 72 20 6d 6f 72 65 20 6f 70 65 6e 20 ** or more open
4850: 73 61 76 65 70 6f 69 6e 74 73 20 66 6f 72 20 77 savepoints for w
4860: 68 69 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 hich:.**.** *
4870: 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 The page-number
4880: 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 is less than or
4890: 65 71 75 61 6c 20 74 6f 20 50 61 67 65 72 53 61 equal to PagerSa
48a0: 76 65 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c 20 61 vepoint.nOrig, a
48b0: 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 62 69 nd.** * The bi
48c0: 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 t corresponding
48d0: 74 6f 20 74 68 65 20 70 61 67 65 2d 6e 75 6d 62 to the page-numb
48e0: 65 72 20 69 73 20 6e 6f 74 20 73 65 74 20 69 6e er is not set in
48f0: 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 .** PagerSav
4900: 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f epoint.pInSavepo
4910: 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 int..*/.static i
4920: 6e 74 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 nt subjRequiresP
4930: 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b age(PgHdr *pPg){
4940: 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 . Pgno pgno = p
4950: 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 Pg->pgno;. Page
4960: 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d r *pPager = pPg-
4970: 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 69 >pPager;. int i
4980: 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 ;. for(i=0; i<p
4990: 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e Pager->nSavepoin
49a0: 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 t; i++){. Pag
49b0: 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d erSavepoint *p =
49c0: 20 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 &pPager->aSavep
49d0: 6f 69 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66 28 oint[i];. if(
49e0: 20 70 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e 6f 20 p->nOrig>=pgno
49f0: 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 69 74 && 0==sqlite3Bit
4a00: 76 65 63 54 65 73 74 28 70 2d 3e 70 49 6e 53 61 vecTest(p->pInSa
4a10: 76 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 20 29 vepoint, pgno) )
4a20: 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 {. return 1
4a30: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
4a40: 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
4a50: 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
4a60: 74 68 65 20 70 61 67 65 20 69 73 20 61 6c 72 65 the page is alre
4a70: 61 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e ady in the journ
4a80: 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 al file..*/.stat
4a90: 69 63 20 69 6e 74 20 70 61 67 65 49 6e 4a 6f 75 ic int pageInJou
4aa0: 72 6e 61 6c 28 50 67 48 64 72 20 2a 70 50 67 29 rnal(PgHdr *pPg)
4ab0: 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
4ac0: 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50 67 e3BitvecTest(pPg
4ad0: 2d 3e 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 ->pPager->pInJou
4ae0: 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 rnal, pPg->pgno)
4af0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
4b00: 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 a 32-bit integer
4b10: 20 66 72 6f 6d 20 74 68 65 20 67 69 76 65 6e 20 from the given
4b20: 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e file descriptor.
4b30: 20 20 53 74 6f 72 65 20 74 68 65 20 69 6e 74 65 Store the inte
4b40: 67 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 72 ger.** that is r
4b50: 65 61 64 20 69 6e 20 2a 70 52 65 73 2e 20 20 52 ead in *pRes. R
4b60: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
4b70: 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f if everything wo
4b80: 72 6b 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a 20 65 rked, or an.** e
4b90: 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d rror code is som
4ba0: 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e ething goes wron
4bb0: 67 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 61 6c g..**.** All val
4bc0: 75 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f ues are stored o
4bd0: 6e 20 64 69 73 6b 20 61 73 20 62 69 67 2d 65 6e n disk as big-en
4be0: 64 69 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 dian..*/.static
4bf0: 69 6e 74 20 72 65 61 64 33 32 62 69 74 73 28 73 int read32bits(s
4c00: 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c qlite3_file *fd,
4c10: 20 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 i64 offset, u32
4c20: 20 2a 70 52 65 73 29 7b 0a 20 20 75 6e 73 69 67 *pRes){. unsig
4c30: 6e 65 64 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a ned char ac[4];.
4c40: 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 int rc = sqlit
4c50: 65 33 4f 73 52 65 61 64 28 66 64 2c 20 61 63 2c e3OsRead(fd, ac,
4c60: 20 73 69 7a 65 6f 66 28 61 63 29 2c 20 6f 66 66 sizeof(ac), off
4c70: 73 65 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d set);. if( rc==
4c80: 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
4c90: 20 2a 70 52 65 73 20 3d 20 73 71 6c 69 74 65 33 *pRes = sqlite3
4ca0: 47 65 74 34 62 79 74 65 28 61 63 29 3b 0a 20 20 Get4byte(ac);.
4cb0: 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
4cc0: 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 ../*.** Write a
4cd0: 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 32-bit integer i
4ce0: 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 62 75 66 nto a string buf
4cf0: 66 65 72 20 69 6e 20 62 69 67 2d 65 6e 64 69 61 fer in big-endia
4d00: 6e 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f n byte order..*/
4d10: 0a 23 64 65 66 69 6e 65 20 70 75 74 33 32 62 69 .#define put32bi
4d20: 74 73 28 41 2c 42 29 20 20 73 71 6c 69 74 65 33 ts(A,B) sqlite3
4d30: 50 75 74 34 62 79 74 65 28 28 75 38 2a 29 41 2c Put4byte((u8*)A,
4d40: 42 29 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 B)../*.** Write
4d50: 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 a 32-bit integer
4d60: 20 69 6e 74 6f 20 74 68 65 20 67 69 76 65 6e 20 into the given
4d70: 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e file descriptor.
4d80: 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f Return SQLITE_
4d90: 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 OK.** on success
4da0: 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 or an error cod
4db0: 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 e is something g
4dc0: 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 oes wrong..*/.st
4dd0: 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 33 32 atic int write32
4de0: 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69 6c bits(sqlite3_fil
4df0: 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 65 e *fd, i64 offse
4e00: 74 2c 20 75 33 32 20 76 61 6c 29 7b 0a 20 20 63 t, u32 val){. c
4e10: 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20 70 75 74 har ac[4];. put
4e20: 33 32 62 69 74 73 28 61 63 2c 20 76 61 6c 29 3b 32bits(ac, val);
4e30: 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
4e40: 33 4f 73 57 72 69 74 65 28 66 64 2c 20 61 63 2c 3OsWrite(fd, ac,
4e50: 20 34 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 0a 4, offset);.}..
4e60: 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 /*.** The argume
4e70: 6e 74 20 74 6f 20 74 68 69 73 20 6d 61 63 72 6f nt to this macro
4e80: 20 69 73 20 61 20 66 69 6c 65 20 64 65 73 63 72 is a file descr
4e90: 69 70 74 6f 72 20 28 74 79 70 65 20 73 71 6c 69 iptor (type sqli
4ea0: 74 65 33 5f 66 69 6c 65 2a 29 2e 0a 2a 2a 20 52 te3_file*)..** R
4eb0: 65 74 75 72 6e 20 30 20 69 66 20 69 74 20 69 73 eturn 0 if it is
4ec0: 20 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 6e 6f not open, or no
4ed0: 6e 2d 7a 65 72 6f 20 28 62 75 74 20 6e 6f 74 20 n-zero (but not
4ee0: 31 29 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 1) if it is..**.
4ef0: 2a 2a 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 ** This is so th
4f00: 61 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 at expressions c
4f10: 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 73 an be written as
4f20: 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 69 73 :.**.** if( is
4f30: 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
4f40: 29 20 29 7b 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 69 ) ){ ....**.** i
4f50: 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 0a 2a 2a 20 nstead of.**.**
4f60: 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 66 if( pPager->jf
4f70: 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 20 2e d->pMethods ){ .
4f80: 2e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69 73 ...*/.#define is
4f90: 4f 70 65 6e 28 70 46 64 29 20 28 28 70 46 64 29 Open(pFd) ((pFd)
4fa0: 2d 3e 70 4d 65 74 68 6f 64 73 29 0a 0a 2f 2a 0a ->pMethods)../*.
4fb0: 2a 2a 20 49 66 20 66 69 6c 65 20 70 46 64 20 69 ** If file pFd i
4fc0: 73 20 6f 70 65 6e 2c 20 63 61 6c 6c 20 73 71 6c s open, call sql
4fd0: 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 20 6f ite3OsUnlock() o
4fe0: 6e 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 n it..*/.static
4ff0: 69 6e 74 20 6f 73 55 6e 6c 6f 63 6b 28 73 71 6c int osUnlock(sql
5000: 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 64 2c 20 ite3_file *pFd,
5010: 69 6e 74 20 65 4c 6f 63 6b 29 7b 0a 20 20 69 66 int eLock){. if
5020: 28 20 21 69 73 4f 70 65 6e 28 70 46 64 29 20 29 ( !isOpen(pFd) )
5030: 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
5040: 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 ITE_OK;. }. re
5050: 74 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 55 6e turn sqlite3OsUn
5060: 6c 6f 63 6b 28 70 46 64 2c 20 65 4c 6f 63 6b 29 lock(pFd, eLock)
5070: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
5080: 66 75 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d 69 function determi
5090: 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e nes whether or n
50a0: 6f 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 ot the atomic-wr
50b0: 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ite optimization
50c0: 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 .** can be used
50d0: 77 69 74 68 20 74 68 69 73 20 70 61 67 65 72 2e with this pager.
50e0: 20 54 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f The optimizatio
50f0: 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 66 n can be used if
5100: 3a 0a 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68 65 :.**.** (a) the
5110: 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
5120: 62 79 20 4f 73 44 65 76 69 63 65 43 68 61 72 61 by OsDeviceChara
5130: 63 74 65 72 69 73 74 69 63 73 28 29 20 69 6e 64 cteristics() ind
5140: 69 63 61 74 65 73 20 74 68 61 74 0a 2a 2a 20 20 icates that.**
5150: 20 20 20 20 61 20 64 61 74 61 62 61 73 65 20 70 a database p
5160: 61 67 65 20 6d 61 79 20 62 65 20 77 72 69 74 74 age may be writt
5170: 65 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20 61 en atomically, a
5180: 6e 64 0a 2a 2a 20 20 28 62 29 20 74 68 65 20 76 nd.** (b) the v
5190: 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
51a0: 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 20 OsSectorSize()
51b0: 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 is less than or
51c0: 65 71 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74 6f equal.** to
51d0: 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 2e 0a the page size..
51e0: 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69 **.** The optimi
51f0: 7a 61 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 61 zation is also a
5200: 6c 77 61 79 73 20 65 6e 61 62 6c 65 64 20 66 6f lways enabled fo
5210: 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 r temporary file
5220: 73 2e 20 49 74 20 69 73 0a 2a 2a 20 61 6e 20 65 s. It is.** an e
5230: 72 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68 69 rror to call thi
5240: 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 70 50 s function if pP
5250: 61 67 65 72 20 69 73 20 6f 70 65 6e 65 64 20 6f ager is opened o
5260: 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a n an in-memory.*
5270: 2a 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a * database..**.*
5280: 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a * If the optimiz
5290: 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 ation cannot be
52a0: 75 73 65 64 2c 20 30 20 69 73 20 72 65 74 75 72 used, 0 is retur
52b0: 6e 65 64 2e 20 49 66 20 69 74 20 63 61 6e 20 62 ned. If it can b
52c0: 65 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 e used,.** then
52d0: 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
52e0: 65 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f ed is the size o
52f0: 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
5300: 6c 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 63 6f le when it.** co
5310: 6e 74 61 69 6e 73 20 72 6f 6c 6c 62 61 63 6b 20 ntains rollback
5320: 64 61 74 61 20 66 6f 72 20 65 78 61 63 74 6c 79 data for exactly
5330: 20 6f 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 one page..*/.#i
5340: 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
5350: 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a LE_ATOMIC_WRITE.
5360: 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 42 static int jrnlB
5370: 75 66 66 65 72 53 69 7a 65 28 50 61 67 65 72 20 ufferSize(Pager
5380: 2a 70 50 61 67 65 72 29 7b 0a 20 20 61 73 73 65 *pPager){. asse
5390: 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 rt( !MEMDB );.
53a0: 69 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d if( !pPager->tem
53b0: 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 pFile ){. int
53c0: 20 64 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 dc;
53d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
53e0: 2a 20 44 65 76 69 63 65 20 63 68 61 72 61 63 74 * Device charact
53f0: 65 72 69 73 74 69 63 73 20 2a 2f 0a 20 20 20 20 eristics */.
5400: 69 6e 74 20 6e 53 65 63 74 6f 72 3b 20 20 20 20 int nSector;
5410: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5420: 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 /* Sector size
5430: 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 50 61 */. int szPa
5440: 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
5450: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
5460: 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 20 20 61 e size */.. a
5470: 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
5480: 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 20 ager->fd) );.
5490: 20 64 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 dc = sqlite3OsD
54a0: 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
54b0: 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 tics(pPager->fd)
54c0: 3b 0a 20 20 20 20 6e 53 65 63 74 6f 72 20 3d 20 ;. nSector =
54d0: 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
54e0: 7a 65 3b 0a 20 20 20 20 73 7a 50 61 67 65 20 3d ze;. szPage =
54f0: 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
5500: 65 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 53 e;.. assert(S
5510: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
5520: 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 IC512==(512>>8))
5530: 3b 0a 20 20 20 20 61 73 73 65 72 74 28 53 51 4c ;. assert(SQL
5540: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
5550: 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 64K==(65536>>8))
5560: 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 64 63 ;. if( 0==(dc
5570: 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 &(SQLITE_IOCAP_A
5580: 54 4f 4d 49 43 7c 28 73 7a 50 61 67 65 3e 3e 38 TOMIC|(szPage>>8
5590: 29 29 20 7c 7c 20 6e 53 65 63 74 6f 72 3e 73 7a )) || nSector>sz
55a0: 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20 72 Page) ){. r
55b0: 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 eturn 0;. }.
55c0: 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 4a 4f 55 }.. return JOU
55d0: 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
55e0: 65 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50 47 er) + JOURNAL_PG
55f0: 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 7d 0a 23 _SZ(pPager);.}.#
5600: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 endif../*.** If
5610: 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
5620: 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 74 68 ES is defined th
5630: 65 6e 20 77 65 20 64 6f 20 73 6f 6d 65 20 73 61 en we do some sa
5640: 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a nity checking.**
5650: 20 6f 6e 20 74 68 65 20 63 61 63 68 65 20 75 73 on the cache us
5660: 69 6e 67 20 61 20 68 61 73 68 20 66 75 6e 63 74 ing a hash funct
5670: 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 ion. This is us
5680: 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a ed for testing.*
5690: 2a 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 * and debugging
56a0: 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 only..*/.#ifdef
56b0: 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
56c0: 45 53 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 ES./*.** Return
56d0: 61 20 33 32 2d 62 69 74 20 68 61 73 68 20 6f 66 a 32-bit hash of
56e0: 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20 66 the page data f
56f0: 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 or pPage..*/.sta
5700: 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 64 61 tic u32 pager_da
5710: 74 61 68 61 73 68 28 69 6e 74 20 6e 42 79 74 65 tahash(int nByte
5720: 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 , unsigned char
5730: 2a 70 44 61 74 61 29 7b 0a 20 20 75 33 32 20 68 *pData){. u32 h
5740: 61 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 ash = 0;. int i
5750: 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e ;. for(i=0; i<n
5760: 42 79 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Byte; i++){.
5770: 68 61 73 68 20 3d 20 28 68 61 73 68 2a 31 30 33 hash = (hash*103
5780: 39 29 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a 20 9) + pData[i];.
5790: 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 61 73 68 }. return hash
57a0: 3b 0a 7d 0a 73 74 61 74 69 63 20 75 33 32 20 70 ;.}.static u32 p
57b0: 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 50 67 ager_pagehash(Pg
57c0: 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 Hdr *pPage){. r
57d0: 65 74 75 72 6e 20 70 61 67 65 72 5f 64 61 74 61 eturn pager_data
57e0: 68 61 73 68 28 70 50 61 67 65 2d 3e 70 50 61 67 hash(pPage->pPag
57f0: 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 75 er->pageSize, (u
5800: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 nsigned char *)p
5810: 50 61 67 65 2d 3e 70 44 61 74 61 29 3b 0a 7d 0a Page->pData);.}.
5820: 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 static void page
5830: 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 28 50 r_set_pagehash(P
5840: 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 gHdr *pPage){.
5850: 70 50 61 67 65 2d 3e 70 61 67 65 48 61 73 68 20 pPage->pageHash
5860: 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 = pager_pagehash
5870: 28 70 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a (pPage);.}../*.*
5880: 2a 20 54 68 65 20 43 48 45 43 4b 5f 50 41 47 45 * The CHECK_PAGE
5890: 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 20 50 macro takes a P
58a0: 67 48 64 72 2a 20 61 73 20 61 6e 20 61 72 67 75 gHdr* as an argu
58b0: 6d 65 6e 74 2e 20 49 66 20 53 51 4c 49 54 45 5f ment. If SQLITE_
58c0: 43 48 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20 69 CHECK_PAGES.** i
58d0: 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 64 20 4e s defined, and N
58e0: 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64 65 66 DEBUG is not def
58f0: 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28 ined, an assert(
5900: 29 20 73 74 61 74 65 6d 65 6e 74 20 63 68 65 63 ) statement chec
5910: 6b 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 ks.** that the p
5920: 61 67 65 20 69 73 20 65 69 74 68 65 72 20 64 69 age is either di
5930: 72 74 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61 74 rty or still mat
5940: 63 68 65 73 20 74 68 65 20 63 61 6c 63 75 6c 61 ches the calcula
5950: 74 65 64 20 70 61 67 65 2d 68 61 73 68 2e 0a 2a ted page-hash..*
5960: 2f 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f /.#define CHECK_
5970: 50 41 47 45 28 78 29 20 63 68 65 63 6b 50 61 67 PAGE(x) checkPag
5980: 65 28 78 29 0a 73 74 61 74 69 63 20 76 6f 69 64 e(x).static void
5990: 20 63 68 65 63 6b 50 61 67 65 28 50 67 48 64 72 checkPage(PgHdr
59a0: 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 *pPg){. Pager
59b0: 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
59c0: 50 61 67 65 72 3b 0a 20 20 61 73 73 65 72 74 28 Pager;. assert(
59d0: 20 21 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 !pPg->pageHash
59e0: 7c 7c 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f || pPager->errCo
59f0: 64 65 0a 20 20 20 20 20 20 7c 7c 20 28 70 50 67 de. || (pPg
5a00: 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
5a10: 52 54 59 29 20 7c 7c 20 70 50 67 2d 3e 70 61 67 RTY) || pPg->pag
5a20: 65 48 61 73 68 3d 3d 70 61 67 65 72 5f 70 61 67 eHash==pager_pag
5a30: 65 68 61 73 68 28 70 50 67 29 20 29 3b 0a 7d 0a ehash(pPg) );.}.
5a40: 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 70 .#else.#define p
5a50: 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 58 2c ager_datahash(X,
5a60: 59 29 20 20 30 0a 23 64 65 66 69 6e 65 20 70 61 Y) 0.#define pa
5a70: 67 65 72 5f 70 61 67 65 68 61 73 68 28 58 29 20 ger_pagehash(X)
5a80: 20 30 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 0.#define CHECK
5a90: 5f 50 41 47 45 28 78 29 0a 23 65 6e 64 69 66 20 _PAGE(x).#endif
5aa0: 20 2f 2a 20 53 51 4c 49 54 45 5f 43 48 45 43 4b /* SQLITE_CHECK
5ab0: 5f 50 41 47 45 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a _PAGES */../*.**
5ac0: 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 When this is ca
5ad0: 6c 6c 65 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c lled the journal
5ae0: 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72 20 file for pager
5af0: 70 50 61 67 65 72 20 6d 75 73 74 20 62 65 20 6f pPager must be o
5b00: 70 65 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e pen..** This fun
5b10: 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 ction attempts t
5b20: 6f 20 72 65 61 64 20 61 20 6d 61 73 74 65 72 20 o read a master
5b30: 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d journal file nam
5b40: 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 65 e from the .** e
5b50: 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 nd of the file a
5b60: 6e 64 2c 20 69 66 20 73 75 63 63 65 73 73 66 75 nd, if successfu
5b70: 6c 2c 20 63 6f 70 69 65 73 20 69 74 20 69 6e 74 l, copies it int
5b80: 6f 20 6d 65 6d 6f 72 79 20 73 75 70 70 6c 69 65 o memory supplie
5b90: 64 20 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 6c d .** by the cal
5ba0: 6c 65 72 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 ler. See comment
5bb0: 73 20 61 62 6f 76 65 20 77 72 69 74 65 4d 61 73 s above writeMas
5bc0: 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 66 6f 72 terJournal() for
5bd0: 20 74 68 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 75 the format.** u
5be0: 73 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 6d sed to store a m
5bf0: 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
5c00: 6c 65 20 6e 61 6d 65 20 61 74 20 74 68 65 20 65 le name at the e
5c10: 6e 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 nd of a journal
5c20: 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 file..**.** zMas
5c30: 74 65 72 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 ter must point t
5c40: 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 61 74 o a buffer of at
5c50: 20 6c 65 61 73 74 20 6e 4d 61 73 74 65 72 20 62 least nMaster b
5c60: 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 ytes allocated b
5c70: 79 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 2e y.** the caller.
5c80: 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 This should be
5c90: 73 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 sqlite3_vfs.mxPa
5ca0: 74 68 6e 61 6d 65 2b 31 20 28 74 6f 20 65 6e 73 thname+1 (to ens
5cb0: 75 72 65 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 ure there is.**
5cc0: 65 6e 6f 75 67 68 20 73 70 61 63 65 20 74 6f 20 enough space to
5cd0: 77 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 write the master
5ce0: 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 29 2e 20 journal name).
5cf0: 49 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f If the master jo
5d00: 75 72 6e 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69 6e urnal.** name in
5d10: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
5d20: 6c 6f 6e 67 65 72 20 74 68 61 6e 20 6e 4d 61 73 longer than nMas
5d30: 74 65 72 20 62 79 74 65 73 20 28 69 6e 63 6c 75 ter bytes (inclu
5d40: 64 69 6e 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 ding a.** nul-te
5d50: 72 6d 69 6e 61 74 6f 72 29 2c 20 74 68 65 6e 20 rminator), then
5d60: 74 68 69 73 20 69 73 20 68 61 6e 64 6c 65 64 20 this is handled
5d70: 61 73 20 69 66 20 6e 6f 20 6d 61 73 74 65 72 20 as if no master
5d80: 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a 20 journal name.**
5d90: 77 65 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 were present in
5da0: 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a the journal..**.
5db0: 2a 2a 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a ** If a master j
5dc0: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
5dd0: 20 69 73 20 70 72 65 73 65 6e 74 20 61 74 20 74 is present at t
5de0: 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f he end of the jo
5df0: 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2c 20 74 urnal.** file, t
5e00: 68 65 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 hen it is copied
5e10: 20 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 into the buffer
5e20: 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a pointed to by z
5e30: 4d 61 73 74 65 72 2e 20 41 0a 2a 2a 20 6e 75 6c Master. A.** nul
5e40: 2d 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 -terminator byte
5e50: 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 is appended to
5e60: 74 68 65 20 62 75 66 66 65 72 20 66 6f 6c 6c 6f the buffer follo
5e70: 77 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 0a wing the master.
5e80: 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ** journal file
5e90: 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 name..**.** If i
5ea0: 74 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 t is determined
5eb0: 74 68 61 74 20 6e 6f 20 6d 61 73 74 65 72 20 6a that no master j
5ec0: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
5ed0: 20 69 73 20 70 72 65 73 65 6e 74 20 0a 2a 2a 20 is present .**
5ee0: 7a 4d 61 73 74 65 72 5b 30 5d 20 69 73 20 73 65 zMaster[0] is se
5ef0: 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 t to 0 and SQLIT
5f00: 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a E_OK returned..*
5f10: 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 *.** If an error
5f20: 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 occurs while re
5f30: 61 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a ading from the j
5f40: 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 61 6e 20 ournal file, an
5f50: 53 51 4c 69 74 65 0a 2a 2a 20 65 72 72 6f 72 20 SQLite.** error
5f60: 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
5f70: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
5f80: 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 readMasterJourna
5f90: 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a l(sqlite3_file *
5fa0: 70 4a 72 6e 6c 2c 20 63 68 61 72 20 2a 7a 4d 61 pJrnl, char *zMa
5fb0: 73 74 65 72 2c 20 75 33 32 20 6e 4d 61 73 74 65 ster, u32 nMaste
5fc0: 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 r){. int rc;
5fd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5fe0: 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
5ff0: 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 3b 20 20 20 */. u32 len;
6000: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6010: 2f 2a 20 4c 65 6e 67 74 68 20 69 6e 20 62 79 74 /* Length in byt
6020: 65 73 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 es of master jou
6030: 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 rnal name */. i
6040: 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 64 szJ;
6050: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 /* Tot
6060: 61 6c 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 al size in bytes
6070: 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 of journal file
6080: 20 70 4a 72 6e 6c 20 2a 2f 0a 20 20 75 33 32 20 pJrnl */. u32
6090: 63 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 cksum;
60a0: 20 20 20 20 20 20 20 2f 2a 20 4d 4a 20 63 68 65 /* MJ che
60b0: 63 6b 73 75 6d 20 76 61 6c 75 65 20 72 65 61 64 cksum value read
60c0: 20 66 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 2a 2f from journal */
60d0: 0a 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 . u32 u;
60e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
60f0: 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 Unsigned loop c
6100: 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69 ounter */. unsi
6110: 67 6e 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 gned char aMagic
6120: 5b 38 5d 3b 20 20 20 2f 2a 20 41 20 62 75 66 66 [8]; /* A buff
6130: 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d er to hold the m
6140: 61 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0a 20 agic header */.
6150: 20 7a 4d 61 73 74 65 72 5b 30 5d 20 3d 20 27 5c zMaster[0] = '\
6160: 30 27 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 0';.. if( SQLIT
6170: 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 E_OK!=(rc = sqli
6180: 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4a te3OsFileSize(pJ
6190: 72 6e 6c 2c 20 26 73 7a 4a 29 29 0a 20 20 20 7c rnl, &szJ)). |
61a0: 7c 20 73 7a 4a 3c 31 36 0a 20 20 20 7c 7c 20 53 | szJ<16. || S
61b0: 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 QLITE_OK!=(rc =
61c0: 72 65 61 64 33 32 62 69 74 73 28 70 4a 72 6e 6c read32bits(pJrnl
61d0: 2c 20 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e 29 29 , szJ-16, &len))
61e0: 0a 20 20 20 7c 7c 20 6c 65 6e 3e 3d 6e 4d 61 73 . || len>=nMas
61f0: 74 65 72 20 0a 20 20 20 7c 7c 20 53 51 4c 49 54 ter . || SQLIT
6200: 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 E_OK!=(rc = read
6210: 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 32bits(pJrnl, sz
6220: 4a 2d 31 32 2c 20 26 63 6b 73 75 6d 29 29 0a 20 J-12, &cksum)).
6230: 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d || SQLITE_OK!=
6240: 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 (rc = sqlite3OsR
6250: 65 61 64 28 70 4a 72 6e 6c 2c 20 61 4d 61 67 69 ead(pJrnl, aMagi
6260: 63 2c 20 38 2c 20 73 7a 4a 2d 38 29 29 0a 20 20 c, 8, szJ-8)).
6270: 20 7c 7c 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 || memcmp(aMagi
6280: 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 c, aJournalMagic
6290: 2c 20 38 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 , 8). || SQLIT
62a0: 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 E_OK!=(rc = sqli
62b0: 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c te3OsRead(pJrnl,
62c0: 20 7a 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 73 zMaster, len, s
62d0: 7a 4a 2d 31 36 2d 6c 65 6e 29 29 0a 20 20 29 7b zJ-16-len)). ){
62e0: 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
62f0: 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 }.. /* See if
6300: 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 6d 61 the checksum ma
6310: 74 63 68 65 73 20 74 68 65 20 6d 61 73 74 65 72 tches the master
6320: 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f journal name */
6330: 0a 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65 . for(u=0; u<le
6340: 6e 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 n; u++){. cks
6350: 75 6d 20 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d um -= zMaster[u]
6360: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 6b 73 75 ;. }. if( cksu
6370: 6d 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 m ){. /* If t
6380: 68 65 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 73 he checksum does
6390: 6e 27 74 20 61 64 64 20 75 70 2c 20 74 68 65 6e n't add up, then
63a0: 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 one or more of
63b0: 74 68 65 20 64 69 73 6b 20 73 65 63 74 6f 72 73 the disk sectors
63c0: 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 . ** containi
63d0: 6e 67 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f ng the master jo
63e0: 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 urnal filename i
63f0: 73 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 s corrupted. Thi
6400: 73 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 64 s means. ** d
6410: 65 66 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 62 efinitely roll b
6420: 61 63 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65 74 ack, so just ret
6430: 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e urn SQLITE_OK an
6440: 64 20 72 65 70 6f 72 74 20 61 20 28 6e 75 6c 29 d report a (nul)
6450: 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a . ** master-j
6460: 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 2e ournal filename.
6470: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 . */. len
6480: 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 = 0;. }. zMast
6490: 65 72 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b 0a er[len] = '\0';.
64a0: 20 20 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c . return SQL
64b0: 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
64c0: 20 52 65 74 75 72 6e 20 74 68 65 20 6f 66 66 73 Return the offs
64d0: 65 74 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 et of the sector
64e0: 20 62 6f 75 6e 64 61 72 79 20 61 74 20 6f 72 20 boundary at or
64f0: 69 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a 2a 20 immediately .**
6500: 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 76 61 following the va
6510: 6c 75 65 20 69 6e 20 70 50 61 67 65 72 2d 3e 6a lue in pPager->j
6520: 6f 75 72 6e 61 6c 4f 66 66 2c 20 61 73 73 75 6d ournalOff, assum
6530: 69 6e 67 20 61 20 73 65 63 74 6f 72 20 0a 2a 2a ing a sector .**
6540: 20 73 69 7a 65 20 6f 66 20 70 50 61 67 65 72 2d size of pPager-
6550: 3e 73 65 63 74 6f 72 53 69 7a 65 20 62 79 74 65 >sectorSize byte
6560: 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 20 66 6f 72 s..**.** i.e for
6570: 20 61 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f a sector size o
6580: 66 20 35 31 32 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 f 512:.**.** P
6590: 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ager.journalOff
65a0: 20 20 20 20 20 20 20 20 20 52 65 74 75 72 6e 20 Return
65b0: 76 61 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d value.** -----
65c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
65d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
65e0: 2d 2d 0a 2a 2a 20 20 20 30 20 20 20 20 20 20 20 --.** 0
65f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6600: 20 20 30 0a 2a 2a 20 20 20 35 31 32 20 20 20 20 0.** 512
6610: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6620: 20 20 20 35 31 32 0a 2a 2a 20 20 20 31 30 30 20 512.** 100
6630: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6640: 20 20 20 20 20 20 35 31 32 0a 2a 2a 20 20 20 32 512.** 2
6650: 30 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 000
6660: 20 20 20 20 20 20 20 20 20 32 30 34 38 0a 2a 2a 2048.**
6670: 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 .*/.static i64
6680: 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 journalHdrOffset
6690: 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
66a0: 0a 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 . i64 offset =
66b0: 30 3b 0a 20 20 69 36 34 20 63 20 3d 20 70 50 61 0;. i64 c = pPa
66c0: 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b ger->journalOff;
66d0: 0a 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 . if( c ){.
66e0: 6f 66 66 73 65 74 20 3d 20 28 28 63 2d 31 29 2f offset = ((c-1)/
66f0: 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 JOURNAL_HDR_SZ(p
6700: 50 61 67 65 72 29 20 2b 20 31 29 20 2a 20 4a 4f Pager) + 1) * JO
6710: 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
6720: 67 65 72 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 ger);. }. asse
6730: 72 74 28 20 6f 66 66 73 65 74 25 4a 4f 55 52 4e rt( offset%JOURN
6740: 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
6750: 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 )==0 );. assert
6760: 28 20 6f 66 66 73 65 74 3e 3d 63 20 29 3b 0a 20 ( offset>=c );.
6770: 20 61 73 73 65 72 74 28 20 28 6f 66 66 73 65 74 assert( (offset
6780: 2d 63 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f -c)<JOURNAL_HDR_
6790: 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 SZ(pPager) );.
67a0: 72 65 74 75 72 6e 20 6f 66 66 73 65 74 3b 0a 7d return offset;.}
67b0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 ../*.** The jour
67c0: 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 nal file must be
67d0: 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 open when this
67e0: 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
67f0: 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ed..**.** This f
6800: 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d unction is a no-
6810: 6f 70 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 op if the journa
6820: 6c 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 l file has not b
6830: 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a een written to.*
6840: 2a 20 77 69 74 68 69 6e 20 74 68 65 20 63 75 72 * within the cur
6850: 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e rent transaction
6860: 20 28 69 2e 65 2e 20 69 66 20 50 61 67 65 72 2e (i.e. if Pager.
6870: 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 29 2e 0a journalOff==0)..
6880: 2a 2a 0a 2a 2a 20 49 66 20 64 6f 54 72 75 6e 63 **.** If doTrunc
6890: 61 74 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 ate is non-zero
68a0: 6f 72 20 74 68 65 20 50 61 67 65 72 2e 6a 6f 75 or the Pager.jou
68b0: 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 76 61 rnalSizeLimit va
68c0: 72 69 61 62 6c 65 20 69 73 0a 2a 2a 20 73 65 74 riable is.** set
68d0: 20 74 6f 20 30 2c 20 74 68 65 6e 20 74 72 75 6e to 0, then trun
68e0: 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c cate the journal
68f0: 20 66 69 6c 65 20 74 6f 20 7a 65 72 6f 20 62 79 file to zero by
6900: 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 4f 74 68 tes in size. Oth
6910: 65 72 77 69 73 65 2c 0a 2a 2a 20 7a 65 72 6f 20 erwise,.** zero
6920: 74 68 65 20 32 38 2d 62 79 74 65 20 68 65 61 64 the 28-byte head
6930: 65 72 20 61 74 20 74 68 65 20 73 74 61 72 74 20 er at the start
6940: 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
6950: 69 6c 65 2e 20 49 6e 20 65 69 74 68 65 72 20 63 ile. In either c
6960: 61 73 65 2c 20 0a 2a 2a 20 69 66 20 74 68 65 20 ase, .** if the
6970: 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 pager is not in
6980: 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 73 79 no-sync mode, sy
6990: 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 nc the journal f
69a0: 69 6c 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 ile immediately
69b0: 0a 2a 2a 20 61 66 74 65 72 20 77 72 69 74 69 6e .** after writin
69c0: 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 g or truncating
69d0: 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 61 67 it..**.** If Pag
69e0: 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 er.journalSizeLi
69f0: 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 61 20 mit is set to a
6a00: 70 6f 73 69 74 69 76 65 2c 20 6e 6f 6e 2d 7a 65 positive, non-ze
6a10: 72 6f 20 76 61 6c 75 65 2c 20 61 6e 64 0a 2a 2a ro value, and.**
6a20: 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 following the t
6a30: 72 75 6e 63 61 74 69 6f 6e 20 6f 72 20 7a 65 72 runcation or zer
6a40: 6f 69 6e 67 20 64 65 73 63 72 69 62 65 64 20 61 oing described a
6a50: 62 6f 76 65 20 74 68 65 20 73 69 7a 65 20 6f 66 bove the size of
6a60: 20 74 68 65 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c the .** journal
6a70: 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 20 69 file in bytes i
6a80: 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 s larger than th
6a90: 69 73 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 is value, then t
6aa0: 72 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 6a runcate the.** j
6ab0: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 50 ournal file to P
6ac0: 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 ager.journalSize
6ad0: 4c 69 6d 69 74 20 62 79 74 65 73 2e 20 54 68 65 Limit bytes. The
6ae0: 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f journal file do
6af0: 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 es.** not need t
6b00: 6f 20 62 65 20 73 79 6e 63 65 64 20 66 6f 6c 6c o be synced foll
6b10: 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61 owing this opera
6b20: 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 tion..**.** If a
6b30: 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 n IO error occur
6b40: 73 2c 20 61 62 61 6e 64 6f 6e 20 70 72 6f 63 65 s, abandon proce
6b50: 73 73 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e ssing and return
6b60: 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f the IO error co
6b70: 64 65 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 de..** Otherwise
6b80: 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
6b90: 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e OK..*/.static in
6ba0: 74 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 t zeroJournalHdr
6bb0: 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
6bc0: 69 6e 74 20 64 6f 54 72 75 6e 63 61 74 65 29 7b int doTruncate){
6bd0: 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
6be0: 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 TE_OK;
6bf0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c00: 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
6c10: 6f 64 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ode */. assert(
6c20: 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
6c30: 6a 66 64 29 20 29 3b 0a 20 20 69 66 28 20 70 50 jfd) );. if( pP
6c40: 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
6c50: 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 36 ){. const i6
6c60: 34 20 69 4c 69 6d 69 74 20 3d 20 70 50 61 67 65 4 iLimit = pPage
6c70: 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 r->journalSizeLi
6c80: 6d 69 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 6c mit; /* Local
6c90: 20 63 61 63 68 65 20 6f 66 20 6a 73 6c 20 2a 2f cache of jsl */
6ca0: 0a 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 .. IOTRACE(("
6cb0: 4a 5a 45 52 4f 48 44 52 20 25 70 5c 6e 22 2c 20 JZEROHDR %p\n",
6cc0: 70 50 61 67 65 72 29 29 0a 20 20 20 20 69 66 28 pPager)). if(
6cd0: 20 64 6f 54 72 75 6e 63 61 74 65 20 7c 7c 20 69 doTruncate || i
6ce0: 4c 69 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 Limit==0 ){.
6cf0: 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
6d00: 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d Truncate(pPager-
6d10: 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 7d 65 >jfd, 0);. }e
6d20: 6c 73 65 7b 0a 20 20 20 20 20 20 73 74 61 74 69 lse{. stati
6d30: 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 65 72 c const char zer
6d40: 6f 48 64 72 5b 32 38 5d 20 3d 20 7b 30 7d 3b 0a oHdr[28] = {0};.
6d50: 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
6d60: 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 e3OsWrite(pPager
6d70: 2d 3e 6a 66 64 2c 20 7a 65 72 6f 48 64 72 2c 20 ->jfd, zeroHdr,
6d80: 73 69 7a 65 6f 66 28 7a 65 72 6f 48 64 72 29 2c sizeof(zeroHdr),
6d90: 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 0);. }. i
6da0: 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
6db0: 20 26 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 && !pPager->noS
6dc0: 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 ync ){. rc
6dd0: 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 = sqlite3OsSync(
6de0: 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c pPager->jfd, SQL
6df0: 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c ITE_SYNC_DATAONL
6e00: 59 7c 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 Y|pPager->sync_f
6e10: 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 lags);. }..
6e20: 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 /* At this poi
6e30: 6e 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 nt the transacti
6e40: 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 on is committed
6e50: 62 75 74 20 74 68 65 20 77 72 69 74 65 20 6c 6f but the write lo
6e60: 63 6b 20 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 ck . ** is st
6e70: 69 6c 6c 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 ill held on the
6e80: 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 file. If there i
6e90: 73 20 61 20 73 69 7a 65 20 6c 69 6d 69 74 20 63 s a size limit c
6ea0: 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 0a 20 onfigured for .
6eb0: 20 20 20 2a 2a 20 74 68 65 20 70 65 72 73 69 73 ** the persis
6ec0: 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 tent journal and
6ed0: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
6ee0: 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 e currently cons
6ef0: 75 6d 65 73 20 6d 6f 72 65 0a 20 20 20 20 2a 2a umes more. **
6f00: 20 73 70 61 63 65 20 74 68 61 6e 20 74 68 61 74 space than that
6f10: 20 6c 69 6d 69 74 20 61 6c 6c 6f 77 73 20 66 6f limit allows fo
6f20: 72 2c 20 74 72 75 6e 63 61 74 65 20 69 74 20 6e r, truncate it n
6f30: 6f 77 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 ow. There is no
6f40: 6e 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 73 need. ** to s
6f50: 79 6e 63 20 74 68 65 20 66 69 6c 65 20 66 6f 6c ync the file fol
6f60: 6c 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 lowing this oper
6f70: 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 ation.. */.
6f80: 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
6f90: 5f 4f 4b 20 26 26 20 69 4c 69 6d 69 74 3e 30 20 _OK && iLimit>0
6fa0: 29 7b 0a 20 20 20 20 20 20 69 36 34 20 73 7a 3b ){. i64 sz;
6fb0: 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
6fc0: 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 te3OsFileSize(pP
6fd0: 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 29 3b ager->jfd, &sz);
6fe0: 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
6ff0: 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 7a 3e 69 QLITE_OK && sz>i
7000: 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 Limit ){.
7010: 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 rc = sqlite3OsT
7020: 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e runcate(pPager->
7030: 6a 66 64 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 jfd, iLimit);.
7040: 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
7050: 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
7060: 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 /*.** The journa
7070: 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f l file must be o
7080: 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f pen when this ro
7090: 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e utine is called.
70a0: 20 41 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 68 65 A journal.** he
70b0: 61 64 65 72 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 ader (JOURNAL_HD
70c0: 52 5f 53 5a 20 62 79 74 65 73 29 20 69 73 20 77 R_SZ bytes) is w
70d0: 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
70e0: 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 74 20 journal file at
70f0: 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c the.** current l
7100: 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 ocation..**.** T
7110: 68 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 74 68 he format for th
7120: 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
7130: 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a is as follows:.
7140: 2a 2a 20 2d 20 38 20 62 79 74 65 73 3a 20 4d 61 ** - 8 bytes: Ma
7150: 67 69 63 20 69 64 65 6e 74 69 66 79 69 6e 67 20 gic identifying
7160: 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 2e 0a journal format..
7170: 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 4e 75 ** - 4 bytes: Nu
7180: 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64 73 20 mber of records
7190: 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 2d in journal, or -
71a0: 31 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 69 1 no-sync mode i
71b0: 73 20 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 s on..** - 4 byt
71c0: 65 73 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 es: Random numbe
71d0: 72 20 75 73 65 64 20 66 6f 72 20 70 61 67 65 20 r used for page
71e0: 68 61 73 68 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 hash..** - 4 byt
71f0: 65 73 3a 20 49 6e 69 74 69 61 6c 20 64 61 74 61 es: Initial data
7200: 62 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74 2e base page count.
7210: 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 53 .** - 4 bytes: S
7220: 65 63 74 6f 72 20 73 69 7a 65 20 75 73 65 64 20 ector size used
7230: 62 79 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 by the process t
7240: 68 61 74 20 77 72 6f 74 65 20 74 68 69 73 20 6a hat wrote this j
7250: 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 2d 20 34 20 62 ournal..** - 4 b
7260: 79 74 65 73 3a 20 44 61 74 61 62 61 73 65 20 70 ytes: Database p
7270: 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 0a 2a 2a age size..** .**
7280: 20 46 6f 6c 6c 6f 77 65 64 20 62 79 20 28 4a 4f Followed by (JO
7290: 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 URNAL_HDR_SZ - 2
72a0: 38 29 20 62 79 74 65 73 20 6f 66 20 75 6e 75 73 8) bytes of unus
72b0: 65 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 ed space..*/.sta
72c0: 74 69 63 20 69 6e 74 20 77 72 69 74 65 4a 6f 75 tic int writeJou
72d0: 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 rnalHdr(Pager *p
72e0: 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 Pager){. int rc
72f0: 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
7300: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
7310: 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
7320: 20 20 63 68 61 72 20 2a 7a 48 65 61 64 65 72 20 char *zHeader
7330: 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 = pPager->pTmpSp
7340: 61 63 65 3b 20 20 2f 2a 20 54 65 6d 70 6f 72 61 ace; /* Tempora
7350: 72 79 20 73 70 61 63 65 20 75 73 65 64 20 74 6f ry space used to
7360: 20 62 75 69 6c 64 20 68 65 61 64 65 72 20 2a 2f build header */
7370: 0a 20 20 75 33 32 20 6e 48 65 61 64 65 72 20 3d . u32 nHeader =
7380: 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
7390: 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f e; /* Size o
73a0: 66 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 f buffer pointed
73b0: 20 74 6f 20 62 79 20 7a 48 65 61 64 65 72 20 2a to by zHeader *
73c0: 2f 0a 20 20 75 33 32 20 6e 57 72 69 74 65 3b 20 /. u32 nWrite;
73d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
73e0: 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 /* Bytes
73f0: 20 6f 66 20 68 65 61 64 65 72 20 73 65 63 74 6f of header secto
7400: 72 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 r written */. i
7410: 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 nt ii;
7420: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7430: 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
7440: 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 er */.. assert(
7450: 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
7460: 6a 66 64 29 20 29 3b 20 20 20 20 20 20 2f 2a 20 jfd) ); /*
7470: 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 Journal file mus
7480: 74 20 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 t be open. */..
7490: 20 69 66 28 20 6e 48 65 61 64 65 72 3e 4a 4f 55 if( nHeader>JOU
74a0: 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
74b0: 65 72 29 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 er) ){. nHead
74c0: 65 72 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 er = JOURNAL_HDR
74d0: 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d _SZ(pPager);. }
74e0: 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 .. /* If there
74f0: 61 72 65 20 61 63 74 69 76 65 20 73 61 76 65 70 are active savep
7500: 6f 69 6e 74 73 20 61 6e 64 20 61 6e 79 20 6f 66 oints and any of
7510: 20 74 68 65 6d 20 77 65 72 65 20 63 72 65 61 74 them were creat
7520: 65 64 20 0a 20 20 2a 2a 20 73 69 6e 63 65 20 74 ed . ** since t
7530: 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 6a he most recent j
7540: 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 77 61 ournal header wa
7550: 73 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 s written, updat
7560: 65 20 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 e the . ** Page
7570: 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f rSavepoint.iHdrO
7580: 66 66 73 65 74 20 66 69 65 6c 64 73 20 6e 6f 77 ffset fields now
7590: 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d .. */. for(ii=
75a0: 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 0; ii<pPager->nS
75b0: 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b avepoint; ii++){
75c0: 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d . if( pPager-
75d0: 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e >aSavepoint[ii].
75e0: 69 48 64 72 4f 66 66 73 65 74 3d 3d 30 20 29 7b iHdrOffset==0 ){
75f0: 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61 . pPager->a
7600: 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 Savepoint[ii].iH
7610: 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 drOffset = pPage
7620: 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 r->journalOff;.
7630: 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61 67 }. }.. pPag
7640: 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d er->journalHdr =
7650: 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
7660: 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 Off = journalHdr
7670: 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a Offset(pPager);.
7680: 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 57 72 69 74 . /* . ** Writ
7690: 65 20 74 68 65 20 6e 52 65 63 20 46 69 65 6c 64 e the nRec Field
76a0: 20 2d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 - the number of
76b0: 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 74 68 page records th
76c0: 61 74 20 66 6f 6c 6c 6f 77 20 74 68 69 73 0a 20 at follow this.
76d0: 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 ** journal head
76e0: 65 72 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 7a 65 er. Normally, ze
76f0: 72 6f 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f ro is written to
7700: 20 74 68 69 73 20 76 61 6c 75 65 20 61 74 20 74 this value at t
7710: 68 69 73 20 74 69 6d 65 2e 0a 20 20 2a 2a 20 41 his time.. ** A
7720: 66 74 65 72 20 74 68 65 20 72 65 63 6f 72 64 73 fter the records
7730: 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74 68 are added to th
7740: 65 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 74 e journal (and t
7750: 68 65 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 65 he journal synce
7760: 64 2c 20 0a 20 20 2a 2a 20 69 66 20 69 6e 20 66 d, . ** if in f
7770: 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 29 2c 20 ull-sync mode),
7780: 74 68 65 20 7a 65 72 6f 20 69 73 20 6f 76 65 72 the zero is over
7790: 77 72 69 74 74 65 6e 20 77 69 74 68 20 74 68 65 written with the
77a0: 20 74 72 75 65 20 6e 75 6d 62 65 72 0a 20 20 2a true number. *
77b0: 2a 20 6f 66 20 72 65 63 6f 72 64 73 20 28 73 65 * of records (se
77c0: 65 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 29 29 e syncJournal())
77d0: 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 66 61 .. **. ** A fa
77e0: 73 74 65 72 20 61 6c 74 65 72 6e 61 74 69 76 65 ster alternative
77f0: 20 69 73 20 74 6f 20 77 72 69 74 65 20 30 78 46 is to write 0xF
7800: 46 46 46 46 46 46 46 20 74 6f 20 74 68 65 20 6e FFFFFFF to the n
7810: 52 65 63 20 66 69 65 6c 64 2e 20 57 68 65 6e 0a Rec field. When.
7820: 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 74 68 65 ** reading the
7830: 20 6a 6f 75 72 6e 61 6c 20 74 68 69 73 20 76 61 journal this va
7840: 6c 75 65 20 74 65 6c 6c 73 20 53 51 4c 69 74 65 lue tells SQLite
7850: 20 74 6f 20 61 73 73 75 6d 65 20 74 68 61 74 20 to assume that
7860: 74 68 65 0a 20 20 2a 2a 20 72 65 73 74 20 6f 66 the. ** rest of
7870: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
7880: 65 20 63 6f 6e 74 61 69 6e 73 20 76 61 6c 69 64 e contains valid
7890: 20 70 61 67 65 20 72 65 63 6f 72 64 73 2e 20 54 page records. T
78a0: 68 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 0a 20 his assumption.
78b0: 20 2a 2a 20 69 73 20 64 61 6e 67 65 72 6f 75 73 ** is dangerous
78c0: 2c 20 61 73 20 69 66 20 61 20 66 61 69 6c 75 72 , as if a failur
78d0: 65 20 6f 63 63 75 72 72 65 64 20 77 68 69 6c 73 e occurred whils
78e0: 74 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 t writing to the
78f0: 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 journal. ** fi
7900: 6c 65 20 69 74 20 6d 61 79 20 63 6f 6e 74 61 69 le it may contai
7910: 6e 20 73 6f 6d 65 20 67 61 72 62 61 67 65 20 64 n some garbage d
7920: 61 74 61 2e 20 54 68 65 72 65 20 61 72 65 20 74 ata. There are t
7930: 77 6f 20 73 63 65 6e 61 72 69 6f 73 0a 20 20 2a wo scenarios. *
7940: 2a 20 77 68 65 72 65 20 74 68 69 73 20 72 69 73 * where this ris
7950: 6b 20 63 61 6e 20 62 65 20 69 67 6e 6f 72 65 64 k can be ignored
7960: 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 :. **. ** *
7970: 57 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 69 When the pager i
7980: 73 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 s in no-sync mod
7990: 65 2e 20 43 6f 72 72 75 70 74 69 6f 6e 20 63 61 e. Corruption ca
79a0: 6e 20 66 6f 6c 6c 6f 77 20 61 0a 20 20 2a 2a 20 n follow a. **
79b0: 20 20 20 20 70 6f 77 65 72 20 66 61 69 6c 75 72 power failur
79c0: 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 61 e in this case a
79d0: 6e 79 77 61 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a nyway.. **. **
79e0: 20 20 20 2a 20 57 68 65 6e 20 74 68 65 20 53 51 * When the SQ
79f0: 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f LITE_IOCAP_SAFE_
7a00: 41 50 50 45 4e 44 20 66 6c 61 67 20 69 73 20 73 APPEND flag is s
7a10: 65 74 2e 20 54 68 69 73 20 67 75 61 72 61 6e 74 et. This guarant
7a20: 65 65 73 0a 20 20 2a 2a 20 20 20 20 20 74 68 61 ees. ** tha
7a30: 74 20 67 61 72 62 61 67 65 20 64 61 74 61 20 69 t garbage data i
7a40: 73 20 6e 65 76 65 72 20 61 70 70 65 6e 64 65 64 s never appended
7a50: 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 to the journal
7a60: 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 file.. */. ass
7a70: 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 ert( isOpen(pPag
7a80: 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 er->fd) || pPage
7a90: 72 2d 3e 6e 6f 53 79 6e 63 20 29 3b 0a 20 20 69 r->noSync );. i
7aa0: 66 28 20 28 70 50 61 67 65 72 2d 3e 6e 6f 53 79 f( (pPager->noSy
7ab0: 6e 63 29 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e nc) || (pPager->
7ac0: 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 journalMode==PAG
7ad0: 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d ER_JOURNALMODE_M
7ae0: 45 4d 4f 52 59 29 0a 20 20 20 7c 7c 20 28 73 71 EMORY). || (sq
7af0: 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 lite3OsDeviceCha
7b00: 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 racteristics(pPa
7b10: 67 65 72 2d 3e 66 64 29 26 53 51 4c 49 54 45 5f ger->fd)&SQLITE_
7b20: 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e IOCAP_SAFE_APPEN
7b30: 44 29 20 0a 20 20 29 7b 0a 20 20 20 20 6d 65 6d D) . ){. mem
7b40: 63 70 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f cpy(zHeader, aJo
7b50: 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 urnalMagic, size
7b60: 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 of(aJournalMagic
7b70: 29 29 3b 0a 20 20 20 20 70 75 74 33 32 62 69 74 ));. put32bit
7b80: 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f s(&zHeader[sizeo
7b90: 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 f(aJournalMagic)
7ba0: 5d 2c 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a ], 0xffffffff);.
7bb0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 48 65 }else{. zHe
7bc0: 61 64 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a ader[0] = '\0';.
7bd0: 20 20 20 20 70 75 74 33 32 62 69 74 73 28 26 7a put32bits(&z
7be0: 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a Header[sizeof(aJ
7bf0: 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 30 ournalMagic)], 0
7c00: 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 );. }.. /* The
7c10: 20 72 61 6e 64 6f 6d 20 63 68 65 63 6b 2d 68 61 random check-ha
7c20: 73 68 20 69 6e 69 74 69 61 6c 69 73 65 72 20 2a sh initialiser *
7c30: 2f 20 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e / . sqlite3_ran
7c40: 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 70 domness(sizeof(p
7c50: 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 Pager->cksumInit
7c60: 29 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 75 ), &pPager->cksu
7c70: 6d 49 6e 69 74 29 3b 0a 20 20 70 75 74 33 32 62 mInit);. put32b
7c80: 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a its(&zHeader[siz
7c90: 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 eof(aJournalMagi
7ca0: 63 29 2b 34 5d 2c 20 70 50 61 67 65 72 2d 3e 63 c)+4], pPager->c
7cb0: 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 2f 2a 20 ksumInit);. /*
7cc0: 54 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61 The initial data
7cd0: 62 61 73 65 20 73 69 7a 65 20 2a 2f 0a 20 20 70 base size */. p
7ce0: 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 ut32bits(&zHeade
7cf0: 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 r[sizeof(aJourna
7d00: 6c 4d 61 67 69 63 29 2b 38 5d 2c 20 70 50 61 67 lMagic)+8], pPag
7d10: 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 29 3b er->dbOrigSize);
7d20: 0a 20 20 2f 2a 20 54 68 65 20 61 73 73 75 6d 65 . /* The assume
7d30: 64 20 73 65 63 74 6f 72 20 73 69 7a 65 20 66 6f d sector size fo
7d40: 72 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 2a r this process *
7d50: 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 7a /. put32bits(&z
7d60: 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a Header[sizeof(aJ
7d70: 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 32 5d ournalMagic)+12]
7d80: 2c 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 , pPager->sector
7d90: 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 Size);.. /* The
7da0: 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 page size */.
7db0: 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 put32bits(&zHead
7dc0: 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e er[sizeof(aJourn
7dd0: 61 6c 4d 61 67 69 63 29 2b 31 36 5d 2c 20 70 50 alMagic)+16], pP
7de0: 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b ager->pageSize);
7df0: 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a .. /* Initializ
7e00: 69 6e 67 20 74 68 65 20 74 61 69 6c 20 6f 66 20 ing the tail of
7e10: 74 68 65 20 62 75 66 66 65 72 20 69 73 20 6e 6f the buffer is no
7e20: 74 20 6e 65 63 65 73 73 61 72 79 2e 20 20 45 76 t necessary. Ev
7e30: 65 72 79 74 68 69 6e 67 0a 20 20 2a 2a 20 77 6f erything. ** wo
7e40: 72 6b 73 20 66 69 6e 64 20 69 66 20 74 68 65 20 rks find if the
7e50: 66 6f 6c 6c 6f 77 69 6e 67 20 6d 65 6d 73 65 74 following memset
7e60: 28 29 20 69 73 20 6f 6d 69 74 74 65 64 2e 20 20 () is omitted.
7e70: 42 75 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 But initializing
7e80: 0a 20 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 . ** the memory
7e90: 20 70 72 65 76 65 6e 74 73 20 76 61 6c 67 72 69 prevents valgri
7ea0: 6e 64 20 66 72 6f 6d 20 63 6f 6d 70 6c 61 69 6e nd from complain
7eb0: 69 6e 67 2c 20 73 6f 20 77 65 20 61 72 65 20 77 ing, so we are w
7ec0: 69 6c 6c 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 illing to. ** t
7ed0: 61 6b 65 20 74 68 65 20 70 65 72 66 6f 72 6d 61 ake the performa
7ee0: 6e 63 65 20 68 69 74 2e 0a 20 20 2a 2f 0a 20 20 nce hit.. */.
7ef0: 6d 65 6d 73 65 74 28 26 7a 48 65 61 64 65 72 5b memset(&zHeader[
7f00: 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d sizeof(aJournalM
7f10: 61 67 69 63 29 2b 32 30 5d 2c 20 30 2c 0a 20 20 agic)+20], 0,.
7f20: 20 20 20 20 20 20 20 6e 48 65 61 64 65 72 2d 28 nHeader-(
7f30: 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d sizeof(aJournalM
7f40: 61 67 69 63 29 2b 32 30 29 29 3b 0a 0a 20 20 2f agic)+20));.. /
7f50: 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20 69 74 20 * In theory, it
7f60: 69 73 20 6f 6e 6c 79 20 6e 65 63 65 73 73 61 72 is only necessar
7f70: 79 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 32 y to write the 2
7f80: 38 20 62 79 74 65 73 20 74 68 61 74 20 74 68 65 8 bytes that the
7f90: 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68 . ** journal h
7fa0: 65 61 64 65 72 20 63 6f 6e 73 75 6d 65 73 20 74 eader consumes t
7fb0: 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 o the journal fi
7fc0: 6c 65 20 68 65 72 65 2e 20 54 68 65 6e 20 69 6e le here. Then in
7fd0: 63 72 65 6d 65 6e 74 20 74 68 65 20 0a 20 20 2a crement the . *
7fe0: 2a 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f * Pager.journalO
7ff0: 66 66 20 76 61 72 69 61 62 6c 65 20 62 79 20 4a ff variable by J
8000: 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 73 6f OURNAL_HDR_SZ so
8010: 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 20 0a that the next .
8020: 20 20 2a 2a 20 72 65 63 6f 72 64 20 69 73 20 77 ** record is w
8030: 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 66 6f ritten to the fo
8040: 6c 6c 6f 77 69 6e 67 20 73 65 63 74 6f 72 20 28 llowing sector (
8050: 6c 65 61 76 69 6e 67 20 61 20 67 61 70 20 69 6e leaving a gap in
8060: 20 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 74 the file. ** t
8070: 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6d 70 6c hat will be impl
8080: 69 63 69 74 6c 79 20 66 69 6c 6c 65 64 20 69 6e icitly filled in
8090: 20 62 79 20 74 68 65 20 4f 53 29 2e 0a 20 20 2a by the OS).. *
80a0: 2a 0a 20 20 2a 2a 20 48 6f 77 65 76 65 72 20 69 *. ** However i
80b0: 74 20 68 61 73 20 62 65 65 6e 20 64 69 73 63 6f t has been disco
80c0: 76 65 72 65 64 20 74 68 61 74 20 6f 6e 20 73 6f vered that on so
80d0: 6d 65 20 73 79 73 74 65 6d 73 20 74 68 69 73 20 me systems this
80e0: 70 61 74 74 65 72 6e 20 63 61 6e 20 0a 20 20 2a pattern can . *
80f0: 2a 20 62 65 20 73 69 67 6e 69 66 69 63 61 6e 74 * be significant
8100: 6c 79 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 63 ly slower than c
8110: 6f 6e 74 69 67 75 6f 75 73 6c 79 20 77 72 69 74 ontiguously writ
8120: 69 6e 67 20 64 61 74 61 20 74 6f 20 74 68 65 20 ing data to the
8130: 66 69 6c 65 2c 0a 20 20 2a 2a 20 65 76 65 6e 20 file,. ** even
8140: 69 66 20 74 68 61 74 20 6d 65 61 6e 73 20 65 78 if that means ex
8150: 70 6c 69 63 69 74 6c 79 20 77 72 69 74 69 6e 67 plicitly writing
8160: 20 64 61 74 61 20 74 6f 20 74 68 65 20 62 6c 6f data to the blo
8170: 63 6b 20 6f 66 20 0a 20 20 2a 2a 20 28 4a 4f 55 ck of . ** (JOU
8180: 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 RNAL_HDR_SZ - 28
8190: 29 20 62 79 74 65 73 20 74 68 61 74 20 77 69 6c ) bytes that wil
81a0: 6c 20 6e 6f 74 20 62 65 20 75 73 65 64 2e 20 53 l not be used. S
81b0: 6f 20 74 68 61 74 20 69 73 20 77 68 61 74 0a 20 o that is what.
81c0: 20 2a 2a 20 69 73 20 64 6f 6e 65 2e 20 0a 20 20 ** is done. .
81d0: 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 6c 6f 6f 70 **. ** The loop
81e0: 20 69 73 20 72 65 71 75 69 72 65 64 20 68 65 72 is required her
81f0: 65 20 69 6e 20 63 61 73 65 20 74 68 65 20 73 65 e in case the se
8200: 63 74 6f 72 2d 73 69 7a 65 20 69 73 20 6c 61 72 ctor-size is lar
8210: 67 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20 ger than the .
8220: 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 65 ** database page
8230: 20 73 69 7a 65 2e 20 53 69 6e 63 65 20 74 68 65 size. Since the
8240: 20 7a 48 65 61 64 65 72 20 62 75 66 66 65 72 20 zHeader buffer
8250: 69 73 20 6f 6e 6c 79 20 50 61 67 65 72 2e 70 61 is only Pager.pa
8260: 67 65 53 69 7a 65 0a 20 20 2a 2a 20 62 79 74 65 geSize. ** byte
8270: 73 20 69 6e 20 73 69 7a 65 2c 20 6d 6f 72 65 20 s in size, more
8280: 74 68 61 6e 20 6f 6e 65 20 63 61 6c 6c 20 74 6f than one call to
8290: 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
82a0: 29 20 6d 61 79 20 62 65 20 72 65 71 75 69 72 65 ) may be require
82b0: 64 0a 20 20 2a 2a 20 74 6f 20 70 6f 70 75 6c 61 d. ** to popula
82c0: 74 65 20 74 68 65 20 65 6e 74 69 72 65 20 6a 6f te the entire jo
82d0: 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 65 63 urnal header sec
82e0: 74 6f 72 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 tor.. */ . for
82f0: 28 6e 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d 53 (nWrite=0; rc==S
8300: 51 4c 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74 65 QLITE_OK&&nWrite
8310: 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 <JOURNAL_HDR_SZ(
8320: 70 50 61 67 65 72 29 3b 20 6e 57 72 69 74 65 2b pPager); nWrite+
8330: 3d 6e 48 65 61 64 65 72 29 7b 0a 20 20 20 20 49 =nHeader){. I
8340: 4f 54 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 OTRACE(("JHDR %p
8350: 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 %lld %d\n", pPa
8360: 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 ger, pPager->jou
8370: 72 6e 61 6c 48 64 72 2c 20 6e 48 65 61 64 65 72 rnalHdr, nHeader
8380: 29 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 )). rc = sqli
8390: 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 te3OsWrite(pPage
83a0: 72 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c r->jfd, zHeader,
83b0: 20 6e 48 65 61 64 65 72 2c 20 70 50 61 67 65 72 nHeader, pPager
83c0: 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 ->journalOff);.
83d0: 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
83e0: 61 6c 4f 66 66 20 2b 3d 20 6e 48 65 61 64 65 72 alOff += nHeader
83f0: 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
8400: 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 rc;.}../*.** The
8410: 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 journal file mu
8420: 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 st be open when
8430: 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20 this is called.
8440: 41 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 A journal header
8450: 20 66 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 4e 41 file.** (JOURNA
8460: 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 L_HDR_SZ bytes)
8470: 69 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 is read from the
8480: 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f current locatio
8490: 6e 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c n in the journal
84a0: 0a 2a 2a 20 66 69 6c 65 2e 20 54 68 65 20 63 75 .** file. The cu
84b0: 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 69 rrent location i
84c0: 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
84d0: 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a le is given by.*
84e0: 2a 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 * pPager->journa
84f0: 6c 4f 66 66 2e 20 53 65 65 20 63 6f 6d 6d 65 6e lOff. See commen
8500: 74 73 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f ts above functio
8510: 6e 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 n writeJournalHd
8520: 72 28 29 20 66 6f 72 0a 2a 2a 20 61 20 64 65 73 r() for.** a des
8530: 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 cription of the
8540: 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 journal header f
8550: 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ormat..**.** If
8560: 74 68 65 20 68 65 61 64 65 72 20 69 73 20 72 65 the header is re
8570: 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c ad successfully,
8580: 20 2a 70 4e 52 65 63 20 69 73 20 73 65 74 20 74 *pNRec is set t
8590: 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a o the number of.
85a0: 2a 2a 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 ** page records
85b0: 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 68 following this h
85c0: 65 61 64 65 72 20 61 6e 64 20 2a 70 44 62 53 69 eader and *pDbSi
85d0: 7a 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 ze is set to the
85e0: 20 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 size of the.**
85f0: 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 20 database before
8600: 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
8610: 62 65 67 61 6e 2c 20 69 6e 20 70 61 67 65 73 2e began, in pages.
8620: 20 41 6c 73 6f 2c 20 70 50 61 67 65 72 2d 3e 63 Also, pPager->c
8630: 6b 73 75 6d 49 6e 69 74 0a 2a 2a 20 69 73 20 73 ksumInit.** is s
8640: 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 et to the value
8650: 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f read from the jo
8660: 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 53 51 urnal header. SQ
8670: 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
8680: 6e 65 64 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 ned.** in this c
8690: 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ase..**.** If th
86a0: 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
86b0: 20 66 69 6c 65 20 61 70 70 65 61 72 73 20 74 6f file appears to
86c0: 20 62 65 20 63 6f 72 72 75 70 74 65 64 2c 20 53 be corrupted, S
86d0: 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 0a 2a 2a QLITE_DONE is.**
86e0: 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 returned and *p
86f0: 4e 52 65 63 20 61 6e 64 20 2a 50 44 62 53 69 7a NRec and *PDbSiz
8700: 65 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e e are undefined.
8710: 20 20 49 66 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 If JOURNAL_HDR
8720: 5f 53 5a 20 62 79 74 65 73 0a 2a 2a 20 63 61 6e _SZ bytes.** can
8730: 6e 6f 74 20 62 65 20 72 65 61 64 20 66 72 6f 6d not be read from
8740: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
8750: 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 e an error code
8760: 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a is returned..*/.
8770: 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 4a static int readJ
8780: 6f 75 72 6e 61 6c 48 64 72 28 0a 20 20 50 61 67 ournalHdr(. Pag
8790: 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 er *pPager,
87a0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
87b0: 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 er object */. i
87c0: 6e 74 20 69 73 48 6f 74 2c 0a 20 20 69 36 34 20 nt isHot,. i64
87d0: 6a 6f 75 72 6e 61 6c 53 69 7a 65 2c 20 20 20 20 journalSize,
87e0: 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
87f0: 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 6a 6f 75 of the open jou
8800: 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 rnal file in byt
8810: 65 73 20 2a 2f 0a 20 20 75 33 32 20 2a 70 4e 52 es */. u32 *pNR
8820: 65 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ec,
8830: 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 56 61 6c /* OUT: Val
8840: 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 ue read from the
8850: 20 6e 52 65 63 20 66 69 65 6c 64 20 2a 2f 0a 20 nRec field */.
8860: 20 75 33 32 20 2a 70 44 62 53 69 7a 65 20 20 20 u32 *pDbSize
8870: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
8880: 20 4f 55 54 3a 20 56 61 6c 75 65 20 6f 66 20 6f OUT: Value of o
8890: 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 riginal database
88a0: 20 73 69 7a 65 20 66 69 65 6c 64 20 2a 2f 0a 29 size field */.)
88b0: 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
88c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
88d0: 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
88e0: 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
88f0: 61 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20 ar aMagic[8];
8900: 20 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f /* A buffer to
8910: 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 20 hold the magic
8920: 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20 header */. i64
8930: 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 iHdrOff;
8940: 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
8950: 65 74 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 et of journal he
8960: 61 64 65 72 20 62 65 69 6e 67 20 72 65 61 64 20 ader being read
8970: 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 73 */.. assert( is
8980: 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
8990: 29 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f 75 ) ); /* Jou
89a0: 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 rnal file must b
89b0: 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 2f 2a e open. */.. /*
89c0: 20 41 64 76 61 6e 63 65 20 50 61 67 65 72 2e 6a Advance Pager.j
89d0: 6f 75 72 6e 61 6c 4f 66 66 20 74 6f 20 74 68 65 ournalOff to the
89e0: 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6e 65 start of the ne
89f0: 78 74 20 73 65 63 74 6f 72 2e 20 49 66 20 74 68 xt sector. If th
8a00: 65 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 e. ** journal f
8a10: 69 6c 65 20 69 73 20 74 6f 6f 20 73 6d 61 6c 6c ile is too small
8a20: 20 66 6f 72 20 74 68 65 72 65 20 74 6f 20 62 65 for there to be
8a30: 20 61 20 68 65 61 64 65 72 20 73 74 6f 72 65 64 a header stored
8a40: 20 61 74 20 74 68 69 73 0a 20 20 2a 2a 20 70 6f at this. ** po
8a50: 69 6e 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 int, return SQLI
8a60: 54 45 5f 44 4f 4e 45 2e 0a 20 20 2a 2f 0a 20 20 TE_DONE.. */.
8a70: 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
8a80: 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f ff = journalHdrO
8a90: 66 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 ffset(pPager);.
8aa0: 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 if( pPager->jou
8ab0: 72 6e 61 6c 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f rnalOff+JOURNAL_
8ac0: 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 3e HDR_SZ(pPager) >
8ad0: 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 20 29 7b 0a journalSize ){.
8ae0: 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
8af0: 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 48 E_DONE;. }. iH
8b00: 64 72 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e drOff = pPager->
8b10: 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f journalOff;.. /
8b20: 2a 20 52 65 61 64 20 69 6e 20 74 68 65 20 66 69 * Read in the fi
8b30: 72 73 74 20 38 20 62 79 74 65 73 20 6f 66 20 74 rst 8 bytes of t
8b40: 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 he journal heade
8b50: 72 2e 20 49 66 20 74 68 65 79 20 64 6f 20 6e 6f r. If they do no
8b60: 74 20 6d 61 74 63 68 0a 20 20 2a 2a 20 74 68 65 t match. ** the
8b70: 20 20 6d 61 67 69 63 20 73 74 72 69 6e 67 20 66 magic string f
8b80: 6f 75 6e 64 20 61 74 20 74 68 65 20 73 74 61 72 ound at the star
8b90: 74 20 6f 66 20 65 61 63 68 20 6a 6f 75 72 6e 61 t of each journa
8ba0: 6c 20 68 65 61 64 65 72 2c 20 72 65 74 75 72 6e l header, return
8bb0: 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 4f 4e . ** SQLITE_DON
8bc0: 45 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f E. If an IO erro
8bd0: 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e r occurs, return
8be0: 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 an error code.
8bf0: 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 2a 2a 20 Otherwise,. **
8c00: 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 proceed.. */.
8c10: 69 66 28 20 69 73 48 6f 74 20 7c 7c 20 69 48 64 if( isHot || iHd
8c20: 72 4f 66 66 21 3d 70 50 61 67 65 72 2d 3e 6a 6f rOff!=pPager->jo
8c30: 75 72 6e 61 6c 48 64 72 20 29 7b 0a 20 20 20 20 urnalHdr ){.
8c40: 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 rc = sqlite3OsRe
8c50: 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ad(pPager->jfd,
8c60: 61 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 aMagic, sizeof(a
8c70: 4d 61 67 69 63 29 2c 20 69 48 64 72 4f 66 66 29 Magic), iHdrOff)
8c80: 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a ;. if( rc ){.
8c90: 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
8ca0: 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d . }. if( m
8cb0: 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a emcmp(aMagic, aJ
8cc0: 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a ournalMagic, siz
8cd0: 65 6f 66 28 61 4d 61 67 69 63 29 29 21 3d 30 20 eof(aMagic))!=0
8ce0: 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
8cf0: 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 SQLITE_DONE;.
8d00: 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 }. }.. /* Rea
8d10: 64 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65 d the first thre
8d20: 65 20 33 32 2d 62 69 74 20 66 69 65 6c 64 73 20 e 32-bit fields
8d30: 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 of the journal h
8d40: 65 61 64 65 72 3a 20 54 68 65 20 6e 52 65 63 0a eader: The nRec.
8d50: 20 20 2a 2a 20 66 69 65 6c 64 2c 20 74 68 65 20 ** field, the
8d60: 63 68 65 63 6b 73 75 6d 2d 69 6e 69 74 69 61 6c checksum-initial
8d70: 69 7a 65 72 20 61 6e 64 20 74 68 65 20 64 61 74 izer and the dat
8d80: 61 62 61 73 65 20 73 69 7a 65 20 61 74 20 74 68 abase size at th
8d90: 65 20 73 74 61 72 74 0a 20 20 2a 2a 20 6f 66 20 e start. ** of
8da0: 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e the transaction.
8db0: 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 Return an error
8dc0: 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e code if anythin
8dd0: 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 20 20 g goes wrong..
8de0: 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f */. if( SQLITE_
8df0: 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 OK!=(rc = read32
8e00: 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
8e10: 2c 20 69 48 64 72 4f 66 66 2b 38 2c 20 70 4e 52 , iHdrOff+8, pNR
8e20: 65 63 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 ec)). || SQLIT
8e30: 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 E_OK!=(rc = read
8e40: 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
8e50: 66 64 2c 20 69 48 64 72 4f 66 66 2b 31 32 2c 20 fd, iHdrOff+12,
8e60: 26 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e &pPager->cksumIn
8e70: 69 74 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 it)). || SQLIT
8e80: 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 E_OK!=(rc = read
8e90: 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
8ea0: 66 64 2c 20 69 48 64 72 4f 66 66 2b 31 36 2c 20 fd, iHdrOff+16,
8eb0: 70 44 62 53 69 7a 65 29 29 0a 20 20 29 7b 0a 20 pDbSize)). ){.
8ec0: 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
8ed0: 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d }.. if( pPager-
8ee0: 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 >journalOff==0 )
8ef0: 7b 0a 20 20 20 20 75 33 32 20 69 50 61 67 65 53 {. u32 iPageS
8f00: 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
8f10: 20 20 20 2f 2a 20 50 61 67 65 2d 73 69 7a 65 20 /* Page-size
8f20: 66 69 65 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c field of journal
8f30: 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 header */. u
8f40: 33 32 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 20 32 iSectorSize;
8f50: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
8f60: 65 63 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c 64 ector-size field
8f70: 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 of journal head
8f80: 65 72 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 50 er */. u16 iP
8f90: 61 67 65 53 69 7a 65 31 36 3b 20 20 20 20 20 20 ageSize16;
8fa0: 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f /* Copy o
8fb0: 66 20 69 50 61 67 65 53 69 7a 65 20 69 6e 20 31 f iPageSize in 1
8fc0: 36 2d 62 69 74 20 76 61 72 69 61 62 6c 65 20 2a 6-bit variable *
8fd0: 2f 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 /.. /* Read t
8fe0: 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 he page-size and
8ff0: 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 6a 6f 75 sector-size jou
9000: 72 6e 61 6c 20 68 65 61 64 65 72 20 66 69 65 6c rnal header fiel
9010: 64 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 ds. */. if( S
9020: 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 QLITE_OK!=(rc =
9030: 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67 65 read32bits(pPage
9040: 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b r->jfd, iHdrOff+
9050: 32 30 2c 20 26 69 53 65 63 74 6f 72 53 69 7a 65 20, &iSectorSize
9060: 29 29 0a 20 20 20 20 20 7c 7c 20 53 51 4c 49 54 )). || SQLIT
9070: 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 E_OK!=(rc = read
9080: 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
9090: 66 64 2c 20 69 48 64 72 4f 66 66 2b 32 34 2c 20 fd, iHdrOff+24,
90a0: 26 69 50 61 67 65 53 69 7a 65 29 29 0a 20 20 20 &iPageSize)).
90b0: 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
90c0: 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 rc;. }..
90d0: 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 /* Check that th
90e0: 65 20 76 61 6c 75 65 73 20 72 65 61 64 20 66 72 e values read fr
90f0: 6f 6d 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 om the page-size
9100: 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 and sector-size
9110: 20 66 69 65 6c 64 73 0a 20 20 20 20 2a 2a 20 61 fields. ** a
9120: 72 65 20 77 69 74 68 69 6e 20 72 61 6e 67 65 2e re within range.
9130: 20 54 6f 20 62 65 20 27 69 6e 20 72 61 6e 67 65 To be 'in range
9140: 27 2c 20 62 6f 74 68 20 76 61 6c 75 65 73 20 6e ', both values n
9150: 65 65 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 eed to be a powe
9160: 72 0a 20 20 20 20 2a 2a 20 6f 66 20 74 77 6f 20 r. ** of two
9170: 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 greater than or
9180: 65 71 75 61 6c 20 74 6f 20 35 31 32 2c 20 61 6e equal to 512, an
9190: 64 20 6e 6f 74 20 67 72 65 61 74 65 72 20 74 68 d not greater th
91a0: 61 6e 20 74 68 65 69 72 20 0a 20 20 20 20 2a 2a an their . **
91b0: 20 72 65 73 70 65 63 74 69 76 65 20 63 6f 6d 70 respective comp
91c0: 69 6c 65 20 74 69 6d 65 20 6d 61 78 69 6d 75 6d ile time maximum
91d0: 20 6c 69 6d 69 74 73 2e 0a 20 20 20 20 2a 2f 0a limits.. */.
91e0: 20 20 20 20 69 66 28 20 69 50 61 67 65 53 69 7a if( iPageSiz
91f0: 65 3c 35 31 32 20 20 20 20 20 20 20 20 20 20 20 e<512
9200: 20 20 20 20 20 20 20 7c 7c 20 69 53 65 63 74 6f || iSecto
9210: 72 53 69 7a 65 3c 35 31 32 0a 20 20 20 20 20 7c rSize<512. |
9220: 7c 20 69 50 61 67 65 53 69 7a 65 3e 53 51 4c 49 | iPageSize>SQLI
9230: 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 TE_MAX_PAGE_SIZE
9240: 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 3e || iSectorSize>
9250: 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 0a MAX_SECTOR_SIZE.
9260: 20 20 20 20 20 7c 7c 20 28 28 69 50 61 67 65 53 || ((iPageS
9270: 69 7a 65 2d 31 29 26 69 50 61 67 65 53 69 7a 65 ize-1)&iPageSize
9280: 29 21 3d 30 20 20 20 7c 7c 20 28 28 69 53 65 63 )!=0 || ((iSec
9290: 74 6f 72 53 69 7a 65 2d 31 29 26 69 53 65 63 74 torSize-1)&iSect
92a0: 6f 72 53 69 7a 65 29 21 3d 30 20 0a 20 20 20 20 orSize)!=0 .
92b0: 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 ){. /* If t
92c0: 68 65 20 65 69 74 68 65 72 20 74 68 65 20 70 61 he either the pa
92d0: 67 65 2d 73 69 7a 65 20 6f 72 20 73 65 63 74 6f ge-size or secto
92e0: 72 2d 73 69 7a 65 20 69 6e 20 74 68 65 20 6a 6f r-size in the jo
92f0: 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 urnal-header is
9300: 0a 20 20 20 20 20 20 2a 2a 20 69 6e 76 61 6c 69 . ** invali
9310: 64 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f 63 d, then the proc
9320: 65 73 73 20 74 68 61 74 20 77 72 6f 74 65 20 74 ess that wrote t
9330: 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 he journal-heade
9340: 72 20 6d 75 73 74 20 68 61 76 65 20 0a 20 20 20 r must have .
9350: 20 20 20 2a 2a 20 63 72 61 73 68 65 64 20 62 65 ** crashed be
9360: 66 6f 72 65 20 74 68 65 20 68 65 61 64 65 72 20 fore the header
9370: 77 61 73 20 73 79 6e 63 65 64 2e 20 49 6e 20 74 was synced. In t
9380: 68 69 73 20 63 61 73 65 20 73 74 6f 70 20 72 65 his case stop re
9390: 61 64 69 6e 67 20 0a 20 20 20 20 20 20 2a 2a 20 ading . **
93a0: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
93b0: 20 68 65 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a here.. */.
93c0: 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
93d0: 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a ITE_DONE;. }.
93e0: 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 . /* Update t
93f0: 68 65 20 70 61 67 65 2d 73 69 7a 65 20 74 6f 20 he page-size to
9400: 6d 61 74 63 68 20 74 68 65 20 76 61 6c 75 65 20 match the value
9410: 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f read from the jo
9420: 75 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2a 20 55 urnal. . ** U
9430: 73 65 20 61 20 74 65 73 74 63 61 73 65 28 29 20 se a testcase()
9440: 6d 61 63 72 6f 20 74 6f 20 6d 61 6b 65 20 73 75 macro to make su
9450: 72 65 20 74 68 61 74 20 6d 61 6c 6c 6f 63 20 66 re that malloc f
9460: 61 69 6c 75 72 65 20 77 69 74 68 69 6e 20 0a 20 ailure within .
9470: 20 20 20 2a 2a 20 50 61 67 65 72 53 65 74 50 61 ** PagerSetPa
9480: 67 65 73 69 7a 65 28 29 20 69 73 20 74 65 73 74 gesize() is test
9490: 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ed.. */. i
94a0: 50 61 67 65 53 69 7a 65 31 36 20 3d 20 28 75 31 PageSize16 = (u1
94b0: 36 29 69 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 6)iPageSize;.
94c0: 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
94d0: 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 50 erSetPagesize(pP
94e0: 61 67 65 72 2c 20 26 69 50 61 67 65 53 69 7a 65 ager, &iPageSize
94f0: 31 36 2c 20 2d 31 29 3b 0a 20 20 20 20 74 65 73 16, -1);. tes
9500: 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 tcase( rc!=SQLIT
9510: 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 E_OK );. asse
9520: 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f rt( rc!=SQLITE_O
9530: 4b 20 7c 7c 20 69 50 61 67 65 53 69 7a 65 31 36 K || iPageSize16
9540: 3d 3d 28 75 31 36 29 69 50 61 67 65 53 69 7a 65 ==(u16)iPageSize
9550: 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 );.. /* Upda
9560: 74 65 20 74 68 65 20 61 73 73 75 6d 65 64 20 73 te the assumed s
9570: 65 63 74 6f 72 2d 73 69 7a 65 20 74 6f 20 6d 61 ector-size to ma
9580: 74 63 68 20 74 68 65 20 76 61 6c 75 65 20 75 73 tch the value us
9590: 65 64 20 62 79 20 0a 20 20 20 20 2a 2a 20 74 68 ed by . ** th
95a0: 65 20 70 72 6f 63 65 73 73 20 74 68 61 74 20 63 e process that c
95b0: 72 65 61 74 65 64 20 74 68 69 73 20 6a 6f 75 72 reated this jour
95c0: 6e 61 6c 2e 20 49 66 20 74 68 69 73 20 6a 6f 75 nal. If this jou
95d0: 72 6e 61 6c 20 77 61 73 0a 20 20 20 20 2a 2a 20 rnal was. **
95e0: 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 6f created by a pro
95f0: 63 65 73 73 20 6f 74 68 65 72 20 74 68 61 6e 20 cess other than
9600: 74 68 69 73 20 6f 6e 65 2c 20 74 68 65 6e 20 74 this one, then t
9610: 68 69 73 20 72 6f 75 74 69 6e 65 0a 20 20 20 20 his routine.
9620: 2a 2a 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c ** is being call
9630: 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 ed from within p
9640: 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 2e ager_playback().
9650: 20 54 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 The local value
9660: 0a 20 20 20 20 2a 2a 20 6f 66 20 50 61 67 65 72 . ** of Pager
9670: 2e 73 65 63 74 6f 72 53 69 7a 65 20 69 73 20 72 .sectorSize is r
9680: 65 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 65 estored at the e
9690: 6e 64 20 6f 66 20 74 68 61 74 20 72 6f 75 74 69 nd of that routi
96a0: 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 ne.. */. p
96b0: 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a Pager->sectorSiz
96c0: 65 20 3d 20 69 53 65 63 74 6f 72 53 69 7a 65 3b e = iSectorSize;
96d0: 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 72 2d 3e . }.. pPager->
96e0: 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 4a 4f journalOff += JO
96f0: 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
9700: 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 ger);. return r
9710: 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 c;.}.../*.** Wri
9720: 74 65 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 te the supplied
9730: 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
9740: 61 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 ame into the jou
9750: 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 rnal file for pa
9760: 67 65 72 0a 2a 2a 20 70 50 61 67 65 72 20 61 74 ger.** pPager at
9770: 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 the current loc
9780: 61 74 69 6f 6e 2e 20 54 68 65 20 6d 61 73 74 65 ation. The maste
9790: 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 6d r journal name m
97a0: 75 73 74 20 62 65 20 74 68 65 20 6c 61 73 74 0a ust be the last.
97b0: 2a 2a 20 74 68 69 6e 67 20 77 72 69 74 74 65 6e ** thing written
97c0: 20 74 6f 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 to a journal fi
97d0: 6c 65 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 le. If the pager
97e0: 20 69 73 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 is in full-sync
97f0: 20 6d 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 6a 6f mode, the.** jo
9800: 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 urnal file descr
9810: 69 70 74 6f 72 20 69 73 20 61 64 76 61 6e 63 65 iptor is advance
9820: 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65 d to the next se
9830: 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 20 62 65 ctor boundary be
9840: 66 6f 72 65 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 fore.** anything
9850: 20 69 73 20 77 72 69 74 74 65 6e 2e 20 54 68 65 is written. The
9860: 20 66 6f 72 6d 61 74 20 69 73 3a 0a 2a 2a 0a 2a format is:.**.*
9870: 2a 20 20 20 2b 20 34 20 62 79 74 65 73 3a 20 50 * + 4 bytes: P
9880: 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 2e 0a 2a 2a AGER_MJ_PGNO..**
9890: 20 20 20 2b 20 4e 20 62 79 74 65 73 3a 20 4d 61 + N bytes: Ma
98a0: 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
98b0: 65 6e 61 6d 65 20 69 6e 20 75 74 66 2d 38 2e 0a ename in utf-8..
98c0: 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 73 3a 20 ** + 4 bytes:
98d0: 4e 20 28 6c 65 6e 67 74 68 20 6f 66 20 6d 61 73 N (length of mas
98e0: 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 ter journal name
98f0: 20 69 6e 20 62 79 74 65 73 2c 20 6e 6f 20 6e 75 in bytes, no nu
9900: 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 29 2e 0a 2a l-terminator)..*
9910: 2a 20 20 20 2b 20 34 20 62 79 74 65 73 3a 20 4d * + 4 bytes: M
9920: 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
9930: 6d 65 20 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a 20 me checksum..**
9940: 20 20 2b 20 38 20 62 79 74 65 73 3a 20 61 4a 6f + 8 bytes: aJo
9950: 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a urnalMagic[]..**
9960: 0a 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 6a .** The master j
9970: 6f 75 72 6e 61 6c 20 70 61 67 65 20 63 68 65 63 ournal page chec
9980: 6b 73 75 6d 20 69 73 20 74 68 65 20 73 75 6d 20 ksum is the sum
9990: 6f 66 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 of the bytes in
99a0: 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f the master.** jo
99b0: 75 72 6e 61 6c 20 6e 61 6d 65 2c 20 77 68 65 72 urnal name, wher
99c0: 65 20 65 61 63 68 20 62 79 74 65 20 69 73 20 69 e each byte is i
99d0: 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61 20 nterpreted as a
99e0: 73 69 67 6e 65 64 20 38 2d 62 69 74 20 69 6e 74 signed 8-bit int
99f0: 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a eger..**.** If z
9a00: 4d 61 73 74 65 72 20 69 73 20 61 20 4e 55 4c 4c Master is a NULL
9a10: 20 70 6f 69 6e 74 65 72 20 28 6f 63 63 75 72 73 pointer (occurs
9a20: 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 for a single da
9a30: 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 tabase transacti
9a40: 6f 6e 29 2c 20 0a 2a 2a 20 74 68 69 73 20 63 61 on), .** this ca
9a50: 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a ll is a no-op..*
9a60: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 /.static int wri
9a70: 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 teMasterJournal(
9a80: 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 Pager *pPager, c
9a90: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 onst char *zMast
9aa0: 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 er){. int rc;
9ab0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ac0: 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
9ad0: 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 n code */. int
9ae0: 6e 4d 61 73 74 65 72 3b 20 20 20 20 20 20 20 20 nMaster;
9af0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
9b00: 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67 Length of string
9b10: 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 20 20 69 36 zMaster */. i6
9b20: 34 20 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20 4 iHdrOff;
9b30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
9b40: 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65 61 64 * Offset of head
9b50: 65 72 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 er in journal fi
9b60: 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a 72 6e 6c le */. i64 jrnl
9b70: 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 Size;
9b80: 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
9b90: 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 of journal file
9ba0: 20 6f 6e 20 64 69 73 6b 20 2a 2f 0a 20 20 75 33 on disk */. u3
9bb0: 32 20 63 6b 73 75 6d 20 3d 20 30 3b 20 20 20 20 2 cksum = 0;
9bc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
9bd0: 2a 20 43 68 65 63 6b 73 75 6d 20 6f 66 20 73 74 * Checksum of st
9be0: 72 69 6e 67 20 7a 4d 61 73 74 65 72 20 2a 2f 0a ring zMaster */.
9bf0: 0a 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 . if( !zMaster
9c00: 7c 7c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 || pPager->setMa
9c10: 73 74 65 72 0a 20 20 20 7c 7c 20 70 50 61 67 65 ster. || pPage
9c20: 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d r->journalMode==
9c30: 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
9c40: 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 7c 7c 20 E_MEMORY . ||
9c50: 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
9c60: 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
9c70: 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 29 7b ALMODE_OFF . ){
9c80: 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
9c90: 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 50 61 TE_OK;. }. pPa
9ca0: 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d ger->setMaster =
9cb0: 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 1;. assert( is
9cc0: 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
9cd0: 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 ) );.. /* Calcu
9ce0: 6c 61 74 65 20 74 68 65 20 6c 65 6e 67 74 68 20 late the length
9cf0: 69 6e 20 62 79 74 65 73 20 61 6e 64 20 74 68 65 in bytes and the
9d00: 20 63 68 65 63 6b 73 75 6d 20 6f 66 20 7a 4d 61 checksum of zMa
9d10: 73 74 65 72 20 2a 2f 0a 20 20 66 6f 72 28 6e 4d ster */. for(nM
9d20: 61 73 74 65 72 3d 30 3b 20 7a 4d 61 73 74 65 72 aster=0; zMaster
9d30: 5b 6e 4d 61 73 74 65 72 5d 3b 20 6e 4d 61 73 74 [nMaster]; nMast
9d40: 65 72 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d er++){. cksum
9d50: 20 2b 3d 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 += zMaster[nMas
9d60: 74 65 72 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 ter];. }.. /*
9d70: 49 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 If in full-sync
9d80: 6d 6f 64 65 2c 20 61 64 76 61 6e 63 65 20 74 6f mode, advance to
9d90: 20 74 68 65 20 6e 65 78 74 20 64 69 73 6b 20 73 the next disk s
9da0: 65 63 74 6f 72 20 62 65 66 6f 72 65 20 77 72 69 ector before wri
9db0: 74 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d 61 ting. ** the ma
9dc0: 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster journal nam
9dd0: 65 2e 20 54 68 69 73 20 69 73 20 69 6e 20 63 61 e. This is in ca
9de0: 73 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 se the previous
9df0: 70 61 67 65 20 77 72 69 74 74 65 6e 20 74 6f 0a page written to.
9e00: 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c ** the journal
9e10: 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
9e20: 6e 20 73 79 6e 63 65 64 2e 0a 20 20 2a 2f 0a 20 n synced.. */.
9e30: 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c if( pPager->ful
9e40: 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 70 50 61 lSync ){. pPa
9e50: 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
9e60: 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 = journalHdrOffs
9e70: 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a et(pPager);. }.
9e80: 20 20 69 48 64 72 4f 66 66 20 3d 20 70 50 61 67 iHdrOff = pPag
9e90: 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a er->journalOff;.
9ea0: 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 . /* Write the
9eb0: 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 64 master journal d
9ec0: 61 74 61 20 74 6f 20 74 68 65 20 65 6e 64 20 6f ata to the end o
9ed0: 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
9ee0: 6c 65 2e 20 49 66 0a 20 20 2a 2a 20 61 6e 20 65 le. If. ** an e
9ef0: 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 rror occurs, ret
9f00: 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f urn the error co
9f10: 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 de to the caller
9f20: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 30 20 .. */. if( (0
9f30: 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65 33 32 != (rc = write32
9f40: 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
9f50: 2c 20 69 48 64 72 4f 66 66 2c 20 50 41 47 45 52 , iHdrOff, PAGER
9f60: 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 _MJ_PGNO(pPager)
9f70: 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 ))). || (0 !=
9f80: 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 (rc = sqlite3OsW
9f90: 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 rite(pPager->jfd
9fa0: 2c 20 7a 4d 61 73 74 65 72 2c 20 6e 4d 61 73 74 , zMaster, nMast
9fb0: 65 72 2c 20 69 48 64 72 4f 66 66 2b 34 29 29 29 er, iHdrOff+4)))
9fc0: 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 . || (0 != (rc
9fd0: 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 = write32bits(p
9fe0: 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 Pager->jfd, iHdr
9ff0: 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2c 20 6e Off+4+nMaster, n
a000: 4d 61 73 74 65 72 29 29 29 0a 20 20 20 7c 7c 20 Master))). ||
a010: 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74 (0 != (rc = writ
a020: 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e e32bits(pPager->
a030: 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e jfd, iHdrOff+4+n
a040: 4d 61 73 74 65 72 2b 34 2c 20 63 6b 73 75 6d 29 Master+4, cksum)
a050: 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 )). || (0 != (
a060: 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
a070: 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ite(pPager->jfd,
a080: 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 aJournalMagic,
a090: 38 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 8, iHdrOff+4+nMa
a0a0: 73 74 65 72 2b 38 29 29 29 0a 20 20 29 7b 0a 20 ster+8))). ){.
a0b0: 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
a0c0: 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 }. pPager->jour
a0d0: 6e 61 6c 4f 66 66 20 2b 3d 20 28 6e 4d 61 73 74 nalOff += (nMast
a0e0: 65 72 2b 32 30 29 3b 0a 20 20 70 50 61 67 65 72 er+20);. pPager
a0f0: 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 21 70 50 ->needSync = !pP
a100: 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 0a 20 ager->noSync;..
a110: 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 /* If the pager
a120: 20 69 73 20 69 6e 20 70 65 72 69 73 74 65 6e 74 is in peristent
a130: 2d 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2c 20 74 -journal mode, t
a140: 68 65 6e 20 74 68 65 20 70 68 79 73 69 63 61 6c hen the physical
a150: 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 66 . ** journal-f
a160: 69 6c 65 20 6d 61 79 20 65 78 74 65 6e 64 20 70 ile may extend p
a170: 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 ast the end of t
a180: 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 he master-journa
a190: 6c 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 20 l name. ** and
a1a0: 38 20 62 79 74 65 73 20 6f 66 20 6d 61 67 69 63 8 bytes of magic
a1b0: 20 64 61 74 61 20 6a 75 73 74 20 77 72 69 74 74 data just writt
a1c0: 65 6e 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 en to the file.
a1d0: 54 68 69 73 20 69 73 20 0a 20 20 2a 2a 20 64 61 This is . ** da
a1e0: 6e 67 65 72 6f 75 73 20 62 65 63 61 75 73 65 20 ngerous because
a1f0: 74 68 65 20 63 6f 64 65 20 74 6f 20 72 6f 6c 6c the code to roll
a200: 62 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e back a hot-journ
a210: 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 77 69 6c al file. ** wil
a220: 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f l not be able to
a230: 20 66 69 6e 64 20 74 68 65 20 6d 61 73 74 65 72 find the master
a240: 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 74 6f -journal name to
a250: 20 64 65 74 65 72 6d 69 6e 65 20 0a 20 20 2a 2a determine . **
a260: 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
a270: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 68 the journal is h
a280: 6f 74 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 ot. . **. ** E
a290: 61 73 69 65 73 74 20 74 68 69 6e 67 20 74 6f 20 asiest thing to
a2a0: 64 6f 20 69 6e 20 74 68 69 73 20 73 63 65 6e 61 do in this scena
a2b0: 72 69 6f 20 69 73 20 74 6f 20 74 72 75 6e 63 61 rio is to trunca
a2c0: 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a te the journal .
a2d0: 20 20 2a 2a 20 66 69 6c 65 20 74 6f 20 74 68 65 ** file to the
a2e0: 20 72 65 71 75 69 72 65 64 20 73 69 7a 65 2e 0a required size..
a2f0: 20 20 2a 2f 20 0a 20 20 69 66 28 20 53 51 4c 49 */ . if( SQLI
a300: 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c TE_OK==(rc = sql
a310: 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 ite3OsFileSize(p
a320: 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 6a 72 6e Pager->jfd, &jrn
a330: 6c 53 69 7a 65 29 29 0a 20 20 20 26 26 20 6a 72 lSize)). && jr
a340: 6e 6c 53 69 7a 65 3e 70 50 61 67 65 72 2d 3e 6a nlSize>pPager->j
a350: 6f 75 72 6e 61 6c 4f 66 66 0a 20 20 29 7b 0a 20 ournalOff. ){.
a360: 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
a370: 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 sTruncate(pPager
a380: 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a ->jfd, pPager->j
a390: 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20 7d 0a ournalOff);. }.
a3a0: 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
a3b0: 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 20 70 61 67 /*.** Find a pag
a3c0: 65 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 e in the hash ta
a3d0: 62 6c 65 20 67 69 76 65 6e 20 69 74 73 20 70 61 ble given its pa
a3e0: 67 65 20 6e 75 6d 62 65 72 2e 20 52 65 74 75 72 ge number. Retur
a3f0: 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 n.** a pointer t
a400: 6f 20 74 68 65 20 70 61 67 65 20 6f 72 20 4e 55 o the page or NU
a410: 4c 4c 20 69 66 20 74 68 65 20 72 65 71 75 65 73 LL if the reques
a420: 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74 20 ted page is not
a430: 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e 20 6d .** already in m
a440: 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 emory..*/.static
a450: 20 50 67 48 64 72 20 2a 70 61 67 65 72 5f 6c 6f PgHdr *pager_lo
a460: 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 okup(Pager *pPag
a470: 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a er, Pgno pgno){.
a480: 20 20 50 67 48 64 72 20 2a 70 3b 20 20 20 20 20 PgHdr *p;
a490: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4a0: 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 /* Return va
a4b0: 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 74 20 lue */.. /* It
a4c0: 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
a4d0: 66 6f 72 20 61 20 63 61 6c 6c 20 74 6f 20 50 63 for a call to Pc
a4e0: 61 63 68 65 46 65 74 63 68 28 29 20 77 69 74 68 acheFetch() with
a4f0: 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 74 createFlag==0 t
a500: 6f 0a 20 20 2a 2a 20 66 61 69 6c 2c 20 73 69 6e o. ** fail, sin
a510: 63 65 20 6e 6f 20 61 74 74 65 6d 70 74 20 74 6f ce no attempt to
a520: 20 61 6c 6c 6f 63 61 74 65 20 64 79 6e 61 6d 69 allocate dynami
a530: 63 20 6d 65 6d 6f 72 79 20 77 69 6c 6c 20 62 65 c memory will be
a540: 20 6d 61 64 65 2e 0a 20 20 2a 2f 0a 20 20 28 76 made.. */. (v
a550: 6f 69 64 29 73 71 6c 69 74 65 33 50 63 61 63 68 oid)sqlite3Pcach
a560: 65 46 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 eFetch(pPager->p
a570: 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 30 2c PCache, pgno, 0,
a580: 20 26 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 &p);. return p
a590: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 65 73 ;.}../*.** Unles
a5a0: 73 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 s the pager is i
a5b0: 6e 20 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 64 n error-state, d
a5c0: 69 73 63 61 72 64 20 61 6c 6c 20 69 6e 2d 6d 65 iscard all in-me
a5d0: 6d 6f 72 79 20 70 61 67 65 73 2e 20 49 66 0a 2a mory pages. If.*
a5e0: 2a 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 * the pager is i
a5f0: 6e 20 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 74 n error-state, t
a600: 68 65 6e 20 74 68 69 73 20 63 61 6c 6c 20 69 73 hen this call is
a610: 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 a no-op..**.**
a620: 54 4f 44 4f 3a 20 57 68 79 20 63 61 6e 20 77 65 TODO: Why can we
a630: 20 6e 6f 74 20 72 65 73 65 74 20 74 68 65 20 70 not reset the p
a640: 61 67 65 72 20 77 68 69 6c 65 20 69 6e 20 65 72 ager while in er
a650: 72 6f 72 20 73 74 61 74 65 3f 0a 2a 2f 0a 73 74 ror state?.*/.st
a660: 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f atic void pager_
a670: 72 65 73 65 74 28 50 61 67 65 72 20 2a 70 50 61 reset(Pager *pPa
a680: 67 65 72 29 7b 0a 20 20 69 66 28 20 53 51 4c 49 ger){. if( SQLI
a690: 54 45 5f 4f 4b 3d 3d 70 50 61 67 65 72 2d 3e 65 TE_OK==pPager->e
a6a0: 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 73 71 rrCode ){. sq
a6b0: 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 lite3BackupResta
a6c0: 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b rt(pPager->pBack
a6d0: 75 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 up);. sqlite3
a6e0: 50 63 61 63 68 65 43 6c 65 61 72 28 70 50 61 67 PcacheClear(pPag
a6f0: 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 er->pPCache);.
a700: 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
a710: 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 7d Valid = 0;. }.}
a720: 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c ../*.** Free all
a730: 20 73 74 72 75 63 74 75 72 65 73 20 69 6e 20 74 structures in t
a740: 68 65 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f he Pager.aSavepo
a750: 69 6e 74 5b 5d 20 61 72 72 61 79 20 61 6e 64 20 int[] array and
a760: 73 65 74 20 62 6f 74 68 0a 2a 2a 20 50 61 67 65 set both.** Page
a770: 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 61 6e 64 r.aSavepoint and
a780: 20 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e Pager.nSavepoin
a790: 74 20 74 6f 20 7a 65 72 6f 2e 20 43 6c 6f 73 65 t to zero. Close
a7a0: 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
a7b0: 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 6f 70 65 .** if it is ope
a7c0: 6e 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 20 n and the pager
a7d0: 69 73 20 6e 6f 74 20 69 6e 20 65 78 63 6c 75 73 is not in exclus
a7e0: 69 76 65 20 6d 6f 64 65 2e 0a 2a 2f 0a 73 74 61 ive mode..*/.sta
a7f0: 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 tic void release
a800: 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 50 61 AllSavepoints(Pa
a810: 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
a820: 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 int ii;
a830: 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f /* Iterato
a840: 72 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 r for looping th
a850: 72 6f 75 67 68 20 50 61 67 65 72 2e 61 53 61 76 rough Pager.aSav
a860: 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 66 6f 72 28 epoint */. for(
a870: 69 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d ii=0; ii<pPager-
a880: 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b >nSavepoint; ii+
a890: 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 +){. sqlite3B
a8a0: 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 itvecDestroy(pPa
a8b0: 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b ger->aSavepoint[
a8c0: 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 ii].pInSavepoint
a8d0: 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50 );. }. if( !pP
a8e0: 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d ager->exclusiveM
a8f0: 6f 64 65 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 ode || sqlite3Is
a900: 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 MemJournal(pPage
a910: 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 r->sjfd) ){.
a920: 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 sqlite3OsClose(p
a930: 50 61 67 65 72 2d 3e 73 6a 66 64 29 3b 0a 20 20 Pager->sjfd);.
a940: 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 }. sqlite3_free
a950: 28 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f (pPager->aSavepo
a960: 69 6e 74 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e int);. pPager->
a970: 61 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a aSavepoint = 0;.
a980: 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 pPager->nSavep
a990: 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 oint = 0;. pPag
a9a0: 65 72 2d 3e 6e 53 75 62 52 65 63 20 3d 20 30 3b er->nSubRec = 0;
a9b0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
a9c0: 65 20 62 69 74 20 6e 75 6d 62 65 72 20 70 67 6e e bit number pgn
a9d0: 6f 20 69 6e 20 74 68 65 20 50 61 67 65 72 53 61 o in the PagerSa
a9e0: 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 vepoint.pInSavep
a9f0: 6f 69 6e 74 20 0a 2a 2a 20 62 69 74 76 65 63 73 oint .** bitvecs
aa00: 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76 of all open sav
aa10: 65 70 6f 69 6e 74 73 2e 20 52 65 74 75 72 6e 20 epoints. Return
aa20: 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 SQLITE_OK if suc
aa30: 63 65 73 73 66 75 6c 0a 2a 2a 20 6f 72 20 53 51 cessful.** or SQ
aa40: 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 LITE_NOMEM if a
aa50: 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f malloc failure o
aa60: 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 ccurs..*/.static
aa70: 20 69 6e 74 20 61 64 64 54 6f 53 61 76 65 70 6f int addToSavepo
aa80: 69 6e 74 42 69 74 76 65 63 73 28 50 61 67 65 72 intBitvecs(Pager
aa90: 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 *pPager, Pgno p
aaa0: 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20 gno){. int ii;
aab0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aac0: 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
aad0: 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 r */. int rc =
aae0: 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
aaf0: 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 /* Result code
ab00: 2a 2f 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 */.. for(ii=0;
ab10: 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 ii<pPager->nSave
ab20: 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 point; ii++){.
ab30: 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 PagerSavepoint
ab40: 20 2a 70 20 3d 20 26 70 50 61 67 65 72 2d 3e 61 *p = &pPager->a
ab50: 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 3b 0a 20 Savepoint[ii];.
ab60: 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 2d 3e if( pgno<=p->
ab70: 6e 4f 72 69 67 20 29 7b 0a 20 20 20 20 20 20 72 nOrig ){. r
ab80: 63 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 c |= sqlite3Bitv
ab90: 65 63 53 65 74 28 70 2d 3e 70 49 6e 53 61 76 65 ecSet(p->pInSave
aba0: 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 3b 0a 20 20 point, pgno);.
abb0: 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 testcase( rc
abc0: 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 ==SQLITE_NOMEM )
abd0: 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
abe0: 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
abf0: 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc==SQLITE_NOME
ac00: 4d 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 M );. }. }.
ac10: 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
ac20: 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 74 68 65 20 *.** Unlock the
ac30: 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 database file. T
ac40: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
ac50: 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 70 a no-op if the p
ac60: 61 67 65 72 0a 2a 2a 20 69 73 20 69 6e 20 65 78 ager.** is in ex
ac70: 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e 0a 2a 2a clusive mode..**
ac80: 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 .** If the pager
ac90: 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e is currently in
aca0: 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64 69 error state, di
acb0: 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e scard the conten
acc0: 74 73 20 6f 66 20 0a 2a 2a 20 74 68 65 20 63 61 ts of .** the ca
acd0: 63 68 65 20 61 6e 64 20 72 65 73 65 74 20 74 68 che and reset th
ace0: 65 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 e Pager structur
acf0: 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 e internal state
ad00: 2e 20 49 66 20 74 68 65 72 65 20 69 73 0a 2a 2a . If there is.**
ad10: 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c an open journal
ad20: 2d 66 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 -file, then the
ad30: 6e 65 78 74 20 74 69 6d 65 20 61 20 73 68 61 72 next time a shar
ad40: 65 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 ed-lock is obtai
ad50: 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 70 61 ned.** on the pa
ad60: 67 65 72 20 66 69 6c 65 20 28 62 79 20 74 68 69 ger file (by thi
ad70: 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 s or any other p
ad80: 72 6f 63 65 73 73 29 2c 20 69 74 20 77 69 6c 6c rocess), it will
ad90: 20 62 65 0a 2a 2a 20 74 72 65 61 74 65 64 20 61 be.** treated a
ada0: 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 s a hot-journal
adb0: 61 6e 64 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e and rolled back.
adc0: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
add0: 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 50 61 67 pager_unlock(Pag
ade0: 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 er *pPager){. i
adf0: 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c f( !pPager->excl
ae00: 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 usiveMode ){.
ae10: 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
ae20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
ae30: 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
ae40: 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 63 . /* Always c
ae50: 6c 6f 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c lose the journal
ae60: 20 66 69 6c 65 20 77 68 65 6e 20 64 72 6f 70 70 file when dropp
ae70: 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 ing the database
ae80: 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2a 20 4f 74 lock.. ** Ot
ae90: 68 65 72 77 69 73 65 2c 20 61 6e 6f 74 68 65 72 herwise, another
aea0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 connection with
aeb0: 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 64 65 journal_mode=de
aec0: 6c 65 74 65 20 6d 69 67 68 74 0a 20 20 20 20 2a lete might. *
aed0: 2a 20 64 65 6c 65 74 65 20 74 68 65 20 66 69 6c * delete the fil
aee0: 65 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 e out from under
aef0: 20 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 us.. */.
af00: 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 sqlite3OsClose(p
af10: 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 Pager->jfd);.
af20: 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 sqlite3BitvecDe
af30: 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 stroy(pPager->pI
af40: 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 70 nJournal);. p
af50: 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
af60: 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 6c 65 61 l = 0;. relea
af70: 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 seAllSavepoints(
af80: 70 50 61 67 65 72 29 3b 0a 0a 20 20 20 20 2f 2a pPager);.. /*
af90: 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 If the file is
afa0: 75 6e 6c 6f 63 6b 65 64 2c 20 73 6f 6d 65 62 6f unlocked, somebo
afb0: 64 79 20 65 6c 73 65 20 6d 69 67 68 74 20 63 68 dy else might ch
afc0: 61 6e 67 65 20 69 74 2e 20 54 68 65 0a 20 20 20 ange it. The.
afd0: 20 2a 2a 20 76 61 6c 75 65 73 20 73 74 6f 72 65 ** values store
afe0: 64 20 69 6e 20 50 61 67 65 72 2e 64 62 53 69 7a d in Pager.dbSiz
aff0: 65 20 65 74 63 2e 20 6d 69 67 68 74 20 62 65 63 e etc. might bec
b000: 6f 6d 65 20 69 6e 76 61 6c 69 64 20 69 66 0a 20 ome invalid if.
b010: 20 20 20 2a 2a 20 74 68 69 73 20 68 61 70 70 65 ** this happe
b020: 6e 73 2e 20 54 4f 44 4f 3a 20 52 65 61 6c 6c 79 ns. TODO: Really
b030: 2c 20 74 68 69 73 20 64 6f 65 73 6e 27 74 20 6e , this doesn't n
b040: 65 65 64 20 74 6f 20 62 65 20 63 6c 65 61 72 65 eed to be cleare
b050: 64 0a 20 20 20 20 2a 2a 20 75 6e 74 69 6c 20 74 d. ** until t
b060: 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 he change-counte
b070: 72 20 63 68 65 63 6b 20 66 61 69 6c 73 20 69 6e r check fails in
b080: 20 50 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b PagerSharedLock
b090: 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 ().. */. p
b0a0: 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c Pager->dbSizeVal
b0b0: 69 64 20 3d 20 30 3b 0a 0a 20 20 20 20 72 63 20 id = 0;.. rc
b0c0: 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 = osUnlock(pPage
b0d0: 72 2d 3e 66 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b r->fd, NO_LOCK);
b0e0: 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 . if( rc ){.
b0f0: 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 pPager->err
b100: 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 20 20 7d Code = rc;. }
b110: 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 55 . IOTRACE(("U
b120: 4e 4c 4f 43 4b 20 25 70 5c 6e 22 2c 20 70 50 61 NLOCK %p\n", pPa
b130: 67 65 72 29 29 0a 0a 20 20 20 20 2f 2a 20 49 66 ger)).. /* If
b140: 20 50 61 67 65 72 2e 65 72 72 43 6f 64 65 20 69 Pager.errCode i
b150: 73 20 73 65 74 2c 20 74 68 65 20 63 6f 6e 74 65 s set, the conte
b160: 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 nts of the pager
b170: 20 63 61 63 68 65 20 63 61 6e 6e 6f 74 20 62 65 cache cannot be
b180: 0a 20 20 20 20 2a 2a 20 74 72 75 73 74 65 64 2e . ** trusted.
b190: 20 4e 6f 77 20 74 68 61 74 20 74 68 65 20 70 61 Now that the pa
b1a0: 67 65 72 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f ger file is unlo
b1b0: 63 6b 65 64 2c 20 74 68 65 20 63 6f 6e 74 65 6e cked, the conten
b1c0: 74 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a ts of the. **
b1d0: 20 63 61 63 68 65 20 63 61 6e 20 62 65 20 64 69 cache can be di
b1e0: 73 63 61 72 64 65 64 20 61 6e 64 20 74 68 65 20 scarded and the
b1f0: 65 72 72 6f 72 20 63 6f 64 65 20 73 61 66 65 6c error code safel
b200: 79 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 2a y cleared.. *
b210: 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 /. if( pPager
b220: 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 ->errCode ){.
b230: 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
b240: 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
b250: 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
b260: 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
b270: 20 20 20 7d 0a 20 20 20 20 20 20 70 61 67 65 72 }. pager
b280: 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a _reset(pPager);.
b290: 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 }.. pPage
b2a0: 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f r->changeCountDo
b2b0: 6e 65 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 ne = 0;. pPag
b2c0: 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 er->state = PAGE
b2d0: 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 7d 0a 7d 0a R_UNLOCK;. }.}.
b2e0: 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
b2f0: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 63 tion should be c
b300: 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 49 4f alled when an IO
b310: 45 52 52 2c 20 43 4f 52 52 55 50 54 20 6f 72 20 ERR, CORRUPT or
b320: 46 55 4c 4c 20 65 72 72 6f 72 0a 2a 2a 20 6d 61 FULL error.** ma
b330: 79 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e y have occurred.
b340: 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
b350: 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ent is a pointer
b360: 20 74 6f 20 74 68 65 20 70 61 67 65 72 20 0a 2a to the pager .*
b370: 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 * structure, the
b380: 20 73 65 63 6f 6e 64 20 74 68 65 20 65 72 72 6f second the erro
b390: 72 2d 63 6f 64 65 20 61 62 6f 75 74 20 74 6f 20 r-code about to
b3a0: 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 be returned by a
b3b0: 20 70 61 67 65 72 20 0a 2a 2a 20 41 50 49 20 66 pager .** API f
b3c0: 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c unction. The val
b3d0: 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 ue returned is a
b3e0: 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 65 63 copy of the sec
b3f0: 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a ond argument .**
b400: 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f to this functio
b410: 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 n. .**.** If the
b420: 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
b430: 20 69 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 is SQLITE_IOERR
b440: 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 , SQLITE_CORRUPT
b450: 2c 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c , or SQLITE_FULL
b460: 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 62 65 .** the error be
b470: 63 6f 6d 65 73 20 70 65 72 73 69 73 74 65 6e 74 comes persistent
b480: 2e 20 55 6e 74 69 6c 20 74 68 65 20 70 65 72 73 . Until the pers
b490: 69 73 74 65 6e 20 65 72 72 6f 72 20 69 73 20 63 isten error is c
b4a0: 6c 65 61 72 65 64 2c 0a 2a 2a 20 73 75 62 73 65 leared,.** subse
b4b0: 71 75 65 6e 74 20 41 50 49 20 63 61 6c 6c 73 20 quent API calls
b4c0: 6f 6e 20 74 68 69 73 20 50 61 67 65 72 20 77 69 on this Pager wi
b4d0: 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 72 ll immediately r
b4e0: 65 74 75 72 6e 20 74 68 65 20 73 61 6d 65 20 0a eturn the same .
b4f0: 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a ** error code..*
b500: 2a 0a 2a 2a 20 41 20 70 65 72 73 69 73 74 65 6e *.** A persisten
b510: 74 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 65 t error indicate
b520: 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 s that the conte
b530: 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 nts of the pager
b540: 2d 63 61 63 68 65 20 0a 2a 2a 20 63 61 6e 6e 6f -cache .** canno
b550: 74 20 62 65 20 74 72 75 73 74 65 64 2e 20 54 68 t be trusted. Th
b560: 69 73 20 73 74 61 74 65 20 63 61 6e 20 62 65 20 is state can be
b570: 63 6c 65 61 72 65 64 20 62 79 20 63 6f 6d 70 6c cleared by compl
b580: 65 74 65 6c 79 20 64 69 73 63 61 72 64 69 6e 67 etely discarding
b590: 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 .** the content
b5a0: 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 s of the pager-c
b5b0: 61 63 68 65 2e 20 49 66 20 61 20 74 72 61 6e 73 ache. If a trans
b5c0: 61 63 74 69 6f 6e 20 77 61 73 20 61 63 74 69 76 action was activ
b5d0: 65 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 65 e when.** the pe
b5e0: 72 73 69 73 74 65 6e 74 20 65 72 72 6f 72 20 6f rsistent error o
b5f0: 63 63 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68 ccurred, then th
b600: 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
b610: 61 6c 20 6d 61 79 20 6e 65 65 64 0a 2a 2a 20 74 al may need.** t
b620: 6f 20 62 65 20 72 65 70 6c 61 79 65 64 20 74 6f o be replayed to
b630: 20 72 65 73 74 6f 72 65 20 74 68 65 20 63 6f 6e restore the con
b640: 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74 tents of the dat
b650: 61 62 61 73 65 20 66 69 6c 65 20 28 61 73 20 69 abase file (as i
b660: 66 0a 2a 2a 20 69 74 20 77 65 72 65 20 61 20 68 f.** it were a h
b670: 6f 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2f 0a ot-journal)..*/.
b680: 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
b690: 5f 65 72 72 6f 72 28 50 61 67 65 72 20 2a 70 50 _error(Pager *pP
b6a0: 61 67 65 72 2c 20 69 6e 74 20 72 63 29 7b 0a 20 ager, int rc){.
b6b0: 20 69 6e 74 20 72 63 32 20 3d 20 72 63 20 26 20 int rc2 = rc &
b6c0: 30 78 66 66 3b 0a 20 20 61 73 73 65 72 74 28 20 0xff;. assert(
b6d0: 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
b6e0: 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73 73 !MEMDB );. ass
b6f0: 65 72 74 28 0a 20 20 20 20 20 20 20 70 50 61 67 ert(. pPag
b700: 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c er->errCode==SQL
b710: 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20 20 ITE_FULL ||.
b720: 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f pPager->errCo
b730: 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c de==SQLITE_OK ||
b740: 0a 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d . (pPager-
b750: 3e 65 72 72 43 6f 64 65 20 26 20 30 78 66 66 29 >errCode & 0xff)
b760: 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 ==SQLITE_IOERR.
b770: 20 29 3b 0a 20 20 69 66 28 20 72 63 32 3d 3d 53 );. if( rc2==S
b780: 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 20 72 63 QLITE_FULL || rc
b790: 32 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 2==SQLITE_IOERR
b7a0: 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 ){. pPager->e
b7b0: 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 7d rrCode = rc;. }
b7c0: 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
b7d0: 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 ./*.** Execute a
b7e0: 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 61 20 74 rollback if a t
b7f0: 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 ransaction is ac
b800: 74 69 76 65 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 tive and unlock
b810: 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 the .** database
b820: 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 file. .**.** If
b830: 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 61 the pager has a
b840: 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64 20 74 lready entered t
b850: 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 he error state,
b860: 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 0a do not attempt .
b870: 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 ** the rollback
b880: 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 49 6e at this time. In
b890: 73 74 65 61 64 2c 20 70 61 67 65 72 5f 75 6e 6c stead, pager_unl
b8a0: 6f 63 6b 28 29 20 69 73 20 63 61 6c 6c 65 64 2e ock() is called.
b8b0: 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 The.** call to
b8c0: 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 77 pager_unlock() w
b8d0: 69 6c 6c 20 64 69 73 63 61 72 64 20 61 6c 6c 20 ill discard all
b8e0: 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 2c in-memory pages,
b8f0: 20 75 6e 6c 6f 63 6b 0a 2a 2a 20 74 68 65 20 64 unlock.** the d
b900: 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
b910: 20 63 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 clear the error
b920: 20 73 74 61 74 65 2e 20 49 66 20 74 68 69 73 20 state. If this
b930: 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 means that.** th
b940: 65 72 65 20 69 73 20 61 20 68 6f 74 2d 6a 6f 75 ere is a hot-jou
b950: 72 6e 61 6c 20 6c 65 66 74 20 69 6e 20 74 68 65 rnal left in the
b960: 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 file-system, th
b970: 65 20 6e 65 78 74 20 63 6f 6e 6e 65 63 74 69 6f e next connectio
b980: 6e 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61 n.** to obtain a
b990: 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 shared lock on
b9a0: 74 68 65 20 70 61 67 65 72 20 28 77 68 69 63 68 the pager (which
b9b0: 20 6d 61 79 20 62 65 20 74 68 69 73 20 6f 6e 65 may be this one
b9c0: 29 20 77 69 6c 6c 0a 2a 2a 20 72 6f 6c 6c 20 69 ) will.** roll i
b9d0: 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 t back..**.** If
b9e0: 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 6e the pager has n
b9f0: 6f 74 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 ot already enter
ba00: 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 ed the error sta
ba10: 74 65 2c 20 62 75 74 20 61 6e 20 49 4f 20 6f 72 te, but an IO or
ba20: 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 .** malloc error
ba30: 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 occurs during a
ba40: 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e 20 rollback, then
ba50: 74 68 69 73 20 77 69 6c 6c 20 69 74 73 65 6c 66 this will itself
ba60: 20 63 61 75 73 65 20 0a 2a 2a 20 74 68 65 20 70 cause .** the p
ba70: 61 67 65 72 20 74 6f 20 65 6e 74 65 72 20 74 68 ager to enter th
ba80: 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 57 e error state. W
ba90: 68 69 63 68 20 77 69 6c 6c 20 62 65 20 63 6c 65 hich will be cle
baa0: 61 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 ared by the.** c
bab0: 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c all to pager_unl
bac0: 6f 63 6b 28 29 2c 20 61 73 20 64 65 73 63 72 69 ock(), as descri
bad0: 62 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 bed above..*/.st
bae0: 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 55 atic void pagerU
baf0: 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b nlockAndRollback
bb00: 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
bb10: 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 . if( pPager->e
bb20: 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f rrCode==SQLITE_O
bb30: 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 73 74 61 K && pPager->sta
bb40: 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 te>=PAGER_RESERV
bb50: 45 44 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ED ){. sqlite
bb60: 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 3BeginBenignMall
bb70: 6f 63 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 oc();. sqlite
bb80: 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 3PagerRollback(p
bb90: 50 61 67 65 72 29 3b 0a 20 20 20 20 73 71 6c 69 Pager);. sqli
bba0: 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
bbb0: 6f 63 28 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65 oc();. }. page
bbc0: 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 r_unlock(pPager)
bbd0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
bbe0: 72 6f 75 74 69 6e 65 20 65 6e 64 73 20 61 20 74 routine ends a t
bbf0: 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20 74 72 ransaction. A tr
bc00: 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 75 73 75 ansaction is usu
bc10: 61 6c 6c 79 20 65 6e 64 65 64 20 62 79 20 0a 2a ally ended by .*
bc20: 2a 20 65 69 74 68 65 72 20 61 20 43 4f 4d 4d 49 * either a COMMI
bc30: 54 20 6f 72 20 61 20 52 4f 4c 4c 42 41 43 4b 20 T or a ROLLBACK
bc40: 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 20 operation. This
bc50: 72 6f 75 74 69 6e 65 20 6d 61 79 20 62 65 20 63 routine may be c
bc60: 61 6c 6c 65 64 20 0a 2a 2a 20 61 66 74 65 72 20 alled .** after
bc70: 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20 68 6f rollback of a ho
bc80: 74 2d 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 69 66 t-journal, or if
bc90: 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
bca0: 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67 0a 2a while opening.*
bcb0: 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 * the journal fi
bcc0: 6c 65 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68 le or writing th
bcd0: 65 20 76 65 72 79 20 66 69 72 73 74 20 6a 6f 75 e very first jou
bce0: 72 6e 61 6c 2d 68 65 61 64 65 72 20 6f 66 20 61 rnal-header of a
bcf0: 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 72 61 .** database tra
bd00: 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a 2a 2a nsaction..** .**
bd10: 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 If the pager is
bd20: 20 69 6e 20 50 41 47 45 52 5f 53 48 41 52 45 44 in PAGER_SHARED
bd30: 20 6f 72 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b or PAGER_UNLOCK
bd40: 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 state when this
bd50: 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63 .** routine is c
bd60: 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e alled, it is a n
bd70: 6f 2d 6f 70 20 28 72 65 74 75 72 6e 73 20 53 51 o-op (returns SQ
bd80: 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 LITE_OK)..**.**
bd90: 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 79 20 61 Otherwise, any a
bda0: 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 ctive savepoints
bdb0: 20 61 72 65 20 72 65 6c 65 61 73 65 64 2e 0a 2a are released..*
bdc0: 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 *.** If the jour
bdd0: 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e nal file is open
bde0: 2c 20 74 68 65 6e 20 69 74 20 69 73 20 22 66 69 , then it is "fi
bdf0: 6e 61 6c 69 7a 65 64 22 2e 20 4f 6e 63 65 20 61 nalized". Once a
be00: 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 66 69 6c journal .** fil
be10: 65 20 68 61 73 20 62 65 65 6e 20 66 69 6e 61 6c e has been final
be20: 69 7a 65 64 20 69 74 20 69 73 20 6e 6f 74 20 70 ized it is not p
be30: 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 69 ossible to use i
be40: 74 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 61 t to roll back a
be50: 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e .** transaction
be60: 2e 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 62 65 . Nor will it be
be70: 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 considered to b
be80: 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 e a hot-journal
be90: 62 79 20 74 68 69 73 0a 2a 2a 20 6f 72 20 61 6e by this.** or an
bea0: 79 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 y other database
beb0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 45 78 61 connection. Exa
bec0: 63 74 6c 79 20 68 6f 77 20 61 20 6a 6f 75 72 6e ctly how a journ
bed0: 61 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 0a al is finalized.
bee0: 2a 2a 20 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 ** depends on wh
bef0: 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 ether or not the
bf00: 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e pager is runnin
bf10: 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d g in exclusive m
bf20: 6f 64 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 63 ode and.** the c
bf30: 75 72 72 65 6e 74 20 6a 6f 75 72 6e 61 6c 2d 6d urrent journal-m
bf40: 6f 64 65 20 28 50 61 67 65 72 2e 6a 6f 75 72 6e ode (Pager.journ
bf50: 61 6c 4d 6f 64 65 20 76 61 6c 75 65 29 2c 20 61 alMode value), a
bf60: 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
bf70: 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d journalMode==
bf80: 4d 45 4d 4f 52 59 0a 2a 2a 20 20 20 20 20 4a 6f MEMORY.** Jo
bf90: 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 urnal file descr
bfa0: 69 70 74 6f 72 20 69 73 20 73 69 6d 70 6c 79 20 iptor is simply
bfb0: 63 6c 6f 73 65 64 2e 20 54 68 69 73 20 64 65 73 closed. This des
bfc0: 74 72 6f 79 73 20 61 6e 20 0a 2a 2a 20 20 20 20 troys an .**
bfd0: 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e in-memory journ
bfe0: 61 6c 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 al..**.** jour
bff0: 6e 61 6c 4d 6f 64 65 3d 3d 54 52 55 4e 43 41 54 nalMode==TRUNCAT
c000: 45 0a 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c E.** Journal
c010: 20 66 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74 file is truncat
c020: 65 64 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 ed to zero bytes
c030: 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 in size..**.**
c040: 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 journalMode==P
c050: 45 52 53 49 53 54 0a 2a 2a 20 20 20 20 20 54 68 ERSIST.** Th
c060: 65 20 66 69 72 73 74 20 32 38 20 62 79 74 65 73 e first 28 bytes
c070: 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
c080: 66 69 6c 65 20 61 72 65 20 7a 65 72 6f 65 64 2e file are zeroed.
c090: 20 54 68 69 73 20 69 6e 76 61 6c 69 64 61 74 65 This invalidate
c0a0: 73 0a 2a 2a 20 20 20 20 20 74 68 65 20 66 69 72 s.** the fir
c0b0: 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 st journal heade
c0c0: 72 20 69 6e 20 74 68 65 20 66 69 6c 65 2c 20 61 r in the file, a
c0d0: 6e 64 20 68 65 6e 63 65 20 74 68 65 20 65 6e 74 nd hence the ent
c0e0: 69 72 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 ire journal.**
c0f0: 20 20 20 66 69 6c 65 2e 20 41 6e 20 69 6e 76 61 file. An inva
c100: 6c 69 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 lid journal file
c110: 20 63 61 6e 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 cannot be rolle
c120: 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 d back..**.**
c130: 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 44 45 4c journalMode==DEL
c140: 45 54 45 0a 2a 2a 20 20 20 20 20 54 68 65 20 6a ETE.** The j
c150: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 ournal file is c
c160: 6c 6f 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65 losed and delete
c170: 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f d using sqlite3O
c180: 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a sDelete()..**.**
c190: 20 20 20 20 20 49 66 20 74 68 65 20 70 61 67 65 If the page
c1a0: 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 r is running in
c1b0: 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 exclusive mode,
c1c0: 74 68 69 73 20 6d 65 74 68 6f 64 20 6f 66 20 66 this method of f
c1d0: 69 6e 61 6c 69 7a 69 6e 67 0a 2a 2a 20 20 20 20 inalizing.**
c1e0: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
c1f0: 65 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e e is never used.
c200: 20 49 6e 73 74 65 61 64 2c 20 69 66 20 74 68 65 Instead, if the
c210: 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 69 73 0a journalMode is.
c220: 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 61 6e ** DELETE an
c230: 64 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 d the pager is i
c240: 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n exclusive mode
c250: 2c 20 74 68 65 20 6d 65 74 68 6f 64 20 64 65 73 , the method des
c260: 63 72 69 62 65 64 20 75 6e 64 65 72 0a 2a 2a 20 cribed under.**
c270: 20 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d journalMode=
c280: 3d 50 45 52 53 49 53 54 20 69 73 20 75 73 65 64 =PERSIST is used
c290: 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 instead..**.**
c2a0: 41 66 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 After the journa
c2b0: 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 2c 20 l is finalized,
c2c0: 69 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 6e 6f if running in no
c2d0: 6e 2d 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n-exclusive mode
c2e0: 2c 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 6d , the.** pager m
c2f0: 6f 76 65 73 20 74 6f 20 50 41 47 45 52 5f 53 48 oves to PAGER_SH
c300: 41 52 45 44 20 73 74 61 74 65 20 28 61 6e 64 20 ARED state (and
c310: 64 6f 77 6e 67 72 61 64 65 73 20 74 68 65 20 6c downgrades the l
c320: 6f 63 6b 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61 ock on the.** da
c330: 74 61 62 61 73 65 20 66 69 6c 65 20 61 63 63 6f tabase file acco
c340: 72 64 69 6e 67 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 rdingly)..**.**
c350: 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 If the pager is
c360: 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 running in exclu
c370: 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 73 sive mode and is
c380: 20 69 6e 20 50 41 47 45 52 5f 53 59 4e 43 45 44 in PAGER_SYNCED
c390: 20 73 74 61 74 65 2c 0a 2a 2a 20 69 74 20 6d 6f state,.** it mo
c3a0: 76 65 73 20 74 6f 20 50 41 47 45 52 5f 45 58 43 ves to PAGER_EXC
c3b0: 4c 55 53 49 56 45 2e 20 4e 6f 20 6c 6f 63 6b 73 LUSIVE. No locks
c3c0: 20 61 72 65 20 64 6f 77 6e 67 72 61 64 65 64 20 are downgraded
c3d0: 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 0a when running in.
c3e0: 2a 2a 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 ** exclusive mod
c3f0: 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f e..**.** SQLITE_
c400: 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 OK is returned i
c410: 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 f no error occur
c420: 73 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f s. If an error o
c430: 63 63 75 72 73 20 64 75 72 69 6e 67 0a 2a 2a 20 ccurs during.**
c440: 61 6e 79 20 6f 66 20 74 68 65 20 49 4f 20 6f 70 any of the IO op
c450: 65 72 61 74 69 6f 6e 73 20 74 6f 20 66 69 6e 61 erations to fina
c460: 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c lize the journal
c470: 20 66 69 6c 65 20 6f 72 20 75 6e 6c 6f 63 6b 20 file or unlock
c480: 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
c490: 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f then the IO erro
c4a0: 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
c4b0: 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 ed to the user.
c4c0: 49 66 20 74 68 65 20 0a 2a 2a 20 6f 70 65 72 61 If the .** opera
c4d0: 74 69 6f 6e 20 74 6f 20 66 69 6e 61 6c 69 7a 65 tion to finalize
c4e0: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
c4f0: 65 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 68 e fails, then th
c500: 65 20 63 6f 64 65 20 73 74 69 6c 6c 0a 2a 2a 20 e code still.**
c510: 74 72 69 65 73 20 74 6f 20 75 6e 6c 6f 63 6b 20 tries to unlock
c520: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
c530: 65 20 69 66 20 6e 6f 74 20 69 6e 20 65 78 63 6c e if not in excl
c540: 75 73 69 76 65 20 6d 6f 64 65 2e 20 49 66 20 74 usive mode. If t
c550: 68 65 0a 2a 2a 20 75 6e 6c 6f 63 6b 20 6f 70 65 he.** unlock ope
c560: 72 61 74 69 6f 6e 20 66 61 69 6c 73 20 61 73 20 ration fails as
c570: 77 65 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 66 well, then the f
c580: 69 72 73 74 20 65 72 72 6f 72 20 63 6f 64 65 20 irst error code
c590: 72 65 6c 61 74 65 64 0a 2a 2a 20 74 6f 20 74 68 related.** to th
c5a0: 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 65 6e e first error en
c5b0: 63 6f 75 6e 74 65 72 65 64 20 28 74 68 65 20 6a countered (the j
c5c0: 6f 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74 ournal finalizat
c5d0: 69 6f 6e 20 6f 6e 65 29 20 69 73 0a 2a 2a 20 72 ion one) is.** r
c5e0: 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 eturned..*/.stat
c5f0: 69 63 20 69 6e 74 20 70 61 67 65 72 5f 65 6e 64 ic int pager_end
c600: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 67 _transaction(Pag
c610: 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
c620: 68 61 73 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e hasMaster){. in
c630: 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
c640: 3b 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 ; /* Error
c650: 63 6f 64 65 20 66 72 6f 6d 20 6a 6f 75 72 6e 61 code from journa
c660: 6c 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f l finalization o
c670: 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e peration */. in
c680: 74 20 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f t rc2 = SQLITE_O
c690: 4b 3b 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 K; /* Error
c6a0: 63 6f 64 65 20 66 72 6f 6d 20 64 62 20 66 69 6c code from db fil
c6b0: 65 20 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 69 e unlock operati
c6c0: 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 50 61 on */.. if( pPa
c6d0: 67 65 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 52 ger->state<PAGER
c6e0: 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 _RESERVED ){.
c6f0: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
c700: 4b 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 K;. }. release
c710: 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50 AllSavepoints(pP
c720: 61 67 65 72 29 3b 0a 0a 20 20 61 73 73 65 72 74 ager);.. assert
c730: 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
c740: 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d >jfd) || pPager-
c750: 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 >pInJournal==0 )
c760: 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 ;. if( isOpen(p
c770: 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 0a Pager->jfd) ){..
c780: 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 54 68 65 /* TODO: The
c790: 72 65 27 73 20 61 20 70 72 6f 62 6c 65 6d 20 68 re's a problem h
c7a0: 65 72 65 20 69 66 20 61 20 6a 6f 75 72 6e 61 6c ere if a journal
c7b0: 2d 66 69 6c 65 20 77 61 73 20 6f 70 65 6e 65 64 -file was opened
c7c0: 20 69 6e 20 4d 45 4d 4f 52 59 0a 20 20 20 20 2a in MEMORY. *
c7d0: 2a 20 6d 6f 64 65 20 61 6e 64 20 74 68 65 6e 20 * mode and then
c7e0: 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 the journal-mode
c7f0: 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 54 is changed to T
c800: 52 55 4e 43 41 54 45 20 6f 72 20 50 45 52 53 49 RUNCATE or PERSI
c810: 53 54 0a 20 20 20 20 2a 2a 20 64 75 72 69 6e 67 ST. ** during
c820: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
c830: 2e 20 54 68 69 73 20 63 6f 64 65 20 73 68 6f 75 . This code shou
c840: 6c 64 20 62 65 20 63 68 61 6e 67 65 64 20 74 6f ld be changed to
c850: 20 61 73 73 75 6d 65 0a 20 20 20 20 2a 2a 20 74 assume. ** t
c860: 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hat the journal
c870: 6d 6f 64 65 20 68 61 73 20 6e 6f 74 20 63 68 61 mode has not cha
c880: 6e 67 65 64 20 73 69 6e 63 65 20 74 68 65 20 74 nged since the t
c890: 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 0a 20 ransaction was.
c8a0: 20 20 20 2a 2a 20 73 74 61 72 74 65 64 2e 20 41 ** started. A
c8b0: 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 nd the sqlite3Pa
c8c0: 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 29 gerJournalMode()
c8d0: 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 function should
c8e0: 20 62 65 0a 20 20 20 20 2a 2a 20 63 68 61 6e 67 be. ** chang
c8f0: 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 ed to make sure
c900: 74 68 61 74 20 74 68 69 73 20 69 73 20 74 68 65 that this is the
c910: 20 63 61 73 65 20 74 6f 6f 2e 0a 20 20 20 20 2a case too.. *
c920: 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 61 6c 69 /.. /* Finali
c930: 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ze the journal f
c940: 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 ile. */. if(
c950: 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
c960: 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
c970: 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b ALMODE_MEMORY ){
c980: 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 . assert( s
c990: 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e qlite3IsMemJourn
c9a0: 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 al(pPager->jfd)
c9b0: 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
c9c0: 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e OsClose(pPager->
c9d0: 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 jfd);. }else
c9e0: 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 if( pPager->jour
c9f0: 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
ca00: 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 OURNALMODE_TRUNC
ca10: 41 54 45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ATE ){. if(
ca20: 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
ca30: 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Off==0 ){.
ca40: 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
ca50: 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
ca60: 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
ca70: 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 te3OsTruncate(pP
ca80: 61 67 65 72 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 ager->jfd, 0);.
ca90: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 }. pPa
caa0: 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
cab0: 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 = 0;. pPage
cac0: 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 r->journalStarte
cad0: 64 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 d = 0;. }else
cae0: 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 78 63 if( pPager->exc
caf0: 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 20 lusiveMode .
cb00: 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 || pPager->jour
cb10: 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
cb20: 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 OURNALMODE_PERSI
cb30: 53 54 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 ST. ){.
cb40: 72 63 20 3d 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c rc = zeroJournal
cb50: 48 64 72 28 70 50 61 67 65 72 2c 20 68 61 73 4d Hdr(pPager, hasM
cb60: 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 70 61 aster);. pa
cb70: 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 ger_error(pPager
cb80: 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 70 50 61 , rc);. pPa
cb90: 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
cba0: 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 = 0;. pPage
cbb0: 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 r->journalStarte
cbc0: 64 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 d = 0;. }else
cbd0: 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
cbe0: 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
cbf0: 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
cc00: 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20 29 3b ALMODE_DELETE );
cc10: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 . sqlite3Os
cc20: 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 Close(pPager->jf
cc30: 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 d);. if( !p
cc40: 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
cc50: 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
cc60: 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 sqlite3OsDelete(
cc70: 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 pPager->pVfs, pP
cc80: 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 ager->zJournal,
cc90: 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0);. }.
cca0: 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
ccb0: 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 _CHECK_PAGES.
ccc0: 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 sqlite3PcacheIt
ccd0: 65 72 61 74 65 44 69 72 74 79 28 70 50 61 67 65 erateDirty(pPage
cce0: 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 65 r->pPCache, page
ccf0: 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 29 3b r_set_pagehash);
cd00: 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 73 71 6c .#endif.. sql
cd10: 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 ite3PcacheCleanA
cd20: 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ll(pPager->pPCac
cd30: 68 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 he);. sqlite3
cd40: 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 BitvecDestroy(pP
cd50: 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
cd60: 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 );. pPager->p
cd70: 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 InJournal = 0;.
cd80: 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 pPager->nRec
cd90: 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 = 0;. }.. if(
cda0: 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 !pPager->exclusi
cdb0: 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 veMode ){. rc
cdc0: 32 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 2 = osUnlock(pPa
cdd0: 67 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f ger->fd, SHARED_
cde0: 4c 4f 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 65 LOCK);. pPage
cdf0: 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 r->state = PAGER
ce00: 5f 53 48 41 52 45 44 3b 0a 20 20 20 20 70 50 61 _SHARED;. pPa
ce10: 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 ger->changeCount
ce20: 44 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 Done = 0;. }els
ce30: 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 e if( pPager->st
ce40: 61 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 ate==PAGER_SYNCE
ce50: 44 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d D ){. pPager-
ce60: 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 >state = PAGER_E
ce70: 58 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20 20 XCLUSIVE;. }.
ce80: 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 pPager->setMaste
ce90: 72 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d r = 0;. pPager-
cea0: 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 >needSync = 0;.
ceb0: 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 pPager->dbModif
cec0: 69 65 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 ied = 0;.. /* T
ced0: 4f 44 4f 3a 20 49 73 20 74 68 69 73 20 6f 70 74 ODO: Is this opt
cee0: 69 6d 61 6c 3f 20 57 68 79 20 69 73 20 74 68 65 imal? Why is the
cef0: 20 64 62 20 73 69 7a 65 20 69 6e 76 61 6c 69 64 db size invalid
cf00: 61 74 65 64 20 68 65 72 65 20 0a 20 20 2a 2a 20 ated here . **
cf10: 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 when the databas
cf20: 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 75 6e e file is not un
cf30: 6c 6f 63 6b 65 64 3f 20 2a 2f 0a 20 20 70 50 61 locked? */. pPa
cf40: 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 ger->dbOrigSize
cf50: 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 = 0;. sqlite3Pc
cf60: 61 63 68 65 54 72 75 6e 63 61 74 65 28 70 50 61 acheTruncate(pPa
cf70: 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 50 ger->pPCache, pP
cf80: 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 ager->dbSize);.
cf90: 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 if( !MEMDB ){.
cfa0: 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a pPager->dbSiz
cfb0: 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a eValid = 0;. }.
cfc0: 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 . return (rc==S
cfd0: 51 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63 29 QLITE_OK?rc2:rc)
cfe0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d ;.}../*.** Param
cff0: 65 74 65 72 20 61 44 61 74 61 20 6d 75 73 74 20 eter aData must
d000: 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 point to a buffe
d010: 72 20 6f 66 20 70 50 61 67 65 72 2d 3e 70 61 67 r of pPager->pag
d020: 65 53 69 7a 65 20 62 79 74 65 73 0a 2a 2a 20 6f eSize bytes.** o
d030: 66 20 64 61 74 61 2e 20 43 6f 6d 70 75 74 65 20 f data. Compute
d040: 61 6e 64 20 72 65 74 75 72 6e 20 61 20 63 68 65 and return a che
d050: 63 6b 73 75 6d 20 62 61 73 65 64 20 6f 6e 74 20 cksum based ont
d060: 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
d070: 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 6f 66 20 the .** page of
d080: 64 61 74 61 20 61 6e 64 20 74 68 65 20 63 75 72 data and the cur
d090: 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 50 rent value of pP
d0a0: 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 2e ager->cksumInit.
d0b0: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e .**.** This is n
d0c0: 6f 74 20 61 20 72 65 61 6c 20 63 68 65 63 6b 73 ot a real checks
d0d0: 75 6d 2e 20 49 74 20 69 73 20 72 65 61 6c 6c 79 um. It is really
d0e0: 20 6a 75 73 74 20 74 68 65 20 73 75 6d 20 6f 66 just the sum of
d0f0: 20 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 the .** random
d100: 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 28 70 initial value (p
d110: 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 Pager->cksumInit
d120: 29 20 61 6e 64 20 65 76 65 72 79 20 32 30 30 74 ) and every 200t
d130: 68 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 65 h byte.** of the
d140: 20 70 61 67 65 20 64 61 74 61 2c 20 73 74 61 72 page data, star
d150: 74 69 6e 67 20 77 69 74 68 20 62 79 74 65 20 6f ting with byte o
d160: 66 66 73 65 74 20 28 70 50 61 67 65 72 2d 3e 70 ffset (pPager->p
d170: 61 67 65 53 69 7a 65 25 32 30 30 29 2e 0a 2a 2a ageSize%200)..**
d180: 20 45 61 63 68 20 62 79 74 65 20 69 73 20 69 6e Each byte is in
d190: 74 65 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 terpreted as an
d1a0: 38 2d 62 69 74 20 75 6e 73 69 67 6e 65 64 20 69 8-bit unsigned i
d1b0: 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 68 nteger..**.** Ch
d1c0: 61 6e 67 69 6e 67 20 74 68 65 20 66 6f 72 6d 75 anging the formu
d1d0: 6c 61 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 la used to compu
d1e0: 74 65 20 74 68 69 73 20 63 68 65 63 6b 73 75 6d te this checksum
d1f0: 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a 2a results in an.*
d200: 2a 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 6a * incompatible j
d210: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d ournal file form
d220: 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6a 6f 75 at..**.** If jou
d230: 72 6e 61 6c 20 63 6f 72 72 75 70 74 69 6f 6e 20 rnal corruption
d240: 6f 63 63 75 72 73 20 64 75 65 20 74 6f 20 61 20 occurs due to a
d250: 70 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20 74 power failure, t
d260: 68 65 20 6d 6f 73 74 20 6c 69 6b 65 6c 79 20 0a he most likely .
d270: 2a 2a 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 ** scenario is t
d280: 68 61 74 20 6f 6e 65 20 65 6e 64 20 6f 72 20 74 hat one end or t
d290: 68 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 he other of the
d2a0: 72 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 20 63 record will be c
d2b0: 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 74 20 69 hanged. .** It i
d2c0: 73 20 6d 75 63 68 20 6c 65 73 73 20 6c 69 6b 65 s much less like
d2d0: 6c 79 20 74 68 61 74 20 74 68 65 20 74 77 6f 20 ly that the two
d2e0: 65 6e 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 ends of the jour
d2f0: 6e 61 6c 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 nal record will
d300: 62 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 6e be.** correct an
d310: 64 20 74 68 65 20 6d 69 64 64 6c 65 20 62 65 20 d the middle be
d320: 63 6f 72 72 75 70 74 2e 20 20 54 68 75 73 2c 20 corrupt. Thus,
d330: 74 68 69 73 20 22 63 68 65 63 6b 73 75 6d 22 20 this "checksum"
d340: 73 63 68 65 6d 65 2c 0a 2a 2a 20 74 68 6f 75 67 scheme,.** thoug
d350: 68 20 66 61 73 74 20 61 6e 64 20 73 69 6d 70 6c h fast and simpl
d360: 65 2c 20 63 61 74 63 68 65 73 20 74 68 65 20 6d e, catches the m
d370: 6f 73 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69 6e ostly likely kin
d380: 64 20 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e 2e d of corruption.
d390: 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20 70 .*/.static u32 p
d3a0: 61 67 65 72 5f 63 6b 73 75 6d 28 50 61 67 65 72 ager_cksum(Pager
d3b0: 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 *pPager, const
d3c0: 75 38 20 2a 61 44 61 74 61 29 7b 0a 20 20 75 33 u8 *aData){. u3
d3d0: 32 20 63 6b 73 75 6d 20 3d 20 70 50 61 67 65 72 2 cksum = pPager
d3e0: 2d 3e 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 ->cksumInit;
d3f0: 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d /* Checksum
d400: 20 76 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e value to return
d410: 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 50 */. int i = pP
d420: 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d 32 ager->pageSize-2
d430: 30 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 00; /*
d440: 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a Loop counter */.
d450: 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a while( i>0 ){.
d460: 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44 61 cksum += aDa
d470: 74 61 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d 20 ta[i];. i -=
d480: 32 30 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 200;. }. retur
d490: 6e 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 0a 2a n cksum;.}../*.*
d4a0: 2a 20 52 65 61 64 20 61 20 73 69 6e 67 6c 65 20 * Read a single
d4b0: 70 61 67 65 20 66 72 6f 6d 20 65 69 74 68 65 72 page from either
d4c0: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
d4d0: 65 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c e (if isMainJrnl
d4e0: 3d 3d 31 29 20 6f 72 0a 2a 2a 20 66 72 6f 6d 20 ==1) or.** from
d4f0: 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 the sub-journal
d500: 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d (if isMainJrnl==
d510: 30 29 20 61 6e 64 20 70 6c 61 79 62 61 63 6b 20 0) and playback
d520: 74 68 61 74 20 70 61 67 65 2e 0a 2a 2a 20 54 68 that page..** Th
d530: 65 20 70 61 67 65 20 62 65 67 69 6e 73 20 61 74 e page begins at
d540: 20 6f 66 66 73 65 74 20 2a 70 4f 66 66 73 65 74 offset *pOffset
d550: 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 into the file.
d560: 54 68 65 20 2a 70 4f 66 66 73 65 74 0a 2a 2a 20 The *pOffset.**
d570: 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 value is increas
d580: 65 64 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 ed to the start
d590: 6f 66 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 of the next page
d5a0: 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e in the journal.
d5b0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 4d 61 69 .**.** The isMai
d5c0: 6e 4a 72 6e 6c 20 66 6c 61 67 20 69 73 20 74 72 nJrnl flag is tr
d5d0: 75 65 20 69 66 20 74 68 69 73 20 69 73 20 74 68 ue if this is th
d5e0: 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 e main rollback
d5f0: 6a 6f 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 66 journal and.** f
d600: 61 6c 73 65 20 66 6f 72 20 74 68 65 20 73 74 61 alse for the sta
d610: 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 tement journal.
d620: 20 54 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 The main rollba
d630: 63 6b 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73 0a ck journal uses.
d640: 2a 2a 20 63 68 65 63 6b 73 75 6d 73 20 2d 20 74 ** checksums - t
d650: 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 he statement jou
d660: 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a rnal does not..*
d670: 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 *.** If the page
d680: 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 number of the p
d690: 61 67 65 20 72 65 63 6f 72 64 20 72 65 61 64 20 age record read
d6a0: 66 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a from the (sub-)j
d6b0: 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 ournal file.** i
d6c0: 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 s greater than t
d6d0: 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 he current value
d6e0: 20 6f 66 20 50 61 67 65 72 2e 64 62 53 69 7a 65 of Pager.dbSize
d6f0: 2c 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 20 , then playback
d700: 69 73 0a 2a 2a 20 73 6b 69 70 70 65 64 20 61 6e is.** skipped an
d710: 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 d SQLITE_OK is r
d720: 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 eturned..**.** I
d730: 66 20 70 44 6f 6e 65 20 69 73 20 6e 6f 74 20 4e f pDone is not N
d740: 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 ULL, then it is
d750: 61 20 72 65 63 6f 72 64 20 6f 66 20 70 61 67 65 a record of page
d760: 73 20 74 68 61 74 20 68 61 76 65 20 61 6c 72 65 s that have alre
d770: 61 64 79 0a 2a 2a 20 62 65 65 6e 20 70 6c 61 79 ady.** been play
d780: 65 64 20 62 61 63 6b 2e 20 20 49 66 20 74 68 65 ed back. If the
d790: 20 70 61 67 65 20 61 74 20 2a 70 4f 66 66 73 65 page at *pOffse
d7a0: 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 t has already be
d7b0: 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 0a 2a en played back.*
d7c0: 2a 20 28 69 66 20 74 68 65 20 63 6f 72 72 65 73 * (if the corres
d7d0: 70 6f 6e 64 69 6e 67 20 70 44 6f 6e 65 20 62 69 ponding pDone bi
d7e0: 74 20 69 73 20 73 65 74 29 20 74 68 65 6e 20 73 t is set) then s
d7f0: 6b 69 70 20 74 68 65 20 70 6c 61 79 62 61 63 6b kip the playback
d800: 2e 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 ..** Make sure t
d810: 68 65 20 70 44 6f 6e 65 20 62 69 74 20 63 6f 72 he pDone bit cor
d820: 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 responding to th
d830: 65 20 2a 70 4f 66 66 73 65 74 20 70 61 67 65 20 e *pOffset page
d840: 69 73 20 73 65 74 0a 2a 2a 20 70 72 69 6f 72 20 is set.** prior
d850: 74 6f 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a to returning..**
d860: 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 .** If the page
d870: 72 65 63 6f 72 64 20 69 73 20 73 75 63 63 65 73 record is succes
d880: 73 66 75 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d sfully read from
d890: 20 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e the (sub-)journ
d8a0: 61 6c 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 70 al file.** and p
d8b0: 6c 61 79 65 64 20 62 61 63 6b 2c 20 74 68 65 6e layed back, then
d8c0: 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
d8d0: 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f turned. If an IO
d8e0: 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a 2a error occurs.**
d8f0: 20 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 74 while reading t
d900: 68 65 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 74 he record from t
d910: 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c he (sub-)journal
d920: 20 66 69 6c 65 20 6f 72 20 77 68 69 6c 65 20 77 file or while w
d930: 72 69 74 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 65 riting.** to the
d940: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
d950: 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f then the IO erro
d960: 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
d970: 65 64 2e 20 49 66 20 64 61 74 61 0a 2a 2a 20 69 ed. If data.** i
d980: 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 s successfully r
d990: 65 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 ead from the (su
d9a0: 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 b-)journal file
d9b0: 62 75 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 but appears to b
d9c0: 65 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 2c 20 e.** corrupted,
d9d0: 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 SQLITE_DONE is r
d9e0: 65 74 75 72 6e 65 64 2e 20 44 61 74 61 20 69 73 eturned. Data is
d9f0: 20 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 72 72 considered corr
da00: 75 70 74 65 64 20 69 6e 0a 2a 2a 20 74 77 6f 20 upted in.** two
da10: 63 69 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a 2a circumstances:.*
da20: 2a 20 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 * .** * If the
da30: 20 72 65 63 6f 72 64 20 70 61 67 65 2d 6e 75 6d record page-num
da40: 62 65 72 20 69 73 20 69 6c 6c 65 67 61 6c 20 28 ber is illegal (
da50: 30 20 6f 72 20 50 41 47 45 52 5f 4d 4a 5f 50 47 0 or PAGER_MJ_PG
da60: 4e 4f 29 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20 49 NO), or.** * I
da70: 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 f the record is
da80: 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 being rolled bac
da90: 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 k from the main
daa0: 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 journal file.**
dab0: 20 20 20 20 61 6e 64 20 74 68 65 20 63 68 65 63 and the chec
dac0: 6b 73 75 6d 20 66 69 65 6c 64 20 64 6f 65 73 20 ksum field does
dad0: 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 72 65 not match the re
dae0: 63 6f 72 64 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a cord content..**
daf0: 0a 2a 2a 20 4e 65 69 74 68 65 72 20 6f 66 20 74 .** Neither of t
db00: 68 65 73 65 20 74 77 6f 20 73 63 65 6e 61 72 69 hese two scenari
db10: 6f 73 20 61 72 65 20 70 6f 73 73 69 62 6c 65 20 os are possible
db20: 64 75 72 69 6e 67 20 61 20 73 61 76 65 70 6f 69 during a savepoi
db30: 6e 74 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a nt rollback..**.
db40: 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 ** If this is a
db50: 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 savepoint rollba
db60: 63 6b 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 20 ck, then memory
db70: 6d 61 79 20 68 61 76 65 20 74 6f 20 62 65 20 64 may have to be d
db80: 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c ynamically.** al
db90: 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 20 located by this
dba0: 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 74 68 69 function. If thi
dbb0: 73 20 69 73 20 74 68 65 20 63 61 73 65 20 61 6e s is the case an
dbc0: 64 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 d an allocation
dbd0: 66 61 69 6c 73 2c 0a 2a 2a 20 53 51 4c 49 54 45 fails,.** SQLITE
dbe0: 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e _NOMEM is return
dbf0: 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
dc00: 74 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b t pager_playback
dc10: 5f 6f 6e 65 5f 70 61 67 65 28 0a 20 20 50 61 67 _one_page(. Pag
dc20: 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 er *pPager,
dc30: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
dc40: 65 20 70 61 67 65 72 20 62 65 69 6e 67 20 70 6c e pager being pl
dc50: 61 79 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 ayed back */. i
dc60: 6e 74 20 69 73 4d 61 69 6e 4a 72 6e 6c 2c 20 20 nt isMainJrnl,
dc70: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
dc80: 31 20 2d 3e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 1 -> main journa
dc90: 6c 2e 20 30 20 2d 3e 20 73 75 62 2d 6a 6f 75 72 l. 0 -> sub-jour
dca0: 6e 61 6c 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 nal. */. int is
dcb0: 55 6e 73 79 6e 63 2c 20 20 20 20 20 20 20 20 20 Unsync,
dcc0: 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
dcd0: 69 66 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 if reading from
dce0: 75 6e 73 79 6e 63 65 64 20 6d 61 69 6e 20 6a 6f unsynced main jo
dcf0: 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 2a urnal */. i64 *
dd00: 70 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 pOffset,
dd10: 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
dd20: 65 74 20 6f 66 20 72 65 63 6f 72 64 20 74 6f 20 et of record to
dd30: 70 6c 61 79 62 61 63 6b 20 2a 2f 0a 20 20 69 6e playback */. in
dd40: 74 20 69 73 53 61 76 65 70 6e 74 2c 20 20 20 20 t isSavepnt,
dd50: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
dd60: 72 75 65 20 66 6f 72 20 61 20 73 61 76 65 70 6f rue for a savepo
dd70: 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a int rollback */.
dd80: 20 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 Bitvec *pDone
dd90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dda0: 2f 2a 20 42 69 74 76 65 63 20 6f 66 20 70 61 67 /* Bitvec of pag
ddb0: 65 73 20 61 6c 72 65 61 64 79 20 70 6c 61 79 65 es already playe
ddc0: 64 20 62 61 63 6b 20 2a 2f 0a 29 7b 0a 20 20 69 d back */.){. i
ddd0: 6e 74 20 72 63 3b 0a 20 20 50 67 48 64 72 20 2a nt rc;. PgHdr *
dde0: 70 50 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 pPg;
ddf0: 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 69 /* An exi
de00: 73 74 69 6e 67 20 70 61 67 65 20 69 6e 20 74 68 sting page in th
de10: 65 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e e cache */. Pgn
de20: 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 o pgno;
de30: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
de40: 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 e page number of
de50: 20 61 20 70 61 67 65 20 69 6e 20 6a 6f 75 72 6e a page in journ
de60: 61 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 al */. u32 cksu
de70: 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m;
de80: 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 /* Checksu
de90: 6d 20 75 73 65 64 20 66 6f 72 20 73 61 6e 69 74 m used for sanit
dea0: 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 y checking */.
deb0: 75 38 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 u8 *aData;
dec0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
ded0: 20 54 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 Temporary stora
dee0: 67 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 20 ge for the page
def0: 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c */. sqlite3_fil
df00: 65 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 20 e *jfd;
df10: 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 /* The file d
df20: 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74 68 escriptor for th
df30: 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a e journal file *
df40: 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 /.. assert( (is
df50: 4d 61 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d 30 20 MainJrnl&~1)==0
df60: 29 3b 20 20 20 20 20 20 2f 2a 20 69 73 4d 61 69 ); /* isMai
df70: 6e 4a 72 6e 6c 20 69 73 20 30 20 6f 72 20 31 20 nJrnl is 0 or 1
df80: 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 */. assert( (is
df90: 53 61 76 65 70 6e 74 26 7e 31 29 3d 3d 30 20 29 Savepnt&~1)==0 )
dfa0: 3b 20 20 20 20 20 20 20 2f 2a 20 69 73 53 61 76 ; /* isSav
dfb0: 65 70 6e 74 20 69 73 20 30 20 6f 72 20 31 20 2a epnt is 0 or 1 *
dfc0: 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4d 61 /. assert( isMa
dfd0: 69 6e 4a 72 6e 6c 20 7c 7c 20 70 44 6f 6e 65 20 inJrnl || pDone
dfe0: 29 3b 20 20 20 20 20 2f 2a 20 70 44 6f 6e 65 20 ); /* pDone
dff0: 61 6c 77 61 79 73 20 75 73 65 64 20 6f 6e 20 73 always used on s
e000: 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a 20 ub-journals */.
e010: 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65 70 assert( isSavep
e020: 6e 74 20 7c 7c 20 70 44 6f 6e 65 3d 3d 30 20 29 nt || pDone==0 )
e030: 3b 20 20 20 2f 2a 20 70 44 6f 6e 65 20 6e 65 76 ; /* pDone nev
e040: 65 72 20 75 73 65 64 20 6f 6e 20 6e 6f 6e 2d 73 er used on non-s
e050: 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 61 avepoint */.. a
e060: 44 61 74 61 20 3d 20 28 75 38 2a 29 70 50 61 67 Data = (u8*)pPag
e070: 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 er->pTmpSpace;.
e080: 20 61 73 73 65 72 74 28 20 61 44 61 74 61 20 29 assert( aData )
e090: 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d ; /* Tem
e0a0: 70 20 73 74 6f 72 61 67 65 20 6d 75 73 74 20 68 p storage must h
e0b0: 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e ave already been
e0c0: 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 20 allocated */..
e0d0: 20 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 /* Read the pag
e0e0: 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67 e number and pag
e0f0: 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 e data from the
e100: 6a 6f 75 72 6e 61 6c 20 6f 72 20 73 75 62 2d 6a journal or sub-j
e110: 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 ournal. ** file
e120: 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f . Return an erro
e130: 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 r code to the ca
e140: 6c 6c 65 72 20 69 66 20 61 6e 20 49 4f 20 65 72 ller if an IO er
e150: 72 6f 72 20 6f 63 63 75 72 73 2e 0a 20 20 2a 2f ror occurs.. */
e160: 0a 20 20 6a 66 64 20 3d 20 69 73 4d 61 69 6e 4a . jfd = isMainJ
e170: 72 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e 6a 66 rnl ? pPager->jf
e180: 64 20 3a 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 d : pPager->sjfd
e190: 3b 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 ;. rc = read32b
e1a0: 69 74 73 28 6a 66 64 2c 20 2a 70 4f 66 66 73 65 its(jfd, *pOffse
e1b0: 74 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28 t, &pgno);. if(
e1c0: 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
e1d0: 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 return rc;. rc
e1e0: 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 = sqlite3OsRead
e1f0: 28 6a 66 64 2c 20 61 44 61 74 61 2c 20 70 50 61 (jfd, aData, pPa
e200: 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 ger->pageSize, (
e210: 2a 70 4f 66 66 73 65 74 29 2b 34 29 3b 0a 20 20 *pOffset)+4);.
e220: 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
e230: 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 K ) return rc;.
e240: 20 2a 70 4f 66 66 73 65 74 20 2b 3d 20 70 50 61 *pOffset += pPa
e250: 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 ger->pageSize +
e260: 34 20 2b 20 69 73 4d 61 69 6e 4a 72 6e 6c 2a 34 4 + isMainJrnl*4
e270: 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 ;.. /* Sanity c
e280: 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 70 hecking on the p
e290: 61 67 65 2e 20 20 54 68 69 73 20 69 73 20 6d 6f age. This is mo
e2a0: 72 65 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 re important tha
e2b0: 74 20 49 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 t I originally.
e2c0: 20 2a 2a 20 74 68 6f 75 67 68 74 2e 20 20 49 66 ** thought. If
e2d0: 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 a power failure
e2e0: 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 68 occurs while th
e2f0: 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69 e journal is bei
e300: 6e 67 20 77 72 69 74 74 65 6e 2c 0a 20 20 2a 2a ng written,. **
e310: 20 69 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 it could cause
e320: 69 6e 76 61 6c 69 64 20 64 61 74 61 20 74 6f 20 invalid data to
e330: 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 be written into
e340: 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 57 65 the journal. We
e350: 20 6e 65 65 64 20 74 6f 0a 20 20 2a 2a 20 64 65 need to. ** de
e360: 74 65 63 74 20 74 68 69 73 20 69 6e 76 61 6c 69 tect this invali
e370: 64 20 64 61 74 61 20 28 77 69 74 68 20 68 69 67 d data (with hig
e380: 68 20 70 72 6f 62 61 62 69 6c 69 74 79 29 20 61 h probability) a
e390: 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 20 20 nd ignore it..
e3a0: 2a 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 */. if( pgno==0
e3b0: 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f || pgno==PAGER_
e3c0: 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 MJ_PGNO(pPager)
e3d0: 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 ){. assert( !
e3e0: 69 73 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 20 isSavepnt );.
e3f0: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 return SQLITE_D
e400: 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 ONE;. }. if( p
e410: 67 6e 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72 gno>(Pgno)pPager
e420: 2d 3e 64 62 53 69 7a 65 20 7c 7c 20 73 71 6c 69 ->dbSize || sqli
e430: 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 44 te3BitvecTest(pD
e440: 6f 6e 65 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 one, pgno) ){.
e450: 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e460: 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 OK;. }. if( is
e470: 4d 61 69 6e 4a 72 6e 6c 20 29 7b 0a 20 20 20 20 MainJrnl ){.
e480: 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
e490: 6a 66 64 2c 20 28 2a 70 4f 66 66 73 65 74 29 2d jfd, (*pOffset)-
e4a0: 34 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 20 20 20 4, &cksum);.
e4b0: 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
e4c0: 72 63 3b 0a 20 20 20 20 69 66 28 20 21 69 73 53 rc;. if( !isS
e4d0: 61 76 65 70 6e 74 20 26 26 20 70 61 67 65 72 5f avepnt && pager_
e4e0: 63 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 61 44 cksum(pPager, aD
e4f0: 61 74 61 29 21 3d 63 6b 73 75 6d 20 29 7b 0a 20 ata)!=cksum ){.
e500: 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
e510: 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 TE_DONE;. }.
e520: 20 7d 0a 0a 20 20 69 66 28 20 70 44 6f 6e 65 20 }.. if( pDone
e530: 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 && (rc = sqlite3
e540: 42 69 74 76 65 63 53 65 74 28 70 44 6f 6e 65 2c BitvecSet(pDone,
e550: 20 70 67 6e 6f 29 29 21 3d 53 51 4c 49 54 45 5f pgno))!=SQLITE_
e560: 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OK ){. return
e570: 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 rc;. }.. asse
e580: 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
e590: 65 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 e==PAGER_RESERVE
e5a0: 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 D || pPager->sta
e5b0: 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 te>=PAGER_EXCLUS
e5c0: 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 IVE );.. /* If
e5d0: 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
e5e0: 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 RESERVED state,
e5f0: 74 68 65 6e 20 74 68 65 72 65 20 6d 75 73 74 20 then there must
e600: 62 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 69 be a copy of thi
e610: 73 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 s. ** page in t
e620: 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 he pager cache.
e630: 49 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75 73 In this case jus
e640: 74 20 75 70 64 61 74 65 20 74 68 65 20 70 61 67 t update the pag
e650: 65 72 20 63 61 63 68 65 2c 0a 20 20 2a 2a 20 6e er cache,. ** n
e660: 6f 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ot the database
e670: 66 69 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 file. The page i
e680: 73 20 6c 65 66 74 20 6d 61 72 6b 65 64 20 64 69 s left marked di
e690: 72 74 79 20 69 6e 20 74 68 69 73 20 63 61 73 65 rty in this case
e6a0: 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 .. **. ** An e
e6b0: 78 63 65 70 74 69 6f 6e 20 74 6f 20 74 68 65 20 xception to the
e6c0: 61 62 6f 76 65 20 72 75 6c 65 3a 20 49 66 20 74 above rule: If t
e6d0: 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 69 he database is i
e6e0: 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 n no-sync mode.
e6f0: 20 2a 2a 20 61 6e 64 20 61 20 70 61 67 65 20 69 ** and a page i
e700: 73 20 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61 s moved during a
e710: 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 n incremental va
e720: 63 75 75 6d 20 74 68 65 6e 20 74 68 65 20 70 61 cuum then the pa
e730: 67 65 20 6d 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 ge may. ** not
e740: 62 65 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 be in the pager
e750: 63 61 63 68 65 2e 20 4c 61 74 65 72 3a 20 69 66 cache. Later: if
e760: 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 a malloc() or I
e770: 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 20 O error occurs.
e780: 20 2a 2a 20 64 75 72 69 6e 67 20 61 20 4d 6f 76 ** during a Mov
e790: 65 70 61 67 65 28 29 20 63 61 6c 6c 2c 20 74 68 epage() call, th
e7a0: 65 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79 20 en the page may
e7b0: 6e 6f 74 20 62 65 20 69 6e 20 74 68 65 20 63 61 not be in the ca
e7c0: 63 68 65 0a 20 20 2a 2a 20 65 69 74 68 65 72 2e che. ** either.
e7d0: 20 53 6f 20 74 68 65 20 63 6f 6e 64 69 74 69 6f So the conditio
e7e0: 6e 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 74 n described in t
e7f0: 68 65 20 61 62 6f 76 65 20 70 61 72 61 67 72 61 he above paragra
e800: 70 68 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 ph is not. ** a
e810: 73 73 65 72 74 28 29 61 62 6c 65 2e 0a 20 20 2a ssert()able.. *
e820: 2a 0a 20 20 2a 2a 20 49 66 20 69 6e 20 45 58 43 *. ** If in EXC
e830: 4c 55 53 49 56 45 20 73 74 61 74 65 2c 20 74 68 LUSIVE state, th
e840: 65 6e 20 77 65 20 75 70 64 61 74 65 20 74 68 65 en we update the
e850: 20 70 61 67 65 72 20 63 61 63 68 65 20 69 66 20 pager cache if
e860: 69 74 20 65 78 69 73 74 73 0a 20 20 2a 2a 20 61 it exists. ** a
e870: 6e 64 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 nd the main file
e880: 2e 20 54 68 65 20 70 61 67 65 20 69 73 20 74 68 . The page is th
e890: 65 6e 20 6d 61 72 6b 65 64 20 6e 6f 74 20 64 69 en marked not di
e8a0: 72 74 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 rty.. **. ** T
e8b0: 69 63 6b 65 74 20 23 31 31 37 31 3a 20 20 54 68 icket #1171: Th
e8c0: 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 e statement jour
e8d0: 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 nal might contai
e8e0: 6e 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 n page content t
e8f0: 68 61 74 20 69 73 0a 20 20 2a 2a 20 64 69 66 66 hat is. ** diff
e900: 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 erent from the p
e910: 61 67 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 74 age content at t
e920: 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 he start of the
e930: 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a transaction.. *
e940: 2a 20 54 68 69 73 20 6f 63 63 75 72 73 20 77 68 * This occurs wh
e950: 65 6e 20 61 20 70 61 67 65 20 69 73 20 63 68 61 en a page is cha
e960: 6e 67 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 nged prior to th
e970: 65 20 73 74 61 72 74 20 6f 66 20 61 20 73 74 61 e start of a sta
e980: 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 68 65 6e tement. ** then
e990: 20 63 68 61 6e 67 65 64 20 61 67 61 69 6e 20 77 changed again w
e9a0: 69 74 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d ithin the statem
e9b0: 65 6e 74 2e 20 20 57 68 65 6e 20 72 6f 6c 6c 69 ent. When rolli
e9c0: 6e 67 20 62 61 63 6b 20 73 75 63 68 20 61 0a 20 ng back such a.
e9d0: 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 ** statement we
e9e0: 20 6d 75 73 74 20 6e 6f 74 20 77 72 69 74 65 20 must not write
e9f0: 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 to the original
ea00: 64 61 74 61 62 61 73 65 20 75 6e 6c 65 73 73 20 database unless
ea10: 77 65 20 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 we know. ** for
ea20: 20 63 65 72 74 61 69 6e 20 74 68 61 74 20 6f 72 certain that or
ea30: 69 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 iginal page cont
ea40: 65 6e 74 73 20 61 72 65 20 73 79 6e 63 65 64 20 ents are synced
ea50: 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f into the main ro
ea60: 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 llback. ** jour
ea70: 6e 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 2c nal. Otherwise,
ea80: 20 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6d 69 a power loss mi
ea90: 67 68 74 20 6c 65 61 76 65 20 6d 6f 64 69 66 69 ght leave modifi
eaa0: 65 64 20 64 61 74 61 20 69 6e 20 74 68 65 0a 20 ed data in the.
eab0: 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c ** database fil
eac0: 65 20 77 69 74 68 6f 75 74 20 61 6e 20 65 6e 74 e without an ent
ead0: 72 79 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 ry in the rollba
eae0: 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 ck journal that
eaf0: 63 61 6e 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 can. ** restore
eb00: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f the database to
eb10: 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 66 6f its original fo
eb20: 72 6d 2e 20 20 54 77 6f 20 63 6f 6e 64 69 74 69 rm. Two conditi
eb30: 6f 6e 73 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a ons must be. **
eb40: 20 6d 65 74 20 62 65 66 6f 72 65 20 77 72 69 74 met before writ
eb50: 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 ing to the datab
eb60: 61 73 65 20 66 69 6c 65 73 2e 20 28 31 29 20 74 ase files. (1) t
eb70: 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 he database must
eb80: 20 62 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e be. ** locked.
eb90: 20 20 28 32 29 20 77 65 20 6b 6e 6f 77 20 74 68 (2) we know th
eba0: 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 at the original
ebb0: 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 page content is
ebc0: 66 75 6c 6c 79 20 73 79 6e 63 65 64 0a 20 20 2a fully synced. *
ebd0: 2a 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f * in the main jo
ebe0: 75 72 6e 61 6c 20 65 69 74 68 65 72 20 62 65 63 urnal either bec
ebf0: 61 75 73 65 20 74 68 65 20 70 61 67 65 20 69 73 ause the page is
ec00: 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 not in cache or
ec10: 20 65 6c 73 65 0a 20 20 2a 2a 20 74 68 65 20 70 else. ** the p
ec20: 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 age is marked as
ec30: 20 6e 65 65 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 needSync==0..
ec40: 2a 2a 0a 20 20 2a 2a 20 32 30 30 38 2d 30 34 2d **. ** 2008-04-
ec50: 31 34 3a 20 20 57 68 65 6e 20 61 74 74 65 6d 70 14: When attemp
ec60: 74 69 6e 67 20 74 6f 20 76 61 63 75 75 6d 20 61 ting to vacuum a
ec70: 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 corrupt databas
ec80: 65 20 66 69 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 e file, it. **
ec90: 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 is possible to f
eca0: 61 69 6c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 ail a statement
ecb0: 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 68 on a database th
ecc0: 61 74 20 64 6f 65 73 20 6e 6f 74 20 79 65 74 20 at does not yet
ecd0: 65 78 69 73 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e exist.. ** Do n
ece0: 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 ot attempt to wr
ecf0: 69 74 65 20 69 66 20 64 61 74 61 62 61 73 65 20 ite if database
ed00: 66 69 6c 65 20 68 61 73 20 6e 65 76 65 72 20 62 file has never b
ed10: 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f een opened.. */
ed20: 0a 20 20 70 50 67 20 3d 20 70 61 67 65 72 5f 6c . pPg = pager_l
ed30: 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 ookup(pPager, pg
ed40: 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 no);. assert( p
ed50: 50 67 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b 0a Pg || !MEMDB );.
ed60: 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 50 PAGERTRACE(("P
ed70: 4c 41 59 42 41 43 4b 20 25 64 20 70 61 67 65 20 LAYBACK %d page
ed80: 25 64 20 68 61 73 68 28 25 30 38 78 29 20 25 73 %d hash(%08x) %s
ed90: 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
eda0: 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 PAGERID(pPag
edb0: 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 er), pgno, pager
edc0: 5f 64 61 74 61 68 61 73 68 28 70 50 61 67 65 72 _datahash(pPager
edd0: 2d 3e 70 61 67 65 53 69 7a 65 2c 20 61 44 61 74 ->pageSize, aDat
ede0: 61 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 a),.
edf0: 20 20 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 3f 22 (isMainJrnl?"
ee00: 6d 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22 3a 22 73 main-journal":"s
ee10: 75 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a 20 20 29 ub-journal"). )
ee20: 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65 72 );. if( (pPager
ee30: 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 ->state>=PAGER_E
ee40: 58 43 4c 55 53 49 56 45 29 0a 20 20 20 26 26 20 XCLUSIVE). &&
ee50: 28 70 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d 28 70 (pPg==0 || 0==(p
ee60: 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
ee70: 4e 45 45 44 5f 53 59 4e 43 29 29 0a 20 20 20 26 NEED_SYNC)). &
ee80: 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d & isOpen(pPager-
ee90: 3e 66 64 29 0a 20 20 20 26 26 20 21 69 73 55 6e >fd). && !isUn
eea0: 73 79 6e 63 0a 20 20 29 7b 0a 20 20 20 20 69 36 sync. ){. i6
eeb0: 34 20 6f 66 73 74 20 3d 20 28 70 67 6e 6f 2d 31 4 ofst = (pgno-1
eec0: 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 )*(i64)pPager->p
eed0: 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 ageSize;. rc
eee0: 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 = sqlite3OsWrite
eef0: 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 61 44 61 (pPager->fd, aDa
ef00: 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 ta, pPager->page
ef10: 53 69 7a 65 2c 20 6f 66 73 74 29 3b 0a 20 20 20 Size, ofst);.
ef20: 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 if( pgno>pPager
ef30: 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a ->dbFileSize ){.
ef40: 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 pPager->db
ef50: 46 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b FileSize = pgno;
ef60: 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
ef70: 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 29 Pager->pBackup )
ef80: 7b 0a 20 20 20 20 20 20 43 4f 44 45 43 31 28 70 {. CODEC1(p
ef90: 50 61 67 65 72 2c 20 61 44 61 74 61 2c 20 70 67 Pager, aData, pg
efa0: 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c 49 54 45 no, 3, rc=SQLITE
efb0: 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 20 20 73 _NOMEM);. s
efc0: 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 qlite3BackupUpda
efd0: 74 65 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b te(pPager->pBack
efe0: 75 70 2c 20 70 67 6e 6f 2c 20 61 44 61 74 61 29 up, pgno, aData)
eff0: 3b 0a 20 20 20 20 20 20 43 4f 44 45 43 31 28 70 ;. CODEC1(p
f000: 50 61 67 65 72 2c 20 61 44 61 74 61 2c 20 70 67 Pager, aData, pg
f010: 6e 6f 2c 20 30 2c 20 72 63 3d 53 51 4c 49 54 45 no, 0, rc=SQLITE
f020: 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 7d 0a 20 _NOMEM);. }.
f030: 20 7d 65 6c 73 65 20 69 66 28 20 21 69 73 4d 61 }else if( !isMa
f040: 69 6e 4a 72 6e 6c 20 26 26 20 70 50 67 3d 3d 30 inJrnl && pPg==0
f050: 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 ){. /* If th
f060: 69 73 20 69 73 20 61 20 72 6f 6c 6c 62 61 63 6b is is a rollback
f070: 20 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 of a savepoint
f080: 61 6e 64 20 64 61 74 61 20 77 61 73 20 6e 6f 74 and data was not
f090: 20 77 72 69 74 74 65 6e 20 74 6f 0a 20 20 20 20 written to.
f0a0: 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
f0b0: 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 20 and the page is
f0c0: 6e 6f 74 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20 74 not in-memory, t
f0d0: 68 65 72 65 20 69 73 20 61 20 70 6f 74 65 6e 74 here is a potent
f0e0: 69 61 6c 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c ial. ** probl
f0f0: 65 6d 2e 20 57 68 65 6e 20 74 68 65 20 70 61 67 em. When the pag
f100: 65 20 69 73 20 6e 65 78 74 20 66 65 74 63 68 65 e is next fetche
f110: 64 20 62 79 20 74 68 65 20 62 2d 74 72 65 65 20 d by the b-tree
f120: 6c 61 79 65 72 2c 20 69 74 20 0a 20 20 20 20 2a layer, it . *
f130: 2a 20 77 69 6c 6c 20 62 65 20 72 65 61 64 20 66 * will be read f
f140: 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
f150: 20 66 69 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 file, which may
f160: 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 0a or may not be .
f170: 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 2e 20 ** current.
f180: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
f190: 68 65 72 65 20 61 72 65 20 61 20 63 6f 75 70 6c here are a coupl
f1a0: 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 77 e of different w
f1b0: 61 79 73 20 74 68 69 73 20 63 61 6e 20 68 61 70 ays this can hap
f1c0: 70 65 6e 2e 20 41 6c 6c 20 61 72 65 20 71 75 69 pen. All are qui
f1d0: 74 65 0a 20 20 20 20 2a 2a 20 6f 62 73 63 75 72 te. ** obscur
f1e0: 65 2e 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 e. When running
f1f0: 69 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d in synchronous m
f200: 6f 64 65 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e ode, this can on
f210: 6c 79 20 68 61 70 70 65 6e 20 0a 20 20 20 20 2a ly happen . *
f220: 2a 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 * if the page is
f230: 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 on the free-lis
f240: 74 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f t at the start o
f250: 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f f the transactio
f260: 6e 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 70 n, then. ** p
f270: 6f 70 75 6c 61 74 65 64 2c 20 74 68 65 6e 20 6d opulated, then m
f280: 6f 76 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 oved using sqlit
f290: 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 e3PagerMovepage(
f2a0: 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a ).. **. **
f2b0: 20 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 The solution is
f2c0: 20 74 6f 20 61 64 64 20 61 6e 20 69 6e 2d 6d 65 to add an in-me
f2d0: 6d 6f 72 79 20 70 61 67 65 20 74 6f 20 74 68 65 mory page to the
f2e0: 20 63 61 63 68 65 20 63 6f 6e 74 61 69 6e 69 6e cache containin
f2f0: 67 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 g. ** the dat
f300: 61 20 6a 75 73 74 20 72 65 61 64 20 66 72 6f 6d a just read from
f310: 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
f320: 2e 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 . Mark the page
f330: 61 73 20 64 69 72 74 79 20 0a 20 20 20 20 2a 2a as dirty . **
f340: 20 61 6e 64 20 69 66 20 74 68 65 20 70 61 67 65 and if the page
f350: 72 20 72 65 71 75 69 72 65 73 20 61 20 6a 6f 75 r requires a jou
f360: 72 6e 61 6c 2d 73 79 6e 63 2c 20 74 68 65 6e 20 rnal-sync, then
f370: 6d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 mark the page as
f380: 20 0a 20 20 20 20 2a 2a 20 72 65 71 75 69 72 69 . ** requiri
f390: 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e ng a journal-syn
f3a0: 63 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 77 c before it is w
f3b0: 72 69 74 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 ritten.. */.
f3c0: 20 20 20 61 73 73 65 72 74 28 20 69 73 53 61 76 assert( isSav
f3d0: 65 70 6e 74 20 29 3b 0a 20 20 20 20 69 66 28 20 epnt );. if(
f3e0: 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 (rc = sqlite3Pag
f3f0: 65 72 41 63 71 75 69 72 65 28 70 50 61 67 65 72 erAcquire(pPager
f400: 2c 20 70 67 6e 6f 2c 20 26 70 50 67 2c 20 31 29 , pgno, &pPg, 1)
f410: 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a )!=SQLITE_OK ){.
f420: 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
f430: 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 67 2d 3e . }. pPg->
f440: 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f flags &= ~PGHDR_
f450: 4e 45 45 44 5f 52 45 41 44 3b 0a 20 20 20 20 73 NEED_READ;. s
f460: 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
f470: 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20 7d 0a Dirty(pPg);. }.
f480: 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 if( pPg ){.
f490: 20 2f 2a 20 4e 6f 20 70 61 67 65 20 73 68 6f 75 /* No page shou
f4a0: 6c 64 20 65 76 65 72 20 62 65 20 65 78 70 6c 69 ld ever be expli
f4b0: 63 69 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 citly rolled bac
f4c0: 6b 20 74 68 61 74 20 69 73 20 69 6e 20 75 73 65 k that is in use
f4d0: 2c 20 65 78 63 65 70 74 0a 20 20 20 20 2a 2a 20 , except. **
f4e0: 66 6f 72 20 70 61 67 65 20 31 20 77 68 69 63 68 for page 1 which
f4f0: 20 69 73 20 68 65 6c 64 20 69 6e 20 75 73 65 20 is held in use
f500: 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 in order to keep
f510: 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 the lock on the
f520: 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 . ** database
f530: 20 61 63 74 69 76 65 2e 20 48 6f 77 65 76 65 72 active. However
f540: 20 73 75 63 68 20 61 20 70 61 67 65 20 6d 61 79 such a page may
f550: 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 be rolled back
f560: 61 73 20 61 20 72 65 73 75 6c 74 0a 20 20 20 20 as a result.
f570: 2a 2a 20 6f 66 20 61 6e 20 69 6e 74 65 72 6e 61 ** of an interna
f580: 6c 20 65 72 72 6f 72 20 72 65 73 75 6c 74 69 6e l error resultin
f590: 67 20 69 6e 20 61 6e 20 61 75 74 6f 6d 61 74 69 g in an automati
f5a0: 63 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a c call to. **
f5b0: 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c sqlite3PagerRol
f5c0: 6c 62 61 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a lback().. */.
f5d0: 20 20 20 20 76 6f 69 64 20 2a 70 44 61 74 61 3b void *pData;
f5e0: 0a 20 20 20 20 70 44 61 74 61 20 3d 20 70 50 67 . pData = pPg
f5f0: 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 6d 65 6d ->pData;. mem
f600: 63 70 79 28 70 44 61 74 61 2c 20 61 44 61 74 61 cpy(pData, aData
f610: 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 , pPager->pageSi
f620: 7a 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d ze);. pPager-
f630: 3e 78 52 65 69 6e 69 74 65 72 28 70 50 67 29 3b >xReiniter(pPg);
f640: 0a 20 20 20 20 69 66 28 20 69 73 4d 61 69 6e 4a . if( isMainJ
f650: 72 6e 6c 20 26 26 20 28 21 69 73 53 61 76 65 70 rnl && (!isSavep
f660: 6e 74 20 7c 7c 20 2a 70 4f 66 66 73 65 74 3c 3d nt || *pOffset<=
f670: 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 pPager->journalH
f680: 64 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 dr) ){. /*
f690: 49 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 If the contents
f6a0: 6f 66 20 74 68 69 73 20 70 61 67 65 20 77 65 72 of this page wer
f6b0: 65 20 6a 75 73 74 20 72 65 73 74 6f 72 65 64 20 e just restored
f6c0: 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 0a 20 from the main .
f6d0: 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 ** journal
f6e0: 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 73 20 63 file, then its c
f6f0: 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 61 ontent must be a
f700: 73 20 74 68 65 79 20 77 65 72 65 20 77 68 65 6e s they were when
f710: 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 the . ** t
f720: 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 66 ransaction was f
f730: 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 49 6e 20 irst opened. In
f740: 74 68 69 73 20 63 61 73 65 20 77 65 20 63 61 6e this case we can
f750: 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 0a 20 mark the page.
f760: 20 20 20 20 20 2a 2a 20 61 73 20 63 6c 65 61 6e ** as clean
f770: 2c 20 73 69 6e 63 65 20 74 68 65 72 65 20 77 69 , since there wi
f780: 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20 74 6f ll be no need to
f790: 20 77 72 69 74 65 20 69 74 20 6f 75 74 20 74 6f write it out to
f7a0: 20 74 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 the.. **.
f7b0: 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20 69 73 ** There is
f7c0: 20 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e 20 74 one exception t
f7d0: 6f 20 74 68 69 73 20 72 75 6c 65 2e 20 49 66 20 o this rule. If
f7e0: 74 68 65 20 70 61 67 65 20 69 73 20 62 65 69 6e the page is bein
f7f0: 67 20 72 6f 6c 6c 65 64 0a 20 20 20 20 20 20 2a g rolled. *
f800: 2a 20 62 61 63 6b 20 61 73 20 70 61 72 74 20 6f * back as part o
f810: 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 28 6f f a savepoint (o
f820: 72 20 73 74 61 74 65 6d 65 6e 74 29 20 72 6f 6c r statement) rol
f830: 6c 62 61 63 6b 20 66 72 6f 6d 20 61 6e 20 0a 20 lback from an .
f840: 20 20 20 20 20 2a 2a 20 75 6e 73 79 6e 63 65 64 ** unsynced
f850: 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 portion of the
f860: 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c main journal fil
f870: 65 2c 20 74 68 65 6e 20 69 74 20 69 73 20 6e 6f e, then it is no
f880: 74 20 73 61 66 65 0a 20 20 20 20 20 20 2a 2a 20 t safe. **
f890: 74 6f 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 to mark the page
f8a0: 20 61 73 20 63 6c 65 61 6e 2e 20 54 68 69 73 20 as clean. This
f8b0: 69 73 20 62 65 63 61 75 73 65 20 6d 61 72 6b 69 is because marki
f8c0: 6e 67 20 74 68 65 20 70 61 67 65 20 61 73 0a 20 ng the page as.
f8d0: 20 20 20 20 20 2a 2a 20 63 6c 65 61 6e 20 77 69 ** clean wi
f8e0: 6c 6c 20 63 6c 65 61 72 20 74 68 65 20 50 47 48 ll clear the PGH
f8f0: 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 DR_NEED_SYNC fla
f900: 67 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61 67 g. Since the pag
f910: 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c e is. ** al
f920: 72 65 61 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 ready in the jou
f930: 72 6e 61 6c 20 66 69 6c 65 20 28 72 65 63 6f 72 rnal file (recor
f940: 64 65 64 20 69 6e 20 50 61 67 65 72 2e 70 49 6e ded in Pager.pIn
f950: 4a 6f 75 72 6e 61 6c 29 20 61 6e 64 0a 20 20 20 Journal) and.
f960: 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52 5f ** the PGHDR_
f970: 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 NEED_SYNC flag i
f980: 73 20 63 6c 65 61 72 65 64 2c 20 69 66 20 74 68 s cleared, if th
f990: 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65 e page is writte
f9a0: 6e 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 61 67 n to. ** ag
f9b0: 61 69 6e 20 77 69 74 68 69 6e 20 74 68 69 73 20 ain within this
f9c0: 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 transaction, it
f9d0: 77 69 6c 6c 20 62 65 20 6d 61 72 6b 65 64 20 61 will be marked a
f9e0: 73 20 64 69 72 74 79 20 62 75 74 0a 20 20 20 20 s dirty but.
f9f0: 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e ** the PGHDR_N
fa00: 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 77 69 EED_SYNC flag wi
fa10: 6c 6c 20 6e 6f 74 20 62 65 20 73 65 74 2e 20 49 ll not be set. I
fa20: 74 20 63 6f 75 6c 64 20 74 68 65 6e 20 70 6f 74 t could then pot
fa30: 65 6e 74 69 61 6c 6c 79 0a 20 20 20 20 20 20 2a entially. *
fa40: 2a 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75 74 * be written out
fa50: 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 into the databa
fa60: 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20 69 se file before i
fa70: 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a ts journal file.
fa80: 20 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74 ** segment
fa90: 20 69 73 20 73 79 6e 63 65 64 2e 20 49 66 20 61 is synced. If a
faa0: 20 63 72 61 73 68 20 6f 63 63 75 72 73 20 64 75 crash occurs du
fab0: 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f 77 69 6e ring or followin
fac0: 67 20 74 68 69 73 2c 0a 20 20 20 20 20 20 2a 2a g this,. **
fad0: 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 database corrup
fae0: 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75 65 2e 0a tion may ensue..
faf0: 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 */. s
fb00: 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
fb10: 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 20 20 Clean(pPg);.
fb20: 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f }.#ifdef SQLITE_
fb30: 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 CHECK_PAGES.
fb40: 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 pPg->pageHash =
fb50: 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 pager_pagehash(p
fb60: 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 Pg);.#endif.
fb70: 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 70 /* If this was p
fb80: 61 67 65 20 31 2c 20 74 68 65 6e 20 72 65 73 74 age 1, then rest
fb90: 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 ore the value of
fba0: 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 Pager.dbFileVer
fbb0: 73 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 74 68 69 s.. ** Do thi
fbc0: 73 20 62 65 66 6f 72 65 20 61 6e 79 20 64 65 63 s before any dec
fbd0: 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 69 66 oding. */. if
fbe0: 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 ( pgno==1 ){.
fbf0: 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 memcpy(&pPage
fc00: 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 26 r->dbFileVers, &
fc10: 28 28 75 38 2a 29 70 44 61 74 61 29 5b 32 34 5d ((u8*)pData)[24]
fc20: 2c 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e ,sizeof(pPager->
fc30: 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 dbFileVers));.
fc40: 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 6f }.. /* Deco
fc50: 64 65 20 74 68 65 20 70 61 67 65 20 6a 75 73 74 de the page just
fc60: 20 72 65 61 64 20 66 72 6f 6d 20 64 69 73 6b 20 read from disk
fc70: 2a 2f 0a 20 20 20 20 43 4f 44 45 43 31 28 70 50 */. CODEC1(pP
fc80: 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 ager, pData, pPg
fc90: 2d 3e 70 67 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 ->pgno, 3, rc=SQ
fca0: 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 LITE_NOMEM);.
fcb0: 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
fcc0: 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 20 7d 0a lease(pPg);. }.
fcd0: 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
fce0: 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 /*.** Parameter
fcf0: 7a 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 6e zMaster is the n
fd00: 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 20 ame of a master
fd10: 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 20 journal file. A
fd20: 73 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 6c 0a 2a single journal.*
fd30: 2a 20 66 69 6c 65 20 74 68 61 74 20 72 65 66 65 * file that refe
fd40: 72 72 65 64 20 74 6f 20 74 68 65 20 6d 61 73 74 rred to the mast
fd50: 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
fd60: 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 72 6f has just been ro
fd70: 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 20 54 68 lled back..** Th
fd80: 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b is routine check
fd90: 73 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 s if it is possi
fda0: 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 ble to delete th
fdb0: 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
fdc0: 20 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64 20 64 6f file,.** and do
fdd0: 65 73 20 73 6f 20 69 66 20 69 74 20 69 73 2e 0a es so if it is..
fde0: 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a **.** Argument z
fdf0: 4d 61 73 74 65 72 20 6d 61 79 20 70 6f 69 6e 74 Master may point
fe00: 20 74 6f 20 50 61 67 65 72 2e 70 54 6d 70 53 70 to Pager.pTmpSp
fe10: 61 63 65 2e 20 53 6f 20 74 68 61 74 20 62 75 66 ace. So that buf
fe20: 66 65 72 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 fer is not .** a
fe30: 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 vailable for use
fe40: 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 75 6e within this fun
fe50: 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 ction..**.** Whe
fe60: 6e 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e n a master journ
fe70: 61 6c 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 al file is creat
fe80: 65 64 2c 20 69 74 20 69 73 20 70 6f 70 75 6c 61 ed, it is popula
fe90: 74 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d ted with the nam
fea0: 65 73 20 0a 2a 2a 20 6f 66 20 61 6c 6c 20 6f 66 es .** of all of
feb0: 20 69 74 73 20 63 68 69 6c 64 20 6a 6f 75 72 6e its child journ
fec0: 61 6c 73 2c 20 6f 6e 65 20 61 66 74 65 72 20 61 als, one after a
fed0: 6e 6f 74 68 65 72 2c 20 66 6f 72 6d 61 74 74 65 nother, formatte
fee0: 64 20 61 73 20 75 74 66 2d 38 20 0a 2a 2a 20 65 d as utf-8 .** e
fef0: 6e 63 6f 64 65 64 20 74 65 78 74 2e 20 54 68 65 ncoded text. The
ff00: 20 65 6e 64 20 6f 66 20 65 61 63 68 20 63 68 69 end of each chi
ff10: 6c 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ld journal file
ff20: 69 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 61 is marked with a
ff30: 20 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 .** nul-termina
ff40: 74 6f 72 20 62 79 74 65 20 28 30 78 30 30 29 2e tor byte (0x00).
ff50: 20 69 2e 65 2e 20 74 68 65 20 65 6e 74 69 72 65 i.e. the entire
ff60: 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20 6d contents of a m
ff70: 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a aster journal.**
ff80: 20 66 69 6c 65 20 66 6f 72 20 61 20 74 72 61 6e file for a tran
ff90: 73 61 63 74 69 6f 6e 20 69 6e 76 6f 6c 76 69 6e saction involvin
ffa0: 67 20 74 77 6f 20 64 61 74 61 62 61 73 65 73 20 g two databases
ffb0: 6d 69 67 68 74 20 62 65 3a 0a 2a 2a 0a 2a 2a 20 might be:.**.**
ffc0: 20 20 22 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 61 2e "/home/bill/a.
ffd0: 64 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 2f 68 db-journal\x00/h
ffe0: 6f 6d 65 2f 62 69 6c 6c 2f 62 2e 64 62 2d 6a 6f ome/bill/b.db-jo
fff0: 75 72 6e 61 6c 5c 78 30 30 22 0a 2a 2a 0a 2a 2a urnal\x00".**.**
10000 20 41 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 A master journa
10010 6c 20 66 69 6c 65 20 6d 61 79 20 6f 6e 6c 79 20 l file may only
10020 62 65 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 be deleted once
10030 61 6c 6c 20 6f 66 20 69 74 73 20 63 68 69 6c 64 all of its child
10040 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 73 20 68 61 .** journals ha
10050 76 65 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 ve been rolled b
10060 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ack..**.** This
10070 66 75 6e 63 74 69 6f 6e 20 72 65 61 64 73 20 74 function reads t
10080 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
10090 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 he master-journa
100a0 6c 20 66 69 6c 65 20 69 6e 74 6f 20 0a 2a 2a 20 l file into .**
100b0 6d 65 6d 6f 72 79 20 61 6e 64 20 6c 6f 6f 70 73 memory and loops
100c0 20 74 68 72 6f 75 67 68 20 65 61 63 68 20 6f 66 through each of
100d0 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e the child journ
100e0 61 6c 20 6e 61 6d 65 73 2e 20 46 6f 72 0a 2a 2a al names. For.**
100f0 20 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 each child jour
10100 6e 61 6c 2c 20 69 74 20 63 68 65 63 6b 73 20 69 nal, it checks i
10110 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 69 66 20 f:.**.** * if
10120 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 the child journa
10130 6c 20 65 78 69 73 74 73 2c 20 61 6e 64 20 69 66 l exists, and if
10140 20 73 6f 0a 2a 2a 20 20 20 2a 20 69 66 20 74 68 so.** * if th
10150 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 e child journal
10160 63 6f 6e 74 61 69 6e 73 20 61 20 72 65 66 65 72 contains a refer
10170 65 6e 63 65 20 74 6f 20 6d 61 73 74 65 72 20 6a ence to master j
10180 6f 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 66 ournal .** f
10190 69 6c 65 20 7a 4d 61 73 74 65 72 0a 2a 2a 0a 2a ile zMaster.**.*
101a0 2a 20 49 66 20 61 20 63 68 69 6c 64 20 6a 6f 75 * If a child jou
101b0 72 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e rnal can be foun
101c0 64 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 62 d that matches b
101d0 6f 74 68 20 6f 66 20 74 68 65 20 63 72 69 74 65 oth of the crite
101e0 72 69 61 0a 2a 2a 20 61 62 6f 76 65 2c 20 74 68 ria.** above, th
101f0 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
10200 72 6e 73 20 77 69 74 68 6f 75 74 20 64 6f 69 6e rns without doin
10210 67 20 61 6e 79 74 68 69 6e 67 2e 20 4f 74 68 65 g anything. Othe
10220 72 77 69 73 65 2c 20 69 66 0a 2a 2a 20 6e 6f 20 rwise, if.** no
10230 73 75 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e such child journ
10240 61 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c al can be found,
10250 20 66 69 6c 65 20 7a 4d 61 73 74 65 72 20 69 73 file zMaster is
10260 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 0a 2a 2a deleted from.**
10270 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d the file-system
10280 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 using sqlite3Os
10290 44 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 Delete()..**.**
102a0 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 77 If an IO error w
102b0 69 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 74 ithin this funct
102c0 69 6f 6e 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f ion, an error co
102d0 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 de is returned.
102e0 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e This.** function
102f0 20 61 6c 6c 6f 63 61 74 65 73 20 6d 65 6d 6f 72 allocates memor
10300 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c y by calling sql
10310 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 20 49 66 ite3Malloc(). If
10320 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a an allocation.*
10330 2a 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f * fails, SQLITE_
10340 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 NOMEM is returne
10350 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 d. Otherwise, if
10360 20 6e 6f 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 no IO or malloc
10370 20 65 72 72 6f 72 73 20 0a 2a 2a 20 6f 63 63 75 errors .** occu
10380 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 r, SQLITE_OK is
10390 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
103a0 54 4f 44 4f 3a 20 54 68 69 73 20 66 75 6e 63 74 TODO: This funct
103b0 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 ion allocates a
103c0 73 69 6e 67 6c 65 20 62 6c 6f 63 6b 20 6f 66 20 single block of
103d0 6d 65 6d 6f 72 79 20 74 6f 20 6c 6f 61 64 0a 2a memory to load.*
103e0 2a 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e * the entire con
103f0 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6d 61 73 tents of the mas
10400 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
10410 2e 20 54 68 69 73 20 63 6f 75 6c 64 20 62 65 0a . This could be.
10420 2a 2a 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 6b ** a couple of k
10430 69 6c 6f 62 79 74 65 73 20 6f 72 20 73 6f 20 2d ilobytes or so -
10440 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 6c 61 72 potentially lar
10450 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 61 67 ger than the pag
10460 65 20 0a 2a 2a 20 73 69 7a 65 2e 0a 2a 2f 0a 73 e .** size..*/.s
10470 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f tatic int pager_
10480 64 65 6c 6d 61 73 74 65 72 28 50 61 67 65 72 20 delmaster(Pager
10490 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 *pPager, const c
104a0 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 har *zMaster){.
104b0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
104c0 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 fs = pPager->pVf
104d0 73 3b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 s;. int rc;
104e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
104f0 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
10500 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
10510 2a 70 4d 61 73 74 65 72 3b 20 20 20 20 2f 2a 20 *pMaster; /*
10520 4d 61 6c 6c 6f 63 27 64 20 6d 61 73 74 65 72 2d Malloc'd master-
10530 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 journal file des
10540 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c criptor */. sql
10550 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 6f 75 72 ite3_file *pJour
10560 6e 61 6c 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 nal; /* Malloc
10570 27 64 20 63 68 69 6c 64 2d 6a 6f 75 72 6e 61 6c 'd child-journal
10580 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
10590 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 */. char *zMas
105a0 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 20 terJournal = 0;
105b0 2f 2a 20 43 6f 6e 74 65 6e 74 73 20 6f 66 20 6d /* Contents of m
105c0 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
105d0 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6e 4d 61 73 le */. i64 nMas
105e0 74 65 72 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 terJournal;
105f0 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6d 61 73 /* Size of mas
10600 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
10610 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 */.. /* Alloca
10620 74 65 20 73 70 61 63 65 20 66 6f 72 20 62 6f 74 te space for bot
10630 68 20 74 68 65 20 70 4a 6f 75 72 6e 61 6c 20 61 h the pJournal a
10640 6e 64 20 70 4d 61 73 74 65 72 20 66 69 6c 65 20 nd pMaster file
10650 64 65 73 63 72 69 70 74 6f 72 73 2e 0a 20 20 2a descriptors.. *
10660 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c * If successful,
10670 20 6f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 open the master
10680 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f journal file fo
10690 72 20 72 65 61 64 69 6e 67 2e 0a 20 20 2a 2f 0a r reading.. */.
106a0 20 20 70 4d 61 73 74 65 72 20 3d 20 28 73 71 6c pMaster = (sql
106b0 69 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 ite3_file *)sqli
106c0 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 56 te3MallocZero(pV
106d0 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 2a 20 32 fs->szOsFile * 2
106e0 29 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c 20 3d 20 );. pJournal =
106f0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 (sqlite3_file *)
10700 28 28 28 75 38 20 2a 29 70 4d 61 73 74 65 72 29 (((u8 *)pMaster)
10710 20 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c + pVfs->szOsFil
10720 65 29 3b 0a 20 20 69 66 28 20 21 70 4d 61 73 74 e);. if( !pMast
10730 65 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 er ){. rc = S
10740 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
10750 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 else{. const
10760 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 4c int flags = (SQL
10770 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
10780 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 Y|SQLITE_OPEN_MA
10790 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 STER_JOURNAL);.
107a0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
107b0 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61 73 sOpen(pVfs, zMas
107c0 74 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66 6c ter, pMaster, fl
107d0 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 ags, 0);. }. i
107e0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
107f0 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 ) goto delmaste
10800 72 5f 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20 73 r_out;.. rc = s
10810 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 qlite3OsFileSize
10820 28 70 4d 61 73 74 65 72 2c 20 26 6e 4d 61 73 74 (pMaster, &nMast
10830 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 69 66 erJournal);. if
10840 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
10850 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 ) goto delmaster
10860 5f 6f 75 74 3b 0a 0a 20 20 69 66 28 20 6e 4d 61 _out;.. if( nMa
10870 73 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 20 29 7b sterJournal>0 ){
10880 0a 20 20 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 . char *zJour
10890 6e 61 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a nal;. char *z
108a0 4d 61 73 74 65 72 50 74 72 20 3d 20 30 3b 0a 20 MasterPtr = 0;.
108b0 20 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 50 74 int nMasterPt
108c0 72 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 r = pVfs->mxPath
108d0 6e 61 6d 65 2b 31 3b 0a 0a 20 20 20 20 2f 2a 20 name+1;.. /*
108e0 4c 6f 61 64 20 74 68 65 20 65 6e 74 69 72 65 20 Load the entire
108f0 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
10900 69 6c 65 20 69 6e 74 6f 20 73 70 61 63 65 20 6f ile into space o
10910 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 20 20 20 btained from.
10920 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c ** sqlite3_mall
10930 6f 63 28 29 20 61 6e 64 20 70 6f 69 6e 74 65 64 oc() and pointed
10940 20 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 4a 6f to by zMasterJo
10950 75 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2f 0a 20 urnal. . */.
10960 20 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 zMasterJourna
10970 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f l = sqlite3Mallo
10980 63 28 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f c((int)nMasterJo
10990 75 72 6e 61 6c 20 2b 20 6e 4d 61 73 74 65 72 50 urnal + nMasterP
109a0 74 72 20 2b 20 31 29 3b 0a 20 20 20 20 69 66 28 tr + 1);. if(
109b0 20 21 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c !zMasterJournal
109c0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
109d0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
109e0 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 goto delmaste
109f0 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 r_out;. }.
10a00 20 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 26 7a zMasterPtr = &z
10a10 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d MasterJournal[nM
10a20 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2b 31 5d 3b asterJournal+1];
10a30 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
10a40 33 4f 73 52 65 61 64 28 70 4d 61 73 74 65 72 2c 3OsRead(pMaster,
10a50 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c zMasterJournal,
10a60 20 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 (int)nMasterJou
10a70 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 69 66 rnal, 0);. if
10a80 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
10a90 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 ) goto delmaster
10aa0 5f 6f 75 74 3b 0a 20 20 20 20 7a 4d 61 73 74 65 _out;. zMaste
10ab0 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 rJournal[nMaster
10ac0 4a 6f 75 72 6e 61 6c 5d 20 3d 20 30 3b 0a 0a 20 Journal] = 0;..
10ad0 20 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4d zJournal = zM
10ae0 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a 20 20 asterJournal;.
10af0 20 20 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72 6e while( (zJourn
10b00 61 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 al-zMasterJourna
10b10 6c 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 l)<nMasterJourna
10b20 6c 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 l ){. int e
10b30 78 69 73 74 73 3b 0a 20 20 20 20 20 20 72 63 20 xists;. rc
10b40 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 = sqlite3OsAcces
10b50 73 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c s(pVfs, zJournal
10b60 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f , SQLITE_ACCESS_
10b70 45 58 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 EXISTS, &exists)
10b80 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
10b90 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
10ba0 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 goto delmas
10bb0 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d ter_out;. }
10bc0 0a 20 20 20 20 20 20 69 66 28 20 65 78 69 73 74 . if( exist
10bd0 73 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 s ){. /*
10be0 4f 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e One of the journ
10bf0 61 6c 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 als pointed to b
10c00 79 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 y the master jou
10c10 72 6e 61 6c 20 65 78 69 73 74 73 2e 0a 20 20 20 rnal exists..
10c20 20 20 20 20 20 2a 2a 20 4f 70 65 6e 20 69 74 20 ** Open it
10c30 61 6e 64 20 63 68 65 63 6b 20 69 66 20 69 74 20 and check if it
10c40 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6d 61 points at the ma
10c50 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 ster journal. If
10c60 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20 . ** so,
10c70 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 return without d
10c80 65 6c 65 74 69 6e 67 20 74 68 65 20 6d 61 73 74 eleting the mast
10c90 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e er journal file.
10ca0 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
10cb0 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 int c;.
10cc0 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 int flags = (
10cd0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
10ce0 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e ONLY|SQLITE_OPEN
10cf0 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 3b 0a _MAIN_JOURNAL);.
10d00 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
10d10 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c ite3OsOpen(pVfs,
10d20 20 7a 4a 6f 75 72 6e 61 6c 2c 20 70 4a 6f 75 72 zJournal, pJour
10d30 6e 61 6c 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a nal, flags, 0);.
10d40 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
10d50 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
10d60 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d goto delm
10d70 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 aster_out;.
10d80 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 72 63 }.. rc
10d90 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 = readMasterJou
10da0 72 6e 61 6c 28 70 4a 6f 75 72 6e 61 6c 2c 20 7a rnal(pJournal, z
10db0 4d 61 73 74 65 72 50 74 72 2c 20 6e 4d 61 73 74 MasterPtr, nMast
10dc0 65 72 50 74 72 29 3b 0a 20 20 20 20 20 20 20 20 erPtr);.
10dd0 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 sqlite3OsClose(p
10de0 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 Journal);.
10df0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
10e00 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
10e10 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f goto delmaster_
10e20 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a out;. }..
10e30 20 20 20 20 20 20 20 20 63 20 3d 20 7a 4d 61 73 c = zMas
10e40 74 65 72 50 74 72 5b 30 5d 21 3d 30 20 26 26 20 terPtr[0]!=0 &&
10e50 73 74 72 63 6d 70 28 7a 4d 61 73 74 65 72 50 74 strcmp(zMasterPt
10e60 72 2c 20 7a 4d 61 73 74 65 72 29 3d 3d 30 3b 0a r, zMaster)==0;.
10e70 20 20 20 20 20 20 20 20 69 66 28 20 63 20 29 7b if( c ){
10e80 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 65 . /* We
10e90 20 68 61 76 65 20 61 20 6d 61 74 63 68 2e 20 44 have a match. D
10ea0 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 o not delete the
10eb0 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
10ec0 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 file. */.
10ed0 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 goto delmaste
10ee0 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d r_out;. }
10ef0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a . }. z
10f00 4a 6f 75 72 6e 61 6c 20 2b 3d 20 28 73 71 6c 69 Journal += (sqli
10f10 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4a 6f 75 te3Strlen30(zJou
10f20 72 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d 0a rnal)+1);. }.
10f30 20 20 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73 71 }. . rc = sq
10f40 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 lite3OsDelete(pV
10f50 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b fs, zMaster, 0);
10f60 0a 0a 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3a ..delmaster_out:
10f70 0a 20 20 69 66 28 20 7a 4d 61 73 74 65 72 4a 6f . if( zMasterJo
10f80 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c urnal ){. sql
10f90 69 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 ite3_free(zMaste
10fa0 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20 20 rJournal);. }
10fb0 0a 20 20 69 66 28 20 70 4d 61 73 74 65 72 20 29 . if( pMaster )
10fc0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 {. sqlite3OsC
10fd0 6c 6f 73 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 lose(pMaster);.
10fe0 20 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 assert( !isOp
10ff0 65 6e 28 70 4a 6f 75 72 6e 61 6c 29 20 29 3b 0a en(pJournal) );.
11000 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 }. sqlite3_fr
11010 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 ee(pMaster);. r
11020 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
11030 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
11040 6e 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 n is used to cha
11050 6e 67 65 20 74 68 65 20 61 63 74 75 61 6c 20 73 nge the actual s
11060 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ize of the datab
11070 61 73 65 20 0a 2a 2a 20 66 69 6c 65 20 69 6e 20 ase .** file in
11080 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e the file-system.
11090 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 This only happe
110a0 6e 73 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 ns when committi
110b0 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e ng a transaction
110c0 2c 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 ,.** or rolling
110d0 62 61 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 back a transacti
110e0 6f 6e 20 28 69 6e 63 6c 75 64 69 6e 67 20 72 6f on (including ro
110f0 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74 lling back a hot
11100 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2a 0a 2a 2a -journal)..**.**
11110 20 49 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 If the main dat
11120 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6e 6f abase file is no
11130 74 20 6f 70 65 6e 2c 20 6f 72 20 61 6e 20 65 78 t open, or an ex
11140 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 clusive lock is
11150 6e 6f 74 0a 2a 2a 20 68 65 6c 64 2c 20 74 68 69 not.** held, thi
11160 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 s function is a
11170 6e 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 no-op. Otherwise
11180 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 , the size of th
11190 65 20 66 69 6c 65 20 69 73 0a 2a 2a 20 63 68 61 e file is.** cha
111a0 6e 67 65 64 20 74 6f 20 6e 50 61 67 65 20 70 61 nged to nPage pa
111b0 67 65 73 20 28 6e 50 61 67 65 2a 70 50 61 67 65 ges (nPage*pPage
111c0 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 r->pageSize byte
111d0 73 29 2e 20 49 66 20 74 68 65 20 66 69 6c 65 0a s). If the file.
111e0 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 63 75 ** on disk is cu
111f0 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 rrently larger t
11200 68 61 6e 20 6e 50 61 67 65 20 70 61 67 65 73 2c han nPage pages,
11210 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 56 46 then use the VF
11220 53 0a 2a 2a 20 78 54 72 75 6e 63 61 74 65 28 29 S.** xTruncate()
11230 20 6d 65 74 68 6f 64 20 74 6f 20 74 72 75 6e 63 method to trunc
11240 61 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f 72 ate it..**.** Or
11250 2c 20 69 74 20 6d 69 67 68 74 20 6d 69 67 68 74 , it might might
11260 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 be the case tha
11270 74 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 t the file on di
11280 73 6b 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 sk is smaller th
11290 61 6e 20 0a 2a 2a 20 6e 50 61 67 65 20 70 61 67 an .** nPage pag
112a0 65 73 2e 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 es. Some operati
112b0 6e 67 20 73 79 73 74 65 6d 20 69 6d 70 6c 65 6d ng system implem
112c0 65 6e 74 61 74 69 6f 6e 73 20 63 61 6e 20 67 65 entations can ge
112d0 74 20 63 6f 6e 66 75 73 65 64 20 69 66 20 0a 2a t confused if .*
112e0 2a 20 79 6f 75 20 74 72 79 20 74 6f 20 74 72 75 * you try to tru
112f0 6e 63 61 74 65 20 61 20 66 69 6c 65 20 74 6f 20 ncate a file to
11300 73 6f 6d 65 20 73 69 7a 65 20 74 68 61 74 20 69 some size that i
11310 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 74 s larger than it
11320 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 69 .** currently i
11330 73 2c 20 73 6f 20 64 65 74 65 63 74 20 74 68 69 s, so detect thi
11340 73 20 63 61 73 65 20 61 6e 64 20 77 72 69 74 65 s case and write
11350 20 61 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20 62 a single zero b
11360 79 74 65 20 74 6f 20 0a 2a 2a 20 74 68 65 20 65 yte to .** the e
11370 6e 64 20 6f 66 20 74 68 65 20 6e 65 77 20 66 69 nd of the new fi
11380 6c 65 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a le instead..**.*
11390 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c * If successful,
113a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
113b0 4b 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f K. If an IO erro
113c0 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6d r occurs while m
113d0 6f 64 69 66 79 69 6e 67 0a 2a 2a 20 74 68 65 20 odifying.** the
113e0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 72 database file, r
113f0 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 eturn the error
11400 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c code to the call
11410 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e er..*/.static in
11420 74 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 t pager_truncate
11430 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
11440 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 69 Pgno nPage){. i
11450 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
11460 4b 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d K;. if( pPager-
11470 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 >state>=PAGER_EX
11480 43 4c 55 53 49 56 45 20 26 26 20 69 73 4f 70 65 CLUSIVE && isOpe
11490 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b n(pPager->fd) ){
114a0 0a 20 20 20 20 69 36 34 20 63 75 72 72 65 6e 74 . i64 current
114b0 53 69 7a 65 2c 20 6e 65 77 53 69 7a 65 3b 0a 20 Size, newSize;.
114c0 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 69 /* TODO: Is i
114d0 74 20 73 61 66 65 20 74 6f 20 75 73 65 20 50 61 t safe to use Pa
114e0 67 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 68 ger.dbFileSize h
114f0 65 72 65 3f 20 2a 2f 0a 20 20 20 20 72 63 20 3d ere? */. rc =
11500 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 sqlite3OsFileSi
11510 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 ze(pPager->fd, &
11520 63 75 72 72 65 6e 74 53 69 7a 65 29 3b 0a 20 20 currentSize);.
11530 20 20 6e 65 77 53 69 7a 65 20 3d 20 70 50 61 67 newSize = pPag
11540 65 72 2d 3e 70 61 67 65 53 69 7a 65 2a 28 69 36 er->pageSize*(i6
11550 34 29 6e 50 61 67 65 3b 0a 20 20 20 20 69 66 28 4)nPage;. if(
11560 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
11570 26 20 63 75 72 72 65 6e 74 53 69 7a 65 21 3d 6e & currentSize!=n
11580 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 ewSize ){.
11590 69 66 28 20 63 75 72 72 65 6e 74 53 69 7a 65 3e if( currentSize>
115a0 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 newSize ){.
115b0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
115c0 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 sTruncate(pPager
115d0 2d 3e 66 64 2c 20 6e 65 77 53 69 7a 65 29 3b 0a ->fd, newSize);.
115e0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
115f0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
11600 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3OsWrite(pPager-
11610 3e 66 64 2c 20 22 22 2c 20 31 2c 20 6e 65 77 53 >fd, "", 1, newS
11620 69 7a 65 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a ize-1);. }.
11630 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
11640 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
11650 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c pPager->dbFil
11660 65 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 eSize = nPage;.
11670 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
11680 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
11690 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 ./*.** Set the v
116a0 61 6c 75 65 20 6f 66 20 74 68 65 20 50 61 67 65 alue of the Page
116b0 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72 r.sectorSize var
116c0 69 61 62 6c 65 20 66 6f 72 20 74 68 65 20 67 69 iable for the gi
116d0 76 65 6e 0a 2a 2a 20 70 61 67 65 72 20 62 61 73 ven.** pager bas
116e0 65 64 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 ed on the value
116f0 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 returned by the
11700 78 53 65 63 74 6f 72 53 69 7a 65 20 6d 65 74 68 xSectorSize meth
11710 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6f 70 65 od.** of the ope
11720 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e n database file.
11730 20 54 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 The sector size
11740 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 75 73 will be used us
11750 65 64 20 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d ed .** to determ
11760 69 6e 65 20 74 68 65 20 73 69 7a 65 20 61 6e 64 ine the size and
11770 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 6a 6f alignment of jo
11780 75 72 6e 61 6c 20 68 65 61 64 65 72 20 61 6e 64 urnal header and
11790 20 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 .** master jour
117a0 6e 61 6c 20 70 6f 69 6e 74 65 72 73 20 77 69 74 nal pointers wit
117b0 68 69 6e 20 63 72 65 61 74 65 64 20 6a 6f 75 72 hin created jour
117c0 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a nal files..**.**
117d0 20 46 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 For temporary f
117e0 69 6c 65 73 20 74 68 65 20 65 66 66 65 63 74 69 iles the effecti
117f0 76 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 ve sector size i
11800 73 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 s always 512 byt
11810 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 es..**.** Otherw
11820 69 73 65 2c 20 66 6f 72 20 6e 6f 6e 2d 74 65 6d ise, for non-tem
11830 70 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 74 68 porary files, th
11840 65 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 e effective sect
11850 6f 72 20 73 69 7a 65 20 69 73 0a 2a 2a 20 74 68 or size is.** th
11860 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
11870 20 62 79 20 74 68 65 20 78 53 65 63 74 6f 72 53 by the xSectorS
11880 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 72 6f 75 ize() method rou
11890 6e 64 65 64 20 75 70 20 74 6f 20 35 31 32 20 69 nded up to 512 i
118a0 66 0a 2a 2a 20 69 74 20 69 73 20 6c 65 73 73 20 f.** it is less
118b0 74 68 61 6e 20 35 31 32 2c 20 6f 72 20 72 6f 75 than 512, or rou
118c0 6e 64 65 64 20 64 6f 77 6e 20 74 6f 20 4d 41 58 nded down to MAX
118d0 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 66 20 _SECTOR_SIZE if
118e0 69 74 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 it.** is greater
118f0 20 74 68 61 6e 20 4d 41 58 5f 53 45 43 54 4f 52 than MAX_SECTOR
11900 5f 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 _SIZE..*/.static
11910 20 76 6f 69 64 20 73 65 74 53 65 63 74 6f 72 53 void setSectorS
11920 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ize(Pager *pPage
11930 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 73 r){. assert( is
11940 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
11950 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 || pPager->temp
11960 46 69 6c 65 20 29 3b 0a 0a 20 20 69 66 28 20 21 File );.. if( !
11970 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
11980 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74 6f ){. /* Secto
11990 72 20 73 69 7a 65 20 64 6f 65 73 6e 27 74 20 6d r size doesn't m
119a0 61 74 74 65 72 20 66 6f 72 20 74 65 6d 70 6f 72 atter for tempor
119b0 61 72 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f 2c ary files. Also,
119c0 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a the file. **
119d0 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 62 65 may not have be
119e0 65 6e 20 6f 70 65 6e 65 64 20 79 65 74 2c 20 69 en opened yet, i
119f0 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 n which case the
11a00 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a OsSectorSize().
11a10 20 20 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c ** call will
11a20 20 73 65 67 66 61 75 6c 74 2e 0a 20 20 20 20 2a segfault.. *
11a30 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 /. pPager->se
11a40 63 74 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 ctorSize = sqlit
11a50 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 70 e3OsSectorSize(p
11a60 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d 0a Pager->fd);. }.
11a70 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 if( pPager->se
11a80 63 74 6f 72 53 69 7a 65 3c 35 31 32 20 29 7b 0a ctorSize<512 ){.
11a90 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 pPager->sect
11aa0 6f 72 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20 orSize = 512;.
11ab0 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e }. if( pPager->
11ac0 73 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 sectorSize>MAX_S
11ad0 45 43 54 4f 52 5f 53 49 5a 45 20 29 7b 0a 20 20 ECTOR_SIZE ){.
11ae0 20 20 61 73 73 65 72 74 28 20 4d 41 58 5f 53 45 assert( MAX_SE
11af0 43 54 4f 52 5f 53 49 5a 45 3e 3d 35 31 32 20 29 CTOR_SIZE>=512 )
11b00 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 ;. pPager->se
11b10 63 74 6f 72 53 69 7a 65 20 3d 20 4d 41 58 5f 53 ctorSize = MAX_S
11b20 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 7d 0a ECTOR_SIZE;. }.
11b30 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 }../*.** Playbac
11b40 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e k the journal an
11b50 64 20 74 68 75 73 20 72 65 73 74 6f 72 65 20 74 d thus restore t
11b60 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
11b70 20 74 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 to.** the state
11b80 20 69 74 20 77 61 73 20 69 6e 20 62 65 66 6f 72 it was in befor
11b90 65 20 77 65 20 73 74 61 72 74 65 64 20 6d 61 6b e we started mak
11ba0 69 6e 67 20 63 68 61 6e 67 65 73 2e 20 20 0a 2a ing changes. .*
11bb0 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c *.** The journal
11bc0 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 69 73 20 file format is
11bd0 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a 0a as follows: .**.
11be0 2a 2a 20 20 28 31 29 20 20 38 20 62 79 74 65 20 ** (1) 8 byte
11bf0 70 72 65 66 69 78 2e 20 20 41 20 63 6f 70 79 20 prefix. A copy
11c00 6f 66 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 of aJournalMagic
11c10 5b 5d 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20 62 []..** (2) 4 b
11c20 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 yte big-endian i
11c30 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 nteger which is
11c40 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 the number of va
11c50 6c 69 64 20 70 61 67 65 20 72 65 63 6f 72 64 73 lid page records
11c60 0a 2a 2a 20 20 20 20 20 20 20 69 6e 20 74 68 65 .** in the
11c70 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74 68 journal. If th
11c80 69 73 20 76 61 6c 75 65 20 69 73 20 30 78 66 66 is value is 0xff
11c90 66 66 66 66 66 66 2c 20 74 68 65 6e 20 63 6f 6d ffffff, then com
11ca0 70 75 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 pute the.**
11cb0 20 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 number of page
11cc0 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 records from th
11cd0 65 20 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e 0a e journal size..
11ce0 2a 2a 20 20 28 33 29 20 20 34 20 62 79 74 65 20 ** (3) 4 byte
11cf0 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 big-endian integ
11d00 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 er which is the
11d10 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 66 6f initial value fo
11d20 72 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 20 r the .**
11d30 73 61 6e 69 74 79 20 63 68 65 63 6b 73 75 6d 2e sanity checksum.
11d40 0a 2a 2a 20 20 28 34 29 20 20 34 20 62 79 74 65 .** (4) 4 byte
11d50 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 integer which i
11d60 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
11d70 70 61 67 65 73 20 74 6f 20 74 72 75 6e 63 61 74 pages to truncat
11d80 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 64 e the.** d
11d90 61 74 61 62 61 73 65 20 74 6f 20 64 75 72 69 6e atabase to durin
11da0 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a g a rollback..**
11db0 20 20 28 35 29 20 20 34 20 62 79 74 65 20 62 69 (5) 4 byte bi
11dc0 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 g-endian integer
11dd0 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73 65 which is the se
11de0 63 74 6f 72 20 73 69 7a 65 2e 20 20 54 68 65 20 ctor size. The
11df0 68 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 20 20 header.**
11e00 69 73 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 is this many byt
11e10 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 20 es in size..**
11e20 28 36 29 20 20 34 20 62 79 74 65 20 62 69 67 2d (6) 4 byte big-
11e30 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 endian integer w
11e40 68 69 63 68 20 69 73 20 74 68 65 20 70 61 67 65 hich is the page
11e50 20 63 61 73 65 2e 0a 2a 2a 20 20 28 37 29 20 20 case..** (7)
11e60 34 20 62 79 74 65 20 69 6e 74 65 67 65 72 20 77 4 byte integer w
11e70 68 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 hich is the numb
11e80 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 er of bytes in t
11e90 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
11ea0 6c 0a 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 2e l.** name.
11eb0 20 20 54 68 65 20 76 61 6c 75 65 20 6d 61 79 20 The value may
11ec0 62 65 20 7a 65 72 6f 20 28 69 6e 64 69 63 61 74 be zero (indicat
11ed0 65 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 e that there is
11ee0 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20 no master.**
11ef0 20 20 20 6a 6f 75 72 6e 61 6c 2e 29 0a 2a 2a 20 journal.).**
11f00 20 28 38 29 20 20 4e 20 62 79 74 65 73 20 6f 66 (8) N bytes of
11f10 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
11f20 6e 61 6c 20 6e 61 6d 65 2e 20 20 54 68 65 20 6e nal name. The n
11f30 61 6d 65 20 77 69 6c 6c 20 62 65 20 6e 75 6c 2d ame will be nul-
11f40 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 20 terminated.**
11f50 20 20 20 20 61 6e 64 20 6d 69 67 68 74 20 62 65 and might be
11f60 20 73 68 6f 72 74 65 72 20 74 68 61 6e 20 74 68 shorter than th
11f70 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f e value read fro
11f80 6d 20 28 35 29 2e 20 20 49 66 20 74 68 65 20 66 m (5). If the f
11f90 69 72 73 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 irst byte.**
11fa0 20 20 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 of the name i
11fb0 73 20 5c 30 30 30 20 74 68 65 6e 20 74 68 65 72 s \000 then ther
11fc0 65 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 20 6a e is no master j
11fd0 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61 73 ournal. The mas
11fe0 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 6a 6f 75 ter.** jou
11ff0 72 6e 61 6c 20 6e 61 6d 65 20 69 73 20 73 74 6f rnal name is sto
12000 72 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a 2a red in UTF-8..**
12010 20 20 28 39 29 20 20 5a 65 72 6f 20 6f 72 20 6d (9) Zero or m
12020 6f 72 65 20 70 61 67 65 73 20 69 6e 73 74 61 6e ore pages instan
12030 63 65 73 2c 20 65 61 63 68 20 61 73 20 66 6f 6c ces, each as fol
12040 6c 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 lows:.**
12050 2b 20 20 34 20 62 79 74 65 20 70 61 67 65 20 6e + 4 byte page n
12060 75 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 20 20 umber..**
12070 20 2b 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65 + pPager->page
12080 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64 61 Size bytes of da
12090 74 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 ta..** +
120a0 20 34 20 62 79 74 65 20 63 68 65 63 6b 73 75 6d 4 byte checksum
120b0 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 20 73 .**.** When we s
120c0 70 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 peak of the jour
120d0 6e 61 6c 20 68 65 61 64 65 72 2c 20 77 65 20 6d nal header, we m
120e0 65 61 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 ean the first 8
120f0 69 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 items above..**
12100 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74 68 Each entry in th
12110 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e 20 e journal is an
12120 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
12130 39 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 9th item..**.**
12140 43 61 6c 6c 20 74 68 65 20 76 61 6c 75 65 20 66 Call the value f
12150 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 20 62 rom the second b
12160 75 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20 20 6e ullet "nRec". n
12170 52 65 63 20 69 73 20 74 68 65 20 6e 75 6d 62 65 Rec is the numbe
12180 72 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 70 61 r of.** valid pa
12190 67 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 ge entries in th
121a0 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20 6d e journal. In m
121b0 6f 73 74 20 63 61 73 65 73 2c 20 79 6f 75 20 63 ost cases, you c
121c0 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a an compute the.*
121d0 2a 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 * value of nRec
121e0 66 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f 66 from the size of
121f0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
12200 65 2e 20 20 42 75 74 20 69 66 20 61 20 70 6f 77 e. But if a pow
12210 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 63 er.** failure oc
12220 63 75 72 72 65 64 20 77 68 69 6c 65 20 74 68 65 curred while the
12230 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 69 journal was bei
12240 6e 67 20 77 72 69 74 74 65 6e 2c 20 69 74 20 63 ng written, it c
12250 6f 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a 20 63 ould be the.** c
12260 61 73 65 20 74 68 61 74 20 74 68 65 20 73 69 7a ase that the siz
12270 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c e of the journal
12280 20 66 69 6c 65 20 68 61 64 20 61 6c 72 65 61 64 file had alread
12290 79 20 62 65 65 6e 20 69 6e 63 72 65 61 73 65 64 y been increased
122a0 20 62 75 74 0a 2a 2a 20 74 68 65 20 65 78 74 72 but.** the extr
122b0 61 20 65 6e 74 72 69 65 73 20 68 61 64 20 6e 6f a entries had no
122c0 74 20 79 65 74 20 6d 61 64 65 20 69 74 20 73 61 t yet made it sa
122d0 66 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20 20 49 fely to disk. I
122e0 6e 20 73 75 63 68 20 61 20 63 61 73 65 2c 0a 2a n such a case,.*
122f0 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e * the value of n
12300 52 65 63 20 63 6f 6d 70 75 74 65 64 20 66 72 6f Rec computed fro
12310 6d 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 m the file size
12320 77 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c 61 72 would be too lar
12330 67 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 61 74 ge. For.** that
12340 20 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c 77 61 reason, we alwa
12350 79 73 20 75 73 65 20 74 68 65 20 6e 52 65 63 20 ys use the nRec
12360 76 61 6c 75 65 20 69 6e 20 74 68 65 20 68 65 61 value in the hea
12370 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 der..**.** If th
12380 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 73 20 e nRec value is
12390 30 78 66 66 66 66 66 66 66 66 20 69 74 20 6d 65 0xffffffff it me
123a0 61 6e 73 20 74 68 61 74 20 6e 52 65 63 20 73 68 ans that nRec sh
123b0 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 ould be computed
123c0 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69 6c .** from the fil
123d0 65 20 73 69 7a 65 2e 20 20 54 68 69 73 20 76 61 e size. This va
123e0 6c 75 65 20 69 73 20 75 73 65 64 20 77 68 65 6e lue is used when
123f0 20 74 68 65 20 75 73 65 72 20 73 65 6c 65 63 74 the user select
12400 73 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e 63 s the.** no-sync
12410 20 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 option for the
12420 6a 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77 65 journal. A powe
12430 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 r failure could
12440 6c 65 61 64 20 74 6f 20 63 6f 72 72 75 70 74 69 lead to corrupti
12450 6f 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 on.** in this ca
12460 73 65 2e 20 20 42 75 74 20 66 6f 72 20 74 68 69 se. But for thi
12470 6e 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72 61 ngs like tempora
12480 72 79 20 74 61 62 6c 65 20 28 77 68 69 63 68 20 ry table (which
12490 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c 65 74 will be.** delet
124a0 65 64 20 77 68 65 6e 20 74 68 65 20 70 6f 77 65 ed when the powe
124b0 72 20 69 73 20 72 65 73 74 6f 72 65 64 29 20 77 r is restored) w
124c0 65 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 20 0a e don't care. .
124d0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6c **.** If the fil
124e0 65 20 6f 70 65 6e 65 64 20 61 73 20 74 68 65 20 e opened as the
124f0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
12500 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 not a well-forme
12510 64 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c d.** journal fil
12520 65 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 e then all pages
12530 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 up to the first
12540 20 63 6f 72 72 75 70 74 65 64 20 70 61 67 65 20 corrupted page
12550 61 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 are rolled.** ba
12560 63 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65 73 20 ck (or no pages
12570 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 if the journal h
12580 65 61 64 65 72 20 69 73 20 63 6f 72 72 75 70 74 eader is corrupt
12590 65 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c ed). The journal
125a0 20 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68 65 6e file.** is then
125b0 20 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51 4c deleted and SQL
125c0 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2c ITE_OK returned,
125d0 20 6a 75 73 74 20 61 73 20 69 66 20 6e 6f 20 63 just as if no c
125e0 6f 72 72 75 70 74 69 6f 6e 20 68 61 64 0a 2a 2a orruption had.**
125f0 20 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72 65 been encountere
12600 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 d..**.** If an I
12610 2f 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 65 /O or malloc() e
12620 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 rror occurs, the
12630 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69 73 journal-file is
12640 20 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a 2a 20 not deleted.**
12650 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 and an error cod
12660 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a e is returned..*
12670 2a 0a 2a 2a 20 54 68 65 20 69 73 48 6f 74 20 70 *.** The isHot p
12680 61 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74 arameter indicat
12690 65 73 20 74 68 61 74 20 77 65 20 61 72 65 20 74 es that we are t
126a0 72 79 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 rying to rollbac
126b0 6b 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 74 k a journal.** t
126c0 68 61 74 20 6d 69 67 68 74 20 62 65 20 61 20 68 hat might be a h
126d0 6f 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 72 2c ot journal. Or,
126e0 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 it could be tha
126f0 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 t the journal is
12700 20 0a 2a 2a 20 70 72 65 73 65 72 76 65 64 20 62 .** preserved b
12710 65 63 61 75 73 65 20 6f 66 20 4a 4f 55 52 4e 41 ecause of JOURNA
12720 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 6f 72 LMODE_PERSIST or
12730 20 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 JOURNALMODE_TRU
12740 4e 43 41 54 45 2e 0a 2a 2a 20 49 66 20 74 68 65 NCATE..** If the
12750 20 6a 6f 75 72 6e 61 6c 20 72 65 61 6c 6c 79 20 journal really
12760 69 73 20 68 6f 74 2c 20 72 65 73 65 74 20 74 68 is hot, reset th
12770 65 20 70 61 67 65 72 20 63 61 63 68 65 20 70 72 e pager cache pr
12780 69 6f 72 20 72 6f 6c 6c 69 6e 67 0a 2a 2a 20 62 ior rolling.** b
12790 61 63 6b 20 61 6e 79 20 63 6f 6e 74 65 6e 74 2e ack any content.
127a0 20 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c If the journal
127b0 20 69 73 20 6d 65 72 65 6c 79 20 70 65 72 73 69 is merely persi
127c0 73 74 65 6e 74 2c 20 6e 6f 20 72 65 73 65 74 20 stent, no reset
127d0 69 73 0a 2a 2a 20 6e 65 65 64 65 64 2e 0a 2a 2f is.** needed..*/
127e0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 .static int page
127f0 72 5f 70 6c 61 79 62 61 63 6b 28 50 61 67 65 72 r_playback(Pager
12800 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69 73 *pPager, int is
12810 48 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f Hot){. sqlite3_
12820 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 vfs *pVfs = pPag
12830 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36 34 20 er->pVfs;. i64
12840 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20 szJ;
12850 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
12860 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
12870 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 in bytes */. u
12880 33 32 20 6e 52 65 63 3b 20 20 20 20 20 20 20 20 32 nRec;
12890 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
128a0 72 20 6f 66 20 52 65 63 6f 72 64 73 20 69 6e 20 r of Records in
128b0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 the journal */.
128c0 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 20 u32 u;
128d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 /* Uns
128e0 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 igned loop count
128f0 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 er */. Pgno mxP
12900 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 g = 0;
12910 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 /* Size of the
12920 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 20 69 6e original file in
12930 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20 pages */. int
12940 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
12950 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 /* Result c
12960 6f 64 65 20 6f 66 20 61 20 73 75 62 72 6f 75 74 ode of a subrout
12970 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 ine */. int res
12980 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 = 1;
12990 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 /* Value retur
129a0 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 4f 73 ned by sqlite3Os
129b0 41 63 63 65 73 73 28 29 20 2a 2f 0a 20 20 63 68 Access() */. ch
129c0 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30 3b ar *zMaster = 0;
129d0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
129e0 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c f master journal
129f0 20 66 69 6c 65 20 69 66 20 61 6e 79 20 2a 2f 0a file if any */.
12a00 20 20 69 6e 74 20 6e 65 65 64 50 61 67 65 72 52 int needPagerR
12a10 65 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 54 72 eset; /* Tr
12a20 75 65 20 74 6f 20 72 65 73 65 74 20 70 61 67 65 ue to reset page
12a30 20 70 72 69 6f 72 20 74 6f 20 66 69 72 73 74 20 prior to first
12a40 70 61 67 65 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f page rollback */
12a50 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 .. /* Figure ou
12a60 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f 72 t how many recor
12a70 64 73 20 61 72 65 20 69 6e 20 74 68 65 20 6a 6f ds are in the jo
12a80 75 72 6e 61 6c 2e 20 20 41 62 6f 72 74 20 65 61 urnal. Abort ea
12a90 72 6c 79 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 rly if. ** the
12aa0 6a 6f 75 72 6e 61 6c 20 69 73 20 65 6d 70 74 79 journal is empty
12ab0 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
12ac0 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
12ad0 6a 66 64 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 jfd) );. rc = s
12ae0 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 qlite3OsFileSize
12af0 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 (pPager->jfd, &s
12b00 7a 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 zJ);. if( rc!=S
12b10 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a 4a 3d QLITE_OK || szJ=
12b20 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 =0 ){. goto e
12b30 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d nd_playback;. }
12b40 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 .. /* Read the
12b50 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
12b60 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 ame from the jou
12b70 72 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20 70 rnal, if it is p
12b80 72 65 73 65 6e 74 2e 0a 20 20 2a 2a 20 49 66 20 resent.. ** If
12b90 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
12ba0 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 73 70 file name is sp
12bb0 65 63 69 66 69 65 64 2c 20 62 75 74 20 74 68 65 ecified, but the
12bc0 20 66 69 6c 65 20 69 73 20 6e 6f 74 0a 20 20 2a file is not. *
12bd0 2a 20 70 72 65 73 65 6e 74 20 6f 6e 20 64 69 73 * present on dis
12be0 6b 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 k, then the jour
12bf0 6e 61 6c 20 69 73 20 6e 6f 74 20 68 6f 74 20 61 nal is not hot a
12c00 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 nd does not need
12c10 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70 6c 61 79 to be. ** play
12c20 65 64 20 62 61 63 6b 2e 0a 20 20 2a 2a 0a 20 20 ed back.. **.
12c30 2a 2a 20 54 4f 44 4f 3a 20 54 65 63 68 6e 69 63 ** TODO: Technic
12c40 61 6c 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 ally the followi
12c50 6e 67 20 69 73 20 61 6e 20 65 72 72 6f 72 20 62 ng is an error b
12c60 65 63 61 75 73 65 20 69 74 20 61 73 73 75 6d 65 ecause it assume
12c70 73 20 74 68 61 74 0a 20 20 2a 2a 20 62 75 66 66 s that. ** buff
12c80 65 72 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 er Pager.pTmpSpa
12c90 63 65 20 69 73 20 28 6d 78 50 61 74 68 6e 61 6d ce is (mxPathnam
12ca0 65 2b 31 29 20 62 79 74 65 73 20 6f 72 20 6c 61 e+1) bytes or la
12cb0 72 67 65 72 2e 20 69 2e 65 2e 20 74 68 61 74 0a rger. i.e. that.
12cc0 20 20 2a 2a 20 28 70 50 61 67 65 72 2d 3e 70 61 ** (pPager->pa
12cd0 67 65 53 69 7a 65 20 3e 3d 20 70 50 61 67 65 72 geSize >= pPager
12ce0 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 ->pVfs->mxPathna
12cf0 6d 65 2b 31 29 2e 20 55 73 69 6e 67 20 6f 73 5f me+1). Using os_
12d00 75 6e 69 78 2e 63 2c 0a 20 20 2a 2a 20 20 6d 78 unix.c,. ** mx
12d10 50 61 74 68 6e 61 6d 65 20 69 73 20 35 31 32 2c Pathname is 512,
12d20 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73 61 which is the sa
12d30 6d 65 20 61 73 20 74 68 65 20 6d 69 6e 69 6d 75 me as the minimu
12d40 6d 20 61 6c 6c 6f 77 61 62 6c 65 20 76 61 6c 75 m allowable valu
12d50 65 0a 20 20 2a 2a 20 66 6f 72 20 70 61 67 65 53 e. ** for pageS
12d60 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 7a 4d 61 73 ize.. */. zMas
12d70 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 ter = pPager->pT
12d80 6d 70 53 70 61 63 65 3b 0a 20 20 72 63 20 3d 20 mpSpace;. rc =
12d90 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 readMasterJourna
12da0 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a l(pPager->jfd, z
12db0 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e Master, pPager->
12dc0 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 pVfs->mxPathname
12dd0 2b 31 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 +1);. if( rc==S
12de0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 QLITE_OK && zMas
12df0 74 65 72 5b 30 5d 20 29 7b 0a 20 20 20 20 72 63 ter[0] ){. rc
12e00 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 = sqlite3OsAcce
12e10 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 ss(pVfs, zMaster
12e20 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f , SQLITE_ACCESS_
12e30 45 58 49 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 EXISTS, &res);.
12e40 20 7d 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 }. zMaster = 0
12e50 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
12e60 54 45 5f 4f 4b 20 7c 7c 20 21 72 65 73 20 29 7b TE_OK || !res ){
12e70 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c . goto end_pl
12e80 61 79 62 61 63 6b 3b 0a 20 20 7d 0a 20 20 70 50 ayback;. }. pP
12e90 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
12ea0 20 3d 20 30 3b 0a 20 20 6e 65 65 64 50 61 67 65 = 0;. needPage
12eb0 72 52 65 73 65 74 20 3d 20 69 73 48 6f 74 3b 0a rReset = isHot;.
12ec0 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 . /* This loop
12ed0 74 65 72 6d 69 6e 61 74 65 73 20 65 69 74 68 65 terminates eithe
12ee0 72 20 77 68 65 6e 20 61 20 72 65 61 64 4a 6f 75 r when a readJou
12ef0 72 6e 61 6c 48 64 72 28 29 20 6f 72 20 0a 20 20 rnalHdr() or .
12f00 2a 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 ** pager_playbac
12f10 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 63 61 6c k_one_page() cal
12f20 6c 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 l returns SQLITE
12f30 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 49 4f 20 65 _DONE or an IO e
12f40 72 72 6f 72 20 0a 20 20 2a 2a 20 6f 63 63 75 72 rror . ** occur
12f50 73 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 s. . */. while
12f60 28 20 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 ( 1 ){. int i
12f70 73 55 6e 73 79 6e 63 20 3d 20 30 3b 0a 0a 20 20 sUnsync = 0;..
12f80 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6e 65 /* Read the ne
12f90 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 xt journal heade
12fa0 72 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e r from the journ
12fb0 61 6c 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 al file. If the
12fc0 72 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e 6f re are. ** no
12fd0 74 20 65 6e 6f 75 67 68 20 62 79 74 65 73 20 6c t enough bytes l
12fe0 65 66 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e eft in the journ
12ff0 61 6c 20 66 69 6c 65 20 66 6f 72 20 61 20 63 6f al file for a co
13000 6d 70 6c 65 74 65 20 68 65 61 64 65 72 2c 20 6f mplete header, o
13010 72 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 63 r. ** it is c
13020 6f 72 72 75 70 74 65 64 2c 20 74 68 65 6e 20 61 orrupted, then a
13030 20 70 72 6f 63 65 73 73 20 6d 75 73 74 20 6f 66 process must of
13040 20 66 61 69 6c 65 64 20 77 68 69 6c 65 20 77 72 failed while wr
13050 69 74 69 6e 67 20 69 74 2e 0a 20 20 20 20 2a 2a iting it.. **
13060 20 54 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 This indicates
13070 6e 6f 74 68 69 6e 67 20 6d 6f 72 65 20 6e 65 65 nothing more nee
13080 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 ds to be rolled
13090 62 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 back.. */.
130a0 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 rc = readJourna
130b0 6c 48 64 72 28 70 50 61 67 65 72 2c 20 69 73 48 lHdr(pPager, isH
130c0 6f 74 2c 20 73 7a 4a 2c 20 26 6e 52 65 63 2c 20 ot, szJ, &nRec,
130d0 26 6d 78 50 67 29 3b 0a 20 20 20 20 69 66 28 20 &mxPg);. if(
130e0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
130f0 20 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d . if( rc==
13100 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 SQLITE_DONE ){.
13110 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
13120 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 TE_OK;. }.
13130 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c goto end_pl
13140 61 79 62 61 63 6b 3b 0a 20 20 20 20 7d 0a 0a 20 ayback;. }..
13150 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 /* If nRec is
13160 20 30 78 66 66 66 66 66 66 66 66 2c 20 74 68 65 0xffffffff, the
13170 6e 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 n this journal w
13180 61 73 20 63 72 65 61 74 65 64 20 62 79 20 61 20 as created by a
13190 70 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a 20 77 process. ** w
131a0 6f 72 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e orking in no-syn
131b0 63 20 6d 6f 64 65 2e 20 54 68 69 73 20 6d 65 61 c mode. This mea
131c0 6e 73 20 74 68 61 74 20 74 68 65 20 72 65 73 74 ns that the rest
131d0 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a of the journal.
131e0 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 6f 6e 73 ** file cons
131f0 69 73 74 73 20 6f 66 20 70 61 67 65 73 2c 20 74 ists of pages, t
13200 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 here are no more
13210 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 73 journal headers
13220 2e 20 43 6f 6d 70 75 74 65 0a 20 20 20 20 2a 2a . Compute. **
13230 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 the value of nR
13240 65 63 20 62 61 73 65 64 20 6f 6e 20 74 68 69 73 ec based on this
13250 20 61 73 73 75 6d 70 74 69 6f 6e 2e 0a 20 20 20 assumption..
13260 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 63 */. if( nRec
13270 3d 3d 30 78 66 66 66 66 66 66 66 66 20 29 7b 0a ==0xffffffff ){.
13280 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
13290 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
132a0 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a ==JOURNAL_HDR_SZ
132b0 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 (pPager) );.
132c0 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 28 nRec = (int)((
132d0 73 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c 5f 48 44 szJ - JOURNAL_HD
132e0 52 5f 53 5a 28 70 50 61 67 65 72 29 29 2f 4a 4f R_SZ(pPager))/JO
132f0 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 URNAL_PG_SZ(pPag
13300 65 72 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 er));. }..
13310 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20 30 /* If nRec is 0
13320 20 61 6e 64 20 74 68 69 73 20 72 6f 6c 6c 62 61 and this rollba
13330 63 6b 20 69 73 20 6f 66 20 61 20 74 72 61 6e 73 ck is of a trans
13340 61 63 74 69 6f 6e 20 63 72 65 61 74 65 64 20 62 action created b
13350 79 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 70 72 y this. ** pr
13360 6f 63 65 73 73 20 61 6e 64 20 69 66 20 74 68 69 ocess and if thi
13370 73 20 69 73 20 74 68 65 20 66 69 6e 61 6c 20 68 s is the final h
13380 65 61 64 65 72 20 69 6e 20 74 68 65 20 6a 6f 75 eader in the jou
13390 72 6e 61 6c 2c 20 74 68 65 6e 20 69 74 20 6d 65 rnal, then it me
133a0 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 ans. ** that
133b0 74 68 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 this part of the
133c0 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 69 journal was bei
133d0 6e 67 20 66 69 6c 6c 65 64 20 62 75 74 20 68 61 ng filled but ha
133e0 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 0a 20 s not yet been.
133f0 20 20 20 2a 2a 20 73 79 6e 63 65 64 20 74 6f 20 ** synced to
13400 64 69 73 6b 2e 20 20 43 6f 6d 70 75 74 65 20 74 disk. Compute t
13410 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
13420 65 73 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 es based on the
13430 72 65 6d 61 69 6e 69 6e 67 0a 20 20 20 20 2a 2a remaining. **
13440 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c size of the fil
13450 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a e.. **. **
13460 20 54 68 65 20 74 68 69 72 64 20 74 65 72 6d 20 The third term
13470 6f 66 20 74 68 65 20 74 65 73 74 20 77 61 73 20 of the test was
13480 61 64 64 65 64 20 74 6f 20 66 69 78 20 74 69 63 added to fix tic
13490 6b 65 74 20 23 32 35 36 35 2e 0a 20 20 20 20 2a ket #2565.. *
134a0 2a 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 * When rolling b
134b0 61 63 6b 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 ack a hot journa
134c0 6c 2c 20 6e 52 65 63 3d 3d 30 20 61 6c 77 61 79 l, nRec==0 alway
134d0 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 s means that the
134e0 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20 63 68 75 next. ** chu
134f0 6e 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 nk of the journa
13500 6c 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72 6f 20 l contains zero
13510 70 61 67 65 73 20 74 6f 20 62 65 20 72 6f 6c 6c pages to be roll
13520 65 64 20 62 61 63 6b 2e 20 20 42 75 74 0a 20 20 ed back. But.
13530 20 20 2a 2a 20 77 68 65 6e 20 64 6f 69 6e 67 20 ** when doing
13540 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 20 74 a ROLLBACK and t
13550 68 65 20 6e 52 65 63 3d 3d 30 20 63 68 75 6e 6b he nRec==0 chunk
13560 20 69 73 20 74 68 65 20 6c 61 73 74 20 63 68 75 is the last chu
13570 6e 6b 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 nk in. ** the
13580 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 6d 65 61 journal, it mea
13590 6e 73 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 ns that the jour
135a0 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 nal might contai
135b0 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 20 20 20 n additional.
135c0 20 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20 6e ** pages that n
135d0 65 65 64 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 eed to be rolled
135e0 20 62 61 63 6b 20 61 6e 64 20 74 68 61 74 20 74 back and that t
135f0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
13600 65 73 20 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c es . ** shoul
13610 64 20 62 65 20 63 6f 6d 70 75 74 65 64 20 62 61 d be computed ba
13620 73 65 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e sed on the journ
13630 61 6c 20 66 69 6c 65 20 73 69 7a 65 2e 0a 20 20 al file size..
13640 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 */. if( nRe
13650 63 3d 3d 30 20 26 26 20 21 69 73 48 6f 74 20 26 c==0 && !isHot &
13660 26 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 &. pPager
13670 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 ->journalHdr+JOU
13680 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
13690 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 er)==pPager->jou
136a0 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 20 rnalOff ){.
136b0 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 28 73 nRec = (int)((s
136c0 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 zJ - pPager->jou
136d0 72 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e rnalOff) / JOURN
136e0 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 AL_PG_SZ(pPager)
136f0 29 3b 0a 20 20 20 20 20 20 69 73 55 6e 73 79 6e );. isUnsyn
13700 63 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 c = 1;. }..
13710 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
13720 74 68 65 20 66 69 72 73 74 20 68 65 61 64 65 72 the first header
13730 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a read from the j
13740 6f 75 72 6e 61 6c 2c 20 74 72 75 6e 63 61 74 65 ournal, truncate
13750 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 the. ** data
13760 62 61 73 65 20 66 69 6c 65 20 62 61 63 6b 20 74 base file back t
13770 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 o its original s
13780 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ize.. */.
13790 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 if( pPager->jour
137a0 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f nalOff==JOURNAL_
137b0 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 HDR_SZ(pPager) )
137c0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 {. rc = pag
137d0 65 72 5f 74 72 75 6e 63 61 74 65 28 70 50 61 67 er_truncate(pPag
137e0 65 72 2c 20 6d 78 50 67 29 3b 0a 20 20 20 20 20 er, mxPg);.
137f0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
13800 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
13810 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b to end_playback;
13820 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
13830 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 Pager->dbSize =
13840 6d 78 50 67 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 mxPg;. }..
13850 20 2f 2a 20 43 6f 70 79 20 6f 72 69 67 69 6e 61 /* Copy origina
13860 6c 20 70 61 67 65 73 20 6f 75 74 20 6f 66 20 74 l pages out of t
13870 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 62 he journal and b
13880 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 ack into the .
13890 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 ** database fi
138a0 6c 65 20 61 6e 64 2f 6f 72 20 70 61 67 65 20 63 le and/or page c
138b0 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ache.. */.
138c0 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6e 52 65 63 for(u=0; u<nRec
138d0 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; u++){. if
138e0 28 20 6e 65 65 64 50 61 67 65 72 52 65 73 65 74 ( needPagerReset
138f0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 ){. page
13900 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b r_reset(pPager);
13910 0a 20 20 20 20 20 20 20 20 6e 65 65 64 50 61 67 . needPag
13920 65 72 52 65 73 65 74 20 3d 20 30 3b 0a 20 20 20 erReset = 0;.
13930 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
13940 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f pager_playback_o
13950 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c 31 ne_page(pPager,1
13960 2c 69 73 55 6e 73 79 6e 63 2c 26 70 50 61 67 65 ,isUnsync,&pPage
13970 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 30 2c r->journalOff,0,
13980 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 0);. if( rc
13990 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
139a0 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
139b0 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 QLITE_DONE ){.
139c0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
139d0 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 ITE_OK;.
139e0 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
139f0 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 20 20 lOff = szJ;.
13a00 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
13a10 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
13a20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 /* If we a
13a30 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 72 6f 6c re unable to rol
13a40 6c 62 61 63 6b 2c 20 71 75 69 74 20 61 6e 64 20 lback, quit and
13a50 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 return the error
13a60 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f . ** co
13a70 64 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 63 de. This will c
13a80 61 75 73 65 20 74 68 65 20 70 61 67 65 72 20 74 ause the pager t
13a90 6f 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f o enter the erro
13aa0 72 20 73 74 61 74 65 0a 20 20 20 20 20 20 20 20 r state.
13ab0 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 6e 6f 20 ** so that no
13ac0 66 75 72 74 68 65 72 20 68 61 72 6d 20 77 69 6c further harm wil
13ad0 6c 20 62 65 20 64 6f 6e 65 2e 20 20 50 65 72 68 l be done. Perh
13ae0 61 70 73 20 74 68 65 20 6e 65 78 74 0a 20 20 20 aps the next.
13af0 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 ** proces
13b00 73 20 74 6f 20 63 6f 6d 65 20 61 6c 6f 6e 67 20 s to come along
13b10 77 69 6c 6c 20 62 65 20 61 62 6c 65 20 74 6f 20 will be able to
13b20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 64 61 74 rollback the dat
13b30 61 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20 abase..
13b40 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 67 6f */. go
13b50 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b to end_playback;
13b60 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
13b70 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f }. }. }. /
13b80 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 *NOTREACHED*/.
13b90 61 73 73 65 72 74 28 20 30 20 29 3b 0a 0a 65 6e assert( 0 );..en
13ba0 64 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 2f 2a d_playback:. /*
13bb0 20 46 6f 6c 6c 6f 77 69 6e 67 20 61 20 72 6f 6c Following a rol
13bc0 6c 62 61 63 6b 2c 20 74 68 65 20 64 61 74 61 62 lback, the datab
13bd0 61 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 ase file should
13be0 62 65 20 62 61 63 6b 20 69 6e 20 69 74 73 20 6f be back in its o
13bf0 72 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 73 74 61 riginal. ** sta
13c00 74 65 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 te prior to the
13c10 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 start of the tra
13c20 6e 73 61 63 74 69 6f 6e 2c 20 73 6f 20 69 6e 76 nsaction, so inv
13c30 6f 6b 65 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c oke the. ** SQL
13c40 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 ITE_FCNTL_DB_UNC
13c50 48 41 4e 47 45 44 20 66 69 6c 65 2d 63 6f 6e 74 HANGED file-cont
13c60 72 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20 64 69 rol method to di
13c70 73 61 62 6c 65 20 74 68 65 0a 20 20 2a 2a 20 61 sable the. ** a
13c80 73 73 65 72 74 69 6f 6e 20 74 68 61 74 20 74 68 ssertion that th
13c90 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f e transaction co
13ca0 75 6e 74 65 72 20 77 61 73 20 6d 6f 64 69 66 69 unter was modifi
13cb0 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ed.. */. asser
13cc0 74 28 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 66 t(. pPager->f
13cd0 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 20 7c d->pMethods==0 |
13ce0 7c 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 |. sqlite3OsF
13cf0 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 61 67 65 ileControl(pPage
13d00 72 2d 3e 66 64 2c 53 51 4c 49 54 45 5f 46 43 4e r->fd,SQLITE_FCN
13d10 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 2c TL_DB_UNCHANGED,
13d20 30 29 3e 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 0)>=SQLITE_OK.
13d30 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 );.. /* If this
13d40 20 70 6c 61 79 62 61 63 6b 20 69 73 20 68 61 70 playback is hap
13d50 70 65 6e 69 6e 67 20 61 75 74 6f 6d 61 74 69 63 pening automatic
13d60 61 6c 6c 79 20 61 73 20 61 20 72 65 73 75 6c 74 ally as a result
13d70 20 6f 66 20 61 6e 20 49 4f 20 6f 72 20 0a 20 20 of an IO or .
13d80 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 ** malloc error
13d90 74 68 61 74 20 6f 63 63 75 72 72 65 64 20 61 66 that occurred af
13da0 74 65 72 20 74 68 65 20 63 68 61 6e 67 65 2d 63 ter the change-c
13db0 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64 61 74 ounter was updat
13dc0 65 64 20 62 75 74 20 0a 20 20 2a 2a 20 62 65 66 ed but . ** bef
13dd0 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 ore the transact
13de0 69 6f 6e 20 77 61 73 20 63 6f 6d 6d 69 74 74 65 ion was committe
13df0 64 2c 20 74 68 65 6e 20 74 68 65 20 63 68 61 6e d, then the chan
13e00 67 65 2d 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2a ge-counter . **
13e10 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 6d 61 modification ma
13e20 79 20 6a 75 73 74 20 68 61 76 65 20 62 65 65 6e y just have been
13e30 20 72 65 76 65 72 74 65 64 2e 20 49 66 20 74 68 reverted. If th
13e40 69 73 20 68 61 70 70 65 6e 73 20 69 6e 20 65 78 is happens in ex
13e50 63 6c 75 73 69 76 65 20 0a 20 20 2a 2a 20 6d 6f clusive . ** mo
13e60 64 65 2c 20 74 68 65 6e 20 73 75 62 73 65 71 75 de, then subsequ
13e70 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 ent transactions
13e80 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 performed by th
13e90 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 6c e connection wil
13ea0 6c 20 6e 6f 74 0a 20 20 2a 2a 20 75 70 64 61 74 l not. ** updat
13eb0 65 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 e the change-cou
13ec0 6e 74 65 72 20 61 74 20 61 6c 6c 2e 20 54 68 69 nter at all. Thi
13ed0 73 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 63 61 s may lead to ca
13ee0 63 68 65 20 69 6e 63 6f 6e 73 69 73 74 65 6e 63 che inconsistenc
13ef0 79 0a 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 y. ** problems
13f00 66 6f 72 20 6f 74 68 65 72 20 70 72 6f 63 65 73 for other proces
13f10 73 65 73 20 61 74 20 73 6f 6d 65 20 70 6f 69 6e ses at some poin
13f20 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e t in the future.
13f30 20 53 6f 2c 20 6a 75 73 74 0a 20 20 2a 2a 20 69 So, just. ** i
13f40 6e 20 63 61 73 65 20 74 68 69 73 20 68 61 73 20 n case this has
13f50 68 61 70 70 65 6e 65 64 2c 20 63 6c 65 61 72 20 happened, clear
13f60 74 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 the changeCountD
13f70 6f 6e 65 20 66 6c 61 67 20 6e 6f 77 2e 0a 20 20 one flag now..
13f80 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 63 68 61 */. pPager->cha
13f90 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 ngeCountDone = p
13fa0 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b Pager->tempFile;
13fb0 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 .. if( rc==SQLI
13fc0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 7a 4d 61 TE_OK ){. zMa
13fd0 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 ster = pPager->p
13fe0 54 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 72 63 TmpSpace;. rc
13ff0 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 = readMasterJou
14000 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 rnal(pPager->jfd
14010 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 , zMaster, pPage
14020 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e r->pVfs->mxPathn
14030 61 6d 65 2b 31 29 3b 0a 20 20 20 20 74 65 73 74 ame+1);. test
14040 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 case( rc!=SQLITE
14050 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 _OK );. }. if(
14060 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
14070 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 {. rc = pager
14080 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e _end_transaction
14090 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 (pPager, zMaster
140a0 5b 30 5d 21 3d 27 5c 30 27 29 3b 0a 20 20 20 20 [0]!='\0');.
140b0 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 testcase( rc!=SQ
140c0 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 LITE_OK );. }.
140d0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
140e0 4f 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d OK && zMaster[0]
140f0 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 2f && res ){. /
14100 2a 20 49 66 20 74 68 65 72 65 20 77 61 73 20 61 * If there was a
14110 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
14120 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 and this routine
14130 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 73 75 63 will return suc
14140 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20 73 65 65 cess,. ** see
14150 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 if it is possib
14160 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 le to delete the
14170 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e master journal.
14180 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
14190 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 65 72 pager_delmaster
141a0 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 (pPager, zMaster
141b0 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
141c0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
141d0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 ;. }.. /* The
141e0 50 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 Pager.sectorSize
141f0 20 76 61 72 69 61 62 6c 65 20 6d 61 79 20 68 61 variable may ha
14200 76 65 20 62 65 65 6e 20 75 70 64 61 74 65 64 20 ve been updated
14210 77 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 0a 20 20 while rolling.
14220 2a 2a 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 ** back a journa
14230 6c 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70 l created by a p
14240 72 6f 63 65 73 73 20 77 69 74 68 20 61 20 64 69 rocess with a di
14250 66 66 65 72 65 6e 74 20 73 65 63 74 6f 72 20 73 fferent sector s
14260 69 7a 65 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 ize. ** value.
14270 52 65 73 65 74 20 69 74 20 74 6f 20 74 68 65 20 Reset it to the
14280 63 6f 72 72 65 63 74 20 76 61 6c 75 65 20 66 6f correct value fo
14290 72 20 74 68 69 73 20 70 72 6f 63 65 73 73 2e 0a r this process..
142a0 20 20 2a 2f 0a 20 20 73 65 74 53 65 63 74 6f 72 */. setSector
142b0 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 Size(pPager);.
142c0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
142d0 0a 2a 2a 20 50 6c 61 79 62 61 63 6b 20 73 61 76 .** Playback sav
142e0 65 70 6f 69 6e 74 20 70 53 61 76 65 70 6f 69 6e epoint pSavepoin
142f0 74 2e 20 4f 72 2c 20 69 66 20 70 53 61 76 65 70 t. Or, if pSavep
14300 6f 69 6e 74 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e oint==NULL, then
14310 20 70 6c 61 79 62 61 63 6b 0a 2a 2a 20 74 68 65 playback.** the
14320 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72 20 6a entire master j
14330 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 ournal file. The
14340 20 63 61 73 65 20 70 53 61 76 65 70 6f 69 6e 74 case pSavepoint
14350 3d 3d 4e 55 4c 4c 20 6f 63 63 75 72 73 20 77 68 ==NULL occurs wh
14360 65 6e 20 0a 2a 2a 20 61 20 52 4f 4c 4c 42 41 43 en .** a ROLLBAC
14370 4b 20 54 4f 20 63 6f 6d 6d 61 6e 64 20 69 73 20 K TO command is
14380 69 6e 76 6f 6b 65 64 20 6f 6e 20 61 20 53 41 56 invoked on a SAV
14390 45 50 4f 49 4e 54 20 74 68 61 74 20 69 73 20 61 EPOINT that is a
143a0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a transaction .**
143b0 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a savepoint..**.*
143c0 2a 20 57 68 65 6e 20 70 53 61 76 65 70 6f 69 6e * When pSavepoin
143d0 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 28 6d t is not NULL (m
143e0 65 61 6e 69 6e 67 20 61 20 6e 6f 6e 2d 74 72 61 eaning a non-tra
143f0 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 nsaction savepoi
14400 6e 74 20 69 73 20 0a 2a 2a 20 62 65 69 6e 67 20 nt is .** being
14410 72 6f 6c 6c 65 64 20 62 61 63 6b 29 2c 20 74 68 rolled back), th
14420 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 en the rollback
14430 63 6f 6e 73 69 73 74 73 20 6f 66 20 75 70 20 74 consists of up t
14440 6f 20 74 68 72 65 65 20 73 74 61 67 65 73 2c 0a o three stages,.
14450 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 69 6e 20 ** performed in
14460 74 68 65 20 6f 72 64 65 72 20 73 70 65 63 69 66 the order specif
14470 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 ied:.**.** * P
14480 61 67 65 73 20 61 72 65 20 70 6c 61 79 65 64 20 ages are played
14490 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 back from the ma
144a0 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 in journal start
144b0 69 6e 67 20 61 74 20 62 79 74 65 0a 2a 2a 20 20 ing at byte.**
144c0 20 20 20 6f 66 66 73 65 74 20 50 61 67 65 72 53 offset PagerS
144d0 61 76 65 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 avepoint.iOffset
144e0 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 and continuing
144f0 74 6f 20 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 to .** Pager
14500 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 Savepoint.iHdrOf
14510 66 73 65 74 2c 20 6f 72 20 74 6f 20 74 68 65 20 fset, or to the
14520 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 end of the main
14530 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 66 journal.** f
14540 69 6c 65 20 69 66 20 50 61 67 65 72 53 61 76 65 ile if PagerSave
14550 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 point.iHdrOffset
14560 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 is zero..**.**
14570 20 20 2a 20 49 66 20 50 61 67 65 72 53 61 76 65 * If PagerSave
14580 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 point.iHdrOffset
14590 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 is not zero, th
145a0 65 6e 20 70 61 67 65 73 20 61 72 65 20 70 6c 61 en pages are pla
145b0 79 65 64 0a 2a 2a 20 20 20 20 20 62 61 63 6b 20 yed.** back
145c0 73 74 61 72 74 69 6e 67 20 66 72 6f 6d 20 74 68 starting from th
145d0 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
145e0 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c immediately fol
145f0 6c 6f 77 69 6e 67 20 0a 2a 2a 20 20 20 20 20 50 lowing .** P
14600 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 agerSavepoint.iH
14610 64 72 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 drOffset to the
14620 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 end of the main
14630 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a journal file..**
14640 0a 2a 2a 20 20 20 2a 20 50 61 67 65 73 20 61 72 .** * Pages ar
14650 65 20 74 68 65 6e 20 70 6c 61 79 65 64 20 62 61 e then played ba
14660 63 6b 20 66 72 6f 6d 20 74 68 65 20 73 75 62 2d ck from the sub-
14670 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 73 74 journal file, st
14680 61 72 74 69 6e 67 0a 2a 2a 20 20 20 20 20 77 69 arting.** wi
14690 74 68 20 74 68 65 20 50 61 67 65 72 53 61 76 65 th the PagerSave
146a0 70 6f 69 6e 74 2e 69 53 75 62 52 65 63 20 61 6e point.iSubRec an
146b0 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 d continuing to
146c0 74 68 65 20 65 6e 64 20 6f 66 0a 2a 2a 20 20 20 the end of.**
146d0 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 the journal fi
146e0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 6f 75 67 le..**.** Throug
146f0 68 6f 75 74 20 74 68 65 20 72 6f 6c 6c 62 61 63 hout the rollbac
14700 6b 20 70 72 6f 63 65 73 73 2c 20 65 61 63 68 20 k process, each
14710 74 69 6d 65 20 61 20 70 61 67 65 20 69 73 20 72 time a page is r
14720 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65 0a olled back, the.
14730 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ** corresponding
14740 20 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 61 bit is set in a
14750 20 62 69 74 76 65 63 20 73 74 72 75 63 74 75 72 bitvec structur
14760 65 20 28 76 61 72 69 61 62 6c 65 20 70 44 6f 6e e (variable pDon
14770 65 20 69 6e 20 74 68 65 0a 2a 2a 20 69 6d 70 6c e in the.** impl
14780 65 6d 65 6e 74 61 74 69 6f 6e 20 62 65 6c 6f 77 ementation below
14790 29 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 ). This is used
147a0 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 61 to ensure that a
147b0 20 70 61 67 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a page is only.**
147c0 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 rolled back the
147d0 20 66 69 72 73 74 20 74 69 6d 65 20 69 74 20 69 first time it i
147e0 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 69 6e s encountered in
147f0 20 65 69 74 68 65 72 20 6a 6f 75 72 6e 61 6c 2e either journal.
14800 0a 2a 2a 0a 2a 2a 20 49 66 20 70 53 61 76 65 70 .**.** If pSavep
14810 6f 69 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 oint is NULL, th
14820 65 6e 20 70 61 67 65 73 20 61 72 65 20 6f 6e 6c en pages are onl
14830 79 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66 72 y played back fr
14840 6f 6d 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a om the main.** j
14850 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 ournal file. The
14860 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f re is no need fo
14870 72 20 61 20 62 69 74 76 65 63 20 69 6e 20 74 68 r a bitvec in th
14880 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 is case..**.** I
14890 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c 20 62 n either case, b
148a0 65 66 6f 72 65 20 70 6c 61 79 62 61 63 6b 20 63 efore playback c
148b0 6f 6d 6d 65 6e 63 65 73 20 74 68 65 20 50 61 67 ommences the Pag
148c0 65 72 2e 64 62 53 69 7a 65 20 76 61 72 69 61 62 er.dbSize variab
148d0 6c 65 0a 2a 2a 20 69 73 20 72 65 73 65 74 20 74 le.** is reset t
148e0 6f 20 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 o the value that
148f0 20 69 74 20 68 65 6c 64 20 61 74 20 74 68 65 20 it held at the
14900 73 74 61 72 74 20 6f 66 20 74 68 65 20 73 61 76 start of the sav
14910 65 70 6f 69 6e 74 20 0a 2a 2a 20 28 6f 72 20 74 epoint .** (or t
14920 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 4e 6f 20 ransaction). No
14930 70 61 67 65 20 77 69 74 68 20 61 20 70 61 67 65 page with a page
14940 2d 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 -number greater
14950 74 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65 0a than this value.
14960 2a 2a 20 69 73 20 70 6c 61 79 65 64 20 62 61 63 ** is played bac
14970 6b 2e 20 49 66 20 6f 6e 65 20 69 73 20 65 6e 63 k. If one is enc
14980 6f 75 6e 74 65 72 65 64 20 69 74 20 69 73 20 73 ountered it is s
14990 69 6d 70 6c 79 20 73 6b 69 70 70 65 64 2e 0a 2a imply skipped..*
149a0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
149b0 65 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f erPlaybackSavepo
149c0 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 int(Pager *pPage
149d0 72 2c 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e r, PagerSavepoin
149e0 74 20 2a 70 53 61 76 65 70 6f 69 6e 74 29 7b 0a t *pSavepoint){.
149f0 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 i64 szJ;
14a00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 66 /* Ef
14a10 66 65 63 74 69 76 65 20 73 69 7a 65 20 6f 66 20 fective size of
14a20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c the main journal
14a30 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 */. i64 iHdrOf
14a40 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f f; /
14a50 2a 20 45 6e 64 20 6f 66 20 66 69 72 73 74 20 73 * End of first s
14a60 65 67 6d 65 6e 74 20 6f 66 20 6d 61 69 6e 2d 6a egment of main-j
14a70 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 73 20 2a ournal records *
14a80 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c /. int rc = SQL
14a90 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 ITE_OK; /*
14aa0 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
14ab0 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 3d Bitvec *pDone =
14ac0 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 42 69 74 0; /* Bit
14ad0 76 65 63 20 74 6f 20 65 6e 73 75 72 65 20 70 61 vec to ensure pa
14ae0 67 65 73 20 70 6c 61 79 65 64 20 62 61 63 6b 20 ges played back
14af0 6f 6e 6c 79 20 6f 6e 63 65 20 2a 2f 0a 0a 20 20 only once */..
14b00 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
14b10 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 state>=PAGER_SHA
14b20 52 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c RED );.. /* All
14b30 6f 63 61 74 65 20 61 20 62 69 74 76 65 63 20 74 ocate a bitvec t
14b40 6f 20 75 73 65 20 74 6f 20 73 74 6f 72 65 20 74 o use to store t
14b50 68 65 20 73 65 74 20 6f 66 20 70 61 67 65 73 20 he set of pages
14b60 72 6f 6c 6c 65 64 20 62 61 63 6b 20 2a 2f 0a 20 rolled back */.
14b70 20 69 66 28 20 70 53 61 76 65 70 6f 69 6e 74 20 if( pSavepoint
14b80 29 7b 0a 20 20 20 20 70 44 6f 6e 65 20 3d 20 73 ){. pDone = s
14b90 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 qlite3BitvecCrea
14ba0 74 65 28 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e te(pSavepoint->n
14bb0 4f 72 69 67 29 3b 0a 20 20 20 20 69 66 28 20 21 Orig);. if( !
14bc0 70 44 6f 6e 65 20 29 7b 0a 20 20 20 20 20 20 72 pDone ){. r
14bd0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
14be0 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 EM;. }. }..
14bf0 20 2f 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 /* Set the data
14c00 62 61 73 65 20 73 69 7a 65 20 62 61 63 6b 20 74 base size back t
14c10 6f 20 74 68 65 20 76 61 6c 75 65 20 69 74 20 77 o the value it w
14c20 61 73 20 62 65 66 6f 72 65 20 74 68 65 20 73 61 as before the sa
14c30 76 65 70 6f 69 6e 74 20 0a 20 20 2a 2a 20 62 65 vepoint . ** be
14c40 69 6e 67 20 72 65 76 65 72 74 65 64 20 77 61 73 ing reverted was
14c50 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 opened.. */.
14c60 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d pPager->dbSize =
14c70 20 70 53 61 76 65 70 6f 69 6e 74 20 3f 20 70 53 pSavepoint ? pS
14c80 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67 20 avepoint->nOrig
14c90 3a 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 : pPager->dbOrig
14ca0 53 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 73 65 20 Size;.. /* Use
14cb0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
14cc0 66 66 20 61 73 20 74 68 65 20 65 66 66 65 63 74 ff as the effect
14cd0 69 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 ive size of the
14ce0 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 main rollback.
14cf0 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 ** journal. The
14d00 20 61 63 74 75 61 6c 20 66 69 6c 65 20 6d 69 67 actual file mig
14d10 68 74 20 62 65 20 6c 61 72 67 65 72 20 74 68 61 ht be larger tha
14d20 6e 20 74 68 69 73 20 69 6e 0a 20 20 2a 2a 20 50 n this in. ** P
14d30 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
14d40 5f 54 52 55 4e 43 41 54 45 20 6f 72 20 50 41 47 _TRUNCATE or PAG
14d50 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 ER_JOURNALMODE_P
14d60 45 52 53 49 53 54 2e 20 20 42 75 74 20 61 6e 79 ERSIST. But any
14d70 74 68 69 6e 67 0a 20 20 2a 2a 20 70 61 73 74 20 thing. ** past
14d80 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
14d90 66 66 20 69 73 20 6f 66 66 2d 6c 69 6d 69 74 73 ff is off-limits
14da0 20 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a 20 20 73 to us.. */. s
14db0 7a 4a 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 zJ = pPager->jou
14dc0 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 42 rnalOff;.. /* B
14dd0 65 67 69 6e 20 62 79 20 72 6f 6c 6c 69 6e 67 20 egin by rolling
14de0 62 61 63 6b 20 72 65 63 6f 72 64 73 20 66 72 6f back records fro
14df0 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e m the main journ
14e00 61 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a 20 al starting at.
14e10 20 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f 69 ** PagerSavepoi
14e20 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 20 63 nt.iOffset and c
14e30 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 ontinuing to the
14e40 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 next journal he
14e50 61 64 65 72 2e 0a 20 20 2a 2a 20 54 68 65 72 65 ader.. ** There
14e60 20 6d 69 67 68 74 20 62 65 20 72 65 63 6f 72 64 might be record
14e70 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f s in the main jo
14e80 75 72 6e 61 6c 20 74 68 61 74 20 68 61 76 65 20 urnal that have
14e90 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 a page number.
14ea0 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 ** greater than
14eb0 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 the current data
14ec0 62 61 73 65 20 73 69 7a 65 20 28 70 50 61 67 65 base size (pPage
14ed0 72 2d 3e 64 62 53 69 7a 65 29 20 62 75 74 20 74 r->dbSize) but t
14ee0 68 6f 73 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 hose. ** will b
14ef0 65 20 73 6b 69 70 70 65 64 20 61 75 74 6f 6d 61 e skipped automa
14f00 74 69 63 61 6c 6c 79 2e 20 20 50 61 67 65 73 20 tically. Pages
14f10 61 72 65 20 61 64 64 65 64 20 74 6f 20 70 44 6f are added to pDo
14f20 6e 65 20 61 73 20 74 68 65 79 0a 20 20 2a 2a 20 ne as they. **
14f30 61 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e are played back.
14f40 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 . */. if( pSav
14f50 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69 48 epoint ){. iH
14f60 64 72 4f 66 66 20 3d 20 70 53 61 76 65 70 6f 69 drOff = pSavepoi
14f70 6e 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 20 3f nt->iHdrOffset ?
14f80 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 48 64 pSavepoint->iHd
14f90 72 4f 66 66 73 65 74 20 3a 20 73 7a 4a 3b 0a 20 rOffset : szJ;.
14fa0 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
14fb0 61 6c 4f 66 66 20 3d 20 70 53 61 76 65 70 6f 69 alOff = pSavepoi
14fc0 6e 74 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 nt->iOffset;.
14fd0 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 while( rc==SQLI
14fe0 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d TE_OK && pPager-
14ff0 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 69 48 64 72 >journalOff<iHdr
15000 4f 66 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 Off ){. rc
15010 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b = pager_playback
15020 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 _one_page(pPager
15030 2c 20 31 2c 20 30 2c 20 26 70 50 61 67 65 72 2d , 1, 0, &pPager-
15040 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 >journalOff, 1,
15050 70 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 pDone);. }.
15060 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 assert( rc!=SQ
15070 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d LITE_DONE );. }
15080 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 72 else{. pPager
15090 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 ->journalOff = 0
150a0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 74 ;. }.. /* Cont
150b0 69 6e 75 65 20 72 6f 6c 6c 69 6e 67 20 62 61 63 inue rolling bac
150c0 6b 20 72 65 63 6f 72 64 73 20 6f 75 74 20 6f 66 k records out of
150d0 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 the main journa
150e0 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a 20 20 l starting at.
150f0 2a 2a 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 ** the first jou
15100 72 6e 61 6c 20 68 65 61 64 65 72 20 73 65 65 6e rnal header seen
15110 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 and continuing
15120 75 6e 74 69 6c 20 74 68 65 20 65 66 66 65 63 74 until the effect
15130 69 76 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 ive end. ** of
15140 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c the main journal
15150 20 66 69 6c 65 2e 20 20 43 6f 6e 74 69 6e 75 65 file. Continue
15160 20 74 6f 20 73 6b 69 70 20 6f 75 74 2d 6f 66 2d to skip out-of-
15170 72 61 6e 67 65 20 70 61 67 65 73 20 61 6e 64 0a range pages and.
15180 20 20 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 61 64 ** continue ad
15190 64 69 6e 67 20 70 61 67 65 73 20 72 6f 6c 6c 65 ding pages rolle
151a0 64 20 62 61 63 6b 20 74 6f 20 70 44 6f 6e 65 2e d back to pDone.
151b0 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 72 . */. while( r
151c0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
151d0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
151e0 66 66 3c 73 7a 4a 20 29 7b 0a 20 20 20 20 75 33 ff<szJ ){. u3
151f0 32 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 2 ii;
15200 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
15210 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 4a 52 65 */. u32 nJRe
15220 63 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 75 c = 0; /* Nu
15230 6d 62 65 72 20 6f 66 20 4a 6f 75 72 6e 61 6c 20 mber of Journal
15240 52 65 63 6f 72 64 73 20 2a 2f 0a 20 20 20 20 75 Records */. u
15250 33 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 72 63 32 dummy;. rc
15260 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 = readJournalHd
15270 72 28 70 50 61 67 65 72 2c 20 30 2c 20 73 7a 4a r(pPager, 0, szJ
15280 2c 20 26 6e 4a 52 65 63 2c 20 26 64 75 6d 6d 79 , &nJRec, &dummy
15290 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 );. assert( r
152a0 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 c!=SQLITE_DONE )
152b0 3b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a ;.. /*. **
152c0 20 54 68 65 20 22 70 50 61 67 65 72 2d 3e 6a 6f The "pPager->jo
152d0 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c urnalHdr+JOURNAL
152e0 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d _HDR_SZ(pPager)=
152f0 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c =pPager->journal
15300 4f 66 66 22 0a 20 20 20 20 2a 2a 20 74 65 73 74 Off". ** test
15310 20 69 73 20 72 65 6c 61 74 65 64 20 74 6f 20 74 is related to t
15320 69 63 6b 65 74 20 23 32 35 36 35 2e 20 20 53 65 icket #2565. Se
15330 65 20 74 68 65 20 64 69 73 63 75 73 73 69 6f 6e e the discussion
15340 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 in the. ** p
15350 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 ager_playback()
15360 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 64 64 function for add
15370 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
15380 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ion.. */.
15390 69 66 28 20 6e 4a 52 65 63 3d 3d 30 20 0a 20 20 if( nJRec==0 .
153a0 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f && pPager->jo
153b0 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c urnalHdr+JOURNAL
153c0 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d _HDR_SZ(pPager)=
153d0 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c =pPager->journal
153e0 4f 66 66 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 Off. ){.
153f0 20 6e 4a 52 65 63 20 3d 20 28 75 33 32 29 28 28 nJRec = (u32)((
15400 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f szJ - pPager->jo
15410 75 72 6e 61 6c 4f 66 66 29 2f 4a 4f 55 52 4e 41 urnalOff)/JOURNA
15420 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 L_PG_SZ(pPager))
15430 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 ;. }. for(
15440 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 ii=0; rc==SQLITE
15450 5f 4f 4b 20 26 26 20 69 69 3c 6e 4a 52 65 63 20 _OK && ii<nJRec
15460 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e && pPager->journ
15470 61 6c 4f 66 66 3c 73 7a 4a 3b 20 69 69 2b 2b 29 alOff<szJ; ii++)
15480 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 {. rc = pag
15490 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f er_playback_one_
154a0 70 61 67 65 28 70 50 61 67 65 72 2c 20 31 2c 20 page(pPager, 1,
154b0 30 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 0, &pPager->jour
154c0 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e 65 nalOff, 1, pDone
154d0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 );. }. ass
154e0 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ert( rc!=SQLITE_
154f0 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 20 20 61 73 DONE );. }. as
15500 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
15510 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a _OK || pPager->j
15520 6f 75 72 6e 61 6c 4f 66 66 3d 3d 73 7a 4a 20 29 ournalOff==szJ )
15530 3b 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c ;.. /* Finally,
15540 20 20 72 6f 6c 6c 62 61 63 6b 20 70 61 67 65 73 rollback pages
15550 20 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f from the sub-jo
15560 75 72 6e 61 6c 2e 20 20 50 61 67 65 20 74 68 61 urnal. Page tha
15570 74 20 77 65 72 65 0a 20 20 2a 2a 20 70 72 65 76 t were. ** prev
15580 69 6f 75 73 6c 79 20 72 6f 6c 6c 65 64 20 62 61 iously rolled ba
15590 63 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 ck out of the ma
155a0 69 6e 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 in journal (and
155b0 61 72 65 20 68 65 6e 63 65 20 69 6e 20 70 44 6f are hence in pDo
155c0 6e 65 29 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 ne). ** will be
155d0 20 73 6b 69 70 70 65 64 2e 20 20 4f 75 74 2d 6f skipped. Out-o
155e0 66 2d 72 61 6e 67 65 20 70 61 67 65 73 20 61 72 f-range pages ar
155f0 65 20 61 6c 73 6f 20 73 6b 69 70 70 65 64 2e 0a e also skipped..
15600 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65 */. if( pSave
15610 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 75 33 32 point ){. u32
15620 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 ii;
15630 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
15640 2a 2f 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 */. i64 offse
15650 74 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e t = pSavepoint->
15660 69 53 75 62 52 65 63 2a 28 34 2b 70 50 61 67 65 iSubRec*(4+pPage
15670 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 r->pageSize);.
15680 20 20 66 6f 72 28 69 69 3d 70 53 61 76 65 70 6f for(ii=pSavepo
15690 69 6e 74 2d 3e 69 53 75 62 52 65 63 3b 20 72 63 int->iSubRec; rc
156a0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 ==SQLITE_OK && i
156b0 69 3c 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 i<pPager->nSubRe
156c0 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 c; ii++){.
156d0 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 3d 3d assert( offset==
156e0 69 69 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 ii*(4+pPager->pa
156f0 67 65 53 69 7a 65 29 20 29 3b 0a 20 20 20 20 20 geSize) );.
15700 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 rc = pager_play
15710 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 back_one_page(pP
15720 61 67 65 72 2c 20 30 2c 20 30 2c 20 26 6f 66 66 ager, 0, 0, &off
15730 73 65 74 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a set, 1, pDone);.
15740 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
15750 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e ( rc!=SQLITE_DON
15760 45 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 E );. }.. sqli
15770 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 te3BitvecDestroy
15780 28 70 44 6f 6e 65 29 3b 0a 20 20 69 66 28 20 72 (pDone);. if( r
15790 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
157a0 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
157b0 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 nalOff = szJ;.
157c0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
157d0 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
157e0 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
157f0 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 r of in-memory p
15800 61 67 65 73 20 74 68 61 74 20 61 72 65 20 61 6c ages that are al
15810 6c 6f 77 65 64 2e 0a 2a 2f 0a 76 6f 69 64 20 73 lowed..*/.void s
15820 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 61 qlite3PagerSetCa
15830 63 68 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70 chesize(Pager *p
15840 50 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 Pager, int mxPag
15850 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 e){. sqlite3Pca
15860 63 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 cheSetCachesize(
15870 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c pPager->pPCache,
15880 20 6d 78 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a mxPage);.}../*.
15890 2a 2a 20 41 64 6a 75 73 74 20 74 68 65 20 72 6f ** Adjust the ro
158a0 62 75 73 74 6e 65 73 73 20 6f 66 20 74 68 65 20 bustness of the
158b0 64 61 74 61 62 61 73 65 20 74 6f 20 64 61 6d 61 database to dama
158c0 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72 61 ge due to OS cra
158d0 73 68 65 73 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 shes.** or power
158e0 20 66 61 69 6c 75 72 65 73 20 62 79 20 63 68 61 failures by cha
158f0 6e 67 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 nging the number
15900 20 6f 66 20 73 79 6e 63 73 28 29 73 20 77 68 65 of syncs()s whe
15910 6e 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 n writing.** the
15920 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
15930 6c 2e 20 20 54 68 65 72 65 20 61 72 65 20 74 68 l. There are th
15940 72 65 65 20 6c 65 76 65 6c 73 3a 0a 2a 2a 0a 2a ree levels:.**.*
15950 2a 20 20 20 20 4f 46 46 20 20 20 20 20 20 20 73 * OFF s
15960 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 29 20 69 qlite3OsSync() i
15970 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 2e 20 s never called.
15980 20 54 68 69 73 20 69 73 20 74 68 65 20 64 65 66 This is the def
15990 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 ault.**
159a0 20 20 20 20 20 66 6f 72 20 74 65 6d 70 6f 72 61 for tempora
159b0 72 79 20 61 6e 64 20 74 72 61 6e 73 69 65 6e 74 ry and transient
159c0 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 files..**.**
159d0 20 4e 4f 52 4d 41 4c 20 20 20 20 54 68 65 20 6a NORMAL The j
159e0 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 ournal is synced
159f0 20 6f 6e 63 65 20 62 65 66 6f 72 65 20 77 72 69 once before wri
15a00 74 65 73 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 tes begin on the
15a10 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
15a20 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 database. This
15a30 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 65 is normally ade
15a40 71 75 61 74 65 20 70 72 6f 74 65 63 74 69 6f 6e quate protection
15a50 2c 20 62 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 , but.**
15a60 20 20 20 20 20 20 69 74 20 69 73 20 74 68 65 6f it is theo
15a70 72 65 74 69 63 61 6c 6c 79 20 70 6f 73 73 69 62 retically possib
15a80 6c 65 2c 20 74 68 6f 75 67 68 20 76 65 72 79 20 le, though very
15a90 75 6e 6c 69 6b 65 6c 79 2c 0a 2a 2a 20 20 20 20 unlikely,.**
15aa0 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 61 that a
15ab0 6e 20 69 6e 6f 70 65 72 74 75 6e 65 20 70 6f 77 n inopertune pow
15ac0 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 er failure could
15ad0 20 6c 65 61 76 65 20 74 68 65 20 6a 6f 75 72 6e leave the journ
15ae0 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 al.**
15af0 20 20 20 69 6e 20 61 20 73 74 61 74 65 20 77 68 in a state wh
15b00 69 63 68 20 77 6f 75 6c 64 20 63 61 75 73 65 20 ich would cause
15b10 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61 damage to the da
15b20 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 tabase.**
15b30 20 20 20 20 20 20 20 77 68 65 6e 20 69 74 20 69 when it i
15b40 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a s rolled back..*
15b50 2a 0a 2a 2a 20 20 20 20 46 55 4c 4c 20 20 20 20 *.** FULL
15b60 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 The journal is
15b70 20 73 79 6e 63 65 64 20 74 77 69 63 65 20 62 65 synced twice be
15b80 66 6f 72 65 20 77 72 69 74 65 73 20 62 65 67 69 fore writes begi
15b90 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 n on the.**
15ba0 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 databas
15bb0 65 20 28 77 69 74 68 20 73 6f 6d 65 20 61 64 64 e (with some add
15bc0 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
15bd0 69 6f 6e 20 2d 20 74 68 65 20 6e 52 65 63 20 66 ion - the nRec f
15be0 69 65 6c 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 ield.**
15bf0 20 20 20 20 20 6f 66 20 74 68 65 20 6a 6f 75 72 of the jour
15c00 6e 61 6c 20 68 65 61 64 65 72 20 2d 20 62 65 69 nal header - bei
15c10 6e 67 20 77 72 69 74 74 65 6e 20 69 6e 20 62 65 ng written in be
15c20 74 77 65 65 6e 20 74 68 65 20 74 77 6f 0a 2a 2a tween the two.**
15c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 79 sy
15c40 6e 63 73 29 2e 20 20 49 66 20 77 65 20 61 73 73 ncs). If we ass
15c50 75 6d 65 20 74 68 61 74 20 77 72 69 74 69 6e 67 ume that writing
15c60 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 a.**
15c70 20 20 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 single disk s
15c80 65 63 74 6f 72 20 69 73 20 61 74 6f 6d 69 63 2c ector is atomic,
15c90 20 74 68 65 6e 20 74 68 69 73 20 6d 6f 64 65 20 then this mode
15ca0 70 72 6f 76 69 64 65 73 0a 2a 2a 20 20 20 20 20 provides.**
15cb0 20 20 20 20 20 20 20 20 20 61 73 73 75 72 61 6e assuran
15cc0 63 65 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 ce that the jour
15cd0 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 nal will not be
15ce0 63 6f 72 72 75 70 74 65 64 20 74 6f 20 74 68 65 corrupted to the
15cf0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
15d00 20 70 6f 69 6e 74 20 6f 66 20 63 61 75 73 69 6e point of causin
15d10 67 20 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20 g damage to the
15d20 64 61 74 61 62 61 73 65 20 64 75 72 69 6e 67 20 database during
15d30 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 rollback..**.**
15d40 4e 75 6d 65 72 69 63 20 76 61 6c 75 65 73 20 61 Numeric values a
15d50 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
15d60 68 65 73 65 20 73 74 61 74 65 73 20 61 72 65 20 hese states are
15d70 4f 46 46 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 OFF==1, NORMAL=2
15d80 2c 0a 2a 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2e ,.** and FULL=3.
15d90 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
15da0 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 TE_OMIT_PAGER_PR
15db0 41 47 4d 41 53 0a 76 6f 69 64 20 73 71 6c 69 74 AGMAS.void sqlit
15dc0 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79 e3PagerSetSafety
15dd0 4c 65 76 65 6c 28 50 61 67 65 72 20 2a 70 50 61 Level(Pager *pPa
15de0 67 65 72 2c 20 69 6e 74 20 6c 65 76 65 6c 2c 20 ger, int level,
15df0 69 6e 74 20 62 46 75 6c 6c 46 73 79 6e 63 29 7b int bFullFsync){
15e00 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e . pPager->noSyn
15e10 63 20 3d 20 20 28 6c 65 76 65 6c 3d 3d 31 20 7c c = (level==1 |
15e20 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 | pPager->tempFi
15e30 6c 65 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 le) ?1:0;. pPag
15e40 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 28 er->fullSync = (
15e50 6c 65 76 65 6c 3d 3d 33 20 26 26 20 21 70 50 61 level==3 && !pPa
15e60 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 20 3f ger->tempFile) ?
15e70 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 1:0;. pPager->s
15e80 79 6e 63 5f 66 6c 61 67 73 20 3d 20 28 62 46 75 ync_flags = (bFu
15e90 6c 6c 46 73 79 6e 63 3f 53 51 4c 49 54 45 5f 53 llFsync?SQLITE_S
15ea0 59 4e 43 5f 46 55 4c 4c 3a 53 51 4c 49 54 45 5f YNC_FULL:SQLITE_
15eb0 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 SYNC_NORMAL);.
15ec0 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 if( pPager->noSy
15ed0 6e 63 20 29 20 70 50 61 67 65 72 2d 3e 6e 65 65 nc ) pPager->nee
15ee0 64 53 79 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e dSync = 0;.}.#en
15ef0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 dif../*.** The f
15f00 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 ollowing global
15f10 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 variable is incr
15f20 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 emented whenever
15f30 20 74 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 the library.**
15f40 61 74 74 65 6d 70 74 73 20 74 6f 20 6f 70 65 6e attempts to open
15f50 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c a temporary fil
15f60 65 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 e. This informa
15f70 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72 tion is used for
15f80 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 .** testing and
15f90 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 20 analysis only.
15fa0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
15fb0 45 5f 54 45 53 54 0a 69 6e 74 20 73 71 6c 69 74 E_TEST.int sqlit
15fc0 65 33 5f 6f 70 65 6e 74 65 6d 70 5f 63 6f 75 6e e3_opentemp_coun
15fd0 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f t = 0;.#endif../
15fe0 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 74 65 6d 70 *.** Open a temp
15ff0 6f 72 61 72 79 20 66 69 6c 65 2e 0a 2a 2a 0a 2a orary file..**.*
16000 2a 20 57 72 69 74 65 20 74 68 65 20 66 69 6c 65 * Write the file
16010 20 64 65 73 63 72 69 70 74 6f 72 20 69 6e 74 6f descriptor into
16020 20 2a 70 46 69 6c 65 2e 20 52 65 74 75 72 6e 20 *pFile. Return
16030 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 SQLITE_OK on suc
16040 63 65 73 73 20 0a 2a 2a 20 6f 72 20 73 6f 6d 65 cess .** or some
16050 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 other error cod
16060 65 20 69 66 20 77 65 20 66 61 69 6c 2e 20 54 68 e if we fail. Th
16070 65 20 4f 53 20 77 69 6c 6c 20 61 75 74 6f 6d 61 e OS will automa
16080 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 64 65 6c 65 tically .** dele
16090 74 65 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 te the temporary
160a0 20 66 69 6c 65 20 77 68 65 6e 20 69 74 20 69 73 file when it is
160b0 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 closed..**.** T
160c0 68 65 20 66 6c 61 67 73 20 70 61 73 73 65 64 20 he flags passed
160d0 74 6f 20 74 68 65 20 56 46 53 20 6c 61 79 65 72 to the VFS layer
160e0 20 78 4f 70 65 6e 28 29 20 63 61 6c 6c 20 61 72 xOpen() call ar
160f0 65 20 74 68 6f 73 65 20 73 70 65 63 69 66 69 65 e those specifie
16100 64 0a 2a 2a 20 62 79 20 70 61 72 61 6d 65 74 65 d.** by paramete
16110 72 20 76 66 73 46 6c 61 67 73 20 4f 52 65 64 20 r vfsFlags ORed
16120 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 with the followi
16130 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 ng:.**.** SQ
16140 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
16150 49 54 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 ITE.** SQLIT
16160 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 0a 2a 2a E_OPEN_CREATE.**
16170 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e SQLITE_OPEN
16180 5f 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 _EXCLUSIVE.**
16190 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 SQLITE_OPEN_DE
161a0 4c 45 54 45 4f 4e 43 4c 4f 53 45 0a 2a 2f 0a 73 LETEONCLOSE.*/.s
161b0 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 4f tatic int pagerO
161c0 70 65 6e 74 65 6d 70 28 0a 20 20 50 61 67 65 72 pentemp(. Pager
161d0 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 *pPager,
161e0 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f 62 /* The pager ob
161f0 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ject */. sqlite
16200 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 3_file *pFile,
16210 2f 2a 20 57 72 69 74 65 20 74 68 65 20 66 69 6c /* Write the fil
16220 65 20 64 65 73 63 72 69 70 74 6f 72 20 68 65 72 e descriptor her
16230 65 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c e */. int vfsFl
16240 61 67 73 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ags /*
16250 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72 Flags passed thr
16260 6f 75 67 68 20 74 6f 20 74 68 65 20 56 46 53 20 ough to the VFS
16270 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 */.){. int rc;
16280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16290 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
162a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
162b0 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 EST. sqlite3_op
162c0 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 2b 2b 3b 20 entemp_count++;
162d0 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 74 65 73 /* Used for tes
162e0 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 ting and analysi
162f0 73 20 6f 6e 6c 79 20 2a 2f 0a 23 65 6e 64 69 66 s only */.#endif
16300 0a 0a 20 20 76 66 73 46 6c 61 67 73 20 7c 3d 20 .. vfsFlags |=
16310 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
16320 44 57 52 49 54 45 20 7c 20 53 51 4c 49 54 45 5f DWRITE | SQLITE_
16330 4f 50 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20 OPEN_CREATE |.
16340 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 SQLITE
16350 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 _OPEN_EXCLUSIVE
16360 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 | SQLITE_OPEN_DE
16370 4c 45 54 45 4f 4e 43 4c 4f 53 45 3b 0a 20 20 72 LETEONCLOSE;. r
16380 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 c = sqlite3OsOpe
16390 6e 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 n(pPager->pVfs,
163a0 30 2c 20 70 46 69 6c 65 2c 20 76 66 73 46 6c 61 0, pFile, vfsFla
163b0 67 73 2c 20 30 29 3b 0a 20 20 61 73 73 65 72 74 gs, 0);. assert
163c0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
163d0 7c 7c 20 69 73 4f 70 65 6e 28 70 46 69 6c 65 29 || isOpen(pFile)
163e0 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b );. return rc;
163f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
16400 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 e busy handler f
16410 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 unction..**.** T
16420 68 65 20 70 61 67 65 72 20 69 6e 76 6f 6b 65 73 he pager invokes
16430 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 the busy-handle
16440 72 20 69 66 20 73 71 6c 69 74 65 33 4f 73 4c 6f r if sqlite3OsLo
16450 63 6b 28 29 20 72 65 74 75 72 6e 73 20 0a 2a 2a ck() returns .**
16460 20 53 51 4c 49 54 45 5f 42 55 53 59 20 77 68 65 SQLITE_BUSY whe
16470 6e 20 74 72 79 69 6e 67 20 74 6f 20 75 70 67 72 n trying to upgr
16480 61 64 65 20 66 72 6f 6d 20 6e 6f 2d 6c 6f 63 6b ade from no-lock
16490 20 74 6f 20 61 20 53 48 41 52 45 44 20 6c 6f 63 to a SHARED loc
164a0 6b 2c 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 74 72 k,.** or when tr
164b0 79 69 6e 67 20 74 6f 20 75 70 67 72 61 64 65 20 ying to upgrade
164c0 66 72 6f 6d 20 61 20 52 45 53 45 52 56 45 44 20 from a RESERVED
164d0 6c 6f 63 6b 20 74 6f 20 61 6e 20 45 58 43 4c 55 lock to an EXCLU
164e0 53 49 56 45 20 0a 2a 2a 20 6c 6f 63 6b 2e 20 49 SIVE .** lock. I
164f0 74 20 64 6f 65 73 20 2a 6e 6f 74 2a 20 69 6e 76 t does *not* inv
16500 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e oke the busy han
16510 64 6c 65 72 20 77 68 65 6e 20 75 70 67 72 61 64 dler when upgrad
16520 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20 53 48 41 52 ing from.** SHAR
16530 45 44 20 74 6f 20 52 45 53 45 52 56 45 44 2c 20 ED to RESERVED,
16540 6f 72 20 77 68 65 6e 20 75 70 67 72 61 64 69 6e or when upgradin
16550 67 20 66 72 6f 6d 20 53 48 41 52 45 44 20 74 6f g from SHARED to
16560 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 28 77 EXCLUSIVE.** (w
16570 68 69 63 68 20 6f 63 63 75 72 73 20 64 75 72 69 hich occurs duri
16580 6e 67 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 ng hot-journal r
16590 6f 6c 6c 62 61 63 6b 29 2e 20 53 75 6d 6d 61 72 ollback). Summar
165a0 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 54 72 61 6e 73 y:.**.** Trans
165b0 69 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 ition
165c0 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 49 | I
165d0 6e 76 6f 6b 65 73 20 78 42 75 73 79 48 61 6e 64 nvokes xBusyHand
165e0 6c 65 72 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d ler.** -------
165f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
16600 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
16610 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
16620 2d 0a 2a 2a 20 20 20 4e 4f 5f 4c 4f 43 4b 20 20 -.** NO_LOCK
16630 20 20 20 20 20 2d 3e 20 53 48 41 52 45 44 5f 4c -> SHARED_L
16640 4f 43 4b 20 20 20 20 20 20 7c 20 59 65 73 0a 2a OCK | Yes.*
16650 2a 20 20 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 * SHARED_LOCK
16660 20 20 2d 3e 20 52 45 53 45 52 56 45 44 5f 4c 4f -> RESERVED_LO
16670 43 4b 20 20 20 20 7c 20 4e 6f 0a 2a 2a 20 20 20 CK | No.**
16680 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e SHARED_LOCK ->
16690 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 EXCLUSIVE_LOCK
166a0 20 20 7c 20 4e 6f 0a 2a 2a 20 20 20 52 45 53 45 | No.** RESE
166b0 52 56 45 44 5f 4c 4f 43 4b 20 2d 3e 20 45 58 43 RVED_LOCK -> EXC
166c0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 LUSIVE_LOCK |
166d0 59 65 73 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 Yes.**.** If the
166e0 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 busy-handler ca
166f0 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e llback returns n
16700 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 6c 6f 63 on-zero, the loc
16710 6b 20 69 73 20 0a 2a 2a 20 72 65 74 72 69 65 64 k is .** retried
16720 2e 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 . If it returns
16730 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 53 zero, then the S
16740 51 4c 49 54 45 5f 42 55 53 59 20 65 72 72 6f 72 QLITE_BUSY error
16750 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 is.** returned
16760 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 6f 66 to the caller of
16770 20 74 68 65 20 70 61 67 65 72 20 41 50 49 20 66 the pager API f
16780 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 unction..*/.void
16790 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
167a0 42 75 73 79 68 61 6e 64 6c 65 72 28 0a 20 20 50 Busyhandler(. P
167b0 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 ager *pPager,
167c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
167d0 20 20 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a /* Pager obj
167e0 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 ect */. int (*x
167f0 42 75 73 79 48 61 6e 64 6c 65 72 29 28 76 6f 69 BusyHandler)(voi
16800 64 20 2a 29 2c 20 20 20 20 20 20 20 20 20 2f 2a d *), /*
16810 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 73 79 Pointer to busy
16820 2d 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f -handler functio
16830 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 n */. void *pBu
16840 73 79 48 61 6e 64 6c 65 72 41 72 67 20 20 20 20 syHandlerArg
16850 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
16860 72 67 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 rgument to pass
16870 74 6f 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20 to xBusyHandler
16880 2a 2f 0a 29 7b 20 20 0a 20 20 70 50 61 67 65 72 */.){ . pPager
16890 2d 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 20 3d ->xBusyHandler =
168a0 20 78 42 75 73 79 48 61 6e 64 6c 65 72 3b 0a 20 xBusyHandler;.
168b0 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 pPager->pBusyHa
168c0 6e 64 6c 65 72 41 72 67 20 3d 20 70 42 75 73 79 ndlerArg = pBusy
168d0 48 61 6e 64 6c 65 72 41 72 67 3b 0a 7d 0a 0a 2f HandlerArg;.}../
168e0 2a 0a 2a 2a 20 52 65 70 6f 72 74 20 74 68 65 20 *.** Report the
168f0 63 75 72 72 65 6e 74 20 70 61 67 65 20 73 69 7a current page siz
16900 65 20 61 6e 64 20 6e 75 6d 62 65 72 20 6f 66 20 e and number of
16910 72 65 73 65 72 76 65 64 20 62 79 74 65 73 20 62 reserved bytes b
16920 61 63 6b 0a 2a 2a 20 74 6f 20 74 68 65 20 63 6f ack.** to the co
16930 64 65 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 dec..*/.#ifdef S
16940 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a QLITE_HAS_CODEC.
16950 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 static void page
16960 72 52 65 70 6f 72 74 53 69 7a 65 28 50 61 67 65 rReportSize(Page
16970 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 r *pPager){. if
16980 28 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 ( pPager->xCodec
16990 53 69 7a 65 43 68 6e 67 20 29 7b 0a 20 20 20 20 SizeChng ){.
169a0 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 53 69 pPager->xCodecSi
169b0 7a 65 43 68 6e 67 28 70 50 61 67 65 72 2d 3e 70 zeChng(pPager->p
169c0 43 6f 64 65 63 2c 20 70 50 61 67 65 72 2d 3e 70 Codec, pPager->p
169d0 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 ageSize,.
169e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
169f0 20 20 20 20 28 69 6e 74 29 70 50 61 67 65 72 2d (int)pPager-
16a00 3e 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 7d 0a >nReserve);. }.
16a10 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 }.#else.# define
16a20 20 70 61 67 65 72 52 65 70 6f 72 74 53 69 7a 65 pagerReportSize
16a30 28 58 29 20 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 (X) /* No-op
16a40 20 69 66 20 77 65 20 64 6f 20 6e 6f 74 20 73 75 if we do not su
16a50 70 70 6f 72 74 20 61 20 63 6f 64 65 63 20 2a 2f pport a codec */
16a60 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 .#endif../*.** C
16a70 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 73 hange the page s
16a80 69 7a 65 20 75 73 65 64 20 62 79 20 74 68 65 20 ize used by the
16a90 50 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 54 68 Pager object. Th
16aa0 65 20 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20 e new page size
16ab0 0a 2a 2a 20 69 73 20 70 61 73 73 65 64 20 69 6e .** is passed in
16ac0 20 2a 70 50 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a *pPageSize..**.
16ad0 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 ** If the pager
16ae0 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 is in the error
16af0 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 state when this
16b00 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
16b10 65 64 2c 20 69 74 0a 2a 2a 20 69 73 20 61 20 6e ed, it.** is a n
16b20 6f 2d 6f 70 2e 20 54 68 65 20 76 61 6c 75 65 20 o-op. The value
16b30 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 returned is the
16b40 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f error state erro
16b50 72 20 63 6f 64 65 20 28 69 2e 65 2e 20 0a 2a 2a r code (i.e. .**
16b60 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 49 one of SQLITE_I
16b70 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52 OERR, SQLITE_COR
16b80 52 55 50 54 20 6f 72 20 53 51 4c 49 54 45 5f 46 RUPT or SQLITE_F
16b90 55 4c 4c 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 ULL)..**.** Othe
16ba0 72 77 69 73 65 2c 20 69 66 20 61 6c 6c 20 6f 66 rwise, if all of
16bb0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 the following a
16bc0 72 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 re true:.**.**
16bd0 20 2a 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 * the new page
16be0 73 69 7a 65 20 28 76 61 6c 75 65 20 6f 66 20 2a size (value of *
16bf0 70 50 61 67 65 53 69 7a 65 29 20 69 73 20 76 61 pPageSize) is va
16c00 6c 69 64 20 28 61 20 70 6f 77 65 72 20 0a 2a 2a lid (a power .**
16c10 20 20 20 20 20 6f 66 20 74 77 6f 20 62 65 74 77 of two betw
16c20 65 65 6e 20 35 31 32 20 61 6e 64 20 53 51 4c 49 een 512 and SQLI
16c30 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 TE_MAX_PAGE_SIZE
16c40 2c 20 69 6e 63 6c 75 73 69 76 65 29 2c 20 61 6e , inclusive), an
16c50 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 72 d.**.** * ther
16c60 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e e are no outstan
16c70 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 65 ding page refere
16c80 6e 63 65 73 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 nces, and.**.**
16c90 20 20 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 * the database
16ca0 20 69 73 20 65 69 74 68 65 72 20 6e 6f 74 20 61 is either not a
16cb0 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
16cc0 62 61 73 65 20 6f 72 20 69 74 20 69 73 0a 2a 2a base or it is.**
16cd0 20 20 20 20 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 an in-memor
16ce0 79 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 y database that
16cf0 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 73 currently consis
16d00 74 73 20 6f 66 20 7a 65 72 6f 20 70 61 67 65 73 ts of zero pages
16d10 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74 68 65 ..**.** then the
16d20 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 70 61 pager object pa
16d30 67 65 20 73 69 7a 65 20 69 73 20 73 65 74 20 74 ge size is set t
16d40 6f 20 2a 70 50 61 67 65 53 69 7a 65 2e 0a 2a 2a o *pPageSize..**
16d50 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 .** If the page
16d60 73 69 7a 65 20 69 73 20 63 68 61 6e 67 65 64 2c size is changed,
16d70 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 then this funct
16d80 69 6f 6e 20 75 73 65 73 20 73 71 6c 69 74 65 33 ion uses sqlite3
16d90 50 61 67 65 72 4d 61 6c 6c 6f 63 28 29 20 0a 2a PagerMalloc() .*
16da0 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 6e 65 * to obtain a ne
16db0 77 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 w Pager.pTmpSpac
16dc0 65 20 62 75 66 66 65 72 2e 20 49 66 20 74 68 69 e buffer. If thi
16dd0 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 s allocation att
16de0 65 6d 70 74 20 0a 2a 2a 20 66 61 69 6c 73 2c 20 empt .** fails,
16df0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 SQLITE_NOMEM is
16e00 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 returned and the
16e10 20 70 61 67 65 20 73 69 7a 65 20 72 65 6d 61 69 page size remai
16e20 6e 73 20 75 6e 63 68 61 6e 67 65 64 2e 20 0a 2a ns unchanged. .*
16e30 2a 20 49 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 * In all other c
16e40 61 73 65 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 ases, SQLITE_OK
16e50 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
16e60 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 73 ** If the page s
16e70 69 7a 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 ize is not chang
16e80 65 64 2c 20 65 69 74 68 65 72 20 62 65 63 61 75 ed, either becau
16e90 73 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 65 6e se one of the en
16ea0 75 6d 65 72 61 74 65 64 0a 2a 2a 20 63 6f 6e 64 umerated.** cond
16eb0 69 74 69 6f 6e 73 20 61 62 6f 76 65 20 69 73 20 itions above is
16ec0 6e 6f 74 20 74 72 75 65 2c 20 74 68 65 20 70 61 not true, the pa
16ed0 67 65 72 20 77 61 73 20 69 6e 20 65 72 72 6f 72 ger was in error
16ee0 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 state when this
16ef0 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 .** function was
16f00 20 63 61 6c 6c 65 64 2c 20 6f 72 20 62 65 63 61 called, or beca
16f10 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 use the memory a
16f20 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 llocation attemp
16f30 74 20 66 61 69 6c 65 64 2c 20 0a 2a 2a 20 74 68 t failed, .** th
16f40 65 6e 20 2a 70 50 61 67 65 53 69 7a 65 20 69 73 en *pPageSize is
16f50 20 73 65 74 20 74 6f 20 74 68 65 20 6f 6c 64 2c set to the old,
16f60 20 72 65 74 61 69 6e 65 64 20 70 61 67 65 20 73 retained page s
16f70 69 7a 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 ize before retur
16f80 6e 69 6e 67 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c ning..*/.int sql
16f90 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65 ite3PagerSetPage
16fa0 73 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 size(Pager *pPag
16fb0 65 72 2c 20 75 31 36 20 2a 70 50 61 67 65 53 69 er, u16 *pPageSi
16fc0 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65 72 76 65 ze, int nReserve
16fd0 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 50 ){. int rc = pP
16fe0 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a ager->errCode;..
16ff0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
17000 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 31 36 20 70 _OK ){. u16 p
17010 61 67 65 53 69 7a 65 20 3d 20 2a 70 50 61 67 65 ageSize = *pPage
17020 53 69 7a 65 3b 0a 20 20 20 20 61 73 73 65 72 74 Size;. assert
17030 28 20 70 61 67 65 53 69 7a 65 3d 3d 30 20 7c 7c ( pageSize==0 ||
17040 20 28 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 (pageSize>=512
17050 26 26 20 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c && pageSize<=SQL
17060 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a ITE_MAX_PAGE_SIZ
17070 45 29 20 29 3b 0a 20 20 20 20 69 66 28 20 28 70 E) );. if( (p
17080 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 Pager->memDb==0
17090 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a || pPager->dbSiz
170a0 65 3d 3d 30 29 0a 20 20 20 20 20 26 26 20 73 71 e==0). && sq
170b0 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f lite3PcacheRefCo
170c0 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 unt(pPager->pPCa
170d0 63 68 65 29 3d 3d 30 20 0a 20 20 20 20 20 26 26 che)==0 . &&
170e0 20 70 61 67 65 53 69 7a 65 20 26 26 20 70 61 67 pageSize && pag
170f0 65 53 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 70 eSize!=pPager->p
17100 61 67 65 53 69 7a 65 20 0a 20 20 20 20 29 7b 0a ageSize . ){.
17110 20 20 20 20 20 20 63 68 61 72 20 2a 70 4e 65 77 char *pNew
17120 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 = (char *)sqlit
17130 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 70 61 67 e3PageMalloc(pag
17140 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 eSize);. if
17150 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 ( !pNew ){.
17160 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
17170 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 OMEM;. }els
17180 65 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 e{. pager
17190 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a _reset(pPager);.
171a0 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
171b0 70 61 67 65 53 69 7a 65 20 3d 20 70 61 67 65 53 pageSize = pageS
171c0 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c ize;. sql
171d0 69 74 65 33 50 61 67 65 46 72 65 65 28 70 50 61 ite3PageFree(pPa
171e0 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b ger->pTmpSpace);
171f0 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d . pPager-
17200 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 70 4e 65 >pTmpSpace = pNe
17210 77 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 w;. sqlit
17220 65 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 e3PcacheSetPageS
17230 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 ize(pPager->pPCa
17240 63 68 65 2c 20 70 61 67 65 53 69 7a 65 29 3b 0a che, pageSize);.
17250 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
17260 20 20 2a 70 50 61 67 65 53 69 7a 65 20 3d 20 28 *pPageSize = (
17270 75 31 36 29 70 50 61 67 65 72 2d 3e 70 61 67 65 u16)pPager->page
17280 53 69 7a 65 3b 0a 20 20 20 20 69 66 28 20 6e 52 Size;. if( nR
17290 65 73 65 72 76 65 3c 30 20 29 20 6e 52 65 73 65 eserve<0 ) nRese
172a0 72 76 65 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 rve = pPager->nR
172b0 65 73 65 72 76 65 3b 0a 20 20 20 20 61 73 73 65 eserve;. asse
172c0 72 74 28 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 rt( nReserve>=0
172d0 26 26 20 6e 52 65 73 65 72 76 65 3c 31 30 30 30 && nReserve<1000
172e0 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e );. pPager->
172f0 6e 52 65 73 65 72 76 65 20 3d 20 28 69 31 36 29 nReserve = (i16)
17300 6e 52 65 73 65 72 76 65 3b 0a 20 20 20 20 70 61 nReserve;. pa
17310 67 65 72 52 65 70 6f 72 74 53 69 7a 65 28 70 50 gerReportSize(pP
17320 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ager);. }. ret
17330 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
17340 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
17350 72 20 74 6f 20 74 68 65 20 22 74 65 6d 70 6f 72 r to the "tempor
17360 61 72 79 20 70 61 67 65 22 20 62 75 66 66 65 72 ary page" buffer
17370 20 68 65 6c 64 20 69 6e 74 65 72 6e 61 6c 6c 79 held internally
17380 0a 2a 2a 20 62 79 20 74 68 65 20 70 61 67 65 72 .** by the pager
17390 2e 20 20 54 68 69 73 20 69 73 20 61 20 62 75 66 . This is a buf
173a0 66 65 72 20 74 68 61 74 20 69 73 20 62 69 67 20 fer that is big
173b0 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 enough to hold t
173c0 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20 63 6f 6e he.** entire con
173d0 74 65 6e 74 20 6f 66 20 61 20 64 61 74 61 62 61 tent of a databa
173e0 73 65 20 70 61 67 65 2e 20 20 54 68 69 73 20 62 se page. This b
173f0 75 66 66 65 72 20 69 73 20 75 73 65 64 20 69 6e uffer is used in
17400 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 64 75 72 69 ternally.** duri
17410 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 ng rollback and
17420 77 69 6c 6c 20 62 65 20 6f 76 65 72 77 72 69 74 will be overwrit
17430 74 65 6e 20 77 68 65 6e 65 76 65 72 20 61 20 72 ten whenever a r
17440 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 ollback.** occur
17450 73 2e 20 20 42 75 74 20 6f 74 68 65 72 20 6d 6f s. But other mo
17460 64 75 6c 65 73 20 61 72 65 20 66 72 65 65 20 74 dules are free t
17470 6f 20 75 73 65 20 69 74 20 74 6f 6f 2c 20 61 73 o use it too, as
17480 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 6e 6f 20 72 long as.** no r
17490 6f 6c 6c 62 61 63 6b 73 20 61 72 65 20 68 61 70 ollbacks are hap
174a0 70 65 6e 69 6e 67 2e 0a 2a 2f 0a 76 6f 69 64 20 pening..*/.void
174b0 2a 73 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d *sqlite3PagerTem
174c0 70 53 70 61 63 65 28 50 61 67 65 72 20 2a 70 50 pSpace(Pager *pP
174d0 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
174e0 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 pPager->pTmpSpac
174f0 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 e;.}../*.** Atte
17500 6d 70 74 20 74 6f 20 73 65 74 20 74 68 65 20 6d mpt to set the m
17510 61 78 69 6d 75 6d 20 64 61 74 61 62 61 73 65 20 aximum database
17520 70 61 67 65 20 63 6f 75 6e 74 20 69 66 20 6d 78 page count if mx
17530 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 Page is positive
17540 2e 20 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 . .** Make no ch
17550 61 6e 67 65 73 20 69 66 20 6d 78 50 61 67 65 20 anges if mxPage
17560 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 is zero or negat
17570 69 76 65 2e 20 20 41 6e 64 20 6e 65 76 65 72 20 ive. And never
17580 72 65 64 75 63 65 20 74 68 65 0a 2a 2a 20 6d 61 reduce the.** ma
17590 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 ximum page count
175a0 20 62 65 6c 6f 77 20 74 68 65 20 63 75 72 72 65 below the curre
175b0 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 nt size of the d
175c0 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 52 atabase..**.** R
175d0 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6d 78 50 egardless of mxP
175e0 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 age, return the
175f0 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 6d 20 current maximum
17600 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 69 page count..*/.i
17610 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d nt sqlite3PagerM
17620 61 78 50 61 67 65 43 6f 75 6e 74 28 50 61 67 65 axPageCount(Page
17630 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6d r *pPager, int m
17640 78 50 61 67 65 29 7b 0a 20 20 69 66 28 20 6d 78 xPage){. if( mx
17650 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 70 50 Page>0 ){. pP
17660 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 6d ager->mxPgno = m
17670 78 50 61 67 65 3b 0a 20 20 7d 0a 20 20 73 71 6c xPage;. }. sql
17680 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 ite3PagerPagecou
17690 6e 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 nt(pPager, 0);.
176a0 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e return pPager->
176b0 6d 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a mxPgno;.}../*.**
176c0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 The following s
176d0 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 et of routines a
176e0 72 65 20 75 73 65 64 20 74 6f 20 64 69 73 61 62 re used to disab
176f0 6c 65 20 74 68 65 20 73 69 6d 75 6c 61 74 65 64 le the simulated
17700 0a 2a 2a 20 49 2f 4f 20 65 72 72 6f 72 20 6d 65 .** I/O error me
17710 63 68 61 6e 69 73 6d 2e 20 20 54 68 65 73 65 20 chanism. These
17720 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 routines are use
17730 64 20 74 6f 20 61 76 6f 69 64 20 73 69 6d 75 6c d to avoid simul
17740 61 74 65 64 0a 2a 2a 20 65 72 72 6f 72 73 20 69 ated.** errors i
17750 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 77 n places where w
17760 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 e do not care ab
17770 6f 75 74 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a out errors..**.*
17780 2a 20 55 6e 6c 65 73 73 20 2d 44 53 51 4c 49 54 * Unless -DSQLIT
17790 45 5f 54 45 53 54 3d 31 20 69 73 20 75 73 65 64 E_TEST=1 is used
177a0 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 , these routines
177b0 20 61 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70 73 0a are all no-ops.
177c0 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20 ** and generate
177d0 6e 6f 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64 no code..*/.#ifd
177e0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 65 ef SQLITE_TEST.e
177f0 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 xtern int sqlite
17800 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 3_io_error_pendi
17810 6e 67 3b 0a 65 78 74 65 72 6e 20 69 6e 74 20 73 ng;.extern int s
17820 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
17830 68 69 74 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 hit;.static int
17840 73 61 76 65 64 5f 63 6e 74 3b 0a 76 6f 69 64 20 saved_cnt;.void
17850 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 disable_simulate
17860 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 d_io_errors(void
17870 29 7b 0a 20 20 73 61 76 65 64 5f 63 6e 74 20 3d ){. saved_cnt =
17880 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
17890 72 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 73 71 6c r_pending;. sql
178a0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 ite3_io_error_pe
178b0 6e 64 69 6e 67 20 3d 20 2d 31 3b 0a 7d 0a 76 6f nding = -1;.}.vo
178c0 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 id enable_simula
178d0 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f ted_io_errors(vo
178e0 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 id){. sqlite3_i
178f0 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 o_error_pending
17900 3d 20 73 61 76 65 64 5f 63 6e 74 3b 0a 7d 0a 23 = saved_cnt;.}.#
17910 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 64 69 else.# define di
17920 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f sable_simulated_
17930 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 20 64 65 io_errors().# de
17940 66 69 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d 75 fine enable_simu
17950 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 lated_io_errors(
17960 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
17970 52 65 61 64 20 74 68 65 20 66 69 72 73 74 20 4e Read the first N
17980 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 bytes from the
17990 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 beginning of the
179a0 20 66 69 6c 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 file into memor
179b0 79 0a 2a 2a 20 74 68 61 74 20 70 44 65 73 74 20 y.** that pDest
179c0 70 6f 69 6e 74 73 20 74 6f 2e 20 0a 2a 2a 0a 2a points to. .**.*
179d0 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 77 * If the pager w
179e0 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 74 as opened on a t
179f0 72 61 6e 73 69 65 6e 74 20 66 69 6c 65 20 28 7a ransient file (z
17a00 46 69 6c 65 6e 61 6d 65 3d 3d 22 22 29 2c 20 6f Filename==""), o
17a10 72 0a 2a 2a 20 6f 70 65 6e 65 64 20 6f 6e 20 61 r.** opened on a
17a20 20 66 69 6c 65 20 6c 65 73 73 20 74 68 61 6e 20 file less than
17a30 4e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c N bytes in size,
17a40 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 the output buff
17a50 65 72 20 69 73 0a 2a 2a 20 7a 65 72 6f 65 64 20 er is.** zeroed
17a60 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 and SQLITE_OK re
17a70 74 75 72 6e 65 64 2e 20 54 68 65 20 72 61 74 69 turned. The rati
17a80 6f 6e 61 6c 65 20 66 6f 72 20 74 68 69 73 20 69 onale for this i
17a90 73 20 74 68 61 74 20 74 68 69 73 20 0a 2a 2a 20 s that this .**
17aa0 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 function is used
17ab0 20 74 6f 20 72 65 61 64 20 64 61 74 61 62 61 73 to read databas
17ac0 65 20 68 65 61 64 65 72 73 2c 20 61 6e 64 20 61 e headers, and a
17ad0 20 6e 65 77 20 74 72 61 6e 73 69 65 6e 74 20 6f new transient o
17ae0 72 0a 2a 2a 20 7a 65 72 6f 20 73 69 7a 65 64 20 r.** zero sized
17af0 64 61 74 61 62 61 73 65 20 68 61 73 20 61 20 68 database has a h
17b00 65 61 64 65 72 20 74 68 61 6e 20 63 6f 6e 73 69 eader than consi
17b10 73 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 66 20 sts entirely of
17b20 7a 65 72 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 zeroes..**.** If
17b30 20 61 6e 79 20 49 4f 20 65 72 72 6f 72 20 61 70 any IO error ap
17b40 61 72 74 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f art from SQLITE_
17b50 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 IOERR_SHORT_READ
17b60 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c is encountered,
17b70 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 63 6f .** the error co
17b80 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 de is returned t
17b90 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 61 6e 64 o the caller and
17ba0 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
17bb0 20 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62 the.** output b
17bc0 75 66 66 65 72 20 75 6e 64 65 66 69 6e 65 64 2e uffer undefined.
17bd0 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 .*/.int sqlite3P
17be0 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 agerReadFilehead
17bf0 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 er(Pager *pPager
17c00 2c 20 69 6e 74 20 4e 2c 20 75 6e 73 69 67 6e 65 , int N, unsigne
17c10 64 20 63 68 61 72 20 2a 70 44 65 73 74 29 7b 0a d char *pDest){.
17c20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
17c30 45 5f 4f 4b 3b 0a 20 20 6d 65 6d 73 65 74 28 70 E_OK;. memset(p
17c40 44 65 73 74 2c 20 30 2c 20 4e 29 3b 0a 20 20 61 Dest, 0, N);. a
17c50 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
17c60 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 ager->fd) || pPa
17c70 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b ger->tempFile );
17c80 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 . if( isOpen(pP
17c90 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 ager->fd) ){.
17ca0 20 49 4f 54 52 41 43 45 28 28 22 44 42 48 44 52 IOTRACE(("DBHDR
17cb0 20 25 70 20 30 20 25 64 5c 6e 22 2c 20 70 50 61 %p 0 %d\n", pPa
17cc0 67 65 72 2c 20 4e 29 29 0a 20 20 20 20 72 63 20 ger, N)). rc
17cd0 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 = sqlite3OsRead(
17ce0 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 44 65 73 pPager->fd, pDes
17cf0 74 2c 20 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 t, N, 0);. if
17d00 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 ( rc==SQLITE_IOE
17d10 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b RR_SHORT_READ ){
17d20 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
17d30 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d TE_OK;. }. }
17d40 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
17d50 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
17d60 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
17d70 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 f pages in the d
17d80 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 73 73 atabase file ass
17d90 6f 63 69 61 74 65 64 20 0a 2a 2a 20 77 69 74 68 ociated .** with
17da0 20 70 50 61 67 65 72 2e 20 4e 6f 72 6d 61 6c 6c pPager. Normall
17db0 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c 63 75 y, this is calcu
17dc0 6c 61 74 65 64 20 61 73 20 28 3c 64 62 20 66 69 lated as (<db fi
17dd0 6c 65 20 73 69 7a 65 3e 2f 3c 70 61 67 65 2d 73 le size>/<page-s
17de0 69 7a 65 3e 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 ize>)..** Howeve
17df0 72 2c 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 r, if the file i
17e00 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 s between 1 and
17e10 3c 70 61 67 65 2d 73 69 7a 65 3e 20 62 79 74 65 <page-size> byte
17e20 73 20 69 6e 20 73 69 7a 65 2c 20 74 68 65 6e 20 s in size, then
17e30 0a 2a 2a 20 74 68 69 73 20 69 73 20 63 6f 6e 73 .** this is cons
17e40 69 64 65 72 65 64 20 61 20 31 20 70 61 67 65 20 idered a 1 page
17e50 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 file..**.** If t
17e60 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 65 he pager is in e
17e70 72 72 6f 72 20 73 74 61 74 65 20 77 68 65 6e 20 rror state when
17e80 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
17e90 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 74 68 called, then th
17ea0 65 0a 2a 2a 20 65 72 72 6f 72 20 73 74 61 74 65 e.** error state
17eb0 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
17ec0 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 6e 50 eturned and *pnP
17ed0 61 67 65 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 age left unchang
17ee0 65 64 2e 20 4f 72 2c 0a 2a 2a 20 69 66 20 74 68 ed. Or,.** if th
17ef0 65 20 66 69 6c 65 20 73 79 73 74 65 6d 20 68 61 e file system ha
17f00 73 20 74 6f 20 62 65 20 71 75 65 72 69 65 64 20 s to be queried
17f10 66 6f 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 for the size of
17f20 74 68 65 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 the file and.**
17f30 74 68 65 20 71 75 65 72 79 20 61 74 74 65 6d 70 the query attemp
17f40 74 20 72 65 74 75 72 6e 73 20 61 6e 20 49 4f 20 t returns an IO
17f50 65 72 72 6f 72 2c 20 74 68 65 20 49 4f 20 65 72 error, the IO er
17f60 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
17f70 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 2a 70 6e 50 rned.** and *pnP
17f80 61 67 65 20 69 73 20 6c 65 66 74 20 75 6e 63 68 age is left unch
17f90 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 anged..**.** Oth
17fa0 65 72 77 69 73 65 2c 20 69 66 20 65 76 65 72 79 erwise, if every
17fb0 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 thing is success
17fc0 66 75 6c 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 ful, then SQLITE
17fd0 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 0a _OK is returned.
17fe0 2a 2a 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 69 ** and *pnPage i
17ff0 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d s set to the num
18000 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
18010 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f the database..*/
18020 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 .int sqlite3Page
18030 72 50 61 67 65 63 6f 75 6e 74 28 50 61 67 65 72 rPagecount(Pager
18040 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 *pPager, int *p
18050 6e 50 61 67 65 29 7b 0a 20 20 50 67 6e 6f 20 6e nPage){. Pgno n
18060 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
18070 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 /* Value to
18080 72 65 74 75 72 6e 20 76 69 61 20 2a 70 6e 50 61 return via *pnPa
18090 67 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 ge */.. /* If t
180a0 68 65 20 70 61 67 65 72 20 69 73 20 61 6c 72 65 he pager is alre
180b0 61 64 79 20 69 6e 20 74 68 65 20 65 72 72 6f 72 ady in the error
180c0 20 73 74 61 74 65 2c 20 72 65 74 75 72 6e 20 74 state, return t
180d0 68 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 2a he error code. *
180e0 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e /. if( pPager->
180f0 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 72 errCode ){. r
18100 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 eturn pPager->er
18110 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a rCode;. }.. /*
18120 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6e Determine the n
18130 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
18140 6e 20 74 68 65 20 66 69 6c 65 2e 20 53 74 6f 72 n the file. Stor
18150 65 20 74 68 69 73 20 69 6e 20 6e 50 61 67 65 2e e this in nPage.
18160 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 */. if( pPager
18170 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 7b ->dbSizeValid ){
18180 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 70 50 61 . nPage = pPa
18190 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 7d ger->dbSize;. }
181a0 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 else{. int rc
181b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
181c0 20 20 2f 2a 20 45 72 72 6f 72 20 72 65 74 75 72 /* Error retur
181d0 6e 65 64 20 62 79 20 4f 73 46 69 6c 65 53 69 7a ned by OsFileSiz
181e0 65 28 29 20 2a 2f 0a 20 20 20 20 69 36 34 20 6e e() */. i64 n
181f0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
18200 20 20 20 2f 2a 20 46 69 6c 65 20 73 69 7a 65 20 /* File size
18210 69 6e 20 62 79 74 65 73 20 72 65 74 75 72 6e 65 in bytes returne
18220 64 20 62 79 20 4f 73 46 69 6c 65 53 69 7a 65 28 d by OsFileSize(
18230 29 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 ) */.. assert
18240 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
18250 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e >fd) || pPager->
18260 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 tempFile );.
18270 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 if( isOpen(pPage
18280 72 2d 3e 66 64 29 20 26 26 20 28 30 20 21 3d 20 r->fd) && (0 !=
18290 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 (rc = sqlite3OsF
182a0 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e ileSize(pPager->
182b0 66 64 2c 20 26 6e 29 29 29 20 29 7b 0a 20 20 20 fd, &n))) ){.
182c0 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 pager_error(p
182d0 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 Pager, rc);.
182e0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
182f0 20 7d 0a 20 20 20 20 69 66 28 20 6e 3e 30 20 26 }. if( n>0 &
18300 26 20 6e 3c 70 50 61 67 65 72 2d 3e 70 61 67 65 & n<pPager->page
18310 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 6e 50 Size ){. nP
18320 61 67 65 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c age = 1;. }el
18330 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 se{. nPage
18340 3d 20 28 50 67 6e 6f 29 28 6e 20 2f 20 70 50 61 = (Pgno)(n / pPa
18350 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a ger->pageSize);.
18360 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50 }. if( pP
18370 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 ager->state!=PAG
18380 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 ER_UNLOCK ){.
18390 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a pPager->dbSiz
183a0 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 e = nPage;.
183b0 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 pPager->dbFileS
183c0 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 ize = nPage;.
183d0 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a pPager->dbSiz
183e0 65 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 eValid = 1;.
183f0 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 }. }.. /* If t
18400 68 65 20 63 75 72 72 65 6e 74 20 6e 75 6d 62 65 he current numbe
18410 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
18420 65 20 66 69 6c 65 20 69 73 20 67 72 65 61 74 65 e file is greate
18430 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20 2a 2a r than the . **
18440 20 63 6f 6e 66 69 67 75 72 65 64 20 6d 61 78 69 configured maxi
18450 6d 75 6d 20 70 61 67 65 72 20 6e 75 6d 62 65 72 mum pager number
18460 2c 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 61 , increase the a
18470 6c 6c 6f 77 65 64 20 6c 69 6d 69 74 20 73 6f 0a llowed limit so.
18480 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 66 69 ** that the fi
18490 6c 65 20 63 61 6e 20 62 65 20 72 65 61 64 2e 0a le can be read..
184a0 20 20 2a 2f 0a 20 20 69 66 28 20 6e 50 61 67 65 */. if( nPage
184b0 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 >pPager->mxPgno
184c0 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6d ){. pPager->m
184d0 78 50 67 6e 6f 20 3d 20 28 50 67 6e 6f 29 6e 50 xPgno = (Pgno)nP
184e0 61 67 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 age;. }.. /* S
184f0 65 74 20 74 68 65 20 6f 75 74 70 75 74 20 76 61 et the output va
18500 72 69 61 62 6c 65 20 61 6e 64 20 72 65 74 75 72 riable and retur
18510 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 2a 2f 0a 20 n SQLITE_OK */.
18520 20 69 66 28 20 70 6e 50 61 67 65 20 29 7b 0a 20 if( pnPage ){.
18530 20 20 20 2a 70 6e 50 61 67 65 20 3d 20 6e 50 61 *pnPage = nPa
18540 67 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ge;. }. return
18550 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a SQLITE_OK;.}...
18560 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 /*.** Try to obt
18570 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79 ain a lock of ty
18580 70 65 20 6c 6f 63 6b 74 79 70 65 20 6f 6e 20 74 pe locktype on t
18590 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
185a0 2e 20 49 66 0a 2a 2a 20 61 20 73 69 6d 69 6c 61 . If.** a simila
185b0 72 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 r or greater loc
185c0 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c k is already hel
185d0 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e d, this function
185e0 20 69 73 20 61 20 6e 6f 2d 6f 70 0a 2a 2a 20 28 is a no-op.** (
185f0 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 returning SQLITE
18600 5f 4f 4b 20 69 6d 6d 65 64 69 61 74 65 6c 79 29 _OK immediately)
18610 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ..**.** Otherwis
18620 65 2c 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 62 e, attempt to ob
18630 74 61 69 6e 20 74 68 65 20 6c 6f 63 6b 20 75 73 tain the lock us
18640 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 ing sqlite3OsLoc
18650 6b 28 29 2e 20 49 6e 76 6f 6b 65 20 0a 2a 2a 20 k(). Invoke .**
18660 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 the busy callbac
18670 6b 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 69 73 k if the lock is
18680 20 63 75 72 72 65 6e 74 6c 79 20 6e 6f 74 20 61 currently not a
18690 76 61 69 6c 61 62 6c 65 2e 20 52 65 70 65 61 74 vailable. Repeat
186a0 20 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 62 .** until the b
186b0 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 usy callback ret
186c0 75 72 6e 73 20 66 61 6c 73 65 20 6f 72 20 75 6e urns false or un
186d0 74 69 6c 20 74 68 65 20 61 74 74 65 6d 70 74 20 til the attempt
186e0 74 6f 20 0a 2a 2a 20 6f 62 74 61 69 6e 20 74 68 to .** obtain th
186f0 65 20 6c 6f 63 6b 20 73 75 63 63 65 65 64 73 2e e lock succeeds.
18700 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 .**.** Return SQ
18710 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 LITE_OK on succe
18720 73 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 ss and an error
18730 63 6f 64 65 20 69 66 20 77 65 20 63 61 6e 6e 6f code if we canno
18740 74 20 6f 62 74 61 69 6e 0a 2a 2a 20 74 68 65 20 t obtain.** the
18750 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 6c 6f 63 lock. If the loc
18760 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 73 75 k is obtained su
18770 63 63 65 73 73 66 75 6c 6c 79 2c 20 73 65 74 20 ccessfully, set
18780 74 68 65 20 50 61 67 65 72 2e 73 74 61 74 65 20 the Pager.state
18790 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 74 6f 20 .** variable to
187a0 6c 6f 63 6b 74 79 70 65 20 62 65 66 6f 72 65 20 locktype before
187b0 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 returning..*/.st
187c0 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 atic int pager_w
187d0 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 50 61 67 65 ait_on_lock(Page
187e0 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6c r *pPager, int l
187f0 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 ocktype){. int
18800 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
18810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18820 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
18830 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 4f 53 20 */.. /* The OS
18840 6c 6f 63 6b 20 76 61 6c 75 65 73 20 6d 75 73 74 lock values must
18850 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 be the same as
18860 74 68 65 20 50 61 67 65 72 20 6c 6f 63 6b 20 76 the Pager lock v
18870 61 6c 75 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 alues */. asser
18880 74 28 20 50 41 47 45 52 5f 53 48 41 52 45 44 3d t( PAGER_SHARED=
18890 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
188a0 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f assert( PAGER_
188b0 52 45 53 45 52 56 45 44 3d 3d 52 45 53 45 52 56 RESERVED==RESERV
188c0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 ED_LOCK );. ass
188d0 65 72 74 28 20 50 41 47 45 52 5f 45 58 43 4c 55 ert( PAGER_EXCLU
188e0 53 49 56 45 3d 3d 45 58 43 4c 55 53 49 56 45 5f SIVE==EXCLUSIVE_
188f0 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 LOCK );.. /* If
18900 20 74 68 65 20 66 69 6c 65 20 69 73 20 63 75 72 the file is cur
18910 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 20 rently unlocked
18920 74 68 65 6e 20 74 68 65 20 73 69 7a 65 20 6d 75 then the size mu
18930 73 74 20 62 65 20 75 6e 6b 6e 6f 77 6e 20 2a 2f st be unknown */
18940 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
18950 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f r->state>=PAGER_
18960 53 48 41 52 45 44 20 7c 7c 20 70 50 61 67 65 72 SHARED || pPager
18970 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30 ->dbSizeValid==0
18980 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 );.. /* Check
18990 74 68 61 74 20 74 68 69 73 20 69 73 20 65 69 74 that this is eit
189a0 68 65 72 20 61 20 6e 6f 2d 6f 70 20 28 62 65 63 her a no-op (bec
189b0 61 75 73 65 20 74 68 65 20 72 65 71 75 65 73 74 ause the request
189c0 65 64 20 6c 6f 63 6b 20 69 73 20 0a 20 20 2a 2a ed lock is . **
189d0 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f already held, o
189e0 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 72 61 r one of the tra
189f0 6e 73 69 73 74 69 6f 6e 73 20 74 68 61 74 20 74 nsistions that t
18a00 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a he busy-handler.
18a10 20 20 2a 2a 20 6d 61 79 20 62 65 20 69 6e 76 6f ** may be invo
18a20 6b 65 64 20 64 75 72 69 6e 67 2c 20 61 63 63 6f ked during, acco
18a30 72 64 69 6e 67 20 74 6f 20 74 68 65 20 63 6f 6d rding to the com
18a40 6d 65 6e 74 20 61 62 6f 76 65 0a 20 20 2a 2a 20 ment above. **
18a50 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 sqlite3PagerSetB
18a60 75 73 79 68 61 6e 64 6c 65 72 28 29 2e 0a 20 20 usyhandler()..
18a70 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 50 */. assert( (pP
18a80 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 6c 6f 63 ager->state>=loc
18a90 6b 74 79 70 65 29 0a 20 20 20 20 20 20 20 7c 7c ktype). ||
18aa0 20 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d (pPager->state=
18ab0 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 26 26 =PAGER_UNLOCK &&
18ac0 20 6c 6f 63 6b 74 79 70 65 3d 3d 50 41 47 45 52 locktype==PAGER
18ad0 5f 53 48 41 52 45 44 29 0a 20 20 20 20 20 20 20 _SHARED).
18ae0 7c 7c 20 28 70 50 61 67 65 72 2d 3e 73 74 61 74 || (pPager->stat
18af0 65 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 e==PAGER_RESERVE
18b00 44 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 50 D && locktype==P
18b10 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 29 0a AGER_EXCLUSIVE).
18b20 20 20 29 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 );.. if( pPag
18b30 65 72 2d 3e 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 er->state>=lockt
18b40 79 70 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ype ){. rc =
18b50 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c SQLITE_OK;. }el
18b60 73 65 7b 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 se{. do {.
18b70 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
18b80 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 sLock(pPager->fd
18b90 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 , locktype);.
18ba0 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c }while( rc==SQL
18bb0 49 54 45 5f 42 55 53 59 20 26 26 20 70 50 61 67 ITE_BUSY && pPag
18bc0 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 er->xBusyHandler
18bd0 28 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 (pPager->pBusyHa
18be0 6e 64 6c 65 72 41 72 67 29 20 29 3b 0a 20 20 20 ndlerArg) );.
18bf0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
18c00 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 OK ){. pPag
18c10 65 72 2d 3e 73 74 61 74 65 20 3d 20 28 75 38 29 er->state = (u8)
18c20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 locktype;.
18c30 49 4f 54 52 41 43 45 28 28 22 4c 4f 43 4b 20 25 IOTRACE(("LOCK %
18c40 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c p %d\n", pPager,
18c50 20 6c 6f 63 6b 74 79 70 65 29 29 0a 20 20 20 20 locktype)).
18c60 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
18c70 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 c;.}../*.** Func
18c80 74 69 6f 6e 20 61 73 73 65 72 74 54 72 75 6e 63 tion assertTrunc
18c90 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 ateConstraint(pP
18ca0 61 67 65 72 29 20 63 68 65 63 6b 73 20 74 68 61 ager) checks tha
18cb0 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 0a 2a 2a t one of the .**
18cc0 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 following is tr
18cd0 75 65 20 66 6f 72 20 61 6c 6c 20 64 69 72 74 79 ue for all dirty
18ce0 20 70 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 pages currently
18cf0 20 69 6e 20 74 68 65 20 70 61 67 65 2d 63 61 63 in the page-cac
18d00 68 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 29 20 54 he:.**.** a) T
18d10 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 he page number i
18d20 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 s less than or e
18d30 71 75 61 6c 20 74 6f 20 74 68 65 20 73 69 7a 65 qual to the size
18d40 20 6f 66 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 of the .**
18d50 20 63 75 72 72 65 6e 74 20 64 61 74 61 62 61 73 current databas
18d60 65 20 69 6d 61 67 65 2c 20 69 6e 20 70 61 67 65 e image, in page
18d70 73 2c 20 4f 52 0a 2a 2a 0a 2a 2a 20 20 20 62 29 s, OR.**.** b)
18d80 20 69 66 20 74 68 65 20 70 61 67 65 20 63 6f 6e if the page con
18d90 74 65 6e 74 20 77 65 72 65 20 77 72 69 74 74 65 tent were writte
18da0 6e 20 61 74 20 74 68 69 73 20 74 69 6d 65 2c 20 n at this time,
18db0 69 74 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 it would not.**
18dc0 20 20 20 20 20 62 65 20 6e 65 63 65 73 73 61 72 be necessar
18dd0 79 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 y to write the c
18de0 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 20 6f urrent content o
18df0 75 74 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f ut to the sub-jo
18e00 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 28 61 urnal.** (a
18e10 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 s determined by
18e20 66 75 6e 63 74 69 6f 6e 20 73 75 62 6a 52 65 71 function subjReq
18e30 75 69 72 65 73 50 61 67 65 28 29 29 2e 0a 2a 2a uiresPage())..**
18e40 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6e 64 69 .** If the condi
18e50 74 69 6f 6e 20 61 73 73 65 72 74 65 64 20 62 79 tion asserted by
18e60 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 this function w
18e70 65 72 65 20 6e 6f 74 20 74 72 75 65 2c 20 61 6e ere not true, an
18e80 64 20 74 68 65 0a 2a 2a 20 64 69 72 74 79 20 70 d the.** dirty p
18e90 61 67 65 20 77 65 72 65 20 74 6f 20 62 65 20 64 age were to be d
18ea0 69 73 63 61 72 64 65 64 20 66 72 6f 6d 20 74 68 iscarded from th
18eb0 65 20 63 61 63 68 65 20 76 69 61 20 74 68 65 20 e cache via the
18ec0 70 61 67 65 72 53 74 72 65 73 73 28 29 0a 2a 2a pagerStress().**
18ed0 20 72 6f 75 74 69 6e 65 2c 20 70 61 67 65 72 53 routine, pagerS
18ee0 74 72 65 73 73 28 29 20 77 6f 75 6c 64 20 6e 6f tress() would no
18ef0 74 20 77 72 69 74 65 20 74 68 65 20 63 75 72 72 t write the curr
18f00 65 6e 74 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 ent page content
18f10 20 74 6f 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 to.** the datab
18f20 61 73 65 20 66 69 6c 65 2e 20 49 66 20 61 20 73 ase file. If a s
18f30 61 76 65 70 6f 69 6e 74 20 74 72 61 6e 73 61 63 avepoint transac
18f40 74 69 6f 6e 20 77 65 72 65 20 72 6f 6c 6c 65 64 tion were rolled
18f50 20 62 61 63 6b 20 61 66 74 65 72 0a 2a 2a 20 74 back after.** t
18f60 68 69 73 20 68 61 70 70 65 6e 65 64 2c 20 74 68 his happened, th
18f70 65 20 63 6f 72 72 65 63 74 20 62 65 68 61 76 69 e correct behavi
18f80 6f 75 72 20 77 6f 75 6c 64 20 62 65 20 74 6f 20 our would be to
18f90 72 65 73 74 6f 72 65 20 74 68 65 20 63 75 72 72 restore the curr
18fa0 65 6e 74 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f ent.** content o
18fb0 66 20 74 68 65 20 70 61 67 65 2e 20 48 6f 77 65 f the page. Howe
18fc0 76 65 72 2c 20 73 69 6e 63 65 20 74 68 69 73 20 ver, since this
18fd0 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 70 content is not p
18fe0 72 65 73 65 6e 74 20 69 6e 20 65 69 74 68 65 72 resent in either
18ff0 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
19000 20 66 69 6c 65 20 6f 72 20 74 68 65 20 70 6f 72 file or the por
19010 74 69 6f 6e 20 6f 66 20 74 68 65 20 72 6f 6c 6c tion of the roll
19020 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 back journal and
19030 20 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c .** sub-journal
19040 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 rolled back the
19050 20 63 6f 6e 74 65 6e 74 20 63 6f 75 6c 64 20 6e content could n
19060 6f 74 20 62 65 20 72 65 73 74 6f 72 65 64 20 61 ot be restored a
19070 6e 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 nd the.** databa
19080 73 65 20 69 6d 61 67 65 20 77 6f 75 6c 64 20 62 se image would b
19090 65 63 6f 6d 65 20 63 6f 72 72 75 70 74 2e 20 49 ecome corrupt. I
190a0 74 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 66 t is therefore f
190b0 6f 72 74 75 6e 61 74 65 20 74 68 61 74 20 0a 2a ortunate that .*
190c0 2a 20 74 68 69 73 20 63 69 72 63 75 6d 73 74 61 * this circumsta
190d0 6e 63 65 20 63 61 6e 6e 6f 74 20 61 72 69 73 65 nce cannot arise
190e0 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 ..*/.#if defined
190f0 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 73 (SQLITE_DEBUG).s
19100 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 tatic void asser
19110 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 tTruncateConstra
19120 69 6e 74 43 62 28 50 67 48 64 72 20 2a 70 50 67 intCb(PgHdr *pPg
19130 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 ){. assert( pPg
19140 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
19150 52 54 59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 RTY );. assert(
19160 20 21 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 !subjRequiresPa
19170 67 65 28 70 50 67 29 20 7c 7c 20 70 50 67 2d 3e ge(pPg) || pPg->
19180 70 67 6e 6f 3c 3d 70 50 67 2d 3e 70 50 61 67 65 pgno<=pPg->pPage
19190 72 2d 3e 64 62 53 69 7a 65 20 29 3b 0a 7d 0a 73 r->dbSize );.}.s
191a0 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 tatic void asser
191b0 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 tTruncateConstra
191c0 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 int(Pager *pPage
191d0 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 r){. sqlite3Pca
191e0 63 68 65 49 74 65 72 61 74 65 44 69 72 74 79 28 cheIterateDirty(
191f0 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c pPager->pPCache,
19200 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 assertTruncateC
19210 6f 6e 73 74 72 61 69 6e 74 43 62 29 3b 0a 7d 0a onstraintCb);.}.
19220 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 61 #else.# define a
19230 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e ssertTruncateCon
19240 73 74 72 61 69 6e 74 28 70 50 61 67 65 72 29 0a straint(pPager).
19250 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 #endif../*.** Tr
19260 75 6e 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 uncate the in-me
19270 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 66 69 mory database fi
19280 6c 65 20 69 6d 61 67 65 20 74 6f 20 6e 50 61 67 le image to nPag
19290 65 20 70 61 67 65 73 2e 20 54 68 69 73 20 0a 2a e pages. This .*
192a0 2a 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 * function does
192b0 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 6d 6f 64 not actually mod
192c0 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73 65 ify the database
192d0 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 49 file on disk. I
192e0 74 20 0a 2a 2a 20 6a 75 73 74 20 73 65 74 73 20 t .** just sets
192f0 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 the internal sta
19300 74 65 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 te of the pager
19310 6f 62 6a 65 63 74 20 73 6f 20 74 68 61 74 20 74 object so that t
19320 68 65 20 0a 2a 2a 20 74 72 75 6e 63 61 74 69 6f he .** truncatio
19330 6e 20 77 69 6c 6c 20 62 65 20 64 6f 6e 65 20 77 n will be done w
19340 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 hen the current
19350 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 transaction is c
19360 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2f 0a 76 6f 69 ommitted..*/.voi
19370 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 d sqlite3PagerTr
19380 75 6e 63 61 74 65 49 6d 61 67 65 28 50 61 67 65 uncateImage(Page
19390 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 r *pPager, Pgno
193a0 6e 50 61 67 65 29 7b 0a 20 20 61 73 73 65 72 74 nPage){. assert
193b0 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 ( pPager->dbSize
193c0 56 61 6c 69 64 20 29 3b 0a 20 20 61 73 73 65 72 Valid );. asser
193d0 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a t( pPager->dbSiz
193e0 65 3e 3d 6e 50 61 67 65 20 29 3b 0a 20 20 61 73 e>=nPage );. as
193f0 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 sert( pPager->st
19400 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 ate>=PAGER_RESER
19410 56 45 44 20 29 3b 0a 20 20 70 50 61 67 65 72 2d VED );. pPager-
19420 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b >dbSize = nPage;
19430 0a 20 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 . assertTruncat
19440 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 67 eConstraint(pPag
19450 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 er);.}../*.** Sh
19460 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20 utdown the page
19470 63 61 63 68 65 2e 20 20 46 72 65 65 20 61 6c 6c cache. Free all
19480 20 6d 65 6d 6f 72 79 20 61 6e 64 20 63 6c 6f 73 memory and clos
19490 65 20 61 6c 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a e all files..**.
194a0 2a 2a 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 ** If a transact
194b0 69 6f 6e 20 77 61 73 20 69 6e 20 70 72 6f 67 72 ion was in progr
194c0 65 73 73 20 77 68 65 6e 20 74 68 69 73 20 72 6f ess when this ro
194d0 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c utine is called,
194e0 20 74 68 61 74 0a 2a 2a 20 74 72 61 6e 73 61 63 that.** transac
194f0 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 tion is rolled b
19500 61 63 6b 2e 20 20 41 6c 6c 20 6f 75 74 73 74 61 ack. All outsta
19510 6e 64 69 6e 67 20 70 61 67 65 73 20 61 72 65 20 nding pages are
19520 69 6e 76 61 6c 69 64 61 74 65 64 0a 2a 2a 20 61 invalidated.** a
19530 6e 64 20 74 68 65 69 72 20 6d 65 6d 6f 72 79 20 nd their memory
19540 69 73 20 66 72 65 65 64 2e 20 20 41 6e 79 20 61 is freed. Any a
19550 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 61 20 ttempt to use a
19560 70 61 67 65 20 61 73 73 6f 63 69 61 74 65 64 0a page associated.
19570 2a 2a 20 77 69 74 68 20 74 68 69 73 20 70 61 67 ** with this pag
19580 65 20 63 61 63 68 65 20 61 66 74 65 72 20 74 68 e cache after th
19590 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
195a0 72 6e 73 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 0a rns will likely.
195b0 2a 2a 20 72 65 73 75 6c 74 20 69 6e 20 61 20 63 ** result in a c
195c0 6f 72 65 64 75 6d 70 2e 0a 2a 2a 0a 2a 2a 20 54 oredump..**.** T
195d0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 his function alw
195e0 61 79 73 20 73 75 63 63 65 65 64 73 2e 20 49 66 ays succeeds. If
195f0 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
19600 73 20 61 63 74 69 76 65 20 61 6e 20 61 74 74 65 s active an atte
19610 6d 70 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 mpt.** is made t
19620 6f 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 o roll it back.
19630 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
19640 72 73 20 64 75 72 69 6e 67 20 74 68 65 20 72 6f rs during the ro
19650 6c 6c 62 61 63 6b 20 0a 2a 2a 20 61 20 68 6f 74 llback .** a hot
19660 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 20 62 65 20 journal may be
19670 6c 65 66 74 20 69 6e 20 74 68 65 20 66 69 6c 65 left in the file
19680 73 79 73 74 65 6d 20 62 75 74 20 6e 6f 20 65 72 system but no er
19690 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 0a ror is returned.
196a0 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 ** to the caller
196b0 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
196c0 50 61 67 65 72 43 6c 6f 73 65 28 50 61 67 65 72 PagerClose(Pager
196d0 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 64 69 73 *pPager){. dis
196e0 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 able_simulated_i
196f0 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 73 71 o_errors();. sq
19700 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e lite3BeginBenign
19710 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 70 50 61 67 Malloc();. pPag
19720 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 30 3b er->errCode = 0;
19730 0a 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 . pPager->exclu
19740 73 69 76 65 4d 6f 64 65 20 3d 20 30 3b 0a 20 20 siveMode = 0;.
19750 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 pager_reset(pPag
19760 65 72 29 3b 0a 20 20 69 66 28 20 4d 45 4d 44 42 er);. if( MEMDB
19770 20 29 7b 0a 20 20 20 20 70 61 67 65 72 5f 75 6e ){. pager_un
19780 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 lock(pPager);.
19790 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 53 65 }else{. /* Se
197a0 74 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 48 t Pager.journalH
197b0 64 72 20 74 6f 20 2d 31 20 66 6f 72 20 74 68 65 dr to -1 for the
197c0 20 62 65 6e 65 66 69 74 20 6f 66 20 74 68 65 20 benefit of the
197d0 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 pager_playback()
197e0 20 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 77 68 . ** call wh
197f0 69 63 68 20 6d 61 79 20 62 65 20 6d 61 64 65 20 ich may be made
19800 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 61 67 65 from within page
19810 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 rUnlockAndRollba
19820 63 6b 28 29 2e 20 49 66 20 69 74 0a 20 20 20 20 ck(). If it.
19830 2a 2a 20 69 73 20 6e 6f 74 20 2d 31 2c 20 74 68 ** is not -1, th
19840 65 6e 20 74 68 65 20 75 6e 73 79 6e 63 65 64 20 en the unsynced
19850 70 6f 72 74 69 6f 6e 20 6f 66 20 61 6e 20 6f 70 portion of an op
19860 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 en journal file
19870 6d 61 79 0a 20 20 20 20 2a 2a 20 62 65 20 70 6c may. ** be pl
19880 61 79 65 64 20 62 61 63 6b 20 69 6e 74 6f 20 74 ayed back into t
19890 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 he database. If
198a0 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 a power failure
198b0 6f 63 63 75 72 73 20 77 68 69 6c 65 0a 20 20 20 occurs while.
198c0 20 2a 2a 20 74 68 69 73 20 69 73 20 68 61 70 70 ** this is happ
198d0 65 6e 69 6e 67 2c 20 74 68 65 20 64 61 74 61 62 ening, the datab
198e0 61 73 65 20 6d 61 79 20 62 65 63 6f 6d 65 20 63 ase may become c
198f0 6f 72 72 75 70 74 2e 0a 20 20 20 20 2a 2f 0a 20 orrupt.. */.
19900 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
19910 61 6c 48 64 72 20 3d 20 2d 31 3b 0a 20 20 20 20 alHdr = -1;.
19920 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f pagerUnlockAndRo
19930 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a llback(pPager);.
19940 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 }. sqlite3End
19950 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
19960 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 enable_simulat
19970 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a ed_io_errors();.
19980 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 43 PAGERTRACE(("C
19990 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 50 41 47 45 LOSE %d\n", PAGE
199a0 52 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 RID(pPager)));.
199b0 20 49 4f 54 52 41 43 45 28 28 22 43 4c 4f 53 45 IOTRACE(("CLOSE
199c0 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 %p\n", pPager))
199d0 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 . sqlite3OsClos
199e0 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 e(pPager->fd);.
199f0 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 sqlite3PageFree
19a00 28 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 (pPager->pTmpSpa
19a10 63 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 ce);. sqlite3Pc
19a20 61 63 68 65 43 6c 6f 73 65 28 70 50 61 67 65 72 acheClose(pPager
19a30 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a 23 69 66 ->pPCache);..#if
19a40 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 def SQLITE_HAS_C
19a50 4f 44 45 43 0a 20 20 69 66 28 20 70 50 61 67 65 ODEC. if( pPage
19a60 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 20 29 20 r->xCodecFree )
19a70 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 pPager->xCodecFr
19a80 65 65 28 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 ee(pPager->pCode
19a90 63 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 c);.#endif.. as
19aa0 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 61 sert( !pPager->a
19ab0 53 61 76 65 70 6f 69 6e 74 20 26 26 20 21 70 50 Savepoint && !pP
19ac0 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
19ad0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69 );. assert( !i
19ae0 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
19af0 64 29 20 26 26 20 21 69 73 4f 70 65 6e 28 70 50 d) && !isOpen(pP
19b00 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 3b 0a 0a ager->sjfd) );..
19b10 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
19b20 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e Pager);. return
19b30 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 SQLITE_OK;.}..#
19b40 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 if !defined(NDEB
19b50 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 UG) || defined(S
19b60 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a QLITE_TEST)./*.*
19b70 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 67 * Return the pag
19b80 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 70 61 67 e number for pag
19b90 65 20 70 50 67 2e 0a 2a 2f 0a 50 67 6e 6f 20 73 e pPg..*/.Pgno s
19ba0 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e qlite3PagerPagen
19bb0 75 6d 62 65 72 28 44 62 50 61 67 65 20 2a 70 50 umber(DbPage *pP
19bc0 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 g){. return pPg
19bd0 2d 3e 70 67 6e 6f 3b 0a 7d 0a 23 65 6e 64 69 66 ->pgno;.}.#endif
19be0 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e ../*.** Incremen
19bf0 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 t the reference
19c00 63 6f 75 6e 74 20 66 6f 72 20 70 61 67 65 20 70 count for page p
19c10 50 67 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 Pg..*/.void sqli
19c20 74 65 33 50 61 67 65 72 52 65 66 28 44 62 50 61 te3PagerRef(DbPa
19c30 67 65 20 2a 70 50 67 29 7b 0a 20 20 73 71 6c 69 ge *pPg){. sqli
19c40 74 65 33 50 63 61 63 68 65 52 65 66 28 70 50 67 te3PcacheRef(pPg
19c50 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 );.}../*.** Sync
19c60 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 49 6e the journal. In
19c70 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 6d 61 other words, ma
19c80 6b 65 20 73 75 72 65 20 61 6c 6c 20 74 68 65 20 ke sure all the
19c90 70 61 67 65 73 20 74 68 61 74 20 68 61 76 65 0a pages that have.
19ca0 2a 2a 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 ** been written
19cb0 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 to the journal h
19cc0 61 76 65 20 61 63 74 75 61 6c 6c 79 20 72 65 61 ave actually rea
19cd0 63 68 65 64 20 74 68 65 20 73 75 72 66 61 63 65 ched the surface
19ce0 20 6f 66 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20 of the.** disk
19cf0 61 6e 64 20 63 61 6e 20 62 65 20 72 65 73 74 6f and can be resto
19d00 72 65 64 20 69 6e 20 74 68 65 20 65 76 65 6e 74 red in the event
19d10 20 6f 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 of a hot-journa
19d20 6c 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a l rollback..**.*
19d30 2a 20 49 66 20 74 68 65 20 50 61 67 65 72 2e 6e * If the Pager.n
19d40 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73 20 eedSync flag is
19d50 6e 6f 74 20 73 65 74 2c 20 74 68 65 6e 20 74 68 not set, then th
19d60 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
19d70 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20 4f 74 68 65 72 .** no-op. Other
19d80 77 69 73 65 2c 20 74 68 65 20 61 63 74 69 6f 6e wise, the action
19d90 73 20 72 65 71 75 69 72 65 64 20 64 65 70 65 6e s required depen
19da0 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c d on the journal
19db0 2d 6d 6f 64 65 0a 2a 2a 20 61 6e 64 20 74 68 65 -mode.** and the
19dc0 20 64 65 76 69 63 65 20 63 68 61 72 61 63 74 65 device characte
19dd0 72 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 74 ristics of the t
19de0 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 he file-system,
19df0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
19e00 2a 20 20 20 2a 20 49 66 20 74 68 65 20 6a 6f 75 * * If the jou
19e10 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 61 6e 20 rnal file is an
19e20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 in-memory journa
19e30 6c 20 66 69 6c 65 2c 20 6e 6f 20 61 63 74 69 6f l file, no actio
19e40 6e 20 6e 65 65 64 0a 2a 2a 20 20 20 20 20 62 65 n need.** be
19e50 20 74 61 6b 65 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 taken..**.**
19e60 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 * Otherwise, if
19e70 74 68 65 20 64 65 76 69 63 65 20 64 6f 65 73 20 the device does
19e80 6e 6f 74 20 73 75 70 70 6f 72 74 20 74 68 65 20 not support the
19e90 53 41 46 45 5f 41 50 50 45 4e 44 20 70 72 6f 70 SAFE_APPEND prop
19ea0 65 72 74 79 2c 0a 2a 2a 20 20 20 20 20 74 68 65 erty,.** the
19eb0 6e 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 n the nRec field
19ec0 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 of the most rec
19ed0 65 6e 74 6c 79 20 77 72 69 74 74 65 6e 20 6a 6f ently written jo
19ee0 75 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a 2a 20 urnal header.**
19ef0 20 20 20 20 69 73 20 75 70 64 61 74 65 64 20 74 is updated t
19f00 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 75 o contain the nu
19f10 6d 62 65 72 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 mber of journal
19f20 72 65 63 6f 72 64 73 20 74 68 61 74 20 68 61 76 records that hav
19f30 65 0a 2a 2a 20 20 20 20 20 62 65 65 6e 20 77 72 e.** been wr
19f40 69 74 74 65 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 itten following
19f50 69 74 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 it. If the pager
19f60 20 69 73 20 6f 70 65 72 61 74 69 6e 67 20 69 6e is operating in
19f70 20 66 75 6c 6c 2d 73 79 6e 63 0a 2a 2a 20 20 20 full-sync.**
19f80 20 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 mode, then the
19f90 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
19fa0 20 73 79 6e 63 65 64 20 62 65 66 6f 72 65 20 74 synced before t
19fb0 68 69 73 20 66 69 65 6c 64 20 69 73 20 75 70 64 his field is upd
19fc0 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 ated..**.** *
19fd0 49 66 20 74 68 65 20 64 65 76 69 63 65 20 64 6f If the device do
19fe0 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 74 es not support t
19ff0 68 65 20 53 45 51 55 45 4e 54 49 41 4c 20 70 72 he SEQUENTIAL pr
1a000 6f 70 65 72 74 79 2c 20 74 68 65 6e 20 0a 2a 2a operty, then .**
1a010 20 20 20 20 20 6a 6f 75 72 6e 61 6c 20 66 69 6c journal fil
1a020 65 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a e is synced..**.
1a030 2a 2a 20 4f 72 2c 20 69 6e 20 70 73 65 75 64 6f ** Or, in pseudo
1a040 2d 63 6f 64 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 -code:.**.** i
1a050 66 28 20 4e 4f 54 20 3c 69 6e 2d 6d 65 6d 6f 72 f( NOT <in-memor
1a060 79 20 6a 6f 75 72 6e 61 6c 3e 20 29 7b 0a 2a 2a y journal> ){.**
1a070 20 20 20 20 20 69 66 28 20 4e 4f 54 20 53 41 46 if( NOT SAF
1a080 45 5f 41 50 50 45 4e 44 20 29 7b 0a 2a 2a 20 20 E_APPEND ){.**
1a090 20 20 20 20 20 69 66 28 20 3c 66 75 6c 6c 2d 73 if( <full-s
1a0a0 79 6e 63 20 6d 6f 64 65 3e 20 29 20 78 53 79 6e ync mode> ) xSyn
1a0b0 63 28 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 3e c(<journal file>
1a0c0 29 3b 0a 2a 2a 20 20 20 20 20 20 20 3c 75 70 64 );.** <upd
1a0d0 61 74 65 20 6e 52 65 63 20 66 69 65 6c 64 3e 0a ate nRec field>.
1a0e0 2a 2a 20 20 20 20 20 7d 20 0a 2a 2a 20 20 20 20 ** } .**
1a0f0 20 69 66 28 20 4e 4f 54 20 53 45 51 55 45 4e 54 if( NOT SEQUENT
1a100 49 41 4c 20 29 20 78 53 79 6e 63 28 3c 6a 6f 75 IAL ) xSync(<jou
1a110 72 6e 61 6c 20 66 69 6c 65 3e 29 3b 0a 2a 2a 20 rnal file>);.**
1a120 20 20 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 61 }.**.** The Pa
1a130 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 ger.needSync fla
1a140 67 20 69 73 20 6e 65 76 65 72 20 62 65 20 73 65 g is never be se
1a150 74 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 t for temporary
1a160 66 69 6c 65 73 2c 20 6f 72 20 61 6e 79 0a 2a 2a files, or any.**
1a170 20 66 69 6c 65 20 6f 70 65 72 61 74 69 6e 67 20 file operating
1a180 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 in no-sync mode
1a190 28 50 61 67 65 72 2e 6e 6f 53 79 6e 63 20 73 65 (Pager.noSync se
1a1a0 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 29 2e 0a t to non-zero)..
1a1b0 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 **.** If success
1a1c0 66 75 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e ful, this routin
1a1d0 65 20 63 6c 65 61 72 73 20 74 68 65 20 50 47 48 e clears the PGH
1a1e0 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 DR_NEED_SYNC fla
1a1f0 67 20 6f 66 20 65 76 65 72 79 20 0a 2a 2a 20 70 g of every .** p
1a200 61 67 65 20 63 75 72 72 65 6e 74 6c 79 20 68 65 age currently he
1a210 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62 65 66 ld in memory bef
1a220 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 53 51 ore returning SQ
1a230 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61 6e 20 49 LITE_OK. If an I
1a240 4f 0a 2a 2a 20 65 72 72 6f 72 20 69 73 20 65 6e O.** error is en
1a250 63 6f 75 6e 74 65 72 65 64 2c 20 74 68 65 6e 20 countered, then
1a260 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 the IO error cod
1a270 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f e is returned to
1a280 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a the caller..*/.
1a290 73 74 61 74 69 63 20 69 6e 74 20 73 79 6e 63 4a static int syncJ
1a2a0 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 ournal(Pager *pP
1a2b0 61 67 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 ager){. if( pPa
1a2c0 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 7b ger->needSync ){
1a2d0 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 . assert( !pP
1a2e0 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 ager->tempFile )
1a2f0 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 ;. if( pPager
1a300 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 ->journalMode!=P
1a310 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
1a320 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 _MEMORY ){.
1a330 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
1a340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a350 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
1a360 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f code */. co
1a370 6e 73 74 20 69 6e 74 20 69 44 63 20 3d 20 73 71 nst int iDc = sq
1a380 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 lite3OsDeviceCha
1a390 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 racteristics(pPa
1a3a0 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20 20 ger->fd);.
1a3b0 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
1a3c0 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a Pager->jfd) );..
1a3d0 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44 if( 0==(iD
1a3e0 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 c&SQLITE_IOCAP_S
1a3f0 41 46 45 5f 41 50 50 45 4e 44 29 20 29 7b 0a 20 AFE_APPEND) ){.
1a400 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 /* This b
1a410 6c 6f 63 6b 20 64 65 61 6c 73 20 77 69 74 68 20 lock deals with
1a420 61 6e 20 6f 62 73 63 75 72 65 20 70 72 6f 62 6c an obscure probl
1a430 65 6d 2e 20 49 66 20 74 68 65 20 6c 61 73 74 20 em. If the last
1a440 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20 20 connection.
1a450 20 20 20 2a 2a 20 74 68 61 74 20 77 72 6f 74 65 ** that wrote
1a460 20 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 to this databas
1a470 65 20 77 61 73 20 6f 70 65 72 61 74 69 6e 67 20 e was operating
1a480 69 6e 20 70 65 72 73 69 73 74 65 6e 74 2d 6a 6f in persistent-jo
1a490 75 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a urnal. **
1a4a0 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 mode, then the
1a4b0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 journal file may
1a4c0 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 61 at this point a
1a4d0 63 74 75 61 6c 6c 79 20 62 65 20 6c 61 72 67 65 ctually be large
1a4e0 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 r. ** tha
1a4f0 6e 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f n Pager.journalO
1a500 66 66 20 62 79 74 65 73 2e 20 49 66 20 74 68 65 ff bytes. If the
1a510 20 6e 65 78 74 20 74 68 69 6e 67 20 69 6e 20 74 next thing in t
1a520 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 he journal.
1a530 20 20 20 2a 2a 20 66 69 6c 65 20 68 61 70 70 65 ** file happe
1a540 6e 73 20 74 6f 20 62 65 20 61 20 6a 6f 75 72 6e ns to be a journ
1a550 61 6c 2d 68 65 61 64 65 72 20 28 77 72 69 74 74 al-header (writt
1a560 65 6e 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 en as part of th
1a570 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72 65 e. ** pre
1a580 76 69 6f 75 73 20 63 6f 6e 6e 65 63 74 69 6f 6e vious connection
1a590 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 s transaction),
1a5a0 61 6e 64 20 61 20 63 72 61 73 68 20 6f 72 20 70 and a crash or p
1a5b0 6f 77 65 72 2d 66 61 69 6c 75 72 65 20 0a 20 20 ower-failure .
1a5c0 20 20 20 20 20 20 2a 2a 20 6f 63 63 75 72 73 20 ** occurs
1a5d0 61 66 74 65 72 20 6e 52 65 63 20 69 73 20 75 70 after nRec is up
1a5e0 64 61 74 65 64 20 62 75 74 20 62 65 66 6f 72 65 dated but before
1a5f0 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e this connection
1a600 20 77 72 69 74 65 73 20 0a 20 20 20 20 20 20 20 writes .
1a610 20 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 ** anything els
1a620 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c e to the journal
1a630 20 66 69 6c 65 20 28 6f 72 20 63 6f 6d 6d 69 74 file (or commit
1a640 73 2f 72 6f 6c 6c 73 20 62 61 63 6b 20 69 74 73 s/rolls back its
1a650 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 61 . ** tra
1a660 6e 73 61 63 74 69 6f 6e 29 2c 20 74 68 65 6e 20 nsaction), then
1a670 53 51 4c 69 74 65 20 6d 61 79 20 62 65 63 6f 6d SQLite may becom
1a680 65 20 63 6f 6e 66 75 73 65 64 20 77 68 65 6e 20 e confused when
1a690 64 6f 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 doing the .
1a6a0 20 20 20 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 ** hot-journa
1a6b0 6c 20 72 6f 6c 6c 62 61 63 6b 20 66 6f 6c 6c 6f l rollback follo
1a6c0 77 69 6e 67 20 72 65 63 6f 76 65 72 79 2e 20 49 wing recovery. I
1a6d0 74 20 6d 61 79 20 72 6f 6c 6c 20 62 61 63 6b 20 t may roll back
1a6e0 61 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f all. ** o
1a6f0 66 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f f this connectio
1a700 6e 73 20 64 61 74 61 2c 20 74 68 65 6e 20 70 72 ns data, then pr
1a710 6f 63 65 65 64 20 74 6f 20 72 6f 6c 6c 69 6e 67 oceed to rolling
1a720 20 62 61 63 6b 20 74 68 65 20 6f 6c 64 2c 0a 20 back the old,.
1a730 20 20 20 20 20 20 20 2a 2a 20 6f 75 74 2d 6f 66 ** out-of
1a740 2d 64 61 74 65 20 64 61 74 61 20 74 68 61 74 20 -date data that
1a750 66 6f 6c 6c 6f 77 73 20 69 74 2e 20 44 61 74 61 follows it. Data
1a760 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e base corruption.
1a770 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 . **.
1a780 20 20 20 20 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 ** To work a
1a790 72 6f 75 6e 64 20 74 68 69 73 2c 20 69 66 20 74 round this, if t
1a7a0 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1a7b0 64 6f 65 73 20 61 70 70 65 61 72 20 74 6f 20 63 does appear to c
1a7c0 6f 6e 74 61 69 6e 0a 20 20 20 20 20 20 20 20 2a ontain. *
1a7d0 2a 20 61 20 76 61 6c 69 64 20 68 65 61 64 65 72 * a valid header
1a7e0 20 66 6f 6c 6c 6f 77 69 6e 67 20 50 61 67 65 72 following Pager
1a7f0 2e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 74 68 65 .journalOff, the
1a800 6e 20 77 72 69 74 65 20 61 20 30 78 30 30 0a 20 n write a 0x00.
1a810 20 20 20 20 20 20 20 2a 2a 20 62 79 74 65 20 74 ** byte t
1a820 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 69 o the start of i
1a830 74 20 74 6f 20 70 72 65 76 65 6e 74 20 69 74 20 t to prevent it
1a840 66 72 6f 6d 20 62 65 69 6e 67 20 72 65 63 6f 67 from being recog
1a850 6e 69 7a 65 64 2e 0a 20 20 20 20 20 20 20 20 2a nized.. *
1a860 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 56 61 72 *. ** Var
1a870 69 61 62 6c 65 20 69 4e 65 78 74 48 64 72 4f 66 iable iNextHdrOf
1a880 66 73 65 74 20 69 73 20 73 65 74 20 74 6f 20 74 fset is set to t
1a890 68 65 20 6f 66 66 73 65 74 20 61 74 20 77 68 69 he offset at whi
1a8a0 63 68 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 ch this.
1a8b0 2a 2a 20 70 72 6f 62 6c 65 6d 61 74 69 63 20 68 ** problematic h
1a8c0 65 61 64 65 72 20 77 69 6c 6c 20 6f 63 63 75 72 eader will occur
1a8d0 2c 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 20 , if it exists.
1a8e0 61 4d 61 67 69 63 20 69 73 20 75 73 65 64 20 0a aMagic is used .
1a8f0 20 20 20 20 20 20 20 20 2a 2a 20 61 73 20 61 20 ** as a
1a900 74 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65 72 temporary buffer
1a910 20 74 6f 20 69 6e 73 70 65 63 74 20 74 68 65 20 to inspect the
1a920 66 69 72 73 74 20 63 6f 75 70 6c 65 20 6f 66 20 first couple of
1a930 62 79 74 65 73 20 6f 66 0a 20 20 20 20 20 20 20 bytes of.
1a940 20 2a 2a 20 74 68 65 20 70 6f 74 65 6e 74 69 61 ** the potentia
1a950 6c 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 l journal header
1a960 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
1a970 20 20 20 20 20 69 36 34 20 69 4e 65 78 74 48 64 i64 iNextHd
1a980 72 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 20 rOffset;.
1a990 20 75 38 20 61 4d 61 67 69 63 5b 38 5d 3b 0a 09 u8 aMagic[8];..
1a9a0 75 38 20 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f u8 zHeader[sizeo
1a9b0 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 f(aJournalMagic)
1a9c0 2b 34 5d 3b 0a 0a 09 6d 65 6d 63 70 79 28 7a 48 +4];...memcpy(zH
1a9d0 65 61 64 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d eader, aJournalM
1a9e0 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f agic, sizeof(aJo
1a9f0 75 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 09 70 urnalMagic));..p
1aa00 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 ut32bits(&zHeade
1aa10 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 r[sizeof(aJourna
1aa20 6c 4d 61 67 69 63 29 5d 2c 20 70 50 61 67 65 72 lMagic)], pPager
1aa30 2d 3e 6e 52 65 63 29 3b 0a 0a 20 20 20 20 20 20 ->nRec);..
1aa40 20 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 iNextHdrOffset
1aa50 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 = journalHdrOff
1aa60 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 set(pPager);.
1aa70 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1aa80 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 3OsRead(pPager->
1aa90 6a 66 64 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 jfd, aMagic, 8,
1aaa0 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 29 3b iNextHdrOffset);
1aab0 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d . if( rc=
1aac0 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d =SQLITE_OK && 0=
1aad0 3d 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 =memcmp(aMagic,
1aae0 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 38 aJournalMagic, 8
1aaf0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 ) ){. s
1ab00 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 7a tatic const u8 z
1ab10 65 72 6f 62 79 74 65 20 3d 20 30 3b 0a 20 20 20 erobyte = 0;.
1ab20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1ab30 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 te3OsWrite(pPage
1ab40 72 2d 3e 6a 66 64 2c 20 26 7a 65 72 6f 62 79 74 r->jfd, &zerobyt
1ab50 65 2c 20 31 2c 20 69 4e 65 78 74 48 64 72 4f 66 e, 1, iNextHdrOf
1ab60 66 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d fset);. }
1ab70 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
1ab80 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 =SQLITE_OK && rc
1ab90 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 !=SQLITE_IOERR_S
1aba0 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 HORT_READ ){.
1abb0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
1abc0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 ;. }..
1abd0 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 /* Write th
1abe0 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 6e 74 e nRec value int
1abf0 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 o the journal fi
1ac00 6c 65 20 68 65 61 64 65 72 2e 20 49 66 20 69 6e le header. If in
1ac10 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6c 6c . ** full
1ac20 2d 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 -synchronous mod
1ac30 65 2c 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 e, sync the jour
1ac40 6e 61 6c 20 66 69 72 73 74 2e 20 54 68 69 73 20 nal first. This
1ac50 65 6e 73 75 72 65 73 20 74 68 61 74 0a 20 20 20 ensures that.
1ac60 20 20 20 20 20 2a 2a 20 61 6c 6c 20 64 61 74 61 ** all data
1ac70 20 68 61 73 20 72 65 61 6c 6c 79 20 68 69 74 20 has really hit
1ac80 74 68 65 20 64 69 73 6b 20 62 65 66 6f 72 65 20 the disk before
1ac90 6e 52 65 63 20 69 73 20 75 70 64 61 74 65 64 20 nRec is updated
1aca0 74 6f 20 6d 61 72 6b 0a 20 20 20 20 20 20 20 20 to mark.
1acb0 2a 2a 20 69 74 20 61 73 20 61 20 63 61 6e 64 69 ** it as a candi
1acc0 64 61 74 65 20 66 6f 72 20 72 6f 6c 6c 62 61 63 date for rollbac
1acd0 6b 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 k.. **.
1ace0 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 ** This is
1acf0 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 69 66 not required if
1ad00 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 the persistent
1ad10 6d 65 64 69 61 20 73 75 70 70 6f 72 74 73 20 74 media supports t
1ad20 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 41 he. ** SA
1ad30 46 45 5f 41 50 50 45 4e 44 20 70 72 6f 70 65 72 FE_APPEND proper
1ad40 74 79 2e 20 42 65 63 61 75 73 65 20 69 6e 20 74 ty. Because in t
1ad50 68 69 73 20 63 61 73 65 20 69 74 20 69 73 20 6e his case it is n
1ad60 6f 74 20 70 6f 73 73 69 62 6c 65 20 0a 20 20 20 ot possible .
1ad70 20 20 20 20 20 2a 2a 20 66 6f 72 20 67 61 72 62 ** for garb
1ad80 61 67 65 20 64 61 74 61 20 74 6f 20 62 65 20 61 age data to be a
1ad90 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 66 ppended to the f
1ada0 69 6c 65 2c 20 74 68 65 20 6e 52 65 63 20 66 69 ile, the nRec fi
1adb0 65 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 eld. ** i
1adc0 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 s populated with
1add0 20 30 78 46 46 46 46 46 46 46 46 20 77 68 65 6e 0xFFFFFFFF when
1ade0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 the journal hea
1adf0 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 0a 20 der is written.
1ae00 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6e 65 ** and ne
1ae10 76 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 ver needs to be
1ae20 75 70 64 61 74 65 64 2e 0a 20 20 20 20 20 20 20 updated..
1ae30 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
1ae40 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 pPager->fullSync
1ae50 20 26 26 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 && 0==(iDc&SQLI
1ae60 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 TE_IOCAP_SEQUENT
1ae70 49 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 IAL) ){.
1ae80 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53 PAGERTRACE(("S
1ae90 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 YNC journal of %
1aea0 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 d\n", PAGERID(pP
1aeb0 61 67 65 72 29 29 29 3b 0a 20 20 20 20 20 20 20 ager)));.
1aec0 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 53 59 IOTRACE(("JSY
1aed0 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 NC %p\n", pPager
1aee0 29 29 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 )). rc
1aef0 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 = sqlite3OsSync(
1af00 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 pPager->jfd, pPa
1af10 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 ger->sync_flags)
1af20 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
1af30 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
1af40 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
1af50 20 20 20 7d 0a 20 20 20 20 20 20 20 20 49 4f 54 }. IOT
1af60 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 20 25 RACE(("JHDR %p %
1af70 6c 6c 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 lld\n", pPager,
1af80 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 pPager->journalH
1af90 64 72 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63 dr));. rc
1afa0 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
1afb0 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 e(. p
1afc0 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 48 65 61 Pager->jfd, zHea
1afd0 64 65 72 2c 20 73 69 7a 65 6f 66 28 7a 48 65 61 der, sizeof(zHea
1afe0 64 65 72 29 2c 20 70 50 61 67 65 72 2d 3e 6a 6f der), pPager->jo
1aff0 75 72 6e 61 6c 48 64 72 0a 09 29 3b 0a 20 20 20 urnalHdr..);.
1b000 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1b010 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 ITE_OK ) return
1b020 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
1b030 20 20 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 if( 0==(iDc&SQ
1b040 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 LITE_IOCAP_SEQUE
1b050 4e 54 49 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 NTIAL) ){.
1b060 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53 PAGERTRACE(("S
1b070 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 YNC journal of %
1b080 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 d\n", PAGERID(pP
1b090 61 67 65 72 29 29 29 3b 0a 20 20 20 20 20 20 20 ager)));.
1b0a0 20 49 4f 54 52 41 43 45 28 28 22 4a 53 59 4e 43 IOTRACE(("JSYNC
1b0b0 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 %p\n", pPager))
1b0c0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
1b0d0 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 lite3OsSync(pPag
1b0e0 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d er->jfd, pPager-
1b0f0 3e 73 79 6e 63 5f 66 6c 61 67 73 7c 20 0a 20 20 >sync_flags| .
1b100 20 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d (pPager-
1b110 3e 73 79 6e 63 5f 66 6c 61 67 73 3d 3d 53 51 4c >sync_flags==SQL
1b120 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 3f 53 51 ITE_SYNC_FULL?SQ
1b130 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e LITE_SYNC_DATAON
1b140 4c 59 3a 30 29 0a 20 20 20 20 20 20 20 20 29 3b LY:0). );
1b150 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
1b160 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 =SQLITE_OK ) ret
1b170 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
1b180 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 }.. /* Th
1b190 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 e journal file w
1b1a0 61 73 20 6a 75 73 74 20 73 75 63 63 65 73 73 66 as just successf
1b1b0 75 6c 6c 79 20 73 79 6e 63 65 64 2e 20 53 65 74 ully synced. Set
1b1c0 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 Pager.needSync
1b1d0 0a 20 20 20 20 2a 2a 20 74 6f 20 7a 65 72 6f 20 . ** to zero
1b1e0 61 6e 64 20 63 6c 65 61 72 20 74 68 65 20 50 47 and clear the PG
1b1f0 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c HDR_NEED_SYNC fl
1b200 61 67 20 6f 6e 20 61 6c 6c 20 70 61 67 65 73 73 ag on all pagess
1b210 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 .. */. pPa
1b220 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 ger->needSync =
1b230 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 0;. pPager->j
1b240 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 ournalStarted =
1b250 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 1;. sqlite3Pc
1b260 61 63 68 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 acheClearSyncFla
1b270 67 73 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 gs(pPager->pPCac
1b280 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 he);. }.. retu
1b290 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1b2a0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d ./*.** The argum
1b2b0 65 6e 74 20 69 73 20 74 68 65 20 66 69 72 73 74 ent is the first
1b2c0 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 in a linked lis
1b2d0 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 t of dirty pages
1b2e0 20 63 6f 6e 6e 65 63 74 65 64 0a 2a 2a 20 62 79 connected.** by
1b2f0 20 74 68 65 20 50 67 48 64 72 2e 70 44 69 72 74 the PgHdr.pDirt
1b300 79 20 70 6f 69 6e 74 65 72 2e 20 54 68 69 73 20 y pointer. This
1b310 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 73 20 function writes
1b320 65 61 63 68 20 6f 6e 65 20 6f 66 20 74 68 65 0a each one of the.
1b330 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 ** in-memory pag
1b340 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 74 es in the list t
1b350 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
1b360 69 6c 65 2e 20 54 68 65 20 61 72 67 75 6d 65 6e ile. The argumen
1b370 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c t may.** be NULL
1b380 2c 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 61 , representing a
1b390 6e 20 65 6d 70 74 79 20 6c 69 73 74 2e 20 49 6e n empty list. In
1b3a0 20 74 68 69 73 20 63 61 73 65 20 74 68 69 73 20 this case this
1b3b0 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 function is.** a
1b3c0 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 no-op..**.** Th
1b3d0 65 20 70 61 67 65 72 20 6d 75 73 74 20 68 6f 6c e pager must hol
1b3e0 64 20 61 74 20 6c 65 61 73 74 20 61 20 52 45 53 d at least a RES
1b3f0 45 52 56 45 44 20 6c 6f 63 6b 20 77 68 65 6e 20 ERVED lock when
1b400 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a this function.**
1b410 20 69 73 20 63 61 6c 6c 65 64 2e 20 42 65 66 6f is called. Befo
1b420 72 65 20 77 72 69 74 69 6e 67 20 61 6e 79 74 68 re writing anyth
1b430 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 ing to the datab
1b440 61 73 65 20 66 69 6c 65 2c 20 74 68 69 73 20 6c ase file, this l
1b450 6f 63 6b 0a 2a 2a 20 69 73 20 75 70 67 72 61 64 ock.** is upgrad
1b460 65 64 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 ed to an EXCLUSI
1b470 56 45 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 VE lock. If the
1b480 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20 6f lock cannot be o
1b490 62 74 61 69 6e 65 64 2c 0a 2a 2a 20 53 51 4c 49 btained,.** SQLI
1b4a0 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 TE_BUSY is retur
1b4b0 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 ned and no data
1b4c0 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 is written to th
1b4d0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
1b4e0 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 70 .** .** If the p
1b4f0 61 67 65 72 20 69 73 20 61 20 74 65 6d 70 2d 66 ager is a temp-f
1b500 69 6c 65 20 70 61 67 65 72 20 61 6e 64 20 74 68 ile pager and th
1b510 65 20 61 63 74 75 61 6c 20 66 69 6c 65 2d 73 79 e actual file-sy
1b520 73 74 65 6d 20 66 69 6c 65 0a 2a 2a 20 69 73 20 stem file.** is
1b530 6e 6f 74 20 79 65 74 20 6f 70 65 6e 2c 20 69 74 not yet open, it
1b540 20 69 73 20 63 72 65 61 74 65 64 20 61 6e 64 20 is created and
1b550 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 61 6e opened before an
1b560 79 20 64 61 74 61 20 69 73 20 0a 2a 2a 20 77 72 y data is .** wr
1b570 69 74 74 65 6e 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a itten out..**.**
1b580 20 4f 6e 63 65 20 74 68 65 20 6c 6f 63 6b 20 68 Once the lock h
1b590 61 73 20 62 65 65 6e 20 75 70 67 72 61 64 65 64 as been upgraded
1b5a0 20 61 6e 64 2c 20 69 66 20 6e 65 63 65 73 73 61 and, if necessa
1b5b0 72 79 2c 20 74 68 65 20 66 69 6c 65 20 6f 70 65 ry, the file ope
1b5c0 6e 65 64 2c 0a 2a 2a 20 74 68 65 20 70 61 67 65 ned,.** the page
1b5d0 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f 75 s are written ou
1b5e0 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 t to the databas
1b5f0 65 20 66 69 6c 65 20 69 6e 20 6c 69 73 74 20 6f e file in list o
1b600 72 64 65 72 2e 20 57 72 69 74 69 6e 67 0a 2a 2a rder. Writing.**
1b610 20 61 20 70 61 67 65 20 69 73 20 73 6b 69 70 70 a page is skipp
1b620 65 64 20 69 66 20 69 74 20 6d 65 65 74 73 20 65 ed if it meets e
1b630 69 74 68 65 72 20 6f 66 20 74 68 65 20 66 6f 6c ither of the fol
1b640 6c 6f 77 69 6e 67 20 63 72 69 74 65 72 69 61 3a lowing criteria:
1b650 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 70 .**.** * The p
1b660 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 67 72 age number is gr
1b670 65 61 74 65 72 20 74 68 61 6e 20 50 61 67 65 72 eater than Pager
1b680 2e 64 62 53 69 7a 65 2c 20 6f 72 0a 2a 2a 20 20 .dbSize, or.**
1b690 20 2a 20 54 68 65 20 50 47 48 44 52 5f 44 4f 4e * The PGHDR_DON
1b6a0 54 5f 57 52 49 54 45 20 66 6c 61 67 20 69 73 20 T_WRITE flag is
1b6b0 73 65 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2e set on the page.
1b6c0 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 69 74 69 6e .**.** If writin
1b6d0 67 20 6f 75 74 20 61 20 70 61 67 65 20 63 61 75 g out a page cau
1b6e0 73 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 ses the database
1b6f0 20 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20 50 file to grow, P
1b700 61 67 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 0a ager.dbFileSize.
1b710 2a 2a 20 69 73 20 75 70 64 61 74 65 64 20 61 63 ** is updated ac
1b720 63 6f 72 64 69 6e 67 6c 79 2e 20 49 66 20 70 61 cordingly. If pa
1b730 67 65 20 31 20 69 73 20 77 72 69 74 74 65 6e 20 ge 1 is written
1b740 6f 75 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61 out, then the va
1b750 6c 75 65 20 63 61 63 68 65 64 0a 2a 2a 20 69 6e lue cached.** in
1b760 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 Pager.dbFileVer
1b770 73 5b 5d 20 69 73 20 75 70 64 61 74 65 64 20 74 s[] is updated t
1b780 6f 20 6d 61 74 63 68 20 74 68 65 20 6e 65 77 20 o match the new
1b790 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 0a value stored in.
1b7a0 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
1b7b0 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 file..**.** If e
1b7c0 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 verything is suc
1b7d0 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f cessful, SQLITE_
1b7e0 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 OK is returned.
1b7f0 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 0a If an IO error .
1b800 2a 2a 20 6f 63 63 75 72 73 2c 20 61 6e 20 49 4f ** occurs, an IO
1b810 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
1b820 65 74 75 72 6e 65 64 2e 20 4f 72 2c 20 69 66 20 eturned. Or, if
1b830 74 68 65 20 45 58 43 4c 55 53 49 56 45 20 6c 6f the EXCLUSIVE lo
1b840 63 6b 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 ck cannot.** be
1b850 6f 62 74 61 69 6e 65 64 2c 20 53 51 4c 49 54 45 obtained, SQLITE
1b860 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 _BUSY is returne
1b870 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
1b880 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 pager_write_pag
1b890 65 6c 69 73 74 28 50 67 48 64 72 20 2a 70 4c 69 elist(PgHdr *pLi
1b8a0 73 74 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 st){. Pager *pP
1b8b0 61 67 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 ager;
1b8c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1b8d0 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 ager object */.
1b8e0 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
1b8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b900 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
1b910 63 6f 64 65 20 2a 2f 0a 0a 20 20 69 66 28 20 4e code */.. if( N
1b920 45 56 45 52 28 70 4c 69 73 74 3d 3d 30 29 20 29 EVER(pList==0) )
1b930 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1b940 4b 3b 0a 20 20 70 50 61 67 65 72 20 3d 20 70 4c K;. pPager = pL
1b950 69 73 74 2d 3e 70 50 61 67 65 72 3b 0a 0a 20 20 ist->pPager;..
1b960 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 /* At this point
1b970 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 65 69 there may be ei
1b980 74 68 65 72 20 61 20 52 45 53 45 52 56 45 44 20 ther a RESERVED
1b990 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 or EXCLUSIVE loc
1b9a0 6b 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 k on the. ** da
1b9b0 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 tabase file. If
1b9c0 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 there is already
1b9d0 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f an EXCLUSIVE lo
1b9e0 63 6b 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e ck, the followin
1b9f0 67 0a 20 20 2a 2a 20 63 61 6c 6c 20 69 73 20 61 g. ** call is a
1ba00 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a no-op.. **. *
1ba10 2a 20 4d 6f 76 69 6e 67 20 74 68 65 20 6c 6f 63 * Moving the loc
1ba20 6b 20 66 72 6f 6d 20 52 45 53 45 52 56 45 44 20 k from RESERVED
1ba30 74 6f 20 45 58 43 4c 55 53 49 56 45 20 61 63 74 to EXCLUSIVE act
1ba40 75 61 6c 6c 79 20 69 6e 76 6f 6c 76 65 73 20 67 ually involves g
1ba50 6f 69 6e 67 0a 20 20 2a 2a 20 74 68 72 6f 75 67 oing. ** throug
1ba60 68 20 61 6e 20 69 6e 74 65 72 6d 65 64 69 61 74 h an intermediat
1ba70 65 20 73 74 61 74 65 20 50 45 4e 44 49 4e 47 2e e state PENDING.
1ba80 20 20 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 A PENDING loc
1ba90 6b 20 70 72 65 76 65 6e 74 73 20 6e 65 77 0a 20 k prevents new.
1baa0 20 2a 2a 20 72 65 61 64 65 72 73 20 66 72 6f 6d ** readers from
1bab0 20 61 74 74 61 63 68 69 6e 67 20 74 6f 20 74 68 attaching to th
1bac0 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20 69 e database but i
1bad0 73 20 75 6e 73 75 66 66 69 63 69 65 6e 74 20 66 s unsufficient f
1bae0 6f 72 20 75 73 20 74 6f 0a 20 20 2a 2a 20 77 72 or us to. ** wr
1baf0 69 74 65 2e 20 20 54 68 65 20 69 64 65 61 20 6f ite. The idea o
1bb00 66 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b f a PENDING lock
1bb10 20 69 73 20 74 6f 20 70 72 65 76 65 6e 74 20 6e is to prevent n
1bb20 65 77 20 72 65 61 64 65 72 73 20 66 72 6f 6d 0a ew readers from.
1bb30 20 20 2a 2a 20 63 6f 6d 69 6e 67 20 69 6e 20 77 ** coming in w
1bb40 68 69 6c 65 20 77 65 20 77 61 69 74 20 66 6f 72 hile we wait for
1bb50 20 65 78 69 73 74 69 6e 67 20 72 65 61 64 65 72 existing reader
1bb60 73 20 74 6f 20 63 6c 65 61 72 2e 0a 20 20 2a 2a s to clear.. **
1bb70 0a 20 20 2a 2a 20 57 68 69 6c 65 20 74 68 65 20 . ** While the
1bb80 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 65 20 pager is in the
1bb90 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 RESERVED state,
1bba0 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 the original dat
1bbb0 61 62 61 73 65 20 66 69 6c 65 0a 20 20 2a 2a 20 abase file. **
1bbc0 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 is unchanged and
1bbd0 20 77 65 20 63 61 6e 20 72 6f 6c 6c 62 61 63 6b we can rollback
1bbe0 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 without having
1bbf0 74 6f 20 70 6c 61 79 62 61 63 6b 20 74 68 65 0a to playback the.
1bc00 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 69 6e 74 ** journal int
1bc10 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 o the original d
1bc20 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4f atabase file. O
1bc30 6e 63 65 20 77 65 20 74 72 61 6e 73 69 74 69 6f nce we transitio
1bc40 6e 20 74 6f 0a 20 20 2a 2a 20 45 58 43 4c 55 53 n to. ** EXCLUS
1bc50 49 56 45 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 IVE, it means th
1bc60 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1bc70 68 61 73 20 62 65 65 6e 20 63 68 61 6e 67 65 64 has been changed
1bc80 20 61 6e 64 20 61 6e 79 20 72 6f 6c 6c 62 61 63 and any rollbac
1bc90 6b 0a 20 20 2a 2a 20 77 69 6c 6c 20 72 65 71 75 k. ** will requ
1bca0 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 20 70 6c ire a journal pl
1bcb0 61 79 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 61 ayback.. */. a
1bcc0 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
1bcd0 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 tate>=PAGER_RESE
1bce0 52 56 45 44 20 29 3b 0a 20 20 72 63 20 3d 20 70 RVED );. rc = p
1bcf0 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 ager_wait_on_loc
1bd00 6b 28 70 50 61 67 65 72 2c 20 45 58 43 4c 55 53 k(pPager, EXCLUS
1bd10 49 56 45 5f 4c 4f 43 4b 29 3b 0a 0a 20 20 2f 2a IVE_LOCK);.. /*
1bd20 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 If the file is
1bd30 61 20 74 65 6d 70 2d 66 69 6c 65 20 68 61 73 20 a temp-file has
1bd40 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 not yet been ope
1bd50 6e 65 64 2c 20 6f 70 65 6e 20 69 74 20 6e 6f 77 ned, open it now
1bd60 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 . It. ** is not
1bd70 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 72 63 possible for rc
1bd80 20 74 6f 20 62 65 20 6f 74 68 65 72 20 74 68 61 to be other tha
1bd90 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 n SQLITE_OK if t
1bda0 68 69 73 20 62 72 61 6e 63 68 0a 20 20 2a 2a 20 his branch. **
1bdb0 69 73 20 74 61 6b 65 6e 2c 20 61 73 20 70 61 67 is taken, as pag
1bdc0 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 er_wait_on_lock(
1bdd0 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 ) is a no-op for
1bde0 20 74 65 6d 70 2d 66 69 6c 65 73 2e 0a 20 20 2a temp-files.. *
1bdf0 2f 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 /. if( !isOpen(
1be00 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 pPager->fd) ){.
1be10 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
1be20 72 2d 3e 74 65 6d 70 46 69 6c 65 20 26 26 20 72 r->tempFile && r
1be30 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a c==SQLITE_OK );.
1be40 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 4f 70 rc = pagerOp
1be50 65 6e 74 65 6d 70 28 70 50 61 67 65 72 2c 20 70 entemp(pPager, p
1be60 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 Pager->fd, pPage
1be70 72 2d 3e 76 66 73 46 6c 61 67 73 29 3b 0a 20 20 r->vfsFlags);.
1be80 7d 0a 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d }.. while( rc==
1be90 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 4c 69 SQLITE_OK && pLi
1bea0 73 74 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 st ){. Pgno p
1beb0 67 6e 6f 20 3d 20 70 4c 69 73 74 2d 3e 70 67 6e gno = pList->pgn
1bec0 6f 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 o;.. /* If th
1bed0 65 72 65 20 61 72 65 20 64 69 72 74 79 20 70 61 ere are dirty pa
1bee0 67 65 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 ges in the page
1bef0 63 61 63 68 65 20 77 69 74 68 20 70 61 67 65 20 cache with page
1bf00 6e 75 6d 62 65 72 73 20 67 72 65 61 74 65 72 0a numbers greater.
1bf10 20 20 20 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 ** than Page
1bf20 72 2e 64 62 53 69 7a 65 2c 20 74 68 69 73 20 6d r.dbSize, this m
1bf30 65 61 6e 73 20 73 71 6c 69 74 65 33 50 61 67 65 eans sqlite3Page
1bf40 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 29 rTruncateImage()
1bf50 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 0a 20 was called to.
1bf60 20 20 20 2a 2a 20 6d 61 6b 65 20 74 68 65 20 66 ** make the f
1bf70 69 6c 65 20 73 6d 61 6c 6c 65 72 20 28 70 72 65 ile smaller (pre
1bf80 73 75 6d 61 62 6c 79 20 62 79 20 61 75 74 6f 2d sumably by auto-
1bf90 76 61 63 75 75 6d 20 63 6f 64 65 29 2e 20 44 6f vacuum code). Do
1bfa0 20 6e 6f 74 20 77 72 69 74 65 0a 20 20 20 20 2a not write. *
1bfb0 2a 20 61 6e 79 20 73 75 63 68 20 70 61 67 65 73 * any such pages
1bfc0 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 to the file..
1bfd0 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 6c 73 6f **. ** Also
1bfe0 2c 20 64 6f 20 6e 6f 74 20 77 72 69 74 65 20 6f , do not write o
1bff0 75 74 20 61 6e 79 20 70 61 67 65 20 74 68 61 74 ut any page that
1c000 20 68 61 73 20 74 68 65 20 50 47 48 44 52 5f 44 has the PGHDR_D
1c010 4f 4e 54 5f 57 52 49 54 45 20 66 6c 61 67 0a 20 ONT_WRITE flag.
1c020 20 20 20 2a 2a 20 73 65 74 20 28 73 65 74 20 62 ** set (set b
1c030 79 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f y sqlite3PagerDo
1c040 6e 74 57 72 69 74 65 28 29 29 2e 0a 20 20 20 20 ntWrite())..
1c050 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3c */. if( pgno<
1c060 3d 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 =pPager->dbSize
1c070 26 26 20 30 3d 3d 28 70 4c 69 73 74 2d 3e 66 6c && 0==(pList->fl
1c080 61 67 73 26 50 47 48 44 52 5f 44 4f 4e 54 5f 57 ags&PGHDR_DONT_W
1c090 52 49 54 45 29 20 29 7b 0a 20 20 20 20 20 20 69 RITE) ){. i
1c0a0 36 34 20 6f 66 66 73 65 74 20 3d 20 28 70 67 6e 64 offset = (pgn
1c0b0 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 o-1)*(i64)pPager
1c0c0 2d 3e 70 61 67 65 53 69 7a 65 3b 20 20 20 2f 2a ->pageSize; /*
1c0d0 20 4f 66 66 73 65 74 20 74 6f 20 77 72 69 74 65 Offset to write
1c0e0 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a */. char *
1c0f0 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 pData;
1c100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c110 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
1c120 20 74 6f 20 77 72 69 74 65 20 2a 2f 20 20 20 20 to write */
1c130 0a 0a 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 .. /* Encod
1c140 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a e the database *
1c150 2f 0a 20 20 20 20 20 20 43 4f 44 45 43 32 28 70 /. CODEC2(p
1c160 50 61 67 65 72 2c 20 70 4c 69 73 74 2d 3e 70 44 Pager, pList->pD
1c170 61 74 61 2c 20 70 67 6e 6f 2c 20 36 2c 20 72 65 ata, pgno, 6, re
1c180 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
1c190 4d 2c 20 70 44 61 74 61 29 3b 0a 0a 20 20 20 20 M, pData);..
1c1a0 20 20 2f 2a 20 57 72 69 74 65 20 6f 75 74 20 74 /* Write out t
1c1b0 68 65 20 70 61 67 65 20 64 61 74 61 2e 20 2a 2f he page data. */
1c1c0 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1c1d0 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 te3OsWrite(pPage
1c1e0 72 2d 3e 66 64 2c 20 70 44 61 74 61 2c 20 70 50 r->fd, pData, pP
1c1f0 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 ager->pageSize,
1c200 6f 66 66 73 65 74 29 3b 0a 0a 20 20 20 20 20 20 offset);..
1c210 2f 2a 20 49 66 20 70 61 67 65 20 31 20 77 61 73 /* If page 1 was
1c220 20 6a 75 73 74 20 77 72 69 74 74 65 6e 2c 20 75 just written, u
1c230 70 64 61 74 65 20 50 61 67 65 72 2e 64 62 46 69 pdate Pager.dbFi
1c240 6c 65 56 65 72 73 20 74 6f 20 6d 61 74 63 68 0a leVers to match.
1c250 20 20 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c ** the val
1c260 75 65 20 6e 6f 77 20 73 74 6f 72 65 64 20 69 6e ue now stored in
1c270 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1c280 6c 65 2e 20 49 66 20 77 72 69 74 69 6e 67 20 74 le. If writing t
1c290 68 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 70 61 his . ** pa
1c2a0 67 65 20 63 61 75 73 65 64 20 74 68 65 20 64 61 ge caused the da
1c2b0 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 67 tabase file to g
1c2c0 72 6f 77 2c 20 75 70 64 61 74 65 20 64 62 46 69 row, update dbFi
1c2d0 6c 65 53 69 7a 65 2e 20 0a 20 20 20 20 20 20 2a leSize. . *
1c2e0 2f 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f /. if( pgno
1c2f0 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 6d ==1 ){. m
1c300 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 emcpy(&pPager->d
1c310 62 46 69 6c 65 56 65 72 73 2c 20 26 70 44 61 74 bFileVers, &pDat
1c320 61 5b 32 34 5d 2c 20 73 69 7a 65 6f 66 28 70 50 a[24], sizeof(pP
1c330 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 ager->dbFileVers
1c340 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ));. }.
1c350 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 if( pgno>pPage
1c360 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 7b r->dbFileSize ){
1c370 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d . pPager-
1c380 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d 20 70 67 >dbFileSize = pg
1c390 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 no;. }..
1c3a0 20 20 20 2f 2a 20 55 70 64 61 74 65 20 61 6e 79 /* Update any
1c3b0 20 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 73 20 backup objects
1c3c0 63 6f 70 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 copying the cont
1c3d0 65 6e 74 73 20 6f 66 20 74 68 69 73 20 70 61 67 ents of this pag
1c3e0 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c er. */. sql
1c3f0 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 65 ite3BackupUpdate
1c400 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 (pPager->pBackup
1c410 2c 20 70 67 6e 6f 2c 20 28 75 38 2a 29 70 4c 69 , pgno, (u8*)pLi
1c420 73 74 2d 3e 70 44 61 74 61 29 3b 0a 0a 20 20 20 st->pData);..
1c430 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 PAGERTRACE(("
1c440 53 54 4f 52 45 20 25 64 20 70 61 67 65 20 25 64 STORE %d page %d
1c450 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a hash(%08x)\n",.
1c460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c470 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 PAGERID(pPage
1c480 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f r), pgno, pager_
1c490 70 61 67 65 68 61 73 68 28 70 4c 69 73 74 29 29 pagehash(pList))
1c4a0 29 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 );. IOTRACE
1c4b0 28 28 22 50 47 4f 55 54 20 25 70 20 25 64 5c 6e (("PGOUT %p %d\n
1c4c0 22 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 ", pPager, pgno)
1c4d0 29 3b 0a 20 20 20 20 20 20 50 41 47 45 52 5f 49 );. PAGER_I
1c4e0 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 NCR(sqlite3_page
1c4f0 72 5f 77 72 69 74 65 64 62 5f 63 6f 75 6e 74 29 r_writedb_count)
1c500 3b 0a 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e ;. PAGER_IN
1c510 43 52 28 70 50 61 67 65 72 2d 3e 6e 57 72 69 74 CR(pPager->nWrit
1c520 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 e);. }else{.
1c530 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 PAGERTRACE(
1c540 28 22 4e 4f 53 54 4f 52 45 20 25 64 20 70 61 67 ("NOSTORE %d pag
1c550 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 e %d\n", PAGERID
1c560 28 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 29 29 (pPager), pgno))
1c570 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 ;. }.#ifdef S
1c580 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 QLITE_CHECK_PAGE
1c590 53 0a 20 20 20 20 70 4c 69 73 74 2d 3e 70 61 67 S. pList->pag
1c5a0 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61 eHash = pager_pa
1c5b0 67 65 68 61 73 68 28 70 4c 69 73 74 29 3b 0a 23 gehash(pList);.#
1c5c0 65 6e 64 69 66 0a 20 20 20 20 70 4c 69 73 74 20 endif. pList
1c5d0 3d 20 70 4c 69 73 74 2d 3e 70 44 69 72 74 79 3b = pList->pDirty;
1c5e0 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 . }.. return r
1c5f0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 c;.}../*.** Appe
1c600 6e 64 20 61 20 72 65 63 6f 72 64 20 6f 66 20 74 nd a record of t
1c610 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 he current state
1c620 20 6f 66 20 70 61 67 65 20 70 50 67 20 74 6f 20 of page pPg to
1c630 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e the sub-journal.
1c640 20 0a 2a 2a 20 49 74 20 69 73 20 74 68 65 20 63 .** It is the c
1c650 61 6c 6c 65 72 73 20 72 65 73 70 6f 6e 73 69 62 allers responsib
1c660 69 6c 69 74 79 20 74 6f 20 75 73 65 20 73 75 62 ility to use sub
1c670 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 29 20 jRequiresPage()
1c680 74 6f 20 63 68 65 63 6b 20 0a 2a 2a 20 74 68 61 to check .** tha
1c690 74 20 69 74 20 69 73 20 72 65 61 6c 6c 79 20 72 t it is really r
1c6a0 65 71 75 69 72 65 64 20 62 65 66 6f 72 65 20 63 equired before c
1c6b0 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 alling this func
1c6c0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 tion..**.** If s
1c6d0 75 63 63 65 73 73 66 75 6c 2c 20 73 65 74 20 74 uccessful, set t
1c6e0 68 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e he bit correspon
1c6f0 64 69 6e 67 20 74 6f 20 70 50 67 2d 3e 70 67 6e ding to pPg->pgn
1c700 6f 20 69 6e 20 74 68 65 20 62 69 74 76 65 63 73 o in the bitvecs
1c710 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 6f 70 65 6e .** for all open
1c720 20 73 61 76 65 70 6f 69 6e 74 73 20 62 65 66 6f savepoints befo
1c730 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a re returning..**
1c740 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
1c750 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 n returns SQLITE
1c760 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e _OK if everythin
1c770 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c g is successful,
1c780 20 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 an IO.** error
1c790 63 6f 64 65 20 69 66 20 74 68 65 20 61 74 74 65 code if the atte
1c7a0 6d 70 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 mpt to write to
1c7b0 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 the sub-journal
1c7c0 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 53 51 fails, or .** SQ
1c7d0 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 LITE_NOMEM if a
1c7e0 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 malloc fails whi
1c7f0 6c 65 20 73 65 74 74 69 6e 67 20 61 20 62 69 74 le setting a bit
1c800 20 69 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 0a in a savepoint.
1c810 2a 2a 20 62 69 74 76 65 63 2e 0a 2a 2f 0a 73 74 ** bitvec..*/.st
1c820 61 74 69 63 20 69 6e 74 20 73 75 62 6a 6f 75 72 atic int subjour
1c830 6e 61 6c 50 61 67 65 28 50 67 48 64 72 20 2a 70 nalPage(PgHdr *p
1c840 50 67 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 Pg){. int rc =
1c850 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 SQLITE_OK;. Pag
1c860 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 er *pPager = pPg
1c870 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 66 28 20 ->pPager;. if(
1c880 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 isOpen(pPager->s
1c890 6a 66 64 29 20 29 7b 0a 20 20 20 20 76 6f 69 64 jfd) ){. void
1c8a0 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 *pData = pPg->p
1c8b0 44 61 74 61 3b 0a 20 20 20 20 69 36 34 20 6f 66 Data;. i64 of
1c8c0 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6e fset = pPager->n
1c8d0 53 75 62 52 65 63 2a 28 34 2b 70 50 61 67 65 72 SubRec*(4+pPager
1c8e0 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 ->pageSize);.
1c8f0 20 63 68 61 72 20 2a 70 44 61 74 61 32 3b 0a 0a char *pData2;..
1c900 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 65 CODEC2(pPage
1c910 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 r, pData, pPg->p
1c920 67 6e 6f 2c 20 37 2c 20 72 65 74 75 72 6e 20 53 gno, 7, return S
1c930 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 QLITE_NOMEM, pDa
1c940 74 61 32 29 3b 0a 20 20 20 20 50 41 47 45 52 54 ta2);. PAGERT
1c950 52 41 43 45 28 28 22 53 54 4d 54 2d 4a 4f 55 52 RACE(("STMT-JOUR
1c960 4e 41 4c 20 25 64 20 70 61 67 65 20 25 64 5c 6e NAL %d page %d\n
1c970 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 ", PAGERID(pPage
1c980 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 3b r), pPg->pgno));
1c990 0a 20 20 0a 20 20 20 20 61 73 73 65 72 74 28 20 . . assert(
1c9a0 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 pageInJournal(pP
1c9b0 67 29 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e g) || pPg->pgno>
1c9c0 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 pPager->dbOrigSi
1c9d0 7a 65 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 77 ze );. rc = w
1c9e0 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67 65 rite32bits(pPage
1c9f0 72 2d 3e 73 6a 66 64 2c 20 6f 66 66 73 65 74 2c r->sjfd, offset,
1ca00 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 pPg->pgno);.
1ca10 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1ca20 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d OK ){. rc =
1ca30 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
1ca40 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 70 44 pPager->sjfd, pD
1ca50 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 ata2, pPager->pa
1ca60 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 74 2b 34 geSize, offset+4
1ca70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 );. }. }. i
1ca80 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1ca90 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
1caa0 6e 53 75 62 52 65 63 2b 2b 3b 0a 20 20 20 20 61 nSubRec++;. a
1cab0 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e ssert( pPager->n
1cac0 53 61 76 65 70 6f 69 6e 74 3e 30 20 29 3b 0a 20 Savepoint>0 );.
1cad0 20 20 20 72 63 20 3d 20 61 64 64 54 6f 53 61 76 rc = addToSav
1cae0 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 70 50 epointBitvecs(pP
1caf0 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 ager, pPg->pgno)
1cb00 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1cb10 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 c;.}.../*.** Thi
1cb20 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
1cb30 6c 6c 65 64 20 62 79 20 74 68 65 20 70 63 61 63 lled by the pcac
1cb40 68 65 20 6c 61 79 65 72 20 77 68 65 6e 20 69 74 he layer when it
1cb50 20 68 61 73 20 72 65 61 63 68 65 64 20 73 6f 6d has reached som
1cb60 65 0a 2a 2a 20 73 6f 66 74 20 6d 65 6d 6f 72 79 e.** soft memory
1cb70 20 6c 69 6d 69 74 2e 20 54 68 65 20 66 69 72 73 limit. The firs
1cb80 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 t argument is a
1cb90 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 50 61 67 pointer to a Pag
1cba0 65 72 20 6f 62 6a 65 63 74 0a 2a 2a 20 28 63 61 er object.** (ca
1cbb0 73 74 20 61 73 20 61 20 76 6f 69 64 2a 29 2e 20 st as a void*).
1cbc0 54 68 65 20 70 61 67 65 72 20 69 73 20 61 6c 77 The pager is alw
1cbd0 61 79 73 20 27 70 75 72 67 65 61 62 6c 65 27 20 ays 'purgeable'
1cbe0 28 6e 6f 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 (not an in-memor
1cbf0 79 0a 2a 2a 20 64 61 74 61 62 61 73 65 29 2e 20 y.** database).
1cc00 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d The second argum
1cc10 65 6e 74 20 69 73 20 61 20 72 65 66 65 72 65 6e ent is a referen
1cc20 63 65 20 74 6f 20 61 20 70 61 67 65 20 74 68 61 ce to a page tha
1cc30 74 20 69 73 20 0a 2a 2a 20 63 75 72 72 65 6e 74 t is .** current
1cc40 6c 79 20 64 69 72 74 79 20 62 75 74 20 68 61 73 ly dirty but has
1cc50 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 no outstanding
1cc60 72 65 66 65 72 65 6e 63 65 73 2e 20 54 68 65 20 references. The
1cc70 70 61 67 65 0a 2a 2a 20 69 73 20 61 6c 77 61 79 page.** is alway
1cc80 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
1cc90 68 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a 65 h the Pager obje
1cca0 63 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65 ct passed as the
1ccb0 20 66 69 72 73 74 20 0a 2a 2a 20 61 72 67 75 6d first .** argum
1ccc0 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a ent..**.** The j
1ccd0 6f 62 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 ob of this funct
1cce0 69 6f 6e 20 69 73 20 74 6f 20 6d 61 6b 65 20 70 ion is to make p
1ccf0 50 67 20 63 6c 65 61 6e 20 62 79 20 77 72 69 74 Pg clean by writ
1cd00 69 6e 67 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 ing its contents
1cd10 0a 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65 20 64 .** out to the d
1cd20 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69 66 atabase file, if
1cd30 20 70 6f 73 73 69 62 6c 65 2e 20 54 68 69 73 20 possible. This
1cd40 6d 61 79 20 69 6e 76 6f 6c 76 65 20 73 79 6e 63 may involve sync
1cd50 69 6e 67 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e ing the.** journ
1cd60 61 6c 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 al file. .**.**
1cd70 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 If successful, s
1cd80 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
1cd90 43 6c 65 61 6e 28 29 20 69 73 20 63 61 6c 6c 65 Clean() is calle
1cda0 64 20 6f 6e 20 74 68 65 20 70 61 67 65 20 61 6e d on the page an
1cdb0 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 72 d.** SQLITE_OK r
1cdc0 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 eturned. If an I
1cdd0 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 O error occurs w
1cde0 68 69 6c 65 20 74 72 79 69 6e 67 20 74 6f 20 6d hile trying to m
1cdf0 61 6b 65 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 ake the.** page
1ce00 63 6c 65 61 6e 2c 20 74 68 65 20 49 4f 20 65 72 clean, the IO er
1ce10 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
1ce20 72 6e 65 64 2e 20 49 66 20 74 68 65 20 70 61 67 rned. If the pag
1ce30 65 20 63 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20 6d e cannot be.** m
1ce40 61 64 65 20 63 6c 65 61 6e 20 66 6f 72 20 73 6f ade clean for so
1ce50 6d 65 20 6f 74 68 65 72 20 72 65 61 73 6f 6e 2c me other reason,
1ce60 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 but no error oc
1ce70 63 75 72 73 2c 20 74 68 65 6e 20 53 51 4c 49 54 curs, then SQLIT
1ce80 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 E_OK.** is retur
1ce90 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 50 63 ned by sqlite3Pc
1cea0 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 acheMakeClean()
1ceb0 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 2e 0a 2a is not called..*
1cec0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
1ced0 65 72 53 74 72 65 73 73 28 76 6f 69 64 20 2a 70 erStress(void *p
1cee0 2c 20 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 , PgHdr *pPg){.
1cef0 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
1cf00 20 28 50 61 67 65 72 20 2a 29 70 3b 0a 20 20 69 (Pager *)p;. i
1cf10 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1cf20 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 K;.. assert( pP
1cf30 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67 65 g->pPager==pPage
1cf40 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 r );. assert( p
1cf50 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
1cf60 44 49 52 54 59 20 29 3b 0a 0a 20 20 2f 2a 20 54 DIRTY );.. /* T
1cf70 68 65 20 64 6f 4e 6f 74 53 79 6e 63 20 66 6c 61 he doNotSync fla
1cf80 67 20 69 73 20 73 65 74 20 62 79 20 74 68 65 20 g is set by the
1cf90 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
1cfa0 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 68 69 e() function whi
1cfb0 6c 65 20 69 74 0a 20 20 2a 2a 20 69 73 20 6a 6f le it. ** is jo
1cfc0 75 72 6e 61 6c 6c 69 6e 67 20 61 20 73 65 74 20 urnalling a set
1cfd0 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 64 of two or more d
1cfe0 61 74 61 62 61 73 65 20 70 61 67 65 73 20 74 68 atabase pages th
1cff0 61 74 20 61 72 65 20 73 74 6f 72 65 64 0a 20 20 at are stored.
1d000 2a 2a 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 ** on the same d
1d010 69 73 6b 20 73 65 63 74 6f 72 2e 20 53 79 6e 63 isk sector. Sync
1d020 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ing the journal
1d030 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 77 is not allowed w
1d040 68 69 6c 65 0a 20 20 2a 2a 20 74 68 69 73 20 69 hile. ** this i
1d050 73 20 68 61 70 70 65 6e 69 6e 67 20 61 73 20 69 s happening as i
1d060 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 t is important t
1d070 68 61 74 20 61 6c 6c 20 6d 65 6d 62 65 72 73 20 hat all members
1d080 6f 66 20 73 75 63 68 20 61 0a 20 20 2a 2a 20 73 of such a. ** s
1d090 65 74 20 6f 66 20 70 61 67 65 73 20 61 72 65 20 et of pages are
1d0a0 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 20 74 synced to disk t
1d0b0 6f 67 65 74 68 65 72 2e 20 53 6f 2c 20 69 66 20 ogether. So, if
1d0c0 74 68 65 20 70 61 67 65 20 74 68 69 73 20 66 75 the page this fu
1d0d0 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 74 nction. ** is t
1d0e0 72 79 69 6e 67 20 74 6f 20 6d 61 6b 65 20 63 6c rying to make cl
1d0f0 65 61 6e 20 77 69 6c 6c 20 72 65 71 75 69 72 65 ean will require
1d100 20 61 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 20 a journal sync
1d110 61 6e 64 20 74 68 65 20 64 6f 4e 6f 74 53 79 6e and the doNotSyn
1d120 63 0a 20 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 c. ** flag is s
1d130 65 74 2c 20 72 65 74 75 72 6e 20 77 69 74 68 6f et, return witho
1d140 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e ut doing anythin
1d150 67 2e 20 54 68 65 20 70 63 61 63 68 65 20 6c 61 g. The pcache la
1d160 79 65 72 20 77 69 6c 6c 0a 20 20 2a 2a 20 6a 75 yer will. ** ju
1d170 73 74 20 68 61 76 65 20 74 6f 20 67 6f 20 61 68 st have to go ah
1d180 65 61 64 20 61 6e 64 20 61 6c 6c 6f 63 61 74 65 ead and allocate
1d190 20 61 20 6e 65 77 20 70 61 67 65 20 62 75 66 66 a new page buff
1d1a0 65 72 20 69 6e 73 74 65 61 64 20 6f 66 0a 20 20 er instead of.
1d1b0 2a 2a 20 72 65 75 73 69 6e 67 20 70 50 67 2e 0a ** reusing pPg..
1d1c0 20 20 2a 2a 0a 20 20 2a 2a 20 53 69 6d 69 6c 61 **. ** Simila
1d1d0 72 6c 79 2c 20 69 66 20 74 68 65 20 70 61 67 65 rly, if the page
1d1e0 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 65 6e r has already en
1d1f0 74 65 72 65 64 20 74 68 65 20 65 72 72 6f 72 20 tered the error
1d200 73 74 61 74 65 2c 20 64 6f 20 6e 6f 74 0a 20 20 state, do not.
1d210 2a 2a 20 74 72 79 20 74 6f 20 77 72 69 74 65 20 ** try to write
1d220 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
1d230 70 50 67 20 74 6f 20 64 69 73 6b 2e 0a 20 20 2a pPg to disk.. *
1d240 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 /. if( NEVER(pP
1d250 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 0a 20 ager->errCode).
1d260 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 64 6f || (pPager->do
1d270 4e 6f 74 53 79 6e 63 20 26 26 20 70 50 67 2d 3e NotSync && pPg->
1d280 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
1d290 5f 53 59 4e 43 29 0a 20 20 29 7b 0a 20 20 20 20 _SYNC). ){.
1d2a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1d2b0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 79 6e 63 ;. }.. /* Sync
1d2c0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
1d2d0 65 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a e if required. *
1d2e0 2f 0a 20 20 69 66 28 20 70 50 67 2d 3e 66 6c 61 /. if( pPg->fla
1d2f0 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
1d300 4e 43 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 NC ){. rc = s
1d310 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 yncJournal(pPage
1d320 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d r);. if( rc==
1d330 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 SQLITE_OK && pPa
1d340 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 26 26 ger->fullSync &&
1d350 20 0a 20 20 20 20 20 20 21 28 70 50 61 67 65 72 . !(pPager
1d360 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
1d370 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
1d380 5f 4d 45 4d 4f 52 59 29 20 26 26 0a 20 20 20 20 _MEMORY) &&.
1d390 20 20 21 28 73 71 6c 69 74 65 33 4f 73 44 65 76 !(sqlite3OsDev
1d3a0 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
1d3b0 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 26 53 cs(pPager->fd)&S
1d3c0 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 QLITE_IOCAP_SAFE
1d3d0 5f 41 50 50 45 4e 44 29 0a 20 20 20 20 29 7b 0a _APPEND). ){.
1d3e0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 pPager->nR
1d3f0 65 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 ec = 0;. rc
1d400 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 = writeJournalH
1d410 64 72 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 dr(pPager);.
1d420 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 }. }.. /* If t
1d430 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f he page number o
1d440 66 20 74 68 69 73 20 70 61 67 65 20 69 73 20 6c f this page is l
1d450 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 63 arger than the c
1d460 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 0a 20 urrent size of.
1d470 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 ** the database
1d480 20 69 6d 61 67 65 2c 20 69 74 20 6d 61 79 20 6e image, it may n
1d490 65 65 64 20 74 6f 20 62 65 20 77 72 69 74 74 65 eed to be writte
1d4a0 6e 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 n to the sub-jou
1d4b0 72 6e 61 6c 2e 0a 20 20 2a 2a 20 54 68 69 73 20 rnal.. ** This
1d4c0 69 73 20 62 65 63 61 75 73 65 20 74 68 65 20 63 is because the c
1d4d0 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 77 72 69 all to pager_wri
1d4e0 74 65 5f 70 61 67 65 6c 69 73 74 28 29 20 62 65 te_pagelist() be
1d4f0 6c 6f 77 20 77 69 6c 6c 20 6e 6f 74 0a 20 20 2a low will not. *
1d500 2a 20 61 63 74 75 61 6c 6c 79 20 77 72 69 74 65 * actually write
1d510 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 6c data to the fil
1d520 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a e in this case..
1d530 20 20 2a 2a 0a 20 20 2a 2a 20 43 6f 6e 73 69 64 **. ** Consid
1d540 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 er the following
1d550 20 73 65 71 75 65 6e 63 65 20 6f 66 20 65 76 65 sequence of eve
1d560 6e 74 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 nts:. **. **
1d570 20 42 45 47 49 4e 3b 0a 20 20 2a 2a 20 20 20 20 BEGIN;. **
1d580 20 3c 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 58 <journal page X
1d590 3e 0a 20 20 2a 2a 20 20 20 20 20 3c 6d 6f 64 69 >. ** <modi
1d5a0 66 79 20 70 61 67 65 20 58 3e 0a 20 20 2a 2a 20 fy page X>. **
1d5b0 20 20 20 20 53 41 56 45 50 4f 49 4e 54 20 73 70 SAVEPOINT sp
1d5c0 3b 0a 20 20 2a 2a 20 20 20 20 20 20 20 3c 73 68 ;. ** <sh
1d5d0 72 69 6e 6b 20 64 61 74 61 62 61 73 65 20 66 69 rink database fi
1d5e0 6c 65 20 74 6f 20 59 20 70 61 67 65 73 3e 0a 20 le to Y pages>.
1d5f0 20 2a 2a 20 20 20 20 20 20 20 70 61 67 65 72 53 ** pagerS
1d600 74 72 65 73 73 28 70 61 67 65 20 58 29 0a 20 20 tress(page X).
1d610 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 20 ** ROLLBACK
1d620 54 4f 20 73 70 3b 0a 20 20 2a 2a 0a 20 20 2a 2a TO sp;. **. **
1d630 20 49 66 20 28 58 3e 59 29 2c 20 74 68 65 6e 20 If (X>Y), then
1d640 77 68 65 6e 20 70 61 67 65 72 53 74 72 65 73 73 when pagerStress
1d650 20 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 is called page
1d660 58 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 77 72 X will not be wr
1d670 69 74 74 65 6e 0a 20 20 2a 2a 20 6f 75 74 20 74 itten. ** out t
1d680 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
1d690 69 6c 65 2c 20 62 75 74 20 77 69 6c 6c 20 62 65 ile, but will be
1d6a0 20 64 72 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 dropped from th
1d6b0 65 20 63 61 63 68 65 2e 20 54 68 65 6e 2c 0a 20 e cache. Then,.
1d6c0 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 ** following th
1d6d0 65 20 22 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 e "ROLLBACK TO s
1d6e0 70 22 20 73 74 61 74 65 6d 65 6e 74 2c 20 72 65 p" statement, re
1d6f0 61 64 69 6e 67 20 70 61 67 65 20 58 20 77 69 6c ading page X wil
1d700 6c 20 72 65 61 64 0a 20 20 2a 2a 20 64 61 74 61 l read. ** data
1d710 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 from the databa
1d720 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 77 69 se file. This wi
1d730 6c 6c 20 62 65 20 74 68 65 20 63 6f 70 79 20 6f ll be the copy o
1d740 66 20 70 61 67 65 20 58 20 61 73 20 69 74 0a 20 f page X as it.
1d750 20 2a 2a 20 77 61 73 20 77 68 65 6e 20 74 68 65 ** was when the
1d760 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 transaction sta
1d770 72 74 65 64 2c 20 6e 6f 74 20 61 73 20 69 74 20 rted, not as it
1d780 77 61 73 20 77 68 65 6e 20 22 53 41 56 45 50 4f was when "SAVEPO
1d790 49 4e 54 20 73 70 22 0a 20 20 2a 2a 20 77 61 73 INT sp". ** was
1d7a0 20 65 78 65 63 75 74 65 64 2e 0a 20 20 2a 2a 0a executed.. **.
1d7b0 20 20 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f ** The solutio
1d7c0 6e 20 69 73 20 74 6f 20 77 72 69 74 65 20 74 68 n is to write th
1d7d0 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 20 66 e current data f
1d7e0 6f 72 20 70 61 67 65 20 58 20 69 6e 74 6f 20 74 or page X into t
1d7f0 68 65 20 0a 20 20 2a 2a 20 73 75 62 2d 6a 6f 75 he . ** sub-jou
1d800 72 6e 61 6c 20 66 69 6c 65 20 6e 6f 77 20 28 69 rnal file now (i
1d810 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 f it is not alre
1d820 61 64 79 20 74 68 65 72 65 29 2c 20 73 6f 20 74 ady there), so t
1d830 68 61 74 20 69 74 20 77 69 6c 6c 0a 20 20 2a 2a hat it will. **
1d840 20 62 65 20 72 65 73 74 6f 72 65 64 20 74 6f 20 be restored to
1d850 69 74 73 20 63 75 72 72 65 6e 74 20 76 61 6c 75 its current valu
1d860 65 20 77 68 65 6e 20 74 68 65 20 22 52 4f 4c 4c e when the "ROLL
1d870 42 41 43 4b 20 54 4f 20 73 70 22 20 69 73 20 0a BACK TO sp" is .
1d880 20 20 2a 2a 20 65 78 65 63 75 74 65 64 2e 0a 20 ** executed..
1d890 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 */. if( NEVER(
1d8a0 0a 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 . rc==SQLIT
1d8b0 45 5f 4f 4b 20 26 26 20 70 50 67 2d 3e 70 67 6e E_OK && pPg->pgn
1d8c0 6f 3e 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 o>pPager->dbSize
1d8d0 20 26 26 20 73 75 62 6a 52 65 71 75 69 72 65 73 && subjRequires
1d8e0 50 61 67 65 28 70 50 67 29 0a 20 20 29 20 29 7b Page(pPg). ) ){
1d8f0 0a 20 20 20 20 72 63 20 3d 20 73 75 62 6a 6f 75 . rc = subjou
1d900 72 6e 61 6c 50 61 67 65 28 70 50 67 29 3b 0a 20 rnalPage(pPg);.
1d910 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 }.. /* Write t
1d920 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
1d930 68 65 20 70 61 67 65 20 6f 75 74 20 74 6f 20 74 he page out to t
1d940 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1d950 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 . */. if( rc==S
1d960 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1d970 70 50 67 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b pPg->pDirty = 0;
1d980 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f . rc = pager_
1d990 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28 70 write_pagelist(p
1d9a0 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d Pg);. }.. /* M
1d9b0 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 ark the page as
1d9c0 63 6c 65 61 6e 2e 20 2a 2f 0a 20 20 69 66 28 20 clean. */. if(
1d9d0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1d9e0 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 . PAGERTRACE(
1d9f0 28 22 53 54 52 45 53 53 20 25 64 20 70 61 67 65 ("STRESS %d page
1da00 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 %d\n", PAGERID(
1da10 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 pPager), pPg->pg
1da20 6e 6f 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 no));. sqlite
1da30 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 3PcacheMakeClean
1da40 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 (pPg);. }.. re
1da50 74 75 72 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 turn pager_error
1da60 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 7d 0a (pPager, rc);.}.
1da70 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 ../*.** Allocate
1da80 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 and initialize
1da90 61 20 6e 65 77 20 50 61 67 65 72 20 6f 62 6a 65 a new Pager obje
1daa0 63 74 20 61 6e 64 20 70 75 74 20 61 20 70 6f 69 ct and put a poi
1dab0 6e 74 65 72 20 74 6f 20 69 74 0a 2a 2a 20 69 6e nter to it.** in
1dac0 20 2a 70 70 50 61 67 65 72 2e 20 54 68 65 20 70 *ppPager. The p
1dad0 61 67 65 72 20 73 68 6f 75 6c 64 20 65 76 65 6e ager should even
1dae0 74 75 61 6c 6c 79 20 62 65 20 66 72 65 65 64 20 tually be freed
1daf0 62 79 20 70 61 73 73 69 6e 67 20 69 74 0a 2a 2a by passing it.**
1db00 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 to sqlite3Pager
1db10 43 6c 6f 73 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 Close()..**.** T
1db20 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 61 72 67 he zFilename arg
1db30 75 6d 65 6e 74 20 69 73 20 74 68 65 20 70 61 74 ument is the pat
1db40 68 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 h to the databas
1db50 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 2e 0a e file to open..
1db60 2a 2a 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 ** If zFilename
1db70 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 61 20 72 is NULL then a r
1db80 61 6e 64 6f 6d 6c 79 2d 6e 61 6d 65 64 20 74 65 andomly-named te
1db90 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 69 73 20 mporary file is
1dba0 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64 20 75 created.** and u
1dbb0 73 65 64 20 61 73 20 74 68 65 20 66 69 6c 65 20 sed as the file
1dbc0 74 6f 20 62 65 20 63 61 63 68 65 64 2e 20 54 65 to be cached. Te
1dbd0 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 61 72 mporary files ar
1dbe0 65 20 62 65 20 64 65 6c 65 74 65 64 0a 2a 2a 20 e be deleted.**
1dbf0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77 68 automatically wh
1dc00 65 6e 20 74 68 65 79 20 61 72 65 20 63 6c 6f 73 en they are clos
1dc10 65 64 2e 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 ed. If zFilename
1dc20 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 74 is ":memory:" t
1dc30 68 65 6e 20 0a 2a 2a 20 61 6c 6c 20 69 6e 66 6f hen .** all info
1dc40 72 6d 61 74 69 6f 6e 20 69 73 20 68 65 6c 64 20 rmation is held
1dc50 69 6e 20 63 61 63 68 65 2e 20 49 74 20 69 73 20 in cache. It is
1dc60 6e 65 76 65 72 20 77 72 69 74 74 65 6e 20 74 6f never written to
1dc70 20 64 69 73 6b 2e 20 0a 2a 2a 20 54 68 69 73 20 disk. .** This
1dc80 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 69 can be used to i
1dc90 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d mplement an in-m
1dca0 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e 0a emory database..
1dcb0 2a 2a 0a 2a 2a 20 54 68 65 20 6e 45 78 74 72 61 **.** The nExtra
1dcc0 20 70 61 72 61 6d 65 74 65 72 20 73 70 65 63 69 parameter speci
1dcd0 66 69 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 fies the number
1dce0 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 of bytes of spac
1dcf0 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 61 e allocated.** a
1dd00 6c 6f 6e 67 20 77 69 74 68 20 65 61 63 68 20 70 long with each p
1dd10 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 20 54 age reference. T
1dd20 68 69 73 20 73 70 61 63 65 20 69 73 20 61 76 61 his space is ava
1dd30 69 6c 61 62 6c 65 20 74 6f 20 74 68 65 20 75 73 ilable to the us
1dd40 65 72 0a 2a 2a 20 76 69 61 20 74 68 65 20 73 71 er.** via the sq
1dd50 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 lite3PagerGetExt
1dd60 72 61 28 29 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 ra() API..**.**
1dd70 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 The flags argume
1dd80 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 70 nt is used to sp
1dd90 65 63 69 66 79 20 70 72 6f 70 65 72 74 69 65 73 ecify properties
1dda0 20 74 68 61 74 20 61 66 66 65 63 74 20 74 68 65 that affect the
1ddb0 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 .** operation of
1ddc0 20 74 68 65 20 70 61 67 65 72 2e 20 49 74 20 73 the pager. It s
1ddd0 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 hould be passed
1dde0 73 6f 6d 65 20 62 69 74 77 69 73 65 20 63 6f 6d some bitwise com
1ddf0 62 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 bination.** of t
1de00 68 65 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f he PAGER_OMIT_JO
1de10 55 52 4e 41 4c 20 61 6e 64 20 50 41 47 45 52 5f URNAL and PAGER_
1de20 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 66 6c 61 67 NO_READLOCK flag
1de30 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 66 73 s..**.** The vfs
1de40 46 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 Flags parameter
1de50 69 73 20 61 20 62 69 74 6d 61 73 6b 20 74 6f 20 is a bitmask to
1de60 70 61 73 73 20 74 6f 20 74 68 65 20 66 6c 61 67 pass to the flag
1de70 73 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6f s parameter.** o
1de80 66 20 74 68 65 20 78 4f 70 65 6e 28 29 20 6d 65 f the xOpen() me
1de90 74 68 6f 64 20 6f 66 20 74 68 65 20 73 75 70 70 thod of the supp
1dea0 6c 69 65 64 20 56 46 53 20 77 68 65 6e 20 6f 70 lied VFS when op
1deb0 65 6e 69 6e 67 20 66 69 6c 65 73 2e 20 0a 2a 2a ening files. .**
1dec0 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 .** If the pager
1ded0 20 6f 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f 63 object is alloc
1dee0 61 74 65 64 20 61 6e 64 20 74 68 65 20 73 70 65 ated and the spe
1def0 63 69 66 69 65 64 20 66 69 6c 65 20 6f 70 65 6e cified file open
1df00 65 64 20 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 ed .** successfu
1df10 6c 6c 79 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 lly, SQLITE_OK i
1df20 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a s returned and *
1df30 70 70 50 61 67 65 72 20 73 65 74 20 74 6f 20 70 ppPager set to p
1df40 6f 69 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 6e oint to.** the n
1df50 65 77 20 70 61 67 65 72 20 6f 62 6a 65 63 74 2e ew pager object.
1df60 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 If an error occ
1df70 75 72 73 2c 20 2a 70 70 50 61 67 65 72 20 69 73 urs, *ppPager is
1df80 20 73 65 74 20 74 6f 20 4e 55 4c 4c 0a 2a 2a 20 set to NULL.**
1df90 61 6e 64 20 65 72 72 6f 72 20 63 6f 64 65 20 72 and error code r
1dfa0 65 74 75 72 6e 65 64 2e 20 54 68 69 73 20 66 75 eturned. This fu
1dfb0 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 72 nction may retur
1dfc0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a n SQLITE_NOMEM.*
1dfd0 2a 20 28 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 * (sqlite3Malloc
1dfe0 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61 6c () is used to al
1dff0 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 29 2c 20 locate memory),
1e000 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 SQLITE_CANTOPEN
1e010 6f 72 20 0a 2a 2a 20 76 61 72 69 6f 75 73 20 53 or .** various S
1e020 51 4c 49 54 45 5f 49 4f 5f 58 58 58 20 65 72 72 QLITE_IO_XXX err
1e030 6f 72 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 ors..*/.int sqli
1e040 74 65 33 50 61 67 65 72 4f 70 65 6e 28 0a 20 20 te3PagerOpen(.
1e050 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
1e060 73 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 s, /* The
1e070 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73 virtual file sys
1e080 74 65 6d 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 tem to use */.
1e090 50 61 67 65 72 20 2a 2a 70 70 50 61 67 65 72 2c Pager **ppPager,
1e0a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a /* OUT:
1e0b0 20 52 65 74 75 72 6e 20 74 68 65 20 50 61 67 65 Return the Page
1e0c0 72 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65 r structure here
1e0d0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
1e0e0 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f *zFilename, /
1e0f0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 * Name of the da
1e100 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f tabase file to o
1e110 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 pen */. int nEx
1e120 74 72 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 tra,
1e130 20 20 2f 2a 20 45 78 74 72 61 20 62 79 74 65 73 /* Extra bytes
1e140 20 61 70 70 65 6e 64 20 74 6f 20 65 61 63 68 20 append to each
1e150 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 2a in-memory page *
1e160 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 /. int flags,
1e170 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e180 66 6c 61 67 73 20 63 6f 6e 74 72 6f 6c 6c 69 6e flags controllin
1e190 67 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 g this file */.
1e1a0 20 69 6e 74 20 76 66 73 46 6c 61 67 73 2c 20 20 int vfsFlags,
1e1b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 /* fla
1e1c0 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 gs passed throug
1e1d0 68 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 h to sqlite3_vfs
1e1e0 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 76 6f .xOpen() */. vo
1e1f0 69 64 20 28 2a 78 52 65 69 6e 69 74 29 28 44 62 id (*xReinit)(Db
1e200 50 61 67 65 2a 29 20 2f 2a 20 46 75 6e 63 74 69 Page*) /* Functi
1e210 6f 6e 20 74 6f 20 72 65 69 6e 69 74 69 61 6c 69 on to reinitiali
1e220 7a 65 20 70 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 ze pages */.){.
1e230 20 75 38 20 2a 70 50 74 72 3b 0a 20 20 50 61 67 u8 *pPtr;. Pag
1e240 65 72 20 2a 70 50 61 67 65 72 20 3d 20 30 3b 20 er *pPager = 0;
1e250 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 6f /* Pager o
1e260 62 6a 65 63 74 20 74 6f 20 61 6c 6c 6f 63 61 74 bject to allocat
1e270 65 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a e and return */.
1e280 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1e290 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 52 65 E_OK; /* Re
1e2a0 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 turn code */. i
1e2b0 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d 20 30 3b nt tempFile = 0;
1e2c0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
1e2d0 66 6f 72 20 74 65 6d 70 20 66 69 6c 65 73 20 28 for temp files (
1e2e0 69 6e 63 6c 2e 20 69 6e 2d 6d 65 6d 6f 72 79 20 incl. in-memory
1e2f0 66 69 6c 65 73 29 20 2a 2f 0a 20 20 69 6e 74 20 files) */. int
1e300 6d 65 6d 44 62 20 3d 20 30 3b 20 20 20 20 20 20 memDb = 0;
1e310 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
1e320 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d 6d 65 this is an in-me
1e330 6d 6f 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20 69 mory file */. i
1e340 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d 20 30 3b nt readOnly = 0;
1e350 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
1e360 69 66 20 74 68 69 73 20 69 73 20 61 20 72 65 61 if this is a rea
1e370 64 2d 6f 6e 6c 79 20 66 69 6c 65 20 2a 2f 0a 20 d-only file */.
1e380 20 69 6e 74 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 int journalFile
1e390 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 42 79 74 Size; /* Byt
1e3a0 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 66 es to allocate f
1e3b0 6f 72 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 or each journal
1e3c0 66 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 50 fd */. char *zP
1e3d0 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20 20 20 20 athname = 0;
1e3e0 20 2f 2a 20 46 75 6c 6c 20 70 61 74 68 20 74 6f /* Full path to
1e3f0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
1e400 2f 0a 20 20 69 6e 74 20 6e 50 61 74 68 6e 61 6d /. int nPathnam
1e410 65 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 e = 0; /*
1e420 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1e430 69 6e 20 7a 50 61 74 68 6e 61 6d 65 20 2a 2f 0a in zPathname */.
1e440 20 20 69 6e 74 20 75 73 65 4a 6f 75 72 6e 61 6c int useJournal
1e450 20 3d 20 28 66 6c 61 67 73 20 26 20 50 41 47 45 = (flags & PAGE
1e460 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 29 3d R_OMIT_JOURNAL)=
1e470 3d 30 3b 20 2f 2a 20 46 61 6c 73 65 20 74 6f 20 =0; /* False to
1e480 6f 6d 69 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a omit journal */.
1e490 20 20 69 6e 74 20 6e 6f 52 65 61 64 6c 6f 63 6b int noReadlock
1e4a0 20 3d 20 28 66 6c 61 67 73 20 26 20 50 41 47 45 = (flags & PAGE
1e4b0 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 29 21 3d R_NO_READLOCK)!=
1e4c0 30 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 0; /* True to o
1e4d0 6d 69 74 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f mit read-lock */
1e4e0 0a 20 20 69 6e 74 20 70 63 61 63 68 65 53 69 7a . int pcacheSiz
1e4f0 65 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 e = sqlite3Pcach
1e500 65 53 69 7a 65 28 29 3b 20 20 20 20 20 20 20 2f eSize(); /
1e510 2a 20 42 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63 * Bytes to alloc
1e520 61 74 65 20 66 6f 72 20 50 43 61 63 68 65 20 2a ate for PCache *
1e530 2f 0a 20 20 75 31 36 20 73 7a 50 61 67 65 44 66 /. u16 szPageDf
1e540 6c 74 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 lt = SQLITE_DEFA
1e550 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 20 20 ULT_PAGE_SIZE;
1e560 2f 2a 20 44 65 66 61 75 6c 74 20 70 61 67 65 20 /* Default page
1e570 73 69 7a 65 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 size */.. /* Fi
1e580 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 75 63 gure out how muc
1e590 68 20 73 70 61 63 65 20 69 73 20 72 65 71 75 69 h space is requi
1e5a0 72 65 64 20 66 6f 72 20 65 61 63 68 20 6a 6f 75 red for each jou
1e5b0 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e 64 6c 65 rnal file-handle
1e5c0 0a 20 20 2a 2a 20 28 74 68 65 72 65 20 61 72 65 . ** (there are
1e5d0 20 74 77 6f 20 6f 66 20 74 68 65 6d 2c 20 74 68 two of them, th
1e5e0 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 61 e main journal a
1e5f0 6e 64 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e nd the sub-journ
1e600 61 6c 29 2e 20 54 68 69 73 0a 20 20 2a 2a 20 69 al). This. ** i
1e610 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 73 70 s the maximum sp
1e620 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 ace required for
1e630 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f an in-memory jo
1e640 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c urnal file handl
1e650 65 20 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 65 e . ** and a re
1e660 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c 20 66 69 gular journal fi
1e670 6c 65 2d 68 61 6e 64 6c 65 2e 20 4e 6f 74 65 20 le-handle. Note
1e680 74 68 61 74 20 61 20 22 72 65 67 75 6c 61 72 20 that a "regular
1e690 6a 6f 75 72 6e 61 6c 2d 68 61 6e 64 6c 65 22 0a journal-handle".
1e6a0 20 20 2a 2a 20 6d 61 79 20 62 65 20 61 20 77 72 ** may be a wr
1e6b0 61 70 70 65 72 20 63 61 70 61 62 6c 65 20 6f 66 apper capable of
1e6c0 20 63 61 63 68 69 6e 67 20 74 68 65 20 66 69 72 caching the fir
1e6d0 73 74 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 st portion of th
1e6e0 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 e journal. ** f
1e6f0 69 6c 65 20 69 6e 20 6d 65 6d 6f 72 79 20 74 6f ile in memory to
1e700 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 61 implement the a
1e710 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 tomic-write opti
1e720 6d 69 7a 61 74 69 6f 6e 20 28 73 65 65 20 0a 20 mization (see .
1e730 20 2a 2a 20 73 6f 75 72 63 65 20 66 69 6c 65 20 ** source file
1e740 6a 6f 75 72 6e 61 6c 2e 63 29 2e 0a 20 20 2a 2f journal.c).. */
1e750 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4a 6f . if( sqlite3Jo
1e760 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 3e urnalSize(pVfs)>
1e770 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 sqlite3MemJourna
1e780 6c 53 69 7a 65 28 29 20 29 7b 0a 20 20 20 20 6a lSize() ){. j
1e790 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 3d ournalFileSize =
1e7a0 20 52 4f 55 4e 44 38 28 73 71 6c 69 74 65 33 4a ROUND8(sqlite3J
1e7b0 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 ournalSize(pVfs)
1e7c0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1e7d0 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 journalFileSize
1e7e0 3d 20 52 4f 55 4e 44 38 28 73 71 6c 69 74 65 33 = ROUND8(sqlite3
1e7f0 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 MemJournalSize()
1e800 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 );. }.. /* Set
1e810 20 74 68 65 20 6f 75 74 70 75 74 20 76 61 72 69 the output vari
1e820 61 62 6c 65 20 74 6f 20 4e 55 4c 4c 20 69 6e 20 able to NULL in
1e830 63 61 73 65 20 61 6e 20 65 72 72 6f 72 20 6f 63 case an error oc
1e840 63 75 72 73 2e 20 2a 2f 0a 20 20 2a 70 70 50 61 curs. */. *ppPa
1e850 67 65 72 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43 ger = 0;.. /* C
1e860 6f 6d 70 75 74 65 20 61 6e 64 20 73 74 6f 72 65 ompute and store
1e870 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 the full pathna
1e880 6d 65 20 69 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 me in an allocat
1e890 65 64 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 ed buffer pointe
1e8a0 64 0a 20 20 2a 2a 20 74 6f 20 62 79 20 7a 50 61 d. ** to by zPa
1e8b0 74 68 6e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e thname, length n
1e8c0 50 61 74 68 6e 61 6d 65 2e 20 4f 72 2c 20 69 66 Pathname. Or, if
1e8d0 20 74 68 69 73 20 69 73 20 61 20 74 65 6d 70 6f this is a tempo
1e8e0 72 61 72 79 20 66 69 6c 65 2c 0a 20 20 2a 2a 20 rary file,. **
1e8f0 6c 65 61 76 65 20 62 6f 74 68 20 6e 50 61 74 68 leave both nPath
1e900 6e 61 6d 65 20 61 6e 64 20 7a 50 61 74 68 6e 61 name and zPathna
1e910 6d 65 20 73 65 74 20 74 6f 20 30 2e 0a 20 20 2a me set to 0.. *
1e920 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d /. if( zFilenam
1e930 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 e && zFilename[0
1e940 5d 20 29 7b 0a 20 20 20 20 6e 50 61 74 68 6e 61 ] ){. nPathna
1e950 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 me = pVfs->mxPat
1e960 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 7a 50 61 hname+1;. zPa
1e970 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 thname = sqlite3
1e980 4d 61 6c 6c 6f 63 28 6e 50 61 74 68 6e 61 6d 65 Malloc(nPathname
1e990 2a 32 29 3b 0a 20 20 20 20 69 66 28 20 7a 50 61 *2);. if( zPa
1e9a0 74 68 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 thname==0 ){.
1e9b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1e9c0 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 23 69 _NOMEM;. }.#i
1e9d0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1e9e0 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 69 T_MEMORYDB. i
1e9f0 66 28 20 73 74 72 63 6d 70 28 7a 46 69 6c 65 6e f( strcmp(zFilen
1ea00 61 6d 65 2c 22 3a 6d 65 6d 6f 72 79 3a 22 29 3d ame,":memory:")=
1ea10 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 44 =0 ){. memD
1ea20 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 7a 50 61 b = 1;. zPa
1ea30 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b 0a 20 thname[0] = 0;.
1ea40 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a }else.#endif.
1ea50 20 20 20 20 7b 0a 20 20 20 20 20 20 7a 50 61 74 {. zPat
1ea60 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b 20 2f 2a hname[0] = 0; /*
1ea70 20 4d 61 6b 65 20 73 75 72 65 20 69 6e 69 74 69 Make sure initi
1ea80 61 6c 69 7a 65 64 20 65 76 65 6e 20 69 66 20 46 alized even if F
1ea90 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 20 66 61 ullPathname() fa
1eaa0 69 6c 73 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 ils */. rc
1eab0 3d 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 = sqlite3OsFullP
1eac0 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46 athname(pVfs, zF
1ead0 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 ilename, nPathna
1eae0 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 29 3b 0a me, zPathname);.
1eaf0 20 20 20 20 7d 0a 0a 20 20 20 20 6e 50 61 74 68 }.. nPath
1eb00 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 name = sqlite3St
1eb10 72 6c 65 6e 33 30 28 7a 50 61 74 68 6e 61 6d 65 rlen30(zPathname
1eb20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
1eb30 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 50 61 74 QLITE_OK && nPat
1eb40 68 6e 61 6d 65 2b 38 3e 70 56 66 73 2d 3e 6d 78 hname+8>pVfs->mx
1eb50 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 Pathname ){.
1eb60 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68 /* This branch
1eb70 20 69 73 20 74 61 6b 65 6e 20 77 68 65 6e 20 74 is taken when t
1eb80 68 65 20 6a 6f 75 72 6e 61 6c 20 70 61 74 68 20 he journal path
1eb90 72 65 71 75 69 72 65 64 20 62 79 0a 20 20 20 20 required by.
1eba0 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 ** the databas
1ebb0 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 77 e being opened w
1ebc0 69 6c 6c 20 62 65 20 6d 6f 72 65 20 74 68 61 6e ill be more than
1ebd0 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d pVfs->mxPathnam
1ebe0 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 e. ** bytes
1ebf0 20 69 6e 20 6c 65 6e 67 74 68 2e 20 54 68 69 73 in length. This
1ec00 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 means the datab
1ec10 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 6f 70 ase cannot be op
1ec20 65 6e 65 64 2c 0a 20 20 20 20 20 20 2a 2a 20 61 ened,. ** a
1ec30 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 s it will not be
1ec40 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6f 70 65 possible to ope
1ec50 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
1ec60 6c 65 20 6f 72 20 65 76 65 6e 0a 20 20 20 20 20 le or even.
1ec70 20 2a 2a 20 63 68 65 63 6b 20 66 6f 72 20 61 20 ** check for a
1ec80 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 65 66 6f hot-journal befo
1ec90 72 65 20 72 65 61 64 69 6e 67 2e 0a 20 20 20 20 re reading..
1eca0 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 */. rc =
1ecb0 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b SQLITE_CANTOPEN;
1ecc0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 . }. if( r
1ecd0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1ece0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
1ecf0 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 ee(zPathname);.
1ed00 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1ed10 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
1ed20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 Allocate memory
1ed30 66 6f 72 20 74 68 65 20 50 61 67 65 72 20 73 74 for the Pager st
1ed40 72 75 63 74 75 72 65 2c 20 50 43 61 63 68 65 20 ructure, PCache
1ed50 6f 62 6a 65 63 74 2c 20 74 68 65 0a 20 20 2a 2a object, the. **
1ed60 20 74 68 72 65 65 20 66 69 6c 65 20 64 65 73 63 three file desc
1ed70 72 69 70 74 6f 72 73 2c 20 74 68 65 20 64 61 74 riptors, the dat
1ed80 61 62 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 20 abase file name
1ed90 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 and the journal
1eda0 0a 20 20 2a 2a 20 66 69 6c 65 20 6e 61 6d 65 2e . ** file name.
1edb0 20 54 68 65 20 6c 61 79 6f 75 74 20 69 6e 20 6d The layout in m
1edc0 65 6d 6f 72 79 20 69 73 20 61 73 20 66 6f 6c 6c emory is as foll
1edd0 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 ows:. **. **
1ede0 20 20 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 Pager object
1edf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ee00 20 20 20 28 73 69 7a 65 6f 66 28 50 61 67 65 72 (sizeof(Pager
1ee10 29 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 ) bytes). **
1ee20 20 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 20 PCache object
1ee30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ee40 20 20 28 73 71 6c 69 74 65 33 50 63 61 63 68 65 (sqlite3Pcache
1ee50 53 69 7a 65 28 29 20 62 79 74 65 73 29 0a 20 20 Size() bytes).
1ee60 2a 2a 20 20 20 20 20 44 61 74 61 62 61 73 65 20 ** Database
1ee70 66 69 6c 65 20 68 61 6e 64 6c 65 20 20 20 20 20 file handle
1ee80 20 20 20 20 20 20 20 28 70 56 66 73 2d 3e 73 7a (pVfs->sz
1ee90 4f 73 46 69 6c 65 20 62 79 74 65 73 29 0a 20 20 OsFile bytes).
1eea0 2a 2a 20 20 20 20 20 53 75 62 2d 6a 6f 75 72 6e ** Sub-journ
1eeb0 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 20 al file handle
1eec0 20 20 20 20 20 20 20 28 6a 6f 75 72 6e 61 6c 46 (journalF
1eed0 69 6c 65 53 69 7a 65 20 62 79 74 65 73 29 0a 20 ileSize bytes).
1eee0 20 2a 2a 20 20 20 20 20 4d 61 69 6e 20 6a 6f 75 ** Main jou
1eef0 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c 65 rnal file handle
1ef00 20 20 20 20 20 20 20 20 28 6a 6f 75 72 6e 61 6c (journal
1ef10 46 69 6c 65 53 69 7a 65 20 62 79 74 65 73 29 0a FileSize bytes).
1ef20 20 20 2a 2a 20 20 20 20 20 44 61 74 61 62 61 73 ** Databas
1ef30 65 20 66 69 6c 65 20 6e 61 6d 65 20 20 20 20 20 e file name
1ef40 20 20 20 20 20 20 20 20 20 28 6e 50 61 74 68 6e (nPathn
1ef50 61 6d 65 2b 31 20 62 79 74 65 73 29 0a 20 20 2a ame+1 bytes). *
1ef60 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20 66 69 * Journal fi
1ef70 6c 65 20 6e 61 6d 65 20 20 20 20 20 20 20 20 20 le name
1ef80 20 20 20 20 20 20 28 6e 50 61 74 68 6e 61 6d 65 (nPathname
1ef90 2b 38 2b 31 20 62 79 74 65 73 29 0a 20 20 2a 2f +8+1 bytes). */
1efa0 0a 20 20 70 50 74 72 20 3d 20 28 75 38 20 2a 29 . pPtr = (u8 *)
1efb0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
1efc0 6f 28 0a 20 20 20 20 52 4f 55 4e 44 38 28 73 69 o(. ROUND8(si
1efd0 7a 65 6f 66 28 2a 70 50 61 67 65 72 29 29 20 2b zeof(*pPager)) +
1efe0 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 73 /* Pager s
1eff0 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 20 tructure */.
1f000 52 4f 55 4e 44 38 28 70 63 61 63 68 65 53 69 7a ROUND8(pcacheSiz
1f010 65 29 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f e) + /
1f020 2a 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 20 * PCache object
1f030 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38 28 70 56 */. ROUND8(pV
1f040 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 20 2b 20 fs->szOsFile) +
1f050 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 /* The mai
1f060 6e 20 64 62 20 66 69 6c 65 20 2a 2f 0a 20 20 20 n db file */.
1f070 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 journalFileSize
1f080 20 2a 20 32 20 2b 20 20 20 20 20 20 20 20 20 20 * 2 +
1f090 2f 2a 20 54 68 65 20 74 77 6f 20 6a 6f 75 72 6e /* The two journ
1f0a0 61 6c 20 66 69 6c 65 73 20 2a 2f 20 0a 20 20 20 al files */ .
1f0b0 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20 31 20 2b nPathname + 1 +
1f0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f0d0 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 2a 2f 0a /* zFilename */.
1f0e0 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20 nPathname +
1f0f0 38 20 2b 20 31 20 20 20 20 20 20 20 20 20 20 20 8 + 1
1f100 20 20 20 2f 2a 20 7a 4a 6f 75 72 6e 61 6c 20 2a /* zJournal *
1f110 2f 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 /. );. assert(
1f120 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 EIGHT_BYTE_ALIG
1f130 4e 4d 45 4e 54 28 53 51 4c 49 54 45 5f 49 4e 54 NMENT(SQLITE_INT
1f140 5f 54 4f 5f 50 54 52 28 6a 6f 75 72 6e 61 6c 46 _TO_PTR(journalF
1f150 69 6c 65 53 69 7a 65 29 29 20 29 3b 0a 20 20 69 ileSize)) );. i
1f160 66 28 20 21 70 50 74 72 20 29 7b 0a 20 20 20 20 f( !pPtr ){.
1f170 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61 sqlite3_free(zPa
1f180 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 thname);. ret
1f190 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
1f1a0 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 20 3d ;. }. pPager =
1f1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 50 (P
1f1c0 61 67 65 72 2a 29 28 70 50 74 72 29 3b 0a 20 20 ager*)(pPtr);.
1f1d0 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 20 pPager->pPCache
1f1e0 3d 20 20 20 20 28 50 43 61 63 68 65 2a 29 28 70 = (PCache*)(p
1f1f0 50 74 72 20 2b 3d 20 52 4f 55 4e 44 38 28 73 69 Ptr += ROUND8(si
1f200 7a 65 6f 66 28 2a 70 50 61 67 65 72 29 29 29 3b zeof(*pPager)));
1f210 0a 20 20 70 50 61 67 65 72 2d 3e 66 64 20 3d 20 . pPager->fd =
1f220 20 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a (sqlite3_file*
1f230 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 4e 44 38 )(pPtr += ROUND8
1f240 28 70 63 61 63 68 65 53 69 7a 65 29 29 3b 0a 20 (pcacheSize));.
1f250 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 20 3d 20 pPager->sjfd =
1f260 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 28 (sqlite3_file*)(
1f270 70 50 74 72 20 2b 3d 20 52 4f 55 4e 44 38 28 70 pPtr += ROUND8(p
1f280 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 29 3b Vfs->szOsFile));
1f290 0a 20 20 70 50 61 67 65 72 2d 3e 6a 66 64 20 3d . pPager->jfd =
1f2a0 20 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a (sqlite3_file*
1f2b0 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75 72 6e 61 )(pPtr += journa
1f2c0 6c 46 69 6c 65 53 69 7a 65 29 3b 0a 20 20 70 50 lFileSize);. pP
1f2d0 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 20 ager->zFilename
1f2e0 3d 20 20 20 20 28 63 68 61 72 2a 29 28 70 50 74 = (char*)(pPt
1f2f0 72 20 2b 3d 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 r += journalFile
1f300 53 69 7a 65 29 3b 0a 20 20 61 73 73 65 72 74 28 Size);. assert(
1f310 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 EIGHT_BYTE_ALIG
1f320 4e 4d 45 4e 54 28 70 50 61 67 65 72 2d 3e 6a 66 NMENT(pPager->jf
1f330 64 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c d) );.. /* Fill
1f340 20 69 6e 20 74 68 65 20 50 61 67 65 72 2e 7a 46 in the Pager.zF
1f350 69 6c 65 6e 61 6d 65 20 61 6e 64 20 50 61 67 65 ilename and Page
1f360 72 2e 7a 4a 6f 75 72 6e 61 6c 20 62 75 66 66 65 r.zJournal buffe
1f370 72 73 2c 20 69 66 20 72 65 71 75 69 72 65 64 2e rs, if required.
1f380 20 2a 2f 0a 20 20 69 66 28 20 7a 50 61 74 68 6e */. if( zPathn
1f390 61 6d 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65 ame ){. pPage
1f3a0 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d 20 20 20 r->zJournal =
1f3b0 28 63 68 61 72 2a 29 28 70 50 74 72 20 2b 3d 20 (char*)(pPtr +=
1f3c0 6e 50 61 74 68 6e 61 6d 65 20 2b 20 31 29 3b 0a nPathname + 1);.
1f3d0 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 memcpy(pPage
1f3e0 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a 50 r->zFilename, zP
1f3f0 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 athname, nPathna
1f400 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 me);. memcpy(
1f410 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
1f420 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6e 50 61 , zPathname, nPa
1f430 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 6d thname);. mem
1f440 63 70 79 28 26 70 50 61 67 65 72 2d 3e 7a 4a 6f cpy(&pPager->zJo
1f450 75 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 6d 65 5d urnal[nPathname]
1f460 2c 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c 20 38 29 , "-journal", 8)
1f470 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 ;. if( pPager
1f480 2d 3e 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d ->zFilename[0]==
1f490 30 20 29 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 0 ) pPager->zJou
1f4a0 72 6e 61 6c 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 rnal[0] = 0;.
1f4b0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 sqlite3_free(zP
1f4c0 61 74 68 6e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 athname);. }.
1f4d0 70 50 61 67 65 72 2d 3e 70 56 66 73 20 3d 20 70 pPager->pVfs = p
1f4e0 56 66 73 3b 0a 20 20 70 50 61 67 65 72 2d 3e 76 Vfs;. pPager->v
1f4f0 66 73 46 6c 61 67 73 20 3d 20 76 66 73 46 6c 61 fsFlags = vfsFla
1f500 67 73 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 gs;.. /* Open t
1f510 68 65 20 70 61 67 65 72 20 66 69 6c 65 2e 0a 20 he pager file..
1f520 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e */. if( zFilen
1f530 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 ame && zFilename
1f540 5b 30 5d 20 26 26 20 21 6d 65 6d 44 62 20 29 7b [0] && !memDb ){
1f550 0a 20 20 20 20 69 6e 74 20 66 6f 75 74 20 3d 20 . int fout =
1f560 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1f570 20 20 20 20 20 20 2f 2a 20 56 46 53 20 66 6c 61 /* VFS fla
1f580 67 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 78 gs returned by x
1f590 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 20 20 72 63 Open() */. rc
1f5a0 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e = sqlite3OsOpen
1f5b0 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a (pVfs, pPager->z
1f5c0 46 69 6c 65 6e 61 6d 65 2c 20 70 50 61 67 65 72 Filename, pPager
1f5d0 2d 3e 66 64 2c 20 76 66 73 46 6c 61 67 73 2c 20 ->fd, vfsFlags,
1f5e0 26 66 6f 75 74 29 3b 0a 20 20 20 20 72 65 61 64 &fout);. read
1f5f0 4f 6e 6c 79 20 3d 20 28 66 6f 75 74 26 53 51 4c Only = (fout&SQL
1f600 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
1f610 59 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 Y);.. /* If t
1f620 68 65 20 66 69 6c 65 20 77 61 73 20 73 75 63 63 he file was succ
1f630 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 20 essfully opened
1f640 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20 61 for read/write a
1f650 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20 63 68 ccess,. ** ch
1f660 6f 6f 73 65 20 61 20 64 65 66 61 75 6c 74 20 70 oose a default p
1f670 61 67 65 20 73 69 7a 65 20 69 6e 20 63 61 73 65 age size in case
1f680 20 77 65 20 68 61 76 65 20 74 6f 20 63 72 65 61 we have to crea
1f690 74 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 te the. ** da
1f6a0 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 tabase file. The
1f6b0 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 default page si
1f6c0 7a 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 ze is the maximu
1f6d0 6d 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 m of:. **.
1f6e0 20 2a 2a 20 20 20 20 2b 20 53 51 4c 49 54 45 5f ** + SQLITE_
1f6f0 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
1f700 45 2c 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 E,. ** + T
1f710 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
1f720 64 20 62 79 20 73 71 6c 69 74 65 33 4f 73 53 65 d by sqlite3OsSe
1f730 63 74 6f 72 53 69 7a 65 28 29 0a 20 20 20 20 2a ctorSize(). *
1f740 2a 20 20 20 20 2b 20 54 68 65 20 6c 61 72 67 65 * + The large
1f750 73 74 20 70 61 67 65 20 73 69 7a 65 20 74 68 61 st page size tha
1f760 74 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e t can be written
1f770 20 61 74 6f 6d 69 63 61 6c 6c 79 2e 0a 20 20 20 atomically..
1f780 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d */. if( rc==
1f790 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 72 65 SQLITE_OK && !re
1f7a0 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 adOnly ){.
1f7b0 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 setSectorSize(pP
1f7c0 61 67 65 72 29 3b 0a 20 20 20 20 20 20 61 73 73 ager);. ass
1f7d0 65 72 74 28 53 51 4c 49 54 45 5f 44 45 46 41 55 ert(SQLITE_DEFAU
1f7e0 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c 3d 53 51 LT_PAGE_SIZE<=SQ
1f7f0 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 LITE_MAX_DEFAULT
1f800 5f 50 41 47 45 5f 53 49 5a 45 29 3b 0a 20 20 20 _PAGE_SIZE);.
1f810 20 20 20 69 66 28 20 73 7a 50 61 67 65 44 66 6c if( szPageDfl
1f820 74 3c 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 t<pPager->sector
1f830 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Size ){.
1f840 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63 74 if( pPager->sect
1f850 6f 72 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 orSize>SQLITE_MA
1f860 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 X_DEFAULT_PAGE_S
1f870 49 5a 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 IZE ){.
1f880 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 53 51 szPageDflt = SQ
1f890 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 LITE_MAX_DEFAULT
1f8a0 5f 50 41 47 45 5f 53 49 5a 45 3b 0a 20 20 20 20 _PAGE_SIZE;.
1f8b0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1f8c0 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20 szPageDflt
1f8d0 3d 20 28 75 31 36 29 70 50 61 67 65 72 2d 3e 73 = (u16)pPager->s
1f8e0 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 20 20 20 ectorSize;.
1f8f0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 69 66 }. }.#if
1f900 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
1f910 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 E_ATOMIC_WRITE.
1f920 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69 {. i
1f930 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33 nt iDc = sqlite3
1f940 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 OsDeviceCharacte
1f950 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e ristics(pPager->
1f960 66 64 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 fd);. int
1f970 20 69 69 3b 0a 20 20 20 20 20 20 20 20 61 73 73 ii;. ass
1f980 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 ert(SQLITE_IOCAP
1f990 5f 41 54 4f 4d 49 43 35 31 32 3d 3d 28 35 31 32 _ATOMIC512==(512
1f9a0 3e 3e 38 29 29 3b 0a 20 20 20 20 20 20 20 20 61 >>8));. a
1f9b0 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 ssert(SQLITE_IOC
1f9c0 41 50 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 AP_ATOMIC64K==(6
1f9d0 35 35 33 36 3e 3e 38 29 29 3b 0a 20 20 20 20 20 5536>>8));.
1f9e0 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 assert(SQLITE
1f9f0 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 _MAX_DEFAULT_PAG
1fa00 45 5f 53 49 5a 45 3c 3d 36 35 35 33 36 29 3b 0a E_SIZE<=65536);.
1fa10 20 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d 73 for(ii=s
1fa20 7a 50 61 67 65 44 66 6c 74 3b 20 69 69 3c 3d 53 zPageDflt; ii<=S
1fa30 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c QLITE_MAX_DEFAUL
1fa40 54 5f 50 41 47 45 5f 53 49 5a 45 3b 20 69 69 3d T_PAGE_SIZE; ii=
1fa50 69 69 2a 32 29 7b 0a 20 20 20 20 20 20 20 20 20 ii*2){.
1fa60 20 69 66 28 20 69 44 63 26 28 53 51 4c 49 54 45 if( iDc&(SQLITE
1fa70 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 69 _IOCAP_ATOMIC|(i
1fa80 69 3e 3e 38 29 29 20 29 7b 0a 20 20 20 20 20 20 i>>8)) ){.
1fa90 20 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 szPageDflt
1faa0 20 3d 20 69 69 3b 0a 20 20 20 20 20 20 20 20 20 = ii;.
1fab0 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1fac0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
1fad0 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f }. }else{. /
1fae0 2a 20 49 66 20 61 20 74 65 6d 70 6f 72 61 72 79 * If a temporary
1faf0 20 66 69 6c 65 20 69 73 20 72 65 71 75 65 73 74 file is request
1fb00 65 64 2c 20 69 74 20 69 73 20 6e 6f 74 20 6f 70 ed, it is not op
1fb10 65 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 ened immediately
1fb20 2e 0a 20 20 20 20 2a 2a 20 49 6e 20 74 68 69 73 .. ** In this
1fb30 20 63 61 73 65 20 77 65 20 61 63 63 65 70 74 20 case we accept
1fb40 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 the default page
1fb50 20 73 69 7a 65 20 61 6e 64 20 64 65 6c 61 79 20 size and delay
1fb60 61 63 74 75 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 actually. **
1fb70 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 opening the file
1fb80 20 75 6e 74 69 6c 20 74 68 65 20 66 69 72 73 74 until the first
1fb90 20 63 61 6c 6c 20 74 6f 20 4f 73 57 72 69 74 65 call to OsWrite
1fba0 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a ().. **. *
1fbb0 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 69 73 * This branch is
1fbc0 20 61 6c 73 6f 20 72 75 6e 20 66 6f 72 20 61 6e also run for an
1fbd0 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 in-memory datab
1fbe0 61 73 65 2e 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 ase. An in-memor
1fbf0 79 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 y. ** databas
1fc00 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 e is the same as
1fc10 20 61 20 74 65 6d 70 2d 66 69 6c 65 20 74 68 61 a temp-file tha
1fc20 74 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 74 t is never writt
1fc30 65 6e 20 6f 75 74 20 74 6f 0a 20 20 20 20 2a 2a en out to. **
1fc40 20 64 69 73 6b 20 61 6e 64 20 75 73 65 73 20 61 disk and uses a
1fc50 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c n in-memory roll
1fc60 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 back journal..
1fc70 20 20 2a 2f 20 0a 20 20 20 20 74 65 6d 70 46 69 */ . tempFi
1fc80 6c 65 20 3d 20 31 3b 0a 20 20 20 20 70 50 61 67 le = 1;. pPag
1fc90 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 er->state = PAGE
1fca0 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 20 R_EXCLUSIVE;.
1fcb0 20 72 65 61 64 4f 6e 6c 79 20 3d 20 28 76 66 73 readOnly = (vfs
1fcc0 46 6c 61 67 73 26 53 51 4c 49 54 45 5f 4f 50 45 Flags&SQLITE_OPE
1fcd0 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 7d N_READONLY);. }
1fce0 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f .. /* The follo
1fcf0 77 69 6e 67 20 63 61 6c 6c 20 74 6f 20 50 61 67 wing call to Pag
1fd00 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 29 20 erSetPagesize()
1fd10 73 65 72 76 65 73 20 74 6f 20 73 65 74 20 74 68 serves to set th
1fd20 65 20 76 61 6c 75 65 20 6f 66 20 0a 20 20 2a 2a e value of . **
1fd30 20 50 61 67 65 72 2e 70 61 67 65 53 69 7a 65 20 Pager.pageSize
1fd40 61 6e 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 and to allocate
1fd50 74 68 65 20 50 61 67 65 72 2e 70 54 6d 70 53 70 the Pager.pTmpSp
1fd60 61 63 65 20 62 75 66 66 65 72 2e 0a 20 20 2a 2f ace buffer.. */
1fd70 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1fd80 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 E_OK ){. asse
1fd90 72 74 28 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 rt( pPager->memD
1fda0 62 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d b==0 );. rc =
1fdb0 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
1fdc0 50 61 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c Pagesize(pPager,
1fdd0 20 26 73 7a 50 61 67 65 44 66 6c 74 2c 20 2d 31 &szPageDflt, -1
1fde0 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
1fdf0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1fe00 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 ;. }.. /* If a
1fe10 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 n error occurred
1fe20 20 69 6e 20 65 69 74 68 65 72 20 6f 66 20 74 68 in either of th
1fe30 65 20 62 6c 6f 63 6b 73 20 61 62 6f 76 65 2c 20 e blocks above,
1fe40 66 72 65 65 20 74 68 65 20 0a 20 20 2a 2a 20 50 free the . ** P
1fe50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 61 ager structure a
1fe60 6e 64 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c nd close the fil
1fe70 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 e.. */. if( rc
1fe80 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1fe90 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67 assert( !pPag
1fea0 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 3b er->pTmpSpace );
1feb0 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c . sqlite3OsCl
1fec0 6f 73 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b ose(pPager->fd);
1fed0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
1fee0 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 72 e(pPager);. r
1fef0 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 eturn rc;. }..
1ff00 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 /* Initialize t
1ff10 68 65 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 he PCache object
1ff20 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6e . */. assert( n
1ff30 45 78 74 72 61 3c 31 30 30 30 20 29 3b 0a 20 20 Extra<1000 );.
1ff40 6e 45 78 74 72 61 20 3d 20 52 4f 55 4e 44 38 28 nExtra = ROUND8(
1ff50 6e 45 78 74 72 61 29 3b 0a 20 20 73 71 6c 69 74 nExtra);. sqlit
1ff60 65 33 50 63 61 63 68 65 4f 70 65 6e 28 73 7a 50 e3PcacheOpen(szP
1ff70 61 67 65 44 66 6c 74 2c 20 6e 45 78 74 72 61 2c ageDflt, nExtra,
1ff80 20 21 6d 65 6d 44 62 2c 0a 20 20 20 20 20 20 20 !memDb,.
1ff90 20 20 20 20 20 20 20 20 20 20 20 20 20 21 6d 65 !me
1ffa0 6d 44 62 3f 70 61 67 65 72 53 74 72 65 73 73 3a mDb?pagerStress:
1ffb0 30 2c 20 28 76 6f 69 64 20 2a 29 70 50 61 67 65 0, (void *)pPage
1ffc0 72 2c 20 70 50 61 67 65 72 2d 3e 70 50 43 61 63 r, pPager->pPCac
1ffd0 68 65 29 3b 0a 0a 20 20 50 41 47 45 52 54 52 41 he);.. PAGERTRA
1ffe0 43 45 28 28 22 4f 50 45 4e 20 25 64 20 25 73 5c CE(("OPEN %d %s\
1fff0 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 n", FILEHANDLEID
20000 28 70 50 61 67 65 72 2d 3e 66 64 29 2c 20 70 50 (pPager->fd), pP
20010 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 ager->zFilename)
20020 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 4f );. IOTRACE(("O
20030 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c 20 70 50 PEN %p %s\n", pP
20040 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 7a 46 ager, pPager->zF
20050 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20 70 50 61 ilename)).. pPa
20060 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 ger->useJournal
20070 3d 20 28 75 38 29 75 73 65 4a 6f 75 72 6e 61 6c = (u8)useJournal
20080 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 52 65 ;. pPager->noRe
20090 61 64 6c 6f 63 6b 20 3d 20 28 6e 6f 52 65 61 64 adlock = (noRead
200a0 6c 6f 63 6b 20 26 26 20 72 65 61 64 4f 6e 6c 79 lock && readOnly
200b0 29 20 3f 31 3a 30 3b 0a 20 20 2f 2a 20 70 50 61 ) ?1:0;. /* pPa
200c0 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d 20 ger->stmtOpen =
200d0 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 0; */. /* pPage
200e0 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 3d 20 30 r->stmtInUse = 0
200f0 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 ; */. /* pPager
20100 2d 3e 6e 52 65 66 20 3d 20 30 3b 20 2a 2f 0a 20 ->nRef = 0; */.
20110 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 pPager->dbSizeV
20120 61 6c 69 64 20 3d 20 28 75 38 29 6d 65 6d 44 62 alid = (u8)memDb
20130 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 ;. /* pPager->s
20140 74 6d 74 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a tmtSize = 0; */.
20150 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d /* pPager->stm
20160 74 4a 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a 20 tJSize = 0; */.
20170 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 50 61 67 /* pPager->nPag
20180 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 e = 0; */. pPag
20190 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 53 51 4c er->mxPgno = SQL
201a0 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43 4f 55 ITE_MAX_PAGE_COU
201b0 4e 54 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d NT;. /* pPager-
201c0 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 55 >state = PAGER_U
201d0 4e 4c 4f 43 4b 3b 20 2a 2f 0a 20 20 61 73 73 65 NLOCK; */. asse
201e0 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
201f0 65 20 3d 3d 20 28 74 65 6d 70 46 69 6c 65 20 3f e == (tempFile ?
20200 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 PAGER_EXCLUSIVE
20210 20 3a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 29 : PAGER_UNLOCK)
20220 20 29 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d );. /* pPager-
20230 3e 65 72 72 4d 61 73 6b 20 3d 20 30 3b 20 2a 2f >errMask = 0; */
20240 0a 20 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 . pPager->tempF
20250 69 6c 65 20 3d 20 28 75 38 29 74 65 6d 70 46 69 ile = (u8)tempFi
20260 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 74 65 le;. assert( te
20270 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f 4c 4f mpFile==PAGER_LO
20280 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c CKINGMODE_NORMAL
20290 20 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 74 . || t
202a0 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f 4c empFile==PAGER_L
202b0 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 OCKINGMODE_EXCLU
202c0 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72 74 SIVE );. assert
202d0 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d ( PAGER_LOCKINGM
202e0 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3d 3d 31 ODE_EXCLUSIVE==1
202f0 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 78 );. pPager->ex
20300 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28 75 clusiveMode = (u
20310 38 29 74 65 6d 70 46 69 6c 65 3b 20 0a 20 20 70 8)tempFile; . p
20320 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 Pager->changeCou
20330 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 65 72 2d ntDone = pPager-
20340 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70 50 61 >tempFile;. pPa
20350 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20 28 75 38 ger->memDb = (u8
20360 29 6d 65 6d 44 62 3b 0a 20 20 70 50 61 67 65 72 )memDb;. pPager
20370 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 28 75 38 ->readOnly = (u8
20380 29 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 2f 2a 20 )readOnly;. /*
20390 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 pPager->needSync
203a0 20 3d 20 30 3b 20 2a 2f 0a 20 20 61 73 73 65 72 = 0; */. asser
203b0 74 28 20 75 73 65 4a 6f 75 72 6e 61 6c 20 7c 7c t( useJournal ||
203c0 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c pPager->tempFil
203d0 65 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e e );. pPager->n
203e0 6f 53 79 6e 63 20 3d 20 70 50 61 67 65 72 2d 3e oSync = pPager->
203f0 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70 50 61 67 tempFile;. pPag
20400 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 70 er->fullSync = p
20410 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3f 30 Pager->noSync ?0
20420 3a 31 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 :1;. pPager->sy
20430 6e 63 5f 66 6c 61 67 73 20 3d 20 53 51 4c 49 54 nc_flags = SQLIT
20440 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 3b 0a 20 E_SYNC_NORMAL;.
20450 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 69 72 /* pPager->pFir
20460 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 st = 0; */. /*
20470 70 50 61 67 65 72 2d 3e 70 46 69 72 73 74 53 79 pPager->pFirstSy
20480 6e 63 65 64 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f nced = 0; */. /
20490 2a 20 70 50 61 67 65 72 2d 3e 70 4c 61 73 74 20 * pPager->pLast
204a0 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 = 0; */. pPager
204b0 2d 3e 6e 45 78 74 72 61 20 3d 20 28 75 31 36 29 ->nExtra = (u16)
204c0 6e 45 78 74 72 61 3b 0a 20 20 70 50 61 67 65 72 nExtra;. pPager
204d0 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d ->journalSizeLim
204e0 69 74 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 it = SQLITE_DEFA
204f0 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 ULT_JOURNAL_SIZE
20500 5f 4c 49 4d 49 54 3b 0a 20 20 61 73 73 65 72 74 _LIMIT;. assert
20510 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
20520 3e 66 64 29 20 7c 7c 20 74 65 6d 70 46 69 6c 65 >fd) || tempFile
20530 20 29 3b 0a 20 20 73 65 74 53 65 63 74 6f 72 53 );. setSectorS
20540 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 69 ize(pPager);. i
20550 66 28 20 21 75 73 65 4a 6f 75 72 6e 61 6c 20 29 f( !useJournal )
20560 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f {. pPager->jo
20570 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45 urnalMode = PAGE
20580 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 R_JOURNALMODE_OF
20590 46 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6d F;. }else if( m
205a0 65 6d 44 62 20 29 7b 0a 20 20 20 20 70 50 61 67 emDb ){. pPag
205b0 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 er->journalMode
205c0 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d = PAGER_JOURNALM
205d0 4f 44 45 5f 4d 45 4d 4f 52 59 3b 0a 20 20 7d 0a ODE_MEMORY;. }.
205e0 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 78 42 75 /* pPager->xBu
205f0 73 79 48 61 6e 64 6c 65 72 20 3d 20 30 3b 20 2a syHandler = 0; *
20600 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 /. /* pPager->p
20610 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 20 3d BusyHandlerArg =
20620 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 0; */. pPager-
20630 3e 78 52 65 69 6e 69 74 65 72 20 3d 20 78 52 65 >xReiniter = xRe
20640 69 6e 69 74 3b 0a 20 20 2f 2a 20 6d 65 6d 73 65 init;. /* memse
20650 74 28 70 50 61 67 65 72 2d 3e 61 48 61 73 68 2c t(pPager->aHash,
20660 20 30 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65 0, sizeof(pPage
20670 72 2d 3e 61 48 61 73 68 29 29 3b 20 2a 2f 0a 20 r->aHash)); */.
20680 20 2a 70 70 50 61 67 65 72 20 3d 20 70 50 61 67 *ppPager = pPag
20690 65 72 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c er;. return SQL
206a0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a ITE_OK;.}..../*.
206b0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
206c0 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 is called after
206d0 20 74 72 61 6e 73 69 74 69 6f 6e 69 6e 67 20 66 transitioning f
206e0 72 6f 6d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b rom PAGER_UNLOCK
206f0 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f 53 48 41 to.** PAGER_SHA
20700 52 45 44 20 73 74 61 74 65 2e 20 49 74 20 74 65 RED state. It te
20710 73 74 73 20 69 66 20 74 68 65 72 65 20 69 73 20 sts if there is
20720 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 70 72 a hot journal pr
20730 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 74 68 65 20 esent in.** the
20740 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 6f 72 20 file-system for
20750 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 72 2e the given pager.
20760 20 41 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 69 A hot journal i
20770 73 20 6f 6e 65 20 74 68 61 74 20 0a 2a 2a 20 6e s one that .** n
20780 65 65 64 73 20 74 6f 20 62 65 20 70 6c 61 79 65 eeds to be playe
20790 64 20 62 61 63 6b 2e 20 41 63 63 6f 72 64 69 6e d back. Accordin
207a0 67 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 g to this functi
207b0 6f 6e 2c 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 on, a hot-journa
207c0 6c 0a 2a 2a 20 66 69 6c 65 20 65 78 69 73 74 73 l.** file exists
207d0 20 69 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e if the followin
207e0 67 20 63 72 69 74 65 72 69 61 20 61 72 65 20 6d g criteria are m
207f0 65 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 et:.**.** * Th
20800 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 e journal file e
20810 78 69 73 74 73 20 69 6e 20 74 68 65 20 66 69 6c xists in the fil
20820 65 20 73 79 73 74 65 6d 2c 20 61 6e 64 0a 2a 2a e system, and.**
20830 20 20 20 2a 20 4e 6f 20 70 72 6f 63 65 73 73 20 * No process
20840 68 6f 6c 64 73 20 61 20 52 45 53 45 52 56 45 44 holds a RESERVED
20850 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b or greater lock
20860 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
20870 20 66 69 6c 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 file, and.**
20880 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 * The database f
20890 69 6c 65 20 69 74 73 65 6c 66 20 69 73 20 67 72 ile itself is gr
208a0 65 61 74 65 72 20 74 68 61 6e 20 30 20 62 79 74 eater than 0 byt
208b0 65 73 20 69 6e 20 73 69 7a 65 2c 20 61 6e 64 0a es in size, and.
208c0 2a 2a 20 20 20 2a 20 54 68 65 20 66 69 72 73 74 ** * The first
208d0 20 62 79 74 65 20 6f 66 20 74 68 65 20 6a 6f 75 byte of the jou
208e0 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 rnal file exists
208f0 20 61 6e 64 20 69 73 20 6e 6f 74 20 30 78 30 30 and is not 0x00
20900 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 ..**.** If the c
20910 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 urrent size of t
20920 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
20930 20 69 73 20 30 20 62 75 74 20 61 20 6a 6f 75 72 is 0 but a jour
20940 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 65 78 69 73 nal file.** exis
20950 74 73 2c 20 74 68 61 74 20 69 73 20 70 72 6f 62 ts, that is prob
20960 61 62 6c 79 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 ably an old jour
20970 6e 61 6c 20 6c 65 66 74 20 6f 76 65 72 20 66 72 nal left over fr
20980 6f 6d 20 61 20 70 72 69 6f 72 0a 2a 2a 20 64 61 om a prior.** da
20990 74 61 62 61 73 65 20 77 69 74 68 20 74 68 65 20 tabase with the
209a0 73 61 6d 65 20 6e 61 6d 65 2e 20 49 6e 20 74 68 same name. In th
209b0 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f 75 72 is case the jour
209c0 6e 61 6c 20 66 69 6c 65 20 69 73 0a 2a 2a 20 6a nal file is.** j
209d0 75 73 74 20 64 65 6c 65 74 65 64 20 75 73 69 6e ust deleted usin
209e0 67 20 4f 73 44 65 6c 65 74 65 2c 20 2a 70 45 78 g OsDelete, *pEx
209f0 69 73 74 73 20 69 73 20 73 65 74 20 74 6f 20 30 ists is set to 0
20a00 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a and SQLITE_OK.*
20a10 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a * is returned..*
20a20 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
20a30 65 20 64 6f 65 73 20 6e 6f 74 20 63 68 65 63 6b e does not check
20a40 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 6d if there is a m
20a50 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
20a60 6c 65 6e 61 6d 65 0a 2a 2a 20 61 74 20 74 68 65 lename.** at the
20a70 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 end of the file
20a80 2e 20 49 66 20 74 68 65 72 65 20 69 73 2c 20 61 . If there is, a
20a90 6e 64 20 74 68 61 74 20 6d 61 73 74 65 72 20 6a nd that master j
20aa0 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 64 ournal file.** d
20ab0 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 74 oes not exist, t
20ac0 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hen the journal
20ad0 66 69 6c 65 20 69 73 20 6e 6f 74 20 72 65 61 6c file is not real
20ae0 6c 79 20 68 6f 74 2e 20 49 6e 20 74 68 69 73 0a ly hot. In this.
20af0 2a 2a 20 63 61 73 65 20 74 68 69 73 20 72 6f 75 ** case this rou
20b00 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e tine will return
20b10 20 61 20 66 61 6c 73 65 2d 70 6f 73 69 74 69 76 a false-positiv
20b20 65 2e 20 54 68 65 20 70 61 67 65 72 5f 70 6c 61 e. The pager_pla
20b30 79 62 61 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 yback().** routi
20b40 6e 65 20 77 69 6c 6c 20 64 69 73 63 6f 76 65 72 ne will discover
20b50 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 that the journa
20b60 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 72 65 l file is not re
20b70 61 6c 6c 79 20 68 6f 74 20 61 6e 64 20 0a 2a 2a ally hot and .**
20b80 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c 6c 20 69 will not roll i
20b90 74 20 62 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 49 t back. .**.** I
20ba0 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 f a hot-journal
20bb0 66 69 6c 65 20 69 73 20 66 6f 75 6e 64 20 74 6f file is found to
20bc0 20 65 78 69 73 74 2c 20 2a 70 45 78 69 73 74 73 exist, *pExists
20bd0 20 69 73 20 73 65 74 20 74 6f 20 31 20 61 6e 64 is set to 1 and
20be0 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 72 .** SQLITE_OK r
20bf0 65 74 75 72 6e 65 64 2e 20 49 66 20 6e 6f 20 68 eturned. If no h
20c00 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ot-journal file
20c10 69 73 20 70 72 65 73 65 6e 74 2c 20 2a 70 45 78 is present, *pEx
20c20 69 73 74 73 20 69 73 0a 2a 2a 20 73 65 74 20 74 ists is.** set t
20c30 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f o 0 and SQLITE_O
20c40 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 K returned. If a
20c50 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 n IO error occur
20c60 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67 0a 2a s while trying.*
20c70 2a 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 * to determine w
20c80 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 hether or not a
20c90 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 hot-journal file
20ca0 20 65 78 69 73 74 73 2c 20 74 68 65 20 49 4f 20 exists, the IO
20cb0 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73 error.** code is
20cc0 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 returned and th
20cd0 65 20 76 61 6c 75 65 20 6f 66 20 2a 70 45 78 69 e value of *pExi
20ce0 73 74 73 20 69 73 20 75 6e 64 65 66 69 6e 65 64 sts is undefined
20cf0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
20d00 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 50 61 hasHotJournal(Pa
20d10 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
20d20 20 2a 70 45 78 69 73 74 73 29 7b 0a 20 20 73 71 *pExists){. sq
20d30 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 6f 6e 73 lite3_vfs * cons
20d40 74 20 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d t pVfs = pPager-
20d50 3e 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b >pVfs;. int rc;
20d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d70 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
20d80 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 65 code */. int e
20d90 78 69 73 74 73 3b 20 20 20 20 20 20 20 20 20 20 xists;
20da0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
20db0 20 69 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 if a journal fi
20dc0 6c 65 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f le is present */
20dd0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
20de0 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 er!=0 );. asser
20df0 74 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f t( pPager->useJo
20e00 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 urnal );. asser
20e10 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
20e20 2d 3e 66 64 29 20 29 3b 0a 20 20 61 73 73 65 72 ->fd) );. asser
20e30 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 t( !isOpen(pPage
20e40 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 61 73 73 r->jfd) );. ass
20e50 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
20e60 74 65 20 3c 3d 20 50 41 47 45 52 5f 53 48 41 52 te <= PAGER_SHAR
20e70 45 44 20 29 3b 0a 0a 20 20 2a 70 45 78 69 73 74 ED );.. *pExist
20e80 73 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73 71 s = 0;. rc = sq
20e90 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 lite3OsAccess(pV
20ea0 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 fs, pPager->zJou
20eb0 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 rnal, SQLITE_ACC
20ec0 45 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78 69 ESS_EXISTS, &exi
20ed0 73 74 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d sts);. if( rc==
20ee0 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 78 69 SQLITE_OK && exi
20ef0 73 74 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 6c sts ){. int l
20f00 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 20 20 ocked;
20f10 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
20f20 66 20 73 6f 6d 65 20 70 72 6f 63 65 73 73 20 68 f some process h
20f30 6f 6c 64 73 20 61 20 52 45 53 45 52 56 45 44 20 olds a RESERVED
20f40 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 lock */.. /*
20f50 52 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 68 Race condition h
20f60 65 72 65 3a 20 20 41 6e 6f 74 68 65 72 20 70 72 ere: Another pr
20f70 6f 63 65 73 73 20 6d 69 67 68 74 20 68 61 76 65 ocess might have
20f80 20 62 65 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 been holding th
20f90 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 52 45 53 e. ** the RES
20fa0 45 52 56 45 44 20 6c 6f 63 6b 20 61 6e 64 20 68 ERVED lock and h
20fb0 61 76 65 20 61 20 6a 6f 75 72 6e 61 6c 20 6f 70 ave a journal op
20fc0 65 6e 20 61 74 20 74 68 65 20 73 71 6c 69 74 65 en at the sqlite
20fd0 33 4f 73 41 63 63 65 73 73 28 29 20 0a 20 20 20 3OsAccess() .
20fe0 20 2a 2a 20 63 61 6c 6c 20 61 62 6f 76 65 2c 20 ** call above,
20ff0 62 75 74 20 74 68 65 6e 20 64 65 6c 65 74 65 20 but then delete
21000 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 the journal and
21010 64 72 6f 70 20 74 68 65 20 6c 6f 63 6b 20 62 65 drop the lock be
21020 66 6f 72 65 0a 20 20 20 20 2a 2a 20 77 65 20 67 fore. ** we g
21030 65 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 et to the follow
21040 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 43 68 65 ing sqlite3OsChe
21050 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 29 ckReservedLock()
21060 20 63 61 6c 6c 2e 20 20 49 66 20 74 68 61 74 0a call. If that.
21070 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20 63 61 ** is the ca
21080 73 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 se, this routine
21090 20 6d 69 67 68 74 20 74 68 69 6e 6b 20 74 68 65 might think the
210a0 72 65 20 69 73 20 61 20 68 6f 74 20 6a 6f 75 72 re is a hot jour
210b0 6e 61 6c 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 nal when. **
210c0 69 6e 20 66 61 63 74 20 74 68 65 72 65 20 69 73 in fact there is
210d0 20 6e 6f 6e 65 2e 20 20 54 68 69 73 20 72 65 73 none. This res
210e0 75 6c 74 73 20 69 6e 20 61 20 66 61 6c 73 65 2d ults in a false-
210f0 70 6f 73 69 74 69 76 65 20 77 68 69 63 68 20 77 positive which w
21100 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65 20 64 65 ill. ** be de
21110 61 6c 74 20 77 69 74 68 20 62 79 20 74 68 65 20 alt with by the
21120 70 6c 61 79 62 61 63 6b 20 72 6f 75 74 69 6e 65 playback routine
21130 2e 20 20 54 69 63 6b 65 74 20 23 33 38 38 33 2e . Ticket #3883.
21140 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
21150 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 sqlite3OsCheckR
21160 65 73 65 72 76 65 64 4c 6f 63 6b 28 70 50 61 67 eservedLock(pPag
21170 65 72 2d 3e 66 64 2c 20 26 6c 6f 63 6b 65 64 29 er->fd, &locked)
21180 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
21190 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c 6f 63 6b LITE_OK && !lock
211a0 65 64 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 ed ){. int
211b0 6e 50 61 67 65 3b 0a 0a 20 20 20 20 20 20 2f 2a nPage;.. /*
211c0 20 43 68 65 63 6b 20 74 68 65 20 73 69 7a 65 20 Check the size
211d0 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
211e0 66 69 6c 65 2e 20 49 66 20 69 74 20 63 6f 6e 73 file. If it cons
211f0 69 73 74 73 20 6f 66 20 30 20 70 61 67 65 73 2c ists of 0 pages,
21200 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 64 . ** then d
21210 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 elete the journa
21220 6c 20 66 69 6c 65 2e 20 53 65 65 20 74 68 65 20 l file. See the
21230 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 61 header comment a
21240 62 6f 76 65 20 66 6f 72 20 0a 20 20 20 20 20 20 bove for .
21250 2a 2a 20 74 68 65 20 72 65 61 73 6f 6e 69 6e 67 ** the reasoning
21260 20 68 65 72 65 2e 20 20 44 65 6c 65 74 65 20 74 here. Delete t
21270 68 65 20 6f 62 73 6f 6c 65 74 65 20 6a 6f 75 72 he obsolete jour
21280 6e 61 6c 20 66 69 6c 65 20 75 6e 64 65 72 0a 20 nal file under.
21290 20 20 20 20 20 2a 2a 20 61 20 52 45 53 45 52 56 ** a RESERV
212a0 45 44 20 6c 6f 63 6b 20 74 6f 20 61 76 6f 69 64 ED lock to avoid
212b0 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 73 race conditions
212c0 20 61 6e 64 20 74 6f 20 61 76 6f 69 64 20 76 69 and to avoid vi
212d0 6f 6c 61 74 69 6e 67 0a 20 20 20 20 20 20 2a 2a olating. **
212e0 20 5b 48 33 33 30 32 30 5d 2e 0a 20 20 20 20 20 [H33020]..
212f0 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 */. rc = s
21300 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 qlite3PagerPagec
21310 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 26 6e 50 ount(pPager, &nP
21320 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 age);. if(
21330 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
21340 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 50 61 . if( nPa
21350 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ge==0 ){.
21360 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 sqlite3BeginB
21370 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 enignMalloc();.
21380 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c if( sql
21390 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 ite3OsLock(pPage
213a0 72 2d 3e 66 64 2c 20 52 45 53 45 52 56 45 44 5f r->fd, RESERVED_
213b0 4c 4f 43 4b 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b LOCK)==SQLITE_OK
213c0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
213d0 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 sqlite3OsDelete(
213e0 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a pVfs, pPager->zJ
213f0 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 ournal, 0);.
21400 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f sqlite3O
21410 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e sUnlock(pPager->
21420 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 fd, SHARED_LOCK)
21430 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
21440 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
21450 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 ndBenignMalloc()
21460 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
21470 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 . /* Th
21480 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 e journal file e
21490 78 69 73 74 73 20 61 6e 64 20 6e 6f 20 6f 74 68 xists and no oth
214a0 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 er connection ha
214b0 73 20 61 20 72 65 73 65 72 76 65 64 0a 20 20 20 s a reserved.
214c0 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 67 72 65 ** or gre
214d0 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 ater lock on the
214e0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
214f0 4e 6f 77 20 63 68 65 63 6b 20 74 68 61 74 20 74 Now check that t
21500 68 65 72 65 20 69 73 0a 20 20 20 20 20 20 20 20 here is.
21510 20 20 2a 2a 20 61 74 20 6c 65 61 73 74 20 6f 6e ** at least on
21520 65 20 6e 6f 6e 2d 7a 65 72 6f 20 62 79 74 65 73 e non-zero bytes
21530 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
21540 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
21550 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 e.. **
21560 49 66 20 74 68 65 72 65 20 69 73 2c 20 74 68 65 If there is, the
21570 6e 20 77 65 20 63 6f 6e 73 69 64 65 72 20 74 68 n we consider th
21580 69 73 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65 is journal to be
21590 20 68 6f 74 2e 20 49 66 20 6e 6f 74 2c 20 0a 20 hot. If not, .
215a0 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 63 ** it c
215b0 61 6e 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a 20 an be ignored..
215c0 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 */.
215d0 20 20 20 20 20 20 69 6e 74 20 66 20 3d 20 53 51 int f = SQ
215e0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e LITE_OPEN_READON
215f0 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d LY|SQLITE_OPEN_M
21600 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 AIN_JOURNAL;.
21610 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
21620 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 te3OsOpen(pVfs,
21630 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
21640 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 , pPager->jfd, f
21650 2c 20 26 66 29 3b 0a 20 20 20 20 20 20 20 20 20 , &f);.
21660 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
21670 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
21680 20 20 75 38 20 66 69 72 73 74 20 3d 20 30 3b 0a u8 first = 0;.
21690 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
216a0 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 sqlite3OsRead(p
216b0 50 61 67 65 72 2d 3e 6a 66 64 2c 20 28 76 6f 69 Pager->jfd, (voi
216c0 64 20 2a 29 26 66 69 72 73 74 2c 20 31 2c 20 30 d *)&first, 1, 0
216d0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 );. i
216e0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f f( rc==SQLITE_IO
216f0 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 ERR_SHORT_READ )
21700 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
21710 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
21720 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
21730 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
21740 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3OsClose(pPager-
21750 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 >jfd);.
21760 20 20 20 2a 70 45 78 69 73 74 73 20 3d 20 28 66 *pExists = (f
21770 69 72 73 74 21 3d 30 29 3b 0a 20 20 20 20 20 20 irst!=0);.
21780 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 }else if( rc
21790 3d 3d 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 ==SQLITE_CANTOPE
217a0 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 N ){.
217b0 20 2f 2a 20 49 66 20 77 65 20 63 61 6e 6e 6f 74 /* If we cannot
217c0 20 6f 70 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 open the rollba
217d0 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ck journal file
217e0 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 65 65 20 in order to see
217f0 69 66 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a if. *
21800 2a 20 69 74 73 20 68 61 73 20 61 20 7a 65 72 6f * its has a zero
21810 20 68 65 61 64 65 72 2c 20 74 68 61 74 20 6d 69 header, that mi
21820 67 68 74 20 62 65 20 64 75 65 20 74 6f 20 61 6e ght be due to an
21830 20 49 2f 4f 20 65 72 72 6f 72 2c 20 6f 72 0a 20 I/O error, or.
21840 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74 ** it
21850 20 6d 69 67 68 74 20 62 65 20 64 75 65 20 74 6f might be due to
21860 20 74 68 65 20 72 61 63 65 20 63 6f 6e 64 69 74 the race condit
21870 69 6f 6e 20 64 65 73 63 72 69 62 65 64 20 61 62 ion described ab
21880 6f 76 65 20 61 6e 64 20 69 6e 0a 20 20 20 20 20 ove and in.
21890 20 20 20 20 20 20 20 2a 2a 20 74 69 63 6b 65 74 ** ticket
218a0 20 23 33 38 38 33 2e 20 20 45 69 74 68 65 72 20 #3883. Either
218b0 77 61 79 2c 20 61 73 73 75 6d 65 20 74 68 61 74 way, assume that
218c0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
218d0 68 6f 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 hot..
218e0 20 2a 2a 20 54 68 69 73 20 6d 69 67 68 74 20 62 ** This might b
218f0 65 20 61 20 66 61 6c 73 65 20 70 6f 73 69 74 69 e a false positi
21900 76 65 2e 20 20 42 75 74 20 69 66 20 69 74 20 69 ve. But if it i
21910 73 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20 s, then the.
21920 20 20 20 20 20 20 20 20 2a 2a 20 61 75 74 6f 6d ** autom
21930 61 74 69 63 20 6a 6f 75 72 6e 61 6c 20 70 6c 61 atic journal pla
21940 79 62 61 63 6b 20 61 6e 64 20 72 65 63 6f 76 65 yback and recove
21950 72 79 20 6d 65 63 68 61 6e 69 73 6d 20 77 69 6c ry mechanism wil
21960 6c 20 64 65 61 6c 0a 20 20 20 20 20 20 20 20 20 l deal.
21970 20 20 20 2a 2a 20 77 69 74 68 20 69 74 20 75 6e ** with it un
21980 64 65 72 20 61 6e 20 45 58 43 4c 55 53 49 56 45 der an EXCLUSIVE
21990 20 6c 6f 63 6b 20 77 68 65 72 65 20 77 65 20 64 lock where we d
219a0 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0a 20 20 o not need to.
219b0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 77 6f 72 ** wor
219c0 72 79 20 73 6f 20 6d 75 63 68 20 77 69 74 68 20 ry so much with
219d0 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 73 2e race conditions.
219e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a . */.
219f0 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 45 78 *pEx
21a00 69 73 74 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 ists = 1;.
21a10 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
21a20 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 E_OK;.
21a30 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
21a40 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 }. }. }..
21a50 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
21a60 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 63 6f *.** Read the co
21a70 6e 74 65 6e 74 20 66 6f 72 20 70 61 67 65 20 70 ntent for page p
21a80 50 67 20 6f 75 74 20 6f 66 20 74 68 65 20 64 61 Pg out of the da
21a90 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 tabase file and
21aa0 69 6e 74 6f 20 0a 2a 2a 20 70 50 67 2d 3e 70 44 into .** pPg->pD
21ab0 61 74 61 2e 20 41 20 73 68 61 72 65 64 20 6c 6f ata. A shared lo
21ac0 63 6b 20 6f 72 20 67 72 65 61 74 65 72 20 6d 75 ck or greater mu
21ad0 73 74 20 62 65 20 68 65 6c 64 20 6f 6e 20 74 68 st be held on th
21ae0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 e database.** fi
21af0 6c 65 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 le before this f
21b00 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
21b10 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 67 65 d..**.** If page
21b20 20 31 20 69 73 20 72 65 61 64 2c 20 74 68 65 6e 1 is read, then
21b30 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61 the value of Pa
21b40 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 5b 5d ger.dbFileVers[]
21b50 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 74 68 is set to.** th
21b60 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f e value read fro
21b70 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 m the database f
21b80 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ile..**.** If an
21b90 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 IO error occurs
21ba0 2c 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 , then the IO er
21bb0 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 ror is returned
21bc0 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a to the caller..*
21bd0 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c * Otherwise, SQL
21be0 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
21bf0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
21c00 74 20 72 65 61 64 44 62 50 61 67 65 28 50 67 48 t readDbPage(PgH
21c10 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 dr *pPg){. Page
21c20 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d r *pPager = pPg-
21c30 3e 70 50 61 67 65 72 3b 20 2f 2a 20 50 61 67 65 >pPager; /* Page
21c40 72 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 r object associa
21c50 74 65 64 20 77 69 74 68 20 70 61 67 65 20 70 50 ted with page pP
21c60 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f g */. Pgno pgno
21c70 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 20 20 20 = pPg->pgno;
21c80 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 /* Page numb
21c90 65 72 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 er to read */.
21ca0 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
21cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
21cc0 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
21cd0 20 69 36 34 20 69 4f 66 66 73 65 74 3b 20 20 20 i64 iOffset;
21ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
21cf0 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 Byte offset of
21d00 66 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f file to read fro
21d10 6d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 m */.. assert(
21d20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 pPager->state>=P
21d30 41 47 45 52 5f 53 48 41 52 45 44 20 26 26 20 21 AGER_SHARED && !
21d40 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73 73 65 72 MEMDB );. asser
21d50 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
21d60 2d 3e 66 64 29 20 29 3b 0a 0a 20 20 69 66 28 20 ->fd) );.. if(
21d70 4e 45 56 45 52 28 21 69 73 4f 70 65 6e 28 70 50 NEVER(!isOpen(pP
21d80 61 67 65 72 2d 3e 66 64 29 29 20 29 7b 0a 20 20 ager->fd)) ){.
21d90 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
21da0 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 ->tempFile );.
21db0 20 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e 70 44 memset(pPg->pD
21dc0 61 74 61 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e ata, 0, pPager->
21dd0 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 72 pageSize);. r
21de0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
21df0 0a 20 20 7d 0a 20 20 69 4f 66 66 73 65 74 20 3d . }. iOffset =
21e00 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 70 (pgno-1)*(i64)p
21e10 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b Pager->pageSize;
21e20 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f . rc = sqlite3O
21e30 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 sRead(pPager->fd
21e40 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c 20 70 50 , pPg->pData, pP
21e50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 ager->pageSize,
21e60 69 4f 66 66 73 65 74 29 3b 0a 20 20 69 66 28 20 iOffset);. if(
21e70 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 rc==SQLITE_IOERR
21e80 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 _SHORT_READ ){.
21e90 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
21ea0 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 67 6e K;. }. if( pgn
21eb0 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 75 38 20 2a o==1 ){. u8 *
21ec0 64 62 46 69 6c 65 56 65 72 73 20 3d 20 26 28 28 dbFileVers = &((
21ed0 75 38 2a 29 70 50 67 2d 3e 70 44 61 74 61 29 5b u8*)pPg->pData)[
21ee0 32 34 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 24];. memcpy(
21ef0 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 &pPager->dbFileV
21f00 65 72 73 2c 20 64 62 46 69 6c 65 56 65 72 73 2c ers, dbFileVers,
21f10 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e sizeof(pPager->
21f20 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 dbFileVers));.
21f30 7d 0a 20 20 43 4f 44 45 43 31 28 70 50 61 67 65 }. CODEC1(pPage
21f40 72 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c 20 70 r, pPg->pData, p
21f50 67 6e 6f 2c 20 33 2c 20 72 63 20 3d 20 53 51 4c gno, 3, rc = SQL
21f60 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 0a 20 20 50 ITE_NOMEM);.. P
21f70 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 AGER_INCR(sqlite
21f80 33 5f 70 61 67 65 72 5f 72 65 61 64 64 62 5f 63 3_pager_readdb_c
21f90 6f 75 6e 74 29 3b 0a 20 20 50 41 47 45 52 5f 49 ount);. PAGER_I
21fa0 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 52 65 61 NCR(pPager->nRea
21fb0 64 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 d);. IOTRACE(("
21fc0 50 47 49 4e 20 25 70 20 25 64 5c 6e 22 2c 20 70 PGIN %p %d\n", p
21fd0 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 Pager, pgno));.
21fe0 20 50 41 47 45 52 54 52 41 43 45 28 28 22 46 45 PAGERTRACE(("FE
21ff0 54 43 48 20 25 64 20 70 61 67 65 20 25 64 20 68 TCH %d page %d h
22000 61 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 ash(%08x)\n",.
22010 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47 PAG
22020 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 67 ERID(pPager), pg
22030 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 no, pager_pageha
22040 73 68 28 70 50 67 29 29 29 3b 0a 0a 20 20 72 65 sh(pPg)));.. re
22050 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
22060 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
22070 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 6f 62 74 is called to obt
22080 61 69 6e 20 61 20 73 68 61 72 65 64 20 6c 6f 63 ain a shared loc
22090 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
220a0 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 74 20 69 73 e file..** It is
220b0 20 69 6c 6c 65 67 61 6c 20 74 6f 20 63 61 6c 6c illegal to call
220c0 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 sqlite3PagerAcq
220d0 75 69 72 65 28 29 20 75 6e 74 69 6c 20 61 66 74 uire() until aft
220e0 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e er this function
220f0 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 73 75 63 .** has been suc
22100 63 65 73 73 66 75 6c 6c 79 20 63 61 6c 6c 65 64 cessfully called
22110 2e 20 49 66 20 61 20 73 68 61 72 65 64 2d 6c 6f . If a shared-lo
22120 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 ck is already he
22130 6c 64 20 77 68 65 6e 0a 2a 2a 20 74 68 69 73 20 ld when.** this
22140 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
22150 65 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f ed, it is a no-o
22160 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c p..**.** The fol
22170 6c 6f 77 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e lowing operation
22180 73 20 61 72 65 20 61 6c 73 6f 20 70 65 72 66 6f s are also perfo
22190 72 6d 65 64 20 62 79 20 74 68 69 73 20 66 75 6e rmed by this fun
221a0 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 31 ction..**.** 1
221b0 29 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 ) If the pager i
221c0 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 50 s currently in P
221d0 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 AGER_UNLOCK stat
221e0 65 20 28 6e 6f 20 6c 6f 63 6b 20 68 65 6c 64 0a e (no lock held.
221f0 2a 2a 20 20 20 20 20 20 6f 6e 20 74 68 65 20 64 ** on the d
22200 61 74 61 62 61 73 65 20 66 69 6c 65 29 2c 20 74 atabase file), t
22210 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 hen an attempt i
22220 73 20 6d 61 64 65 20 74 6f 20 6f 62 74 61 69 6e s made to obtain
22230 20 61 0a 2a 2a 20 20 20 20 20 20 53 48 41 52 45 a.** SHARE
22240 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 D lock on the da
22250 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 6d 6d tabase file. Imm
22260 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 6f ediately after o
22270 62 74 61 69 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 btaining.**
22280 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b the SHARED lock
22290 2c 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 , the file-syste
222a0 6d 20 69 73 20 63 68 65 63 6b 65 64 20 66 6f 72 m is checked for
222b0 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 0a a hot-journal,.
222c0 2a 2a 20 20 20 20 20 20 77 68 69 63 68 20 69 73 ** which is
222d0 20 70 6c 61 79 65 64 20 62 61 63 6b 20 69 66 20 played back if
222e0 70 72 65 73 65 6e 74 2e 20 46 6f 6c 6c 6f 77 69 present. Followi
222f0 6e 67 20 61 6e 79 20 68 6f 74 2d 6a 6f 75 72 6e ng any hot-journ
22300 61 6c 20 0a 2a 2a 20 20 20 20 20 20 72 6f 6c 6c al .** roll
22310 62 61 63 6b 2c 20 74 68 65 20 63 6f 6e 74 65 6e back, the conten
22320 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 ts of the cache
22330 61 72 65 20 76 61 6c 69 64 61 74 65 64 20 62 79 are validated by
22340 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 checking.**
22350 20 20 74 68 65 20 27 63 68 61 6e 67 65 2d 63 6f the 'change-co
22360 75 6e 74 65 72 27 20 66 69 65 6c 64 20 6f 66 20 unter' field of
22370 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
22380 65 20 68 65 61 64 65 72 20 61 6e 64 0a 2a 2a 20 e header and.**
22390 20 20 20 20 20 64 69 73 63 61 72 64 65 64 20 69 discarded i
223a0 66 20 74 68 65 79 20 61 72 65 20 66 6f 75 6e 64 f they are found
223b0 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 2e 0a to be invalid..
223c0 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 66 20 74 68 **.** 2) If th
223d0 65 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 e pager is runni
223e0 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d ng in exclusive-
223f0 6d 6f 64 65 2c 20 61 6e 64 20 74 68 65 72 65 20 mode, and there
22400 61 72 65 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a are currently.**
22410 20 20 20 20 20 20 6e 6f 20 6f 75 74 73 74 61 6e no outstan
22420 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 20 ding references
22430 74 6f 20 61 6e 79 20 70 61 67 65 73 2c 20 61 6e to any pages, an
22440 64 20 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f d is in the erro
22450 72 20 73 74 61 74 65 2c 0a 2a 2a 20 20 20 20 20 r state,.**
22460 20 74 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 then an attempt
22470 20 69 73 20 6d 61 64 65 20 74 6f 20 63 6c 65 61 is made to clea
22480 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 r the error stat
22490 65 20 62 79 20 64 69 73 63 61 72 64 69 6e 67 0a e by discarding.
224a0 2a 2a 20 20 20 20 20 20 74 68 65 20 63 6f 6e 74 ** the cont
224b0 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 ents of the page
224c0 20 63 61 63 68 65 20 61 6e 64 20 72 6f 6c 6c 69 cache and rolli
224d0 6e 67 20 62 61 63 6b 20 61 6e 79 20 6f 70 65 6e ng back any open
224e0 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 journal.**
224f0 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 file..**.** If
22500 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 64 65 the operation de
22510 73 63 72 69 62 65 64 20 62 79 20 28 32 29 20 61 scribed by (2) a
22520 62 6f 76 65 20 69 73 20 6e 6f 74 20 61 74 74 65 bove is not atte
22530 6d 70 74 65 64 2c 20 61 6e 64 20 69 66 20 74 68 mpted, and if th
22540 65 0a 2a 2a 20 70 61 67 65 72 20 69 73 20 69 6e e.** pager is in
22550 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 an error state
22560 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 other than SQLIT
22570 45 5f 46 55 4c 4c 20 77 68 65 6e 20 74 68 69 73 E_FULL when this
22580 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 is called,.** t
22590 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 65 he error state e
225a0 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
225b0 75 72 6e 65 64 2e 20 49 74 20 69 73 20 70 65 72 urned. It is per
225c0 6d 69 74 74 65 64 20 74 6f 20 72 65 61 64 20 74 mitted to read t
225d0 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77 he.** database w
225e0 68 65 6e 20 69 6e 20 53 51 4c 49 54 45 5f 46 55 hen in SQLITE_FU
225f0 4c 4c 20 65 72 72 6f 72 20 73 74 61 74 65 2e 0a LL error state..
22600 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c **.** Otherwise,
22610 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 if everything i
22620 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 s successful, SQ
22630 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
22640 6e 65 64 2e 20 49 66 20 61 6e 0a 2a 2a 20 49 4f ned. If an.** IO
22650 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 error occurs wh
22660 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 74 68 65 20 ile locking the
22670 64 61 74 61 62 61 73 65 2c 20 63 68 65 63 6b 69 database, checki
22680 6e 67 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 ng for a hot-jou
22690 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 6f 72 20 rnal.** file or
226a0 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a rolling back a j
226b0 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 ournal file, the
226c0 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 IO error code i
226d0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 69 s returned..*/.i
226e0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 nt sqlite3PagerS
226f0 68 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 haredLock(Pager
22700 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 *pPager){. int
22710 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
22720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
22730 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
22740 0a 20 20 69 6e 74 20 69 73 45 72 72 6f 72 52 65 . int isErrorRe
22750 73 65 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 set = 0;
22760 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
22770 20 72 65 63 6f 76 65 72 69 6e 67 20 66 72 6f 6d recovering from
22780 20 65 72 72 6f 72 20 73 74 61 74 65 20 2a 2f 0a error state */.
22790 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 . /* This routi
227a0 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 ne is only calle
227b0 64 20 66 72 6f 6d 20 62 2d 74 72 65 65 20 61 6e d from b-tree an
227c0 64 20 6f 6e 6c 79 20 77 68 65 6e 20 74 68 65 72 d only when ther
227d0 65 20 61 72 65 20 6e 6f 0a 20 20 2a 2a 20 6f 75 e are no. ** ou
227e0 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 73 20 tstanding pages
227f0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c */. assert( sql
22800 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 ite3PcacheRefCou
22810 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 nt(pPager->pPCac
22820 68 65 29 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 he)==0 );. if(
22830 4e 45 56 45 52 28 4d 45 4d 44 42 20 26 26 20 70 NEVER(MEMDB && p
22840 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 Pager->errCode)
22850 29 7b 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 ){ return pPager
22860 2d 3e 65 72 72 43 6f 64 65 3b 20 7d 0a 0a 20 20 ->errCode; }..
22870 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62 /* If this datab
22880 61 73 65 20 69 73 20 69 6e 20 61 6e 20 65 72 72 ase is in an err
22890 6f 72 2d 73 74 61 74 65 2c 20 6e 6f 77 20 69 73 or-state, now is
228a0 20 61 20 63 68 61 6e 63 65 20 74 6f 20 63 6c 65 a chance to cle
228b0 61 72 0a 20 20 2a 2a 20 74 68 65 20 65 72 72 6f ar. ** the erro
228c0 72 2e 20 44 69 73 63 61 72 64 20 74 68 65 20 63 r. Discard the c
228d0 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 ontents of the p
228e0 61 67 65 72 2d 63 61 63 68 65 20 61 6e 64 20 72 ager-cache and r
228f0 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 61 6e 79 ollback. ** any
22900 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 69 6e 20 hot journal in
22910 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e the file-system.
22920 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 . */. if( pPag
22930 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 er->errCode ){.
22940 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 if( isOpen(pP
22950 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 ager->jfd) || pP
22960 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 29 ager->zJournal )
22970 7b 0a 20 20 20 20 20 20 69 73 45 72 72 6f 72 52 {. isErrorR
22980 65 73 65 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a eset = 1;. }.
22990 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 pPager->errC
229a0 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b ode = SQLITE_OK;
229b0 0a 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 . pager_reset
229c0 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 (pPager);. }..
229d0 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 if( pPager->sta
229e0 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b te==PAGER_UNLOCK
229f0 20 7c 7c 20 69 73 45 72 72 6f 72 52 65 73 65 74 || isErrorReset
22a00 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
22a10 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 vfs * const pVfs
22a20 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b = pPager->pVfs;
22a30 0a 20 20 20 20 69 6e 74 20 69 73 48 6f 74 4a 6f . int isHotJo
22a40 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 61 urnal = 0;. a
22a50 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b ssert( !MEMDB );
22a60 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
22a70 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 ite3PcacheRefCou
22a80 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 nt(pPager->pPCac
22a90 68 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 he)==0 );. if
22aa0 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 52 65 61 ( !pPager->noRea
22ab0 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 72 dlock ){. r
22ac0 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f 6f c = pager_wait_o
22ad0 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 53 n_lock(pPager, S
22ae0 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 HARED_LOCK);.
22af0 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
22b00 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
22b10 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
22b20 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c state==PAGER_UNL
22b30 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 20 20 72 OCK );. r
22b40 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 72 6f eturn pager_erro
22b50 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 r(pPager, rc);.
22b60 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
22b70 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 if( pPager->sta
22b80 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b te==PAGER_UNLOCK
22b90 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 ){. pPager
22ba0 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f ->state = PAGER_
22bb0 53 48 41 52 45 44 3b 0a 20 20 20 20 7d 0a 20 20 SHARED;. }.
22bc0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
22bd0 2d 3e 73 74 61 74 65 3e 3d 53 48 41 52 45 44 5f ->state>=SHARED_
22be0 4c 4f 43 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 LOCK );.. /*
22bf0 49 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c If a journal fil
22c00 65 20 65 78 69 73 74 73 2c 20 61 6e 64 20 74 68 e exists, and th
22c10 65 72 65 20 69 73 20 6e 6f 20 52 45 53 45 52 56 ere is no RESERV
22c20 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 ED lock on the.
22c30 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 ** database f
22c40 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 65 69 74 ile, then it eit
22c50 68 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 her needs to be
22c60 70 6c 61 79 65 64 20 62 61 63 6b 20 6f 72 20 64 played back or d
22c70 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 eleted.. */.
22c80 20 20 20 69 66 28 20 21 69 73 45 72 72 6f 72 52 if( !isErrorR
22c90 65 73 65 74 20 29 7b 0a 20 20 20 20 20 20 61 73 eset ){. as
22ca0 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 sert( pPager->st
22cb0 61 74 65 20 3c 3d 20 50 41 47 45 52 5f 53 48 41 ate <= PAGER_SHA
22cc0 52 45 44 20 29 3b 0a 20 20 20 20 20 20 72 63 20 RED );. rc
22cd0 3d 20 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 = hasHotJournal(
22ce0 70 50 61 67 65 72 2c 20 26 69 73 48 6f 74 4a 6f pPager, &isHotJo
22cf0 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 69 66 urnal);. if
22d00 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
22d10 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 ){. goto
22d20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a failed;. }.
22d30 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 }. if( is
22d40 45 72 72 6f 72 52 65 73 65 74 20 7c 7c 20 69 73 ErrorReset || is
22d50 48 6f 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 HotJournal ){.
22d60 20 20 20 20 2f 2a 20 47 65 74 20 61 6e 20 45 58 /* Get an EX
22d70 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 CLUSIVE lock on
22d80 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
22d90 65 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 e. At this point
22da0 20 69 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 it is. **
22db0 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 important that a
22dc0 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 RESERVED lock i
22dd0 73 20 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20 6f s not obtained o
22de0 6e 20 74 68 65 20 77 61 79 20 74 6f 20 74 68 65 n the way to the
22df0 0a 20 20 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 . ** EXCLUS
22e00 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 69 74 20 IVE lock. If it
22e10 77 65 72 65 2c 20 61 6e 6f 74 68 65 72 20 70 72 were, another pr
22e20 6f 63 65 73 73 20 6d 69 67 68 74 20 6f 70 65 6e ocess might open
22e30 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 the. ** da
22e40 74 61 62 61 73 65 20 66 69 6c 65 2c 20 64 65 74 tabase file, det
22e50 65 63 74 20 74 68 65 20 52 45 53 45 52 56 45 44 ect the RESERVED
22e60 20 6c 6f 63 6b 2c 20 61 6e 64 20 63 6f 6e 63 6c lock, and concl
22e70 75 64 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 ude that the.
22e80 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69 ** database i
22e90 73 20 73 61 66 65 20 74 6f 20 72 65 61 64 20 77 s safe to read w
22ea0 68 69 6c 65 20 74 68 69 73 20 70 72 6f 63 65 73 hile this proces
22eb0 73 20 69 73 20 73 74 69 6c 6c 20 72 6f 6c 6c 69 s is still rolli
22ec0 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a ng the . **
22ed0 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 61 63 hot-journal bac
22ee0 6b 2e 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 k.. ** .
22ef0 20 20 20 2a 2a 20 42 65 63 61 75 73 65 20 74 68 ** Because th
22f00 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 52 e intermediate R
22f10 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 ESERVED lock is
22f20 6e 6f 74 20 72 65 71 75 65 73 74 65 64 2c 20 61 not requested, a
22f30 6e 79 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68 65 ny. ** othe
22f40 72 20 70 72 6f 63 65 73 73 20 61 74 74 65 6d 70 r process attemp
22f50 74 69 6e 67 20 74 6f 20 61 63 63 65 73 73 20 74 ting to access t
22f60 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
22f70 20 77 69 6c 6c 20 67 65 74 20 74 6f 20 0a 20 20 will get to .
22f80 20 20 20 20 2a 2a 20 74 68 69 73 20 70 6f 69 6e ** this poin
22f90 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20 61 6e t in the code an
22fa0 64 20 66 61 69 6c 20 74 6f 20 6f 62 74 61 69 6e d fail to obtain
22fb0 20 69 74 73 20 6f 77 6e 20 45 58 43 4c 55 53 49 its own EXCLUSI
22fc0 56 45 20 6c 6f 63 6b 20 0a 20 20 20 20 20 20 2a VE lock . *
22fd0 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 * on the databas
22fe0 65 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 2a 2f e file.. */
22ff0 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 . if( pPage
23000 72 2d 3e 73 74 61 74 65 3c 45 58 43 4c 55 53 49 r->state<EXCLUSI
23010 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 VE_LOCK ){.
23020 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
23030 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 sLock(pPager->fd
23040 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b , EXCLUSIVE_LOCK
23050 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
23060 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
23070 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 rc = p
23080 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 ager_error(pPage
23090 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20 20 r, rc);.
230a0 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 goto failed;.
230b0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
230c0 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d pPager->state =
230d0 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 PAGER_EXCLUSIVE
230e0 3b 0a 20 20 20 20 20 20 7d 0a 20 0a 20 20 20 20 ;. }. .
230f0 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f /* Open the jo
23100 75 72 6e 61 6c 20 66 6f 72 20 72 65 61 64 2f 77 urnal for read/w
23110 72 69 74 65 20 61 63 63 65 73 73 2e 20 54 68 69 rite access. Thi
23120 73 20 69 73 20 62 65 63 61 75 73 65 20 69 6e 20 s is because in
23130 0a 20 20 20 20 20 20 2a 2a 20 65 78 63 6c 75 73 . ** exclus
23140 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 ive-access mode
23150 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
23160 74 6f 72 20 77 69 6c 6c 20 62 65 20 6b 65 70 74 tor will be kept
23170 20 6f 70 65 6e 20 61 6e 64 0a 20 20 20 20 20 20 open and.
23180 2a 2a 20 70 6f 73 73 69 62 6c 79 20 75 73 65 64 ** possibly used
23190 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69 for a transacti
231a0 6f 6e 20 6c 61 74 65 72 20 6f 6e 2e 20 4f 6e 20 on later on. On
231b0 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20 74 68 some systems, th
231c0 65 0a 20 20 20 20 20 20 2a 2a 20 4f 73 54 72 75 e. ** OsTru
231d0 6e 63 61 74 65 28 29 20 63 61 6c 6c 20 75 73 65 ncate() call use
231e0 64 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 61 d in exclusive-a
231f0 63 63 65 73 73 20 6d 6f 64 65 20 61 6c 73 6f 20 ccess mode also
23200 72 65 71 75 69 72 65 73 0a 20 20 20 20 20 20 2a requires. *
23210 2a 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 66 * a read/write f
23220 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 20 20 20 20 ile handle..
23230 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21 */. if( !
23240 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
23250 66 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 fd) ){. i
23260 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 20 20 nt res;.
23270 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 rc = sqlite3OsAc
23280 63 65 73 73 28 70 56 66 73 2c 70 50 61 67 65 72 cess(pVfs,pPager
23290 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 53 51 4c 49 54 ->zJournal,SQLIT
232a0 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c E_ACCESS_EXISTS,
232b0 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 &res);. i
232c0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
232d0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ){. if
232e0 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 20 ( res ){.
232f0 20 20 20 20 20 69 6e 74 20 66 6f 75 74 20 3d 20 int fout =
23300 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 0;. i
23310 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f 50 nt f = SQLITE_OP
23320 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c EN_READWRITE|SQL
23330 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f ITE_OPEN_MAIN_JO
23340 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 URNAL;.
23350 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67 assert( !pPag
23360 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a er->tempFile );.
23370 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
23380 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 sqlite3OsOpen(p
23390 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f Vfs, pPager->zJo
233a0 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a urnal, pPager->j
233b0 66 64 2c 20 66 2c 20 26 66 6f 75 74 29 3b 0a 20 fd, f, &fout);.
233c0 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 asser
233d0 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b t( rc!=SQLITE_OK
233e0 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 || isOpen(pPage
233f0 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 20 20 20 r->jfd) );.
23400 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
23410 51 4c 49 54 45 5f 4f 4b 20 26 26 20 66 6f 75 74 QLITE_OK && fout
23420 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 &SQLITE_OPEN_REA
23430 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 20 DONLY ){.
23440 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
23450 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 TE_CANTOPEN;.
23460 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
23470 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 e3OsClose(pPager
23480 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 20 20 ->jfd);.
23490 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
234a0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
234b0 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 /* If the jou
234c0 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 20 65 78 rnal does not ex
234d0 69 73 74 2c 20 69 74 20 75 73 75 61 6c 6c 79 20 ist, it usually
234e0 6d 65 61 6e 73 20 74 68 61 74 20 73 6f 6d 65 20 means that some
234f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . **
23500 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e other connection
23510 20 6d 61 6e 61 67 65 64 20 74 6f 20 67 65 74 20 managed to get
23520 69 6e 20 61 6e 64 20 72 6f 6c 6c 20 69 74 20 62 in and roll it b
23530 61 63 6b 20 62 65 66 6f 72 65 20 0a 20 20 20 20 ack before .
23540 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 ** this
23550 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 74 61 69 connection obtai
23560 6e 65 64 20 74 68 65 20 65 78 63 6c 75 73 69 76 ned the exclusiv
23570 65 20 6c 6f 63 6b 20 61 62 6f 76 65 2e 20 4f 72 e lock above. Or
23580 2c 20 69 74 20 0a 20 20 20 20 20 20 20 20 20 20 , it .
23590 20 20 2a 2a 20 6d 61 79 20 6d 65 61 6e 20 74 68 ** may mean th
235a0 61 74 20 74 68 65 20 70 61 67 65 72 20 77 61 73 at the pager was
235b0 20 69 6e 20 74 68 65 20 65 72 72 6f 72 2d 73 74 in the error-st
235c0 61 74 65 20 77 68 65 6e 20 74 68 69 73 0a 20 20 ate when this.
235d0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e ** fun
235e0 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 ction was called
235f0 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c and the journal
23600 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 file does not e
23610 78 69 73 74 2e 20 20 2a 2f 0a 20 20 20 20 20 20 xist. */.
23620 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
23630 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e _end_transaction
23640 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 (pPager, 0);.
23650 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
23660 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
23670 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
23680 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
23690 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 to failed;.
236a0 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 }.. /* TOD
236b0 4f 3a 20 57 68 79 20 61 72 65 20 74 68 65 73 65 O: Why are these
236c0 20 63 6c 65 61 72 65 64 20 68 65 72 65 3f 20 49 cleared here? I
236d0 73 20 69 74 20 6e 65 63 65 73 73 61 72 79 3f 20 s it necessary?
236e0 2a 2f 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d */. pPager-
236f0 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 >journalStarted
23700 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 = 0;. pPage
23710 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
23720 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 0;. pPager-
23730 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a >setMaster = 0;.
23740 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f pPager->jo
23750 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20 0a urnalHdr = 0;. .
23760 20 20 20 20 20 20 2f 2a 20 50 6c 61 79 62 61 63 /* Playbac
23770 6b 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 k and delete the
23780 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 72 6f 70 20 journal. Drop
23790 74 68 65 20 64 61 74 61 62 61 73 65 20 77 72 69 the database wri
237a0 74 65 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 63 6b te. ** lock
237b0 20 61 6e 64 20 72 65 61 63 71 75 69 72 65 20 74 and reacquire t
237c0 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 20 50 75 he read lock. Pu
237d0 72 67 65 20 74 68 65 20 63 61 63 68 65 20 62 65 rge the cache be
237e0 66 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 70 6c fore. ** pl
237f0 61 79 69 6e 67 20 62 61 63 6b 20 74 68 65 20 68 aying back the h
23800 6f 74 2d 6a 6f 75 72 6e 61 6c 20 73 6f 20 74 68 ot-journal so th
23810 61 74 20 77 65 20 64 6f 6e 27 74 20 65 6e 64 20 at we don't end
23820 75 70 20 77 69 74 68 0a 20 20 20 20 20 20 2a 2a up with. **
23830 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 an inconsistent
23840 20 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a 2f cache.. */
23850 0a 20 20 20 20 20 20 69 66 28 20 69 73 4f 70 65 . if( isOpe
23860 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 n(pPager->jfd) )
23870 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 {. rc = p
23880 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 ager_playback(pP
23890 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 20 20 ager, 1);.
238a0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
238b0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
238c0 20 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72 6f rc = pager_erro
238d0 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 r(pPager, rc);.
238e0 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 goto fa
238f0 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a iled;. }.
23900 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 }. as
23910 73 65 72 74 28 20 28 70 50 61 67 65 72 2d 3e 73 sert( (pPager->s
23920 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 tate==PAGER_SHAR
23930 45 44 29 0a 20 20 20 20 20 20 20 20 20 20 20 7c ED). |
23940 7c 20 28 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 | (pPager->exclu
23950 73 69 76 65 4d 6f 64 65 20 26 26 20 70 50 61 67 siveMode && pPag
23960 65 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 52 5f er->state>PAGER_
23970 53 48 41 52 45 44 29 0a 20 20 20 20 20 20 29 3b SHARED). );
23980 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
23990 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 pPager->pBackup
239a0 7c 7c 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 || sqlite3Pcache
239b0 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 Pagecount(pPager
239c0 2d 3e 70 50 43 61 63 68 65 29 3e 30 20 29 7b 0a ->pPCache)>0 ){.
239d0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 68 61 /* The sha
239e0 72 65 64 2d 6c 6f 63 6b 20 68 61 73 20 6a 75 73 red-lock has jus
239f0 74 20 62 65 65 6e 20 61 63 71 75 69 72 65 64 20 t been acquired
23a00 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
23a10 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e file. ** an
23a20 64 20 74 68 65 72 65 20 61 72 65 20 61 6c 72 65 d there are alre
23a30 61 64 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 ady pages in the
23a40 20 63 61 63 68 65 20 28 66 72 6f 6d 20 61 20 70 cache (from a p
23a50 72 65 76 69 6f 75 73 0a 20 20 20 20 20 20 2a 2a revious. **
23a60 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 read or write t
23a70 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 20 43 68 ransaction). Ch
23a80 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 eck to see if th
23a90 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 20 e database.
23aa0 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 ** has been mod
23ab0 69 66 69 65 64 2e 20 20 49 66 20 74 68 65 20 64 ified. If the d
23ac0 61 74 61 62 61 73 65 20 68 61 73 20 63 68 61 6e atabase has chan
23ad0 67 65 64 2c 20 66 6c 75 73 68 20 74 68 65 0a 20 ged, flush the.
23ae0 20 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 0a 20 ** cache..
23af0 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
23b00 20 44 61 74 61 62 61 73 65 20 63 68 61 6e 67 65 Database change
23b10 73 20 69 73 20 64 65 74 65 63 74 65 64 20 62 79 s is detected by
23b20 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 35 20 62 looking at 15 b
23b30 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 67 0a 20 ytes beginning.
23b40 20 20 20 20 20 2a 2a 20 61 74 20 6f 66 66 73 65 ** at offse
23b50 74 20 32 34 20 69 6e 74 6f 20 74 68 65 20 66 69 t 24 into the fi
23b60 6c 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 34 le. The first 4
23b70 20 6f 66 20 74 68 65 73 65 20 31 36 20 62 79 74 of these 16 byt
23b80 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 es are. **
23b90 61 20 33 32 2d 62 69 74 20 63 6f 75 6e 74 65 72 a 32-bit counter
23ba0 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d 65 that is increme
23bb0 6e 74 65 64 20 77 69 74 68 20 65 61 63 68 20 63 nted with each c
23bc0 68 61 6e 67 65 2e 20 20 54 68 65 0a 20 20 20 20 hange. The.
23bd0 20 20 2a 2a 20 6f 74 68 65 72 20 62 79 74 65 73 ** other bytes
23be0 20 63 68 61 6e 67 65 20 72 61 6e 64 6f 6d 6c 79 change randomly
23bf0 20 77 69 74 68 20 65 61 63 68 20 66 69 6c 65 20 with each file
23c00 63 68 61 6e 67 65 20 77 68 65 6e 0a 20 20 20 20 change when.
23c10 20 20 2a 2a 20 61 20 63 6f 64 65 63 20 69 73 20 ** a codec is
23c20 69 6e 20 75 73 65 2e 0a 20 20 20 20 20 20 2a 2a in use.. **
23c30 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 . ** There
23c40 20 69 73 20 61 20 76 61 6e 69 73 68 69 6e 67 6c is a vanishingl
23c50 79 20 73 6d 61 6c 6c 20 63 68 61 6e 63 65 20 74 y small chance t
23c60 68 61 74 20 61 20 63 68 61 6e 67 65 20 77 69 6c hat a change wil
23c70 6c 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 20 20 l not be .
23c80 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 20 54 68 ** detected. Th
23c90 65 20 63 68 61 6e 63 65 20 6f 66 20 61 6e 20 75 e chance of an u
23ca0 6e 64 65 74 65 63 74 65 64 20 63 68 61 6e 67 65 ndetected change
23cb0 20 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68 61 is so small tha
23cc0 74 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 63 61 t. ** it ca
23cd0 6e 20 62 65 20 6e 65 67 6c 65 63 74 65 64 2e 0a n be neglected..
23ce0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 */. c
23cf0 68 61 72 20 64 62 46 69 6c 65 56 65 72 73 5b 73 har dbFileVers[s
23d00 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 izeof(pPager->db
23d10 46 69 6c 65 56 65 72 73 29 5d 3b 0a 20 20 20 20 FileVers)];.
23d20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 sqlite3PagerPa
23d30 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 gecount(pPager,
23d40 30 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 0);.. if( p
23d50 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 Pager->errCode )
23d60 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 {. rc = p
23d70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a Pager->errCode;.
23d80 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 goto fai
23d90 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 led;. }..
23da0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
23db0 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 er->dbSizeValid
23dc0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 );. if( pPa
23dd0 67 65 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29 7b ger->dbSize>0 ){
23de0 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 . IOTRACE
23df0 28 28 22 43 4b 56 45 52 53 20 25 70 20 25 64 5c (("CKVERS %p %d\
23e00 6e 22 2c 20 70 50 61 67 65 72 2c 20 73 69 7a 65 n", pPager, size
23e10 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 29 29 of(dbFileVers)))
23e20 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
23e30 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 qlite3OsRead(pPa
23e40 67 65 72 2d 3e 66 64 2c 20 26 64 62 46 69 6c 65 ger->fd, &dbFile
23e50 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 62 46 Vers, sizeof(dbF
23e60 69 6c 65 56 65 72 73 29 2c 20 32 34 29 3b 0a 20 ileVers), 24);.
23e70 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
23e80 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
23e90 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 goto faile
23ea0 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 d;. }.
23eb0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
23ec0 20 20 6d 65 6d 73 65 74 28 64 62 46 69 6c 65 56 memset(dbFileV
23ed0 65 72 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 ers, 0, sizeof(d
23ee0 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 bFileVers));.
23ef0 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 }.. if(
23f00 6d 65 6d 63 6d 70 28 70 50 61 67 65 72 2d 3e 64 memcmp(pPager->d
23f10 62 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69 6c bFileVers, dbFil
23f20 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 62 eVers, sizeof(db
23f30 46 69 6c 65 56 65 72 73 29 29 21 3d 30 20 29 7b FileVers))!=0 ){
23f40 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 . pager_r
23f50 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 eset(pPager);.
23f60 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
23f70 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
23f80 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c exclusiveMode ||
23f90 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d pPager->state==
23fa0 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a PAGER_SHARED );.
23fb0 20 20 7d 0a 0a 20 66 61 69 6c 65 64 3a 0a 20 20 }.. failed:.
23fc0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
23fd0 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67 65 K ){. /* page
23fe0 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 61 20 r_unlock() is a
23ff0 6e 6f 2d 6f 70 20 66 6f 72 20 65 78 63 6c 75 73 no-op for exclus
24000 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 6e 2d ive mode and in-
24010 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 73 memory databases
24020 2e 20 2a 2f 0a 20 20 20 20 70 61 67 65 72 5f 75 . */. pager_u
24030 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 nlock(pPager);.
24040 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
24050 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 }../*.** If the
24060 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 reference count
24070 68 61 73 20 72 65 61 63 68 65 64 20 7a 65 72 6f has reached zero
24080 2c 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 , rollback any a
24090 63 74 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61 63 ctive.** transac
240a0 74 69 6f 6e 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 tion and unlock
240b0 74 68 65 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a 2a the pager..**.**
240c0 20 45 78 63 65 70 74 2c 20 69 6e 20 6c 6f 63 6b Except, in lock
240d0 69 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53 49 ing_mode=EXCLUSI
240e0 56 45 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 VE when there is
240f0 20 6e 6f 74 68 69 6e 67 20 74 6f 20 69 6e 0a 2a nothing to in.*
24100 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a * the rollback j
24110 6f 75 72 6e 61 6c 2c 20 74 68 65 20 75 6e 6c 6f ournal, the unlo
24120 63 6b 20 69 73 20 6e 6f 74 20 70 65 72 66 6f 72 ck is not perfor
24130 6d 65 64 20 61 6e 64 20 74 68 65 72 65 20 69 73 med and there is
24140 0a 2a 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 72 .** nothing to r
24150 6f 6c 6c 62 61 63 6b 2c 20 73 6f 20 74 68 69 73 ollback, so this
24160 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
24170 2d 6f 70 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 -op..*/ .static
24180 76 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b void pagerUnlock
24190 49 66 55 6e 75 73 65 64 28 50 61 67 65 72 20 2a IfUnused(Pager *
241a0 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 28 pPager){. if( (
241b0 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 sqlite3PcacheRef
241c0 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 Count(pPager->pP
241d0 43 61 63 68 65 29 3d 3d 30 29 0a 20 20 20 26 26 Cache)==0). &&
241e0 20 28 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 (!pPager->exclu
241f0 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 siveMode || pPag
24200 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 er->journalOff>0
24210 29 20 0a 20 20 29 7b 0a 20 20 20 20 70 61 67 65 ) . ){. page
24220 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 rUnlockAndRollba
24230 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a ck(pPager);. }.
24240 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 }../*.** Acquire
24250 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 a reference to
24260 70 61 67 65 20 6e 75 6d 62 65 72 20 70 67 6e 6f page number pgno
24270 20 69 6e 20 70 61 67 65 72 20 70 50 61 67 65 72 in pager pPager
24280 20 28 61 20 70 61 67 65 0a 2a 2a 20 72 65 66 65 (a page.** refe
24290 72 65 6e 63 65 20 68 61 73 20 74 79 70 65 20 44 rence has type D
242a0 62 50 61 67 65 2a 29 2e 20 49 66 20 74 68 65 20 bPage*). If the
242b0 72 65 71 75 65 73 74 65 64 20 72 65 66 65 72 65 requested refere
242c0 6e 63 65 20 69 73 20 0a 2a 2a 20 73 75 63 63 65 nce is .** succe
242d0 73 73 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65 64 ssfully obtained
242e0 2c 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 74 , it is copied t
242f0 6f 20 2a 70 70 50 61 67 65 20 61 6e 64 20 53 51 o *ppPage and SQ
24300 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 LITE_OK returned
24310 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 ..**.** If the r
24320 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73 equested page is
24330 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
24340 63 61 63 68 65 2c 20 69 74 20 69 73 20 72 65 74 cache, it is ret
24350 75 72 6e 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 urned. .** Other
24360 77 69 73 65 2c 20 61 20 6e 65 77 20 70 61 67 65 wise, a new page
24370 20 6f 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f 63 object is alloc
24380 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61 74 ated and populat
24390 65 64 20 77 69 74 68 20 64 61 74 61 0a 2a 2a 20 ed with data.**
243a0 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 read from the da
243b0 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 6e 20 tabase file. In
243c0 73 6f 6d 65 20 63 61 73 65 73 2c 20 74 68 65 20 some cases, the
243d0 70 63 61 63 68 65 20 6d 6f 64 75 6c 65 20 6d 61 pcache module ma
243e0 79 0a 2a 2a 20 63 68 6f 6f 73 65 20 6e 6f 74 20 y.** choose not
243f0 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 to allocate a ne
24400 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61 6e w page object an
24410 64 20 6d 61 79 20 72 65 75 73 65 20 61 6e 20 65 d may reuse an e
24420 78 69 73 74 69 6e 67 0a 2a 2a 20 6f 62 6a 65 63 xisting.** objec
24430 74 20 77 69 74 68 20 6e 6f 20 6f 75 74 73 74 61 t with no outsta
24440 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 nding references
24450 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 78 74 72 ..**.** The extr
24460 61 20 64 61 74 61 20 61 70 70 65 6e 64 65 64 20 a data appended
24470 74 6f 20 61 20 70 61 67 65 20 69 73 20 61 6c 77 to a page is alw
24480 61 79 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 ays initialized
24490 74 6f 20 7a 65 72 6f 73 20 74 68 65 20 0a 2a 2a to zeros the .**
244a0 20 66 69 72 73 74 20 74 69 6d 65 20 61 20 70 61 first time a pa
244b0 67 65 20 69 73 20 6c 6f 61 64 65 64 20 69 6e 74 ge is loaded int
244c0 6f 20 6d 65 6d 6f 72 79 2e 20 49 66 20 74 68 65 o memory. If the
244d0 20 70 61 67 65 20 72 65 71 75 65 73 74 65 64 20 page requested
244e0 69 73 20 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 is .** already i
244f0 6e 20 74 68 65 20 63 61 63 68 65 20 77 68 65 6e n the cache when
24500 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
24510 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 74 s called, then t
24520 68 65 20 65 78 74 72 61 0a 2a 2a 20 64 61 74 61 he extra.** data
24530 20 69 73 20 6c 65 66 74 20 61 73 20 69 74 20 77 is left as it w
24540 61 73 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 as when the page
24550 20 6f 62 6a 65 63 74 20 77 61 73 20 6c 61 73 74 object was last
24560 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 used..**.** If
24570 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 the database ima
24580 67 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 ge is smaller th
24590 61 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 64 an the requested
245a0 20 70 61 67 65 20 6f 72 20 69 66 20 61 20 0a 2a page or if a .*
245b0 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 * non-zero value
245c0 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 is passed as th
245d0 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 70 61 72 61 e noContent para
245e0 6d 65 74 65 72 20 61 6e 64 20 74 68 65 20 0a 2a meter and the .*
245f0 2a 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 * requested page
24600 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 is not already
24610 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61 stored in the ca
24620 63 68 65 2c 20 74 68 65 6e 20 6e 6f 20 0a 2a 2a che, then no .**
24630 20 61 63 74 75 61 6c 20 64 69 73 6b 20 72 65 61 actual disk rea
24640 64 20 6f 63 63 75 72 73 2e 20 49 6e 20 74 68 69 d occurs. In thi
24650 73 20 63 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 s case the memor
24660 79 20 69 6d 61 67 65 20 6f 66 20 74 68 65 20 0a y image of the .
24670 2a 2a 20 70 61 67 65 20 69 73 20 69 6e 69 74 69 ** page is initi
24680 61 6c 69 7a 65 64 20 74 6f 20 61 6c 6c 20 7a 65 alized to all ze
24690 72 6f 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 6e ros. .**.** If n
246a0 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74 72 75 65 oContent is true
246b0 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 , it means that
246c0 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 we do not care a
246d0 62 6f 75 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 bout the content
246e0 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 s.** of the page
246f0 2e 20 54 68 69 73 20 6f 63 63 75 72 73 20 69 6e . This occurs in
24700 20 74 77 6f 20 73 65 70 65 72 61 74 65 20 73 63 two seperate sc
24710 65 6e 61 72 69 6f 73 3a 0a 2a 2a 0a 2a 2a 20 20 enarios:.**.**
24720 20 61 29 20 57 68 65 6e 20 72 65 61 64 69 6e 67 a) When reading
24730 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 a free-list lea
24740 66 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 f page from the
24750 64 61 74 61 62 61 73 65 2c 20 61 6e 64 0a 2a 2a database, and.**
24760 0a 2a 2a 20 20 20 62 29 20 57 68 65 6e 20 61 20 .** b) When a
24770 73 61 76 65 70 6f 69 6e 74 20 69 73 20 62 65 69 savepoint is bei
24780 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 ng rolled back a
24790 6e 64 20 77 65 20 6e 65 65 64 20 74 6f 20 6c 6f nd we need to lo
247a0 61 64 0a 2a 2a 20 20 20 20 20 20 61 20 6e 65 77 ad.** a new
247b0 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 63 page into the c
247c0 61 63 68 65 20 74 6f 20 70 6f 70 75 6c 61 74 65 ache to populate
247d0 20 77 69 74 68 20 74 68 65 20 64 61 74 61 20 72 with the data r
247e0 65 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f 6d ead.** from
247f0 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 6a the savepoint j
24800 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 ournal..**.** If
24810 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74 72 noContent is tr
24820 75 65 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 ue, then the dat
24830 61 20 72 65 74 75 72 6e 65 64 20 69 73 20 7a 65 a returned is ze
24840 72 6f 65 64 20 69 6e 73 74 65 61 64 20 6f 66 0a roed instead of.
24850 2a 2a 20 62 65 69 6e 67 20 72 65 61 64 20 66 72 ** being read fr
24860 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e om the database.
24870 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 74 Additionally, t
24880 68 65 20 62 69 74 73 20 63 6f 72 72 65 73 70 6f he bits correspo
24890 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 70 67 6e 6f nding.** to pgno
248a0 20 69 6e 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 in Pager.pInJou
248b0 72 6e 61 6c 20 28 62 69 74 76 65 63 20 6f 66 20 rnal (bitvec of
248c0 70 61 67 65 73 20 61 6c 72 65 61 64 79 20 77 72 pages already wr
248d0 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 itten to the.**
248e0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 61 6e journal file) an
248f0 64 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 d the PagerSavep
24900 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e oint.pInSavepoin
24910 74 20 62 69 74 76 65 63 73 20 6f 66 20 61 6e 79 t bitvecs of any
24920 20 6f 70 65 6e 0a 2a 2a 20 73 61 76 65 70 6f 69 open.** savepoi
24930 6e 74 73 20 61 72 65 20 73 65 74 2e 20 54 68 69 nts are set. Thi
24940 73 20 6d 65 61 6e 73 20 69 66 20 74 68 65 20 70 s means if the p
24950 61 67 65 20 69 73 20 6d 61 64 65 20 77 72 69 74 age is made writ
24960 61 62 6c 65 20 61 74 20 61 6e 79 0a 2a 2a 20 70 able at any.** p
24970 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 oint in the futu
24980 72 65 2c 20 75 73 69 6e 67 20 61 20 63 61 6c 6c re, using a call
24990 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 to sqlite3Pager
249a0 57 72 69 74 65 28 29 2c 20 69 74 73 20 63 6f 6e Write(), its con
249b0 74 65 6e 74 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f tents.** will no
249c0 74 20 62 65 20 6a 6f 75 72 6e 61 6c 65 64 2e 20 t be journaled.
249d0 54 68 69 73 20 73 61 76 65 73 20 49 4f 2e 0a 2a This saves IO..*
249e0 2a 0a 2a 2a 20 54 68 65 20 61 63 71 75 69 73 69 *.** The acquisi
249f0 74 69 6f 6e 20 6d 69 67 68 74 20 66 61 69 6c 20 tion might fail
24a00 66 6f 72 20 73 65 76 65 72 61 6c 20 72 65 61 73 for several reas
24a10 6f 6e 73 2e 20 20 49 6e 20 61 6c 6c 20 63 61 73 ons. In all cas
24a20 65 73 2c 0a 2a 2a 20 61 6e 20 61 70 70 72 6f 70 es,.** an approp
24a30 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 riate error code
24a40 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
24a50 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74 20 *ppPage is set
24a60 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 to NULL..**.** S
24a70 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 ee also sqlite3P
24a80 61 67 65 72 4c 6f 6f 6b 75 70 28 29 2e 20 20 42 agerLookup(). B
24a90 6f 74 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65 oth this routine
24aa0 20 61 6e 64 20 4c 6f 6f 6b 75 70 28 29 20 61 74 and Lookup() at
24ab0 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 66 69 6e 64 tempt.** to find
24ac0 20 61 20 70 61 67 65 20 69 6e 20 74 68 65 20 69 a page in the i
24ad0 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 66 n-memory cache f
24ae0 69 72 73 74 2e 20 20 49 66 20 74 68 65 20 70 61 irst. If the pa
24af0 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 ge is not alread
24b00 79 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 2c 20 y.** in memory,
24b10 74 68 69 73 20 72 6f 75 74 69 6e 65 20 67 6f 65 this routine goe
24b20 73 20 74 6f 20 64 69 73 6b 20 74 6f 20 72 65 61 s to disk to rea
24b30 64 20 69 74 20 69 6e 20 77 68 65 72 65 61 73 20 d it in whereas
24b40 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 6a 75 73 74 Lookup().** just
24b50 20 72 65 74 75 72 6e 73 20 30 2e 20 20 54 68 69 returns 0. Thi
24b60 73 20 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72 s routine acquir
24b70 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 74 es a read-lock t
24b80 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69 74 he first time it
24b90 0a 2a 2a 20 68 61 73 20 74 6f 20 67 6f 20 74 6f .** has to go to
24ba0 20 64 69 73 6b 2c 20 61 6e 64 20 63 6f 75 6c 64 disk, and could
24bb0 20 61 6c 73 6f 20 70 6c 61 79 62 61 63 6b 20 61 also playback a
24bc0 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 69 66 n old journal if
24bd0 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 53 necessary..** S
24be0 69 6e 63 65 20 4c 6f 6f 6b 75 70 28 29 20 6e 65 ince Lookup() ne
24bf0 76 65 72 20 67 6f 65 73 20 74 6f 20 64 69 73 6b ver goes to disk
24c00 2c 20 69 74 20 6e 65 76 65 72 20 68 61 73 20 74 , it never has t
24c10 6f 20 64 65 61 6c 20 77 69 74 68 20 6c 6f 63 6b o deal with lock
24c20 73 0a 2a 2a 20 6f 72 20 6a 6f 75 72 6e 61 6c 20 s.** or journal
24c30 66 69 6c 65 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 files..*/.int sq
24c40 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 lite3PagerAcquir
24c50 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 e(. Pager *pPag
24c60 65 72 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 er, /* The
24c70 70 61 67 65 72 20 6f 70 65 6e 20 6f 6e 20 74 68 pager open on th
24c80 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
24c90 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 */. Pgno pgno,
24ca0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
24cb0 20 6e 75 6d 62 65 72 20 74 6f 20 66 65 74 63 68 number to fetch
24cc0 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 2a 70 */. DbPage **p
24cd0 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 57 72 69 pPage, /* Wri
24ce0 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 te a pointer to
24cf0 74 68 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f the page here */
24d00 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 . int noContent
24d10 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 /* Do not
24d20 20 62 6f 74 68 65 72 20 72 65 61 64 69 6e 67 20 bother reading
24d30 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 64 69 73 content from dis
24d40 6b 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a k if true */.){.
24d50 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 48 64 int rc;. PgHd
24d60 72 20 2a 70 50 67 3b 0a 0a 20 20 61 73 73 65 72 r *pPg;.. asser
24d70 74 28 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f t( assert_pager_
24d80 73 74 61 74 65 28 70 50 61 67 65 72 29 20 29 3b state(pPager) );
24d90 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
24da0 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 52 5f 55 r->state>PAGER_U
24db0 4e 4c 4f 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20 NLOCK );.. if(
24dc0 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 72 pgno==0 ){. r
24dd0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
24de0 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a RUPT_BKPT;. }..
24df0 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 /* If the page
24e00 72 20 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f r is in the erro
24e10 72 20 73 74 61 74 65 2c 20 72 65 74 75 72 6e 20 r state, return
24e20 61 6e 20 65 72 72 6f 72 20 69 6d 6d 65 64 69 61 an error immedia
24e30 74 65 6c 79 2e 20 0a 20 20 2a 2a 20 4f 74 68 65 tely. . ** Othe
24e40 72 77 69 73 65 2c 20 72 65 71 75 65 73 74 20 74 rwise, request t
24e50 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 he page from the
24e60 20 50 43 61 63 68 65 20 6c 61 79 65 72 2e 20 2a PCache layer. *
24e70 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e /. if( pPager->
24e80 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f errCode!=SQLITE_
24e90 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 OK && pPager->er
24ea0 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 55 rCode!=SQLITE_FU
24eb0 4c 4c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 LL ){. rc = p
24ec0 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a Pager->errCode;.
24ed0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 }else{. rc
24ee0 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 = sqlite3PcacheF
24ef0 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 etch(pPager->pPC
24f00 61 63 68 65 2c 20 70 67 6e 6f 2c 20 31 2c 20 70 ache, pgno, 1, p
24f10 70 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 pPage);. }.. i
24f20 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
24f30 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 ){. /* Eithe
24f40 72 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 r the call to sq
24f50 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 lite3PcacheFetch
24f60 28 29 20 72 65 74 75 72 6e 65 64 20 61 6e 20 65 () returned an e
24f70 72 72 6f 72 20 6f 72 20 74 68 65 0a 20 20 20 20 rror or the.
24f80 2a 2a 20 70 61 67 65 72 20 77 61 73 20 61 6c 72 ** pager was alr
24f90 65 61 64 79 20 69 6e 20 74 68 65 20 65 72 72 6f eady in the erro
24fa0 72 2d 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 r-state when thi
24fb0 73 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 s function was c
24fc0 61 6c 6c 65 64 2e 0a 20 20 20 20 2a 2a 20 53 65 alled.. ** Se
24fd0 74 20 70 50 67 20 74 6f 20 30 20 61 6e 64 20 6a t pPg to 0 and j
24fe0 75 6d 70 20 74 6f 20 74 68 65 20 65 78 63 65 70 ump to the excep
24ff0 74 69 6f 6e 20 68 61 6e 64 6c 65 72 2e 20 20 2a tion handler. *
25000 2f 0a 20 20 20 20 70 50 67 20 3d 20 30 3b 0a 20 /. pPg = 0;.
25010 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 goto pager_ac
25020 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 7d 0a 20 quire_err;. }.
25030 20 61 73 73 65 72 74 28 20 28 2a 70 70 50 61 67 assert( (*ppPag
25040 65 29 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 20 29 e)->pgno==pgno )
25050 3b 0a 20 20 61 73 73 65 72 74 28 20 28 2a 70 70 ;. assert( (*pp
25060 50 61 67 65 29 2d 3e 70 50 61 67 65 72 3d 3d 70 Page)->pPager==p
25070 50 61 67 65 72 20 7c 7c 20 28 2a 70 70 50 61 67 Pager || (*ppPag
25080 65 29 2d 3e 70 50 61 67 65 72 3d 3d 30 20 29 3b e)->pPager==0 );
25090 0a 0a 20 20 69 66 28 20 28 2a 70 70 50 61 67 65 .. if( (*ppPage
250a0 29 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20 20 20 )->pPager ){.
250b0 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 /* In this case
250c0 20 74 68 65 20 70 63 61 63 68 65 20 61 6c 72 65 the pcache alre
250d0 61 64 79 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 ady contains an
250e0 69 6e 69 74 69 61 6c 69 7a 65 64 20 63 6f 70 79 initialized copy
250f0 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 of. ** the p
25100 61 67 65 2e 20 52 65 74 75 72 6e 20 77 69 74 68 age. Return with
25110 6f 75 74 20 66 75 72 74 68 65 72 20 61 64 6f 2e out further ado.
25120 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
25130 20 70 67 6e 6f 3c 3d 50 41 47 45 52 5f 4d 41 58 pgno<=PAGER_MAX
25140 5f 50 47 4e 4f 20 26 26 20 70 67 6e 6f 21 3d 50 _PGNO && pgno!=P
25150 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 AGER_MJ_PGNO(pPa
25160 67 65 72 29 20 29 3b 0a 20 20 20 20 50 41 47 45 ger) );. PAGE
25170 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e R_INCR(pPager->n
25180 48 69 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e Hit);. return
25190 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 7d SQLITE_OK;.. }
251a0 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 else{. /* The
251b0 20 70 61 67 65 72 20 63 61 63 68 65 20 68 61 73 pager cache has
251c0 20 63 72 65 61 74 65 64 20 61 20 6e 65 77 20 70 created a new p
251d0 61 67 65 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 age. Its content
251e0 20 6e 65 65 64 73 20 74 6f 20 0a 20 20 20 20 2a needs to . *
251f0 2a 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 * be initialized
25200 2e 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 4d . */. int nM
25210 61 78 3b 0a 0a 20 20 20 20 50 41 47 45 52 5f 49 ax;.. PAGER_I
25220 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 4d 69 73 NCR(pPager->nMis
25230 73 29 3b 0a 20 20 20 20 70 50 67 20 3d 20 2a 70 s);. pPg = *p
25240 70 50 61 67 65 3b 0a 20 20 20 20 70 50 67 2d 3e pPage;. pPg->
25250 70 50 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b pPager = pPager;
25260 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 .. /* The max
25270 69 6d 75 6d 20 70 61 67 65 20 6e 75 6d 62 65 72 imum page number
25280 20 69 73 20 32 5e 33 31 2e 20 52 65 74 75 72 6e is 2^31. Return
25290 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 SQLITE_CORRUPT
252a0 69 66 20 61 20 70 61 67 65 0a 20 20 20 20 2a 2a if a page. **
252b0 20 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 number greater
252c0 74 68 61 6e 20 74 68 69 73 2c 20 6f 72 20 74 68 than this, or th
252d0 65 20 75 6e 75 73 65 64 20 6c 6f 63 6b 69 6e 67 e unused locking
252e0 2d 70 61 67 65 2c 20 69 73 20 72 65 71 75 65 73 -page, is reques
252f0 74 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 ted. */. if(
25300 70 67 6e 6f 3e 50 41 47 45 52 5f 4d 41 58 5f 50 pgno>PAGER_MAX_P
25310 47 4e 4f 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 GNO || pgno==PAG
25320 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 ER_MJ_PGNO(pPage
25330 72 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d r) ){. rc =
25340 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
25350 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f BKPT;. goto
25360 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 pager_acquire_e
25370 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 rr;. }.. r
25380 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
25390 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 Pagecount(pPager
253a0 2c 20 26 6e 4d 61 78 29 3b 0a 20 20 20 20 69 66 , &nMax);. if
253b0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
253c0 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 ){. goto pa
253d0 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b ger_acquire_err;
253e0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
253f0 6e 4d 61 78 3c 28 69 6e 74 29 70 67 6e 6f 20 7c nMax<(int)pgno |
25400 7c 20 4d 45 4d 44 42 20 7c 7c 20 6e 6f 43 6f 6e | MEMDB || noCon
25410 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 69 66 tent ){. if
25420 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6d ( pgno>pPager->m
25430 78 50 67 6e 6f 20 29 7b 0a 09 72 63 20 3d 20 53 xPgno ){..rc = S
25440 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 09 67 6f 74 QLITE_FULL;..got
25450 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f o pager_acquire_
25460 65 72 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 err;. }.
25470 20 20 20 69 66 28 20 6e 6f 43 6f 6e 74 65 6e 74 if( noContent
25480 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 ){. /* F
25490 61 69 6c 75 72 65 20 74 6f 20 73 65 74 20 74 68 ailure to set th
254a0 65 20 62 69 74 73 20 69 6e 20 74 68 65 20 49 6e e bits in the In
254b0 4a 6f 75 72 6e 61 6c 20 62 69 74 2d 76 65 63 74 Journal bit-vect
254c0 6f 72 73 20 69 73 20 62 65 6e 69 67 6e 2e 0a 20 ors is benign..
254d0 20 20 20 20 20 20 20 2a 2a 20 49 74 20 6d 65 72 ** It mer
254e0 65 6c 79 20 6d 65 61 6e 73 20 74 68 61 74 20 77 ely means that w
254f0 65 20 6d 69 67 68 74 20 64 6f 20 73 6f 6d 65 20 e might do some
25500 65 78 74 72 61 20 77 6f 72 6b 20 74 6f 20 6a 6f extra work to jo
25510 75 72 6e 61 6c 20 61 20 0a 20 20 20 20 20 20 20 urnal a .
25520 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 64 6f ** page that do
25530 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 es not need to b
25540 65 20 6a 6f 75 72 6e 61 6c 65 64 2e 20 20 4e 65 e journaled. Ne
25550 76 65 72 74 68 65 6c 65 73 73 2c 20 62 65 20 73 vertheless, be s
25560 75 72 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 ure . **
25570 74 6f 20 74 65 73 74 20 74 68 65 20 63 61 73 65 to test the case
25580 20 77 68 65 72 65 20 61 20 6d 61 6c 6c 6f 63 20 where a malloc
25590 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 error occurs whi
255a0 6c 65 20 74 72 79 69 6e 67 20 74 6f 20 73 65 74 le trying to set
255b0 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 62 . ** a b
255c0 69 74 20 69 6e 20 61 20 62 69 74 20 76 65 63 74 it in a bit vect
255d0 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 or.. */.
255e0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 sqlite3Be
255f0 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 ginBenignMalloc(
25600 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
25610 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f gno<=pPager->dbO
25620 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 rigSize ){.
25630 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 TESTONLY( r
25640 63 20 3d 20 29 20 73 71 6c 69 74 65 33 42 69 74 c = ) sqlite3Bit
25650 76 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70 vecSet(pPager->p
25660 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 6e 6f 29 InJournal, pgno)
25670 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65 73 74 ;. test
25680 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 case( rc==SQLITE
25690 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 _NOMEM );.
256a0 20 20 7d 0a 20 20 20 20 20 20 20 20 54 45 53 54 }. TEST
256b0 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 61 64 64 ONLY( rc = ) add
256c0 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 ToSavepointBitve
256d0 63 73 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 cs(pPager, pgno)
256e0 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 ;. testca
256f0 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e se( rc==SQLITE_N
25700 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 OMEM );.
25710 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
25720 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 Malloc();.
25730 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d }else{. m
25740 65 6d 73 65 74 28 70 50 67 2d 3e 70 44 61 74 61 emset(pPg->pData
25750 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 , 0, pPager->pag
25760 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a eSize);. }.
25770 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 IOTRACE(("
25780 5a 45 52 4f 20 25 70 20 25 64 5c 6e 22 2c 20 70 ZERO %p %d\n", p
25790 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 Pager, pgno));.
257a0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
257b0 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 50 61 assert( pPg->pPa
257c0 67 65 72 3d 3d 70 50 61 67 65 72 20 29 3b 0a 20 ger==pPager );.
257d0 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 44 62 rc = readDb
257e0 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 20 Page(pPg);.
257f0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
25800 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
25810 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 to pager_acquire
25820 5f 65 72 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 _err;. }.
25830 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }.#ifdef SQLIT
25840 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 E_CHECK_PAGES.
25850 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 pPg->pageHash
25860 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 = pager_pagehash
25870 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 (pPg);.#endif.
25880 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }.. return SQLI
25890 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 72 5f 61 63 TE_OK;..pager_ac
258a0 71 75 69 72 65 5f 65 72 72 3a 0a 20 20 61 73 73 quire_err:. ass
258b0 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ert( rc!=SQLITE_
258c0 4f 4b 20 29 3b 0a 20 20 69 66 28 20 70 50 67 20 OK );. if( pPg
258d0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 ){. sqlite3Pc
258e0 61 63 68 65 44 72 6f 70 28 70 50 67 29 3b 0a 20 acheDrop(pPg);.
258f0 20 7d 0a 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b }. pagerUnlock
25900 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 72 29 IfUnused(pPager)
25910 3b 0a 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 30 ;.. *ppPage = 0
25920 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
25930 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 ../*.** Acquire
25940 61 20 70 61 67 65 20 69 66 20 69 74 20 69 73 20 a page if it is
25950 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 69 already in the i
25960 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20 n-memory cache.
25970 20 44 6f 0a 2a 2a 20 6e 6f 74 20 72 65 61 64 20 Do.** not read
25980 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 64 69 the page from di
25990 73 6b 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f sk. Return a po
259a0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 inter to the pag
259b0 65 2c 0a 2a 2a 20 6f 72 20 30 20 69 66 20 74 68 e,.** or 0 if th
259c0 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e e page is not in
259d0 20 63 61 63 68 65 2e 20 41 6c 73 6f 2c 20 72 65 cache. Also, re
259e0 74 75 72 6e 20 30 20 69 66 20 74 68 65 20 0a 2a turn 0 if the .*
259f0 2a 20 70 61 67 65 72 20 69 73 20 69 6e 20 50 41 * pager is in PA
25a00 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 65 GER_UNLOCK state
25a10 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
25a20 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a ion is called,.*
25a30 2a 20 6f 72 20 69 66 20 74 68 65 20 70 61 67 65 * or if the page
25a40 72 20 69 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 r is in an error
25a50 20 73 74 61 74 65 20 6f 74 68 65 72 20 74 68 61 state other tha
25a60 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 0a 2a n SQLITE_FULL..*
25a70 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 *.** See also sq
25a80 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 2e lite3PagerGet().
25a90 20 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 The difference
25aa0 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 72 6f between this ro
25ab0 75 74 69 6e 65 0a 2a 2a 20 61 6e 64 20 73 71 6c utine.** and sql
25ac0 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 69 ite3PagerGet() i
25ad0 73 20 74 68 61 74 20 5f 67 65 74 28 29 20 77 69 s that _get() wi
25ae0 6c 6c 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73 ll go to the dis
25af0 6b 20 61 6e 64 20 72 65 61 64 0a 2a 2a 20 69 6e k and read.** in
25b00 20 74 68 65 20 70 61 67 65 20 69 66 20 74 68 65 the page if the
25b10 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 page is not alr
25b20 65 61 64 79 20 69 6e 20 63 61 63 68 65 2e 20 20 eady in cache.
25b30 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 This routine.**
25b40 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 returns NULL if
25b50 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 the page is not
25b60 69 6e 20 63 61 63 68 65 20 6f 72 20 69 66 20 61 in cache or if a
25b70 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20 disk I/O error
25b80 0a 2a 2a 20 68 61 73 20 65 76 65 72 20 68 61 70 .** has ever hap
25b90 70 65 6e 65 64 2e 0a 2a 2f 0a 44 62 50 61 67 65 pened..*/.DbPage
25ba0 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f *sqlite3PagerLo
25bb0 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 okup(Pager *pPag
25bc0 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a er, Pgno pgno){.
25bd0 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 30 PgHdr *pPg = 0
25be0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
25bf0 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 er!=0 );. asser
25c00 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 20 20 t( pgno!=0 );.
25c10 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
25c20 70 50 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 20 pPCache!=0 );.
25c30 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
25c40 73 74 61 74 65 20 3e 20 50 41 47 45 52 5f 55 4e state > PAGER_UN
25c50 4c 4f 43 4b 20 29 3b 0a 20 20 73 71 6c 69 74 65 LOCK );. sqlite
25c60 33 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 3PcacheFetch(pPa
25c70 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 ger->pPCache, pg
25c80 6e 6f 2c 20 30 2c 20 26 70 50 67 29 3b 0a 20 20 no, 0, &pPg);.
25c90 72 65 74 75 72 6e 20 70 50 67 3b 0a 7d 0a 0a 2f return pPg;.}../
25ca0 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 70 *.** Release a p
25cb0 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a age reference..*
25cc0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d 62 *.** If the numb
25cd0 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 er of references
25ce0 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 72 6f to the page dro
25cf0 70 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e 20 p to zero, then
25d00 74 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 the.** page is a
25d10 64 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55 20 dded to the LRU
25d20 6c 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c 20 list. When all
25d30 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6c references to al
25d40 6c 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20 72 l pages.** are r
25d50 65 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c 62 eleased, a rollb
25d60 61 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 74 ack occurs and t
25d70 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 he lock on the d
25d80 61 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72 65 atabase is.** re
25d90 6d 6f 76 65 64 2e 0a 2a 2f 0a 76 6f 69 64 20 73 moved..*/.void s
25da0 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
25db0 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 (DbPage *pPg){.
25dc0 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 if( pPg ){.
25dd0 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 Pager *pPager =
25de0 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 pPg->pPager;.
25df0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
25e00 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 20 20 20 lease(pPg);.
25e10 70 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 pagerUnlockIfUnu
25e20 73 65 64 28 70 50 61 67 65 72 29 3b 0a 20 20 7d sed(pPager);. }
25e30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
25e40 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 main journal fi
25e50 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 le has already b
25e60 65 65 6e 20 6f 70 65 6e 65 64 2c 20 65 6e 73 75 een opened, ensu
25e70 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 re that the.** s
25e80 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ub-journal file
25e90 69 73 20 6f 70 65 6e 20 74 6f 6f 2e 20 49 66 20 is open too. If
25ea0 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c the main journal
25eb0 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 0a 2a 2a is not open,.**
25ec0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
25ed0 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a s a no-op..**.**
25ee0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
25ef0 74 75 72 6e 65 64 20 69 66 20 65 76 65 72 79 74 turned if everyt
25f00 68 69 6e 67 20 67 6f 65 73 20 61 63 63 6f 72 64 hing goes accord
25f10 69 6e 67 20 74 6f 20 70 6c 61 6e 2e 20 0a 2a 2a ing to plan. .**
25f20 20 41 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 An SQLITE_IOERR
25f30 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64 65 20 _XXX error code
25f40 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 is returned if a
25f50 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 73 71 6c call to .** sql
25f60 69 74 65 33 4f 73 4f 70 65 6e 28 29 20 66 61 69 ite3OsOpen() fai
25f70 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ls..*/.static in
25f80 74 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c t openSubJournal
25f90 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
25fa0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
25fb0 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 73 4f TE_OK;. if( isO
25fc0 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
25fd0 20 26 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 && !isOpen(pPag
25fe0 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 er->sjfd) ){.
25ff0 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 if( pPager->jou
26000 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f rnalMode==PAGER_
26010 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f JOURNALMODE_MEMO
26020 52 59 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 75 RY || pPager->su
26030 62 6a 49 6e 4d 65 6d 6f 72 79 20 29 7b 0a 20 20 bjInMemory ){.
26040 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f sqlite3MemJo
26050 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 urnalOpen(pPager
26060 2d 3e 73 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c ->sjfd);. }el
26070 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 se{. rc = p
26080 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 agerOpentemp(pPa
26090 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 6a 66 ger, pPager->sjf
260a0 64 2c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 d, SQLITE_OPEN_S
260b0 55 42 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 UBJOURNAL);.
260c0 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
260d0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
260e0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
260f0 6c 65 64 20 61 74 20 74 68 65 20 73 74 61 72 74 led at the start
26100 20 6f 66 20 65 76 65 72 79 20 77 72 69 74 65 20 of every write
26110 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 transaction..**
26120 54 68 65 72 65 20 6d 75 73 74 20 61 6c 72 65 61 There must alrea
26130 64 79 20 62 65 20 61 20 52 45 53 45 52 56 45 44 dy be a RESERVED
26140 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f or EXCLUSIVE lo
26150 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ck on the databa
26160 73 65 20 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e se .** file when
26170 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
26180 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f called..**.** O
26190 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 pen the journal
261a0 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70 file for pager p
261b0 50 61 67 65 72 20 61 6e 64 20 77 72 69 74 65 20 Pager and write
261c0 61 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 a journal header
261d0 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 72 74 .** to the start
261e0 20 6f 66 20 69 74 2e 20 49 66 20 74 68 65 72 65 of it. If there
261f0 20 61 72 65 20 61 63 74 69 76 65 20 73 61 76 65 are active save
26200 70 6f 69 6e 74 73 2c 20 6f 70 65 6e 20 74 68 65 points, open the
26210 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 sub-journal.**
26220 61 73 20 77 65 6c 6c 2e 20 54 68 69 73 20 66 75 as well. This fu
26230 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 nction is only u
26240 73 65 64 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 sed when the jou
26250 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 62 65 69 rnal file is bei
26260 6e 67 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 74 6f ng .** opened to
26270 20 77 72 69 74 65 20 61 20 72 6f 6c 6c 62 61 63 write a rollbac
26280 6b 20 6c 6f 67 20 66 6f 72 20 61 20 74 72 61 6e k log for a tran
26290 73 61 63 74 69 6f 6e 2e 20 49 74 20 69 73 20 6e saction. It is n
262a0 6f 74 20 75 73 65 64 20 0a 2a 2a 20 77 68 65 6e ot used .** when
262b0 20 6f 70 65 6e 69 6e 67 20 61 20 68 6f 74 20 6a opening a hot j
262c0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 72 ournal file to r
262d0 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a oll it back..**.
262e0 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 ** If the journa
262f0 6c 20 66 69 6c 65 20 69 73 20 61 6c 72 65 61 64 l file is alread
26300 79 20 6f 70 65 6e 20 28 61 73 20 69 74 20 6d 61 y open (as it ma
26310 79 20 62 65 20 69 6e 20 65 78 63 6c 75 73 69 76 y be in exclusiv
26320 65 20 6d 6f 64 65 29 2c 0a 2a 2a 20 74 68 65 6e e mode),.** then
26330 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6a this function j
26340 75 73 74 20 77 72 69 74 65 73 20 61 20 6a 6f 75 ust writes a jou
26350 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20 74 rnal header to t
26360 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 0a he start of the.
26370 2a 2a 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 ** already open
26380 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 file. .**.** Whe
26390 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 ther or not the
263a0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
263b0 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73 20 66 opened by this f
263c0 75 6e 63 74 69 6f 6e 2c 20 74 68 65 0a 2a 2a 20 unction, the.**
263d0 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c Pager.pInJournal
263e0 20 62 69 74 76 65 63 20 73 74 72 75 63 74 75 72 bitvec structur
263f0 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2e 0a e is allocated..
26400 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c **.** Return SQL
26410 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 ITE_OK if everyt
26420 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 hing is successf
26430 75 6c 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 ul. Otherwise, r
26440 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 eturn .** SQLITE
26450 5f 4e 4f 4d 45 4d 20 69 66 20 74 68 65 20 61 74 _NOMEM if the at
26460 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 tempt to allocat
26470 65 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e e Pager.pInJourn
26480 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a al fails, or .**
26490 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 an IO error cod
264a0 65 20 69 66 20 6f 70 65 6e 69 6e 67 20 6f 72 20 e if opening or
264b0 77 72 69 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 writing the jour
264c0 6e 61 6c 20 66 69 6c 65 20 66 61 69 6c 73 2e 0a nal file fails..
264d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
264e0 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c ger_open_journal
264f0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
26500 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
26510 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 TE_OK;
26520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
26530 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
26540 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 sqlite3_vfs *
26550 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61 const pVfs = pPa
26560 67 65 72 2d 3e 70 56 66 73 3b 20 20 20 2f 2a 20 ger->pVfs; /*
26570 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 76 Local cache of v
26580 66 73 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 0a 20 fs pointer */..
26590 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
265a0 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 >state>=PAGER_RE
265b0 53 45 52 56 45 44 20 29 3b 0a 20 20 61 73 73 65 SERVED );. asse
265c0 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a rt( pPager->useJ
265d0 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 ournal );. asse
265e0 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 rt( pPager->jour
265f0 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a nalMode!=PAGER_J
26600 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 OURNALMODE_OFF )
26610 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
26620 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d er->pInJournal==
26630 30 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 20 0 );. . /* If
26640 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 65 already in the e
26650 72 72 6f 72 20 73 74 61 74 65 2c 20 74 68 69 73 rror state, this
26660 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
26670 6f 2d 6f 70 2e 20 20 42 75 74 20 6f 6e 0a 20 20 o-op. But on.
26680 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e ** the other han
26690 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 d, this routine
266a0 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 is never called
266b0 69 66 20 77 65 20 61 72 65 20 61 6c 72 65 61 64 if we are alread
266c0 79 20 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 72 72 y in. ** an err
266d0 6f 72 20 73 74 61 74 65 2e 20 2a 2f 0a 20 20 69 or state. */. i
266e0 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d f( NEVER(pPager-
266f0 3e 65 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 >errCode) ) retu
26700 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f rn pPager->errCo
26710 64 65 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 de;.. /* TODO:
26720 49 73 20 69 74 20 72 65 61 6c 6c 79 20 70 6f 73 Is it really pos
26730 73 69 62 6c 65 20 74 6f 20 67 65 74 20 68 65 72 sible to get her
26740 65 20 77 69 74 68 20 64 62 53 69 7a 65 56 61 6c e with dbSizeVal
26750 69 64 3d 3d 30 3f 20 49 66 20 6e 6f 74 2c 0a 20 id==0? If not,.
26760 20 2a 2a 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 ** the call to
26770 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29 PagerPagecount()
26780 20 63 61 6e 20 62 65 20 72 65 6d 6f 76 65 64 2e can be removed.
26790 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 . */. testcase
267a0 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 ( pPager->dbSize
267b0 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 73 71 Valid==0 );. sq
267c0 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f lite3PagerPageco
267d0 75 6e 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a unt(pPager, 0);.
267e0 0a 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f . pPager->pInJo
267f0 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 42 urnal = sqlite3B
26800 69 74 76 65 63 43 72 65 61 74 65 28 70 50 61 67 itvecCreate(pPag
26810 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 69 er->dbSize);. i
26820 66 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f f( pPager->pInJo
26830 75 72 6e 61 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 urnal==0 ){.
26840 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
26850 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f MEM;. }.. /* O
26860 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 pen the journal
26870 66 69 6c 65 20 69 66 20 69 74 20 69 73 20 6e 6f file if it is no
26880 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 2e 20 t already open.
26890 2a 2f 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e */. if( !isOpen
268a0 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b (pPager->jfd) ){
268b0 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d . if( pPager-
268c0 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 >journalMode==PA
268d0 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
268e0 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 20 MEMORY ){.
268f0 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 sqlite3MemJourna
26900 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 lOpen(pPager->jf
26910 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 d);. }else{.
26920 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 66 const int f
26930 6c 61 67 73 20 3d 20 20 20 20 20 20 20 20 20 20 lags =
26940 20 20 20 20 20 20 20 20 20 2f 2a 20 56 46 53 20 /* VFS
26950 66 6c 61 67 73 20 74 6f 20 6f 70 65 6e 20 6a 6f flags to open jo
26960 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 urnal file */.
26970 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 SQLITE_OPE
26980 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 N_READWRITE|SQLI
26990 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 0a TE_OPEN_CREATE|.
269a0 20 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d (pPager-
269b0 3e 74 65 6d 70 46 69 6c 65 20 3f 20 0a 20 20 20 >tempFile ? .
269c0 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f (SQLITE_O
269d0 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 PEN_DELETEONCLOS
269e0 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 E|SQLITE_OPEN_TE
269f0 4d 50 5f 4a 4f 55 52 4e 41 4c 29 3a 0a 20 20 20 MP_JOURNAL):.
26a00 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f (SQLITE_O
26a10 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c PEN_MAIN_JOURNAL
26a20 29 0a 20 20 20 20 20 20 20 20 29 3b 0a 23 69 66 ). );.#if
26a30 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
26a40 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 E_ATOMIC_WRITE.
26a50 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
26a60 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 3JournalOpen(.
26a70 20 20 20 20 20 20 20 20 70 56 66 73 2c 20 70 50 pVfs, pP
26a80 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 ager->zJournal,
26a90 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 pPager->jfd, fla
26aa0 67 73 2c 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 gs, jrnlBufferSi
26ab0 7a 65 28 70 50 61 67 65 72 29 0a 20 20 20 20 20 ze(pPager).
26ac0 20 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 );.#else.
26ad0 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 rc = sqlite3OsOp
26ae0 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d en(pVfs, pPager-
26af0 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 >zJournal, pPage
26b00 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 30 r->jfd, flags, 0
26b10 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a );.#endif. }.
26b20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d assert( rc!=
26b30 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f SQLITE_OK || isO
26b40 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
26b50 20 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 57 );. }... /* W
26b60 72 69 74 65 20 74 68 65 20 66 69 72 73 74 20 6a rite the first j
26b70 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f ournal header to
26b80 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
26b90 65 20 61 6e 64 20 6f 70 65 6e 20 0a 20 20 2a 2a e and open . **
26ba0 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
26bb0 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 if necessary..
26bc0 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 */. if( rc==SQ
26bd0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f LITE_OK ){. /
26be0 2a 20 54 4f 44 4f 3a 20 43 68 65 63 6b 20 69 66 * TODO: Check if
26bf0 20 61 6c 6c 20 6f 66 20 74 68 65 73 65 20 61 72 all of these ar
26c00 65 20 72 65 61 6c 6c 79 20 72 65 71 75 69 72 65 e really require
26c10 64 2e 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 d. */. pPager
26c20 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d 20 70 ->dbOrigSize = p
26c30 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 Pager->dbSize;.
26c40 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
26c50 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 alStarted = 0;.
26c60 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 pPager->needS
26c70 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 ync = 0;. pPa
26c80 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 ger->nRec = 0;.
26c90 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
26ca0 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 70 alOff = 0;. p
26cb0 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 Pager->setMaster
26cc0 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 = 0;. pPager
26cd0 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 ->journalHdr = 0
26ce0 3b 0a 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 ;. rc = write
26cf0 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 JournalHdr(pPage
26d00 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 r);. }. if( rc
26d10 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 ==SQLITE_OK && p
26d20 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e Pager->nSavepoin
26d30 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70 t ){. rc = op
26d40 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50 61 enSubJournal(pPa
26d50 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 ger);. }.. if(
26d60 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
26d70 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 {. sqlite3Bit
26d80 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 vecDestroy(pPage
26d90 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a r->pInJournal);.
26da0 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a pPager->pInJ
26db0 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 7d 0a ournal = 0;. }.
26dc0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
26dd0 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 77 72 /*.** Begin a wr
26de0 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite-transaction
26df0 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 on the specified
26e00 20 70 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 49 pager object. I
26e10 66 20 61 20 0a 2a 2a 20 77 72 69 74 65 2d 74 72 f a .** write-tr
26e20 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c ansaction has al
26e30 72 65 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65 ready been opene
26e40 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e d, this function
26e50 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a is a no-op..**.
26e60 2a 2a 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 ** If the exFlag
26e70 20 61 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c argument is fal
26e80 73 65 2c 20 74 68 65 6e 20 61 63 71 75 69 72 65 se, then acquire
26e90 20 61 74 20 6c 65 61 73 74 20 61 20 52 45 53 45 at least a RESE
26ea0 52 56 45 44 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 RVED.** lock on
26eb0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
26ec0 65 2e 20 49 66 20 65 78 46 6c 61 67 20 69 73 20 e. If exFlag is
26ed0 74 72 75 65 2c 20 74 68 65 6e 20 61 63 71 75 69 true, then acqui
26ee0 72 65 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 61 re at least.** a
26ef0 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b n EXCLUSIVE lock
26f00 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b . If such a lock
26f10 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 is already held
26f20 2c 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a , no locking .**
26f30 20 66 75 6e 63 74 69 6f 6e 73 20 6e 65 65 64 20 functions need
26f40 62 65 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a be called..**.**
26f50 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 If this is not
26f60 61 20 74 65 6d 70 6f 72 61 72 79 20 6f 72 20 69 a temporary or i
26f70 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 61 6e n-memory file an
26f80 64 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 d, the journal f
26f90 69 6c 65 20 69 73 20 0a 2a 2a 20 6f 70 65 6e 65 ile is .** opene
26fa0 64 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 d if it has not
26fb0 62 65 65 6e 20 61 6c 72 65 61 64 79 2e 20 46 6f been already. Fo
26fc0 72 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 r a temporary fi
26fd0 6c 65 2c 20 74 68 65 20 6f 70 65 6e 69 6e 67 20 le, the opening
26fe0 0a 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e .** of the journ
26ff0 61 6c 20 66 69 6c 65 20 69 73 20 64 65 66 65 72 al file is defer
27000 72 65 64 20 75 6e 74 69 6c 20 74 68 65 72 65 20 red until there
27010 69 73 20 61 6e 20 61 63 74 75 61 6c 20 6e 65 65 is an actual nee
27020 64 20 74 6f 20 0a 2a 2a 20 77 72 69 74 65 20 74 d to .** write t
27030 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 54 o the journal. T
27040 4f 44 4f 3a 20 57 68 79 20 68 61 6e 64 6c 65 20 ODO: Why handle
27050 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 temporary files
27060 64 69 66 66 65 72 65 6e 74 6c 79 3f 0a 2a 2a 0a differently?.**.
27070 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 ** If the journa
27080 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 l file is opened
27090 20 28 6f 72 20 69 66 20 69 74 20 69 73 20 61 6c (or if it is al
270a0 72 65 61 64 79 20 6f 70 65 6e 29 2c 20 74 68 65 ready open), the
270b0 6e 20 61 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 68 n a.** journal-h
270c0 65 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e eader is written
270d0 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 to the start of
270e0 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 it..**.** If th
270f0 65 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 e subjInMemory a
27100 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 2d 7a rgument is non-z
27110 65 72 6f 2c 20 74 68 65 6e 20 61 6e 79 20 73 75 ero, then any su
27120 62 2d 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e 65 64 b-journal opened
27130 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 69 73 20 .** within this
27140 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c transaction will
27150 20 62 65 20 6f 70 65 6e 65 64 20 61 73 20 61 6e be opened as an
27160 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 2e in-memory file.
27170 20 54 68 69 73 0a 2a 2a 20 68 61 73 20 6e 6f 20 This.** has no
27180 65 66 66 65 63 74 20 69 66 20 74 68 65 20 73 75 effect if the su
27190 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c 72 b-journal is alr
271a0 65 61 64 79 20 6f 70 65 6e 65 64 20 28 61 73 20 eady opened (as
271b0 69 74 20 6d 61 79 20 62 65 20 77 68 65 6e 0a 2a it may be when.*
271c0 2a 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 * running in exc
271d0 6c 75 73 69 76 65 20 6d 6f 64 65 29 20 6f 72 20 lusive mode) or
271e0 69 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 if the transacti
271f0 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 72 65 71 75 on does not requ
27200 69 72 65 20 61 0a 2a 2a 20 73 75 62 2d 6a 6f 75 ire a.** sub-jou
27210 72 6e 61 6c 2e 20 49 66 20 74 68 65 20 73 75 62 rnal. If the sub
27220 6a 49 6e 4d 65 6d 6f 72 79 20 61 72 67 75 6d 65 jInMemory argume
27230 6e 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e nt is zero, then
27240 20 61 6e 79 20 72 65 71 75 69 72 65 64 0a 2a 2a any required.**
27250 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 sub-journal is
27260 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 2d 6d implemented in-m
27270 65 6d 6f 72 79 20 69 66 20 70 50 61 67 65 72 20 emory if pPager
27280 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 is an in-memory
27290 64 61 74 61 62 61 73 65 2c 20 0a 2a 2a 20 6f 72 database, .** or
272a0 20 75 73 69 6e 67 20 61 20 74 65 6d 70 6f 72 61 using a tempora
272b0 72 79 20 66 69 6c 65 20 6f 74 68 65 72 77 69 73 ry file otherwis
272c0 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 e..*/.int sqlite
272d0 33 50 61 67 65 72 42 65 67 69 6e 28 50 61 67 65 3PagerBegin(Page
272e0 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 r *pPager, int e
272f0 78 46 6c 61 67 2c 20 69 6e 74 20 73 75 62 6a 49 xFlag, int subjI
27300 6e 4d 65 6d 6f 72 79 29 7b 0a 20 20 69 6e 74 20 nMemory){. int
27310 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
27320 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
27330 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55 ->state!=PAGER_U
27340 4e 4c 4f 43 4b 20 29 3b 0a 20 20 70 50 61 67 65 NLOCK );. pPage
27350 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 r->subjInMemory
27360 3d 20 28 75 38 29 73 75 62 6a 49 6e 4d 65 6d 6f = (u8)subjInMemo
27370 72 79 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 ry;. if( pPager
27380 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 ->state==PAGER_S
27390 48 41 52 45 44 20 29 7b 0a 20 20 20 20 61 73 73 HARED ){. ass
273a0 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e ert( pPager->pIn
273b0 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 Journal==0 );.
273c0 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 assert( !MEMDB
273d0 20 26 26 20 21 70 50 61 67 65 72 2d 3e 74 65 6d && !pPager->tem
273e0 70 46 69 6c 65 20 29 3b 0a 0a 20 20 20 20 2f 2a pFile );.. /*
273f0 20 4f 62 74 61 69 6e 20 61 20 52 45 53 45 52 56 Obtain a RESERV
27400 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 ED lock on the d
27410 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 atabase file. If
27420 20 74 68 65 20 65 78 46 6c 61 67 20 70 61 72 61 the exFlag para
27430 6d 65 74 65 72 0a 20 20 20 20 2a 2a 20 69 73 20 meter. ** is
27440 74 72 75 65 2c 20 74 68 65 6e 20 69 6d 6d 65 64 true, then immed
27450 69 61 74 65 6c 79 20 75 70 67 72 61 64 65 20 74 iately upgrade t
27460 68 69 73 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 his to an EXCLUS
27470 49 56 45 20 6c 6f 63 6b 2e 20 54 68 65 0a 20 20 IVE lock. The.
27480 20 20 2a 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 ** busy-handle
27490 72 20 63 61 6c 6c 62 61 63 6b 20 63 61 6e 20 62 r callback can b
274a0 65 20 75 73 65 64 20 77 68 65 6e 20 75 70 67 72 e used when upgr
274b0 61 64 69 6e 67 20 74 6f 20 74 68 65 20 45 58 43 ading to the EXC
274c0 4c 55 53 49 56 45 0a 20 20 20 20 2a 2a 20 6c 6f LUSIVE. ** lo
274d0 63 6b 2c 20 62 75 74 20 6e 6f 74 20 77 68 65 6e ck, but not when
274e0 20 6f 62 74 61 69 6e 69 6e 67 20 74 68 65 20 52 obtaining the R
274f0 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 0a 20 20 ESERVED lock..
27500 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 */. rc = sq
27510 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 lite3OsLock(pPag
27520 65 72 2d 3e 66 64 2c 20 52 45 53 45 52 56 45 44 er->fd, RESERVED
27530 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 _LOCK);. if(
27540 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
27550 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 . pPager->s
27560 74 61 74 65 20 3d 20 50 41 47 45 52 5f 52 45 53 tate = PAGER_RES
27570 45 52 56 45 44 3b 0a 20 20 20 20 20 20 69 66 28 ERVED;. if(
27580 20 65 78 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 exFlag ){.
27590 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 rc = pager_wa
275a0 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 it_on_lock(pPage
275b0 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 r, EXCLUSIVE_LOC
275c0 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 K);. }.
275d0 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
275e0 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 73 20 required locks
275f0 77 65 72 65 20 73 75 63 63 65 73 73 66 75 6c 6c were successfull
27600 79 20 6f 62 74 61 69 6e 65 64 2c 20 6f 70 65 6e y obtained, open
27610 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 the journal.
27620 20 2a 2a 20 66 69 6c 65 20 61 6e 64 20 77 72 69 ** file and wri
27630 74 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 te the first jou
27640 72 6e 61 6c 2d 68 65 61 64 65 72 20 74 6f 20 69 rnal-header to i
27650 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 t.. */. if
27660 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
27670 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e && pPager->journ
27680 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f alMode!=PAGER_JO
27690 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b URNALMODE_OFF ){
276a0 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
276b0 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 r_open_journal(p
276c0 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 Pager);. }.
276d0 7d 65 6c 73 65 20 69 66 28 20 69 73 4f 70 65 6e }else if( isOpen
276e0 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 (pPager->jfd) &&
276f0 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
27700 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a Off==0 ){. /*
27710 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 This happens wh
27720 65 6e 20 74 68 65 20 70 61 67 65 72 20 77 61 73 en the pager was
27730 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 61 63 in exclusive-ac
27740 63 65 73 73 20 6d 6f 64 65 20 74 68 65 20 6c 61 cess mode the la
27750 73 74 0a 20 20 20 20 2a 2a 20 74 69 6d 65 20 61 st. ** time a
27760 20 28 72 65 61 64 20 6f 72 20 77 72 69 74 65 29 (read or write)
27770 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 transaction was
27780 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f successfully co
27790 6e 63 6c 75 64 65 64 0a 20 20 20 20 2a 2a 20 62 ncluded. ** b
277a0 79 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f y this connectio
277b0 6e 2e 20 49 6e 73 74 65 61 64 20 6f 66 20 64 65 n. Instead of de
277c0 6c 65 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e leting the journ
277d0 61 6c 20 66 69 6c 65 20 69 74 20 77 61 73 20 0a al file it was .
277e0 20 20 20 20 2a 2a 20 6b 65 70 74 20 6f 70 65 6e ** kept open
277f0 20 61 6e 64 20 65 69 74 68 65 72 20 77 61 73 20 and either was
27800 74 72 75 6e 63 61 74 65 64 20 74 6f 20 30 20 62 truncated to 0 b
27810 79 74 65 73 20 6f 72 20 69 74 73 20 68 65 61 64 ytes or its head
27820 65 72 20 77 61 73 0a 20 20 20 20 2a 2a 20 6f 76 er was. ** ov
27830 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 7a erwritten with z
27840 65 72 6f 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 eros.. */.
27850 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
27860 3e 6e 52 65 63 3d 3d 30 20 29 3b 0a 20 20 20 20 >nRec==0 );.
27870 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
27880 64 62 4f 72 69 67 53 69 7a 65 3d 3d 30 20 29 3b dbOrigSize==0 );
27890 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
278a0 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d ger->pInJournal=
278b0 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 =0 );. rc = p
278c0 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 ager_open_journa
278d0 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a l(pPager);. }..
278e0 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 54 PAGERTRACE(("T
278f0 52 41 4e 53 41 43 54 49 4f 4e 20 25 64 5c 6e 22 RANSACTION %d\n"
27900 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 , PAGERID(pPager
27910 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 )));. assert( !
27920 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
27930 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a fd) || pPager->j
27940 6f 75 72 6e 61 6c 4f 66 66 3e 30 20 7c 7c 20 72 ournalOff>0 || r
27950 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a c!=SQLITE_OK );.
27960 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
27970 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 _OK ){. asser
27980 74 28 20 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f t( !pPager->dbMo
27990 64 69 66 69 65 64 20 29 3b 0a 20 20 20 20 2f 2a dified );. /*
279a0 20 49 67 6e 6f 72 65 20 61 6e 79 20 49 4f 20 65 Ignore any IO e
279b0 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73 rror that occurs
279c0 20 77 69 74 68 69 6e 20 70 61 67 65 72 5f 65 6e within pager_en
279d0 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 2e d_transaction().
279e0 20 54 68 65 0a 20 20 20 20 2a 2a 20 70 75 72 70 The. ** purp
279f0 6f 73 65 20 6f 66 20 74 68 69 73 20 63 61 6c 6c ose of this call
27a00 20 69 73 20 74 6f 20 72 65 73 65 74 20 74 68 65 is to reset the
27a10 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 20 internal state
27a20 6f 66 20 74 68 65 20 70 61 67 65 72 0a 20 20 20 of the pager.
27a30 20 2a 2a 20 73 75 62 2d 73 79 73 74 65 6d 2e 20 ** sub-system.
27a40 49 74 20 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 It doesn't matte
27a50 72 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c r if the journal
27a60 2d 66 69 6c 65 20 69 73 20 6e 6f 74 20 70 72 6f -file is not pro
27a70 70 65 72 6c 79 0a 20 20 20 20 2a 2a 20 66 69 6e perly. ** fin
27a80 61 6c 69 7a 65 64 20 61 74 20 74 68 69 73 20 70 alized at this p
27a90 6f 69 6e 74 20 28 73 69 6e 63 65 20 69 74 20 69 oint (since it i
27aa0 73 20 6e 6f 74 20 61 20 76 61 6c 69 64 20 6a 6f s not a valid jo
27ab0 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 79 77 61 urnal file anywa
27ac0 79 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 y).. */. p
27ad0 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 ager_end_transac
27ae0 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 30 29 3b tion(pPager, 0);
27af0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
27b00 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 ;.}../*.** Mark
27b10 61 20 73 69 6e 67 6c 65 20 64 61 74 61 20 70 61 a single data pa
27b20 67 65 20 61 73 20 77 72 69 74 65 61 62 6c 65 2e ge as writeable.
27b30 20 54 68 65 20 70 61 67 65 20 69 73 20 77 72 69 The page is wri
27b40 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a tten into the .*
27b50 2a 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f * main journal o
27b60 72 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 r sub-journal as
27b70 20 72 65 71 75 69 72 65 64 2e 20 49 66 20 74 68 required. If th
27b80 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65 e page is writte
27b90 6e 20 69 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 n into.** one of
27ba0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 the journals, t
27bb0 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
27bc0 20 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 bit is set in t
27bd0 68 65 20 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e he .** Pager.pIn
27be0 4a 6f 75 72 6e 61 6c 20 62 69 74 76 65 63 20 61 Journal bitvec a
27bf0 6e 64 20 74 68 65 20 50 61 67 65 72 53 61 76 65 nd the PagerSave
27c00 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 point.pInSavepoi
27c10 6e 74 20 62 69 74 76 65 63 73 0a 2a 2a 20 6f 66 nt bitvecs.** of
27c20 20 61 6e 79 20 6f 70 65 6e 20 73 61 76 65 70 6f any open savepo
27c30 69 6e 74 73 20 61 73 20 61 70 70 72 6f 70 72 69 ints as appropri
27c40 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ate..*/.static i
27c50 6e 74 20 70 61 67 65 72 5f 77 72 69 74 65 28 50 nt pager_write(P
27c60 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f gHdr *pPg){. vo
27c70 69 64 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d id *pData = pPg-
27c80 3e 70 44 61 74 61 3b 0a 20 20 50 61 67 65 72 20 >pData;. Pager
27c90 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
27ca0 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 Pager;. int rc
27cb0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 = SQLITE_OK;..
27cc0 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 /* This routine
27cd0 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20 75 6e is not called un
27ce0 6c 65 73 73 20 61 20 74 72 61 6e 73 61 63 74 69 less a transacti
27cf0 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 on has already b
27d00 65 65 6e 0a 20 20 2a 2a 20 73 74 61 72 74 65 64 een. ** started
27d10 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
27d20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d pPager->state>=
27d30 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 PAGER_RESERVED )
27d40 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 ;.. /* If an er
27d50 72 6f 72 20 68 61 73 20 62 65 65 6e 20 70 72 65 ror has been pre
27d60 76 69 6f 75 73 6c 79 20 64 65 74 65 63 74 65 64 viously detected
27d70 2c 20 77 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 , we should not
27d80 62 65 0a 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 20 be. ** calling
27d90 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 52 this routine. R
27da0 65 70 65 61 74 20 74 68 65 20 65 72 72 6f 72 20 epeat the error
27db0 66 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 2e 0a for robustness..
27dc0 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 */. if( NEVER
27dd0 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 (pPager->errCode
27de0 29 20 29 20 20 72 65 74 75 72 6e 20 70 50 61 67 ) ) return pPag
27df0 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 er->errCode;..
27e00 2f 2a 20 48 69 67 68 65 72 2d 6c 65 76 65 6c 20 /* Higher-level
27e10 72 6f 75 74 69 6e 65 73 20 6e 65 76 65 72 20 63 routines never c
27e20 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f all this functio
27e30 6e 20 69 66 20 64 61 74 61 62 61 73 65 20 69 73 n if database is
27e40 20 6e 6f 74 0a 20 20 2a 2a 20 77 72 69 74 61 62 not. ** writab
27e50 6c 65 2e 20 20 42 75 74 20 63 68 65 63 6b 20 61 le. But check a
27e60 6e 79 77 61 79 2c 20 6a 75 73 74 20 66 6f 72 20 nyway, just for
27e70 72 6f 62 75 73 74 6e 65 73 73 2e 20 2a 2f 0a 20 robustness. */.
27e80 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 if( NEVER(pPage
27e90 72 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29 20 72 r->readOnly) ) r
27ea0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 eturn SQLITE_PER
27eb0 4d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 M;.. assert( !p
27ec0 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 Pager->setMaster
27ed0 20 29 3b 0a 0a 20 20 43 48 45 43 4b 5f 50 41 47 );.. CHECK_PAG
27ee0 45 28 70 50 67 29 3b 0a 0a 20 20 2f 2a 20 4d 61 E(pPg);.. /* Ma
27ef0 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 64 rk the page as d
27f00 69 72 74 79 2e 20 20 49 66 20 74 68 65 20 70 61 irty. If the pa
27f10 67 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 ge has already b
27f20 65 65 6e 20 77 72 69 74 74 65 6e 0a 20 20 2a 2a een written. **
27f30 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 to the journal
27f40 74 68 65 6e 20 77 65 20 63 61 6e 20 72 65 74 75 then we can retu
27f50 72 6e 20 72 69 67 68 74 20 61 77 61 79 2e 0a 20 rn right away..
27f60 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 63 61 */. sqlite3Pca
27f70 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 67 cheMakeDirty(pPg
27f80 29 3b 0a 20 20 69 66 28 20 70 61 67 65 49 6e 4a );. if( pageInJ
27f90 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 21 ournal(pPg) && !
27fa0 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 subjRequiresPage
27fb0 28 70 50 67 29 20 29 7b 0a 20 20 20 20 70 50 61 (pPg) ){. pPa
27fc0 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 ger->dbModified
27fd0 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20 = 1;. }else{..
27fe0 20 20 20 2f 2a 20 49 66 20 77 65 20 67 65 74 20 /* If we get
27ff0 74 68 69 73 20 66 61 72 2c 20 69 74 20 6d 65 61 this far, it mea
28000 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65 ns that the page
28010 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 needs to be.
28020 20 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 74 ** written to t
28030 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a he transaction j
28040 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 63 6b ournal or the ck
28050 65 63 6b 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c eckpoint journal
28060 0a 20 20 20 20 2a 2a 20 6f 72 20 62 6f 74 68 2e . ** or both.
28070 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 48 . **. ** H
28080 69 67 68 65 72 20 6c 65 76 65 6c 20 72 6f 75 74 igher level rout
28090 69 6e 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 ines should have
280a0 20 61 6c 72 65 61 64 79 20 73 74 61 72 74 65 64 already started
280b0 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a a transaction,.
280c0 20 20 20 20 2a 2a 20 77 68 69 63 68 20 6d 65 61 ** which mea
280d0 6e 73 20 74 68 65 79 20 68 61 76 65 20 61 63 71 ns they have acq
280e0 75 69 72 65 64 20 74 68 65 20 6e 65 63 65 73 73 uired the necess
280f0 61 72 79 20 6c 6f 63 6b 73 20 61 6e 64 20 6f 70 ary locks and op
28100 65 6e 65 64 0a 20 20 20 20 2a 2a 20 61 20 72 6f ened. ** a ro
28110 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 llback journal.
28120 20 44 6f 75 62 6c 65 2d 63 68 65 63 6b 20 74 6f Double-check to
28130 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68 69 73 makes sure this
28140 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 20 is the case..
28150 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 */. rc = sq
28160 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 lite3PagerBegin(
28170 70 50 61 67 65 72 2c 20 30 2c 20 70 50 61 67 65 pPager, 0, pPage
28180 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 r->subjInMemory)
28190 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 ;. if( NEVER(
281a0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 rc!=SQLITE_OK) )
281b0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 {. return r
281c0 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 c;. }. if(
281d0 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d !isOpen(pPager-
281e0 3e 6a 66 64 29 20 26 26 20 70 50 61 67 65 72 2d >jfd) && pPager-
281f0 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 >journalMode!=PA
28200 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
28210 4f 46 46 20 29 7b 0a 20 20 20 20 20 20 61 73 73 OFF ){. ass
28220 65 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73 65 ert( pPager->use
28230 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 20 20 20 Journal );.
28240 20 72 63 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e rc = pager_open
28250 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 _journal(pPager)
28260 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
28270 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 SQLITE_OK ) retu
28280 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
28290 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 pPager->dbModif
282a0 69 65 64 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20 ied = 1;. .
282b0 2f 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 /* The transacti
282c0 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 on journal now e
282d0 78 69 73 74 73 20 61 6e 64 20 77 65 20 68 61 76 xists and we hav
282e0 65 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 e a RESERVED or
282f0 61 6e 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 an. ** EXCLUS
28300 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 IVE lock on the
28310 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 main database fi
28320 6c 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 63 le. Write the c
28330 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 urrent page to.
28340 20 20 20 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 ** the transa
28350 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 ction journal if
28360 20 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72 65 it is not there
28370 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f already.. */
28380 0a 20 20 20 20 69 66 28 20 21 70 61 67 65 49 6e . if( !pageIn
28390 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 Journal(pPg) &&
283a0 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
283b0 66 64 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 fd) ){. if(
283c0 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 pPg->pgno<=pPag
283d0 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 er->dbOrigSize )
283e0 7b 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 6b {. u32 ck
283f0 73 75 6d 3b 0a 20 20 20 20 20 20 20 20 63 68 61 sum;. cha
28400 72 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 r *pData2;..
28410 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 /* We should
28420 20 6e 65 76 65 72 20 77 72 69 74 65 20 74 6f 20 never write to
28430 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
28440 20 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 20 the page that.
28450 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 ** contai
28460 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ns the database
28470 6c 6f 63 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c locks. The foll
28480 6f 77 69 6e 67 20 61 73 73 65 72 74 20 76 65 72 owing assert ver
28490 69 66 69 65 73 0a 20 20 20 20 20 20 20 20 2a 2a ifies. **
284a0 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e that we do not.
284b0 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 */. asse
284c0 72 74 28 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 rt( pPg->pgno!=P
284d0 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 AGER_MJ_PGNO(pPa
284e0 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20 20 20 ger) );.
284f0 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 70 CODEC2(pPager, p
28500 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c Data, pPg->pgno,
28510 20 37 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 7, return SQLIT
28520 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 32 29 E_NOMEM, pData2)
28530 3b 0a 20 20 20 20 20 20 20 20 63 6b 73 75 6d 20 ;. cksum
28540 3d 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70 50 = pager_cksum(pP
28550 61 67 65 72 2c 20 28 75 38 2a 29 70 44 61 74 61 ager, (u8*)pData
28560 32 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 2);. rc =
28570 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 write32bits(pPa
28580 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 ger->jfd, pPager
28590 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 ->journalOff, pP
285a0 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 g->pgno);.
285b0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
285c0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
285d0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 rc = sqlite3OsW
285e0 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 rite(pPager->jfd
285f0 2c 20 70 44 61 74 61 32 2c 20 70 50 61 67 65 72 , pData2, pPager
28600 2d 3e 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 ->pageSize,.
28610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28620 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 pPager
28630 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 20 34 ->journalOff + 4
28640 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 );. pPa
28650 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
28660 2b 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 += pPager->pageS
28670 69 7a 65 2b 34 3b 0a 20 20 20 20 20 20 20 20 7d ize+4;. }
28680 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d . if( rc=
28690 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
286a0 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 rc = wri
286b0 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d te32bits(pPager-
286c0 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f >jfd, pPager->jo
286d0 75 72 6e 61 6c 4f 66 66 2c 20 63 6b 73 75 6d 29 urnalOff, cksum)
286e0 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 ;. pPag
286f0 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b er->journalOff +
28700 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = 4;. }.
28710 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 IOTRACE((
28720 22 4a 4f 55 54 20 25 70 20 25 64 20 25 6c 6c 64 "JOUT %p %d %lld
28730 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 %d\n", pPager,
28740 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 pPg->pgno, .
28750 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 pPa
28760 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c ger->journalOff,
28770 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
28780 65 29 29 3b 0a 20 20 20 20 20 20 20 20 50 41 47 e));. PAG
28790 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f ER_INCR(sqlite3_
287a0 70 61 67 65 72 5f 77 72 69 74 65 6a 5f 63 6f 75 pager_writej_cou
287b0 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 50 41 47 nt);. PAG
287c0 45 52 54 52 41 43 45 28 28 22 4a 4f 55 52 4e 41 ERTRACE(("JOURNA
287d0 4c 20 25 64 20 70 61 67 65 20 25 64 20 6e 65 65 L %d page %d nee
287e0 64 53 79 6e 63 3d 25 64 20 68 61 73 68 28 25 30 dSync=%d hash(%0
287f0 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 8x)\n",.
28800 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 PAGERID(pPa
28810 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c ger), pPg->pgno,
28820 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 28 . (
28830 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 (pPg->flags&PGHD
28840 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 R_NEED_SYNC)?1:0
28850 29 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 ), pager_pagehas
28860 68 28 70 50 67 29 29 29 3b 0a 0a 20 20 20 20 20 h(pPg)));..
28870 20 20 20 2f 2a 20 45 76 65 6e 20 69 66 20 61 6e /* Even if an
28880 20 49 4f 20 6f 72 20 64 69 73 6b 66 75 6c 6c 20 IO or diskfull
28890 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 77 error occurred w
288a0 68 69 6c 65 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 hile journalling
288b0 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 the. **
288c0 70 61 67 65 20 69 6e 20 74 68 65 20 62 6c 6f 63 page in the bloc
288d0 6b 20 61 62 6f 76 65 2c 20 73 65 74 20 74 68 65 k above, set the
288e0 20 6e 65 65 64 2d 73 79 6e 63 20 66 6c 61 67 20 need-sync flag
288f0 66 6f 72 20 74 68 65 20 70 61 67 65 2e 0a 20 20 for the page..
28900 20 20 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 ** Otherwi
28910 73 65 2c 20 77 68 65 6e 20 74 68 65 20 74 72 61 se, when the tra
28920 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c nsaction is roll
28930 65 64 20 62 61 63 6b 2c 20 74 68 65 20 6c 6f 67 ed back, the log
28940 69 63 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a ic in. **
28950 20 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 playback_one_pa
28960 67 65 28 29 20 77 69 6c 6c 20 74 68 69 6e 6b 20 ge() will think
28970 74 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 65 that the page ne
28980 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 6f 72 eds to be restor
28990 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e ed. ** in
289a0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
289b0 6c 65 2e 20 41 6e 64 20 69 66 20 61 6e 20 49 4f le. And if an IO
289c0 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 error occurs wh
289d0 69 6c 65 20 64 6f 69 6e 67 20 73 6f 2c 0a 20 20 ile doing so,.
289e0 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 6f ** then co
289f0 72 72 75 70 74 69 6f 6e 20 6d 61 79 20 66 6f 6c rruption may fol
28a00 6c 6f 77 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a low.. */.
28a10 20 20 20 20 20 20 20 20 69 66 28 20 21 70 50 61 if( !pPa
28a20 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 ger->noSync ){.
28a30 20 20 20 20 20 20 20 20 20 70 50 67 2d 3e 66 6c pPg->fl
28a40 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 ags |= PGHDR_NEE
28a50 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 D_SYNC;.
28a60 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 pPager->needSy
28a70 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 nc = 1;.
28a80 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 6e }.. /* An
28a90 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 error has occur
28aa0 72 65 64 20 77 72 69 74 69 6e 67 20 74 6f 20 74 red writing to t
28ab0 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e he journal file.
28ac0 20 54 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a The . **
28ad0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c transaction wil
28ae0 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b l be rolled back
28af0 20 62 79 20 74 68 65 20 6c 61 79 65 72 20 61 62 by the layer ab
28b00 6f 76 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a ove.. */.
28b10 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
28b20 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
28b30 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
28b40 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 ;. }..
28b50 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 pPager->nRe
28b60 63 2b 2b 3b 0a 20 20 20 20 20 20 20 20 61 73 73 c++;. ass
28b70 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e ert( pPager->pIn
28b80 4a 6f 75 72 6e 61 6c 21 3d 30 20 29 3b 0a 20 20 Journal!=0 );.
28b90 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
28ba0 65 33 42 69 74 76 65 63 53 65 74 28 70 50 61 67 e3BitvecSet(pPag
28bb0 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 er->pInJournal,
28bc0 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 pPg->pgno);.
28bd0 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 testcase( rc
28be0 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 ==SQLITE_NOMEM )
28bf0 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
28c00 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
28c10 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f || rc==SQLITE_NO
28c20 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 72 MEM );. r
28c30 63 20 7c 3d 20 61 64 64 54 6f 53 61 76 65 70 6f c |= addToSavepo
28c40 69 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 65 intBitvecs(pPage
28c50 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 r, pPg->pgno);.
28c60 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
28c70 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
28c80 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 assert( rc
28c90 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 ==SQLITE_NOMEM )
28ca0 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ;. retu
28cb0 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d rn rc;. }
28cc0 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
28cd0 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 if( !pPage
28ce0 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 r->journalStarte
28cf0 64 20 26 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f d && !pPager->no
28d00 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 Sync ){.
28d10 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 pPg->flags |=
28d20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b PGHDR_NEED_SYNC;
28d30 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 . pPage
28d40 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b r->needSync = 1;
28d50 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
28d60 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 PAGERTRACE(("
28d70 41 50 50 45 4e 44 20 25 64 20 70 61 67 65 20 25 APPEND %d page %
28d80 64 20 6e 65 65 64 53 79 6e 63 3d 25 64 5c 6e 22 d needSync=%d\n"
28d90 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
28da0 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 PAGERID(pPager
28db0 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 0a 20 20 ), pPg->pgno,.
28dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 28 28 70 ((p
28dd0 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
28de0 4e 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29 29 NEED_SYNC)?1:0))
28df0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
28e00 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 . . /* If th
28e10 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 e statement jour
28e20 6e 61 6c 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 nal is open and
28e30 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 the page is not
28e40 69 6e 20 69 74 2c 0a 20 20 20 20 2a 2a 20 74 68 in it,. ** th
28e50 65 6e 20 77 72 69 74 65 20 74 68 65 20 63 75 72 en write the cur
28e60 72 65 6e 74 20 70 61 67 65 20 74 6f 20 74 68 65 rent page to the
28e70 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e statement journ
28e80 61 6c 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 al. Note that.
28e90 20 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65 6d ** the statem
28ea0 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d ent journal form
28eb0 61 74 20 64 69 66 66 65 72 73 20 66 72 6f 6d 20 at differs from
28ec0 74 68 65 20 73 74 61 6e 64 61 72 64 20 6a 6f 75 the standard jou
28ed0 72 6e 61 6c 20 66 6f 72 6d 61 74 0a 20 20 20 20 rnal format.
28ee0 2a 2a 20 69 6e 20 74 68 61 74 20 69 74 20 6f 6d ** in that it om
28ef0 69 74 73 20 74 68 65 20 63 68 65 63 6b 73 75 6d its the checksum
28f00 73 20 61 6e 64 20 74 68 65 20 68 65 61 64 65 72 s and the header
28f10 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
28f20 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 subjRequiresPag
28f30 65 28 70 50 67 29 20 29 7b 0a 20 20 20 20 20 20 e(pPg) ){.
28f40 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 rc = subjournalP
28f50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a age(pPg);. }.
28f60 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 }.. /* Update
28f70 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 69 the database si
28f80 7a 65 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 ze and return..
28f90 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 */. assert( pP
28fa0 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
28fb0 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20 69 ER_SHARED );. i
28fc0 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a f( pPager->dbSiz
28fd0 65 3c 70 50 67 2d 3e 70 67 6e 6f 20 29 7b 0a 20 e<pPg->pgno ){.
28fe0 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a pPager->dbSiz
28ff0 65 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 e = pPg->pgno;.
29000 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
29010 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 }../*.** Mark a
29020 64 61 74 61 20 70 61 67 65 20 61 73 20 77 72 69 data page as wri
29030 74 65 61 62 6c 65 2e 20 54 68 69 73 20 72 6f 75 teable. This rou
29040 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c tine must be cal
29050 6c 65 64 20 62 65 66 6f 72 65 20 0a 2a 2a 20 6d led before .** m
29060 61 6b 69 6e 67 20 63 68 61 6e 67 65 73 20 74 6f aking changes to
29070 20 61 20 70 61 67 65 2e 20 54 68 65 20 63 61 6c a page. The cal
29080 6c 65 72 20 6d 75 73 74 20 63 68 65 63 6b 20 74 ler must check t
29090 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
290a0 0a 2a 2a 20 6f 66 20 74 68 69 73 20 66 75 6e 63 .** of this func
290b0 74 69 6f 6e 20 61 6e 64 20 62 65 20 63 61 72 65 tion and be care
290c0 66 75 6c 20 6e 6f 74 20 74 6f 20 63 68 61 6e 67 ful not to chang
290d0 65 20 61 6e 79 20 70 61 67 65 20 64 61 74 61 20 e any page data
290e0 75 6e 6c 65 73 73 20 0a 2a 2a 20 74 68 69 73 20 unless .** this
290f0 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
29100 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a SQLITE_OK..**.**
29110 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 The difference
29120 62 65 74 77 65 65 6e 20 74 68 69 73 20 66 75 6e between this fun
29130 63 74 69 6f 6e 20 61 6e 64 20 70 61 67 65 72 5f ction and pager_
29140 77 72 69 74 65 28 29 20 69 73 20 74 68 61 74 20 write() is that
29150 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e this.** function
29160 20 61 6c 73 6f 20 64 65 61 6c 73 20 77 69 74 68 also deals with
29170 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 61 73 the special cas
29180 65 20 77 68 65 72 65 20 32 20 6f 72 20 6d 6f 72 e where 2 or mor
29190 65 20 70 61 67 65 73 0a 2a 2a 20 66 69 74 20 6f e pages.** fit o
291a0 6e 20 61 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 n a single disk
291b0 73 65 63 74 6f 72 2e 20 49 6e 20 74 68 69 73 20 sector. In this
291c0 63 61 73 65 20 61 6c 6c 20 63 6f 2d 72 65 73 69 case all co-resi
291d0 64 65 6e 74 20 70 61 67 65 73 0a 2a 2a 20 6d 75 dent pages.** mu
291e0 73 74 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 st have been wri
291f0 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 tten to the jour
29200 6e 61 6c 20 66 69 6c 65 20 62 65 66 6f 72 65 20 nal file before
29210 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a returning..**.**
29220 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 If an error occ
29230 75 72 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 urs, SQLITE_NOME
29240 4d 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 M or an IO error
29250 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
29260 64 0a 2a 2a 20 61 73 20 61 70 70 72 6f 70 72 69 d.** as appropri
29270 61 74 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ate. Otherwise,
29280 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 69 6e SQLITE_OK..*/.in
29290 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 t sqlite3PagerWr
292a0 69 74 65 28 44 62 50 61 67 65 20 2a 70 44 62 50 ite(DbPage *pDbP
292b0 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d age){. int rc =
292c0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 50 SQLITE_OK;.. P
292d0 67 48 64 72 20 2a 70 50 67 20 3d 20 70 44 62 50 gHdr *pPg = pDbP
292e0 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 age;. Pager *pP
292f0 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 ager = pPg->pPag
29300 65 72 3b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 er;. Pgno nPage
29310 50 65 72 53 65 63 74 6f 72 20 3d 20 28 70 50 61 PerSector = (pPa
29320 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 2f ger->sectorSize/
29330 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
29340 29 3b 0a 0a 20 20 69 66 28 20 6e 50 61 67 65 50 );.. if( nPageP
29350 65 72 53 65 63 74 6f 72 3e 31 20 29 7b 0a 20 20 erSector>1 ){.
29360 20 20 50 67 6e 6f 20 6e 50 61 67 65 43 6f 75 6e Pgno nPageCoun
29370 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 t; /* T
29380 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 otal number of p
29390 61 67 65 73 20 69 6e 20 64 61 74 61 62 61 73 65 ages in database
293a0 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 50 67 6e file */. Pgn
293b0 6f 20 70 67 31 3b 20 20 20 20 20 20 20 20 20 20 o pg1;
293c0 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
293d0 70 61 67 65 20 6f 66 20 74 68 65 20 73 65 63 74 page of the sect
293e0 6f 72 20 70 50 67 20 69 73 20 6c 6f 63 61 74 65 or pPg is locate
293f0 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 d on. */. int
29400 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 nPage;
29410 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
29420 20 6f 66 20 70 61 67 65 73 20 73 74 61 72 74 69 of pages starti
29430 6e 67 20 61 74 20 70 67 31 20 74 6f 20 6a 6f 75 ng at pg1 to jou
29440 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 69 6e 74 20 rnal */. int
29450 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ii;
29460 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
29470 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 unter */. int
29480 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 20 20 needSync = 0;
29490 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
294a0 66 20 61 6e 79 20 70 61 67 65 20 68 61 73 20 50 f any page has P
294b0 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 2a GHDR_NEED_SYNC *
294c0 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 /.. /* Set th
294d0 65 20 64 6f 4e 6f 74 53 79 6e 63 20 66 6c 61 67 e doNotSync flag
294e0 20 74 6f 20 31 2e 20 54 68 69 73 20 69 73 20 62 to 1. This is b
294f0 65 63 61 75 73 65 20 77 65 20 63 61 6e 6e 6f 74 ecause we cannot
29500 20 61 6c 6c 6f 77 20 61 20 6a 6f 75 72 6e 61 6c allow a journal
29510 0a 20 20 20 20 2a 2a 20 68 65 61 64 65 72 20 74 . ** header t
29520 6f 20 62 65 20 77 72 69 74 74 65 6e 20 62 65 74 o be written bet
29530 77 65 65 6e 20 74 68 65 20 70 61 67 65 73 20 6a ween the pages j
29540 6f 75 72 6e 61 6c 65 64 20 62 79 20 74 68 69 73 ournaled by this
29550 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 2a function.. *
29560 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d /. assert( !M
29570 45 4d 44 42 20 29 3b 0a 20 20 20 20 61 73 73 65 EMDB );. asse
29580 72 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f rt( pPager->doNo
29590 74 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 tSync==0 );.
295a0 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e pPager->doNotSyn
295b0 63 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a 20 54 c = 1;.. /* T
295c0 68 69 73 20 74 72 69 63 6b 20 61 73 73 75 6d 65 his trick assume
295d0 73 20 74 68 61 74 20 62 6f 74 68 20 74 68 65 20 s that both the
295e0 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65 page-size and se
295f0 63 74 6f 72 2d 73 69 7a 65 20 61 72 65 0a 20 20 ctor-size are.
29600 20 20 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 ** an integer
29610 70 6f 77 65 72 20 6f 66 20 32 2e 20 49 74 20 73 power of 2. It s
29620 65 74 73 20 76 61 72 69 61 62 6c 65 20 70 67 31 ets variable pg1
29630 20 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69 to the identifi
29640 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 er. ** of the
29650 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 first page of t
29660 68 65 20 73 65 63 74 6f 72 20 70 50 67 20 69 73 he sector pPg is
29670 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 0a 20 20 20 located on..
29680 20 2a 2f 0a 20 20 20 20 70 67 31 20 3d 20 28 28 */. pg1 = ((
29690 70 50 67 2d 3e 70 67 6e 6f 2d 31 29 20 26 20 7e pPg->pgno-1) & ~
296a0 28 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 2d (nPagePerSector-
296b0 31 29 29 20 2b 20 31 3b 0a 0a 20 20 20 20 73 71 1)) + 1;.. sq
296c0 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f lite3PagerPageco
296d0 75 6e 74 28 70 50 61 67 65 72 2c 20 28 69 6e 74 unt(pPager, (int
296e0 20 2a 29 26 6e 50 61 67 65 43 6f 75 6e 74 29 3b *)&nPageCount);
296f0 0a 20 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67 . if( pPg->pg
29700 6e 6f 3e 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b no>nPageCount ){
29710 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 28 . nPage = (
29720 70 50 67 2d 3e 70 67 6e 6f 20 2d 20 70 67 31 29 pPg->pgno - pg1)
29730 2b 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 +1;. }else if
29740 28 20 28 70 67 31 2b 6e 50 61 67 65 50 65 72 53 ( (pg1+nPagePerS
29750 65 63 74 6f 72 2d 31 29 3e 6e 50 61 67 65 43 6f ector-1)>nPageCo
29760 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 unt ){. nPa
29770 67 65 20 3d 20 6e 50 61 67 65 43 6f 75 6e 74 2b ge = nPageCount+
29780 31 2d 70 67 31 3b 0a 20 20 20 20 7d 65 6c 73 65 1-pg1;. }else
29790 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 {. nPage =
297a0 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 3b 0a nPagePerSector;.
297b0 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
297c0 28 6e 50 61 67 65 3e 30 29 3b 0a 20 20 20 20 61 (nPage>0);. a
297d0 73 73 65 72 74 28 70 67 31 3c 3d 70 50 67 2d 3e ssert(pg1<=pPg->
297e0 70 67 6e 6f 29 3b 0a 20 20 20 20 61 73 73 65 72 pgno);. asser
297f0 74 28 28 70 67 31 2b 6e 50 61 67 65 29 3e 70 50 t((pg1+nPage)>pP
29800 67 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 20 66 g->pgno);.. f
29810 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67 or(ii=0; ii<nPag
29820 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f e && rc==SQLITE_
29830 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 OK; ii++){.
29840 20 50 67 6e 6f 20 70 67 20 3d 20 70 67 31 2b 69 Pgno pg = pg1+i
29850 69 3b 0a 20 20 20 20 20 20 50 67 48 64 72 20 2a i;. PgHdr *
29860 70 50 61 67 65 3b 0a 20 20 20 20 20 20 69 66 28 pPage;. if(
29870 20 70 67 3d 3d 70 50 67 2d 3e 70 67 6e 6f 20 7c pg==pPg->pgno |
29880 7c 20 21 73 71 6c 69 74 65 33 42 69 74 76 65 63 | !sqlite3Bitvec
29890 54 65 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e Test(pPager->pIn
298a0 4a 6f 75 72 6e 61 6c 2c 20 70 67 29 20 29 7b 0a Journal, pg) ){.
298b0 20 20 20 20 20 20 20 20 69 66 28 20 70 67 21 3d if( pg!=
298c0 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 PAGER_MJ_PGNO(pP
298d0 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 20 ager) ){.
298e0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
298f0 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 agerGet(pPager,
29900 70 67 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 20 pg, &pPage);.
29910 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
29920 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
29930 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 rc = pag
29940 65 72 5f 77 72 69 74 65 28 70 50 61 67 65 29 3b er_write(pPage);
29950 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
29960 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 pPage->flags&PG
29970 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b HDR_NEED_SYNC ){
29980 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e . n
29990 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 eedSync = 1;.
299a0 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 asser
299b0 74 28 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 t(pPager->needSy
299c0 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 nc);.
299d0 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 }. s
299e0 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
299f0 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 (pPage);.
29a00 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
29a10 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 }else if( (
29a20 70 50 61 67 65 20 3d 20 70 61 67 65 72 5f 6c 6f pPage = pager_lo
29a30 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 29 okup(pPager, pg)
29a40 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )!=0 ){.
29a50 69 66 28 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 if( pPage->flags
29a60 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
29a70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 ){. ne
29a80 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 edSync = 1;.
29a90 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 }. sq
29aa0 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
29ab0 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a pPage);. }.
29ac0 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 }.. /* If
29ad0 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f the PGHDR_NEED_
29ae0 53 59 4e 43 20 66 6c 61 67 20 69 73 20 73 65 74 SYNC flag is set
29af0 20 66 6f 72 20 61 6e 79 20 6f 66 20 74 68 65 20 for any of the
29b00 6e 50 61 67 65 20 70 61 67 65 73 20 0a 20 20 20 nPage pages .
29b10 20 2a 2a 20 73 74 61 72 74 69 6e 67 20 61 74 20 ** starting at
29b20 70 67 31 2c 20 74 68 65 6e 20 69 74 20 6e 65 65 pg1, then it nee
29b30 64 73 20 74 6f 20 62 65 20 73 65 74 20 66 6f 72 ds to be set for
29b40 20 61 6c 6c 20 6f 66 20 74 68 65 6d 2e 20 42 65 all of them. Be
29b50 63 61 75 73 65 0a 20 20 20 20 2a 2a 20 77 72 69 cause. ** wri
29b60 74 69 6e 67 20 74 6f 20 61 6e 79 20 6f 66 20 74 ting to any of t
29b70 68 65 73 65 20 6e 50 61 67 65 20 70 61 67 65 73 hese nPage pages
29b80 20 6d 61 79 20 64 61 6d 61 67 65 20 74 68 65 20 may damage the
29b90 6f 74 68 65 72 73 2c 20 74 68 65 0a 20 20 20 20 others, the.
29ba0 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ** journal file
29bb0 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 73 79 6e must contain syn
29bc0 63 28 29 65 64 20 63 6f 70 69 65 73 20 6f 66 20 c()ed copies of
29bd0 61 6c 6c 20 6f 66 20 74 68 65 6d 0a 20 20 20 20 all of them.
29be0 2a 2a 20 62 65 66 6f 72 65 20 61 6e 79 20 6f 66 ** before any of
29bf0 20 74 68 65 6d 20 63 61 6e 20 62 65 20 77 72 69 them can be wri
29c00 74 74 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 tten out to the
29c10 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 database file..
29c20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 */. if( rc
29c30 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e ==SQLITE_OK && n
29c40 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 eedSync ){.
29c50 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 assert( !MEMDB
29c60 26 26 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e && pPager->noSyn
29c70 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 66 6f c==0 );. fo
29c80 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65 r(ii=0; ii<nPage
29c90 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 ; ii++){.
29ca0 20 50 67 48 64 72 20 2a 70 50 61 67 65 20 3d 20 PgHdr *pPage =
29cb0 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 pager_lookup(pPa
29cc0 67 65 72 2c 20 70 67 31 2b 69 69 29 3b 0a 20 20 ger, pg1+ii);.
29cd0 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 20 if( pPage
29ce0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 ){. pPa
29cf0 67 65 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 ge->flags |= PGH
29d00 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 DR_NEED_SYNC;.
29d10 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 sqlite3P
29d20 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 agerUnref(pPage)
29d30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
29d40 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 }. assert
29d50 28 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e (pPager->needSyn
29d60 63 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 c);. }.. a
29d70 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 ssert( pPager->d
29d80 6f 4e 6f 74 53 79 6e 63 3d 3d 31 20 29 3b 0a 20 oNotSync==1 );.
29d90 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 pPager->doNot
29da0 53 79 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 Sync = 0;. }els
29db0 65 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 e{. rc = page
29dc0 72 5f 77 72 69 74 65 28 70 44 62 50 61 67 65 29 r_write(pDbPage)
29dd0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
29de0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 c;.}../*.** Retu
29df0 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 70 rn TRUE if the p
29e00 61 67 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 age given in the
29e10 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 70 72 argument was pr
29e20 65 76 69 6f 75 73 6c 79 20 70 61 73 73 65 64 0a eviously passed.
29e30 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 ** to sqlite3Pag
29e40 65 72 57 72 69 74 65 28 29 2e 20 20 49 6e 20 6f erWrite(). In o
29e50 74 68 65 72 20 77 6f 72 64 73 2c 20 72 65 74 75 ther words, retu
29e60 72 6e 20 54 52 55 45 20 69 66 20 69 74 20 69 73 rn TRUE if it is
29e70 20 6f 6b 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 ok.** to change
29e80 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 the content of
29e90 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 the page..*/.#if
29ea0 6e 64 65 66 20 4e 44 45 42 55 47 0a 69 6e 74 20 ndef NDEBUG.int
29eb0 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
29ec0 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 20 2a iteable(DbPage *
29ed0 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 pPg){. return p
29ee0 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
29ef0 44 49 52 54 59 3b 0a 7d 0a 23 65 6e 64 69 66 0a DIRTY;.}.#endif.
29f00 0a 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f ./*.** A call to
29f10 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 65 this routine te
29f20 6c 6c 73 20 74 68 65 20 70 61 67 65 72 20 74 68 lls the pager th
29f30 61 74 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 at it is not nec
29f40 65 73 73 61 72 79 20 74 6f 0a 2a 2a 20 77 72 69 essary to.** wri
29f50 74 65 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 te the informati
29f60 6f 6e 20 6f 6e 20 70 61 67 65 20 70 50 67 20 62 on on page pPg b
29f70 61 63 6b 20 74 6f 20 74 68 65 20 64 69 73 6b 2c ack to the disk,
29f80 20 65 76 65 6e 20 74 68 6f 75 67 68 0a 2a 2a 20 even though.**
29f90 74 68 61 74 20 70 61 67 65 20 6d 69 67 68 74 20 that page might
29fa0 62 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 be marked as dir
29fb0 74 79 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e ty. This happen
29fc0 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 s, for example,
29fd0 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 when.** the page
29fe0 20 68 61 73 20 62 65 65 6e 20 61 64 64 65 64 20 has been added
29ff0 61 73 20 61 20 6c 65 61 66 20 6f 66 20 74 68 65 as a leaf of the
2a000 20 66 72 65 65 6c 69 73 74 20 61 6e 64 20 73 6f freelist and so
2a010 20 69 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 its.** content
2a020 6e 6f 20 6c 6f 6e 67 65 72 20 6d 61 74 74 65 72 no longer matter
2a030 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76 65 s..**.** The ove
2a040 72 6c 79 69 6e 67 20 73 6f 66 74 77 61 72 65 20 rlying software
2a050 6c 61 79 65 72 20 63 61 6c 6c 73 20 74 68 69 73 layer calls this
2a060 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 61 6c routine when al
2a070 6c 20 6f 66 20 74 68 65 20 64 61 74 61 0a 2a 2a l of the data.**
2a080 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 70 61 on the given pa
2a090 67 65 20 69 73 20 75 6e 75 73 65 64 2e 20 54 68 ge is unused. Th
2a0a0 65 20 70 61 67 65 72 20 6d 61 72 6b 73 20 74 68 e pager marks th
2a0b0 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e 20 e page as clean
2a0c0 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 64 6f so.** that it do
2a0d0 65 73 20 6e 6f 74 20 67 65 74 20 77 72 69 74 74 es not get writt
2a0e0 65 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a en to disk..**.*
2a0f0 2a 20 54 65 73 74 73 20 73 68 6f 77 20 74 68 61 * Tests show tha
2a100 74 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 t this optimizat
2a110 69 6f 6e 20 63 61 6e 20 71 75 61 64 72 75 70 6c ion can quadrupl
2a120 65 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 6c e the speed of l
2a130 61 72 67 65 20 0a 2a 2a 20 44 45 4c 45 54 45 20 arge .** DELETE
2a140 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 76 operations..*/.v
2a150 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
2a160 44 6f 6e 74 57 72 69 74 65 28 50 67 48 64 72 20 DontWrite(PgHdr
2a170 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a *pPg){. Pager *
2a180 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 pPager = pPg->pP
2a190 61 67 65 72 3b 0a 20 20 69 66 28 20 28 70 50 67 ager;. if( (pPg
2a1a0 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
2a1b0 52 54 59 29 20 26 26 20 70 50 61 67 65 72 2d 3e RTY) && pPager->
2a1c0 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 7b nSavepoint==0 ){
2a1d0 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 . PAGERTRACE(
2a1e0 28 22 44 4f 4e 54 5f 57 52 49 54 45 20 70 61 67 ("DONT_WRITE pag
2a1f0 65 20 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 e %d of %d\n", p
2a200 50 67 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 Pg->pgno, PAGERI
2a210 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 D(pPager)));.
2a220 20 49 4f 54 52 41 43 45 28 28 22 43 4c 45 41 4e IOTRACE(("CLEAN
2a230 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 %p %d\n", pPage
2a240 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 r, pPg->pgno)).
2a250 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d pPg->flags |=
2a260 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 PGHDR_DONT_WRIT
2a270 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 E;.#ifdef SQLITE
2a280 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 _CHECK_PAGES.
2a290 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d pPg->pageHash =
2a2a0 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 pager_pagehash(
2a2b0 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d pPg);.#endif. }
2a2c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
2a2d0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
2a2e0 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 to increment th
2a2f0 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 e value of the d
2a300 61 74 61 62 61 73 65 20 66 69 6c 65 20 0a 2a 2a atabase file .**
2a310 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2c change-counter,
2a320 20 73 74 6f 72 65 64 20 61 73 20 61 20 34 2d 62 stored as a 4-b
2a330 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 yte big-endian i
2a340 6e 74 65 67 65 72 20 73 74 61 72 74 69 6e 67 20 nteger starting
2a350 61 74 20 0a 2a 2a 20 62 79 74 65 20 6f 66 66 73 at .** byte offs
2a360 65 74 20 32 34 20 6f 66 20 74 68 65 20 70 61 67 et 24 of the pag
2a370 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 er file..**.** I
2a380 66 20 74 68 65 20 69 73 44 69 72 65 63 74 4d 6f f the isDirectMo
2a390 64 65 20 66 6c 61 67 20 69 73 20 7a 65 72 6f 2c de flag is zero,
2a3a0 20 74 68 65 6e 20 74 68 69 73 20 69 73 20 64 6f then this is do
2a3b0 6e 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 0a 2a ne by calling .*
2a3c0 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 * sqlite3PagerWr
2a3d0 69 74 65 28 29 20 6f 6e 20 70 61 67 65 20 31 2c ite() on page 1,
2a3e0 20 74 68 65 6e 20 6d 6f 64 69 66 79 69 6e 67 20 then modifying
2a3f0 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
2a400 74 68 65 0a 2a 2a 20 70 61 67 65 20 64 61 74 61 the.** page data
2a410 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
2a420 68 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 he file will be
2a430 75 70 64 61 74 65 64 20 77 68 65 6e 20 74 68 65 updated when the
2a440 20 63 75 72 72 65 6e 74 0a 2a 2a 20 74 72 61 6e current.** tran
2a450 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 saction is commi
2a460 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tted..**.** The
2a470 69 73 44 69 72 65 63 74 4d 6f 64 65 20 66 6c 61 isDirectMode fla
2a480 67 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6e 6f g may only be no
2a490 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65 20 6c 69 n-zero if the li
2a4a0 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69 6c brary was compil
2a4b0 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 53 ed.** with the S
2a4c0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f QLITE_ENABLE_ATO
2a4d0 4d 49 43 5f 57 52 49 54 45 20 6d 61 63 72 6f 20 MIC_WRITE macro
2a4e0 64 65 66 69 6e 65 64 2e 20 49 6e 20 74 68 69 73 defined. In this
2a4f0 20 63 61 73 65 2c 0a 2a 2a 20 69 66 20 69 73 44 case,.** if isD
2a500 69 72 65 63 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 irect is non-zer
2a510 6f 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 o, then the data
2a520 62 61 73 65 20 66 69 6c 65 20 69 73 20 75 70 64 base file is upd
2a530 61 74 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a ated directly.**
2a540 20 62 79 20 77 72 69 74 69 6e 67 20 61 6e 20 75 by writing an u
2a550 70 64 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f pdated version o
2a560 66 20 70 61 67 65 20 31 20 75 73 69 6e 67 20 61 f page 1 using a
2a570 20 63 61 6c 6c 20 74 6f 20 74 68 65 20 0a 2a 2a call to the .**
2a580 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
2a590 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 ) function..*/.s
2a5a0 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f tatic int pager_
2a5b0 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 incr_changecount
2a5c0 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 er(Pager *pPager
2a5d0 2c 20 69 6e 74 20 69 73 44 69 72 65 63 74 4d 6f , int isDirectMo
2a5e0 64 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 de){. int rc =
2a5f0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a SQLITE_OK;.. /*
2a600 20 44 65 63 6c 61 72 65 20 61 6e 64 20 69 6e 69 Declare and ini
2a610 74 69 61 6c 69 7a 65 20 63 6f 6e 73 74 61 6e 74 tialize constant
2a620 20 69 6e 74 65 67 65 72 20 27 69 73 44 69 72 65 integer 'isDire
2a630 63 74 27 2e 20 49 66 20 74 68 65 0a 20 20 2a 2a ct'. If the. **
2a640 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 atomic-write op
2a650 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 65 6e timization is en
2a660 61 62 6c 65 64 20 69 6e 20 74 68 69 73 20 62 75 abled in this bu
2a670 69 6c 64 2c 20 74 68 65 6e 20 69 73 44 69 72 65 ild, then isDire
2a680 63 74 0a 20 20 2a 2a 20 69 73 20 69 6e 69 74 69 ct. ** is initi
2a690 61 6c 69 7a 65 64 20 74 6f 20 74 68 65 20 76 61 alized to the va
2a6a0 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 lue passed as th
2a6b0 65 20 69 73 44 69 72 65 63 74 4d 6f 64 65 20 70 e isDirectMode p
2a6c0 61 72 61 6d 65 74 65 72 0a 20 20 2a 2a 20 74 6f arameter. ** to
2a6d0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 this function.
2a6e0 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 69 73 Otherwise, it is
2a6f0 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20 7a always set to z
2a700 65 72 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 ero.. **. ** T
2a710 68 65 20 69 64 65 61 20 69 73 20 74 68 61 74 20 he idea is that
2a720 69 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 if the atomic-wr
2a730 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ite optimization
2a740 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 65 6e 61 is not. ** ena
2a750 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 20 bled at compile
2a760 74 69 6d 65 2c 20 74 68 65 20 63 6f 6d 70 69 6c time, the compil
2a770 65 72 20 63 61 6e 20 6f 6d 69 74 20 74 68 65 20 er can omit the
2a780 74 65 73 74 73 20 6f 66 0a 20 20 2a 2a 20 27 69 tests of. ** 'i
2a790 73 44 69 72 65 63 74 27 20 62 65 6c 6f 77 2c 20 sDirect' below,
2a7a0 61 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 62 as well as the b
2a7b0 6c 6f 63 6b 20 65 6e 63 6c 6f 73 65 64 20 69 6e lock enclosed in
2a7c0 20 74 68 65 0a 20 20 2a 2a 20 22 69 66 28 20 69 the. ** "if( i
2a7d0 73 44 69 72 65 63 74 20 29 22 20 63 6f 6e 64 69 sDirect )" condi
2a7e0 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 tion.. */.#ifnd
2a7f0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
2a800 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 23 20 _ATOMIC_WRITE.#
2a810 64 65 66 69 6e 65 20 44 49 52 45 43 54 5f 4d 4f define DIRECT_MO
2a820 44 45 20 30 0a 20 20 61 73 73 65 72 74 28 20 69 DE 0. assert( i
2a830 73 44 69 72 65 63 74 4d 6f 64 65 3d 3d 30 20 29 sDirectMode==0 )
2a840 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
2a850 45 54 45 52 28 69 73 44 69 72 65 63 74 4d 6f 64 ETER(isDirectMod
2a860 65 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 e);.#else.# defi
2a870 6e 65 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 69 ne DIRECT_MODE i
2a880 73 44 69 72 65 63 74 4d 6f 64 65 0a 23 65 6e 64 sDirectMode.#end
2a890 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 if.. assert( pP
2a8a0 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
2a8b0 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 ER_RESERVED );.
2a8c0 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 63 68 if( !pPager->ch
2a8d0 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 26 26 angeCountDone &&
2a8e0 20 41 4c 57 41 59 53 28 70 50 61 67 65 72 2d 3e ALWAYS(pPager->
2a8f0 64 62 53 69 7a 65 3e 30 29 20 29 7b 0a 20 20 20 dbSize>0) ){.
2a900 20 50 67 48 64 72 20 2a 70 50 67 48 64 72 3b 20 PgHdr *pPgHdr;
2a910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2a920 2a 20 52 65 66 65 72 65 6e 63 65 20 74 6f 20 70 * Reference to p
2a930 61 67 65 20 31 20 2a 2f 0a 20 20 20 20 75 33 32 age 1 */. u32
2a940 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 3b change_counter;
2a950 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
2a960 69 74 69 61 6c 20 76 61 6c 75 65 20 6f 66 20 63 itial value of c
2a970 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 66 69 hange-counter fi
2a980 65 6c 64 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 eld */.. asse
2a990 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d rt( !pPager->tem
2a9a0 70 46 69 6c 65 20 26 26 20 69 73 4f 70 65 6e 28 pFile && isOpen(
2a9b0 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 0a pPager->fd) );..
2a9c0 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 /* Open page
2a9d0 20 31 20 6f 66 20 74 68 65 20 66 69 6c 65 20 66 1 of the file f
2a9e0 6f 72 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a 20 or writing. */.
2a9f0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
2aa00 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 agerGet(pPager,
2aa10 31 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 20 20 1, &pPgHdr);.
2aa20 20 61 73 73 65 72 74 28 20 70 50 67 48 64 72 3d assert( pPgHdr=
2aa30 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 =0 || rc==SQLITE
2aa40 5f 4f 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 _OK );.. /* I
2aa50 66 20 70 61 67 65 20 6f 6e 65 20 77 61 73 20 66 f page one was f
2aa60 65 74 63 68 65 64 20 73 75 63 63 65 73 73 66 75 etched successfu
2aa70 6c 6c 79 2c 20 61 6e 64 20 74 68 69 73 20 66 75 lly, and this fu
2aa80 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 20 20 nction is not.
2aa90 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 69 ** operating i
2aaa0 6e 20 64 69 72 65 63 74 2d 6d 6f 64 65 2c 20 6d n direct-mode, m
2aab0 61 6b 65 20 70 61 67 65 20 31 20 77 72 69 74 61 ake page 1 writa
2aac0 62 6c 65 2e 20 20 57 68 65 6e 20 6e 6f 74 20 69 ble. When not i
2aad0 6e 20 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 74 n . ** direct
2aae0 20 6d 6f 64 65 2c 20 70 61 67 65 20 31 20 69 73 mode, page 1 is
2aaf0 20 61 6c 77 61 79 73 20 68 65 6c 64 20 69 6e 20 always held in
2ab00 63 61 63 68 65 20 61 6e 64 20 68 65 6e 63 65 20 cache and hence
2ab10 74 68 65 20 50 61 67 65 72 47 65 74 28 29 0a 20 the PagerGet().
2ab20 20 20 20 2a 2a 20 61 62 6f 76 65 20 69 73 20 61 ** above is a
2ab30 6c 77 61 79 73 20 73 75 63 63 65 73 73 66 75 6c lways successful
2ab40 20 2d 20 68 65 6e 63 65 20 74 68 65 20 41 4c 57 - hence the ALW
2ab50 41 59 53 20 6f 6e 20 72 63 3d 3d 53 51 4c 49 54 AYS on rc==SQLIT
2ab60 45 5f 4f 4b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 E_OK.. */.
2ab70 20 69 66 28 20 21 44 49 52 45 43 54 5f 4d 4f 44 if( !DIRECT_MOD
2ab80 45 20 26 26 20 41 4c 57 41 59 53 28 72 63 3d 3d E && ALWAYS(rc==
2ab90 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 20 SQLITE_OK) ){.
2aba0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
2abb0 50 61 67 65 72 57 72 69 74 65 28 70 50 67 48 64 PagerWrite(pPgHd
2abc0 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 r);. }.. i
2abd0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
2abe0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 63 ){. /* Inc
2abf0 72 65 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 65 rement the value
2ac00 20 6a 75 73 74 20 72 65 61 64 20 61 6e 64 20 77 just read and w
2ac10 72 69 74 65 20 69 74 20 62 61 63 6b 20 74 6f 20 rite it back to
2ac20 62 79 74 65 20 32 34 2e 20 2a 2f 0a 20 20 20 20 byte 24. */.
2ac30 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 change_counter
2ac40 20 3d 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 = sqlite3Get4by
2ac50 74 65 28 28 75 38 2a 29 70 50 61 67 65 72 2d 3e te((u8*)pPager->
2ac60 64 62 46 69 6c 65 56 65 72 73 29 3b 0a 20 20 20 dbFileVers);.
2ac70 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 change_counte
2ac80 72 2b 2b 3b 0a 20 20 20 20 20 20 70 75 74 33 32 r++;. put32
2ac90 62 69 74 73 28 28 28 63 68 61 72 2a 29 70 50 67 bits(((char*)pPg
2aca0 48 64 72 2d 3e 70 44 61 74 61 29 2b 32 34 2c 20 Hdr->pData)+24,
2acb0 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 29 3b change_counter);
2acc0 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 72 75 .. /* If ru
2acd0 6e 6e 69 6e 67 20 69 6e 20 64 69 72 65 63 74 20 nning in direct
2ace0 6d 6f 64 65 2c 20 77 72 69 74 65 20 74 68 65 20 mode, write the
2acf0 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 61 67 65 contents of page
2ad00 20 31 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 1 to the file.
2ad10 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 44 49 52 */. if( DIR
2ad20 45 43 54 5f 4d 4f 44 45 20 29 7b 0a 20 20 20 20 ECT_MODE ){.
2ad30 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a const void *
2ad40 7a 42 75 66 20 3d 20 70 50 67 48 64 72 2d 3e 70 zBuf = pPgHdr->p
2ad50 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 61 73 Data;. as
2ad60 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 sert( pPager->db
2ad70 46 69 6c 65 53 69 7a 65 3e 30 20 29 3b 0a 20 20 FileSize>0 );.
2ad80 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
2ad90 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 e3OsWrite(pPager
2ada0 2d 3e 66 64 2c 20 7a 42 75 66 2c 20 70 50 61 67 ->fd, zBuf, pPag
2adb0 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 30 29 er->pageSize, 0)
2adc0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
2add0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
2ade0 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d pPager-
2adf0 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 >changeCountDone
2ae00 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 1;. }.
2ae10 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
2ae20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 pPager->cha
2ae30 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 ngeCountDone = 1
2ae40 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
2ae50 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 . /* Release
2ae60 74 68 65 20 70 61 67 65 20 72 65 66 65 72 65 6e the page referen
2ae70 63 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 ce. */. sqlit
2ae80 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 e3PagerUnref(pPg
2ae90 48 64 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 Hdr);. }. retu
2aea0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
2aeb0 53 79 6e 63 20 74 68 65 20 70 61 67 65 72 20 66 Sync the pager f
2aec0 69 6c 65 20 74 6f 20 64 69 73 6b 2e 20 54 68 69 ile to disk. Thi
2aed0 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 s is a no-op for
2aee0 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 73 in-memory files
2aef0 0a 2a 2a 20 6f 72 20 70 61 67 65 73 20 77 69 74 .** or pages wit
2af00 68 20 74 68 65 20 50 61 67 65 72 2e 6e 6f 53 79 h the Pager.noSy
2af10 6e 63 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2a 0a nc flag set..**.
2af20 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c ** If successful
2af30 2c 20 6f 72 20 63 61 6c 6c 65 64 20 6f 6e 20 61 , or called on a
2af40 20 70 61 67 65 72 20 66 6f 72 20 77 68 69 63 68 pager for which
2af50 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 it is a no-op,
2af60 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e this.** function
2af70 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
2af80 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 OK. Otherwise, a
2af90 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 n IO error code
2afa0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a is returned..*/.
2afb0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
2afc0 53 79 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 Sync(Pager *pPag
2afd0 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 er){. int rc;
2afe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2aff0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
2b000 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
2b010 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 assert( !MEMDB )
2b020 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e ;. if( pPager->
2b030 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 noSync ){. rc
2b040 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
2b050 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
2b060 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 sqlite3OsSync(pP
2b070 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 ager->fd, pPager
2b080 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 ->sync_flags);.
2b090 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
2b0a0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 }../*.** Sync th
2b0b0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
2b0c0 66 6f 72 20 74 68 65 20 70 61 67 65 72 20 70 50 for the pager pP
2b0d0 61 67 65 72 2e 20 7a 4d 61 73 74 65 72 20 70 6f ager. zMaster po
2b0e0 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 61 6d 65 ints to the name
2b0f0 0a 2a 2a 20 6f 66 20 61 20 6d 61 73 74 65 72 20 .** of a master
2b100 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 61 journal file tha
2b110 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74 t should be writ
2b120 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 ten into the ind
2b130 69 76 69 64 75 61 6c 0a 2a 2a 20 6a 6f 75 72 6e ividual.** journ
2b140 61 6c 20 66 69 6c 65 2e 20 7a 4d 61 73 74 65 72 al file. zMaster
2b150 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2c 20 77 68 may be NULL, wh
2b160 69 63 68 20 69 73 20 69 6e 74 65 72 70 72 65 74 ich is interpret
2b170 65 64 20 61 73 20 6e 6f 20 6d 61 73 74 65 72 0a ed as no master.
2b180 2a 2a 20 6a 6f 75 72 6e 61 6c 20 28 61 20 73 69 ** journal (a si
2b190 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 ngle database tr
2b1a0 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a ansaction)..**.*
2b1b0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 * This routine e
2b1c0 6e 73 75 72 65 73 20 74 68 61 74 3a 0a 2a 2a 0a nsures that:.**.
2b1d0 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 74 61 62 ** * The datab
2b1e0 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 2d ase file change-
2b1f0 63 6f 75 6e 74 65 72 20 69 73 20 75 70 64 61 74 counter is updat
2b200 65 64 2c 0a 2a 2a 20 20 20 2a 20 74 68 65 20 6a ed,.** * the j
2b210 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 ournal is synced
2b220 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 74 6f (unless the ato
2b230 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 mic-write optimi
2b240 7a 61 74 69 6f 6e 20 69 73 20 75 73 65 64 29 2c zation is used),
2b250 0a 2a 2a 20 20 20 2a 20 61 6c 6c 20 64 69 72 74 .** * all dirt
2b260 79 20 70 61 67 65 73 20 61 72 65 20 77 72 69 74 y pages are writ
2b270 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 ten to the datab
2b280 61 73 65 20 66 69 6c 65 2c 20 0a 2a 2a 20 20 20 ase file, .**
2b290 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 * the database f
2b2a0 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74 65 64 ile is truncated
2b2b0 20 28 69 66 20 72 65 71 75 69 72 65 64 29 2c 20 (if required),
2b2c0 61 6e 64 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 and.** * the d
2b2d0 61 74 61 62 61 73 65 20 66 69 6c 65 20 73 79 6e atabase file syn
2b2e0 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ced. .**.** The
2b2f0 6f 6e 6c 79 20 74 68 69 6e 67 20 74 68 61 74 20 only thing that
2b300 72 65 6d 61 69 6e 73 20 74 6f 20 63 6f 6d 6d 69 remains to commi
2b310 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f t the transactio
2b320 6e 20 69 73 20 74 6f 20 66 69 6e 61 6c 69 7a 65 n is to finalize
2b330 20 0a 2a 2a 20 28 64 65 6c 65 74 65 2c 20 74 72 .** (delete, tr
2b340 75 6e 63 61 74 65 20 6f 72 20 7a 65 72 6f 20 74 uncate or zero t
2b350 68 65 20 66 69 72 73 74 20 70 61 72 74 20 6f 66 he first part of
2b360 29 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 ) the journal fi
2b370 6c 65 20 28 6f 72 20 0a 2a 2a 20 64 65 6c 65 74 le (or .** delet
2b380 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 e the master jou
2b390 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 73 70 65 rnal file if spe
2b3a0 63 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 4e cified)..**.** N
2b3b0 6f 74 65 20 74 68 61 74 20 69 66 20 7a 4d 61 73 ote that if zMas
2b3c0 74 65 72 3d 3d 4e 55 4c 4c 2c 20 74 68 69 73 20 ter==NULL, this
2b3d0 64 6f 65 73 20 6e 6f 74 20 6f 76 65 72 77 72 69 does not overwri
2b3e0 74 65 20 61 20 70 72 65 76 69 6f 75 73 20 76 61 te a previous va
2b3f0 6c 75 65 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f lue.** passed to
2b400 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 an sqlite3Pager
2b410 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 CommitPhaseOne()
2b420 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 call..**.** If
2b430 74 68 65 20 66 69 6e 61 6c 20 70 61 72 61 6d 65 the final parame
2b440 74 65 72 20 2d 20 6e 6f 53 79 6e 63 20 2d 20 69 ter - noSync - i
2b450 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 s true, then the
2b460 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
2b470 74 73 65 6c 66 0a 2a 2a 20 69 73 20 6e 6f 74 20 tself.** is not
2b480 73 79 6e 63 65 64 2e 20 54 68 65 20 63 61 6c 6c synced. The call
2b490 65 72 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c er must call sql
2b4a0 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 29 20 ite3PagerSync()
2b4b0 64 69 72 65 63 74 6c 79 20 74 6f 0a 2a 2a 20 73 directly to.** s
2b4c0 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 ync the database
2b4d0 20 66 69 6c 65 20 62 65 66 6f 72 65 20 63 61 6c file before cal
2b4e0 6c 69 6e 67 20 43 6f 6d 6d 69 74 50 68 61 73 65 ling CommitPhase
2b4f0 54 77 6f 28 29 20 74 6f 20 64 65 6c 65 74 65 20 Two() to delete
2b500 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 the.** journal f
2b510 69 6c 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 ile in this case
2b520 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
2b530 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 PagerCommitPhase
2b540 4f 6e 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 One(. Pager *pP
2b550 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 ager,
2b560 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 /* Pager
2b570 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 object */. cons
2b580 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c t char *zMaster,
2b590 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
2b5a0 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 20 f not NULL, the
2b5b0 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
2b5c0 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 53 ame */. int noS
2b5d0 79 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20 ync
2b5e0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
2b5f0 20 74 6f 20 6f 6d 69 74 20 74 68 65 20 78 53 79 to omit the xSy
2b600 6e 63 20 6f 6e 20 74 68 65 20 64 62 20 66 69 6c nc on the db fil
2b610 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
2b620 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
2b630 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
2b640 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f urn code */.. /
2b650 2a 20 54 68 65 20 64 62 4f 72 69 67 53 69 7a 65 * The dbOrigSize
2b660 20 69 73 20 6e 65 76 65 72 20 73 65 74 20 69 66 is never set if
2b670 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 journal_mode=OF
2b680 46 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 F */. assert( p
2b690 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
2b6a0 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de!=PAGER_JOURNA
2b6b0 4c 4d 4f 44 45 5f 4f 46 46 20 7c 7c 20 70 50 61 LMODE_OFF || pPa
2b6c0 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3d ger->dbOrigSize=
2b6d0 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 =0 );.. /* If a
2b6e0 20 70 72 69 6f 72 20 65 72 72 6f 72 20 6f 63 63 prior error occ
2b6f0 75 72 72 65 64 2c 20 74 68 69 73 20 72 6f 75 74 urred, this rout
2b700 69 6e 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 ine should not b
2b710 65 20 63 61 6c 6c 65 64 2e 20 20 52 4f 4c 4c 42 e called. ROLLB
2b720 41 43 4b 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 ACK. ** is the
2b730 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 70 appropriate resp
2b740 6f 6e 73 65 20 74 6f 20 61 6e 20 65 72 72 6f 72 onse to an error
2b750 2c 20 6e 6f 74 20 43 4f 4d 4d 49 54 2e 20 20 47 , not COMMIT. G
2b760 75 61 72 64 20 61 67 61 69 6e 73 74 0a 20 20 2a uard against. *
2b770 2a 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 73 20 * coding errors
2b780 62 79 20 72 65 70 65 61 74 69 6e 67 20 74 68 65 by repeating the
2b790 20 70 72 69 6f 72 20 65 72 72 6f 72 2e 20 2a 2f prior error. */
2b7a0 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 . if( NEVER(pPa
2b7b0 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 ger->errCode) )
2b7c0 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 return pPager->e
2b7d0 72 72 43 6f 64 65 3b 0a 0a 20 20 50 41 47 45 52 rrCode;.. PAGER
2b7e0 54 52 41 43 45 28 28 22 44 41 54 41 42 41 53 45 TRACE(("DATABASE
2b7f0 20 53 59 4e 43 3a 20 46 69 6c 65 3d 25 73 20 7a SYNC: File=%s z
2b800 4d 61 73 74 65 72 3d 25 73 20 6e 53 69 7a 65 3d Master=%s nSize=
2b810 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 70 50 %d\n", . pP
2b820 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c ager->zFilename,
2b830 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72 zMaster, pPager
2b840 2d 3e 64 62 53 69 7a 65 29 29 3b 0a 0a 20 20 69 ->dbSize));.. i
2b850 66 28 20 4d 45 4d 44 42 20 26 26 20 70 50 61 67 f( MEMDB && pPag
2b860 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 er->dbModified )
2b870 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 {. /* If this
2b880 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 is an in-memory
2b890 20 64 62 2c 20 6f 72 20 6e 6f 20 70 61 67 65 73 db, or no pages
2b8a0 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 have been writt
2b8b0 65 6e 20 74 6f 2c 20 6f 72 20 74 68 69 73 0a 20 en to, or this.
2b8c0 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 ** function h
2b8d0 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
2b8e0 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 6d 6f called, it is mo
2b8f0 73 74 6c 79 20 61 20 6e 6f 2d 6f 70 2e 20 20 48 stly a no-op. H
2b900 6f 77 65 76 65 72 2c 20 61 6e 79 0a 20 20 20 20 owever, any.
2b910 2a 2a 20 62 61 63 6b 75 70 20 69 6e 20 70 72 6f ** backup in pro
2b920 67 72 65 73 73 20 6e 65 65 64 73 20 74 6f 20 62 gress needs to b
2b930 65 20 72 65 73 74 61 72 74 65 64 2e 0a 20 20 20 e restarted..
2b940 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 */. sqlite3B
2b950 61 63 6b 75 70 52 65 73 74 61 72 74 28 70 50 61 ackupRestart(pPa
2b960 67 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b 0a 20 ger->pBackup);.
2b970 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 }else if( pPage
2b980 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f r->state!=PAGER_
2b990 53 59 4e 43 45 44 20 26 26 20 70 50 61 67 65 72 SYNCED && pPager
2b9a0 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 7b 0a ->dbModified ){.
2b9b0 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c . /* The foll
2b9c0 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 75 70 64 61 owing block upda
2b9d0 74 65 73 20 74 68 65 20 63 68 61 6e 67 65 2d 63 tes the change-c
2b9e0 6f 75 6e 74 65 72 2e 20 45 78 61 63 74 6c 79 20 ounter. Exactly
2b9f0 68 6f 77 20 69 74 0a 20 20 20 20 2a 2a 20 64 6f how it. ** do
2ba00 65 73 20 74 68 69 73 20 64 65 70 65 6e 64 73 20 es this depends
2ba10 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f on whether or no
2ba20 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 75 70 64 t the atomic-upd
2ba30 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ate optimization
2ba40 0a 20 20 20 20 2a 2a 20 77 61 73 20 65 6e 61 62 . ** was enab
2ba50 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 led at compile t
2ba60 69 6d 65 2c 20 61 6e 64 20 69 66 20 74 68 69 73 ime, and if this
2ba70 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 65 65 transaction mee
2ba80 74 73 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 ts the . ** r
2ba90 75 6e 74 69 6d 65 20 63 72 69 74 65 72 69 61 20 untime criteria
2baa0 74 6f 20 75 73 65 20 74 68 65 20 6f 70 65 72 61 to use the opera
2bab0 74 69 6f 6e 3a 20 0a 20 20 20 20 2a 2a 0a 20 20 tion: . **.
2bac0 20 20 2a 2a 20 20 20 20 2a 20 54 68 65 20 66 69 ** * The fi
2bad0 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 70 6f 72 le-system suppor
2bae0 74 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 ts the atomic-wr
2baf0 69 74 65 20 70 72 6f 70 65 72 74 79 20 66 6f 72 ite property for
2bb00 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 62 6c 6f . ** blo
2bb10 63 6b 73 20 6f 66 20 73 69 7a 65 20 70 61 67 65 cks of size page
2bb20 2d 73 69 7a 65 2c 20 61 6e 64 20 0a 20 20 20 20 -size, and .
2bb30 2a 2a 20 20 20 20 2a 20 54 68 69 73 20 63 6f 6d ** * This com
2bb40 6d 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 mit is not part
2bb50 6f 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c 65 20 of a multi-file
2bb60 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 transaction, and
2bb70 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20 45 78 61 . ** * Exa
2bb80 63 74 6c 79 20 6f 6e 65 20 70 61 67 65 20 68 61 ctly one page ha
2bb90 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 s been modified
2bba0 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 74 68 65 and store in the
2bbb0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 journal file..
2bbc0 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 **. ** If
2bbd0 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e the optimization
2bbe0 20 77 61 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64 was not enabled
2bbf0 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 at compile time
2bc00 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20 2a , then the. *
2bc10 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 * pager_incr_cha
2bc20 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 66 75 6e ngecounter() fun
2bc30 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
2bc40 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68 to update the ch
2bc50 61 6e 67 65 0a 20 20 20 20 2a 2a 20 63 6f 75 6e ange. ** coun
2bc60 74 65 72 20 69 6e 20 27 69 6e 64 69 72 65 63 74 ter in 'indirect
2bc70 2d 6d 6f 64 65 27 2e 20 49 66 20 74 68 65 20 6f -mode'. If the o
2bc80 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 63 ptimization is c
2bc90 6f 6d 70 69 6c 65 64 20 69 6e 20 62 75 74 0a 20 ompiled in but.
2bca0 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61 70 70 ** is not app
2bcb0 6c 69 63 61 62 6c 65 20 74 6f 20 74 68 69 73 20 licable to this
2bcc0 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 63 61 6c transaction, cal
2bcd0 6c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c l sqlite3Journal
2bce0 43 72 65 61 74 65 28 29 0a 20 20 20 20 2a 2a 20 Create(). **
2bcf0 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 to make sure the
2bd00 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 journal file ha
2bd10 73 20 61 63 74 75 61 6c 6c 79 20 62 65 65 6e 20 s actually been
2bd20 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20 63 61 created, then ca
2bd30 6c 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f ll. ** pager_
2bd40 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 incr_changecount
2bd50 65 72 28 29 20 74 6f 20 75 70 64 61 74 65 20 74 er() to update t
2bd60 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 he change-counte
2bd70 72 20 69 6e 20 69 6e 64 69 72 65 63 74 0a 20 20 r in indirect.
2bd80 20 20 2a 2a 20 6d 6f 64 65 2e 20 0a 20 20 20 20 ** mode. .
2bd90 2a 2a 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 **. ** Otherw
2bda0 69 73 65 2c 20 69 66 20 74 68 65 20 6f 70 74 69 ise, if the opti
2bdb0 6d 69 7a 61 74 69 6f 6e 20 69 73 20 62 6f 74 68 mization is both
2bdc0 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 61 70 70 enabled and app
2bdd0 6c 69 63 61 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 licable,. **
2bde0 74 68 65 6e 20 63 61 6c 6c 20 70 61 67 65 72 5f then call pager_
2bdf0 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 incr_changecount
2be00 65 72 28 29 20 74 6f 20 75 70 64 61 74 65 20 74 er() to update t
2be10 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 he change-counte
2be20 72 0a 20 20 20 20 2a 2a 20 69 6e 20 27 64 69 72 r. ** in 'dir
2be30 65 63 74 27 20 6d 6f 64 65 2e 20 49 6e 20 74 68 ect' mode. In th
2be40 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f 75 72 is case the jour
2be50 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 6e 65 nal file will ne
2be60 76 65 72 20 62 65 0a 20 20 20 20 2a 2a 20 63 72 ver be. ** cr
2be70 65 61 74 65 64 20 66 6f 72 20 74 68 69 73 20 74 eated for this t
2be80 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 ransaction..
2be90 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
2bea0 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 _ENABLE_ATOMIC_W
2beb0 52 49 54 45 0a 20 20 20 20 50 67 48 64 72 20 2a RITE. PgHdr *
2bec0 70 50 67 3b 0a 20 20 20 20 61 73 73 65 72 74 28 pPg;. assert(
2bed0 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
2bee0 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e jfd) || pPager->
2bef0 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 journalMode==PAG
2bf00 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f ER_JOURNALMODE_O
2bf10 46 46 20 29 3b 0a 20 20 20 20 69 66 28 20 21 7a FF );. if( !z
2bf20 4d 61 73 74 65 72 20 26 26 20 69 73 4f 70 65 6e Master && isOpen
2bf30 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 0a 20 (pPager->jfd) .
2bf40 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a && pPager->j
2bf50 6f 75 72 6e 61 6c 4f 66 66 3d 3d 6a 72 6e 6c 42 ournalOff==jrnlB
2bf60 75 66 66 65 72 53 69 7a 65 28 70 50 61 67 65 72 ufferSize(pPager
2bf70 29 20 0a 20 20 20 20 20 26 26 20 70 50 61 67 65 ) . && pPage
2bf80 72 2d 3e 64 62 53 69 7a 65 3e 3d 70 50 61 67 65 r->dbSize>=pPage
2bf90 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 0a 20 20 r->dbFileSize.
2bfa0 20 20 20 26 26 20 28 30 3d 3d 28 70 50 67 20 3d && (0==(pPg =
2bfb0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 sqlite3PcacheDi
2bfc0 72 74 79 4c 69 73 74 28 70 50 61 67 65 72 2d 3e rtyList(pPager->
2bfd0 70 50 43 61 63 68 65 29 29 20 7c 7c 20 30 3d 3d pPCache)) || 0==
2bfe0 70 50 67 2d 3e 70 44 69 72 74 79 29 0a 20 20 20 pPg->pDirty).
2bff0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 ){. /* Upd
2c000 61 74 65 20 74 68 65 20 64 62 20 66 69 6c 65 20 ate the db file
2c010 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 76 change counter v
2c020 69 61 20 74 68 65 20 64 69 72 65 63 74 2d 77 72 ia the direct-wr
2c030 69 74 65 20 6d 65 74 68 6f 64 2e 20 54 68 65 20 ite method. The
2c040 0a 20 20 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 . ** follow
2c050 69 6e 67 20 63 61 6c 6c 20 77 69 6c 6c 20 6d 6f ing call will mo
2c060 64 69 66 79 20 74 68 65 20 69 6e 2d 6d 65 6d 6f dify the in-memo
2c070 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ry representatio
2c080 6e 20 6f 66 20 70 61 67 65 20 31 20 0a 20 20 20 n of page 1 .
2c090 20 20 20 2a 2a 20 74 6f 20 69 6e 63 6c 75 64 65 ** to include
2c0a0 20 74 68 65 20 75 70 64 61 74 65 64 20 63 68 61 the updated cha
2c0b0 6e 67 65 20 63 6f 75 6e 74 65 72 20 61 6e 64 20 nge counter and
2c0c0 74 68 65 6e 20 77 72 69 74 65 20 70 61 67 65 20 then write page
2c0d0 31 20 0a 20 20 20 20 20 20 2a 2a 20 64 69 72 65 1 . ** dire
2c0e0 63 74 6c 79 20 74 6f 20 74 68 65 20 64 61 74 61 ctly to the data
2c0f0 62 61 73 65 20 66 69 6c 65 2e 20 42 65 63 61 75 base file. Becau
2c100 73 65 20 6f 66 20 74 68 65 20 61 74 6f 6d 69 63 se of the atomic
2c110 2d 77 72 69 74 65 20 0a 20 20 20 20 20 20 2a 2a -write . **
2c120 20 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65 property of the
2c130 20 68 6f 73 74 20 66 69 6c 65 2d 73 79 73 74 65 host file-syste
2c140 6d 2c 20 74 68 69 73 20 69 73 20 73 61 66 65 2e m, this is safe.
2c150 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
2c160 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f rc = pager_incr_
2c170 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 changecounter(pP
2c180 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 7d 65 ager, 1);. }e
2c190 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
2c1a0 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 sqlite3JournalCr
2c1b0 65 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 eate(pPager->jfd
2c1c0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
2c1d0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
2c1e0 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
2c1f0 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e _incr_changecoun
2c200 74 65 72 28 70 50 61 67 65 72 2c 20 30 29 3b 0a ter(pPager, 0);.
2c210 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 }. }.#e
2c220 6c 73 65 0a 20 20 20 20 72 63 20 3d 20 70 61 67 lse. rc = pag
2c230 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f er_incr_changeco
2c240 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 30 29 unter(pPager, 0)
2c250 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 ;.#endif. if(
2c260 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
2c270 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 goto commit_pha
2c280 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 20 se_one_exit;..
2c290 20 20 2f 2a 20 49 66 20 74 68 69 73 20 74 72 61 /* If this tra
2c2a0 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6d 61 64 nsaction has mad
2c2b0 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 e the database s
2c2c0 6d 61 6c 6c 65 72 2c 20 74 68 65 6e 20 61 6c 6c maller, then all
2c2d0 20 70 61 67 65 73 0a 20 20 20 20 2a 2a 20 62 65 pages. ** be
2c2e0 69 6e 67 20 64 69 73 63 61 72 64 65 64 20 62 79 ing discarded by
2c2f0 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f 6e 20 the truncation
2c300 6d 75 73 74 20 62 65 20 77 72 69 74 74 65 6e 20 must be written
2c310 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 to the journal.
2c320 20 20 20 2a 2a 20 66 69 6c 65 2e 20 54 68 69 73 ** file. This
2c330 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e can only happen
2c340 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 in auto-vacuum
2c350 6d 6f 64 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 mode.. **.
2c360 20 2a 2a 20 42 65 66 6f 72 65 20 72 65 61 64 69 ** Before readi
2c370 6e 67 20 74 68 65 20 70 61 67 65 73 20 77 69 74 ng the pages wit
2c380 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 6c h page numbers l
2c390 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 0a arger than the .
2c3a0 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 76 ** current v
2c3b0 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62 alue of Pager.db
2c3c0 53 69 7a 65 2c 20 73 65 74 20 64 62 53 69 7a 65 Size, set dbSize
2c3d0 20 62 61 63 6b 20 74 6f 20 74 68 65 20 76 61 6c back to the val
2c3e0 75 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 69 ue. ** that i
2c3f0 74 20 74 6f 6f 6b 20 61 74 20 74 68 65 20 73 74 t took at the st
2c400 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 art of the trans
2c410 61 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 69 73 action. Otherwis
2c420 65 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 e, the. ** ca
2c430 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 50 61 lls to sqlite3Pa
2c440 67 65 72 47 65 74 28 29 20 72 65 74 75 72 6e 20 gerGet() return
2c450 7a 65 72 6f 65 64 20 70 61 67 65 73 20 69 6e 73 zeroed pages ins
2c460 74 65 61 64 20 6f 66 20 0a 20 20 20 20 2a 2a 20 tead of . **
2c470 72 65 61 64 69 6e 67 20 64 61 74 61 20 66 72 6f reading data fro
2c480 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 m the database f
2c490 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ile.. **.
2c4a0 2a 2a 20 57 68 65 6e 20 6a 6f 75 72 6e 61 6c 5f ** When journal_
2c4b0 6d 6f 64 65 3d 3d 4f 46 46 20 74 68 65 20 64 62 mode==OFF the db
2c4c0 4f 72 69 67 53 69 7a 65 20 69 73 20 61 6c 77 61 OrigSize is alwa
2c4d0 79 73 20 7a 65 72 6f 2c 20 73 6f 20 74 68 69 73 ys zero, so this
2c4e0 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 6e 65 . ** block ne
2c4f0 76 65 72 20 72 75 6e 73 20 69 66 20 6a 6f 75 72 ver runs if jour
2c500 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46 2e 0a 20 20 nal_mode=OFF..
2c510 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c */.#ifndef SQL
2c520 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
2c530 55 55 4d 0a 20 20 20 20 69 66 28 20 70 50 61 67 UUM. if( pPag
2c540 65 72 2d 3e 64 62 53 69 7a 65 3c 70 50 61 67 65 er->dbSize<pPage
2c550 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 0a 20 r->dbOrigSize .
2c560 20 20 20 20 26 26 20 41 4c 57 41 59 53 28 70 50 && ALWAYS(pP
2c570 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
2c580 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e!=PAGER_JOURNAL
2c590 4d 4f 44 45 5f 4f 46 46 29 0a 20 20 20 20 29 7b MODE_OFF). ){
2c5a0 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 3b 20 20 . Pgno i;
2c5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2c5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2c5d0 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 /* Iterator var
2c5e0 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 63 iable */. c
2c5f0 6f 6e 73 74 20 50 67 6e 6f 20 69 53 6b 69 70 20 onst Pgno iSkip
2c600 3d 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 = PAGER_MJ_PGNO(
2c610 70 50 61 67 65 72 29 3b 20 2f 2a 20 50 65 6e 64 pPager); /* Pend
2c620 69 6e 67 20 6c 6f 63 6b 20 70 61 67 65 20 2a 2f ing lock page */
2c630 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e . const Pgn
2c640 6f 20 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65 o dbSize = pPage
2c650 72 2d 3e 64 62 53 69 7a 65 3b 20 20 20 20 20 20 r->dbSize;
2c660 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6d 61 /* Database ima
2c670 67 65 20 73 69 7a 65 20 2a 2f 20 0a 20 20 20 20 ge size */ .
2c680 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
2c690 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 = pPager->dbOri
2c6a0 67 53 69 7a 65 3b 0a 20 20 20 20 20 20 66 6f 72 gSize;. for
2c6b0 28 20 69 3d 64 62 53 69 7a 65 2b 31 3b 20 69 3c ( i=dbSize+1; i<
2c6c0 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 =pPager->dbOrigS
2c6d0 69 7a 65 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 ize; i++ ){.
2c6e0 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 if( !sqlite3
2c6f0 42 69 74 76 65 63 54 65 73 74 28 70 50 61 67 65 BitvecTest(pPage
2c700 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 69 r->pInJournal, i
2c710 29 20 26 26 20 69 21 3d 69 53 6b 69 70 20 29 7b ) && i!=iSkip ){
2c720 0a 20 20 20 20 20 20 20 20 20 20 50 67 48 64 72 . PgHdr
2c730 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 *pPage;
2c740 20 20 20 20 20 2f 2a 20 50 61 67 65 20 74 6f 20 /* Page to
2c750 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 20 journal */.
2c760 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
2c770 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 3PagerGet(pPager
2c780 2c 20 69 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 , i, &pPage);.
2c790 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
2c7a0 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
2c7b0 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e commit_phase_on
2c7c0 65 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 e_exit;.
2c7d0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
2c7e0 67 65 72 57 72 69 74 65 28 70 50 61 67 65 29 3b gerWrite(pPage);
2c7f0 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
2c800 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 e3PagerUnref(pPa
2c810 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ge);. i
2c820 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
2c830 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 ) goto commit_p
2c840 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 hase_one_exit;.
2c850 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
2c860 20 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e . pPager->
2c870 64 62 53 69 7a 65 20 3d 20 64 62 53 69 7a 65 3b dbSize = dbSize;
2c880 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 . }.#endif..
2c890 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 /* Write the
2c8a0 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
2c8b0 61 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 ame into the jou
2c8c0 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 20 rnal file. If a
2c8d0 6d 61 73 74 65 72 20 0a 20 20 20 20 2a 2a 20 6a master . ** j
2c8e0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
2c8f0 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
2c900 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 n written to the
2c910 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 0a journal file, .
2c920 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 7a 4d 61 ** or if zMa
2c930 73 74 65 72 20 69 73 20 4e 55 4c 4c 20 28 6e 6f ster is NULL (no
2c940 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 29 master journal)
2c950 2c 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c 6c , then this call
2c960 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 is a no-op..
2c970 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 77 72 69 */. rc = wri
2c980 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 teMasterJournal(
2c990 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 pPager, zMaster)
2c9a0 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
2c9b0 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 LITE_OK ) goto c
2c9c0 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f ommit_phase_one_
2c9d0 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 53 79 exit;.. /* Sy
2c9e0 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 nc the journal f
2c9f0 69 6c 65 2e 20 49 66 20 74 68 65 20 61 74 6f 6d ile. If the atom
2ca00 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69 6d 69 ic-update optimi
2ca10 7a 61 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 0a zation is being.
2ca20 20 20 20 20 2a 2a 20 75 73 65 64 2c 20 74 68 69 ** used, thi
2ca30 73 20 63 61 6c 6c 20 77 69 6c 6c 20 6e 6f 74 20 s call will not
2ca40 63 72 65 61 74 65 20 74 68 65 20 6a 6f 75 72 6e create the journ
2ca50 61 6c 20 66 69 6c 65 20 6f 72 20 70 65 72 66 6f al file or perfo
2ca60 72 6d 20 61 6e 79 0a 20 20 20 20 2a 2a 20 72 65 rm any. ** re
2ca70 61 6c 20 49 4f 2e 0a 20 20 20 20 2a 2f 0a 20 20 al IO.. */.
2ca80 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e rc = syncJourn
2ca90 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 al(pPager);.
2caa0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
2cab0 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f K ) goto commit_
2cac0 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a phase_one_exit;.
2cad0 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61 6c . /* Write al
2cae0 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 74 6f l dirty pages to
2caf0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
2cb00 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 le. */. rc =
2cb10 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 pager_write_page
2cb20 6c 69 73 74 28 73 71 6c 69 74 65 33 50 63 61 63 list(sqlite3Pcac
2cb30 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 61 67 heDirtyList(pPag
2cb40 65 72 2d 3e 70 50 43 61 63 68 65 29 29 3b 0a 20 er->pPCache));.
2cb50 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
2cb60 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 E_OK ){. as
2cb70 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
2cb80 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 _IOERR_BLOCKED )
2cb90 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 63 6f 6d ;. goto com
2cba0 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 mit_phase_one_ex
2cbb0 69 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 it;. }. sq
2cbc0 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e lite3PcacheClean
2cbd0 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 All(pPager->pPCa
2cbe0 63 68 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 che);.. /* If
2cbf0 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 the file on dis
2cc00 6b 20 69 73 20 6e 6f 74 20 74 68 65 20 73 61 6d k is not the sam
2cc10 65 20 73 69 7a 65 20 61 73 20 74 68 65 20 64 61 e size as the da
2cc20 74 61 62 61 73 65 20 69 6d 61 67 65 2c 0a 20 20 tabase image,.
2cc30 20 20 2a 2a 20 74 68 65 6e 20 75 73 65 20 70 61 ** then use pa
2cc40 67 65 72 5f 74 72 75 6e 63 61 74 65 20 74 6f 20 ger_truncate to
2cc50 67 72 6f 77 20 6f 72 20 73 68 72 69 6e 6b 20 74 grow or shrink t
2cc60 68 65 20 66 69 6c 65 20 68 65 72 65 2e 0a 20 20 he file here..
2cc70 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 */. if( pPa
2cc80 67 65 72 2d 3e 64 62 53 69 7a 65 21 3d 70 50 61 ger->dbSize!=pPa
2cc90 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 ger->dbFileSize
2cca0 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6e 4e ){. Pgno nN
2ccb0 65 77 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 ew = pPager->dbS
2ccc0 69 7a 65 20 2d 20 28 70 50 61 67 65 72 2d 3e 64 ize - (pPager->d
2ccd0 62 53 69 7a 65 3d 3d 50 41 47 45 52 5f 4d 4a 5f bSize==PAGER_MJ_
2cce0 50 47 4e 4f 28 70 50 61 67 65 72 29 29 3b 0a 20 PGNO(pPager));.
2ccf0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
2cd00 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
2cd10 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 R_EXCLUSIVE );.
2cd20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
2cd30 74 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c truncate(pPager,
2cd40 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20 20 69 66 nNew);. if
2cd50 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
2cd60 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 ) goto commit_ph
2cd70 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 ase_one_exit;.
2cd80 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 61 }.. /* Fina
2cd90 6c 6c 79 2c 20 73 79 6e 63 20 74 68 65 20 64 61 lly, sync the da
2cda0 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a tabase file. */.
2cdb0 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d if( !pPager-
2cdc0 3e 6e 6f 53 79 6e 63 20 26 26 20 21 6e 6f 53 79 >noSync && !noSy
2cdd0 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d nc ){. rc =
2cde0 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 sqlite3OsSync(p
2cdf0 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 Pager->fd, pPage
2ce00 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a r->sync_flags);.
2ce10 20 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 41 43 }. IOTRAC
2ce20 45 28 28 22 44 42 53 59 4e 43 20 25 70 5c 6e 22 E(("DBSYNC %p\n"
2ce30 2c 20 70 50 61 67 65 72 29 29 0a 0a 20 20 20 20 , pPager))..
2ce40 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 pPager->state =
2ce50 50 41 47 45 52 5f 53 59 4e 43 45 44 3b 0a 20 20 PAGER_SYNCED;.
2ce60 7d 0a 0a 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f }..commit_phase_
2ce70 6f 6e 65 5f 65 78 69 74 3a 0a 20 20 72 65 74 75 one_exit:. retu
2ce80 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rn rc;.}.../*.**
2ce90 20 57 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 When this funct
2cea0 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 ion is called, t
2ceb0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
2cec0 20 68 61 73 20 62 65 65 6e 20 63 6f 6d 70 6c 65 has been comple
2ced0 74 65 6c 79 0a 2a 2a 20 75 70 64 61 74 65 64 20 tely.** updated
2cee0 74 6f 20 72 65 66 6c 65 63 74 20 74 68 65 20 63 to reflect the c
2cef0 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 hanges made by t
2cf00 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 he current trans
2cf10 61 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 73 79 action and.** sy
2cf20 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 54 68 nced to disk. Th
2cf30 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 e journal file s
2cf40 74 69 6c 6c 20 65 78 69 73 74 73 20 69 6e 20 74 till exists in t
2cf50 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 0a he file-system .
2cf60 2a 2a 20 74 68 6f 75 67 68 2c 20 61 6e 64 20 69 ** though, and i
2cf70 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75 f a failure occu
2cf80 72 73 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 rs at this point
2cf90 20 69 74 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 it will eventua
2cfa0 6c 6c 79 0a 2a 2a 20 62 65 20 75 73 65 64 20 61 lly.** be used a
2cfb0 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 s a hot-journal
2cfc0 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 and the current
2cfd0 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c transaction roll
2cfe0 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 ed back..**.** T
2cff0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e his function fin
2d000 61 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e alizes the journ
2d010 61 6c 20 66 69 6c 65 2c 20 65 69 74 68 65 72 20 al file, either
2d020 62 79 20 64 65 6c 65 74 69 6e 67 2c 20 0a 2a 2a by deleting, .**
2d030 20 74 72 75 6e 63 61 74 69 6e 67 20 6f 72 20 70 truncating or p
2d040 61 72 74 69 61 6c 6c 79 20 7a 65 72 6f 69 6e 67 artially zeroing
2d050 20 69 74 2c 20 73 6f 20 74 68 61 74 20 69 74 20 it, so that it
2d060 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 0a cannot be used .
2d070 2a 2a 20 66 6f 72 20 68 6f 74 2d 6a 6f 75 72 6e ** for hot-journ
2d080 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e 20 4f 6e 63 al rollback. Onc
2d090 65 20 74 68 69 73 20 69 73 20 64 6f 6e 65 20 74 e this is done t
2d0a0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 he transaction i
2d0b0 73 0a 2a 2a 20 69 72 72 65 76 6f 63 61 62 6c 79 s.** irrevocably
2d0c0 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a committed..**.*
2d0d0 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 * If an error oc
2d0e0 63 75 72 73 2c 20 61 6e 20 49 4f 20 65 72 72 6f curs, an IO erro
2d0f0 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
2d100 65 64 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 ed and the pager
2d110 0a 2a 2a 20 6d 6f 76 65 73 20 69 6e 74 6f 20 74 .** moves into t
2d120 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 he error state.
2d130 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 Otherwise, SQLIT
2d140 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
2d150 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
2d160 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 PagerCommitPhase
2d170 54 77 6f 28 50 61 67 65 72 20 2a 70 50 61 67 65 Two(Pager *pPage
2d180 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 r){. int rc = S
2d190 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 QLITE_OK;
2d1a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
2d1b0 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 turn code */..
2d1c0 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 /* This routine
2d1d0 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 should not be ca
2d1e0 6c 6c 65 64 20 69 66 20 61 20 70 72 69 6f 72 20 lled if a prior
2d1f0 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 error has occurr
2d200 65 64 2e 0a 20 20 2a 2a 20 42 75 74 20 69 66 20 ed.. ** But if
2d210 28 64 75 65 20 74 6f 20 61 20 63 6f 64 69 6e 67 (due to a coding
2d220 20 65 72 72 6f 72 20 65 6c 73 65 77 68 65 72 65 error elsewhere
2d230 20 69 6e 20 74 68 65 20 73 79 73 74 65 6d 29 20 in the system)
2d240 69 74 20 64 6f 65 73 20 67 65 74 0a 20 20 2a 2a it does get. **
2d250 20 63 61 6c 6c 65 64 2c 20 6a 75 73 74 20 72 65 called, just re
2d260 74 75 72 6e 20 74 68 65 20 73 61 6d 65 20 65 72 turn the same er
2d270 72 6f 72 20 63 6f 64 65 20 77 69 74 68 6f 75 74 ror code without
2d280 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e doing anything.
2d290 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 */. if( NEVER(
2d2a0 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 pPager->errCode)
2d2b0 20 29 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 ) return pPager
2d2c0 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a ->errCode;.. /*
2d2d0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 This function s
2d2e0 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c hould not be cal
2d2f0 6c 65 64 20 69 66 20 74 68 65 20 70 61 67 65 72 led if the pager
2d300 20 69 73 20 6e 6f 74 20 69 6e 20 61 74 20 6c 65 is not in at le
2d310 61 73 74 0a 20 20 2a 2a 20 50 41 47 45 52 5f 52 ast. ** PAGER_R
2d320 45 53 45 52 56 45 44 20 73 74 61 74 65 2e 20 41 ESERVED state. A
2d330 6e 64 20 69 6e 64 65 65 64 20 53 51 4c 69 74 65 nd indeed SQLite
2d340 20 6e 65 76 65 72 20 64 6f 65 73 20 74 68 69 73 never does this
2d350 2e 20 42 75 74 20 69 74 20 69 73 0a 20 20 2a 2a . But it is. **
2d360 20 6e 69 63 65 20 74 6f 20 68 61 76 65 20 74 68 nice to have th
2d370 69 73 20 64 65 66 65 6e 73 69 76 65 20 74 65 73 is defensive tes
2d380 74 20 68 65 72 65 20 61 6e 79 77 61 79 2e 0a 20 t here anyway..
2d390 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 */. if( NEVER(
2d3a0 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 50 41 pPager->state<PA
2d3b0 47 45 52 5f 52 45 53 45 52 56 45 44 29 20 29 20 GER_RESERVED) )
2d3c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
2d3d0 52 4f 52 3b 0a 0a 20 20 2f 2a 20 41 6e 20 6f 70 ROR;.. /* An op
2d3e0 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 49 66 20 74 timization. If t
2d3f0 68 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20 he database was
2d400 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 6d 6f 64 not actually mod
2d410 69 66 69 65 64 20 64 75 72 69 6e 67 0a 20 20 2a ified during. *
2d420 2a 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 * this transacti
2d430 6f 6e 2c 20 74 68 65 20 70 61 67 65 72 20 69 73 on, the pager is
2d440 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c running in excl
2d450 75 73 69 76 65 2d 6d 6f 64 65 20 61 6e 64 20 69 usive-mode and i
2d460 73 0a 20 20 2a 2a 20 75 73 69 6e 67 20 70 65 72 s. ** using per
2d470 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 73 sistent journals
2d480 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 , then this func
2d490 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tion is a no-op.
2d4a0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 . **. ** The s
2d4b0 74 61 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 tart of the jour
2d4c0 6e 61 6c 20 66 69 6c 65 20 63 75 72 72 65 6e 74 nal file current
2d4d0 6c 79 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 ly contains a si
2d4e0 6e 67 6c 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 ngle journal .
2d4f0 2a 2a 20 68 65 61 64 65 72 20 77 69 74 68 20 74 ** header with t
2d500 68 65 20 6e 52 65 63 20 66 69 65 6c 64 20 73 65 he nRec field se
2d510 74 20 74 6f 20 30 2e 20 49 66 20 73 75 63 68 20 t to 0. If such
2d520 61 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75 73 65 a journal is use
2d530 64 20 61 73 0a 20 20 2a 2a 20 61 20 68 6f 74 2d d as. ** a hot-
2d540 6a 6f 75 72 6e 61 6c 20 64 75 72 69 6e 67 20 68 journal during h
2d550 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 ot-journal rollb
2d560 61 63 6b 2c 20 30 20 63 68 61 6e 67 65 73 20 77 ack, 0 changes w
2d570 69 6c 6c 20 62 65 20 6d 61 64 65 0a 20 20 2a 2a ill be made. **
2d580 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
2d590 20 66 69 6c 65 2e 20 53 6f 20 74 68 65 72 65 20 file. So there
2d5a0 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 7a 65 is no need to ze
2d5b0 72 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a ro the journal .
2d5c0 20 20 2a 2a 20 68 65 61 64 65 72 2e 20 53 69 6e ** header. Sin
2d5d0 63 65 20 74 68 65 20 70 61 67 65 72 20 69 73 20 ce the pager is
2d5e0 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 in exclusive mod
2d5f0 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e e, there is no n
2d600 65 65 64 0a 20 20 2a 2a 20 74 6f 20 64 72 6f 70 eed. ** to drop
2d610 20 61 6e 79 20 6c 6f 63 6b 73 20 65 69 74 68 65 any locks eithe
2d620 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 r.. */. if( pP
2d630 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 ager->dbModified
2d640 3d 3d 30 20 26 26 20 70 50 61 67 65 72 2d 3e 65 ==0 && pPager->e
2d650 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20 20 xclusiveMode .
2d660 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 && pPager->jour
2d670 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
2d680 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 OURNALMODE_PERSI
2d690 53 54 0a 20 20 29 7b 0a 20 20 20 20 61 73 73 65 ST. ){. asse
2d6a0 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 rt( pPager->jour
2d6b0 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f nalOff==JOURNAL_
2d6c0 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 HDR_SZ(pPager) )
2d6d0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
2d6e0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 50 ITE_OK;. }.. P
2d6f0 41 47 45 52 54 52 41 43 45 28 28 22 43 4f 4d 4d AGERTRACE(("COMM
2d700 49 54 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 IT %d\n", PAGERI
2d710 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 61 D(pPager)));. a
2d720 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
2d730 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 tate==PAGER_SYNC
2d740 45 44 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 21 ED || MEMDB || !
2d750 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 pPager->dbModifi
2d760 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 70 61 67 ed );. rc = pag
2d770 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 er_end_transacti
2d780 6f 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 on(pPager, pPage
2d790 72 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 r->setMaster);.
2d7a0 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 return pager_er
2d7b0 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b ror(pPager, rc);
2d7c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 .}../*.** Rollba
2d7d0 63 6b 20 61 6c 6c 20 63 68 61 6e 67 65 73 2e 20 ck all changes.
2d7e0 54 68 65 20 64 61 74 61 62 61 73 65 20 66 61 6c The database fal
2d7f0 6c 73 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 ls back to PAGER
2d800 5f 53 48 41 52 45 44 20 6d 6f 64 65 2e 0a 2a 2a _SHARED mode..**
2d810 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
2d820 6e 20 70 65 72 66 6f 72 6d 73 20 74 77 6f 20 74 n performs two t
2d830 61 73 6b 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 asks:.**.** 1)
2d840 20 49 74 20 72 6f 6c 6c 73 20 62 61 63 6b 20 74 It rolls back t
2d850 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c he journal file,
2d860 20 72 65 73 74 6f 72 69 6e 67 20 61 6c 6c 20 64 restoring all d
2d870 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
2d880 20 0a 2a 2a 20 20 20 20 20 20 69 6e 2d 6d 65 6d .** in-mem
2d890 6f 72 79 20 63 61 63 68 65 20 70 61 67 65 73 20 ory cache pages
2d8a0 74 6f 20 74 68 65 20 73 74 61 74 65 20 74 68 65 to the state the
2d8b0 79 20 77 65 72 65 20 69 6e 20 77 68 65 6e 20 74 y were in when t
2d8c0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a he transaction.*
2d8d0 2a 20 20 20 20 20 20 77 61 73 20 6f 70 65 6e 65 * was opene
2d8e0 64 2c 20 61 6e 64 0a 2a 2a 20 20 20 32 29 20 49 d, and.** 2) I
2d8f0 74 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 t finalizes the
2d900 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 73 6f journal file, so
2d910 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 that it is not
2d920 75 73 65 64 20 66 6f 72 20 68 6f 74 0a 2a 2a 20 used for hot.**
2d930 20 20 20 20 20 72 6f 6c 6c 62 61 63 6b 20 61 74 rollback at
2d940 20 61 6e 79 20 70 6f 69 6e 74 20 69 6e 20 74 68 any point in th
2d950 65 20 66 75 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 e future..**.**
2d960 73 75 62 6a 65 63 74 20 74 6f 20 74 68 65 20 66 subject to the f
2d970 6f 6c 6c 6f 77 69 6e 67 20 71 75 61 6c 69 66 69 ollowing qualifi
2d980 63 61 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 2a cations:.**.** *
2d990 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 If the journal
2d9a0 66 69 6c 65 20 69 73 20 6e 6f 74 20 79 65 74 20 file is not yet
2d9b0 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66 open when this f
2d9c0 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
2d9d0 64 2c 0a 2a 2a 20 20 20 74 68 65 6e 20 6f 6e 6c d,.** then onl
2d9e0 79 20 28 32 29 20 69 73 20 70 65 72 66 6f 72 6d y (2) is perform
2d9f0 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
2da00 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6a 6f 75 there is no jou
2da10 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 20 74 rnal file.** t
2da20 6f 20 72 6f 6c 6c 20 62 61 63 6b 2e 0a 2a 2a 0a o roll back..**.
2da30 2a 2a 20 2a 20 49 66 20 69 6e 20 61 6e 20 65 72 ** * If in an er
2da40 72 6f 72 20 73 74 61 74 65 20 6f 74 68 65 72 20 ror state other
2da50 74 68 61 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c than SQLITE_FULL
2da60 2c 20 74 68 65 6e 20 74 61 73 6b 20 28 31 29 20 , then task (1)
2da70 69 73 20 0a 2a 2a 20 20 20 70 65 72 66 6f 72 6d is .** perform
2da80 65 64 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 ed. If successfu
2da90 6c 2c 20 74 61 73 6b 20 28 32 29 2e 20 52 65 67 l, task (2). Reg
2daa0 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6f ardless of the o
2dab0 75 74 63 6f 6d 65 0a 2a 2a 20 20 20 6f 66 20 65 utcome.** of e
2dac0 69 74 68 65 72 2c 20 74 68 65 20 65 72 72 6f 72 ither, the error
2dad0 20 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 state error cod
2dae0 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f e is returned to
2daf0 20 74 68 65 20 63 61 6c 6c 65 72 0a 2a 2a 20 20 the caller.**
2db00 20 28 69 2e 65 2e 20 65 69 74 68 65 72 20 53 51 (i.e. either SQ
2db10 4c 49 54 45 5f 49 4f 45 52 52 20 6f 72 20 53 51 LITE_IOERR or SQ
2db20 4c 49 54 45 5f 43 4f 52 52 55 50 54 29 2e 0a 2a LITE_CORRUPT)..*
2db30 2a 0a 2a 2a 20 2a 20 49 66 20 74 68 65 20 70 61 *.** * If the pa
2db40 67 65 72 20 69 73 20 69 6e 20 50 41 47 45 52 5f ger is in PAGER_
2db50 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 RESERVED state,
2db60 74 68 65 6e 20 61 74 74 65 6d 70 74 20 28 31 29 then attempt (1)
2db70 2e 20 57 68 65 74 68 65 72 0a 2a 2a 20 20 20 6f . Whether.** o
2db80 72 20 6e 6f 74 20 28 31 29 20 69 73 20 73 75 63 r not (1) is suc
2db90 63 75 73 73 66 75 6c 2c 20 61 6c 73 6f 20 61 74 cussful, also at
2dba0 74 65 6d 70 74 20 28 32 29 2e 20 49 66 20 73 75 tempt (2). If su
2dbb0 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e ccessful, return
2dbc0 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4f 4b 2e .** SQLITE_OK.
2dbd0 20 4f 74 68 65 72 77 69 73 65 2c 20 65 6e 74 65 Otherwise, ente
2dbe0 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 r the error stat
2dbf0 65 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 e and return the
2dc00 20 66 69 72 73 74 20 0a 2a 2a 20 20 20 65 72 72 first .** err
2dc10 6f 72 20 63 6f 64 65 20 65 6e 63 6f 75 6e 74 65 or code encounte
2dc20 72 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49 6e red. .**.** In
2dc30 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 this case there
2dc40 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 is no chance th
2dc50 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 at the database
2dc60 77 61 73 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 was written to.
2dc70 0a 2a 2a 20 20 20 53 6f 20 69 73 20 73 61 66 65 .** So is safe
2dc80 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 to finalize the
2dc90 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 76 journal file ev
2dca0 65 6e 20 69 66 20 74 68 65 20 70 6c 61 79 62 61 en if the playba
2dcb0 63 6b 20 0a 2a 2a 20 20 20 28 6f 70 65 72 61 74 ck .** (operat
2dcc0 69 6f 6e 20 31 29 20 66 61 69 6c 65 64 2e 20 48 ion 1) failed. H
2dcd0 6f 77 65 76 65 72 20 74 68 65 20 70 61 67 65 72 owever the pager
2dce0 20 6d 75 73 74 20 65 6e 74 65 72 20 74 68 65 20 must enter the
2dcf0 65 72 72 6f 72 20 73 74 61 74 65 0a 2a 2a 20 20 error state.**
2dd00 20 61 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 as the contents
2dd10 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 of the in-memor
2dd20 79 20 63 61 63 68 65 20 61 72 65 20 6e 6f 77 20 y cache are now
2dd30 73 75 73 70 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 2a suspect..**.** *
2dd40 20 46 69 6e 61 6c 6c 79 2c 20 69 66 20 69 6e 20 Finally, if in
2dd50 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 PAGER_EXCLUSIVE
2dd60 73 74 61 74 65 2c 20 74 68 65 6e 20 61 74 74 65 state, then atte
2dd70 6d 70 74 20 28 31 29 2e 20 4f 6e 6c 79 0a 2a 2a mpt (1). Only.**
2dd80 20 20 20 61 74 74 65 6d 70 74 20 28 32 29 20 69 attempt (2) i
2dd90 66 20 28 31 29 20 69 73 20 73 75 63 63 65 73 73 f (1) is success
2dda0 66 75 6c 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 ful. Return SQLI
2ddb0 54 45 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73 TE_OK if success
2ddc0 66 75 6c 2c 0a 2a 2a 20 20 20 6f 74 68 65 72 77 ful,.** otherw
2ddd0 69 73 65 20 65 6e 74 65 72 20 74 68 65 20 65 72 ise enter the er
2dde0 72 6f 72 20 73 74 61 74 65 20 61 6e 64 20 72 65 ror state and re
2ddf0 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 turn the error c
2de00 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a ode from the .**
2de10 20 20 20 66 61 69 6c 69 6e 67 20 6f 70 65 72 61 failing opera
2de20 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 6e tion..**.** In
2de30 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 64 this case the d
2de40 61 74 61 62 61 73 65 20 66 69 6c 65 20 6d 61 79 atabase file may
2de50 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 have been writt
2de60 65 6e 20 74 6f 2e 20 53 6f 20 69 66 20 74 68 65 en to. So if the
2de70 0a 2a 2a 20 20 20 70 6c 61 79 62 61 63 6b 20 6f .** playback o
2de80 70 65 72 61 74 69 6f 6e 20 64 69 64 20 6e 6f 74 peration did not
2de90 20 73 75 63 63 65 65 64 20 69 74 20 77 6f 75 6c succeed it woul
2dea0 64 20 6e 6f 74 20 62 65 20 73 61 66 65 20 74 6f d not be safe to
2deb0 20 66 69 6e 61 6c 69 7a 65 0a 2a 2a 20 20 20 74 finalize.** t
2dec0 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e he journal file.
2ded0 20 49 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 It needs to be
2dee0 6c 65 66 74 20 69 6e 20 74 68 65 20 66 69 6c 65 left in the file
2def0 2d 73 79 73 74 65 6d 20 73 6f 20 74 68 61 74 0a -system so that.
2df00 2a 2a 20 20 20 73 6f 6d 65 20 6f 74 68 65 72 20 ** some other
2df10 70 72 6f 63 65 73 73 20 63 61 6e 20 75 73 65 20 process can use
2df20 69 74 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 it to restore th
2df30 65 20 64 61 74 61 62 61 73 65 20 73 74 61 74 65 e database state
2df40 20 28 62 79 0a 2a 2a 20 20 20 68 6f 74 2d 6a 6f (by.** hot-jo
2df50 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e urnal rollback).
2df60 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 .*/.int sqlite3P
2df70 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 50 61 67 agerRollback(Pag
2df80 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 er *pPager){. i
2df90 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
2dfa0 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 K;
2dfb0 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
2dfc0 64 65 20 2a 2f 0a 20 20 50 41 47 45 52 54 52 41 de */. PAGERTRA
2dfd0 43 45 28 28 22 52 4f 4c 4c 42 41 43 4b 20 25 64 CE(("ROLLBACK %d
2dfe0 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 \n", PAGERID(pPa
2dff0 67 65 72 29 29 29 3b 0a 20 20 69 66 28 20 21 70 ger)));. if( !p
2e000 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 Pager->dbModifie
2e010 64 20 7c 7c 20 21 69 73 4f 70 65 6e 28 70 50 61 d || !isOpen(pPa
2e020 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 ger->jfd) ){.
2e030 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f rc = pager_end_
2e040 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 transaction(pPag
2e050 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d er, pPager->setM
2e060 61 73 74 65 72 29 3b 0a 20 20 7d 65 6c 73 65 20 aster);. }else
2e070 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 if( pPager->errC
2e080 6f 64 65 20 26 26 20 70 50 61 67 65 72 2d 3e 65 ode && pPager->e
2e090 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 rrCode!=SQLITE_F
2e0a0 55 4c 4c 20 29 7b 0a 20 20 20 20 69 66 28 20 70 ULL ){. if( p
2e0b0 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
2e0c0 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 7b GER_EXCLUSIVE ){
2e0d0 0a 20 20 20 20 20 20 70 61 67 65 72 5f 70 6c 61 . pager_pla
2e0e0 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29 yback(pPager, 0)
2e0f0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
2e100 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
2e110 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
2e120 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 f( pPager->state
2e130 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 ==PAGER_RESERVED
2e140 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 ){. int rc
2e150 32 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 2;. rc = pa
2e160 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 ger_playback(pPa
2e170 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 ger, 0);. r
2e180 63 32 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 c2 = pager_end_t
2e190 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 ransaction(pPage
2e1a0 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 r, pPager->setMa
2e1b0 73 74 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 ster);. if(
2e1c0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
2e1d0 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 {. rc = r
2e1e0 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 c2;. }.
2e1f0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
2e200 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b = pager_playback
2e210 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 (pPager, 0);.
2e220 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 4d 45 4d }.. if( !MEM
2e230 44 42 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 DB ){. pPag
2e240 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 er->dbSizeValid
2e250 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 = 0;. }..
2e260 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f /* If an error o
2e270 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 20 52 ccurs during a R
2e280 4f 4c 4c 42 41 43 4b 2c 20 77 65 20 63 61 6e 20 OLLBACK, we can
2e290 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 75 73 74 20 no longer trust
2e2a0 74 68 65 20 70 61 67 65 72 0a 20 20 20 20 2a 2a the pager. **
2e2b0 20 63 61 63 68 65 2e 20 53 6f 20 63 61 6c 6c 20 cache. So call
2e2c0 70 61 67 65 72 5f 65 72 72 6f 72 28 29 20 6f 6e pager_error() on
2e2d0 20 74 68 65 20 77 61 79 20 6f 75 74 20 74 6f 20 the way out to
2e2e0 6d 61 6b 65 20 61 6e 79 20 65 72 72 6f 72 20 0a make any error .
2e2f0 20 20 20 20 2a 2a 20 70 65 72 73 69 73 74 65 6e ** persisten
2e300 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 t.. */. rc
2e310 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 = pager_error(p
2e320 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 7d 0a Pager, rc);. }.
2e330 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
2e340 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 /*.** Return TRU
2e350 45 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 E if the databas
2e360 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 e file is opened
2e370 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 52 65 74 read-only. Ret
2e380 75 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 66 20 urn FALSE.** if
2e390 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
2e3a0 28 69 6e 20 74 68 65 6f 72 79 29 20 77 72 69 74 (in theory) writ
2e3b0 61 62 6c 65 2e 0a 2a 2f 0a 75 38 20 73 71 6c 69 able..*/.u8 sqli
2e3c0 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e te3PagerIsreadon
2e3d0 6c 79 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ly(Pager *pPager
2e3e0 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 ){. return pPag
2e3f0 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 3b 0a 7d 0a er->readOnly;.}.
2e400 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
2e410 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 e number of refe
2e420 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 rences to the pa
2e430 67 65 72 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 ger..*/.int sqli
2e440 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 te3PagerRefcount
2e450 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
2e460 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
2e470 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 3PcacheRefCount(
2e480 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
2e490 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
2e4a0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
2e4b0 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 references to th
2e4c0 65 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 e specified page
2e4d0 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
2e4e0 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e PagerPageRefcoun
2e4f0 74 28 44 62 50 61 67 65 20 2a 70 50 61 67 65 29 t(DbPage *pPage)
2e500 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
2e510 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66 63 e3PcachePageRefc
2e520 6f 75 6e 74 28 70 50 61 67 65 29 3b 0a 7d 0a 0a ount(pPage);.}..
2e530 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
2e540 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f ST./*.** This ro
2e550 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f utine is used fo
2e560 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e r testing and an
2e570 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a alysis only..*/.
2e580 69 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 int *sqlite3Page
2e590 72 53 74 61 74 73 28 50 61 67 65 72 20 2a 70 50 rStats(Pager *pP
2e5a0 61 67 65 72 29 7b 0a 20 20 73 74 61 74 69 63 20 ager){. static
2e5b0 69 6e 74 20 61 5b 31 31 5d 3b 0a 20 20 61 5b 30 int a[11];. a[0
2e5c0 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 ] = sqlite3Pcach
2e5d0 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 eRefCount(pPager
2e5e0 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 61 5b ->pPCache);. a[
2e5f0 31 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 1] = sqlite3Pcac
2e600 68 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 hePagecount(pPag
2e610 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 er->pPCache);.
2e620 61 5b 32 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 a[2] = sqlite3Pc
2e630 61 63 68 65 47 65 74 43 61 63 68 65 73 69 7a 65 acheGetCachesize
2e640 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
2e650 29 3b 0a 20 20 61 5b 33 5d 20 3d 20 70 50 61 67 );. a[3] = pPag
2e660 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 er->dbSizeValid
2e670 3f 20 28 69 6e 74 29 20 70 50 61 67 65 72 2d 3e ? (int) pPager->
2e680 64 62 53 69 7a 65 20 3a 20 2d 31 3b 0a 20 20 61 dbSize : -1;. a
2e690 5b 34 5d 20 3d 20 70 50 61 67 65 72 2d 3e 73 74 [4] = pPager->st
2e6a0 61 74 65 3b 0a 20 20 61 5b 35 5d 20 3d 20 70 50 ate;. a[5] = pP
2e6b0 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 ager->errCode;.
2e6c0 20 61 5b 36 5d 20 3d 20 70 50 61 67 65 72 2d 3e a[6] = pPager->
2e6d0 6e 48 69 74 3b 0a 20 20 61 5b 37 5d 20 3d 20 70 nHit;. a[7] = p
2e6e0 50 61 67 65 72 2d 3e 6e 4d 69 73 73 3b 0a 20 20 Pager->nMiss;.
2e6f0 61 5b 38 5d 20 3d 20 30 3b 20 20 2f 2a 20 55 73 a[8] = 0; /* Us
2e700 65 64 20 74 6f 20 62 65 20 70 50 61 67 65 72 2d ed to be pPager-
2e710 3e 6e 4f 76 66 6c 20 2a 2f 0a 20 20 61 5b 39 5d >nOvfl */. a[9]
2e720 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65 61 64 = pPager->nRead
2e730 3b 0a 20 20 61 5b 31 30 5d 20 3d 20 70 50 61 67 ;. a[10] = pPag
2e740 65 72 2d 3e 6e 57 72 69 74 65 3b 0a 20 20 72 65 er->nWrite;. re
2e750 74 75 72 6e 20 61 3b 0a 7d 0a 23 65 6e 64 69 66 turn a;.}.#endif
2e760 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
2e770 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 rue if this is a
2e780 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 n in-memory page
2e790 72 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 r..*/.int sqlite
2e7a0 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28 50 61 3PagerIsMemdb(Pa
2e7b0 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
2e7c0 72 65 74 75 72 6e 20 4d 45 4d 44 42 3b 0a 7d 0a return MEMDB;.}.
2e7d0 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 ./*.** Check tha
2e7e0 74 20 74 68 65 72 65 20 61 72 65 20 61 74 20 6c t there are at l
2e7f0 65 61 73 74 20 6e 53 61 76 65 70 6f 69 6e 74 20 east nSavepoint
2e800 73 61 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e 2e savepoints open.
2e810 20 49 66 20 74 68 65 72 65 20 61 72 65 0a 2a 2a If there are.**
2e820 20 63 75 72 72 65 6e 74 6c 79 20 6c 65 73 73 20 currently less
2e830 74 68 61 6e 20 6e 53 61 76 65 70 6f 69 6e 74 73 than nSavepoints
2e840 20 6f 70 65 6e 2c 20 74 68 65 6e 20 6f 70 65 6e open, then open
2e850 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 61 76 one or more sav
2e860 65 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 6d 61 epoints.** to ma
2e870 6b 65 20 75 70 20 74 68 65 20 64 69 66 66 65 72 ke up the differ
2e880 65 6e 63 65 2e 20 49 66 20 74 68 65 20 6e 75 6d ence. If the num
2e890 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 ber of savepoint
2e8a0 73 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 s is already.**
2e8b0 65 71 75 61 6c 20 74 6f 20 6e 53 61 76 65 70 6f equal to nSavepo
2e8c0 69 6e 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 int, then this f
2e8d0 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d unction is a no-
2e8e0 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d op..**.** If a m
2e8f0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
2e900 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e fails, SQLITE_N
2e910 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 OMEM is returned
2e920 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 0a 2a . If an error .*
2e930 2a 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6f * occurs while o
2e940 70 65 6e 69 6e 67 20 74 68 65 20 73 75 62 2d 6a pening the sub-j
2e950 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 ournal file, the
2e960 6e 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f n an IO error co
2e970 64 65 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 de is.** returne
2e980 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 d. Otherwise, SQ
2e990 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 69 6e 74 20 LITE_OK..*/.int
2e9a0 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e sqlite3PagerOpen
2e9b0 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 Savepoint(Pager
2e9c0 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6e 53 61 *pPager, int nSa
2e9d0 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 vepoint){. int
2e9e0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
2e9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ea00 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
2ea10 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 code */. int nC
2ea20 75 72 72 65 6e 74 20 3d 20 70 50 61 67 65 72 2d urrent = pPager-
2ea30 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 >nSavepoint;
2ea40 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e /* Current n
2ea50 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69 umber of savepoi
2ea60 6e 74 73 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 53 nts */.. if( nS
2ea70 61 76 65 70 6f 69 6e 74 3e 6e 43 75 72 72 65 6e avepoint>nCurren
2ea80 74 20 26 26 20 70 50 61 67 65 72 2d 3e 75 73 65 t && pPager->use
2ea90 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 69 Journal ){. i
2eaa0 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 nt ii;
2eab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2eac0 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 /* Iterat
2ead0 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 or variable */.
2eae0 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e PagerSavepoin
2eaf0 74 20 2a 61 4e 65 77 3b 20 20 20 20 20 20 20 20 t *aNew;
2eb00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 /* Ne
2eb10 77 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 w Pager.aSavepoi
2eb20 6e 74 20 61 72 72 61 79 20 2a 2f 0a 0a 20 20 20 nt array */..
2eb30 20 2f 2a 20 45 69 74 68 65 72 20 74 68 65 72 65 /* Either there
2eb40 20 69 73 20 6e 6f 20 61 63 74 69 76 65 20 6a 6f is no active jo
2eb50 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 73 75 62 urnal or the sub
2eb60 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e -journal is open
2eb70 20 6f 72 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 or . ** the
2eb80 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c 77 61 79 journal is alway
2eb90 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f s stored in memo
2eba0 72 79 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 ry */. assert
2ebb0 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 ( pPager->nSavep
2ebc0 6f 69 6e 74 3d 3d 30 20 7c 7c 20 69 73 4f 70 65 oint==0 || isOpe
2ebd0 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 n(pPager->sjfd)
2ebe0 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 ||. p
2ebf0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
2ec00 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
2ec10 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a LMODE_MEMORY );.
2ec20 0a 20 20 20 20 2f 2a 20 47 72 6f 77 20 74 68 65 . /* Grow the
2ec30 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e Pager.aSavepoin
2ec40 74 20 61 72 72 61 79 20 75 73 69 6e 67 20 72 65 t array using re
2ec50 61 6c 6c 6f 63 28 29 2e 20 52 65 74 75 72 6e 20 alloc(). Return
2ec60 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 20 SQLITE_NOMEM.
2ec70 20 2a 2a 20 69 66 20 74 68 65 20 61 6c 6c 6f 63 ** if the alloc
2ec80 61 74 69 6f 6e 20 66 61 69 6c 73 2e 20 4f 74 68 ation fails. Oth
2ec90 65 72 77 69 73 65 2c 20 7a 65 72 6f 20 74 68 65 erwise, zero the
2eca0 20 6e 65 77 20 70 6f 72 74 69 6f 6e 20 69 6e 20 new portion in
2ecb0 63 61 73 65 20 61 20 0a 20 20 20 20 2a 2a 20 6d case a . ** m
2ecc0 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 alloc failure oc
2ecd0 63 75 72 73 20 77 68 69 6c 65 20 70 6f 70 75 6c curs while popul
2ece0 61 74 69 6e 67 20 69 74 20 69 6e 20 74 68 65 20 ating it in the
2ecf0 66 6f 72 28 2e 2e 2e 29 20 6c 6f 6f 70 20 62 65 for(...) loop be
2ed00 6c 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 low.. */.
2ed10 61 4e 65 77 20 3d 20 28 50 61 67 65 72 53 61 76 aNew = (PagerSav
2ed20 65 70 6f 69 6e 74 20 2a 29 73 71 6c 69 74 65 33 epoint *)sqlite3
2ed30 52 65 61 6c 6c 6f 63 28 0a 20 20 20 20 20 20 20 Realloc(.
2ed40 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f pPager->aSavepo
2ed50 69 6e 74 2c 20 73 69 7a 65 6f 66 28 50 61 67 65 int, sizeof(Page
2ed60 72 53 61 76 65 70 6f 69 6e 74 29 2a 6e 53 61 76 rSavepoint)*nSav
2ed70 65 70 6f 69 6e 74 0a 20 20 20 20 29 3b 0a 20 20 epoint. );.
2ed80 20 20 69 66 28 20 21 61 4e 65 77 20 29 7b 0a 20 if( !aNew ){.
2ed90 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
2eda0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
2edb0 20 20 20 20 6d 65 6d 73 65 74 28 26 61 4e 65 77 memset(&aNew
2edc0 5b 6e 43 75 72 72 65 6e 74 5d 2c 20 30 2c 20 28 [nCurrent], 0, (
2edd0 6e 53 61 76 65 70 6f 69 6e 74 2d 6e 43 75 72 72 nSavepoint-nCurr
2ede0 65 6e 74 29 20 2a 20 73 69 7a 65 6f 66 28 50 61 ent) * sizeof(Pa
2edf0 67 65 72 53 61 76 65 70 6f 69 6e 74 29 29 3b 0a gerSavepoint));.
2ee00 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 pPager->aSav
2ee10 65 70 6f 69 6e 74 20 3d 20 61 4e 65 77 3b 0a 20 epoint = aNew;.
2ee20 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 pPager->nSave
2ee30 70 6f 69 6e 74 20 3d 20 6e 53 61 76 65 70 6f 69 point = nSavepoi
2ee40 6e 74 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 nt;.. /* Popu
2ee50 6c 61 74 65 20 74 68 65 20 50 61 67 65 72 53 61 late the PagerSa
2ee60 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 vepoint structur
2ee70 65 73 20 6a 75 73 74 20 61 6c 6c 6f 63 61 74 65 es just allocate
2ee80 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 69 d. */. for(ii
2ee90 3d 6e 43 75 72 72 65 6e 74 3b 20 69 69 3c 6e 53 =nCurrent; ii<nS
2eea0 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b avepoint; ii++){
2eeb0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
2eec0 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c Pager->dbSizeVal
2eed0 69 64 20 29 3b 0a 20 20 20 20 20 20 61 4e 65 77 id );. aNew
2eee0 5b 69 69 5d 2e 6e 4f 72 69 67 20 3d 20 70 50 61 [ii].nOrig = pPa
2eef0 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 ger->dbSize;.
2ef00 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 if( isOpen(pP
2ef10 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 41 4c ager->jfd) && AL
2ef20 57 41 59 53 28 70 50 61 67 65 72 2d 3e 6a 6f 75 WAYS(pPager->jou
2ef30 72 6e 61 6c 4f 66 66 3e 30 29 20 29 7b 0a 20 20 rnalOff>0) ){.
2ef40 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 aNew[ii].i
2ef50 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d Offset = pPager-
2ef60 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20 >journalOff;.
2ef70 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
2ef80 20 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 73 aNew[ii].iOffs
2ef90 65 74 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 et = JOURNAL_HDR
2efa0 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 20 _SZ(pPager);.
2efb0 20 20 20 7d 0a 20 20 20 20 20 20 61 4e 65 77 5b }. aNew[
2efc0 69 69 5d 2e 69 53 75 62 52 65 63 20 3d 20 70 50 ii].iSubRec = pP
2efd0 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 3b 0a 20 ager->nSubRec;.
2efe0 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 70 49 aNew[ii].pI
2eff0 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 73 71 6c nSavepoint = sql
2f000 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 ite3BitvecCreate
2f010 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 (pPager->dbSize)
2f020 3b 0a 20 20 20 20 20 20 69 66 28 20 21 61 4e 65 ;. if( !aNe
2f030 77 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 w[ii].pInSavepoi
2f040 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 nt ){. re
2f050 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
2f060 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d M;. }. }
2f070 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 .. /* Open th
2f080 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2c 20 69 e sub-journal, i
2f090 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 f it is not alre
2f0a0 61 64 79 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 ady opened. */.
2f0b0 20 20 20 72 63 20 3d 20 6f 70 65 6e 53 75 62 4a rc = openSubJ
2f0c0 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a ournal(pPager);.
2f0d0 20 20 20 20 61 73 73 65 72 74 54 72 75 6e 63 61 assertTrunca
2f0e0 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 teConstraint(pPa
2f0f0 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 ger);. }.. ret
2f100 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
2f110 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
2f120 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 6f 6c 6c s called to roll
2f130 62 61 63 6b 20 6f 72 20 72 65 6c 65 61 73 65 20 back or release
2f140 28 63 6f 6d 6d 69 74 29 20 61 20 73 61 76 65 70 (commit) a savep
2f150 6f 69 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 61 76 oint..** The sav
2f160 65 70 6f 69 6e 74 20 74 6f 20 72 65 6c 65 61 73 epoint to releas
2f170 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 e or rollback ne
2f180 65 64 20 6e 6f 74 20 62 65 20 74 68 65 20 6d 6f ed not be the mo
2f190 73 74 20 72 65 63 65 6e 74 6c 79 20 0a 2a 2a 20 st recently .**
2f1a0 63 72 65 61 74 65 64 20 73 61 76 65 70 6f 69 6e created savepoin
2f1b0 74 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 t..**.** Paramet
2f1c0 65 72 20 6f 70 20 69 73 20 61 6c 77 61 79 73 20 er op is always
2f1d0 65 69 74 68 65 72 20 53 41 56 45 50 4f 49 4e 54 either SAVEPOINT
2f1e0 5f 52 4f 4c 4c 42 41 43 4b 20 6f 72 20 53 41 56 _ROLLBACK or SAV
2f1f0 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 0a EPOINT_RELEASE..
2f200 2a 2a 20 49 66 20 69 74 20 69 73 20 53 41 56 45 ** If it is SAVE
2f210 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74 POINT_RELEASE, t
2f220 68 65 6e 20 72 65 6c 65 61 73 65 20 61 6e 64 20 hen release and
2f230 64 65 73 74 72 6f 79 20 74 68 65 20 73 61 76 65 destroy the save
2f240 70 6f 69 6e 74 20 77 69 74 68 0a 2a 2a 20 69 6e point with.** in
2f250 64 65 78 20 69 53 61 76 65 70 6f 69 6e 74 2e 20 dex iSavepoint.
2f260 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f 49 If it is SAVEPOI
2f270 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 NT_ROLLBACK, the
2f280 6e 20 72 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 n rollback all c
2f290 68 61 6e 67 65 73 0a 2a 2a 20 74 68 61 74 20 68 hanges.** that h
2f2a0 61 76 65 20 6f 63 63 75 72 72 65 64 20 73 69 6e ave occurred sin
2f2b0 63 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64 ce the specified
2f2c0 20 73 61 76 65 70 6f 69 6e 74 20 77 61 73 20 63 savepoint was c
2f2d0 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 reated..**.** Th
2f2e0 65 20 73 61 76 65 70 6f 69 6e 74 20 74 6f 20 72 e savepoint to r
2f2f0 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 65 6c 65 61 ollback or relea
2f300 73 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 se is identified
2f310 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 0a 2a by parameter .*
2f320 2a 20 69 53 61 76 65 70 6f 69 6e 74 2e 20 41 20 * iSavepoint. A
2f330 76 61 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e 73 value of 0 means
2f340 20 74 6f 20 6f 70 65 72 61 74 65 20 6f 6e 20 74 to operate on t
2f350 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 he outermost sav
2f360 65 70 6f 69 6e 74 0a 2a 2a 20 28 74 68 65 20 66 epoint.** (the f
2f370 69 72 73 74 20 63 72 65 61 74 65 64 29 2e 20 41 irst created). A
2f380 20 76 61 6c 75 65 20 6f 66 20 28 50 61 67 65 72 value of (Pager
2f390 2e 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 20 6d .nSavepoint-1) m
2f3a0 65 61 6e 73 20 6f 70 65 72 61 74 65 0a 2a 2a 20 eans operate.**
2f3b0 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 on the most rece
2f3c0 6e 74 6c 79 20 63 72 65 61 74 65 64 20 73 61 76 ntly created sav
2f3d0 65 70 6f 69 6e 74 2e 20 49 66 20 69 53 61 76 65 epoint. If iSave
2f3e0 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 65 72 point is greater
2f3f0 20 74 68 61 6e 0a 2a 2a 20 28 50 61 67 65 72 2e than.** (Pager.
2f400 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 2c 20 74 nSavepoint-1), t
2f410 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
2f420 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a n is a no-op..**
2f430 0a 2a 2a 20 49 66 20 61 20 6e 65 67 61 74 69 76 .** If a negativ
2f440 65 20 76 61 6c 75 65 20 69 73 20 70 61 73 73 65 e value is passe
2f450 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 d to this functi
2f460 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 on, then the cur
2f470 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 rent.** transact
2f480 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 ion is rolled ba
2f490 63 6b 2e 20 54 68 69 73 20 69 73 20 64 69 66 66 ck. This is diff
2f4a0 65 72 65 6e 74 20 74 6f 20 63 61 6c 6c 69 6e 67 erent to calling
2f4b0 20 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 .** sqlite3Page
2f4c0 72 52 6f 6c 6c 62 61 63 6b 28 29 20 62 65 63 61 rRollback() beca
2f4d0 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f use this functio
2f4e0 6e 20 64 6f 65 73 20 6e 6f 74 20 74 65 72 6d 69 n does not termi
2f4f0 6e 61 74 65 0a 2a 2a 20 74 68 65 20 74 72 61 6e nate.** the tran
2f500 73 61 63 74 69 6f 6e 20 6f 72 20 75 6e 6c 6f 63 saction or unloc
2f510 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 k the database,
2f520 69 74 20 6a 75 73 74 20 72 65 73 74 6f 72 65 73 it just restores
2f530 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74 the .** content
2f540 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 s of the databas
2f550 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 e to its origina
2f560 6c 20 73 74 61 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 l state. .**.**
2f570 49 6e 20 61 6e 79 20 63 61 73 65 2c 20 61 6c 6c In any case, all
2f580 20 73 61 76 65 70 6f 69 6e 74 73 20 77 69 74 68 savepoints with
2f590 20 61 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65 an index greate
2f5a0 72 20 74 68 61 6e 20 69 53 61 76 65 70 6f 69 6e r than iSavepoin
2f5b0 74 20 0a 2a 2a 20 61 72 65 20 64 65 73 74 72 6f t .** are destro
2f5c0 79 65 64 2e 20 49 66 20 74 68 69 73 20 69 73 20 yed. If this is
2f5d0 61 20 72 65 6c 65 61 73 65 20 6f 70 65 72 61 74 a release operat
2f5e0 69 6f 6e 20 28 6f 70 3d 3d 53 41 56 45 50 4f 49 ion (op==SAVEPOI
2f5f0 4e 54 5f 52 45 4c 45 41 53 45 29 2c 0a 2a 2a 20 NT_RELEASE),.**
2f600 74 68 65 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 then savepoint i
2f610 53 61 76 65 70 6f 69 6e 74 20 69 73 20 61 6c 73 Savepoint is als
2f620 6f 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2a 0a o destroyed..**.
2f630 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
2f640 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 may return SQLI
2f650 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 65 TE_NOMEM if a me
2f660 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
2f670 66 61 69 6c 73 2c 0a 2a 2a 20 6f 72 20 61 6e 20 fails,.** or an
2f680 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 IO error code if
2f690 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 an IO error occ
2f6a0 75 72 73 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e urs while rollin
2f6b0 67 20 62 61 63 6b 20 61 20 0a 2a 2a 20 73 61 76 g back a .** sav
2f6c0 65 70 6f 69 6e 74 2e 20 49 66 20 6e 6f 20 65 72 epoint. If no er
2f6d0 72 6f 72 73 20 6f 63 63 75 72 2c 20 53 51 4c 49 rors occur, SQLI
2f6e0 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
2f6f0 64 2e 0a 2a 2f 20 0a 69 6e 74 20 73 71 6c 69 74 d..*/ .int sqlit
2f700 65 33 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 e3PagerSavepoint
2f710 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
2f720 69 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 int op, int iSav
2f730 65 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 epoint){. int r
2f740 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a c = SQLITE_OK;..
2f750 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 assert( op==SA
2f760 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 VEPOINT_RELEASE
2f770 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 || op==SAVEPOINT
2f780 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 61 _ROLLBACK );. a
2f790 73 73 65 72 74 28 20 69 53 61 76 65 70 6f 69 6e ssert( iSavepoin
2f7a0 74 3e 3d 30 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 t>=0 || op==SAVE
2f7b0 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 POINT_ROLLBACK )
2f7c0 3b 0a 0a 20 20 69 66 28 20 69 53 61 76 65 70 6f ;.. if( iSavepo
2f7d0 69 6e 74 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 int<pPager->nSav
2f7e0 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69 6e epoint ){. in
2f7f0 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 t ii;
2f800 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 /* Iterator var
2f810 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 iable */. int
2f820 20 6e 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 nNew;
2f830 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 6d /* Number of rem
2f840 61 69 6e 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 aining savepoint
2f850 73 20 61 66 74 65 72 20 74 68 69 73 20 6f 70 2e s after this op.
2f860 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 */.. /* Figu
2f870 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 re out how many
2f880 73 61 76 65 70 6f 69 6e 74 73 20 77 69 6c 6c 20 savepoints will
2f890 73 74 69 6c 6c 20 62 65 20 61 63 74 69 76 65 20 still be active
2f8a0 61 66 74 65 72 20 74 68 69 73 0a 20 20 20 20 2a after this. *
2f8b0 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 20 53 74 6f * operation. Sto
2f8c0 72 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 6e re this value in
2f8d0 20 6e 4e 65 77 2e 20 54 68 65 6e 20 66 72 65 65 nNew. Then free
2f8e0 20 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 resources assoc
2f8f0 69 61 74 65 64 20 0a 20 20 20 20 2a 2a 20 77 69 iated . ** wi
2f900 74 68 20 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 th any savepoint
2f910 73 20 74 68 61 74 20 61 72 65 20 64 65 73 74 72 s that are destr
2f920 6f 79 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 oyed by this ope
2f930 72 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 ration.. */.
2f940 20 20 20 6e 4e 65 77 20 3d 20 69 53 61 76 65 70 nNew = iSavep
2f950 6f 69 6e 74 20 2b 20 28 6f 70 3d 3d 53 41 56 45 oint + (op==SAVE
2f960 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 3b POINT_ROLLBACK);
2f970 0a 20 20 20 20 66 6f 72 28 69 69 3d 6e 4e 65 77 . for(ii=nNew
2f980 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 ; ii<pPager->nSa
2f990 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a vepoint; ii++){.
2f9a0 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 sqlite3Bit
2f9b0 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 vecDestroy(pPage
2f9c0 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 r->aSavepoint[ii
2f9d0 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 29 3b ].pInSavepoint);
2f9e0 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 . }. pPage
2f9f0 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 r->nSavepoint =
2fa00 6e 4e 65 77 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 nNew;.. /* If
2fa10 20 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c 62 this is a rollb
2fa20 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e 2c 20 70 ack operation, p
2fa30 6c 61 79 62 61 63 6b 20 74 68 65 20 73 70 65 63 layback the spec
2fa40 69 66 69 65 64 20 73 61 76 65 70 6f 69 6e 74 2e ified savepoint.
2fa50 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 . ** If this
2fa60 69 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 2c 20 is a temp-file,
2fa70 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 it is possible t
2fa80 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hat the journal
2fa90 66 69 6c 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 file has. **
2faa0 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 not yet been ope
2fab0 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ned. In this cas
2fac0 65 20 74 68 65 72 65 20 68 61 76 65 20 62 65 65 e there have bee
2fad0 6e 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f 0a n no changes to.
2fae0 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 ** the datab
2faf0 61 73 65 20 66 69 6c 65 2c 20 73 6f 20 74 68 65 ase file, so the
2fb00 20 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 74 playback operat
2fb10 69 6f 6e 20 63 61 6e 20 62 65 20 73 6b 69 70 70 ion can be skipp
2fb20 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ed.. */. i
2fb30 66 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 f( op==SAVEPOINT
2fb40 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20 69 73 4f _ROLLBACK && isO
2fb50 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
2fb60 20 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72 53 ){. PagerS
2fb70 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 avepoint *pSavep
2fb80 6f 69 6e 74 20 3d 20 28 6e 4e 65 77 3d 3d 30 29 oint = (nNew==0)
2fb90 3f 30 3a 26 70 50 61 67 65 72 2d 3e 61 53 61 76 ?0:&pPager->aSav
2fba0 65 70 6f 69 6e 74 5b 6e 4e 65 77 2d 31 5d 3b 0a epoint[nNew-1];.
2fbb0 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
2fbc0 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e PlaybackSavepoin
2fbd0 74 28 70 50 61 67 65 72 2c 20 70 53 61 76 65 70 t(pPager, pSavep
2fbe0 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 61 73 73 oint);. ass
2fbf0 65 72 74 28 72 63 21 3d 53 51 4c 49 54 45 5f 44 ert(rc!=SQLITE_D
2fc00 4f 4e 45 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 ONE);. }. .
2fc10 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 /* If this is
2fc20 20 61 20 72 65 6c 65 61 73 65 20 6f 66 20 74 68 a release of th
2fc30 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 65 e outermost save
2fc40 70 6f 69 6e 74 2c 20 74 72 75 6e 63 61 74 65 20 point, truncate
2fc50 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 75 62 2d . ** the sub-
2fc60 6a 6f 75 72 6e 61 6c 20 74 6f 20 7a 65 72 6f 20 journal to zero
2fc70 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 2a bytes in size. *
2fc80 2f 0a 20 20 20 20 69 66 28 20 6e 4e 65 77 3d 3d /. if( nNew==
2fc90 30 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 4f 49 0 && op==SAVEPOI
2fca0 4e 54 5f 52 45 4c 45 41 53 45 20 26 26 20 69 73 NT_RELEASE && is
2fcb0 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 Open(pPager->sjf
2fcc0 64 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 d) ){. asse
2fcd0 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rt( rc==SQLITE_O
2fce0 4b 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 K );. rc =
2fcf0 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 sqlite3OsTruncat
2fd00 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 e(pPager->sjfd,
2fd10 30 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 0);. pPager
2fd20 2d 3e 6e 53 75 62 52 65 63 20 3d 20 30 3b 0a 20 ->nSubRec = 0;.
2fd30 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
2fd40 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
2fd50 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 eturn the full p
2fd60 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 athname of the d
2fd70 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f atabase file..*/
2fd80 0a 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c .const char *sql
2fd90 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d ite3PagerFilenam
2fda0 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 e(Pager *pPager)
2fdb0 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 {. return pPage
2fdc0 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a r->zFilename;.}.
2fdd0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
2fde0 65 20 56 46 53 20 73 74 72 75 63 74 75 72 65 20 e VFS structure
2fdf0 66 6f 72 20 74 68 65 20 70 61 67 65 72 2e 0a 2a for the pager..*
2fe00 2f 0a 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f /.const sqlite3_
2fe10 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67 65 vfs *sqlite3Page
2fe20 72 56 66 73 28 50 61 67 65 72 20 2a 70 50 61 67 rVfs(Pager *pPag
2fe30 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 er){. return pP
2fe40 61 67 65 72 2d 3e 70 56 66 73 3b 0a 7d 0a 0a 2f ager->pVfs;.}../
2fe50 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
2fe60 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 file handle for
2fe70 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
2fe80 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 e associated.**
2fe90 77 69 74 68 20 74 68 65 20 70 61 67 65 72 2e 20 with the pager.
2fea0 20 54 68 69 73 20 6d 69 67 68 74 20 72 65 74 75 This might retu
2feb0 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 66 rn NULL if the f
2fec0 69 6c 65 20 68 61 73 0a 2a 2a 20 6e 6f 74 20 79 ile has.** not y
2fed0 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a et been opened..
2fee0 2a 2f 0a 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 */.sqlite3_file
2fef0 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c *sqlite3PagerFil
2ff00 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 e(Pager *pPager)
2ff10 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 {. return pPage
2ff20 72 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 r->fd;.}../*.**
2ff30 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 Return the full
2ff40 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 pathname of the
2ff50 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f journal file..*/
2ff60 0a 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c .const char *sql
2ff70 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c ite3PagerJournal
2ff80 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67 name(Pager *pPag
2ff90 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 er){. return pP
2ffa0 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a ager->zJournal;.
2ffb0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
2ffc0 74 72 75 65 20 69 66 20 66 73 79 6e 63 28 29 20 true if fsync()
2ffd0 63 61 6c 6c 73 20 61 72 65 20 64 69 73 61 62 6c calls are disabl
2ffe0 65 64 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 ed for this page
2fff0 72 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 r. Return FALSE
30000 0a 2a 2a 20 69 66 20 66 73 79 6e 63 28 29 73 20 .** if fsync()s
30010 61 72 65 20 65 78 65 63 75 74 65 64 20 6e 6f 72 are executed nor
30020 6d 61 6c 6c 79 2e 0a 2a 2f 0a 69 6e 74 20 73 71 mally..*/.int sq
30030 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 lite3PagerNosync
30040 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
30050 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 . return pPager
30060 2d 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23 69 66 ->noSync;.}..#if
30070 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 def SQLITE_HAS_C
30080 4f 44 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f ODEC./*.** Set o
30090 72 20 72 65 74 72 69 65 76 65 20 74 68 65 20 63 r retrieve the c
300a0 6f 64 65 63 20 66 6f 72 20 74 68 69 73 20 70 61 odec for this pa
300b0 67 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ger.*/.static vo
300c0 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 id sqlite3PagerS
300d0 65 74 43 6f 64 65 63 28 0a 20 20 50 61 67 65 72 etCodec(. Pager
300e0 20 2a 70 50 61 67 65 72 2c 0a 20 20 76 6f 69 64 *pPager,. void
300f0 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 *(*xCodec)(void
30100 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 *,void*,Pgno,int
30110 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f 64 ),. void (*xCod
30120 65 63 53 69 7a 65 43 68 6e 67 29 28 76 6f 69 64 ecSizeChng)(void
30130 2a 2c 69 6e 74 2c 69 6e 74 29 2c 0a 20 20 76 6f *,int,int),. vo
30140 69 64 20 28 2a 78 43 6f 64 65 63 46 72 65 65 29 id (*xCodecFree)
30150 28 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 20 (void*),. void
30160 2a 70 43 6f 64 65 63 0a 29 7b 0a 20 20 69 66 28 *pCodec.){. if(
30170 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 pPager->xCodecF
30180 72 65 65 20 29 20 70 50 61 67 65 72 2d 3e 78 43 ree ) pPager->xC
30190 6f 64 65 63 46 72 65 65 28 70 50 61 67 65 72 2d odecFree(pPager-
301a0 3e 70 43 6f 64 65 63 29 3b 0a 20 20 70 50 61 67 >pCodec);. pPag
301b0 65 72 2d 3e 78 43 6f 64 65 63 20 3d 20 78 43 6f er->xCodec = xCo
301c0 64 65 63 3b 0a 20 20 70 50 61 67 65 72 2d 3e 78 dec;. pPager->x
301d0 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 20 3d 20 CodecSizeChng =
301e0 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 3b 0a xCodecSizeChng;.
301f0 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 pPager->xCodec
30200 46 72 65 65 20 3d 20 78 43 6f 64 65 63 46 72 65 Free = xCodecFre
30210 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 43 6f e;. pPager->pCo
30220 64 65 63 20 3d 20 70 43 6f 64 65 63 3b 0a 20 20 dec = pCodec;.
30230 70 61 67 65 72 52 65 70 6f 72 74 53 69 7a 65 28 pagerReportSize(
30240 70 50 61 67 65 72 29 3b 0a 7d 0a 73 74 61 74 69 pPager);.}.stati
30250 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 c void *sqlite3P
30260 61 67 65 72 47 65 74 43 6f 64 65 63 28 50 61 67 agerGetCodec(Pag
30270 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 er *pPager){. r
30280 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 70 43 eturn pPager->pC
30290 6f 64 65 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a odec;.}.#endif..
302a0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
302b0 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f MIT_AUTOVACUUM./
302c0 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 *.** Move the pa
302d0 67 65 20 70 50 67 20 74 6f 20 6c 6f 63 61 74 69 ge pPg to locati
302e0 6f 6e 20 70 67 6e 6f 20 69 6e 20 74 68 65 20 66 on pgno in the f
302f0 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 ile..**.** There
30300 20 6d 75 73 74 20 62 65 20 6e 6f 20 72 65 66 65 must be no refe
30310 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 rences to the pa
30320 67 65 20 70 72 65 76 69 6f 75 73 6c 79 20 6c 6f ge previously lo
30330 63 61 74 65 64 20 61 74 0a 2a 2a 20 70 67 6e 6f cated at.** pgno
30340 20 28 77 68 69 63 68 20 77 65 20 63 61 6c 6c 20 (which we call
30350 70 50 67 4f 6c 64 29 20 74 68 6f 75 67 68 20 74 pPgOld) though t
30360 68 61 74 20 70 61 67 65 20 69 73 20 61 6c 6c 6f hat page is allo
30370 77 65 64 20 74 6f 20 62 65 0a 2a 2a 20 69 6e 20 wed to be.** in
30380 63 61 63 68 65 2e 20 20 49 66 20 74 68 65 20 70 cache. If the p
30390 61 67 65 20 70 72 65 76 69 6f 75 73 6c 79 20 6c age previously l
303a0 6f 63 61 74 65 64 20 61 74 20 70 67 6e 6f 20 69 ocated at pgno i
303b0 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a s not already.**
303c0 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b in the rollback
303d0 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 69 73 20 journal, it is
303e0 6e 6f 74 20 70 75 74 20 74 68 65 72 65 20 62 79 not put there by
303f0 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 by this routine
30400 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 65 6e 63 ..**.** Referenc
30410 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 70 es to the page p
30420 50 67 20 72 65 6d 61 69 6e 20 76 61 6c 69 64 2e Pg remain valid.
30430 20 55 70 64 61 74 69 6e 67 20 61 6e 79 0a 2a 2a Updating any.**
30440 20 6d 65 74 61 2d 64 61 74 61 20 61 73 73 6f 63 meta-data assoc
30450 69 61 74 65 64 20 77 69 74 68 20 70 50 67 20 28 iated with pPg (
30460 69 2e 65 2e 20 64 61 74 61 20 73 74 6f 72 65 64 i.e. data stored
30470 20 69 6e 20 74 68 65 20 6e 45 78 74 72 61 20 62 in the nExtra b
30480 79 74 65 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 ytes.** allocate
30490 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 d along with the
304a0 20 70 61 67 65 29 20 69 73 20 74 68 65 20 72 65 page) is the re
304b0 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 sponsibility of
304c0 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a the caller..**.*
304d0 2a 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 * A transaction
304e0 6d 75 73 74 20 62 65 20 61 63 74 69 76 65 20 77 must be active w
304f0 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
30500 20 69 73 20 63 61 6c 6c 65 64 2e 20 49 74 20 75 is called. It u
30510 73 65 64 20 74 6f 20 62 65 0a 2a 2a 20 72 65 71 sed to be.** req
30520 75 69 72 65 64 20 74 68 61 74 20 61 20 73 74 61 uired that a sta
30530 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
30540 6f 6e 20 77 61 73 20 6e 6f 74 20 61 63 74 69 76 on was not activ
30550 65 2c 20 62 75 74 20 74 68 69 73 20 72 65 73 74 e, but this rest
30560 72 69 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 riction.** has b
30570 65 65 6e 20 72 65 6d 6f 76 65 64 20 28 43 52 45 een removed (CRE
30580 41 54 45 20 49 4e 44 45 58 20 6e 65 65 64 73 20 ATE INDEX needs
30590 74 6f 20 6d 6f 76 65 20 61 20 70 61 67 65 20 77 to move a page w
305a0 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 0a hen a statement.
305b0 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 ** transaction i
305c0 73 20 61 63 74 69 76 65 29 2e 0a 2a 2a 0a 2a 2a s active)..**.**
305d0 20 49 66 20 74 68 65 20 66 6f 75 72 74 68 20 61 If the fourth a
305e0 72 67 75 6d 65 6e 74 2c 20 69 73 43 6f 6d 6d 69 rgument, isCommi
305f0 74 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 t, is non-zero,
30600 74 68 65 6e 20 74 68 69 73 20 70 61 67 65 20 69 then this page i
30610 73 20 62 65 69 6e 67 0a 2a 2a 20 6d 6f 76 65 64 s being.** moved
30620 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 64 61 as part of a da
30630 74 61 62 61 73 65 20 72 65 6f 72 67 61 6e 69 7a tabase reorganiz
30640 61 74 69 6f 6e 20 6a 75 73 74 20 62 65 66 6f 72 ation just befor
30650 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f e the transactio
30660 6e 20 0a 2a 2a 20 69 73 20 62 65 69 6e 67 20 63 n .** is being c
30670 6f 6d 6d 69 74 74 65 64 2e 20 49 6e 20 74 68 69 ommitted. In thi
30680 73 20 63 61 73 65 2c 20 69 74 20 69 73 20 67 75 s case, it is gu
30690 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 74 68 aranteed that th
306a0 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 e database page
306b0 0a 2a 2a 20 70 50 67 20 72 65 66 65 72 73 20 74 .** pPg refers t
306c0 6f 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 77 72 o will not be wr
306d0 69 74 74 65 6e 20 74 6f 20 61 67 61 69 6e 20 77 itten to again w
306e0 69 74 68 69 6e 20 74 68 69 73 20 74 72 61 6e 73 ithin this trans
306f0 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 action..**.** Th
30700 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 is function may
30710 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
30720 4d 45 4d 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 MEM or an IO err
30730 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 20 65 72 or code if an er
30740 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 4f ror.** occurs. O
30750 74 68 65 72 77 69 73 65 2c 20 69 74 20 72 65 74 therwise, it ret
30760 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a urns SQLITE_OK..
30770 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 61 */.int sqlite3Pa
30780 67 65 72 4d 6f 76 65 70 61 67 65 28 50 61 67 65 gerMovepage(Page
30790 72 20 2a 70 50 61 67 65 72 2c 20 44 62 50 61 67 r *pPager, DbPag
307a0 65 20 2a 70 50 67 2c 20 50 67 6e 6f 20 70 67 6e e *pPg, Pgno pgn
307b0 6f 2c 20 69 6e 74 20 69 73 43 6f 6d 6d 69 74 29 o, int isCommit)
307c0 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 4f 6c {. PgHdr *pPgOl
307d0 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
307e0 20 2f 2a 20 54 68 65 20 70 61 67 65 20 62 65 69 /* The page bei
307f0 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 ng overwritten.
30800 2a 2f 0a 20 20 50 67 6e 6f 20 6e 65 65 64 53 79 */. Pgno needSy
30810 6e 63 50 67 6e 6f 20 3d 20 30 3b 20 20 20 20 20 ncPgno = 0;
30820 20 20 2f 2a 20 4f 6c 64 20 76 61 6c 75 65 20 6f /* Old value o
30830 66 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 69 66 20 f pPg->pgno, if
30840 73 79 6e 63 20 69 73 20 72 65 71 75 69 72 65 64 sync is required
30850 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
30860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
30870 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
30880 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6f 72 69 67 e */. Pgno orig
30890 50 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 Pgno;
308a0 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 69 /* The origi
308b0 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62 65 72 20 nal page number
308c0 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 */.. assert( pP
308d0 67 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 0a 20 20 g->nRef>0 );..
308e0 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 62 /* If the page b
308f0 65 69 6e 67 20 6d 6f 76 65 64 20 69 73 20 64 69 eing moved is di
30900 72 74 79 20 61 6e 64 20 68 61 73 20 6e 6f 74 20 rty and has not
30910 62 65 65 6e 20 73 61 76 65 64 20 62 79 20 74 68 been saved by th
30920 65 20 6c 61 74 65 73 74 0a 20 20 2a 2a 20 73 61 e latest. ** sa
30930 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 73 61 vepoint, then sa
30940 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 ve the current c
30950 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 ontents of the p
30960 61 67 65 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 age into the .
30970 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6e ** sub-journal n
30980 6f 77 2e 20 54 68 69 73 20 69 73 20 72 65 71 75 ow. This is requ
30990 69 72 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 74 ired to handle t
309a0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 63 65 he following sce
309b0 6e 61 72 69 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a nario:. **. **
309c0 20 20 20 42 45 47 49 4e 3b 0a 20 20 2a 2a 20 20 BEGIN;. **
309d0 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 61 67 65 <journal page
309e0 20 58 2c 20 74 68 65 6e 20 6d 6f 64 69 66 79 20 X, then modify
309f0 69 74 20 69 6e 20 6d 65 6d 6f 72 79 3e 0a 20 20 it in memory>.
30a00 2a 2a 20 20 20 20 20 53 41 56 45 50 4f 49 4e 54 ** SAVEPOINT
30a10 20 6f 6e 65 3b 0a 20 20 2a 2a 20 20 20 20 20 20 one;. **
30a20 20 3c 4d 6f 76 65 20 70 61 67 65 20 58 20 74 6f <Move page X to
30a30 20 6c 6f 63 61 74 69 6f 6e 20 59 3e 0a 20 20 2a location Y>. *
30a40 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 20 54 * ROLLBACK T
30a50 4f 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20 2a 2a O one;. **. **
30a60 20 49 66 20 70 61 67 65 20 58 20 77 65 72 65 20 If page X were
30a70 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f 20 74 not written to t
30a80 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 68 he sub-journal h
30a90 65 72 65 2c 20 69 74 20 77 6f 75 6c 64 20 6e 6f ere, it would no
30aa0 74 0a 20 20 2a 2a 20 62 65 20 70 6f 73 73 69 62 t. ** be possib
30ab0 6c 65 20 74 6f 20 72 65 73 74 6f 72 65 20 69 74 le to restore it
30ac0 73 20 63 6f 6e 74 65 6e 74 73 20 77 68 65 6e 20 s contents when
30ad0 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 54 4f the "ROLLBACK TO
30ae0 20 6f 6e 65 22 0a 20 20 2a 2a 20 73 74 61 74 65 one". ** state
30af0 6d 65 6e 74 20 77 65 72 65 20 69 73 20 70 72 6f ment were is pro
30b00 63 65 73 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a cessed.. **. *
30b10 2a 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 * subjournalPage
30b20 28 29 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 61 () may need to a
30b30 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 74 6f llocate space to
30b40 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f store pPg->pgno
30b50 20 69 6e 74 6f 0a 20 20 2a 2a 20 6f 6e 65 20 6f into. ** one o
30b60 72 20 6d 6f 72 65 20 73 61 76 65 70 6f 69 6e 74 r more savepoint
30b70 20 62 69 74 76 65 63 73 2e 20 54 68 69 73 20 69 bitvecs. This i
30b80 73 20 74 68 65 20 72 65 61 73 6f 6e 20 74 68 69 s the reason thi
30b90 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 s function. **
30ba0 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 may return SQLIT
30bb0 45 5f 4e 4f 4d 45 4d 2e 0a 20 20 2a 2f 0a 20 20 E_NOMEM.. */.
30bc0 69 66 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 if( pPg->flags&P
30bd0 47 48 44 52 5f 44 49 52 54 59 20 0a 20 20 20 26 GHDR_DIRTY . &
30be0 26 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 & subjRequiresPa
30bf0 67 65 28 70 50 67 29 0a 20 20 20 26 26 20 53 51 ge(pPg). && SQ
30c00 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 LITE_OK!=(rc = s
30c10 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 ubjournalPage(pP
30c20 67 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 g)). ){. ret
30c30 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 50 urn rc;. }.. P
30c40 41 47 45 52 54 52 41 43 45 28 28 22 4d 4f 56 45 AGERTRACE(("MOVE
30c50 20 25 64 20 70 61 67 65 20 25 64 20 28 6e 65 65 %d page %d (nee
30c60 64 53 79 6e 63 3d 25 64 29 20 6d 6f 76 65 73 20 dSync=%d) moves
30c70 74 6f 20 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 to %d\n", .
30c80 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 PAGERID(pPager)
30c90 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 28 70 50 , pPg->pgno, (pP
30ca0 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
30cb0 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 2c 20 70 EED_SYNC)?1:0, p
30cc0 67 6e 6f 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 gno));. IOTRACE
30cd0 28 28 22 4d 4f 56 45 20 25 70 20 25 64 20 25 64 (("MOVE %p %d %d
30ce0 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 \n", pPager, pPg
30cf0 2d 3e 70 67 6e 6f 2c 20 70 67 6e 6f 29 29 0a 0a ->pgno, pgno))..
30d00 20 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 /* If the jour
30d10 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 nal needs to be
30d20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 sync()ed before
30d30 70 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 63 page pPg->pgno c
30d40 61 6e 0a 20 20 2a 2a 20 62 65 20 77 72 69 74 74 an. ** be writt
30d50 65 6e 20 74 6f 2c 20 73 74 6f 72 65 20 70 50 67 en to, store pPg
30d60 2d 3e 70 67 6e 6f 20 69 6e 20 6c 6f 63 61 6c 20 ->pgno in local
30d70 76 61 72 69 61 62 6c 65 20 6e 65 65 64 53 79 6e variable needSyn
30d80 63 50 67 6e 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a cPgno.. **. **
30d90 20 49 66 20 74 68 65 20 69 73 43 6f 6d 6d 69 74 If the isCommit
30da0 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 flag is set, th
30db0 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 ere is no need t
30dc0 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61 74 0a o remember that.
30dd0 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c ** the journal
30de0 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e needs to be syn
30df0 63 28 29 65 64 20 62 65 66 6f 72 65 20 64 61 74 c()ed before dat
30e00 61 62 61 73 65 20 70 61 67 65 20 70 50 67 2d 3e abase page pPg->
30e10 70 67 6e 6f 20 0a 20 20 2a 2a 20 63 61 6e 20 62 pgno . ** can b
30e20 65 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 54 68 e written to. Th
30e30 65 20 63 61 6c 6c 65 72 20 68 61 73 20 61 6c 72 e caller has alr
30e40 65 61 64 79 20 70 72 6f 6d 69 73 65 64 20 6e 6f eady promised no
30e50 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 69 74 t to write to it
30e60 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 50 .. */. if( (pP
30e70 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
30e80 45 45 44 5f 53 59 4e 43 29 20 26 26 20 21 69 73 EED_SYNC) && !is
30e90 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 6e 65 Commit ){. ne
30ea0 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20 70 50 67 edSyncPgno = pPg
30eb0 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 61 73 73 65 ->pgno;. asse
30ec0 72 74 28 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 rt( pageInJourna
30ed0 6c 28 70 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70 l(pPg) || pPg->p
30ee0 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72 gno>pPager->dbOr
30ef0 69 67 53 69 7a 65 20 29 3b 0a 20 20 20 20 61 73 igSize );. as
30f00 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 sert( pPg->flags
30f10 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a &PGHDR_DIRTY );.
30f20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
30f30 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 3b 0a er->needSync );.
30f40 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
30f50 20 63 61 63 68 65 20 63 6f 6e 74 61 69 6e 73 20 cache contains
30f60 61 20 70 61 67 65 20 77 69 74 68 20 70 61 67 65 a page with page
30f70 2d 6e 75 6d 62 65 72 20 70 67 6e 6f 2c 20 72 65 -number pgno, re
30f80 6d 6f 76 65 20 69 74 0a 20 20 2a 2a 20 66 72 6f move it. ** fro
30f90 6d 20 69 74 73 20 68 61 73 68 20 63 68 61 69 6e m its hash chain
30fa0 2e 20 41 6c 73 6f 2c 20 69 66 20 74 68 65 20 50 . Also, if the P
30fb0 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 77 61 gHdr.needSync wa
30fc0 73 20 73 65 74 20 66 6f 72 20 0a 20 20 2a 2a 20 s set for . **
30fd0 70 61 67 65 20 70 67 6e 6f 20 62 65 66 6f 72 65 page pgno before
30fe0 20 74 68 65 20 27 6d 6f 76 65 27 20 6f 70 65 72 the 'move' oper
30ff0 61 74 69 6f 6e 2c 20 69 74 20 6e 65 65 64 73 20 ation, it needs
31000 74 6f 20 62 65 20 72 65 74 61 69 6e 65 64 20 0a to be retained .
31010 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 70 61 67 ** for the pag
31020 65 20 6d 6f 76 65 64 20 74 68 65 72 65 2e 0a 20 e moved there..
31030 20 2a 2f 0a 20 20 70 50 67 2d 3e 66 6c 61 67 73 */. pPg->flags
31040 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f &= ~PGHDR_NEED_
31050 53 59 4e 43 3b 0a 20 20 70 50 67 4f 6c 64 20 3d SYNC;. pPgOld =
31060 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 pager_lookup(pP
31070 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 61 ager, pgno);. a
31080 73 73 65 72 74 28 20 21 70 50 67 4f 6c 64 20 7c ssert( !pPgOld |
31090 7c 20 70 50 67 4f 6c 64 2d 3e 6e 52 65 66 3d 3d | pPgOld->nRef==
310a0 31 20 29 3b 0a 20 20 69 66 28 20 70 50 67 4f 6c 1 );. if( pPgOl
310b0 64 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e 66 6c d ){. pPg->fl
310c0 61 67 73 20 7c 3d 20 28 70 50 67 4f 6c 64 2d 3e ags |= (pPgOld->
310d0 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
310e0 5f 53 59 4e 43 29 3b 0a 20 20 20 20 73 71 6c 69 _SYNC);. sqli
310f0 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 70 50 te3PcacheDrop(pP
31100 67 4f 6c 64 29 3b 0a 20 20 7d 0a 0a 20 20 6f 72 gOld);. }.. or
31110 69 67 50 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 igPgno = pPg->pg
31120 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 no;. sqlite3Pca
31130 63 68 65 4d 6f 76 65 28 70 50 67 2c 20 70 67 6e cheMove(pPg, pgn
31140 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 o);. sqlite3Pca
31150 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 67 cheMakeDirty(pPg
31160 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 4d );. pPager->dbM
31170 6f 64 69 66 69 65 64 20 3d 20 31 3b 0a 0a 20 20 odified = 1;..
31180 69 66 28 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f if( needSyncPgno
31190 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 65 ){. /* If ne
311a0 65 64 53 79 6e 63 50 67 6e 6f 20 69 73 20 6e 6f edSyncPgno is no
311b0 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 n-zero, then the
311c0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 65 journal file ne
311d0 65 64 73 20 74 6f 20 62 65 20 0a 20 20 20 20 2a eds to be . *
311e0 2a 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 * sync()ed befor
311f0 65 20 61 6e 79 20 64 61 74 61 20 69 73 20 77 72 e any data is wr
31200 69 74 74 65 6e 20 74 6f 20 64 61 74 61 62 61 73 itten to databas
31210 65 20 66 69 6c 65 20 70 61 67 65 20 6e 65 65 64 e file page need
31220 53 79 6e 63 50 67 6e 6f 2e 0a 20 20 20 20 2a 2a SyncPgno.. **
31230 20 43 75 72 72 65 6e 74 6c 79 2c 20 6e 6f 20 73 Currently, no s
31240 75 63 68 20 70 61 67 65 20 65 78 69 73 74 73 20 uch page exists
31250 69 6e 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 in the page-cach
31260 65 20 61 6e 64 20 74 68 65 20 0a 20 20 20 20 2a e and the . *
31270 2a 20 22 69 73 20 6a 6f 75 72 6e 61 6c 65 64 22 * "is journaled"
31280 20 62 69 74 76 65 63 20 66 6c 61 67 20 68 61 73 bitvec flag has
31290 20 62 65 65 6e 20 73 65 74 2e 20 54 68 69 73 20 been set. This
312a0 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 6d 65 needs to be reme
312b0 64 69 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 6c died by. ** l
312c0 6f 61 64 69 6e 67 20 74 68 65 20 70 61 67 65 20 oading the page
312d0 69 6e 74 6f 20 74 68 65 20 70 61 67 65 72 2d 63 into the pager-c
312e0 61 63 68 65 20 61 6e 64 20 73 65 74 74 69 6e 67 ache and setting
312f0 20 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64 53 the PgHdr.needS
31300 79 6e 63 20 0a 20 20 20 20 2a 2a 20 66 6c 61 67 ync . ** flag
31310 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
31320 49 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 If the attempt t
31330 6f 20 6c 6f 61 64 20 74 68 65 20 70 61 67 65 20 o load the page
31340 69 6e 74 6f 20 74 68 65 20 70 61 67 65 2d 63 61 into the page-ca
31350 63 68 65 20 66 61 69 6c 73 2c 20 28 64 75 65 0a che fails, (due.
31360 20 20 20 20 2a 2a 20 74 6f 20 61 20 6d 61 6c 6c ** to a mall
31370 6f 63 28 29 20 6f 72 20 49 4f 20 66 61 69 6c 75 oc() or IO failu
31380 72 65 29 2c 20 63 6c 65 61 72 20 74 68 65 20 62 re), clear the b
31390 69 74 20 69 6e 20 74 68 65 20 70 49 6e 4a 6f 75 it in the pInJou
313a0 72 6e 61 6c 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 rnal[]. ** ar
313b0 72 61 79 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ray. Otherwise,
313c0 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6c if the page is l
313d0 6f 61 64 65 64 20 61 6e 64 20 77 72 69 74 74 65 oaded and writte
313e0 6e 20 61 67 61 69 6e 20 69 6e 0a 20 20 20 20 2a n again in. *
313f0 2a 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 * this transacti
31400 6f 6e 2c 20 69 74 20 6d 61 79 20 62 65 20 77 72 on, it may be wr
31410 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 itten to the dat
31420 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 abase file befor
31430 65 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 73 e. ** it is s
31440 79 6e 63 65 64 20 69 6e 74 6f 20 74 68 65 20 6a ynced into the j
31450 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 ournal file. Thi
31460 73 20 77 61 79 2c 20 69 74 20 6d 61 79 20 65 6e s way, it may en
31470 64 20 75 70 20 69 6e 0a 20 20 20 20 2a 2a 20 74 d up in. ** t
31480 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
31490 74 77 69 63 65 2c 20 62 75 74 20 74 68 61 74 20 twice, but that
314a0 69 73 20 6e 6f 74 20 61 20 70 72 6f 62 6c 65 6d is not a problem
314b0 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
314c0 54 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 The sqlite3Pager
314d0 47 65 74 28 29 20 63 61 6c 6c 20 6d 61 79 20 63 Get() call may c
314e0 61 75 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c ause the journal
314f0 20 74 6f 20 73 79 6e 63 2e 20 53 6f 20 6d 61 6b to sync. So mak
31500 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 74 68 e. ** sure th
31510 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 e Pager.needSync
31520 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 6f 6f flag is set too
31530 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 50 67 48 .. */. PgH
31540 64 72 20 2a 70 50 67 48 64 72 3b 0a 20 20 20 20 dr *pPgHdr;.
31550 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
31560 6e 65 65 64 53 79 6e 63 20 29 3b 0a 20 20 20 20 needSync );.
31570 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
31580 72 47 65 74 28 70 50 61 67 65 72 2c 20 6e 65 65 rGet(pPager, nee
31590 64 53 79 6e 63 50 67 6e 6f 2c 20 26 70 50 67 48 dSyncPgno, &pPgH
315a0 64 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 dr);. if( rc!
315b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
315c0 20 20 20 20 69 66 28 20 6e 65 65 64 53 79 6e 63 if( needSync
315d0 50 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 Pgno<=pPager->db
315e0 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 OrigSize ){.
315f0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
31600 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 21 3d 30 er->pTmpSpace!=0
31610 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 );. sqli
31620 74 65 33 42 69 74 76 65 63 43 6c 65 61 72 28 70 te3BitvecClear(p
31630 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
31640 6c 2c 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2c l, needSyncPgno,
31650 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 pPager->pTmpSpa
31660 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ce);. }.
31670 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
31680 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e }. pPager->
31690 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 needSync = 1;.
316a0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
316b0 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 26 26 20 21 ->noSync==0 && !
316c0 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 70 50 67 MEMDB );. pPg
316d0 48 64 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 Hdr->flags |= PG
316e0 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 HDR_NEED_SYNC;.
316f0 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 sqlite3Pcache
31700 4d 61 6b 65 44 69 72 74 79 28 70 50 67 48 64 72 MakeDirty(pPgHdr
31710 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 );. sqlite3Pa
31720 67 65 72 55 6e 72 65 66 28 70 50 67 48 64 72 29 gerUnref(pPgHdr)
31730 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a ;. }.. /*. **
31740 20 46 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 For an in-memor
31750 79 20 64 61 74 61 62 61 73 65 2c 20 6d 61 6b 65 y database, make
31760 20 73 75 72 65 20 74 68 65 20 6f 72 69 67 69 6e sure the origin
31770 61 6c 20 70 61 67 65 20 63 6f 6e 74 69 6e 75 65 al page continue
31780 73 0a 20 20 2a 2a 20 74 6f 20 65 78 69 73 74 2c s. ** to exist,
31790 20 69 6e 20 63 61 73 65 20 74 68 65 20 74 72 61 in case the tra
317a0 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 nsaction needs t
317b0 6f 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 20 57 65 o roll back. We
317c0 20 61 6c 6c 6f 63 61 74 65 0a 20 20 2a 2a 20 74 allocate. ** t
317d0 68 65 20 70 61 67 65 20 6e 6f 77 2c 20 69 6e 73 he page now, ins
317e0 74 65 61 64 20 6f 66 20 61 74 20 72 6f 6c 6c 62 tead of at rollb
317f0 61 63 6b 2c 20 62 65 63 61 75 73 65 20 77 65 20 ack, because we
31800 63 61 6e 20 62 65 74 74 65 72 20 64 65 61 6c 0a can better deal.
31810 20 20 2a 2a 20 77 69 74 68 20 61 6e 20 6f 75 74 ** with an out
31820 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72 -of-memory error
31830 20 6e 6f 77 2e 20 20 54 69 63 6b 65 74 20 23 33 now. Ticket #3
31840 37 36 31 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 761.. */. if(
31850 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 44 62 50 MEMDB ){. DbP
31860 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20 20 20 72 age *pNew;. r
31870 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
31880 41 63 71 75 69 72 65 28 70 50 61 67 65 72 2c 20 Acquire(pPager,
31890 6f 72 69 67 50 67 6e 6f 2c 20 26 70 4e 65 77 2c origPgno, &pNew,
318a0 20 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 1);. if( rc!
318b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
318c0 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 sqlite3Pcach
318d0 65 4d 6f 76 65 28 70 50 67 2c 20 6f 72 69 67 50 eMove(pPg, origP
318e0 67 6e 6f 29 3b 0a 20 20 20 20 20 20 72 65 74 75 gno);. retu
318f0 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
31900 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
31910 65 66 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20 ef(pNew);. }..
31920 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
31930 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a K;.}.#endif../*.
31940 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e ** Return a poin
31950 74 65 72 20 74 6f 20 74 68 65 20 64 61 74 61 20 ter to the data
31960 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 for the specifie
31970 64 20 70 61 67 65 2e 0a 2a 2f 0a 76 6f 69 64 20 d page..*/.void
31980 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 *sqlite3PagerGet
31990 44 61 74 61 28 44 62 50 61 67 65 20 2a 70 50 67 Data(DbPage *pPg
319a0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 ){. assert( pPg
319b0 2d 3e 6e 52 65 66 3e 30 20 7c 7c 20 70 50 67 2d ->nRef>0 || pPg-
319c0 3e 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29 >pPager->memDb )
319d0 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e ;. return pPg->
319e0 70 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pData;.}../*.**
319f0 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
31a00 20 74 6f 20 74 68 65 20 50 61 67 65 72 2e 6e 45 to the Pager.nE
31a10 78 74 72 61 20 62 79 74 65 73 20 6f 66 20 22 65 xtra bytes of "e
31a20 78 74 72 61 22 20 73 70 61 63 65 20 0a 2a 2a 20 xtra" space .**
31a30 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 allocated along
31a40 77 69 74 68 20 74 68 65 20 73 70 65 63 69 66 69 with the specifi
31a50 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 76 6f 69 64 ed page..*/.void
31a60 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *sqlite3PagerGe
31a70 74 45 78 74 72 61 28 44 62 50 61 67 65 20 2a 70 tExtra(DbPage *p
31a80 50 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 Pg){. return pP
31a90 67 2d 3e 70 45 78 74 72 61 3b 0a 7d 0a 0a 2f 2a g->pExtra;.}../*
31aa0 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20 .** Get/set the
31ab0 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 locking-mode for
31ac0 20 74 68 69 73 20 70 61 67 65 72 2e 20 50 61 72 this pager. Par
31ad0 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d 75 73 ameter eMode mus
31ae0 74 20 62 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 50 t be one.** of P
31af0 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
31b00 5f 51 55 45 52 59 2c 20 50 41 47 45 52 5f 4c 4f _QUERY, PAGER_LO
31b10 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c CKINGMODE_NORMAL
31b20 20 6f 72 20 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f or .** PAGER_LO
31b30 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 CKINGMODE_EXCLUS
31b40 49 56 45 2e 20 49 66 20 74 68 65 20 70 61 72 61 IVE. If the para
31b50 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 5f 51 55 meter is not _QU
31b60 45 52 59 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 ERY, then.** the
31b70 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 69 73 locking-mode is
31b80 20 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 set to the valu
31b90 65 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a 0a e specified..**.
31ba0 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 ** The returned
31bb0 76 61 6c 75 65 20 69 73 20 65 69 74 68 65 72 20 value is either
31bc0 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 PAGER_LOCKINGMOD
31bd0 45 5f 4e 4f 52 4d 41 4c 20 6f 72 0a 2a 2a 20 50 E_NORMAL or.** P
31be0 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
31bf0 5f 45 58 43 4c 55 53 49 56 45 2c 20 69 6e 64 69 _EXCLUSIVE, indi
31c00 63 61 74 69 6e 67 20 74 68 65 20 63 75 72 72 65 cating the curre
31c10 6e 74 20 28 70 6f 73 73 69 62 6c 79 20 75 70 64 nt (possibly upd
31c20 61 74 65 64 29 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 ated).** locking
31c30 2d 6d 6f 64 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 -mode..*/.int sq
31c40 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e lite3PagerLockin
31c50 67 4d 6f 64 65 28 50 61 67 65 72 20 2a 70 50 61 gMode(Pager *pPa
31c60 67 65 72 2c 20 69 6e 74 20 65 4d 6f 64 65 29 7b ger, int eMode){
31c70 0a 20 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65 . assert( eMode
31c80 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d ==PAGER_LOCKINGM
31c90 4f 44 45 5f 51 55 45 52 59 0a 20 20 20 20 20 20 ODE_QUERY.
31ca0 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d || eMode==
31cb0 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 PAGER_LOCKINGMOD
31cc0 45 5f 4e 4f 52 4d 41 4c 0a 20 20 20 20 20 20 20 E_NORMAL.
31cd0 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 || eMode==P
31ce0 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
31cf0 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 _EXCLUSIVE );.
31d00 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f assert( PAGER_LO
31d10 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 3c CKINGMODE_QUERY<
31d20 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 0 );. assert( P
31d30 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
31d40 5f 4e 4f 52 4d 41 4c 3e 3d 30 20 26 26 20 50 41 _NORMAL>=0 && PA
31d50 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
31d60 45 58 43 4c 55 53 49 56 45 3e 3d 30 20 29 3b 0a EXCLUSIVE>=0 );.
31d70 20 20 69 66 28 20 65 4d 6f 64 65 3e 3d 30 20 26 if( eMode>=0 &
31d80 26 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 & !pPager->tempF
31d90 69 6c 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65 ile ){. pPage
31da0 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
31db0 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 = (u8)eMode;.
31dc0 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 }. return (int)
31dd0 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
31de0 65 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 eMode;.}../*.**
31df0 47 65 74 2f 73 65 74 20 74 68 65 20 6a 6f 75 72 Get/set the jour
31e00 6e 61 6c 2d 6d 6f 64 65 20 66 6f 72 20 74 68 69 nal-mode for thi
31e10 73 20 70 61 67 65 72 2e 20 50 61 72 61 6d 65 74 s pager. Paramet
31e20 65 72 20 65 4d 6f 64 65 20 6d 75 73 74 20 62 65 er eMode must be
31e30 20 6f 6e 65 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 one of:.**.**
31e40 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d PAGER_JOURNALM
31e50 4f 44 45 5f 51 55 45 52 59 0a 2a 2a 20 20 20 20 ODE_QUERY.**
31e60 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
31e70 45 5f 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 50 E_DELETE.** P
31e80 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
31e90 5f 54 52 55 4e 43 41 54 45 0a 2a 2a 20 20 20 20 _TRUNCATE.**
31ea0 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
31eb0 45 5f 50 45 52 53 49 53 54 0a 2a 2a 20 20 20 20 E_PERSIST.**
31ec0 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
31ed0 45 5f 4f 46 46 0a 2a 2a 20 20 20 20 50 41 47 45 E_OFF.** PAGE
31ee0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 R_JOURNALMODE_ME
31ef0 4d 4f 52 59 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 MORY.**.** If th
31f00 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e e parameter is n
31f10 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e 20 ot _QUERY, then
31f20 74 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 the journal_mode
31f30 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 0a 2a is set to the.*
31f40 2a 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65 * value specifie
31f50 64 20 69 66 20 74 68 65 20 63 68 61 6e 67 65 20 d if the change
31f60 69 73 20 61 6c 6c 6f 77 65 64 2e 20 20 54 68 65 is allowed. The
31f70 20 63 68 61 6e 67 65 20 69 73 20 64 69 73 61 6c change is disal
31f80 6c 6f 77 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 lowed.** for the
31f90 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 61 73 6f following reaso
31fa0 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 ns:.**.** * A
31fb0 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
31fc0 62 61 73 65 20 63 61 6e 20 6f 6e 6c 79 20 68 61 base can only ha
31fd0 76 65 20 69 74 73 20 6a 6f 75 72 6e 61 6c 5f 6d ve its journal_m
31fe0 6f 64 65 20 73 65 74 20 74 6f 20 5f 4f 46 46 0a ode set to _OFF.
31ff0 2a 2a 20 20 20 20 20 20 6f 72 20 5f 4d 45 4d 4f ** or _MEMO
32000 52 59 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 54 RY..**.** * T
32010 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 he journal mode
32020 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 may not be chang
32030 65 64 20 77 68 69 6c 65 20 61 20 74 72 61 6e 73 ed while a trans
32040 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 action is active
32050 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 ..**.** The retu
32060 72 6e 65 64 20 69 6e 64 69 63 61 74 65 20 74 68 rned indicate th
32070 65 20 63 75 72 72 65 6e 74 20 28 70 6f 73 73 69 e current (possi
32080 62 6c 79 20 75 70 64 61 74 65 64 29 20 6a 6f 75 bly updated) jou
32090 72 6e 61 6c 2d 6d 6f 64 65 2e 0a 2a 2f 0a 69 6e rnal-mode..*/.in
320a0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f t sqlite3PagerJo
320b0 75 72 6e 61 6c 4d 6f 64 65 28 50 61 67 65 72 20 urnalMode(Pager
320c0 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 4d 6f *pPager, int eMo
320d0 64 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 65 de){. assert( e
320e0 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
320f0 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a 20 20 NALMODE_QUERY.
32100 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f || eMo
32110 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
32120 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 20 20 20 LMODE_DELETE.
32130 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 || eMod
32140 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
32150 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a 20 20 MODE_TRUNCATE.
32160 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f || eMo
32170 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
32180 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20 LMODE_PERSIST.
32190 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f || eMo
321a0 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
321b0 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 20 20 20 LMODE_OFF .
321c0 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d || eMode=
321d0 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
321e0 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 20 20 61 DE_MEMORY );. a
321f0 73 73 65 72 74 28 20 50 41 47 45 52 5f 4a 4f 55 ssert( PAGER_JOU
32200 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 3c 30 RNALMODE_QUERY<0
32210 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f 64 65 3e );. if( eMode>
32220 3d 30 0a 20 20 20 26 26 20 28 21 4d 45 4d 44 42 =0. && (!MEMDB
32230 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 || eMode==PAGER
32240 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d _JOURNALMODE_MEM
32250 4f 52 59 20 0a 20 20 20 20 20 20 20 20 20 20 20 ORY .
32260 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 || eMode==PAG
32270 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f ER_JOURNALMODE_O
32280 46 46 29 0a 20 20 20 26 26 20 21 70 50 61 67 65 FF). && !pPage
32290 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 0a 20 20 r->dbModified.
322a0 20 26 26 20 28 21 69 73 4f 70 65 6e 28 70 50 61 && (!isOpen(pPa
322b0 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 30 3d 3d ger->jfd) || 0==
322c0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
322d0 66 66 29 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 ff). ){. if(
322e0 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
322f0 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 73 71 jfd) ){. sq
32300 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 lite3OsClose(pPa
32310 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 7d ger->jfd);. }
32320 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 . pPager->jou
32330 72 6e 61 6c 4d 6f 64 65 20 3d 20 28 75 38 29 65 rnalMode = (u8)e
32340 4d 6f 64 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 Mode;. }. retu
32350 72 6e 20 28 69 6e 74 29 70 50 61 67 65 72 2d 3e rn (int)pPager->
32360 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 0a 7d 0a 0a journalMode;.}..
32370 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 /*.** Get/set th
32380 65 20 73 69 7a 65 2d 6c 69 6d 69 74 20 75 73 65 e size-limit use
32390 64 20 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 d for persistent
323a0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a journal files..
323b0 2a 2a 0a 2a 2a 20 53 65 74 74 69 6e 67 20 74 68 **.** Setting th
323c0 65 20 73 69 7a 65 20 6c 69 6d 69 74 20 74 6f 20 e size limit to
323d0 2d 31 20 6d 65 61 6e 73 20 6e 6f 20 6c 69 6d 69 -1 means no limi
323e0 74 20 69 73 20 65 6e 66 6f 72 63 65 64 2e 0a 2a t is enforced..*
323f0 2a 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 * An attempt to
32400 73 65 74 20 61 20 6c 69 6d 69 74 20 73 6d 61 6c set a limit smal
32410 6c 65 72 20 74 68 61 6e 20 2d 31 20 69 73 20 61 ler than -1 is a
32420 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 69 36 34 20 73 no-op..*/.i64 s
32430 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e qlite3PagerJourn
32440 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61 67 65 alSizeLimit(Page
32450 72 20 2a 70 50 61 67 65 72 2c 20 69 36 34 20 69 r *pPager, i64 i
32460 4c 69 6d 69 74 29 7b 0a 20 20 69 66 28 20 69 4c Limit){. if( iL
32470 69 6d 69 74 3e 3d 2d 31 20 29 7b 0a 20 20 20 20 imit>=-1 ){.
32480 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 pPager->journalS
32490 69 7a 65 4c 69 6d 69 74 20 3d 20 69 4c 69 6d 69 izeLimit = iLimi
324a0 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 t;. }. return
324b0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 pPager->journalS
324c0 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a 0a 2f 2a 0a izeLimit;.}../*.
324d0 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e ** Return a poin
324e0 74 65 72 20 74 6f 20 74 68 65 20 70 50 61 67 65 ter to the pPage
324f0 72 2d 3e 70 42 61 63 6b 75 70 20 76 61 72 69 61 r->pBackup varia
32500 62 6c 65 2e 20 54 68 65 20 62 61 63 6b 75 70 20 ble. The backup
32510 6d 6f 64 75 6c 65 0a 2a 2a 20 69 6e 20 62 61 63 module.** in bac
32520 6b 75 70 2e 63 20 6d 61 69 6e 74 61 69 6e 73 20 kup.c maintains
32530 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 the content of t
32540 68 69 73 20 76 61 72 69 61 62 6c 65 2e 20 54 68 his variable. Th
32550 69 73 20 6d 6f 64 75 6c 65 0a 2a 2a 20 75 73 65 is module.** use
32560 73 20 69 74 20 6f 70 61 71 75 65 6c 79 20 61 73 s it opaquely as
32570 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 an argument to
32580 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 sqlite3BackupRes
32590 74 61 72 74 28 29 20 61 6e 64 0a 2a 2a 20 73 71 tart() and.** sq
325a0 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 lite3BackupUpdat
325b0 65 28 29 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 71 6c e() only..*/.sql
325c0 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 73 71 ite3_backup **sq
325d0 6c 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 lite3PagerBackup
325e0 50 74 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 Ptr(Pager *pPage
325f0 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 26 70 50 r){. return &pP
32600 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 3b 0a 7d ager->pBackup;.}
32610 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 ..#endif /* SQLI
32620 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 20 2a TE_OMIT_DISKIO *
32630 2f 0a /.