0000: 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 /*.** 2004 May 2
0010: 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 2.**.** The auth
0020: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
0030: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
0040: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
0050: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
0060: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
0070: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
0080: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
0090: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
00a0: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
00b0: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
00c0: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
00d0: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
00e0: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
00f0: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
0100: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
0110: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
0120: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0170: 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
0180: 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
0190: 6f 64 65 20 74 68 61 74 20 6d 6f 64 69 66 69 65 ode that modifie
01a0: 64 20 74 68 65 20 4f 53 20 6c 61 79 65 72 20 69 d the OS layer i
01b0: 6e 20 6f 72 64 65 72 20 74 6f 20 73 69 6d 75 6c n order to simul
01c0: 61 74 65 0a 2a 2a 20 74 68 65 20 65 66 66 65 63 ate.** the effec
01d0: 74 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 t on the databas
01e0: 65 20 66 69 6c 65 20 6f 66 20 61 6e 20 4f 53 20 e file of an OS
01f0: 63 72 61 73 68 20 6f 72 20 70 6f 77 65 72 20 66 crash or power f
0200: 61 69 6c 75 72 65 2e 20 20 54 68 69 73 0a 2a 2a ailure. This.**
0210: 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 is used to test
0220: 20 74 68 65 20 61 62 69 6c 69 74 79 20 6f 66 20 the ability of
0230: 53 51 4c 69 74 65 20 74 6f 20 72 65 63 6f 76 65 SQLite to recove
0240: 72 20 66 72 6f 6d 20 74 68 6f 73 65 20 73 69 74 r from those sit
0250: 75 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 24 uations..**.** $
0260: 49 64 3a 20 74 65 73 74 36 2e 63 2c 76 20 31 2e Id: test6.c,v 1.
0270: 34 31 20 32 30 30 38 2f 31 32 2f 32 30 20 31 38 41 2008/12/20 18
0280: 3a 33 33 3a 35 39 20 64 61 6e 69 65 6c 6b 31 39 :33:59 danielk19
0290: 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 20 77 Exp $.*/.#if
02a0: 53 51 4c 49 54 45 5f 54 45 53 54 20 20 20 20 20 SQLITE_TEST
02b0: 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c /* This fil
02c0: 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 e is used for te
02d0: 73 74 69 6e 67 20 6f 6e 6c 79 20 2a 2f 0a 23 69 sting only */.#i
02e0: 6e 63 6c 75 64 65 20 22 73 71 6c 69 74 65 49 6e nclude "sqliteIn
02f0: 74 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 22 74 t.h".#include "t
0300: 63 6c 2e 68 22 0a 0a 23 69 66 6e 64 65 66 20 53 cl.h"..#ifndef S
0310: 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 QLITE_OMIT_DISKI
0320: 4f 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65 20 O /* This file
0330: 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 64 69 is a no-op if di
0340: 73 6b 20 49 2f 4f 20 69 73 20 64 69 73 61 62 6c sk I/O is disabl
0350: 65 64 20 2a 2f 0a 0a 2f 2a 20 23 64 65 66 69 6e ed */../* #defin
0360: 65 20 54 52 41 43 45 5f 43 52 41 53 48 54 45 53 e TRACE_CRASHTES
0370: 54 20 2a 2f 0a 0a 74 79 70 65 64 65 66 20 73 74 T */..typedef st
0380: 72 75 63 74 20 43 72 61 73 68 46 69 6c 65 20 43 ruct CrashFile C
0390: 72 61 73 68 46 69 6c 65 3b 0a 74 79 70 65 64 65 rashFile;.typede
03a0: 66 20 73 74 72 75 63 74 20 43 72 61 73 68 47 6c f struct CrashGl
03b0: 6f 62 61 6c 20 43 72 61 73 68 47 6c 6f 62 61 6c obal CrashGlobal
03c0: 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
03d0: 20 57 72 69 74 65 42 75 66 66 65 72 20 57 72 69 WriteBuffer Wri
03e0: 74 65 42 75 66 66 65 72 3b 0a 0a 2f 2a 0a 2a 2a teBuffer;../*.**
03f0: 20 4d 65 74 68 6f 64 3a 0a 2a 2a 0a 2a 2a 20 20 Method:.**.**
0400: 20 54 68 69 73 20 6c 61 79 65 72 20 69 73 20 69 This layer is i
0410: 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 mplemented as a
0420: 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 wrapper around t
0430: 68 65 20 22 72 65 61 6c 22 20 0a 2a 2a 20 20 20 he "real" .**
0440: 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a sqlite3_file obj
0450: 65 63 74 20 66 6f 72 20 74 68 65 20 68 6f 73 74 ect for the host
0460: 20 73 79 73 74 65 6d 2e 20 45 61 63 68 20 74 69 system. Each ti
0470: 6d 65 20 64 61 74 61 20 69 73 20 0a 2a 2a 20 20 me data is .**
0480: 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
0490: 66 69 6c 65 20 6f 62 6a 65 63 74 2c 20 69 6e 73 file object, ins
04a0: 74 65 61 64 20 6f 66 20 62 65 69 6e 67 20 77 72 tead of being wr
04b0: 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 itten to the.**
04c0: 20 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c underlying fil
04d0: 65 2c 20 74 68 65 20 77 72 69 74 65 20 6f 70 65 e, the write ope
04e0: 72 61 74 69 6f 6e 20 69 73 20 73 74 6f 72 65 64 ration is stored
04f0: 20 69 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 in an in-memory
0500: 20 0a 2a 2a 20 20 20 73 74 72 75 63 74 75 72 65 .** structure
0510: 20 28 74 79 70 65 20 57 72 69 74 65 42 75 66 66 (type WriteBuff
0520: 65 72 29 2e 20 54 68 69 73 20 73 74 72 75 63 74 er). This struct
0530: 75 72 65 20 69 73 20 70 6c 61 63 65 64 20 61 74 ure is placed at
0540: 20 74 68 65 0a 2a 2a 20 20 20 65 6e 64 20 6f 66 the.** end of
0550: 20 61 20 67 6c 6f 62 61 6c 20 6f 72 64 65 72 65 a global ordere
0560: 64 20 6c 69 73 74 20 28 74 68 65 20 77 72 69 74 d list (the writ
0570: 65 2d 6c 69 73 74 29 2e 0a 2a 2a 0a 2a 2a 20 20 e-list)..**.**
0580: 20 57 68 65 6e 20 64 61 74 61 20 69 73 20 72 65 When data is re
0590: 61 64 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 6f ad from a file o
05a0: 62 6a 65 63 74 2c 20 74 68 65 20 72 65 71 75 65 bject, the reque
05b0: 73 74 65 64 20 72 65 67 69 6f 6e 20 69 73 0a 2a sted region is.*
05c0: 2a 20 20 20 66 69 72 73 74 20 72 65 74 72 69 65 * first retrie
05d0: 76 65 64 20 66 72 6f 6d 20 74 68 65 20 72 65 61 ved from the rea
05e0: 6c 20 66 69 6c 65 2e 20 54 68 65 20 77 72 69 74 l file. The writ
05f0: 65 2d 6c 69 73 74 20 69 73 20 74 68 65 6e 20 0a e-list is then .
0600: 2a 2a 20 20 20 74 72 61 76 65 72 73 65 64 20 61 ** traversed a
0610: 6e 64 20 64 61 74 61 20 63 6f 70 69 65 64 20 66 nd data copied f
0620: 72 6f 6d 20 61 6e 79 20 6f 76 65 72 6c 61 70 70 rom any overlapp
0630: 69 6e 67 20 57 72 69 74 65 42 75 66 66 65 72 20 ing WriteBuffer
0640: 0a 2a 2a 20 20 20 73 74 72 75 63 74 75 72 65 73 .** structures
0650: 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20 62 to the output b
0660: 75 66 66 65 72 2e 20 69 2e 65 2e 20 61 20 72 65 uffer. i.e. a re
0670: 61 64 28 29 20 6f 70 65 72 61 74 69 6f 6e 20 66 ad() operation f
0680: 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 20 20 6f 6e ollowing.** on
0690: 65 20 6f 72 20 6d 6f 72 65 20 77 72 69 74 65 28 e or more write(
06a0: 29 20 6f 70 65 72 61 74 69 6f 6e 73 20 77 6f 72 ) operations wor
06b0: 6b 73 20 61 73 20 65 78 70 65 63 74 65 64 2c 20 ks as expected,
06c0: 65 76 65 6e 20 69 66 20 6e 6f 0a 2a 2a 20 20 20 even if no.**
06d0: 64 61 74 61 20 68 61 73 20 61 63 74 75 61 6c 6c data has actuall
06e0: 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 6f y been written o
06f0: 75 74 20 74 6f 20 74 68 65 20 72 65 61 6c 20 66 ut to the real f
0700: 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 57 68 65 ile..**.** Whe
0710: 6e 20 61 20 66 73 79 6e 63 28 29 20 6f 70 65 72 n a fsync() oper
0720: 61 74 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d ation is perform
0730: 65 64 2c 20 61 6e 20 6f 70 65 72 61 74 69 6e 67 ed, an operating
0740: 20 73 79 73 74 65 6d 20 63 72 61 73 68 20 0a 2a system crash .*
0750: 2a 20 20 20 6d 61 79 20 62 65 20 73 69 6d 75 6c * may be simul
0760: 61 74 65 64 2c 20 69 6e 20 77 68 69 63 68 20 63 ated, in which c
0770: 61 73 65 20 65 78 69 74 28 2d 31 29 20 69 73 20 ase exit(-1) is
0780: 63 61 6c 6c 65 64 20 28 74 68 65 20 63 61 6c 6c called (the call
0790: 20 74 6f 20 0a 2a 2a 20 20 20 78 53 79 6e 63 28 to .** xSync(
07a0: 29 20 6e 65 76 65 72 20 72 65 74 75 72 6e 73 29 ) never returns)
07b0: 2e 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 . Whether or not
07c0: 20 61 20 63 72 61 73 68 20 69 73 20 73 69 6d 75 a crash is simu
07d0: 6c 61 74 65 64 2c 0a 2a 2a 20 20 20 74 68 65 20 lated,.** the
07e0: 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 data associated
07f0: 77 69 74 68 20 61 20 73 75 62 73 65 74 20 6f 66 with a subset of
0800: 20 74 68 65 20 57 72 69 74 65 42 75 66 66 65 72 the WriteBuffer
0810: 20 73 74 72 75 63 74 75 72 65 73 20 0a 2a 2a 20 structures .**
0820: 20 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 stored in the
0830: 77 72 69 74 65 2d 6c 69 73 74 20 69 73 20 77 72 write-list is wr
0840: 69 74 74 65 6e 20 74 6f 20 74 68 65 20 72 65 61 itten to the rea
0850: 6c 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c l underlying fil
0860: 65 73 20 0a 2a 2a 20 20 20 61 6e 64 20 74 68 65 es .** and the
0870: 20 65 6e 74 72 69 65 73 20 72 65 6d 6f 76 65 64 entries removed
0880: 20 66 72 6f 6d 20 74 68 65 20 77 72 69 74 65 2d from the write-
0890: 6c 69 73 74 2e 20 49 66 20 61 20 63 72 61 73 68 list. If a crash
08a0: 20 69 73 20 73 69 6d 75 6c 61 74 65 64 2c 0a 2a is simulated,.*
08b0: 2a 20 20 20 61 20 73 75 62 73 65 74 20 6f 66 20 * a subset of
08c0: 74 68 65 20 62 75 66 66 65 72 73 20 6d 61 79 20 the buffers may
08d0: 62 65 20 63 6f 72 72 75 70 74 65 64 20 62 65 66 be corrupted bef
08e0: 6f 72 65 20 74 68 65 20 64 61 74 61 20 69 73 20 ore the data is
08f0: 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 20 written..**.**
0900: 20 54 68 65 20 65 78 61 63 74 20 73 75 62 73 65 The exact subse
0910: 74 20 6f 66 20 74 68 65 20 77 72 69 74 65 2d 6c t of the write-l
0920: 69 73 74 20 77 72 69 74 74 65 6e 20 61 6e 64 2f ist written and/
0930: 6f 72 20 63 6f 72 72 75 70 74 65 64 20 69 73 0a or corrupted is.
0940: 2a 2a 20 20 20 64 65 74 65 72 6d 69 6e 65 64 20 ** determined
0950: 62 79 20 74 68 65 20 73 69 6d 75 6c 61 74 65 64 by the simulated
0960: 20 64 65 76 69 63 65 20 63 68 61 72 61 63 74 65 device characte
0970: 72 69 73 74 69 63 73 20 61 6e 64 20 73 65 63 74 ristics and sect
0980: 6f 72 2d 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 22 or-size..**.** "
0990: 4e 6f 72 6d 61 6c 22 20 6d 6f 64 65 3a 0a 2a 2a Normal" mode:.**
09a0: 0a 2a 2a 20 20 20 4e 6f 72 6d 61 6c 20 6d 6f 64 .** Normal mod
09b0: 65 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 74 e is used when t
09c0: 68 65 20 73 69 6d 75 6c 61 74 65 64 20 64 65 76 he simulated dev
09d0: 69 63 65 20 68 61 73 20 6e 6f 6e 65 20 6f 66 20 ice has none of
09e0: 74 68 65 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f the.** SQLITE_
09f0: 49 4f 43 41 50 5f 58 58 58 20 66 6c 61 67 73 20 IOCAP_XXX flags
0a00: 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 set..**.** In
0a10: 6e 6f 72 6d 61 6c 20 6d 6f 64 65 2c 20 69 66 20 normal mode, if
0a20: 74 68 65 20 66 73 79 6e 63 28 29 20 69 73 20 6e the fsync() is n
0a30: 6f 74 20 61 20 73 69 6d 75 6c 61 74 65 64 20 63 ot a simulated c
0a40: 72 61 73 68 2c 20 74 68 65 20 0a 2a 2a 20 20 20 rash, the .**
0a50: 77 72 69 74 65 2d 6c 69 73 74 20 69 73 20 74 72 write-list is tr
0a60: 61 76 65 72 73 65 64 20 66 72 6f 6d 20 62 65 67 aversed from beg
0a70: 69 6e 6e 69 6e 67 20 74 6f 20 65 6e 64 2e 20 45 inning to end. E
0a80: 61 63 68 20 57 72 69 74 65 42 75 66 66 65 72 0a ach WriteBuffer.
0a90: 2a 2a 20 20 20 73 74 72 75 63 74 75 72 65 20 61 ** structure a
0aa0: 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
0ab0: 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 75 he file handle u
0ac0: 73 65 64 20 74 6f 20 63 61 6c 6c 20 78 53 79 6e sed to call xSyn
0ad0: 63 28 29 0a 2a 2a 20 20 20 69 73 20 77 72 69 74 c().** is writ
0ae0: 74 65 6e 20 74 6f 20 74 68 65 20 72 65 61 6c 20 ten to the real
0af0: 66 69 6c 65 20 61 6e 64 20 72 65 6d 6f 76 65 64 file and removed
0b00: 20 66 72 6f 6d 20 74 68 65 20 77 72 69 74 65 2d from the write-
0b10: 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 66 list..**.** If
0b20: 20 61 20 63 72 61 73 68 20 69 73 20 73 69 6d 75 a crash is simu
0b30: 6c 61 74 65 64 2c 20 6f 6e 65 20 6f 66 20 74 68 lated, one of th
0b40: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 6b 65 e following take
0b50: 73 20 70 6c 61 63 65 20 66 6f 72 20 0a 2a 2a 20 s place for .**
0b60: 20 20 65 61 63 68 20 57 72 69 74 65 42 75 66 66 each WriteBuff
0b70: 65 72 20 69 6e 20 74 68 65 20 77 72 69 74 65 2d er in the write-
0b80: 6c 69 73 74 2c 20 72 65 67 61 72 64 6c 65 73 73 list, regardless
0b90: 20 6f 66 20 77 68 69 63 68 20 0a 2a 2a 20 20 20 of which .**
0ba0: 66 69 6c 65 2d 68 61 6e 64 6c 65 20 69 74 20 69 file-handle it i
0bb0: 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
0bc0: 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 31 2e 20 h:.**.** 1.
0bd0: 54 68 65 20 62 75 66 66 65 72 20 69 73 20 63 6f The buffer is co
0be0: 72 72 65 63 74 6c 79 20 77 72 69 74 74 65 6e 20 rrectly written
0bf0: 74 6f 20 74 68 65 20 66 69 6c 65 2c 20 6a 75 73 to the file, jus
0c00: 74 20 61 73 20 69 66 0a 2a 2a 20 20 20 20 20 20 t as if.**
0c10: 20 20 61 20 63 72 61 73 68 20 77 65 72 65 20 6e a crash were n
0c20: 6f 74 20 62 65 69 6e 67 20 73 69 6d 75 6c 61 74 ot being simulat
0c30: 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 32 2e ed..**.** 2.
0c40: 20 4e 6f 74 68 69 6e 67 20 69 73 20 64 6f 6e 65 Nothing is done
0c50: 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 33 2e 20 47 ..**.** 3. G
0c60: 61 72 62 61 67 65 20 64 61 74 61 20 69 73 20 77 arbage data is w
0c70: 72 69 74 74 65 6e 20 74 6f 20 61 6c 6c 20 73 65 ritten to all se
0c80: 63 74 6f 72 73 20 6f 66 20 74 68 65 20 66 69 6c ctors of the fil
0c90: 65 20 74 68 61 74 20 0a 2a 2a 20 20 20 20 20 20 e that .**
0ca0: 20 20 6f 76 65 72 6c 61 70 20 74 68 65 20 72 65 overlap the re
0cb0: 67 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 62 gion specified b
0cc0: 79 20 74 68 65 20 57 72 69 74 65 42 75 66 66 65 y the WriteBuffe
0cd0: 72 2e 20 4f 72 20 67 61 72 62 61 67 65 0a 2a 2a r. Or garbage.**
0ce0: 20 20 20 20 20 20 20 20 64 61 74 61 20 69 73 20 data is
0cf0: 77 72 69 74 74 65 6e 20 74 6f 20 73 6f 6d 65 20 written to some
0d00: 63 6f 6e 74 69 67 75 6f 75 73 20 73 65 63 74 69 contiguous secti
0d10: 6f 6e 20 77 69 74 68 69 6e 20 74 68 65 20 0a 2a on within the .*
0d20: 2a 20 20 20 20 20 20 20 20 6f 76 65 72 6c 61 70 * overlap
0d30: 70 65 64 20 73 65 63 74 6f 72 73 2e 0a 2a 2a 0a ped sectors..**.
0d40: 2a 2a 20 44 65 76 69 63 65 20 43 68 61 72 61 63 ** Device Charac
0d50: 74 65 72 69 73 74 69 63 20 66 6c 61 67 20 68 61 teristic flag ha
0d60: 6e 64 6c 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 ndling:.**.**
0d70: 49 66 20 74 68 65 20 49 4f 43 41 50 5f 41 54 4f If the IOCAP_ATO
0d80: 4d 49 43 20 66 6c 61 67 20 69 73 20 73 65 74 2c MIC flag is set,
0d90: 20 74 68 65 6e 20 6f 70 74 69 6f 6e 20 28 33 29 then option (3)
0da0: 20 61 62 6f 76 65 20 69 73 20 0a 2a 2a 20 20 20 above is .**
0db0: 6e 65 76 65 72 20 73 65 6c 65 63 74 65 64 2e 0a never selected..
0dc0: 2a 2a 0a 2a 2a 20 20 20 49 66 20 74 68 65 20 49 **.** If the I
0dd0: 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 20 66 OCAP_ATOMIC512 f
0de0: 6c 61 67 20 69 73 20 73 65 74 2c 20 61 6e 64 20 lag is set, and
0df0: 74 68 65 20 57 72 69 74 65 42 75 66 66 65 72 20 the WriteBuffer
0e00: 72 65 70 72 65 73 65 6e 74 73 0a 2a 2a 20 20 20 represents.**
0e10: 61 6e 20 61 6c 69 67 6e 65 64 20 77 72 69 74 65 an aligned write
0e20: 28 29 20 6f 66 20 61 6e 20 69 6e 74 65 67 65 72 () of an integer
0e30: 20 6e 75 6d 62 65 72 20 6f 66 20 35 31 32 20 62 number of 512 b
0e40: 79 74 65 20 72 65 67 69 6f 6e 73 2c 20 74 68 65 yte regions, the
0e50: 6e 0a 2a 2a 20 20 20 6f 70 74 69 6f 6e 20 28 33 n.** option (3
0e60: 29 20 61 62 6f 76 65 20 69 73 20 6e 65 76 65 72 ) above is never
0e70: 20 73 65 6c 65 63 74 65 64 2e 20 49 6e 73 74 65 selected. Inste
0e80: 61 64 2c 20 65 61 63 68 20 35 31 32 20 62 79 74 ad, each 512 byt
0e90: 65 20 72 65 67 69 6f 6e 0a 2a 2a 20 20 20 69 73 e region.** is
0ea0: 20 65 69 74 68 65 72 20 63 6f 72 72 65 63 74 6c either correctl
0eb0: 79 20 77 72 69 74 74 65 6e 20 6f 72 20 6c 65 66 y written or lef
0ec0: 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 75 6e 74 t completely unt
0ed0: 6f 75 63 68 65 64 2e 20 53 69 6d 69 6c 61 72 0a ouched. Similar.
0ee0: 2a 2a 20 20 20 6c 6f 67 69 63 20 67 6f 76 65 72 ** logic gover
0ef0: 6e 73 20 74 68 65 20 62 65 68 61 76 69 6f 75 72 ns the behaviour
0f00: 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20 6f if any of the o
0f10: 74 68 65 72 20 41 54 4f 4d 49 43 58 58 58 20 66 ther ATOMICXXX f
0f20: 6c 61 67 73 0a 2a 2a 20 20 20 69 73 20 73 65 74 lags.** is set
0f30: 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 66 20 65 69 74 ..**.** If eit
0f40: 68 65 72 20 74 68 65 20 49 4f 43 41 50 5f 53 41 her the IOCAP_SA
0f50: 46 45 41 50 50 45 4e 44 20 6f 72 20 49 4f 43 41 FEAPPEND or IOCA
0f60: 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 66 6c 61 P_SEQUENTIAL fla
0f70: 67 73 20 61 72 65 20 73 65 74 0a 2a 2a 20 20 20 gs are set.**
0f80: 61 6e 64 20 61 20 63 72 61 73 68 20 69 73 20 62 and a crash is b
0f90: 65 69 6e 67 20 73 69 6d 75 6c 61 74 65 64 2c 20 eing simulated,
0fa0: 74 68 65 6e 20 61 6e 20 65 6e 74 72 79 20 6f 66 then an entry of
0fb0: 20 74 68 65 20 77 72 69 74 65 2d 6c 69 73 74 20 the write-list
0fc0: 69 73 0a 2a 2a 20 20 20 73 65 6c 65 63 74 65 64 is.** selected
0fd0: 20 61 74 20 72 61 6e 64 6f 6d 2e 20 45 76 65 72 at random. Ever
0fe0: 79 74 68 69 6e 67 20 69 6e 20 74 68 65 20 6c 69 ything in the li
0ff0: 73 74 20 61 66 74 65 72 20 74 68 65 20 73 65 6c st after the sel
1000: 65 63 74 65 64 20 65 6e 74 72 79 20 0a 2a 2a 20 ected entry .**
1010: 20 20 69 73 20 64 69 73 63 61 72 64 65 64 20 62 is discarded b
1020: 65 66 6f 72 65 20 70 72 6f 63 65 73 73 69 6e 67 efore processing
1030: 20 62 65 67 69 6e 73 2e 0a 2a 2a 0a 2a 2a 20 20 begins..**.**
1040: 20 49 66 20 49 4f 43 41 50 5f 53 45 51 55 45 4e If IOCAP_SEQUEN
1050: 54 49 41 4c 20 69 73 20 73 65 74 20 61 6e 64 20 TIAL is set and
1060: 61 20 63 72 61 73 68 20 69 73 20 62 65 69 6e 67 a crash is being
1070: 20 73 69 6d 75 6c 61 74 65 64 2c 20 6f 70 74 69 simulated, opti
1080: 6f 6e 20 0a 2a 2a 20 20 20 28 31 29 20 69 73 20 on .** (1) is
1090: 73 65 6c 65 63 74 65 64 20 66 6f 72 20 61 6c 6c selected for all
10a0: 20 77 72 69 74 65 2d 6c 69 73 74 20 65 6e 74 72 write-list entr
10b0: 69 65 73 20 65 78 63 65 70 74 20 74 68 65 20 6c ies except the l
10c0: 61 73 74 2e 20 49 66 20 61 20 0a 2a 2a 20 20 20 ast. If a .**
10d0: 63 72 61 73 68 20 69 73 20 6e 6f 74 20 62 65 69 crash is not bei
10e0: 6e 67 20 73 69 6d 75 6c 61 74 65 64 2c 20 74 68 ng simulated, th
10f0: 65 6e 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 69 en all entries i
1100: 6e 20 74 68 65 20 77 72 69 74 65 2d 6c 69 73 74 n the write-list
1110: 0a 2a 2a 20 20 20 74 68 61 74 20 6f 63 63 75 72 .** that occur
1120: 20 62 65 66 6f 72 65 20 61 74 20 6c 65 61 73 74 before at least
1130: 20 6f 6e 65 20 77 72 69 74 65 28 29 20 6f 6e 20 one write() on
1140: 74 68 65 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 the file-handle
1150: 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 20 20 61 specified.** a
1160: 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 78 53 s part of the xS
1170: 79 6e 63 28 29 20 61 72 65 20 77 72 69 74 74 65 ync() are writte
1180: 6e 20 74 6f 20 74 68 65 69 72 20 61 73 73 6f 63 n to their assoc
1190: 69 61 74 65 64 20 72 65 61 6c 20 66 69 6c 65 73 iated real files
11a0: 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 66 20 49 4f 43 ..**.** If IOC
11b0: 41 50 5f 53 41 46 45 41 50 50 45 4e 44 20 69 73 AP_SAFEAPPEND is
11c0: 20 73 65 74 20 61 6e 64 20 74 68 65 20 66 69 72 set and the fir
11d0: 73 74 20 62 79 74 65 20 77 72 69 74 74 65 6e 20 st byte written
11e0: 62 79 20 74 68 65 20 77 72 69 74 65 28 29 0a 2a by the write().*
11f0: 2a 20 20 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 * operation is
1200: 20 6f 6e 65 20 62 79 74 65 20 70 61 73 74 20 74 one byte past t
1210: 68 65 20 63 75 72 72 65 6e 74 20 65 6e 64 20 6f he current end o
1220: 66 20 74 68 65 20 66 69 6c 65 2c 20 74 68 65 6e f the file, then
1230: 20 6f 70 74 69 6f 6e 0a 2a 2a 20 20 20 28 31 29 option.** (1)
1240: 20 69 73 20 61 6c 77 61 79 73 20 73 65 6c 65 63 is always selec
1250: 74 65 64 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 ted..*/../*.** E
1260: 61 63 68 20 77 72 69 74 65 20 6f 70 65 72 61 74 ach write operat
1270: 69 6f 6e 20 69 6e 20 74 68 65 20 77 72 69 74 65 ion in the write
1280: 2d 6c 69 73 74 20 69 73 20 72 65 70 72 65 73 65 -list is represe
1290: 6e 74 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 nted by an insta
12a0: 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f nce.** of the fo
12b0: 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
12c0: 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 42 75 66 e..**.** If zBuf
12d0: 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 69 73 is 0, then this
12e0: 20 73 74 72 75 63 74 75 72 65 20 72 65 70 72 65 structure repre
12f0: 73 65 6e 74 73 20 61 20 63 61 6c 6c 20 74 6f 20 sents a call to
1300: 78 54 72 75 6e 63 61 74 65 28 29 2c 20 0a 2a 2a xTruncate(), .**
1310: 20 6e 6f 74 20 78 57 72 69 74 65 28 29 2e 20 49 not xWrite(). I
1320: 6e 20 74 68 61 74 20 63 61 73 65 2c 20 69 4f 66 n that case, iOf
1330: 66 73 65 74 20 69 73 20 74 68 65 20 73 69 7a 65 fset is the size
1340: 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 20 69 that the file i
1350: 73 0a 2a 2a 20 74 72 75 6e 63 61 74 65 64 20 74 s.** truncated t
1360: 6f 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 72 69 o..*/.struct Wri
1370: 74 65 42 75 66 66 65 72 20 7b 0a 20 20 69 36 34 teBuffer {. i64
1380: 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 iOffset;
1390: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 /* Byt
13a0: 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 e offset of the
13b0: 73 74 61 72 74 20 6f 66 20 74 68 69 73 20 77 72 start of this wr
13c0: 69 74 65 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e ite() */. int n
13d0: 42 75 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Buf;
13e0: 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
13f0: 72 20 6f 66 20 62 79 74 65 73 20 77 72 69 74 74 r of bytes writt
1400: 65 6e 20 2a 2f 0a 20 20 75 38 20 2a 7a 42 75 66 en */. u8 *zBuf
1410: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1420: 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
1430: 74 6f 20 63 6f 70 79 20 6f 66 20 77 72 69 74 74 to copy of writt
1440: 65 6e 20 64 61 74 61 20 2a 2f 0a 20 20 43 72 61 en data */. Cra
1450: 73 68 46 69 6c 65 20 2a 70 46 69 6c 65 3b 20 20 shFile *pFile;
1460: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c /* Fil
1470: 65 20 74 68 69 73 20 77 72 69 74 65 28 29 20 61 e this write() a
1480: 70 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 0a 20 20 pplies to */..
1490: 57 72 69 74 65 42 75 66 66 65 72 20 2a 70 4e 65 WriteBuffer *pNe
14a0: 78 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 xt; /*
14b0: 4e 65 78 74 20 69 6e 20 43 72 61 73 68 47 6c 6f Next in CrashGlo
14c0: 62 61 6c 2e 70 57 72 69 74 65 4c 69 73 74 20 2a bal.pWriteList *
14d0: 2f 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 43 72 61 /.};..struct Cra
14e0: 73 68 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 shFile {. const
14f0: 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
1500: 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 ods *pMethod;
1510: 2f 2a 20 4d 75 73 74 20 62 65 20 66 69 72 73 74 /* Must be first
1520: 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 */. sqlite3_fi
1530: 6c 65 20 2a 70 52 65 61 6c 46 69 6c 65 3b 20 20 le *pRealFile;
1540: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e /* Un
1550: 64 65 72 6c 79 69 6e 67 20 22 72 65 61 6c 22 20 derlying "real"
1560: 66 69 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 file handle */.
1570: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 0a 20 char *zName;..
1580: 20 2f 2a 20 43 61 63 68 65 20 6f 66 20 74 68 65 /* Cache of the
1590: 20 65 6e 74 69 72 65 20 66 69 6c 65 2e 20 54 68 entire file. Th
15a0: 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 73 70 is is used to sp
15b0: 65 65 64 20 75 70 20 4f 73 52 65 61 64 28 29 20 eed up OsRead()
15c0: 61 6e 64 20 0a 20 20 2a 2a 20 4f 73 46 69 6c 65 and . ** OsFile
15d0: 53 69 7a 65 28 29 20 63 61 6c 6c 73 2e 20 41 6c Size() calls. Al
15e0: 74 68 6f 75 67 68 20 62 6f 74 68 20 63 6f 75 6c though both coul
15f0: 64 20 62 65 20 64 6f 6e 65 20 62 79 20 74 72 61 d be done by tra
1600: 76 65 72 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a versing the. **
1610: 20 77 72 69 74 65 2d 6c 69 73 74 2c 20 69 6e 20 write-list, in
1620: 70 72 61 63 74 69 63 65 20 74 68 69 73 20 69 73 practice this is
1630: 20 69 6d 70 72 61 63 74 69 63 61 6c 6c 79 20 73 impractically s
1640: 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 low.. */. int
1650: 69 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 iSize;
1660: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1670: 20 2f 2a 20 53 69 7a 65 20 6f 66 20 66 69 6c 65 /* Size of file
1680: 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 in bytes */. i
1690: 6e 74 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20 nt nData;
16a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b0: 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 /* Size of b
16c0: 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 uffer allocated
16d0: 61 74 20 7a 44 61 74 61 20 2a 2f 0a 20 20 75 38 at zData */. u8
16e0: 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 20 20 *zData;
16f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1700: 20 20 20 2f 2a 20 42 75 66 66 65 72 20 63 6f 6e /* Buffer con
1710: 74 61 69 6e 69 6e 67 20 66 69 6c 65 20 63 6f 6e taining file con
1720: 74 65 6e 74 73 20 2a 2f 0a 7d 3b 0a 0a 73 74 72 tents */.};..str
1730: 75 63 74 20 43 72 61 73 68 47 6c 6f 62 61 6c 20 uct CrashGlobal
1740: 7b 0a 20 20 57 72 69 74 65 42 75 66 66 65 72 20 {. WriteBuffer
1750: 2a 70 57 72 69 74 65 4c 69 73 74 3b 20 20 20 20 *pWriteList;
1760: 20 2f 2a 20 48 65 61 64 20 6f 66 20 77 72 69 74 /* Head of writ
1770: 65 2d 6c 69 73 74 20 2a 2f 0a 20 20 57 72 69 74 e-list */. Writ
1780: 65 42 75 66 66 65 72 20 2a 70 57 72 69 74 65 4c eBuffer *pWriteL
1790: 69 73 74 45 6e 64 3b 20 20 2f 2a 20 45 6e 64 20 istEnd; /* End
17a0: 6f 66 20 77 72 69 74 65 2d 6c 69 73 74 20 2a 2f of write-list */
17b0: 0a 0a 20 20 69 6e 74 20 69 53 65 63 74 6f 72 53 .. int iSectorS
17c0: 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
17d0: 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 73 69 6d /* Value of sim
17e0: 75 6c 61 74 65 64 20 73 65 63 74 6f 72 20 73 69 ulated sector si
17f0: 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 65 76 ze */. int iDev
1800: 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
1810: 63 73 3b 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 cs; /* Value of
1820: 20 73 69 6d 75 6c 61 74 65 64 20 64 65 76 69 63 simulated devic
1830: 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 e characteristic
1840: 73 20 2a 2f 0a 0a 20 20 69 6e 74 20 69 43 72 61 s */.. int iCra
1850: 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 sh;
1860: 20 20 20 20 20 2f 2a 20 43 72 61 73 68 20 6f 6e /* Crash on
1870: 20 74 68 65 20 69 43 72 61 73 68 27 74 68 20 63 the iCrash'th c
1880: 61 6c 6c 20 74 6f 20 78 53 79 6e 63 28 29 20 2a all to xSync() *
1890: 2f 0a 20 20 63 68 61 72 20 7a 43 72 61 73 68 46 /. char zCrashF
18a0: 69 6c 65 5b 35 30 30 5d 3b 20 20 20 20 20 20 20 ile[500];
18b0: 20 2f 2a 20 43 72 61 73 68 20 64 75 72 69 6e 67 /* Crash during
18c0: 20 61 6e 20 78 53 79 6e 63 28 29 20 6f 6e 20 74 an xSync() on t
18d0: 68 69 73 20 66 69 6c 65 20 2a 2f 20 0a 7d 3b 0a his file */ .};.
18e0: 0a 73 74 61 74 69 63 20 43 72 61 73 68 47 6c 6f .static CrashGlo
18f0: 62 61 6c 20 67 20 3d 20 7b 30 2c 20 30 2c 20 53 bal g = {0, 0, S
1900: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 QLITE_DEFAULT_SE
1910: 43 54 4f 52 5f 53 49 5a 45 2c 20 30 2c 20 30 7d CTOR_SIZE, 0, 0}
1920: 3b 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 69 ;../*.** Set thi
1930: 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c s global variabl
1940: 65 20 74 6f 20 31 20 74 6f 20 65 6e 61 62 6c 65 e to 1 to enable
1950: 20 63 72 61 73 68 20 74 65 73 74 69 6e 67 2e 0a crash testing..
1960: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 */.static int sq
1970: 6c 69 74 65 33 43 72 61 73 68 54 65 73 74 45 6e lite3CrashTestEn
1980: 61 62 6c 65 20 3d 20 30 3b 0a 0a 73 74 61 74 69 able = 0;..stati
1990: 63 20 76 6f 69 64 20 2a 63 72 61 73 68 5f 6d 61 c void *crash_ma
19a0: 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b lloc(int nByte){
19b0: 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 20 . return (void
19c0: 2a 29 54 63 6c 5f 41 6c 6c 6f 63 28 28 73 69 7a *)Tcl_Alloc((siz
19d0: 65 5f 74 29 6e 42 79 74 65 29 3b 0a 7d 0a 73 74 e_t)nByte);.}.st
19e0: 61 74 69 63 20 76 6f 69 64 20 63 72 61 73 68 5f atic void crash_
19f0: 66 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 free(void *p){.
1a00: 20 54 63 6c 5f 46 72 65 65 28 70 29 3b 0a 7d 0a Tcl_Free(p);.}.
1a10: 73 74 61 74 69 63 20 76 6f 69 64 20 2a 63 72 61 static void *cra
1a20: 73 68 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 64 20 sh_realloc(void
1a30: 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 72 65 *p, int n){. re
1a40: 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 54 63 6c turn (void *)Tcl
1a50: 5f 52 65 61 6c 6c 6f 63 28 70 2c 20 28 73 69 7a _Realloc(p, (siz
1a60: 65 5f 74 29 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a e_t)n);.}../*.**
1a70: 20 46 6c 75 73 68 20 74 68 65 20 77 72 69 74 65 Flush the write
1a80: 2d 6c 69 73 74 20 61 73 20 69 66 20 78 53 79 6e -list as if xSyn
1a90: 63 28 29 20 68 61 64 20 62 65 65 6e 20 63 61 6c c() had been cal
1aa0: 6c 65 64 20 6f 6e 20 66 69 6c 65 20 68 61 6e 64 led on file hand
1ab0: 6c 65 0a 2a 2a 20 70 46 69 6c 65 2e 20 49 66 20 le.** pFile. If
1ac0: 69 73 43 72 61 73 68 20 69 73 20 74 72 75 65 2c isCrash is true,
1ad0: 20 73 69 6d 75 6c 61 74 65 20 61 20 63 72 61 73 simulate a cras
1ae0: 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 h..*/.static int
1af0: 20 77 72 69 74 65 4c 69 73 74 53 79 6e 63 28 43 writeListSync(C
1b00: 72 61 73 68 46 69 6c 65 20 2a 70 46 69 6c 65 2c rashFile *pFile,
1b10: 20 69 6e 74 20 69 73 43 72 61 73 68 29 7b 0a 20 int isCrash){.
1b20: 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1b30: 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 44 63 20 3d _OK;. int iDc =
1b40: 20 67 2e 69 44 65 76 69 63 65 43 68 61 72 61 63 g.iDeviceCharac
1b50: 74 65 72 69 73 74 69 63 73 3b 0a 0a 20 20 57 72 teristics;.. Wr
1b60: 69 74 65 42 75 66 66 65 72 20 2a 70 57 72 69 74 iteBuffer *pWrit
1b70: 65 3b 0a 20 20 57 72 69 74 65 42 75 66 66 65 72 e;. WriteBuffer
1b80: 20 2a 2a 70 70 50 74 72 3b 0a 0a 20 20 2f 2a 20 **ppPtr;.. /*
1b90: 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 If this is not a
1ba0: 20 63 72 61 73 68 20 73 69 6d 75 6c 61 74 69 6f crash simulatio
1bb0: 6e 2c 20 73 65 74 20 70 46 69 6e 61 6c 20 74 6f n, set pFinal to
1bc0: 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 0a 20 point to the .
1bd0: 20 2a 2a 20 6c 61 73 74 20 65 6c 65 6d 65 6e 74 ** last element
1be0: 20 6f 66 20 74 68 65 20 77 72 69 74 65 2d 6c 69 of the write-li
1bf0: 73 74 20 74 68 61 74 20 69 73 20 61 73 73 6f 63 st that is assoc
1c00: 69 61 74 65 64 20 77 69 74 68 20 66 69 6c 65 20 iated with file
1c10: 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 70 46 69 6c handle. ** pFil
1c20: 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 e.. **. ** If
1c30: 74 68 69 73 20 69 73 20 61 20 63 72 61 73 68 20 this is a crash
1c40: 73 69 6d 75 6c 61 74 69 6f 6e 2c 20 73 65 74 20 simulation, set
1c50: 70 46 69 6e 61 6c 20 74 6f 20 61 6e 20 61 72 62 pFinal to an arb
1c60: 69 74 72 61 72 69 6c 79 20 73 65 6c 65 63 74 65 itrarily selecte
1c70: 64 0a 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 20 6f d. ** element o
1c80: 66 20 74 68 65 20 77 72 69 74 65 2d 6c 69 73 74 f the write-list
1c90: 2e 0a 20 20 2a 2f 0a 20 20 57 72 69 74 65 42 75 .. */. WriteBu
1ca0: 66 66 65 72 20 2a 70 46 69 6e 61 6c 20 3d 20 30 ffer *pFinal = 0
1cb0: 3b 0a 20 20 69 66 28 20 21 69 73 43 72 61 73 68 ;. if( !isCrash
1cc0: 20 29 7b 0a 20 20 20 20 66 6f 72 28 70 57 72 69 ){. for(pWri
1cd0: 74 65 3d 67 2e 70 57 72 69 74 65 4c 69 73 74 3b te=g.pWriteList;
1ce0: 20 70 57 72 69 74 65 3b 20 70 57 72 69 74 65 3d pWrite; pWrite=
1cf0: 70 57 72 69 74 65 2d 3e 70 4e 65 78 74 29 7b 0a pWrite->pNext){.
1d00: 20 20 20 20 20 20 69 66 28 20 70 57 72 69 74 65 if( pWrite
1d10: 2d 3e 70 46 69 6c 65 3d 3d 70 46 69 6c 65 20 29 ->pFile==pFile )
1d20: 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6e 61 6c {. pFinal
1d30: 20 3d 20 70 57 72 69 74 65 3b 0a 20 20 20 20 20 = pWrite;.
1d40: 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 }. }. }else
1d50: 20 69 66 28 20 69 44 63 26 28 53 51 4c 49 54 45 if( iDc&(SQLITE
1d60: 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 _IOCAP_SEQUENTIA
1d70: 4c 7c 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 L|SQLITE_IOCAP_S
1d80: 41 46 45 5f 41 50 50 45 4e 44 29 20 29 7b 0a 20 AFE_APPEND) ){.
1d90: 20 20 20 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 int nWrite =
1da0: 30 3b 0a 20 20 20 20 69 6e 74 20 69 46 69 6e 61 0;. int iFina
1db0: 6c 3b 0a 20 20 20 20 66 6f 72 28 70 57 72 69 74 l;. for(pWrit
1dc0: 65 3d 67 2e 70 57 72 69 74 65 4c 69 73 74 3b 20 e=g.pWriteList;
1dd0: 70 57 72 69 74 65 3b 20 70 57 72 69 74 65 3d 70 pWrite; pWrite=p
1de0: 57 72 69 74 65 2d 3e 70 4e 65 78 74 29 20 6e 57 Write->pNext) nW
1df0: 72 69 74 65 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 rite++;. sqli
1e00: 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 te3_randomness(s
1e10: 69 7a 65 6f 66 28 69 6e 74 29 2c 20 26 69 46 69 izeof(int), &iFi
1e20: 6e 61 6c 29 3b 0a 20 20 20 20 69 46 69 6e 61 6c nal);. iFinal
1e30: 20 3d 20 28 28 69 46 69 6e 61 6c 3c 30 29 3f 2d = ((iFinal<0)?-
1e40: 31 2a 69 46 69 6e 61 6c 3a 69 46 69 6e 61 6c 29 1*iFinal:iFinal)
1e50: 25 6e 57 72 69 74 65 3b 0a 20 20 20 20 66 6f 72 %nWrite;. for
1e60: 28 70 57 72 69 74 65 3d 67 2e 70 57 72 69 74 65 (pWrite=g.pWrite
1e70: 4c 69 73 74 3b 20 69 46 69 6e 61 6c 3e 30 3b 20 List; iFinal>0;
1e80: 70 57 72 69 74 65 3d 70 57 72 69 74 65 2d 3e 70 pWrite=pWrite->p
1e90: 4e 65 78 74 29 20 69 46 69 6e 61 6c 2d 2d 3b 0a Next) iFinal--;.
1ea0: 20 20 20 20 70 46 69 6e 61 6c 20 3d 20 70 57 72 pFinal = pWr
1eb0: 69 74 65 3b 0a 20 20 7d 0a 0a 23 69 66 64 65 66 ite;. }..#ifdef
1ec0: 20 54 52 41 43 45 5f 43 52 41 53 48 54 45 53 54 TRACE_CRASHTEST
1ed0: 0a 20 20 70 72 69 6e 74 66 28 22 53 79 6e 63 20 . printf("Sync
1ee0: 25 73 20 28 69 73 20 25 73 20 63 72 61 73 68 29 %s (is %s crash)
1ef0: 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 7a 4e 61 6d \n", pFile->zNam
1f00: 65 2c 20 28 69 73 43 72 61 73 68 3f 22 61 22 3a e, (isCrash?"a":
1f10: 22 6e 6f 74 20 61 22 29 29 3b 0a 23 65 6e 64 69 "not a"));.#endi
1f20: 66 0a 0a 20 20 70 70 50 74 72 20 3d 20 26 67 2e f.. ppPtr = &g.
1f30: 70 57 72 69 74 65 4c 69 73 74 3b 0a 20 20 66 6f pWriteList;. fo
1f40: 72 28 70 57 72 69 74 65 3d 2a 70 70 50 74 72 3b r(pWrite=*ppPtr;
1f50: 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
1f60: 26 20 70 57 72 69 74 65 3b 20 70 57 72 69 74 65 & pWrite; pWrite
1f70: 3d 2a 70 70 50 74 72 29 7b 0a 20 20 20 20 73 71 =*ppPtr){. sq
1f80: 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 52 65 61 lite3_file *pRea
1f90: 6c 46 69 6c 65 20 3d 20 70 57 72 69 74 65 2d 3e lFile = pWrite->
1fa0: 70 46 69 6c 65 2d 3e 70 52 65 61 6c 46 69 6c 65 pFile->pRealFile
1fb0: 3b 0a 0a 20 20 20 20 2f 2a 20 28 65 41 63 74 69 ;.. /* (eActi
1fc0: 6f 6e 3d 3d 31 29 20 20 20 20 20 20 2d 3e 20 77 on==1) -> w
1fd0: 72 69 74 65 20 62 6c 6f 63 6b 20 6f 75 74 20 6e rite block out n
1fe0: 6f 72 6d 61 6c 6c 79 2c 0a 20 20 20 20 2a 2a 20 ormally,. **
1ff0: 28 65 41 63 74 69 6f 6e 3d 3d 32 29 20 20 20 20 (eAction==2)
2000: 20 20 2d 3e 20 64 6f 20 6e 6f 74 68 69 6e 67 2c -> do nothing,
2010: 0a 20 20 20 20 2a 2a 20 28 65 41 63 74 69 6f 6e . ** (eAction
2020: 3d 3d 33 29 20 20 20 20 20 20 2d 3e 20 74 72 61 ==3) -> tra
2030: 73 68 20 73 65 63 74 6f 72 73 2e 0a 20 20 20 20 sh sectors..
2040: 2a 2f 0a 20 20 20 20 69 6e 74 20 65 41 63 74 69 */. int eActi
2050: 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 on = 0;. if(
2060: 21 69 73 43 72 61 73 68 20 29 7b 0a 20 20 20 20 !isCrash ){.
2070: 20 20 65 41 63 74 69 6f 6e 20 3d 20 32 3b 0a 20 eAction = 2;.
2080: 20 20 20 20 20 69 66 28 20 28 70 57 72 69 74 65 if( (pWrite
2090: 2d 3e 70 46 69 6c 65 3d 3d 70 46 69 6c 65 20 7c ->pFile==pFile |
20a0: 7c 20 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 | iDc&SQLITE_IOC
20b0: 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 29 20 29 AP_SEQUENTIAL) )
20c0: 7b 0a 20 20 20 20 20 20 20 20 65 41 63 74 69 6f {. eActio
20d0: 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 n = 1;. }.
20e0: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
20f0: 63 68 61 72 20 72 61 6e 64 6f 6d 3b 0a 20 20 20 char random;.
2100: 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f sqlite3_rando
2110: 6d 6e 65 73 73 28 31 2c 20 26 72 61 6e 64 6f 6d mness(1, &random
2120: 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 44 6f 20 );.. /* Do
2130: 6e 6f 74 20 73 65 6c 65 63 74 20 6f 70 74 69 6f not select optio
2140: 6e 20 33 20 28 73 65 63 74 6f 72 20 74 72 61 73 n 3 (sector tras
2150: 68 69 6e 67 29 20 69 66 20 74 68 65 20 49 4f 43 hing) if the IOC
2160: 41 50 5f 41 54 4f 4d 49 43 20 66 6c 61 67 20 0a AP_ATOMIC flag .
2170: 20 20 20 20 20 20 2a 2a 20 69 73 20 73 65 74 20 ** is set
2180: 6f 72 20 74 68 69 73 20 69 73 20 61 6e 20 4f 73 or this is an Os
2190: 54 72 75 6e 63 61 74 65 28 29 2c 20 6e 6f 74 20 Truncate(), not
21a0: 61 6e 20 4f 73 77 72 69 74 65 28 29 2e 0a 20 20 an Oswrite()..
21b0: 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
21c0: 20 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 (iDc&SQLITE_IOC
21d0: 41 50 5f 41 54 4f 4d 49 43 29 20 7c 7c 20 28 70 AP_ATOMIC) || (p
21e0: 57 72 69 74 65 2d 3e 7a 42 75 66 3d 3d 30 29 20 Write->zBuf==0)
21f0: 29 7b 0a 20 20 20 20 20 20 20 20 72 61 6e 64 6f ){. rando
2200: 6d 20 26 3d 20 30 78 30 31 3b 0a 20 20 20 20 20 m &= 0x01;.
2210: 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
2220: 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c IOCAP_SEQUENTIAL
2230: 20 69 73 20 73 65 74 20 61 6e 64 20 74 68 69 73 is set and this
2240: 20 69 73 20 6e 6f 74 20 74 68 65 20 66 69 6e 61 is not the fina
2250: 6c 20 65 6e 74 72 79 0a 20 20 20 20 20 20 2a 2a l entry. **
2260: 20 69 6e 20 74 68 65 20 74 72 75 6e 63 61 74 65 in the truncate
2270: 64 20 77 72 69 74 65 2d 6c 69 73 74 2c 20 61 6c d write-list, al
2280: 77 61 79 73 20 73 65 6c 65 63 74 20 6f 70 74 69 ways select opti
2290: 6f 6e 20 31 20 28 77 72 69 74 65 0a 20 20 20 20 on 1 (write.
22a0: 20 20 2a 2a 20 6f 75 74 20 63 6f 72 72 65 63 74 ** out correct
22b0: 6c 79 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ly).. */.
22c0: 20 20 20 20 69 66 28 20 28 69 44 63 26 53 51 4c if( (iDc&SQL
22d0: 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e ITE_IOCAP_SEQUEN
22e0: 54 49 41 4c 20 26 26 20 70 57 72 69 74 65 21 3d TIAL && pWrite!=
22f0: 70 46 69 6e 61 6c 29 20 29 7b 0a 20 20 20 20 20 pFinal) ){.
2300: 20 20 20 72 61 6e 64 6f 6d 20 3d 20 30 3b 0a 20 random = 0;.
2310: 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a }.. /*
2320: 20 49 66 20 49 4f 43 41 50 5f 53 41 46 45 5f 41 If IOCAP_SAFE_A
2330: 50 50 45 4e 44 20 69 73 20 73 65 74 20 61 6e 64 PPEND is set and
2340: 20 74 68 69 73 20 4f 73 57 72 69 74 65 28 29 20 this OsWrite()
2350: 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a 20 20 20 operation is.
2360: 20 20 20 2a 2a 20 61 6e 20 61 70 70 65 6e 64 20 ** an append
2370: 28 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 (first byte of t
2380: 68 65 20 77 72 69 74 74 65 6e 20 72 65 67 69 6f he written regio
2390: 6e 20 69 73 20 31 20 62 79 74 65 20 70 61 73 74 n is 1 byte past
23a0: 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 63 75 the. ** cu
23b0: 72 72 65 6e 74 20 45 4f 46 29 2c 20 61 6c 77 61 rrent EOF), alwa
23c0: 79 73 20 73 65 6c 65 63 74 20 6f 70 74 69 6f 6e ys select option
23d0: 20 31 20 28 77 72 69 74 65 20 6f 75 74 20 63 6f 1 (write out co
23e0: 72 72 65 63 74 6c 79 29 2e 0a 20 20 20 20 20 20 rrectly)..
23f0: 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 44 63 */. if( iDc
2400: 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 &SQLITE_IOCAP_SA
2410: 46 45 5f 41 50 50 45 4e 44 20 26 26 20 70 57 72 FE_APPEND && pWr
2420: 69 74 65 2d 3e 7a 42 75 66 20 29 7b 0a 20 20 20 ite->zBuf ){.
2430: 20 20 20 20 20 69 36 34 20 69 53 69 7a 65 3b 0a i64 iSize;.
2440: 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f sqlite3O
2450: 73 46 69 6c 65 53 69 7a 65 28 70 52 65 61 6c 46 sFileSize(pRealF
2460: 69 6c 65 2c 20 26 69 53 69 7a 65 29 3b 0a 20 20 ile, &iSize);.
2470: 20 20 20 20 20 20 69 66 28 20 69 53 69 7a 65 3d if( iSize=
2480: 3d 70 57 72 69 74 65 2d 3e 69 4f 66 66 73 65 74 =pWrite->iOffset
2490: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 61 ){. ra
24a0: 6e 64 6f 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 ndom = 0;.
24b0: 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 }. }..
24c0: 20 20 20 69 66 28 20 28 72 61 6e 64 6f 6d 26 30 if( (random&0
24d0: 78 30 36 29 3d 3d 30 78 30 36 20 29 7b 0a 20 20 x06)==0x06 ){.
24e0: 20 20 20 20 20 20 65 41 63 74 69 6f 6e 20 3d 20 eAction =
24f0: 33 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 3;. }else{.
2500: 20 20 20 20 20 20 20 20 65 41 63 74 69 6f 6e 20 eAction
2510: 3d 20 28 28 72 61 6e 64 6f 6d 26 30 78 30 31 29 = ((random&0x01)
2520: 3f 32 3a 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ?2:1);. }.
2530: 20 20 20 7d 0a 0a 20 20 20 20 73 77 69 74 63 68 }.. switch
2540: 28 20 65 41 63 74 69 6f 6e 20 29 7b 0a 20 20 20 ( eAction ){.
2550: 20 20 20 63 61 73 65 20 31 3a 20 7b 20 20 20 20 case 1: {
2560: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 /* Wr
2570: 69 74 65 20 6f 75 74 20 63 6f 72 72 65 63 74 6c ite out correctl
2580: 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 y */. if(
2590: 20 70 57 72 69 74 65 2d 3e 7a 42 75 66 20 29 7b pWrite->zBuf ){
25a0: 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
25b0: 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 0a sqlite3OsWrite(.
25c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 52 pR
25d0: 65 61 6c 46 69 6c 65 2c 20 70 57 72 69 74 65 2d ealFile, pWrite-
25e0: 3e 7a 42 75 66 2c 20 70 57 72 69 74 65 2d 3e 6e >zBuf, pWrite->n
25f0: 42 75 66 2c 20 70 57 72 69 74 65 2d 3e 69 4f 66 Buf, pWrite->iOf
2600: 66 73 65 74 0a 20 20 20 20 20 20 20 20 20 20 29 fset. )
2610: 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
2620: 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
2630: 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 sqlite3OsTruncat
2640: 65 28 70 52 65 61 6c 46 69 6c 65 2c 20 70 57 72 e(pRealFile, pWr
2650: 69 74 65 2d 3e 69 4f 66 66 73 65 74 29 3b 0a 20 ite->iOffset);.
2660: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
2670: 20 2a 70 70 50 74 72 20 3d 20 70 57 72 69 74 65 *ppPtr = pWrite
2680: 2d 3e 70 4e 65 78 74 3b 0a 23 69 66 64 65 66 20 ->pNext;.#ifdef
2690: 54 52 41 43 45 5f 43 52 41 53 48 54 45 53 54 0a TRACE_CRASHTEST.
26a0: 20 20 20 20 20 20 20 20 69 66 28 20 69 73 43 72 if( isCr
26b0: 61 73 68 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ash ){.
26c0: 20 70 72 69 6e 74 66 28 22 57 72 69 74 69 6e 67 printf("Writing
26d0: 20 25 64 20 62 79 74 65 73 20 40 20 25 64 20 28 %d bytes @ %d (
26e0: 25 73 29 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 %s)\n", .
26f0: 20 20 20 20 20 70 57 72 69 74 65 2d 3e 6e 42 75 pWrite->nBu
2700: 66 2c 20 28 69 6e 74 29 70 57 72 69 74 65 2d 3e f, (int)pWrite->
2710: 69 4f 66 66 73 65 74 2c 20 70 57 72 69 74 65 2d iOffset, pWrite-
2720: 3e 70 46 69 6c 65 2d 3e 7a 4e 61 6d 65 0a 20 20 >pFile->zName.
2730: 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 );.
2740: 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
2750: 20 20 20 20 63 72 61 73 68 5f 66 72 65 65 28 70 crash_free(p
2760: 57 72 69 74 65 29 3b 0a 20 20 20 20 20 20 20 20 Write);.
2770: 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
2780: 20 20 20 20 20 63 61 73 65 20 32 3a 20 7b 20 20 case 2: {
2790: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
27a0: 44 6f 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 Do nothing */.
27b0: 20 20 20 20 20 20 70 70 50 74 72 20 3d 20 26 70 ppPtr = &p
27c0: 57 72 69 74 65 2d 3e 70 4e 65 78 74 3b 0a 23 69 Write->pNext;.#i
27d0: 66 64 65 66 20 54 52 41 43 45 5f 43 52 41 53 48 fdef TRACE_CRASH
27e0: 54 45 53 54 0a 20 20 20 20 20 20 20 20 69 66 28 TEST. if(
27f0: 20 69 73 43 72 61 73 68 20 29 7b 0a 20 20 20 20 isCrash ){.
2800: 20 20 20 20 20 20 70 72 69 6e 74 66 28 22 4f 6d printf("Om
2810: 69 74 69 6e 67 20 25 64 20 62 79 74 65 73 20 40 iting %d bytes @
2820: 20 25 64 20 28 25 73 29 5c 6e 22 2c 20 0a 20 20 %d (%s)\n", .
2830: 20 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65 pWrite
2840: 2d 3e 6e 42 75 66 2c 20 28 69 6e 74 29 70 57 72 ->nBuf, (int)pWr
2850: 69 74 65 2d 3e 69 4f 66 66 73 65 74 2c 20 70 57 ite->iOffset, pW
2860: 72 69 74 65 2d 3e 70 46 69 6c 65 2d 3e 7a 4e 61 rite->pFile->zNa
2870: 6d 65 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a me. );.
2880: 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 }.#endif
2890: 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
28a0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 }. ca
28b0: 73 65 20 33 3a 20 7b 20 20 20 20 20 20 20 20 20 se 3: {
28c0: 20 20 20 20 20 20 2f 2a 20 54 72 61 73 68 20 73 /* Trash s
28d0: 65 63 74 6f 72 73 20 2a 2f 0a 20 20 20 20 20 20 ectors */.
28e0: 20 20 75 38 20 2a 7a 47 61 72 62 61 67 65 3b 0a u8 *zGarbage;.
28f0: 20 20 20 20 20 20 20 20 69 6e 74 20 69 46 69 72 int iFir
2900: 73 74 20 3d 20 28 70 57 72 69 74 65 2d 3e 69 4f st = (pWrite->iO
2910: 66 66 73 65 74 2f 67 2e 69 53 65 63 74 6f 72 53 ffset/g.iSectorS
2920: 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 69 6e ize);. in
2930: 74 20 69 4c 61 73 74 20 3d 20 28 70 57 72 69 74 t iLast = (pWrit
2940: 65 2d 3e 69 4f 66 66 73 65 74 2b 70 57 72 69 74 e->iOffset+pWrit
2950: 65 2d 3e 6e 42 75 66 2d 31 29 2f 67 2e 69 53 65 e->nBuf-1)/g.iSe
2960: 63 74 6f 72 53 69 7a 65 3b 0a 0a 20 20 20 20 20 ctorSize;..
2970: 20 20 20 61 73 73 65 72 74 28 70 57 72 69 74 65 assert(pWrite
2980: 2d 3e 7a 42 75 66 29 3b 0a 0a 23 69 66 64 65 66 ->zBuf);..#ifdef
2990: 20 54 52 41 43 45 5f 43 52 41 53 48 54 45 53 54 TRACE_CRASHTEST
29a0: 0a 20 20 20 20 20 20 20 20 70 72 69 6e 74 66 28 . printf(
29b0: 22 54 72 61 73 68 69 6e 67 20 25 64 20 73 65 63 "Trashing %d sec
29c0: 74 6f 72 73 20 40 20 73 65 63 74 6f 72 20 25 64 tors @ sector %d
29d0: 20 28 25 73 29 5c 6e 22 2c 20 0a 20 20 20 20 20 (%s)\n", .
29e0: 20 20 20 20 20 20 20 31 2b 69 4c 61 73 74 2d 69 1+iLast-i
29f0: 46 69 72 73 74 2c 20 69 46 69 72 73 74 2c 20 70 First, iFirst, p
2a00: 57 72 69 74 65 2d 3e 70 46 69 6c 65 2d 3e 7a 4e Write->pFile->zN
2a10: 61 6d 65 0a 20 20 20 20 20 20 20 20 29 3b 0a 23 ame. );.#
2a20: 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 20 20 7a endif.. z
2a30: 47 61 72 62 61 67 65 20 3d 20 63 72 61 73 68 5f Garbage = crash_
2a40: 6d 61 6c 6c 6f 63 28 67 2e 69 53 65 63 74 6f 72 malloc(g.iSector
2a50: 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 69 Size);. i
2a60: 66 28 20 7a 47 61 72 62 61 67 65 20 29 7b 0a 20 f( zGarbage ){.
2a70: 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
2a80: 5f 69 6e 74 36 34 20 69 3b 0a 20 20 20 20 20 20 _int64 i;.
2a90: 20 20 20 20 66 6f 72 28 69 3d 69 46 69 72 73 74 for(i=iFirst
2aa0: 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ; rc==SQLITE_OK
2ab0: 26 26 20 69 3c 3d 69 4c 61 73 74 3b 20 69 2b 2b && i<=iLast; i++
2ac0: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 ){. s
2ad0: 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
2ae0: 73 28 67 2e 69 53 65 63 74 6f 72 53 69 7a 65 2c s(g.iSectorSize,
2af0: 20 7a 47 61 72 62 61 67 65 29 3b 20 0a 20 20 20 zGarbage); .
2b00: 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
2b10: 6c 69 74 65 33 4f 73 57 72 69 74 65 28 0a 20 20 lite3OsWrite(.
2b20: 20 20 20 20 20 20 20 20 20 20 20 20 70 52 65 61 pRea
2b30: 6c 46 69 6c 65 2c 20 7a 47 61 72 62 61 67 65 2c lFile, zGarbage,
2b40: 20 67 2e 69 53 65 63 74 6f 72 53 69 7a 65 2c 20 g.iSectorSize,
2b50: 69 2a 67 2e 69 53 65 63 74 6f 72 53 69 7a 65 0a i*g.iSectorSize.
2b60: 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 );.
2b70: 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
2b80: 20 20 20 20 20 63 72 61 73 68 5f 66 72 65 65 28 crash_free(
2b90: 7a 47 61 72 62 61 67 65 29 3b 0a 20 20 20 20 20 zGarbage);.
2ba0: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
2bb0: 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
2bc0: 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 7d NOMEM;. }
2bd0: 0a 0a 20 20 20 20 20 20 20 20 70 70 50 74 72 20 .. ppPtr
2be0: 3d 20 26 70 57 72 69 74 65 2d 3e 70 4e 65 78 74 = &pWrite->pNext
2bf0: 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
2c00: 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
2c10: 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 default:.
2c20: 20 61 73 73 65 72 74 28 21 22 43 61 6e 6e 6f 74 assert(!"Cannot
2c30: 20 68 61 70 70 65 6e 22 29 3b 0a 20 20 20 20 7d happen");. }
2c40: 0a 0a 20 20 20 20 69 66 28 20 70 57 72 69 74 65 .. if( pWrite
2c50: 3d 3d 70 46 69 6e 61 6c 20 29 20 62 72 65 61 6b ==pFinal ) break
2c60: 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d ;. }.. if( rc=
2c70: 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 73 =SQLITE_OK && is
2c80: 43 72 61 73 68 20 29 7b 0a 20 20 20 20 65 78 69 Crash ){. exi
2c90: 74 28 2d 31 29 3b 0a 20 20 7d 0a 0a 20 20 66 6f t(-1);. }.. fo
2ca0: 72 28 70 57 72 69 74 65 3d 67 2e 70 57 72 69 74 r(pWrite=g.pWrit
2cb0: 65 4c 69 73 74 3b 20 70 57 72 69 74 65 20 26 26 eList; pWrite &&
2cc0: 20 70 57 72 69 74 65 2d 3e 70 4e 65 78 74 3b 20 pWrite->pNext;
2cd0: 70 57 72 69 74 65 3d 70 57 72 69 74 65 2d 3e 70 pWrite=pWrite->p
2ce0: 4e 65 78 74 29 3b 0a 20 20 67 2e 70 57 72 69 74 Next);. g.pWrit
2cf0: 65 4c 69 73 74 45 6e 64 20 3d 20 70 57 72 69 74 eListEnd = pWrit
2d00: 65 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b e;.. return rc;
2d10: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e .}../*.** Add an
2d20: 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 65 6e entry to the en
2d30: 64 20 6f 66 20 74 68 65 20 77 72 69 74 65 2d 6c d of the write-l
2d40: 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ist..*/.static i
2d50: 6e 74 20 77 72 69 74 65 4c 69 73 74 41 70 70 65 nt writeListAppe
2d60: 6e 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 nd(. sqlite3_fi
2d70: 6c 65 20 2a 70 46 69 6c 65 2c 0a 20 20 73 71 6c le *pFile,. sql
2d80: 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 66 73 ite3_int64 iOffs
2d90: 65 74 2c 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a et,. const u8 *
2da0: 7a 42 75 66 2c 0a 20 20 69 6e 74 20 6e 42 75 66 zBuf,. int nBuf
2db0: 0a 29 7b 0a 20 20 57 72 69 74 65 42 75 66 66 65 .){. WriteBuffe
2dc0: 72 20 2a 70 4e 65 77 3b 0a 0a 20 20 61 73 73 65 r *pNew;.. asse
2dd0: 72 74 28 28 7a 42 75 66 20 26 26 20 6e 42 75 66 rt((zBuf && nBuf
2de0: 29 20 7c 7c 20 28 21 6e 42 75 66 20 26 26 20 21 ) || (!nBuf && !
2df0: 7a 42 75 66 29 29 3b 0a 0a 20 20 70 4e 65 77 20 zBuf));.. pNew
2e00: 3d 20 28 57 72 69 74 65 42 75 66 66 65 72 20 2a = (WriteBuffer *
2e10: 29 63 72 61 73 68 5f 6d 61 6c 6c 6f 63 28 73 69 )crash_malloc(si
2e20: 7a 65 6f 66 28 57 72 69 74 65 42 75 66 66 65 72 zeof(WriteBuffer
2e30: 29 20 2b 20 6e 42 75 66 29 3b 0a 20 20 69 66 28 ) + nBuf);. if(
2e40: 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 pNew==0 ){.
2e50: 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 fprintf(stderr,
2e60: 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 69 "out of memory i
2e70: 6e 20 74 68 65 20 63 72 61 73 68 20 73 69 6d 75 n the crash simu
2e80: 6c 61 74 6f 72 5c 6e 22 29 3b 0a 20 20 7d 0a 20 lator\n");. }.
2e90: 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c 20 30 2c memset(pNew, 0,
2ea0: 20 73 69 7a 65 6f 66 28 57 72 69 74 65 42 75 66 sizeof(WriteBuf
2eb0: 66 65 72 29 2b 6e 42 75 66 29 3b 0a 20 20 70 4e fer)+nBuf);. pN
2ec0: 65 77 2d 3e 69 4f 66 66 73 65 74 20 3d 20 69 4f ew->iOffset = iO
2ed0: 66 66 73 65 74 3b 0a 20 20 70 4e 65 77 2d 3e 6e ffset;. pNew->n
2ee0: 42 75 66 20 3d 20 6e 42 75 66 3b 0a 20 20 70 4e Buf = nBuf;. pN
2ef0: 65 77 2d 3e 70 46 69 6c 65 20 3d 20 28 43 72 61 ew->pFile = (Cra
2f00: 73 68 46 69 6c 65 20 2a 29 70 46 69 6c 65 3b 0a shFile *)pFile;.
2f10: 20 20 69 66 28 20 7a 42 75 66 20 29 7b 0a 20 20 if( zBuf ){.
2f20: 20 20 70 4e 65 77 2d 3e 7a 42 75 66 20 3d 20 28 pNew->zBuf = (
2f30: 75 38 20 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 u8 *)&pNew[1];.
2f40: 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e memcpy(pNew->
2f50: 7a 42 75 66 2c 20 7a 42 75 66 2c 20 6e 42 75 66 zBuf, zBuf, nBuf
2f60: 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 67 2e );. }.. if( g.
2f70: 70 57 72 69 74 65 4c 69 73 74 20 29 7b 0a 20 20 pWriteList ){.
2f80: 20 20 61 73 73 65 72 74 28 67 2e 70 57 72 69 74 assert(g.pWrit
2f90: 65 4c 69 73 74 45 6e 64 29 3b 0a 20 20 20 20 67 eListEnd);. g
2fa0: 2e 70 57 72 69 74 65 4c 69 73 74 45 6e 64 2d 3e .pWriteListEnd->
2fb0: 70 4e 65 78 74 20 3d 20 70 4e 65 77 3b 0a 20 20 pNext = pNew;.
2fc0: 7d 65 6c 73 65 7b 0a 20 20 20 20 67 2e 70 57 72 }else{. g.pWr
2fd0: 69 74 65 4c 69 73 74 20 3d 20 70 4e 65 77 3b 0a iteList = pNew;.
2fe0: 20 20 7d 0a 20 20 67 2e 70 57 72 69 74 65 4c 69 }. g.pWriteLi
2ff0: 73 74 45 6e 64 20 3d 20 70 4e 65 77 3b 0a 20 20 stEnd = pNew;.
3000: 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
3010: 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c _OK;.}../*.** Cl
3020: 6f 73 65 20 61 20 63 72 61 73 68 2d 66 69 6c 65 ose a crash-file
3030: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
3040: 63 66 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f cfClose(sqlite3_
3050: 66 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 file *pFile){.
3060: 43 72 61 73 68 46 69 6c 65 20 2a 70 43 72 61 73 CrashFile *pCras
3070: 68 20 3d 20 28 43 72 61 73 68 46 69 6c 65 20 2a h = (CrashFile *
3080: 29 70 46 69 6c 65 3b 0a 20 20 77 72 69 74 65 4c )pFile;. writeL
3090: 69 73 74 53 79 6e 63 28 70 43 72 61 73 68 2c 20 istSync(pCrash,
30a0: 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 0);. sqlite3OsC
30b0: 6c 6f 73 65 28 70 43 72 61 73 68 2d 3e 70 52 65 lose(pCrash->pRe
30c0: 61 6c 46 69 6c 65 29 3b 0a 20 20 72 65 74 75 72 alFile);. retur
30d0: 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
30e0: 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 /*.** Read data
30f0: 66 72 6f 6d 20 61 20 63 72 61 73 68 2d 66 69 6c from a crash-fil
3100: 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
3110: 20 63 66 52 65 61 64 28 0a 20 20 73 71 6c 69 74 cfRead(. sqlit
3120: 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 e3_file *pFile,
3130: 0a 20 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 0a . void *zBuf, .
3140: 20 20 69 6e 74 20 69 41 6d 74 2c 20 0a 20 20 73 int iAmt, . s
3150: 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 qlite_int64 iOfs
3160: 74 0a 29 7b 0a 20 20 43 72 61 73 68 46 69 6c 65 t.){. CrashFile
3170: 20 2a 70 43 72 61 73 68 20 3d 20 28 43 72 61 73 *pCrash = (Cras
3180: 68 46 69 6c 65 20 2a 29 70 46 69 6c 65 3b 0a 0a hFile *)pFile;..
3190: 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 66 /* Check the f
31a0: 69 6c 65 2d 73 69 7a 65 20 74 6f 20 73 65 65 20 ile-size to see
31b0: 69 66 20 74 68 69 73 20 69 73 20 61 20 73 68 6f if this is a sho
31c0: 72 74 2d 72 65 61 64 20 2a 2f 0a 20 20 69 66 28 rt-read */. if(
31d0: 20 70 43 72 61 73 68 2d 3e 69 53 69 7a 65 3c 28 pCrash->iSize<(
31e0: 69 4f 66 73 74 2b 69 41 6d 74 29 20 29 7b 0a 20 iOfst+iAmt) ){.
31f0: 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
3200: 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
3210: 44 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d 63 70 79 D;. }.. memcpy
3220: 28 7a 42 75 66 2c 20 26 70 43 72 61 73 68 2d 3e (zBuf, &pCrash->
3230: 7a 44 61 74 61 5b 69 4f 66 73 74 5d 2c 20 69 41 zData[iOfst], iA
3240: 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 mt);. return SQ
3250: 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
3260: 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20 * Write data to
3270: 61 20 63 72 61 73 68 2d 66 69 6c 65 2e 0a 2a 2f a crash-file..*/
3280: 0a 73 74 61 74 69 63 20 69 6e 74 20 63 66 57 72 .static int cfWr
3290: 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 ite(. sqlite3_f
32a0: 69 6c 65 20 2a 70 46 69 6c 65 2c 20 0a 20 20 63 ile *pFile, . c
32b0: 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c onst void *zBuf,
32c0: 20 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 0a 20 . int iAmt, .
32d0: 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f sqlite_int64 iO
32e0: 66 73 74 0a 29 7b 0a 20 20 43 72 61 73 68 46 69 fst.){. CrashFi
32f0: 6c 65 20 2a 70 43 72 61 73 68 20 3d 20 28 43 72 le *pCrash = (Cr
3300: 61 73 68 46 69 6c 65 20 2a 29 70 46 69 6c 65 3b ashFile *)pFile;
3310: 0a 20 20 69 66 28 20 69 41 6d 74 2b 69 4f 66 73 . if( iAmt+iOfs
3320: 74 3e 70 43 72 61 73 68 2d 3e 69 53 69 7a 65 20 t>pCrash->iSize
3330: 29 7b 0a 20 20 20 20 70 43 72 61 73 68 2d 3e 69 ){. pCrash->i
3340: 53 69 7a 65 20 3d 20 69 41 6d 74 2b 69 4f 66 73 Size = iAmt+iOfs
3350: 74 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 t;. }. while(
3360: 70 43 72 61 73 68 2d 3e 69 53 69 7a 65 3e 70 43 pCrash->iSize>pC
3370: 72 61 73 68 2d 3e 6e 44 61 74 61 20 29 7b 0a 20 rash->nData ){.
3380: 20 20 20 75 38 20 2a 7a 4e 65 77 3b 0a 20 20 20 u8 *zNew;.
3390: 20 69 6e 74 20 6e 4e 65 77 20 3d 20 28 70 43 72 int nNew = (pCr
33a0: 61 73 68 2d 3e 6e 44 61 74 61 2a 32 29 20 2b 20 ash->nData*2) +
33b0: 34 30 39 36 3b 0a 20 20 20 20 7a 4e 65 77 20 3d 4096;. zNew =
33c0: 20 63 72 61 73 68 5f 72 65 61 6c 6c 6f 63 28 70 crash_realloc(p
33d0: 43 72 61 73 68 2d 3e 7a 44 61 74 61 2c 20 6e 4e Crash->zData, nN
33e0: 65 77 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 4e ew);. if( !zN
33f0: 65 77 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ew ){. retu
3400: 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
3410: 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 . }. memse
3420: 74 28 26 7a 4e 65 77 5b 70 43 72 61 73 68 2d 3e t(&zNew[pCrash->
3430: 6e 44 61 74 61 5d 2c 20 30 2c 20 6e 4e 65 77 2d nData], 0, nNew-
3440: 70 43 72 61 73 68 2d 3e 6e 44 61 74 61 29 3b 0a pCrash->nData);.
3450: 20 20 20 20 70 43 72 61 73 68 2d 3e 6e 44 61 74 pCrash->nDat
3460: 61 20 3d 20 6e 4e 65 77 3b 0a 20 20 20 20 70 43 a = nNew;. pC
3470: 72 61 73 68 2d 3e 7a 44 61 74 61 20 3d 20 7a 4e rash->zData = zN
3480: 65 77 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 ew;. }. memcpy
3490: 28 26 70 43 72 61 73 68 2d 3e 7a 44 61 74 61 5b (&pCrash->zData[
34a0: 69 4f 66 73 74 5d 2c 20 7a 42 75 66 2c 20 69 41 iOfst], zBuf, iA
34b0: 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 77 72 mt);. return wr
34c0: 69 74 65 4c 69 73 74 41 70 70 65 6e 64 28 70 46 iteListAppend(pF
34d0: 69 6c 65 2c 20 69 4f 66 73 74 2c 20 7a 42 75 66 ile, iOfst, zBuf
34e0: 2c 20 69 41 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a , iAmt);.}../*.*
34f0: 2a 20 54 72 75 6e 63 61 74 65 20 61 20 63 72 61 * Truncate a cra
3500: 73 68 2d 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 sh-file..*/.stat
3510: 69 63 20 69 6e 74 20 63 66 54 72 75 6e 63 61 74 ic int cfTruncat
3520: 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
3530: 70 46 69 6c 65 2c 20 73 71 6c 69 74 65 5f 69 6e pFile, sqlite_in
3540: 74 36 34 20 73 69 7a 65 29 7b 0a 20 20 43 72 61 t64 size){. Cra
3550: 73 68 46 69 6c 65 20 2a 70 43 72 61 73 68 20 3d shFile *pCrash =
3560: 20 28 43 72 61 73 68 46 69 6c 65 20 2a 29 70 46 (CrashFile *)pF
3570: 69 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 73 69 ile;. assert(si
3580: 7a 65 3e 3d 30 29 3b 0a 20 20 69 66 28 20 70 43 ze>=0);. if( pC
3590: 72 61 73 68 2d 3e 69 53 69 7a 65 3e 73 69 7a 65 rash->iSize>size
35a0: 20 29 7b 0a 20 20 20 20 70 43 72 61 73 68 2d 3e ){. pCrash->
35b0: 69 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 iSize = size;.
35c0: 7d 0a 20 20 72 65 74 75 72 6e 20 77 72 69 74 65 }. return write
35d0: 4c 69 73 74 41 70 70 65 6e 64 28 70 46 69 6c 65 ListAppend(pFile
35e0: 2c 20 73 69 7a 65 2c 20 30 2c 20 30 29 3b 0a 7d , size, 0, 0);.}
35f0: 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 61 20 63 ../*.** Sync a c
3600: 72 61 73 68 2d 66 69 6c 65 2e 0a 2a 2f 0a 73 74 rash-file..*/.st
3610: 61 74 69 63 20 69 6e 74 20 63 66 53 79 6e 63 28 atic int cfSync(
3620: 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 sqlite3_file *pF
3630: 69 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b ile, int flags){
3640: 0a 20 20 43 72 61 73 68 46 69 6c 65 20 2a 70 43 . CrashFile *pC
3650: 72 61 73 68 20 3d 20 28 43 72 61 73 68 46 69 6c rash = (CrashFil
3660: 65 20 2a 29 70 46 69 6c 65 3b 0a 20 20 69 6e 74 e *)pFile;. int
3670: 20 69 73 43 72 61 73 68 20 3d 20 30 3b 0a 0a 20 isCrash = 0;..
3680: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
3690: 6d 65 20 3d 20 70 43 72 61 73 68 2d 3e 7a 4e 61 me = pCrash->zNa
36a0: 6d 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 me;. const char
36b0: 20 2a 7a 43 72 61 73 68 46 69 6c 65 20 3d 20 67 *zCrashFile = g
36c0: 2e 7a 43 72 61 73 68 46 69 6c 65 3b 0a 20 20 69 .zCrashFile;. i
36d0: 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 nt nName = strle
36e0: 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 n(zName);. int
36f0: 6e 43 72 61 73 68 46 69 6c 65 20 3d 20 73 74 72 nCrashFile = str
3700: 6c 65 6e 28 7a 43 72 61 73 68 46 69 6c 65 29 3b len(zCrashFile);
3710: 0a 0a 20 20 69 66 28 20 6e 43 72 61 73 68 46 69 .. if( nCrashFi
3720: 6c 65 3e 30 20 26 26 20 7a 43 72 61 73 68 46 69 le>0 && zCrashFi
3730: 6c 65 5b 6e 43 72 61 73 68 46 69 6c 65 2d 31 5d le[nCrashFile-1]
3740: 3d 3d 27 2a 27 20 29 7b 0a 20 20 20 20 6e 43 72 =='*' ){. nCr
3750: 61 73 68 46 69 6c 65 2d 2d 3b 0a 20 20 20 20 69 ashFile--;. i
3760: 66 28 20 6e 4e 61 6d 65 3e 6e 43 72 61 73 68 46 f( nName>nCrashF
3770: 69 6c 65 20 29 20 6e 4e 61 6d 65 20 3d 20 6e 43 ile ) nName = nC
3780: 72 61 73 68 46 69 6c 65 3b 0a 20 20 7d 0a 0a 20 rashFile;. }..
3790: 20 69 66 28 20 6e 4e 61 6d 65 3d 3d 6e 43 72 61 if( nName==nCra
37a0: 73 68 46 69 6c 65 20 26 26 20 30 3d 3d 6d 65 6d shFile && 0==mem
37b0: 63 6d 70 28 7a 4e 61 6d 65 2c 20 7a 43 72 61 73 cmp(zName, zCras
37c0: 68 46 69 6c 65 2c 20 6e 4e 61 6d 65 29 20 29 7b hFile, nName) ){
37d0: 0a 20 20 20 20 69 66 28 20 28 2d 2d 67 2e 69 43 . if( (--g.iC
37e0: 72 61 73 68 29 3d 3d 30 20 29 20 69 73 43 72 61 rash)==0 ) isCra
37f0: 73 68 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 72 sh = 1;. }.. r
3800: 65 74 75 72 6e 20 77 72 69 74 65 4c 69 73 74 53 eturn writeListS
3810: 79 6e 63 28 70 43 72 61 73 68 2c 20 69 73 43 72 ync(pCrash, isCr
3820: 61 73 68 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ash);.}../*.** R
3830: 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e eturn the curren
3840: 74 20 66 69 6c 65 2d 73 69 7a 65 20 6f 66 20 74 t file-size of t
3850: 68 65 20 63 72 61 73 68 2d 66 69 6c 65 2e 0a 2a he crash-file..*
3860: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 66 46 /.static int cfF
3870: 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f ileSize(sqlite3_
3880: 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 73 71 6c file *pFile, sql
3890: 69 74 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 ite_int64 *pSize
38a0: 29 7b 0a 20 20 43 72 61 73 68 46 69 6c 65 20 2a ){. CrashFile *
38b0: 70 43 72 61 73 68 20 3d 20 28 43 72 61 73 68 46 pCrash = (CrashF
38c0: 69 6c 65 20 2a 29 70 46 69 6c 65 3b 0a 20 20 2a ile *)pFile;. *
38d0: 70 53 69 7a 65 20 3d 20 28 69 36 34 29 70 43 72 pSize = (i64)pCr
38e0: 61 73 68 2d 3e 69 53 69 7a 65 3b 0a 20 20 72 65 ash->iSize;. re
38f0: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
3900: 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 73 20 72 }../*.** Calls r
3910: 65 6c 61 74 65 64 20 74 6f 20 66 69 6c 65 2d 6c elated to file-l
3920: 6f 63 6b 73 20 61 72 65 20 70 61 73 73 65 64 20 ocks are passed
3930: 6f 6e 20 74 6f 20 74 68 65 20 72 65 61 6c 20 66 on to the real f
3940: 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 ile handle..*/.s
3950: 74 61 74 69 63 20 69 6e 74 20 63 66 4c 6f 63 6b tatic int cfLock
3960: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
3970: 46 69 6c 65 2c 20 69 6e 74 20 65 4c 6f 63 6b 29 File, int eLock)
3980: 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
3990: 65 33 4f 73 4c 6f 63 6b 28 28 28 43 72 61 73 68 e3OsLock(((Crash
39a0: 46 69 6c 65 20 2a 29 70 46 69 6c 65 29 2d 3e 70 File *)pFile)->p
39b0: 52 65 61 6c 46 69 6c 65 2c 20 65 4c 6f 63 6b 29 RealFile, eLock)
39c0: 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 63 ;.}.static int c
39d0: 66 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f fUnlock(sqlite3_
39e0: 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 69 6e 74 file *pFile, int
39f0: 20 65 4c 6f 63 6b 29 7b 0a 20 20 72 65 74 75 72 eLock){. retur
3a00: 6e 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 n sqlite3OsUnloc
3a10: 6b 28 28 28 43 72 61 73 68 46 69 6c 65 20 2a 29 k(((CrashFile *)
3a20: 70 46 69 6c 65 29 2d 3e 70 52 65 61 6c 46 69 6c pFile)->pRealFil
3a30: 65 2c 20 65 4c 6f 63 6b 29 3b 0a 7d 0a 73 74 61 e, eLock);.}.sta
3a40: 74 69 63 20 69 6e 74 20 63 66 43 68 65 63 6b 52 tic int cfCheckR
3a50: 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 eservedLock(sqli
3a60: 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c te3_file *pFile,
3a70: 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a int *pResOut){.
3a80: 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
3a90: 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c OsCheckReservedL
3aa0: 6f 63 6b 28 28 28 43 72 61 73 68 46 69 6c 65 20 ock(((CrashFile
3ab0: 2a 29 70 46 69 6c 65 29 2d 3e 70 52 65 61 6c 46 *)pFile)->pRealF
3ac0: 69 6c 65 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d ile, pResOut);.}
3ad0: 0a 73 74 61 74 69 63 20 69 6e 74 20 63 66 46 69 .static int cfFi
3ae0: 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 leControl(sqlite
3af0: 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 69 3_file *pFile, i
3b00: 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 nt op, void *pAr
3b10: 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c g){. return sql
3b20: 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f ite3OsFileContro
3b30: 6c 28 28 28 43 72 61 73 68 46 69 6c 65 20 2a 29 l(((CrashFile *)
3b40: 70 46 69 6c 65 29 2d 3e 70 52 65 61 6c 46 69 6c pFile)->pRealFil
3b50: 65 2c 20 6f 70 2c 20 70 41 72 67 29 3b 0a 7d 0a e, op, pArg);.}.
3b60: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 78 53 65 63 74 ./*.** The xSect
3b70: 6f 72 53 69 7a 65 28 29 20 61 6e 64 20 78 44 65 orSize() and xDe
3b80: 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
3b90: 69 63 73 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 ics() functions
3ba0: 72 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 67 6c return.** the gl
3bb0: 6f 62 61 6c 20 76 61 6c 75 65 73 20 63 6f 6e 66 obal values conf
3bc0: 69 67 75 72 65 64 20 62 79 20 74 68 65 20 5b 73 igured by the [s
3bd0: 71 6c 69 74 65 5f 63 72 61 73 68 70 61 72 61 6d qlite_crashparam
3be0: 73 5d 20 74 63 6c 0a 2a 20 20 69 6e 74 65 72 66 s] tcl.* interf
3bf0: 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ace..*/.static i
3c00: 6e 74 20 63 66 53 65 63 74 6f 72 53 69 7a 65 28 nt cfSectorSize(
3c10: 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 sqlite3_file *pF
3c20: 69 6c 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 67 ile){. return g
3c30: 2e 69 53 65 63 74 6f 72 53 69 7a 65 3b 0a 7d 0a .iSectorSize;.}.
3c40: 73 74 61 74 69 63 20 69 6e 74 20 63 66 44 65 76 static int cfDev
3c50: 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
3c60: 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 cs(sqlite3_file
3c70: 2a 70 46 69 6c 65 29 7b 0a 20 20 72 65 74 75 72 *pFile){. retur
3c80: 6e 20 67 2e 69 44 65 76 69 63 65 43 68 61 72 61 n g.iDeviceChara
3c90: 63 74 65 72 69 73 74 69 63 73 3b 0a 7d 0a 0a 73 cteristics;.}..s
3ca0: 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 tatic const sqli
3cb0: 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 43 te3_io_methods C
3cc0: 72 61 73 68 46 69 6c 65 56 74 61 62 20 3d 20 7b rashFileVtab = {
3cd0: 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 . 1,
3ce0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3cf0: 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a /* iVersion */.
3d00: 20 20 63 66 43 6c 6f 73 65 2c 20 20 20 20 20 20 cfClose,
3d10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3d20: 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 63 /* xClose */. c
3d30: 66 52 65 61 64 2c 20 20 20 20 20 20 20 20 20 20 fRead,
3d40: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
3d50: 78 52 65 61 64 20 2a 2f 0a 20 20 63 66 57 72 69 xRead */. cfWri
3d60: 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 te,
3d70: 20 20 20 20 20 20 20 20 20 2f 2a 20 78 57 72 69 /* xWri
3d80: 74 65 20 2a 2f 0a 20 20 63 66 54 72 75 6e 63 61 te */. cfTrunca
3d90: 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 te,
3da0: 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 /* xTrunca
3db0: 74 65 20 2a 2f 0a 20 20 63 66 53 79 6e 63 2c 20 te */. cfSync,
3dc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3dd0: 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a /* xSync *
3de0: 2f 0a 20 20 63 66 46 69 6c 65 53 69 7a 65 2c 20 /. cfFileSize,
3df0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3e00: 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a /* xFileSize *
3e10: 2f 0a 20 20 63 66 4c 6f 63 6b 2c 20 20 20 20 20 /. cfLock,
3e20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3e30: 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 /* xLock */.
3e40: 63 66 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 cfUnlock,
3e50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
3e60: 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 63 66 xUnlock */. cf
3e70: 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
3e80: 6b 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 k, /* x
3e90: 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
3ea0: 6b 20 2a 2f 0a 20 20 63 66 46 69 6c 65 43 6f 6e k */. cfFileCon
3eb0: 74 72 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 trol,
3ec0: 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e /* xFileCon
3ed0: 74 72 6f 6c 20 2a 2f 0a 20 20 63 66 53 65 63 74 trol */. cfSect
3ee0: 6f 72 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20 orSize,
3ef0: 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 74 /* xSect
3f00: 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 63 66 44 65 orSize */. cfDe
3f10: 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
3f20: 69 63 73 20 20 20 20 20 20 20 2f 2a 20 78 44 65 ics /* xDe
3f30: 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
3f40: 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ics */.};../*.**
3f50: 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 64 61 74 Application dat
3f60: 61 20 66 6f 72 20 74 68 65 20 63 72 61 73 68 20 a for the crash
3f70: 56 46 53 0a 2a 2f 0a 73 74 72 75 63 74 20 63 72 VFS.*/.struct cr
3f80: 61 73 68 41 70 70 44 61 74 61 20 7b 0a 20 20 73 ashAppData {. s
3f90: 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 4f 72 69 qlite3_vfs *pOri
3fa0: 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
3fb0: 20 20 20 20 20 2f 2a 20 57 72 61 70 70 65 64 20 /* Wrapped
3fc0: 76 66 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f vfs structure */
3fd0: 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 .};../*.** Open
3fe0: 61 20 63 72 61 73 68 2d 66 69 6c 65 20 66 69 6c a crash-file fil
3ff0: 65 20 68 61 6e 64 6c 65 2e 0a 2a 2a 0a 2a 2a 20 e handle..**.**
4000: 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 The caller will
4010: 68 61 76 65 20 61 6c 6c 6f 63 61 74 65 64 20 70 have allocated p
4020: 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 62 79 Vfs->szOsFile by
4030: 74 65 73 20 6f 66 20 73 70 61 63 65 0a 2a 2a 20 tes of space.**
4040: 61 74 20 70 46 69 6c 65 2e 20 54 68 69 73 20 66 at pFile. This f
4050: 69 6c 65 20 75 73 65 73 20 74 68 69 73 20 73 70 ile uses this sp
4060: 61 63 65 20 66 6f 72 20 74 68 65 20 43 72 61 73 ace for the Cras
4070: 68 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 0a hFile structure.
4080: 2a 2a 20 61 6e 64 20 61 6c 6c 6f 63 61 74 65 73 ** and allocates
4090: 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 22 space for the "
40a0: 72 65 61 6c 22 20 66 69 6c 65 20 73 74 72 75 63 real" file struc
40b0: 74 75 72 65 20 75 73 69 6e 67 20 0a 2a 2a 20 73 ture using .** s
40c0: 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e qlite3_malloc().
40d0: 20 54 68 65 20 61 73 73 75 6d 70 74 69 6f 6e 20 The assumption
40e0: 68 65 72 65 20 69 73 20 28 70 56 66 73 2d 3e 73 here is (pVfs->s
40f0: 7a 4f 73 46 69 6c 65 29 20 69 73 0a 2a 2a 20 65 zOsFile) is.** e
4100: 71 75 61 6c 20 6f 72 20 67 72 65 61 74 65 72 20 qual or greater
4110: 74 68 61 6e 20 73 69 7a 65 6f 66 28 43 72 61 73 than sizeof(Cras
4120: 68 46 69 6c 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 hFile)..*/.stati
4130: 63 20 69 6e 74 20 63 66 4f 70 65 6e 28 0a 20 20 c int cfOpen(.
4140: 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 43 66 sqlite3_vfs *pCf
4150: 56 66 73 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 Vfs,. const cha
4160: 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 73 71 6c 69 r *zName,. sqli
4170: 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c te3_file *pFile,
4180: 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 . int flags,.
4190: 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 0a 29 int *pOutFlags.)
41a0: 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 {. sqlite3_vfs
41b0: 2a 70 56 66 73 20 3d 20 28 73 71 6c 69 74 65 33 *pVfs = (sqlite3
41c0: 5f 76 66 73 20 2a 29 70 43 66 56 66 73 2d 3e 70 _vfs *)pCfVfs->p
41d0: 41 70 70 44 61 74 61 3b 0a 20 20 69 6e 74 20 72 AppData;. int r
41e0: 63 3b 0a 20 20 43 72 61 73 68 46 69 6c 65 20 2a c;. CrashFile *
41f0: 70 57 72 61 70 70 65 72 20 3d 20 28 43 72 61 73 pWrapper = (Cras
4200: 68 46 69 6c 65 20 2a 29 70 46 69 6c 65 3b 0a 20 hFile *)pFile;.
4210: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
4220: 52 65 61 6c 20 3d 20 28 73 71 6c 69 74 65 33 5f Real = (sqlite3_
4230: 66 69 6c 65 2a 29 26 70 57 72 61 70 70 65 72 5b file*)&pWrapper[
4240: 31 5d 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 70 57 1];.. memset(pW
4250: 72 61 70 70 65 72 2c 20 30 2c 20 73 69 7a 65 6f rapper, 0, sizeo
4260: 66 28 43 72 61 73 68 46 69 6c 65 29 29 3b 0a 20 f(CrashFile));.
4270: 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f rc = sqlite3OsO
4280: 70 65 6e 28 70 56 66 73 2c 20 7a 4e 61 6d 65 2c pen(pVfs, zName,
4290: 20 70 52 65 61 6c 2c 20 66 6c 61 67 73 2c 20 70 pReal, flags, p
42a0: 4f 75 74 46 6c 61 67 73 29 3b 0a 0a 20 20 69 66 OutFlags);.. if
42b0: 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
42c0: 29 7b 0a 20 20 20 20 69 36 34 20 69 53 69 7a 65 ){. i64 iSize
42d0: 3b 0a 20 20 20 20 70 57 72 61 70 70 65 72 2d 3e ;. pWrapper->
42e0: 70 4d 65 74 68 6f 64 20 3d 20 26 43 72 61 73 68 pMethod = &Crash
42f0: 46 69 6c 65 56 74 61 62 3b 0a 20 20 20 20 70 57 FileVtab;. pW
4300: 72 61 70 70 65 72 2d 3e 7a 4e 61 6d 65 20 3d 20 rapper->zName =
4310: 28 63 68 61 72 20 2a 29 7a 4e 61 6d 65 3b 0a 20 (char *)zName;.
4320: 20 20 20 70 57 72 61 70 70 65 72 2d 3e 70 52 65 pWrapper->pRe
4330: 61 6c 46 69 6c 65 20 3d 20 70 52 65 61 6c 3b 0a alFile = pReal;.
4340: 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
4350: 4f 73 46 69 6c 65 53 69 7a 65 28 70 52 65 61 6c OsFileSize(pReal
4360: 2c 20 26 69 53 69 7a 65 29 3b 0a 20 20 20 20 70 , &iSize);. p
4370: 57 72 61 70 70 65 72 2d 3e 69 53 69 7a 65 20 3d Wrapper->iSize =
4380: 20 28 69 6e 74 29 69 53 69 7a 65 3b 0a 20 20 7d (int)iSize;. }
4390: 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
43a0: 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 57 72 61 E_OK ){. pWra
43b0: 70 70 65 72 2d 3e 6e 44 61 74 61 20 3d 20 28 34 pper->nData = (4
43c0: 30 39 36 20 2b 20 70 57 72 61 70 70 65 72 2d 3e 096 + pWrapper->
43d0: 69 53 69 7a 65 29 3b 0a 20 20 20 20 70 57 72 61 iSize);. pWra
43e0: 70 70 65 72 2d 3e 7a 44 61 74 61 20 3d 20 63 72 pper->zData = cr
43f0: 61 73 68 5f 6d 61 6c 6c 6f 63 28 70 57 72 61 70 ash_malloc(pWrap
4400: 70 65 72 2d 3e 6e 44 61 74 61 29 3b 0a 20 20 20 per->nData);.
4410: 20 69 66 28 20 70 57 72 61 70 70 65 72 2d 3e 7a if( pWrapper->z
4420: 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 6d 65 Data ){. me
4430: 6d 73 65 74 28 70 57 72 61 70 70 65 72 2d 3e 7a mset(pWrapper->z
4440: 44 61 74 61 2c 20 30 2c 20 70 57 72 61 70 70 65 Data, 0, pWrappe
4450: 72 2d 3e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 r->nData);.
4460: 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 rc = sqlite3OsR
4470: 65 61 64 28 70 52 65 61 6c 2c 20 70 57 72 61 70 ead(pReal, pWrap
4480: 70 65 72 2d 3e 7a 44 61 74 61 2c 20 70 57 72 61 per->zData, pWra
4490: 70 70 65 72 2d 3e 69 53 69 7a 65 2c 20 30 29 3b pper->iSize, 0);
44a0: 20 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 . }else{.
44b0: 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
44c0: 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a OMEM;. }. }.
44d0: 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
44e0: 5f 4f 4b 20 26 26 20 70 57 72 61 70 70 65 72 2d _OK && pWrapper-
44f0: 3e 70 4d 65 74 68 6f 64 20 29 7b 0a 20 20 20 20 >pMethod ){.
4500: 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 sqlite3OsClose(p
4510: 46 69 6c 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 File);. }. ret
4520: 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 urn rc;.}..stati
4530: 63 20 69 6e 74 20 63 66 44 65 6c 65 74 65 28 73 c int cfDelete(s
4540: 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 43 66 56 qlite3_vfs *pCfV
4550: 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a fs, const char *
4560: 7a 50 61 74 68 2c 20 69 6e 74 20 64 69 72 53 79 zPath, int dirSy
4570: 6e 63 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 nc){. sqlite3_v
4580: 66 73 20 2a 70 56 66 73 20 3d 20 28 73 71 6c 69 fs *pVfs = (sqli
4590: 74 65 33 5f 76 66 73 20 2a 29 70 43 66 56 66 73 te3_vfs *)pCfVfs
45a0: 2d 3e 70 41 70 70 44 61 74 61 3b 0a 20 20 72 65 ->pAppData;. re
45b0: 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 65 6c 65 turn pVfs->xDele
45c0: 74 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 te(pVfs, zPath,
45d0: 64 69 72 53 79 6e 63 29 3b 0a 7d 0a 73 74 61 74 dirSync);.}.stat
45e0: 69 63 20 69 6e 74 20 63 66 41 63 63 65 73 73 28 ic int cfAccess(
45f0: 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
4600: 70 43 66 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 pCfVfs, . const
4610: 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 char *zPath, .
4620: 20 69 6e 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 int flags, . i
4630: 6e 74 20 2a 70 52 65 73 4f 75 74 0a 29 7b 0a 20 nt *pResOut.){.
4640: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
4650: 66 73 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 66 fs = (sqlite3_vf
4660: 73 20 2a 29 70 43 66 56 66 73 2d 3e 70 41 70 70 s *)pCfVfs->pApp
4670: 44 61 74 61 3b 0a 20 20 72 65 74 75 72 6e 20 70 Data;. return p
4680: 56 66 73 2d 3e 78 41 63 63 65 73 73 28 70 56 66 Vfs->xAccess(pVf
4690: 73 2c 20 7a 50 61 74 68 2c 20 66 6c 61 67 73 2c s, zPath, flags,
46a0: 20 70 52 65 73 4f 75 74 29 3b 0a 7d 0a 73 74 61 pResOut);.}.sta
46b0: 74 69 63 20 69 6e 74 20 63 66 46 75 6c 6c 50 61 tic int cfFullPa
46c0: 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 thname(. sqlite
46d0: 33 5f 76 66 73 20 2a 70 43 66 56 66 73 2c 20 0a 3_vfs *pCfVfs, .
46e0: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 const char *zP
46f0: 61 74 68 2c 20 0a 20 20 69 6e 74 20 6e 50 61 74 ath, . int nPat
4700: 68 4f 75 74 2c 0a 20 20 63 68 61 72 20 2a 7a 50 hOut,. char *zP
4710: 61 74 68 4f 75 74 0a 29 7b 0a 20 20 73 71 6c 69 athOut.){. sqli
4720: 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 te3_vfs *pVfs =
4730: 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 29 70 (sqlite3_vfs *)p
4740: 43 66 56 66 73 2d 3e 70 41 70 70 44 61 74 61 3b CfVfs->pAppData;
4750: 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e . return pVfs->
4760: 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 xFullPathname(pV
4770: 66 73 2c 20 7a 50 61 74 68 2c 20 6e 50 61 74 68 fs, zPath, nPath
4780: 4f 75 74 2c 20 7a 50 61 74 68 4f 75 74 29 3b 0a Out, zPathOut);.
4790: 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 63 }.static void *c
47a0: 66 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f fDlOpen(sqlite3_
47b0: 76 66 73 20 2a 70 43 66 56 66 73 2c 20 63 6f 6e vfs *pCfVfs, con
47c0: 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 29 7b st char *zPath){
47d0: 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
47e0: 70 56 66 73 20 3d 20 28 73 71 6c 69 74 65 33 5f pVfs = (sqlite3_
47f0: 76 66 73 20 2a 29 70 43 66 56 66 73 2d 3e 70 41 vfs *)pCfVfs->pA
4800: 70 70 44 61 74 61 3b 0a 20 20 72 65 74 75 72 6e ppData;. return
4810: 20 70 56 66 73 2d 3e 78 44 6c 4f 70 65 6e 28 70 pVfs->xDlOpen(p
4820: 56 66 73 2c 20 7a 50 61 74 68 29 3b 0a 7d 0a 73 Vfs, zPath);.}.s
4830: 74 61 74 69 63 20 76 6f 69 64 20 63 66 44 6c 45 tatic void cfDlE
4840: 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 rror(sqlite3_vfs
4850: 20 2a 70 43 66 56 66 73 2c 20 69 6e 74 20 6e 42 *pCfVfs, int nB
4860: 79 74 65 2c 20 63 68 61 72 20 2a 7a 45 72 72 4d yte, char *zErrM
4870: 73 67 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 sg){. sqlite3_v
4880: 66 73 20 2a 70 56 66 73 20 3d 20 28 73 71 6c 69 fs *pVfs = (sqli
4890: 74 65 33 5f 76 66 73 20 2a 29 70 43 66 56 66 73 te3_vfs *)pCfVfs
48a0: 2d 3e 70 41 70 70 44 61 74 61 3b 0a 20 20 70 56 ->pAppData;. pV
48b0: 66 73 2d 3e 78 44 6c 45 72 72 6f 72 28 70 56 66 fs->xDlError(pVf
48c0: 73 2c 20 6e 42 79 74 65 2c 20 7a 45 72 72 4d 73 s, nByte, zErrMs
48d0: 67 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 g);.}.static voi
48e0: 64 20 28 2a 63 66 44 6c 53 79 6d 28 73 71 6c 69 d (*cfDlSym(sqli
48f0: 74 65 33 5f 76 66 73 20 2a 70 43 66 56 66 73 2c te3_vfs *pCfVfs,
4900: 20 76 6f 69 64 20 2a 70 48 2c 20 63 6f 6e 73 74 void *pH, const
4910: 20 63 68 61 72 20 2a 7a 53 79 6d 29 29 28 76 6f char *zSym))(vo
4920: 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 id){. sqlite3_v
4930: 66 73 20 2a 70 56 66 73 20 3d 20 28 73 71 6c 69 fs *pVfs = (sqli
4940: 74 65 33 5f 76 66 73 20 2a 29 70 43 66 56 66 73 te3_vfs *)pCfVfs
4950: 2d 3e 70 41 70 70 44 61 74 61 3b 0a 20 20 72 65 ->pAppData;. re
4960: 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 6c 53 79 turn pVfs->xDlSy
4970: 6d 28 70 56 66 73 2c 20 70 48 2c 20 7a 53 79 6d m(pVfs, pH, zSym
4980: 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 );.}.static void
4990: 20 63 66 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 cfDlClose(sqlit
49a0: 65 33 5f 76 66 73 20 2a 70 43 66 56 66 73 2c 20 e3_vfs *pCfVfs,
49b0: 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a void *pHandle){.
49c0: 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
49d0: 56 66 73 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 Vfs = (sqlite3_v
49e0: 66 73 20 2a 29 70 43 66 56 66 73 2d 3e 70 41 70 fs *)pCfVfs->pAp
49f0: 70 44 61 74 61 3b 0a 20 20 70 56 66 73 2d 3e 78 pData;. pVfs->x
4a00: 44 6c 43 6c 6f 73 65 28 70 56 66 73 2c 20 70 48 DlClose(pVfs, pH
4a10: 61 6e 64 6c 65 29 3b 0a 7d 0a 73 74 61 74 69 63 andle);.}.static
4a20: 20 69 6e 74 20 63 66 52 61 6e 64 6f 6d 6e 65 73 int cfRandomnes
4a30: 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 s(sqlite3_vfs *p
4a40: 43 66 56 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 CfVfs, int nByte
4a50: 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 , char *zBufOut)
4a60: 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 {. sqlite3_vfs
4a70: 2a 70 56 66 73 20 3d 20 28 73 71 6c 69 74 65 33 *pVfs = (sqlite3
4a80: 5f 76 66 73 20 2a 29 70 43 66 56 66 73 2d 3e 70 _vfs *)pCfVfs->p
4a90: 41 70 70 44 61 74 61 3b 0a 20 20 72 65 74 75 72 AppData;. retur
4aa0: 6e 20 70 56 66 73 2d 3e 78 52 61 6e 64 6f 6d 6e n pVfs->xRandomn
4ab0: 65 73 73 28 70 56 66 73 2c 20 6e 42 79 74 65 2c ess(pVfs, nByte,
4ac0: 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 73 74 61 zBufOut);.}.sta
4ad0: 74 69 63 20 69 6e 74 20 63 66 53 6c 65 65 70 28 tic int cfSleep(
4ae0: 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 43 66 sqlite3_vfs *pCf
4af0: 56 66 73 2c 20 69 6e 74 20 6e 4d 69 63 72 6f 29 Vfs, int nMicro)
4b00: 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 {. sqlite3_vfs
4b10: 2a 70 56 66 73 20 3d 20 28 73 71 6c 69 74 65 33 *pVfs = (sqlite3
4b20: 5f 76 66 73 20 2a 29 70 43 66 56 66 73 2d 3e 70 _vfs *)pCfVfs->p
4b30: 41 70 70 44 61 74 61 3b 0a 20 20 72 65 74 75 72 AppData;. retur
4b40: 6e 20 70 56 66 73 2d 3e 78 53 6c 65 65 70 28 70 n pVfs->xSleep(p
4b50: 56 66 73 2c 20 6e 4d 69 63 72 6f 29 3b 0a 7d 0a Vfs, nMicro);.}.
4b60: 73 74 61 74 69 63 20 69 6e 74 20 63 66 43 75 72 static int cfCur
4b70: 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 rentTime(sqlite3
4b80: 5f 76 66 73 20 2a 70 43 66 56 66 73 2c 20 64 6f _vfs *pCfVfs, do
4b90: 75 62 6c 65 20 2a 70 54 69 6d 65 4f 75 74 29 7b uble *pTimeOut){
4ba0: 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
4bb0: 70 56 66 73 20 3d 20 28 73 71 6c 69 74 65 33 5f pVfs = (sqlite3_
4bc0: 76 66 73 20 2a 29 70 43 66 56 66 73 2d 3e 70 41 vfs *)pCfVfs->pA
4bd0: 70 70 44 61 74 61 3b 0a 20 20 72 65 74 75 72 6e ppData;. return
4be0: 20 70 56 66 73 2d 3e 78 43 75 72 72 65 6e 74 54 pVfs->xCurrentT
4bf0: 69 6d 65 28 70 56 66 73 2c 20 70 54 69 6d 65 4f ime(pVfs, pTimeO
4c00: 75 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 ut);.}..static i
4c10: 6e 74 20 70 72 6f 63 65 73 73 44 65 76 53 79 6d nt processDevSym
4c20: 41 72 67 73 28 0a 20 20 54 63 6c 5f 49 6e 74 65 Args(. Tcl_Inte
4c30: 72 70 20 2a 69 6e 74 65 72 70 2c 0a 20 20 69 6e rp *interp,. in
4c40: 74 20 6f 62 6a 63 2c 0a 20 20 54 63 6c 5f 4f 62 t objc,. Tcl_Ob
4c50: 6a 20 2a 43 4f 4e 53 54 20 6f 62 6a 76 5b 5d 2c j *CONST objv[],
4c60: 0a 20 20 69 6e 74 20 2a 70 69 44 65 76 69 63 65 . int *piDevice
4c70: 43 68 61 72 2c 0a 20 20 69 6e 74 20 2a 70 69 53 Char,. int *piS
4c80: 65 63 74 6f 72 53 69 7a 65 0a 29 7b 0a 20 20 73 ectorSize.){. s
4c90: 74 72 75 63 74 20 44 65 76 69 63 65 46 6c 61 67 truct DeviceFlag
4ca0: 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 {. char *zNa
4cb0: 6d 65 3b 0a 20 20 20 20 69 6e 74 20 69 56 61 6c me;. int iVal
4cc0: 75 65 3b 0a 20 20 7d 20 61 46 6c 61 67 5b 5d 20 ue;. } aFlag[]
4cd0: 3d 20 7b 0a 20 20 20 20 7b 20 22 61 74 6f 6d 69 = {. { "atomi
4ce0: 63 22 2c 20 20 20 20 20 20 53 51 4c 49 54 45 5f c", SQLITE_
4cf0: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20 20 20 20 IOCAP_ATOMIC
4d00: 20 20 7d 2c 0a 20 20 20 20 7b 20 22 61 74 6f 6d },. { "atom
4d10: 69 63 35 31 32 22 2c 20 20 20 53 51 4c 49 54 45 ic512", SQLITE
4d20: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 _IOCAP_ATOMIC512
4d30: 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 61 74 6f },. { "ato
4d40: 6d 69 63 31 6b 22 2c 20 20 20 20 53 51 4c 49 54 mic1k", SQLIT
4d50: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 4b E_IOCAP_ATOMIC1K
4d60: 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 61 74 },. { "at
4d70: 6f 6d 69 63 32 6b 22 2c 20 20 20 20 53 51 4c 49 omic2k", SQLI
4d80: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 32 TE_IOCAP_ATOMIC2
4d90: 4b 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 61 K },. { "a
4da0: 74 6f 6d 69 63 34 6b 22 2c 20 20 20 20 53 51 4c tomic4k", SQL
4db0: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
4dc0: 34 4b 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 4K },. { "
4dd0: 61 74 6f 6d 69 63 38 6b 22 2c 20 20 20 20 53 51 atomic8k", SQ
4de0: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
4df0: 43 38 4b 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 C8K },. {
4e00: 22 61 74 6f 6d 69 63 31 36 6b 22 2c 20 20 20 53 "atomic16k", S
4e10: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
4e20: 49 43 31 36 4b 20 20 20 7d 2c 0a 20 20 20 20 7b IC16K },. {
4e30: 20 22 61 74 6f 6d 69 63 33 32 6b 22 2c 20 20 20 "atomic32k",
4e40: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
4e50: 4d 49 43 33 32 4b 20 20 20 7d 2c 0a 20 20 20 20 MIC32K },.
4e60: 7b 20 22 61 74 6f 6d 69 63 36 34 6b 22 2c 20 20 { "atomic64k",
4e70: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 SQLITE_IOCAP_AT
4e80: 4f 4d 49 43 36 34 4b 20 20 20 7d 2c 0a 20 20 20 OMIC64K },.
4e90: 20 7b 20 22 73 65 71 75 65 6e 74 69 61 6c 22 2c { "sequential",
4ea0: 20 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 SQLITE_IOCAP_S
4eb0: 45 51 55 45 4e 54 49 41 4c 20 20 7d 2c 0a 20 20 EQUENTIAL },.
4ec0: 20 20 7b 20 22 73 61 66 65 5f 61 70 70 65 6e 64 { "safe_append
4ed0: 22 2c 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f ", SQLITE_IOCAP_
4ee0: 53 41 46 45 5f 41 50 50 45 4e 44 20 7d 2c 0a 20 SAFE_APPEND },.
4ef0: 20 20 20 7b 20 30 2c 20 30 20 7d 0a 20 20 7d 3b { 0, 0 }. };
4f00: 0a 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 .. int i;. int
4f10: 20 69 44 63 20 3d 20 30 3b 0a 20 20 69 6e 74 20 iDc = 0;. int
4f20: 69 53 65 63 74 6f 72 53 69 7a 65 20 3d 20 30 3b iSectorSize = 0;
4f30: 0a 20 20 69 6e 74 20 73 65 74 53 65 63 74 6f 72 . int setSector
4f40: 73 69 7a 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 size = 0;. int
4f50: 73 65 74 44 65 76 69 63 65 43 68 61 72 20 3d 20 setDeviceChar =
4f60: 30 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 0;.. for(i=0; i
4f70: 3c 6f 62 6a 63 3b 20 69 2b 3d 32 29 7b 0a 20 20 <objc; i+=2){.
4f80: 20 20 69 6e 74 20 6e 4f 70 74 3b 0a 20 20 20 20 int nOpt;.
4f90: 63 68 61 72 20 2a 7a 4f 70 74 20 3d 20 54 63 6c char *zOpt = Tcl
4fa0: 5f 47 65 74 53 74 72 69 6e 67 46 72 6f 6d 4f 62 _GetStringFromOb
4fb0: 6a 28 6f 62 6a 76 5b 69 5d 2c 20 26 6e 4f 70 74 j(objv[i], &nOpt
4fc0: 29 3b 0a 0a 20 20 20 20 69 66 28 20 28 6e 4f 70 );.. if( (nOp
4fd0: 74 3e 31 31 20 7c 7c 20 6e 4f 70 74 3c 32 20 7c t>11 || nOpt<2 |
4fe0: 7c 20 73 74 72 6e 63 6d 70 28 22 2d 73 65 63 74 | strncmp("-sect
4ff0: 6f 72 73 69 7a 65 22 2c 20 7a 4f 70 74 2c 20 6e orsize", zOpt, n
5000: 4f 70 74 29 29 20 0a 20 20 20 20 20 26 26 20 28 Opt)) . && (
5010: 6e 4f 70 74 3e 31 36 20 7c 7c 20 6e 4f 70 74 3c nOpt>16 || nOpt<
5020: 32 20 7c 7c 20 73 74 72 6e 63 6d 70 28 22 2d 63 2 || strncmp("-c
5030: 68 61 72 61 63 74 65 72 69 73 74 69 63 73 22 2c haracteristics",
5040: 20 7a 4f 70 74 2c 20 6e 4f 70 74 29 29 0a 20 20 zOpt, nOpt)).
5050: 20 20 29 7b 0a 20 20 20 20 20 20 54 63 6c 5f 41 ){. Tcl_A
5060: 70 70 65 6e 64 52 65 73 75 6c 74 28 69 6e 74 65 ppendResult(inte
5070: 72 70 2c 20 0a 20 20 20 20 20 20 20 20 22 42 61 rp, . "Ba
5080: 64 20 6f 70 74 69 6f 6e 3a 20 5c 22 22 2c 20 7a d option: \"", z
5090: 4f 70 74 2c 20 0a 20 20 20 20 20 20 20 20 22 5c Opt, . "\
50a0: 22 20 2d 20 6d 75 73 74 20 62 65 20 5c 22 2d 63 " - must be \"-c
50b0: 68 61 72 61 63 74 65 72 69 73 74 69 63 73 5c 22 haracteristics\"
50c0: 20 6f 72 20 5c 22 2d 73 65 63 74 6f 72 73 69 7a or \"-sectorsiz
50d0: 65 5c 22 22 2c 20 30 0a 20 20 20 20 20 20 29 3b e\"", 0. );
50e0: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 54 43 . return TC
50f0: 4c 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 L_ERROR;. }.
5100: 20 20 20 69 66 28 20 69 3d 3d 6f 62 6a 63 2d 31 if( i==objc-1
5110: 20 29 7b 0a 20 20 20 20 20 20 54 63 6c 5f 41 70 ){. Tcl_Ap
5120: 70 65 6e 64 52 65 73 75 6c 74 28 69 6e 74 65 72 pendResult(inter
5130: 70 2c 20 22 4f 70 74 69 6f 6e 20 72 65 71 75 69 p, "Option requi
5140: 72 65 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 3a res an argument:
5150: 20 5c 22 22 2c 20 7a 4f 70 74 2c 20 22 5c 22 22 \"", zOpt, "\""
5160: 2c 30 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 ,0);. retur
5170: 6e 20 54 43 4c 5f 45 52 52 4f 52 3b 0a 20 20 20 n TCL_ERROR;.
5180: 20 7d 0a 0a 20 20 20 20 69 66 28 20 7a 4f 70 74 }.. if( zOpt
5190: 5b 31 5d 3d 3d 27 73 27 20 29 7b 0a 20 20 20 20 [1]=='s' ){.
51a0: 20 20 69 66 28 20 54 63 6c 5f 47 65 74 49 6e 74 if( Tcl_GetInt
51b0: 46 72 6f 6d 4f 62 6a 28 69 6e 74 65 72 70 2c 20 FromObj(interp,
51c0: 6f 62 6a 76 5b 69 2b 31 5d 2c 20 26 69 53 65 63 objv[i+1], &iSec
51d0: 74 6f 72 53 69 7a 65 29 20 29 7b 0a 20 20 20 20 torSize) ){.
51e0: 20 20 20 20 72 65 74 75 72 6e 20 54 43 4c 5f 45 return TCL_E
51f0: 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 RROR;. }.
5200: 20 20 20 20 73 65 74 53 65 63 74 6f 72 73 69 7a setSectorsiz
5210: 65 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 e = 1;. }else
5220: 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 {. int j;.
5230: 20 20 20 20 20 54 63 6c 5f 4f 62 6a 20 2a 2a 61 Tcl_Obj **a
5240: 70 4f 62 6a 3b 0a 20 20 20 20 20 20 69 6e 74 20 pObj;. int
5250: 6e 4f 62 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 nObj;. if(
5260: 54 63 6c 5f 4c 69 73 74 4f 62 6a 47 65 74 45 6c Tcl_ListObjGetEl
5270: 65 6d 65 6e 74 73 28 69 6e 74 65 72 70 2c 20 6f ements(interp, o
5280: 62 6a 76 5b 69 2b 31 5d 2c 20 26 6e 4f 62 6a 2c bjv[i+1], &nObj,
5290: 20 26 61 70 4f 62 6a 29 20 29 7b 0a 20 20 20 20 &apObj) ){.
52a0: 20 20 20 20 72 65 74 75 72 6e 20 54 43 4c 5f 45 return TCL_E
52b0: 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 RROR;. }.
52c0: 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e for(j=0; j<n
52d0: 4f 62 6a 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 Obj; j++){.
52e0: 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 int rc;.
52f0: 20 20 20 69 6e 74 20 69 43 68 6f 69 63 65 3b 0a int iChoice;.
5300: 20 20 20 20 20 20 20 20 54 63 6c 5f 4f 62 6a 20 Tcl_Obj
5310: 2a 70 46 6c 61 67 20 3d 20 54 63 6c 5f 44 75 70 *pFlag = Tcl_Dup
5320: 6c 69 63 61 74 65 4f 62 6a 28 61 70 4f 62 6a 5b licateObj(apObj[
5330: 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 54 63 6c j]);. Tcl
5340: 5f 49 6e 63 72 52 65 66 43 6f 75 6e 74 28 70 46 _IncrRefCount(pF
5350: 6c 61 67 29 3b 0a 20 20 20 20 20 20 20 20 54 63 lag);. Tc
5360: 6c 5f 55 74 66 54 6f 4c 6f 77 65 72 28 54 63 6c l_UtfToLower(Tcl
5370: 5f 47 65 74 53 74 72 69 6e 67 28 70 46 6c 61 67 _GetString(pFlag
5380: 29 29 3b 0a 20 0a 20 20 20 20 20 20 20 20 72 63 ));. . rc
5390: 20 3d 20 54 63 6c 5f 47 65 74 49 6e 64 65 78 46 = Tcl_GetIndexF
53a0: 72 6f 6d 4f 62 6a 53 74 72 75 63 74 28 0a 20 20 romObjStruct(.
53b0: 20 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 70 interp
53c0: 2c 20 70 46 6c 61 67 2c 20 61 46 6c 61 67 2c 20 , pFlag, aFlag,
53d0: 73 69 7a 65 6f 66 28 61 46 6c 61 67 5b 30 5d 29 sizeof(aFlag[0])
53e0: 2c 20 22 6e 6f 20 73 75 63 68 20 66 6c 61 67 22 , "no such flag"
53f0: 2c 20 30 2c 20 26 69 43 68 6f 69 63 65 0a 20 20 , 0, &iChoice.
5400: 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 );.
5410: 20 54 63 6c 5f 44 65 63 72 52 65 66 43 6f 75 6e Tcl_DecrRefCoun
5420: 74 28 70 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 t(pFlag);.
5430: 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
5440: 20 20 20 20 20 20 72 65 74 75 72 6e 20 54 43 4c return TCL
5450: 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 _ERROR;.
5460: 7d 0a 0a 20 20 20 20 20 20 20 20 69 44 63 20 7c }.. iDc |
5470: 3d 20 61 46 6c 61 67 5b 69 43 68 6f 69 63 65 5d = aFlag[iChoice]
5480: 2e 69 56 61 6c 75 65 3b 0a 20 20 20 20 20 20 7d .iValue;. }
5490: 0a 20 20 20 20 20 20 73 65 74 44 65 76 69 63 65 . setDevice
54a0: 43 68 61 72 20 3d 20 31 3b 0a 20 20 20 20 7d 0a Char = 1;. }.
54b0: 20 20 7d 0a 0a 20 20 69 66 28 20 73 65 74 44 65 }.. if( setDe
54c0: 76 69 63 65 43 68 61 72 20 29 7b 0a 20 20 20 20 viceChar ){.
54d0: 2a 70 69 44 65 76 69 63 65 43 68 61 72 20 3d 20 *piDeviceChar =
54e0: 69 44 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 iDc;. }. if( s
54f0: 65 74 53 65 63 74 6f 72 73 69 7a 65 20 29 7b 0a etSectorsize ){.
5500: 20 20 20 20 2a 70 69 53 65 63 74 6f 72 53 69 7a *piSectorSiz
5510: 65 20 3d 20 69 53 65 63 74 6f 72 53 69 7a 65 3b e = iSectorSize;
5520: 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 54 . }.. return T
5530: 43 4c 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 CL_OK;.}../*.**
5540: 74 63 6c 63 6d 64 3a 20 20 20 73 71 6c 69 74 65 tclcmd: sqlite
5550: 5f 63 72 61 73 68 5f 65 6e 61 62 6c 65 20 45 4e _crash_enable EN
5560: 41 42 4c 45 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d ABLE.**.** Param
5570: 65 74 65 72 20 45 4e 41 42 4c 45 20 6d 75 73 74 eter ENABLE must
5580: 20 62 65 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 be a boolean va
5590: 6c 75 65 2e 20 49 66 20 74 72 75 65 2c 20 74 68 lue. If true, th
55a0: 65 6e 20 74 68 65 20 22 63 72 61 73 68 22 0a 2a en the "crash".*
55b0: 2a 20 76 66 73 20 69 73 20 61 64 64 65 64 20 74 * vfs is added t
55c0: 6f 20 74 68 65 20 73 79 73 74 65 6d 2e 20 49 66 o the system. If
55d0: 20 66 61 6c 73 65 2c 20 69 74 20 69 73 20 72 65 false, it is re
55e0: 6d 6f 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 moved..*/.static
55f0: 20 69 6e 74 20 63 72 61 73 68 45 6e 61 62 6c 65 int crashEnable
5600: 43 6d 64 28 0a 20 20 76 6f 69 64 20 2a 20 63 6c Cmd(. void * cl
5610: 69 65 6e 74 44 61 74 61 2c 0a 20 20 54 63 6c 5f ientData,. Tcl_
5620: 49 6e 74 65 72 70 20 2a 69 6e 74 65 72 70 2c 0a Interp *interp,.
5630: 20 20 69 6e 74 20 6f 62 6a 63 2c 0a 20 20 54 63 int objc,. Tc
5640: 6c 5f 4f 62 6a 20 2a 43 4f 4e 53 54 20 6f 62 6a l_Obj *CONST obj
5650: 76 5b 5d 0a 29 7b 0a 20 20 69 6e 74 20 69 73 45 v[].){. int isE
5660: 6e 61 62 6c 65 3b 0a 20 20 73 74 61 74 69 63 20 nable;. static
5670: 73 71 6c 69 74 65 33 5f 76 66 73 20 63 72 61 73 sqlite3_vfs cras
5680: 68 56 66 73 20 3d 20 7b 0a 20 20 20 20 31 2c 20 hVfs = {. 1,
5690: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
56a0: 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a /* iVersion */.
56b0: 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 0,
56c0: 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 4f 73 46 /* szOsF
56d0: 69 6c 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 ile */. 0,
56e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
56f0: 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f 0a * mxPathname */.
5700: 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 0,
5710: 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 /* pNext
5720: 20 2a 2f 0a 20 20 20 20 22 63 72 61 73 68 22 2c */. "crash",
5730: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a /* z
5740: 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 Name */. 0,
5750: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5760: 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 20 /* pAppData */.
5770: 20 0a 20 20 20 20 63 66 4f 70 65 6e 2c 20 20 20 . cfOpen,
5780: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
5790: 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 63 66 44 65 Open */. cfDe
57a0: 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 lete,
57b0: 20 20 2f 2a 20 78 44 65 6c 65 74 65 20 2a 2f 0a /* xDelete */.
57c0: 20 20 20 20 63 66 41 63 63 65 73 73 2c 20 20 20 cfAccess,
57d0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 41 63 /* xAc
57e0: 63 65 73 73 20 2a 2f 0a 20 20 20 20 63 66 46 75 cess */. cfFu
57f0: 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20 20 20 llPathname,
5800: 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e 61 /* xFullPathna
5810: 6d 65 20 2a 2f 0a 20 20 20 20 63 66 44 6c 4f 70 me */. cfDlOp
5820: 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 en,
5830: 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 /* xDlOpen */.
5840: 20 20 63 66 44 6c 45 72 72 6f 72 2c 20 20 20 20 cfDlError,
5850: 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 /* xDlEr
5860: 72 6f 72 20 2a 2f 0a 20 20 20 20 63 66 44 6c 53 ror */. cfDlS
5870: 79 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ym,
5880: 20 2f 2a 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 /* xDlSym */.
5890: 20 20 63 66 44 6c 43 6c 6f 73 65 2c 20 20 20 20 cfDlClose,
58a0: 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c /* xDlCl
58b0: 6f 73 65 20 2a 2f 0a 20 20 20 20 63 66 52 61 6e ose */. cfRan
58c0: 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 20 20 20 domness,
58d0: 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 /* xRandomness
58e0: 2a 2f 0a 20 20 20 20 63 66 53 6c 65 65 70 2c 20 */. cfSleep,
58f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5900: 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 63 66 xSleep */. cf
5910: 43 75 72 72 65 6e 74 54 69 6d 65 20 20 20 20 20 CurrentTime
5920: 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 /* xCurrentT
5930: 69 6d 65 20 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 69 ime */. };.. i
5940: 66 28 20 6f 62 6a 63 21 3d 32 20 29 7b 0a 20 20 f( objc!=2 ){.
5950: 20 20 54 63 6c 5f 57 72 6f 6e 67 4e 75 6d 41 72 Tcl_WrongNumAr
5960: 67 73 28 69 6e 74 65 72 70 2c 20 31 2c 20 6f 62 gs(interp, 1, ob
5970: 6a 76 2c 20 22 45 4e 41 42 4c 45 22 29 3b 0a 20 jv, "ENABLE");.
5980: 20 20 20 72 65 74 75 72 6e 20 54 43 4c 5f 45 52 return TCL_ER
5990: 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 ROR;. }.. if(
59a0: 54 63 6c 5f 47 65 74 42 6f 6f 6c 65 61 6e 46 72 Tcl_GetBooleanFr
59b0: 6f 6d 4f 62 6a 28 69 6e 74 65 72 70 2c 20 6f 62 omObj(interp, ob
59c0: 6a 76 5b 31 5d 2c 20 26 69 73 45 6e 61 62 6c 65 jv[1], &isEnable
59d0: 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
59e0: 54 43 4c 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a TCL_ERROR;. }..
59f0: 20 20 69 66 28 20 28 69 73 45 6e 61 62 6c 65 20 if( (isEnable
5a00: 26 26 20 63 72 61 73 68 56 66 73 2e 70 41 70 70 && crashVfs.pApp
5a10: 44 61 74 61 29 20 7c 7c 20 28 21 69 73 45 6e 61 Data) || (!isEna
5a20: 62 6c 65 20 26 26 20 21 63 72 61 73 68 56 66 73 ble && !crashVfs
5a30: 2e 70 41 70 70 44 61 74 61 29 20 29 7b 0a 20 20 .pAppData) ){.
5a40: 20 20 72 65 74 75 72 6e 20 54 43 4c 5f 4f 4b 3b return TCL_OK;
5a50: 0a 20 20 7d 0a 0a 20 20 69 66 28 20 63 72 61 73 . }.. if( cras
5a60: 68 56 66 73 2e 70 41 70 70 44 61 74 61 3d 3d 30 hVfs.pAppData==0
5a70: 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
5a80: 76 66 73 20 2a 70 4f 72 69 67 69 6e 61 6c 56 66 vfs *pOriginalVf
5a90: 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 66 73 5f s = sqlite3_vfs_
5aa0: 66 69 6e 64 28 30 29 3b 0a 20 20 20 20 63 72 61 find(0);. cra
5ab0: 73 68 56 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 shVfs.mxPathname
5ac0: 20 3d 20 70 4f 72 69 67 69 6e 61 6c 56 66 73 2d = pOriginalVfs-
5ad0: 3e 6d 78 50 61 74 68 6e 61 6d 65 3b 0a 20 20 20 >mxPathname;.
5ae0: 20 63 72 61 73 68 56 66 73 2e 70 41 70 70 44 61 crashVfs.pAppDa
5af0: 74 61 20 3d 20 28 76 6f 69 64 20 2a 29 70 4f 72 ta = (void *)pOr
5b00: 69 67 69 6e 61 6c 56 66 73 3b 0a 20 20 20 20 63 iginalVfs;. c
5b10: 72 61 73 68 56 66 73 2e 73 7a 4f 73 46 69 6c 65 rashVfs.szOsFile
5b20: 20 3d 20 73 69 7a 65 6f 66 28 43 72 61 73 68 46 = sizeof(CrashF
5b30: 69 6c 65 29 20 2b 20 70 4f 72 69 67 69 6e 61 6c ile) + pOriginal
5b40: 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 3b 0a 20 Vfs->szOsFile;.
5b50: 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 sqlite3_vfs_r
5b60: 65 67 69 73 74 65 72 28 26 63 72 61 73 68 56 66 egister(&crashVf
5b70: 73 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a s, 0);. }else{.
5b80: 20 20 20 20 63 72 61 73 68 56 66 73 2e 70 41 70 crashVfs.pAp
5b90: 70 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 73 pData = 0;. s
5ba0: 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 qlite3_vfs_unreg
5bb0: 69 73 74 65 72 28 26 63 72 61 73 68 56 66 73 29 ister(&crashVfs)
5bc0: 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
5bd0: 54 43 4c 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a TCL_OK;.}../*.**
5be0: 20 74 63 6c 63 6d 64 3a 20 20 20 73 71 6c 69 74 tclcmd: sqlit
5bf0: 65 5f 63 72 61 73 68 70 61 72 61 6d 73 20 3f 4f e_crashparams ?O
5c00: 50 54 49 4f 4e 53 3f 20 44 45 4c 41 59 20 43 52 PTIONS? DELAY CR
5c10: 41 53 48 46 49 4c 45 0a 2a 2a 0a 2a 2a 20 54 68 ASHFILE.**.** Th
5c20: 69 73 20 70 72 6f 63 65 64 75 72 65 20 69 6d 70 is procedure imp
5c30: 6c 65 6d 65 6e 74 73 20 61 20 54 43 4c 20 63 6f lements a TCL co
5c40: 6d 6d 61 6e 64 20 74 68 61 74 20 65 6e 61 62 6c mmand that enabl
5c50: 65 73 20 63 72 61 73 68 20 74 65 73 74 69 6e 67 es crash testing
5c60: 0a 2a 2a 20 69 6e 20 74 65 73 74 66 69 78 74 75 .** in testfixtu
5c70: 72 65 2e 20 20 4f 6e 63 65 20 65 6e 61 62 6c 65 re. Once enable
5c80: 64 2c 20 63 72 61 73 68 20 74 65 73 74 69 6e 67 d, crash testing
5c90: 20 63 61 6e 6e 6f 74 20 62 65 20 64 69 73 61 62 cannot be disab
5ca0: 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 76 61 69 6c led..**.** Avail
5cb0: 61 62 6c 65 20 6f 70 74 69 6f 6e 73 20 61 72 65 able options are
5cc0: 20 22 2d 63 68 61 72 61 63 74 65 72 69 73 74 69 "-characteristi
5cd0: 63 73 22 20 61 6e 64 20 22 2d 73 65 63 74 6f 72 cs" and "-sector
5ce0: 73 69 7a 65 22 2e 20 42 6f 74 68 20 72 65 71 75 size". Both requ
5cf0: 69 72 65 0a 2a 2a 20 61 6e 20 61 72 67 75 6d 65 ire.** an argume
5d00: 6e 74 2e 20 46 6f 72 20 2d 73 65 63 74 6f 72 73 nt. For -sectors
5d10: 69 7a 65 2c 20 74 68 69 73 20 69 73 20 74 68 65 ize, this is the
5d20: 20 73 69 6d 75 6c 61 74 65 64 20 73 65 63 74 6f simulated secto
5d30: 72 20 73 69 7a 65 20 69 6e 0a 2a 2a 20 62 79 74 r size in.** byt
5d40: 65 73 2e 20 46 6f 72 20 2d 63 68 61 72 61 63 74 es. For -charact
5d50: 65 72 69 73 74 69 63 73 2c 20 74 68 65 20 61 72 eristics, the ar
5d60: 67 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 61 gument must be a
5d70: 20 6c 69 73 74 20 6f 66 20 69 6f 2d 63 61 70 61 list of io-capa
5d80: 62 69 6c 69 74 79 0a 2a 2a 20 66 6c 61 67 73 20 bility.** flags
5d90: 74 6f 20 73 69 6d 75 6c 61 74 65 2e 20 56 61 6c to simulate. Val
5da0: 69 64 20 66 6c 61 67 73 20 61 72 65 20 22 61 74 id flags are "at
5db0: 6f 6d 69 63 22 2c 20 22 61 74 6f 6d 69 63 35 31 omic", "atomic51
5dc0: 32 22 2c 20 22 61 74 6f 6d 69 63 31 4b 22 2c 0a 2", "atomic1K",.
5dd0: 2a 2a 20 22 61 74 6f 6d 69 63 32 4b 22 2c 20 22 ** "atomic2K", "
5de0: 61 74 6f 6d 69 63 34 4b 22 2c 20 22 61 74 6f 6d atomic4K", "atom
5df0: 69 63 38 4b 22 2c 20 22 61 74 6f 6d 69 63 31 36 ic8K", "atomic16
5e00: 4b 22 2c 20 22 61 74 6f 6d 69 63 33 32 4b 22 2c K", "atomic32K",
5e10: 20 0a 2a 2a 20 22 61 74 6f 6d 69 63 36 34 4b 22 .** "atomic64K"
5e20: 2c 20 22 73 65 71 75 65 6e 74 69 61 6c 22 20 61 , "sequential" a
5e30: 6e 64 20 22 73 61 66 65 5f 61 70 70 65 6e 64 22 nd "safe_append"
5e40: 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a ..**.** Example:
5e50: 0a 2a 2a 0a 2a 2a 20 20 20 73 71 6c 69 74 65 5f .**.** sqlite_
5e60: 63 72 61 73 68 70 61 72 61 6d 73 20 2d 73 65 63 crashparams -sec
5e70: 74 20 31 30 32 34 20 2d 63 68 61 72 20 7b 61 74 t 1024 -char {at
5e80: 6f 6d 69 63 20 73 65 71 75 65 6e 74 69 61 6c 7d omic sequential}
5e90: 20 2e 2f 74 65 73 74 2e 64 62 20 31 0a 2a 2a 0a ./test.db 1.**.
5ea0: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72 */.static int cr
5eb0: 61 73 68 50 61 72 61 6d 73 4f 62 6a 43 6d 64 28 ashParamsObjCmd(
5ec0: 0a 20 20 76 6f 69 64 20 2a 20 63 6c 69 65 6e 74 . void * client
5ed0: 44 61 74 61 2c 0a 20 20 54 63 6c 5f 49 6e 74 65 Data,. Tcl_Inte
5ee0: 72 70 20 2a 69 6e 74 65 72 70 2c 0a 20 20 69 6e rp *interp,. in
5ef0: 74 20 6f 62 6a 63 2c 0a 20 20 54 63 6c 5f 4f 62 t objc,. Tcl_Ob
5f00: 6a 20 2a 43 4f 4e 53 54 20 6f 62 6a 76 5b 5d 0a j *CONST objv[].
5f10: 29 7b 0a 20 20 69 6e 74 20 69 44 65 6c 61 79 3b ){. int iDelay;
5f20: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
5f30: 43 72 61 73 68 46 69 6c 65 3b 0a 20 20 69 6e 74 CrashFile;. int
5f40: 20 6e 43 72 61 73 68 46 69 6c 65 2c 20 69 44 63 nCrashFile, iDc
5f50: 2c 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 0a 0a , iSectorSize;..
5f60: 20 20 69 44 63 20 3d 20 2d 31 3b 0a 20 20 69 53 iDc = -1;. iS
5f70: 65 63 74 6f 72 53 69 7a 65 20 3d 20 2d 31 3b 0a ectorSize = -1;.
5f80: 0a 20 20 69 66 28 20 6f 62 6a 63 3c 33 20 29 7b . if( objc<3 ){
5f90: 0a 20 20 20 20 54 63 6c 5f 57 72 6f 6e 67 4e 75 . Tcl_WrongNu
5fa0: 6d 41 72 67 73 28 69 6e 74 65 72 70 2c 20 31 2c mArgs(interp, 1,
5fb0: 20 6f 62 6a 76 2c 20 22 3f 4f 50 54 49 4f 4e 53 objv, "?OPTIONS
5fc0: 3f 20 44 45 4c 41 59 20 43 52 41 53 48 46 49 4c ? DELAY CRASHFIL
5fd0: 45 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72 E");. goto er
5fe0: 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 7a 43 72 61 ror;. }.. zCra
5ff0: 73 68 46 69 6c 65 20 3d 20 54 63 6c 5f 47 65 74 shFile = Tcl_Get
6000: 53 74 72 69 6e 67 46 72 6f 6d 4f 62 6a 28 6f 62 StringFromObj(ob
6010: 6a 76 5b 6f 62 6a 63 2d 31 5d 2c 20 26 6e 43 72 jv[objc-1], &nCr
6020: 61 73 68 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 ashFile);. if(
6030: 6e 43 72 61 73 68 46 69 6c 65 3e 3d 73 69 7a 65 nCrashFile>=size
6040: 6f 66 28 67 2e 7a 43 72 61 73 68 46 69 6c 65 29 of(g.zCrashFile)
6050: 20 29 7b 0a 20 20 20 20 54 63 6c 5f 41 70 70 65 ){. Tcl_Appe
6060: 6e 64 52 65 73 75 6c 74 28 69 6e 74 65 72 70 2c ndResult(interp,
6070: 20 22 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 6f "Filename is to
6080: 6f 20 6c 6f 6e 67 3a 20 5c 22 22 2c 20 7a 43 72 o long: \"", zCr
6090: 61 73 68 46 69 6c 65 2c 20 22 5c 22 22 2c 20 30 ashFile, "\"", 0
60a0: 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 6f );. goto erro
60b0: 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 54 63 6c r;. }. if( Tcl
60c0: 5f 47 65 74 49 6e 74 46 72 6f 6d 4f 62 6a 28 69 _GetIntFromObj(i
60d0: 6e 74 65 72 70 2c 20 6f 62 6a 76 5b 6f 62 6a 63 nterp, objv[objc
60e0: 2d 32 5d 2c 20 26 69 44 65 6c 61 79 29 20 29 7b -2], &iDelay) ){
60f0: 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 3b . goto error;
6100: 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 72 6f 63 . }.. if( proc
6110: 65 73 73 44 65 76 53 79 6d 41 72 67 73 28 69 6e essDevSymArgs(in
6120: 74 65 72 70 2c 20 6f 62 6a 63 2d 33 2c 20 26 6f terp, objc-3, &o
6130: 62 6a 76 5b 31 5d 2c 20 26 69 44 63 2c 20 26 69 bjv[1], &iDc, &i
6140: 53 65 63 74 6f 72 53 69 7a 65 29 20 29 7b 0a 20 SectorSize) ){.
6150: 20 20 20 72 65 74 75 72 6e 20 54 43 4c 5f 45 52 return TCL_ER
6160: 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 ROR;. }.. if(
6170: 69 44 63 3e 3d 30 20 29 7b 0a 20 20 20 20 67 2e iDc>=0 ){. g.
6180: 69 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 iDeviceCharacter
6190: 69 73 74 69 63 73 20 3d 20 69 44 63 3b 0a 20 20 istics = iDc;.
61a0: 7d 0a 20 20 69 66 28 20 69 53 65 63 74 6f 72 53 }. if( iSectorS
61b0: 69 7a 65 3e 3d 30 20 29 7b 0a 20 20 20 20 67 2e ize>=0 ){. g.
61c0: 69 53 65 63 74 6f 72 53 69 7a 65 20 3d 20 69 53 iSectorSize = iS
61d0: 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 7d 0a 0a ectorSize;. }..
61e0: 20 20 67 2e 69 43 72 61 73 68 20 3d 20 69 44 65 g.iCrash = iDe
61f0: 6c 61 79 3b 0a 20 20 6d 65 6d 63 70 79 28 67 2e lay;. memcpy(g.
6200: 7a 43 72 61 73 68 46 69 6c 65 2c 20 7a 43 72 61 zCrashFile, zCra
6210: 73 68 46 69 6c 65 2c 20 6e 43 72 61 73 68 46 69 shFile, nCrashFi
6220: 6c 65 2b 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 le+1);. sqlite3
6230: 43 72 61 73 68 54 65 73 74 45 6e 61 62 6c 65 20 CrashTestEnable
6240: 3d 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 54 43 = 1;. return TC
6250: 4c 5f 4f 4b 3b 0a 0a 65 72 72 6f 72 3a 0a 20 20 L_OK;..error:.
6260: 72 65 74 75 72 6e 20 54 43 4c 5f 45 52 52 4f 52 return TCL_ERROR
6270: 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 ;.}..static int
6280: 64 65 76 53 79 6d 4f 62 6a 43 6d 64 28 0a 20 20 devSymObjCmd(.
6290: 76 6f 69 64 20 2a 20 63 6c 69 65 6e 74 44 61 74 void * clientDat
62a0: 61 2c 0a 20 20 54 63 6c 5f 49 6e 74 65 72 70 20 a,. Tcl_Interp
62b0: 2a 69 6e 74 65 72 70 2c 0a 20 20 69 6e 74 20 6f *interp,. int o
62c0: 62 6a 63 2c 0a 20 20 54 63 6c 5f 4f 62 6a 20 2a bjc,. Tcl_Obj *
62d0: 43 4f 4e 53 54 20 6f 62 6a 76 5b 5d 0a 29 7b 0a CONST objv[].){.
62e0: 20 20 76 6f 69 64 20 64 65 76 73 79 6d 5f 72 65 void devsym_re
62f0: 67 69 73 74 65 72 28 69 6e 74 20 69 44 65 76 69 gister(int iDevi
6300: 63 65 43 68 61 72 2c 20 69 6e 74 20 69 53 65 63 ceChar, int iSec
6310: 74 6f 72 53 69 7a 65 29 3b 0a 0a 20 20 69 6e 74 torSize);.. int
6320: 20 69 44 63 20 3d 20 2d 31 3b 0a 20 20 69 6e 74 iDc = -1;. int
6330: 20 69 53 65 63 74 6f 72 53 69 7a 65 20 3d 20 2d iSectorSize = -
6340: 31 3b 0a 0a 20 20 69 66 28 20 70 72 6f 63 65 73 1;.. if( proces
6350: 73 44 65 76 53 79 6d 41 72 67 73 28 69 6e 74 65 sDevSymArgs(inte
6360: 72 70 2c 20 6f 62 6a 63 2d 31 2c 20 26 6f 62 6a rp, objc-1, &obj
6370: 76 5b 31 5d 2c 20 26 69 44 63 2c 20 26 69 53 65 v[1], &iDc, &iSe
6380: 63 74 6f 72 53 69 7a 65 29 20 29 7b 0a 20 20 20 ctorSize) ){.
6390: 20 72 65 74 75 72 6e 20 54 43 4c 5f 45 52 52 4f return TCL_ERRO
63a0: 52 3b 0a 20 20 7d 0a 20 20 64 65 76 73 79 6d 5f R;. }. devsym_
63b0: 72 65 67 69 73 74 65 72 28 69 44 63 2c 20 69 53 register(iDc, iS
63c0: 65 63 74 6f 72 53 69 7a 65 29 3b 0a 0a 20 20 72 ectorSize);.. r
63d0: 65 74 75 72 6e 20 54 43 4c 5f 4f 4b 3b 0a 7d 0a eturn TCL_OK;.}.
63e0: 0a 2f 2a 0a 2a 2a 20 74 63 6c 63 6d 64 3a 20 72 ./*.** tclcmd: r
63f0: 65 67 69 73 74 65 72 5f 6a 74 5f 76 66 73 20 3f egister_jt_vfs ?
6400: 2d 64 65 66 61 75 6c 74 3f 20 50 41 52 45 4e 54 -default? PARENT
6410: 2d 56 46 53 0a 2a 2f 0a 73 74 61 74 69 63 20 69 -VFS.*/.static i
6420: 6e 74 20 6a 74 4f 62 6a 43 6d 64 28 0a 20 20 76 nt jtObjCmd(. v
6430: 6f 69 64 20 2a 20 63 6c 69 65 6e 74 44 61 74 61 oid * clientData
6440: 2c 0a 20 20 54 63 6c 5f 49 6e 74 65 72 70 20 2a ,. Tcl_Interp *
6450: 69 6e 74 65 72 70 2c 0a 20 20 69 6e 74 20 6f 62 interp,. int ob
6460: 6a 63 2c 0a 20 20 54 63 6c 5f 4f 62 6a 20 2a 43 jc,. Tcl_Obj *C
6470: 4f 4e 53 54 20 6f 62 6a 76 5b 5d 0a 29 7b 0a 20 ONST objv[].){.
6480: 20 69 6e 74 20 6a 74 5f 72 65 67 69 73 74 65 72 int jt_register
6490: 28 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 20 (char *, int);.
64a0: 20 63 68 61 72 20 2a 7a 50 61 72 65 6e 74 20 3d char *zParent =
64b0: 20 30 3b 0a 0a 20 20 69 66 28 20 6f 62 6a 63 21 0;.. if( objc!
64c0: 3d 32 20 26 26 20 6f 62 6a 63 21 3d 33 20 29 7b =2 && objc!=3 ){
64d0: 0a 20 20 20 20 54 63 6c 5f 57 72 6f 6e 67 4e 75 . Tcl_WrongNu
64e0: 6d 41 72 67 73 28 69 6e 74 65 72 70 2c 20 31 2c mArgs(interp, 1,
64f0: 20 6f 62 6a 76 2c 20 22 3f 2d 64 65 66 61 75 6c objv, "?-defaul
6500: 74 3f 20 50 41 52 45 4e 54 2d 56 46 53 22 29 3b t? PARENT-VFS");
6510: 0a 20 20 20 20 72 65 74 75 72 6e 20 54 43 4c 5f . return TCL_
6520: 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 7a 50 61 ERROR;. }. zPa
6530: 72 65 6e 74 20 3d 20 54 63 6c 5f 47 65 74 53 74 rent = Tcl_GetSt
6540: 72 69 6e 67 28 6f 62 6a 76 5b 31 5d 29 3b 0a 20 ring(objv[1]);.
6550: 20 69 66 28 20 6f 62 6a 63 3d 3d 33 20 29 7b 0a if( objc==3 ){.
6560: 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a if( strcmp(z
6570: 50 61 72 65 6e 74 2c 20 22 2d 64 65 66 61 75 6c Parent, "-defaul
6580: 74 22 29 20 29 7b 0a 20 20 20 20 20 20 54 63 6c t") ){. Tcl
6590: 5f 41 70 70 65 6e 64 52 65 73 75 6c 74 28 69 6e _AppendResult(in
65a0: 74 65 72 70 2c 20 0a 20 20 20 20 20 20 20 20 20 terp, .
65b0: 20 22 62 61 64 20 6f 70 74 69 6f 6e 20 5c 22 22 "bad option \""
65c0: 2c 20 7a 50 61 72 65 6e 74 2c 20 22 5c 22 3a 20 , zParent, "\":
65d0: 6d 75 73 74 20 62 65 20 2d 64 65 66 61 75 6c 74 must be -default
65e0: 22 2c 20 30 0a 20 20 20 20 20 20 29 3b 0a 20 20 ", 0. );.
65f0: 20 20 20 20 72 65 74 75 72 6e 20 54 43 4c 5f 45 return TCL_E
6600: 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 RROR;. }.
6610: 7a 50 61 72 65 6e 74 20 3d 20 54 63 6c 5f 47 65 zParent = Tcl_Ge
6620: 74 53 74 72 69 6e 67 28 6f 62 6a 76 5b 32 5d 29 tString(objv[2])
6630: 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 28 2a ;. }.. if( !(*
6640: 7a 50 61 72 65 6e 74 29 20 29 7b 0a 20 20 20 20 zParent) ){.
6650: 7a 50 61 72 65 6e 74 20 3d 20 30 3b 0a 20 20 7d zParent = 0;. }
6660: 0a 20 20 69 66 28 20 6a 74 5f 72 65 67 69 73 74 . if( jt_regist
6670: 65 72 28 7a 50 61 72 65 6e 74 2c 20 6f 62 6a 63 er(zParent, objc
6680: 3d 3d 33 29 20 29 7b 0a 20 20 20 20 54 63 6c 5f ==3) ){. Tcl_
6690: 41 70 70 65 6e 64 52 65 73 75 6c 74 28 69 6e 74 AppendResult(int
66a0: 65 72 70 2c 20 22 45 72 72 6f 72 20 69 6e 20 6a erp, "Error in j
66b0: 74 5f 72 65 67 69 73 74 65 72 22 2c 20 30 29 3b t_register", 0);
66c0: 0a 20 20 20 20 72 65 74 75 72 6e 20 54 43 4c 5f . return TCL_
66d0: 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 72 65 ERROR;. }.. re
66e0: 74 75 72 6e 20 54 43 4c 5f 4f 4b 3b 0a 7d 0a 0a turn TCL_OK;.}..
66f0: 2f 2a 0a 2a 2a 20 74 63 6c 63 6d 64 3a 20 75 6e /*.** tclcmd: un
6700: 72 65 67 69 73 74 65 72 5f 6a 74 5f 76 66 73 0a register_jt_vfs.
6710: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 74 */.static int jt
6720: 55 6e 72 65 67 69 73 74 65 72 4f 62 6a 43 6d 64 UnregisterObjCmd
6730: 28 0a 20 20 76 6f 69 64 20 2a 20 63 6c 69 65 6e (. void * clien
6740: 74 44 61 74 61 2c 0a 20 20 54 63 6c 5f 49 6e 74 tData,. Tcl_Int
6750: 65 72 70 20 2a 69 6e 74 65 72 70 2c 0a 20 20 69 erp *interp,. i
6760: 6e 74 20 6f 62 6a 63 2c 0a 20 20 54 63 6c 5f 4f nt objc,. Tcl_O
6770: 62 6a 20 2a 43 4f 4e 53 54 20 6f 62 6a 76 5b 5d bj *CONST objv[]
6780: 0a 29 7b 0a 20 20 76 6f 69 64 20 6a 74 5f 75 6e .){. void jt_un
6790: 72 65 67 69 73 74 65 72 28 76 6f 69 64 29 3b 0a register(void);.
67a0: 0a 20 20 69 66 28 20 6f 62 6a 63 21 3d 31 20 29 . if( objc!=1 )
67b0: 7b 0a 20 20 20 20 54 63 6c 5f 57 72 6f 6e 67 4e {. Tcl_WrongN
67c0: 75 6d 41 72 67 73 28 69 6e 74 65 72 70 2c 20 31 umArgs(interp, 1
67d0: 2c 20 6f 62 6a 76 2c 20 22 22 29 3b 0a 20 20 20 , objv, "");.
67e0: 20 72 65 74 75 72 6e 20 54 43 4c 5f 45 52 52 4f return TCL_ERRO
67f0: 52 3b 0a 20 20 7d 0a 0a 20 20 6a 74 5f 75 6e 72 R;. }.. jt_unr
6800: 65 67 69 73 74 65 72 28 29 3b 0a 20 20 72 65 74 egister();. ret
6810: 75 72 6e 20 54 43 4c 5f 4f 4b 3b 0a 7d 0a 0a 23 urn TCL_OK;.}..#
6820: 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
6830: 4f 4d 49 54 5f 44 49 53 4b 49 4f 20 2a 2f 0a 0a OMIT_DISKIO */..
6840: 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65 /*.** This proce
6850: 64 75 72 65 20 72 65 67 69 73 74 65 72 73 20 74 dure registers t
6860: 68 65 20 54 43 4c 20 70 72 6f 63 65 64 75 72 65 he TCL procedure
6870: 73 20 64 65 66 69 6e 65 64 20 69 6e 20 74 68 69 s defined in thi
6880: 73 20 66 69 6c 65 2e 0a 2a 2f 0a 69 6e 74 20 53 s file..*/.int S
6890: 71 6c 69 74 65 74 65 73 74 36 5f 49 6e 69 74 28 qlitetest6_Init(
68a0: 54 63 6c 5f 49 6e 74 65 72 70 20 2a 69 6e 74 65 Tcl_Interp *inte
68b0: 72 70 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c rp){.#ifndef SQL
68c0: 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a ITE_OMIT_DISKIO.
68d0: 20 20 54 63 6c 5f 43 72 65 61 74 65 4f 62 6a 43 Tcl_CreateObjC
68e0: 6f 6d 6d 61 6e 64 28 69 6e 74 65 72 70 2c 20 22 ommand(interp, "
68f0: 73 71 6c 69 74 65 33 5f 63 72 61 73 68 5f 65 6e sqlite3_crash_en
6900: 61 62 6c 65 22 2c 20 63 72 61 73 68 45 6e 61 62 able", crashEnab
6910: 6c 65 43 6d 64 2c 20 30 2c 20 30 29 3b 0a 20 20 leCmd, 0, 0);.
6920: 54 63 6c 5f 43 72 65 61 74 65 4f 62 6a 43 6f 6d Tcl_CreateObjCom
6930: 6d 61 6e 64 28 69 6e 74 65 72 70 2c 20 22 73 71 mand(interp, "sq
6940: 6c 69 74 65 33 5f 63 72 61 73 68 70 61 72 61 6d lite3_crashparam
6950: 73 22 2c 20 63 72 61 73 68 50 61 72 61 6d 73 4f s", crashParamsO
6960: 62 6a 43 6d 64 2c 20 30 2c 20 30 29 3b 0a 20 20 bjCmd, 0, 0);.
6970: 54 63 6c 5f 43 72 65 61 74 65 4f 62 6a 43 6f 6d Tcl_CreateObjCom
6980: 6d 61 6e 64 28 69 6e 74 65 72 70 2c 20 22 73 71 mand(interp, "sq
6990: 6c 69 74 65 33 5f 73 69 6d 75 6c 61 74 65 5f 64 lite3_simulate_d
69a0: 65 76 69 63 65 22 2c 20 64 65 76 53 79 6d 4f 62 evice", devSymOb
69b0: 6a 43 6d 64 2c 20 30 2c 20 30 29 3b 0a 20 20 54 jCmd, 0, 0);. T
69c0: 63 6c 5f 43 72 65 61 74 65 4f 62 6a 43 6f 6d 6d cl_CreateObjComm
69d0: 61 6e 64 28 69 6e 74 65 72 70 2c 20 22 72 65 67 and(interp, "reg
69e0: 69 73 74 65 72 5f 6a 74 5f 76 66 73 22 2c 20 6a ister_jt_vfs", j
69f0: 74 4f 62 6a 43 6d 64 2c 20 30 2c 20 30 29 3b 0a tObjCmd, 0, 0);.
6a00: 20 20 54 63 6c 5f 43 72 65 61 74 65 4f 62 6a 43 Tcl_CreateObjC
6a10: 6f 6d 6d 61 6e 64 28 69 6e 74 65 72 70 2c 20 22 ommand(interp, "
6a20: 75 6e 72 65 67 69 73 74 65 72 5f 6a 74 5f 76 66 unregister_jt_vf
6a30: 73 22 2c 20 6a 74 55 6e 72 65 67 69 73 74 65 72 s", jtUnregister
6a40: 4f 62 6a 43 6d 64 2c 20 30 2c 20 30 29 3b 0a 23 ObjCmd, 0, 0);.#
6a50: 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 54 endif. return T
6a60: 43 4c 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 CL_OK;.}..#endif
6a70: 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 /* SQLITE_TEST
6a80: 2a 2f 0a */.