Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/sqlite/src/src/pager.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* | - | ||||||||||||||||||
2 | ** 2001 September 15 | - | ||||||||||||||||||
3 | ** | - | ||||||||||||||||||
4 | ** The author disclaims copyright to this source code. In place of | - | ||||||||||||||||||
5 | ** a legal notice, here is a blessing: | - | ||||||||||||||||||
6 | ** | - | ||||||||||||||||||
7 | ** 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. | - | ||||||||||||||||||
10 | ** | - | ||||||||||||||||||
11 | ************************************************************************* | - | ||||||||||||||||||
12 | ** This is the implementation of the page cache subsystem or "pager". | - | ||||||||||||||||||
13 | ** | - | ||||||||||||||||||
14 | ** 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. | - | ||||||||||||||||||
20 | */ | - | ||||||||||||||||||
21 | #ifndef SQLITE_OMIT_DISKIO | - | ||||||||||||||||||
22 | #include "sqliteInt.h" | - | ||||||||||||||||||
23 | #include "wal.h" | - | ||||||||||||||||||
24 | - | |||||||||||||||||||
25 | - | |||||||||||||||||||
26 | /******************* NOTES ON THE DESIGN OF THE PAGER ************************ | - | ||||||||||||||||||
27 | ** | - | ||||||||||||||||||
28 | ** This comment block describes invariants that hold when using a rollback | - | ||||||||||||||||||
29 | ** journal. These invariants do not apply for journal_mode=WAL, | - | ||||||||||||||||||
30 | ** journal_mode=MEMORY, or journal_mode=OFF. | - | ||||||||||||||||||
31 | ** | - | ||||||||||||||||||
32 | ** Within this comment block, a page is deemed to have been synced | - | ||||||||||||||||||
33 | ** automatically as soon as it is written when PRAGMA synchronous=OFF. | - | ||||||||||||||||||
34 | ** Otherwise, the page is not synced until the xSync method of the VFS | - | ||||||||||||||||||
35 | ** is called successfully on the file containing the page. | - | ||||||||||||||||||
36 | ** | - | ||||||||||||||||||
37 | ** Definition: A page of the database file is said to be "overwriteable" if | - | ||||||||||||||||||
38 | ** one or more of the following are true about the page: | - | ||||||||||||||||||
39 | ** | - | ||||||||||||||||||
40 | ** (a) The original content of the page as it was at the beginning of | - | ||||||||||||||||||
41 | ** the transaction has been written into the rollback journal and | - | ||||||||||||||||||
42 | ** synced. | - | ||||||||||||||||||
43 | ** | - | ||||||||||||||||||
44 | ** (b) The page was a freelist leaf page at the start of the transaction. | - | ||||||||||||||||||
45 | ** | - | ||||||||||||||||||
46 | ** (c) The page number is greater than the largest page that existed in | - | ||||||||||||||||||
47 | ** the database file at the start of the transaction. | - | ||||||||||||||||||
48 | ** | - | ||||||||||||||||||
49 | ** (1) A page of the database file is never overwritten unless one of the | - | ||||||||||||||||||
50 | ** following are true: | - | ||||||||||||||||||
51 | ** | - | ||||||||||||||||||
52 | ** (a) The page and all other pages on the same sector are overwriteable. | - | ||||||||||||||||||
53 | ** | - | ||||||||||||||||||
54 | ** (b) The atomic page write optimization is enabled, and the entire | - | ||||||||||||||||||
55 | ** transaction other than the update of the transaction sequence | - | ||||||||||||||||||
56 | ** number consists of a single page change. | - | ||||||||||||||||||
57 | ** | - | ||||||||||||||||||
58 | ** (2) The content of a page written into the rollback journal exactly matches | - | ||||||||||||||||||
59 | ** both the content in the database when the rollback journal was written | - | ||||||||||||||||||
60 | ** and the content in the database at the beginning of the current | - | ||||||||||||||||||
61 | ** transaction. | - | ||||||||||||||||||
62 | ** | - | ||||||||||||||||||
63 | ** (3) Writes to the database file are an integer multiple of the page size | - | ||||||||||||||||||
64 | ** in length and are aligned on a page boundary. | - | ||||||||||||||||||
65 | ** | - | ||||||||||||||||||
66 | ** (4) Reads from the database file are either aligned on a page boundary and | - | ||||||||||||||||||
67 | ** an integer multiple of the page size in length or are taken from the | - | ||||||||||||||||||
68 | ** first 100 bytes of the database file. | - | ||||||||||||||||||
69 | ** | - | ||||||||||||||||||
70 | ** (5) All writes to the database file are synced prior to the rollback journal | - | ||||||||||||||||||
71 | ** being deleted, truncated, or zeroed. | - | ||||||||||||||||||
72 | ** | - | ||||||||||||||||||
73 | ** (6) If a master journal file is used, then all writes to the database file | - | ||||||||||||||||||
74 | ** are synced prior to the master journal being deleted. | - | ||||||||||||||||||
75 | ** | - | ||||||||||||||||||
76 | ** Definition: Two databases (or the same database at two points it time) | - | ||||||||||||||||||
77 | ** are said to be "logically equivalent" if they give the same answer to | - | ||||||||||||||||||
78 | ** all queries. Note in particular the content of freelist leaf | - | ||||||||||||||||||
79 | ** pages can be changed arbitrarily without affecting the logical equivalence | - | ||||||||||||||||||
80 | ** of the database. | - | ||||||||||||||||||
81 | ** | - | ||||||||||||||||||
82 | ** (7) At any time, if any subset, including the empty set and the total set, | - | ||||||||||||||||||
83 | ** of the unsynced changes to a rollback journal are removed and the | - | ||||||||||||||||||
84 | ** journal is rolled back, the resulting database file will be logically | - | ||||||||||||||||||
85 | ** equivalent to the database file at the beginning of the transaction. | - | ||||||||||||||||||
86 | ** | - | ||||||||||||||||||
87 | ** (8) When a transaction is rolled back, the xTruncate method of the VFS | - | ||||||||||||||||||
88 | ** is called to restore the database file to the same size it was at | - | ||||||||||||||||||
89 | ** the beginning of the transaction. (In some VFSes, the xTruncate | - | ||||||||||||||||||
90 | ** method is a no-op, but that does not change the fact the SQLite will | - | ||||||||||||||||||
91 | ** invoke it.) | - | ||||||||||||||||||
92 | ** | - | ||||||||||||||||||
93 | ** (9) Whenever the database file is modified, at least one bit in the range | - | ||||||||||||||||||
94 | ** of bytes from 24 through 39 inclusive will be changed prior to releasing | - | ||||||||||||||||||
95 | ** the EXCLUSIVE lock, thus signaling other connections on the same | - | ||||||||||||||||||
96 | ** database to flush their caches. | - | ||||||||||||||||||
97 | ** | - | ||||||||||||||||||
98 | ** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less | - | ||||||||||||||||||
99 | ** than one billion transactions. | - | ||||||||||||||||||
100 | ** | - | ||||||||||||||||||
101 | ** (11) A database file is well-formed at the beginning and at the conclusion | - | ||||||||||||||||||
102 | ** of every transaction. | - | ||||||||||||||||||
103 | ** | - | ||||||||||||||||||
104 | ** (12) An EXCLUSIVE lock is held on the database file when writing to | - | ||||||||||||||||||
105 | ** the database file. | - | ||||||||||||||||||
106 | ** | - | ||||||||||||||||||
107 | ** (13) A SHARED lock is held on the database file while reading any | - | ||||||||||||||||||
108 | ** content out of the database file. | - | ||||||||||||||||||
109 | ** | - | ||||||||||||||||||
110 | ******************************************************************************/ | - | ||||||||||||||||||
111 | - | |||||||||||||||||||
112 | /* | - | ||||||||||||||||||
113 | ** Macros for troubleshooting. Normally turned off | - | ||||||||||||||||||
114 | */ | - | ||||||||||||||||||
115 | #if 0 | - | ||||||||||||||||||
116 | int sqlite3PagerTrace=1; /* True to enable tracing */ | - | ||||||||||||||||||
117 | #define sqlite3DebugPrintf printf | - | ||||||||||||||||||
118 | #define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; } | - | ||||||||||||||||||
119 | #else | - | ||||||||||||||||||
120 | #define PAGERTRACE(X) | - | ||||||||||||||||||
121 | #endif | - | ||||||||||||||||||
122 | - | |||||||||||||||||||
123 | /* | - | ||||||||||||||||||
124 | ** The following two macros are used within the PAGERTRACE() macros above | - | ||||||||||||||||||
125 | ** to print out file-descriptors. | - | ||||||||||||||||||
126 | ** | - | ||||||||||||||||||
127 | ** PAGERID() takes a pointer to a Pager struct as its argument. The | - | ||||||||||||||||||
128 | ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file | - | ||||||||||||||||||
129 | ** struct as its argument. | - | ||||||||||||||||||
130 | */ | - | ||||||||||||||||||
131 | #define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd)) | - | ||||||||||||||||||
132 | #define FILEHANDLEID(fd) (SQLITE_PTR_TO_INT(fd)) | - | ||||||||||||||||||
133 | - | |||||||||||||||||||
134 | /* | - | ||||||||||||||||||
135 | ** The Pager.eState variable stores the current 'state' of a pager. A | - | ||||||||||||||||||
136 | ** pager may be in any one of the seven states shown in the following | - | ||||||||||||||||||
137 | ** state diagram. | - | ||||||||||||||||||
138 | ** | - | ||||||||||||||||||
139 | ** OPEN <------+------+ | - | ||||||||||||||||||
140 | ** | | | | - | ||||||||||||||||||
141 | ** V | | | - | ||||||||||||||||||
142 | ** +---------> READER-------+ | | - | ||||||||||||||||||
143 | ** | | | | - | ||||||||||||||||||
144 | ** | V | | - | ||||||||||||||||||
145 | ** |<-------WRITER_LOCKED------> ERROR | - | ||||||||||||||||||
146 | ** | | ^ | - | ||||||||||||||||||
147 | ** | V | | - | ||||||||||||||||||
148 | ** |<------WRITER_CACHEMOD-------->| | - | ||||||||||||||||||
149 | ** | | | | - | ||||||||||||||||||
150 | ** | V | | - | ||||||||||||||||||
151 | ** |<-------WRITER_DBMOD---------->| | - | ||||||||||||||||||
152 | ** | | | | - | ||||||||||||||||||
153 | ** | V | | - | ||||||||||||||||||
154 | ** +<------WRITER_FINISHED-------->+ | - | ||||||||||||||||||
155 | ** | - | ||||||||||||||||||
156 | ** | - | ||||||||||||||||||
157 | ** List of state transitions and the C [function] that performs each: | - | ||||||||||||||||||
158 | ** | - | ||||||||||||||||||
159 | ** OPEN -> READER [sqlite3PagerSharedLock] | - | ||||||||||||||||||
160 | ** READER -> OPEN [pager_unlock] | - | ||||||||||||||||||
161 | ** | - | ||||||||||||||||||
162 | ** READER -> WRITER_LOCKED [sqlite3PagerBegin] | - | ||||||||||||||||||
163 | ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal] | - | ||||||||||||||||||
164 | ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal] | - | ||||||||||||||||||
165 | ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne] | - | ||||||||||||||||||
166 | ** WRITER_*** -> READER [pager_end_transaction] | - | ||||||||||||||||||
167 | ** | - | ||||||||||||||||||
168 | ** WRITER_*** -> ERROR [pager_error] | - | ||||||||||||||||||
169 | ** ERROR -> OPEN [pager_unlock] | - | ||||||||||||||||||
170 | ** | - | ||||||||||||||||||
171 | ** | - | ||||||||||||||||||
172 | ** OPEN: | - | ||||||||||||||||||
173 | ** | - | ||||||||||||||||||
174 | ** The pager starts up in this state. Nothing is guaranteed in this | - | ||||||||||||||||||
175 | ** state - the file may or may not be locked and the database size is | - | ||||||||||||||||||
176 | ** unknown. The database may not be read or written. | - | ||||||||||||||||||
177 | ** | - | ||||||||||||||||||
178 | ** * No read or write transaction is active. | - | ||||||||||||||||||
179 | ** * Any lock, or no lock at all, may be held on the database file. | - | ||||||||||||||||||
180 | ** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted. | - | ||||||||||||||||||
181 | ** | - | ||||||||||||||||||
182 | ** READER: | - | ||||||||||||||||||
183 | ** | - | ||||||||||||||||||
184 | ** In this state all the requirements for reading the database in | - | ||||||||||||||||||
185 | ** rollback (non-WAL) mode are met. Unless the pager is (or recently | - | ||||||||||||||||||
186 | ** was) in exclusive-locking mode, a user-level read transaction is | - | ||||||||||||||||||
187 | ** open. The database size is known in this state. | - | ||||||||||||||||||
188 | ** | - | ||||||||||||||||||
189 | ** A connection running with locking_mode=normal enters this state when | - | ||||||||||||||||||
190 | ** it opens a read-transaction on the database and returns to state | - | ||||||||||||||||||
191 | ** OPEN after the read-transaction is completed. However a connection | - | ||||||||||||||||||
192 | ** running in locking_mode=exclusive (including temp databases) remains in | - | ||||||||||||||||||
193 | ** this state even after the read-transaction is closed. The only way | - | ||||||||||||||||||
194 | ** a locking_mode=exclusive connection can transition from READER to OPEN | - | ||||||||||||||||||
195 | ** is via the ERROR state (see below). | - | ||||||||||||||||||
196 | ** | - | ||||||||||||||||||
197 | ** * A read transaction may be active (but a write-transaction cannot). | - | ||||||||||||||||||
198 | ** * A SHARED or greater lock is held on the database file. | - | ||||||||||||||||||
199 | ** * The dbSize variable may be trusted (even if a user-level read | - | ||||||||||||||||||
200 | ** transaction is not active). The dbOrigSize and dbFileSize variables | - | ||||||||||||||||||
201 | ** may not be trusted at this point. | - | ||||||||||||||||||
202 | ** * If the database is a WAL database, then the WAL connection is open. | - | ||||||||||||||||||
203 | ** * Even if a read-transaction is not open, it is guaranteed that | - | ||||||||||||||||||
204 | ** there is no hot-journal in the file-system. | - | ||||||||||||||||||
205 | ** | - | ||||||||||||||||||
206 | ** WRITER_LOCKED: | - | ||||||||||||||||||
207 | ** | - | ||||||||||||||||||
208 | ** The pager moves to this state from READER when a write-transaction | - | ||||||||||||||||||
209 | ** is first opened on the database. In WRITER_LOCKED state, all locks | - | ||||||||||||||||||
210 | ** required to start a write-transaction are held, but no actual | - | ||||||||||||||||||
211 | ** modifications to the cache or database have taken place. | - | ||||||||||||||||||
212 | ** | - | ||||||||||||||||||
213 | ** In rollback mode, a RESERVED or (if the transaction was opened with | - | ||||||||||||||||||
214 | ** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when | - | ||||||||||||||||||
215 | ** moving to this state, but the journal file is not written to or opened | - | ||||||||||||||||||
216 | ** to in this state. If the transaction is committed or rolled back while | - | ||||||||||||||||||
217 | ** in WRITER_LOCKED state, all that is required is to unlock the database | - | ||||||||||||||||||
218 | ** file. | - | ||||||||||||||||||
219 | ** | - | ||||||||||||||||||
220 | ** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file. | - | ||||||||||||||||||
221 | ** If the connection is running with locking_mode=exclusive, an attempt | - | ||||||||||||||||||
222 | ** is made to obtain an EXCLUSIVE lock on the database file. | - | ||||||||||||||||||
223 | ** | - | ||||||||||||||||||
224 | ** * A write transaction is active. | - | ||||||||||||||||||
225 | ** * If the connection is open in rollback-mode, a RESERVED or greater | - | ||||||||||||||||||
226 | ** lock is held on the database file. | - | ||||||||||||||||||
227 | ** * If the connection is open in WAL-mode, a WAL write transaction | - | ||||||||||||||||||
228 | ** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully | - | ||||||||||||||||||
229 | ** called). | - | ||||||||||||||||||
230 | ** * The dbSize, dbOrigSize and dbFileSize variables are all valid. | - | ||||||||||||||||||
231 | ** * The contents of the pager cache have not been modified. | - | ||||||||||||||||||
232 | ** * The journal file may or may not be open. | - | ||||||||||||||||||
233 | ** * Nothing (not even the first header) has been written to the journal. | - | ||||||||||||||||||
234 | ** | - | ||||||||||||||||||
235 | ** WRITER_CACHEMOD: | - | ||||||||||||||||||
236 | ** | - | ||||||||||||||||||
237 | ** A pager moves from WRITER_LOCKED state to this state when a page is | - | ||||||||||||||||||
238 | ** first modified by the upper layer. In rollback mode the journal file | - | ||||||||||||||||||
239 | ** is opened (if it is not already open) and a header written to the | - | ||||||||||||||||||
240 | ** start of it. The database file on disk has not been modified. | - | ||||||||||||||||||
241 | ** | - | ||||||||||||||||||
242 | ** * A write transaction is active. | - | ||||||||||||||||||
243 | ** * A RESERVED or greater lock is held on the database file. | - | ||||||||||||||||||
244 | ** * The journal file is open and the first header has been written | - | ||||||||||||||||||
245 | ** to it, but the header has not been synced to disk. | - | ||||||||||||||||||
246 | ** * The contents of the page cache have been modified. | - | ||||||||||||||||||
247 | ** | - | ||||||||||||||||||
248 | ** WRITER_DBMOD: | - | ||||||||||||||||||
249 | ** | - | ||||||||||||||||||
250 | ** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state | - | ||||||||||||||||||
251 | ** when it modifies the contents of the database file. WAL connections | - | ||||||||||||||||||
252 | ** never enter this state (since they do not modify the database file, | - | ||||||||||||||||||
253 | ** just the log file). | - | ||||||||||||||||||
254 | ** | - | ||||||||||||||||||
255 | ** * A write transaction is active. | - | ||||||||||||||||||
256 | ** * An EXCLUSIVE or greater lock is held on the database file. | - | ||||||||||||||||||
257 | ** * The journal file is open and the first header has been written | - | ||||||||||||||||||
258 | ** and synced to disk. | - | ||||||||||||||||||
259 | ** * The contents of the page cache have been modified (and possibly | - | ||||||||||||||||||
260 | ** written to disk). | - | ||||||||||||||||||
261 | ** | - | ||||||||||||||||||
262 | ** WRITER_FINISHED: | - | ||||||||||||||||||
263 | ** | - | ||||||||||||||||||
264 | ** It is not possible for a WAL connection to enter this state. | - | ||||||||||||||||||
265 | ** | - | ||||||||||||||||||
266 | ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD | - | ||||||||||||||||||
267 | ** state after the entire transaction has been successfully written into the | - | ||||||||||||||||||
268 | ** database file. In this state the transaction may be committed simply | - | ||||||||||||||||||
269 | ** by finalizing the journal file. Once in WRITER_FINISHED state, it is | - | ||||||||||||||||||
270 | ** not possible to modify the database further. At this point, the upper | - | ||||||||||||||||||
271 | ** layer must either commit or rollback the transaction. | - | ||||||||||||||||||
272 | ** | - | ||||||||||||||||||
273 | ** * A write transaction is active. | - | ||||||||||||||||||
274 | ** * An EXCLUSIVE or greater lock is held on the database file. | - | ||||||||||||||||||
275 | ** * All writing and syncing of journal and database data has finished. | - | ||||||||||||||||||
276 | ** If no error occurred, all that remains is to finalize the journal to | - | ||||||||||||||||||
277 | ** commit the transaction. If an error did occur, the caller will need | - | ||||||||||||||||||
278 | ** to rollback the transaction. | - | ||||||||||||||||||
279 | ** | - | ||||||||||||||||||
280 | ** ERROR: | - | ||||||||||||||||||
281 | ** | - | ||||||||||||||||||
282 | ** The ERROR state is entered when an IO or disk-full error (including | - | ||||||||||||||||||
283 | ** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it | - | ||||||||||||||||||
284 | ** difficult to be sure that the in-memory pager state (cache contents, | - | ||||||||||||||||||
285 | ** db size etc.) are consistent with the contents of the file-system. | - | ||||||||||||||||||
286 | ** | - | ||||||||||||||||||
287 | ** Temporary pager files may enter the ERROR state, but in-memory pagers | - | ||||||||||||||||||
288 | ** cannot. | - | ||||||||||||||||||
289 | ** | - | ||||||||||||||||||
290 | ** For example, if an IO error occurs while performing a rollback, | - | ||||||||||||||||||
291 | ** the contents of the page-cache may be left in an inconsistent state. | - | ||||||||||||||||||
292 | ** At this point it would be dangerous to change back to READER state | - | ||||||||||||||||||
293 | ** (as usually happens after a rollback). Any subsequent readers might | - | ||||||||||||||||||
294 | ** report database corruption (due to the inconsistent cache), and if | - | ||||||||||||||||||
295 | ** they upgrade to writers, they may inadvertently corrupt the database | - | ||||||||||||||||||
296 | ** file. To avoid this hazard, the pager switches into the ERROR state | - | ||||||||||||||||||
297 | ** instead of READER following such an error. | - | ||||||||||||||||||
298 | ** | - | ||||||||||||||||||
299 | ** Once it has entered the ERROR state, any attempt to use the pager | - | ||||||||||||||||||
300 | ** to read or write data returns an error. Eventually, once all | - | ||||||||||||||||||
301 | ** outstanding transactions have been abandoned, the pager is able to | - | ||||||||||||||||||
302 | ** transition back to OPEN state, discarding the contents of the | - | ||||||||||||||||||
303 | ** page-cache and any other in-memory state at the same time. Everything | - | ||||||||||||||||||
304 | ** is reloaded from disk (and, if necessary, hot-journal rollback peformed) | - | ||||||||||||||||||
305 | ** when a read-transaction is next opened on the pager (transitioning | - | ||||||||||||||||||
306 | ** the pager into READER state). At that point the system has recovered | - | ||||||||||||||||||
307 | ** from the error. | - | ||||||||||||||||||
308 | ** | - | ||||||||||||||||||
309 | ** Specifically, the pager jumps into the ERROR state if: | - | ||||||||||||||||||
310 | ** | - | ||||||||||||||||||
311 | ** 1. An error occurs while attempting a rollback. This happens in | - | ||||||||||||||||||
312 | ** function sqlite3PagerRollback(). | - | ||||||||||||||||||
313 | ** | - | ||||||||||||||||||
314 | ** 2. An error occurs while attempting to finalize a journal file | - | ||||||||||||||||||
315 | ** following a commit in function sqlite3PagerCommitPhaseTwo(). | - | ||||||||||||||||||
316 | ** | - | ||||||||||||||||||
317 | ** 3. An error occurs while attempting to write to the journal or | - | ||||||||||||||||||
318 | ** database file in function pagerStress() in order to free up | - | ||||||||||||||||||
319 | ** memory. | - | ||||||||||||||||||
320 | ** | - | ||||||||||||||||||
321 | ** In other cases, the error is returned to the b-tree layer. The b-tree | - | ||||||||||||||||||
322 | ** layer then attempts a rollback operation. If the error condition | - | ||||||||||||||||||
323 | ** persists, the pager enters the ERROR state via condition (1) above. | - | ||||||||||||||||||
324 | ** | - | ||||||||||||||||||
325 | ** Condition (3) is necessary because it can be triggered by a read-only | - | ||||||||||||||||||
326 | ** statement executed within a transaction. In this case, if the error | - | ||||||||||||||||||
327 | ** code were simply returned to the user, the b-tree layer would not | - | ||||||||||||||||||
328 | ** automatically attempt a rollback, as it assumes that an error in a | - | ||||||||||||||||||
329 | ** read-only statement cannot leave the pager in an internally inconsistent | - | ||||||||||||||||||
330 | ** state. | - | ||||||||||||||||||
331 | ** | - | ||||||||||||||||||
332 | ** * The Pager.errCode variable is set to something other than SQLITE_OK. | - | ||||||||||||||||||
333 | ** * There are one or more outstanding references to pages (after the | - | ||||||||||||||||||
334 | ** last reference is dropped the pager should move back to OPEN state). | - | ||||||||||||||||||
335 | ** * The pager is not an in-memory pager. | - | ||||||||||||||||||
336 | ** | - | ||||||||||||||||||
337 | ** | - | ||||||||||||||||||
338 | ** Notes: | - | ||||||||||||||||||
339 | ** | - | ||||||||||||||||||
340 | ** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the | - | ||||||||||||||||||
341 | ** connection is open in WAL mode. A WAL connection is always in one | - | ||||||||||||||||||
342 | ** of the first four states. | - | ||||||||||||||||||
343 | ** | - | ||||||||||||||||||
344 | ** * Normally, a connection open in exclusive mode is never in PAGER_OPEN | - | ||||||||||||||||||
345 | ** state. There are two exceptions: immediately after exclusive-mode has | - | ||||||||||||||||||
346 | ** been turned on (and before any read or write transactions are | - | ||||||||||||||||||
347 | ** executed), and when the pager is leaving the "error state". | - | ||||||||||||||||||
348 | ** | - | ||||||||||||||||||
349 | ** * See also: assert_pager_state(). | - | ||||||||||||||||||
350 | */ | - | ||||||||||||||||||
351 | #define PAGER_OPEN 0 | - | ||||||||||||||||||
352 | #define PAGER_READER 1 | - | ||||||||||||||||||
353 | #define PAGER_WRITER_LOCKED 2 | - | ||||||||||||||||||
354 | #define PAGER_WRITER_CACHEMOD 3 | - | ||||||||||||||||||
355 | #define PAGER_WRITER_DBMOD 4 | - | ||||||||||||||||||
356 | #define PAGER_WRITER_FINISHED 5 | - | ||||||||||||||||||
357 | #define PAGER_ERROR 6 | - | ||||||||||||||||||
358 | - | |||||||||||||||||||
359 | /* | - | ||||||||||||||||||
360 | ** The Pager.eLock variable is almost always set to one of the | - | ||||||||||||||||||
361 | ** following locking-states, according to the lock currently held on | - | ||||||||||||||||||
362 | ** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK. | - | ||||||||||||||||||
363 | ** This variable is kept up to date as locks are taken and released by | - | ||||||||||||||||||
364 | ** the pagerLockDb() and pagerUnlockDb() wrappers. | - | ||||||||||||||||||
365 | ** | - | ||||||||||||||||||
366 | ** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY | - | ||||||||||||||||||
367 | ** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not | - | ||||||||||||||||||
368 | ** the operation was successful. In these circumstances pagerLockDb() and | - | ||||||||||||||||||
369 | ** pagerUnlockDb() take a conservative approach - eLock is always updated | - | ||||||||||||||||||
370 | ** when unlocking the file, and only updated when locking the file if the | - | ||||||||||||||||||
371 | ** VFS call is successful. This way, the Pager.eLock variable may be set | - | ||||||||||||||||||
372 | ** to a less exclusive (lower) value than the lock that is actually held | - | ||||||||||||||||||
373 | ** at the system level, but it is never set to a more exclusive value. | - | ||||||||||||||||||
374 | ** | - | ||||||||||||||||||
375 | ** This is usually safe. If an xUnlock fails or appears to fail, there may | - | ||||||||||||||||||
376 | ** be a few redundant xLock() calls or a lock may be held for longer than | - | ||||||||||||||||||
377 | ** required, but nothing really goes wrong. | - | ||||||||||||||||||
378 | ** | - | ||||||||||||||||||
379 | ** The exception is when the database file is unlocked as the pager moves | - | ||||||||||||||||||
380 | ** from ERROR to OPEN state. At this point there may be a hot-journal file | - | ||||||||||||||||||
381 | ** in the file-system that needs to be rolled back (as part of an OPEN->SHARED | - | ||||||||||||||||||
382 | ** transition, by the same pager or any other). If the call to xUnlock() | - | ||||||||||||||||||
383 | ** fails at this point and the pager is left holding an EXCLUSIVE lock, this | - | ||||||||||||||||||
384 | ** can confuse the call to xCheckReservedLock() call made later as part | - | ||||||||||||||||||
385 | ** of hot-journal detection. | - | ||||||||||||||||||
386 | ** | - | ||||||||||||||||||
387 | ** xCheckReservedLock() is defined as returning true "if there is a RESERVED | - | ||||||||||||||||||
388 | ** lock held by this process or any others". So xCheckReservedLock may | - | ||||||||||||||||||
389 | ** return true because the caller itself is holding an EXCLUSIVE lock (but | - | ||||||||||||||||||
390 | ** doesn't know it because of a previous error in xUnlock). If this happens | - | ||||||||||||||||||
391 | ** a hot-journal may be mistaken for a journal being created by an active | - | ||||||||||||||||||
392 | ** transaction in another process, causing SQLite to read from the database | - | ||||||||||||||||||
393 | ** without rolling it back. | - | ||||||||||||||||||
394 | ** | - | ||||||||||||||||||
395 | ** To work around this, if a call to xUnlock() fails when unlocking the | - | ||||||||||||||||||
396 | ** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It | - | ||||||||||||||||||
397 | ** is only changed back to a real locking state after a successful call | - | ||||||||||||||||||
398 | ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition | - | ||||||||||||||||||
399 | ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK | - | ||||||||||||||||||
400 | ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE | - | ||||||||||||||||||
401 | ** lock on the database file before attempting to roll it back. See function | - | ||||||||||||||||||
402 | ** PagerSharedLock() for more detail. | - | ||||||||||||||||||
403 | ** | - | ||||||||||||||||||
404 | ** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in | - | ||||||||||||||||||
405 | ** PAGER_OPEN state. | - | ||||||||||||||||||
406 | */ | - | ||||||||||||||||||
407 | #define UNKNOWN_LOCK (EXCLUSIVE_LOCK+1) | - | ||||||||||||||||||
408 | - | |||||||||||||||||||
409 | /* | - | ||||||||||||||||||
410 | ** A macro used for invoking the codec if there is one | - | ||||||||||||||||||
411 | */ | - | ||||||||||||||||||
412 | #ifdef SQLITE_HAS_CODEC | - | ||||||||||||||||||
413 | # define CODEC1(P,D,N,X,E) \ | - | ||||||||||||||||||
414 | if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; } | - | ||||||||||||||||||
415 | # define CODEC2(P,D,N,X,E,O) \ | - | ||||||||||||||||||
416 | if( P->xCodec==0 ){ O=(char*)D; }else \ | - | ||||||||||||||||||
417 | if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; } | - | ||||||||||||||||||
418 | #else | - | ||||||||||||||||||
419 | # define CODEC1(P,D,N,X,E) /* NO-OP */ | - | ||||||||||||||||||
420 | # define CODEC2(P,D,N,X,E,O) O=(char*)D | - | ||||||||||||||||||
421 | #endif | - | ||||||||||||||||||
422 | - | |||||||||||||||||||
423 | /* | - | ||||||||||||||||||
424 | ** The maximum allowed sector size. 64KiB. If the xSectorsize() method | - | ||||||||||||||||||
425 | ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead. | - | ||||||||||||||||||
426 | ** This could conceivably cause corruption following a power failure on | - | ||||||||||||||||||
427 | ** such a system. This is currently an undocumented limit. | - | ||||||||||||||||||
428 | */ | - | ||||||||||||||||||
429 | #define MAX_SECTOR_SIZE 0x10000 | - | ||||||||||||||||||
430 | - | |||||||||||||||||||
431 | - | |||||||||||||||||||
432 | /* | - | ||||||||||||||||||
433 | ** An instance of the following structure is allocated for each active | - | ||||||||||||||||||
434 | ** savepoint and statement transaction in the system. All such structures | - | ||||||||||||||||||
435 | ** are stored in the Pager.aSavepoint[] array, which is allocated and | - | ||||||||||||||||||
436 | ** resized using sqlite3Realloc(). | - | ||||||||||||||||||
437 | ** | - | ||||||||||||||||||
438 | ** When a savepoint is created, the PagerSavepoint.iHdrOffset field is | - | ||||||||||||||||||
439 | ** set to 0. If a journal-header is written into the main journal while | - | ||||||||||||||||||
440 | ** the savepoint is active, then iHdrOffset is set to the byte offset | - | ||||||||||||||||||
441 | ** immediately following the last journal record written into the main | - | ||||||||||||||||||
442 | ** journal before the journal-header. This is required during savepoint | - | ||||||||||||||||||
443 | ** rollback (see pagerPlaybackSavepoint()). | - | ||||||||||||||||||
444 | */ | - | ||||||||||||||||||
445 | typedef struct PagerSavepoint PagerSavepoint; | - | ||||||||||||||||||
446 | struct PagerSavepoint { | - | ||||||||||||||||||
447 | i64 iOffset; /* Starting offset in main journal */ | - | ||||||||||||||||||
448 | i64 iHdrOffset; /* See above */ | - | ||||||||||||||||||
449 | Bitvec *pInSavepoint; /* Set of pages in this savepoint */ | - | ||||||||||||||||||
450 | Pgno nOrig; /* Original number of pages in file */ | - | ||||||||||||||||||
451 | Pgno iSubRec; /* Index of first record in sub-journal */ | - | ||||||||||||||||||
452 | #ifndef SQLITE_OMIT_WAL | - | ||||||||||||||||||
453 | u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */ | - | ||||||||||||||||||
454 | #endif | - | ||||||||||||||||||
455 | }; | - | ||||||||||||||||||
456 | - | |||||||||||||||||||
457 | /* | - | ||||||||||||||||||
458 | ** Bits of the Pager.doNotSpill flag. See further description below. | - | ||||||||||||||||||
459 | */ | - | ||||||||||||||||||
460 | #define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */ | - | ||||||||||||||||||
461 | #define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */ | - | ||||||||||||||||||
462 | #define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */ | - | ||||||||||||||||||
463 | - | |||||||||||||||||||
464 | /* | - | ||||||||||||||||||
465 | ** An open page cache is an instance of struct Pager. A description of | - | ||||||||||||||||||
466 | ** some of the more important member variables follows: | - | ||||||||||||||||||
467 | ** | - | ||||||||||||||||||
468 | ** eState | - | ||||||||||||||||||
469 | ** | - | ||||||||||||||||||
470 | ** The current 'state' of the pager object. See the comment and state | - | ||||||||||||||||||
471 | ** diagram above for a description of the pager state. | - | ||||||||||||||||||
472 | ** | - | ||||||||||||||||||
473 | ** eLock | - | ||||||||||||||||||
474 | ** | - | ||||||||||||||||||
475 | ** For a real on-disk database, the current lock held on the database file - | - | ||||||||||||||||||
476 | ** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK. | - | ||||||||||||||||||
477 | ** | - | ||||||||||||||||||
478 | ** For a temporary or in-memory database (neither of which require any | - | ||||||||||||||||||
479 | ** locks), this variable is always set to EXCLUSIVE_LOCK. Since such | - | ||||||||||||||||||
480 | ** databases always have Pager.exclusiveMode==1, this tricks the pager | - | ||||||||||||||||||
481 | ** logic into thinking that it already has all the locks it will ever | - | ||||||||||||||||||
482 | ** need (and no reason to release them). | - | ||||||||||||||||||
483 | ** | - | ||||||||||||||||||
484 | ** In some (obscure) circumstances, this variable may also be set to | - | ||||||||||||||||||
485 | ** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for | - | ||||||||||||||||||
486 | ** details. | - | ||||||||||||||||||
487 | ** | - | ||||||||||||||||||
488 | ** changeCountDone | - | ||||||||||||||||||
489 | ** | - | ||||||||||||||||||
490 | ** This boolean variable is used to make sure that the change-counter | - | ||||||||||||||||||
491 | ** (the 4-byte header field at byte offset 24 of the database file) is | - | ||||||||||||||||||
492 | ** not updated more often than necessary. | - | ||||||||||||||||||
493 | ** | - | ||||||||||||||||||
494 | ** It is set to true when the change-counter field is updated, which | - | ||||||||||||||||||
495 | ** can only happen if an exclusive lock is held on the database file. | - | ||||||||||||||||||
496 | ** It is cleared (set to false) whenever an exclusive lock is | - | ||||||||||||||||||
497 | ** relinquished on the database file. Each time a transaction is committed, | - | ||||||||||||||||||
498 | ** The changeCountDone flag is inspected. If it is true, the work of | - | ||||||||||||||||||
499 | ** updating the change-counter is omitted for the current transaction. | - | ||||||||||||||||||
500 | ** | - | ||||||||||||||||||
501 | ** This mechanism means that when running in exclusive mode, a connection | - | ||||||||||||||||||
502 | ** need only update the change-counter once, for the first transaction | - | ||||||||||||||||||
503 | ** committed. | - | ||||||||||||||||||
504 | ** | - | ||||||||||||||||||
505 | ** setMaster | - | ||||||||||||||||||
506 | ** | - | ||||||||||||||||||
507 | ** When PagerCommitPhaseOne() is called to commit a transaction, it may | - | ||||||||||||||||||
508 | ** (or may not) specify a master-journal name to be written into the | - | ||||||||||||||||||
509 | ** journal file before it is synced to disk. | - | ||||||||||||||||||
510 | ** | - | ||||||||||||||||||
511 | ** Whether or not a journal file contains a master-journal pointer affects | - | ||||||||||||||||||
512 | ** the way in which the journal file is finalized after the transaction is | - | ||||||||||||||||||
513 | ** committed or rolled back when running in "journal_mode=PERSIST" mode. | - | ||||||||||||||||||
514 | ** If a journal file does not contain a master-journal pointer, it is | - | ||||||||||||||||||
515 | ** finalized by overwriting the first journal header with zeroes. If | - | ||||||||||||||||||
516 | ** it does contain a master-journal pointer the journal file is finalized | - | ||||||||||||||||||
517 | ** by truncating it to zero bytes, just as if the connection were | - | ||||||||||||||||||
518 | ** running in "journal_mode=truncate" mode. | - | ||||||||||||||||||
519 | ** | - | ||||||||||||||||||
520 | ** Journal files that contain master journal pointers cannot be finalized | - | ||||||||||||||||||
521 | ** simply by overwriting the first journal-header with zeroes, as the | - | ||||||||||||||||||
522 | ** master journal pointer could interfere with hot-journal rollback of any | - | ||||||||||||||||||
523 | ** subsequently interrupted transaction that reuses the journal file. | - | ||||||||||||||||||
524 | ** | - | ||||||||||||||||||
525 | ** The flag is cleared as soon as the journal file is finalized (either | - | ||||||||||||||||||
526 | ** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the | - | ||||||||||||||||||
527 | ** journal file from being successfully finalized, the setMaster flag | - | ||||||||||||||||||
528 | ** is cleared anyway (and the pager will move to ERROR state). | - | ||||||||||||||||||
529 | ** | - | ||||||||||||||||||
530 | ** doNotSpill | - | ||||||||||||||||||
531 | ** | - | ||||||||||||||||||
532 | ** This variables control the behavior of cache-spills (calls made by | - | ||||||||||||||||||
533 | ** the pcache module to the pagerStress() routine to write cached data | - | ||||||||||||||||||
534 | ** to the file-system in order to free up memory). | - | ||||||||||||||||||
535 | ** | - | ||||||||||||||||||
536 | ** When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set, | - | ||||||||||||||||||
537 | ** writing to the database from pagerStress() is disabled altogether. | - | ||||||||||||||||||
538 | ** The SPILLFLAG_ROLLBACK case is done in a very obscure case that | - | ||||||||||||||||||
539 | ** comes up during savepoint rollback that requires the pcache module | - | ||||||||||||||||||
540 | ** to allocate a new page to prevent the journal file from being written | - | ||||||||||||||||||
541 | ** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF | - | ||||||||||||||||||
542 | ** case is a user preference. | - | ||||||||||||||||||
543 | ** | - | ||||||||||||||||||
544 | ** If the SPILLFLAG_NOSYNC bit is set, writing to the database from | - | ||||||||||||||||||
545 | ** pagerStress() is permitted, but syncing the journal file is not. | - | ||||||||||||||||||
546 | ** This flag is set by sqlite3PagerWrite() when the file-system sector-size | - | ||||||||||||||||||
547 | ** is larger than the database page-size in order to prevent a journal sync | - | ||||||||||||||||||
548 | ** from happening in between the journalling of two pages on the same sector. | - | ||||||||||||||||||
549 | ** | - | ||||||||||||||||||
550 | ** subjInMemory | - | ||||||||||||||||||
551 | ** | - | ||||||||||||||||||
552 | ** This is a boolean variable. If true, then any required sub-journal | - | ||||||||||||||||||
553 | ** is opened as an in-memory journal file. If false, then in-memory | - | ||||||||||||||||||
554 | ** sub-journals are only used for in-memory pager files. | - | ||||||||||||||||||
555 | ** | - | ||||||||||||||||||
556 | ** This variable is updated by the upper layer each time a new | - | ||||||||||||||||||
557 | ** write-transaction is opened. | - | ||||||||||||||||||
558 | ** | - | ||||||||||||||||||
559 | ** dbSize, dbOrigSize, dbFileSize | - | ||||||||||||||||||
560 | ** | - | ||||||||||||||||||
561 | ** Variable dbSize is set to the number of pages in the database file. | - | ||||||||||||||||||
562 | ** It is valid in PAGER_READER and higher states (all states except for | - | ||||||||||||||||||
563 | ** OPEN and ERROR). | - | ||||||||||||||||||
564 | ** | - | ||||||||||||||||||
565 | ** dbSize is set based on the size of the database file, which may be | - | ||||||||||||||||||
566 | ** larger than the size of the database (the value stored at offset | - | ||||||||||||||||||
567 | ** 28 of the database header by the btree). If the size of the file | - | ||||||||||||||||||
568 | ** is not an integer multiple of the page-size, the value stored in | - | ||||||||||||||||||
569 | ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2). | - | ||||||||||||||||||
570 | ** Except, any file that is greater than 0 bytes in size is considered | - | ||||||||||||||||||
571 | ** to have at least one page. (i.e. a 1KB file with 2K page-size leads | - | ||||||||||||||||||
572 | ** to dbSize==1). | - | ||||||||||||||||||
573 | ** | - | ||||||||||||||||||
574 | ** During a write-transaction, if pages with page-numbers greater than | - | ||||||||||||||||||
575 | ** dbSize are modified in the cache, dbSize is updated accordingly. | - | ||||||||||||||||||
576 | ** Similarly, if the database is truncated using PagerTruncateImage(), | - | ||||||||||||||||||
577 | ** dbSize is updated. | - | ||||||||||||||||||
578 | ** | - | ||||||||||||||||||
579 | ** Variables dbOrigSize and dbFileSize are valid in states | - | ||||||||||||||||||
580 | ** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize | - | ||||||||||||||||||
581 | ** variable at the start of the transaction. It is used during rollback, | - | ||||||||||||||||||
582 | ** and to determine whether or not pages need to be journalled before | - | ||||||||||||||||||
583 | ** being modified. | - | ||||||||||||||||||
584 | ** | - | ||||||||||||||||||
585 | ** Throughout a write-transaction, dbFileSize contains the size of | - | ||||||||||||||||||
586 | ** the file on disk in pages. It is set to a copy of dbSize when the | - | ||||||||||||||||||
587 | ** write-transaction is first opened, and updated when VFS calls are made | - | ||||||||||||||||||
588 | ** to write or truncate the database file on disk. | - | ||||||||||||||||||
589 | ** | - | ||||||||||||||||||
590 | ** The only reason the dbFileSize variable is required is to suppress | - | ||||||||||||||||||
591 | ** unnecessary calls to xTruncate() after committing a transaction. If, | - | ||||||||||||||||||
592 | ** when a transaction is committed, the dbFileSize variable indicates | - | ||||||||||||||||||
593 | ** that the database file is larger than the database image (Pager.dbSize), | - | ||||||||||||||||||
594 | ** pager_truncate() is called. The pager_truncate() call uses xFilesize() | - | ||||||||||||||||||
595 | ** to measure the database file on disk, and then truncates it if required. | - | ||||||||||||||||||
596 | ** dbFileSize is not used when rolling back a transaction. In this case | - | ||||||||||||||||||
597 | ** pager_truncate() is called unconditionally (which means there may be | - | ||||||||||||||||||
598 | ** a call to xFilesize() that is not strictly required). In either case, | - | ||||||||||||||||||
599 | ** pager_truncate() may cause the file to become smaller or larger. | - | ||||||||||||||||||
600 | ** | - | ||||||||||||||||||
601 | ** dbHintSize | - | ||||||||||||||||||
602 | ** | - | ||||||||||||||||||
603 | ** The dbHintSize variable is used to limit the number of calls made to | - | ||||||||||||||||||
604 | ** the VFS xFileControl(FCNTL_SIZE_HINT) method. | - | ||||||||||||||||||
605 | ** | - | ||||||||||||||||||
606 | ** dbHintSize is set to a copy of the dbSize variable when a | - | ||||||||||||||||||
607 | ** write-transaction is opened (at the same time as dbFileSize and | - | ||||||||||||||||||
608 | ** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called, | - | ||||||||||||||||||
609 | ** dbHintSize is increased to the number of pages that correspond to the | - | ||||||||||||||||||
610 | ** size-hint passed to the method call. See pager_write_pagelist() for | - | ||||||||||||||||||
611 | ** details. | - | ||||||||||||||||||
612 | ** | - | ||||||||||||||||||
613 | ** errCode | - | ||||||||||||||||||
614 | ** | - | ||||||||||||||||||
615 | ** The Pager.errCode variable is only ever used in PAGER_ERROR state. It | - | ||||||||||||||||||
616 | ** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode | - | ||||||||||||||||||
617 | ** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX | - | ||||||||||||||||||
618 | ** sub-codes. | - | ||||||||||||||||||
619 | ** | - | ||||||||||||||||||
620 | ** syncFlags, walSyncFlags | - | ||||||||||||||||||
621 | ** | - | ||||||||||||||||||
622 | ** syncFlags is either SQLITE_SYNC_NORMAL (0x02) or SQLITE_SYNC_FULL (0x03). | - | ||||||||||||||||||
623 | ** syncFlags is used for rollback mode. walSyncFlags is used for WAL mode | - | ||||||||||||||||||
624 | ** and contains the flags used to sync the checkpoint operations in the | - | ||||||||||||||||||
625 | ** lower two bits, and sync flags used for transaction commits in the WAL | - | ||||||||||||||||||
626 | ** file in bits 0x04 and 0x08. In other words, to get the correct sync flags | - | ||||||||||||||||||
627 | ** for checkpoint operations, use (walSyncFlags&0x03) and to get the correct | - | ||||||||||||||||||
628 | ** sync flags for transaction commit, use ((walSyncFlags>>2)&0x03). Note | - | ||||||||||||||||||
629 | ** that with synchronous=NORMAL in WAL mode, transaction commit is not synced | - | ||||||||||||||||||
630 | ** meaning that the 0x04 and 0x08 bits are both zero. | - | ||||||||||||||||||
631 | */ | - | ||||||||||||||||||
632 | struct Pager { | - | ||||||||||||||||||
633 | sqlite3_vfs *pVfs; /* OS functions to use for IO */ | - | ||||||||||||||||||
634 | u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */ | - | ||||||||||||||||||
635 | u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */ | - | ||||||||||||||||||
636 | u8 useJournal; /* Use a rollback journal on this file */ | - | ||||||||||||||||||
637 | u8 noSync; /* Do not sync the journal if true */ | - | ||||||||||||||||||
638 | u8 fullSync; /* Do extra syncs of the journal for robustness */ | - | ||||||||||||||||||
639 | u8 extraSync; /* sync directory after journal delete */ | - | ||||||||||||||||||
640 | u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */ | - | ||||||||||||||||||
641 | u8 walSyncFlags; /* See description above */ | - | ||||||||||||||||||
642 | u8 tempFile; /* zFilename is a temporary or immutable file */ | - | ||||||||||||||||||
643 | u8 noLock; /* Do not lock (except in WAL mode) */ | - | ||||||||||||||||||
644 | u8 readOnly; /* True for a read-only database */ | - | ||||||||||||||||||
645 | u8 memDb; /* True to inhibit all file I/O */ | - | ||||||||||||||||||
646 | - | |||||||||||||||||||
647 | /************************************************************************** | - | ||||||||||||||||||
648 | ** The following block contains those class members that change during | - | ||||||||||||||||||
649 | ** routine operation. Class members not in this block are either fixed | - | ||||||||||||||||||
650 | ** when the pager is first created or else only change when there is a | - | ||||||||||||||||||
651 | ** significant mode change (such as changing the page_size, locking_mode, | - | ||||||||||||||||||
652 | ** or the journal_mode). From another view, these class members describe | - | ||||||||||||||||||
653 | ** the "state" of the pager, while other class members describe the | - | ||||||||||||||||||
654 | ** "configuration" of the pager. | - | ||||||||||||||||||
655 | */ | - | ||||||||||||||||||
656 | u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */ | - | ||||||||||||||||||
657 | u8 eLock; /* Current lock held on database file */ | - | ||||||||||||||||||
658 | u8 changeCountDone; /* Set after incrementing the change-counter */ | - | ||||||||||||||||||
659 | u8 setMaster; /* True if a m-j name has been written to jrnl */ | - | ||||||||||||||||||
660 | u8 doNotSpill; /* Do not spill the cache when non-zero */ | - | ||||||||||||||||||
661 | u8 subjInMemory; /* True to use in-memory sub-journals */ | - | ||||||||||||||||||
662 | u8 bUseFetch; /* True to use xFetch() */ | - | ||||||||||||||||||
663 | u8 hasHeldSharedLock; /* True if a shared lock has ever been held */ | - | ||||||||||||||||||
664 | Pgno dbSize; /* Number of pages in the database */ | - | ||||||||||||||||||
665 | Pgno dbOrigSize; /* dbSize before the current transaction */ | - | ||||||||||||||||||
666 | Pgno dbFileSize; /* Number of pages in the database file */ | - | ||||||||||||||||||
667 | Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */ | - | ||||||||||||||||||
668 | int errCode; /* One of several kinds of errors */ | - | ||||||||||||||||||
669 | int nRec; /* Pages journalled since last j-header written */ | - | ||||||||||||||||||
670 | u32 cksumInit; /* Quasi-random value added to every checksum */ | - | ||||||||||||||||||
671 | u32 nSubRec; /* Number of records written to sub-journal */ | - | ||||||||||||||||||
672 | Bitvec *pInJournal; /* One bit for each page in the database file */ | - | ||||||||||||||||||
673 | sqlite3_file *fd; /* File descriptor for database */ | - | ||||||||||||||||||
674 | sqlite3_file *jfd; /* File descriptor for main journal */ | - | ||||||||||||||||||
675 | sqlite3_file *sjfd; /* File descriptor for sub-journal */ | - | ||||||||||||||||||
676 | i64 journalOff; /* Current write offset in the journal file */ | - | ||||||||||||||||||
677 | i64 journalHdr; /* Byte offset to previous journal header */ | - | ||||||||||||||||||
678 | sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */ | - | ||||||||||||||||||
679 | PagerSavepoint *aSavepoint; /* Array of active savepoints */ | - | ||||||||||||||||||
680 | int nSavepoint; /* Number of elements in aSavepoint[] */ | - | ||||||||||||||||||
681 | u32 iDataVersion; /* Changes whenever database content changes */ | - | ||||||||||||||||||
682 | char dbFileVers[16]; /* Changes whenever database file changes */ | - | ||||||||||||||||||
683 | - | |||||||||||||||||||
684 | int nMmapOut; /* Number of mmap pages currently outstanding */ | - | ||||||||||||||||||
685 | sqlite3_int64 szMmap; /* Desired maximum mmap size */ | - | ||||||||||||||||||
686 | PgHdr *pMmapFreelist; /* List of free mmap page headers (pDirty) */ | - | ||||||||||||||||||
687 | /* | - | ||||||||||||||||||
688 | ** End of the routinely-changing class members | - | ||||||||||||||||||
689 | ***************************************************************************/ | - | ||||||||||||||||||
690 | - | |||||||||||||||||||
691 | u16 nExtra; /* Add this many bytes to each in-memory page */ | - | ||||||||||||||||||
692 | i16 nReserve; /* Number of unused bytes at end of each page */ | - | ||||||||||||||||||
693 | u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */ | - | ||||||||||||||||||
694 | u32 sectorSize; /* Assumed sector size during rollback */ | - | ||||||||||||||||||
695 | int pageSize; /* Number of bytes in a page */ | - | ||||||||||||||||||
696 | Pgno mxPgno; /* Maximum allowed size of the database */ | - | ||||||||||||||||||
697 | i64 journalSizeLimit; /* Size limit for persistent journal files */ | - | ||||||||||||||||||
698 | char *zFilename; /* Name of the database file */ | - | ||||||||||||||||||
699 | char *zJournal; /* Name of the journal file */ | - | ||||||||||||||||||
700 | int (*xBusyHandler)(void*); /* Function to call when busy */ | - | ||||||||||||||||||
701 | void *pBusyHandlerArg; /* Context argument for xBusyHandler */ | - | ||||||||||||||||||
702 | int aStat[4]; /* Total cache hits, misses, writes, spills */ | - | ||||||||||||||||||
703 | #ifdef SQLITE_TEST | - | ||||||||||||||||||
704 | int nRead; /* Database pages read */ | - | ||||||||||||||||||
705 | #endif | - | ||||||||||||||||||
706 | void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */ | - | ||||||||||||||||||
707 | int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */ | - | ||||||||||||||||||
708 | #ifdef SQLITE_HAS_CODEC | - | ||||||||||||||||||
709 | void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */ | - | ||||||||||||||||||
710 | void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */ | - | ||||||||||||||||||
711 | void (*xCodecFree)(void*); /* Destructor for the codec */ | - | ||||||||||||||||||
712 | void *pCodec; /* First argument to xCodec... methods */ | - | ||||||||||||||||||
713 | #endif | - | ||||||||||||||||||
714 | char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */ | - | ||||||||||||||||||
715 | PCache *pPCache; /* Pointer to page cache object */ | - | ||||||||||||||||||
716 | #ifndef SQLITE_OMIT_WAL | - | ||||||||||||||||||
717 | Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */ | - | ||||||||||||||||||
718 | char *zWal; /* File name for write-ahead log */ | - | ||||||||||||||||||
719 | #endif | - | ||||||||||||||||||
720 | }; | - | ||||||||||||||||||
721 | - | |||||||||||||||||||
722 | /* | - | ||||||||||||||||||
723 | ** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains | - | ||||||||||||||||||
724 | ** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS | - | ||||||||||||||||||
725 | ** or CACHE_WRITE to sqlite3_db_status(). | - | ||||||||||||||||||
726 | */ | - | ||||||||||||||||||
727 | #define PAGER_STAT_HIT 0 | - | ||||||||||||||||||
728 | #define PAGER_STAT_MISS 1 | - | ||||||||||||||||||
729 | #define PAGER_STAT_WRITE 2 | - | ||||||||||||||||||
730 | #define PAGER_STAT_SPILL 3 | - | ||||||||||||||||||
731 | - | |||||||||||||||||||
732 | /* | - | ||||||||||||||||||
733 | ** The following global variables hold counters used for | - | ||||||||||||||||||
734 | ** testing purposes only. These variables do not exist in | - | ||||||||||||||||||
735 | ** a non-testing build. These variables are not thread-safe. | - | ||||||||||||||||||
736 | */ | - | ||||||||||||||||||
737 | #ifdef SQLITE_TEST | - | ||||||||||||||||||
738 | int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */ | - | ||||||||||||||||||
739 | int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */ | - | ||||||||||||||||||
740 | int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */ | - | ||||||||||||||||||
741 | # define PAGER_INCR(v) v++ | - | ||||||||||||||||||
742 | #else | - | ||||||||||||||||||
743 | # define PAGER_INCR(v) | - | ||||||||||||||||||
744 | #endif | - | ||||||||||||||||||
745 | - | |||||||||||||||||||
746 | - | |||||||||||||||||||
747 | - | |||||||||||||||||||
748 | /* | - | ||||||||||||||||||
749 | ** Journal files begin with the following magic string. The data | - | ||||||||||||||||||
750 | ** was obtained from /dev/random. It is used only as a sanity check. | - | ||||||||||||||||||
751 | ** | - | ||||||||||||||||||
752 | ** Since version 2.8.0, the journal format contains additional sanity | - | ||||||||||||||||||
753 | ** checking information. If the power fails while the journal is being | - | ||||||||||||||||||
754 | ** written, semi-random garbage data might appear in the journal | - | ||||||||||||||||||
755 | ** file after power is restored. If an attempt is then made | - | ||||||||||||||||||
756 | ** to roll the journal back, the database could be corrupted. The additional | - | ||||||||||||||||||
757 | ** sanity checking data is an attempt to discover the garbage in the | - | ||||||||||||||||||
758 | ** journal and ignore it. | - | ||||||||||||||||||
759 | ** | - | ||||||||||||||||||
760 | ** The sanity checking information for the new journal format consists | - | ||||||||||||||||||
761 | ** of a 32-bit checksum on each page of data. The checksum covers both | - | ||||||||||||||||||
762 | ** the page number and the pPager->pageSize bytes of data for the page. | - | ||||||||||||||||||
763 | ** This cksum is initialized to a 32-bit random value that appears in the | - | ||||||||||||||||||
764 | ** journal file right after the header. The random initializer is important, | - | ||||||||||||||||||
765 | ** because garbage data that appears at the end of a journal is likely | - | ||||||||||||||||||
766 | ** data that was once in other files that have now been deleted. If the | - | ||||||||||||||||||
767 | ** garbage data came from an obsolete journal file, the checksums might | - | ||||||||||||||||||
768 | ** be correct. But by initializing the checksum to random value which | - | ||||||||||||||||||
769 | ** is different for every journal, we minimize that risk. | - | ||||||||||||||||||
770 | */ | - | ||||||||||||||||||
771 | static const unsigned char aJournalMagic[] = { | - | ||||||||||||||||||
772 | 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7, | - | ||||||||||||||||||
773 | }; | - | ||||||||||||||||||
774 | - | |||||||||||||||||||
775 | /* | - | ||||||||||||||||||
776 | ** The size of the of each page record in the journal is given by | - | ||||||||||||||||||
777 | ** the following macro. | - | ||||||||||||||||||
778 | */ | - | ||||||||||||||||||
779 | #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8) | - | ||||||||||||||||||
780 | - | |||||||||||||||||||
781 | /* | - | ||||||||||||||||||
782 | ** The journal header size for this pager. This is usually the same | - | ||||||||||||||||||
783 | ** size as a single disk sector. See also setSectorSize(). | - | ||||||||||||||||||
784 | */ | - | ||||||||||||||||||
785 | #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize) | - | ||||||||||||||||||
786 | - | |||||||||||||||||||
787 | /* | - | ||||||||||||||||||
788 | ** The macro MEMDB is true if we are dealing with an in-memory database. | - | ||||||||||||||||||
789 | ** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set, | - | ||||||||||||||||||
790 | ** the value of MEMDB will be a constant and the compiler will optimize | - | ||||||||||||||||||
791 | ** out code that would never execute. | - | ||||||||||||||||||
792 | */ | - | ||||||||||||||||||
793 | #ifdef SQLITE_OMIT_MEMORYDB | - | ||||||||||||||||||
794 | # define MEMDB 0 | - | ||||||||||||||||||
795 | #else | - | ||||||||||||||||||
796 | # define MEMDB pPager->memDb | - | ||||||||||||||||||
797 | #endif | - | ||||||||||||||||||
798 | - | |||||||||||||||||||
799 | /* | - | ||||||||||||||||||
800 | ** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch | - | ||||||||||||||||||
801 | ** interfaces to access the database using memory-mapped I/O. | - | ||||||||||||||||||
802 | */ | - | ||||||||||||||||||
803 | #if SQLITE_MAX_MMAP_SIZE>0 | - | ||||||||||||||||||
804 | # define USEFETCH(x) ((x)->bUseFetch) | - | ||||||||||||||||||
805 | #else | - | ||||||||||||||||||
806 | # define USEFETCH(x) 0 | - | ||||||||||||||||||
807 | #endif | - | ||||||||||||||||||
808 | - | |||||||||||||||||||
809 | /* | - | ||||||||||||||||||
810 | ** The maximum legal page number is (2^31 - 1). | - | ||||||||||||||||||
811 | */ | - | ||||||||||||||||||
812 | #define PAGER_MAX_PGNO 2147483647 | - | ||||||||||||||||||
813 | - | |||||||||||||||||||
814 | /* | - | ||||||||||||||||||
815 | ** The argument to this macro is a file descriptor (type sqlite3_file*). | - | ||||||||||||||||||
816 | ** Return 0 if it is not open, or non-zero (but not 1) if it is. | - | ||||||||||||||||||
817 | ** | - | ||||||||||||||||||
818 | ** This is so that expressions can be written as: | - | ||||||||||||||||||
819 | ** | - | ||||||||||||||||||
820 | ** if( isOpen(pPager->jfd) ){ ... | - | ||||||||||||||||||
821 | ** | - | ||||||||||||||||||
822 | ** instead of | - | ||||||||||||||||||
823 | ** | - | ||||||||||||||||||
824 | ** if( pPager->jfd->pMethods ){ ... | - | ||||||||||||||||||
825 | */ | - | ||||||||||||||||||
826 | #define isOpen(pFd) ((pFd)->pMethods!=0) | - | ||||||||||||||||||
827 | - | |||||||||||||||||||
828 | /* | - | ||||||||||||||||||
829 | ** Return true if this pager uses a write-ahead log to read page pgno. | - | ||||||||||||||||||
830 | ** Return false if the pager reads pgno directly from the database. | - | ||||||||||||||||||
831 | */ | - | ||||||||||||||||||
832 | #if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_DIRECT_OVERFLOW_READ) | - | ||||||||||||||||||
833 | int sqlite3PagerUseWal(Pager *pPager, Pgno pgno){ | - | ||||||||||||||||||
834 | u32 iRead = 0; | - | ||||||||||||||||||
835 | int rc; | - | ||||||||||||||||||
836 | if( pPager->pWal==0 ) return 0; | - | ||||||||||||||||||
837 | rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead); | - | ||||||||||||||||||
838 | return rc || iRead; | - | ||||||||||||||||||
839 | } | - | ||||||||||||||||||
840 | #endif | - | ||||||||||||||||||
841 | #ifndef SQLITE_OMIT_WAL | - | ||||||||||||||||||
842 | # define pagerUseWal(x) ((x)->pWal!=0) | - | ||||||||||||||||||
843 | #else | - | ||||||||||||||||||
844 | # define pagerUseWal(x) 0 | - | ||||||||||||||||||
845 | # define pagerRollbackWal(x) 0 | - | ||||||||||||||||||
846 | # define pagerWalFrames(v,w,x,y) 0 | - | ||||||||||||||||||
847 | # define pagerOpenWalIfPresent(z) SQLITE_OK | - | ||||||||||||||||||
848 | # define pagerBeginReadTransaction(z) SQLITE_OK | - | ||||||||||||||||||
849 | #endif | - | ||||||||||||||||||
850 | - | |||||||||||||||||||
851 | #ifndef NDEBUG | - | ||||||||||||||||||
852 | /* | - | ||||||||||||||||||
853 | ** Usage: | - | ||||||||||||||||||
854 | ** | - | ||||||||||||||||||
855 | ** assert( assert_pager_state(pPager) ); | - | ||||||||||||||||||
856 | ** | - | ||||||||||||||||||
857 | ** This function runs many asserts to try to find inconsistencies in | - | ||||||||||||||||||
858 | ** the internal state of the Pager object. | - | ||||||||||||||||||
859 | */ | - | ||||||||||||||||||
860 | static int assert_pager_state(Pager *p){ | - | ||||||||||||||||||
861 | Pager *pPager = p; | - | ||||||||||||||||||
862 | - | |||||||||||||||||||
863 | /* State must be valid. */ | - | ||||||||||||||||||
864 | assert( p->eState==PAGER_OPEN | - | ||||||||||||||||||
865 | || p->eState==PAGER_READER | - | ||||||||||||||||||
866 | || p->eState==PAGER_WRITER_LOCKED | - | ||||||||||||||||||
867 | || p->eState==PAGER_WRITER_CACHEMOD | - | ||||||||||||||||||
868 | || p->eState==PAGER_WRITER_DBMOD | - | ||||||||||||||||||
869 | || p->eState==PAGER_WRITER_FINISHED | - | ||||||||||||||||||
870 | || p->eState==PAGER_ERROR | - | ||||||||||||||||||
871 | ); | - | ||||||||||||||||||
872 | - | |||||||||||||||||||
873 | /* Regardless of the current state, a temp-file connection always behaves | - | ||||||||||||||||||
874 | ** as if it has an exclusive lock on the database file. It never updates | - | ||||||||||||||||||
875 | ** the change-counter field, so the changeCountDone flag is always set. | - | ||||||||||||||||||
876 | */ | - | ||||||||||||||||||
877 | assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK ); | - | ||||||||||||||||||
878 | assert( p->tempFile==0 || pPager->changeCountDone ); | - | ||||||||||||||||||
879 | - | |||||||||||||||||||
880 | /* If the useJournal flag is clear, the journal-mode must be "OFF". | - | ||||||||||||||||||
881 | ** And if the journal-mode is "OFF", the journal file must not be open. | - | ||||||||||||||||||
882 | */ | - | ||||||||||||||||||
883 | assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal ); | - | ||||||||||||||||||
884 | assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) ); | - | ||||||||||||||||||
885 | - | |||||||||||||||||||
886 | /* Check that MEMDB implies noSync. And an in-memory journal. Since | - | ||||||||||||||||||
887 | ** this means an in-memory pager performs no IO at all, it cannot encounter | - | ||||||||||||||||||
888 | ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing | - | ||||||||||||||||||
889 | ** a journal file. (although the in-memory journal implementation may | - | ||||||||||||||||||
890 | ** return SQLITE_IOERR_NOMEM while the journal file is being written). It | - | ||||||||||||||||||
891 | ** is therefore not possible for an in-memory pager to enter the ERROR | - | ||||||||||||||||||
892 | ** state. | - | ||||||||||||||||||
893 | */ | - | ||||||||||||||||||
894 | if( MEMDB ){ | - | ||||||||||||||||||
895 | assert( !isOpen(p->fd) ); | - | ||||||||||||||||||
896 | assert( p->noSync ); | - | ||||||||||||||||||
897 | assert( p->journalMode==PAGER_JOURNALMODE_OFF | - | ||||||||||||||||||
898 | || p->journalMode==PAGER_JOURNALMODE_MEMORY | - | ||||||||||||||||||
899 | ); | - | ||||||||||||||||||
900 | assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN ); | - | ||||||||||||||||||
901 | assert( pagerUseWal(p)==0 ); | - | ||||||||||||||||||
902 | } | - | ||||||||||||||||||
903 | - | |||||||||||||||||||
904 | /* If changeCountDone is set, a RESERVED lock or greater must be held | - | ||||||||||||||||||
905 | ** on the file. | - | ||||||||||||||||||
906 | */ | - | ||||||||||||||||||
907 | assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK ); | - | ||||||||||||||||||
908 | assert( p->eLock!=PENDING_LOCK ); | - | ||||||||||||||||||
909 | - | |||||||||||||||||||
910 | switch( p->eState ){ | - | ||||||||||||||||||
911 | case PAGER_OPEN: | - | ||||||||||||||||||
912 | assert( !MEMDB ); | - | ||||||||||||||||||
913 | assert( pPager->errCode==SQLITE_OK ); | - | ||||||||||||||||||
914 | assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile ); | - | ||||||||||||||||||
915 | break; | - | ||||||||||||||||||
916 | - | |||||||||||||||||||
917 | case PAGER_READER: | - | ||||||||||||||||||
918 | assert( pPager->errCode==SQLITE_OK ); | - | ||||||||||||||||||
919 | assert( p->eLock!=UNKNOWN_LOCK ); | - | ||||||||||||||||||
920 | assert( p->eLock>=SHARED_LOCK ); | - | ||||||||||||||||||
921 | break; | - | ||||||||||||||||||
922 | - | |||||||||||||||||||
923 | case PAGER_WRITER_LOCKED: | - | ||||||||||||||||||
924 | assert( p->eLock!=UNKNOWN_LOCK ); | - | ||||||||||||||||||
925 | assert( pPager->errCode==SQLITE_OK ); | - | ||||||||||||||||||
926 | if( !pagerUseWal(pPager) ){ | - | ||||||||||||||||||
927 | assert( p->eLock>=RESERVED_LOCK ); | - | ||||||||||||||||||
928 | } | - | ||||||||||||||||||
929 | assert( pPager->dbSize==pPager->dbOrigSize ); | - | ||||||||||||||||||
930 | assert( pPager->dbOrigSize==pPager->dbFileSize ); | - | ||||||||||||||||||
931 | assert( pPager->dbOrigSize==pPager->dbHintSize ); | - | ||||||||||||||||||
932 | assert( pPager->setMaster==0 ); | - | ||||||||||||||||||
933 | break; | - | ||||||||||||||||||
934 | - | |||||||||||||||||||
935 | case PAGER_WRITER_CACHEMOD: | - | ||||||||||||||||||
936 | assert( p->eLock!=UNKNOWN_LOCK ); | - | ||||||||||||||||||
937 | assert( pPager->errCode==SQLITE_OK ); | - | ||||||||||||||||||
938 | if( !pagerUseWal(pPager) ){ | - | ||||||||||||||||||
939 | /* It is possible that if journal_mode=wal here that neither the | - | ||||||||||||||||||
940 | ** journal file nor the WAL file are open. This happens during | - | ||||||||||||||||||
941 | ** a rollback transaction that switches from journal_mode=off | - | ||||||||||||||||||
942 | ** to journal_mode=wal. | - | ||||||||||||||||||
943 | */ | - | ||||||||||||||||||
944 | assert( p->eLock>=RESERVED_LOCK ); | - | ||||||||||||||||||
945 | assert( isOpen(p->jfd) | - | ||||||||||||||||||
946 | || p->journalMode==PAGER_JOURNALMODE_OFF | - | ||||||||||||||||||
947 | || p->journalMode==PAGER_JOURNALMODE_WAL | - | ||||||||||||||||||
948 | ); | - | ||||||||||||||||||
949 | } | - | ||||||||||||||||||
950 | assert( pPager->dbOrigSize==pPager->dbFileSize ); | - | ||||||||||||||||||
951 | assert( pPager->dbOrigSize==pPager->dbHintSize ); | - | ||||||||||||||||||
952 | break; | - | ||||||||||||||||||
953 | - | |||||||||||||||||||
954 | case PAGER_WRITER_DBMOD: | - | ||||||||||||||||||
955 | assert( p->eLock==EXCLUSIVE_LOCK ); | - | ||||||||||||||||||
956 | assert( pPager->errCode==SQLITE_OK ); | - | ||||||||||||||||||
957 | assert( !pagerUseWal(pPager) ); | - | ||||||||||||||||||
958 | assert( p->eLock>=EXCLUSIVE_LOCK ); | - | ||||||||||||||||||
959 | assert( isOpen(p->jfd) | - | ||||||||||||||||||
960 | || p->journalMode==PAGER_JOURNALMODE_OFF | - | ||||||||||||||||||
961 | || p->journalMode==PAGER_JOURNALMODE_WAL | - | ||||||||||||||||||
962 | || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC) | - | ||||||||||||||||||
963 | ); | - | ||||||||||||||||||
964 | assert( pPager->dbOrigSize<=pPager->dbHintSize ); | - | ||||||||||||||||||
965 | break; | - | ||||||||||||||||||
966 | - | |||||||||||||||||||
967 | case PAGER_WRITER_FINISHED: | - | ||||||||||||||||||
968 | assert( p->eLock==EXCLUSIVE_LOCK ); | - | ||||||||||||||||||
969 | assert( pPager->errCode==SQLITE_OK ); | - | ||||||||||||||||||
970 | assert( !pagerUseWal(pPager) ); | - | ||||||||||||||||||
971 | assert( isOpen(p->jfd) | - | ||||||||||||||||||
972 | || p->journalMode==PAGER_JOURNALMODE_OFF | - | ||||||||||||||||||
973 | || p->journalMode==PAGER_JOURNALMODE_WAL | - | ||||||||||||||||||
974 | || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC) | - | ||||||||||||||||||
975 | ); | - | ||||||||||||||||||
976 | break; | - | ||||||||||||||||||
977 | - | |||||||||||||||||||
978 | case PAGER_ERROR: | - | ||||||||||||||||||
979 | /* There must be at least one outstanding reference to the pager if | - | ||||||||||||||||||
980 | ** in ERROR state. Otherwise the pager should have already dropped | - | ||||||||||||||||||
981 | ** back to OPEN state. | - | ||||||||||||||||||
982 | */ | - | ||||||||||||||||||
983 | assert( pPager->errCode!=SQLITE_OK ); | - | ||||||||||||||||||
984 | assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile ); | - | ||||||||||||||||||
985 | break; | - | ||||||||||||||||||
986 | } | - | ||||||||||||||||||
987 | - | |||||||||||||||||||
988 | return 1; | - | ||||||||||||||||||
989 | } | - | ||||||||||||||||||
990 | #endif /* ifndef NDEBUG */ | - | ||||||||||||||||||
991 | - | |||||||||||||||||||
992 | #ifdef SQLITE_DEBUG | - | ||||||||||||||||||
993 | /* | - | ||||||||||||||||||
994 | ** Return a pointer to a human readable string in a static buffer | - | ||||||||||||||||||
995 | ** containing the state of the Pager object passed as an argument. This | - | ||||||||||||||||||
996 | ** is intended to be used within debuggers. For example, as an alternative | - | ||||||||||||||||||
997 | ** to "print *pPager" in gdb: | - | ||||||||||||||||||
998 | ** | - | ||||||||||||||||||
999 | ** (gdb) printf "%s", print_pager_state(pPager) | - | ||||||||||||||||||
1000 | ** | - | ||||||||||||||||||
1001 | ** This routine has external linkage in order to suppress compiler warnings | - | ||||||||||||||||||
1002 | ** about an unused function. It is enclosed within SQLITE_DEBUG and so does | - | ||||||||||||||||||
1003 | ** not appear in normal builds. | - | ||||||||||||||||||
1004 | */ | - | ||||||||||||||||||
1005 | char *print_pager_state(Pager *p){ | - | ||||||||||||||||||
1006 | static char zRet[1024]; | - | ||||||||||||||||||
1007 | - | |||||||||||||||||||
1008 | sqlite3_snprintf(1024, zRet, | - | ||||||||||||||||||
1009 | "Filename: %s\n" | - | ||||||||||||||||||
1010 | "State: %s errCode=%d\n" | - | ||||||||||||||||||
1011 | "Lock: %s\n" | - | ||||||||||||||||||
1012 | "Locking mode: locking_mode=%s\n" | - | ||||||||||||||||||
1013 | "Journal mode: journal_mode=%s\n" | - | ||||||||||||||||||
1014 | "Backing store: tempFile=%d memDb=%d useJournal=%d\n" | - | ||||||||||||||||||
1015 | "Journal: journalOff=%lld journalHdr=%lld\n" | - | ||||||||||||||||||
1016 | "Size: dbsize=%d dbOrigSize=%d dbFileSize=%d\n" | - | ||||||||||||||||||
1017 | , p->zFilename | - | ||||||||||||||||||
1018 | , p->eState==PAGER_OPEN ? "OPEN" : | - | ||||||||||||||||||
1019 | p->eState==PAGER_READER ? "READER" : | - | ||||||||||||||||||
1020 | p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" : | - | ||||||||||||||||||
1021 | p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" : | - | ||||||||||||||||||
1022 | p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" : | - | ||||||||||||||||||
1023 | p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" : | - | ||||||||||||||||||
1024 | p->eState==PAGER_ERROR ? "ERROR" : "?error?" | - | ||||||||||||||||||
1025 | , (int)p->errCode | - | ||||||||||||||||||
1026 | , p->eLock==NO_LOCK ? "NO_LOCK" : | - | ||||||||||||||||||
1027 | p->eLock==RESERVED_LOCK ? "RESERVED" : | - | ||||||||||||||||||
1028 | p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" : | - | ||||||||||||||||||
1029 | p->eLock==SHARED_LOCK ? "SHARED" : | - | ||||||||||||||||||
1030 | p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?" | - | ||||||||||||||||||
1031 | , p->exclusiveMode ? "exclusive" : "normal" | - | ||||||||||||||||||
1032 | , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" : | - | ||||||||||||||||||
1033 | p->journalMode==PAGER_JOURNALMODE_OFF ? "off" : | - | ||||||||||||||||||
1034 | p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" : | - | ||||||||||||||||||
1035 | p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" : | - | ||||||||||||||||||
1036 | p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" : | - | ||||||||||||||||||
1037 | p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?" | - | ||||||||||||||||||
1038 | , (int)p->tempFile, (int)p->memDb, (int)p->useJournal | - | ||||||||||||||||||
1039 | , p->journalOff, p->journalHdr | - | ||||||||||||||||||
1040 | , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize | - | ||||||||||||||||||
1041 | ); | - | ||||||||||||||||||
1042 | - | |||||||||||||||||||
1043 | return zRet; | - | ||||||||||||||||||
1044 | } | - | ||||||||||||||||||
1045 | #endif | - | ||||||||||||||||||
1046 | - | |||||||||||||||||||
1047 | /* Forward references to the various page getters */ | - | ||||||||||||||||||
1048 | static int getPageNormal(Pager*,Pgno,DbPage**,int); | - | ||||||||||||||||||
1049 | static int getPageError(Pager*,Pgno,DbPage**,int); | - | ||||||||||||||||||
1050 | #if SQLITE_MAX_MMAP_SIZE>0 | - | ||||||||||||||||||
1051 | static int getPageMMap(Pager*,Pgno,DbPage**,int); | - | ||||||||||||||||||
1052 | #endif | - | ||||||||||||||||||
1053 | - | |||||||||||||||||||
1054 | /* | - | ||||||||||||||||||
1055 | ** Set the Pager.xGet method for the appropriate routine used to fetch | - | ||||||||||||||||||
1056 | ** content from the pager. | - | ||||||||||||||||||
1057 | */ | - | ||||||||||||||||||
1058 | static void setGetterMethod(Pager *pPager){ | - | ||||||||||||||||||
1059 | if( pPager->errCode ){
| 60410-254106 | ||||||||||||||||||
1060 | pPager->xGet = getPageError; | - | ||||||||||||||||||
1061 | #if SQLITE_MAX_MMAP_SIZE>0 | - | ||||||||||||||||||
1062 | }else if( USEFETCH(pPager) executed 60410 times by 5 tests: end of block Executed by:
| 224-253882 | ||||||||||||||||||
1063 | #ifdef SQLITE_HAS_CODEC | - | ||||||||||||||||||
1064 | && pPager->xCodec==0 | - | ||||||||||||||||||
1065 | #endif | - | ||||||||||||||||||
1066 | ){ | - | ||||||||||||||||||
1067 | pPager->xGet = getPageMMap; | - | ||||||||||||||||||
1068 | #endif /* SQLITE_MAX_MMAP_SIZE>0 */ | - | ||||||||||||||||||
1069 | }else{ executed 224 times by 2 tests: end of block Executed by:
| 224 | ||||||||||||||||||
1070 | pPager->xGet = getPageNormal; | - | ||||||||||||||||||
1071 | } executed 253882 times by 438 tests: end of block Executed by:
| 253882 | ||||||||||||||||||
1072 | } | - | ||||||||||||||||||
1073 | - | |||||||||||||||||||
1074 | /* | - | ||||||||||||||||||
1075 | ** Return true if it is necessary to write page *pPg into the sub-journal. | - | ||||||||||||||||||
1076 | ** A page needs to be written into the sub-journal if there exists one | - | ||||||||||||||||||
1077 | ** or more open savepoints for which: | - | ||||||||||||||||||
1078 | ** | - | ||||||||||||||||||
1079 | ** * The page-number is less than or equal to PagerSavepoint.nOrig, and | - | ||||||||||||||||||
1080 | ** * The bit corresponding to the page-number is not set in | - | ||||||||||||||||||
1081 | ** PagerSavepoint.pInSavepoint. | - | ||||||||||||||||||
1082 | */ | - | ||||||||||||||||||
1083 | static int subjRequiresPage(PgHdr *pPg){ | - | ||||||||||||||||||
1084 | Pager *pPager = pPg->pPager; | - | ||||||||||||||||||
1085 | PagerSavepoint *p; | - | ||||||||||||||||||
1086 | Pgno pgno = pPg->pgno; | - | ||||||||||||||||||
1087 | int i; | - | ||||||||||||||||||
1088 | for(i=0; i<pPager->nSavepoint; i++){
| 955488-22270187 | ||||||||||||||||||
1089 | p = &pPager->aSavepoint[i]; | - | ||||||||||||||||||
1090 | if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
| 128957-21899078 | ||||||||||||||||||
1091 | return 1; executed 128957 times by 2 tests: return 1; Executed by:
| 128957 | ||||||||||||||||||
1092 | } | - | ||||||||||||||||||
1093 | } executed 22141230 times by 14 tests: end of block Executed by:
| 22141230 | ||||||||||||||||||
1094 | return 0; executed 955488 times by 18 tests: return 0; Executed by:
| 955488 | ||||||||||||||||||
1095 | } | - | ||||||||||||||||||
1096 | - | |||||||||||||||||||
1097 | #ifdef SQLITE_DEBUG | - | ||||||||||||||||||
1098 | /* | - | ||||||||||||||||||
1099 | ** Return true if the page is already in the journal file. | - | ||||||||||||||||||
1100 | */ | - | ||||||||||||||||||
1101 | static int pageInJournal(Pager *pPager, PgHdr *pPg){ | - | ||||||||||||||||||
1102 | return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno); | - | ||||||||||||||||||
1103 | } | - | ||||||||||||||||||
1104 | #endif | - | ||||||||||||||||||
1105 | - | |||||||||||||||||||
1106 | /* | - | ||||||||||||||||||
1107 | ** Read a 32-bit integer from the given file descriptor. Store the integer | - | ||||||||||||||||||
1108 | ** that is read in *pRes. Return SQLITE_OK if everything worked, or an | - | ||||||||||||||||||
1109 | ** error code is something goes wrong. | - | ||||||||||||||||||
1110 | ** | - | ||||||||||||||||||
1111 | ** All values are stored on disk as big-endian. | - | ||||||||||||||||||
1112 | */ | - | ||||||||||||||||||
1113 | static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){ | - | ||||||||||||||||||
1114 | unsigned char ac[4]; | - | ||||||||||||||||||
1115 | int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset); | - | ||||||||||||||||||
1116 | if( rc==SQLITE_OK ){
| 1-521226 | ||||||||||||||||||
1117 | *pRes = sqlite3Get4byte(ac); | - | ||||||||||||||||||
1118 | } executed 521226 times by 12 tests: end of block Executed by:
| 521226 | ||||||||||||||||||
1119 | return rc; executed 521227 times by 12 tests: return rc; Executed by:
| 521227 | ||||||||||||||||||
1120 | } | - | ||||||||||||||||||
1121 | - | |||||||||||||||||||
1122 | /* | - | ||||||||||||||||||
1123 | ** Write a 32-bit integer into a string buffer in big-endian byte order. | - | ||||||||||||||||||
1124 | */ | - | ||||||||||||||||||
1125 | #define put32bits(A,B) sqlite3Put4byte((u8*)A,B) | - | ||||||||||||||||||
1126 | - | |||||||||||||||||||
1127 | - | |||||||||||||||||||
1128 | /* | - | ||||||||||||||||||
1129 | ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK | - | ||||||||||||||||||
1130 | ** on success or an error code is something goes wrong. | - | ||||||||||||||||||
1131 | */ | - | ||||||||||||||||||
1132 | static int write32bits(sqlite3_file *fd, i64 offset, u32 val){ | - | ||||||||||||||||||
1133 | char ac[4]; | - | ||||||||||||||||||
1134 | put32bits(ac, val); | - | ||||||||||||||||||
1135 | return sqlite3OsWrite(fd, ac, 4, offset); executed 839042 times by 377 tests: return sqlite3OsWrite(fd, ac, 4, offset); Executed by:
| 839042 | ||||||||||||||||||
1136 | } | - | ||||||||||||||||||
1137 | - | |||||||||||||||||||
1138 | /* | - | ||||||||||||||||||
1139 | ** Unlock the database file to level eLock, which must be either NO_LOCK | - | ||||||||||||||||||
1140 | ** or SHARED_LOCK. Regardless of whether or not the call to xUnlock() | - | ||||||||||||||||||
1141 | ** succeeds, set the Pager.eLock variable to match the (attempted) new lock. | - | ||||||||||||||||||
1142 | ** | - | ||||||||||||||||||
1143 | ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is | - | ||||||||||||||||||
1144 | ** called, do not modify it. See the comment above the #define of | - | ||||||||||||||||||
1145 | ** UNKNOWN_LOCK for an explanation of this. | - | ||||||||||||||||||
1146 | */ | - | ||||||||||||||||||
1147 | static int pagerUnlockDb(Pager *pPager, int eLock){ | - | ||||||||||||||||||
1148 | int rc = SQLITE_OK; | - | ||||||||||||||||||
1149 | - | |||||||||||||||||||
1150 | assert( !pPager->exclusiveMode || pPager->eLock==eLock ); | - | ||||||||||||||||||
1151 | assert( eLock==NO_LOCK || eLock==SHARED_LOCK ); | - | ||||||||||||||||||
1152 | assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 ); | - | ||||||||||||||||||
1153 | if( isOpen(pPager->fd) ){
| 126780-205445 | ||||||||||||||||||
1154 | assert( pPager->eLock>=eLock ); | - | ||||||||||||||||||
1155 | rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
| 88-205357 | ||||||||||||||||||
1156 | if( pPager->eLock!=UNKNOWN_LOCK ){
| 0-205445 | ||||||||||||||||||
1157 | pPager->eLock = (u8)eLock; | - | ||||||||||||||||||
1158 | } executed 205445 times by 428 tests: end of block Executed by:
| 205445 | ||||||||||||||||||
1159 | IOTRACE(("UNLOCK %p %d\n", pPager, eLock)) | - | ||||||||||||||||||
1160 | } executed 205445 times by 428 tests: end of block Executed by:
| 205445 | ||||||||||||||||||
1161 | return rc; executed 332225 times by 428 tests: return rc; Executed by:
| 332225 | ||||||||||||||||||
1162 | } | - | ||||||||||||||||||
1163 | - | |||||||||||||||||||
1164 | /* | - | ||||||||||||||||||
1165 | ** Lock the database file to level eLock, which must be either SHARED_LOCK, | - | ||||||||||||||||||
1166 | ** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the | - | ||||||||||||||||||
1167 | ** Pager.eLock variable to the new locking state. | - | ||||||||||||||||||
1168 | ** | - | ||||||||||||||||||
1169 | ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is | - | ||||||||||||||||||
1170 | ** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK. | - | ||||||||||||||||||
1171 | ** See the comment above the #define of UNKNOWN_LOCK for an explanation | - | ||||||||||||||||||
1172 | ** of this. | - | ||||||||||||||||||
1173 | */ | - | ||||||||||||||||||
1174 | static int pagerLockDb(Pager *pPager, int eLock){ | - | ||||||||||||||||||
1175 | int rc = SQLITE_OK; | - | ||||||||||||||||||
1176 | - | |||||||||||||||||||
1177 | assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK ); | - | ||||||||||||||||||
1178 | if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
| 0-218643 | ||||||||||||||||||
1179 | rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
| 18-218625 | ||||||||||||||||||
1180 | if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
| 0-215596 | ||||||||||||||||||
1181 | pPager->eLock = (u8)eLock; | - | ||||||||||||||||||
1182 | IOTRACE(("LOCK %p %d\n", pPager, eLock)) | - | ||||||||||||||||||
1183 | } executed 215596 times by 435 tests: end of block Executed by:
| 215596 | ||||||||||||||||||
1184 | } executed 218643 times by 436 tests: end of block Executed by:
| 218643 | ||||||||||||||||||
1185 | return rc; executed 360541 times by 436 tests: return rc; Executed by:
| 360541 | ||||||||||||||||||
1186 | } | - | ||||||||||||||||||
1187 | - | |||||||||||||||||||
1188 | /* | - | ||||||||||||||||||
1189 | ** This function determines whether or not the atomic-write or | - | ||||||||||||||||||
1190 | ** atomic-batch-write optimizations can be used with this pager. The | - | ||||||||||||||||||
1191 | ** atomic-write optimization can be used if: | - | ||||||||||||||||||
1192 | ** | - | ||||||||||||||||||
1193 | ** (a) the value returned by OsDeviceCharacteristics() indicates that | - | ||||||||||||||||||
1194 | ** a database page may be written atomically, and | - | ||||||||||||||||||
1195 | ** (b) the value returned by OsSectorSize() is less than or equal | - | ||||||||||||||||||
1196 | ** to the page size. | - | ||||||||||||||||||
1197 | ** | - | ||||||||||||||||||
1198 | ** If it can be used, then the value returned is the size of the journal | - | ||||||||||||||||||
1199 | ** file when it contains rollback data for exactly one page. | - | ||||||||||||||||||
1200 | ** | - | ||||||||||||||||||
1201 | ** The atomic-batch-write optimization can be used if OsDeviceCharacteristics() | - | ||||||||||||||||||
1202 | ** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is | - | ||||||||||||||||||
1203 | ** returned in this case. | - | ||||||||||||||||||
1204 | ** | - | ||||||||||||||||||
1205 | ** If neither optimization can be used, 0 is returned. | - | ||||||||||||||||||
1206 | */ | - | ||||||||||||||||||
1207 | static int jrnlBufferSize(Pager *pPager){ | - | ||||||||||||||||||
1208 | assert( !MEMDB ); | - | ||||||||||||||||||
1209 | - | |||||||||||||||||||
1210 | #if defined(SQLITE_ENABLE_ATOMIC_WRITE) \ | - | ||||||||||||||||||
1211 | || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) | - | ||||||||||||||||||
1212 | int dc; /* Device characteristics */ | - | ||||||||||||||||||
1213 | - | |||||||||||||||||||
1214 | assert( isOpen(pPager->fd) ); | - | ||||||||||||||||||
1215 | dc = sqlite3OsDeviceCharacteristics(pPager->fd); | - | ||||||||||||||||||
1216 | #else | - | ||||||||||||||||||
1217 | UNUSED_PARAMETER(pPager); | - | ||||||||||||||||||
1218 | #endif | - | ||||||||||||||||||
1219 | - | |||||||||||||||||||
1220 | #ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE | - | ||||||||||||||||||
1221 | if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){ | - | ||||||||||||||||||
1222 | return -1; | - | ||||||||||||||||||
1223 | } | - | ||||||||||||||||||
1224 | #endif | - | ||||||||||||||||||
1225 | - | |||||||||||||||||||
1226 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE | - | ||||||||||||||||||
1227 | { | - | ||||||||||||||||||
1228 | int nSector = pPager->sectorSize; | - | ||||||||||||||||||
1229 | int szPage = pPager->pageSize; | - | ||||||||||||||||||
1230 | - | |||||||||||||||||||
1231 | assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); | - | ||||||||||||||||||
1232 | assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); | - | ||||||||||||||||||
1233 | if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){ | - | ||||||||||||||||||
1234 | return 0; | - | ||||||||||||||||||
1235 | } | - | ||||||||||||||||||
1236 | } | - | ||||||||||||||||||
1237 | - | |||||||||||||||||||
1238 | return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager); | - | ||||||||||||||||||
1239 | #endif | - | ||||||||||||||||||
1240 | - | |||||||||||||||||||
1241 | return 0; executed 40369 times by 380 tests: return 0; Executed by:
| 40369 | ||||||||||||||||||
1242 | } | - | ||||||||||||||||||
1243 | - | |||||||||||||||||||
1244 | /* | - | ||||||||||||||||||
1245 | ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking | - | ||||||||||||||||||
1246 | ** on the cache using a hash function. This is used for testing | - | ||||||||||||||||||
1247 | ** and debugging only. | - | ||||||||||||||||||
1248 | */ | - | ||||||||||||||||||
1249 | #ifdef SQLITE_CHECK_PAGES | - | ||||||||||||||||||
1250 | /* | - | ||||||||||||||||||
1251 | ** Return a 32-bit hash of the page data for pPage. | - | ||||||||||||||||||
1252 | */ | - | ||||||||||||||||||
1253 | static u32 pager_datahash(int nByte, unsigned char *pData){ | - | ||||||||||||||||||
1254 | u32 hash = 0; | - | ||||||||||||||||||
1255 | int i; | - | ||||||||||||||||||
1256 | for(i=0; i<nByte; i++){ | - | ||||||||||||||||||
1257 | hash = (hash*1039) + pData[i]; | - | ||||||||||||||||||
1258 | } | - | ||||||||||||||||||
1259 | return hash; | - | ||||||||||||||||||
1260 | } | - | ||||||||||||||||||
1261 | static u32 pager_pagehash(PgHdr *pPage){ | - | ||||||||||||||||||
1262 | return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData); | - | ||||||||||||||||||
1263 | } | - | ||||||||||||||||||
1264 | static void pager_set_pagehash(PgHdr *pPage){ | - | ||||||||||||||||||
1265 | pPage->pageHash = pager_pagehash(pPage); | - | ||||||||||||||||||
1266 | } | - | ||||||||||||||||||
1267 | - | |||||||||||||||||||
1268 | /* | - | ||||||||||||||||||
1269 | ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES | - | ||||||||||||||||||
1270 | ** is defined, and NDEBUG is not defined, an assert() statement checks | - | ||||||||||||||||||
1271 | ** that the page is either dirty or still matches the calculated page-hash. | - | ||||||||||||||||||
1272 | */ | - | ||||||||||||||||||
1273 | #define CHECK_PAGE(x) checkPage(x) | - | ||||||||||||||||||
1274 | static void checkPage(PgHdr *pPg){ | - | ||||||||||||||||||
1275 | Pager *pPager = pPg->pPager; | - | ||||||||||||||||||
1276 | assert( pPager->eState!=PAGER_ERROR ); | - | ||||||||||||||||||
1277 | assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) ); | - | ||||||||||||||||||
1278 | } | - | ||||||||||||||||||
1279 | - | |||||||||||||||||||
1280 | #else | - | ||||||||||||||||||
1281 | #define pager_datahash(X,Y) 0 | - | ||||||||||||||||||
1282 | #define pager_pagehash(X) 0 | - | ||||||||||||||||||
1283 | #define pager_set_pagehash(X) | - | ||||||||||||||||||
1284 | #define CHECK_PAGE(x) | - | ||||||||||||||||||
1285 | #endif /* SQLITE_CHECK_PAGES */ | - | ||||||||||||||||||
1286 | - | |||||||||||||||||||
1287 | /* | - | ||||||||||||||||||
1288 | ** When this is called the journal file for pager pPager must be open. | - | ||||||||||||||||||
1289 | ** This function attempts to read a master journal file name from the | - | ||||||||||||||||||
1290 | ** end of the file and, if successful, copies it into memory supplied | - | ||||||||||||||||||
1291 | ** by the caller. See comments above writeMasterJournal() for the format | - | ||||||||||||||||||
1292 | ** used to store a master journal file name at the end of a journal file. | - | ||||||||||||||||||
1293 | ** | - | ||||||||||||||||||
1294 | ** zMaster must point to a buffer of at least nMaster bytes allocated by | - | ||||||||||||||||||
1295 | ** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is | - | ||||||||||||||||||
1296 | ** enough space to write the master journal name). If the master journal | - | ||||||||||||||||||
1297 | ** name in the journal is longer than nMaster bytes (including a | - | ||||||||||||||||||
1298 | ** nul-terminator), then this is handled as if no master journal name | - | ||||||||||||||||||
1299 | ** were present in the journal. | - | ||||||||||||||||||
1300 | ** | - | ||||||||||||||||||
1301 | ** If a master journal file name is present at the end of the journal | - | ||||||||||||||||||
1302 | ** file, then it is copied into the buffer pointed to by zMaster. A | - | ||||||||||||||||||
1303 | ** nul-terminator byte is appended to the buffer following the master | - | ||||||||||||||||||
1304 | ** journal file name. | - | ||||||||||||||||||
1305 | ** | - | ||||||||||||||||||
1306 | ** If it is determined that no master journal file name is present | - | ||||||||||||||||||
1307 | ** zMaster[0] is set to 0 and SQLITE_OK returned. | - | ||||||||||||||||||
1308 | ** | - | ||||||||||||||||||
1309 | ** If an error occurs while reading from the journal file, an SQLite | - | ||||||||||||||||||
1310 | ** error code is returned. | - | ||||||||||||||||||
1311 | */ | - | ||||||||||||||||||
1312 | static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){ | - | ||||||||||||||||||
1313 | int rc; /* Return code */ | - | ||||||||||||||||||
1314 | u32 len; /* Length in bytes of master journal name */ | - | ||||||||||||||||||
1315 | i64 szJ; /* Total size in bytes of journal file pJrnl */ | - | ||||||||||||||||||
1316 | u32 cksum; /* MJ checksum value read from journal */ | - | ||||||||||||||||||
1317 | u32 u; /* Unsigned loop counter */ | - | ||||||||||||||||||
1318 | unsigned char aMagic[8]; /* A buffer to hold the magic header */ | - | ||||||||||||||||||
1319 | zMaster[0] = '\0'; | - | ||||||||||||||||||
1320 | - | |||||||||||||||||||
1321 | if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
| 0-6500 | ||||||||||||||||||
1322 | || szJ<16
| 3-6497 | ||||||||||||||||||
1323 | || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
| 0-6497 | ||||||||||||||||||
1324 | || len>=nMaster
| 3118-3379 | ||||||||||||||||||
1325 | || len>szJ-16
| 0-3379 | ||||||||||||||||||
1326 | || len==0
| 145-3234 | ||||||||||||||||||
1327 | || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
| 0-145 | ||||||||||||||||||
1328 | || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
| 0-145 | ||||||||||||||||||
1329 | || memcmp(aMagic, aJournalMagic, 8)
| 26-119 | ||||||||||||||||||
1330 | || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
| 0-119 | ||||||||||||||||||
1331 | ){ | - | ||||||||||||||||||
1332 | return rc; executed 6381 times by 12 tests: return rc; Executed by:
| 6381 | ||||||||||||||||||
1333 | } | - | ||||||||||||||||||
1334 | - | |||||||||||||||||||
1335 | /* See if the checksum matches the master journal name */ | - | ||||||||||||||||||
1336 | for(u=0; u<len; u++){
| 119-25420 | ||||||||||||||||||
1337 | cksum -= zMaster[u]; | - | ||||||||||||||||||
1338 | } executed 25420 times by 1 test: end of block Executed by:
| 25420 | ||||||||||||||||||
1339 | if( cksum ){
| 4-115 | ||||||||||||||||||
1340 | /* If the checksum doesn't add up, then one or more of the disk sectors | - | ||||||||||||||||||
1341 | ** containing the master journal filename is corrupted. This means | - | ||||||||||||||||||
1342 | ** definitely roll back, so just return SQLITE_OK and report a (nul) | - | ||||||||||||||||||
1343 | ** master-journal filename. | - | ||||||||||||||||||
1344 | */ | - | ||||||||||||||||||
1345 | len = 0; | - | ||||||||||||||||||
1346 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||
1347 | zMaster[len] = '\0'; | - | ||||||||||||||||||
1348 | - | |||||||||||||||||||
1349 | return SQLITE_OK; executed 119 times by 1 test: return 0; Executed by:
| 119 | ||||||||||||||||||
1350 | } | - | ||||||||||||||||||
1351 | - | |||||||||||||||||||
1352 | /* | - | ||||||||||||||||||
1353 | ** Return the offset of the sector boundary at or immediately | - | ||||||||||||||||||
1354 | ** following the value in pPager->journalOff, assuming a sector | - | ||||||||||||||||||
1355 | ** size of pPager->sectorSize bytes. | - | ||||||||||||||||||
1356 | ** | - | ||||||||||||||||||
1357 | ** i.e for a sector size of 512: | - | ||||||||||||||||||
1358 | ** | - | ||||||||||||||||||
1359 | ** Pager.journalOff Return value | - | ||||||||||||||||||
1360 | ** --------------------------------------- | - | ||||||||||||||||||
1361 | ** 0 0 | - | ||||||||||||||||||
1362 | ** 512 512 | - | ||||||||||||||||||
1363 | ** 100 512 | - | ||||||||||||||||||
1364 | ** 2000 2048 | - | ||||||||||||||||||
1365 | ** | - | ||||||||||||||||||
1366 | */ | - | ||||||||||||||||||
1367 | static i64 journalHdrOffset(Pager *pPager){ | - | ||||||||||||||||||
1368 | i64 offset = 0; | - | ||||||||||||||||||
1369 | i64 c = pPager->journalOff; | - | ||||||||||||||||||
1370 | if( c ){
| 48578-90873 | ||||||||||||||||||
1371 | offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager); | - | ||||||||||||||||||
1372 | } executed 90873 times by 133 tests: end of block Executed by:
| 90873 | ||||||||||||||||||
1373 | assert( offset%JOURNAL_HDR_SZ(pPager)==0 ); | - | ||||||||||||||||||
1374 | assert( offset>=c ); | - | ||||||||||||||||||
1375 | assert( (offset-c)<JOURNAL_HDR_SZ(pPager) ); | - | ||||||||||||||||||
1376 | return offset; executed 139451 times by 380 tests: return offset; Executed by:
| 139451 | ||||||||||||||||||
1377 | } | - | ||||||||||||||||||
1378 | - | |||||||||||||||||||
1379 | /* | - | ||||||||||||||||||
1380 | ** The journal file must be open when this function is called. | - | ||||||||||||||||||
1381 | ** | - | ||||||||||||||||||
1382 | ** This function is a no-op if the journal file has not been written to | - | ||||||||||||||||||
1383 | ** within the current transaction (i.e. if Pager.journalOff==0). | - | ||||||||||||||||||
1384 | ** | - | ||||||||||||||||||
1385 | ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is | - | ||||||||||||||||||
1386 | ** set to 0, then truncate the journal file to zero bytes in size. Otherwise, | - | ||||||||||||||||||
1387 | ** zero the 28-byte header at the start of the journal file. In either case, | - | ||||||||||||||||||
1388 | ** if the pager is not in no-sync mode, sync the journal file immediately | - | ||||||||||||||||||
1389 | ** after writing or truncating it. | - | ||||||||||||||||||
1390 | ** | - | ||||||||||||||||||
1391 | ** If Pager.journalSizeLimit is set to a positive, non-zero value, and | - | ||||||||||||||||||
1392 | ** following the truncation or zeroing described above the size of the | - | ||||||||||||||||||
1393 | ** journal file in bytes is larger than this value, then truncate the | - | ||||||||||||||||||
1394 | ** journal file to Pager.journalSizeLimit bytes. The journal file does | - | ||||||||||||||||||
1395 | ** not need to be synced following this operation. | - | ||||||||||||||||||
1396 | ** | - | ||||||||||||||||||
1397 | ** If an IO error occurs, abandon processing and return the IO error code. | - | ||||||||||||||||||
1398 | ** Otherwise, return SQLITE_OK. | - | ||||||||||||||||||
1399 | */ | - | ||||||||||||||||||
1400 | static int zeroJournalHdr(Pager *pPager, int doTruncate){ | - | ||||||||||||||||||
1401 | int rc = SQLITE_OK; /* Return code */ | - | ||||||||||||||||||
1402 | assert( isOpen(pPager->jfd) ); | - | ||||||||||||||||||
1403 | assert( !sqlite3JournalIsInMemory(pPager->jfd) ); | - | ||||||||||||||||||
1404 | if( pPager->journalOff ){
| 4-554 | ||||||||||||||||||
1405 | const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */ | - | ||||||||||||||||||
1406 | - | |||||||||||||||||||
1407 | IOTRACE(("JZEROHDR %p\n", pPager)) | - | ||||||||||||||||||
1408 | if( doTruncate || iLimit==0 ){
| 1-425 | ||||||||||||||||||
1409 | rc = sqlite3OsTruncate(pPager->jfd, 0); | - | ||||||||||||||||||
1410 | }else{ executed 130 times by 1 test: end of block Executed by:
| 130 | ||||||||||||||||||
1411 | static const char zeroHdr[28] = {0}; | - | ||||||||||||||||||
1412 | rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0); | - | ||||||||||||||||||
1413 | } executed 424 times by 3 tests: end of block Executed by:
| 424 | ||||||||||||||||||
1414 | if( rc==SQLITE_OK && !pPager->noSync ){
| 1-553 | ||||||||||||||||||
1415 | rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags); | - | ||||||||||||||||||
1416 | } executed 438 times by 3 tests: end of block Executed by:
| 438 | ||||||||||||||||||
1417 | - | |||||||||||||||||||
1418 | /* At this point the transaction is committed but the write lock | - | ||||||||||||||||||
1419 | ** is still held on the file. If there is a size limit configured for | - | ||||||||||||||||||
1420 | ** the persistent journal and the journal file currently consumes more | - | ||||||||||||||||||
1421 | ** space than that limit allows for, truncate it now. There is no need | - | ||||||||||||||||||
1422 | ** to sync the file following this operation. | - | ||||||||||||||||||
1423 | */ | - | ||||||||||||||||||
1424 | if( rc==SQLITE_OK && iLimit>0 ){
| 2-552 | ||||||||||||||||||
1425 | i64 sz; | - | ||||||||||||||||||
1426 | rc = sqlite3OsFileSize(pPager->jfd, &sz); | - | ||||||||||||||||||
1427 | if( rc==SQLITE_OK && sz>iLimit ){
| 0-6 | ||||||||||||||||||
1428 | rc = sqlite3OsTruncate(pPager->jfd, iLimit); | - | ||||||||||||||||||
1429 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
1430 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||||||||
1431 | } executed 554 times by 3 tests: end of block Executed by:
| 554 | ||||||||||||||||||
1432 | return rc; executed 558 times by 3 tests: return rc; Executed by:
| 558 | ||||||||||||||||||
1433 | } | - | ||||||||||||||||||
1434 | - | |||||||||||||||||||
1435 | /* | - | ||||||||||||||||||
1436 | ** The journal file must be open when this routine is called. A journal | - | ||||||||||||||||||
1437 | ** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the | - | ||||||||||||||||||
1438 | ** current location. | - | ||||||||||||||||||
1439 | ** | - | ||||||||||||||||||
1440 | ** The format for the journal header is as follows: | - | ||||||||||||||||||
1441 | ** - 8 bytes: Magic identifying journal format. | - | ||||||||||||||||||
1442 | ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on. | - | ||||||||||||||||||
1443 | ** - 4 bytes: Random number used for page hash. | - | ||||||||||||||||||
1444 | ** - 4 bytes: Initial database page count. | - | ||||||||||||||||||
1445 | ** - 4 bytes: Sector size used by the process that wrote this journal. | - | ||||||||||||||||||
1446 | ** - 4 bytes: Database page size. | - | ||||||||||||||||||
1447 | ** | - | ||||||||||||||||||
1448 | ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space. | - | ||||||||||||||||||
1449 | */ | - | ||||||||||||||||||
1450 | static int writeJournalHdr(Pager *pPager){ | - | ||||||||||||||||||
1451 | int rc = SQLITE_OK; /* Return code */ | - | ||||||||||||||||||
1452 | char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */ | - | ||||||||||||||||||
1453 | u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */ | - | ||||||||||||||||||
1454 | u32 nWrite; /* Bytes of header sector written */ | - | ||||||||||||||||||
1455 | int ii; /* Loop counter */ | - | ||||||||||||||||||
1456 | - | |||||||||||||||||||
1457 | assert( isOpen(pPager->jfd) ); /* Journal file must be open. */ | - | ||||||||||||||||||
1458 | - | |||||||||||||||||||
1459 | if( nHeader>JOURNAL_HDR_SZ(pPager) ){
| 934-60161 | ||||||||||||||||||
1460 | nHeader = JOURNAL_HDR_SZ(pPager); | - | ||||||||||||||||||
1461 | } executed 60161 times by 358 tests: end of block Executed by:
| 60161 | ||||||||||||||||||
1462 | - | |||||||||||||||||||
1463 | /* If there are active savepoints and any of them were created | - | ||||||||||||||||||
1464 | ** since the most recent journal header was written, update the | - | ||||||||||||||||||
1465 | ** PagerSavepoint.iHdrOffset fields now. | - | ||||||||||||||||||
1466 | */ | - | ||||||||||||||||||
1467 | for(ii=0; ii<pPager->nSavepoint; ii++){
| 13913-61095 | ||||||||||||||||||
1468 | if( pPager->aSavepoint[ii].iHdrOffset==0 ){
| 2202-11711 | ||||||||||||||||||
1469 | pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff; | - | ||||||||||||||||||
1470 | } executed 2202 times by 14 tests: end of block Executed by:
| 2202 | ||||||||||||||||||
1471 | } executed 13913 times by 14 tests: end of block Executed by:
| 13913 | ||||||||||||||||||
1472 | - | |||||||||||||||||||
1473 | pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager); | - | ||||||||||||||||||
1474 | - | |||||||||||||||||||
1475 | /* | - | ||||||||||||||||||
1476 | ** Write the nRec Field - the number of page records that follow this | - | ||||||||||||||||||
1477 | ** journal header. Normally, zero is written to this value at this time. | - | ||||||||||||||||||
1478 | ** After the records are added to the journal (and the journal synced, | - | ||||||||||||||||||
1479 | ** if in full-sync mode), the zero is overwritten with the true number | - | ||||||||||||||||||
1480 | ** of records (see syncJournal()). | - | ||||||||||||||||||
1481 | ** | - | ||||||||||||||||||
1482 | ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When | - | ||||||||||||||||||
1483 | ** reading the journal this value tells SQLite to assume that the | - | ||||||||||||||||||
1484 | ** rest of the journal file contains valid page records. This assumption | - | ||||||||||||||||||
1485 | ** is dangerous, as if a failure occurred whilst writing to the journal | - | ||||||||||||||||||
1486 | ** file it may contain some garbage data. There are two scenarios | - | ||||||||||||||||||
1487 | ** where this risk can be ignored: | - | ||||||||||||||||||
1488 | ** | - | ||||||||||||||||||
1489 | ** * When the pager is in no-sync mode. Corruption can follow a | - | ||||||||||||||||||
1490 | ** power failure in this case anyway. | - | ||||||||||||||||||
1491 | ** | - | ||||||||||||||||||
1492 | ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees | - | ||||||||||||||||||
1493 | ** that garbage data is never appended to the journal file. | - | ||||||||||||||||||
1494 | */ | - | ||||||||||||||||||
1495 | assert( isOpen(pPager->fd) || pPager->noSync ); | - | ||||||||||||||||||
1496 | if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
| 108-56208 | ||||||||||||||||||
1497 | || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
| 27-56073 | ||||||||||||||||||
1498 | ){ | - | ||||||||||||||||||
1499 | memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic)); | - | ||||||||||||||||||
1500 | put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff); | - | ||||||||||||||||||
1501 | }else{ executed 5022 times by 2 tests: end of block Executed by:
| 5022 | ||||||||||||||||||
1502 | memset(zHeader, 0, sizeof(aJournalMagic)+4); | - | ||||||||||||||||||
1503 | } executed 56073 times by 380 tests: end of block Executed by:
| 56073 | ||||||||||||||||||
1504 | - | |||||||||||||||||||
1505 | /* The random check-hash initializer */ | - | ||||||||||||||||||
1506 | sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit); | - | ||||||||||||||||||
1507 | put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit); | - | ||||||||||||||||||
1508 | /* The initial database size */ | - | ||||||||||||||||||
1509 | put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize); | - | ||||||||||||||||||
1510 | /* The assumed sector size for this process */ | - | ||||||||||||||||||
1511 | put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize); | - | ||||||||||||||||||
1512 | - | |||||||||||||||||||
1513 | /* The page size */ | - | ||||||||||||||||||
1514 | put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize); | - | ||||||||||||||||||
1515 | - | |||||||||||||||||||
1516 | /* Initializing the tail of the buffer is not necessary. Everything | - | ||||||||||||||||||
1517 | ** works find if the following memset() is omitted. But initializing | - | ||||||||||||||||||
1518 | ** the memory prevents valgrind from complaining, so we are willing to | - | ||||||||||||||||||
1519 | ** take the performance hit. | - | ||||||||||||||||||
1520 | */ | - | ||||||||||||||||||
1521 | memset(&zHeader[sizeof(aJournalMagic)+20], 0, | - | ||||||||||||||||||
1522 | nHeader-(sizeof(aJournalMagic)+20)); | - | ||||||||||||||||||
1523 | - | |||||||||||||||||||
1524 | /* In theory, it is only necessary to write the 28 bytes that the | - | ||||||||||||||||||
1525 | ** journal header consumes to the journal file here. Then increment the | - | ||||||||||||||||||
1526 | ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next | - | ||||||||||||||||||
1527 | ** record is written to the following sector (leaving a gap in the file | - | ||||||||||||||||||
1528 | ** that will be implicitly filled in by the OS). | - | ||||||||||||||||||
1529 | ** | - | ||||||||||||||||||
1530 | ** However it has been discovered that on some systems this pattern can | - | ||||||||||||||||||
1531 | ** be significantly slower than contiguously writing data to the file, | - | ||||||||||||||||||
1532 | ** even if that means explicitly writing data to the block of | - | ||||||||||||||||||
1533 | ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what | - | ||||||||||||||||||
1534 | ** is done. | - | ||||||||||||||||||
1535 | ** | - | ||||||||||||||||||
1536 | ** The loop is required here in case the sector-size is larger than the | - | ||||||||||||||||||
1537 | ** database page size. Since the zHeader buffer is only Pager.pageSize | - | ||||||||||||||||||
1538 | ** bytes in size, more than one call to sqlite3OsWrite() may be required | - | ||||||||||||||||||
1539 | ** to populate the entire journal header sector. | - | ||||||||||||||||||
1540 | */ | - | ||||||||||||||||||
1541 | for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
| 16-128403 | ||||||||||||||||||
1542 | IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader)) | - | ||||||||||||||||||
1543 | rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff); | - | ||||||||||||||||||
1544 | assert( pPager->journalHdr <= pPager->journalOff ); | - | ||||||||||||||||||
1545 | pPager->journalOff += nHeader; | - | ||||||||||||||||||
1546 | } executed 67324 times by 379 tests: end of block Executed by:
| 67324 | ||||||||||||||||||
1547 | - | |||||||||||||||||||
1548 | return rc; executed 61094 times by 379 tests: return rc; Executed by:
| 61094 | ||||||||||||||||||
1549 | } | - | ||||||||||||||||||
1550 | - | |||||||||||||||||||
1551 | /* | - | ||||||||||||||||||
1552 | ** The journal file must be open when this is called. A journal header file | - | ||||||||||||||||||
1553 | ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal | - | ||||||||||||||||||
1554 | ** file. The current location in the journal file is given by | - | ||||||||||||||||||
1555 | ** pPager->journalOff. See comments above function writeJournalHdr() for | - | ||||||||||||||||||
1556 | ** a description of the journal header format. | - | ||||||||||||||||||
1557 | ** | - | ||||||||||||||||||
1558 | ** If the header is read successfully, *pNRec is set to the number of | - | ||||||||||||||||||
1559 | ** page records following this header and *pDbSize is set to the size of the | - | ||||||||||||||||||
1560 | ** database before the transaction began, in pages. Also, pPager->cksumInit | - | ||||||||||||||||||
1561 | ** is set to the value read from the journal header. SQLITE_OK is returned | - | ||||||||||||||||||
1562 | ** in this case. | - | ||||||||||||||||||
1563 | ** | - | ||||||||||||||||||
1564 | ** If the journal header file appears to be corrupted, SQLITE_DONE is | - | ||||||||||||||||||
1565 | ** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes | - | ||||||||||||||||||
1566 | ** cannot be read from the journal file an error code is returned. | - | ||||||||||||||||||
1567 | */ | - | ||||||||||||||||||
1568 | static int readJournalHdr( | - | ||||||||||||||||||
1569 | Pager *pPager, /* Pager object */ | - | ||||||||||||||||||
1570 | int isHot, | - | ||||||||||||||||||
1571 | i64 journalSize, /* Size of the open journal file in bytes */ | - | ||||||||||||||||||
1572 | u32 *pNRec, /* OUT: Value read from the nRec field */ | - | ||||||||||||||||||
1573 | u32 *pDbSize /* OUT: Value of original database size field */ | - | ||||||||||||||||||
1574 | ){ | - | ||||||||||||||||||
1575 | int rc; /* Return code */ | - | ||||||||||||||||||
1576 | unsigned char aMagic[8]; /* A buffer to hold the magic header */ | - | ||||||||||||||||||
1577 | i64 iHdrOff; /* Offset of journal header being read */ | - | ||||||||||||||||||
1578 | - | |||||||||||||||||||
1579 | assert( isOpen(pPager->jfd) ); /* Journal file must be open. */ | - | ||||||||||||||||||
1580 | - | |||||||||||||||||||
1581 | /* Advance Pager.journalOff to the start of the next sector. If the | - | ||||||||||||||||||
1582 | ** journal file is too small for there to be a header stored at this | - | ||||||||||||||||||
1583 | ** point, return SQLITE_DONE. | - | ||||||||||||||||||
1584 | */ | - | ||||||||||||||||||
1585 | pPager->journalOff = journalHdrOffset(pPager); | - | ||||||||||||||||||
1586 | if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
| 2887-21481 | ||||||||||||||||||
1587 | return SQLITE_DONE; executed 2887 times by 12 tests: return 101; Executed by:
| 2887 | ||||||||||||||||||
1588 | } | - | ||||||||||||||||||
1589 | iHdrOff = pPager->journalOff; | - | ||||||||||||||||||
1590 | - | |||||||||||||||||||
1591 | /* Read in the first 8 bytes of the journal header. If they do not match | - | ||||||||||||||||||
1592 | ** the magic string found at the start of each journal header, return | - | ||||||||||||||||||
1593 | ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise, | - | ||||||||||||||||||
1594 | ** proceed. | - | ||||||||||||||||||
1595 | */ | - | ||||||||||||||||||
1596 | if( isHot || iHdrOff!=pPager->journalHdr ){
| 2855-16403 | ||||||||||||||||||
1597 | rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff); | - | ||||||||||||||||||
1598 | if( rc ){
| 0-18626 | ||||||||||||||||||
1599 | return rc; never executed: return rc; | 0 | ||||||||||||||||||
1600 | } | - | ||||||||||||||||||
1601 | if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
| 271-18355 | ||||||||||||||||||
1602 | return SQLITE_DONE; executed 271 times by 1 test: return 101; Executed by:
| 271 | ||||||||||||||||||
1603 | } | - | ||||||||||||||||||
1604 | } executed 18355 times by 10 tests: end of block Executed by:
| 18355 | ||||||||||||||||||
1605 | - | |||||||||||||||||||
1606 | /* Read the first three 32-bit fields of the journal header: The nRec | - | ||||||||||||||||||
1607 | ** field, the checksum-initializer and the database size at the start | - | ||||||||||||||||||
1608 | ** of the transaction. Return an error code if anything goes wrong. | - | ||||||||||||||||||
1609 | */ | - | ||||||||||||||||||
1610 | if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
| 0-21210 | ||||||||||||||||||
1611 | || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
| 0-21210 | ||||||||||||||||||
1612 | || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
| 0-21210 | ||||||||||||||||||
1613 | ){ | - | ||||||||||||||||||
1614 | return rc; never executed: return rc; | 0 | ||||||||||||||||||
1615 | } | - | ||||||||||||||||||
1616 | - | |||||||||||||||||||
1617 | if( pPager->journalOff==0 ){
| 3308-17902 | ||||||||||||||||||
1618 | u32 iPageSize; /* Page-size field of journal header */ | - | ||||||||||||||||||
1619 | u32 iSectorSize; /* Sector-size field of journal header */ | - | ||||||||||||||||||
1620 | - | |||||||||||||||||||
1621 | /* Read the page-size and sector-size journal header fields. */ | - | ||||||||||||||||||
1622 | if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
| 0-3308 | ||||||||||||||||||
1623 | || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
| 0-3308 | ||||||||||||||||||
1624 | ){ | - | ||||||||||||||||||
1625 | return rc; never executed: return rc; | 0 | ||||||||||||||||||
1626 | } | - | ||||||||||||||||||
1627 | - | |||||||||||||||||||
1628 | /* Versions of SQLite prior to 3.5.8 set the page-size field of the | - | ||||||||||||||||||
1629 | ** journal header to zero. In this case, assume that the Pager.pageSize | - | ||||||||||||||||||
1630 | ** variable is already set to the correct page size. | - | ||||||||||||||||||
1631 | */ | - | ||||||||||||||||||
1632 | if( iPageSize==0 ){
| 1-3307 | ||||||||||||||||||
1633 | iPageSize = pPager->pageSize; | - | ||||||||||||||||||
1634 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
1635 | - | |||||||||||||||||||
1636 | /* Check that the values read from the page-size and sector-size fields | - | ||||||||||||||||||
1637 | ** are within range. To be 'in range', both values need to be a power | - | ||||||||||||||||||
1638 | ** of two greater than or equal to 512 or 32, and not greater than their | - | ||||||||||||||||||
1639 | ** respective compile time maximum limits. | - | ||||||||||||||||||
1640 | */ | - | ||||||||||||||||||
1641 | if( iPageSize<512 || iSectorSize<32
| 1-3306 | ||||||||||||||||||
1642 | || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
| 2-3303 | ||||||||||||||||||
1643 | || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
| 2-3299 | ||||||||||||||||||
1644 | ){ | - | ||||||||||||||||||
1645 | /* If the either the page-size or sector-size in the journal-header is | - | ||||||||||||||||||
1646 | ** invalid, then the process that wrote the journal-header must have | - | ||||||||||||||||||
1647 | ** crashed before the header was synced. In this case stop reading | - | ||||||||||||||||||
1648 | ** the journal file here. | - | ||||||||||||||||||
1649 | */ | - | ||||||||||||||||||
1650 | return SQLITE_DONE; executed 11 times by 1 test: return 101; Executed by:
| 11 | ||||||||||||||||||
1651 | } | - | ||||||||||||||||||
1652 | - | |||||||||||||||||||
1653 | /* Update the page-size to match the value read from the journal. | - | ||||||||||||||||||
1654 | ** Use a testcase() macro to make sure that malloc failure within | - | ||||||||||||||||||
1655 | ** PagerSetPagesize() is tested. | - | ||||||||||||||||||
1656 | */ | - | ||||||||||||||||||
1657 | rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1); | - | ||||||||||||||||||
1658 | testcase( rc!=SQLITE_OK ); | - | ||||||||||||||||||
1659 | - | |||||||||||||||||||
1660 | /* Update the assumed sector-size to match the value used by | - | ||||||||||||||||||
1661 | ** the process that created this journal. If this journal was | - | ||||||||||||||||||
1662 | ** created by a process other than this one, then this routine | - | ||||||||||||||||||
1663 | ** is being called from within pager_playback(). The local value | - | ||||||||||||||||||
1664 | ** of Pager.sectorSize is restored at the end of that routine. | - | ||||||||||||||||||
1665 | */ | - | ||||||||||||||||||
1666 | pPager->sectorSize = iSectorSize; | - | ||||||||||||||||||
1667 | } executed 3297 times by 12 tests: end of block Executed by:
| 3297 | ||||||||||||||||||
1668 | - | |||||||||||||||||||
1669 | pPager->journalOff += JOURNAL_HDR_SZ(pPager); | - | ||||||||||||||||||
1670 | return rc; executed 21199 times by 12 tests: return rc; Executed by:
| 21199 | ||||||||||||||||||
1671 | } | - | ||||||||||||||||||
1672 | - | |||||||||||||||||||
1673 | - | |||||||||||||||||||
1674 | /* | - | ||||||||||||||||||
1675 | ** Write the supplied master journal name into the journal file for pager | - | ||||||||||||||||||
1676 | ** pPager at the current location. The master journal name must be the last | - | ||||||||||||||||||
1677 | ** thing written to a journal file. If the pager is in full-sync mode, the | - | ||||||||||||||||||
1678 | ** journal file descriptor is advanced to the next sector boundary before | - | ||||||||||||||||||
1679 | ** anything is written. The format is: | - | ||||||||||||||||||
1680 | ** | - | ||||||||||||||||||
1681 | ** + 4 bytes: PAGER_MJ_PGNO. | - | ||||||||||||||||||
1682 | ** + N bytes: Master journal filename in utf-8. | - | ||||||||||||||||||
1683 | ** + 4 bytes: N (length of master journal name in bytes, no nul-terminator). | - | ||||||||||||||||||
1684 | ** + 4 bytes: Master journal name checksum. | - | ||||||||||||||||||
1685 | ** + 8 bytes: aJournalMagic[]. | - | ||||||||||||||||||
1686 | ** | - | ||||||||||||||||||
1687 | ** The master journal page checksum is the sum of the bytes in the master | - | ||||||||||||||||||
1688 | ** journal name, where each byte is interpreted as a signed 8-bit integer. | - | ||||||||||||||||||
1689 | ** | - | ||||||||||||||||||
1690 | ** If zMaster is a NULL pointer (occurs for a single database transaction), | - | ||||||||||||||||||
1691 | ** this call is a no-op. | - | ||||||||||||||||||
1692 | */ | - | ||||||||||||||||||
1693 | static int writeMasterJournal(Pager *pPager, const char *zMaster){ | - | ||||||||||||||||||
1694 | int rc; /* Return code */ | - | ||||||||||||||||||
1695 | int nMaster; /* Length of string zMaster */ | - | ||||||||||||||||||
1696 | i64 iHdrOff; /* Offset of header in journal file */ | - | ||||||||||||||||||
1697 | i64 jrnlSize; /* Size of journal file on disk */ | - | ||||||||||||||||||
1698 | u32 cksum = 0; /* Checksum of string zMaster */ | - | ||||||||||||||||||
1699 | - | |||||||||||||||||||
1700 | assert( pPager->setMaster==0 ); | - | ||||||||||||||||||
1701 | assert( !pagerUseWal(pPager) ); | - | ||||||||||||||||||
1702 | - | |||||||||||||||||||
1703 | if( !zMaster
| 135-38371 | ||||||||||||||||||
1704 | || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
| 0-135 | ||||||||||||||||||
1705 | || !isOpen(pPager->jfd)
| 0-135 | ||||||||||||||||||
1706 | ){ | - | ||||||||||||||||||
1707 | return SQLITE_OK; executed 38371 times by 125 tests: return 0; Executed by:
| 38371 | ||||||||||||||||||
1708 | } | - | ||||||||||||||||||
1709 | pPager->setMaster = 1; | - | ||||||||||||||||||
1710 | assert( pPager->journalHdr <= pPager->journalOff ); | - | ||||||||||||||||||
1711 | - | |||||||||||||||||||
1712 | /* Calculate the length in bytes and the checksum of zMaster */ | - | ||||||||||||||||||
1713 | for(nMaster=0; zMaster[nMaster]; nMaster++){
| 135-15562 | ||||||||||||||||||
1714 | cksum += zMaster[nMaster]; | - | ||||||||||||||||||
1715 | } executed 15562 times by 3 tests: end of block Executed by:
| 15562 | ||||||||||||||||||
1716 | - | |||||||||||||||||||
1717 | /* If in full-sync mode, advance to the next disk sector before writing | - | ||||||||||||||||||
1718 | ** the master journal name. This is in case the previous page written to | - | ||||||||||||||||||
1719 | ** the journal has already been synced. | - | ||||||||||||||||||
1720 | */ | - | ||||||||||||||||||
1721 | if( pPager->fullSync ){
| 15-120 | ||||||||||||||||||
1722 | pPager->journalOff = journalHdrOffset(pPager); | - | ||||||||||||||||||
1723 | } executed 120 times by 3 tests: end of block Executed by:
| 120 | ||||||||||||||||||
1724 | iHdrOff = pPager->journalOff; | - | ||||||||||||||||||
1725 | - | |||||||||||||||||||
1726 | /* Write the master journal data to the end of the journal file. If | - | ||||||||||||||||||
1727 | ** an error occurs, return the error code to the caller. | - | ||||||||||||||||||
1728 | */ | - | ||||||||||||||||||
1729 | if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
| 0-135 | ||||||||||||||||||
1730 | || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
| 0-135 | ||||||||||||||||||
1731 | || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
| 0-135 | ||||||||||||||||||
1732 | || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
| 0-135 | ||||||||||||||||||
1733 | || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
| 0-135 | ||||||||||||||||||
1734 | iHdrOff+4+nMaster+8)))
| 0-135 | ||||||||||||||||||
1735 | ){ | - | ||||||||||||||||||
1736 | return rc; never executed: return rc; | 0 | ||||||||||||||||||
1737 | } | - | ||||||||||||||||||
1738 | pPager->journalOff += (nMaster+20); | - | ||||||||||||||||||
1739 | - | |||||||||||||||||||
1740 | /* If the pager is in peristent-journal mode, then the physical | - | ||||||||||||||||||
1741 | ** journal-file may extend past the end of the master-journal name | - | ||||||||||||||||||
1742 | ** and 8 bytes of magic data just written to the file. This is | - | ||||||||||||||||||
1743 | ** dangerous because the code to rollback a hot-journal file | - | ||||||||||||||||||
1744 | ** will not be able to find the master-journal name to determine | - | ||||||||||||||||||
1745 | ** whether or not the journal is hot. | - | ||||||||||||||||||
1746 | ** | - | ||||||||||||||||||
1747 | ** Easiest thing to do in this scenario is to truncate the journal | - | ||||||||||||||||||
1748 | ** file to the required size. | - | ||||||||||||||||||
1749 | */ | - | ||||||||||||||||||
1750 | if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
| 0-135 | ||||||||||||||||||
1751 | && jrnlSize>pPager->journalOff
| 3-132 | ||||||||||||||||||
1752 | ){ | - | ||||||||||||||||||
1753 | rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff); | - | ||||||||||||||||||
1754 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||||||||
1755 | return rc; executed 135 times by 3 tests: return rc; Executed by:
| 135 | ||||||||||||||||||
1756 | } | - | ||||||||||||||||||
1757 | - | |||||||||||||||||||
1758 | /* | - | ||||||||||||||||||
1759 | ** Discard the entire contents of the in-memory page-cache. | - | ||||||||||||||||||
1760 | */ | - | ||||||||||||||||||
1761 | static void pager_reset(Pager *pPager){ | - | ||||||||||||||||||
1762 | pPager->iDataVersion++; | - | ||||||||||||||||||
1763 | sqlite3BackupRestart(pPager->pBackup); | - | ||||||||||||||||||
1764 | sqlite3PcacheClear(pPager->pPCache); | - | ||||||||||||||||||
1765 | } executed 194667 times by 438 tests: end of block Executed by:
| 194667 | ||||||||||||||||||
1766 | - | |||||||||||||||||||
1767 | /* | - | ||||||||||||||||||
1768 | ** Return the pPager->iDataVersion value | - | ||||||||||||||||||
1769 | */ | - | ||||||||||||||||||
1770 | u32 sqlite3PagerDataVersion(Pager *pPager){ | - | ||||||||||||||||||
1771 | return pPager->iDataVersion; executed 47 times by 1 test: return pPager->iDataVersion; Executed by:
| 47 | ||||||||||||||||||
1772 | } | - | ||||||||||||||||||
1773 | - | |||||||||||||||||||
1774 | /* | - | ||||||||||||||||||
1775 | ** Free all structures in the Pager.aSavepoint[] array and set both | - | ||||||||||||||||||
1776 | ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal | - | ||||||||||||||||||
1777 | ** if it is open and the pager is not in exclusive mode. | - | ||||||||||||||||||
1778 | */ | - | ||||||||||||||||||
1779 | static void releaseAllSavepoints(Pager *pPager){ | - | ||||||||||||||||||
1780 | int ii; /* Iterator for looping through Pager.aSavepoint */ | - | ||||||||||||||||||
1781 | for(ii=0; ii<pPager->nSavepoint; ii++){
| 5924-584296 | ||||||||||||||||||
1782 | sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint); | - | ||||||||||||||||||
1783 | } executed 5924 times by 1 test: end of block Executed by:
| 5924 | ||||||||||||||||||
1784 | if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
| 35-446732 | ||||||||||||||||||
1785 | sqlite3OsClose(pPager->sjfd); | - | ||||||||||||||||||
1786 | } executed 446767 times by 438 tests: end of block Executed by:
| 446767 | ||||||||||||||||||
1787 | sqlite3_free(pPager->aSavepoint); | - | ||||||||||||||||||
1788 | pPager->aSavepoint = 0; | - | ||||||||||||||||||
1789 | pPager->nSavepoint = 0; | - | ||||||||||||||||||
1790 | pPager->nSubRec = 0; | - | ||||||||||||||||||
1791 | } executed 584296 times by 438 tests: end of block Executed by:
| 584296 | ||||||||||||||||||
1792 | - | |||||||||||||||||||
1793 | /* | - | ||||||||||||||||||
1794 | ** Set the bit number pgno in the PagerSavepoint.pInSavepoint | - | ||||||||||||||||||
1795 | ** bitvecs of all open savepoints. Return SQLITE_OK if successful | - | ||||||||||||||||||
1796 | ** or SQLITE_NOMEM if a malloc failure occurs. | - | ||||||||||||||||||
1797 | */ | - | ||||||||||||||||||
1798 | static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){ | - | ||||||||||||||||||
1799 | int ii; /* Loop counter */ | - | ||||||||||||||||||
1800 | int rc = SQLITE_OK; /* Result code */ | - | ||||||||||||||||||
1801 | - | |||||||||||||||||||
1802 | for(ii=0; ii<pPager->nSavepoint; ii++){
| 1319072-16938736 | ||||||||||||||||||
1803 | PagerSavepoint *p = &pPager->aSavepoint[ii]; | - | ||||||||||||||||||
1804 | if( pgno<=p->nOrig ){
| 160471-16778265 | ||||||||||||||||||
1805 | rc |= sqlite3BitvecSet(p->pInSavepoint, pgno); | - | ||||||||||||||||||
1806 | testcase( rc==SQLITE_NOMEM ); | - | ||||||||||||||||||
1807 | assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); | - | ||||||||||||||||||
1808 | } executed 16778265 times by 14 tests: end of block Executed by:
| 16778265 | ||||||||||||||||||
1809 | } executed 16938736 times by 14 tests: end of block Executed by:
| 16938736 | ||||||||||||||||||
1810 | return rc; executed 1319072 times by 390 tests: return rc; Executed by:
| 1319072 | ||||||||||||||||||
1811 | } | - | ||||||||||||||||||
1812 | - | |||||||||||||||||||
1813 | /* | - | ||||||||||||||||||
1814 | ** This function is a no-op if the pager is in exclusive mode and not | - | ||||||||||||||||||
1815 | ** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN | - | ||||||||||||||||||
1816 | ** state. | - | ||||||||||||||||||
1817 | ** | - | ||||||||||||||||||
1818 | ** If the pager is not in exclusive-access mode, the database file is | - | ||||||||||||||||||
1819 | ** completely unlocked. If the file is unlocked and the file-system does | - | ||||||||||||||||||
1820 | ** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is | - | ||||||||||||||||||
1821 | ** closed (if it is open). | - | ||||||||||||||||||
1822 | ** | - | ||||||||||||||||||
1823 | ** If the pager is in ERROR state when this function is called, the | - | ||||||||||||||||||
1824 | ** contents of the pager cache are discarded before switching back to | - | ||||||||||||||||||
1825 | ** the OPEN state. Regardless of whether the pager is in exclusive-mode | - | ||||||||||||||||||
1826 | ** or not, any journal file left in the file-system will be treated | - | ||||||||||||||||||
1827 | ** as a hot-journal and rolled back the next time a read-transaction | - | ||||||||||||||||||
1828 | ** is opened (by this or by any other connection). | - | ||||||||||||||||||
1829 | */ | - | ||||||||||||||||||
1830 | static void pager_unlock(Pager *pPager){ | - | ||||||||||||||||||
1831 | - | |||||||||||||||||||
1832 | assert( pPager->eState==PAGER_READER | - | ||||||||||||||||||
1833 | || pPager->eState==PAGER_OPEN | - | ||||||||||||||||||
1834 | || pPager->eState==PAGER_ERROR | - | ||||||||||||||||||
1835 | ); | - | ||||||||||||||||||
1836 | - | |||||||||||||||||||
1837 | sqlite3BitvecDestroy(pPager->pInJournal); | - | ||||||||||||||||||
1838 | pPager->pInJournal = 0; | - | ||||||||||||||||||
1839 | releaseAllSavepoints(pPager); | - | ||||||||||||||||||
1840 | - | |||||||||||||||||||
1841 | if( pagerUseWal(pPager) ){
| 59008-298248 | ||||||||||||||||||
1842 | assert( !isOpen(pPager->jfd) ); | - | ||||||||||||||||||
1843 | sqlite3WalEndReadTransaction(pPager->pWal); | - | ||||||||||||||||||
1844 | pPager->eState = PAGER_OPEN; | - | ||||||||||||||||||
1845 | }else if( !pPager->exclusiveMode ){ executed 59008 times by 49 tests: end of block Executed by:
| 59008-226019 | ||||||||||||||||||
1846 | int rc; /* Error code returned by pagerUnlockDb() */ | - | ||||||||||||||||||
1847 | int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
| 63547-162472 | ||||||||||||||||||
1848 | - | |||||||||||||||||||
1849 | /* If the operating system support deletion of open files, then | - | ||||||||||||||||||
1850 | ** close the journal file when dropping the database lock. Otherwise | - | ||||||||||||||||||
1851 | ** another connection with journal_mode=delete might delete the file | - | ||||||||||||||||||
1852 | ** out from under us. | - | ||||||||||||||||||
1853 | */ | - | ||||||||||||||||||
1854 | assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 ); | - | ||||||||||||||||||
1855 | assert( (PAGER_JOURNALMODE_OFF & 5)!=1 ); | - | ||||||||||||||||||
1856 | assert( (PAGER_JOURNALMODE_WAL & 5)!=1 ); | - | ||||||||||||||||||
1857 | assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 ); | - | ||||||||||||||||||
1858 | assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 ); | - | ||||||||||||||||||
1859 | assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 ); | - | ||||||||||||||||||
1860 | if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
| 34-225985 | ||||||||||||||||||
1861 | || 1!=(pPager->journalMode & 5)
| 13-21 | ||||||||||||||||||
1862 | ){ | - | ||||||||||||||||||
1863 | sqlite3OsClose(pPager->jfd); | - | ||||||||||||||||||
1864 | } executed 225998 times by 428 tests: end of block Executed by:
| 225998 | ||||||||||||||||||
1865 | - | |||||||||||||||||||
1866 | /* If the pager is in the ERROR state and the call to unlock the database | - | ||||||||||||||||||
1867 | ** file fails, set the current lock to UNKNOWN_LOCK. See the comment | - | ||||||||||||||||||
1868 | ** above the #define for UNKNOWN_LOCK for an explanation of why this | - | ||||||||||||||||||
1869 | ** is necessary. | - | ||||||||||||||||||
1870 | */ | - | ||||||||||||||||||
1871 | rc = pagerUnlockDb(pPager, NO_LOCK); | - | ||||||||||||||||||
1872 | if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
| 0-226019 | ||||||||||||||||||
1873 | pPager->eLock = UNKNOWN_LOCK; | - | ||||||||||||||||||
1874 | } never executed: end of block | 0 | ||||||||||||||||||
1875 | - | |||||||||||||||||||
1876 | /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here | - | ||||||||||||||||||
1877 | ** without clearing the error code. This is intentional - the error | - | ||||||||||||||||||
1878 | ** code is cleared and the cache reset in the block below. | - | ||||||||||||||||||
1879 | */ | - | ||||||||||||||||||
1880 | assert( pPager->errCode || pPager->eState!=PAGER_ERROR ); | - | ||||||||||||||||||
1881 | pPager->changeCountDone = 0; | - | ||||||||||||||||||
1882 | pPager->eState = PAGER_OPEN; | - | ||||||||||||||||||
1883 | } executed 226019 times by 428 tests: end of block Executed by:
| 226019 | ||||||||||||||||||
1884 | - | |||||||||||||||||||
1885 | /* If Pager.errCode is set, the contents of the pager cache cannot be | - | ||||||||||||||||||
1886 | ** trusted. Now that there are no outstanding references to the pager, | - | ||||||||||||||||||
1887 | ** it can safely move back to PAGER_OPEN state. This happens in both | - | ||||||||||||||||||
1888 | ** normal and exclusive-locking mode. | - | ||||||||||||||||||
1889 | */ | - | ||||||||||||||||||
1890 | assert( pPager->errCode==SQLITE_OK || !MEMDB ); | - | ||||||||||||||||||
1891 | if( pPager->errCode ){
| 60410-296846 | ||||||||||||||||||
1892 | if( pPager->tempFile==0 ){
| 291-60119 | ||||||||||||||||||
1893 | pager_reset(pPager); | - | ||||||||||||||||||
1894 | pPager->changeCountDone = 0; | - | ||||||||||||||||||
1895 | pPager->eState = PAGER_OPEN; | - | ||||||||||||||||||
1896 | }else{ executed 291 times by 1 test: end of block Executed by:
| 291 | ||||||||||||||||||
1897 | pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
| 0-60119 | ||||||||||||||||||
1898 | } executed 60119 times by 5 tests: end of block Executed by:
| 60119 | ||||||||||||||||||
1899 | if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0); never executed: sqlite3OsUnfetch(pPager->fd, 0, 0);
| 0-60410 | ||||||||||||||||||
1900 | pPager->errCode = SQLITE_OK; | - | ||||||||||||||||||
1901 | setGetterMethod(pPager); | - | ||||||||||||||||||
1902 | } executed 60410 times by 5 tests: end of block Executed by:
| 60410 | ||||||||||||||||||
1903 | - | |||||||||||||||||||
1904 | pPager->journalOff = 0; | - | ||||||||||||||||||
1905 | pPager->journalHdr = 0; | - | ||||||||||||||||||
1906 | pPager->setMaster = 0; | - | ||||||||||||||||||
1907 | } executed 357256 times by 438 tests: end of block Executed by:
| 357256 | ||||||||||||||||||
1908 | - | |||||||||||||||||||
1909 | /* | - | ||||||||||||||||||
1910 | ** This function is called whenever an IOERR or FULL error that requires | - | ||||||||||||||||||
1911 | ** the pager to transition into the ERROR state may ahve occurred. | - | ||||||||||||||||||
1912 | ** The first argument is a pointer to the pager structure, the second | - | ||||||||||||||||||
1913 | ** the error-code about to be returned by a pager API function. The | - | ||||||||||||||||||
1914 | ** value returned is a copy of the second argument to this function. | - | ||||||||||||||||||
1915 | ** | - | ||||||||||||||||||
1916 | ** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the | - | ||||||||||||||||||
1917 | ** IOERR sub-codes, the pager enters the ERROR state and the error code | - | ||||||||||||||||||
1918 | ** is stored in Pager.errCode. While the pager remains in the ERROR state, | - | ||||||||||||||||||
1919 | ** all major API calls on the Pager will immediately return Pager.errCode. | - | ||||||||||||||||||
1920 | ** | - | ||||||||||||||||||
1921 | ** The ERROR state indicates that the contents of the pager-cache | - | ||||||||||||||||||
1922 | ** cannot be trusted. This state can be cleared by completely discarding | - | ||||||||||||||||||
1923 | ** the contents of the pager-cache. If a transaction was active when | - | ||||||||||||||||||
1924 | ** the persistent error occurred, then the rollback journal may need | - | ||||||||||||||||||
1925 | ** to be replayed to restore the contents of the database file (as if | - | ||||||||||||||||||
1926 | ** it were a hot-journal). | - | ||||||||||||||||||
1927 | */ | - | ||||||||||||||||||
1928 | static int pager_error(Pager *pPager, int rc){ | - | ||||||||||||||||||
1929 | int rc2 = rc & 0xff; | - | ||||||||||||||||||
1930 | assert( rc==SQLITE_OK || !MEMDB ); | - | ||||||||||||||||||
1931 | assert( | - | ||||||||||||||||||
1932 | pPager->errCode==SQLITE_FULL || | - | ||||||||||||||||||
1933 | pPager->errCode==SQLITE_OK || | - | ||||||||||||||||||
1934 | (pPager->errCode & 0xff)==SQLITE_IOERR | - | ||||||||||||||||||
1935 | ); | - | ||||||||||||||||||
1936 | if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
| 100-1453477 | ||||||||||||||||||
1937 | pPager->errCode = rc; | - | ||||||||||||||||||
1938 | pPager->eState = PAGER_ERROR; | - | ||||||||||||||||||
1939 | setGetterMethod(pPager); | - | ||||||||||||||||||
1940 | } executed 278 times by 1 test: end of block Executed by:
| 278 | ||||||||||||||||||
1941 | return rc; executed 1453577 times by 71 tests: return rc; Executed by:
| 1453577 | ||||||||||||||||||
1942 | } | - | ||||||||||||||||||
1943 | - | |||||||||||||||||||
1944 | static int pager_truncate(Pager *pPager, Pgno nPage); | - | ||||||||||||||||||
1945 | - | |||||||||||||||||||
1946 | /* | - | ||||||||||||||||||
1947 | ** The write transaction open on pPager is being committed (bCommit==1) | - | ||||||||||||||||||
1948 | ** or rolled back (bCommit==0). | - | ||||||||||||||||||
1949 | ** | - | ||||||||||||||||||
1950 | ** Return TRUE if and only if all dirty pages should be flushed to disk. | - | ||||||||||||||||||
1951 | ** | - | ||||||||||||||||||
1952 | ** Rules: | - | ||||||||||||||||||
1953 | ** | - | ||||||||||||||||||
1954 | ** * For non-TEMP databases, always sync to disk. This is necessary | - | ||||||||||||||||||
1955 | ** for transactions to be durable. | - | ||||||||||||||||||
1956 | ** | - | ||||||||||||||||||
1957 | ** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing | - | ||||||||||||||||||
1958 | ** file has been created already (via a spill on pagerStress()) and | - | ||||||||||||||||||
1959 | ** when the number of dirty pages in memory exceeds 25% of the total | - | ||||||||||||||||||
1960 | ** cache size. | - | ||||||||||||||||||
1961 | */ | - | ||||||||||||||||||
1962 | static int pagerFlushOnCommit(Pager *pPager, int bCommit){ | - | ||||||||||||||||||
1963 | if( pPager->tempFile==0 ) return 1; executed 192390 times by 152 tests: return 1; Executed by:
| 131328-192390 | ||||||||||||||||||
1964 | if( !bCommit ) return 0; executed 124227 times by 17 tests: return 0; Executed by:
| 7101-124227 | ||||||||||||||||||
1965 | if( !isOpen(pPager->fd) ) return 0; executed 6781 times by 2 tests: return 0; Executed by:
| 320-6781 | ||||||||||||||||||
1966 | return (sqlite3PCachePercentDirty(pPager->pPCache)>=25); executed 320 times by 1 test: return (sqlite3PCachePercentDirty(pPager->pPCache)>=25); Executed by:
| 320 | ||||||||||||||||||
1967 | } | - | ||||||||||||||||||
1968 | - | |||||||||||||||||||
1969 | /* | - | ||||||||||||||||||
1970 | ** This routine ends a transaction. A transaction is usually ended by | - | ||||||||||||||||||
1971 | ** either a COMMIT or a ROLLBACK operation. This routine may be called | - | ||||||||||||||||||
1972 | ** after rollback of a hot-journal, or if an error occurs while opening | - | ||||||||||||||||||
1973 | ** the journal file or writing the very first journal-header of a | - | ||||||||||||||||||
1974 | ** database transaction. | - | ||||||||||||||||||
1975 | ** | - | ||||||||||||||||||
1976 | ** This routine is never called in PAGER_ERROR state. If it is called | - | ||||||||||||||||||
1977 | ** in PAGER_NONE or PAGER_SHARED state and the lock held is less | - | ||||||||||||||||||
1978 | ** exclusive than a RESERVED lock, it is a no-op. | - | ||||||||||||||||||
1979 | ** | - | ||||||||||||||||||
1980 | ** Otherwise, any active savepoints are released. | - | ||||||||||||||||||
1981 | ** | - | ||||||||||||||||||
1982 | ** If the journal file is open, then it is "finalized". Once a journal | - | ||||||||||||||||||
1983 | ** file has been finalized it is not possible to use it to roll back a | - | ||||||||||||||||||
1984 | ** transaction. Nor will it be considered to be a hot-journal by this | - | ||||||||||||||||||
1985 | ** or any other database connection. Exactly how a journal is finalized | - | ||||||||||||||||||
1986 | ** depends on whether or not the pager is running in exclusive mode and | - | ||||||||||||||||||
1987 | ** the current journal-mode (Pager.journalMode value), as follows: | - | ||||||||||||||||||
1988 | ** | - | ||||||||||||||||||
1989 | ** journalMode==MEMORY | - | ||||||||||||||||||
1990 | ** Journal file descriptor is simply closed. This destroys an | - | ||||||||||||||||||
1991 | ** in-memory journal. | - | ||||||||||||||||||
1992 | ** | - | ||||||||||||||||||
1993 | ** journalMode==TRUNCATE | - | ||||||||||||||||||
1994 | ** Journal file is truncated to zero bytes in size. | - | ||||||||||||||||||
1995 | ** | - | ||||||||||||||||||
1996 | ** journalMode==PERSIST | - | ||||||||||||||||||
1997 | ** The first 28 bytes of the journal file are zeroed. This invalidates | - | ||||||||||||||||||
1998 | ** the first journal header in the file, and hence the entire journal | - | ||||||||||||||||||
1999 | ** file. An invalid journal file cannot be rolled back. | - | ||||||||||||||||||
2000 | ** | - | ||||||||||||||||||
2001 | ** journalMode==DELETE | - | ||||||||||||||||||
2002 | ** The journal file is closed and deleted using sqlite3OsDelete(). | - | ||||||||||||||||||
2003 | ** | - | ||||||||||||||||||
2004 | ** If the pager is running in exclusive mode, this method of finalizing | - | ||||||||||||||||||
2005 | ** the journal file is never used. Instead, if the journalMode is | - | ||||||||||||||||||
2006 | ** DELETE and the pager is in exclusive mode, the method described under | - | ||||||||||||||||||
2007 | ** journalMode==PERSIST is used instead. | - | ||||||||||||||||||
2008 | ** | - | ||||||||||||||||||
2009 | ** After the journal is finalized, the pager moves to PAGER_READER state. | - | ||||||||||||||||||
2010 | ** If running in non-exclusive rollback mode, the lock on the file is | - | ||||||||||||||||||
2011 | ** downgraded to a SHARED_LOCK. | - | ||||||||||||||||||
2012 | ** | - | ||||||||||||||||||
2013 | ** SQLITE_OK is returned if no error occurs. If an error occurs during | - | ||||||||||||||||||
2014 | ** any of the IO operations to finalize the journal file or unlock the | - | ||||||||||||||||||
2015 | ** database then the IO error code is returned to the user. If the | - | ||||||||||||||||||
2016 | ** operation to finalize the journal file fails, then the code still | - | ||||||||||||||||||
2017 | ** tries to unlock the database file if not in exclusive mode. If the | - | ||||||||||||||||||
2018 | ** unlock operation fails as well, then the first error code related | - | ||||||||||||||||||
2019 | ** to the first error encountered (the journal finalization one) is | - | ||||||||||||||||||
2020 | ** returned. | - | ||||||||||||||||||
2021 | */ | - | ||||||||||||||||||
2022 | static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){ | - | ||||||||||||||||||
2023 | int rc = SQLITE_OK; /* Error code from journal finalization operation */ | - | ||||||||||||||||||
2024 | int rc2 = SQLITE_OK; /* Error code from db file unlock operation */ | - | ||||||||||||||||||
2025 | - | |||||||||||||||||||
2026 | /* Do nothing if the pager does not have an open write transaction | - | ||||||||||||||||||
2027 | ** or at least a RESERVED lock. This function may be called when there | - | ||||||||||||||||||
2028 | ** is no write-transaction active but a RESERVED or greater lock is | - | ||||||||||||||||||
2029 | ** held under two circumstances: | - | ||||||||||||||||||
2030 | ** | - | ||||||||||||||||||
2031 | ** 1. After a successful hot-journal rollback, it is called with | - | ||||||||||||||||||
2032 | ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK. | - | ||||||||||||||||||
2033 | ** | - | ||||||||||||||||||
2034 | ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE | - | ||||||||||||||||||
2035 | ** lock switches back to locking_mode=normal and then executes a | - | ||||||||||||||||||
2036 | ** read-transaction, this function is called with eState==PAGER_READER | - | ||||||||||||||||||
2037 | ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed. | - | ||||||||||||||||||
2038 | */ | - | ||||||||||||||||||
2039 | assert( assert_pager_state(pPager) ); | - | ||||||||||||||||||
2040 | assert( pPager->eState!=PAGER_ERROR ); | - | ||||||||||||||||||
2041 | if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
| 63708-252119 | ||||||||||||||||||
2042 | return SQLITE_OK; executed 188411 times by 435 tests: return 0; Executed by:
| 188411 | ||||||||||||||||||
2043 | } | - | ||||||||||||||||||
2044 | - | |||||||||||||||||||
2045 | releaseAllSavepoints(pPager); | - | ||||||||||||||||||
2046 | assert( isOpen(pPager->jfd) || pPager->pInJournal==0 | - | ||||||||||||||||||
2047 | || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC) | - | ||||||||||||||||||
2048 | ); | - | ||||||||||||||||||
2049 | if( isOpen(pPager->jfd) ){
| 45114-181926 | ||||||||||||||||||
2050 | assert( !pagerUseWal(pPager) ); | - | ||||||||||||||||||
2051 | - | |||||||||||||||||||
2052 | /* Finalize the journal file. */ | - | ||||||||||||||||||
2053 | if( sqlite3JournalIsInMemory(pPager->jfd) ){
| 4785-40329 | ||||||||||||||||||
2054 | /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */ | - | ||||||||||||||||||
2055 | sqlite3OsClose(pPager->jfd); | - | ||||||||||||||||||
2056 | }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){ executed 4785 times by 2 tests: end of block Executed by:
| 113-40216 | ||||||||||||||||||
2057 | if( pPager->journalOff==0 ){
| 2-111 | ||||||||||||||||||
2058 | rc = SQLITE_OK; | - | ||||||||||||||||||
2059 | }else{ executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
2060 | rc = sqlite3OsTruncate(pPager->jfd, 0); | - | ||||||||||||||||||
2061 | if( rc==SQLITE_OK && pPager->fullSync ){
| 0-111 | ||||||||||||||||||
2062 | /* Make sure the new file size is written into the inode right away. | - | ||||||||||||||||||
2063 | ** Otherwise the journal might resurrect following a power loss and | - | ||||||||||||||||||
2064 | ** cause the last transaction to roll back. See | - | ||||||||||||||||||
2065 | ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773 | - | ||||||||||||||||||
2066 | */ | - | ||||||||||||||||||
2067 | rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags); | - | ||||||||||||||||||
2068 | } executed 98 times by 1 test: end of block Executed by:
| 98 | ||||||||||||||||||
2069 | } executed 111 times by 1 test: end of block Executed by:
| 111 | ||||||||||||||||||
2070 | pPager->journalOff = 0; | - | ||||||||||||||||||
2071 | }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST executed 113 times by 1 test: end of block Executed by:
| 113-39814 | ||||||||||||||||||
2072 | || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
| 5-39653 | ||||||||||||||||||
2073 | ){ | - | ||||||||||||||||||
2074 | rc = zeroJournalHdr(pPager, hasMaster||pPager->tempFile); | - | ||||||||||||||||||
2075 | pPager->journalOff = 0; | - | ||||||||||||||||||
2076 | }else{ executed 558 times by 3 tests: end of block Executed by:
| 558 | ||||||||||||||||||
2077 | /* This branch may be executed with Pager.journalMode==MEMORY if | - | ||||||||||||||||||
2078 | ** a hot-journal was just rolled back. In this case the journal | - | ||||||||||||||||||
2079 | ** file should be closed and deleted. If this connection writes to | - | ||||||||||||||||||
2080 | ** the database file, it will do so using an in-memory journal. | - | ||||||||||||||||||
2081 | */ | - | ||||||||||||||||||
2082 | int bDelete = !pPager->tempFile; | - | ||||||||||||||||||
2083 | assert( sqlite3JournalIsInMemory(pPager->jfd)==0 ); | - | ||||||||||||||||||
2084 | assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE | - | ||||||||||||||||||
2085 | || pPager->journalMode==PAGER_JOURNALMODE_MEMORY | - | ||||||||||||||||||
2086 | || pPager->journalMode==PAGER_JOURNALMODE_WAL | - | ||||||||||||||||||
2087 | ); | - | ||||||||||||||||||
2088 | sqlite3OsClose(pPager->jfd); | - | ||||||||||||||||||
2089 | if( bDelete ){
| 7-39651 | ||||||||||||||||||
2090 | rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync); | - | ||||||||||||||||||
2091 | } executed 39651 times by 36 tests: end of block Executed by:
| 39651 | ||||||||||||||||||
2092 | } executed 39658 times by 36 tests: end of block Executed by:
| 39658 | ||||||||||||||||||
2093 | } | - | ||||||||||||||||||
2094 | - | |||||||||||||||||||
2095 | #ifdef SQLITE_CHECK_PAGES | - | ||||||||||||||||||
2096 | sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash); | - | ||||||||||||||||||
2097 | if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){ | - | ||||||||||||||||||
2098 | PgHdr *p = sqlite3PagerLookup(pPager, 1); | - | ||||||||||||||||||
2099 | if( p ){ | - | ||||||||||||||||||
2100 | p->pageHash = 0; | - | ||||||||||||||||||
2101 | sqlite3PagerUnrefNotNull(p); | - | ||||||||||||||||||
2102 | } | - | ||||||||||||||||||
2103 | } | - | ||||||||||||||||||
2104 | #endif | - | ||||||||||||||||||
2105 | - | |||||||||||||||||||
2106 | sqlite3BitvecDestroy(pPager->pInJournal); | - | ||||||||||||||||||
2107 | pPager->pInJournal = 0; | - | ||||||||||||||||||
2108 | pPager->nRec = 0; | - | ||||||||||||||||||
2109 | if( rc==SQLITE_OK ){
| 28-227012 | ||||||||||||||||||
2110 | if( MEMDB || pagerFlushOnCommit(pPager, bCommit) ){
| 1083-225929 | ||||||||||||||||||
2111 | sqlite3PcacheCleanAll(pPager->pPCache); | - | ||||||||||||||||||
2112 | }else{ executed 99637 times by 62 tests: end of block Executed by:
| 99637 | ||||||||||||||||||
2113 | sqlite3PcacheClearWritable(pPager->pPCache); | - | ||||||||||||||||||
2114 | } executed 127375 times by 17 tests: end of block Executed by:
| 127375 | ||||||||||||||||||
2115 | sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize); | - | ||||||||||||||||||
2116 | } executed 227012 times by 73 tests: end of block Executed by:
| 227012 | ||||||||||||||||||
2117 | - | |||||||||||||||||||
2118 | if( pagerUseWal(pPager) ){
| 55608-171432 | ||||||||||||||||||
2119 | /* Drop the WAL write-lock, if any. Also, if the connection was in | - | ||||||||||||||||||
2120 | ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE | - | ||||||||||||||||||
2121 | ** lock held on the database file. | - | ||||||||||||||||||
2122 | */ | - | ||||||||||||||||||
2123 | rc2 = sqlite3WalEndWriteTransaction(pPager->pWal); | - | ||||||||||||||||||
2124 | assert( rc2==SQLITE_OK ); | - | ||||||||||||||||||
2125 | }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){ executed 55608 times by 28 tests: end of block Executed by:
| 28-171404 | ||||||||||||||||||
2126 | /* This branch is taken when committing a transaction in rollback-journal | - | ||||||||||||||||||
2127 | ** mode if the database file on disk is larger than the database image. | - | ||||||||||||||||||
2128 | ** At this point the journal has been finalized and the transaction | - | ||||||||||||||||||
2129 | ** successfully committed, but the EXCLUSIVE lock is still held on the | - | ||||||||||||||||||
2130 | ** file. So it is safe to truncate the database file to its minimum | - | ||||||||||||||||||
2131 | ** required size. */ | - | ||||||||||||||||||
2132 | assert( pPager->eLock==EXCLUSIVE_LOCK ); | - | ||||||||||||||||||
2133 | rc = pager_truncate(pPager, pPager->dbSize); | - | ||||||||||||||||||
2134 | } executed 860 times by 4 tests: end of block Executed by:
| 860 | ||||||||||||||||||
2135 | - | |||||||||||||||||||
2136 | if( rc==SQLITE_OK && bCommit ){
| 44-226996 | ||||||||||||||||||
2137 | rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0); | - | ||||||||||||||||||
2138 | if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; executed 98109 times by 54 tests: rc = 0; Executed by:
| 0-98109 | ||||||||||||||||||
2139 | } executed 98109 times by 54 tests: end of block Executed by:
| 98109 | ||||||||||||||||||
2140 | - | |||||||||||||||||||
2141 | if( !pPager->exclusiveMode
| 65282-161758 | ||||||||||||||||||
2142 | && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
| 8-106174 | ||||||||||||||||||
2143 | ){ | - | ||||||||||||||||||
2144 | rc2 = pagerUnlockDb(pPager, SHARED_LOCK); | - | ||||||||||||||||||
2145 | pPager->changeCountDone = 0; | - | ||||||||||||||||||
2146 | } executed 106182 times by 50 tests: end of block Executed by:
| 106182 | ||||||||||||||||||
2147 | pPager->eState = PAGER_READER; | - | ||||||||||||||||||
2148 | pPager->setMaster = 0; | - | ||||||||||||||||||
2149 | - | |||||||||||||||||||
2150 | return (rc==SQLITE_OK?rc2:rc); executed 227040 times by 73 tests: return (rc==0?rc2:rc); Executed by:
| 44-227040 | ||||||||||||||||||
2151 | } | - | ||||||||||||||||||
2152 | - | |||||||||||||||||||
2153 | /* | - | ||||||||||||||||||
2154 | ** Execute a rollback if a transaction is active and unlock the | - | ||||||||||||||||||
2155 | ** database file. | - | ||||||||||||||||||
2156 | ** | - | ||||||||||||||||||
2157 | ** If the pager has already entered the ERROR state, do not attempt | - | ||||||||||||||||||
2158 | ** the rollback at this time. Instead, pager_unlock() is called. The | - | ||||||||||||||||||
2159 | ** call to pager_unlock() will discard all in-memory pages, unlock | - | ||||||||||||||||||
2160 | ** the database file and move the pager back to OPEN state. If this | - | ||||||||||||||||||
2161 | ** means that there is a hot-journal left in the file-system, the next | - | ||||||||||||||||||
2162 | ** connection to obtain a shared lock on the pager (which may be this one) | - | ||||||||||||||||||
2163 | ** will roll it back. | - | ||||||||||||||||||
2164 | ** | - | ||||||||||||||||||
2165 | ** If the pager has not already entered the ERROR state, but an IO or | - | ||||||||||||||||||
2166 | ** malloc error occurs during a rollback, then this will itself cause | - | ||||||||||||||||||
2167 | ** the pager to enter the ERROR state. Which will be cleared by the | - | ||||||||||||||||||
2168 | ** call to pager_unlock(), as described above. | - | ||||||||||||||||||
2169 | */ | - | ||||||||||||||||||
2170 | static void pagerUnlockAndRollback(Pager *pPager){ | - | ||||||||||||||||||
2171 | if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
| 32377-296240 | ||||||||||||||||||
2172 | assert( assert_pager_state(pPager) ); | - | ||||||||||||||||||
2173 | if( pPager->eState>=PAGER_WRITER_LOCKED ){
| 0-263863 | ||||||||||||||||||
2174 |