0000: 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
0010: 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
0020: 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
0030: 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
0040: 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
0050: 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
0060: 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
0070: 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
0080: 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
0090: 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
00a0: 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
00b0: 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
00c0: 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
00d0: 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
00e0: 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
00f0: 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
0100: 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
0110: 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
0120: 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0170: 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 ******.** This i
0180: 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 s the implementa
0190: 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 65 tion of the page
01a0: 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d cache subsystem
01b0: 20 6f 72 20 22 70 61 67 65 72 22 2e 0a 2a 2a 20 or "pager"..**
01c0: 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 69 73 .** The pager is
01d0: 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 used to access
01e0: 61 20 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 a database disk
01f0: 66 69 6c 65 2e 20 20 49 74 20 69 6d 70 6c 65 6d file. It implem
0200: 65 6e 74 73 0a 2a 2a 20 61 74 6f 6d 69 63 20 63 ents.** atomic c
0210: 6f 6d 6d 69 74 20 61 6e 64 20 72 6f 6c 6c 62 61 ommit and rollba
0220: 63 6b 20 74 68 72 6f 75 67 68 20 74 68 65 20 75 ck through the u
0230: 73 65 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 se of a journal
0240: 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 file that.** is
0250: 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68 separate from th
0260: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
0270: 20 20 54 68 65 20 70 61 67 65 72 20 61 6c 73 6f The pager also
0280: 20 69 6d 70 6c 65 6d 65 6e 74 73 20 66 69 6c 65 implements file
0290: 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 70 .** locking to p
02a0: 72 65 76 65 6e 74 20 74 77 6f 20 70 72 6f 63 65 revent two proce
02b0: 73 73 65 73 20 66 72 6f 6d 20 77 72 69 74 69 6e sses from writin
02c0: 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 g the same datab
02d0: 61 73 65 0a 2a 2a 20 66 69 6c 65 20 73 69 6d 75 ase.** file simu
02e0: 6c 74 61 6e 65 6f 75 73 6c 79 2c 20 6f 72 20 6f ltaneously, or o
02f0: 6e 65 20 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 ne process from
0300: 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 reading the data
0310: 62 61 73 65 20 77 68 69 6c 65 0a 2a 2a 20 61 6e base while.** an
0320: 6f 74 68 65 72 20 69 73 20 77 72 69 74 69 6e 67 other is writing
0330: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
0340: 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a ITE_OMIT_DISKIO.
0350: 23 69 6e 63 6c 75 64 65 20 22 73 71 6c 69 74 65 #include "sqlite
0360: 49 6e 74 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 Int.h".#include
0370: 22 77 61 6c 2e 68 22 0a 0a 0a 2f 2a 2a 2a 2a 2a "wal.h".../*****
0380: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e ************** N
0390: 4f 54 45 53 20 4f 4e 20 54 48 45 20 44 45 53 49 OTES ON THE DESI
03a0: 47 4e 20 4f 46 20 54 48 45 20 50 41 47 45 52 20 GN OF THE PAGER
03b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
03c0: 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
03d0: 68 69 73 20 63 6f 6d 6d 65 6e 74 20 62 6c 6f 63 his comment bloc
03e0: 6b 20 64 65 73 63 72 69 62 65 73 20 69 6e 76 61 k describes inva
03f0: 72 69 61 6e 74 73 20 74 68 61 74 20 68 6f 6c 64 riants that hold
0400: 20 77 68 65 6e 20 75 73 69 6e 67 20 61 20 72 6f when using a ro
0410: 6c 6c 62 61 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61 llback.** journa
0420: 6c 2e 20 20 54 68 65 73 65 20 69 6e 76 61 72 69 l. These invari
0430: 61 6e 74 73 20 64 6f 20 6e 6f 74 20 61 70 70 6c ants do not appl
0440: 79 20 66 6f 72 20 6a 6f 75 72 6e 61 6c 5f 6d 6f y for journal_mo
0450: 64 65 3d 57 41 4c 2c 0a 2a 2a 20 6a 6f 75 72 6e de=WAL,.** journ
0460: 61 6c 5f 6d 6f 64 65 3d 4d 45 4d 4f 52 59 2c 20 al_mode=MEMORY,
0470: 6f 72 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d or journal_mode=
0480: 4f 46 46 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 OFF..**.** Withi
0490: 6e 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 20 62 n this comment b
04a0: 6c 6f 63 6b 2c 20 61 20 70 61 67 65 20 69 73 20 lock, a page is
04b0: 64 65 65 6d 65 64 20 74 6f 20 68 61 76 65 20 62 deemed to have b
04c0: 65 65 6e 20 73 79 6e 63 65 64 0a 2a 2a 20 61 75 een synced.** au
04d0: 74 6f 6d 61 74 69 63 61 6c 6c 79 20 61 73 20 73 tomatically as s
04e0: 6f 6f 6e 20 61 73 20 69 74 20 69 73 20 77 72 69 oon as it is wri
04f0: 74 74 65 6e 20 77 68 65 6e 20 50 52 41 47 4d 41 tten when PRAGMA
0500: 20 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 4f 46 46 synchronous=OFF
0510: 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 ..** Otherwise,
0520: 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 the page is not
0530: 73 79 6e 63 65 64 20 75 6e 74 69 6c 20 74 68 65 synced until the
0540: 20 78 53 79 6e 63 20 6d 65 74 68 6f 64 20 6f 66 xSync method of
0550: 20 74 68 65 20 56 46 53 0a 2a 2a 20 69 73 20 63 the VFS.** is c
0560: 61 6c 6c 65 64 20 73 75 63 63 65 73 73 66 75 6c alled successful
0570: 6c 79 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 63 ly on the file c
0580: 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 70 61 ontaining the pa
0590: 67 65 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 69 6e 69 ge..**.** Defini
05a0: 74 69 6f 6e 3a 20 20 41 20 70 61 67 65 20 6f 66 tion: A page of
05b0: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
05c0: 6c 65 20 69 73 20 73 61 69 64 20 74 6f 20 62 65 le is said to be
05d0: 20 22 6f 76 65 72 77 72 69 74 65 61 62 6c 65 22 "overwriteable"
05e0: 20 69 66 0a 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f if.** one or mo
05f0: 72 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 re of the follow
0600: 69 6e 67 20 61 72 65 20 74 72 75 65 20 61 62 6f ing are true abo
0610: 75 74 20 74 68 65 20 70 61 67 65 3a 0a 2a 2a 20 ut the page:.**
0620: 0a 2a 2a 20 20 20 20 20 28 61 29 20 20 54 68 65 .** (a) The
0630: 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74 65 6e original conten
0640: 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 73 t of the page as
0650: 20 69 74 20 77 61 73 20 61 74 20 74 68 65 20 62 it was at the b
0660: 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 20 eginning of.**
0670: 20 20 20 20 20 20 20 20 74 68 65 20 74 72 61 6e the tran
0680: 73 61 63 74 69 6f 6e 20 68 61 73 20 62 65 65 6e saction has been
0690: 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
06a0: 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
06b0: 61 6c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 al and.**
06c0: 20 20 20 73 79 6e 63 65 64 2e 0a 2a 2a 20 0a 2a synced..** .*
06d0: 2a 20 20 20 20 20 28 62 29 20 20 54 68 65 20 70 * (b) The p
06e0: 61 67 65 20 77 61 73 20 61 20 66 72 65 65 6c 69 age was a freeli
06f0: 73 74 20 6c 65 61 66 20 70 61 67 65 20 61 74 20 st leaf page at
0700: 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 the start of the
0710: 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a transaction..**
0720: 20 0a 2a 2a 20 20 20 20 20 28 63 29 20 20 54 68 .** (c) Th
0730: 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 e page number is
0740: 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 greater than th
0750: 65 20 6c 61 72 67 65 73 74 20 70 61 67 65 20 74 e largest page t
0760: 68 61 74 20 65 78 69 73 74 65 64 20 69 6e 0a 2a hat existed in.*
0770: 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 64 * the d
0780: 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 atabase file at
0790: 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 the start of the
07a0: 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a transaction..**
07b0: 20 0a 2a 2a 20 28 31 29 20 41 20 70 61 67 65 20 .** (1) A page
07c0: 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
07d0: 66 69 6c 65 20 69 73 20 6e 65 76 65 72 20 6f 76 file is never ov
07e0: 65 72 77 72 69 74 74 65 6e 20 75 6e 6c 65 73 73 erwritten unless
07f0: 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20 20 one of the.**
0800: 20 20 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 following are
0810: 20 74 72 75 65 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 true:.** .**
0820: 20 20 28 61 29 20 54 68 65 20 70 61 67 65 20 61 (a) The page a
0830: 6e 64 20 61 6c 6c 20 6f 74 68 65 72 20 70 61 67 nd all other pag
0840: 65 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 73 es on the same s
0850: 65 63 74 6f 72 20 61 72 65 20 6f 76 65 72 77 72 ector are overwr
0860: 69 74 65 61 62 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 iteable..** .**
0870: 20 20 20 20 28 62 29 20 54 68 65 20 61 74 6f 6d (b) The atom
0880: 69 63 20 70 61 67 65 20 77 72 69 74 65 20 6f 70 ic page write op
0890: 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 65 6e timization is en
08a0: 61 62 6c 65 64 2c 20 61 6e 64 20 74 68 65 20 65 abled, and the e
08b0: 6e 74 69 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 ntire.**
08c0: 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 74 68 transaction oth
08d0: 65 72 20 74 68 61 6e 20 74 68 65 20 75 70 64 61 er than the upda
08e0: 74 65 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 te of the transa
08f0: 63 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a 2a ction sequence.*
0900: 2a 20 20 20 20 20 20 20 20 20 6e 75 6d 62 65 72 * number
0910: 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73 consists of a s
0920: 69 6e 67 6c 65 20 70 61 67 65 20 63 68 61 6e 67 ingle page chang
0930: 65 2e 0a 2a 2a 20 0a 2a 2a 20 28 32 29 20 54 68 e..** .** (2) Th
0940: 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 70 e content of a p
0950: 61 67 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f age written into
0960: 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
0970: 75 72 6e 61 6c 20 65 78 61 63 74 6c 79 20 6d 61 urnal exactly ma
0980: 74 63 68 65 73 0a 2a 2a 20 20 20 20 20 62 6f 74 tches.** bot
0990: 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 69 6e h the content in
09a0: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 68 the database wh
09b0: 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 en the rollback
09c0: 6a 6f 75 72 6e 61 6c 20 77 61 73 20 77 72 69 74 journal was writ
09d0: 74 65 6e 0a 2a 2a 20 20 20 20 20 61 6e 64 20 74 ten.** and t
09e0: 68 65 20 63 6f 6e 74 65 6e 74 20 69 6e 20 74 68 he content in th
09f0: 65 20 64 61 74 61 62 61 73 65 20 61 74 20 74 68 e database at th
0a00: 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 e beginning of t
0a10: 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 20 20 he current.**
0a20: 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a transaction..*
0a30: 2a 20 0a 2a 2a 20 28 33 29 20 57 72 69 74 65 73 * .** (3) Writes
0a40: 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
0a50: 20 66 69 6c 65 20 61 72 65 20 61 6e 20 69 6e 74 file are an int
0a60: 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 eger multiple of
0a70: 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 0a 2a the page size.*
0a80: 2a 20 20 20 20 20 69 6e 20 6c 65 6e 67 74 68 20 * in length
0a90: 61 6e 64 20 61 72 65 20 61 6c 69 67 6e 65 64 20 and are aligned
0aa0: 6f 6e 20 61 20 70 61 67 65 20 62 6f 75 6e 64 61 on a page bounda
0ab0: 72 79 2e 0a 2a 2a 20 0a 2a 2a 20 28 34 29 20 52 ry..** .** (4) R
0ac0: 65 61 64 73 20 66 72 6f 6d 20 74 68 65 20 64 61 eads from the da
0ad0: 74 61 62 61 73 65 20 66 69 6c 65 20 61 72 65 20 tabase file are
0ae0: 65 69 74 68 65 72 20 61 6c 69 67 6e 65 64 20 6f either aligned o
0af0: 6e 20 61 20 70 61 67 65 20 62 6f 75 6e 64 61 72 n a page boundar
0b00: 79 20 61 6e 64 0a 2a 2a 20 20 20 20 20 61 6e 20 y and.** an
0b10: 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 integer multiple
0b20: 20 6f 66 20 74 68 65 20 70 61 67 65 20 73 69 7a of the page siz
0b30: 65 20 69 6e 20 6c 65 6e 67 74 68 20 6f 72 20 61 e in length or a
0b40: 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 re taken from th
0b50: 65 0a 2a 2a 20 20 20 20 20 66 69 72 73 74 20 31 e.** first 1
0b60: 30 30 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 00 bytes of the
0b70: 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
0b80: 2a 20 0a 2a 2a 20 28 35 29 20 41 6c 6c 20 77 72 * .** (5) All wr
0b90: 69 74 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 ites to the data
0ba0: 62 61 73 65 20 66 69 6c 65 20 61 72 65 20 73 79 base file are sy
0bb0: 6e 63 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 nced prior to th
0bc0: 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
0bd0: 61 6c 0a 2a 2a 20 20 20 20 20 62 65 69 6e 67 20 al.** being
0be0: 64 65 6c 65 74 65 64 2c 20 74 72 75 6e 63 61 74 deleted, truncat
0bf0: 65 64 2c 20 6f 72 20 7a 65 72 6f 65 64 2e 0a 2a ed, or zeroed..*
0c00: 2a 20 0a 2a 2a 20 28 36 29 20 49 66 20 61 20 6d * .** (6) If a m
0c10: 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
0c20: 6c 65 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e le is used, then
0c30: 20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 74 all writes to t
0c40: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
0c50: 0a 2a 2a 20 20 20 20 20 61 72 65 20 73 79 6e 63 .** are sync
0c60: 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 ed prior to the
0c70: 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 62 master journal b
0c80: 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 2a 2a eing deleted..**
0c90: 20 0a 2a 2a 20 44 65 66 69 6e 69 74 69 6f 6e 3a .** Definition:
0ca0: 20 54 77 6f 20 64 61 74 61 62 61 73 65 73 20 28 Two databases (
0cb0: 6f 72 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 or the same data
0cc0: 62 61 73 65 20 61 74 20 74 77 6f 20 70 6f 69 6e base at two poin
0cd0: 74 73 20 69 74 20 74 69 6d 65 29 0a 2a 2a 20 61 ts it time).** a
0ce0: 72 65 20 73 61 69 64 20 74 6f 20 62 65 20 22 6c re said to be "l
0cf0: 6f 67 69 63 61 6c 6c 79 20 65 71 75 69 76 61 6c ogically equival
0d00: 65 6e 74 22 20 69 66 20 74 68 65 79 20 67 69 76 ent" if they giv
0d10: 65 20 74 68 65 20 73 61 6d 65 20 61 6e 73 77 65 e the same answe
0d20: 72 20 74 6f 0a 2a 2a 20 61 6c 6c 20 71 75 65 72 r to.** all quer
0d30: 69 65 73 2e 20 20 4e 6f 74 65 20 69 6e 20 70 61 ies. Note in pa
0d40: 72 74 69 63 75 6c 61 72 20 74 68 65 20 63 6f 6e rticular the con
0d50: 74 65 6e 74 20 6f 66 20 66 72 65 65 6c 69 73 74 tent of freelist
0d60: 20 6c 65 61 66 0a 2a 2a 20 70 61 67 65 73 20 63 leaf.** pages c
0d70: 61 6e 20 62 65 20 63 68 61 6e 67 65 64 20 61 72 an be changed ar
0d80: 62 69 74 61 72 69 6c 79 20 77 69 74 68 6f 75 74 bitarily without
0d90: 20 65 66 66 65 63 74 69 6e 67 20 74 68 65 20 6c effecting the l
0da0: 6f 67 69 63 61 6c 20 65 71 75 69 76 61 6c 65 6e ogical equivalen
0db0: 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 ce.** of the dat
0dc0: 61 62 61 73 65 2e 0a 2a 2a 20 0a 2a 2a 20 28 37 abase..** .** (7
0dd0: 29 20 41 74 20 61 6e 79 20 74 69 6d 65 2c 20 69 ) At any time, i
0de0: 66 20 61 6e 79 20 73 75 62 73 65 74 2c 20 69 6e f any subset, in
0df0: 63 6c 75 64 69 6e 67 20 74 68 65 20 65 6d 70 74 cluding the empt
0e00: 79 20 73 65 74 20 61 6e 64 20 74 68 65 20 74 6f y set and the to
0e10: 74 61 6c 20 73 65 74 2c 0a 2a 2a 20 20 20 20 20 tal set,.**
0e20: 6f 66 20 74 68 65 20 75 6e 73 79 6e 63 65 64 20 of the unsynced
0e30: 63 68 61 6e 67 65 73 20 74 6f 20 61 20 72 6f 6c changes to a rol
0e40: 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 61 72 lback journal ar
0e50: 65 20 72 65 6d 6f 76 65 64 20 61 6e 64 20 74 68 e removed and th
0e60: 65 20 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 e .** journa
0e70: 6c 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b l is rolled back
0e80: 2c 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 , the resulting
0e90: 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 database file wi
0ea0: 6c 6c 20 62 65 20 6c 6f 67 69 63 61 6c 0a 2a 2a ll be logical.**
0eb0: 20 20 20 20 20 65 71 75 69 76 61 6c 65 6e 74 20 equivalent
0ec0: 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
0ed0: 66 69 6c 65 20 61 74 20 74 68 65 20 62 65 67 69 file at the begi
0ee0: 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 74 72 61 nning of the tra
0ef0: 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a 2a 2a nsaction..** .**
0f00: 20 28 38 29 20 57 68 65 6e 20 61 20 74 72 61 6e (8) When a tran
0f10: 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 saction is rolle
0f20: 64 20 62 61 63 6b 2c 20 74 68 65 20 78 54 72 75 d back, the xTru
0f30: 6e 63 61 74 65 20 6d 65 74 68 6f 64 20 6f 66 20 ncate method of
0f40: 74 68 65 20 56 46 53 0a 2a 2a 20 20 20 20 20 69 the VFS.** i
0f50: 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 73 74 s called to rest
0f60: 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ore the database
0f70: 20 66 69 6c 65 20 74 6f 20 74 68 65 20 73 61 6d file to the sam
0f80: 65 20 73 69 7a 65 20 69 74 20 77 61 73 20 61 74 e size it was at
0f90: 0a 2a 2a 20 20 20 20 20 74 68 65 20 62 65 67 69 .** the begi
0fa0: 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 74 72 61 nning of the tra
0fb0: 6e 73 61 63 74 69 6f 6e 2e 20 20 28 49 6e 20 73 nsaction. (In s
0fc0: 6f 6d 65 20 56 46 53 65 73 2c 20 74 68 65 20 78 ome VFSes, the x
0fd0: 54 72 75 6e 63 61 74 65 0a 2a 2a 20 20 20 20 20 Truncate.**
0fe0: 6d 65 74 68 6f 64 20 69 73 20 61 20 6e 6f 2d 6f method is a no-o
0ff0: 70 2c 20 62 75 74 20 74 68 61 74 20 64 6f 65 73 p, but that does
1000: 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 not change the
1010: 66 61 63 74 20 74 68 65 20 53 51 4c 69 74 65 20 fact the SQLite
1020: 77 69 6c 6c 0a 2a 2a 20 20 20 20 20 69 6e 76 6f will.** invo
1030: 6b 65 20 69 74 2e 29 0a 2a 2a 20 0a 2a 2a 20 28 ke it.).** .** (
1040: 39 29 20 57 68 65 6e 65 76 65 72 20 74 68 65 20 9) Whenever the
1050: 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 database file is
1060: 20 6d 6f 64 69 66 69 65 64 2c 20 61 74 20 6c 65 modified, at le
1070: 61 73 74 20 6f 6e 65 20 62 69 74 20 69 6e 20 74 ast one bit in t
1080: 68 65 20 72 61 6e 67 65 0a 2a 2a 20 20 20 20 20 he range.**
1090: 6f 66 20 62 79 74 65 73 20 66 72 6f 6d 20 32 34 of bytes from 24
10a0: 20 74 68 72 6f 75 67 68 20 33 39 20 69 6e 63 6c through 39 incl
10b0: 75 73 69 76 65 20 77 69 6c 6c 20 62 65 20 63 68 usive will be ch
10c0: 61 6e 67 65 64 20 70 72 69 6f 72 20 74 6f 20 72 anged prior to r
10d0: 65 6c 65 61 73 69 6e 67 0a 2a 2a 20 20 20 20 20 eleasing.**
10e0: 74 68 65 20 45 58 43 4c 55 53 49 56 45 20 6c 6f the EXCLUSIVE lo
10f0: 63 6b 2c 20 74 68 75 73 20 73 69 67 6e 61 6c 69 ck, thus signali
1100: 6e 67 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 ng other connect
1110: 69 6f 6e 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 ions on the same
1120: 0a 2a 2a 20 20 20 20 20 64 61 74 61 62 61 73 65 .** database
1130: 20 74 6f 20 66 6c 75 73 68 20 74 68 65 69 72 20 to flush their
1140: 63 61 63 68 65 73 2e 0a 2a 2a 0a 2a 2a 20 28 31 caches..**.** (1
1150: 30 29 20 54 68 65 20 70 61 74 74 65 72 6e 20 6f 0) The pattern o
1160: 66 20 62 69 74 73 20 69 6e 20 62 79 74 65 73 20 f bits in bytes
1170: 32 34 20 74 68 72 6f 75 67 68 20 33 39 20 73 68 24 through 39 sh
1180: 61 6c 6c 20 6e 6f 74 20 72 65 70 65 61 74 20 69 all not repeat i
1190: 6e 20 6c 65 73 73 0a 2a 2a 20 20 20 20 20 20 74 n less.** t
11a0: 68 61 6e 20 6f 6e 65 20 62 69 6c 6c 69 6f 6e 20 han one billion
11b0: 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 2a 2a transactions..**
11c0: 0a 2a 2a 20 28 31 31 29 20 41 20 64 61 74 61 62 .** (11) A datab
11d0: 61 73 65 20 66 69 6c 65 20 69 73 20 77 65 6c 6c ase file is well
11e0: 2d 66 6f 72 6d 65 64 20 61 74 20 74 68 65 20 62 -formed at the b
11f0: 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 61 74 20 eginning and at
1200: 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 0a 2a the conclusion.*
1210: 2a 20 20 20 20 20 20 6f 66 20 65 76 65 72 79 20 * of every
1220: 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a transaction..**.
1230: 2a 2a 20 28 31 32 29 20 41 6e 20 45 58 43 4c 55 ** (12) An EXCLU
1240: 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 68 65 6c SIVE lock is hel
1250: 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 d on the databas
1260: 65 20 66 69 6c 65 20 77 68 65 6e 20 77 72 69 74 e file when writ
1270: 69 6e 67 20 74 6f 0a 2a 2a 20 20 20 20 20 20 74 ing to.** t
1280: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1290: 2e 0a 2a 2a 0a 2a 2a 20 28 31 33 29 20 41 20 53 ..**.** (13) A S
12a0: 48 41 52 45 44 20 6c 6f 63 6b 20 69 73 20 68 65 HARED lock is he
12b0: 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ld on the databa
12c0: 73 65 20 66 69 6c 65 20 77 68 69 6c 65 20 72 65 se file while re
12d0: 61 64 69 6e 67 20 61 6e 79 0a 2a 2a 20 20 20 20 ading any.**
12e0: 20 20 63 6f 6e 74 65 6e 74 20 6f 75 74 20 6f 66 content out of
12f0: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1300: 6c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a le..**.*********
1310: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1350: 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 *****/../*.** Ma
1360: 63 72 6f 73 20 66 6f 72 20 74 72 6f 75 62 6c 65 cros for trouble
1370: 73 68 6f 6f 74 69 6e 67 2e 20 20 4e 6f 72 6d 61 shooting. Norma
1380: 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 0a 2a lly turned off.*
1390: 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c 69 /.#if 0.int sqli
13a0: 74 65 33 50 61 67 65 72 54 72 61 63 65 3d 31 3b te3PagerTrace=1;
13b0: 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 /* True to ena
13c0: 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a 23 ble tracing */.#
13d0: 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65 define sqlite3De
13e0: 62 75 67 50 72 69 6e 74 66 20 70 72 69 6e 74 66 bugPrintf printf
13f0: 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52 .#define PAGERTR
1400: 41 43 45 28 58 29 20 20 20 20 20 69 66 28 20 73 ACE(X) if( s
1410: 71 6c 69 74 65 33 50 61 67 65 72 54 72 61 63 65 qlite3PagerTrace
1420: 20 29 7b 20 73 71 6c 69 74 65 33 44 65 62 75 67 ){ sqlite3Debug
1430: 50 72 69 6e 74 66 20 58 3b 20 7d 0a 23 65 6c 73 Printf X; }.#els
1440: 65 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 e.#define PAGERT
1450: 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a RACE(X).#endif..
1460: 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
1470: 69 6e 67 20 74 77 6f 20 6d 61 63 72 6f 73 20 61 ing two macros a
1480: 72 65 20 75 73 65 64 20 77 69 74 68 69 6e 20 74 re used within t
1490: 68 65 20 50 41 47 45 52 54 52 41 43 45 28 29 20 he PAGERTRACE()
14a0: 6d 61 63 72 6f 73 20 61 62 6f 76 65 0a 2a 2a 20 macros above.**
14b0: 74 6f 20 70 72 69 6e 74 20 6f 75 74 20 66 69 6c to print out fil
14c0: 65 2d 64 65 73 63 72 69 70 74 6f 72 73 2e 20 0a e-descriptors. .
14d0: 2a 2a 0a 2a 2a 20 50 41 47 45 52 49 44 28 29 20 **.** PAGERID()
14e0: 74 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 takes a pointer
14f0: 74 6f 20 61 20 50 61 67 65 72 20 73 74 72 75 63 to a Pager struc
1500: 74 20 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e t as its argumen
1510: 74 2e 20 54 68 65 0a 2a 2a 20 61 73 73 6f 63 69 t. The.** associ
1520: 61 74 65 64 20 66 69 6c 65 2d 64 65 73 63 72 69 ated file-descri
1530: 70 74 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 ptor is returned
1540: 2e 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 29 . FILEHANDLEID()
1550: 20 74 61 6b 65 73 20 61 6e 20 73 71 6c 69 74 65 takes an sqlite
1560: 33 5f 66 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 3_file.** struct
1570: 20 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e 74 as its argument
1580: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 ..*/.#define PAG
1590: 45 52 49 44 28 70 29 20 28 28 69 6e 74 29 28 70 ERID(p) ((int)(p
15a0: 2d 3e 66 64 29 29 0a 23 64 65 66 69 6e 65 20 46 ->fd)).#define F
15b0: 49 4c 45 48 41 4e 44 4c 45 49 44 28 66 64 29 20 ILEHANDLEID(fd)
15c0: 28 28 69 6e 74 29 66 64 29 0a 0a 2f 2a 0a 2a 2a ((int)fd)../*.**
15d0: 20 54 68 65 20 50 61 67 65 72 2e 65 53 74 61 74 The Pager.eStat
15e0: 65 20 76 61 72 69 61 62 6c 65 20 73 74 6f 72 65 e variable store
15f0: 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 27 73 s the current 's
1600: 74 61 74 65 27 20 6f 66 20 61 20 70 61 67 65 72 tate' of a pager
1610: 2e 20 41 0a 2a 2a 20 70 61 67 65 72 20 6d 61 79 . A.** pager may
1620: 20 62 65 20 69 6e 20 61 6e 79 20 6f 6e 65 20 6f be in any one o
1630: 66 20 74 68 65 20 73 65 76 65 6e 20 73 74 61 74 f the seven stat
1640: 65 73 20 73 68 6f 77 6e 20 69 6e 20 74 68 65 20 es shown in the
1650: 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 61 following.** sta
1660: 74 65 20 64 69 61 67 72 61 6d 2e 0a 2a 2a 0a 2a te diagram..**.*
1670: 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
1680: 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 45 OPE
1690: 4e 20 3c 2d 2d 2d 2d 2d 2d 2b 2d 2d 2d 2d 2d 2d N <------+------
16a0: 2b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 +.**
16b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c0: 20 20 7c 20 20 20 20 20 20 20 20 20 7c 20 20 20 | |
16d0: 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20 |.**
16e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16f0: 20 20 20 20 20 56 20 20 20 20 20 20 20 20 20 7c V |
1700: 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 |.**
1710: 20 20 20 20 20 20 20 20 20 2b 2d 2d 2d 2d 2d 2d +------
1720: 2d 2d 2d 3e 20 52 45 41 44 45 52 2d 2d 2d 2d 2d ---> READER-----
1730: 2d 2d 2b 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 --+ |.**
1740: 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 |
1750: 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 20 |
1760: 20 20 20 20 20 20 20 20 20 20 20 20 7c 0a 2a 2a |.**
1770: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
1780: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 56 20 V
1790: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
17a0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
17b0: 20 20 7c 3c 2d 2d 2d 2d 2d 2d 2d 57 52 49 54 45 |<-------WRITE
17c0: 52 5f 4c 4f 43 4b 45 44 2d 2d 2d 2d 2d 2d 3e 20 R_LOCKED------>
17d0: 45 52 52 4f 52 0a 2a 2a 20 20 20 20 20 20 20 20 ERROR.**
17e0: 20 20 20 20 20 20 20 7c 20 20 20 20 20 20 20 20 |
17f0: 20 20 20 20 20 20 7c 20 20 20 20 20 20 20 20 20 |
1800: 20 20 20 20 20 20 20 5e 20 20 0a 2a 2a 20 20 20 ^ .**
1810: 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 |
1820: 20 20 20 20 20 20 20 20 20 20 20 56 20 20 20 20 V
1830: 20 20 20 20 20 20 20 20 20 20 20 20 7c 0a 2a 2a |.**
1840: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
1850: 3c 2d 2d 2d 2d 2d 2d 57 52 49 54 45 52 5f 43 41 <------WRITER_CA
1860: 43 48 45 4d 4f 44 2d 2d 2d 2d 2d 2d 2d 2d 3e 7c CHEMOD-------->|
1870: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1880: 20 20 7c 20 20 20 20 20 20 20 20 20 20 20 20 20 |
1890: 20 7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 |
18a0: 20 20 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 |.**
18b0: 20 20 20 20 20 7c 20 20 20 20 20 20 20 20 20 20 |
18c0: 20 20 20 20 56 20 20 20 20 20 20 20 20 20 20 20 V
18d0: 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 20 |.**
18e0: 20 20 20 20 20 20 20 20 7c 3c 2d 2d 2d 2d 2d 2d |<------
18f0: 2d 57 52 49 54 45 52 5f 44 42 4d 4f 44 2d 2d 2d -WRITER_DBMOD---
1900: 2d 2d 2d 2d 2d 2d 2d 3e 7c 0a 2a 2a 20 20 20 20 ------->|.**
1910: 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 20 |
1920: 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 20 20 |
1930: 20 20 20 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 |.**
1940: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 |
1950: 20 20 20 20 20 20 20 20 20 20 20 20 20 56 20 20 V
1960: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 0a |.
1970: 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
1980: 20 2b 3c 2d 2d 2d 2d 2d 2d 57 52 49 54 45 52 5f +<------WRITER_
1990: 46 49 4e 49 53 48 45 44 2d 2d 2d 2d 2d 2d 2d 2d FINISHED--------
19a0: 3e 2b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4c 69 73 74 >+.**.**.** List
19b0: 20 6f 66 20 73 74 61 74 65 20 74 72 61 6e 73 69 of state transi
19c0: 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 43 20 tions and the C
19d0: 5b 66 75 6e 63 74 69 6f 6e 5d 20 74 68 61 74 20 [function] that
19e0: 70 65 72 66 6f 72 6d 73 20 65 61 63 68 3a 0a 2a performs each:.*
19f0: 2a 20 0a 2a 2a 20 20 20 4f 50 45 4e 20 20 20 20 * .** OPEN
1a00: 20 20 20 20 20 20 20 20 20 20 2d 3e 20 52 45 41 -> REA
1a10: 44 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 DER
1a20: 20 5b 73 71 6c 69 74 65 33 50 61 67 65 72 53 68 [sqlite3PagerSh
1a30: 61 72 65 64 4c 6f 63 6b 5d 0a 2a 2a 20 20 20 52 aredLock].** R
1a40: 45 41 44 45 52 20 20 20 20 20 20 20 20 20 20 20 EADER
1a50: 20 2d 3e 20 4f 50 45 4e 20 20 20 20 20 20 20 20 -> OPEN
1a60: 20 20 20 20 20 20 20 20 5b 70 61 67 65 72 5f 75 [pager_u
1a70: 6e 6c 6f 63 6b 5d 0a 2a 2a 0a 2a 2a 20 20 20 52 nlock].**.** R
1a80: 45 41 44 45 52 20 20 20 20 20 20 20 20 20 20 20 EADER
1a90: 20 2d 3e 20 57 52 49 54 45 52 5f 4c 4f 43 4b 45 -> WRITER_LOCKE
1aa0: 44 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 D [sqlite3
1ab0: 50 61 67 65 72 42 65 67 69 6e 5d 0a 2a 2a 20 20 PagerBegin].**
1ac0: 20 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 20 20 WRITER_LOCKED
1ad0: 20 20 20 2d 3e 20 57 52 49 54 45 52 5f 43 41 43 -> WRITER_CAC
1ae0: 48 45 4d 4f 44 20 20 20 20 20 5b 70 61 67 65 72 HEMOD [pager
1af0: 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 5d 0a 2a _open_journal].*
1b00: 2a 20 20 20 57 52 49 54 45 52 5f 43 41 43 48 45 * WRITER_CACHE
1b10: 4d 4f 44 20 20 20 2d 3e 20 57 52 49 54 45 52 5f MOD -> WRITER_
1b20: 44 42 4d 4f 44 20 20 20 20 20 20 20 20 5b 73 79 DBMOD [sy
1b30: 6e 63 4a 6f 75 72 6e 61 6c 5d 0a 2a 2a 20 20 20 ncJournal].**
1b40: 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 20 20 20 WRITER_DBMOD
1b50: 20 20 2d 3e 20 57 52 49 54 45 52 5f 46 49 4e 49 -> WRITER_FINI
1b60: 53 48 45 44 20 20 20 20 20 5b 73 71 6c 69 74 65 SHED [sqlite
1b70: 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 3PagerCommitPhas
1b80: 65 4f 6e 65 5d 0a 2a 2a 20 20 20 57 52 49 54 45 eOne].** WRITE
1b90: 52 5f 2a 2a 2a 20 20 20 20 20 20 20 20 2d 3e 20 R_*** ->
1ba0: 52 45 41 44 45 52 20 20 20 20 20 20 20 20 20 20 READER
1bb0: 20 20 20 20 5b 70 61 67 65 72 5f 65 6e 64 5f 74 [pager_end_t
1bc0: 72 61 6e 73 61 63 74 69 6f 6e 5d 0a 2a 2a 0a 2a ransaction].**.*
1bd0: 2a 20 20 20 57 52 49 54 45 52 5f 2a 2a 2a 20 20 * WRITER_***
1be0: 20 20 20 20 20 20 2d 3e 20 45 52 52 4f 52 20 20 -> ERROR
1bf0: 20 20 20 20 20 20 20 20 20 20 20 20 20 5b 70 61 [pa
1c00: 67 65 72 5f 65 72 72 6f 72 5d 0a 2a 2a 20 20 20 ger_error].**
1c10: 45 52 52 4f 52 20 20 20 20 20 20 20 20 20 20 20 ERROR
1c20: 20 20 2d 3e 20 4f 50 45 4e 20 20 20 20 20 20 20 -> OPEN
1c30: 20 20 20 20 20 20 20 20 20 5b 70 61 67 65 72 5f [pager_
1c40: 75 6e 6c 6f 63 6b 5d 0a 2a 2a 20 0a 2a 2a 0a 2a unlock].** .**.*
1c50: 2a 20 20 4f 50 45 4e 3a 0a 2a 2a 0a 2a 2a 20 20 * OPEN:.**.**
1c60: 20 20 54 68 65 20 70 61 67 65 72 20 73 74 61 72 The pager star
1c70: 74 73 20 75 70 20 69 6e 20 74 68 69 73 20 73 74 ts up in this st
1c80: 61 74 65 2e 20 4e 6f 74 68 69 6e 67 20 69 73 20 ate. Nothing is
1c90: 67 75 61 72 61 6e 74 65 65 64 20 69 6e 20 74 68 guaranteed in th
1ca0: 69 73 0a 2a 2a 20 20 20 20 73 74 61 74 65 20 2d is.** state -
1cb0: 20 74 68 65 20 66 69 6c 65 20 6d 61 79 20 6f 72 the file may or
1cc0: 20 6d 61 79 20 6e 6f 74 20 62 65 20 6c 6f 63 6b may not be lock
1cd0: 65 64 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 ed and the datab
1ce0: 61 73 65 20 73 69 7a 65 20 69 73 0a 2a 2a 20 20 ase size is.**
1cf0: 20 20 75 6e 6b 6e 6f 77 6e 2e 20 54 68 65 20 64 unknown. The d
1d00: 61 74 61 62 61 73 65 20 6d 61 79 20 6e 6f 74 20 atabase may not
1d10: 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 be read or writt
1d20: 65 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 4e en..**.** * N
1d30: 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 o read or write
1d40: 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 transaction is a
1d50: 63 74 69 76 65 2e 0a 2a 2a 20 20 20 20 2a 20 41 ctive..** * A
1d60: 6e 79 20 6c 6f 63 6b 2c 20 6f 72 20 6e 6f 20 6c ny lock, or no l
1d70: 6f 63 6b 20 61 74 20 61 6c 6c 2c 20 6d 61 79 20 ock at all, may
1d80: 62 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 be held on the d
1d90: 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
1da0: 20 20 20 20 2a 20 54 68 65 20 64 62 53 69 7a 65 * The dbSize
1db0: 2c 20 64 62 4f 72 69 67 53 69 7a 65 20 61 6e 64 , dbOrigSize and
1dc0: 20 64 62 46 69 6c 65 53 69 7a 65 20 76 61 72 69 dbFileSize vari
1dd0: 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 62 65 ables may not be
1de0: 20 74 72 75 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 trusted..**.**
1df0: 20 52 45 41 44 45 52 3a 0a 2a 2a 0a 2a 2a 20 20 READER:.**.**
1e00: 20 20 49 6e 20 74 68 69 73 20 73 74 61 74 65 20 In this state
1e10: 61 6c 6c 20 74 68 65 20 72 65 71 75 69 72 65 6d all the requirem
1e20: 65 6e 74 73 20 66 6f 72 20 72 65 61 64 69 6e 67 ents for reading
1e30: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6e the database in
1e40: 20 0a 2a 2a 20 20 20 20 72 6f 6c 6c 62 61 63 6b .** rollback
1e50: 20 28 6e 6f 6e 2d 57 41 4c 29 20 6d 6f 64 65 20 (non-WAL) mode
1e60: 61 72 65 20 6d 65 74 2e 20 55 6e 6c 65 73 73 20 are met. Unless
1e70: 74 68 65 20 70 61 67 65 72 20 69 73 20 28 6f 72 the pager is (or
1e80: 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 20 20 20 recently.**
1e90: 77 61 73 29 20 69 6e 20 65 78 63 6c 75 73 69 76 was) in exclusiv
1ea0: 65 2d 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 2c 20 e-locking mode,
1eb0: 61 20 75 73 65 72 2d 6c 65 76 65 6c 20 72 65 61 a user-level rea
1ec0: 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 d transaction is
1ed0: 20 0a 2a 2a 20 20 20 20 6f 70 65 6e 2e 20 54 68 .** open. Th
1ee0: 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 e database size
1ef0: 69 73 20 6b 6e 6f 77 6e 20 69 6e 20 74 68 69 73 is known in this
1f00: 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 state..**.**
1f10: 20 41 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 72 75 A connection ru
1f20: 6e 6e 69 6e 67 20 77 69 74 68 20 6c 6f 63 6b 69 nning with locki
1f30: 6e 67 5f 6d 6f 64 65 3d 6e 6f 72 6d 61 6c 20 65 ng_mode=normal e
1f40: 6e 74 65 72 73 20 74 68 69 73 20 73 74 61 74 65 nters this state
1f50: 20 77 68 65 6e 0a 2a 2a 20 20 20 20 69 74 20 6f when.** it o
1f60: 70 65 6e 73 20 61 20 72 65 61 64 2d 74 72 61 6e pens a read-tran
1f70: 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 64 saction on the d
1f80: 61 74 61 62 61 73 65 20 61 6e 64 20 72 65 74 75 atabase and retu
1f90: 72 6e 73 20 74 6f 20 73 74 61 74 65 0a 2a 2a 20 rns to state.**
1fa0: 20 20 20 4f 50 45 4e 20 61 66 74 65 72 20 74 68 OPEN after th
1fb0: 65 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 e read-transacti
1fc0: 6f 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 64 2e on is completed.
1fd0: 20 48 6f 77 65 76 65 72 20 61 20 63 6f 6e 6e 65 However a conne
1fe0: 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 72 75 6e 6e ction.** runn
1ff0: 69 6e 67 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f 6d ing in locking_m
2000: 6f 64 65 3d 65 78 63 6c 75 73 69 76 65 20 28 69 ode=exclusive (i
2010: 6e 63 6c 75 64 69 6e 67 20 74 65 6d 70 20 64 61 ncluding temp da
2020: 74 61 62 61 73 65 73 29 20 72 65 6d 61 69 6e 73 tabases) remains
2030: 20 69 6e 0a 2a 2a 20 20 20 20 74 68 69 73 20 73 in.** this s
2040: 74 61 74 65 20 65 76 65 6e 20 61 66 74 65 72 20 tate even after
2050: 74 68 65 20 72 65 61 64 2d 74 72 61 6e 73 61 63 the read-transac
2060: 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 20 tion is closed.
2070: 54 68 65 20 6f 6e 6c 79 20 77 61 79 0a 2a 2a 20 The only way.**
2080: 20 20 20 61 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 a locking_mod
2090: 65 3d 65 78 63 6c 75 73 69 76 65 20 63 6f 6e 6e e=exclusive conn
20a0: 65 63 74 69 6f 6e 20 63 61 6e 20 74 72 61 6e 73 ection can trans
20b0: 69 74 69 6f 6e 20 66 72 6f 6d 20 52 45 41 44 45 ition from READE
20c0: 52 20 74 6f 20 4f 50 45 4e 0a 2a 2a 20 20 20 20 R to OPEN.**
20d0: 69 73 20 76 69 61 20 74 68 65 20 45 52 52 4f 52 is via the ERROR
20e0: 20 73 74 61 74 65 20 28 73 65 65 20 62 65 6c 6f state (see belo
20f0: 77 29 2e 0a 2a 2a 20 0a 2a 2a 20 20 20 20 2a 20 w)..** .** *
2100: 41 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 A read transacti
2110: 6f 6e 20 6d 61 79 20 62 65 20 61 63 74 69 76 65 on may be active
2120: 20 28 62 75 74 20 61 20 77 72 69 74 65 2d 74 72 (but a write-tr
2130: 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 ansaction cannot
2140: 29 2e 0a 2a 2a 20 20 20 20 2a 20 41 20 53 48 41 )..** * A SHA
2150: 52 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c RED or greater l
2160: 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 ock is held on t
2170: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
2180: 2e 0a 2a 2a 20 20 20 20 2a 20 54 68 65 20 64 62 ..** * The db
2190: 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 6d 61 Size variable ma
21a0: 79 20 62 65 20 74 72 75 73 74 65 64 20 28 65 76 y be trusted (ev
21b0: 65 6e 20 69 66 20 61 20 75 73 65 72 2d 6c 65 76 en if a user-lev
21c0: 65 6c 20 72 65 61 64 20 0a 2a 2a 20 20 20 20 20 el read .**
21d0: 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
21e0: 6e 6f 74 20 61 63 74 69 76 65 29 2e 20 54 68 65 not active). The
21f0: 20 64 62 4f 72 69 67 53 69 7a 65 20 61 6e 64 20 dbOrigSize and
2200: 64 62 46 69 6c 65 53 69 7a 65 20 76 61 72 69 61 dbFileSize varia
2210: 62 6c 65 73 0a 2a 2a 20 20 20 20 20 20 6d 61 79 bles.** may
2220: 20 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 20 not be trusted
2230: 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2e 0a 2a at this point..*
2240: 2a 20 20 20 20 2a 20 49 66 20 74 68 65 20 64 61 * * If the da
2250: 74 61 62 61 73 65 20 69 73 20 61 20 57 41 4c 20 tabase is a WAL
2260: 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 74 database, then t
2270: 68 65 20 57 41 4c 20 63 6f 6e 6e 65 63 74 69 6f he WAL connectio
2280: 6e 20 69 73 20 6f 70 65 6e 2e 0a 2a 2a 20 20 20 n is open..**
2290: 20 2a 20 45 76 65 6e 20 69 66 20 61 20 72 65 61 * Even if a rea
22a0: 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 d-transaction is
22b0: 20 6e 6f 74 20 6f 70 65 6e 2c 20 69 74 20 69 73 not open, it is
22c0: 20 67 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 guaranteed that
22d0: 20 0a 2a 2a 20 20 20 20 20 20 74 68 65 72 65 20 .** there
22e0: 69 73 20 6e 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61 is no hot-journa
22f0: 6c 20 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 l in the file-sy
2300: 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 20 57 52 49 stem..**.** WRI
2310: 54 45 52 5f 4c 4f 43 4b 45 44 3a 0a 2a 2a 0a 2a TER_LOCKED:.**.*
2320: 2a 20 20 20 20 54 68 65 20 70 61 67 65 72 20 6d * The pager m
2330: 6f 76 65 73 20 74 6f 20 74 68 69 73 20 73 74 61 oves to this sta
2340: 74 65 20 66 72 6f 6d 20 52 45 41 44 45 52 20 77 te from READER w
2350: 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e hen a write-tran
2360: 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 69 73 saction.** is
2370: 20 66 69 72 73 74 20 6f 70 65 6e 65 64 20 6f 6e first opened on
2380: 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 the database. I
2390: 6e 20 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 20 n WRITER_LOCKED
23a0: 73 74 61 74 65 2c 20 61 6c 6c 20 6c 6f 63 6b 73 state, all locks
23b0: 20 0a 2a 2a 20 20 20 20 72 65 71 75 69 72 65 64 .** required
23c0: 20 74 6f 20 73 74 61 72 74 20 61 20 77 72 69 74 to start a writ
23d0: 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 72 e-transaction ar
23e0: 65 20 68 65 6c 64 2c 20 62 75 74 20 6e 6f 20 61 e held, but no a
23f0: 63 74 75 61 6c 20 0a 2a 2a 20 20 20 20 6d 6f 64 ctual .** mod
2400: 69 66 69 63 61 74 69 6f 6e 73 20 74 6f 20 74 68 ifications to th
2410: 65 20 63 61 63 68 65 20 6f 72 20 64 61 74 61 62 e cache or datab
2420: 61 73 65 20 68 61 76 65 20 74 61 6b 65 6e 20 70 ase have taken p
2430: 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 49 lace..**.** I
2440: 6e 20 72 6f 6c 6c 62 61 63 6b 20 6d 6f 64 65 2c n rollback mode,
2450: 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 28 a RESERVED or (
2460: 69 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 if the transacti
2470: 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 20 77 69 on was opened wi
2480: 74 68 20 0a 2a 2a 20 20 20 20 42 45 47 49 4e 20 th .** BEGIN
2490: 45 58 43 4c 55 53 49 56 45 29 20 45 58 43 4c 55 EXCLUSIVE) EXCLU
24a0: 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 6f 62 74 SIVE lock is obt
24b0: 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 ained on the dat
24c0: 61 62 61 73 65 20 66 69 6c 65 20 77 68 65 6e 0a abase file when.
24d0: 2a 2a 20 20 20 20 6d 6f 76 69 6e 67 20 74 6f 20 ** moving to
24e0: 74 68 69 73 20 73 74 61 74 65 2c 20 62 75 74 20 this state, but
24f0: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
2500: 20 69 73 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 is not written
2510: 74 6f 20 6f 72 20 6f 70 65 6e 65 64 20 0a 2a 2a to or opened .**
2520: 20 20 20 20 74 6f 20 69 6e 20 74 68 69 73 20 73 to in this s
2530: 74 61 74 65 2e 20 49 66 20 74 68 65 20 74 72 61 tate. If the tra
2540: 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d nsaction is comm
2550: 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 itted or rolled
2560: 62 61 63 6b 20 77 68 69 6c 65 20 0a 2a 2a 20 20 back while .**
2570: 20 20 69 6e 20 57 52 49 54 45 52 5f 4c 4f 43 4b in WRITER_LOCK
2580: 45 44 20 73 74 61 74 65 2c 20 61 6c 6c 20 74 68 ED state, all th
2590: 61 74 20 69 73 20 72 65 71 75 69 72 65 64 20 69 at is required i
25a0: 73 20 74 6f 20 75 6e 6c 6f 63 6b 20 74 68 65 20 s to unlock the
25b0: 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 20 20 20 database .**
25c0: 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 49 file..**.** I
25d0: 4e 20 57 41 4c 20 6d 6f 64 65 2c 20 57 61 6c 42 N WAL mode, WalB
25e0: 65 67 69 6e 57 72 69 74 65 54 72 61 6e 73 61 63 eginWriteTransac
25f0: 74 69 6f 6e 28 29 20 69 73 20 63 61 6c 6c 65 64 tion() is called
2600: 20 74 6f 20 6c 6f 63 6b 20 74 68 65 20 6c 6f 67 to lock the log
2610: 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 20 49 66 20 file..** If
2620: 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 the connection i
2630: 73 20 72 75 6e 6e 69 6e 67 20 77 69 74 68 20 6c s running with l
2640: 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 65 78 63 6c ocking_mode=excl
2650: 75 73 69 76 65 2c 20 61 6e 20 61 74 74 65 6d 70 usive, an attemp
2660: 74 0a 2a 2a 20 20 20 20 69 73 20 6d 61 64 65 20 t.** is made
2670: 74 6f 20 6f 62 74 61 69 6e 20 61 6e 20 45 58 43 to obtain an EXC
2680: 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 LUSIVE lock on t
2690: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
26a0: 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 41 20 77 ..**.** * A w
26b0: 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
26c0: 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2a 20 20 is active..**
26d0: 20 20 2a 20 49 66 20 74 68 65 20 63 6f 6e 6e 65 * If the conne
26e0: 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 20 69 6e ction is open in
26f0: 20 72 6f 6c 6c 62 61 63 6b 2d 6d 6f 64 65 2c 20 rollback-mode,
2700: 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 67 72 a RESERVED or gr
2710: 65 61 74 65 72 20 0a 2a 2a 20 20 20 20 20 20 6c eater .** l
2720: 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 ock is held on t
2730: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
2740: 2e 0a 2a 2a 20 20 20 20 2a 20 49 66 20 74 68 65 ..** * If the
2750: 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f connection is o
2760: 70 65 6e 20 69 6e 20 57 41 4c 2d 6d 6f 64 65 2c pen in WAL-mode,
2770: 20 61 20 57 41 4c 20 77 72 69 74 65 20 74 72 61 a WAL write tra
2780: 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 nsaction.**
2790: 20 69 73 20 6f 70 65 6e 20 28 69 2e 65 2e 20 73 is open (i.e. s
27a0: 71 6c 69 74 65 33 57 61 6c 42 65 67 69 6e 57 72 qlite3WalBeginWr
27b0: 69 74 65 54 72 61 6e 73 61 63 74 69 6f 6e 28 29 iteTransaction()
27c0: 20 68 61 73 20 62 65 65 6e 20 73 75 63 63 65 73 has been succes
27d0: 73 66 75 6c 6c 79 0a 2a 2a 20 20 20 20 20 20 63 sfully.** c
27e0: 61 6c 6c 65 64 29 2e 0a 2a 2a 20 20 20 20 2a 20 alled)..** *
27f0: 54 68 65 20 64 62 53 69 7a 65 2c 20 64 62 4f 72 The dbSize, dbOr
2800: 69 67 53 69 7a 65 20 61 6e 64 20 64 62 46 69 6c igSize and dbFil
2810: 65 53 69 7a 65 20 76 61 72 69 61 62 6c 65 73 20 eSize variables
2820: 61 72 65 20 61 6c 6c 20 76 61 6c 69 64 2e 0a 2a are all valid..*
2830: 2a 20 20 20 20 2a 20 54 68 65 20 63 6f 6e 74 65 * * The conte
2840: 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 nts of the pager
2850: 20 63 61 63 68 65 20 68 61 76 65 20 6e 6f 74 20 cache have not
2860: 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 0a 2a been modified..*
2870: 2a 20 20 20 20 2a 20 54 68 65 20 6a 6f 75 72 6e * * The journ
2880: 61 6c 20 66 69 6c 65 20 6d 61 79 20 6f 72 20 6d al file may or m
2890: 61 79 20 6e 6f 74 20 62 65 20 6f 70 65 6e 2e 0a ay not be open..
28a0: 2a 2a 20 20 20 20 2a 20 4e 6f 74 68 69 6e 67 20 ** * Nothing
28b0: 28 6e 6f 74 20 65 76 65 6e 20 74 68 65 20 66 69 (not even the fi
28c0: 72 73 74 20 68 65 61 64 65 72 29 20 68 61 73 20 rst header) has
28d0: 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 been written to
28e0: 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a the journal..**.
28f0: 2a 2a 20 20 57 52 49 54 45 52 5f 43 41 43 48 45 ** WRITER_CACHE
2900: 4d 4f 44 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 41 20 MOD:.**.** A
2910: 70 61 67 65 72 20 6d 6f 76 65 73 20 66 72 6f 6d pager moves from
2920: 20 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 20 73 WRITER_LOCKED s
2930: 74 61 74 65 20 74 6f 20 74 68 69 73 20 73 74 61 tate to this sta
2940: 74 65 20 77 68 65 6e 20 61 20 70 61 67 65 20 69 te when a page i
2950: 73 0a 2a 2a 20 20 20 20 66 69 72 73 74 20 6d 6f s.** first mo
2960: 64 69 66 69 65 64 20 62 79 20 74 68 65 20 75 70 dified by the up
2970: 70 65 72 20 6c 61 79 65 72 2e 20 49 6e 20 72 6f per layer. In ro
2980: 6c 6c 62 61 63 6b 20 6d 6f 64 65 20 74 68 65 20 llback mode the
2990: 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 journal file.**
29a0: 20 20 20 69 73 20 6f 70 65 6e 65 64 20 28 69 66 is opened (if
29b0: 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 it is not alrea
29c0: 64 79 20 6f 70 65 6e 29 20 61 6e 64 20 61 20 68 dy open) and a h
29d0: 65 61 64 65 72 20 77 72 69 74 74 65 6e 20 74 6f eader written to
29e0: 20 74 68 65 0a 2a 2a 20 20 20 20 73 74 61 72 74 the.** start
29f0: 20 6f 66 20 69 74 2e 20 54 68 65 20 64 61 74 61 of it. The data
2a00: 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 base file on dis
2a10: 6b 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 6d k has not been m
2a20: 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 odified..**.**
2a30: 20 20 2a 20 41 20 77 72 69 74 65 20 74 72 61 6e * A write tran
2a40: 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 saction is activ
2a50: 65 2e 0a 2a 2a 20 20 20 20 2a 20 41 20 52 45 53 e..** * A RES
2a60: 45 52 56 45 44 20 6f 72 20 67 72 65 61 74 65 72 ERVED or greater
2a70: 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e lock is held on
2a80: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
2a90: 6c 65 2e 0a 2a 2a 20 20 20 20 2a 20 54 68 65 20 le..** * The
2aa0: 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
2ab0: 6f 70 65 6e 20 61 6e 64 20 74 68 65 20 66 69 72 open and the fir
2ac0: 73 74 20 68 65 61 64 65 72 20 68 61 73 20 62 65 st header has be
2ad0: 65 6e 20 77 72 69 74 74 65 6e 20 0a 2a 2a 20 20 en written .**
2ae0: 20 20 20 20 74 6f 20 69 74 2c 20 62 75 74 20 74 to it, but t
2af0: 68 65 20 68 65 61 64 65 72 20 68 61 73 20 6e 6f he header has no
2b00: 74 20 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f t been synced to
2b10: 20 64 69 73 6b 2e 0a 2a 2a 20 20 20 20 2a 20 54 disk..** * T
2b20: 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
2b30: 68 65 20 70 61 67 65 20 63 61 63 68 65 20 68 61 he page cache ha
2b40: 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 ve been modified
2b50: 2e 0a 2a 2a 0a 2a 2a 20 20 57 52 49 54 45 52 5f ..**.** WRITER_
2b60: 44 42 4d 4f 44 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 DBMOD:.**.**
2b70: 54 68 65 20 70 61 67 65 72 20 74 72 61 6e 73 69 The pager transi
2b80: 74 69 6f 6e 73 20 66 72 6f 6d 20 57 52 49 54 45 tions from WRITE
2b90: 52 5f 43 41 43 48 45 4d 4f 44 20 69 6e 74 6f 20 R_CACHEMOD into
2ba0: 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 73 74 61 WRITER_DBMOD sta
2bb0: 74 65 0a 2a 2a 20 20 20 20 77 68 65 6e 20 69 74 te.** when it
2bc0: 20 6d 6f 64 69 66 69 65 73 20 74 68 65 20 63 6f modifies the co
2bd0: 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 61 ntents of the da
2be0: 74 61 62 61 73 65 20 66 69 6c 65 2e 20 57 41 4c tabase file. WAL
2bf0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 2a 2a 20 connections.**
2c00: 20 20 20 6e 65 76 65 72 20 65 6e 74 65 72 20 74 never enter t
2c10: 68 69 73 20 73 74 61 74 65 20 28 73 69 6e 63 65 his state (since
2c20: 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 6d 6f 64 they do not mod
2c30: 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73 65 ify the database
2c40: 20 66 69 6c 65 2c 0a 2a 2a 20 20 20 20 6a 75 73 file,.** jus
2c50: 74 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 29 2e t the log file).
2c60: 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 41 20 77 72 .**.** * A wr
2c70: 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite transaction
2c80: 69 73 20 61 63 74 69 76 65 2e 0a 2a 2a 20 20 20 is active..**
2c90: 20 2a 20 41 6e 20 45 58 43 4c 55 53 49 56 45 20 * An EXCLUSIVE
2ca0: 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 or greater lock
2cb0: 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 is held on the d
2cc0: 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
2cd0: 20 20 20 20 2a 20 54 68 65 20 6a 6f 75 72 6e 61 * The journa
2ce0: 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 61 l file is open a
2cf0: 6e 64 20 74 68 65 20 66 69 72 73 74 20 68 65 61 nd the first hea
2d00: 64 65 72 20 68 61 73 20 62 65 65 6e 20 77 72 69 der has been wri
2d10: 74 74 65 6e 20 0a 2a 2a 20 20 20 20 20 20 61 6e tten .** an
2d20: 64 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b d synced to disk
2d30: 2e 0a 2a 2a 20 20 20 20 2a 20 54 68 65 20 63 6f ..** * The co
2d40: 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 ntents of the pa
2d50: 67 65 20 63 61 63 68 65 20 68 61 76 65 20 62 65 ge cache have be
2d60: 65 6e 20 6d 6f 64 69 66 69 65 64 20 28 61 6e 64 en modified (and
2d70: 20 70 6f 73 73 69 62 6c 79 0a 2a 2a 20 20 20 20 possibly.**
2d80: 20 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 written to dis
2d90: 6b 29 2e 0a 2a 2a 0a 2a 2a 20 20 57 52 49 54 45 k)..**.** WRITE
2da0: 52 5f 46 49 4e 49 53 48 45 44 3a 0a 2a 2a 0a 2a R_FINISHED:.**.*
2db0: 2a 20 20 20 20 49 74 20 69 73 20 6e 6f 74 20 70 * It is not p
2dc0: 6f 73 73 69 62 6c 65 20 66 6f 72 20 61 20 57 41 ossible for a WA
2dd0: 4c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 L connection to
2de0: 65 6e 74 65 72 20 74 68 69 73 20 73 74 61 74 65 enter this state
2df0: 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 41 20 72 6f 6c ..**.** A rol
2e00: 6c 62 61 63 6b 2d 6d 6f 64 65 20 70 61 67 65 72 lback-mode pager
2e10: 20 63 68 61 6e 67 65 73 20 74 6f 20 57 52 49 54 changes to WRIT
2e20: 45 52 5f 46 49 4e 49 53 48 45 44 20 73 74 61 74 ER_FINISHED stat
2e30: 65 20 66 72 6f 6d 20 57 52 49 54 45 52 5f 44 42 e from WRITER_DB
2e40: 4d 4f 44 0a 2a 2a 20 20 20 20 73 74 61 74 65 20 MOD.** state
2e50: 61 66 74 65 72 20 74 68 65 20 65 6e 74 69 72 65 after the entire
2e60: 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 transaction has
2e70: 20 62 65 65 6e 20 73 75 63 63 65 73 73 66 75 6c been successful
2e80: 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 ly written into
2e90: 74 68 65 0a 2a 2a 20 20 20 20 64 61 74 61 62 61 the.** databa
2ea0: 73 65 20 66 69 6c 65 2e 20 49 6e 20 74 68 69 73 se file. In this
2eb0: 20 73 74 61 74 65 20 74 68 65 20 74 72 61 6e 73 state the trans
2ec0: 61 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 6f action may be co
2ed0: 6d 6d 69 74 74 65 64 20 73 69 6d 70 6c 79 0a 2a mmitted simply.*
2ee0: 2a 20 20 20 20 62 79 20 66 69 6e 61 6c 69 7a 69 * by finalizi
2ef0: 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ng the journal f
2f00: 69 6c 65 2e 20 4f 6e 63 65 20 69 6e 20 57 52 49 ile. Once in WRI
2f10: 54 45 52 5f 46 49 4e 49 53 48 45 44 20 73 74 61 TER_FINISHED sta
2f20: 74 65 2c 20 69 74 20 69 73 20 0a 2a 2a 20 20 20 te, it is .**
2f30: 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f not possible to
2f40: 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 modify the data
2f50: 62 61 73 65 20 66 75 72 74 68 65 72 2e 20 41 74 base further. At
2f60: 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 this point, the
2f70: 20 75 70 70 65 72 20 0a 2a 2a 20 20 20 20 6c 61 upper .** la
2f80: 79 65 72 20 6d 75 73 74 20 65 69 74 68 65 72 20 yer must either
2f90: 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 commit or rollba
2fa0: 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 ck the transacti
2fb0: 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 41 on..**.** * A
2fc0: 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
2fd0: 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2a on is active..**
2fe0: 20 20 20 20 2a 20 41 6e 20 45 58 43 4c 55 53 49 * An EXCLUSI
2ff0: 56 45 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f VE or greater lo
3000: 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 ck is held on th
3010: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
3020: 0a 2a 2a 20 20 20 20 2a 20 41 6c 6c 20 77 72 69 .** * All wri
3030: 74 69 6e 67 20 61 6e 64 20 73 79 6e 63 69 6e 67 ting and syncing
3040: 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 of journal and
3050: 64 61 74 61 62 61 73 65 20 64 61 74 61 20 68 61 database data ha
3060: 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2a 20 20 s finished..**
3070: 20 20 20 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 If no error
3080: 6f 63 63 75 72 65 64 2c 20 61 6c 6c 20 74 68 61 occured, all tha
3090: 74 20 72 65 6d 61 69 6e 73 20 69 73 20 74 6f 20 t remains is to
30a0: 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 finalize the jou
30b0: 72 6e 61 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 rnal to.**
30c0: 63 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 commit the trans
30d0: 61 63 74 69 6f 6e 2e 20 49 66 20 61 6e 20 65 72 action. If an er
30e0: 72 6f 72 20 64 69 64 20 6f 63 63 75 72 2c 20 74 ror did occur, t
30f0: 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6e he caller will n
3100: 65 65 64 0a 2a 2a 20 20 20 20 20 20 74 6f 20 72 eed.** to r
3110: 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e ollback the tran
3120: 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 saction. .**.**
3130: 20 45 52 52 4f 52 3a 0a 2a 2a 0a 2a 2a 20 20 20 ERROR:.**.**
3140: 20 54 68 65 20 45 52 52 4f 52 20 73 74 61 74 65 The ERROR state
3150: 20 69 73 20 65 6e 74 65 72 65 64 20 77 68 65 6e is entered when
3160: 20 61 6e 20 49 4f 20 6f 72 20 64 69 73 6b 2d 66 an IO or disk-f
3170: 75 6c 6c 20 65 72 72 6f 72 20 28 69 6e 63 6c 75 ull error (inclu
3180: 64 69 6e 67 0a 2a 2a 20 20 20 20 53 51 4c 49 54 ding.** SQLIT
3190: 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 29 20 6f E_IOERR_NOMEM) o
31a0: 63 63 75 72 73 20 61 74 20 61 20 70 6f 69 6e 74 ccurs at a point
31b0: 20 69 6e 20 74 68 65 20 63 6f 64 65 20 74 68 61 in the code tha
31c0: 74 20 6d 61 6b 65 73 20 69 74 20 0a 2a 2a 20 20 t makes it .**
31d0: 20 20 64 69 66 66 69 63 75 6c 74 20 74 6f 20 62 difficult to b
31e0: 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 e sure that the
31f0: 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 72 20 in-memory pager
3200: 73 74 61 74 65 20 28 63 61 63 68 65 20 63 6f 6e state (cache con
3210: 74 65 6e 74 73 2c 20 0a 2a 2a 20 20 20 20 64 62 tents, .** db
3220: 20 73 69 7a 65 20 65 74 63 2e 29 20 61 72 65 20 size etc.) are
3230: 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 consistent with
3240: 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
3250: 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e the file-system.
3260: 0a 2a 2a 0a 2a 2a 20 20 20 20 54 65 6d 70 6f 72 .**.** Tempor
3270: 61 72 79 20 70 61 67 65 72 20 66 69 6c 65 73 20 ary pager files
3280: 6d 61 79 20 65 6e 74 65 72 20 74 68 65 20 45 52 may enter the ER
3290: 52 4f 52 20 73 74 61 74 65 2c 20 62 75 74 20 69 ROR state, but i
32a0: 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 72 73 0a n-memory pagers.
32b0: 2a 2a 20 20 20 20 63 61 6e 6e 6f 74 2e 0a 2a 2a ** cannot..**
32c0: 0a 2a 2a 20 20 20 20 46 6f 72 20 65 78 61 6d 70 .** For examp
32d0: 6c 65 2c 20 69 66 20 61 6e 20 49 4f 20 65 72 72 le, if an IO err
32e0: 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 or occurs while
32f0: 70 65 72 66 6f 72 6d 69 6e 67 20 61 20 72 6f 6c performing a rol
3300: 6c 62 61 63 6b 2c 20 0a 2a 2a 20 20 20 20 74 68 lback, .** th
3310: 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
3320: 65 20 70 61 67 65 2d 63 61 63 68 65 20 6d 61 79 e page-cache may
3330: 20 62 65 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 be left in an i
3340: 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 nconsistent stat
3350: 65 2e 0a 2a 2a 20 20 20 20 41 74 20 74 68 69 73 e..** At this
3360: 20 70 6f 69 6e 74 20 69 74 20 77 6f 75 6c 64 20 point it would
3370: 62 65 20 64 61 6e 67 65 72 6f 75 73 20 74 6f 20 be dangerous to
3380: 63 68 61 6e 67 65 20 62 61 63 6b 20 74 6f 20 52 change back to R
3390: 45 41 44 45 52 20 73 74 61 74 65 0a 2a 2a 20 20 EADER state.**
33a0: 20 20 28 61 73 20 75 73 75 61 6c 6c 79 20 68 61 (as usually ha
33b0: 70 70 65 6e 73 20 61 66 74 65 72 20 61 20 72 6f ppens after a ro
33c0: 6c 6c 62 61 63 6b 29 2e 20 41 6e 79 20 73 75 62 llback). Any sub
33d0: 73 65 71 75 65 6e 74 20 72 65 61 64 65 72 73 20 sequent readers
33e0: 6d 69 67 68 74 0a 2a 2a 20 20 20 20 72 65 70 6f might.** repo
33f0: 72 74 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 rt database corr
3400: 75 70 74 69 6f 6e 20 28 64 75 65 20 74 6f 20 74 uption (due to t
3410: 68 65 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 he inconsistent
3420: 63 61 63 68 65 29 2c 20 61 6e 64 20 69 66 0a 2a cache), and if.*
3430: 2a 20 20 20 20 74 68 65 79 20 75 70 67 72 61 64 * they upgrad
3440: 65 20 74 6f 20 77 72 69 74 65 72 73 2c 20 74 68 e to writers, th
3450: 65 79 20 6d 61 79 20 69 6e 61 64 76 65 72 74 65 ey may inadverte
3460: 6e 74 6c 79 20 63 6f 72 72 75 70 74 20 74 68 65 ntly corrupt the
3470: 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 database.**
3480: 66 69 6c 65 2e 20 54 6f 20 61 76 6f 69 64 20 74 file. To avoid t
3490: 68 69 73 20 68 61 7a 61 72 64 2c 20 74 68 65 20 his hazard, the
34a0: 70 61 67 65 72 20 73 77 69 74 63 68 65 73 20 69 pager switches i
34b0: 6e 74 6f 20 74 68 65 20 45 52 52 4f 52 20 73 74 nto the ERROR st
34c0: 61 74 65 0a 2a 2a 20 20 20 20 69 6e 73 74 65 61 ate.** instea
34d0: 64 20 6f 66 20 52 45 41 44 45 52 20 66 6f 6c 6c d of READER foll
34e0: 6f 77 69 6e 67 20 73 75 63 68 20 61 6e 20 65 72 owing such an er
34f0: 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4f 6e ror..**.** On
3500: 63 65 20 69 74 20 68 61 73 20 65 6e 74 65 72 65 ce it has entere
3510: 64 20 74 68 65 20 45 52 52 4f 52 20 73 74 61 74 d the ERROR stat
3520: 65 2c 20 61 6e 79 20 61 74 74 65 6d 70 74 20 74 e, any attempt t
3530: 6f 20 75 73 65 20 74 68 65 20 70 61 67 65 72 0a o use the pager.
3540: 2a 2a 20 20 20 20 74 6f 20 72 65 61 64 20 6f 72 ** to read or
3550: 20 77 72 69 74 65 20 64 61 74 61 20 72 65 74 75 write data retu
3560: 72 6e 73 20 61 6e 20 65 72 72 6f 72 2e 20 45 76 rns an error. Ev
3570: 65 6e 74 75 61 6c 6c 79 2c 20 6f 6e 63 65 20 61 entually, once a
3580: 6c 6c 20 0a 2a 2a 20 20 20 20 6f 75 74 73 74 61 ll .** outsta
3590: 6e 64 69 6e 67 20 74 72 61 6e 73 61 63 74 69 6f nding transactio
35a0: 6e 73 20 68 61 76 65 20 62 65 65 6e 20 61 62 61 ns have been aba
35b0: 6e 64 6f 6e 65 64 2c 20 74 68 65 20 70 61 67 65 ndoned, the page
35c0: 72 20 69 73 20 61 62 6c 65 20 74 6f 0a 2a 2a 20 r is able to.**
35d0: 20 20 20 74 72 61 6e 73 69 74 69 6f 6e 20 62 61 transition ba
35e0: 63 6b 20 74 6f 20 4f 50 45 4e 20 73 74 61 74 65 ck to OPEN state
35f0: 2c 20 64 69 73 63 61 72 64 69 6e 67 20 74 68 65 , discarding the
3600: 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
3610: 20 0a 2a 2a 20 20 20 20 70 61 67 65 2d 63 61 63 .** page-cac
3620: 68 65 20 61 6e 64 20 61 6e 79 20 6f 74 68 65 72 he and any other
3630: 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 61 74 65 in-memory state
3640: 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
3650: 65 2e 20 45 76 65 72 79 74 68 69 6e 67 0a 2a 2a e. Everything.**
3660: 20 20 20 20 69 73 20 72 65 6c 6f 61 64 65 64 20 is reloaded
3670: 66 72 6f 6d 20 64 69 73 6b 20 28 61 6e 64 2c 20 from disk (and,
3680: 69 66 20 6e 65 63 65 73 73 61 72 79 2c 20 68 6f if necessary, ho
3690: 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 t-journal rollba
36a0: 63 6b 20 70 65 66 6f 72 6d 65 64 29 0a 2a 2a 20 ck peformed).**
36b0: 20 20 20 77 68 65 6e 20 61 20 72 65 61 64 2d 74 when a read-t
36c0: 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e 65 ransaction is ne
36d0: 78 74 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65 xt opened on the
36e0: 20 70 61 67 65 72 20 28 74 72 61 6e 73 69 74 69 pager (transiti
36f0: 6f 6e 69 6e 67 0a 2a 2a 20 20 20 20 74 68 65 20 oning.** the
3700: 70 61 67 65 72 20 69 6e 74 6f 20 52 45 41 44 45 pager into READE
3710: 52 20 73 74 61 74 65 29 2e 20 41 74 20 74 68 61 R state). At tha
3720: 74 20 70 6f 69 6e 74 20 74 68 65 20 73 79 73 74 t point the syst
3730: 65 6d 20 68 61 73 20 72 65 63 6f 76 65 72 65 64 em has recovered
3740: 20 0a 2a 2a 20 20 20 20 66 72 6f 6d 20 74 68 65 .** from the
3750: 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 error..**.**
3760: 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74 Specifically, t
3770: 68 65 20 70 61 67 65 72 20 6a 75 6d 70 73 20 69 he pager jumps i
3780: 6e 74 6f 20 74 68 65 20 45 52 52 4f 52 20 73 74 nto the ERROR st
3790: 61 74 65 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 ate if:.**.**
37a0: 20 20 20 31 2e 20 41 6e 20 65 72 72 6f 72 20 6f 1. An error o
37b0: 63 63 75 72 73 20 77 68 69 6c 65 20 61 74 74 65 ccurs while atte
37c0: 6d 70 74 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 mpting a rollbac
37d0: 6b 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 k. This happens
37e0: 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 75 in.** fu
37f0: 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 50 61 nction sqlite3Pa
3800: 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 2e 0a 2a gerRollback()..*
3810: 2a 0a 2a 2a 20 20 20 20 20 20 32 2e 20 41 6e 20 *.** 2. An
3820: 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 error occurs whi
3830: 6c 65 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f le attempting to
3840: 20 66 69 6e 61 6c 69 7a 65 20 61 20 6a 6f 75 72 finalize a jour
3850: 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 nal file.**
3860: 20 20 20 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 following a
3870: 63 6f 6d 6d 69 74 20 69 6e 20 66 75 6e 63 74 69 commit in functi
3880: 6f 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 on sqlite3PagerC
3890: 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 2e ommitPhaseTwo().
38a0: 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 33 2e 20 41 .**.** 3. A
38b0: 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 n error occurs w
38c0: 68 69 6c 65 20 61 74 74 65 6d 70 74 69 6e 67 20 hile attempting
38d0: 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 to write to the
38e0: 6a 6f 75 72 6e 61 6c 20 6f 72 0a 2a 2a 20 20 20 journal or.**
38f0: 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 66 database f
3900: 69 6c 65 20 69 6e 20 66 75 6e 63 74 69 6f 6e 20 ile in function
3910: 70 61 67 65 72 53 74 72 65 73 73 28 29 20 69 6e pagerStress() in
3920: 20 6f 72 64 65 72 20 74 6f 20 66 72 65 65 20 75 order to free u
3930: 70 0a 2a 2a 20 20 20 20 20 20 20 20 20 6d 65 6d p.** mem
3940: 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 49 6e ory..**.** In
3950: 20 6f 74 68 65 72 20 63 61 73 65 73 2c 20 74 68 other cases, th
3960: 65 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 e error is retur
3970: 6e 65 64 20 74 6f 20 74 68 65 20 62 2d 74 72 65 ned to the b-tre
3980: 65 20 6c 61 79 65 72 2e 20 54 68 65 20 62 2d 74 e layer. The b-t
3990: 72 65 65 0a 2a 2a 20 20 20 20 6c 61 79 65 72 20 ree.** layer
39a0: 74 68 65 6e 20 61 74 74 65 6d 70 74 73 20 61 20 then attempts a
39b0: 72 6f 6c 6c 62 61 63 6b 20 6f 70 65 72 61 74 69 rollback operati
39c0: 6f 6e 2e 20 49 66 20 74 68 65 20 65 72 72 6f 72 on. If the error
39d0: 20 63 6f 6e 64 69 74 69 6f 6e 20 0a 2a 2a 20 20 condition .**
39e0: 20 20 70 65 72 73 69 73 74 73 2c 20 74 68 65 20 persists, the
39f0: 70 61 67 65 72 20 65 6e 74 65 72 73 20 74 68 65 pager enters the
3a00: 20 45 52 52 4f 52 20 73 74 61 74 65 20 76 69 61 ERROR state via
3a10: 20 63 6f 6e 64 69 74 69 6f 6e 20 28 31 29 20 61 condition (1) a
3a20: 62 6f 76 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 43 bove..**.** C
3a30: 6f 6e 64 69 74 69 6f 6e 20 28 33 29 20 69 73 20 ondition (3) is
3a40: 6e 65 63 65 73 73 61 72 79 20 62 65 63 61 75 73 necessary becaus
3a50: 65 20 69 74 20 63 61 6e 20 62 65 20 74 72 69 67 e it can be trig
3a60: 67 65 72 65 64 20 62 79 20 61 20 72 65 61 64 2d gered by a read-
3a70: 6f 6e 6c 79 0a 2a 2a 20 20 20 20 73 74 61 74 65 only.** state
3a80: 6d 65 6e 74 20 65 78 65 63 75 74 65 64 20 77 69 ment executed wi
3a90: 74 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 thin a transacti
3aa0: 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 on. In this case
3ab0: 2c 20 69 66 20 74 68 65 20 65 72 72 6f 72 0a 2a , if the error.*
3ac0: 2a 20 20 20 20 63 6f 64 65 20 77 65 72 65 20 73 * code were s
3ad0: 69 6d 70 6c 79 20 72 65 74 75 72 6e 65 64 20 74 imply returned t
3ae0: 6f 20 74 68 65 20 75 73 65 72 2c 20 74 68 65 20 o the user, the
3af0: 62 2d 74 72 65 65 20 6c 61 79 65 72 20 77 6f 75 b-tree layer wou
3b00: 6c 64 20 6e 6f 74 0a 2a 2a 20 20 20 20 61 75 74 ld not.** aut
3b10: 6f 6d 61 74 69 63 61 6c 6c 79 20 61 74 74 65 6d omatically attem
3b20: 70 74 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 61 pt a rollback, a
3b30: 73 20 69 74 20 61 73 73 75 6d 65 73 20 74 68 61 s it assumes tha
3b40: 74 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 61 0a t an error in a.
3b50: 2a 2a 20 20 20 20 72 65 61 64 2d 6f 6e 6c 79 20 ** read-only
3b60: 73 74 61 74 65 6d 65 6e 74 20 63 61 6e 6e 6f 74 statement cannot
3b70: 20 6c 65 61 76 65 20 74 68 65 20 70 61 67 65 72 leave the pager
3b80: 20 69 6e 20 61 6e 20 69 6e 74 65 72 6e 61 6c 6c in an internall
3b90: 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 0a y inconsistent .
3ba0: 2a 2a 20 20 20 20 73 74 61 74 65 2e 0a 2a 2a 0a ** state..**.
3bb0: 2a 2a 20 20 20 20 2a 20 54 68 65 20 50 61 67 65 ** * The Page
3bc0: 72 2e 65 72 72 43 6f 64 65 20 76 61 72 69 61 62 r.errCode variab
3bd0: 6c 65 20 69 73 20 73 65 74 20 74 6f 20 73 6f 6d le is set to som
3be0: 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 ething other tha
3bf0: 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 n SQLITE_OK..**
3c00: 20 20 20 2a 20 54 68 65 72 65 20 61 72 65 20 6f * There are o
3c10: 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 75 74 73 74 ne or more outst
3c20: 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 anding reference
3c30: 73 20 74 6f 20 70 61 67 65 73 20 28 61 66 74 65 s to pages (afte
3c40: 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 6c 61 r the.** la
3c50: 73 74 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 st reference is
3c60: 64 72 6f 70 70 65 64 20 74 68 65 20 70 61 67 65 dropped the page
3c70: 72 20 73 68 6f 75 6c 64 20 6d 6f 76 65 20 62 61 r should move ba
3c80: 63 6b 20 74 6f 20 4f 50 45 4e 20 73 74 61 74 65 ck to OPEN state
3c90: 29 2e 0a 2a 2a 20 20 20 20 2a 20 54 68 65 20 70 )..** * The p
3ca0: 61 67 65 72 20 69 73 20 6e 6f 74 20 61 6e 20 69 ager is not an i
3cb0: 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 72 2e 0a n-memory pager..
3cc0: 2a 2a 20 20 20 20 0a 2a 2a 0a 2a 2a 20 4e 6f 74 ** .**.** Not
3cd0: 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 20 es:.**.** * A
3ce0: 70 61 67 65 72 20 69 73 20 6e 65 76 65 72 20 69 pager is never i
3cf0: 6e 20 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 6f n WRITER_DBMOD o
3d00: 72 20 57 52 49 54 45 52 5f 46 49 4e 49 53 48 45 r WRITER_FINISHE
3d10: 44 20 73 74 61 74 65 20 69 66 20 74 68 65 0a 2a D state if the.*
3d20: 2a 20 20 20 20 20 63 6f 6e 6e 65 63 74 69 6f 6e * connection
3d30: 20 69 73 20 6f 70 65 6e 20 69 6e 20 57 41 4c 20 is open in WAL
3d40: 6d 6f 64 65 2e 20 41 20 57 41 4c 20 63 6f 6e 6e mode. A WAL conn
3d50: 65 63 74 69 6f 6e 20 69 73 20 61 6c 77 61 79 73 ection is always
3d60: 20 69 6e 20 6f 6e 65 0a 2a 2a 20 20 20 20 20 6f in one.** o
3d70: 66 20 74 68 65 20 66 69 72 73 74 20 66 6f 75 72 f the first four
3d80: 20 73 74 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 states..**.**
3d90: 20 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 61 20 63 * Normally, a c
3da0: 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70 65 6e 20 69 onnection open i
3db0: 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n exclusive mode
3dc0: 20 69 73 20 6e 65 76 65 72 20 69 6e 20 50 41 47 is never in PAG
3dd0: 45 52 5f 4f 50 45 4e 0a 2a 2a 20 20 20 20 20 73 ER_OPEN.** s
3de0: 74 61 74 65 2e 20 54 68 65 72 65 20 61 72 65 20 tate. There are
3df0: 74 77 6f 20 65 78 63 65 70 74 69 6f 6e 73 3a 20 two exceptions:
3e00: 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 immediately afte
3e10: 72 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65 r exclusive-mode
3e20: 20 68 61 73 0a 2a 2a 20 20 20 20 20 62 65 65 6e has.** been
3e30: 20 74 75 72 6e 65 64 20 6f 6e 20 28 61 6e 64 20 turned on (and
3e40: 62 65 66 6f 72 65 20 61 6e 79 20 72 65 61 64 20 before any read
3e50: 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 or write transac
3e60: 74 69 6f 6e 73 20 61 72 65 20 0a 2a 2a 20 20 20 tions are .**
3e70: 20 20 65 78 65 63 75 74 65 64 29 2c 20 61 6e 64 executed), and
3e80: 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 when the pager
3e90: 69 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 22 is leaving the "
3ea0: 65 72 72 6f 72 20 73 74 61 74 65 22 2e 0a 2a 2a error state"..**
3eb0: 0a 2a 2a 20 20 20 2a 20 53 65 65 20 61 6c 73 6f .** * See also
3ec0: 3a 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 : assert_pager_s
3ed0: 74 61 74 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 tate()..*/.#defi
3ee0: 6e 65 20 50 41 47 45 52 5f 4f 50 45 4e 20 20 20 ne PAGER_OPEN
3ef0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
3f00: 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 52 .#define PAGER_R
3f10: 45 41 44 45 52 20 20 20 20 20 20 20 20 20 20 20 EADER
3f20: 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 50 1.#define P
3f30: 41 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b AGER_WRITER_LOCK
3f40: 45 44 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 ED 2.#de
3f50: 66 69 6e 65 20 50 41 47 45 52 5f 57 52 49 54 45 fine PAGER_WRITE
3f60: 52 5f 43 41 43 48 45 4d 4f 44 20 20 20 20 20 20 R_CACHEMOD
3f70: 20 33 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 3.#define PAGER
3f80: 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 20 20 _WRITER_DBMOD
3f90: 20 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 4.#define
3fa0: 20 50 41 47 45 52 5f 57 52 49 54 45 52 5f 46 49 PAGER_WRITER_FI
3fb0: 4e 49 53 48 45 44 20 20 20 20 20 20 20 35 0a 23 NISHED 5.#
3fc0: 64 65 66 69 6e 65 20 50 41 47 45 52 5f 45 52 52 define PAGER_ERR
3fd0: 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OR
3fe0: 20 20 20 36 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6../*.** The
3ff0: 50 61 67 65 72 2e 65 4c 6f 63 6b 20 76 61 72 69 Pager.eLock vari
4000: 61 62 6c 65 20 69 73 20 61 6c 6d 6f 73 74 20 61 able is almost a
4010: 6c 77 61 79 73 20 73 65 74 20 74 6f 20 6f 6e 65 lways set to one
4020: 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 6f 6c 6c of the .** foll
4030: 6f 77 69 6e 67 20 6c 6f 63 6b 69 6e 67 2d 73 74 owing locking-st
4040: 61 74 65 73 2c 20 61 63 63 6f 72 64 69 6e 67 20 ates, according
4050: 74 6f 20 74 68 65 20 6c 6f 63 6b 20 63 75 72 72 to the lock curr
4060: 65 6e 74 6c 79 20 68 65 6c 64 20 6f 6e 0a 2a 2a ently held on.**
4070: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
4080: 6c 65 3a 20 4e 4f 5f 4c 4f 43 4b 2c 20 53 48 41 le: NO_LOCK, SHA
4090: 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53 45 52 56 RED_LOCK, RESERV
40a0: 45 44 5f 4c 4f 43 4b 20 6f 72 20 45 58 43 4c 55 ED_LOCK or EXCLU
40b0: 53 49 56 45 5f 4c 4f 43 4b 2e 0a 2a 2a 20 54 68 SIVE_LOCK..** Th
40c0: 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 6b is variable is k
40d0: 65 70 74 20 75 70 20 74 6f 20 64 61 74 65 20 61 ept up to date a
40e0: 73 20 6c 6f 63 6b 73 20 61 72 65 20 74 61 6b 65 s locks are take
40f0: 6e 20 61 6e 64 20 72 65 6c 65 61 73 65 64 20 62 n and released b
4100: 79 0a 2a 2a 20 74 68 65 20 70 61 67 65 72 4c 6f y.** the pagerLo
4110: 63 6b 44 62 28 29 20 61 6e 64 20 70 61 67 65 72 ckDb() and pager
4120: 55 6e 6c 6f 63 6b 44 62 28 29 20 77 72 61 70 70 UnlockDb() wrapp
4130: 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ers..**.** If th
4140: 65 20 56 46 53 20 78 4c 6f 63 6b 28 29 20 6f 72 e VFS xLock() or
4150: 20 78 55 6e 6c 6f 63 6b 28 29 20 72 65 74 75 72 xUnlock() retur
4160: 6e 73 20 61 6e 20 65 72 72 6f 72 20 6f 74 68 65 ns an error othe
4170: 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 42 55 r than SQLITE_BU
4180: 53 59 0a 2a 2a 20 28 69 2e 65 2e 20 6f 6e 65 20 SY.** (i.e. one
4190: 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f of the SQLITE_IO
41a0: 45 52 52 20 73 75 62 74 79 70 65 73 29 2c 20 69 ERR subtypes), i
41b0: 74 20 69 73 20 6e 6f 74 20 63 6c 65 61 72 20 77 t is not clear w
41c0: 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a hether or not.**
41d0: 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 77 the operation w
41e0: 61 73 20 73 75 63 63 65 73 73 66 75 6c 2e 20 49 as successful. I
41f0: 6e 20 74 68 65 73 65 20 63 69 72 63 75 6d 73 74 n these circumst
4200: 61 6e 63 65 73 20 70 61 67 65 72 4c 6f 63 6b 44 ances pagerLockD
4210: 62 28 29 20 61 6e 64 0a 2a 2a 20 70 61 67 65 72 b() and.** pager
4220: 55 6e 6c 6f 63 6b 44 62 28 29 20 74 61 6b 65 20 UnlockDb() take
4230: 61 20 63 6f 6e 73 65 72 76 61 74 69 76 65 20 61 a conservative a
4240: 70 70 72 6f 61 63 68 20 2d 20 65 4c 6f 63 6b 20 pproach - eLock
4250: 69 73 20 61 6c 77 61 79 73 20 75 70 64 61 74 65 is always update
4260: 64 0a 2a 2a 20 77 68 65 6e 20 75 6e 6c 6f 63 6b d.** when unlock
4270: 69 6e 67 20 74 68 65 20 66 69 6c 65 2c 20 61 6e ing the file, an
4280: 64 20 6f 6e 6c 79 20 75 70 64 61 74 65 64 20 77 d only updated w
4290: 68 65 6e 20 6c 6f 63 6b 69 6e 67 20 74 68 65 20 hen locking the
42a0: 66 69 6c 65 20 69 66 20 74 68 65 0a 2a 2a 20 56 file if the.** V
42b0: 46 53 20 63 61 6c 6c 20 69 73 20 73 75 63 63 65 FS call is succe
42c0: 73 73 66 75 6c 2e 20 54 68 69 73 20 77 61 79 2c ssful. This way,
42d0: 20 74 68 65 20 50 61 67 65 72 2e 65 4c 6f 63 6b the Pager.eLock
42e0: 20 76 61 72 69 61 62 6c 65 20 6d 61 79 20 62 65 variable may be
42f0: 20 73 65 74 0a 2a 2a 20 74 6f 20 61 20 6c 65 73 set.** to a les
4300: 73 20 65 78 63 6c 75 73 69 76 65 20 28 6c 6f 77 s exclusive (low
4310: 65 72 29 20 76 61 6c 75 65 20 74 68 61 6e 20 74 er) value than t
4320: 68 65 20 6c 6f 63 6b 20 74 68 61 74 20 69 73 20 he lock that is
4330: 61 63 74 75 61 6c 6c 79 20 68 65 6c 64 0a 2a 2a actually held.**
4340: 20 61 74 20 74 68 65 20 73 79 73 74 65 6d 20 6c at the system l
4350: 65 76 65 6c 2c 20 62 75 74 20 69 74 20 69 73 20 evel, but it is
4360: 6e 65 76 65 72 20 73 65 74 20 74 6f 20 61 20 6d never set to a m
4370: 6f 72 65 20 65 78 63 6c 75 73 69 76 65 20 76 61 ore exclusive va
4380: 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 lue..**.** This
4390: 69 73 20 75 73 75 61 6c 6c 79 20 73 61 66 65 2e is usually safe.
43a0: 20 49 66 20 61 6e 20 78 55 6e 6c 6f 63 6b 20 66 If an xUnlock f
43b0: 61 69 6c 73 20 6f 72 20 61 70 70 65 61 72 73 20 ails or appears
43c0: 74 6f 20 66 61 69 6c 2c 20 74 68 65 72 65 20 6d to fail, there m
43d0: 61 79 20 0a 2a 2a 20 62 65 20 61 20 66 65 77 20 ay .** be a few
43e0: 72 65 64 75 6e 64 61 6e 74 20 78 4c 6f 63 6b 28 redundant xLock(
43f0: 29 20 63 61 6c 6c 73 20 6f 72 20 61 20 6c 6f 63 ) calls or a loc
4400: 6b 20 6d 61 79 20 62 65 20 68 65 6c 64 20 66 6f k may be held fo
4410: 72 20 6c 6f 6e 67 65 72 20 74 68 61 6e 0a 2a 2a r longer than.**
4420: 20 72 65 71 75 69 72 65 64 2c 20 62 75 74 20 6e required, but n
4430: 6f 74 68 69 6e 67 20 72 65 61 6c 6c 79 20 67 6f othing really go
4440: 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 es wrong..**.**
4450: 54 68 65 20 65 78 63 65 70 74 69 6f 6e 20 69 73 The exception is
4460: 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 when the databa
4470: 73 65 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 se file is unloc
4480: 6b 65 64 20 61 73 20 74 68 65 20 70 61 67 65 72 ked as the pager
4490: 20 6d 6f 76 65 73 0a 2a 2a 20 66 72 6f 6d 20 45 moves.** from E
44a0: 52 52 4f 52 20 74 6f 20 4f 50 45 4e 20 73 74 61 RROR to OPEN sta
44b0: 74 65 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e te. At this poin
44c0: 74 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 61 t there may be a
44d0: 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c hot-journal fil
44e0: 65 20 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 6c e .** in the fil
44f0: 65 2d 73 79 73 74 65 6d 20 74 68 61 74 20 6e 65 e-system that ne
4500: 65 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 eds to be rolled
4510: 20 62 61 63 6b 20 28 61 73 20 70 61 72 74 20 6f back (as part o
4520: 66 20 61 20 4f 50 45 4e 2d 3e 53 48 41 52 45 44 f a OPEN->SHARED
4530: 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 2c 20 .** transition,
4540: 62 79 20 74 68 65 20 73 61 6d 65 20 70 61 67 65 by the same page
4550: 72 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 29 2e r or any other).
4560: 20 49 66 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 If the call to
4570: 78 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 66 61 69 xUnlock().** fai
4580: 6c 73 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 ls at this point
4590: 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 20 69 and the pager i
45a0: 73 20 6c 65 66 74 20 68 6f 6c 64 69 6e 67 20 61 s left holding a
45b0: 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b n EXCLUSIVE lock
45c0: 2c 20 74 68 69 73 0a 2a 2a 20 63 61 6e 20 63 6f , this.** can co
45d0: 6e 66 75 73 65 20 74 68 65 20 63 61 6c 6c 20 74 nfuse the call t
45e0: 6f 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 o xCheckReserved
45f0: 4c 6f 63 6b 28 29 20 63 61 6c 6c 20 6d 61 64 65 Lock() call made
4600: 20 6c 61 74 65 72 20 61 73 20 70 61 72 74 0a 2a later as part.*
4610: 2a 20 6f 66 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c * of hot-journal
4620: 20 64 65 74 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a detection..**.*
4630: 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 * xCheckReserved
4640: 4c 6f 63 6b 28 29 20 69 73 20 64 65 66 69 6e 65 Lock() is define
4650: 64 20 61 73 20 72 65 74 75 72 6e 69 6e 67 20 74 d as returning t
4660: 72 75 65 20 22 69 66 20 74 68 65 72 65 20 69 73 rue "if there is
4670: 20 61 20 52 45 53 45 52 56 45 44 20 0a 2a 2a 20 a RESERVED .**
4680: 6c 6f 63 6b 20 68 65 6c 64 20 62 79 20 74 68 69 lock held by thi
4690: 73 20 70 72 6f 63 65 73 73 20 6f 72 20 61 6e 79 s process or any
46a0: 20 6f 74 68 65 72 73 22 2e 20 53 6f 20 78 43 68 others". So xCh
46b0: 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 eckReservedLock
46c0: 6d 61 79 20 0a 2a 2a 20 72 65 74 75 72 6e 20 74 may .** return t
46d0: 72 75 65 20 62 65 63 61 75 73 65 20 74 68 65 20 rue because the
46e0: 63 61 6c 6c 65 72 20 69 74 73 65 6c 66 20 69 73 caller itself is
46f0: 20 68 6f 6c 64 69 6e 67 20 61 6e 20 45 58 43 4c holding an EXCL
4700: 55 53 49 56 45 20 6c 6f 63 6b 20 28 62 75 74 0a USIVE lock (but.
4710: 2a 2a 20 64 6f 65 73 6e 27 74 20 6b 6e 6f 77 20 ** doesn't know
4720: 69 74 20 62 65 63 61 75 73 65 20 6f 66 20 61 20 it because of a
4730: 70 72 65 76 69 6f 75 73 20 65 72 72 6f 72 20 69 previous error i
4740: 6e 20 78 55 6e 6c 6f 63 6b 29 2e 20 49 66 20 74 n xUnlock). If t
4750: 68 69 73 20 68 61 70 70 65 6e 73 0a 2a 2a 20 61 his happens.** a
4760: 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 6d 61 79 hot-journal may
4770: 20 62 65 20 6d 69 73 74 61 6b 65 6e 20 66 6f 72 be mistaken for
4780: 20 61 20 6a 6f 75 72 6e 61 6c 20 62 65 69 6e 67 a journal being
4790: 20 63 72 65 61 74 65 64 20 62 79 20 61 6e 20 61 created by an a
47a0: 63 74 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61 63 ctive.** transac
47b0: 74 69 6f 6e 20 69 6e 20 61 6e 6f 74 68 65 72 20 tion in another
47c0: 70 72 6f 63 65 73 73 2c 20 63 61 75 73 69 6e 67 process, causing
47d0: 20 53 51 4c 69 74 65 20 74 6f 20 72 65 61 64 20 SQLite to read
47e0: 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
47f0: 65 0a 2a 2a 20 77 69 74 68 6f 75 74 20 72 6f 6c e.** without rol
4800: 6c 69 6e 67 20 69 74 20 62 61 63 6b 2e 0a 2a 2a ling it back..**
4810: 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 .** To work arou
4820: 6e 64 20 74 68 69 73 2c 20 69 66 20 61 20 63 61 nd this, if a ca
4830: 6c 6c 20 74 6f 20 78 55 6e 6c 6f 63 6b 28 29 20 ll to xUnlock()
4840: 66 61 69 6c 73 20 77 68 65 6e 20 75 6e 6c 6f 63 fails when unloc
4850: 6b 69 6e 67 20 74 68 65 0a 2a 2a 20 64 61 74 61 king the.** data
4860: 62 61 73 65 20 69 6e 20 74 68 65 20 45 52 52 4f base in the ERRO
4870: 52 20 73 74 61 74 65 2c 20 50 61 67 65 72 2e 65 R state, Pager.e
4880: 4c 6f 63 6b 20 69 73 20 73 65 74 20 74 6f 20 55 Lock is set to U
4890: 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 2e 20 49 74 0a NKNOWN_LOCK. It.
48a0: 2a 2a 20 69 73 20 6f 6e 6c 79 20 63 68 61 6e 67 ** is only chang
48b0: 65 64 20 62 61 63 6b 20 74 6f 20 61 20 72 65 61 ed back to a rea
48c0: 6c 20 6c 6f 63 6b 69 6e 67 20 73 74 61 74 65 20 l locking state
48d0: 61 66 74 65 72 20 61 20 73 75 63 63 65 73 73 66 after a successf
48e0: 75 6c 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 78 4c ul call.** to xL
48f0: 6f 63 6b 28 45 58 43 4c 55 53 49 56 45 29 2e 20 ock(EXCLUSIVE).
4900: 41 6c 73 6f 2c 20 74 68 65 20 63 6f 64 65 20 74 Also, the code t
4910: 6f 20 64 6f 20 74 68 65 20 4f 50 45 4e 2d 3e 53 o do the OPEN->S
4920: 48 41 52 45 44 20 73 74 61 74 65 20 74 72 61 6e HARED state tran
4930: 73 69 74 69 6f 6e 0a 2a 2a 20 6f 6d 69 74 73 20 sition.** omits
4940: 74 68 65 20 63 68 65 63 6b 20 66 6f 72 20 61 20 the check for a
4950: 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 50 hot-journal if P
4960: 61 67 65 72 2e 65 4c 6f 63 6b 20 69 73 20 73 65 ager.eLock is se
4970: 74 20 74 6f 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 t to UNKNOWN_LOC
4980: 4b 20 0a 2a 2a 20 6c 6f 63 6b 2e 20 49 6e 73 74 K .** lock. Inst
4990: 65 61 64 2c 20 69 74 20 61 73 73 75 6d 65 73 20 ead, it assumes
49a0: 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 65 78 a hot-journal ex
49b0: 69 73 74 73 20 61 6e 64 20 6f 62 74 61 69 6e 73 ists and obtains
49c0: 20 61 6e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a an EXCLUSIVE.**
49d0: 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
49e0: 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 abase file befor
49f0: 65 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 e attempting to
4a00: 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 53 65 roll it back. Se
4a10: 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 50 61 e function.** Pa
4a20: 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 29 20 gerSharedLock()
4a30: 66 6f 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 2e for more detail.
4a40: 0a 2a 2a 0a 2a 2a 20 50 61 67 65 72 2e 65 4c 6f .**.** Pager.eLo
4a50: 63 6b 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 73 ck may only be s
4a60: 65 74 20 74 6f 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f et to UNKNOWN_LO
4a70: 43 4b 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 CK when the page
4a80: 72 20 69 73 20 69 6e 20 0a 2a 2a 20 50 41 47 45 r is in .** PAGE
4a90: 52 5f 4f 50 45 4e 20 73 74 61 74 65 2e 0a 2a 2f R_OPEN state..*/
4aa0: 0a 23 64 65 66 69 6e 65 20 55 4e 4b 4e 4f 57 4e .#define UNKNOWN
4ab0: 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 20 20 _LOCK
4ac0: 20 20 20 20 20 28 45 58 43 4c 55 53 49 56 45 5f (EXCLUSIVE_
4ad0: 4c 4f 43 4b 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 41 LOCK+1)../*.** A
4ae0: 20 6d 61 63 72 6f 20 75 73 65 64 20 66 6f 72 20 macro used for
4af0: 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f 64 invoking the cod
4b00: 65 63 20 69 66 20 74 68 65 72 65 20 69 73 20 6f ec if there is o
4b10: 6e 65 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c ne.*/.#ifdef SQL
4b20: 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 23 20 ITE_HAS_CODEC.#
4b30: 64 65 66 69 6e 65 20 43 4f 44 45 43 31 28 50 2c define CODEC1(P,
4b40: 44 2c 4e 2c 58 2c 45 29 20 5c 0a 20 20 20 20 69 D,N,X,E) \. i
4b50: 66 28 20 50 2d 3e 78 43 6f 64 65 63 20 26 26 20 f( P->xCodec &&
4b60: 50 2d 3e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f P->xCodec(P->pCo
4b70: 64 65 63 2c 44 2c 4e 2c 58 29 3d 3d 30 20 29 7b dec,D,N,X)==0 ){
4b80: 20 45 3b 20 7d 0a 23 20 64 65 66 69 6e 65 20 43 E; }.# define C
4b90: 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 2c 45 2c ODEC2(P,D,N,X,E,
4ba0: 4f 29 20 5c 0a 20 20 20 20 69 66 28 20 50 2d 3e O) \. if( P->
4bb0: 78 43 6f 64 65 63 3d 3d 30 20 29 7b 20 4f 3d 28 xCodec==0 ){ O=(
4bc0: 63 68 61 72 2a 29 44 3b 20 7d 65 6c 73 65 20 5c char*)D; }else \
4bd0: 0a 20 20 20 20 69 66 28 20 28 4f 3d 28 63 68 61 . if( (O=(cha
4be0: 72 2a 29 28 50 2d 3e 78 43 6f 64 65 63 28 50 2d r*)(P->xCodec(P-
4bf0: 3e 70 43 6f 64 65 63 2c 44 2c 4e 2c 58 29 29 29 >pCodec,D,N,X)))
4c00: 3d 3d 30 20 29 7b 20 45 3b 20 7d 0a 23 65 6c 73 ==0 ){ E; }.#els
4c10: 65 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 e.# define CODEC
4c20: 31 28 50 2c 44 2c 4e 2c 58 2c 45 29 20 20 20 2f 1(P,D,N,X,E) /
4c30: 2a 20 4e 4f 2d 4f 50 20 2a 2f 0a 23 20 64 65 66 * NO-OP */.# def
4c40: 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e ine CODEC2(P,D,N
4c50: 2c 58 2c 45 2c 4f 29 20 4f 3d 28 63 68 61 72 2a ,X,E,O) O=(char*
4c60: 29 44 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a )D.#endif../*.**
4c70: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c The maximum all
4c80: 6f 77 65 64 20 73 65 63 74 6f 72 20 73 69 7a 65 owed sector size
4c90: 2e 20 36 34 4b 69 42 2e 20 49 66 20 74 68 65 20 . 64KiB. If the
4ca0: 78 53 65 63 74 6f 72 73 69 7a 65 28 29 20 6d 65 xSectorsize() me
4cb0: 74 68 6f 64 20 0a 2a 2a 20 72 65 74 75 72 6e 73 thod .** returns
4cc0: 20 61 20 76 61 6c 75 65 20 6c 61 72 67 65 72 20 a value larger
4cd0: 74 68 61 6e 20 74 68 69 73 2c 20 74 68 65 6e 20 than this, then
4ce0: 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 MAX_SECTOR_SIZE
4cf0: 69 73 20 75 73 65 64 20 69 6e 73 74 65 61 64 2e is used instead.
4d00: 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6c 64 20 63 .** This could c
4d10: 6f 6e 63 65 69 76 61 62 6c 79 20 63 61 75 73 65 onceivably cause
4d20: 20 63 6f 72 72 75 70 74 69 6f 6e 20 66 6f 6c 6c corruption foll
4d30: 6f 77 69 6e 67 20 61 20 70 6f 77 65 72 20 66 61 owing a power fa
4d40: 69 6c 75 72 65 20 6f 6e 0a 2a 2a 20 73 75 63 68 ilure on.** such
4d50: 20 61 20 73 79 73 74 65 6d 2e 20 54 68 69 73 20 a system. This
4d60: 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6e 20 is currently an
4d70: 75 6e 64 6f 63 75 6d 65 6e 74 65 64 20 6c 69 6d undocumented lim
4d80: 69 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d it..*/.#define M
4d90: 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 30 AX_SECTOR_SIZE 0
4da0: 78 31 30 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6e x10000../*.** An
4db0: 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
4dc0: 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
4dd0: 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 ture is allocate
4de0: 64 20 66 6f 72 20 65 61 63 68 20 61 63 74 69 76 d for each activ
4df0: 65 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 20 61 e.** savepoint a
4e00: 6e 64 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 nd statement tra
4e10: 6e 73 61 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 nsaction in the
4e20: 73 79 73 74 65 6d 2e 20 41 6c 6c 20 73 75 63 68 system. All such
4e30: 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 61 structures.** a
4e40: 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 re stored in the
4e50: 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e Pager.aSavepoin
4e60: 74 5b 5d 20 61 72 72 61 79 2c 20 77 68 69 63 68 t[] array, which
4e70: 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e is allocated an
4e80: 64 0a 2a 2a 20 72 65 73 69 7a 65 64 20 75 73 69 d.** resized usi
4e90: 6e 67 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f ng sqlite3Reallo
4ea0: 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 c()..**.** When
4eb0: 61 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 63 a savepoint is c
4ec0: 72 65 61 74 65 64 2c 20 74 68 65 20 50 61 67 65 reated, the Page
4ed0: 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f rSavepoint.iHdrO
4ee0: 66 66 73 65 74 20 66 69 65 6c 64 20 69 73 0a 2a ffset field is.*
4ef0: 2a 20 73 65 74 20 74 6f 20 30 2e 20 49 66 20 61 * set to 0. If a
4f00: 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 journal-header
4f10: 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 is written into
4f20: 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c the main journal
4f30: 20 77 68 69 6c 65 0a 2a 2a 20 74 68 65 20 73 61 while.** the sa
4f40: 76 65 70 6f 69 6e 74 20 69 73 20 61 63 74 69 76 vepoint is activ
4f50: 65 2c 20 74 68 65 6e 20 69 48 64 72 4f 66 66 73 e, then iHdrOffs
4f60: 65 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 et is set to the
4f70: 20 62 79 74 65 20 6f 66 66 73 65 74 20 0a 2a 2a byte offset .**
4f80: 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c immediately fol
4f90: 6c 6f 77 69 6e 67 20 74 68 65 20 6c 61 73 74 20 lowing the last
4fa0: 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 20 77 journal record w
4fb0: 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
4fc0: 6d 61 69 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 main.** journal
4fd0: 62 65 66 6f 72 65 20 74 68 65 20 6a 6f 75 72 6e before the journ
4fe0: 61 6c 2d 68 65 61 64 65 72 2e 20 54 68 69 73 20 al-header. This
4ff0: 69 73 20 72 65 71 75 69 72 65 64 20 64 75 72 69 is required duri
5000: 6e 67 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 ng savepoint.**
5010: 72 6f 6c 6c 62 61 63 6b 20 28 73 65 65 20 70 61 rollback (see pa
5020: 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 gerPlaybackSavep
5030: 6f 69 6e 74 28 29 29 2e 0a 2a 2f 0a 74 79 70 65 oint())..*/.type
5040: 64 65 66 20 73 74 72 75 63 74 20 50 61 67 65 72 def struct Pager
5050: 53 61 76 65 70 6f 69 6e 74 20 50 61 67 65 72 53 Savepoint PagerS
5060: 61 76 65 70 6f 69 6e 74 3b 0a 73 74 72 75 63 74 avepoint;.struct
5070: 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 PagerSavepoint
5080: 7b 0a 20 20 69 36 34 20 69 4f 66 66 73 65 74 3b {. i64 iOffset;
5090: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a0: 20 2f 2a 20 53 74 61 72 74 69 6e 67 20 6f 66 66 /* Starting off
50b0: 73 65 74 20 69 6e 20 6d 61 69 6e 20 6a 6f 75 72 set in main jour
50c0: 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 nal */. i64 iHd
50d0: 72 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 rOffset;
50e0: 20 20 20 20 20 20 2f 2a 20 53 65 65 20 61 62 6f /* See abo
50f0: 76 65 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a ve */. Bitvec *
5100: 70 49 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 pInSavepoint;
5110: 20 20 20 20 20 2f 2a 20 53 65 74 20 6f 66 20 70 /* Set of p
5120: 61 67 65 73 20 69 6e 20 74 68 69 73 20 73 61 76 ages in this sav
5130: 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 50 67 6e 6f epoint */. Pgno
5140: 20 6e 4f 72 69 67 3b 20 20 20 20 20 20 20 20 20 nOrig;
5150: 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 /* Orig
5160: 69 6e 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 inal number of p
5170: 61 67 65 73 20 69 6e 20 66 69 6c 65 20 2a 2f 0a ages in file */.
5180: 20 20 50 67 6e 6f 20 69 53 75 62 52 65 63 3b 20 Pgno iSubRec;
5190: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
51a0: 2a 20 49 6e 64 65 78 20 6f 66 20 66 69 72 73 74 * Index of first
51b0: 20 72 65 63 6f 72 64 20 69 6e 20 73 75 62 2d 6a record in sub-j
51c0: 6f 75 72 6e 61 6c 20 2a 2f 0a 23 69 66 6e 64 65 ournal */.#ifnde
51d0: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 f SQLITE_OMIT_WA
51e0: 4c 0a 20 20 75 33 32 20 61 57 61 6c 44 61 74 61 L. u32 aWalData
51f0: 5b 57 41 4c 5f 53 41 56 45 50 4f 49 4e 54 5f 4e [WAL_SAVEPOINT_N
5200: 44 41 54 41 5d 3b 20 20 20 20 20 20 20 20 2f 2a DATA]; /*
5210: 20 57 41 4c 20 73 61 76 65 70 6f 69 6e 74 20 63 WAL savepoint c
5220: 6f 6e 74 65 78 74 20 2a 2f 0a 23 65 6e 64 69 66 ontext */.#endif
5230: 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6f 70 65 .};../*.** A ope
5240: 6e 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 n page cache is
5250: 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 73 an instance of s
5260: 74 72 75 63 74 20 50 61 67 65 72 2e 20 41 20 64 truct Pager. A d
5270: 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 0a 2a 2a escription of.**
5280: 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 6d 6f 72 some of the mor
5290: 65 20 69 6d 70 6f 72 74 61 6e 74 20 6d 65 6d 62 e important memb
52a0: 65 72 20 76 61 72 69 61 62 6c 65 73 20 66 6f 6c er variables fol
52b0: 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 65 53 74 61 lows:.**.** eSta
52c0: 74 65 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 63 te.**.** The c
52d0: 75 72 72 65 6e 74 20 27 73 74 61 74 65 27 20 6f urrent 'state' o
52e0: 66 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 f the pager obje
52f0: 63 74 2e 20 53 65 65 20 74 68 65 20 63 6f 6d 6d ct. See the comm
5300: 65 6e 74 20 61 6e 64 20 73 74 61 74 65 0a 2a 2a ent and state.**
5310: 20 20 20 64 69 61 67 72 61 6d 20 61 62 6f 76 65 diagram above
5320: 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 69 for a descripti
5330: 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 on of the pager
5340: 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 65 4c 6f state..**.** eLo
5350: 63 6b 0a 2a 2a 0a 2a 2a 20 20 20 46 6f 72 20 61 ck.**.** For a
5360: 20 72 65 61 6c 20 6f 6e 2d 64 69 73 6b 20 64 61 real on-disk da
5370: 74 61 62 61 73 65 2c 20 74 68 65 20 63 75 72 72 tabase, the curr
5380: 65 6e 74 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e ent lock held on
5390: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
53a0: 6c 65 20 2d 0a 2a 2a 20 20 20 4e 4f 5f 4c 4f 43 le -.** NO_LOC
53b0: 4b 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 20 K, SHARED_LOCK,
53c0: 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 6f 72 RESERVED_LOCK or
53d0: 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 2e EXCLUSIVE_LOCK.
53e0: 0a 2a 2a 0a 2a 2a 20 20 20 46 6f 72 20 61 20 74 .**.** For a t
53f0: 65 6d 70 6f 72 61 72 79 20 6f 72 20 69 6e 2d 6d emporary or in-m
5400: 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 28 emory database (
5410: 6e 65 69 74 68 65 72 20 6f 66 20 77 68 69 63 68 neither of which
5420: 20 72 65 71 75 69 72 65 20 61 6e 79 0a 2a 2a 20 require any.**
5430: 20 20 6c 6f 63 6b 73 29 2c 20 74 68 69 73 20 76 locks), this v
5440: 61 72 69 61 62 6c 65 20 69 73 20 61 6c 77 61 79 ariable is alway
5450: 73 20 73 65 74 20 74 6f 20 45 58 43 4c 55 53 49 s set to EXCLUSI
5460: 56 45 5f 4c 4f 43 4b 2e 20 53 69 6e 63 65 20 73 VE_LOCK. Since s
5470: 75 63 68 0a 2a 2a 20 20 20 64 61 74 61 62 61 73 uch.** databas
5480: 65 73 20 61 6c 77 61 79 73 20 68 61 76 65 20 50 es always have P
5490: 61 67 65 72 2e 65 78 63 6c 75 73 69 76 65 4d 6f ager.exclusiveMo
54a0: 64 65 3d 3d 31 2c 20 74 68 69 73 20 74 72 69 63 de==1, this tric
54b0: 6b 73 20 74 68 65 20 70 61 67 65 72 0a 2a 2a 20 ks the pager.**
54c0: 20 20 6c 6f 67 69 63 20 69 6e 74 6f 20 74 68 69 logic into thi
54d0: 6e 6b 69 6e 67 20 74 68 61 74 20 69 74 20 61 6c nking that it al
54e0: 72 65 61 64 79 20 68 61 73 20 61 6c 6c 20 74 68 ready has all th
54f0: 65 20 6c 6f 63 6b 73 20 69 74 20 77 69 6c 6c 20 e locks it will
5500: 65 76 65 72 0a 2a 2a 20 20 20 6e 65 65 64 20 28 ever.** need (
5510: 61 6e 64 20 6e 6f 20 72 65 61 73 6f 6e 20 74 6f and no reason to
5520: 20 72 65 6c 65 61 73 65 20 74 68 65 6d 29 2e 0a release them)..
5530: 2a 2a 0a 2a 2a 20 20 20 49 6e 20 73 6f 6d 65 20 **.** In some
5540: 28 6f 62 73 63 75 72 65 29 20 63 69 72 63 75 6d (obscure) circum
5550: 73 74 61 6e 63 65 73 2c 20 74 68 69 73 20 76 61 stances, this va
5560: 72 69 61 62 6c 65 20 6d 61 79 20 61 6c 73 6f 20 riable may also
5570: 62 65 20 73 65 74 20 74 6f 0a 2a 2a 20 20 20 55 be set to.** U
5580: 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 2e 20 53 65 65 NKNOWN_LOCK. See
5590: 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f the comment abo
55a0: 76 65 20 74 68 65 20 23 64 65 66 69 6e 65 20 6f ve the #define o
55b0: 66 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 66 f UNKNOWN_LOCK f
55c0: 6f 72 0a 2a 2a 20 20 20 64 65 74 61 69 6c 73 2e or.** details.
55d0: 0a 2a 2a 0a 2a 2a 20 63 68 61 6e 67 65 43 6f 75 .**.** changeCou
55e0: 6e 74 44 6f 6e 65 0a 2a 2a 0a 2a 2a 20 20 20 54 ntDone.**.** T
55f0: 68 69 73 20 62 6f 6f 6c 65 61 6e 20 76 61 72 69 his boolean vari
5600: 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 able is used to
5610: 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 make sure that t
5620: 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 he change-counte
5630: 72 20 0a 2a 2a 20 20 20 28 74 68 65 20 34 2d 62 r .** (the 4-b
5640: 79 74 65 20 68 65 61 64 65 72 20 66 69 65 6c 64 yte header field
5650: 20 61 74 20 62 79 74 65 20 6f 66 66 73 65 74 20 at byte offset
5660: 32 34 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 24 of the databa
5670: 73 65 20 66 69 6c 65 29 20 69 73 20 0a 2a 2a 20 se file) is .**
5680: 20 20 6e 6f 74 20 75 70 64 61 74 65 64 20 6d 6f not updated mo
5690: 72 65 20 6f 66 74 65 6e 20 74 68 61 6e 20 6e 65 re often than ne
56a0: 63 65 73 73 61 72 79 2e 20 0a 2a 2a 0a 2a 2a 20 cessary. .**.**
56b0: 20 20 49 74 20 69 73 20 73 65 74 20 74 6f 20 74 It is set to t
56c0: 72 75 65 20 77 68 65 6e 20 74 68 65 20 63 68 61 rue when the cha
56d0: 6e 67 65 2d 63 6f 75 6e 74 65 72 20 66 69 65 6c nge-counter fiel
56e0: 64 20 69 73 20 75 70 64 61 74 65 64 2c 20 77 68 d is updated, wh
56f0: 69 63 68 20 0a 2a 2a 20 20 20 63 61 6e 20 6f 6e ich .** can on
5700: 6c 79 20 68 61 70 70 65 6e 20 69 66 20 61 6e 20 ly happen if an
5710: 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 exclusive lock i
5720: 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 s held on the da
5730: 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 tabase file..**
5740: 20 20 49 74 20 69 73 20 63 6c 65 61 72 65 64 20 It is cleared
5750: 28 73 65 74 20 74 6f 20 66 61 6c 73 65 29 20 77 (set to false) w
5760: 68 65 6e 65 76 65 72 20 61 6e 20 65 78 63 6c 75 henever an exclu
5770: 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a sive lock is .**
5780: 20 20 20 72 65 6c 69 6e 71 75 69 73 68 65 64 20 relinquished
5790: 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
57a0: 66 69 6c 65 2e 20 45 61 63 68 20 74 69 6d 65 20 file. Each time
57b0: 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 a transaction is
57c0: 20 63 6f 6d 6d 69 74 74 65 64 2c 0a 2a 2a 20 20 committed,.**
57d0: 20 54 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 The changeCount
57e0: 44 6f 6e 65 20 66 6c 61 67 20 69 73 20 69 6e 73 Done flag is ins
57f0: 70 65 63 74 65 64 2e 20 49 66 20 69 74 20 69 73 pected. If it is
5800: 20 74 72 75 65 2c 20 74 68 65 20 77 6f 72 6b 20 true, the work
5810: 6f 66 0a 2a 2a 20 20 20 75 70 64 61 74 69 6e 67 of.** updating
5820: 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e the change-coun
5830: 74 65 72 20 69 73 20 6f 6d 69 74 74 65 64 20 66 ter is omitted f
5840: 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 or the current t
5850: 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a ransaction..**.*
5860: 2a 20 20 20 54 68 69 73 20 6d 65 63 68 61 6e 69 * This mechani
5870: 73 6d 20 6d 65 61 6e 73 20 74 68 61 74 20 77 68 sm means that wh
5880: 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 en running in ex
5890: 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 61 20 clusive mode, a
58a0: 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 20 connection .**
58b0: 20 6e 65 65 64 20 6f 6e 6c 79 20 75 70 64 61 74 need only updat
58c0: 65 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 e the change-cou
58d0: 6e 74 65 72 20 6f 6e 63 65 2c 20 66 6f 72 20 74 nter once, for t
58e0: 68 65 20 66 69 72 73 74 20 74 72 61 6e 73 61 63 he first transac
58f0: 74 69 6f 6e 0a 2a 2a 20 20 20 63 6f 6d 6d 69 74 tion.** commit
5900: 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 73 65 74 4d 61 ted..**.** setMa
5910: 73 74 65 72 0a 2a 2a 0a 2a 2a 20 20 20 57 68 65 ster.**.** Whe
5920: 6e 20 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 n PagerCommitPha
5930: 73 65 4f 6e 65 28 29 20 69 73 20 63 61 6c 6c 65 seOne() is calle
5940: 64 20 74 6f 20 63 6f 6d 6d 69 74 20 61 20 74 72 d to commit a tr
5950: 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 6d 61 ansaction, it ma
5960: 79 0a 2a 2a 20 20 20 28 6f 72 20 6d 61 79 20 6e y.** (or may n
5970: 6f 74 29 20 73 70 65 63 69 66 79 20 61 20 6d 61 ot) specify a ma
5980: 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster-journal nam
5990: 65 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 e to be written
59a0: 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 20 20 6a into the .** j
59b0: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 65 66 6f ournal file befo
59c0: 72 65 20 69 74 20 69 73 20 73 79 6e 63 65 64 20 re it is synced
59d0: 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 20 to disk..**.**
59e0: 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 Whether or not
59f0: 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 a journal file c
5a00: 6f 6e 74 61 69 6e 73 20 61 20 6d 61 73 74 65 72 ontains a master
5a10: 2d 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 -journal pointer
5a20: 20 61 66 66 65 63 74 73 20 0a 2a 2a 20 20 20 74 affects .** t
5a30: 68 65 20 77 61 79 20 69 6e 20 77 68 69 63 68 20 he way in which
5a40: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
5a50: 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 20 61 66 is finalized af
5a60: 74 65 72 20 74 68 65 20 74 72 61 6e 73 61 63 74 ter the transact
5a70: 69 6f 6e 20 69 73 20 0a 2a 2a 20 20 20 63 6f 6d ion is .** com
5a80: 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 mitted or rolled
5a90: 20 62 61 63 6b 20 77 68 65 6e 20 72 75 6e 6e 69 back when runni
5aa0: 6e 67 20 69 6e 20 22 6a 6f 75 72 6e 61 6c 5f 6d ng in "journal_m
5ab0: 6f 64 65 3d 50 45 52 53 49 53 54 22 20 6d 6f 64 ode=PERSIST" mod
5ac0: 65 2e 0a 2a 2a 20 20 20 49 66 20 61 20 6a 6f 75 e..** If a jou
5ad0: 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 20 6e rnal file does n
5ae0: 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 6d 61 73 ot contain a mas
5af0: 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e ter-journal poin
5b00: 74 65 72 2c 20 69 74 20 69 73 0a 2a 2a 20 20 20 ter, it is.**
5b10: 66 69 6e 61 6c 69 7a 65 64 20 62 79 20 6f 76 65 finalized by ove
5b20: 72 77 72 69 74 69 6e 67 20 74 68 65 20 66 69 72 rwriting the fir
5b30: 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 st journal heade
5b40: 72 20 77 69 74 68 20 7a 65 72 6f 65 73 2e 20 49 r with zeroes. I
5b50: 66 0a 2a 2a 20 20 20 69 74 20 64 6f 65 73 20 63 f.** it does c
5b60: 6f 6e 74 61 69 6e 20 61 20 6d 61 73 74 65 72 2d ontain a master-
5b70: 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 20 journal pointer
5b80: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
5b90: 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 20 0a 2a is finalized .*
5ba0: 2a 20 20 20 62 79 20 74 72 75 6e 63 61 74 69 6e * by truncatin
5bb0: 67 20 69 74 20 74 6f 20 7a 65 72 6f 20 62 79 74 g it to zero byt
5bc0: 65 73 2c 20 6a 75 73 74 20 61 73 20 69 66 20 74 es, just as if t
5bd0: 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 65 he connection we
5be0: 72 65 20 0a 2a 2a 20 20 20 72 75 6e 6e 69 6e 67 re .** running
5bf0: 20 69 6e 20 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 in "journal_mod
5c00: 65 3d 74 72 75 6e 63 61 74 65 22 20 6d 6f 64 65 e=truncate" mode
5c10: 2e 0a 2a 2a 0a 2a 2a 20 20 20 4a 6f 75 72 6e 61 ..**.** Journa
5c20: 6c 20 66 69 6c 65 73 20 74 68 61 74 20 63 6f 6e l files that con
5c30: 74 61 69 6e 20 6d 61 73 74 65 72 20 6a 6f 75 72 tain master jour
5c40: 6e 61 6c 20 70 6f 69 6e 74 65 72 73 20 63 61 6e nal pointers can
5c50: 6e 6f 74 20 62 65 20 66 69 6e 61 6c 69 7a 65 64 not be finalized
5c60: 0a 2a 2a 20 20 20 73 69 6d 70 6c 79 20 62 79 20 .** simply by
5c70: 6f 76 65 72 77 72 69 74 69 6e 67 20 74 68 65 20 overwriting the
5c80: 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 2d 68 65 first journal-he
5c90: 61 64 65 72 20 77 69 74 68 20 7a 65 72 6f 65 73 ader with zeroes
5ca0: 2c 20 61 73 20 74 68 65 0a 2a 2a 20 20 20 6d 61 , as the.** ma
5cb0: 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 ster journal poi
5cc0: 6e 74 65 72 20 63 6f 75 6c 64 20 69 6e 74 65 72 nter could inter
5cd0: 66 65 72 65 20 77 69 74 68 20 68 6f 74 2d 6a 6f fere with hot-jo
5ce0: 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 20 6f urnal rollback o
5cf0: 66 20 61 6e 79 0a 2a 2a 20 20 20 73 75 62 73 65 f any.** subse
5d00: 71 75 65 6e 74 6c 79 20 69 6e 74 65 72 72 75 70 quently interrup
5d10: 74 65 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ted transaction
5d20: 74 68 61 74 20 72 65 75 73 65 73 20 74 68 65 20 that reuses the
5d30: 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a journal file..**
5d40: 0a 2a 2a 20 20 20 54 68 65 20 66 6c 61 67 20 69 .** The flag i
5d50: 73 20 63 6c 65 61 72 65 64 20 61 73 20 73 6f 6f s cleared as soo
5d60: 6e 20 61 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c n as the journal
5d70: 20 66 69 6c 65 20 69 73 20 66 69 6e 61 6c 69 7a file is finaliz
5d80: 65 64 20 28 65 69 74 68 65 72 0a 2a 2a 20 20 20 ed (either.**
5d90: 62 79 20 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 by PagerCommitPh
5da0: 61 73 65 54 77 6f 20 6f 72 20 50 61 67 65 72 52 aseTwo or PagerR
5db0: 6f 6c 6c 62 61 63 6b 29 2e 20 49 66 20 61 6e 20 ollback). If an
5dc0: 49 4f 20 65 72 72 6f 72 20 70 72 65 76 65 6e 74 IO error prevent
5dd0: 73 20 74 68 65 0a 2a 2a 20 20 20 6a 6f 75 72 6e s the.** journ
5de0: 61 6c 20 66 69 6c 65 20 66 72 6f 6d 20 62 65 69 al file from bei
5df0: 6e 67 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 ng successfully
5e00: 66 69 6e 61 6c 69 7a 65 64 2c 20 74 68 65 20 73 finalized, the s
5e10: 65 74 4d 61 73 74 65 72 20 66 6c 61 67 0a 2a 2a etMaster flag.**
5e20: 20 20 20 69 73 20 63 6c 65 61 72 65 64 20 61 6e is cleared an
5e30: 79 77 61 79 20 28 61 6e 64 20 74 68 65 20 70 61 yway (and the pa
5e40: 67 65 72 20 77 69 6c 6c 20 6d 6f 76 65 20 74 6f ger will move to
5e50: 20 45 52 52 4f 52 20 73 74 61 74 65 29 2e 0a 2a ERROR state)..*
5e60: 2a 0a 2a 2a 20 64 6f 4e 6f 74 53 70 69 6c 6c 2c *.** doNotSpill,
5e70: 20 64 6f 4e 6f 74 53 79 6e 63 53 70 69 6c 6c 0a doNotSyncSpill.
5e80: 2a 2a 0a 2a 2a 20 20 20 54 68 65 73 65 20 74 77 **.** These tw
5e90: 6f 20 62 6f 6f 6c 65 61 6e 20 76 61 72 69 61 62 o boolean variab
5ea0: 6c 65 73 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 les control the
5eb0: 62 65 68 61 76 69 6f 75 72 20 6f 66 20 63 61 63 behaviour of cac
5ec0: 68 65 2d 73 70 69 6c 6c 73 0a 2a 2a 20 20 20 28 he-spills.** (
5ed0: 63 61 6c 6c 73 20 6d 61 64 65 20 62 79 20 74 68 calls made by th
5ee0: 65 20 70 63 61 63 68 65 20 6d 6f 64 75 6c 65 20 e pcache module
5ef0: 74 6f 20 74 68 65 20 70 61 67 65 72 53 74 72 65 to the pagerStre
5f00: 73 73 28 29 20 72 6f 75 74 69 6e 65 20 74 6f 0a ss() routine to.
5f10: 2a 2a 20 20 20 77 72 69 74 65 20 63 61 63 68 65 ** write cache
5f20: 64 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 d data to the fi
5f30: 6c 65 2d 73 79 73 74 65 6d 20 69 6e 20 6f 72 64 le-system in ord
5f40: 65 72 20 74 6f 20 66 72 65 65 20 75 70 20 6d 65 er to free up me
5f50: 6d 6f 72 79 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 57 mory)..**.** W
5f60: 68 65 6e 20 64 6f 4e 6f 74 53 70 69 6c 6c 20 69 hen doNotSpill i
5f70: 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 77 72 69 74 s non-zero, writ
5f80: 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 ing to the datab
5f90: 61 73 65 20 66 72 6f 6d 20 70 61 67 65 72 53 74 ase from pagerSt
5fa0: 72 65 73 73 28 29 0a 2a 2a 20 20 20 69 73 20 64 ress().** is d
5fb0: 69 73 61 62 6c 65 64 20 61 6c 74 6f 67 65 74 68 isabled altogeth
5fc0: 65 72 2e 20 54 68 69 73 20 69 73 20 64 6f 6e 65 er. This is done
5fd0: 20 69 6e 20 61 20 76 65 72 79 20 6f 62 73 63 75 in a very obscu
5fe0: 72 65 20 63 61 73 65 20 74 68 61 74 0a 2a 2a 20 re case that.**
5ff0: 20 20 63 6f 6d 65 73 20 75 70 20 64 75 72 69 6e comes up durin
6000: 67 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c g savepoint roll
6010: 62 61 63 6b 20 74 68 61 74 20 72 65 71 75 69 72 back that requir
6020: 65 73 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f es the pcache mo
6030: 64 75 6c 65 0a 2a 2a 20 20 20 74 6f 20 61 6c 6c dule.** to all
6040: 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 ocate a new page
6050: 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65 20 to prevent the
6060: 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 72 6f journal file fro
6070: 6d 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 0a m being written.
6080: 2a 2a 20 20 20 77 68 69 6c 65 20 69 74 20 69 73 ** while it is
6090: 20 62 65 69 6e 67 20 74 72 61 76 65 72 73 65 64 being traversed
60a0: 20 62 79 20 63 6f 64 65 20 69 6e 20 70 61 67 65 by code in page
60b0: 72 5f 70 6c 61 79 62 61 63 6b 28 29 2e 0a 2a 2a r_playback()..**
60c0: 20 0a 2a 2a 20 20 20 49 66 20 64 6f 4e 6f 74 53 .** If doNotS
60d0: 79 6e 63 53 70 69 6c 6c 20 69 73 20 6e 6f 6e 2d yncSpill is non-
60e0: 7a 65 72 6f 2c 20 77 72 69 74 69 6e 67 20 74 6f zero, writing to
60f0: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 72 the database fr
6100: 6f 6d 20 70 61 67 65 72 53 74 72 65 73 73 28 29 om pagerStress()
6110: 0a 2a 2a 20 20 20 69 73 20 70 65 72 6d 69 74 74 .** is permitt
6120: 65 64 2c 20 62 75 74 20 73 79 6e 63 69 6e 67 20 ed, but syncing
6130: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
6140: 20 69 73 20 6e 6f 74 2e 20 54 68 69 73 20 66 6c is not. This fl
6150: 61 67 20 69 73 20 73 65 74 0a 2a 2a 20 20 20 62 ag is set.** b
6160: 79 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 y sqlite3PagerWr
6170: 69 74 65 28 29 20 77 68 65 6e 20 74 68 65 20 66 ite() when the f
6180: 69 6c 65 2d 73 79 73 74 65 6d 20 73 65 63 74 6f ile-system secto
6190: 72 2d 73 69 7a 65 20 69 73 20 6c 61 72 67 65 72 r-size is larger
61a0: 20 74 68 61 6e 0a 2a 2a 20 20 20 74 68 65 20 64 than.** the d
61b0: 61 74 61 62 61 73 65 20 70 61 67 65 2d 73 69 7a atabase page-siz
61c0: 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72 e in order to pr
61d0: 65 76 65 6e 74 20 61 20 6a 6f 75 72 6e 61 6c 20 event a journal
61e0: 73 79 6e 63 20 66 72 6f 6d 20 68 61 70 70 65 6e sync from happen
61f0: 69 6e 67 20 0a 2a 2a 20 20 20 69 6e 20 62 65 74 ing .** in bet
6200: 77 65 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c ween the journal
6210: 6c 69 6e 67 20 6f 66 20 74 77 6f 20 70 61 67 65 ling of two page
6220: 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 73 65 s on the same se
6230: 63 74 6f 72 2e 20 0a 2a 2a 0a 2a 2a 20 73 75 62 ctor. .**.** sub
6240: 6a 49 6e 4d 65 6d 6f 72 79 0a 2a 2a 0a 2a 2a 20 jInMemory.**.**
6250: 20 20 54 68 69 73 20 69 73 20 61 20 62 6f 6f 6c This is a bool
6260: 65 61 6e 20 76 61 72 69 61 62 6c 65 2e 20 49 66 ean variable. If
6270: 20 74 72 75 65 2c 20 74 68 65 6e 20 61 6e 79 20 true, then any
6280: 72 65 71 75 69 72 65 64 20 73 75 62 2d 6a 6f 75 required sub-jou
6290: 72 6e 61 6c 0a 2a 2a 20 20 20 69 73 20 6f 70 65 rnal.** is ope
62a0: 6e 65 64 20 61 73 20 61 6e 20 69 6e 2d 6d 65 6d ned as an in-mem
62b0: 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ory journal file
62c0: 2e 20 49 66 20 66 61 6c 73 65 2c 20 74 68 65 6e . If false, then
62d0: 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 in-memory.**
62e0: 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 61 72 65 sub-journals are
62f0: 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72 20 69 only used for i
6300: 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 72 20 66 n-memory pager f
6310: 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 iles..**.** Th
6320: 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 is variable is u
6330: 70 64 61 74 65 64 20 62 79 20 74 68 65 20 75 70 pdated by the up
6340: 70 65 72 20 6c 61 79 65 72 20 65 61 63 68 20 74 per layer each t
6350: 69 6d 65 20 61 20 6e 65 77 20 0a 2a 2a 20 20 20 ime a new .**
6360: 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
6370: 6e 20 69 73 20 6f 70 65 6e 65 64 2e 0a 2a 2a 0a n is opened..**.
6380: 2a 2a 20 64 62 53 69 7a 65 2c 20 64 62 4f 72 69 ** dbSize, dbOri
6390: 67 53 69 7a 65 2c 20 64 62 46 69 6c 65 53 69 7a gSize, dbFileSiz
63a0: 65 0a 2a 2a 0a 2a 2a 20 20 20 56 61 72 69 61 62 e.**.** Variab
63b0: 6c 65 20 64 62 53 69 7a 65 20 69 73 20 73 65 74 le dbSize is set
63c0: 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f to the number o
63d0: 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 f pages in the d
63e0: 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
63f0: 20 20 20 49 74 20 69 73 20 76 61 6c 69 64 20 69 It is valid i
6400: 6e 20 50 41 47 45 52 5f 52 45 41 44 45 52 20 61 n PAGER_READER a
6410: 6e 64 20 68 69 67 68 65 72 20 73 74 61 74 65 73 nd higher states
6420: 20 28 61 6c 6c 20 73 74 61 74 65 73 20 65 78 63 (all states exc
6430: 65 70 74 20 66 6f 72 0a 2a 2a 20 20 20 4f 50 45 ept for.** OPE
6440: 4e 20 61 6e 64 20 45 52 52 4f 52 29 2e 20 0a 2a N and ERROR). .*
6450: 2a 0a 2a 2a 20 20 20 64 62 53 69 7a 65 20 69 73 *.** dbSize is
6460: 20 73 65 74 20 62 61 73 65 64 20 6f 6e 20 74 68 set based on th
6470: 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 e size of the da
6480: 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77 68 69 tabase file, whi
6490: 63 68 20 6d 61 79 20 62 65 20 0a 2a 2a 20 20 20 ch may be .**
64a0: 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 larger than the
64b0: 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 size of the data
64c0: 62 61 73 65 20 28 74 68 65 20 76 61 6c 75 65 20 base (the value
64d0: 73 74 6f 72 65 64 20 61 74 20 6f 66 66 73 65 74 stored at offset
64e0: 0a 2a 2a 20 20 20 32 38 20 6f 66 20 74 68 65 20 .** 28 of the
64f0: 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72 20 database header
6500: 62 79 20 74 68 65 20 62 74 72 65 65 29 2e 20 49 by the btree). I
6510: 66 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 f the size of th
6520: 65 20 66 69 6c 65 0a 2a 2a 20 20 20 69 73 20 6e e file.** is n
6530: 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 ot an integer mu
6540: 6c 74 69 70 6c 65 20 6f 66 20 74 68 65 20 70 61 ltiple of the pa
6550: 67 65 2d 73 69 7a 65 2c 20 74 68 65 20 76 61 6c ge-size, the val
6560: 75 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 ue stored in.**
6570: 20 20 64 62 53 69 7a 65 20 69 73 20 72 6f 75 6e dbSize is roun
6580: 64 65 64 20 64 6f 77 6e 20 28 69 2e 65 2e 20 61 ded down (i.e. a
6590: 20 35 4b 42 20 66 69 6c 65 20 77 69 74 68 20 32 5KB file with 2
65a0: 4b 20 70 61 67 65 2d 73 69 7a 65 20 68 61 73 20 K page-size has
65b0: 64 62 53 69 7a 65 3d 3d 32 29 2e 0a 2a 2a 20 20 dbSize==2)..**
65c0: 20 45 78 63 65 70 74 2c 20 61 6e 79 20 66 69 6c Except, any fil
65d0: 65 20 74 68 61 74 20 69 73 20 67 72 65 61 74 65 e that is greate
65e0: 72 20 74 68 61 6e 20 30 20 62 79 74 65 73 20 69 r than 0 bytes i
65f0: 6e 20 73 69 7a 65 20 69 73 20 63 6f 6e 73 69 64 n size is consid
6600: 65 72 65 64 0a 2a 2a 20 20 20 74 6f 20 68 61 76 ered.** to hav
6610: 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 70 e at least one p
6620: 61 67 65 2e 20 28 69 2e 65 2e 20 61 20 31 4b 42 age. (i.e. a 1KB
6630: 20 66 69 6c 65 20 77 69 74 68 20 32 4b 20 70 61 file with 2K pa
6640: 67 65 2d 73 69 7a 65 20 6c 65 61 64 73 0a 2a 2a ge-size leads.**
6650: 20 20 20 74 6f 20 64 62 53 69 7a 65 3d 3d 31 29 to dbSize==1)
6660: 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 75 72 69 6e 67 ..**.** During
6670: 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 a write-transac
6680: 74 69 6f 6e 2c 20 69 66 20 70 61 67 65 73 20 77 tion, if pages w
6690: 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 65 72 73 ith page-numbers
66a0: 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a greater than.**
66b0: 20 20 20 64 62 53 69 7a 65 20 61 72 65 20 6d 6f dbSize are mo
66c0: 64 69 66 69 65 64 20 69 6e 20 74 68 65 20 63 61 dified in the ca
66d0: 63 68 65 2c 20 64 62 53 69 7a 65 20 69 73 20 75 che, dbSize is u
66e0: 70 64 61 74 65 64 20 61 63 63 6f 72 64 69 6e 67 pdated according
66f0: 6c 79 2e 0a 2a 2a 20 20 20 53 69 6d 69 6c 61 72 ly..** Similar
6700: 6c 79 2c 20 69 66 20 74 68 65 20 64 61 74 61 62 ly, if the datab
6710: 61 73 65 20 69 73 20 74 72 75 6e 63 61 74 65 64 ase is truncated
6720: 20 75 73 69 6e 67 20 50 61 67 65 72 54 72 75 6e using PagerTrun
6730: 63 61 74 65 49 6d 61 67 65 28 29 2c 20 0a 2a 2a cateImage(), .**
6740: 20 20 20 64 62 53 69 7a 65 20 69 73 20 75 70 64 dbSize is upd
6750: 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 56 61 ated..**.** Va
6760: 72 69 61 62 6c 65 73 20 64 62 4f 72 69 67 53 69 riables dbOrigSi
6770: 7a 65 20 61 6e 64 20 64 62 46 69 6c 65 53 69 7a ze and dbFileSiz
6780: 65 20 61 72 65 20 76 61 6c 69 64 20 69 6e 20 73 e are valid in s
6790: 74 61 74 65 73 20 0a 2a 2a 20 20 20 50 41 47 45 tates .** PAGE
67a0: 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 20 R_WRITER_LOCKED
67b0: 61 6e 64 20 68 69 67 68 65 72 2e 20 64 62 4f 72 and higher. dbOr
67c0: 69 67 53 69 7a 65 20 69 73 20 61 20 63 6f 70 79 igSize is a copy
67d0: 20 6f 66 20 74 68 65 20 64 62 53 69 7a 65 0a 2a of the dbSize.*
67e0: 2a 20 20 20 76 61 72 69 61 62 6c 65 20 61 74 20 * variable at
67f0: 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 the start of the
6800: 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 74 transaction. It
6810: 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67 20 is used during
6820: 72 6f 6c 6c 62 61 63 6b 2c 0a 2a 2a 20 20 20 61 rollback,.** a
6830: 6e 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 nd to determine
6840: 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 70 whether or not p
6850: 61 67 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 ages need to be
6860: 6a 6f 75 72 6e 61 6c 6c 65 64 20 62 65 66 6f 72 journalled befor
6870: 65 0a 2a 2a 20 20 20 62 65 69 6e 67 20 6d 6f 64 e.** being mod
6880: 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 ified..**.** T
6890: 68 72 6f 75 67 68 6f 75 74 20 61 20 77 72 69 74 hroughout a writ
68a0: 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 64 e-transaction, d
68b0: 62 46 69 6c 65 53 69 7a 65 20 63 6f 6e 74 61 69 bFileSize contai
68c0: 6e 73 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a ns the size of.*
68d0: 2a 20 20 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 * the file on
68e0: 64 69 73 6b 20 69 6e 20 70 61 67 65 73 2e 20 49 disk in pages. I
68f0: 74 20 69 73 20 73 65 74 20 74 6f 20 61 20 63 6f t is set to a co
6900: 70 79 20 6f 66 20 64 62 53 69 7a 65 20 77 68 65 py of dbSize whe
6910: 6e 20 74 68 65 0a 2a 2a 20 20 20 77 72 69 74 65 n the.** write
6920: 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 -transaction is
6930: 66 69 72 73 74 20 6f 70 65 6e 65 64 2c 20 61 6e first opened, an
6940: 64 20 75 70 64 61 74 65 64 20 77 68 65 6e 20 56 d updated when V
6950: 46 53 20 63 61 6c 6c 73 20 61 72 65 20 6d 61 64 FS calls are mad
6960: 65 0a 2a 2a 20 20 20 74 6f 20 77 72 69 74 65 20 e.** to write
6970: 6f 72 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 or truncate the
6980: 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e database file on
6990: 20 64 69 73 6b 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 disk. .**.**
69a0: 54 68 65 20 6f 6e 6c 79 20 72 65 61 73 6f 6e 20 The only reason
69b0: 74 68 65 20 64 62 46 69 6c 65 53 69 7a 65 20 76 the dbFileSize v
69c0: 61 72 69 61 62 6c 65 20 69 73 20 72 65 71 75 69 ariable is requi
69d0: 72 65 64 20 69 73 20 74 6f 20 73 75 70 70 72 65 red is to suppre
69e0: 73 73 20 0a 2a 2a 20 20 20 75 6e 6e 65 63 65 73 ss .** unneces
69f0: 73 61 72 79 20 63 61 6c 6c 73 20 74 6f 20 78 54 sary calls to xT
6a00: 72 75 6e 63 61 74 65 28 29 20 61 66 74 65 72 20 runcate() after
6a10: 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 72 61 committing a tra
6a20: 6e 73 61 63 74 69 6f 6e 2e 20 49 66 2c 20 0a 2a nsaction. If, .*
6a30: 2a 20 20 20 77 68 65 6e 20 61 20 74 72 61 6e 73 * when a trans
6a40: 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 action is commit
6a50: 74 65 64 2c 20 74 68 65 20 64 62 46 69 6c 65 53 ted, the dbFileS
6a60: 69 7a 65 20 76 61 72 69 61 62 6c 65 20 69 6e 64 ize variable ind
6a70: 69 63 61 74 65 73 20 0a 2a 2a 20 20 20 74 68 61 icates .** tha
6a80: 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 t the database f
6a90: 69 6c 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 ile is larger th
6aa0: 61 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 an the database
6ab0: 69 6d 61 67 65 20 28 50 61 67 65 72 2e 64 62 53 image (Pager.dbS
6ac0: 69 7a 65 29 2c 20 0a 2a 2a 20 20 20 70 61 67 65 ize), .** page
6ad0: 72 5f 74 72 75 6e 63 61 74 65 28 29 20 69 73 20 r_truncate() is
6ae0: 63 61 6c 6c 65 64 2e 20 54 68 65 20 70 61 67 65 called. The page
6af0: 72 5f 74 72 75 6e 63 61 74 65 28 29 20 63 61 6c r_truncate() cal
6b00: 6c 20 75 73 65 73 20 78 46 69 6c 65 73 69 7a 65 l uses xFilesize
6b10: 28 29 0a 2a 2a 20 20 20 74 6f 20 6d 65 61 73 75 ().** to measu
6b20: 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 re the database
6b30: 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2c 20 61 6e file on disk, an
6b40: 64 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 73 d then truncates
6b50: 20 69 74 20 69 66 20 72 65 71 75 69 72 65 64 2e it if required.
6b60: 0a 2a 2a 20 20 20 64 62 46 69 6c 65 53 69 7a 65 .** dbFileSize
6b70: 20 69 73 20 6e 6f 74 20 75 73 65 64 20 77 68 65 is not used whe
6b80: 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 n rolling back a
6b90: 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 6e transaction. In
6ba0: 20 74 68 69 73 20 63 61 73 65 0a 2a 2a 20 20 20 this case.**
6bb0: 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 29 pager_truncate()
6bc0: 20 69 73 20 63 61 6c 6c 65 64 20 75 6e 63 6f 6e is called uncon
6bd0: 64 69 74 69 6f 6e 61 6c 6c 79 20 28 77 68 69 63 ditionally (whic
6be0: 68 20 6d 65 61 6e 73 20 74 68 65 72 65 20 6d 61 h means there ma
6bf0: 79 20 62 65 0a 2a 2a 20 20 20 61 20 63 61 6c 6c y be.** a call
6c00: 20 74 6f 20 78 46 69 6c 65 73 69 7a 65 28 29 20 to xFilesize()
6c10: 74 68 61 74 20 69 73 20 6e 6f 74 20 73 74 72 69 that is not stri
6c20: 63 74 6c 79 20 72 65 71 75 69 72 65 64 29 2e 20 ctly required).
6c30: 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c 0a In either case,.
6c40: 2a 2a 20 20 20 70 61 67 65 72 5f 74 72 75 6e 63 ** pager_trunc
6c50: 61 74 65 28 29 20 6d 61 79 20 63 61 75 73 65 20 ate() may cause
6c60: 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 63 6f the file to beco
6c70: 6d 65 20 73 6d 61 6c 6c 65 72 20 6f 72 20 6c 61 me smaller or la
6c80: 72 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 64 62 48 69 rger..**.** dbHi
6c90: 6e 74 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 20 20 54 ntSize.**.** T
6ca0: 68 65 20 64 62 48 69 6e 74 53 69 7a 65 20 76 61 he dbHintSize va
6cb0: 72 69 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 riable is used t
6cc0: 6f 20 6c 69 6d 69 74 20 74 68 65 20 6e 75 6d 62 o limit the numb
6cd0: 65 72 20 6f 66 20 63 61 6c 6c 73 20 6d 61 64 65 er of calls made
6ce0: 20 74 6f 0a 2a 2a 20 20 20 74 68 65 20 56 46 53 to.** the VFS
6cf0: 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 46 43 xFileControl(FC
6d00: 4e 54 4c 5f 53 49 5a 45 5f 48 49 4e 54 29 20 6d NTL_SIZE_HINT) m
6d10: 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 ethod. .**.**
6d20: 64 62 48 69 6e 74 53 69 7a 65 20 69 73 20 73 65 dbHintSize is se
6d30: 74 20 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 t to a copy of t
6d40: 68 65 20 64 62 53 69 7a 65 20 76 61 72 69 61 62 he dbSize variab
6d50: 6c 65 20 77 68 65 6e 20 61 0a 2a 2a 20 20 20 77 le when a.** w
6d60: 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
6d70: 20 69 73 20 6f 70 65 6e 65 64 20 28 61 74 20 74 is opened (at t
6d80: 68 65 20 73 61 6d 65 20 74 69 6d 65 20 61 73 20 he same time as
6d90: 64 62 46 69 6c 65 53 69 7a 65 20 61 6e 64 0a 2a dbFileSize and.*
6da0: 2a 20 20 20 64 62 4f 72 69 67 53 69 7a 65 29 2e * dbOrigSize).
6db0: 20 49 66 20 74 68 65 20 78 46 69 6c 65 43 6f 6e If the xFileCon
6dc0: 74 72 6f 6c 28 46 43 4e 54 4c 5f 53 49 5a 45 5f trol(FCNTL_SIZE_
6dd0: 48 49 4e 54 29 20 6d 65 74 68 6f 64 20 69 73 20 HINT) method is
6de0: 63 61 6c 6c 65 64 2c 0a 2a 2a 20 20 20 64 62 48 called,.** dbH
6df0: 69 6e 74 53 69 7a 65 20 69 73 20 69 6e 63 72 65 intSize is incre
6e00: 61 73 65 64 20 74 6f 20 74 68 65 20 6e 75 6d 62 ased to the numb
6e10: 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 61 74 er of pages that
6e20: 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 correspond to t
6e30: 68 65 0a 2a 2a 20 20 20 73 69 7a 65 2d 68 69 6e he.** size-hin
6e40: 74 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 t passed to the
6e50: 6d 65 74 68 6f 64 20 63 61 6c 6c 2e 20 53 65 65 method call. See
6e60: 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 pager_write_pag
6e70: 65 6c 69 73 74 28 29 20 66 6f 72 20 0a 2a 2a 20 elist() for .**
6e80: 20 20 64 65 74 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a details..**.**
6e90: 20 65 72 72 43 6f 64 65 0a 2a 2a 0a 2a 2a 20 20 errCode.**.**
6ea0: 20 54 68 65 20 50 61 67 65 72 2e 65 72 72 43 6f The Pager.errCo
6eb0: 64 65 20 76 61 72 69 61 62 6c 65 20 69 73 20 6f de variable is o
6ec0: 6e 6c 79 20 65 76 65 72 20 75 73 65 64 20 69 6e nly ever used in
6ed0: 20 50 41 47 45 52 5f 45 52 52 4f 52 20 73 74 61 PAGER_ERROR sta
6ee0: 74 65 2e 20 49 74 0a 2a 2a 20 20 20 69 73 20 73 te. It.** is s
6ef0: 65 74 20 74 6f 20 7a 65 72 6f 20 69 6e 20 61 6c et to zero in al
6f00: 6c 20 6f 74 68 65 72 20 73 74 61 74 65 73 2e 20 l other states.
6f10: 49 6e 20 50 41 47 45 52 5f 45 52 52 4f 52 20 73 In PAGER_ERROR s
6f20: 74 61 74 65 2c 20 50 61 67 65 72 2e 65 72 72 43 tate, Pager.errC
6f30: 6f 64 65 20 0a 2a 2a 20 20 20 69 73 20 61 6c 77 ode .** is alw
6f40: 61 79 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 ays set to SQLIT
6f50: 45 5f 46 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 49 E_FULL, SQLITE_I
6f60: 4f 45 52 52 20 6f 72 20 6f 6e 65 20 6f 66 20 74 OERR or one of t
6f70: 68 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f he SQLITE_IOERR_
6f80: 58 58 58 20 0a 2a 2a 20 20 20 73 75 62 2d 63 6f XXX .** sub-co
6f90: 64 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 des..*/.struct P
6fa0: 61 67 65 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 ager {. sqlite3
6fb0: 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 _vfs *pVfs;
6fc0: 20 20 20 20 20 2f 2a 20 4f 53 20 66 75 6e 63 74 /* OS funct
6fd0: 69 6f 6e 73 20 74 6f 20 75 73 65 20 66 6f 72 20 ions to use for
6fe0: 49 4f 20 2a 2f 0a 20 20 75 38 20 65 78 63 6c 75 IO */. u8 exclu
6ff0: 73 69 76 65 4d 6f 64 65 3b 20 20 20 20 20 20 20 siveMode;
7000: 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 /* Boolean.
7010: 54 72 75 65 20 69 66 20 6c 6f 63 6b 69 6e 67 5f True if locking_
7020: 6d 6f 64 65 3d 3d 45 58 43 4c 55 53 49 56 45 20 mode==EXCLUSIVE
7030: 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c 4d */. u8 journalM
7040: 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ode;
7050: 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 50 /* One of the P
7060: 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
7070: 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 _* values */. u
7080: 38 20 75 73 65 4a 6f 75 72 6e 61 6c 3b 20 20 20 8 useJournal;
7090: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
70a0: 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 e a rollback jou
70b0: 72 6e 61 6c 20 6f 6e 20 74 68 69 73 20 66 69 6c rnal on this fil
70c0: 65 20 2a 2f 0a 20 20 75 38 20 6e 6f 53 79 6e 63 e */. u8 noSync
70d0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
70e0: 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 79 6e /* Do not syn
70f0: 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 66 c the journal if
7100: 20 74 72 75 65 20 2a 2f 0a 20 20 75 38 20 66 75 true */. u8 fu
7110: 6c 6c 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 llSync;
7120: 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 65 78 74 /* Do ext
7130: 72 61 20 73 79 6e 63 73 20 6f 66 20 74 68 65 20 ra syncs of the
7140: 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 6f 62 75 journal for robu
7150: 73 74 6e 65 73 73 20 2a 2f 0a 20 20 75 38 20 63 stness */. u8 c
7160: 6b 70 74 53 79 6e 63 46 6c 61 67 73 3b 20 20 20 kptSyncFlags;
7170: 20 20 20 20 20 20 20 20 2f 2a 20 53 59 4e 43 5f /* SYNC_
7180: 4e 4f 52 4d 41 4c 20 6f 72 20 53 59 4e 43 5f 46 NORMAL or SYNC_F
7190: 55 4c 4c 20 66 6f 72 20 63 68 65 63 6b 70 6f 69 ULL for checkpoi
71a0: 6e 74 20 2a 2f 0a 20 20 75 38 20 77 61 6c 53 79 nt */. u8 walSy
71b0: 6e 63 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 ncFlags;
71c0: 20 20 20 20 2f 2a 20 53 59 4e 43 5f 4e 4f 52 4d /* SYNC_NORM
71d0: 41 4c 20 6f 72 20 53 59 4e 43 5f 46 55 4c 4c 20 AL or SYNC_FULL
71e0: 66 6f 72 20 77 61 6c 20 77 72 69 74 65 73 20 2a for wal writes *
71f0: 2f 0a 20 20 75 38 20 73 79 6e 63 46 6c 61 67 73 /. u8 syncFlags
7200: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
7210: 2f 2a 20 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f /* SYNC_NORMAL o
7220: 72 20 53 59 4e 43 5f 46 55 4c 4c 20 6f 74 68 65 r SYNC_FULL othe
7230: 72 77 69 73 65 20 2a 2f 0a 20 20 75 38 20 74 65 rwise */. u8 te
7240: 6d 70 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20 mpFile;
7250: 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e /* zFilen
7260: 61 6d 65 20 69 73 20 61 20 74 65 6d 70 6f 72 61 ame is a tempora
7270: 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 ry file */. u8
7280: 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20 readOnly;
7290: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
72a0: 20 66 6f 72 20 61 20 72 65 61 64 2d 6f 6e 6c 79 for a read-only
72b0: 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 database */. u
72c0: 38 20 6d 65 6d 44 62 3b 20 20 20 20 20 20 20 20 8 memDb;
72d0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
72e0: 75 65 20 74 6f 20 69 6e 68 69 62 69 74 20 61 6c ue to inhibit al
72f0: 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 0a 20 l file I/O */..
7300: 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /**************
7310: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a ************. *
7350: 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
7360: 62 6c 6f 63 6b 20 63 6f 6e 74 61 69 6e 73 20 74 block contains t
7370: 68 6f 73 65 20 63 6c 61 73 73 20 6d 65 6d 62 65 hose class membe
7380: 72 73 20 74 68 61 74 20 63 68 61 6e 67 65 20 64 rs that change d
7390: 75 72 69 6e 67 0a 20 20 2a 2a 20 72 6f 75 74 69 uring. ** routi
73a0: 6e 65 20 6f 70 65 72 74 69 6f 6e 2e 20 20 43 6c ne opertion. Cl
73b0: 61 73 73 20 6d 65 6d 62 65 72 73 20 6e 6f 74 20 ass members not
73c0: 69 6e 20 74 68 69 73 20 62 6c 6f 63 6b 20 61 72 in this block ar
73d0: 65 20 65 69 74 68 65 72 20 66 69 78 65 64 0a 20 e either fixed.
73e0: 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 70 61 67 ** when the pag
73f0: 65 72 20 69 73 20 66 69 72 73 74 20 63 72 65 61 er is first crea
7400: 74 65 64 20 6f 72 20 65 6c 73 65 20 6f 6e 6c 79 ted or else only
7410: 20 63 68 61 6e 67 65 20 77 68 65 6e 20 74 68 65 change when the
7420: 72 65 20 69 73 20 61 0a 20 20 2a 2a 20 73 69 67 re is a. ** sig
7430: 6e 69 66 69 63 61 6e 74 20 6d 6f 64 65 20 63 68 nificant mode ch
7440: 61 6e 67 65 20 28 73 75 63 68 20 61 73 20 63 68 ange (such as ch
7450: 61 6e 67 69 6e 67 20 74 68 65 20 70 61 67 65 5f anging the page_
7460: 73 69 7a 65 2c 20 6c 6f 63 6b 69 6e 67 5f 6d 6f size, locking_mo
7470: 64 65 2c 0a 20 20 2a 2a 20 6f 72 20 74 68 65 20 de,. ** or the
7480: 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 29 2e 20 20 journal_mode).
7490: 46 72 6f 6d 20 61 6e 6f 74 68 65 72 20 76 69 65 From another vie
74a0: 77 2c 20 74 68 65 73 65 20 63 6c 61 73 73 20 6d w, these class m
74b0: 65 6d 62 65 72 73 20 64 65 73 63 72 69 62 65 0a embers describe.
74c0: 20 20 2a 2a 20 74 68 65 20 22 73 74 61 74 65 22 ** the "state"
74d0: 20 6f 66 20 74 68 65 20 70 61 67 65 72 2c 20 77 of the pager, w
74e0: 68 69 6c 65 20 6f 74 68 65 72 20 63 6c 61 73 73 hile other class
74f0: 20 6d 65 6d 62 65 72 73 20 64 65 73 63 72 69 62 members describ
7500: 65 20 74 68 65 0a 20 20 2a 2a 20 22 63 6f 6e 66 e the. ** "conf
7510: 69 67 75 72 61 74 69 6f 6e 22 20 6f 66 20 74 68 iguration" of th
7520: 65 20 70 61 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 e pager.. */.
7530: 75 38 20 65 53 74 61 74 65 3b 20 20 20 20 20 20 u8 eState;
7540: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
7550: 61 67 65 72 20 73 74 61 74 65 20 28 4f 50 45 4e ager state (OPEN
7560: 2c 20 52 45 41 44 45 52 2c 20 57 52 49 54 45 52 , READER, WRITER
7570: 5f 4c 4f 43 4b 45 44 2e 2e 29 20 2a 2f 0a 20 20 _LOCKED..) */.
7580: 75 38 20 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 u8 eLock;
7590: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
75a0: 75 72 72 65 6e 74 20 6c 6f 63 6b 20 68 65 6c 64 urrent lock held
75b0: 20 6f 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c on database fil
75c0: 65 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 e */. u8 change
75d0: 43 6f 75 6e 74 44 6f 6e 65 3b 20 20 20 20 20 20 CountDone;
75e0: 20 20 20 2f 2a 20 53 65 74 20 61 66 74 65 72 20 /* Set after
75f0: 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 74 68 65 incrementing the
7600: 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 change-counter
7610: 2a 2f 0a 20 20 75 38 20 73 65 74 4d 61 73 74 65 */. u8 setMaste
7620: 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
7630: 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 6d 2d /* True if a m-
7640: 6a 20 6e 61 6d 65 20 68 61 73 20 62 65 65 6e 20 j name has been
7650: 77 72 69 74 74 65 6e 20 74 6f 20 6a 72 6e 6c 20 written to jrnl
7660: 2a 2f 0a 20 20 75 38 20 64 6f 4e 6f 74 53 70 69 */. u8 doNotSpi
7670: 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ll;
7680: 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 70 69 6c 6c /* Do not spill
7690: 20 74 68 65 20 63 61 63 68 65 20 77 68 65 6e 20 the cache when
76a0: 6e 6f 6e 2d 7a 65 72 6f 20 2a 2f 0a 20 20 75 38 non-zero */. u8
76b0: 20 64 6f 4e 6f 74 53 79 6e 63 53 70 69 6c 6c 3b doNotSyncSpill;
76c0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 /* Do
76d0: 6e 6f 74 20 64 6f 20 61 20 73 70 69 6c 6c 20 74 not do a spill t
76e0: 68 61 74 20 72 65 71 75 69 72 65 73 20 6a 72 6e hat requires jrn
76f0: 6c 20 73 79 6e 63 20 2a 2f 0a 20 20 75 38 20 73 l sync */. u8 s
7700: 75 62 6a 49 6e 4d 65 6d 6f 72 79 3b 20 20 20 20 ubjInMemory;
7710: 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
7720: 74 6f 20 75 73 65 20 69 6e 2d 6d 65 6d 6f 72 79 to use in-memory
7730: 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f sub-journals */
7740: 0a 20 20 50 67 6e 6f 20 64 62 53 69 7a 65 3b 20 . Pgno dbSize;
7750: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
7760: 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
7770: 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 s in the databas
7780: 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 4f 72 e */. Pgno dbOr
7790: 69 67 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 igSize;
77a0: 20 20 20 2f 2a 20 64 62 53 69 7a 65 20 62 65 66 /* dbSize bef
77b0: 6f 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ore the current
77c0: 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 transaction */.
77d0: 20 50 67 6e 6f 20 64 62 46 69 6c 65 53 69 7a 65 Pgno dbFileSize
77e0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
77f0: 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
7800: 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
7810: 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 file */. Pgno d
7820: 62 48 69 6e 74 53 69 7a 65 3b 20 20 20 20 20 20 bHintSize;
7830: 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 70 /* Value p
7840: 61 73 73 65 64 20 74 6f 20 46 43 4e 54 4c 5f 53 assed to FCNTL_S
7850: 49 5a 45 5f 48 49 4e 54 20 63 61 6c 6c 20 2a 2f IZE_HINT call */
7860: 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 . int errCode;
7870: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
7880: 2a 20 4f 6e 65 20 6f 66 20 73 65 76 65 72 61 6c * One of several
7890: 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 kinds of errors
78a0: 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 63 3b 20 */. int nRec;
78b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78c0: 20 20 2f 2a 20 50 61 67 65 73 20 6a 6f 75 72 6e /* Pages journ
78d0: 61 6c 6c 65 64 20 73 69 6e 63 65 20 6c 61 73 74 alled since last
78e0: 20 6a 2d 68 65 61 64 65 72 20 77 72 69 74 74 65 j-header writte
78f0: 6e 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d n */. u32 cksum
7900: 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 Init;
7910: 20 20 20 2f 2a 20 51 75 61 73 69 2d 72 61 6e 64 /* Quasi-rand
7920: 6f 6d 20 76 61 6c 75 65 20 61 64 64 65 64 20 74 om value added t
7930: 6f 20 65 76 65 72 79 20 63 68 65 63 6b 73 75 6d o every checksum
7940: 20 2a 2f 0a 20 20 75 33 32 20 6e 53 75 62 52 65 */. u32 nSubRe
7950: 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
7960: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 /* Number of r
7970: 65 63 6f 72 64 73 20 77 72 69 74 74 65 6e 20 74 ecords written t
7980: 6f 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f o sub-journal */
7990: 0a 20 20 42 69 74 76 65 63 20 2a 70 49 6e 4a 6f . Bitvec *pInJo
79a0: 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 2f urnal; /
79b0: 2a 20 4f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 * One bit for ea
79c0: 63 68 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 ch page in the d
79d0: 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
79e0: 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
79f0: 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a fd; /*
7a00: 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 File descriptor
7a10: 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 2a 2f for database */
7a20: 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
7a30: 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 20 20 2f *jfd; /
7a40: 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f * File descripto
7a50: 72 20 66 6f 72 20 6d 61 69 6e 20 6a 6f 75 72 6e r for main journ
7a60: 61 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f al */. sqlite3_
7a70: 66 69 6c 65 20 2a 73 6a 66 64 3b 20 20 20 20 20 file *sjfd;
7a80: 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 /* File desc
7a90: 72 69 70 74 6f 72 20 66 6f 72 20 73 75 62 2d 6a riptor for sub-j
7aa0: 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 ournal */. i64
7ab0: 6a 6f 75 72 6e 61 6c 4f 66 66 3b 20 20 20 20 20 journalOff;
7ac0: 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 /* Curre
7ad0: 6e 74 20 77 72 69 74 65 20 6f 66 66 73 65 74 20 nt write offset
7ae0: 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 in the journal f
7af0: 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 ile */. i64 jou
7b00: 72 6e 61 6c 48 64 72 3b 20 20 20 20 20 20 20 20 rnalHdr;
7b10: 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 /* Byte off
7b20: 73 65 74 20 74 6f 20 70 72 65 76 69 6f 75 73 20 set to previous
7b30: 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a journal header *
7b40: 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b /. sqlite3_back
7b50: 75 70 20 2a 70 42 61 63 6b 75 70 3b 20 20 20 20 up *pBackup;
7b60: 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6c 69 /* Pointer to li
7b70: 73 74 20 6f 66 20 6f 6e 67 6f 69 6e 67 20 62 61 st of ongoing ba
7b80: 63 6b 75 70 20 70 72 6f 63 65 73 73 65 73 20 2a ckup processes *
7b90: 2f 0a 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 /. PagerSavepoi
7ba0: 6e 74 20 2a 61 53 61 76 65 70 6f 69 6e 74 3b 20 nt *aSavepoint;
7bb0: 2f 2a 20 41 72 72 61 79 20 6f 66 20 61 63 74 69 /* Array of acti
7bc0: 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f ve savepoints */
7bd0: 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e . int nSavepoin
7be0: 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f t; /
7bf0: 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d * Number of elem
7c00: 65 6e 74 73 20 69 6e 20 61 53 61 76 65 70 6f 69 ents in aSavepoi
7c10: 6e 74 5b 5d 20 2a 2f 0a 20 20 63 68 61 72 20 64 nt[] */. char d
7c20: 62 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b 20 20 bFileVers[16];
7c30: 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 73 /* Changes
7c40: 20 77 68 65 6e 65 76 65 72 20 64 61 74 61 62 61 whenever databa
7c50: 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 73 20 se file changes
7c60: 2a 2f 0a 0a 20 20 76 6f 69 64 20 2a 70 4d 61 70 */.. void *pMap
7c70: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
7c80: 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 70 70 /* Memory mapp
7c90: 65 64 20 70 72 65 66 69 78 20 6f 66 20 64 61 74 ed prefix of dat
7ca0: 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 abase file */.
7cb0: 69 36 34 20 6e 4d 61 70 3b 20 20 20 20 20 20 20 i64 nMap;
7cc0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
7cd0: 69 7a 65 20 6f 66 20 6d 61 70 70 69 6e 67 20 61 ize of mapping a
7ce0: 74 20 70 4d 61 70 20 69 6e 20 62 79 74 65 73 20 t pMap in bytes
7cf0: 2a 2f 20 0a 20 20 69 6e 74 20 6e 4d 6d 61 70 4f */ . int nMmapO
7d00: 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ut;
7d10: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d /* Number of m
7d20: 6d 61 70 20 70 61 67 65 73 20 63 75 72 72 65 6e map pages curren
7d30: 74 6c 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 tly outstanding
7d40: 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 46 72 65 */. PgHdr *pFre
7d50: 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
7d60: 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 72 65 65 /* List of free
7d70: 20 6d 6d 61 70 20 70 61 67 65 20 68 65 61 64 65 mmap page heade
7d80: 72 73 20 28 70 44 69 72 74 79 29 20 2a 2f 0a 20 rs (pDirty) */.
7d90: 20 2f 2a 0a 20 20 2a 2a 20 45 6e 64 20 6f 66 20 /*. ** End of
7da0: 74 68 65 20 72 6f 75 74 69 6e 65 6c 79 2d 63 68 the routinely-ch
7db0: 61 6e 67 69 6e 67 20 63 6c 61 73 73 20 6d 65 6d anging class mem
7dc0: 62 65 72 73 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a bers. *********
7dd0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7de0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7df0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e00: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7e10: 2a 2a 2f 0a 0a 20 20 75 31 36 20 6e 45 78 74 72 **/.. u16 nExtr
7e20: 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 a;
7e30: 20 20 20 2f 2a 20 41 64 64 20 74 68 69 73 20 6d /* Add this m
7e40: 61 6e 79 20 62 79 74 65 73 20 74 6f 20 65 61 63 any bytes to eac
7e50: 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 h in-memory page
7e60: 20 2a 2f 0a 20 20 69 31 36 20 6e 52 65 73 65 72 */. i16 nReser
7e70: 76 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ve;
7e80: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 /* Number of u
7e90: 6e 75 73 65 64 20 62 79 74 65 73 20 61 74 20 65 nused bytes at e
7ea0: 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 nd of each page
7eb0: 2a 2f 0a 20 20 75 33 32 20 76 66 73 46 6c 61 67 */. u32 vfsFlag
7ec0: 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
7ed0: 20 2f 2a 20 46 6c 61 67 73 20 66 6f 72 20 73 71 /* Flags for sq
7ee0: 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 lite3_vfs.xOpen(
7ef0: 29 20 2a 2f 0a 20 20 75 33 32 20 73 65 63 74 6f ) */. u32 secto
7f00: 72 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 rSize;
7f10: 20 20 20 2f 2a 20 41 73 73 75 6d 65 64 20 73 65 /* Assumed se
7f20: 63 74 6f 72 20 73 69 7a 65 20 64 75 72 69 6e 67 ctor size during
7f30: 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 rollback */. i
7f40: 6e 74 20 70 61 67 65 53 69 7a 65 3b 20 20 20 20 nt pageSize;
7f50: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
7f60: 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
7f70: 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e a page */. Pgn
7f80: 6f 20 6d 78 50 67 6e 6f 3b 20 20 20 20 20 20 20 o mxPgno;
7f90: 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 /* Maxi
7fa0: 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 69 7a 65 mum allowed size
7fb0: 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
7fc0: 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 */. i64 journa
7fd0: 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 20 lSizeLimit;
7fe0: 20 20 2f 2a 20 53 69 7a 65 20 6c 69 6d 69 74 20 /* Size limit
7ff0: 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a for persistent j
8000: 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a ournal files */.
8010: 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d char *zFilenam
8020: 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
8030: 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 Name of the dat
8040: 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 abase file */.
8050: 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 char *zJournal;
8060: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
8070: 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e ame of the journ
8080: 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 al file */. int
8090: 20 28 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 29 (*xBusyHandler)
80a0: 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 46 75 6e 63 (void*); /* Func
80b0: 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 68 65 tion to call whe
80c0: 6e 20 62 75 73 79 20 2a 2f 0a 20 20 76 6f 69 64 n busy */. void
80d0: 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 *pBusyHandlerAr
80e0: 67 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 g; /* Conte
80f0: 78 74 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 xt argument for
8100: 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a xBusyHandler */.
8110: 20 20 69 6e 74 20 61 53 74 61 74 5b 33 5d 3b 20 int aStat[3];
8120: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
8130: 20 54 6f 74 61 6c 20 63 61 63 68 65 20 68 69 74 Total cache hit
8140: 73 2c 20 6d 69 73 73 65 73 20 61 6e 64 20 77 72 s, misses and wr
8150: 69 74 65 73 20 2a 2f 0a 23 69 66 64 65 66 20 53 ites */.#ifdef S
8160: 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 6e 74 QLITE_TEST. int
8170: 20 6e 52 65 61 64 3b 20 20 20 20 20 20 20 20 20 nRead;
8180: 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
8190: 62 61 73 65 20 70 61 67 65 73 20 72 65 61 64 20 base pages read
81a0: 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 */.#endif. void
81b0: 20 28 2a 78 52 65 69 6e 69 74 65 72 29 28 44 62 (*xReiniter)(Db
81c0: 50 61 67 65 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20 Page*); /* Call
81d0: 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 this routine whe
81e0: 6e 20 72 65 6c 6f 61 64 69 6e 67 20 70 61 67 65 n reloading page
81f0: 73 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 s */.#ifdef SQLI
8200: 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 76 TE_HAS_CODEC. v
8210: 6f 69 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 oid *(*xCodec)(v
8220: 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c oid*,void*,Pgno,
8230: 69 6e 74 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 int); /* Routine
8240: 20 66 6f 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67 for en/decoding
8250: 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 data */. void
8260: 28 2a 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 (*xCodecSizeChng
8270: 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29 )(void*,int,int)
8280: 3b 20 2f 2a 20 4e 6f 74 69 66 79 20 6f 66 20 70 ; /* Notify of p
8290: 61 67 65 20 73 69 7a 65 20 63 68 61 6e 67 65 73 age size changes
82a0: 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f */. void (*xCo
82b0: 64 65 63 46 72 65 65 29 28 76 6f 69 64 2a 29 3b decFree)(void*);
82c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
82d0: 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 Destructor for t
82e0: 68 65 20 63 6f 64 65 63 20 2a 2f 0a 20 20 76 6f he codec */. vo
82f0: 69 64 20 2a 70 43 6f 64 65 63 3b 20 20 20 20 20 id *pCodec;
8300: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
8310: 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 st argument to x
8320: 43 6f 64 65 63 2e 2e 2e 20 6d 65 74 68 6f 64 73 Codec... methods
8330: 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 68 61 */.#endif. cha
8340: 72 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 20 r *pTmpSpace;
8350: 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
8360: 72 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 r.pageSize bytes
8370: 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74 6d of space for tm
8380: 70 20 75 73 65 20 2a 2f 0a 20 20 50 43 61 63 68 p use */. PCach
8390: 65 20 2a 70 50 43 61 63 68 65 3b 20 20 20 20 20 e *pPCache;
83a0: 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
83b0: 72 20 74 6f 20 70 61 67 65 20 63 61 63 68 65 20 r to page cache
83c0: 6f 62 6a 65 63 74 20 2a 2f 0a 23 69 66 6e 64 65 object */.#ifnde
83d0: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 f SQLITE_OMIT_WA
83e0: 4c 0a 20 20 57 61 6c 20 2a 70 57 61 6c 3b 20 20 L. Wal *pWal;
83f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8400: 2f 2a 20 57 72 69 74 65 2d 61 68 65 61 64 20 6c /* Write-ahead l
8410: 6f 67 20 75 73 65 64 20 62 79 20 22 6a 6f 75 72 og used by "jour
8420: 6e 61 6c 5f 6d 6f 64 65 3d 77 61 6c 22 20 2a 2f nal_mode=wal" */
8430: 0a 20 20 63 68 61 72 20 2a 7a 57 61 6c 3b 20 20 . char *zWal;
8440: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
8450: 2a 20 46 69 6c 65 20 6e 61 6d 65 20 66 6f 72 20 * File name for
8460: 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 20 write-ahead log
8470: 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a */.#endif.};../*
8480: 0a 2a 2a 20 49 6e 64 65 78 65 73 20 66 6f 72 20 .** Indexes for
8490: 75 73 65 20 77 69 74 68 20 50 61 67 65 72 2e 61 use with Pager.a
84a0: 53 74 61 74 5b 5d 2e 20 54 68 65 20 50 61 67 65 Stat[]. The Page
84b0: 72 2e 61 53 74 61 74 5b 5d 20 61 72 72 61 79 20 r.aStat[] array
84c0: 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 contains.** the
84d0: 76 61 6c 75 65 73 20 61 63 63 65 73 73 65 64 20 values accessed
84e0: 62 79 20 70 61 73 73 69 6e 67 20 53 51 4c 49 54 by passing SQLIT
84f0: 45 5f 44 42 53 54 41 54 55 53 5f 43 41 43 48 45 E_DBSTATUS_CACHE
8500: 5f 48 49 54 2c 20 43 41 43 48 45 5f 4d 49 53 53 _HIT, CACHE_MISS
8510: 20 0a 2a 2a 20 6f 72 20 43 41 43 48 45 5f 57 52 .** or CACHE_WR
8520: 49 54 45 20 74 6f 20 73 71 6c 69 74 65 33 5f 64 ITE to sqlite3_d
8530: 62 5f 73 74 61 74 75 73 28 29 2e 0a 2a 2f 0a 23 b_status()..*/.#
8540: 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53 54 41 define PAGER_STA
8550: 54 5f 48 49 54 20 20 20 30 0a 23 64 65 66 69 6e T_HIT 0.#defin
8560: 65 20 50 41 47 45 52 5f 53 54 41 54 5f 4d 49 53 e PAGER_STAT_MIS
8570: 53 20 20 31 0a 23 64 65 66 69 6e 65 20 50 41 47 S 1.#define PAG
8580: 45 52 5f 53 54 41 54 5f 57 52 49 54 45 20 32 0a ER_STAT_WRITE 2.
8590: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
85a0: 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 wing global vari
85b0: 61 62 6c 65 73 20 68 6f 6c 64 20 63 6f 75 6e 74 ables hold count
85c0: 65 72 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 ers used for.**
85d0: 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 testing purposes
85e0: 20 6f 6e 6c 79 2e 20 20 54 68 65 73 65 20 76 61 only. These va
85f0: 72 69 61 62 6c 65 73 20 64 6f 20 6e 6f 74 20 65 riables do not e
8600: 78 69 73 74 20 69 6e 0a 2a 2a 20 61 20 6e 6f 6e xist in.** a non
8610: 2d 74 65 73 74 69 6e 67 20 62 75 69 6c 64 2e 20 -testing build.
8620: 20 54 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 These variables
8630: 20 61 72 65 20 6e 6f 74 20 74 68 72 65 61 64 2d are not thread-
8640: 73 61 66 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 safe..*/.#ifdef
8650: 53 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e 74 20 SQLITE_TEST.int
8660: 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72 65 sqlite3_pager_re
8670: 61 64 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 addb_count = 0;
8680: 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
8690: 66 75 6c 6c 20 70 61 67 65 73 20 72 65 61 64 20 full pages read
86a0: 66 72 6f 6d 20 44 42 20 2a 2f 0a 69 6e 74 20 73 from DB */.int s
86b0: 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 qlite3_pager_wri
86c0: 74 65 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 tedb_count = 0;
86d0: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 /* Number of f
86e0: 75 6c 6c 20 70 61 67 65 73 20 77 72 69 74 74 65 ull pages writte
86f0: 6e 20 74 6f 20 44 42 20 2a 2f 0a 69 6e 74 20 73 n to DB */.int s
8700: 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 qlite3_pager_wri
8710: 74 65 6a 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 tej_count = 0;
8720: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
8730: 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 ages written to
8740: 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 20 64 65 66 journal */.# def
8750: 69 6e 65 20 50 41 47 45 52 5f 49 4e 43 52 28 76 ine PAGER_INCR(v
8760: 29 20 20 76 2b 2b 0a 23 65 6c 73 65 0a 23 20 64 ) v++.#else.# d
8770: 65 66 69 6e 65 20 50 41 47 45 52 5f 49 4e 43 52 efine PAGER_INCR
8780: 28 76 29 0a 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a (v).#endif..../*
8790: 0a 2a 2a 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 .** Journal file
87a0: 73 20 62 65 67 69 6e 20 77 69 74 68 20 74 68 65 s begin with the
87b0: 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 67 69 63 following magic
87c0: 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 64 61 string. The da
87d0: 74 61 0a 2a 2a 20 77 61 73 20 6f 62 74 61 69 6e ta.** was obtain
87e0: 65 64 20 66 72 6f 6d 20 2f 64 65 76 2f 72 61 6e ed from /dev/ran
87f0: 64 6f 6d 2e 20 20 49 74 20 69 73 20 75 73 65 64 dom. It is used
8800: 20 6f 6e 6c 79 20 61 73 20 61 20 73 61 6e 69 74 only as a sanit
8810: 79 20 63 68 65 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 y check..**.** S
8820: 69 6e 63 65 20 76 65 72 73 69 6f 6e 20 32 2e 38 ince version 2.8
8830: 2e 30 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 .0, the journal
8840: 66 6f 72 6d 61 74 20 63 6f 6e 74 61 69 6e 73 20 format contains
8850: 61 64 64 69 74 69 6f 6e 61 6c 20 73 61 6e 69 74 additional sanit
8860: 79 0a 2a 2a 20 63 68 65 63 6b 69 6e 67 20 69 6e y.** checking in
8870: 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 49 66 20 74 formation. If t
8880: 68 65 20 70 6f 77 65 72 20 66 61 69 6c 73 20 77 he power fails w
8890: 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c hile the journal
88a0: 20 69 73 20 62 65 69 6e 67 0a 2a 2a 20 77 72 69 is being.** wri
88b0: 74 74 65 6e 2c 20 73 65 6d 69 2d 72 61 6e 64 6f tten, semi-rando
88c0: 6d 20 67 61 72 62 61 67 65 20 64 61 74 61 20 6d m garbage data m
88d0: 69 67 68 74 20 61 70 70 65 61 72 20 69 6e 20 74 ight appear in t
88e0: 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 he journal.** fi
88f0: 6c 65 20 61 66 74 65 72 20 70 6f 77 65 72 20 69 le after power i
8900: 73 20 72 65 73 74 6f 72 65 64 2e 20 20 49 66 20 s restored. If
8910: 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 74 68 an attempt is th
8920: 65 6e 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 72 6f en made.** to ro
8930: 6c 6c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 62 ll the journal b
8940: 61 63 6b 2c 20 74 68 65 20 64 61 74 61 62 61 73 ack, the databas
8950: 65 20 63 6f 75 6c 64 20 62 65 20 63 6f 72 72 75 e could be corru
8960: 70 74 65 64 2e 20 20 54 68 65 20 61 64 64 69 74 pted. The addit
8970: 69 6f 6e 61 6c 0a 2a 2a 20 73 61 6e 69 74 79 20 ional.** sanity
8980: 63 68 65 63 6b 69 6e 67 20 64 61 74 61 20 69 73 checking data is
8990: 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 64 an attempt to d
89a0: 69 73 63 6f 76 65 72 20 74 68 65 20 67 61 72 62 iscover the garb
89b0: 61 67 65 20 69 6e 20 74 68 65 0a 2a 2a 20 6a 6f age in the.** jo
89c0: 75 72 6e 61 6c 20 61 6e 64 20 69 67 6e 6f 72 65 urnal and ignore
89d0: 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 it..**.** The s
89e0: 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 69 anity checking i
89f0: 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 nformation for t
8a00: 68 65 20 6e 65 77 20 6a 6f 75 72 6e 61 6c 20 66 he new journal f
8a10: 6f 72 6d 61 74 20 63 6f 6e 73 69 73 74 73 0a 2a ormat consists.*
8a20: 2a 20 6f 66 20 61 20 33 32 2d 62 69 74 20 63 68 * of a 32-bit ch
8a30: 65 63 6b 73 75 6d 20 6f 6e 20 65 61 63 68 20 70 ecksum on each p
8a40: 61 67 65 20 6f 66 20 64 61 74 61 2e 20 20 54 68 age of data. Th
8a50: 65 20 63 68 65 63 6b 73 75 6d 20 63 6f 76 65 72 e checksum cover
8a60: 73 20 62 6f 74 68 0a 2a 2a 20 74 68 65 20 70 61 s both.** the pa
8a70: 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 ge number and th
8a80: 65 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 e pPager->pageSi
8a90: 7a 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 ze bytes of data
8aa0: 20 66 6f 72 20 74 68 65 20 70 61 67 65 2e 0a 2a for the page..*
8ab0: 2a 20 54 68 69 73 20 63 6b 73 75 6d 20 69 73 20 * This cksum is
8ac0: 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 initialized to a
8ad0: 20 33 32 2d 62 69 74 20 72 61 6e 64 6f 6d 20 76 32-bit random v
8ae0: 61 6c 75 65 20 74 68 61 74 20 61 70 70 65 61 72 alue that appear
8af0: 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 s in the.** jour
8b00: 6e 61 6c 20 66 69 6c 65 20 72 69 67 68 74 20 61 nal file right a
8b10: 66 74 65 72 20 74 68 65 20 68 65 61 64 65 72 2e fter the header.
8b20: 20 20 54 68 65 20 72 61 6e 64 6f 6d 20 69 6e 69 The random ini
8b30: 74 69 61 6c 69 7a 65 72 20 69 73 20 69 6d 70 6f tializer is impo
8b40: 72 74 61 6e 74 2c 0a 2a 2a 20 62 65 63 61 75 73 rtant,.** becaus
8b50: 65 20 67 61 72 62 61 67 65 20 64 61 74 61 20 74 e garbage data t
8b60: 68 61 74 20 61 70 70 65 61 72 73 20 61 74 20 74 hat appears at t
8b70: 68 65 20 65 6e 64 20 6f 66 20 61 20 6a 6f 75 72 he end of a jour
8b80: 6e 61 6c 20 69 73 20 6c 69 6b 65 6c 79 0a 2a 2a nal is likely.**
8b90: 20 64 61 74 61 20 74 68 61 74 20 77 61 73 20 6f data that was o
8ba0: 6e 63 65 20 69 6e 20 6f 74 68 65 72 20 66 69 6c nce in other fil
8bb0: 65 73 20 74 68 61 74 20 68 61 76 65 20 6e 6f 77 es that have now
8bc0: 20 62 65 65 6e 20 64 65 6c 65 74 65 64 2e 20 20 been deleted.
8bd0: 49 66 20 74 68 65 0a 2a 2a 20 67 61 72 62 61 67 If the.** garbag
8be0: 65 20 64 61 74 61 20 63 61 6d 65 20 66 72 6f 6d e data came from
8bf0: 20 61 6e 20 6f 62 73 6f 6c 65 74 65 20 6a 6f 75 an obsolete jou
8c00: 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 20 63 rnal file, the c
8c10: 68 65 63 6b 73 75 6d 73 20 6d 69 67 68 74 0a 2a hecksums might.*
8c20: 2a 20 62 65 20 63 6f 72 72 65 63 74 2e 20 20 42 * be correct. B
8c30: 75 74 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 ut by initializi
8c40: 6e 67 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 ng the checksum
8c50: 74 6f 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 to random value
8c60: 77 68 69 63 68 0a 2a 2a 20 69 73 20 64 69 66 66 which.** is diff
8c70: 65 72 65 6e 74 20 66 6f 72 20 65 76 65 72 79 20 erent for every
8c80: 6a 6f 75 72 6e 61 6c 2c 20 77 65 20 6d 69 6e 69 journal, we mini
8c90: 6d 69 7a 65 20 74 68 61 74 20 72 69 73 6b 2e 0a mize that risk..
8ca0: 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 */.static const
8cb0: 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4a unsigned char aJ
8cc0: 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 20 3d 20 ournalMagic[] =
8cd0: 7b 0a 20 20 30 78 64 39 2c 20 30 78 64 35 2c 20 {. 0xd9, 0xd5,
8ce0: 30 78 30 35 2c 20 30 78 66 39 2c 20 30 78 32 30 0x05, 0xf9, 0x20
8cf0: 2c 20 30 78 61 31 2c 20 30 78 36 33 2c 20 30 78 , 0xa1, 0x63, 0x
8d00: 64 37 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 d7,.};../*.** Th
8d10: 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6f 66 e size of the of
8d20: 20 65 61 63 68 20 70 61 67 65 20 72 65 63 6f 72 each page recor
8d30: 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c d in the journal
8d40: 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20 is given by.**
8d50: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 the following ma
8d60: 63 72 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 cro..*/.#define
8d70: 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 JOURNAL_PG_SZ(pP
8d80: 61 67 65 72 29 20 20 28 28 70 50 61 67 65 72 2d ager) ((pPager-
8d90: 3e 70 61 67 65 53 69 7a 65 29 20 2b 20 38 29 0a >pageSize) + 8).
8da0: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e ./*.** The journ
8db0: 61 6c 20 68 65 61 64 65 72 20 73 69 7a 65 20 66 al header size f
8dc0: 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 54 or this pager. T
8dd0: 68 69 73 20 69 73 20 75 73 75 61 6c 6c 79 20 74 his is usually t
8de0: 68 65 20 73 61 6d 65 20 0a 2a 2a 20 73 69 7a 65 he same .** size
8df0: 20 61 73 20 61 20 73 69 6e 67 6c 65 20 64 69 73 as a single dis
8e00: 6b 20 73 65 63 74 6f 72 2e 20 53 65 65 20 61 6c k sector. See al
8e10: 73 6f 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 so setSectorSize
8e20: 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a ()..*/.#define J
8e30: 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
8e40: 61 67 65 72 29 20 28 70 50 61 67 65 72 2d 3e 73 ager) (pPager->s
8e50: 65 63 74 6f 72 53 69 7a 65 29 0a 0a 2f 2a 0a 2a ectorSize)../*.*
8e60: 2a 20 54 68 65 20 6d 61 63 72 6f 20 4d 45 4d 44 * The macro MEMD
8e70: 42 20 69 73 20 74 72 75 65 20 69 66 20 77 65 20 B is true if we
8e80: 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 are dealing with
8e90: 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
8ea0: 74 61 62 61 73 65 2e 0a 2a 2a 20 57 65 20 64 6f tabase..** We do
8eb0: 20 74 68 69 73 20 61 73 20 61 20 6d 61 63 72 6f this as a macro
8ec0: 20 73 6f 20 74 68 61 74 20 69 66 20 74 68 65 20 so that if the
8ed0: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f SQLITE_OMIT_MEMO
8ee0: 52 59 44 42 20 6d 61 63 72 6f 20 69 73 20 73 65 RYDB macro is se
8ef0: 74 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 t,.** the value
8f00: 6f 66 20 4d 45 4d 44 42 20 77 69 6c 6c 20 62 65 of MEMDB will be
8f10: 20 61 20 63 6f 6e 73 74 61 6e 74 20 61 6e 64 20 a constant and
8f20: 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 77 69 6c the compiler wil
8f30: 6c 20 6f 70 74 69 6d 69 7a 65 0a 2a 2a 20 6f 75 l optimize.** ou
8f40: 74 20 63 6f 64 65 20 74 68 61 74 20 77 6f 75 6c t code that woul
8f50: 64 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 2e d never execute.
8f60: 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
8f70: 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a E_OMIT_MEMORYDB.
8f80: 23 20 64 65 66 69 6e 65 20 4d 45 4d 44 42 20 30 # define MEMDB 0
8f90: 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
8fa0: 4d 45 4d 44 42 20 70 50 61 67 65 72 2d 3e 6d 65 MEMDB pPager->me
8fb0: 6d 44 62 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a mDb.#endif../*.*
8fc0: 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 * The maximum le
8fd0: 67 61 6c 20 70 61 67 65 20 6e 75 6d 62 65 72 20 gal page number
8fe0: 69 73 20 28 32 5e 33 31 20 2d 20 31 29 2e 0a 2a is (2^31 - 1)..*
8ff0: 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f /.#define PAGER_
9000: 4d 41 58 5f 50 47 4e 4f 20 32 31 34 37 34 38 33 MAX_PGNO 2147483
9010: 36 34 37 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 647../*.** The a
9020: 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 rgument to this
9030: 6d 61 63 72 6f 20 69 73 20 61 20 66 69 6c 65 20 macro is a file
9040: 64 65 73 63 72 69 70 74 6f 72 20 28 74 79 70 65 descriptor (type
9050: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 2e sqlite3_file*).
9060: 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 69 66 20 .** Return 0 if
9070: 69 74 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 20 it is not open,
9080: 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 20 28 62 75 74 or non-zero (but
9090: 20 6e 6f 74 20 31 29 20 69 66 20 69 74 20 69 73 not 1) if it is
90a0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ..**.** This is
90b0: 73 6f 20 74 68 61 74 20 65 78 70 72 65 73 73 69 so that expressi
90c0: 6f 6e 73 20 63 61 6e 20 62 65 20 77 72 69 74 74 ons can be writt
90d0: 65 6e 20 61 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 en as:.**.** i
90e0: 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 f( isOpen(pPager
90f0: 2d 3e 6a 66 64 29 20 29 7b 20 2e 2e 2e 0a 2a 2a ->jfd) ){ ....**
9100: 0a 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a .** instead of.*
9110: 2a 0a 2a 2a 20 20 20 69 66 28 20 70 50 61 67 65 *.** if( pPage
9120: 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68 6f 64 73 r->jfd->pMethods
9130: 20 29 7b 20 2e 2e 2e 0a 2a 2f 0a 23 64 65 66 69 ){ ....*/.#defi
9140: 6e 65 20 69 73 4f 70 65 6e 28 70 46 64 29 20 28 ne isOpen(pFd) (
9150: 28 70 46 64 29 2d 3e 70 4d 65 74 68 6f 64 73 29 (pFd)->pMethods)
9160: 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
9170: 72 75 65 20 69 66 20 74 68 69 73 20 70 61 67 65 rue if this page
9180: 72 20 75 73 65 73 20 61 20 77 72 69 74 65 2d 61 r uses a write-a
9190: 68 65 61 64 20 6c 6f 67 20 69 6e 73 74 65 61 64 head log instead
91a0: 20 6f 66 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a of the usual.**
91b0: 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
91c0: 6c 2e 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c l. Otherwise fal
91d0: 73 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 se..*/.#ifndef S
91e0: 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 73 QLITE_OMIT_WAL.s
91f0: 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 55 tatic int pagerU
9200: 73 65 57 61 6c 28 50 61 67 65 72 20 2a 70 50 61 seWal(Pager *pPa
9210: 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 ger){. return (
9220: 70 50 61 67 65 72 2d 3e 70 57 61 6c 21 3d 30 29 pPager->pWal!=0)
9230: 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 ;.}.#else.# defi
9240: 6e 65 20 70 61 67 65 72 55 73 65 57 61 6c 28 78 ne pagerUseWal(x
9250: 29 20 30 0a 23 20 64 65 66 69 6e 65 20 70 61 67 ) 0.# define pag
9260: 65 72 52 6f 6c 6c 62 61 63 6b 57 61 6c 28 78 29 erRollbackWal(x)
9270: 20 30 0a 23 20 64 65 66 69 6e 65 20 70 61 67 65 0.# define page
9280: 72 57 61 6c 46 72 61 6d 65 73 28 76 2c 77 2c 78 rWalFrames(v,w,x
9290: 2c 79 29 20 30 0a 23 20 64 65 66 69 6e 65 20 70 ,y) 0.# define p
92a0: 61 67 65 72 4f 70 65 6e 57 61 6c 49 66 50 72 65 agerOpenWalIfPre
92b0: 73 65 6e 74 28 7a 29 20 53 51 4c 49 54 45 5f 4f sent(z) SQLITE_O
92c0: 4b 0a 23 20 64 65 66 69 6e 65 20 70 61 67 65 72 K.# define pager
92d0: 42 65 67 69 6e 52 65 61 64 54 72 61 6e 73 61 63 BeginReadTransac
92e0: 74 69 6f 6e 28 7a 29 20 53 51 4c 49 54 45 5f 4f tion(z) SQLITE_O
92f0: 4b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 K.#endif..#ifnde
9300: 66 20 4e 44 45 42 55 47 20 0a 2f 2a 0a 2a 2a 20 f NDEBUG ./*.**
9310: 55 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 Usage:.**.** a
9320: 73 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 ssert( assert_pa
9330: 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 ger_state(pPager
9340: 29 20 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ) );.**.** This
9350: 66 75 6e 63 74 69 6f 6e 20 72 75 6e 73 20 6d 61 function runs ma
9360: 6e 79 20 61 73 73 65 72 74 73 20 74 6f 20 74 72 ny asserts to tr
9370: 79 20 74 6f 20 66 69 6e 64 20 69 6e 63 6f 6e 73 y to find incons
9380: 69 73 74 65 6e 63 69 65 73 20 69 6e 0a 2a 2a 20 istencies in.**
9390: 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 the internal sta
93a0: 74 65 20 6f 66 20 74 68 65 20 50 61 67 65 72 20 te of the Pager
93b0: 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 object..*/.stati
93c0: 63 20 69 6e 74 20 61 73 73 65 72 74 5f 70 61 67 c int assert_pag
93d0: 65 72 5f 73 74 61 74 65 28 50 61 67 65 72 20 2a er_state(Pager *
93e0: 70 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 p){. Pager *pPa
93f0: 67 65 72 20 3d 20 70 3b 0a 0a 20 20 2f 2a 20 53 ger = p;.. /* S
9400: 74 61 74 65 20 6d 75 73 74 20 62 65 20 76 61 6c tate must be val
9410: 69 64 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 id. */. assert(
9420: 20 70 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 p->eState==PAGE
9430: 52 5f 4f 50 45 4e 0a 20 20 20 20 20 20 20 7c 7c R_OPEN. ||
9440: 20 70 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 p->eState==PAGE
9450: 52 5f 52 45 41 44 45 52 0a 20 20 20 20 20 20 20 R_READER.
9460: 7c 7c 20 70 2d 3e 65 53 74 61 74 65 3d 3d 50 41 || p->eState==PA
9470: 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45 GER_WRITER_LOCKE
9480: 44 0a 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 65 D. || p->e
9490: 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 State==PAGER_WRI
94a0: 54 45 52 5f 43 41 43 48 45 4d 4f 44 0a 20 20 20 TER_CACHEMOD.
94b0: 20 20 20 20 7c 7c 20 70 2d 3e 65 53 74 61 74 65 || p->eState
94c0: 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 44 ==PAGER_WRITER_D
94d0: 42 4d 4f 44 0a 20 20 20 20 20 20 20 7c 7c 20 70 BMOD. || p
94e0: 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f ->eState==PAGER_
94f0: 57 52 49 54 45 52 5f 46 49 4e 49 53 48 45 44 0a WRITER_FINISHED.
9500: 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 65 53 74 || p->eSt
9510: 61 74 65 3d 3d 50 41 47 45 52 5f 45 52 52 4f 52 ate==PAGER_ERROR
9520: 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 67 61 . );.. /* Rega
9530: 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 63 75 rdless of the cu
9540: 72 72 65 6e 74 20 73 74 61 74 65 2c 20 61 20 74 rrent state, a t
9550: 65 6d 70 2d 66 69 6c 65 20 63 6f 6e 6e 65 63 74 emp-file connect
9560: 69 6f 6e 20 61 6c 77 61 79 73 20 62 65 68 61 76 ion always behav
9570: 65 73 0a 20 20 2a 2a 20 61 73 20 69 66 20 69 74 es. ** as if it
9580: 20 68 61 73 20 61 6e 20 65 78 63 6c 75 73 69 76 has an exclusiv
9590: 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 e lock on the da
95a0: 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 74 20 tabase file. It
95b0: 6e 65 76 65 72 20 75 70 64 61 74 65 73 0a 20 20 never updates.
95c0: 2a 2a 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f ** the change-co
95d0: 75 6e 74 65 72 20 66 69 65 6c 64 2c 20 73 6f 20 unter field, so
95e0: 74 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 the changeCountD
95f0: 6f 6e 65 20 66 6c 61 67 20 69 73 20 61 6c 77 61 one flag is alwa
9600: 79 73 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 61 ys set.. */. a
9610: 73 73 65 72 74 28 20 70 2d 3e 74 65 6d 70 46 69 ssert( p->tempFi
9620: 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 65 4c 6f 63 le==0 || p->eLoc
9630: 6b 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 k==EXCLUSIVE_LOC
9640: 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 K );. assert( p
9650: 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c ->tempFile==0 ||
9660: 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 pPager->changeC
9670: 6f 75 6e 74 44 6f 6e 65 20 29 3b 0a 0a 20 20 2f ountDone );.. /
9680: 2a 20 49 66 20 74 68 65 20 75 73 65 4a 6f 75 72 * If the useJour
9690: 6e 61 6c 20 66 6c 61 67 20 69 73 20 63 6c 65 61 nal flag is clea
96a0: 72 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d r, the journal-m
96b0: 6f 64 65 20 6d 75 73 74 20 62 65 20 22 4f 46 46 ode must be "OFF
96c0: 22 2e 20 0a 20 20 2a 2a 20 41 6e 64 20 69 66 20 ". . ** And if
96d0: 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 the journal-mode
96e0: 20 69 73 20 22 4f 46 46 22 2c 20 74 68 65 20 6a is "OFF", the j
96f0: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 ournal file must
9700: 20 6e 6f 74 20 62 65 20 6f 70 65 6e 2e 0a 20 20 not be open..
9710: 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e */. assert( p->
9720: 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 journalMode==PAG
9730: 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f ER_JOURNALMODE_O
9740: 46 46 20 7c 7c 20 70 2d 3e 75 73 65 4a 6f 75 72 FF || p->useJour
9750: 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 nal );. assert(
9760: 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 p->journalMode!
9770: 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
9780: 44 45 5f 4f 46 46 20 7c 7c 20 21 69 73 4f 70 65 DE_OFF || !isOpe
9790: 6e 28 70 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 n(p->jfd) );..
97a0: 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 4d 45 /* Check that ME
97b0: 4d 44 42 20 69 6d 70 6c 69 65 73 20 6e 6f 53 79 MDB implies noSy
97c0: 6e 63 2e 20 41 6e 64 20 61 6e 20 69 6e 2d 6d 65 nc. And an in-me
97d0: 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 2e 20 53 69 mory journal. Si
97e0: 6e 63 65 20 0a 20 20 2a 2a 20 74 68 69 73 20 6d nce . ** this m
97f0: 65 61 6e 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 eans an in-memor
9800: 79 20 70 61 67 65 72 20 70 65 72 66 6f 72 6d 73 y pager performs
9810: 20 6e 6f 20 49 4f 20 61 74 20 61 6c 6c 2c 20 69 no IO at all, i
9820: 74 20 63 61 6e 6e 6f 74 20 65 6e 63 6f 75 6e 74 t cannot encount
9830: 65 72 20 0a 20 20 2a 2a 20 65 69 74 68 65 72 20 er . ** either
9840: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 6f 72 20 SQLITE_IOERR or
9850: 53 51 4c 49 54 45 5f 46 55 4c 4c 20 64 75 72 69 SQLITE_FULL duri
9860: 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 77 ng rollback or w
9870: 68 69 6c 65 20 66 69 6e 61 6c 69 7a 69 6e 67 20 hile finalizing
9880: 0a 20 20 2a 2a 20 61 20 6a 6f 75 72 6e 61 6c 20 . ** a journal
9890: 66 69 6c 65 2e 20 28 61 6c 74 68 6f 75 67 68 20 file. (although
98a0: 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f the in-memory jo
98b0: 75 72 6e 61 6c 20 69 6d 70 6c 65 6d 65 6e 74 61 urnal implementa
98c0: 74 69 6f 6e 20 6d 61 79 20 0a 20 20 2a 2a 20 72 tion may . ** r
98d0: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
98e0: 52 52 5f 4e 4f 4d 45 4d 20 77 68 69 6c 65 20 74 RR_NOMEM while t
98f0: 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
9900: 69 73 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e is being written
9910: 29 2e 20 49 74 20 0a 20 20 2a 2a 20 69 73 20 74 ). It . ** is t
9920: 68 65 72 65 66 6f 72 65 20 6e 6f 74 20 70 6f 73 herefore not pos
9930: 73 69 62 6c 65 20 66 6f 72 20 61 6e 20 69 6e 2d sible for an in-
9940: 6d 65 6d 6f 72 79 20 70 61 67 65 72 20 74 6f 20 memory pager to
9950: 65 6e 74 65 72 20 74 68 65 20 45 52 52 4f 52 20 enter the ERROR
9960: 0a 20 20 2a 2a 20 73 74 61 74 65 2e 0a 20 20 2a . ** state.. *
9970: 2f 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b /. if( MEMDB ){
9980: 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e . assert( p->
9990: 6e 6f 53 79 6e 63 20 29 3b 0a 20 20 20 20 61 73 noSync );. as
99a0: 73 65 72 74 28 20 70 2d 3e 6a 6f 75 72 6e 61 6c sert( p->journal
99b0: 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
99c0: 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 20 NALMODE_OFF .
99d0: 20 20 20 20 20 20 7c 7c 20 70 2d 3e 6a 6f 75 72 || p->jour
99e0: 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
99f0: 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 OURNALMODE_MEMOR
9a00: 59 20 0a 20 20 20 20 29 3b 0a 20 20 20 20 61 73 Y . );. as
9a10: 73 65 72 74 28 20 70 2d 3e 65 53 74 61 74 65 21 sert( p->eState!
9a20: 3d 50 41 47 45 52 5f 45 52 52 4f 52 20 26 26 20 =PAGER_ERROR &&
9a30: 70 2d 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52 p->eState!=PAGER
9a40: 5f 4f 50 45 4e 20 29 3b 0a 20 20 20 20 61 73 73 _OPEN );. ass
9a50: 65 72 74 28 20 70 61 67 65 72 55 73 65 57 61 6c ert( pagerUseWal
9a60: 28 70 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a 0a 20 (p)==0 );. }..
9a70: 20 2f 2a 20 49 66 20 63 68 61 6e 67 65 43 6f 75 /* If changeCou
9a80: 6e 74 44 6f 6e 65 20 69 73 20 73 65 74 2c 20 61 ntDone is set, a
9a90: 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f RESERVED lock o
9aa0: 72 20 67 72 65 61 74 65 72 20 6d 75 73 74 20 62 r greater must b
9ab0: 65 20 68 65 6c 64 0a 20 20 2a 2a 20 6f 6e 20 74 e held. ** on t
9ac0: 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 he file.. */.
9ad0: 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
9ae0: 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 3d changeCountDone=
9af0: 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 4c =0 || pPager->eL
9b00: 6f 63 6b 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f ock>=RESERVED_LO
9b10: 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 CK );. assert(
9b20: 70 2d 3e 65 4c 6f 63 6b 21 3d 50 45 4e 44 49 4e p->eLock!=PENDIN
9b30: 47 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 73 77 69 G_LOCK );.. swi
9b40: 74 63 68 28 20 70 2d 3e 65 53 74 61 74 65 20 29 tch( p->eState )
9b50: 7b 0a 20 20 20 20 63 61 73 65 20 50 41 47 45 52 {. case PAGER
9b60: 5f 4f 50 45 4e 3a 0a 20 20 20 20 20 20 61 73 73 _OPEN:. ass
9b70: 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 ert( !MEMDB );.
9b80: 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
9b90: 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 ger->errCode==SQ
9ba0: 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 LITE_OK );.
9bb0: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
9bc0: 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 PcacheRefCount(p
9bd0: 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d Pager->pPCache)=
9be0: 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 =0 || pPager->te
9bf0: 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 20 20 mpFile );.
9c00: 62 72 65 61 6b 3b 0a 0a 20 20 20 20 63 61 73 65 break;.. case
9c10: 20 50 41 47 45 52 5f 52 45 41 44 45 52 3a 0a 20 PAGER_READER:.
9c20: 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
9c30: 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 ger->errCode==SQ
9c40: 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 LITE_OK );.
9c50: 20 61 73 73 65 72 74 28 20 70 2d 3e 65 4c 6f 63 assert( p->eLoc
9c60: 6b 21 3d 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 k!=UNKNOWN_LOCK
9c70: 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
9c80: 20 70 2d 3e 65 4c 6f 63 6b 3e 3d 53 48 41 52 45 p->eLock>=SHARE
9c90: 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 D_LOCK );.
9ca0: 62 72 65 61 6b 3b 0a 0a 20 20 20 20 63 61 73 65 break;.. case
9cb0: 20 50 41 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f PAGER_WRITER_LO
9cc0: 43 4b 45 44 3a 0a 20 20 20 20 20 20 61 73 73 65 CKED:. asse
9cd0: 72 74 28 20 70 2d 3e 65 4c 6f 63 6b 21 3d 55 4e rt( p->eLock!=UN
9ce0: 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 29 3b 0a 20 20 KNOWN_LOCK );.
9cf0: 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
9d00: 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c er->errCode==SQL
9d10: 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 ITE_OK );.
9d20: 69 66 28 20 21 70 61 67 65 72 55 73 65 57 61 6c if( !pagerUseWal
9d30: 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 (pPager) ){.
9d40: 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65 assert( p->e
9d50: 4c 6f 63 6b 3e 3d 52 45 53 45 52 56 45 44 5f 4c Lock>=RESERVED_L
9d60: 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 OCK );. }.
9d70: 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
9d80: 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 70 50 61 ger->dbSize==pPa
9d90: 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 ger->dbOrigSize
9da0: 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
9db0: 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 pPager->dbOrigS
9dc0: 69 7a 65 3d 3d 70 50 61 67 65 72 2d 3e 64 62 46 ize==pPager->dbF
9dd0: 69 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 ileSize );.
9de0: 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
9df0: 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d 70 50 61 >dbOrigSize==pPa
9e00: 67 65 72 2d 3e 64 62 48 69 6e 74 53 69 7a 65 20 ger->dbHintSize
9e10: 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
9e20: 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 pPager->setMast
9e30: 65 72 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 er==0 );. b
9e40: 72 65 61 6b 3b 0a 0a 20 20 20 20 63 61 73 65 20 reak;.. case
9e50: 50 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 PAGER_WRITER_CAC
9e60: 48 45 4d 4f 44 3a 0a 20 20 20 20 20 20 61 73 73 HEMOD:. ass
9e70: 65 72 74 28 20 70 2d 3e 65 4c 6f 63 6b 21 3d 55 ert( p->eLock!=U
9e80: 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 29 3b 0a 20 NKNOWN_LOCK );.
9e90: 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
9ea0: 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 ger->errCode==SQ
9eb0: 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 LITE_OK );.
9ec0: 20 69 66 28 20 21 70 61 67 65 72 55 73 65 57 61 if( !pagerUseWa
9ed0: 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 l(pPager) ){.
9ee0: 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 70 6f /* It is po
9ef0: 73 73 69 62 6c 65 20 74 68 61 74 20 69 66 20 6a ssible that if j
9f00: 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 77 61 6c 20 ournal_mode=wal
9f10: 68 65 72 65 20 74 68 61 74 20 6e 65 69 74 68 65 here that neithe
9f20: 72 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a r the. **
9f30: 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 6f journal file no
9f40: 72 20 74 68 65 20 57 41 4c 20 66 69 6c 65 20 61 r the WAL file a
9f50: 72 65 20 6f 70 65 6e 2e 20 54 68 69 73 20 68 61 re open. This ha
9f60: 70 70 65 6e 73 20 64 75 72 69 6e 67 0a 20 20 20 ppens during.
9f70: 20 20 20 20 20 2a 2a 20 61 20 72 6f 6c 6c 62 61 ** a rollba
9f80: 63 6b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 ck transaction t
9f90: 68 61 74 20 73 77 69 74 63 68 65 73 20 66 72 6f hat switches fro
9fa0: 6d 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 6f m journal_mode=o
9fb0: 66 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f ff. ** to
9fc0: 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 77 61 journal_mode=wa
9fd0: 6c 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 l.. */.
9fe0: 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d assert( p-
9ff0: 3e 65 4c 6f 63 6b 3e 3d 52 45 53 45 52 56 45 44 >eLock>=RESERVED
a000: 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 20 _LOCK );.
a010: 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 assert( isOpen(
a020: 70 2d 3e 6a 66 64 29 20 0a 20 20 20 20 20 20 20 p->jfd) .
a030: 20 20 20 20 20 20 7c 7c 20 70 2d 3e 6a 6f 75 72 || p->jour
a040: 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
a050: 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a OURNALMODE_OFF .
a060: 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
a070: 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d p->journalMode==
a080: 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
a090: 45 5f 57 41 4c 20 0a 20 20 20 20 20 20 20 20 29 E_WAL . )
a0a0: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
a0b0: 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
a0c0: 64 62 4f 72 69 67 53 69 7a 65 3d 3d 70 50 61 67 dbOrigSize==pPag
a0d0: 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 er->dbFileSize )
a0e0: 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
a0f0: 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 pPager->dbOrigSi
a100: 7a 65 3d 3d 70 50 61 67 65 72 2d 3e 64 62 48 69 ze==pPager->dbHi
a110: 6e 74 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 ntSize );.
a120: 62 72 65 61 6b 3b 0a 0a 20 20 20 20 63 61 73 65 break;.. case
a130: 20 50 41 47 45 52 5f 57 52 49 54 45 52 5f 44 42 PAGER_WRITER_DB
a140: 4d 4f 44 3a 0a 20 20 20 20 20 20 61 73 73 65 72 MOD:. asser
a150: 74 28 20 70 2d 3e 65 4c 6f 63 6b 3d 3d 45 58 43 t( p->eLock==EXC
a160: 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 20 LUSIVE_LOCK );.
a170: 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
a180: 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 ger->errCode==SQ
a190: 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 LITE_OK );.
a1a0: 20 61 73 73 65 72 74 28 20 21 70 61 67 65 72 55 assert( !pagerU
a1b0: 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 3b seWal(pPager) );
a1c0: 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
a1d0: 2d 3e 65 4c 6f 63 6b 3e 3d 45 58 43 4c 55 53 49 ->eLock>=EXCLUSI
a1e0: 56 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 VE_LOCK );.
a1f0: 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 assert( isOpen(
a200: 70 2d 3e 6a 66 64 29 20 0a 20 20 20 20 20 20 20 p->jfd) .
a210: 20 20 20 20 7c 7c 20 70 2d 3e 6a 6f 75 72 6e 61 || p->journa
a220: 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
a230: 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 RNALMODE_OFF .
a240: 20 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 6a || p->j
a250: 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 ournalMode==PAGE
a260: 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 R_JOURNALMODE_WA
a270: 4c 20 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 L . );.
a280: 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
a290: 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3c 3d 70 50 ->dbOrigSize<=pP
a2a0: 61 67 65 72 2d 3e 64 62 48 69 6e 74 53 69 7a 65 ager->dbHintSize
a2b0: 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b );. break;
a2c0: 0a 0a 20 20 20 20 63 61 73 65 20 50 41 47 45 52 .. case PAGER
a2d0: 5f 57 52 49 54 45 52 5f 46 49 4e 49 53 48 45 44 _WRITER_FINISHED
a2e0: 3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 :. assert(
a2f0: 70 2d 3e 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 p->eLock==EXCLUS
a300: 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 IVE_LOCK );.
a310: 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
a320: 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 ->errCode==SQLIT
a330: 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 61 73 E_OK );. as
a340: 73 65 72 74 28 20 21 70 61 67 65 72 55 73 65 57 sert( !pagerUseW
a350: 61 6c 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 al(pPager) );.
a360: 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 assert( isOp
a370: 65 6e 28 70 2d 3e 6a 66 64 29 20 0a 20 20 20 20 en(p->jfd) .
a380: 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 6a 6f 75 || p->jou
a390: 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f rnalMode==PAGER_
a3a0: 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 JOURNALMODE_OFF
a3b0: 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 . || p
a3c0: 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
a3d0: 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
a3e0: 5f 57 41 4c 20 0a 20 20 20 20 20 20 29 3b 0a 20 _WAL . );.
a3f0: 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 break;..
a400: 20 63 61 73 65 20 50 41 47 45 52 5f 45 52 52 4f case PAGER_ERRO
a410: 52 3a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 72 R:. /* Ther
a420: 65 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 e must be at lea
a430: 73 74 20 6f 6e 65 20 6f 75 74 73 74 61 6e 64 69 st one outstandi
a440: 6e 67 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 ng reference to
a450: 74 68 65 20 70 61 67 65 72 20 69 66 0a 20 20 20 the pager if.
a460: 20 20 20 2a 2a 20 69 6e 20 45 52 52 4f 52 20 73 ** in ERROR s
a470: 74 61 74 65 2e 20 4f 74 68 65 72 77 69 73 65 20 tate. Otherwise
a480: 74 68 65 20 70 61 67 65 72 20 73 68 6f 75 6c 64 the pager should
a490: 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 64 72 have already dr
a4a0: 6f 70 70 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 opped. ** b
a4b0: 61 63 6b 20 74 6f 20 4f 50 45 4e 20 73 74 61 74 ack to OPEN stat
a4c0: 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 e.. */.
a4d0: 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
a4e0: 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 ->errCode!=SQLIT
a4f0: 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 61 73 E_OK );. as
a500: 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 63 61 sert( sqlite3Pca
a510: 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 cheRefCount(pPag
a520: 65 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20 29 er->pPCache)>0 )
a530: 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
a540: 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a }.. return 1;.
a550: 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e 64 }.#endif /* ifnd
a560: 65 66 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 23 69 ef NDEBUG */..#i
a570: 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
a580: 47 20 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 G ./*.** Return
a590: 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 68 a pointer to a h
a5a0: 75 6d 61 6e 20 72 65 61 64 61 62 6c 65 20 73 74 uman readable st
a5b0: 72 69 6e 67 20 69 6e 20 61 20 73 74 61 74 69 63 ring in a static
a5c0: 20 62 75 66 66 65 72 0a 2a 2a 20 63 6f 6e 74 61 buffer.** conta
a5d0: 69 6e 69 6e 67 20 74 68 65 20 73 74 61 74 65 20 ining the state
a5e0: 6f 66 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a of the Pager obj
a5f0: 65 63 74 20 70 61 73 73 65 64 20 61 73 20 61 6e ect passed as an
a600: 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 0a argument. This.
a610: 2a 2a 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 ** is intended t
a620: 6f 20 62 65 20 75 73 65 64 20 77 69 74 68 69 6e o be used within
a630: 20 64 65 62 75 67 67 65 72 73 2e 20 46 6f 72 20 debuggers. For
a640: 65 78 61 6d 70 6c 65 2c 20 61 73 20 61 6e 20 61 example, as an a
a650: 6c 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 74 6f lternative.** to
a660: 20 22 70 72 69 6e 74 20 2a 70 50 61 67 65 72 22 "print *pPager"
a670: 20 69 6e 20 67 64 62 3a 0a 2a 2a 0a 2a 2a 20 28 in gdb:.**.** (
a680: 67 64 62 29 20 70 72 69 6e 74 66 20 22 25 73 22 gdb) printf "%s"
a690: 2c 20 70 72 69 6e 74 5f 70 61 67 65 72 5f 73 74 , print_pager_st
a6a0: 61 74 65 28 70 50 61 67 65 72 29 0a 2a 2f 0a 73 ate(pPager).*/.s
a6b0: 74 61 74 69 63 20 63 68 61 72 20 2a 70 72 69 6e tatic char *prin
a6c0: 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 50 61 t_pager_state(Pa
a6d0: 67 65 72 20 2a 70 29 7b 0a 20 20 73 74 61 74 69 ger *p){. stati
a6e0: 63 20 63 68 61 72 20 7a 52 65 74 5b 31 30 32 34 c char zRet[1024
a6f0: 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e ];.. sqlite3_sn
a700: 70 72 69 6e 74 66 28 31 30 32 34 2c 20 7a 52 65 printf(1024, zRe
a710: 74 2c 0a 20 20 20 20 20 20 22 46 69 6c 65 6e 61 t,. "Filena
a720: 6d 65 3a 20 20 20 20 20 20 25 73 5c 6e 22 0a 20 me: %s\n".
a730: 20 20 20 20 20 22 53 74 61 74 65 3a 20 20 20 20 "State:
a740: 20 20 20 20 20 25 73 20 65 72 72 43 6f 64 65 3d %s errCode=
a750: 25 64 5c 6e 22 0a 20 20 20 20 20 20 22 4c 6f 63 %d\n". "Loc
a760: 6b 3a 20 20 20 20 20 20 20 20 20 20 25 73 5c 6e k: %s\n
a770: 22 0a 20 20 20 20 20 20 22 4c 6f 63 6b 69 6e 67 ". "Locking
a780: 20 6d 6f 64 65 3a 20 20 6c 6f 63 6b 69 6e 67 5f mode: locking_
a790: 6d 6f 64 65 3d 25 73 5c 6e 22 0a 20 20 20 20 20 mode=%s\n".
a7a0: 20 22 4a 6f 75 72 6e 61 6c 20 6d 6f 64 65 3a 20 "Journal mode:
a7b0: 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 25 73 journal_mode=%s
a7c0: 5c 6e 22 0a 20 20 20 20 20 20 22 42 61 63 6b 69 \n". "Backi
a7d0: 6e 67 20 73 74 6f 72 65 3a 20 74 65 6d 70 46 69 ng store: tempFi
a7e0: 6c 65 3d 25 64 20 6d 65 6d 44 62 3d 25 64 20 75 le=%d memDb=%d u
a7f0: 73 65 4a 6f 75 72 6e 61 6c 3d 25 64 5c 6e 22 0a seJournal=%d\n".
a800: 20 20 20 20 20 20 22 4a 6f 75 72 6e 61 6c 3a 20 "Journal:
a810: 20 20 20 20 20 20 6a 6f 75 72 6e 61 6c 4f 66 66 journalOff
a820: 3d 25 6c 6c 64 20 6a 6f 75 72 6e 61 6c 48 64 72 =%lld journalHdr
a830: 3d 25 6c 6c 64 5c 6e 22 0a 20 20 20 20 20 20 22 =%lld\n". "
a840: 53 69 7a 65 3a 20 20 20 20 20 20 20 20 20 20 64 Size: d
a850: 62 73 69 7a 65 3d 25 64 20 64 62 4f 72 69 67 53 bsize=%d dbOrigS
a860: 69 7a 65 3d 25 64 20 64 62 46 69 6c 65 53 69 7a ize=%d dbFileSiz
a870: 65 3d 25 64 5c 6e 22 0a 20 20 20 20 20 20 2c 20 e=%d\n". ,
a880: 70 2d 3e 7a 46 69 6c 65 6e 61 6d 65 0a 20 20 20 p->zFilename.
a890: 20 20 20 2c 20 70 2d 3e 65 53 74 61 74 65 3d 3d , p->eState==
a8a0: 50 41 47 45 52 5f 4f 50 45 4e 20 20 20 20 20 20 PAGER_OPEN
a8b0: 20 20 20 20 20 20 3f 20 22 4f 50 45 4e 22 20 3a ? "OPEN" :
a8c0: 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 53 74 61 . p->eSta
a8d0: 74 65 3d 3d 50 41 47 45 52 5f 52 45 41 44 45 52 te==PAGER_READER
a8e0: 20 20 20 20 20 20 20 20 20 20 3f 20 22 52 45 41 ? "REA
a8f0: 44 45 52 22 20 3a 0a 20 20 20 20 20 20 20 20 70 DER" :. p
a900: 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f ->eState==PAGER_
a910: 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 20 20 20 WRITER_LOCKED
a920: 3f 20 22 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 ? "WRITER_LOCKED
a930: 22 20 3a 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 " :. p->e
a940: 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 State==PAGER_WRI
a950: 54 45 52 5f 43 41 43 48 45 4d 4f 44 20 3f 20 22 TER_CACHEMOD ? "
a960: 57 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 22 WRITER_CACHEMOD"
a970: 20 3a 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 53 :. p->eS
a980: 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 tate==PAGER_WRIT
a990: 45 52 5f 44 42 4d 4f 44 20 20 20 20 3f 20 22 57 ER_DBMOD ? "W
a9a0: 52 49 54 45 52 5f 44 42 4d 4f 44 22 20 3a 0a 20 RITER_DBMOD" :.
a9b0: 20 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65 p->eState
a9c0: 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 46 ==PAGER_WRITER_F
a9d0: 49 4e 49 53 48 45 44 20 3f 20 22 57 52 49 54 45 INISHED ? "WRITE
a9e0: 52 5f 46 49 4e 49 53 48 45 44 22 20 3a 0a 20 20 R_FINISHED" :.
a9f0: 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65 3d p->eState=
aa00: 3d 50 41 47 45 52 5f 45 52 52 4f 52 20 20 20 20 =PAGER_ERROR
aa10: 20 20 20 20 20 20 20 3f 20 22 45 52 52 4f 52 22 ? "ERROR"
aa20: 20 3a 20 22 3f 65 72 72 6f 72 3f 22 0a 20 20 20 : "?error?".
aa30: 20 20 20 2c 20 28 69 6e 74 29 70 2d 3e 65 72 72 , (int)p->err
aa40: 43 6f 64 65 0a 20 20 20 20 20 20 2c 20 70 2d 3e Code. , p->
aa50: 65 4c 6f 63 6b 3d 3d 4e 4f 5f 4c 4f 43 4b 20 20 eLock==NO_LOCK
aa60: 20 20 20 20 20 20 20 3f 20 22 4e 4f 5f 4c 4f 43 ? "NO_LOC
aa70: 4b 22 20 3a 0a 20 20 20 20 20 20 20 20 70 2d 3e K" :. p->
aa80: 65 4c 6f 63 6b 3d 3d 52 45 53 45 52 56 45 44 5f eLock==RESERVED_
aa90: 4c 4f 43 4b 20 20 20 3f 20 22 52 45 53 45 52 56 LOCK ? "RESERV
aaa0: 45 44 22 20 3a 0a 20 20 20 20 20 20 20 20 70 2d ED" :. p-
aab0: 3e 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 >eLock==EXCLUSIV
aac0: 45 5f 4c 4f 43 4b 20 20 3f 20 22 45 58 43 4c 55 E_LOCK ? "EXCLU
aad0: 53 49 56 45 22 20 3a 0a 20 20 20 20 20 20 20 20 SIVE" :.
aae0: 70 2d 3e 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 p->eLock==SHARED
aaf0: 5f 4c 4f 43 4b 20 20 20 20 20 3f 20 22 53 48 41 _LOCK ? "SHA
ab00: 52 45 44 22 20 3a 0a 20 20 20 20 20 20 20 20 70 RED" :. p
ab10: 2d 3e 65 4c 6f 63 6b 3d 3d 55 4e 4b 4e 4f 57 4e ->eLock==UNKNOWN
ab20: 5f 4c 4f 43 4b 20 20 20 20 3f 20 22 55 4e 4b 4e _LOCK ? "UNKN
ab30: 4f 57 4e 22 20 3a 20 22 3f 65 72 72 6f 72 3f 22 OWN" : "?error?"
ab40: 0a 20 20 20 20 20 20 2c 20 70 2d 3e 65 78 63 6c . , p->excl
ab50: 75 73 69 76 65 4d 6f 64 65 20 3f 20 22 65 78 63 usiveMode ? "exc
ab60: 6c 75 73 69 76 65 22 20 3a 20 22 6e 6f 72 6d 61 lusive" : "norma
ab70: 6c 22 0a 20 20 20 20 20 20 2c 20 70 2d 3e 6a 6f l". , p->jo
ab80: 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 urnalMode==PAGER
ab90: 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d _JOURNALMODE_MEM
aba0: 4f 52 59 20 20 20 3f 20 22 6d 65 6d 6f 72 79 22 ORY ? "memory"
abb0: 20 3a 0a 20 20 20 20 20 20 20 20 70 2d 3e 6a 6f :. p->jo
abc0: 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 urnalMode==PAGER
abd0: 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 _JOURNALMODE_OFF
abe0: 20 20 20 20 20 20 3f 20 22 6f 66 66 22 20 3a 0a ? "off" :.
abf0: 20 20 20 20 20 20 20 20 70 2d 3e 6a 6f 75 72 6e p->journ
ac00: 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
ac10: 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 URNALMODE_DELETE
ac20: 20 20 20 3f 20 22 64 65 6c 65 74 65 22 20 3a 0a ? "delete" :.
ac30: 20 20 20 20 20 20 20 20 70 2d 3e 6a 6f 75 72 6e p->journ
ac40: 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
ac50: 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 URNALMODE_PERSIS
ac60: 54 20 20 3f 20 22 70 65 72 73 69 73 74 22 20 3a T ? "persist" :
ac70: 0a 20 20 20 20 20 20 20 20 70 2d 3e 6a 6f 75 72 . p->jour
ac80: 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
ac90: 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 OURNALMODE_TRUNC
aca0: 41 54 45 20 3f 20 22 74 72 75 6e 63 61 74 65 22 ATE ? "truncate"
acb0: 20 3a 0a 20 20 20 20 20 20 20 20 70 2d 3e 6a 6f :. p->jo
acc0: 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 urnalMode==PAGER
acd0: 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c _JOURNALMODE_WAL
ace0: 20 20 20 20 20 20 3f 20 22 77 61 6c 22 20 3a 20 ? "wal" :
acf0: 22 3f 65 72 72 6f 72 3f 22 0a 20 20 20 20 20 20 "?error?".
ad00: 2c 20 28 69 6e 74 29 70 2d 3e 74 65 6d 70 46 69 , (int)p->tempFi
ad10: 6c 65 2c 20 28 69 6e 74 29 70 2d 3e 6d 65 6d 44 le, (int)p->memD
ad20: 62 2c 20 28 69 6e 74 29 70 2d 3e 75 73 65 4a 6f b, (int)p->useJo
ad30: 75 72 6e 61 6c 0a 20 20 20 20 20 20 2c 20 70 2d urnal. , p-
ad40: 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 2d 3e >journalOff, p->
ad50: 6a 6f 75 72 6e 61 6c 48 64 72 0a 20 20 20 20 20 journalHdr.
ad60: 20 2c 20 28 69 6e 74 29 70 2d 3e 64 62 53 69 7a , (int)p->dbSiz
ad70: 65 2c 20 28 69 6e 74 29 70 2d 3e 64 62 4f 72 69 e, (int)p->dbOri
ad80: 67 53 69 7a 65 2c 20 28 69 6e 74 29 70 2d 3e 64 gSize, (int)p->d
ad90: 62 46 69 6c 65 53 69 7a 65 0a 20 20 29 3b 0a 0a bFileSize. );..
ada0: 20 20 72 65 74 75 72 6e 20 7a 52 65 74 3b 0a 7d return zRet;.}
adb0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
adc0: 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 69 74 eturn true if it
add0: 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f is necessary to
ade0: 20 77 72 69 74 65 20 70 61 67 65 20 2a 70 50 67 write page *pPg
adf0: 20 69 6e 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f into the sub-jo
ae00: 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20 70 61 67 65 urnal..** A page
ae10: 20 6e 65 65 64 73 20 74 6f 20 62 65 20 77 72 69 needs to be wri
ae20: 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 73 75 tten into the su
ae30: 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 74 68 65 b-journal if the
ae40: 72 65 20 65 78 69 73 74 73 20 6f 6e 65 0a 2a 2a re exists one.**
ae50: 20 6f 72 20 6d 6f 72 65 20 6f 70 65 6e 20 73 61 or more open sa
ae60: 76 65 70 6f 69 6e 74 73 20 66 6f 72 20 77 68 69 vepoints for whi
ae70: 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 ch:.**.** * Th
ae80: 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 e page-number is
ae90: 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 less than or eq
aea0: 75 61 6c 20 74 6f 20 50 61 67 65 72 53 61 76 65 ual to PagerSave
aeb0: 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c 20 61 6e 64 point.nOrig, and
aec0: 0a 2a 2a 20 20 20 2a 20 54 68 65 20 62 69 74 20 .** * The bit
aed0: 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f corresponding to
aee0: 20 74 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 the page-number
aef0: 20 69 73 20 6e 6f 74 20 73 65 74 20 69 6e 0a 2a is not set in.*
af00: 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65 70 * PagerSavep
af10: 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e oint.pInSavepoin
af20: 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
af30: 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 subjRequiresPag
af40: 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 e(PgHdr *pPg){.
af50: 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 67 Pgno pgno = pPg
af60: 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 ->pgno;. Pager
af70: 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
af80: 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 69 3b 0a Pager;. int i;.
af90: 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 for(i=0; i<pPa
afa0: 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b ger->nSavepoint;
afb0: 20 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 72 i++){. Pager
afc0: 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 26 Savepoint *p = &
afd0: 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 pPager->aSavepoi
afe0: 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 nt[i];. if( p
aff0: 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e 6f 20 26 26 ->nOrig>=pgno &&
b000: 20 30 3d 3d 73 71 6c 69 74 65 33 42 69 74 76 65 0==sqlite3Bitve
b010: 63 54 65 73 74 28 70 2d 3e 70 49 6e 53 61 76 65 cTest(p->pInSave
b020: 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 20 29 7b 0a point, pgno) ){.
b030: 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
b040: 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
b050: 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 rn 0;.}../*.** R
b060: 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 eturn true if th
b070: 65 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 e page is alread
b080: 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c y in the journal
b090: 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
b0a0: 20 69 6e 74 20 70 61 67 65 49 6e 4a 6f 75 72 6e int pageInJourn
b0b0: 61 6c 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a al(PgHdr *pPg){.
b0c0: 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
b0d0: 42 69 74 76 65 63 54 65 73 74 28 70 50 67 2d 3e BitvecTest(pPg->
b0e0: 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
b0f0: 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a al, pPg->pgno);.
b100: 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 }../*.** Read a
b110: 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 66 32-bit integer f
b120: 72 6f 6d 20 74 68 65 20 67 69 76 65 6e 20 66 69 rom the given fi
b130: 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 le descriptor.
b140: 53 74 6f 72 65 20 74 68 65 20 69 6e 74 65 67 65 Store the intege
b150: 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 72 65 61 r.** that is rea
b160: 64 20 69 6e 20 2a 70 52 65 73 2e 20 20 52 65 74 d in *pRes. Ret
b170: 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 urn SQLITE_OK if
b180: 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b everything work
b190: 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a 20 65 72 72 ed, or an.** err
b1a0: 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d 65 74 or code is somet
b1b0: 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e hing goes wrong.
b1c0: 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 61 6c 75 65 .**.** All value
b1d0: 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 s are stored on
b1e0: 64 69 73 6b 20 61 73 20 62 69 67 2d 65 6e 64 69 disk as big-endi
b1f0: 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e an..*/.static in
b200: 74 20 72 65 61 64 33 32 62 69 74 73 28 73 71 6c t read32bits(sql
b210: 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 69 ite3_file *fd, i
b220: 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 2a 64 offset, u32 *
b230: 70 52 65 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65 pRes){. unsigne
b240: 64 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20 d char ac[4];.
b250: 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 int rc = sqlite3
b260: 4f 73 52 65 61 64 28 66 64 2c 20 61 63 2c 20 73 OsRead(fd, ac, s
b270: 69 7a 65 6f 66 28 61 63 29 2c 20 6f 66 66 73 65 izeof(ac), offse
b280: 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 t);. if( rc==SQ
b290: 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a LITE_OK ){. *
b2a0: 70 52 65 73 20 3d 20 73 71 6c 69 74 65 33 47 65 pRes = sqlite3Ge
b2b0: 74 34 62 79 74 65 28 61 63 29 3b 0a 20 20 7d 0a t4byte(ac);. }.
b2c0: 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
b2d0: 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 33 32 /*.** Write a 32
b2e0: 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 6e 74 -bit integer int
b2f0: 6f 20 61 20 73 74 72 69 6e 67 20 62 75 66 66 65 o a string buffe
b300: 72 20 69 6e 20 62 69 67 2d 65 6e 64 69 61 6e 20 r in big-endian
b310: 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 23 byte order..*/.#
b320: 64 65 66 69 6e 65 20 70 75 74 33 32 62 69 74 73 define put32bits
b330: 28 41 2c 42 29 20 20 73 71 6c 69 74 65 33 50 75 (A,B) sqlite3Pu
b340: 74 34 62 79 74 65 28 28 75 38 2a 29 41 2c 42 29 t4byte((u8*)A,B)
b350: 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 .../*.** Write a
b360: 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 32-bit integer
b370: 69 6e 74 6f 20 74 68 65 20 67 69 76 65 6e 20 66 into the given f
b380: 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 ile descriptor.
b390: 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
b3a0: 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 K.** on success
b3b0: 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 or an error code
b3c0: 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f is something go
b3d0: 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 es wrong..*/.sta
b3e0: 74 69 63 20 69 6e 74 20 77 72 69 74 65 33 32 62 tic int write32b
b3f0: 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 its(sqlite3_file
b400: 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 65 74 *fd, i64 offset
b410: 2c 20 75 33 32 20 76 61 6c 29 7b 0a 20 20 63 68 , u32 val){. ch
b420: 61 72 20 61 63 5b 34 5d 3b 0a 20 20 70 75 74 33 ar ac[4];. put3
b430: 32 62 69 74 73 28 61 63 2c 20 76 61 6c 29 3b 0a 2bits(ac, val);.
b440: 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
b450: 4f 73 57 72 69 74 65 28 66 64 2c 20 61 63 2c 20 OsWrite(fd, ac,
b460: 34 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 0a 2f 4, offset);.}../
b470: 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 74 68 65 20 *.** Unlock the
b480: 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f database file to
b490: 20 6c 65 76 65 6c 20 65 4c 6f 63 6b 2c 20 77 68 level eLock, wh
b4a0: 69 63 68 20 6d 75 73 74 20 62 65 20 65 69 74 68 ich must be eith
b4b0: 65 72 20 4e 4f 5f 4c 4f 43 4b 0a 2a 2a 20 6f 72 er NO_LOCK.** or
b4c0: 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 20 52 65 SHARED_LOCK. Re
b4d0: 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 gardless of whet
b4e0: 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 63 her or not the c
b4f0: 61 6c 6c 20 74 6f 20 78 55 6e 6c 6f 63 6b 28 29 all to xUnlock()
b500: 0a 2a 2a 20 73 75 63 63 65 65 64 73 2c 20 73 65 .** succeeds, se
b510: 74 20 74 68 65 20 50 61 67 65 72 2e 65 4c 6f 63 t the Pager.eLoc
b520: 6b 20 76 61 72 69 61 62 6c 65 20 74 6f 20 6d 61 k variable to ma
b530: 74 63 68 20 74 68 65 20 28 61 74 74 65 6d 70 74 tch the (attempt
b540: 65 64 29 20 6e 65 77 20 6c 6f 63 6b 2e 0a 2a 2a ed) new lock..**
b550: 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20 50 .** Except, if P
b560: 61 67 65 72 2e 65 4c 6f 63 6b 20 69 73 20 73 65 ager.eLock is se
b570: 74 20 74 6f 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 t to UNKNOWN_LOC
b580: 4b 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 K when this func
b590: 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 tion is.** calle
b5a0: 64 2c 20 64 6f 20 6e 6f 74 20 6d 6f 64 69 66 79 d, do not modify
b5b0: 20 69 74 2e 20 53 65 65 20 74 68 65 20 63 6f 6d it. See the com
b5c0: 6d 65 6e 74 20 61 62 6f 76 65 20 74 68 65 20 23 ment above the #
b5d0: 64 65 66 69 6e 65 20 6f 66 20 0a 2a 2a 20 55 4e define of .** UN
b5e0: 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 66 6f 72 20 61 KNOWN_LOCK for a
b5f0: 6e 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 6f 66 n explanation of
b600: 20 74 68 69 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 this..*/.static
b610: 20 69 6e 74 20 70 61 67 65 72 55 6e 6c 6f 63 6b int pagerUnlock
b620: 44 62 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 Db(Pager *pPager
b630: 2c 20 69 6e 74 20 65 4c 6f 63 6b 29 7b 0a 20 20 , int eLock){.
b640: 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
b650: 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 21 OK;.. assert( !
b660: 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
b670: 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 72 2d eMode || pPager-
b680: 3e 65 4c 6f 63 6b 3d 3d 65 4c 6f 63 6b 20 29 3b >eLock==eLock );
b690: 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b . assert( eLock
b6a0: 3d 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f ==NO_LOCK || eLo
b6b0: 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ck==SHARED_LOCK
b6c0: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f );. assert( eLo
b6d0: 63 6b 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 70 ck!=NO_LOCK || p
b6e0: 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 agerUseWal(pPage
b6f0: 72 29 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 r)==0 );. if( i
b700: 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 sOpen(pPager->fd
b710: 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 ) ){. assert(
b720: 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3e 3d pPager->eLock>=
b730: 65 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 72 63 20 eLock );. rc
b740: 3d 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 = sqlite3OsUnloc
b750: 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 65 4c k(pPager->fd, eL
b760: 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 70 50 ock);. if( pP
b770: 61 67 65 72 2d 3e 65 4c 6f 63 6b 21 3d 55 4e 4b ager->eLock!=UNK
b780: 4e 4f 57 4e 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 NOWN_LOCK ){.
b790: 20 20 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b pPager->eLock
b7a0: 20 3d 20 28 75 38 29 65 4c 6f 63 6b 3b 0a 20 20 = (u8)eLock;.
b7b0: 20 20 7d 0a 20 20 20 20 49 4f 54 52 41 43 45 28 }. IOTRACE(
b7c0: 28 22 55 4e 4c 4f 43 4b 20 25 70 20 25 64 5c 6e ("UNLOCK %p %d\n
b7d0: 22 2c 20 70 50 61 67 65 72 2c 20 65 4c 6f 63 6b ", pPager, eLock
b7e0: 29 29 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 )). }. return
b7f0: 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 rc;.}../*.** Loc
b800: 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 k the database f
b810: 69 6c 65 20 74 6f 20 6c 65 76 65 6c 20 65 4c 6f ile to level eLo
b820: 63 6b 2c 20 77 68 69 63 68 20 6d 75 73 74 20 62 ck, which must b
b830: 65 20 65 69 74 68 65 72 20 53 48 41 52 45 44 5f e either SHARED_
b840: 4c 4f 43 4b 2c 0a 2a 2a 20 52 45 53 45 52 56 45 LOCK,.** RESERVE
b850: 44 5f 4c 4f 43 4b 20 6f 72 20 45 58 43 4c 55 53 D_LOCK or EXCLUS
b860: 49 56 45 5f 4c 4f 43 4b 2e 20 49 66 20 74 68 65 IVE_LOCK. If the
b870: 20 63 61 6c 6c 65 72 20 69 73 20 73 75 63 63 65 caller is succe
b880: 73 73 66 75 6c 2c 20 73 65 74 20 74 68 65 0a 2a ssful, set the.*
b890: 2a 20 50 61 67 65 72 2e 65 4c 6f 63 6b 20 76 61 * Pager.eLock va
b8a0: 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20 6e 65 riable to the ne
b8b0: 77 20 6c 6f 63 6b 69 6e 67 20 73 74 61 74 65 2e w locking state.
b8c0: 20 0a 2a 2a 0a 2a 2a 20 45 78 63 65 70 74 2c 20 .**.** Except,
b8d0: 69 66 20 50 61 67 65 72 2e 65 4c 6f 63 6b 20 69 if Pager.eLock i
b8e0: 73 20 73 65 74 20 74 6f 20 55 4e 4b 4e 4f 57 4e s set to UNKNOWN
b8f0: 5f 4c 4f 43 4b 20 77 68 65 6e 20 74 68 69 73 20 _LOCK when this
b900: 66 75 6e 63 74 69 6f 6e 20 69 73 20 0a 2a 2a 20 function is .**
b910: 63 61 6c 6c 65 64 2c 20 64 6f 20 6e 6f 74 20 6d called, do not m
b920: 6f 64 69 66 79 20 69 74 20 75 6e 6c 65 73 73 20 odify it unless
b930: 74 68 65 20 6e 65 77 20 6c 6f 63 6b 69 6e 67 20 the new locking
b940: 73 74 61 74 65 20 69 73 20 45 58 43 4c 55 53 49 state is EXCLUSI
b950: 56 45 5f 4c 4f 43 4b 2e 20 0a 2a 2a 20 53 65 65 VE_LOCK. .** See
b960: 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f the comment abo
b970: 76 65 20 74 68 65 20 23 64 65 66 69 6e 65 20 6f ve the #define o
b980: 66 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 66 f UNKNOWN_LOCK f
b990: 6f 72 20 61 6e 20 65 78 70 6c 61 6e 61 74 69 6f or an explanatio
b9a0: 6e 20 0a 2a 2a 20 6f 66 20 74 68 69 73 2e 0a 2a n .** of this..*
b9b0: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
b9c0: 65 72 4c 6f 63 6b 44 62 28 50 61 67 65 72 20 2a erLockDb(Pager *
b9d0: 70 50 61 67 65 72 2c 20 69 6e 74 20 65 4c 6f 63 pPager, int eLoc
b9e0: 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 k){. int rc = S
b9f0: 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 QLITE_OK;.. ass
ba00: 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 53 48 41 52 ert( eLock==SHAR
ba10: 45 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b ED_LOCK || eLock
ba20: 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 ==RESERVED_LOCK
ba30: 7c 7c 20 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 || eLock==EXCLUS
ba40: 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 IVE_LOCK );. if
ba50: 28 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3c ( pPager->eLock<
ba60: 65 4c 6f 63 6b 20 7c 7c 20 70 50 61 67 65 72 2d eLock || pPager-
ba70: 3e 65 4c 6f 63 6b 3d 3d 55 4e 4b 4e 4f 57 4e 5f >eLock==UNKNOWN_
ba80: 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d LOCK ){. rc =
ba90: 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 sqlite3OsLock(p
baa0: 50 61 67 65 72 2d 3e 66 64 2c 20 65 4c 6f 63 6b Pager->fd, eLock
bab0: 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
bac0: 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 70 50 61 QLITE_OK && (pPa
bad0: 67 65 72 2d 3e 65 4c 6f 63 6b 21 3d 55 4e 4b 4e ger->eLock!=UNKN
bae0: 4f 57 4e 5f 4c 4f 43 4b 7c 7c 65 4c 6f 63 6b 3d OWN_LOCK||eLock=
baf0: 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 =EXCLUSIVE_LOCK)
bb00: 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 ){. pPager
bb10: 2d 3e 65 4c 6f 63 6b 20 3d 20 28 75 38 29 65 4c ->eLock = (u8)eL
bb20: 6f 63 6b 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 ock;. IOTRA
bb30: 43 45 28 28 22 4c 4f 43 4b 20 25 70 20 25 64 5c CE(("LOCK %p %d\
bb40: 6e 22 2c 20 70 50 61 67 65 72 2c 20 65 4c 6f 63 n", pPager, eLoc
bb50: 6b 29 29 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 k)). }. }.
bb60: 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
bb70: 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
bb80: 6e 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 n determines whe
bb90: 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 ther or not the
bba0: 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 atomic-write opt
bbb0: 69 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6e imization.** can
bbc0: 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74 68 be used with th
bbd0: 69 73 20 70 61 67 65 72 2e 20 54 68 65 20 6f 70 is pager. The op
bbe0: 74 69 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 20 62 timization can b
bbf0: 65 20 75 73 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a e used if:.**.**
bc00: 20 20 28 61 29 20 74 68 65 20 76 61 6c 75 65 20 (a) the value
bc10: 72 65 74 75 72 6e 65 64 20 62 79 20 4f 73 44 65 returned by OsDe
bc20: 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
bc30: 69 63 73 28 29 20 69 6e 64 69 63 61 74 65 73 20 ics() indicates
bc40: 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 61 20 64 that.** a d
bc50: 61 74 61 62 61 73 65 20 70 61 67 65 20 6d 61 79 atabase page may
bc60: 20 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d be written atom
bc70: 69 63 61 6c 6c 79 2c 20 61 6e 64 0a 2a 2a 20 20 ically, and.**
bc80: 28 62 29 20 74 68 65 20 76 61 6c 75 65 20 72 65 (b) the value re
bc90: 74 75 72 6e 65 64 20 62 79 20 4f 73 53 65 63 74 turned by OsSect
bca0: 6f 72 53 69 7a 65 28 29 20 69 73 20 6c 65 73 73 orSize() is less
bcb0: 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a than or equal.*
bcc0: 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20 70 61 * to the pa
bcd0: 67 65 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 ge size..**.** T
bce0: 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 he optimization
bcf0: 69 73 20 61 6c 73 6f 20 61 6c 77 61 79 73 20 65 is also always e
bd00: 6e 61 62 6c 65 64 20 66 6f 72 20 74 65 6d 70 6f nabled for tempo
bd10: 72 61 72 79 20 66 69 6c 65 73 2e 20 49 74 20 69 rary files. It i
bd20: 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 74 6f s.** an error to
bd30: 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 call this funct
bd40: 69 6f 6e 20 69 66 20 70 50 61 67 65 72 20 69 73 ion if pPager is
bd50: 20 6f 70 65 6e 65 64 20 6f 6e 20 61 6e 20 69 6e opened on an in
bd60: 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 -memory.** datab
bd70: 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ase..**.** If th
bd80: 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63 e optimization c
bd90: 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 2c 20 30 annot be used, 0
bda0: 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 is returned. If
bdb0: 20 69 74 20 63 61 6e 20 62 65 20 75 73 65 64 2c it can be used,
bdc0: 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c .** then the val
bdd0: 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 ue returned is t
bde0: 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a he size of the j
bdf0: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e ournal file when
be00: 20 69 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 it.** contains
be10: 72 6f 6c 6c 62 61 63 6b 20 64 61 74 61 20 66 6f rollback data fo
be20: 72 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 70 61 r exactly one pa
be30: 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ge..*/.#ifdef SQ
be40: 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d LITE_ENABLE_ATOM
be50: 49 43 5f 57 52 49 54 45 0a 73 74 61 74 69 63 20 IC_WRITE.static
be60: 69 6e 74 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 int jrnlBufferSi
be70: 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ze(Pager *pPager
be80: 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 4d 45 ){. assert( !ME
be90: 4d 44 42 20 29 3b 0a 20 20 69 66 28 20 21 70 50 MDB );. if( !pP
bea0: 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 ager->tempFile )
beb0: 7b 0a 20 20 20 20 69 6e 74 20 64 63 3b 20 20 20 {. int dc;
bec0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bed0: 20 20 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63 /* Devic
bee0: 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 e characteristic
bef0: 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 65 s */. int nSe
bf00: 63 74 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 ctor;
bf10: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 /* Se
bf20: 63 74 6f 72 20 73 69 7a 65 20 2a 2f 0a 20 20 20 ctor size */.
bf30: 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 int szPage;
bf40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bf50: 20 20 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 /* Page size
bf60: 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 */.. assert(
bf70: 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
bf80: 64 29 20 29 3b 0a 20 20 20 20 64 63 20 3d 20 73 d) );. dc = s
bf90: 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 qlite3OsDeviceCh
bfa0: 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 aracteristics(pP
bfb0: 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 6e ager->fd);. n
bfc0: 53 65 63 74 6f 72 20 3d 20 70 50 61 67 65 72 2d Sector = pPager-
bfd0: 3e 73 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 20 >sectorSize;.
bfe0: 20 73 7a 50 61 67 65 20 3d 20 70 50 61 67 65 72 szPage = pPager
bff0: 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 0a 20 20 20 ->pageSize;..
c000: 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 assert(SQLITE_I
c010: 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d 3d OCAP_ATOMIC512==
c020: 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 61 (512>>8));. a
c030: 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 ssert(SQLITE_IOC
c040: 41 50 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 AP_ATOMIC64K==(6
c050: 35 35 33 36 3e 3e 38 29 29 3b 0a 20 20 20 20 69 5536>>8));. i
c060: 66 28 20 30 3d 3d 28 64 63 26 28 53 51 4c 49 54 f( 0==(dc&(SQLIT
c070: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 E_IOCAP_ATOMIC|(
c080: 73 7a 50 61 67 65 3e 3e 38 29 29 20 7c 7c 20 6e szPage>>8)) || n
c090: 53 65 63 74 6f 72 3e 73 7a 50 61 67 65 29 20 29 Sector>szPage) )
c0a0: 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 {. return 0
c0b0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 ;. }. }.. r
c0c0: 65 74 75 72 6e 20 4a 4f 55 52 4e 41 4c 5f 48 44 eturn JOURNAL_HD
c0d0: 52 5f 53 5a 28 70 50 61 67 65 72 29 20 2b 20 4a R_SZ(pPager) + J
c0e0: 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 OURNAL_PG_SZ(pPa
c0f0: 67 65 72 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a ger);.}.#endif..
c100: 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f /*.** If SQLITE_
c110: 43 48 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 CHECK_PAGES is d
c120: 65 66 69 6e 65 64 20 74 68 65 6e 20 77 65 20 64 efined then we d
c130: 6f 20 73 6f 6d 65 20 73 61 6e 69 74 79 20 63 68 o some sanity ch
c140: 65 63 6b 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 ecking.** on the
c150: 20 63 61 63 68 65 20 75 73 69 6e 67 20 61 20 68 cache using a h
c160: 61 73 68 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 ash function. T
c170: 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 his is used for
c180: 74 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 testing.** and d
c190: 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a ebugging only..*
c1a0: 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
c1b0: 43 48 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a CHECK_PAGES./*.*
c1c0: 2a 20 52 65 74 75 72 6e 20 61 20 33 32 2d 62 69 * Return a 32-bi
c1d0: 74 20 68 61 73 68 20 6f 66 20 74 68 65 20 70 61 t hash of the pa
c1e0: 67 65 20 64 61 74 61 20 66 6f 72 20 70 50 61 67 ge data for pPag
c1f0: 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 e..*/.static u32
c200: 20 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 pager_datahash(
c210: 69 6e 74 20 6e 42 79 74 65 2c 20 75 6e 73 69 67 int nByte, unsig
c220: 6e 65 64 20 63 68 61 72 20 2a 70 44 61 74 61 29 ned char *pData)
c230: 7b 0a 20 20 75 33 32 20 68 61 73 68 20 3d 20 30 {. u32 hash = 0
c240: 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 ;. int i;. for
c250: 28 69 3d 30 3b 20 69 3c 6e 42 79 74 65 3b 20 69 (i=0; i<nByte; i
c260: 2b 2b 29 7b 0a 20 20 20 20 68 61 73 68 20 3d 20 ++){. hash =
c270: 28 68 61 73 68 2a 31 30 33 39 29 20 2b 20 70 44 (hash*1039) + pD
c280: 61 74 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 ata[i];. }. re
c290: 74 75 72 6e 20 68 61 73 68 3b 0a 7d 0a 73 74 61 turn hash;.}.sta
c2a0: 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 70 61 tic u32 pager_pa
c2b0: 67 65 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 gehash(PgHdr *pP
c2c0: 61 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 age){. return p
c2d0: 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 ager_datahash(pP
c2e0: 61 67 65 2d 3e 70 50 61 67 65 72 2d 3e 70 61 67 age->pPager->pag
c2f0: 65 53 69 7a 65 2c 20 28 75 6e 73 69 67 6e 65 64 eSize, (unsigned
c300: 20 63 68 61 72 20 2a 29 70 50 61 67 65 2d 3e 70 char *)pPage->p
c310: 44 61 74 61 29 3b 0a 7d 0a 73 74 61 74 69 63 20 Data);.}.static
c320: 76 6f 69 64 20 70 61 67 65 72 5f 73 65 74 5f 70 void pager_set_p
c330: 61 67 65 68 61 73 68 28 50 67 48 64 72 20 2a 70 agehash(PgHdr *p
c340: 50 61 67 65 29 7b 0a 20 20 70 50 61 67 65 2d 3e Page){. pPage->
c350: 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 pageHash = pager
c360: 5f 70 61 67 65 68 61 73 68 28 70 50 61 67 65 29 _pagehash(pPage)
c370: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 ;.}../*.** The C
c380: 48 45 43 4b 5f 50 41 47 45 20 6d 61 63 72 6f 20 HECK_PAGE macro
c390: 74 61 6b 65 73 20 61 20 50 67 48 64 72 2a 20 61 takes a PgHdr* a
c3a0: 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 s an argument. I
c3b0: 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 f SQLITE_CHECK_P
c3c0: 41 47 45 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e AGES.** is defin
c3d0: 65 64 2c 20 61 6e 64 20 4e 44 45 42 55 47 20 69 ed, and NDEBUG i
c3e0: 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2c 20 61 s not defined, a
c3f0: 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 n assert() state
c400: 6d 65 6e 74 20 63 68 65 63 6b 73 0a 2a 2a 20 74 ment checks.** t
c410: 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 hat the page is
c420: 65 69 74 68 65 72 20 64 69 72 74 79 20 6f 72 20 either dirty or
c430: 73 74 69 6c 6c 20 6d 61 74 63 68 65 73 20 74 68 still matches th
c440: 65 20 63 61 6c 63 75 6c 61 74 65 64 20 70 61 67 e calculated pag
c450: 65 2d 68 61 73 68 2e 0a 2a 2f 0a 23 64 65 66 69 e-hash..*/.#defi
c460: 6e 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29 ne CHECK_PAGE(x)
c470: 20 63 68 65 63 6b 50 61 67 65 28 78 29 0a 73 74 checkPage(x).st
c480: 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 atic void checkP
c490: 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b age(PgHdr *pPg){
c4a0: 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
c4b0: 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a = pPg->pPager;.
c4c0: 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
c4d0: 2d 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52 5f ->eState!=PAGER_
c4e0: 45 52 52 4f 52 20 29 3b 0a 20 20 61 73 73 65 72 ERROR );. asser
c4f0: 74 28 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 t( (pPg->flags&P
c500: 47 48 44 52 5f 44 49 52 54 59 29 20 7c 7c 20 70 GHDR_DIRTY) || p
c510: 50 67 2d 3e 70 61 67 65 48 61 73 68 3d 3d 70 61 Pg->pageHash==pa
c520: 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 ger_pagehash(pPg
c530: 29 20 29 3b 0a 7d 0a 0a 23 65 6c 73 65 0a 23 64 ) );.}..#else.#d
c540: 65 66 69 6e 65 20 70 61 67 65 72 5f 64 61 74 61 efine pager_data
c550: 68 61 73 68 28 58 2c 59 29 20 20 30 0a 23 64 65 hash(X,Y) 0.#de
c560: 66 69 6e 65 20 70 61 67 65 72 5f 70 61 67 65 68 fine pager_pageh
c570: 61 73 68 28 58 29 20 20 30 0a 23 64 65 66 69 6e ash(X) 0.#defin
c580: 65 20 70 61 67 65 72 5f 73 65 74 5f 70 61 67 65 e pager_set_page
c590: 68 61 73 68 28 58 29 0a 23 64 65 66 69 6e 65 20 hash(X).#define
c5a0: 43 48 45 43 4b 5f 50 41 47 45 28 78 29 0a 23 65 CHECK_PAGE(x).#e
c5b0: 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f ndif /* SQLITE_
c5c0: 43 48 45 43 4b 5f 50 41 47 45 53 20 2a 2f 0a 0a CHECK_PAGES */..
c5d0: 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 /*.** When this
c5e0: 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20 6a 6f is called the jo
c5f0: 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 urnal file for p
c600: 61 67 65 72 20 70 50 61 67 65 72 20 6d 75 73 74 ager pPager must
c610: 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a 20 54 68 69 be open..** Thi
c620: 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d s function attem
c630: 70 74 73 20 74 6f 20 72 65 61 64 20 61 20 6d 61 pts to read a ma
c640: 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
c650: 65 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 e name from the
c660: 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 66 .** end of the f
c670: 69 6c 65 20 61 6e 64 2c 20 69 66 20 73 75 63 63 ile and, if succ
c680: 65 73 73 66 75 6c 2c 20 63 6f 70 69 65 73 20 69 essful, copies i
c690: 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 73 75 t into memory su
c6a0: 70 70 6c 69 65 64 20 0a 2a 2a 20 62 79 20 74 68 pplied .** by th
c6b0: 65 20 63 61 6c 6c 65 72 2e 20 53 65 65 20 63 6f e caller. See co
c6c0: 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 77 72 69 mments above wri
c6d0: 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 teMasterJournal(
c6e0: 29 20 66 6f 72 20 74 68 65 20 66 6f 72 6d 61 74 ) for the format
c6f0: 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 .** used to stor
c700: 65 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e e a master journ
c710: 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 61 74 20 al file name at
c720: 74 68 65 20 65 6e 64 20 6f 66 20 61 20 6a 6f 75 the end of a jou
c730: 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a rnal file..**.**
c740: 20 7a 4d 61 73 74 65 72 20 6d 75 73 74 20 70 6f zMaster must po
c750: 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 20 int to a buffer
c760: 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 4d 61 73 of at least nMas
c770: 74 65 72 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 ter bytes alloca
c780: 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 63 61 ted by.** the ca
c790: 6c 6c 65 72 2e 20 54 68 69 73 20 73 68 6f 75 6c ller. This shoul
c7a0: 64 20 62 65 20 73 71 6c 69 74 65 33 5f 76 66 73 d be sqlite3_vfs
c7b0: 2e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 20 28 74 .mxPathname+1 (t
c7c0: 6f 20 65 6e 73 75 72 65 20 74 68 65 72 65 20 69 o ensure there i
c7d0: 73 0a 2a 2a 20 65 6e 6f 75 67 68 20 73 70 61 63 s.** enough spac
c7e0: 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 6d e to write the m
c7f0: 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
c800: 6d 65 29 2e 20 49 66 20 74 68 65 20 6d 61 73 74 me). If the mast
c810: 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 6e 61 er journal.** na
c820: 6d 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 me in the journa
c830: 6c 20 69 73 20 6c 6f 6e 67 65 72 20 74 68 61 6e l is longer than
c840: 20 6e 4d 61 73 74 65 72 20 62 79 74 65 73 20 28 nMaster bytes (
c850: 69 6e 63 6c 75 64 69 6e 67 20 61 0a 2a 2a 20 6e including a.** n
c860: 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 29 2c 20 ul-terminator),
c870: 74 68 65 6e 20 74 68 69 73 20 69 73 20 68 61 6e then this is han
c880: 64 6c 65 64 20 61 73 20 69 66 20 6e 6f 20 6d 61 dled as if no ma
c890: 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster journal nam
c8a0: 65 0a 2a 2a 20 77 65 72 65 20 70 72 65 73 65 6e e.** were presen
c8b0: 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c t in the journal
c8c0: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 61 73 ..**.** If a mas
c8d0: 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
c8e0: 20 6e 61 6d 65 20 69 73 20 70 72 65 73 65 6e 74 name is present
c8f0: 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
c900: 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 he journal.** fi
c910: 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 20 63 le, then it is c
c920: 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 62 opied into the b
c930: 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f uffer pointed to
c940: 20 62 79 20 7a 4d 61 73 74 65 72 2e 20 41 0a 2a by zMaster. A.*
c950: 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 * nul-terminator
c960: 20 62 79 74 65 20 69 73 20 61 70 70 65 6e 64 65 byte is appende
c970: 64 20 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 d to the buffer
c980: 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6d 61 following the ma
c990: 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 ster.** journal
c9a0: 66 69 6c 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a file name..**.**
c9b0: 20 49 66 20 69 74 20 69 73 20 64 65 74 65 72 6d If it is determ
c9c0: 69 6e 65 64 20 74 68 61 74 20 6e 6f 20 6d 61 73 ined that no mas
c9d0: 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
c9e0: 20 6e 61 6d 65 20 69 73 20 70 72 65 73 65 6e 74 name is present
c9f0: 20 0a 2a 2a 20 7a 4d 61 73 74 65 72 5b 30 5d 20 .** zMaster[0]
ca00: 69 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 is set to 0 and
ca10: 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e SQLITE_OK return
ca20: 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 ed..**.** If an
ca30: 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 error occurs whi
ca40: 6c 65 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 le reading from
ca50: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
ca60: 2c 20 61 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 65 , an SQLite.** e
ca70: 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
ca80: 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 urned..*/.static
ca90: 20 69 6e 74 20 72 65 61 64 4d 61 73 74 65 72 4a int readMasterJ
caa0: 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 ournal(sqlite3_f
cab0: 69 6c 65 20 2a 70 4a 72 6e 6c 2c 20 63 68 61 72 ile *pJrnl, char
cac0: 20 2a 7a 4d 61 73 74 65 72 2c 20 75 33 32 20 6e *zMaster, u32 n
cad0: 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 Master){. int r
cae0: 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
caf0: 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
cb00: 63 6f 64 65 20 2a 2f 0a 20 20 75 33 32 20 6c 65 code */. u32 le
cb10: 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
cb20: 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 69 /* Length i
cb30: 6e 20 62 79 74 65 73 20 6f 66 20 6d 61 73 74 65 n bytes of maste
cb40: 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a r journal name *
cb50: 2f 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 /. i64 szJ;
cb60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
cb70: 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 69 6e 20 * Total size in
cb80: 62 79 74 65 73 20 6f 66 20 6a 6f 75 72 6e 61 6c bytes of journal
cb90: 20 66 69 6c 65 20 70 4a 72 6e 6c 20 2a 2f 0a 20 file pJrnl */.
cba0: 20 75 33 32 20 63 6b 73 75 6d 3b 20 20 20 20 20 u32 cksum;
cbb0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
cbc0: 4a 20 63 68 65 63 6b 73 75 6d 20 76 61 6c 75 65 J checksum value
cbd0: 20 72 65 61 64 20 66 72 6f 6d 20 6a 6f 75 72 6e read from journ
cbe0: 61 6c 20 2a 2f 0a 20 20 75 33 32 20 75 3b 20 20 al */. u32 u;
cbf0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cc00: 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c /* Unsigned l
cc10: 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
cc20: 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 unsigned char a
cc30: 4d 61 67 69 63 5b 38 5d 3b 20 20 20 2f 2a 20 41 Magic[8]; /* A
cc40: 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 buffer to hold
cc50: 74 68 65 20 6d 61 67 69 63 20 68 65 61 64 65 72 the magic header
cc60: 20 2a 2f 0a 20 20 7a 4d 61 73 74 65 72 5b 30 5d */. zMaster[0]
cc70: 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 69 66 28 20 = '\0';.. if(
cc80: 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
cc90: 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 sqlite3OsFileSi
cca0: 7a 65 28 70 4a 72 6e 6c 2c 20 26 73 7a 4a 29 29 ze(pJrnl, &szJ))
ccb0: 0a 20 20 20 7c 7c 20 73 7a 4a 3c 31 36 0a 20 20 . || szJ<16.
ccc0: 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 || SQLITE_OK!=(
ccd0: 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
cce0: 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 36 2c 20 26 pJrnl, szJ-16, &
ccf0: 6c 65 6e 29 29 0a 20 20 20 7c 7c 20 6c 65 6e 3e len)). || len>
cd00: 3d 6e 4d 61 73 74 65 72 20 0a 20 20 20 7c 7c 20 =nMaster . ||
cd10: 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
cd20: 20 72 65 61 64 33 32 62 69 74 73 28 70 4a 72 6e read32bits(pJrn
cd30: 6c 2c 20 73 7a 4a 2d 31 32 2c 20 26 63 6b 73 75 l, szJ-12, &cksu
cd40: 6d 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 m)). || SQLITE
cd50: 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 _OK!=(rc = sqlit
cd60: 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20 e3OsRead(pJrnl,
cd70: 61 4d 61 67 69 63 2c 20 38 2c 20 73 7a 4a 2d 38 aMagic, 8, szJ-8
cd80: 29 29 0a 20 20 20 7c 7c 20 6d 65 6d 63 6d 70 28 )). || memcmp(
cd90: 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c aMagic, aJournal
cda0: 4d 61 67 69 63 2c 20 38 29 0a 20 20 20 7c 7c 20 Magic, 8). ||
cdb0: 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
cdc0: 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 sqlite3OsRead(p
cdd0: 4a 72 6e 6c 2c 20 7a 4d 61 73 74 65 72 2c 20 6c Jrnl, zMaster, l
cde0: 65 6e 2c 20 73 7a 4a 2d 31 36 2d 6c 65 6e 29 29 en, szJ-16-len))
cdf0: 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e . ){. return
ce00: 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 rc;. }.. /* S
ce10: 65 65 20 69 66 20 74 68 65 20 63 68 65 63 6b 73 ee if the checks
ce20: 75 6d 20 6d 61 74 63 68 65 73 20 74 68 65 20 6d um matches the m
ce30: 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
ce40: 6d 65 20 2a 2f 0a 20 20 66 6f 72 28 75 3d 30 3b me */. for(u=0;
ce50: 20 75 3c 6c 65 6e 3b 20 75 2b 2b 29 7b 0a 20 20 u<len; u++){.
ce60: 20 20 63 6b 73 75 6d 20 2d 3d 20 7a 4d 61 73 74 cksum -= zMast
ce70: 65 72 5b 75 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 er[u];. }. if(
ce80: 20 63 6b 73 75 6d 20 29 7b 0a 20 20 20 20 2f 2a cksum ){. /*
ce90: 20 49 66 20 74 68 65 20 63 68 65 63 6b 73 75 6d If the checksum
cea0: 20 64 6f 65 73 6e 27 74 20 61 64 64 20 75 70 2c doesn't add up,
ceb0: 20 74 68 65 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 then one or mor
cec0: 65 20 6f 66 20 74 68 65 20 64 69 73 6b 20 73 65 e of the disk se
ced0: 63 74 6f 72 73 0a 20 20 20 20 2a 2a 20 63 6f 6e ctors. ** con
cee0: 74 61 69 6e 69 6e 67 20 74 68 65 20 6d 61 73 74 taining the mast
cef0: 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e er journal filen
cf00: 61 6d 65 20 69 73 20 63 6f 72 72 75 70 74 65 64 ame is corrupted
cf10: 2e 20 54 68 69 73 20 6d 65 61 6e 73 0a 20 20 20 . This means.
cf20: 20 2a 2a 20 64 65 66 69 6e 69 74 65 6c 79 20 72 ** definitely r
cf30: 6f 6c 6c 20 62 61 63 6b 2c 20 73 6f 20 6a 75 73 oll back, so jus
cf40: 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f t return SQLITE_
cf50: 4f 4b 20 61 6e 64 20 72 65 70 6f 72 74 20 61 20 OK and report a
cf60: 28 6e 75 6c 29 0a 20 20 20 20 2a 2a 20 6d 61 73 (nul). ** mas
cf70: 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter-journal file
cf80: 6e 61 6d 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 name.. */.
cf90: 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 len = 0;. }.
cfa0: 7a 4d 61 73 74 65 72 5b 6c 65 6e 5d 20 3d 20 27 zMaster[len] = '
cfb0: 5c 30 27 3b 0a 20 20 20 0a 20 20 72 65 74 75 72 \0';. . retur
cfc0: 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
cfd0: 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
cfe0: 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 73 offset of the s
cff0: 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 20 61 ector boundary a
d000: 74 20 6f 72 20 69 6d 6d 65 64 69 61 74 65 6c 79 t or immediately
d010: 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 .** following t
d020: 68 65 20 76 61 6c 75 65 20 69 6e 20 70 50 61 67 he value in pPag
d030: 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 er->journalOff,
d040: 61 73 73 75 6d 69 6e 67 20 61 20 73 65 63 74 6f assuming a secto
d050: 72 20 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 70 50 r .** size of pP
d060: 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 ager->sectorSize
d070: 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e bytes..**.** i.
d080: 65 20 66 6f 72 20 61 20 73 65 63 74 6f 72 20 73 e for a sector s
d090: 69 7a 65 20 6f 66 20 35 31 32 3a 0a 2a 2a 0a 2a ize of 512:.**.*
d0a0: 2a 20 20 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 * Pager.journa
d0b0: 6c 4f 66 66 20 20 20 20 20 20 20 20 20 20 52 65 lOff Re
d0c0: 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 20 20 turn value.**
d0d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
d0e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
d0f0: 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 30 20 20 -------.** 0
d100: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d110: 20 20 20 20 20 20 20 30 0a 2a 2a 20 20 20 35 31 0.** 51
d120: 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2
d130: 20 20 20 20 20 20 20 20 35 31 32 0a 2a 2a 20 20 512.**
d140: 20 31 30 30 20 20 20 20 20 20 20 20 20 20 20 20 100
d150: 20 20 20 20 20 20 20 20 20 20 20 35 31 32 0a 2a 512.*
d160: 2a 20 20 20 32 30 30 30 20 20 20 20 20 20 20 20 * 2000
d170: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 30 20
d180: 34 38 0a 2a 2a 20 0a 2a 2f 0a 73 74 61 74 69 63 48.** .*/.static
d190: 20 69 36 34 20 6a 6f 75 72 6e 61 6c 48 64 72 4f i64 journalHdrO
d1a0: 66 66 73 65 74 28 50 61 67 65 72 20 2a 70 50 61 ffset(Pager *pPa
d1b0: 67 65 72 29 7b 0a 20 20 69 36 34 20 6f 66 66 73 ger){. i64 offs
d1c0: 65 74 20 3d 20 30 3b 0a 20 20 69 36 34 20 63 20 et = 0;. i64 c
d1d0: 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 = pPager->journa
d1e0: 6c 4f 66 66 3b 0a 20 20 69 66 28 20 63 20 29 7b lOff;. if( c ){
d1f0: 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20 28 28 . offset = ((
d200: 63 2d 31 29 2f 4a 4f 55 52 4e 41 4c 5f 48 44 52 c-1)/JOURNAL_HDR
d210: 5f 53 5a 28 70 50 61 67 65 72 29 20 2b 20 31 29 _SZ(pPager) + 1)
d220: 20 2a 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 * JOURNAL_HDR_S
d230: 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 Z(pPager);. }.
d240: 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 25 assert( offset%
d250: 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 JOURNAL_HDR_SZ(p
d260: 50 61 67 65 72 29 3d 3d 30 20 29 3b 0a 20 20 61 Pager)==0 );. a
d270: 73 73 65 72 74 28 20 6f 66 66 73 65 74 3e 3d 63 ssert( offset>=c
d280: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6f );. assert( (o
d290: 66 66 73 65 74 2d 63 29 3c 4a 4f 55 52 4e 41 4c ffset-c)<JOURNAL
d2a0: 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 _HDR_SZ(pPager)
d2b0: 29 3b 0a 20 20 72 65 74 75 72 6e 20 6f 66 66 73 );. return offs
d2c0: 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 et;.}../*.** The
d2d0: 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 journal file mu
d2e0: 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 st be open when
d2f0: 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
d300: 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 called..**.** T
d310: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
d320: 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 6a a no-op if the j
d330: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 ournal file has
d340: 6e 6f 74 20 62 65 65 6e 20 77 72 69 74 74 65 6e not been written
d350: 20 74 6f 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 to.** within th
d360: 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 e current transa
d370: 63 74 69 6f 6e 20 28 69 2e 65 2e 20 69 66 20 50 ction (i.e. if P
d380: 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 3d ager.journalOff=
d390: 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 6f =0)..**.** If do
d3a0: 54 72 75 6e 63 61 74 65 20 69 73 20 6e 6f 6e 2d Truncate is non-
d3b0: 7a 65 72 6f 20 6f 72 20 74 68 65 20 50 61 67 65 zero or the Page
d3c0: 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d r.journalSizeLim
d3d0: 69 74 20 76 61 72 69 61 62 6c 65 20 69 73 0a 2a it variable is.*
d3e0: 2a 20 73 65 74 20 74 6f 20 30 2c 20 74 68 65 6e * set to 0, then
d3f0: 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f truncate the jo
d400: 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 7a 65 urnal file to ze
d410: 72 6f 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 ro bytes in size
d420: 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a 2a 20 . Otherwise,.**
d430: 7a 65 72 6f 20 74 68 65 20 32 38 2d 62 79 74 65 zero the 28-byte
d440: 20 68 65 61 64 65 72 20 61 74 20 74 68 65 20 73 header at the s
d450: 74 61 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 tart of the jour
d460: 6e 61 6c 20 66 69 6c 65 2e 20 49 6e 20 65 69 74 nal file. In eit
d470: 68 65 72 20 63 61 73 65 2c 20 0a 2a 2a 20 69 66 her case, .** if
d480: 20 74 68 65 20 70 61 67 65 72 20 69 73 20 6e 6f the pager is no
d490: 74 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 t in no-sync mod
d4a0: 65 2c 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 e, sync the jour
d4b0: 6e 61 6c 20 66 69 6c 65 20 69 6d 6d 65 64 69 61 nal file immedia
d4c0: 74 65 6c 79 20 0a 2a 2a 20 61 66 74 65 72 20 77 tely .** after w
d4d0: 72 69 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 riting or trunca
d4e0: 74 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 ting it..**.** I
d4f0: 66 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 f Pager.journalS
d500: 69 7a 65 4c 69 6d 69 74 20 69 73 20 73 65 74 20 izeLimit is set
d510: 74 6f 20 61 20 70 6f 73 69 74 69 76 65 2c 20 6e to a positive, n
d520: 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 61 on-zero value, a
d530: 6e 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 nd.** following
d540: 74 68 65 20 74 72 75 6e 63 61 74 69 6f 6e 20 6f the truncation o
d550: 72 20 7a 65 72 6f 69 6e 67 20 64 65 73 63 72 69 r zeroing descri
d560: 62 65 64 20 61 62 6f 76 65 20 74 68 65 20 73 69 bed above the si
d570: 7a 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 6a 6f ze of the .** jo
d580: 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 urnal file in by
d590: 74 65 73 20 69 73 20 6c 61 72 67 65 72 20 74 68 tes is larger th
d5a0: 61 6e 20 74 68 69 73 20 76 61 6c 75 65 2c 20 74 an this value, t
d5b0: 68 65 6e 20 74 72 75 6e 63 61 74 65 20 74 68 65 hen truncate the
d5c0: 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 .** journal file
d5d0: 20 74 6f 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 to Pager.journa
d5e0: 6c 53 69 7a 65 4c 69 6d 69 74 20 62 79 74 65 73 lSizeLimit bytes
d5f0: 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 . The journal fi
d600: 6c 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 6e le does.** not n
d610: 65 65 64 20 74 6f 20 62 65 20 73 79 6e 63 65 64 eed to be synced
d620: 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 following this
d630: 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a operation..**.**
d640: 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 If an IO error
d650: 6f 63 63 75 72 73 2c 20 61 62 61 6e 64 6f 6e 20 occurs, abandon
d660: 70 72 6f 63 65 73 73 69 6e 67 20 61 6e 64 20 72 processing and r
d670: 65 74 75 72 6e 20 74 68 65 20 49 4f 20 65 72 72 eturn the IO err
d680: 6f 72 20 63 6f 64 65 2e 0a 2a 2a 20 4f 74 68 65 or code..** Othe
d690: 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20 53 51 rwise, return SQ
d6a0: 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 LITE_OK..*/.stat
d6b0: 69 63 20 69 6e 74 20 7a 65 72 6f 4a 6f 75 72 6e ic int zeroJourn
d6c0: 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 50 61 alHdr(Pager *pPa
d6d0: 67 65 72 2c 20 69 6e 74 20 64 6f 54 72 75 6e 63 ger, int doTrunc
d6e0: 61 74 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ate){. int rc =
d6f0: 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
d700: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d710: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
d720: 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 61 73 urn code */. as
d730: 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 sert( isOpen(pPa
d740: 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 69 ger->jfd) );. i
d750: 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
d760: 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 63 6f 6e alOff ){. con
d770: 73 74 20 69 36 34 20 69 4c 69 6d 69 74 20 3d 20 st i64 iLimit =
d780: 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 pPager->journalS
d790: 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 2f 2a 20 izeLimit; /*
d7a0: 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 6a Local cache of j
d7b0: 73 6c 20 2a 2f 0a 0a 20 20 20 20 49 4f 54 52 41 sl */.. IOTRA
d7c0: 43 45 28 28 22 4a 5a 45 52 4f 48 44 52 20 25 70 CE(("JZEROHDR %p
d7d0: 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 \n", pPager)).
d7e0: 20 20 69 66 28 20 64 6f 54 72 75 6e 63 61 74 65 if( doTruncate
d7f0: 20 7c 7c 20 69 4c 69 6d 69 74 3d 3d 30 20 29 7b || iLimit==0 ){
d800: 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
d810: 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 te3OsTruncate(pP
d820: 61 67 65 72 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 ager->jfd, 0);.
d830: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
d840: 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 static const cha
d850: 72 20 7a 65 72 6f 48 64 72 5b 32 38 5d 20 3d 20 r zeroHdr[28] =
d860: 7b 30 7d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 {0};. rc =
d870: 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 sqlite3OsWrite(p
d880: 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 65 72 6f Pager->jfd, zero
d890: 48 64 72 2c 20 73 69 7a 65 6f 66 28 7a 65 72 6f Hdr, sizeof(zero
d8a0: 48 64 72 29 2c 20 30 29 3b 0a 20 20 20 20 7d 0a Hdr), 0);. }.
d8b0: 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
d8c0: 54 45 5f 4f 4b 20 26 26 20 21 70 50 61 67 65 72 TE_OK && !pPager
d8d0: 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 ->noSync ){.
d8e0: 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
d8f0: 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 Sync(pPager->jfd
d900: 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 , SQLITE_SYNC_DA
d910: 54 41 4f 4e 4c 59 7c 70 50 61 67 65 72 2d 3e 73 TAONLY|pPager->s
d920: 79 6e 63 46 6c 61 67 73 29 3b 0a 20 20 20 20 7d yncFlags);. }
d930: 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73 .. /* At this
d940: 20 70 6f 69 6e 74 20 74 68 65 20 74 72 61 6e 73 point the trans
d950: 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 action is commit
d960: 74 65 64 20 62 75 74 20 74 68 65 20 77 72 69 74 ted but the writ
d970: 65 20 6c 6f 63 6b 20 0a 20 20 20 20 2a 2a 20 69 e lock . ** i
d980: 73 20 73 74 69 6c 6c 20 68 65 6c 64 20 6f 6e 20 s still held on
d990: 74 68 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 the file. If the
d9a0: 72 65 20 69 73 20 61 20 73 69 7a 65 20 6c 69 6d re is a size lim
d9b0: 69 74 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f it configured fo
d9c0: 72 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 65 r . ** the pe
d9d0: 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c rsistent journal
d9e0: 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c and the journal
d9f0: 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 file currently
da00: 63 6f 6e 73 75 6d 65 73 20 6d 6f 72 65 0a 20 20 consumes more.
da10: 20 20 2a 2a 20 73 70 61 63 65 20 74 68 61 6e 20 ** space than
da20: 74 68 61 74 20 6c 69 6d 69 74 20 61 6c 6c 6f 77 that limit allow
da30: 73 20 66 6f 72 2c 20 74 72 75 6e 63 61 74 65 20 s for, truncate
da40: 69 74 20 6e 6f 77 2e 20 54 68 65 72 65 20 69 73 it now. There is
da50: 20 6e 6f 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 no need. **
da60: 74 6f 20 73 79 6e 63 20 74 68 65 20 66 69 6c 65 to sync the file
da70: 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 following this
da80: 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20 20 20 2a operation.. *
da90: 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 /. if( rc==SQ
daa0: 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4c 69 6d 69 LITE_OK && iLimi
dab0: 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 36 34 t>0 ){. i64
dac0: 20 73 7a 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 sz;. rc =
dad0: 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a sqlite3OsFileSiz
dae0: 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 e(pPager->jfd, &
daf0: 73 7a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 sz);. if( r
db00: 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
db10: 73 7a 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 sz>iLimit ){.
db20: 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
db30: 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 3OsTruncate(pPag
db40: 65 72 2d 3e 6a 66 64 2c 20 69 4c 69 6d 69 74 29 er->jfd, iLimit)
db50: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
db60: 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
db70: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f .}../*.** The jo
db80: 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 urnal file must
db90: 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 be open when thi
dba0: 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
dbb0: 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61 6c 0a 2a led. A journal.*
dbc0: 2a 20 68 65 61 64 65 72 20 28 4a 4f 55 52 4e 41 * header (JOURNA
dbd0: 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 L_HDR_SZ bytes)
dbe0: 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 is written into
dbf0: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
dc00: 20 61 74 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 at the.** curre
dc10: 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a nt location..**.
dc20: 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 66 6f ** The format fo
dc30: 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 r the journal he
dc40: 61 64 65 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f ader is as follo
dc50: 77 73 3a 0a 2a 2a 20 2d 20 38 20 62 79 74 65 73 ws:.** - 8 bytes
dc60: 3a 20 4d 61 67 69 63 20 69 64 65 6e 74 69 66 79 : Magic identify
dc70: 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d ing journal form
dc80: 61 74 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 at..** - 4 bytes
dc90: 3a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f : Number of reco
dca0: 72 64 73 20 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 rds in journal,
dcb0: 6f 72 20 2d 31 20 6e 6f 2d 73 79 6e 63 20 6d 6f or -1 no-sync mo
dcc0: 64 65 20 69 73 20 6f 6e 2e 0a 2a 2a 20 2d 20 34 de is on..** - 4
dcd0: 20 62 79 74 65 73 3a 20 52 61 6e 64 6f 6d 20 6e bytes: Random n
dce0: 75 6d 62 65 72 20 75 73 65 64 20 66 6f 72 20 70 umber used for p
dcf0: 61 67 65 20 68 61 73 68 2e 0a 2a 2a 20 2d 20 34 age hash..** - 4
dd00: 20 62 79 74 65 73 3a 20 49 6e 69 74 69 61 6c 20 bytes: Initial
dd10: 64 61 74 61 62 61 73 65 20 70 61 67 65 20 63 6f database page co
dd20: 75 6e 74 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 unt..** - 4 byte
dd30: 73 3a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 75 s: Sector size u
dd40: 73 65 64 20 62 79 20 74 68 65 20 70 72 6f 63 65 sed by the proce
dd50: 73 73 20 74 68 61 74 20 77 72 6f 74 65 20 74 68 ss that wrote th
dd60: 69 73 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 2d is journal..** -
dd70: 20 34 20 62 79 74 65 73 3a 20 44 61 74 61 62 61 4 bytes: Databa
dd80: 73 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a se page size..**
dd90: 20 0a 2a 2a 20 46 6f 6c 6c 6f 77 65 64 20 62 79 .** Followed by
dda0: 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a (JOURNAL_HDR_SZ
ddb0: 20 2d 20 32 38 29 20 62 79 74 65 73 20 6f 66 20 - 28) bytes of
ddc0: 75 6e 75 73 65 64 20 73 70 61 63 65 2e 0a 2a 2f unused space..*/
ddd0: 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 .static int writ
dde0: 65 4a 6f 75 72 6e 61 6c 48 64 72 28 50 61 67 65 eJournalHdr(Page
ddf0: 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e r *pPager){. in
de00: 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
de10: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
de20: 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
de30: 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 48 65 61 */. char *zHea
de40: 64 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 der = pPager->pT
de50: 6d 70 53 70 61 63 65 3b 20 20 2f 2a 20 54 65 6d mpSpace; /* Tem
de60: 70 6f 72 61 72 79 20 73 70 61 63 65 20 75 73 65 porary space use
de70: 64 20 74 6f 20 62 75 69 6c 64 20 68 65 61 64 65 d to build heade
de80: 72 20 2a 2f 0a 20 20 75 33 32 20 6e 48 65 61 64 r */. u32 nHead
de90: 65 72 20 3d 20 28 75 33 32 29 70 50 61 67 65 72 er = (u32)pPager
dea0: 2d 3e 70 61 67 65 53 69 7a 65 3b 2f 2a 20 53 69 ->pageSize;/* Si
deb0: 7a 65 20 6f 66 20 62 75 66 66 65 72 20 70 6f 69 ze of buffer poi
dec0: 6e 74 65 64 20 74 6f 20 62 79 20 7a 48 65 61 64 nted to by zHead
ded0: 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 57 72 69 er */. u32 nWri
dee0: 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 te;
def0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
df00: 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 ytes of header s
df10: 65 63 74 6f 72 20 77 72 69 74 74 65 6e 20 2a 2f ector written */
df20: 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 . int ii;
df30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
df40: 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
df50: 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 61 73 73 ounter */.. ass
df60: 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 ert( isOpen(pPag
df70: 65 72 2d 3e 6a 66 64 29 20 29 3b 20 20 20 20 20 er->jfd) );
df80: 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 /* Journal file
df90: 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 2e 20 2a must be open. *
dfa0: 2f 0a 0a 20 20 69 66 28 20 6e 48 65 61 64 65 72 /.. if( nHeader
dfb0: 3e 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 >JOURNAL_HDR_SZ(
dfc0: 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 6e pPager) ){. n
dfd0: 48 65 61 64 65 72 20 3d 20 4a 4f 55 52 4e 41 4c Header = JOURNAL
dfe0: 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b _HDR_SZ(pPager);
dff0: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 . }.. /* If th
e000: 65 72 65 20 61 72 65 20 61 63 74 69 76 65 20 73 ere are active s
e010: 61 76 65 70 6f 69 6e 74 73 20 61 6e 64 20 61 6e avepoints and an
e020: 79 20 6f 66 20 74 68 65 6d 20 77 65 72 65 20 63 y of them were c
e030: 72 65 61 74 65 64 20 0a 20 20 2a 2a 20 73 69 6e reated . ** sin
e040: 63 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 ce the most rece
e050: 6e 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 nt journal heade
e060: 72 20 77 61 73 20 77 72 69 74 74 65 6e 2c 20 75 r was written, u
e070: 70 64 61 74 65 20 74 68 65 20 0a 20 20 2a 2a 20 pdate the . **
e080: 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 PagerSavepoint.i
e090: 48 64 72 4f 66 66 73 65 74 20 66 69 65 6c 64 73 HdrOffset fields
e0a0: 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 now.. */. for
e0b0: 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 (ii=0; ii<pPager
e0c0: 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 ->nSavepoint; ii
e0d0: 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 ++){. if( pPa
e0e0: 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b ger->aSavepoint[
e0f0: 69 69 5d 2e 69 48 64 72 4f 66 66 73 65 74 3d 3d ii].iHdrOffset==
e100: 30 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 0 ){. pPage
e110: 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 r->aSavepoint[ii
e120: 5d 2e 69 48 64 72 4f 66 66 73 65 74 20 3d 20 70 ].iHdrOffset = p
e130: 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
e140: 66 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 f;. }. }..
e150: 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 pPager->journalH
e160: 64 72 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 dr = pPager->jou
e170: 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 rnalOff = journa
e180: 6c 48 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 lHdrOffset(pPage
e190: 72 29 3b 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 r);.. /* . **
e1a0: 57 72 69 74 65 20 74 68 65 20 6e 52 65 63 20 46 Write the nRec F
e1b0: 69 65 6c 64 20 2d 20 74 68 65 20 6e 75 6d 62 65 ield - the numbe
e1c0: 72 20 6f 66 20 70 61 67 65 20 72 65 63 6f 72 64 r of page record
e1d0: 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 74 68 s that follow th
e1e0: 69 73 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 is. ** journal
e1f0: 68 65 61 64 65 72 2e 20 4e 6f 72 6d 61 6c 6c 79 header. Normally
e200: 2c 20 7a 65 72 6f 20 69 73 20 77 72 69 74 74 65 , zero is writte
e210: 6e 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 20 n to this value
e220: 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 20 20 at this time..
e230: 2a 2a 20 41 66 74 65 72 20 74 68 65 20 72 65 63 ** After the rec
e240: 6f 72 64 73 20 61 72 65 20 61 64 64 65 64 20 74 ords are added t
e250: 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 28 61 o the journal (a
e260: 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 73 nd the journal s
e270: 79 6e 63 65 64 2c 20 0a 20 20 2a 2a 20 69 66 20 ynced, . ** if
e280: 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 in full-sync mod
e290: 65 29 2c 20 74 68 65 20 7a 65 72 6f 20 69 73 20 e), the zero is
e2a0: 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 overwritten with
e2b0: 20 74 68 65 20 74 72 75 65 20 6e 75 6d 62 65 72 the true number
e2c0: 0a 20 20 2a 2a 20 6f 66 20 72 65 63 6f 72 64 73 . ** of records
e2d0: 20 28 73 65 65 20 73 79 6e 63 4a 6f 75 72 6e 61 (see syncJourna
e2e0: 6c 28 29 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 l()).. **. **
e2f0: 41 20 66 61 73 74 65 72 20 61 6c 74 65 72 6e 61 A faster alterna
e300: 74 69 76 65 20 69 73 20 74 6f 20 77 72 69 74 65 tive is to write
e310: 20 30 78 46 46 46 46 46 46 46 46 20 74 6f 20 74 0xFFFFFFFF to t
e320: 68 65 20 6e 52 65 63 20 66 69 65 6c 64 2e 20 57 he nRec field. W
e330: 68 65 6e 0a 20 20 2a 2a 20 72 65 61 64 69 6e 67 hen. ** reading
e340: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 68 69 the journal thi
e350: 73 20 76 61 6c 75 65 20 74 65 6c 6c 73 20 53 51 s value tells SQ
e360: 4c 69 74 65 20 74 6f 20 61 73 73 75 6d 65 20 74 Lite to assume t
e370: 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 72 65 73 hat the. ** res
e380: 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c t of the journal
e390: 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 76 file contains v
e3a0: 61 6c 69 64 20 70 61 67 65 20 72 65 63 6f 72 64 alid page record
e3b0: 73 2e 20 54 68 69 73 20 61 73 73 75 6d 70 74 69 s. This assumpti
e3c0: 6f 6e 0a 20 20 2a 2a 20 69 73 20 64 61 6e 67 65 on. ** is dange
e3d0: 72 6f 75 73 2c 20 61 73 20 69 66 20 61 20 66 61 rous, as if a fa
e3e0: 69 6c 75 72 65 20 6f 63 63 75 72 72 65 64 20 77 ilure occurred w
e3f0: 68 69 6c 73 74 20 77 72 69 74 69 6e 67 20 74 6f hilst writing to
e400: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a the journal. *
e410: 2a 20 66 69 6c 65 20 69 74 20 6d 61 79 20 63 6f * file it may co
e420: 6e 74 61 69 6e 20 73 6f 6d 65 20 67 61 72 62 61 ntain some garba
e430: 67 65 20 64 61 74 61 2e 20 54 68 65 72 65 20 61 ge data. There a
e440: 72 65 20 74 77 6f 20 73 63 65 6e 61 72 69 6f 73 re two scenarios
e450: 0a 20 20 2a 2a 20 77 68 65 72 65 20 74 68 69 73 . ** where this
e460: 20 72 69 73 6b 20 63 61 6e 20 62 65 20 69 67 6e risk can be ign
e470: 6f 72 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 ored:. **. **
e480: 20 20 2a 20 57 68 65 6e 20 74 68 65 20 70 61 67 * When the pag
e490: 65 72 20 69 73 20 69 6e 20 6e 6f 2d 73 79 6e 63 er is in no-sync
e4a0: 20 6d 6f 64 65 2e 20 43 6f 72 72 75 70 74 69 6f mode. Corruptio
e4b0: 6e 20 63 61 6e 20 66 6f 6c 6c 6f 77 20 61 0a 20 n can follow a.
e4c0: 20 2a 2a 20 20 20 20 20 70 6f 77 65 72 20 66 61 ** power fa
e4d0: 69 6c 75 72 65 20 69 6e 20 74 68 69 73 20 63 61 ilure in this ca
e4e0: 73 65 20 61 6e 79 77 61 79 2e 0a 20 20 2a 2a 0a se anyway.. **.
e4f0: 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 68 ** * When th
e500: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 e SQLITE_IOCAP_S
e510: 41 46 45 5f 41 50 50 45 4e 44 20 66 6c 61 67 20 AFE_APPEND flag
e520: 69 73 20 73 65 74 2e 20 54 68 69 73 20 67 75 61 is set. This gua
e530: 72 61 6e 74 65 65 73 0a 20 20 2a 2a 20 20 20 20 rantees. **
e540: 20 74 68 61 74 20 67 61 72 62 61 67 65 20 64 61 that garbage da
e550: 74 61 20 69 73 20 6e 65 76 65 72 20 61 70 70 65 ta is never appe
e560: 6e 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 nded to the jour
e570: 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 nal file.. */.
e580: 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 assert( isOpen(
e590: 70 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 pPager->fd) || p
e5a0: 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 3b Pager->noSync );
e5b0: 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e . if( pPager->n
e5c0: 6f 53 79 6e 63 20 7c 7c 20 28 70 50 61 67 65 72 oSync || (pPager
e5d0: 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
e5e0: 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
e5f0: 5f 4d 45 4d 4f 52 59 29 0a 20 20 20 7c 7c 20 28 _MEMORY). || (
e600: 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 sqlite3OsDeviceC
e610: 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 haracteristics(p
e620: 50 61 67 65 72 2d 3e 66 64 29 26 53 51 4c 49 54 Pager->fd)&SQLIT
e630: 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 E_IOCAP_SAFE_APP
e640: 45 4e 44 29 20 0a 20 20 29 7b 0a 20 20 20 20 6d END) . ){. m
e650: 65 6d 63 70 79 28 7a 48 65 61 64 65 72 2c 20 61 emcpy(zHeader, a
e660: 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 JournalMagic, si
e670: 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
e680: 69 63 29 29 3b 0a 20 20 20 20 70 75 74 33 32 62 ic));. put32b
e690: 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a its(&zHeader[siz
e6a0: 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 eof(aJournalMagi
e6b0: 63 29 5d 2c 20 30 78 66 66 66 66 66 66 66 66 29 c)], 0xffffffff)
e6c0: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d ;. }else{. m
e6d0: 65 6d 73 65 74 28 7a 48 65 61 64 65 72 2c 20 30 emset(zHeader, 0
e6e0: 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 , sizeof(aJourna
e6f0: 6c 4d 61 67 69 63 29 2b 34 29 3b 0a 20 20 7d 0a lMagic)+4);. }.
e700: 0a 20 20 2f 2a 20 54 68 65 20 72 61 6e 64 6f 6d . /* The random
e710: 20 63 68 65 63 6b 2d 68 61 73 68 20 69 6e 69 74 check-hash init
e720: 69 61 6c 69 73 65 72 20 2a 2f 20 0a 20 20 73 71 ialiser */ . sq
e730: 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 lite3_randomness
e740: 28 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e (sizeof(pPager->
e750: 63 6b 73 75 6d 49 6e 69 74 29 2c 20 26 70 50 61 cksumInit), &pPa
e760: 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b ger->cksumInit);
e770: 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 . put32bits(&zH
e780: 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f eader[sizeof(aJo
e790: 75 72 6e 61 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 urnalMagic)+4],
e7a0: 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 pPager->cksumIni
e7b0: 74 29 3b 0a 20 20 2f 2a 20 54 68 65 20 69 6e 69 t);. /* The ini
e7c0: 74 69 61 6c 20 64 61 74 61 62 61 73 65 20 73 69 tial database si
e7d0: 7a 65 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 ze */. put32bit
e7e0: 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f s(&zHeader[sizeo
e7f0: 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 f(aJournalMagic)
e800: 2b 38 5d 2c 20 70 50 61 67 65 72 2d 3e 64 62 4f +8], pPager->dbO
e810: 72 69 67 53 69 7a 65 29 3b 0a 20 20 2f 2a 20 54 rigSize);. /* T
e820: 68 65 20 61 73 73 75 6d 65 64 20 73 65 63 74 6f he assumed secto
e830: 72 20 73 69 7a 65 20 66 6f 72 20 74 68 69 73 20 r size for this
e840: 70 72 6f 63 65 73 73 20 2a 2f 0a 20 20 70 75 74 process */. put
e850: 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 32bits(&zHeader[
e860: 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d sizeof(aJournalM
e870: 61 67 69 63 29 2b 31 32 5d 2c 20 70 50 61 67 65 agic)+12], pPage
e880: 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a r->sectorSize);.
e890: 0a 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 73 . /* The page s
e8a0: 69 7a 65 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 ize */. put32bi
e8b0: 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 ts(&zHeader[size
e8c0: 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 of(aJournalMagic
e8d0: 29 2b 31 36 5d 2c 20 70 50 61 67 65 72 2d 3e 70 )+16], pPager->p
e8e0: 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 ageSize);.. /*
e8f0: 49 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 Initializing the
e900: 20 74 61 69 6c 20 6f 66 20 74 68 65 20 62 75 66 tail of the buf
e910: 66 65 72 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 fer is not neces
e920: 73 61 72 79 2e 20 20 45 76 65 72 79 74 68 69 6e sary. Everythin
e930: 67 0a 20 20 2a 2a 20 77 6f 72 6b 73 20 66 69 6e g. ** works fin
e940: 64 20 69 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 d if the followi
e950: 6e 67 20 6d 65 6d 73 65 74 28 29 20 69 73 20 6f ng memset() is o
e960: 6d 69 74 74 65 64 2e 20 20 42 75 74 20 69 6e 69 mitted. But ini
e970: 74 69 61 6c 69 7a 69 6e 67 0a 20 20 2a 2a 20 74 tializing. ** t
e980: 68 65 20 6d 65 6d 6f 72 79 20 70 72 65 76 65 6e he memory preven
e990: 74 73 20 76 61 6c 67 72 69 6e 64 20 66 72 6f 6d ts valgrind from
e9a0: 20 63 6f 6d 70 6c 61 69 6e 69 6e 67 2c 20 73 6f complaining, so
e9b0: 20 77 65 20 61 72 65 20 77 69 6c 6c 69 6e 67 20 we are willing
e9c0: 74 6f 0a 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 to. ** take the
e9d0: 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 68 69 74 performance hit
e9e0: 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 .. */. memset(
e9f0: 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 &zHeader[sizeof(
ea00: 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 aJournalMagic)+2
ea10: 30 5d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 0], 0,.
ea20: 6e 48 65 61 64 65 72 2d 28 73 69 7a 65 6f 66 28 nHeader-(sizeof(
ea30: 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 aJournalMagic)+2
ea40: 30 29 29 3b 0a 0a 20 20 2f 2a 20 49 6e 20 74 68 0));.. /* In th
ea50: 65 6f 72 79 2c 20 69 74 20 69 73 20 6f 6e 6c 79 eory, it is only
ea60: 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 72 necessary to wr
ea70: 69 74 65 20 74 68 65 20 32 38 20 62 79 74 65 73 ite the 28 bytes
ea80: 20 74 68 61 74 20 74 68 65 20 0a 20 20 2a 2a 20 that the . **
ea90: 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 63 journal header c
eaa0: 6f 6e 73 75 6d 65 73 20 74 6f 20 74 68 65 20 6a onsumes to the j
eab0: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 ournal file here
eac0: 2e 20 54 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 . Then increment
ead0: 20 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 the . ** Pager
eae0: 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 76 61 72 69 .journalOff vari
eaf0: 61 62 6c 65 20 62 79 20 4a 4f 55 52 4e 41 4c 5f able by JOURNAL_
eb00: 48 44 52 5f 53 5a 20 73 6f 20 74 68 61 74 20 74 HDR_SZ so that t
eb10: 68 65 20 6e 65 78 74 20 0a 20 20 2a 2a 20 72 65 he next . ** re
eb20: 63 6f 72 64 20 69 73 20 77 72 69 74 74 65 6e 20 cord is written
eb30: 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 to the following
eb40: 20 73 65 63 74 6f 72 20 28 6c 65 61 76 69 6e 67 sector (leaving
eb50: 20 61 20 67 61 70 20 69 6e 20 74 68 65 20 66 69 a gap in the fi
eb60: 6c 65 0a 20 20 2a 2a 20 74 68 61 74 20 77 69 6c le. ** that wil
eb70: 6c 20 62 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 l be implicitly
eb80: 66 69 6c 6c 65 64 20 69 6e 20 62 79 20 74 68 65 filled in by the
eb90: 20 4f 53 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 OS).. **. **
eba0: 48 6f 77 65 76 65 72 20 69 74 20 68 61 73 20 62 However it has b
ebb0: 65 65 6e 20 64 69 73 63 6f 76 65 72 65 64 20 74 een discovered t
ebc0: 68 61 74 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74 hat on some syst
ebd0: 65 6d 73 20 74 68 69 73 20 70 61 74 74 65 72 6e ems this pattern
ebe0: 20 63 61 6e 20 0a 20 20 2a 2a 20 62 65 20 73 69 can . ** be si
ebf0: 67 6e 69 66 69 63 61 6e 74 6c 79 20 73 6c 6f 77 gnificantly slow
ec00: 65 72 20 74 68 61 6e 20 63 6f 6e 74 69 67 75 6f er than contiguo
ec10: 75 73 6c 79 20 77 72 69 74 69 6e 67 20 64 61 74 usly writing dat
ec20: 61 20 74 6f 20 74 68 65 20 66 69 6c 65 2c 0a 20 a to the file,.
ec30: 20 2a 2a 20 65 76 65 6e 20 69 66 20 74 68 61 74 ** even if that
ec40: 20 6d 65 61 6e 73 20 65 78 70 6c 69 63 69 74 6c means explicitl
ec50: 79 20 77 72 69 74 69 6e 67 20 64 61 74 61 20 74 y writing data t
ec60: 6f 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 0a o the block of .
ec70: 20 20 2a 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 ** (JOURNAL_HD
ec80: 52 5f 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73 R_SZ - 28) bytes
ec90: 20 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 62 that will not b
eca0: 65 20 75 73 65 64 2e 20 53 6f 20 74 68 61 74 20 e used. So that
ecb0: 69 73 20 77 68 61 74 0a 20 20 2a 2a 20 69 73 20 is what. ** is
ecc0: 64 6f 6e 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a done. . **. **
ecd0: 20 54 68 65 20 6c 6f 6f 70 20 69 73 20 72 65 71 The loop is req
ece0: 75 69 72 65 64 20 68 65 72 65 20 69 6e 20 63 61 uired here in ca
ecf0: 73 65 20 74 68 65 20 73 65 63 74 6f 72 2d 73 69 se the sector-si
ed00: 7a 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 ze is larger tha
ed10: 6e 20 74 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 n the . ** data
ed20: 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 2e 20 base page size.
ed30: 53 69 6e 63 65 20 74 68 65 20 7a 48 65 61 64 65 Since the zHeade
ed40: 72 20 62 75 66 66 65 72 20 69 73 20 6f 6e 6c 79 r buffer is only
ed50: 20 50 61 67 65 72 2e 70 61 67 65 53 69 7a 65 0a Pager.pageSize.
ed60: 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 ** bytes in si
ed70: 7a 65 2c 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e ze, more than on
ed80: 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 e call to sqlite
ed90: 33 4f 73 57 72 69 74 65 28 29 20 6d 61 79 20 62 3OsWrite() may b
eda0: 65 20 72 65 71 75 69 72 65 64 0a 20 20 2a 2a 20 e required. **
edb0: 74 6f 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 to populate the
edc0: 65 6e 74 69 72 65 20 6a 6f 75 72 6e 61 6c 20 68 entire journal h
edd0: 65 61 64 65 72 20 73 65 63 74 6f 72 2e 0a 20 20 eader sector..
ede0: 2a 2f 20 0a 20 20 66 6f 72 28 6e 57 72 69 74 65 */ . for(nWrite
edf0: 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f =0; rc==SQLITE_O
ee00: 4b 26 26 6e 57 72 69 74 65 3c 4a 4f 55 52 4e 41 K&&nWrite<JOURNA
ee10: 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
ee20: 3b 20 6e 57 72 69 74 65 2b 3d 6e 48 65 61 64 65 ; nWrite+=nHeade
ee30: 72 29 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 r){. IOTRACE(
ee40: 28 22 4a 48 44 52 20 25 70 20 25 6c 6c 64 20 25 ("JHDR %p %lld %
ee50: 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 d\n", pPager, pP
ee60: 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 ager->journalHdr
ee70: 2c 20 6e 48 65 61 64 65 72 29 29 0a 20 20 20 20 , nHeader)).
ee80: 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
ee90: 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ite(pPager->jfd,
eea0: 20 7a 48 65 61 64 65 72 2c 20 6e 48 65 61 64 65 zHeader, nHeade
eeb0: 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e r, pPager->journ
eec0: 61 6c 4f 66 66 29 3b 0a 20 20 20 20 61 73 73 65 alOff);. asse
eed0: 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 rt( pPager->jour
eee0: 6e 61 6c 48 64 72 20 3c 3d 20 70 50 61 67 65 72 nalHdr <= pPager
eef0: 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 3b 0a ->journalOff );.
ef00: 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
ef10: 6e 61 6c 4f 66 66 20 2b 3d 20 6e 48 65 61 64 65 nalOff += nHeade
ef20: 72 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e r;. }.. return
ef30: 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
ef40: 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d e journal file m
ef50: 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e ust be open when
ef60: 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2e this is called.
ef70: 20 41 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 A journal heade
ef80: 72 20 66 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 4e r file.** (JOURN
ef90: 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 29 AL_HDR_SZ bytes)
efa0: 20 69 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 is read from th
efb0: 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 e current locati
efc0: 6f 6e 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 on in the journa
efd0: 6c 0a 2a 2a 20 66 69 6c 65 2e 20 54 68 65 20 63 l.** file. The c
efe0: 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 urrent location
eff0: 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 in the journal f
f000: 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79 0a ile is given by.
f010: 2a 2a 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e ** pPager->journ
f020: 61 6c 4f 66 66 2e 20 53 65 65 20 63 6f 6d 6d 65 alOff. See comme
f030: 6e 74 73 20 61 62 6f 76 65 20 66 75 6e 63 74 69 nts above functi
f040: 6f 6e 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 on writeJournalH
f050: 64 72 28 29 20 66 6f 72 0a 2a 2a 20 61 20 64 65 dr() for.** a de
f060: 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 scription of the
f070: 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
f080: 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 format..**.** If
f090: 20 74 68 65 20 68 65 61 64 65 72 20 69 73 20 72 the header is r
f0a0: 65 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 ead successfully
f0b0: 2c 20 2a 70 4e 52 65 63 20 69 73 20 73 65 74 20 , *pNRec is set
f0c0: 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 to the number of
f0d0: 0a 2a 2a 20 70 61 67 65 20 72 65 63 6f 72 64 73 .** page records
f0e0: 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 following this
f0f0: 68 65 61 64 65 72 20 61 6e 64 20 2a 70 44 62 53 header and *pDbS
f100: 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 ize is set to th
f110: 65 20 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a e size of the.**
f120: 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 database before
f130: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
f140: 20 62 65 67 61 6e 2c 20 69 6e 20 70 61 67 65 73 began, in pages
f150: 2e 20 41 6c 73 6f 2c 20 70 50 61 67 65 72 2d 3e . Also, pPager->
f160: 63 6b 73 75 6d 49 6e 69 74 0a 2a 2a 20 69 73 20 cksumInit.** is
f170: 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 set to the value
f180: 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a read from the j
f190: 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 53 ournal header. S
f1a0: 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
f1b0: 72 6e 65 64 0a 2a 2a 20 69 6e 20 74 68 69 73 20 rned.** in this
f1c0: 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 case..**.** If t
f1d0: 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 he journal heade
f1e0: 72 20 66 69 6c 65 20 61 70 70 65 61 72 73 20 74 r file appears t
f1f0: 6f 20 62 65 20 63 6f 72 72 75 70 74 65 64 2c 20 o be corrupted,
f200: 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 0a 2a SQLITE_DONE is.*
f210: 2a 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a * returned and *
f220: 70 4e 52 65 63 20 61 6e 64 20 2a 50 44 62 53 69 pNRec and *PDbSi
f230: 7a 65 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 ze are undefined
f240: 2e 20 20 49 66 20 4a 4f 55 52 4e 41 4c 5f 48 44 . If JOURNAL_HD
f250: 52 5f 53 5a 20 62 79 74 65 73 0a 2a 2a 20 63 61 R_SZ bytes.** ca
f260: 6e 6e 6f 74 20 62 65 20 72 65 61 64 20 66 72 6f nnot be read fro
f270: 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 m the journal fi
f280: 6c 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 le an error code
f290: 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f is returned..*/
f2a0: 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 .static int read
f2b0: 4a 6f 75 72 6e 61 6c 48 64 72 28 0a 20 20 50 61 JournalHdr(. Pa
f2c0: 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 ger *pPager,
f2d0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
f2e0: 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 ger object */.
f2f0: 69 6e 74 20 69 73 48 6f 74 2c 0a 20 20 69 36 34 int isHot,. i64
f300: 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 2c 20 20 20 journalSize,
f310: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
f320: 65 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 6a 6f e of the open jo
f330: 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 urnal file in by
f340: 74 65 73 20 2a 2f 0a 20 20 75 33 32 20 2a 70 4e tes */. u32 *pN
f350: 52 65 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 Rec,
f360: 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 56 61 /* OUT: Va
f370: 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 lue read from th
f380: 65 20 6e 52 65 63 20 66 69 65 6c 64 20 2a 2f 0a e nRec field */.
f390: 20 20 75 33 32 20 2a 70 44 62 53 69 7a 65 20 20 u32 *pDbSize
f3a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
f3b0: 2a 20 4f 55 54 3a 20 56 61 6c 75 65 20 6f 66 20 * OUT: Value of
f3c0: 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 original databas
f3d0: 65 20 73 69 7a 65 20 66 69 65 6c 64 20 2a 2f 0a e size field */.
f3e0: 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
f3f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f400: 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
f410: 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
f420: 68 61 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 har aMagic[8];
f430: 20 20 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74 /* A buffer t
f440: 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 o hold the magic
f450: 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 header */. i64
f460: 20 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20 20 iHdrOff;
f470: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 /* Off
f480: 73 65 74 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 set of journal h
f490: 65 61 64 65 72 20 62 65 69 6e 67 20 72 65 61 64 eader being read
f4a0: 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 */.. assert( i
f4b0: 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
f4c0: 64 29 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f d) ); /* Jo
f4d0: 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 urnal file must
f4e0: 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 2f be open. */.. /
f4f0: 2a 20 41 64 76 61 6e 63 65 20 50 61 67 65 72 2e * Advance Pager.
f500: 6a 6f 75 72 6e 61 6c 4f 66 66 20 74 6f 20 74 68 journalOff to th
f510: 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6e e start of the n
f520: 65 78 74 20 73 65 63 74 6f 72 2e 20 49 66 20 74 ext sector. If t
f530: 68 65 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 he. ** journal
f540: 66 69 6c 65 20 69 73 20 74 6f 6f 20 73 6d 61 6c file is too smal
f550: 6c 20 66 6f 72 20 74 68 65 72 65 20 74 6f 20 62 l for there to b
f560: 65 20 61 20 68 65 61 64 65 72 20 73 74 6f 72 65 e a header store
f570: 64 20 61 74 20 74 68 69 73 0a 20 20 2a 2a 20 70 d at this. ** p
f580: 6f 69 6e 74 2c 20 72 65 74 75 72 6e 20 53 51 4c oint, return SQL
f590: 49 54 45 5f 44 4f 4e 45 2e 0a 20 20 2a 2f 0a 20 ITE_DONE.. */.
f5a0: 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
f5b0: 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 Off = journalHdr
f5c0: 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a Offset(pPager);.
f5d0: 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f if( pPager->jo
f5e0: 75 72 6e 61 6c 4f 66 66 2b 4a 4f 55 52 4e 41 4c urnalOff+JOURNAL
f5f0: 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 _HDR_SZ(pPager)
f600: 3e 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 20 29 7b > journalSize ){
f610: 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
f620: 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 TE_DONE;. }. i
f630: 48 64 72 4f 66 66 20 3d 20 70 50 61 67 65 72 2d HdrOff = pPager-
f640: 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 >journalOff;..
f650: 2f 2a 20 52 65 61 64 20 69 6e 20 74 68 65 20 66 /* Read in the f
f660: 69 72 73 74 20 38 20 62 79 74 65 73 20 6f 66 20 irst 8 bytes of
f670: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 the journal head
f680: 65 72 2e 20 49 66 20 74 68 65 79 20 64 6f 20 6e er. If they do n
f690: 6f 74 20 6d 61 74 63 68 0a 20 20 2a 2a 20 74 68 ot match. ** th
f6a0: 65 20 20 6d 61 67 69 63 20 73 74 72 69 6e 67 20 e magic string
f6b0: 66 6f 75 6e 64 20 61 74 20 74 68 65 20 73 74 61 found at the sta
f6c0: 72 74 20 6f 66 20 65 61 63 68 20 6a 6f 75 72 6e rt of each journ
f6d0: 61 6c 20 68 65 61 64 65 72 2c 20 72 65 74 75 72 al header, retur
f6e0: 6e 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 4f n. ** SQLITE_DO
f6f0: 4e 45 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 NE. If an IO err
f700: 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 or occurs, retur
f710: 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e n an error code.
f720: 20 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 2a 2a Otherwise,. **
f730: 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 proceed.. */.
f740: 20 69 66 28 20 69 73 48 6f 74 20 7c 7c 20 69 48 if( isHot || iH
f750: 64 72 4f 66 66 21 3d 70 50 61 67 65 72 2d 3e 6a drOff!=pPager->j
f760: 6f 75 72 6e 61 6c 48 64 72 20 29 7b 0a 20 20 20 ournalHdr ){.
f770: 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 rc = sqlite3OsR
f780: 65 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ead(pPager->jfd,
f790: 20 61 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 aMagic, sizeof(
f7a0: 61 4d 61 67 69 63 29 2c 20 69 48 64 72 4f 66 66 aMagic), iHdrOff
f7b0: 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b );. if( rc ){
f7c0: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
f7d0: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
f7e0: 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 memcmp(aMagic, a
f7f0: 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 JournalMagic, si
f800: 7a 65 6f 66 28 61 4d 61 67 69 63 29 29 21 3d 30 zeof(aMagic))!=0
f810: 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
f820: 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 SQLITE_DONE;.
f830: 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 }. }.. /* Re
f840: 61 64 20 74 68 65 20 66 69 72 73 74 20 74 68 72 ad the first thr
f850: 65 65 20 33 32 2d 62 69 74 20 66 69 65 6c 64 73 ee 32-bit fields
f860: 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
f870: 68 65 61 64 65 72 3a 20 54 68 65 20 6e 52 65 63 header: The nRec
f880: 0a 20 20 2a 2a 20 66 69 65 6c 64 2c 20 74 68 65 . ** field, the
f890: 20 63 68 65 63 6b 73 75 6d 2d 69 6e 69 74 69 61 checksum-initia
f8a0: 6c 69 7a 65 72 20 61 6e 64 20 74 68 65 20 64 61 lizer and the da
f8b0: 74 61 62 61 73 65 20 73 69 7a 65 20 61 74 20 74 tabase size at t
f8c0: 68 65 20 73 74 61 72 74 0a 20 20 2a 2a 20 6f 66 he start. ** of
f8d0: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
f8e0: 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f . Return an erro
f8f0: 72 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69 r code if anythi
f900: 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 20 ng goes wrong..
f910: 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 */. if( SQLITE
f920: 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 _OK!=(rc = read3
f930: 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 2bits(pPager->jf
f940: 64 2c 20 69 48 64 72 4f 66 66 2b 38 2c 20 70 4e d, iHdrOff+8, pN
f950: 52 65 63 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 Rec)). || SQLI
f960: 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 TE_OK!=(rc = rea
f970: 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e d32bits(pPager->
f980: 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 31 32 2c jfd, iHdrOff+12,
f990: 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 &pPager->cksumI
f9a0: 6e 69 74 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 nit)). || SQLI
f9b0: 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 TE_OK!=(rc = rea
f9c0: 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e d32bits(pPager->
f9d0: 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 31 36 2c jfd, iHdrOff+16,
f9e0: 20 70 44 62 53 69 7a 65 29 29 0a 20 20 29 7b 0a pDbSize)). ){.
f9f0: 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
fa00: 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 }.. if( pPager
fa10: 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 ->journalOff==0
fa20: 29 7b 0a 20 20 20 20 75 33 32 20 69 50 61 67 65 ){. u32 iPage
fa30: 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 Size;
fa40: 20 20 20 20 2f 2a 20 50 61 67 65 2d 73 69 7a 65 /* Page-size
fa50: 20 66 69 65 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 field of journa
fa60: 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 l header */.
fa70: 75 33 32 20 69 53 65 63 74 6f 72 53 69 7a 65 3b u32 iSectorSize;
fa80: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
fa90: 53 65 63 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c Sector-size fiel
faa0: 64 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 d of journal hea
fab0: 64 65 72 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 52 der */.. /* R
fac0: 65 61 64 20 74 68 65 20 70 61 67 65 2d 73 69 7a ead the page-siz
fad0: 65 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a e and sector-siz
fae0: 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
faf0: 20 66 69 65 6c 64 73 2e 20 2a 2f 0a 20 20 20 20 fields. */.
fb00: 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 if( SQLITE_OK!=(
fb10: 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
fb20: 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 pPager->jfd, iHd
fb30: 72 4f 66 66 2b 32 30 2c 20 26 69 53 65 63 74 6f rOff+20, &iSecto
fb40: 72 53 69 7a 65 29 29 0a 20 20 20 20 20 7c 7c 20 rSize)). ||
fb50: 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
fb60: 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67 read32bits(pPag
fb70: 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 er->jfd, iHdrOff
fb80: 2b 32 34 2c 20 26 69 50 61 67 65 53 69 7a 65 29 +24, &iPageSize)
fb90: 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 ). ){. r
fba0: 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
fbb0: 0a 20 20 20 20 2f 2a 20 56 65 72 73 69 6f 6e 73 . /* Versions
fbc0: 20 6f 66 20 53 51 4c 69 74 65 20 70 72 69 6f 72 of SQLite prior
fbd0: 20 74 6f 20 33 2e 35 2e 38 20 73 65 74 20 74 68 to 3.5.8 set th
fbe0: 65 20 70 61 67 65 2d 73 69 7a 65 20 66 69 65 6c e page-size fiel
fbf0: 64 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 d of the. **
fc00: 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 74 journal header t
fc10: 6f 20 7a 65 72 6f 2e 20 49 6e 20 74 68 69 73 20 o zero. In this
fc20: 63 61 73 65 2c 20 61 73 73 75 6d 65 20 74 68 61 case, assume tha
fc30: 74 20 74 68 65 20 50 61 67 65 72 2e 70 61 67 65 t the Pager.page
fc40: 53 69 7a 65 0a 20 20 20 20 2a 2a 20 76 61 72 69 Size. ** vari
fc50: 61 62 6c 65 20 69 73 20 61 6c 72 65 61 64 79 20 able is already
fc60: 73 65 74 20 74 6f 20 74 68 65 20 63 6f 72 72 65 set to the corre
fc70: 63 74 20 70 61 67 65 20 73 69 7a 65 2e 0a 20 20 ct page size..
fc80: 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 50 61 */. if( iPa
fc90: 67 65 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 geSize==0 ){.
fca0: 20 20 20 69 50 61 67 65 53 69 7a 65 20 3d 20 70 iPageSize = p
fcb0: 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b Pager->pageSize;
fcc0: 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 . }.. /* C
fcd0: 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 76 61 heck that the va
fce0: 6c 75 65 73 20 72 65 61 64 20 66 72 6f 6d 20 74 lues read from t
fcf0: 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 he page-size and
fd00: 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 66 69 65 sector-size fie
fd10: 6c 64 73 0a 20 20 20 20 2a 2a 20 61 72 65 20 77 lds. ** are w
fd20: 69 74 68 69 6e 20 72 61 6e 67 65 2e 20 54 6f 20 ithin range. To
fd30: 62 65 20 27 69 6e 20 72 61 6e 67 65 27 2c 20 62 be 'in range', b
fd40: 6f 74 68 20 76 61 6c 75 65 73 20 6e 65 65 64 20 oth values need
fd50: 74 6f 20 62 65 20 61 20 70 6f 77 65 72 0a 20 20 to be a power.
fd60: 20 20 2a 2a 20 6f 66 20 74 77 6f 20 67 72 65 61 ** of two grea
fd70: 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ter than or equa
fd80: 6c 20 74 6f 20 35 31 32 20 6f 72 20 33 32 2c 20 l to 512 or 32,
fd90: 61 6e 64 20 6e 6f 74 20 67 72 65 61 74 65 72 20 and not greater
fda0: 74 68 61 6e 20 74 68 65 69 72 20 0a 20 20 20 20 than their .
fdb0: 2a 2a 20 72 65 73 70 65 63 74 69 76 65 20 63 6f ** respective co
fdc0: 6d 70 69 6c 65 20 74 69 6d 65 20 6d 61 78 69 6d mpile time maxim
fdd0: 75 6d 20 6c 69 6d 69 74 73 2e 0a 20 20 20 20 2a um limits.. *
fde0: 2f 0a 20 20 20 20 69 66 28 20 69 50 61 67 65 53 /. if( iPageS
fdf0: 69 7a 65 3c 35 31 32 20 20 20 20 20 20 20 20 20 ize<512
fe00: 20 20 20 20 20 20 20 20 20 7c 7c 20 69 53 65 63 || iSec
fe10: 74 6f 72 53 69 7a 65 3c 33 32 0a 20 20 20 20 20 torSize<32.
fe20: 7c 7c 20 69 50 61 67 65 53 69 7a 65 3e 53 51 4c || iPageSize>SQL
fe30: 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a ITE_MAX_PAGE_SIZ
fe40: 45 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 E || iSectorSize
fe50: 3e 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 >MAX_SECTOR_SIZE
fe60: 0a 20 20 20 20 20 7c 7c 20 28 28 69 50 61 67 65 . || ((iPage
fe70: 53 69 7a 65 2d 31 29 26 69 50 61 67 65 53 69 7a Size-1)&iPageSiz
fe80: 65 29 21 3d 30 20 20 20 7c 7c 20 28 28 69 53 65 e)!=0 || ((iSe
fe90: 63 74 6f 72 53 69 7a 65 2d 31 29 26 69 53 65 63 ctorSize-1)&iSec
fea0: 74 6f 72 53 69 7a 65 29 21 3d 30 20 0a 20 20 20 torSize)!=0 .
feb0: 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 ){. /* If
fec0: 74 68 65 20 65 69 74 68 65 72 20 74 68 65 20 70 the either the p
fed0: 61 67 65 2d 73 69 7a 65 20 6f 72 20 73 65 63 74 age-size or sect
fee0: 6f 72 2d 73 69 7a 65 20 69 6e 20 74 68 65 20 6a or-size in the j
fef0: 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 ournal-header is
ff00: 20 0a 20 20 20 20 20 20 2a 2a 20 69 6e 76 61 6c . ** inval
ff10: 69 64 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f id, then the pro
ff20: 63 65 73 73 20 74 68 61 74 20 77 72 6f 74 65 20 cess that wrote
ff30: 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 the journal-head
ff40: 65 72 20 6d 75 73 74 20 68 61 76 65 20 0a 20 20 er must have .
ff50: 20 20 20 20 2a 2a 20 63 72 61 73 68 65 64 20 62 ** crashed b
ff60: 65 66 6f 72 65 20 74 68 65 20 68 65 61 64 65 72 efore the header
ff70: 20 77 61 73 20 73 79 6e 63 65 64 2e 20 49 6e 20 was synced. In
ff80: 74 68 69 73 20 63 61 73 65 20 73 74 6f 70 20 72 this case stop r
ff90: 65 61 64 69 6e 67 20 0a 20 20 20 20 20 20 2a 2a eading . **
ffa0: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
ffb0: 65 20 68 65 72 65 2e 0a 20 20 20 20 20 20 2a 2f e here.. */
ffc0: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
ffd0: 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d LITE_DONE;. }
ffe0: 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 .. /* Update
fff0: 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 74 6f the page-size to
10000 20 6d 61 74 63 68 20 74 68 65 20 76 61 6c 75 65 match the value
10010 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a read from the j
10020 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2a 20 ournal. . **
10030 55 73 65 20 61 20 74 65 73 74 63 61 73 65 28 29 Use a testcase()
10040 20 6d 61 63 72 6f 20 74 6f 20 6d 61 6b 65 20 73 macro to make s
10050 75 72 65 20 74 68 61 74 20 6d 61 6c 6c 6f 63 20 ure that malloc
10060 66 61 69 6c 75 72 65 20 77 69 74 68 69 6e 20 0a failure within .
10070 20 20 20 20 2a 2a 20 50 61 67 65 72 53 65 74 50 ** PagerSetP
10080 61 67 65 73 69 7a 65 28 29 20 69 73 20 74 65 73 agesize() is tes
10090 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ted.. */.
100a0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
100b0 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 50 61 rSetPagesize(pPa
100c0 67 65 72 2c 20 26 69 50 61 67 65 53 69 7a 65 2c ger, &iPageSize,
100d0 20 2d 31 29 3b 0a 20 20 20 20 74 65 73 74 63 61 -1);. testca
100e0 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f se( rc!=SQLITE_O
100f0 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 55 70 64 K );.. /* Upd
10100 61 74 65 20 74 68 65 20 61 73 73 75 6d 65 64 20 ate the assumed
10110 73 65 63 74 6f 72 2d 73 69 7a 65 20 74 6f 20 6d sector-size to m
10120 61 74 63 68 20 74 68 65 20 76 61 6c 75 65 20 75 atch the value u
10130 73 65 64 20 62 79 20 0a 20 20 20 20 2a 2a 20 74 sed by . ** t
10140 68 65 20 70 72 6f 63 65 73 73 20 74 68 61 74 20 he process that
10150 63 72 65 61 74 65 64 20 74 68 69 73 20 6a 6f 75 created this jou
10160 72 6e 61 6c 2e 20 49 66 20 74 68 69 73 20 6a 6f rnal. If this jo
10170 75 72 6e 61 6c 20 77 61 73 0a 20 20 20 20 2a 2a urnal was. **
10180 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 created by a pr
10190 6f 63 65 73 73 20 6f 74 68 65 72 20 74 68 61 6e ocess other than
101a0 20 74 68 69 73 20 6f 6e 65 2c 20 74 68 65 6e 20 this one, then
101b0 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 20 20 20 this routine.
101c0 20 2a 2a 20 69 73 20 62 65 69 6e 67 20 63 61 6c ** is being cal
101d0 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 led from within
101e0 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 pager_playback()
101f0 2e 20 54 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 . The local valu
10200 65 0a 20 20 20 20 2a 2a 20 6f 66 20 50 61 67 65 e. ** of Page
10210 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 69 73 20 r.sectorSize is
10220 72 65 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 restored at the
10230 65 6e 64 20 6f 66 20 74 68 61 74 20 72 6f 75 74 end of that rout
10240 69 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ine.. */.
10250 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
10260 7a 65 20 3d 20 69 53 65 63 74 6f 72 53 69 7a 65 ze = iSectorSize
10270 3b 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 72 2d ;. }.. pPager-
10280 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 4a >journalOff += J
10290 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
102a0 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 ager);. return
102b0 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 rc;.}.../*.** Wr
102c0 69 74 65 20 74 68 65 20 73 75 70 70 6c 69 65 64 ite the supplied
102d0 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
102e0 6e 61 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f name into the jo
102f0 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 urnal file for p
10300 61 67 65 72 0a 2a 2a 20 70 50 61 67 65 72 20 61 ager.** pPager a
10310 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f t the current lo
10320 63 61 74 69 6f 6e 2e 20 54 68 65 20 6d 61 73 74 cation. The mast
10330 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 er journal name
10340 6d 75 73 74 20 62 65 20 74 68 65 20 6c 61 73 74 must be the last
10350 0a 2a 2a 20 74 68 69 6e 67 20 77 72 69 74 74 65 .** thing writte
10360 6e 20 74 6f 20 61 20 6a 6f 75 72 6e 61 6c 20 66 n to a journal f
10370 69 6c 65 2e 20 49 66 20 74 68 65 20 70 61 67 65 ile. If the page
10380 72 20 69 73 20 69 6e 20 66 75 6c 6c 2d 73 79 6e r is in full-syn
10390 63 20 6d 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 6a c mode, the.** j
103a0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 ournal file desc
103b0 72 69 70 74 6f 72 20 69 73 20 61 64 76 61 6e 63 riptor is advanc
103c0 65 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73 ed to the next s
103d0 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 20 62 ector boundary b
103e0 65 66 6f 72 65 0a 2a 2a 20 61 6e 79 74 68 69 6e efore.** anythin
103f0 67 20 69 73 20 77 72 69 74 74 65 6e 2e 20 54 68 g is written. Th
10400 65 20 66 6f 72 6d 61 74 20 69 73 3a 0a 2a 2a 0a e format is:.**.
10410 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 73 3a 20 ** + 4 bytes:
10420 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 2e 0a 2a PAGER_MJ_PGNO..*
10430 2a 20 20 20 2b 20 4e 20 62 79 74 65 73 3a 20 4d * + N bytes: M
10440 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
10450 6c 65 6e 61 6d 65 20 69 6e 20 75 74 66 2d 38 2e lename in utf-8.
10460 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 73 3a .** + 4 bytes:
10470 20 4e 20 28 6c 65 6e 67 74 68 20 6f 66 20 6d 61 N (length of ma
10480 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster journal nam
10490 65 20 69 6e 20 62 79 74 65 73 2c 20 6e 6f 20 6e e in bytes, no n
104a0 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 29 2e 0a ul-terminator)..
104b0 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 73 3a 20 ** + 4 bytes:
104c0 4d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e Master journal n
104d0 61 6d 65 20 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a ame checksum..**
104e0 20 20 20 2b 20 38 20 62 79 74 65 73 3a 20 61 4a + 8 bytes: aJ
104f0 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a 2a ournalMagic[]..*
10500 2a 0a 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 *.** The master
10510 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 63 68 65 journal page che
10520 63 6b 73 75 6d 20 69 73 20 74 68 65 20 73 75 6d cksum is the sum
10530 20 6f 66 20 74 68 65 20 62 79 74 65 73 20 69 6e of the bytes in
10540 20 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 6a the master.** j
10550 6f 75 72 6e 61 6c 20 6e 61 6d 65 2c 20 77 68 65 ournal name, whe
10560 72 65 20 65 61 63 68 20 62 79 74 65 20 69 73 20 re each byte is
10570 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61 interpreted as a
10580 20 73 69 67 6e 65 64 20 38 2d 62 69 74 20 69 6e signed 8-bit in
10590 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 teger..**.** If
105a0 7a 4d 61 73 74 65 72 20 69 73 20 61 20 4e 55 4c zMaster is a NUL
105b0 4c 20 70 6f 69 6e 74 65 72 20 28 6f 63 63 75 72 L pointer (occur
105c0 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 s for a single d
105d0 61 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 atabase transact
105e0 69 6f 6e 29 2c 20 0a 2a 2a 20 74 68 69 73 20 63 ion), .** this c
105f0 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a all is a no-op..
10600 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 */.static int wr
10610 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c iteMasterJournal
10620 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
10630 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 const char *zMas
10640 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 ter){. int rc;
10650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10660 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
10670 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 rn code */. int
10680 20 6e 4d 61 73 74 65 72 3b 20 20 20 20 20 20 20 nMaster;
10690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
106a0 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69 6e Length of strin
106b0 67 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 20 20 69 g zMaster */. i
106c0 36 34 20 69 48 64 72 4f 66 66 3b 20 20 20 20 20 64 iHdrOff;
106d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106e0 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65 61 /* Offset of hea
106f0 64 65 72 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 der in journal f
10700 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a 72 6e ile */. i64 jrn
10710 6c 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 lSize;
10720 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
10730 65 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c e of journal fil
10740 65 20 6f 6e 20 64 69 73 6b 20 2a 2f 0a 20 20 75 e on disk */. u
10750 33 32 20 63 6b 73 75 6d 20 3d 20 30 3b 20 20 20 32 cksum = 0;
10760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10770 2f 2a 20 43 68 65 63 6b 73 75 6d 20 6f 66 20 73 /* Checksum of s
10780 74 72 69 6e 67 20 7a 4d 61 73 74 65 72 20 2a 2f tring zMaster */
10790 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
107a0 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 3d 3d 30 er->setMaster==0
107b0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 );. assert( !p
107c0 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 agerUseWal(pPage
107d0 72 29 20 29 3b 0a 0a 20 20 69 66 28 20 21 7a 4d r) );.. if( !zM
107e0 61 73 74 65 72 20 0a 20 20 20 7c 7c 20 70 50 61 aster . || pPa
107f0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
10800 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
10810 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 7c ODE_MEMORY . |
10820 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 | pPager->journa
10830 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
10840 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 RNALMODE_OFF .
10850 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
10860 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 LITE_OK;. }. p
10870 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 Pager->setMaster
10880 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 = 1;. assert(
10890 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
108a0 66 64 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 fd) );. assert(
108b0 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
108c0 48 64 72 20 3c 3d 20 70 50 61 67 65 72 2d 3e 6a Hdr <= pPager->j
108d0 6f 75 72 6e 61 6c 4f 66 66 20 29 3b 0a 0a 20 20 ournalOff );..
108e0 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65 /* Calculate the
108f0 20 6c 65 6e 67 74 68 20 69 6e 20 62 79 74 65 73 length in bytes
10900 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b 73 75 and the checksu
10910 6d 20 6f 66 20 7a 4d 61 73 74 65 72 20 2a 2f 0a m of zMaster */.
10920 20 20 66 6f 72 28 6e 4d 61 73 74 65 72 3d 30 3b for(nMaster=0;
10930 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 74 65 72 zMaster[nMaster
10940 5d 3b 20 6e 4d 61 73 74 65 72 2b 2b 29 7b 0a 20 ]; nMaster++){.
10950 20 20 20 63 6b 73 75 6d 20 2b 3d 20 7a 4d 61 73 cksum += zMas
10960 74 65 72 5b 6e 4d 61 73 74 65 72 5d 3b 0a 20 20 ter[nMaster];.
10970 7d 0a 0a 20 20 2f 2a 20 49 66 20 69 6e 20 66 75 }.. /* If in fu
10980 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 61 64 ll-sync mode, ad
10990 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78 vance to the nex
109a0 74 20 64 69 73 6b 20 73 65 63 74 6f 72 20 62 65 t disk sector be
109b0 66 6f 72 65 20 77 72 69 74 69 6e 67 0a 20 20 2a fore writing. *
109c0 2a 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 * the master jou
109d0 72 6e 61 6c 20 6e 61 6d 65 2e 20 54 68 69 73 20 rnal name. This
109e0 69 73 20 69 6e 20 63 61 73 65 20 74 68 65 20 70 is in case the p
109f0 72 65 76 69 6f 75 73 20 70 61 67 65 20 77 72 69 revious page wri
10a00 74 74 65 6e 20 74 6f 0a 20 20 2a 2a 20 74 68 65 tten to. ** the
10a10 20 6a 6f 75 72 6e 61 6c 20 68 61 73 20 61 6c 72 journal has alr
10a20 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64 eady been synced
10a30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 .. */. if( pPa
10a40 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 29 7b ger->fullSync ){
10a50 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 . pPager->jou
10a60 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 rnalOff = journa
10a70 6c 48 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 lHdrOffset(pPage
10a80 72 29 3b 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 r);. }. iHdrOf
10a90 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 f = pPager->jour
10aa0 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 57 72 nalOff;.. /* Wr
10ab0 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a ite the master j
10ac0 6f 75 72 6e 61 6c 20 64 61 74 61 20 74 6f 20 74 ournal data to t
10ad0 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f he end of the jo
10ae0 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 0a 20 urnal file. If.
10af0 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 ** an error occ
10b00 75 72 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 urs, return the
10b10 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 error code to th
10b20 65 20 63 61 6c 6c 65 72 2e 0a 20 20 2a 2f 0a 20 e caller.. */.
10b30 20 69 66 28 20 28 30 20 21 3d 20 28 72 63 20 3d if( (0 != (rc =
10b40 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 write32bits(pPa
10b50 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 ger->jfd, iHdrOf
10b60 66 2c 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f f, PAGER_MJ_PGNO
10b70 28 70 50 61 67 65 72 29 29 29 29 0a 20 20 20 7c (pPager)))). |
10b80 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 | (0 != (rc = sq
10b90 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 lite3OsWrite(pPa
10ba0 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 ger->jfd, zMaste
10bb0 72 2c 20 6e 4d 61 73 74 65 72 2c 20 69 48 64 72 r, nMaster, iHdr
10bc0 4f 66 66 2b 34 29 29 29 0a 20 20 20 7c 7c 20 28 Off+4))). || (
10bd0 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65 0 != (rc = write
10be0 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
10bf0 66 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d fd, iHdrOff+4+nM
10c00 61 73 74 65 72 2c 20 6e 4d 61 73 74 65 72 29 29 aster, nMaster))
10c10 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 ). || (0 != (r
10c20 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 c = write32bits(
10c30 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 pPager->jfd, iHd
10c40 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 34 rOff+4+nMaster+4
10c50 2c 20 63 6b 73 75 6d 29 29 29 0a 20 20 20 7c 7c , cksum))). ||
10c60 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c (0 != (rc = sql
10c70 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 ite3OsWrite(pPag
10c80 65 72 2d 3e 6a 66 64 2c 20 61 4a 6f 75 72 6e 61 er->jfd, aJourna
10c90 6c 4d 61 67 69 63 2c 20 38 2c 20 69 48 64 72 4f lMagic, 8, iHdrO
10ca0 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 38 29 29 ff+4+nMaster+8))
10cb0 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ). ){. retur
10cc0 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 61 67 n rc;. }. pPag
10cd0 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b er->journalOff +
10ce0 3d 20 28 6e 4d 61 73 74 65 72 2b 32 30 29 3b 0a = (nMaster+20);.
10cf0 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 . /* If the pag
10d00 65 72 20 69 73 20 69 6e 20 70 65 72 69 73 74 65 er is in periste
10d10 6e 74 2d 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2c nt-journal mode,
10d20 20 74 68 65 6e 20 74 68 65 20 70 68 79 73 69 63 then the physic
10d30 61 6c 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c al . ** journal
10d40 2d 66 69 6c 65 20 6d 61 79 20 65 78 74 65 6e 64 -file may extend
10d50 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
10d60 20 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 the master-jour
10d70 6e 61 6c 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e nal name. ** an
10d80 64 20 38 20 62 79 74 65 73 20 6f 66 20 6d 61 67 d 8 bytes of mag
10d90 69 63 20 64 61 74 61 20 6a 75 73 74 20 77 72 69 ic data just wri
10da0 74 74 65 6e 20 74 6f 20 74 68 65 20 66 69 6c 65 tten to the file
10db0 2e 20 54 68 69 73 20 69 73 20 0a 20 20 2a 2a 20 . This is . **
10dc0 64 61 6e 67 65 72 6f 75 73 20 62 65 63 61 75 73 dangerous becaus
10dd0 65 20 74 68 65 20 63 6f 64 65 20 74 6f 20 72 6f e the code to ro
10de0 6c 6c 62 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 llback a hot-jou
10df0 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 77 rnal file. ** w
10e00 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 ill not be able
10e10 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 61 73 74 to find the mast
10e20 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 er-journal name
10e30 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 0a 20 20 to determine .
10e40 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ** whether or no
10e50 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 t the journal is
10e60 20 68 6f 74 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a hot. . **. **
10e70 20 45 61 73 69 65 73 74 20 74 68 69 6e 67 20 74 Easiest thing t
10e80 6f 20 64 6f 20 69 6e 20 74 68 69 73 20 73 63 65 o do in this sce
10e90 6e 61 72 69 6f 20 69 73 20 74 6f 20 74 72 75 6e nario is to trun
10ea0 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c cate the journal
10eb0 20 0a 20 20 2a 2a 20 66 69 6c 65 20 74 6f 20 74 . ** file to t
10ec0 68 65 20 72 65 71 75 69 72 65 64 20 73 69 7a 65 he required size
10ed0 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28 20 53 51 .. */ . if( SQ
10ee0 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 LITE_OK==(rc = s
10ef0 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 qlite3OsFileSize
10f00 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 6a (pPager->jfd, &j
10f10 72 6e 6c 53 69 7a 65 29 29 0a 20 20 20 26 26 20 rnlSize)). &&
10f20 6a 72 6e 6c 53 69 7a 65 3e 70 50 61 67 65 72 2d jrnlSize>pPager-
10f30 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a 20 20 29 7b >journalOff. ){
10f40 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
10f50 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 3OsTruncate(pPag
10f60 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d er->jfd, pPager-
10f70 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20 >journalOff);.
10f80 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
10f90 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 20 70 ../*.** Find a p
10fa0 61 67 65 20 69 6e 20 74 68 65 20 68 61 73 68 20 age in the hash
10fb0 74 61 62 6c 65 20 67 69 76 65 6e 20 69 74 73 20 table given its
10fc0 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 52 65 74 page number. Ret
10fd0 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 urn.** a pointer
10fe0 20 74 6f 20 74 68 65 20 70 61 67 65 20 6f 72 20 to the page or
10ff0 4e 55 4c 4c 20 69 66 20 74 68 65 20 72 65 71 75 NULL if the requ
11000 65 73 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f ested page is no
11010 74 20 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e t .** already in
11020 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 memory..*/.stat
11030 69 63 20 50 67 48 64 72 20 2a 70 61 67 65 72 5f ic PgHdr *pager_
11040 6c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 lookup(Pager *pP
11050 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 ager, Pgno pgno)
11060 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 20 20 20 {. PgHdr *p;
11070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11080 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
11090 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 value */.. /* I
110a0 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
110b0 65 20 66 6f 72 20 61 20 63 61 6c 6c 20 74 6f 20 e for a call to
110c0 50 63 61 63 68 65 46 65 74 63 68 28 29 20 77 69 PcacheFetch() wi
110d0 74 68 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 30 th createFlag==0
110e0 20 74 6f 0a 20 20 2a 2a 20 66 61 69 6c 2c 20 73 to. ** fail, s
110f0 69 6e 63 65 20 6e 6f 20 61 74 74 65 6d 70 74 20 ince no attempt
11100 74 6f 20 61 6c 6c 6f 63 61 74 65 20 64 79 6e 61 to allocate dyna
11110 6d 69 63 20 6d 65 6d 6f 72 79 20 77 69 6c 6c 20 mic memory will
11120 62 65 20 6d 61 64 65 2e 0a 20 20 2a 2f 0a 20 20 be made.. */.
11130 28 76 6f 69 64 29 73 71 6c 69 74 65 33 50 63 61 (void)sqlite3Pca
11140 63 68 65 46 65 74 63 68 28 70 50 61 67 65 72 2d cheFetch(pPager-
11150 3e 70 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 >pPCache, pgno,
11160 30 2c 20 26 70 29 3b 0a 20 20 72 65 74 75 72 6e 0, &p);. return
11170 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 p;.}../*.** Dis
11180 63 61 72 64 20 74 68 65 20 65 6e 74 69 72 65 20 card the entire
11190 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
111a0 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 2d 63 in-memory page-c
111b0 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ache..*/.static
111c0 76 6f 69 64 20 70 61 67 65 72 5f 72 65 73 65 74 void pager_reset
111d0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
111e0 0a 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 . sqlite3Backup
111f0 52 65 73 74 61 72 74 28 70 50 61 67 65 72 2d 3e Restart(pPager->
11200 70 42 61 63 6b 75 70 29 3b 0a 20 20 73 71 6c 69 pBackup);. sqli
11210 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 28 70 te3PcacheClear(p
11220 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b Pager->pPCache);
11230 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 .}../*.** Free a
11240 6c 6c 20 73 74 72 75 63 74 75 72 65 73 20 69 6e ll structures in
11250 20 74 68 65 20 50 61 67 65 72 2e 61 53 61 76 65 the Pager.aSave
11260 70 6f 69 6e 74 5b 5d 20 61 72 72 61 79 20 61 6e point[] array an
11270 64 20 73 65 74 20 62 6f 74 68 0a 2a 2a 20 50 61 d set both.** Pa
11280 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 61 ger.aSavepoint a
11290 6e 64 20 50 61 67 65 72 2e 6e 53 61 76 65 70 6f nd Pager.nSavepo
112a0 69 6e 74 20 74 6f 20 7a 65 72 6f 2e 20 43 6c 6f int to zero. Clo
112b0 73 65 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e se the sub-journ
112c0 61 6c 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 6f al.** if it is o
112d0 70 65 6e 20 61 6e 64 20 74 68 65 20 70 61 67 65 pen and the page
112e0 72 20 69 73 20 6e 6f 74 20 69 6e 20 65 78 63 6c r is not in excl
112f0 75 73 69 76 65 20 6d 6f 64 65 2e 0a 2a 2f 0a 73 usive mode..*/.s
11300 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 tatic void relea
11310 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 seAllSavepoints(
11320 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
11330 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 int ii;
11340 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 /* Itera
11350 74 6f 72 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 tor for looping
11360 74 68 72 6f 75 67 68 20 50 61 67 65 72 2e 61 53 through Pager.aS
11370 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 66 6f avepoint */. fo
11380 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 r(ii=0; ii<pPage
11390 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 r->nSavepoint; i
113a0 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 i++){. sqlite
113b0 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 3BitvecDestroy(p
113c0 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e Pager->aSavepoin
113d0 74 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 t[ii].pInSavepoi
113e0 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 nt);. }. if( !
113f0 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
11400 65 4d 6f 64 65 20 7c 7c 20 73 71 6c 69 74 65 33 eMode || sqlite3
11410 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 IsMemJournal(pPa
11420 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 ger->sjfd) ){.
11430 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 sqlite3OsClose
11440 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 3b 0a (pPager->sjfd);.
11450 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 }. sqlite3_fr
11460 65 65 28 70 50 61 67 65 72 2d 3e 61 53 61 76 65 ee(pPager->aSave
11470 70 6f 69 6e 74 29 3b 0a 20 20 70 50 61 67 65 72 point);. pPager
11480 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 ->aSavepoint = 0
11490 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 ;. pPager->nSav
114a0 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 70 50 epoint = 0;. pP
114b0 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 20 3d 20 ager->nSubRec =
114c0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 0;.}../*.** Set
114d0 74 68 65 20 62 69 74 20 6e 75 6d 62 65 72 20 70 the bit number p
114e0 67 6e 6f 20 69 6e 20 74 68 65 20 50 61 67 65 72 gno in the Pager
114f0 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 Savepoint.pInSav
11500 65 70 6f 69 6e 74 20 0a 2a 2a 20 62 69 74 76 65 epoint .** bitve
11510 63 73 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 73 cs of all open s
11520 61 76 65 70 6f 69 6e 74 73 2e 20 52 65 74 75 72 avepoints. Retur
11530 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 n SQLITE_OK if s
11540 75 63 63 65 73 73 66 75 6c 0a 2a 2a 20 6f 72 20 uccessful.** or
11550 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 SQLITE_NOMEM if
11560 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 a malloc failure
11570 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 occurs..*/.stat
11580 69 63 20 69 6e 74 20 61 64 64 54 6f 53 61 76 65 ic int addToSave
11590 70 6f 69 6e 74 42 69 74 76 65 63 73 28 50 61 67 pointBitvecs(Pag
115a0 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f er *pPager, Pgno
115b0 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 69 69 pgno){. int ii
115c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
115d0 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
115e0 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 ter */. int rc
115f0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
11600 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 /* Result cod
11610 65 20 2a 2f 0a 0a 20 20 66 6f 72 28 69 69 3d 30 e */.. for(ii=0
11620 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 ; ii<pPager->nSa
11630 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a vepoint; ii++){.
11640 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 PagerSavepoi
11650 6e 74 20 2a 70 20 3d 20 26 70 50 61 67 65 72 2d nt *p = &pPager-
11660 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 3b >aSavepoint[ii];
11670 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 . if( pgno<=p
11680 2d 3e 6e 4f 72 69 67 20 29 7b 0a 20 20 20 20 20 ->nOrig ){.
11690 20 72 63 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 rc |= sqlite3Bi
116a0 74 76 65 63 53 65 74 28 70 2d 3e 70 49 6e 53 61 tvecSet(p->pInSa
116b0 76 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 3b 0a vepoint, pgno);.
116c0 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
116d0 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
116e0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
116f0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
11700 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f || rc==SQLITE_NO
11710 4d 45 4d 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d MEM );. }. }
11720 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
11730 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
11740 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 20 tion is a no-op
11750 69 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 if the pager is
11760 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 in exclusive mod
11770 65 20 61 6e 64 20 6e 6f 74 0a 2a 2a 20 69 6e 20 e and not.** in
11780 74 68 65 20 45 52 52 4f 52 20 73 74 61 74 65 2e the ERROR state.
11790 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 73 Otherwise, it s
117a0 77 69 74 63 68 65 73 20 74 68 65 20 70 61 67 65 witches the page
117b0 72 20 74 6f 20 50 41 47 45 52 5f 4f 50 45 4e 0a r to PAGER_OPEN.
117c0 2a 2a 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 ** state..**.**
117d0 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 If the pager is
117e0 6e 6f 74 20 69 6e 20 65 78 63 6c 75 73 69 76 65 not in exclusive
117f0 2d 61 63 63 65 73 73 20 6d 6f 64 65 2c 20 74 68 -access mode, th
11800 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
11810 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 6c 79 is.** completely
11820 20 75 6e 6c 6f 63 6b 65 64 2e 20 49 66 20 74 68 unlocked. If th
11830 65 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b e file is unlock
11840 65 64 20 61 6e 64 20 74 68 65 20 66 69 6c 65 2d ed and the file-
11850 73 79 73 74 65 6d 20 64 6f 65 73 0a 2a 2a 20 6e system does.** n
11860 6f 74 20 65 78 68 69 62 69 74 20 74 68 65 20 55 ot exhibit the U
11870 4e 44 45 4c 45 54 41 42 4c 45 5f 57 48 45 4e 5f NDELETABLE_WHEN_
11880 4f 50 45 4e 20 70 72 6f 70 65 72 74 79 2c 20 74 OPEN property, t
11890 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
118a0 69 73 0a 2a 2a 20 63 6c 6f 73 65 64 20 28 69 66 is.** closed (if
118b0 20 69 74 20 69 73 20 6f 70 65 6e 29 2e 0a 2a 2a it is open)..**
118c0 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 .** If the pager
118d0 20 69 73 20 69 6e 20 45 52 52 4f 52 20 73 74 61 is in ERROR sta
118e0 74 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e te when this fun
118f0 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c ction is called,
11900 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74 the .** content
11910 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 63 s of the pager c
11920 61 63 68 65 20 61 72 65 20 64 69 73 63 61 72 64 ache are discard
11930 65 64 20 62 65 66 6f 72 65 20 73 77 69 74 63 68 ed before switch
11940 69 6e 67 20 62 61 63 6b 20 74 6f 20 0a 2a 2a 20 ing back to .**
11950 74 68 65 20 4f 50 45 4e 20 73 74 61 74 65 2e 20 the OPEN state.
11960 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 Regardless of wh
11970 65 74 68 65 72 20 74 68 65 20 70 61 67 65 72 20 ether the pager
11980 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d is in exclusive-
11990 6d 6f 64 65 0a 2a 2a 20 6f 72 20 6e 6f 74 2c 20 mode.** or not,
119a0 61 6e 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 any journal file
119b0 20 6c 65 66 74 20 69 6e 20 74 68 65 20 66 69 6c left in the fil
119c0 65 2d 73 79 73 74 65 6d 20 77 69 6c 6c 20 62 65 e-system will be
119d0 20 74 72 65 61 74 65 64 0a 2a 2a 20 61 73 20 61 treated.** as a
119e0 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64 hot-journal and
119f0 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 rolled back the
11a00 20 6e 65 78 74 20 74 69 6d 65 20 61 20 72 65 61 next time a rea
11a10 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a d-transaction.**
11a20 20 69 73 20 6f 70 65 6e 65 64 20 28 62 79 20 74 is opened (by t
11a30 68 69 73 20 6f 72 20 62 79 20 61 6e 79 20 6f 74 his or by any ot
11a40 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 2e her connection).
11a50 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
11a60 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 50 61 67 pager_unlock(Pag
11a70 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20 20 er *pPager){..
11a80 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
11a90 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 eState==PAGER_RE
11aa0 41 44 45 52 20 0a 20 20 20 20 20 20 20 7c 7c 20 ADER . ||
11ab0 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d pPager->eState==
11ac0 50 41 47 45 52 5f 4f 50 45 4e 20 0a 20 20 20 20 PAGER_OPEN .
11ad0 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53 || pPager->eS
11ae0 74 61 74 65 3d 3d 50 41 47 45 52 5f 45 52 52 4f tate==PAGER_ERRO
11af0 52 20 0a 20 20 29 3b 0a 0a 20 20 73 71 6c 69 74 R . );.. sqlit
11b00 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 e3BitvecDestroy(
11b10 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
11b20 61 6c 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 al);. pPager->p
11b30 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 InJournal = 0;.
11b40 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 70 releaseAllSavep
11b50 6f 69 6e 74 73 28 70 50 61 67 65 72 29 3b 0a 0a oints(pPager);..
11b60 20 20 69 66 28 20 70 61 67 65 72 55 73 65 57 61 if( pagerUseWa
11b70 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 l(pPager) ){.
11b80 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 6e assert( !isOpen
11b90 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b (pPager->jfd) );
11ba0 0a 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 45 . sqlite3WalE
11bb0 6e 64 52 65 61 64 54 72 61 6e 73 61 63 74 69 6f ndReadTransactio
11bc0 6e 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 29 3b n(pPager->pWal);
11bd0 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 53 74 . pPager->eSt
11be0 61 74 65 20 3d 20 50 41 47 45 52 5f 4f 50 45 4e ate = PAGER_OPEN
11bf0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 ;. }else if( !p
11c00 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 Pager->exclusive
11c10 4d 6f 64 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 Mode ){. int
11c20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
11c30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 /* Err
11c40 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 or code returned
11c50 20 62 79 20 70 61 67 65 72 55 6e 6c 6f 63 6b 44 by pagerUnlockD
11c60 62 28 29 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 b() */. int i
11c70 44 63 20 3d 20 69 73 4f 70 65 6e 28 70 50 61 67 Dc = isOpen(pPag
11c80 65 72 2d 3e 66 64 29 3f 73 71 6c 69 74 65 33 4f er->fd)?sqlite3O
11c90 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 sDeviceCharacter
11ca0 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 istics(pPager->f
11cb0 64 29 3a 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 d):0;.. /* If
11cc0 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 the operating s
11cd0 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 64 65 ystem support de
11ce0 6c 65 74 69 6f 6e 20 6f 66 20 6f 70 65 6e 20 66 letion of open f
11cf0 69 6c 65 73 2c 20 74 68 65 6e 0a 20 20 20 20 2a iles, then. *
11d00 2a 20 63 6c 6f 73 65 20 74 68 65 20 6a 6f 75 72 * close the jour
11d10 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e 20 64 72 nal file when dr
11d20 6f 70 70 69 6e 67 20 74 68 65 20 64 61 74 61 62 opping the datab
11d30 61 73 65 20 6c 6f 63 6b 2e 20 20 4f 74 68 65 72 ase lock. Other
11d40 77 69 73 65 0a 20 20 20 20 2a 2a 20 61 6e 6f 74 wise. ** anot
11d50 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 her connection w
11d60 69 74 68 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 ith journal_mode
11d70 3d 64 65 6c 65 74 65 20 6d 69 67 68 74 20 64 65 =delete might de
11d80 6c 65 74 65 20 74 68 65 20 66 69 6c 65 0a 20 20 lete the file.
11d90 20 20 2a 2a 20 6f 75 74 20 66 72 6f 6d 20 75 6e ** out from un
11da0 64 65 72 20 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 der us.. */.
11db0 20 20 20 61 73 73 65 72 74 28 20 28 50 41 47 45 assert( (PAGE
11dc0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 R_JOURNALMODE_ME
11dd0 4d 4f 52 59 20 20 20 26 20 35 29 21 3d 31 20 29 MORY & 5)!=1 )
11de0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 50 ;. assert( (P
11df0 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
11e00 5f 4f 46 46 20 20 20 20 20 20 26 20 35 29 21 3d _OFF & 5)!=
11e10 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 1 );. assert(
11e20 20 28 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d (PAGER_JOURNALM
11e30 4f 44 45 5f 57 41 4c 20 20 20 20 20 20 26 20 35 ODE_WAL & 5
11e40 29 21 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 )!=1 );. asse
11e50 72 74 28 20 28 50 41 47 45 52 5f 4a 4f 55 52 4e rt( (PAGER_JOURN
11e60 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20 20 20 ALMODE_DELETE
11e70 26 20 35 29 21 3d 31 20 29 3b 0a 20 20 20 20 61 & 5)!=1 );. a
11e80 73 73 65 72 74 28 20 28 50 41 47 45 52 5f 4a 4f ssert( (PAGER_JO
11e90 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 URNALMODE_TRUNCA
11ea0 54 45 20 26 20 35 29 3d 3d 31 20 29 3b 0a 20 20 TE & 5)==1 );.
11eb0 20 20 61 73 73 65 72 74 28 20 28 50 41 47 45 52 assert( (PAGER
11ec0 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 _JOURNALMODE_PER
11ed0 53 49 53 54 20 20 26 20 35 29 3d 3d 31 20 29 3b SIST & 5)==1 );
11ee0 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44 63 . if( 0==(iDc
11ef0 20 26 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f & SQLITE_IOCAP_
11f00 55 4e 44 45 4c 45 54 41 42 4c 45 5f 57 48 45 4e UNDELETABLE_WHEN
11f10 5f 4f 50 45 4e 29 0a 20 20 20 20 20 7c 7c 20 31 _OPEN). || 1
11f20 21 3d 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e !=(pPager->journ
11f30 61 6c 4d 6f 64 65 20 26 20 35 29 0a 20 20 20 20 alMode & 5).
11f40 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
11f50 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e OsClose(pPager->
11f60 6a 66 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 jfd);. }..
11f70 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 /* If the pager
11f80 20 69 73 20 69 6e 20 74 68 65 20 45 52 52 4f 52 is in the ERROR
11f90 20 73 74 61 74 65 20 61 6e 64 20 74 68 65 20 63 state and the c
11fa0 61 6c 6c 20 74 6f 20 75 6e 6c 6f 63 6b 20 74 68 all to unlock th
11fb0 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a e database. *
11fc0 2a 20 66 69 6c 65 20 66 61 69 6c 73 2c 20 73 65 * file fails, se
11fd0 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f t the current lo
11fe0 63 6b 20 74 6f 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f ck to UNKNOWN_LO
11ff0 43 4b 2e 20 53 65 65 20 74 68 65 20 63 6f 6d 6d CK. See the comm
12000 65 6e 74 0a 20 20 20 20 2a 2a 20 61 62 6f 76 65 ent. ** above
12010 20 74 68 65 20 23 64 65 66 69 6e 65 20 66 6f 72 the #define for
12020 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 66 6f UNKNOWN_LOCK fo
12030 72 20 61 6e 20 65 78 70 6c 61 6e 61 74 69 6f 6e r an explanation
12040 20 6f 66 20 77 68 79 20 74 68 69 73 0a 20 20 20 of why this.
12050 20 2a 2a 20 69 73 20 6e 65 63 65 73 73 61 72 79 ** is necessary
12060 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
12070 3d 20 70 61 67 65 72 55 6e 6c 6f 63 6b 44 62 28 = pagerUnlockDb(
12080 70 50 61 67 65 72 2c 20 4e 4f 5f 4c 4f 43 4b 29 pPager, NO_LOCK)
12090 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
120a0 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 LITE_OK && pPage
120b0 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 r->eState==PAGER
120c0 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 _ERROR ){.
120d0 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 20 3d 20 pPager->eLock =
120e0 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 3b 0a 20 20 UNKNOWN_LOCK;.
120f0 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 }.. /* The
12100 70 61 67 65 72 20 73 74 61 74 65 20 6d 61 79 20 pager state may
12110 62 65 20 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 be changed from
12120 50 41 47 45 52 5f 45 52 52 4f 52 20 74 6f 20 50 PAGER_ERROR to P
12130 41 47 45 52 5f 4f 50 45 4e 20 68 65 72 65 0a 20 AGER_OPEN here.
12140 20 20 20 2a 2a 20 77 69 74 68 6f 75 74 20 63 6c ** without cl
12150 65 61 72 69 6e 67 20 74 68 65 20 65 72 72 6f 72 earing the error
12160 20 63 6f 64 65 2e 20 54 68 69 73 20 69 73 20 69 code. This is i
12170 6e 74 65 6e 74 69 6f 6e 61 6c 20 2d 20 74 68 65 ntentional - the
12180 20 65 72 72 6f 72 0a 20 20 20 20 2a 2a 20 63 6f error. ** co
12190 64 65 20 69 73 20 63 6c 65 61 72 65 64 20 61 6e de is cleared an
121a0 64 20 74 68 65 20 63 61 63 68 65 20 72 65 73 65 d the cache rese
121b0 74 20 69 6e 20 74 68 65 20 62 6c 6f 63 6b 20 62 t in the block b
121c0 65 6c 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 elow.. */.
121d0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
121e0 3e 65 72 72 43 6f 64 65 20 7c 7c 20 70 50 61 67 >errCode || pPag
121f0 65 72 2d 3e 65 53 74 61 74 65 21 3d 50 41 47 45 er->eState!=PAGE
12200 52 5f 45 52 52 4f 52 20 29 3b 0a 20 20 20 20 70 R_ERROR );. p
12210 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 Pager->changeCou
12220 6e 74 44 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 ntDone = 0;.
12230 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 20 3d pPager->eState =
12240 20 50 41 47 45 52 5f 4f 50 45 4e 3b 0a 20 20 7d PAGER_OPEN;. }
12250 0a 0a 20 20 2f 2a 20 49 66 20 50 61 67 65 72 2e .. /* If Pager.
12260 65 72 72 43 6f 64 65 20 69 73 20 73 65 74 2c 20 errCode is set,
12270 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
12280 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20 the pager cache
12290 63 61 6e 6e 6f 74 20 62 65 0a 20 20 2a 2a 20 74 cannot be. ** t
122a0 72 75 73 74 65 64 2e 20 4e 6f 77 20 74 68 61 74 rusted. Now that
122b0 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 there are no ou
122c0 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 tstanding refere
122d0 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 nces to the page
122e0 72 2c 0a 20 20 2a 2a 20 69 74 20 63 61 6e 20 73 r,. ** it can s
122f0 61 66 65 6c 79 20 6d 6f 76 65 20 62 61 63 6b 20 afely move back
12300 74 6f 20 50 41 47 45 52 5f 4f 50 45 4e 20 73 74 to PAGER_OPEN st
12310 61 74 65 2e 20 54 68 69 73 20 68 61 70 70 65 6e ate. This happen
12320 73 20 69 6e 20 62 6f 74 68 0a 20 20 2a 2a 20 6e s in both. ** n
12330 6f 72 6d 61 6c 20 61 6e 64 20 65 78 63 6c 75 73 ormal and exclus
12340 69 76 65 2d 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 ive-locking mode
12350 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 .. */. if( pPa
12360 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a ger->errCode ){.
12370 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d assert( !MEM
12380 44 42 20 29 3b 0a 20 20 20 20 70 61 67 65 72 5f DB );. pager_
12390 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 reset(pPager);.
123a0 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 pPager->chang
123b0 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 eCountDone = pPa
123c0 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 ger->tempFile;.
123d0 20 20 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 pPager->eStat
123e0 65 20 3d 20 50 41 47 45 52 5f 4f 50 45 4e 3b 0a e = PAGER_OPEN;.
123f0 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 pPager->errC
12400 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b ode = SQLITE_OK;
12410 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 72 2d 3e . }.. pPager->
12420 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a journalOff = 0;.
12430 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
12440 6c 48 64 72 20 3d 20 30 3b 0a 20 20 70 50 61 67 lHdr = 0;. pPag
12450 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 er->setMaster =
12460 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 0;.}../*.** This
12470 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
12480 6c 65 64 20 77 68 65 6e 65 76 65 72 20 61 6e 20 led whenever an
12490 49 4f 45 52 52 20 6f 72 20 46 55 4c 4c 20 65 72 IOERR or FULL er
124a0 72 6f 72 20 74 68 61 74 20 72 65 71 75 69 72 65 ror that require
124b0 73 0a 2a 2a 20 74 68 65 20 70 61 67 65 72 20 74 s.** the pager t
124c0 6f 20 74 72 61 6e 73 69 74 69 6f 6e 20 69 6e 74 o transition int
124d0 6f 20 74 68 65 20 45 52 52 4f 52 20 73 74 61 74 o the ERROR stat
124e0 65 20 6d 61 79 20 61 68 76 65 20 6f 63 63 75 72 e may ahve occur
124f0 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 red..** The firs
12500 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 t argument is a
12510 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 pointer to the p
12520 61 67 65 72 20 73 74 72 75 63 74 75 72 65 2c 20 ager structure,
12530 74 68 65 20 73 65 63 6f 6e 64 20 0a 2a 2a 20 74 the second .** t
12540 68 65 20 65 72 72 6f 72 2d 63 6f 64 65 20 61 62 he error-code ab
12550 6f 75 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e out to be return
12560 65 64 20 62 79 20 61 20 70 61 67 65 72 20 41 50 ed by a pager AP
12570 49 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 I function. The
12580 0a 2a 2a 20 76 61 6c 75 65 20 72 65 74 75 72 6e .** value return
12590 65 64 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 ed is a copy of
125a0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
125b0 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 ent to this func
125c0 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 tion. .**.** If
125d0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
125e0 65 6e 74 20 69 73 20 53 51 4c 49 54 45 5f 46 55 ent is SQLITE_FU
125f0 4c 4c 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 LL, SQLITE_IOERR
12600 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a or one of the.*
12610 2a 20 49 4f 45 52 52 20 73 75 62 2d 63 6f 64 65 * IOERR sub-code
12620 73 2c 20 74 68 65 20 70 61 67 65 72 20 65 6e 74 s, the pager ent
12630 65 72 73 20 74 68 65 20 45 52 52 4f 52 20 73 74 ers the ERROR st
12640 61 74 65 20 61 6e 64 20 74 68 65 20 65 72 72 6f ate and the erro
12650 72 20 63 6f 64 65 0a 2a 2a 20 69 73 20 73 74 6f r code.** is sto
12660 72 65 64 20 69 6e 20 50 61 67 65 72 2e 65 72 72 red in Pager.err
12670 43 6f 64 65 2e 20 57 68 69 6c 65 20 74 68 65 20 Code. While the
12680 70 61 67 65 72 20 72 65 6d 61 69 6e 73 20 69 6e pager remains in
12690 20 74 68 65 20 45 52 52 4f 52 20 73 74 61 74 65 the ERROR state
126a0 2c 0a 2a 2a 20 61 6c 6c 20 6d 61 6a 6f 72 20 41 ,.** all major A
126b0 50 49 20 63 61 6c 6c 73 20 6f 6e 20 74 68 65 20 PI calls on the
126c0 50 61 67 65 72 20 77 69 6c 6c 20 69 6d 6d 65 64 Pager will immed
126d0 69 61 74 65 6c 79 20 72 65 74 75 72 6e 20 50 61 iately return Pa
126e0 67 65 72 2e 65 72 72 43 6f 64 65 2e 0a 2a 2a 0a ger.errCode..**.
126f0 2a 2a 20 54 68 65 20 45 52 52 4f 52 20 73 74 61 ** The ERROR sta
12700 74 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 te indicates tha
12710 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f t the contents o
12720 66 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 f the pager-cach
12730 65 20 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 e .** cannot be
12740 74 72 75 73 74 65 64 2e 20 54 68 69 73 20 73 74 trusted. This st
12750 61 74 65 20 63 61 6e 20 62 65 20 63 6c 65 61 72 ate can be clear
12760 65 64 20 62 79 20 63 6f 6d 70 6c 65 74 65 6c 79 ed by completely
12770 20 64 69 73 63 61 72 64 69 6e 67 20 0a 2a 2a 20 discarding .**
12780 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
12790 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 2e the pager-cache.
127a0 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f If a transactio
127b0 6e 20 77 61 73 20 61 63 74 69 76 65 20 77 68 65 n was active whe
127c0 6e 0a 2a 2a 20 74 68 65 20 70 65 72 73 69 73 74 n.** the persist
127d0 65 6e 74 20 65 72 72 6f 72 20 6f 63 63 75 72 72 ent error occurr
127e0 65 64 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 6c ed, then the rol
127f0 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 6d 61 lback journal ma
12800 79 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 y need.** to be
12810 72 65 70 6c 61 79 65 64 20 74 6f 20 72 65 73 74 replayed to rest
12820 6f 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ore the contents
12830 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
12840 20 66 69 6c 65 20 28 61 73 20 69 66 0a 2a 2a 20 file (as if.**
12850 69 74 20 77 65 72 65 20 61 20 68 6f 74 2d 6a 6f it were a hot-jo
12860 75 72 6e 61 6c 29 2e 0a 2a 2f 0a 73 74 61 74 69 urnal)..*/.stati
12870 63 20 69 6e 74 20 70 61 67 65 72 5f 65 72 72 6f c int pager_erro
12880 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c r(Pager *pPager,
12890 20 69 6e 74 20 72 63 29 7b 0a 20 20 69 6e 74 20 int rc){. int
128a0 72 63 32 20 3d 20 72 63 20 26 20 30 78 66 66 3b rc2 = rc & 0xff;
128b0 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 . assert( rc==S
128c0 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 4d 45 4d QLITE_OK || !MEM
128d0 44 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28 0a DB );. assert(.
128e0 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 pPager->e
128f0 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 46 rrCode==SQLITE_F
12900 55 4c 4c 20 7c 7c 0a 20 20 20 20 20 20 20 70 50 ULL ||. pP
12910 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 ager->errCode==S
12920 51 4c 49 54 45 5f 4f 4b 20 7c 7c 0a 20 20 20 20 QLITE_OK ||.
12930 20 20 20 28 70 50 61 67 65 72 2d 3e 65 72 72 43 (pPager->errC
12940 6f 64 65 20 26 20 30 78 66 66 29 3d 3d 53 51 4c ode & 0xff)==SQL
12950 49 54 45 5f 49 4f 45 52 52 0a 20 20 29 3b 0a 20 ITE_IOERR. );.
12960 20 69 66 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 if( rc2==SQLITE
12970 5f 46 55 4c 4c 20 7c 7c 20 72 63 32 3d 3d 53 51 _FULL || rc2==SQ
12980 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 LITE_IOERR ){.
12990 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 pPager->errCod
129a0 65 20 3d 20 72 63 3b 0a 20 20 20 20 70 50 61 67 e = rc;. pPag
129b0 65 72 2d 3e 65 53 74 61 74 65 20 3d 20 50 41 47 er->eState = PAG
129c0 45 52 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 ER_ERROR;. }.
129d0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 return rc;.}..st
129e0 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 74 atic int pager_t
129f0 72 75 6e 63 61 74 65 28 50 61 67 65 72 20 2a 70 runcate(Pager *p
12a00 50 61 67 65 72 2c 20 50 67 6e 6f 20 6e 50 61 67 Pager, Pgno nPag
12a10 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 e);../*.** This
12a20 72 6f 75 74 69 6e 65 20 65 6e 64 73 20 61 20 74 routine ends a t
12a30 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20 74 72 ransaction. A tr
12a40 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 75 73 75 ansaction is usu
12a50 61 6c 6c 79 20 65 6e 64 65 64 20 62 79 20 0a 2a ally ended by .*
12a60 2a 20 65 69 74 68 65 72 20 61 20 43 4f 4d 4d 49 * either a COMMI
12a70 54 20 6f 72 20 61 20 52 4f 4c 4c 42 41 43 4b 20 T or a ROLLBACK
12a80 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 20 operation. This
12a90 72 6f 75 74 69 6e 65 20 6d 61 79 20 62 65 20 63 routine may be c
12aa0 61 6c 6c 65 64 20 0a 2a 2a 20 61 66 74 65 72 20 alled .** after
12ab0 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20 68 6f rollback of a ho
12ac0 74 2d 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 69 66 t-journal, or if
12ad0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
12ae0 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67 0a 2a while opening.*
12af0 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 * the journal fi
12b00 6c 65 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68 le or writing th
12b10 65 20 76 65 72 79 20 66 69 72 73 74 20 6a 6f 75 e very first jou
12b20 72 6e 61 6c 2d 68 65 61 64 65 72 20 6f 66 20 61 rnal-header of a
12b30 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 72 61 .** database tra
12b40 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a 2a 2a nsaction..** .**
12b50 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
12b60 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 69 6e never called in
12b70 20 50 41 47 45 52 5f 45 52 52 4f 52 20 73 74 61 PAGER_ERROR sta
12b80 74 65 2e 20 49 66 20 69 74 20 69 73 20 63 61 6c te. If it is cal
12b90 6c 65 64 0a 2a 2a 20 69 6e 20 50 41 47 45 52 5f led.** in PAGER_
12ba0 4e 4f 4e 45 20 6f 72 20 50 41 47 45 52 5f 53 48 NONE or PAGER_SH
12bb0 41 52 45 44 20 73 74 61 74 65 20 61 6e 64 20 74 ARED state and t
12bc0 68 65 20 6c 6f 63 6b 20 68 65 6c 64 20 69 73 20 he lock held is
12bd0 6c 65 73 73 0a 2a 2a 20 65 78 63 6c 75 73 69 76 less.** exclusiv
12be0 65 20 74 68 61 6e 20 61 20 52 45 53 45 52 56 45 e than a RESERVE
12bf0 44 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 61 20 D lock, it is a
12c00 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 no-op..**.** Oth
12c10 65 72 77 69 73 65 2c 20 61 6e 79 20 61 63 74 69 erwise, any acti
12c20 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 72 ve savepoints ar
12c30 65 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a e released..**.*
12c40 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c * If the journal
12c50 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 2c 20 74 file is open, t
12c60 68 65 6e 20 69 74 20 69 73 20 22 66 69 6e 61 6c hen it is "final
12c70 69 7a 65 64 22 2e 20 4f 6e 63 65 20 61 20 6a 6f ized". Once a jo
12c80 75 72 6e 61 6c 20 0a 2a 2a 20 66 69 6c 65 20 68 urnal .** file h
12c90 61 73 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65 as been finalize
12ca0 64 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 d it is not poss
12cb0 69 62 6c 65 20 74 6f 20 75 73 65 20 69 74 20 74 ible to use it t
12cc0 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 61 20 0a 2a o roll back a .*
12cd0 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 4e * transaction. N
12ce0 6f 72 20 77 69 6c 6c 20 69 74 20 62 65 20 63 6f or will it be co
12cf0 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 61 nsidered to be a
12d00 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 79 20 hot-journal by
12d10 74 68 69 73 0a 2a 2a 20 6f 72 20 61 6e 79 20 6f this.** or any o
12d20 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f ther database co
12d30 6e 6e 65 63 74 69 6f 6e 2e 20 45 78 61 63 74 6c nnection. Exactl
12d40 79 20 68 6f 77 20 61 20 6a 6f 75 72 6e 61 6c 20 y how a journal
12d50 69 73 20 66 69 6e 61 6c 69 7a 65 64 0a 2a 2a 20 is finalized.**
12d60 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 65 74 68 depends on wheth
12d70 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 70 61 er or not the pa
12d80 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 ger is running i
12d90 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n exclusive mode
12da0 20 61 6e 64 0a 2a 2a 20 74 68 65 20 63 75 72 72 and.** the curr
12db0 65 6e 74 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 ent journal-mode
12dc0 20 28 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4d (Pager.journalM
12dd0 6f 64 65 20 76 61 6c 75 65 29 2c 20 61 73 20 66 ode value), as f
12de0 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 ollows:.**.**
12df0 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 4d journalMode==MEM
12e00 4f 52 59 0a 2a 2a 20 20 20 20 20 4a 6f 75 72 6e ORY.** Journ
12e10 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 al file descript
12e20 6f 72 20 69 73 20 73 69 6d 70 6c 79 20 63 6c 6f or is simply clo
12e30 73 65 64 2e 20 54 68 69 73 20 64 65 73 74 72 6f sed. This destro
12e40 79 73 20 61 6e 20 0a 2a 2a 20 20 20 20 20 69 6e ys an .** in
12e50 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 2e -memory journal.
12e60 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c .**.** journal
12e70 4d 6f 64 65 3d 3d 54 52 55 4e 43 41 54 45 0a 2a Mode==TRUNCATE.*
12e80 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20 66 69 * Journal fi
12e90 6c 65 20 69 73 20 74 72 75 6e 63 61 74 65 64 20 le is truncated
12ea0 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69 6e to zero bytes in
12eb0 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a size..**.** j
12ec0 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 45 52 53 ournalMode==PERS
12ed0 49 53 54 0a 2a 2a 20 20 20 20 20 54 68 65 20 66 IST.** The f
12ee0 69 72 73 74 20 32 38 20 62 79 74 65 73 20 6f 66 irst 28 bytes of
12ef0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
12f00 65 20 61 72 65 20 7a 65 72 6f 65 64 2e 20 54 68 e are zeroed. Th
12f10 69 73 20 69 6e 76 61 6c 69 64 61 74 65 73 0a 2a is invalidates.*
12f20 2a 20 20 20 20 20 74 68 65 20 66 69 72 73 74 20 * the first
12f30 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 journal header i
12f40 6e 20 74 68 65 20 66 69 6c 65 2c 20 61 6e 64 20 n the file, and
12f50 68 65 6e 63 65 20 74 68 65 20 65 6e 74 69 72 65 hence the entire
12f60 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 journal.**
12f70 66 69 6c 65 2e 20 41 6e 20 69 6e 76 61 6c 69 64 file. An invalid
12f80 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 61 journal file ca
12f90 6e 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 nnot be rolled b
12fa0 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 ack..**.** jou
12fb0 72 6e 61 6c 4d 6f 64 65 3d 3d 44 45 4c 45 54 45 rnalMode==DELETE
12fc0 0a 2a 2a 20 20 20 20 20 54 68 65 20 6a 6f 75 72 .** The jour
12fd0 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 6c 6f 73 nal file is clos
12fe0 65 64 20 61 6e 64 20 64 65 6c 65 74 65 64 20 75 ed and deleted u
12ff0 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 44 65 sing sqlite3OsDe
13000 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 lete()..**.**
13010 20 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 If the pager i
13020 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 s running in exc
13030 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 69 lusive mode, thi
13040 73 20 6d 65 74 68 6f 64 20 6f 66 20 66 69 6e 61 s method of fina
13050 6c 69 7a 69 6e 67 0a 2a 2a 20 20 20 20 20 74 68 lizing.** th
13060 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
13070 73 20 6e 65 76 65 72 20 75 73 65 64 2e 20 49 6e s never used. In
13080 73 74 65 61 64 2c 20 69 66 20 74 68 65 20 6a 6f stead, if the jo
13090 75 72 6e 61 6c 4d 6f 64 65 20 69 73 0a 2a 2a 20 urnalMode is.**
130a0 20 20 20 20 44 45 4c 45 54 45 20 61 6e 64 20 74 DELETE and t
130b0 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 65 he pager is in e
130c0 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 xclusive mode, t
130d0 68 65 20 6d 65 74 68 6f 64 20 64 65 73 63 72 69 he method descri
130e0 62 65 64 20 75 6e 64 65 72 0a 2a 2a 20 20 20 20 bed under.**
130f0 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 45 journalMode==PE
13100 52 53 49 53 54 20 69 73 20 75 73 65 64 20 69 6e RSIST is used in
13110 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 stead..**.** Aft
13120 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 er the journal i
13130 73 20 66 69 6e 61 6c 69 7a 65 64 2c 20 74 68 65 s finalized, the
13140 20 70 61 67 65 72 20 6d 6f 76 65 73 20 74 6f 20 pager moves to
13150 50 41 47 45 52 5f 52 45 41 44 45 52 20 73 74 61 PAGER_READER sta
13160 74 65 2e 0a 2a 2a 20 49 66 20 72 75 6e 6e 69 6e te..** If runnin
13170 67 20 69 6e 20 6e 6f 6e 2d 65 78 63 6c 75 73 69 g in non-exclusi
13180 76 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 6f 64 65 ve rollback mode
13190 2c 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 , the lock on th
131a0 65 20 66 69 6c 65 20 69 73 20 0a 2a 2a 20 64 6f e file is .** do
131b0 77 6e 67 72 61 64 65 64 20 74 6f 20 61 20 53 48 wngraded to a SH
131c0 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a ARED_LOCK..**.**
131d0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
131e0 74 75 72 6e 65 64 20 69 66 20 6e 6f 20 65 72 72 turned if no err
131f0 6f 72 20 6f 63 63 75 72 73 2e 20 49 66 20 61 6e or occurs. If an
13200 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 error occurs du
13210 72 69 6e 67 0a 2a 2a 20 61 6e 79 20 6f 66 20 74 ring.** any of t
13220 68 65 20 49 4f 20 6f 70 65 72 61 74 69 6f 6e 73 he IO operations
13230 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 to finalize the
13240 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 journal file or
13250 20 75 6e 6c 6f 63 6b 20 74 68 65 0a 2a 2a 20 64 unlock the.** d
13260 61 74 61 62 61 73 65 20 74 68 65 6e 20 74 68 65 atabase then the
13270 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 IO error code i
13280 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 s returned to th
13290 65 20 75 73 65 72 2e 20 49 66 20 74 68 65 20 0a e user. If the .
132a0 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 ** operation to
132b0 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 finalize the jou
132c0 72 6e 61 6c 20 66 69 6c 65 20 66 61 69 6c 73 2c rnal file fails,
132d0 20 74 68 65 6e 20 74 68 65 20 63 6f 64 65 20 73 then the code s
132e0 74 69 6c 6c 0a 2a 2a 20 74 72 69 65 73 20 74 6f till.** tries to
132f0 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 unlock the data
13300 62 61 73 65 20 66 69 6c 65 20 69 66 20 6e 6f 74 base file if not
13310 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f in exclusive mo
13320 64 65 2e 20 49 66 20 74 68 65 0a 2a 2a 20 75 6e de. If the.** un
13330 6c 6f 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20 66 lock operation f
13340 61 69 6c 73 20 61 73 20 77 65 6c 6c 2c 20 74 68 ails as well, th
13350 65 6e 20 74 68 65 20 66 69 72 73 74 20 65 72 72 en the first err
13360 6f 72 20 63 6f 64 65 20 72 65 6c 61 74 65 64 0a or code related.
13370 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 ** to the first
13380 65 72 72 6f 72 20 65 6e 63 6f 75 6e 74 65 72 65 error encountere
13390 64 20 28 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 d (the journal f
133a0 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f 6e 65 29 inalization one)
133b0 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e is.** returned.
133c0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
133d0 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 ager_end_transac
133e0 74 69 6f 6e 28 50 61 67 65 72 20 2a 70 50 61 67 tion(Pager *pPag
133f0 65 72 2c 20 69 6e 74 20 68 61 73 4d 61 73 74 65 er, int hasMaste
13400 72 2c 20 69 6e 74 20 62 43 6f 6d 6d 69 74 29 7b r, int bCommit){
13410 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
13420 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 45 TE_OK; /* E
13430 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 6a rror code from j
13440 6f 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74 ournal finalizat
13450 69 6f 6e 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f ion operation */
13460 0a 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51 4c . int rc2 = SQL
13470 49 54 45 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 45 ITE_OK; /* E
13480 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 64 rror code from d
13490 62 20 66 69 6c 65 20 75 6e 6c 6f 63 6b 20 6f 70 b file unlock op
134a0 65 72 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a eration */.. /*
134b0 20 44 6f 20 6e 6f 74 68 69 6e 67 20 69 66 20 74 Do nothing if t
134c0 68 65 20 70 61 67 65 72 20 64 6f 65 73 20 6e 6f he pager does no
134d0 74 20 68 61 76 65 20 61 6e 20 6f 70 65 6e 20 77 t have an open w
134e0 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
134f0 0a 20 20 2a 2a 20 6f 72 20 61 74 20 6c 65 61 73 . ** or at leas
13500 74 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 t a RESERVED loc
13510 6b 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e k. This function
13520 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 77 may be called w
13530 68 65 6e 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 hen there. ** i
13540 73 20 6e 6f 20 77 72 69 74 65 2d 74 72 61 6e 73 s no write-trans
13550 61 63 74 69 6f 6e 20 61 63 74 69 76 65 20 62 75 action active bu
13560 74 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 t a RESERVED or
13570 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 69 73 0a greater lock is.
13580 20 20 2a 2a 20 68 65 6c 64 20 75 6e 64 65 72 20 ** held under
13590 74 77 6f 20 63 69 72 63 75 6d 73 74 61 6e 63 65 two circumstance
135a0 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 31 s:. **. ** 1
135b0 2e 20 41 66 74 65 72 20 61 20 73 75 63 63 65 73 . After a succes
135c0 73 66 75 6c 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c sful hot-journal
135d0 20 72 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 69 73 rollback, it is
135e0 20 63 61 6c 6c 65 64 20 77 69 74 68 0a 20 20 2a called with. *
135f0 2a 20 20 20 20 20 20 65 53 74 61 74 65 3d 3d 50 * eState==P
13600 41 47 45 52 5f 4e 4f 4e 45 20 61 6e 64 20 65 4c AGER_NONE and eL
13610 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c ock==EXCLUSIVE_L
13620 4f 43 4b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 OCK.. **. **
13630 20 32 2e 20 49 66 20 61 20 63 6f 6e 6e 65 63 74 2. If a connect
13640 69 6f 6e 20 77 69 74 68 20 6c 6f 63 6b 69 6e 67 ion with locking
13650 5f 6d 6f 64 65 3d 65 78 63 6c 75 73 69 76 65 20 _mode=exclusive
13660 68 6f 6c 64 69 6e 67 20 61 6e 20 45 58 43 4c 55 holding an EXCLU
13670 53 49 56 45 20 0a 20 20 2a 2a 20 20 20 20 20 20 SIVE . **
13680 6c 6f 63 6b 20 73 77 69 74 63 68 65 73 20 62 61 lock switches ba
13690 63 6b 20 74 6f 20 6c 6f 63 6b 69 6e 67 5f 6d 6f ck to locking_mo
136a0 64 65 3d 6e 6f 72 6d 61 6c 20 61 6e 64 20 74 68 de=normal and th
136b0 65 6e 20 65 78 65 63 75 74 65 73 20 61 0a 20 20 en executes a.
136c0 2a 2a 20 20 20 20 20 20 72 65 61 64 2d 74 72 61 ** read-tra
136d0 6e 73 61 63 74 69 6f 6e 2c 20 74 68 69 73 20 66 nsaction, this f
136e0 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
136f0 64 20 77 69 74 68 20 65 53 74 61 74 65 3d 3d 50 d with eState==P
13700 41 47 45 52 5f 52 45 41 44 45 52 20 0a 20 20 2a AGER_READER . *
13710 2a 20 20 20 20 20 20 61 6e 64 20 65 4c 6f 63 6b * and eLock
13720 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ==EXCLUSIVE_LOCK
13730 20 77 68 65 6e 20 74 68 65 20 72 65 61 64 2d 74 when the read-t
13740 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6c ransaction is cl
13750 6f 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 osed.. */. ass
13760 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 67 65 ert( assert_page
13770 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 29 20 r_state(pPager)
13780 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
13790 67 65 72 2d 3e 65 53 74 61 74 65 21 3d 50 41 47 ger->eState!=PAG
137a0 45 52 5f 45 52 52 4f 52 20 29 3b 0a 20 20 69 66 ER_ERROR );. if
137b0 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 ( pPager->eState
137c0 3c 50 41 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f <PAGER_WRITER_LO
137d0 43 4b 45 44 20 26 26 20 70 50 61 67 65 72 2d 3e CKED && pPager->
137e0 65 4c 6f 63 6b 3c 52 45 53 45 52 56 45 44 5f 4c eLock<RESERVED_L
137f0 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 OCK ){. retur
13800 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
13810 0a 0a 20 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 .. releaseAllSa
13820 76 65 70 6f 69 6e 74 73 28 70 50 61 67 65 72 29 vepoints(pPager)
13830 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 ;. assert( isOp
13840 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
13850 7c 7c 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f || pPager->pInJo
13860 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 urnal==0 );. if
13870 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
13880 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 61 73 73 >jfd) ){. ass
13890 65 72 74 28 20 21 70 61 67 65 72 55 73 65 57 61 ert( !pagerUseWa
138a0 6c 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 l(pPager) );..
138b0 20 20 2f 2a 20 46 69 6e 61 6c 69 7a 65 20 74 68 /* Finalize th
138c0 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 e journal file.
138d0 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 */. if( sqlit
138e0 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 e3IsMemJournal(p
138f0 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 Pager->jfd) ){.
13900 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
13910 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
13920 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
13930 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 20 20 ODE_MEMORY );.
13940 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
13950 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b se(pPager->jfd);
13960 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 . }else if( p
13970 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
13980 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
13990 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 29 LMODE_TRUNCATE )
139a0 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 {. if( pPag
139b0 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d er->journalOff==
139c0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 0 ){. rc
139d0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
139e0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
139f0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
13a00 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d Truncate(pPager-
13a10 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 >jfd, 0);.
13a20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e }. pPager->
13a30 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a journalOff = 0;.
13a40 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 }else if( pP
13a50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
13a60 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
13a70 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20 20 MODE_PERSIST.
13a80 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 65 || (pPager->e
13a90 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 26 26 20 xclusiveMode &&
13aa0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
13ab0 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode!=PAGER_JOURN
13ac0 41 4c 4d 4f 44 45 5f 57 41 4c 29 0a 20 20 20 20 ALMODE_WAL).
13ad0 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 7a 65 ){. rc = ze
13ae0 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 roJournalHdr(pPa
13af0 67 65 72 2c 20 68 61 73 4d 61 73 74 65 72 29 3b ger, hasMaster);
13b00 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a . pPager->j
13b10 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 ournalOff = 0;.
13b20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
13b30 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 6d /* This branch m
13b40 61 79 20 62 65 20 65 78 65 63 75 74 65 64 20 77 ay be executed w
13b50 69 74 68 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 ith Pager.journa
13b60 6c 4d 6f 64 65 3d 3d 4d 45 4d 4f 52 59 20 69 66 lMode==MEMORY if
13b70 0a 20 20 20 20 20 20 2a 2a 20 61 20 68 6f 74 2d . ** a hot-
13b80 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6a 75 73 74 journal was just
13b90 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 6e rolled back. In
13ba0 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 6a this case the j
13bb0 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20 ournal. **
13bc0 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 63 file should be c
13bd0 6c 6f 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65 losed and delete
13be0 64 2e 20 49 66 20 74 68 69 73 20 63 6f 6e 6e 65 d. If this conne
13bf0 63 74 69 6f 6e 20 77 72 69 74 65 73 20 74 6f 0a ction writes to.
13c00 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 ** the dat
13c10 61 62 61 73 65 20 66 69 6c 65 2c 20 69 74 20 77 abase file, it w
13c20 69 6c 6c 20 64 6f 20 73 6f 20 75 73 69 6e 67 20 ill do so using
13c30 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 an in-memory jou
13c40 72 6e 61 6c 2e 20 0a 20 20 20 20 20 20 2a 2f 0a rnal. . */.
13c50 20 20 20 20 20 20 69 6e 74 20 62 44 65 6c 65 74 int bDelet
13c60 65 20 3d 20 28 21 70 50 61 67 65 72 2d 3e 74 65 e = (!pPager->te
13c70 6d 70 46 69 6c 65 20 26 26 20 73 71 6c 69 74 65 mpFile && sqlite
13c80 33 4a 6f 75 72 6e 61 6c 45 78 69 73 74 73 28 70 3JournalExists(p
13c90 50 61 67 65 72 2d 3e 6a 66 64 29 29 3b 0a 20 20 Pager->jfd));.
13ca0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
13cb0 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d er->journalMode=
13cc0 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
13cd0 44 45 5f 44 45 4c 45 54 45 20 0a 20 20 20 20 20 DE_DELETE .
13ce0 20 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d || pPager-
13cf0 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 >journalMode==PA
13d00 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
13d10 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 20 20 MEMORY .
13d20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f || pPager->jo
13d30 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 urnalMode==PAGER
13d40 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c _JOURNALMODE_WAL
13d50 20 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 . );.
13d60 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
13d70 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 pPager->jfd);.
13d80 20 20 20 20 69 66 28 20 62 44 65 6c 65 74 65 20 if( bDelete
13d90 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
13da0 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 sqlite3OsDelete(
13db0 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 pPager->pVfs, pP
13dc0 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 ager->zJournal,
13dd0 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0);. }.
13de0 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 }. }..#ifdef SQ
13df0 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 LITE_CHECK_PAGES
13e00 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 . sqlite3Pcache
13e10 49 74 65 72 61 74 65 44 69 72 74 79 28 70 50 61 IterateDirty(pPa
13e20 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 ger->pPCache, pa
13e30 67 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 ger_set_pagehash
13e40 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d );. if( pPager-
13e50 3e 64 62 53 69 7a 65 3d 3d 30 20 26 26 20 73 71 >dbSize==0 && sq
13e60 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f lite3PcacheRefCo
13e70 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 unt(pPager->pPCa
13e80 63 68 65 29 3e 30 20 29 7b 0a 20 20 20 20 50 67 che)>0 ){. Pg
13e90 48 64 72 20 2a 70 20 3d 20 70 61 67 65 72 5f 6c Hdr *p = pager_l
13ea0 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 31 29 ookup(pPager, 1)
13eb0 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 ;. if( p ){.
13ec0 20 20 20 20 20 70 2d 3e 70 61 67 65 48 61 73 68 p->pageHash
13ed0 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 = 0;. sqli
13ee0 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 29 te3PagerUnref(p)
13ef0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 ;. }. }.#end
13f00 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 42 69 74 if.. sqlite3Bit
13f10 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 vecDestroy(pPage
13f20 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a r->pInJournal);.
13f30 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 pPager->pInJou
13f40 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 70 50 61 67 rnal = 0;. pPag
13f50 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 er->nRec = 0;.
13f60 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 sqlite3PcacheCle
13f70 61 6e 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 anAll(pPager->pP
13f80 43 61 63 68 65 29 3b 0a 20 20 73 71 6c 69 74 65 Cache);. sqlite
13f90 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 3PcacheTruncate(
13fa0 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c pPager->pPCache,
13fb0 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 pPager->dbSize)
13fc0 3b 0a 0a 20 20 69 66 28 20 70 61 67 65 72 55 73 ;.. if( pagerUs
13fd0 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0a eWal(pPager) ){.
13fe0 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 /* Drop the
13ff0 57 41 4c 20 77 72 69 74 65 2d 6c 6f 63 6b 2c 20 WAL write-lock,
14000 69 66 20 61 6e 79 2e 20 41 6c 73 6f 2c 20 69 66 if any. Also, if
14010 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 the connection
14020 77 61 73 20 69 6e 20 0a 20 20 20 20 2a 2a 20 6c was in . ** l
14030 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 65 78 63 6c ocking_mode=excl
14040 75 73 69 76 65 20 6d 6f 64 65 20 62 75 74 20 69 usive mode but i
14050 73 20 6e 6f 20 6c 6f 6e 67 65 72 2c 20 64 72 6f s no longer, dro
14060 70 20 74 68 65 20 45 58 43 4c 55 53 49 56 45 20 p the EXCLUSIVE
14070 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 20 68 65 6c . ** lock hel
14080 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 d on the databas
14090 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 e file.. */.
140a0 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 rc2 = sqlite3
140b0 57 61 6c 45 6e 64 57 72 69 74 65 54 72 61 6e 73 WalEndWriteTrans
140c0 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2d 3e 70 action(pPager->p
140d0 57 61 6c 29 3b 0a 20 20 20 20 61 73 73 65 72 74 Wal);. assert
140e0 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 4f 4b ( rc2==SQLITE_OK
140f0 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 );. }else if(
14100 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
14110 20 62 43 6f 6d 6d 69 74 20 26 26 20 70 50 61 67 bCommit && pPag
14120 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 3e 70 er->dbFileSize>p
14130 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 29 7b Pager->dbSize ){
14140 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 . /* This bra
14150 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 77 68 65 nch is taken whe
14160 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 n committing a t
14170 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 72 6f ransaction in ro
14180 6c 6c 62 61 63 6b 2d 6a 6f 75 72 6e 61 6c 0a 20 llback-journal.
14190 20 20 20 2a 2a 20 6d 6f 64 65 20 69 66 20 74 68 ** mode if th
141a0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
141b0 6f 6e 20 64 69 73 6b 20 69 73 20 6c 61 72 67 65 on disk is large
141c0 72 20 74 68 61 6e 20 74 68 65 20 64 61 74 61 62 r than the datab
141d0 61 73 65 20 69 6d 61 67 65 2e 0a 20 20 20 20 2a ase image.. *
141e0 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 * At this point
141f0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 61 73 20 the journal has
14200 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64 20 61 been finalized a
14210 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 nd the transacti
14220 6f 6e 20 0a 20 20 20 20 2a 2a 20 73 75 63 63 65 on . ** succe
14230 73 73 66 75 6c 6c 79 20 63 6f 6d 6d 69 74 74 65 ssfully committe
14240 64 2c 20 62 75 74 20 74 68 65 20 45 58 43 4c 55 d, but the EXCLU
14250 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 73 74 69 SIVE lock is sti
14260 6c 6c 20 68 65 6c 64 20 6f 6e 20 74 68 65 0a 20 ll held on the.
14270 20 20 20 2a 2a 20 66 69 6c 65 2e 20 53 6f 20 69 ** file. So i
14280 74 20 69 73 20 73 61 66 65 20 74 6f 20 74 72 75 t is safe to tru
14290 6e 63 61 74 65 20 74 68 65 20 64 61 74 61 62 61 ncate the databa
142a0 73 65 20 66 69 6c 65 20 74 6f 20 69 74 73 20 6d se file to its m
142b0 69 6e 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 72 65 inimum. ** re
142c0 71 75 69 72 65 64 20 73 69 7a 65 2e 20 20 2a 2f quired size. */
142d0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
142e0 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 45 58 43 4c ger->eLock==EXCL
142f0 55 53 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 USIVE_LOCK );.
14300 20 20 72 63 20 3d 20 70 61 67 65 72 5f 74 72 75 rc = pager_tru
14310 6e 63 61 74 65 28 70 50 61 67 65 72 2c 20 70 50 ncate(pPager, pP
14320 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 ager->dbSize);.
14330 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 }.. if( !pPage
14340 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
14350 20 0a 20 20 20 26 26 20 28 21 70 61 67 65 72 55 . && (!pagerU
14360 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 7c 7c seWal(pPager) ||
14370 20 73 71 6c 69 74 65 33 57 61 6c 45 78 63 6c 75 sqlite3WalExclu
14380 73 69 76 65 4d 6f 64 65 28 70 50 61 67 65 72 2d siveMode(pPager-
14390 3e 70 57 61 6c 2c 20 30 29 29 0a 20 20 29 7b 0a >pWal, 0)). ){.
143a0 20 20 20 20 72 63 32 20 3d 20 70 61 67 65 72 55 rc2 = pagerU
143b0 6e 6c 6f 63 6b 44 62 28 70 50 61 67 65 72 2c 20 nlockDb(pPager,
143c0 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 SHARED_LOCK);.
143d0 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 pPager->change
143e0 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 30 3b 0a 20 CountDone = 0;.
143f0 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 65 53 74 }. pPager->eSt
14400 61 74 65 20 3d 20 50 41 47 45 52 5f 52 45 41 44 ate = PAGER_READ
14410 45 52 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 65 ER;. pPager->se
14420 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 0a 20 20 tMaster = 0;..
14430 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 return (rc==SQLI
14440 54 45 5f 4f 4b 3f 72 63 32 3a 72 63 29 3b 0a 7d TE_OK?rc2:rc);.}
14450 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 ../*.** Execute
14460 61 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 61 20 a rollback if a
14470 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 transaction is a
14480 63 74 69 76 65 20 61 6e 64 20 75 6e 6c 6f 63 6b ctive and unlock
14490 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 the .** databas
144a0 65 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 e file. .**.** I
144b0 66 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 f the pager has
144c0 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64 20 already entered
144d0 74 68 65 20 45 52 52 4f 52 20 73 74 61 74 65 2c the ERROR state,
144e0 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 do not attempt
144f0 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b .** the rollback
14500 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 49 at this time. I
14510 6e 73 74 65 61 64 2c 20 70 61 67 65 72 5f 75 6e nstead, pager_un
14520 6c 6f 63 6b 28 29 20 69 73 20 63 61 6c 6c 65 64 lock() is called
14530 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f . The.** call to
14540 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 pager_unlock()
14550 77 69 6c 6c 20 64 69 73 63 61 72 64 20 61 6c 6c will discard all
14560 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 in-memory pages
14570 2c 20 75 6e 6c 6f 63 6b 0a 2a 2a 20 74 68 65 20 , unlock.** the
14580 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e database file an
14590 64 20 6d 6f 76 65 20 74 68 65 20 70 61 67 65 72 d move the pager
145a0 20 62 61 63 6b 20 74 6f 20 4f 50 45 4e 20 73 74 back to OPEN st
145b0 61 74 65 2e 20 49 66 20 74 68 69 73 20 0a 2a 2a ate. If this .**
145c0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 72 means that ther
145d0 65 20 69 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e e is a hot-journ
145e0 61 6c 20 6c 65 66 74 20 69 6e 20 74 68 65 20 66 al left in the f
145f0 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 65 20 ile-system, the
14600 6e 65 78 74 20 0a 2a 2a 20 63 6f 6e 6e 65 63 74 next .** connect
14610 69 6f 6e 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 ion to obtain a
14620 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 shared lock on t
14630 68 65 20 70 61 67 65 72 20 28 77 68 69 63 68 20 he pager (which
14640 6d 61 79 20 62 65 20 74 68 69 73 20 6f 6e 65 29 may be this one)
14650 20 0a 2a 2a 20 77 69 6c 6c 20 72 6f 6c 6c 20 69 .** will roll i
14660 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 t back..**.** If
14670 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 6e the pager has n
14680 6f 74 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 ot already enter
14690 65 64 20 74 68 65 20 45 52 52 4f 52 20 73 74 61 ed the ERROR sta
146a0 74 65 2c 20 62 75 74 20 61 6e 20 49 4f 20 6f 72 te, but an IO or
146b0 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 .** malloc error
146c0 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 occurs during a
146d0 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e 20 rollback, then
146e0 74 68 69 73 20 77 69 6c 6c 20 69 74 73 65 6c 66 this will itself
146f0 20 63 61 75 73 65 20 0a 2a 2a 20 74 68 65 20 70 cause .** the p
14700 61 67 65 72 20 74 6f 20 65 6e 74 65 72 20 74 68 ager to enter th
14710 65 20 45 52 52 4f 52 20 73 74 61 74 65 2e 20 57 e ERROR state. W
14720 68 69 63 68 20 77 69 6c 6c 20 62 65 20 63 6c 65 hich will be cle
14730 61 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 ared by the.** c
14740 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c all to pager_unl
14750 6f 63 6b 28 29 2c 20 61 73 20 64 65 73 63 72 69 ock(), as descri
14760 62 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 bed above..*/.st
14770 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 55 atic void pagerU
14780 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b nlockAndRollback
14790 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
147a0 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 . if( pPager->e
147b0 53 74 61 74 65 21 3d 50 41 47 45 52 5f 45 52 52 State!=PAGER_ERR
147c0 4f 52 20 26 26 20 70 50 61 67 65 72 2d 3e 65 53 OR && pPager->eS
147d0 74 61 74 65 21 3d 50 41 47 45 52 5f 4f 50 45 4e tate!=PAGER_OPEN
147e0 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
147f0 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 assert_pager_sta
14800 74 65 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 te(pPager) );.
14810 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 53 if( pPager->eS
14820 74 61 74 65 3e 3d 50 41 47 45 52 5f 57 52 49 54 tate>=PAGER_WRIT
14830 45 52 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20 20 20 ER_LOCKED ){.
14840 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 sqlite3BeginB
14850 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 enignMalloc();.
14860 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
14870 72 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 rRollback(pPager
14880 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
14890 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 EndBenignMalloc(
148a0 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 );. }else if(
148b0 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 !pPager->exclus
148c0 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 20 iveMode ){.
148d0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
148e0 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 >eState==PAGER_R
148f0 45 41 44 45 52 20 29 3b 0a 20 20 20 20 20 20 70 EADER );. p
14900 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 ager_end_transac
14910 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 30 2c 20 tion(pPager, 0,
14920 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0);. }. }.
14930 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 pager_unlock(pPa
14940 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 ger);.}../*.** P
14950 61 72 61 6d 65 74 65 72 20 61 44 61 74 61 20 6d arameter aData m
14960 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 ust point to a b
14970 75 66 66 65 72 20 6f 66 20 70 50 61 67 65 72 2d uffer of pPager-
14980 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 0a >pageSize bytes.
14990 2a 2a 20 6f 66 20 64 61 74 61 2e 20 43 6f 6d 70 ** of data. Comp
149a0 75 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 ute and return a
149b0 20 63 68 65 63 6b 73 75 6d 20 62 61 73 65 64 20 checksum based
149c0 6f 6e 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ont the contents
149d0 20 6f 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 of the .** page
149e0 20 6f 66 20 64 61 74 61 20 61 6e 64 20 74 68 65 of data and the
149f0 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f current value o
14a00 66 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 f pPager->cksumI
14a10 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 nit..**.** This
14a20 69 73 20 6e 6f 74 20 61 20 72 65 61 6c 20 63 68 is not a real ch
14a30 65 63 6b 73 75 6d 2e 20 49 74 20 69 73 20 72 65 ecksum. It is re
14a40 61 6c 6c 79 20 6a 75 73 74 20 74 68 65 20 73 75 ally just the su
14a50 6d 20 6f 66 20 74 68 65 20 0a 2a 2a 20 72 61 6e m of the .** ran
14a60 64 6f 6d 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 dom initial valu
14a70 65 20 28 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d e (pPager->cksum
14a80 49 6e 69 74 29 20 61 6e 64 20 65 76 65 72 79 20 Init) and every
14a90 32 30 30 74 68 20 62 79 74 65 0a 2a 2a 20 6f 66 200th byte.** of
14aa0 20 74 68 65 20 70 61 67 65 20 64 61 74 61 2c 20 the page data,
14ab0 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 62 79 starting with by
14ac0 74 65 20 6f 66 66 73 65 74 20 28 70 50 61 67 65 te offset (pPage
14ad0 72 2d 3e 70 61 67 65 53 69 7a 65 25 32 30 30 29 r->pageSize%200)
14ae0 2e 0a 2a 2a 20 45 61 63 68 20 62 79 74 65 20 69 ..** Each byte i
14af0 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 s interpreted as
14b00 20 61 6e 20 38 2d 62 69 74 20 75 6e 73 69 67 6e an 8-bit unsign
14b10 65 64 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a ed integer..**.*
14b20 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20 66 * Changing the f
14b30 6f 72 6d 75 6c 61 20 75 73 65 64 20 74 6f 20 63 ormula used to c
14b40 6f 6d 70 75 74 65 20 74 68 69 73 20 63 68 65 63 ompute this chec
14b50 6b 73 75 6d 20 72 65 73 75 6c 74 73 20 69 6e 20 ksum results in
14b60 61 6e 0a 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 62 an.** incompatib
14b70 6c 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 le journal file
14b80 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 format..**.** If
14b90 20 6a 6f 75 72 6e 61 6c 20 63 6f 72 72 75 70 74 journal corrupt
14ba0 69 6f 6e 20 6f 63 63 75 72 73 20 64 75 65 20 74 ion occurs due t
14bb0 6f 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 o a power failur
14bc0 65 2c 20 74 68 65 20 6d 6f 73 74 20 6c 69 6b 65 e, the most like
14bd0 6c 79 20 0a 2a 2a 20 73 63 65 6e 61 72 69 6f 20 ly .** scenario
14be0 69 73 20 74 68 61 74 20 6f 6e 65 20 65 6e 64 20 is that one end
14bf0 6f 72 20 74 68 65 20 6f 74 68 65 72 20 6f 66 20 or the other of
14c00 74 68 65 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 the record will
14c10 62 65 20 63 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 be changed. .**
14c20 49 74 20 69 73 20 6d 75 63 68 20 6c 65 73 73 20 It is much less
14c30 6c 69 6b 65 6c 79 20 74 68 61 74 20 74 68 65 20 likely that the
14c40 74 77 6f 20 65 6e 64 73 20 6f 66 20 74 68 65 20 two ends of the
14c50 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 20 77 journal record w
14c60 69 6c 6c 20 62 65 0a 2a 2a 20 63 6f 72 72 65 63 ill be.** correc
14c70 74 20 61 6e 64 20 74 68 65 20 6d 69 64 64 6c 65 t and the middle
14c80 20 62 65 20 63 6f 72 72 75 70 74 2e 20 20 54 68 be corrupt. Th
14c90 75 73 2c 20 74 68 69 73 20 22 63 68 65 63 6b 73 us, this "checks
14ca0 75 6d 22 20 73 63 68 65 6d 65 2c 0a 2a 2a 20 74 um" scheme,.** t
14cb0 68 6f 75 67 68 20 66 61 73 74 20 61 6e 64 20 73 hough fast and s
14cc0 69 6d 70 6c 65 2c 20 63 61 74 63 68 65 73 20 74 imple, catches t
14cd0 68 65 20 6d 6f 73 74 6c 79 20 6c 69 6b 65 6c 79 he mostly likely
14ce0 20 6b 69 6e 64 20 6f 66 20 63 6f 72 72 75 70 74 kind of corrupt
14cf0 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 ion..*/.static u
14d00 33 32 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 50 32 pager_cksum(P
14d10 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f ager *pPager, co
14d20 6e 73 74 20 75 38 20 2a 61 44 61 74 61 29 7b 0a nst u8 *aData){.
14d30 20 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 70 50 u32 cksum = pP
14d40 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 3b ager->cksumInit;
14d50 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 /* Chec
14d60 6b 73 75 6d 20 76 61 6c 75 65 20 74 6f 20 72 65 ksum value to re
14d70 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 20 turn */. int i
14d80 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 = pPager->pageSi
14d90 7a 65 2d 32 30 30 3b 20 20 20 20 20 20 20 20 20 ze-200;
14da0 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
14db0 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 */. while( i>0
14dc0 20 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d ){. cksum +=
14dd0 20 61 44 61 74 61 5b 69 5d 3b 0a 20 20 20 20 69 aData[i];. i
14de0 20 2d 3d 20 32 30 30 3b 0a 20 20 7d 0a 20 20 72 -= 200;. }. r
14df0 65 74 75 72 6e 20 63 6b 73 75 6d 3b 0a 7d 0a 0a eturn cksum;.}..
14e00 2f 2a 0a 2a 2a 20 52 65 70 6f 72 74 20 74 68 65 /*.** Report the
14e10 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 73 69 current page si
14e20 7a 65 20 61 6e 64 20 6e 75 6d 62 65 72 20 6f 66 ze and number of
14e30 20 72 65 73 65 72 76 65 64 20 62 79 74 65 73 20 reserved bytes
14e40 62 61 63 6b 0a 2a 2a 20 74 6f 20 74 68 65 20 63 back.** to the c
14e50 6f 64 65 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 odec..*/.#ifdef
14e60 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 SQLITE_HAS_CODEC
14e70 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 .static void pag
14e80 65 72 52 65 70 6f 72 74 53 69 7a 65 28 50 61 67 erReportSize(Pag
14e90 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 er *pPager){. i
14ea0 66 28 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 f( pPager->xCode
14eb0 63 53 69 7a 65 43 68 6e 67 20 29 7b 0a 20 20 20 cSizeChng ){.
14ec0 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 53 pPager->xCodecS
14ed0 69 7a 65 43 68 6e 67 28 70 50 61 67 65 72 2d 3e izeChng(pPager->
14ee0 70 43 6f 64 65 63 2c 20 70 50 61 67 65 72 2d 3e pCodec, pPager->
14ef0 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 pageSize,.
14f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14f10 20 20 20 20 20 28 69 6e 74 29 70 50 61 67 65 72 (int)pPager
14f20 2d 3e 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 7d ->nReserve);. }
14f30 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e .}.#else.# defin
14f40 65 20 70 61 67 65 72 52 65 70 6f 72 74 53 69 7a e pagerReportSiz
14f50 65 28 58 29 20 20 20 20 20 2f 2a 20 4e 6f 2d 6f e(X) /* No-o
14f60 70 20 69 66 20 77 65 20 64 6f 20 6e 6f 74 20 73 p if we do not s
14f70 75 70 70 6f 72 74 20 61 20 63 6f 64 65 63 20 2a upport a codec *
14f80 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 /.#endif../*.**
14f90 52 65 61 64 20 61 20 73 69 6e 67 6c 65 20 70 61 Read a single pa
14fa0 67 65 20 66 72 6f 6d 20 65 69 74 68 65 72 20 74 ge from either t
14fb0 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
14fc0 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d (if isMainJrnl==
14fd0 31 29 20 6f 72 0a 2a 2a 20 66 72 6f 6d 20 74 68 1) or.** from th
14fe0 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 28 69 e sub-journal (i
14ff0 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 30 29 f isMainJrnl==0)
15000 20 61 6e 64 20 70 6c 61 79 62 61 63 6b 20 74 68 and playback th
15010 61 74 20 70 61 67 65 2e 0a 2a 2a 20 54 68 65 20 at page..** The
15020 70 61 67 65 20 62 65 67 69 6e 73 20 61 74 20 6f page begins at o
15030 66 66 73 65 74 20 2a 70 4f 66 66 73 65 74 20 69 ffset *pOffset i
15040 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 54 68 nto the file. Th
15050 65 20 2a 70 4f 66 66 73 65 74 0a 2a 2a 20 76 61 e *pOffset.** va
15060 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 65 64 lue is increased
15070 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 to the start of
15080 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 the next page i
15090 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a n the journal..*
150a0 2a 0a 2a 2a 20 54 68 65 20 6d 61 69 6e 20 72 6f *.** The main ro
150b0 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 75 llback journal u
150c0 73 65 73 20 63 68 65 63 6b 73 75 6d 73 20 2d 20 ses checksums -
150d0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f the statement jo
150e0 75 72 6e 61 6c 20 64 6f 65 73 20 0a 2a 2a 20 6e urnal does .** n
150f0 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ot..**.** If the
15100 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
15110 74 68 65 20 70 61 67 65 20 72 65 63 6f 72 64 20 the page record
15120 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73 read from the (s
15130 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ub-)journal file
15140 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20 74 .** is greater t
15150 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 han the current
15160 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 value of Pager.d
15170 62 53 69 7a 65 2c 20 74 68 65 6e 20 70 6c 61 79 bSize, then play
15180 62 61 63 6b 20 69 73 0a 2a 2a 20 73 6b 69 70 70 back is.** skipp
15190 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b ed and SQLITE_OK
151a0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
151b0 0a 2a 2a 20 49 66 20 70 44 6f 6e 65 20 69 73 20 .** If pDone is
151c0 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 not NULL, then i
151d0 74 20 69 73 20 61 20 72 65 63 6f 72 64 20 6f 66 t is a record of
151e0 20 70 61 67 65 73 20 74 68 61 74 20 68 61 76 65 pages that have
151f0 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65 6e already.** been
15200 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 20 49 played back. I
15210 66 20 74 68 65 20 70 61 67 65 20 61 74 20 2a 70 f the page at *p
15220 4f 66 66 73 65 74 20 68 61 73 20 61 6c 72 65 61 Offset has alrea
15230 64 79 20 62 65 65 6e 20 70 6c 61 79 65 64 20 62 dy been played b
15240 61 63 6b 0a 2a 2a 20 28 69 66 20 74 68 65 20 63 ack.** (if the c
15250 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 44 6f orresponding pDo
15260 6e 65 20 62 69 74 20 69 73 20 73 65 74 29 20 74 ne bit is set) t
15270 68 65 6e 20 73 6b 69 70 20 74 68 65 20 70 6c 61 hen skip the pla
15280 79 62 61 63 6b 2e 0a 2a 2a 20 4d 61 6b 65 20 73 yback..** Make s
15290 75 72 65 20 74 68 65 20 70 44 6f 6e 65 20 62 69 ure the pDone bi
152a0 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 t corresponding
152b0 74 6f 20 74 68 65 20 2a 70 4f 66 66 73 65 74 20 to the *pOffset
152c0 70 61 67 65 20 69 73 20 73 65 74 0a 2a 2a 20 70 page is set.** p
152d0 72 69 6f 72 20 74 6f 20 72 65 74 75 72 6e 69 6e rior to returnin
152e0 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 g..**.** If the
152f0 70 61 67 65 20 72 65 63 6f 72 64 20 69 73 20 73 page record is s
15300 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65 61 64 uccessfully read
15310 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 from the (sub-)
15320 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 journal file.**
15330 61 6e 64 20 70 6c 61 79 65 64 20 62 61 63 6b 2c and played back,
15340 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 then SQLITE_OK
15350 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 is returned. If
15360 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 an IO error occu
15370 72 73 0a 2a 2a 20 77 68 69 6c 65 20 72 65 61 64 rs.** while read
15380 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 66 ing the record f
15390 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f rom the (sub-)jo
153a0 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 77 68 urnal file or wh
153b0 69 6c 65 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 ile writing.** t
153c0 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
153d0 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 49 4f ile, then the IO
153e0 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
153f0 65 74 75 72 6e 65 64 2e 20 49 66 20 64 61 74 61 eturned. If data
15400 0a 2a 2a 20 69 73 20 73 75 63 63 65 73 73 66 75 .** is successfu
15410 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20 74 68 lly read from th
15420 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 e (sub-)journal
15430 66 69 6c 65 20 62 75 74 20 61 70 70 65 61 72 73 file but appears
15440 20 74 6f 20 62 65 0a 2a 2a 20 63 6f 72 72 75 70 to be.** corrup
15450 74 65 64 2c 20 53 51 4c 49 54 45 5f 44 4f 4e 45 ted, SQLITE_DONE
15460 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 44 61 is returned. Da
15470 74 61 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 ta is considered
15480 20 63 6f 72 72 75 70 74 65 64 20 69 6e 0a 2a 2a corrupted in.**
15490 20 74 77 6f 20 63 69 72 63 75 6d 73 74 61 6e 63 two circumstanc
154a0 65 73 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 2a 20 49 es:.** .** * I
154b0 66 20 74 68 65 20 72 65 63 6f 72 64 20 70 61 67 f the record pag
154c0 65 2d 6e 75 6d 62 65 72 20 69 73 20 69 6c 6c 65 e-number is ille
154d0 67 61 6c 20 28 30 20 6f 72 20 50 41 47 45 52 5f gal (0 or PAGER_
154e0 4d 4a 5f 50 47 4e 4f 29 2c 20 6f 72 0a 2a 2a 20 MJ_PGNO), or.**
154f0 20 20 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 * If the recor
15500 64 20 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 d is being rolle
15510 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 d back from the
15520 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c main journal fil
15530 65 0a 2a 2a 20 20 20 20 20 61 6e 64 20 74 68 65 e.** and the
15540 20 63 68 65 63 6b 73 75 6d 20 66 69 65 6c 64 20 checksum field
15550 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 does not match t
15560 68 65 20 72 65 63 6f 72 64 20 63 6f 6e 74 65 6e he record conten
15570 74 2e 0a 2a 2a 0a 2a 2a 20 4e 65 69 74 68 65 72 t..**.** Neither
15580 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 73 63 of these two sc
15590 65 6e 61 72 69 6f 73 20 61 72 65 20 70 6f 73 73 enarios are poss
155a0 69 62 6c 65 20 64 75 72 69 6e 67 20 61 20 73 61 ible during a sa
155b0 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b vepoint rollback
155c0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 ..**.** If this
155d0 69 73 20 61 20 73 61 76 65 70 6f 69 6e 74 20 72 is a savepoint r
155e0 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e 20 6d 65 ollback, then me
155f0 6d 6f 72 79 20 6d 61 79 20 68 61 76 65 20 74 6f mory may have to
15600 20 62 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a be dynamically.
15610 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 ** allocated by
15620 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 49 this function. I
15630 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61 f this is the ca
15640 73 65 20 61 6e 64 20 61 6e 20 61 6c 6c 6f 63 61 se and an alloca
15650 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a 2a 20 53 tion fails,.** S
15660 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 QLITE_NOMEM is r
15670 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 eturned..*/.stat
15680 69 63 20 69 6e 74 20 70 61 67 65 72 5f 70 6c 61 ic int pager_pla
15690 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 0a yback_one_page(.
156a0 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c Pager *pPager,
156b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
156c0 2f 2a 20 54 68 65 20 70 61 67 65 72 20 62 65 69 /* The pager bei
156d0 6e 67 20 70 6c 61 79 65 64 20 62 61 63 6b 20 2a ng played back *
156e0 2f 0a 20 20 69 36 34 20 2a 70 4f 66 66 73 65 74 /. i64 *pOffset
156f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
15700 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 72 /* Offset of r
15710 65 63 6f 72 64 20 74 6f 20 70 6c 61 79 62 61 63 ecord to playbac
15720 6b 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 k */. Bitvec *p
15730 44 6f 6e 65 2c 20 20 20 20 20 20 20 20 20 20 20 Done,
15740 20 20 20 20 20 2f 2a 20 42 69 74 76 65 63 20 6f /* Bitvec o
15750 66 20 70 61 67 65 73 20 61 6c 72 65 61 64 79 20 f pages already
15760 70 6c 61 79 65 64 20 62 61 63 6b 20 2a 2f 0a 20 played back */.
15770 20 69 6e 74 20 69 73 4d 61 69 6e 4a 72 6e 6c 2c int isMainJrnl,
15780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
15790 2a 20 31 20 2d 3e 20 6d 61 69 6e 20 6a 6f 75 72 * 1 -> main jour
157a0 6e 61 6c 2e 20 30 20 2d 3e 20 73 75 62 2d 6a 6f nal. 0 -> sub-jo
157b0 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 69 6e 74 20 urnal. */. int
157c0 69 73 53 61 76 65 70 6e 74 20 20 20 20 20 20 20 isSavepnt
157d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
157e0 65 20 66 6f 72 20 61 20 73 61 76 65 70 6f 69 6e e for a savepoin
157f0 74 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 29 7b t rollback */.){
15800 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 48 . int rc;. PgH
15810 64 72 20 2a 70 50 67 3b 20 20 20 20 20 20 20 20 dr *pPg;
15820 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e /* An
15830 20 65 78 69 73 74 69 6e 67 20 70 61 67 65 20 69 existing page i
15840 6e 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 20 n the cache */.
15850 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 Pgno pgno;
15860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
15870 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 * The page numbe
15880 72 20 6f 66 20 61 20 70 61 67 65 20 69 6e 20 6a r of a page in j
15890 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 ournal */. u32
158a0 63 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 cksum;
158b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 /* Che
158c0 63 6b 73 75 6d 20 75 73 65 64 20 66 6f 72 20 73 cksum used for s
158d0 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a anity checking *
158e0 2f 0a 20 20 63 68 61 72 20 2a 61 44 61 74 61 3b /. char *aData;
158f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15900 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 /* Temporary s
15910 74 6f 72 61 67 65 20 66 6f 72 20 74 68 65 20 70 torage for the p
15920 61 67 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 age */. sqlite3
15930 5f 66 69 6c 65 20 2a 6a 66 64 3b 20 20 20 20 20 _file *jfd;
15940 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 /* The fi
15950 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f le descriptor fo
15960 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 r the journal fi
15970 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 53 79 le */. int isSy
15980 6e 63 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 nced;
15990 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
159a0 20 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 69 73 journal page is
159b0 20 73 79 6e 63 65 64 20 2a 2f 0a 0a 20 20 61 73 synced */.. as
159c0 73 65 72 74 28 20 28 69 73 4d 61 69 6e 4a 72 6e sert( (isMainJrn
159d0 6c 26 7e 31 29 3d 3d 30 20 29 3b 20 20 20 20 20 l&~1)==0 );
159e0 20 2f 2a 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 69 /* isMainJrnl i
159f0 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 61 73 s 0 or 1 */. as
15a00 73 65 72 74 28 20 28 69 73 53 61 76 65 70 6e 74 sert( (isSavepnt
15a10 26 7e 31 29 3d 3d 30 20 29 3b 20 20 20 20 20 20 &~1)==0 );
15a20 20 2f 2a 20 69 73 53 61 76 65 70 6e 74 20 69 73 /* isSavepnt is
15a30 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 61 73 73 0 or 1 */. ass
15a40 65 72 74 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 ert( isMainJrnl
15a50 7c 7c 20 70 44 6f 6e 65 20 29 3b 20 20 20 20 20 || pDone );
15a60 2f 2a 20 70 44 6f 6e 65 20 61 6c 77 61 79 73 20 /* pDone always
15a70 75 73 65 64 20 6f 6e 20 73 75 62 2d 6a 6f 75 72 used on sub-jour
15a80 6e 61 6c 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 nals */. assert
15a90 28 20 69 73 53 61 76 65 70 6e 74 20 7c 7c 20 70 ( isSavepnt || p
15aa0 44 6f 6e 65 3d 3d 30 20 29 3b 20 20 20 2f 2a 20 Done==0 ); /*
15ab0 70 44 6f 6e 65 20 6e 65 76 65 72 20 75 73 65 64 pDone never used
15ac0 20 6f 6e 20 6e 6f 6e 2d 73 61 76 65 70 6f 69 6e on non-savepoin
15ad0 74 20 2a 2f 0a 0a 20 20 61 44 61 74 61 20 3d 20 t */.. aData =
15ae0 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 pPager->pTmpSpac
15af0 65 3b 0a 20 20 61 73 73 65 72 74 28 20 61 44 61 e;. assert( aDa
15b00 74 61 20 29 3b 20 20 20 20 20 20 20 20 20 2f 2a ta ); /*
15b10 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20 6d 75 Temp storage mu
15b20 73 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 st have already
15b30 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a been allocated *
15b40 2f 0a 20 20 61 73 73 65 72 74 28 20 70 61 67 65 /. assert( page
15b50 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 3d rUseWal(pPager)=
15b60 3d 30 20 7c 7c 20 28 21 69 73 4d 61 69 6e 4a 72 =0 || (!isMainJr
15b70 6e 6c 20 26 26 20 69 73 53 61 76 65 70 6e 74 29 nl && isSavepnt)
15b80 20 29 3b 0a 0a 20 20 2f 2a 20 45 69 74 68 65 72 );.. /* Either
15b90 20 74 68 65 20 73 74 61 74 65 20 69 73 20 67 72 the state is gr
15ba0 65 61 74 65 72 20 74 68 61 6e 20 50 41 47 45 52 eater than PAGER
15bb0 5f 57 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 _WRITER_CACHEMOD
15bc0 20 28 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 (a transaction
15bd0 0a 20 20 2a 2a 20 6f 72 20 73 61 76 65 70 6f 69 . ** or savepoi
15be0 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 64 6f 6e 65 nt rollback done
15bf0 20 61 74 20 74 68 65 20 72 65 71 75 65 73 74 20 at the request
15c00 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 29 20 6f of the caller) o
15c10 72 20 74 68 69 73 20 69 73 0a 20 20 2a 2a 20 61 r this is. ** a
15c20 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c hot-journal rol
15c30 6c 62 61 63 6b 2e 20 49 66 20 69 74 20 69 73 20 lback. If it is
15c40 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f a hot-journal ro
15c50 6c 6c 62 61 63 6b 2c 20 74 68 65 20 70 61 67 65 llback, the page
15c60 72 0a 20 20 2a 2a 20 69 73 20 69 6e 20 73 74 61 r. ** is in sta
15c70 74 65 20 4f 50 45 4e 20 61 6e 64 20 68 6f 6c 64 te OPEN and hold
15c80 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c s an EXCLUSIVE l
15c90 6f 63 6b 2e 20 48 6f 74 2d 6a 6f 75 72 6e 61 6c ock. Hot-journal
15ca0 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6f rollback. ** o
15cb0 6e 6c 79 20 72 65 61 64 73 20 66 72 6f 6d 20 74 nly reads from t
15cc0 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 2c he main journal,
15cd0 20 6e 6f 74 20 74 68 65 20 73 75 62 2d 6a 6f 75 not the sub-jou
15ce0 72 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 rnal.. */. ass
15cf0 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 ert( pPager->eSt
15d00 61 74 65 3e 3d 50 41 47 45 52 5f 57 52 49 54 45 ate>=PAGER_WRITE
15d10 52 5f 43 41 43 48 45 4d 4f 44 0a 20 20 20 20 20 R_CACHEMOD.
15d20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 65 53 || (pPager->eS
15d30 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45 4e tate==PAGER_OPEN
15d40 20 26 26 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 && pPager->eLoc
15d50 6b 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 k==EXCLUSIVE_LOC
15d60 4b 29 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 74 K). );. assert
15d70 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 ( pPager->eState
15d80 3e 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 43 >=PAGER_WRITER_C
15d90 41 43 48 45 4d 4f 44 20 7c 7c 20 69 73 4d 61 69 ACHEMOD || isMai
15da0 6e 4a 72 6e 6c 20 29 3b 0a 0a 20 20 2f 2a 20 52 nJrnl );.. /* R
15db0 65 61 64 20 74 68 65 20 70 61 67 65 20 6e 75 6d ead the page num
15dc0 62 65 72 20 61 6e 64 20 70 61 67 65 20 64 61 74 ber and page dat
15dd0 61 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e a from the journ
15de0 61 6c 20 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61 al or sub-journa
15df0 6c 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 52 65 74 l. ** file. Ret
15e00 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 urn an error cod
15e10 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 e to the caller
15e20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f if an IO error o
15e30 63 63 75 72 73 2e 0a 20 20 2a 2f 0a 20 20 6a 66 ccurs.. */. jf
15e40 64 20 3d 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 3f d = isMainJrnl ?
15e50 20 70 50 61 67 65 72 2d 3e 6a 66 64 20 3a 20 70 pPager->jfd : p
15e60 50 61 67 65 72 2d 3e 73 6a 66 64 3b 0a 20 20 72 Pager->sjfd;. r
15e70 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a c = read32bits(j
15e80 66 64 2c 20 2a 70 4f 66 66 73 65 74 2c 20 26 70 fd, *pOffset, &p
15e90 67 6e 6f 29 3b 0a 20 20 69 66 28 20 72 63 21 3d gno);. if( rc!=
15ea0 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 SQLITE_OK ) retu
15eb0 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 rn rc;. rc = sq
15ec0 6c 69 74 65 33 4f 73 52 65 61 64 28 6a 66 64 2c lite3OsRead(jfd,
15ed0 20 28 75 38 2a 29 61 44 61 74 61 2c 20 70 50 61 (u8*)aData, pPa
15ee0 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 ger->pageSize, (
15ef0 2a 70 4f 66 66 73 65 74 29 2b 34 29 3b 0a 20 20 *pOffset)+4);.
15f00 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
15f10 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 K ) return rc;.
15f20 20 2a 70 4f 66 66 73 65 74 20 2b 3d 20 70 50 61 *pOffset += pPa
15f30 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 ger->pageSize +
15f40 34 20 2b 20 69 73 4d 61 69 6e 4a 72 6e 6c 2a 34 4 + isMainJrnl*4
15f50 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 ;.. /* Sanity c
15f60 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 70 hecking on the p
15f70 61 67 65 2e 20 20 54 68 69 73 20 69 73 20 6d 6f age. This is mo
15f80 72 65 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 re important tha
15f90 74 20 49 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 t I originally.
15fa0 20 2a 2a 20 74 68 6f 75 67 68 74 2e 20 20 49 66 ** thought. If
15fb0 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 a power failure
15fc0 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 68 occurs while th
15fd0 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69 e journal is bei
15fe0 6e 67 20 77 72 69 74 74 65 6e 2c 0a 20 20 2a 2a ng written,. **
15ff0 20 69 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 it could cause
16000 69 6e 76 61 6c 69 64 20 64 61 74 61 20 74 6f 20 invalid data to
16010 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 be written into
16020 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 57 65 the journal. We
16030 20 6e 65 65 64 20 74 6f 0a 20 20 2a 2a 20 64 65 need to. ** de
16040 74 65 63 74 20 74 68 69 73 20 69 6e 76 61 6c 69 tect this invali
16050 64 20 64 61 74 61 20 28 77 69 74 68 20 68 69 67 d data (with hig
16060 68 20 70 72 6f 62 61 62 69 6c 69 74 79 29 20 61 h probability) a
16070 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 20 20 nd ignore it..
16080 2a 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 */. if( pgno==0
16090 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f || pgno==PAGER_
160a0 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 MJ_PGNO(pPager)
160b0 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 ){. assert( !
160c0 69 73 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 20 isSavepnt );.
160d0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 return SQLITE_D
160e0 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 ONE;. }. if( p
160f0 67 6e 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72 gno>(Pgno)pPager
16100 2d 3e 64 62 53 69 7a 65 20 7c 7c 20 73 71 6c 69 ->dbSize || sqli
16110 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 44 te3BitvecTest(pD
16120 6f 6e 65 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 one, pgno) ){.
16130 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
16140 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 OK;. }. if( is
16150 4d 61 69 6e 4a 72 6e 6c 20 29 7b 0a 20 20 20 20 MainJrnl ){.
16160 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
16170 6a 66 64 2c 20 28 2a 70 4f 66 66 73 65 74 29 2d jfd, (*pOffset)-
16180 34 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 20 20 20 4, &cksum);.
16190 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
161a0 72 63 3b 0a 20 20 20 20 69 66 28 20 21 69 73 53 rc;. if( !isS
161b0 61 76 65 70 6e 74 20 26 26 20 70 61 67 65 72 5f avepnt && pager_
161c0 63 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 28 75 cksum(pPager, (u
161d0 38 2a 29 61 44 61 74 61 29 21 3d 63 6b 73 75 6d 8*)aData)!=cksum
161e0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
161f0 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 SQLITE_DONE;.
16200 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 }. }.. /* If
16210 20 74 68 69 73 20 70 61 67 65 20 68 61 73 20 61 this page has a
16220 6c 72 65 61 64 79 20 62 65 65 6e 20 70 6c 61 79 lready been play
16230 65 64 20 62 79 20 62 65 66 6f 72 65 20 64 75 72 ed by before dur
16240 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 0a ing the current.
16250 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 ** rollback, t
16260 68 65 6e 20 64 6f 6e 27 74 20 62 6f 74 68 65 72 hen don't bother
16270 20 74 6f 20 70 6c 61 79 20 69 74 20 62 61 63 6b to play it back
16280 20 61 67 61 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69 again.. */. i
16290 66 28 20 70 44 6f 6e 65 20 26 26 20 28 72 63 20 f( pDone && (rc
162a0 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 = sqlite3BitvecS
162b0 65 74 28 70 44 6f 6e 65 2c 20 70 67 6e 6f 29 29 et(pDone, pgno))
162c0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
162d0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
162e0 7d 0a 0a 20 20 2f 2a 20 57 68 65 6e 20 70 6c 61 }.. /* When pla
162f0 79 69 6e 67 20 62 61 63 6b 20 70 61 67 65 20 31 ying back page 1
16300 2c 20 72 65 73 74 6f 72 65 20 74 68 65 20 6e 52 , restore the nR
16310 65 73 65 72 76 65 20 73 65 74 74 69 6e 67 0a 20 eserve setting.
16320 20 2a 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d */. if( pgno==
16330 31 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 52 65 1 && pPager->nRe
16340 73 65 72 76 65 21 3d 28 28 75 38 2a 29 61 44 61 serve!=((u8*)aDa
16350 74 61 29 5b 32 30 5d 20 29 7b 0a 20 20 20 20 70 ta)[20] ){. p
16360 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 20 Pager->nReserve
16370 3d 20 28 28 75 38 2a 29 61 44 61 74 61 29 5b 32 = ((u8*)aData)[2
16380 30 5d 3b 0a 20 20 20 20 70 61 67 65 72 52 65 70 0];. pagerRep
16390 6f 72 74 53 69 7a 65 28 70 50 61 67 65 72 29 3b ortSize(pPager);
163a0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 . }.. /* If th
163b0 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 43 41 e pager is in CA
163c0 43 48 45 4d 4f 44 20 73 74 61 74 65 2c 20 74 68 CHEMOD state, th
163d0 65 6e 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 en there must be
163e0 20 61 20 63 6f 70 79 20 6f 66 20 74 68 69 73 0a a copy of this.
163f0 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 ** page in the
16400 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 49 6e pager cache. In
16410 20 74 68 69 73 20 63 61 73 65 20 6a 75 73 74 20 this case just
16420 75 70 64 61 74 65 20 74 68 65 20 70 61 67 65 72 update the pager
16430 20 63 61 63 68 65 2c 0a 20 20 2a 2a 20 6e 6f 74 cache,. ** not
16440 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
16450 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73 20 le. The page is
16460 6c 65 66 74 20 6d 61 72 6b 65 64 20 64 69 72 74 left marked dirt
16470 79 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a y in this case..
16480 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 78 63 **. ** An exc
16490 65 70 74 69 6f 6e 20 74 6f 20 74 68 65 20 61 62 eption to the ab
164a0 6f 76 65 20 72 75 6c 65 3a 20 49 66 20 74 68 65 ove rule: If the
164b0 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e 20 database is in
164c0 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 20 2a no-sync mode. *
164d0 2a 20 61 6e 64 20 61 20 70 61 67 65 20 69 73 20 * and a page is
164e0 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61 6e 20 moved during an
164f0 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 incremental vacu
16500 75 6d 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 um then the page
16510 20 6d 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 may. ** not be
16520 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 in the pager ca
16530 63 68 65 2e 20 4c 61 74 65 72 3a 20 69 66 20 61 che. Later: if a
16540 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f 20 malloc() or IO
16550 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 20 20 2a error occurs. *
16560 2a 20 64 75 72 69 6e 67 20 61 20 4d 6f 76 65 70 * during a Movep
16570 61 67 65 28 29 20 63 61 6c 6c 2c 20 74 68 65 6e age() call, then
16580 20 74 68 65 20 70 61 67 65 20 6d 61 79 20 6e 6f the page may no
16590 74 20 62 65 20 69 6e 20 74 68 65 20 63 61 63 68 t be in the cach
165a0 65 0a 20 20 2a 2a 20 65 69 74 68 65 72 2e 20 53 e. ** either. S
165b0 6f 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 o the condition
165c0 64 65 73 63 72 69 62 65 64 20 69 6e 20 74 68 65 described in the
165d0 20 61 62 6f 76 65 20 70 61 72 61 67 72 61 70 68 above paragraph
165e0 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 73 73 is not. ** ass
165f0 65 72 74 28 29 61 62 6c 65 2e 0a 20 20 2a 2a 0a ert()able.. **.
16600 20 20 2a 2a 20 49 66 20 69 6e 20 57 52 49 54 45 ** If in WRITE
16610 52 5f 44 42 4d 4f 44 2c 20 57 52 49 54 45 52 5f R_DBMOD, WRITER_
16620 46 49 4e 49 53 48 45 44 20 6f 72 20 4f 50 45 4e FINISHED or OPEN
16630 20 73 74 61 74 65 2c 20 74 68 65 6e 20 77 65 20 state, then we
16640 75 70 64 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 update the. **
16650 70 61 67 65 72 20 63 61 63 68 65 20 69 66 20 69 pager cache if i
16660 74 20 65 78 69 73 74 73 20 61 6e 64 20 74 68 65 t exists and the
16670 20 6d 61 69 6e 20 66 69 6c 65 2e 20 54 68 65 20 main file. The
16680 70 61 67 65 20 69 73 20 74 68 65 6e 20 6d 61 72 page is then mar
16690 6b 65 64 20 0a 20 20 2a 2a 20 6e 6f 74 20 64 69 ked . ** not di
166a0 72 74 79 2e 20 53 69 6e 63 65 20 74 68 69 73 20 rty. Since this
166b0 63 6f 64 65 20 69 73 20 6f 6e 6c 79 20 65 78 65 code is only exe
166c0 63 75 74 65 64 20 69 6e 20 50 41 47 45 52 5f 4f cuted in PAGER_O
166d0 50 45 4e 20 73 74 61 74 65 20 66 6f 72 0a 20 20 PEN state for.
166e0 2a 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c ** a hot-journal
166f0 20 72 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 69 73 rollback, it is
16700 20 67 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 guaranteed that
16710 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 the page-cache
16720 69 73 20 65 6d 70 74 79 0a 20 20 2a 2a 20 69 66 is empty. ** if
16730 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
16740 20 4f 50 45 4e 20 73 74 61 74 65 2e 0a 20 20 2a OPEN state.. *
16750 2a 0a 20 20 2a 2a 20 54 69 63 6b 65 74 20 23 31 *. ** Ticket #1
16760 31 37 31 3a 20 20 54 68 65 20 73 74 61 74 65 6d 171: The statem
16770 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 ent journal migh
16780 74 20 63 6f 6e 74 61 69 6e 20 70 61 67 65 20 63 t contain page c
16790 6f 6e 74 65 6e 74 20 74 68 61 74 20 69 73 0a 20 ontent that is.
167a0 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 66 72 ** different fr
167b0 6f 6d 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74 om the page cont
167c0 65 6e 74 20 61 74 20 74 68 65 20 73 74 61 72 74 ent at the start
167d0 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 of the transact
167e0 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 69 73 20 6f ion.. ** This o
167f0 63 63 75 72 73 20 77 68 65 6e 20 61 20 70 61 67 ccurs when a pag
16800 65 20 69 73 20 63 68 61 6e 67 65 64 20 70 72 69 e is changed pri
16810 6f 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 or to the start
16820 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 0a 20 of a statement.
16830 20 2a 2a 20 74 68 65 6e 20 63 68 61 6e 67 65 64 ** then changed
16840 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 again within th
16850 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 57 68 e statement. Wh
16860 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 en rolling back
16870 73 75 63 68 20 61 0a 20 20 2a 2a 20 73 74 61 74 such a. ** stat
16880 65 6d 65 6e 74 20 77 65 20 6d 75 73 74 20 6e 6f ement we must no
16890 74 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 6f t write to the o
168a0 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 riginal database
168b0 20 75 6e 6c 65 73 73 20 77 65 20 6b 6e 6f 77 0a unless we know.
168c0 20 20 2a 2a 20 66 6f 72 20 63 65 72 74 61 69 6e ** for certain
168d0 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c 20 70 that original p
168e0 61 67 65 20 63 6f 6e 74 65 6e 74 73 20 61 72 65 age contents are
168f0 20 73 79 6e 63 65 64 20 69 6e 74 6f 20 74 68 65 synced into the
16900 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a 20 main rollback.
16910 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 74 ** journal. Ot
16920 68 65 72 77 69 73 65 2c 20 61 20 70 6f 77 65 72 herwise, a power
16930 20 6c 6f 73 73 20 6d 69 67 68 74 20 6c 65 61 76 loss might leav
16940 65 20 6d 6f 64 69 66 69 65 64 20 64 61 74 61 20 e modified data
16950 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 in the. ** data
16960 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 6f 75 base file withou
16970 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 68 t an entry in th
16980 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
16990 61 6c 20 74 68 61 74 20 63 61 6e 0a 20 20 2a 2a al that can. **
169a0 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 restore the dat
169b0 61 62 61 73 65 20 74 6f 20 69 74 73 20 6f 72 69 abase to its ori
169c0 67 69 6e 61 6c 20 66 6f 72 6d 2e 20 20 54 77 6f ginal form. Two
169d0 20 63 6f 6e 64 69 74 69 6f 6e 73 20 6d 75 73 74 conditions must
169e0 20 62 65 0a 20 20 2a 2a 20 6d 65 74 20 62 65 66 be. ** met bef
169f0 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20 74 ore writing to t
16a00 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
16a10 73 2e 20 28 31 29 20 74 68 65 20 64 61 74 61 62 s. (1) the datab
16a20 61 73 65 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a ase must be. **
16a30 20 6c 6f 63 6b 65 64 2e 20 20 28 32 29 20 77 65 locked. (2) we
16a40 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6f know that the o
16a50 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e riginal page con
16a60 74 65 6e 74 20 69 73 20 66 75 6c 6c 79 20 73 79 tent is fully sy
16a70 6e 63 65 64 0a 20 20 2a 2a 20 69 6e 20 74 68 65 nced. ** in the
16a80 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 65 69 main journal ei
16a90 74 68 65 72 20 62 65 63 61 75 73 65 20 74 68 65 ther because the
16aa0 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 page is not in
16ab0 63 61 63 68 65 20 6f 72 20 65 6c 73 65 0a 20 20 cache or else.
16ac0 2a 2a 20 74 68 65 20 70 61 67 65 20 69 73 20 6d ** the page is m
16ad0 61 72 6b 65 64 20 61 73 20 6e 65 65 64 53 79 6e arked as needSyn
16ae0 63 3d 3d 30 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 c==0.. **. **
16af0 32 30 30 38 2d 30 34 2d 31 34 3a 20 20 57 68 65 2008-04-14: Whe
16b00 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 n attempting to
16b10 76 61 63 75 75 6d 20 61 20 63 6f 72 72 75 70 74 vacuum a corrupt
16b20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
16b30 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f 73 73 69 it. ** is possi
16b40 62 6c 65 20 74 6f 20 66 61 69 6c 20 61 20 73 74 ble to fail a st
16b50 61 74 65 6d 65 6e 74 20 6f 6e 20 61 20 64 61 74 atement on a dat
16b60 61 62 61 73 65 20 74 68 61 74 20 64 6f 65 73 20 abase that does
16b70 6e 6f 74 20 79 65 74 20 65 78 69 73 74 2e 0a 20 not yet exist..
16b80 20 2a 2a 20 44 6f 20 6e 6f 74 20 61 74 74 65 6d ** Do not attem
16b90 70 74 20 74 6f 20 77 72 69 74 65 20 69 66 20 64 pt to write if d
16ba0 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 atabase file has
16bb0 20 6e 65 76 65 72 20 62 65 65 6e 20 6f 70 65 6e never been open
16bc0 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 ed.. */. if( p
16bd0 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 agerUseWal(pPage
16be0 72 29 20 29 7b 0a 20 20 20 20 70 50 67 20 3d 20 r) ){. pPg =
16bf0 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 0;. }else{.
16c00 70 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b pPg = pager_look
16c10 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 up(pPager, pgno)
16c20 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
16c30 70 50 67 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b pPg || !MEMDB );
16c40 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
16c50 72 2d 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52 r->eState!=PAGER
16c60 5f 4f 50 45 4e 20 7c 7c 20 70 50 67 3d 3d 30 20 _OPEN || pPg==0
16c70 29 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 );. PAGERTRACE(
16c80 28 22 50 4c 41 59 42 41 43 4b 20 25 64 20 70 61 ("PLAYBACK %d pa
16c90 67 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29 ge %d hash(%08x)
16ca0 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 %s\n",.
16cb0 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 PAGERID(pPage
16cc0 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f r), pgno, pager_
16cd0 64 61 74 61 68 61 73 68 28 70 50 61 67 65 72 2d datahash(pPager-
16ce0 3e 70 61 67 65 53 69 7a 65 2c 20 28 75 38 2a 29 >pageSize, (u8*)
16cf0 61 44 61 74 61 29 2c 0a 20 20 20 20 20 20 20 20 aData),.
16d00 20 20 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 3f 22 (isMainJrnl?"
16d10 6d 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22 3a 22 73 main-journal":"s
16d20 75 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a 20 20 29 ub-journal"). )
16d30 29 3b 0a 20 20 69 66 28 20 69 73 4d 61 69 6e 4a );. if( isMainJ
16d40 72 6e 6c 20 29 7b 0a 20 20 20 20 69 73 53 79 6e rnl ){. isSyn
16d50 63 65 64 20 3d 20 70 50 61 67 65 72 2d 3e 6e 6f ced = pPager->no
16d60 53 79 6e 63 20 7c 7c 20 28 2a 70 4f 66 66 73 65 Sync || (*pOffse
16d70 74 20 3c 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 t <= pPager->jou
16d80 72 6e 61 6c 48 64 72 29 3b 0a 20 20 7d 65 6c 73 rnalHdr);. }els
16d90 65 7b 0a 20 20 20 20 69 73 53 79 6e 63 65 64 20 e{. isSynced
16da0 3d 20 28 70 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d = (pPg==0 || 0==
16db0 28 70 50 67 2d 3e 66 6c 61 67 73 20 26 20 50 47 (pPg->flags & PG
16dc0 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 3b HDR_NEED_SYNC));
16dd0 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4f 70 65 . }. if( isOpe
16de0 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 0a 20 20 n(pPager->fd).
16df0 20 26 26 20 28 70 50 61 67 65 72 2d 3e 65 53 74 && (pPager->eSt
16e00 61 74 65 3e 3d 50 41 47 45 52 5f 57 52 49 54 45 ate>=PAGER_WRITE
16e10 52 5f 44 42 4d 4f 44 20 7c 7c 20 70 50 61 67 65 R_DBMOD || pPage
16e20 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 r->eState==PAGER
16e30 5f 4f 50 45 4e 29 0a 20 20 20 26 26 20 69 73 53 _OPEN). && isS
16e40 79 6e 63 65 64 0a 20 20 29 7b 0a 20 20 20 20 69 ynced. ){. i
16e50 36 34 20 6f 66 73 74 20 3d 20 28 70 67 6e 6f 2d 64 ofst = (pgno-
16e60 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 1)*(i64)pPager->
16e70 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 74 65 pageSize;. te
16e80 73 74 63 61 73 65 28 20 21 69 73 53 61 76 65 70 stcase( !isSavep
16e90 6e 74 20 26 26 20 70 50 67 21 3d 30 20 26 26 20 nt && pPg!=0 &&
16ea0 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 (pPg->flags&PGHD
16eb0 52 5f 4e 45 45 44 5f 53 59 4e 43 29 21 3d 30 20 R_NEED_SYNC)!=0
16ec0 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 );. assert( !
16ed0 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 pagerUseWal(pPag
16ee0 65 72 29 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 er) );. rc =
16ef0 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 sqlite3OsWrite(p
16f00 50 61 67 65 72 2d 3e 66 64 2c 20 28 75 38 2a 29 Pager->fd, (u8*)
16f10 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 aData, pPager->p
16f20 61 67 65 53 69 7a 65 2c 20 6f 66 73 74 29 3b 0a ageSize, ofst);.
16f30 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 if( pgno>pPa
16f40 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 ger->dbFileSize
16f50 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d ){. pPager-
16f60 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d 20 70 67 >dbFileSize = pg
16f70 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 no;. }. if
16f80 28 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 ( pPager->pBacku
16f90 70 20 29 7b 0a 20 20 20 20 20 20 43 4f 44 45 43 p ){. CODEC
16fa0 31 28 70 50 61 67 65 72 2c 20 61 44 61 74 61 2c 1(pPager, aData,
16fb0 20 70 67 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c pgno, 3, rc=SQL
16fc0 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 ITE_NOMEM);.
16fd0 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 sqlite3BackupU
16fe0 70 64 61 74 65 28 70 50 61 67 65 72 2d 3e 70 42 pdate(pPager->pB
16ff0 61 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 28 75 38 ackup, pgno, (u8
17000 2a 29 61 44 61 74 61 29 3b 0a 20 20 20 20 20 20 *)aData);.
17010 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 61 CODEC2(pPager, a
17020 44 61 74 61 2c 20 70 67 6e 6f 2c 20 37 2c 20 72 Data, pgno, 7, r
17030 63 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 c=SQLITE_NOMEM,
17040 61 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 aData);. }.
17050 7d 65 6c 73 65 20 69 66 28 20 21 69 73 4d 61 69 }else if( !isMai
17060 6e 4a 72 6e 6c 20 26 26 20 70 50 67 3d 3d 30 20 nJrnl && pPg==0
17070 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 ){. /* If thi
17080 73 20 69 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 s is a rollback
17090 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 61 of a savepoint a
170a0 6e 64 20 64 61 74 61 20 77 61 73 20 6e 6f 74 20 nd data was not
170b0 77 72 69 74 74 65 6e 20 74 6f 0a 20 20 20 20 2a written to. *
170c0 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 * the database a
170d0 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e nd the page is n
170e0 6f 74 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20 74 68 ot in-memory, th
170f0 65 72 65 20 69 73 20 61 20 70 6f 74 65 6e 74 69 ere is a potenti
17100 61 6c 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 al. ** proble
17110 6d 2e 20 57 68 65 6e 20 74 68 65 20 70 61 67 65 m. When the page
17120 20 69 73 20 6e 65 78 74 20 66 65 74 63 68 65 64 is next fetched
17130 20 62 79 20 74 68 65 20 62 2d 74 72 65 65 20 6c by the b-tree l
17140 61 79 65 72 2c 20 69 74 20 0a 20 20 20 20 2a 2a ayer, it . **
17150 20 77 69 6c 6c 20 62 65 20 72 65 61 64 20 66 72 will be read fr
17160 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
17170 66 69 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20 file, which may
17180 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 0a 20 or may not be .
17190 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 2e 20 0a ** current. .
171a0 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 **. ** Th
171b0 65 72 65 20 61 72 65 20 61 20 63 6f 75 70 6c 65 ere are a couple
171c0 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 77 61 of different wa
171d0 79 73 20 74 68 69 73 20 63 61 6e 20 68 61 70 70 ys this can happ
171e0 65 6e 2e 20 41 6c 6c 20 61 72 65 20 71 75 69 74 en. All are quit
171f0 65 0a 20 20 20 20 2a 2a 20 6f 62 73 63 75 72 65 e. ** obscure
17200 2e 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 . When running i
17210 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f n synchronous mo
17220 64 65 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c de, this can onl
17230 79 20 68 61 70 70 65 6e 20 0a 20 20 20 20 2a 2a y happen . **
17240 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 if the page is
17250 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 on the free-list
17260 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
17270 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
17280 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 70 6f , then. ** po
17290 70 75 6c 61 74 65 64 2c 20 74 68 65 6e 20 6d 6f pulated, then mo
172a0 76 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 ved using sqlite
172b0 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 29 3PagerMovepage()
172c0 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
172d0 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 The solution is
172e0 74 6f 20 61 64 64 20 61 6e 20 69 6e 2d 6d 65 6d to add an in-mem
172f0 6f 72 79 20 70 61 67 65 20 74 6f 20 74 68 65 20 ory page to the
17300 63 61 63 68 65 20 63 6f 6e 74 61 69 6e 69 6e 67 cache containing
17310 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 . ** the data
17320 20 6a 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 just read from
17330 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e the sub-journal.
17340 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 Mark the page a
17350 73 20 64 69 72 74 79 20 0a 20 20 20 20 2a 2a 20 s dirty . **
17360 61 6e 64 20 69 66 20 74 68 65 20 70 61 67 65 72 and if the pager
17370 20 72 65 71 75 69 72 65 73 20 61 20 6a 6f 75 72 requires a jour
17380 6e 61 6c 2d 73 79 6e 63 2c 20 74 68 65 6e 20 6d nal-sync, then m
17390 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 ark the page as
173a0 0a 20 20 20 20 2a 2a 20 72 65 71 75 69 72 69 6e . ** requirin
173b0 67 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 g a journal-sync
173c0 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 77 72 before it is wr
173d0 69 74 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 itten.. */.
173e0 20 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65 assert( isSave
173f0 70 6e 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 pnt );. asser
17400 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 t( pPager->doNot
17410 53 70 69 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 Spill==0 );.
17420 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 70 69 pPager->doNotSpi
17430 6c 6c 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 73 ll++;. rc = s
17440 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 qlite3PagerAcqui
17450 72 65 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 2c re(pPager, pgno,
17460 20 26 70 50 67 2c 20 31 29 3b 0a 20 20 20 20 61 &pPg, 1);. a
17470 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 ssert( pPager->d
17480 6f 4e 6f 74 53 70 69 6c 6c 3d 3d 31 20 29 3b 0a oNotSpill==1 );.
17490 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f pPager->doNo
174a0 74 53 70 69 6c 6c 2d 2d 3b 0a 20 20 20 20 69 66 tSpill--;. if
174b0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
174c0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
174d0 20 70 50 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e pPg->flags &= ~
174e0 50 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 44 3b PGHDR_NEED_READ;
174f0 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 . sqlite3Pcac
17500 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29 heMakeDirty(pPg)
17510 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 67 20 ;. }. if( pPg
17520 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 20 70 61 67 ){. /* No pag
17530 65 20 73 68 6f 75 6c 64 20 65 76 65 72 20 62 65 e should ever be
17540 20 65 78 70 6c 69 63 69 74 6c 79 20 72 6f 6c 6c explicitly roll
17550 65 64 20 62 61 63 6b 20 74 68 61 74 20 69 73 20 ed back that is
17560 69 6e 20 75 73 65 2c 20 65 78 63 65 70 74 0a 20 in use, except.
17570 20 20 20 2a 2a 20 66 6f 72 20 70 61 67 65 20 31 ** for page 1
17580 20 77 68 69 63 68 20 69 73 20 68 65 6c 64 20 69 which is held i
17590 6e 20 75 73 65 20 69 6e 20 6f 72 64 65 72 20 74 n use in order t
175a0 6f 20 6b 65 65 70 20 74 68 65 20 6c 6f 63 6b 20 o keep the lock
175b0 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 on the. ** da
175c0 74 61 62 61 73 65 20 61 63 74 69 76 65 2e 20 48 tabase active. H
175d0 6f 77 65 76 65 72 20 73 75 63 68 20 61 20 70 61 owever such a pa
175e0 67 65 20 6d 61 79 20 62 65 20 72 6f 6c 6c 65 64 ge may be rolled
175f0 20 62 61 63 6b 20 61 73 20 61 20 72 65 73 75 6c back as a resul
17600 74 0a 20 20 20 20 2a 2a 20 6f 66 20 61 6e 20 69 t. ** of an i
17610 6e 74 65 72 6e 61 6c 20 65 72 72 6f 72 20 72 65 nternal error re
17620 73 75 6c 74 69 6e 67 20 69 6e 20 61 6e 20 61 75 sulting in an au
17630 74 6f 6d 61 74 69 63 20 63 61 6c 6c 20 74 6f 0a tomatic call to.
17640 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 ** sqlite3Pa
17650 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 2e 0a 20 gerRollback()..
17660 20 20 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a */. void *
17670 70 44 61 74 61 3b 0a 20 20 20 20 70 44 61 74 61 pData;. pData
17680 20 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 = pPg->pData;.
17690 20 20 20 6d 65 6d 63 70 79 28 70 44 61 74 61 2c memcpy(pData,
176a0 20 28 75 38 2a 29 61 44 61 74 61 2c 20 70 50 61 (u8*)aData, pPa
176b0 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a ger->pageSize);.
176c0 20 20 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 pPager->xRei
176d0 6e 69 74 65 72 28 70 50 67 29 3b 0a 20 20 20 20 niter(pPg);.
176e0 69 66 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 26 if( isMainJrnl &
176f0 26 20 28 21 69 73 53 61 76 65 70 6e 74 20 7c 7c & (!isSavepnt ||
17700 20 2a 70 4f 66 66 73 65 74 3c 3d 70 50 61 67 65 *pOffset<=pPage
17710 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 29 20 29 r->journalHdr) )
17720 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 {. /* If th
17730 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
17740 69 73 20 70 61 67 65 20 77 65 72 65 20 6a 75 73 is page were jus
17750 74 20 72 65 73 74 6f 72 65 64 20 66 72 6f 6d 20 t restored from
17760 74 68 65 20 6d 61 69 6e 20 0a 20 20 20 20 20 20 the main .
17770 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c ** journal file,
17780 20 74 68 65 6e 20 69 74 73 20 63 6f 6e 74 65 6e then its conten
17790 74 20 6d 75 73 74 20 62 65 20 61 73 20 74 68 65 t must be as the
177a0 79 20 77 65 72 65 20 77 68 65 6e 20 74 68 65 20 y were when the
177b0 0a 20 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 . ** transa
177c0 63 74 69 6f 6e 20 77 61 73 20 66 69 72 73 74 20 ction was first
177d0 6f 70 65 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 opened. In this
177e0 63 61 73 65 20 77 65 20 63 61 6e 20 6d 61 72 6b case we can mark
177f0 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 the page.
17800 2a 2a 20 61 73 20 63 6c 65 61 6e 2c 20 73 69 6e ** as clean, sin
17810 63 65 20 74 68 65 72 65 20 77 69 6c 6c 20 62 65 ce there will be
17820 20 6e 6f 20 6e 65 65 64 20 74 6f 20 77 72 69 74 no need to writ
17830 65 20 69 74 20 6f 75 74 20 74 6f 20 74 68 65 0a e it out to the.
17840 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 ** databas
17850 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 e.. **.
17860 20 20 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e ** There is on
17870 65 20 65 78 63 65 70 74 69 6f 6e 20 74 6f 20 74 e exception to t
17880 68 69 73 20 72 75 6c 65 2e 20 49 66 20 74 68 65 his rule. If the
17890 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 72 page is being r
178a0 6f 6c 6c 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 olled. ** b
178b0 61 63 6b 20 61 73 20 70 61 72 74 20 6f 66 20 61 ack as part of a
178c0 20 73 61 76 65 70 6f 69 6e 74 20 28 6f 72 20 73 savepoint (or s
178d0 74 61 74 65 6d 65 6e 74 29 20 72 6f 6c 6c 62 61 tatement) rollba
178e0 63 6b 20 66 72 6f 6d 20 61 6e 20 0a 20 20 20 20 ck from an .
178f0 20 20 2a 2a 20 75 6e 73 79 6e 63 65 64 20 70 6f ** unsynced po
17900 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 61 69 rtion of the mai
17910 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 n journal file,
17920 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 20 73 then it is not s
17930 61 66 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 afe. ** to
17940 6d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 mark the page as
17950 20 63 6c 65 61 6e 2e 20 54 68 69 73 20 69 73 20 clean. This is
17960 62 65 63 61 75 73 65 20 6d 61 72 6b 69 6e 67 20 because marking
17970 74 68 65 20 70 61 67 65 20 61 73 0a 20 20 20 20 the page as.
17980 20 20 2a 2a 20 63 6c 65 61 6e 20 77 69 6c 6c 20 ** clean will
17990 63 6c 65 61 72 20 74 68 65 20 50 47 48 44 52 5f clear the PGHDR_
179a0 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 2e 20 NEED_SYNC flag.
179b0 53 69 6e 63 65 20 74 68 65 20 70 61 67 65 20 69 Since the page i
179c0 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 65 61 s. ** alrea
179d0 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 dy in the journa
179e0 6c 20 66 69 6c 65 20 28 72 65 63 6f 72 64 65 64 l file (recorded
179f0 20 69 6e 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 in Pager.pInJou
17a00 72 6e 61 6c 29 20 61 6e 64 0a 20 20 20 20 20 20 rnal) and.
17a10 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 ** the PGHDR_NEE
17a20 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73 20 63 D_SYNC flag is c
17a30 6c 65 61 72 65 64 2c 20 69 66 20 74 68 65 20 70 leared, if the p
17a40 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 74 age is written t
17a50 6f 0a 20 20 20 20 20 20 2a 2a 20 61 67 61 69 6e o. ** again
17a60 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 72 61 within this tra
17a70 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 77 69 6c nsaction, it wil
17a80 6c 20 62 65 20 6d 61 72 6b 65 64 20 61 73 20 64 l be marked as d
17a90 69 72 74 79 20 62 75 74 0a 20 20 20 20 20 20 2a irty but. *
17aa0 2a 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 * the PGHDR_NEED
17ab0 5f 53 59 4e 43 20 66 6c 61 67 20 77 69 6c 6c 20 _SYNC flag will
17ac0 6e 6f 74 20 62 65 20 73 65 74 2e 20 49 74 20 63 not be set. It c
17ad0 6f 75 6c 64 20 74 68 65 6e 20 70 6f 74 65 6e 74 ould then potent
17ae0 69 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 62 ially. ** b
17af0 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20 69 6e e written out in
17b00 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
17b10 66 69 6c 65 20 62 65 66 6f 72 65 20 69 74 73 20 file before its
17b20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 20 journal file.
17b30 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74 20 69 73 ** segment is
17b40 20 73 79 6e 63 65 64 2e 20 49 66 20 61 20 63 72 synced. If a cr
17b50 61 73 68 20 6f 63 63 75 72 73 20 64 75 72 69 6e ash occurs durin
17b60 67 20 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 g or following t
17b70 68 69 73 2c 0a 20 20 20 20 20 20 2a 2a 20 64 61 his,. ** da
17b80 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f tabase corruptio
17b90 6e 20 6d 61 79 20 65 6e 73 75 65 2e 0a 20 20 20 n may ensue..
17ba0 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 */. asse
17bb0 72 74 28 20 21 70 61 67 65 72 55 73 65 57 61 6c rt( !pagerUseWal
17bc0 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 (pPager) );.
17bd0 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d sqlite3PcacheM
17be0 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 akeClean(pPg);.
17bf0 20 20 20 7d 0a 20 20 20 20 70 61 67 65 72 5f 73 }. pager_s
17c00 65 74 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 et_pagehash(pPg)
17c10 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 ;.. /* If thi
17c20 73 20 77 61 73 20 70 61 67 65 20 31 2c 20 74 68 s was page 1, th
17c30 65 6e 20 72 65 73 74 6f 72 65 20 74 68 65 20 76 en restore the v
17c40 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62 alue of Pager.db
17c50 46 69 6c 65 56 65 72 73 2e 0a 20 20 20 20 2a 2a FileVers.. **
17c60 20 44 6f 20 74 68 69 73 20 62 65 66 6f 72 65 20 Do this before
17c70 61 6e 79 20 64 65 63 6f 64 69 6e 67 2e 20 2a 2f any decoding. */
17c80 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 . if( pgno==1
17c90 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 ){. memcpy
17ca0 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 (&pPager->dbFile
17cb0 56 65 72 73 2c 20 26 28 28 75 38 2a 29 70 44 61 Vers, &((u8*)pDa
17cc0 74 61 29 5b 32 34 5d 2c 73 69 7a 65 6f 66 28 70 ta)[24],sizeof(p
17cd0 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 Pager->dbFileVer
17ce0 73 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 s));. }..
17cf0 2f 2a 20 44 65 63 6f 64 65 20 74 68 65 20 70 61 /* Decode the pa
17d00 67 65 20 6a 75 73 74 20 72 65 61 64 20 66 72 6f ge just read fro
17d10 6d 20 64 69 73 6b 20 2a 2f 0a 20 20 20 20 43 4f m disk */. CO
17d20 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 44 61 DEC1(pPager, pDa
17d30 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 33 ta, pPg->pgno, 3
17d40 2c 20 72 63 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 , rc=SQLITE_NOME
17d50 4d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 M);. sqlite3P
17d60 63 61 63 68 65 52 65 6c 65 61 73 65 28 70 50 67 cacheRelease(pPg
17d70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
17d80 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 rc;.}../*.** Par
17d90 61 6d 65 74 65 72 20 7a 4d 61 73 74 65 72 20 69 ameter zMaster i
17da0 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 s the name of a
17db0 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
17dc0 69 6c 65 2e 20 41 20 73 69 6e 67 6c 65 20 6a 6f ile. A single jo
17dd0 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 74 68 urnal.** file th
17de0 61 74 20 72 65 66 65 72 72 65 64 20 74 6f 20 74 at referred to t
17df0 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
17e00 6c 20 66 69 6c 65 20 68 61 73 20 6a 75 73 74 20 l file has just
17e10 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b been rolled back
17e20 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e ..** This routin
17e30 65 20 63 68 65 63 6b 73 20 69 66 20 69 74 20 69 e checks if it i
17e40 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 s possible to de
17e50 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 lete the master
17e60 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 0a 2a 2a journal file,.**
17e70 20 61 6e 64 20 64 6f 65 73 20 73 6f 20 69 66 20 and does so if
17e80 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 it is..**.** Arg
17e90 75 6d 65 6e 74 20 7a 4d 61 73 74 65 72 20 6d 61 ument zMaster ma
17ea0 79 20 70 6f 69 6e 74 20 74 6f 20 50 61 67 65 72 y point to Pager
17eb0 2e 70 54 6d 70 53 70 61 63 65 2e 20 53 6f 20 74 .pTmpSpace. So t
17ec0 68 61 74 20 62 75 66 66 65 72 20 69 73 20 6e 6f hat buffer is no
17ed0 74 20 0a 2a 2a 20 61 76 61 69 6c 61 62 6c 65 20 t .** available
17ee0 66 6f 72 20 75 73 65 20 77 69 74 68 69 6e 20 74 for use within t
17ef0 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a his function..**
17f00 0a 2a 2a 20 57 68 65 6e 20 61 20 6d 61 73 74 65 .** When a maste
17f10 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 r journal file i
17f20 73 20 63 72 65 61 74 65 64 2c 20 69 74 20 69 73 s created, it is
17f30 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 populated with
17f40 74 68 65 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 the names .** of
17f50 20 61 6c 6c 20 6f 66 20 69 74 73 20 63 68 69 6c all of its chil
17f60 64 20 6a 6f 75 72 6e 61 6c 73 2c 20 6f 6e 65 20 d journals, one
17f70 61 66 74 65 72 20 61 6e 6f 74 68 65 72 2c 20 66 after another, f
17f80 6f 72 6d 61 74 74 65 64 20 61 73 20 75 74 66 2d ormatted as utf-
17f90 38 20 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 74 65 8 .** encoded te
17fa0 78 74 2e 20 54 68 65 20 65 6e 64 20 6f 66 20 65 xt. The end of e
17fb0 61 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 ach child journa
17fc0 6c 20 66 69 6c 65 20 69 73 20 6d 61 72 6b 65 64 l file is marked
17fd0 20 77 69 74 68 20 61 20 0a 2a 2a 20 6e 75 6c 2d with a .** nul-
17fe0 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 20 terminator byte
17ff0 28 30 78 30 30 29 2e 20 69 2e 65 2e 20 74 68 65 (0x00). i.e. the
18000 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 entire contents
18010 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 of a master jou
18020 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 rnal.** file for
18030 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
18040 6e 76 6f 6c 76 69 6e 67 20 74 77 6f 20 64 61 74 nvolving two dat
18050 61 62 61 73 65 73 20 6d 69 67 68 74 20 62 65 3a abases might be:
18060 0a 2a 2a 0a 2a 2a 20 20 20 22 2f 68 6f 6d 65 2f .**.** "/home/
18070 62 69 6c 6c 2f 61 2e 64 62 2d 6a 6f 75 72 6e 61 bill/a.db-journa
18080 6c 5c 78 30 30 2f 68 6f 6d 65 2f 62 69 6c 6c 2f l\x00/home/bill/
18090 62 2e 64 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 b.db-journal\x00
180a0 22 0a 2a 2a 0a 2a 2a 20 41 20 6d 61 73 74 65 72 ".**.** A master
180b0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 journal file ma
180c0 79 20 6f 6e 6c 79 20 62 65 20 64 65 6c 65 74 65 y only be delete
180d0 64 20 6f 6e 63 65 20 61 6c 6c 20 6f 66 20 69 74 d once all of it
180e0 73 20 63 68 69 6c 64 20 0a 2a 2a 20 6a 6f 75 72 s child .** jour
180f0 6e 61 6c 73 20 68 61 76 65 20 62 65 65 6e 20 72 nals have been r
18100 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a olled back..**.*
18110 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
18120 72 65 61 64 73 20 74 68 65 20 63 6f 6e 74 65 6e reads the conten
18130 74 73 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 ts of the master
18140 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e -journal file in
18150 74 6f 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6e to .** memory an
18160 64 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 d loops through
18170 65 61 63 68 20 6f 66 20 74 68 65 20 63 68 69 6c each of the chil
18180 64 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 73 2e d journal names.
18190 20 46 6f 72 0a 2a 2a 20 65 61 63 68 20 63 68 69 For.** each chi
181a0 6c 64 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 63 ld journal, it c
181b0 68 65 63 6b 73 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 hecks if:.**.**
181c0 20 20 2a 20 69 66 20 74 68 65 20 63 68 69 6c 64 * if the child
181d0 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 2c journal exists,
181e0 20 61 6e 64 20 69 66 20 73 6f 0a 2a 2a 20 20 20 and if so.**
181f0 2a 20 69 66 20 74 68 65 20 63 68 69 6c 64 20 6a * if the child j
18200 6f 75 72 6e 61 6c 20 63 6f 6e 74 61 69 6e 73 20 ournal contains
18210 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 6d a reference to m
18220 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 0a 2a aster journal .*
18230 2a 20 20 20 20 20 66 69 6c 65 20 7a 4d 61 73 74 * file zMast
18240 65 72 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 63 68 er.**.** If a ch
18250 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 61 6e 20 ild journal can
18260 62 65 20 66 6f 75 6e 64 20 74 68 61 74 20 6d 61 be found that ma
18270 74 63 68 65 73 20 62 6f 74 68 20 6f 66 20 74 68 tches both of th
18280 65 20 63 72 69 74 65 72 69 61 0a 2a 2a 20 61 62 e criteria.** ab
18290 6f 76 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 ove, this functi
182a0 6f 6e 20 72 65 74 75 72 6e 73 20 77 69 74 68 6f on returns witho
182b0 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e ut doing anythin
182c0 67 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 g. Otherwise, if
182d0 0a 2a 2a 20 6e 6f 20 73 75 63 68 20 63 68 69 6c .** no such chil
182e0 64 20 6a 6f 75 72 6e 61 6c 20 63 61 6e 20 62 65 d journal can be
182f0 20 66 6f 75 6e 64 2c 20 66 69 6c 65 20 7a 4d 61 found, file zMa
18300 73 74 65 72 20 69 73 20 64 65 6c 65 74 65 64 20 ster is deleted
18310 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 66 69 6c 65 from.** the file
18320 2d 73 79 73 74 65 6d 20 75 73 69 6e 67 20 73 71 -system using sq
18330 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 29 2e lite3OsDelete().
18340 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 .**.** If an IO
18350 65 72 72 6f 72 20 77 69 74 68 69 6e 20 74 68 69 error within thi
18360 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 6e 20 65 s function, an e
18370 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
18380 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a 2a 20 66 urned. This.** f
18390 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 unction allocate
183a0 73 20 6d 65 6d 6f 72 79 20 62 79 20 63 61 6c 6c s memory by call
183b0 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f ing sqlite3Mallo
183c0 63 28 29 2e 20 49 66 20 61 6e 20 61 6c 6c 6f 63 c(). If an alloc
183d0 61 74 69 6f 6e 0a 2a 2a 20 66 61 69 6c 73 2c 20 ation.** fails,
183e0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 SQLITE_NOMEM is
183f0 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 returned. Otherw
18400 69 73 65 2c 20 69 66 20 6e 6f 20 49 4f 20 6f 72 ise, if no IO or
18410 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 0a malloc errors .
18420 2a 2a 20 6f 63 63 75 72 2c 20 53 51 4c 49 54 45 ** occur, SQLITE
18430 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e _OK is returned.
18440 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 54 68 69 .**.** TODO: Thi
18450 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 s function alloc
18460 61 74 65 73 20 61 20 73 69 6e 67 6c 65 20 62 6c ates a single bl
18470 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74 6f ock of memory to
18480 20 6c 6f 61 64 0a 2a 2a 20 74 68 65 20 65 6e 74 load.** the ent
18490 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 ire contents of
184a0 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
184b0 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 63 6f al file. This co
184c0 75 6c 64 20 62 65 0a 2a 2a 20 61 20 63 6f 75 70 uld be.** a coup
184d0 6c 65 20 6f 66 20 6b 69 6c 6f 62 79 74 65 73 20 le of kilobytes
184e0 6f 72 20 73 6f 20 2d 20 70 6f 74 65 6e 74 69 61 or so - potentia
184f0 6c 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20 lly larger than
18500 74 68 65 20 70 61 67 65 20 0a 2a 2a 20 73 69 7a the page .** siz
18510 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
18520 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 65 72 pager_delmaster
18530 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
18540 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 const char *zMas
18550 74 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f ter){. sqlite3_
18560 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 vfs *pVfs = pPag
18570 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 6e 74 20 er->pVfs;. int
18580 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
18590 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
185a0 63 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 code */. sqlite
185b0 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 72 3b 3_file *pMaster;
185c0 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 /* Malloc'd
185d0 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 master-journal f
185e0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a ile descriptor *
185f0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 /. sqlite3_file
18600 20 2a 70 4a 6f 75 72 6e 61 6c 3b 20 20 20 2f 2a *pJournal; /*
18610 20 4d 61 6c 6c 6f 63 27 64 20 63 68 69 6c 64 2d Malloc'd child-
18620 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 journal file des
18630 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 63 68 61 criptor */. cha
18640 72 20 2a 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 r *zMasterJourna
18650 6c 20 3d 20 30 3b 20 2f 2a 20 43 6f 6e 74 65 6e l = 0; /* Conten
18660 74 73 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 ts of master jou
18670 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 rnal file */. i
18680 36 34 20 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 64 nMasterJourna
18690 6c 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 l; /* Size
186a0 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e of master journ
186b0 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 al file */. cha
186c0 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 r *zJournal;
186d0 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
186e0 72 20 74 6f 20 6f 6e 65 20 6a 6f 75 72 6e 61 6c r to one journal
186f0 20 77 69 74 68 69 6e 20 4d 4a 20 66 69 6c 65 20 within MJ file
18700 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 */. char *zMast
18710 65 72 50 74 72 3b 20 20 20 20 20 20 20 20 20 2f erPtr; /
18720 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 * Space to hold
18730 4d 4a 20 66 69 6c 65 6e 61 6d 65 20 66 72 6f 6d MJ filename from
18740 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 a journal file
18750 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 */. int nMaster
18760 50 74 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f Ptr; /
18770 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 * Amount of spac
18780 65 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 7a e allocated to z
18790 4d 61 73 74 65 72 50 74 72 5b 5d 20 2a 2f 0a 0a MasterPtr[] */..
187a0 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 /* Allocate sp
187b0 61 63 65 20 66 6f 72 20 62 6f 74 68 20 74 68 65 ace for both the
187c0 20 70 4a 6f 75 72 6e 61 6c 20 61 6e 64 20 70 4d pJournal and pM
187d0 61 73 74 65 72 20 66 69 6c 65 20 64 65 73 63 72 aster file descr
187e0 69 70 74 6f 72 73 2e 0a 20 20 2a 2a 20 49 66 20 iptors.. ** If
187f0 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 70 65 6e successful, open
18800 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
18810 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 72 65 61 nal file for rea
18820 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 70 4d 61 ding.. */. pMa
18830 73 74 65 72 20 3d 20 28 73 71 6c 69 74 65 33 5f ster = (sqlite3_
18840 66 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d 61 file *)sqlite3Ma
18850 6c 6c 6f 63 5a 65 72 6f 28 70 56 66 73 2d 3e 73 llocZero(pVfs->s
18860 7a 4f 73 46 69 6c 65 20 2a 20 32 29 3b 0a 20 20 zOsFile * 2);.
18870 70 4a 6f 75 72 6e 61 6c 20 3d 20 28 73 71 6c 69 pJournal = (sqli
18880 74 65 33 5f 66 69 6c 65 20 2a 29 28 28 28 75 38 te3_file *)(((u8
18890 20 2a 29 70 4d 61 73 74 65 72 29 20 2b 20 70 56 *)pMaster) + pV
188a0 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20 fs->szOsFile);.
188b0 20 69 66 28 20 21 70 4d 61 73 74 65 72 20 29 7b if( !pMaster ){
188c0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
188d0 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b _NOMEM;. }else{
188e0 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 66 . const int f
188f0 6c 61 67 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f lags = (SQLITE_O
18900 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c PEN_READONLY|SQL
18910 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f ITE_OPEN_MASTER_
18920 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 72 63 JOURNAL);. rc
18930 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e = sqlite3OsOpen
18940 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 (pVfs, zMaster,
18950 70 4d 61 73 74 65 72 2c 20 66 6c 61 67 73 2c 20 pMaster, flags,
18960 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 0);. }. if( rc
18970 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
18980 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 to delmaster_out
18990 3b 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 ;.. /* Load the
189a0 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72 20 6a entire master j
189b0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f ournal file into
189c0 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20 space obtained
189d0 66 72 6f 6d 0a 20 20 2a 2a 20 73 71 6c 69 74 65 from. ** sqlite
189e0 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 3_malloc() and p
189f0 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 ointed to by zMa
18a00 73 74 65 72 4a 6f 75 72 6e 61 6c 2e 20 20 20 41 sterJournal. A
18a10 6c 73 6f 20 6f 62 74 61 69 6e 0a 20 20 2a 2a 20 lso obtain. **
18a20 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 sufficient space
18a30 20 28 69 6e 20 7a 4d 61 73 74 65 72 50 74 72 29 (in zMasterPtr)
18a40 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e 61 6d to hold the nam
18a50 65 73 20 6f 66 20 6d 61 73 74 65 72 0a 20 20 2a es of master. *
18a60 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 * journal files
18a70 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 72 extracted from r
18a80 65 67 75 6c 61 72 20 72 6f 6c 6c 62 61 63 6b 2d egular rollback-
18a90 6a 6f 75 72 6e 61 6c 73 2e 0a 20 20 2a 2f 0a 20 journals.. */.
18aa0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 rc = sqlite3OsF
18ab0 69 6c 65 53 69 7a 65 28 70 4d 61 73 74 65 72 2c ileSize(pMaster,
18ac0 20 26 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c &nMasterJournal
18ad0 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
18ae0 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 ITE_OK ) goto de
18af0 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 6e lmaster_out;. n
18b00 4d 61 73 74 65 72 50 74 72 20 3d 20 70 56 66 73 MasterPtr = pVfs
18b10 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 3b 0a ->mxPathname+1;.
18b20 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c zMasterJournal
18b30 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
18b40 28 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 ((int)nMasterJou
18b50 72 6e 61 6c 20 2b 20 6e 4d 61 73 74 65 72 50 74 rnal + nMasterPt
18b60 72 20 2b 20 31 29 3b 0a 20 20 69 66 28 20 21 7a r + 1);. if( !z
18b70 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b MasterJournal ){
18b80 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
18b90 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f _NOMEM;. goto
18ba0 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a delmaster_out;.
18bb0 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 50 74 72 }. zMasterPtr
18bc0 20 3d 20 26 7a 4d 61 73 74 65 72 4a 6f 75 72 6e = &zMasterJourn
18bd0 61 6c 5b 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 al[nMasterJourna
18be0 6c 2b 31 5d 3b 0a 20 20 72 63 20 3d 20 73 71 6c l+1];. rc = sql
18bf0 69 74 65 33 4f 73 52 65 61 64 28 70 4d 61 73 74 ite3OsRead(pMast
18c00 65 72 2c 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e er, zMasterJourn
18c10 61 6c 2c 20 28 69 6e 74 29 6e 4d 61 73 74 65 72 al, (int)nMaster
18c20 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 69 Journal, 0);. i
18c30 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
18c40 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 ) goto delmaste
18c50 72 5f 6f 75 74 3b 0a 20 20 7a 4d 61 73 74 65 72 r_out;. zMaster
18c60 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a Journal[nMasterJ
18c70 6f 75 72 6e 61 6c 5d 20 3d 20 30 3b 0a 0a 20 20 ournal] = 0;..
18c80 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4d 61 73 74 zJournal = zMast
18c90 65 72 4a 6f 75 72 6e 61 6c 3b 0a 20 20 77 68 69 erJournal;. whi
18ca0 6c 65 28 20 28 7a 4a 6f 75 72 6e 61 6c 2d 7a 4d le( (zJournal-zM
18cb0 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3c 6e 4d asterJournal)<nM
18cc0 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a asterJournal ){.
18cd0 20 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b 0a int exists;.
18ce0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
18cf0 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a OsAccess(pVfs, z
18d00 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f Journal, SQLITE_
18d10 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 ACCESS_EXISTS, &
18d20 65 78 69 73 74 73 29 3b 0a 20 20 20 20 69 66 28 exists);. if(
18d30 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
18d40 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c {. goto del
18d50 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 master_out;.
18d60 7d 0a 20 20 20 20 69 66 28 20 65 78 69 73 74 73 }. if( exists
18d70 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 65 ){. /* One
18d80 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 73 of the journals
18d90 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 pointed to by t
18da0 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
18db0 6c 20 65 78 69 73 74 73 2e 0a 20 20 20 20 20 20 l exists..
18dc0 2a 2a 20 4f 70 65 6e 20 69 74 20 61 6e 64 20 63 ** Open it and c
18dd0 68 65 63 6b 20 69 66 20 69 74 20 70 6f 69 6e 74 heck if it point
18de0 73 20 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 s at the master
18df0 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 journal. If.
18e00 20 20 2a 2a 20 73 6f 2c 20 72 65 74 75 72 6e 20 ** so, return
18e10 77 69 74 68 6f 75 74 20 64 65 6c 65 74 69 6e 67 without deleting
18e20 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
18e30 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 nal file..
18e40 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 63 3b 0a */. int c;.
18e50 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 int flags
18e60 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 = (SQLITE_OPEN_R
18e70 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f EADONLY|SQLITE_O
18e80 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c PEN_MAIN_JOURNAL
18e90 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 );. rc = sq
18ea0 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 lite3OsOpen(pVfs
18eb0 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 70 4a 6f 75 , zJournal, pJou
18ec0 72 6e 61 6c 2c 20 66 6c 61 67 73 2c 20 30 29 3b rnal, flags, 0);
18ed0 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
18ee0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
18ef0 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 goto delmast
18f00 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a er_out;. }.
18f10 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 . rc = read
18f20 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 4a MasterJournal(pJ
18f30 6f 75 72 6e 61 6c 2c 20 7a 4d 61 73 74 65 72 50 ournal, zMasterP
18f40 74 72 2c 20 6e 4d 61 73 74 65 72 50 74 72 29 3b tr, nMasterPtr);
18f50 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 . sqlite3Os
18f60 43 6c 6f 73 65 28 70 4a 6f 75 72 6e 61 6c 29 3b Close(pJournal);
18f70 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
18f80 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
18f90 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 goto delmast
18fa0 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a er_out;. }.
18fb0 0a 20 20 20 20 20 20 63 20 3d 20 7a 4d 61 73 74 . c = zMast
18fc0 65 72 50 74 72 5b 30 5d 21 3d 30 20 26 26 20 73 erPtr[0]!=0 && s
18fd0 74 72 63 6d 70 28 7a 4d 61 73 74 65 72 50 74 72 trcmp(zMasterPtr
18fe0 2c 20 7a 4d 61 73 74 65 72 29 3d 3d 30 3b 0a 20 , zMaster)==0;.
18ff0 20 20 20 20 20 69 66 28 20 63 20 29 7b 0a 20 20 if( c ){.
19000 20 20 20 20 20 20 2f 2a 20 57 65 20 68 61 76 65 /* We have
19010 20 61 20 6d 61 74 63 68 2e 20 44 6f 20 6e 6f 74 a match. Do not
19020 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 delete the mast
19030 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e er journal file.
19040 20 2a 2f 0a 20 20 20 20 20 20 20 20 67 6f 74 6f */. goto
19050 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a delmaster_out;.
19060 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
19070 20 20 7a 4a 6f 75 72 6e 61 6c 20 2b 3d 20 28 73 zJournal += (s
19080 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
19090 4a 6f 75 72 6e 61 6c 29 2b 31 29 3b 0a 20 20 7d Journal)+1);. }
190a0 0a 20 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 6c . . sqlite3OsCl
190b0 6f 73 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 ose(pMaster);.
190c0 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 rc = sqlite3OsDe
190d0 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 lete(pVfs, zMast
190e0 65 72 2c 20 30 29 3b 0a 0a 64 65 6c 6d 61 73 74 er, 0);..delmast
190f0 65 72 5f 6f 75 74 3a 0a 20 20 73 71 6c 69 74 65 er_out:. sqlite
19100 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 72 4a 6f 3_free(zMasterJo
19110 75 72 6e 61 6c 29 3b 0a 20 20 69 66 28 20 70 4d urnal);. if( pM
19120 61 73 74 65 72 20 29 7b 0a 20 20 20 20 73 71 6c aster ){. sql
19130 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4d 61 73 ite3OsClose(pMas
19140 74 65 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74 ter);. assert
19150 28 20 21 69 73 4f 70 65 6e 28 70 4a 6f 75 72 6e ( !isOpen(pJourn
19160 61 6c 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 al) );. sqlit
19170 65 33 5f 66 72 65 65 28 70 4d 61 73 74 65 72 29 e3_free(pMaster)
19180 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
19190 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 c;.}.../*.** Thi
191a0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 s function is us
191b0 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 ed to change the
191c0 20 61 63 74 75 61 6c 20 73 69 7a 65 20 6f 66 20 actual size of
191d0 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a the database .**
191e0 20 66 69 6c 65 20 69 6e 20 74 68 65 20 66 69 6c file in the fil
191f0 65 2d 73 79 73 74 65 6d 2e 20 54 68 69 73 20 6f e-system. This o
19200 6e 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e nly happens when
19210 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 72 committing a tr
19220 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 6f 72 ansaction,.** or
19230 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 rolling back a
19240 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 69 6e 63 transaction (inc
19250 6c 75 64 69 6e 67 20 72 6f 6c 6c 69 6e 67 20 62 luding rolling b
19260 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 ack a hot-journa
19270 6c 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 l)..**.** If the
19280 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 main database f
19290 69 6c 65 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c ile is not open,
192a0 20 6f 72 20 74 68 65 20 70 61 67 65 72 20 69 73 or the pager is
192b0 20 6e 6f 74 20 69 6e 20 65 69 74 68 65 72 0a 2a not in either.*
192c0 2a 20 44 42 4d 4f 44 20 6f 72 20 4f 50 45 4e 20 * DBMOD or OPEN
192d0 73 74 61 74 65 2c 20 74 68 69 73 20 66 75 6e 63 state, this func
192e0 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tion is a no-op.
192f0 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 Otherwise, the
19300 73 69 7a 65 20 0a 2a 2a 20 6f 66 20 74 68 65 20 size .** of the
19310 66 69 6c 65 20 69 73 20 63 68 61 6e 67 65 64 20 file is changed
19320 74 6f 20 6e 50 61 67 65 20 70 61 67 65 73 20 28 to nPage pages (
19330 6e 50 61 67 65 2a 70 50 61 67 65 72 2d 3e 70 61 nPage*pPager->pa
19340 67 65 53 69 7a 65 20 62 79 74 65 73 29 2e 20 0a geSize bytes). .
19350 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 6f ** If the file o
19360 6e 20 64 69 73 6b 20 69 73 20 63 75 72 72 65 6e n disk is curren
19370 74 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20 tly larger than
19380 6e 50 61 67 65 20 70 61 67 65 73 2c 20 74 68 65 nPage pages, the
19390 6e 20 75 73 65 20 74 68 65 20 56 46 53 0a 2a 2a n use the VFS.**
193a0 20 78 54 72 75 6e 63 61 74 65 28 29 20 6d 65 74 xTruncate() met
193b0 68 6f 64 20 74 6f 20 74 72 75 6e 63 61 74 65 20 hod to truncate
193c0 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 74 it..**.** Or, it
193d0 20 6d 69 67 68 74 20 6d 69 67 68 74 20 62 65 20 might might be
193e0 74 68 65 20 63 61 73 65 20 74 68 61 74 20 74 68 the case that th
193f0 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 e file on disk i
19400 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 0a s smaller than .
19410 2a 2a 20 6e 50 61 67 65 20 70 61 67 65 73 2e 20 ** nPage pages.
19420 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73 Some operating s
19430 79 73 74 65 6d 20 69 6d 70 6c 65 6d 65 6e 74 61 ystem implementa
19440 74 69 6f 6e 73 20 63 61 6e 20 67 65 74 20 63 6f tions can get co
19450 6e 66 75 73 65 64 20 69 66 20 0a 2a 2a 20 79 6f nfused if .** yo
19460 75 20 74 72 79 20 74 6f 20 74 72 75 6e 63 61 74 u try to truncat
19470 65 20 61 20 66 69 6c 65 20 74 6f 20 73 6f 6d 65 e a file to some
19480 20 73 69 7a 65 20 74 68 61 74 20 69 73 20 6c 61 size that is la
19490 72 67 65 72 20 74 68 61 6e 20 69 74 20 0a 2a 2a rger than it .**
194a0 20 63 75 72 72 65 6e 74 6c 79 20 69 73 2c 20 73 currently is, s
194b0 6f 20 64 65 74 65 63 74 20 74 68 69 73 20 63 61 o detect this ca
194c0 73 65 20 61 6e 64 20 77 72 69 74 65 20 61 20 73 se and write a s
194d0 69 6e 67 6c 65 20 7a 65 72 6f 20 62 79 74 65 20 ingle zero byte
194e0 74 6f 20 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f to .** the end o
194f0 66 20 74 68 65 20 6e 65 77 20 66 69 6c 65 20 69 f the new file i
19500 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 nstead..**.** If
19510 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 successful, ret
19520 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 urn SQLITE_OK. I
19530 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 f an IO error oc
19540 63 75 72 73 20 77 68 69 6c 65 20 6d 6f 64 69 66 curs while modif
19550 79 69 6e 67 0a 2a 2a 20 74 68 65 20 64 61 74 61 ying.** the data
19560 62 61 73 65 20 66 69 6c 65 2c 20 72 65 74 75 72 base file, retur
19570 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 n the error code
19580 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a to the caller..
19590 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
195a0 67 65 72 5f 74 72 75 6e 63 61 74 65 28 50 61 67 ger_truncate(Pag
195b0 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f er *pPager, Pgno
195c0 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 nPage){. int r
195d0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
195e0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
195f0 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52 5f 45 >eState!=PAGER_E
19600 52 52 4f 52 20 29 3b 0a 20 20 61 73 73 65 72 74 RROR );. assert
19610 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 ( pPager->eState
19620 21 3d 50 41 47 45 52 5f 52 45 41 44 45 52 20 29 !=PAGER_READER )
19630 3b 0a 20 20 0a 20 20 69 66 28 20 69 73 4f 70 65 ;. . if( isOpe
19640 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 0a 20 n(pPager->fd) .
19650 20 20 26 26 20 28 70 50 61 67 65 72 2d 3e 65 53 && (pPager->eS
19660 74 61 74 65 3e 3d 50 41 47 45 52 5f 57 52 49 54 tate>=PAGER_WRIT
19670 45 52 5f 44 42 4d 4f 44 20 7c 7c 20 70 50 61 67 ER_DBMOD || pPag
19680 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 er->eState==PAGE
19690 52 5f 4f 50 45 4e 29 20 0a 20 20 29 7b 0a 20 20 R_OPEN) . ){.
196a0 20 20 69 36 34 20 63 75 72 72 65 6e 74 53 69 7a i64 currentSiz
196b0 65 2c 20 6e 65 77 53 69 7a 65 3b 0a 20 20 20 20 e, newSize;.
196c0 69 6e 74 20 73 7a 50 61 67 65 20 3d 20 70 50 61 int szPage = pPa
196d0 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 ger->pageSize;.
196e0 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
196f0 72 2d 3e 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 r->eLock==EXCLUS
19700 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 IVE_LOCK );.
19710 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 69 74 20 73 /* TODO: Is it s
19720 61 66 65 20 74 6f 20 75 73 65 20 50 61 67 65 72 afe to use Pager
19730 2e 64 62 46 69 6c 65 53 69 7a 65 20 68 65 72 65 .dbFileSize here
19740 3f 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 ? */. rc = sq
19750 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
19760 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 63 75 72 pPager->fd, &cur
19770 72 65 6e 74 53 69 7a 65 29 3b 0a 20 20 20 20 6e rentSize);. n
19780 65 77 53 69 7a 65 20 3d 20 73 7a 50 61 67 65 2a ewSize = szPage*
19790 28 69 36 34 29 6e 50 61 67 65 3b 0a 20 20 20 20 (i64)nPage;.
197a0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
197b0 4b 20 26 26 20 63 75 72 72 65 6e 74 53 69 7a 65 K && currentSize
197c0 21 3d 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 !=newSize ){.
197d0 20 20 20 69 66 28 20 63 75 72 72 65 6e 74 53 69 if( currentSi
197e0 7a 65 3e 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 ze>newSize ){.
197f0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
19800 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 e3OsTruncate(pPa
19810 67 65 72 2d 3e 66 64 2c 20 6e 65 77 53 69 7a 65 ger->fd, newSize
19820 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 );. }else i
19830 66 28 20 28 63 75 72 72 65 6e 74 53 69 7a 65 2b f( (currentSize+
19840 73 7a 50 61 67 65 29 3c 3d 6e 65 77 53 69 7a 65 szPage)<=newSize
19850 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 ){. char
19860 20 2a 70 54 6d 70 20 3d 20 70 50 61 67 65 72 2d *pTmp = pPager-
19870 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 >pTmpSpace;.
19880 20 20 20 20 6d 65 6d 73 65 74 28 70 54 6d 70 2c memset(pTmp,
19890 20 30 2c 20 73 7a 50 61 67 65 29 3b 0a 20 20 20 0, szPage);.
198a0 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 28 testcase( (
198b0 6e 65 77 53 69 7a 65 2d 73 7a 50 61 67 65 29 20 newSize-szPage)
198c0 3d 3d 20 63 75 72 72 65 6e 74 53 69 7a 65 20 29 == currentSize )
198d0 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 ;. testca
198e0 73 65 28 20 28 6e 65 77 53 69 7a 65 2d 73 7a 50 se( (newSize-szP
198f0 61 67 65 29 20 3e 20 20 63 75 72 72 65 6e 74 53 age) > currentS
19900 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 20 20 72 ize );. r
19910 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
19920 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 te(pPager->fd, p
19930 54 6d 70 2c 20 73 7a 50 61 67 65 2c 20 6e 65 77 Tmp, szPage, new
19940 53 69 7a 65 2d 73 7a 50 61 67 65 29 3b 0a 20 20 Size-szPage);.
19950 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
19960 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
19970 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d . pPager-
19980 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d 20 6e 50 >dbFileSize = nP
19990 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 age;. }.
199a0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
199b0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 rc;.}../*.** Ret
199c0 75 72 6e 20 61 20 73 61 6e 69 74 69 7a 65 64 20 urn a sanitized
199d0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 73 version of the s
199e0 65 63 74 6f 72 2d 73 69 7a 65 20 6f 66 20 4f 53 ector-size of OS
199f0 20 66 69 6c 65 20 70 46 69 6c 65 2e 20 54 68 65 file pFile. The
19a00 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 .** return value
19a10 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 is guaranteed t
19a20 6f 20 6c 69 65 20 62 65 74 77 65 65 6e 20 33 32 o lie between 32
19a30 20 61 6e 64 20 4d 41 58 5f 53 45 43 54 4f 52 5f and MAX_SECTOR_
19a40 53 49 5a 45 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c SIZE..*/.int sql
19a50 69 74 65 33 53 65 63 74 6f 72 53 69 7a 65 28 73 ite3SectorSize(s
19a60 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 qlite3_file *pFi
19a70 6c 65 29 7b 0a 20 20 69 6e 74 20 69 52 65 74 20 le){. int iRet
19a80 3d 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f = sqlite3OsSecto
19a90 72 53 69 7a 65 28 70 46 69 6c 65 29 3b 0a 20 20 rSize(pFile);.
19aa0 69 66 28 20 69 52 65 74 3c 33 32 20 29 7b 0a 20 if( iRet<32 ){.
19ab0 20 20 20 69 52 65 74 20 3d 20 35 31 32 3b 0a 20 iRet = 512;.
19ac0 20 7d 65 6c 73 65 20 69 66 28 20 69 52 65 74 3e }else if( iRet>
19ad0 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 MAX_SECTOR_SIZE
19ae0 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 4d ){. assert( M
19af0 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3e 3d AX_SECTOR_SIZE>=
19b00 35 31 32 20 29 3b 0a 20 20 20 20 69 52 65 74 20 512 );. iRet
19b10 3d 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a = MAX_SECTOR_SIZ
19b20 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 E;. }. return
19b30 69 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 iRet;.}../*.** S
19b40 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 et the value of
19b50 74 68 65 20 50 61 67 65 72 2e 73 65 63 74 6f 72 the Pager.sector
19b60 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 66 6f Size variable fo
19b70 72 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 70 r the given.** p
19b80 61 67 65 72 20 62 61 73 65 64 20 6f 6e 20 74 68 ager based on th
19b90 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
19ba0 20 62 79 20 74 68 65 20 78 53 65 63 74 6f 72 53 by the xSectorS
19bb0 69 7a 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 ize method.** of
19bc0 20 74 68 65 20 6f 70 65 6e 20 64 61 74 61 62 61 the open databa
19bd0 73 65 20 66 69 6c 65 2e 20 54 68 65 20 73 65 63 se file. The sec
19be0 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 tor size will be
19bf0 20 75 73 65 64 20 75 73 65 64 20 0a 2a 2a 20 74 used used .** t
19c00 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
19c10 73 69 7a 65 20 61 6e 64 20 61 6c 69 67 6e 6d 65 size and alignme
19c20 6e 74 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 nt of journal he
19c30 61 64 65 72 20 61 6e 64 20 0a 2a 2a 20 6d 61 73 ader and .** mas
19c40 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e ter journal poin
19c50 74 65 72 73 20 77 69 74 68 69 6e 20 63 72 65 61 ters within crea
19c60 74 65 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ted journal file
19c70 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 65 6d s..**.** For tem
19c80 70 6f 72 61 72 79 20 66 69 6c 65 73 20 74 68 65 porary files the
19c90 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 6f effective secto
19ca0 72 20 73 69 7a 65 20 69 73 20 61 6c 77 61 79 73 r size is always
19cb0 20 35 31 32 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 512 bytes..**.*
19cc0 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 6f 72 * Otherwise, for
19cd0 20 6e 6f 6e 2d 74 65 6d 70 6f 72 61 72 79 20 66 non-temporary f
19ce0 69 6c 65 73 2c 20 74 68 65 20 65 66 66 65 63 74 iles, the effect
19cf0 69 76 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 ive sector size
19d00 69 73 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 is.** the value
19d10 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 returned by the
19d20 78 53 65 63 74 6f 72 53 69 7a 65 28 29 20 6d 65 xSectorSize() me
19d30 74 68 6f 64 20 72 6f 75 6e 64 65 64 20 75 70 20 thod rounded up
19d40 74 6f 20 33 32 20 69 66 0a 2a 2a 20 69 74 20 69 to 32 if.** it i
19d50 73 20 6c 65 73 73 20 74 68 61 6e 20 33 32 2c 20 s less than 32,
19d60 6f 72 20 72 6f 75 6e 64 65 64 20 64 6f 77 6e 20 or rounded down
19d70 74 6f 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 to MAX_SECTOR_SI
19d80 5a 45 20 69 66 20 69 74 0a 2a 2a 20 69 73 20 67 ZE if it.** is g
19d90 72 65 61 74 65 72 20 74 68 61 6e 20 4d 41 58 5f reater than MAX_
19da0 53 45 43 54 4f 52 5f 53 49 5a 45 2e 0a 2a 2a 0a SECTOR_SIZE..**.
19db0 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 68 ** If the file h
19dc0 61 73 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f as the SQLITE_IO
19dd0 43 41 50 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 CAP_POWERSAFE_OV
19de0 45 52 57 52 49 54 45 20 70 72 6f 70 65 72 74 79 ERWRITE property
19df0 2c 20 74 68 65 6e 20 73 65 74 0a 2a 2a 20 74 68 , then set.** th
19e00 65 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 e effective sect
19e10 6f 72 20 73 69 7a 65 20 74 6f 20 69 74 73 20 6d or size to its m
19e20 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 28 35 31 inimum value (51
19e30 32 29 2e 20 20 54 68 65 20 70 75 72 70 6f 73 65 2). The purpose
19e40 20 6f 66 0a 2a 2a 20 70 50 61 67 65 72 2d 3e 73 of.** pPager->s
19e50 65 63 74 6f 72 53 69 7a 65 20 69 73 20 74 6f 20 ectorSize is to
19e60 64 65 66 69 6e 65 20 74 68 65 20 22 62 6c 61 73 define the "blas
19e70 74 20 72 61 64 69 75 73 22 20 6f 66 20 62 79 74 t radius" of byt
19e80 65 73 20 74 68 61 74 0a 2a 2a 20 6d 69 67 68 74 es that.** might
19e90 20 63 68 61 6e 67 65 20 69 66 20 61 20 63 72 61 change if a cra
19ea0 73 68 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 sh occurs while
19eb0 77 72 69 74 69 6e 67 20 74 6f 20 61 20 73 69 6e writing to a sin
19ec0 67 6c 65 20 62 79 74 65 20 69 6e 0a 2a 2a 20 74 gle byte in.** t
19ed0 68 61 74 20 72 61 6e 67 65 2e 20 20 42 75 74 20 hat range. But
19ee0 77 69 74 68 20 50 4f 57 45 52 53 41 46 45 5f 4f with POWERSAFE_O
19ef0 56 45 52 57 52 49 54 45 2c 20 74 68 65 20 62 6c VERWRITE, the bl
19f00 61 73 74 20 72 61 64 69 75 73 20 69 73 20 7a 65 ast radius is ze
19f10 72 6f 0a 2a 2a 20 28 74 68 61 74 20 69 73 20 77 ro.** (that is w
19f20 68 61 74 20 50 4f 57 45 52 53 41 46 45 5f 4f 56 hat POWERSAFE_OV
19f30 45 52 57 52 49 54 45 20 6d 65 61 6e 73 29 2c 20 ERWRITE means),
19f40 73 6f 20 77 65 20 6d 69 6e 69 6d 69 7a 65 20 74 so we minimize t
19f50 68 65 20 73 65 63 74 6f 72 0a 2a 2a 20 73 69 7a he sector.** siz
19f60 65 2e 20 20 46 6f 72 20 62 61 63 6b 77 61 72 64 e. For backward
19f70 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 s compatibility
19f80 6f 66 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 of the rollback
19f90 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 journal file for
19fa0 6d 61 74 2c 0a 2a 2a 20 77 65 20 63 61 6e 6e 6f mat,.** we canno
19fb0 74 20 72 65 64 75 63 65 20 74 68 65 20 65 66 66 t reduce the eff
19fc0 65 63 74 69 76 65 20 73 65 63 74 6f 72 20 73 69 ective sector si
19fd0 7a 65 20 62 65 6c 6f 77 20 35 31 32 2e 0a 2a 2f ze below 512..*/
19fe0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 .static void set
19ff0 53 65 63 74 6f 72 53 69 7a 65 28 50 61 67 65 72 SectorSize(Pager
1a000 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 61 73 73 *pPager){. ass
1a010 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 ert( isOpen(pPag
1a020 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 er->fd) || pPage
1a030 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 0a r->tempFile );..
1a040 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 74 65 if( pPager->te
1a050 6d 70 46 69 6c 65 0a 20 20 20 7c 7c 20 28 73 71 mpFile. || (sq
1a060 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 lite3OsDeviceCha
1a070 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 racteristics(pPa
1a080 67 65 72 2d 3e 66 64 29 20 26 20 0a 20 20 20 20 ger->fd) & .
1a090 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 SQLITE
1a0a0 5f 49 4f 43 41 50 5f 50 4f 57 45 52 53 41 46 45 _IOCAP_POWERSAFE
1a0b0 5f 4f 56 45 52 57 52 49 54 45 29 21 3d 30 0a 20 _OVERWRITE)!=0.
1a0c0 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74 6f ){. /* Secto
1a0d0 72 20 73 69 7a 65 20 64 6f 65 73 6e 27 74 20 6d r size doesn't m
1a0e0 61 74 74 65 72 20 66 6f 72 20 74 65 6d 70 6f 72 atter for tempor
1a0f0 61 72 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f 2c ary files. Also,
1a100 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a the file. **
1a110 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 62 65 may not have be
1a120 65 6e 20 6f 70 65 6e 65 64 20 79 65 74 2c 20 69 en opened yet, i
1a130 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 n which case the
1a140 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a OsSectorSize().
1a150 20 20 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c ** call will
1a160 20 73 65 67 66 61 75 6c 74 2e 20 2a 2f 0a 20 20 segfault. */.
1a170 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 pPager->sector
1a180 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20 7d 65 Size = 512;. }e
1a190 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 72 2d lse{. pPager-
1a1a0 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 73 71 >sectorSize = sq
1a1b0 6c 69 74 65 33 53 65 63 74 6f 72 53 69 7a 65 28 lite3SectorSize(
1a1c0 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d pPager->fd);. }
1a1d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 .}../*.** Playba
1a1e0 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 ck the journal a
1a1f0 6e 64 20 74 68 75 73 20 72 65 73 74 6f 72 65 20 nd thus restore
1a200 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1a210 65 20 74 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74 e to.** the stat
1a220 65 20 69 74 20 77 61 73 20 69 6e 20 62 65 66 6f e it was in befo
1a230 72 65 20 77 65 20 73 74 61 72 74 65 64 20 6d 61 re we started ma
1a240 6b 69 6e 67 20 63 68 61 6e 67 65 73 2e 20 20 0a king changes. .
1a250 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 **.** The journa
1a260 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 69 73 l file format is
1a270 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a as follows: .**
1a280 0a 2a 2a 20 20 28 31 29 20 20 38 20 62 79 74 65 .** (1) 8 byte
1a290 20 70 72 65 66 69 78 2e 20 20 41 20 63 6f 70 79 prefix. A copy
1a2a0 20 6f 66 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 of aJournalMagi
1a2b0 63 5b 5d 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20 c[]..** (2) 4
1a2c0 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 byte big-endian
1a2d0 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 integer which is
1a2e0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 the number of v
1a2f0 61 6c 69 64 20 70 61 67 65 20 72 65 63 6f 72 64 alid page record
1a300 73 0a 2a 2a 20 20 20 20 20 20 20 69 6e 20 74 68 s.** in th
1a310 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74 e journal. If t
1a320 68 69 73 20 76 61 6c 75 65 20 69 73 20 30 78 66 his value is 0xf
1a330 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20 63 6f fffffff, then co
1a340 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 mpute the.**
1a350 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 number of pag
1a360 65 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 e records from t
1a370 68 65 20 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e he journal size.
1a380 0a 2a 2a 20 20 28 33 29 20 20 34 20 62 79 74 65 .** (3) 4 byte
1a390 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 big-endian inte
1a3a0 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 ger which is the
1a3b0 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 66 initial value f
1a3c0 6f 72 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 or the .**
1a3d0 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73 75 6d sanity checksum
1a3e0 2e 0a 2a 2a 20 20 28 34 29 20 20 34 20 62 79 74 ..** (4) 4 byt
1a3f0 65 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 e integer which
1a400 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
1a410 20 70 61 67 65 73 20 74 6f 20 74 72 75 6e 63 61 pages to trunca
1a420 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 te the.**
1a430 64 61 74 61 62 61 73 65 20 74 6f 20 64 75 72 69 database to duri
1a440 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a ng a rollback..*
1a450 2a 20 20 28 35 29 20 20 34 20 62 79 74 65 20 62 * (5) 4 byte b
1a460 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 ig-endian intege
1a470 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73 r which is the s
1a480 65 63 74 6f 72 20 73 69 7a 65 2e 20 20 54 68 65 ector size. The
1a490 20 68 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 20 header.**
1a4a0 20 69 73 20 74 68 69 73 20 6d 61 6e 79 20 62 79 is this many by
1a4b0 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 tes in size..**
1a4c0 20 28 36 29 20 20 34 20 62 79 74 65 20 62 69 67 (6) 4 byte big
1a4d0 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 -endian integer
1a4e0 77 68 69 63 68 20 69 73 20 74 68 65 20 70 61 67 which is the pag
1a4f0 65 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 37 29 20 e size..** (7)
1a500 20 7a 65 72 6f 20 70 61 64 64 69 6e 67 20 6f 75 zero padding ou
1a510 74 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65 t to the next se
1a520 63 74 6f 72 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 ctor size..** (
1a530 38 29 20 20 5a 65 72 6f 20 6f 72 20 6d 6f 72 65 8) Zero or more
1a540 20 70 61 67 65 73 20 69 6e 73 74 61 6e 63 65 73 pages instances
1a550 2c 20 65 61 63 68 20 61 73 20 66 6f 6c 6c 6f 77 , each as follow
1a560 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 20 s:.** +
1a570 34 20 62 79 74 65 20 70 61 67 65 20 6e 75 6d 62 4 byte page numb
1a580 65 72 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 er..** +
1a590 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
1a5a0 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 2e e bytes of data.
1a5b0 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 20 34 20 .** + 4
1a5c0 62 79 74 65 20 63 68 65 63 6b 73 75 6d 0a 2a 2a byte checksum.**
1a5d0 0a 2a 2a 20 57 68 65 6e 20 77 65 20 73 70 65 61 .** When we spea
1a5e0 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c k of the journal
1a5f0 20 68 65 61 64 65 72 2c 20 77 65 20 6d 65 61 6e header, we mean
1a600 20 74 68 65 20 66 69 72 73 74 20 37 20 69 74 65 the first 7 ite
1a610 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 45 61 63 ms above..** Eac
1a620 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 6a h entry in the j
1a630 6f 75 72 6e 61 6c 20 69 73 20 61 6e 20 69 6e 73 ournal is an ins
1a640 74 61 6e 63 65 20 6f 66 20 74 68 65 20 38 74 68 tance of the 8th
1a650 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c item..**.** Cal
1a660 6c 20 74 68 65 20 76 61 6c 75 65 20 66 72 6f 6d l the value from
1a670 20 74 68 65 20 73 65 63 6f 6e 64 20 62 75 6c 6c the second bull
1a680 65 74 20 22 6e 52 65 63 22 2e 20 20 6e 52 65 63 et "nRec". nRec
1a690 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
1a6a0 66 0a 2a 2a 20 76 61 6c 69 64 20 70 61 67 65 20 f.** valid page
1a6b0 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 6a entries in the j
1a6c0 6f 75 72 6e 61 6c 2e 20 20 49 6e 20 6d 6f 73 74 ournal. In most
1a6d0 20 63 61 73 65 73 2c 20 79 6f 75 20 63 61 6e 20 cases, you can
1a6e0 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20 76 compute the.** v
1a6f0 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 66 72 6f alue of nRec fro
1a700 6d 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 m the size of th
1a710 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 e journal file.
1a720 20 42 75 74 20 69 66 20 61 20 70 6f 77 65 72 0a But if a power.
1a730 2a 2a 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 ** failure occur
1a740 72 65 64 20 77 68 69 6c 65 20 74 68 65 20 6a 6f red while the jo
1a750 75 72 6e 61 6c 20 77 61 73 20 62 65 69 6e 67 20 urnal was being
1a760 77 72 69 74 74 65 6e 2c 20 69 74 20 63 6f 75 6c written, it coul
1a770 64 20 62 65 20 74 68 65 0a 2a 2a 20 63 61 73 65 d be the.** case
1a780 20 74 68 61 74 20 74 68 65 20 73 69 7a 65 20 6f that the size o
1a790 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
1a7a0 6c 65 20 68 61 64 20 61 6c 72 65 61 64 79 20 62 le had already b
1a7b0 65 65 6e 20 69 6e 63 72 65 61 73 65 64 20 62 75 een increased bu
1a7c0 74 0a 2a 2a 20 74 68 65 20 65 78 74 72 61 20 65 t.** the extra e
1a7d0 6e 74 72 69 65 73 20 68 61 64 20 6e 6f 74 20 79 ntries had not y
1a7e0 65 74 20 6d 61 64 65 20 69 74 20 73 61 66 65 6c et made it safel
1a7f0 79 20 74 6f 20 64 69 73 6b 2e 20 20 49 6e 20 73 y to disk. In s
1a800 75 63 68 20 61 20 63 61 73 65 2c 0a 2a 2a 20 74 uch a case,.** t
1a810 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 he value of nRec
1a820 20 63 6f 6d 70 75 74 65 64 20 66 72 6f 6d 20 74 computed from t
1a830 68 65 20 66 69 6c 65 20 73 69 7a 65 20 77 6f 75 he file size wou
1a840 6c 64 20 62 65 20 74 6f 6f 20 6c 61 72 67 65 2e ld be too large.
1a850 20 20 46 6f 72 0a 2a 2a 20 74 68 61 74 20 72 65 For.** that re
1a860 61 73 6f 6e 2c 20 77 65 20 61 6c 77 61 79 73 20 ason, we always
1a870 75 73 65 20 74 68 65 20 6e 52 65 63 20 76 61 6c use the nRec val
1a880 75 65 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 ue in the header
1a890 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e ..**.** If the n
1a8a0 52 65 63 20 76 61 6c 75 65 20 69 73 20 30 78 66 Rec value is 0xf
1a8b0 66 66 66 66 66 66 66 20 69 74 20 6d 65 61 6e 73 fffffff it means
1a8c0 20 74 68 61 74 20 6e 52 65 63 20 73 68 6f 75 6c that nRec shoul
1a8d0 64 20 62 65 20 63 6f 6d 70 75 74 65 64 0a 2a 2a d be computed.**
1a8e0 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 from the file s
1a8f0 69 7a 65 2e 20 20 54 68 69 73 20 76 61 6c 75 65 ize. This value
1a900 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 74 68 is used when th
1a910 65 20 75 73 65 72 20 73 65 6c 65 63 74 73 20 74 e user selects t
1a920 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e 63 20 6f 70 he.** no-sync op
1a930 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6a 6f 75 tion for the jou
1a940 72 6e 61 6c 2e 20 20 41 20 70 6f 77 65 72 20 66 rnal. A power f
1a950 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c 65 61 ailure could lea
1a960 64 20 74 6f 20 63 6f 72 72 75 70 74 69 6f 6e 0a d to corruption.
1a970 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e ** in this case.
1a980 20 20 42 75 74 20 66 6f 72 20 74 68 69 6e 67 73 But for things
1a990 20 6c 69 6b 65 20 74 65 6d 70 6f 72 61 72 79 20 like temporary
1a9a0 74 61 62 6c 65 20 28 77 68 69 63 68 20 77 69 6c table (which wil
1a9b0 6c 20 62 65 0a 2a 2a 20 64 65 6c 65 74 65 64 20 l be.** deleted
1a9c0 77 68 65 6e 20 74 68 65 20 70 6f 77 65 72 20 69 when the power i
1a9d0 73 20 72 65 73 74 6f 72 65 64 29 20 77 65 20 64 s restored) we d
1a9e0 6f 6e 27 74 20 63 61 72 65 2e 20 20 0a 2a 2a 0a on't care. .**.
1a9f0 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 6f ** If the file o
1aa00 70 65 6e 65 64 20 61 73 20 74 68 65 20 6a 6f 75 pened as the jou
1aa10 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 rnal file is not
1aa20 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a a well-formed.*
1aa30 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 * journal file t
1aa40 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 20 75 70 hen all pages up
1aa50 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 63 6f to the first co
1aa60 72 72 75 70 74 65 64 20 70 61 67 65 20 61 72 65 rrupted page are
1aa70 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 rolled.** back
1aa80 28 6f 72 20 6e 6f 20 70 61 67 65 73 20 69 66 20 (or no pages if
1aa90 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 the journal head
1aaa0 65 72 20 69 73 20 63 6f 72 72 75 70 74 65 64 29 er is corrupted)
1aab0 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 . The journal fi
1aac0 6c 65 0a 2a 2a 20 69 73 20 74 68 65 6e 20 64 65 le.** is then de
1aad0 6c 65 74 65 64 20 61 6e 64 20 53 51 4c 49 54 45 leted and SQLITE
1aae0 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2c 20 6a 75 _OK returned, ju
1aaf0 73 74 20 61 73 20 69 66 20 6e 6f 20 63 6f 72 72 st as if no corr
1ab00 75 70 74 69 6f 6e 20 68 61 64 0a 2a 2a 20 62 65 uption had.** be
1ab10 65 6e 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a en encountered..
1ab20 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 2f 4f 20 **.** If an I/O
1ab30 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 65 72 72 6f or malloc() erro
1ab40 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 6a 6f r occurs, the jo
1ab50 75 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20 6e 6f urnal-file is no
1ab60 74 20 64 65 6c 65 74 65 64 0a 2a 2a 20 61 6e 64 t deleted.** and
1ab70 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 an error code i
1ab80 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
1ab90 2a 20 54 68 65 20 69 73 48 6f 74 20 70 61 72 61 * The isHot para
1aba0 6d 65 74 65 72 20 69 6e 64 69 63 61 74 65 73 20 meter indicates
1abb0 74 68 61 74 20 77 65 20 61 72 65 20 74 72 79 69 that we are tryi
1abc0 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 ng to rollback a
1abd0 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 74 68 61 74 journal.** that
1abe0 20 6d 69 67 68 74 20 62 65 20 61 20 68 6f 74 20 might be a hot
1abf0 6a 6f 75 72 6e 61 6c 2e 20 20 4f 72 2c 20 69 74 journal. Or, it
1ac00 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20 74 could be that t
1ac10 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 0a 2a he journal is .*
1ac20 2a 20 70 72 65 73 65 72 76 65 64 20 62 65 63 61 * preserved beca
1ac30 75 73 65 20 6f 66 20 4a 4f 55 52 4e 41 4c 4d 4f use of JOURNALMO
1ac40 44 45 5f 50 45 52 53 49 53 54 20 6f 72 20 4a 4f DE_PERSIST or JO
1ac50 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 URNALMODE_TRUNCA
1ac60 54 45 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f TE..** If the jo
1ac70 75 72 6e 61 6c 20 72 65 61 6c 6c 79 20 69 73 20 urnal really is
1ac80 68 6f 74 2c 20 72 65 73 65 74 20 74 68 65 20 70 hot, reset the p
1ac90 61 67 65 72 20 63 61 63 68 65 20 70 72 69 6f 72 ager cache prior
1aca0 20 72 6f 6c 6c 69 6e 67 0a 2a 2a 20 62 61 63 6b rolling.** back
1acb0 20 61 6e 79 20 63 6f 6e 74 65 6e 74 2e 20 20 49 any content. I
1acc0 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 f the journal is
1acd0 20 6d 65 72 65 6c 79 20 70 65 72 73 69 73 74 65 merely persiste
1ace0 6e 74 2c 20 6e 6f 20 72 65 73 65 74 20 69 73 0a nt, no reset is.
1acf0 2a 2a 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 ** needed..*/.st
1ad00 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 70 atic int pager_p
1ad10 6c 61 79 62 61 63 6b 28 50 61 67 65 72 20 2a 70 layback(Pager *p
1ad20 50 61 67 65 72 2c 20 69 6e 74 20 69 73 48 6f 74 Pager, int isHot
1ad30 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 ){. sqlite3_vfs
1ad40 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d *pVfs = pPager-
1ad50 3e 70 56 66 73 3b 0a 20 20 69 36 34 20 73 7a 4a >pVfs;. i64 szJ
1ad60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1ad70 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
1ad80 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e journal file in
1ad90 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 33 32 20 bytes */. u32
1ada0 6e 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 nRec;
1adb0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1adc0 66 20 52 65 63 6f 72 64 73 20 69 6e 20 74 68 65 f Records in the
1add0 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 journal */. u3
1ade0 32 20 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 2 u;
1adf0 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e /* Unsign
1ae00 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 ed loop counter
1ae10 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67 20 3d */. Pgno mxPg =
1ae20 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 0; /*
1ae30 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f 72 69 Size of the ori
1ae40 67 69 6e 61 6c 20 66 69 6c 65 20 69 6e 20 70 61 ginal file in pa
1ae50 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b ges */. int rc;
1ae60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ae70 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 /* Result code
1ae80 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e 65 of a subroutine
1ae90 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 */. int res =
1aea0 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 1; /
1aeb0 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 * Value returned
1aec0 20 62 79 20 73 71 6c 69 74 65 33 4f 73 41 63 63 by sqlite3OsAcc
1aed0 65 73 73 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 ess() */. char
1aee0 2a 7a 4d 61 73 74 65 72 20 3d 20 30 3b 20 20 20 *zMaster = 0;
1aef0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 6d /* Name of m
1af00 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1af10 6c 65 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 69 le if any */. i
1af20 6e 74 20 6e 65 65 64 50 61 67 65 72 52 65 73 65 nt needPagerRese
1af30 74 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 t; /* True
1af40 74 6f 20 72 65 73 65 74 20 70 61 67 65 20 70 72 to reset page pr
1af50 69 6f 72 20 74 6f 20 66 69 72 73 74 20 70 61 67 ior to first pag
1af60 65 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 20 e rollback */..
1af70 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 /* Figure out h
1af80 6f 77 20 6d 61 6e 79 20 72 65 63 6f 72 64 73 20 ow many records
1af90 61 72 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e are in the journ
1afa0 61 6c 2e 20 20 41 62 6f 72 74 20 65 61 72 6c 79 al. Abort early
1afb0 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 if. ** the jou
1afc0 72 6e 61 6c 20 69 73 20 65 6d 70 74 79 2e 0a 20 rnal is empty..
1afd0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 */. assert( is
1afe0 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
1aff0 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 ) );. rc = sqli
1b000 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 te3OsFileSize(pP
1b010 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 4a 29 ager->jfd, &szJ)
1b020 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
1b030 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 TE_OK ){. got
1b040 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a o end_playback;.
1b050 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 }.. /* Read t
1b060 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
1b070 6c 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 l name from the
1b080 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 69 journal, if it i
1b090 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 2a 2a 20 s present.. **
1b0a0 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 If a master jour
1b0b0 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 nal file name is
1b0c0 20 73 70 65 63 69 66 69 65 64 2c 20 62 75 74 20 specified, but
1b0d0 74 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 0a the file is not.
1b0e0 20 20 2a 2a 20 70 72 65 73 65 6e 74 20 6f 6e 20 ** present on
1b0f0 64 69 73 6b 2c 20 74 68 65 6e 20 74 68 65 20 6a disk, then the j
1b100 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 68 6f ournal is not ho
1b110 74 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e t and does not n
1b120 65 65 64 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70 eed to be. ** p
1b130 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a 2a layed back.. **
1b140 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 54 65 63 68 . ** TODO: Tech
1b150 6e 69 63 61 6c 6c 79 20 74 68 65 20 66 6f 6c 6c nically the foll
1b160 6f 77 69 6e 67 20 69 73 20 61 6e 20 65 72 72 6f owing is an erro
1b170 72 20 62 65 63 61 75 73 65 20 69 74 20 61 73 73 r because it ass
1b180 75 6d 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 62 umes that. ** b
1b190 75 66 66 65 72 20 50 61 67 65 72 2e 70 54 6d 70 uffer Pager.pTmp
1b1a0 53 70 61 63 65 20 69 73 20 28 6d 78 50 61 74 68 Space is (mxPath
1b1b0 6e 61 6d 65 2b 31 29 20 62 79 74 65 73 20 6f 72 name+1) bytes or
1b1c0 20 6c 61 72 67 65 72 2e 20 69 2e 65 2e 20 74 68 larger. i.e. th
1b1d0 61 74 0a 20 20 2a 2a 20 28 70 50 61 67 65 72 2d at. ** (pPager-
1b1e0 3e 70 61 67 65 53 69 7a 65 20 3e 3d 20 70 50 61 >pageSize >= pPa
1b1f0 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 ger->pVfs->mxPat
1b200 68 6e 61 6d 65 2b 31 29 2e 20 55 73 69 6e 67 20 hname+1). Using
1b210 6f 73 5f 75 6e 69 78 2e 63 2c 0a 20 20 2a 2a 20 os_unix.c,. **
1b220 20 6d 78 50 61 74 68 6e 61 6d 65 20 69 73 20 35 mxPathname is 5
1b230 31 32 2c 20 77 68 69 63 68 20 69 73 20 74 68 65 12, which is the
1b240 20 73 61 6d 65 20 61 73 20 74 68 65 20 6d 69 6e same as the min
1b250 69 6d 75 6d 20 61 6c 6c 6f 77 61 62 6c 65 20 76 imum allowable v
1b260 61 6c 75 65 0a 20 20 2a 2a 20 66 6f 72 20 70 61 alue. ** for pa
1b270 67 65 53 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 7a geSize.. */. z
1b280 4d 61 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d Master = pPager-
1b290 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 72 63 >pTmpSpace;. rc
1b2a0 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 = readMasterJou
1b2b0 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 rnal(pPager->jfd
1b2c0 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 , zMaster, pPage
1b2d0 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e r->pVfs->mxPathn
1b2e0 61 6d 65 2b 31 29 3b 0a 20 20 69 66 28 20 72 63 ame+1);. if( rc
1b2f0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a ==SQLITE_OK && z
1b300 4d 61 73 74 65 72 5b 30 5d 20 29 7b 0a 20 20 20 Master[0] ){.
1b310 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 rc = sqlite3OsA
1b320 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 ccess(pVfs, zMas
1b330 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 ter, SQLITE_ACCE
1b340 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 SS_EXISTS, &res)
1b350 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 20 ;. }. zMaster
1b360 3d 20 30 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 = 0;. if( rc!=S
1b370 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 72 65 73 QLITE_OK || !res
1b380 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 ){. goto end
1b390 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a 20 _playback;. }.
1b3a0 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
1b3b0 4f 66 66 20 3d 20 30 3b 0a 20 20 6e 65 65 64 50 Off = 0;. needP
1b3c0 61 67 65 72 52 65 73 65 74 20 3d 20 69 73 48 6f agerReset = isHo
1b3d0 74 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f t;.. /* This lo
1b3e0 6f 70 20 74 65 72 6d 69 6e 61 74 65 73 20 65 69 op terminates ei
1b3f0 74 68 65 72 20 77 68 65 6e 20 61 20 72 65 61 64 ther when a read
1b400 4a 6f 75 72 6e 61 6c 48 64 72 28 29 20 6f 72 20 JournalHdr() or
1b410 0a 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61 79 . ** pager_play
1b420 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 back_one_page()
1b430 63 61 6c 6c 20 72 65 74 75 72 6e 73 20 53 51 4c call returns SQL
1b440 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 49 ITE_DONE or an I
1b450 4f 20 65 72 72 6f 72 20 0a 20 20 2a 2a 20 6f 63 O error . ** oc
1b460 63 75 72 73 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 curs. . */. wh
1b470 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 2f 2a ile( 1 ){. /*
1b480 20 52 65 61 64 20 74 68 65 20 6e 65 78 74 20 6a Read the next j
1b490 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 72 ournal header fr
1b4a0 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 om the journal f
1b4b0 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 20 61 ile. If there a
1b4c0 72 65 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 65 6e re. ** not en
1b4d0 6f 75 67 68 20 62 79 74 65 73 20 6c 65 66 74 20 ough bytes left
1b4e0 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 in the journal f
1b4f0 69 6c 65 20 66 6f 72 20 61 20 63 6f 6d 70 6c 65 ile for a comple
1b500 74 65 20 68 65 61 64 65 72 2c 20 6f 72 0a 20 20 te header, or.
1b510 20 20 2a 2a 20 69 74 20 69 73 20 63 6f 72 72 75 ** it is corru
1b520 70 74 65 64 2c 20 74 68 65 6e 20 61 20 70 72 6f pted, then a pro
1b530 63 65 73 73 20 6d 75 73 74 20 68 61 76 65 20 66 cess must have f
1b540 61 69 6c 65 64 20 77 68 69 6c 65 20 77 72 69 74 ailed while writ
1b550 69 6e 67 20 69 74 2e 0a 20 20 20 20 2a 2a 20 54 ing it.. ** T
1b560 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 6e 6f his indicates no
1b570 74 68 69 6e 67 20 6d 6f 72 65 20 6e 65 65 64 73 thing more needs
1b580 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 to be rolled ba
1b590 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 ck.. */. r
1b5a0 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 c = readJournalH
1b5b0 64 72 28 70 50 61 67 65 72 2c 20 69 73 48 6f 74 dr(pPager, isHot
1b5c0 2c 20 73 7a 4a 2c 20 26 6e 52 65 63 2c 20 26 6d , szJ, &nRec, &m
1b5d0 78 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 xPg);. if( rc
1b5e0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 0a !=SQLITE_OK ){ .
1b5f0 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
1b600 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 LITE_DONE ){.
1b610 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1b620 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 _OK;. }.
1b630 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 goto end_play
1b640 62 61 63 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 back;. }..
1b650 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20 30 /* If nRec is 0
1b660 78 66 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20 xffffffff, then
1b670 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 this journal was
1b680 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 created by a pr
1b690 6f 63 65 73 73 0a 20 20 20 20 2a 2a 20 77 6f 72 ocess. ** wor
1b6a0 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 king in no-sync
1b6b0 6d 6f 64 65 2e 20 54 68 69 73 20 6d 65 61 6e 73 mode. This means
1b6c0 20 74 68 61 74 20 74 68 65 20 72 65 73 74 20 6f that the rest o
1b6d0 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 f the journal.
1b6e0 20 20 2a 2a 20 66 69 6c 65 20 63 6f 6e 73 69 73 ** file consis
1b6f0 74 73 20 6f 66 20 70 61 67 65 73 2c 20 74 68 65 ts of pages, the
1b700 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6a re are no more j
1b710 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 73 2e 20 ournal headers.
1b720 43 6f 6d 70 75 74 65 0a 20 20 20 20 2a 2a 20 74 Compute. ** t
1b730 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 he value of nRec
1b740 20 62 61 73 65 64 20 6f 6e 20 74 68 69 73 20 61 based on this a
1b750 73 73 75 6d 70 74 69 6f 6e 2e 0a 20 20 20 20 2a ssumption.. *
1b760 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d /. if( nRec==
1b770 30 78 66 66 66 66 66 66 66 66 20 29 7b 0a 20 20 0xffffffff ){.
1b780 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
1b790 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d er->journalOff==
1b7a0 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 JOURNAL_HDR_SZ(p
1b7b0 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20 Pager) );.
1b7c0 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 28 73 7a nRec = (int)((sz
1b7d0 4a 20 2d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f J - JOURNAL_HDR_
1b7e0 53 5a 28 70 50 61 67 65 72 29 29 2f 4a 4f 55 52 SZ(pPager))/JOUR
1b7f0 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 NAL_PG_SZ(pPager
1b800 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f ));. }.. /
1b810 2a 20 49 66 20 6e 52 65 63 20 69 73 20 30 20 61 * If nRec is 0 a
1b820 6e 64 20 74 68 69 73 20 72 6f 6c 6c 62 61 63 6b nd this rollback
1b830 20 69 73 20 6f 66 20 61 20 74 72 61 6e 73 61 63 is of a transac
1b840 74 69 6f 6e 20 63 72 65 61 74 65 64 20 62 79 20 tion created by
1b850 74 68 69 73 0a 20 20 20 20 2a 2a 20 70 72 6f 63 this. ** proc
1b860 65 73 73 20 61 6e 64 20 69 66 20 74 68 69 73 20 ess and if this
1b870 69 73 20 74 68 65 20 66 69 6e 61 6c 20 68 65 61 is the final hea
1b880 64 65 72 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e der in the journ
1b890 61 6c 2c 20 74 68 65 6e 20 69 74 20 6d 65 61 6e al, then it mean
1b8a0 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 s. ** that th
1b8b0 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 6a is part of the j
1b8c0 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 69 6e 67 ournal was being
1b8d0 20 66 69 6c 6c 65 64 20 62 75 74 20 68 61 73 20 filled but has
1b8e0 6e 6f 74 20 79 65 74 20 62 65 65 6e 0a 20 20 20 not yet been.
1b8f0 20 2a 2a 20 73 79 6e 63 65 64 20 74 6f 20 64 69 ** synced to di
1b900 73 6b 2e 20 20 43 6f 6d 70 75 74 65 20 74 68 65 sk. Compute the
1b910 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
1b920 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 72 65 based on the re
1b930 6d 61 69 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 73 maining. ** s
1b940 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 2e ize of the file.
1b950 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
1b960 68 65 20 74 68 69 72 64 20 74 65 72 6d 20 6f 66 he third term of
1b970 20 74 68 65 20 74 65 73 74 20 77 61 73 20 61 64 the test was ad
1b980 64 65 64 20 74 6f 20 66 69 78 20 74 69 63 6b 65 ded to fix ticke
1b990 74 20 23 32 35 36 35 2e 0a 20 20 20 20 2a 2a 20 t #2565.. **
1b9a0 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 When rolling bac
1b9b0 6b 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 2c k a hot journal,
1b9c0 20 6e 52 65 63 3d 3d 30 20 61 6c 77 61 79 73 20 nRec==0 always
1b9d0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 6e means that the n
1b9e0 65 78 74 0a 20 20 20 20 2a 2a 20 63 68 75 6e 6b ext. ** chunk
1b9f0 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
1ba00 63 6f 6e 74 61 69 6e 73 20 7a 65 72 6f 20 70 61 contains zero pa
1ba10 67 65 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 ges to be rolled
1ba20 20 62 61 63 6b 2e 20 20 42 75 74 0a 20 20 20 20 back. But.
1ba30 2a 2a 20 77 68 65 6e 20 64 6f 69 6e 67 20 61 20 ** when doing a
1ba40 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 20 74 68 65 ROLLBACK and the
1ba50 20 6e 52 65 63 3d 3d 30 20 63 68 75 6e 6b 20 69 nRec==0 chunk i
1ba60 73 20 74 68 65 20 6c 61 73 74 20 63 68 75 6e 6b s the last chunk
1ba70 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a in. ** the j
1ba80 6f 75 72 6e 61 6c 2c 20 69 74 20 6d 65 61 6e 73 ournal, it means
1ba90 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 that the journa
1baa0 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 l might contain
1bab0 61 64 64 69 74 69 6f 6e 61 6c 0a 20 20 20 20 2a additional. *
1bac0 2a 20 70 61 67 65 73 20 74 68 61 74 20 6e 65 65 * pages that nee
1bad0 64 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 d to be rolled b
1bae0 61 63 6b 20 61 6e 64 20 74 68 61 74 20 74 68 65 ack and that the
1baf0 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
1bb00 20 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 . ** should
1bb10 62 65 20 63 6f 6d 70 75 74 65 64 20 62 61 73 65 be computed base
1bb20 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c d on the journal
1bb30 20 66 69 6c 65 20 73 69 7a 65 2e 0a 20 20 20 20 file size..
1bb40 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 63 3d */. if( nRec=
1bb50 3d 30 20 26 26 20 21 69 73 48 6f 74 20 26 26 0a =0 && !isHot &&.
1bb60 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
1bb70 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e journalHdr+JOURN
1bb80 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
1bb90 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e )==pPager->journ
1bba0 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 20 20 6e alOff ){. n
1bbb0 52 65 63 20 3d 20 28 69 6e 74 29 28 28 73 7a 4a Rec = (int)((szJ
1bbc0 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e - pPager->journ
1bbd0 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41 4c alOff) / JOURNAL
1bbe0 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 3b _PG_SZ(pPager));
1bbf0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 . }.. /* I
1bc00 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 f this is the fi
1bc10 72 73 74 20 68 65 61 64 65 72 20 72 65 61 64 20 rst header read
1bc20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c from the journal
1bc30 2c 20 74 72 75 6e 63 61 74 65 20 74 68 65 0a 20 , truncate the.
1bc40 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 ** database f
1bc50 69 6c 65 20 62 61 63 6b 20 74 6f 20 69 74 73 20 ile back to its
1bc60 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 2e 0a 20 original size..
1bc70 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 */. if( pP
1bc80 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
1bc90 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a ==JOURNAL_HDR_SZ
1bca0 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 (pPager) ){.
1bcb0 20 20 72 63 20 3d 20 70 61 67 65 72 5f 74 72 75 rc = pager_tru
1bcc0 6e 63 61 74 65 28 70 50 61 67 65 72 2c 20 6d 78 ncate(pPager, mx
1bcd0 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 Pg);. if( r
1bce0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1bcf0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
1bd00 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 20 _playback;.
1bd10 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d }. pPager-
1bd20 3e 64 62 53 69 7a 65 20 3d 20 6d 78 50 67 3b 0a >dbSize = mxPg;.
1bd30 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f }.. /* Co
1bd40 70 79 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 py original page
1bd50 73 20 6f 75 74 20 6f 66 20 74 68 65 20 6a 6f 75 s out of the jou
1bd60 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20 69 6e rnal and back in
1bd70 74 6f 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 64 to the . ** d
1bd80 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
1bd90 2f 6f 72 20 70 61 67 65 20 63 61 63 68 65 2e 0a /or page cache..
1bda0 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 75 */. for(u
1bdb0 3d 30 3b 20 75 3c 6e 52 65 63 3b 20 75 2b 2b 29 =0; u<nRec; u++)
1bdc0 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 65 64 {. if( need
1bdd0 50 61 67 65 72 52 65 73 65 74 20 29 7b 0a 20 20 PagerReset ){.
1bde0 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 pager_rese
1bdf0 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 t(pPager);.
1be00 20 20 20 6e 65 65 64 50 61 67 65 72 52 65 73 65 needPagerRese
1be10 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 t = 0;. }.
1be20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
1be30 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 playback_one_pag
1be40 65 28 70 50 61 67 65 72 2c 26 70 50 61 67 65 72 e(pPager,&pPager
1be50 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 30 2c 31 ->journalOff,0,1
1be60 2c 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ,0);. if( r
1be70 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1be80 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
1be90 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 SQLITE_DONE ){.
1bea0 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d pPager-
1beb0 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a >journalOff = sz
1bec0 4a 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 J;. bre
1bed0 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 ak;. }els
1bee0 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 e if( rc==SQLITE
1bef0 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
1bf00 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f D ){. /
1bf10 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c * If the journal
1bf20 20 68 61 73 20 62 65 65 6e 20 74 72 75 6e 63 61 has been trunca
1bf30 74 65 64 2c 20 73 69 6d 70 6c 79 20 73 74 6f 70 ted, simply stop
1bf40 20 72 65 61 64 69 6e 67 20 61 6e 64 0a 20 20 20 reading and.
1bf50 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 ** proces
1bf60 73 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c sing the journal
1bf70 2e 20 54 68 69 73 20 6d 69 67 68 74 20 68 61 70 . This might hap
1bf80 70 65 6e 20 69 66 20 74 68 65 20 6a 6f 75 72 6e pen if the journ
1bf90 61 6c 20 77 61 73 0a 20 20 20 20 20 20 20 20 20 al was.
1bfa0 20 2a 2a 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 ** not complete
1bfb0 6c 79 20 77 72 69 74 74 65 6e 20 61 6e 64 20 73 ly written and s
1bfc0 79 6e 63 65 64 20 70 72 69 6f 72 20 74 6f 20 61 ynced prior to a
1bfd0 20 63 72 61 73 68 2e 20 20 49 6e 20 74 68 61 74 crash. In that
1bfe0 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 61 . ** ca
1bff0 73 65 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 se, the database
1c000 20 73 68 6f 75 6c 64 20 68 61 76 65 20 6e 65 76 should have nev
1c010 65 72 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 er been written
1c020 69 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 in the.
1c030 20 2a 2a 20 66 69 72 73 74 20 70 6c 61 63 65 20 ** first place
1c040 73 6f 20 69 74 20 69 73 20 4f 4b 20 74 6f 20 73 so it is OK to s
1c050 69 6d 70 6c 79 20 61 62 61 6e 64 6f 6e 20 74 68 imply abandon th
1c060 65 20 72 6f 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 e rollback. */.
1c070 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 rc = SQ
1c080 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 LITE_OK;.
1c090 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 goto end_play
1c0a0 62 61 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 back;. }e
1c0b0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f lse{. /
1c0c0 2a 20 49 66 20 77 65 20 61 72 65 20 75 6e 61 62 * If we are unab
1c0d0 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 le to rollback,
1c0e0 71 75 69 74 20 61 6e 64 20 72 65 74 75 72 6e 20 quit and return
1c0f0 74 68 65 20 65 72 72 6f 72 0a 20 20 20 20 20 20 the error.
1c100 20 20 20 20 2a 2a 20 63 6f 64 65 2e 20 20 54 68 ** code. Th
1c110 69 73 20 77 69 6c 6c 20 63 61 75 73 65 20 74 68 is will cause th
1c120 65 20 70 61 67 65 72 20 74 6f 20 65 6e 74 65 72 e pager to enter
1c130 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
1c140 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 6f . ** so
1c150 20 74 68 61 74 20 6e 6f 20 66 75 72 74 68 65 72 that no further
1c160 20 68 61 72 6d 20 77 69 6c 6c 20 62 65 20 64 6f harm will be do
1c170 6e 65 2e 20 20 50 65 72 68 61 70 73 20 74 68 65 ne. Perhaps the
1c180 20 6e 65 78 74 0a 20 20 20 20 20 20 20 20 20 20 next.
1c190 2a 2a 20 70 72 6f 63 65 73 73 20 74 6f 20 63 6f ** process to co
1c1a0 6d 65 20 61 6c 6f 6e 67 20 77 69 6c 6c 20 62 65 me along will be
1c1b0 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 able to rollbac
1c1c0 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a k the database..
1c1d0 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 */.
1c1e0 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
1c1f0 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 20 20 playback;.
1c200 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1c210 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 }. }. /*NOTREA
1c220 43 48 45 44 2a 2f 0a 20 20 61 73 73 65 72 74 28 CHED*/. assert(
1c230 20 30 20 29 3b 0a 0a 65 6e 64 5f 70 6c 61 79 62 0 );..end_playb
1c240 61 63 6b 3a 0a 20 20 2f 2a 20 46 6f 6c 6c 6f 77 ack:. /* Follow
1c250 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 ing a rollback,
1c260 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1c270 65 20 73 68 6f 75 6c 64 20 62 65 20 62 61 63 6b e should be back
1c280 20 69 6e 20 69 74 73 20 6f 72 69 67 69 6e 61 6c in its original
1c290 0a 20 20 2a 2a 20 73 74 61 74 65 20 70 72 69 6f . ** state prio
1c2a0 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f r to the start o
1c2b0 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f f the transactio
1c2c0 6e 2c 20 73 6f 20 69 6e 76 6f 6b 65 20 74 68 65 n, so invoke the
1c2d0 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 46 43 4e . ** SQLITE_FCN
1c2e0 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 TL_DB_UNCHANGED
1c2f0 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 6d 65 74 file-control met
1c300 68 6f 64 20 74 6f 20 64 69 73 61 62 6c 65 20 74 hod to disable t
1c310 68 65 0a 20 20 2a 2a 20 61 73 73 65 72 74 69 6f he. ** assertio
1c320 6e 20 74 68 61 74 20 74 68 65 20 74 72 61 6e 73 n that the trans
1c330 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 77 action counter w
1c340 61 73 20 6d 6f 64 69 66 69 65 64 2e 0a 20 20 2a as modified.. *
1c350 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
1c360 44 45 42 55 47 0a 20 20 69 66 28 20 70 50 61 67 DEBUG. if( pPag
1c370 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 er->fd->pMethods
1c380 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f ){. sqlite3O
1c390 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 48 69 6e 74 sFileControlHint
1c3a0 28 70 50 61 67 65 72 2d 3e 66 64 2c 53 51 4c 49 (pPager->fd,SQLI
1c3b0 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 TE_FCNTL_DB_UNCH
1c3c0 41 4e 47 45 44 2c 30 29 3b 0a 20 20 7d 0a 23 65 ANGED,0);. }.#e
1c3d0 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 ndif.. /* If th
1c3e0 69 73 20 70 6c 61 79 62 61 63 6b 20 69 73 20 68 is playback is h
1c3f0 61 70 70 65 6e 69 6e 67 20 61 75 74 6f 6d 61 74 appening automat
1c400 69 63 61 6c 6c 79 20 61 73 20 61 20 72 65 73 75 ically as a resu
1c410 6c 74 20 6f 66 20 61 6e 20 49 4f 20 6f 72 20 0a lt of an IO or .
1c420 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f ** malloc erro
1c430 72 20 74 68 61 74 20 6f 63 63 75 72 72 65 64 20 r that occurred
1c440 61 66 74 65 72 20 74 68 65 20 63 68 61 6e 67 65 after the change
1c450 2d 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64 -counter was upd
1c460 61 74 65 64 20 62 75 74 20 0a 20 20 2a 2a 20 62 ated but . ** b
1c470 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61 efore the transa
1c480 63 74 69 6f 6e 20 77 61 73 20 63 6f 6d 6d 69 74 ction was commit
1c490 74 65 64 2c 20 74 68 65 6e 20 74 68 65 20 63 68 ted, then the ch
1c4a0 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 0a 20 20 ange-counter .
1c4b0 2a 2a 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 ** modification
1c4c0 6d 61 79 20 6a 75 73 74 20 68 61 76 65 20 62 65 may just have be
1c4d0 65 6e 20 72 65 76 65 72 74 65 64 2e 20 49 66 20 en reverted. If
1c4e0 74 68 69 73 20 68 61 70 70 65 6e 73 20 69 6e 20 this happens in
1c4f0 65 78 63 6c 75 73 69 76 65 20 0a 20 20 2a 2a 20 exclusive . **
1c500 6d 6f 64 65 2c 20 74 68 65 6e 20 73 75 62 73 65 mode, then subse
1c510 71 75 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f quent transactio
1c520 6e 73 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 ns performed by
1c530 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 the connection w
1c540 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 75 70 64 ill not. ** upd
1c550 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d 63 ate the change-c
1c560 6f 75 6e 74 65 72 20 61 74 20 61 6c 6c 2e 20 54 ounter at all. T
1c570 68 69 73 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 his may lead to
1c580 63 61 63 68 65 20 69 6e 63 6f 6e 73 69 73 74 65 cache inconsiste
1c590 6e 63 79 0a 20 20 2a 2a 20 70 72 6f 62 6c 65 6d ncy. ** problem
1c5a0 73 20 66 6f 72 20 6f 74 68 65 72 20 70 72 6f 63 s for other proc
1c5b0 65 73 73 65 73 20 61 74 20 73 6f 6d 65 20 70 6f esses at some po
1c5c0 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 int in the futur
1c5d0 65 2e 20 53 6f 2c 20 6a 75 73 74 0a 20 20 2a 2a e. So, just. **
1c5e0 20 69 6e 20 63 61 73 65 20 74 68 69 73 20 68 61 in case this ha
1c5f0 73 20 68 61 70 70 65 6e 65 64 2c 20 63 6c 65 61 s happened, clea
1c600 72 20 74 68 65 20 63 68 61 6e 67 65 43 6f 75 6e r the changeCoun
1c610 74 44 6f 6e 65 20 66 6c 61 67 20 6e 6f 77 2e 0a tDone flag now..
1c620 20 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 63 */. pPager->c
1c630 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d hangeCountDone =
1c640 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c pPager->tempFil
1c650 65 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 e;.. if( rc==SQ
1c660 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 7a LITE_OK ){. z
1c670 4d 61 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d Master = pPager-
1c680 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 >pTmpSpace;.
1c690 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a rc = readMasterJ
1c6a0 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a ournal(pPager->j
1c6b0 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 fd, zMaster, pPa
1c6c0 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 ger->pVfs->mxPat
1c6d0 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 74 65 hname+1);. te
1c6e0 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 stcase( rc!=SQLI
1c6f0 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 TE_OK );. }. i
1c700 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1c710 0a 20 20 20 26 26 20 28 70 50 61 67 65 72 2d 3e . && (pPager->
1c720 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 57 52 eState>=PAGER_WR
1c730 49 54 45 52 5f 44 42 4d 4f 44 20 7c 7c 20 70 50 ITER_DBMOD || pP
1c740 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 ager->eState==PA
1c750 47 45 52 5f 4f 50 45 4e 29 0a 20 20 29 7b 0a 20 GER_OPEN). ){.
1c760 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
1c770 61 67 65 72 53 79 6e 63 28 70 50 61 67 65 72 29 agerSync(pPager)
1c780 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d ;. }. if( rc==
1c790 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1c7a0 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f rc = pager_end_
1c7b0 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 transaction(pPag
1c7c0 65 72 2c 20 7a 4d 61 73 74 65 72 5b 30 5d 21 3d er, zMaster[0]!=
1c7d0 27 5c 30 27 2c 20 30 29 3b 0a 20 20 20 20 74 65 '\0', 0);. te
1c7e0 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 stcase( rc!=SQLI
1c7f0 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 TE_OK );. }. i
1c800 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1c810 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 26 && zMaster[0] &
1c820 26 20 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a 20 & res ){. /*
1c830 49 66 20 74 68 65 72 65 20 77 61 73 20 61 20 6d If there was a m
1c840 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61 6e aster journal an
1c850 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 d this routine w
1c860 69 6c 6c 20 72 65 74 75 72 6e 20 73 75 63 63 65 ill return succe
1c870 73 73 2c 0a 20 20 20 20 2a 2a 20 73 65 65 20 69 ss,. ** see i
1c880 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 f it is possible
1c890 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6d to delete the m
1c8a0 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 20 aster journal..
1c8b0 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 */. rc = p
1c8c0 61 67 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 70 ager_delmaster(p
1c8d0 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 3b Pager, zMaster);
1c8e0 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 . testcase( r
1c8f0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a c!=SQLITE_OK );.
1c900 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 50 61 }.. /* The Pa
1c910 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 ger.sectorSize v
1c920 61 72 69 61 62 6c 65 20 6d 61 79 20 68 61 76 65 ariable may have
1c930 20 62 65 65 6e 20 75 70 64 61 74 65 64 20 77 68 been updated wh
1c940 69 6c 65 20 72 6f 6c 6c 69 6e 67 0a 20 20 2a 2a ile rolling. **
1c950 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 back a journal
1c960 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 6f created by a pro
1c970 63 65 73 73 20 77 69 74 68 20 61 20 64 69 66 66 cess with a diff
1c980 65 72 65 6e 74 20 73 65 63 74 6f 72 20 73 69 7a erent sector siz
1c990 65 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 52 65 e. ** value. Re
1c9a0 73 65 74 20 69 74 20 74 6f 20 74 68 65 20 63 6f set it to the co
1c9b0 72 72 65 63 74 20 76 61 6c 75 65 20 66 6f 72 20 rrect value for
1c9c0 74 68 69 73 20 70 72 6f 63 65 73 73 2e 0a 20 20 this process..
1c9d0 2a 2f 0a 20 20 73 65 74 53 65 63 74 6f 72 53 69 */. setSectorSi
1c9e0 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 ze(pPager);. re
1c9f0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
1ca00 2a 2a 20 52 65 61 64 20 74 68 65 20 63 6f 6e 74 ** Read the cont
1ca10 65 6e 74 20 66 6f 72 20 70 61 67 65 20 70 50 67 ent for page pPg
1ca20 20 6f 75 74 20 6f 66 20 74 68 65 20 64 61 74 61 out of the data
1ca30 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 69 6e base file and in
1ca40 74 6f 20 0a 2a 2a 20 70 50 67 2d 3e 70 44 61 74 to .** pPg->pDat
1ca50 61 2e 20 41 20 73 68 61 72 65 64 20 6c 6f 63 6b a. A shared lock
1ca60 20 6f 72 20 67 72 65 61 74 65 72 20 6d 75 73 74 or greater must
1ca70 20 62 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 be held on the
1ca80 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 database.** file
1ca90 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e before this fun
1caa0 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e ction is called.
1cab0 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 67 65 20 31 .**.** If page 1
1cac0 20 69 73 20 72 65 61 64 2c 20 74 68 65 6e 20 74 is read, then t
1cad0 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61 67 65 he value of Page
1cae0 72 2e 64 62 46 69 6c 65 56 65 72 73 5b 5d 20 69 r.dbFileVers[] i
1caf0 73 20 73 65 74 20 74 6f 0a 2a 2a 20 74 68 65 20 s set to.** the
1cb00 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 value read from
1cb10 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1cb20 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 e..**.** If an I
1cb30 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 O error occurs,
1cb40 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f then the IO erro
1cb50 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f r is returned to
1cb60 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 20 the caller..**
1cb70 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 Otherwise, SQLIT
1cb80 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
1cb90 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1cba0 72 65 61 64 44 62 50 61 67 65 28 50 67 48 64 72 readDbPage(PgHdr
1cbb0 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 *pPg){. Pager
1cbc0 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
1cbd0 50 61 67 65 72 3b 20 2f 2a 20 50 61 67 65 72 20 Pager; /* Pager
1cbe0 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65 object associate
1cbf0 64 20 77 69 74 68 20 70 61 67 65 20 70 50 67 20 d with page pPg
1cc00 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d */. Pgno pgno =
1cc10 20 70 50 67 2d 3e 70 67 6e 6f 3b 20 20 20 20 20 pPg->pgno;
1cc20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 /* Page number
1cc30 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e to read */. in
1cc40 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
1cc50 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 ; /* Re
1cc60 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 turn code */. i
1cc70 6e 74 20 69 73 49 6e 57 61 6c 20 3d 20 30 3b 20 nt isInWal = 0;
1cc80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1cc90 72 75 65 20 69 66 20 70 61 67 65 20 69 73 20 69 rue if page is i
1cca0 6e 20 6c 6f 67 20 66 69 6c 65 20 2a 2f 0a 20 20 n log file */.
1ccb0 69 6e 74 20 70 67 73 7a 20 3d 20 70 50 61 67 65 int pgsz = pPage
1ccc0 72 2d 3e 70 61 67 65 53 69 7a 65 3b 20 2f 2a 20 r->pageSize; /*
1ccd0 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1cce0 74 6f 20 72 65 61 64 20 2a 2f 0a 0a 20 20 61 73 to read */.. as
1ccf0 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 sert( pPager->eS
1cd00 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 41 44 tate>=PAGER_READ
1cd10 45 52 20 26 26 20 21 4d 45 4d 44 42 20 29 3b 0a ER && !MEMDB );.
1cd20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e assert( isOpen
1cd30 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a (pPager->fd) );.
1cd40 0a 20 20 69 66 28 20 4e 45 56 45 52 28 21 69 73 . if( NEVER(!is
1cd50 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
1cd60 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 ) ){. assert(
1cd70 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c pPager->tempFil
1cd80 65 20 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 e );. memset(
1cd90 70 50 67 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 pPg->pData, 0, p
1cda0 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 Pager->pageSize)
1cdb0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
1cdc0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 ITE_OK;. }.. i
1cdd0 66 28 20 70 61 67 65 72 55 73 65 57 61 6c 28 70 f( pagerUseWal(p
1cde0 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 2f 2a Pager) ){. /*
1cdf0 20 54 72 79 20 74 6f 20 70 75 6c 6c 20 74 68 65 Try to pull the
1ce00 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 77 page from the w
1ce10 72 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 2e 20 rite-ahead log.
1ce20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 */. rc = sqli
1ce30 74 65 33 57 61 6c 52 65 61 64 28 70 50 61 67 65 te3WalRead(pPage
1ce40 72 2d 3e 70 57 61 6c 2c 20 70 67 6e 6f 2c 20 26 r->pWal, pgno, &
1ce50 69 73 49 6e 57 61 6c 2c 20 70 67 73 7a 2c 20 70 isInWal, pgsz, p
1ce60 50 67 2d 3e 70 44 61 74 61 29 3b 0a 20 20 7d 0a Pg->pData);. }.
1ce70 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1ce80 5f 4f 4b 20 26 26 20 21 69 73 49 6e 57 61 6c 20 _OK && !isInWal
1ce90 29 7b 0a 20 20 20 20 69 36 34 20 69 4f 66 66 73 ){. i64 iOffs
1cea0 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 et = (pgno-1)*(i
1ceb0 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 64)pPager->pageS
1cec0 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 ize;. rc = sq
1ced0 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 lite3OsRead(pPag
1cee0 65 72 2d 3e 66 64 2c 20 70 50 67 2d 3e 70 44 61 er->fd, pPg->pDa
1cef0 74 61 2c 20 70 67 73 7a 2c 20 69 4f 66 66 73 65 ta, pgsz, iOffse
1cf00 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d t);. if( rc==
1cf10 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f SQLITE_IOERR_SHO
1cf20 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 RT_READ ){.
1cf30 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1cf40 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 . }. }.. if
1cf50 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 ( pgno==1 ){.
1cf60 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
1cf70 20 2f 2a 20 49 66 20 74 68 65 20 72 65 61 64 20 /* If the read
1cf80 69 73 20 75 6e 73 75 63 63 65 73 73 66 75 6c 2c is unsuccessful,
1cf90 20 73 65 74 20 74 68 65 20 64 62 46 69 6c 65 56 set the dbFileV
1cfa0 65 72 73 5b 5d 20 74 6f 20 73 6f 6d 65 74 68 69 ers[] to somethi
1cfb0 6e 67 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74 ng. ** that
1cfc0 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 61 will never be a
1cfd0 20 76 61 6c 69 64 20 66 69 6c 65 20 76 65 72 73 valid file vers
1cfe0 69 6f 6e 2e 20 20 64 62 46 69 6c 65 56 65 72 73 ion. dbFileVers
1cff0 5b 5d 20 69 73 20 61 20 63 6f 70 79 0a 20 20 20 [] is a copy.
1d000 20 20 20 2a 2a 20 6f 66 20 62 79 74 65 73 20 32 ** of bytes 2
1d010 34 2e 2e 33 39 20 6f 66 20 74 68 65 20 64 61 74 4..39 of the dat
1d020 61 62 61 73 65 2e 20 20 42 79 74 65 73 20 32 38 abase. Bytes 28
1d030 2e 2e 33 31 20 73 68 6f 75 6c 64 20 61 6c 77 61 ..31 should alwa
1d040 79 73 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 7a ys be. ** z
1d050 65 72 6f 20 6f 72 20 74 68 65 20 73 69 7a 65 20 ero or the size
1d060 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1d070 69 6e 20 70 61 67 65 2e 20 42 79 74 65 73 20 33 in page. Bytes 3
1d080 32 2e 2e 33 35 20 61 6e 64 20 33 35 2e 2e 33 39 2..35 and 35..39
1d090 0a 20 20 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 . ** should
1d0a0 20 62 65 20 70 61 67 65 20 6e 75 6d 62 65 72 73 be page numbers
1d0b0 20 77 68 69 63 68 20 61 72 65 20 6e 65 76 65 72 which are never
1d0c0 20 30 78 66 66 66 66 66 66 66 66 2e 20 20 53 6f 0xffffffff. So
1d0d0 20 66 69 6c 6c 69 6e 67 0a 20 20 20 20 20 20 2a filling. *
1d0e0 2a 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 * pPager->dbFile
1d0f0 56 65 72 73 5b 5d 20 77 69 74 68 20 61 6c 6c 20 Vers[] with all
1d100 30 78 66 66 20 62 79 74 65 73 20 73 68 6f 75 6c 0xff bytes shoul
1d110 64 20 73 75 66 66 69 63 65 2e 0a 20 20 20 20 20 d suffice..
1d120 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 46 6f 72 **. ** For
1d130 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20 64 61 an encrypted da
1d140 74 61 62 61 73 65 2c 20 74 68 65 20 73 69 74 75 tabase, the situ
1d150 61 74 69 6f 6e 20 69 73 20 6d 6f 72 65 20 63 6f ation is more co
1d160 6d 70 6c 65 78 3a 20 20 62 79 74 65 73 0a 20 20 mplex: bytes.
1d170 20 20 20 20 2a 2a 20 32 34 2e 2e 33 39 20 6f 66 ** 24..39 of
1d180 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 72 the database ar
1d190 65 20 77 68 69 74 65 20 6e 6f 69 73 65 2e 20 20 e white noise.
1d1a0 42 75 74 20 74 68 65 20 70 72 6f 62 61 62 69 6c But the probabil
1d1b0 69 74 79 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 ity of. **
1d1c0 77 68 69 74 65 20 6e 6f 69 73 69 6e 67 20 65 71 white noising eq
1d1d0 75 61 6c 69 6e 67 20 31 36 20 62 79 74 65 73 20 ualing 16 bytes
1d1e0 6f 66 20 30 78 66 66 20 69 73 20 76 61 6e 69 73 of 0xff is vanis
1d1f0 68 69 6e 67 6c 79 20 73 6d 61 6c 6c 20 73 6f 0a hingly small so.
1d200 20 20 20 20 20 20 2a 2a 20 77 65 20 73 68 6f 75 ** we shou
1d210 6c 64 20 73 74 69 6c 6c 20 62 65 20 6f 6b 2e 0a ld still be ok..
1d220 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6d */. m
1d230 65 6d 73 65 74 28 70 50 61 67 65 72 2d 3e 64 62 emset(pPager->db
1d240 46 69 6c 65 56 65 72 73 2c 20 30 78 66 66 2c 20 FileVers, 0xff,
1d250 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 sizeof(pPager->d
1d260 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 bFileVers));.
1d270 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 38 }else{. u8
1d280 20 2a 64 62 46 69 6c 65 56 65 72 73 20 3d 20 26 *dbFileVers = &
1d290 28 28 75 38 2a 29 70 50 67 2d 3e 70 44 61 74 61 ((u8*)pPg->pData
1d2a0 29 5b 32 34 5d 3b 0a 20 20 20 20 20 20 6d 65 6d )[24];. mem
1d2b0 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62 46 cpy(&pPager->dbF
1d2c0 69 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65 56 ileVers, dbFileV
1d2d0 65 72 73 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 ers, sizeof(pPag
1d2e0 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 er->dbFileVers))
1d2f0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 43 4f ;. }. }. CO
1d300 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 50 67 DEC1(pPager, pPg
1d310 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 33 ->pData, pgno, 3
1d320 2c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f , rc = SQLITE_NO
1d330 4d 45 4d 29 3b 0a 0a 20 20 50 41 47 45 52 5f 49 MEM);.. PAGER_I
1d340 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 NCR(sqlite3_page
1d350 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 29 3b r_readdb_count);
1d360 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 . PAGER_INCR(pP
1d370 61 67 65 72 2d 3e 6e 52 65 61 64 29 3b 0a 20 20 ager->nRead);.
1d380 49 4f 54 52 41 43 45 28 28 22 50 47 49 4e 20 25 IOTRACE(("PGIN %
1d390 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c p %d\n", pPager,
1d3a0 20 70 67 6e 6f 29 29 3b 0a 20 20 50 41 47 45 52 pgno));. PAGER
1d3b0 54 52 41 43 45 28 28 22 46 45 54 43 48 20 25 64 TRACE(("FETCH %d
1d3c0 20 70 61 67 65 20 25 64 20 68 61 73 68 28 25 30 page %d hash(%0
1d3d0 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 8x)\n",.
1d3e0 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 PAGERID(p
1d3f0 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 Pager), pgno, pa
1d400 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 ger_pagehash(pPg
1d410 29 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 )));.. return r
1d420 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 70 64 61 c;.}../*.** Upda
1d430 74 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 te the value of
1d440 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
1d450 65 72 20 61 74 20 6f 66 66 73 65 74 73 20 32 34 er at offsets 24
1d460 20 61 6e 64 20 39 32 20 69 6e 0a 2a 2a 20 74 68 and 92 in.** th
1d470 65 20 68 65 61 64 65 72 20 61 6e 64 20 74 68 65 e header and the
1d480 20 73 71 6c 69 74 65 20 76 65 72 73 69 6f 6e 20 sqlite version
1d490 6e 75 6d 62 65 72 20 61 74 20 6f 66 66 73 65 74 number at offset
1d4a0 20 39 36 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 96..**.** This
1d4b0 69 73 20 61 6e 20 75 6e 63 6f 6e 64 69 74 69 6f is an unconditio
1d4c0 6e 61 6c 20 75 70 64 61 74 65 2e 20 20 53 65 65 nal update. See
1d4d0 20 61 6c 73 6f 20 74 68 65 20 70 61 67 65 72 5f also the pager_
1d4e0 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 incr_changecount
1d4f0 65 72 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 er().** routine
1d500 77 68 69 63 68 20 6f 6e 6c 79 20 75 70 64 61 74 which only updat
1d510 65 73 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f es the change-co
1d520 75 6e 74 65 72 20 69 66 20 74 68 65 20 75 70 64 unter if the upd
1d530 61 74 65 20 69 73 20 61 63 74 75 61 6c 6c 79 0a ate is actually.
1d540 2a 2a 20 6e 65 65 64 65 64 2c 20 61 73 20 64 65 ** needed, as de
1d550 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 termined by the
1d560 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f pPager->changeCo
1d570 75 6e 74 44 6f 6e 65 20 73 74 61 74 65 20 76 61 untDone state va
1d580 72 69 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 riable..*/.stati
1d590 63 20 76 6f 69 64 20 70 61 67 65 72 5f 77 72 69 c void pager_wri
1d5a0 74 65 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 te_changecounter
1d5b0 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 (PgHdr *pPg){.
1d5c0 75 33 32 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 u32 change_count
1d5d0 65 72 3b 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d er;.. /* Increm
1d5e0 65 6e 74 20 74 68 65 20 76 61 6c 75 65 20 6a 75 ent the value ju
1d5f0 73 74 20 72 65 61 64 20 61 6e 64 20 77 72 69 74 st read and writ
1d600 65 20 69 74 20 62 61 63 6b 20 74 6f 20 62 79 74 e it back to byt
1d610 65 20 32 34 2e 20 2a 2f 0a 20 20 63 68 61 6e 67 e 24. */. chang
1d620 65 5f 63 6f 75 6e 74 65 72 20 3d 20 73 71 6c 69 e_counter = sqli
1d630 74 65 33 47 65 74 34 62 79 74 65 28 28 75 38 2a te3Get4byte((u8*
1d640 29 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 64 62 )pPg->pPager->db
1d650 46 69 6c 65 56 65 72 73 29 2b 31 3b 0a 20 20 70 FileVers)+1;. p
1d660 75 74 33 32 62 69 74 73 28 28 28 63 68 61 72 2a ut32bits(((char*
1d670 29 70 50 67 2d 3e 70 44 61 74 61 29 2b 32 34 2c )pPg->pData)+24,
1d680 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 29 change_counter)
1d690 3b 0a 0a 20 20 2f 2a 20 41 6c 73 6f 20 73 74 6f ;.. /* Also sto
1d6a0 72 65 20 74 68 65 20 53 51 4c 69 74 65 20 76 65 re the SQLite ve
1d6b0 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 69 6e 20 rsion number in
1d6c0 62 79 74 65 73 20 39 36 2e 2e 39 39 20 61 6e 64 bytes 96..99 and
1d6d0 20 69 6e 0a 20 20 2a 2a 20 62 79 74 65 73 20 39 in. ** bytes 9
1d6e0 32 2e 2e 39 35 20 73 74 6f 72 65 20 74 68 65 20 2..95 store the
1d6f0 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 66 change counter f
1d700 6f 72 20 77 68 69 63 68 20 74 68 65 20 76 65 72 or which the ver
1d710 73 69 6f 6e 20 6e 75 6d 62 65 72 0a 20 20 2a 2a sion number. **
1d720 20 69 73 20 76 61 6c 69 64 2e 20 2a 2f 0a 20 20 is valid. */.
1d730 70 75 74 33 32 62 69 74 73 28 28 28 63 68 61 72 put32bits(((char
1d740 2a 29 70 50 67 2d 3e 70 44 61 74 61 29 2b 39 32 *)pPg->pData)+92
1d750 2c 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 , change_counter
1d760 29 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28 28 );. put32bits((
1d770 28 63 68 61 72 2a 29 70 50 67 2d 3e 70 44 61 74 (char*)pPg->pDat
1d780 61 29 2b 39 36 2c 20 53 51 4c 49 54 45 5f 56 45 a)+96, SQLITE_VE
1d790 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 29 3b 0a 7d RSION_NUMBER);.}
1d7a0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1d7b0 5f 4f 4d 49 54 5f 57 41 4c 0a 2f 2a 0a 2a 2a 20 _OMIT_WAL./*.**
1d7c0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
1d7d0 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f invoked once fo
1d7e0 72 20 65 61 63 68 20 70 61 67 65 20 74 68 61 74 r each page that
1d7f0 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
1d800 6e 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69 6e n .** written in
1d810 74 6f 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 20 to the log file
1d820 77 68 65 6e 20 61 20 57 41 4c 20 74 72 61 6e 73 when a WAL trans
1d830 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 action is rolled
1d840 20 62 61 63 6b 2e 0a 2a 2a 20 50 61 72 61 6d 65 back..** Parame
1d850 74 65 72 20 69 50 67 20 69 73 20 74 68 65 20 70 ter iPg is the p
1d860 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 age number of sa
1d870 69 64 20 70 61 67 65 2e 20 54 68 65 20 70 43 74 id page. The pCt
1d880 78 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 69 x argument .** i
1d890 73 20 61 63 74 75 61 6c 6c 79 20 61 20 70 6f 69 s actually a poi
1d8a0 6e 74 65 72 20 74 6f 20 74 68 65 20 50 61 67 65 nter to the Page
1d8b0 72 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a r structure..**.
1d8c0 2a 2a 20 49 66 20 70 61 67 65 20 69 50 67 20 69 ** If page iPg i
1d8d0 73 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 s present in the
1d8e0 20 63 61 63 68 65 2c 20 61 6e 64 20 68 61 73 20 cache, and has
1d8f0 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 no outstanding r
1d900 65 66 65 72 65 6e 63 65 73 2c 0a 2a 2a 20 69 74 eferences,.** it
1d910 20 69 73 20 64 69 73 63 61 72 64 65 64 2e 20 4f is discarded. O
1d920 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 therwise, if the
1d930 72 65 20 61 72 65 20 6f 6e 65 20 6f 72 20 6d 6f re are one or mo
1d940 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a 2a re outstanding.*
1d950 2a 20 72 65 66 65 72 65 6e 63 65 73 2c 20 74 68 * references, th
1d960 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 e page content i
1d970 73 20 72 65 6c 6f 61 64 65 64 20 66 72 6f 6d 20 s reloaded from
1d980 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 66 the database. If
1d990 20 74 68 65 0a 2a 2a 20 61 74 74 65 6d 70 74 20 the.** attempt
1d9a0 74 6f 20 72 65 6c 6f 61 64 20 63 6f 6e 74 65 6e to reload conten
1d9b0 74 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 t from the datab
1d9c0 61 73 65 20 69 73 20 72 65 71 75 69 72 65 64 20 ase is required
1d9d0 61 6e 64 20 66 61 69 6c 73 2c 20 0a 2a 2a 20 72 and fails, .** r
1d9e0 65 74 75 72 6e 20 61 6e 20 53 51 4c 69 74 65 20 eturn an SQLite
1d9f0 65 72 72 6f 72 20 63 6f 64 65 2e 20 4f 74 68 65 error code. Othe
1da00 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b rwise, SQLITE_OK
1da10 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1da20 70 61 67 65 72 55 6e 64 6f 43 61 6c 6c 62 61 63 pagerUndoCallbac
1da30 6b 28 76 6f 69 64 20 2a 70 43 74 78 2c 20 50 67 k(void *pCtx, Pg
1da40 6e 6f 20 69 50 67 29 7b 0a 20 20 69 6e 74 20 72 no iPg){. int r
1da50 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1da60 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
1da70 20 28 50 61 67 65 72 20 2a 29 70 43 74 78 3b 0a (Pager *)pCtx;.
1da80 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a 20 PgHdr *pPg;..
1da90 20 70 50 67 20 3d 20 73 71 6c 69 74 65 33 50 61 pPg = sqlite3Pa
1daa0 67 65 72 4c 6f 6f 6b 75 70 28 70 50 61 67 65 72 gerLookup(pPager
1dab0 2c 20 69 50 67 29 3b 0a 20 20 69 66 28 20 70 50 , iPg);. if( pP
1dac0 67 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c g ){. if( sql
1dad0 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 ite3PcachePageRe
1dae0 66 63 6f 75 6e 74 28 70 50 67 29 3d 3d 31 20 29 fcount(pPg)==1 )
1daf0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 {. sqlite3P
1db00 63 61 63 68 65 44 72 6f 70 28 70 50 67 29 3b 0a cacheDrop(pPg);.
1db10 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1db20 20 72 63 20 3d 20 72 65 61 64 44 62 50 61 67 65 rc = readDbPage
1db30 28 70 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 (pPg);. if(
1db40 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1db50 7b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 {. pPager
1db60 2d 3e 78 52 65 69 6e 69 74 65 72 28 70 50 67 29 ->xReiniter(pPg)
1db70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1db80 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
1db90 66 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20 f(pPg);. }.
1dba0 7d 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 6c 79 }.. /* Normally
1dbb0 2c 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 , if a transacti
1dbc0 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 on is rolled bac
1dbd0 6b 2c 20 61 6e 79 20 62 61 63 6b 75 70 20 70 72 k, any backup pr
1dbe0 6f 63 65 73 73 65 73 20 61 72 65 0a 20 20 2a 2a ocesses are. **
1dbf0 20 75 70 64 61 74 65 64 20 61 73 20 64 61 74 61 updated as data
1dc00 20 69 73 20 63 6f 70 69 65 64 20 6f 75 74 20 6f is copied out o
1dc10 66 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a f the rollback j
1dc20 6f 75 72 6e 61 6c 20 61 6e 64 20 69 6e 74 6f 20 ournal and into
1dc30 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 the. ** databas
1dc40 65 2e 20 54 68 69 73 20 69 73 20 6e 6f 74 20 67 e. This is not g
1dc50 65 6e 65 72 61 6c 6c 79 20 70 6f 73 73 69 62 6c enerally possibl
1dc60 65 20 77 69 74 68 20 61 20 57 41 4c 20 64 61 74 e with a WAL dat
1dc70 61 62 61 73 65 2c 20 61 73 0a 20 20 2a 2a 20 72 abase, as. ** r
1dc80 6f 6c 6c 62 61 63 6b 20 69 6e 76 6f 6c 76 65 73 ollback involves
1dc90 20 73 69 6d 70 6c 79 20 74 72 75 6e 63 61 74 69 simply truncati
1dca0 6e 67 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 2e ng the log file.
1dcb0 20 54 68 65 72 65 66 6f 72 65 2c 20 69 66 20 6f Therefore, if o
1dcc0 6e 65 0a 20 20 2a 2a 20 6f 72 20 6d 6f 72 65 20 ne. ** or more
1dcd0 66 72 61 6d 65 73 20 68 61 76 65 20 61 6c 72 65 frames have alre
1dce0 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e ady been written
1dcf0 20 74 6f 20 74 68 65 20 6c 6f 67 20 28 61 6e 64 to the log (and
1dd00 20 74 68 65 72 65 66 6f 72 65 20 0a 20 20 2a 2a therefore . **
1dd10 20 61 6c 73 6f 20 63 6f 70 69 65 64 20 69 6e 74 also copied int
1dd20 6f 20 74 68 65 20 62 61 63 6b 75 70 20 64 61 74 o the backup dat
1dd30 61 62 61 73 65 73 29 20 61 73 20 70 61 72 74 20 abases) as part
1dd40 6f 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 of this transact
1dd50 69 6f 6e 2c 0a 20 20 2a 2a 20 74 68 65 20 62 61 ion,. ** the ba
1dd60 63 6b 75 70 73 20 6d 75 73 74 20 62 65 20 72 65 ckups must be re
1dd70 73 74 61 72 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 started.. */.
1dd80 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 sqlite3BackupRes
1dd90 74 61 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61 tart(pPager->pBa
1dda0 63 6b 75 70 29 3b 0a 0a 20 20 72 65 74 75 72 6e ckup);.. return
1ddb0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
1ddc0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
1ddd0 61 6c 6c 65 64 20 74 6f 20 72 6f 6c 6c 62 61 63 alled to rollbac
1dde0 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 k a transaction
1ddf0 6f 6e 20 61 20 57 41 4c 20 64 61 74 61 62 61 73 on a WAL databas
1de00 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1de10 20 70 61 67 65 72 52 6f 6c 6c 62 61 63 6b 57 61 pagerRollbackWa
1de20 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 l(Pager *pPager)
1de30 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
1de40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1de50 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 43 6f /* Return Co
1de60 64 65 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 de */. PgHdr *p
1de70 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 List;
1de80 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 /* List
1de90 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 20 74 of dirty pages t
1dea0 6f 20 72 65 76 65 72 74 20 2a 2f 0a 0a 20 20 2f o revert */.. /
1deb0 2a 20 46 6f 72 20 61 6c 6c 20 70 61 67 65 73 20 * For all pages
1dec0 69 6e 20 74 68 65 20 63 61 63 68 65 20 74 68 61 in the cache tha
1ded0 74 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 t are currently
1dee0 64 69 72 74 79 20 6f 72 20 68 61 76 65 20 61 6c dirty or have al
1def0 72 65 61 64 79 0a 20 20 2a 2a 20 62 65 65 6e 20 ready. ** been
1df00 77 72 69 74 74 65 6e 20 28 62 75 74 20 6e 6f 74 written (but not
1df10 20 63 6f 6d 6d 69 74 74 65 64 29 20 74 6f 20 74 committed) to t
1df20 68 65 20 6c 6f 67 20 66 69 6c 65 2c 20 64 6f 20 he log file, do
1df30 6f 6e 65 20 6f 66 20 74 68 65 20 0a 20 20 2a 2a one of the . **
1df40 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 20 2a 2a following:. **
1df50 0a 20 20 2a 2a 20 20 20 2b 20 44 69 73 63 61 72 . ** + Discar
1df60 64 20 74 68 65 20 63 61 63 68 65 64 20 70 61 67 d the cached pag
1df70 65 20 28 69 66 20 72 65 66 63 6f 75 6e 74 3d 3d e (if refcount==
1df80 30 29 2c 20 6f 72 0a 20 20 2a 2a 20 20 20 2b 20 0), or. ** +
1df90 52 65 6c 6f 61 64 20 70 61 67 65 20 63 6f 6e 74 Reload page cont
1dfa0 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 64 61 74 ent from the dat
1dfb0 61 62 61 73 65 20 28 69 66 20 72 65 66 63 6f 75 abase (if refcou
1dfc0 6e 74 3e 30 29 2e 0a 20 20 2a 2f 0a 20 20 70 50 nt>0).. */. pP
1dfd0 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 ager->dbSize = p
1dfe0 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a Pager->dbOrigSiz
1dff0 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 e;. rc = sqlite
1e000 33 57 61 6c 55 6e 64 6f 28 70 50 61 67 65 72 2d 3WalUndo(pPager-
1e010 3e 70 57 61 6c 2c 20 70 61 67 65 72 55 6e 64 6f >pWal, pagerUndo
1e020 43 61 6c 6c 62 61 63 6b 2c 20 28 76 6f 69 64 20 Callback, (void
1e030 2a 29 70 50 61 67 65 72 29 3b 0a 20 20 70 4c 69 *)pPager);. pLi
1e040 73 74 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 st = sqlite3Pcac
1e050 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 61 67 heDirtyList(pPag
1e060 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 er->pPCache);.
1e070 77 68 69 6c 65 28 20 70 4c 69 73 74 20 26 26 20 while( pList &&
1e080 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1e090 0a 20 20 20 20 50 67 48 64 72 20 2a 70 4e 65 78 . PgHdr *pNex
1e0a0 74 20 3d 20 70 4c 69 73 74 2d 3e 70 44 69 72 74 t = pList->pDirt
1e0b0 79 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 y;. rc = page
1e0c0 72 55 6e 64 6f 43 61 6c 6c 62 61 63 6b 28 28 76 rUndoCallback((v
1e0d0 6f 69 64 20 2a 29 70 50 61 67 65 72 2c 20 70 4c oid *)pPager, pL
1e0e0 69 73 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 ist->pgno);.
1e0f0 70 4c 69 73 74 20 3d 20 70 4e 65 78 74 3b 0a 20 pList = pNext;.
1e100 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }.. return rc;
1e110 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
1e120 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 72 61 unction is a wra
1e130 70 70 65 72 20 61 72 6f 75 6e 64 20 73 71 6c 69 pper around sqli
1e140 74 65 33 57 61 6c 46 72 61 6d 65 73 28 29 2e 20 te3WalFrames().
1e150 41 73 20 77 65 6c 6c 20 61 73 20 6c 6f 67 67 69 As well as loggi
1e160 6e 67 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e ng.** the conten
1e170 74 73 20 6f 66 20 74 68 65 20 6c 69 73 74 20 6f ts of the list o
1e180 66 20 70 61 67 65 73 20 68 65 61 64 65 64 20 62 f pages headed b
1e190 79 20 70 4c 69 73 74 20 28 63 6f 6e 6e 65 63 74 y pList (connect
1e1a0 65 64 20 62 79 20 70 44 69 72 74 79 29 2c 0a 2a ed by pDirty),.*
1e1b0 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * this function
1e1c0 6e 6f 74 69 66 69 65 73 20 61 6e 79 20 61 63 74 notifies any act
1e1d0 69 76 65 20 62 61 63 6b 75 70 20 70 72 6f 63 65 ive backup proce
1e1e0 73 73 65 73 20 74 68 61 74 20 74 68 65 20 70 61 sses that the pa
1e1f0 67 65 73 20 68 61 76 65 0a 2a 2a 20 63 68 61 6e ges have.** chan
1e200 67 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ged. .**.** The
1e210 6c 69 73 74 20 6f 66 20 70 61 67 65 73 20 70 61 list of pages pa
1e220 73 73 65 64 20 69 6e 74 6f 20 74 68 69 73 20 72 ssed into this r
1e230 6f 75 74 69 6e 65 20 69 73 20 61 6c 77 61 79 73 outine is always
1e240 20 73 6f 72 74 65 64 20 62 79 20 70 61 67 65 20 sorted by page
1e250 6e 75 6d 62 65 72 2e 0a 2a 2a 20 48 65 6e 63 65 number..** Hence
1e260 2c 20 69 66 20 70 61 67 65 20 31 20 61 70 70 65 , if page 1 appe
1e270 61 72 73 20 61 6e 79 77 68 65 72 65 20 6f 6e 20 ars anywhere on
1e280 74 68 65 20 6c 69 73 74 2c 20 69 74 20 77 69 6c the list, it wil
1e290 6c 20 62 65 20 74 68 65 20 66 69 72 73 74 20 70 l be the first p
1e2a0 61 67 65 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 age..*/ .static
1e2b0 69 6e 74 20 70 61 67 65 72 57 61 6c 46 72 61 6d int pagerWalFram
1e2c0 65 73 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 es(. Pager *pPa
1e2d0 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 ger,
1e2e0 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 6f /* Pager o
1e2f0 62 6a 65 63 74 20 2a 2f 0a 20 20 50 67 48 64 72 bject */. PgHdr
1e300 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20 20 *pList,
1e310 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 /* Li
1e320 73 74 20 6f 66 20 66 72 61 6d 65 73 20 74 6f 20 st of frames to
1e330 6c 6f 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 54 log */. Pgno nT
1e340 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 runcate,
1e350 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
1e360 62 61 73 65 20 73 69 7a 65 20 61 66 74 65 72 20 base size after
1e370 74 68 69 73 20 63 6f 6d 6d 69 74 20 2a 2f 0a 20 this commit */.
1e380 20 69 6e 74 20 69 73 43 6f 6d 6d 69 74 20 20 20 int isCommit
1e390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e3a0 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 /* True if this
1e3b0 20 69 73 20 61 20 63 6f 6d 6d 69 74 20 2a 2f 0a is a commit */.
1e3c0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
1e3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e3e0 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
1e3f0 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 69 ode */. int nLi
1e400 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 st;
1e410 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1e420 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 70 er of pages in p
1e430 4c 69 73 74 20 2a 2f 0a 23 69 66 20 64 65 66 69 List */.#if defi
1e440 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
1e450 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c ) || defined(SQL
1e460 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 ITE_CHECK_PAGES)
1e470 0a 20 20 50 67 48 64 72 20 2a 70 3b 20 20 20 20 . PgHdr *p;
1e480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e490 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e /* For loopin
1e4a0 67 20 6f 76 65 72 20 70 61 67 65 73 20 2a 2f 0a g over pages */.
1e4b0 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 #endif.. assert
1e4c0 28 20 70 50 61 67 65 72 2d 3e 70 57 61 6c 20 29 ( pPager->pWal )
1e4d0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 ;. assert( pLis
1e4e0 74 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 t );.#ifdef SQLI
1e4f0 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 56 65 TE_DEBUG. /* Ve
1e500 72 69 66 79 20 74 68 61 74 20 74 68 65 20 70 61 rify that the pa
1e510 67 65 20 6c 69 73 74 20 69 73 20 69 6e 20 61 63 ge list is in ac
1e520 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f cending order */
1e530 0a 20 20 66 6f 72 28 70 3d 70 4c 69 73 74 3b 20 . for(p=pList;
1e540 70 20 26 26 20 70 2d 3e 70 44 69 72 74 79 3b 20 p && p->pDirty;
1e550 70 3d 70 2d 3e 70 44 69 72 74 79 29 7b 0a 20 20 p=p->pDirty){.
1e560 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 67 6e assert( p->pgn
1e570 6f 20 3c 20 70 2d 3e 70 44 69 72 74 79 2d 3e 70 o < p->pDirty->p
1e580 67 6e 6f 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 gno );. }.#endi
1e590 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 f.. assert( pLi
1e5a0 73 74 2d 3e 70 44 69 72 74 79 3d 3d 30 20 7c 7c st->pDirty==0 ||
1e5b0 20 69 73 43 6f 6d 6d 69 74 20 29 3b 0a 20 20 69 isCommit );. i
1e5c0 66 28 20 69 73 43 6f 6d 6d 69 74 20 29 7b 0a 20 f( isCommit ){.
1e5d0 20 20 20 2f 2a 20 49 66 20 61 20 57 41 4c 20 74 /* If a WAL t
1e5e0 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 62 65 ransaction is be
1e5f0 69 6e 67 20 63 6f 6d 6d 69 74 74 65 64 2c 20 74 ing committed, t
1e600 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 69 6e 74 here is no point
1e610 20 69 6e 20 77 72 69 74 69 6e 67 0a 20 20 20 20 in writing.
1e620 2a 2a 20 61 6e 79 20 70 61 67 65 73 20 77 69 74 ** any pages wit
1e630 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 67 h page numbers g
1e640 72 65 61 74 65 72 20 74 68 61 6e 20 6e 54 72 75 reater than nTru
1e650 6e 63 61 74 65 20 69 6e 74 6f 20 74 68 65 20 57 ncate into the W
1e660 41 4c 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 20 AL file.. **
1e670 54 68 65 79 20 77 69 6c 6c 20 6e 65 76 65 72 20 They will never
1e680 62 65 20 72 65 61 64 20 62 79 20 61 6e 79 20 63 be read by any c
1e690 6c 69 65 6e 74 2e 20 53 6f 20 72 65 6d 6f 76 65 lient. So remove
1e6a0 20 74 68 65 6d 20 66 72 6f 6d 20 74 68 65 20 70 them from the p
1e6b0 44 69 72 74 79 0a 20 20 20 20 2a 2a 20 6c 69 73 Dirty. ** lis
1e6c0 74 20 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20 50 t here. */. P
1e6d0 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 50 67 48 gHdr *p;. PgH
1e6e0 64 72 20 2a 2a 70 70 4e 65 78 74 20 3d 20 26 70 dr **ppNext = &p
1e6f0 4c 69 73 74 3b 0a 20 20 20 20 6e 4c 69 73 74 20 List;. nList
1e700 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70 3d 70 = 0;. for(p=p
1e710 4c 69 73 74 3b 20 28 2a 70 70 4e 65 78 74 20 3d List; (*ppNext =
1e720 20 70 29 21 3d 30 3b 20 70 3d 70 2d 3e 70 44 69 p)!=0; p=p->pDi
1e730 72 74 79 29 7b 0a 20 20 20 20 20 20 69 66 28 20 rty){. if(
1e740 70 2d 3e 70 67 6e 6f 3c 3d 6e 54 72 75 6e 63 61 p->pgno<=nTrunca
1e750 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 70 te ){. pp
1e760 4e 65 78 74 20 3d 20 26 70 2d 3e 70 44 69 72 74 Next = &p->pDirt
1e770 79 3b 0a 20 20 20 20 20 20 20 20 6e 4c 69 73 74 y;. nList
1e780 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ++;. }.
1e790 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c }. assert( pL
1e7a0 69 73 74 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a ist );. }else{.
1e7b0 20 20 20 20 6e 4c 69 73 74 20 3d 20 31 3b 0a 20 nList = 1;.
1e7c0 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 61 53 74 }. pPager->aSt
1e7d0 61 74 5b 50 41 47 45 52 5f 53 54 41 54 5f 57 52 at[PAGER_STAT_WR
1e7e0 49 54 45 5d 20 2b 3d 20 6e 4c 69 73 74 3b 0a 0a ITE] += nList;..
1e7f0 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 70 67 6e if( pList->pgn
1e800 6f 3d 3d 31 20 29 20 70 61 67 65 72 5f 77 72 69 o==1 ) pager_wri
1e810 74 65 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 te_changecounter
1e820 28 70 4c 69 73 74 29 3b 0a 20 20 72 63 20 3d 20 (pList);. rc =
1e830 73 71 6c 69 74 65 33 57 61 6c 46 72 61 6d 65 73 sqlite3WalFrames
1e840 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 0a (pPager->pWal, .
1e850 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 61 pPager->pa
1e860 67 65 53 69 7a 65 2c 20 70 4c 69 73 74 2c 20 6e geSize, pList, n
1e870 54 72 75 6e 63 61 74 65 2c 20 69 73 43 6f 6d 6d Truncate, isComm
1e880 69 74 2c 20 70 50 61 67 65 72 2d 3e 77 61 6c 53 it, pPager->walS
1e890 79 6e 63 46 6c 61 67 73 0a 20 20 29 3b 0a 20 20 yncFlags. );.
1e8a0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1e8b0 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 70 42 61 K && pPager->pBa
1e8c0 63 6b 75 70 20 29 7b 0a 20 20 20 20 50 67 48 64 ckup ){. PgHd
1e8d0 72 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70 3d r *p;. for(p=
1e8e0 70 4c 69 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 pList; p; p=p->p
1e8f0 44 69 72 74 79 29 7b 0a 20 20 20 20 20 20 73 71 Dirty){. sq
1e900 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 lite3BackupUpdat
1e910 65 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 e(pPager->pBacku
1e920 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 28 75 38 20 p, p->pgno, (u8
1e930 2a 29 70 2d 3e 70 44 61 74 61 29 3b 0a 20 20 20 *)p->pData);.
1e940 20 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 }. }..#ifdef S
1e950 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 QLITE_CHECK_PAGE
1e960 53 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 S. pList = sqli
1e970 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 te3PcacheDirtyLi
1e980 73 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 st(pPager->pPCac
1e990 68 65 29 3b 0a 20 20 66 6f 72 28 70 3d 70 4c 69 he);. for(p=pLi
1e9a0 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 st; p; p=p->pDir
1e9b0 74 79 29 7b 0a 20 20 20 20 70 61 67 65 72 5f 73 ty){. pager_s
1e9c0 65 74 5f 70 61 67 65 68 61 73 68 28 70 29 3b 0a et_pagehash(p);.
1e9d0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 }.#endif.. re
1e9e0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1e9f0 2a 20 42 65 67 69 6e 20 61 20 72 65 61 64 20 74 * Begin a read t
1ea00 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 ransaction on th
1ea10 65 20 57 41 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 e WAL..**.** Thi
1ea20 73 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 74 s routine used t
1ea30 6f 20 62 65 20 63 61 6c 6c 65 64 20 22 70 61 67 o be called "pag
1ea40 65 72 4f 70 65 6e 53 6e 61 70 73 68 6f 74 28 29 erOpenSnapshot()
1ea50 22 20 62 65 63 61 75 73 65 20 69 74 20 65 73 73 " because it ess
1ea60 65 6e 74 69 61 6c 6c 79 0a 2a 2a 20 6d 61 6b 65 entially.** make
1ea70 73 20 61 20 73 6e 61 70 73 68 6f 74 20 6f 66 20 s a snapshot of
1ea80 74 68 65 20 64 61 74 61 62 61 73 65 20 61 74 20 the database at
1ea90 74 68 65 20 63 75 72 72 65 6e 74 20 70 6f 69 6e the current poin
1eaa0 74 20 69 6e 20 74 69 6d 65 20 61 6e 64 20 70 72 t in time and pr
1eab0 65 73 65 72 76 65 73 0a 2a 2a 20 74 68 61 74 20 eserves.** that
1eac0 73 6e 61 70 73 68 6f 74 20 66 6f 72 20 75 73 65 snapshot for use
1ead0 20 62 79 20 74 68 65 20 72 65 61 64 65 72 20 69 by the reader i
1eae0 6e 20 73 70 69 74 65 20 6f 66 20 63 6f 6e 63 75 n spite of concu
1eaf0 72 72 65 6e 74 6c 79 20 63 68 61 6e 67 65 73 20 rrently changes
1eb00 62 79 0a 2a 2a 20 6f 74 68 65 72 20 77 72 69 74 by.** other writ
1eb10 65 72 73 20 6f 72 20 63 68 65 63 6b 70 6f 69 6e ers or checkpoin
1eb20 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ters..*/.static
1eb30 69 6e 74 20 70 61 67 65 72 42 65 67 69 6e 52 65 int pagerBeginRe
1eb40 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 adTransaction(Pa
1eb50 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
1eb60 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
1eb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1eb80 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
1eb90 2f 0a 20 20 69 6e 74 20 63 68 61 6e 67 65 64 20 /. int changed
1eba0 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
1ebb0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 /* True if c
1ebc0 61 63 68 65 20 6d 75 73 74 20 62 65 20 72 65 73 ache must be res
1ebd0 65 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 et */.. assert(
1ebe0 20 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 pagerUseWal(pPa
1ebf0 67 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 ger) );. assert
1ec00 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 ( pPager->eState
1ec10 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 20 7c 7c 20 ==PAGER_OPEN ||
1ec20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d pPager->eState==
1ec30 50 41 47 45 52 5f 52 45 41 44 45 52 20 29 3b 0a PAGER_READER );.
1ec40 0a 20 20 2f 2a 20 73 71 6c 69 74 65 33 57 61 6c . /* sqlite3Wal
1ec50 45 6e 64 52 65 61 64 54 72 61 6e 73 61 63 74 69 EndReadTransacti
1ec60 6f 6e 28 29 20 77 61 73 20 6e 6f 74 20 63 61 6c on() was not cal
1ec70 6c 65 64 20 66 6f 72 20 74 68 65 20 70 72 65 76 led for the prev
1ec80 69 6f 75 73 0a 20 20 2a 2a 20 74 72 61 6e 73 61 ious. ** transa
1ec90 63 74 69 6f 6e 20 69 6e 20 6c 6f 63 6b 69 6e 67 ction in locking
1eca0 5f 6d 6f 64 65 3d 45 58 43 4c 55 53 49 56 45 2e _mode=EXCLUSIVE.
1ecb0 20 20 53 6f 20 63 61 6c 6c 20 69 74 20 6e 6f 77 So call it now
1ecc0 2e 20 20 49 66 20 77 65 0a 20 20 2a 2a 20 61 72 . If we. ** ar
1ecd0 65 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 e in locking_mod
1ece0 65 3d 4e 4f 52 4d 41 4c 20 61 6e 64 20 45 6e 64 e=NORMAL and End
1ecf0 52 65 61 64 28 29 20 77 61 73 20 70 72 65 76 69 Read() was previ
1ed00 6f 75 73 6c 79 20 63 61 6c 6c 65 64 2c 0a 20 20 ously called,.
1ed10 2a 2a 20 74 68 65 20 64 75 70 6c 69 63 61 74 65 ** the duplicate
1ed20 20 63 61 6c 6c 20 69 73 20 68 61 72 6d 6c 65 73 call is harmles
1ed30 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 s.. */. sqlite
1ed40 33 57 61 6c 45 6e 64 52 65 61 64 54 72 61 6e 73 3WalEndReadTrans
1ed50 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2d 3e 70 action(pPager->p
1ed60 57 61 6c 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 Wal);.. rc = sq
1ed70 6c 69 74 65 33 57 61 6c 42 65 67 69 6e 52 65 61 lite3WalBeginRea
1ed80 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 dTransaction(pPa
1ed90 67 65 72 2d 3e 70 57 61 6c 2c 20 26 63 68 61 6e ger->pWal, &chan
1eda0 67 65 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d ged);. if( rc!=
1edb0 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 63 68 61 SQLITE_OK || cha
1edc0 6e 67 65 64 20 29 7b 0a 20 20 20 20 70 61 67 65 nged ){. page
1edd0 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b r_reset(pPager);
1ede0 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 . }.. return r
1edf0 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a c;.}.#endif../*.
1ee00 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
1ee10 20 69 73 20 63 61 6c 6c 65 64 20 61 73 20 70 61 is called as pa
1ee20 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 69 rt of the transi
1ee30 74 69 6f 6e 20 66 72 6f 6d 20 50 41 47 45 52 5f tion from PAGER_
1ee40 4f 50 45 4e 0a 2a 2a 20 74 6f 20 50 41 47 45 52 OPEN.** to PAGER
1ee50 5f 52 45 41 44 45 52 20 73 74 61 74 65 20 74 6f _READER state to
1ee60 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 73 determine the s
1ee70 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ize of the datab
1ee80 61 73 65 20 66 69 6c 65 0a 2a 2a 20 69 6e 20 70 ase file.** in p
1ee90 61 67 65 73 20 28 61 73 73 75 6d 69 6e 67 20 74 ages (assuming t
1eea0 68 65 20 70 61 67 65 20 73 69 7a 65 20 63 75 72 he page size cur
1eeb0 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e rently stored in
1eec0 20 50 61 67 65 72 2e 70 61 67 65 53 69 7a 65 29 Pager.pageSize)
1eed0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 72 ..**.** If no er
1eee0 72 6f 72 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 ror occurs, SQLI
1eef0 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
1ef00 64 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20 6f d and the size o
1ef10 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a f the database.*
1ef20 2a 20 69 6e 20 70 61 67 65 73 20 69 73 20 73 74 * in pages is st
1ef30 6f 72 65 64 20 69 6e 20 2a 70 6e 50 61 67 65 2e ored in *pnPage.
1ef40 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 65 Otherwise, an e
1ef50 72 72 6f 72 20 63 6f 64 65 20 28 70 65 72 68 61 rror code (perha
1ef60 70 73 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 ps.** SQLITE_IOE
1ef70 52 52 5f 46 53 54 41 54 29 20 69 73 20 72 65 74 RR_FSTAT) is ret
1ef80 75 72 6e 65 64 20 61 6e 64 20 2a 70 6e 50 61 67 urned and *pnPag
1ef90 65 20 69 73 20 6c 65 66 74 20 75 6e 6d 6f 64 69 e is left unmodi
1efa0 66 69 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 fied..*/.static
1efb0 69 6e 74 20 70 61 67 65 72 50 61 67 65 63 6f 75 int pagerPagecou
1efc0 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nt(Pager *pPager
1efd0 2c 20 50 67 6e 6f 20 2a 70 6e 50 61 67 65 29 7b , Pgno *pnPage){
1efe0 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20 20 . Pgno nPage;
1eff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f000 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 /* Value to r
1f010 65 74 75 72 6e 20 76 69 61 20 2a 70 6e 50 61 67 eturn via *pnPag
1f020 65 20 2a 2f 0a 0a 20 20 2f 2a 20 51 75 65 72 79 e */.. /* Query
1f030 20 74 68 65 20 57 41 4c 20 73 75 62 2d 73 79 73 the WAL sub-sys
1f040 74 65 6d 20 66 6f 72 20 74 68 65 20 64 61 74 61 tem for the data
1f050 62 61 73 65 20 73 69 7a 65 2e 20 54 68 65 20 57 base size. The W
1f060 61 6c 44 62 73 69 7a 65 28 29 0a 20 20 2a 2a 20 alDbsize(). **
1f070 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
1f080 20 7a 65 72 6f 20 69 66 20 74 68 65 20 57 41 4c zero if the WAL
1f090 20 69 73 20 6e 6f 74 20 6f 70 65 6e 20 28 69 2e is not open (i.
1f0a0 65 2e 20 50 61 67 65 72 2e 70 57 61 6c 3d 3d 30 e. Pager.pWal==0
1f0b0 29 2c 20 6f 72 0a 20 20 2a 2a 20 69 66 20 74 68 ), or. ** if th
1f0c0 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 e database size
1f0d0 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 is not available
1f0e0 2e 20 54 68 65 20 64 61 74 61 62 61 73 65 20 73 . The database s
1f0f0 69 7a 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 ize is not. **
1f100 61 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20 74 available from t
1f110 68 65 20 57 41 4c 20 73 75 62 2d 73 79 73 74 65 he WAL sub-syste
1f120 6d 20 69 66 20 74 68 65 20 6c 6f 67 20 66 69 6c m if the log fil
1f130 65 20 69 73 20 65 6d 70 74 79 20 6f 72 0a 20 20 e is empty or.
1f140 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 76 ** contains no v
1f150 61 6c 69 64 20 63 6f 6d 6d 69 74 74 65 64 20 74 alid committed t
1f160 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 20 20 2a ransactions.. *
1f170 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 /. assert( pPag
1f180 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 er->eState==PAGE
1f190 52 5f 4f 50 45 4e 20 29 3b 0a 20 20 61 73 73 65 R_OPEN );. asse
1f1a0 72 74 28 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 rt( pPager->eLoc
1f1b0 6b 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 k>=SHARED_LOCK )
1f1c0 3b 0a 20 20 6e 50 61 67 65 20 3d 20 73 71 6c 69 ;. nPage = sqli
1f1d0 74 65 33 57 61 6c 44 62 73 69 7a 65 28 70 50 61 te3WalDbsize(pPa
1f1e0 67 65 72 2d 3e 70 57 61 6c 29 3b 0a 0a 20 20 2f ger->pWal);.. /
1f1f0 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
1f200 65 20 73 69 7a 65 20 77 61 73 20 6e 6f 74 20 61 e size was not a
1f210 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20 74 68 vailable from th
1f220 65 20 57 41 4c 20 73 75 62 2d 73 79 73 74 65 6d e WAL sub-system
1f230 2c 0a 20 20 2a 2a 20 64 65 74 65 72 6d 69 6e 65 ,. ** determine
1f240 20 69 74 20 62 61 73 65 64 20 6f 6e 20 74 68 65 it based on the
1f250 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 size of the dat
1f260 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 abase file. If t
1f270 68 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 he size. ** of
1f280 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1f290 65 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 e is not an inte
1f2a0 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 ger multiple of
1f2b0 74 68 65 20 70 61 67 65 2d 73 69 7a 65 2c 0a 20 the page-size,.
1f2c0 20 2a 2a 20 72 6f 75 6e 64 20 64 6f 77 6e 20 74 ** round down t
1f2d0 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 70 61 o the nearest pa
1f2e0 67 65 2e 20 45 78 63 65 70 74 2c 20 61 6e 79 20 ge. Except, any
1f2f0 66 69 6c 65 20 6c 61 72 67 65 72 20 74 68 61 6e file larger than
1f300 20 30 0a 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 0. ** bytes in
1f310 20 73 69 7a 65 20 69 73 20 63 6f 6e 73 69 64 65 size is conside
1f320 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 red to contain a
1f330 74 20 6c 65 61 73 74 20 6f 6e 65 20 70 61 67 65 t least one page
1f340 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 50 61 .. */. if( nPa
1f350 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 36 34 ge==0 ){. i64
1f360 20 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 n = 0;
1f370 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
1f380 7a 65 20 6f 66 20 64 62 20 66 69 6c 65 20 69 6e ze of db file in
1f390 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 61 73 bytes */. as
1f3a0 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 sert( isOpen(pPa
1f3b0 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 ger->fd) || pPag
1f3c0 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a er->tempFile );.
1f3d0 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 if( isOpen(p
1f3e0 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 Pager->fd) ){.
1f3f0 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c int rc = sql
1f400 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 ite3OsFileSize(p
1f410 50 61 67 65 72 2d 3e 66 64 2c 20 26 6e 29 3b 0a Pager->fd, &n);.
1f420 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1f430 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1f440 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1f450 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1f460 6e 50 61 67 65 20 3d 20 28 50 67 6e 6f 29 28 28 nPage = (Pgno)((
1f470 6e 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 n+pPager->pageSi
1f480 7a 65 2d 31 29 20 2f 20 70 50 61 67 65 72 2d 3e ze-1) / pPager->
1f490 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 7d 0a 0a pageSize);. }..
1f4a0 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 72 /* If the curr
1f4b0 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 ent number of pa
1f4c0 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 20 ges in the file
1f4d0 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
1f4e0 74 68 65 0a 20 20 2a 2a 20 63 6f 6e 66 69 67 75 the. ** configu
1f4f0 72 65 64 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 red maximum page
1f500 72 20 6e 75 6d 62 65 72 2c 20 69 6e 63 72 65 61 r number, increa
1f510 73 65 20 74 68 65 20 61 6c 6c 6f 77 65 64 20 6c se the allowed l
1f520 69 6d 69 74 20 73 6f 0a 20 20 2a 2a 20 74 68 61 imit so. ** tha
1f530 74 20 74 68 65 20 66 69 6c 65 20 63 61 6e 20 62 t the file can b
1f540 65 20 72 65 61 64 2e 0a 20 20 2a 2f 0a 20 20 69 e read.. */. i
1f550 66 28 20 6e 50 61 67 65 3e 70 50 61 67 65 72 2d f( nPage>pPager-
1f560 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20 20 20 70 >mxPgno ){. p
1f570 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 Pager->mxPgno =
1f580 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a 20 20 7d (Pgno)nPage;. }
1f590 0a 0a 20 20 2a 70 6e 50 61 67 65 20 3d 20 6e 50 .. *pnPage = nP
1f5a0 61 67 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 age;. return SQ
1f5b0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e LITE_OK;.}..#ifn
1f5c0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1f5d0 57 41 4c 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 WAL./*.** Check
1f5e0 69 66 20 74 68 65 20 2a 2d 77 61 6c 20 66 69 6c if the *-wal fil
1f5f0 65 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e e that correspon
1f600 64 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ds to the databa
1f610 73 65 20 6f 70 65 6e 65 64 20 62 79 20 70 50 61 se opened by pPa
1f620 67 65 72 0a 2a 2a 20 65 78 69 73 74 73 20 69 66 ger.** exists if
1f630 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
1f640 20 6e 6f 74 20 65 6d 70 79 2c 20 6f 72 20 76 65 not empy, or ve
1f650 72 69 66 79 20 74 68 61 74 20 74 68 65 20 2a 2d rify that the *-
1f660 77 61 6c 20 66 69 6c 65 20 64 6f 65 73 0a 2a 2a wal file does.**
1f670 20 6e 6f 74 20 65 78 69 73 74 20 28 62 79 20 64 not exist (by d
1f680 65 6c 65 74 69 6e 67 20 69 74 29 20 69 66 20 74 eleting it) if t
1f690 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1f6a0 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a is empty..**.**
1f6b0 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 If the database
1f6c0 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 20 61 6e is not empty an
1f6d0 64 20 74 68 65 20 2a 2d 77 61 6c 20 66 69 6c 65 d the *-wal file
1f6e0 20 65 78 69 73 74 73 2c 20 6f 70 65 6e 20 74 68 exists, open th
1f6f0 65 20 70 61 67 65 72 0a 2a 2a 20 69 6e 20 57 41 e pager.** in WA
1f700 4c 20 6d 6f 64 65 2e 20 20 49 66 20 74 68 65 20 L mode. If the
1f710 64 61 74 61 62 61 73 65 20 69 73 20 65 6d 70 74 database is empt
1f720 79 20 6f 72 20 69 66 20 6e 6f 20 2a 2d 77 61 6c y or if no *-wal
1f730 20 66 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 file exists and
1f740 0a 2a 2a 20 69 66 20 6e 6f 20 65 72 72 6f 72 20 .** if no error
1f750 6f 63 63 75 72 73 2c 20 6d 61 6b 65 20 73 75 72 occurs, make sur
1f760 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4d e Pager.journalM
1f770 6f 64 65 20 69 73 20 6e 6f 74 20 73 65 74 20 74 ode is not set t
1f780 6f 0a 2a 2a 20 50 41 47 45 52 5f 4a 4f 55 52 4e o.** PAGER_JOURN
1f790 41 4c 4d 4f 44 45 5f 57 41 4c 2e 0a 2a 2a 0a 2a ALMODE_WAL..**.*
1f7a0 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f * Return SQLITE_
1f7b0 4f 4b 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 OK or an error c
1f7c0 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ode..**.** The c
1f7d0 61 6c 6c 65 72 20 6d 75 73 74 20 68 6f 6c 64 20 aller must hold
1f7e0 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 6f 6e a SHARED lock on
1f7f0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1f800 6c 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 0a le to call this.
1f810 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 42 65 63 ** function. Bec
1f820 61 75 73 65 20 61 6e 20 45 58 43 4c 55 53 49 56 ause an EXCLUSIV
1f830 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 62 E lock on the db
1f840 20 66 69 6c 65 20 69 73 20 72 65 71 75 69 72 65 file is require
1f850 64 20 74 6f 20 64 65 6c 65 74 65 20 0a 2a 2a 20 d to delete .**
1f860 61 20 57 41 4c 20 6f 6e 20 61 20 6e 6f 6e 65 2d a WAL on a none-
1f870 65 6d 70 74 79 20 64 61 74 61 62 61 73 65 2c 20 empty database,
1f880 74 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 65 this ensures the
1f890 72 65 20 69 73 20 6e 6f 20 72 61 63 65 20 63 6f re is no race co
1f8a0 6e 64 69 74 69 6f 6e 20 0a 2a 2a 20 62 65 74 77 ndition .** betw
1f8b0 65 65 6e 20 74 68 65 20 78 41 63 63 65 73 73 28 een the xAccess(
1f8c0 29 20 62 65 6c 6f 77 20 61 6e 64 20 61 6e 20 78 ) below and an x
1f8d0 44 65 6c 65 74 65 28 29 20 62 65 69 6e 67 20 65 Delete() being e
1f8e0 78 65 63 75 74 65 64 20 62 79 20 73 6f 6d 65 20 xecuted by some
1f8f0 0a 2a 2a 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 .** other connec
1f900 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
1f910 69 6e 74 20 70 61 67 65 72 4f 70 65 6e 57 61 6c int pagerOpenWal
1f920 49 66 50 72 65 73 65 6e 74 28 50 61 67 65 72 20 IfPresent(Pager
1f930 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 *pPager){. int
1f940 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1f950 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
1f960 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f ->eState==PAGER_
1f970 4f 50 45 4e 20 29 3b 0a 20 20 61 73 73 65 72 74 OPEN );. assert
1f980 28 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3e ( pPager->eLock>
1f990 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
1f9a0 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e . if( !pPager->
1f9b0 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 tempFile ){.
1f9c0 69 6e 74 20 69 73 57 61 6c 3b 20 20 20 20 20 20 int isWal;
1f9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1f9e0 20 54 72 75 65 20 69 66 20 57 41 4c 20 66 69 6c True if WAL fil
1f9f0 65 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 e exists */.
1fa00 50 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 Pgno nPage;
1fa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1fa20 20 53 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 Size of the dat
1fa30 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 abase file */..
1fa40 20 20 20 72 63 20 3d 20 70 61 67 65 72 50 61 67 rc = pagerPag
1fa50 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 26 ecount(pPager, &
1fa60 6e 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 nPage);. if(
1fa70 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
1fa80 20 20 20 20 69 66 28 20 6e 50 61 67 65 3d 3d 30 if( nPage==0
1fa90 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
1faa0 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 qlite3OsDelete(p
1fab0 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 61 Pager->pVfs, pPa
1fac0 67 65 72 2d 3e 7a 57 61 6c 2c 20 30 29 3b 0a 20 ger->zWal, 0);.
1fad0 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
1fae0 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 ITE_IOERR_DELETE
1faf0 5f 4e 4f 45 4e 54 20 29 20 72 63 20 3d 20 53 51 _NOENT ) rc = SQ
1fb00 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 69 LITE_OK;. i
1fb10 73 57 61 6c 20 3d 20 30 3b 0a 20 20 20 20 7d 65 sWal = 0;. }e
1fb20 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
1fb30 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 sqlite3OsAccess(
1fb40 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 . pPage
1fb50 72 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d r->pVfs, pPager-
1fb60 3e 7a 57 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 >zWal, SQLITE_AC
1fb70 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 69 73 CESS_EXISTS, &is
1fb80 57 61 6c 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 Wal. );.
1fb90 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 }. if( rc==S
1fba0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1fbb0 20 20 69 66 28 20 69 73 57 61 6c 20 29 7b 0a 20 if( isWal ){.
1fbc0 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
1fbd0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 sqlite3PcachePa
1fbe0 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e gecount(pPager->
1fbf0 70 50 43 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 pPCache)==0 );.
1fc00 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1fc10 74 65 33 50 61 67 65 72 4f 70 65 6e 57 61 6c 28 te3PagerOpenWal(
1fc20 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 pPager, 0);.
1fc30 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 }else if( pPag
1fc40 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d er->journalMode=
1fc50 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
1fc60 44 45 5f 57 41 4c 20 29 7b 0a 20 20 20 20 20 20 DE_WAL ){.
1fc70 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
1fc80 6c 4d 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f lMode = PAGER_JO
1fc90 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 URNALMODE_DELETE
1fca0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1fcb0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1fcc0 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
1fcd0 20 50 6c 61 79 62 61 63 6b 20 73 61 76 65 70 6f Playback savepo
1fce0 69 6e 74 20 70 53 61 76 65 70 6f 69 6e 74 2e 20 int pSavepoint.
1fcf0 4f 72 2c 20 69 66 20 70 53 61 76 65 70 6f 69 6e Or, if pSavepoin
1fd00 74 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 6c t==NULL, then pl
1fd10 61 79 62 61 63 6b 0a 2a 2a 20 74 68 65 20 65 6e ayback.** the en
1fd20 74 69 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 tire master jour
1fd30 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20 63 61 nal file. The ca
1fd40 73 65 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d 4e se pSavepoint==N
1fd50 55 4c 4c 20 6f 63 63 75 72 73 20 77 68 65 6e 20 ULL occurs when
1fd60 0a 2a 2a 20 61 20 52 4f 4c 4c 42 41 43 4b 20 54 .** a ROLLBACK T
1fd70 4f 20 63 6f 6d 6d 61 6e 64 20 69 73 20 69 6e 76 O command is inv
1fd80 6f 6b 65 64 20 6f 6e 20 61 20 53 41 56 45 50 4f oked on a SAVEPO
1fd90 49 4e 54 20 74 68 61 74 20 69 73 20 61 20 74 72 INT that is a tr
1fda0 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 73 61 ansaction .** sa
1fdb0 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 vepoint..**.** W
1fdc0 68 65 6e 20 70 53 61 76 65 70 6f 69 6e 74 20 69 hen pSavepoint i
1fdd0 73 20 6e 6f 74 20 4e 55 4c 4c 20 28 6d 65 61 6e s not NULL (mean
1fde0 69 6e 67 20 61 20 6e 6f 6e 2d 74 72 61 6e 73 61 ing a non-transa
1fdf0 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 ction savepoint
1fe00 69 73 20 0a 2a 2a 20 62 65 69 6e 67 20 72 6f 6c is .** being rol
1fe10 6c 65 64 20 62 61 63 6b 29 2c 20 74 68 65 6e 20 led back), then
1fe20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 6f 6e the rollback con
1fe30 73 69 73 74 73 20 6f 66 20 75 70 20 74 6f 20 74 sists of up to t
1fe40 68 72 65 65 20 73 74 61 67 65 73 2c 0a 2a 2a 20 hree stages,.**
1fe50 70 65 72 66 6f 72 6d 65 64 20 69 6e 20 74 68 65 performed in the
1fe60 20 6f 72 64 65 72 20 73 70 65 63 69 66 69 65 64 order specified
1fe70 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 67 65 :.**.** * Page
1fe80 73 20 61 72 65 20 70 6c 61 79 65 64 20 62 61 63 s are played bac
1fe90 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 k from the main
1fea0 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 journal starting
1feb0 20 61 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 20 at byte.**
1fec0 6f 66 66 73 65 74 20 50 61 67 65 72 53 61 76 65 offset PagerSave
1fed0 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e point.iOffset an
1fee0 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 d continuing to
1fef0 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 .** PagerSav
1ff00 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 epoint.iHdrOffse
1ff10 74 2c 20 6f 72 20 74 6f 20 74 68 65 20 65 6e 64 t, or to the end
1ff20 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 of the main jou
1ff30 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 66 69 6c 65 rnal.** file
1ff40 20 69 66 20 50 61 67 65 72 53 61 76 65 70 6f 69 if PagerSavepoi
1ff50 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 69 73 nt.iHdrOffset is
1ff60 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a zero..**.** *
1ff70 20 49 66 20 50 61 67 65 72 53 61 76 65 70 6f 69 If PagerSavepoi
1ff80 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 69 73 nt.iHdrOffset is
1ff90 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 not zero, then
1ffa0 70 61 67 65 73 20 61 72 65 20 70 6c 61 79 65 64 pages are played
1ffb0 0a 2a 2a 20 20 20 20 20 62 61 63 6b 20 73 74 61 .** back sta
1ffc0 72 74 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a rting from the j
1ffd0 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 6d ournal header im
1ffe0 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 mediately follow
1fff0 69 6e 67 20 0a 2a 2a 20 20 20 20 20 50 61 67 65 ing .** Page
20000 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f rSavepoint.iHdrO
20010 66 66 73 65 74 20 74 6f 20 74 68 65 20 65 6e 64 ffset to the end
20020 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 of the main jou
20030 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a rnal file..**.**
20040 20 20 20 2a 20 50 61 67 65 73 20 61 72 65 20 74 * Pages are t
20050 68 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 20 hen played back
20060 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 from the sub-jou
20070 72 6e 61 6c 20 66 69 6c 65 2c 20 73 74 61 72 74 rnal file, start
20080 69 6e 67 0a 2a 2a 20 20 20 20 20 77 69 74 68 20 ing.** with
20090 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 the PagerSavepoi
200a0 6e 74 2e 69 53 75 62 52 65 63 20 61 6e 64 20 63 nt.iSubRec and c
200b0 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 ontinuing to the
200c0 20 65 6e 64 20 6f 66 0a 2a 2a 20 20 20 20 20 74 end of.** t
200d0 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e he journal file.
200e0 0a 2a 2a 0a 2a 2a 20 54 68 72 6f 75 67 68 6f 75 .**.** Throughou
200f0 74 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 70 t the rollback p
20100 72 6f 63 65 73 73 2c 20 65 61 63 68 20 74 69 6d rocess, each tim
20110 65 20 61 20 70 61 67 65 20 69 73 20 72 6f 6c 6c e a page is roll
20120 65 64 20 62 61 63 6b 2c 20 74 68 65 0a 2a 2a 20 ed back, the.**
20130 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 corresponding bi
20140 74 20 69 73 20 73 65 74 20 69 6e 20 61 20 62 69 t is set in a bi
20150 74 76 65 63 20 73 74 72 75 63 74 75 72 65 20 28 tvec structure (
20160 76 61 72 69 61 62 6c 65 20 70 44 6f 6e 65 20 69 variable pDone i
20170 6e 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 n the.** impleme
20180 6e 74 61 74 69 6f 6e 20 62 65 6c 6f 77 29 2e 20 ntation below).
20190 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 This is used to
201a0 65 6e 73 75 72 65 20 74 68 61 74 20 61 20 70 61 ensure that a pa
201b0 67 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 72 6f ge is only.** ro
201c0 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20 66 69 lled back the fi
201d0 72 73 74 20 74 69 6d 65 20 69 74 20 69 73 20 65 rst time it is e
201e0 6e 63 6f 75 6e 74 65 72 65 64 20 69 6e 20 65 69 ncountered in ei
201f0 74 68 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a ther journal..**
20200 0a 2a 2a 20 49 66 20 70 53 61 76 65 70 6f 69 6e .** If pSavepoin
20210 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 t is NULL, then
20220 70 61 67 65 73 20 61 72 65 20 6f 6e 6c 79 20 70 pages are only p
20230 6c 61 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 layed back from
20240 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f 75 72 the main.** jour
20250 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 72 65 20 nal file. There
20260 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 61 is no need for a
20270 20 62 69 74 76 65 63 20 69 6e 20 74 68 69 73 20 bitvec in this
20280 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 case..**.** In e
20290 69 74 68 65 72 20 63 61 73 65 2c 20 62 65 66 6f ither case, befo
202a0 72 65 20 70 6c 61 79 62 61 63 6b 20 63 6f 6d 6d re playback comm
202b0 65 6e 63 65 73 20 74 68 65 20 50 61 67 65 72 2e ences the Pager.
202c0 64 62 53 69 7a 65 20 76 61 72 69 61 62 6c 65 0a dbSize variable.
202d0 2a 2a 20 69 73 20 72 65 73 65 74 20 74 6f 20 74 ** is reset to t
202e0 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 69 74 he value that it
202f0 20 68 65 6c 64 20 61 74 20 74 68 65 20 73 74 61 held at the sta
20300 72 74 20 6f 66 20 74 68 65 20 73 61 76 65 70 6f rt of the savepo
20310 69 6e 74 20 0a 2a 2a 20 28 6f 72 20 74 72 61 6e int .** (or tran
20320 73 61 63 74 69 6f 6e 29 2e 20 4e 6f 20 70 61 67 saction). No pag
20330 65 20 77 69 74 68 20 61 20 70 61 67 65 2d 6e 75 e with a page-nu
20340 6d 62 65 72 20 67 72 65 61 74 65 72 20 74 68 61 mber greater tha
20350 6e 20 74 68 69 73 20 76 61 6c 75 65 0a 2a 2a 20 n this value.**
20360 69 73 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 is played back.
20370 49 66 20 6f 6e 65 20 69 73 20 65 6e 63 6f 75 6e If one is encoun
20380 74 65 72 65 64 20 69 74 20 69 73 20 73 69 6d 70 tered it is simp
20390 6c 79 20 73 6b 69 70 70 65 64 2e 0a 2a 2f 0a 73 ly skipped..*/.s
203a0 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 50 tatic int pagerP
203b0 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 laybackSavepoint
203c0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
203d0 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a PagerSavepoint *
203e0 70 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 pSavepoint){. i
203f0 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 64 szJ;
20400 20 20 20 20 20 20 20 20 2f 2a 20 45 66 66 65 63 /* Effec
20410 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 tive size of the
20420 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f main journal */
20430 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20 . i64 iHdrOff;
20440 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
20450 6e 64 20 6f 66 20 66 69 72 73 74 20 73 65 67 6d nd of first segm
20460 65 6e 74 20 6f 66 20 6d 61 69 6e 2d 6a 6f 75 72 ent of main-jour
20470 6e 61 6c 20 72 65 63 6f 72 64 73 20 2a 2f 0a 20 nal records */.
20480 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
20490 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 52 65 74 _OK; /* Ret
204a0 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 69 urn code */. Bi
204b0 74 76 65 63 20 2a 70 44 6f 6e 65 20 3d 20 30 3b tvec *pDone = 0;
204c0 20 20 20 20 20 20 20 2f 2a 20 42 69 74 76 65 63 /* Bitvec
204d0 20 74 6f 20 65 6e 73 75 72 65 20 70 61 67 65 73 to ensure pages
204e0 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f 6e 6c played back onl
204f0 79 20 6f 6e 63 65 20 2a 2f 0a 0a 20 20 61 73 73 y once */.. ass
20500 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 ert( pPager->eSt
20510 61 74 65 21 3d 50 41 47 45 52 5f 45 52 52 4f 52 ate!=PAGER_ERROR
20520 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
20530 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50 41 ager->eState>=PA
20540 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45 GER_WRITER_LOCKE
20550 44 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 D );.. /* Alloc
20560 61 74 65 20 61 20 62 69 74 76 65 63 20 74 6f 20 ate a bitvec to
20570 75 73 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 use to store the
20580 20 73 65 74 20 6f 66 20 70 61 67 65 73 20 72 6f set of pages ro
20590 6c 6c 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 lled back */. i
205a0 66 28 20 70 53 61 76 65 70 6f 69 6e 74 20 29 7b f( pSavepoint ){
205b0 0a 20 20 20 20 70 44 6f 6e 65 20 3d 20 73 71 6c . pDone = sql
205c0 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 ite3BitvecCreate
205d0 28 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 (pSavepoint->nOr
205e0 69 67 29 3b 0a 20 20 20 20 69 66 28 20 21 70 44 ig);. if( !pD
205f0 6f 6e 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74 one ){. ret
20600 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
20610 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
20620 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 62 61 * Set the databa
20630 73 65 20 73 69 7a 65 20 62 61 63 6b 20 74 6f 20 se size back to
20640 74 68 65 20 76 61 6c 75 65 20 69 74 20 77 61 73 the value it was
20650 20 62 65 66 6f 72 65 20 74 68 65 20 73 61 76 65 before the save
20660 70 6f 69 6e 74 20 0a 20 20 2a 2a 20 62 65 69 6e point . ** bein
20670 67 20 72 65 76 65 72 74 65 64 20 77 61 73 20 6f g reverted was o
20680 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 50 pened.. */. pP
20690 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 ager->dbSize = p
206a0 53 61 76 65 70 6f 69 6e 74 20 3f 20 70 53 61 76 Savepoint ? pSav
206b0 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67 20 3a 20 epoint->nOrig :
206c0 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 pPager->dbOrigSi
206d0 7a 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 63 68 ze;. pPager->ch
206e0 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 angeCountDone =
206f0 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
20700 3b 0a 0a 20 20 69 66 28 20 21 70 53 61 76 65 70 ;.. if( !pSavep
20710 6f 69 6e 74 20 26 26 20 70 61 67 65 72 55 73 65 oint && pagerUse
20720 57 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 Wal(pPager) ){.
20730 20 20 20 72 65 74 75 72 6e 20 70 61 67 65 72 52 return pagerR
20740 6f 6c 6c 62 61 63 6b 57 61 6c 28 70 50 61 67 65 ollbackWal(pPage
20750 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 73 r);. }.. /* Us
20760 65 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 e pPager->journa
20770 6c 4f 66 66 20 61 73 20 74 68 65 20 65 66 66 65 lOff as the effe
20780 63 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74 68 ctive size of th
20790 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a e main rollback.
207a0 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 ** journal. T
207b0 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 20 6d he actual file m
207c0 69 67 68 74 20 62 65 20 6c 61 72 67 65 72 20 74 ight be larger t
207d0 68 61 6e 20 74 68 69 73 20 69 6e 0a 20 20 2a 2a han this in. **
207e0 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f PAGER_JOURNALMO
207f0 44 45 5f 54 52 55 4e 43 41 54 45 20 6f 72 20 50 DE_TRUNCATE or P
20800 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
20810 5f 50 45 52 53 49 53 54 2e 20 20 42 75 74 20 61 _PERSIST. But a
20820 6e 79 74 68 69 6e 67 0a 20 20 2a 2a 20 70 61 73 nything. ** pas
20830 74 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 t pPager->journa
20840 6c 4f 66 66 20 69 73 20 6f 66 66 2d 6c 69 6d 69 lOff is off-limi
20850 74 73 20 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a 20 ts to us.. */.
20860 20 73 7a 4a 20 3d 20 70 50 61 67 65 72 2d 3e 6a szJ = pPager->j
20870 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 61 73 73 ournalOff;. ass
20880 65 72 74 28 20 70 61 67 65 72 55 73 65 57 61 6c ert( pagerUseWal
20890 28 70 50 61 67 65 72 29 3d 3d 30 20 7c 7c 20 73 (pPager)==0 || s
208a0 7a 4a 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 42 zJ==0 );.. /* B
208b0 65 67 69 6e 20 62 79 20 72 6f 6c 6c 69 6e 67 20 egin by rolling
208c0 62 61 63 6b 20 72 65 63 6f 72 64 73 20 66 72 6f back records fro
208d0 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e m the main journ
208e0 61 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a 20 al starting at.
208f0 20 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f 69 ** PagerSavepoi
20900 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 20 63 nt.iOffset and c
20910 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 ontinuing to the
20920 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 next journal he
20930 61 64 65 72 2e 0a 20 20 2a 2a 20 54 68 65 72 65 ader.. ** There
20940 20 6d 69 67 68 74 20 62 65 20 72 65 63 6f 72 64 might be record
20950 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f s in the main jo
20960 75 72 6e 61 6c 20 74 68 61 74 20 68 61 76 65 20 urnal that have
20970 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 a page number.
20980 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 ** greater than
20990 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 the current data
209a0 62 61 73 65 20 73 69 7a 65 20 28 70 50 61 67 65 base size (pPage
209b0 72 2d 3e 64 62 53 69 7a 65 29 20 62 75 74 20 74 r->dbSize) but t
209c0 68 6f 73 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 hose. ** will b
209d0 65 20 73 6b 69 70 70 65 64 20 61 75 74 6f 6d 61 e skipped automa
209e0 74 69 63 61 6c 6c 79 2e 20 20 50 61 67 65 73 20 tically. Pages
209f0 61 72 65 20 61 64 64 65 64 20 74 6f 20 70 44 6f are added to pDo
20a00 6e 65 20 61 73 20 74 68 65 79 0a 20 20 2a 2a 20 ne as they. **
20a10 61 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e are played back.
20a20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 . */. if( pSav
20a30 65 70 6f 69 6e 74 20 26 26 20 21 70 61 67 65 72 epoint && !pager
20a40 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 UseWal(pPager) )
20a50 7b 0a 20 20 20 20 69 48 64 72 4f 66 66 20 3d 20 {. iHdrOff =
20a60 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72 pSavepoint->iHdr
20a70 4f 66 66 73 65 74 20 3f 20 70 53 61 76 65 70 6f Offset ? pSavepo
20a80 69 6e 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 20 int->iHdrOffset
20a90 3a 20 73 7a 4a 3b 0a 20 20 20 20 70 50 61 67 65 : szJ;. pPage
20aa0 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
20ab0 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 4f 66 66 pSavepoint->iOff
20ac0 73 65 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 set;. while(
20ad0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
20ae0 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
20af0 4f 66 66 3c 69 48 64 72 4f 66 66 20 29 7b 0a 20 Off<iHdrOff ){.
20b00 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
20b10 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 playback_one_pag
20b20 65 28 70 50 61 67 65 72 2c 20 26 70 50 61 67 65 e(pPager, &pPage
20b30 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 r->journalOff, p
20b40 44 6f 6e 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20 Done, 1, 1);.
20b50 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 }. assert( r
20b60 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 c!=SQLITE_DONE )
20b70 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
20b80 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
20b90 66 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a f = 0;. }.. /*
20ba0 20 43 6f 6e 74 69 6e 75 65 20 72 6f 6c 6c 69 6e Continue rollin
20bb0 67 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20 6f g back records o
20bc0 75 74 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a ut of the main j
20bd0 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 ournal starting
20be0 61 74 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 at. ** the firs
20bf0 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 t journal header
20c00 20 73 65 65 6e 20 61 6e 64 20 63 6f 6e 74 69 6e seen and contin
20c10 75 69 6e 67 20 75 6e 74 69 6c 20 74 68 65 20 65 uing until the e
20c20 66 66 65 63 74 69 76 65 20 65 6e 64 0a 20 20 2a ffective end. *
20c30 2a 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f * of the main jo
20c40 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 43 6f 6e urnal file. Con
20c50 74 69 6e 75 65 20 74 6f 20 73 6b 69 70 20 6f 75 tinue to skip ou
20c60 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 73 t-of-range pages
20c70 20 61 6e 64 0a 20 20 2a 2a 20 63 6f 6e 74 69 6e and. ** contin
20c80 75 65 20 61 64 64 69 6e 67 20 70 61 67 65 73 20 ue adding pages
20c90 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 6f 20 70 rolled back to p
20ca0 44 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 Done.. */. whi
20cb0 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f le( rc==SQLITE_O
20cc0 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 K && pPager->jou
20cd0 72 6e 61 6c 4f 66 66 3c 73 7a 4a 20 29 7b 0a 20 rnalOff<szJ ){.
20ce0 20 20 20 75 33 32 20 69 69 3b 20 20 20 20 20 20 u32 ii;
20cf0 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
20d00 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 75 33 32 unter */. u32
20d10 20 6e 4a 52 65 63 20 3d 20 30 3b 20 20 20 20 20 nJRec = 0;
20d20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4a 6f 75 /* Number of Jou
20d30 72 6e 61 6c 20 52 65 63 6f 72 64 73 20 2a 2f 0a rnal Records */.
20d40 20 20 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a 20 u32 dummy;.
20d50 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 rc = readJour
20d60 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 30 nalHdr(pPager, 0
20d70 2c 20 73 7a 4a 2c 20 26 6e 4a 52 65 63 2c 20 26 , szJ, &nJRec, &
20d80 64 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 dummy);. asse
20d90 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 rt( rc!=SQLITE_D
20da0 4f 4e 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 0a 20 ONE );.. /*.
20db0 20 20 20 2a 2a 20 54 68 65 20 22 70 50 61 67 65 ** The "pPage
20dc0 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f r->journalHdr+JO
20dd0 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
20de0 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f ger)==pPager->jo
20df0 75 72 6e 61 6c 4f 66 66 22 0a 20 20 20 20 2a 2a urnalOff". **
20e00 20 74 65 73 74 20 69 73 20 72 65 6c 61 74 65 64 test is related
20e10 20 74 6f 20 74 69 63 6b 65 74 20 23 32 35 36 35 to ticket #2565
20e20 2e 20 20 53 65 65 20 74 68 65 20 64 69 73 63 75 . See the discu
20e30 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a 20 20 20 ssion in the.
20e40 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 61 ** pager_playba
20e50 63 6b 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f ck() function fo
20e60 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 r additional inf
20e70 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f ormation.. */
20e80 0a 20 20 20 20 69 66 28 20 6e 4a 52 65 63 3d 3d . if( nJRec==
20e90 30 20 0a 20 20 20 20 20 26 26 20 70 50 61 67 65 0 . && pPage
20ea0 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f r->journalHdr+JO
20eb0 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
20ec0 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f ger)==pPager->jo
20ed0 75 72 6e 61 6c 4f 66 66 0a 20 20 20 20 29 7b 0a urnalOff. ){.
20ee0 20 20 20 20 20 20 6e 4a 52 65 63 20 3d 20 28 75 nJRec = (u
20ef0 33 32 29 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 32)((szJ - pPage
20f00 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 2f 4a r->journalOff)/J
20f10 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 OURNAL_PG_SZ(pPa
20f20 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ger));. }.
20f30 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 for(ii=0; rc==S
20f40 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e QLITE_OK && ii<n
20f50 4a 52 65 63 20 26 26 20 70 50 61 67 65 72 2d 3e JRec && pPager->
20f60 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 3b 20 journalOff<szJ;
20f70 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63 20 ii++){. rc
20f80 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b = pager_playback
20f90 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 _one_page(pPager
20fa0 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e , &pPager->journ
20fb0 61 6c 4f 66 66 2c 20 70 44 6f 6e 65 2c 20 31 2c alOff, pDone, 1,
20fc0 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 1);. }. a
20fd0 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
20fe0 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 20 20 E_DONE );. }.
20ff0 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 assert( rc!=SQLI
21000 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d TE_OK || pPager-
21010 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 3d 73 7a 4a >journalOff>=szJ
21020 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c 6c );.. /* Finall
21030 79 2c 20 20 72 6f 6c 6c 62 61 63 6b 20 70 61 67 y, rollback pag
21040 65 73 20 66 72 6f 6d 20 74 68 65 20 73 75 62 2d es from the sub-
21050 6a 6f 75 72 6e 61 6c 2e 20 20 50 61 67 65 20 74 journal. Page t
21060 68 61 74 20 77 65 72 65 0a 20 20 2a 2a 20 70 72 hat were. ** pr
21070 65 76 69 6f 75 73 6c 79 20 72 6f 6c 6c 65 64 20 eviously rolled
21080 62 61 63 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 back out of the
21090 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 28 61 6e main journal (an
210a0 64 20 61 72 65 20 68 65 6e 63 65 20 69 6e 20 70 d are hence in p
210b0 44 6f 6e 65 29 0a 20 20 2a 2a 20 77 69 6c 6c 20 Done). ** will
210c0 62 65 20 73 6b 69 70 70 65 64 2e 20 20 4f 75 74 be skipped. Out
210d0 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 73 20 -of-range pages
210e0 61 72 65 20 61 6c 73 6f 20 73 6b 69 70 70 65 64 are also skipped
210f0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 .. */. if( pSa
21100 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 75 vepoint ){. u
21110 33 32 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 32 ii;
21120 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
21130 72 20 2a 2f 0a 20 20 20 20 69 36 34 20 6f 66 66 r */. i64 off
21140 73 65 74 20 3d 20 28 69 36 34 29 70 53 61 76 65 set = (i64)pSave
21150 70 6f 69 6e 74 2d 3e 69 53 75 62 52 65 63 2a 28 point->iSubRec*(
21160 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 4+pPager->pageSi
21170 7a 65 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 61 ze);.. if( pa
21180 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 gerUseWal(pPager
21190 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 ) ){. rc =
211a0 73 71 6c 69 74 65 33 57 61 6c 53 61 76 65 70 6f sqlite3WalSavepo
211b0 69 6e 74 55 6e 64 6f 28 70 50 61 67 65 72 2d 3e intUndo(pPager->
211c0 70 57 61 6c 2c 20 70 53 61 76 65 70 6f 69 6e 74 pWal, pSavepoint
211d0 2d 3e 61 57 61 6c 44 61 74 61 29 3b 0a 20 20 20 ->aWalData);.
211e0 20 7d 0a 20 20 20 20 66 6f 72 28 69 69 3d 70 53 }. for(ii=pS
211f0 61 76 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52 65 avepoint->iSubRe
21200 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b c; rc==SQLITE_OK
21210 20 26 26 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e && ii<pPager->n
21220 53 75 62 52 65 63 3b 20 69 69 2b 2b 29 7b 0a 20 SubRec; ii++){.
21230 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 66 66 assert( off
21240 73 65 74 3d 3d 28 69 36 34 29 69 69 2a 28 34 2b set==(i64)ii*(4+
21250 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
21260 29 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 ) );. rc =
21270 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f pager_playback_o
21280 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c 20 ne_page(pPager,
21290 26 6f 66 66 73 65 74 2c 20 70 44 6f 6e 65 2c 20 &offset, pDone,
212a0 30 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 0, 1);. }.
212b0 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c assert( rc!=SQL
212c0 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a ITE_DONE );. }.
212d0 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 . sqlite3Bitvec
212e0 44 65 73 74 72 6f 79 28 70 44 6f 6e 65 29 3b 0a Destroy(pDone);.
212f0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
21300 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 67 65 _OK ){. pPage
21310 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
21320 73 7a 4a 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 szJ;. }.. retu
21330 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
21340 43 68 61 6e 67 65 20 74 68 65 20 6d 61 78 69 6d Change the maxim
21350 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d um number of in-
21360 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 74 68 61 memory pages tha
21370 74 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a t are allowed..*
21380 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 /.void sqlite3Pa
21390 67 65 72 53 65 74 43 61 63 68 65 73 69 7a 65 28 gerSetCachesize(
213a0 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
213b0 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 73 71 nt mxPage){. sq
213c0 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61 lite3PcacheSetCa
213d0 63 68 65 73 69 7a 65 28 70 50 61 67 65 72 2d 3e chesize(pPager->
213e0 70 50 43 61 63 68 65 2c 20 6d 78 50 61 67 65 29 pPCache, mxPage)
213f0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 ;.}../*.** Free
21400 61 73 20 6d 75 63 68 20 6d 65 6d 6f 72 79 20 61 as much memory a
21410 73 20 70 6f 73 73 69 62 6c 65 20 66 72 6f 6d 20 s possible from
21420 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a 76 6f the pager..*/.vo
21430 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 id sqlite3PagerS
21440 68 72 69 6e 6b 28 50 61 67 65 72 20 2a 70 50 61 hrink(Pager *pPa
21450 67 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 ger){. sqlite3P
21460 63 61 63 68 65 53 68 72 69 6e 6b 28 70 50 61 67 cacheShrink(pPag
21470 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 7d 0a er->pPCache);.}.
21480 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 74 68 ./*.** Adjust th
21490 65 20 72 6f 62 75 73 74 6e 65 73 73 20 6f 66 20 e robustness of
214a0 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 the database to
214b0 64 61 6d 61 67 65 20 64 75 65 20 74 6f 20 4f 53 damage due to OS
214c0 20 63 72 61 73 68 65 73 0a 2a 2a 20 6f 72 20 70 crashes.** or p
214d0 6f 77 65 72 20 66 61 69 6c 75 72 65 73 20 62 79 ower failures by
214e0 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 6e 75 changing the nu
214f0 6d 62 65 72 20 6f 66 20 73 79 6e 63 73 28 29 73 mber of syncs()s
21500 20 77 68 65 6e 20 77 72 69 74 69 6e 67 0a 2a 2a when writing.**
21510 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
21520 75 72 6e 61 6c 2e 20 20 54 68 65 72 65 20 61 72 urnal. There ar
21530 65 20 74 68 72 65 65 20 6c 65 76 65 6c 73 3a 0a e three levels:.
21540 2a 2a 0a 2a 2a 20 20 20 20 4f 46 46 20 20 20 20 **.** OFF
21550 20 20 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 sqlite3OsSync
21560 28 29 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c () is never call
21570 65 64 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 ed. This is the
21580 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 default.**
21590 20 20 20 20 20 20 20 20 20 66 6f 72 20 74 65 6d for tem
215a0 70 6f 72 61 72 79 20 61 6e 64 20 74 72 61 6e 73 porary and trans
215b0 69 65 6e 74 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a ient files..**.*
215c0 2a 20 20 20 20 4e 4f 52 4d 41 4c 20 20 20 20 54 * NORMAL T
215d0 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 he journal is sy
215e0 6e 63 65 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 nced once before
215f0 20 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f 6e writes begin on
21600 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
21610 20 20 20 20 20 64 61 74 61 62 61 73 65 2e 20 20 database.
21620 54 68 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 This is normally
21630 20 61 64 65 71 75 61 74 65 20 70 72 6f 74 65 63 adequate protec
21640 74 69 6f 6e 2c 20 62 75 74 0a 2a 2a 20 20 20 20 tion, but.**
21650 20 20 20 20 20 20 20 20 20 20 69 74 20 69 73 20 it is
21660 74 68 65 6f 72 65 74 69 63 61 6c 6c 79 20 70 6f theoretically po
21670 73 73 69 62 6c 65 2c 20 74 68 6f 75 67 68 20 76 ssible, though v
21680 65 72 79 20 75 6e 6c 69 6b 65 6c 79 2c 0a 2a 2a ery unlikely,.**
21690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 th
216a0 61 74 20 61 6e 20 69 6e 6f 70 65 72 74 75 6e 65 at an inopertune
216b0 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 63 power failure c
216c0 6f 75 6c 64 20 6c 65 61 76 65 20 74 68 65 20 6a ould leave the j
216d0 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 ournal.**
216e0 20 20 20 20 20 20 20 69 6e 20 61 20 73 74 61 74 in a stat
216f0 65 20 77 68 69 63 68 20 77 6f 75 6c 64 20 63 61 e which would ca
21700 75 73 65 20 64 61 6d 61 67 65 20 74 6f 20 74 68 use damage to th
21710 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 e database.**
21720 20 20 20 20 20 20 20 20 20 20 20 77 68 65 6e 20 when
21730 69 74 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 it is rolled bac
21740 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 46 55 4c 4c k..**.** FULL
21750 20 20 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61 The journa
21760 6c 20 69 73 20 73 79 6e 63 65 64 20 74 77 69 63 l is synced twic
21770 65 20 62 65 66 6f 72 65 20 77 72 69 74 65 73 20 e before writes
21780 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 begin on the.**
21790 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 dat
217a0 61 62 61 73 65 20 28 77 69 74 68 20 73 6f 6d 65 abase (with some
217b0 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f additional info
217c0 72 6d 61 74 69 6f 6e 20 2d 20 74 68 65 20 6e 52 rmation - the nR
217d0 65 63 20 66 69 65 6c 64 0a 2a 2a 20 20 20 20 20 ec field.**
217e0 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 of the
217f0 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2d journal header -
21800 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 20 69 being written i
21810 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 n between the tw
21820 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 o.**
21830 20 20 73 79 6e 63 73 29 2e 20 20 49 66 20 77 65 syncs). If we
21840 20 61 73 73 75 6d 65 20 74 68 61 74 20 77 72 69 assume that wri
21850 74 69 6e 67 20 61 0a 2a 2a 20 20 20 20 20 20 20 ting a.**
21860 20 20 20 20 20 20 20 73 69 6e 67 6c 65 20 64 69 single di
21870 73 6b 20 73 65 63 74 6f 72 20 69 73 20 61 74 6f sk sector is ato
21880 6d 69 63 2c 20 74 68 65 6e 20 74 68 69 73 20 6d mic, then this m
21890 6f 64 65 20 70 72 6f 76 69 64 65 73 0a 2a 2a 20 ode provides.**
218a0 20 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 ass
218b0 75 72 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 urance that the
218c0 6a 6f 75 72 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 journal will not
218d0 20 62 65 20 63 6f 72 72 75 70 74 65 64 20 74 6f be corrupted to
218e0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
218f0 20 20 20 20 20 70 6f 69 6e 74 20 6f 66 20 63 61 point of ca
21900 75 73 69 6e 67 20 64 61 6d 61 67 65 20 74 6f 20 using damage to
21910 74 68 65 20 64 61 74 61 62 61 73 65 20 64 75 72 the database dur
21920 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a ing rollback..**
21930 0a 2a 2a 20 54 68 65 20 61 62 6f 76 65 20 69 73 .** The above is
21940 20 66 6f 72 20 61 20 72 6f 6c 6c 62 61 63 6b 2d for a rollback-
21950 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2e 20 20 46 journal mode. F
21960 6f 72 20 57 41 4c 20 6d 6f 64 65 2c 20 4f 46 46 or WAL mode, OFF
21970 20 63 6f 6e 74 69 6e 75 65 73 0a 2a 2a 20 74 6f continues.** to
21980 20 6d 65 61 6e 20 74 68 61 74 20 6e 6f 20 73 79 mean that no sy
21990 6e 63 73 20 65 76 65 72 20 6f 63 63 75 72 2e 20 ncs ever occur.
219a0 20 4e 4f 52 4d 41 4c 20 6d 65 61 6e 73 20 74 68 NORMAL means th
219b0 61 74 20 74 68 65 20 57 41 4c 20 69 73 20 73 79 at the WAL is sy
219c0 6e 63 65 64 0a 2a 2a 20 70 72 69 6f 72 20 74 6f nced.** prior to
219d0 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 63 68 the start of ch
219e0 65 63 6b 70 6f 69 6e 74 20 61 6e 64 20 74 68 61 eckpoint and tha
219f0 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 t the database f
21a00 69 6c 65 20 69 73 20 73 79 6e 63 65 64 0a 2a 2a ile is synced.**
21a10 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 at the conclusi
21a20 6f 6e 20 6f 66 20 74 68 65 20 63 68 65 63 6b 70 on of the checkp
21a30 6f 69 6e 74 20 69 66 20 74 68 65 20 65 6e 74 69 oint if the enti
21a40 72 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 re content of th
21a50 65 20 57 41 4c 0a 2a 2a 20 77 61 73 20 77 72 69 e WAL.** was wri
21a60 74 74 65 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74 tten back into t
21a70 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 42 75 he database. Bu
21a80 74 20 6e 6f 20 73 79 6e 63 20 6f 70 65 72 61 74 t no sync operat
21a90 69 6f 6e 73 20 6f 63 63 75 72 20 66 6f 72 0a 2a ions occur for.*
21aa0 2a 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 63 6f * an ordinary co
21ab0 6d 6d 69 74 20 69 6e 20 4e 4f 52 4d 41 4c 20 6d mmit in NORMAL m
21ac0 6f 64 65 20 77 69 74 68 20 57 41 4c 2e 20 20 46 ode with WAL. F
21ad0 55 4c 4c 20 6d 65 61 6e 73 20 74 68 61 74 20 74 ULL means that t
21ae0 68 65 20 57 41 4c 0a 2a 2a 20 66 69 6c 65 20 69 he WAL.** file i
21af0 73 20 73 79 6e 63 65 64 20 66 6f 6c 6c 6f 77 69 s synced followi
21b00 6e 67 20 65 61 63 68 20 63 6f 6d 6d 69 74 20 6f ng each commit o
21b10 70 65 72 61 74 69 6f 6e 2c 20 69 6e 20 61 64 64 peration, in add
21b20 69 74 69 6f 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 ition to the.**
21b30 73 79 6e 63 73 20 61 73 73 6f 63 69 61 74 65 64 syncs associated
21b40 20 77 69 74 68 20 4e 4f 52 4d 41 4c 2e 0a 2a 2a with NORMAL..**
21b50 0a 2a 2a 20 44 6f 20 6e 6f 74 20 63 6f 6e 66 75 .** Do not confu
21b60 73 65 20 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 46 se synchronous=F
21b70 55 4c 4c 20 77 69 74 68 20 53 51 4c 49 54 45 5f ULL with SQLITE_
21b80 53 59 4e 43 5f 46 55 4c 4c 2e 20 20 54 68 65 0a SYNC_FULL. The.
21b90 2a 2a 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 ** SQLITE_SYNC_F
21ba0 55 4c 4c 20 6d 61 63 72 6f 20 6d 65 61 6e 73 20 ULL macro means
21bb0 74 6f 20 75 73 65 20 74 68 65 20 4d 61 63 4f 53 to use the MacOS
21bc0 58 2d 73 74 79 6c 65 20 66 75 6c 6c 2d 66 73 79 X-style full-fsy
21bd0 6e 63 0a 2a 2a 20 75 73 69 6e 67 20 66 63 6e 74 nc.** using fcnt
21be0 6c 28 46 5f 46 55 4c 4c 46 53 59 4e 43 29 2e 20 l(F_FULLFSYNC).
21bf0 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 SQLITE_SYNC_NOR
21c00 4d 41 4c 20 6d 65 61 6e 73 20 74 6f 20 64 6f 20 MAL means to do
21c10 61 6e 0a 2a 2a 20 6f 72 64 69 6e 61 72 79 20 66 an.** ordinary f
21c20 73 79 6e 63 28 29 20 63 61 6c 6c 2e 20 20 54 68 sync() call. Th
21c30 65 72 65 20 69 73 20 6e 6f 20 64 69 66 66 65 72 ere is no differ
21c40 65 6e 63 65 20 62 65 74 77 65 65 6e 20 53 51 4c ence between SQL
21c50 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 2a 2a ITE_SYNC_FULL.**
21c60 20 61 6e 64 20 53 51 4c 49 54 45 5f 53 59 4e 43 and SQLITE_SYNC
21c70 5f 4e 4f 52 4d 41 4c 20 6f 6e 20 70 6c 61 74 66 _NORMAL on platf
21c80 6f 72 6d 73 20 6f 74 68 65 72 20 74 68 61 6e 20 orms other than
21c90 4d 61 63 4f 53 58 2e 20 20 42 75 74 20 74 68 65 MacOSX. But the
21ca0 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e 6f 75 73 3d .** synchronous=
21cb0 46 55 4c 4c 20 76 65 72 73 75 73 20 73 79 6e 63 FULL versus sync
21cc0 68 72 6f 6e 6f 75 73 3d 4e 4f 52 4d 41 4c 20 73 hronous=NORMAL s
21cd0 65 74 74 69 6e 67 20 64 65 74 65 72 6d 69 6e 65 etting determine
21ce0 73 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 78 53 s when.** the xS
21cf0 79 6e 63 20 70 72 69 6d 69 74 69 76 65 20 69 73 ync primitive is
21d00 20 63 61 6c 6c 65 64 20 61 6e 64 20 69 73 20 72 called and is r
21d10 65 6c 65 76 61 6e 74 20 74 6f 20 61 6c 6c 20 70 elevant to all p
21d20 6c 61 74 66 6f 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 latforms..**.**
21d30 4e 75 6d 65 72 69 63 20 76 61 6c 75 65 73 20 61 Numeric values a
21d40 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
21d50 68 65 73 65 20 73 74 61 74 65 73 20 61 72 65 20 hese states are
21d60 4f 46 46 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 OFF==1, NORMAL=2
21d70 2c 0a 2a 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2e ,.** and FULL=3.
21d80 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
21d90 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 TE_OMIT_PAGER_PR
21da0 41 47 4d 41 53 0a 76 6f 69 64 20 73 71 6c 69 74 AGMAS.void sqlit
21db0 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79 e3PagerSetSafety
21dc0 4c 65 76 65 6c 28 0a 20 20 50 61 67 65 72 20 2a Level(. Pager *
21dd0 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 2f pPager, /
21de0 2a 20 54 68 65 20 70 61 67 65 72 20 74 6f 20 73 * The pager to s
21df0 65 74 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20 et safety level
21e00 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 76 for */. int lev
21e10 65 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f el, /
21e20 2a 20 50 52 41 47 4d 41 20 73 79 6e 63 68 72 6f * PRAGMA synchro
21e30 6e 6f 75 73 2e 20 20 31 3d 4f 46 46 2c 20 32 3d nous. 1=OFF, 2=
21e40 4e 4f 52 4d 41 4c 2c 20 33 3d 46 55 4c 4c 20 2a NORMAL, 3=FULL *
21e50 2f 20 20 0a 20 20 69 6e 74 20 62 46 75 6c 6c 46 / . int bFullF
21e60 73 79 6e 63 2c 20 20 20 20 20 20 20 2f 2a 20 50 sync, /* P
21e70 52 41 47 4d 41 20 66 75 6c 6c 66 73 79 6e 63 20 RAGMA fullfsync
21e80 2a 2f 0a 20 20 69 6e 74 20 62 43 6b 70 74 46 75 */. int bCkptFu
21e90 6c 6c 46 73 79 6e 63 20 20 20 20 2f 2a 20 50 52 llFsync /* PR
21ea0 41 47 4d 41 20 63 68 65 63 6b 70 6f 69 6e 74 5f AGMA checkpoint_
21eb0 66 75 6c 6c 66 73 79 6e 63 20 2a 2f 0a 29 7b 0a fullfsync */.){.
21ec0 20 20 61 73 73 65 72 74 28 20 6c 65 76 65 6c 3e assert( level>
21ed0 3d 31 20 26 26 20 6c 65 76 65 6c 3c 3d 33 20 29 =1 && level<=3 )
21ee0 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 ;. pPager->noSy
21ef0 6e 63 20 3d 20 20 28 6c 65 76 65 6c 3d 3d 31 20 nc = (level==1
21f00 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 || pPager->tempF
21f10 69 6c 65 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61 ile) ?1:0;. pPa
21f20 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 ger->fullSync =
21f30 28 6c 65 76 65 6c 3d 3d 33 20 26 26 20 21 70 50 (level==3 && !pP
21f40 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 20 ager->tempFile)
21f50 3f 31 3a 30 3b 0a 20 20 69 66 28 20 70 50 61 67 ?1:0;. if( pPag
21f60 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 er->noSync ){.
21f70 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c pPager->syncFl
21f80 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 ags = 0;. pPa
21f90 67 65 72 2d 3e 63 6b 70 74 53 79 6e 63 46 6c 61 ger->ckptSyncFla
21fa0 67 73 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 gs = 0;. }else
21fb0 69 66 28 20 62 46 75 6c 6c 46 73 79 6e 63 20 29 if( bFullFsync )
21fc0 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 79 {. pPager->sy
21fd0 6e 63 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 ncFlags = SQLITE
21fe0 5f 53 59 4e 43 5f 46 55 4c 4c 3b 0a 20 20 20 20 _SYNC_FULL;.
21ff0 70 50 61 67 65 72 2d 3e 63 6b 70 74 53 79 6e 63 pPager->ckptSync
22000 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 Flags = SQLITE_S
22010 59 4e 43 5f 46 55 4c 4c 3b 0a 20 20 7d 65 6c 73 YNC_FULL;. }els
22020 65 20 69 66 28 20 62 43 6b 70 74 46 75 6c 6c 46 e if( bCkptFullF
22030 73 79 6e 63 20 29 7b 0a 20 20 20 20 70 50 61 67 sync ){. pPag
22040 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73 20 3d 20 er->syncFlags =
22050 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d SQLITE_SYNC_NORM
22060 41 4c 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e AL;. pPager->
22070 63 6b 70 74 53 79 6e 63 46 6c 61 67 73 20 3d 20 ckptSyncFlags =
22080 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c SQLITE_SYNC_FULL
22090 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
220a0 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73 Pager->syncFlags
220b0 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e = SQLITE_SYNC_N
220c0 4f 52 4d 41 4c 3b 0a 20 20 20 20 70 50 61 67 65 ORMAL;. pPage
220d0 72 2d 3e 63 6b 70 74 53 79 6e 63 46 6c 61 67 73 r->ckptSyncFlags
220e0 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e = SQLITE_SYNC_N
220f0 4f 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20 70 50 61 ORMAL;. }. pPa
22100 67 65 72 2d 3e 77 61 6c 53 79 6e 63 46 6c 61 67 ger->walSyncFlag
22110 73 20 3d 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 s = pPager->sync
22120 46 6c 61 67 73 3b 0a 20 20 69 66 28 20 70 50 61 Flags;. if( pPa
22130 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 29 7b ger->fullSync ){
22140 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 77 61 6c . pPager->wal
22150 53 79 6e 63 46 6c 61 67 73 20 7c 3d 20 57 41 4c SyncFlags |= WAL
22160 5f 53 59 4e 43 5f 54 52 41 4e 53 41 43 54 49 4f _SYNC_TRANSACTIO
22170 4e 53 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 NS;. }.}.#endif
22180 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
22190 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 owing global var
221a0 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 iable is increme
221b0 6e 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 nted whenever th
221c0 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 61 74 74 e library.** att
221d0 65 6d 70 74 73 20 74 6f 20 6f 70 65 6e 20 61 20 empts to open a
221e0 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 temporary file.
221f0 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f This informatio
22200 6e 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a n is used for.**
22210 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 testing and ana
22220 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a 2f lysis only. .*/
22230 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
22240 45 53 54 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f EST.int sqlite3_
22250 6f 70 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 20 3d opentemp_count =
22260 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 0;.#endif../*.*
22270 2a 20 4f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 * Open a tempora
22280 72 79 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 ry file..**.** W
22290 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 64 65 rite the file de
222a0 73 63 72 69 70 74 6f 72 20 69 6e 74 6f 20 2a 70 scriptor into *p
222b0 46 69 6c 65 2e 20 52 65 74 75 72 6e 20 53 51 4c File. Return SQL
222c0 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 ITE_OK on succes
222d0 73 20 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 s .** or some ot
222e0 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 69 her error code i
222f0 66 20 77 65 20 66 61 69 6c 2e 20 54 68 65 20 4f f we fail. The O
22300 53 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 S will automatic
22310 61 6c 6c 79 20 0a 2a 2a 20 64 65 6c 65 74 65 20 ally .** delete
22320 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69 the temporary fi
22330 6c 65 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c le when it is cl
22340 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 osed..**.** The
22350 66 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f 20 flags passed to
22360 74 68 65 20 56 46 53 20 6c 61 79 65 72 20 78 4f the VFS layer xO
22370 70 65 6e 28 29 20 63 61 6c 6c 20 61 72 65 20 74 pen() call are t
22380 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 0a 2a hose specified.*
22390 2a 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 76 * by parameter v
223a0 66 73 46 6c 61 67 73 20 4f 52 65 64 20 77 69 74 fsFlags ORed wit
223b0 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a h the following:
223c0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 .**.** SQLIT
223d0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
223e0 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f .** SQLITE_O
223f0 50 45 4e 5f 43 52 45 41 54 45 0a 2a 2a 20 20 20 PEN_CREATE.**
22400 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 SQLITE_OPEN_EX
22410 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 53 CLUSIVE.** S
22420 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 QLITE_OPEN_DELET
22430 45 4f 4e 43 4c 4f 53 45 0a 2a 2f 0a 73 74 61 74 EONCLOSE.*/.stat
22440 69 63 20 69 6e 74 20 70 61 67 65 72 4f 70 65 6e ic int pagerOpen
22450 74 65 6d 70 28 0a 20 20 50 61 67 65 72 20 2a 70 temp(. Pager *p
22460 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 2f 2a Pager, /*
22470 20 54 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 The pager objec
22480 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 t */. sqlite3_f
22490 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 2f 2a 20 ile *pFile, /*
224a0 57 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 64 Write the file d
224b0 65 73 63 72 69 70 74 6f 72 20 68 65 72 65 20 2a escriptor here *
224c0 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 /. int vfsFlags
224d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 /* Fla
224e0 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 gs passed throug
224f0 68 20 74 6f 20 74 68 65 20 56 46 53 20 2a 2f 0a h to the VFS */.
22500 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
22510 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
22520 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 23 69 turn code */..#i
22530 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
22540 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 . sqlite3_opent
22550 65 6d 70 5f 63 6f 75 6e 74 2b 2b 3b 20 20 2f 2a emp_count++; /*
22560 20 55 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e Used for testin
22570 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f g and analysis o
22580 6e 6c 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 nly */.#endif..
22590 20 76 66 73 46 6c 61 67 73 20 7c 3d 20 20 53 51 vfsFlags |= SQ
225a0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
225b0 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 ITE | SQLITE_OPE
225c0 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20 20 20 20 N_CREATE |.
225d0 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 SQLITE_OP
225e0 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 20 53 EN_EXCLUSIVE | S
225f0 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 QLITE_OPEN_DELET
22600 45 4f 4e 43 4c 4f 53 45 3b 0a 20 20 72 63 20 3d EONCLOSE;. rc =
22610 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 sqlite3OsOpen(p
22620 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 30 2c 20 Pager->pVfs, 0,
22630 70 46 69 6c 65 2c 20 76 66 73 46 6c 61 67 73 2c pFile, vfsFlags,
22640 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 0);. assert( r
22650 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c!=SQLITE_OK ||
22660 69 73 4f 70 65 6e 28 70 46 69 6c 65 29 20 29 3b isOpen(pFile) );
22670 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
22680 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 ./*.** Set the b
22690 75 73 79 20 68 61 6e 64 6c 65 72 20 66 75 6e 63 usy handler func
226a0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tion..**.** The
226b0 70 61 67 65 72 20 69 6e 76 6f 6b 65 73 20 74 68 pager invokes th
226c0 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 e busy-handler i
226d0 66 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 f sqlite3OsLock(
226e0 29 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53 51 ) returns .** SQ
226f0 4c 49 54 45 5f 42 55 53 59 20 77 68 65 6e 20 74 LITE_BUSY when t
22700 72 79 69 6e 67 20 74 6f 20 75 70 67 72 61 64 65 rying to upgrade
22710 20 66 72 6f 6d 20 6e 6f 2d 6c 6f 63 6b 20 74 6f from no-lock to
22720 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 0a a SHARED lock,.
22730 2a 2a 20 6f 72 20 77 68 65 6e 20 74 72 79 69 6e ** or when tryin
22740 67 20 74 6f 20 75 70 67 72 61 64 65 20 66 72 6f g to upgrade fro
22750 6d 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 m a RESERVED loc
22760 6b 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 k to an EXCLUSIV
22770 45 20 0a 2a 2a 20 6c 6f 63 6b 2e 20 49 74 20 64 E .** lock. It d
22780 6f 65 73 20 2a 6e 6f 74 2a 20 69 6e 76 6f 6b 65 oes *not* invoke
22790 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
227a0 72 20 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67 r when upgrading
227b0 20 66 72 6f 6d 0a 2a 2a 20 53 48 41 52 45 44 20 from.** SHARED
227c0 74 6f 20 52 45 53 45 52 56 45 44 2c 20 6f 72 20 to RESERVED, or
227d0 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67 20 66 when upgrading f
227e0 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 45 58 rom SHARED to EX
227f0 43 4c 55 53 49 56 45 0a 2a 2a 20 28 77 68 69 63 CLUSIVE.** (whic
22800 68 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 h occurs during
22810 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c hot-journal roll
22820 62 61 63 6b 29 2e 20 53 75 6d 6d 61 72 79 3a 0a back). Summary:.
22830 2a 2a 0a 2a 2a 20 20 20 54 72 61 6e 73 69 74 69 **.** Transiti
22840 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 on
22850 20 20 20 20 20 20 20 20 20 20 7c 20 49 6e 76 6f | Invo
22860 6b 65 73 20 78 42 75 73 79 48 61 6e 64 6c 65 72 kes xBusyHandler
22870 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d .** ----------
22880 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
22890 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
228a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a --------------.*
228b0 2a 20 20 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 * NO_LOCK
228c0 20 20 2d 3e 20 53 48 41 52 45 44 5f 4c 4f 43 4b -> SHARED_LOCK
228d0 20 20 20 20 20 20 7c 20 59 65 73 0a 2a 2a 20 20 | Yes.**
228e0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d SHARED_LOCK -
228f0 3e 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 > RESERVED_LOCK
22900 20 20 20 7c 20 4e 6f 0a 2a 2a 20 20 20 53 48 41 | No.** SHA
22910 52 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 45 58 RED_LOCK -> EX
22920 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c CLUSIVE_LOCK |
22930 20 4e 6f 0a 2a 2a 20 20 20 52 45 53 45 52 56 45 No.** RESERVE
22940 44 5f 4c 4f 43 4b 20 2d 3e 20 45 58 43 4c 55 53 D_LOCK -> EXCLUS
22950 49 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 59 65 73 IVE_LOCK | Yes
22960 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 .**.** If the bu
22970 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 sy-handler callb
22980 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d ack returns non-
22990 7a 65 72 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69 zero, the lock i
229a0 73 20 0a 2a 2a 20 72 65 74 72 69 65 64 2e 20 49 s .** retried. I
229b0 66 20 69 74 20 72 65 74 75 72 6e 73 20 7a 65 72 f it returns zer
229c0 6f 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c 49 o, then the SQLI
229d0 54 45 5f 42 55 53 59 20 65 72 72 6f 72 20 69 73 TE_BUSY error is
229e0 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 74 6f 20 .** returned to
229f0 74 68 65 20 63 61 6c 6c 65 72 20 6f 66 20 74 68 the caller of th
22a00 65 20 70 61 67 65 72 20 41 50 49 20 66 75 6e 63 e pager API func
22a10 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 tion..*/.void sq
22a20 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73 lite3PagerSetBus
22a30 79 68 61 6e 64 6c 65 72 28 0a 20 20 50 61 67 65 yhandler(. Page
22a40 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 r *pPager,
22a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22a60 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 /* Pager object
22a70 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 */. int (*xBus
22a80 79 48 61 6e 64 6c 65 72 29 28 76 6f 69 64 20 2a yHandler)(void *
22a90 29 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f ), /* Po
22aa0 69 6e 74 65 72 20 74 6f 20 62 75 73 79 2d 68 61 inter to busy-ha
22ab0 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a ndler function *
22ac0 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 73 79 48 /. void *pBusyH
22ad0 61 6e 64 6c 65 72 41 72 67 20 20 20 20 20 20 20 andlerArg
22ae0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 /* Argu
22af0 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f 20 ment to pass to
22b00 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a xBusyHandler */.
22b10 29 7b 0a 20 20 70 50 61 67 65 72 2d 3e 78 42 75 ){. pPager->xBu
22b20 73 79 48 61 6e 64 6c 65 72 20 3d 20 78 42 75 73 syHandler = xBus
22b30 79 48 61 6e 64 6c 65 72 3b 0a 20 20 70 50 61 67 yHandler;. pPag
22b40 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 er->pBusyHandler
22b50 41 72 67 20 3d 20 70 42 75 73 79 48 61 6e 64 6c Arg = pBusyHandl
22b60 65 72 41 72 67 3b 0a 0a 20 20 69 66 28 20 69 73 erArg;.. if( is
22b70 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
22b80 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 2a 61 ){. void **a
22b90 70 20 3d 20 28 76 6f 69 64 20 2a 2a 29 26 70 50 p = (void **)&pP
22ba0 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 6c ager->xBusyHandl
22bb0 65 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 er;. assert(
22bc0 28 28 69 6e 74 28 2a 29 28 76 6f 69 64 20 2a 29 ((int(*)(void *)
22bd0 29 28 61 70 5b 30 5d 29 29 3d 3d 78 42 75 73 79 )(ap[0]))==xBusy
22be0 48 61 6e 64 6c 65 72 20 29 3b 0a 20 20 20 20 61 Handler );. a
22bf0 73 73 65 72 74 28 20 61 70 5b 31 5d 3d 3d 70 42 ssert( ap[1]==pB
22c00 75 73 79 48 61 6e 64 6c 65 72 41 72 67 20 29 3b usyHandlerArg );
22c10 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69 . sqlite3OsFi
22c20 6c 65 43 6f 6e 74 72 6f 6c 48 69 6e 74 28 70 50 leControlHint(pP
22c30 61 67 65 72 2d 3e 66 64 2c 20 53 51 4c 49 54 45 ager->fd, SQLITE
22c40 5f 46 43 4e 54 4c 5f 42 55 53 59 48 41 4e 44 4c _FCNTL_BUSYHANDL
22c50 45 52 2c 20 28 76 6f 69 64 20 2a 29 61 70 29 3b ER, (void *)ap);
22c60 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 . }.}../*.** Ch
22c70 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 73 69 ange the page si
22c80 7a 65 20 75 73 65 64 20 62 79 20 74 68 65 20 50 ze used by the P
22c90 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 54 68 65 ager object. The
22ca0 20 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20 0a new page size .
22cb0 2a 2a 20 69 73 20 70 61 73 73 65 64 20 69 6e 20 ** is passed in
22cc0 2a 70 50 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a *pPageSize..**.*
22cd0 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 * If the pager i
22ce0 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 73 s in the error s
22cf0 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 66 tate when this f
22d00 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
22d10 64 2c 20 69 74 0a 2a 2a 20 69 73 20 61 20 6e 6f d, it.** is a no
22d20 2d 6f 70 2e 20 54 68 65 20 76 61 6c 75 65 20 72 -op. The value r
22d30 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 65 eturned is the e
22d40 72 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f 72 rror state error
22d50 20 63 6f 64 65 20 28 69 2e 65 2e 20 0a 2a 2a 20 code (i.e. .**
22d60 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 49 4f one of SQLITE_IO
22d70 45 52 52 2c 20 61 6e 20 53 51 4c 49 54 45 5f 49 ERR, an SQLITE_I
22d80 4f 45 52 52 5f 78 78 78 20 73 75 62 2d 63 6f 64 OERR_xxx sub-cod
22d90 65 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c e or SQLITE_FULL
22da0 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 )..**.** Otherwi
22db0 73 65 2c 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 se, if all of th
22dc0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 e following are
22dd0 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 true:.**.** *
22de0 74 68 65 20 6e 65 77 20 70 61 67 65 20 73 69 7a the new page siz
22df0 65 20 28 76 61 6c 75 65 20 6f 66 20 2a 70 50 61 e (value of *pPa
22e00 67 65 53 69 7a 65 29 20 69 73 20 76 61 6c 69 64 geSize) is valid
22e10 20 28 61 20 70 6f 77 65 72 20 0a 2a 2a 20 20 20 (a power .**
22e20 20 20 6f 66 20 74 77 6f 20 62 65 74 77 65 65 6e of two between
22e30 20 35 31 32 20 61 6e 64 20 53 51 4c 49 54 45 5f 512 and SQLITE_
22e40 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 2c 20 69 MAX_PAGE_SIZE, i
22e50 6e 63 6c 75 73 69 76 65 29 2c 20 61 6e 64 0a 2a nclusive), and.*
22e60 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 72 65 20 61 *.** * there a
22e70 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e re no outstandin
22e80 67 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 g page reference
22e90 73 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a s, and.**.** *
22ea0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
22eb0 20 65 69 74 68 65 72 20 6e 6f 74 20 61 6e 20 69 either not an i
22ec0 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
22ed0 65 20 6f 72 20 69 74 20 69 73 0a 2a 2a 20 20 20 e or it is.**
22ee0 20 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 an in-memory d
22ef0 61 74 61 62 61 73 65 20 74 68 61 74 20 63 75 72 atabase that cur
22f00 72 65 6e 74 6c 79 20 63 6f 6e 73 69 73 74 73 20 rently consists
22f10 6f 66 20 7a 65 72 6f 20 70 61 67 65 73 2e 0a 2a of zero pages..*
22f20 2a 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 70 61 *.** then the pa
22f30 67 65 72 20 6f 62 6a 65 63 74 20 70 61 67 65 20 ger object page
22f40 73 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 2a size is set to *
22f50 70 50 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a pPageSize..**.**
22f60 20 49 66 20 74 68 65 20 70 61 67 65 20 73 69 7a If the page siz
22f70 65 20 69 73 20 63 68 61 6e 67 65 64 2c 20 74 68 e is changed, th
22f80 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
22f90 20 75 73 65 73 20 73 71 6c 69 74 65 33 50 61 67 uses sqlite3Pag
22fa0 65 72 4d 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 74 erMalloc() .** t
22fb0 6f 20 6f 62 74 61 69 6e 20 61 20 6e 65 77 20 50 o obtain a new P
22fc0 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65 20 62 ager.pTmpSpace b
22fd0 75 66 66 65 72 2e 20 49 66 20 74 68 69 73 20 61 uffer. If this a
22fe0 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 llocation attemp
22ff0 74 20 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c t .** fails, SQL
23000 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 ITE_NOMEM is ret
23010 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 70 61 urned and the pa
23020 67 65 20 73 69 7a 65 20 72 65 6d 61 69 6e 73 20 ge size remains
23030 75 6e 63 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 unchanged. .** I
23040 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 61 73 65 n all other case
23050 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 s, SQLITE_OK is
23060 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
23070 49 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 If the page size
23080 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 2c is not changed,
23090 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65 20 either because
230a0 6f 6e 65 20 6f 66 20 74 68 65 20 65 6e 75 6d 65 one of the enume
230b0 72 61 74 65 64 0a 2a 2a 20 63 6f 6e 64 69 74 69 rated.** conditi
230c0 6f 6e 73 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 ons above is not
230d0 20 74 72 75 65 2c 20 74 68 65 20 70 61 67 65 72 true, the pager
230e0 20 77 61 73 20 69 6e 20 65 72 72 6f 72 20 73 74 was in error st
230f0 61 74 65 20 77 68 65 6e 20 74 68 69 73 0a 2a 2a ate when this.**
23100 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 function was ca
23110 6c 6c 65 64 2c 20 6f 72 20 62 65 63 61 75 73 65 lled, or because
23120 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
23130 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 20 66 cation attempt f
23140 61 69 6c 65 64 2c 20 0a 2a 2a 20 74 68 65 6e 20 ailed, .** then
23150 2a 70 50 61 67 65 53 69 7a 65 20 69 73 20 73 65 *pPageSize is se
23160 74 20 74 6f 20 74 68 65 20 6f 6c 64 2c 20 72 65 t to the old, re
23170 74 61 69 6e 65 64 20 70 61 67 65 20 73 69 7a 65 tained page size
23180 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
23190 67 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 g..*/.int sqlite
231a0 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 3PagerSetPagesiz
231b0 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c e(Pager *pPager,
231c0 20 75 33 32 20 2a 70 50 61 67 65 53 69 7a 65 2c u32 *pPageSize,
231d0 20 69 6e 74 20 6e 52 65 73 65 72 76 65 29 7b 0a int nReserve){.
231e0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
231f0 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 E_OK;.. /* It i
23200 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 s not possible t
23210 6f 20 64 6f 20 61 20 66 75 6c 6c 20 61 73 73 65 o do a full asse
23220 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 29 rt_pager_state()
23230 20 68 65 72 65 2c 20 61 73 20 74 68 69 73 0a 20 here, as this.
23240 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 ** function may
23250 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 be called from
23260 77 69 74 68 69 6e 20 50 61 67 65 72 4f 70 65 6e within PagerOpen
23270 28 29 2c 20 62 65 66 6f 72 65 20 74 68 65 20 73 (), before the s
23280 74 61 74 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 tate. ** of the
23290 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 69 73 Pager object is
232a0 20 69 6e 74 65 72 6e 61 6c 6c 79 20 63 6f 6e 73 internally cons
232b0 69 73 74 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a istent.. **. *
232c0 2a 20 41 74 20 6f 6e 65 20 70 6f 69 6e 74 20 74 * At one point t
232d0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
232e0 75 72 6e 65 64 20 61 6e 20 65 72 72 6f 72 20 69 urned an error i
232f0 66 20 74 68 65 20 70 61 67 65 72 20 77 61 73 20 f the pager was
23300 69 6e 20 0a 20 20 2a 2a 20 50 41 47 45 52 5f 45 in . ** PAGER_E
23310 52 52 4f 52 20 73 74 61 74 65 2e 20 42 75 74 20 RROR state. But
23320 73 69 6e 63 65 20 50 41 47 45 52 5f 45 52 52 4f since PAGER_ERRO
23330 52 20 73 74 61 74 65 20 67 75 61 72 61 6e 74 65 R state guarante
23340 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 es that. ** the
23350 72 65 20 69 73 20 61 74 20 6c 65 61 73 74 20 6f re is at least o
23360 6e 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 ne outstanding p
23370 61 67 65 20 72 65 66 65 72 65 6e 63 65 2c 20 74 age reference, t
23380 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a his function. *
23390 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 * is a no-op for
233a0 20 74 68 61 74 20 63 61 73 65 20 61 6e 79 68 6f that case anyho
233b0 77 2e 0a 20 20 2a 2f 0a 0a 20 20 75 33 32 20 70 w.. */.. u32 p
233c0 61 67 65 53 69 7a 65 20 3d 20 2a 70 50 61 67 65 ageSize = *pPage
233d0 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 Size;. assert(
233e0 70 61 67 65 53 69 7a 65 3d 3d 30 20 7c 7c 20 28 pageSize==0 || (
233f0 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 pageSize>=512 &&
23400 20 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 pageSize<=SQLIT
23410 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 E_MAX_PAGE_SIZE)
23420 20 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65 );. if( (pPage
23430 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 7c 7c 20 70 r->memDb==0 || p
23440 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 30 Pager->dbSize==0
23450 29 0a 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 ). && sqlite3P
23460 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 cacheRefCount(pP
23470 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d ager->pPCache)==
23480 30 20 0a 20 20 20 26 26 20 70 61 67 65 53 69 7a 0 . && pageSiz
23490 65 20 26 26 20 70 61 67 65 53 69 7a 65 21 3d 28 e && pageSize!=(
234a0 75 33 32 29 70 50 61 67 65 72 2d 3e 70 61 67 65 u32)pPager->page
234b0 53 69 7a 65 20 0a 20 20 29 7b 0a 20 20 20 20 63 Size . ){. c
234c0 68 61 72 20 2a 70 4e 65 77 20 3d 20 4e 55 4c 4c har *pNew = NULL
234d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
234e0 20 4e 65 77 20 74 65 6d 70 20 73 70 61 63 65 20 New temp space
234f0 2a 2f 0a 20 20 20 20 69 36 34 20 6e 42 79 74 65 */. i64 nByte
23500 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66 28 20 70 = 0;.. if( p
23510 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 50 41 Pager->eState>PA
23520 47 45 52 5f 4f 50 45 4e 20 26 26 20 69 73 4f 70 GER_OPEN && isOp
23530 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 en(pPager->fd) )
23540 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
23550 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 ite3OsFileSize(p
23560 50 61 67 65 72 2d 3e 66 64 2c 20 26 6e 42 79 74 Pager->fd, &nByt
23570 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 e);. }. if
23580 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
23590 29 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 ){. pNew =
235a0 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50 (char *)sqlite3P
235b0 61 67 65 4d 61 6c 6c 6f 63 28 70 61 67 65 53 69 ageMalloc(pageSi
235c0 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 ze);. if( !
235d0 70 4e 65 77 20 29 20 72 63 20 3d 20 53 51 4c 49 pNew ) rc = SQLI
235e0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
235f0 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
23600 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
23610 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 pager_reset(pPag
23620 65 72 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 er);. pPage
23630 72 2d 3e 64 62 53 69 7a 65 20 3d 20 28 50 67 6e r->dbSize = (Pgn
23640 6f 29 28 28 6e 42 79 74 65 2b 70 61 67 65 53 69 o)((nByte+pageSi
23650 7a 65 2d 31 29 2f 70 61 67 65 53 69 7a 65 29 3b ze-1)/pageSize);
23660 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 . pPager->p
23670 61 67 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 ageSize = pageSi
23680 7a 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ze;. sqlite
23690 33 50 61 67 65 46 72 65 65 28 70 50 61 67 65 72 3PageFree(pPager
236a0 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 ->pTmpSpace);.
236b0 20 20 20 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 pPager->pTmp
236c0 53 70 61 63 65 20 3d 20 70 4e 65 77 3b 0a 20 20 Space = pNew;.
236d0 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 sqlite3Pcach
236e0 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 50 61 eSetPageSize(pPa
236f0 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 ger->pPCache, pa
23700 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 geSize);. }.
23710 20 7d 0a 0a 20 20 2a 70 50 61 67 65 53 69 7a 65 }.. *pPageSize
23720 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 = pPager->pageS
23730 69 7a 65 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 ize;. if( rc==S
23740 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
23750 69 66 28 20 6e 52 65 73 65 72 76 65 3c 30 20 29 if( nReserve<0 )
23760 20 6e 52 65 73 65 72 76 65 20 3d 20 70 50 61 67 nReserve = pPag
23770 65 72 2d 3e 6e 52 65 73 65 72 76 65 3b 0a 20 20 er->nReserve;.
23780 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 65 72 assert( nReser
23790 76 65 3e 3d 30 20 26 26 20 6e 52 65 73 65 72 76 ve>=0 && nReserv
237a0 65 3c 31 30 30 30 20 29 3b 0a 20 20 20 20 70 50 e<1000 );. pP
237b0 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 20 3d ager->nReserve =
237c0 20 28 69 31 36 29 6e 52 65 73 65 72 76 65 3b 0a (i16)nReserve;.
237d0 20 20 20 20 70 61 67 65 72 52 65 70 6f 72 74 53 pagerReportS
237e0 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 7d ize(pPager);. }
237f0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
23800 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
23810 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 22 pointer to the "
23820 74 65 6d 70 6f 72 61 72 79 20 70 61 67 65 22 20 temporary page"
23830 62 75 66 66 65 72 20 68 65 6c 64 20 69 6e 74 65 buffer held inte
23840 72 6e 61 6c 6c 79 0a 2a 2a 20 62 79 20 74 68 65 rnally.** by the
23850 20 70 61 67 65 72 2e 20 20 54 68 69 73 20 69 73 pager. This is
23860 20 61 20 62 75 66 66 65 72 20 74 68 61 74 20 69 a buffer that i
23870 73 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 s big enough to
23880 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 65 6e 74 69 hold the.** enti
23890 72 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 re content of a
238a0 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 20 database page.
238b0 54 68 69 73 20 62 75 66 66 65 72 20 69 73 20 75 This buffer is u
238c0 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a sed internally.*
238d0 2a 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 * during rollbac
238e0 6b 20 61 6e 64 20 77 69 6c 6c 20 62 65 20 6f 76 k and will be ov
238f0 65 72 77 72 69 74 74 65 6e 20 77 68 65 6e 65 76 erwritten whenev
23900 65 72 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a er a rollback.**
23910 20 6f 63 63 75 72 73 2e 20 20 42 75 74 20 6f 74 occurs. But ot
23920 68 65 72 20 6d 6f 64 75 6c 65 73 20 61 72 65 20 her modules are
23930 66 72 65 65 20 74 6f 20 75 73 65 20 69 74 20 74 free to use it t
23940 6f 6f 2c 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a oo, as long as.*
23950 2a 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 61 * no rollbacks a
23960 72 65 20 68 61 70 70 65 6e 69 6e 67 2e 0a 2a 2f re happening..*/
23970 0a 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 .void *sqlite3Pa
23980 67 65 72 54 65 6d 70 53 70 61 63 65 28 50 61 67 gerTempSpace(Pag
23990 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 er *pPager){. r
239a0 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 70 54 eturn pPager->pT
239b0 6d 70 53 70 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a mpSpace;.}../*.*
239c0 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73 65 74 * Attempt to set
239d0 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 64 61 74 the maximum dat
239e0 61 62 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74 abase page count
239f0 20 69 66 20 6d 78 50 61 67 65 20 69 73 20 70 6f if mxPage is po
23a00 73 69 74 69 76 65 2e 20 0a 2a 2a 20 4d 61 6b 65 sitive. .** Make
23a10 20 6e 6f 20 63 68 61 6e 67 65 73 20 69 66 20 6d no changes if m
23a20 78 50 61 67 65 20 69 73 20 7a 65 72 6f 20 6f 72 xPage is zero or
23a30 20 6e 65 67 61 74 69 76 65 2e 20 20 41 6e 64 20 negative. And
23a40 6e 65 76 65 72 20 72 65 64 75 63 65 20 74 68 65 never reduce the
23a50 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 .** maximum page
23a60 20 63 6f 75 6e 74 20 62 65 6c 6f 77 20 74 68 65 count below the
23a70 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 current size of
23a80 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a the database..*
23a90 2a 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 *.** Regardless
23aa0 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 of mxPage, retur
23ab0 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 n the current ma
23ac0 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 ximum page count
23ad0 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
23ae0 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e PagerMaxPageCoun
23af0 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c t(Pager *pPager,
23b00 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 int mxPage){.
23b10 69 66 28 20 6d 78 50 61 67 65 3e 30 20 29 7b 0a if( mxPage>0 ){.
23b20 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 pPager->mxPg
23b30 6e 6f 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 7d no = mxPage;. }
23b40 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
23b50 72 2d 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52 r->eState!=PAGER
23b60 5f 4f 50 45 4e 20 29 3b 20 20 20 20 20 20 2f 2a _OPEN ); /*
23b70 20 43 61 6c 6c 65 64 20 6f 6e 6c 79 20 62 79 20 Called only by
23b80 4f 50 5f 4d 61 78 50 67 63 6e 74 20 2a 2f 0a 20 OP_MaxPgcnt */.
23b90 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
23ba0 3e 6d 78 50 67 6e 6f 3e 3d 70 50 61 67 65 72 2d >mxPgno>=pPager-
23bb0 3e 64 62 53 69 7a 65 20 29 3b 20 20 2f 2a 20 4f >dbSize ); /* O
23bc0 50 5f 4d 61 78 50 67 63 6e 74 20 65 6e 66 6f 72 P_MaxPgcnt enfor
23bd0 63 65 73 20 74 68 69 73 20 2a 2f 0a 20 20 72 65 ces this */. re
23be0 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6d 78 50 turn pPager->mxP
23bf0 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 gno;.}../*.** Th
23c00 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 74 20 e following set
23c10 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 of routines are
23c20 75 73 65 64 20 74 6f 20 64 69 73 61 62 6c 65 20 used to disable
23c30 74 68 65 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a the simulated.**
23c40 20 49 2f 4f 20 65 72 72 6f 72 20 6d 65 63 68 61 I/O error mecha
23c50 6e 69 73 6d 2e 20 20 54 68 65 73 65 20 72 6f 75 nism. These rou
23c60 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 tines are used t
23c70 6f 20 61 76 6f 69 64 20 73 69 6d 75 6c 61 74 65 o avoid simulate
23c80 64 0a 2a 2a 20 65 72 72 6f 72 73 20 69 6e 20 70 d.** errors in p
23c90 6c 61 63 65 73 20 77 68 65 72 65 20 77 65 20 64 laces where we d
23ca0 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 o not care about
23cb0 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 55 errors..**.** U
23cc0 6e 6c 65 73 73 20 2d 44 53 51 4c 49 54 45 5f 54 nless -DSQLITE_T
23cd0 45 53 54 3d 31 20 69 73 20 75 73 65 64 2c 20 74 EST=1 is used, t
23ce0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 hese routines ar
23cf0 65 20 61 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a 2a 20 e all no-ops.**
23d00 61 6e 64 20 67 65 6e 65 72 61 74 65 20 6e 6f 20 and generate no
23d10 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 code..*/.#ifdef
23d20 53 51 4c 49 54 45 5f 54 45 53 54 0a 65 78 74 65 SQLITE_TEST.exte
23d30 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 rn int sqlite3_i
23d40 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b o_error_pending;
23d50 0a 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 .extern int sqli
23d60 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 te3_io_error_hit
23d70 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 ;.static int sav
23d80 65 64 5f 63 6e 74 3b 0a 76 6f 69 64 20 64 69 73 ed_cnt;.void dis
23d90 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 able_simulated_i
23da0 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a o_errors(void){.
23db0 20 20 73 61 76 65 64 5f 63 6e 74 20 3d 20 73 71 saved_cnt = sq
23dc0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
23dd0 65 6e 64 69 6e 67 3b 0a 20 20 73 71 6c 69 74 65 ending;. sqlite
23de0 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 3_io_error_pendi
23df0 6e 67 20 3d 20 2d 31 3b 0a 7d 0a 76 6f 69 64 20 ng = -1;.}.void
23e00 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 enable_simulated
23e10 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 _io_errors(void)
23e20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 {. sqlite3_io_e
23e30 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 73 rror_pending = s
23e40 61 76 65 64 5f 63 6e 74 3b 0a 7d 0a 23 65 6c 73 aved_cnt;.}.#els
23e50 65 0a 23 20 64 65 66 69 6e 65 20 64 69 73 61 62 e.# define disab
23e60 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f le_simulated_io_
23e70 65 72 72 6f 72 73 28 29 0a 23 20 64 65 66 69 6e errors().# defin
23e80 65 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 e enable_simulat
23e90 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 ed_io_errors().#
23ea0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 endif../*.** Rea
23eb0 64 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79 d the first N by
23ec0 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 tes from the beg
23ed0 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 66 69 inning of the fi
23ee0 6c 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 0a 2a le into memory.*
23ef0 2a 20 74 68 61 74 20 70 44 65 73 74 20 70 6f 69 * that pDest poi
23f00 6e 74 73 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 nts to. .**.** I
23f10 66 20 74 68 65 20 70 61 67 65 72 20 77 61 73 20 f the pager was
23f20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 74 72 61 6e opened on a tran
23f30 73 69 65 6e 74 20 66 69 6c 65 20 28 7a 46 69 6c sient file (zFil
23f40 65 6e 61 6d 65 3d 3d 22 22 29 2c 20 6f 72 0a 2a ename==""), or.*
23f50 2a 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 66 69 * opened on a fi
23f60 6c 65 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 le less than N b
23f70 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 74 68 ytes in size, th
23f80 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 e output buffer
23f90 69 73 0a 2a 2a 20 7a 65 72 6f 65 64 20 61 6e 64 is.** zeroed and
23fa0 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 SQLITE_OK retur
23fb0 6e 65 64 2e 20 54 68 65 20 72 61 74 69 6f 6e 61 ned. The rationa
23fc0 6c 65 20 66 6f 72 20 74 68 69 73 20 69 73 20 74 le for this is t
23fd0 68 61 74 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e hat this .** fun
23fe0 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f ction is used to
23ff0 20 72 65 61 64 20 64 61 74 61 62 61 73 65 20 68 read database h
24000 65 61 64 65 72 73 2c 20 61 6e 64 20 61 20 6e 65 eaders, and a ne
24010 77 20 74 72 61 6e 73 69 65 6e 74 20 6f 72 0a 2a w transient or.*
24020 2a 20 7a 65 72 6f 20 73 69 7a 65 64 20 64 61 74 * zero sized dat
24030 61 62 61 73 65 20 68 61 73 20 61 20 68 65 61 64 abase has a head
24040 65 72 20 74 68 61 6e 20 63 6f 6e 73 69 73 74 73 er than consists
24050 20 65 6e 74 69 72 65 6c 79 20 6f 66 20 7a 65 72 entirely of zer
24060 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e oes..**.** If an
24070 79 20 49 4f 20 65 72 72 6f 72 20 61 70 61 72 74 y IO error apart
24080 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 49 4f 45 from SQLITE_IOE
24090 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 69 73 RR_SHORT_READ is
240a0 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 0a 2a 2a encountered,.**
240b0 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 the error code
240c0 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 is returned to t
240d0 68 65 20 63 61 6c 6c 65 72 20 61 6e 64 20 74 68 he caller and th
240e0 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
240f0 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75 66 66 e.** output buff
24100 65 72 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f er undefined..*/
24110 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 .int sqlite3Page
24120 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 rReadFileheader(
24130 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
24140 6e 74 20 4e 2c 20 75 6e 73 69 67 6e 65 64 20 63 nt N, unsigned c
24150 68 61 72 20 2a 70 44 65 73 74 29 7b 0a 20 20 69 har *pDest){. i
24160 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
24170 4b 3b 0a 20 20 6d 65 6d 73 65 74 28 70 44 65 73 K;. memset(pDes
24180 74 2c 20 30 2c 20 4e 29 3b 0a 20 20 61 73 73 65 t, 0, N);. asse
24190 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 rt( isOpen(pPage
241a0 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 72 r->fd) || pPager
241b0 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 0a 20 ->tempFile );..
241c0 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 /* This routine
241d0 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 is only called
241e0 62 79 20 62 74 72 65 65 20 69 6d 6d 65 64 69 61 by btree immedia
241f0 74 65 6c 79 20 61 66 74 65 72 20 63 72 65 61 74 tely after creat
24200 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 50 61 67 ing. ** the Pag
24210 65 72 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 72 er object. Ther
24220 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61 e has not been a
24230 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f n opportunity to
24240 20 74 72 61 6e 73 69 74 69 6f 6e 0a 20 20 2a 2a transition. **
24250 20 74 6f 20 57 41 4c 20 6d 6f 64 65 20 79 65 74 to WAL mode yet
24260 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
24270 20 21 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 !pagerUseWal(pP
24280 61 67 65 72 29 20 29 3b 0a 0a 20 20 69 66 28 20 ager) );.. if(
24290 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
242a0 64 29 20 29 7b 0a 20 20 20 20 49 4f 54 52 41 43 d) ){. IOTRAC
242b0 45 28 28 22 44 42 48 44 52 20 25 70 20 30 20 25 E(("DBHDR %p 0 %
242c0 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 4e 29 d\n", pPager, N)
242d0 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ). rc = sqlit
242e0 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d e3OsRead(pPager-
242f0 3e 66 64 2c 20 70 44 65 73 74 2c 20 4e 2c 20 30 >fd, pDest, N, 0
24300 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
24310 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 QLITE_IOERR_SHOR
24320 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 T_READ ){.
24330 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
24340 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
24350 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
24360 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 This function ma
24370 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 y only be called
24380 20 77 68 65 6e 20 61 20 72 65 61 64 2d 74 72 61 when a read-tra
24390 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e nsaction is open
243a0 20 6f 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 72 on.** the pager
243b0 2e 20 49 74 20 72 65 74 75 72 6e 73 20 74 68 65 . It returns the
243c0 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
243d0 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 pages in the da
243e0 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 48 6f tabase..**.** Ho
243f0 77 65 76 65 72 2c 20 69 66 20 74 68 65 20 66 69 wever, if the fi
24400 6c 65 20 69 73 20 62 65 74 77 65 65 6e 20 31 20 le is between 1
24410 61 6e 64 20 3c 70 61 67 65 2d 73 69 7a 65 3e 20 and <page-size>
24420 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 74 bytes in size, t
24430 68 65 6e 20 0a 2a 2a 20 74 68 69 73 20 69 73 20 hen .** this is
24440 63 6f 6e 73 69 64 65 72 65 64 20 61 20 31 20 70 considered a 1 p
24450 61 67 65 20 66 69 6c 65 2e 0a 2a 2f 0a 76 6f 69 age file..*/.voi
24460 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 d sqlite3PagerPa
24470 67 65 63 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 gecount(Pager *p
24480 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 6e 50 61 Pager, int *pnPa
24490 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 ge){. assert( p
244a0 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50 Pager->eState>=P
244b0 41 47 45 52 5f 52 45 41 44 45 52 20 29 3b 0a 20 AGER_READER );.
244c0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
244d0 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52 5f 57 >eState!=PAGER_W
244e0 52 49 54 45 52 5f 46 49 4e 49 53 48 45 44 20 29 RITER_FINISHED )
244f0 3b 0a 20 20 2a 70 6e 50 61 67 65 20 3d 20 28 69 ;. *pnPage = (i
24500 6e 74 29 70 50 61 67 65 72 2d 3e 64 62 53 69 7a nt)pPager->dbSiz
24510 65 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 e;.}.../*.** Try
24520 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63 to obtain a loc
24530 6b 20 6f 66 20 74 79 70 65 20 6c 6f 63 6b 74 79 k of type lockty
24540 70 65 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 pe on the databa
24550 73 65 20 66 69 6c 65 2e 20 49 66 0a 2a 2a 20 61 se file. If.** a
24560 20 73 69 6d 69 6c 61 72 20 6f 72 20 67 72 65 61 similar or grea
24570 74 65 72 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 ter lock is alre
24580 61 64 79 20 68 65 6c 64 2c 20 74 68 69 73 20 66 ady held, this f
24590 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d unction is a no-
245a0 6f 70 0a 2a 2a 20 28 72 65 74 75 72 6e 69 6e 67 op.** (returning
245b0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 6d 6d 65 64 SQLITE_OK immed
245c0 69 61 74 65 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 4f iately)..**.** O
245d0 74 68 65 72 77 69 73 65 2c 20 61 74 74 65 6d 70 therwise, attemp
245e0 74 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 t to obtain the
245f0 6c 6f 63 6b 20 75 73 69 6e 67 20 73 71 6c 69 74 lock using sqlit
24600 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e 76 6f e3OsLock(). Invo
24610 6b 65 20 0a 2a 2a 20 74 68 65 20 62 75 73 79 20 ke .** the busy
24620 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 20 callback if the
24630 6c 6f 63 6b 20 69 73 20 63 75 72 72 65 6e 74 6c lock is currentl
24640 79 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e y not available.
24650 20 52 65 70 65 61 74 20 0a 2a 2a 20 75 6e 74 69 Repeat .** unti
24660 6c 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 l the busy callb
24670 61 63 6b 20 72 65 74 75 72 6e 73 20 66 61 6c 73 ack returns fals
24680 65 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 61 e or until the a
24690 74 74 65 6d 70 74 20 74 6f 20 0a 2a 2a 20 6f 62 ttempt to .** ob
246a0 74 61 69 6e 20 74 68 65 20 6c 6f 63 6b 20 73 75 tain the lock su
246b0 63 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 cceeds..**.** Re
246c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f turn SQLITE_OK o
246d0 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 61 6e n success and an
246e0 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 77 error code if w
246f0 65 20 63 61 6e 6e 6f 74 20 6f 62 74 61 69 6e 0a e cannot obtain.
24700 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 20 49 66 20 ** the lock. If
24710 74 68 65 20 6c 6f 63 6b 20 69 73 20 6f 62 74 61 the lock is obta
24720 69 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c ined successfull
24730 79 2c 20 73 65 74 20 74 68 65 20 50 61 67 65 72 y, set the Pager
24740 2e 73 74 61 74 65 20 0a 2a 2a 20 76 61 72 69 61 .state .** varia
24750 62 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 20 ble to locktype
24760 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 before returning
24770 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
24780 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f pager_wait_on_lo
24790 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ck(Pager *pPager
247a0 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b , int locktype){
247b0 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
247c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
247d0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
247e0 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 n code */.. /*
247f0 43 68 65 63 6b 20 74 68 61 74 20 74 68 69 73 20 Check that this
24800 69 73 20 65 69 74 68 65 72 20 61 20 6e 6f 2d 6f is either a no-o
24810 70 20 28 62 65 63 61 75 73 65 20 74 68 65 20 72 p (because the r
24820 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 69 73 equested lock is
24830 20 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 68 . ** already h
24840 65 6c 64 2c 20 6f 72 20 6f 6e 65 20 6f 66 20 74 eld, or one of t
24850 68 65 20 74 72 61 6e 73 69 73 74 69 6f 6e 73 20 he transistions
24860 74 68 61 74 20 74 68 65 20 62 75 73 79 2d 68 61 that the busy-ha
24870 6e 64 6c 65 72 0a 20 20 2a 2a 20 6d 61 79 20 62 ndler. ** may b
24880 65 20 69 6e 76 6f 6b 65 64 20 64 75 72 69 6e 67 e invoked during
24890 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 , according to t
248a0 68 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 he comment above
248b0 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 . ** sqlite3Pag
248c0 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 erSetBusyhandler
248d0 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ().. */. asser
248e0 74 28 20 28 70 50 61 67 65 72 2d 3e 65 4c 6f 63 t( (pPager->eLoc
248f0 6b 3e 3d 6c 6f 63 6b 74 79 70 65 29 0a 20 20 20 k>=locktype).
24900 20 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e || (pPager->
24910 65 4c 6f 63 6b 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 eLock==NO_LOCK &
24920 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 & locktype==SHAR
24930 45 44 5f 4c 4f 43 4b 29 0a 20 20 20 20 20 20 20 ED_LOCK).
24940 7c 7c 20 28 70 50 61 67 65 72 2d 3e 65 4c 6f 63 || (pPager->eLoc
24950 6b 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b k==RESERVED_LOCK
24960 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 && locktype==EX
24970 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 0a 20 20 CLUSIVE_LOCK).
24980 29 3b 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20 72 );.. do {. r
24990 63 20 3d 20 70 61 67 65 72 4c 6f 63 6b 44 62 28 c = pagerLockDb(
249a0 70 50 61 67 65 72 2c 20 6c 6f 63 6b 74 79 70 65 pPager, locktype
249b0 29 3b 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d );. }while( rc=
249c0 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 =SQLITE_BUSY &&
249d0 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e pPager->xBusyHan
249e0 64 6c 65 72 28 70 50 61 67 65 72 2d 3e 70 42 75 dler(pPager->pBu
249f0 73 79 48 61 6e 64 6c 65 72 41 72 67 29 20 29 3b syHandlerArg) );
24a00 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
24a10 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 ./*.** Function
24a20 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f assertTruncateCo
24a30 6e 73 74 72 61 69 6e 74 28 70 50 61 67 65 72 29 nstraint(pPager)
24a40 20 63 68 65 63 6b 73 20 74 68 61 74 20 6f 6e 65 checks that one
24a50 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 6f 6c 6c of the .** foll
24a60 6f 77 69 6e 67 20 69 73 20 74 72 75 65 20 66 6f owing is true fo
24a70 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 r all dirty page
24a80 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 s currently in t
24a90 68 65 20 70 61 67 65 2d 63 61 63 68 65 3a 0a 2a he page-cache:.*
24aa0 2a 0a 2a 2a 20 20 20 61 29 20 54 68 65 20 70 61 *.** a) The pa
24ab0 67 65 20 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 ge number is les
24ac0 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 s than or equal
24ad0 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 to the size of t
24ae0 68 65 20 0a 2a 2a 20 20 20 20 20 20 63 75 72 72 he .** curr
24af0 65 6e 74 20 64 61 74 61 62 61 73 65 20 69 6d 61 ent database ima
24b00 67 65 2c 20 69 6e 20 70 61 67 65 73 2c 20 4f 52 ge, in pages, OR
24b10 0a 2a 2a 0a 2a 2a 20 20 20 62 29 20 69 66 20 74 .**.** b) if t
24b20 68 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 he page content
24b30 77 65 72 65 20 77 72 69 74 74 65 6e 20 61 74 20 were written at
24b40 74 68 69 73 20 74 69 6d 65 2c 20 69 74 20 77 6f this time, it wo
24b50 75 6c 64 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 uld not.**
24b60 62 65 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 be necessary to
24b70 77 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e write the curren
24b80 74 20 63 6f 6e 74 65 6e 74 20 6f 75 74 20 74 6f t content out to
24b90 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
24ba0 0a 2a 2a 20 20 20 20 20 20 28 61 73 20 64 65 74 .** (as det
24bb0 65 72 6d 69 6e 65 64 20 62 79 20 66 75 6e 63 74 ermined by funct
24bc0 69 6f 6e 20 73 75 62 6a 52 65 71 75 69 72 65 73 ion subjRequires
24bd0 50 61 67 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 49 Page())..**.** I
24be0 66 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 f the condition
24bf0 61 73 73 65 72 74 65 64 20 62 79 20 74 68 69 73 asserted by this
24c00 20 66 75 6e 63 74 69 6f 6e 20 77 65 72 65 20 6e function were n
24c10 6f 74 20 74 72 75 65 2c 20 61 6e 64 20 74 68 65 ot true, and the
24c20 0a 2a 2a 20 64 69 72 74 79 20 70 61 67 65 20 77 .** dirty page w
24c30 65 72 65 20 74 6f 20 62 65 20 64 69 73 63 61 72 ere to be discar
24c40 64 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63 ded from the cac
24c50 68 65 20 76 69 61 20 74 68 65 20 70 61 67 65 72 he via the pager
24c60 53 74 72 65 73 73 28 29 0a 2a 2a 20 72 6f 75 74 Stress().** rout
24c70 69 6e 65 2c 20 70 61 67 65 72 53 74 72 65 73 73 ine, pagerStress
24c80 28 29 20 77 6f 75 6c 64 20 6e 6f 74 20 77 72 69 () would not wri
24c90 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 te the current p
24ca0 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 6f 0a 2a age content to.*
24cb0 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 * the database f
24cc0 69 6c 65 2e 20 49 66 20 61 20 73 61 76 65 70 6f ile. If a savepo
24cd0 69 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 int transaction
24ce0 77 65 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b were rolled back
24cf0 20 61 66 74 65 72 0a 2a 2a 20 74 68 69 73 20 68 after.** this h
24d00 61 70 70 65 6e 65 64 2c 20 74 68 65 20 63 6f 72 appened, the cor
24d10 72 65 63 74 20 62 65 68 61 76 69 6f 75 72 20 77 rect behaviour w
24d20 6f 75 6c 64 20 62 65 20 74 6f 20 72 65 73 74 6f ould be to resto
24d30 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a re the current.*
24d40 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 * content of the
24d50 20 70 61 67 65 2e 20 48 6f 77 65 76 65 72 2c 20 page. However,
24d60 73 69 6e 63 65 20 74 68 69 73 20 63 6f 6e 74 65 since this conte
24d70 6e 74 20 69 73 20 6e 6f 74 20 70 72 65 73 65 6e nt is not presen
24d80 74 20 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 74 t in either.** t
24d90 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
24da0 20 6f 72 20 74 68 65 20 70 6f 72 74 69 6f 6e 20 or the portion
24db0 6f 66 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 of the rollback
24dc0 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 0a 2a 2a 20 journal and .**
24dd0 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c sub-journal roll
24de0 65 64 20 62 61 63 6b 20 74 68 65 20 63 6f 6e 74 ed back the cont
24df0 65 6e 74 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 ent could not be
24e00 20 72 65 73 74 6f 72 65 64 20 61 6e 64 20 74 68 restored and th
24e10 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69 6d e.** database im
24e20 61 67 65 20 77 6f 75 6c 64 20 62 65 63 6f 6d 65 age would become
24e30 20 63 6f 72 72 75 70 74 2e 20 49 74 20 69 73 20 corrupt. It is
24e40 74 68 65 72 65 66 6f 72 65 20 66 6f 72 74 75 6e therefore fortun
24e50 61 74 65 20 74 68 61 74 20 0a 2a 2a 20 74 68 69 ate that .** thi
24e60 73 20 63 69 72 63 75 6d 73 74 61 6e 63 65 20 63 s circumstance c
24e70 61 6e 6e 6f 74 20 61 72 69 73 65 2e 0a 2a 2f 0a annot arise..*/.
24e80 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
24e90 54 45 5f 44 45 42 55 47 29 0a 73 74 61 74 69 63 TE_DEBUG).static
24ea0 20 76 6f 69 64 20 61 73 73 65 72 74 54 72 75 6e void assertTrun
24eb0 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 62 cateConstraintCb
24ec0 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 (PgHdr *pPg){.
24ed0 61 73 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 assert( pPg->fla
24ee0 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 gs&PGHDR_DIRTY )
24ef0 3b 0a 20 20 61 73 73 65 72 74 28 20 21 73 75 62 ;. assert( !sub
24f00 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50 jRequiresPage(pP
24f10 67 29 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3c g) || pPg->pgno<
24f20 3d 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 64 62 =pPg->pPager->db
24f30 53 69 7a 65 20 29 3b 0a 7d 0a 73 74 61 74 69 63 Size );.}.static
24f40 20 76 6f 69 64 20 61 73 73 65 72 74 54 72 75 6e void assertTrun
24f50 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 50 cateConstraint(P
24f60 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
24f70 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 sqlite3PcacheIt
24f80 65 72 61 74 65 44 69 72 74 79 28 70 50 61 67 65 erateDirty(pPage
24f90 72 2d 3e 70 50 43 61 63 68 65 2c 20 61 73 73 65 r->pPCache, asse
24fa0 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 rtTruncateConstr
24fb0 61 69 6e 74 43 62 29 3b 0a 7d 0a 23 65 6c 73 65 aintCb);.}.#else
24fc0 0a 23 20 64 65 66 69 6e 65 20 61 73 73 65 72 74 .# define assert
24fd0 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 TruncateConstrai
24fe0 6e 74 28 70 50 61 67 65 72 29 0a 23 65 6e 64 69 nt(pPager).#endi
24ff0 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 f../*.** Truncat
25000 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 e the in-memory
25010 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6d database file im
25020 61 67 65 20 74 6f 20 6e 50 61 67 65 20 70 61 67 age to nPage pag
25030 65 73 2e 20 54 68 69 73 20 0a 2a 2a 20 66 75 6e es. This .** fun
25040 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 ction does not a
25050 63 74 75 61 6c 6c 79 20 6d 6f 64 69 66 79 20 74 ctually modify t
25060 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
25070 20 6f 6e 20 64 69 73 6b 2e 20 49 74 20 0a 2a 2a on disk. It .**
25080 20 6a 75 73 74 20 73 65 74 73 20 74 68 65 20 69 just sets the i
25090 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 nternal state of
250a0 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 the pager objec
250b0 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 0a 2a t so that the .*
250c0 2a 20 74 72 75 6e 63 61 74 69 6f 6e 20 77 69 6c * truncation wil
250d0 6c 20 62 65 20 64 6f 6e 65 20 77 68 65 6e 20 74 l be done when t
250e0 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 he current trans
250f0 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 action is commit
25100 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ted..**.** This
25110 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 function is only
25120 20 63 61 6c 6c 65 64 20 72 69 67 68 74 20 62 65 called right be
25130 66 6f 72 65 20 63 6f 6d 6d 69 74 74 69 6e 67 20 fore committing
25140 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a a transaction..*
25150 2a 20 4f 6e 63 65 20 74 68 69 73 20 66 75 6e 63 * Once this func
25160 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 20 63 61 tion has been ca
25170 6c 6c 65 64 2c 20 74 68 65 20 74 72 61 6e 73 61 lled, the transa
25180 63 74 69 6f 6e 20 6d 75 73 74 20 65 69 74 68 65 ction must eithe
25190 72 20 62 65 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 r be.** rolled b
251a0 61 63 6b 20 6f 72 20 63 6f 6d 6d 69 74 74 65 64 ack or committed
251b0 2e 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 . It is not safe
251c0 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 66 75 to call this fu
251d0 6e 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 74 68 nction and.** th
251e0 65 6e 20 63 6f 6e 74 69 6e 75 65 20 77 72 69 74 en continue writ
251f0 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 ing to the datab
25200 61 73 65 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c ase..*/.void sql
25210 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 ite3PagerTruncat
25220 65 49 6d 61 67 65 28 50 61 67 65 72 20 2a 70 50 eImage(Pager *pP
25230 61 67 65 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65 ager, Pgno nPage
25240 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 ){. assert( pPa
25250 67 65 72 2d 3e 64 62 53 69 7a 65 3e 3d 6e 50 61 ger->dbSize>=nPa
25260 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ge );. assert(
25270 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d pPager->eState>=
25280 50 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 PAGER_WRITER_CAC
25290 48 45 4d 4f 44 20 29 3b 0a 20 20 70 50 61 67 65 HEMOD );. pPage
252a0 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61 67 r->dbSize = nPag
252b0 65 3b 0a 0a 20 20 2f 2a 20 41 74 20 6f 6e 65 20 e;.. /* At one
252c0 70 6f 69 6e 74 20 74 68 65 20 63 6f 64 65 20 68 point the code h
252d0 65 72 65 20 63 61 6c 6c 65 64 20 61 73 73 65 72 ere called asser
252e0 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 tTruncateConstra
252f0 69 6e 74 28 29 20 74 6f 0a 20 20 2a 2a 20 65 6e int() to. ** en
25300 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20 70 61 sure that all pa
25310 67 65 73 20 62 65 69 6e 67 20 74 72 75 6e 63 61 ges being trunca
25320 74 65 64 20 61 77 61 79 20 62 79 20 74 68 69 73 ted away by this
25330 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 2c 0a operation are,.
25340 20 20 2a 2a 20 69 66 20 6f 6e 65 20 6f 72 20 6d ** if one or m
25350 6f 72 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 ore savepoints a
25360 72 65 20 6f 70 65 6e 2c 20 70 72 65 73 65 6e 74 re open, present
25370 20 69 6e 20 74 68 65 20 73 61 76 65 70 6f 69 6e in the savepoin
25380 74 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 t . ** journal
25390 73 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 6e so that they can
253a0 20 62 65 20 72 65 73 74 6f 72 65 64 20 69 66 20 be restored if
253b0 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 69 73 the savepoint is
253c0 20 72 6f 6c 6c 65 64 0a 20 20 2a 2a 20 62 61 63 rolled. ** bac
253d0 6b 2e 20 54 68 69 73 20 69 73 20 6e 6f 20 6c 6f k. This is no lo
253e0 6e 67 65 72 20 6e 65 63 65 73 73 61 72 79 20 61 nger necessary a
253f0 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 s this function
25400 69 73 20 6e 6f 77 20 6f 6e 6c 79 0a 20 20 2a 2a is now only. **
25410 20 63 61 6c 6c 65 64 20 72 69 67 68 74 20 62 65 called right be
25420 66 6f 72 65 20 63 6f 6d 6d 69 74 74 69 6e 67 20 fore committing
25430 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 53 a transaction. S
25440 6f 20 61 6c 74 68 6f 75 67 68 20 74 68 65 20 0a o although the .
25450 20 20 2a 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 ** Pager objec
25460 74 20 6d 61 79 20 73 74 69 6c 6c 20 68 61 76 65 t may still have
25470 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 open savepoints
25480 20 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 (Pager.nSavepoi
25490 6e 74 21 3d 30 29 2c 20 0a 20 20 2a 2a 20 74 68 nt!=0), . ** th
254a0 65 79 20 63 61 6e 6e 6f 74 20 62 65 20 72 6f 6c ey cannot be rol
254b0 6c 65 64 20 62 61 63 6b 2e 20 53 6f 20 74 68 65 led back. So the
254c0 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 assertTruncateC
254d0 6f 6e 73 74 72 61 69 6e 74 28 29 20 63 61 6c 6c onstraint() call
254e0 0a 20 20 2a 2a 20 69 73 20 6e 6f 20 6c 6f 6e 67 . ** is no long
254f0 65 72 20 63 6f 72 72 65 63 74 2e 20 2a 2f 0a 7d er correct. */.}
25500 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 .../*.** This fu
25510 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
25520 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 69 before attempti
25530 6e 67 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c ng a hot-journal
25540 20 72 6f 6c 6c 62 61 63 6b 2e 20 49 74 0a 2a 2a rollback. It.**
25550 20 73 79 6e 63 73 20 74 68 65 20 6a 6f 75 72 6e syncs the journ
25560 61 6c 20 66 69 6c 65 20 74 6f 20 64 69 73 6b 2c al file to disk,
25570 20 74 68 65 6e 20 73 65 74 73 20 70 50 61 67 65 then sets pPage
25580 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 74 6f r->journalHdr to
25590 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 the.** size of
255a0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
255b0 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 61 67 so that the pag
255c0 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 72 6f er_playback() ro
255d0 75 74 69 6e 65 20 6b 6e 6f 77 73 0a 2a 2a 20 74 utine knows.** t
255e0 68 61 74 20 74 68 65 20 65 6e 74 69 72 65 20 6a hat the entire j
255f0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 ournal file has
25600 62 65 65 6e 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a been synced..**.
25610 2a 2a 20 53 79 6e 63 69 6e 67 20 61 20 68 6f 74 ** Syncing a hot
25620 2d 6a 6f 75 72 6e 61 6c 20 74 6f 20 64 69 73 6b -journal to disk
25630 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 69 before attempti
25640 6e 67 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61 ng to roll it ba
25650 63 6b 20 65 6e 73 75 72 65 73 20 0a 2a 2a 20 74 ck ensures .** t
25660 68 61 74 20 69 66 20 61 20 70 6f 77 65 72 2d 66 hat if a power-f
25670 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 64 75 ailure occurs du
25680 72 69 6e 67 20 74 68 65 20 72 6f 6c 6c 62 61 63 ring the rollbac
25690 6b 2c 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 k, the process t
256a0 68 61 74 0a 2a 2a 20 61 74 74 65 6d 70 74 73 20 hat.** attempts
256b0 72 6f 6c 6c 62 61 63 6b 20 66 6f 6c 6c 6f 77 69 rollback followi
256c0 6e 67 20 73 79 73 74 65 6d 20 72 65 63 6f 76 65 ng system recove
256d0 72 79 20 73 65 65 73 20 74 68 65 20 73 61 6d 65 ry sees the same
256e0 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 63 6f 6e 74 journal.** cont
256f0 65 6e 74 20 61 73 20 74 68 69 73 20 70 72 6f 63 ent as this proc
25700 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 76 ess..**.** If ev
25710 65 72 79 74 68 69 6e 67 20 67 6f 65 73 20 61 73 erything goes as
25720 20 70 6c 61 6e 6e 65 64 2c 20 53 51 4c 49 54 45 planned, SQLITE
25730 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e _OK is returned.
25740 20 4f 74 68 65 72 77 69 73 65 2c 20 0a 2a 2a 20 Otherwise, .**
25750 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 an SQLite error
25760 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 code..*/.static
25770 69 6e 74 20 70 61 67 65 72 53 79 6e 63 48 6f 74 int pagerSyncHot
25780 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 Journal(Pager *p
25790 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 Pager){. int rc
257a0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
257b0 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 if( !pPager->noS
257c0 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ync ){. rc =
257d0 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 sqlite3OsSync(pP
257e0 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c 49 54 ager->jfd, SQLIT
257f0 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 3b 0a E_SYNC_NORMAL);.
25800 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
25810 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
25820 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c c = sqlite3OsFil
25830 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 eSize(pPager->jf
25840 64 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 d, &pPager->jour
25850 6e 61 6c 48 64 72 29 3b 0a 20 20 7d 0a 20 20 72 nalHdr);. }. r
25860 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 6e eturn rc;.}..#in
25870 63 6c 75 64 65 20 3c 73 79 73 2f 6d 6d 61 6e 2e clude <sys/mman.
25880 68 3e 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6d 61 70 20 h>../*.** Unmap
25890 61 6e 79 20 6d 61 70 70 69 6e 67 20 6f 66 20 74 any mapping of t
258a0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
258b0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
258c0 70 61 67 65 72 55 6e 6d 61 70 28 50 61 67 65 72 pagerUnmap(Pager
258d0 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 *pPager){. if(
258e0 20 70 50 61 67 65 72 2d 3e 70 4d 61 70 20 29 7b pPager->pMap ){
258f0 0a 20 20 20 20 6d 75 6e 6d 61 70 28 70 50 61 67 . munmap(pPag
25900 65 72 2d 3e 70 4d 61 70 2c 20 70 50 61 67 65 72 er->pMap, pPager
25910 2d 3e 6e 4d 61 70 29 3b 0a 20 20 20 20 70 50 61 ->nMap);. pPa
25920 67 65 72 2d 3e 70 4d 61 70 20 3d 20 30 3b 0a 20 ger->pMap = 0;.
25930 20 20 20 70 50 61 67 65 72 2d 3e 6e 4d 61 70 20 pPager->nMap
25940 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 0;. }. retur
25950 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
25960 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
25970 4d 61 70 28 50 61 67 65 72 20 2a 70 50 61 67 65 Map(Pager *pPage
25980 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 r){. int rc;.
25990 69 36 34 20 73 7a 20 3d 20 30 3b 0a 0a 20 20 61 i64 sz = 0;.. a
259a0 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 ssert( pPager->p
259b0 4d 61 70 3d 3d 30 20 26 26 20 70 50 61 67 65 72 Map==0 && pPager
259c0 2d 3e 6e 4d 61 70 3d 3d 30 20 29 3b 0a 0a 20 20 ->nMap==0 );..
259d0 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 rc = sqlite3OsFi
259e0 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 leSize(pPager->f
259f0 64 2c 20 26 73 7a 29 3b 0a 20 20 69 66 28 20 72 d, &sz);. if( r
25a00 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
25a10 73 7a 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 sz>0 ){. int
25a20 66 64 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c fd;. rc = sql
25a30 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f ite3OsFileContro
25a40 6c 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 53 51 l(pPager->fd, SQ
25a50 4c 49 54 45 5f 46 43 4e 54 4c 5f 47 45 54 46 44 LITE_FCNTL_GETFD
25a60 2c 20 28 76 6f 69 64 20 2a 29 26 66 64 29 3b 0a , (void *)&fd);.
25a70 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
25a80 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 76 TE_OK ){. v
25a90 6f 69 64 20 2a 70 4d 61 70 20 3d 20 6d 6d 61 70 oid *pMap = mmap
25aa0 28 30 2c 20 73 7a 2c 20 50 52 4f 54 5f 52 45 41 (0, sz, PROT_REA
25ab0 44 2c 20 4d 41 50 5f 53 48 41 52 45 44 2c 20 66 D, MAP_SHARED, f
25ac0 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 d, 0);. if(
25ad0 20 70 4d 61 70 3d 3d 4d 41 50 5f 46 41 49 4c 45 pMap==MAP_FAILE
25ae0 44 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 D ){. asser
25af0 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 20 20 t( 0 );.
25b00 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
25b10 45 52 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ERR;. }.
25b20 20 20 20 70 50 61 67 65 72 2d 3e 70 4d 61 70 20 pPager->pMap
25b30 3d 20 70 4d 61 70 3b 0a 20 20 20 20 20 20 70 50 = pMap;. pP
25b40 61 67 65 72 2d 3e 6e 4d 61 70 20 3d 20 73 7a 3b ager->nMap = sz;
25b50 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 . }. }.. re
25b60 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 turn rc;.}..stat
25b70 69 63 20 69 6e 74 20 70 61 67 65 72 41 63 71 75 ic int pagerAcqu
25b80 69 72 65 4d 61 70 50 61 67 65 28 50 61 67 65 72 ireMapPage(Pager
25b90 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 *pPager, Pgno p
25ba0 67 6e 6f 2c 20 50 67 48 64 72 20 2a 2a 70 70 50 gno, PgHdr **ppP
25bb0 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a age){. int rc;.
25bc0 20 20 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a 0a *ppPage = 0;..
25bd0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
25be0 2d 3e 70 57 61 6c 3d 3d 30 20 29 3b 0a 0a 20 20 ->pWal==0 );..
25bf0 69 66 28 20 4d 45 4d 44 42 3d 3d 30 20 26 26 20 if( MEMDB==0 &&
25c00 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
25c10 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 ==0 ){. if( p
25c20 50 61 67 65 72 2d 3e 70 4d 61 70 3d 3d 30 20 29 Pager->pMap==0 )
25c30 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 {. rc = pag
25c40 65 72 4d 61 70 28 70 50 61 67 65 72 29 3b 0a 20 erMap(pPager);.
25c50 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
25c60 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 ITE_OK ) return
25c70 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 rc;. }.. i
25c80 66 28 20 70 67 6e 6f 21 3d 31 20 26 26 20 70 50 f( pgno!=1 && pP
25c90 61 67 65 72 2d 3e 70 4d 61 70 20 26 26 20 70 50 ager->pMap && pP
25ca0 61 67 65 72 2d 3e 6e 4d 61 70 3e 3d 28 28 69 36 ager->nMap>=((i6
25cb0 34 29 70 67 6e 6f 2a 70 50 61 67 65 72 2d 3e 70 4)pgno*pPager->p
25cc0 61 67 65 53 69 7a 65 29 20 29 7b 0a 20 20 20 20 ageSize) ){.
25cd0 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 PgHdr *p;.
25ce0 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 46 if( pPager->pF
25cf0 72 65 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ree ){. p
25d00 20 3d 20 70 50 61 67 65 72 2d 3e 70 46 72 65 65 = pPager->pFree
25d10 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 ;. pPager
25d20 2d 3e 70 46 72 65 65 20 3d 20 70 2d 3e 70 44 69 ->pFree = p->pDi
25d30 72 74 79 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e rty;. p->
25d40 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 pDirty = 0;.
25d50 20 20 20 20 6d 65 6d 73 65 74 28 70 2d 3e 70 45 memset(p->pE
25d60 78 74 72 61 2c 20 30 2c 20 70 50 61 67 65 72 2d xtra, 0, pPager-
25d70 3e 6e 45 78 74 72 61 29 3b 0a 20 20 20 20 20 20 >nExtra);.
25d80 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
25d90 20 3d 20 28 50 67 48 64 72 20 2a 29 73 71 6c 69 = (PgHdr *)sqli
25da0 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 te3MallocZero(si
25db0 7a 65 6f 66 28 50 67 48 64 72 29 20 2b 20 70 50 zeof(PgHdr) + pP
25dc0 61 67 65 72 2d 3e 6e 45 78 74 72 61 29 3b 0a 20 ager->nExtra);.
25dd0 20 20 20 20 20 20 20 69 66 28 20 70 3d 3d 30 20 if( p==0
25de0 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
25df0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 70 NOMEM;. p
25e00 2d 3e 70 45 78 74 72 61 20 3d 20 28 76 6f 69 64 ->pExtra = (void
25e10 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 20 20 *)&p[1];.
25e20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 50 47 48 p->flags = PGH
25e30 44 52 5f 4d 4d 41 50 3b 0a 20 20 20 20 20 20 20 DR_MMAP;.
25e40 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 p->nRef = 1;.
25e50 20 20 20 20 20 20 70 2d 3e 70 50 61 67 65 72 20 p->pPager
25e60 3d 20 70 50 61 67 65 72 3b 0a 20 20 20 20 20 20 = pPager;.
25e70 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 }.. assert(
25e80 20 70 2d 3e 70 45 78 74 72 61 3d 3d 28 76 6f 69 p->pExtra==(voi
25e90 64 20 2a 29 26 70 5b 31 5d 20 29 3b 0a 20 20 20 d *)&p[1] );.
25ea0 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 assert( p->pP
25eb0 61 67 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 age==0 );.
25ec0 61 73 73 65 72 74 28 20 70 2d 3e 66 6c 61 67 73 assert( p->flags
25ed0 3d 3d 50 47 48 44 52 5f 4d 4d 41 50 20 29 3b 0a ==PGHDR_MMAP );.
25ee0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d assert( p-
25ef0 3e 70 50 61 67 65 72 3d 3d 70 50 61 67 65 72 20 >pPager==pPager
25f00 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
25f10 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 0a p->nRef==1 );..
25f20 20 20 20 20 20 20 70 2d 3e 70 44 61 74 61 20 3d p->pData =
25f30 20 26 28 28 75 38 20 2a 29 70 50 61 67 65 72 2d &((u8 *)pPager-
25f40 3e 70 4d 61 70 29 5b 28 69 36 34 29 28 70 67 6e >pMap)[(i64)(pgn
25f50 6f 2d 31 29 20 2a 20 70 50 61 67 65 72 2d 3e 70 o-1) * pPager->p
25f60 61 67 65 53 69 7a 65 5d 3b 0a 20 20 20 20 20 20 ageSize];.
25f70 70 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a p->pgno = pgno;.
25f80 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 4d pPager->nM
25f90 6d 61 70 4f 75 74 2b 2b 3b 0a 20 20 20 20 20 20 mapOut++;.
25fa0 2a 70 70 50 61 67 65 20 3d 20 70 3b 0a 20 20 20 *ppPage = p;.
25fb0 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e }. }.. return
25fc0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 SQLITE_OK;.}..s
25fd0 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 tatic void pager
25fe0 52 65 6c 65 61 73 65 4d 61 70 50 61 67 65 28 50 ReleaseMapPage(P
25ff0 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 gHdr *pPg){. Pa
26000 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 ger *pPager = pP
26010 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 70 50 61 g->pPager;. pPa
26020 67 65 72 2d 3e 6e 4d 6d 61 70 4f 75 74 2d 2d 3b ger->nMmapOut--;
26030 0a 20 20 70 50 67 2d 3e 70 44 69 72 74 79 20 3d . pPg->pDirty =
26040 20 70 50 61 67 65 72 2d 3e 70 46 72 65 65 3b 0a pPager->pFree;.
26050 20 20 70 50 61 67 65 72 2d 3e 70 46 72 65 65 20 pPager->pFree
26060 3d 20 70 50 67 3b 0a 7d 0a 0a 73 74 61 74 69 63 = pPg;.}..static
26070 20 76 6f 69 64 20 70 61 67 65 72 46 72 65 65 4d void pagerFreeM
26080 61 70 48 64 72 73 28 50 61 67 65 72 20 2a 70 50 apHdrs(Pager *pP
26090 61 67 65 72 29 7b 0a 20 20 50 67 48 64 72 20 2a ager){. PgHdr *
260a0 70 3b 0a 20 20 50 67 48 64 72 20 2a 70 4e 65 78 p;. PgHdr *pNex
260b0 74 3b 0a 20 20 66 6f 72 28 70 3d 70 50 61 67 65 t;. for(p=pPage
260c0 72 2d 3e 70 46 72 65 65 3b 20 70 3b 20 70 3d 70 r->pFree; p; p=p
260d0 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e 65 78 74 Next){. pNext
260e0 20 3d 20 70 2d 3e 70 44 69 72 74 79 3b 0a 20 20 = p->pDirty;.
260f0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
26100 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a );. }.}.../*.**
26110 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61 Shutdown the pa
26120 67 65 20 63 61 63 68 65 2e 20 20 46 72 65 65 20 ge cache. Free
26130 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6e 64 20 63 all memory and c
26140 6c 6f 73 65 20 61 6c 6c 20 66 69 6c 65 73 2e 0a lose all files..
26150 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72 61 6e 73 **.** If a trans
26160 61 63 74 69 6f 6e 20 77 61 73 20 69 6e 20 70 72 action was in pr
26170 6f 67 72 65 73 73 20 77 68 65 6e 20 74 68 69 73 ogress when this
26180 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
26190 65 64 2c 20 74 68 61 74 0a 2a 2a 20 74 72 61 6e ed, that.** tran
261a0 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 saction is rolle
261b0 64 20 62 61 63 6b 2e 20 20 41 6c 6c 20 6f 75 74 d back. All out
261c0 73 74 61 6e 64 69 6e 67 20 70 61 67 65 73 20 61 standing pages a
261d0 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 0a 2a re invalidated.*
261e0 2a 20 61 6e 64 20 74 68 65 69 72 20 6d 65 6d 6f * and their memo
261f0 72 79 20 69 73 20 66 72 65 65 64 2e 20 20 41 6e ry is freed. An
26200 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 y attempt to use
26210 20 61 20 70 61 67 65 20 61 73 73 6f 63 69 61 74 a page associat
26220 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 69 73 20 ed.** with this
26230 70 61 67 65 20 63 61 63 68 65 20 61 66 74 65 72 page cache after
26240 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
26250 65 74 75 72 6e 73 20 77 69 6c 6c 20 6c 69 6b 65 eturns will like
26260 6c 79 0a 2a 2a 20 72 65 73 75 6c 74 20 69 6e 20 ly.** result in
26270 61 20 63 6f 72 65 64 75 6d 70 2e 0a 2a 2a 0a 2a a coredump..**.*
26280 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
26290 61 6c 77 61 79 73 20 73 75 63 63 65 65 64 73 2e always succeeds.
262a0 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f If a transactio
262b0 6e 20 69 73 20 61 63 74 69 76 65 20 61 6e 20 61 n is active an a
262c0 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d 61 64 ttempt.** is mad
262d0 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61 63 e to roll it bac
262e0 6b 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f k. If an error o
262f0 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 65 ccurs during the
26300 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a 2a 20 61 20 rollback .** a
26310 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 20 hot journal may
26320 62 65 20 6c 65 66 74 20 69 6e 20 74 68 65 20 66 be left in the f
26330 69 6c 65 73 79 73 74 65 6d 20 62 75 74 20 6e 6f ilesystem but no
26340 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e error is return
26350 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c ed.** to the cal
26360 6c 65 72 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 ler..*/.int sqli
26370 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61 te3PagerClose(Pa
26380 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
26390 75 38 20 2a 70 54 6d 70 20 3d 20 28 75 38 20 2a u8 *pTmp = (u8 *
263a0 29 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 )pPager->pTmpSpa
263b0 63 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 ce;.. assert( a
263c0 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 ssert_pager_stat
263d0 65 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 64 e(pPager) );. d
263e0 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 isable_simulated
263f0 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 _io_errors();.
26400 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 sqlite3BeginBeni
26410 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 70 61 gnMalloc();. pa
26420 67 65 72 55 6e 6d 61 70 28 70 50 61 67 65 72 29 gerUnmap(pPager)
26430 3b 0a 20 20 70 61 67 65 72 46 72 65 65 4d 61 70 ;. pagerFreeMap
26440 48 64 72 73 28 70 50 61 67 65 72 29 3b 0a 20 20 Hdrs(pPager);.
26450 2f 2a 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f /* pPager->errCo
26460 64 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 de = 0; */. pPa
26470 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f ger->exclusiveMo
26480 64 65 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 de = 0;.#ifndef
26490 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a SQLITE_OMIT_WAL.
264a0 20 20 73 71 6c 69 74 65 33 57 61 6c 43 6c 6f 73 sqlite3WalClos
264b0 65 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 e(pPager->pWal,
264c0 70 50 61 67 65 72 2d 3e 63 6b 70 74 53 79 6e 63 pPager->ckptSync
264d0 46 6c 61 67 73 2c 20 70 50 61 67 65 72 2d 3e 70 Flags, pPager->p
264e0 61 67 65 53 69 7a 65 2c 20 70 54 6d 70 29 3b 0a ageSize, pTmp);.
264f0 20 20 70 50 61 67 65 72 2d 3e 70 57 61 6c 20 3d pPager->pWal =
26500 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 70 61 67 0;.#endif. pag
26510 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 er_reset(pPager)
26520 3b 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b ;. if( MEMDB ){
26530 0a 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 . pager_unloc
26540 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 65 6c k(pPager);. }el
26550 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 69 74 se{. /* If it
26560 20 69 73 20 6f 70 65 6e 2c 20 73 79 6e 63 20 74 is open, sync t
26570 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
26580 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 55 before calling U
26590 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b nlockAndRollback
265a0 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 .. ** If this
265b0 20 69 73 20 6e 6f 74 20 64 6f 6e 65 2c 20 74 68 is not done, th
265c0 65 6e 20 61 6e 20 75 6e 73 79 6e 63 65 64 20 70 en an unsynced p
265d0 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 ortion of the op
265e0 65 6e 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 20 20 en journal .
265f0 2a 2a 20 66 69 6c 65 20 6d 61 79 20 62 65 20 70 ** file may be p
26600 6c 61 79 65 64 20 62 61 63 6b 20 69 6e 74 6f 20 layed back into
26610 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 66 the database. If
26620 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 a power failure
26630 20 6f 63 63 75 72 73 20 0a 20 20 20 20 2a 2a 20 occurs . **
26640 77 68 69 6c 65 20 74 68 69 73 20 69 73 20 68 61 while this is ha
26650 70 70 65 6e 69 6e 67 2c 20 74 68 65 20 64 61 74 ppening, the dat
26660 61 62 61 73 65 20 63 6f 75 6c 64 20 62 65 63 6f abase could beco
26670 6d 65 20 63 6f 72 72 75 70 74 2e 0a 20 20 20 20 me corrupt..
26680 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 61 6e 20 **. ** If an
26690 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 error occurs whi
266a0 6c 65 20 74 72 79 69 6e 67 20 74 6f 20 73 79 6e le trying to syn
266b0 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 73 c the journal, s
266c0 68 69 66 74 20 74 68 65 20 70 61 67 65 72 0a 20 hift the pager.
266d0 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 45 ** into the E
266e0 52 52 4f 52 20 73 74 61 74 65 2e 20 54 68 69 73 RROR state. This
266f0 20 63 61 75 73 65 73 20 55 6e 6c 6f 63 6b 41 6e causes UnlockAn
26700 64 52 6f 6c 6c 62 61 63 6b 20 74 6f 20 75 6e 6c dRollback to unl
26710 6f 63 6b 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 ock the. ** d
26720 61 74 61 62 61 73 65 20 61 6e 64 20 63 6c 6f 73 atabase and clos
26730 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 e the journal fi
26740 6c 65 20 77 69 74 68 6f 75 74 20 61 74 74 65 6d le without attem
26750 70 74 69 6e 67 20 74 6f 20 72 6f 6c 6c 20 69 74 pting to roll it
26760 0a 20 20 20 20 2a 2a 20 62 61 63 6b 20 6f 72 20 . ** back or
26770 66 69 6e 61 6c 69 7a 65 20 69 74 2e 20 54 68 65 finalize it. The
26780 20 6e 65 78 74 20 64 61 74 61 62 61 73 65 20 75 next database u
26790 73 65 72 20 77 69 6c 6c 20 68 61 76 65 20 74 6f ser will have to
267a0 20 64 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a do hot-journal.
267b0 20 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 ** rollback
267c0 62 65 66 6f 72 65 20 61 63 63 65 73 73 69 6e 67 before accessing
267d0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
267e0 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 le.. */. i
267f0 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 f( isOpen(pPager
26800 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 ->jfd) ){.
26810 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 pager_error(pPag
26820 65 72 2c 20 70 61 67 65 72 53 79 6e 63 48 6f 74 er, pagerSyncHot
26830 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 29 Journal(pPager))
26840 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 67 65 ;. }. page
26850 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 rUnlockAndRollba
26860 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a ck(pPager);. }.
26870 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 sqlite3EndBeni
26880 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 65 6e gnMalloc();. en
26890 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 able_simulated_i
268a0 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 50 41 o_errors();. PA
268b0 47 45 52 54 52 41 43 45 28 28 22 43 4c 4f 53 45 GERTRACE(("CLOSE
268c0 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 %d\n", PAGERID(
268d0 70 50 61 67 65 72 29 29 29 3b 0a 20 20 49 4f 54 pPager)));. IOT
268e0 52 41 43 45 28 28 22 43 4c 4f 53 45 20 25 70 5c RACE(("CLOSE %p\
268f0 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 73 n", pPager)). s
26900 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 qlite3OsClose(pP
26910 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 73 71 ager->jfd);. sq
26920 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 lite3OsClose(pPa
26930 67 65 72 2d 3e 66 64 29 3b 0a 20 20 73 71 6c 69 ger->fd);. sqli
26940 74 65 33 50 61 67 65 46 72 65 65 28 70 54 6d 70 te3PageFree(pTmp
26950 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 );. sqlite3Pcac
26960 68 65 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e heClose(pPager->
26970 70 50 43 61 63 68 65 29 3b 0a 0a 23 69 66 64 65 pPCache);..#ifde
26980 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 f SQLITE_HAS_COD
26990 45 43 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d EC. if( pPager-
269a0 3e 78 43 6f 64 65 63 46 72 65 65 20 29 20 70 50 >xCodecFree ) pP
269b0 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 ager->xCodecFree
269c0 28 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 29 (pPager->pCodec)
269d0 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 ;.#endif.. asse
269e0 72 74 28 20 21 70 50 61 67 65 72 2d 3e 61 53 61 rt( !pPager->aSa
269f0 76 65 70 6f 69 6e 74 20 26 26 20 21 70 50 61 67 vepoint && !pPag
26a00 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 29 er->pInJournal )
26a10 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69 73 4f ;. assert( !isO
26a20 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
26a30 20 26 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 && !isOpen(pPag
26a40 65 72 2d 3e 73 6a 66 64 29 20 29 3b 0a 0a 20 20 er->sjfd) );..
26a50 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 sqlite3_free(pPa
26a60 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 ger);. return S
26a70 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 QLITE_OK;.}..#if
26a80 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
26a90 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c ) || defined(SQL
26aa0 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 ITE_TEST)./*.**
26ab0 52 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 Return the page
26ac0 6e 75 6d 62 65 72 20 66 6f 72 20 70 61 67 65 20 number for page
26ad0 70 50 67 2e 0a 2a 2f 0a 50 67 6e 6f 20 73 71 6c pPg..*/.Pgno sql
26ae0 69 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d ite3PagerPagenum
26af0 62 65 72 28 44 62 50 61 67 65 20 2a 70 50 67 29 ber(DbPage *pPg)
26b00 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e {. return pPg->
26b10 70 67 6e 6f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a pgno;.}.#endif..
26b20 2f 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 /*.** Increment
26b30 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f the reference co
26b40 75 6e 74 20 66 6f 72 20 70 61 67 65 20 70 50 67 unt for page pPg
26b50 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
26b60 33 50 61 67 65 72 52 65 66 28 44 62 50 61 67 65 3PagerRef(DbPage
26b70 20 2a 70 50 67 29 7b 0a 20 20 73 71 6c 69 74 65 *pPg){. sqlite
26b80 33 50 63 61 63 68 65 52 65 66 28 70 50 67 29 3b 3PcacheRef(pPg);
26b90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 .}../*.** Sync t
26ba0 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 49 6e 20 6f he journal. In o
26bb0 74 68 65 72 20 77 6f 72 64 73 2c 20 6d 61 6b 65 ther words, make
26bc0 20 73 75 72 65 20 61 6c 6c 20 74 68 65 20 70 61 sure all the pa
26bd0 67 65 73 20 74 68 61 74 20 68 61 76 65 0a 2a 2a ges that have.**
26be0 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f been written to
26bf0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 61 76 the journal hav
26c00 65 20 61 63 74 75 61 6c 6c 79 20 72 65 61 63 68 e actually reach
26c10 65 64 20 74 68 65 20 73 75 72 66 61 63 65 20 6f ed the surface o
26c20 66 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20 61 6e f the.** disk an
26c30 64 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 d can be restore
26c40 64 20 69 6e 20 74 68 65 20 65 76 65 6e 74 20 6f d in the event o
26c50 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 f a hot-journal
26c60 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 rollback..**.**
26c70 49 66 20 74 68 65 20 50 61 67 65 72 2e 6e 6f 53 If the Pager.noS
26c80 79 6e 63 20 66 6c 61 67 20 69 73 20 73 65 74 2c ync flag is set,
26c90 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 then this funct
26ca0 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ion is a no-op..
26cb0 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 ** Otherwise, th
26cc0 65 20 61 63 74 69 6f 6e 73 20 72 65 71 75 69 72 e actions requir
26cd0 65 64 20 64 65 70 65 6e 64 20 6f 6e 20 74 68 65 ed depend on the
26ce0 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 61 6e journal-mode an
26cf0 64 20 74 68 65 20 0a 2a 2a 20 64 65 76 69 63 65 d the .** device
26d00 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 characteristics
26d10 20 6f 66 20 74 68 65 20 66 69 6c 65 2d 73 79 73 of the file-sys
26d20 74 65 6d 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a tem, as follows:
26d30 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 .**.** * If th
26d40 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
26d50 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a s an in-memory j
26d60 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6e 6f 20 ournal file, no
26d70 61 63 74 69 6f 6e 20 6e 65 65 64 0a 2a 2a 20 20 action need.**
26d80 20 20 20 62 65 20 74 61 6b 65 6e 2e 0a 2a 2a 0a be taken..**.
26d90 2a 2a 20 20 20 2a 20 4f 74 68 65 72 77 69 73 65 ** * Otherwise
26da0 2c 20 69 66 20 74 68 65 20 64 65 76 69 63 65 20 , if the device
26db0 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 does not support
26dc0 20 74 68 65 20 53 41 46 45 5f 41 50 50 45 4e 44 the SAFE_APPEND
26dd0 20 70 72 6f 70 65 72 74 79 2c 0a 2a 2a 20 20 20 property,.**
26de0 20 20 74 68 65 6e 20 74 68 65 20 6e 52 65 63 20 then the nRec
26df0 66 69 65 6c 64 20 6f 66 20 74 68 65 20 6d 6f 73 field of the mos
26e00 74 20 72 65 63 65 6e 74 6c 79 20 77 72 69 74 74 t recently writt
26e10 65 6e 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 en journal heade
26e20 72 0a 2a 2a 20 20 20 20 20 69 73 20 75 70 64 61 r.** is upda
26e30 74 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 ted to contain t
26e40 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6a 6f 75 he number of jou
26e50 72 6e 61 6c 20 72 65 63 6f 72 64 73 20 74 68 61 rnal records tha
26e60 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 62 65 t have.** be
26e70 65 6e 20 77 72 69 74 74 65 6e 20 66 6f 6c 6c 6f en written follo
26e80 77 69 6e 67 20 69 74 2e 20 49 66 20 74 68 65 20 wing it. If the
26e90 70 61 67 65 72 20 69 73 20 6f 70 65 72 61 74 69 pager is operati
26ea0 6e 67 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 0a ng in full-sync.
26eb0 2a 2a 20 20 20 20 20 6d 6f 64 65 2c 20 74 68 65 ** mode, the
26ec0 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
26ed0 6c 65 20 69 73 20 73 79 6e 63 65 64 20 62 65 66 le is synced bef
26ee0 6f 72 65 20 74 68 69 73 20 66 69 65 6c 64 20 69 ore this field i
26ef0 73 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a s updated..**.**
26f00 20 20 20 2a 20 49 66 20 74 68 65 20 64 65 76 69 * If the devi
26f10 63 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 ce does not supp
26f20 6f 72 74 20 74 68 65 20 53 45 51 55 45 4e 54 49 ort the SEQUENTI
26f30 41 4c 20 70 72 6f 70 65 72 74 79 2c 20 74 68 65 AL property, the
26f40 6e 20 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 n .** journa
26f50 6c 20 66 69 6c 65 20 69 73 20 73 79 6e 63 65 64 l file is synced
26f60 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 6e 20 70 ..**.** Or, in p
26f70 73 65 75 64 6f 2d 63 6f 64 65 3a 0a 2a 2a 0a 2a seudo-code:.**.*
26f80 2a 20 20 20 69 66 28 20 4e 4f 54 20 3c 69 6e 2d * if( NOT <in-
26f90 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 3e 20 memory journal>
26fa0 29 7b 0a 2a 2a 20 20 20 20 20 69 66 28 20 4e 4f ){.** if( NO
26fb0 54 20 53 41 46 45 5f 41 50 50 45 4e 44 20 29 7b T SAFE_APPEND ){
26fc0 0a 2a 2a 20 20 20 20 20 20 20 69 66 28 20 3c 66 .** if( <f
26fd0 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 3e 20 29 ull-sync mode> )
26fe0 20 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 xSync(<journal
26ff0 66 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 20 20 20 file>);.**
27000 20 3c 75 70 64 61 74 65 20 6e 52 65 63 20 66 69 <update nRec fi
27010 65 6c 64 3e 0a 2a 2a 20 20 20 20 20 7d 20 0a 2a eld>.** } .*
27020 2a 20 20 20 20 20 69 66 28 20 4e 4f 54 20 53 45 * if( NOT SE
27030 51 55 45 4e 54 49 41 4c 20 29 20 78 53 79 6e 63 QUENTIAL ) xSync
27040 28 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 3e 29 (<journal file>)
27050 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 49 ;.** }.**.** I
27060 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 f successful, th
27070 69 73 20 72 6f 75 74 69 6e 65 20 63 6c 65 61 72 is routine clear
27080 73 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 s the PGHDR_NEED
27090 5f 53 59 4e 43 20 66 6c 61 67 20 6f 66 20 65 76 _SYNC flag of ev
270a0 65 72 79 20 0a 2a 2a 20 70 61 67 65 20 63 75 72 ery .** page cur
270b0 72 65 6e 74 6c 79 20 68 65 6c 64 20 69 6e 20 6d rently held in m
270c0 65 6d 6f 72 79 20 62 65 66 6f 72 65 20 72 65 74 emory before ret
270d0 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b urning SQLITE_OK
270e0 2e 20 49 66 20 61 6e 20 49 4f 0a 2a 2a 20 65 72 . If an IO.** er
270f0 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 ror is encounter
27100 65 64 2c 20 74 68 65 6e 20 74 68 65 20 49 4f 20 ed, then the IO
27110 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
27120 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 turned to the ca
27130 6c 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ller..*/.static
27140 69 6e 74 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 int syncJournal(
27150 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
27160 6e 74 20 6e 65 77 48 64 72 29 7b 0a 20 20 69 6e nt newHdr){. in
27170 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
27180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
27190 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
271a0 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
271b0 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 r->eState==PAGER
271c0 5f 57 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 _WRITER_CACHEMOD
271d0 0a 20 20 20 20 20 20 20 7c 7c 20 70 50 61 67 65 . || pPage
271e0 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 r->eState==PAGER
271f0 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44 0a 20 20 _WRITER_DBMOD.
27200 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 73 73 );. assert( ass
27210 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 ert_pager_state(
27220 70 50 61 67 65 72 29 20 29 3b 0a 20 20 61 73 73 pPager) );. ass
27230 65 72 74 28 20 21 70 61 67 65 72 55 73 65 57 61 ert( !pagerUseWa
27240 6c 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 l(pPager) );..
27250 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
27260 72 45 78 63 6c 75 73 69 76 65 4c 6f 63 6b 28 70 rExclusiveLock(p
27270 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 72 63 Pager);. if( rc
27280 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 !=SQLITE_OK ) re
27290 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69 66 28 20 turn rc;.. if(
272a0 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 !pPager->noSync
272b0 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 ){. assert( !
272c0 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
272d0 20 29 3b 0a 20 20 20 20 69 66 28 20 69 73 4f 70 );. if( isOp
272e0 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
272f0 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e && pPager->journ
27300 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f alMode!=PAGER_JO
27310 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 URNALMODE_MEMORY
27320 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 ){. const
27330 69 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 int iDc = sqlite
27340 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 3OsDeviceCharact
27350 65 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d eristics(pPager-
27360 3e 66 64 29 3b 0a 20 20 20 20 20 20 61 73 73 65 >fd);. asse
27370 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 rt( isOpen(pPage
27380 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 20 20 r->jfd) );..
27390 20 20 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 if( 0==(iDc&SQ
273a0 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f LITE_IOCAP_SAFE_
273b0 41 50 50 45 4e 44 29 20 29 7b 0a 20 20 20 20 20 APPEND) ){.
273c0 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b /* This block
273d0 20 64 65 61 6c 73 20 77 69 74 68 20 61 6e 20 6f deals with an o
273e0 62 73 63 75 72 65 20 70 72 6f 62 6c 65 6d 2e 20 bscure problem.
273f0 49 66 20 74 68 65 20 6c 61 73 74 20 63 6f 6e 6e If the last conn
27400 65 63 74 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a ection. *
27410 2a 20 74 68 61 74 20 77 72 6f 74 65 20 74 6f 20 * that wrote to
27420 74 68 69 73 20 64 61 74 61 62 61 73 65 20 77 61 this database wa
27430 73 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 70 s operating in p
27440 65 72 73 69 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 ersistent-journa
27450 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 6f 64 l. ** mod
27460 65 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 e, then the jour
27470 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 61 74 20 nal file may at
27480 74 68 69 73 20 70 6f 69 6e 74 20 61 63 74 75 61 this point actua
27490 6c 6c 79 20 62 65 20 6c 61 72 67 65 72 0a 20 20 lly be larger.
274a0 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 50 61 ** than Pa
274b0 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 62 ger.journalOff b
274c0 79 74 65 73 2e 20 49 66 20 74 68 65 20 6e 65 78 ytes. If the nex
274d0 74 20 74 68 69 6e 67 20 69 6e 20 74 68 65 20 6a t thing in the j
274e0 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a ournal. *
274f0 2a 20 66 69 6c 65 20 68 61 70 70 65 6e 73 20 74 * file happens t
27500 6f 20 62 65 20 61 20 6a 6f 75 72 6e 61 6c 2d 68 o be a journal-h
27510 65 61 64 65 72 20 28 77 72 69 74 74 65 6e 20 61 eader (written a
27520 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a 20 20 s part of the.
27530 20 20 20 20 20 20 2a 2a 20 70 72 65 76 69 6f 75 ** previou
27540 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 27 73 20 74 s connection's t
27550 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 61 6e 64 ransaction), and
27560 20 61 20 63 72 61 73 68 20 6f 72 20 70 6f 77 65 a crash or powe
27570 72 2d 66 61 69 6c 75 72 65 20 0a 20 20 20 20 20 r-failure .
27580 20 20 20 2a 2a 20 6f 63 63 75 72 73 20 61 66 74 ** occurs aft
27590 65 72 20 6e 52 65 63 20 69 73 20 75 70 64 61 74 er nRec is updat
275a0 65 64 20 62 75 74 20 62 65 66 6f 72 65 20 74 68 ed but before th
275b0 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 72 is connection wr
275c0 69 74 65 73 20 0a 20 20 20 20 20 20 20 20 2a 2a ites . **
275d0 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 74 anything else t
275e0 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 o the journal fi
275f0 6c 65 20 28 6f 72 20 63 6f 6d 6d 69 74 73 2f 72 le (or commits/r
27600 6f 6c 6c 73 20 62 61 63 6b 20 69 74 73 20 0a 20 olls back its .
27610 20 20 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 ** transa
27620 63 74 69 6f 6e 29 2c 20 74 68 65 6e 20 53 51 4c ction), then SQL
27630 69 74 65 20 6d 61 79 20 62 65 63 6f 6d 65 20 63 ite may become c
27640 6f 6e 66 75 73 65 64 20 77 68 65 6e 20 64 6f 69 onfused when doi
27650 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 ng the .
27660 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 ** hot-journal r
27670 6f 6c 6c 62 61 63 6b 20 66 6f 6c 6c 6f 77 69 6e ollback followin
27680 67 20 72 65 63 6f 76 65 72 79 2e 20 49 74 20 6d g recovery. It m
27690 61 79 20 72 6f 6c 6c 20 62 61 63 6b 20 61 6c 6c ay roll back all
276a0 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 . ** of t
276b0 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 his connections
276c0 64 61 74 61 2c 20 74 68 65 6e 20 70 72 6f 63 65 data, then proce
276d0 65 64 20 74 6f 20 72 6f 6c 6c 69 6e 67 20 62 61 ed to rolling ba
276e0 63 6b 20 74 68 65 20 6f 6c 64 2c 0a 20 20 20 20 ck the old,.
276f0 20 20 20 20 2a 2a 20 6f 75 74 2d 6f 66 2d 64 61 ** out-of-da
27700 74 65 20 64 61 74 61 20 74 68 61 74 20 66 6f 6c te data that fol
27710 6c 6f 77 73 20 69 74 2e 20 44 61 74 61 62 61 73 lows it. Databas
27720 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 e corruption..
27730 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 **.
27740 20 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 ** To work arou
27750 6e 64 20 74 68 69 73 2c 20 69 66 20 74 68 65 20 nd this, if the
27760 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 journal file doe
27770 73 20 61 70 70 65 61 72 20 74 6f 20 63 6f 6e 74 s appear to cont
27780 61 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 ain. ** a
27790 20 76 61 6c 69 64 20 68 65 61 64 65 72 20 66 6f valid header fo
277a0 6c 6c 6f 77 69 6e 67 20 50 61 67 65 72 2e 6a 6f llowing Pager.jo
277b0 75 72 6e 61 6c 4f 66 66 2c 20 74 68 65 6e 20 77 urnalOff, then w
277c0 72 69 74 65 20 61 20 30 78 30 30 0a 20 20 20 20 rite a 0x00.
277d0 20 20 20 20 2a 2a 20 62 79 74 65 20 74 6f 20 74 ** byte to t
277e0 68 65 20 73 74 61 72 74 20 6f 66 20 69 74 20 74 he start of it t
277f0 6f 20 70 72 65 76 65 6e 74 20 69 74 20 66 72 6f o prevent it fro
27800 6d 20 62 65 69 6e 67 20 72 65 63 6f 67 6e 69 7a m being recogniz
27810 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 ed.. **.
27820 20 20 20 20 20 20 20 2a 2a 20 56 61 72 69 61 62 ** Variab
27830 6c 65 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 le iNextHdrOffse
27840 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 t is set to the
27850 6f 66 66 73 65 74 20 61 74 20 77 68 69 63 68 20 offset at which
27860 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 this. **
27870 70 72 6f 62 6c 65 6d 61 74 69 63 20 68 65 61 64 problematic head
27880 65 72 20 77 69 6c 6c 20 6f 63 63 75 72 2c 20 69 er will occur, i
27890 66 20 69 74 20 65 78 69 73 74 73 2e 20 61 4d 61 f it exists. aMa
278a0 67 69 63 20 69 73 20 75 73 65 64 20 0a 20 20 20 gic is used .
278b0 20 20 20 20 20 2a 2a 20 61 73 20 61 20 74 65 6d ** as a tem
278c0 70 6f 72 61 72 79 20 62 75 66 66 65 72 20 74 6f porary buffer to
278d0 20 69 6e 73 70 65 63 74 20 74 68 65 20 66 69 72 inspect the fir
278e0 73 74 20 63 6f 75 70 6c 65 20 6f 66 20 62 79 74 st couple of byt
278f0 65 73 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a es of. **
27900 20 74 68 65 20 70 6f 74 65 6e 74 69 61 6c 20 6a the potential j
27910 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 0a 20 ournal header..
27920 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
27930 20 20 69 36 34 20 69 4e 65 78 74 48 64 72 4f 66 i64 iNextHdrOf
27940 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 75 38 fset;. u8
27950 20 61 4d 61 67 69 63 5b 38 5d 3b 0a 20 20 20 20 aMagic[8];.
27960 20 20 20 20 75 38 20 7a 48 65 61 64 65 72 5b 73 u8 zHeader[s
27970 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 izeof(aJournalMa
27980 67 69 63 29 2b 34 5d 3b 0a 0a 20 20 20 20 20 20 gic)+4];..
27990 20 20 6d 65 6d 63 70 79 28 7a 48 65 61 64 65 72 memcpy(zHeader
279a0 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c , aJournalMagic,
279b0 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c sizeof(aJournal
279c0 4d 61 67 69 63 29 29 3b 0a 20 20 20 20 20 20 20 Magic));.
279d0 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 put32bits(&zHea
279e0 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 der[sizeof(aJour
279f0 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 70 50 61 67 nalMagic)], pPag
27a00 65 72 2d 3e 6e 52 65 63 29 3b 0a 0a 20 20 20 20 er->nRec);..
27a10 20 20 20 20 69 4e 65 78 74 48 64 72 4f 66 66 73 iNextHdrOffs
27a20 65 74 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f et = journalHdrO
27a30 66 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 ffset(pPager);.
27a40 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
27a50 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 te3OsRead(pPager
27a60 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c 20 38 ->jfd, aMagic, 8
27a70 2c 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 , iNextHdrOffset
27a80 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
27a90 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
27aa0 30 3d 3d 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 0==memcmp(aMagic
27ab0 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c , aJournalMagic,
27ac0 20 38 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 8) ){.
27ad0 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 static const u8
27ae0 20 7a 65 72 6f 62 79 74 65 20 3d 20 30 3b 0a 20 zerobyte = 0;.
27af0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
27b00 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 lite3OsWrite(pPa
27b10 67 65 72 2d 3e 6a 66 64 2c 20 26 7a 65 72 6f 62 ger->jfd, &zerob
27b20 79 74 65 2c 20 31 2c 20 69 4e 65 78 74 48 64 72 yte, 1, iNextHdr
27b30 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 20 Offset);.
27b40 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 }. if( r
27b50 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c!=SQLITE_OK &&
27b60 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 rc!=SQLITE_IOERR
27b70 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 _SHORT_READ ){.
27b80 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
27b90 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 rc;. }..
27ba0 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
27bb0 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 the nRec value i
27bc0 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 nto the journal
27bd0 66 69 6c 65 20 68 65 61 64 65 72 2e 20 49 66 20 file header. If
27be0 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75 in. ** fu
27bf0 6c 6c 2d 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d ll-synchronous m
27c00 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20 6a 6f ode, sync the jo
27c10 75 72 6e 61 6c 20 66 69 72 73 74 2e 20 54 68 69 urnal first. Thi
27c20 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 0a 20 s ensures that.
27c30 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 20 64 61 ** all da
27c40 74 61 20 68 61 73 20 72 65 61 6c 6c 79 20 68 69 ta has really hi
27c50 74 20 74 68 65 20 64 69 73 6b 20 62 65 66 6f 72 t the disk befor
27c60 65 20 6e 52 65 63 20 69 73 20 75 70 64 61 74 65 e nRec is update
27c70 64 20 74 6f 20 6d 61 72 6b 0a 20 20 20 20 20 20 d to mark.
27c80 20 20 2a 2a 20 69 74 20 61 73 20 61 20 63 61 6e ** it as a can
27c90 64 69 64 61 74 65 20 66 6f 72 20 72 6f 6c 6c 62 didate for rollb
27ca0 61 63 6b 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a ack.. **.
27cb0 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 ** This
27cc0 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 is not required
27cd0 69 66 20 74 68 65 20 70 65 72 73 69 73 74 65 6e if the persisten
27ce0 74 20 6d 65 64 69 61 20 73 75 70 70 6f 72 74 73 t media supports
27cf0 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 the. **
27d00 53 41 46 45 5f 41 50 50 45 4e 44 20 70 72 6f 70 SAFE_APPEND prop
27d10 65 72 74 79 2e 20 42 65 63 61 75 73 65 20 69 6e erty. Because in
27d20 20 74 68 69 73 20 63 61 73 65 20 69 74 20 69 73 this case it is
27d30 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 0a 20 not possible .
27d40 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 67 61 ** for ga
27d50 72 62 61 67 65 20 64 61 74 61 20 74 6f 20 62 65 rbage data to be
27d60 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 appended to the
27d70 20 66 69 6c 65 2c 20 74 68 65 20 6e 52 65 63 20 file, the nRec
27d80 66 69 65 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a field. **
27d90 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 is populated wi
27da0 74 68 20 30 78 46 46 46 46 46 46 46 46 20 77 68 th 0xFFFFFFFF wh
27db0 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 en the journal h
27dc0 65 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e eader is written
27dd0 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 . ** and
27de0 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f 20 62 never needs to b
27df0 65 20 75 70 64 61 74 65 64 2e 0a 20 20 20 20 20 e updated..
27e00 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 */. if
27e10 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 ( pPager->fullSy
27e20 6e 63 20 26 26 20 30 3d 3d 28 69 44 63 26 53 51 nc && 0==(iDc&SQ
27e30 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 LITE_IOCAP_SEQUE
27e40 4e 54 49 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 NTIAL) ){.
27e50 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 PAGERTRACE((
27e60 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 "SYNC journal of
27e70 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 %d\n", PAGERID(
27e80 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 20 20 pPager)));.
27e90 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a IOTRACE(("J
27ea0 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 SYNC %p\n", pPag
27eb0 65 72 29 29 0a 20 20 20 20 20 20 20 20 20 20 72 er)). r
27ec0 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e c = sqlite3OsSyn
27ed0 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 c(pPager->jfd, p
27ee0 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73 Pager->syncFlags
27ef0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
27f00 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
27f10 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
27f20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 49 4f }. IO
27f30 54 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 20 TRACE(("JHDR %p
27f40 25 6c 6c 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c %lld\n", pPager,
27f50 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
27f60 48 64 72 29 29 3b 0a 20 20 20 20 20 20 20 20 72 Hdr));. r
27f70 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
27f80 74 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 te(.
27f90 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 48 65 pPager->jfd, zHe
27fa0 61 64 65 72 2c 20 73 69 7a 65 6f 66 28 7a 48 65 ader, sizeof(zHe
27fb0 61 64 65 72 29 2c 20 70 50 61 67 65 72 2d 3e 6a ader), pPager->j
27fc0 6f 75 72 6e 61 6c 48 64 72 0a 20 20 20 20 20 20 ournalHdr.
27fd0 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 );. if(
27fe0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
27ff0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
28000 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 30 3d }. if( 0=
28010 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 =(iDc&SQLITE_IOC
28020 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 29 20 29 AP_SEQUENTIAL) )
28030 7b 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54 {. PAGERT
28040 52 41 43 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 RACE(("SYNC jour
28050 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 nal of %d\n", PA
28060 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b GERID(pPager)));
28070 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 . IOTRACE
28080 28 28 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 (("JSYNC %p\n",
28090 70 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 pPager)).
280a0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 rc = sqlite3OsS
280b0 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ync(pPager->jfd,
280c0 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 pPager->syncFla
280d0 67 73 7c 20 0a 20 20 20 20 20 20 20 20 20 20 28 gs| . (
280e0 70 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 pPager->syncFlag
280f0 73 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 s==SQLITE_SYNC_F
28100 55 4c 4c 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f ULL?SQLITE_SYNC_
28110 44 41 54 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 DATAONLY:0).
28120 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 );. i
28130 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
28140 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
28150 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 70 50 61 }.. pPa
28160 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 ger->journalHdr
28170 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 = pPager->journa
28180 6c 4f 66 66 3b 0a 20 20 20 20 20 20 69 66 28 20 lOff;. if(
28190 6e 65 77 48 64 72 20 26 26 20 30 3d 3d 28 69 44 newHdr && 0==(iD
281a0 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 c&SQLITE_IOCAP_S
281b0 41 46 45 5f 41 50 50 45 4e 44 29 20 29 7b 0a 20 AFE_APPEND) ){.
281c0 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e pPager->n
281d0 52 65 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 Rec = 0;.
281e0 20 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e rc = writeJourn
281f0 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a 20 alHdr(pPager);.
28200 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
28210 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 QLITE_OK ) retur
28220 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n rc;. }.
28230 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
28240 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
28250 72 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 r = pPager->jour
28260 6e 61 6c 4f 66 66 3b 0a 20 20 20 20 7d 0a 20 20 nalOff;. }.
28270 7d 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20 74 }.. /* Unless t
28280 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 6e he pager is in n
28290 6f 53 79 6e 63 20 6d 6f 64 65 2c 20 74 68 65 20 oSync mode, the
282a0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 61 73 journal file was
282b0 20 6a 75 73 74 20 0a 20 20 2a 2a 20 73 75 63 63 just . ** succ
282c0 65 73 73 66 75 6c 6c 79 20 73 79 6e 63 65 64 2e essfully synced.
282d0 20 45 69 74 68 65 72 20 77 61 79 2c 20 63 6c 65 Either way, cle
282e0 61 72 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 ar the PGHDR_NEE
282f0 44 5f 53 59 4e 43 20 66 6c 61 67 20 6f 6e 20 0a D_SYNC flag on .
28300 20 20 2a 2a 20 61 6c 6c 20 70 61 67 65 73 2e 0a ** all pages..
28310 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 63 */. sqlite3Pc
28320 61 63 68 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 acheClearSyncFla
28330 67 73 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 gs(pPager->pPCac
28340 68 65 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 he);. pPager->e
28350 53 74 61 74 65 20 3d 20 50 41 47 45 52 5f 57 52 State = PAGER_WR
28360 49 54 45 52 5f 44 42 4d 4f 44 3b 0a 20 20 61 73 ITER_DBMOD;. as
28370 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 67 sert( assert_pag
28380 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 29 er_state(pPager)
28390 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c );. return SQL
283a0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
283b0 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 The argument is
283c0 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 the first in a
283d0 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 64 linked list of d
283e0 69 72 74 79 20 70 61 67 65 73 20 63 6f 6e 6e 65 irty pages conne
283f0 63 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 50 cted.** by the P
28400 67 48 64 72 2e 70 44 69 72 74 79 20 70 6f 69 6e gHdr.pDirty poin
28410 74 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 ter. This functi
28420 6f 6e 20 77 72 69 74 65 73 20 65 61 63 68 20 6f on writes each o
28430 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20 69 6e 2d ne of the.** in-
28440 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 69 6e 20 memory pages in
28450 74 68 65 20 6c 69 73 74 20 74 6f 20 74 68 65 20 the list to the
28460 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 database file. T
28470 68 65 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 0a he argument may.
28480 2a 2a 20 62 65 20 4e 55 4c 4c 2c 20 72 65 70 72 ** be NULL, repr
28490 65 73 65 6e 74 69 6e 67 20 61 6e 20 65 6d 70 74 esenting an empt
284a0 79 20 6c 69 73 74 2e 20 49 6e 20 74 68 69 73 20 y list. In this
284b0 63 61 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 case this functi
284c0 6f 6e 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 on is.** a no-op
284d0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 ..**.** The page
284e0 72 20 6d 75 73 74 20 68 6f 6c 64 20 61 74 20 6c r must hold at l
284f0 65 61 73 74 20 61 20 52 45 53 45 52 56 45 44 20 east a RESERVED
28500 6c 6f 63 6b 20 77 68 65 6e 20 74 68 69 73 20 66 lock when this f
28510 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 61 unction.** is ca
28520 6c 6c 65 64 2e 20 42 65 66 6f 72 65 20 77 72 69 lled. Before wri
28530 74 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 74 6f ting anything to
28540 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
28550 6c 65 2c 20 74 68 69 73 20 6c 6f 63 6b 0a 2a 2a le, this lock.**
28560 20 69 73 20 75 70 67 72 61 64 65 64 20 74 6f 20 is upgraded to
28570 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
28580 6b 2e 20 49 66 20 74 68 65 20 6c 6f 63 6b 20 63 k. If the lock c
28590 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 annot be obtaine
285a0 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 d,.** SQLITE_BUS
285b0 59 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e Y is returned an
285c0 64 20 6e 6f 20 64 61 74 61 20 69 73 20 77 72 69 d no data is wri
285d0 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 tten to the data
285e0 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a base file..** .*
285f0 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 * If the pager i
28600 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 20 70 61 s a temp-file pa
28610 67 65 72 20 61 6e 64 20 74 68 65 20 61 63 74 75 ger and the actu
28620 61 6c 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 al file-system f
28630 69 6c 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 79 65 ile.** is not ye
28640 74 20 6f 70 65 6e 2c 20 69 74 20 69 73 20 63 72 t open, it is cr
28650 65 61 74 65 64 20 61 6e 64 20 6f 70 65 6e 65 64 eated and opened
28660 20 62 65 66 6f 72 65 20 61 6e 79 20 64 61 74 61 before any data
28670 20 69 73 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 is .** written
28680 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 out..**.** Once
28690 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 the lock has bee
286a0 6e 20 75 70 67 72 61 64 65 64 20 61 6e 64 2c 20 n upgraded and,
286b0 69 66 20 6e 65 63 65 73 73 61 72 79 2c 20 74 68 if necessary, th
286c0 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c 0a 2a e file opened,.*
286d0 2a 20 74 68 65 20 70 61 67 65 73 20 61 72 65 20 * the pages are
286e0 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 20 74 written out to t
286f0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
28700 20 69 6e 20 6c 69 73 74 20 6f 72 64 65 72 2e 20 in list order.
28710 57 72 69 74 69 6e 67 0a 2a 2a 20 61 20 70 61 67 Writing.** a pag
28720 65 20 69 73 20 73 6b 69 70 70 65 64 20 69 66 20 e is skipped if
28730 69 74 20 6d 65 65 74 73 20 65 69 74 68 65 72 20 it meets either
28740 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
28750 20 63 72 69 74 65 72 69 61 3a 0a 2a 2a 0a 2a 2a criteria:.**.**
28760 20 20 20 2a 20 54 68 65 20 70 61 67 65 20 6e 75 * The page nu
28770 6d 62 65 72 20 69 73 20 67 72 65 61 74 65 72 20 mber is greater
28780 74 68 61 6e 20 50 61 67 65 72 2e 64 62 53 69 7a than Pager.dbSiz
28790 65 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20 54 68 65 e, or.** * The
287a0 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 PGHDR_DONT_WRIT
287b0 45 20 66 6c 61 67 20 69 73 20 73 65 74 20 6f 6e E flag is set on
287c0 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a the page..**.**
287d0 20 49 66 20 77 72 69 74 69 6e 67 20 6f 75 74 20 If writing out
287e0 61 20 70 61 67 65 20 63 61 75 73 65 73 20 74 68 a page causes th
287f0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
28800 74 6f 20 67 72 6f 77 2c 20 50 61 67 65 72 2e 64 to grow, Pager.d
28810 62 46 69 6c 65 53 69 7a 65 0a 2a 2a 20 69 73 20 bFileSize.** is
28820 75 70 64 61 74 65 64 20 61 63 63 6f 72 64 69 6e updated accordin
28830 67 6c 79 2e 20 49 66 20 70 61 67 65 20 31 20 69 gly. If page 1 i
28840 73 20 77 72 69 74 74 65 6e 20 6f 75 74 2c 20 74 s written out, t
28850 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 63 61 hen the value ca
28860 63 68 65 64 0a 2a 2a 20 69 6e 20 50 61 67 65 72 ched.** in Pager
28870 2e 64 62 46 69 6c 65 56 65 72 73 5b 5d 20 69 73 .dbFileVers[] is
28880 20 75 70 64 61 74 65 64 20 74 6f 20 6d 61 74 63 updated to matc
28890 68 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 20 h the new value
288a0 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 74 68 65 stored in.** the
288b0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
288c0 2a 2a 0a 2a 2a 20 49 66 20 65 76 65 72 79 74 68 **.** If everyth
288d0 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 ing is successfu
288e0 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 l, SQLITE_OK is
288f0 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 returned. If an
28900 49 4f 20 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 IO error .** occ
28910 75 72 73 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 urs, an IO error
28920 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
28930 64 2e 20 4f 72 2c 20 69 66 20 74 68 65 20 45 58 d. Or, if the EX
28940 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 63 61 6e CLUSIVE lock can
28950 6e 6f 74 0a 2a 2a 20 62 65 20 6f 62 74 61 69 6e not.** be obtain
28960 65 64 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 20 ed, SQLITE_BUSY
28970 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a is returned..*/.
28980 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
28990 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28 _write_pagelist(
289a0 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 Pager *pPager, P
289b0 67 48 64 72 20 2a 70 4c 69 73 74 29 7b 0a 20 20 gHdr *pList){.
289c0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
289d0 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 OK;
289e0 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
289f0 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 ode */.. /* Thi
28a00 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e s function is on
28a10 6c 79 20 63 61 6c 6c 65 64 20 66 6f 72 20 72 6f ly called for ro
28a20 6c 6c 62 61 63 6b 20 70 61 67 65 72 73 20 69 6e llback pagers in
28a30 20 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 73 74 WRITER_DBMOD st
28a40 61 74 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 ate. */. assert
28a50 28 20 21 70 61 67 65 72 55 73 65 57 61 6c 28 70 ( !pagerUseWal(p
28a60 50 61 67 65 72 29 20 29 3b 0a 20 20 61 73 73 65 Pager) );. asse
28a70 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 rt( pPager->eSta
28a80 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 te==PAGER_WRITER
28a90 5f 44 42 4d 4f 44 20 29 3b 0a 20 20 61 73 73 65 _DBMOD );. asse
28aa0 72 74 28 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 rt( pPager->eLoc
28ab0 6b 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 k==EXCLUSIVE_LOC
28ac0 4b 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 K );.. /* If th
28ad0 65 20 66 69 6c 65 20 69 73 20 61 20 74 65 6d 70 e file is a temp
28ae0 2d 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 79 65 -file has not ye
28af0 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c 20 6f t been opened, o
28b00 70 65 6e 20 69 74 20 6e 6f 77 2e 20 49 74 0a 20 pen it now. It.
28b10 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 ** is not possi
28b20 62 6c 65 20 66 6f 72 20 72 63 20 74 6f 20 62 65 ble for rc to be
28b30 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 other than SQLI
28b40 54 45 5f 4f 4b 20 69 66 20 74 68 69 73 20 62 72 TE_OK if this br
28b50 61 6e 63 68 0a 20 20 2a 2a 20 69 73 20 74 61 6b anch. ** is tak
28b60 65 6e 2c 20 61 73 20 70 61 67 65 72 5f 77 61 69 en, as pager_wai
28b70 74 5f 6f 6e 5f 6c 6f 63 6b 28 29 20 69 73 20 61 t_on_lock() is a
28b80 20 6e 6f 2d 6f 70 20 66 6f 72 20 74 65 6d 70 2d no-op for temp-
28b90 66 69 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 files.. */. if
28ba0 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 ( !isOpen(pPager
28bb0 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 61 73 73 ->fd) ){. ass
28bc0 65 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65 6d ert( pPager->tem
28bd0 70 46 69 6c 65 20 26 26 20 72 63 3d 3d 53 51 4c pFile && rc==SQL
28be0 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 63 ITE_OK );. rc
28bf0 20 3d 20 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 = pagerOpentemp
28c00 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d (pPager, pPager-
28c10 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 76 66 73 >fd, pPager->vfs
28c20 46 6c 61 67 73 29 3b 0a 20 20 7d 0a 0a 20 20 2f Flags);. }.. /
28c30 2a 20 42 65 66 6f 72 65 20 74 68 65 20 66 69 72 * Before the fir
28c40 73 74 20 77 72 69 74 65 2c 20 67 69 76 65 20 74 st write, give t
28c50 68 65 20 56 46 53 20 61 20 68 69 6e 74 20 6f 66 he VFS a hint of
28c60 20 77 68 61 74 20 74 68 65 20 66 69 6e 61 6c 0a what the final.
28c70 20 20 2a 2a 20 66 69 6c 65 20 73 69 7a 65 20 77 ** file size w
28c80 69 6c 6c 20 62 65 2e 0a 20 20 2a 2f 0a 20 20 61 ill be.. */. a
28c90 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
28ca0 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 E_OK || isOpen(p
28cb0 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 Pager->fd) );.
28cc0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
28cd0 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62 53 K && pPager->dbS
28ce0 69 7a 65 3e 70 50 61 67 65 72 2d 3e 64 62 48 69 ize>pPager->dbHi
28cf0 6e 74 53 69 7a 65 20 29 7b 0a 20 20 20 20 73 71 ntSize ){. sq
28d00 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 7a 46 69 lite3_int64 szFi
28d10 6c 65 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 le = pPager->pag
28d20 65 53 69 7a 65 20 2a 20 28 73 71 6c 69 74 65 33 eSize * (sqlite3
28d30 5f 69 6e 74 36 34 29 70 50 61 67 65 72 2d 3e 64 _int64)pPager->d
28d40 62 53 69 7a 65 3b 0a 20 20 20 20 73 71 6c 69 74 bSize;. sqlit
28d50 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 48 e3OsFileControlH
28d60 69 6e 74 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 int(pPager->fd,
28d70 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 53 49 5a SQLITE_FCNTL_SIZ
28d80 45 5f 48 49 4e 54 2c 20 26 73 7a 46 69 6c 65 29 E_HINT, &szFile)
28d90 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 ;. pPager->db
28da0 48 69 6e 74 53 69 7a 65 20 3d 20 70 50 61 67 65 HintSize = pPage
28db0 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 7d 0a 0a r->dbSize;. }..
28dc0 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c while( rc==SQL
28dd0 49 54 45 5f 4f 4b 20 26 26 20 70 4c 69 73 74 20 ITE_OK && pList
28de0 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f ){. Pgno pgno
28df0 20 3d 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 3b 0a = pList->pgno;.
28e00 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 . /* If there
28e10 20 61 72 65 20 64 69 72 74 79 20 70 61 67 65 73 are dirty pages
28e20 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 in the page cac
28e30 68 65 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d he with page num
28e40 62 65 72 73 20 67 72 65 61 74 65 72 0a 20 20 20 bers greater.
28e50 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 2e 64 ** than Pager.d
28e60 62 53 69 7a 65 2c 20 74 68 69 73 20 6d 65 61 6e bSize, this mean
28e70 73 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 s sqlite3PagerTr
28e80 75 6e 63 61 74 65 49 6d 61 67 65 28 29 20 77 61 uncateImage() wa
28e90 73 20 63 61 6c 6c 65 64 20 74 6f 0a 20 20 20 20 s called to.
28ea0 2a 2a 20 6d 61 6b 65 20 74 68 65 20 66 69 6c 65 ** make the file
28eb0 20 73 6d 61 6c 6c 65 72 20 28 70 72 65 73 75 6d smaller (presum
28ec0 61 62 6c 79 20 62 79 20 61 75 74 6f 2d 76 61 63 ably by auto-vac
28ed0 75 75 6d 20 63 6f 64 65 29 2e 20 44 6f 20 6e 6f uum code). Do no
28ee0 74 20 77 72 69 74 65 0a 20 20 20 20 2a 2a 20 61 t write. ** a
28ef0 6e 79 20 73 75 63 68 20 70 61 67 65 73 20 74 6f ny such pages to
28f00 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a the file.. *
28f10 2a 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 64 *. ** Also, d
28f20 6f 20 6e 6f 74 20 77 72 69 74 65 20 6f 75 74 20 o not write out
28f30 61 6e 79 20 70 61 67 65 20 74 68 61 74 20 68 61 any page that ha
28f40 73 20 74 68 65 20 50 47 48 44 52 5f 44 4f 4e 54 s the PGHDR_DONT
28f50 5f 57 52 49 54 45 20 66 6c 61 67 0a 20 20 20 20 _WRITE flag.
28f60 2a 2a 20 73 65 74 20 28 73 65 74 20 62 79 20 73 ** set (set by s
28f70 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 qlite3PagerDontW
28f80 72 69 74 65 28 29 29 2e 0a 20 20 20 20 2a 2f 0a rite()).. */.
28f90 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 if( pgno<=pP
28fa0 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 ager->dbSize &&
28fb0 30 3d 3d 28 70 4c 69 73 74 2d 3e 66 6c 61 67 73 0==(pList->flags
28fc0 26 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 &PGHDR_DONT_WRIT
28fd0 45 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 E) ){. i64
28fe0 6f 66 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 offset = (pgno-1
28ff0 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 )*(i64)pPager->p
29000 61 67 65 53 69 7a 65 3b 20 20 20 2f 2a 20 4f 66 ageSize; /* Of
29010 66 73 65 74 20 74 6f 20 77 72 69 74 65 20 2a 2f fset to write */
29020 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 44 61 . char *pDa
29030 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ta;
29040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
29050 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 74 6f /* Data to
29060 20 77 72 69 74 65 20 2a 2f 20 20 20 20 0a 0a 20 write */ ..
29070 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 4c assert( (pL
29080 69 73 74 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 ist->flags&PGHDR
29090 5f 4e 45 45 44 5f 53 59 4e 43 29 3d 3d 30 20 29 _NEED_SYNC)==0 )
290a0 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69 73 ;. if( pLis
290b0 74 2d 3e 70 67 6e 6f 3d 3d 31 20 29 20 70 61 67 t->pgno==1 ) pag
290c0 65 72 5f 77 72 69 74 65 5f 63 68 61 6e 67 65 63 er_write_changec
290d0 6f 75 6e 74 65 72 28 70 4c 69 73 74 29 3b 0a 0a ounter(pList);..
290e0 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 65 20 /* Encode
290f0 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a the database */.
29100 20 20 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 CODEC2(pPa
29110 67 65 72 2c 20 70 4c 69 73 74 2d 3e 70 44 61 74 ger, pList->pDat
29120 61 2c 20 70 67 6e 6f 2c 20 36 2c 20 72 65 74 75 a, pgno, 6, retu
29130 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c rn SQLITE_NOMEM,
29140 20 70 44 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 pData);..
29150 2f 2a 20 57 72 69 74 65 20 6f 75 74 20 74 68 65 /* Write out the
29160 20 70 61 67 65 20 64 61 74 61 2e 20 2a 2f 0a 20 page data. */.
29170 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
29180 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3OsWrite(pPager-
29190 3e 66 64 2c 20 70 44 61 74 61 2c 20 70 50 61 67 >fd, pData, pPag
291a0 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 er->pageSize, of
291b0 66 73 65 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a fset);.. /*
291c0 20 49 66 20 70 61 67 65 20 31 20 77 61 73 20 6a If page 1 was j
291d0 75 73 74 20 77 72 69 74 74 65 6e 2c 20 75 70 64 ust written, upd
291e0 61 74 65 20 50 61 67 65 72 2e 64 62 46 69 6c 65 ate Pager.dbFile
291f0 56 65 72 73 20 74 6f 20 6d 61 74 63 68 0a 20 20 Vers to match.
29200 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 ** the value
29210 20 6e 6f 77 20 73 74 6f 72 65 64 20 69 6e 20 74 now stored in t
29220 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
29230 2e 20 49 66 20 77 72 69 74 69 6e 67 20 74 68 69 . If writing thi
29240 73 20 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 s . ** page
29250 20 63 61 75 73 65 64 20 74 68 65 20 64 61 74 61 caused the data
29260 62 61 73 65 20 66 69 6c 65 20 74 6f 20 67 72 6f base file to gro
29270 77 2c 20 75 70 64 61 74 65 20 64 62 46 69 6c 65 w, update dbFile
29280 53 69 7a 65 2e 20 0a 20 20 20 20 20 20 2a 2f 0a Size. . */.
29290 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d if( pgno==
292a0 31 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 1 ){. mem
292b0 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62 46 cpy(&pPager->dbF
292c0 69 6c 65 56 65 72 73 2c 20 26 70 44 61 74 61 5b ileVers, &pData[
292d0 32 34 5d 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 24], sizeof(pPag
292e0 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 er->dbFileVers))
292f0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
29300 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d if( pgno>pPager-
29310 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 >dbFileSize ){.
29320 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 pPager->d
29330 62 46 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f bFileSize = pgno
29340 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
29350 70 50 61 67 65 72 2d 3e 61 53 74 61 74 5b 50 41 pPager->aStat[PA
29360 47 45 52 5f 53 54 41 54 5f 57 52 49 54 45 5d 2b GER_STAT_WRITE]+
29370 2b 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 +;.. /* Upd
29380 61 74 65 20 61 6e 79 20 62 61 63 6b 75 70 20 6f ate any backup o
29390 62 6a 65 63 74 73 20 63 6f 70 79 69 6e 67 20 74 bjects copying t
293a0 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
293b0 68 69 73 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20 his pager. */.
293c0 20 20 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 sqlite3Backu
293d0 70 55 70 64 61 74 65 28 70 50 61 67 65 72 2d 3e pUpdate(pPager->
293e0 70 42 61 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 28 pBackup, pgno, (
293f0 75 38 2a 29 70 4c 69 73 74 2d 3e 70 44 61 74 61 u8*)pList->pData
29400 29 3b 0a 0a 20 20 20 20 20 20 50 41 47 45 52 54 );.. PAGERT
29410 52 41 43 45 28 28 22 53 54 4f 52 45 20 25 64 20 RACE(("STORE %d
29420 70 61 67 65 20 25 64 20 68 61 73 68 28 25 30 38 page %d hash(%08
29430 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 x)\n",.
29440 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49 PAGERI
29450 44 28 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c D(pPager), pgno,
29460 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 pager_pagehash(
29470 70 4c 69 73 74 29 29 29 3b 0a 20 20 20 20 20 20 pList)));.
29480 49 4f 54 52 41 43 45 28 28 22 50 47 4f 55 54 20 IOTRACE(("PGOUT
29490 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 %p %d\n", pPager
294a0 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 , pgno));.
294b0 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 PAGER_INCR(sqlit
294c0 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 e3_pager_writedb
294d0 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 7d 65 6c _count);. }el
294e0 73 65 7b 0a 20 20 20 20 20 20 50 41 47 45 52 54 se{. PAGERT
294f0 52 41 43 45 28 28 22 4e 4f 53 54 4f 52 45 20 25 RACE(("NOSTORE %
29500 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 d page %d\n", PA
29510 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 GERID(pPager), p
29520 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 gno));. }.
29530 20 70 61 67 65 72 5f 73 65 74 5f 70 61 67 65 68 pager_set_pageh
29540 61 73 68 28 70 4c 69 73 74 29 3b 0a 20 20 20 20 ash(pList);.
29550 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 2d 3e 70 pList = pList->p
29560 44 69 72 74 79 3b 0a 20 20 7d 0a 0a 20 20 72 65 Dirty;. }.. re
29570 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
29580 2a 20 45 6e 73 75 72 65 20 74 68 61 74 20 74 68 * Ensure that th
29590 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 e sub-journal fi
295a0 6c 65 20 69 73 20 6f 70 65 6e 2e 20 49 66 20 69 le is open. If i
295b0 74 20 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65 t is already ope
295c0 6e 2c 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 n, this .** func
295d0 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tion is a no-op.
295e0 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
295f0 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
29600 65 76 65 72 79 74 68 69 6e 67 20 67 6f 65 73 20 everything goes
29610 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 70 6c 61 according to pla
29620 6e 2e 20 41 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 n. An .** SQLITE
29630 5f 49 4f 45 52 52 5f 58 58 58 20 65 72 72 6f 72 _IOERR_XXX error
29640 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
29650 64 20 69 66 20 61 20 63 61 6c 6c 20 74 6f 20 73 d if a call to s
29660 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 29 20 0a qlite3OsOpen() .
29670 2a 2a 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 ** fails..*/.sta
29680 74 69 63 20 69 6e 74 20 6f 70 65 6e 53 75 62 4a tic int openSubJ
29690 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 ournal(Pager *pP
296a0 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 ager){. int rc
296b0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
296c0 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 f( !isOpen(pPage
296d0 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 r->sjfd) ){.
296e0 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 if( pPager->jour
296f0 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
29700 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 OURNALMODE_MEMOR
29710 59 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 75 62 Y || pPager->sub
29720 6a 49 6e 4d 65 6d 6f 72 79 20 29 7b 0a 20 20 20 jInMemory ){.
29730 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 sqlite3MemJou
29740 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d rnalOpen(pPager-
29750 3e 73 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 >sjfd);. }els
29760 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 e{. rc = pa
29770 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 gerOpentemp(pPag
29780 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 er, pPager->sjfd
29790 2c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 , SQLITE_OPEN_SU
297a0 42 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 7d BJOURNAL);. }
297b0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
297c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e ;.}../*.** Appen
297d0 64 20 61 20 72 65 63 6f 72 64 20 6f 66 20 74 68 d a record of th
297e0 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 e current state
297f0 6f 66 20 70 61 67 65 20 70 50 67 20 74 6f 20 74 of page pPg to t
29800 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 he sub-journal.
29810 0a 2a 2a 20 49 74 20 69 73 20 74 68 65 20 63 61 .** It is the ca
29820 6c 6c 65 72 73 20 72 65 73 70 6f 6e 73 69 62 69 llers responsibi
29830 6c 69 74 79 20 74 6f 20 75 73 65 20 73 75 62 6a lity to use subj
29840 52 65 71 75 69 72 65 73 50 61 67 65 28 29 20 74 RequiresPage() t
29850 6f 20 63 68 65 63 6b 20 0a 2a 2a 20 74 68 61 74 o check .** that
29860 20 69 74 20 69 73 20 72 65 61 6c 6c 79 20 72 65 it is really re
29870 71 75 69 72 65 64 20 62 65 66 6f 72 65 20 63 61 quired before ca
29880 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 lling this funct
29890 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 ion..**.** If su
298a0 63 63 65 73 73 66 75 6c 2c 20 73 65 74 20 74 68 ccessful, set th
298b0 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 e bit correspond
298c0 69 6e 67 20 74 6f 20 70 50 67 2d 3e 70 67 6e 6f ing to pPg->pgno
298d0 20 69 6e 20 74 68 65 20 62 69 74 76 65 63 73 0a in the bitvecs.
298e0 2a 2a 20 66 6f 72 20 61 6c 6c 20 6f 70 65 6e 20 ** for all open
298f0 73 61 76 65 70 6f 69 6e 74 73 20 62 65 66 6f 72 savepoints befor
29900 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a e returning..**.
29910 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
29920 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
29930 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 OK if everything
29940 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 is successful,
29950 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 63 an IO.** error c
29960 6f 64 65 20 69 66 20 74 68 65 20 61 74 74 65 6d ode if the attem
29970 70 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 pt to write to t
29980 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 he sub-journal f
29990 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 53 51 4c ails, or .** SQL
299a0 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d ITE_NOMEM if a m
299b0 61 6c 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 6c alloc fails whil
299c0 65 20 73 65 74 74 69 6e 67 20 61 20 62 69 74 20 e setting a bit
299d0 69 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 0a 2a in a savepoint.*
299e0 2a 20 62 69 74 76 65 63 2e 0a 2a 2f 0a 73 74 61 * bitvec..*/.sta
299f0 74 69 63 20 69 6e 74 20 73 75 62 6a 6f 75 72 6e tic int subjourn
29a00 61 6c 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 alPage(PgHdr *pP
29a10 67 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 g){. int rc = S
29a20 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 QLITE_OK;. Page
29a30 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d r *pPager = pPg-
29a40 3e 70 50 61 67 65 72 3b 0a 20 20 69 66 28 20 70 >pPager;. if( p
29a50 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
29a60 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de!=PAGER_JOURNA
29a70 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a 0a 20 20 LMODE_OFF ){..
29a80 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73 75 /* Open the su
29a90 62 2d 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 b-journal, if it
29aa0 20 68 61 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 has not already
29ab0 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 2a 2f 0a been opened */.
29ac0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
29ad0 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 er->useJournal )
29ae0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 ;. assert( is
29af0 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
29b00 29 20 7c 7c 20 70 61 67 65 72 55 73 65 57 61 6c ) || pagerUseWal
29b10 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 (pPager) );.
29b20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
29b30 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 7c 7c 20 Pager->sjfd) ||
29b40 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 3d pPager->nSubRec=
29b50 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =0 );. assert
29b60 28 20 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 ( pagerUseWal(pP
29b70 61 67 65 72 29 20 0a 20 20 20 20 20 20 20 20 20 ager) .
29b80 7c 7c 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c || pageInJournal
29b90 28 70 50 67 29 20 0a 20 20 20 20 20 20 20 20 20 (pPg) .
29ba0 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 || pPg->pgno>pPa
29bb0 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 ger->dbOrigSize
29bc0 0a 20 20 20 20 29 3b 0a 20 20 20 20 72 63 20 3d . );. rc =
29bd0 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 openSubJournal(
29be0 70 50 61 67 65 72 29 3b 0a 0a 20 20 20 20 2f 2a pPager);.. /*
29bf0 20 49 66 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 If the sub-jour
29c00 6e 61 6c 20 77 61 73 20 6f 70 65 6e 65 64 20 73 nal was opened s
29c10 75 63 63 65 73 73 66 75 6c 6c 79 20 28 6f 72 20 uccessfully (or
29c20 77 61 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e was already open
29c30 29 2c 0a 20 20 20 20 2a 2a 20 77 72 69 74 65 20 ),. ** write
29c40 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f the journal reco
29c50 72 64 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 rd into the file
29c60 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 . */. if( rc
29c70 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
29c80 20 20 20 20 20 76 6f 69 64 20 2a 70 44 61 74 61 void *pData
29c90 20 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 = pPg->pData;.
29ca0 20 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 i64 offset
29cb0 3d 20 28 69 36 34 29 70 50 61 67 65 72 2d 3e 6e = (i64)pPager->n
29cc0 53 75 62 52 65 63 2a 28 34 2b 70 50 61 67 65 72 SubRec*(4+pPager
29cd0 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 ->pageSize);.
29ce0 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 32 3b char *pData2;
29cf0 0a 20 20 0a 20 20 20 20 20 20 43 4f 44 45 43 32 . . CODEC2
29d00 28 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 (pPager, pData,
29d10 70 50 67 2d 3e 70 67 6e 6f 2c 20 37 2c 20 72 65 pPg->pgno, 7, re
29d20 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
29d30 4d 2c 20 70 44 61 74 61 32 29 3b 0a 20 20 20 20 M, pData2);.
29d40 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53 PAGERTRACE(("S
29d50 54 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25 64 20 70 TMT-JOURNAL %d p
29d60 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 age %d\n", PAGER
29d70 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d ID(pPager), pPg-
29d80 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 72 >pgno));. r
29d90 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 c = write32bits(
29da0 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 6f 66 pPager->sjfd, of
29db0 66 73 65 74 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 fset, pPg->pgno)
29dc0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
29dd0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
29de0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
29df0 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3OsWrite(pPager-
29e00 3e 73 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 70 >sjfd, pData2, p
29e10 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c Pager->pageSize,
29e20 20 6f 66 66 73 65 74 2b 34 29 3b 0a 20 20 20 20 offset+4);.
29e30 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
29e40 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
29e50 4b 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d K ){. pPager-
29e60 3e 6e 53 75 62 52 65 63 2b 2b 3b 0a 20 20 20 20 >nSubRec++;.
29e70 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
29e80 6e 53 61 76 65 70 6f 69 6e 74 3e 30 20 29 3b 0a nSavepoint>0 );.
29e90 20 20 20 20 72 63 20 3d 20 61 64 64 54 6f 53 61 rc = addToSa
29ea0 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 70 vepointBitvecs(p
29eb0 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f Pager, pPg->pgno
29ec0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
29ed0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rc;.}../*.** Thi
29ee0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
29ef0 6c 6c 65 64 20 62 79 20 74 68 65 20 70 63 61 63 lled by the pcac
29f00 68 65 20 6c 61 79 65 72 20 77 68 65 6e 20 69 74 he layer when it
29f10 20 68 61 73 20 72 65 61 63 68 65 64 20 73 6f 6d has reached som
29f20 65 0a 2a 2a 20 73 6f 66 74 20 6d 65 6d 6f 72 79 e.** soft memory
29f30 20 6c 69 6d 69 74 2e 20 54 68 65 20 66 69 72 73 limit. The firs
29f40 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 t argument is a
29f50 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 50 61 67 pointer to a Pag
29f60 65 72 20 6f 62 6a 65 63 74 0a 2a 2a 20 28 63 61 er object.** (ca
29f70 73 74 20 61 73 20 61 20 76 6f 69 64 2a 29 2e 20 st as a void*).
29f80 54 68 65 20 70 61 67 65 72 20 69 73 20 61 6c 77 The pager is alw
29f90 61 79 73 20 27 70 75 72 67 65 61 62 6c 65 27 20 ays 'purgeable'
29fa0 28 6e 6f 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 (not an in-memor
29fb0 79 0a 2a 2a 20 64 61 74 61 62 61 73 65 29 2e 20 y.** database).
29fc0 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d The second argum
29fd0 65 6e 74 20 69 73 20 61 20 72 65 66 65 72 65 6e ent is a referen
29fe0 63 65 20 74 6f 20 61 20 70 61 67 65 20 74 68 61 ce to a page tha
29ff0 74 20 69 73 20 0a 2a 2a 20 63 75 72 72 65 6e 74 t is .** current
2a000 6c 79 20 64 69 72 74 79 20 62 75 74 20 68 61 73 ly dirty but has
2a010 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 no outstanding
2a020 72 65 66 65 72 65 6e 63 65 73 2e 20 54 68 65 20 references. The
2a030 70 61 67 65 0a 2a 2a 20 69 73 20 61 6c 77 61 79 page.** is alway
2a040 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
2a050 68 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a 65 h the Pager obje
2a060 63 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65 ct passed as the
2a070 20 66 69 72 73 74 20 0a 2a 2a 20 61 72 67 75 6d first .** argum
2a080 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a ent..**.** The j
2a090 6f 62 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 ob of this funct
2a0a0 69 6f 6e 20 69 73 20 74 6f 20 6d 61 6b 65 20 70 ion is to make p
2a0b0 50 67 20 63 6c 65 61 6e 20 62 79 20 77 72 69 74 Pg clean by writ
2a0c0 69 6e 67 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 ing its contents
2a0d0 0a 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65 20 64 .** out to the d
2a0e0 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69 66 atabase file, if
2a0f0 20 70 6f 73 73 69 62 6c 65 2e 20 54 68 69 73 20 possible. This
2a100 6d 61 79 20 69 6e 76 6f 6c 76 65 20 73 79 6e 63 may involve sync
2a110 69 6e 67 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e ing the.** journ
2a120 61 6c 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 al file. .**.**
2a130 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 If successful, s
2a140 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
2a150 43 6c 65 61 6e 28 29 20 69 73 20 63 61 6c 6c 65 Clean() is calle
2a160 64 20 6f 6e 20 74 68 65 20 70 61 67 65 20 61 6e d on the page an
2a170 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 72 d.** SQLITE_OK r
2a180 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 eturned. If an I
2a190 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 O error occurs w
2a1a0 68 69 6c 65 20 74 72 79 69 6e 67 20 74 6f 20 6d hile trying to m
2a1b0 61 6b 65 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 ake the.** page
2a1c0 63 6c 65 61 6e 2c 20 74 68 65 20 49 4f 20 65 72 clean, the IO er
2a1d0 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
2a1e0 72 6e 65 64 2e 20 49 66 20 74 68 65 20 70 61 67 rned. If the pag
2a1f0 65 20 63 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20 6d e cannot be.** m
2a200 61 64 65 20 63 6c 65 61 6e 20 66 6f 72 20 73 6f ade clean for so
2a210 6d 65 20 6f 74 68 65 72 20 72 65 61 73 6f 6e 2c me other reason,
2a220 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 but no error oc
2a230 63 75 72 73 2c 20 74 68 65 6e 20 53 51 4c 49 54 curs, then SQLIT
2a240 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 E_OK.** is retur
2a250 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 50 63 ned by sqlite3Pc
2a260 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 acheMakeClean()
2a270 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 2e 0a 2a is not called..*
2a280 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
2a290 65 72 53 74 72 65 73 73 28 76 6f 69 64 20 2a 70 erStress(void *p
2a2a0 2c 20 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 , PgHdr *pPg){.
2a2b0 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
2a2c0 20 28 50 61 67 65 72 20 2a 29 70 3b 0a 20 20 69 (Pager *)p;. i
2a2d0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
2a2e0 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 K;.. assert( pP
2a2f0 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67 65 g->pPager==pPage
2a300 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 r );. assert( p
2a310 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
2a320 44 49 52 54 59 20 29 3b 0a 0a 20 20 2f 2a 20 54 DIRTY );.. /* T
2a330 68 65 20 64 6f 4e 6f 74 53 79 6e 63 53 70 69 6c he doNotSyncSpil
2a340 6c 20 66 6c 61 67 20 69 73 20 73 65 74 20 64 75 l flag is set du
2a350 72 69 6e 67 20 74 69 6d 65 73 20 77 68 65 6e 20 ring times when
2a360 64 6f 69 6e 67 20 61 20 73 79 6e 63 20 6f 66 0a doing a sync of.
2a370 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 28 61 6e ** journal (an
2a380 64 20 61 64 64 69 6e 67 20 61 20 6e 65 77 20 68 d adding a new h
2a390 65 61 64 65 72 29 20 69 73 20 6e 6f 74 20 61 6c eader) is not al
2a3a0 6c 6f 77 65 64 2e 20 20 54 68 69 73 20 6f 63 63 lowed. This occ
2a3b0 75 72 73 0a 20 20 2a 2a 20 64 75 72 69 6e 67 20 urs. ** during
2a3c0 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 calls to sqlite3
2a3d0 50 61 67 65 72 57 72 69 74 65 28 29 20 77 68 69 PagerWrite() whi
2a3e0 6c 65 20 74 72 79 69 6e 67 20 74 6f 20 6a 6f 75 le trying to jou
2a3f0 72 6e 61 6c 20 6d 75 6c 74 69 70 6c 65 0a 20 20 rnal multiple.
2a400 2a 2a 20 70 61 67 65 73 20 62 65 6c 6f 6e 67 69 ** pages belongi
2a410 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 73 ng to the same s
2a420 65 63 74 6f 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a ector.. **. **
2a430 20 54 68 65 20 64 6f 4e 6f 74 53 70 69 6c 6c 20 The doNotSpill
2a440 66 6c 61 67 20 69 6e 68 69 62 69 74 73 20 61 6c flag inhibits al
2a450 6c 20 63 61 63 68 65 20 73 70 69 6c 6c 69 6e 67 l cache spilling
2a460 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 regardless of w
2a470 68 65 74 68 65 72 0a 20 20 2a 2a 20 6f 72 20 6e hether. ** or n
2a480 6f 74 20 61 20 73 79 6e 63 20 69 73 20 72 65 71 ot a sync is req
2a490 75 69 72 65 64 2e 20 20 54 68 69 73 20 69 73 20 uired. This is
2a4a0 73 65 74 20 64 75 72 69 6e 67 20 61 20 72 6f 6c set during a rol
2a4b0 6c 62 61 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a lback.. **. **
2a4c0 20 53 70 69 6c 6c 69 6e 67 20 69 73 20 61 6c 73 Spilling is als
2a4d0 6f 20 70 72 6f 68 69 62 69 74 65 64 20 77 68 65 o prohibited whe
2a4e0 6e 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 n in an error st
2a4f0 61 74 65 20 73 69 6e 63 65 20 74 68 61 74 20 63 ate since that c
2a500 6f 75 6c 64 0a 20 20 2a 2a 20 6c 65 61 64 20 74 ould. ** lead t
2a510 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 o database corru
2a520 70 74 69 6f 6e 2e 20 20 20 49 6e 20 74 68 65 20 ption. In the
2a530 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e current implemen
2a540 74 61 74 6f 6e 20 69 74 20 0a 20 20 2a 2a 20 69 taton it . ** i
2a550 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72 s impossible for
2a560 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 sqlite3PcacheFe
2a570 74 63 68 28 29 20 74 6f 20 62 65 20 63 61 6c 6c tch() to be call
2a580 65 64 20 77 69 74 68 20 63 72 65 61 74 65 46 6c ed with createFl
2a590 61 67 3d 3d 31 0a 20 20 2a 2a 20 77 68 69 6c 65 ag==1. ** while
2a5a0 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 in the error st
2a5b0 61 74 65 2c 20 68 65 6e 63 65 20 69 74 20 69 73 ate, hence it is
2a5c0 20 69 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 impossible for
2a5d0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 0a this routine to.
2a5e0 20 20 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 69 ** be called i
2a5f0 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 n the error stat
2a600 65 2e 20 20 4e 65 76 65 72 74 68 65 6c 65 73 73 e. Nevertheless
2a610 2c 20 77 65 20 69 6e 63 6c 75 64 65 20 61 20 4e , we include a N
2a620 45 56 45 52 28 29 0a 20 20 2a 2a 20 74 65 73 74 EVER(). ** test
2a630 20 66 6f 72 20 74 68 65 20 65 72 72 6f 72 20 73 for the error s
2a640 74 61 74 65 20 61 73 20 61 20 73 61 66 65 67 75 tate as a safegu
2a650 61 72 64 20 61 67 61 69 6e 73 74 20 66 75 74 75 ard against futu
2a660 72 65 20 63 68 61 6e 67 65 73 2e 0a 20 20 2a 2f re changes.. */
2a670 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 . if( NEVER(pPa
2a680 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 ger->errCode) )
2a690 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
2a6a0 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e ;. if( pPager->
2a6b0 64 6f 4e 6f 74 53 70 69 6c 6c 20 29 20 72 65 74 doNotSpill ) ret
2a6c0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
2a6d0 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e if( pPager->doN
2a6e0 6f 74 53 79 6e 63 53 70 69 6c 6c 20 26 26 20 28 otSyncSpill && (
2a6f0 70 50 67 2d 3e 66 6c 61 67 73 20 26 20 50 47 48 pPg->flags & PGH
2a700 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 21 3d 30 DR_NEED_SYNC)!=0
2a710 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
2a720 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 QLITE_OK;. }..
2a730 20 70 50 67 2d 3e 70 44 69 72 74 79 20 3d 20 30 pPg->pDirty = 0
2a740 3b 0a 20 20 69 66 28 20 70 61 67 65 72 55 73 65 ;. if( pagerUse
2a750 57 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 Wal(pPager) ){.
2a760 20 20 20 2f 2a 20 57 72 69 74 65 20 61 20 73 69 /* Write a si
2a770 6e 67 6c 65 20 66 72 61 6d 65 20 66 6f 72 20 74 ngle frame for t
2a780 68 69 73 20 70 61 67 65 20 74 6f 20 74 68 65 20 his page to the
2a790 6c 6f 67 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 log. */. if(
2a7a0 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 subjRequiresPage
2a7b0 28 70 50 67 29 20 29 7b 20 0a 20 20 20 20 20 20 (pPg) ){ .
2a7c0 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 rc = subjournalP
2a7d0 61 67 65 28 70 50 67 29 3b 20 0a 20 20 20 20 7d age(pPg); . }
2a7e0 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
2a7f0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
2a800 72 63 20 3d 20 70 61 67 65 72 57 61 6c 46 72 61 rc = pagerWalFra
2a810 6d 65 73 28 70 50 61 67 65 72 2c 20 70 50 67 2c mes(pPager, pPg,
2a820 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 0, 0);. }.
2a830 7d 65 6c 73 65 7b 0a 20 20 0a 20 20 20 20 2f 2a }else{. . /*
2a840 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 Sync the journa
2a850 6c 20 66 69 6c 65 20 69 66 20 72 65 71 75 69 72 l file if requir
2a860 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 ed. */. if( p
2a870 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
2a880 4e 45 45 44 5f 53 59 4e 43 20 0a 20 20 20 20 20 NEED_SYNC .
2a890 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 || pPager->eStat
2a8a0 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f e==PAGER_WRITER_
2a8b0 43 41 43 48 45 4d 4f 44 0a 20 20 20 20 29 7b 0a CACHEMOD. ){.
2a8c0 20 20 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a rc = syncJ
2a8d0 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c 20 31 ournal(pPager, 1
2a8e0 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 );. }. .
2a8f0 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 6e /* If the page n
2a900 75 6d 62 65 72 20 6f 66 20 74 68 69 73 20 70 61 umber of this pa
2a910 67 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 ge is larger tha
2a920 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 n the current si
2a930 7a 65 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 ze of. ** the
2a940 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 2c database image,
2a950 20 69 74 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 it may need to
2a960 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 be written to th
2a970 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 e sub-journal..
2a980 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 62 65 ** This is be
2a990 63 61 75 73 65 20 74 68 65 20 63 61 6c 6c 20 74 cause the call t
2a9a0 6f 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 o pager_write_pa
2a9b0 67 65 6c 69 73 74 28 29 20 62 65 6c 6f 77 20 77 gelist() below w
2a9c0 69 6c 6c 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 61 ill not. ** a
2a9d0 63 74 75 61 6c 6c 79 20 77 72 69 74 65 20 64 61 ctually write da
2a9e0 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65 20 69 ta to the file i
2a9f0 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20 n this case..
2aa00 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6f 6e 73 69 **. ** Consi
2aa10 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e der the followin
2aa20 67 20 73 65 71 75 65 6e 63 65 20 6f 66 20 65 76 g sequence of ev
2aa30 65 6e 74 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 ents:. **.
2aa40 20 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 ** BEGIN;.
2aa50 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c ** <journal
2aa60 20 70 61 67 65 20 58 3e 0a 20 20 20 20 2a 2a 20 page X>. **
2aa70 20 20 20 20 3c 6d 6f 64 69 66 79 20 70 61 67 65 <modify page
2aa80 20 58 3e 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 X>. ** S
2aa90 41 56 45 50 4f 49 4e 54 20 73 70 3b 0a 20 20 20 AVEPOINT sp;.
2aaa0 20 2a 2a 20 20 20 20 20 20 20 3c 73 68 72 69 6e ** <shrin
2aab0 6b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 k database file
2aac0 74 6f 20 59 20 70 61 67 65 73 3e 0a 20 20 20 20 to Y pages>.
2aad0 2a 2a 20 20 20 20 20 20 20 70 61 67 65 72 53 74 ** pagerSt
2aae0 72 65 73 73 28 70 61 67 65 20 58 29 0a 20 20 20 ress(page X).
2aaf0 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b ** ROLLBACK
2ab00 20 54 4f 20 73 70 3b 0a 20 20 20 20 2a 2a 0a 20 TO sp;. **.
2ab10 20 20 20 2a 2a 20 49 66 20 28 58 3e 59 29 2c 20 ** If (X>Y),
2ab20 74 68 65 6e 20 77 68 65 6e 20 70 61 67 65 72 53 then when pagerS
2ab30 74 72 65 73 73 20 69 73 20 63 61 6c 6c 65 64 20 tress is called
2ab40 70 61 67 65 20 58 20 77 69 6c 6c 20 6e 6f 74 20 page X will not
2ab50 62 65 20 77 72 69 74 74 65 6e 0a 20 20 20 20 2a be written. *
2ab60 2a 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 * out to the dat
2ab70 61 62 61 73 65 20 66 69 6c 65 2c 20 62 75 74 20 abase file, but
2ab80 77 69 6c 6c 20 62 65 20 64 72 6f 70 70 65 64 20 will be dropped
2ab90 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 2e 20 from the cache.
2aba0 54 68 65 6e 2c 0a 20 20 20 20 2a 2a 20 66 6f 6c Then,. ** fol
2abb0 6c 6f 77 69 6e 67 20 74 68 65 20 22 52 4f 4c 4c lowing the "ROLL
2abc0 42 41 43 4b 20 54 4f 20 73 70 22 20 73 74 61 74 BACK TO sp" stat
2abd0 65 6d 65 6e 74 2c 20 72 65 61 64 69 6e 67 20 70 ement, reading p
2abe0 61 67 65 20 58 20 77 69 6c 6c 20 72 65 61 64 0a age X will read.
2abf0 20 20 20 20 2a 2a 20 64 61 74 61 20 66 72 6f 6d ** data from
2ac00 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
2ac10 6c 65 2e 20 54 68 69 73 20 77 69 6c 6c 20 62 65 le. This will be
2ac20 20 74 68 65 20 63 6f 70 79 20 6f 66 20 70 61 67 the copy of pag
2ac30 65 20 58 20 61 73 20 69 74 0a 20 20 20 20 2a 2a e X as it. **
2ac40 20 77 61 73 20 77 68 65 6e 20 74 68 65 20 74 72 was when the tr
2ac50 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 72 74 65 ansaction starte
2ac60 64 2c 20 6e 6f 74 20 61 73 20 69 74 20 77 61 73 d, not as it was
2ac70 20 77 68 65 6e 20 22 53 41 56 45 50 4f 49 4e 54 when "SAVEPOINT
2ac80 20 73 70 22 0a 20 20 20 20 2a 2a 20 77 61 73 20 sp". ** was
2ac90 65 78 65 63 75 74 65 64 2e 0a 20 20 20 20 2a 2a executed.. **
2aca0 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 6f 6c 75 . ** The solu
2acb0 74 69 6f 6e 20 69 73 20 74 6f 20 77 72 69 74 65 tion is to write
2acc0 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 the current dat
2acd0 61 20 66 6f 72 20 70 61 67 65 20 58 20 69 6e 74 a for page X int
2ace0 6f 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 73 75 o the . ** su
2acf0 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e b-journal file n
2ad00 6f 77 20 28 69 66 20 69 74 20 69 73 20 6e 6f 74 ow (if it is not
2ad10 20 61 6c 72 65 61 64 79 20 74 68 65 72 65 29 2c already there),
2ad20 20 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c so that it will
2ad30 0a 20 20 20 20 2a 2a 20 62 65 20 72 65 73 74 6f . ** be resto
2ad40 72 65 64 20 74 6f 20 69 74 73 20 63 75 72 72 65 red to its curre
2ad50 6e 74 20 76 61 6c 75 65 20 77 68 65 6e 20 74 68 nt value when th
2ad60 65 20 22 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 e "ROLLBACK TO s
2ad70 70 22 20 69 73 20 0a 20 20 20 20 2a 2a 20 65 78 p" is . ** ex
2ad80 65 63 75 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 ecuted.. */.
2ad90 20 20 20 69 66 28 20 4e 45 56 45 52 28 0a 20 20 if( NEVER(.
2ada0 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 rc==SQLITE
2adb0 5f 4f 4b 20 26 26 20 70 50 67 2d 3e 70 67 6e 6f _OK && pPg->pgno
2adc0 3e 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 >pPager->dbSize
2add0 26 26 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 && subjRequiresP
2ade0 61 67 65 28 70 50 67 29 0a 20 20 20 20 29 20 29 age(pPg). ) )
2adf0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 75 62 {. rc = sub
2ae00 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 67 29 journalPage(pPg)
2ae10 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f ;. }. . /
2ae20 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6e 74 * Write the cont
2ae30 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 ents of the page
2ae40 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 out to the data
2ae50 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 base file. */.
2ae60 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
2ae70 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 _OK ){. ass
2ae80 65 72 74 28 20 28 70 50 67 2d 3e 66 6c 61 67 73 ert( (pPg->flags
2ae90 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
2aea0 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 63 )==0 );. rc
2aeb0 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 = pager_write_p
2aec0 61 67 65 6c 69 73 74 28 70 50 61 67 65 72 2c 20 agelist(pPager,
2aed0 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a pPg);. }. }.
2aee0 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 . /* Mark the p
2aef0 61 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 2a 2f age as clean. */
2af00 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
2af10 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 50 41 47 45 E_OK ){. PAGE
2af20 52 54 52 41 43 45 28 28 22 53 54 52 45 53 53 20 RTRACE(("STRESS
2af30 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 %d page %d\n", P
2af40 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 AGERID(pPager),
2af50 70 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 pPg->pgno));.
2af60 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 sqlite3PcacheMa
2af70 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 keClean(pPg);.
2af80 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 61 67 65 }.. return page
2af90 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 r_error(pPager,
2afa0 72 63 29 3b 20 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 rc); .}.../*.**
2afb0 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e 69 Allocate and ini
2afc0 74 69 61 6c 69 7a 65 20 61 20 6e 65 77 20 50 61 tialize a new Pa
2afd0 67 65 72 20 6f 62 6a 65 63 74 20 61 6e 64 20 70 ger object and p
2afe0 75 74 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ut a pointer to
2aff0 69 74 0a 2a 2a 20 69 6e 20 2a 70 70 50 61 67 65 it.** in *ppPage
2b000 72 2e 20 54 68 65 20 70 61 67 65 72 20 73 68 6f r. The pager sho
2b010 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c 79 20 62 uld eventually b
2b020 65 20 66 72 65 65 64 20 62 79 20 70 61 73 73 69 e freed by passi
2b030 6e 67 20 69 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 ng it.** to sqli
2b040 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 29 2e te3PagerClose().
2b050 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 46 69 6c 65 .**.** The zFile
2b060 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73 name argument is
2b070 20 74 68 65 20 70 61 74 68 20 74 6f 20 74 68 65 the path to the
2b080 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
2b090 6f 20 6f 70 65 6e 2e 0a 2a 2a 20 49 66 20 7a 46 o open..** If zF
2b0a0 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 20 ilename is NULL
2b0b0 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d 6c 79 2d then a randomly-
2b0c0 6e 61 6d 65 64 20 74 65 6d 70 6f 72 61 72 79 20 named temporary
2b0d0 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 0a file is created.
2b0e0 2a 2a 20 61 6e 64 20 75 73 65 64 20 61 73 20 74 ** and used as t
2b0f0 68 65 20 66 69 6c 65 20 74 6f 20 62 65 20 63 61 he file to be ca
2b100 63 68 65 64 2e 20 54 65 6d 70 6f 72 61 72 79 20 ched. Temporary
2b110 66 69 6c 65 73 20 61 72 65 20 62 65 20 64 65 6c files are be del
2b120 65 74 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 eted.** automati
2b130 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 79 20 cally when they
2b140 61 72 65 20 63 6c 6f 73 65 64 2e 20 49 66 20 7a are closed. If z
2b150 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 Filename is ":me
2b160 6d 6f 72 79 3a 22 20 74 68 65 6e 20 0a 2a 2a 20 mory:" then .**
2b170 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 all information
2b180 69 73 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 is held in cache
2b190 2e 20 49 74 20 69 73 20 6e 65 76 65 72 20 77 72 . It is never wr
2b1a0 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 20 0a itten to disk. .
2b1b0 2a 2a 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 ** This can be u
2b1c0 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 sed to implement
2b1d0 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
2b1e0 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 tabase..**.** Th
2b1f0 65 20 6e 45 78 74 72 61 20 70 61 72 61 6d 65 74 e nExtra paramet
2b200 65 72 20 73 70 65 63 69 66 69 65 73 20 74 68 65 er specifies the
2b210 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
2b220 20 6f 66 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 of space alloca
2b230 74 65 64 0a 2a 2a 20 61 6c 6f 6e 67 20 77 69 74 ted.** along wit
2b240 68 20 65 61 63 68 20 70 61 67 65 20 72 65 66 65 h each page refe
2b250 72 65 6e 63 65 2e 20 54 68 69 73 20 73 70 61 63 rence. This spac
2b260 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 74 e is available t
2b270 6f 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 76 69 o the user.** vi
2b280 61 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 a the sqlite3Pag
2b290 65 72 47 65 74 45 78 74 72 61 28 29 20 41 50 49 erGetExtra() API
2b2a0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 ..**.** The flag
2b2b0 73 20 61 72 67 75 6d 65 6e 74 20 69 73 20 75 73 s argument is us
2b2c0 65 64 20 74 6f 20 73 70 65 63 69 66 79 20 70 72 ed to specify pr
2b2d0 6f 70 65 72 74 69 65 73 20 74 68 61 74 20 61 66 operties that af
2b2e0 66 65 63 74 20 74 68 65 0a 2a 2a 20 6f 70 65 72 fect the.** oper
2b2f0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 ation of the pag
2b300 65 72 2e 20 49 74 20 73 68 6f 75 6c 64 20 62 65 er. It should be
2b310 20 70 61 73 73 65 64 20 73 6f 6d 65 20 62 69 74 passed some bit
2b320 77 69 73 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e wise combination
2b330 0a 2a 2a 20 6f 66 20 74 68 65 20 50 41 47 45 52 .** of the PAGER
2b340 5f 2a 20 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 _* flags..**.**
2b350 54 68 65 20 76 66 73 46 6c 61 67 73 20 70 61 72 The vfsFlags par
2b360 61 6d 65 74 65 72 20 69 73 20 61 20 62 69 74 6d ameter is a bitm
2b370 61 73 6b 20 74 6f 20 70 61 73 73 20 74 6f 20 74 ask to pass to t
2b380 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 he flags paramet
2b390 65 72 0a 2a 2a 20 6f 66 20 74 68 65 20 78 4f 70 er.** of the xOp
2b3a0 65 6e 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 en() method of t
2b3b0 68 65 20 73 75 70 70 6c 69 65 64 20 56 46 53 20 he supplied VFS
2b3c0 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 66 69 6c when opening fil
2b3d0 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 es. .**.** If th
2b3e0 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 69 e pager object i
2b3f0 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 s allocated and
2b400 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 the specified fi
2b410 6c 65 20 6f 70 65 6e 65 64 20 0a 2a 2a 20 73 75 le opened .** su
2b420 63 63 65 73 73 66 75 6c 6c 79 2c 20 53 51 4c 49 ccessfully, SQLI
2b430 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
2b440 64 20 61 6e 64 20 2a 70 70 50 61 67 65 72 20 73 d and *ppPager s
2b450 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 0a 2a et to point to.*
2b460 2a 20 74 68 65 20 6e 65 77 20 70 61 67 65 72 20 * the new pager
2b470 6f 62 6a 65 63 74 2e 20 49 66 20 61 6e 20 65 72 object. If an er
2b480 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70 70 50 ror occurs, *ppP
2b490 61 67 65 72 20 69 73 20 73 65 74 20 74 6f 20 4e ager is set to N
2b4a0 55 4c 4c 0a 2a 2a 20 61 6e 64 20 65 72 72 6f 72 ULL.** and error
2b4b0 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 2e 20 code returned.
2b4c0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 This function ma
2b4d0 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f y return SQLITE_
2b4e0 4e 4f 4d 45 4d 0a 2a 2a 20 28 73 71 6c 69 74 65 NOMEM.** (sqlite
2b4f0 33 4d 61 6c 6c 6f 63 28 29 20 69 73 20 75 73 65 3Malloc() is use
2b500 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 d to allocate me
2b510 6d 6f 72 79 29 2c 20 53 51 4c 49 54 45 5f 43 41 mory), SQLITE_CA
2b520 4e 54 4f 50 45 4e 20 6f 72 20 0a 2a 2a 20 76 61 NTOPEN or .** va
2b530 72 69 6f 75 73 20 53 51 4c 49 54 45 5f 49 4f 5f rious SQLITE_IO_
2b540 58 58 58 20 65 72 72 6f 72 73 2e 0a 2a 2f 0a 69 XXX errors..*/.i
2b550 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f nt sqlite3PagerO
2b560 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 pen(. sqlite3_v
2b570 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 fs *pVfs,
2b580 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 66 /* The virtual f
2b590 69 6c 65 20 73 79 73 74 65 6d 20 74 6f 20 75 73 ile system to us
2b5a0 65 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 2a 70 e */. Pager **p
2b5b0 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 pPager,
2b5c0 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 74 /* OUT: Return t
2b5d0 68 65 20 50 61 67 65 72 20 73 74 72 75 63 74 75 he Pager structu
2b5e0 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e re here */. con
2b5f0 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
2b600 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 me, /* Name of
2b610 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
2b620 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 le to open */.
2b630 69 6e 74 20 6e 45 78 74 72 61 2c 20 20 20 20 20 int nExtra,
2b640 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 /* Extr
2b650 61 20 62 79 74 65 73 20 61 70 70 65 6e 64 20 74 a bytes append t
2b660 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 o each in-memory
2b670 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 66 page */. int f
2b680 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 lags,
2b690 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 63 6f 6e /* flags con
2b6a0 74 72 6f 6c 6c 69 6e 67 20 74 68 69 73 20 66 69 trolling this fi
2b6b0 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 le */. int vfsF
2b6c0 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 lags,
2b6d0 20 2f 2a 20 66 6c 61 67 73 20 70 61 73 73 65 64 /* flags passed
2b6e0 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71 6c 69 through to sqli
2b6f0 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 te3_vfs.xOpen()
2b700 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 69 */. void (*xRei
2b710 6e 69 74 29 28 44 62 50 61 67 65 2a 29 20 2f 2a nit)(DbPage*) /*
2b720 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 72 65 69 Function to rei
2b730 6e 69 74 69 61 6c 69 7a 65 20 70 61 67 65 73 20 nitialize pages
2b740 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 70 50 74 72 */.){. u8 *pPtr
2b750 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 ;. Pager *pPage
2b760 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 r = 0; /*
2b770 50 61 67 65 72 20 6f 62 6a 65 63 74 20 74 6f 20 Pager object to
2b780 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 74 allocate and ret
2b790 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 urn */. int rc
2b7a0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
2b7b0 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
2b7c0 20 2a 2f 0a 20 20 69 6e 74 20 74 65 6d 70 46 69 */. int tempFi
2b7d0 6c 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f le = 0; /
2b7e0 2a 20 54 72 75 65 20 66 6f 72 20 74 65 6d 70 20 * True for temp
2b7f0 66 69 6c 65 73 20 28 69 6e 63 6c 2e 20 69 6e 2d files (incl. in-
2b800 6d 65 6d 6f 72 79 20 66 69 6c 65 73 29 20 2a 2f memory files) */
2b810 0a 20 20 69 6e 74 20 6d 65 6d 44 62 20 3d 20 30 . int memDb = 0
2b820 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
2b830 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 rue if this is a
2b840 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 n in-memory file
2b850 20 2a 2f 0a 20 20 69 6e 74 20 72 65 61 64 4f 6e */. int readOn
2b860 6c 79 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f ly = 0; /
2b870 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 * True if this i
2b880 73 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 66 69 s a read-only fi
2b890 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6a 6f 75 72 le */. int jour
2b8a0 6e 61 6c 46 69 6c 65 53 69 7a 65 3b 20 20 20 20 nalFileSize;
2b8b0 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 6c 6c /* Bytes to all
2b8c0 6f 63 61 74 65 20 66 6f 72 20 65 61 63 68 20 6a ocate for each j
2b8d0 6f 75 72 6e 61 6c 20 66 64 20 2a 2f 0a 20 20 63 ournal fd */. c
2b8e0 68 61 72 20 2a 7a 50 61 74 68 6e 61 6d 65 20 3d har *zPathname =
2b8f0 20 30 3b 20 20 20 20 20 2f 2a 20 46 75 6c 6c 20 0; /* Full
2b900 70 61 74 68 20 74 6f 20 64 61 74 61 62 61 73 65 path to database
2b910 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e file */. int n
2b920 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20 20 20 Pathname = 0;
2b930 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
2b940 20 62 79 74 65 73 20 69 6e 20 7a 50 61 74 68 6e bytes in zPathn
2b950 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73 65 ame */. int use
2b960 4a 6f 75 72 6e 61 6c 20 3d 20 28 66 6c 61 67 73 Journal = (flags
2b970 20 26 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f & PAGER_OMIT_JO
2b980 55 52 4e 41 4c 29 3d 3d 30 3b 20 2f 2a 20 46 61 URNAL)==0; /* Fa
2b990 6c 73 65 20 74 6f 20 6f 6d 69 74 20 6a 6f 75 72 lse to omit jour
2b9a0 6e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 70 63 61 nal */. int pca
2b9b0 63 68 65 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 cheSize = sqlite
2b9c0 33 50 63 61 63 68 65 53 69 7a 65 28 29 3b 20 20 3PcacheSize();
2b9d0 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 74 6f /* Bytes to
2b9e0 20 61 6c 6c 6f 63 61 74 65 20 66 6f 72 20 50 43 allocate for PC
2b9f0 61 63 68 65 20 2a 2f 0a 20 20 75 33 32 20 73 7a ache */. u32 sz
2ba00 50 61 67 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 PageDflt = SQLIT
2ba10 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 E_DEFAULT_PAGE_S
2ba20 49 5a 45 3b 20 20 2f 2a 20 44 65 66 61 75 6c 74 IZE; /* Default
2ba30 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 page size */.
2ba40 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 72 69 const char *zUri
2ba50 20 3d 20 30 3b 20 20 20 20 2f 2a 20 55 52 49 20 = 0; /* URI
2ba60 61 72 67 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a args to copy */.
2ba70 20 20 69 6e 74 20 6e 55 72 69 20 3d 20 30 3b 20 int nUri = 0;
2ba80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
2ba90 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
2baa0 20 55 52 49 20 61 72 67 73 20 61 74 20 2a 7a 55 URI args at *zU
2bab0 72 69 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 ri */.. /* Figu
2bac0 72 65 20 6f 75 74 20 68 6f 77 20 6d 75 63 68 20 re out how much
2bad0 73 70 61 63 65 20 69 73 20 72 65 71 75 69 72 65 space is require
2bae0 64 20 66 6f 72 20 65 61 63 68 20 6a 6f 75 72 6e d for each journ
2baf0 61 6c 20 66 69 6c 65 2d 68 61 6e 64 6c 65 0a 20 al file-handle.
2bb00 20 2a 2a 20 28 74 68 65 72 65 20 61 72 65 20 74 ** (there are t
2bb10 77 6f 20 6f 66 20 74 68 65 6d 2c 20 74 68 65 20 wo of them, the
2bb20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 main journal and
2bb30 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
2bb40 29 2e 20 54 68 69 73 0a 20 20 2a 2a 20 69 73 20 ). This. ** is
2bb50 74 68 65 20 6d 61 78 69 6d 75 6d 20 73 70 61 63 the maximum spac
2bb60 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 e required for a
2bb70 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 n in-memory jour
2bb80 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 nal file handle
2bb90 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 65 67 75 . ** and a regu
2bba0 6c 61 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 lar journal file
2bbb0 2d 68 61 6e 64 6c 65 2e 20 4e 6f 74 65 20 74 68 -handle. Note th
2bbc0 61 74 20 61 20 22 72 65 67 75 6c 61 72 20 6a 6f at a "regular jo
2bbd0 75 72 6e 61 6c 2d 68 61 6e 64 6c 65 22 0a 20 20 urnal-handle".
2bbe0 2a 2a 20 6d 61 79 20 62 65 20 61 20 77 72 61 70 ** may be a wrap
2bbf0 70 65 72 20 63 61 70 61 62 6c 65 20 6f 66 20 63 per capable of c
2bc00 61 63 68 69 6e 67 20 74 68 65 20 66 69 72 73 74 aching the first
2bc10 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 portion of the
2bc20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c journal. ** fil
2bc30 65 20 69 6e 20 6d 65 6d 6f 72 79 20 74 6f 20 69 e in memory to i
2bc40 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 61 74 6f mplement the ato
2bc50 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 mic-write optimi
2bc60 7a 61 74 69 6f 6e 20 28 73 65 65 20 0a 20 20 2a zation (see . *
2bc70 2a 20 73 6f 75 72 63 65 20 66 69 6c 65 20 6a 6f * source file jo
2bc80 75 72 6e 61 6c 2e 63 29 2e 0a 20 20 2a 2f 0a 20 urnal.c).. */.
2bc90 20 69 66 28 20 73 71 6c 69 74 65 33 4a 6f 75 72 if( sqlite3Jour
2bca0 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 3e 73 71 nalSize(pVfs)>sq
2bcb0 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 lite3MemJournalS
2bcc0 69 7a 65 28 29 20 29 7b 0a 20 20 20 20 6a 6f 75 ize() ){. jou
2bcd0 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 3d 20 52 rnalFileSize = R
2bce0 4f 55 4e 44 38 28 73 71 6c 69 74 65 33 4a 6f 75 OUND8(sqlite3Jou
2bcf0 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 29 3b rnalSize(pVfs));
2bd00 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6a 6f . }else{. jo
2bd10 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 3d 20 urnalFileSize =
2bd20 52 4f 55 4e 44 38 28 73 71 6c 69 74 65 33 4d 65 ROUND8(sqlite3Me
2bd30 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 29 3b mJournalSize());
2bd40 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 . }.. /* Set t
2bd50 68 65 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 he output variab
2bd60 6c 65 20 74 6f 20 4e 55 4c 4c 20 69 6e 20 63 61 le to NULL in ca
2bd70 73 65 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 se an error occu
2bd80 72 73 2e 20 2a 2f 0a 20 20 2a 70 70 50 61 67 65 rs. */. *ppPage
2bd90 72 20 3d 20 30 3b 0a 0a 23 69 66 6e 64 65 66 20 r = 0;..#ifndef
2bda0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f SQLITE_OMIT_MEMO
2bdb0 52 59 44 42 0a 20 20 69 66 28 20 66 6c 61 67 73 RYDB. if( flags
2bdc0 20 26 20 50 41 47 45 52 5f 4d 45 4d 4f 52 59 20 & PAGER_MEMORY
2bdd0 29 7b 0a 20 20 20 20 6d 65 6d 44 62 20 3d 20 31 ){. memDb = 1
2bde0 3b 0a 20 20 20 20 69 66 28 20 7a 46 69 6c 65 6e ;. if( zFilen
2bdf0 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 ame && zFilename
2be00 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 7a 50 61 [0] ){. zPa
2be10 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 thname = sqlite3
2be20 44 62 53 74 72 44 75 70 28 30 2c 20 7a 46 69 6c DbStrDup(0, zFil
2be30 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 ename);. if
2be40 28 20 7a 50 61 74 68 6e 61 6d 65 3d 3d 30 20 20 ( zPathname==0
2be50 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
2be60 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 6e 50 61 NOMEM;. nPa
2be70 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 thname = sqlite3
2be80 53 74 72 6c 65 6e 33 30 28 7a 50 61 74 68 6e 61 Strlen30(zPathna
2be90 6d 65 29 3b 0a 20 20 20 20 20 20 7a 46 69 6c 65 me);. zFile
2bea0 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a name = 0;. }.
2beb0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a }.#endif.. /*
2bec0 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 73 74 6f Compute and sto
2bed0 72 65 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 re the full path
2bee0 6e 61 6d 65 20 69 6e 20 61 6e 20 61 6c 6c 6f 63 name in an alloc
2bef0 61 74 65 64 20 62 75 66 66 65 72 20 70 6f 69 6e ated buffer poin
2bf00 74 65 64 0a 20 20 2a 2a 20 74 6f 20 62 79 20 7a ted. ** to by z
2bf10 50 61 74 68 6e 61 6d 65 2c 20 6c 65 6e 67 74 68 Pathname, length
2bf20 20 6e 50 61 74 68 6e 61 6d 65 2e 20 4f 72 2c 20 nPathname. Or,
2bf30 69 66 20 74 68 69 73 20 69 73 20 61 20 74 65 6d if this is a tem
2bf40 70 6f 72 61 72 79 20 66 69 6c 65 2c 0a 20 20 2a porary file,. *
2bf50 2a 20 6c 65 61 76 65 20 62 6f 74 68 20 6e 50 61 * leave both nPa
2bf60 74 68 6e 61 6d 65 20 61 6e 64 20 7a 50 61 74 68 thname and zPath
2bf70 6e 61 6d 65 20 73 65 74 20 74 6f 20 30 2e 0a 20 name set to 0..
2bf80 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e */. if( zFilen
2bf90 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 ame && zFilename
2bfa0 5b 30 5d 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 [0] ){. const
2bfb0 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 6e 50 char *z;. nP
2bfc0 61 74 68 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e athname = pVfs->
2bfd0 6d 78 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 mxPathname+1;.
2bfe0 20 20 7a 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 zPathname = sq
2bff0 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
2c000 28 30 2c 20 6e 50 61 74 68 6e 61 6d 65 2a 32 29 (0, nPathname*2)
2c010 3b 0a 20 20 20 20 69 66 28 20 7a 50 61 74 68 6e ;. if( zPathn
2c020 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ame==0 ){.
2c030 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
2c040 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a MEM;. }. z
2c050 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b Pathname[0] = 0;
2c060 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 69 6e /* Make sure in
2c070 69 74 69 61 6c 69 7a 65 64 20 65 76 65 6e 20 69 itialized even i
2c080 66 20 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 f FullPathname()
2c090 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 72 63 fails */. rc
2c0a0 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c = sqlite3OsFull
2c0b0 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a Pathname(pVfs, z
2c0c0 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74 68 6e Filename, nPathn
2c0d0 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 29 3b ame, zPathname);
2c0e0 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d . nPathname =
2c0f0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
2c100 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 (zPathname);.
2c110 20 7a 20 3d 20 7a 55 72 69 20 3d 20 26 7a 46 69 z = zUri = &zFi
2c120 6c 65 6e 61 6d 65 5b 73 71 6c 69 74 65 33 53 74 lename[sqlite3St
2c130 72 6c 65 6e 33 30 28 7a 46 69 6c 65 6e 61 6d 65 rlen30(zFilename
2c140 29 2b 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 )+1];. while(
2c150 20 2a 7a 20 29 7b 0a 20 20 20 20 20 20 7a 20 2b *z ){. z +
2c160 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
2c170 30 28 7a 29 2b 31 3b 0a 20 20 20 20 20 20 7a 20 0(z)+1;. z
2c180 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e += sqlite3Strlen
2c190 33 30 28 7a 29 2b 31 3b 0a 20 20 20 20 7d 0a 20 30(z)+1;. }.
2c1a0 20 20 20 6e 55 72 69 20 3d 20 28 69 6e 74 29 28 nUri = (int)(
2c1b0 26 7a 5b 31 5d 20 2d 20 7a 55 72 69 29 3b 0a 20 &z[1] - zUri);.
2c1c0 20 20 20 61 73 73 65 72 74 28 20 6e 55 72 69 3e assert( nUri>
2c1d0 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 =0 );. if( rc
2c1e0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e ==SQLITE_OK && n
2c1f0 50 61 74 68 6e 61 6d 65 2b 38 3e 70 56 66 73 2d Pathname+8>pVfs-
2c200 3e 6d 78 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 >mxPathname ){.
2c210 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 /* This bra
2c220 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 77 68 65 nch is taken whe
2c230 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 70 61 n the journal pa
2c240 74 68 20 72 65 71 75 69 72 65 64 20 62 79 0a 20 th required by.
2c250 20 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 ** the data
2c260 62 61 73 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 base being opene
2c270 64 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 74 d will be more t
2c280 68 61 6e 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 han pVfs->mxPath
2c290 6e 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 name. ** by
2c2a0 74 65 73 20 69 6e 20 6c 65 6e 67 74 68 2e 20 54 tes in length. T
2c2b0 68 69 73 20 6d 65 61 6e 73 20 74 68 65 20 64 61 his means the da
2c2c0 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 tabase cannot be
2c2d0 20 6f 70 65 6e 65 64 2c 0a 20 20 20 20 20 20 2a opened,. *
2c2e0 2a 20 61 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 * as it will not
2c2f0 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 be possible to
2c300 6f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c open the journal
2c310 20 66 69 6c 65 20 6f 72 20 65 76 65 6e 0a 20 20 file or even.
2c320 20 20 20 20 2a 2a 20 63 68 65 63 6b 20 66 6f 72 ** check for
2c330 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 a hot-journal b
2c340 65 66 6f 72 65 20 72 65 61 64 69 6e 67 2e 0a 20 efore reading..
2c350 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 */. rc
2c360 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 = SQLITE_CANTOP
2c370 45 4e 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 EN_BKPT;. }.
2c380 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
2c390 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 E_OK ){. sq
2c3a0 6c 69 74 65 33 44 62 46 72 65 65 28 30 2c 20 7a lite3DbFree(0, z
2c3b0 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 Pathname);.
2c3c0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
2c3d0 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f }. }.. /* Allo
2c3e0 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 cate memory for
2c3f0 74 68 65 20 50 61 67 65 72 20 73 74 72 75 63 74 the Pager struct
2c400 75 72 65 2c 20 50 43 61 63 68 65 20 6f 62 6a 65 ure, PCache obje
2c410 63 74 2c 20 74 68 65 0a 20 20 2a 2a 20 74 68 72 ct, the. ** thr
2c420 65 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ee file descript
2c430 6f 72 73 2c 20 74 68 65 20 64 61 74 61 62 61 73 ors, the databas
2c440 65 20 66 69 6c 65 20 6e 61 6d 65 20 61 6e 64 20 e file name and
2c450 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a the journal . *
2c460 2a 20 66 69 6c 65 20 6e 61 6d 65 2e 20 54 68 65 * file name. The
2c470 20 6c 61 79 6f 75 74 20 69 6e 20 6d 65 6d 6f 72 layout in memor
2c480 79 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a y is as follows:
2c490 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 50 . **. ** P
2c4a0 61 67 65 72 20 6f 62 6a 65 63 74 20 20 20 20 20 ager object
2c4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
2c4c0 73 69 7a 65 6f 66 28 50 61 67 65 72 29 20 62 79 sizeof(Pager) by
2c4d0 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 50 43 tes). ** PC
2c4e0 61 63 68 65 20 6f 62 6a 65 63 74 20 20 20 20 20 ache object
2c4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 73 (s
2c500 71 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 qlite3PcacheSize
2c510 28 29 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 () bytes). **
2c520 20 20 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 Database file
2c530 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20 handle
2c540 20 20 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 (pVfs->szOsFi
2c550 6c 65 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 le bytes). **
2c560 20 20 20 53 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 Sub-journal f
2c570 69 6c 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20 ile handle
2c580 20 20 20 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 (journalFileS
2c590 69 7a 65 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 ize bytes). **
2c5a0 20 20 20 20 4d 61 69 6e 20 6a 6f 75 72 6e 61 6c Main journal
2c5b0 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 20 20 20 file handle
2c5c0 20 20 20 20 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 (journalFile
2c5d0 53 69 7a 65 20 62 79 74 65 73 29 0a 20 20 2a 2a Size bytes). **
2c5e0 20 20 20 20 20 44 61 74 61 62 61 73 65 20 66 69 Database fi
2c5f0 6c 65 20 6e 61 6d 65 20 20 20 20 20 20 20 20 20 le name
2c600 20 20 20 20 20 28 6e 50 61 74 68 6e 61 6d 65 2b (nPathname+
2c610 31 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 1 bytes). **
2c620 20 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e Journal file n
2c630 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ame
2c640 20 20 28 6e 50 61 74 68 6e 61 6d 65 2b 38 2b 31 (nPathname+8+1
2c650 20 62 79 74 65 73 29 0a 20 20 2a 2f 0a 20 20 70 bytes). */. p
2c660 50 74 72 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 Ptr = (u8 *)sqli
2c670 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 0a 20 te3MallocZero(.
2c680 20 20 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 ROUND8(sizeof
2c690 28 2a 70 50 61 67 65 72 29 29 20 2b 20 20 20 20 (*pPager)) +
2c6a0 20 20 2f 2a 20 50 61 67 65 72 20 73 74 72 75 63 /* Pager struc
2c6b0 74 75 72 65 20 2a 2f 0a 20 20 20 20 52 4f 55 4e ture */. ROUN
2c6c0 44 38 28 70 63 61 63 68 65 53 69 7a 65 29 20 2b D8(pcacheSize) +
2c6d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 43 /* PC
2c6e0 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 ache object */.
2c6f0 20 20 20 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e ROUND8(pVfs->
2c700 73 7a 4f 73 46 69 6c 65 29 20 2b 20 20 20 20 20 szOsFile) +
2c710 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 62 /* The main db
2c720 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 file */. jou
2c730 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 2a 20 32 rnalFileSize * 2
2c740 20 2b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 + /* T
2c750 68 65 20 74 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 he two journal f
2c760 69 6c 65 73 20 2a 2f 20 0a 20 20 20 20 6e 50 61 iles */ . nPa
2c770 74 68 6e 61 6d 65 20 2b 20 31 20 2b 20 6e 55 72 thname + 1 + nUr
2c780 69 20 2b 20 20 20 20 20 20 20 20 20 2f 2a 20 7a i + /* z
2c790 46 69 6c 65 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 Filename */.
2c7a0 6e 50 61 74 68 6e 61 6d 65 20 2b 20 38 20 2b 20 nPathname + 8 +
2c7b0 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2 /
2c7c0 2a 20 7a 4a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 69 * zJournal */.#i
2c7d0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
2c7e0 54 5f 57 41 4c 0a 20 20 20 20 2b 20 6e 50 61 74 T_WAL. + nPat
2c7f0 68 6e 61 6d 65 20 2b 20 34 20 2b 20 32 20 20 20 hname + 4 + 2
2c800 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 57 61 6c /* zWal
2c810 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a */.#endif. );.
2c820 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f assert( EIGHT_
2c830 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 53 BYTE_ALIGNMENT(S
2c840 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 QLITE_INT_TO_PTR
2c850 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 (journalFileSize
2c860 29 29 20 29 3b 0a 20 20 69 66 28 20 21 70 50 74 )) );. if( !pPt
2c870 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 r ){. sqlite3
2c880 44 62 46 72 65 65 28 30 2c 20 7a 50 61 74 68 6e DbFree(0, zPathn
2c890 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ame);. return
2c8a0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
2c8b0 20 7d 0a 20 20 70 50 61 67 65 72 20 3d 20 20 20 }. pPager =
2c8c0 20 20 20 20 20 20 20 20 20 20 20 28 50 61 67 65 (Page
2c8d0 72 2a 29 28 70 50 74 72 29 3b 0a 20 20 70 50 61 r*)(pPtr);. pPa
2c8e0 67 65 72 2d 3e 70 50 43 61 63 68 65 20 3d 20 20 ger->pPCache =
2c8f0 20 20 28 50 43 61 63 68 65 2a 29 28 70 50 74 72 (PCache*)(pPtr
2c900 20 2b 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f += ROUND8(sizeo
2c910 66 28 2a 70 50 61 67 65 72 29 29 29 3b 0a 20 20 f(*pPager)));.
2c920 70 50 61 67 65 72 2d 3e 66 64 20 3d 20 20 20 28 pPager->fd = (
2c930 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 sqlite3_file*)(p
2c940 50 74 72 20 2b 3d 20 52 4f 55 4e 44 38 28 70 63 Ptr += ROUND8(pc
2c950 61 63 68 65 53 69 7a 65 29 29 3b 0a 20 20 70 50 acheSize));. pP
2c960 61 67 65 72 2d 3e 73 6a 66 64 20 3d 20 28 73 71 ager->sjfd = (sq
2c970 6c 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 lite3_file*)(pPt
2c980 72 20 2b 3d 20 52 4f 55 4e 44 38 28 70 56 66 73 r += ROUND8(pVfs
2c990 2d 3e 73 7a 4f 73 46 69 6c 65 29 29 3b 0a 20 20 ->szOsFile));.
2c9a0 70 50 61 67 65 72 2d 3e 6a 66 64 20 3d 20 20 28 pPager->jfd = (
2c9b0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 sqlite3_file*)(p
2c9c0 50 74 72 20 2b 3d 20 6a 6f 75 72 6e 61 6c 46 69 Ptr += journalFi
2c9d0 6c 65 53 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 leSize);. pPage
2c9e0 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 20 r->zFilename =
2c9f0 20 20 28 63 68 61 72 2a 29 28 70 50 74 72 20 2b (char*)(pPtr +
2ca00 3d 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a = journalFileSiz
2ca10 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 e);. assert( EI
2ca20 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
2ca30 4e 54 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 NT(pPager->jfd)
2ca40 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e );.. /* Fill in
2ca50 20 74 68 65 20 50 61 67 65 72 2e 7a 46 69 6c 65 the Pager.zFile
2ca60 6e 61 6d 65 20 61 6e 64 20 50 61 67 65 72 2e 7a name and Pager.z
2ca70 4a 6f 75 72 6e 61 6c 20 62 75 66 66 65 72 73 2c Journal buffers,
2ca80 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f if required. */
2ca90 0a 20 20 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 . if( zPathname
2caa0 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
2cab0 6e 50 61 74 68 6e 61 6d 65 3e 30 20 29 3b 0a 20 nPathname>0 );.
2cac0 20 20 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 pPager->zJour
2cad0 6e 61 6c 20 3d 20 20 20 28 63 68 61 72 2a 29 28 nal = (char*)(
2cae0 70 50 74 72 20 2b 3d 20 6e 50 61 74 68 6e 61 6d pPtr += nPathnam
2caf0 65 20 2b 20 31 20 2b 20 6e 55 72 69 29 3b 0a 20 e + 1 + nUri);.
2cb00 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72 memcpy(pPager
2cb10 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a 50 61 ->zFilename, zPa
2cb20 74 68 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d thname, nPathnam
2cb30 65 29 3b 0a 20 20 20 20 69 66 28 20 6e 55 72 69 e);. if( nUri
2cb40 20 29 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 ) memcpy(&pPage
2cb50 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 5b 6e 50 61 r->zFilename[nPa
2cb60 74 68 6e 61 6d 65 2b 31 5d 2c 20 7a 55 72 69 2c thname+1], zUri,
2cb70 20 6e 55 72 69 29 3b 0a 20 20 20 20 6d 65 6d 63 nUri);. memc
2cb80 70 79 28 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 py(pPager->zJour
2cb90 6e 61 6c 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20 nal, zPathname,
2cba0 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 nPathname);.
2cbb0 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e memcpy(&pPager->
2cbc0 7a 4a 6f 75 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 zJournal[nPathna
2cbd0 6d 65 5d 2c 20 22 2d 6a 6f 75 72 6e 61 6c 5c 30 me], "-journal\0
2cbe0 30 30 22 2c 20 38 2b 32 29 3b 0a 20 20 20 20 73 00", 8+2);. s
2cbf0 71 6c 69 74 65 33 46 69 6c 65 53 75 66 66 69 78 qlite3FileSuffix
2cc00 33 28 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 3(pPager->zFilen
2cc10 61 6d 65 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f ame, pPager->zJo
2cc20 75 72 6e 61 6c 29 3b 0a 23 69 66 6e 64 65 66 20 urnal);.#ifndef
2cc30 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a SQLITE_OMIT_WAL.
2cc40 20 20 20 20 70 50 61 67 65 72 2d 3e 7a 57 61 6c pPager->zWal
2cc50 20 3d 20 26 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 = &pPager->zJou
2cc60 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 6d 65 2b 38 rnal[nPathname+8
2cc70 2b 31 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 +1];. memcpy(
2cc80 70 50 61 67 65 72 2d 3e 7a 57 61 6c 2c 20 7a 50 pPager->zWal, zP
2cc90 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 athname, nPathna
2cca0 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 me);. memcpy(
2ccb0 26 70 50 61 67 65 72 2d 3e 7a 57 61 6c 5b 6e 50 &pPager->zWal[nP
2ccc0 61 74 68 6e 61 6d 65 5d 2c 20 22 2d 77 61 6c 5c athname], "-wal\
2ccd0 30 30 30 22 2c 20 34 2b 31 29 3b 0a 20 20 20 20 000", 4+1);.
2cce0 73 71 6c 69 74 65 33 46 69 6c 65 53 75 66 66 69 sqlite3FileSuffi
2ccf0 78 33 28 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 x3(pPager->zFile
2cd00 6e 61 6d 65 2c 20 70 50 61 67 65 72 2d 3e 7a 57 name, pPager->zW
2cd10 61 6c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 al);.#endif.
2cd20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 30 2c sqlite3DbFree(0,
2cd30 20 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 7d zPathname);. }
2cd40 0a 20 20 70 50 61 67 65 72 2d 3e 70 56 66 73 20 . pPager->pVfs
2cd50 3d 20 70 56 66 73 3b 0a 20 20 70 50 61 67 65 72 = pVfs;. pPager
2cd60 2d 3e 76 66 73 46 6c 61 67 73 20 3d 20 76 66 73 ->vfsFlags = vfs
2cd70 46 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 4f 70 65 Flags;.. /* Ope
2cd80 6e 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 n the pager file
2cd90 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 .. */. if( zFi
2cda0 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e lename && zFilen
2cdb0 61 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 20 69 6e ame[0] ){. in
2cdc0 74 20 66 6f 75 74 20 3d 20 30 3b 20 20 20 20 20 t fout = 0;
2cdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2cde0 2a 20 56 46 53 20 66 6c 61 67 73 20 72 65 74 75 * VFS flags retu
2cdf0 72 6e 65 64 20 62 79 20 78 4f 70 65 6e 28 29 20 rned by xOpen()
2ce00 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 */. rc = sqli
2ce10 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 te3OsOpen(pVfs,
2ce20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d pPager->zFilenam
2ce30 65 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 76 e, pPager->fd, v
2ce40 66 73 46 6c 61 67 73 2c 20 26 66 6f 75 74 29 3b fsFlags, &fout);
2ce50 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 6d 65 . assert( !me
2ce60 6d 44 62 20 29 3b 0a 20 20 20 20 72 65 61 64 4f mDb );. readO
2ce70 6e 6c 79 20 3d 20 28 66 6f 75 74 26 53 51 4c 49 nly = (fout&SQLI
2ce80 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
2ce90 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 );.. /* If th
2cea0 65 20 66 69 6c 65 20 77 61 73 20 73 75 63 63 65 e file was succe
2ceb0 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 20 66 ssfully opened f
2cec0 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 or read/write ac
2ced0 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20 63 68 6f cess,. ** cho
2cee0 6f 73 65 20 61 20 64 65 66 61 75 6c 74 20 70 61 ose a default pa
2cef0 67 65 20 73 69 7a 65 20 69 6e 20 63 61 73 65 20 ge size in case
2cf00 77 65 20 68 61 76 65 20 74 6f 20 63 72 65 61 74 we have to creat
2cf10 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 e the. ** dat
2cf20 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 abase file. The
2cf30 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a default page siz
2cf40 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d e is the maximum
2cf50 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 of:. **.
2cf60 2a 2a 20 20 20 20 2b 20 53 51 4c 49 54 45 5f 44 ** + SQLITE_D
2cf70 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
2cf80 2c 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 ,. ** + Th
2cf90 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
2cfa0 20 62 79 20 73 71 6c 69 74 65 33 4f 73 53 65 63 by sqlite3OsSec
2cfb0 74 6f 72 53 69 7a 65 28 29 0a 20 20 20 20 2a 2a torSize(). **
2cfc0 20 20 20 20 2b 20 54 68 65 20 6c 61 72 67 65 73 + The larges
2cfd0 74 20 70 61 67 65 20 73 69 7a 65 20 74 68 61 74 t page size that
2cfe0 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 can be written
2cff0 61 74 6f 6d 69 63 61 6c 6c 79 2e 0a 20 20 20 20 atomically..
2d000 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 */. if( rc==S
2d010 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 72 65 61 QLITE_OK && !rea
2d020 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 73 dOnly ){. s
2d030 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 etSectorSize(pPa
2d040 67 65 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65 ger);. asse
2d050 72 74 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c rt(SQLITE_DEFAUL
2d060 54 5f 50 41 47 45 5f 53 49 5a 45 3c 3d 53 51 4c T_PAGE_SIZE<=SQL
2d070 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f ITE_MAX_DEFAULT_
2d080 50 41 47 45 5f 53 49 5a 45 29 3b 0a 20 20 20 20 PAGE_SIZE);.
2d090 20 20 69 66 28 20 73 7a 50 61 67 65 44 66 6c 74 if( szPageDflt
2d0a0 3c 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 <pPager->sectorS
2d0b0 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ize ){. i
2d0c0 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f f( pPager->secto
2d0d0 72 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 rSize>SQLITE_MAX
2d0e0 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 _DEFAULT_PAGE_SI
2d0f0 5a 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ZE ){.
2d100 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 53 51 4c szPageDflt = SQL
2d110 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f ITE_MAX_DEFAULT_
2d120 50 41 47 45 5f 53 49 5a 45 3b 0a 20 20 20 20 20 PAGE_SIZE;.
2d130 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
2d140 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20 3d szPageDflt =
2d150 20 28 75 33 32 29 70 50 61 67 65 72 2d 3e 73 65 (u32)pPager->se
2d160 63 74 6f 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 ctorSize;.
2d170 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 69 66 64 }. }.#ifd
2d180 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
2d190 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 _ATOMIC_WRITE.
2d1a0 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69 6e {. in
2d1b0 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33 4f t iDc = sqlite3O
2d1c0 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 sDeviceCharacter
2d1d0 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 istics(pPager->f
2d1e0 64 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 d);. int
2d1f0 69 69 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ii;. asse
2d200 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f rt(SQLITE_IOCAP_
2d210 41 54 4f 4d 49 43 35 31 32 3d 3d 28 35 31 32 3e ATOMIC512==(512>
2d220 3e 38 29 29 3b 0a 20 20 20 20 20 20 20 20 61 73 >8));. as
2d230 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 sert(SQLITE_IOCA
2d240 50 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 35 P_ATOMIC64K==(65
2d250 35 33 36 3e 3e 38 29 29 3b 0a 20 20 20 20 20 20 536>>8));.
2d260 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f assert(SQLITE_
2d270 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 MAX_DEFAULT_PAGE
2d280 5f 53 49 5a 45 3c 3d 36 35 35 33 36 29 3b 0a 20 _SIZE<=65536);.
2d290 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d 73 7a for(ii=sz
2d2a0 50 61 67 65 44 66 6c 74 3b 20 69 69 3c 3d 53 51 PageDflt; ii<=SQ
2d2b0 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 LITE_MAX_DEFAULT
2d2c0 5f 50 41 47 45 5f 53 49 5a 45 3b 20 69 69 3d 69 _PAGE_SIZE; ii=i
2d2d0 69 2a 32 29 7b 0a 20 20 20 20 20 20 20 20 20 20 i*2){.
2d2e0 69 66 28 20 69 44 63 26 28 53 51 4c 49 54 45 5f if( iDc&(SQLITE_
2d2f0 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 69 69 IOCAP_ATOMIC|(ii
2d300 3e 3e 38 29 29 20 29 7b 0a 20 20 20 20 20 20 20 >>8)) ){.
2d310 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20 szPageDflt
2d320 3d 20 69 69 3b 0a 20 20 20 20 20 20 20 20 20 20 = ii;.
2d330 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
2d340 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d }.#endif. }
2d350 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
2d360 20 49 66 20 61 20 74 65 6d 70 6f 72 61 72 79 20 If a temporary
2d370 66 69 6c 65 20 69 73 20 72 65 71 75 65 73 74 65 file is requeste
2d380 64 2c 20 69 74 20 69 73 20 6e 6f 74 20 6f 70 65 d, it is not ope
2d390 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e ned immediately.
2d3a0 0a 20 20 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 . ** In this
2d3b0 63 61 73 65 20 77 65 20 61 63 63 65 70 74 20 74 case we accept t
2d3c0 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 he default page
2d3d0 73 69 7a 65 20 61 6e 64 20 64 65 6c 61 79 20 61 size and delay a
2d3e0 63 74 75 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 6f ctually. ** o
2d3f0 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20 pening the file
2d400 75 6e 74 69 6c 20 74 68 65 20 66 69 72 73 74 20 until the first
2d410 63 61 6c 6c 20 74 6f 20 4f 73 57 72 69 74 65 28 call to OsWrite(
2d420 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a ).. **. **
2d430 20 54 68 69 73 20 62 72 61 6e 63 68 20 69 73 20 This branch is
2d440 61 6c 73 6f 20 72 75 6e 20 66 6f 72 20 61 6e 20 also run for an
2d450 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
2d460 73 65 2e 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79 se. An in-memory
2d470 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 . ** database
2d480 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 is the same as
2d490 61 20 74 65 6d 70 2d 66 69 6c 65 20 74 68 61 74 a temp-file that
2d4a0 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 74 65 is never writte
2d4b0 6e 20 6f 75 74 20 74 6f 0a 20 20 20 20 2a 2a 20 n out to. **
2d4c0 64 69 73 6b 20 61 6e 64 20 75 73 65 73 20 61 6e disk and uses an
2d4d0 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 in-memory rollb
2d4e0 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 ack journal..
2d4f0 20 2a 2f 20 0a 20 20 20 20 74 65 6d 70 46 69 6c */ . tempFil
2d500 65 20 3d 20 31 3b 0a 20 20 20 20 70 50 61 67 65 e = 1;. pPage
2d510 72 2d 3e 65 53 74 61 74 65 20 3d 20 50 41 47 45 r->eState = PAGE
2d520 52 5f 52 45 41 44 45 52 3b 0a 20 20 20 20 70 50 R_READER;. pP
2d530 61 67 65 72 2d 3e 65 4c 6f 63 6b 20 3d 20 45 58 ager->eLock = EX
2d540 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 CLUSIVE_LOCK;.
2d550 20 20 72 65 61 64 4f 6e 6c 79 20 3d 20 28 76 66 readOnly = (vf
2d560 73 46 6c 61 67 73 26 53 51 4c 49 54 45 5f 4f 50 sFlags&SQLITE_OP
2d570 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 EN_READONLY);.
2d580 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c }.. /* The foll
2d590 6f 77 69 6e 67 20 63 61 6c 6c 20 74 6f 20 50 61 owing call to Pa
2d5a0 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 29 gerSetPagesize()
2d5b0 20 73 65 72 76 65 73 20 74 6f 20 73 65 74 20 74 serves to set t
2d5c0 68 65 20 76 61 6c 75 65 20 6f 66 20 0a 20 20 2a he value of . *
2d5d0 2a 20 50 61 67 65 72 2e 70 61 67 65 53 69 7a 65 * Pager.pageSize
2d5e0 20 61 6e 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 and to allocate
2d5f0 20 74 68 65 20 50 61 67 65 72 2e 70 54 6d 70 53 the Pager.pTmpS
2d600 70 61 63 65 20 62 75 66 66 65 72 2e 0a 20 20 2a pace buffer.. *
2d610 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 /. if( rc==SQLI
2d620 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 TE_OK ){. ass
2d630 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6d 65 6d ert( pPager->mem
2d640 44 62 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 Db==0 );. rc
2d650 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 = sqlite3PagerSe
2d660 74 50 61 67 65 73 69 7a 65 28 70 50 61 67 65 72 tPagesize(pPager
2d670 2c 20 26 73 7a 50 61 67 65 44 66 6c 74 2c 20 2d , &szPageDflt, -
2d680 31 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 1);. testcase
2d690 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
2d6a0 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 );. }.. /* If
2d6b0 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 an error occurre
2d6c0 64 20 69 6e 20 65 69 74 68 65 72 20 6f 66 20 74 d in either of t
2d6d0 68 65 20 62 6c 6f 63 6b 73 20 61 62 6f 76 65 2c he blocks above,
2d6e0 20 66 72 65 65 20 74 68 65 20 0a 20 20 2a 2a 20 free the . **
2d6f0 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 Pager structure
2d700 61 6e 64 20 63 6c 6f 73 65 20 74 68 65 20 66 69 and close the fi
2d710 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 le.. */. if( r
2d720 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
2d730 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 assert( !pPa
2d740 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 ger->pTmpSpace )
2d750 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 ;. sqlite3OsC
2d760 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 66 64 29 lose(pPager->fd)
2d770 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 ;. sqlite3_fr
2d780 65 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 ee(pPager);.
2d790 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a return rc;. }..
2d7a0 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 /* Initialize
2d7b0 74 68 65 20 50 43 61 63 68 65 20 6f 62 6a 65 63 the PCache objec
2d7c0 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 t. */. assert(
2d7d0 6e 45 78 74 72 61 3c 31 30 30 30 20 29 3b 0a 20 nExtra<1000 );.
2d7e0 20 6e 45 78 74 72 61 20 3d 20 52 4f 55 4e 44 38 nExtra = ROUND8
2d7f0 28 6e 45 78 74 72 61 29 3b 0a 20 20 73 71 6c 69 (nExtra);. sqli
2d800 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28 73 7a te3PcacheOpen(sz
2d810 50 61 67 65 44 66 6c 74 2c 20 6e 45 78 74 72 61 PageDflt, nExtra
2d820 2c 20 21 6d 65 6d 44 62 2c 0a 20 20 20 20 20 20 , !memDb,.
2d830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 6d !m
2d840 65 6d 44 62 3f 70 61 67 65 72 53 74 72 65 73 73 emDb?pagerStress
2d850 3a 30 2c 20 28 76 6f 69 64 20 2a 29 70 50 61 67 :0, (void *)pPag
2d860 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 50 43 61 er, pPager->pPCa
2d870 63 68 65 29 3b 0a 0a 20 20 50 41 47 45 52 54 52 che);.. PAGERTR
2d880 41 43 45 28 28 22 4f 50 45 4e 20 25 64 20 25 73 ACE(("OPEN %d %s
2d890 5c 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c 45 49 \n", FILEHANDLEI
2d8a0 44 28 70 50 61 67 65 72 2d 3e 66 64 29 2c 20 70 D(pPager->fd), p
2d8b0 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 Pager->zFilename
2d8c0 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 ));. IOTRACE(("
2d8d0 4f 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c 20 70 OPEN %p %s\n", p
2d8e0 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 7a Pager, pPager->z
2d8f0 46 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20 70 50 Filename)).. pP
2d900 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c ager->useJournal
2d910 20 3d 20 28 75 38 29 75 73 65 4a 6f 75 72 6e 61 = (u8)useJourna
2d920 6c 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e l;. /* pPager->
2d930 73 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 20 2a 2f stmtOpen = 0; */
2d940 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 . /* pPager->st
2d950 6d 74 49 6e 55 73 65 20 3d 20 30 3b 20 2a 2f 0a mtInUse = 0; */.
2d960 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 52 65 /* pPager->nRe
2d970 66 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 f = 0; */. /* p
2d980 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 20 Pager->stmtSize
2d990 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 = 0; */. /* pPa
2d9a0 67 65 72 2d 3e 73 74 6d 74 4a 53 69 7a 65 20 3d ger->stmtJSize =
2d9b0 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 0; */. /* pPag
2d9c0 65 72 2d 3e 6e 50 61 67 65 20 3d 20 30 3b 20 2a er->nPage = 0; *
2d9d0 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 /. pPager->mxPg
2d9e0 6e 6f 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f no = SQLITE_MAX_
2d9f0 50 41 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 2f 2a PAGE_COUNT;. /*
2da00 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d pPager->state =
2da10 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b 20 2a PAGER_UNLOCK; *
2da20 2f 0a 23 69 66 20 30 0a 20 20 61 73 73 65 72 74 /.#if 0. assert
2da30 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 ( pPager->state
2da40 3d 3d 20 28 74 65 6d 70 46 69 6c 65 20 3f 20 50 == (tempFile ? P
2da50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 3a AGER_EXCLUSIVE :
2da60 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 29 20 29 PAGER_UNLOCK) )
2da70 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 70 50 ;.#endif. /* pP
2da80 61 67 65 72 2d 3e 65 72 72 4d 61 73 6b 20 3d 20 ager->errMask =
2da90 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 0; */. pPager->
2daa0 74 65 6d 70 46 69 6c 65 20 3d 20 28 75 38 29 74 tempFile = (u8)t
2dab0 65 6d 70 46 69 6c 65 3b 0a 20 20 61 73 73 65 72 empFile;. asser
2dac0 74 28 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 t( tempFile==PAG
2dad0 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e ER_LOCKINGMODE_N
2dae0 4f 52 4d 41 4c 20 0a 20 20 20 20 20 20 20 20 20 ORMAL .
2daf0 20 7c 7c 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 || tempFile==PA
2db00 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
2db10 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 61 EXCLUSIVE );. a
2db20 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 ssert( PAGER_LOC
2db30 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 KINGMODE_EXCLUSI
2db40 56 45 3d 3d 31 20 29 3b 0a 20 20 70 50 61 67 65 VE==1 );. pPage
2db50 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
2db60 20 3d 20 28 75 38 29 74 65 6d 70 46 69 6c 65 3b = (u8)tempFile;
2db70 20 0a 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e . pPager->chan
2db80 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50 geCountDone = pP
2db90 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a ager->tempFile;.
2dba0 20 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 pPager->memDb
2dbb0 3d 20 28 75 38 29 6d 65 6d 44 62 3b 0a 20 20 70 = (u8)memDb;. p
2dbc0 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 Pager->readOnly
2dbd0 3d 20 28 75 38 29 72 65 61 64 4f 6e 6c 79 3b 0a = (u8)readOnly;.
2dbe0 20 20 61 73 73 65 72 74 28 20 75 73 65 4a 6f 75 assert( useJou
2dbf0 72 6e 61 6c 20 7c 7c 20 70 50 61 67 65 72 2d 3e rnal || pPager->
2dc00 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 70 50 tempFile );. pP
2dc10 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 70 ager->noSync = p
2dc20 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b Pager->tempFile;
2dc30 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e . if( pPager->n
2dc40 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 61 73 73 oSync ){. ass
2dc50 65 72 74 28 20 70 50 61 67 65 72 2d 3e 66 75 6c ert( pPager->ful
2dc60 6c 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 lSync==0 );.
2dc70 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
2dc80 73 79 6e 63 46 6c 61 67 73 3d 3d 30 20 29 3b 0a syncFlags==0 );.
2dc90 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
2dca0 65 72 2d 3e 77 61 6c 53 79 6e 63 46 6c 61 67 73 er->walSyncFlags
2dcb0 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==0 );. asser
2dcc0 74 28 20 70 50 61 67 65 72 2d 3e 63 6b 70 74 53 t( pPager->ckptS
2dcd0 79 6e 63 46 6c 61 67 73 3d 3d 30 20 29 3b 0a 20 yncFlags==0 );.
2dce0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 }else{. pPag
2dcf0 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 31 er->fullSync = 1
2dd00 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 79 ;. pPager->sy
2dd10 6e 63 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 ncFlags = SQLITE
2dd20 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 _SYNC_NORMAL;.
2dd30 20 20 70 50 61 67 65 72 2d 3e 77 61 6c 53 79 6e pPager->walSyn
2dd40 63 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f cFlags = SQLITE_
2dd50 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 7c 20 57 41 SYNC_NORMAL | WA
2dd60 4c 5f 53 59 4e 43 5f 54 52 41 4e 53 41 43 54 49 L_SYNC_TRANSACTI
2dd70 4f 4e 53 3b 0a 20 20 20 20 70 50 61 67 65 72 2d ONS;. pPager-
2dd80 3e 63 6b 70 74 53 79 6e 63 46 6c 61 67 73 20 3d >ckptSyncFlags =
2dd90 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 SQLITE_SYNC_NOR
2dda0 4d 41 4c 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70 50 MAL;. }. /* pP
2ddb0 61 67 65 72 2d 3e 70 46 69 72 73 74 20 3d 20 30 ager->pFirst = 0
2ddc0 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 ; */. /* pPager
2ddd0 2d 3e 70 46 69 72 73 74 53 79 6e 63 65 64 20 3d ->pFirstSynced =
2dde0 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 0; */. /* pPag
2ddf0 65 72 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 20 2a er->pLast = 0; *
2de00 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 45 78 74 /. pPager->nExt
2de10 72 61 20 3d 20 28 75 31 36 29 6e 45 78 74 72 61 ra = (u16)nExtra
2de20 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 ;. pPager->jour
2de30 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 53 nalSizeLimit = S
2de40 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a 4f QLITE_DEFAULT_JO
2de50 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54 URNAL_SIZE_LIMIT
2de60 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 ;. assert( isOp
2de70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c en(pPager->fd) |
2de80 7c 20 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 | tempFile );.
2de90 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 setSectorSize(pP
2dea0 61 67 65 72 29 3b 0a 20 20 69 66 28 20 21 75 73 ager);. if( !us
2deb0 65 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 eJournal ){.
2dec0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
2ded0 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 ode = PAGER_JOUR
2dee0 4e 41 4c 4d 4f 44 45 5f 4f 46 46 3b 0a 20 20 7d NALMODE_OFF;. }
2def0 65 6c 73 65 20 69 66 28 20 6d 65 6d 44 62 20 29 else if( memDb )
2df00 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f {. pPager->jo
2df10 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45 urnalMode = PAGE
2df20 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 R_JOURNALMODE_ME
2df30 4d 4f 52 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70 MORY;. }. /* p
2df40 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 Pager->xBusyHand
2df50 6c 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a ler = 0; */. /*
2df60 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 pPager->pBusyHa
2df70 6e 64 6c 65 72 41 72 67 20 3d 20 30 3b 20 2a 2f ndlerArg = 0; */
2df80 0a 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e . pPager->xRein
2df90 69 74 65 72 20 3d 20 78 52 65 69 6e 69 74 3b 0a iter = xReinit;.
2dfa0 20 20 2f 2a 20 6d 65 6d 73 65 74 28 70 50 61 67 /* memset(pPag
2dfb0 65 72 2d 3e 61 48 61 73 68 2c 20 30 2c 20 73 69 er->aHash, 0, si
2dfc0 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 61 48 61 zeof(pPager->aHa
2dfd0 73 68 29 29 3b 20 2a 2f 0a 0a 20 20 2a 70 70 50 sh)); */.. *ppP
2dfe0 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a 20 ager = pPager;.
2dff0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
2e000 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 K;.}..../*.** Th
2e010 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
2e020 61 6c 6c 65 64 20 61 66 74 65 72 20 74 72 61 6e alled after tran
2e030 73 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 50 sitioning from P
2e040 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 74 6f 0a 2a AGER_UNLOCK to.*
2e050 2a 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 73 * PAGER_SHARED s
2e060 74 61 74 65 2e 20 49 74 20 74 65 73 74 73 20 69 tate. It tests i
2e070 66 20 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 f there is a hot
2e080 20 6a 6f 75 72 6e 61 6c 20 70 72 65 73 65 6e 74 journal present
2e090 20 69 6e 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d in.** the file-
2e0a0 73 79 73 74 65 6d 20 66 6f 72 20 74 68 65 20 67 system for the g
2e0b0 69 76 65 6e 20 70 61 67 65 72 2e 20 41 20 68 6f iven pager. A ho
2e0c0 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 6e 65 t journal is one
2e0d0 20 74 68 61 74 20 0a 2a 2a 20 6e 65 65 64 73 20 that .** needs
2e0e0 74 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 to be played bac
2e0f0 6b 2e 20 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 k. According to
2e100 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 this function, a
2e110 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 hot-journal.**
2e120 66 69 6c 65 20 65 78 69 73 74 73 20 69 66 20 74 file exists if t
2e130 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 he following cri
2e140 74 65 72 69 61 20 61 72 65 20 6d 65 74 3a 0a 2a teria are met:.*
2e150 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 6a 6f 75 *.** * The jou
2e160 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 rnal file exists
2e170 20 69 6e 20 74 68 65 20 66 69 6c 65 20 73 79 73 in the file sys
2e180 74 65 6d 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 tem, and.** *
2e190 4e 6f 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 No process holds
2e1a0 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 67 a RESERVED or g
2e1b0 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 reater lock on t
2e1c0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
2e1d0 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 , and.** * The
2e1e0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
2e1f0 74 73 65 6c 66 20 69 73 20 67 72 65 61 74 65 72 tself is greater
2e200 20 74 68 61 6e 20 30 20 62 79 74 65 73 20 69 6e than 0 bytes in
2e210 20 73 69 7a 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 size, and.**
2e220 2a 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65 * The first byte
2e230 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
2e240 66 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 20 file exists and
2e250 69 73 20 6e 6f 74 20 30 78 30 30 2e 0a 2a 2a 0a is not 0x00..**.
2e260 2a 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 6e ** If the curren
2e270 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 t size of the da
2e280 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 30 tabase file is 0
2e290 20 62 75 74 20 61 20 6a 6f 75 72 6e 61 6c 20 66 but a journal f
2e2a0 69 6c 65 0a 2a 2a 20 65 78 69 73 74 73 2c 20 74 ile.** exists, t
2e2b0 68 61 74 20 69 73 20 70 72 6f 62 61 62 6c 79 20 hat is probably
2e2c0 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 6c an old journal l
2e2d0 65 66 74 20 6f 76 65 72 20 66 72 6f 6d 20 61 20 eft over from a
2e2e0 70 72 69 6f 72 0a 2a 2a 20 64 61 74 61 62 61 73 prior.** databas
2e2f0 65 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 e with the same
2e300 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 name. In this ca
2e310 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 se the journal f
2e320 69 6c 65 20 69 73 0a 2a 2a 20 6a 75 73 74 20 64 ile is.** just d
2e330 65 6c 65 74 65 64 20 75 73 69 6e 67 20 4f 73 44 eleted using OsD
2e340 65 6c 65 74 65 2c 20 2a 70 45 78 69 73 74 73 20 elete, *pExists
2e350 69 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 is set to 0 and
2e360 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 SQLITE_OK.** is
2e370 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
2e380 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 This routine doe
2e390 73 20 6e 6f 74 20 63 68 65 63 6b 20 69 66 20 74 s not check if t
2e3a0 68 65 72 65 20 69 73 20 61 20 6d 61 73 74 65 72 here is a master
2e3b0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d journal filenam
2e3c0 65 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 e.** at the end
2e3d0 6f 66 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20 of the file. If
2e3e0 74 68 65 72 65 20 69 73 2c 20 61 6e 64 20 74 68 there is, and th
2e3f0 61 74 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 at master journa
2e400 6c 20 66 69 6c 65 0a 2a 2a 20 64 6f 65 73 20 6e l file.** does n
2e410 6f 74 20 65 78 69 73 74 2c 20 74 68 65 6e 20 74 ot exist, then t
2e420 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
2e430 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f is not really ho
2e440 74 2e 20 49 6e 20 74 68 69 73 0a 2a 2a 20 63 61 t. In this.** ca
2e450 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 se this routine
2e460 77 69 6c 6c 20 72 65 74 75 72 6e 20 61 20 66 61 will return a fa
2e470 6c 73 65 2d 70 6f 73 69 74 69 76 65 2e 20 54 68 lse-positive. Th
2e480 65 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b e pager_playback
2e490 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 69 ().** routine wi
2e4a0 6c 6c 20 64 69 73 63 6f 76 65 72 20 74 68 61 74 ll discover that
2e4b0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
2e4c0 65 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 e is not really
2e4d0 68 6f 74 20 61 6e 64 20 0a 2a 2a 20 77 69 6c 6c hot and .** will
2e4e0 20 6e 6f 74 20 72 6f 6c 6c 20 69 74 20 62 61 63 not roll it bac
2e4f0 6b 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 68 k. .**.** If a h
2e500 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ot-journal file
2e510 69 73 20 66 6f 75 6e 64 20 74 6f 20 65 78 69 73 is found to exis
2e520 74 2c 20 2a 70 45 78 69 73 74 73 20 69 73 20 73 t, *pExists is s
2e530 65 74 20 74 6f 20 31 20 61 6e 64 20 0a 2a 2a 20 et to 1 and .**
2e540 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e SQLITE_OK return
2e550 65 64 2e 20 49 66 20 6e 6f 20 68 6f 74 2d 6a 6f ed. If no hot-jo
2e560 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70 72 urnal file is pr
2e570 65 73 65 6e 74 2c 20 2a 70 45 78 69 73 74 73 20 esent, *pExists
2e580 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 20 61 is.** set to 0 a
2e590 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 nd SQLITE_OK ret
2e5a0 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 urned. If an IO
2e5b0 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 error occurs whi
2e5c0 6c 65 20 74 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 le trying.** to
2e5d0 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 determine whethe
2e5e0 72 20 6f 72 20 6e 6f 74 20 61 20 68 6f 74 2d 6a r or not a hot-j
2e5f0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 ournal file exis
2e600 74 73 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 ts, the IO error
2e610 0a 2a 2a 20 63 6f 64 65 20 69 73 20 72 65 74 75 .** code is retu
2e620 72 6e 65 64 20 61 6e 64 20 74 68 65 20 76 61 6c rned and the val
2e630 75 65 20 6f 66 20 2a 70 45 78 69 73 74 73 20 69 ue of *pExists i
2e640 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a s undefined..*/.
2e650 73 74 61 74 69 63 20 69 6e 74 20 68 61 73 48 6f static int hasHo
2e660 74 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a tJournal(Pager *
2e670 70 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 45 78 pPager, int *pEx
2e680 69 73 74 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 ists){. sqlite3
2e690 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 _vfs * const pVf
2e6a0 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 s = pPager->pVfs
2e6b0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
2e6c0 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 ITE_OK;
2e6d0 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
2e6e0 20 2a 2f 0a 20 20 69 6e 74 20 65 78 69 73 74 73 */. int exists
2e6f0 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 = 1;
2e700 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 /* True if a
2e710 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
2e720 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e present */. in
2e730 74 20 6a 72 6e 6c 4f 70 65 6e 20 3d 20 21 21 69 t jrnlOpen = !!i
2e740 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
2e750 64 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 d);.. assert( p
2e760 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 Pager->useJourna
2e770 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 l );. assert( i
2e780 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 sOpen(pPager->fd
2e790 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
2e7a0 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 Pager->eState==P
2e7b0 41 47 45 52 5f 4f 50 45 4e 20 29 3b 0a 0a 20 20 AGER_OPEN );..
2e7c0 61 73 73 65 72 74 28 20 6a 72 6e 6c 4f 70 65 6e assert( jrnlOpen
2e7d0 3d 3d 30 20 7c 7c 20 28 20 73 71 6c 69 74 65 33 ==0 || ( sqlite3
2e7e0 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 OsDeviceCharacte
2e7f0 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e ristics(pPager->
2e800 6a 66 64 29 20 26 0a 20 20 20 20 53 51 4c 49 54 jfd) &. SQLIT
2e810 45 5f 49 4f 43 41 50 5f 55 4e 44 45 4c 45 54 41 E_IOCAP_UNDELETA
2e820 42 4c 45 5f 57 48 45 4e 5f 4f 50 45 4e 0a 20 20 BLE_WHEN_OPEN.
2e830 29 29 3b 0a 0a 20 20 2a 70 45 78 69 73 74 73 20 ));.. *pExists
2e840 3d 20 30 3b 0a 20 20 69 66 28 20 21 6a 72 6e 6c = 0;. if( !jrnl
2e850 4f 70 65 6e 20 29 7b 0a 20 20 20 20 72 63 20 3d Open ){. rc =
2e860 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 sqlite3OsAccess
2e870 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a (pVfs, pPager->z
2e880 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f Journal, SQLITE_
2e890 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 ACCESS_EXISTS, &
2e8a0 65 78 69 73 74 73 29 3b 0a 20 20 7d 0a 20 20 69 exists);. }. i
2e8b0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
2e8c0 20 26 26 20 65 78 69 73 74 73 20 29 7b 0a 20 20 && exists ){.
2e8d0 20 20 69 6e 74 20 6c 6f 63 6b 65 64 20 3d 20 30 int locked = 0
2e8e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
2e8f0 20 54 72 75 65 20 69 66 20 73 6f 6d 65 20 70 72 True if some pr
2e900 6f 63 65 73 73 20 68 6f 6c 64 73 20 61 20 52 45 ocess holds a RE
2e910 53 45 52 56 45 44 20 6c 6f 63 6b 20 2a 2f 0a 0a SERVED lock */..
2e920 20 20 20 20 2f 2a 20 52 61 63 65 20 63 6f 6e 64 /* Race cond
2e930 69 74 69 6f 6e 20 68 65 72 65 3a 20 20 41 6e 6f ition here: Ano
2e940 74 68 65 72 20 70 72 6f 63 65 73 73 20 6d 69 67 ther process mig
2e950 68 74 20 68 61 76 65 20 62 65 65 6e 20 68 6f 6c ht have been hol
2e960 64 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 ding the. **
2e970 74 68 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 the RESERVED loc
2e980 6b 20 61 6e 64 20 68 61 76 65 20 61 20 6a 6f 75 k and have a jou
2e990 72 6e 61 6c 20 6f 70 65 6e 20 61 74 20 74 68 65 rnal open at the
2e9a0 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 sqlite3OsAccess
2e9b0 28 29 20 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 () . ** call
2e9c0 61 62 6f 76 65 2c 20 62 75 74 20 74 68 65 6e 20 above, but then
2e9d0 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e delete the journ
2e9e0 61 6c 20 61 6e 64 20 64 72 6f 70 20 74 68 65 20 al and drop the
2e9f0 6c 6f 63 6b 20 62 65 66 6f 72 65 0a 20 20 20 20 lock before.
2ea00 2a 2a 20 77 65 20 67 65 74 20 74 6f 20 74 68 65 ** we get to the
2ea10 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 71 6c 69 74 following sqlit
2ea20 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 e3OsCheckReserve
2ea30 64 4c 6f 63 6b 28 29 20 63 61 6c 6c 2e 20 20 49 dLock() call. I
2ea40 66 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 69 73 f that. ** is
2ea50 20 74 68 65 20 63 61 73 65 2c 20 74 68 69 73 20 the case, this
2ea60 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 74 68 routine might th
2ea70 69 6e 6b 20 74 68 65 72 65 20 69 73 20 61 20 68 ink there is a h
2ea80 6f 74 20 6a 6f 75 72 6e 61 6c 20 77 68 65 6e 0a ot journal when.
2ea90 20 20 20 20 2a 2a 20 69 6e 20 66 61 63 74 20 74 ** in fact t
2eaa0 68 65 72 65 20 69 73 20 6e 6f 6e 65 2e 20 20 54 here is none. T
2eab0 68 69 73 20 72 65 73 75 6c 74 73 20 69 6e 20 61 his results in a
2eac0 20 66 61 6c 73 65 2d 70 6f 73 69 74 69 76 65 20 false-positive
2ead0 77 68 69 63 68 20 77 69 6c 6c 0a 20 20 20 20 2a which will. *
2eae0 2a 20 62 65 20 64 65 61 6c 74 20 77 69 74 68 20 * be dealt with
2eaf0 62 79 20 74 68 65 20 70 6c 61 79 62 61 63 6b 20 by the playback
2eb00 72 6f 75 74 69 6e 65 2e 20 20 54 69 63 6b 65 74 routine. Ticket
2eb10 20 23 33 38 38 33 2e 0a 20 20 20 20 2a 2f 0a 20 #3883.. */.
2eb20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
2eb30 73 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f sCheckReservedLo
2eb40 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 ck(pPager->fd, &
2eb50 6c 6f 63 6b 65 64 29 3b 0a 20 20 20 20 69 66 28 locked);. if(
2eb60 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
2eb70 26 20 21 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 & !locked ){.
2eb80 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20 20 Pgno nPage;
2eb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2eba0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
2ebb0 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 s in database fi
2ebc0 6c 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 le */.. /*
2ebd0 43 68 65 63 6b 20 74 68 65 20 73 69 7a 65 20 6f Check the size o
2ebe0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
2ebf0 69 6c 65 2e 20 49 66 20 69 74 20 63 6f 6e 73 69 ile. If it consi
2ec00 73 74 73 20 6f 66 20 30 20 70 61 67 65 73 2c 0a sts of 0 pages,.
2ec10 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 64 65 ** then de
2ec20 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c lete the journal
2ec30 20 66 69 6c 65 2e 20 53 65 65 20 74 68 65 20 68 file. See the h
2ec40 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 61 62 eader comment ab
2ec50 6f 76 65 20 66 6f 72 20 0a 20 20 20 20 20 20 2a ove for . *
2ec60 2a 20 74 68 65 20 72 65 61 73 6f 6e 69 6e 67 20 * the reasoning
2ec70 68 65 72 65 2e 20 20 44 65 6c 65 74 65 20 74 68 here. Delete th
2ec80 65 20 6f 62 73 6f 6c 65 74 65 20 6a 6f 75 72 6e e obsolete journ
2ec90 61 6c 20 66 69 6c 65 20 75 6e 64 65 72 0a 20 20 al file under.
2eca0 20 20 20 20 2a 2a 20 61 20 52 45 53 45 52 56 45 ** a RESERVE
2ecb0 44 20 6c 6f 63 6b 20 74 6f 20 61 76 6f 69 64 20 D lock to avoid
2ecc0 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 race conditions
2ecd0 61 6e 64 20 74 6f 20 61 76 6f 69 64 20 76 69 6f and to avoid vio
2ece0 6c 61 74 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 lating. **
2ecf0 5b 48 33 33 30 32 30 5d 2e 0a 20 20 20 20 20 20 [H33020]..
2ed00 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 */. rc = pa
2ed10 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 gerPagecount(pPa
2ed20 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 ger, &nPage);.
2ed30 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
2ed40 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
2ed50 20 69 66 28 20 6e 50 61 67 65 3d 3d 30 20 29 7b if( nPage==0 ){
2ed60 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
2ed70 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c e3BeginBenignMal
2ed80 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 20 20 loc();.
2ed90 20 69 66 28 20 70 61 67 65 72 4c 6f 63 6b 44 62 if( pagerLockDb
2eda0 28 70 50 61 67 65 72 2c 20 52 45 53 45 52 56 45 (pPager, RESERVE
2edb0 44 5f 4c 4f 43 4b 29 3d 3d 53 51 4c 49 54 45 5f D_LOCK)==SQLITE_
2edc0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
2edd0 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 sqlite3OsDelet
2ede0 65 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e e(pVfs, pPager->
2edf0 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 zJournal, 0);.
2ee00 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70 if( !p
2ee10 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 Pager->exclusive
2ee20 4d 6f 64 65 20 29 20 70 61 67 65 72 55 6e 6c 6f Mode ) pagerUnlo
2ee30 63 6b 44 62 28 70 50 61 67 65 72 2c 20 53 48 41 ckDb(pPager, SHA
2ee40 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 RED_LOCK);.
2ee50 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
2ee60 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
2ee70 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 nMalloc();.
2ee80 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
2ee90 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e /* The journ
2eea0 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20 61 al file exists a
2eeb0 6e 64 20 6e 6f 20 6f 74 68 65 72 20 63 6f 6e 6e nd no other conn
2eec0 65 63 74 69 6f 6e 20 68 61 73 20 61 20 72 65 73 ection has a res
2eed0 65 72 76 65 64 0a 20 20 20 20 20 20 20 20 20 20 erved.
2eee0 2a 2a 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f ** or greater lo
2eef0 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ck on the databa
2ef00 73 65 20 66 69 6c 65 2e 20 4e 6f 77 20 63 68 65 se file. Now che
2ef10 63 6b 20 74 68 61 74 20 74 68 65 72 65 20 69 73 ck that there is
2ef20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 74 . ** at
2ef30 20 6c 65 61 73 74 20 6f 6e 65 20 6e 6f 6e 2d 7a least one non-z
2ef40 65 72 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 ero bytes at the
2ef50 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a 6f start of the jo
2ef60 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 urnal file..
2ef70 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 72 ** If ther
2ef80 65 20 69 73 2c 20 74 68 65 6e 20 77 65 20 63 6f e is, then we co
2ef90 6e 73 69 64 65 72 20 74 68 69 73 20 6a 6f 75 72 nsider this jour
2efa0 6e 61 6c 20 74 6f 20 62 65 20 68 6f 74 2e 20 49 nal to be hot. I
2efb0 66 20 6e 6f 74 2c 20 0a 20 20 20 20 20 20 20 20 f not, .
2efc0 20 20 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 69 ** it can be i
2efd0 67 6e 6f 72 65 64 2e 0a 20 20 20 20 20 20 20 20 gnored..
2efe0 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 */. i
2eff0 66 28 20 21 6a 72 6e 6c 4f 70 65 6e 20 29 7b 0a f( !jrnlOpen ){.
2f000 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 int
2f010 66 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f f = SQLITE_OPEN_
2f020 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f READONLY|SQLITE_
2f030 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 OPEN_MAIN_JOURNA
2f040 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 L;. r
2f050 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 c = sqlite3OsOpe
2f060 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e n(pVfs, pPager->
2f070 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 zJournal, pPager
2f080 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 29 3b 0a 20 ->jfd, f, &f);.
2f090 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
2f0a0 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
2f0b0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
2f0c0 20 20 20 20 20 20 75 38 20 66 69 72 73 74 20 3d u8 first =
2f0d0 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 0;.
2f0e0 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 rc = sqlite3OsRe
2f0f0 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ad(pPager->jfd,
2f100 28 76 6f 69 64 20 2a 29 26 66 69 72 73 74 2c 20 (void *)&first,
2f110 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 1, 0);.
2f120 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
2f130 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 E_IOERR_SHORT_RE
2f140 41 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 AD ){.
2f150 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
2f160 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 OK;.
2f170 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 }. if
2f180 28 20 21 6a 72 6e 6c 4f 70 65 6e 20 29 7b 0a 20 ( !jrnlOpen ){.
2f190 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
2f1a0 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 ite3OsClose(pPag
2f1b0 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 er->jfd);.
2f1c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
2f1d0 20 20 20 20 2a 70 45 78 69 73 74 73 20 3d 20 28 *pExists = (
2f1e0 66 69 72 73 74 21 3d 30 29 3b 0a 20 20 20 20 20 first!=0);.
2f1f0 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 }else if( r
2f200 63 3d 3d 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 c==SQLITE_CANTOP
2f210 45 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 EN ){.
2f220 20 20 2f 2a 20 49 66 20 77 65 20 63 61 6e 6e 6f /* If we canno
2f230 74 20 6f 70 65 6e 20 74 68 65 20 72 6f 6c 6c 62 t open the rollb
2f240 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ack journal file
2f250 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 65 65 in order to see
2f260 20 69 66 0a 20 20 20 20 20 20 20 20 20 20 20 20 if.
2f270 2a 2a 20 69 74 73 20 68 61 73 20 61 20 7a 65 72 ** its has a zer
2f280 6f 20 68 65 61 64 65 72 2c 20 74 68 61 74 20 6d o header, that m
2f290 69 67 68 74 20 62 65 20 64 75 65 20 74 6f 20 61 ight be due to a
2f2a0 6e 20 49 2f 4f 20 65 72 72 6f 72 2c 20 6f 72 0a n I/O error, or.
2f2b0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 ** i
2f2c0 74 20 6d 69 67 68 74 20 62 65 20 64 75 65 20 74 t might be due t
2f2d0 6f 20 74 68 65 20 72 61 63 65 20 63 6f 6e 64 69 o the race condi
2f2e0 74 69 6f 6e 20 64 65 73 63 72 69 62 65 64 20 61 tion described a
2f2f0 62 6f 76 65 20 61 6e 64 20 69 6e 0a 20 20 20 20 bove and in.
2f300 20 20 20 20 20 20 20 20 2a 2a 20 74 69 63 6b 65 ** ticke
2f310 74 20 23 33 38 38 33 2e 20 20 45 69 74 68 65 72 t #3883. Either
2f320 20 77 61 79 2c 20 61 73 73 75 6d 65 20 74 68 61 way, assume tha
2f330 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 t the journal is
2f340 20 68 6f 74 2e 0a 20 20 20 20 20 20 20 20 20 20 hot..
2f350 20 20 2a 2a 20 54 68 69 73 20 6d 69 67 68 74 20 ** This might
2f360 62 65 20 61 20 66 61 6c 73 65 20 70 6f 73 69 74 be a false posit
2f370 69 76 65 2e 20 20 42 75 74 20 69 66 20 69 74 20 ive. But if it
2f380 69 73 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 20 is, then the.
2f390 20 20 20 20 20 20 20 20 20 2a 2a 20 61 75 74 6f ** auto
2f3a0 6d 61 74 69 63 20 6a 6f 75 72 6e 61 6c 20 70 6c matic journal pl
2f3b0 61 79 62 61 63 6b 20 61 6e 64 20 72 65 63 6f 76 ayback and recov
2f3c0 65 72 79 20 6d 65 63 68 61 6e 69 73 6d 20 77 69 ery mechanism wi
2f3d0 6c 6c 20 64 65 61 6c 0a 20 20 20 20 20 20 20 20 ll deal.
2f3e0 20 20 20 20 2a 2a 20 77 69 74 68 20 69 74 20 75 ** with it u
2f3f0 6e 64 65 72 20 61 6e 20 45 58 43 4c 55 53 49 56 nder an EXCLUSIV
2f400 45 20 6c 6f 63 6b 20 77 68 65 72 65 20 77 65 20 E lock where we
2f410 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0a 20 do not need to.
2f420 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 77 6f ** wo
2f430 72 72 79 20 73 6f 20 6d 75 63 68 20 77 69 74 68 rry so much with
2f440 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 73 race conditions
2f450 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f .. */
2f460 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 45 . *pE
2f470 78 69 73 74 73 20 3d 20 31 3b 0a 20 20 20 20 20 xists = 1;.
2f480 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
2f490 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 TE_OK;.
2f4a0 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
2f4b0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a }. }. }..
2f4c0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
2f4d0 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
2f4e0 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f ion is called to
2f4f0 20 6f 62 74 61 69 6e 20 61 20 73 68 61 72 65 64 obtain a shared
2f500 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
2f510 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 abase file..** I
2f520 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 t is illegal to
2f530 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 call sqlite3Page
2f540 72 41 63 71 75 69 72 65 28 29 20 75 6e 74 69 6c rAcquire() until
2f550 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 after this func
2f560 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e tion.** has been
2f570 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 61 successfully ca
2f580 6c 6c 65 64 2e 20 49 66 20 61 20 73 68 61 72 65 lled. If a share
2f590 64 2d 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 d-lock is alread
2f5a0 79 20 68 65 6c 64 20 77 68 65 6e 0a 2a 2a 20 74 y held when.** t
2f5b0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
2f5c0 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 called, it is a
2f5d0 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 no-op..**.** The
2f5e0 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 65 72 61 following opera
2f5f0 74 69 6f 6e 73 20 61 72 65 20 61 6c 73 6f 20 70 tions are also p
2f600 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73 erformed by this
2f610 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a function..**.**
2f620 20 20 20 31 29 20 49 66 20 74 68 65 20 70 61 67 1) If the pag
2f630 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 er is currently
2f640 69 6e 20 50 41 47 45 52 5f 4f 50 45 4e 20 73 74 in PAGER_OPEN st
2f650 61 74 65 20 28 6e 6f 20 6c 6f 63 6b 20 68 65 6c ate (no lock hel
2f660 64 0a 2a 2a 20 20 20 20 20 20 6f 6e 20 74 68 65 d.** on the
2f670 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 29 2c database file),
2f680 20 74 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 then an attempt
2f690 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 62 74 61 is made to obta
2f6a0 69 6e 20 61 0a 2a 2a 20 20 20 20 20 20 53 48 41 in a.** SHA
2f6b0 52 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 RED lock on the
2f6c0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 database file. I
2f6d0 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 mmediately after
2f6e0 20 6f 62 74 61 69 6e 69 6e 67 0a 2a 2a 20 20 20 obtaining.**
2f6f0 20 20 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f the SHARED lo
2f700 63 6b 2c 20 74 68 65 20 66 69 6c 65 2d 73 79 73 ck, the file-sys
2f710 74 65 6d 20 69 73 20 63 68 65 63 6b 65 64 20 66 tem is checked f
2f720 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c or a hot-journal
2f730 2c 0a 2a 2a 20 20 20 20 20 20 77 68 69 63 68 20 ,.** which
2f740 69 73 20 70 6c 61 79 65 64 20 62 61 63 6b 20 69 is played back i
2f750 66 20 70 72 65 73 65 6e 74 2e 20 46 6f 6c 6c 6f f present. Follo
2f760 77 69 6e 67 20 61 6e 79 20 68 6f 74 2d 6a 6f 75 wing any hot-jou
2f770 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 20 72 6f rnal .** ro
2f780 6c 6c 62 61 63 6b 2c 20 74 68 65 20 63 6f 6e 74 llback, the cont
2f790 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 ents of the cach
2f7a0 65 20 61 72 65 20 76 61 6c 69 64 61 74 65 64 20 e are validated
2f7b0 62 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 20 by checking.**
2f7c0 20 20 20 20 74 68 65 20 27 63 68 61 6e 67 65 2d the 'change-
2f7d0 63 6f 75 6e 74 65 72 27 20 66 69 65 6c 64 20 6f counter' field o
2f7e0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
2f7f0 69 6c 65 20 68 65 61 64 65 72 20 61 6e 64 0a 2a ile header and.*
2f800 2a 20 20 20 20 20 20 64 69 73 63 61 72 64 65 64 * discarded
2f810 20 69 66 20 74 68 65 79 20 61 72 65 20 66 6f 75 if they are fou
2f820 6e 64 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 nd to be invalid
2f830 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 66 20 ..**.** 2) If
2f840 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75 6e the pager is run
2f850 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 ning in exclusiv
2f860 65 2d 6d 6f 64 65 2c 20 61 6e 64 20 74 68 65 72 e-mode, and ther
2f870 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 0a e are currently.
2f880 2a 2a 20 20 20 20 20 20 6e 6f 20 6f 75 74 73 74 ** no outst
2f890 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 anding reference
2f8a0 73 20 74 6f 20 61 6e 79 20 70 61 67 65 73 2c 20 s to any pages,
2f8b0 61 6e 64 20 69 73 20 69 6e 20 74 68 65 20 65 72 and is in the er
2f8c0 72 6f 72 20 73 74 61 74 65 2c 0a 2a 2a 20 20 20 ror state,.**
2f8d0 20 20 20 74 68 65 6e 20 61 6e 20 61 74 74 65 6d then an attem
2f8e0 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 63 6c pt is made to cl
2f8f0 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 ear the error st
2f900 61 74 65 20 62 79 20 64 69 73 63 61 72 64 69 6e ate by discardin
2f910 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 63 6f g.** the co
2f920 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 ntents of the pa
2f930 67 65 20 63 61 63 68 65 20 61 6e 64 20 72 6f 6c ge cache and rol
2f940 6c 69 6e 67 20 62 61 63 6b 20 61 6e 79 20 6f 70 ling back any op
2f950 65 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 en journal.**
2f960 20 20 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 file..**.** I
2f970 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 f everything is
2f980 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 successful, SQLI
2f990 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
2f9a0 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f d. If an IO erro
2f9b0 72 20 0a 2a 2a 20 6f 63 63 75 72 73 20 77 68 69 r .** occurs whi
2f9c0 6c 65 20 6c 6f 63 6b 69 6e 67 20 74 68 65 20 64 le locking the d
2f9d0 61 74 61 62 61 73 65 2c 20 63 68 65 63 6b 69 6e atabase, checkin
2f9e0 67 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 g for a hot-jour
2f9f0 6e 61 6c 20 66 69 6c 65 20 6f 72 20 0a 2a 2a 20 nal file or .**
2fa00 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a rolling back a j
2fa10 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 ournal file, the
2fa20 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 IO error code i
2fa30 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 69 s returned..*/.i
2fa40 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 nt sqlite3PagerS
2fa50 68 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 haredLock(Pager
2fa60 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 *pPager){. int
2fa70 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
2fa80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2fa90 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
2faa0 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 .. /* This rout
2fab0 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c ine is only call
2fac0 65 64 20 66 72 6f 6d 20 62 2d 74 72 65 65 20 61 ed from b-tree a
2fad0 6e 64 20 6f 6e 6c 79 20 77 68 65 6e 20 74 68 65 nd only when the
2fae0 72 65 20 61 72 65 20 6e 6f 0a 20 20 2a 2a 20 6f re are no. ** o
2faf0 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 73 utstanding pages
2fb00 2e 20 54 68 69 73 20 69 6d 70 6c 69 65 73 20 74 . This implies t
2fb10 68 61 74 20 74 68 65 20 70 61 67 65 72 20 73 74 hat the pager st
2fb20 61 74 65 20 73 68 6f 75 6c 64 20 65 69 74 68 65 ate should eithe
2fb30 72 0a 20 20 2a 2a 20 62 65 20 4f 50 45 4e 20 6f r. ** be OPEN o
2fb40 72 20 52 45 41 44 45 52 2e 20 52 45 41 44 45 52 r READER. READER
2fb50 20 69 73 20 6f 6e 6c 79 20 70 6f 73 73 69 62 6c is only possibl
2fb60 65 20 69 66 20 74 68 65 20 70 61 67 65 72 20 69 e if the pager i
2fb70 73 20 6f 72 20 77 61 73 20 69 6e 20 0a 20 20 2a s or was in . *
2fb80 2a 20 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 * exclusive acce
2fb90 73 73 20 6d 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 ss mode.. */.
2fba0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
2fbb0 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 cacheRefCount(pP
2fbc0 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d ager->pPCache)==
2fbd0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 0 );. assert( a
2fbe0 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 ssert_pager_stat
2fbf0 65 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 61 e(pPager) );. a
2fc00 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 ssert( pPager->e
2fc10 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45 State==PAGER_OPE
2fc20 4e 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53 74 N || pPager->eSt
2fc30 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 41 44 45 ate==PAGER_READE
2fc40 52 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 R );. if( NEVER
2fc50 28 4d 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 (MEMDB && pPager
2fc60 2d 3e 65 72 72 43 6f 64 65 29 20 29 7b 20 72 65 ->errCode) ){ re
2fc70 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 turn pPager->err
2fc80 43 6f 64 65 3b 20 7d 0a 0a 20 20 69 66 28 20 21 Code; }.. if( !
2fc90 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 pagerUseWal(pPag
2fca0 65 72 29 20 26 26 20 70 50 61 67 65 72 2d 3e 65 er) && pPager->e
2fcb0 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45 State==PAGER_OPE
2fcc0 4e 20 29 7b 0a 20 20 20 20 69 6e 74 20 62 48 6f N ){. int bHo
2fcd0 74 4a 6f 75 72 6e 61 6c 20 3d 20 31 3b 20 20 20 tJournal = 1;
2fce0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
2fcf0 66 20 74 68 65 72 65 20 65 78 69 73 74 73 20 61 f there exists a
2fd00 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c hot journal-fil
2fd10 65 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 e */.. assert
2fd20 28 20 21 4d 45 4d 44 42 20 29 3b 0a 0a 20 20 20 ( !MEMDB );..
2fd30 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 rc = pager_wait
2fd40 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c _on_lock(pPager,
2fd50 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 SHARED_LOCK);.
2fd60 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
2fd70 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 E_OK ){. as
2fd80 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 4c sert( pPager->eL
2fd90 6f 63 6b 3d 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 ock==NO_LOCK ||
2fda0 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 55 pPager->eLock==U
2fdb0 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 29 3b 0a 20 NKNOWN_LOCK );.
2fdc0 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 goto failed
2fdd0 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
2fde0 49 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c If a journal fil
2fdf0 65 20 65 78 69 73 74 73 2c 20 61 6e 64 20 74 68 e exists, and th
2fe00 65 72 65 20 69 73 20 6e 6f 20 52 45 53 45 52 56 ere is no RESERV
2fe10 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 ED lock on the.
2fe20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 ** database f
2fe30 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 65 69 74 ile, then it eit
2fe40 68 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 her needs to be
2fe50 70 6c 61 79 65 64 20 62 61 63 6b 20 6f 72 20 64 played back or d
2fe60 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 eleted.. */.
2fe70 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 if( pPager->e
2fe80 4c 6f 63 6b 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 Lock<=SHARED_LOC
2fe90 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 K ){. rc =
2fea0 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 70 50 hasHotJournal(pP
2feb0 61 67 65 72 2c 20 26 62 48 6f 74 4a 6f 75 72 6e ager, &bHotJourn
2fec0 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 al);. }. i
2fed0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
2fee0 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 ){. goto f
2fef0 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 ailed;. }.
2ff00 20 69 66 28 20 62 48 6f 74 4a 6f 75 72 6e 61 6c if( bHotJournal
2ff10 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 ){. if( pP
2ff20 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 ager->readOnly )
2ff30 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 {. rc = S
2ff40 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5f 52 QLITE_READONLY_R
2ff50 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 20 20 20 20 OLLBACK;.
2ff60 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 goto failed;.
2ff70 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 }.. /*
2ff80 47 65 74 20 61 6e 20 45 58 43 4c 55 53 49 56 45 Get an EXCLUSIVE
2ff90 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
2ffa0 61 62 61 73 65 20 66 69 6c 65 2e 20 41 74 20 74 abase file. At t
2ffb0 68 69 73 20 70 6f 69 6e 74 20 69 74 20 69 73 0a his point it is.
2ffc0 20 20 20 20 20 20 2a 2a 20 69 6d 70 6f 72 74 61 ** importa
2ffd0 6e 74 20 74 68 61 74 20 61 20 52 45 53 45 52 56 nt that a RESERV
2ffe0 45 44 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 6f ED lock is not o
2fff0 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 77 btained on the w
30000 61 79 20 74 6f 20 74 68 65 0a 20 20 20 20 20 20 ay to the.
30010 2a 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 ** EXCLUSIVE loc
30020 6b 2e 20 49 66 20 69 74 20 77 65 72 65 2c 20 61 k. If it were, a
30030 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 6d nother process m
30040 69 67 68 74 20 6f 70 65 6e 20 74 68 65 0a 20 20 ight open the.
30050 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 ** database
30060 66 69 6c 65 2c 20 64 65 74 65 63 74 20 74 68 65 file, detect the
30070 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 RESERVED lock,
30080 61 6e 64 20 63 6f 6e 63 6c 75 64 65 20 74 68 61 and conclude tha
30090 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 t the. ** d
300a0 61 74 61 62 61 73 65 20 69 73 20 73 61 66 65 20 atabase is safe
300b0 74 6f 20 72 65 61 64 20 77 68 69 6c 65 20 74 68 to read while th
300c0 69 73 20 70 72 6f 63 65 73 73 20 69 73 20 73 74 is process is st
300d0 69 6c 6c 20 72 6f 6c 6c 69 6e 67 20 74 68 65 20 ill rolling the
300e0 0a 20 20 20 20 20 20 2a 2a 20 68 6f 74 2d 6a 6f . ** hot-jo
300f0 75 72 6e 61 6c 20 62 61 63 6b 2e 0a 20 20 20 20 urnal back..
30100 20 20 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20 42 ** . ** B
30110 65 63 61 75 73 65 20 74 68 65 20 69 6e 74 65 72 ecause the inter
30120 6d 65 64 69 61 74 65 20 52 45 53 45 52 56 45 44 mediate RESERVED
30130 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65 71 lock is not req
30140 75 65 73 74 65 64 2c 20 61 6e 79 0a 20 20 20 20 uested, any.
30150 20 20 2a 2a 20 6f 74 68 65 72 20 70 72 6f 63 65 ** other proce
30160 73 73 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f ss attempting to
30170 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61 access the data
30180 62 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 67 base file will g
30190 65 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 et to . **
301a0 74 68 69 73 20 70 6f 69 6e 74 20 69 6e 20 74 68 this point in th
301b0 65 20 63 6f 64 65 20 61 6e 64 20 66 61 69 6c 20 e code and fail
301c0 74 6f 20 6f 62 74 61 69 6e 20 69 74 73 20 6f 77 to obtain its ow
301d0 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b n EXCLUSIVE lock
301e0 20 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 . ** on th
301f0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
30200 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
30210 2a 2a 20 55 6e 6c 65 73 73 20 74 68 65 20 70 61 ** Unless the pa
30220 67 65 72 20 69 73 20 69 6e 20 6c 6f 63 6b 69 6e ger is in lockin
30230 67 5f 6d 6f 64 65 3d 65 78 63 6c 75 73 69 76 65 g_mode=exclusive
30240 20 6d 6f 64 65 2c 20 74 68 65 20 6c 6f 63 6b 20 mode, the lock
30250 69 73 0a 20 20 20 20 20 20 2a 2a 20 64 6f 77 6e is. ** down
30260 67 72 61 64 65 64 20 74 6f 20 53 48 41 52 45 44 graded to SHARED
30270 5f 4c 4f 43 4b 20 62 65 66 6f 72 65 20 74 68 69 _LOCK before thi
30280 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
30290 6e 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ns.. */.
302a0 20 20 20 72 63 20 3d 20 70 61 67 65 72 4c 6f 63 rc = pagerLoc
302b0 6b 44 62 28 70 50 61 67 65 72 2c 20 45 58 43 4c kDb(pPager, EXCL
302c0 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 USIVE_LOCK);.
302d0 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
302e0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
302f0 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 goto failed;.
30300 20 20 20 7d 0a 20 0a 20 20 20 20 20 20 2f 2a 20 }. . /*
30310 49 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 If it is not alr
30320 65 61 64 79 20 6f 70 65 6e 20 61 6e 64 20 74 68 eady open and th
30330 65 20 66 69 6c 65 20 65 78 69 73 74 73 20 6f 6e e file exists on
30340 20 64 69 73 6b 2c 20 6f 70 65 6e 20 74 68 65 20 disk, open the
30350 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 . ** journa
30360 6c 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 l for read/write
30370 20 61 63 63 65 73 73 2e 20 57 72 69 74 65 20 61 access. Write a
30380 63 63 65 73 73 20 69 73 20 72 65 71 75 69 72 65 ccess is require
30390 64 20 62 65 63 61 75 73 65 20 0a 20 20 20 20 20 d because .
303a0 20 2a 2a 20 69 6e 20 65 78 63 6c 75 73 69 76 65 ** in exclusive
303b0 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 74 68 65 -access mode the
303c0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
303d0 20 77 69 6c 6c 20 62 65 20 6b 65 70 74 20 6f 70 will be kept op
303e0 65 6e 20 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 en . ** and
303f0 20 70 6f 73 73 69 62 6c 79 20 75 73 65 64 20 66 possibly used f
30400 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e or a transaction
30410 20 6c 61 74 65 72 20 6f 6e 2e 20 41 6c 73 6f 2c later on. Also,
30420 20 77 72 69 74 65 2d 61 63 63 65 73 73 20 0a 20 write-access .
30430 20 20 20 20 20 2a 2a 20 69 73 20 75 73 75 61 6c ** is usual
30440 6c 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 66 ly required to f
30450 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 inalize the jour
30460 6e 61 6c 20 69 6e 20 6a 6f 75 72 6e 61 6c 5f 6d nal in journal_m
30470 6f 64 65 3d 70 65 72 73 69 73 74 20 0a 20 20 20 ode=persist .
30480 20 20 20 2a 2a 20 6d 6f 64 65 20 28 61 6e 64 20 ** mode (and
30490 61 6c 73 6f 20 66 6f 72 20 6a 6f 75 72 6e 61 6c also for journal
304a0 5f 6d 6f 64 65 3d 74 72 75 6e 63 61 74 65 20 6f _mode=truncate o
304b0 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 29 2e n some systems).
304c0 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
304d0 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 ** If the journa
304e0 6c 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 l does not exist
304f0 2c 20 69 74 20 75 73 75 61 6c 6c 79 20 6d 65 61 , it usually mea
30500 6e 73 20 74 68 61 74 20 73 6f 6d 65 20 0a 20 20 ns that some .
30510 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f 6e ** other con
30520 6e 65 63 74 69 6f 6e 20 6d 61 6e 61 67 65 64 20 nection managed
30530 74 6f 20 67 65 74 20 69 6e 20 61 6e 64 20 72 6f to get in and ro
30540 6c 6c 20 69 74 20 62 61 63 6b 20 62 65 66 6f 72 ll it back befor
30550 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 69 73 e . ** this
30560 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 74 61 connection obta
30570 69 6e 65 64 20 74 68 65 20 65 78 63 6c 75 73 69 ined the exclusi
30580 76 65 20 6c 6f 63 6b 20 61 62 6f 76 65 2e 20 4f ve lock above. O
30590 72 2c 20 69 74 20 0a 20 20 20 20 20 20 2a 2a 20 r, it . **
305a0 6d 61 79 20 6d 65 61 6e 20 74 68 61 74 20 74 68 may mean that th
305b0 65 20 70 61 67 65 72 20 77 61 73 20 69 6e 20 74 e pager was in t
305c0 68 65 20 65 72 72 6f 72 2d 73 74 61 74 65 20 77 he error-state w
305d0 68 65 6e 20 74 68 69 73 0a 20 20 20 20 20 20 2a hen this. *
305e0 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 * function was c
305f0 61 6c 6c 65 64 20 61 6e 64 20 74 68 65 20 6a 6f alled and the jo
30600 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 20 urnal file does
30610 6e 6f 74 20 65 78 69 73 74 2e 0a 20 20 20 20 20 not exist..
30620 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21 69 */. if( !i
30630 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
30640 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 d) ){. sq
30650 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 6f 6e 73 lite3_vfs * cons
30660 74 20 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d t pVfs = pPager-
30670 3e 70 56 66 73 3b 0a 20 20 20 20 20 20 20 20 69 >pVfs;. i
30680 6e 74 20 62 45 78 69 73 74 73 3b 20 20 20 20 20 nt bExists;
30690 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
306a0 20 69 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 if journal file
306b0 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 20 exists */.
306c0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
306d0 73 41 63 63 65 73 73 28 0a 20 20 20 20 20 20 20 sAccess(.
306e0 20 20 20 20 20 70 56 66 73 2c 20 70 50 61 67 65 pVfs, pPage
306f0 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c r->zJournal, SQL
30700 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 ITE_ACCESS_EXIST
30710 53 2c 20 26 62 45 78 69 73 74 73 29 3b 0a 20 20 S, &bExists);.
30720 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
30730 4c 49 54 45 5f 4f 4b 20 26 26 20 62 45 78 69 73 LITE_OK && bExis
30740 74 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ts ){.
30750 69 6e 74 20 66 6f 75 74 20 3d 20 30 3b 0a 20 20 int fout = 0;.
30760 20 20 20 20 20 20 20 20 69 6e 74 20 66 20 3d 20 int f =
30770 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
30780 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 WRITE|SQLITE_OPE
30790 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a N_MAIN_JOURNAL;.
307a0 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
307b0 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 ( !pPager->tempF
307c0 69 6c 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 ile );.
307d0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f rc = sqlite3OsO
307e0 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 pen(pVfs, pPager
307f0 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 ->zJournal, pPag
30800 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 6f 75 er->jfd, f, &fou
30810 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 t);. as
30820 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
30830 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50 _OK || isOpen(pP
30840 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 ager->jfd) );.
30850 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
30860 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 66 6f 75 SQLITE_OK && fou
30870 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 t&SQLITE_OPEN_RE
30880 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 ADONLY ){.
30890 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
308a0 45 5f 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 3b E_CANTOPEN_BKPT;
308b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
308c0 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 ite3OsClose(pPag
308d0 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 er->jfd);.
308e0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
308f0 20 20 20 20 20 20 7d 0a 20 0a 20 20 20 20 20 20 }. .
30900 2f 2a 20 50 6c 61 79 62 61 63 6b 20 61 6e 64 20 /* Playback and
30910 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e delete the journ
30920 61 6c 2e 20 20 44 72 6f 70 20 74 68 65 20 64 61 al. Drop the da
30930 74 61 62 61 73 65 20 77 72 69 74 65 0a 20 20 20 tabase write.
30940 20 20 20 2a 2a 20 6c 6f 63 6b 20 61 6e 64 20 72 ** lock and r
30950 65 61 63 71 75 69 72 65 20 74 68 65 20 72 65 61 eacquire the rea
30960 64 20 6c 6f 63 6b 2e 20 50 75 72 67 65 20 74 68 d lock. Purge th
30970 65 20 63 61 63 68 65 20 62 65 66 6f 72 65 0a 20 e cache before.
30980 20 20 20 20 20 2a 2a 20 70 6c 61 79 69 6e 67 20 ** playing
30990 62 61 63 6b 20 74 68 65 20 68 6f 74 2d 6a 6f 75 back the hot-jou
309a0 72 6e 61 6c 20 73 6f 20 74 68 61 74 20 77 65 20 rnal so that we
309b0 64 6f 6e 27 74 20 65 6e 64 20 75 70 20 77 69 74 don't end up wit
309c0 68 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 6e h. ** an in
309d0 63 6f 6e 73 69 73 74 65 6e 74 20 63 61 63 68 65 consistent cache
309e0 2e 20 20 53 79 6e 63 20 74 68 65 20 68 6f 74 20 . Sync the hot
309f0 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 20 70 journal before p
30a00 6c 61 79 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 laying. **
30a10 69 74 20 62 61 63 6b 20 73 69 6e 63 65 20 74 68 it back since th
30a20 65 20 70 72 6f 63 65 73 73 20 74 68 61 74 20 63 e process that c
30a30 72 61 73 68 65 64 20 61 6e 64 20 6c 65 66 74 20 rashed and left
30a40 74 68 65 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 0a the hot journal.
30a50 20 20 20 20 20 20 2a 2a 20 70 72 6f 62 61 62 6c ** probabl
30a60 79 20 64 69 64 20 6e 6f 74 20 73 79 6e 63 20 69 y did not sync i
30a70 74 20 61 6e 64 20 77 65 20 61 72 65 20 72 65 71 t and we are req
30a80 75 69 72 65 64 20 74 6f 20 61 6c 77 61 79 73 20 uired to always
30a90 73 79 6e 63 0a 20 20 20 20 20 20 2a 2a 20 74 68 sync. ** th
30aa0 65 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 e journal before
30ab0 20 70 6c 61 79 69 6e 67 20 69 74 20 62 61 63 6b playing it back
30ac0 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
30ad0 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 if( isOpen(pPag
30ae0 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 er->jfd) ){.
30af0 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
30b00 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 SQLITE_OK );.
30b10 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 53 rc = pagerS
30b20 79 6e 63 48 6f 74 4a 6f 75 72 6e 61 6c 28 70 50 yncHotJournal(pP
30b30 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 69 ager);. i
30b40 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
30b50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
30b60 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 = pager_playbac
30b70 6b 28 70 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 k(pPager, 1);.
30b80 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
30b90 65 53 74 61 74 65 20 3d 20 50 41 47 45 52 5f 4f eState = PAGER_O
30ba0 50 45 4e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 PEN;. }.
30bb0 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 }else if( !
30bc0 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
30bd0 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20 eMode ){.
30be0 20 70 61 67 65 72 55 6e 6c 6f 63 6b 44 62 28 70 pagerUnlockDb(p
30bf0 50 61 67 65 72 2c 20 53 48 41 52 45 44 5f 4c 4f Pager, SHARED_LO
30c00 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 CK);. }..
30c10 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
30c20 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
30c30 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 /* This branch
30c40 69 73 20 74 61 6b 65 6e 20 69 66 20 61 6e 20 65 is taken if an e
30c50 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c rror occurs whil
30c60 65 20 74 72 79 69 6e 67 20 74 6f 20 6f 70 65 6e e trying to open
30c70 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 72 . ** or r
30c80 6f 6c 6c 20 62 61 63 6b 20 61 20 68 6f 74 2d 6a oll back a hot-j
30c90 6f 75 72 6e 61 6c 20 77 68 69 6c 65 20 68 6f 6c ournal while hol
30ca0 64 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 49 56 ding an EXCLUSIV
30cb0 45 20 6c 6f 63 6b 2e 20 54 68 65 0a 20 20 20 20 E lock. The.
30cc0 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 75 6e 6c ** pager_unl
30cd0 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 20 77 69 ock() routine wi
30ce0 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 ll be called bef
30cf0 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 74 6f ore returning to
30d00 20 75 6e 6c 6f 63 6b 0a 20 20 20 20 20 20 20 20 unlock.
30d10 2a 2a 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20 ** the file. If
30d20 74 68 65 20 75 6e 6c 6f 63 6b 20 61 74 74 65 6d the unlock attem
30d30 70 74 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 50 pt fails, then P
30d40 61 67 65 72 2e 65 4c 6f 63 6b 20 6d 75 73 74 20 ager.eLock must
30d50 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 be. ** se
30d60 74 20 74 6f 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 t to UNKNOWN_LOC
30d70 4b 20 28 73 65 65 20 74 68 65 20 63 6f 6d 6d 65 K (see the comme
30d80 6e 74 20 61 62 6f 76 65 20 74 68 65 20 23 64 65 nt above the #de
30d90 66 69 6e 65 20 66 6f 72 20 0a 20 20 20 20 20 20 fine for .
30da0 20 20 2a 2a 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 ** UNKNOWN_LOC
30db0 4b 20 61 62 6f 76 65 20 66 6f 72 20 61 6e 20 65 K above for an e
30dc0 78 70 6c 61 6e 61 74 69 6f 6e 29 2e 20 0a 20 20 xplanation). .
30dd0 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 **.
30de0 20 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 ** In order to
30df0 67 65 74 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b get pager_unlock
30e00 28 29 20 74 6f 20 64 6f 20 74 68 69 73 2c 20 73 () to do this, s
30e10 65 74 20 50 61 67 65 72 2e 65 53 74 61 74 65 20 et Pager.eState
30e20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 50 41 to. ** PA
30e30 47 45 52 5f 45 52 52 4f 52 20 6e 6f 77 2e 20 54 GER_ERROR now. T
30e40 68 69 73 20 69 73 20 6e 6f 74 20 61 63 74 75 61 his is not actua
30e50 6c 6c 79 20 63 6f 75 6e 74 65 64 20 61 73 20 61 lly counted as a
30e60 20 74 72 61 6e 73 69 74 69 6f 6e 0a 20 20 20 20 transition.
30e70 20 20 20 20 2a 2a 20 74 6f 20 45 52 52 4f 52 20 ** to ERROR
30e80 73 74 61 74 65 20 69 6e 20 74 68 65 20 73 74 61 state in the sta
30e90 74 65 20 64 69 61 67 72 61 6d 20 61 74 20 74 68 te diagram at th
30ea0 65 20 74 6f 70 20 6f 66 20 74 68 69 73 20 66 69 e top of this fi
30eb0 6c 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 le,. ** s
30ec0 69 6e 63 65 20 77 65 20 6b 6e 6f 77 20 74 68 61 ince we know tha
30ed0 74 20 74 68 65 20 73 61 6d 65 20 63 61 6c 6c 20 t the same call
30ee0 74 6f 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 to pager_unlock(
30ef0 29 20 77 69 6c 6c 20 76 65 72 79 0a 20 20 20 20 ) will very.
30f00 20 20 20 20 2a 2a 20 73 68 6f 72 74 6c 79 20 74 ** shortly t
30f10 72 61 6e 73 69 74 69 6f 6e 20 74 68 65 20 70 61 ransition the pa
30f20 67 65 72 20 6f 62 6a 65 63 74 20 74 6f 20 74 68 ger object to th
30f30 65 20 4f 50 45 4e 20 73 74 61 74 65 2e 20 43 61 e OPEN state. Ca
30f40 6c 6c 69 6e 67 0a 20 20 20 20 20 20 20 20 2a 2a lling. **
30f50 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 assert_pager_st
30f60 61 74 65 28 29 20 77 6f 75 6c 64 20 66 61 69 6c ate() would fail
30f70 20 6e 6f 77 2c 20 61 73 20 69 74 20 73 68 6f 75 now, as it shou
30f80 6c 64 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 ld not be possib
30f90 6c 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f le. ** to
30fa0 20 62 65 20 69 6e 20 45 52 52 4f 52 20 73 74 61 be in ERROR sta
30fb0 74 65 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 te when there ar
30fc0 65 20 7a 65 72 6f 20 6f 75 74 73 74 61 6e 64 69 e zero outstandi
30fd0 6e 67 20 70 61 67 65 20 0a 20 20 20 20 20 20 20 ng page .
30fe0 20 2a 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 0a ** references..
30ff0 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
31000 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 pager_error(p
31010 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 Pager, rc);.
31020 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b goto failed;
31030 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
31040 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
31050 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 eState==PAGER_OP
31060 45 4e 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 EN );. asse
31070 72 74 28 20 28 70 50 61 67 65 72 2d 3e 65 4c 6f rt( (pPager->eLo
31080 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 ck==SHARED_LOCK)
31090 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 . || (
310a0 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
310b0 65 4d 6f 64 65 20 26 26 20 70 50 61 67 65 72 2d eMode && pPager-
310c0 3e 65 4c 6f 63 6b 3e 53 48 41 52 45 44 5f 4c 4f >eLock>SHARED_LO
310d0 43 4b 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 CK). );.
310e0 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 70 50 61 }.. if( !pPa
310f0 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 0a 20 ger->tempFile .
31100 20 20 20 20 26 26 20 28 70 50 61 67 65 72 2d 3e && (pPager->
31110 70 42 61 63 6b 75 70 20 7c 7c 20 73 71 6c 69 74 pBackup || sqlit
31120 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e e3PcachePagecoun
31130 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 t(pPager->pPCach
31140 65 29 3e 30 29 20 0a 20 20 20 20 29 7b 0a 20 20 e)>0) . ){.
31150 20 20 20 20 2f 2a 20 54 68 65 20 73 68 61 72 65 /* The share
31160 64 2d 6c 6f 63 6b 20 68 61 73 20 6a 75 73 74 20 d-lock has just
31170 62 65 65 6e 20 61 63 71 75 69 72 65 64 20 6f 6e been acquired on
31180 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
31190 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 le. ** and
311a0 74 68 65 72 65 20 61 72 65 20 61 6c 72 65 61 64 there are alread
311b0 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 63 y pages in the c
311c0 61 63 68 65 20 28 66 72 6f 6d 20 61 20 70 72 65 ache (from a pre
311d0 76 69 6f 75 73 0a 20 20 20 20 20 20 2a 2a 20 72 vious. ** r
311e0 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 61 ead or write tra
311f0 6e 73 61 63 74 69 6f 6e 29 2e 20 20 43 68 65 63 nsaction). Chec
31200 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 k to see if the
31210 64 61 74 61 62 61 73 65 0a 20 20 20 20 20 20 2a database. *
31220 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 * has been modif
31230 69 65 64 2e 20 20 49 66 20 74 68 65 20 64 61 74 ied. If the dat
31240 61 62 61 73 65 20 68 61 73 20 63 68 61 6e 67 65 abase has change
31250 64 2c 20 66 6c 75 73 68 20 74 68 65 0a 20 20 20 d, flush the.
31260 20 20 20 2a 2a 20 63 61 63 68 65 2e 0a 20 20 20 ** cache..
31270 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 44 **. ** D
31280 61 74 61 62 61 73 65 20 63 68 61 6e 67 65 73 20 atabase changes
31290 69 73 20 64 65 74 65 63 74 65 64 20 62 79 20 6c is detected by l
312a0 6f 6f 6b 69 6e 67 20 61 74 20 31 35 20 62 79 74 ooking at 15 byt
312b0 65 73 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20 es beginning.
312c0 20 20 20 2a 2a 20 61 74 20 6f 66 66 73 65 74 20 ** at offset
312d0 32 34 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 24 into the file
312e0 2e 20 20 54 68 65 20 66 69 72 73 74 20 34 20 6f . The first 4 o
312f0 66 20 74 68 65 73 65 20 31 36 20 62 79 74 65 73 f these 16 bytes
31300 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 20 are. ** a
31310 33 32 2d 62 69 74 20 63 6f 75 6e 74 65 72 20 74 32-bit counter t
31320 68 61 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 hat is increment
31330 65 64 20 77 69 74 68 20 65 61 63 68 20 63 68 61 ed with each cha
31340 6e 67 65 2e 20 20 54 68 65 0a 20 20 20 20 20 20 nge. The.
31350 2a 2a 20 6f 74 68 65 72 20 62 79 74 65 73 20 63 ** other bytes c
31360 68 61 6e 67 65 20 72 61 6e 64 6f 6d 6c 79 20 77 hange randomly w
31370 69 74 68 20 65 61 63 68 20 66 69 6c 65 20 63 68 ith each file ch
31380 61 6e 67 65 20 77 68 65 6e 0a 20 20 20 20 20 20 ange when.
31390 2a 2a 20 61 20 63 6f 64 65 63 20 69 73 20 69 6e ** a codec is in
313a0 20 75 73 65 2e 0a 20 20 20 20 20 20 2a 2a 20 0a use.. ** .
313b0 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20 69 ** There i
313c0 73 20 61 20 76 61 6e 69 73 68 69 6e 67 6c 79 20 s a vanishingly
313d0 73 6d 61 6c 6c 20 63 68 61 6e 63 65 20 74 68 61 small chance tha
313e0 74 20 61 20 63 68 61 6e 67 65 20 77 69 6c 6c 20 t a change will
313f0 6e 6f 74 20 62 65 20 0a 20 20 20 20 20 20 2a 2a not be . **
31400 20 64 65 74 65 63 74 65 64 2e 20 20 54 68 65 20 detected. The
31410 63 68 61 6e 63 65 20 6f 66 20 61 6e 20 75 6e 64 chance of an und
31420 65 74 65 63 74 65 64 20 63 68 61 6e 67 65 20 69 etected change i
31430 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68 61 74 0a s so small that.
31440 20 20 20 20 20 20 2a 2a 20 69 74 20 63 61 6e 20 ** it can
31450 62 65 20 6e 65 67 6c 65 63 74 65 64 2e 0a 20 20 be neglected..
31460 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 50 67 6e */. Pgn
31470 6f 20 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 o nPage = 0;.
31480 20 20 20 63 68 61 72 20 64 62 46 69 6c 65 56 65 char dbFileVe
31490 72 73 5b 73 69 7a 65 6f 66 28 70 50 61 67 65 72 rs[sizeof(pPager
314a0 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 5d 3b 0a ->dbFileVers)];.
314b0 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
314c0 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 rPagecount(pPage
314d0 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 r, &nPage);.
314e0 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
314f0 66 61 69 6c 65 64 3b 0a 0a 20 20 20 20 20 20 69 failed;.. i
31500 66 28 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20 f( nPage>0 ){.
31510 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 IOTRACE(("
31520 43 4b 56 45 52 53 20 25 70 20 25 64 5c 6e 22 2c CKVERS %p %d\n",
31530 20 70 50 61 67 65 72 2c 20 73 69 7a 65 6f 66 28 pPager, sizeof(
31540 64 62 46 69 6c 65 56 65 72 73 29 29 29 3b 0a 20 dbFileVers)));.
31550 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
31560 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 te3OsRead(pPager
31570 2d 3e 66 64 2c 20 26 64 62 46 69 6c 65 56 65 72 ->fd, &dbFileVer
31580 73 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 s, sizeof(dbFile
31590 56 65 72 73 29 2c 20 32 34 29 3b 0a 20 20 20 20 Vers), 24);.
315a0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
315b0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
315c0 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a goto failed;.
315d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
315e0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d }else{. m
315f0 65 6d 73 65 74 28 64 62 46 69 6c 65 56 65 72 73 emset(dbFileVers
31600 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 , 0, sizeof(dbFi
31610 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20 20 20 leVers));.
31620 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d }.. if( mem
31630 63 6d 70 28 70 50 61 67 65 72 2d 3e 64 62 46 69 cmp(pPager->dbFi
31640 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65 56 65 leVers, dbFileVe
31650 72 73 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c rs, sizeof(dbFil
31660 65 56 65 72 73 29 29 21 3d 30 20 29 7b 0a 20 20 eVers))!=0 ){.
31670 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 pager_rese
31680 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 t(pPager);.
31690 20 20 20 70 61 67 65 72 55 6e 6d 61 70 28 70 50 pagerUnmap(pP
316a0 61 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ager);. }.
316b0 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
316c0 74 68 65 72 65 20 69 73 20 61 20 57 41 4c 20 66 there is a WAL f
316d0 69 6c 65 20 69 6e 20 74 68 65 20 66 69 6c 65 2d ile in the file-
316e0 73 79 73 74 65 6d 2c 20 6f 70 65 6e 20 74 68 69 system, open thi
316f0 73 20 64 61 74 61 62 61 73 65 20 69 6e 20 57 41 s database in WA
31700 4c 0a 20 20 20 20 2a 2a 20 6d 6f 64 65 2e 20 4f L. ** mode. O
31710 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 66 6f therwise, the fo
31720 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e llowing function
31730 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 call is a no-op
31740 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
31750 3d 20 70 61 67 65 72 4f 70 65 6e 57 61 6c 49 66 = pagerOpenWalIf
31760 50 72 65 73 65 6e 74 28 70 50 61 67 65 72 29 3b Present(pPager);
31770 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
31780 4f 4d 49 54 5f 57 41 4c 0a 20 20 20 20 61 73 73 OMIT_WAL. ass
31790 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 57 61 ert( pPager->pWa
317a0 6c 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 l==0 || rc==SQLI
317b0 54 45 5f 4f 4b 20 29 3b 0a 23 65 6e 64 69 66 0a TE_OK );.#endif.
317c0 20 20 7d 0a 0a 20 20 69 66 28 20 70 61 67 65 72 }.. if( pager
317d0 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 UseWal(pPager) )
317e0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 {. assert( rc
317f0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 ==SQLITE_OK );.
31800 20 20 20 72 63 20 3d 20 70 61 67 65 72 42 65 67 rc = pagerBeg
31810 69 6e 52 65 61 64 54 72 61 6e 73 61 63 74 69 6f inReadTransactio
31820 6e 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a n(pPager);. }..
31830 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 53 if( pPager->eS
31840 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45 4e tate==PAGER_OPEN
31850 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f && rc==SQLITE_O
31860 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 K ){. rc = pa
31870 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 gerPagecount(pPa
31880 67 65 72 2c 20 26 70 50 61 67 65 72 2d 3e 64 62 ger, &pPager->db
31890 53 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 66 61 69 Size);. }.. fai
318a0 6c 65 64 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 led:. if( rc!=S
318b0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
318c0 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 assert( !MEMDB )
318d0 3b 0a 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f ;. pager_unlo
318e0 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 ck(pPager);.
318f0 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
31900 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 eState==PAGER_OP
31910 45 4e 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 EN );. }else{.
31920 20 20 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 pPager->eStat
31930 65 20 3d 20 50 41 47 45 52 5f 52 45 41 44 45 52 e = PAGER_READER
31940 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
31950 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 c;.}../*.** If t
31960 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 he reference cou
31970 6e 74 20 68 61 73 20 72 65 61 63 68 65 64 20 7a nt has reached z
31980 65 72 6f 2c 20 72 6f 6c 6c 62 61 63 6b 20 61 6e ero, rollback an
31990 79 20 61 63 74 69 76 65 0a 2a 2a 20 74 72 61 6e y active.** tran
319a0 73 61 63 74 69 6f 6e 20 61 6e 64 20 75 6e 6c 6f saction and unlo
319b0 63 6b 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2a ck the pager..**
319c0 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 6e 20 6c .** Except, in l
319d0 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c ocking_mode=EXCL
319e0 55 53 49 56 45 20 77 68 65 6e 20 74 68 65 72 65 USIVE when there
319f0 20 69 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 69 is nothing to i
31a00 6e 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 n.** the rollbac
31a10 6b 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 20 75 k journal, the u
31a20 6e 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 70 65 72 nlock is not per
31a30 66 6f 72 6d 65 64 20 61 6e 64 20 74 68 65 72 65 formed and there
31a40 20 69 73 0a 2a 2a 20 6e 6f 74 68 69 6e 67 20 74 is.** nothing t
31a50 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 73 6f 20 74 o rollback, so t
31a60 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
31a70 20 6e 6f 2d 6f 70 2e 0a 2a 2f 20 0a 73 74 61 74 no-op..*/ .stat
31a80 69 63 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c ic void pagerUnl
31a90 6f 63 6b 49 66 55 6e 75 73 65 64 28 50 61 67 65 ockIfUnused(Page
31aa0 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 r *pPager){. if
31ab0 28 20 28 73 71 6c 69 74 65 33 50 63 61 63 68 65 ( (sqlite3Pcache
31ac0 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d RefCount(pPager-
31ad0 3e 70 50 43 61 63 68 65 29 3d 3d 30 29 20 26 26 >pPCache)==0) &&
31ae0 20 70 50 61 67 65 72 2d 3e 6e 4d 6d 61 70 4f 75 pPager->nMmapOu
31af0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 61 67 65 t==0 ){. page
31b00 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 rUnlockAndRollba
31b10 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a ck(pPager);. }.
31b20 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 }../*.** Acquire
31b30 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 a reference to
31b40 70 61 67 65 20 6e 75 6d 62 65 72 20 70 67 6e 6f page number pgno
31b50 20 69 6e 20 70 61 67 65 72 20 70 50 61 67 65 72 in pager pPager
31b60 20 28 61 20 70 61 67 65 0a 2a 2a 20 72 65 66 65 (a page.** refe
31b70 72 65 6e 63 65 20 68 61 73 20 74 79 70 65 20 44 rence has type D
31b80 62 50 61 67 65 2a 29 2e 20 49 66 20 74 68 65 20 bPage*). If the
31b90 72 65 71 75 65 73 74 65 64 20 72 65 66 65 72 65 requested refere
31ba0 6e 63 65 20 69 73 20 0a 2a 2a 20 73 75 63 63 65 nce is .** succe
31bb0 73 73 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65 64 ssfully obtained
31bc0 2c 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 74 , it is copied t
31bd0 6f 20 2a 70 70 50 61 67 65 20 61 6e 64 20 53 51 o *ppPage and SQ
31be0 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 LITE_OK returned
31bf0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 ..**.** If the r
31c00 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73 equested page is
31c10 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
31c20 63 61 63 68 65 2c 20 69 74 20 69 73 20 72 65 74 cache, it is ret
31c30 75 72 6e 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 urned. .** Other
31c40 77 69 73 65 2c 20 61 20 6e 65 77 20 70 61 67 65 wise, a new page
31c50 20 6f 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f 63 object is alloc
31c60 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61 74 ated and populat
31c70 65 64 20 77 69 74 68 20 64 61 74 61 0a 2a 2a 20 ed with data.**
31c80 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 read from the da
31c90 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 6e 20 tabase file. In
31ca0 73 6f 6d 65 20 63 61 73 65 73 2c 20 74 68 65 20 some cases, the
31cb0 70 63 61 63 68 65 20 6d 6f 64 75 6c 65 20 6d 61 pcache module ma
31cc0 79 0a 2a 2a 20 63 68 6f 6f 73 65 20 6e 6f 74 20 y.** choose not
31cd0 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 to allocate a ne
31ce0 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61 6e w page object an
31cf0 64 20 6d 61 79 20 72 65 75 73 65 20 61 6e 20 65 d may reuse an e
31d00 78 69 73 74 69 6e 67 0a 2a 2a 20 6f 62 6a 65 63 xisting.** objec
31d10 74 20 77 69 74 68 20 6e 6f 20 6f 75 74 73 74 61 t with no outsta
31d20 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 nding references
31d30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 78 74 72 ..**.** The extr
31d40 61 20 64 61 74 61 20 61 70 70 65 6e 64 65 64 20 a data appended
31d50 74 6f 20 61 20 70 61 67 65 20 69 73 20 61 6c 77 to a page is alw
31d60 61 79 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 ays initialized
31d70 74 6f 20 7a 65 72 6f 73 20 74 68 65 20 0a 2a 2a to zeros the .**
31d80 20 66 69 72 73 74 20 74 69 6d 65 20 61 20 70 61 first time a pa
31d90 67 65 20 69 73 20 6c 6f 61 64 65 64 20 69 6e 74 ge is loaded int
31da0 6f 20 6d 65 6d 6f 72 79 2e 20 49 66 20 74 68 65 o memory. If the
31db0 20 70 61 67 65 20 72 65 71 75 65 73 74 65 64 20 page requested
31dc0 69 73 20 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 is .** already i
31dd0 6e 20 74 68 65 20 63 61 63 68 65 20 77 68 65 6e n the cache when
31de0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
31df0 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 74 s called, then t
31e00 68 65 20 65 78 74 72 61 0a 2a 2a 20 64 61 74 61 he extra.** data
31e10 20 69 73 20 6c 65 66 74 20 61 73 20 69 74 20 77 is left as it w
31e20 61 73 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 as when the page
31e30 20 6f 62 6a 65 63 74 20 77 61 73 20 6c 61 73 74 object was last
31e40 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 used..**.** If
31e50 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 the database ima
31e60 67 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 ge is smaller th
31e70 61 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 64 an the requested
31e80 20 70 61 67 65 20 6f 72 20 69 66 20 61 20 0a 2a page or if a .*
31e90 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 * non-zero value
31ea0 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 is passed as th
31eb0 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 70 61 72 61 e noContent para
31ec0 6d 65 74 65 72 20 61 6e 64 20 74 68 65 20 0a 2a meter and the .*
31ed0 2a 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 * requested page
31ee0 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 is not already
31ef0 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61 stored in the ca
31f00 63 68 65 2c 20 74 68 65 6e 20 6e 6f 20 0a 2a 2a che, then no .**
31f10 20 61 63 74 75 61 6c 20 64 69 73 6b 20 72 65 61 actual disk rea
31f20 64 20 6f 63 63 75 72 73 2e 20 49 6e 20 74 68 69 d occurs. In thi
31f30 73 20 63 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 s case the memor
31f40 79 20 69 6d 61 67 65 20 6f 66 20 74 68 65 20 0a y image of the .
31f50 2a 2a 20 70 61 67 65 20 69 73 20 69 6e 69 74 69 ** page is initi
31f60 61 6c 69 7a 65 64 20 74 6f 20 61 6c 6c 20 7a 65 alized to all ze
31f70 72 6f 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 6e ros. .**.** If n
31f80 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74 72 75 65 oContent is true
31f90 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 , it means that
31fa0 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 we do not care a
31fb0 62 6f 75 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 bout the content
31fc0 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 s.** of the page
31fd0 2e 20 54 68 69 73 20 6f 63 63 75 72 73 20 69 6e . This occurs in
31fe0 20 74 77 6f 20 73 65 70 65 72 61 74 65 20 73 63 two seperate sc
31ff0 65 6e 61 72 69 6f 73 3a 0a 2a 2a 0a 2a 2a 20 20 enarios:.**.**
32000 20 61 29 20 57 68 65 6e 20 72 65 61 64 69 6e 67 a) When reading
32010 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 a free-list lea
32020 66 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 f page from the
32030 64 61 74 61 62 61 73 65 2c 20 61 6e 64 0a 2a 2a database, and.**
32040 0a 2a 2a 20 20 20 62 29 20 57 68 65 6e 20 61 20 .** b) When a
32050 73 61 76 65 70 6f 69 6e 74 20 69 73 20 62 65 69 savepoint is bei
32060 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 ng rolled back a
32070 6e 64 20 77 65 20 6e 65 65 64 20 74 6f 20 6c 6f nd we need to lo
32080 61 64 0a 2a 2a 20 20 20 20 20 20 61 20 6e 65 77 ad.** a new
32090 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 63 page into the c
320a0 61 63 68 65 20 74 6f 20 62 65 20 66 69 6c 6c 65 ache to be fille
320b0 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 20 d with the data
320c0 72 65 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f read.** fro
320d0 6d 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 m the savepoint
320e0 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 journal..**.** I
320f0 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74 f noContent is t
32100 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 64 61 rue, then the da
32110 74 61 20 72 65 74 75 72 6e 65 64 20 69 73 20 7a ta returned is z
32120 65 72 6f 65 64 20 69 6e 73 74 65 61 64 20 6f 66 eroed instead of
32130 0a 2a 2a 20 62 65 69 6e 67 20 72 65 61 64 20 66 .** being read f
32140 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
32150 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 . Additionally,
32160 74 68 65 20 62 69 74 73 20 63 6f 72 72 65 73 70 the bits corresp
32170 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 70 67 6e onding.** to pgn
32180 6f 20 69 6e 20 50 61 67 65 72 2e 70 49 6e 4a 6f o in Pager.pInJo
32190 75 72 6e 61 6c 20 28 62 69 74 76 65 63 20 6f 66 urnal (bitvec of
321a0 20 70 61 67 65 73 20 61 6c 72 65 61 64 79 20 77 pages already w
321b0 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a ritten to the.**
321c0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 61 journal file) a
321d0 6e 64 20 74 68 65 20 50 61 67 65 72 53 61 76 65 nd the PagerSave
321e0 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 point.pInSavepoi
321f0 6e 74 20 62 69 74 76 65 63 73 20 6f 66 20 61 6e nt bitvecs of an
32200 79 20 6f 70 65 6e 0a 2a 2a 20 73 61 76 65 70 6f y open.** savepo
32210 69 6e 74 73 20 61 72 65 20 73 65 74 2e 20 54 68 ints are set. Th
32220 69 73 20 6d 65 61 6e 73 20 69 66 20 74 68 65 20 is means if the
32230 70 61 67 65 20 69 73 20 6d 61 64 65 20 77 72 69 page is made wri
32240 74 61 62 6c 65 20 61 74 20 61 6e 79 0a 2a 2a 20 table at any.**
32250 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 point in the fut
32260 75 72 65 2c 20 75 73 69 6e 67 20 61 20 63 61 6c ure, using a cal
32270 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 l to sqlite3Page
32280 72 57 72 69 74 65 28 29 2c 20 69 74 73 20 63 6f rWrite(), its co
32290 6e 74 65 6e 74 73 0a 2a 2a 20 77 69 6c 6c 20 6e ntents.** will n
322a0 6f 74 20 62 65 20 6a 6f 75 72 6e 61 6c 65 64 2e ot be journaled.
322b0 20 54 68 69 73 20 73 61 76 65 73 20 49 4f 2e 0a This saves IO..
322c0 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 71 75 69 73 **.** The acquis
322d0 69 74 69 6f 6e 20 6d 69 67 68 74 20 66 61 69 6c ition might fail
322e0 20 66 6f 72 20 73 65 76 65 72 61 6c 20 72 65 61 for several rea
322f0 73 6f 6e 73 2e 20 20 49 6e 20 61 6c 6c 20 63 61 sons. In all ca
32300 73 65 73 2c 0a 2a 2a 20 61 6e 20 61 70 70 72 6f ses,.** an appro
32310 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64 priate error cod
32320 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e e is returned an
32330 64 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74 d *ppPage is set
32340 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 to NULL..**.**
32350 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 See also sqlite3
32360 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 2e 20 20 PagerLookup().
32370 42 6f 74 68 20 74 68 69 73 20 72 6f 75 74 69 6e Both this routin
32380 65 20 61 6e 64 20 4c 6f 6f 6b 75 70 28 29 20 61 e and Lookup() a
32390 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 66 69 6e ttempt.** to fin
323a0 64 20 61 20 70 61 67 65 20 69 6e 20 74 68 65 20 d a page in the
323b0 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 in-memory cache
323c0 66 69 72 73 74 2e 20 20 49 66 20 74 68 65 20 70 first. If the p
323d0 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 age is not alrea
323e0 64 79 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 2c dy.** in memory,
323f0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 67 6f this routine go
32400 65 73 20 74 6f 20 64 69 73 6b 20 74 6f 20 72 65 es to disk to re
32410 61 64 20 69 74 20 69 6e 20 77 68 65 72 65 61 73 ad it in whereas
32420 20 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 6a 75 73 Lookup().** jus
32430 74 20 72 65 74 75 72 6e 73 20 30 2e 20 20 54 68 t returns 0. Th
32440 69 73 20 72 6f 75 74 69 6e 65 20 61 63 71 75 69 is routine acqui
32450 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 res a read-lock
32460 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69 the first time i
32470 74 0a 2a 2a 20 68 61 73 20 74 6f 20 67 6f 20 74 t.** has to go t
32480 6f 20 64 69 73 6b 2c 20 61 6e 64 20 63 6f 75 6c o disk, and coul
32490 64 20 61 6c 73 6f 20 70 6c 61 79 62 61 63 6b 20 d also playback
324a0 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 69 an old journal i
324b0 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 f necessary..**
324c0 53 69 6e 63 65 20 4c 6f 6f 6b 75 70 28 29 20 6e Since Lookup() n
324d0 65 76 65 72 20 67 6f 65 73 20 74 6f 20 64 69 73 ever goes to dis
324e0 6b 2c 20 69 74 20 6e 65 76 65 72 20 68 61 73 20 k, it never has
324f0 74 6f 20 64 65 61 6c 20 77 69 74 68 20 6c 6f 63 to deal with loc
32500 6b 73 0a 2a 2a 20 6f 72 20 6a 6f 75 72 6e 61 6c ks.** or journal
32510 20 66 69 6c 65 73 2e 0a 2a 2f 0a 69 6e 74 20 73 files..*/.int s
32520 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 qlite3PagerAcqui
32530 72 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 re(. Pager *pPa
32540 67 65 72 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 ger, /* The
32550 20 70 61 67 65 72 20 6f 70 65 6e 20 6f 6e 20 74 pager open on t
32560 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
32570 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c */. Pgno pgno,
32580 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
32590 65 20 6e 75 6d 62 65 72 20 74 6f 20 66 65 74 63 e number to fetc
325a0 68 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 2a h */. DbPage **
325b0 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 57 72 ppPage, /* Wr
325c0 69 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ite a pointer to
325d0 20 74 68 65 20 70 61 67 65 20 68 65 72 65 20 2a the page here *
325e0 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e /. int noConten
325f0 74 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f t /* Do no
32600 74 20 62 6f 74 68 65 72 20 72 65 61 64 69 6e 67 t bother reading
32610 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 64 69 content from di
32620 73 6b 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b sk if true */.){
32630 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 48 . int rc;. PgH
32640 64 72 20 2a 70 50 67 3b 0a 0a 20 20 61 73 73 65 dr *pPg;.. asse
32650 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 rt( pPager->eSta
32660 74 65 3e 3d 50 41 47 45 52 5f 52 45 41 44 45 52 te>=PAGER_READER
32670 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 73 );. assert( as
32680 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 sert_pager_state
32690 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 69 (pPager) );.. i
326a0 66 28 20 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20 20 f( pgno==0 ){.
326b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
326c0 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
326d0 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 }.. /* If the p
326e0 61 67 65 72 20 69 73 20 69 6e 20 74 68 65 20 65 ager is in the e
326f0 72 72 6f 72 20 73 74 61 74 65 2c 20 72 65 74 75 rror state, retu
32700 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6d 6d 65 rn an error imme
32710 64 69 61 74 65 6c 79 2e 20 0a 20 20 2a 2a 20 4f diately. . ** O
32720 74 68 65 72 77 69 73 65 2c 20 72 65 71 75 65 73 therwise, reques
32730 74 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 t the page from
32740 74 68 65 20 50 43 61 63 68 65 20 6c 61 79 65 72 the PCache layer
32750 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 . */. if( pPage
32760 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 r->errCode!=SQLI
32770 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 TE_OK ){. rc
32780 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 = pPager->errCod
32790 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 e;. }else{.
327a0 69 66 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 if( pPager->eSta
327b0 74 65 3d 3d 50 41 47 45 52 5f 52 45 41 44 45 52 te==PAGER_READER
327c0 20 26 26 20 70 50 61 67 65 72 2d 3e 70 57 61 6c && pPager->pWal
327d0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 ==0 ){. rc
327e0 3d 20 70 61 67 65 72 41 63 71 75 69 72 65 4d 61 = pagerAcquireMa
327f0 70 50 61 67 65 28 70 50 61 67 65 72 2c 20 70 67 pPage(pPager, pg
32800 6e 6f 2c 20 26 70 50 67 29 3b 0a 20 20 20 20 20 no, &pPg);.
32810 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
32820 4f 4b 20 29 20 67 6f 74 6f 20 70 61 67 65 72 5f OK ) goto pager_
32830 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 acquire_err;.
32840 20 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 if( pPg ){.
32850 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 *ppPage =
32860 70 50 67 3b 0a 20 20 20 20 20 20 20 20 72 65 74 pPg;. ret
32870 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
32880 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 }. }..
32890 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 63 rc = sqlite3Pc
328a0 61 63 68 65 46 65 74 63 68 28 70 50 61 67 65 72 acheFetch(pPager
328b0 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c ->pPCache, pgno,
328c0 20 31 2c 20 70 70 50 61 67 65 29 3b 0a 20 20 7d 1, ppPage);. }
328d0 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 .. if( rc!=SQLI
328e0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 TE_OK ){. /*
328f0 45 69 74 68 65 72 20 74 68 65 20 63 61 6c 6c 20 Either the call
32900 74 6f 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 to sqlite3Pcache
32910 46 65 74 63 68 28 29 20 72 65 74 75 72 6e 65 64 Fetch() returned
32920 20 61 6e 20 65 72 72 6f 72 20 6f 72 20 74 68 65 an error or the
32930 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 20 77 61 . ** pager wa
32940 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 s already in the
32950 20 65 72 72 6f 72 2d 73 74 61 74 65 20 77 68 65 error-state whe
32960 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
32970 77 61 73 20 63 61 6c 6c 65 64 2e 0a 20 20 20 20 was called..
32980 2a 2a 20 53 65 74 20 70 50 67 20 74 6f 20 30 20 ** Set pPg to 0
32990 61 6e 64 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 and jump to the
329a0 65 78 63 65 70 74 69 6f 6e 20 68 61 6e 64 6c 65 exception handle
329b0 72 2e 20 20 2a 2f 0a 20 20 20 20 70 50 67 20 3d r. */. pPg =
329c0 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 70 61 67 0;. goto pag
329d0 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b 0a er_acquire_err;.
329e0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 2a }. assert( (*
329f0 70 70 50 61 67 65 29 2d 3e 70 67 6e 6f 3d 3d 70 ppPage)->pgno==p
32a00 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 gno );. assert(
32a10 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 (*ppPage)->pPag
32a20 65 72 3d 3d 70 50 61 67 65 72 20 7c 7c 20 28 2a er==pPager || (*
32a30 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65 72 3d ppPage)->pPager=
32a40 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 28 2a 70 =0 );.. if( (*p
32a50 70 50 61 67 65 29 2d 3e 70 50 61 67 65 72 20 26 pPage)->pPager &
32a60 26 20 21 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a & !noContent ){.
32a70 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 /* In this c
32a80 61 73 65 20 74 68 65 20 70 63 61 63 68 65 20 61 ase the pcache a
32a90 6c 72 65 61 64 79 20 63 6f 6e 74 61 69 6e 73 20 lready contains
32aa0 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 63 an initialized c
32ab0 6f 70 79 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 opy of. ** th
32ac0 65 20 70 61 67 65 2e 20 52 65 74 75 72 6e 20 77 e page. Return w
32ad0 69 74 68 6f 75 74 20 66 75 72 74 68 65 72 20 61 ithout further a
32ae0 64 6f 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 do. */. asse
32af0 72 74 28 20 70 67 6e 6f 3c 3d 50 41 47 45 52 5f rt( pgno<=PAGER_
32b00 4d 41 58 5f 50 47 4e 4f 20 26 26 20 70 67 6e 6f MAX_PGNO && pgno
32b10 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 !=PAGER_MJ_PGNO(
32b20 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 70 pPager) );. p
32b30 50 61 67 65 72 2d 3e 61 53 74 61 74 5b 50 41 47 Pager->aStat[PAG
32b40 45 52 5f 53 54 41 54 5f 48 49 54 5d 2b 2b 3b 0a ER_STAT_HIT]++;.
32b50 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
32b60 45 5f 4f 4b 3b 0a 0a 20 20 7d 65 6c 73 65 7b 0a E_OK;.. }else{.
32b70 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 /* The pager
32b80 20 63 61 63 68 65 20 68 61 73 20 63 72 65 61 74 cache has creat
32b90 65 64 20 61 20 6e 65 77 20 70 61 67 65 2e 20 49 ed a new page. I
32ba0 74 73 20 63 6f 6e 74 65 6e 74 20 6e 65 65 64 73 ts content needs
32bb0 20 74 6f 20 0a 20 20 20 20 2a 2a 20 62 65 20 69 to . ** be i
32bc0 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 2a 2f 0a nitialized. */.
32bd0 0a 20 20 20 20 70 50 67 20 3d 20 2a 70 70 50 61 . pPg = *ppPa
32be0 67 65 3b 0a 20 20 20 20 70 50 67 2d 3e 70 50 61 ge;. pPg->pPa
32bf0 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a 0a 20 ger = pPager;..
32c00 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 /* The maximu
32c10 6d 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 m page number is
32c20 20 32 5e 33 31 2e 20 52 65 74 75 72 6e 20 53 51 2^31. Return SQ
32c30 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 66 20 LITE_CORRUPT if
32c40 61 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 6e 75 a page. ** nu
32c50 6d 62 65 72 20 67 72 65 61 74 65 72 20 74 68 61 mber greater tha
32c60 6e 20 74 68 69 73 2c 20 6f 72 20 74 68 65 20 75 n this, or the u
32c70 6e 75 73 65 64 20 6c 6f 63 6b 69 6e 67 2d 70 61 nused locking-pa
32c80 67 65 2c 20 69 73 20 72 65 71 75 65 73 74 65 64 ge, is requested
32c90 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e . */. if( pgn
32ca0 6f 3e 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f o>PAGER_MAX_PGNO
32cb0 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f || pgno==PAGER_
32cc0 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 MJ_PGNO(pPager)
32cd0 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
32ce0 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
32cf0 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 T;. goto pa
32d00 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b ger_acquire_err;
32d10 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
32d20 4d 45 4d 44 42 20 7c 7c 20 70 50 61 67 65 72 2d MEMDB || pPager-
32d30 3e 64 62 53 69 7a 65 3c 70 67 6e 6f 20 7c 7c 20 >dbSize<pgno ||
32d40 6e 6f 43 6f 6e 74 65 6e 74 20 7c 7c 20 21 69 73 noContent || !is
32d50 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
32d60 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 67 ){. if( pg
32d70 6e 6f 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e no>pPager->mxPgn
32d80 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 o ){. rc
32d90 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 = SQLITE_FULL;.
32da0 20 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 goto page
32db0 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 r_acquire_err;.
32dc0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
32dd0 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 noContent ){.
32de0 20 20 20 20 20 20 2f 2a 20 46 61 69 6c 75 72 65 /* Failure
32df0 20 74 6f 20 73 65 74 20 74 68 65 20 62 69 74 73 to set the bits
32e00 20 69 6e 20 74 68 65 20 49 6e 4a 6f 75 72 6e 61 in the InJourna
32e10 6c 20 62 69 74 2d 76 65 63 74 6f 72 73 20 69 73 l bit-vectors is
32e20 20 62 65 6e 69 67 6e 2e 0a 20 20 20 20 20 20 20 benign..
32e30 20 2a 2a 20 49 74 20 6d 65 72 65 6c 79 20 6d 65 ** It merely me
32e40 61 6e 73 20 74 68 61 74 20 77 65 20 6d 69 67 68 ans that we migh
32e50 74 20 64 6f 20 73 6f 6d 65 20 65 78 74 72 61 20 t do some extra
32e60 77 6f 72 6b 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 work to journal
32e70 61 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 a . ** pa
32e80 67 65 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 ge that does not
32e90 20 6e 65 65 64 20 74 6f 20 62 65 20 6a 6f 75 72 need to be jour
32ea0 6e 61 6c 65 64 2e 20 20 4e 65 76 65 72 74 68 65 naled. Neverthe
32eb0 6c 65 73 73 2c 20 62 65 20 73 75 72 65 20 0a 20 less, be sure .
32ec0 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 65 73 ** to tes
32ed0 74 20 74 68 65 20 63 61 73 65 20 77 68 65 72 65 t the case where
32ee0 20 61 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 a malloc error
32ef0 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 occurs while try
32f00 69 6e 67 20 74 6f 20 73 65 74 20 0a 20 20 20 20 ing to set .
32f10 20 20 20 20 2a 2a 20 61 20 62 69 74 20 69 6e 20 ** a bit in
32f20 61 20 62 69 74 20 76 65 63 74 6f 72 2e 0a 20 20 a bit vector..
32f30 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
32f40 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e sqlite3BeginBen
32f50 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 ignMalloc();.
32f60 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 if( pgno<=p
32f70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a Pager->dbOrigSiz
32f80 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 54 e ){. T
32f90 45 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 ESTONLY( rc = )
32fa0 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 sqlite3BitvecSet
32fb0 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 (pPager->pInJour
32fc0 6e 61 6c 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 nal, pgno);.
32fd0 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
32fe0 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
32ff0 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 );. }.
33000 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 TESTONLY(
33010 72 63 20 3d 20 29 20 61 64 64 54 6f 53 61 76 65 rc = ) addToSave
33020 70 6f 69 6e 74 42 69 74 76 65 63 73 28 70 50 61 pointBitvecs(pPa
33030 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 ger, pgno);.
33040 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 testcase( rc
33050 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 ==SQLITE_NOMEM )
33060 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
33070 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 3EndBenignMalloc
33080 28 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ();. }.
33090 20 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e 70 44 memset(pPg->pD
330a0 61 74 61 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e ata, 0, pPager->
330b0 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 pageSize);.
330c0 20 49 4f 54 52 41 43 45 28 28 22 5a 45 52 4f 20 IOTRACE(("ZERO
330d0 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 %p %d\n", pPager
330e0 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 65 , pgno));. }e
330f0 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 lse{. asser
33100 74 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d t( pPg->pPager==
33110 70 50 61 67 65 72 20 29 3b 0a 20 20 20 20 20 20 pPager );.
33120 70 50 61 67 65 72 2d 3e 61 53 74 61 74 5b 50 41 pPager->aStat[PA
33130 47 45 52 5f 53 54 41 54 5f 4d 49 53 53 5d 2b 2b GER_STAT_MISS]++
33140 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 ;. rc = rea
33150 64 44 62 50 61 67 65 28 70 50 67 29 3b 0a 20 20 dDbPage(pPg);.
33160 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
33170 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
33180 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75 goto pager_acqu
33190 69 72 65 5f 65 72 72 3b 0a 20 20 20 20 20 20 7d ire_err;. }
331a0 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 67 65 72 . }. pager
331b0 5f 73 65 74 5f 70 61 67 65 68 61 73 68 28 70 50 _set_pagehash(pP
331c0 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 g);. }.. retur
331d0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 n SQLITE_OK;..pa
331e0 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3a ger_acquire_err:
331f0 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 . assert( rc!=S
33200 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 QLITE_OK );. if
33210 28 20 70 50 67 20 29 7b 0a 20 20 20 20 73 71 6c ( pPg ){. sql
33220 69 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 70 ite3PcacheDrop(p
33230 50 67 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65 72 Pg);. }. pager
33240 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 70 UnlockIfUnused(p
33250 50 61 67 65 72 29 3b 0a 0a 20 20 2a 70 70 50 61 Pager);.. *ppPa
33260 67 65 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e ge = 0;. return
33270 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 rc;.}../*.** Ac
33280 71 75 69 72 65 20 61 20 70 61 67 65 20 69 66 20 quire a page if
33290 69 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e it is already in
332a0 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 the in-memory c
332b0 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20 6e 6f 74 ache. Do.** not
332c0 20 72 65 61 64 20 74 68 65 20 70 61 67 65 20 66 read the page f
332d0 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65 74 75 72 rom disk. Retur
332e0 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
332f0 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f 72 20 30 he page,.** or 0
33300 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 if the page is
33310 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e 20 0a 2a not in cache. .*
33320 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 *.** See also sq
33330 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 2e lite3PagerGet().
33340 20 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 The difference
33350 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 72 6f between this ro
33360 75 74 69 6e 65 0a 2a 2a 20 61 6e 64 20 73 71 6c utine.** and sql
33370 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 69 ite3PagerGet() i
33380 73 20 74 68 61 74 20 5f 67 65 74 28 29 20 77 69 s that _get() wi
33390 6c 6c 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73 ll go to the dis
333a0 6b 20 61 6e 64 20 72 65 61 64 0a 2a 2a 20 69 6e k and read.** in
333b0 20 74 68 65 20 70 61 67 65 20 69 66 20 74 68 65 the page if the
333c0 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 page is not alr
333d0 65 61 64 79 20 69 6e 20 63 61 63 68 65 2e 20 20 eady in cache.
333e0 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 This routine.**
333f0 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 returns NULL if
33400 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 the page is not
33410 69 6e 20 63 61 63 68 65 20 6f 72 20 69 66 20 61 in cache or if a
33420 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20 disk I/O error
33430 0a 2a 2a 20 68 61 73 20 65 76 65 72 20 68 61 70 .** has ever hap
33440 70 65 6e 65 64 2e 0a 2a 2f 0a 44 62 50 61 67 65 pened..*/.DbPage
33450 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f *sqlite3PagerLo
33460 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 okup(Pager *pPag
33470 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a er, Pgno pgno){.
33480 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 30 PgHdr *pPg = 0
33490 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
334a0 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 er!=0 );. asser
334b0 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 20 20 t( pgno!=0 );.
334c0 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
334d0 70 50 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 20 pPCache!=0 );.
334e0 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
334f0 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 eState>=PAGER_RE
33500 41 44 45 52 20 26 26 20 70 50 61 67 65 72 2d 3e ADER && pPager->
33510 65 53 74 61 74 65 21 3d 50 41 47 45 52 5f 45 52 eState!=PAGER_ER
33520 52 4f 52 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ROR );. sqlite3
33530 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67 PcacheFetch(pPag
33540 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e er->pPCache, pgn
33550 6f 2c 20 30 2c 20 26 70 50 67 29 3b 0a 20 20 72 o, 0, &pPg);. r
33560 65 74 75 72 6e 20 70 50 67 3b 0a 7d 0a 0a 2f 2a eturn pPg;.}../*
33570 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 70 61 .** Release a pa
33580 67 65 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a 2a ge reference..**
33590 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d 62 65 .** If the numbe
335a0 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 r of references
335b0 74 6f 20 74 68 65 20 70 61 67 65 20 64 72 6f 70 to the page drop
335c0 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 to zero, then t
335d0 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 64 he.** page is ad
335e0 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55 20 6c ded to the LRU l
335f0 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c 20 72 ist. When all r
33600 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6c 6c eferences to all
33610 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20 72 65 pages.** are re
33620 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c 62 61 leased, a rollba
33630 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68 ck occurs and th
33640 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 e lock on the da
33650 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72 65 6d tabase is.** rem
33660 6f 76 65 64 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 oved..*/.void sq
33670 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
33680 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 DbPage *pPg){.
33690 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 50 if( pPg ){. P
336a0 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 ager *pPager = p
336b0 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 20 Pg->pPager;.
336c0 69 66 28 20 70 50 67 2d 3e 66 6c 61 67 73 20 26 if( pPg->flags &
336d0 20 50 47 48 44 52 5f 4d 4d 41 50 20 29 7b 0a 20 PGHDR_MMAP ){.
336e0 20 20 20 20 20 70 61 67 65 72 52 65 6c 65 61 73 pagerReleas
336f0 65 4d 61 70 50 61 67 65 28 70 50 67 29 3b 0a 20 eMapPage(pPg);.
33700 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
33710 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c sqlite3PcacheRel
33720 65 61 73 65 28 70 50 67 29 3b 0a 20 20 20 20 7d ease(pPg);. }
33730 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b . pagerUnlock
33740 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 72 29 IfUnused(pPager)
33750 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 ;. }.}../*.** T
33760 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
33770 63 61 6c 6c 65 64 20 61 74 20 74 68 65 20 73 74 called at the st
33780 61 72 74 20 6f 66 20 65 76 65 72 79 20 77 72 69 art of every wri
33790 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a te transaction..
337a0 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 61 6c ** There must al
337b0 72 65 61 64 79 20 62 65 20 61 20 52 45 53 45 52 ready be a RESER
337c0 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 VED or EXCLUSIVE
337d0 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
337e0 61 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65 20 77 abase .** file w
337f0 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
33800 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a is called..**.*
33810 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e * Open the journ
33820 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65 al file for page
33830 72 20 70 50 61 67 65 72 20 61 6e 64 20 77 72 69 r pPager and wri
33840 74 65 20 61 20 6a 6f 75 72 6e 61 6c 20 68 65 61 te a journal hea
33850 64 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 der.** to the st
33860 61 72 74 20 6f 66 20 69 74 2e 20 49 66 20 74 68 art of it. If th
33870 65 72 65 20 61 72 65 20 61 63 74 69 76 65 20 73 ere are active s
33880 61 76 65 70 6f 69 6e 74 73 2c 20 6f 70 65 6e 20 avepoints, open
33890 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a the sub-journal.
338a0 2a 2a 20 61 73 20 77 65 6c 6c 2e 20 54 68 69 73 ** as well. This
338b0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c function is onl
338c0 79 20 75 73 65 64 20 77 68 65 6e 20 74 68 65 20 y used when the
338d0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
338e0 62 65 69 6e 67 20 0a 2a 2a 20 6f 70 65 6e 65 64 being .** opened
338f0 20 74 6f 20 77 72 69 74 65 20 61 20 72 6f 6c 6c to write a roll
33900 62 61 63 6b 20 6c 6f 67 20 66 6f 72 20 61 20 74 back log for a t
33910 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 74 20 69 ransaction. It i
33920 73 20 6e 6f 74 20 75 73 65 64 20 0a 2a 2a 20 77 s not used .** w
33930 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20 68 6f hen opening a ho
33940 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 t journal file t
33950 6f 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 0a o roll it back..
33960 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 **.** If the jou
33970 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 61 6c 72 rnal file is alr
33980 65 61 64 79 20 6f 70 65 6e 20 28 61 73 20 69 74 eady open (as it
33990 20 6d 61 79 20 62 65 20 69 6e 20 65 78 63 6c 75 may be in exclu
339a0 73 69 76 65 20 6d 6f 64 65 29 2c 0a 2a 2a 20 74 sive mode),.** t
339b0 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
339c0 6e 20 6a 75 73 74 20 77 72 69 74 65 73 20 61 20 n just writes a
339d0 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 74 journal header t
339e0 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 o the start of t
339f0 68 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 6f 70 he.** already op
33a00 65 6e 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 en file. .**.**
33a10 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 Whether or not t
33a20 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
33a30 69 73 20 6f 70 65 6e 65 64 20 62 79 20 74 68 69 is opened by thi
33a40 73 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 0a s function, the.
33a50 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 ** Pager.pInJour
33a60 6e 61 6c 20 62 69 74 76 65 63 20 73 74 72 75 63 nal bitvec struc
33a70 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 ture is allocate
33a80 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 d..**.** Return
33a90 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 SQLITE_OK if eve
33aa0 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 rything is succe
33ab0 73 73 66 75 6c 2e 20 4f 74 68 65 72 77 69 73 65 ssful. Otherwise
33ac0 2c 20 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c , return .** SQL
33ad0 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 74 68 65 ITE_NOMEM if the
33ae0 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f attempt to allo
33af0 63 61 74 65 20 50 61 67 65 72 2e 70 49 6e 4a 6f cate Pager.pInJo
33b00 75 72 6e 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20 urnal fails, or
33b10 0a 2a 2a 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 .** an IO error
33b20 63 6f 64 65 20 69 66 20 6f 70 65 6e 69 6e 67 20 code if opening
33b30 6f 72 20 77 72 69 74 69 6e 67 20 74 68 65 20 6a or writing the j
33b40 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 61 69 6c ournal file fail
33b50 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
33b60 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 pager_open_jour
33b70 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 nal(Pager *pPage
33b80 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 r){. int rc = S
33b90 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 QLITE_OK;
33ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
33bb0 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
33bc0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 */. sqlite3_vfs
33bd0 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 * const pVfs =
33be0 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 20 20 20 pPager->pVfs;
33bf0 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f /* Local cache o
33c00 66 20 76 66 73 20 70 6f 69 6e 74 65 72 20 2a 2f f vfs pointer */
33c10 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
33c20 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 er->eState==PAGE
33c30 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 20 R_WRITER_LOCKED
33c40 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 73 73 );. assert( ass
33c50 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 ert_pager_state(
33c60 70 50 61 67 65 72 29 20 29 3b 0a 20 20 61 73 73 pPager) );. ass
33c70 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e ert( pPager->pIn
33c80 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 Journal==0 );.
33c90 0a 20 20 2f 2a 20 49 66 20 61 6c 72 65 61 64 79 . /* If already
33ca0 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 in the error st
33cb0 61 74 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 ate, this functi
33cc0 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 on is a no-op.
33cd0 42 75 74 20 6f 6e 0a 20 20 2a 2a 20 74 68 65 20 But on. ** the
33ce0 6f 74 68 65 72 20 68 61 6e 64 2c 20 74 68 69 73 other hand, this
33cf0 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 76 65 routine is neve
33d00 72 20 63 61 6c 6c 65 64 20 69 66 20 77 65 20 61 r called if we a
33d10 72 65 20 61 6c 72 65 61 64 79 20 69 6e 0a 20 20 re already in.
33d20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 ** an error stat
33d30 65 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 e. */. if( NEVE
33d40 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 R(pPager->errCod
33d50 65 29 20 29 20 72 65 74 75 72 6e 20 70 50 61 67 e) ) return pPag
33d60 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 er->errCode;..
33d70 69 66 28 20 21 70 61 67 65 72 55 73 65 57 61 6c if( !pagerUseWal
33d80 28 70 50 61 67 65 72 29 20 26 26 20 70 50 61 67 (pPager) && pPag
33d90 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 er->journalMode!
33da0 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
33db0 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20 20 70 50 DE_OFF ){. pP
33dc0 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
33dd0 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 = sqlite3Bitvec
33de0 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e 64 Create(pPager->d
33df0 62 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 20 bSize);. if(
33e00 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
33e10 61 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 al==0 ){. r
33e20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
33e30 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 EM;. }. .
33e40 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 /* Open the jou
33e50 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 69 74 20 rnal file if it
33e60 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f is not already o
33e70 70 65 6e 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 pen. */. if(
33e80 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e !isOpen(pPager->
33e90 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 69 66 jfd) ){. if
33ea0 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
33eb0 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
33ec0 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 RNALMODE_MEMORY
33ed0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
33ee0 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e e3MemJournalOpen
33ef0 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 (pPager->jfd);.
33f00 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
33f10 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c const int fl
33f20 61 67 73 20 3d 20 20 20 20 20 20 20 20 20 20 20 ags =
33f30 20 20 20 20 20 20 20 20 2f 2a 20 56 46 53 20 66 /* VFS f
33f40 6c 61 67 73 20 74 6f 20 6f 70 65 6e 20 6a 6f 75 lags to open jou
33f50 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 20 rnal file */.
33f60 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 SQLITE_OP
33f70 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c EN_READWRITE|SQL
33f80 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c ITE_OPEN_CREATE|
33f90 0a 20 20 20 20 20 20 20 20 20 20 28 70 50 61 67 . (pPag
33fa0 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3f 20 0a er->tempFile ? .
33fb0 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c (SQL
33fc0 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f ITE_OPEN_DELETEO
33fd0 4e 43 4c 4f 53 45 7c 53 51 4c 49 54 45 5f 4f 50 NCLOSE|SQLITE_OP
33fe0 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 29 EN_TEMP_JOURNAL)
33ff0 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 28 53 :. (S
34000 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
34010 4a 4f 55 52 4e 41 4c 29 0a 20 20 20 20 20 20 20 JOURNAL).
34020 20 20 20 29 3b 0a 20 20 23 69 66 64 65 66 20 53 );. #ifdef S
34030 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f QLITE_ENABLE_ATO
34040 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 20 20 MIC_WRITE.
34050 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4a 6f rc = sqlite3Jo
34060 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 20 20 20 urnalOpen(.
34070 20 20 20 20 20 20 20 70 56 66 73 2c 20 70 50 61 pVfs, pPa
34080 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 ger->zJournal, p
34090 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 Pager->jfd, flag
340a0 73 2c 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a s, jrnlBufferSiz
340b0 65 28 70 50 61 67 65 72 29 0a 20 20 20 20 20 20 e(pPager).
340c0 20 20 29 3b 0a 20 20 23 65 6c 73 65 0a 20 20 20 );. #else.
340d0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
340e0 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 3OsOpen(pVfs, pP
340f0 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 ager->zJournal,
34100 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 pPager->jfd, fla
34110 67 73 2c 20 30 29 3b 0a 20 20 23 65 6e 64 69 66 gs, 0);. #endif
34120 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
34130 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
34140 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 E_OK || isOpen(p
34150 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 Pager->jfd) );.
34160 20 20 20 7d 0a 20 20 0a 20 20 0a 20 20 20 20 2f }. . . /
34170 2a 20 57 72 69 74 65 20 74 68 65 20 66 69 72 73 * Write the firs
34180 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 t journal header
34190 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 to the journal
341a0 66 69 6c 65 20 61 6e 64 20 6f 70 65 6e 20 0a 20 file and open .
341b0 20 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f ** the sub-jo
341c0 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61 urnal if necessa
341d0 72 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ry.. */. i
341e0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
341f0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 ){. /* TOD
34200 4f 3a 20 43 68 65 63 6b 20 69 66 20 61 6c 6c 20 O: Check if all
34210 6f 66 20 74 68 65 73 65 20 61 72 65 20 72 65 61 of these are rea
34220 6c 6c 79 20 72 65 71 75 69 72 65 64 2e 20 2a 2f lly required. */
34230 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e . pPager->n
34240 52 65 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 Rec = 0;. p
34250 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
34260 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 f = 0;. pPa
34270 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d ger->setMaster =
34280 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 0;. pPager
34290 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 ->journalHdr = 0
342a0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 ;. rc = wri
342b0 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 teJournalHdr(pPa
342c0 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ger);. }. }.
342d0 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
342e0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 E_OK ){. sqli
342f0 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 te3BitvecDestroy
34300 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 (pPager->pInJour
34310 6e 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 nal);. pPager
34320 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 ->pInJournal = 0
34330 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
34340 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 ssert( pPager->e
34350 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 State==PAGER_WRI
34360 54 45 52 5f 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 TER_LOCKED );.
34370 20 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 pPager->eState
34380 20 3d 20 50 41 47 45 52 5f 57 52 49 54 45 52 5f = PAGER_WRITER_
34390 43 41 43 48 45 4d 4f 44 3b 0a 20 20 7d 0a 0a 20 CACHEMOD;. }..
343a0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
343b0 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 77 72 69 *.** Begin a wri
343c0 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f te-transaction o
343d0 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 n the specified
343e0 70 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 49 66 pager object. If
343f0 20 61 20 0a 2a 2a 20 77 72 69 74 65 2d 74 72 61 a .** write-tra
34400 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 nsaction has alr
34410 65 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65 64 eady been opened
34420 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 , this function
34430 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
34440 2a 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 20 * If the exFlag
34450 61 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 argument is fals
34460 65 2c 20 74 68 65 6e 20 61 63 71 75 69 72 65 20 e, then acquire
34470 61 74 20 6c 65 61 73 74 20 61 20 52 45 53 45 52 at least a RESER
34480 56 45 44 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 74 VED.** lock on t
34490 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
344a0 2e 20 49 66 20 65 78 46 6c 61 67 20 69 73 20 74 . If exFlag is t
344b0 72 75 65 2c 20 74 68 65 6e 20 61 63 71 75 69 72 rue, then acquir
344c0 65 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 61 6e e at least.** an
344d0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e EXCLUSIVE lock.
344e0 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 If such a lock
344f0 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c is already held,
34500 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a 20 no locking .**
34510 66 75 6e 63 74 69 6f 6e 73 20 6e 65 65 64 20 62 functions need b
34520 65 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 e called..**.**
34530 49 66 20 74 68 65 20 73 75 62 6a 49 6e 4d 65 6d If the subjInMem
34540 6f 72 79 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ory argument is
34550 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 non-zero, then a
34560 6e 79 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6f ny sub-journal o
34570 70 65 6e 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 pened.** within
34580 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e this transaction
34590 20 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 will be opened
345a0 61 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 as an in-memory
345b0 66 69 6c 65 2e 20 54 68 69 73 0a 2a 2a 20 68 61 file. This.** ha
345c0 73 20 6e 6f 20 65 66 66 65 63 74 20 69 66 20 74 s no effect if t
345d0 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 he sub-journal i
345e0 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 s already opened
345f0 20 28 61 73 20 69 74 20 6d 61 79 20 62 65 20 77 (as it may be w
34600 68 65 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 69 hen.** running i
34610 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n exclusive mode
34620 29 20 6f 72 20 69 66 20 74 68 65 20 74 72 61 6e ) or if the tran
34630 73 61 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 saction does not
34640 20 72 65 71 75 69 72 65 20 61 0a 2a 2a 20 73 75 require a.** su
34650 62 2d 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 b-journal. If th
34660 65 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 e subjInMemory a
34670 72 67 75 6d 65 6e 74 20 69 73 20 7a 65 72 6f 2c rgument is zero,
34680 20 74 68 65 6e 20 61 6e 79 20 72 65 71 75 69 72 then any requir
34690 65 64 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 ed.** sub-journa
346a0 6c 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 l is implemented
346b0 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 66 20 70 50 in-memory if pP
346c0 61 67 65 72 20 69 73 20 61 6e 20 69 6e 2d 6d 65 ager is an in-me
346d0 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 0a mory database, .
346e0 2a 2a 20 6f 72 20 75 73 69 6e 67 20 61 20 74 65 ** or using a te
346f0 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6f 74 68 mporary file oth
34700 65 72 77 69 73 65 2e 0a 2a 2f 0a 69 6e 74 20 73 erwise..*/.int s
34710 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e qlite3PagerBegin
34720 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
34730 69 6e 74 20 65 78 46 6c 61 67 2c 20 69 6e 74 20 int exFlag, int
34740 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 7b 0a 20 subjInMemory){.
34750 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
34760 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 _OK;.. if( pPag
34770 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 20 72 65 er->errCode ) re
34780 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 turn pPager->err
34790 43 6f 64 65 3b 0a 20 20 61 73 73 65 72 74 28 20 Code;. assert(
347a0 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d pPager->eState>=
347b0 50 41 47 45 52 5f 52 45 41 44 45 52 20 26 26 20 PAGER_READER &&
347c0 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3c 50 pPager->eState<P
347d0 41 47 45 52 5f 45 52 52 4f 52 20 29 3b 0a 20 20 AGER_ERROR );.
347e0 70 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 pPager->subjInMe
347f0 6d 6f 72 79 20 3d 20 28 75 38 29 73 75 62 6a 49 mory = (u8)subjI
34800 6e 4d 65 6d 6f 72 79 3b 0a 0a 20 20 70 61 67 65 nMemory;.. page
34810 72 55 6e 6d 61 70 28 70 50 61 67 65 72 29 3b 0a rUnmap(pPager);.
34820 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 50 . if( ALWAYS(pP
34830 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 ager->eState==PA
34840 47 45 52 5f 52 45 41 44 45 52 29 20 29 7b 0a 20 GER_READER) ){.
34850 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
34860 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 r->pInJournal==0
34870 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 61 67 );.. if( pag
34880 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 erUseWal(pPager)
34890 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 ){. /* If
348a0 74 68 65 20 70 61 67 65 72 20 69 73 20 63 6f 6e the pager is con
348b0 66 69 67 75 72 65 64 20 74 6f 20 75 73 65 20 6c figured to use l
348c0 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 65 78 63 6c ocking_mode=excl
348d0 75 73 69 76 65 2c 20 61 6e 64 20 61 6e 0a 20 20 usive, and an.
348e0 20 20 20 20 2a 2a 20 65 78 63 6c 75 73 69 76 65 ** exclusive
348f0 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
34900 61 62 61 73 65 20 69 73 20 6e 6f 74 20 61 6c 72 abase is not alr
34910 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 69 eady held, obtai
34920 6e 20 69 74 20 6e 6f 77 2e 0a 20 20 20 20 20 20 n it now..
34930 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 */. if( pPa
34940 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f ger->exclusiveMo
34950 64 65 20 26 26 20 73 71 6c 69 74 65 33 57 61 6c de && sqlite3Wal
34960 45 78 63 6c 75 73 69 76 65 4d 6f 64 65 28 70 50 ExclusiveMode(pP
34970 61 67 65 72 2d 3e 70 57 61 6c 2c 20 2d 31 29 20 ager->pWal, -1)
34980 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
34990 70 61 67 65 72 4c 6f 63 6b 44 62 28 70 50 61 67 pagerLockDb(pPag
349a0 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f er, EXCLUSIVE_LO
349b0 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 CK);. if(
349c0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
349d0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 {. retu
349e0 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d rn rc;. }
349f0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
34a00 57 61 6c 45 78 63 6c 75 73 69 76 65 4d 6f 64 65 WalExclusiveMode
34a10 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 31 (pPager->pWal, 1
34a20 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 );. }..
34a30 20 20 2f 2a 20 47 72 61 62 20 74 68 65 20 77 72 /* Grab the wr
34a40 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 ite lock on the
34a50 6c 6f 67 20 66 69 6c 65 2e 20 49 66 20 73 75 63 log file. If suc
34a60 63 65 73 73 66 75 6c 2c 20 75 70 67 72 61 64 65 cessful, upgrade
34a70 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 50 41 47 to. ** PAG
34a80 45 52 5f 52 45 53 45 52 56 45 44 20 73 74 61 74 ER_RESERVED stat
34a90 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 e. Otherwise, re
34aa0 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f turn an error co
34ab0 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 de to the caller
34ac0 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 62 .. ** The b
34ad0 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 73 20 6e usy-handler is n
34ae0 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 6e ot invoked if an
34af0 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e other connection
34b00 20 61 6c 72 65 61 64 79 0a 20 20 20 20 20 20 2a already. *
34b10 2a 20 68 6f 6c 64 73 20 74 68 65 20 77 72 69 74 * holds the writ
34b20 65 2d 6c 6f 63 6b 2e 20 49 66 20 70 6f 73 73 69 e-lock. If possi
34b30 62 6c 65 2c 20 74 68 65 20 75 70 70 65 72 20 6c ble, the upper l
34b40 61 79 65 72 20 77 69 6c 6c 20 63 61 6c 6c 20 69 ayer will call i
34b50 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 t.. */.
34b60 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 57 61 rc = sqlite3Wa
34b70 6c 42 65 67 69 6e 57 72 69 74 65 54 72 61 6e 73 lBeginWriteTrans
34b80 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2d 3e 70 action(pPager->p
34b90 57 61 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b Wal);. }else{
34ba0 0a 20 20 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e . /* Obtain
34bb0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
34bc0 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
34bd0 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 65 78 file. If the ex
34be0 46 6c 61 67 20 70 61 72 61 6d 65 74 65 72 0a 20 Flag parameter.
34bf0 20 20 20 20 20 2a 2a 20 69 73 20 74 72 75 65 2c ** is true,
34c00 20 74 68 65 6e 20 69 6d 6d 65 64 69 61 74 65 6c then immediatel
34c10 79 20 75 70 67 72 61 64 65 20 74 68 69 73 20 74 y upgrade this t
34c20 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c o an EXCLUSIVE l
34c30 6f 63 6b 2e 20 54 68 65 0a 20 20 20 20 20 20 2a ock. The. *
34c40 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 * busy-handler c
34c50 61 6c 6c 62 61 63 6b 20 63 61 6e 20 62 65 20 75 allback can be u
34c60 73 65 64 20 77 68 65 6e 20 75 70 67 72 61 64 69 sed when upgradi
34c70 6e 67 20 74 6f 20 74 68 65 20 45 58 43 4c 55 53 ng to the EXCLUS
34c80 49 56 45 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 63 IVE. ** loc
34c90 6b 2c 20 62 75 74 20 6e 6f 74 20 77 68 65 6e 20 k, but not when
34ca0 6f 62 74 61 69 6e 69 6e 67 20 74 68 65 20 52 45 obtaining the RE
34cb0 53 45 52 56 45 44 20 6c 6f 63 6b 2e 0a 20 20 20 SERVED lock..
34cc0 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d */. rc =
34cd0 20 70 61 67 65 72 4c 6f 63 6b 44 62 28 70 50 61 pagerLockDb(pPa
34ce0 67 65 72 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f ger, RESERVED_LO
34cf0 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 CK);. if( r
34d00 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
34d10 65 78 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 exFlag ){.
34d20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 rc = pager_wai
34d30 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 t_on_lock(pPager
34d40 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b , EXCLUSIVE_LOCK
34d50 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
34d60 0a 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 .. if( rc==SQ
34d70 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
34d80 20 2f 2a 20 43 68 61 6e 67 65 20 74 6f 20 57 52 /* Change to WR
34d90 49 54 45 52 5f 4c 4f 43 4b 45 44 20 73 74 61 74 ITER_LOCKED stat
34da0 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 e.. **.
34db0 20 20 2a 2a 20 57 41 4c 20 6d 6f 64 65 20 73 65 ** WAL mode se
34dc0 74 73 20 50 61 67 65 72 2e 65 53 74 61 74 65 20 ts Pager.eState
34dd0 74 6f 20 50 41 47 45 52 5f 57 52 49 54 45 52 5f to PAGER_WRITER_
34de0 4c 4f 43 4b 45 44 20 6f 72 20 43 41 43 48 45 4d LOCKED or CACHEM
34df0 4f 44 0a 20 20 20 20 20 20 2a 2a 20 77 68 65 6e OD. ** when
34e00 20 69 74 20 68 61 73 20 61 6e 20 6f 70 65 6e 20 it has an open
34e10 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 62 75 74 transaction, but
34e20 20 6e 65 76 65 72 20 74 6f 20 44 42 4d 4f 44 20 never to DBMOD
34e30 6f 72 20 46 49 4e 49 53 48 45 44 2e 0a 20 20 20 or FINISHED..
34e40 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 62 65 ** This is be
34e50 63 61 75 73 65 20 69 6e 20 74 68 6f 73 65 20 73 cause in those s
34e60 74 61 74 65 73 20 74 68 65 20 63 6f 64 65 20 74 tates the code t
34e70 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 73 61 76 65 o roll back save
34e80 70 6f 69 6e 74 20 0a 20 20 20 20 20 20 2a 2a 20 point . **
34e90 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 6d 61 79 transactions may
34ea0 20 63 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 copy data from
34eb0 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 the sub-journal
34ec0 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 into the databas
34ed0 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 e . ** file
34ee0 20 61 73 20 77 65 6c 6c 20 61 73 20 69 6e 74 6f as well as into
34ef0 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2e the page cache.
34f00 20 57 68 69 63 68 20 77 6f 75 6c 64 20 62 65 20 Which would be
34f10 69 6e 63 6f 72 72 65 63 74 20 69 6e 20 0a 20 20 incorrect in .
34f20 20 20 20 20 2a 2a 20 57 41 4c 20 6d 6f 64 65 2e ** WAL mode.
34f30 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
34f40 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 20 3d pPager->eState =
34f50 20 50 41 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f PAGER_WRITER_LO
34f60 43 4b 45 44 3b 0a 20 20 20 20 20 20 70 50 61 67 CKED;. pPag
34f70 65 72 2d 3e 64 62 48 69 6e 74 53 69 7a 65 20 3d er->dbHintSize =
34f80 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b pPager->dbSize;
34f90 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 . pPager->d
34fa0 62 46 69 6c 65 53 69 7a 65 20 3d 20 70 50 61 67 bFileSize = pPag
34fb0 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 20 er->dbSize;.
34fc0 20 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 pPager->dbOrig
34fd0 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 Size = pPager->d
34fe0 62 53 69 7a 65 3b 0a 20 20 20 20 20 20 70 50 61 bSize;. pPa
34ff0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
35000 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 = 0;. }..
35010 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
35020 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d TE_OK || pPager-
35030 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 >eState==PAGER_R
35040 45 41 44 45 52 20 29 3b 0a 20 20 20 20 61 73 73 EADER );. ass
35050 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ert( rc!=SQLITE_
35060 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53 OK || pPager->eS
35070 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 tate==PAGER_WRIT
35080 45 52 5f 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 20 ER_LOCKED );.
35090 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74 5f assert( assert_
350a0 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 pager_state(pPag
350b0 65 72 29 20 29 3b 0a 20 20 7d 0a 0a 20 20 50 41 er) );. }.. PA
350c0 47 45 52 54 52 41 43 45 28 28 22 54 52 41 4e 53 GERTRACE(("TRANS
350d0 41 43 54 49 4f 4e 20 25 64 5c 6e 22 2c 20 50 41 ACTION %d\n", PA
350e0 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b GERID(pPager)));
350f0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
35100 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 73 69 ./*.** Mark a si
35110 6e 67 6c 65 20 64 61 74 61 20 70 61 67 65 20 61 ngle data page a
35120 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68 65 s writeable. The
35130 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65 6e page is written
35140 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 6d 61 into the .** ma
35150 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 73 75 in journal or su
35160 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 20 72 65 71 b-journal as req
35170 75 69 72 65 64 2e 20 49 66 20 74 68 65 20 70 61 uired. If the pa
35180 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e ge is written in
35190 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65 to.** one of the
351a0 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 20 63 journals, the c
351b0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 orresponding bit
351c0 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 0a is set in the .
351d0 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 ** Pager.pInJour
351e0 6e 61 6c 20 62 69 74 76 65 63 20 61 6e 64 20 74 nal bitvec and t
351f0 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e he PagerSavepoin
35200 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 62 t.pInSavepoint b
35210 69 74 76 65 63 73 0a 2a 2a 20 6f 66 20 61 6e 79 itvecs.** of any
35220 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 open savepoints
35230 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e as appropriate.
35240 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
35250 61 67 65 72 5f 77 72 69 74 65 28 50 67 48 64 72 ager_write(PgHdr
35260 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69 64 20 2a *pPg){. void *
35270 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 pData = pPg->pDa
35280 74 61 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 ta;. Pager *pPa
35290 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 ger = pPg->pPage
352a0 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 r;. int rc = SQ
352b0 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 54 LITE_OK;.. /* T
352c0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e his routine is n
352d0 6f 74 20 63 61 6c 6c 65 64 20 75 6e 6c 65 73 73 ot called unless
352e0 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 a write-transac
352f0 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79 tion has already
35300 20 0a 20 20 2a 2a 20 62 65 65 6e 20 73 74 61 72 . ** been star
35310 74 65 64 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c ted. The journal
35320 20 66 69 6c 65 20 6d 61 79 20 6f 72 20 6d 61 79 file may or may
35330 20 6e 6f 74 20 62 65 20 6f 70 65 6e 20 61 74 20 not be open at
35340 74 68 69 73 20 70 6f 69 6e 74 2e 0a 20 20 2a 2a this point.. **
35350 20 49 74 20 69 73 20 6e 65 76 65 72 20 63 61 6c It is never cal
35360 6c 65 64 20 69 6e 20 74 68 65 20 45 52 52 4f 52 led in the ERROR
35370 20 73 74 61 74 65 2e 0a 20 20 2a 2f 0a 20 20 61 state.. */. a
35380 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 ssert( pPager->e
35390 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 State==PAGER_WRI
353a0 54 45 52 5f 4c 4f 43 4b 45 44 0a 20 20 20 20 20 TER_LOCKED.
353b0 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53 74 || pPager->eSt
353c0 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 ate==PAGER_WRITE
353d0 52 5f 43 41 43 48 45 4d 4f 44 0a 20 20 20 20 20 R_CACHEMOD.
353e0 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53 74 || pPager->eSt
353f0 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 ate==PAGER_WRITE
35400 52 5f 44 42 4d 4f 44 0a 20 20 29 3b 0a 20 20 61 R_DBMOD. );. a
35410 73 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 ssert( assert_pa
35420 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 ger_state(pPager
35430 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e ) );.. /* If an
35440 20 65 72 72 6f 72 20 68 61 73 20 62 65 65 6e 20 error has been
35450 70 72 65 76 69 6f 75 73 6c 79 20 64 65 74 65 63 previously detec
35460 74 65 64 2c 20 72 65 70 6f 72 74 20 74 68 65 20 ted, report the
35470 73 61 6d 65 20 65 72 72 6f 72 0a 20 20 2a 2a 20 same error. **
35480 61 67 61 69 6e 2e 20 54 68 69 73 20 73 68 6f 75 again. This shou
35490 6c 64 20 6e 6f 74 20 68 61 70 70 65 6e 2c 20 62 ld not happen, b
354a0 75 74 20 74 68 65 20 63 68 65 63 6b 20 70 72 6f ut the check pro
354b0 76 69 64 65 73 20 72 6f 62 75 73 74 6e 65 73 73 vides robustness
354c0 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 . */. if( NEVER
354d0 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 (pPager->errCode
354e0 29 20 29 20 20 72 65 74 75 72 6e 20 70 50 61 67 ) ) return pPag
354f0 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 er->errCode;..
35500 2f 2a 20 48 69 67 68 65 72 2d 6c 65 76 65 6c 20 /* Higher-level
35510 72 6f 75 74 69 6e 65 73 20 6e 65 76 65 72 20 63 routines never c
35520 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f all this functio
35530 6e 20 69 66 20 64 61 74 61 62 61 73 65 20 69 73 n if database is
35540 20 6e 6f 74 0a 20 20 2a 2a 20 77 72 69 74 61 62 not. ** writab
35550 6c 65 2e 20 20 42 75 74 20 63 68 65 63 6b 20 61 le. But check a
35560 6e 79 77 61 79 2c 20 6a 75 73 74 20 66 6f 72 20 nyway, just for
35570 72 6f 62 75 73 74 6e 65 73 73 2e 20 2a 2f 0a 20 robustness. */.
35580 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 if( NEVER(pPage
35590 72 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29 20 72 r->readOnly) ) r
355a0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 eturn SQLITE_PER
355b0 4d 3b 0a 0a 20 20 43 48 45 43 4b 5f 50 41 47 45 M;.. CHECK_PAGE
355c0 28 70 50 67 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 (pPg);.. /* The
355d0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 65 journal file ne
355e0 65 64 73 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 eds to be opened
355f0 2e 20 48 69 67 68 65 72 20 6c 65 76 65 6c 20 72 . Higher level r
35600 6f 75 74 69 6e 65 73 20 68 61 76 65 20 61 6c 72 outines have alr
35610 65 61 64 79 0a 20 20 2a 2a 20 6f 62 74 61 69 6e eady. ** obtain
35620 65 64 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 ed the necessary
35630 20 6c 6f 63 6b 73 20 74 6f 20 62 65 67 69 6e 20 locks to begin
35640 74 68 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 the write-transa
35650 63 74 69 6f 6e 2c 20 62 75 74 20 74 68 65 0a 20 ction, but the.
35660 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 ** rollback jou
35670 72 6e 61 6c 20 6d 69 67 68 74 20 6e 6f 74 20 79 rnal might not y
35680 65 74 20 62 65 20 6f 70 65 6e 2e 20 4f 70 65 6e et be open. Open
35690 20 69 74 20 6e 6f 77 20 69 66 20 74 68 69 73 20 it now if this
356a0 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 20 2a is the case.. *
356b0 2a 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 64 *. ** This is d
356c0 6f 6e 65 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 one before calli
356d0 6e 67 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 ng sqlite3Pcache
356e0 4d 61 6b 65 44 69 72 74 79 28 29 20 6f 6e 20 74 MakeDirty() on t
356f0 68 65 20 70 61 67 65 2e 20 0a 20 20 2a 2a 20 4f he page. . ** O
35700 74 68 65 72 77 69 73 65 2c 20 69 66 20 69 74 20 therwise, if it
35710 77 65 72 65 20 64 6f 6e 65 20 61 66 74 65 72 20 were done after
35720 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 50 calling sqlite3P
35730 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 29 cacheMakeDirty()
35740 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 61 6e 20 65 , then. ** an e
35750 72 72 6f 72 20 6d 69 67 68 74 20 6f 63 63 75 72 rror might occur
35760 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 20 77 and the pager w
35770 6f 75 6c 64 20 65 6e 64 20 75 70 20 69 6e 20 57 ould end up in W
35780 52 49 54 45 52 5f 4c 4f 43 4b 45 44 20 73 74 61 RITER_LOCKED sta
35790 74 65 0a 20 20 2a 2a 20 77 69 74 68 20 70 61 67 te. ** with pag
357a0 65 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 es marked as dir
357b0 74 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e ty in the cache.
357c0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 . */. if( pPag
357d0 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 er->eState==PAGE
357e0 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 20 R_WRITER_LOCKED
357f0 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 ){. rc = page
35800 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 r_open_journal(p
35810 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 Pager);. if(
35820 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
35830 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
35840 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
35850 3e 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 57 >eState>=PAGER_W
35860 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 20 29 RITER_CACHEMOD )
35870 3b 0a 20 20 61 73 73 65 72 74 28 20 61 73 73 65 ;. assert( asse
35880 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 rt_pager_state(p
35890 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 Pager) );.. /*
358a0 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 Mark the page as
358b0 20 64 69 72 74 79 2e 20 20 49 66 20 74 68 65 20 dirty. If the
358c0 70 61 67 65 20 68 61 73 20 61 6c 72 65 61 64 79 page has already
358d0 20 62 65 65 6e 20 77 72 69 74 74 65 6e 0a 20 20 been written.
358e0 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 ** to the journa
358f0 6c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 72 65 l then we can re
35900 74 75 72 6e 20 72 69 67 68 74 20 61 77 61 79 2e turn right away.
35910 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 . */. sqlite3P
35920 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 cacheMakeDirty(p
35930 50 67 29 3b 0a 20 20 69 66 28 20 70 61 67 65 49 Pg);. if( pageI
35940 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26 nJournal(pPg) &&
35950 20 21 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 !subjRequiresPa
35960 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20 20 61 ge(pPg) ){. a
35970 73 73 65 72 74 28 20 21 70 61 67 65 72 55 73 65 ssert( !pagerUse
35980 57 61 6c 28 70 50 61 67 65 72 29 20 29 3b 0a 20 Wal(pPager) );.
35990 20 7d 65 6c 73 65 7b 0a 20 20 0a 20 20 20 20 2f }else{. . /
359a0 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f * The transactio
359b0 6e 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 78 n journal now ex
359c0 69 73 74 73 20 61 6e 64 20 77 65 20 68 61 76 65 ists and we have
359d0 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 61 a RESERVED or a
359e0 6e 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 n. ** EXCLUSI
359f0 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d VE lock on the m
35a00 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c ain database fil
35a10 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 63 75 e. Write the cu
35a20 72 72 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 20 rrent page to.
35a30 20 20 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 ** the transac
35a40 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 20 tion journal if
35a50 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20 it is not there
35a60 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a already.. */.
35a70 20 20 20 20 69 66 28 20 21 70 61 67 65 49 6e 4a if( !pageInJ
35a80 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 21 ournal(pPg) && !
35a90 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 pagerUseWal(pPag
35aa0 65 72 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 er) ){. ass
35ab0 65 72 74 28 20 70 61 67 65 72 55 73 65 57 61 6c ert( pagerUseWal
35ac0 28 70 50 61 67 65 72 29 3d 3d 30 20 29 3b 0a 20 (pPager)==0 );.
35ad0 20 20 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67 if( pPg->pg
35ae0 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 no<=pPager->dbOr
35af0 69 67 53 69 7a 65 20 26 26 20 69 73 4f 70 65 6e igSize && isOpen
35b00 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b (pPager->jfd) ){
35b10 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 6b 73 . u32 cks
35b20 75 6d 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 um;. char
35b30 20 2a 70 44 61 74 61 32 3b 0a 20 20 20 20 20 20 *pData2;.
35b40 20 20 69 36 34 20 69 4f 66 66 20 3d 20 70 50 61 i64 iOff = pPa
35b50 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b ger->journalOff;
35b60 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 .. /* We
35b70 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 77 72 69 should never wri
35b80 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 te to the journa
35b90 6c 20 66 69 6c 65 20 74 68 65 20 70 61 67 65 20 l file the page
35ba0 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 that. **
35bb0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 61 74 contains the dat
35bc0 61 62 61 73 65 20 6c 6f 63 6b 73 2e 20 20 54 68 abase locks. Th
35bd0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 e following asse
35be0 72 74 20 76 65 72 69 66 69 65 73 0a 20 20 20 20 rt verifies.
35bf0 20 20 20 20 2a 2a 20 74 68 61 74 20 77 65 20 64 ** that we d
35c00 6f 20 6e 6f 74 2e 20 2a 2f 0a 20 20 20 20 20 20 o not. */.
35c10 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 assert( pPg->p
35c20 67 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 gno!=PAGER_MJ_PG
35c30 4e 4f 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 NO(pPager) );..
35c40 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
35c50 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
35c60 72 3c 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e r<=pPager->journ
35c70 61 6c 4f 66 66 20 29 3b 0a 20 20 20 20 20 20 20 alOff );.
35c80 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 CODEC2(pPager,
35c90 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f pData, pPg->pgno
35ca0 2c 20 37 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 , 7, return SQLI
35cb0 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 32 TE_NOMEM, pData2
35cc0 29 3b 0a 20 20 20 20 20 20 20 20 63 6b 73 75 6d );. cksum
35cd0 20 3d 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70 = pager_cksum(p
35ce0 50 61 67 65 72 2c 20 28 75 38 2a 29 70 44 61 74 Pager, (u8*)pDat
35cf0 61 32 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a a2);.. /*
35d00 20 45 76 65 6e 20 69 66 20 61 6e 20 49 4f 20 6f Even if an IO o
35d10 72 20 64 69 73 6b 66 75 6c 6c 20 65 72 72 6f 72 r diskfull error
35d20 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6a 6f occurs while jo
35d30 75 72 6e 61 6c 6c 69 6e 67 20 74 68 65 0a 20 20 urnalling the.
35d40 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e ** page in
35d50 20 74 68 65 20 62 6c 6f 63 6b 20 61 62 6f 76 65 the block above
35d60 2c 20 73 65 74 20 74 68 65 20 6e 65 65 64 2d 73 , set the need-s
35d70 79 6e 63 20 66 6c 61 67 20 66 6f 72 20 74 68 65 ync flag for the
35d80 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 2a page.. *
35d90 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77 68 65 * Otherwise, whe
35da0 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f n the transactio
35db0 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b n is rolled back
35dc0 2c 20 74 68 65 20 6c 6f 67 69 63 20 69 6e 0a 20 , the logic in.
35dd0 20 20 20 20 20 20 20 2a 2a 20 70 6c 61 79 62 61 ** playba
35de0 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 77 69 ck_one_page() wi
35df0 6c 6c 20 74 68 69 6e 6b 20 74 68 61 74 20 74 68 ll think that th
35e00 65 20 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20 e page needs to
35e10 62 65 20 72 65 73 74 6f 72 65 64 0a 20 20 20 20 be restored.
35e20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 64 61 ** in the da
35e30 74 61 62 61 73 65 20 66 69 6c 65 2e 20 41 6e 64 tabase file. And
35e40 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 if an IO error
35e50 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 6f 69 occurs while doi
35e60 6e 67 20 73 6f 2c 0a 20 20 20 20 20 20 20 20 2a ng so,. *
35e70 2a 20 74 68 65 6e 20 63 6f 72 72 75 70 74 69 6f * then corruptio
35e80 6e 20 6d 61 79 20 66 6f 6c 6c 6f 77 2e 0a 20 20 n may follow..
35e90 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
35ea0 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 pPg->flags |= P
35eb0 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a GHDR_NEED_SYNC;.
35ec0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 . rc = wr
35ed0 69 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 ite32bits(pPager
35ee0 2d 3e 6a 66 64 2c 20 69 4f 66 66 2c 20 70 50 67 ->jfd, iOff, pPg
35ef0 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 ->pgno);.
35f00 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
35f10 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a OK ) return rc;.
35f20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
35f30 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 ite3OsWrite(pPag
35f40 65 72 2d 3e 6a 66 64 2c 20 70 44 61 74 61 32 2c er->jfd, pData2,
35f50 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
35f60 65 2c 20 69 4f 66 66 2b 34 29 3b 0a 20 20 20 20 e, iOff+4);.
35f70 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
35f80 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 TE_OK ) return r
35f90 63 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 c;. rc =
35fa0 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67 write32bits(pPag
35fb0 65 72 2d 3e 6a 66 64 2c 20 69 4f 66 66 2b 70 50 er->jfd, iOff+pP
35fc0 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34 ager->pageSize+4
35fd0 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 20 20 20 20 , cksum);.
35fe0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
35ff0 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b _OK ) return rc;
36000 0a 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 .. IOTRAC
36010 45 28 28 22 4a 4f 55 54 20 25 70 20 25 64 20 25 E(("JOUT %p %d %
36020 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 lld %d\n", pPage
36030 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 r, pPg->pgno, .
36040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
36050 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
36060 66 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 ff, pPager->page
36070 53 69 7a 65 29 29 3b 0a 20 20 20 20 20 20 20 20 Size));.
36080 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 PAGER_INCR(sqlit
36090 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f e3_pager_writej_
360a0 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 count);.
360b0 50 41 47 45 52 54 52 41 43 45 28 28 22 4a 4f 55 PAGERTRACE(("JOU
360c0 52 4e 41 4c 20 25 64 20 70 61 67 65 20 25 64 20 RNAL %d page %d
360d0 6e 65 65 64 53 79 6e 63 3d 25 64 20 68 61 73 68 needSync=%d hash
360e0 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 (%08x)\n",.
360f0 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 PAGERID(
36100 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 pPager), pPg->pg
36110 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 no, .
36120 20 20 28 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 ((pPg->flags&P
36130 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3f GHDR_NEED_SYNC)?
36140 31 3a 30 29 2c 20 70 61 67 65 72 5f 70 61 67 65 1:0), pager_page
36150 68 61 73 68 28 70 50 67 29 29 29 3b 0a 0a 20 20 hash(pPg)));..
36160 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f pPager->jo
36170 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 38 20 2b 20 urnalOff += 8 +
36180 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
36190 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 ;. pPager
361a0 2d 3e 6e 52 65 63 2b 2b 3b 0a 20 20 20 20 20 20 ->nRec++;.
361b0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
361c0 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 ->pInJournal!=0
361d0 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
361e0 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 sqlite3BitvecSet
361f0 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 (pPager->pInJour
36200 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b nal, pPg->pgno);
36210 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
36220 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f e( rc==SQLITE_NO
36230 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 61 MEM );. a
36240 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
36250 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 E_OK || rc==SQLI
36260 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 TE_NOMEM );.
36270 20 20 20 20 72 63 20 7c 3d 20 61 64 64 54 6f 53 rc |= addToS
36280 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 avepointBitvecs(
36290 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e pPager, pPg->pgn
362a0 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 o);. if(
362b0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
362c0 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
362d0 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f t( rc==SQLITE_NO
362e0 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 MEM );.
362f0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
36300 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
36310 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 e{. if( p
36320 50 61 67 65 72 2d 3e 65 53 74 61 74 65 21 3d 50 Pager->eState!=P
36330 41 47 45 52 5f 57 52 49 54 45 52 5f 44 42 4d 4f AGER_WRITER_DBMO
36340 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 D ){. p
36350 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 Pg->flags |= PGH
36360 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 DR_NEED_SYNC;.
36370 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
36380 50 41 47 45 52 54 52 41 43 45 28 28 22 41 50 50 PAGERTRACE(("APP
36390 45 4e 44 20 25 64 20 70 61 67 65 20 25 64 20 6e END %d page %d n
363a0 65 65 64 53 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20 eedSync=%d\n",.
363b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 P
363c0 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 AGERID(pPager),
363d0 70 50 67 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 pPg->pgno,.
363e0 20 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d ((pPg-
363f0 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
36400 44 5f 53 59 4e 43 29 3f 31 3a 30 29 29 29 3b 0a D_SYNC)?1:0)));.
36410 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
36420 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 . /* If the s
36430 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c tatement journal
36440 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 is open and the
36450 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 page is not in
36460 69 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 it,. ** then
36470 77 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e write the curren
36480 74 20 70 61 67 65 20 74 6f 20 74 68 65 20 73 74 t page to the st
36490 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e atement journal.
364a0 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 20 20 Note that.
364b0 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 ** the statement
364c0 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 journal format
364d0 64 69 66 66 65 72 73 20 66 72 6f 6d 20 74 68 65 differs from the
364e0 20 73 74 61 6e 64 61 72 64 20 6a 6f 75 72 6e 61 standard journa
364f0 6c 20 66 6f 72 6d 61 74 0a 20 20 20 20 2a 2a 20 l format. **
36500 69 6e 20 74 68 61 74 20 69 74 20 6f 6d 69 74 73 in that it omits
36510 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 20 61 the checksums a
36520 6e 64 20 74 68 65 20 68 65 61 64 65 72 2e 0a 20 nd the header..
36530 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 75 */. if( su
36540 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70 bjRequiresPage(p
36550 50 67 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 Pg) ){. rc
36560 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 = subjournalPage
36570 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d (pPg);. }. }
36580 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 .. /* Update th
36590 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 e database size
365a0 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f and return.. */
365b0 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 . if( pPager->d
365c0 62 53 69 7a 65 3c 70 50 67 2d 3e 70 67 6e 6f 20 bSize<pPg->pgno
365d0 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 ){. pPager->d
365e0 62 53 69 7a 65 20 3d 20 70 50 67 2d 3e 70 67 6e bSize = pPg->pgn
365f0 6f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 o;. }. return
36600 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 rc;.}../*.** Mar
36610 6b 20 61 20 64 61 74 61 20 70 61 67 65 20 61 73 k a data page as
36620 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68 69 73 writeable. This
36630 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 routine must be
36640 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 0a called before .
36650 2a 2a 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65 ** making change
36660 73 20 74 6f 20 61 20 70 61 67 65 2e 20 54 68 65 s to a page. The
36670 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 68 65 caller must che
36680 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 ck the return va
36690 6c 75 65 20 0a 2a 2a 20 6f 66 20 74 68 69 73 20 lue .** of this
366a0 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 62 65 20 function and be
366b0 63 61 72 65 66 75 6c 20 6e 6f 74 20 74 6f 20 63 careful not to c
366c0 68 61 6e 67 65 20 61 6e 79 20 70 61 67 65 20 64 hange any page d
366d0 61 74 61 20 75 6e 6c 65 73 73 20 0a 2a 2a 20 74 ata unless .** t
366e0 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
366f0 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a rns SQLITE_OK..*
36700 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 *.** The differe
36710 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 nce between this
36720 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 70 61 function and pa
36730 67 65 72 5f 77 72 69 74 65 28 29 20 69 73 20 74 ger_write() is t
36740 68 61 74 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 hat this.** func
36750 74 69 6f 6e 20 61 6c 73 6f 20 64 65 61 6c 73 20 tion also deals
36760 77 69 74 68 20 74 68 65 20 73 70 65 63 69 61 6c with the special
36770 20 63 61 73 65 20 77 68 65 72 65 20 32 20 6f 72 case where 2 or
36780 20 6d 6f 72 65 20 70 61 67 65 73 0a 2a 2a 20 66 more pages.** f
36790 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 it on a single d
367a0 69 73 6b 20 73 65 63 74 6f 72 2e 20 49 6e 20 74 isk sector. In t
367b0 68 69 73 20 63 61 73 65 20 61 6c 6c 20 63 6f 2d his case all co-
367c0 72 65 73 69 64 65 6e 74 20 70 61 67 65 73 0a 2a resident pages.*
367d0 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e * must have been
367e0 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
367f0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 65 66 journal file bef
36800 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
36810 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 *.** If an error
36820 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54 45 5f occurs, SQLITE_
36830 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f 20 65 NOMEM or an IO e
36840 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
36850 75 72 6e 65 64 0a 2a 2a 20 61 73 20 61 70 70 72 urned.** as appr
36860 6f 70 72 69 61 74 65 2e 20 4f 74 68 65 72 77 69 opriate. Otherwi
36870 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a se, SQLITE_OK..*
36880 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 /.int sqlite3Pag
36890 65 72 57 72 69 74 65 28 44 62 50 61 67 65 20 2a erWrite(DbPage *
368a0 70 44 62 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 pDbPage){. int
368b0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
368c0 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 . PgHdr *pPg =
368d0 70 44 62 50 61 67 65 3b 0a 20 20 50 61 67 65 72 pDbPage;. Pager
368e0 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e *pPager = pPg->
368f0 70 50 61 67 65 72 3b 0a 20 20 50 67 6e 6f 20 6e pPager;. Pgno n
36900 50 61 67 65 50 65 72 53 65 63 74 6f 72 20 3d 20 PagePerSector =
36910 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 (pPager->sectorS
36920 69 7a 65 2f 70 50 61 67 65 72 2d 3e 70 61 67 65 ize/pPager->page
36930 53 69 7a 65 29 3b 0a 0a 20 20 61 73 73 65 72 74 Size);.. assert
36940 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 ( pPager->eState
36950 3e 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 4c >=PAGER_WRITER_L
36960 4f 43 4b 45 44 20 29 3b 0a 20 20 61 73 73 65 72 OCKED );. asser
36970 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 t( pPager->eStat
36980 65 21 3d 50 41 47 45 52 5f 45 52 52 4f 52 20 29 e!=PAGER_ERROR )
36990 3b 0a 20 20 61 73 73 65 72 74 28 20 61 73 73 65 ;. assert( asse
369a0 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 rt_pager_state(p
369b0 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 Pager) );.. /*
369c0 54 68 65 72 65 20 6d 75 73 74 20 6e 6f 74 20 62 There must not b
369d0 65 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e e any outstandin
369e0 67 20 6d 6d 61 70 20 70 61 67 65 73 20 61 74 20 g mmap pages at
369f0 74 68 69 73 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 this point */.
36a00 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
36a10 6e 4d 6d 61 70 4f 75 74 3d 3d 30 20 29 3b 0a 0a nMmapOut==0 );..
36a20 20 20 69 66 28 20 6e 50 61 67 65 50 65 72 53 65 if( nPagePerSe
36a30 63 74 6f 72 3e 31 20 29 7b 0a 20 20 20 20 50 67 ctor>1 ){. Pg
36a40 6e 6f 20 6e 50 61 67 65 43 6f 75 6e 74 3b 20 20 no nPageCount;
36a50 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
36a60 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
36a70 20 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c in database fil
36a80 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 70 67 e */. Pgno pg
36a90 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
36aa0 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65 /* First page
36ab0 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 of the sector p
36ac0 50 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e Pg is located on
36ad0 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61 . */. int nPa
36ae0 67 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ge = 0;
36af0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
36b00 70 61 67 65 73 20 73 74 61 72 74 69 6e 67 20 61 pages starting a
36b10 74 20 70 67 31 20 74 6f 20 6a 6f 75 72 6e 61 6c t pg1 to journal
36b20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 */. int ii;
36b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
36b40 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
36b50 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65 65 r */. int nee
36b60 64 53 79 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 dSync = 0;
36b70 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e /* True if an
36b80 79 20 70 61 67 65 20 68 61 73 20 50 47 48 44 52 y page has PGHDR
36b90 5f 4e 45 45 44 5f 53 59 4e 43 20 2a 2f 0a 0a 20 _NEED_SYNC */..
36ba0 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 64 6f /* Set the do
36bb0 4e 6f 74 53 79 6e 63 53 70 69 6c 6c 20 66 6c 61 NotSyncSpill fla
36bc0 67 20 74 6f 20 31 2e 20 54 68 69 73 20 69 73 20 g to 1. This is
36bd0 62 65 63 61 75 73 65 20 77 65 20 63 61 6e 6e 6f because we canno
36be0 74 20 61 6c 6c 6f 77 0a 20 20 20 20 2a 2a 20 61 t allow. ** a
36bf0 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
36c00 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 62 65 to be written be
36c10 74 77 65 65 6e 20 74 68 65 20 70 61 67 65 73 20 tween the pages
36c20 6a 6f 75 72 6e 61 6c 65 64 20 62 79 0a 20 20 20 journaled by.
36c30 20 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f ** this functio
36c40 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 n.. */. as
36c50 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a sert( !MEMDB );.
36c60 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
36c70 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 53 70 69 er->doNotSyncSpi
36c80 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 ll==0 );. pPa
36c90 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 53 70 ger->doNotSyncSp
36ca0 69 6c 6c 2b 2b 3b 0a 0a 20 20 20 20 2f 2a 20 54 ill++;.. /* T
36cb0 68 69 73 20 74 72 69 63 6b 20 61 73 73 75 6d 65 his trick assume
36cc0 73 20 74 68 61 74 20 62 6f 74 68 20 74 68 65 20 s that both the
36cd0 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65 page-size and se
36ce0 63 74 6f 72 2d 73 69 7a 65 20 61 72 65 0a 20 20 ctor-size are.
36cf0 20 20 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 ** an integer
36d00 70 6f 77 65 72 20 6f 66 20 32 2e 20 49 74 20 73 power of 2. It s
36d10 65 74 73 20 76 61 72 69 61 62 6c 65 20 70 67 31 ets variable pg1
36d20 20 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69 to the identifi
36d30 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 er. ** of the
36d40 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 first page of t
36d50 68 65 20 73 65 63 74 6f 72 20 70 50 67 20 69 73 he sector pPg is
36d60 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 0a 20 20 20 located on..
36d70 20 2a 2f 0a 20 20 20 20 70 67 31 20 3d 20 28 28 */. pg1 = ((
36d80 70 50 67 2d 3e 70 67 6e 6f 2d 31 29 20 26 20 7e pPg->pgno-1) & ~
36d90 28 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 2d (nPagePerSector-
36da0 31 29 29 20 2b 20 31 3b 0a 0a 20 20 20 20 6e 50 1)) + 1;.. nP
36db0 61 67 65 43 6f 75 6e 74 20 3d 20 70 50 61 67 65 ageCount = pPage
36dc0 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 20 69 r->dbSize;. i
36dd0 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3e 6e 50 61 f( pPg->pgno>nPa
36de0 67 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 geCount ){.
36df0 20 6e 50 61 67 65 20 3d 20 28 70 50 67 2d 3e 70 nPage = (pPg->p
36e00 67 6e 6f 20 2d 20 70 67 31 29 2b 31 3b 0a 20 20 gno - pg1)+1;.
36e10 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 67 31 }else if( (pg1
36e20 2b 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 2d +nPagePerSector-
36e30 31 29 3e 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b 1)>nPageCount ){
36e40 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 6e . nPage = n
36e50 50 61 67 65 43 6f 75 6e 74 2b 31 2d 70 67 31 3b PageCount+1-pg1;
36e60 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
36e70 20 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 50 nPage = nPageP
36e80 65 72 53 65 63 74 6f 72 3b 0a 20 20 20 20 7d 0a erSector;. }.
36e90 20 20 20 20 61 73 73 65 72 74 28 6e 50 61 67 65 assert(nPage
36ea0 3e 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 >0);. assert(
36eb0 70 67 31 3c 3d 70 50 67 2d 3e 70 67 6e 6f 29 3b pg1<=pPg->pgno);
36ec0 0a 20 20 20 20 61 73 73 65 72 74 28 28 70 67 31 . assert((pg1
36ed0 2b 6e 50 61 67 65 29 3e 70 50 67 2d 3e 70 67 6e +nPage)>pPg->pgn
36ee0 6f 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 69 3d o);.. for(ii=
36ef0 30 3b 20 69 69 3c 6e 50 61 67 65 20 26 26 20 72 0; ii<nPage && r
36f00 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 69 c==SQLITE_OK; ii
36f10 2b 2b 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 ++){. Pgno
36f20 70 67 20 3d 20 70 67 31 2b 69 69 3b 0a 20 20 20 pg = pg1+ii;.
36f30 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 3b PgHdr *pPage;
36f40 0a 20 20 20 20 20 20 69 66 28 20 70 67 3d 3d 70 . if( pg==p
36f50 50 67 2d 3e 70 67 6e 6f 20 7c 7c 20 21 73 71 6c Pg->pgno || !sql
36f60 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 ite3BitvecTest(p
36f70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
36f80 6c 2c 20 70 67 29 20 29 7b 0a 20 20 20 20 20 20 l, pg) ){.
36f90 20 20 69 66 28 20 70 67 21 3d 50 41 47 45 52 5f if( pg!=PAGER_
36fa0 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 MJ_PGNO(pPager)
36fb0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
36fc0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
36fd0 74 28 70 50 61 67 65 72 2c 20 70 67 2c 20 26 70 t(pPager, pg, &p
36fe0 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Page);.
36ff0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
37000 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
37010 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 rc = pager_wri
37020 74 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 te(pPage);.
37030 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 if( pPage
37040 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 ->flags&PGHDR_NE
37050 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 20 ED_SYNC ){.
37060 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e needSyn
37070 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 c = 1;.
37080 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 }.
37090 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
370a0 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 ef(pPage);.
370b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
370c0 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
370d0 20 28 70 50 61 67 65 20 3d 20 70 61 67 65 72 5f (pPage = pager_
370e0 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 lookup(pPager, p
370f0 67 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 g))!=0 ){.
37100 20 20 69 66 28 20 70 50 61 67 65 2d 3e 66 6c 61 if( pPage->fla
37110 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
37120 4e 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 NC ){.
37130 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 needSync = 1;.
37140 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
37150 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
37160 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 f(pPage);.
37170 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 }. }.. /*
37180 49 66 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 If the PGHDR_NEE
37190 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73 20 73 D_SYNC flag is s
371a0 65 74 20 66 6f 72 20 61 6e 79 20 6f 66 20 74 68 et for any of th
371b0 65 20 6e 50 61 67 65 20 70 61 67 65 73 20 0a 20 e nPage pages .
371c0 20 20 20 2a 2a 20 73 74 61 72 74 69 6e 67 20 61 ** starting a
371d0 74 20 70 67 31 2c 20 74 68 65 6e 20 69 74 20 6e t pg1, then it n
371e0 65 65 64 73 20 74 6f 20 62 65 20 73 65 74 20 66 eeds to be set f
371f0 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 6d 2e 20 or all of them.
37200 42 65 63 61 75 73 65 0a 20 20 20 20 2a 2a 20 77 Because. ** w
37210 72 69 74 69 6e 67 20 74 6f 20 61 6e 79 20 6f 66 riting to any of
37220 20 74 68 65 73 65 20 6e 50 61 67 65 20 70 61 67 these nPage pag
37230 65 73 20 6d 61 79 20 64 61 6d 61 67 65 20 74 68 es may damage th
37240 65 20 6f 74 68 65 72 73 2c 20 74 68 65 0a 20 20 e others, the.
37250 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ** journal fil
37260 65 20 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 73 e must contain s
37270 79 6e 63 28 29 65 64 20 63 6f 70 69 65 73 20 6f ync()ed copies o
37280 66 20 61 6c 6c 20 6f 66 20 74 68 65 6d 0a 20 20 f all of them.
37290 20 20 2a 2a 20 62 65 66 6f 72 65 20 61 6e 79 20 ** before any
372a0 6f 66 20 74 68 65 6d 20 63 61 6e 20 62 65 20 77 of them can be w
372b0 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 20 74 68 ritten out to th
372c0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
372d0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
372e0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
372f0 20 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 needSync ){.
37300 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 assert( !MEMD
37310 42 20 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 B );. for(i
37320 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65 3b 20 69 i=0; ii<nPage; i
37330 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 50 67 i++){. Pg
37340 48 64 72 20 2a 70 50 61 67 65 20 3d 20 70 61 67 Hdr *pPage = pag
37350 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 er_lookup(pPager
37360 2c 20 70 67 31 2b 69 69 29 3b 0a 20 20 20 20 20 , pg1+ii);.
37370 20 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a if( pPage ){.
37380 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 2d pPage-
37390 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f >flags |= PGHDR_
373a0 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 NEED_SYNC;.
373b0 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
373c0 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20 rUnref(pPage);.
373d0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
373e0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 . }.. asse
373f0 72 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f rt( pPager->doNo
37400 74 53 79 6e 63 53 70 69 6c 6c 3d 3d 31 20 29 3b tSyncSpill==1 );
37410 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e . pPager->doN
37420 6f 74 53 79 6e 63 53 70 69 6c 6c 2d 2d 3b 0a 20 otSyncSpill--;.
37430 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d }else{. rc =
37440 20 70 61 67 65 72 5f 77 72 69 74 65 28 70 44 62 pager_write(pDb
37450 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 Page);. }. ret
37460 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
37470 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 Return TRUE if
37480 74 68 65 20 70 61 67 65 20 67 69 76 65 6e 20 69 the page given i
37490 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 77 n the argument w
374a0 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 70 61 as previously pa
374b0 73 73 65 64 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 ssed.** to sqlit
374c0 65 33 50 61 67 65 72 57 72 69 74 65 28 29 2e 20 e3PagerWrite().
374d0 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
374e0 20 72 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 return TRUE if
374f0 69 74 20 69 73 20 6f 6b 0a 2a 2a 20 74 6f 20 63 it is ok.** to c
37500 68 61 6e 67 65 20 74 68 65 20 63 6f 6e 74 65 6e hange the conten
37510 74 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a t of the page..*
37520 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 /.#ifndef NDEBUG
37530 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 .int sqlite3Page
37540 72 49 73 77 72 69 74 65 61 62 6c 65 28 44 62 50 rIswriteable(DbP
37550 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 age *pPg){. ret
37560 75 72 6e 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 urn pPg->flags&P
37570 47 48 44 52 5f 44 49 52 54 59 3b 0a 7d 0a 23 65 GHDR_DIRTY;.}.#e
37580 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 61 ndif../*.** A ca
37590 6c 6c 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 ll to this routi
375a0 6e 65 20 74 65 6c 6c 73 20 74 68 65 20 70 61 67 ne tells the pag
375b0 65 72 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f er that it is no
375c0 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f 0a 2a t necessary to.*
375d0 2a 20 77 72 69 74 65 20 74 68 65 20 69 6e 66 6f * write the info
375e0 72 6d 61 74 69 6f 6e 20 6f 6e 20 70 61 67 65 20 rmation on page
375f0 70 50 67 20 62 61 63 6b 20 74 6f 20 74 68 65 20 pPg back to the
37600 64 69 73 6b 2c 20 65 76 65 6e 20 74 68 6f 75 67 disk, even thoug
37610 68 0a 2a 2a 20 74 68 61 74 20 70 61 67 65 20 6d h.** that page m
37620 69 67 68 74 20 62 65 20 6d 61 72 6b 65 64 20 61 ight be marked a
37630 73 20 64 69 72 74 79 2e 20 20 54 68 69 73 20 68 s dirty. This h
37640 61 70 70 65 6e 73 2c 20 66 6f 72 20 65 78 61 6d appens, for exam
37650 70 6c 65 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 65 ple, when.** the
37660 20 70 61 67 65 20 68 61 73 20 62 65 65 6e 20 61 page has been a
37670 64 64 65 64 20 61 73 20 61 20 6c 65 61 66 20 6f dded as a leaf o
37680 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 61 f the freelist a
37690 6e 64 20 73 6f 20 69 74 73 0a 2a 2a 20 63 6f 6e nd so its.** con
376a0 74 65 6e 74 20 6e 6f 20 6c 6f 6e 67 65 72 20 6d tent no longer m
376b0 61 74 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 atters..**.** Th
376c0 65 20 6f 76 65 72 6c 79 69 6e 67 20 73 6f 66 74 e overlying soft
376d0 77 61 72 65 20 6c 61 79 65 72 20 63 61 6c 6c 73 ware layer calls
376e0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 this routine wh
376f0 65 6e 20 61 6c 6c 20 6f 66 20 74 68 65 20 64 61 en all of the da
37700 74 61 0a 2a 2a 20 6f 6e 20 74 68 65 20 67 69 76 ta.** on the giv
37710 65 6e 20 70 61 67 65 20 69 73 20 75 6e 75 73 65 en page is unuse
37720 64 2e 20 54 68 65 20 70 61 67 65 72 20 6d 61 72 d. The pager mar
37730 6b 73 20 74 68 65 20 70 61 67 65 20 61 73 20 63 ks the page as c
37740 6c 65 61 6e 20 73 6f 0a 2a 2a 20 74 68 61 74 20 lean so.** that
37750 69 74 20 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 it does not get
37760 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e written to disk.
37770 0a 2a 2a 0a 2a 2a 20 54 65 73 74 73 20 73 68 6f .**.** Tests sho
37780 77 20 74 68 61 74 20 74 68 69 73 20 6f 70 74 69 w that this opti
37790 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 20 71 75 61 mization can qua
377a0 64 72 75 70 6c 65 20 74 68 65 20 73 70 65 65 64 druple the speed
377b0 20 6f 66 20 6c 61 72 67 65 20 0a 2a 2a 20 44 45 of large .** DE
377c0 4c 45 54 45 20 6f 70 65 72 61 74 69 6f 6e 73 2e LETE operations.
377d0 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 .*/.void sqlite3
377e0 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 50 PagerDontWrite(P
377f0 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 gHdr *pPg){. Pa
37800 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 ger *pPager = pP
37810 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 66 28 g->pPager;. if(
37820 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 (pPg->flags&PGH
37830 44 52 5f 44 49 52 54 59 29 20 26 26 20 70 50 61 DR_DIRTY) && pPa
37840 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3d ger->nSavepoint=
37850 3d 30 20 29 7b 0a 20 20 20 20 50 41 47 45 52 54 =0 ){. PAGERT
37860 52 41 43 45 28 28 22 44 4f 4e 54 5f 57 52 49 54 RACE(("DONT_WRIT
37870 45 20 70 61 67 65 20 25 64 20 6f 66 20 25 64 5c E page %d of %d\
37880 6e 22 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 50 n", pPg->pgno, P
37890 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 AGERID(pPager)))
378a0 3b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 ;. IOTRACE(("
378b0 43 4c 45 41 4e 20 25 70 20 25 64 5c 6e 22 2c 20 CLEAN %p %d\n",
378c0 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e pPager, pPg->pgn
378d0 6f 29 29 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 o)). pPg->fla
378e0 67 73 20 7c 3d 20 50 47 48 44 52 5f 44 4f 4e 54 gs |= PGHDR_DONT
378f0 5f 57 52 49 54 45 3b 0a 20 20 20 20 70 61 67 65 _WRITE;. page
37900 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 28 70 r_set_pagehash(p
37910 50 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a Pg);. }.}../*.*
37920 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
37930 73 20 63 61 6c 6c 65 64 20 74 6f 20 69 6e 63 72 s called to incr
37940 65 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 65 20 ement the value
37950 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
37960 66 69 6c 65 20 0a 2a 2a 20 63 68 61 6e 67 65 2d file .** change-
37970 63 6f 75 6e 74 65 72 2c 20 73 74 6f 72 65 64 20 counter, stored
37980 61 73 20 61 20 34 2d 62 79 74 65 20 62 69 67 2d as a 4-byte big-
37990 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 73 endian integer s
379a0 74 61 72 74 69 6e 67 20 61 74 20 0a 2a 2a 20 62 tarting at .** b
379b0 79 74 65 20 6f 66 66 73 65 74 20 32 34 20 6f 66 yte offset 24 of
379c0 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 2e the pager file.
379d0 20 20 54 68 65 20 73 65 63 6f 6e 64 61 72 79 20 The secondary
379e0 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 61 change counter a
379f0 74 0a 2a 2a 20 39 32 20 69 73 20 61 6c 73 6f 20 t.** 92 is also
37a00 75 70 64 61 74 65 64 2c 20 61 73 20 69 73 20 74 updated, as is t
37a10 68 65 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f he SQLite versio
37a20 6e 20 6e 75 6d 62 65 72 20 61 74 20 6f 66 66 73 n number at offs
37a30 65 74 20 39 36 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 et 96..**.** But
37a40 20 74 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 this only happe
37a50 6e 73 20 69 66 20 74 68 65 20 70 50 61 67 65 72 ns if the pPager
37a60 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e ->changeCountDon
37a70 65 20 66 6c 61 67 20 69 73 20 66 61 6c 73 65 2e e flag is false.
37a80 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 65 78 63 .** To avoid exc
37a90 65 73 73 20 63 68 75 72 6e 69 6e 67 20 6f 66 20 ess churning of
37aa0 70 61 67 65 20 31 2c 20 74 68 65 20 75 70 64 61 page 1, the upda
37ab0 74 65 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 te only happens
37ac0 6f 6e 63 65 2e 0a 2a 2a 20 53 65 65 20 61 6c 73 once..** See als
37ad0 6f 20 74 68 65 20 70 61 67 65 72 5f 77 72 69 74 o the pager_writ
37ae0 65 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 e_changecounter(
37af0 29 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 64 ) routine that d
37b00 6f 65 73 20 61 6e 20 0a 2a 2a 20 75 6e 63 6f 6e oes an .** uncon
37b10 64 69 74 69 6f 6e 61 6c 20 75 70 64 61 74 65 20 ditional update
37b20 6f 66 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f of the change co
37b30 75 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 unters..**.** If
37b40 20 74 68 65 20 69 73 44 69 72 65 63 74 4d 6f 64 the isDirectMod
37b50 65 20 66 6c 61 67 20 69 73 20 7a 65 72 6f 2c 20 e flag is zero,
37b60 74 68 65 6e 20 74 68 69 73 20 69 73 20 64 6f 6e then this is don
37b70 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a e by calling .**
37b80 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
37b90 74 65 28 29 20 6f 6e 20 70 61 67 65 20 31 2c 20 te() on page 1,
37ba0 74 68 65 6e 20 6d 6f 64 69 66 79 69 6e 67 20 74 then modifying t
37bb0 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
37bc0 68 65 0a 2a 2a 20 70 61 67 65 20 64 61 74 61 2e he.** page data.
37bd0 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
37be0 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 e file will be u
37bf0 70 64 61 74 65 64 20 77 68 65 6e 20 74 68 65 20 pdated when the
37c00 63 75 72 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 current.** trans
37c10 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 action is commit
37c20 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 ted..**.** The i
37c30 73 44 69 72 65 63 74 4d 6f 64 65 20 66 6c 61 67 sDirectMode flag
37c40 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6e 6f 6e may only be non
37c50 2d 7a 65 72 6f 20 69 66 20 74 68 65 20 6c 69 62 -zero if the lib
37c60 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 rary was compile
37c70 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 53 51 d.** with the SQ
37c80 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d LITE_ENABLE_ATOM
37c90 49 43 5f 57 52 49 54 45 20 6d 61 63 72 6f 20 64 IC_WRITE macro d
37ca0 65 66 69 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 efined. In this
37cb0 63 61 73 65 2c 0a 2a 2a 20 69 66 20 69 73 44 69 case,.** if isDi
37cc0 72 65 63 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f rect is non-zero
37cd0 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 , then the datab
37ce0 61 73 65 20 66 69 6c 65 20 69 73 20 75 70 64 61 ase file is upda
37cf0 74 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 ted directly.**
37d00 62 79 20 77 72 69 74 69 6e 67 20 61 6e 20 75 70 by writing an up
37d10 64 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 dated version of
37d20 20 70 61 67 65 20 31 20 75 73 69 6e 67 20 61 20 page 1 using a
37d30 63 61 6c 6c 20 74 6f 20 74 68 65 20 0a 2a 2a 20 call to the .**
37d40 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 29 sqlite3OsWrite()
37d50 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 function..*/.st
37d60 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 69 atic int pager_i
37d70 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 ncr_changecounte
37d80 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c r(Pager *pPager,
37d90 20 69 6e 74 20 69 73 44 69 72 65 63 74 4d 6f 64 int isDirectMod
37da0 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 e){. int rc = S
37db0 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 QLITE_OK;.. ass
37dc0 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 ert( pPager->eSt
37dd0 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 ate==PAGER_WRITE
37de0 52 5f 43 41 43 48 45 4d 4f 44 0a 20 20 20 20 20 R_CACHEMOD.
37df0 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53 74 || pPager->eSt
37e00 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 ate==PAGER_WRITE
37e10 52 5f 44 42 4d 4f 44 0a 20 20 29 3b 0a 20 20 61 R_DBMOD. );. a
37e20 73 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 ssert( assert_pa
37e30 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 ger_state(pPager
37e40 29 20 29 3b 0a 0a 20 20 2f 2a 20 44 65 63 6c 61 ) );.. /* Decla
37e50 72 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a re and initializ
37e60 65 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 e constant integ
37e70 65 72 20 27 69 73 44 69 72 65 63 74 27 2e 20 49 er 'isDirect'. I
37e80 66 20 74 68 65 0a 20 20 2a 2a 20 61 74 6f 6d 69 f the. ** atomi
37e90 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 c-write optimiza
37ea0 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 20 tion is enabled
37eb0 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2c 20 74 in this build, t
37ec0 68 65 6e 20 69 73 44 69 72 65 63 74 0a 20 20 2a hen isDirect. *
37ed0 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 * is initialized
37ee0 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70 61 to the value pa
37ef0 73 73 65 64 20 61 73 20 74 68 65 20 69 73 44 69 ssed as the isDi
37f00 72 65 63 74 4d 6f 64 65 20 70 61 72 61 6d 65 74 rectMode paramet
37f10 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 69 73 20 er. ** to this
37f20 66 75 6e 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 function. Otherw
37f30 69 73 65 2c 20 69 74 20 69 73 20 61 6c 77 61 79 ise, it is alway
37f40 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 20 s set to zero..
37f50 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 69 64 65 **. ** The ide
37f60 61 20 69 73 20 74 68 61 74 20 69 66 20 74 68 65 a is that if the
37f70 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 atomic-write op
37f80 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 6e 6f timization is no
37f90 74 0a 20 20 2a 2a 20 65 6e 61 62 6c 65 64 20 61 t. ** enabled a
37fa0 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 t compile time,
37fb0 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 61 6e the compiler can
37fc0 20 6f 6d 69 74 20 74 68 65 20 74 65 73 74 73 20 omit the tests
37fd0 6f 66 0a 20 20 2a 2a 20 27 69 73 44 69 72 65 63 of. ** 'isDirec
37fe0 74 27 20 62 65 6c 6f 77 2c 20 61 73 20 77 65 6c t' below, as wel
37ff0 6c 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 20 65 l as the block e
38000 6e 63 6c 6f 73 65 64 20 69 6e 20 74 68 65 0a 20 nclosed in the.
38010 20 2a 2a 20 22 69 66 28 20 69 73 44 69 72 65 63 ** "if( isDirec
38020 74 20 29 22 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a t )" condition..
38030 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c */.#ifndef SQL
38040 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 ITE_ENABLE_ATOMI
38050 43 5f 57 52 49 54 45 0a 23 20 64 65 66 69 6e 65 C_WRITE.# define
38060 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 30 0a 20 DIRECT_MODE 0.
38070 20 61 73 73 65 72 74 28 20 69 73 44 69 72 65 63 assert( isDirec
38080 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a 20 20 55 4e tMode==0 );. UN
38090 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 USED_PARAMETER(i
380a0 73 44 69 72 65 63 74 4d 6f 64 65 29 3b 0a 23 65 sDirectMode);.#e
380b0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 44 49 52 lse.# define DIR
380c0 45 43 54 5f 4d 4f 44 45 20 69 73 44 69 72 65 63 ECT_MODE isDirec
380d0 74 4d 6f 64 65 0a 23 65 6e 64 69 66 0a 0a 20 20 tMode.#endif..
380e0 69 66 28 20 21 70 50 61 67 65 72 2d 3e 63 68 61 if( !pPager->cha
380f0 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 26 26 20 ngeCountDone &&
38100 41 4c 57 41 59 53 28 70 50 61 67 65 72 2d 3e 64 ALWAYS(pPager->d
38110 62 53 69 7a 65 3e 30 29 20 29 7b 0a 20 20 20 20 bSize>0) ){.
38120 50 67 48 64 72 20 2a 70 50 67 48 64 72 3b 20 20 PgHdr *pPgHdr;
38130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
38140 20 52 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 Reference to pa
38150 67 65 20 31 20 2a 2f 0a 0a 20 20 20 20 61 73 73 ge 1 */.. ass
38160 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65 ert( !pPager->te
38170 6d 70 46 69 6c 65 20 26 26 20 69 73 4f 70 65 6e mpFile && isOpen
38180 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a (pPager->fd) );.
38190 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 . /* Open pag
381a0 65 20 31 20 6f 66 20 74 68 65 20 66 69 6c 65 20 e 1 of the file
381b0 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a for writing. */.
381c0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
381d0 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c PagerGet(pPager,
381e0 20 31 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 20 1, &pPgHdr);.
381f0 20 20 61 73 73 65 72 74 28 20 70 50 67 48 64 72 assert( pPgHdr
38200 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 ==0 || rc==SQLIT
38210 45 5f 4f 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 E_OK );.. /*
38220 49 66 20 70 61 67 65 20 6f 6e 65 20 77 61 73 20 If page one was
38230 66 65 74 63 68 65 64 20 73 75 63 63 65 73 73 66 fetched successf
38240 75 6c 6c 79 2c 20 61 6e 64 20 74 68 69 73 20 66 ully, and this f
38250 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 20 unction is not.
38260 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 ** operating
38270 69 6e 20 64 69 72 65 63 74 2d 6d 6f 64 65 2c 20 in direct-mode,
38280 6d 61 6b 65 20 70 61 67 65 20 31 20 77 72 69 74 make page 1 writ
38290 61 62 6c 65 2e 20 20 57 68 65 6e 20 6e 6f 74 20 able. When not
382a0 69 6e 20 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 in . ** direc
382b0 74 20 6d 6f 64 65 2c 20 70 61 67 65 20 31 20 69 t mode, page 1 i
382c0 73 20 61 6c 77 61 79 73 20 68 65 6c 64 20 69 6e s always held in
382d0 20 63 61 63 68 65 20 61 6e 64 20 68 65 6e 63 65 cache and hence
382e0 20 74 68 65 20 50 61 67 65 72 47 65 74 28 29 0a the PagerGet().
382f0 20 20 20 20 2a 2a 20 61 62 6f 76 65 20 69 73 20 ** above is
38300 61 6c 77 61 79 73 20 73 75 63 63 65 73 73 66 75 always successfu
38310 6c 20 2d 20 68 65 6e 63 65 20 74 68 65 20 41 4c l - hence the AL
38320 57 41 59 53 20 6f 6e 20 72 63 3d 3d 53 51 4c 49 WAYS on rc==SQLI
38330 54 45 5f 4f 4b 2e 0a 20 20 20 20 2a 2f 0a 20 20 TE_OK.. */.
38340 20 20 69 66 28 20 21 44 49 52 45 43 54 5f 4d 4f if( !DIRECT_MO
38350 44 45 20 26 26 20 41 4c 57 41 59 53 28 72 63 3d DE && ALWAYS(rc=
38360 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 =SQLITE_OK) ){.
38370 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
38380 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67 48 3PagerWrite(pPgH
38390 64 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 dr);. }..
383a0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
383b0 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 63 K ){. /* Ac
383c0 74 75 61 6c 6c 79 20 64 6f 20 74 68 65 20 75 70 tually do the up
383d0 64 61 74 65 20 6f 66 20 74 68 65 20 63 68 61 6e date of the chan
383e0 67 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 ge counter */.
383f0 20 20 20 20 70 61 67 65 72 5f 77 72 69 74 65 5f pager_write_
38400 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 changecounter(pP
38410 67 48 64 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a gHdr);.. /*
38420 20 49 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 64 If running in d
38430 69 72 65 63 74 20 6d 6f 64 65 2c 20 77 72 69 74 irect mode, writ
38440 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f e the contents o
38450 66 20 70 61 67 65 20 31 20 74 6f 20 74 68 65 20 f page 1 to the
38460 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 file. */. i
38470 66 28 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 29 f( DIRECT_MODE )
38480 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 {. const
38490 76 6f 69 64 20 2a 7a 42 75 66 3b 0a 20 20 20 20 void *zBuf;.
384a0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
384b0 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 3e 30 er->dbFileSize>0
384c0 20 29 3b 0a 20 20 20 20 20 20 20 20 43 4f 44 45 );. CODE
384d0 43 32 28 70 50 61 67 65 72 2c 20 70 50 67 48 64 C2(pPager, pPgHd
384e0 72 2d 3e 70 44 61 74 61 2c 20 31 2c 20 36 2c 20 r->pData, 1, 6,
384f0 72 63 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c rc=SQLITE_NOMEM,
38500 20 7a 42 75 66 29 3b 0a 20 20 20 20 20 20 20 20 zBuf);.
38510 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
38520 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
38530 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
38540 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 7a te(pPager->fd, z
38550 42 75 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 Buf, pPager->pag
38560 65 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20 eSize, 0);.
38570 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 74 pPager->aSt
38580 61 74 5b 50 41 47 45 52 5f 53 54 41 54 5f 57 52 at[PAGER_STAT_WR
38590 49 54 45 5d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 ITE]++;.
385a0 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 }. if( rc
385b0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
385c0 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d pPager-
385d0 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 >changeCountDone
385e0 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 1;. }.
385f0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
38600 20 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 pPager->cha
38610 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 ngeCountDone = 1
38620 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
38630 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 . /* Release
38640 74 68 65 20 70 61 67 65 20 72 65 66 65 72 65 6e the page referen
38650 63 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 ce. */. sqlit
38660 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 e3PagerUnref(pPg
38670 48 64 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 Hdr);. }. retu
38680 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
38690 53 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 Sync the databas
386a0 65 20 66 69 6c 65 20 74 6f 20 64 69 73 6b 2e 20 e file to disk.
386b0 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 This is a no-op
386c0 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 for in-memory da
386d0 74 61 62 61 73 65 73 0a 2a 2a 20 6f 72 20 70 61 tabases.** or pa
386e0 67 65 73 20 77 69 74 68 20 74 68 65 20 50 61 67 ges with the