blob: d2b84b97b70816ea382745bf085b3b417271bb68 [file] [log] [blame]
drhed7c8552001-04-11 14:29:21 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhed7c8552001-04-11 14:29:21 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhed7c8552001-04-11 14:29:21 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drhed7c8552001-04-11 14:29:21 +000010**
11*************************************************************************
drhb19a2bc2001-09-16 00:13:26 +000012** This is the implementation of the page cache subsystem or "pager".
drhed7c8552001-04-11 14:29:21 +000013**
drhb19a2bc2001-09-16 00:13:26 +000014** The pager is used to access a database disk file. It implements
15** atomic commit and rollback through the use of a journal file that
16** is separate from the database file. The pager also implements file
17** locking to prevent two processes from writing the same database
18** file simultaneously, or one process from reading the database while
19** another is writing.
drhed7c8552001-04-11 14:29:21 +000020**
danielk19771d850a72004-05-31 08:26:49 +000021** @(#) $Id: pager.c,v 1.109 2004/05/31 08:26:49 danielk1977 Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh829e8022002-11-06 14:08:11 +000023#include "os.h" /* Must be first to enable large file support */
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025#include "pager.h"
drhed7c8552001-04-11 14:29:21 +000026#include <assert.h>
drhd9b02572001-04-15 00:37:09 +000027#include <string.h>
drhed7c8552001-04-11 14:29:21 +000028
29/*
drhdb48ee02003-01-16 13:42:43 +000030** Macros for troubleshooting. Normally turned off
31*/
32#if 0
33static Pager *mainPager = 0;
34#define SET_PAGER(X) if( mainPager==0 ) mainPager = (X)
35#define CLR_PAGER(X) if( mainPager==(X) ) mainPager = 0
36#define TRACE1(X) if( pPager==mainPager ) fprintf(stderr,X)
37#define TRACE2(X,Y) if( pPager==mainPager ) fprintf(stderr,X,Y)
38#define TRACE3(X,Y,Z) if( pPager==mainPager ) fprintf(stderr,X,Y,Z)
39#else
40#define SET_PAGER(X)
41#define CLR_PAGER(X)
42#define TRACE1(X)
43#define TRACE2(X,Y)
44#define TRACE3(X,Y,Z)
45#endif
46
47
48/*
drhed7c8552001-04-11 14:29:21 +000049** The page cache as a whole is always in one of the following
50** states:
51**
52** SQLITE_UNLOCK The page cache is not currently reading or
53** writing the database file. There is no
54** data held in memory. This is the initial
55** state.
56**
57** SQLITE_READLOCK The page cache is reading the database.
58** Writing is not permitted. There can be
59** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000060** file at the same time.
drhed7c8552001-04-11 14:29:21 +000061**
62** SQLITE_WRITELOCK The page cache is writing the database.
63** Access is exclusive. No other processes or
64** threads can be reading or writing while one
65** process is writing.
66**
drh306dc212001-05-21 13:45:10 +000067** The page cache comes up in SQLITE_UNLOCK. The first time a
68** sqlite_page_get() occurs, the state transitions to SQLITE_READLOCK.
drhed7c8552001-04-11 14:29:21 +000069** After all pages have been released using sqlite_page_unref(),
drh306dc212001-05-21 13:45:10 +000070** the state transitions back to SQLITE_UNLOCK. The first time
drhed7c8552001-04-11 14:29:21 +000071** that sqlite_page_write() is called, the state transitions to
drh306dc212001-05-21 13:45:10 +000072** SQLITE_WRITELOCK. (Note that sqlite_page_write() can only be
73** called on an outstanding page which means that the pager must
74** be in SQLITE_READLOCK before it transitions to SQLITE_WRITELOCK.)
75** The sqlite_page_rollback() and sqlite_page_commit() functions
76** transition the state from SQLITE_WRITELOCK back to SQLITE_READLOCK.
drhed7c8552001-04-11 14:29:21 +000077*/
78#define SQLITE_UNLOCK 0
79#define SQLITE_READLOCK 1
80#define SQLITE_WRITELOCK 2
81
drhd9b02572001-04-15 00:37:09 +000082
drhed7c8552001-04-11 14:29:21 +000083/*
84** Each in-memory image of a page begins with the following header.
drhbd03cae2001-06-02 02:40:57 +000085** This header is only visible to this pager module. The client
86** code that calls pager sees only the data that follows the header.
drhf6038712004-02-08 18:07:34 +000087**
drh3aac2dd2004-04-26 14:10:20 +000088** Client code should call sqlite3pager_write() on a page prior to making
89** any modifications to that page. The first time sqlite3pager_write()
drhf6038712004-02-08 18:07:34 +000090** is called, the original page contents are written into the rollback
91** journal and PgHdr.inJournal and PgHdr.needSync are set. Later, once
92** the journal page has made it onto the disk surface, PgHdr.needSync
93** is cleared. The modified page cannot be written back into the original
94** database file until the journal pages has been synced to disk and the
95** PgHdr.needSync has been cleared.
96**
drh3aac2dd2004-04-26 14:10:20 +000097** The PgHdr.dirty flag is set when sqlite3pager_write() is called and
drhf6038712004-02-08 18:07:34 +000098** is cleared again when the page content is written back to the original
99** database file.
drhed7c8552001-04-11 14:29:21 +0000100*/
drhd9b02572001-04-15 00:37:09 +0000101typedef struct PgHdr PgHdr;
drhed7c8552001-04-11 14:29:21 +0000102struct PgHdr {
103 Pager *pPager; /* The pager to which this page belongs */
104 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +0000105 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
drhd9b02572001-04-15 00:37:09 +0000106 PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */
drhac69b052004-05-12 13:30:07 +0000107 PgHdr *pNextAll; /* A list of all pages */
108 PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */
drh193a6b42002-07-07 16:52:46 +0000109 u8 inJournal; /* TRUE if has been written to journal */
drhac69b052004-05-12 13:30:07 +0000110 u8 inStmt; /* TRUE if in the statement subjournal */
drh193a6b42002-07-07 16:52:46 +0000111 u8 dirty; /* TRUE if we need to write back changes */
drhdb48ee02003-01-16 13:42:43 +0000112 u8 needSync; /* Sync journal before writing this page */
drh193a6b42002-07-07 16:52:46 +0000113 u8 alwaysRollback; /* Disable dont_rollback() for this page */
drhac69b052004-05-12 13:30:07 +0000114 short int nRef; /* Number of users of this page */
drh2554f8b2003-01-22 01:26:44 +0000115 PgHdr *pDirty; /* Dirty pages sorted by PgHdr.pgno */
drhd0ba1932004-02-10 01:54:28 +0000116 /* SQLITE_PAGE_SIZE bytes of page data follow this header */
drh973b6e32003-02-12 14:09:42 +0000117 /* Pager.nExtra bytes of local data follow the page data */
drhed7c8552001-04-11 14:29:21 +0000118};
119
drhac69b052004-05-12 13:30:07 +0000120/*
121** For an in-memory only database, some extra information is recorded about
122** each page so that changes can be rolled back. (Journal files are not
123** used for in-memory databases.) The following information is added to
124** the end of every EXTRA block for in-memory databases.
125**
126** This information could have been added directly to the PgHdr structure.
127** But then it would take up an extra 8 bytes of storage on every PgHdr
128** even for disk-based databases. Splitting it out saves 8 bytes. This
129** is only a savings of 0.8% but those percentages add up.
130*/
131typedef struct PgHistory PgHistory;
132struct PgHistory {
133 u8 *pOrig; /* Original page text. Restore to this on a full rollback */
134 u8 *pStmt; /* Text as it was at the beginning of the current statement */
135};
drh9eb9e262004-02-11 02:18:05 +0000136
137/*
138** A macro used for invoking the codec if there is one
139*/
140#ifdef SQLITE_HAS_CODEC
141# define CODEC(P,D,N,X) if( P->xCodec ){ P->xCodec(P->pCodecArg,D,N,X); }
142#else
143# define CODEC(P,D,N,X)
144#endif
145
drhed7c8552001-04-11 14:29:21 +0000146/*
drh69688d52001-04-14 16:38:23 +0000147** Convert a pointer to a PgHdr into a pointer to its data
148** and back again.
drhed7c8552001-04-11 14:29:21 +0000149*/
150#define PGHDR_TO_DATA(P) ((void*)(&(P)[1]))
151#define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1])
drhd0ba1932004-02-10 01:54:28 +0000152#define PGHDR_TO_EXTRA(P) ((void*)&((char*)(&(P)[1]))[SQLITE_PAGE_SIZE])
drhac69b052004-05-12 13:30:07 +0000153#define PGHDR_TO_HIST(P,PGR) \
154 ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->pageSize+(PGR)->nExtra])
drhed7c8552001-04-11 14:29:21 +0000155
156/*
drhed7c8552001-04-11 14:29:21 +0000157** How big to make the hash table used for locating in-memory pages
drh836faa42003-01-11 13:30:57 +0000158** by page number.
drhed7c8552001-04-11 14:29:21 +0000159*/
drh836faa42003-01-11 13:30:57 +0000160#define N_PG_HASH 2048
161
162/*
163** Hash a page number
164*/
165#define pager_hash(PN) ((PN)&(N_PG_HASH-1))
drhed7c8552001-04-11 14:29:21 +0000166
167/*
168** A open page cache is an instance of the following structure.
169*/
170struct Pager {
171 char *zFilename; /* Name of the database file */
172 char *zJournal; /* Name of the journal file */
drha76c82e2003-07-27 18:59:42 +0000173 char *zDirectory; /* Directory hold database and journal files */
drh8cfbf082001-09-19 13:22:39 +0000174 OsFile fd, jfd; /* File descriptors for database and journal */
drhac69b052004-05-12 13:30:07 +0000175 OsFile stfd; /* File descriptor for the statement subjournal*/
drhed7c8552001-04-11 14:29:21 +0000176 int dbSize; /* Number of pages in the file */
drh69688d52001-04-14 16:38:23 +0000177 int origDbSize; /* dbSize before the current change */
drhac69b052004-05-12 13:30:07 +0000178 int stmtSize; /* Size of database (in pages) at stmt_begin() */
179 off_t stmtJSize; /* Size of journal at stmt_begin() */
drh968af522003-02-11 14:55:40 +0000180 int nRec; /* Number of pages written to the journal */
181 u32 cksumInit; /* Quasi-random value added to every checksum */
drhac69b052004-05-12 13:30:07 +0000182 int stmtNRec; /* Number of records in stmt subjournal */
drh7e3b0a02001-04-28 16:52:40 +0000183 int nExtra; /* Add this many bytes to each in-memory page */
drhb6f41482004-05-14 01:58:11 +0000184 void (*xDestructor)(void*,int); /* Call this routine when freeing pages */
185 int pageSize; /* Number of bytes in a page */
drhed7c8552001-04-11 14:29:21 +0000186 int nPage; /* Total number of in-memory pages */
drhd9b02572001-04-15 00:37:09 +0000187 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
drhed7c8552001-04-11 14:29:21 +0000188 int mxPage; /* Maximum number of pages to hold in cache */
drhd9b02572001-04-15 00:37:09 +0000189 int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */
drh9eb9e262004-02-11 02:18:05 +0000190 void (*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drhb20ea9d2004-02-09 01:20:36 +0000191 void *pCodecArg; /* First argument to xCodec() */
drh603240c2002-03-05 01:11:12 +0000192 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000193 u8 journalStarted; /* True if header of journal is synced */
194 u8 useJournal; /* Use a rollback journal on this file */
drhac69b052004-05-12 13:30:07 +0000195 u8 stmtOpen; /* True if the statement subjournal is open */
196 u8 stmtInUse; /* True we are in a statement subtransaction */
197 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000198 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000199 u8 fullSync; /* Do extra syncs of the journal for robustness */
drh603240c2002-03-05 01:11:12 +0000200 u8 state; /* SQLITE_UNLOCK, _READLOCK or _WRITELOCK */
201 u8 errMask; /* One of several kinds of errors */
202 u8 tempFile; /* zFilename is a temporary file */
203 u8 readOnly; /* True for a read-only database */
204 u8 needSync; /* True if an fsync() is needed on the journal */
drha1680452002-04-18 01:56:57 +0000205 u8 dirtyFile; /* True if database file has changed in any way */
drh193a6b42002-07-07 16:52:46 +0000206 u8 alwaysRollback; /* Disable dont_rollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000207 u8 memDb; /* True to inhibit all file I/O */
drh603240c2002-03-05 01:11:12 +0000208 u8 *aInJournal; /* One bit for each page in the database file */
drhac69b052004-05-12 13:30:07 +0000209 u8 *aInStmt; /* One bit for each page in the database */
drhed7c8552001-04-11 14:29:21 +0000210 PgHdr *pFirst, *pLast; /* List of free pages */
drh341eae82003-01-21 02:39:36 +0000211 PgHdr *pFirstSynced; /* First free page with PgHdr.needSync==0 */
drhd9b02572001-04-15 00:37:09 +0000212 PgHdr *pAll; /* List of all pages */
drhac69b052004-05-12 13:30:07 +0000213 PgHdr *pStmt; /* List of pages in the statement subjournal */
drhed7c8552001-04-11 14:29:21 +0000214 PgHdr *aHash[N_PG_HASH]; /* Hash table to map page number of PgHdr */
drhd9b02572001-04-15 00:37:09 +0000215};
216
217/*
218** These are bits that can be set in Pager.errMask.
219*/
220#define PAGER_ERR_FULL 0x01 /* a write() failed */
221#define PAGER_ERR_MEM 0x02 /* malloc() failed */
222#define PAGER_ERR_LOCK 0x04 /* error in the locking protocol */
223#define PAGER_ERR_CORRUPT 0x08 /* database or journal corruption */
drh81a20f22001-10-12 17:30:04 +0000224#define PAGER_ERR_DISK 0x10 /* general disk I/O error - bad hard drive? */
drhd9b02572001-04-15 00:37:09 +0000225
226/*
227** The journal file contains page records in the following
228** format.
drh968af522003-02-11 14:55:40 +0000229**
230** Actually, this structure is the complete page record for pager
231** formats less than 3. Beginning with format 3, this record is surrounded
232** by two checksums.
drhd9b02572001-04-15 00:37:09 +0000233*/
234typedef struct PageRecord PageRecord;
235struct PageRecord {
drhb20ea9d2004-02-09 01:20:36 +0000236 Pgno pgno; /* The page number */
drhd0ba1932004-02-10 01:54:28 +0000237 char aData[SQLITE_PAGE_SIZE]; /* Original data for page pgno */
drhd9b02572001-04-15 00:37:09 +0000238};
239
240/*
drh5e00f6c2001-09-13 13:46:56 +0000241** Journal files begin with the following magic string. The data
242** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000243**
drh968af522003-02-11 14:55:40 +0000244** There are three journal formats (so far). The 1st journal format writes
245** 32-bit integers in the byte-order of the host machine. New
246** formats writes integers as big-endian. All new journals use the
drh94f33312002-08-12 12:29:56 +0000247** new format, but we have to be able to read an older journal in order
drh968af522003-02-11 14:55:40 +0000248** to rollback journals created by older versions of the library.
249**
250** The 3rd journal format (added for 2.8.0) adds additional sanity
251** checking information to the journal. If the power fails while the
252** journal is being written, semi-random garbage data might appear in
253** the journal file after power is restored. If an attempt is then made
254** to roll the journal back, the database could be corrupted. The additional
255** sanity checking data is an attempt to discover the garbage in the
256** journal and ignore it.
257**
258** The sanity checking information for the 3rd journal format consists
259** of a 32-bit checksum on each page of data. The checksum covers both
drhd0ba1932004-02-10 01:54:28 +0000260** the page number and the SQLITE_PAGE_SIZE bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000261** This cksum is initialized to a 32-bit random value that appears in the
262** journal file right after the header. The random initializer is important,
263** because garbage data that appears at the end of a journal is likely
264** data that was once in other files that have now been deleted. If the
265** garbage data came from an obsolete journal file, the checksums might
266** be correct. But by initializing the checksum to random value which
267** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000268*/
drh968af522003-02-11 14:55:40 +0000269static const unsigned char aJournalMagic1[] = {
drhd9b02572001-04-15 00:37:09 +0000270 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4,
drhed7c8552001-04-11 14:29:21 +0000271};
drh968af522003-02-11 14:55:40 +0000272static const unsigned char aJournalMagic2[] = {
drh94f33312002-08-12 12:29:56 +0000273 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd5,
274};
drh968af522003-02-11 14:55:40 +0000275static const unsigned char aJournalMagic3[] = {
276 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd6,
277};
278#define JOURNAL_FORMAT_1 1
279#define JOURNAL_FORMAT_2 2
280#define JOURNAL_FORMAT_3 3
drh94f33312002-08-12 12:29:56 +0000281
282/*
drh968af522003-02-11 14:55:40 +0000283** The following integer determines what format to use when creating
284** new primary journal files. By default we always use format 3.
285** When testing, we can set this value to older journal formats in order to
286** make sure that newer versions of the library are able to rollback older
287** journal files.
288**
drhac69b052004-05-12 13:30:07 +0000289** Note that statement journals always use format 2 and omit the header.
drh94f33312002-08-12 12:29:56 +0000290*/
291#ifdef SQLITE_TEST
drh968af522003-02-11 14:55:40 +0000292int journal_format = 3;
drh74587e52002-08-13 00:01:16 +0000293#else
drh968af522003-02-11 14:55:40 +0000294# define journal_format 3
drh94f33312002-08-12 12:29:56 +0000295#endif
drhed7c8552001-04-11 14:29:21 +0000296
297/*
drh968af522003-02-11 14:55:40 +0000298** The size of the header and of each page in the journal varies according
299** to which journal format is being used. The following macros figure out
300** the sizes based on format numbers.
301*/
302#define JOURNAL_HDR_SZ(X) \
303 (sizeof(aJournalMagic1) + sizeof(Pgno) + ((X)>=3)*2*sizeof(u32))
304#define JOURNAL_PG_SZ(X) \
drhd0ba1932004-02-10 01:54:28 +0000305 (SQLITE_PAGE_SIZE + sizeof(Pgno) + ((X)>=3)*sizeof(u32))
drh968af522003-02-11 14:55:40 +0000306
307/*
drhdd793422001-06-28 01:54:48 +0000308** Enable reference count tracking here:
309*/
drh74587e52002-08-13 00:01:16 +0000310#ifdef SQLITE_TEST
drh3aac2dd2004-04-26 14:10:20 +0000311 int pager3_refinfo_enable = 0;
drhdd793422001-06-28 01:54:48 +0000312 static void pager_refinfo(PgHdr *p){
313 static int cnt = 0;
drh3aac2dd2004-04-26 14:10:20 +0000314 if( !pager3_refinfo_enable ) return;
drhdd793422001-06-28 01:54:48 +0000315 printf(
316 "REFCNT: %4d addr=0x%08x nRef=%d\n",
317 p->pgno, (int)PGHDR_TO_DATA(p), p->nRef
318 );
319 cnt++; /* Something to set a breakpoint on */
320 }
321# define REFINFO(X) pager_refinfo(X)
322#else
323# define REFINFO(X)
324#endif
325
326/*
drh34e79ce2004-02-08 06:05:46 +0000327** Read a 32-bit integer from the given file descriptor. Store the integer
328** that is read in *pRes. Return SQLITE_OK if everything worked, or an
329** error code is something goes wrong.
330**
331** If the journal format is 2 or 3, read a big-endian integer. If the
332** journal format is 1, read an integer in the native byte-order of the
333** host machine.
drh94f33312002-08-12 12:29:56 +0000334*/
drh968af522003-02-11 14:55:40 +0000335static int read32bits(int format, OsFile *fd, u32 *pRes){
drh94f33312002-08-12 12:29:56 +0000336 u32 res;
337 int rc;
danielk19774adee202004-05-08 08:23:19 +0000338 rc = sqlite3OsRead(fd, &res, sizeof(res));
drh968af522003-02-11 14:55:40 +0000339 if( rc==SQLITE_OK && format>JOURNAL_FORMAT_1 ){
drh94f33312002-08-12 12:29:56 +0000340 unsigned char ac[4];
341 memcpy(ac, &res, 4);
342 res = (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3];
343 }
344 *pRes = res;
345 return rc;
346}
347
348/*
drh34e79ce2004-02-08 06:05:46 +0000349** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
350** on success or an error code is something goes wrong.
351**
352** If the journal format is 2 or 3, write the integer as 4 big-endian
353** bytes. If the journal format is 1, write the integer in the native
354** byte order. In normal operation, only formats 2 and 3 are used.
355** Journal format 1 is only used for testing.
drh94f33312002-08-12 12:29:56 +0000356*/
357static int write32bits(OsFile *fd, u32 val){
358 unsigned char ac[4];
drh968af522003-02-11 14:55:40 +0000359 if( journal_format<=1 ){
danielk19774adee202004-05-08 08:23:19 +0000360 return sqlite3OsWrite(fd, &val, 4);
drh94f33312002-08-12 12:29:56 +0000361 }
drh94f33312002-08-12 12:29:56 +0000362 ac[0] = (val>>24) & 0xff;
363 ac[1] = (val>>16) & 0xff;
364 ac[2] = (val>>8) & 0xff;
365 ac[3] = val & 0xff;
danielk19774adee202004-05-08 08:23:19 +0000366 return sqlite3OsWrite(fd, ac, 4);
drh94f33312002-08-12 12:29:56 +0000367}
368
drh2554f8b2003-01-22 01:26:44 +0000369/*
370** Write a 32-bit integer into a page header right before the
371** page data. This will overwrite the PgHdr.pDirty pointer.
drh34e79ce2004-02-08 06:05:46 +0000372**
373** The integer is big-endian for formats 2 and 3 and native byte order
374** for journal format 1.
drh2554f8b2003-01-22 01:26:44 +0000375*/
drh968af522003-02-11 14:55:40 +0000376static void store32bits(u32 val, PgHdr *p, int offset){
drh2554f8b2003-01-22 01:26:44 +0000377 unsigned char *ac;
drhec1bd0b2003-08-26 11:41:27 +0000378 ac = &((unsigned char*)PGHDR_TO_DATA(p))[offset];
drh968af522003-02-11 14:55:40 +0000379 if( journal_format<=1 ){
drh2554f8b2003-01-22 01:26:44 +0000380 memcpy(ac, &val, 4);
381 }else{
382 ac[0] = (val>>24) & 0xff;
383 ac[1] = (val>>16) & 0xff;
384 ac[2] = (val>>8) & 0xff;
385 ac[3] = val & 0xff;
386 }
387}
388
drh94f33312002-08-12 12:29:56 +0000389
390/*
drhd9b02572001-04-15 00:37:09 +0000391** Convert the bits in the pPager->errMask into an approprate
392** return code.
393*/
394static int pager_errcode(Pager *pPager){
395 int rc = SQLITE_OK;
396 if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL;
drh81a20f22001-10-12 17:30:04 +0000397 if( pPager->errMask & PAGER_ERR_DISK ) rc = SQLITE_IOERR;
drhd9b02572001-04-15 00:37:09 +0000398 if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL;
399 if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM;
400 if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT;
401 return rc;
drhed7c8552001-04-11 14:29:21 +0000402}
403
404/*
drh03eb96a2002-11-10 23:32:56 +0000405** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +0000406** statement journal.
drh03eb96a2002-11-10 23:32:56 +0000407**
408** The Pager keeps a separate list of pages that are currently in
drhac69b052004-05-12 13:30:07 +0000409** the statement journal. This helps the sqlite3pager_stmt_commit()
drh03eb96a2002-11-10 23:32:56 +0000410** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +0000411** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +0000412*/
drh3aac2dd2004-04-26 14:10:20 +0000413static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +0000414 Pager *pPager = pPg->pPager;
drhac69b052004-05-12 13:30:07 +0000415 if( pPg->inStmt ) return;
416 assert( pPg->pPrevStmt==0 && pPg->pNextStmt==0 );
417 pPg->pPrevStmt = 0;
418 if( pPager->pStmt ){
419 pPager->pStmt->pPrevStmt = pPg;
drh03eb96a2002-11-10 23:32:56 +0000420 }
drhac69b052004-05-12 13:30:07 +0000421 pPg->pNextStmt = pPager->pStmt;
422 pPager->pStmt = pPg;
423 pPg->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +0000424}
drh3aac2dd2004-04-26 14:10:20 +0000425static void page_remove_from_stmt_list(PgHdr *pPg){
drhac69b052004-05-12 13:30:07 +0000426 if( !pPg->inStmt ) return;
427 if( pPg->pPrevStmt ){
428 assert( pPg->pPrevStmt->pNextStmt==pPg );
429 pPg->pPrevStmt->pNextStmt = pPg->pNextStmt;
drh03eb96a2002-11-10 23:32:56 +0000430 }else{
drhac69b052004-05-12 13:30:07 +0000431 assert( pPg->pPager->pStmt==pPg );
432 pPg->pPager->pStmt = pPg->pNextStmt;
drh03eb96a2002-11-10 23:32:56 +0000433 }
drhac69b052004-05-12 13:30:07 +0000434 if( pPg->pNextStmt ){
435 assert( pPg->pNextStmt->pPrevStmt==pPg );
436 pPg->pNextStmt->pPrevStmt = pPg->pPrevStmt;
drh03eb96a2002-11-10 23:32:56 +0000437 }
drhac69b052004-05-12 13:30:07 +0000438 pPg->pNextStmt = 0;
439 pPg->pPrevStmt = 0;
440 pPg->inStmt = 0;
drh03eb96a2002-11-10 23:32:56 +0000441}
442
443/*
drhed7c8552001-04-11 14:29:21 +0000444** Find a page in the hash table given its page number. Return
445** a pointer to the page or NULL if not found.
446*/
drhd9b02572001-04-15 00:37:09 +0000447static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh836faa42003-01-11 13:30:57 +0000448 PgHdr *p = pPager->aHash[pager_hash(pgno)];
drhed7c8552001-04-11 14:29:21 +0000449 while( p && p->pgno!=pgno ){
450 p = p->pNextHash;
451 }
452 return p;
453}
454
455/*
456** Unlock the database and clear the in-memory cache. This routine
457** sets the state of the pager back to what it was when it was first
458** opened. Any outstanding pages are invalidated and subsequent attempts
459** to access those pages will likely result in a coredump.
460*/
drhd9b02572001-04-15 00:37:09 +0000461static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000462 PgHdr *pPg, *pNext;
drhd9b02572001-04-15 00:37:09 +0000463 for(pPg=pPager->pAll; pPg; pPg=pNext){
464 pNext = pPg->pNextAll;
465 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000466 }
467 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +0000468 pPager->pFirstSynced = 0;
drhd9b02572001-04-15 00:37:09 +0000469 pPager->pLast = 0;
470 pPager->pAll = 0;
drhed7c8552001-04-11 14:29:21 +0000471 memset(pPager->aHash, 0, sizeof(pPager->aHash));
472 pPager->nPage = 0;
drhfa86c412002-02-02 15:01:15 +0000473 if( pPager->state>=SQLITE_WRITELOCK ){
drh3aac2dd2004-04-26 14:10:20 +0000474 sqlite3pager_rollback(pPager);
drhed7c8552001-04-11 14:29:21 +0000475 }
danielk19774adee202004-05-08 08:23:19 +0000476 sqlite3OsUnlock(&pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000477 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000478 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +0000479 pPager->nRef = 0;
drh8cfbf082001-09-19 13:22:39 +0000480 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +0000481}
482
483/*
484** When this routine is called, the pager has the journal file open and
485** a write lock on the database. This routine releases the database
486** write lock and acquires a read lock in its place. The journal file
487** is deleted and closed.
drh50457892003-09-06 01:10:47 +0000488**
489** TODO: Consider keeping the journal file open for temporary databases.
490** This might give a performance improvement on windows where opening
491** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +0000492*/
drhd9b02572001-04-15 00:37:09 +0000493static int pager_unwritelock(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000494 int rc;
drhd9b02572001-04-15 00:37:09 +0000495 PgHdr *pPg;
drhfa86c412002-02-02 15:01:15 +0000496 if( pPager->state<SQLITE_WRITELOCK ) return SQLITE_OK;
drh3aac2dd2004-04-26 14:10:20 +0000497 sqlite3pager_stmt_commit(pPager);
drhac69b052004-05-12 13:30:07 +0000498 if( pPager->stmtOpen ){
499 sqlite3OsClose(&pPager->stfd);
500 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +0000501 }
drhda47d772002-12-02 04:25:19 +0000502 if( pPager->journalOpen ){
danielk19774adee202004-05-08 08:23:19 +0000503 sqlite3OsClose(&pPager->jfd);
drhda47d772002-12-02 04:25:19 +0000504 pPager->journalOpen = 0;
danielk19774adee202004-05-08 08:23:19 +0000505 sqlite3OsDelete(pPager->zJournal);
drhda47d772002-12-02 04:25:19 +0000506 sqliteFree( pPager->aInJournal );
507 pPager->aInJournal = 0;
508 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
509 pPg->inJournal = 0;
510 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +0000511 pPg->needSync = 0;
drhda47d772002-12-02 04:25:19 +0000512 }
513 }else{
514 assert( pPager->dirtyFile==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +0000515 }
danielk19774adee202004-05-08 08:23:19 +0000516 rc = sqlite3OsReadLock(&pPager->fd);
drh8e298f92002-07-06 16:28:47 +0000517 if( rc==SQLITE_OK ){
518 pPager->state = SQLITE_READLOCK;
519 }else{
520 /* This can only happen if a process does a BEGIN, then forks and the
521 ** child process does the COMMIT. Because of the semantics of unix
522 ** file locking, the unlock will fail.
523 */
524 pPager->state = SQLITE_UNLOCK;
525 }
drhed7c8552001-04-11 14:29:21 +0000526 return rc;
527}
528
drhed7c8552001-04-11 14:29:21 +0000529/*
drh968af522003-02-11 14:55:40 +0000530** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +0000531**
532** This is not a real checksum. It is really just the sum of the
533** random initial value and the page number. We considered do a checksum
534** of the database, but that was found to be too slow.
drh968af522003-02-11 14:55:40 +0000535*/
536static u32 pager_cksum(Pager *pPager, Pgno pgno, const char *aData){
537 u32 cksum = pPager->cksumInit + pgno;
drh968af522003-02-11 14:55:40 +0000538 return cksum;
539}
540
541/*
drhfa86c412002-02-02 15:01:15 +0000542** Read a single page from the journal file opened on file descriptor
543** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +0000544**
545** There are three different journal formats. The format parameter determines
546** which format is used by the journal that is played back.
drhfa86c412002-02-02 15:01:15 +0000547*/
drh968af522003-02-11 14:55:40 +0000548static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int format){
drhfa86c412002-02-02 15:01:15 +0000549 int rc;
550 PgHdr *pPg; /* An existing page in the cache */
551 PageRecord pgRec;
drh968af522003-02-11 14:55:40 +0000552 u32 cksum;
drhfa86c412002-02-02 15:01:15 +0000553
drh968af522003-02-11 14:55:40 +0000554 rc = read32bits(format, jfd, &pgRec.pgno);
drh99ee3602003-02-16 19:13:36 +0000555 if( rc!=SQLITE_OK ) return rc;
danielk19774adee202004-05-08 08:23:19 +0000556 rc = sqlite3OsRead(jfd, &pgRec.aData, sizeof(pgRec.aData));
drh99ee3602003-02-16 19:13:36 +0000557 if( rc!=SQLITE_OK ) return rc;
drhfa86c412002-02-02 15:01:15 +0000558
drh968af522003-02-11 14:55:40 +0000559 /* Sanity checking on the page. This is more important that I originally
560 ** thought. If a power failure occurs while the journal is being written,
561 ** it could cause invalid data to be written into the journal. We need to
562 ** detect this invalid data (with high probability) and ignore it.
563 */
564 if( pgRec.pgno==0 ){
565 return SQLITE_DONE;
566 }
drh7d02cb72003-06-04 16:24:39 +0000567 if( pgRec.pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +0000568 return SQLITE_OK;
569 }
570 if( format>=JOURNAL_FORMAT_3 ){
571 rc = read32bits(format, jfd, &cksum);
drh99ee3602003-02-16 19:13:36 +0000572 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +0000573 if( pager_cksum(pPager, pgRec.pgno, pgRec.aData)!=cksum ){
574 return SQLITE_DONE;
575 }
576 }
drhfa86c412002-02-02 15:01:15 +0000577
578 /* Playback the page. Update the in-memory copy of the page
579 ** at the same time, if there is one.
580 */
581 pPg = pager_lookup(pPager, pgRec.pgno);
drh99ee3602003-02-16 19:13:36 +0000582 TRACE2("PLAYBACK %d\n", pgRec.pgno);
danielk19774adee202004-05-08 08:23:19 +0000583 sqlite3OsSeek(&pPager->fd, (pgRec.pgno-1)*(off_t)SQLITE_PAGE_SIZE);
584 rc = sqlite3OsWrite(&pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE);
drhfa86c412002-02-02 15:01:15 +0000585 if( pPg ){
drhacf4ac92003-12-17 23:57:34 +0000586 /* No page should ever be rolled back that is in use, except for page
587 ** 1 which is held in use in order to keep the lock on the database
588 ** active.
589 */
drhb6f41482004-05-14 01:58:11 +0000590 void *pData;
drhacf4ac92003-12-17 23:57:34 +0000591 assert( pPg->nRef==0 || pPg->pgno==1 );
drhb6f41482004-05-14 01:58:11 +0000592 pData = PGHDR_TO_DATA(pPg);
593 memcpy(pData, pgRec.aData, pPager->pageSize);
drhde647132004-05-07 17:57:49 +0000594 if( pPager->xDestructor ){
drhb6f41482004-05-14 01:58:11 +0000595 pPager->xDestructor(pData, pPager->pageSize);
drhde647132004-05-07 17:57:49 +0000596 }
drhdb48ee02003-01-16 13:42:43 +0000597 pPg->dirty = 0;
598 pPg->needSync = 0;
drhb6f41482004-05-14 01:58:11 +0000599 CODEC(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +0000600 }
601 return rc;
602}
603
604/*
drhed7c8552001-04-11 14:29:21 +0000605** Playback the journal and thus restore the database file to
606** the state it was in before we started making changes.
607**
drh34e79ce2004-02-08 06:05:46 +0000608** The journal file format is as follows:
609**
610** * 8 byte prefix. One of the aJournalMagic123 vectors defined
611** above. The format of the journal file is determined by which
612** of the three prefix vectors is seen.
613** * 4 byte big-endian integer which is the number of valid page records
614** in the journal. If this value is 0xffffffff, then compute the
615** number of page records from the journal size. This field appears
616** in format 3 only.
617** * 4 byte big-endian integer which is the initial value for the
618** sanity checksum. This field appears in format 3 only.
619** * 4 byte integer which is the number of pages to truncate the
620** database to during a rollback.
621** * Zero or more pages instances, each as follows:
622** + 4 byte page number.
drhd0ba1932004-02-10 01:54:28 +0000623** + SQLITE_PAGE_SIZE bytes of data.
drh34e79ce2004-02-08 06:05:46 +0000624** + 4 byte checksum (format 3 only)
625**
626** When we speak of the journal header, we mean the first 4 bullets above.
627** Each entry in the journal is an instance of the 5th bullet. Note that
628** bullets 2 and 3 only appear in format-3 journals.
629**
630** Call the value from the second bullet "nRec". nRec is the number of
631** valid page entries in the journal. In most cases, you can compute the
632** value of nRec from the size of the journal file. But if a power
633** failure occurred while the journal was being written, it could be the
634** case that the size of the journal file had already been increased but
635** the extra entries had not yet made it safely to disk. In such a case,
636** the value of nRec computed from the file size would be too large. For
637** that reason, we always use the nRec value in the header.
638**
639** If the nRec value is 0xffffffff it means that nRec should be computed
640** from the file size. This value is used when the user selects the
641** no-sync option for the journal. A power failure could lead to corruption
642** in this case. But for things like temporary table (which will be
643** deleted when the power is restored) we don't care.
644**
645** Journal formats 1 and 2 do not have an nRec value in the header so we
646** have to compute nRec from the file size. This has risks (as described
647** above) which is why all persistent tables have been changed to use
648** format 3.
drhed7c8552001-04-11 14:29:21 +0000649**
drhd9b02572001-04-15 00:37:09 +0000650** If the file opened as the journal file is not a well-formed
drh34e79ce2004-02-08 06:05:46 +0000651** journal file then the database will likely already be
652** corrupted, so the PAGER_ERR_CORRUPT bit is set in pPager->errMask
653** and SQLITE_CORRUPT is returned. If it all works, then this routine
654** returns SQLITE_OK.
drhed7c8552001-04-11 14:29:21 +0000655*/
drh99ee3602003-02-16 19:13:36 +0000656static int pager_playback(Pager *pPager, int useJournalSize){
drh968af522003-02-11 14:55:40 +0000657 off_t szJ; /* Size of the journal file in bytes */
658 int nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +0000659 int i; /* Loop counter */
660 Pgno mxPg = 0; /* Size of the original file in pages */
drh968af522003-02-11 14:55:40 +0000661 int format; /* Format of the journal file. */
662 unsigned char aMagic[sizeof(aJournalMagic1)];
drhed7c8552001-04-11 14:29:21 +0000663 int rc;
664
drhc3a64ba2001-11-22 00:01:27 +0000665 /* Figure out how many records are in the journal. Abort early if
666 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +0000667 */
drh8cfbf082001-09-19 13:22:39 +0000668 assert( pPager->journalOpen );
danielk19774adee202004-05-08 08:23:19 +0000669 sqlite3OsSeek(&pPager->jfd, 0);
670 rc = sqlite3OsFileSize(&pPager->jfd, &szJ);
drhc3a64ba2001-11-22 00:01:27 +0000671 if( rc!=SQLITE_OK ){
672 goto end_playback;
673 }
drh240c5792004-02-08 00:40:52 +0000674
675 /* If the journal file is too small to contain a complete header,
drh34e79ce2004-02-08 06:05:46 +0000676 ** it must mean that the process that created the journal was just
677 ** beginning to write the journal file when it died. In that case,
678 ** the database file should have still been completely unchanged.
679 ** Nothing needs to be rolled back. We can safely ignore this journal.
drh240c5792004-02-08 00:40:52 +0000680 */
drh968af522003-02-11 14:55:40 +0000681 if( szJ < sizeof(aMagic)+sizeof(Pgno) ){
drhc3a64ba2001-11-22 00:01:27 +0000682 goto end_playback;
683 }
684
685 /* Read the beginning of the journal and truncate the
686 ** database file back to its original size.
687 */
danielk19774adee202004-05-08 08:23:19 +0000688 rc = sqlite3OsRead(&pPager->jfd, aMagic, sizeof(aMagic));
drh94f33312002-08-12 12:29:56 +0000689 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000690 rc = SQLITE_PROTOCOL;
691 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000692 }
drh968af522003-02-11 14:55:40 +0000693 if( memcmp(aMagic, aJournalMagic3, sizeof(aMagic))==0 ){
694 format = JOURNAL_FORMAT_3;
695 }else if( memcmp(aMagic, aJournalMagic2, sizeof(aMagic))==0 ){
696 format = JOURNAL_FORMAT_2;
697 }else if( memcmp(aMagic, aJournalMagic1, sizeof(aMagic))==0 ){
698 format = JOURNAL_FORMAT_1;
drh94f33312002-08-12 12:29:56 +0000699 }else{
700 rc = SQLITE_PROTOCOL;
701 goto end_playback;
702 }
drh968af522003-02-11 14:55:40 +0000703 if( format>=JOURNAL_FORMAT_3 ){
drh240c5792004-02-08 00:40:52 +0000704 if( szJ < sizeof(aMagic) + 3*sizeof(u32) ){
705 /* Ignore the journal if it is too small to contain a complete
706 ** header. We already did this test once above, but at the prior
707 ** test, we did not know the journal format and so we had to assume
708 ** the smallest possible header. Now we know the header is bigger
drh34e79ce2004-02-08 06:05:46 +0000709 ** than the minimum so we test again.
drh240c5792004-02-08 00:40:52 +0000710 */
711 goto end_playback;
712 }
drh133cdf62004-01-07 02:52:07 +0000713 rc = read32bits(format, &pPager->jfd, (u32*)&nRec);
drh968af522003-02-11 14:55:40 +0000714 if( rc ) goto end_playback;
715 rc = read32bits(format, &pPager->jfd, &pPager->cksumInit);
716 if( rc ) goto end_playback;
drh99ee3602003-02-16 19:13:36 +0000717 if( nRec==0xffffffff || useJournalSize ){
drh968af522003-02-11 14:55:40 +0000718 nRec = (szJ - JOURNAL_HDR_SZ(3))/JOURNAL_PG_SZ(3);
719 }
720 }else{
drhd8d66e82003-02-12 02:10:15 +0000721 nRec = (szJ - JOURNAL_HDR_SZ(2))/JOURNAL_PG_SZ(2);
722 assert( nRec*JOURNAL_PG_SZ(2)+JOURNAL_HDR_SZ(2)==szJ );
drh968af522003-02-11 14:55:40 +0000723 }
724 rc = read32bits(format, &pPager->jfd, &mxPg);
drhd9b02572001-04-15 00:37:09 +0000725 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000726 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000727 }
drhd8d66e82003-02-12 02:10:15 +0000728 assert( pPager->origDbSize==0 || pPager->origDbSize==mxPg );
danielk19774adee202004-05-08 08:23:19 +0000729 rc = sqlite3OsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)mxPg);
drh81a20f22001-10-12 17:30:04 +0000730 if( rc!=SQLITE_OK ){
731 goto end_playback;
732 }
drhd9b02572001-04-15 00:37:09 +0000733 pPager->dbSize = mxPg;
734
drhfa86c412002-02-02 15:01:15 +0000735 /* Copy original pages out of the journal and back into the database file.
drhed7c8552001-04-11 14:29:21 +0000736 */
drh968af522003-02-11 14:55:40 +0000737 for(i=0; i<nRec; i++){
738 rc = pager_playback_one_page(pPager, &pPager->jfd, format);
739 if( rc!=SQLITE_OK ){
740 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +0000741 rc = SQLITE_OK;
742 }
743 break;
744 }
drhed7c8552001-04-11 14:29:21 +0000745 }
drh81a20f22001-10-12 17:30:04 +0000746
drh4a0681e2003-02-13 01:58:20 +0000747 /* Pages that have been written to the journal but never synced
748 ** where not restored by the loop above. We have to restore those
drh240c5792004-02-08 00:40:52 +0000749 ** pages by reading them back from the original database.
drhdb48ee02003-01-16 13:42:43 +0000750 */
751 if( rc==SQLITE_OK ){
752 PgHdr *pPg;
753 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drhd0ba1932004-02-10 01:54:28 +0000754 char zBuf[SQLITE_PAGE_SIZE];
drh4a0681e2003-02-13 01:58:20 +0000755 if( !pPg->dirty ) continue;
drhdb48ee02003-01-16 13:42:43 +0000756 if( (int)pPg->pgno <= pPager->origDbSize ){
danielk19774adee202004-05-08 08:23:19 +0000757 sqlite3OsSeek(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)(pPg->pgno-1));
758 rc = sqlite3OsRead(&pPager->fd, zBuf, SQLITE_PAGE_SIZE);
drh9eb9e262004-02-11 02:18:05 +0000759 TRACE2("REFETCH %d\n", pPg->pgno);
760 CODEC(pPager, zBuf, pPg->pgno, 2);
drhdb48ee02003-01-16 13:42:43 +0000761 if( rc ) break;
762 }else{
drhd0ba1932004-02-10 01:54:28 +0000763 memset(zBuf, 0, SQLITE_PAGE_SIZE);
drhdb48ee02003-01-16 13:42:43 +0000764 }
drhd0ba1932004-02-10 01:54:28 +0000765 if( pPg->nRef==0 || memcmp(zBuf, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE) ){
766 memcpy(PGHDR_TO_DATA(pPg), zBuf, SQLITE_PAGE_SIZE);
drh3a840692003-01-29 22:58:26 +0000767 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
768 }
drhdb48ee02003-01-16 13:42:43 +0000769 pPg->needSync = 0;
770 pPg->dirty = 0;
771 }
772 }
drh4a0681e2003-02-13 01:58:20 +0000773
774end_playback:
drhd9b02572001-04-15 00:37:09 +0000775 if( rc!=SQLITE_OK ){
776 pager_unwritelock(pPager);
777 pPager->errMask |= PAGER_ERR_CORRUPT;
778 rc = SQLITE_CORRUPT;
779 }else{
780 rc = pager_unwritelock(pPager);
drhed7c8552001-04-11 14:29:21 +0000781 }
drhd9b02572001-04-15 00:37:09 +0000782 return rc;
drhed7c8552001-04-11 14:29:21 +0000783}
784
785/*
drhac69b052004-05-12 13:30:07 +0000786** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +0000787**
788** This is similar to playing back the transaction journal but with
789** a few extra twists.
790**
drh663fc632002-02-02 18:49:19 +0000791** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +0000792** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +0000793** journal file itself.
drhfa86c412002-02-02 15:01:15 +0000794**
drhac69b052004-05-12 13:30:07 +0000795** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +0000796** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +0000797** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +0000798*/
drh3aac2dd2004-04-26 14:10:20 +0000799static int pager_stmt_playback(Pager *pPager){
drh968af522003-02-11 14:55:40 +0000800 off_t szJ; /* Size of the full journal */
801 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +0000802 int i; /* Loop counter */
803 int rc;
804
805 /* Truncate the database back to its original size.
806 */
drhac69b052004-05-12 13:30:07 +0000807 rc = sqlite3OsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)pPager->stmtSize);
808 pPager->dbSize = pPager->stmtSize;
drhfa86c412002-02-02 15:01:15 +0000809
drhac69b052004-05-12 13:30:07 +0000810 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +0000811 */
drhac69b052004-05-12 13:30:07 +0000812 assert( pPager->stmtInUse && pPager->journalOpen );
813 sqlite3OsSeek(&pPager->stfd, 0);
814 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +0000815
drhac69b052004-05-12 13:30:07 +0000816 /* Copy original pages out of the statement journal and back into the
817 ** database file. Note that the statement journal always uses format
drh968af522003-02-11 14:55:40 +0000818 ** 2 instead of format 3 since it does not need to be concerned with
819 ** power failures corrupting the journal and can thus omit the checksums.
drhfa86c412002-02-02 15:01:15 +0000820 */
821 for(i=nRec-1; i>=0; i--){
drhac69b052004-05-12 13:30:07 +0000822 rc = pager_playback_one_page(pPager, &pPager->stfd, 2);
drh968af522003-02-11 14:55:40 +0000823 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +0000824 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +0000825 }
826
827 /* Figure out how many pages need to be copied out of the transaction
828 ** journal.
829 */
drhac69b052004-05-12 13:30:07 +0000830 rc = sqlite3OsSeek(&pPager->jfd, pPager->stmtJSize);
drhfa86c412002-02-02 15:01:15 +0000831 if( rc!=SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +0000832 goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +0000833 }
danielk19774adee202004-05-08 08:23:19 +0000834 rc = sqlite3OsFileSize(&pPager->jfd, &szJ);
drhfa86c412002-02-02 15:01:15 +0000835 if( rc!=SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +0000836 goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +0000837 }
drhac69b052004-05-12 13:30:07 +0000838 nRec = (szJ - pPager->stmtJSize)/JOURNAL_PG_SZ(journal_format);
drhfa86c412002-02-02 15:01:15 +0000839 for(i=nRec-1; i>=0; i--){
drh968af522003-02-11 14:55:40 +0000840 rc = pager_playback_one_page(pPager, &pPager->jfd, journal_format);
841 if( rc!=SQLITE_OK ){
842 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +0000843 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +0000844 }
drhfa86c412002-02-02 15:01:15 +0000845 }
846
drh3aac2dd2004-04-26 14:10:20 +0000847end_stmt_playback:
drhfa86c412002-02-02 15:01:15 +0000848 if( rc!=SQLITE_OK ){
drhfa86c412002-02-02 15:01:15 +0000849 pPager->errMask |= PAGER_ERR_CORRUPT;
850 rc = SQLITE_CORRUPT;
drhfa86c412002-02-02 15:01:15 +0000851 }
852 return rc;
853}
854
855/*
drhf57b14a2001-09-14 18:54:08 +0000856** Change the maximum number of in-memory pages that are allowed.
drhcd61c282002-03-06 22:01:34 +0000857**
858** The maximum number is the absolute value of the mxPage parameter.
859** If mxPage is negative, the noSync flag is also set. noSync bypasses
danielk19774adee202004-05-08 08:23:19 +0000860** calls to sqlite3OsSync(). The pager runs much faster with noSync on,
drhcd61c282002-03-06 22:01:34 +0000861** but if the operating system crashes or there is an abrupt power
862** failure, the database file might be left in an inconsistent and
863** unrepairable state.
drhf57b14a2001-09-14 18:54:08 +0000864*/
drh3aac2dd2004-04-26 14:10:20 +0000865void sqlite3pager_set_cachesize(Pager *pPager, int mxPage){
drh603240c2002-03-05 01:11:12 +0000866 if( mxPage>=0 ){
drha1680452002-04-18 01:56:57 +0000867 pPager->noSync = pPager->tempFile;
danielk19771d850a72004-05-31 08:26:49 +0000868 if( pPager->noSync ) pPager->needSync = 0;
drh603240c2002-03-05 01:11:12 +0000869 }else{
870 pPager->noSync = 1;
871 mxPage = -mxPage;
872 }
drhf57b14a2001-09-14 18:54:08 +0000873 if( mxPage>10 ){
874 pPager->mxPage = mxPage;
875 }
876}
877
878/*
drh973b6e32003-02-12 14:09:42 +0000879** Adjust the robustness of the database to damage due to OS crashes
880** or power failures by changing the number of syncs()s when writing
881** the rollback journal. There are three levels:
882**
danielk19774adee202004-05-08 08:23:19 +0000883** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +0000884** for temporary and transient files.
885**
886** NORMAL The journal is synced once before writes begin on the
887** database. This is normally adequate protection, but
888** it is theoretically possible, though very unlikely,
889** that an inopertune power failure could leave the journal
890** in a state which would cause damage to the database
891** when it is rolled back.
892**
893** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +0000894** database (with some additional information - the nRec field
895** of the journal header - being written in between the two
896** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +0000897** single disk sector is atomic, then this mode provides
898** assurance that the journal will not be corrupted to the
899** point of causing damage to the database during rollback.
900**
901** Numeric values associated with these states are OFF==1, NORMAL=2,
902** and FULL=3.
903*/
drh3aac2dd2004-04-26 14:10:20 +0000904void sqlite3pager_set_safety_level(Pager *pPager, int level){
drh973b6e32003-02-12 14:09:42 +0000905 pPager->noSync = level==1 || pPager->tempFile;
906 pPager->fullSync = level==3 && !pPager->tempFile;
danielk19771d850a72004-05-31 08:26:49 +0000907 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +0000908}
909
910/*
drhfa86c412002-02-02 15:01:15 +0000911** Open a temporary file. Write the name of the file into zName
912** (zName must be at least SQLITE_TEMPNAME_SIZE bytes long.) Write
913** the file descriptor into *fd. Return SQLITE_OK on success or some
914** other error code if we fail.
915**
916** The OS will automatically delete the temporary file when it is
917** closed.
918*/
drh3aac2dd2004-04-26 14:10:20 +0000919static int sqlite3pager_opentemp(char *zFile, OsFile *fd){
drhfa86c412002-02-02 15:01:15 +0000920 int cnt = 8;
921 int rc;
922 do{
923 cnt--;
danielk19774adee202004-05-08 08:23:19 +0000924 sqlite3OsTempFileName(zFile);
925 rc = sqlite3OsOpenExclusive(zFile, fd, 1);
drhfa86c412002-02-02 15:01:15 +0000926 }while( cnt>0 && rc!=SQLITE_OK );
927 return rc;
928}
929
930/*
drhed7c8552001-04-11 14:29:21 +0000931** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +0000932** The file to be cached need not exist. The file is not locked until
drh3aac2dd2004-04-26 14:10:20 +0000933** the first call to sqlite3pager_get() and is only held open until the
934** last page is released using sqlite3pager_unref().
drh382c0242001-10-06 16:33:02 +0000935**
drh6446c4d2001-12-15 14:22:18 +0000936** If zFilename is NULL then a randomly-named temporary file is created
937** and used as the file to be cached. The file will be deleted
938** automatically when it is closed.
drhed7c8552001-04-11 14:29:21 +0000939*/
drh3aac2dd2004-04-26 14:10:20 +0000940int sqlite3pager_open(
drh7e3b0a02001-04-28 16:52:40 +0000941 Pager **ppPager, /* Return the Pager structure here */
942 const char *zFilename, /* Name of the database file to open */
943 int mxPage, /* Max number of in-memory cache pages */
drhda47d772002-12-02 04:25:19 +0000944 int nExtra, /* Extra bytes append to each in-memory page */
945 int useJournal /* TRUE to use a rollback journal on this file */
drh7e3b0a02001-04-28 16:52:40 +0000946){
drhed7c8552001-04-11 14:29:21 +0000947 Pager *pPager;
drh3e7a6092002-12-07 21:45:14 +0000948 char *zFullPathname;
drhed7c8552001-04-11 14:29:21 +0000949 int nameLen;
drh8cfbf082001-09-19 13:22:39 +0000950 OsFile fd;
drha76c82e2003-07-27 18:59:42 +0000951 int rc, i;
drh5e00f6c2001-09-13 13:46:56 +0000952 int tempFile;
drhac69b052004-05-12 13:30:07 +0000953 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +0000954 int readOnly = 0;
drh8cfbf082001-09-19 13:22:39 +0000955 char zTemp[SQLITE_TEMPNAME_SIZE];
drhed7c8552001-04-11 14:29:21 +0000956
drhd9b02572001-04-15 00:37:09 +0000957 *ppPager = 0;
danielk19776f8a5032004-05-10 10:34:51 +0000958 if( sqlite3_malloc_failed ){
drhd9b02572001-04-15 00:37:09 +0000959 return SQLITE_NOMEM;
960 }
drh901afd42003-08-26 11:25:58 +0000961 if( zFilename && zFilename[0] ){
drhac69b052004-05-12 13:30:07 +0000962 if( strcmp(zFilename,":memory:")==0 ){
963 memDb = 1;
964 zFullPathname = sqliteMalloc(4);
965 if( zFullPathname ) strcpy(zFullPathname, "nil");
966 rc = SQLITE_OK;
967 }else{
968 zFullPathname = sqlite3OsFullPathname(zFilename);
969 rc = sqlite3OsOpenReadWrite(zFullPathname, &fd, &readOnly);
970 tempFile = 0;
971 }
drh5e00f6c2001-09-13 13:46:56 +0000972 }else{
drh3aac2dd2004-04-26 14:10:20 +0000973 rc = sqlite3pager_opentemp(zTemp, &fd);
drh5e00f6c2001-09-13 13:46:56 +0000974 zFilename = zTemp;
danielk19774adee202004-05-08 08:23:19 +0000975 zFullPathname = sqlite3OsFullPathname(zFilename);
drh5e00f6c2001-09-13 13:46:56 +0000976 tempFile = 1;
977 }
danielk19776f8a5032004-05-10 10:34:51 +0000978 if( sqlite3_malloc_failed ){
drh3e7a6092002-12-07 21:45:14 +0000979 return SQLITE_NOMEM;
980 }
drh8cfbf082001-09-19 13:22:39 +0000981 if( rc!=SQLITE_OK ){
drh3e7a6092002-12-07 21:45:14 +0000982 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +0000983 return SQLITE_CANTOPEN;
984 }
drh3e7a6092002-12-07 21:45:14 +0000985 nameLen = strlen(zFullPathname);
drha76c82e2003-07-27 18:59:42 +0000986 pPager = sqliteMalloc( sizeof(*pPager) + nameLen*3 + 30 );
drhd9b02572001-04-15 00:37:09 +0000987 if( pPager==0 ){
danielk19774adee202004-05-08 08:23:19 +0000988 sqlite3OsClose(&fd);
drh3e7a6092002-12-07 21:45:14 +0000989 sqliteFree(zFullPathname);
drhd9b02572001-04-15 00:37:09 +0000990 return SQLITE_NOMEM;
991 }
drhdb48ee02003-01-16 13:42:43 +0000992 SET_PAGER(pPager);
drhed7c8552001-04-11 14:29:21 +0000993 pPager->zFilename = (char*)&pPager[1];
drha76c82e2003-07-27 18:59:42 +0000994 pPager->zDirectory = &pPager->zFilename[nameLen+1];
995 pPager->zJournal = &pPager->zDirectory[nameLen+1];
drh3e7a6092002-12-07 21:45:14 +0000996 strcpy(pPager->zFilename, zFullPathname);
drha76c82e2003-07-27 18:59:42 +0000997 strcpy(pPager->zDirectory, zFullPathname);
998 for(i=nameLen; i>0 && pPager->zDirectory[i-1]!='/'; i--){}
999 if( i>0 ) pPager->zDirectory[i-1] = 0;
drh3e7a6092002-12-07 21:45:14 +00001000 strcpy(pPager->zJournal, zFullPathname);
1001 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +00001002 strcpy(&pPager->zJournal[nameLen], "-journal");
1003 pPager->fd = fd;
drh8cfbf082001-09-19 13:22:39 +00001004 pPager->journalOpen = 0;
drhac69b052004-05-12 13:30:07 +00001005 pPager->useJournal = useJournal && !memDb;
1006 pPager->stmtOpen = 0;
1007 pPager->stmtInUse = 0;
drhed7c8552001-04-11 14:29:21 +00001008 pPager->nRef = 0;
drhac69b052004-05-12 13:30:07 +00001009 pPager->dbSize = memDb-1;
1010 pPager->pageSize = SQLITE_PAGE_SIZE;
1011 pPager->stmtSize = 0;
1012 pPager->stmtJSize = 0;
drhed7c8552001-04-11 14:29:21 +00001013 pPager->nPage = 0;
drhd79caeb2001-04-15 02:27:24 +00001014 pPager->mxPage = mxPage>5 ? mxPage : 10;
drhed7c8552001-04-11 14:29:21 +00001015 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +00001016 pPager->errMask = 0;
drh5e00f6c2001-09-13 13:46:56 +00001017 pPager->tempFile = tempFile;
drhac69b052004-05-12 13:30:07 +00001018 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00001019 pPager->readOnly = readOnly;
drhf57b14a2001-09-14 18:54:08 +00001020 pPager->needSync = 0;
drhda47d772002-12-02 04:25:19 +00001021 pPager->noSync = pPager->tempFile || !useJournal;
drhed7c8552001-04-11 14:29:21 +00001022 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +00001023 pPager->pFirstSynced = 0;
drhed7c8552001-04-11 14:29:21 +00001024 pPager->pLast = 0;
drh7c717f72001-06-24 20:39:41 +00001025 pPager->nExtra = nExtra;
drhed7c8552001-04-11 14:29:21 +00001026 memset(pPager->aHash, 0, sizeof(pPager->aHash));
1027 *ppPager = pPager;
1028 return SQLITE_OK;
1029}
1030
1031/*
drh72f82862001-05-24 21:06:34 +00001032** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00001033** when the reference count on each page reaches zero. The destructor can
1034** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00001035**
drh3aac2dd2004-04-26 14:10:20 +00001036** The destructor is not called as a result sqlite3pager_close().
1037** Destructors are only called by sqlite3pager_unref().
drh72f82862001-05-24 21:06:34 +00001038*/
drhb6f41482004-05-14 01:58:11 +00001039void sqlite3pager_set_destructor(Pager *pPager, void (*xDesc)(void*,int)){
drh72f82862001-05-24 21:06:34 +00001040 pPager->xDestructor = xDesc;
1041}
1042
1043/*
drh5e00f6c2001-09-13 13:46:56 +00001044** Return the total number of pages in the disk file associated with
1045** pPager.
drhed7c8552001-04-11 14:29:21 +00001046*/
drh3aac2dd2004-04-26 14:10:20 +00001047int sqlite3pager_pagecount(Pager *pPager){
drh28be87c2002-11-05 23:03:02 +00001048 off_t n;
drhd9b02572001-04-15 00:37:09 +00001049 assert( pPager!=0 );
drhed7c8552001-04-11 14:29:21 +00001050 if( pPager->dbSize>=0 ){
1051 return pPager->dbSize;
1052 }
danielk19774adee202004-05-08 08:23:19 +00001053 if( sqlite3OsFileSize(&pPager->fd, &n)!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +00001054 pPager->errMask |= PAGER_ERR_DISK;
drh8cfbf082001-09-19 13:22:39 +00001055 return 0;
drhed7c8552001-04-11 14:29:21 +00001056 }
drhd0ba1932004-02-10 01:54:28 +00001057 n /= SQLITE_PAGE_SIZE;
drhd9b02572001-04-15 00:37:09 +00001058 if( pPager->state!=SQLITE_UNLOCK ){
drhed7c8552001-04-11 14:29:21 +00001059 pPager->dbSize = n;
1060 }
1061 return n;
1062}
1063
1064/*
drhf7c57532003-04-25 13:22:51 +00001065** Forward declaration
1066*/
drh34e79ce2004-02-08 06:05:46 +00001067static int syncJournal(Pager*);
drhf7c57532003-04-25 13:22:51 +00001068
drhac69b052004-05-12 13:30:07 +00001069
1070/*
1071** Unlink a page from the free list (the list of all pages where nRef==0)
1072** and from its hash collision chain.
1073*/
1074static void unlinkPage(PgHdr *pPg){
1075 Pager *pPager = pPg->pPager;
1076
1077 /* Keep the pFirstSynced pointer pointing at the first synchronized page */
1078 if( pPg==pPager->pFirstSynced ){
1079 PgHdr *p = pPg->pNextFree;
1080 while( p && p->needSync ){ p = p->pNextFree; }
1081 pPager->pFirstSynced = p;
1082 }
1083
1084 /* Unlink from the freelist */
1085 if( pPg->pPrevFree ){
1086 pPg->pPrevFree->pNextFree = pPg->pNextFree;
1087 }else{
1088 assert( pPager->pFirst==pPg );
1089 pPager->pFirst = pPg->pNextFree;
1090 }
1091 if( pPg->pNextFree ){
1092 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1093 }else{
1094 assert( pPager->pLast==pPg );
1095 pPager->pLast = pPg->pPrevFree;
1096 }
1097 pPg->pNextFree = pPg->pPrevFree = 0;
1098
1099 /* Unlink from the pgno hash table */
1100 if( pPg->pNextHash ){
1101 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
1102 }
1103 if( pPg->pPrevHash ){
1104 pPg->pPrevHash->pNextHash = pPg->pNextHash;
1105 }else{
1106 int h = pager_hash(pPg->pgno);
1107 assert( pPager->aHash[h]==pPg );
1108 pPager->aHash[h] = pPg->pNextHash;
1109 }
1110 pPg->pNextHash = pPg->pPrevHash = 0;
1111}
1112
1113/*
1114** This routine is used to truncate an in-memory database. Delete
1115** every pages whose pgno is larger than pPager->dbSize and is unreferenced.
1116** Referenced pages larger than pPager->dbSize are zeroed.
1117*/
1118static void memoryTruncate(Pager *pPager){
1119 PgHdr *pPg;
1120 PgHdr **ppPg;
1121 int dbSize = pPager->dbSize;
1122
1123 ppPg = &pPager->pAll;
1124 while( (pPg = *ppPg)!=0 ){
1125 if( pPg->pgno<=dbSize ){
1126 ppPg = &pPg->pNextAll;
1127 }else if( pPg->nRef>0 ){
1128 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
1129 ppPg = &pPg->pNextAll;
1130 }else{
1131 *ppPg = pPg->pNextAll;
1132 unlinkPage(pPg);
1133 sqliteFree(pPg);
1134 pPager->nPage--;
1135 }
1136 }
1137}
1138
drhf7c57532003-04-25 13:22:51 +00001139/*
1140** Truncate the file to the number of pages specified.
1141*/
drh3aac2dd2004-04-26 14:10:20 +00001142int sqlite3pager_truncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00001143 int rc;
drh2e6d11b2003-04-25 15:37:57 +00001144 if( pPager->dbSize<0 ){
drh3aac2dd2004-04-26 14:10:20 +00001145 sqlite3pager_pagecount(pPager);
drh2e6d11b2003-04-25 15:37:57 +00001146 }
1147 if( pPager->errMask!=0 ){
1148 rc = pager_errcode(pPager);
1149 return rc;
1150 }
drh7d02cb72003-06-04 16:24:39 +00001151 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00001152 return SQLITE_OK;
1153 }
drhac69b052004-05-12 13:30:07 +00001154 if( pPager->memDb ){
1155 pPager->dbSize = nPage;
1156 memoryTruncate(pPager);
1157 return SQLITE_OK;
1158 }
drh34e79ce2004-02-08 06:05:46 +00001159 syncJournal(pPager);
danielk19774adee202004-05-08 08:23:19 +00001160 rc = sqlite3OsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)nPage);
drhf7c57532003-04-25 13:22:51 +00001161 if( rc==SQLITE_OK ){
1162 pPager->dbSize = nPage;
1163 }
1164 return rc;
1165}
1166
1167/*
drhed7c8552001-04-11 14:29:21 +00001168** Shutdown the page cache. Free all memory and close all files.
1169**
1170** If a transaction was in progress when this routine is called, that
1171** transaction is rolled back. All outstanding pages are invalidated
1172** and their memory is freed. Any attempt to use a page associated
1173** with this page cache after this function returns will likely
1174** result in a coredump.
1175*/
drh3aac2dd2004-04-26 14:10:20 +00001176int sqlite3pager_close(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00001177 PgHdr *pPg, *pNext;
drhed7c8552001-04-11 14:29:21 +00001178 switch( pPager->state ){
1179 case SQLITE_WRITELOCK: {
drh3aac2dd2004-04-26 14:10:20 +00001180 sqlite3pager_rollback(pPager);
drhac69b052004-05-12 13:30:07 +00001181 if( !pPager->memDb ){
1182 sqlite3OsUnlock(&pPager->fd);
1183 }
drh8cfbf082001-09-19 13:22:39 +00001184 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +00001185 break;
1186 }
1187 case SQLITE_READLOCK: {
drhac69b052004-05-12 13:30:07 +00001188 if( !pPager->memDb ){
1189 sqlite3OsUnlock(&pPager->fd);
1190 }
drhed7c8552001-04-11 14:29:21 +00001191 break;
1192 }
1193 default: {
1194 /* Do nothing */
1195 break;
1196 }
1197 }
drhd9b02572001-04-15 00:37:09 +00001198 for(pPg=pPager->pAll; pPg; pPg=pNext){
1199 pNext = pPg->pNextAll;
1200 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +00001201 }
drhac69b052004-05-12 13:30:07 +00001202 if( !pPager->memDb ){
1203 sqlite3OsClose(&pPager->fd);
1204 }
drh8cfbf082001-09-19 13:22:39 +00001205 assert( pPager->journalOpen==0 );
drh0f892532002-05-30 12:27:03 +00001206 /* Temp files are automatically deleted by the OS
1207 ** if( pPager->tempFile ){
danielk19774adee202004-05-08 08:23:19 +00001208 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00001209 ** }
1210 */
drhdb48ee02003-01-16 13:42:43 +00001211 CLR_PAGER(pPager);
drh73509ee2003-04-06 20:44:45 +00001212 if( pPager->zFilename!=(char*)&pPager[1] ){
drha76c82e2003-07-27 18:59:42 +00001213 assert( 0 ); /* Cannot happen */
drh73509ee2003-04-06 20:44:45 +00001214 sqliteFree(pPager->zFilename);
1215 sqliteFree(pPager->zJournal);
drha76c82e2003-07-27 18:59:42 +00001216 sqliteFree(pPager->zDirectory);
drh73509ee2003-04-06 20:44:45 +00001217 }
drhed7c8552001-04-11 14:29:21 +00001218 sqliteFree(pPager);
1219 return SQLITE_OK;
1220}
1221
1222/*
drh5e00f6c2001-09-13 13:46:56 +00001223** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00001224*/
drh3aac2dd2004-04-26 14:10:20 +00001225Pgno sqlite3pager_pagenumber(void *pData){
drhed7c8552001-04-11 14:29:21 +00001226 PgHdr *p = DATA_TO_PGHDR(pData);
1227 return p->pgno;
1228}
1229
1230/*
drhc8629a12004-05-08 20:07:40 +00001231** The page_ref() function increments the reference count for a page.
1232** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00001233** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00001234**
1235** For non-test systems, page_ref() is a macro that calls _page_ref()
1236** online of the reference count is zero. For test systems, page_ref()
1237** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00001238*/
drh836faa42003-01-11 13:30:57 +00001239static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00001240 if( pPg->nRef==0 ){
1241 /* The page is currently on the freelist. Remove it. */
drh341eae82003-01-21 02:39:36 +00001242 if( pPg==pPg->pPager->pFirstSynced ){
1243 PgHdr *p = pPg->pNextFree;
1244 while( p && p->needSync ){ p = p->pNextFree; }
1245 pPg->pPager->pFirstSynced = p;
1246 }
drh7e3b0a02001-04-28 16:52:40 +00001247 if( pPg->pPrevFree ){
1248 pPg->pPrevFree->pNextFree = pPg->pNextFree;
1249 }else{
1250 pPg->pPager->pFirst = pPg->pNextFree;
1251 }
1252 if( pPg->pNextFree ){
1253 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1254 }else{
1255 pPg->pPager->pLast = pPg->pPrevFree;
1256 }
1257 pPg->pPager->nRef++;
1258 }
1259 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +00001260 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +00001261}
drhc8629a12004-05-08 20:07:40 +00001262#ifdef SQLITE_TEST
1263 static void page_ref(PgHdr *pPg){
1264 if( pPg->nRef==0 ){
1265 _page_ref(pPg);
1266 }else{
1267 pPg->nRef++;
1268 REFINFO(pPg);
1269 }
1270 }
1271#else
1272# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
1273#endif
drhdf0b3b02001-06-23 11:36:20 +00001274
1275/*
1276** Increment the reference count for a page. The input pointer is
1277** a reference to the page data.
1278*/
drh3aac2dd2004-04-26 14:10:20 +00001279int sqlite3pager_ref(void *pData){
drhdf0b3b02001-06-23 11:36:20 +00001280 PgHdr *pPg = DATA_TO_PGHDR(pData);
1281 page_ref(pPg);
drh8c42ca92001-06-22 19:15:00 +00001282 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00001283}
1284
1285/*
drh34e79ce2004-02-08 06:05:46 +00001286** Sync the journal. In other words, make sure all the pages that have
1287** been written to the journal have actually reached the surface of the
1288** disk. It is not safe to modify the original database file until after
1289** the journal has been synced. If the original database is modified before
1290** the journal is synced and a power failure occurs, the unsynced journal
1291** data would be lost and we would be unable to completely rollback the
1292** database changes. Database corruption would occur.
1293**
1294** This routine also updates the nRec field in the header of the journal.
1295** (See comments on the pager_playback() routine for additional information.)
1296** If the sync mode is FULL, two syncs will occur. First the whole journal
1297** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00001298**
drh34e79ce2004-02-08 06:05:46 +00001299** For temporary databases, we do not care if we are able to rollback
1300** after a power failure, so sync occurs.
drhfa86c412002-02-02 15:01:15 +00001301**
drh34e79ce2004-02-08 06:05:46 +00001302** This routine clears the needSync field of every page current held in
1303** memory.
drh50e5dad2001-09-15 00:57:28 +00001304*/
drh34e79ce2004-02-08 06:05:46 +00001305static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00001306 PgHdr *pPg;
1307 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00001308
1309 /* Sync the journal before modifying the main database
1310 ** (assuming there is a journal and it needs to be synced.)
1311 */
drh50e5dad2001-09-15 00:57:28 +00001312 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00001313 if( !pPager->tempFile ){
drhdb48ee02003-01-16 13:42:43 +00001314 assert( pPager->journalOpen );
drh946966f2004-02-25 02:20:41 +00001315 /* assert( !pPager->noSync ); // noSync might be set if synchronous
1316 ** was turned off after the transaction was started. Ticket #615 */
drh968af522003-02-11 14:55:40 +00001317#ifndef NDEBUG
1318 {
drh34e79ce2004-02-08 06:05:46 +00001319 /* Make sure the pPager->nRec counter we are keeping agrees
1320 ** with the nRec computed from the size of the journal file.
1321 */
drh4a0681e2003-02-13 01:58:20 +00001322 off_t hdrSz, pgSz, jSz;
drh968af522003-02-11 14:55:40 +00001323 hdrSz = JOURNAL_HDR_SZ(journal_format);
1324 pgSz = JOURNAL_PG_SZ(journal_format);
danielk19774adee202004-05-08 08:23:19 +00001325 rc = sqlite3OsFileSize(&pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00001326 if( rc!=0 ) return rc;
drh4a0681e2003-02-13 01:58:20 +00001327 assert( pPager->nRec*pgSz+hdrSz==jSz );
drh968af522003-02-11 14:55:40 +00001328 }
1329#endif
drhd8d66e82003-02-12 02:10:15 +00001330 if( journal_format>=3 ){
drh34e79ce2004-02-08 06:05:46 +00001331 /* Write the nRec value into the journal file header */
drhd8d66e82003-02-12 02:10:15 +00001332 off_t szJ;
1333 if( pPager->fullSync ){
1334 TRACE1("SYNC\n");
danielk19774adee202004-05-08 08:23:19 +00001335 rc = sqlite3OsSync(&pPager->jfd);
drhd8d66e82003-02-12 02:10:15 +00001336 if( rc!=0 ) return rc;
1337 }
danielk19774adee202004-05-08 08:23:19 +00001338 sqlite3OsSeek(&pPager->jfd, sizeof(aJournalMagic1));
drh99ee3602003-02-16 19:13:36 +00001339 rc = write32bits(&pPager->jfd, pPager->nRec);
1340 if( rc ) return rc;
drhd8d66e82003-02-12 02:10:15 +00001341 szJ = JOURNAL_HDR_SZ(journal_format) +
1342 pPager->nRec*JOURNAL_PG_SZ(journal_format);
danielk19774adee202004-05-08 08:23:19 +00001343 sqlite3OsSeek(&pPager->jfd, szJ);
drh968af522003-02-11 14:55:40 +00001344 }
drhdb48ee02003-01-16 13:42:43 +00001345 TRACE1("SYNC\n");
danielk19774adee202004-05-08 08:23:19 +00001346 rc = sqlite3OsSync(&pPager->jfd);
drhfa86c412002-02-02 15:01:15 +00001347 if( rc!=0 ) return rc;
drhdb48ee02003-01-16 13:42:43 +00001348 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00001349 }
drh50e5dad2001-09-15 00:57:28 +00001350 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00001351
1352 /* Erase the needSync flag from every page.
1353 */
1354 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1355 pPg->needSync = 0;
1356 }
1357 pPager->pFirstSynced = pPager->pFirst;
drh50e5dad2001-09-15 00:57:28 +00001358 }
drh03eb96a2002-11-10 23:32:56 +00001359
drh341eae82003-01-21 02:39:36 +00001360#ifndef NDEBUG
1361 /* If the Pager.needSync flag is clear then the PgHdr.needSync
1362 ** flag must also be clear for all pages. Verify that this
1363 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00001364 */
drh341eae82003-01-21 02:39:36 +00001365 else{
1366 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1367 assert( pPg->needSync==0 );
1368 }
1369 assert( pPager->pFirstSynced==pPager->pFirst );
drh03eb96a2002-11-10 23:32:56 +00001370 }
drh341eae82003-01-21 02:39:36 +00001371#endif
drhdb48ee02003-01-16 13:42:43 +00001372
drh81a20f22001-10-12 17:30:04 +00001373 return rc;
drh50e5dad2001-09-15 00:57:28 +00001374}
1375
1376/*
drh2554f8b2003-01-22 01:26:44 +00001377** Given a list of pages (connected by the PgHdr.pDirty pointer) write
1378** every one of those pages out to the database file and mark them all
1379** as clean.
1380*/
1381static int pager_write_pagelist(PgHdr *pList){
1382 Pager *pPager;
1383 int rc;
1384
1385 if( pList==0 ) return SQLITE_OK;
1386 pPager = pList->pPager;
1387 while( pList ){
1388 assert( pList->dirty );
danielk19774adee202004-05-08 08:23:19 +00001389 sqlite3OsSeek(&pPager->fd, (pList->pgno-1)*(off_t)SQLITE_PAGE_SIZE);
drh9eb9e262004-02-11 02:18:05 +00001390 CODEC(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
1391 TRACE2("STORE %d\n", pList->pgno);
danielk19774adee202004-05-08 08:23:19 +00001392 rc = sqlite3OsWrite(&pPager->fd, PGHDR_TO_DATA(pList), SQLITE_PAGE_SIZE);
drh9eb9e262004-02-11 02:18:05 +00001393 CODEC(pPager, PGHDR_TO_DATA(pList), pList->pgno, 0);
drh2554f8b2003-01-22 01:26:44 +00001394 if( rc ) return rc;
1395 pList->dirty = 0;
1396 pList = pList->pDirty;
1397 }
1398 return SQLITE_OK;
1399}
1400
1401/*
1402** Collect every dirty page into a dirty list and
1403** return a pointer to the head of that list. All pages are
1404** collected even if they are still in use.
1405*/
1406static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
1407 PgHdr *p, *pList;
1408 pList = 0;
1409 for(p=pPager->pAll; p; p=p->pNextAll){
1410 if( p->dirty ){
1411 p->pDirty = pList;
1412 pList = p;
1413 }
1414 }
1415 return pList;
1416}
1417
1418/*
drhd9b02572001-04-15 00:37:09 +00001419** Acquire a page.
1420**
drh58a11682001-11-10 13:51:08 +00001421** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00001422** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00001423**
drh306dc212001-05-21 13:45:10 +00001424** A _get works for any page number greater than 0. If the database
1425** file is smaller than the requested page, then no actual disk
1426** read occurs and the memory image of the page is initialized to
1427** all zeros. The extra data appended to a page is always initialized
1428** to zeros the first time a page is loaded into memory.
1429**
drhd9b02572001-04-15 00:37:09 +00001430** The acquisition might fail for several reasons. In all cases,
1431** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00001432**
drh3aac2dd2004-04-26 14:10:20 +00001433** See also sqlite3pager_lookup(). Both this routine and _lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00001434** to find a page in the in-memory cache first. If the page is not already
drh5e00f6c2001-09-13 13:46:56 +00001435** in memory, this routine goes to disk to read it in whereas _lookup()
drh7e3b0a02001-04-28 16:52:40 +00001436** just returns 0. This routine acquires a read-lock the first time it
1437** has to go to disk, and could also playback an old journal if necessary.
1438** Since _lookup() never goes to disk, it never has to deal with locks
1439** or journal files.
drhed7c8552001-04-11 14:29:21 +00001440*/
drh3aac2dd2004-04-26 14:10:20 +00001441int sqlite3pager_get(Pager *pPager, Pgno pgno, void **ppPage){
drhed7c8552001-04-11 14:29:21 +00001442 PgHdr *pPg;
drh8766c342002-11-09 00:33:15 +00001443 int rc;
drhed7c8552001-04-11 14:29:21 +00001444
drhd9b02572001-04-15 00:37:09 +00001445 /* Make sure we have not hit any critical errors.
1446 */
drh836faa42003-01-11 13:30:57 +00001447 assert( pPager!=0 );
1448 assert( pgno!=0 );
drh2e6d11b2003-04-25 15:37:57 +00001449 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00001450 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1451 return pager_errcode(pPager);
1452 }
1453
drhed7c8552001-04-11 14:29:21 +00001454 /* If this is the first page accessed, then get a read lock
1455 ** on the database file.
1456 */
drhac69b052004-05-12 13:30:07 +00001457 if( pPager->nRef==0 && !pPager->memDb ){
danielk19774adee202004-05-08 08:23:19 +00001458 rc = sqlite3OsReadLock(&pPager->fd);
drh8766c342002-11-09 00:33:15 +00001459 if( rc!=SQLITE_OK ){
drh8766c342002-11-09 00:33:15 +00001460 return rc;
drhed7c8552001-04-11 14:29:21 +00001461 }
drhd9b02572001-04-15 00:37:09 +00001462 pPager->state = SQLITE_READLOCK;
drhed7c8552001-04-11 14:29:21 +00001463
1464 /* If a journal file exists, try to play it back.
1465 */
danielk19774adee202004-05-08 08:23:19 +00001466 if( pPager->useJournal && sqlite3OsFileExists(pPager->zJournal) ){
drhe2227f02003-06-14 11:42:57 +00001467 int rc;
drhed7c8552001-04-11 14:29:21 +00001468
drha7fcb052001-12-14 15:09:55 +00001469 /* Get a write lock on the database
1470 */
danielk19774adee202004-05-08 08:23:19 +00001471 rc = sqlite3OsWriteLock(&pPager->fd);
drha7fcb052001-12-14 15:09:55 +00001472 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00001473 if( sqlite3OsUnlock(&pPager->fd)!=SQLITE_OK ){
drh8766c342002-11-09 00:33:15 +00001474 /* This should never happen! */
1475 rc = SQLITE_INTERNAL;
1476 }
drh8766c342002-11-09 00:33:15 +00001477 return rc;
drha7fcb052001-12-14 15:09:55 +00001478 }
1479 pPager->state = SQLITE_WRITELOCK;
1480
drhe2227f02003-06-14 11:42:57 +00001481 /* Open the journal for reading only. Return SQLITE_BUSY if
1482 ** we are unable to open the journal file.
drhf57b3392001-10-08 13:22:32 +00001483 **
drhe2227f02003-06-14 11:42:57 +00001484 ** The journal file does not need to be locked itself. The
1485 ** journal file is never open unless the main database file holds
1486 ** a write lock, so there is never any chance of two or more
1487 ** processes opening the journal at the same time.
drhed7c8552001-04-11 14:29:21 +00001488 */
danielk19774adee202004-05-08 08:23:19 +00001489 rc = sqlite3OsOpenReadOnly(pPager->zJournal, &pPager->jfd);
drha7fcb052001-12-14 15:09:55 +00001490 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00001491 rc = sqlite3OsUnlock(&pPager->fd);
drha7fcb052001-12-14 15:09:55 +00001492 assert( rc==SQLITE_OK );
drhed7c8552001-04-11 14:29:21 +00001493 return SQLITE_BUSY;
1494 }
drha7fcb052001-12-14 15:09:55 +00001495 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00001496 pPager->journalStarted = 0;
drhed7c8552001-04-11 14:29:21 +00001497
1498 /* Playback and delete the journal. Drop the database write
1499 ** lock and reacquire the read lock.
1500 */
drh99ee3602003-02-16 19:13:36 +00001501 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00001502 if( rc!=SQLITE_OK ){
1503 return rc;
1504 }
drhed7c8552001-04-11 14:29:21 +00001505 }
1506 pPg = 0;
1507 }else{
1508 /* Search for page in cache */
drhd9b02572001-04-15 00:37:09 +00001509 pPg = pager_lookup(pPager, pgno);
drhac69b052004-05-12 13:30:07 +00001510 if( pPager->memDb && pPager->state==SQLITE_UNLOCK ){
1511 pPager->state = SQLITE_READLOCK;
1512 }
drhed7c8552001-04-11 14:29:21 +00001513 }
1514 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00001515 /* The requested page is not in the page cache. */
drhed7c8552001-04-11 14:29:21 +00001516 int h;
drh7e3b0a02001-04-28 16:52:40 +00001517 pPager->nMiss++;
drhac69b052004-05-12 13:30:07 +00001518 if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 || pPager->memDb ){
drhed7c8552001-04-11 14:29:21 +00001519 /* Create a new page */
drhd0ba1932004-02-10 01:54:28 +00001520 pPg = sqliteMallocRaw( sizeof(*pPg) + SQLITE_PAGE_SIZE
drhac69b052004-05-12 13:30:07 +00001521 + sizeof(u32) + pPager->nExtra
1522 + pPager->memDb*sizeof(PgHistory) );
drhd9b02572001-04-15 00:37:09 +00001523 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00001524 pager_unwritelock(pPager);
1525 pPager->errMask |= PAGER_ERR_MEM;
1526 return SQLITE_NOMEM;
1527 }
drh8c1238a2003-01-02 14:43:55 +00001528 memset(pPg, 0, sizeof(*pPg));
drhac69b052004-05-12 13:30:07 +00001529 if( pPager->memDb ){
1530 memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory));
1531 }
drhed7c8552001-04-11 14:29:21 +00001532 pPg->pPager = pPager;
drhd9b02572001-04-15 00:37:09 +00001533 pPg->pNextAll = pPager->pAll;
drhd79caeb2001-04-15 02:27:24 +00001534 pPager->pAll = pPg;
drhd9b02572001-04-15 00:37:09 +00001535 pPager->nPage++;
drhed7c8552001-04-11 14:29:21 +00001536 }else{
drhdb48ee02003-01-16 13:42:43 +00001537 /* Find a page to recycle. Try to locate a page that does not
1538 ** require us to do an fsync() on the journal.
1539 */
drh341eae82003-01-21 02:39:36 +00001540 pPg = pPager->pFirstSynced;
drhb19a2bc2001-09-16 00:13:26 +00001541
drhdb48ee02003-01-16 13:42:43 +00001542 /* If we could not find a page that does not require an fsync()
1543 ** on the journal file then fsync the journal file. This is a
1544 ** very slow operation, so we work hard to avoid it. But sometimes
1545 ** it can't be helped.
drhb19a2bc2001-09-16 00:13:26 +00001546 */
drh603240c2002-03-05 01:11:12 +00001547 if( pPg==0 ){
drh34e79ce2004-02-08 06:05:46 +00001548 int rc = syncJournal(pPager);
drh50e5dad2001-09-15 00:57:28 +00001549 if( rc!=0 ){
drh3aac2dd2004-04-26 14:10:20 +00001550 sqlite3pager_rollback(pPager);
drh50e5dad2001-09-15 00:57:28 +00001551 return SQLITE_IOERR;
1552 }
1553 pPg = pPager->pFirst;
1554 }
drhd9b02572001-04-15 00:37:09 +00001555 assert( pPg->nRef==0 );
drhdb48ee02003-01-16 13:42:43 +00001556
1557 /* Write the page to the database file if it is dirty.
1558 */
1559 if( pPg->dirty ){
1560 assert( pPg->needSync==0 );
drh2554f8b2003-01-22 01:26:44 +00001561 pPg->pDirty = 0;
1562 rc = pager_write_pagelist( pPg );
drhdb48ee02003-01-16 13:42:43 +00001563 if( rc!=SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +00001564 sqlite3pager_rollback(pPager);
drhdb48ee02003-01-16 13:42:43 +00001565 return SQLITE_IOERR;
1566 }
drhdb48ee02003-01-16 13:42:43 +00001567 }
drh50e5dad2001-09-15 00:57:28 +00001568 assert( pPg->dirty==0 );
drhd9b02572001-04-15 00:37:09 +00001569
drhdb48ee02003-01-16 13:42:43 +00001570 /* If the page we are recycling is marked as alwaysRollback, then
drh193a6b42002-07-07 16:52:46 +00001571 ** set the global alwaysRollback flag, thus disabling the
1572 ** sqlite_dont_rollback() optimization for the rest of this transaction.
1573 ** It is necessary to do this because the page marked alwaysRollback
1574 ** might be reloaded at a later time but at that point we won't remember
1575 ** that is was marked alwaysRollback. This means that all pages must
1576 ** be marked as alwaysRollback from here on out.
1577 */
1578 if( pPg->alwaysRollback ){
1579 pPager->alwaysRollback = 1;
1580 }
1581
drhd9b02572001-04-15 00:37:09 +00001582 /* Unlink the old page from the free list and the hash table
1583 */
drhac69b052004-05-12 13:30:07 +00001584 unlinkPage(pPg);
drhd9b02572001-04-15 00:37:09 +00001585 pPager->nOvfl++;
drhed7c8552001-04-11 14:29:21 +00001586 }
1587 pPg->pgno = pgno;
drh1ab43002002-01-14 09:28:19 +00001588 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
danielk19774adee202004-05-08 08:23:19 +00001589 sqlite3CheckMemory(pPager->aInJournal, pgno/8);
drhdb48ee02003-01-16 13:42:43 +00001590 assert( pPager->journalOpen );
drh6019e162001-07-02 17:51:45 +00001591 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drhdb48ee02003-01-16 13:42:43 +00001592 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00001593 }else{
1594 pPg->inJournal = 0;
drhdb48ee02003-01-16 13:42:43 +00001595 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00001596 }
drhac69b052004-05-12 13:30:07 +00001597 if( pPager->aInStmt && (int)pgno<=pPager->stmtSize
1598 && (pPager->aInStmt[pgno/8] & (1<<(pgno&7)))!=0 ){
drh3aac2dd2004-04-26 14:10:20 +00001599 page_add_to_stmt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001600 }else{
drh3aac2dd2004-04-26 14:10:20 +00001601 page_remove_from_stmt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001602 }
drhed7c8552001-04-11 14:29:21 +00001603 pPg->dirty = 0;
1604 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +00001605 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00001606 pPager->nRef++;
1607 h = pager_hash(pgno);
drhed7c8552001-04-11 14:29:21 +00001608 pPg->pNextHash = pPager->aHash[h];
1609 pPager->aHash[h] = pPg;
1610 if( pPg->pNextHash ){
1611 assert( pPg->pNextHash->pPrevHash==0 );
1612 pPg->pNextHash->pPrevHash = pPg;
1613 }
drh2e6d11b2003-04-25 15:37:57 +00001614 if( pPager->nExtra>0 ){
1615 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
1616 }
drh3aac2dd2004-04-26 14:10:20 +00001617 if( pPager->dbSize<0 ) sqlite3pager_pagecount(pPager);
drh2e6d11b2003-04-25 15:37:57 +00001618 if( pPager->errMask!=0 ){
drh3aac2dd2004-04-26 14:10:20 +00001619 sqlite3pager_unref(PGHDR_TO_DATA(pPg));
drh2e6d11b2003-04-25 15:37:57 +00001620 rc = pager_errcode(pPager);
1621 return rc;
1622 }
drh1ab43002002-01-14 09:28:19 +00001623 if( pPager->dbSize<(int)pgno ){
drhd0ba1932004-02-10 01:54:28 +00001624 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
drh306dc212001-05-21 13:45:10 +00001625 }else{
drh81a20f22001-10-12 17:30:04 +00001626 int rc;
drhac69b052004-05-12 13:30:07 +00001627 assert( pPager->memDb==0 );
danielk19774adee202004-05-08 08:23:19 +00001628 sqlite3OsSeek(&pPager->fd, (pgno-1)*(off_t)SQLITE_PAGE_SIZE);
1629 rc = sqlite3OsRead(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
drh9eb9e262004-02-11 02:18:05 +00001630 TRACE2("FETCH %d\n", pPg->pgno);
1631 CODEC(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh81a20f22001-10-12 17:30:04 +00001632 if( rc!=SQLITE_OK ){
drh28be87c2002-11-05 23:03:02 +00001633 off_t fileSize;
danielk19774adee202004-05-08 08:23:19 +00001634 if( sqlite3OsFileSize(&pPager->fd,&fileSize)!=SQLITE_OK
drhd0ba1932004-02-10 01:54:28 +00001635 || fileSize>=pgno*SQLITE_PAGE_SIZE ){
drh3aac2dd2004-04-26 14:10:20 +00001636 sqlite3pager_unref(PGHDR_TO_DATA(pPg));
drh4e371ee2002-09-05 16:08:27 +00001637 return rc;
1638 }else{
drhd0ba1932004-02-10 01:54:28 +00001639 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
drh4e371ee2002-09-05 16:08:27 +00001640 }
drh81a20f22001-10-12 17:30:04 +00001641 }
drh306dc212001-05-21 13:45:10 +00001642 }
drhed7c8552001-04-11 14:29:21 +00001643 }else{
drhd9b02572001-04-15 00:37:09 +00001644 /* The requested page is in the page cache. */
drh7e3b0a02001-04-28 16:52:40 +00001645 pPager->nHit++;
drhdf0b3b02001-06-23 11:36:20 +00001646 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00001647 }
1648 *ppPage = PGHDR_TO_DATA(pPg);
1649 return SQLITE_OK;
1650}
1651
1652/*
drh7e3b0a02001-04-28 16:52:40 +00001653** Acquire a page if it is already in the in-memory cache. Do
1654** not read the page from disk. Return a pointer to the page,
1655** or 0 if the page is not in cache.
1656**
drh3aac2dd2004-04-26 14:10:20 +00001657** See also sqlite3pager_get(). The difference between this routine
1658** and sqlite3pager_get() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00001659** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00001660** returns NULL if the page is not in cache or if a disk I/O error
1661** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00001662*/
drh3aac2dd2004-04-26 14:10:20 +00001663void *sqlite3pager_lookup(Pager *pPager, Pgno pgno){
drh7e3b0a02001-04-28 16:52:40 +00001664 PgHdr *pPg;
1665
drh836faa42003-01-11 13:30:57 +00001666 assert( pPager!=0 );
1667 assert( pgno!=0 );
drh7e3b0a02001-04-28 16:52:40 +00001668 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1669 return 0;
1670 }
drh7e3b0a02001-04-28 16:52:40 +00001671 pPg = pager_lookup(pPager, pgno);
1672 if( pPg==0 ) return 0;
drhdf0b3b02001-06-23 11:36:20 +00001673 page_ref(pPg);
drh7e3b0a02001-04-28 16:52:40 +00001674 return PGHDR_TO_DATA(pPg);
1675}
1676
1677/*
drhed7c8552001-04-11 14:29:21 +00001678** Release a page.
1679**
1680** If the number of references to the page drop to zero, then the
1681** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00001682** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00001683** removed.
1684*/
drh3aac2dd2004-04-26 14:10:20 +00001685int sqlite3pager_unref(void *pData){
drhed7c8552001-04-11 14:29:21 +00001686 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00001687
1688 /* Decrement the reference count for this page
1689 */
drhed7c8552001-04-11 14:29:21 +00001690 pPg = DATA_TO_PGHDR(pData);
1691 assert( pPg->nRef>0 );
drhed7c8552001-04-11 14:29:21 +00001692 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +00001693 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00001694
drh72f82862001-05-24 21:06:34 +00001695 /* When the number of references to a page reach 0, call the
1696 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00001697 */
drhed7c8552001-04-11 14:29:21 +00001698 if( pPg->nRef==0 ){
drh1eaa2692001-09-18 02:02:23 +00001699 Pager *pPager;
1700 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00001701 pPg->pNextFree = 0;
1702 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +00001703 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +00001704 if( pPg->pPrevFree ){
1705 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +00001706 }else{
1707 pPager->pFirst = pPg;
1708 }
drh341eae82003-01-21 02:39:36 +00001709 if( pPg->needSync==0 && pPager->pFirstSynced==0 ){
1710 pPager->pFirstSynced = pPg;
1711 }
drh72f82862001-05-24 21:06:34 +00001712 if( pPager->xDestructor ){
drhb6f41482004-05-14 01:58:11 +00001713 pPager->xDestructor(pData, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00001714 }
drhd9b02572001-04-15 00:37:09 +00001715
1716 /* When all pages reach the freelist, drop the read lock from
1717 ** the database file.
1718 */
1719 pPager->nRef--;
1720 assert( pPager->nRef>=0 );
drhac69b052004-05-12 13:30:07 +00001721 if( pPager->nRef==0 && !pPager->memDb ){
drhd9b02572001-04-15 00:37:09 +00001722 pager_reset(pPager);
1723 }
drhed7c8552001-04-11 14:29:21 +00001724 }
drhd9b02572001-04-15 00:37:09 +00001725 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00001726}
1727
1728/*
drhda47d772002-12-02 04:25:19 +00001729** Create a journal file for pPager. There should already be a write
1730** lock on the database file when this routine is called.
1731**
1732** Return SQLITE_OK if everything. Return an error code and release the
1733** write lock if anything goes wrong.
1734*/
1735static int pager_open_journal(Pager *pPager){
1736 int rc;
1737 assert( pPager->state==SQLITE_WRITELOCK );
1738 assert( pPager->journalOpen==0 );
1739 assert( pPager->useJournal );
drh3aac2dd2004-04-26 14:10:20 +00001740 sqlite3pager_pagecount(pPager);
drhda47d772002-12-02 04:25:19 +00001741 pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 );
1742 if( pPager->aInJournal==0 ){
danielk19774adee202004-05-08 08:23:19 +00001743 sqlite3OsReadLock(&pPager->fd);
drhda47d772002-12-02 04:25:19 +00001744 pPager->state = SQLITE_READLOCK;
1745 return SQLITE_NOMEM;
1746 }
danielk19774adee202004-05-08 08:23:19 +00001747 rc = sqlite3OsOpenExclusive(pPager->zJournal, &pPager->jfd,pPager->tempFile);
drhda47d772002-12-02 04:25:19 +00001748 if( rc!=SQLITE_OK ){
1749 sqliteFree(pPager->aInJournal);
1750 pPager->aInJournal = 0;
danielk19774adee202004-05-08 08:23:19 +00001751 sqlite3OsReadLock(&pPager->fd);
drhda47d772002-12-02 04:25:19 +00001752 pPager->state = SQLITE_READLOCK;
1753 return SQLITE_CANTOPEN;
1754 }
danielk19774adee202004-05-08 08:23:19 +00001755 sqlite3OsOpenDirectory(pPager->zDirectory, &pPager->jfd);
drhda47d772002-12-02 04:25:19 +00001756 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00001757 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00001758 pPager->needSync = 0;
1759 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00001760 pPager->nRec = 0;
drh2e6d11b2003-04-25 15:37:57 +00001761 if( pPager->errMask!=0 ){
1762 rc = pager_errcode(pPager);
1763 return rc;
1764 }
drhda47d772002-12-02 04:25:19 +00001765 pPager->origDbSize = pPager->dbSize;
drh968af522003-02-11 14:55:40 +00001766 if( journal_format==JOURNAL_FORMAT_3 ){
danielk19774adee202004-05-08 08:23:19 +00001767 rc = sqlite3OsWrite(&pPager->jfd, aJournalMagic3, sizeof(aJournalMagic3));
drh968af522003-02-11 14:55:40 +00001768 if( rc==SQLITE_OK ){
drh4303fee2003-02-15 23:09:17 +00001769 rc = write32bits(&pPager->jfd, pPager->noSync ? 0xffffffff : 0);
drh968af522003-02-11 14:55:40 +00001770 }
1771 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00001772 sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh968af522003-02-11 14:55:40 +00001773 rc = write32bits(&pPager->jfd, pPager->cksumInit);
1774 }
1775 }else if( journal_format==JOURNAL_FORMAT_2 ){
danielk19774adee202004-05-08 08:23:19 +00001776 rc = sqlite3OsWrite(&pPager->jfd, aJournalMagic2, sizeof(aJournalMagic2));
drhda47d772002-12-02 04:25:19 +00001777 }else{
drh968af522003-02-11 14:55:40 +00001778 assert( journal_format==JOURNAL_FORMAT_1 );
danielk19774adee202004-05-08 08:23:19 +00001779 rc = sqlite3OsWrite(&pPager->jfd, aJournalMagic1, sizeof(aJournalMagic1));
drhda47d772002-12-02 04:25:19 +00001780 }
1781 if( rc==SQLITE_OK ){
1782 rc = write32bits(&pPager->jfd, pPager->dbSize);
1783 }
drhac69b052004-05-12 13:30:07 +00001784 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +00001785 rc = sqlite3pager_stmt_begin(pPager);
drhda47d772002-12-02 04:25:19 +00001786 }
1787 if( rc!=SQLITE_OK ){
1788 rc = pager_unwritelock(pPager);
1789 if( rc==SQLITE_OK ){
1790 rc = SQLITE_FULL;
1791 }
1792 }
1793 return rc;
1794}
1795
1796/*
drh4b845d72002-03-05 12:41:19 +00001797** Acquire a write-lock on the database. The lock is removed when
1798** the any of the following happen:
1799**
drh3aac2dd2004-04-26 14:10:20 +00001800** * sqlite3pager_commit() is called.
1801** * sqlite3pager_rollback() is called.
1802** * sqlite3pager_close() is called.
1803** * sqlite3pager_unref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00001804**
1805** The parameter to this routine is a pointer to any open page of the
1806** database file. Nothing changes about the page - it is used merely
1807** to acquire a pointer to the Pager structure and as proof that there
1808** is already a read-lock on the database.
1809**
drhda47d772002-12-02 04:25:19 +00001810** A journal file is opened if this is not a temporary file. For
1811** temporary files, the opening of the journal file is deferred until
1812** there is an actual need to write to the journal.
1813**
drh4b845d72002-03-05 12:41:19 +00001814** If the database is already write-locked, this routine is a no-op.
1815*/
drh3aac2dd2004-04-26 14:10:20 +00001816int sqlite3pager_begin(void *pData){
drh4b845d72002-03-05 12:41:19 +00001817 PgHdr *pPg = DATA_TO_PGHDR(pData);
1818 Pager *pPager = pPg->pPager;
1819 int rc = SQLITE_OK;
1820 assert( pPg->nRef>0 );
1821 assert( pPager->state!=SQLITE_UNLOCK );
1822 if( pPager->state==SQLITE_READLOCK ){
1823 assert( pPager->aInJournal==0 );
drhac69b052004-05-12 13:30:07 +00001824 if( pPager->memDb ){
1825 pPager->state = SQLITE_WRITELOCK;
1826 pPager->origDbSize = pPager->dbSize;
1827 }else{
1828 rc = sqlite3OsWriteLock(&pPager->fd);
1829 if( rc!=SQLITE_OK ){
1830 return rc;
1831 }
1832 pPager->state = SQLITE_WRITELOCK;
1833 pPager->dirtyFile = 0;
1834 TRACE1("TRANSACTION\n");
1835 if( pPager->useJournal && !pPager->tempFile ){
1836 rc = pager_open_journal(pPager);
1837 }
drh4b845d72002-03-05 12:41:19 +00001838 }
1839 }
1840 return rc;
1841}
1842
1843/*
drhed7c8552001-04-11 14:29:21 +00001844** Mark a data page as writeable. The page is written into the journal
1845** if it is not there already. This routine must be called before making
1846** changes to a page.
1847**
1848** The first time this routine is called, the pager creates a new
1849** journal and acquires a write lock on the database. If the write
1850** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00001851** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00001852** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00001853**
1854** If the journal file could not be written because the disk is full,
1855** then this routine returns SQLITE_FULL and does an immediate rollback.
1856** All subsequent write attempts also return SQLITE_FULL until there
drh3aac2dd2004-04-26 14:10:20 +00001857** is a call to sqlite3pager_commit() or sqlite3pager_rollback() to
drhd9b02572001-04-15 00:37:09 +00001858** reset.
drhed7c8552001-04-11 14:29:21 +00001859*/
drh3aac2dd2004-04-26 14:10:20 +00001860int sqlite3pager_write(void *pData){
drh69688d52001-04-14 16:38:23 +00001861 PgHdr *pPg = DATA_TO_PGHDR(pData);
1862 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00001863 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00001864
drh6446c4d2001-12-15 14:22:18 +00001865 /* Check for errors
1866 */
drhd9b02572001-04-15 00:37:09 +00001867 if( pPager->errMask ){
1868 return pager_errcode(pPager);
1869 }
drh5e00f6c2001-09-13 13:46:56 +00001870 if( pPager->readOnly ){
1871 return SQLITE_PERM;
1872 }
drh6446c4d2001-12-15 14:22:18 +00001873
1874 /* Mark the page as dirty. If the page has already been written
1875 ** to the journal then we can return right away.
1876 */
drhd9b02572001-04-15 00:37:09 +00001877 pPg->dirty = 1;
drhac69b052004-05-12 13:30:07 +00001878 if( pPg->inJournal && (pPg->inStmt || pPager->stmtInUse==0) ){
drha1680452002-04-18 01:56:57 +00001879 pPager->dirtyFile = 1;
drhfa86c412002-02-02 15:01:15 +00001880 return SQLITE_OK;
1881 }
drh6446c4d2001-12-15 14:22:18 +00001882
1883 /* If we get this far, it means that the page needs to be
drhfa86c412002-02-02 15:01:15 +00001884 ** written to the transaction journal or the ckeckpoint journal
1885 ** or both.
1886 **
1887 ** First check to see that the transaction journal exists and
1888 ** create it if it does not.
drh6446c4d2001-12-15 14:22:18 +00001889 */
drhd9b02572001-04-15 00:37:09 +00001890 assert( pPager->state!=SQLITE_UNLOCK );
drh3aac2dd2004-04-26 14:10:20 +00001891 rc = sqlite3pager_begin(pData);
drhda47d772002-12-02 04:25:19 +00001892 if( rc!=SQLITE_OK ){
1893 return rc;
1894 }
drhd9b02572001-04-15 00:37:09 +00001895 assert( pPager->state==SQLITE_WRITELOCK );
drhda47d772002-12-02 04:25:19 +00001896 if( !pPager->journalOpen && pPager->useJournal ){
1897 rc = pager_open_journal(pPager);
1898 if( rc!=SQLITE_OK ) return rc;
1899 }
1900 assert( pPager->journalOpen || !pPager->useJournal );
1901 pPager->dirtyFile = 1;
drh6446c4d2001-12-15 14:22:18 +00001902
drhfa86c412002-02-02 15:01:15 +00001903 /* The transaction journal now exists and we have a write lock on the
1904 ** main database file. Write the current page to the transaction
1905 ** journal if it is not there already.
drh6446c4d2001-12-15 14:22:18 +00001906 */
drhac69b052004-05-12 13:30:07 +00001907 if( !pPg->inJournal && (pPager->useJournal || pPager->memDb) ){
drhdb48ee02003-01-16 13:42:43 +00001908 if( (int)pPg->pgno <= pPager->origDbSize ){
drh968af522003-02-11 14:55:40 +00001909 int szPg;
1910 u32 saved;
drhac69b052004-05-12 13:30:07 +00001911 if( pPager->memDb ){
1912 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1913 TRACE2("JOURNAL %d\n", pPg->pgno);
1914 assert( pHist->pOrig==0 );
1915 pHist->pOrig = sqliteMallocRaw( pPager->pageSize );
1916 if( pHist->pOrig ){
1917 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
1918 }
1919 pPg->inJournal = 1;
1920 }else {
1921 if( journal_format>=JOURNAL_FORMAT_3 ){
1922 u32 cksum = pager_cksum(pPager, pPg->pgno, pData);
1923 saved = *(u32*)PGHDR_TO_EXTRA(pPg);
1924 store32bits(cksum, pPg, SQLITE_PAGE_SIZE);
1925 szPg = SQLITE_PAGE_SIZE+8;
1926 }else{
1927 szPg = SQLITE_PAGE_SIZE+4;
1928 }
1929 store32bits(pPg->pgno, pPg, -4);
1930 CODEC(pPager, pData, pPg->pgno, 7);
1931 rc = sqlite3OsWrite(&pPager->jfd, &((char*)pData)[-4], szPg);
1932 TRACE3("JOURNAL %d %d\n", pPg->pgno, pPg->needSync);
1933 CODEC(pPager, pData, pPg->pgno, 0);
1934 if( journal_format>=JOURNAL_FORMAT_3 ){
1935 *(u32*)PGHDR_TO_EXTRA(pPg) = saved;
1936 }
1937 if( rc!=SQLITE_OK ){
1938 sqlite3pager_rollback(pPager);
1939 pPager->errMask |= PAGER_ERR_FULL;
1940 return rc;
1941 }
1942 pPager->nRec++;
1943 assert( pPager->aInJournal!=0 );
1944 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1945 pPg->needSync = !pPager->noSync;
1946 pPg->inJournal = 1;
1947 if( pPager->stmtInUse ){
1948 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1949 page_add_to_stmt_list(pPg);
1950 }
drhdb48ee02003-01-16 13:42:43 +00001951 }
drhdb48ee02003-01-16 13:42:43 +00001952 }else{
1953 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
1954 TRACE3("APPEND %d %d\n", pPg->pgno, pPg->needSync);
drhd9b02572001-04-15 00:37:09 +00001955 }
drhdb48ee02003-01-16 13:42:43 +00001956 if( pPg->needSync ){
1957 pPager->needSync = 1;
drhfa86c412002-02-02 15:01:15 +00001958 }
drh69688d52001-04-14 16:38:23 +00001959 }
drh6446c4d2001-12-15 14:22:18 +00001960
drhac69b052004-05-12 13:30:07 +00001961 /* If the statement journal is open and the page is not in it,
1962 ** then write the current page to the statement journal. Note that
1963 ** the statement journal always uses the simplier format 2 that lacks
1964 ** checksums. The header is also omitted from the statement journal.
drh6446c4d2001-12-15 14:22:18 +00001965 */
drhac69b052004-05-12 13:30:07 +00001966 if( pPager->stmtInUse && !pPg->inStmt && (int)pPg->pgno<=pPager->stmtSize ){
drh1e336b42002-02-14 12:50:33 +00001967 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drhac69b052004-05-12 13:30:07 +00001968 if( pPager->memDb ){
1969 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1970 assert( pHist->pStmt==0 );
1971 pHist->pStmt = sqliteMallocRaw( pPager->pageSize );
1972 if( pHist->pStmt ){
1973 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
1974 }
1975 TRACE2("STMT-JOURNAL %d\n", pPg->pgno);
1976 }else{
1977 store32bits(pPg->pgno, pPg, -4);
1978 CODEC(pPager, pData, pPg->pgno, 7);
1979 rc = sqlite3OsWrite(&pPager->stfd, ((char*)pData)-4, SQLITE_PAGE_SIZE+4);
1980 TRACE2("STMT-JOURNAL %d\n", pPg->pgno);
1981 CODEC(pPager, pData, pPg->pgno, 0);
1982 if( rc!=SQLITE_OK ){
1983 sqlite3pager_rollback(pPager);
1984 pPager->errMask |= PAGER_ERR_FULL;
1985 return rc;
1986 }
1987 pPager->stmtNRec++;
1988 assert( pPager->aInStmt!=0 );
1989 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drhfa86c412002-02-02 15:01:15 +00001990 }
drh3aac2dd2004-04-26 14:10:20 +00001991 page_add_to_stmt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001992 }
1993
1994 /* Update the database size and return.
1995 */
drh1ab43002002-01-14 09:28:19 +00001996 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00001997 pPager->dbSize = pPg->pgno;
1998 }
drh69688d52001-04-14 16:38:23 +00001999 return rc;
drhed7c8552001-04-11 14:29:21 +00002000}
2001
2002/*
drhaacc5432002-01-06 17:07:40 +00002003** Return TRUE if the page given in the argument was previously passed
drh3aac2dd2004-04-26 14:10:20 +00002004** to sqlite3pager_write(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00002005** to change the content of the page.
2006*/
drh3aac2dd2004-04-26 14:10:20 +00002007int sqlite3pager_iswriteable(void *pData){
drh6019e162001-07-02 17:51:45 +00002008 PgHdr *pPg = DATA_TO_PGHDR(pData);
2009 return pPg->dirty;
2010}
2011
2012/*
drh001bbcb2003-03-19 03:14:00 +00002013** Replace the content of a single page with the information in the third
2014** argument.
2015*/
drh3aac2dd2004-04-26 14:10:20 +00002016int sqlite3pager_overwrite(Pager *pPager, Pgno pgno, void *pData){
drh001bbcb2003-03-19 03:14:00 +00002017 void *pPage;
2018 int rc;
2019
drh3aac2dd2004-04-26 14:10:20 +00002020 rc = sqlite3pager_get(pPager, pgno, &pPage);
drh001bbcb2003-03-19 03:14:00 +00002021 if( rc==SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +00002022 rc = sqlite3pager_write(pPage);
drh001bbcb2003-03-19 03:14:00 +00002023 if( rc==SQLITE_OK ){
drhd0ba1932004-02-10 01:54:28 +00002024 memcpy(pPage, pData, SQLITE_PAGE_SIZE);
drh001bbcb2003-03-19 03:14:00 +00002025 }
drh3aac2dd2004-04-26 14:10:20 +00002026 sqlite3pager_unref(pPage);
drh001bbcb2003-03-19 03:14:00 +00002027 }
2028 return rc;
2029}
2030
2031/*
drh30e58752002-03-02 20:41:57 +00002032** A call to this routine tells the pager that it is not necessary to
2033** write the information on page "pgno" back to the disk, even though
2034** that page might be marked as dirty.
2035**
2036** The overlying software layer calls this routine when all of the data
2037** on the given page is unused. The pager marks the page as clean so
2038** that it does not get written to disk.
2039**
2040** Tests show that this optimization, together with the
drh3aac2dd2004-04-26 14:10:20 +00002041** sqlite3pager_dont_rollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00002042** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00002043**
2044** When this routine is called, set the alwaysRollback flag to true.
drh3aac2dd2004-04-26 14:10:20 +00002045** Subsequent calls to sqlite3pager_dont_rollback() for the same page
drh8e298f92002-07-06 16:28:47 +00002046** will thereafter be ignored. This is necessary to avoid a problem
2047** where a page with data is added to the freelist during one part of
2048** a transaction then removed from the freelist during a later part
2049** of the same transaction and reused for some other purpose. When it
2050** is first added to the freelist, this routine is called. When reused,
2051** the dont_rollback() routine is called. But because the page contains
2052** critical data, we still need to be sure it gets rolled back in spite
2053** of the dont_rollback() call.
drh30e58752002-03-02 20:41:57 +00002054*/
drh3aac2dd2004-04-26 14:10:20 +00002055void sqlite3pager_dont_write(Pager *pPager, Pgno pgno){
drh30e58752002-03-02 20:41:57 +00002056 PgHdr *pPg;
drh8e298f92002-07-06 16:28:47 +00002057
drh30e58752002-03-02 20:41:57 +00002058 pPg = pager_lookup(pPager, pgno);
drh8e298f92002-07-06 16:28:47 +00002059 pPg->alwaysRollback = 1;
drh30e58752002-03-02 20:41:57 +00002060 if( pPg && pPg->dirty ){
drh8124a302002-06-25 14:43:57 +00002061 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
2062 /* If this pages is the last page in the file and the file has grown
2063 ** during the current transaction, then do NOT mark the page as clean.
2064 ** When the database file grows, we must make sure that the last page
2065 ** gets written at least once so that the disk file will be the correct
2066 ** size. If you do not write this page and the size of the file
2067 ** on the disk ends up being too small, that can lead to database
2068 ** corruption during the next transaction.
2069 */
2070 }else{
drhdb48ee02003-01-16 13:42:43 +00002071 TRACE2("DONT_WRITE %d\n", pgno);
drh8124a302002-06-25 14:43:57 +00002072 pPg->dirty = 0;
2073 }
drh30e58752002-03-02 20:41:57 +00002074 }
2075}
2076
2077/*
2078** A call to this routine tells the pager that if a rollback occurs,
2079** it is not necessary to restore the data on the given page. This
2080** means that the pager does not have to record the given page in the
2081** rollback journal.
2082*/
drh3aac2dd2004-04-26 14:10:20 +00002083void sqlite3pager_dont_rollback(void *pData){
drh30e58752002-03-02 20:41:57 +00002084 PgHdr *pPg = DATA_TO_PGHDR(pData);
2085 Pager *pPager = pPg->pPager;
2086
2087 if( pPager->state!=SQLITE_WRITELOCK || pPager->journalOpen==0 ) return;
drhac69b052004-05-12 13:30:07 +00002088 if( pPg->alwaysRollback || pPager->alwaysRollback || pPager->memDb ) return;
drh30e58752002-03-02 20:41:57 +00002089 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
2090 assert( pPager->aInJournal!=0 );
2091 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
2092 pPg->inJournal = 1;
drhac69b052004-05-12 13:30:07 +00002093 if( pPager->stmtInUse ){
2094 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh3aac2dd2004-04-26 14:10:20 +00002095 page_add_to_stmt_list(pPg);
drh30e58752002-03-02 20:41:57 +00002096 }
drhdb48ee02003-01-16 13:42:43 +00002097 TRACE2("DONT_ROLLBACK %d\n", pPg->pgno);
drh30e58752002-03-02 20:41:57 +00002098 }
drhac69b052004-05-12 13:30:07 +00002099 if( pPager->stmtInUse && !pPg->inStmt && (int)pPg->pgno<=pPager->stmtSize ){
drh30e58752002-03-02 20:41:57 +00002100 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drhac69b052004-05-12 13:30:07 +00002101 assert( pPager->aInStmt!=0 );
2102 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh3aac2dd2004-04-26 14:10:20 +00002103 page_add_to_stmt_list(pPg);
drh30e58752002-03-02 20:41:57 +00002104 }
2105}
2106
drhac69b052004-05-12 13:30:07 +00002107
2108/*
2109** Clear a PgHistory block
2110*/
2111static void clearHistory(PgHistory *pHist){
2112 sqliteFree(pHist->pOrig);
2113 sqliteFree(pHist->pStmt);
2114 pHist->pOrig = 0;
2115 pHist->pStmt = 0;
2116}
2117
drh30e58752002-03-02 20:41:57 +00002118/*
drhed7c8552001-04-11 14:29:21 +00002119** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00002120**
2121** If the commit fails for any reason, a rollback attempt is made
2122** and an error code is returned. If the commit worked, SQLITE_OK
2123** is returned.
drhed7c8552001-04-11 14:29:21 +00002124*/
drh3aac2dd2004-04-26 14:10:20 +00002125int sqlite3pager_commit(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00002126 int rc;
drhed7c8552001-04-11 14:29:21 +00002127 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00002128
2129 if( pPager->errMask==PAGER_ERR_FULL ){
drh3aac2dd2004-04-26 14:10:20 +00002130 rc = sqlite3pager_rollback(pPager);
drh4e371ee2002-09-05 16:08:27 +00002131 if( rc==SQLITE_OK ){
2132 rc = SQLITE_FULL;
2133 }
drhd9b02572001-04-15 00:37:09 +00002134 return rc;
2135 }
2136 if( pPager->errMask!=0 ){
2137 rc = pager_errcode(pPager);
2138 return rc;
2139 }
2140 if( pPager->state!=SQLITE_WRITELOCK ){
2141 return SQLITE_ERROR;
2142 }
drhdb48ee02003-01-16 13:42:43 +00002143 TRACE1("COMMIT\n");
drhac69b052004-05-12 13:30:07 +00002144 if( pPager->memDb ){
2145 pPg = pager_get_all_dirty_pages(pPager);
2146 while( pPg ){
2147 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2148 pPg->dirty = 0;
2149 pPg->inJournal = 0;
2150 pPg->inStmt = 0;
2151 pPg->pPrevStmt = pPg->pNextStmt = 0;
2152 pPg = pPg->pDirty;
2153 }
2154 pPager->pStmt = 0;
2155 pPager->state = SQLITE_READLOCK;
2156 return SQLITE_OK;
2157 }
drha1680452002-04-18 01:56:57 +00002158 if( pPager->dirtyFile==0 ){
danielk19774adee202004-05-08 08:23:19 +00002159 /* Exit early (without doing the time-consuming sqlite3OsSync() calls)
drha1680452002-04-18 01:56:57 +00002160 ** if there have been no changes to the database file. */
drh341eae82003-01-21 02:39:36 +00002161 assert( pPager->needSync==0 );
drha1680452002-04-18 01:56:57 +00002162 rc = pager_unwritelock(pPager);
2163 pPager->dbSize = -1;
2164 return rc;
2165 }
drhda47d772002-12-02 04:25:19 +00002166 assert( pPager->journalOpen );
drh34e79ce2004-02-08 06:05:46 +00002167 rc = syncJournal(pPager);
drh240c5792004-02-08 00:40:52 +00002168 if( rc!=SQLITE_OK ){
drhd9b02572001-04-15 00:37:09 +00002169 goto commit_abort;
drhed7c8552001-04-11 14:29:21 +00002170 }
drh2554f8b2003-01-22 01:26:44 +00002171 pPg = pager_get_all_dirty_pages(pPager);
2172 if( pPg ){
2173 rc = pager_write_pagelist(pPg);
danielk19774adee202004-05-08 08:23:19 +00002174 if( rc || (!pPager->noSync && sqlite3OsSync(&pPager->fd)!=SQLITE_OK) ){
drh2554f8b2003-01-22 01:26:44 +00002175 goto commit_abort;
2176 }
drh603240c2002-03-05 01:11:12 +00002177 }
drhd9b02572001-04-15 00:37:09 +00002178 rc = pager_unwritelock(pPager);
2179 pPager->dbSize = -1;
2180 return rc;
2181
2182 /* Jump here if anything goes wrong during the commit process.
2183 */
2184commit_abort:
drh3aac2dd2004-04-26 14:10:20 +00002185 rc = sqlite3pager_rollback(pPager);
drhd9b02572001-04-15 00:37:09 +00002186 if( rc==SQLITE_OK ){
2187 rc = SQLITE_FULL;
drhed7c8552001-04-11 14:29:21 +00002188 }
drhed7c8552001-04-11 14:29:21 +00002189 return rc;
2190}
2191
2192/*
2193** Rollback all changes. The database falls back to read-only mode.
2194** All in-memory cache pages revert to their original data contents.
2195** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00002196**
2197** This routine cannot fail unless some other process is not following
2198** the correct locking protocol (SQLITE_PROTOCOL) or unless some other
2199** process is writing trash into the journal file (SQLITE_CORRUPT) or
2200** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
2201** codes are returned for all these occasions. Otherwise,
2202** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00002203*/
drh3aac2dd2004-04-26 14:10:20 +00002204int sqlite3pager_rollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00002205 int rc;
drhdb48ee02003-01-16 13:42:43 +00002206 TRACE1("ROLLBACK\n");
drhac69b052004-05-12 13:30:07 +00002207 if( pPager->memDb ){
2208 PgHdr *p;
2209 for(p=pPager->pAll; p; p=p->pNextAll){
2210 PgHistory *pHist;
2211 if( !p->dirty ) continue;
2212 pHist = PGHDR_TO_HIST(p, pPager);
2213 if( pHist->pOrig ){
2214 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
2215 TRACE2("ROLLBACK-PAGE %d\n", p->pgno);
2216 }else{
2217 TRACE2("PAGE %d is clean\n", p->pgno);
2218 }
2219 clearHistory(pHist);
2220 p->dirty = 0;
2221 p->inJournal = 0;
2222 p->inStmt = 0;
2223 p->pPrevStmt = p->pNextStmt = 0;
2224 }
2225 pPager->pStmt = 0;
2226 pPager->dbSize = pPager->origDbSize;
2227 memoryTruncate(pPager);
2228 pPager->stmtInUse = 0;
2229 pPager->state = SQLITE_READLOCK;
2230 return SQLITE_OK;
2231 }
2232
drhda47d772002-12-02 04:25:19 +00002233 if( !pPager->dirtyFile || !pPager->journalOpen ){
2234 rc = pager_unwritelock(pPager);
2235 pPager->dbSize = -1;
2236 return rc;
2237 }
drhdb48ee02003-01-16 13:42:43 +00002238
drhd9b02572001-04-15 00:37:09 +00002239 if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){
drh4b845d72002-03-05 12:41:19 +00002240 if( pPager->state>=SQLITE_WRITELOCK ){
drh99ee3602003-02-16 19:13:36 +00002241 pager_playback(pPager, 1);
drh4b845d72002-03-05 12:41:19 +00002242 }
drhd9b02572001-04-15 00:37:09 +00002243 return pager_errcode(pPager);
drhed7c8552001-04-11 14:29:21 +00002244 }
drhd9b02572001-04-15 00:37:09 +00002245 if( pPager->state!=SQLITE_WRITELOCK ){
2246 return SQLITE_OK;
2247 }
drh99ee3602003-02-16 19:13:36 +00002248 rc = pager_playback(pPager, 1);
drhd9b02572001-04-15 00:37:09 +00002249 if( rc!=SQLITE_OK ){
2250 rc = SQLITE_CORRUPT;
2251 pPager->errMask |= PAGER_ERR_CORRUPT;
2252 }
2253 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +00002254 return rc;
drh98808ba2001-10-18 12:34:46 +00002255}
drhd9b02572001-04-15 00:37:09 +00002256
2257/*
drh5e00f6c2001-09-13 13:46:56 +00002258** Return TRUE if the database file is opened read-only. Return FALSE
2259** if the database is (in theory) writable.
2260*/
drh3aac2dd2004-04-26 14:10:20 +00002261int sqlite3pager_isreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00002262 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00002263}
2264
2265/*
drhd9b02572001-04-15 00:37:09 +00002266** This routine is used for testing and analysis only.
2267*/
drh3aac2dd2004-04-26 14:10:20 +00002268int *sqlite3pager_stats(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00002269 static int a[9];
2270 a[0] = pPager->nRef;
2271 a[1] = pPager->nPage;
2272 a[2] = pPager->mxPage;
2273 a[3] = pPager->dbSize;
2274 a[4] = pPager->state;
2275 a[5] = pPager->errMask;
2276 a[6] = pPager->nHit;
2277 a[7] = pPager->nMiss;
2278 a[8] = pPager->nOvfl;
2279 return a;
2280}
drhdd793422001-06-28 01:54:48 +00002281
drhfa86c412002-02-02 15:01:15 +00002282/*
drhac69b052004-05-12 13:30:07 +00002283** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00002284**
2285** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00002286** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00002287** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00002288*/
drh3aac2dd2004-04-26 14:10:20 +00002289int sqlite3pager_stmt_begin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00002290 int rc;
2291 char zTemp[SQLITE_TEMPNAME_SIZE];
drhac69b052004-05-12 13:30:07 +00002292 assert( !pPager->stmtInUse );
2293 TRACE1("STMT-BEGIN\n");
2294 if( pPager->memDb ){
2295 pPager->stmtInUse = 1;
2296 pPager->stmtSize = pPager->dbSize;
2297 return SQLITE_OK;
2298 }
drhda47d772002-12-02 04:25:19 +00002299 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00002300 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00002301 return SQLITE_OK;
2302 }
drhfa86c412002-02-02 15:01:15 +00002303 assert( pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00002304 pPager->aInStmt = sqliteMalloc( pPager->dbSize/8 + 1 );
2305 if( pPager->aInStmt==0 ){
danielk19774adee202004-05-08 08:23:19 +00002306 sqlite3OsReadLock(&pPager->fd);
drhfa86c412002-02-02 15:01:15 +00002307 return SQLITE_NOMEM;
2308 }
drh968af522003-02-11 14:55:40 +00002309#ifndef NDEBUG
drhac69b052004-05-12 13:30:07 +00002310 rc = sqlite3OsFileSize(&pPager->jfd, &pPager->stmtJSize);
2311 if( rc ) goto stmt_begin_failed;
2312 assert( pPager->stmtJSize ==
drh968af522003-02-11 14:55:40 +00002313 pPager->nRec*JOURNAL_PG_SZ(journal_format)+JOURNAL_HDR_SZ(journal_format) );
2314#endif
drhac69b052004-05-12 13:30:07 +00002315 pPager->stmtJSize = pPager->nRec*JOURNAL_PG_SZ(journal_format)
drh968af522003-02-11 14:55:40 +00002316 + JOURNAL_HDR_SZ(journal_format);
drhac69b052004-05-12 13:30:07 +00002317 pPager->stmtSize = pPager->dbSize;
2318 if( !pPager->stmtOpen ){
2319 rc = sqlite3pager_opentemp(zTemp, &pPager->stfd);
2320 if( rc ) goto stmt_begin_failed;
2321 pPager->stmtOpen = 1;
2322 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00002323 }
drhac69b052004-05-12 13:30:07 +00002324 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00002325 return SQLITE_OK;
2326
drhac69b052004-05-12 13:30:07 +00002327stmt_begin_failed:
2328 if( pPager->aInStmt ){
2329 sqliteFree(pPager->aInStmt);
2330 pPager->aInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00002331 }
2332 return rc;
2333}
2334
2335/*
drhac69b052004-05-12 13:30:07 +00002336** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00002337*/
drh3aac2dd2004-04-26 14:10:20 +00002338int sqlite3pager_stmt_commit(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002339 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00002340 PgHdr *pPg, *pNext;
drhac69b052004-05-12 13:30:07 +00002341 TRACE1("STMT-COMMIT\n");
2342 if( !pPager->memDb ){
2343 sqlite3OsSeek(&pPager->stfd, 0);
2344 /* sqlite3OsTruncate(&pPager->stfd, 0); */
2345 sqliteFree( pPager->aInStmt );
2346 pPager->aInStmt = 0;
drh663fc632002-02-02 18:49:19 +00002347 }
drhac69b052004-05-12 13:30:07 +00002348 for(pPg=pPager->pStmt; pPg; pPg=pNext){
2349 pNext = pPg->pNextStmt;
2350 assert( pPg->inStmt );
2351 pPg->inStmt = 0;
2352 pPg->pPrevStmt = pPg->pNextStmt = 0;
2353 if( pPager->memDb ){
2354 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
2355 sqliteFree(pHist->pStmt);
2356 pHist->pStmt = 0;
2357 }
2358 }
2359 pPager->stmtNRec = 0;
2360 pPager->stmtInUse = 0;
2361 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00002362 }
drhac69b052004-05-12 13:30:07 +00002363 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00002364 return SQLITE_OK;
2365}
2366
2367/*
drhac69b052004-05-12 13:30:07 +00002368** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00002369*/
drh3aac2dd2004-04-26 14:10:20 +00002370int sqlite3pager_stmt_rollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00002371 int rc;
drhac69b052004-05-12 13:30:07 +00002372 if( pPager->stmtInUse ){
2373 TRACE1("STMT-ROLLBACK\n");
2374 if( pPager->memDb ){
2375 PgHdr *pPg;
2376 for(pPg=pPager->pStmt; pPg; pPg=pPg->pNextStmt){
2377 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
2378 if( pHist->pStmt ){
2379 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
2380 sqliteFree(pHist->pStmt);
2381 pHist->pStmt = 0;
2382 }
2383 }
2384 pPager->dbSize = pPager->stmtSize;
2385 memoryTruncate(pPager);
2386 rc = SQLITE_OK;
2387 }else{
2388 rc = pager_stmt_playback(pPager);
2389 }
drh3aac2dd2004-04-26 14:10:20 +00002390 sqlite3pager_stmt_commit(pPager);
drh663fc632002-02-02 18:49:19 +00002391 }else{
2392 rc = SQLITE_OK;
2393 }
drhac69b052004-05-12 13:30:07 +00002394 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00002395 return rc;
2396}
2397
drh73509ee2003-04-06 20:44:45 +00002398/*
2399** Return the full pathname of the database file.
2400*/
drh3aac2dd2004-04-26 14:10:20 +00002401const char *sqlite3pager_filename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00002402 return pPager->zFilename;
2403}
2404
drhb20ea9d2004-02-09 01:20:36 +00002405/*
2406** Set the codec for this pager
2407*/
drh3aac2dd2004-04-26 14:10:20 +00002408void sqlite3pager_set_codec(
drhb20ea9d2004-02-09 01:20:36 +00002409 Pager *pPager,
drh9eb9e262004-02-11 02:18:05 +00002410 void (*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00002411 void *pCodecArg
2412){
2413 pPager->xCodec = xCodec;
2414 pPager->pCodecArg = pCodecArg;
2415}
2416
drh74587e52002-08-13 00:01:16 +00002417#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00002418/*
2419** Print a listing of all referenced pages and their ref count.
2420*/
drh3aac2dd2004-04-26 14:10:20 +00002421void sqlite3pager_refdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00002422 PgHdr *pPg;
2423 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2424 if( pPg->nRef<=0 ) continue;
2425 printf("PAGE %3d addr=0x%08x nRef=%d\n",
2426 pPg->pgno, (int)PGHDR_TO_DATA(pPg), pPg->nRef);
2427 }
2428}
2429#endif