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 74 file contains t
0190: 68 65 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 he VFS implement
01a0: 61 74 69 6f 6e 20 66 6f 72 20 75 6e 69 78 2d 6c ation for unix-l
01b0: 69 6b 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 ike operating sy
01c0: 73 74 65 6d 73 0a 2a 2a 20 69 6e 63 6c 75 64 65 stems.** include
01d0: 20 4c 69 6e 75 78 2c 20 4d 61 63 4f 53 58 2c 20 Linux, MacOSX,
01e0: 2a 42 53 44 2c 20 51 4e 58 2c 20 56 78 57 6f 72 *BSD, QNX, VxWor
01f0: 6b 73 2c 20 41 49 58 2c 20 48 50 55 58 2c 20 61 ks, AIX, HPUX, a
0200: 6e 64 20 6f 74 68 65 72 73 2e 0a 2a 2a 0a 2a 2a nd others..**.**
0210: 20 54 68 65 72 65 20 61 72 65 20 61 63 74 75 61 There are actua
0220: 6c 6c 79 20 73 65 76 65 72 61 6c 20 64 69 66 66 lly several diff
0230: 65 72 65 6e 74 20 56 46 53 20 69 6d 70 6c 65 6d erent VFS implem
0240: 65 6e 74 61 74 69 6f 6e 73 20 69 6e 20 74 68 69 entations in thi
0250: 73 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 64 s file..** The d
0260: 69 66 66 65 72 65 6e 63 65 73 20 61 72 65 20 69 ifferences are i
0270: 6e 20 74 68 65 20 77 61 79 20 74 68 61 74 20 66 n the way that f
0280: 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 64 ile locking is d
0290: 6f 6e 65 2e 20 20 54 68 65 20 64 65 66 61 75 6c one. The defaul
02a0: 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 t.** implementat
02b0: 69 6f 6e 20 75 73 65 73 20 50 6f 73 69 78 20 41 ion uses Posix A
02c0: 64 76 69 73 6f 72 79 20 4c 6f 63 6b 73 2e 20 20 dvisory Locks.
02d0: 41 6c 74 65 72 6e 61 74 69 76 65 20 69 6d 70 6c Alternative impl
02e0: 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 75 ementations.** u
02f0: 73 65 20 66 6c 6f 63 6b 28 29 2c 20 64 6f 74 2d se flock(), dot-
0300: 66 69 6c 65 73 2c 20 76 61 72 69 6f 75 73 20 70 files, various p
0310: 72 6f 70 72 69 65 74 61 72 79 20 6c 6f 63 6b 69 roprietary locki
0320: 6e 67 20 73 63 68 65 6d 61 73 2c 20 6f 72 20 73 ng schemas, or s
0330: 69 6d 70 6c 79 0a 2a 2a 20 73 6b 69 70 20 6c 6f imply.** skip lo
0340: 63 6b 69 6e 67 20 61 6c 6c 20 74 6f 67 65 74 68 cking all togeth
0350: 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 er..**.** This s
0360: 6f 75 72 63 65 20 66 69 6c 65 20 69 73 20 6f 72 ource file is or
0370: 67 61 6e 69 7a 65 64 20 69 6e 74 6f 20 64 69 76 ganized into div
0380: 69 73 69 6f 6e 73 20 77 68 65 72 65 20 74 68 65 isions where the
0390: 20 6c 6f 67 69 63 20 66 6f 72 20 76 61 72 69 6f logic for vario
03a0: 75 73 0a 2a 2a 20 73 75 62 66 75 6e 63 74 69 6f us.** subfunctio
03b0: 6e 73 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 20 ns is contained
03c0: 77 69 74 68 69 6e 20 74 68 65 20 61 70 70 72 6f within the appro
03d0: 70 72 69 61 74 65 20 64 69 76 69 73 69 6f 6e 2e priate division.
03e0: 20 20 50 4c 45 41 53 45 0a 2a 2a 20 4b 45 45 50 PLEASE.** KEEP
03f0: 20 54 48 45 20 53 54 52 55 43 54 55 52 45 20 4f THE STRUCTURE O
0400: 46 20 54 48 49 53 20 46 49 4c 45 20 49 4e 54 41 F THIS FILE INTA
0410: 43 54 2e 20 20 4e 65 77 20 63 6f 64 65 20 73 68 CT. New code sh
0420: 6f 75 6c 64 20 62 65 20 70 6c 61 63 65 64 0a 2a ould be placed.*
0430: 2a 20 69 6e 20 74 68 65 20 63 6f 72 72 65 63 74 * in the correct
0440: 20 64 69 76 69 73 69 6f 6e 20 61 6e 64 20 73 68 division and sh
0450: 6f 75 6c 64 20 62 65 20 63 6c 65 61 72 6c 79 20 ould be clearly
0460: 6c 61 62 65 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 labeled..**.** T
0470: 68 65 20 6c 61 79 6f 75 74 20 6f 66 20 64 69 76 he layout of div
0480: 69 73 69 6f 6e 73 20 69 73 20 61 73 20 66 6f 6c isions is as fol
0490: 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 lows:.**.** *
04a0: 20 47 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73 65 General-purpose
04b0: 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 61 6e declarations an
04c0: 64 20 75 74 69 6c 69 74 79 20 66 75 6e 63 74 69 d utility functi
04d0: 6f 6e 73 2e 0a 2a 2a 20 20 20 2a 20 20 55 6e 69 ons..** * Uni
04e0: 71 75 65 20 66 69 6c 65 20 49 44 20 6c 6f 67 69 que file ID logi
04f0: 63 20 75 73 65 64 20 62 79 20 56 78 57 6f 72 6b c used by VxWork
0500: 73 2e 0a 2a 2a 20 20 20 2a 20 20 56 61 72 69 6f s..** * Vario
0510: 75 73 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 us locking primi
0520: 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 tive implementat
0530: 69 6f 6e 73 20 28 61 6c 6c 20 65 78 63 65 70 74 ions (all except
0540: 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 29 3a proxy locking):
0550: 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 50 .** + for P
0560: 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f osix Advisory Lo
0570: 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f cks.** + fo
0580: 72 20 6e 6f 2d 6f 70 20 6c 6f 63 6b 73 0a 2a 2a r no-op locks.**
0590: 20 20 20 20 20 20 2b 20 66 6f 72 20 64 6f 74 2d + for dot-
05a0: 66 69 6c 65 20 6c 6f 63 6b 73 0a 2a 2a 20 20 20 file locks.**
05b0: 20 20 20 2b 20 66 6f 72 20 66 6c 6f 63 6b 28 29 + for flock()
05c0: 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 locking.**
05d0: 20 2b 20 66 6f 72 20 6e 61 6d 65 64 20 73 65 6d + for named sem
05e0: 61 70 68 6f 72 65 20 6c 6f 63 6b 73 20 28 56 78 aphore locks (Vx
05f0: 57 6f 72 6b 73 20 6f 6e 6c 79 29 0a 2a 2a 20 20 Works only).**
0600: 20 20 20 20 2b 20 66 6f 72 20 41 46 50 20 66 69 + for AFP fi
0610: 6c 65 73 79 73 74 65 6d 20 6c 6f 63 6b 73 20 28 lesystem locks (
0620: 4d 61 63 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a 20 MacOSX only).**
0630: 20 20 2a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c * sqlite3_fil
0640: 65 20 6d 65 74 68 6f 64 73 20 6e 6f 74 20 61 73 e methods not as
0650: 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6c 6f sociated with lo
0660: 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20 2a 20 20 44 cking..** * D
0670: 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71 efinitions of sq
0680: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
0690: 20 6f 62 6a 65 63 74 73 20 66 6f 72 20 61 6c 6c objects for all
06a0: 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 locking.**
06b0: 20 6d 65 74 68 6f 64 73 20 70 6c 75 73 20 22 66 methods plus "f
06c0: 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 73 inder" functions
06d0: 20 66 6f 72 20 65 61 63 68 20 6c 6f 63 6b 69 6e for each lockin
06e0: 67 20 6d 65 74 68 6f 64 2e 0a 2a 2a 20 20 20 2a g method..** *
06f0: 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 sqlite3_vfs me
0700: 74 68 6f 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 thod implementat
0710: 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a 20 20 4c 6f ions..** * Lo
0720: 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73 cking primitives
0730: 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79 20 75 for the proxy u
0740: 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d 6d 65 74 68 ber-locking-meth
0750: 6f 64 2e 20 28 4d 61 63 4f 53 58 20 6f 6e 6c 79 od. (MacOSX only
0760: 29 0a 2a 2a 20 20 20 2a 20 20 44 65 66 69 6e 69 ).** * Defini
0770: 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 tions of sqlite3
0780: 5f 76 66 73 20 6f 62 6a 65 63 74 73 20 66 6f 72 _vfs objects for
0790: 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 all locking met
07a0: 68 6f 64 73 0a 2a 2a 20 20 20 20 20 20 70 6c 75 hods.** plu
07b0: 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e s implementation
07c0: 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 6f 73 5f s of sqlite3_os_
07d0: 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c 69 74 init() and sqlit
07e0: 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2f 0a e3_os_end()..*/.
07f0: 23 69 6e 63 6c 75 64 65 20 22 73 71 6c 69 74 65 #include "sqlite
0800: 49 6e 74 2e 68 22 0a 23 69 66 20 53 51 4c 49 54 Int.h".#if SQLIT
0810: 45 5f 4f 53 5f 55 4e 49 58 20 20 20 20 20 20 20 E_OS_UNIX
0820: 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 /* This f
0830: 69 6c 65 20 69 73 20 75 73 65 64 20 6f 6e 20 75 ile is used on u
0840: 6e 69 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a nix only */../*.
0850: 2a 2a 20 54 68 65 72 65 20 61 72 65 20 76 61 72 ** There are var
0860: 69 6f 75 73 20 6d 65 74 68 6f 64 73 20 66 6f 72 ious methods for
0870: 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 file locking us
0880: 65 64 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e ed for concurren
0890: 63 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 3a 0a 2a cy.** control:.*
08a0: 2a 0a 2a 2a 20 20 20 31 2e 20 50 4f 53 49 58 20 *.** 1. POSIX
08b0: 6c 6f 63 6b 69 6e 67 20 28 74 68 65 20 64 65 66 locking (the def
08c0: 61 75 6c 74 29 2c 0a 2a 2a 20 20 20 32 2e 20 4e ault),.** 2. N
08d0: 6f 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 o locking,.**
08e0: 33 2e 20 44 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 3. Dot-file lock
08f0: 69 6e 67 2c 0a 2a 2a 20 20 20 34 2e 20 66 6c 6f ing,.** 4. flo
0900: 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a ck() locking,.**
0910: 20 20 20 35 2e 20 41 46 50 20 6c 6f 63 6b 69 6e 5. AFP lockin
0920: 67 20 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a 2a 2a g (OSX only),.**
0930: 20 20 20 36 2e 20 4e 61 6d 65 64 20 50 4f 53 49 6. Named POSI
0940: 58 20 73 65 6d 61 70 68 6f 72 65 73 20 28 56 58 X semaphores (VX
0950: 57 6f 72 6b 73 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 Works only),.**
0960: 20 20 37 2e 20 70 72 6f 78 79 20 6c 6f 63 6b 69 7. proxy locki
0970: 6e 67 2e 20 28 4f 53 58 20 6f 6e 6c 79 29 0a 2a ng. (OSX only).*
0980: 2a 0a 2a 2a 20 53 74 79 6c 65 73 20 34 2c 20 35 *.** Styles 4, 5
0990: 2c 20 61 6e 64 20 37 20 61 72 65 20 6f 6e 6c 79 , and 7 are only
09a0: 20 61 76 61 69 6c 61 62 6c 65 20 6f 66 20 53 51 available of SQ
09b0: 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
09c0: 49 4e 47 5f 53 54 59 4c 45 0a 2a 2a 20 69 73 20 ING_STYLE.** is
09d0: 64 65 66 69 6e 65 64 20 74 6f 20 31 2e 20 20 54 defined to 1. T
09e0: 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 he SQLITE_ENABLE
09f0: 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 61 _LOCKING_STYLE a
0a00: 6c 73 6f 20 65 6e 61 62 6c 65 73 20 61 75 74 6f lso enables auto
0a10: 6d 61 74 69 63 0a 2a 2a 20 73 65 6c 65 63 74 69 matic.** selecti
0a20: 6f 6e 20 6f 66 20 74 68 65 20 61 70 70 72 6f 70 on of the approp
0a30: 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 73 74 riate locking st
0a40: 79 6c 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 yle based on the
0a50: 20 66 69 6c 65 73 79 73 74 65 6d 0a 2a 2a 20 77 filesystem.** w
0a60: 68 65 72 65 20 74 68 65 20 64 61 74 61 62 61 73 here the databas
0a70: 65 20 69 73 20 6c 6f 63 61 74 65 64 2e 20 20 0a e is located. .
0a80: 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 */.#if !defined(
0a90: 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
0aa0: 43 4b 49 4e 47 5f 53 54 59 4c 45 29 0a 23 20 20 CKING_STYLE).#
0ab0: 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 if defined(__APP
0ac0: 4c 45 5f 5f 29 0a 23 20 20 20 20 64 65 66 69 6e LE__).# defin
0ad0: 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f e SQLITE_ENABLE_
0ae0: 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0a LOCKING_STYLE 1.
0af0: 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 # else.# def
0b00: 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c ine SQLITE_ENABL
0b10: 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
0b20: 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 0.# endif.#endi
0b30: 66 0a 0a 2f 2a 20 55 73 65 20 70 72 65 61 64 28 f../* Use pread(
0b40: 29 20 61 6e 64 20 70 77 72 69 74 65 28 29 20 69 ) and pwrite() i
0b50: 66 20 74 68 65 79 20 61 72 65 20 61 76 61 69 6c f they are avail
0b60: 61 62 6c 65 20 2a 2f 0a 23 69 66 20 64 65 66 69 able */.#if defi
0b70: 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 23 ned(__APPLE__).#
0b80: 20 64 65 66 69 6e 65 20 48 41 56 45 5f 50 52 45 define HAVE_PRE
0b90: 41 44 20 31 0a 23 20 64 65 66 69 6e 65 20 48 41 AD 1.# define HA
0ba0: 56 45 5f 50 57 52 49 54 45 20 31 0a 23 65 6e 64 VE_PWRITE 1.#end
0bb0: 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 48 if.#if defined(H
0bc0: 41 56 45 5f 50 52 45 41 44 36 34 29 20 26 26 20 AVE_PREAD64) &&
0bd0: 64 65 66 69 6e 65 64 28 48 41 56 45 5f 50 57 52 defined(HAVE_PWR
0be0: 49 54 45 36 34 29 0a 23 20 75 6e 64 65 66 20 55 ITE64).# undef U
0bf0: 53 45 5f 50 52 45 41 44 0a 23 20 64 65 66 69 6e SE_PREAD.# defin
0c00: 65 20 55 53 45 5f 50 52 45 41 44 36 34 20 31 0a e USE_PREAD64 1.
0c10: 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 #elif defined(HA
0c20: 56 45 5f 50 52 45 41 44 29 20 26 26 20 64 65 66 VE_PREAD) && def
0c30: 69 6e 65 64 28 48 41 56 45 5f 50 57 52 49 54 45 ined(HAVE_PWRITE
0c40: 29 0a 23 20 75 6e 64 65 66 20 55 53 45 5f 50 52 ).# undef USE_PR
0c50: 45 41 44 36 34 0a 23 20 64 65 66 69 6e 65 20 55 EAD64.# define U
0c60: 53 45 5f 50 52 45 41 44 20 31 0a 23 65 6e 64 69 SE_PREAD 1.#endi
0c70: 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 6e 64 61 72 f../*.** standar
0c80: 64 20 69 6e 63 6c 75 64 65 20 66 69 6c 65 73 2e d include files.
0c90: 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 .*/.#include <sy
0ca0: 73 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c s/types.h>.#incl
0cb0: 75 64 65 20 3c 73 79 73 2f 73 74 61 74 2e 68 3e ude <sys/stat.h>
0cc0: 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e 74 6c .#include <fcntl
0cd0: 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e .h>.#include <un
0ce0: 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 istd.h>.#include
0cf0: 20 3c 74 69 6d 65 2e 68 3e 0a 23 69 6e 63 6c 75 <time.h>.#inclu
0d00: 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e 68 3e 0a de <sys/time.h>.
0d10: 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e 6f 2e #include <errno.
0d20: 68 3e 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 h>.#if !defined(
0d30: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 29 SQLITE_OMIT_WAL)
0d40: 20 7c 7c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4d || SQLITE_MAX_M
0d50: 4d 41 50 5f 53 49 5a 45 3e 30 0a 23 20 69 6e 63 MAP_SIZE>0.# inc
0d60: 6c 75 64 65 20 3c 73 79 73 2f 6d 6d 61 6e 2e 68 lude <sys/mman.h
0d70: 3e 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 53 51 >.#endif..#if SQ
0d80: 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
0d90: 49 4e 47 5f 53 54 59 4c 45 0a 23 20 69 6e 63 6c ING_STYLE.# incl
0da0: 75 64 65 20 3c 73 79 73 2f 69 6f 63 74 6c 2e 68 ude <sys/ioctl.h
0db0: 3e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 >.# include <sys
0dc0: 2f 66 69 6c 65 2e 68 3e 0a 23 20 69 6e 63 6c 75 /file.h>.# inclu
0dd0: 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e 68 3e de <sys/param.h>
0de0: 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
0df0: 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
0e00: 5f 53 54 59 4c 45 20 2a 2f 0a 0a 23 69 66 20 64 _STYLE */..#if d
0e10: 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f efined(__APPLE__
0e20: 29 20 26 26 20 28 28 5f 5f 4d 41 43 5f 4f 53 5f ) && ((__MAC_OS_
0e30: 58 5f 56 45 52 53 49 4f 4e 5f 4d 49 4e 5f 52 45 X_VERSION_MIN_RE
0e40: 51 55 49 52 45 44 20 3e 20 31 30 35 30 29 20 7c QUIRED > 1050) |
0e50: 7c 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 | \.
0e60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
0e70: 5f 5f 49 50 48 4f 4e 45 5f 4f 53 5f 56 45 52 53 __IPHONE_OS_VERS
0e80: 49 4f 4e 5f 4d 49 4e 5f 52 45 51 55 49 52 45 44 ION_MIN_REQUIRED
0e90: 20 3e 20 32 30 30 30 29 29 0a 23 20 20 69 66 20 > 2000)).# if
0ea0: 28 21 64 65 66 69 6e 65 64 28 54 41 52 47 45 54 (!defined(TARGET
0eb0: 5f 4f 53 5f 45 4d 42 45 44 44 45 44 29 20 7c 7c _OS_EMBEDDED) ||
0ec0: 20 28 54 41 52 47 45 54 5f 4f 53 5f 45 4d 42 45 (TARGET_OS_EMBE
0ed0: 44 44 45 44 3d 3d 30 29 29 20 5c 0a 20 20 20 20 DDED==0)) \.
0ee0: 20 20 20 26 26 20 28 21 64 65 66 69 6e 65 64 28 && (!defined(
0ef0: 54 41 52 47 45 54 5f 49 50 48 4f 4e 45 5f 53 49 TARGET_IPHONE_SI
0f00: 4d 55 4c 41 54 4f 52 29 20 7c 7c 20 28 54 41 52 MULATOR) || (TAR
0f10: 47 45 54 5f 49 50 48 4f 4e 45 5f 53 49 4d 55 4c GET_IPHONE_SIMUL
0f20: 41 54 4f 52 3d 3d 30 29 29 0a 23 20 20 20 20 64 ATOR==0)).# d
0f30: 65 66 69 6e 65 20 48 41 56 45 5f 47 45 54 48 4f efine HAVE_GETHO
0f40: 53 54 55 55 49 44 20 31 0a 23 20 20 65 6c 73 65 STUUID 1.# else
0f50: 0a 23 20 20 20 20 77 61 72 6e 69 6e 67 20 22 67 .# warning "g
0f60: 65 74 68 6f 73 74 75 75 69 64 28 29 20 69 73 20 ethostuuid() is
0f70: 64 69 73 61 62 6c 65 64 2e 22 0a 23 20 20 65 6e disabled.".# en
0f80: 64 69 66 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 dif.#endif...#if
0f90: 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20 69 6e OS_VXWORKS.# in
0fa0: 63 6c 75 64 65 20 3c 73 79 73 2f 69 6f 63 74 6c clude <sys/ioctl
0fb0: 2e 68 3e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 .h>.# include <s
0fc0: 65 6d 61 70 68 6f 72 65 2e 68 3e 0a 23 20 69 6e emaphore.h>.# in
0fd0: 63 6c 75 64 65 20 3c 6c 69 6d 69 74 73 2e 68 3e clude <limits.h>
0fe0: 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 .#endif /* OS_VX
0ff0: 57 4f 52 4b 53 20 2a 2f 0a 0a 23 69 66 20 64 65 WORKS */..#if de
1000: 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
1010: 20 7c 7c 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c || SQLITE_ENABL
1020: 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
1030: 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 6d # include <sys/m
1040: 6f 75 6e 74 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a ount.h>.#endif..
1050: 23 69 66 64 65 66 20 48 41 56 45 5f 55 54 49 4d #ifdef HAVE_UTIM
1060: 45 0a 23 20 69 6e 63 6c 75 64 65 20 3c 75 74 69 E.# include <uti
1070: 6d 65 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a me.h>.#endif../*
1080: 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 .** Allowed valu
1090: 65 73 20 6f 66 20 75 6e 69 78 46 69 6c 65 2e 66 es of unixFile.f
10a0: 73 46 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e sFlags.*/.#defin
10b0: 65 20 53 51 4c 49 54 45 5f 46 53 46 4c 41 47 53 e SQLITE_FSFLAGS
10c0: 5f 49 53 5f 4d 53 44 4f 53 20 20 20 20 20 30 78 _IS_MSDOS 0x
10d0: 31 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 1../*.** If we a
10e0: 72 65 20 74 6f 20 62 65 20 74 68 72 65 61 64 2d re to be thread-
10f0: 73 61 66 65 2c 20 69 6e 63 6c 75 64 65 20 74 68 safe, include th
1100: 65 20 70 74 68 72 65 61 64 73 20 68 65 61 64 65 e pthreads heade
1110: 72 20 61 6e 64 20 64 65 66 69 6e 65 0a 2a 2a 20 r and define.**
1120: 74 68 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f the SQLITE_UNIX_
1130: 54 48 52 45 41 44 53 20 6d 61 63 72 6f 2e 0a 2a THREADS macro..*
1140: 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 /.#if SQLITE_THR
1150: 45 41 44 53 41 46 45 0a 23 20 69 6e 63 6c 75 64 EADSAFE.# includ
1160: 65 20 3c 70 74 68 72 65 61 64 2e 68 3e 0a 23 20 e <pthread.h>.#
1170: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 4e define SQLITE_UN
1180: 49 58 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e IX_THREADS 1.#en
1190: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 75 dif../*.** Defau
11a0: 6c 74 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 77 lt permissions w
11b0: 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e hen creating a n
11c0: 65 77 20 66 69 6c 65 0a 2a 2f 0a 23 69 66 6e 64 ew file.*/.#ifnd
11d0: 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ef SQLITE_DEFAUL
11e0: 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f T_FILE_PERMISSIO
11f0: 4e 53 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 NS.# define SQLI
1200: 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f TE_DEFAULT_FILE_
1210: 50 45 52 4d 49 53 53 49 4f 4e 53 20 30 36 34 34 PERMISSIONS 0644
1220: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 .#endif../*.** D
1230: 65 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69 6f efault permissio
1240: 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 ns when creating
1250: 20 61 75 74 6f 20 70 72 6f 78 79 20 64 69 72 0a auto proxy dir.
1260: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
1270: 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 E_DEFAULT_PROXYD
1280: 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23 IR_PERMISSIONS.#
1290: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 define SQLITE_D
12a0: 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f EFAULT_PROXYDIR_
12b0: 50 45 52 4d 49 53 53 49 4f 4e 53 20 30 37 35 35 PERMISSIONS 0755
12c0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d .#endif../*.** M
12d0: 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 65 64 aximum supported
12e0: 20 70 61 74 68 2d 6c 65 6e 67 74 68 2e 0a 2a 2f path-length..*/
12f0: 0a 23 64 65 66 69 6e 65 20 4d 41 58 5f 50 41 54 .#define MAX_PAT
1300: 48 4e 41 4d 45 20 35 31 32 0a 0a 2f 2a 0a 2a 2a HNAME 512../*.**
1310: 20 4d 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 Maximum support
1320: 65 64 20 73 79 6d 62 6f 6c 69 63 20 6c 69 6e 6b ed symbolic link
1330: 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c s.*/.#define SQL
1340: 49 54 45 5f 4d 41 58 5f 53 59 4d 4c 49 4e 4b 53 ITE_MAX_SYMLINKS
1350: 20 31 30 30 0a 0a 2f 2a 20 41 6c 77 61 79 73 20 100../* Always
1360: 63 61 73 74 20 74 68 65 20 67 65 74 70 69 64 28 cast the getpid(
1370: 29 20 72 65 74 75 72 6e 20 74 79 70 65 20 66 6f ) return type fo
1380: 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 r compatibility
1390: 77 69 74 68 0a 2a 2a 20 6b 65 72 6e 65 6c 20 6d with.** kernel m
13a0: 6f 64 75 6c 65 73 20 69 6e 20 56 78 57 6f 72 6b odules in VxWork
13b0: 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 6f 73 s. */.#define os
13c0: 47 65 74 70 69 64 28 58 29 20 28 70 69 64 5f 74 Getpid(X) (pid_t
13d0: 29 67 65 74 70 69 64 28 29 0a 0a 2f 2a 0a 2a 2a )getpid()../*.**
13e0: 20 4f 6e 6c 79 20 73 65 74 20 74 68 65 20 6c 61 Only set the la
13f0: 73 74 45 72 72 6e 6f 20 69 66 20 74 68 65 20 65 stErrno if the e
1400: 72 72 6f 72 20 63 6f 64 65 20 69 73 20 61 20 72 rror code is a r
1410: 65 61 6c 20 65 72 72 6f 72 20 61 6e 64 20 6e 6f eal error and no
1420: 74 20 0a 2a 2a 20 61 20 6e 6f 72 6d 61 6c 20 65 t .** a normal e
1430: 78 70 65 63 74 65 64 20 72 65 74 75 72 6e 20 63 xpected return c
1440: 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f 42 55 ode of SQLITE_BU
1450: 53 59 20 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 0a SY or SQLITE_OK.
1460: 2a 2f 0a 23 64 65 66 69 6e 65 20 49 53 5f 4c 4f */.#define IS_LO
1470: 43 4b 5f 45 52 52 4f 52 28 78 29 20 20 28 28 78 CK_ERROR(x) ((x
1480: 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 20 26 != SQLITE_OK) &
1490: 26 20 28 78 20 21 3d 20 53 51 4c 49 54 45 5f 42 & (x != SQLITE_B
14a0: 55 53 59 29 29 0a 0a 2f 2a 20 46 6f 72 77 61 72 USY))../* Forwar
14b0: 64 20 72 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a d references */.
14c0: 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 75 typedef struct u
14d0: 6e 69 78 53 68 6d 20 75 6e 69 78 53 68 6d 3b 20 nixShm unixShm;
14e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
14f0: 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 73 68 61 72 Connection shar
1500: 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 74 79 70 ed memory */.typ
1510: 65 64 65 66 20 73 74 72 75 63 74 20 75 6e 69 78 edef struct unix
1520: 53 68 6d 4e 6f 64 65 20 75 6e 69 78 53 68 6d 4e ShmNode unixShmN
1530: 6f 64 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 68 ode; /* Sh
1540: 61 72 65 64 20 6d 65 6d 6f 72 79 20 69 6e 73 74 ared memory inst
1550: 61 6e 63 65 20 2a 2f 0a 74 79 70 65 64 65 66 20 ance */.typedef
1560: 73 74 72 75 63 74 20 75 6e 69 78 49 6e 6f 64 65 struct unixInode
1570: 49 6e 66 6f 20 75 6e 69 78 49 6e 6f 64 65 49 6e Info unixInodeIn
1580: 66 6f 3b 20 20 20 2f 2a 20 41 6e 20 69 2d 6e 6f fo; /* An i-no
1590: 64 65 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 de */.typedef st
15a0: 72 75 63 74 20 55 6e 69 78 55 6e 75 73 65 64 46 ruct UnixUnusedF
15b0: 64 20 55 6e 69 78 55 6e 75 73 65 64 46 64 3b 20 d UnixUnusedFd;
15c0: 20 20 20 20 2f 2a 20 41 6e 20 75 6e 75 73 65 64 /* An unused
15d0: 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
15e0: 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 */../*.** Somet
15f0: 69 6d 65 73 2c 20 61 66 74 65 72 20 61 20 66 69 imes, after a fi
1600: 6c 65 20 68 61 6e 64 6c 65 20 69 73 20 63 6c 6f le handle is clo
1610: 73 65 64 20 62 79 20 53 51 4c 69 74 65 2c 20 74 sed by SQLite, t
1620: 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
1630: 6f 72 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 or.** cannot be
1640: 63 6c 6f 73 65 64 20 69 6d 6d 65 64 69 61 74 65 closed immediate
1650: 6c 79 2e 20 49 6e 20 74 68 65 73 65 20 63 61 73 ly. In these cas
1660: 65 73 2c 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 es, instances of
1670: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a the following.*
1680: 2a 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 * structure are
1690: 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 used to store th
16a0: 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
16b0: 72 20 77 68 69 6c 65 20 77 61 69 74 69 6e 67 20 r while waiting
16c0: 66 6f 72 20 61 6e 0a 2a 2a 20 6f 70 70 6f 72 74 for an.** opport
16d0: 75 6e 69 74 79 20 74 6f 20 65 69 74 68 65 72 20 unity to either
16e0: 63 6c 6f 73 65 20 6f 72 20 72 65 75 73 65 20 69 close or reuse i
16f0: 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 55 6e 69 t..*/.struct Uni
1700: 78 55 6e 75 73 65 64 46 64 20 7b 0a 20 20 69 6e xUnusedFd {. in
1710: 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 t fd;
1720: 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 /* File
1730: 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 63 6c descriptor to cl
1740: 6f 73 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 ose */. int fla
1750: 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 gs;
1760: 20 20 20 2f 2a 20 46 6c 61 67 73 20 74 68 69 73 /* Flags this
1770: 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
1780: 20 77 61 73 20 6f 70 65 6e 65 64 20 77 69 74 68 was opened with
1790: 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 */. UnixUnused
17a0: 46 64 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 Fd *pNext;
17b0: 2f 2a 20 4e 65 78 74 20 75 6e 75 73 65 64 20 66 /* Next unused f
17c0: 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f ile descriptor o
17d0: 6e 20 73 61 6d 65 20 66 69 6c 65 20 2a 2f 0a 7d n same file */.}
17e0: 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 75 6e 69 ;../*.** The uni
17f0: 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 xFile structure
1800: 69 73 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 is subclass of s
1810: 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63 qlite3_file spec
1820: 69 66 69 63 20 74 6f 20 74 68 65 20 75 6e 69 78 ific to the unix
1830: 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e .** VFS implemen
1840: 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 65 tations..*/.type
1850: 64 65 66 20 73 74 72 75 63 74 20 75 6e 69 78 46 def struct unixF
1860: 69 6c 65 20 75 6e 69 78 46 69 6c 65 3b 0a 73 74 ile unixFile;.st
1870: 72 75 63 74 20 75 6e 69 78 46 69 6c 65 20 7b 0a ruct unixFile {.
1880: 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 sqlite3_io_met
1890: 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 4d 65 74 hods const *pMet
18a0: 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 hod; /* Always
18b0: 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 the first entry
18c0: 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 */. sqlite3_vfs
18d0: 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 *pVfs;
18e0: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
18f0: 56 46 53 20 74 68 61 74 20 63 72 65 61 74 65 64 VFS that created
1900: 20 74 68 69 73 20 75 6e 69 78 46 69 6c 65 20 2a this unixFile *
1910: 2f 0a 20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 /. unixInodeInf
1920: 6f 20 2a 70 49 6e 6f 64 65 3b 20 20 20 20 20 20 o *pInode;
1930: 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 /* Info
1940: 61 62 6f 75 74 20 6c 6f 63 6b 73 20 6f 6e 20 74 about locks on t
1950: 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 69 his inode */. i
1960: 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 nt h;
1970: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1980: 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 /* The file d
1990: 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 75 escriptor */. u
19a0: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 65 46 69 nsigned char eFi
19b0: 6c 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 leLock;
19c0: 20 20 20 2f 2a 20 54 68 65 20 74 79 70 65 20 6f /* The type o
19d0: 66 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 f lock held on t
19e0: 68 69 73 20 66 64 20 2a 2f 0a 20 20 75 6e 73 69 his fd */. unsi
19f0: 67 6e 65 64 20 73 68 6f 72 74 20 69 6e 74 20 63 gned short int c
1a00: 74 72 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 20 trlFlags;
1a10: 2f 2a 20 42 65 68 61 76 69 6f 72 61 6c 20 62 69 /* Behavioral bi
1a20: 74 73 2e 20 20 55 4e 49 58 46 49 4c 45 5f 2a 20 ts. UNIXFILE_*
1a30: 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 6c flags */. int l
1a40: 61 73 74 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 astErrno;
1a50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a60: 2a 20 54 68 65 20 75 6e 69 78 20 65 72 72 6e 6f * The unix errno
1a70: 20 66 72 6f 6d 20 6c 61 73 74 20 49 2f 4f 20 65 from last I/O e
1a80: 72 72 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 20 2a rror */. void *
1a90: 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 lockingContext;
1aa0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ab0: 20 4c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 73 Locking style s
1ac0: 70 65 63 69 66 69 63 20 73 74 61 74 65 20 2a 2f pecific state */
1ad0: 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 . UnixUnusedFd
1ae0: 2a 70 55 6e 75 73 65 64 3b 20 20 20 20 20 20 20 *pUnused;
1af0: 20 20 20 20 20 20 20 2f 2a 20 50 72 65 2d 61 6c /* Pre-al
1b00: 6c 6f 63 61 74 65 64 20 55 6e 69 78 55 6e 75 73 located UnixUnus
1b10: 65 64 46 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 edFd */. const
1b20: 63 68 61 72 20 2a 7a 50 61 74 68 3b 20 20 20 20 char *zPath;
1b30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1b40: 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c Name of the fil
1b50: 65 20 2a 2f 0a 20 20 75 6e 69 78 53 68 6d 20 2a e */. unixShm *
1b60: 70 53 68 6d 3b 20 20 20 20 20 20 20 20 20 20 20 pShm;
1b70: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 /* Sh
1b80: 61 72 65 64 20 6d 65 6d 6f 72 79 20 73 65 67 6d ared memory segm
1b90: 65 6e 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ent information
1ba0: 2a 2f 0a 20 20 69 6e 74 20 73 7a 43 68 75 6e 6b */. int szChunk
1bb0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1bc0: 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 /* Conf
1bd0: 69 67 75 72 65 64 20 62 79 20 46 43 4e 54 4c 5f igured by FCNTL_
1be0: 43 48 55 4e 4b 5f 53 49 5a 45 20 2a 2f 0a 23 69 CHUNK_SIZE */.#i
1bf0: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4d 4d 41 f SQLITE_MAX_MMA
1c00: 50 5f 53 49 5a 45 3e 30 0a 20 20 69 6e 74 20 6e P_SIZE>0. int n
1c10: 46 65 74 63 68 4f 75 74 3b 20 20 20 20 20 20 20 FetchOut;
1c20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1c30: 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 75 74 73 * Number of outs
1c40: 74 61 6e 64 69 6e 67 20 78 46 65 74 63 68 20 72 tanding xFetch r
1c50: 65 66 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 efs */. sqlite3
1c60: 5f 69 6e 74 36 34 20 6d 6d 61 70 53 69 7a 65 3b _int64 mmapSize;
1c70: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c80: 55 73 61 62 6c 65 20 73 69 7a 65 20 6f 66 20 6d Usable size of m
1c90: 61 70 70 69 6e 67 20 61 74 20 70 4d 61 70 52 65 apping at pMapRe
1ca0: 67 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 gion */. sqlite
1cb0: 33 5f 69 6e 74 36 34 20 6d 6d 61 70 53 69 7a 65 3_int64 mmapSize
1cc0: 41 63 74 75 61 6c 3b 20 20 20 20 20 20 20 2f 2a Actual; /*
1cd0: 20 41 63 74 75 61 6c 20 73 69 7a 65 20 6f 66 20 Actual size of
1ce0: 6d 61 70 70 69 6e 67 20 61 74 20 70 4d 61 70 52 mapping at pMapR
1cf0: 65 67 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 egion */. sqlit
1d00: 65 33 5f 69 6e 74 36 34 20 6d 6d 61 70 53 69 7a e3_int64 mmapSiz
1d10: 65 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20 2f eMax; /
1d20: 2a 20 43 6f 6e 66 69 67 75 72 65 64 20 46 43 4e * Configured FCN
1d30: 54 4c 5f 4d 4d 41 50 5f 53 49 5a 45 20 76 61 6c TL_MMAP_SIZE val
1d40: 75 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4d ue */. void *pM
1d50: 61 70 52 65 67 69 6f 6e 3b 20 20 20 20 20 20 20 apRegion;
1d60: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
1d70: 65 6d 6f 72 79 20 6d 61 70 70 65 64 20 72 65 67 emory mapped reg
1d80: 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 ion */.#endif.#i
1d90: 66 64 65 66 20 5f 5f 51 4e 58 4e 54 4f 5f 5f 0a fdef __QNXNTO__.
1da0: 20 20 69 6e 74 20 73 65 63 74 6f 72 53 69 7a 65 int sectorSize
1db0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1dc0: 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63 65 20 /* Device
1dd0: 73 65 63 74 6f 72 20 73 69 7a 65 20 2a 2f 0a 20 sector size */.
1de0: 20 69 6e 74 20 64 65 76 69 63 65 43 68 61 72 61 int deviceChara
1df0: 63 74 65 72 69 73 74 69 63 73 3b 20 20 20 20 20 cteristics;
1e00: 20 20 20 20 20 2f 2a 20 50 72 65 63 6f 6d 70 75 /* Precompu
1e10: 74 65 64 20 64 65 76 69 63 65 20 63 68 61 72 61 ted device chara
1e20: 63 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 23 65 cteristics */.#e
1e30: 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f ndif.#if SQLITE_
1e40: 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
1e50: 54 59 4c 45 0a 20 20 69 6e 74 20 6f 70 65 6e 46 TYLE. int openF
1e60: 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 lags;
1e70: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1e80: 65 20 66 6c 61 67 73 20 73 70 65 63 69 66 69 65 e flags specifie
1e90: 64 20 61 74 20 6f 70 65 6e 28 29 20 2a 2f 0a 23 d at open() */.#
1ea0: 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 endif.#if SQLITE
1eb0: 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
1ec0: 53 54 59 4c 45 20 7c 7c 20 64 65 66 69 6e 65 64 STYLE || defined
1ed0: 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 75 6e (__APPLE__). un
1ee0: 73 69 67 6e 65 64 20 66 73 46 6c 61 67 73 3b 20 signed fsFlags;
1ef0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f00: 20 20 2f 2a 20 63 61 63 68 65 64 20 64 65 74 61 /* cached deta
1f10: 69 6c 73 20 66 72 6f 6d 20 73 74 61 74 66 73 28 ils from statfs(
1f20: 29 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 20 ) */.#endif.#if
1f30: 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 OS_VXWORKS. str
1f40: 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 uct vxworksFileI
1f50: 64 20 2a 70 49 64 3b 20 20 20 20 20 20 20 20 20 d *pId;
1f60: 20 2f 2a 20 55 6e 69 71 75 65 20 66 69 6c 65 20 /* Unique file
1f70: 49 44 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 ID */.#endif.#if
1f80: 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
1f90: 0a 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 67 . /* The next g
1fa0: 72 6f 75 70 20 6f 66 20 76 61 72 69 61 62 6c 65 roup of variable
1fb0: 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 74 72 s are used to tr
1fc0: 61 63 6b 20 77 68 65 74 68 65 72 20 6f 72 20 6e ack whether or n
1fd0: 6f 74 20 74 68 65 0a 20 20 2a 2a 20 74 72 61 6e ot the. ** tran
1fe0: 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 saction counter
1ff0: 69 6e 20 62 79 74 65 73 20 32 34 2d 32 37 20 6f in bytes 24-27 o
2000: 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 f database files
2010: 20 61 72 65 20 75 70 64 61 74 65 64 0a 20 20 2a are updated. *
2020: 2a 20 77 68 65 6e 65 76 65 72 20 61 6e 79 20 70 * whenever any p
2030: 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 art of the datab
2040: 61 73 65 20 63 68 61 6e 67 65 73 2e 20 20 41 6e ase changes. An
2050: 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 assertion fault
2060: 20 77 69 6c 6c 0a 20 20 2a 2a 20 6f 63 63 75 72 will. ** occur
2070: 20 69 66 20 61 20 66 69 6c 65 20 69 73 20 75 70 if a file is up
2080: 64 61 74 65 64 20 77 69 74 68 6f 75 74 20 61 6c dated without al
2090: 73 6f 20 75 70 64 61 74 69 6e 67 20 74 68 65 20 so updating the
20a0: 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a transaction. **
20b0: 20 63 6f 75 6e 74 65 72 2e 20 20 54 68 69 73 20 counter. This
20c0: 74 65 73 74 20 69 73 20 6d 61 64 65 20 74 6f 20 test is made to
20d0: 61 76 6f 69 64 20 6e 65 77 20 70 72 6f 62 6c 65 avoid new proble
20e0: 6d 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 ms similar to th
20f0: 65 0a 20 20 2a 2a 20 6f 6e 65 20 64 65 73 63 72 e. ** one descr
2100: 69 62 65 64 20 62 79 20 74 69 63 6b 65 74 20 23 ibed by ticket #
2110: 33 35 38 34 2e 20 0a 20 20 2a 2f 0a 20 20 75 6e 3584. . */. un
2120: 73 69 67 6e 65 64 20 63 68 61 72 20 74 72 61 6e signed char tran
2130: 73 43 6e 74 72 43 68 6e 67 3b 20 20 20 2f 2a 20 sCntrChng; /*
2140: 54 72 75 65 20 69 66 20 74 68 65 20 74 72 61 6e True if the tran
2150: 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 saction counter
2160: 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 6e 73 changed */. uns
2170: 69 67 6e 65 64 20 63 68 61 72 20 64 62 55 70 64 igned char dbUpd
2180: 61 74 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 ate; /* T
2190: 72 75 65 20 69 66 20 61 6e 79 20 70 61 72 74 20 rue if any part
21a0: 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 of database file
21b0: 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 6e changed */. un
21c0: 73 69 67 6e 65 64 20 63 68 61 72 20 69 6e 4e 6f signed char inNo
21d0: 72 6d 61 6c 57 72 69 74 65 3b 20 20 20 2f 2a 20 rmalWrite; /*
21e0: 54 72 75 65 20 69 66 20 69 6e 20 61 20 6e 6f 72 True if in a nor
21f0: 6d 61 6c 20 77 72 69 74 65 20 6f 70 65 72 61 74 mal write operat
2200: 69 6f 6e 20 2a 2f 0a 0a 23 65 6e 64 69 66 0a 0a ion */..#endif..
2210: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
2220: 53 54 0a 20 20 2f 2a 20 49 6e 20 74 65 73 74 20 ST. /* In test
2230: 6d 6f 64 65 2c 20 69 6e 63 72 65 61 73 65 20 74 mode, increase t
2240: 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 20 he size of this
2250: 73 74 72 75 63 74 75 72 65 20 61 20 62 69 74 20 structure a bit
2260: 73 6f 20 74 68 61 74 20 0a 20 20 2a 2a 20 69 74 so that . ** it
2270: 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 is larger than
2280: 74 68 65 20 73 74 72 75 63 74 20 43 72 61 73 68 the struct Crash
2290: 46 69 6c 65 20 64 65 66 69 6e 65 64 20 69 6e 20 File defined in
22a0: 74 65 73 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20 20 test6.c.. */.
22b0: 63 68 61 72 20 61 50 61 64 64 69 6e 67 5b 33 32 char aPadding[32
22c0: 5d 3b 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a ];.#endif.};../*
22d0: 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 This variable h
22e0: 6f 6c 64 73 20 74 68 65 20 70 72 6f 63 65 73 73 olds the process
22f0: 20 69 64 20 28 70 69 64 29 20 66 72 6f 6d 20 77 id (pid) from w
2300: 68 65 6e 20 74 68 65 20 78 52 61 6e 64 6f 6d 6e hen the xRandomn
2310: 65 73 73 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 ess().** method
2320: 77 61 73 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 was called. If
2330: 78 4f 70 65 6e 28 29 20 69 73 20 63 61 6c 6c 65 xOpen() is calle
2340: 64 20 66 72 6f 6d 20 61 20 64 69 66 66 65 72 65 d from a differe
2350: 6e 74 20 70 72 6f 63 65 73 73 20 69 64 2c 0a 2a nt process id,.*
2360: 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 * indicating tha
2370: 74 20 61 20 66 6f 72 6b 28 29 20 68 61 73 20 6f t a fork() has o
2380: 63 63 75 72 72 65 64 2c 20 74 68 65 20 50 52 4e ccurred, the PRN
2390: 47 20 77 69 6c 6c 20 62 65 20 72 65 73 65 74 2e G will be reset.
23a0: 0a 2a 2f 0a 73 74 61 74 69 63 20 70 69 64 5f 74 .*/.static pid_t
23b0: 20 72 61 6e 64 6f 6d 6e 65 73 73 50 69 64 20 3d randomnessPid =
23c0: 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 0;../*.** Allow
23d0: 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 ed values for th
23e0: 65 20 75 6e 69 78 46 69 6c 65 2e 63 74 72 6c 46 e unixFile.ctrlF
23f0: 6c 61 67 73 20 62 69 74 6d 61 73 6b 3a 0a 2a 2f lags bitmask:.*/
2400: 0a 23 64 65 66 69 6e 65 20 55 4e 49 58 46 49 4c .#define UNIXFIL
2410: 45 5f 45 58 43 4c 20 20 20 20 20 20 20 20 30 78 E_EXCL 0x
2420: 30 31 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 01 /* Connec
2430: 74 69 6f 6e 73 20 66 72 6f 6d 20 6f 6e 65 20 70 tions from one p
2440: 72 6f 63 65 73 73 20 6f 6e 6c 79 20 2a 2f 0a 23 rocess only */.#
2450: 64 65 66 69 6e 65 20 55 4e 49 58 46 49 4c 45 5f define UNIXFILE_
2460: 52 44 4f 4e 4c 59 20 20 20 20 20 20 30 78 30 32 RDONLY 0x02
2470: 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 /* Connecti
2480: 6f 6e 20 69 73 20 72 65 61 64 20 6f 6e 6c 79 20 on is read only
2490: 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 49 58 46 */.#define UNIXF
24a0: 49 4c 45 5f 50 45 52 53 49 53 54 5f 57 41 4c 20 ILE_PERSIST_WAL
24b0: 30 78 30 34 20 20 20 20 20 2f 2a 20 50 65 72 73 0x04 /* Pers
24c0: 69 73 74 65 6e 74 20 57 41 4c 20 6d 6f 64 65 20 istent WAL mode
24d0: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
24e0: 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e E_DISABLE_DIRSYN
24f0: 43 0a 23 20 64 65 66 69 6e 65 20 55 4e 49 58 46 C.# define UNIXF
2500: 49 4c 45 5f 44 49 52 53 59 4e 43 20 20 20 20 30 ILE_DIRSYNC 0
2510: 78 30 38 20 20 20 20 20 2f 2a 20 44 69 72 65 63 x08 /* Direc
2520: 74 6f 72 79 20 73 79 6e 63 20 6e 65 65 64 65 64 tory sync needed
2530: 20 2a 2f 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 */.#else.# defi
2540: 6e 65 20 55 4e 49 58 46 49 4c 45 5f 44 49 52 53 ne UNIXFILE_DIRS
2550: 59 4e 43 20 20 20 20 30 78 30 30 0a 23 65 6e 64 YNC 0x00.#end
2560: 69 66 0a 23 64 65 66 69 6e 65 20 55 4e 49 58 46 if.#define UNIXF
2570: 49 4c 45 5f 50 53 4f 57 20 20 20 20 20 20 20 20 ILE_PSOW
2580: 30 78 31 30 20 20 20 20 20 2f 2a 20 53 51 4c 49 0x10 /* SQLI
2590: 54 45 5f 49 4f 43 41 50 5f 50 4f 57 45 52 53 41 TE_IOCAP_POWERSA
25a0: 46 45 5f 4f 56 45 52 57 52 49 54 45 20 2a 2f 0a FE_OVERWRITE */.
25b0: 23 64 65 66 69 6e 65 20 55 4e 49 58 46 49 4c 45 #define UNIXFILE
25c0: 5f 44 45 4c 45 54 45 20 20 20 20 20 20 30 78 32 _DELETE 0x2
25d0: 30 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 0 /* Delete
25e0: 6f 6e 20 63 6c 6f 73 65 20 2a 2f 0a 23 64 65 66 on close */.#def
25f0: 69 6e 65 20 55 4e 49 58 46 49 4c 45 5f 55 52 49 ine UNIXFILE_URI
2600: 20 20 20 20 20 20 20 20 20 30 78 34 30 20 20 20 0x40
2610: 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 6d 69 /* Filename mi
2620: 67 68 74 20 68 61 76 65 20 71 75 65 72 79 20 70 ght have query p
2630: 61 72 61 6d 65 74 65 72 73 20 2a 2f 0a 23 64 65 arameters */.#de
2640: 66 69 6e 65 20 55 4e 49 58 46 49 4c 45 5f 4e 4f fine UNIXFILE_NO
2650: 4c 4f 43 4b 20 20 20 20 20 20 30 78 38 30 20 20 LOCK 0x80
2660: 20 20 20 2f 2a 20 44 6f 20 6e 6f 20 66 69 6c 65 /* Do no file
2670: 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a locking */../*.
2680: 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 ** Include code
2690: 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 that is common t
26a0: 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c o all os_*.c fil
26b0: 65 73 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 22 es.*/.#include "
26c0: 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 22 0a 0a 2f 2a os_common.h"../*
26d0: 0a 2a 2a 20 44 65 66 69 6e 65 20 76 61 72 69 6f .** Define vario
26e0: 75 73 20 6d 61 63 72 6f 73 20 74 68 61 74 20 61 us macros that a
26f0: 72 65 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 re missing from
2700: 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e 0a 2a 2f some systems..*/
2710: 0a 23 69 66 6e 64 65 66 20 4f 5f 4c 41 52 47 45 .#ifndef O_LARGE
2720: 46 49 4c 45 0a 23 20 64 65 66 69 6e 65 20 4f 5f FILE.# define O_
2730: 4c 41 52 47 45 46 49 4c 45 20 30 0a 23 65 6e 64 LARGEFILE 0.#end
2740: 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 if.#ifdef SQLITE
2750: 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a 23 20 75 _DISABLE_LFS.# u
2760: 6e 64 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 ndef O_LARGEFILE
2770: 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 .# define O_LARG
2780: 45 46 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 EFILE 0.#endif.#
2790: 69 66 6e 64 65 66 20 4f 5f 4e 4f 46 4f 4c 4c 4f ifndef O_NOFOLLO
27a0: 57 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4e 4f 46 W.# define O_NOF
27b0: 4f 4c 4c 4f 57 20 30 0a 23 65 6e 64 69 66 0a 23 OLLOW 0.#endif.#
27c0: 69 66 6e 64 65 66 20 4f 5f 42 49 4e 41 52 59 0a ifndef O_BINARY.
27d0: 23 20 64 65 66 69 6e 65 20 4f 5f 42 49 4e 41 52 # define O_BINAR
27e0: 59 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a Y 0.#endif../*.*
27f0: 2a 20 54 68 65 20 74 68 72 65 61 64 69 64 20 6d * The threadid m
2800: 61 63 72 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f acro resolves to
2810: 20 74 68 65 20 74 68 72 65 61 64 2d 69 64 20 6f the thread-id o
2820: 72 20 74 6f 20 30 2e 20 20 55 73 65 64 20 66 6f r to 0. Used fo
2830: 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 r.** testing and
2840: 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e debugging only.
2850: 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 .*/.#if SQLITE_T
2860: 48 52 45 41 44 53 41 46 45 0a 23 64 65 66 69 6e HREADSAFE.#defin
2870: 65 20 74 68 72 65 61 64 69 64 20 70 74 68 72 65 e threadid pthre
2880: 61 64 5f 73 65 6c 66 28 29 0a 23 65 6c 73 65 0a ad_self().#else.
2890: 23 64 65 66 69 6e 65 20 74 68 72 65 61 64 69 64 #define threadid
28a0: 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0.#endif../*.**
28b0: 20 48 41 56 45 5f 4d 52 45 4d 41 50 20 64 65 66 HAVE_MREMAP def
28c0: 61 75 6c 74 73 20 74 6f 20 74 72 75 65 20 6f 6e aults to true on
28d0: 20 4c 69 6e 75 78 20 61 6e 64 20 66 61 6c 73 65 Linux and false
28e0: 20 65 76 65 72 79 77 68 65 72 65 20 65 6c 73 65 everywhere else
28f0: 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 ..*/.#if !define
2900: 64 28 48 41 56 45 5f 4d 52 45 4d 41 50 29 0a 23 d(HAVE_MREMAP).#
2910: 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 if defined(__li
2920: 6e 75 78 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 nux__) && define
2930: 64 28 5f 47 4e 55 5f 53 4f 55 52 43 45 29 0a 23 d(_GNU_SOURCE).#
2940: 20 20 64 65 66 69 6e 65 20 48 41 56 45 5f 4d 52 define HAVE_MR
2950: 45 4d 41 50 20 31 0a 23 20 65 6c 73 65 0a 23 20 EMAP 1.# else.#
2960: 20 64 65 66 69 6e 65 20 48 41 56 45 5f 4d 52 45 define HAVE_MRE
2970: 4d 41 50 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 MAP 0.# endif.#e
2980: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 6c ndif../*.** Expl
2990: 69 63 69 74 6c 79 20 63 61 6c 6c 20 74 68 65 20 icitly call the
29a0: 36 34 2d 62 69 74 20 76 65 72 73 69 6f 6e 20 6f 64-bit version o
29b0: 66 20 6c 73 65 65 6b 28 29 20 6f 6e 20 41 6e 64 f lseek() on And
29c0: 72 6f 69 64 2e 20 4f 74 68 65 72 77 69 73 65 2c roid. Otherwise,
29d0: 20 6c 73 65 65 6b 28 29 0a 2a 2a 20 69 73 20 74 lseek().** is t
29e0: 68 65 20 33 32 2d 62 69 74 20 76 65 72 73 69 6f he 32-bit versio
29f0: 6e 2c 20 65 76 65 6e 20 69 66 20 5f 46 49 4c 45 n, even if _FILE
2a00: 5f 4f 46 46 53 45 54 5f 42 49 54 53 3d 36 34 20 _OFFSET_BITS=64
2a10: 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 is defined..*/.#
2a20: 69 66 64 65 66 20 5f 5f 41 4e 44 52 4f 49 44 5f ifdef __ANDROID_
2a30: 5f 0a 23 20 64 65 66 69 6e 65 20 6c 73 65 65 6b _.# define lseek
2a40: 20 6c 73 65 65 6b 36 34 0a 23 65 6e 64 69 66 0a lseek64.#endif.
2a50: 0a 2f 2a 0a 2a 2a 20 44 69 66 66 65 72 65 6e 74 ./*.** Different
2a60: 20 55 6e 69 78 20 73 79 73 74 65 6d 73 20 64 65 Unix systems de
2a70: 63 6c 61 72 65 20 6f 70 65 6e 28 29 20 69 6e 20 clare open() in
2a80: 64 69 66 66 65 72 65 6e 74 20 77 61 79 73 2e 20 different ways.
2a90: 20 53 61 6d 65 20 75 73 65 0a 2a 2a 20 6f 70 65 Same use.** ope
2aa0: 6e 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e n(const char*,in
2ab0: 74 2c 6d 6f 64 65 5f 74 29 2e 20 20 4f 74 68 65 t,mode_t). Othe
2ac0: 72 73 20 75 73 65 20 6f 70 65 6e 28 63 6f 6e 73 rs use open(cons
2ad0: 74 20 63 68 61 72 2a 2c 69 6e 74 2c 2e 2e 2e 29 t char*,int,...)
2ae0: 2e 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 ..** The differe
2af0: 6e 63 65 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 nce is important
2b00: 20 77 68 65 6e 20 75 73 69 6e 67 20 61 20 70 6f when using a po
2b10: 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75 6e inter to the fun
2b20: 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ction..**.** The
2b30: 20 73 61 66 65 73 74 20 77 61 79 20 74 6f 20 64 safest way to d
2b40: 65 61 6c 20 77 69 74 68 20 74 68 65 20 70 72 6f eal with the pro
2b50: 62 6c 65 6d 20 69 73 20 74 6f 20 61 6c 77 61 79 blem is to alway
2b60: 73 20 75 73 65 20 74 68 69 73 20 77 72 61 70 70 s use this wrapp
2b70: 65 72 0a 2a 2a 20 77 68 69 63 68 20 61 6c 77 61 er.** which alwa
2b80: 79 73 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 ys has the same
2b90: 77 65 6c 6c 2d 64 65 66 69 6e 65 64 20 69 6e 74 well-defined int
2ba0: 65 72 66 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 erface..*/.stati
2bb0: 63 20 69 6e 74 20 70 6f 73 69 78 4f 70 65 6e 28 c int posixOpen(
2bc0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
2bd0: 65 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e e, int flags, in
2be0: 74 20 6d 6f 64 65 29 7b 0a 20 20 72 65 74 75 72 t mode){. retur
2bf0: 6e 20 6f 70 65 6e 28 7a 46 69 6c 65 2c 20 66 6c n open(zFile, fl
2c00: 61 67 73 2c 20 6d 6f 64 65 29 3b 0a 7d 0a 0a 2f ags, mode);.}../
2c10: 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 * Forward refere
2c20: 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e nce */.static in
2c30: 74 20 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 28 t openDirectory(
2c40: 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 const char*, int
2c50: 2a 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 75 *);.static int u
2c60: 6e 69 78 47 65 74 70 61 67 65 73 69 7a 65 28 76 nixGetpagesize(v
2c70: 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e oid);../*.** Man
2c80: 79 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 61 y system calls a
2c90: 72 65 20 61 63 63 65 73 73 65 64 20 74 68 72 6f re accessed thro
2ca0: 75 67 68 20 70 6f 69 6e 74 65 72 2d 74 6f 2d 66 ugh pointer-to-f
2cb0: 75 6e 63 74 69 6f 6e 73 20 73 6f 20 74 68 61 74 unctions so that
2cc0: 0a 2a 2a 20 74 68 65 79 20 6d 61 79 20 62 65 20 .** they may be
2cd0: 6f 76 65 72 72 69 64 64 65 6e 20 61 74 20 72 75 overridden at ru
2ce0: 6e 74 69 6d 65 20 74 6f 20 66 61 63 69 6c 69 74 ntime to facilit
2cf0: 61 74 65 20 66 61 75 6c 74 20 69 6e 6a 65 63 74 ate fault inject
2d00: 69 6f 6e 20 64 75 72 69 6e 67 0a 2a 2a 20 74 65 ion during.** te
2d10: 73 74 69 6e 67 20 61 6e 64 20 73 61 6e 64 62 6f sting and sandbo
2d20: 78 69 6e 67 2e 20 20 54 68 65 20 66 6f 6c 6c 6f xing. The follo
2d30: 77 69 6e 67 20 61 72 72 61 79 20 68 6f 6c 64 73 wing array holds
2d40: 20 74 68 65 20 6e 61 6d 65 73 20 61 6e 64 20 70 the names and p
2d50: 6f 69 6e 74 65 72 73 0a 2a 2a 20 74 6f 20 61 6c ointers.** to al
2d60: 6c 20 6f 76 65 72 72 69 64 65 61 62 6c 65 20 73 l overrideable s
2d70: 79 73 74 65 6d 20 63 61 6c 6c 73 2e 0a 2a 2f 0a ystem calls..*/.
2d80: 73 74 61 74 69 63 20 73 74 72 75 63 74 20 75 6e static struct un
2d90: 69 78 5f 73 79 73 63 61 6c 6c 20 7b 0a 20 20 63 ix_syscall {. c
2da0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
2db0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
2dc0: 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 79 73 74 Name of the syst
2dd0: 65 6d 20 63 61 6c 6c 20 2a 2f 0a 20 20 73 71 6c em call */. sql
2de0: 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 ite3_syscall_ptr
2df0: 20 70 43 75 72 72 65 6e 74 3b 20 2f 2a 20 43 75 pCurrent; /* Cu
2e00: 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 rrent value of t
2e10: 68 65 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 2a he system call *
2e20: 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 73 63 /. sqlite3_sysc
2e30: 61 6c 6c 5f 70 74 72 20 70 44 65 66 61 75 6c 74 all_ptr pDefault
2e40: 3b 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c ; /* Default val
2e50: 75 65 20 2a 2f 0a 7d 20 61 53 79 73 63 61 6c 6c ue */.} aSyscall
2e60: 5b 5d 20 3d 20 7b 0a 20 20 7b 20 22 6f 70 65 6e [] = {. { "open
2e70: 22 2c 20 20 20 20 20 20 20 20 20 28 73 71 6c 69 ", (sqli
2e80: 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 te3_syscall_ptr)
2e90: 70 6f 73 69 78 4f 70 65 6e 2c 20 20 30 20 20 7d posixOpen, 0 }
2ea0: 2c 0a 23 64 65 66 69 6e 65 20 6f 73 4f 70 65 6e ,.#define osOpen
2eb0: 20 20 20 20 20 20 28 28 69 6e 74 28 2a 29 28 63 ((int(*)(c
2ec0: 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69 onst char*,int,i
2ed0: 6e 74 29 29 61 53 79 73 63 61 6c 6c 5b 30 5d 2e nt))aSyscall[0].
2ee0: 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20 22 pCurrent).. { "
2ef0: 63 6c 6f 73 65 22 2c 20 20 20 20 20 20 20 20 28 close", (
2f00: 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f sqlite3_syscall_
2f10: 70 74 72 29 63 6c 6f 73 65 2c 20 20 20 20 20 20 ptr)close,
2f20: 30 20 20 7d 2c 0a 23 64 65 66 69 6e 65 20 6f 73 0 },.#define os
2f30: 43 6c 6f 73 65 20 20 20 20 20 28 28 69 6e 74 28 Close ((int(
2f40: 2a 29 28 69 6e 74 29 29 61 53 79 73 63 61 6c 6c *)(int))aSyscall
2f50: 5b 31 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 [1].pCurrent)..
2f60: 20 7b 20 22 61 63 63 65 73 73 22 2c 20 20 20 20 { "access",
2f70: 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 (sqlite3_sysc
2f80: 61 6c 6c 5f 70 74 72 29 61 63 63 65 73 73 2c 20 all_ptr)access,
2f90: 20 20 20 20 30 20 20 7d 2c 0a 23 64 65 66 69 6e 0 },.#defin
2fa0: 65 20 6f 73 41 63 63 65 73 73 20 20 20 20 28 28 e osAccess ((
2fb0: 69 6e 74 28 2a 29 28 63 6f 6e 73 74 20 63 68 61 int(*)(const cha
2fc0: 72 2a 2c 69 6e 74 29 29 61 53 79 73 63 61 6c 6c r*,int))aSyscall
2fd0: 5b 32 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 [2].pCurrent)..
2fe0: 20 7b 20 22 67 65 74 63 77 64 22 2c 20 20 20 20 { "getcwd",
2ff0: 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 (sqlite3_sysc
3000: 61 6c 6c 5f 70 74 72 29 67 65 74 63 77 64 2c 20 all_ptr)getcwd,
3010: 20 20 20 20 30 20 20 7d 2c 0a 23 64 65 66 69 6e 0 },.#defin
3020: 65 20 6f 73 47 65 74 63 77 64 20 20 20 20 28 28 e osGetcwd ((
3030: 63 68 61 72 2a 28 2a 29 28 63 68 61 72 2a 2c 73 char*(*)(char*,s
3040: 69 7a 65 5f 74 29 29 61 53 79 73 63 61 6c 6c 5b ize_t))aSyscall[
3050: 33 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 3].pCurrent)..
3060: 7b 20 22 73 74 61 74 22 2c 20 20 20 20 20 20 20 { "stat",
3070: 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61 (sqlite3_sysca
3080: 6c 6c 5f 70 74 72 29 73 74 61 74 2c 20 20 20 20 ll_ptr)stat,
3090: 20 20 20 30 20 20 7d 2c 0a 23 64 65 66 69 6e 65 0 },.#define
30a0: 20 6f 73 53 74 61 74 20 20 20 20 20 20 28 28 69 osStat ((i
30b0: 6e 74 28 2a 29 28 63 6f 6e 73 74 20 63 68 61 72 nt(*)(const char
30c0: 2a 2c 73 74 72 75 63 74 20 73 74 61 74 2a 29 29 *,struct stat*))
30d0: 61 53 79 73 63 61 6c 6c 5b 34 5d 2e 70 43 75 72 aSyscall[4].pCur
30e0: 72 65 6e 74 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 rent)../*.** The
30f0: 20 44 4a 47 50 50 20 63 6f 6d 70 69 6c 65 72 20 DJGPP compiler
3100: 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b environment look
3110: 73 20 6d 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e s mostly like Un
3120: 69 78 2c 20 62 75 74 20 69 74 0a 2a 2a 20 6c 61 ix, but it.** la
3130: 63 6b 73 20 74 68 65 20 66 63 6e 74 6c 28 29 20 cks the fcntl()
3140: 73 79 73 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f system call. So
3150: 20 72 65 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 redefine fcntl(
3160: 29 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e ) to be somethin
3170: 67 0a 2a 2a 20 74 68 61 74 20 61 6c 77 61 79 73 g.** that always
3180: 20 73 75 63 63 65 65 64 73 2e 20 20 54 68 69 73 succeeds. This
3190: 20 6d 65 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b means that lock
31a0: 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 ing does not occ
31b0: 75 72 20 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 ur under.** DJGP
31c0: 50 2e 20 20 42 75 74 20 69 74 20 69 73 20 44 4f P. But it is DO
31d0: 53 20 2d 20 77 68 61 74 20 64 69 64 20 79 6f 75 S - what did you
31e0: 20 65 78 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 expect?.*/.#ifd
31f0: 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 20 20 7b ef __DJGPP__. {
3200: 20 22 66 73 74 61 74 22 2c 20 20 20 20 20 20 20 "fstat",
3210: 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
3220: 20 20 20 20 30 20 20 7d 2c 0a 23 64 65 66 69 6e 0 },.#defin
3230: 65 20 6f 73 46 73 74 61 74 28 61 2c 62 2c 63 29 e osFstat(a,b,c)
3240: 20 20 20 20 30 0a 23 65 6c 73 65 20 20 20 20 20 0.#else
3250: 0a 20 20 7b 20 22 66 73 74 61 74 22 2c 20 20 20 . { "fstat",
3260: 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 (sqlite3_sy
3270: 73 63 61 6c 6c 5f 70 74 72 29 66 73 74 61 74 2c scall_ptr)fstat,
3280: 20 20 20 20 20 20 30 20 20 7d 2c 0a 23 64 65 66 0 },.#def
3290: 69 6e 65 20 6f 73 46 73 74 61 74 20 20 20 20 20 ine osFstat
32a0: 28 28 69 6e 74 28 2a 29 28 69 6e 74 2c 73 74 72 ((int(*)(int,str
32b0: 75 63 74 20 73 74 61 74 2a 29 29 61 53 79 73 63 uct stat*))aSysc
32c0: 61 6c 6c 5b 35 5d 2e 70 43 75 72 72 65 6e 74 29 all[5].pCurrent)
32d0: 0a 23 65 6e 64 69 66 0a 0a 20 20 7b 20 22 66 74 .#endif.. { "ft
32e0: 72 75 6e 63 61 74 65 22 2c 20 20 20 20 28 73 71 runcate", (sq
32f0: 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 lite3_syscall_pt
3300: 72 29 66 74 72 75 6e 63 61 74 65 2c 20 20 30 20 r)ftruncate, 0
3310: 20 7d 2c 0a 23 64 65 66 69 6e 65 20 6f 73 46 74 },.#define osFt
3320: 72 75 6e 63 61 74 65 20 28 28 69 6e 74 28 2a 29 runcate ((int(*)
3330: 28 69 6e 74 2c 6f 66 66 5f 74 29 29 61 53 79 73 (int,off_t))aSys
3340: 63 61 6c 6c 5b 36 5d 2e 70 43 75 72 72 65 6e 74 call[6].pCurrent
3350: 29 0a 0a 20 20 7b 20 22 66 63 6e 74 6c 22 2c 20 ).. { "fcntl",
3360: 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f (sqlite3_
3370: 73 79 73 63 61 6c 6c 5f 70 74 72 29 66 63 6e 74 syscall_ptr)fcnt
3380: 6c 2c 20 20 20 20 20 20 30 20 20 7d 2c 0a 23 64 l, 0 },.#d
3390: 65 66 69 6e 65 20 6f 73 46 63 6e 74 6c 20 20 20 efine osFcntl
33a0: 20 20 28 28 69 6e 74 28 2a 29 28 69 6e 74 2c 69 ((int(*)(int,i
33b0: 6e 74 2c 2e 2e 2e 29 29 61 53 79 73 63 61 6c 6c nt,...))aSyscall
33c0: 5b 37 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 [7].pCurrent)..
33d0: 20 7b 20 22 72 65 61 64 22 2c 20 20 20 20 20 20 { "read",
33e0: 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 (sqlite3_sysc
33f0: 61 6c 6c 5f 70 74 72 29 72 65 61 64 2c 20 20 20 all_ptr)read,
3400: 20 20 20 20 30 20 20 7d 2c 0a 23 64 65 66 69 6e 0 },.#defin
3410: 65 20 6f 73 52 65 61 64 20 20 20 20 20 20 28 28 e osRead ((
3420: 73 73 69 7a 65 5f 74 28 2a 29 28 69 6e 74 2c 76 ssize_t(*)(int,v
3430: 6f 69 64 2a 2c 73 69 7a 65 5f 74 29 29 61 53 79 oid*,size_t))aSy
3440: 73 63 61 6c 6c 5b 38 5d 2e 70 43 75 72 72 65 6e scall[8].pCurren
3450: 74 29 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 t)..#if defined(
3460: 55 53 45 5f 50 52 45 41 44 29 20 7c 7c 20 53 51 USE_PREAD) || SQ
3470: 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
3480: 49 4e 47 5f 53 54 59 4c 45 0a 20 20 7b 20 22 70 ING_STYLE. { "p
3490: 72 65 61 64 22 2c 20 20 20 20 20 20 20 20 28 73 read", (s
34a0: 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 qlite3_syscall_p
34b0: 74 72 29 70 72 65 61 64 2c 20 20 20 20 20 20 30 tr)pread, 0
34c0: 20 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 },.#else. { "
34d0: 70 72 65 61 64 22 2c 20 20 20 20 20 20 20 20 28 pread", (
34e0: 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f sqlite3_syscall_
34f0: 70 74 72 29 30 2c 20 20 20 20 20 20 20 20 20 20 ptr)0,
3500: 30 20 20 7d 2c 0a 23 65 6e 64 69 66 0a 23 64 65 0 },.#endif.#de
3510: 66 69 6e 65 20 6f 73 50 72 65 61 64 20 20 20 20 fine osPread
3520: 20 28 28 73 73 69 7a 65 5f 74 28 2a 29 28 69 6e ((ssize_t(*)(in
3530: 74 2c 76 6f 69 64 2a 2c 73 69 7a 65 5f 74 2c 6f t,void*,size_t,o
3540: 66 66 5f 74 29 29 61 53 79 73 63 61 6c 6c 5b 39 ff_t))aSyscall[9
3550: 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 ].pCurrent)..#if
3560: 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 defined(USE_PRE
3570: 41 44 36 34 29 0a 20 20 7b 20 22 70 72 65 61 64 AD64). { "pread
3580: 36 34 22 2c 20 20 20 20 20 20 28 73 71 6c 69 74 64", (sqlit
3590: 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 70 e3_syscall_ptr)p
35a0: 72 65 61 64 36 34 2c 20 20 20 20 30 20 20 7d 2c read64, 0 },
35b0: 0a 23 65 6c 73 65 0a 20 20 7b 20 22 70 72 65 61 .#else. { "prea
35c0: 64 36 34 22 2c 20 20 20 20 20 20 28 73 71 6c 69 d64", (sqli
35d0: 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 te3_syscall_ptr)
35e0: 30 2c 20 20 20 20 20 20 20 20 20 20 30 20 20 7d 0, 0 }
35f0: 2c 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 ,.#endif.#define
3600: 20 6f 73 50 72 65 61 64 36 34 20 28 28 73 73 69 osPread64 ((ssi
3610: 7a 65 5f 74 28 2a 29 28 69 6e 74 2c 76 6f 69 64 ze_t(*)(int,void
3620: 2a 2c 73 69 7a 65 5f 74 2c 6f 66 66 36 34 5f 74 *,size_t,off64_t
3630: 29 29 61 53 79 73 63 61 6c 6c 5b 31 30 5d 2e 70 ))aSyscall[10].p
3640: 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20 22 77 Current).. { "w
3650: 72 69 74 65 22 2c 20 20 20 20 20 20 20 20 28 73 rite", (s
3660: 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 qlite3_syscall_p
3670: 74 72 29 77 72 69 74 65 2c 20 20 20 20 20 20 30 tr)write, 0
3680: 20 20 7d 2c 0a 23 64 65 66 69 6e 65 20 6f 73 57 },.#define osW
3690: 72 69 74 65 20 20 20 20 20 28 28 73 73 69 7a 65 rite ((ssize
36a0: 5f 74 28 2a 29 28 69 6e 74 2c 63 6f 6e 73 74 20 _t(*)(int,const
36b0: 76 6f 69 64 2a 2c 73 69 7a 65 5f 74 29 29 61 53 void*,size_t))aS
36c0: 79 73 63 61 6c 6c 5b 31 31 5d 2e 70 43 75 72 72 yscall[11].pCurr
36d0: 65 6e 74 29 0a 0a 23 69 66 20 64 65 66 69 6e 65 ent)..#if define
36e0: 64 28 55 53 45 5f 50 52 45 41 44 29 20 7c 7c 20 d(USE_PREAD) ||
36f0: 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
3700: 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 7b 20 CKING_STYLE. {
3710: 22 70 77 72 69 74 65 22 2c 20 20 20 20 20 20 20 "pwrite",
3720: 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c (sqlite3_syscall
3730: 5f 70 74 72 29 70 77 72 69 74 65 2c 20 20 20 20 _ptr)pwrite,
3740: 20 30 20 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 0 },.#else. {
3750: 20 22 70 77 72 69 74 65 22 2c 20 20 20 20 20 20 "pwrite",
3760: 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c (sqlite3_syscal
3770: 6c 5f 70 74 72 29 30 2c 20 20 20 20 20 20 20 20 l_ptr)0,
3780: 20 20 30 20 20 7d 2c 0a 23 65 6e 64 69 66 0a 23 0 },.#endif.#
3790: 64 65 66 69 6e 65 20 6f 73 50 77 72 69 74 65 20 define osPwrite
37a0: 20 20 20 28 28 73 73 69 7a 65 5f 74 28 2a 29 28 ((ssize_t(*)(
37b0: 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c int,const void*,
37c0: 73 69 7a 65 5f 74 2c 6f 66 66 5f 74 29 29 5c 0a size_t,off_t))\.
37d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
37e0: 20 20 20 20 61 53 79 73 63 61 6c 6c 5b 31 32 5d aSyscall[12]
37f0: 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 .pCurrent)..#if
3800: 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 defined(USE_PREA
3810: 44 36 34 29 0a 20 20 7b 20 22 70 77 72 69 74 65 D64). { "pwrite
3820: 36 34 22 2c 20 20 20 20 20 28 73 71 6c 69 74 65 64", (sqlite
3830: 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 70 77 3_syscall_ptr)pw
3840: 72 69 74 65 36 34 2c 20 20 20 30 20 20 7d 2c 0a rite64, 0 },.
3850: 23 65 6c 73 65 0a 20 20 7b 20 22 70 77 72 69 74 #else. { "pwrit
3860: 65 36 34 22 2c 20 20 20 20 20 28 73 71 6c 69 74 e64", (sqlit
3870: 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 30 e3_syscall_ptr)0
3880: 2c 20 20 20 20 20 20 20 20 20 20 30 20 20 7d 2c , 0 },
3890: 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 .#endif.#define
38a0: 6f 73 50 77 72 69 74 65 36 34 20 20 28 28 73 73 osPwrite64 ((ss
38b0: 69 7a 65 5f 74 28 2a 29 28 69 6e 74 2c 63 6f 6e ize_t(*)(int,con
38c0: 73 74 20 76 6f 69 64 2a 2c 73 69 7a 65 5f 74 2c st void*,size_t,
38d0: 6f 66 66 36 34 5f 74 29 29 5c 0a 20 20 20 20 20 off64_t))\.
38e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 a
38f0: 53 79 73 63 61 6c 6c 5b 31 33 5d 2e 70 43 75 72 Syscall[13].pCur
3900: 72 65 6e 74 29 0a 0a 20 20 7b 20 22 66 63 68 6d rent).. { "fchm
3910: 6f 64 22 2c 20 20 20 20 20 20 20 28 73 71 6c 69 od", (sqli
3920: 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 te3_syscall_ptr)
3930: 66 63 68 6d 6f 64 2c 20 20 20 20 20 20 20 20 20 fchmod,
3940: 20 30 20 20 7d 2c 0a 23 64 65 66 69 6e 65 20 6f 0 },.#define o
3950: 73 46 63 68 6d 6f 64 20 20 20 20 28 28 69 6e 74 sFchmod ((int
3960: 28 2a 29 28 69 6e 74 2c 6d 6f 64 65 5f 74 29 29 (*)(int,mode_t))
3970: 61 53 79 73 63 61 6c 6c 5b 31 34 5d 2e 70 43 75 aSyscall[14].pCu
3980: 72 72 65 6e 74 29 0a 0a 23 69 66 20 64 65 66 69 rrent)..#if defi
3990: 6e 65 64 28 48 41 56 45 5f 50 4f 53 49 58 5f 46 ned(HAVE_POSIX_F
39a0: 41 4c 4c 4f 43 41 54 45 29 20 26 26 20 48 41 56 ALLOCATE) && HAV
39b0: 45 5f 50 4f 53 49 58 5f 46 41 4c 4c 4f 43 41 54 E_POSIX_FALLOCAT
39c0: 45 0a 20 20 7b 20 22 66 61 6c 6c 6f 63 61 74 65 E. { "fallocate
39d0: 22 2c 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 ", (sqlite3_s
39e0: 79 73 63 61 6c 6c 5f 70 74 72 29 70 6f 73 69 78 yscall_ptr)posix
39f0: 5f 66 61 6c 6c 6f 63 61 74 65 2c 20 20 30 20 7d _fallocate, 0 }
3a00: 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 66 61 6c ,.#else. { "fal
3a10: 6c 6f 63 61 74 65 22 2c 20 20 20 20 28 73 71 6c locate", (sql
3a20: 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 ite3_syscall_ptr
3a30: 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 )0,
3a40: 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 23 0 },.#endif.#
3a50: 64 65 66 69 6e 65 20 6f 73 46 61 6c 6c 6f 63 61 define osFalloca
3a60: 74 65 20 28 28 69 6e 74 28 2a 29 28 69 6e 74 2c te ((int(*)(int,
3a70: 6f 66 66 5f 74 2c 6f 66 66 5f 74 29 29 61 53 79 off_t,off_t))aSy
3a80: 73 63 61 6c 6c 5b 31 35 5d 2e 70 43 75 72 72 65 scall[15].pCurre
3a90: 6e 74 29 0a 0a 20 20 7b 20 22 75 6e 6c 69 6e 6b nt).. { "unlink
3aa0: 22 2c 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 ", (sqlite
3ab0: 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 75 6e 3_syscall_ptr)un
3ac0: 6c 69 6e 6b 2c 20 20 20 20 20 20 20 20 20 20 20 link,
3ad0: 30 20 7d 2c 0a 23 64 65 66 69 6e 65 20 6f 73 55 0 },.#define osU
3ae0: 6e 6c 69 6e 6b 20 20 20 20 28 28 69 6e 74 28 2a nlink ((int(*
3af0: 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 29 61 )(const char*))a
3b00: 53 79 73 63 61 6c 6c 5b 31 36 5d 2e 70 43 75 72 Syscall[16].pCur
3b10: 72 65 6e 74 29 0a 0a 20 20 7b 20 22 6f 70 65 6e rent).. { "open
3b20: 44 69 72 65 63 74 6f 72 79 22 2c 20 20 20 20 28 Directory", (
3b30: 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f sqlite3_syscall_
3b40: 70 74 72 29 6f 70 65 6e 44 69 72 65 63 74 6f 72 ptr)openDirector
3b50: 79 2c 20 20 20 20 20 20 30 20 7d 2c 0a 23 64 65 y, 0 },.#de
3b60: 66 69 6e 65 20 6f 73 4f 70 65 6e 44 69 72 65 63 fine osOpenDirec
3b70: 74 6f 72 79 20 28 28 69 6e 74 28 2a 29 28 63 6f tory ((int(*)(co
3b80: 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2a 29 29 nst char*,int*))
3b90: 61 53 79 73 63 61 6c 6c 5b 31 37 5d 2e 70 43 75 aSyscall[17].pCu
3ba0: 72 72 65 6e 74 29 0a 0a 20 20 7b 20 22 6d 6b 64 rrent).. { "mkd
3bb0: 69 72 22 2c 20 20 20 20 20 20 20 20 28 73 71 6c ir", (sql
3bc0: 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 ite3_syscall_ptr
3bd0: 29 6d 6b 64 69 72 2c 20 20 20 20 20 20 20 20 20 )mkdir,
3be0: 20 20 30 20 7d 2c 0a 23 64 65 66 69 6e 65 20 6f 0 },.#define o
3bf0: 73 4d 6b 64 69 72 20 20 20 20 20 28 28 69 6e 74 sMkdir ((int
3c00: 28 2a 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c (*)(const char*,
3c10: 6d 6f 64 65 5f 74 29 29 61 53 79 73 63 61 6c 6c mode_t))aSyscall
3c20: 5b 31 38 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a [18].pCurrent)..
3c30: 20 20 7b 20 22 72 6d 64 69 72 22 2c 20 20 20 20 { "rmdir",
3c40: 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 (sqlite3_sys
3c50: 63 61 6c 6c 5f 70 74 72 29 72 6d 64 69 72 2c 20 call_ptr)rmdir,
3c60: 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 0 },.#
3c70: 64 65 66 69 6e 65 20 6f 73 52 6d 64 69 72 20 20 define osRmdir
3c80: 20 20 20 28 28 69 6e 74 28 2a 29 28 63 6f 6e 73 ((int(*)(cons
3c90: 74 20 63 68 61 72 2a 29 29 61 53 79 73 63 61 6c t char*))aSyscal
3ca0: 6c 5b 31 39 5d 2e 70 43 75 72 72 65 6e 74 29 0a l[19].pCurrent).
3cb0: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 48 41 56 .#if defined(HAV
3cc0: 45 5f 46 43 48 4f 57 4e 29 0a 20 20 7b 20 22 66 E_FCHOWN). { "f
3cd0: 63 68 6f 77 6e 22 2c 20 20 20 20 20 20 20 28 73 chown", (s
3ce0: 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 qlite3_syscall_p
3cf0: 74 72 29 66 63 68 6f 77 6e 2c 20 20 20 20 20 20 tr)fchown,
3d00: 20 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 0 },.#else.
3d10: 20 7b 20 22 66 63 68 6f 77 6e 22 2c 20 20 20 20 { "fchown",
3d20: 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 (sqlite3_sysc
3d30: 61 6c 6c 5f 70 74 72 29 30 2c 20 20 20 20 20 20 all_ptr)0,
3d40: 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 0 },.#e
3d50: 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 6f 73 46 ndif.#define osF
3d60: 63 68 6f 77 6e 20 20 20 20 28 28 69 6e 74 28 2a chown ((int(*
3d70: 29 28 69 6e 74 2c 75 69 64 5f 74 2c 67 69 64 5f )(int,uid_t,gid_
3d80: 74 29 29 61 53 79 73 63 61 6c 6c 5b 32 30 5d 2e t))aSyscall[20].
3d90: 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20 22 pCurrent).. { "
3da0: 67 65 74 65 75 69 64 22 2c 20 20 20 20 20 20 28 geteuid", (
3db0: 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f sqlite3_syscall_
3dc0: 70 74 72 29 67 65 74 65 75 69 64 2c 20 20 20 20 ptr)geteuid,
3dd0: 20 20 20 20 20 30 20 7d 2c 0a 23 64 65 66 69 6e 0 },.#defin
3de0: 65 20 6f 73 47 65 74 65 75 69 64 20 20 20 28 28 e osGeteuid ((
3df0: 75 69 64 5f 74 28 2a 29 28 76 6f 69 64 29 29 61 uid_t(*)(void))a
3e00: 53 79 73 63 61 6c 6c 5b 32 31 5d 2e 70 43 75 72 Syscall[21].pCur
3e10: 72 65 6e 74 29 0a 0a 23 69 66 20 21 64 65 66 69 rent)..#if !defi
3e20: 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
3e30: 57 41 4c 29 20 7c 7c 20 53 51 4c 49 54 45 5f 4d WAL) || SQLITE_M
3e40: 41 58 5f 4d 4d 41 50 5f 53 49 5a 45 3e 30 0a 20 AX_MMAP_SIZE>0.
3e50: 20 7b 20 22 6d 6d 61 70 22 2c 20 20 20 20 20 20 { "mmap",
3e60: 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 (sqlite3_sysc
3e70: 61 6c 6c 5f 70 74 72 29 6d 6d 61 70 2c 20 20 20 all_ptr)mmap,
3e80: 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 0 },.#e
3e90: 6c 73 65 0a 20 20 7b 20 22 6d 6d 61 70 22 2c 20 lse. { "mmap",
3ea0: 20 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 (sqlite3
3eb0: 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 30 2c 20 _syscall_ptr)0,
3ec0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 0
3ed0: 7d 2c 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e },.#endif.#defin
3ee0: 65 20 6f 73 4d 6d 61 70 20 28 28 76 6f 69 64 2a e osMmap ((void*
3ef0: 28 2a 29 28 76 6f 69 64 2a 2c 73 69 7a 65 5f 74 (*)(void*,size_t
3f00: 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 6f 66 66 ,int,int,int,off
3f10: 5f 74 29 29 61 53 79 73 63 61 6c 6c 5b 32 32 5d _t))aSyscall[22]
3f20: 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 .pCurrent)..#if
3f30: 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
3f40: 4f 4d 49 54 5f 57 41 4c 29 20 7c 7c 20 53 51 4c OMIT_WAL) || SQL
3f50: 49 54 45 5f 4d 41 58 5f 4d 4d 41 50 5f 53 49 5a ITE_MAX_MMAP_SIZ
3f60: 45 3e 30 0a 20 20 7b 20 22 6d 75 6e 6d 61 70 22 E>0. { "munmap"
3f70: 2c 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 , (sqlite3
3f80: 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 6d 75 6e _syscall_ptr)mun
3f90: 6d 61 70 2c 20 20 20 20 20 20 20 20 20 20 30 20 map, 0
3fa0: 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 6d 75 },.#else. { "mu
3fb0: 6e 6d 61 70 22 2c 20 20 20 20 20 20 20 28 73 71 nmap", (sq
3fc0: 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 lite3_syscall_pt
3fd0: 72 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 r)0,
3fe0: 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 23 0 },.#endif.#
3ff0: 64 65 66 69 6e 65 20 6f 73 4d 75 6e 6d 61 70 20 define osMunmap
4000: 28 28 76 6f 69 64 2a 28 2a 29 28 76 6f 69 64 2a ((void*(*)(void*
4010: 2c 73 69 7a 65 5f 74 29 29 61 53 79 73 63 61 6c ,size_t))aSyscal
4020: 6c 5b 32 33 5d 2e 70 43 75 72 72 65 6e 74 29 0a l[23].pCurrent).
4030: 0a 23 69 66 20 48 41 56 45 5f 4d 52 45 4d 41 50 .#if HAVE_MREMAP
4040: 20 26 26 20 28 21 64 65 66 69 6e 65 64 28 53 51 && (!defined(SQ
4050: 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 29 20 7c LITE_OMIT_WAL) |
4060: 7c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4d 4d 41 | SQLITE_MAX_MMA
4070: 50 5f 53 49 5a 45 3e 30 29 0a 20 20 7b 20 22 6d P_SIZE>0). { "m
4080: 72 65 6d 61 70 22 2c 20 20 20 20 20 20 20 28 73 remap", (s
4090: 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 qlite3_syscall_p
40a0: 74 72 29 6d 72 65 6d 61 70 2c 20 20 20 20 20 20 tr)mremap,
40b0: 20 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 0 },.#else.
40c0: 20 7b 20 22 6d 72 65 6d 61 70 22 2c 20 20 20 20 { "mremap",
40d0: 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 (sqlite3_sysc
40e0: 61 6c 6c 5f 70 74 72 29 30 2c 20 20 20 20 20 20 all_ptr)0,
40f0: 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 0 },.#e
4100: 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 6f 73 4d ndif.#define osM
4110: 72 65 6d 61 70 20 28 28 76 6f 69 64 2a 28 2a 29 remap ((void*(*)
4120: 28 76 6f 69 64 2a 2c 73 69 7a 65 5f 74 2c 73 69 (void*,size_t,si
4130: 7a 65 5f 74 2c 69 6e 74 2c 2e 2e 2e 29 29 61 53 ze_t,int,...))aS
4140: 79 73 63 61 6c 6c 5b 32 34 5d 2e 70 43 75 72 72 yscall[24].pCurr
4150: 65 6e 74 29 0a 0a 23 69 66 20 21 64 65 66 69 6e ent)..#if !defin
4160: 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 ed(SQLITE_OMIT_W
4170: 41 4c 29 20 7c 7c 20 53 51 4c 49 54 45 5f 4d 41 AL) || SQLITE_MA
4180: 58 5f 4d 4d 41 50 5f 53 49 5a 45 3e 30 0a 20 20 X_MMAP_SIZE>0.
4190: 7b 20 22 67 65 74 70 61 67 65 73 69 7a 65 22 2c { "getpagesize",
41a0: 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61 (sqlite3_sysca
41b0: 6c 6c 5f 70 74 72 29 75 6e 69 78 47 65 74 70 61 ll_ptr)unixGetpa
41c0: 67 65 73 69 7a 65 2c 20 30 20 7d 2c 0a 23 65 6c gesize, 0 },.#el
41d0: 73 65 0a 20 20 7b 20 22 67 65 74 70 61 67 65 73 se. { "getpages
41e0: 69 7a 65 22 2c 20 20 28 73 71 6c 69 74 65 33 5f ize", (sqlite3_
41f0: 73 79 73 63 61 6c 6c 5f 70 74 72 29 30 2c 20 20 syscall_ptr)0,
4200: 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 0 }
4210: 2c 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 ,.#endif.#define
4220: 20 6f 73 47 65 74 70 61 67 65 73 69 7a 65 20 28 osGetpagesize (
4230: 28 69 6e 74 28 2a 29 28 76 6f 69 64 29 29 61 53 (int(*)(void))aS
4240: 79 73 63 61 6c 6c 5b 32 35 5d 2e 70 43 75 72 72 yscall[25].pCurr
4250: 65 6e 74 29 0a 0a 23 69 66 20 64 65 66 69 6e 65 ent)..#if define
4260: 64 28 48 41 56 45 5f 52 45 41 44 4c 49 4e 4b 29 d(HAVE_READLINK)
4270: 0a 20 20 7b 20 22 72 65 61 64 6c 69 6e 6b 22 2c . { "readlink",
4280: 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 (sqlite3_sy
4290: 73 63 61 6c 6c 5f 70 74 72 29 72 65 61 64 6c 69 scall_ptr)readli
42a0: 6e 6b 2c 20 20 20 20 20 20 20 20 30 20 7d 2c 0a nk, 0 },.
42b0: 23 65 6c 73 65 0a 20 20 7b 20 22 72 65 61 64 6c #else. { "readl
42c0: 69 6e 6b 22 2c 20 20 20 20 20 28 73 71 6c 69 74 ink", (sqlit
42d0: 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 30 e3_syscall_ptr)0
42e0: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
42f0: 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 23 64 65 66 0 },.#endif.#def
4300: 69 6e 65 20 6f 73 52 65 61 64 6c 69 6e 6b 20 28 ine osReadlink (
4310: 28 73 73 69 7a 65 5f 74 28 2a 29 28 63 6f 6e 73 (ssize_t(*)(cons
4320: 74 20 63 68 61 72 2a 2c 63 68 61 72 2a 2c 73 69 t char*,char*,si
4330: 7a 65 5f 74 29 29 61 53 79 73 63 61 6c 6c 5b 32 ze_t))aSyscall[2
4340: 36 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 6].pCurrent)..#i
4350: 66 20 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c f defined(HAVE_L
4360: 53 54 41 54 29 0a 20 20 7b 20 22 6c 73 74 61 74 STAT). { "lstat
4370: 22 2c 20 20 20 20 20 20 20 20 20 28 73 71 6c 69 ", (sqli
4380: 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 te3_syscall_ptr)
4390: 6c 73 74 61 74 2c 20 20 20 20 20 20 20 20 20 20 lstat,
43a0: 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 0 },.#else. { "
43b0: 6c 73 74 61 74 22 2c 20 20 20 20 20 20 20 20 20 lstat",
43c0: 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c (sqlite3_syscall
43d0: 5f 70 74 72 29 30 2c 20 20 20 20 20 20 20 20 20 _ptr)0,
43e0: 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0 },.#endif
43f0: 0a 23 64 65 66 69 6e 65 20 6f 73 4c 73 74 61 74 .#define osLstat
4400: 20 20 20 20 20 20 28 28 69 6e 74 28 2a 29 28 63 ((int(*)(c
4410: 6f 6e 73 74 20 63 68 61 72 2a 2c 73 74 72 75 63 onst char*,struc
4420: 74 20 73 74 61 74 2a 29 29 61 53 79 73 63 61 6c t stat*))aSyscal
4430: 6c 5b 32 37 5d 2e 70 43 75 72 72 65 6e 74 29 0a l[27].pCurrent).
4440: 0a 7d 3b 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68 .}; /* End of th
4450: 65 20 6f 76 65 72 72 69 64 65 61 62 6c 65 20 73 e overrideable s
4460: 79 73 74 65 6d 20 63 61 6c 6c 73 20 2a 2f 0a 0a ystem calls */..
4470: 0a 2f 2a 0a 2a 2a 20 4f 6e 20 73 6f 6d 65 20 73 ./*.** On some s
4480: 79 73 74 65 6d 73 2c 20 63 61 6c 6c 73 20 74 6f ystems, calls to
4490: 20 66 63 68 6f 77 6e 28 29 20 77 69 6c 6c 20 74 fchown() will t
44a0: 72 69 67 67 65 72 20 61 20 6d 65 73 73 61 67 65 rigger a message
44b0: 20 69 6e 20 61 20 73 65 63 75 72 69 74 79 0a 2a in a security.*
44c0: 2a 20 6c 6f 67 20 69 66 20 74 68 65 79 20 63 6f * log if they co
44d0: 6d 65 20 66 72 6f 6d 20 6e 6f 6e 2d 72 6f 6f 74 me from non-root
44e0: 20 70 72 6f 63 65 73 73 65 73 2e 20 20 53 6f 20 processes. So
44f0: 61 76 6f 69 64 20 63 61 6c 6c 69 6e 67 20 66 63 avoid calling fc
4500: 68 6f 77 6e 28 29 20 69 66 0a 2a 2a 20 77 65 20 hown() if.** we
4510: 61 72 65 20 6e 6f 74 20 72 75 6e 6e 69 6e 67 20 are not running
4520: 61 73 20 72 6f 6f 74 2e 0a 2a 2f 0a 73 74 61 74 as root..*/.stat
4530: 69 63 20 69 6e 74 20 72 6f 62 75 73 74 46 63 68 ic int robustFch
4540: 6f 77 6e 28 69 6e 74 20 66 64 2c 20 75 69 64 5f own(int fd, uid_
4550: 74 20 75 69 64 2c 20 67 69 64 5f 74 20 67 69 64 t uid, gid_t gid
4560: 29 7b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 48 ){.#if defined(H
4570: 41 56 45 5f 46 43 48 4f 57 4e 29 0a 20 20 72 65 AVE_FCHOWN). re
4580: 74 75 72 6e 20 6f 73 47 65 74 65 75 69 64 28 29 turn osGeteuid()
4590: 20 3f 20 30 20 3a 20 6f 73 46 63 68 6f 77 6e 28 ? 0 : osFchown(
45a0: 66 64 2c 75 69 64 2c 67 69 64 29 3b 0a 23 65 6c fd,uid,gid);.#el
45b0: 73 65 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 23 se. return 0;.#
45c0: 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 endif.}../*.** T
45d0: 68 69 73 20 69 73 20 74 68 65 20 78 53 65 74 53 his is the xSetS
45e0: 79 73 74 65 6d 43 61 6c 6c 28 29 20 6d 65 74 68 ystemCall() meth
45f0: 6f 64 20 6f 66 20 73 71 6c 69 74 65 33 5f 76 66 od of sqlite3_vf
4600: 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 s for all of the
4610: 0a 2a 2a 20 22 75 6e 69 78 22 20 56 46 53 65 73 .** "unix" VFSes
4620: 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 . Return SQLITE
4630: 5f 4f 4b 20 6f 70 6f 6e 20 73 75 63 63 65 73 73 _OK opon success
4640: 66 75 6c 6c 79 20 75 70 64 61 74 69 6e 67 20 74 fully updating t
4650: 68 65 0a 2a 2a 20 73 79 73 74 65 6d 20 63 61 6c he.** system cal
4660: 6c 20 70 6f 69 6e 74 65 72 2c 20 6f 72 20 53 51 l pointer, or SQ
4670: 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 69 66 LITE_NOTFOUND if
4680: 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e there is no con
4690: 66 69 67 75 72 61 62 6c 65 0a 2a 2a 20 73 79 73 figurable.** sys
46a0: 74 65 6d 20 63 61 6c 6c 20 6e 61 6d 65 64 20 7a tem call named z
46b0: 4e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 Name..*/.static
46c0: 69 6e 74 20 75 6e 69 78 53 65 74 53 79 73 74 65 int unixSetSyste
46d0: 6d 43 61 6c 6c 28 0a 20 20 73 71 6c 69 74 65 33 mCall(. sqlite3
46e0: 5f 76 66 73 20 2a 70 4e 6f 74 55 73 65 64 2c 20 _vfs *pNotUsed,
46f0: 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 /* The VF
4700: 53 20 70 6f 69 6e 74 65 72 2e 20 20 4e 6f 74 20 S pointer. Not
4710: 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 used */. const
4720: 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 char *zName,
4730: 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
4740: 6f 66 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 74 of system call t
4750: 6f 20 6f 76 65 72 72 69 64 65 20 2a 2f 0a 20 20 o override */.
4760: 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f sqlite3_syscall_
4770: 70 74 72 20 70 4e 65 77 46 75 6e 63 20 20 2f 2a ptr pNewFunc /*
4780: 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20 Pointer to new
4790: 73 79 73 74 65 6d 20 63 61 6c 6c 20 76 61 6c 75 system call valu
47a0: 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e e */.){. unsign
47b0: 65 64 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 ed int i;. int
47c0: 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 54 46 rc = SQLITE_NOTF
47d0: 4f 55 4e 44 3b 0a 0a 20 20 55 4e 55 53 45 44 5f OUND;.. UNUSED_
47e0: 50 41 52 41 4d 45 54 45 52 28 70 4e 6f 74 55 73 PARAMETER(pNotUs
47f0: 65 64 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 ed);. if( zName
4800: 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 ==0 ){. /* If
4810: 20 6e 6f 20 7a 4e 61 6d 65 20 69 73 20 67 69 76 no zName is giv
4820: 65 6e 2c 20 72 65 73 74 6f 72 65 20 61 6c 6c 20 en, restore all
4830: 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 74 6f 20 system calls to
4840: 74 68 65 69 72 20 64 65 66 61 75 6c 74 0a 20 20 their default.
4850: 20 20 2a 2a 20 73 65 74 74 69 6e 67 73 20 61 6e ** settings an
4860: 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 0a 20 20 d return NULL.
4870: 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 */. rc = SQ
4880: 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 66 6f 72 LITE_OK;. for
4890: 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 (i=0; i<sizeof(a
48a0: 53 79 73 63 61 6c 6c 29 2f 73 69 7a 65 6f 66 28 Syscall)/sizeof(
48b0: 61 53 79 73 63 61 6c 6c 5b 30 5d 29 3b 20 69 2b aSyscall[0]); i+
48c0: 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 53 +){. if( aS
48d0: 79 73 63 61 6c 6c 5b 69 5d 2e 70 44 65 66 61 75 yscall[i].pDefau
48e0: 6c 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 53 lt ){. aS
48f0: 79 73 63 61 6c 6c 5b 69 5d 2e 70 43 75 72 72 65 yscall[i].pCurre
4900: 6e 74 20 3d 20 61 53 79 73 63 61 6c 6c 5b 69 5d nt = aSyscall[i]
4910: 2e 70 44 65 66 61 75 6c 74 3b 0a 20 20 20 20 20 .pDefault;.
4920: 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 }. }. }else
4930: 7b 0a 20 20 20 20 2f 2a 20 49 66 20 7a 4e 61 6d {. /* If zNam
4940: 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 e is specified,
4950: 6f 70 65 72 61 74 65 20 6f 6e 20 6f 6e 6c 79 20 operate on only
4960: 74 68 65 20 6f 6e 65 20 73 79 73 74 65 6d 20 63 the one system c
4970: 61 6c 6c 0a 20 20 20 20 2a 2a 20 73 70 65 63 69 all. ** speci
4980: 66 69 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 fied.. */.
4990: 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 for(i=0; i<size
49a0: 6f 66 28 61 53 79 73 63 61 6c 6c 29 2f 73 69 7a of(aSyscall)/siz
49b0: 65 6f 66 28 61 53 79 73 63 61 6c 6c 5b 30 5d 29 eof(aSyscall[0])
49c0: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; i++){. if
49d0: 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 ( strcmp(zName,
49e0: 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 7a 4e 61 6d aSyscall[i].zNam
49f0: 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 e)==0 ){.
4a00: 20 69 66 28 20 61 53 79 73 63 61 6c 6c 5b 69 5d if( aSyscall[i]
4a10: 2e 70 44 65 66 61 75 6c 74 3d 3d 30 20 29 7b 0a .pDefault==0 ){.
4a20: 20 20 20 20 20 20 20 20 20 20 61 53 79 73 63 61 aSysca
4a30: 6c 6c 5b 69 5d 2e 70 44 65 66 61 75 6c 74 20 3d ll[i].pDefault =
4a40: 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 70 43 75 aSyscall[i].pCu
4a50: 72 72 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 7d rrent;. }
4a60: 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
4a70: 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 LITE_OK;.
4a80: 20 69 66 28 20 70 4e 65 77 46 75 6e 63 3d 3d 30 if( pNewFunc==0
4a90: 20 29 20 70 4e 65 77 46 75 6e 63 20 3d 20 61 53 ) pNewFunc = aS
4aa0: 79 73 63 61 6c 6c 5b 69 5d 2e 70 44 65 66 61 75 yscall[i].pDefau
4ab0: 6c 74 3b 0a 20 20 20 20 20 20 20 20 61 53 79 73 lt;. aSys
4ac0: 63 61 6c 6c 5b 69 5d 2e 70 43 75 72 72 65 6e 74 call[i].pCurrent
4ad0: 20 3d 20 70 4e 65 77 46 75 6e 63 3b 0a 20 20 20 = pNewFunc;.
4ae0: 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
4af0: 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
4b00: 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
4b10: 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 .** Return the v
4b20: 61 6c 75 65 20 6f 66 20 61 20 73 79 73 74 65 6d alue of a system
4b30: 20 63 61 6c 6c 2e 20 20 52 65 74 75 72 6e 20 4e call. Return N
4b40: 55 4c 4c 20 69 66 20 7a 4e 61 6d 65 20 69 73 20 ULL if zName is
4b50: 6e 6f 74 20 61 0a 2a 2a 20 72 65 63 6f 67 6e 69 not a.** recogni
4b60: 7a 65 64 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 zed system call
4b70: 6e 61 6d 65 2e 20 20 4e 55 4c 4c 20 69 73 20 61 name. NULL is a
4b80: 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66 20 lso returned if
4b90: 74 68 65 20 73 79 73 74 65 6d 20 63 61 6c 6c 0a the system call.
4ba0: 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 ** is currently
4bb0: 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 undefined..*/.st
4bc0: 61 74 69 63 20 73 71 6c 69 74 65 33 5f 73 79 73 atic sqlite3_sys
4bd0: 63 61 6c 6c 5f 70 74 72 20 75 6e 69 78 47 65 74 call_ptr unixGet
4be0: 53 79 73 74 65 6d 43 61 6c 6c 28 0a 20 20 73 71 SystemCall(. sq
4bf0: 6c 69 74 65 33 5f 76 66 73 20 2a 70 4e 6f 74 55 lite3_vfs *pNotU
4c00: 73 65 64 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 sed,. const cha
4c10: 72 20 2a 7a 4e 61 6d 65 0a 29 7b 0a 20 20 75 6e r *zName.){. un
4c20: 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 0a 20 signed int i;..
4c30: 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
4c40: 52 28 70 4e 6f 74 55 73 65 64 29 3b 0a 20 20 66 R(pNotUsed);. f
4c50: 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 or(i=0; i<sizeof
4c60: 28 61 53 79 73 63 61 6c 6c 29 2f 73 69 7a 65 6f (aSyscall)/sizeo
4c70: 66 28 61 53 79 73 63 61 6c 6c 5b 30 5d 29 3b 20 f(aSyscall[0]);
4c80: 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 74 i++){. if( st
4c90: 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 61 53 79 73 rcmp(zName, aSys
4ca0: 63 61 6c 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d call[i].zName)==
4cb0: 30 20 29 20 72 65 74 75 72 6e 20 61 53 79 73 63 0 ) return aSysc
4cc0: 61 6c 6c 5b 69 5d 2e 70 43 75 72 72 65 6e 74 3b all[i].pCurrent;
4cd0: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
4ce0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
4cf0: 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
4d00: 20 66 69 72 73 74 20 73 79 73 74 65 6d 20 63 61 first system ca
4d10: 6c 6c 20 61 66 74 65 72 20 7a 4e 61 6d 65 2e 20 ll after zName.
4d20: 20 49 66 20 7a 4e 61 6d 65 3d 3d 4e 55 4c 4c 0a If zName==NULL.
4d30: 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74 ** then return t
4d40: 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 he name of the f
4d50: 69 72 73 74 20 73 79 73 74 65 6d 20 63 61 6c 6c irst system call
4d60: 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 . Return NULL i
4d70: 66 20 7a 4e 61 6d 65 0a 2a 2a 20 69 73 20 74 68 f zName.** is th
4d80: 65 20 6c 61 73 74 20 73 79 73 74 65 6d 20 63 61 e last system ca
4d90: 6c 6c 20 6f 72 20 69 66 20 7a 4e 61 6d 65 20 69 ll or if zName i
4da0: 73 20 6e 6f 74 20 74 68 65 20 6e 61 6d 65 20 6f s not the name o
4db0: 66 20 61 20 76 61 6c 69 64 0a 2a 2a 20 73 79 73 f a valid.** sys
4dc0: 74 65 6d 20 63 61 6c 6c 2e 0a 2a 2f 0a 73 74 61 tem call..*/.sta
4dd0: 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tic const char *
4de0: 75 6e 69 78 4e 65 78 74 53 79 73 74 65 6d 43 61 unixNextSystemCa
4df0: 6c 6c 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ll(sqlite3_vfs *
4e00: 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a p, const char *z
4e10: 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69 20 3d Name){. int i =
4e20: 20 2d 31 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 -1;.. UNUSED_P
4e30: 41 52 41 4d 45 54 45 52 28 70 29 3b 0a 20 20 69 ARAMETER(p);. i
4e40: 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 f( zName ){.
4e50: 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 for(i=0; i<Array
4e60: 53 69 7a 65 28 61 53 79 73 63 61 6c 6c 29 2d 31 Size(aSyscall)-1
4e70: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; i++){. if
4e80: 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 ( strcmp(zName,
4e90: 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 7a 4e 61 6d aSyscall[i].zNam
4ea0: 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 e)==0 ) break;.
4eb0: 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 }. }. for(i
4ec0: 2b 2b 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 ++; i<ArraySize(
4ed0: 61 53 79 73 63 61 6c 6c 29 3b 20 69 2b 2b 29 7b aSyscall); i++){
4ee0: 0a 20 20 20 20 69 66 28 20 61 53 79 73 63 61 6c . if( aSyscal
4ef0: 6c 5b 69 5d 2e 70 43 75 72 72 65 6e 74 21 3d 30 l[i].pCurrent!=0
4f00: 20 29 20 72 65 74 75 72 6e 20 61 53 79 73 63 61 ) return aSysca
4f10: 6c 6c 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d ll[i].zName;. }
4f20: 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
4f30: 2f 2a 0a 2a 2a 20 44 6f 20 6e 6f 74 20 61 63 63 /*.** Do not acc
4f40: 65 70 74 20 61 6e 79 20 66 69 6c 65 20 64 65 73 ept any file des
4f50: 63 72 69 70 74 6f 72 20 6c 65 73 73 20 74 68 61 criptor less tha
4f60: 6e 20 74 68 69 73 20 76 61 6c 75 65 2c 20 69 6e n this value, in
4f70: 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 64 0a order to avoid.
4f80: 2a 2a 20 6f 70 65 6e 69 6e 67 20 64 61 74 61 62 ** opening datab
4f90: 61 73 65 20 66 69 6c 65 20 75 73 69 6e 67 20 66 ase file using f
4fa0: 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 ile descriptors
4fb0: 74 68 61 74 20 61 72 65 20 63 6f 6d 6d 6f 6e 6c that are commonl
4fc0: 79 20 75 73 65 64 20 66 6f 72 20 0a 2a 2a 20 73 y used for .** s
4fd0: 74 61 6e 64 61 72 64 20 69 6e 70 75 74 2c 20 6f tandard input, o
4fe0: 75 74 70 75 74 2c 20 61 6e 64 20 65 72 72 6f 72 utput, and error
4ff0: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
5000: 49 54 45 5f 4d 49 4e 49 4d 55 4d 5f 46 49 4c 45 ITE_MINIMUM_FILE
5010: 5f 44 45 53 43 52 49 50 54 4f 52 0a 23 20 64 65 _DESCRIPTOR.# de
5020: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 49 4e 49 fine SQLITE_MINI
5030: 4d 55 4d 5f 46 49 4c 45 5f 44 45 53 43 52 49 50 MUM_FILE_DESCRIP
5040: 54 4f 52 20 33 0a 23 65 6e 64 69 66 0a 0a 2f 2a TOR 3.#endif../*
5050: 0a 2a 2a 20 49 6e 76 6f 6b 65 20 6f 70 65 6e 28 .** Invoke open(
5060: 29 2e 20 20 44 6f 20 73 6f 20 6d 75 6c 74 69 70 ). Do so multip
5070: 6c 65 20 74 69 6d 65 73 2c 20 75 6e 74 69 6c 20 le times, until
5080: 69 74 20 65 69 74 68 65 72 20 73 75 63 63 65 65 it either succee
5090: 64 73 20 6f 72 0a 2a 2a 20 66 61 69 6c 73 20 66 ds or.** fails f
50a0: 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 20 6f or some reason o
50b0: 74 68 65 72 20 74 68 61 6e 20 45 49 4e 54 52 2e ther than EINTR.
50c0: 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 .**.** If the fi
50d0: 6c 65 20 63 72 65 61 74 69 6f 6e 20 6d 6f 64 65 le creation mode
50e0: 20 22 6d 22 20 69 73 20 30 20 74 68 65 6e 20 73 "m" is 0 then s
50f0: 65 74 20 69 74 20 74 6f 20 74 68 65 20 64 65 66 et it to the def
5100: 61 75 6c 74 20 66 6f 72 0a 2a 2a 20 53 51 4c 69 ault for.** SQLi
5110: 74 65 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 te. The default
5120: 20 69 73 20 53 51 4c 49 54 45 5f 44 45 46 41 55 is SQLITE_DEFAU
5130: 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 LT_FILE_PERMISSI
5140: 4f 4e 53 20 28 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a ONS (normally.**
5150: 20 30 36 34 34 29 20 61 73 20 6d 6f 64 69 66 69 0644) as modifi
5160: 65 64 20 62 79 20 74 68 65 20 73 79 73 74 65 6d ed by the system
5170: 20 75 6d 61 73 6b 2e 20 20 49 66 20 6d 20 69 73 umask. If m is
5180: 20 6e 6f 74 20 30 2c 20 74 68 65 6e 0a 2a 2a 20 not 0, then.**
5190: 6d 61 6b 65 20 74 68 65 20 66 69 6c 65 20 63 72 make the file cr
51a0: 65 61 74 69 6f 6e 20 6d 6f 64 65 20 62 65 20 65 eation mode be e
51b0: 78 61 63 74 6c 79 20 6d 20 69 67 6e 6f 72 69 6e xactly m ignorin
51c0: 67 20 74 68 65 20 75 6d 61 73 6b 2e 0a 2a 2a 0a g the umask..**.
51d0: 2a 2a 20 54 68 65 20 6d 20 70 61 72 61 6d 65 74 ** The m paramet
51e0: 65 72 20 77 69 6c 6c 20 62 65 20 6e 6f 6e 2d 7a er will be non-z
51f0: 65 72 6f 20 6f 6e 6c 79 20 77 68 65 6e 20 63 72 ero only when cr
5200: 65 61 74 69 6e 67 20 2d 77 61 6c 2c 20 2d 6a 6f eating -wal, -jo
5210: 75 72 6e 61 6c 2c 0a 2a 2a 20 61 6e 64 20 2d 73 urnal,.** and -s
5220: 68 6d 20 66 69 6c 65 73 2e 20 20 57 65 20 77 61 hm files. We wa
5230: 6e 74 20 74 68 6f 73 65 20 66 69 6c 65 73 20 74 nt those files t
5240: 6f 20 68 61 76 65 20 2a 65 78 61 63 74 6c 79 2a o have *exactly*
5250: 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 70 65 72 the same.** per
5260: 6d 69 73 73 69 6f 6e 73 20 61 73 20 74 68 65 69 missions as thei
5270: 72 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 r original datab
5280: 61 73 65 2c 20 75 6e 61 64 75 6c 74 65 72 61 74 ase, unadulterat
5290: 65 64 20 62 79 20 74 68 65 20 75 6d 61 73 6b 2e ed by the umask.
52a0: 0a 2a 2a 20 49 6e 20 74 68 61 74 20 77 61 79 2c .** In that way,
52b0: 20 69 66 20 61 20 64 61 74 61 62 61 73 65 20 66 if a database f
52c0: 69 6c 65 20 69 73 20 2d 72 77 2d 72 77 2d 72 77 ile is -rw-rw-rw
52d0: 20 6f 72 20 2d 72 77 2d 72 77 2d 72 2d 2c 20 61 or -rw-rw-r-, a
52e0: 6e 64 20 61 0a 2a 2a 20 74 72 61 6e 73 61 63 74 nd a.** transact
52f0: 69 6f 6e 20 63 72 61 73 68 65 73 20 61 6e 64 20 ion crashes and
5300: 6c 65 61 76 65 73 20 62 65 68 69 6e 64 20 68 6f leaves behind ho
5310: 74 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 6e t journals, then
5320: 20 61 6e 79 0a 2a 2a 20 70 72 6f 63 65 73 73 20 any.** process
5330: 74 68 61 74 20 69 73 20 61 62 6c 65 20 74 6f 20 that is able to
5340: 77 72 69 74 65 20 74 6f 20 74 68 65 20 64 61 74 write to the dat
5350: 61 62 61 73 65 20 77 69 6c 6c 20 61 6c 73 6f 20 abase will also
5360: 62 65 20 61 62 6c 65 20 74 6f 0a 2a 2a 20 72 65 be able to.** re
5370: 63 6f 76 65 72 20 74 68 65 20 68 6f 74 20 6a 6f cover the hot jo
5380: 75 72 6e 61 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 urnals..*/.stati
5390: 63 20 69 6e 74 20 72 6f 62 75 73 74 5f 6f 70 65 c int robust_ope
53a0: 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c n(const char *z,
53b0: 20 69 6e 74 20 66 2c 20 6d 6f 64 65 5f 74 20 6d int f, mode_t m
53c0: 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 6d ){. int fd;. m
53d0: 6f 64 65 5f 74 20 6d 32 20 3d 20 6d 20 3f 20 6d ode_t m2 = m ? m
53e0: 20 3a 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c : SQLITE_DEFAUL
53f0: 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f T_FILE_PERMISSIO
5400: 4e 53 3b 0a 20 20 77 68 69 6c 65 28 31 29 7b 0a NS;. while(1){.
5410: 23 69 66 20 64 65 66 69 6e 65 64 28 4f 5f 43 4c #if defined(O_CL
5420: 4f 45 58 45 43 29 0a 20 20 20 20 66 64 20 3d 20 OEXEC). fd =
5430: 6f 73 4f 70 65 6e 28 7a 2c 66 7c 4f 5f 43 4c 4f osOpen(z,f|O_CLO
5440: 45 58 45 43 2c 6d 32 29 3b 0a 23 65 6c 73 65 0a EXEC,m2);.#else.
5450: 20 20 20 20 66 64 20 3d 20 6f 73 4f 70 65 6e 28 fd = osOpen(
5460: 7a 2c 66 2c 6d 32 29 3b 0a 23 65 6e 64 69 66 0a z,f,m2);.#endif.
5470: 20 20 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a if( fd<0 ){.
5480: 20 20 20 20 20 20 69 66 28 20 65 72 72 6e 6f 3d if( errno=
5490: 3d 45 49 4e 54 52 20 29 20 63 6f 6e 74 69 6e 75 =EINTR ) continu
54a0: 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a e;. break;.
54b0: 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 66 64 }. if( fd
54c0: 3e 3d 53 51 4c 49 54 45 5f 4d 49 4e 49 4d 55 4d >=SQLITE_MINIMUM
54d0: 5f 46 49 4c 45 5f 44 45 53 43 52 49 50 54 4f 52 _FILE_DESCRIPTOR
54e0: 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 6f 73 ) break;. os
54f0: 43 6c 6f 73 65 28 66 64 29 3b 0a 20 20 20 20 73 Close(fd);. s
5500: 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 54 qlite3_log(SQLIT
5510: 45 5f 57 41 52 4e 49 4e 47 2c 20 0a 20 20 20 20 E_WARNING, .
5520: 20 20 20 20 20 20 20 20 20 20 20 20 22 61 74 74 "att
5530: 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20 5c 22 25 empt to open \"%
5540: 73 5c 22 20 61 73 20 66 69 6c 65 20 64 65 73 63 s\" as file desc
5550: 72 69 70 74 6f 72 20 25 64 22 2c 20 7a 2c 20 66 riptor %d", z, f
5560: 64 29 3b 0a 20 20 20 20 66 64 20 3d 20 2d 31 3b d);. fd = -1;
5570: 0a 20 20 20 20 69 66 28 20 6f 73 4f 70 65 6e 28 . if( osOpen(
5580: 22 2f 64 65 76 2f 6e 75 6c 6c 22 2c 20 66 2c 20 "/dev/null", f,
5590: 6d 29 3c 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 m)<0 ) break;.
55a0: 7d 0a 20 20 69 66 28 20 66 64 3e 3d 30 20 29 7b }. if( fd>=0 ){
55b0: 0a 20 20 20 20 69 66 28 20 6d 21 3d 30 20 29 7b . if( m!=0 ){
55c0: 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 73 74 . struct st
55d0: 61 74 20 73 74 61 74 62 75 66 3b 0a 20 20 20 20 at statbuf;.
55e0: 20 20 69 66 28 20 6f 73 46 73 74 61 74 28 66 64 if( osFstat(fd
55f0: 2c 20 26 73 74 61 74 62 75 66 29 3d 3d 30 20 0a , &statbuf)==0 .
5600: 20 20 20 20 20 20 20 26 26 20 73 74 61 74 62 75 && statbu
5610: 66 2e 73 74 5f 73 69 7a 65 3d 3d 30 0a 20 20 20 f.st_size==0.
5620: 20 20 20 20 26 26 20 28 73 74 61 74 62 75 66 2e && (statbuf.
5630: 73 74 5f 6d 6f 64 65 26 30 37 37 37 29 21 3d 6d st_mode&0777)!=m
5640: 20 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 . ){.
5650: 20 20 20 6f 73 46 63 68 6d 6f 64 28 66 64 2c 20 osFchmod(fd,
5660: 6d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 m);. }.
5670: 7d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 46 44 }.#if defined(FD
5680: 5f 43 4c 4f 45 58 45 43 29 20 26 26 20 28 21 64 _CLOEXEC) && (!d
5690: 65 66 69 6e 65 64 28 4f 5f 43 4c 4f 45 58 45 43 efined(O_CLOEXEC
56a0: 29 20 7c 7c 20 4f 5f 43 4c 4f 45 58 45 43 3d 3d ) || O_CLOEXEC==
56b0: 30 29 0a 20 20 20 20 6f 73 46 63 6e 74 6c 28 66 0). osFcntl(f
56c0: 64 2c 20 46 5f 53 45 54 46 44 2c 20 6f 73 46 63 d, F_SETFD, osFc
56d0: 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c ntl(fd, F_GETFD,
56e0: 20 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43 0) | FD_CLOEXEC
56f0: 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 );.#endif. }.
5700: 72 65 74 75 72 6e 20 66 64 3b 0a 7d 0a 0a 2f 2a return fd;.}../*
5710: 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 .** Helper funct
5720: 69 6f 6e 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 ions to obtain a
5730: 6e 64 20 72 65 6c 69 6e 71 75 69 73 68 20 74 68 nd relinquish th
5740: 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 2e 20 e global mutex.
5750: 54 68 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 6d 75 The.** global mu
5760: 74 65 78 20 69 73 20 75 73 65 64 20 74 6f 20 70 tex is used to p
5770: 72 6f 74 65 63 74 20 74 68 65 20 75 6e 69 78 49 rotect the unixI
5780: 6e 6f 64 65 49 6e 66 6f 20 61 6e 64 0a 2a 2a 20 nodeInfo and.**
5790: 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 vxworksFileId ob
57a0: 6a 65 63 74 73 20 75 73 65 64 20 62 79 20 74 68 jects used by th
57b0: 69 73 20 66 69 6c 65 2c 20 61 6c 6c 20 6f 66 20 is file, all of
57c0: 77 68 69 63 68 20 6d 61 79 20 62 65 20 0a 2a 2a which may be .**
57d0: 20 73 68 61 72 65 64 20 62 79 20 6d 75 6c 74 69 shared by multi
57e0: 70 6c 65 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a ple threads..**.
57f0: 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 75 6e 69 78 ** Function unix
5800: 4d 75 74 65 78 48 65 6c 64 28 29 20 69 73 20 75 MutexHeld() is u
5810: 73 65 64 20 74 6f 20 61 73 73 65 72 74 28 29 20 sed to assert()
5820: 74 68 61 74 20 74 68 65 20 67 6c 6f 62 61 6c 20 that the global
5830: 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 68 65 6c mutex .** is hel
5840: 64 20 77 68 65 6e 20 72 65 71 75 69 72 65 64 2e d when required.
5850: 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
5860: 73 20 6f 6e 6c 79 20 75 73 65 64 20 61 73 20 70 s only used as p
5870: 61 72 74 20 6f 66 20 61 73 73 65 72 74 28 29 20 art of assert()
5880: 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 .** statements.
5890: 65 2e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 75 6e 69 e.g..**.** uni
58a0: 78 45 6e 74 65 72 4d 75 74 65 78 28 29 0a 2a 2a xEnterMutex().**
58b0: 20 20 20 20 20 61 73 73 65 72 74 28 20 75 6e 69 assert( uni
58c0: 78 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a xMutexHeld() );.
58d0: 2a 2a 20 20 20 75 6e 69 78 45 6e 74 65 72 4c 65 ** unixEnterLe
58e0: 61 76 65 28 29 0a 2a 2f 0a 73 74 61 74 69 63 20 ave().*/.static
58f0: 76 6f 69 64 20 75 6e 69 78 45 6e 74 65 72 4d 75 void unixEnterMu
5900: 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c tex(void){. sql
5910: 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
5920: 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c (sqlite3MutexAll
5930: 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
5940: 53 54 41 54 49 43 5f 56 46 53 31 29 29 3b 0a 7d STATIC_VFS1));.}
5950: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 .static void uni
5960: 78 4c 65 61 76 65 4d 75 74 65 78 28 76 6f 69 64 xLeaveMutex(void
5970: 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 ){. sqlite3_mut
5980: 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 ex_leave(sqlite3
5990: 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
59a0: 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 56 E_MUTEX_STATIC_V
59b0: 46 53 31 29 29 3b 0a 7d 0a 23 69 66 64 65 66 20 FS1));.}.#ifdef
59c0: 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61 SQLITE_DEBUG.sta
59d0: 74 69 63 20 69 6e 74 20 75 6e 69 78 4d 75 74 65 tic int unixMute
59e0: 78 48 65 6c 64 28 76 6f 69 64 29 20 7b 0a 20 20 xHeld(void) {.
59f0: 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d return sqlite3_m
5a00: 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 utex_held(sqlite
5a10: 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
5a20: 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
5a30: 56 46 53 31 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 VFS1));.}.#endif
5a40: 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ...#ifdef SQLITE
5a50: 5f 48 41 56 45 5f 4f 53 5f 54 52 41 43 45 0a 2f _HAVE_OS_TRACE./
5a60: 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 *.** Helper func
5a70: 74 69 6f 6e 20 66 6f 72 20 70 72 69 6e 74 69 6e tion for printin
5a80: 67 20 6f 75 74 20 74 72 61 63 65 20 69 6e 66 6f g out trace info
5a90: 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 64 65 62 rmation from deb
5aa0: 75 67 67 69 6e 67 0a 2a 2a 20 62 69 6e 61 72 69 ugging.** binari
5ab0: 65 73 2e 20 54 68 69 73 20 72 65 74 75 72 6e 73 es. This returns
5ac0: 20 74 68 65 20 73 74 72 69 6e 67 20 72 65 70 72 the string repr
5ad0: 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 esentation of th
5ae0: 65 20 73 75 70 70 6c 69 65 64 0a 2a 2a 20 69 6e e supplied.** in
5af0: 74 65 67 65 72 20 6c 6f 63 6b 2d 74 79 70 65 2e teger lock-type.
5b00: 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 .*/.static const
5b10: 20 63 68 61 72 20 2a 61 7a 46 69 6c 65 4c 6f 63 char *azFileLoc
5b20: 6b 28 69 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29 k(int eFileLock)
5b30: 7b 0a 20 20 73 77 69 74 63 68 28 20 65 46 69 6c {. switch( eFil
5b40: 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 63 61 73 eLock ){. cas
5b50: 65 20 4e 4f 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 e NO_LOCK: retur
5b60: 6e 20 22 4e 4f 4e 45 22 3b 0a 20 20 20 20 63 61 n "NONE";. ca
5b70: 73 65 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3a 20 se SHARED_LOCK:
5b80: 72 65 74 75 72 6e 20 22 53 48 41 52 45 44 22 3b return "SHARED";
5b90: 0a 20 20 20 20 63 61 73 65 20 52 45 53 45 52 56 . case RESERV
5ba0: 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 ED_LOCK: return
5bb0: 22 52 45 53 45 52 56 45 44 22 3b 0a 20 20 20 20 "RESERVED";.
5bc0: 63 61 73 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 case PENDING_LOC
5bd0: 4b 3a 20 72 65 74 75 72 6e 20 22 50 45 4e 44 49 K: return "PENDI
5be0: 4e 47 22 3b 0a 20 20 20 20 63 61 73 65 20 45 58 NG";. case EX
5bf0: 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 20 72 65 CLUSIVE_LOCK: re
5c00: 74 75 72 6e 20 22 45 58 43 4c 55 53 49 56 45 22 turn "EXCLUSIVE"
5c10: 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 22 ;. }. return "
5c20: 45 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e 64 69 66 ERROR";.}.#endif
5c30: 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
5c40: 4c 4f 43 4b 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a LOCK_TRACE./*.**
5c50: 20 50 72 69 6e 74 20 6f 75 74 20 69 6e 66 6f 72 Print out infor
5c60: 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 6c 6c mation about all
5c70: 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 locking operati
5c80: 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ons..**.** This
5c90: 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
5ca0: 66 6f 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 for troubleshoot
5cb0: 69 6e 67 20 6c 6f 63 6b 73 20 6f 6e 20 6d 75 6c ing locks on mul
5cc0: 74 69 74 68 72 65 61 64 65 64 0a 2a 2a 20 70 6c tithreaded.** pl
5cd0: 61 74 66 6f 72 6d 73 2e 20 20 45 6e 61 62 6c 65 atforms. Enable
5ce0: 20 62 79 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 by compiling wi
5cf0: 74 68 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f th the -DSQLITE_
5d00: 4c 4f 43 4b 5f 54 52 41 43 45 0a 2a 2a 20 63 6f LOCK_TRACE.** co
5d10: 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f 70 74 69 6f mmand-line optio
5d20: 6e 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 n on the compile
5d30: 72 2e 20 20 54 68 69 73 20 63 6f 64 65 20 69 73 r. This code is
5d40: 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 74 75 72 normally.** tur
5d50: 6e 65 64 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74 ned off..*/.stat
5d60: 69 63 20 69 6e 74 20 6c 6f 63 6b 54 72 61 63 65 ic int lockTrace
5d70: 28 69 6e 74 20 66 64 2c 20 69 6e 74 20 6f 70 2c (int fd, int op,
5d80: 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 2a 70 struct flock *p
5d90: 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4f 70 4e 61 ){. char *zOpNa
5da0: 6d 65 2c 20 2a 7a 54 79 70 65 3b 0a 20 20 69 6e me, *zType;. in
5db0: 74 20 73 3b 0a 20 20 69 6e 74 20 73 61 76 65 64 t s;. int saved
5dc0: 45 72 72 6e 6f 3b 0a 20 20 69 66 28 20 6f 70 3d Errno;. if( op=
5dd0: 3d 46 5f 47 45 54 4c 4b 20 29 7b 0a 20 20 20 20 =F_GETLK ){.
5de0: 7a 4f 70 4e 61 6d 65 20 3d 20 22 47 45 54 4c 4b zOpName = "GETLK
5df0: 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f ";. }else if( o
5e00: 70 3d 3d 46 5f 53 45 54 4c 4b 20 29 7b 0a 20 20 p==F_SETLK ){.
5e10: 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 53 45 54 zOpName = "SET
5e20: 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 LK";. }else{.
5e30: 20 20 73 20 3d 20 6f 73 46 63 6e 74 6c 28 66 64 s = osFcntl(fd
5e40: 2c 20 6f 70 2c 20 70 29 3b 0a 20 20 20 20 73 71 , op, p);. sq
5e50: 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
5e60: 28 22 66 63 6e 74 6c 20 75 6e 6b 6e 6f 77 6e 20 ("fcntl unknown
5e70: 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 66 64 2c %d %d %d\n", fd,
5e80: 20 6f 70 2c 20 73 29 3b 0a 20 20 20 20 72 65 74 op, s);. ret
5e90: 75 72 6e 20 73 3b 0a 20 20 7d 0a 20 20 69 66 28 urn s;. }. if(
5ea0: 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 p->l_type==F_RD
5eb0: 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 LCK ){. zType
5ec0: 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 20 7d 65 = "RDLCK";. }e
5ed0: 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 lse if( p->l_typ
5ee0: 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 20 e==F_WRLCK ){.
5ef0: 20 20 7a 54 79 70 65 20 3d 20 22 57 52 4c 43 4b zType = "WRLCK
5f00: 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 ";. }else if( p
5f10: 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 ->l_type==F_UNLC
5f20: 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d K ){. zType =
5f30: 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 "UNLCK";. }els
5f40: 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 e{. assert( 0
5f50: 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 );. }. assert
5f60: 28 20 70 2d 3e 6c 5f 77 68 65 6e 63 65 3d 3d 53 ( p->l_whence==S
5f70: 45 45 4b 5f 53 45 54 20 29 3b 0a 20 20 73 20 3d EEK_SET );. s =
5f80: 20 6f 73 46 63 6e 74 6c 28 66 64 2c 20 6f 70 2c osFcntl(fd, op,
5f90: 20 70 29 3b 0a 20 20 73 61 76 65 64 45 72 72 6e p);. savedErrn
5fa0: 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 73 71 6c o = errno;. sql
5fb0: 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
5fc0: 22 66 63 6e 74 6c 20 25 64 20 25 64 20 25 73 20 "fcntl %d %d %s
5fd0: 25 73 20 25 64 20 25 64 20 25 64 20 25 64 5c 6e %s %d %d %d %d\n
5fe0: 22 2c 0a 20 20 20 20 20 74 68 72 65 61 64 69 64 ",. threadid
5ff0: 2c 20 66 64 2c 20 7a 4f 70 4e 61 6d 65 2c 20 7a , fd, zOpName, z
6000: 54 79 70 65 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f Type, (int)p->l_
6010: 73 74 61 72 74 2c 20 28 69 6e 74 29 70 2d 3e 6c start, (int)p->l
6020: 5f 6c 65 6e 2c 0a 20 20 20 20 20 28 69 6e 74 29 _len,. (int)
6030: 70 2d 3e 6c 5f 70 69 64 2c 20 73 29 3b 0a 20 20 p->l_pid, s);.
6040: 69 66 28 20 73 3d 3d 28 2d 31 29 20 26 26 20 6f if( s==(-1) && o
6050: 70 3d 3d 46 5f 53 45 54 4c 4b 20 26 26 20 28 70 p==F_SETLK && (p
6060: 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 ->l_type==F_RDLC
6070: 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d K || p->l_type==
6080: 46 5f 57 52 4c 43 4b 29 20 29 7b 0a 20 20 20 20 F_WRLCK) ){.
6090: 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 32 3b struct flock l2;
60a0: 0a 20 20 20 20 6c 32 20 3d 20 2a 70 3b 0a 20 20 . l2 = *p;.
60b0: 20 20 6f 73 46 63 6e 74 6c 28 66 64 2c 20 46 5f osFcntl(fd, F_
60c0: 47 45 54 4c 4b 2c 20 26 6c 32 29 3b 0a 20 20 20 GETLK, &l2);.
60d0: 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d if( l2.l_type==
60e0: 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 20 F_RDLCK ){.
60f0: 20 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 zType = "RDLCK"
6100: 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
6110: 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c l2.l_type==F_WRL
6120: 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 CK ){. zTyp
6130: 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 20 20 e = "WRLCK";.
6140: 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f }else if( l2.l_
6150: 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b type==F_UNLCK ){
6160: 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 . zType = "
6170: 55 4e 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 UNLCK";. }els
6180: 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 e{. assert(
6190: 20 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0 );. }.
61a0: 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
61b0: 74 66 28 22 66 63 6e 74 6c 2d 66 61 69 6c 75 72 tf("fcntl-failur
61c0: 65 2d 72 65 61 73 6f 6e 3a 20 25 73 20 25 64 20 e-reason: %s %d
61d0: 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 %d %d\n",.
61e0: 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 6c 32 2e zType, (int)l2.
61f0: 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 6c 32 l_start, (int)l2
6200: 2e 6c 5f 6c 65 6e 2c 20 28 69 6e 74 29 6c 32 2e .l_len, (int)l2.
6210: 6c 5f 70 69 64 29 3b 0a 20 20 7d 0a 20 20 65 72 l_pid);. }. er
6220: 72 6e 6f 20 3d 20 73 61 76 65 64 45 72 72 6e 6f rno = savedErrno
6230: 3b 0a 20 20 72 65 74 75 72 6e 20 73 3b 0a 7d 0a ;. return s;.}.
6240: 23 75 6e 64 65 66 20 6f 73 46 63 6e 74 6c 0a 23 #undef osFcntl.#
6250: 64 65 66 69 6e 65 20 6f 73 46 63 6e 74 6c 20 6c define osFcntl l
6260: 6f 63 6b 54 72 61 63 65 0a 23 65 6e 64 69 66 20 ockTrace.#endif
6270: 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 /* SQLITE_LOCK_T
6280: 52 41 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 RACE */../*.** R
6290: 65 74 72 79 20 66 74 72 75 6e 63 61 74 65 28 29 etry ftruncate()
62a0: 20 63 61 6c 6c 73 20 74 68 61 74 20 66 61 69 6c calls that fail
62b0: 20 64 75 65 20 74 6f 20 45 49 4e 54 52 0a 2a 2a due to EINTR.**
62c0: 0a 2a 2a 20 41 6c 6c 20 63 61 6c 6c 73 20 74 6f .** All calls to
62d0: 20 66 74 72 75 6e 63 61 74 65 28 29 20 77 69 74 ftruncate() wit
62e0: 68 69 6e 20 74 68 69 73 20 66 69 6c 65 20 73 68 hin this file sh
62f0: 6f 75 6c 64 20 62 65 20 6d 61 64 65 20 74 68 72 ould be made thr
6300: 6f 75 67 68 0a 2a 2a 20 74 68 69 73 20 77 72 61 ough.** this wra
6310: 70 70 65 72 2e 20 20 4f 6e 20 74 68 65 20 41 6e pper. On the An
6320: 64 72 6f 69 64 20 70 6c 61 74 66 6f 72 6d 2c 20 droid platform,
6330: 62 79 70 61 73 73 69 6e 67 20 74 68 65 20 6c 6f bypassing the lo
6340: 67 69 63 20 62 65 6c 6f 77 0a 2a 2a 20 63 6f 75 gic below.** cou
6350: 6c 64 20 6c 65 61 64 20 74 6f 20 61 20 63 6f 72 ld lead to a cor
6360: 72 75 70 74 20 64 61 74 61 62 61 73 65 2e 0a 2a rupt database..*
6370: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 6f 62 /.static int rob
6380: 75 73 74 5f 66 74 72 75 6e 63 61 74 65 28 69 6e ust_ftruncate(in
6390: 74 20 68 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 t h, sqlite3_int
63a0: 36 34 20 73 7a 29 7b 0a 20 20 69 6e 74 20 72 63 64 sz){. int rc
63b0: 3b 0a 23 69 66 64 65 66 20 5f 5f 41 4e 44 52 4f ;.#ifdef __ANDRO
63c0: 49 44 5f 5f 0a 20 20 2f 2a 20 4f 6e 20 41 6e 64 ID__. /* On And
63d0: 72 6f 69 64 2c 20 66 74 72 75 6e 63 61 74 65 28 roid, ftruncate(
63e0: 29 20 61 6c 77 61 79 73 20 75 73 65 73 20 33 32 ) always uses 32
63f0: 2d 62 69 74 20 6f 66 66 73 65 74 73 2c 20 65 76 -bit offsets, ev
6400: 65 6e 20 69 66 20 0a 20 20 2a 2a 20 5f 46 49 4c en if . ** _FIL
6410: 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 3d 36 34 E_OFFSET_BITS=64
6420: 20 69 73 20 64 65 66 69 6e 65 64 2e 20 54 68 69 is defined. Thi
6430: 73 20 6d 65 61 6e 73 20 69 74 20 69 73 20 75 6e s means it is un
6440: 73 61 66 65 20 74 6f 20 61 74 74 65 6d 70 74 20 safe to attempt
6450: 74 6f 0a 20 20 2a 2a 20 74 72 75 6e 63 61 74 65 to. ** truncate
6460: 20 61 20 66 69 6c 65 20 74 6f 20 61 6e 79 20 73 a file to any s
6470: 69 7a 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20 ize larger than
6480: 32 47 69 42 2e 20 53 69 6c 65 6e 74 6c 79 20 69 2GiB. Silently i
6490: 67 6e 6f 72 65 20 61 6e 79 0a 20 20 2a 2a 20 73 gnore any. ** s
64a0: 75 63 68 20 61 74 74 65 6d 70 74 73 2e 20 20 2a uch attempts. *
64b0: 2f 0a 20 20 69 66 28 20 73 7a 3e 28 73 71 6c 69 /. if( sz>(sqli
64c0: 74 65 33 5f 69 6e 74 36 34 29 30 78 37 46 46 46 te3_int64)0x7FFF
64d0: 46 46 46 46 20 29 7b 0a 20 20 20 20 72 63 20 3d FFFF ){. rc =
64e0: 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 SQLITE_OK;. }e
64f0: 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 64 6f 7b lse.#endif. do{
6500: 20 72 63 20 3d 20 6f 73 46 74 72 75 6e 63 61 74 rc = osFtruncat
6510: 65 28 68 2c 73 7a 29 3b 20 7d 77 68 69 6c 65 28 e(h,sz); }while(
6520: 20 72 63 3c 30 20 26 26 20 65 72 72 6e 6f 3d 3d rc<0 && errno==
6530: 45 49 4e 54 52 20 29 3b 0a 20 20 72 65 74 75 72 EINTR );. retur
6540: 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
6550: 68 69 73 20 72 6f 75 74 69 6e 65 20 74 72 61 6e his routine tran
6560: 73 6c 61 74 65 73 20 61 20 73 74 61 6e 64 61 72 slates a standar
6570: 64 20 50 4f 53 49 58 20 65 72 72 6e 6f 20 63 6f d POSIX errno co
6580: 64 65 20 69 6e 74 6f 20 73 6f 6d 65 74 68 69 6e de into somethin
6590: 67 0a 2a 2a 20 75 73 65 66 75 6c 20 74 6f 20 74 g.** useful to t
65a0: 68 65 20 63 6c 69 65 6e 74 73 20 6f 66 20 74 68 he clients of th
65b0: 65 20 73 71 6c 69 74 65 33 20 66 75 6e 63 74 69 e sqlite3 functi
65c0: 6f 6e 73 2e 20 20 53 70 65 63 69 66 69 63 61 6c ons. Specifical
65d0: 6c 79 2c 20 69 74 20 69 73 0a 2a 2a 20 69 6e 74 ly, it is.** int
65e0: 65 6e 64 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 ended to transla
65f0: 74 65 20 61 20 76 61 72 69 65 74 79 20 6f 66 20 te a variety of
6600: 22 74 72 79 20 61 67 61 69 6e 22 20 65 72 72 6f "try again" erro
6610: 72 73 20 69 6e 74 6f 20 53 51 4c 49 54 45 5f 42 rs into SQLITE_B
6620: 55 53 59 0a 2a 2a 20 61 6e 64 20 61 20 76 61 72 USY.** and a var
6630: 69 65 74 79 20 6f 66 20 22 70 6c 65 61 73 65 20 iety of "please
6640: 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 64 close the file d
6650: 65 73 63 72 69 70 74 6f 72 20 4e 4f 57 22 20 65 escriptor NOW" e
6660: 72 72 6f 72 73 20 69 6e 74 6f 20 0a 2a 2a 20 53 rrors into .** S
6670: 51 4c 49 54 45 5f 49 4f 45 52 52 0a 2a 2a 20 0a QLITE_IOERR.** .
6680: 2a 2a 20 45 72 72 6f 72 73 20 64 75 72 69 6e 67 ** Errors during
6690: 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
66a0: 6f 66 20 6c 6f 63 6b 73 2c 20 6f 72 20 66 69 6c of locks, or fil
66b0: 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 e system support
66c0: 20 66 6f 72 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 73 for locks,.** s
66d0: 68 6f 75 6c 64 20 68 61 6e 64 6c 65 20 45 4e 4f hould handle ENO
66e0: 4c 43 4b 2c 20 45 4e 4f 54 53 55 50 2c 20 45 4f LCK, ENOTSUP, EO
66f0: 50 4e 4f 54 53 55 50 50 20 73 65 70 61 72 61 74 PNOTSUPP separat
6700: 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ely..*/.static i
6710: 6e 74 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 nt sqliteErrorFr
6720: 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 69 6e 74 omPosixError(int
6730: 20 70 6f 73 69 78 45 72 72 6f 72 2c 20 69 6e 74 posixError, int
6740: 20 73 71 6c 69 74 65 49 4f 45 72 72 29 20 7b 0a sqliteIOErr) {.
6750: 20 20 61 73 73 65 72 74 28 20 28 73 71 6c 69 74 assert( (sqlit
6760: 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 eIOErr == SQLITE
6770: 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 20 7c 7c 20 _IOERR_LOCK) ||
6780: 0a 20 20 20 20 20 20 20 20 20 20 28 73 71 6c 69 . (sqli
6790: 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 teIOErr == SQLIT
67a0: 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 20 E_IOERR_UNLOCK)
67b0: 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20 28 73 || . (s
67c0: 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 qliteIOErr == SQ
67d0: 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 LITE_IOERR_RDLOC
67e0: 4b 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 K) ||.
67f0: 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 (sqliteIOErr ==
6800: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 SQLITE_IOERR_CHE
6810: 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 20 CKRESERVEDLOCK)
6820: 29 3b 0a 20 20 73 77 69 74 63 68 20 28 70 6f 73 );. switch (pos
6830: 69 78 45 72 72 6f 72 29 20 7b 0a 20 20 63 61 73 ixError) {. cas
6840: 65 20 45 41 43 43 45 53 3a 20 0a 20 20 63 61 73 e EACCES: . cas
6850: 65 20 45 41 47 41 49 4e 3a 0a 20 20 63 61 73 65 e EAGAIN:. case
6860: 20 45 54 49 4d 45 44 4f 55 54 3a 0a 20 20 63 61 ETIMEDOUT:. ca
6870: 73 65 20 45 42 55 53 59 3a 0a 20 20 63 61 73 65 se EBUSY:. case
6880: 20 45 49 4e 54 52 3a 0a 20 20 63 61 73 65 20 45 EINTR:. case E
6890: 4e 4f 4c 43 4b 3a 20 20 0a 20 20 20 20 2f 2a 20 NOLCK: . /*
68a0: 72 61 6e 64 6f 6d 20 4e 46 53 20 72 65 74 72 79 random NFS retry
68b0: 20 65 72 72 6f 72 2c 20 75 6e 6c 65 73 73 20 64 error, unless d
68c0: 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73 74 65 uring file syste
68d0: 6d 20 73 75 70 70 6f 72 74 20 0a 20 20 20 20 20 m support .
68e0: 2a 20 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c * introspection,
68f0: 20 69 6e 20 77 68 69 63 68 20 69 74 20 61 63 74 in which it act
6900: 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61 74 ually means what
6910: 20 69 74 20 73 61 79 73 20 2a 2f 0a 20 20 20 20 it says */.
6920: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 return SQLITE_BU
6930: 53 59 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20 SY;. . case
6940: 45 50 45 52 4d 3a 20 0a 20 20 20 20 72 65 74 75 EPERM: . retu
6950: 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a rn SQLITE_PERM;.
6960: 20 20 20 20 0a 20 20 64 65 66 61 75 6c 74 3a 20 . default:
6970: 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 . return sqli
6980: 74 65 49 4f 45 72 72 3b 0a 20 20 7d 0a 7d 0a 0a teIOErr;. }.}..
6990: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
69a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
69b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
69c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
69d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
69e0: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
69f0: 2a 2a 2a 20 42 65 67 69 6e 20 55 6e 69 71 75 65 *** Begin Unique
6a00: 20 46 69 6c 65 20 49 44 20 55 74 69 6c 69 74 79 File ID Utility
6a10: 20 55 73 65 64 20 42 79 20 56 78 57 6f 72 6b 73 Used By VxWorks
6a20: 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
6a30: 0a 2a 2a 0a 2a 2a 20 4f 6e 20 6d 6f 73 74 20 76 .**.** On most v
6a40: 65 72 73 69 6f 6e 73 20 6f 66 20 75 6e 69 78 2c ersions of unix,
6a50: 20 77 65 20 63 61 6e 20 67 65 74 20 61 20 75 6e we can get a un
6a60: 69 71 75 65 20 49 44 20 66 6f 72 20 61 20 66 69 ique ID for a fi
6a70: 6c 65 20 62 79 20 63 6f 6e 63 61 74 65 6e 61 74 le by concatenat
6a80: 69 6e 67 0a 2a 2a 20 74 68 65 20 64 65 76 69 63 ing.** the devic
6a90: 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65 e number and the
6aa0: 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20 20 inode number.
6ab0: 42 75 74 20 74 68 69 73 20 64 6f 65 73 20 6e 6f But this does no
6ac0: 74 20 77 6f 72 6b 20 6f 6e 20 56 78 57 6f 72 6b t work on VxWork
6ad0: 73 2e 0a 2a 2a 20 4f 6e 20 56 78 57 6f 72 6b 73 s..** On VxWorks
6ae0: 2c 20 61 20 75 6e 69 71 75 65 20 66 69 6c 65 20 , a unique file
6af0: 69 64 20 6d 75 73 74 20 62 65 20 62 61 73 65 64 id must be based
6b00: 20 6f 6e 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 on the canonica
6b10: 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 0a 2a l filename..**.*
6b20: 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 * A pointer to a
6b30: 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
6b40: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
6b50: 63 74 75 72 65 20 63 61 6e 20 62 65 20 75 73 65 cture can be use
6b60: 64 20 61 73 20 61 0a 2a 2a 20 75 6e 69 71 75 65 d as a.** unique
6b70: 20 66 69 6c 65 20 49 44 20 69 6e 20 56 78 57 6f file ID in VxWo
6b80: 72 6b 73 2e 20 20 45 61 63 68 20 69 6e 73 74 61 rks. Each insta
6b90: 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 nce of this stru
6ba0: 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 0a 2a cture contains.*
6bb0: 2a 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 * a copy of the
6bc0: 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 canonical filena
6bd0: 6d 65 2e 20 20 54 68 65 72 65 20 69 73 20 61 6c me. There is al
6be0: 73 6f 20 61 20 72 65 66 65 72 65 6e 63 65 20 63 so a reference c
6bf0: 6f 75 6e 74 2e 20 20 0a 2a 2a 20 54 68 65 20 73 ount. .** The s
6c00: 74 72 75 63 74 75 72 65 20 69 73 20 72 65 63 6c tructure is recl
6c10: 61 69 6d 65 64 20 77 68 65 6e 20 74 68 65 20 6e aimed when the n
6c20: 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 umber of pointer
6c30: 73 20 74 6f 20 69 74 20 64 72 6f 70 73 20 74 6f s to it drops to
6c40: 0a 2a 2a 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 .** zero..**.**
6c50: 54 68 65 72 65 20 61 72 65 20 6e 65 76 65 72 20 There are never
6c60: 76 65 72 79 20 6d 61 6e 79 20 66 69 6c 65 73 20 very many files
6c70: 6f 70 65 6e 20 61 74 20 6f 6e 65 20 74 69 6d 65 open at one time
6c80: 20 61 6e 64 20 6c 6f 6f 6b 75 70 73 20 61 72 65 and lookups are
6c90: 20 6e 6f 74 0a 2a 2a 20 61 20 70 65 72 66 6f 72 not.** a perfor
6ca0: 6d 61 6e 63 65 2d 63 72 69 74 69 63 61 6c 20 70 mance-critical p
6cb0: 61 74 68 2c 20 73 6f 20 69 74 20 69 73 20 73 75 ath, so it is su
6cc0: 66 66 69 63 69 65 6e 74 20 74 6f 20 70 75 74 20 fficient to put
6cd0: 74 68 65 73 65 0a 2a 2a 20 73 74 72 75 63 74 75 these.** structu
6ce0: 72 65 73 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 res on a linked
6cf0: 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 list..*/.struct
6d00: 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 7b 0a vxworksFileId {.
6d10: 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 struct vxworks
6d20: 46 69 6c 65 49 64 20 2a 70 4e 65 78 74 3b 20 20 FileId *pNext;
6d30: 2f 2a 20 4e 65 78 74 20 69 6e 20 61 20 6c 69 73 /* Next in a lis
6d40: 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20 2a 2f t of them all */
6d50: 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 . int nRef;
6d60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d70: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 /* Number of re
6d80: 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 69 73 ferences to this
6d90: 20 6f 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e one */. int nN
6da0: 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ame;
6db0: 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 /* Lengt
6dc0: 68 20 6f 66 20 74 68 65 20 7a 43 61 6e 6f 6e 69 h of the zCanoni
6dd0: 63 61 6c 4e 61 6d 65 5b 5d 20 73 74 72 69 6e 67 calName[] string
6de0: 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 61 6e */. char *zCan
6df0: 6f 6e 69 63 61 6c 4e 61 6d 65 3b 20 20 20 20 20 onicalName;
6e00: 20 20 20 20 2f 2a 20 43 61 6e 6f 6e 69 63 61 6c /* Canonical
6e10: 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a 7d 3b 0a filename */.};.
6e20: 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a .#if OS_VXWORKS.
6e30: 2f 2a 20 0a 2a 2a 20 41 6c 6c 20 75 6e 69 71 75 /* .** All uniqu
6e40: 65 20 66 69 6c 65 6e 61 6d 65 73 20 61 72 65 20 e filenames are
6e50: 68 65 6c 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 held on a linked
6e60: 20 6c 69 73 74 20 68 65 61 64 65 64 20 62 79 20 list headed by
6e70: 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c 65 this.** variable
6e80: 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 :.*/.static stru
6e90: 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 ct vxworksFileId
6ea0: 20 2a 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 *vxworksFileLis
6eb0: 74 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 53 69 t = 0;../*.** Si
6ec0: 6d 70 6c 69 66 79 20 61 20 66 69 6c 65 6e 61 6d mplify a filenam
6ed0: 65 20 69 6e 74 6f 20 69 74 73 20 63 61 6e 6f 6e e into its canon
6ee0: 69 63 61 6c 20 66 6f 72 6d 0a 2a 2a 20 62 79 20 ical form.** by
6ef0: 6d 61 6b 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f making the follo
6f00: 77 69 6e 67 20 63 68 61 6e 67 65 73 3a 0a 2a 2a wing changes:.**
6f10: 0a 2a 2a 20 20 2a 20 72 65 6d 6f 76 69 6e 67 20 .** * removing
6f20: 61 6e 79 20 74 72 61 69 6c 69 6e 67 20 61 6e 64 any trailing and
6f30: 20 64 75 70 6c 69 63 61 74 65 20 2f 0a 2a 2a 20 duplicate /.**
6f40: 20 2a 20 63 6f 6e 76 65 72 74 20 2f 2e 2f 20 69 * convert /./ i
6f50: 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a 20 20 2a nto just /.** *
6f60: 20 63 6f 6e 76 65 72 74 20 2f 41 2f 2e 2e 2f 20 convert /A/../
6f70: 77 68 65 72 65 20 41 20 69 73 20 61 6e 79 20 73 where A is any s
6f80: 69 6d 70 6c 65 20 6e 61 6d 65 20 69 6e 74 6f 20 imple name into
6f90: 6a 75 73 74 20 2f 0a 2a 2a 0a 2a 2a 20 43 68 61 just /.**.** Cha
6fa0: 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 69 6e nges are made in
6fb0: 2d 70 6c 61 63 65 2e 20 20 52 65 74 75 72 6e 20 -place. Return
6fc0: 74 68 65 20 6e 65 77 20 6e 61 6d 65 20 6c 65 6e the new name len
6fd0: 67 74 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f gth..**.** The o
6fe0: 72 69 67 69 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 riginal filename
6ff0: 20 69 73 20 69 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d is in z[0..n-1]
7000: 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 . Return the nu
7010: 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72 61 mber of.** chara
7020: 63 74 65 72 73 20 69 6e 20 74 68 65 20 73 69 6d cters in the sim
7030: 70 6c 69 66 69 65 64 20 6e 61 6d 65 2e 0a 2a 2f plified name..*/
7040: 0a 73 74 61 74 69 63 20 69 6e 74 20 76 78 77 6f .static int vxwo
7050: 72 6b 73 53 69 6d 70 6c 69 66 79 4e 61 6d 65 28 rksSimplifyName(
7060: 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b char *z, int n){
7070: 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 77 . int i, j;. w
7080: 68 69 6c 65 28 20 6e 3e 31 20 26 26 20 7a 5b 6e hile( n>1 && z[n
7090: 2d 31 5d 3d 3d 27 2f 27 20 29 7b 20 6e 2d 2d 3b -1]=='/' ){ n--;
70a0: 20 7d 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 }. for(i=j=0;
70b0: 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 i<n; i++){. i
70c0: 66 28 20 7a 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a f( z[i]=='/' ){.
70d0: 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d if( z[i+1]
70e0: 3d 3d 27 2f 27 20 29 20 63 6f 6e 74 69 6e 75 65 =='/' ) continue
70f0: 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b ;. if( z[i+
7100: 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b 32 3c 6e 1]=='.' && i+2<n
7110: 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2f 27 20 && z[i+2]=='/'
7120: 29 7b 0a 20 20 20 20 20 20 20 20 69 20 2b 3d 20 ){. i +=
7130: 31 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 1;. conti
7140: 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 nue;. }.
7150: 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 if( z[i+1]=='
7160: 2e 27 20 26 26 20 69 2b 33 3c 6e 20 26 26 20 7a .' && i+3<n && z
7170: 5b 69 2b 32 5d 3d 3d 27 2e 27 20 26 26 20 7a 5b [i+2]=='.' && z[
7180: 69 2b 33 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 i+3]=='/' ){.
7190: 20 20 20 20 20 77 68 69 6c 65 28 20 6a 3e 30 20 while( j>0
71a0: 26 26 20 7a 5b 6a 2d 31 5d 21 3d 27 2f 27 20 29 && z[j-1]!='/' )
71b0: 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 { j--; }.
71c0: 20 69 66 28 20 6a 3e 30 20 29 7b 20 6a 2d 2d 3b if( j>0 ){ j--;
71d0: 20 7d 0a 20 20 20 20 20 20 20 20 69 20 2b 3d 20 }. i +=
71e0: 32 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 2;. conti
71f0: 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 nue;. }.
7200: 20 7d 0a 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 }. z[j++] =
7210: 7a 5b 69 5d 3b 0a 20 20 7d 0a 20 20 7a 5b 6a 5d z[i];. }. z[j]
7220: 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 6a = 0;. return j
7230: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 ;.}../*.** Find
7240: 61 20 75 6e 69 71 75 65 20 66 69 6c 65 20 49 44 a unique file ID
7250: 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 61 for the given a
7260: 62 73 6f 6c 75 74 65 20 70 61 74 68 6e 61 6d 65 bsolute pathname
7270: 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70 . Return.** a p
7280: 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 76 78 ointer to the vx
7290: 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 worksFileId obje
72a0: 63 74 2e 20 20 54 68 69 73 20 70 6f 69 6e 74 65 ct. This pointe
72b0: 72 20 69 73 20 74 68 65 20 75 6e 69 71 75 65 0a r is the unique.
72c0: 2a 2a 20 66 69 6c 65 20 49 44 2e 0a 2a 2a 0a 2a ** file ID..**.*
72d0: 2a 20 54 68 65 20 6e 52 65 66 20 66 69 65 6c 64 * The nRef field
72e0: 20 6f 66 20 74 68 65 20 76 78 77 6f 72 6b 73 46 of the vxworksF
72f0: 69 6c 65 49 64 20 6f 62 6a 65 63 74 20 69 73 20 ileId object is
7300: 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 65 66 6f incremented befo
7310: 72 65 0a 2a 2a 20 74 68 65 20 6f 62 6a 65 63 74 re.** the object
7320: 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 41 is returned. A
7330: 20 6e 65 77 20 76 78 77 6f 72 6b 73 46 69 6c 65 new vxworksFile
7340: 49 64 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 Id object is cre
7350: 61 74 65 64 0a 2a 2a 20 61 6e 64 20 61 64 64 65 ated.** and adde
7360: 64 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20 d to the global
7370: 6c 69 73 74 20 69 66 20 6e 65 63 65 73 73 61 72 list if necessar
7380: 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 y..**.** If a me
7390: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
73a0: 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 error occurs, re
73b0: 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 turn NULL..*/.st
73c0: 61 74 69 63 20 73 74 72 75 63 74 20 76 78 77 6f atic struct vxwo
73d0: 72 6b 73 46 69 6c 65 49 64 20 2a 76 78 77 6f 72 rksFileId *vxwor
73e0: 6b 73 46 69 6e 64 46 69 6c 65 49 64 28 63 6f 6e ksFindFileId(con
73f0: 73 74 20 63 68 61 72 20 2a 7a 41 62 73 6f 6c 75 st char *zAbsolu
7400: 74 65 4e 61 6d 65 29 7b 0a 20 20 73 74 72 75 63 teName){. struc
7410: 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 t vxworksFileId
7420: 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 2f *pNew; /
7430: 2a 20 73 65 61 72 63 68 20 6b 65 79 20 61 6e 64 * search key and
7440: 20 6e 65 77 20 66 69 6c 65 20 49 44 20 2a 2f 0a new file ID */.
7450: 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 struct vxworks
7460: 46 69 6c 65 49 64 20 2a 70 43 61 6e 64 69 64 61 FileId *pCandida
7470: 74 65 3b 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f te; /* For loo
7480: 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69 ping over existi
7490: 6e 67 20 66 69 6c 65 20 49 44 73 20 2a 2f 0a 20 ng file IDs */.
74a0: 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 int n;
74b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
74c0: 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f /* Length o
74d0: 66 20 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 20 f zAbsoluteName
74e0: 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 61 73 73 string */.. ass
74f0: 65 72 74 28 20 7a 41 62 73 6f 6c 75 74 65 4e 61 ert( zAbsoluteNa
7500: 6d 65 5b 30 5d 3d 3d 27 2f 27 20 29 3b 0a 20 20 me[0]=='/' );.
7510: 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 n = (int)strlen(
7520: 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 29 3b 0a zAbsoluteName);.
7530: 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
7540: 5f 6d 61 6c 6c 6f 63 36 34 28 20 73 69 7a 65 6f _malloc64( sizeo
7550: 66 28 2a 70 4e 65 77 29 20 2b 20 28 6e 2b 31 29 f(*pNew) + (n+1)
7560: 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d );. if( pNew==
7570: 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
7580: 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c pNew->zCanonical
7590: 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 70 Name = (char*)&p
75a0: 4e 65 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63 70 79 New[1];. memcpy
75b0: 28 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 (pNew->zCanonica
75c0: 6c 4e 61 6d 65 2c 20 7a 41 62 73 6f 6c 75 74 65 lName, zAbsolute
75d0: 4e 61 6d 65 2c 20 6e 2b 31 29 3b 0a 20 20 6e 20 Name, n+1);. n
75e0: 3d 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69 66 = vxworksSimplif
75f0: 79 4e 61 6d 65 28 70 4e 65 77 2d 3e 7a 43 61 6e yName(pNew->zCan
7600: 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3b 0a onicalName, n);.
7610: 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 . /* Search for
7620: 20 61 6e 20 65 78 69 73 74 69 6e 67 20 65 6e 74 an existing ent
7630: 72 79 20 74 68 61 74 20 6d 61 74 63 68 69 6e 67 ry that matching
7640: 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 6e the canonical n
7650: 61 6d 65 2e 0a 20 20 2a 2a 20 49 66 20 66 6f 75 ame.. ** If fou
7660: 6e 64 2c 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 nd, increment th
7670: 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e e reference coun
7680: 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 t and return a p
7690: 6f 69 6e 74 65 72 20 74 6f 0a 20 20 2a 2a 20 74 ointer to. ** t
76a0: 68 65 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 he existing file
76b0: 20 49 44 2e 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 ID.. */. unix
76c0: 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 EnterMutex();.
76d0: 66 6f 72 28 70 43 61 6e 64 69 64 61 74 65 3d 76 for(pCandidate=v
76e0: 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 20 xworksFileList;
76f0: 70 43 61 6e 64 69 64 61 74 65 3b 20 70 43 61 6e pCandidate; pCan
7700: 64 69 64 61 74 65 3d 70 43 61 6e 64 69 64 61 74 didate=pCandidat
7710: 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 e->pNext){. i
7720: 66 28 20 70 43 61 6e 64 69 64 61 74 65 2d 3e 6e f( pCandidate->n
7730: 4e 61 6d 65 3d 3d 6e 20 0a 20 20 20 20 20 26 26 Name==n . &&
7740: 20 6d 65 6d 63 6d 70 28 70 43 61 6e 64 69 64 61 memcmp(pCandida
7750: 74 65 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 te->zCanonicalNa
7760: 6d 65 2c 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e me, pNew->zCanon
7770: 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3d 3d 30 0a icalName, n)==0.
7780: 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 73 71 ){. sq
7790: 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 65 77 29 lite3_free(pNew)
77a0: 3b 0a 20 20 20 20 20 20 20 70 43 61 6e 64 69 64 ;. pCandid
77b0: 61 74 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 ate->nRef++;.
77c0: 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 unixLeaveMut
77d0: 65 78 28 29 3b 0a 20 20 20 20 20 20 20 72 65 74 ex();. ret
77e0: 75 72 6e 20 70 43 61 6e 64 69 64 61 74 65 3b 0a urn pCandidate;.
77f0: 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
7800: 4e 6f 20 6d 61 74 63 68 20 77 61 73 20 66 6f 75 No match was fou
7810: 6e 64 2e 20 20 57 65 20 77 69 6c 6c 20 6d 61 6b nd. We will mak
7820: 65 20 61 20 6e 65 77 20 66 69 6c 65 20 49 44 20 e a new file ID
7830: 2a 2f 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 */. pNew->nRef
7840: 3d 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 6e 4e 61 = 1;. pNew->nNa
7850: 6d 65 20 3d 20 6e 3b 0a 20 20 70 4e 65 77 2d 3e me = n;. pNew->
7860: 70 4e 65 78 74 20 3d 20 76 78 77 6f 72 6b 73 46 pNext = vxworksF
7870: 69 6c 65 4c 69 73 74 3b 0a 20 20 76 78 77 6f 72 ileList;. vxwor
7880: 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20 70 4e 65 ksFileList = pNe
7890: 77 3b 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 w;. unixLeaveMu
78a0: 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 tex();. return
78b0: 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 pNew;.}../*.** D
78c0: 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 ecrement the ref
78d0: 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 6e 20 erence count on
78e0: 61 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 a vxworksFileId
78f0: 6f 62 6a 65 63 74 2e 20 20 46 72 65 65 0a 2a 2a object. Free.**
7900: 20 74 68 65 20 6f 62 6a 65 63 74 20 77 68 65 6e the object when
7910: 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 the reference c
7920: 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 ount reaches zer
7930: 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 o..*/.static voi
7940: 64 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 d vxworksRelease
7950: 46 69 6c 65 49 64 28 73 74 72 75 63 74 20 76 78 FileId(struct vx
7960: 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 64 worksFileId *pId
7970: 29 7b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 ){. unixEnterMu
7980: 74 65 78 28 29 3b 0a 20 20 61 73 73 65 72 74 28 tex();. assert(
7990: 20 70 49 64 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a pId->nRef>0 );.
79a0: 20 20 70 49 64 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 pId->nRef--;.
79b0: 20 69 66 28 20 70 49 64 2d 3e 6e 52 65 66 3d 3d if( pId->nRef==
79c0: 30 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 0 ){. struct
79d0: 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 2a vxworksFileId **
79e0: 70 70 3b 0a 20 20 20 20 66 6f 72 28 70 70 3d 26 pp;. for(pp=&
79f0: 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b vxworksFileList;
7a00: 20 2a 70 70 20 26 26 20 2a 70 70 21 3d 70 49 64 *pp && *pp!=pId
7a10: 3b 20 70 70 20 3d 20 26 28 28 2a 70 70 29 2d 3e ; pp = &((*pp)->
7a20: 70 4e 65 78 74 29 29 7b 7d 0a 20 20 20 20 61 73 pNext)){}. as
7a30: 73 65 72 74 28 20 2a 70 70 3d 3d 70 49 64 20 29 sert( *pp==pId )
7a40: 3b 0a 20 20 20 20 2a 70 70 20 3d 20 70 49 64 2d ;. *pp = pId-
7a50: 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 >pNext;. sqli
7a60: 74 65 33 5f 66 72 65 65 28 70 49 64 29 3b 0a 20 te3_free(pId);.
7a70: 20 7d 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 }. unixLeaveMu
7a80: 74 65 78 28 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 tex();.}.#endif
7a90: 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f /* OS_VXWORKS */
7aa0: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
7ab0: 2a 20 45 6e 64 20 6f 66 20 55 6e 69 71 75 65 20 * End of Unique
7ac0: 46 69 6c 65 20 49 44 20 55 74 69 6c 69 74 79 20 File ID Utility
7ad0: 55 73 65 64 20 42 79 20 56 78 57 6f 72 6b 73 20 Used By VxWorks
7ae0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7af0: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
7b00: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b10: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b20: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b30: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
7b40: 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .../************
7b50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b80: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7b90: 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
7ba0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 50 ************** P
7bb0: 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f osix Advisory Lo
7bc0: 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a cking **********
7bd0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7be0: 2a 2a 0a 2a 2a 0a 2a 2a 20 50 4f 53 49 58 20 61 **.**.** POSIX a
7bf0: 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20 61 72 dvisory locks ar
7c00: 65 20 62 72 6f 6b 65 6e 20 62 79 20 64 65 73 69 e broken by desi
7c10: 67 6e 2e 20 20 41 4e 53 49 20 53 54 44 20 31 30 gn. ANSI STD 10
7c20: 30 33 2e 31 20 28 31 39 39 36 29 0a 2a 2a 20 73 03.1 (1996).** s
7c30: 65 63 74 69 6f 6e 20 36 2e 35 2e 32 2e 32 20 6c ection 6.5.2.2 l
7c40: 69 6e 65 73 20 34 38 33 20 74 68 72 6f 75 67 68 ines 483 through
7c50: 20 34 39 30 20 73 70 65 63 69 66 79 20 74 68 61 490 specify tha
7c60: 74 20 77 68 65 6e 20 61 20 70 72 6f 63 65 73 73 t when a process
7c70: 0a 2a 2a 20 73 65 74 73 20 6f 72 20 63 6c 65 61 .** sets or clea
7c80: 72 73 20 61 20 6c 6f 63 6b 2c 20 74 68 61 74 20 rs a lock, that
7c90: 6f 70 65 72 61 74 69 6f 6e 20 6f 76 65 72 72 69 operation overri
7ca0: 64 65 73 20 61 6e 79 20 70 72 69 6f 72 20 6c 6f des any prior lo
7cb0: 63 6b 73 20 73 65 74 0a 2a 2a 20 62 79 20 74 68 cks set.** by th
7cc0: 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 20 e same process.
7cd0: 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 70 It does not exp
7ce0: 6c 69 63 69 74 6c 79 20 73 61 79 20 73 6f 2c 20 licitly say so,
7cf0: 62 75 74 20 74 68 69 73 20 69 6d 70 6c 69 65 73 but this implies
7d00: 0a 2a 2a 20 74 68 61 74 20 69 74 20 6f 76 65 72 .** that it over
7d10: 72 69 64 65 73 20 6c 6f 63 6b 73 20 73 65 74 20 rides locks set
7d20: 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 by the same proc
7d30: 65 73 73 20 75 73 69 6e 67 20 61 20 64 69 66 66 ess using a diff
7d40: 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 65 erent.** file de
7d50: 73 63 72 69 70 74 6f 72 2e 20 20 43 6f 6e 73 69 scriptor. Consi
7d60: 64 65 72 20 74 68 69 73 20 74 65 73 74 20 63 61 der this test ca
7d70: 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 se:.**.**
7d80: 69 6e 74 20 66 64 31 20 3d 20 6f 70 65 6e 28 22 int fd1 = open("
7d90: 2e 2f 66 69 6c 65 31 22 2c 20 4f 5f 52 44 57 52 ./file1", O_RDWR
7da0: 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 34 29 3b |O_CREAT, 0644);
7db0: 0a 2a 2a 20 20 20 20 20 20 20 69 6e 74 20 66 64 .** int fd
7dc0: 32 20 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 2 = open("./file
7dd0: 32 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 2", O_RDWR|O_CRE
7de0: 41 54 2c 20 30 36 34 34 29 3b 0a 2a 2a 0a 2a 2a AT, 0644);.**.**
7df0: 20 53 75 70 70 6f 73 65 20 2e 2f 66 69 6c 65 31 Suppose ./file1
7e00: 20 61 6e 64 20 2e 2f 66 69 6c 65 32 20 61 72 65 and ./file2 are
7e10: 20 72 65 61 6c 6c 79 20 74 68 65 20 73 61 6d 65 really the same
7e20: 20 66 69 6c 65 20 28 62 65 63 61 75 73 65 0a 2a file (because.*
7e30: 2a 20 6f 6e 65 20 69 73 20 61 20 68 61 72 64 20 * one is a hard
7e40: 6f 72 20 73 79 6d 62 6f 6c 69 63 20 6c 69 6e 6b or symbolic link
7e50: 20 74 6f 20 74 68 65 20 6f 74 68 65 72 29 20 74 to the other) t
7e60: 68 65 6e 20 69 66 20 79 6f 75 20 73 65 74 0a 2a hen if you set.*
7e70: 2a 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c * an exclusive l
7e80: 6f 63 6b 20 6f 6e 20 66 64 31 2c 20 74 68 65 6e ock on fd1, then
7e90: 20 74 72 79 20 74 6f 20 67 65 74 20 61 6e 20 65 try to get an e
7ea0: 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a xclusive lock.**
7eb0: 20 6f 6e 20 66 64 32 2c 20 69 74 20 77 6f 72 6b on fd2, it work
7ec0: 73 2e 20 20 49 20 77 6f 75 6c 64 20 68 61 76 65 s. I would have
7ed0: 20 65 78 70 65 63 74 65 64 20 74 68 65 20 73 65 expected the se
7ee0: 63 6f 6e 64 20 6c 6f 63 6b 20 74 6f 0a 2a 2a 20 cond lock to.**
7ef0: 66 61 69 6c 20 73 69 6e 63 65 20 74 68 65 72 65 fail since there
7f00: 20 77 61 73 20 61 6c 72 65 61 64 79 20 61 20 6c was already a l
7f10: 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 ock on the file
7f20: 64 75 65 20 74 6f 20 66 64 31 2e 0a 2a 2a 20 42 due to fd1..** B
7f30: 75 74 20 6e 6f 74 20 73 6f 2e 20 20 53 69 6e 63 ut not so. Sinc
7f40: 65 20 62 6f 74 68 20 6c 6f 63 6b 73 20 63 61 6d e both locks cam
7f50: 65 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 e from the same
7f60: 70 72 6f 63 65 73 73 2c 20 74 68 65 0a 2a 2a 20 process, the.**
7f70: 73 65 63 6f 6e 64 20 6f 76 65 72 72 69 64 65 73 second overrides
7f80: 20 74 68 65 20 66 69 72 73 74 2c 20 65 76 65 6e the first, even
7f90: 20 74 68 6f 75 67 68 20 74 68 65 79 20 77 65 72 though they wer
7fa0: 65 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 0a 2a e on different.*
7fb0: 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f * file descripto
7fc0: 72 73 20 6f 70 65 6e 65 64 20 6f 6e 20 64 69 66 rs opened on dif
7fd0: 66 65 72 65 6e 74 20 66 69 6c 65 20 6e 61 6d 65 ferent file name
7fe0: 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 65 s..**.** This me
7ff0: 61 6e 73 20 74 68 61 74 20 77 65 20 63 61 6e 6e ans that we cann
8000: 6f 74 20 75 73 65 20 50 4f 53 49 58 20 6c 6f 63 ot use POSIX loc
8010: 6b 73 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a ks to synchroniz
8020: 65 20 66 69 6c 65 20 61 63 63 65 73 73 0a 2a 2a e file access.**
8030: 20 61 6d 6f 6e 67 20 63 6f 6d 70 65 74 69 6e 67 among competing
8040: 20 74 68 72 65 61 64 73 20 6f 66 20 74 68 65 20 threads of the
8050: 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 20 20 50 same process. P
8060: 4f 53 49 58 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 OSIX locks will
8070: 77 6f 72 6b 20 66 69 6e 65 0a 2a 2a 20 74 6f 20 work fine.** to
8080: 73 79 6e 63 68 72 6f 6e 69 7a 65 20 61 63 63 65 synchronize acce
8090: 73 73 20 66 6f 72 20 74 68 72 65 61 64 73 20 69 ss for threads i
80a0: 6e 20 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 n separate proce
80b0: 73 73 65 73 2c 20 62 75 74 20 6e 6f 74 0a 2a 2a sses, but not.**
80c0: 20 74 68 72 65 61 64 73 20 77 69 74 68 69 6e 20 threads within
80d0: 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 the same process
80e0: 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20 ..**.** To work
80f0: 61 72 6f 75 6e 64 20 74 68 65 20 70 72 6f 62 6c around the probl
8100: 65 6d 2c 20 53 51 4c 69 74 65 20 68 61 73 20 74 em, SQLite has t
8110: 6f 20 6d 61 6e 61 67 65 20 66 69 6c 65 20 6c 6f o manage file lo
8120: 63 6b 73 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a cks internally.*
8130: 2a 20 6f 6e 20 69 74 73 20 6f 77 6e 2e 20 20 57 * on its own. W
8140: 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20 64 61 henever a new da
8150: 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 tabase is opened
8160: 2c 20 77 65 20 68 61 76 65 20 74 6f 20 66 69 6e , we have to fin
8170: 64 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 d the.** specifi
8180: 63 20 69 6e 6f 64 65 20 6f 66 20 74 68 65 20 64 c inode of the d
8190: 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 74 68 atabase file (th
81a0: 65 20 69 6e 6f 64 65 20 69 73 20 64 65 74 65 72 e inode is deter
81b0: 6d 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 mined by the.**
81c0: 73 74 5f 64 65 76 20 61 6e 64 20 73 74 5f 69 6e st_dev and st_in
81d0: 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 o fields of the
81e0: 73 74 61 74 20 73 74 72 75 63 74 75 72 65 20 74 stat structure t
81f0: 68 61 74 20 66 73 74 61 74 28 29 20 66 69 6c 6c hat fstat() fill
8200: 73 20 69 6e 29 0a 2a 2a 20 61 6e 64 20 63 68 65 s in).** and che
8210: 63 6b 20 66 6f 72 20 6c 6f 63 6b 73 20 61 6c 72 ck for locks alr
8220: 65 61 64 79 20 65 78 69 73 74 69 6e 67 20 6f 6e eady existing on
8230: 20 74 68 61 74 20 69 6e 6f 64 65 2e 20 20 57 68 that inode. Wh
8240: 65 6e 20 6c 6f 63 6b 73 20 61 72 65 0a 2a 2a 20 en locks are.**
8250: 63 72 65 61 74 65 64 20 6f 72 20 72 65 6d 6f 76 created or remov
8260: 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f 20 6c ed, we have to l
8270: 6f 6f 6b 20 61 74 20 6f 75 72 20 6f 77 6e 20 69 ook at our own i
8280: 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 20 6f nternal record o
8290: 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 73 20 74 f the.** locks t
82a0: 6f 20 73 65 65 20 69 66 20 61 6e 6f 74 68 65 72 o see if another
82b0: 20 74 68 72 65 61 64 20 68 61 73 20 70 72 65 76 thread has prev
82c0: 69 6f 75 73 6c 79 20 73 65 74 20 61 20 6c 6f 63 iously set a loc
82d0: 6b 20 6f 6e 20 74 68 61 74 20 73 61 6d 65 0a 2a k on that same.*
82e0: 2a 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 28 * inode..**.** (
82f0: 41 73 69 64 65 3a 20 54 68 65 20 75 73 65 20 6f Aside: The use o
8300: 66 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 73 20 f inode numbers
8310: 61 73 20 75 6e 69 71 75 65 20 49 44 73 20 64 6f as unique IDs do
8320: 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 56 es not work on V
8330: 78 57 6f 72 6b 73 2e 0a 2a 2a 20 46 6f 72 20 56 xWorks..** For V
8340: 78 57 6f 72 6b 73 2c 20 77 65 20 68 61 76 65 20 xWorks, we have
8350: 74 6f 20 75 73 65 20 74 68 65 20 61 6c 74 65 72 to use the alter
8360: 6e 61 74 69 76 65 20 75 6e 69 71 75 65 20 49 44 native unique ID
8370: 20 73 79 73 74 65 6d 20 62 61 73 65 64 20 6f 6e system based on
8380: 0a 2a 2a 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 .** canonical fi
8390: 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6d 70 6c 65 lename and imple
83a0: 6d 65 6e 74 65 64 20 69 6e 20 74 68 65 20 70 72 mented in the pr
83b0: 65 76 69 6f 75 73 20 64 69 76 69 73 69 6f 6e 2e evious division.
83c0: 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ).**.** The sqli
83d0: 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75 te3_file structu
83e0: 72 65 20 66 6f 72 20 50 4f 53 49 58 20 69 73 20 re for POSIX is
83f0: 6e 6f 20 6c 6f 6e 67 65 72 20 6a 75 73 74 20 61 no longer just a
8400: 6e 20 69 6e 74 65 67 65 72 20 66 69 6c 65 0a 2a n integer file.*
8410: 2a 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 49 * descriptor. I
8420: 74 20 69 73 20 6e 6f 77 20 61 20 73 74 72 75 63 t is now a struc
8430: 74 75 72 65 20 74 68 61 74 20 68 6f 6c 64 73 20 ture that holds
8440: 74 68 65 20 69 6e 74 65 67 65 72 20 66 69 6c 65 the integer file
8450: 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 61 .** descriptor a
8460: 6e 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 nd a pointer to
8470: 61 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 a structure that
8480: 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 69 describes the i
8490: 6e 74 65 72 6e 61 6c 0a 2a 2a 20 6c 6f 63 6b 73 nternal.** locks
84a0: 20 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f on the correspo
84b0: 6e 64 69 6e 67 20 69 6e 6f 64 65 2e 20 20 54 68 nding inode. Th
84c0: 65 72 65 20 69 73 20 6f 6e 65 20 6c 6f 63 6b 69 ere is one locki
84d0: 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 ng structure.**
84e0: 70 65 72 20 69 6e 6f 64 65 2c 20 73 6f 20 69 66 per inode, so if
84f0: 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 65 20 the same inode
8500: 69 73 20 6f 70 65 6e 65 64 20 74 77 69 63 65 2c is opened twice,
8510: 20 62 6f 74 68 20 75 6e 69 78 46 69 6c 65 20 73 both unixFile s
8520: 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 70 6f 69 tructures.** poi
8530: 6e 74 20 74 6f 20 74 68 65 20 73 61 6d 65 20 6c nt to the same l
8540: 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 ocking structure
8550: 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 73 . The locking s
8560: 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 0a 2a tructure keeps.*
8570: 2a 20 61 20 72 65 66 65 72 65 6e 63 65 20 63 6f * a reference co
8580: 75 6e 74 20 28 73 6f 20 77 65 20 77 69 6c 6c 20 unt (so we will
8590: 6b 6e 6f 77 20 77 68 65 6e 20 74 6f 20 64 65 6c know when to del
85a0: 65 74 65 20 69 74 29 20 61 6e 64 20 61 20 22 63 ete it) and a "c
85b0: 6e 74 22 0a 2a 2a 20 66 69 65 6c 64 20 74 68 61 nt".** field tha
85c0: 74 20 74 65 6c 6c 73 20 75 73 20 69 74 73 20 69 t tells us its i
85d0: 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 nternal lock sta
85e0: 74 75 73 2e 20 20 63 6e 74 3d 3d 30 20 6d 65 61 tus. cnt==0 mea
85f0: 6e 73 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 69 ns the.** file i
8600: 73 20 75 6e 6c 6f 63 6b 65 64 2e 20 20 63 6e 74 s unlocked. cnt
8610: 3d 3d 2d 31 20 6d 65 61 6e 73 20 74 68 65 20 66 ==-1 means the f
8620: 69 6c 65 20 68 61 73 20 61 6e 20 65 78 63 6c 75 ile has an exclu
8630: 73 69 76 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 63 6e sive lock..** cn
8640: 74 3e 30 20 6d 65 61 6e 73 20 74 68 65 72 65 20 t>0 means there
8650: 61 72 65 20 63 6e 74 20 73 68 61 72 65 64 20 6c are cnt shared l
8660: 6f 63 6b 73 20 6f 6e 20 74 68 65 20 66 69 6c 65 ocks on the file
8670: 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 61 74 74 65 ..**.** Any atte
8680: 6d 70 74 20 74 6f 20 6c 6f 63 6b 20 6f 72 20 75 mpt to lock or u
8690: 6e 6c 6f 63 6b 20 61 20 66 69 6c 65 20 66 69 72 nlock a file fir
86a0: 73 74 20 63 68 65 63 6b 73 20 74 68 65 20 6c 6f st checks the lo
86b0: 63 6b 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 cking.** structu
86c0: 72 65 2e 20 20 54 68 65 20 66 63 6e 74 6c 28 29 re. The fcntl()
86d0: 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 69 73 20 system call is
86e0: 6f 6e 6c 79 20 69 6e 76 6f 6b 65 64 20 74 6f 20 only invoked to
86f0: 73 65 74 20 61 20 0a 2a 2a 20 50 4f 53 49 58 20 set a .** POSIX
8700: 6c 6f 63 6b 20 69 66 20 74 68 65 20 69 6e 74 65 lock if the inte
8710: 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 72 75 63 74 rnal lock struct
8720: 75 72 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 ure transitions
8730: 62 65 74 77 65 65 6e 0a 2a 2a 20 61 20 6c 6f 63 between.** a loc
8740: 6b 65 64 20 61 6e 64 20 61 6e 20 75 6e 6c 6f 63 ked and an unloc
8750: 6b 65 64 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a ked state..**.**
8760: 20 42 75 74 20 77 61 69 74 3a 20 20 74 68 65 72 But wait: ther
8770: 65 20 61 72 65 20 79 65 74 20 6d 6f 72 65 20 70 e are yet more p
8780: 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 50 4f 53 roblems with POS
8790: 49 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b IX advisory lock
87a0: 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 79 6f 75 20 s..**.** If you
87b0: 63 6c 6f 73 65 20 61 20 66 69 6c 65 20 64 65 73 close a file des
87c0: 63 72 69 70 74 6f 72 20 74 68 61 74 20 70 6f 69 criptor that poi
87d0: 6e 74 73 20 74 6f 20 61 20 66 69 6c 65 20 74 68 nts to a file th
87e0: 61 74 20 68 61 73 20 6c 6f 63 6b 73 2c 0a 2a 2a at has locks,.**
87f0: 20 61 6c 6c 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 all locks on th
8800: 61 74 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 at file that are
8810: 20 6f 77 6e 65 64 20 62 79 20 74 68 65 20 63 75 owned by the cu
8820: 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20 61 72 rrent process ar
8830: 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 2e 20 20 e.** released.
8840: 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 To work around t
8850: 68 69 73 20 70 72 6f 62 6c 65 6d 2c 20 65 61 63 his problem, eac
8860: 68 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 h unixInodeInfo
8870: 6f 62 6a 65 63 74 0a 2a 2a 20 6d 61 69 6e 74 61 object.** mainta
8880: 69 6e 73 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 ins a count of t
8890: 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 65 6e he number of pen
88a0: 64 69 6e 67 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 ding locks on th
88b0: 61 20 69 6e 6f 64 65 2e 0a 2a 2a 20 57 68 65 6e a inode..** When
88c0: 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d an attempt is m
88d0: 61 64 65 20 74 6f 20 63 6c 6f 73 65 20 61 6e 20 ade to close an
88e0: 75 6e 69 78 46 69 6c 65 2c 20 69 66 20 74 68 65 unixFile, if the
88f0: 72 65 20 61 72 65 0a 2a 2a 20 6f 74 68 65 72 20 re are.** other
8900: 75 6e 69 78 46 69 6c 65 20 6f 70 65 6e 20 6f 6e unixFile open on
8910: 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 65 20 the same inode
8920: 74 68 61 74 20 61 72 65 20 68 6f 6c 64 69 6e 67 that are holding
8930: 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 61 6c 6c locks, the call
8940: 0a 2a 2a 20 74 6f 20 63 6c 6f 73 65 28 29 20 74 .** to close() t
8950: 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
8960: 6f 72 20 69 73 20 64 65 66 65 72 72 65 64 20 75 or is deferred u
8970: 6e 74 69 6c 20 61 6c 6c 20 6f 66 20 74 68 65 20 ntil all of the
8980: 6c 6f 63 6b 73 20 63 6c 65 61 72 2e 0a 2a 2a 20 locks clear..**
8990: 54 68 65 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 The unixInodeInf
89a0: 6f 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 o structure keep
89b0: 73 20 61 20 6c 69 73 74 20 6f 66 20 66 69 6c 65 s a list of file
89c0: 20 64 65 73 63 72 69 70 74 6f 72 73 20 74 68 61 descriptors tha
89d0: 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 62 65 20 t need to.** be
89e0: 63 6c 6f 73 65 64 20 61 6e 64 20 74 68 61 74 20 closed and that
89f0: 6c 69 73 74 20 69 73 20 77 61 6c 6b 65 64 20 28 list is walked (
8a00: 61 6e 64 20 63 6c 65 61 72 65 64 29 20 77 68 65 and cleared) whe
8a10: 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 0a n the last lock.
8a20: 2a 2a 20 63 6c 65 61 72 73 2e 0a 2a 2a 0a 2a 2a ** clears..**.**
8a30: 20 59 65 74 20 61 6e 6f 74 68 65 72 20 70 72 6f Yet another pro
8a40: 62 6c 65 6d 3a 20 20 4c 69 6e 75 78 54 68 72 65 blem: LinuxThre
8a50: 61 64 73 20 64 6f 20 6e 6f 74 20 70 6c 61 79 20 ads do not play
8a60: 77 65 6c 6c 20 77 69 74 68 20 70 6f 73 69 78 20 well with posix
8a70: 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 4d 61 6e locks..**.** Man
8a80: 79 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 y older versions
8a90: 20 6f 66 20 6c 69 6e 75 78 20 75 73 65 20 74 68 of linux use th
8aa0: 65 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 6c e LinuxThreads l
8ab0: 69 62 72 61 72 79 20 77 68 69 63 68 20 69 73 0a ibrary which is.
8ac0: 2a 2a 20 6e 6f 74 20 70 6f 73 69 78 20 63 6f 6d ** not posix com
8ad0: 70 6c 69 61 6e 74 2e 20 20 55 6e 64 65 72 20 4c pliant. Under L
8ae0: 69 6e 75 78 54 68 72 65 61 64 73 2c 20 61 20 6c inuxThreads, a l
8af0: 6f 63 6b 20 63 72 65 61 74 65 64 20 62 79 20 74 ock created by t
8b00: 68 72 65 61 64 0a 2a 2a 20 41 20 63 61 6e 6e 6f hread.** A canno
8b10: 74 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6f 72 t be modified or
8b20: 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 61 overridden by a
8b30: 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 different threa
8b40: 64 20 42 2e 0a 2a 2a 20 4f 6e 6c 79 20 74 68 72 d B..** Only thr
8b50: 65 61 64 20 41 20 63 61 6e 20 6d 6f 64 69 66 79 ead A can modify
8b60: 20 74 68 65 20 6c 6f 63 6b 2e 20 20 4c 6f 63 6b the lock. Lock
8b70: 69 6e 67 20 62 65 68 61 76 69 6f 72 20 69 73 20 ing behavior is
8b80: 63 6f 72 72 65 63 74 0a 2a 2a 20 69 66 20 74 68 correct.** if th
8b90: 65 20 61 70 70 6c 69 61 74 69 6f 6e 20 75 73 65 e appliation use
8ba0: 73 20 74 68 65 20 6e 65 77 65 72 20 4e 61 74 69 s the newer Nati
8bb0: 76 65 20 50 6f 73 69 78 20 54 68 72 65 61 64 20 ve Posix Thread
8bc0: 4c 69 62 72 61 72 79 20 28 4e 50 54 4c 29 0a 2a Library (NPTL).*
8bd0: 2a 20 6f 6e 20 6c 69 6e 75 78 20 2d 20 77 69 74 * on linux - wit
8be0: 68 20 4e 50 54 4c 20 61 20 6c 6f 63 6b 20 63 72 h NPTL a lock cr
8bf0: 65 61 74 65 64 20 62 79 20 74 68 72 65 61 64 20 eated by thread
8c00: 41 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 6c A can override l
8c10: 6f 63 6b 73 0a 2a 2a 20 69 6e 20 74 68 72 65 61 ocks.** in threa
8c20: 64 20 42 2e 20 20 42 75 74 20 74 68 65 72 65 20 d B. But there
8c30: 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b 6e 6f is no way to kno
8c40: 77 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d w at compile-tim
8c50: 65 20 77 68 69 63 68 0a 2a 2a 20 74 68 72 65 61 e which.** threa
8c60: 64 69 6e 67 20 6c 69 62 72 61 72 79 20 69 73 20 ding library is
8c70: 62 65 69 6e 67 20 75 73 65 64 2e 20 20 53 6f 20 being used. So
8c80: 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 there is no way
8c90: 74 6f 20 6b 6e 6f 77 20 61 74 0a 2a 2a 20 63 6f to know at.** co
8ca0: 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 65 74 68 mpile-time wheth
8cb0: 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 er or not thread
8cc0: 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 A can override
8cd0: 6c 6f 63 6b 73 20 6f 6e 20 74 68 72 65 61 64 20 locks on thread
8ce0: 42 2e 0a 2a 2a 20 4f 6e 65 20 68 61 73 20 74 6f B..** One has to
8cf0: 20 64 6f 20 61 20 72 75 6e 2d 74 69 6d 65 20 63 do a run-time c
8d00: 68 65 63 6b 20 74 6f 20 64 69 73 63 6f 76 65 72 heck to discover
8d10: 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 the behavior of
8d20: 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 the.** current
8d30: 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 53 process..**.** S
8d40: 51 4c 69 74 65 20 75 73 65 64 20 74 6f 20 73 75 QLite used to su
8d50: 70 70 6f 72 74 20 4c 69 6e 75 78 54 68 72 65 61 pport LinuxThrea
8d60: 64 73 2e 20 20 42 75 74 20 73 75 70 70 6f 72 74 ds. But support
8d70: 20 66 6f 72 20 4c 69 6e 75 78 54 68 72 65 61 64 for LinuxThread
8d80: 73 0a 2a 2a 20 77 61 73 20 64 72 6f 70 70 65 64 s.** was dropped
8d90: 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 beginning with
8da0: 76 65 72 73 69 6f 6e 20 33 2e 37 2e 30 2e 20 20 version 3.7.0.
8db0: 53 51 4c 69 74 65 20 77 69 6c 6c 20 73 74 69 6c SQLite will stil
8dc0: 6c 20 77 6f 72 6b 20 77 69 74 68 0a 2a 2a 20 4c l work with.** L
8dd0: 69 6e 75 78 54 68 72 65 61 64 73 20 70 72 6f 76 inuxThreads prov
8de0: 69 64 65 64 20 74 68 61 74 20 28 31 29 20 74 68 ided that (1) th
8df0: 65 72 65 20 69 73 20 6e 6f 20 6d 6f 72 65 20 74 ere is no more t
8e00: 68 61 6e 20 6f 6e 65 20 63 6f 6e 6e 65 63 74 69 han one connecti
8e10: 6f 6e 20 0a 2a 2a 20 70 65 72 20 64 61 74 61 62 on .** per datab
8e20: 61 73 65 20 66 69 6c 65 20 69 6e 20 74 68 65 20 ase file in the
8e30: 73 61 6d 65 20 70 72 6f 63 65 73 73 20 61 6e 64 same process and
8e40: 20 28 32 29 20 64 61 74 61 62 61 73 65 20 63 6f (2) database co
8e50: 6e 6e 65 63 74 69 6f 6e 73 0a 2a 2a 20 64 6f 20 nnections.** do
8e60: 6e 6f 74 20 6d 6f 76 65 20 61 63 72 6f 73 73 20 not move across
8e70: 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 0a 2f 2a 0a threads..*/../*.
8e80: 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
8e90: 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
8ea0: 73 74 72 75 63 74 75 72 65 20 73 65 72 76 65 73 structure serves
8eb0: 20 61 73 20 74 68 65 20 6b 65 79 20 75 73 65 64 as the key used
8ec0: 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 20 .** to locate a
8ed0: 70 61 72 74 69 63 75 6c 61 72 20 75 6e 69 78 49 particular unixI
8ee0: 6e 6f 64 65 49 6e 66 6f 20 6f 62 6a 65 63 74 2e nodeInfo object.
8ef0: 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 46 .*/.struct unixF
8f00: 69 6c 65 49 64 20 7b 0a 20 20 64 65 76 5f 74 20 ileId {. dev_t
8f10: 64 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 dev;
8f20: 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63 65 20 /* Device
8f30: 6e 75 6d 62 65 72 20 2a 2f 0a 23 69 66 20 4f 53 number */.#if OS
8f40: 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 _VXWORKS. struc
8f50: 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 t vxworksFileId
8f60: 2a 70 49 64 3b 20 20 2f 2a 20 55 6e 69 71 75 65 *pId; /* Unique
8f70: 20 66 69 6c 65 20 49 44 20 66 6f 72 20 76 78 77 file ID for vxw
8f80: 6f 72 6b 73 2e 20 2a 2f 0a 23 65 6c 73 65 0a 20 orks. */.#else.
8f90: 20 69 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20 20 20 ino_t ino;
8fa0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
8fb0: 49 6e 6f 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0a Inode number */.
8fc0: 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a #endif.};../*.**
8fd0: 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
8fe0: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
8ff0: 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 ructure is alloc
9000: 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 6f 70 ated for each op
9010: 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 4f 72 en.** inode. Or
9020: 2c 20 6f 6e 20 4c 69 6e 75 78 54 68 72 65 61 64 , on LinuxThread
9030: 73 2c 20 74 68 65 72 65 20 69 73 20 6f 6e 65 20 s, there is one
9040: 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 of these structu
9050: 72 65 73 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 res for.** each
9060: 69 6e 6f 64 65 20 6f 70 65 6e 65 64 20 62 79 20 inode opened by
9070: 65 61 63 68 20 74 68 72 65 61 64 2e 0a 2a 2a 0a each thread..**.
9080: 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 6f 64 ** A single inod
9090: 65 20 63 61 6e 20 68 61 76 65 20 6d 75 6c 74 69 e can have multi
90a0: 70 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 ple file descrip
90b0: 74 6f 72 73 2c 20 73 6f 20 65 61 63 68 20 75 6e tors, so each un
90c0: 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 ixFile.** struct
90d0: 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 ure contains a p
90e0: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 ointer to an ins
90f0: 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 tance of this ob
9100: 6a 65 63 74 20 61 6e 64 20 74 68 69 73 0a 2a 2a ject and this.**
9110: 20 6f 62 6a 65 63 74 20 6b 65 65 70 73 20 61 20 object keeps a
9120: 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d count of the num
9130: 62 65 72 20 6f 66 20 75 6e 69 78 46 69 6c 65 20 ber of unixFile
9140: 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 74 2e 0a pointing to it..
9150: 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 49 6e */.struct unixIn
9160: 6f 64 65 49 6e 66 6f 20 7b 0a 20 20 73 74 72 75 odeInfo {. stru
9170: 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69 ct unixFileId fi
9180: 6c 65 49 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 leId; /* T
9190: 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f he lookup key */
91a0: 0a 20 20 69 6e 74 20 6e 53 68 61 72 65 64 3b 20 . int nShared;
91b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
91c0: 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
91d0: 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 68 65 6c SHARED locks hel
91e0: 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 d */. unsigned
91f0: 63 68 61 72 20 65 46 69 6c 65 4c 6f 63 6b 3b 20 char eFileLock;
9200: 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 /* One of
9210: 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 SHARED_LOCK, RE
9220: 53 45 52 56 45 44 5f 4c 4f 43 4b 20 65 74 63 2e SERVED_LOCK etc.
9230: 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
9240: 68 61 72 20 62 50 72 6f 63 65 73 73 4c 6f 63 6b har bProcessLock
9250: 3b 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 63 6c ; /* An excl
9260: 75 73 69 76 65 20 70 72 6f 63 65 73 73 20 6c 6f usive process lo
9270: 63 6b 20 69 73 20 68 65 6c 64 20 2a 2f 0a 20 20 ck is held */.
9280: 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 int nRef;
9290: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
92a0: 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 /* Number of poi
92b0: 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 nters to this st
92c0: 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 75 6e 69 ructure */. uni
92d0: 78 53 68 6d 4e 6f 64 65 20 2a 70 53 68 6d 4e 6f xShmNode *pShmNo
92e0: 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 de; /*
92f0: 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 61 73 Shared memory as
9300: 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
9310: 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 69 6e is inode */. in
9320: 74 20 6e 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 t nLock;
9330: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
9340: 20 4e 75 6d 62 65 72 20 6f 66 20 6f 75 74 73 74 Number of outst
9350: 61 6e 64 69 6e 67 20 66 69 6c 65 20 6c 6f 63 6b anding file lock
9360: 73 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 73 65 s */. UnixUnuse
9370: 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20 20 20 dFd *pUnused;
9380: 20 20 20 20 20 20 20 2f 2a 20 55 6e 75 73 65 64 /* Unused
9390: 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
93a0: 73 20 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 s to close */.
93b0: 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 unixInodeInfo *p
93c0: 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 Next;
93d0: 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20 75 /* List of all u
93e0: 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 6f 62 6a nixInodeInfo obj
93f0: 65 63 74 73 20 2a 2f 0a 20 20 75 6e 69 78 49 6e ects */. unixIn
9400: 6f 64 65 49 6e 66 6f 20 2a 70 50 72 65 76 3b 20 odeInfo *pPrev;
9410: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 20 /*
9420: 2e 2e 2e 2e 20 64 6f 75 62 6c 79 20 6c 69 6e 6b .... doubly link
9430: 65 64 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 ed */.#if SQLITE
9440: 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
9450: 53 54 59 4c 45 0a 20 20 75 6e 73 69 67 6e 65 64 STYLE. unsigned
9460: 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73 68 61 72 65 long long share
9470: 64 42 79 74 65 3b 20 20 2f 2a 20 66 6f 72 20 41 dByte; /* for A
9480: 46 50 20 73 69 6d 75 6c 61 74 65 64 20 73 68 61 FP simulated sha
9490: 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 23 65 6e 64 red lock */.#end
94a0: 69 66 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b if.#if OS_VXWORK
94b0: 53 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 3b S. sem_t *pSem;
94c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
94d0: 20 20 20 20 2f 2a 20 4e 61 6d 65 64 20 50 4f 53 /* Named POS
94e0: 49 58 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a IX semaphore */.
94f0: 20 20 63 68 61 72 20 61 53 65 6d 4e 61 6d 65 5b char aSemName[
9500: 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 32 5d 3b MAX_PATHNAME+2];
9510: 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 61 /* Name of tha
9520: 74 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 23 t semaphore */.#
9530: 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
9540: 41 20 6c 69 73 74 73 20 6f 66 20 61 6c 6c 20 75 A lists of all u
9550: 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 6f 62 6a nixInodeInfo obj
9560: 65 63 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ects..*/.static
9570: 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 69 unixInodeInfo *i
9580: 6e 6f 64 65 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f nodeList = 0;../
9590: 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e *.**.** This fun
95a0: 63 74 69 6f 6e 20 2d 20 75 6e 69 78 4c 6f 67 45 ction - unixLogE
95b0: 72 72 6f 72 41 74 4c 69 6e 65 28 29 2c 20 69 73 rrorAtLine(), is
95c0: 20 6f 6e 6c 79 20 65 76 65 72 20 63 61 6c 6c 65 only ever calle
95d0: 64 20 76 69 61 20 74 68 65 20 6d 61 63 72 6f 0a d via the macro.
95e0: 2a 2a 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 28 ** unixLogError(
95f0: 29 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 )..**.** It is i
9600: 6e 76 6f 6b 65 64 20 61 66 74 65 72 20 61 6e 20 nvoked after an
9610: 65 72 72 6f 72 20 6f 63 63 75 72 73 20 69 6e 20 error occurs in
9620: 61 6e 20 4f 53 20 66 75 6e 63 74 69 6f 6e 20 61 an OS function a
9630: 6e 64 20 65 72 72 6e 6f 20 68 61 73 20 62 65 65 nd errno has bee
9640: 6e 0a 2a 2a 20 73 65 74 2e 20 49 74 20 6c 6f 67 n.** set. It log
9650: 73 20 61 20 6d 65 73 73 61 67 65 20 75 73 69 6e s a message usin
9660: 67 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29 20 g sqlite3_log()
9670: 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 containing the c
9680: 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 0a urrent value of.
9690: 2a 2a 20 65 72 72 6e 6f 20 61 6e 64 2c 20 69 66 ** errno and, if
96a0: 20 70 6f 73 73 69 62 6c 65 2c 20 74 68 65 20 68 possible, the h
96b0: 75 6d 61 6e 2d 72 65 61 64 61 62 6c 65 20 65 71 uman-readable eq
96c0: 75 69 76 61 6c 65 6e 74 20 66 72 6f 6d 20 73 74 uivalent from st
96d0: 72 65 72 72 6f 72 28 29 20 6f 72 0a 2a 2a 20 73 rerror() or.** s
96e0: 74 72 65 72 72 6f 72 5f 72 28 29 2e 0a 2a 2a 0a trerror_r()..**.
96f0: 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 ** The first arg
9700: 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 ument passed to
9710: 74 68 65 20 6d 61 63 72 6f 20 73 68 6f 75 6c 64 the macro should
9720: 20 62 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f be the error co
9730: 64 65 20 74 68 61 74 0a 2a 2a 20 77 69 6c 6c 20 de that.** will
9740: 62 65 20 72 65 74 75 72 6e 65 64 20 74 6f 20 53 be returned to S
9750: 51 4c 69 74 65 20 28 65 2e 67 2e 20 53 51 4c 49 QLite (e.g. SQLI
9760: 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 2c TE_IOERR_DELETE,
9770: 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e SQLITE_CANTOPEN
9780: 29 2e 20 0a 2a 2a 20 54 68 65 20 74 77 6f 20 73 ). .** The two s
9790: 75 62 73 65 71 75 65 6e 74 20 61 72 67 75 6d 65 ubsequent argume
97a0: 6e 74 73 20 73 68 6f 75 6c 64 20 62 65 20 74 68 nts should be th
97b0: 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 4f 53 e name of the OS
97c0: 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 0a 2a function that.*
97d0: 2a 20 66 61 69 6c 65 64 20 28 65 2e 67 2e 20 22 * failed (e.g. "
97e0: 75 6e 6c 69 6e 6b 22 2c 20 22 6f 70 65 6e 22 29 unlink", "open")
97f0: 20 61 6e 64 20 74 68 65 20 61 73 73 6f 63 69 61 and the associa
9800: 74 65 64 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 ted file-system
9810: 70 61 74 68 2c 0a 2a 2a 20 69 66 20 61 6e 79 2e path,.** if any.
9820: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 75 6e 69 78 .*/.#define unix
9830: 4c 6f 67 45 72 72 6f 72 28 61 2c 62 2c 63 29 20 LogError(a,b,c)
9840: 20 20 20 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 unixLogError
9850: 41 74 4c 69 6e 65 28 61 2c 62 2c 63 2c 5f 5f 4c AtLine(a,b,c,__L
9860: 49 4e 45 5f 5f 29 0a 73 74 61 74 69 63 20 69 6e INE__).static in
9870: 74 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 41 74 t unixLogErrorAt
9880: 4c 69 6e 65 28 0a 20 20 69 6e 74 20 65 72 72 63 Line(. int errc
9890: 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ode,
98a0: 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 /* SQLit
98b0: 65 20 65 72 72 6f 72 20 63 6f 64 65 20 2a 2f 0a e error code */.
98c0: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
98d0: 75 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 unc,
98e0: 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 4f 53 20 /* Name of OS
98f0: 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 66 61 function that fa
9900: 69 6c 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 iled */. const
9910: 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 char *zPath,
9920: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c /* Fil
9930: 65 20 70 61 74 68 20 61 73 73 6f 63 69 61 74 65 e path associate
9940: 64 20 77 69 74 68 20 65 72 72 6f 72 20 2a 2f 0a d with error */.
9950: 20 20 69 6e 74 20 69 4c 69 6e 65 20 20 20 20 20 int iLine
9960: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9970: 20 20 2f 2a 20 53 6f 75 72 63 65 20 6c 69 6e 65 /* Source line
9980: 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20 65 72 number where er
9990: 72 6f 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a ror occurred */.
99a0: 29 7b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 3b ){. char *zErr;
99b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
99c0: 20 20 20 20 20 2f 2a 20 4d 65 73 73 61 67 65 20 /* Message
99d0: 66 72 6f 6d 20 73 74 72 65 72 72 6f 72 28 29 20 from strerror()
99e0: 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20 2a 2f or equivalent */
99f0: 0a 20 20 69 6e 74 20 69 45 72 72 6e 6f 20 3d 20 . int iErrno =
9a00: 65 72 72 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 errno;
9a10: 20 20 20 2f 2a 20 53 61 76 65 64 20 73 79 73 63 /* Saved sysc
9a20: 61 6c 6c 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 all error number
9a30: 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 */.. /* If thi
9a40: 73 20 69 73 20 6e 6f 74 20 61 20 74 68 72 65 61 s is not a threa
9a50: 64 73 61 66 65 20 62 75 69 6c 64 20 28 53 51 4c dsafe build (SQL
9a60: 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d ITE_THREADSAFE==
9a70: 30 29 2c 20 74 68 65 6e 20 75 73 65 0a 20 20 2a 0), then use. *
9a80: 2a 20 74 68 65 20 73 74 72 65 72 72 6f 72 28 29 * the strerror()
9a90: 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 6f 62 74 function to obt
9aa0: 61 69 6e 20 74 68 65 20 68 75 6d 61 6e 2d 72 65 ain the human-re
9ab0: 61 64 61 62 6c 65 20 65 72 72 6f 72 20 6d 65 73 adable error mes
9ac0: 73 61 67 65 0a 20 20 2a 2a 20 65 71 75 69 76 61 sage. ** equiva
9ad0: 6c 65 6e 74 20 74 6f 20 65 72 72 6e 6f 2e 20 4f lent to errno. O
9ae0: 74 68 65 72 77 69 73 65 2c 20 75 73 65 20 73 74 therwise, use st
9af0: 72 65 72 72 6f 72 5f 72 28 29 2e 0a 20 20 2a 2f rerror_r().. */
9b00: 20 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 .#if SQLITE_THR
9b10: 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e EADSAFE && defin
9b20: 65 64 28 48 41 56 45 5f 53 54 52 45 52 52 4f 52 ed(HAVE_STRERROR
9b30: 5f 52 29 0a 20 20 63 68 61 72 20 61 45 72 72 5b _R). char aErr[
9b40: 38 30 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61 45 80];. memset(aE
9b50: 72 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 45 rr, 0, sizeof(aE
9b60: 72 72 29 29 3b 0a 20 20 7a 45 72 72 20 3d 20 61 rr));. zErr = a
9b70: 45 72 72 3b 0a 0a 20 20 2f 2a 20 49 66 20 53 54 Err;.. /* If ST
9b80: 52 45 52 52 4f 52 5f 52 5f 43 48 41 52 5f 50 20 RERROR_R_CHAR_P
9b90: 28 73 65 74 20 62 79 20 61 75 74 6f 63 6f 6e 66 (set by autoconf
9ba0: 20 73 63 72 69 70 74 73 29 20 6f 72 20 5f 5f 55 scripts) or __U
9bb0: 53 45 5f 47 4e 55 20 69 73 20 64 65 66 69 6e 65 SE_GNU is define
9bc0: 64 2c 0a 20 20 2a 2a 20 61 73 73 75 6d 65 20 74 d,. ** assume t
9bd0: 68 61 74 20 74 68 65 20 73 79 73 74 65 6d 20 70 hat the system p
9be0: 72 6f 76 69 64 65 73 20 74 68 65 20 47 4e 55 20 rovides the GNU
9bf0: 76 65 72 73 69 6f 6e 20 6f 66 20 73 74 72 65 72 version of strer
9c00: 72 6f 72 5f 72 28 29 20 74 68 61 74 0a 20 20 2a ror_r() that. *
9c10: 2a 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e * returns a poin
9c20: 74 65 72 20 74 6f 20 61 20 62 75 66 66 65 72 20 ter to a buffer
9c30: 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 65 containing the e
9c40: 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 20 54 68 rror message. Th
9c50: 61 74 20 70 6f 69 6e 74 65 72 20 0a 20 20 2a 2a at pointer . **
9c60: 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 45 may point to aE
9c70: 72 72 5b 5d 2c 20 6f 72 20 69 74 20 6d 61 79 20 rr[], or it may
9c80: 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20 73 74 point to some st
9c90: 61 74 69 63 20 73 74 6f 72 61 67 65 20 73 6f 6d atic storage som
9ca0: 65 77 68 65 72 65 2e 20 0a 20 20 2a 2a 20 4f 74 ewhere. . ** Ot
9cb0: 68 65 72 77 69 73 65 2c 20 61 73 73 75 6d 65 20 herwise, assume
9cc0: 74 68 61 74 20 74 68 65 20 73 79 73 74 65 6d 20 that the system
9cd0: 70 72 6f 76 69 64 65 73 20 74 68 65 20 50 4f 53 provides the POS
9ce0: 49 58 20 76 65 72 73 69 6f 6e 20 6f 66 20 0a 20 IX version of .
9cf0: 20 2a 2a 20 73 74 72 65 72 72 6f 72 5f 72 28 29 ** strerror_r()
9d00: 2c 20 77 68 69 63 68 20 61 6c 77 61 79 73 20 77 , which always w
9d10: 72 69 74 65 73 20 61 6e 20 65 72 72 6f 72 20 6d rites an error m
9d20: 65 73 73 61 67 65 20 69 6e 74 6f 20 61 45 72 72 essage into aErr
9d30: 5b 5d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 [].. **. ** If
9d40: 20 74 68 65 20 63 6f 64 65 20 69 6e 63 6f 72 72 the code incorr
9d50: 65 63 74 6c 79 20 61 73 73 75 6d 65 73 20 74 68 ectly assumes th
9d60: 61 74 20 69 74 20 69 73 20 74 68 65 20 50 4f 53 at it is the POS
9d70: 49 58 20 76 65 72 73 69 6f 6e 20 74 68 61 74 20 IX version that
9d80: 69 73 0a 20 20 2a 2a 20 61 76 61 69 6c 61 62 6c is. ** availabl
9d90: 65 2c 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 e, the error mes
9da0: 73 61 67 65 20 77 69 6c 6c 20 6f 66 74 65 6e 20 sage will often
9db0: 62 65 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 be an empty stri
9dc0: 6e 67 2e 20 4e 6f 74 20 61 0a 20 20 2a 2a 20 68 ng. Not a. ** h
9dd0: 75 67 65 20 70 72 6f 62 6c 65 6d 2e 20 49 6e 63 uge problem. Inc
9de0: 6f 72 72 65 63 74 6c 79 20 63 6f 6e 63 6c 75 64 orrectly conclud
9df0: 69 6e 67 20 74 68 61 74 20 74 68 65 20 47 4e 55 ing that the GNU
9e00: 20 76 65 72 73 69 6f 6e 20 69 73 20 61 76 61 69 version is avai
9e10: 6c 61 62 6c 65 20 0a 20 20 2a 2a 20 63 6f 75 6c lable . ** coul
9e20: 64 20 6c 65 61 64 20 74 6f 20 61 20 73 65 67 66 d lead to a segf
9e30: 61 75 6c 74 20 74 68 6f 75 67 68 2e 0a 20 20 2a ault though.. *
9e40: 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 54 /.#if defined(ST
9e50: 52 45 52 52 4f 52 5f 52 5f 43 48 41 52 5f 50 29 RERROR_R_CHAR_P)
9e60: 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 55 53 || defined(__US
9e70: 45 5f 47 4e 55 29 0a 20 20 7a 45 72 72 20 3d 20 E_GNU). zErr =
9e80: 0a 23 20 65 6e 64 69 66 0a 20 20 73 74 72 65 72 .# endif. strer
9e90: 72 6f 72 5f 72 28 69 45 72 72 6e 6f 2c 20 61 45 ror_r(iErrno, aE
9ea0: 72 72 2c 20 73 69 7a 65 6f 66 28 61 45 72 72 29 rr, sizeof(aErr)
9eb0: 2d 31 29 3b 0a 0a 23 65 6c 69 66 20 53 51 4c 49 -1);..#elif SQLI
9ec0: 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 TE_THREADSAFE.
9ed0: 2f 2a 20 54 68 69 73 20 69 73 20 61 20 74 68 72 /* This is a thr
9ee0: 65 61 64 73 61 66 65 20 62 75 69 6c 64 2c 20 62 eadsafe build, b
9ef0: 75 74 20 73 74 72 65 72 72 6f 72 5f 72 28 29 20 ut strerror_r()
9f00: 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 is not available
9f10: 2e 20 2a 2f 0a 20 20 7a 45 72 72 20 3d 20 22 22 . */. zErr = ""
9f20: 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 4e 6f 6e ;.#else. /* Non
9f30: 2d 74 68 72 65 61 64 73 61 66 65 20 62 75 69 6c -threadsafe buil
9f40: 64 2c 20 75 73 65 20 73 74 72 65 72 72 6f 72 28 d, use strerror(
9f50: 29 2e 20 2a 2f 0a 20 20 7a 45 72 72 20 3d 20 73 ). */. zErr = s
9f60: 74 72 65 72 72 6f 72 28 69 45 72 72 6e 6f 29 3b trerror(iErrno);
9f70: 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 7a .#endif.. if( z
9f80: 50 61 74 68 3d 3d 30 20 29 20 7a 50 61 74 68 20 Path==0 ) zPath
9f90: 3d 20 22 22 3b 0a 20 20 73 71 6c 69 74 65 33 5f = "";. sqlite3_
9fa0: 6c 6f 67 28 65 72 72 63 6f 64 65 2c 0a 20 20 20 log(errcode,.
9fb0: 20 20 20 22 6f 73 5f 75 6e 69 78 2e 63 3a 25 64 "os_unix.c:%d
9fc0: 3a 20 28 25 64 29 20 25 73 28 25 73 29 20 2d 20 : (%d) %s(%s) -
9fd0: 25 73 22 2c 0a 20 20 20 20 20 20 69 4c 69 6e 65 %s",. iLine
9fe0: 2c 20 69 45 72 72 6e 6f 2c 20 7a 46 75 6e 63 2c , iErrno, zFunc,
9ff0: 20 7a 50 61 74 68 2c 20 7a 45 72 72 0a 20 20 29 zPath, zErr. )
a000: 3b 0a 0a 20 20 72 65 74 75 72 6e 20 65 72 72 63 ;.. return errc
a010: 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c ode;.}../*.** Cl
a020: 6f 73 65 20 61 20 66 69 6c 65 20 64 65 73 63 72 ose a file descr
a030: 69 70 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 iptor..**.** We
a040: 61 73 73 75 6d 65 20 74 68 61 74 20 63 6c 6f 73 assume that clos
a050: 65 28 29 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 e() almost alway
a060: 73 20 77 6f 72 6b 73 2c 20 73 69 6e 63 65 20 69 s works, since i
a070: 74 20 69 73 20 6f 6e 6c 79 20 69 6e 20 61 0a 2a t is only in a.*
a080: 2a 20 76 65 72 79 20 73 69 63 6b 20 61 70 70 6c * very sick appl
a090: 69 63 61 74 69 6f 6e 20 6f 72 20 6f 6e 20 61 20 ication or on a
a0a0: 76 65 72 79 20 73 69 63 6b 20 70 6c 61 74 66 6f very sick platfo
a0b0: 72 6d 20 74 68 61 74 20 69 74 20 6d 69 67 68 74 rm that it might
a0c0: 20 66 61 69 6c 2e 0a 2a 2a 20 49 66 20 69 74 20 fail..** If it
a0d0: 64 6f 65 73 20 66 61 69 6c 2c 20 73 69 6d 70 6c does fail, simpl
a0e0: 79 20 6c 65 61 6b 20 74 68 65 20 66 69 6c 65 20 y leak the file
a0f0: 64 65 73 63 72 69 70 74 6f 72 2c 20 62 75 74 20 descriptor, but
a100: 64 6f 20 6c 6f 67 20 74 68 65 0a 2a 2a 20 65 72 do log the.** er
a110: 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 ror..**.** Note
a120: 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 73 that it is not s
a130: 61 66 65 20 74 6f 20 72 65 74 72 79 20 63 6c 6f afe to retry clo
a140: 73 65 28 29 20 61 66 74 65 72 20 45 49 4e 54 52 se() after EINTR
a150: 20 73 69 6e 63 65 20 74 68 65 0a 2a 2a 20 66 69 since the.** fi
a160: 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6d 69 le descriptor mi
a170: 67 68 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 ght have already
a180: 20 62 65 65 6e 20 72 65 75 73 65 64 20 62 79 20 been reused by
a190: 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 2e 0a another thread..
a1a0: 2a 2a 20 53 6f 20 77 65 20 64 6f 6e 27 74 20 65 ** So we don't e
a1b0: 76 65 6e 20 74 72 79 20 74 6f 20 72 65 63 6f 76 ven try to recov
a1c0: 65 72 20 66 72 6f 6d 20 61 6e 20 45 49 4e 54 52 er from an EINTR
a1d0: 2e 20 20 4a 75 73 74 20 6c 6f 67 20 74 68 65 20 . Just log the
a1e0: 65 72 72 6f 72 0a 2a 2a 20 61 6e 64 20 6d 6f 76 error.** and mov
a1f0: 65 20 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 e on..*/.static
a200: 76 6f 69 64 20 72 6f 62 75 73 74 5f 63 6c 6f 73 void robust_clos
a210: 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c e(unixFile *pFil
a220: 65 2c 20 69 6e 74 20 68 2c 20 69 6e 74 20 6c 69 e, int h, int li
a230: 6e 65 6e 6f 29 7b 0a 20 20 69 66 28 20 6f 73 43 neno){. if( osC
a240: 6c 6f 73 65 28 68 29 20 29 7b 0a 20 20 20 20 75 lose(h) ){. u
a250: 6e 69 78 4c 6f 67 45 72 72 6f 72 41 74 4c 69 6e nixLogErrorAtLin
a260: 65 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 e(SQLITE_IOERR_C
a270: 4c 4f 53 45 2c 20 22 63 6c 6f 73 65 22 2c 0a 20 LOSE, "close",.
a280: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a290: 20 20 20 20 20 20 70 46 69 6c 65 20 3f 20 70 46 pFile ? pF
a2a0: 69 6c 65 2d 3e 7a 50 61 74 68 20 3a 20 30 2c 20 ile->zPath : 0,
a2b0: 6c 69 6e 65 6e 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a lineno);. }.}..
a2c0: 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 46 /*.** Set the pF
a2d0: 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 2e 20 ile->lastErrno.
a2e0: 20 44 6f 20 74 68 69 73 20 69 6e 20 61 20 73 75 Do this in a su
a2f0: 62 72 6f 75 74 69 6e 65 20 61 73 20 74 68 61 74 broutine as that
a300: 20 70 72 6f 76 69 64 65 73 0a 2a 2a 20 61 20 63 provides.** a c
a310: 6f 6e 76 65 6e 69 65 6e 74 20 70 6c 61 63 65 20 onvenient place
a320: 74 6f 20 73 65 74 20 61 20 62 72 65 61 6b 70 6f to set a breakpo
a330: 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 int..*/.static v
a340: 6f 69 64 20 73 74 6f 72 65 4c 61 73 74 45 72 72 oid storeLastErr
a350: 6e 6f 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 no(unixFile *pFi
a360: 6c 65 2c 20 69 6e 74 20 65 72 72 6f 72 29 7b 0a le, int error){.
a370: 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
a380: 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 7d 0a 0a 2f no = error;.}../
a390: 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 66 *.** Close all f
a3a0: 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 ile descriptors
a3b0: 61 63 63 75 6d 75 61 74 65 64 20 69 6e 20 74 68 accumuated in th
a3c0: 65 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 2d e unixInodeInfo-
a3d0: 3e 70 55 6e 75 73 65 64 20 6c 69 73 74 2e 0a 2a >pUnused list..*
a3e0: 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 / .static void c
a3f0: 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28 75 losePendingFds(u
a400: 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b nixFile *pFile){
a410: 0a 20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f . unixInodeInfo
a420: 20 2a 70 49 6e 6f 64 65 20 3d 20 70 46 69 6c 65 *pInode = pFile
a430: 2d 3e 70 49 6e 6f 64 65 3b 0a 20 20 55 6e 69 78 ->pInode;. Unix
a440: 55 6e 75 73 65 64 46 64 20 2a 70 3b 0a 20 20 55 UnusedFd *p;. U
a450: 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 4e 65 nixUnusedFd *pNe
a460: 78 74 3b 0a 20 20 66 6f 72 28 70 3d 70 49 6e 6f xt;. for(p=pIno
a470: 64 65 2d 3e 70 55 6e 75 73 65 64 3b 20 70 3b 20 de->pUnused; p;
a480: 70 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e p=pNext){. pN
a490: 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a ext = p->pNext;.
a4a0: 20 20 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 robust_close
a4b0: 28 70 46 69 6c 65 2c 20 70 2d 3e 66 64 2c 20 5f (pFile, p->fd, _
a4c0: 5f 4c 49 4e 45 5f 5f 29 3b 0a 20 20 20 20 73 71 _LINE__);. sq
a4d0: 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 lite3_free(p);.
a4e0: 20 7d 0a 20 20 70 49 6e 6f 64 65 2d 3e 70 55 6e }. pInode->pUn
a4f0: 75 73 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a used = 0;.}../*.
a500: 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69 ** Release a uni
a510: 78 49 6e 6f 64 65 49 6e 66 6f 20 73 74 72 75 63 xInodeInfo struc
a520: 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 ture previously
a530: 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e allocated by fin
a540: 64 49 6e 6f 64 65 49 6e 66 6f 28 29 2e 0a 2a 2a dInodeInfo()..**
a550: 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 65 6e .** The mutex en
a560: 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 tered using the
a570: 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
a580: 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 function must b
a590: 65 20 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 e held.** when t
a5a0: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
a5b0: 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 called..*/.stati
a5c0: 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 49 6e c void releaseIn
a5d0: 6f 64 65 49 6e 66 6f 28 75 6e 69 78 46 69 6c 65 odeInfo(unixFile
a5e0: 20 2a 70 46 69 6c 65 29 7b 0a 20 20 75 6e 69 78 *pFile){. unix
a5f0: 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 49 6e 6f 64 InodeInfo *pInod
a600: 65 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 e = pFile->pInod
a610: 65 3b 0a 20 20 61 73 73 65 72 74 28 20 75 6e 69 e;. assert( uni
a620: 78 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a xMutexHeld() );.
a630: 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 49 6e if( ALWAYS(pIn
a640: 6f 64 65 29 20 29 7b 0a 20 20 20 20 70 49 6e 6f ode) ){. pIno
a650: 64 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 de->nRef--;.
a660: 69 66 28 20 70 49 6e 6f 64 65 2d 3e 6e 52 65 66 if( pInode->nRef
a670: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ==0 ){. ass
a680: 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 70 53 68 ert( pInode->pSh
a690: 6d 4e 6f 64 65 3d 3d 30 20 29 3b 0a 20 20 20 20 mNode==0 );.
a6a0: 20 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64 closePendingFd
a6b0: 73 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 s(pFile);.
a6c0: 69 66 28 20 70 49 6e 6f 64 65 2d 3e 70 50 72 65 if( pInode->pPre
a6d0: 76 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 v ){. ass
a6e0: 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 70 50 72 ert( pInode->pPr
a6f0: 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 49 6e 6f 64 ev->pNext==pInod
a700: 65 20 29 3b 0a 20 20 20 20 20 20 20 20 70 49 6e e );. pIn
a710: 6f 64 65 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 ode->pPrev->pNex
a720: 74 20 3d 20 70 49 6e 6f 64 65 2d 3e 70 4e 65 78 t = pInode->pNex
a730: 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a t;. }else{.
a740: 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
a750: 69 6e 6f 64 65 4c 69 73 74 3d 3d 70 49 6e 6f 64 inodeList==pInod
a760: 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 6f e );. ino
a770: 64 65 4c 69 73 74 20 3d 20 70 49 6e 6f 64 65 2d deList = pInode-
a780: 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a >pNext;. }.
a790: 20 20 20 20 20 20 69 66 28 20 70 49 6e 6f 64 65 if( pInode
a7a0: 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 ->pNext ){.
a7b0: 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 6f 64 assert( pInod
a7c0: 65 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d e->pNext->pPrev=
a7d0: 3d 70 49 6e 6f 64 65 20 29 3b 0a 20 20 20 20 20 =pInode );.
a7e0: 20 20 20 70 49 6e 6f 64 65 2d 3e 70 4e 65 78 74 pInode->pNext
a7f0: 2d 3e 70 50 72 65 76 20 3d 20 70 49 6e 6f 64 65 ->pPrev = pInode
a800: 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d ->pPrev;. }
a810: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 . sqlite3_f
a820: 72 65 65 28 70 49 6e 6f 64 65 29 3b 0a 20 20 20 ree(pInode);.
a830: 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
a840: 47 69 76 65 6e 20 61 20 66 69 6c 65 20 64 65 73 Given a file des
a850: 63 72 69 70 74 6f 72 2c 20 6c 6f 63 61 74 65 20 criptor, locate
a860: 74 68 65 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 the unixInodeInf
a870: 6f 20 6f 62 6a 65 63 74 20 74 68 61 74 0a 2a 2a o object that.**
a880: 20 64 65 73 63 72 69 62 65 73 20 74 68 61 74 20 describes that
a890: 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e file descriptor.
a8a0: 20 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 6f Create a new o
a8b0: 6e 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e ne if necessary.
a8c0: 20 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 The.** return
a8d0: 76 61 6c 75 65 20 6d 69 67 68 74 20 62 65 20 75 value might be u
a8e0: 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 66 20 ninitialized if
a8f0: 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e an error occurs.
a900: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 .**.** The mutex
a910: 20 65 6e 74 65 72 65 64 20 75 73 69 6e 67 20 74 entered using t
a920: 68 65 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 he unixEnterMute
a930: 78 28 29 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 x() function mus
a940: 74 20 62 65 20 68 65 6c 64 0a 2a 2a 20 77 68 65 t be held.** whe
a950: 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
a960: 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a is called..**.**
a970: 20 52 65 74 75 72 6e 20 61 6e 20 61 70 70 72 6f Return an appro
a980: 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64 priate error cod
a990: 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
a9a0: 20 66 69 6e 64 49 6e 6f 64 65 49 6e 66 6f 28 0a findInodeInfo(.
a9b0: 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
a9c0: 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
a9d0: 20 2f 2a 20 55 6e 69 78 20 66 69 6c 65 20 77 69 /* Unix file wi
a9e0: 74 68 20 66 69 6c 65 20 64 65 73 63 20 75 73 65 th file desc use
a9f0: 64 20 69 6e 20 74 68 65 20 6b 65 79 20 2a 2f 0a d in the key */.
aa00: 20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 unixInodeInfo
aa10: 2a 2a 70 70 49 6e 6f 64 65 20 20 20 20 20 20 20 **ppInode
aa20: 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 75 /* Return the u
aa30: 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 6f 62 6a nixInodeInfo obj
aa40: 65 63 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 ect here */.){.
aa50: 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
aa60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aa70: 2f 2a 20 53 79 73 74 65 6d 20 63 61 6c 6c 20 72 /* System call r
aa80: 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
aa90: 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 int fd;
aaa0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
aab0: 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 * The file descr
aac0: 69 70 74 6f 72 20 66 6f 72 20 70 46 69 6c 65 20 iptor for pFile
aad0: 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 */. struct unix
aae0: 46 69 6c 65 49 64 20 66 69 6c 65 49 64 3b 20 20 FileId fileId;
aaf0: 20 20 20 20 2f 2a 20 4c 6f 6f 6b 75 70 20 6b 65 /* Lookup ke
ab00: 79 20 66 6f 72 20 74 68 65 20 75 6e 69 78 49 6e y for the unixIn
ab10: 6f 64 65 49 6e 66 6f 20 2a 2f 0a 20 20 73 74 72 odeInfo */. str
ab20: 75 63 74 20 73 74 61 74 20 73 74 61 74 62 75 66 uct stat statbuf
ab30: 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c ; /* L
ab40: 6f 77 2d 6c 65 76 65 6c 20 66 69 6c 65 20 69 6e ow-level file in
ab50: 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 formation */. u
ab60: 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 49 nixInodeInfo *pI
ab70: 6e 6f 64 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a node = 0; /*
ab80: 20 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 49 Candidate unixI
ab90: 6e 6f 64 65 49 6e 66 6f 20 6f 62 6a 65 63 74 20 nodeInfo object
aba0: 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 75 6e */.. assert( un
abb0: 69 78 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b ixMutexHeld() );
abc0: 0a 0a 20 20 2f 2a 20 47 65 74 20 6c 6f 77 2d 6c .. /* Get low-l
abd0: 65 76 65 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e evel information
abe0: 20 61 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20 about the file
abf0: 74 68 61 74 20 77 65 20 63 61 6e 20 75 73 65 64 that we can used
ac00: 20 74 6f 0a 20 20 2a 2a 20 63 72 65 61 74 65 20 to. ** create
ac10: 61 20 75 6e 69 71 75 65 20 6e 61 6d 65 20 66 6f a unique name fo
ac20: 72 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f r the file.. */
ac30: 0a 20 20 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68 . fd = pFile->h
ac40: 3b 0a 20 20 72 63 20 3d 20 6f 73 46 73 74 61 74 ;. rc = osFstat
ac50: 28 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a (fd, &statbuf);.
ac60: 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 if( rc!=0 ){.
ac70: 20 20 20 73 74 6f 72 65 4c 61 73 74 45 72 72 6e storeLastErrn
ac80: 6f 28 70 46 69 6c 65 2c 20 65 72 72 6e 6f 29 3b o(pFile, errno);
ac90: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 45 4f 56 .#if defined(EOV
aca0: 45 52 46 4c 4f 57 29 20 26 26 20 64 65 66 69 6e ERFLOW) && defin
acb0: 65 64 28 53 51 4c 49 54 45 5f 44 49 53 41 42 4c ed(SQLITE_DISABL
acc0: 45 5f 4c 46 53 29 0a 20 20 20 20 69 66 28 20 70 E_LFS). if( p
acd0: 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3d File->lastErrno=
ace0: 3d 45 4f 56 45 52 46 4c 4f 57 20 29 20 72 65 74 =EOVERFLOW ) ret
acf0: 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 urn SQLITE_NOLFS
ad00: 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74 ;.#endif. ret
ad10: 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
ad20: 3b 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 5f 5f ;. }..#ifdef __
ad30: 41 50 50 4c 45 5f 5f 0a 20 20 2f 2a 20 4f 6e 20 APPLE__. /* On
ad40: 4f 53 20 58 20 6f 6e 20 61 6e 20 6d 73 64 6f 73 OS X on an msdos
ad50: 20 66 69 6c 65 73 79 73 74 65 6d 2c 20 74 68 65 filesystem, the
ad60: 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 20 69 73 inode number is
ad70: 20 72 65 70 6f 72 74 65 64 0a 20 20 2a 2a 20 69 reported. ** i
ad80: 6e 63 6f 72 72 65 63 74 6c 79 20 66 6f 72 20 7a ncorrectly for z
ad90: 65 72 6f 2d 73 69 7a 65 20 66 69 6c 65 73 2e 20 ero-size files.
ada0: 20 53 65 65 20 74 69 63 6b 65 74 20 23 33 32 36 See ticket #326
adb0: 30 2e 20 20 54 6f 20 77 6f 72 6b 0a 20 20 2a 2a 0. To work. **
adc0: 20 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72 6f around this pro
add0: 62 6c 65 6d 20 28 77 65 20 63 6f 6e 73 69 64 65 blem (we conside
ade0: 72 20 69 74 20 61 20 62 75 67 20 69 6e 20 4f 53 r it a bug in OS
adf0: 20 58 2c 20 6e 6f 74 20 53 51 4c 69 74 65 29 0a X, not SQLite).
ae00: 20 20 2a 2a 20 77 65 20 61 6c 77 61 79 73 20 69 ** we always i
ae10: 6e 63 72 65 61 73 65 20 74 68 65 20 66 69 6c 65 ncrease the file
ae20: 20 73 69 7a 65 20 74 6f 20 31 20 62 79 20 77 72 size to 1 by wr
ae30: 69 74 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 62 iting a single b
ae40: 79 74 65 0a 20 20 2a 2a 20 70 72 69 6f 72 20 74 yte. ** prior t
ae50: 6f 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 o accessing the
ae60: 69 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20 20 54 inode number. T
ae70: 68 65 20 6f 6e 65 20 62 79 74 65 20 77 72 69 74 he one byte writ
ae80: 74 65 6e 20 69 73 0a 20 20 2a 2a 20 61 6e 20 41 ten is. ** an A
ae90: 53 43 49 49 20 27 53 27 20 63 68 61 72 61 63 74 SCII 'S' charact
aea0: 65 72 20 77 68 69 63 68 20 61 6c 73 6f 20 68 61 er which also ha
aeb0: 70 70 65 6e 73 20 74 6f 20 62 65 20 74 68 65 20 ppens to be the
aec0: 66 69 72 73 74 20 62 79 74 65 0a 20 20 2a 2a 20 first byte. **
aed0: 69 6e 20 74 68 65 20 68 65 61 64 65 72 20 6f 66 in the header of
aee0: 20 65 76 65 72 79 20 53 51 4c 69 74 65 20 64 61 every SQLite da
aef0: 74 61 62 61 73 65 2e 20 20 49 6e 20 74 68 69 73 tabase. In this
af00: 20 77 61 79 2c 20 69 66 20 74 68 65 72 65 0a 20 way, if there.
af10: 20 2a 2a 20 69 73 20 61 20 72 61 63 65 20 63 6f ** is a race co
af20: 6e 64 69 74 69 6f 6e 20 73 75 63 68 20 74 68 61 ndition such tha
af30: 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 t another thread
af40: 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 6f 70 has already pop
af50: 75 6c 61 74 65 64 0a 20 20 2a 2a 20 74 68 65 20 ulated. ** the
af60: 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 first page of th
af70: 65 20 64 61 74 61 62 61 73 65 2c 20 6e 6f 20 64 e database, no d
af80: 61 6d 61 67 65 20 69 73 20 64 6f 6e 65 2e 0a 20 amage is done..
af90: 20 2a 2f 0a 20 20 69 66 28 20 73 74 61 74 62 75 */. if( statbu
afa0: 66 2e 73 74 5f 73 69 7a 65 3d 3d 30 20 26 26 20 f.st_size==0 &&
afb0: 28 70 46 69 6c 65 2d 3e 66 73 46 6c 61 67 73 20 (pFile->fsFlags
afc0: 26 20 53 51 4c 49 54 45 5f 46 53 46 4c 41 47 53 & SQLITE_FSFLAGS
afd0: 5f 49 53 5f 4d 53 44 4f 53 29 21 3d 30 20 29 7b _IS_MSDOS)!=0 ){
afe0: 0a 20 20 20 20 64 6f 7b 20 72 63 20 3d 20 6f 73 . do{ rc = os
aff0: 57 72 69 74 65 28 66 64 2c 20 22 53 22 2c 20 31 Write(fd, "S", 1
b000: 29 3b 20 7d 77 68 69 6c 65 28 20 72 63 3c 30 20 ); }while( rc<0
b010: 26 26 20 65 72 72 6e 6f 3d 3d 45 49 4e 54 52 20 && errno==EINTR
b020: 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 31 );. if( rc!=1
b030: 20 29 7b 0a 20 20 20 20 20 20 73 74 6f 72 65 4c ){. storeL
b040: 61 73 74 45 72 72 6e 6f 28 70 46 69 6c 65 2c 20 astErrno(pFile,
b050: 65 72 72 6e 6f 29 3b 0a 20 20 20 20 20 20 72 65 errno);. re
b060: 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
b070: 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 R;. }. rc
b080: 3d 20 6f 73 46 73 74 61 74 28 66 64 2c 20 26 73 = osFstat(fd, &s
b090: 74 61 74 62 75 66 29 3b 0a 20 20 20 20 69 66 28 tatbuf);. if(
b0a0: 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 rc!=0 ){.
b0b0: 73 74 6f 72 65 4c 61 73 74 45 72 72 6e 6f 28 70 storeLastErrno(p
b0c0: 46 69 6c 65 2c 20 65 72 72 6e 6f 29 3b 0a 20 20 File, errno);.
b0d0: 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
b0e0: 45 5f 49 4f 45 52 52 3b 0a 20 20 20 20 7d 0a 20 E_IOERR;. }.
b0f0: 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 6d 65 6d }.#endif.. mem
b100: 73 65 74 28 26 66 69 6c 65 49 64 2c 20 30 2c 20 set(&fileId, 0,
b110: 73 69 7a 65 6f 66 28 66 69 6c 65 49 64 29 29 3b sizeof(fileId));
b120: 0a 20 20 66 69 6c 65 49 64 2e 64 65 76 20 3d 20 . fileId.dev =
b130: 73 74 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a statbuf.st_dev;.
b140: 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 #if OS_VXWORKS.
b150: 20 66 69 6c 65 49 64 2e 70 49 64 20 3d 20 70 46 fileId.pId = pF
b160: 69 6c 65 2d 3e 70 49 64 3b 0a 23 65 6c 73 65 0a ile->pId;.#else.
b170: 20 20 66 69 6c 65 49 64 2e 69 6e 6f 20 3d 20 73 fileId.ino = s
b180: 74 61 74 62 75 66 2e 73 74 5f 69 6e 6f 3b 0a 23 tatbuf.st_ino;.#
b190: 65 6e 64 69 66 0a 20 20 70 49 6e 6f 64 65 20 3d endif. pInode =
b1a0: 20 69 6e 6f 64 65 4c 69 73 74 3b 0a 20 20 77 68 inodeList;. wh
b1b0: 69 6c 65 28 20 70 49 6e 6f 64 65 20 26 26 20 6d ile( pInode && m
b1c0: 65 6d 63 6d 70 28 26 66 69 6c 65 49 64 2c 20 26 emcmp(&fileId, &
b1d0: 70 49 6e 6f 64 65 2d 3e 66 69 6c 65 49 64 2c 20 pInode->fileId,
b1e0: 73 69 7a 65 6f 66 28 66 69 6c 65 49 64 29 29 20 sizeof(fileId))
b1f0: 29 7b 0a 20 20 20 20 70 49 6e 6f 64 65 20 3d 20 ){. pInode =
b200: 70 49 6e 6f 64 65 2d 3e 70 4e 65 78 74 3b 0a 20 pInode->pNext;.
b210: 20 7d 0a 20 20 69 66 28 20 70 49 6e 6f 64 65 3d }. if( pInode=
b220: 3d 30 20 29 7b 0a 20 20 20 20 70 49 6e 6f 64 65 =0 ){. pInode
b230: 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f = sqlite3_mallo
b240: 63 36 34 28 20 73 69 7a 65 6f 66 28 2a 70 49 6e c64( sizeof(*pIn
b250: 6f 64 65 29 20 29 3b 0a 20 20 20 20 69 66 28 20 ode) );. if(
b260: 70 49 6e 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20 pInode==0 ){.
b270: 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
b280: 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 20 20 _NOMEM_BKPT;.
b290: 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 49 }. memset(pI
b2a0: 6e 6f 64 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 node, 0, sizeof(
b2b0: 2a 70 49 6e 6f 64 65 29 29 3b 0a 20 20 20 20 6d *pInode));. m
b2c0: 65 6d 63 70 79 28 26 70 49 6e 6f 64 65 2d 3e 66 emcpy(&pInode->f
b2d0: 69 6c 65 49 64 2c 20 26 66 69 6c 65 49 64 2c 20 ileId, &fileId,
b2e0: 73 69 7a 65 6f 66 28 66 69 6c 65 49 64 29 29 3b sizeof(fileId));
b2f0: 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 52 65 . pInode->nRe
b300: 66 20 3d 20 31 3b 0a 20 20 20 20 70 49 6e 6f 64 f = 1;. pInod
b310: 65 2d 3e 70 4e 65 78 74 20 3d 20 69 6e 6f 64 65 e->pNext = inode
b320: 4c 69 73 74 3b 0a 20 20 20 20 70 49 6e 6f 64 65 List;. pInode
b330: 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 ->pPrev = 0;.
b340: 20 69 66 28 20 69 6e 6f 64 65 4c 69 73 74 20 29 if( inodeList )
b350: 20 69 6e 6f 64 65 4c 69 73 74 2d 3e 70 50 72 65 inodeList->pPre
b360: 76 20 3d 20 70 49 6e 6f 64 65 3b 0a 20 20 20 20 v = pInode;.
b370: 69 6e 6f 64 65 4c 69 73 74 20 3d 20 70 49 6e 6f inodeList = pIno
b380: 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 de;. }else{.
b390: 20 70 49 6e 6f 64 65 2d 3e 6e 52 65 66 2b 2b 3b pInode->nRef++;
b3a0: 0a 20 20 7d 0a 20 20 2a 70 70 49 6e 6f 64 65 20 . }. *ppInode
b3b0: 3d 20 70 49 6e 6f 64 65 3b 0a 20 20 72 65 74 75 = pInode;. retu
b3c0: 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
b3d0: 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 ./*.** Return TR
b3e0: 55 45 20 69 66 20 70 46 69 6c 65 20 68 61 73 20 UE if pFile has
b3f0: 62 65 65 6e 20 72 65 6e 61 6d 65 64 20 6f 72 20 been renamed or
b400: 75 6e 6c 69 6e 6b 65 64 20 73 69 6e 63 65 20 69 unlinked since i
b410: 74 20 77 61 73 20 66 69 72 73 74 20 6f 70 65 6e t was first open
b420: 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
b430: 74 20 66 69 6c 65 48 61 73 4d 6f 76 65 64 28 75 t fileHasMoved(u
b440: 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b nixFile *pFile){
b450: 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a .#if OS_VXWORKS.
b460: 20 20 72 65 74 75 72 6e 20 70 46 69 6c 65 2d 3e return pFile->
b470: 70 49 6e 6f 64 65 21 3d 30 20 26 26 20 70 46 69 pInode!=0 && pFi
b480: 6c 65 2d 3e 70 49 64 21 3d 70 46 69 6c 65 2d 3e le->pId!=pFile->
b490: 70 49 6e 6f 64 65 2d 3e 66 69 6c 65 49 64 2e 70 pInode->fileId.p
b4a0: 49 64 3b 0a 23 65 6c 73 65 0a 20 20 73 74 72 75 Id;.#else. stru
b4b0: 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 72 ct stat buf;. r
b4c0: 65 74 75 72 6e 20 70 46 69 6c 65 2d 3e 70 49 6e eturn pFile->pIn
b4d0: 6f 64 65 21 3d 30 20 26 26 0a 20 20 20 20 20 20 ode!=0 &&.
b4e0: 28 6f 73 53 74 61 74 28 70 46 69 6c 65 2d 3e 7a (osStat(pFile->z
b4f0: 50 61 74 68 2c 20 26 62 75 66 29 21 3d 30 20 7c Path, &buf)!=0 |
b500: 7c 20 62 75 66 2e 73 74 5f 69 6e 6f 21 3d 70 46 | buf.st_ino!=pF
b510: 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 66 69 6c ile->pInode->fil
b520: 65 49 64 2e 69 6e 6f 29 3b 0a 23 65 6e 64 69 66 eId.ino);.#endif
b530: 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b .}.../*.** Check
b540: 20 61 20 75 6e 69 78 46 69 6c 65 20 74 68 61 74 a unixFile that
b550: 20 69 73 20 61 20 64 61 74 61 62 61 73 65 2e 20 is a database.
b560: 20 56 65 72 69 66 79 20 74 68 65 20 66 6f 6c 6c Verify the foll
b570: 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 28 31 29 owing:.**.** (1)
b580: 20 54 68 65 72 65 20 69 73 20 65 78 61 63 74 6c There is exactl
b590: 79 20 6f 6e 65 20 68 61 72 64 20 6c 69 6e 6b 20 y one hard link
b5a0: 6f 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 28 on the file.** (
b5b0: 32 29 20 54 68 65 20 66 69 6c 65 20 69 73 20 6e 2) The file is n
b5c0: 6f 74 20 61 20 73 79 6d 62 6f 6c 69 63 20 6c 69 ot a symbolic li
b5d0: 6e 6b 0a 2a 2a 20 28 33 29 20 54 68 65 20 66 69 nk.** (3) The fi
b5e0: 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 le has not been
b5f0: 72 65 6e 61 6d 65 64 20 6f 72 20 75 6e 6c 69 6e renamed or unlin
b600: 6b 65 64 0a 2a 2a 0a 2a 2a 20 49 73 73 75 65 20 ked.**.** Issue
b610: 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 sqlite3_log(SQLI
b620: 54 45 5f 57 41 52 4e 49 4e 47 2c 2e 2e 2e 29 20 TE_WARNING,...)
b630: 6d 65 73 73 61 67 65 73 20 69 66 20 61 6e 79 74 messages if anyt
b640: 68 69 6e 67 20 69 73 20 6e 6f 74 20 72 69 67 68 hing is not righ
b650: 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 t..*/.static voi
b660: 64 20 76 65 72 69 66 79 44 62 46 69 6c 65 28 75 d verifyDbFile(u
b670: 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b nixFile *pFile){
b680: 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 20 62 . struct stat b
b690: 75 66 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 uf;. int rc;..
b6a0: 20 2f 2a 20 54 68 65 73 65 20 76 65 72 69 66 69 /* These verifi
b6b0: 63 61 74 69 6f 6e 73 20 6f 63 63 75 72 73 20 66 cations occurs f
b6c0: 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 or the main data
b6d0: 62 61 73 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20 69 base only */. i
b6e0: 66 28 20 70 46 69 6c 65 2d 3e 63 74 72 6c 46 6c f( pFile->ctrlFl
b6f0: 61 67 73 20 26 20 55 4e 49 58 46 49 4c 45 5f 4e ags & UNIXFILE_N
b700: 4f 4c 4f 43 4b 20 29 20 72 65 74 75 72 6e 3b 0a OLOCK ) return;.
b710: 0a 20 20 72 63 20 3d 20 6f 73 46 73 74 61 74 28 . rc = osFstat(
b720: 70 46 69 6c 65 2d 3e 68 2c 20 26 62 75 66 29 3b pFile->h, &buf);
b730: 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a . if( rc!=0 ){.
b740: 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 sqlite3_log(
b750: 53 51 4c 49 54 45 5f 57 41 52 4e 49 4e 47 2c 20 SQLITE_WARNING,
b760: 22 63 61 6e 6e 6f 74 20 66 73 74 61 74 20 64 62 "cannot fstat db
b770: 20 66 69 6c 65 20 25 73 22 2c 20 70 46 69 6c 65 file %s", pFile
b780: 2d 3e 7a 50 61 74 68 29 3b 0a 20 20 20 20 72 65 ->zPath);. re
b790: 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 turn;. }. if(
b7a0: 62 75 66 2e 73 74 5f 6e 6c 69 6e 6b 3d 3d 30 20 buf.st_nlink==0
b7b0: 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c ){. sqlite3_l
b7c0: 6f 67 28 53 51 4c 49 54 45 5f 57 41 52 4e 49 4e og(SQLITE_WARNIN
b7d0: 47 2c 20 22 66 69 6c 65 20 75 6e 6c 69 6e 6b 65 G, "file unlinke
b7e0: 64 20 77 68 69 6c 65 20 6f 70 65 6e 3a 20 25 73 d while open: %s
b7f0: 22 2c 20 70 46 69 6c 65 2d 3e 7a 50 61 74 68 29 ", pFile->zPath)
b800: 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ;. return;.
b810: 7d 0a 20 20 69 66 28 20 62 75 66 2e 73 74 5f 6e }. if( buf.st_n
b820: 6c 69 6e 6b 3e 31 20 29 7b 0a 20 20 20 20 73 71 link>1 ){. sq
b830: 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 54 45 lite3_log(SQLITE
b840: 5f 57 41 52 4e 49 4e 47 2c 20 22 6d 75 6c 74 69 _WARNING, "multi
b850: 70 6c 65 20 6c 69 6e 6b 73 20 74 6f 20 66 69 6c ple links to fil
b860: 65 3a 20 25 73 22 2c 20 70 46 69 6c 65 2d 3e 7a e: %s", pFile->z
b870: 50 61 74 68 29 3b 0a 20 20 20 20 72 65 74 75 72 Path);. retur
b880: 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 69 6c n;. }. if( fil
b890: 65 48 61 73 4d 6f 76 65 64 28 70 46 69 6c 65 29 eHasMoved(pFile)
b8a0: 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
b8b0: 6c 6f 67 28 53 51 4c 49 54 45 5f 57 41 52 4e 49 log(SQLITE_WARNI
b8c0: 4e 47 2c 20 22 66 69 6c 65 20 72 65 6e 61 6d 65 NG, "file rename
b8d0: 64 20 77 68 69 6c 65 20 6f 70 65 6e 3a 20 25 73 d while open: %s
b8e0: 22 2c 20 70 46 69 6c 65 2d 3e 7a 50 61 74 68 29 ", pFile->zPath)
b8f0: 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ;. return;.
b900: 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 }.}.../*.** This
b910: 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 routine checks
b920: 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 if there is a RE
b930: 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 SERVED lock held
b940: 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 on the specifie
b950: 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 d.** file by thi
b960: 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 s or any other p
b970: 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 rocess. If such
b980: 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 a lock is held,
b990: 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 set *pResOut.**
b9a0: 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 to a non-zero va
b9b0: 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 lue otherwise *p
b9c0: 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f ResOut is set to
b9d0: 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 zero. The retu
b9e0: 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 rn value.** is s
b9f0: 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 et to SQLITE_OK
ba00: 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 unless an I/O er
ba10: 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e ror occurs durin
ba20: 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e g lock checking.
ba30: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
ba40: 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 nixCheckReserved
ba50: 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
ba60: 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 e *id, int *pRes
ba70: 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d Out){. int rc =
ba80: 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e SQLITE_OK;. in
ba90: 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a t reserved = 0;.
baa0: 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
bab0: 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
bac0: 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f d;.. SimulateIO
bad0: 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
bae0: 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b LITE_IOERR_CHECK
baf0: 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b RESERVEDLOCK; );
bb00: 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c .. assert( pFil
bb10: 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 e );. assert( p
bb20: 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3c File->eFileLock<
bb30: 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
bb40: 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 unixEnterMutex
bb50: 28 29 3b 20 2f 2a 20 42 65 63 61 75 73 65 20 70 (); /* Because p
bb60: 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 20 69 73 20 File->pInode is
bb70: 73 68 61 72 65 64 20 61 63 72 6f 73 73 20 74 68 shared across th
bb80: 72 65 61 64 73 20 2a 2f 0a 0a 20 20 2f 2a 20 43 reads */.. /* C
bb90: 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 heck if a thread
bba0: 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 in this process
bbb0: 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f holds such a lo
bbc0: 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c ck */. if( pFil
bbd0: 65 2d 3e 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 e->pInode->eFile
bbe0: 4c 6f 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43 4b Lock>SHARED_LOCK
bbf0: 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 ){. reserved
bc00: 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 = 1;. }.. /*
bc10: 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 66 Otherwise see if
bc20: 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 some other proc
bc30: 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 ess holds it..
bc40: 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 5f 44 4a 47 */.#ifndef __DJG
bc50: 50 50 5f 5f 0a 20 20 69 66 28 20 21 72 65 73 65 PP__. if( !rese
bc60: 72 76 65 64 20 26 26 20 21 70 46 69 6c 65 2d 3e rved && !pFile->
bc70: 70 49 6e 6f 64 65 2d 3e 62 50 72 6f 63 65 73 73 pInode->bProcess
bc80: 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 73 74 72 75 Lock ){. stru
bc90: 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 ct flock lock;.
bca0: 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 lock.l_whence
bcb0: 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 = SEEK_SET;.
bcc0: 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 lock.l_start =
bcd0: 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 RESERVED_BYTE;.
bce0: 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 lock.l_len =
bcf0: 31 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 1;. lock.l_ty
bd00: 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 pe = F_WRLCK;.
bd10: 20 20 69 66 28 20 6f 73 46 63 6e 74 6c 28 70 46 if( osFcntl(pF
bd20: 69 6c 65 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c ile->h, F_GETLK,
bd30: 20 26 6c 6f 63 6b 29 20 29 7b 0a 20 20 20 20 20 &lock) ){.
bd40: 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 rc = SQLITE_IOE
bd50: 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 RR_CHECKRESERVED
bd60: 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 73 74 6f 72 LOCK;. stor
bd70: 65 4c 61 73 74 45 72 72 6e 6f 28 70 46 69 6c 65 eLastErrno(pFile
bd80: 2c 20 65 72 72 6e 6f 29 3b 0a 20 20 20 20 7d 20 , errno);. }
bd90: 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 2e 6c 5f else if( lock.l_
bda0: 74 79 70 65 21 3d 46 5f 55 4e 4c 43 4b 20 29 7b type!=F_UNLCK ){
bdb0: 0a 20 20 20 20 20 20 72 65 73 65 72 76 65 64 20 . reserved
bdc0: 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 = 1;. }. }.#
bdd0: 65 6e 64 69 66 0a 20 20 0a 20 20 75 6e 69 78 4c endif. . unixL
bde0: 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f eaveMutex();. O
bdf0: 53 54 52 41 43 45 28 28 22 54 45 53 54 20 57 52 STRACE(("TEST WR
be00: 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 20 28 -LOCK %d %d %d (
be10: 75 6e 69 78 29 5c 6e 22 2c 20 70 46 69 6c 65 2d unix)\n", pFile-
be20: 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 >h, rc, reserved
be30: 29 29 3b 0a 0a 20 20 2a 70 52 65 73 4f 75 74 20 ));.. *pResOut
be40: 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 = reserved;. re
be50: 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
be60: 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73 65 74 * Attempt to set
be70: 20 61 20 73 79 73 74 65 6d 2d 6c 6f 63 6b 20 6f a system-lock o
be80: 6e 20 74 68 65 20 66 69 6c 65 20 70 46 69 6c 65 n the file pFile
be90: 2e 20 20 54 68 65 20 6c 6f 63 6b 20 69 73 20 0a . The lock is .
bea0: 2a 2a 20 64 65 73 63 72 69 62 65 64 20 62 79 20 ** described by
beb0: 70 4c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 pLock..**.** If
bec0: 74 68 65 20 70 46 69 6c 65 20 77 61 73 20 6f 70 the pFile was op
bed0: 65 6e 65 64 20 72 65 61 64 2f 77 72 69 74 65 20 ened read/write
bee0: 66 72 6f 6d 20 75 6e 69 78 2d 65 78 63 6c 2c 20 from unix-excl,
bef0: 74 68 65 6e 20 74 68 65 20 6f 6e 6c 79 20 6c 6f then the only lo
bf00: 63 6b 0a 2a 2a 20 65 76 65 72 20 6f 62 74 61 69 ck.** ever obtai
bf10: 6e 65 64 20 69 73 20 61 6e 20 65 78 63 6c 75 73 ned is an exclus
bf20: 69 76 65 20 6c 6f 63 6b 2c 20 61 6e 64 20 69 74 ive lock, and it
bf30: 20 69 73 20 6f 62 74 61 69 6e 65 64 20 65 78 61 is obtained exa
bf40: 63 74 6c 79 20 6f 6e 63 65 0a 2a 2a 20 74 68 65 ctly once.** the
bf50: 20 66 69 72 73 74 20 74 69 6d 65 20 61 6e 79 20 first time any
bf60: 6c 6f 63 6b 20 69 73 20 61 74 74 65 6d 70 74 65 lock is attempte
bf70: 64 2e 20 20 41 6c 6c 20 73 75 62 73 65 71 75 65 d. All subseque
bf80: 6e 74 20 73 79 73 74 65 6d 20 6c 6f 63 6b 69 6e nt system lockin
bf90: 67 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 73 20 g.** operations
bfa0: 62 65 63 6f 6d 65 20 6e 6f 2d 6f 70 73 2e 20 20 become no-ops.
bfb0: 4c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f Locking operatio
bfc0: 6e 73 20 73 74 69 6c 6c 20 68 61 70 70 65 6e 20 ns still happen
bfd0: 69 6e 74 65 72 6e 61 6c 6c 79 2c 0a 2a 2a 20 69 internally,.** i
bfe0: 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6f 72 64 n order to coord
bff0: 69 6e 61 74 65 20 61 63 63 65 73 73 20 62 65 74 inate access bet
c000: 77 65 65 6e 20 73 65 70 61 72 61 74 65 20 64 61 ween separate da
c010: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
c020: 6e 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 69 ns.** within thi
c030: 73 20 70 72 6f 63 65 73 73 2c 20 62 75 74 20 61 s process, but a
c040: 6c 6c 20 6f 66 20 74 68 61 74 20 69 73 20 68 61 ll of that is ha
c050: 6e 64 6c 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 ndled in memory
c060: 61 6e 64 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 and the.** opera
c070: 74 69 6e 67 20 73 79 73 74 65 6d 20 64 6f 65 73 ting system does
c080: 20 6e 6f 74 20 70 61 72 74 69 63 69 70 61 74 65 not participate
c090: 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
c0a0: 63 74 69 6f 6e 20 69 73 20 61 20 70 61 73 73 2d ction is a pass-
c0b0: 74 68 72 6f 75 67 68 20 74 6f 20 66 63 6e 74 6c through to fcntl
c0c0: 28 46 5f 53 45 54 4c 4b 29 20 69 66 20 70 46 69 (F_SETLK) if pFi
c0d0: 6c 65 20 69 73 20 75 73 69 6e 67 0a 2a 2a 20 61 le is using.** a
c0e0: 6e 79 20 56 46 53 20 6f 74 68 65 72 20 74 68 61 ny VFS other tha
c0f0: 6e 20 22 75 6e 69 78 2d 65 78 63 6c 22 20 6f 72 n "unix-excl" or
c100: 20 69 66 20 70 46 69 6c 65 20 69 73 20 6f 70 65 if pFile is ope
c110: 6e 65 64 20 6f 6e 20 22 75 6e 69 78 2d 65 78 63 ned on "unix-exc
c120: 6c 22 0a 2a 2a 20 61 6e 64 20 69 73 20 72 65 61 l".** and is rea
c130: 64 2d 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 5a 65 d-only..**.** Ze
c140: 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 ro is returned i
c150: 66 20 74 68 65 20 63 61 6c 6c 20 63 6f 6d 70 6c f the call compl
c160: 65 74 65 73 20 73 75 63 63 65 73 73 66 75 6c 6c etes successfull
c170: 79 2c 20 6f 72 20 2d 31 20 69 66 20 61 20 63 61 y, or -1 if a ca
c180: 6c 6c 0a 2a 2a 20 74 6f 20 66 63 6e 74 6c 28 29 ll.** to fcntl()
c190: 20 66 61 69 6c 73 2e 20 49 6e 20 74 68 69 73 20 fails. In this
c1a0: 63 61 73 65 2c 20 65 72 72 6e 6f 20 69 73 20 73 case, errno is s
c1b0: 65 74 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 et appropriately
c1c0: 20 28 62 79 20 66 63 6e 74 6c 28 29 29 2e 0a 2a (by fcntl())..*
c1d0: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
c1e0: 78 46 69 6c 65 4c 6f 63 6b 28 75 6e 69 78 46 69 xFileLock(unixFi
c1f0: 6c 65 20 2a 70 46 69 6c 65 2c 20 73 74 72 75 63 le *pFile, struc
c200: 74 20 66 6c 6f 63 6b 20 2a 70 4c 6f 63 6b 29 7b t flock *pLock){
c210: 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e 69 . int rc;. uni
c220: 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 49 6e 6f xInodeInfo *pIno
c230: 64 65 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e 6f de = pFile->pIno
c240: 64 65 3b 0a 20 20 61 73 73 65 72 74 28 20 75 6e de;. assert( un
c250: 69 78 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b ixMutexHeld() );
c260: 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 6f 64 . assert( pInod
c270: 65 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 e!=0 );. if( (p
c280: 46 69 6c 65 2d 3e 63 74 72 6c 46 6c 61 67 73 20 File->ctrlFlags
c290: 26 20 28 55 4e 49 58 46 49 4c 45 5f 45 58 43 4c & (UNIXFILE_EXCL
c2a0: 7c 55 4e 49 58 46 49 4c 45 5f 52 44 4f 4e 4c 59 |UNIXFILE_RDONLY
c2b0: 29 29 3d 3d 55 4e 49 58 46 49 4c 45 5f 45 58 43 ))==UNIXFILE_EXC
c2c0: 4c 20 29 7b 0a 20 20 20 20 69 66 28 20 70 49 6e L ){. if( pIn
c2d0: 6f 64 65 2d 3e 62 50 72 6f 63 65 73 73 4c 6f 63 ode->bProcessLoc
c2e0: 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 74 k==0 ){. st
c2f0: 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b ruct flock lock;
c300: 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
c310: 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 Inode->nLock==0
c320: 29 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f );. lock.l_
c330: 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 whence = SEEK_SE
c340: 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f T;. lock.l_
c350: 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46 start = SHARED_F
c360: 49 52 53 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b IRST;. lock
c370: 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 44 5f .l_len = SHARED_
c380: 53 49 5a 45 3b 0a 20 20 20 20 20 20 6c 6f 63 6b SIZE;. lock
c390: 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 .l_type = F_WRLC
c3a0: 4b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6f 73 K;. rc = os
c3b0: 46 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 Fcntl(pFile->h,
c3c0: 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b F_SETLK, &lock);
c3d0: 0a 20 20 20 20 20 20 69 66 28 20 72 63 3c 30 20 . if( rc<0
c3e0: 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
c3f0: 20 20 20 70 49 6e 6f 64 65 2d 3e 62 50 72 6f 63 pInode->bProc
c400: 65 73 73 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 essLock = 1;.
c410: 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b pInode->nLock
c420: 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ++;. }else{.
c430: 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 rc = 0;.
c440: 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }. }else{.
c450: 72 63 20 3d 20 6f 73 46 63 6e 74 6c 28 70 46 69 rc = osFcntl(pFi
c460: 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 le->h, F_SETLK,
c470: 70 4c 6f 63 6b 29 3b 0a 20 20 7d 0a 20 20 72 65 pLock);. }. re
c480: 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
c490: 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 * Lock the file
c4a0: 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 with the lock sp
c4b0: 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d ecified by param
c4c0: 65 74 65 72 20 65 46 69 6c 65 4c 6f 63 6b 20 2d eter eFileLock -
c4d0: 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 one.** of the f
c4e0: 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 ollowing:.**.**
c4f0: 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c (1) SHARED_L
c500: 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 OCK.** (2) R
c510: 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 ESERVED_LOCK.**
c520: 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f (3) PENDING_
c530: 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 LOCK.** (4)
c540: 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a EXCLUSIVE_LOCK.*
c550: 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 *.** Sometimes w
c560: 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f hen requesting o
c570: 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 ne lock state, a
c580: 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 dditional lock s
c590: 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 tates.** are ins
c5a0: 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e erted in between
c5b0: 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d . The locking m
c5c0: 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 ight fail on one
c5d0: 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a of the later.**
c5e0: 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 transitions lea
c5f0: 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 ving the lock st
c600: 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 ate different fr
c610: 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 om what it start
c620: 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 ed but.** still
c630: 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 short of its goa
c640: 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e l. The followin
c650: 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 g chart shows th
c660: 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 e allowed.** tra
c670: 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 nsitions and the
c680: 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d inserted interm
c690: 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a ediate states:.*
c6a0: 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 *.** UNLOCKED
c6b0: 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 -> SHARED.**
c6c0: 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 SHARED -> RESER
c6d0: 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 VED.** SHARED
c6e0: 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
c6f0: 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
c700: 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 RESERVED -> (PE
c710: 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
c720: 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e IVE.** PENDIN
c730: 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a G -> EXCLUSIVE.*
c740: 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
c750: 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 e will only incr
c760: 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 ease a lock. Us
c770: 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 e the sqlite3OsU
c780: 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 nlock().** routi
c790: 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f ne to lower a lo
c7a0: 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a cking level..*/.
c7b0: 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 4c static int unixL
c7c0: 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
c7d0: 20 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c 65 4c *id, int eFileL
c7e0: 6f 63 6b 29 7b 0a 20 20 2f 2a 20 54 68 65 20 66 ock){. /* The f
c7f0: 6f 6c 6c 6f 77 69 6e 67 20 64 65 73 63 72 69 62 ollowing describ
c800: 65 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 es the implement
c810: 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 72 ation of the var
c820: 69 6f 75 73 20 6c 6f 63 6b 73 20 61 6e 64 0a 20 ious locks and.
c830: 20 2a 2a 20 6c 6f 63 6b 20 74 72 61 6e 73 69 74 ** lock transit
c840: 69 6f 6e 73 20 69 6e 20 74 65 72 6d 73 20 6f 66 ions in terms of
c850: 20 74 68 65 20 50 4f 53 49 58 20 61 64 76 69 73 the POSIX advis
c860: 6f 72 79 20 73 68 61 72 65 64 20 61 6e 64 20 65 ory shared and e
c870: 78 63 6c 75 73 69 76 65 0a 20 20 2a 2a 20 6c 6f xclusive. ** lo
c880: 63 6b 20 70 72 69 6d 69 74 69 76 65 73 20 28 63 ck primitives (c
c890: 61 6c 6c 65 64 20 72 65 61 64 2d 6c 6f 63 6b 73 alled read-locks
c8a0: 20 61 6e 64 20 77 72 69 74 65 2d 6c 6f 63 6b 73 and write-locks
c8b0: 20 62 65 6c 6f 77 2c 20 74 6f 20 61 76 6f 69 64 below, to avoid
c8c0: 0a 20 20 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 . ** confusion
c8d0: 77 69 74 68 20 53 51 4c 69 74 65 20 6c 6f 63 6b with SQLite lock
c8e0: 20 6e 61 6d 65 73 29 2e 20 54 68 65 20 61 6c 67 names). The alg
c8f0: 6f 72 69 74 68 6d 73 20 61 72 65 20 63 6f 6d 70 orithms are comp
c900: 6c 69 63 61 74 65 64 0a 20 20 2a 2a 20 73 6c 69 licated. ** sli
c910: 67 68 74 6c 79 20 69 6e 20 6f 72 64 65 72 20 74 ghtly in order t
c920: 6f 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 o be compatible
c930: 77 69 74 68 20 57 69 6e 64 6f 77 73 39 35 20 73 with Windows95 s
c940: 79 73 74 65 6d 73 20 73 69 6d 75 6c 74 61 6e 65 ystems simultane
c950: 6f 75 73 6c 79 0a 20 20 2a 2a 20 61 63 63 65 73 ously. ** acces
c960: 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 sing the same da
c970: 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69 6e 20 tabase file, in
c980: 63 61 73 65 20 74 68 61 74 20 69 73 20 65 76 65 case that is eve
c990: 72 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2a r required.. **
c9a0: 0a 20 20 2a 2a 20 53 79 6d 62 6f 6c 73 20 64 65 . ** Symbols de
c9b0: 66 69 6e 65 64 20 69 6e 20 6f 73 2e 68 20 69 6e fined in os.h in
c9c0: 64 65 6e 74 69 66 79 20 74 68 65 20 27 70 65 6e dentify the 'pen
c9d0: 64 69 6e 67 20 62 79 74 65 27 20 61 6e 64 20 74 ding byte' and t
c9e0: 68 65 20 27 72 65 73 65 72 76 65 64 0a 20 20 2a he 'reserved. *
c9f0: 2a 20 62 79 74 65 27 2c 20 65 61 63 68 20 73 69 * byte', each si
ca00: 6e 67 6c 65 20 62 79 74 65 73 20 61 74 20 77 65 ngle bytes at we
ca10: 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 73 ll known offsets
ca20: 2c 20 61 6e 64 20 74 68 65 20 27 73 68 61 72 65 , and the 'share
ca30: 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 d byte. ** rang
ca40: 65 27 2c 20 61 20 72 61 6e 67 65 20 6f 66 20 35 e', a range of 5
ca50: 31 30 20 62 79 74 65 73 20 61 74 20 61 20 77 65 10 bytes at a we
ca60: 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 2e ll known offset.
ca70: 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20 6f 62 . **. ** To ob
ca80: 74 61 69 6e 20 61 20 53 48 41 52 45 44 20 6c 6f tain a SHARED lo
ca90: 63 6b 2c 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 ck, a read-lock
caa0: 69 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 is obtained on t
cab0: 68 65 20 27 70 65 6e 64 69 6e 67 0a 20 20 2a 2a he 'pending. **
cac0: 20 62 79 74 65 27 2e 20 20 49 66 20 74 68 69 73 byte'. If this
cad0: 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 is successful,
cae0: 27 73 68 61 72 65 64 20 62 79 74 65 20 72 61 6e 'shared byte ran
caf0: 67 65 27 20 69 73 20 72 65 61 64 2d 6c 6f 63 6b ge' is read-lock
cb00: 65 64 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 ed. ** and the
cb10: 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 27 70 65 6e lock on the 'pen
cb20: 64 69 6e 67 20 62 79 74 65 27 20 72 65 6c 65 61 ding byte' relea
cb30: 73 65 64 2e 20 20 28 4c 65 67 61 63 79 20 6e 6f sed. (Legacy no
cb40: 74 65 3a 20 20 57 68 65 6e 0a 20 20 2a 2a 20 53 te: When. ** S
cb50: 51 4c 69 74 65 20 77 61 73 20 66 69 72 73 74 20 QLite was first
cb60: 64 65 76 65 6c 6f 70 65 64 2c 20 57 69 6e 64 6f developed, Windo
cb70: 77 73 39 35 20 73 79 73 74 65 6d 73 20 77 65 72 ws95 systems wer
cb80: 65 20 73 74 69 6c 6c 20 76 65 72 79 20 63 6f 6d e still very com
cb90: 6d 6f 6e 2c 0a 20 20 2a 2a 20 61 6e 64 20 57 69 mon,. ** and Wi
cba0: 64 6e 6f 77 73 39 35 20 6c 61 63 6b 73 20 61 20 dnows95 lacks a
cbb0: 73 68 61 72 65 64 2d 6c 6f 63 6b 20 63 61 70 61 shared-lock capa
cbc0: 62 69 6c 69 74 79 2e 20 20 53 6f 20 6f 6e 20 57 bility. So on W
cbd0: 69 6e 64 6f 77 73 39 35 2c 20 61 0a 20 20 2a 2a indows95, a. **
cbe0: 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 6c 79 single randomly
cbf0: 20 73 65 6c 65 63 74 65 64 20 62 79 20 66 72 6f selected by fro
cc00: 6d 20 74 68 65 20 27 73 68 61 72 65 64 20 62 79 m the 'shared by
cc10: 74 65 20 72 61 6e 67 65 27 20 69 73 20 6c 6f 63 te range' is loc
cc20: 6b 65 64 2e 0a 20 20 2a 2a 20 57 69 6e 64 6f 77 ked.. ** Window
cc30: 73 39 35 20 69 73 20 6e 6f 77 20 70 72 65 74 74 s95 is now prett
cc40: 79 20 6d 75 63 68 20 65 78 74 69 6e 63 74 2c 20 y much extinct,
cc50: 62 75 74 20 74 68 69 73 20 77 6f 72 6b 2d 61 72 but this work-ar
cc60: 6f 75 6e 64 20 66 6f 72 20 74 68 65 0a 20 20 2a ound for the. *
cc70: 2a 20 6c 61 63 6b 20 6f 66 20 73 68 61 72 65 64 * lack of shared
cc80: 2d 6c 6f 63 6b 73 20 6f 6e 20 57 69 6e 64 6f 77 -locks on Window
cc90: 73 39 35 20 6c 69 76 65 73 20 6f 6e 2c 20 66 6f s95 lives on, fo
cca0: 72 20 62 61 63 6b 77 61 72 64 73 0a 20 20 2a 2a r backwards. **
ccb0: 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 29 compatibility.)
ccc0: 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f . **. ** A pro
ccd0: 63 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 cess may only ob
cce0: 74 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 tain a RESERVED
ccf0: 6c 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 lock after it ha
cd00: 73 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e s a SHARED lock.
cd10: 0a 20 20 2a 2a 20 41 20 52 45 53 45 52 56 45 44 . ** A RESERVED
cd20: 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 lock is impleme
cd30: 6e 74 65 64 20 62 79 20 67 72 61 62 62 69 6e 67 nted by grabbing
cd40: 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e a write-lock on
cd50: 20 74 68 65 0a 20 20 2a 2a 20 27 72 65 73 65 72 the. ** 'reser
cd60: 76 65 64 20 62 79 74 65 27 2e 20 0a 20 20 2a 2a ved byte'. . **
cd70: 0a 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 . ** A process
cd80: 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 may only obtain
cd90: 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61 a PENDING lock a
cda0: 66 74 65 72 20 69 74 20 68 61 73 20 6f 62 74 61 fter it has obta
cdb0: 69 6e 65 64 20 61 0a 20 20 2a 2a 20 53 48 41 52 ined a. ** SHAR
cdc0: 45 44 20 6c 6f 63 6b 2e 20 41 20 50 45 4e 44 49 ED lock. A PENDI
cdd0: 4e 47 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 NG lock is imple
cde0: 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e mented by obtain
cdf0: 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b ing a write-lock
ce00: 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 27 70 65 . ** on the 'pe
ce10: 6e 64 69 6e 67 20 62 79 74 65 27 2e 20 54 68 69 nding byte'. Thi
ce20: 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e s ensures that n
ce30: 6f 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 o new SHARED loc
ce40: 6b 73 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 6f ks can be. ** o
ce50: 62 74 61 69 6e 65 64 2c 20 62 75 74 20 65 78 69 btained, but exi
ce60: 73 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 sting SHARED loc
ce70: 6b 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 ks are allowed t
ce80: 6f 20 70 65 72 73 69 73 74 2e 20 41 20 70 72 6f o persist. A pro
ce90: 63 65 73 73 0a 20 20 2a 2a 20 64 6f 65 73 20 6e cess. ** does n
cea0: 6f 74 20 68 61 76 65 20 74 6f 20 6f 62 74 61 69 ot have to obtai
ceb0: 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 n a RESERVED loc
cec0: 6b 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 k on the way to
ced0: 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 0a a PENDING lock..
cee0: 20 20 2a 2a 20 54 68 69 73 20 70 72 6f 70 65 72 ** This proper
cef0: 74 79 20 69 73 20 75 73 65 64 20 62 79 20 74 68 ty is used by th
cf00: 65 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 e algorithm for
cf10: 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a rolling back a j
cf20: 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a ournal file. **
cf30: 20 61 66 74 65 72 20 61 20 63 72 61 73 68 2e 0a after a crash..
cf40: 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45 58 43 **. ** An EXC
cf50: 4c 55 53 49 56 45 20 6c 6f 63 6b 2c 20 6f 62 74 LUSIVE lock, obt
cf60: 61 69 6e 65 64 20 61 66 74 65 72 20 61 20 50 45 ained after a PE
cf70: 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 68 65 NDING lock is he
cf80: 6c 64 2c 20 69 73 0a 20 20 2a 2a 20 69 6d 70 6c ld, is. ** impl
cf90: 65 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 emented by obtai
cfa0: 6e 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 ning a write-loc
cfb0: 6b 20 6f 6e 20 74 68 65 20 65 6e 74 69 72 65 20 k on the entire
cfc0: 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 'shared byte. *
cfd0: 2a 20 72 61 6e 67 65 27 2e 20 53 69 6e 63 65 20 * range'. Since
cfe0: 61 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 all other locks
cff0: 72 65 71 75 69 72 65 20 61 20 72 65 61 64 2d 6c require a read-l
d000: 6f 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 ock on one of th
d010: 65 20 62 79 74 65 73 0a 20 20 2a 2a 20 77 69 74 e bytes. ** wit
d020: 68 69 6e 20 74 68 69 73 20 72 61 6e 67 65 2c 20 hin this range,
d030: 74 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 this ensures tha
d040: 74 20 6e 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73 t no other locks
d050: 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 are held on the
d060: 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 . ** database.
d070: 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d . */. int rc =
d080: 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e SQLITE_OK;. un
d090: 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
d0a0: 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
d0b0: 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a unixInodeInfo *
d0c0: 70 49 6e 6f 64 65 3b 0a 20 20 73 74 72 75 63 74 pInode;. struct
d0d0: 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 flock lock;. i
d0e0: 6e 74 20 74 45 72 72 6e 6f 20 3d 20 30 3b 0a 0a nt tErrno = 0;..
d0f0: 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
d100: 29 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 4c );. OSTRACE(("L
d110: 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 OCK %d %s was
d120: 20 25 73 28 25 73 2c 25 64 29 20 70 69 64 3d 25 %s(%s,%d) pid=%
d130: 64 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 69 d (unix)\n", pFi
d140: 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 61 7a 46 le->h,. azF
d150: 69 6c 65 4c 6f 63 6b 28 65 46 69 6c 65 4c 6f 63 ileLock(eFileLoc
d160: 6b 29 2c 20 61 7a 46 69 6c 65 4c 6f 63 6b 28 70 k), azFileLock(p
d170: 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 29 File->eFileLock)
d180: 2c 0a 20 20 20 20 20 20 61 7a 46 69 6c 65 4c 6f ,. azFileLo
d190: 63 6b 28 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 ck(pFile->pInode
d1a0: 2d 3e 65 46 69 6c 65 4c 6f 63 6b 29 2c 20 70 46 ->eFileLock), pF
d1b0: 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 6e 53 68 ile->pInode->nSh
d1c0: 61 72 65 64 2c 0a 20 20 20 20 20 20 6f 73 47 65 ared,. osGe
d1d0: 74 70 69 64 28 30 29 29 29 3b 0a 0a 20 20 2f 2a tpid(0)));.. /*
d1e0: 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 If there is alr
d1f0: 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 eady a lock of t
d200: 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 his type or more
d210: 20 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 restrictive on
d220: 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78 46 69 6c the. ** unixFil
d230: 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 e, do nothing. D
d240: 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64 on't use the end
d250: 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 _lock: exit path
d260: 2c 20 61 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e , as. ** unixEn
d270: 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 terMutex() hasn'
d280: 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 t been called ye
d290: 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 t.. */. if( pF
d2a0: 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3e 3d ile->eFileLock>=
d2b0: 65 46 69 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 eFileLock ){.
d2c0: 20 4f 53 54 52 41 43 45 28 28 22 4c 4f 43 4b 20 OSTRACE(("LOCK
d2d0: 20 20 20 25 64 20 25 73 20 6f 6b 20 28 61 6c 72 %d %s ok (alr
d2e0: 65 61 64 79 20 68 65 6c 64 29 20 28 75 6e 69 78 eady held) (unix
d2f0: 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a )\n", pFile->h,.
d300: 20 20 20 20 20 20 20 20 20 20 20 20 61 7a 46 69 azFi
d310: 6c 65 4c 6f 63 6b 28 65 46 69 6c 65 4c 6f 63 6b leLock(eFileLock
d320: 29 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 )));. return
d330: 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a SQLITE_OK;. }..
d340: 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 /* Make sure t
d350: 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 he locking seque
d360: 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74 2e 0a nce is correct..
d370: 20 20 2a 2a 20 20 28 31 29 20 57 65 20 6e 65 76 ** (1) We nev
d380: 65 72 20 6d 6f 76 65 20 66 72 6f 6d 20 75 6e 6c er move from unl
d390: 6f 63 6b 65 64 20 74 6f 20 61 6e 79 74 68 69 6e ocked to anythin
d3a0: 67 20 68 69 67 68 65 72 20 74 68 61 6e 20 73 68 g higher than sh
d3b0: 61 72 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 ared lock.. **
d3c0: 20 28 32 29 20 53 51 4c 69 74 65 20 6e 65 76 65 (2) SQLite neve
d3d0: 72 20 65 78 70 6c 69 63 69 74 6c 79 20 72 65 71 r explicitly req
d3e0: 75 65 73 74 73 20 61 20 70 65 6e 64 69 67 20 6c uests a pendig l
d3f0: 6f 63 6b 2e 0a 20 20 2a 2a 20 20 28 33 29 20 41 ock.. ** (3) A
d400: 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 73 20 shared lock is
d410: 61 6c 77 61 79 73 20 68 65 6c 64 20 77 68 65 6e always held when
d420: 20 61 20 72 65 73 65 72 76 65 20 6c 6f 63 6b 20 a reserve lock
d430: 69 73 20 72 65 71 75 65 73 74 65 64 2e 0a 20 20 is requested..
d440: 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 */. assert( pFi
d450: 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 21 3d 4e le->eFileLock!=N
d460: 4f 5f 4c 4f 43 4b 20 7c 7c 20 65 46 69 6c 65 4c O_LOCK || eFileL
d470: 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ock==SHARED_LOCK
d480: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 46 );. assert( eF
d490: 69 6c 65 4c 6f 63 6b 21 3d 50 45 4e 44 49 4e 47 ileLock!=PENDING
d4a0: 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 _LOCK );. asser
d4b0: 74 28 20 65 46 69 6c 65 4c 6f 63 6b 21 3d 52 45 t( eFileLock!=RE
d4c0: 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 SERVED_LOCK || p
d4d0: 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d File->eFileLock=
d4e0: 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
d4f0: 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 78 . /* This mutex
d500: 20 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 75 is needed becau
d510: 73 65 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 se pFile->pInode
d520: 20 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 is shared acros
d530: 73 20 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 s threads. */.
d540: 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
d550: 29 3b 0a 20 20 70 49 6e 6f 64 65 20 3d 20 70 46 );. pInode = pF
d560: 69 6c 65 2d 3e 70 49 6e 6f 64 65 3b 0a 0a 20 20 ile->pInode;..
d570: 2f 2a 20 49 66 20 73 6f 6d 65 20 74 68 72 65 61 /* If some threa
d580: 64 20 75 73 69 6e 67 20 74 68 69 73 20 50 49 44 d using this PID
d590: 20 68 61 73 20 61 20 6c 6f 63 6b 20 76 69 61 20 has a lock via
d5a0: 61 20 64 69 66 66 65 72 65 6e 74 20 75 6e 69 78 a different unix
d5b0: 46 69 6c 65 2a 0a 20 20 2a 2a 20 68 61 6e 64 6c File*. ** handl
d5c0: 65 20 74 68 61 74 20 70 72 65 63 6c 75 64 65 73 e that precludes
d5d0: 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
d5e0: 6f 63 6b 2c 20 72 65 74 75 72 6e 20 42 55 53 59 ock, return BUSY
d5f0: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 46 .. */. if( (pF
d600: 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 21 3d ile->eFileLock!=
d610: 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 pInode->eFileLoc
d620: 6b 20 26 26 20 0a 20 20 20 20 20 20 20 20 20 20 k && .
d630: 28 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f (pInode->eFileLo
d640: 63 6b 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b ck>=PENDING_LOCK
d650: 20 7c 7c 20 65 46 69 6c 65 4c 6f 63 6b 3e 53 48 || eFileLock>SH
d660: 41 52 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b ARED_LOCK)). ){
d670: 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
d680: 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20 _BUSY;. goto
d690: 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 end_lock;. }..
d6a0: 20 2f 2a 20 49 66 20 61 20 53 48 41 52 45 44 20 /* If a SHARED
d6b0: 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 lock is requeste
d6c0: 64 2c 20 61 6e 64 20 73 6f 6d 65 20 74 68 72 65 d, and some thre
d6d0: 61 64 20 75 73 69 6e 67 20 74 68 69 73 20 50 49 ad using this PI
d6e0: 44 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 68 D already. ** h
d6f0: 61 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 52 as a SHARED or R
d700: 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74 68 ESERVED lock, th
d710: 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 72 65 66 en increment ref
d720: 65 72 65 6e 63 65 20 63 6f 75 6e 74 73 20 61 6e erence counts an
d730: 64 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51 d. ** return SQ
d740: 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 LITE_OK.. */.
d750: 69 66 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 if( eFileLock==S
d760: 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 0a 20 HARED_LOCK && .
d770: 20 20 20 20 20 28 70 49 6e 6f 64 65 2d 3e 65 46 (pInode->eF
d780: 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f ileLock==SHARED_
d790: 4c 4f 43 4b 20 7c 7c 20 70 49 6e 6f 64 65 2d 3e LOCK || pInode->
d7a0: 65 46 69 6c 65 4c 6f 63 6b 3d 3d 52 45 53 45 52 eFileLock==RESER
d7b0: 56 45 44 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 VED_LOCK) ){.
d7c0: 20 61 73 73 65 72 74 28 20 65 46 69 6c 65 4c 6f assert( eFileLo
d7d0: 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ck==SHARED_LOCK
d7e0: 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
d7f0: 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d File->eFileLock=
d800: 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =0 );. assert
d810: 28 20 70 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 ( pInode->nShare
d820: 64 3e 30 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 d>0 );. pFile
d830: 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 53 48 ->eFileLock = SH
d840: 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 ARED_LOCK;. p
d850: 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 2b 2b Inode->nShared++
d860: 3b 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 4c ;. pInode->nL
d870: 6f 63 6b 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 ock++;. goto
d880: 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 0a end_lock;. }...
d890: 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 6c /* A PENDING l
d8a0: 6f 63 6b 20 69 73 20 6e 65 65 64 65 64 20 62 65 ock is needed be
d8b0: 66 6f 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 fore acquiring a
d8c0: 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e 64 SHARED lock and
d8d0: 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63 71 before. ** acq
d8e0: 75 69 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 uiring an EXCLUS
d8f0: 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74 IVE lock. For t
d900: 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 he SHARED lock,
d910: 74 68 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c the PENDING will
d920: 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 . ** be release
d930: 64 2e 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 2e 6c d.. */. lock.l
d940: 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 20 20 6c 6f 63 _len = 1L;. loc
d950: 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 k.l_whence = SEE
d960: 4b 5f 53 45 54 3b 0a 20 20 69 66 28 20 65 46 69 K_SET;. if( eFi
d970: 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c leLock==SHARED_L
d980: 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20 28 65 OCK . || (e
d990: 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 FileLock==EXCLUS
d9a0: 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c IVE_LOCK && pFil
d9b0: 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3c 50 45 4e e->eFileLock<PEN
d9c0: 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a DING_LOCK). ){.
d9d0: 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 lock.l_type
d9e0: 3d 20 28 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 = (eFileLock==SH
d9f0: 41 52 45 44 5f 4c 4f 43 4b 3f 46 5f 52 44 4c 43 ARED_LOCK?F_RDLC
da00: 4b 3a 46 5f 57 52 4c 43 4b 29 3b 0a 20 20 20 20 K:F_WRLCK);.
da10: 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 lock.l_start = P
da20: 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 ENDING_BYTE;.
da30: 20 69 66 28 20 75 6e 69 78 46 69 6c 65 4c 6f 63 if( unixFileLoc
da40: 6b 28 70 46 69 6c 65 2c 20 26 6c 6f 63 6b 29 20 k(pFile, &lock)
da50: 29 7b 0a 20 20 20 20 20 20 74 45 72 72 6e 6f 20 ){. tErrno
da60: 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 = errno;. r
da70: 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
da80: 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
da90: 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 rrno, SQLITE_IOE
daa0: 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 RR_LOCK);.
dab0: 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 if( rc!=SQLITE_B
dac0: 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 73 USY ){. s
dad0: 74 6f 72 65 4c 61 73 74 45 72 72 6e 6f 28 70 46 toreLastErrno(pF
dae0: 69 6c 65 2c 20 74 45 72 72 6e 6f 29 3b 0a 20 20 ile, tErrno);.
daf0: 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f }. goto
db00: 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d end_lock;. }
db10: 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 49 66 20 63 . }... /* If c
db20: 6f 6e 74 72 6f 6c 20 67 65 74 73 20 74 6f 20 74 ontrol gets to t
db30: 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 his point, then
db40: 61 63 74 75 61 6c 6c 79 20 67 6f 20 61 68 65 61 actually go ahea
db50: 64 20 61 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 d and make. **
db60: 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
db70: 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 73 calls for the s
db80: 70 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 pecified lock..
db90: 20 2a 2f 0a 20 20 69 66 28 20 65 46 69 6c 65 4c */. if( eFileL
dba0: 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ock==SHARED_LOCK
dbb0: 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
dbc0: 70 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 3d pInode->nShared=
dbd0: 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =0 );. assert
dbe0: 28 20 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c ( pInode->eFileL
dbf0: 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 ock==0 );. as
dc00: 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
dc10: 5f 4f 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4e _OK );.. /* N
dc20: 6f 77 20 67 65 74 20 74 68 65 20 72 65 61 64 2d ow get the read-
dc30: 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c 6f 63 6b lock */. lock
dc40: 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 .l_start = SHARE
dc50: 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 6c 6f 63 D_FIRST;. loc
dc60: 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 44 k.l_len = SHARED
dc70: 5f 53 49 5a 45 3b 0a 20 20 20 20 69 66 28 20 75 _SIZE;. if( u
dc80: 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 70 46 69 6c nixFileLock(pFil
dc90: 65 2c 20 26 6c 6f 63 6b 29 20 29 7b 0a 20 20 20 e, &lock) ){.
dca0: 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e tErrno = errn
dcb0: 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 o;. rc = sq
dcc0: 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
dcd0: 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
dce0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 SQLITE_IOERR_LOC
dcf0: 4b 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f K);. }.. /
dd00: 2a 20 44 72 6f 70 20 74 68 65 20 74 65 6d 70 6f * Drop the tempo
dd10: 72 61 72 79 20 50 45 4e 44 49 4e 47 20 6c 6f 63 rary PENDING loc
dd20: 6b 20 2a 2f 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f k */. lock.l_
dd30: 73 74 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f start = PENDING_
dd40: 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c BYTE;. lock.l
dd50: 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 20 20 20 20 6c _len = 1L;. l
dd60: 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 ock.l_type = F_U
dd70: 4e 4c 43 4b 3b 0a 20 20 20 20 69 66 28 20 75 6e NLCK;. if( un
dd80: 69 78 46 69 6c 65 4c 6f 63 6b 28 70 46 69 6c 65 ixFileLock(pFile
dd90: 2c 20 26 6c 6f 63 6b 29 20 26 26 20 72 63 3d 3d , &lock) && rc==
dda0: 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
ddb0: 20 20 20 2f 2a 20 54 68 69 73 20 63 6f 75 6c 64 /* This could
ddc0: 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 20 6e happen with a n
ddd0: 65 74 77 6f 72 6b 20 6d 6f 75 6e 74 20 2a 2f 0a etwork mount */.
dde0: 20 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 tErrno = e
ddf0: 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d rrno;. rc =
de00: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e SQLITE_IOERR_UN
de10: 4c 4f 43 4b 3b 20 0a 20 20 20 20 7d 0a 0a 20 20 LOCK; . }..
de20: 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
de30: 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
de40: 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 _BUSY ){.
de50: 20 73 74 6f 72 65 4c 61 73 74 45 72 72 6e 6f 28 storeLastErrno(
de60: 70 46 69 6c 65 2c 20 74 45 72 72 6e 6f 29 3b 0a pFile, tErrno);.
de70: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f }. go
de80: 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 to end_lock;.
de90: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 }else{. pF
dea0: 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d ile->eFileLock =
deb0: 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 SHARED_LOCK;.
dec0: 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 pInode->nLoc
ded0: 6b 2b 2b 3b 0a 20 20 20 20 20 20 70 49 6e 6f 64 k++;. pInod
dee0: 65 2d 3e 6e 53 68 61 72 65 64 20 3d 20 31 3b 0a e->nShared = 1;.
def0: 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 }. }else if
df00: 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43 ( eFileLock==EXC
df10: 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 LUSIVE_LOCK && p
df20: 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 3e 31 Inode->nShared>1
df30: 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 ){. /* We ar
df40: 65 20 74 72 79 69 6e 67 20 66 6f 72 20 61 6e 20 e trying for an
df50: 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 62 exclusive lock b
df60: 75 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 ut another threa
df70: 64 20 69 6e 20 74 68 69 73 0a 20 20 20 20 2a 2a d in this. **
df80: 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 69 73 same process is
df90: 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 still holding a
dfa0: 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f shared lock. */
dfb0: 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
dfc0: 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a _BUSY;. }else{.
dfd0: 20 20 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 /* The reque
dfe0: 73 74 20 77 61 73 20 66 6f 72 20 61 20 52 45 53 st was for a RES
dff0: 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 ERVED or EXCLUSI
e000: 56 45 20 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a VE lock. It is.
e010: 20 20 20 20 2a 2a 20 61 73 73 75 6d 65 64 20 74 ** assumed t
e020: 68 61 74 20 74 68 65 72 65 20 69 73 20 61 20 53 hat there is a S
e030: 48 41 52 45 44 20 6f 72 20 67 72 65 61 74 65 72 HARED or greater
e040: 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c lock on the fil
e050: 65 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 e. ** already
e060: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
e070: 65 72 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e 65 ert( 0!=pFile->e
e080: 46 69 6c 65 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 FileLock );.
e090: 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f lock.l_type = F_
e0a0: 57 52 4c 43 4b 3b 0a 0a 20 20 20 20 61 73 73 65 WRLCK;.. asse
e0b0: 72 74 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 52 rt( eFileLock==R
e0c0: 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 ESERVED_LOCK ||
e0d0: 65 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 eFileLock==EXCLU
e0e0: 53 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 SIVE_LOCK );.
e0f0: 20 69 66 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d if( eFileLock==
e100: 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b RESERVED_LOCK ){
e110: 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 . lock.l_st
e120: 61 72 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 art = RESERVED_B
e130: 59 54 45 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e YTE;. lock.
e140: 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 20 20 20 20 l_len = 1L;.
e150: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6c 6f 63 }else{. loc
e160: 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 k.l_start = SHAR
e170: 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20 ED_FIRST;.
e180: 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 lock.l_len = SHA
e190: 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 7d 0a RED_SIZE;. }.
e1a0: 0a 20 20 20 20 69 66 28 20 75 6e 69 78 46 69 6c . if( unixFil
e1b0: 65 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 26 6c 6f eLock(pFile, &lo
e1c0: 63 6b 29 20 29 7b 0a 20 20 20 20 20 20 74 45 72 ck) ){. tEr
e1d0: 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
e1e0: 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 rc = sqliteEr
e1f0: 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f rorFromPosixErro
e200: 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 r(tErrno, SQLITE
e210: 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 _IOERR_LOCK);.
e220: 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
e230: 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 TE_BUSY ){.
e240: 20 20 20 73 74 6f 72 65 4c 61 73 74 45 72 72 6e storeLastErrn
e250: 6f 28 70 46 69 6c 65 2c 20 74 45 72 72 6e 6f 29 o(pFile, tErrno)
e260: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
e270: 20 20 7d 0a 20 20 0a 0a 23 69 66 64 65 66 20 53 }. ..#ifdef S
e280: 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a QLITE_DEBUG. /*
e290: 20 53 65 74 20 75 70 20 74 68 65 20 74 72 61 6e Set up the tran
e2a0: 73 61 63 74 69 6f 6e 2d 63 6f 75 6e 74 65 72 20 saction-counter
e2b0: 63 68 61 6e 67 65 20 63 68 65 63 6b 69 6e 67 20 change checking
e2c0: 66 6c 61 67 73 20 77 68 65 6e 0a 20 20 2a 2a 20 flags when. **
e2d0: 74 72 61 6e 73 69 74 69 6f 6e 69 6e 67 20 66 72 transitioning fr
e2e0: 6f 6d 20 61 20 53 48 41 52 45 44 20 74 6f 20 61 om a SHARED to a
e2f0: 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 20 RESERVED lock.
e300: 20 54 68 65 20 63 68 61 6e 67 65 0a 20 20 2a 2a The change. **
e310: 20 66 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 from SHARED to
e320: 52 45 53 45 52 56 45 44 20 6d 61 72 6b 73 20 74 RESERVED marks t
e330: 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
e340: 61 20 6e 6f 72 6d 61 6c 0a 20 20 2a 2a 20 77 72 a normal. ** wr
e350: 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 6e ite operation (n
e360: 6f 74 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c ot a hot journal
e370: 20 72 6f 6c 6c 62 61 63 6b 29 2e 0a 20 20 2a 2f rollback).. */
e380: 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
e390: 45 5f 4f 4b 0a 20 20 20 26 26 20 70 46 69 6c 65 E_OK. && pFile
e3a0: 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3c 3d 53 48 41 ->eFileLock<=SHA
e3b0: 52 45 44 5f 4c 4f 43 4b 0a 20 20 20 26 26 20 65 RED_LOCK. && e
e3c0: 46 69 6c 65 4c 6f 63 6b 3d 3d 52 45 53 45 52 56 FileLock==RESERV
e3d0: 45 44 5f 4c 4f 43 4b 0a 20 20 29 7b 0a 20 20 20 ED_LOCK. ){.
e3e0: 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 pFile->transCnt
e3f0: 72 43 68 6e 67 20 3d 20 30 3b 0a 20 20 20 20 70 rChng = 0;. p
e400: 46 69 6c 65 2d 3e 64 62 55 70 64 61 74 65 20 3d File->dbUpdate =
e410: 20 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 0;. pFile->i
e420: 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20 31 nNormalWrite = 1
e430: 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 0a 20 ;. }.#endif...
e440: 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
e450: 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d OK ){. pFile-
e460: 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 65 46 69 >eFileLock = eFi
e470: 6c 65 4c 6f 63 6b 3b 0a 20 20 20 20 70 49 6e 6f leLock;. pIno
e480: 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 de->eFileLock =
e490: 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20 7d 65 6c eFileLock;. }el
e4a0: 73 65 20 69 66 28 20 65 46 69 6c 65 4c 6f 63 6b se if( eFileLock
e4b0: 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ==EXCLUSIVE_LOCK
e4c0: 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65 ){. pFile->e
e4d0: 46 69 6c 65 4c 6f 63 6b 20 3d 20 50 45 4e 44 49 FileLock = PENDI
e4e0: 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 49 6e NG_LOCK;. pIn
e4f0: 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d ode->eFileLock =
e500: 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 PENDING_LOCK;.
e510: 20 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 }..end_lock:.
e520: 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 unixLeaveMutex()
e530: 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 4c 4f ;. OSTRACE(("LO
e540: 43 4b 20 20 20 20 25 64 20 25 73 20 25 73 20 28 CK %d %s %s (
e550: 75 6e 69 78 29 5c 6e 22 2c 20 70 46 69 6c 65 2d unix)\n", pFile-
e560: 3e 68 2c 20 61 7a 46 69 6c 65 4c 6f 63 6b 28 65 >h, azFileLock(e
e570: 46 69 6c 65 4c 6f 63 6b 29 2c 20 0a 20 20 20 20 FileLock), .
e580: 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 rc==SQLITE_OK
e590: 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 ? "ok" : "failed
e5a0: 22 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 "));. return rc
e5b0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 ;.}../*.** Add t
e5c0: 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
e5d0: 6f 72 20 75 73 65 64 20 62 79 20 66 69 6c 65 20 or used by file
e5e0: 68 61 6e 64 6c 65 20 70 46 69 6c 65 20 74 6f 20 handle pFile to
e5f0: 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
e600: 67 0a 2a 2a 20 70 55 6e 75 73 65 64 20 6c 69 73 g.** pUnused lis
e610: 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 t..*/.static voi
e620: 64 20 73 65 74 50 65 6e 64 69 6e 67 46 64 28 75 d setPendingFd(u
e630: 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b nixFile *pFile){
e640: 0a 20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f . unixInodeInfo
e650: 20 2a 70 49 6e 6f 64 65 20 3d 20 70 46 69 6c 65 *pInode = pFile
e660: 2d 3e 70 49 6e 6f 64 65 3b 0a 20 20 55 6e 69 78 ->pInode;. Unix
e670: 55 6e 75 73 65 64 46 64 20 2a 70 20 3d 20 70 46 UnusedFd *p = pF
e680: 69 6c 65 2d 3e 70 55 6e 75 73 65 64 3b 0a 20 20 ile->pUnused;.
e690: 70 2d 3e 70 4e 65 78 74 20 3d 20 70 49 6e 6f 64 p->pNext = pInod
e6a0: 65 2d 3e 70 55 6e 75 73 65 64 3b 0a 20 20 70 49 e->pUnused;. pI
e6b0: 6e 6f 64 65 2d 3e 70 55 6e 75 73 65 64 20 3d 20 node->pUnused =
e6c0: 70 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 p;. pFile->h =
e6d0: 2d 31 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 55 6e -1;. pFile->pUn
e6e0: 75 73 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a used = 0;.}../*.
e6f0: 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 ** Lower the loc
e700: 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 king level on fi
e710: 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 le descriptor pF
e720: 69 6c 65 20 74 6f 20 65 46 69 6c 65 4c 6f 63 6b ile to eFileLock
e730: 2e 20 20 65 46 69 6c 65 4c 6f 63 6b 0a 2a 2a 20 . eFileLock.**
e740: 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e must be either N
e750: 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 O_LOCK or SHARED
e760: 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 _LOCK..**.** If
e770: 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
e780: 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 l of the file de
e790: 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 scriptor is alre
e7a0: 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a ady at or below.
e7b0: 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 ** the requested
e7c0: 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 locking level,
e7d0: 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
e7e0: 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 20 0a 2a 2a 20 a no-op..** .**
e7f0: 49 66 20 68 61 6e 64 6c 65 4e 46 53 55 6e 6c 6f If handleNFSUnlo
e800: 63 6b 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e ck is true, then
e810: 20 6f 6e 20 64 6f 77 6e 67 72 61 64 69 6e 67 20 on downgrading
e820: 61 6e 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 an EXCLUSIVE_LOC
e830: 4b 20 74 6f 20 53 48 41 52 45 44 0a 2a 2a 20 74 K to SHARED.** t
e840: 68 65 20 62 79 74 65 20 72 61 6e 67 65 20 69 73 he byte range is
e850: 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 32 20 divided into 2
e860: 70 61 72 74 73 20 61 6e 64 20 74 68 65 20 66 69 parts and the fi
e870: 72 73 74 20 70 61 72 74 20 69 73 20 75 6e 6c 6f rst part is unlo
e880: 63 6b 65 64 20 74 68 65 6e 0a 2a 2a 20 73 65 74 cked then.** set
e890: 20 74 6f 20 61 20 72 65 61 64 20 6c 6f 63 6b 2c to a read lock,
e8a0: 20 74 68 65 6e 20 74 68 65 20 6f 74 68 65 72 20 then the other
e8b0: 70 61 72 74 20 69 73 20 73 69 6d 70 6c 79 20 75 part is simply u
e8c0: 6e 6c 6f 63 6b 65 64 2e 20 20 54 68 69 73 20 77 nlocked. This w
e8d0: 6f 72 6b 73 20 0a 2a 2a 20 61 72 6f 75 6e 64 20 orks .** around
e8e0: 61 20 62 75 67 20 69 6e 20 42 53 44 20 4e 46 53 a bug in BSD NFS
e8f0: 20 6c 6f 63 6b 64 20 28 61 6c 73 6f 20 73 65 65 lockd (also see
e900: 6e 20 6f 6e 20 4d 61 63 4f 53 58 20 31 30 2e 33 n on MacOSX 10.3
e910: 2b 29 20 74 68 61 74 20 66 61 69 6c 73 20 74 6f +) that fails to
e920: 20 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 .** remove the
e930: 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 61 20 write lock on a
e940: 72 65 67 69 6f 6e 20 77 68 65 6e 20 61 20 72 65 region when a re
e950: 61 64 20 6c 6f 63 6b 20 69 73 20 73 65 74 2e 0a ad lock is set..
e960: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f */.static int po
e970: 73 69 78 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 sixUnlock(sqlite
e980: 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
e990: 65 46 69 6c 65 4c 6f 63 6b 2c 20 69 6e 74 20 68 eFileLock, int h
e9a0: 61 6e 64 6c 65 4e 46 53 55 6e 6c 6f 63 6b 29 7b andleNFSUnlock){
e9b0: 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
e9c0: 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
e9d0: 69 64 3b 0a 20 20 75 6e 69 78 49 6e 6f 64 65 49 id;. unixInodeI
e9e0: 6e 66 6f 20 2a 70 49 6e 6f 64 65 3b 0a 20 20 73 nfo *pInode;. s
e9f0: 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b truct flock lock
ea00: 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
ea10: 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 ITE_OK;.. asser
ea20: 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 t( pFile );. OS
ea30: 54 52 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20 20 TRACE(("UNLOCK
ea40: 25 64 20 25 64 20 77 61 73 20 25 64 28 25 64 2c %d %d was %d(%d,
ea50: 25 64 29 20 70 69 64 3d 25 64 20 28 75 6e 69 78 %d) pid=%d (unix
ea60: 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 )\n", pFile->h,
ea70: 65 46 69 6c 65 4c 6f 63 6b 2c 0a 20 20 20 20 20 eFileLock,.
ea80: 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 pFile->eFileLoc
ea90: 6b 2c 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 k, pFile->pInode
eaa0: 2d 3e 65 46 69 6c 65 4c 6f 63 6b 2c 20 70 46 69 ->eFileLock, pFi
eab0: 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 6e 53 68 61 le->pInode->nSha
eac0: 72 65 64 2c 0a 20 20 20 20 20 20 6f 73 47 65 74 red,. osGet
ead0: 70 69 64 28 30 29 29 29 3b 0a 0a 20 20 61 73 73 pid(0)));.. ass
eae0: 65 72 74 28 20 65 46 69 6c 65 4c 6f 63 6b 3c 3d ert( eFileLock<=
eaf0: 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 SHARED_LOCK );.
eb00: 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c if( pFile->eFil
eb10: 65 4c 6f 63 6b 3c 3d 65 46 69 6c 65 4c 6f 63 6b eLock<=eFileLock
eb20: 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
eb30: 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
eb40: 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
eb50: 3b 0a 20 20 70 49 6e 6f 64 65 20 3d 20 70 46 69 ;. pInode = pFi
eb60: 6c 65 2d 3e 70 49 6e 6f 64 65 3b 0a 20 20 61 73 le->pInode;. as
eb70: 73 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 6e 53 sert( pInode->nS
eb80: 68 61 72 65 64 21 3d 30 20 29 3b 0a 20 20 69 66 hared!=0 );. if
eb90: 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f ( pFile->eFileLo
eba0: 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 ck>SHARED_LOCK )
ebb0: 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 {. assert( pI
ebc0: 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d node->eFileLock=
ebd0: 3d 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 =pFile->eFileLoc
ebe0: 6b 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c k );..#ifdef SQL
ebf0: 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 2f 2a ITE_DEBUG. /*
ec00: 20 57 68 65 6e 20 72 65 64 75 63 69 6e 67 20 61 When reducing a
ec10: 20 6c 6f 63 6b 20 73 75 63 68 20 74 68 61 74 20 lock such that
ec20: 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 other processes
ec30: 63 61 6e 20 73 74 61 72 74 0a 20 20 20 20 2a 2a can start. **
ec40: 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 reading the dat
ec50: 61 62 61 73 65 20 66 69 6c 65 20 61 67 61 69 6e abase file again
ec60: 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 , make sure that
ec70: 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e the. ** tran
ec80: 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 saction counter
ec90: 77 61 73 20 75 70 64 61 74 65 64 20 69 66 20 61 was updated if a
eca0: 6e 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 ny part of the d
ecb0: 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 66 atabase. ** f
ecc0: 69 6c 65 20 63 68 61 6e 67 65 64 2e 20 20 49 66 ile changed. If
ecd0: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
ece0: 20 63 6f 75 6e 74 65 72 20 69 73 20 6e 6f 74 20 counter is not
ecf0: 75 70 64 61 74 65 64 2c 0a 20 20 20 20 2a 2a 20 updated,. **
ed00: 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e other connection
ed10: 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 66 69 s to the same fi
ed20: 6c 65 20 6d 69 67 68 74 20 6e 6f 74 20 72 65 61 le might not rea
ed30: 6c 69 7a 65 20 74 68 61 74 0a 20 20 20 20 2a 2a lize that. **
ed40: 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 68 the file has ch
ed50: 61 6e 67 65 64 20 61 6e 64 20 68 65 6e 63 65 20 anged and hence
ed60: 6d 69 67 68 74 20 6e 6f 74 20 6b 6e 6f 77 20 74 might not know t
ed70: 6f 20 66 6c 75 73 68 20 74 68 65 69 72 0a 20 20 o flush their.
ed80: 20 20 2a 2a 20 63 61 63 68 65 2e 20 20 54 68 65 ** cache. The
ed90: 20 75 73 65 20 6f 66 20 61 20 73 74 61 6c 65 20 use of a stale
eda0: 63 61 63 68 65 20 63 61 6e 20 6c 65 61 64 20 74 cache can lead t
edb0: 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 o database corru
edc0: 70 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 ption.. */.
edd0: 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 pFile->inNorma
ede0: 6c 57 72 69 74 65 20 3d 20 30 3b 0a 23 65 6e 64 lWrite = 0;.#end
edf0: 69 66 0a 0a 20 20 20 20 2f 2a 20 64 6f 77 6e 67 if.. /* downg
ee00: 72 61 64 69 6e 67 20 74 6f 20 61 20 73 68 61 72 rading to a shar
ee10: 65 64 20 6c 6f 63 6b 20 6f 6e 20 4e 46 53 20 69 ed lock on NFS i
ee20: 6e 76 6f 6c 76 65 73 20 63 6c 65 61 72 69 6e 67 nvolves clearing
ee30: 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 0a the write lock.
ee40: 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 65 73 ** before es
ee50: 74 61 62 6c 69 73 68 69 6e 67 20 74 68 65 20 72 tablishing the r
ee60: 65 61 64 6c 6f 63 6b 20 2d 20 74 6f 20 61 76 6f eadlock - to avo
ee70: 69 64 20 61 20 72 61 63 65 20 63 6f 6e 64 69 74 id a race condit
ee80: 69 6f 6e 20 77 65 20 64 6f 77 6e 67 72 61 64 65 ion we downgrade
ee90: 0a 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63 6b . ** the lock
eea0: 20 69 6e 20 32 20 62 6c 6f 63 6b 73 2c 20 73 6f in 2 blocks, so
eeb0: 20 74 68 61 74 20 70 61 72 74 20 6f 66 20 74 68 that part of th
eec0: 65 20 72 61 6e 67 65 20 77 69 6c 6c 20 62 65 20 e range will be
eed0: 63 6f 76 65 72 65 64 20 62 79 20 61 20 0a 20 20 covered by a .
eee0: 20 20 2a 2a 20 77 72 69 74 65 20 6c 6f 63 6b 20 ** write lock
eef0: 75 6e 74 69 6c 20 74 68 65 20 72 65 73 74 20 69 until the rest i
ef00: 73 20 63 6f 76 65 72 65 64 20 62 79 20 61 20 72 s covered by a r
ef10: 65 61 64 20 6c 6f 63 6b 3a 0a 20 20 20 20 2a 2a ead lock:. **
ef20: 20 20 31 3a 20 20 20 5b 57 57 57 57 57 5d 0a 20 1: [WWWWW].
ef30: 20 20 20 2a 2a 20 20 32 3a 20 20 20 5b 2e 2e 2e ** 2: [...
ef40: 2e 57 5d 0a 20 20 20 20 2a 2a 20 20 33 3a 20 20 .W]. ** 3:
ef50: 20 5b 52 52 52 52 57 5d 0a 20 20 20 20 2a 2a 20 [RRRRW]. **
ef60: 20 34 3a 20 20 20 5b 52 52 52 52 2e 5d 0a 20 20 4: [RRRR.].
ef70: 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 46 69 */. if( eFi
ef80: 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c leLock==SHARED_L
ef90: 4f 43 4b 20 29 7b 0a 23 69 66 20 21 64 65 66 69 OCK ){.#if !defi
efa0: 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 7c ned(__APPLE__) |
efb0: 7c 20 21 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 | !SQLITE_ENABLE
efc0: 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 _LOCKING_STYLE.
efd0: 20 20 20 20 20 28 76 6f 69 64 29 68 61 6e 64 6c (void)handl
efe0: 65 4e 46 53 55 6e 6c 6f 63 6b 3b 0a 20 20 20 20 eNFSUnlock;.
eff0: 20 20 61 73 73 65 72 74 28 20 68 61 6e 64 6c 65 assert( handle
f000: 4e 46 53 55 6e 6c 6f 63 6b 3d 3d 30 20 29 3b 0a NFSUnlock==0 );.
f010: 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e #endif.#if defin
f020: 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 ed(__APPLE__) &&
f030: 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
f040: 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 20 OCKING_STYLE.
f050: 20 20 20 69 66 28 20 68 61 6e 64 6c 65 4e 46 53 if( handleNFS
f060: 55 6e 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 Unlock ){.
f070: 20 20 69 6e 74 20 74 45 72 72 6e 6f 3b 20 20 20 int tErrno;
f080: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
f090: 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 73 rror code from s
f0a0: 79 73 74 65 6d 20 63 61 6c 6c 20 65 72 72 6f 72 ystem call error
f0b0: 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 6f 66 66 s */. off
f0c0: 5f 74 20 64 69 76 53 69 7a 65 20 3d 20 53 48 41 _t divSize = SHA
f0d0: 52 45 44 5f 53 49 5a 45 20 2d 20 31 3b 0a 20 20 RED_SIZE - 1;.
f0e0: 20 20 20 20 20 20 0a 20 20 20 20 20 20 20 20 6c . l
f0f0: 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 ock.l_type = F_U
f100: 4e 4c 43 4b 3b 0a 20 20 20 20 20 20 20 20 6c 6f NLCK;. lo
f110: 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 ck.l_whence = SE
f120: 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20 20 20 EK_SET;.
f130: 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 lock.l_start = S
f140: 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 HARED_FIRST;.
f150: 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 lock.l_len
f160: 3d 20 64 69 76 53 69 7a 65 3b 0a 20 20 20 20 20 = divSize;.
f170: 20 20 20 69 66 28 20 75 6e 69 78 46 69 6c 65 4c if( unixFileL
f180: 6f 63 6b 28 70 46 69 6c 65 2c 20 26 6c 6f 63 6b ock(pFile, &lock
f190: 29 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 )==(-1) ){.
f1a0: 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 tErrno = er
f1b0: 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 72 rno;. r
f1c0: 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 c = SQLITE_IOERR
f1d0: 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 _UNLOCK;.
f1e0: 20 20 20 73 74 6f 72 65 4c 61 73 74 45 72 72 6e storeLastErrn
f1f0: 6f 28 70 46 69 6c 65 2c 20 74 45 72 72 6e 6f 29 o(pFile, tErrno)
f200: 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f ;. goto
f210: 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20 end_unlock;.
f220: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c }. l
f230: 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 ock.l_type = F_R
f240: 44 4c 43 4b 3b 0a 20 20 20 20 20 20 20 20 6c 6f DLCK;. lo
f250: 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 ck.l_whence = SE
f260: 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20 20 20 EK_SET;.
f270: 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 lock.l_start = S
f280: 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 HARED_FIRST;.
f290: 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 lock.l_len
f2a0: 3d 20 64 69 76 53 69 7a 65 3b 0a 20 20 20 20 20 = divSize;.
f2b0: 20 20 20 69 66 28 20 75 6e 69 78 46 69 6c 65 4c if( unixFileL
f2c0: 6f 63 6b 28 70 46 69 6c 65 2c 20 26 6c 6f 63 6b ock(pFile, &lock
f2d0: 29 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 )==(-1) ){.
f2e0: 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 tErrno = er
f2f0: 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 72 rno;. r
f300: 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
f310: 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
f320: 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 rrno, SQLITE_IOE
f330: 52 52 5f 52 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 RR_RDLOCK);.
f340: 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 if( IS_LOC
f350: 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 K_ERROR(rc) ){.
f360: 20 20 20 20 20 20 20 20 20 20 20 73 74 6f 72 65 store
f370: 4c 61 73 74 45 72 72 6e 6f 28 70 46 69 6c 65 2c LastErrno(pFile,
f380: 20 74 45 72 72 6e 6f 29 3b 0a 20 20 20 20 20 20 tErrno);.
f390: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
f3a0: 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b goto end_unlock;
f3b0: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
f3c0: 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d lock.l_type =
f3d0: 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 20 20 F_UNLCK;.
f3e0: 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 lock.l_whence
f3f0: 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 = SEEK_SET;.
f400: 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 lock.l_start
f410: 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b = SHARED_FIRST+
f420: 64 69 76 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 divSize;.
f430: 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 lock.l_len = SH
f440: 41 52 45 44 5f 53 49 5a 45 2d 64 69 76 53 69 7a ARED_SIZE-divSiz
f450: 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 e;. if( u
f460: 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 70 46 69 6c nixFileLock(pFil
f470: 65 2c 20 26 6c 6f 63 6b 29 3d 3d 28 2d 31 29 20 e, &lock)==(-1)
f480: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74 45 72 ){. tEr
f490: 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
f4a0: 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
f4b0: 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b TE_IOERR_UNLOCK;
f4c0: 0a 20 20 20 20 20 20 20 20 20 20 73 74 6f 72 65 . store
f4d0: 4c 61 73 74 45 72 72 6e 6f 28 70 46 69 6c 65 2c LastErrno(pFile,
f4e0: 20 74 45 72 72 6e 6f 29 3b 0a 20 20 20 20 20 20 tErrno);.
f4f0: 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c goto end_unl
f500: 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ock;. }.
f510: 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 }else.#endi
f520: 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 f /* defined(__A
f530: 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 PPLE__) && SQLIT
f540: 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
f550: 5f 53 54 59 4c 45 20 2a 2f 0a 20 20 20 20 20 20 _STYLE */.
f560: 7b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c {. lock.l
f570: 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b _type = F_RDLCK;
f580: 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f . lock.l_
f590: 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 whence = SEEK_SE
f5a0: 54 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e T;. lock.
f5b0: 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 l_start = SHARED
f5c0: 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20 20 20 _FIRST;.
f5d0: 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 lock.l_len = SHA
f5e0: 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 RED_SIZE;.
f5f0: 20 20 69 66 28 20 75 6e 69 78 46 69 6c 65 4c 6f if( unixFileLo
f600: 63 6b 28 70 46 69 6c 65 2c 20 26 6c 6f 63 6b 29 ck(pFile, &lock)
f610: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a ){. /*
f620: 20 49 6e 20 74 68 65 6f 72 79 2c 20 74 68 65 20 In theory, the
f630: 63 61 6c 6c 20 74 6f 20 75 6e 69 78 46 69 6c 65 call to unixFile
f640: 4c 6f 63 6b 28 29 20 63 61 6e 6e 6f 74 20 66 61 Lock() cannot fa
f650: 69 6c 20 62 65 63 61 75 73 65 20 61 6e 6f 74 68 il because anoth
f660: 65 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 er. **
f670: 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69 process is holdi
f680: 6e 67 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 ng an incompatib
f690: 6c 65 20 6c 6f 63 6b 2e 20 49 66 20 69 74 20 64 le lock. If it d
f6a0: 6f 65 73 2c 20 74 68 69 73 20 0a 20 20 20 20 20 oes, this .
f6b0: 20 20 20 20 20 2a 2a 20 69 6e 64 69 63 61 74 65 ** indicate
f6c0: 73 20 74 68 61 74 20 74 68 65 20 6f 74 68 65 72 s that the other
f6d0: 20 70 72 6f 63 65 73 73 20 69 73 20 6e 6f 74 20 process is not
f6e0: 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c 6f following the lo
f6f0: 63 6b 69 6e 67 0a 20 20 20 20 20 20 20 20 20 20 cking.
f700: 2a 2a 20 70 72 6f 74 6f 63 6f 6c 2e 20 49 66 20 ** protocol. If
f710: 74 68 69 73 20 68 61 70 70 65 6e 73 2c 20 72 65 this happens, re
f720: 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
f730: 52 5f 52 44 4c 4f 43 4b 2e 20 52 65 74 75 72 6e R_RDLOCK. Return
f740: 69 6e 67 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ing. **
f750: 20 53 51 4c 49 54 45 5f 42 55 53 59 20 77 6f 75 SQLITE_BUSY wou
f760: 6c 64 20 63 6f 6e 66 75 73 65 20 74 68 65 20 75 ld confuse the u
f770: 70 70 65 72 20 6c 61 79 65 72 20 28 69 6e 20 70 pper layer (in p
f780: 72 61 63 74 69 63 65 20 69 74 20 63 61 75 73 65 ractice it cause
f790: 73 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 s . **
f7a0: 61 6e 20 61 73 73 65 72 74 20 74 6f 20 66 61 69 an assert to fai
f7b0: 6c 29 2e 20 2a 2f 20 0a 20 20 20 20 20 20 20 20 l). */ .
f7c0: 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
f7d0: 45 52 52 5f 52 44 4c 4f 43 4b 3b 0a 20 20 20 20 ERR_RDLOCK;.
f7e0: 20 20 20 20 20 20 73 74 6f 72 65 4c 61 73 74 45 storeLastE
f7f0: 72 72 6e 6f 28 70 46 69 6c 65 2c 20 65 72 72 6e rrno(pFile, errn
f800: 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f o);. go
f810: 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 to end_unlock;.
f820: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
f830: 0a 20 20 20 20 7d 0a 20 20 20 20 6c 6f 63 6b 2e . }. lock.
f840: 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b l_type = F_UNLCK
f850: 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 ;. lock.l_whe
f860: 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a nce = SEEK_SET;.
f870: 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 lock.l_start
f880: 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b = PENDING_BYTE;
f890: 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 . lock.l_len
f8a0: 3d 20 32 4c 3b 20 20 61 73 73 65 72 74 28 20 50 = 2L; assert( P
f8b0: 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 3d 3d 52 ENDING_BYTE+1==R
f8c0: 45 53 45 52 56 45 44 5f 42 59 54 45 20 29 3b 0a ESERVED_BYTE );.
f8d0: 20 20 20 20 69 66 28 20 75 6e 69 78 46 69 6c 65 if( unixFile
f8e0: 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 26 6c 6f 63 Lock(pFile, &loc
f8f0: 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 k)==0 ){. p
f900: 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b Inode->eFileLock
f910: 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a = SHARED_LOCK;.
f920: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
f930: 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 rc = SQLITE_IOE
f940: 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 20 RR_UNLOCK;.
f950: 20 73 74 6f 72 65 4c 61 73 74 45 72 72 6e 6f 28 storeLastErrno(
f960: 70 46 69 6c 65 2c 20 65 72 72 6e 6f 29 3b 0a 20 pFile, errno);.
f970: 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e goto end_un
f980: 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a lock;. }. }.
f990: 20 20 69 66 28 20 65 46 69 6c 65 4c 6f 63 6b 3d if( eFileLock=
f9a0: 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 =NO_LOCK ){.
f9b0: 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 /* Decrement the
f9c0: 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 63 6f 75 shared lock cou
f9d0: 6e 74 65 72 2e 20 20 52 65 6c 65 61 73 65 20 74 nter. Release t
f9e0: 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 61 6e he lock using an
f9f0: 0a 20 20 20 20 2a 2a 20 4f 53 20 63 61 6c 6c 20 . ** OS call
fa00: 6f 6e 6c 79 20 77 68 65 6e 20 61 6c 6c 20 74 68 only when all th
fa10: 72 65 61 64 73 20 69 6e 20 74 68 69 73 20 73 61 reads in this sa
fa20: 6d 65 20 70 72 6f 63 65 73 73 20 68 61 76 65 20 me process have
fa30: 72 65 6c 65 61 73 65 64 0a 20 20 20 20 2a 2a 20 released. **
fa40: 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f the lock.. */
fa50: 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 53 68 . pInode->nSh
fa60: 61 72 65 64 2d 2d 3b 0a 20 20 20 20 69 66 28 20 ared--;. if(
fa70: 70 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 3d pInode->nShared=
fa80: 3d 30 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b =0 ){. lock
fa90: 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 .l_type = F_UNLC
faa0: 4b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f K;. lock.l_
fab0: 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 whence = SEEK_SE
fac0: 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f T;. lock.l_
fad0: 73 74 61 72 74 20 3d 20 6c 6f 63 6b 2e 6c 5f 6c start = lock.l_l
fae0: 65 6e 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 69 en = 0L;. i
faf0: 66 28 20 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 f( unixFileLock(
fb00: 70 46 69 6c 65 2c 20 26 6c 6f 63 6b 29 3d 3d 30 pFile, &lock)==0
fb10: 20 29 7b 0a 20 20 20 20 20 20 20 20 70 49 6e 6f ){. pIno
fb20: 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 de->eFileLock =
fb30: 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 7d NO_LOCK;. }
fb40: 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 else{. rc
fb50: 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
fb60: 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 UNLOCK;.
fb70: 73 74 6f 72 65 4c 61 73 74 45 72 72 6e 6f 28 70 storeLastErrno(p
fb80: 46 69 6c 65 2c 20 65 72 72 6e 6f 29 3b 0a 20 20 File, errno);.
fb90: 20 20 20 20 20 20 70 49 6e 6f 64 65 2d 3e 65 46 pInode->eF
fba0: 69 6c 65 4c 6f 63 6b 20 3d 20 4e 4f 5f 4c 4f 43 ileLock = NO_LOC
fbb0: 4b 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 K;. pFile
fbc0: 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 4e 4f ->eFileLock = NO
fbd0: 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 _LOCK;. }.
fbe0: 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 }.. /* Dec
fbf0: 72 65 6d 65 6e 74 20 74 68 65 20 63 6f 75 6e 74 rement the count
fc00: 20 6f 66 20 6c 6f 63 6b 73 20 61 67 61 69 6e 73 of locks agains
fc10: 74 20 74 68 69 73 20 73 61 6d 65 20 66 69 6c 65 t this same file
fc20: 2e 20 20 57 68 65 6e 20 74 68 65 0a 20 20 20 20 . When the.
fc30: 2a 2a 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 ** count reaches
fc40: 20 7a 65 72 6f 2c 20 63 6c 6f 73 65 20 61 6e 79 zero, close any
fc50: 20 6f 74 68 65 72 20 66 69 6c 65 20 64 65 73 63 other file desc
fc60: 72 69 70 74 6f 72 73 20 77 68 6f 73 65 20 63 6c riptors whose cl
fc70: 6f 73 65 0a 20 20 20 20 2a 2a 20 77 61 73 20 64 ose. ** was d
fc80: 65 66 65 72 72 65 64 20 62 65 63 61 75 73 65 20 eferred because
fc90: 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c of outstanding l
fca0: 6f 63 6b 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ocks.. */.
fcb0: 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 2d 2d pInode->nLock--
fcc0: 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 ;. assert( pI
fcd0: 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 node->nLock>=0 )
fce0: 3b 0a 20 20 20 20 69 66 28 20 70 49 6e 6f 64 65 ;. if( pInode
fcf0: 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 ->nLock==0 ){.
fd00: 20 20 20 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 closePending
fd10: 46 64 73 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 Fds(pFile);.
fd20: 7d 0a 20 20 7d 0a 0a 65 6e 64 5f 75 6e 6c 6f 63 }. }..end_unloc
fd30: 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 k:. unixLeaveMu
fd40: 74 65 78 28 29 3b 0a 20 20 69 66 28 20 72 63 3d tex();. if( rc=
fd50: 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 70 46 69 =SQLITE_OK ) pFi
fd60: 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 le->eFileLock =
fd70: 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20 72 65 74 eFileLock;. ret
fd80: 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
fd90: 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 Lower the locki
fda0: 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 ng level on file
fdb0: 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c descriptor pFil
fdc0: 65 20 74 6f 20 65 46 69 6c 65 4c 6f 63 6b 2e 20 e to eFileLock.
fdd0: 20 65 46 69 6c 65 4c 6f 63 6b 0a 2a 2a 20 6d 75 eFileLock.** mu
fde0: 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f st be either NO_
fdf0: 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c LOCK or SHARED_L
fe00: 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 OCK..**.** If th
fe10: 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
fe20: 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 of the file desc
fe30: 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 riptor is alread
fe40: 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a y at or below.**
fe50: 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
fe60: 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 ocking level, th
fe70: 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
fe80: 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 no-op..*/.static
fe90: 20 69 6e 74 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 int unixUnlock(
fea0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
feb0: 2c 20 69 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29 , int eFileLock)
fec0: 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 {.#if SQLITE_MAX
fed0: 5f 4d 4d 41 50 5f 53 49 5a 45 3e 30 0a 20 20 61 _MMAP_SIZE>0. a
fee0: 73 73 65 72 74 28 20 65 46 69 6c 65 4c 6f 63 6b ssert( eFileLock
fef0: 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 7c 7c ==SHARED_LOCK ||
ff00: 20 28 28 75 6e 69 78 46 69 6c 65 20 2a 29 69 64 ((unixFile *)id
ff10: 29 2d 3e 6e 46 65 74 63 68 4f 75 74 3d 3d 30 20 )->nFetchOut==0
ff20: 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 );.#endif. retu
ff30: 72 6e 20 70 6f 73 69 78 55 6e 6c 6f 63 6b 28 69 rn posixUnlock(i
ff40: 64 2c 20 65 46 69 6c 65 4c 6f 63 6b 2c 20 30 29 d, eFileLock, 0)
ff50: 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f ;.}..#if SQLITE_
ff60: 4d 41 58 5f 4d 4d 41 50 5f 53 49 5a 45 3e 30 0a MAX_MMAP_SIZE>0.
ff70: 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 4d static int unixM
ff80: 61 70 66 69 6c 65 28 75 6e 69 78 46 69 6c 65 20 apfile(unixFile
ff90: 2a 70 46 64 2c 20 69 36 34 20 6e 42 79 74 65 29 *pFd, i64 nByte)
ffa0: 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e ;.static void un
ffb0: 69 78 55 6e 6d 61 70 66 69 6c 65 28 75 6e 69 78 ixUnmapfile(unix
ffc0: 46 69 6c 65 20 2a 70 46 64 29 3b 0a 23 65 6e 64 File *pFd);.#end
ffd0: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 if../*.** This f
ffe0: 75 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73 unction performs
fff0: 20 74 68 65 20 70 61 72 74 73 20 6f 66 20 74 68 the parts of th
10000 65 20 22 63 6c 6f 73 65 20 66 69 6c 65 22 20 6f e "close file" o
10010 70 65 72 61 74 69 6f 6e 20 0a 2a 2a 20 63 6f 6d peration .** com
10020 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 mon to all locki
10030 6e 67 20 73 63 68 65 6d 65 73 2e 20 49 74 20 63 ng schemes. It c
10040 6c 6f 73 65 73 20 74 68 65 20 64 69 72 65 63 74 loses the direct
10050 6f 72 79 20 61 6e 64 20 66 69 6c 65 0a 2a 2a 20 ory and file.**
10060 68 61 6e 64 6c 65 73 2c 20 69 66 20 74 68 65 79 handles, if they
10070 20 61 72 65 20 76 61 6c 69 64 2c 20 61 6e 64 20 are valid, and
10080 73 65 74 73 20 61 6c 6c 20 66 69 65 6c 64 73 20 sets all fields
10090 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 0a of the unixFile.
100a0 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 ** structure to
100b0 30 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 2a 0..**.** It is *
100c0 6e 6f 74 2a 20 6e 65 63 65 73 73 61 72 79 20 74 not* necessary t
100d0 6f 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 o hold the mutex
100e0 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 when this routi
100f0 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a ne is called,.**
10100 20 65 76 65 6e 20 6f 6e 20 56 78 57 6f 72 6b 73 even on VxWorks
10110 2e 20 20 41 20 6d 75 74 65 78 20 77 69 6c 6c 20 . A mutex will
10120 62 65 20 61 63 71 75 69 72 65 64 20 6f 6e 20 56 be acquired on V
10130 78 57 6f 72 6b 73 20 62 79 20 74 68 65 0a 2a 2a xWorks by the.**
10140 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46 vxworksReleaseF
10150 69 6c 65 49 64 28 29 20 72 6f 75 74 69 6e 65 2e ileId() routine.
10160 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 .*/.static int c
10170 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 73 71 6c loseUnixFile(sql
10180 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a ite3_file *id){.
10190 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
101a0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
101b0 64 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 d;.#if SQLITE_MA
101c0 58 5f 4d 4d 41 50 5f 53 49 5a 45 3e 30 0a 20 20 X_MMAP_SIZE>0.
101d0 75 6e 69 78 55 6e 6d 61 70 66 69 6c 65 28 70 46 unixUnmapfile(pF
101e0 69 6c 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 ile);.#endif. i
101f0 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30 20 29 f( pFile->h>=0 )
10200 7b 0a 20 20 20 20 72 6f 62 75 73 74 5f 63 6c 6f {. robust_clo
10210 73 65 28 70 46 69 6c 65 2c 20 70 46 69 6c 65 2d se(pFile, pFile-
10220 3e 68 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0a 20 >h, __LINE__);.
10230 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31 pFile->h = -1
10240 3b 0a 20 20 7d 0a 23 69 66 20 4f 53 5f 56 58 57 ;. }.#if OS_VXW
10250 4f 52 4b 53 0a 20 20 69 66 28 20 70 46 69 6c 65 ORKS. if( pFile
10260 2d 3e 70 49 64 20 29 7b 0a 20 20 20 20 69 66 28 ->pId ){. if(
10270 20 70 46 69 6c 65 2d 3e 63 74 72 6c 46 6c 61 67 pFile->ctrlFlag
10280 73 20 26 20 55 4e 49 58 46 49 4c 45 5f 44 45 4c s & UNIXFILE_DEL
10290 45 54 45 20 29 7b 0a 20 20 20 20 20 20 6f 73 55 ETE ){. osU
102a0 6e 6c 69 6e 6b 28 70 46 69 6c 65 2d 3e 70 49 64 nlink(pFile->pId
102b0 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 ->zCanonicalName
102c0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 76 78 77 );. }. vxw
102d0 6f 72 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49 orksReleaseFileI
102e0 64 28 70 46 69 6c 65 2d 3e 70 49 64 29 3b 0a 20 d(pFile->pId);.
102f0 20 20 20 70 46 69 6c 65 2d 3e 70 49 64 20 3d 20 pFile->pId =
10300 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 0;. }.#endif.#i
10310 66 64 65 66 20 53 51 4c 49 54 45 5f 55 4e 4c 49 fdef SQLITE_UNLI
10320 4e 4b 5f 41 46 54 45 52 5f 43 4c 4f 53 45 0a 20 NK_AFTER_CLOSE.
10330 20 69 66 28 20 70 46 69 6c 65 2d 3e 63 74 72 6c if( pFile->ctrl
10340 46 6c 61 67 73 20 26 20 55 4e 49 58 46 49 4c 45 Flags & UNIXFILE
10350 5f 44 45 4c 45 54 45 20 29 7b 0a 20 20 20 20 6f _DELETE ){. o
10360 73 55 6e 6c 69 6e 6b 28 70 46 69 6c 65 2d 3e 7a sUnlink(pFile->z
10370 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 Path);. sqlit
10380 65 33 5f 66 72 65 65 28 2a 28 63 68 61 72 2a 2a e3_free(*(char**
10390 29 26 70 46 69 6c 65 2d 3e 7a 50 61 74 68 29 3b )&pFile->zPath);
103a0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 7a 50 61 74 . pFile->zPat
103b0 68 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 h = 0;. }.#endi
103c0 66 0a 20 20 4f 53 54 52 41 43 45 28 28 22 43 4c f. OSTRACE(("CL
103d0 4f 53 45 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 OSE %-3d\n", p
103e0 46 69 6c 65 2d 3e 68 29 29 3b 0a 20 20 4f 70 65 File->h));. Ope
103f0 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 nCounter(-1);.
10400 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 sqlite3_free(pFi
10410 6c 65 2d 3e 70 55 6e 75 73 65 64 29 3b 0a 20 20 le->pUnused);.
10420 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c memset(pFile, 0,
10430 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 sizeof(unixFile
10440 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c ));. return SQL
10450 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
10460 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a Close a file..*
10470 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
10480 78 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 xClose(sqlite3_f
10490 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 ile *id){. int
104a0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
104b0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
104c0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 e = (unixFile *)
104d0 69 64 3b 0a 20 20 76 65 72 69 66 79 44 62 46 69 id;. verifyDbFi
104e0 6c 65 28 70 46 69 6c 65 29 3b 0a 20 20 75 6e 69 le(pFile);. uni
104f0 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c xUnlock(id, NO_L
10500 4f 43 4b 29 3b 0a 20 20 75 6e 69 78 45 6e 74 65 OCK);. unixEnte
10510 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 rMutex();.. /*
10520 75 6e 69 78 46 69 6c 65 2e 70 49 6e 6f 64 65 20 unixFile.pInode
10530 69 73 20 61 6c 77 61 79 73 20 76 61 6c 69 64 20 is always valid
10540 68 65 72 65 2e 20 4f 74 68 65 72 77 69 73 65 2c here. Otherwise,
10550 20 61 20 64 69 66 66 65 72 65 6e 74 20 63 6c 6f a different clo
10560 73 65 0a 20 20 2a 2a 20 72 6f 75 74 69 6e 65 20 se. ** routine
10570 28 65 2e 67 2e 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 (e.g. nolockClos
10580 65 28 29 29 20 77 6f 75 6c 64 20 62 65 20 63 61 e()) would be ca
10590 6c 6c 65 64 20 69 6e 73 74 65 61 64 2e 0a 20 20 lled instead..
105a0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 */. assert( pFi
105b0 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 le->pInode->nLoc
105c0 6b 3e 30 20 7c 7c 20 70 46 69 6c 65 2d 3e 70 49 k>0 || pFile->pI
105d0 6e 6f 64 65 2d 3e 62 50 72 6f 63 65 73 73 4c 6f node->bProcessLo
105e0 63 6b 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 41 ck==0 );. if( A
105f0 4c 57 41 59 53 28 70 46 69 6c 65 2d 3e 70 49 6e LWAYS(pFile->pIn
10600 6f 64 65 29 20 26 26 20 70 46 69 6c 65 2d 3e 70 ode) && pFile->p
10610 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a Inode->nLock ){.
10620 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 /* If there
10630 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 are outstanding
10640 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 63 locks, do not ac
10650 74 75 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68 65 tually close the
10660 20 66 69 6c 65 20 6a 75 73 74 0a 20 20 20 20 2a file just. *
10670 2a 20 79 65 74 20 62 65 63 61 75 73 65 20 74 68 * yet because th
10680 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 72 20 74 at would clear t
10690 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 hose locks. Ins
106a0 74 65 61 64 2c 20 61 64 64 20 74 68 65 20 66 69 tead, add the fi
106b0 6c 65 0a 20 20 20 20 2a 2a 20 64 65 73 63 72 69 le. ** descri
106c0 70 74 6f 72 20 74 6f 20 70 49 6e 6f 64 65 2d 3e ptor to pInode->
106d0 70 55 6e 75 73 65 64 20 6c 69 73 74 2e 20 20 49 pUnused list. I
106e0 74 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 t will be automa
106f0 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 64 20 0a tically closed .
10700 20 20 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 ** when the
10710 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65 last lock is cle
10720 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ared.. */.
10730 20 73 65 74 50 65 6e 64 69 6e 67 46 64 28 70 46 setPendingFd(pF
10740 69 6c 65 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 ile);. }. rele
10750 61 73 65 49 6e 6f 64 65 49 6e 66 6f 28 70 46 69 aseInodeInfo(pFi
10760 6c 65 29 3b 0a 20 20 72 63 20 3d 20 63 6c 6f 73 le);. rc = clos
10770 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 eUnixFile(id);.
10780 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 unixLeaveMutex(
10790 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
107a0 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
107b0 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 70 6f ** End of the po
107c0 73 69 78 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 six advisory loc
107d0 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e k implementation
107e0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
107f0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
10800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10840 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
10850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10890 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***.************
108a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
108b0 2a 2a 20 4e 6f 2d 6f 70 20 4c 6f 63 6b 69 6e 67 ** No-op Locking
108c0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
108d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
108e0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 66 20 74 68 65 ***.**.** Of the
108f0 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 various locking
10900 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
10910 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 69 73 available, this
10920 20 69 73 20 62 79 20 66 61 72 20 74 68 65 0a 2a is by far the.*
10930 2a 20 73 69 6d 70 6c 65 73 74 3a 20 20 6c 6f 63 * simplest: loc
10940 6b 69 6e 67 20 69 73 20 69 67 6e 6f 72 65 64 2e king is ignored.
10950 20 20 4e 6f 20 61 74 74 65 6d 70 74 20 69 73 20 No attempt is
10960 6d 61 64 65 20 74 6f 20 6c 6f 63 6b 20 74 68 65 made to lock the
10970 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c database.** fil
10980 65 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 e for reading or
10990 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 writing..**.**
109a0 54 68 69 73 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 This locking mod
109b0 65 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 e is appropriate
109c0 20 66 6f 72 20 75 73 65 20 6f 6e 20 72 65 61 64 for use on read
109d0 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 73 0a -only databases.
109e0 2a 2a 20 28 65 78 3a 20 64 61 74 61 62 61 73 65 ** (ex: database
109f0 73 20 74 68 61 74 20 61 72 65 20 62 75 72 6e 65 s that are burne
10a00 64 20 69 6e 74 6f 20 43 44 2d 52 4f 4d 2c 20 66 d into CD-ROM, f
10a10 6f 72 20 65 78 61 6d 70 6c 65 2e 29 20 20 49 74 or example.) It
10a20 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 can.** also be
10a30 75 73 65 64 20 69 66 20 74 68 65 20 61 70 70 6c used if the appl
10a40 69 63 61 74 69 6f 6e 20 65 6d 70 6c 6f 79 73 20 ication employs
10a50 73 6f 6d 65 20 65 78 74 65 72 6e 61 6c 20 6d 65 some external me
10a60 63 68 61 6e 69 73 6d 20 74 6f 0a 2a 2a 20 70 72 chanism to.** pr
10a70 65 76 65 6e 74 20 73 69 6d 75 6c 74 61 6e 65 6f event simultaneo
10a80 75 73 20 61 63 63 65 73 73 20 6f 66 20 74 68 65 us access of the
10a90 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 62 same database b
10aa0 79 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a y two or more.**
10ab0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
10ac0 74 69 6f 6e 73 2e 20 20 42 75 74 20 74 68 65 72 tions. But ther
10ad0 65 20 69 73 20 61 20 73 65 72 69 6f 75 73 20 72 e is a serious r
10ae0 69 73 6b 20 6f 66 20 64 61 74 61 62 61 73 65 0a isk of database.
10af0 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 66 ** corruption if
10b00 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67 20 6d 6f this locking mo
10b10 64 65 20 69 73 20 75 73 65 64 20 69 6e 20 73 69 de is used in si
10b20 74 75 61 74 69 6f 6e 73 20 77 68 65 72 65 20 6d tuations where m
10b30 75 6c 74 69 70 6c 65 0a 2a 2a 20 64 61 74 61 62 ultiple.** datab
10b40 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 ase connections
10b50 61 72 65 20 61 63 63 65 73 73 69 6e 67 20 74 68 are accessing th
10b60 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 e same database
10b70 66 69 6c 65 20 61 74 20 74 68 65 20 73 61 6d 65 file at the same
10b80 0a 2a 2a 20 74 69 6d 65 20 61 6e 64 20 6f 6e 65 .** time and one
10b90 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 6f 73 or more of thos
10ba0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 e connections ar
10bb0 65 20 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a 0a 73 e writing..*/..s
10bc0 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b tatic int nolock
10bd0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
10be0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
10bf0 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 2a 70 52 NotUsed, int *pR
10c00 65 73 4f 75 74 29 7b 0a 20 20 55 4e 55 53 45 44 esOut){. UNUSED
10c10 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
10c20 65 64 29 3b 0a 20 20 2a 70 52 65 73 4f 75 74 20 ed);. *pResOut
10c30 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 = 0;. return SQ
10c40 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 LITE_OK;.}.stati
10c50 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b c int nolockLock
10c60 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e (sqlite3_file *N
10c70 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 otUsed, int NotU
10c80 73 65 64 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f sed2){. UNUSED_
10c90 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 PARAMETER2(NotUs
10ca0 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 ed, NotUsed2);.
10cb0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
10cc0 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 K;.}.static int
10cd0 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c nolockUnlock(sql
10ce0 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 ite3_file *NotUs
10cf0 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 ed, int NotUsed2
10d00 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
10d10 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 METER2(NotUsed,
10d20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 72 65 74 NotUsed2);. ret
10d30 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
10d40 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 ../*.** Close th
10d50 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 e file..*/.stati
10d60 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 c int nolockClos
10d70 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
10d80 69 64 29 20 7b 0a 20 20 72 65 74 75 72 6e 20 63 id) {. return c
10d90 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 loseUnixFile(id)
10da0 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ;.}../**********
10db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
10dc0 20 74 68 65 20 6e 6f 2d 6f 70 20 6c 6f 63 6b 20 the no-op lock
10dd0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a implementation *
10de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10df0 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****.***********
10e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10e40 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/../*********
10e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10e90 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
10ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
10eb0 42 65 67 69 6e 20 64 6f 74 2d 66 69 6c 65 20 4c Begin dot-file L
10ec0 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ocking *********
10ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10ee0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 *****.**.** The
10ef0 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 dotfile locking
10f00 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 implementation u
10f10 73 65 73 20 74 68 65 20 65 78 69 73 74 65 6e 63 ses the existenc
10f20 65 20 6f 66 20 73 65 70 61 72 61 74 65 20 6c 6f e of separate lo
10f30 63 6b 0a 2a 2a 20 66 69 6c 65 73 20 28 72 65 61 ck.** files (rea
10f40 6c 6c 79 20 61 20 64 69 72 65 63 74 6f 72 79 29 lly a directory)
10f50 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 to control acce
10f60 73 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ss to the databa
10f70 73 65 2e 20 20 54 68 69 73 20 77 6f 72 6b 73 0a se. This works.
10f80 2a 2a 20 6f 6e 20 6a 75 73 74 20 61 62 6f 75 74 ** on just about
10f90 20 65 76 65 72 79 20 66 69 6c 65 73 79 73 74 65 every filesyste
10fa0 6d 20 69 6d 61 67 69 6e 61 62 6c 65 2e 20 20 42 m imaginable. B
10fb0 75 74 20 74 68 65 72 65 20 61 72 65 20 73 65 72 ut there are ser
10fc0 69 6f 75 73 20 64 6f 77 6e 73 69 64 65 73 3a 0a ious downsides:.
10fd0 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20 54 68 **.** (1) Th
10fe0 65 72 65 20 69 73 20 7a 65 72 6f 20 63 6f 6e 63 ere is zero conc
10ff0 75 72 72 65 6e 63 79 2e 20 20 41 20 73 69 6e 67 urrency. A sing
11000 6c 65 20 72 65 61 64 65 72 20 62 6c 6f 63 6b 73 le reader blocks
11010 20 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 20 20 all other.**
11020 20 20 20 20 20 20 63 6f 6e 6e 65 63 74 69 6f 6e connection
11030 73 20 66 72 6f 6d 20 72 65 61 64 69 6e 67 20 6f s from reading o
11040 72 20 77 72 69 74 69 6e 67 20 74 68 65 20 64 61 r writing the da
11050 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 tabase..**.**
11060 20 28 32 29 20 20 41 6e 20 61 70 70 6c 69 63 61 (2) An applica
11070 74 69 6f 6e 20 63 72 61 73 68 20 6f 72 20 70 6f tion crash or po
11080 77 65 72 20 6c 6f 73 73 20 63 61 6e 20 6c 65 61 wer loss can lea
11090 76 65 20 73 74 61 6c 65 20 6c 6f 63 6b 20 66 69 ve stale lock fi
110a0 6c 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 les.** s
110b0 69 74 74 69 6e 67 20 61 72 6f 75 6e 64 20 74 68 itting around th
110c0 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 63 6c at need to be cl
110d0 65 61 72 65 64 20 6d 61 6e 75 61 6c 6c 79 2e 0a eared manually..
110e0 2a 2a 0a 2a 2a 20 4e 65 76 65 72 74 68 65 6c 65 **.** Neverthele
110f0 73 73 2c 20 61 20 64 6f 74 6c 6f 63 6b 20 69 73 ss, a dotlock is
11100 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 an appropriate
11110 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 66 6f 72 locking mode for
11120 20 75 73 65 20 69 66 20 6e 6f 0a 2a 2a 20 6f 74 use if no.** ot
11130 68 65 72 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 her locking stra
11140 74 65 67 79 20 69 73 20 61 76 61 69 6c 61 62 6c tegy is availabl
11150 65 2e 0a 2a 2a 0a 2a 2a 20 44 6f 74 66 69 6c 65 e..**.** Dotfile
11160 20 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73 20 62 locking works b
11170 79 20 63 72 65 61 74 69 6e 67 20 61 20 73 75 62 y creating a sub
11180 64 69 72 65 63 74 6f 72 79 20 69 6e 20 74 68 65 directory in the
11190 20 73 61 6d 65 20 64 69 72 65 63 74 6f 72 79 20 same directory
111a0 61 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 as.** the databa
111b0 73 65 20 61 6e 64 20 77 69 74 68 20 74 68 65 20 se and with the
111c0 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77 69 same name but wi
111d0 74 68 20 61 20 22 2e 6c 6f 63 6b 22 20 65 78 74 th a ".lock" ext
111e0 65 6e 73 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2a ension added..**
111f0 20 54 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f The existence o
11200 66 20 61 20 6c 6f 63 6b 20 64 69 72 65 63 74 6f f a lock directo
11210 72 79 20 69 6d 70 6c 69 65 73 20 61 6e 20 45 58 ry implies an EX
11220 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 41 CLUSIVE lock. A
11230 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 6c 6f 63 6b ll other.** lock
11240 20 74 79 70 65 73 20 28 53 48 41 52 45 44 2c 20 types (SHARED,
11250 52 45 53 45 52 56 45 44 2c 20 50 45 4e 44 49 4e RESERVED, PENDIN
11260 47 29 20 61 72 65 20 6d 61 70 70 65 64 20 69 6e G) are mapped in
11270 74 6f 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2f to EXCLUSIVE..*/
11280 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 ../*.** The file
11290 20 73 75 66 66 69 78 20 61 64 64 65 64 20 74 6f suffix added to
112a0 20 74 68 65 20 64 61 74 61 20 62 61 73 65 20 66 the data base f
112b0 69 6c 65 6e 61 6d 65 20 69 6e 20 6f 72 64 65 72 ilename in order
112c0 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 0a 2a to create the.*
112d0 2a 20 6c 6f 63 6b 20 64 69 72 65 63 74 6f 72 79 * lock directory
112e0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 4f 54 ..*/.#define DOT
112f0 4c 4f 43 4b 5f 53 55 46 46 49 58 20 22 2e 6c 6f LOCK_SUFFIX ".lo
11300 63 6b 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ck"../*.** This
11310 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 routine checks i
11320 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 f there is a RES
11330 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 ERVED lock held
11340 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 on the specified
11350 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 .** file by this
11360 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 or any other pr
11370 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 ocess. If such a
11380 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 lock is held, s
11390 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 et *pResOut.** t
113a0 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c o a non-zero val
113b0 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 ue otherwise *pR
113c0 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 esOut is set to
113d0 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 zero. The retur
113e0 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 n value.** is se
113f0 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 t to SQLITE_OK u
11400 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 nless an I/O err
11410 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
11420 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a lock checking..
11430 2a 2a 0a 2a 2a 20 49 6e 20 64 6f 74 66 69 6c 65 **.** In dotfile
11440 20 6c 6f 63 6b 69 6e 67 2c 20 65 69 74 68 65 72 locking, either
11450 20 61 20 6c 6f 63 6b 20 65 78 69 73 74 73 20 6f a lock exists o
11460 72 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 20 20 r it does not.
11470 53 6f 20 69 6e 20 74 68 69 73 0a 2a 2a 20 76 61 So in this.** va
11480 72 69 61 74 69 6f 6e 20 6f 66 20 43 68 65 63 6b riation of Check
11490 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 29 2c 20 ReservedLock(),
114a0 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 *pResOut is set
114b0 74 6f 20 74 72 75 65 20 69 66 20 61 6e 79 20 6c to true if any l
114c0 6f 63 6b 0a 2a 2a 20 69 73 20 68 65 6c 64 20 6f ock.** is held o
114d0 6e 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 66 n the file and f
114e0 61 6c 73 65 20 69 66 20 74 68 65 20 66 69 6c 65 alse if the file
114f0 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2e 0a 2a 2f is unlocked..*/
11500 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c .static int dotl
11510 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 ockCheckReserved
11520 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
11530 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 e *id, int *pRes
11540 4f 75 74 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 Out) {. int rc
11550 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
11560 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b nt reserved = 0;
11570 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
11580 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
11590 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 id;.. SimulateI
115a0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 OError( return S
115b0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 QLITE_IOERR_CHEC
115c0 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 KRESERVEDLOCK; )
115d0 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 ;. . assert( p
115e0 46 69 6c 65 20 29 3b 0a 20 20 72 65 73 65 72 76 File );. reserv
115f0 65 64 20 3d 20 6f 73 41 63 63 65 73 73 28 28 63 ed = osAccess((c
11600 6f 6e 73 74 20 63 68 61 72 2a 29 70 46 69 6c 65 onst char*)pFile
11610 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
11620 2c 20 30 29 3d 3d 30 3b 0a 20 20 4f 53 54 52 41 , 0)==0;. OSTRA
11630 43 45 28 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 CE(("TEST WR-LOC
11640 4b 20 25 64 20 25 64 20 25 64 20 28 64 6f 74 6c K %d %d %d (dotl
11650 6f 63 6b 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e ock)\n", pFile->
11660 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 h, rc, reserved)
11670 29 3b 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 );. *pResOut =
11680 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 reserved;. retu
11690 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
116a0 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 Lock the file wi
116b0 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 th the lock spec
116c0 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 ified by paramet
116d0 65 72 20 65 46 69 6c 65 4c 6f 63 6b 20 2d 20 6f er eFileLock - o
116e0 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c ne.** of the fol
116f0 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 lowing:.**.**
11700 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 (1) SHARED_LOC
11710 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 K.** (2) RES
11720 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 ERVED_LOCK.**
11730 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f (3) PENDING_LO
11740 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 CK.** (4) EX
11750 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a CLUSIVE_LOCK.**.
11760 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 ** Sometimes whe
11770 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 n requesting one
11780 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 lock state, add
11790 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 itional lock sta
117a0 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 tes.** are inser
117b0 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 ted in between.
117c0 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 The locking mig
117d0 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f ht fail on one o
117e0 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 f the later.** t
117f0 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 ransitions leavi
11800 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 ng the lock stat
11810 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d e different from
11820 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 what it started
11830 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 but.** still sh
11840 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e ort of its goal.
11850 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
11860 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 chart shows the
11870 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 allowed.** trans
11880 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 itions and the i
11890 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 nserted intermed
118a0 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a iate states:.**.
118b0 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d ** UNLOCKED -
118c0 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 > SHARED.** S
118d0 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 HARED -> RESERVE
118e0 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
118f0 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 > (PENDING) -> E
11900 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 XCLUSIVE.** R
11910 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 ESERVED -> (PEND
11920 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
11930 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 E.** PENDING
11940 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a -> EXCLUSIVE.**.
11950 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
11960 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 will only increa
11970 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 se a lock. Use
11980 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c the sqlite3OsUnl
11990 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 ock().** routine
119a0 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b to lower a lock
119b0 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2a 0a 2a 2a ing level..**.**
119c0 20 57 69 74 68 20 64 6f 74 66 69 6c 65 20 6c 6f With dotfile lo
119d0 63 6b 69 6e 67 2c 20 77 65 20 72 65 61 6c 6c 79 cking, we really
119e0 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 20 73 74 only support st
119f0 61 74 65 20 28 34 29 3a 20 45 58 43 4c 55 53 49 ate (4): EXCLUSI
11a00 56 45 2e 0a 2a 2a 20 42 75 74 20 77 65 20 74 72 VE..** But we tr
11a10 61 63 6b 20 74 68 65 20 6f 74 68 65 72 20 6c 6f ack the other lo
11a20 63 6b 69 6e 67 20 6c 65 76 65 6c 73 20 69 6e 74 cking levels int
11a30 65 72 6e 61 6c 6c 79 2e 0a 2a 2f 0a 73 74 61 74 ernally..*/.stat
11a40 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 4c 6f ic int dotlockLo
11a50 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
11a60 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c 65 4c 6f *id, int eFileLo
11a70 63 6b 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 ck) {. unixFile
11a80 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
11a90 69 6c 65 2a 29 69 64 3b 0a 20 20 63 68 61 72 20 ile*)id;. char
11aa0 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 *zLockFile = (ch
11ab0 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b ar *)pFile->lock
11ac0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 69 6e ingContext;. in
11ad0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
11ae0 3b 0a 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 68 ;... /* If we h
11af0 61 76 65 20 61 6e 79 20 6c 6f 63 6b 2c 20 74 68 ave any lock, th
11b00 65 6e 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 en the lock file
11b10 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e already exists.
11b20 20 20 41 6c 6c 20 77 65 20 68 61 76 65 0a 20 20 All we have.
11b30 2a 2a 20 74 6f 20 64 6f 20 69 73 20 61 64 6a 75 ** to do is adju
11b40 73 74 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20 st our internal
11b50 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 6c 6f record of the lo
11b60 63 6b 20 6c 65 76 65 6c 2e 0a 20 20 2a 2f 0a 20 ck level.. */.
11b70 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c if( pFile->eFil
11b80 65 4c 6f 63 6b 20 3e 20 4e 4f 5f 4c 4f 43 4b 20 eLock > NO_LOCK
11b90 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 ){. pFile->eF
11ba0 69 6c 65 4c 6f 63 6b 20 3d 20 65 46 69 6c 65 4c ileLock = eFileL
11bb0 6f 63 6b 3b 0a 20 20 20 20 2f 2a 20 41 6c 77 61 ock;. /* Alwa
11bc0 79 73 20 75 70 64 61 74 65 20 74 68 65 20 74 69 ys update the ti
11bd0 6d 65 73 74 61 6d 70 20 6f 6e 20 74 68 65 20 6f mestamp on the o
11be0 6c 64 20 66 69 6c 65 20 2a 2f 0a 23 69 66 64 65 ld file */.#ifde
11bf0 66 20 48 41 56 45 5f 55 54 49 4d 45 0a 20 20 20 f HAVE_UTIME.
11c00 20 75 74 69 6d 65 28 7a 4c 6f 63 6b 46 69 6c 65 utime(zLockFile
11c10 2c 20 4e 55 4c 4c 29 3b 0a 23 65 6c 73 65 0a 20 , NULL);.#else.
11c20 20 20 20 75 74 69 6d 65 73 28 7a 4c 6f 63 6b 46 utimes(zLockF
11c30 69 6c 65 2c 20 4e 55 4c 4c 29 3b 0a 23 65 6e 64 ile, NULL);.#end
11c40 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 if. return SQ
11c50 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a LITE_OK;. }. .
11c60 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63 /* grab an exc
11c70 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 lusive lock */.
11c80 20 72 63 20 3d 20 6f 73 4d 6b 64 69 72 28 7a 4c rc = osMkdir(zL
11c90 6f 63 6b 46 69 6c 65 2c 20 30 37 37 37 29 3b 0a ockFile, 0777);.
11ca0 20 20 69 66 28 20 72 63 3c 30 20 29 7b 0a 20 20 if( rc<0 ){.
11cb0 20 20 2f 2a 20 66 61 69 6c 65 64 20 74 6f 20 6f /* failed to o
11cc0 70 65 6e 2f 63 72 65 61 74 65 20 74 68 65 20 6c pen/create the l
11cd0 6f 63 6b 20 64 69 72 65 63 74 6f 72 79 20 2a 2f ock directory */
11ce0 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 . int tErrno
11cf0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 = errno;. if(
11d00 20 45 45 58 49 53 54 20 3d 3d 20 74 45 72 72 6e EEXIST == tErrn
11d10 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 o ){. rc =
11d20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 SQLITE_BUSY;.
11d30 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 } else {.
11d40 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 rc = sqliteError
11d50 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 FromPosixError(t
11d60 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f Errno, SQLITE_IO
11d70 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 ERR_LOCK);.
11d80 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
11d90 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 BUSY ){.
11da0 73 74 6f 72 65 4c 61 73 74 45 72 72 6e 6f 28 70 storeLastErrno(p
11db0 46 69 6c 65 2c 20 74 45 72 72 6e 6f 29 3b 0a 20 File, tErrno);.
11dc0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
11dd0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 return rc;. }
11de0 0a 20 20 0a 20 20 2f 2a 20 67 6f 74 20 69 74 2c . . /* got it,
11df0 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61 6e set the type an
11e00 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 d return ok */.
11e10 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 pFile->eFileLoc
11e20 6b 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 k = eFileLock;.
11e30 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
11e40 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c *.** Lower the l
11e50 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 ocking level on
11e60 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
11e70 70 46 69 6c 65 20 74 6f 20 65 46 69 6c 65 4c 6f pFile to eFileLo
11e80 63 6b 2e 20 20 65 46 69 6c 65 4c 6f 63 6b 0a 2a ck. eFileLock.*
11e90 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 * must be either
11ea0 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 NO_LOCK or SHAR
11eb0 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 ED_LOCK..**.** I
11ec0 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 f the locking le
11ed0 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 vel of the file
11ee0 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c descriptor is al
11ef0 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f ready at or belo
11f00 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 w.** the request
11f10 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c ed locking level
11f20 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 , this routine i
11f30 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a s a no-op..**.**
11f40 20 57 68 65 6e 20 74 68 65 20 6c 6f 63 6b 69 6e When the lockin
11f50 67 20 6c 65 76 65 6c 20 72 65 61 63 68 65 73 20 g level reaches
11f60 4e 4f 5f 4c 4f 43 4b 2c 20 64 65 6c 65 74 65 20 NO_LOCK, delete
11f70 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a the lock file..*
11f80 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 /.static int dot
11f90 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 lockUnlock(sqlit
11fa0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
11fb0 20 65 46 69 6c 65 4c 6f 63 6b 29 20 7b 0a 20 20 eFileLock) {.
11fc0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
11fd0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
11fe0 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 . char *zLockFi
11ff0 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 70 46 69 le = (char *)pFi
12000 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
12010 78 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 xt;. int rc;..
12020 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
12030 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 55 4e ;. OSTRACE(("UN
12040 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 LOCK %d %d was
12050 25 64 20 70 69 64 3d 25 64 20 28 64 6f 74 6c 6f %d pid=%d (dotlo
12060 63 6b 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 ck)\n", pFile->h
12070 2c 20 65 46 69 6c 65 4c 6f 63 6b 2c 0a 20 20 20 , eFileLock,.
12080 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 65 pFile->e
12090 46 69 6c 65 4c 6f 63 6b 2c 20 6f 73 47 65 74 70 FileLock, osGetp
120a0 69 64 28 30 29 29 29 3b 0a 20 20 61 73 73 65 72 id(0)));. asser
120b0 74 28 20 65 46 69 6c 65 4c 6f 63 6b 3c 3d 53 48 t( eFileLock<=SH
120c0 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a ARED_LOCK );. .
120d0 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f /* no-op if po
120e0 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 ssible */. if(
120f0 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b pFile->eFileLock
12100 3d 3d 65 46 69 6c 65 4c 6f 63 6b 20 29 7b 0a 20 ==eFileLock ){.
12110 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
12120 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 _OK;. }.. /* T
12130 6f 20 64 6f 77 6e 67 72 61 64 65 20 74 6f 20 73 o downgrade to s
12140 68 61 72 65 64 2c 20 73 69 6d 70 6c 79 20 75 70 hared, simply up
12150 64 61 74 65 20 6f 75 72 20 69 6e 74 65 72 6e 61 date our interna
12160 6c 20 6e 6f 74 69 6f 6e 20 6f 66 20 74 68 65 0a l notion of the.
12170 20 20 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 2e ** lock state.
12180 20 20 4e 6f 20 6e 65 65 64 20 74 6f 20 6d 65 73 No need to mes
12190 73 20 77 69 74 68 20 74 68 65 20 66 69 6c 65 20 s with the file
121a0 6f 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 on disk.. */.
121b0 69 66 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 if( eFileLock==S
121c0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 HARED_LOCK ){.
121d0 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f pFile->eFileLo
121e0 63 6b 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b ck = SHARED_LOCK
121f0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
12200 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 ITE_OK;. }. .
12210 20 2f 2a 20 54 6f 20 66 75 6c 6c 79 20 75 6e 6c /* To fully unl
12220 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 ock the database
12230 2c 20 64 65 6c 65 74 65 20 74 68 65 20 6c 6f 63 , delete the loc
12240 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 61 73 73 65 k file */. asse
12250 72 74 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 4e rt( eFileLock==N
12260 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 72 63 20 3d O_LOCK );. rc =
12270 20 6f 73 52 6d 64 69 72 28 7a 4c 6f 63 6b 46 69 osRmdir(zLockFi
12280 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3c 30 20 le);. if( rc<0
12290 29 7b 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e ){. int tErrn
122a0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 o = errno;. i
122b0 66 28 20 74 45 72 72 6e 6f 3d 3d 45 4e 4f 45 4e f( tErrno==ENOEN
122c0 54 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 T ){. rc =
122d0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
122e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d else{. rc =
122f0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e SQLITE_IOERR_UN
12300 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 73 74 6f 72 LOCK;. stor
12310 65 4c 61 73 74 45 72 72 6e 6f 28 70 46 69 6c 65 eLastErrno(pFile
12320 2c 20 74 45 72 72 6e 6f 29 3b 0a 20 20 20 20 7d , tErrno);. }
12330 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 20 . return rc;
12340 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 65 46 . }. pFile->eF
12350 69 6c 65 4c 6f 63 6b 20 3d 20 4e 4f 5f 4c 4f 43 ileLock = NO_LOC
12360 4b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 K;. return SQLI
12370 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
12380 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 20 20 4d Close a file. M
12390 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 ake sure the loc
123a0 6b 20 68 61 73 20 62 65 65 6e 20 72 65 6c 65 61 k has been relea
123b0 73 65 64 20 62 65 66 6f 72 65 20 63 6c 6f 73 69 sed before closi
123c0 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
123d0 74 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73 65 28 73 t dotlockClose(s
123e0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
123f0 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 {. unixFile *p
12400 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
12410 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 *)id;. assert(
12420 69 64 21 3d 30 20 29 3b 0a 20 20 64 6f 74 6c 6f id!=0 );. dotlo
12430 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f ckUnlock(id, NO_
12440 4c 4f 43 4b 29 3b 0a 20 20 73 71 6c 69 74 65 33 LOCK);. sqlite3
12450 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 _free(pFile->loc
12460 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 kingContext);.
12470 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 return closeUnix
12480 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 2f 2a 2a 2a File(id);.}./***
12490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
124a0 45 6e 64 20 6f 66 20 74 68 65 20 64 6f 74 2d 66 End of the dot-f
124b0 69 6c 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 ile lock impleme
124c0 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a ntation ********
124d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
124e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
124f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a **********/../**
12530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
12580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12590 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 6c ******* Begin fl
125a0 6f 63 6b 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a ock Locking ****
125b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
125c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
125d0 2a 2a 20 55 73 65 20 74 68 65 20 66 6c 6f 63 6b ** Use the flock
125e0 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 74 () system call t
125f0 6f 20 64 6f 20 66 69 6c 65 20 6c 6f 63 6b 69 6e o do file lockin
12600 67 2e 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29 g..**.** flock()
12610 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 69 6b 65 locking is like
12620 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e dot-file lockin
12630 67 20 69 6e 20 74 68 61 74 20 74 68 65 20 76 61 g in that the va
12640 72 69 6f 75 73 0a 2a 2a 20 66 69 6e 65 2d 67 72 rious.** fine-gr
12650 61 69 6e 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 ain locking leve
12660 6c 73 20 73 75 70 70 6f 72 74 65 64 20 62 79 20 ls supported by
12670 53 51 4c 69 74 65 20 61 72 65 20 63 6f 6c 6c 61 SQLite are colla
12680 70 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 61 20 73 psed into.** a s
12690 69 6e 67 6c 65 20 65 78 63 6c 75 73 69 76 65 20 ingle exclusive
126a0 6c 6f 63 6b 2e 20 20 49 6e 20 6f 74 68 65 72 20 lock. In other
126b0 77 6f 72 64 73 2c 20 53 48 41 52 45 44 2c 20 52 words, SHARED, R
126c0 45 53 45 52 56 45 44 2c 20 61 6e 64 0a 2a 2a 20 ESERVED, and.**
126d0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 73 20 61 72 PENDING locks ar
126e0 65 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 e the same thing
126f0 20 61 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 as an EXCLUSIVE
12700 20 6c 6f 63 6b 2e 20 20 53 51 4c 69 74 65 0a 2a lock. SQLite.*
12710 2a 20 73 74 69 6c 6c 20 77 6f 72 6b 73 20 77 68 * still works wh
12720 65 6e 20 79 6f 75 20 64 6f 20 74 68 69 73 2c 20 en you do this,
12730 62 75 74 20 63 6f 6e 63 75 72 72 65 6e 63 79 20 but concurrency
12740 69 73 20 72 65 64 75 63 65 64 20 73 69 6e 63 65 is reduced since
12750 0a 2a 2a 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c .** only a singl
12760 65 20 70 72 6f 63 65 73 73 20 63 61 6e 20 62 65 e process can be
12770 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 reading the dat
12780 61 62 61 73 65 20 61 74 20 61 20 74 69 6d 65 2e abase at a time.
12790 0a 2a 2a 0a 2a 2a 20 4f 6d 69 74 20 74 68 69 73 .**.** Omit this
127a0 20 73 65 63 74 69 6f 6e 20 69 66 20 53 51 4c 49 section if SQLI
127b0 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
127c0 47 5f 53 54 59 4c 45 20 69 73 20 74 75 72 6e 65 G_STYLE is turne
127d0 64 20 6f 66 66 0a 2a 2f 0a 23 69 66 20 53 51 4c d off.*/.#if SQL
127e0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
127f0 4e 47 5f 53 54 59 4c 45 0a 0a 2f 2a 0a 2a 2a 20 NG_STYLE../*.**
12800 52 65 74 72 79 20 66 6c 6f 63 6b 28 29 20 63 61 Retry flock() ca
12810 6c 6c 73 20 74 68 61 74 20 66 61 69 6c 20 77 69 lls that fail wi
12820 74 68 20 45 49 4e 54 52 0a 2a 2f 0a 23 69 66 64 th EINTR.*/.#ifd
12830 65 66 20 45 49 4e 54 52 0a 73 74 61 74 69 63 20 ef EINTR.static
12840 69 6e 74 20 72 6f 62 75 73 74 5f 66 6c 6f 63 6b int robust_flock
12850 28 69 6e 74 20 66 64 2c 20 69 6e 74 20 6f 70 29 (int fd, int op)
12860 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 64 6f {. int rc;. do
12870 7b 20 72 63 20 3d 20 66 6c 6f 63 6b 28 66 64 2c { rc = flock(fd,
12880 6f 70 29 3b 20 7d 77 68 69 6c 65 28 20 72 63 3c op); }while( rc<
12890 30 20 26 26 20 65 72 72 6e 6f 3d 3d 45 49 4e 54 0 && errno==EINT
128a0 52 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 R );. return rc
128b0 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 ;.}.#else.# defi
128c0 6e 65 20 72 6f 62 75 73 74 5f 66 6c 6f 63 6b 28 ne robust_flock(
128d0 61 2c 62 29 20 66 6c 6f 63 6b 28 61 2c 62 29 0a a,b) flock(a,b).
128e0 23 65 6e 64 69 66 0a 20 20 20 20 20 0a 0a 2f 2a #endif. ../*
128f0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
12900 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 checks if there
12910 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c is a RESERVED l
12920 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 ock held on the
12930 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c specified.** fil
12940 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 e by this or any
12950 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 other process.
12960 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 If such a lock i
12970 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 s held, set *pRe
12980 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e sOut.** to a non
12990 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 -zero value othe
129a0 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 rwise *pResOut i
129b0 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 s set to zero.
129c0 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
129d0 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 .** is set to SQ
129e0 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 LITE_OK unless a
129f0 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 n I/O error occu
12a00 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 rs during lock c
12a10 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 hecking..*/.stat
12a20 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 68 65 63 ic int flockChec
12a30 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 kReservedLock(sq
12a40 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
12a50 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 int *pResOut){.
12a60 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
12a70 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 _OK;. int reser
12a80 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 ved = 0;. unixF
12a90 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
12aa0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 ixFile*)id;. .
12ab0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
12ac0 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
12ad0 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 IOERR_CHECKRESER
12ae0 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 VEDLOCK; );. .
12af0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
12b00 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 ;. . /* Check
12b10 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 if a thread in t
12b20 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 his process hold
12b30 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f s such a lock */
12b40 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 . if( pFile->eF
12b50 69 6c 65 4c 6f 63 6b 3e 53 48 41 52 45 44 5f 4c ileLock>SHARED_L
12b60 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 OCK ){. reser
12b70 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a ved = 1;. }. .
12b80 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 /* Otherwise s
12b90 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 ee if some other
12ba0 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 process holds i
12bb0 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 t. */. if( !res
12bc0 65 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 erved ){. /*
12bd0 61 74 74 65 6d 70 74 20 74 6f 20 67 65 74 20 74 attempt to get t
12be0 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 he lock */. i
12bf0 6e 74 20 6c 72 63 20 3d 20 72 6f 62 75 73 74 5f nt lrc = robust_
12c00 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 flock(pFile->h,
12c10 4c 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e LOCK_EX | LOCK_N
12c20 42 29 3b 0a 20 20 20 20 69 66 28 20 21 6c 72 63 B);. if( !lrc
12c30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 67 6f 74 ){. /* got
12c40 20 74 68 65 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 the lock, unloc
12c50 6b 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 6c 72 k it */. lr
12c60 63 20 3d 20 72 6f 62 75 73 74 5f 66 6c 6f 63 6b c = robust_flock
12c70 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f (pFile->h, LOCK_
12c80 55 4e 29 3b 0a 20 20 20 20 20 20 69 66 20 28 20 UN);. if (
12c90 6c 72 63 20 29 20 7b 0a 20 20 20 20 20 20 20 20 lrc ) {.
12ca0 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 int tErrno = err
12cb0 6e 6f 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 75 no;. /* u
12cc0 6e 6c 6f 63 6b 20 66 61 69 6c 65 64 20 77 69 74 nlock failed wit
12cd0 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 h an error */.
12ce0 20 20 20 20 20 20 6c 72 63 20 3d 20 53 51 4c 49 lrc = SQLI
12cf0 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b TE_IOERR_UNLOCK;
12d00 20 0a 20 20 20 20 20 20 20 20 73 74 6f 72 65 4c . storeL
12d10 61 73 74 45 72 72 6e 6f 28 70 46 69 6c 65 2c 20 astErrno(pFile,
12d20 74 45 72 72 6e 6f 29 3b 0a 20 20 20 20 20 20 20 tErrno);.
12d30 20 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20 20 20 rc = lrc;.
12d40 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a }. } else {.
12d50 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f int tErrno
12d60 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
12d70 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 reserved = 1;.
12d80 20 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 /* someone e
12d90 6c 73 65 20 6d 69 67 68 74 20 68 61 76 65 20 69 lse might have i
12da0 74 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20 20 t reserved */.
12db0 20 20 20 20 6c 72 63 20 3d 20 73 71 6c 69 74 65 lrc = sqlite
12dc0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
12dd0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
12de0 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 20 TE_IOERR_LOCK);
12df0 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f . if( IS_LO
12e00 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 7b CK_ERROR(lrc) ){
12e10 0a 20 20 20 20 20 20 20 20 73 74 6f 72 65 4c 61 . storeLa
12e20 73 74 45 72 72 6e 6f 28 70 46 69 6c 65 2c 20 74 stErrno(pFile, t
12e30 45 72 72 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 Errno);.
12e40 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20 20 20 20 rc = lrc;.
12e50 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 }. }. }. OS
12e60 54 52 41 43 45 28 28 22 54 45 53 54 20 57 52 2d TRACE(("TEST WR-
12e70 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 20 28 66 LOCK %d %d %d (f
12e80 6c 6f 63 6b 29 5c 6e 22 2c 20 70 46 69 6c 65 2d lock)\n", pFile-
12e90 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 >h, rc, reserved
12ea0 29 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ));..#ifdef SQLI
12eb0 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f TE_IGNORE_FLOCK_
12ec0 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 69 66 LOCK_ERRORS. if
12ed0 28 20 28 72 63 20 26 20 53 51 4c 49 54 45 5f 49 ( (rc & SQLITE_I
12ee0 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f OERR) == SQLITE_
12ef0 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 72 63 20 IOERR ){. rc
12f00 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
12f10 20 72 65 73 65 72 76 65 64 3d 31 3b 0a 20 20 7d reserved=1;. }
12f20 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
12f30 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c E_IGNORE_FLOCK_L
12f40 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 OCK_ERRORS */.
12f50 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 *pResOut = reser
12f60 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ved;. return rc
12f70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 ;.}../*.** Lock
12f80 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 the file with th
12f90 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 e lock specified
12fa0 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 65 46 by parameter eF
12fb0 69 6c 65 4c 6f 63 6b 20 2d 20 6f 6e 65 0a 2a 2a ileLock - one.**
12fc0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
12fd0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 g:.**.** (1)
12fe0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 SHARED_LOCK.**
12ff0 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 (2) RESERVED
13000 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 _LOCK.** (3)
13010 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a PENDING_LOCK.**
13020 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 (4) EXCLUSI
13030 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f VE_LOCK.**.** So
13040 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 metimes when req
13050 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b uesting one lock
13060 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e state, addition
13070 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a al lock states.*
13080 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 * are inserted i
13090 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 n between. The
130a0 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 locking might fa
130b0 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 il on one of the
130c0 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 later.** transi
130d0 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 tions leaving th
130e0 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 e lock state dif
130f0 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 ferent from what
13100 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a it started but.
13110 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f ** still short o
13120 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 f its goal. The
13130 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 following chart
13140 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 shows the allow
13150 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e ed.** transition
13160 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 s and the insert
13170 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 ed intermediate
13180 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 states:.**.**
13190 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 UNLOCKED -> SHA
131a0 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 RED.** SHARED
131b0 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 -> RESERVED.**
131c0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 SHARED -> (PE
131d0 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
131e0 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 IVE.** RESERV
131f0 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 ED -> (PENDING)
13200 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 -> EXCLUSIVE.**
13210 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 PENDING -> EX
13220 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 66 6c CLUSIVE.**.** fl
13230 6f 63 6b 28 29 20 6f 6e 6c 79 20 72 65 61 6c 6c ock() only reall
13240 79 20 73 75 70 70 6f 72 74 20 45 58 43 4c 55 53 y support EXCLUS
13250 49 56 45 20 6c 6f 63 6b 73 2e 20 20 57 65 20 74 IVE locks. We t
13260 72 61 63 6b 20 69 6e 74 65 72 6d 65 64 69 61 74 rack intermediat
13270 65 0a 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 73 e.** lock states
13280 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f in the sqlite3_
13290 66 69 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20 file structure,
132a0 62 75 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 53 48 but all locks SH
132b0 41 52 45 44 20 6f 72 0a 2a 2a 20 61 62 6f 76 65 ARED or.** above
132c0 20 61 72 65 20 72 65 61 6c 6c 79 20 45 58 43 4c are really EXCL
132d0 55 53 49 56 45 20 6c 6f 63 6b 73 20 61 6e 64 20 USIVE locks and
132e0 65 78 63 6c 75 64 65 20 61 6c 6c 20 6f 74 68 65 exclude all othe
132f0 72 20 70 72 6f 63 65 73 73 65 73 20 66 72 6f 6d r processes from
13300 0a 2a 2a 20 61 63 63 65 73 73 20 74 68 65 20 66 .** access the f
13310 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ile..**.** This
13320 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c routine will onl
13330 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 y increase a loc
13340 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 k. Use the sqli
13350 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a te3OsUnlock().**
13360 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 routine to lowe
13370 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 r a locking leve
13380 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 l..*/.static int
13390 20 66 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 flockLock(sqlit
133a0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
133b0 20 65 46 69 6c 65 4c 6f 63 6b 29 20 7b 0a 20 20 eFileLock) {.
133c0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
133d0 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a OK;. unixFile *
133e0 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
133f0 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 e*)id;.. assert
13400 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a ( pFile );.. /*
13410 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20 68 if we already h
13420 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69 ave a lock, it i
13430 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a 20 s exclusive. .
13440 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 20 ** Just adjust
13450 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f level and punt o
13460 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a 2f n outta here. */
13470 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 65 46 . if (pFile->eF
13480 69 6c 65 4c 6f 63 6b 20 3e 20 4e 4f 5f 4c 4f 43 ileLock > NO_LOC
13490 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K) {. pFile->
134a0 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 65 46 69 6c eFileLock = eFil
134b0 65 4c 6f 63 6b 3b 0a 20 20 20 20 72 65 74 75 72 eLock;. retur
134c0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
134d0 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e . . /* grab an
134e0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 exclusive lock
134f0 2a 2f 0a 20 20 0a 20 20 69 66 20 28 72 6f 62 75 */. . if (robu
13500 73 74 5f 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e st_flock(pFile->
13510 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 h, LOCK_EX | LOC
13520 4b 5f 4e 42 29 29 20 7b 0a 20 20 20 20 69 6e 74 K_NB)) {. int
13530 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b tErrno = errno;
13540 0a 20 20 20 20 2f 2a 20 64 69 64 6e 27 74 20 67 . /* didn't g
13550 65 74 2c 20 6d 75 73 74 20 62 65 20 62 75 73 79 et, must be busy
13560 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c */. rc = sql
13570 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
13580 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
13590 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b QLITE_IOERR_LOCK
135a0 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f );. if( IS_LO
135b0 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a CK_ERROR(rc) ){.
135c0 20 20 20 20 20 20 73 74 6f 72 65 4c 61 73 74 45 storeLastE
135d0 72 72 6e 6f 28 70 46 69 6c 65 2c 20 74 45 72 72 rrno(pFile, tErr
135e0 6e 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 no);. }. } e
135f0 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 67 6f 74 lse {. /* got
13600 20 69 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 it, set the typ
13610 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 e and return ok
13620 2a 2f 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 */. pFile->eF
13630 69 6c 65 4c 6f 63 6b 20 3d 20 65 46 69 6c 65 4c ileLock = eFileL
13640 6f 63 6b 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 ock;. }. OSTRA
13650 43 45 28 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 CE(("LOCK %d
13660 25 73 20 25 73 20 28 66 6c 6f 63 6b 29 5c 6e 22 %s %s (flock)\n"
13670 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 61 7a 46 69 , pFile->h, azFi
13680 6c 65 4c 6f 63 6b 28 65 46 69 6c 65 4c 6f 63 6b leLock(eFileLock
13690 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 72 ), . r
136a0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 c==SQLITE_OK ? "
136b0 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 ok" : "failed"))
136c0 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
136d0 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 IGNORE_FLOCK_LOC
136e0 4b 5f 45 52 52 4f 52 53 0a 20 20 69 66 28 20 28 K_ERRORS. if( (
136f0 72 63 20 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 rc & SQLITE_IOER
13700 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 R) == SQLITE_IOE
13710 52 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 RR ){. rc = S
13720 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a QLITE_BUSY;. }.
13730 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
13740 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f _IGNORE_FLOCK_LO
13750 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 72 CK_ERRORS */. r
13760 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
13770 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f .** Lower the lo
13780 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 cking level on f
13790 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 ile descriptor p
137a0 46 69 6c 65 20 74 6f 20 65 46 69 6c 65 4c 6f 63 File to eFileLoc
137b0 6b 2e 20 20 65 46 69 6c 65 4c 6f 63 6b 0a 2a 2a k. eFileLock.**
137c0 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 must be either
137d0 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 NO_LOCK or SHARE
137e0 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 D_LOCK..**.** If
137f0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
13800 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 el of the file d
13810 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 escriptor is alr
13820 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 eady at or below
13830 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 .** the requeste
13840 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c d locking level,
13850 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
13860 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 a no-op..*/.sta
13870 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 55 6e 6c tic int flockUnl
13880 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
13890 20 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c 65 4c *id, int eFileL
138a0 6f 63 6b 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c ock) {. unixFil
138b0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
138c0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 61 File*)id;. . a
138d0 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a ssert( pFile );.
138e0 20 20 4f 53 54 52 41 43 45 28 28 22 55 4e 4c 4f OSTRACE(("UNLO
138f0 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 CK %d %d was %d
13900 20 70 69 64 3d 25 64 20 28 66 6c 6f 63 6b 29 5c pid=%d (flock)\
13910 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 65 46 n", pFile->h, eF
13920 69 6c 65 4c 6f 63 6b 2c 0a 20 20 20 20 20 20 20 ileLock,.
13930 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 pFile->eFile
13940 4c 6f 63 6b 2c 20 6f 73 47 65 74 70 69 64 28 30 Lock, osGetpid(0
13950 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 )));. assert( e
13960 46 69 6c 65 4c 6f 63 6b 3c 3d 53 48 41 52 45 44 FileLock<=SHARED
13970 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a _LOCK );. . /*
13980 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 no-op if possib
13990 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c le */. if( pFil
139a0 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 65 46 e->eFileLock==eF
139b0 69 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 72 ileLock ){. r
139c0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
139d0 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61 . }. . /* sha
139e0 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 65 20 red can just be
139f0 73 65 74 20 62 65 63 61 75 73 65 20 77 65 20 61 set because we a
13a00 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 65 78 lways have an ex
13a10 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 66 20 clusive */. if
13a20 28 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 (eFileLock==SHAR
13a30 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 ED_LOCK) {. p
13a40 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 File->eFileLock
13a50 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20 20 = eFileLock;.
13a60 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
13a70 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e K;. }. . /* n
13a80 6f 2c 20 72 65 61 6c 6c 79 2c 20 75 6e 6c 6f 63 o, really, unloc
13a90 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 72 6f 62 75 k. */. if( robu
13aa0 73 74 5f 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e st_flock(pFile->
13ab0 68 2c 20 4c 4f 43 4b 5f 55 4e 29 20 29 7b 0a 23 h, LOCK_UN) ){.#
13ac0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e ifdef SQLITE_IGN
13ad0 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 ORE_FLOCK_LOCK_E
13ae0 52 52 4f 52 53 0a 20 20 20 20 72 65 74 75 72 6e RRORS. return
13af0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 SQLITE_OK;.#end
13b00 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e if /* SQLITE_IGN
13b10 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 ORE_FLOCK_LOCK_E
13b20 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 72 65 74 RRORS */. ret
13b30 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
13b40 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 7d 65 6c 73 65 _UNLOCK;. }else
13b50 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 {. pFile->eFi
13b60 6c 65 4c 6f 63 6b 20 3d 20 4e 4f 5f 4c 4f 43 4b leLock = NO_LOCK
13b70 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
13b80 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f ITE_OK;. }.}../
13b90 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c *.** Close a fil
13ba0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
13bb0 20 66 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 flockClose(sqli
13bc0 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a te3_file *id) {.
13bd0 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 assert( id!=0
13be0 29 3b 0a 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b );. flockUnlock
13bf0 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 (id, NO_LOCK);.
13c00 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69 return closeUni
13c10 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 23 65 xFile(id);.}..#e
13c20 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 ndif /* SQLITE_E
13c30 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
13c40 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 YLE && !OS_VXWOR
13c50 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a K */../*********
13c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
13c70 66 20 74 68 65 20 66 6c 6f 63 6b 20 6c 6f 63 6b f the flock lock
13c80 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
13c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13ca0 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
13cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13cf0 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ****/../********
13d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13d40 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
13d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
13d60 42 65 67 69 6e 20 4e 61 6d 65 64 20 53 65 6d 61 Begin Named Sema
13d70 70 68 6f 72 65 20 4c 6f 63 6b 69 6e 67 20 2a 2a phore Locking **
13d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13d90 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 61 6d ******.**.** Nam
13da0 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 ed semaphore loc
13db0 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 73 75 70 king is only sup
13dc0 70 6f 72 74 65 64 20 6f 6e 20 56 78 57 6f 72 6b ported on VxWork
13dd0 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70 68 6f s..**.** Semapho
13de0 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 69 re locking is li
13df0 6b 65 20 64 6f 74 2d 6c 6f 63 6b 20 61 6e 64 20 ke dot-lock and
13e00 66 6c 6f 63 6b 20 69 6e 20 74 68 61 74 20 69 74 flock in that it
13e10 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 0a 2a 2a 20 really only.**
13e20 73 75 70 70 6f 72 74 73 20 45 58 43 4c 55 53 49 supports EXCLUSI
13e30 56 45 20 6c 6f 63 6b 69 6e 67 2e 20 20 4f 6e 6c VE locking. Onl
13e40 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 y a single proce
13e50 73 73 20 63 61 6e 20 72 65 61 64 20 6f 72 20 77 ss can read or w
13e60 72 69 74 65 0a 2a 2a 20 74 68 65 20 64 61 74 61 rite.** the data
13e70 62 61 73 65 20 66 69 6c 65 20 61 74 20 61 20 74 base file at a t
13e80 69 6d 65 2e 20 20 54 68 69 73 20 72 65 64 75 63 ime. This reduc
13e90 65 73 20 70 6f 74 65 6e 74 69 61 6c 20 63 6f 6e es potential con
13ea0 63 75 72 72 65 6e 63 79 2c 20 62 75 74 0a 2a 2a currency, but.**
13eb0 20 6d 61 6b 65 73 20 74 68 65 20 6c 6f 63 6b 20 makes the lock
13ec0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d implementation m
13ed0 75 63 68 20 65 61 73 69 65 72 2e 0a 2a 2f 0a 23 uch easier..*/.#
13ee0 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f if OS_VXWORKS../
13ef0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
13f00 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 e checks if ther
13f10 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 e is a RESERVED
13f20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 lock held on the
13f30 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 specified.** fi
13f40 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e le by this or an
13f50 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e y other process.
13f60 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 If such a lock
13f70 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 is held, set *pR
13f80 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f esOut.** to a no
13f90 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 n-zero value oth
13fa0 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 erwise *pResOut
13fb0 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 is set to zero.
13fc0 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
13fd0 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 e.** is set to S
13fe0 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 QLITE_OK unless
13ff0 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 an I/O error occ
14000 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 urs during lock
14010 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 checking..*/.sta
14020 74 69 63 20 69 6e 74 20 73 65 6d 58 43 68 65 63 tic int semXChec
14030 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 kReservedLock(sq
14040 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
14050 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a int *pResOut) {.
14060 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
14070 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 E_OK;. int rese
14080 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 rved = 0;. unix
14090 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
140a0 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 nixFile*)id;..
140b0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
140c0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
140d0 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 OERR_CHECKRESERV
140e0 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 EDLOCK; );. .
140f0 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
14100 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 .. /* Check if
14110 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 a thread in this
14120 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 process holds s
14130 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 uch a lock */.
14140 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 if( pFile->eFile
14150 4c 6f 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43 4b Lock>SHARED_LOCK
14160 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 ){. reserved
14170 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f = 1;. }. . /
14180 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 * Otherwise see
14190 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 if some other pr
141a0 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 ocess holds it.
141b0 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 */. if( !reserv
141c0 65 64 20 29 7b 0a 20 20 20 20 73 65 6d 5f 74 20 ed ){. sem_t
141d0 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 *pSem = pFile->p
141e0 49 6e 6f 64 65 2d 3e 70 53 65 6d 3b 0a 0a 20 20 Inode->pSem;..
141f0 20 20 69 66 28 20 73 65 6d 5f 74 72 79 77 61 69 if( sem_trywai
14200 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 t(pSem)==-1 ){.
14210 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 int tErrno
14220 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 69 = errno;. i
14230 66 28 20 45 41 47 41 49 4e 20 21 3d 20 74 45 72 f( EAGAIN != tEr
14240 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 72 rno ){. r
14250 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
14260 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
14270 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 rrno, SQLITE_IOE
14280 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 RR_CHECKRESERVED
14290 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 73 LOCK);. s
142a0 74 6f 72 65 4c 61 73 74 45 72 72 6e 6f 28 70 46 toreLastErrno(pF
142b0 69 6c 65 2c 20 74 45 72 72 6e 6f 29 3b 0a 20 20 ile, tErrno);.
142c0 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 } else {.
142d0 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 /* someone
142e0 65 6c 73 65 20 68 61 73 20 74 68 65 20 6c 6f 63 else has the loc
142f0 6b 20 77 68 65 6e 20 77 65 20 61 72 65 20 69 6e k when we are in
14300 20 4e 4f 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20 NO_LOCK */.
14310 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 28 reserved = (
14320 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b pFile->eFileLock
14330 20 3c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b < SHARED_LOCK);
14340 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
14350 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 77 65 20 se{. /* we
14360 63 6f 75 6c 64 20 68 61 76 65 20 69 74 20 69 66 could have it if
14370 20 77 65 20 77 61 6e 74 20 69 74 20 2a 2f 0a 20 we want it */.
14380 20 20 20 20 20 73 65 6d 5f 70 6f 73 74 28 70 53 sem_post(pS
14390 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 em);. }. }.
143a0 20 4f 53 54 52 41 43 45 28 28 22 54 45 53 54 20 OSTRACE(("TEST
143b0 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 WR-LOCK %d %d %d
143c0 20 28 73 65 6d 29 5c 6e 22 2c 20 70 46 69 6c 65 (sem)\n", pFile
143d0 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 ->h, rc, reserve
143e0 64 29 29 3b 0a 0a 20 20 2a 70 52 65 73 4f 75 74 d));.. *pResOut
143f0 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 = reserved;. r
14400 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
14410 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 ** Lock the file
14420 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 with the lock s
14430 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 pecified by para
14440 6d 65 74 65 72 20 65 46 69 6c 65 4c 6f 63 6b 20 meter eFileLock
14450 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 - one.** of the
14460 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a following:.**.**
14470 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f (1) SHARED_
14480 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 LOCK.** (2)
14490 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a RESERVED_LOCK.**
144a0 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 (3) PENDING
144b0 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 _LOCK.** (4)
144c0 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a EXCLUSIVE_LOCK.
144d0 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 **.** Sometimes
144e0 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 when requesting
144f0 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 one lock state,
14500 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 additional lock
14510 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e states.** are in
14520 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 serted in betwee
14530 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 n. The locking
14540 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e might fail on on
14550 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a e of the later.*
14560 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 * transitions le
14570 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 aving the lock s
14580 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 tate different f
14590 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 rom what it star
145a0 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c ted but.** still
145b0 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f short of its go
145c0 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 al. The followi
145d0 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 ng chart shows t
145e0 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 he allowed.** tr
145f0 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 ansitions and th
14600 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 e inserted inter
14610 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a mediate states:.
14620 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 **.** UNLOCKE
14630 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 D -> SHARED.**
14640 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 SHARED -> RESE
14650 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 RVED.** SHARE
14660 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d D -> (PENDING) -
14670 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 > EXCLUSIVE.**
14680 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 RESERVED -> (P
14690 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 ENDING) -> EXCLU
146a0 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 SIVE.** PENDI
146b0 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a NG -> EXCLUSIVE.
146c0 2a 2a 0a 2a 2a 20 53 65 6d 61 70 68 6f 72 65 20 **.** Semaphore
146d0 6c 6f 63 6b 73 20 6f 6e 6c 79 20 72 65 61 6c 6c locks only reall
146e0 79 20 73 75 70 70 6f 72 74 20 45 58 43 4c 55 53 y support EXCLUS
146f0 49 56 45 20 6c 6f 63 6b 73 2e 20 20 57 65 20 74 IVE locks. We t
14700 72 61 63 6b 20 69 6e 74 65 72 6d 65 64 69 61 74 rack intermediat
14710 65 0a 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 73 e.** lock states
14720 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f in the sqlite3_
14730 66 69 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20 file structure,
14740 62 75 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 53 48 but all locks SH
14750 41 52 45 44 20 6f 72 0a 2a 2a 20 61 62 6f 76 65 ARED or.** above
14760 20 61 72 65 20 72 65 61 6c 6c 79 20 45 58 43 4c are really EXCL
14770 55 53 49 56 45 20 6c 6f 63 6b 73 20 61 6e 64 20 USIVE locks and
14780 65 78 63 6c 75 64 65 20 61 6c 6c 20 6f 74 68 65 exclude all othe
14790 72 20 70 72 6f 63 65 73 73 65 73 20 66 72 6f 6d r processes from
147a0 0a 2a 2a 20 61 63 63 65 73 73 20 74 68 65 20 66 .** access the f
147b0 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ile..**.** This
147c0 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c routine will onl
147d0 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 y increase a loc
147e0 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 k. Use the sqli
147f0 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a te3OsUnlock().**
14800 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 routine to lowe
14810 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 r a locking leve
14820 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 l..*/.static int
14830 20 73 65 6d 58 4c 6f 63 6b 28 73 71 6c 69 74 65 semXLock(sqlite
14840 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
14850 65 46 69 6c 65 4c 6f 63 6b 29 20 7b 0a 20 20 75 eFileLock) {. u
14860 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
14870 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
14880 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 sem_t *pSem =
14890 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 70 pFile->pInode->p
148a0 53 65 6d 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 Sem;. int rc =
148b0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a SQLITE_OK;.. /*
148c0 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20 68 if we already h
148d0 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69 ave a lock, it i
148e0 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a 20 s exclusive. .
148f0 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 20 ** Just adjust
14900 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f level and punt o
14910 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a 2f n outta here. */
14920 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 65 46 . if (pFile->eF
14930 69 6c 65 4c 6f 63 6b 20 3e 20 4e 4f 5f 4c 4f 43 ileLock > NO_LOC
14940 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K) {. pFile->
14950 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 65 46 69 6c eFileLock = eFil
14960 65 4c 6f 63 6b 3b 0a 20 20 20 20 72 63 20 3d 20 eLock;. rc =
14970 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67 SQLITE_OK;. g
14980 6f 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b oto sem_end_lock
14990 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6c 6f ;. }. . /* lo
149a0 63 6b 20 73 65 6d 61 70 68 6f 72 65 20 6e 6f 77 ck semaphore now
149b0 20 62 75 74 20 62 61 69 6c 20 6f 75 74 20 77 68 but bail out wh
149c0 65 6e 20 61 6c 72 65 61 64 79 20 6c 6f 63 6b 65 en already locke
149d0 64 2e 20 2a 2f 0a 20 20 69 66 28 20 73 65 6d 5f d. */. if( sem_
149e0 74 72 79 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d trywait(pSem)==-
149f0 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 1 ){. rc = SQ
14a00 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 LITE_BUSY;. g
14a10 6f 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b oto sem_end_lock
14a20 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 67 6f 74 20 ;. }.. /* got
14a30 69 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 it, set the type
14a40 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a and return ok *
14a50 2f 0a 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 /. pFile->eFile
14a60 4c 6f 63 6b 20 3d 20 65 46 69 6c 65 4c 6f 63 6b Lock = eFileLock
14a70 3b 0a 0a 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b ;.. sem_end_lock
14a80 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d :. return rc;.}
14a90 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 ../*.** Lower th
14aa0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
14ab0 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 on file descript
14ac0 6f 72 20 70 46 69 6c 65 20 74 6f 20 65 46 69 6c or pFile to eFil
14ad0 65 4c 6f 63 6b 2e 20 20 65 46 69 6c 65 4c 6f 63 eLock. eFileLoc
14ae0 6b 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 k.** must be eit
14af0 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 her NO_LOCK or S
14b00 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a HARED_LOCK..**.*
14b10 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 * If the locking
14b20 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 level of the fi
14b30 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 le descriptor is
14b40 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 already at or b
14b50 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 elow.** the requ
14b60 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 ested locking le
14b70 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e vel, this routin
14b80 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f e is a no-op..*/
14b90 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 58 .static int semX
14ba0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 Unlock(sqlite3_f
14bb0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 65 46 69 ile *id, int eFi
14bc0 6c 65 4c 6f 63 6b 29 20 7b 0a 20 20 75 6e 69 78 leLock) {. unix
14bd0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
14be0 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 73 nixFile*)id;. s
14bf0 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 em_t *pSem = pFi
14c00 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 70 53 65 6d le->pInode->pSem
14c10 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 ;.. assert( pFi
14c20 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 le );. assert(
14c30 70 53 65 6d 20 29 3b 0a 20 20 4f 53 54 52 41 43 pSem );. OSTRAC
14c40 45 28 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 E(("UNLOCK %d %
14c50 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 20 d was %d pid=%d
14c60 28 73 65 6d 29 5c 6e 22 2c 20 70 46 69 6c 65 2d (sem)\n", pFile-
14c70 3e 68 2c 20 65 46 69 6c 65 4c 6f 63 6b 2c 0a 20 >h, eFileLock,.
14c80 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d pFile-
14c90 3e 65 46 69 6c 65 4c 6f 63 6b 2c 20 6f 73 47 65 >eFileLock, osGe
14ca0 74 70 69 64 28 30 29 29 29 3b 0a 20 20 61 73 73 tpid(0)));. ass
14cb0 65 72 74 28 20 65 46 69 6c 65 4c 6f 63 6b 3c 3d ert( eFileLock<=
14cc0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 SHARED_LOCK );.
14cd0 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 . /* no-op if
14ce0 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 possible */. if
14cf0 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f ( pFile->eFileLo
14d00 63 6b 3d 3d 65 46 69 6c 65 4c 6f 63 6b 20 29 7b ck==eFileLock ){
14d10 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
14d20 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 TE_OK;. }. .
14d30 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a 75 /* shared can ju
14d40 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75 73 st be set becaus
14d50 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 65 e we always have
14d60 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a 2f an exclusive */
14d70 0a 20 20 69 66 20 28 65 46 69 6c 65 4c 6f 63 6b . if (eFileLock
14d80 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b ==SHARED_LOCK) {
14d90 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c . pFile->eFil
14da0 65 4c 6f 63 6b 20 3d 20 65 46 69 6c 65 4c 6f 63 eLock = eFileLoc
14db0 6b 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 k;. return SQ
14dc0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a LITE_OK;. }. .
14dd0 20 20 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 20 /* no, really
14de0 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 66 20 unlock. */. if
14df0 28 20 73 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 ( sem_post(pSem)
14e00 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 20 69 6e 74 ==-1 ) {. int
14e10 20 72 63 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72 rc, tErrno = er
14e20 72 6e 6f 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 rno;. rc = sq
14e30 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
14e40 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
14e50 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c SQLITE_IOERR_UNL
14e60 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 OCK);. if( IS
14e70 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 _LOCK_ERROR(rc)
14e80 29 7b 0a 20 20 20 20 20 20 73 74 6f 72 65 4c 61 ){. storeLa
14e90 73 74 45 72 72 6e 6f 28 70 46 69 6c 65 2c 20 74 stErrno(pFile, t
14ea0 45 72 72 6e 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 Errno);. }.
14eb0 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 20 20 return rc; .
14ec0 7d 0a 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 }. pFile->eFile
14ed0 4c 6f 63 6b 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a Lock = NO_LOCK;.
14ee0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
14ef0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a 2a 20 43 6c OK;.}../*. ** Cl
14f00 6f 73 65 20 61 20 66 69 6c 65 2e 0a 20 2a 2f 0a ose a file.. */.
14f10 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 58 43 static int semXC
14f20 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c lose(sqlite3_fil
14f30 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69 e *id) {. if( i
14f40 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c d ){. unixFil
14f50 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
14f60 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 73 65 File*)id;. se
14f70 6d 58 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f mXUnlock(id, NO_
14f80 4c 4f 43 4b 29 3b 0a 20 20 20 20 61 73 73 65 72 LOCK);. asser
14f90 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 t( pFile );.
14fa0 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
14fb0 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 49 6e 6f ;. releaseIno
14fc0 64 65 49 6e 66 6f 28 70 46 69 6c 65 29 3b 0a 20 deInfo(pFile);.
14fd0 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 unixLeaveMute
14fe0 78 28 29 3b 0a 20 20 20 20 63 6c 6f 73 65 55 6e x();. closeUn
14ff0 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 7d 0a ixFile(id);. }.
15000 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15010 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a OK;.}..#endif /*
15020 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 2f OS_VXWORKS */./
15030 2a 0a 2a 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 *.** Named semap
15040 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 hore locking is
15050 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f only available o
15060 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a n VxWorks..**.**
15070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
15080 64 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 73 d of the named s
15090 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 20 69 6d emaphore lock im
150a0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a plementation ***
150b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
150c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
150d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
150e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
150f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a ************/...
15110 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
15120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
15160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
15180 6e 20 41 46 50 20 4c 6f 63 6b 69 6e 67 20 2a 2a n AFP Locking **
15190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
151a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
151b0 2a 2a 0a 2a 2a 20 41 46 50 20 69 73 20 74 68 65 **.** AFP is the
151c0 20 41 70 70 6c 65 20 46 69 6c 69 6e 67 20 50 72 Apple Filing Pr
151d0 6f 74 6f 63 6f 6c 2e 20 20 41 46 50 20 69 73 20 otocol. AFP is
151e0 61 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65 73 79 a network filesy
151f0 73 74 65 6d 20 66 6f 75 6e 64 0a 2a 2a 20 6f 6e stem found.** on
15200 20 41 70 70 6c 65 20 4d 61 63 69 6e 74 6f 73 68 Apple Macintosh
15210 20 63 6f 6d 70 75 74 65 72 73 20 2d 20 62 6f 74 computers - bot
15220 68 20 4f 53 39 20 61 6e 64 20 4f 53 58 2e 0a 2a h OS9 and OSX..*
15230 2a 0a 2a 2a 20 54 68 69 72 64 2d 70 61 72 74 79 *.** Third-party
15240 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
15250 20 6f 66 20 41 46 50 20 61 72 65 20 61 76 61 69 of AFP are avai
15260 6c 61 62 6c 65 2e 20 20 42 75 74 20 74 68 69 73 lable. But this
15270 20 63 6f 64 65 20 68 65 72 65 0a 2a 2a 20 6f 6e code here.** on
15280 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 4f 53 58 2e ly works on OSX.
15290 0a 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 .*/..#if defined
152a0 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 (__APPLE__) && S
152b0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
152c0 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a 0a 2a 2a KING_STYLE./*.**
152d0 20 54 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 The afpLockingC
152e0 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65 ontext structure
152f0 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 61 66 contains all af
15300 70 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 63 20 p lock specific
15310 73 74 61 74 65 0a 2a 2f 0a 74 79 70 65 64 65 66 state.*/.typedef
15320 20 73 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 struct afpLocki
15330 6e 67 43 6f 6e 74 65 78 74 20 61 66 70 4c 6f 63 ngContext afpLoc
15340 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72 kingContext;.str
15350 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f uct afpLockingCo
15360 6e 74 65 78 74 20 7b 0a 20 20 69 6e 74 20 72 65 ntext {. int re
15370 73 65 72 76 65 64 3b 0a 20 20 63 6f 6e 73 74 20 served;. const
15380 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20 char *dbPath;
15390 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
153a0 65 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 69 e of the open fi
153b0 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 72 75 63 74 le */.};..struct
153c0 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 ByteRangeLockPB
153d0 32 0a 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 2.{. unsigned l
153e0 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 3b ong long offset;
153f0 20 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 65 /* offse
15400 74 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20 t to first byte
15410 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 to lock */. uns
15420 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
15430 6c 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 2f length; /
15440 2a 20 6e 62 72 20 6f 66 20 62 79 74 65 73 20 74 * nbr of bytes t
15450 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 o lock */. unsi
15460 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 gned long long r
15470 65 74 52 61 6e 67 65 53 74 61 72 74 3b 20 2f 2a etRangeStart; /*
15480 20 6e 62 72 20 6f 66 20 31 73 74 20 62 79 74 65 nbr of 1st byte
15490 20 6c 6f 63 6b 65 64 20 69 66 20 73 75 63 63 65 locked if succe
154a0 73 73 66 75 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 ssful */. unsig
154b0 6e 65 64 20 63 68 61 72 20 75 6e 4c 6f 63 6b 46 ned char unLockF
154c0 6c 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 lag; /*
154d0 31 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 20 3d 20 1 = unlock, 0 =
154e0 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e lock */. unsign
154f0 65 64 20 63 68 61 72 20 73 74 61 72 74 45 6e 64 ed char startEnd
15500 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20 31 Flag; /* 1
15510 3d 72 65 6c 20 74 6f 20 65 6e 64 20 6f 66 20 66 =rel to end of f
15520 6f 72 6b 2c 20 30 3d 72 65 6c 20 74 6f 20 73 74 ork, 0=rel to st
15530 61 72 74 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b art */. int fd;
15540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15550 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69 /* fi
15560 6c 65 20 64 65 73 63 20 74 6f 20 61 73 73 6f 63 le desc to assoc
15570 20 74 68 69 73 20 6c 6f 63 6b 20 77 69 74 68 20 this lock with
15580 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 61 */.};..#define a
15590 66 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 fpfsByteRangeLoc
155a0 6b 32 46 53 43 54 4c 20 20 20 20 20 20 20 20 5f k2FSCTL _
155b0 49 4f 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 74 IOWR('z', 23, st
155c0 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f ruct ByteRangeLo
155d0 63 6b 50 42 32 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 ckPB2)../*.** Th
155e0 69 73 20 69 73 20 61 20 75 74 69 6c 69 74 79 20 is is a utility
155f0 66 6f 72 20 73 65 74 74 69 6e 67 20 6f 72 20 63 for setting or c
15600 6c 65 61 72 69 6e 67 20 61 20 62 69 74 2d 72 61 learing a bit-ra
15610 6e 67 65 20 6c 6f 63 6b 20 6f 6e 20 61 6e 0a 2a nge lock on an.*
15620 2a 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d * AFP filesystem
15630 2e 0a 2a 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 ..** .** Return
15640 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 SQLITE_OK on suc
15650 63 65 73 73 2c 20 53 51 4c 49 54 45 5f 42 55 53 cess, SQLITE_BUS
15660 59 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f Y on failure..*/
15670 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 53 .static int afpS
15680 65 74 4c 6f 63 6b 28 0a 20 20 63 6f 6e 73 74 20 etLock(. const
15690 63 68 61 72 20 2a 70 61 74 68 2c 20 20 20 20 20 char *path,
156a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
156b0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 of the file to
156c0 62 65 20 6c 6f 63 6b 65 64 20 6f 72 20 75 6e 6c be locked or unl
156d0 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 69 78 46 ocked */. unixF
156e0 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 ile *pFile,
156f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 /* Ope
15700 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f n file descripto
15710 72 20 6f 6e 20 70 61 74 68 20 2a 2f 0a 20 20 75 r on path */. u
15720 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
15730 67 20 6f 66 66 73 65 74 2c 20 20 20 20 20 2f 2a g offset, /*
15740 20 46 69 72 73 74 20 62 79 74 65 20 74 6f 20 62 First byte to b
15750 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e e locked */. un
15760 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
15770 20 6c 65 6e 67 74 68 2c 20 20 20 20 20 2f 2a 20 length, /*
15780 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
15790 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 to lock */. int
157a0 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20 20 20 20 setLockFlag
157b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
157c0 72 75 65 20 74 6f 20 73 65 74 20 6c 6f 63 6b 2e rue to set lock.
157d0 20 20 46 61 6c 73 65 20 74 6f 20 63 6c 65 61 72 False to clear
157e0 20 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20 20 73 74 lock */.){. st
157f0 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f ruct ByteRangeLo
15800 63 6b 50 42 32 20 70 62 3b 0a 20 20 69 6e 74 20 ckPB2 pb;. int
15810 65 72 72 3b 0a 20 20 0a 20 20 70 62 2e 75 6e 4c err;. . pb.unL
15820 6f 63 6b 46 6c 61 67 20 3d 20 73 65 74 4c 6f 63 ockFlag = setLoc
15830 6b 46 6c 61 67 20 3f 20 30 20 3a 20 31 3b 0a 20 kFlag ? 0 : 1;.
15840 20 70 62 2e 73 74 61 72 74 45 6e 64 46 6c 61 67 pb.startEndFlag
15850 20 3d 20 30 3b 0a 20 20 70 62 2e 6f 66 66 73 65 = 0;. pb.offse
15860 74 20 3d 20 6f 66 66 73 65 74 3b 0a 20 20 70 62 t = offset;. pb
15870 2e 6c 65 6e 67 74 68 20 3d 20 6c 65 6e 67 74 68 .length = length
15880 3b 20 0a 20 20 70 62 2e 66 64 20 3d 20 70 46 69 ; . pb.fd = pFi
15890 6c 65 2d 3e 68 3b 0a 20 20 0a 20 20 4f 53 54 52 le->h;. . OSTR
158a0 41 43 45 28 28 22 41 46 50 53 45 54 4c 4f 43 4b ACE(("AFPSETLOCK
158b0 20 5b 25 73 5d 20 66 6f 72 20 25 64 25 73 20 69 [%s] for %d%s i
158c0 6e 20 72 61 6e 67 65 20 25 6c 6c 78 3a 25 6c 6c n range %llx:%ll
158d0 78 5c 6e 22 2c 20 0a 20 20 20 20 28 73 65 74 4c x\n", . (setL
158e0 6f 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a 22 4f 46 ockFlag?"ON":"OF
158f0 46 22 29 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 28 F"), pFile->h, (
15900 70 62 2e 66 64 3d 3d 2d 31 3f 22 5b 74 65 73 74 pb.fd==-1?"[test
15910 76 61 6c 2d 31 5d 22 3a 22 22 29 2c 0a 20 20 20 val-1]":""),.
15920 20 6f 66 66 73 65 74 2c 20 6c 65 6e 67 74 68 29 offset, length)
15930 29 3b 0a 20 20 65 72 72 20 3d 20 66 73 63 74 6c );. err = fsctl
15940 28 70 61 74 68 2c 20 61 66 70 66 73 42 79 74 65 (path, afpfsByte
15950 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 2c RangeLock2FSCTL,
15960 20 26 70 62 2c 20 30 29 3b 0a 20 20 69 66 20 28 &pb, 0);. if (
15970 20 65 72 72 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 err==-1 ) {.
15980 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 69 6e 74 int rc;. int
15990 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b tErrno = errno;
159a0 0a 20 20 20 20 4f 53 54 52 41 43 45 28 28 22 41 . OSTRACE(("A
159b0 46 50 53 45 54 4c 4f 43 4b 20 66 61 69 6c 65 64 FPSETLOCK failed
159c0 20 74 6f 20 66 73 63 74 6c 28 29 20 27 25 73 27 to fsctl() '%s'
159d0 20 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 %d %s\n",.
159e0 20 20 20 20 20 20 20 20 70 61 74 68 2c 20 74 45 path, tE
159f0 72 72 6e 6f 2c 20 73 74 72 65 72 72 6f 72 28 74 rrno, strerror(t
15a00 45 72 72 6e 6f 29 29 29 3b 0a 23 69 66 64 65 66 Errno)));.#ifdef
15a10 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 SQLITE_IGNORE_A
15a20 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 FP_LOCK_ERRORS.
15a30 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
15a40 55 53 59 3b 0a 23 65 6c 73 65 0a 20 20 20 20 72 USY;.#else. r
15a50 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
15a60 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
15a70 72 72 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 rrno,.
15a80 20 20 20 20 20 20 20 20 20 20 73 65 74 4c 6f 63 setLoc
15a90 6b 46 6c 61 67 20 3f 20 53 51 4c 49 54 45 5f 49 kFlag ? SQLITE_I
15aa0 4f 45 52 52 5f 4c 4f 43 4b 20 3a 20 53 51 4c 49 OERR_LOCK : SQLI
15ab0 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 TE_IOERR_UNLOCK)
15ac0 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 ;.#endif /* SQLI
15ad0 54 45 5f 49 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f TE_IGNORE_AFP_LO
15ae0 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 CK_ERRORS */.
15af0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
15b00 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 OR(rc) ){.
15b10 73 74 6f 72 65 4c 61 73 74 45 72 72 6e 6f 28 70 storeLastErrno(p
15b20 46 69 6c 65 2c 20 74 45 72 72 6e 6f 29 3b 0a 20 File, tErrno);.
15b30 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 }. return
15b40 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 rc;. } else {.
15b50 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
15b60 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a _OK;. }.}../*.*
15b70 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
15b80 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 hecks if there i
15b90 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 s a RESERVED loc
15ba0 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 k held on the sp
15bb0 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 ecified.** file
15bc0 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f by this or any o
15bd0 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 ther process. If
15be0 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 such a lock is
15bf0 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f held, set *pResO
15c00 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a ut.** to a non-z
15c10 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 ero value otherw
15c20 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 ise *pResOut is
15c30 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 set to zero. Th
15c40 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a e return value.*
15c50 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 * is set to SQLI
15c60 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 TE_OK unless an
15c70 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 I/O error occurs
15c80 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 during lock che
15c90 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 cking..*/.static
15ca0 20 69 6e 74 20 61 66 70 43 68 65 63 6b 52 65 73 int afpCheckRes
15cb0 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 ervedLock(sqlite
15cc0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
15cd0 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 *pResOut){. int
15ce0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
15cf0 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 . int reserved
15d00 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 = 0;. unixFile
15d10 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
15d20 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63 le*)id;. afpLoc
15d30 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e kingContext *con
15d40 74 65 78 74 3b 0a 20 20 0a 20 20 53 69 6d 75 6c text;. . Simul
15d50 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 ateIOError( retu
15d60 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
15d70 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 CHECKRESERVEDLOC
15d80 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 K; );. . asser
15d90 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 63 6f t( pFile );. co
15da0 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b ntext = (afpLock
15db0 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 ingContext *) pF
15dc0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
15dd0 65 78 74 3b 0a 20 20 69 66 28 20 63 6f 6e 74 65 ext;. if( conte
15de0 78 74 2d 3e 72 65 73 65 72 76 65 64 20 29 7b 0a xt->reserved ){.
15df0 20 20 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 31 *pResOut = 1
15e00 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
15e10 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 75 6e ITE_OK;. }. un
15e20 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 20 ixEnterMutex();
15e30 2f 2a 20 42 65 63 61 75 73 65 20 70 46 69 6c 65 /* Because pFile
15e40 2d 3e 70 49 6e 6f 64 65 20 69 73 20 73 68 61 72 ->pInode is shar
15e50 65 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 ed across thread
15e60 73 20 2a 2f 0a 20 20 0a 20 20 2f 2a 20 43 68 65 s */. . /* Che
15e70 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 ck if a thread i
15e80 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 n this process h
15e90 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b olds such a lock
15ea0 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d */. if( pFile-
15eb0 3e 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f >pInode->eFileLo
15ec0 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 ck>SHARED_LOCK )
15ed0 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d {. reserved =
15ee0 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 1;. }. . /*
15ef0 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 66 Otherwise see if
15f00 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 some other proc
15f10 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 ess holds it..
15f20 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 */. if( !reser
15f30 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 6c 6f ved ){. /* lo
15f40 63 6b 20 74 68 65 20 52 45 53 45 52 56 45 44 20 ck the RESERVED
15f50 62 79 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 byte */. int
15f60 6c 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b lrc = afpSetLock
15f70 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 (context->dbPath
15f80 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 , pFile, RESERVE
15f90 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 20 20 0a D_BYTE, 1,1); .
15fa0 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f if( SQLITE_O
15fb0 4b 3d 3d 6c 72 63 20 29 7b 0a 20 20 20 20 20 20 K==lrc ){.
15fc0 2f 2a 20 69 66 20 77 65 20 73 75 63 63 65 65 64 /* if we succeed
15fd0 65 64 20 69 6e 20 74 61 6b 69 6e 67 20 74 68 65 ed in taking the
15fe0 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2c 20 reserved lock,
15ff0 75 6e 6c 6f 63 6b 20 69 74 20 74 6f 20 72 65 73 unlock it to res
16000 74 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 tore. ** th
16010 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 e original state
16020 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 */. lrc =
16030 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 afpSetLock(conte
16040 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c xt->dbPath, pFil
16050 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 e, RESERVED_BYTE
16060 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 65 , 1, 0);. } e
16070 6c 73 65 20 7b 0a 20 20 20 20 20 20 2f 2a 20 69 lse {. /* i
16080 66 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20 67 f we failed to g
16090 65 74 20 74 68 65 20 6c 6f 63 6b 20 74 68 65 6e et the lock then
160a0 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 75 someone else mu
160b0 73 74 20 68 61 76 65 20 69 74 20 2a 2f 0a 20 20 st have it */.
160c0 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 reserved = 1
160d0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
160e0 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 IS_LOCK_ERROR(lr
160f0 63 29 20 29 7b 0a 20 20 20 20 20 20 72 63 3d 6c c) ){. rc=l
16100 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 rc;. }. }.
16110 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 . unixLeaveMute
16120 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 x();. OSTRACE((
16130 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 "TEST WR-LOCK %d
16140 20 25 64 20 25 64 20 28 61 66 70 29 5c 6e 22 2c %d %d (afp)\n",
16150 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 pFile->h, rc, r
16160 65 73 65 72 76 65 64 29 29 3b 0a 20 20 0a 20 20 eserved));. .
16170 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 *pResOut = reser
16180 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ved;. return rc
16190 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 ;.}../*.** Lock
161a0 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 the file with th
161b0 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 e lock specified
161c0 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 65 46 by parameter eF
161d0 69 6c 65 4c 6f 63 6b 20 2d 20 6f 6e 65 0a 2a 2a ileLock - one.**
161e0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
161f0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 g:.**.** (1)
16200 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 SHARED_LOCK.**
16210 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 (2) RESERVED
16220 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 _LOCK.** (3)
16230 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a PENDING_LOCK.**
16240 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 (4) EXCLUSI
16250 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f VE_LOCK.**.** So
16260 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 metimes when req
16270 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b uesting one lock
16280 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e state, addition
16290 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a al lock states.*
162a0 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 * are inserted i
162b0 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 n between. The
162c0 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 locking might fa
162d0 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 il on one of the
162e0 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 later.** transi
162f0 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 tions leaving th
16300 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 e lock state dif
16310 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 ferent from what
16320 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a it started but.
16330 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f ** still short o
16340 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 f its goal. The
16350 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 following chart
16360 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 shows the allow
16370 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e ed.** transition
16380 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 s and the insert
16390 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 ed intermediate
163a0 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 states:.**.**
163b0 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 UNLOCKED -> SHA
163c0 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 RED.** SHARED
163d0 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 -> RESERVED.**
163e0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 SHARED -> (PE
163f0 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
16400 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 IVE.** RESERV
16410 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 ED -> (PENDING)
16420 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 -> EXCLUSIVE.**
16430 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 PENDING -> EX
16440 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 CLUSIVE.**.** Th
16450 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
16460 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 only increase a
16470 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 lock. Use the s
16480 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 qlite3OsUnlock()
16490 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c .** routine to l
164a0 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c ower a locking l
164b0 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 evel..*/.static
164c0 69 6e 74 20 61 66 70 4c 6f 63 6b 28 73 71 6c 69 int afpLock(sqli
164d0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
164e0 74 20 65 46 69 6c 65 4c 6f 63 6b 29 7b 0a 20 20 t eFileLock){.
164f0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
16500 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a OK;. unixFile *
16510 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
16520 65 2a 29 69 64 3b 0a 20 20 75 6e 69 78 49 6e 6f e*)id;. unixIno
16530 64 65 49 6e 66 6f 20 2a 70 49 6e 6f 64 65 20 3d deInfo *pInode =
16540 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 3b 0a pFile->pInode;.
16550 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 afpLockingCont
16560 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28 ext *context = (
16570 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 afpLockingContex
16580 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b t *) pFile->lock
16590 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 ingContext;. .
165a0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
165b0 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 4c 4f ;. OSTRACE(("LO
165c0 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 20 CK %d %s was
165d0 25 73 28 25 73 2c 25 64 29 20 70 69 64 3d 25 64 %s(%s,%d) pid=%d
165e0 20 28 61 66 70 29 5c 6e 22 2c 20 70 46 69 6c 65 (afp)\n", pFile
165f0 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ->h,.
16600 61 7a 46 69 6c 65 4c 6f 63 6b 28 65 46 69 6c 65 azFileLock(eFile
16610 4c 6f 63 6b 29 2c 20 61 7a 46 69 6c 65 4c 6f 63 Lock), azFileLoc
16620 6b 28 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f k(pFile->eFileLo
16630 63 6b 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ck),.
16640 61 7a 46 69 6c 65 4c 6f 63 6b 28 70 49 6e 6f 64 azFileLock(pInod
16650 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 29 2c 20 70 e->eFileLock), p
16660 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 20 2c Inode->nShared ,
16670 20 6f 73 47 65 74 70 69 64 28 30 29 29 29 3b 0a osGetpid(0)));.
16680 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 . /* If there i
16690 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b s already a lock
166a0 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 of this type or
166b0 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 more restrictiv
166c0 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e e on the. ** un
166d0 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 ixFile, do nothi
166e0 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 ng. Don't use th
166f0 65 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 20 e afp_end_lock:
16700 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 exit path, as.
16710 2a 2a 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 ** unixEnterMute
16720 78 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 x() hasn't been
16730 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f called yet.. */
16740 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 . if( pFile->eF
16750 69 6c 65 4c 6f 63 6b 3e 3d 65 46 69 6c 65 4c 6f ileLock>=eFileLo
16760 63 6b 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 ck ){. OSTRAC
16770 45 28 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 E(("LOCK %d %
16780 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 s ok (already he
16790 6c 64 29 20 28 61 66 70 29 5c 6e 22 2c 20 70 46 ld) (afp)\n", pF
167a0 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 ile->h,.
167b0 20 20 20 61 7a 46 69 6c 65 4c 6f 63 6b 28 65 46 azFileLock(eF
167c0 69 6c 65 4c 6f 63 6b 29 29 29 3b 0a 20 20 20 20 ileLock)));.
167d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
167e0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 ;. }.. /* Make
167f0 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e sure the lockin
16800 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f g sequence is co
16810 72 72 65 63 74 0a 20 20 2a 2a 20 20 28 31 29 20 rrect. ** (1)
16820 57 65 20 6e 65 76 65 72 20 6d 6f 76 65 20 66 72 We never move fr
16830 6f 6d 20 75 6e 6c 6f 63 6b 65 64 20 74 6f 20 61 om unlocked to a
16840 6e 79 74 68 69 6e 67 20 68 69 67 68 65 72 20 74 nything higher t
16850 68 61 6e 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e han shared lock.
16860 0a 20 20 2a 2a 20 20 28 32 29 20 53 51 4c 69 74 . ** (2) SQLit
16870 65 20 6e 65 76 65 72 20 65 78 70 6c 69 63 69 74 e never explicit
16880 6c 79 20 72 65 71 75 65 73 74 73 20 61 20 70 65 ly requests a pe
16890 6e 64 69 67 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 ndig lock.. **
168a0 20 28 33 29 20 41 20 73 68 61 72 65 64 20 6c 6f (3) A shared lo
168b0 63 6b 20 69 73 20 61 6c 77 61 79 73 20 68 65 6c ck is always hel
168c0 64 20 77 68 65 6e 20 61 20 72 65 73 65 72 76 65 d when a reserve
168d0 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 lock is request
168e0 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ed.. */. asser
168f0 74 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c t( pFile->eFileL
16900 6f 63 6b 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 ock!=NO_LOCK ||
16910 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 eFileLock==SHARE
16920 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 D_LOCK );. asse
16930 72 74 28 20 65 46 69 6c 65 4c 6f 63 6b 21 3d 50 rt( eFileLock!=P
16940 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 ENDING_LOCK );.
16950 20 61 73 73 65 72 74 28 20 65 46 69 6c 65 4c 6f assert( eFileLo
16960 63 6b 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 ck!=RESERVED_LOC
16970 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 65 46 69 6c K || pFile->eFil
16980 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f eLock==SHARED_LO
16990 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 CK );. . /* Th
169a0 69 73 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 is mutex is need
169b0 65 64 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 ed because pFile
169c0 2d 3e 70 49 6e 6f 64 65 20 69 73 20 73 68 61 72 ->pInode is shar
169d0 65 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 ed across thread
169e0 73 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 s. */. unixEnt
169f0 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 49 6e erMutex();. pIn
16a00 6f 64 65 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e ode = pFile->pIn
16a10 6f 64 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f ode;.. /* If so
16a20 6d 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 me thread using
16a30 74 68 69 73 20 50 49 44 20 68 61 73 20 61 20 6c this PID has a l
16a40 6f 63 6b 20 76 69 61 20 61 20 64 69 66 66 65 72 ock via a differ
16a50 65 6e 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 ent unixFile*.
16a60 2a 2a 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70 ** handle that p
16a70 72 65 63 6c 75 64 65 73 20 74 68 65 20 72 65 71 recludes the req
16a80 75 65 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 uested lock, ret
16a90 75 72 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 urn BUSY.. */.
16aa0 20 69 66 28 20 28 70 46 69 6c 65 2d 3e 65 46 69 if( (pFile->eFi
16ab0 6c 65 4c 6f 63 6b 21 3d 70 49 6e 6f 64 65 2d 3e leLock!=pInode->
16ac0 65 46 69 6c 65 4c 6f 63 6b 20 26 26 20 0a 20 20 eFileLock && .
16ad0 20 20 20 20 20 28 70 49 6e 6f 64 65 2d 3e 65 46 (pInode->eF
16ae0 69 6c 65 4c 6f 63 6b 3e 3d 50 45 4e 44 49 4e 47 ileLock>=PENDING
16af0 5f 4c 4f 43 4b 20 7c 7c 20 65 46 69 6c 65 4c 6f _LOCK || eFileLo
16b00 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 29 29 ck>SHARED_LOCK))
16b10 0a 20 20 20 20 20 29 7b 0a 20 20 20 20 72 63 20 . ){. rc
16b20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 = SQLITE_BUSY;.
16b30 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f goto afp_end_
16b40 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f lock;. }. . /
16b50 2a 20 49 66 20 61 20 53 48 41 52 45 44 20 6c 6f * If a SHARED lo
16b60 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 64 2c ck is requested,
16b70 20 61 6e 64 20 73 6f 6d 65 20 74 68 72 65 61 64 and some thread
16b80 20 75 73 69 6e 67 20 74 68 69 73 20 50 49 44 20 using this PID
16b90 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 68 61 73 already. ** has
16ba0 20 61 20 53 48 41 52 45 44 20 6f 72 20 52 45 53 a SHARED or RES
16bb0 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74 68 65 6e ERVED lock, then
16bc0 20 69 6e 63 72 65 6d 65 6e 74 20 72 65 66 65 72 increment refer
16bd0 65 6e 63 65 20 63 6f 75 6e 74 73 20 61 6e 64 0a ence counts and.
16be0 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 ** return SQLI
16bf0 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 69 66 TE_OK.. */. if
16c00 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 ( eFileLock==SHA
16c10 52 45 44 5f 4c 4f 43 4b 20 26 26 20 0a 20 20 20 RED_LOCK && .
16c20 20 20 28 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 (pInode->eFile
16c30 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 Lock==SHARED_LOC
16c40 4b 20 7c 7c 20 70 49 6e 6f 64 65 2d 3e 65 46 69 K || pInode->eFi
16c50 6c 65 4c 6f 63 6b 3d 3d 52 45 53 45 52 56 45 44 leLock==RESERVED
16c60 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 61 73 _LOCK) ){. as
16c70 73 65 72 74 28 20 65 46 69 6c 65 4c 6f 63 6b 3d sert( eFileLock=
16c80 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
16c90 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c assert( pFil
16ca0 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 30 20 e->eFileLock==0
16cb0 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
16cc0 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 3e 30 Inode->nShared>0
16cd0 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65 );. pFile->e
16ce0 46 69 6c 65 4c 6f 63 6b 20 3d 20 53 48 41 52 45 FileLock = SHARE
16cf0 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 49 6e 6f D_LOCK;. pIno
16d00 64 65 2d 3e 6e 53 68 61 72 65 64 2b 2b 3b 0a 20 de->nShared++;.
16d10 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b pInode->nLock
16d20 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 61 66 70 ++;. goto afp
16d30 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 _end_lock;. }.
16d40 20 20 20 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 . /* A PENDI
16d50 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 NG lock is neede
16d60 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 72 69 d before acquiri
16d70 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b ng a SHARED lock
16d80 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a and before. **
16d90 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58 acquiring an EX
16da0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 CLUSIVE lock. F
16db0 6f 72 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f or the SHARED lo
16dc0 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20 ck, the PENDING
16dd0 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c will. ** be rel
16de0 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 eased.. */. if
16df0 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 ( eFileLock==SHA
16e00 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 RED_LOCK .
16e10 7c 7c 20 28 65 46 69 6c 65 4c 6f 63 6b 3d 3d 45 || (eFileLock==E
16e20 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 XCLUSIVE_LOCK &&
16e30 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 pFile->eFileLoc
16e40 6b 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a k<PENDING_LOCK).
16e50 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 61 69 ){. int fai
16e60 6c 65 64 3b 0a 20 20 20 20 66 61 69 6c 65 64 20 led;. failed
16e70 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e = afpSetLock(con
16e80 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 text->dbPath, pF
16e90 69 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 ile, PENDING_BYT
16ea0 45 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69 66 E, 1, 1);. if
16eb0 20 28 66 61 69 6c 65 64 29 20 7b 0a 20 20 20 20 (failed) {.
16ec0 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a 20 rc = failed;.
16ed0 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e goto afp_en
16ee0 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 d_lock;. }.
16ef0 7d 0a 20 20 0a 20 20 2f 2a 20 49 66 20 63 6f 6e }. . /* If con
16f00 74 72 6f 6c 20 67 65 74 73 20 74 6f 20 74 68 69 trol gets to thi
16f10 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 63 s point, then ac
16f20 74 75 61 6c 6c 79 20 67 6f 20 61 68 65 61 64 20 tually go ahead
16f30 61 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 and make. ** op
16f40 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 erating system c
16f50 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 73 70 65 alls for the spe
16f60 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a cified lock.. *
16f70 2f 0a 20 20 69 66 28 20 65 46 69 6c 65 4c 6f 63 /. if( eFileLoc
16f80 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 k==SHARED_LOCK )
16f90 7b 0a 20 20 20 20 69 6e 74 20 6c 72 63 31 2c 20 {. int lrc1,
16fa0 6c 72 63 32 2c 20 6c 72 63 31 45 72 72 6e 6f 20 lrc2, lrc1Errno
16fb0 3d 20 30 3b 0a 20 20 20 20 6c 6f 6e 67 20 6c 6b = 0;. long lk
16fc0 2c 20 6d 61 73 6b 3b 0a 20 20 20 20 0a 20 20 20 , mask;. .
16fd0 20 61 73 73 65 72 74 28 20 70 49 6e 6f 64 65 2d assert( pInode-
16fe0 3e 6e 53 68 61 72 65 64 3d 3d 30 20 29 3b 0a 20 >nShared==0 );.
16ff0 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 6f 64 assert( pInod
17000 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 30 20 e->eFileLock==0
17010 29 3b 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 );. .
17020 6d 61 73 6b 20 3d 20 28 73 69 7a 65 6f 66 28 6c mask = (sizeof(l
17030 6f 6e 67 29 3d 3d 38 29 20 3f 20 4c 41 52 47 45 ong)==8) ? LARGE
17040 53 54 5f 49 4e 54 36 34 20 3a 20 30 78 37 66 66 ST_INT64 : 0x7ff
17050 66 66 66 66 66 3b 0a 20 20 20 20 2f 2a 20 4e 6f fffff;. /* No
17060 77 20 67 65 74 20 74 68 65 20 72 65 61 64 2d 6c w get the read-l
17070 6f 63 6b 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ock SHARED_LOCK
17080 2a 2f 0a 20 20 20 20 2f 2a 20 6e 6f 74 65 20 74 */. /* note t
17090 68 61 74 20 74 68 65 20 71 75 61 6c 69 74 79 20 hat the quality
170a0 6f 66 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73 of the randomnes
170b0 73 20 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 s doesn't matter
170c0 20 74 68 61 74 20 6d 75 63 68 20 2a 2f 0a 20 20 that much */.
170d0 20 20 6c 6b 20 3d 20 72 61 6e 64 6f 6d 28 29 3b lk = random();
170e0 20 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e 73 68 . pInode->sh
170f0 61 72 65 64 42 79 74 65 20 3d 20 28 6c 6b 20 26 aredByte = (lk &
17100 20 6d 61 73 6b 29 25 28 53 48 41 52 45 44 5f 53 mask)%(SHARED_S
17110 49 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20 6c 72 IZE - 1);. lr
17120 63 31 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 c1 = afpSetLock(
17130 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c context->dbPath,
17140 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 pFile, .
17150 20 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b SHARED_FIRST+
17160 70 49 6e 6f 64 65 2d 3e 73 68 61 72 65 64 42 79 pInode->sharedBy
17170 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69 te, 1, 1);. i
17180 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
17190 28 6c 72 63 31 29 20 29 7b 0a 20 20 20 20 20 20 (lrc1) ){.
171a0 6c 72 63 31 45 72 72 6e 6f 20 3d 20 70 46 69 6c lrc1Errno = pFil
171b0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 e->lastErrno;.
171c0 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 }. /* Drop
171d0 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45 the temporary PE
171e0 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 NDING lock */.
171f0 20 20 6c 72 63 32 20 3d 20 61 66 70 53 65 74 4c lrc2 = afpSetL
17200 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 ock(context->dbP
17210 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 ath, pFile, PEND
17220 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b ING_BYTE, 1, 0);
17230 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 49 53 . . if( IS
17240 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 31 _LOCK_ERROR(lrc1
17250 29 20 29 20 7b 0a 20 20 20 20 20 20 73 74 6f 72 ) ) {. stor
17260 65 4c 61 73 74 45 72 72 6e 6f 28 70 46 69 6c 65 eLastErrno(pFile
17270 2c 20 6c 72 63 31 45 72 72 6e 6f 29 3b 0a 20 20 , lrc1Errno);.
17280 20 20 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 rc = lrc1;.
17290 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e goto afp_en
172a0 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c d_lock;. } el
172b0 73 65 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 se if( IS_LOCK_E
172c0 52 52 4f 52 28 6c 72 63 32 29 20 29 7b 0a 20 20 RROR(lrc2) ){.
172d0 20 20 20 20 72 63 20 3d 20 6c 72 63 32 3b 0a 20 rc = lrc2;.
172e0 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e goto afp_en
172f0 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c d_lock;. } el
17300 73 65 20 69 66 28 20 6c 72 63 31 20 21 3d 20 53 se if( lrc1 != S
17310 51 4c 49 54 45 5f 4f 4b 20 29 20 7b 0a 20 20 20 QLITE_OK ) {.
17320 20 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 rc = lrc1;.
17330 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 } else {.
17340 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 pFile->eFileLoc
17350 6b 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b k = SHARED_LOCK;
17360 0a 20 20 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e . pInode->n
17370 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 70 49 Lock++;. pI
17380 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 20 3d 20 node->nShared =
17390 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 1;. }. }else
173a0 20 69 66 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d if( eFileLock==
173b0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 EXCLUSIVE_LOCK &
173c0 26 20 70 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 & pInode->nShare
173d0 64 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 d>1 ){. /* We
173e0 20 61 72 65 20 74 72 79 69 6e 67 20 66 6f 72 20 are trying for
173f0 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
17400 6b 20 62 75 74 20 61 6e 6f 74 68 65 72 20 74 68 k but another th
17410 72 65 61 64 20 69 6e 20 74 68 69 73 0a 20 20 20 read in this.
17420 20 20 2a 2a 20 73 61 6d 65 20 70 72 6f 63 65 73 ** same proces
17430 73 20 69 73 20 73 74 69 6c 6c 20 68 6f 6c 64 69 s is still holdi
17440 6e 67 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b ng a shared lock
17450 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 . */. rc = SQ
17460 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c LITE_BUSY;. }el
17470 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 se{. /* The r
17480 65 71 75 65 73 74 20 77 61 73 20 66 6f 72 20 61 equest was for a
17490 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 RESERVED or EXC
174a0 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 49 74 LUSIVE lock. It
174b0 20 69 73 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d is. ** assum
174c0 65 64 20 74 68 61 74 20 74 68 65 72 65 20 69 73 ed that there is
174d0 20 61 20 53 48 41 52 45 44 20 6f 72 20 67 72 65 a SHARED or gre
174e0 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 ater lock on the
174f0 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61 6c 72 file. ** alr
17500 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 eady.. */.
17510 20 69 6e 74 20 66 61 69 6c 65 64 20 3d 20 30 3b int failed = 0;
17520 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 21 3d . assert( 0!=
17530 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b pFile->eFileLock
17540 20 29 3b 0a 20 20 20 20 69 66 20 28 65 46 69 6c );. if (eFil
17550 65 4c 6f 63 6b 20 3e 3d 20 52 45 53 45 52 56 45 eLock >= RESERVE
17560 44 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d D_LOCK && pFile-
17570 3e 65 46 69 6c 65 4c 6f 63 6b 20 3c 20 52 45 53 >eFileLock < RES
17580 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 ERVED_LOCK) {.
17590 20 20 20 20 20 20 2f 2a 20 41 63 71 75 69 72 65 /* Acquire
175a0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
175b0 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69 6c */. fail
175c0 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 ed = afpSetLock(
175d0 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c context->dbPath,
175e0 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44 pFile, RESERVED
175f0 5f 42 59 54 45 2c 20 31 2c 31 29 3b 0a 20 20 20 _BYTE, 1,1);.
17600 20 20 20 69 66 28 20 21 66 61 69 6c 65 64 20 29 if( !failed )
17610 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 65 78 {. contex
17620 74 2d 3e 72 65 73 65 72 76 65 64 20 3d 20 31 3b t->reserved = 1;
17630 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
17640 20 20 20 69 66 20 28 21 66 61 69 6c 65 64 20 26 if (!failed &
17650 26 20 65 46 69 6c 65 4c 6f 63 6b 20 3d 3d 20 45 & eFileLock == E
17660 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 20 7b XCLUSIVE_LOCK) {
17670 0a 20 20 20 20 20 20 2f 2a 20 41 63 71 75 69 72 . /* Acquir
17680 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c e an EXCLUSIVE l
17690 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 0a ock */. .
176a0 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 /* Remove
176b0 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 the shared lock
176c0 62 65 66 6f 72 65 20 74 72 79 69 6e 67 20 74 68 before trying th
176d0 65 20 72 61 6e 67 65 2e 20 20 77 65 27 6c 6c 20 e range. we'll
176e0 6e 65 65 64 20 74 6f 20 0a 20 20 20 20 20 20 2a need to . *
176f0 2a 20 72 65 65 73 74 61 62 6c 69 73 68 20 74 68 * reestablish th
17700 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 66 e shared lock if
17710 20 77 65 20 63 61 6e 27 74 20 67 65 74 20 74 68 we can't get th
17720 65 20 20 61 66 70 55 6e 6c 6f 63 6b 0a 20 20 20 e afpUnlock.
17730 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
17740 21 28 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 !(failed = afpSe
17750 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 tLock(context->d
17760 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 bPath, pFile, SH
17770 41 52 45 44 5f 46 49 52 53 54 20 2b 0a 20 20 20 ARED_FIRST +.
17780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17790 20 20 20 20 20 20 70 49 6e 6f 64 65 2d 3e 73 68 pInode->sh
177a0 61 72 65 64 42 79 74 65 2c 20 31 2c 20 30 29 29 aredByte, 1, 0))
177b0 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 ){. int
177c0 66 61 69 6c 65 64 32 20 3d 20 53 51 4c 49 54 45 failed2 = SQLITE
177d0 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 _OK;. /*
177e0 6e 6f 77 20 61 74 74 65 6d 6d 70 74 20 74 6f 20 now attemmpt to
177f0 67 65 74 20 74 68 65 20 65 78 63 6c 75 73 69 76 get the exclusiv
17800 65 20 6c 6f 63 6b 20 72 61 6e 67 65 20 2a 2f 0a e lock range */.
17810 20 20 20 20 20 20 20 20 66 61 69 6c 65 64 20 3d failed =
17820 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 afpSetLock(cont
17830 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 ext->dbPath, pFi
17840 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 le, SHARED_FIRST
17850 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
17860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17870 20 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 31 SHARED_SIZE, 1
17880 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 );. if( f
17890 61 69 6c 65 64 20 26 26 20 28 66 61 69 6c 65 64 ailed && (failed
178a0 32 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 2 = afpSetLock(c
178b0 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 ontext->dbPath,
178c0 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 20 pFile, .
178d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 S
178e0 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20 70 49 HARED_FIRST + pI
178f0 6e 6f 64 65 2d 3e 73 68 61 72 65 64 42 79 74 65 node->sharedByte
17900 2c 20 31 2c 20 31 29 29 20 29 7b 0a 20 20 20 20 , 1, 1)) ){.
17910 20 20 20 20 20 20 2f 2a 20 43 61 6e 27 74 20 72 /* Can't r
17920 65 65 73 74 61 62 6c 69 73 68 20 74 68 65 20 73 eestablish the s
17930 68 61 72 65 64 20 6c 6f 63 6b 2e 20 20 53 71 6c hared lock. Sql
17940 69 74 65 20 63 61 6e 27 74 20 64 65 61 6c 2c 20 ite can't deal,
17950 74 68 69 73 20 69 73 0a 20 20 20 20 20 20 20 20 this is.
17960 20 20 2a 2a 20 61 20 63 72 69 74 69 63 61 6c 20 ** a critical
17970 49 2f 4f 20 65 72 72 6f 72 0a 20 20 20 20 20 20 I/O error.
17980 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 */.
17990 20 72 63 20 3d 20 28 28 66 61 69 6c 65 64 20 26 rc = ((failed &
179a0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d SQLITE_IOERR) =
179b0 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 = SQLITE_IOERR)
179c0 3f 20 66 61 69 6c 65 64 32 20 3a 20 0a 20 20 20 ? failed2 : .
179d0 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 SQLI
179e0 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 3b 0a 20 TE_IOERR_LOCK;.
179f0 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 66 goto af
17a00 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 p_end_lock;.
17a10 20 20 20 20 7d 20 0a 20 20 20 20 20 20 7d 65 6c } . }el
17a20 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d se{. rc =
17a30 20 66 61 69 6c 65 64 3b 20 0a 20 20 20 20 20 20 failed; .
17a40 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
17a50 66 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 failed ){.
17a60 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20 20 rc = failed;.
17a70 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 }. }. . if(
17a80 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
17a90 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c . pFile->eFil
17aa0 65 4c 6f 63 6b 20 3d 20 65 46 69 6c 65 4c 6f 63 eLock = eFileLoc
17ab0 6b 3b 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e 65 k;. pInode->e
17ac0 46 69 6c 65 4c 6f 63 6b 20 3d 20 65 46 69 6c 65 FileLock = eFile
17ad0 4c 6f 63 6b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 Lock;. }else if
17ae0 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43 ( eFileLock==EXC
17af0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 LUSIVE_LOCK ){.
17b00 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c pFile->eFileL
17b10 6f 63 6b 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f ock = PENDING_LO
17b20 43 4b 3b 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e CK;. pInode->
17b30 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 50 45 4e 44 eFileLock = PEND
17b40 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20 ING_LOCK;. }.
17b50 0a 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 .afp_end_lock:.
17b60 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 unixLeaveMutex(
17b70 29 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 4c );. OSTRACE(("L
17b80 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25 73 20 OCK %d %s %s
17b90 28 61 66 70 29 5c 6e 22 2c 20 70 46 69 6c 65 2d (afp)\n", pFile-
17ba0 3e 68 2c 20 61 7a 46 69 6c 65 4c 6f 63 6b 28 65 >h, azFileLock(e
17bb0 46 69 6c 65 4c 6f 63 6b 29 2c 20 0a 20 20 20 20 FileLock), .
17bc0 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f rc==SQLITE_
17bd0 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 OK ? "ok" : "fai
17be0 6c 65 64 22 29 29 3b 0a 20 20 72 65 74 75 72 6e led"));. return
17bf0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f rc;.}../*.** Lo
17c00 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 wer the locking
17c10 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 level on file de
17c20 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 scriptor pFile t
17c30 6f 20 65 46 69 6c 65 4c 6f 63 6b 2e 20 20 65 46 o eFileLock. eF
17c40 69 6c 65 4c 6f 63 6b 0a 2a 2a 20 6d 75 73 74 20 ileLock.** must
17c50 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 be either NO_LOC
17c60 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b K or SHARED_LOCK
17c70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c ..**.** If the l
17c80 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 ocking level of
17c90 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
17ca0 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 tor is already a
17cb0 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 t or below.** th
17cc0 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b e requested lock
17cd0 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 ing level, this
17ce0 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d routine is a no-
17cf0 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e op..*/.static in
17d00 74 20 61 66 70 55 6e 6c 6f 63 6b 28 73 71 6c 69 t afpUnlock(sqli
17d10 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
17d20 74 20 65 46 69 6c 65 4c 6f 63 6b 29 20 7b 0a 20 t eFileLock) {.
17d30 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
17d40 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 _OK;. unixFile
17d50 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
17d60 6c 65 2a 29 69 64 3b 0a 20 20 75 6e 69 78 49 6e le*)id;. unixIn
17d70 6f 64 65 49 6e 66 6f 20 2a 70 49 6e 6f 64 65 3b odeInfo *pInode;
17d80 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e . afpLockingCon
17d90 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 text *context =
17da0 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 (afpLockingConte
17db0 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 xt *) pFile->loc
17dc0 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 69 kingContext;. i
17dd0 6e 74 20 73 6b 69 70 53 68 61 72 65 64 20 3d 20 nt skipShared =
17de0 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 0;.#ifdef SQLITE
17df0 5f 54 45 53 54 0a 20 20 69 6e 74 20 68 20 3d 20 _TEST. int h =
17e00 70 46 69 6c 65 2d 3e 68 3b 0a 23 65 6e 64 69 66 pFile->h;.#endif
17e10 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c .. assert( pFil
17e20 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 e );. OSTRACE((
17e30 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 "UNLOCK %d %d w
17e40 61 73 20 25 64 28 25 64 2c 25 64 29 20 70 69 64 as %d(%d,%d) pid
17e50 3d 25 64 20 28 61 66 70 29 5c 6e 22 2c 20 70 46 =%d (afp)\n", pF
17e60 69 6c 65 2d 3e 68 2c 20 65 46 69 6c 65 4c 6f 63 ile->h, eFileLoc
17e70 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 46 k,. pF
17e80 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 2c 20 ile->eFileLock,
17e90 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 65 pFile->pInode->e
17ea0 46 69 6c 65 4c 6f 63 6b 2c 20 70 46 69 6c 65 2d FileLock, pFile-
17eb0 3e 70 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 >pInode->nShared
17ec0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6f 73 47 ,. osG
17ed0 65 74 70 69 64 28 30 29 29 29 3b 0a 0a 20 20 61 etpid(0)));.. a
17ee0 73 73 65 72 74 28 20 65 46 69 6c 65 4c 6f 63 6b ssert( eFileLock
17ef0 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b <=SHARED_LOCK );
17f00 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 . if( pFile->eF
17f10 69 6c 65 4c 6f 63 6b 3c 3d 65 46 69 6c 65 4c 6f ileLock<=eFileLo
17f20 63 6b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ck ){. return
17f30 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
17f40 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 unixEnterMutex
17f50 28 29 3b 0a 20 20 70 49 6e 6f 64 65 20 3d 20 70 ();. pInode = p
17f60 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 3b 0a 20 20 File->pInode;.
17f70 61 73 73 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e assert( pInode->
17f80 6e 53 68 61 72 65 64 21 3d 30 20 29 3b 0a 20 20 nShared!=0 );.
17f90 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 if( pFile->eFile
17fa0 4c 6f 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43 4b Lock>SHARED_LOCK
17fb0 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
17fc0 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 pInode->eFileLoc
17fd0 6b 3d 3d 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c k==pFile->eFileL
17fe0 6f 63 6b 20 29 3b 0a 20 20 20 20 53 69 6d 75 6c ock );. Simul
17ff0 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e ateIOErrorBenign
18000 28 31 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 (1);. Simulat
18010 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 2d 31 29 eIOError( h=(-1)
18020 20 29 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49 ). SimulateI
18030 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b OErrorBenign(0);
18040 0a 20 20 20 20 0a 23 69 66 64 65 66 20 53 51 4c . .#ifdef SQL
18050 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 2f 2a ITE_DEBUG. /*
18060 20 57 68 65 6e 20 72 65 64 75 63 69 6e 67 20 61 When reducing a
18070 20 6c 6f 63 6b 20 73 75 63 68 20 74 68 61 74 20 lock such that
18080 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 other processes
18090 63 61 6e 20 73 74 61 72 74 0a 20 20 20 20 2a 2a can start. **
180a0 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 reading the dat
180b0 61 62 61 73 65 20 66 69 6c 65 20 61 67 61 69 6e abase file again
180c0 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 , make sure that
180d0 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e the. ** tran
180e0 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 saction counter
180f0 77 61 73 20 75 70 64 61 74 65 64 20 69 66 20 61 was updated if a
18100 6e 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 ny part of the d
18110 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 66 atabase. ** f
18120 69 6c 65 20 63 68 61 6e 67 65 64 2e 20 20 49 66 ile changed. If
18130 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
18140 20 63 6f 75 6e 74 65 72 20 69 73 20 6e 6f 74 20 counter is not
18150 75 70 64 61 74 65 64 2c 0a 20 20 20 20 2a 2a 20 updated,. **
18160 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e other connection
18170 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 66 69 s to the same fi
18180 6c 65 20 6d 69 67 68 74 20 6e 6f 74 20 72 65 61 le might not rea
18190 6c 69 7a 65 20 74 68 61 74 0a 20 20 20 20 2a 2a lize that. **
181a0 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 68 the file has ch
181b0 61 6e 67 65 64 20 61 6e 64 20 68 65 6e 63 65 20 anged and hence
181c0 6d 69 67 68 74 20 6e 6f 74 20 6b 6e 6f 77 20 74 might not know t
181d0 6f 20 66 6c 75 73 68 20 74 68 65 69 72 0a 20 20 o flush their.
181e0 20 20 2a 2a 20 63 61 63 68 65 2e 20 20 54 68 65 ** cache. The
181f0 20 75 73 65 20 6f 66 20 61 20 73 74 61 6c 65 20 use of a stale
18200 63 61 63 68 65 20 63 61 6e 20 6c 65 61 64 20 74 cache can lead t
18210 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 o database corru
18220 70 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 ption.. */.
18230 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
18240 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 3d 3d >inNormalWrite==
18250 30 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 0. ||
18260 70 46 69 6c 65 2d 3e 64 62 55 70 64 61 74 65 3d pFile->dbUpdate=
18270 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c =0. ||
18280 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 pFile->transCnt
18290 72 43 68 6e 67 3d 3d 31 20 29 3b 0a 20 20 20 20 rChng==1 );.
182a0 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 pFile->inNormalW
182b0 72 69 74 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 rite = 0;.#endif
182c0 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 70 46 . . if( pF
182d0 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d ile->eFileLock==
182e0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 EXCLUSIVE_LOCK )
182f0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66 70 {. rc = afp
18300 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d SetLock(context-
18310 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 >dbPath, pFile,
18320 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 53 48 SHARED_FIRST, SH
18330 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 ARED_SIZE, 0);.
18340 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
18350 49 54 45 5f 4f 4b 20 26 26 20 28 65 46 69 6c 65 ITE_OK && (eFile
18360 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 Lock==SHARED_LOC
18370 4b 20 7c 7c 20 70 49 6e 6f 64 65 2d 3e 6e 53 68 K || pInode->nSh
18380 61 72 65 64 3e 31 29 20 29 7b 0a 20 20 20 20 20 ared>1) ){.
18390 20 20 20 2f 2a 20 6f 6e 6c 79 20 72 65 2d 65 73 /* only re-es
183a0 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 61 72 tablish the shar
183b0 65 64 20 6c 6f 63 6b 20 69 66 20 6e 65 63 65 73 ed lock if neces
183c0 73 61 72 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 sary */.
183d0 69 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 int sharedLockBy
183e0 74 65 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 te = SHARED_FIRS
183f0 54 2b 70 49 6e 6f 64 65 2d 3e 73 68 61 72 65 64 T+pInode->shared
18400 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20 72 63 Byte;. rc
18410 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f = afpSetLock(co
18420 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 ntext->dbPath, p
18430 46 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f 63 6b File, sharedLock
18440 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20 Byte, 1, 1);.
18450 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 } else {.
18460 20 20 20 20 73 6b 69 70 53 68 61 72 65 64 20 3d skipShared =
18470 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 1;. }.
18480 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
18490 4c 49 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 LITE_OK && pFile
184a0 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3e 3d 50 45 4e ->eFileLock>=PEN
184b0 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 DING_LOCK ){.
184c0 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f rc = afpSetLo
184d0 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 ck(context->dbPa
184e0 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 49 th, pFile, PENDI
184f0 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a NG_BYTE, 1, 0);.
18500 20 20 20 20 7d 20 0a 20 20 20 20 69 66 28 20 72 } . if( r
18510 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
18520 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b pFile->eFileLock
18530 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 >=RESERVED_LOCK
18540 26 26 20 63 6f 6e 74 65 78 74 2d 3e 72 65 73 65 && context->rese
18550 72 76 65 64 20 29 7b 0a 20 20 20 20 20 20 72 63 rved ){. rc
18560 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f = afpSetLock(co
18570 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 ntext->dbPath, p
18580 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 File, RESERVED_B
18590 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 YTE, 1, 0);.
185a0 20 20 69 66 28 20 21 72 63 20 29 7b 20 0a 20 20 if( !rc ){ .
185b0 20 20 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 72 context->r
185c0 65 73 65 72 76 65 64 20 3d 20 30 3b 20 0a 20 20 eserved = 0; .
185d0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
185e0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
185f0 4b 20 26 26 20 28 65 46 69 6c 65 4c 6f 63 6b 3d K && (eFileLock=
18600 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 7c 7c 20 =SHARED_LOCK ||
18610 70 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 3e pInode->nShared>
18620 31 29 29 7b 0a 20 20 20 20 20 20 70 49 6e 6f 64 1)){. pInod
18630 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 53 e->eFileLock = S
18640 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 HARED_LOCK;.
18650 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d }. }. if( rc==
18660 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 46 69 SQLITE_OK && eFi
18670 6c 65 4c 6f 63 6b 3d 3d 4e 4f 5f 4c 4f 43 4b 20 leLock==NO_LOCK
18680 29 7b 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 ){.. /* Decre
18690 6d 65 6e 74 20 74 68 65 20 73 68 61 72 65 64 20 ment the shared
186a0 6c 6f 63 6b 20 63 6f 75 6e 74 65 72 2e 20 20 52 lock counter. R
186b0 65 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b 20 elease the lock
186c0 75 73 69 6e 67 20 61 6e 0a 20 20 20 20 2a 2a 20 using an. **
186d0 4f 53 20 63 61 6c 6c 20 6f 6e 6c 79 20 77 68 65 OS call only whe
186e0 6e 20 61 6c 6c 20 74 68 72 65 61 64 73 20 69 6e n all threads in
186f0 20 74 68 69 73 20 73 61 6d 65 20 70 72 6f 63 65 this same proce
18700 73 73 20 68 61 76 65 20 72 65 6c 65 61 73 65 64 ss have released
18710 0a 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63 6b . ** the lock
18720 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 75 6e 73 .. */. uns
18730 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
18740 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d sharedLockByte =
18750 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b 70 49 SHARED_FIRST+pI
18760 6e 6f 64 65 2d 3e 73 68 61 72 65 64 42 79 74 65 node->sharedByte
18770 3b 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 53 ;. pInode->nS
18780 68 61 72 65 64 2d 2d 3b 0a 20 20 20 20 69 66 28 hared--;. if(
18790 20 70 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 pInode->nShared
187a0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 53 69 6d ==0 ){. Sim
187b0 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 ulateIOErrorBeni
187c0 67 6e 28 31 29 3b 0a 20 20 20 20 20 20 53 69 6d gn(1);. Sim
187d0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 68 3d ulateIOError( h=
187e0 28 2d 31 29 20 29 0a 20 20 20 20 20 20 53 69 6d (-1) ). Sim
187f0 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 ulateIOErrorBeni
18800 67 6e 28 30 29 3b 0a 20 20 20 20 20 20 69 66 28 gn(0);. if(
18810 20 21 73 6b 69 70 53 68 61 72 65 64 20 29 7b 0a !skipShared ){.
18820 20 20 20 20 20 20 20 20 72 63 20 3d 20 61 66 70 rc = afp
18830 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d SetLock(context-
18840 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 >dbPath, pFile,
18850 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 sharedLockByte,
18860 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 1, 0);. }.
18870 20 20 20 20 20 69 66 28 20 21 72 63 20 29 7b 0a if( !rc ){.
18880 20 20 20 20 20 20 20 20 70 49 6e 6f 64 65 2d 3e pInode->
18890 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 4e 4f 5f 4c eFileLock = NO_L
188a0 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 70 46 69 OCK;. pFi
188b0 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 le->eFileLock =
188c0 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 7d NO_LOCK;. }
188d0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 . }. if( r
188e0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
188f0 20 20 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 4c pInode->nL
18900 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 61 73 73 ock--;. ass
18910 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f ert( pInode->nLo
18920 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 ck>=0 );. i
18930 66 28 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b f( pInode->nLock
18940 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 ==0 ){. c
18950 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28 70 losePendingFds(p
18960 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 File);. }.
18970 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 75 6e }. }. . un
18980 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a ixLeaveMutex();.
18990 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
189a0 5f 4f 4b 20 29 20 70 46 69 6c 65 2d 3e 65 46 69 _OK ) pFile->eFi
189b0 6c 65 4c 6f 63 6b 20 3d 20 65 46 69 6c 65 4c 6f leLock = eFileLo
189c0 63 6b 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ck;. return rc;
189d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 .}../*.** Close
189e0 61 20 66 69 6c 65 20 26 20 63 6c 65 61 6e 75 70 a file & cleanup
189f0 20 41 46 50 20 73 70 65 63 69 66 69 63 20 6c 6f AFP specific lo
18a00 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 0a 2a cking context .*
18a10 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 /.static int afp
18a20 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 Close(sqlite3_fi
18a30 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 6e 74 20 le *id) {. int
18a40 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
18a50 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
18a60 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
18a70 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 d;. assert( id!
18a80 3d 30 20 29 3b 0a 20 20 61 66 70 55 6e 6c 6f 63 =0 );. afpUnloc
18a90 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a k(id, NO_LOCK);.
18aa0 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 unixEnterMutex
18ab0 28 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d ();. if( pFile-
18ac0 3e 70 49 6e 6f 64 65 20 26 26 20 70 46 69 6c 65 >pInode && pFile
18ad0 2d 3e 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 20 ->pInode->nLock
18ae0 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 ){. /* If the
18af0 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 re are outstandi
18b00 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 ng locks, do not
18b10 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f 73 65 20 actually close
18b20 74 68 65 20 66 69 6c 65 20 6a 75 73 74 0a 20 20 the file just.
18b30 20 20 2a 2a 20 79 65 74 20 62 65 63 61 75 73 65 ** yet because
18b40 20 74 68 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 that would clea
18b50 72 20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 r those locks.
18b60 49 6e 73 74 65 61 64 2c 20 61 64 64 20 74 68 65 Instead, add the
18b70 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 64 65 73 file. ** des
18b80 63 72 69 70 74 6f 72 20 74 6f 20 70 49 6e 6f 64 criptor to pInod
18b90 65 2d 3e 61 50 65 6e 64 69 6e 67 2e 20 20 49 74 e->aPending. It
18ba0 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 will be automat
18bb0 69 63 61 6c 6c 79 20 63 6c 6f 73 65 64 20 77 68 ically closed wh
18bc0 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6c 61 en. ** the la
18bd0 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65 61 72 st lock is clear
18be0 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 ed.. */. s
18bf0 65 74 50 65 6e 64 69 6e 67 46 64 28 70 46 69 6c etPendingFd(pFil
18c00 65 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 e);. }. releas
18c10 65 49 6e 6f 64 65 49 6e 66 6f 28 70 46 69 6c 65 eInodeInfo(pFile
18c20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 );. sqlite3_fre
18c30 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 e(pFile->locking
18c40 43 6f 6e 74 65 78 74 29 3b 0a 20 20 72 63 20 3d Context);. rc =
18c50 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 closeUnixFile(i
18c60 64 29 3b 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d d);. unixLeaveM
18c70 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e utex();. return
18c80 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f rc;.}..#endif /
18c90 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c * defined(__APPL
18ca0 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 E__) && SQLITE_E
18cb0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
18cc0 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 YLE */./*.** The
18cd0 20 63 6f 64 65 20 61 62 6f 76 65 20 69 73 20 74 code above is t
18ce0 68 65 20 41 46 50 20 6c 6f 63 6b 20 69 6d 70 6c he AFP lock impl
18cf0 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 ementation. The
18d00 20 63 6f 64 65 20 69 73 20 73 70 65 63 69 66 69 code is specifi
18d10 63 0a 2a 2a 20 74 6f 20 4d 61 63 4f 53 58 20 61 c.** to MacOSX a
18d20 6e 64 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b nd does not work
18d30 20 6f 6e 20 6f 74 68 65 72 20 75 6e 69 78 20 70 on other unix p
18d40 6c 61 74 66 6f 72 6d 73 2e 20 20 4e 6f 20 61 6c latforms. No al
18d50 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 69 73 20 ternative.** is
18d60 61 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20 79 available. If y
18d70 6f 75 20 64 6f 6e 27 74 20 63 6f 6d 70 69 6c 65 ou don't compile
18d80 20 66 6f 72 20 61 20 6d 61 63 2c 20 74 68 65 6e for a mac, then
18d90 20 74 68 65 20 22 75 6e 69 78 2d 61 66 70 22 0a the "unix-afp".
18da0 2a 2a 20 56 46 53 20 69 73 20 6e 6f 74 20 61 76 ** VFS is not av
18db0 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ailable..**.****
18dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18dd0 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 41 46 50 * End of the AFP
18de0 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 lock implementa
18df0 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tion ***********
18e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
18e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a **********/../**
18e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
18eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18ec0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 4e ******** Begin N
18ed0 46 53 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a FS Locking *****
18ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 ***********/..#i
18f00 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c f defined(__APPL
18f10 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 E__) && SQLITE_E
18f20 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
18f30 59 4c 45 0a 2f 2a 0a 20 2a 2a 20 4c 6f 77 65 72 YLE./*. ** Lower
18f40 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
18f50 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 el on file descr
18f60 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 65 iptor pFile to e
18f70 46 69 6c 65 4c 6f 63 6b 2e 20 20 65 46 69 6c 65 FileLock. eFile
18f80 4c 6f 63 6b 0a 20 2a 2a 20 6d 75 73 74 20 62 65 Lock. ** must be
18f90 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 either NO_LOCK
18fa0 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a or SHARED_LOCK..
18fb0 20 2a 2a 0a 20 2a 2a 20 49 66 20 74 68 65 20 6c **. ** If the l
18fc0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 ocking level of
18fd0 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
18fe0 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 tor is already a
18ff0 74 20 6f 72 20 62 65 6c 6f 77 0a 20 2a 2a 20 74 t or below. ** t
19000 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 he requested loc
19010 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 king level, this
19020 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
19030 2d 6f 70 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 -op.. */.static
19040 69 6e 74 20 6e 66 73 55 6e 6c 6f 63 6b 28 73 71 int nfsUnlock(sq
19050 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
19060 69 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29 7b 0a int eFileLock){.
19070 20 20 72 65 74 75 72 6e 20 70 6f 73 69 78 55 6e return posixUn
19080 6c 6f 63 6b 28 69 64 2c 20 65 46 69 6c 65 4c 6f lock(id, eFileLo
19090 63 6b 2c 20 31 29 3b 0a 7d 0a 0a 23 65 6e 64 69 ck, 1);.}..#endi
190a0 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 f /* defined(__A
190b0 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 PPLE__) && SQLIT
190c0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
190d0 5f 53 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 _STYLE */./*.**
190e0 54 68 65 20 63 6f 64 65 20 61 62 6f 76 65 20 69 The code above i
190f0 73 20 74 68 65 20 4e 46 53 20 6c 6f 63 6b 20 69 s the NFS lock i
19100 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 mplementation.
19110 54 68 65 20 63 6f 64 65 20 69 73 20 73 70 65 63 The code is spec
19120 69 66 69 63 0a 2a 2a 20 74 6f 20 4d 61 63 4f 53 ific.** to MacOS
19130 58 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 77 X and does not w
19140 6f 72 6b 20 6f 6e 20 6f 74 68 65 72 20 75 6e 69 ork on other uni
19150 78 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20 4e 6f x platforms. No
19160 20 61 6c 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 alternative.**
19170 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 0a is available. .
19180 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
19190 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
191a0 74 68 65 20 4e 46 53 20 6c 6f 63 6b 20 69 6d 70 the NFS lock imp
191b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a lementation ****
191c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
191d0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
191e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
191f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19220 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
19230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19270 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***.************
19280 2a 2a 2a 2a 20 4e 6f 6e 2d 6c 6f 63 6b 69 6e 67 **** Non-locking
19290 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65 sqlite3_file me
192a0 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a thods **********
192b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
192c0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 ***.**.** The ne
192d0 78 74 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 xt division cont
192e0 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 ains implementat
192f0 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 20 6d 65 74 ions for all met
19300 68 6f 64 73 20 6f 66 20 74 68 65 20 0a 2a 2a 20 hods of the .**
19310 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a sqlite3_file obj
19320 65 63 74 20 6f 74 68 65 72 20 74 68 61 6e 20 74 ect other than t
19330 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f he locking metho
19340 64 73 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 ds. The locking
19350 0a 2a 2a 20 6d 65 74 68 6f 64 73 20 77 65 72 65 .** methods were
19360 20 64 65 66 69 6e 65 64 20 69 6e 20 64 69 76 69 defined in divi
19370 73 69 6f 6e 73 20 61 62 6f 76 65 20 28 6f 6e 65 sions above (one
19380 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 locking method
19390 70 65 72 0a 2a 2a 20 64 69 76 69 73 69 6f 6e 29 per.** division)
193a0 2e 20 20 54 68 6f 73 65 20 6d 65 74 68 6f 64 73 . Those methods
193b0 20 74 68 61 74 20 61 72 65 20 63 6f 6d 6d 6f 6e that are common
193c0 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 to all locking
193d0 6d 6f 64 65 73 0a 2a 2a 20 61 72 65 20 67 61 74 modes.** are gat
193e0 68 65 72 20 74 6f 67 65 74 68 65 72 20 69 6e 74 her together int
193f0 6f 20 74 68 69 73 20 64 69 76 69 73 69 6f 6e 2e o this division.
19400 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 6b 20 .*/../*.** Seek
19410 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 70 61 to the offset pa
19420 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f ssed as the seco
19430 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 74 68 65 nd argument, the
19440 6e 20 72 65 61 64 20 63 6e 74 20 0a 2a 2a 20 62 n read cnt .** b
19450 79 74 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 20 ytes into pBuf.
19460 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
19470 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 r of bytes actua
19480 6c 6c 79 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 lly read..**.**
19490 4e 42 3a 20 20 49 66 20 79 6f 75 20 64 65 66 69 NB: If you defi
194a0 6e 65 20 55 53 45 5f 50 52 45 41 44 20 6f 72 20 ne USE_PREAD or
194b0 55 53 45 5f 50 52 45 41 44 36 34 2c 20 74 68 65 USE_PREAD64, the
194c0 6e 20 69 74 20 6d 69 67 68 74 20 61 6c 73 6f 0a n it might also.
194d0 2a 2a 20 62 65 20 6e 65 63 65 73 73 61 72 79 20 ** be necessary
194e0 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e to define _XOPEN
194f0 5f 53 4f 55 52 43 45 20 74 6f 20 62 65 20 35 30 _SOURCE to be 50
19500 30 2e 20 20 54 68 69 73 20 76 61 72 69 65 73 20 0. This varies
19510 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 73 79 73 74 from.** one syst
19520 65 6d 20 74 6f 20 61 6e 6f 74 68 65 72 2e 20 20 em to another.
19530 53 69 6e 63 65 20 53 51 4c 69 74 65 20 64 6f 65 Since SQLite doe
19540 73 20 6e 6f 74 20 64 65 66 69 6e 65 20 55 53 45 s not define USE
19550 5f 50 52 45 41 44 0a 2a 2a 20 69 6e 20 61 6e 79 _PREAD.** in any
19560 20 66 6f 72 6d 20 62 79 20 64 65 66 61 75 6c 74 form by default
19570 2c 20 77 65 20 77 69 6c 6c 20 6e 6f 74 20 61 74 , we will not at
19580 74 65 6d 70 74 20 74 6f 20 64 65 66 69 6e 65 20 tempt to define
19590 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 2e 0a 2a _XOPEN_SOURCE..*
195a0 2a 20 53 65 65 20 74 69 63 6b 65 74 73 20 23 32 * See tickets #2
195b0 37 34 31 20 61 6e 64 20 23 32 36 38 31 2e 0a 2a 741 and #2681..*
195c0 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 73 74 *.** To avoid st
195d0 6f 6d 70 69 6e 67 20 74 68 65 20 65 72 72 6e 6f omping the errno
195e0 20 76 61 6c 75 65 20 6f 6e 20 61 20 66 61 69 6c value on a fail
195f0 65 64 20 72 65 61 64 20 74 68 65 20 6c 61 73 74 ed read the last
19600 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 Errno value.** i
19610 73 20 73 65 74 20 62 65 66 6f 72 65 20 72 65 74 s set before ret
19620 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 urning..*/.stati
19630 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 52 65 61 c int seekAndRea
19640 64 28 75 6e 69 78 46 69 6c 65 20 2a 69 64 2c 20 d(unixFile *id,
19650 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 sqlite3_int64 of
19660 66 73 65 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 fset, void *pBuf
19670 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e , int cnt){. in
19680 74 20 67 6f 74 3b 0a 20 20 69 6e 74 20 70 72 69 t got;. int pri
19690 6f 72 20 3d 20 30 3b 0a 23 69 66 20 28 21 64 65 or = 0;.#if (!de
196a0 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 29 fined(USE_PREAD)
196b0 20 26 26 20 21 64 65 66 69 6e 65 64 28 55 53 45 && !defined(USE
196c0 5f 50 52 45 41 44 36 34 29 29 0a 20 20 69 36 34 _PREAD64)). i64
196d0 20 6e 65 77 4f 66 66 73 65 74 3b 0a 23 65 6e 64 newOffset;.#end
196e0 69 66 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 54 if. TIMER_START
196f0 3b 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 3d ;. assert( cnt=
19700 3d 28 63 6e 74 26 30 78 31 66 66 66 66 29 20 29 =(cnt&0x1ffff) )
19710 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 2d 3e ;. assert( id->
19720 68 3e 32 20 29 3b 0a 20 20 64 6f 7b 0a 23 69 66 h>2 );. do{.#if
19730 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 defined(USE_PRE
19740 41 44 29 0a 20 20 20 20 67 6f 74 20 3d 20 6f 73 AD). got = os
19750 50 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42 75 Pread(id->h, pBu
19760 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b f, cnt, offset);
19770 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 . SimulateIOE
19780 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 rror( got = -1 )
19790 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 ;.#elif defined(
197a0 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 20 USE_PREAD64).
197b0 20 67 6f 74 20 3d 20 6f 73 50 72 65 61 64 36 34 got = osPread64
197c0 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e (id->h, pBuf, cn
197d0 74 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 20 20 t, offset);.
197e0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
197f0 20 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 65 6c got = -1 );.#el
19800 73 65 0a 20 20 20 20 6e 65 77 4f 66 66 73 65 74 se. newOffset
19810 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 = lseek(id->h,
19820 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 offset, SEEK_SET
19830 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49 );. SimulateI
19840 4f 45 72 72 6f 72 28 20 6e 65 77 4f 66 66 73 65 OError( newOffse
19850 74 20 3d 20 2d 31 20 29 3b 0a 20 20 20 20 69 66 t = -1 );. if
19860 28 20 6e 65 77 4f 66 66 73 65 74 3c 30 20 29 7b ( newOffset<0 ){
19870 0a 20 20 20 20 20 20 73 74 6f 72 65 4c 61 73 74 . storeLast
19880 45 72 72 6e 6f 28 28 75 6e 69 78 46 69 6c 65 2a Errno((unixFile*
19890 29 69 64 2c 20 65 72 72 6e 6f 29 3b 0a 20 20 20 )id, errno);.
198a0 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 return -1;.
198b0 20 20 7d 0a 20 20 20 20 67 6f 74 20 3d 20 6f 73 }. got = os
198c0 52 65 61 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 Read(id->h, pBuf
198d0 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 20 , cnt);.#endif.
198e0 20 20 20 69 66 28 20 67 6f 74 3d 3d 63 6e 74 20 if( got==cnt
198f0 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 ) break;. if(
19900 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 20 20 20 got<0 ){.
19910 69 66 28 20 65 72 72 6e 6f 3d 3d 45 49 4e 54 52 if( errno==EINTR
19920 20 29 7b 20 67 6f 74 20 3d 20 31 3b 20 63 6f 6e ){ got = 1; con
19930 74 69 6e 75 65 3b 20 7d 0a 20 20 20 20 20 20 70 tinue; }. p
19940 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 rior = 0;.
19950 73 74 6f 72 65 4c 61 73 74 45 72 72 6e 6f 28 28 storeLastErrno((
19960 75 6e 69 78 46 69 6c 65 2a 29 69 64 2c 20 20 65 unixFile*)id, e
19970 72 72 6e 6f 29 3b 0a 20 20 20 20 20 20 62 72 65 rrno);. bre
19980 61 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 ak;. }else if
19990 28 20 67 6f 74 3e 30 20 29 7b 0a 20 20 20 20 20 ( got>0 ){.
199a0 20 63 6e 74 20 2d 3d 20 67 6f 74 3b 0a 20 20 20 cnt -= got;.
199b0 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 67 6f 74 offset += got
199c0 3b 0a 20 20 20 20 20 20 70 72 69 6f 72 20 2b 3d ;. prior +=
199d0 20 67 6f 74 3b 0a 20 20 20 20 20 20 70 42 75 66 got;. pBuf
199e0 20 3d 20 28 76 6f 69 64 2a 29 28 67 6f 74 20 2b = (void*)(got +
199f0 20 28 63 68 61 72 2a 29 70 42 75 66 29 3b 0a 20 (char*)pBuf);.
19a00 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 67 }. }while( g
19a10 6f 74 3e 30 20 29 3b 0a 20 20 54 49 4d 45 52 5f ot>0 );. TIMER_
19a20 45 4e 44 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 END;. OSTRACE((
19a30 22 52 45 41 44 20 20 20 20 25 2d 33 64 20 25 35 "READ %-3d %5
19a40 64 20 25 37 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c d %7lld %llu\n",
19a50 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 64 2d . id-
19a60 3e 68 2c 20 67 6f 74 2b 70 72 69 6f 72 2c 20 6f >h, got+prior, o
19a70 66 66 73 65 74 2d 70 72 69 6f 72 2c 20 54 49 4d ffset-prior, TIM
19a80 45 52 5f 45 4c 41 50 53 45 44 29 29 3b 0a 20 20 ER_ELAPSED));.
19a90 72 65 74 75 72 6e 20 67 6f 74 2b 70 72 69 6f 72 return got+prior
19aa0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
19ab0 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 data from a file
19ac0 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 into a buffer.
19ad0 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
19ae0 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 K if all.** byte
19af0 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63 s were read succ
19b00 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c essfully and SQL
19b10 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 ITE_IOERR if any
19b20 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 thing goes.** wr
19b30 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ong..*/.static i
19b40 6e 74 20 75 6e 69 78 52 65 61 64 28 0a 20 20 73 nt unixRead(. s
19b50 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
19b60 20 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 . void *pBuf,
19b70 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 . int amt,. sq
19b80 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 lite3_int64 offs
19b90 65 74 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 et.){. unixFile
19ba0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
19bb0 69 6c 65 20 2a 29 69 64 3b 0a 20 20 69 6e 74 20 ile *)id;. int
19bc0 67 6f 74 3b 0a 20 20 61 73 73 65 72 74 28 20 69 got;. assert( i
19bd0 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f d );. assert( o
19be0 66 66 73 65 74 3e 3d 30 20 29 3b 0a 20 20 61 73 ffset>=0 );. as
19bf0 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 0a sert( amt>0 );..
19c00 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
19c10 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 a database file
19c20 28 6e 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 2c 20 (not a journal,
19c30 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6f master-journal o
19c40 72 20 74 65 6d 70 0a 20 20 2a 2a 20 66 69 6c 65 r temp. ** file
19c50 29 2c 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 ), the bytes in
19c60 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 the locking rang
19c70 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 e should never b
19c80 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 e read or writte
19c90 6e 2e 20 2a 2f 0a 23 69 66 20 30 0a 20 20 61 73 n. */.#if 0. as
19ca0 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e sert( pFile->pUn
19cb0 75 73 65 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c used==0. |
19cc0 7c 20 6f 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e | offset>=PENDIN
19cd0 47 5f 42 59 54 45 2b 35 31 32 0a 20 20 20 20 20 G_BYTE+512.
19ce0 20 20 7c 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c || offset+amt<
19cf0 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20 =PENDING_BYTE .
19d00 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 );.#endif..#if
19d10 53 51 4c 49 54 45 5f 4d 41 58 5f 4d 4d 41 50 5f SQLITE_MAX_MMAP_
19d20 53 49 5a 45 3e 30 0a 20 20 2f 2a 20 44 65 61 6c SIZE>0. /* Deal
19d30 20 77 69 74 68 20 61 73 20 6d 75 63 68 20 6f 66 with as much of
19d40 20 74 68 69 73 20 72 65 61 64 20 72 65 71 75 65 this read reque
19d50 73 74 20 61 73 20 70 6f 73 73 69 62 6c 65 20 62 st as possible b
19d60 79 20 74 72 61 6e 73 66 65 72 69 6e 67 0a 20 20 y transfering.
19d70 2a 2a 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 ** data from the
19d80 20 6d 65 6d 6f 72 79 20 6d 61 70 70 69 6e 67 20 memory mapping
19d90 75 73 69 6e 67 20 6d 65 6d 63 70 79 28 29 2e 20 using memcpy().
19da0 20 2a 2f 0a 20 20 69 66 28 20 6f 66 66 73 65 74 */. if( offset
19db0 3c 70 46 69 6c 65 2d 3e 6d 6d 61 70 53 69 7a 65 <pFile->mmapSize
19dc0 20 29 7b 0a 20 20 20 20 69 66 28 20 6f 66 66 73 ){. if( offs
19dd0 65 74 2b 61 6d 74 20 3c 3d 20 70 46 69 6c 65 2d et+amt <= pFile-
19de0 3e 6d 6d 61 70 53 69 7a 65 20 29 7b 0a 20 20 20 >mmapSize ){.
19df0 20 20 20 6d 65 6d 63 70 79 28 70 42 75 66 2c 20 memcpy(pBuf,
19e00 26 28 28 75 38 20 2a 29 28 70 46 69 6c 65 2d 3e &((u8 *)(pFile->
19e10 70 4d 61 70 52 65 67 69 6f 6e 29 29 5b 6f 66 66 pMapRegion))[off
19e20 73 65 74 5d 2c 20 61 6d 74 29 3b 0a 20 20 20 20 set], amt);.
19e30 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
19e40 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 OK;. }else{.
19e50 20 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 20 3d int nCopy =
19e60 20 70 46 69 6c 65 2d 3e 6d 6d 61 70 53 69 7a 65 pFile->mmapSize
19e70 20 2d 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 20 - offset;.
19e80 20 6d 65 6d 63 70 79 28 70 42 75 66 2c 20 26 28 memcpy(pBuf, &(
19e90 28 75 38 20 2a 29 28 70 46 69 6c 65 2d 3e 70 4d (u8 *)(pFile->pM
19ea0 61 70 52 65 67 69 6f 6e 29 29 5b 6f 66 66 73 65 apRegion))[offse
19eb0 74 5d 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 t], nCopy);.
19ec0 20 20 70 42 75 66 20 3d 20 26 28 28 75 38 20 2a pBuf = &((u8 *
19ed0 29 70 42 75 66 29 5b 6e 43 6f 70 79 5d 3b 0a 20 )pBuf)[nCopy];.
19ee0 20 20 20 20 20 61 6d 74 20 2d 3d 20 6e 43 6f 70 amt -= nCop
19ef0 79 3b 0a 20 20 20 20 20 20 6f 66 66 73 65 74 20 y;. offset
19f00 2b 3d 20 6e 43 6f 70 79 3b 0a 20 20 20 20 7d 0a += nCopy;. }.
19f10 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 67 6f }.#endif.. go
19f20 74 20 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 28 t = seekAndRead(
19f30 70 46 69 6c 65 2c 20 6f 66 66 73 65 74 2c 20 70 pFile, offset, p
19f40 42 75 66 2c 20 61 6d 74 29 3b 0a 20 20 69 66 28 Buf, amt);. if(
19f50 20 67 6f 74 3d 3d 61 6d 74 20 29 7b 0a 20 20 20 got==amt ){.
19f60 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
19f70 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 67 K;. }else if( g
19f80 6f 74 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 6c ot<0 ){. /* l
19f90 61 73 74 45 72 72 6e 6f 20 73 65 74 20 62 79 20 astErrno set by
19fa0 73 65 65 6b 41 6e 64 52 65 61 64 20 2a 2f 0a 20 seekAndRead */.
19fb0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
19fc0 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d _IOERR_READ;. }
19fd0 65 6c 73 65 7b 0a 20 20 20 20 73 74 6f 72 65 4c else{. storeL
19fe0 61 73 74 45 72 72 6e 6f 28 70 46 69 6c 65 2c 20 astErrno(pFile,
19ff0 30 29 3b 20 20 20 2f 2a 20 6e 6f 74 20 61 20 73 0); /* not a s
1a000 79 73 74 65 6d 20 65 72 72 6f 72 20 2a 2f 0a 20 ystem error */.
1a010 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 61 72 /* Unread par
1a020 74 73 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 ts of the buffer
1a030 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d 66 69 must be zero-fi
1a040 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 lled */. mems
1a050 65 74 28 26 28 28 63 68 61 72 2a 29 70 42 75 66 et(&((char*)pBuf
1a060 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67 )[got], 0, amt-g
1a070 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ot);. return
1a080 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f SQLITE_IOERR_SHO
1a090 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a 0a RT_READ;. }.}..
1a0a0 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f /*.** Attempt to
1a0b0 20 73 65 65 6b 20 74 68 65 20 66 69 6c 65 2d 64 seek the file-d
1a0c0 65 73 63 72 69 70 74 6f 72 20 70 61 73 73 65 64 escriptor passed
1a0d0 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 as the first ar
1a0e0 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 61 62 73 gument to.** abs
1a0f0 6f 6c 75 74 65 20 6f 66 66 73 65 74 20 69 4f 66 olute offset iOf
1a100 66 2c 20 74 68 65 6e 20 61 74 74 65 6d 70 74 20 f, then attempt
1a110 74 6f 20 77 72 69 74 65 20 6e 42 75 66 20 62 79 to write nBuf by
1a120 74 65 73 20 6f 66 20 64 61 74 61 20 66 72 6f 6d tes of data from
1a130 0a 2a 2a 20 70 42 75 66 20 74 6f 20 69 74 2e 20 .** pBuf to it.
1a140 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
1a150 72 73 2c 20 72 65 74 75 72 6e 20 2d 31 20 61 6e rs, return -1 an
1a160 64 20 73 65 74 20 2a 70 69 45 72 72 6e 6f 2e 20 d set *piErrno.
1a170 4f 74 68 65 72 77 69 73 65 2c 20 0a 2a 2a 20 72 Otherwise, .** r
1a180 65 74 75 72 6e 20 74 68 65 20 61 63 74 75 61 6c eturn the actual
1a190 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
1a1a0 20 77 72 69 74 74 65 6e 20 28 77 68 69 63 68 20 written (which
1a1b0 6d 61 79 20 62 65 20 6c 65 73 73 20 74 68 61 6e may be less than
1a1c0 0a 2a 2a 20 6e 42 75 66 29 2e 0a 2a 2f 0a 73 74 .** nBuf)..*/.st
1a1d0 61 74 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 atic int seekAnd
1a1e0 57 72 69 74 65 46 64 28 0a 20 20 69 6e 74 20 66 WriteFd(. int f
1a1f0 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d,
1a200 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
1a210 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f le descriptor to
1a220 20 77 72 69 74 65 20 74 6f 20 2a 2f 0a 20 20 69 write to */. i
1a230 36 34 20 69 4f 66 66 2c 20 20 20 20 20 20 20 20 64 iOff,
1a240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a250 2a 20 46 69 6c 65 20 6f 66 66 73 65 74 20 74 6f * File offset to
1a260 20 62 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 begin writing a
1a270 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 t */. const voi
1a280 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 d *pBuf,
1a290 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 64 /* Copy d
1a2a0 61 74 61 20 66 72 6f 6d 20 74 68 69 73 20 62 75 ata from this bu
1a2b0 66 66 65 72 20 74 6f 20 74 68 65 20 66 69 6c 65 ffer to the file
1a2c0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 66 2c 20 */. int nBuf,
1a2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a2e0 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
1a2f0 20 62 75 66 66 65 72 20 70 42 75 66 20 69 6e 20 buffer pBuf in
1a300 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 2a bytes */. int *
1a310 70 69 45 72 72 6e 6f 20 20 20 20 20 20 20 20 20 piErrno
1a320 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 /* OU
1a330 54 3a 20 45 72 72 6f 72 20 6e 75 6d 62 65 72 20 T: Error number
1a340 69 66 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 if error occurs
1a350 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d */.){. int rc =
1a360 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1a370 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 /* Value
1a380 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 79 73 returned by sys
1a390 74 65 6d 20 63 61 6c 6c 20 2a 2f 0a 0a 20 20 61 tem call */.. a
1a3a0 73 73 65 72 74 28 20 6e 42 75 66 3d 3d 28 6e 42 ssert( nBuf==(nB
1a3b0 75 66 26 30 78 31 66 66 66 66 29 20 29 3b 0a 20 uf&0x1ffff) );.
1a3c0 20 61 73 73 65 72 74 28 20 66 64 3e 32 20 29 3b assert( fd>2 );
1a3d0 0a 20 20 61 73 73 65 72 74 28 20 70 69 45 72 72 . assert( piErr
1a3e0 6e 6f 21 3d 30 20 29 3b 0a 20 20 6e 42 75 66 20 no!=0 );. nBuf
1a3f0 26 3d 20 30 78 31 66 66 66 66 3b 0a 20 20 54 49 &= 0x1ffff;. TI
1a400 4d 45 52 5f 53 54 41 52 54 3b 0a 0a 23 69 66 20 MER_START;..#if
1a410 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 defined(USE_PREA
1a420 44 29 0a 20 20 64 6f 7b 20 72 63 20 3d 20 28 69 D). do{ rc = (i
1a430 6e 74 29 6f 73 50 77 72 69 74 65 28 66 64 2c 20 nt)osPwrite(fd,
1a440 70 42 75 66 2c 20 6e 42 75 66 2c 20 69 4f 66 66 pBuf, nBuf, iOff
1a450 29 3b 20 7d 77 68 69 6c 65 28 20 72 63 3c 30 20 ); }while( rc<0
1a460 26 26 20 65 72 72 6e 6f 3d 3d 45 49 4e 54 52 20 && errno==EINTR
1a470 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 );.#elif defined
1a480 28 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 (USE_PREAD64).
1a490 64 6f 7b 20 72 63 20 3d 20 28 69 6e 74 29 6f 73 do{ rc = (int)os
1a4a0 50 77 72 69 74 65 36 34 28 66 64 2c 20 70 42 75 Pwrite64(fd, pBu
1a4b0 66 2c 20 6e 42 75 66 2c 20 69 4f 66 66 29 3b 7d f, nBuf, iOff);}
1a4c0 77 68 69 6c 65 28 20 72 63 3c 30 20 26 26 20 65 while( rc<0 && e
1a4d0 72 72 6e 6f 3d 3d 45 49 4e 54 52 29 3b 0a 23 65 rrno==EINTR);.#e
1a4e0 6c 73 65 0a 20 20 64 6f 7b 0a 20 20 20 20 69 36 lse. do{. i6
1a4f0 34 20 69 53 65 65 6b 20 3d 20 6c 73 65 65 6b 28 4 iSeek = lseek(
1a500 66 64 2c 20 69 4f 66 66 2c 20 53 45 45 4b 5f 53 fd, iOff, SEEK_S
1a510 45 54 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 ET);. Simulat
1a520 65 49 4f 45 72 72 6f 72 28 20 69 53 65 65 6b 20 eIOError( iSeek
1a530 3d 20 2d 31 20 29 3b 0a 20 20 20 20 69 66 28 20 = -1 );. if(
1a540 69 53 65 65 6b 3c 30 20 29 7b 0a 20 20 20 20 20 iSeek<0 ){.
1a550 20 72 63 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 rc = -1;.
1a560 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1a570 20 72 63 20 3d 20 6f 73 57 72 69 74 65 28 66 64 rc = osWrite(fd
1a580 2c 20 70 42 75 66 2c 20 6e 42 75 66 29 3b 0a 20 , pBuf, nBuf);.
1a590 20 7d 77 68 69 6c 65 28 20 72 63 3c 30 20 26 26 }while( rc<0 &&
1a5a0 20 65 72 72 6e 6f 3d 3d 45 49 4e 54 52 20 29 3b errno==EINTR );
1a5b0 0a 23 65 6e 64 69 66 0a 0a 20 20 54 49 4d 45 52 .#endif.. TIMER
1a5c0 5f 45 4e 44 3b 0a 20 20 4f 53 54 52 41 43 45 28 _END;. OSTRACE(
1a5d0 28 22 57 52 49 54 45 20 20 20 25 2d 33 64 20 25 ("WRITE %-3d %
1a5e0 35 64 20 25 37 6c 6c 64 20 25 6c 6c 75 5c 6e 22 5d %7lld %llu\n"
1a5f0 2c 20 66 64 2c 20 72 63 2c 20 69 4f 66 66 2c 20 , fd, rc, iOff,
1a600 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 29 3b TIMER_ELAPSED));
1a610 0a 0a 20 20 69 66 28 20 72 63 3c 30 20 29 20 2a .. if( rc<0 ) *
1a620 70 69 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b piErrno = errno;
1a630 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1a640 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f 20 ../*.** Seek to
1a650 74 68 65 20 6f 66 66 73 65 74 20 69 6e 20 69 64 the offset in id
1a660 2d 3e 6f 66 66 73 65 74 20 74 68 65 6e 20 72 65 ->offset then re
1a670 61 64 20 63 6e 74 20 62 79 74 65 73 20 69 6e 74 ad cnt bytes int
1a680 6f 20 70 42 75 66 2e 0a 2a 2a 20 52 65 74 75 72 o pBuf..** Retur
1a690 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
1a6a0 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 72 bytes actually r
1a6b0 65 61 64 2e 20 20 55 70 64 61 74 65 20 74 68 65 ead. Update the
1a6c0 20 6f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 offset..**.** T
1a6d0 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70 69 6e 67 o avoid stomping
1a6e0 20 74 68 65 20 65 72 72 6e 6f 20 76 61 6c 75 65 the errno value
1a6f0 20 6f 6e 20 61 20 66 61 69 6c 65 64 20 77 72 69 on a failed wri
1a700 74 65 20 74 68 65 20 6c 61 73 74 45 72 72 6e 6f te the lastErrno
1a710 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 value.** is set
1a720 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
1a730 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
1a740 20 73 65 65 6b 41 6e 64 57 72 69 74 65 28 75 6e seekAndWrite(un
1a750 69 78 46 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 ixFile *id, i64
1a760 6f 66 66 73 65 74 2c 20 63 6f 6e 73 74 20 76 6f offset, const vo
1a770 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63 6e id *pBuf, int cn
1a780 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 65 65 t){. return see
1a790 6b 41 6e 64 57 72 69 74 65 46 64 28 69 64 2d 3e kAndWriteFd(id->
1a7a0 68 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c h, offset, pBuf,
1a7b0 20 63 6e 74 2c 20 26 69 64 2d 3e 6c 61 73 74 45 cnt, &id->lastE
1a7c0 72 72 6e 6f 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rrno);.}.../*.**
1a7d0 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d Write data from
1a7e0 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 a buffer into a
1a7f0 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 file. Return S
1a800 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 QLITE_OK on succ
1a810 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f ess.** or some o
1a820 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 ther error code
1a830 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 on failure..*/.s
1a840 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 57 72 tatic int unixWr
1a850 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 ite(. sqlite3_f
1a860 69 6c 65 20 2a 69 64 2c 20 0a 20 20 63 6f 6e 73 ile *id, . cons
1a870 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a 20 t void *pBuf, .
1a880 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c 69 int amt,. sqli
1a890 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 te3_int64 offset
1a8a0 20 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 .){. unixFile
1a8b0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
1a8c0 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 77 72 le*)id;. int wr
1a8d0 6f 74 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 ote = 0;. asser
1a8e0 74 28 20 69 64 20 29 3b 0a 20 20 61 73 73 65 72 t( id );. asser
1a8f0 74 28 20 61 6d 74 3e 30 20 29 3b 0a 0a 20 20 2f t( amt>0 );.. /
1a900 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 64 * If this is a d
1a910 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f atabase file (no
1a920 74 20 61 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61 73 t a journal, mas
1a930 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 ter-journal or t
1a940 65 6d 70 0a 20 20 2a 2a 20 66 69 6c 65 29 2c 20 emp. ** file),
1a950 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68 65 the bytes in the
1a960 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 73 locking range s
1a970 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 72 hould never be r
1a980 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 20 ead or written.
1a990 2a 2f 0a 23 69 66 20 30 0a 20 20 61 73 73 65 72 */.#if 0. asser
1a9a0 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 t( pFile->pUnuse
1a9b0 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c 20 6f d==0. || o
1a9c0 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e 47 5f 42 ffset>=PENDING_B
1a9d0 59 54 45 2b 35 31 32 0a 20 20 20 20 20 20 20 7c YTE+512. |
1a9e0 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 50 45 | offset+amt<=PE
1a9f0 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20 20 29 3b NDING_BYTE . );
1aa00 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 .#endif..#ifdef
1aa10 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 2f SQLITE_DEBUG. /
1aa20 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e * If we are doin
1aa30 67 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 g a normal write
1aa40 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 to a database f
1aa50 69 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 ile (as opposed
1aa60 74 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 20 to. ** doing a
1aa70 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c hot-journal roll
1aa80 62 61 63 6b 20 6f 72 20 61 20 77 72 69 74 65 20 back or a write
1aa90 74 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 68 to some file oth
1aaa0 65 72 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20 6e er than a. ** n
1aab0 6f 72 6d 61 6c 20 64 61 74 61 62 61 73 65 20 66 ormal database f
1aac0 69 6c 65 29 20 74 68 65 6e 20 72 65 63 6f 72 64 ile) then record
1aad0 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 the fact that t
1aae0 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a he database. **
1aaf0 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20 20 49 has changed. I
1ab00 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f f the transactio
1ab10 6e 20 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f 64 n counter is mod
1ab20 69 66 69 65 64 2c 20 72 65 63 6f 72 64 20 74 68 ified, record th
1ab30 61 74 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f 6f at. ** fact too
1ab40 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 .. */. if( pFi
1ab50 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 le->inNormalWrit
1ab60 65 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e e ){. pFile->
1ab70 64 62 55 70 64 61 74 65 20 3d 20 31 3b 20 20 2f dbUpdate = 1; /
1ab80 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 68 * The database h
1ab90 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 as been modified
1aba0 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f 66 66 73 */. if( offs
1abb0 65 74 3c 3d 32 34 20 26 26 20 6f 66 66 73 65 74 et<=24 && offset
1abc0 2b 61 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20 20 +amt>=27 ){.
1abd0 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 int rc;.
1abe0 63 68 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d 3b char oldCntr[4];
1abf0 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 . SimulateI
1ac00 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b OErrorBenign(1);
1ac10 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65 65 6b . rc = seek
1ac20 41 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20 32 AndRead(pFile, 2
1ac30 34 2c 20 6f 6c 64 43 6e 74 72 2c 20 34 29 3b 0a 4, oldCntr, 4);.
1ac40 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f SimulateIO
1ac50 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a ErrorBenign(0);.
1ac60 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 34 20 if( rc!=4
1ac70 7c 7c 20 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e 74 || memcmp(oldCnt
1ac80 72 2c 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 r, &((char*)pBuf
1ac90 29 5b 32 34 2d 6f 66 66 73 65 74 5d 2c 20 34 29 )[24-offset], 4)
1aca0 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 !=0 ){. p
1acb0 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 File->transCntrC
1acc0 68 6e 67 20 3d 20 31 3b 20 20 2f 2a 20 54 68 65 hng = 1; /* The
1acd0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 transaction cou
1ace0 6e 74 65 72 20 68 61 73 20 63 68 61 6e 67 65 64 nter has changed
1acf0 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 */. }.
1ad00 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 }. }.#endif..#i
1ad10 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
1ad20 5f 4d 4d 41 50 5f 52 45 41 44 57 52 49 54 45 29 _MMAP_READWRITE)
1ad30 20 26 26 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4d && SQLITE_MAX_M
1ad40 4d 41 50 5f 53 49 5a 45 3e 30 0a 20 20 2f 2a 20 MAP_SIZE>0. /*
1ad50 44 65 61 6c 20 77 69 74 68 20 61 73 20 6d 75 63 Deal with as muc
1ad60 68 20 6f 66 20 74 68 69 73 20 77 72 69 74 65 20 h of this write
1ad70 72 65 71 75 65 73 74 20 61 73 20 70 6f 73 73 69 request as possi
1ad80 62 6c 65 20 62 79 20 74 72 61 6e 73 66 65 72 69 ble by transferi
1ad90 6e 67 0a 20 20 2a 2a 20 64 61 74 61 20 66 72 6f ng. ** data fro
1ada0 6d 20 74 68 65 20 6d 65 6d 6f 72 79 20 6d 61 70 m the memory map
1adb0 70 69 6e 67 20 75 73 69 6e 67 20 6d 65 6d 63 70 ping using memcp
1adc0 79 28 29 2e 20 20 2a 2f 0a 20 20 69 66 28 20 6f y(). */. if( o
1add0 66 66 73 65 74 3c 70 46 69 6c 65 2d 3e 6d 6d 61 ffset<pFile->mma
1ade0 70 53 69 7a 65 20 29 7b 0a 20 20 20 20 69 66 28 pSize ){. if(
1adf0 20 6f 66 66 73 65 74 2b 61 6d 74 20 3c 3d 20 70 offset+amt <= p
1ae00 46 69 6c 65 2d 3e 6d 6d 61 70 53 69 7a 65 20 29 File->mmapSize )
1ae10 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 {. memcpy(&
1ae20 28 28 75 38 20 2a 29 28 70 46 69 6c 65 2d 3e 70 ((u8 *)(pFile->p
1ae30 4d 61 70 52 65 67 69 6f 6e 29 29 5b 6f 66 66 73 MapRegion))[offs
1ae40 65 74 5d 2c 20 70 42 75 66 2c 20 61 6d 74 29 3b et], pBuf, amt);
1ae50 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
1ae60 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c LITE_OK;. }el
1ae70 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43 se{. int nC
1ae80 6f 70 79 20 3d 20 70 46 69 6c 65 2d 3e 6d 6d 61 opy = pFile->mma
1ae90 70 53 69 7a 65 20 2d 20 6f 66 66 73 65 74 3b 0a pSize - offset;.
1aea0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 28 28 memcpy(&((
1aeb0 75 38 20 2a 29 28 70 46 69 6c 65 2d 3e 70 4d 61 u8 *)(pFile->pMa
1aec0 70 52 65 67 69 6f 6e 29 29 5b 6f 66 66 73 65 74 pRegion))[offset
1aed0 5d 2c 20 70 42 75 66 2c 20 6e 43 6f 70 79 29 3b ], pBuf, nCopy);
1aee0 0a 20 20 20 20 20 20 70 42 75 66 20 3d 20 26 28 . pBuf = &(
1aef0 28 75 38 20 2a 29 70 42 75 66 29 5b 6e 43 6f 70 (u8 *)pBuf)[nCop
1af00 79 5d 3b 0a 20 20 20 20 20 20 61 6d 74 20 2d 3d y];. amt -=
1af10 20 6e 43 6f 70 79 3b 0a 20 20 20 20 20 20 6f 66 nCopy;. of
1af20 66 73 65 74 20 2b 3d 20 6e 43 6f 70 79 3b 0a 20 fset += nCopy;.
1af30 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
1af40 20 0a 20 20 77 68 69 6c 65 28 20 28 77 72 6f 74 . while( (wrot
1af50 65 20 3d 20 73 65 65 6b 41 6e 64 57 72 69 74 65 e = seekAndWrite
1af60 28 70 46 69 6c 65 2c 20 6f 66 66 73 65 74 2c 20 (pFile, offset,
1af70 70 42 75 66 2c 20 61 6d 74 29 29 3c 61 6d 74 20 pBuf, amt))<amt
1af80 26 26 20 77 72 6f 74 65 3e 30 20 29 7b 0a 20 20 && wrote>0 ){.
1af90 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a amt -= wrote;.
1afa0 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 77 72 offset += wr
1afb0 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 ote;. pBuf =
1afc0 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 77 &((char*)pBuf)[w
1afd0 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20 20 53 69 6d rote];. }. Sim
1afe0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 28 20 77 ulateIOError(( w
1aff0 72 6f 74 65 3d 28 2d 31 29 2c 20 61 6d 74 3d 31 rote=(-1), amt=1
1b000 20 29 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44 ));. SimulateD
1b010 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 28 20 77 iskfullError(( w
1b020 72 6f 74 65 3d 30 2c 20 61 6d 74 3d 31 20 29 29 rote=0, amt=1 ))
1b030 3b 0a 0a 20 20 69 66 28 20 61 6d 74 3e 77 72 6f ;.. if( amt>wro
1b040 74 65 20 29 7b 0a 20 20 20 20 69 66 28 20 77 72 te ){. if( wr
1b050 6f 74 65 3c 30 20 26 26 20 70 46 69 6c 65 2d 3e ote<0 && pFile->
1b060 6c 61 73 74 45 72 72 6e 6f 21 3d 45 4e 4f 53 50 lastErrno!=ENOSP
1b070 43 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 6c 61 C ){. /* la
1b080 73 74 45 72 72 6e 6f 20 73 65 74 20 62 79 20 73 stErrno set by s
1b090 65 65 6b 41 6e 64 57 72 69 74 65 20 2a 2f 0a 20 eekAndWrite */.
1b0a0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
1b0b0 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a TE_IOERR_WRITE;.
1b0c0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1b0d0 20 73 74 6f 72 65 4c 61 73 74 45 72 72 6e 6f 28 storeLastErrno(
1b0e0 70 46 69 6c 65 2c 20 30 29 3b 20 2f 2a 20 6e 6f pFile, 0); /* no
1b0f0 74 20 61 20 73 79 73 74 65 6d 20 65 72 72 6f 72 t a system error
1b100 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e */. return
1b110 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 SQLITE_FULL;.
1b120 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 }. }.. retur
1b130 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1b140 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
1b150 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 ST./*.** Count t
1b160 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c he number of ful
1b170 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 lsyncs and norma
1b180 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 l syncs. This i
1b190 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a s used to test.*
1b1a0 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 * that syncs and
1b1b0 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f fullsyncs are o
1b1c0 63 63 75 72 72 69 6e 67 20 61 74 20 74 68 65 20 ccurring at the
1b1d0 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a right times..*/.
1b1e0 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 int sqlite3_sync
1b1f0 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 69 6e 74 20 _count = 0;.int
1b200 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 sqlite3_fullsync
1b210 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 _count = 0;.#end
1b220 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 64 6f 20 if../*.** We do
1b230 6e 6f 74 20 74 72 75 73 74 20 73 79 73 74 65 6d not trust system
1b240 73 20 74 6f 20 70 72 6f 76 69 64 65 20 61 20 77 s to provide a w
1b250 6f 72 6b 69 6e 67 20 66 64 61 74 61 73 79 6e 63 orking fdatasync
1b260 28 29 2e 20 20 53 6f 6d 65 20 64 6f 2e 0a 2a 2a (). Some do..**
1b270 20 4f 74 68 65 72 73 20 64 6f 20 6e 6f 2e 20 20 Others do no.
1b280 54 6f 20 62 65 20 73 61 66 65 2c 20 77 65 20 77 To be safe, we w
1b290 69 6c 6c 20 73 74 69 63 6b 20 77 69 74 68 20 74 ill stick with t
1b2a0 68 65 20 28 73 6c 69 67 68 74 6c 79 20 73 6c 6f he (slightly slo
1b2b0 77 65 72 29 0a 2a 2a 20 66 73 79 6e 63 28 29 2e wer).** fsync().
1b2c0 20 49 66 20 79 6f 75 20 6b 6e 6f 77 20 74 68 61 If you know tha
1b2d0 74 20 79 6f 75 72 20 73 79 73 74 65 6d 20 64 6f t your system do
1b2e0 65 73 20 73 75 70 70 6f 72 74 20 66 64 61 74 61 es support fdata
1b2f0 73 79 6e 63 28 29 20 63 6f 72 72 65 63 74 6c 79 sync() correctly
1b300 2c 0a 2a 2a 20 74 68 65 6e 20 73 69 6d 70 6c 79 ,.** then simply
1b310 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 2d 44 compile with -D
1b320 66 64 61 74 61 73 79 6e 63 3d 66 64 61 74 61 73 fdatasync=fdatas
1b330 79 6e 63 20 6f 72 20 2d 44 48 41 56 45 5f 46 44 ync or -DHAVE_FD
1b340 41 54 41 53 59 4e 43 0a 2a 2f 0a 23 69 66 20 21 ATASYNC.*/.#if !
1b350 64 65 66 69 6e 65 64 28 66 64 61 74 61 73 79 6e defined(fdatasyn
1b360 63 29 20 26 26 20 21 48 41 56 45 5f 46 44 41 54 c) && !HAVE_FDAT
1b370 41 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 66 ASYNC.# define f
1b380 64 61 74 61 73 79 6e 63 20 66 73 79 6e 63 0a 23 datasync fsync.#
1b390 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 endif../*.** Def
1b3a0 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 ine HAVE_FULLFSY
1b3b0 4e 43 20 74 6f 20 30 20 6f 72 20 31 20 64 65 70 NC to 0 or 1 dep
1b3c0 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 ending on whethe
1b3d0 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 r or not.** the
1b3e0 46 5f 46 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72 F_FULLFSYNC macr
1b3f0 6f 20 69 73 20 64 65 66 69 6e 65 64 2e 20 20 46 o is defined. F
1b400 5f 46 55 4c 4c 46 53 59 4e 43 20 69 73 20 63 75 _FULLFSYNC is cu
1b410 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20 rrently.** only
1b420 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 available on Mac
1b430 20 4f 53 20 58 2e 20 20 42 75 74 20 74 68 61 74 OS X. But that
1b440 20 63 6f 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a could change..*
1b450 2f 0a 23 69 66 64 65 66 20 46 5f 46 55 4c 4c 46 /.#ifdef F_FULLF
1b460 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 48 41 SYNC.# define HA
1b470 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23 VE_FULLFSYNC 1.#
1b480 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 48 41 else.# define HA
1b490 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23 VE_FULLFSYNC 0.#
1b4a0 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 endif.../*.** Th
1b4b0 65 20 66 73 79 6e 63 28 29 20 73 79 73 74 65 6d e fsync() system
1b4c0 20 63 61 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77 call does not w
1b4d0 6f 72 6b 20 61 73 20 61 64 76 65 72 74 69 73 65 ork as advertise
1b4e0 64 20 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69 d on many.** uni
1b4f0 78 20 73 79 73 74 65 6d 73 2e 20 20 54 68 65 20 x systems. The
1b500 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64 following proced
1b510 75 72 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70 ure is an attemp
1b520 74 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 t to make.** it
1b530 77 6f 72 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a work better..**.
1b540 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f ** The SQLITE_NO
1b550 5f 53 59 4e 43 20 6d 61 63 72 6f 20 64 69 73 61 _SYNC macro disa
1b560 62 6c 65 73 20 61 6c 6c 20 66 73 79 6e 63 28 29 bles all fsync()
1b570 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 66 s. This is usef
1b580 75 6c 0a 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e ul.** for testin
1b590 67 20 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74 g when we want t
1b5a0 6f 20 72 75 6e 20 74 68 72 6f 75 67 68 20 74 68 o run through th
1b5b0 65 20 74 65 73 74 20 73 75 69 74 65 20 71 75 69 e test suite qui
1b5c0 63 6b 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65 ckly..** You are
1b5d0 20 73 74 72 6f 6e 67 6c 79 20 61 64 76 69 73 65 strongly advise
1b5e0 64 20 2a 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f d *not* to deplo
1b5f0 79 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f y with SQLITE_NO
1b600 5f 53 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64 _SYNC.** enabled
1b610 2c 20 68 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 , however, since
1b620 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f with SQLITE_NO_
1b630 53 59 4e 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e SYNC enabled, an
1b640 20 4f 53 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20 OS crash.** or
1b650 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 69 power failure wi
1b660 6c 6c 20 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70 ll likely corrup
1b670 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 t the database f
1b680 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 ile..**.** SQLit
1b690 65 20 73 65 74 73 20 74 68 65 20 64 61 74 61 4f e sets the dataO
1b6a0 6e 6c 79 20 66 6c 61 67 20 69 66 20 74 68 65 20 nly flag if the
1b6b0 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 size of the file
1b6c0 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a is unchanged..*
1b6d0 2a 20 54 68 65 20 69 64 65 61 20 62 65 68 69 6e * The idea behin
1b6e0 64 20 64 61 74 61 4f 6e 6c 79 20 69 73 20 74 68 d dataOnly is th
1b6f0 61 74 20 69 74 20 73 68 6f 75 6c 64 20 6f 6e 6c at it should onl
1b700 79 20 77 72 69 74 65 20 74 68 65 20 66 69 6c 65 y write the file
1b710 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f 20 64 content.** to d
1b720 69 73 6b 2c 20 6e 6f 74 20 74 68 65 20 69 6e 6f isk, not the ino
1b730 64 65 2e 20 20 57 65 20 6f 6e 6c 79 20 73 65 74 de. We only set
1b740 20 64 61 74 61 4f 6e 6c 79 20 69 66 20 74 68 65 dataOnly if the
1b750 20 66 69 6c 65 20 73 69 7a 65 20 69 73 20 0a 2a file size is .*
1b760 2a 20 75 6e 63 68 61 6e 67 65 64 20 73 69 6e 63 * unchanged sinc
1b770 65 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 e the file size
1b780 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 is part of the i
1b790 6e 6f 64 65 2e 20 20 48 6f 77 65 76 65 72 2c 20 node. However,
1b7a0 0a 2a 2a 20 54 65 64 20 54 73 27 6f 20 74 65 6c .** Ted Ts'o tel
1b7b0 6c 73 20 75 73 20 74 68 61 74 20 66 64 61 74 61 ls us that fdata
1b7c0 73 79 6e 63 28 29 20 77 69 6c 6c 20 61 6c 73 6f sync() will also
1b7d0 20 77 72 69 74 65 20 74 68 65 20 69 6e 6f 64 65 write the inode
1b7e0 20 69 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 if the.** file
1b7f0 73 69 7a 65 20 68 61 73 20 63 68 61 6e 67 65 64 size has changed
1b800 2e 20 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 6c . The only real
1b810 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 difference betw
1b820 65 65 6e 20 66 64 61 74 61 73 79 6e 63 28 29 0a een fdatasync().
1b830 2a 2a 20 61 6e 64 20 66 73 79 6e 63 28 29 2c 20 ** and fsync(),
1b840 54 65 64 20 74 65 6c 6c 73 20 75 73 2c 20 69 73 Ted tells us, is
1b850 20 74 68 61 74 20 66 64 61 74 61 73 79 6e 63 28 that fdatasync(
1b860 29 20 77 69 6c 6c 20 6e 6f 74 20 66 6c 75 73 68 ) will not flush
1b870 20 74 68 65 0a 2a 2a 20 69 6e 6f 64 65 20 69 66 the.** inode if
1b880 20 74 68 65 20 6d 74 69 6d 65 20 6f 72 20 6f 77 the mtime or ow
1b890 6e 65 72 20 6f 72 20 6f 74 68 65 72 20 69 6e 6f ner or other ino
1b8a0 64 65 20 61 74 74 72 69 62 75 74 65 73 20 68 61 de attributes ha
1b8b0 76 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 57 ve changed..** W
1b8c0 65 20 6f 6e 6c 79 20 63 61 72 65 20 61 62 6f 75 e only care abou
1b8d0 74 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2c t the file size,
1b8e0 20 6e 6f 74 20 74 68 65 20 6f 74 68 65 72 20 66 not the other f
1b8f0 69 6c 65 20 61 74 74 72 69 62 75 74 65 73 2c 20 ile attributes,
1b900 73 6f 0a 2a 2a 20 61 73 20 66 61 72 20 61 73 20 so.** as far as
1b910 53 51 4c 69 74 65 20 69 73 20 63 6f 6e 63 65 72 SQLite is concer
1b920 6e 65 64 2c 20 61 6e 20 66 64 61 74 61 73 79 6e ned, an fdatasyn
1b930 63 28 29 20 69 73 20 61 6c 77 61 79 73 20 61 64 c() is always ad
1b940 65 71 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c 20 77 equate..** So, w
1b950 65 20 61 6c 77 61 79 73 20 75 73 65 20 66 64 61 e always use fda
1b960 74 61 73 79 6e 63 28 29 20 69 66 20 69 74 20 69 tasync() if it i
1b970 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 72 65 67 s available, reg
1b980 61 72 64 6c 65 73 73 20 6f 66 0a 2a 2a 20 74 68 ardless of.** th
1b990 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 e value of the d
1b9a0 61 74 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a 2a 2f ataOnly flag..*/
1b9b0 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c .static int full
1b9c0 5f 66 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 _fsync(int fd, i
1b9d0 6e 74 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 nt fullSync, int
1b9e0 20 64 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e dataOnly){. in
1b9f0 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 t rc;.. /* The
1ba00 66 6f 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 following "ifdef
1ba10 2f 65 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f /elif/else/" blo
1ba20 63 6b 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 ck has the same
1ba30 73 74 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a structure as. *
1ba40 2a 20 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e * the one below.
1ba50 20 49 74 20 69 73 20 72 65 70 6c 69 63 61 74 65 It is replicate
1ba60 64 20 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f d here solely to
1ba70 20 61 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e avoid clutterin
1ba80 67 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72 g . ** up the r
1ba90 65 61 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68 eal code with th
1baa0 65 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 e UNUSED_PARAMET
1bab0 45 52 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a ER() macros.. *
1bac0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
1bad0 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 NO_SYNC. UNUSED
1bae0 5f 50 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a _PARAMETER(fd);.
1baf0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
1bb00 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 ER(fullSync);.
1bb10 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
1bb20 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 (dataOnly);.#eli
1bb30 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 f HAVE_FULLFSYNC
1bb40 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
1bb50 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 TER(dataOnly);.#
1bb60 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 else. UNUSED_PA
1bb70 52 41 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 RAMETER(fullSync
1bb80 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
1bb90 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b METER(dataOnly);
1bba0 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 .#endif.. /* Re
1bbb0 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20 cord the number
1bbc0 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 77 65 of times that we
1bbd0 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79 do a normal fsy
1bbe0 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46 nc() and . ** F
1bbf0 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69 ULLSYNC. This i
1bc00 73 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 s used during te
1bc10 73 74 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20 sting to verify
1bc20 74 68 61 74 20 74 68 69 73 20 70 72 6f 63 65 64 that this proced
1bc30 75 72 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61 ure. ** gets ca
1bc40 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 6f lled with the co
1bc50 72 72 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e rrect arguments.
1bc60 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c . */.#ifdef SQL
1bc70 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 ITE_TEST. if( f
1bc80 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65 ullSync ) sqlite
1bc90 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 3_fullsync_count
1bca0 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 ++;. sqlite3_sy
1bcb0 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 nc_count++;.#end
1bcc0 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 if.. /* If we c
1bcd0 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 ompiled with the
1bce0 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 SQLITE_NO_SYNC
1bcf0 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 flag, then synci
1bd00 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d ng is a. ** no-
1bd10 6f 70 2e 20 20 42 75 74 20 67 6f 20 61 68 65 61 op. But go ahea
1bd20 64 20 61 6e 64 20 63 61 6c 6c 20 66 73 74 61 74 d and call fstat
1bd30 28 29 20 74 6f 20 76 61 6c 69 64 61 74 65 20 74 () to validate t
1bd40 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 64 65 73 he file. ** des
1bd50 63 72 69 70 74 6f 72 20 61 73 20 77 65 20 6e 65 criptor as we ne
1bd60 65 64 20 61 20 6d 65 74 68 6f 64 20 74 6f 20 70 ed a method to p
1bd70 72 6f 76 6f 6b 65 20 61 20 66 61 69 6c 75 72 65 rovoke a failure
1bd80 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20 63 6f 76 during. ** cov
1bd90 65 72 61 74 65 20 74 65 73 74 69 6e 67 2e 0a 20 erate testing..
1bda0 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
1bdb0 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 7b 0a 20 20 E_NO_SYNC. {.
1bdc0 20 20 73 74 72 75 63 74 20 73 74 61 74 20 62 75 struct stat bu
1bdd0 66 3b 0a 20 20 20 20 72 63 20 3d 20 6f 73 46 73 f;. rc = osFs
1bde0 74 61 74 28 66 64 2c 20 26 62 75 66 29 3b 0a 20 tat(fd, &buf);.
1bdf0 20 7d 0a 23 65 6c 69 66 20 48 41 56 45 5f 46 55 }.#elif HAVE_FU
1be00 4c 4c 46 53 59 4e 43 0a 20 20 69 66 28 20 66 75 LLFSYNC. if( fu
1be10 6c 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 llSync ){. rc
1be20 20 3d 20 6f 73 46 63 6e 74 6c 28 66 64 2c 20 46 = osFcntl(fd, F
1be30 5f 46 55 4c 4c 46 53 59 4e 43 2c 20 30 29 3b 0a _FULLFSYNC, 0);.
1be40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 }else{. rc
1be50 3d 20 31 3b 0a 20 20 7d 0a 20 20 2f 2a 20 49 66 = 1;. }. /* If
1be60 20 74 68 65 20 46 55 4c 4c 46 53 59 4e 43 20 66 the FULLFSYNC f
1be70 61 69 6c 65 64 2c 20 66 61 6c 6c 20 62 61 63 6b ailed, fall back
1be80 20 74 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 61 to attempting a
1be90 6e 20 66 73 79 6e 63 28 29 2e 0a 20 20 2a 2a 20 n fsync().. **
1bea0 49 74 20 73 68 6f 75 6c 64 6e 27 74 20 62 65 20 It shouldn't be
1beb0 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 66 75 6c possible for ful
1bec0 6c 66 73 79 6e 63 20 74 6f 20 66 61 69 6c 20 6f lfsync to fail o
1bed0 6e 20 74 68 65 20 6c 6f 63 61 6c 20 0a 20 20 2a n the local . *
1bee0 2a 20 66 69 6c 65 20 73 79 73 74 65 6d 20 28 6f * file system (o
1bef0 6e 20 4f 53 58 29 2c 20 73 6f 20 66 61 69 6c 75 n OSX), so failu
1bf00 72 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 re indicates tha
1bf10 74 20 46 55 4c 4c 46 53 59 4e 43 0a 20 20 2a 2a t FULLFSYNC. **
1bf20 20 69 73 6e 27 74 20 73 75 70 70 6f 72 74 65 64 isn't supported
1bf30 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 73 for this file s
1bf40 79 73 74 65 6d 2e 20 53 6f 2c 20 61 74 74 65 6d ystem. So, attem
1bf50 70 74 20 61 6e 20 66 73 79 6e 63 20 0a 20 20 2a pt an fsync . *
1bf60 2a 20 61 6e 64 20 28 66 6f 72 20 6e 6f 77 29 20 * and (for now)
1bf70 69 67 6e 6f 72 65 20 74 68 65 20 6f 76 65 72 68 ignore the overh
1bf80 65 61 64 20 6f 66 20 61 20 73 75 70 65 72 66 6c ead of a superfl
1bf90 75 6f 75 73 20 66 63 6e 74 6c 20 63 61 6c 6c 2e uous fcntl call.
1bfa0 20 20 0a 20 20 2a 2a 20 49 74 27 64 20 62 65 20 . ** It'd be
1bfb0 62 65 74 74 65 72 20 74 6f 20 64 65 74 65 63 74 better to detect
1bfc0 20 66 75 6c 6c 66 73 79 6e 63 20 73 75 70 70 6f fullfsync suppo
1bfd0 72 74 20 6f 6e 63 65 20 61 6e 64 20 61 76 6f 69 rt once and avoi
1bfe0 64 20 0a 20 20 2a 2a 20 74 68 65 20 66 63 6e 74 d . ** the fcnt
1bff0 6c 20 63 61 6c 6c 20 65 76 65 72 79 20 74 69 6d l call every tim
1c000 65 20 73 79 6e 63 20 69 73 20 63 61 6c 6c 65 64 e sync is called
1c010 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 20 .. */. if( rc
1c020 29 20 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29 ) rc = fsync(fd)
1c030 3b 0a 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 ;..#elif defined
1c040 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 2f 2a (__APPLE__). /*
1c050 20 66 64 61 74 61 73 79 6e 63 28 29 20 6f 6e 20 fdatasync() on
1c060 48 46 53 2b 20 64 6f 65 73 6e 27 74 20 79 65 74 HFS+ doesn't yet
1c070 20 66 6c 75 73 68 20 74 68 65 20 66 69 6c 65 20 flush the file
1c080 73 69 7a 65 20 69 66 20 69 74 20 63 68 61 6e 67 size if it chang
1c090 65 64 20 63 6f 72 72 65 63 74 6c 79 0a 20 20 2a ed correctly. *
1c0a0 2a 20 73 6f 20 63 75 72 72 65 6e 74 6c 79 20 77 * so currently w
1c0b0 65 20 64 65 66 61 75 6c 74 20 74 6f 20 74 68 65 e default to the
1c0c0 20 6d 61 63 72 6f 20 74 68 61 74 20 72 65 64 65 macro that rede
1c0d0 66 69 6e 65 73 20 66 64 61 74 61 73 79 6e 63 20 fines fdatasync
1c0e0 74 6f 20 66 73 79 6e 63 0a 20 20 2a 2f 0a 20 20 to fsync. */.
1c0f0 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a rc = fsync(fd);.
1c100 23 65 6c 73 65 20 0a 20 20 72 63 20 3d 20 66 64 #else . rc = fd
1c110 61 74 61 73 79 6e 63 28 66 64 29 3b 0a 23 69 66 atasync(fd);.#if
1c120 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 OS_VXWORKS. if
1c130 28 20 72 63 3d 3d 2d 31 20 26 26 20 65 72 72 6e ( rc==-1 && errn
1c140 6f 3d 3d 45 4e 4f 54 53 55 50 20 29 7b 0a 20 20 o==ENOTSUP ){.
1c150 20 20 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29 rc = fsync(fd)
1c160 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ;. }.#endif /*
1c170 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 23 65 OS_VXWORKS */.#e
1c180 6e 64 69 66 20 2f 2a 20 69 66 64 65 66 20 53 51 ndif /* ifdef SQ
1c190 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69 LITE_NO_SYNC eli
1c1a0 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 f HAVE_FULLFSYNC
1c1b0 20 2a 2f 0a 0a 20 20 69 66 28 20 4f 53 5f 56 58 */.. if( OS_VX
1c1c0 57 4f 52 4b 53 20 26 26 20 72 63 21 3d 20 2d 31 WORKS && rc!= -1
1c1d0 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 30 3b 0a ){. rc = 0;.
1c1e0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1c1f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 .}../*.** Open a
1c200 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
1c210 20 74 6f 20 74 68 65 20 64 69 72 65 63 74 6f 72 to the director
1c220 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 6c y containing fil
1c230 65 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 20 e zFilename..**
1c240 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a If successful, *
1c250 70 46 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 pFd is set to th
1c260 65 20 6f 70 65 6e 65 64 20 66 69 6c 65 20 64 65 e opened file de
1c270 73 63 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a 20 scriptor and.**
1c280 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
1c290 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 urned. If an err
1c2a0 6f 72 20 6f 63 63 75 72 73 2c 20 65 69 74 68 65 or occurs, eithe
1c2b0 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a r SQLITE_NOMEM.*
1c2c0 2a 20 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e 54 * or SQLITE_CANT
1c2d0 4f 50 45 4e 20 69 73 20 72 65 74 75 72 6e 65 64 OPEN is returned
1c2e0 20 61 6e 64 20 2a 70 46 64 20 69 73 20 73 65 74 and *pFd is set
1c2f0 20 74 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 to an undefined
1c300 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a .** value..**.**
1c310 20 54 68 65 20 64 69 72 65 63 74 6f 72 79 20 66 The directory f
1c320 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 ile descriptor i
1c330 73 20 75 73 65 64 20 66 6f 72 20 6f 6e 6c 79 20 s used for only
1c340 6f 6e 65 20 74 68 69 6e 67 20 2d 20 74 6f 0a 2a one thing - to.*
1c350 2a 20 66 73 79 6e 63 28 29 20 61 20 64 69 72 65 * fsync() a dire
1c360 63 74 6f 72 79 20 74 6f 20 6d 61 6b 65 20 73 75 ctory to make su
1c370 72 65 20 66 69 6c 65 20 63 72 65 61 74 69 6f 6e re file creation
1c380 20 61 6e 64 20 64 65 6c 65 74 69 6f 6e 20 65 76 and deletion ev
1c390 65 6e 74 73 0a 2a 2a 20 61 72 65 20 66 6c 75 73 ents.** are flus
1c3a0 68 65 64 20 74 6f 20 64 69 73 6b 2e 20 20 53 75 hed to disk. Su
1c3b0 63 68 20 66 73 79 6e 63 73 20 61 72 65 20 6e 6f ch fsyncs are no
1c3c0 74 20 6e 65 65 64 65 64 20 6f 6e 20 6e 65 77 65 t needed on newe
1c3d0 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 69 6e 67 20 r.** journaling
1c3e0 66 69 6c 65 73 79 73 74 65 6d 73 2c 20 62 75 74 filesystems, but
1c3f0 20 61 72 65 20 72 65 71 75 69 72 65 64 20 6f 6e are required on
1c400 20 6f 6c 64 65 72 20 66 69 6c 65 73 79 73 74 65 older filesyste
1c410 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ms..**.** This r
1c420 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 6f 76 outine can be ov
1c430 65 72 72 69 64 64 65 6e 20 75 73 69 6e 67 20 74 erridden using t
1c440 68 65 20 78 53 65 74 53 79 73 43 61 6c 6c 20 69 he xSetSysCall i
1c450 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65 nterface..** The
1c460 20 61 62 69 6c 69 74 79 20 74 6f 20 6f 76 65 72 ability to over
1c470 72 69 64 65 20 74 68 69 73 20 72 6f 75 74 69 6e ride this routin
1c480 65 20 77 61 73 20 61 64 64 65 64 20 69 6e 20 73 e was added in s
1c490 75 70 70 6f 72 74 20 6f 66 20 74 68 65 0a 2a 2a upport of the.**
1c4a0 20 63 68 72 6f 6d 69 75 6d 20 73 61 6e 64 62 6f chromium sandbo
1c4b0 78 2e 20 20 4f 70 65 6e 69 6e 67 20 61 20 64 69 x. Opening a di
1c4c0 72 65 63 74 6f 72 79 20 69 73 20 61 20 73 65 63 rectory is a sec
1c4d0 75 72 69 74 79 20 72 69 73 6b 20 28 77 65 20 61 urity risk (we a
1c4e0 72 65 0a 2a 2a 20 74 6f 6c 64 29 20 73 6f 20 6d re.** told) so m
1c4f0 61 6b 69 6e 67 20 69 74 20 6f 76 65 72 72 69 64 aking it overrid
1c500 65 61 62 6c 65 20 61 6c 6c 6f 77 73 20 74 68 65 eable allows the
1c510 20 63 68 72 6f 6d 69 75 6d 20 73 61 6e 64 62 6f chromium sandbo
1c520 78 20 74 6f 0a 2a 2a 20 72 65 70 6c 61 63 65 20 x to.** replace
1c530 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 this routine wit
1c540 68 20 61 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d h a harmless no-
1c550 6f 70 2e 20 20 54 6f 20 6d 61 6b 65 20 74 68 69 op. To make thi
1c560 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 20 6e s routine.** a n
1c570 6f 2d 6f 70 2c 20 72 65 70 6c 61 63 65 20 69 74 o-op, replace it
1c580 20 77 69 74 68 20 61 20 73 74 75 62 20 74 68 61 with a stub tha
1c590 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 t returns SQLITE
1c5a0 5f 4f 4b 20 62 75 74 20 6c 65 61 76 65 73 0a 2a _OK but leaves.*
1c5b0 2a 20 2a 70 46 64 20 73 65 74 20 74 6f 20 61 20 * *pFd set to a
1c5c0 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 2e negative number.
1c5d0 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 .**.** If SQLITE
1c5e0 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c _OK is returned,
1c5f0 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 the caller is r
1c600 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 esponsible for c
1c610 6c 6f 73 69 6e 67 0a 2a 2a 20 74 68 65 20 66 69 losing.** the fi
1c620 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 70 le descriptor *p
1c630 46 64 20 75 73 69 6e 67 20 63 6c 6f 73 65 28 29 Fd using close()
1c640 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1c650 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 28 63 6f openDirectory(co
1c660 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
1c670 61 6d 65 2c 20 69 6e 74 20 2a 70 46 64 29 7b 0a ame, int *pFd){.
1c680 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 int ii;. int
1c690 66 64 20 3d 20 2d 31 3b 0a 20 20 63 68 61 72 20 fd = -1;. char
1c6a0 7a 44 69 72 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 zDirname[MAX_PAT
1c6b0 48 4e 41 4d 45 2b 31 5d 3b 0a 0a 20 20 73 71 6c HNAME+1];.. sql
1c6c0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 ite3_snprintf(MA
1c6d0 58 5f 50 41 54 48 4e 41 4d 45 2c 20 7a 44 69 72 X_PATHNAME, zDir
1c6e0 6e 61 6d 65 2c 20 22 25 73 22 2c 20 7a 46 69 6c name, "%s", zFil
1c6f0 65 6e 61 6d 65 29 3b 0a 20 20 66 6f 72 28 69 69 ename);. for(ii
1c700 3d 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 44 69 =(int)strlen(zDi
1c710 72 6e 61 6d 65 29 3b 20 69 69 3e 30 20 26 26 20 rname); ii>0 &&
1c720 7a 44 69 72 6e 61 6d 65 5b 69 69 5d 21 3d 27 2f zDirname[ii]!='/
1c730 27 3b 20 69 69 2d 2d 29 3b 0a 20 20 69 66 28 20 '; ii--);. if(
1c740 69 69 3e 30 20 29 7b 0a 20 20 20 20 7a 44 69 72 ii>0 ){. zDir
1c750 6e 61 6d 65 5b 69 69 5d 20 3d 20 27 5c 30 27 3b name[ii] = '\0';
1c760 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 . }else{. if
1c770 28 20 7a 44 69 72 6e 61 6d 65 5b 30 5d 21 3d 27 ( zDirname[0]!='
1c780 2f 27 20 29 20 7a 44 69 72 6e 61 6d 65 5b 30 5d /' ) zDirname[0]
1c790 20 3d 20 27 2e 27 3b 0a 20 20 20 20 7a 44 69 72 = '.';. zDir
1c7a0 6e 61 6d 65 5b 31 5d 20 3d 20 30 3b 0a 20 20 7d name[1] = 0;. }
1c7b0 0a 20 20 66 64 20 3d 20 72 6f 62 75 73 74 5f 6f . fd = robust_o
1c7c0 70 65 6e 28 7a 44 69 72 6e 61 6d 65 2c 20 4f 5f pen(zDirname, O_
1c7d0 52 44 4f 4e 4c 59 7c 4f 5f 42 49 4e 41 52 59 2c RDONLY|O_BINARY,
1c7e0 20 30 29 3b 0a 20 20 69 66 28 20 66 64 3e 3d 30 0);. if( fd>=0
1c7f0 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 28 ){. OSTRACE(
1c800 28 22 4f 50 45 4e 44 49 52 20 25 2d 33 64 20 25 ("OPENDIR %-3d %
1c810 73 5c 6e 22 2c 20 66 64 2c 20 7a 44 69 72 6e 61 s\n", fd, zDirna
1c820 6d 65 29 29 3b 0a 20 20 7d 0a 20 20 2a 70 46 64 me));. }. *pFd
1c830 20 3d 20 66 64 3b 0a 20 20 69 66 28 20 66 64 3e = fd;. if( fd>
1c840 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 =0 ) return SQLI
1c850 54 45 5f 4f 4b 3b 0a 20 20 72 65 74 75 72 6e 20 TE_OK;. return
1c860 75 6e 69 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c unixLogError(SQL
1c870 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 ITE_CANTOPEN_BKP
1c880 54 2c 20 22 6f 70 65 6e 44 69 72 65 63 74 6f 72 T, "openDirector
1c890 79 22 2c 20 7a 44 69 72 6e 61 6d 65 29 3b 0a 7d y", zDirname);.}
1c8a0 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 ../*.** Make sur
1c8b0 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 e all writes to
1c8c0 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c a particular fil
1c8d0 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 e are committed
1c8e0 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 49 to disk..**.** I
1c8f0 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 74 68 f dataOnly==0 th
1c900 65 6e 20 62 6f 74 68 20 74 68 65 20 66 69 6c 65 en both the file
1c910 20 69 74 73 65 6c 66 20 61 6e 64 20 69 74 73 20 itself and its
1c920 6d 65 74 61 64 61 74 61 20 28 66 69 6c 65 0a 2a metadata (file.*
1c930 2a 20 73 69 7a 65 2c 20 61 63 63 65 73 73 20 74 * size, access t
1c940 69 6d 65 2c 20 65 74 63 29 20 61 72 65 20 73 79 ime, etc) are sy
1c950 6e 63 65 64 2e 20 20 49 66 20 64 61 74 61 4f 6e nced. If dataOn
1c960 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e 6c 79 20 ly!=0 then only
1c970 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 61 74 61 the.** file data
1c980 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a is synced..**.*
1c990 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c 20 61 6c * Under Unix, al
1c9a0 73 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 so make sure tha
1c9b0 74 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 t the directory
1c9c0 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 entry for the fi
1c9d0 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 le.** has been c
1c9e0 72 65 61 74 65 64 20 62 79 20 66 73 79 6e 63 2d reated by fsync-
1c9f0 69 6e 67 20 74 68 65 20 64 69 72 65 63 74 6f 72 ing the director
1ca00 79 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 y that contains
1ca10 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 66 20 the file..** If
1ca20 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 69 we do not do thi
1ca30 73 20 61 6e 64 20 77 65 20 65 6e 63 6f 75 6e 74 s and we encount
1ca40 65 72 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 er a power failu
1ca50 72 65 2c 20 74 68 65 20 64 69 72 65 63 74 6f 72 re, the director
1ca60 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 y.** entry for t
1ca70 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 he journal might
1ca80 20 6e 6f 74 20 65 78 69 73 74 20 61 66 74 65 72 not exist after
1ca90 20 77 65 20 72 65 62 6f 6f 74 2e 20 20 54 68 65 we reboot. The
1caa0 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 74 65 20 next.** SQLite
1cab0 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 66 69 to access the fi
1cac0 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f 77 le will not know
1cad0 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 that the journa
1cae0 6c 20 65 78 69 73 74 73 20 28 62 65 63 61 75 73 l exists (becaus
1caf0 65 0a 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f e.** the directo
1cb00 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 ry entry for the
1cb10 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6e 65 76 journal was nev
1cb20 65 72 20 63 72 65 61 74 65 64 29 20 61 6e 64 20 er created) and
1cb30 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a the transaction.
1cb40 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c 6c ** will not roll
1cb50 20 62 61 63 6b 20 2d 20 70 6f 73 73 69 62 6c 79 back - possibly
1cb60 20 6c 65 61 64 69 6e 67 20 74 6f 20 64 61 74 61 leading to data
1cb70 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e base corruption.
1cb80 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
1cb90 6e 69 78 53 79 6e 63 28 73 71 6c 69 74 65 33 5f nixSync(sqlite3_
1cba0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c file *id, int fl
1cbb0 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ags){. int rc;.
1cbc0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
1cbd0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
1cbe0 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44 61 74 61 d;.. int isData
1cbf0 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73 26 53 51 Only = (flags&SQ
1cc00 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e LITE_SYNC_DATAON
1cc10 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 46 75 6c LY);. int isFul
1cc20 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67 73 26 30 lsync = (flags&0
1cc30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e x0F)==SQLITE_SYN
1cc40 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 43 68 C_FULL;.. /* Ch
1cc50 65 63 6b 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 eck that one of
1cc60 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d SQLITE_SYNC_NORM
1cc70 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61 73 20 70 AL or FULL was p
1cc80 61 73 73 65 64 20 2a 2f 0a 20 20 61 73 73 65 72 assed */. asser
1cc90 74 28 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d t((flags&0x0F)==
1cca0 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d SQLITE_SYNC_NORM
1ccb0 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28 66 6c 61 AL. || (fla
1ccc0 67 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 gs&0x0F)==SQLITE
1ccd0 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20 29 3b 0a _SYNC_FULL. );.
1cce0 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61 6e 6e 6f . /* Unix canno
1ccf0 74 2c 20 62 75 74 20 73 6f 6d 65 20 73 79 73 74 t, but some syst
1cd00 65 6d 73 20 6d 61 79 20 72 65 74 75 72 6e 20 53 ems may return S
1cd10 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72 6f 6d 20 QLITE_FULL from
1cd20 68 65 72 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20 here. This. **
1cd30 6c 69 6e 65 20 69 73 20 74 6f 20 74 65 73 74 20 line is to test
1cd40 74 68 61 74 20 64 6f 69 6e 67 20 73 6f 20 64 6f that doing so do
1cd50 65 73 20 6e 6f 74 20 63 61 75 73 65 20 61 6e 79 es not cause any
1cd60 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a 2f 0a problems.. */.
1cd70 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 SimulateDiskfu
1cd80 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 llError( return
1cd90 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 0a SQLITE_FULL );..
1cda0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
1cdb0 29 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 53 );. OSTRACE(("S
1cdc0 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e 22 2c 20 YNC %-3d\n",
1cdd0 70 46 69 6c 65 2d 3e 68 29 29 3b 0a 20 20 72 63 pFile->h));. rc
1cde0 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 = full_fsync(pF
1cdf0 69 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c 73 79 ile->h, isFullsy
1ce00 6e 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79 29 3b nc, isDataOnly);
1ce10 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
1ce20 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 or( rc=1 );. if
1ce30 28 20 72 63 20 29 7b 0a 20 20 20 20 73 74 6f 72 ( rc ){. stor
1ce40 65 4c 61 73 74 45 72 72 6e 6f 28 70 46 69 6c 65 eLastErrno(pFile
1ce50 2c 20 65 72 72 6e 6f 29 3b 0a 20 20 20 20 72 65 , errno);. re
1ce60 74 75 72 6e 20 75 6e 69 78 4c 6f 67 45 72 72 6f turn unixLogErro
1ce70 72 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 r(SQLITE_IOERR_F
1ce80 53 59 4e 43 2c 20 22 66 75 6c 6c 5f 66 73 79 6e SYNC, "full_fsyn
1ce90 63 22 2c 20 70 46 69 6c 65 2d 3e 7a 50 61 74 68 c", pFile->zPath
1cea0 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 73 );. }.. /* Als
1ceb0 6f 20 66 73 79 6e 63 20 74 68 65 20 64 69 72 65 o fsync the dire
1cec0 63 74 6f 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 ctory containing
1ced0 20 74 68 65 20 66 69 6c 65 20 69 66 20 74 68 65 the file if the
1cee0 20 44 49 52 53 59 4e 43 20 66 6c 61 67 0a 20 20 DIRSYNC flag.
1cef0 2a 2a 20 69 73 20 73 65 74 2e 20 20 54 68 69 73 ** is set. This
1cf00 20 69 73 20 61 20 6f 6e 65 2d 74 69 6d 65 20 6f is a one-time o
1cf10 63 63 75 72 72 65 6e 63 65 2e 20 20 4d 61 6e 79 ccurrence. Many
1cf20 20 73 79 73 74 65 6d 73 20 28 65 78 61 6d 70 6c systems (exampl
1cf30 65 73 3a 20 41 49 58 29 0a 20 20 2a 2a 20 61 72 es: AIX). ** ar
1cf40 65 20 75 6e 61 62 6c 65 20 74 6f 20 66 73 79 6e e unable to fsyn
1cf50 63 20 61 20 64 69 72 65 63 74 6f 72 79 2c 20 73 c a directory, s
1cf60 6f 20 69 67 6e 6f 72 65 20 65 72 72 6f 72 73 20 o ignore errors
1cf70 6f 6e 20 74 68 65 20 66 73 79 6e 63 2e 0a 20 20 on the fsync..
1cf80 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e */. if( pFile->
1cf90 63 74 72 6c 46 6c 61 67 73 20 26 20 55 4e 49 58 ctrlFlags & UNIX
1cfa0 46 49 4c 45 5f 44 49 52 53 59 4e 43 20 29 7b 0a FILE_DIRSYNC ){.
1cfb0 20 20 20 20 69 6e 74 20 64 69 72 66 64 3b 0a 20 int dirfd;.
1cfc0 20 20 20 4f 53 54 52 41 43 45 28 28 22 44 49 52 OSTRACE(("DIR
1cfd0 53 59 4e 43 20 25 73 20 28 68 61 76 65 5f 66 75 SYNC %s (have_fu
1cfe0 6c 6c 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c 73 llfsync=%d fulls
1cff0 79 6e 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69 6c ync=%d)\n", pFil
1d000 65 2d 3e 7a 50 61 74 68 2c 0a 20 20 20 20 20 20 e->zPath,.
1d010 20 20 20 20 20 20 48 41 56 45 5f 46 55 4c 4c 46 HAVE_FULLF
1d020 53 59 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e 63 SYNC, isFullsync
1d030 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 6f 73 4f ));. rc = osO
1d040 70 65 6e 44 69 72 65 63 74 6f 72 79 28 70 46 69 penDirectory(pFi
1d050 6c 65 2d 3e 7a 50 61 74 68 2c 20 26 64 69 72 66 le->zPath, &dirf
1d060 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d d);. if( rc==
1d070 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1d080 20 20 20 66 75 6c 6c 5f 66 73 79 6e 63 28 64 69 full_fsync(di
1d090 72 66 64 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 rfd, 0, 0);.
1d0a0 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 70 robust_close(p
1d0b0 46 69 6c 65 2c 20 64 69 72 66 64 2c 20 5f 5f 4c File, dirfd, __L
1d0c0 49 4e 45 5f 5f 29 3b 0a 20 20 20 20 7d 65 6c 73 INE__);. }els
1d0d0 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 e{. assert(
1d0e0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 41 4e 54 rc==SQLITE_CANT
1d0f0 4f 50 45 4e 20 29 3b 0a 20 20 20 20 20 20 72 63 OPEN );. rc
1d100 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1d110 20 20 7d 0a 20 20 20 20 70 46 69 6c 65 2d 3e 63 }. pFile->c
1d120 74 72 6c 46 6c 61 67 73 20 26 3d 20 7e 55 4e 49 trlFlags &= ~UNI
1d130 58 46 49 4c 45 5f 44 49 52 53 59 4e 43 3b 0a 20 XFILE_DIRSYNC;.
1d140 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1d150 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 }../*.** Truncat
1d160 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 e an open file t
1d170 6f 20 61 20 73 70 65 63 69 66 69 65 64 20 73 69 o a specified si
1d180 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 ze.*/.static int
1d190 20 75 6e 69 78 54 72 75 6e 63 61 74 65 28 73 71 unixTruncate(sq
1d1a0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
1d1b0 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 75 6e i64 nByte){. un
1d1c0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
1d1d0 28 75 6e 69 78 46 69 6c 65 20 2a 29 69 64 3b 0a (unixFile *)id;.
1d1e0 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
1d1f0 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 53 rt( pFile );. S
1d200 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
1d210 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
1d220 45 52 52 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a ERR_TRUNCATE );.
1d230 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 75 73 65 . /* If the use
1d240 72 20 68 61 73 20 63 6f 6e 66 69 67 75 72 65 64 r has configured
1d250 20 61 20 63 68 75 6e 6b 2d 73 69 7a 65 20 66 6f a chunk-size fo
1d260 72 20 74 68 69 73 20 66 69 6c 65 2c 20 74 72 75 r this file, tru
1d270 6e 63 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 66 ncate the. ** f
1d280 69 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 ile so that it c
1d290 6f 6e 73 69 73 74 73 20 6f 66 20 61 6e 20 69 6e onsists of an in
1d2a0 74 65 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 teger number of
1d2b0 63 68 75 6e 6b 73 20 28 69 2e 65 2e 20 74 68 65 chunks (i.e. the
1d2c0 0a 20 20 2a 2a 20 61 63 74 75 61 6c 20 66 69 6c . ** actual fil
1d2d0 65 20 73 69 7a 65 20 61 66 74 65 72 20 74 68 65 e size after the
1d2e0 20 6f 70 65 72 61 74 69 6f 6e 20 6d 61 79 20 62 operation may b
1d2f0 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 e larger than th
1d300 65 20 72 65 71 75 65 73 74 65 64 0a 20 20 2a 2a e requested. **
1d310 20 73 69 7a 65 29 2e 0a 20 20 2a 2f 0a 20 20 69 size).. */. i
1d320 66 28 20 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e f( pFile->szChun
1d330 6b 3e 30 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 k>0 ){. nByte
1d340 20 3d 20 28 28 6e 42 79 74 65 20 2b 20 70 46 69 = ((nByte + pFi
1d350 6c 65 2d 3e 73 7a 43 68 75 6e 6b 20 2d 20 31 29 le->szChunk - 1)
1d360 2f 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b 29 /pFile->szChunk)
1d370 20 2a 20 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e * pFile->szChun
1d380 6b 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 72 k;. }.. rc = r
1d390 6f 62 75 73 74 5f 66 74 72 75 6e 63 61 74 65 28 obust_ftruncate(
1d3a0 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 29 pFile->h, nByte)
1d3b0 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 ;. if( rc ){.
1d3c0 20 20 73 74 6f 72 65 4c 61 73 74 45 72 72 6e 6f storeLastErrno
1d3d0 28 70 46 69 6c 65 2c 20 65 72 72 6e 6f 29 3b 0a (pFile, errno);.
1d3e0 20 20 20 20 72 65 74 75 72 6e 20 75 6e 69 78 4c return unixL
1d3f0 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 49 ogError(SQLITE_I
1d400 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 2c 20 22 OERR_TRUNCATE, "
1d410 66 74 72 75 6e 63 61 74 65 22 2c 20 70 46 69 6c ftruncate", pFil
1d420 65 2d 3e 7a 50 61 74 68 29 3b 0a 20 20 7d 65 6c e->zPath);. }el
1d430 73 65 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 se{.#ifdef SQLIT
1d440 45 5f 44 45 42 55 47 0a 20 20 20 20 2f 2a 20 49 E_DEBUG. /* I
1d450 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67 20 61 f we are doing a
1d460 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20 74 6f normal write to
1d470 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 a database file
1d480 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74 6f 0a (as opposed to.
1d490 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 61 20 68 ** doing a h
1d4a0 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 ot-journal rollb
1d4b0 61 63 6b 20 6f 72 20 61 20 77 72 69 74 65 20 74 ack or a write t
1d4c0 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 68 65 o some file othe
1d4d0 72 20 74 68 61 6e 20 61 0a 20 20 20 20 2a 2a 20 r than a. **
1d4e0 6e 6f 72 6d 61 6c 20 64 61 74 61 62 61 73 65 20 normal database
1d4f0 66 69 6c 65 29 20 61 6e 64 20 77 65 20 74 72 75 file) and we tru
1d500 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 20 74 ncate the file t
1d510 6f 20 7a 65 72 6f 20 6c 65 6e 67 74 68 2c 0a 20 o zero length,.
1d520 20 20 20 2a 2a 20 74 68 61 74 20 65 66 66 65 63 ** that effec
1d530 74 69 76 65 6c 79 20 75 70 64 61 74 65 73 20 74 tively updates t
1d540 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 he change counte
1d550 72 2e 20 20 54 68 69 73 20 6d 69 67 68 74 20 68 r. This might h
1d560 61 70 70 65 6e 0a 20 20 20 20 2a 2a 20 77 68 65 appen. ** whe
1d570 6e 20 72 65 73 74 6f 72 69 6e 67 20 61 20 64 61 n restoring a da
1d580 74 61 62 61 73 65 20 75 73 69 6e 67 20 74 68 65 tabase using the
1d590 20 62 61 63 6b 75 70 20 41 50 49 20 66 72 6f 6d backup API from
1d5a0 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 0a 20 a zero-length.
1d5b0 20 20 20 2a 2a 20 73 6f 75 72 63 65 2e 0a 20 20 ** source..
1d5c0 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 46 69 */. if( pFi
1d5d0 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 le->inNormalWrit
1d5e0 65 20 26 26 20 6e 42 79 74 65 3d 3d 30 20 29 7b e && nByte==0 ){
1d5f0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 74 72 . pFile->tr
1d600 61 6e 73 43 6e 74 72 43 68 6e 67 20 3d 20 31 3b ansCntrChng = 1;
1d610 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 . }.#endif..#
1d620 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4d 4d if SQLITE_MAX_MM
1d630 41 50 5f 53 49 5a 45 3e 30 0a 20 20 20 20 2f 2a AP_SIZE>0. /*
1d640 20 49 66 20 74 68 65 20 66 69 6c 65 20 77 61 73 If the file was
1d650 20 6a 75 73 74 20 74 72 75 6e 63 61 74 65 64 20 just truncated
1d660 74 6f 20 61 20 73 69 7a 65 20 73 6d 61 6c 6c 65 to a size smalle
1d670 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 r than the curre
1d680 6e 74 6c 79 0a 20 20 20 20 2a 2a 20 6d 61 70 70 ntly. ** mapp
1d690 65 64 20 72 65 67 69 6f 6e 2c 20 72 65 64 75 63 ed region, reduc
1d6a0 65 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20 e the effective
1d6b0 6d 61 70 70 69 6e 67 20 73 69 7a 65 20 61 73 20 mapping size as
1d6c0 77 65 6c 6c 2e 20 53 51 4c 69 74 65 20 77 69 6c well. SQLite wil
1d6d0 6c 0a 20 20 20 20 2a 2a 20 75 73 65 20 72 65 61 l. ** use rea
1d6e0 64 28 29 20 61 6e 64 20 77 72 69 74 65 28 29 20 d() and write()
1d6f0 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 20 62 to access data b
1d700 65 79 6f 6e 64 20 74 68 69 73 20 70 6f 69 6e 74 eyond this point
1d710 20 66 72 6f 6d 20 6e 6f 77 20 6f 6e 2e 20 20 0a from now on. .
1d720 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e */. if( n
1d730 42 79 74 65 3c 70 46 69 6c 65 2d 3e 6d 6d 61 70 Byte<pFile->mmap
1d740 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 70 46 Size ){. pF
1d750 69 6c 65 2d 3e 6d 6d 61 70 53 69 7a 65 20 3d 20 ile->mmapSize =
1d760 6e 42 79 74 65 3b 0a 20 20 20 20 7d 0a 23 65 6e nByte;. }.#en
1d770 64 69 66 0a 0a 20 20 20 20 72 65 74 75 72 6e 20 dif.. return
1d780 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d SQLITE_OK;. }.}
1d790 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e ../*.** Determin
1d7a0 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 e the current si
1d7b0 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 ze of a file in
1d7c0 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 bytes.*/.static
1d7d0 69 6e 74 20 75 6e 69 78 46 69 6c 65 53 69 7a 65 int unixFileSize
1d7e0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
1d7f0 64 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a d, i64 *pSize){.
1d800 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 int rc;. stru
1d810 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 61 ct stat buf;. a
1d820 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 72 ssert( id );. r
1d830 63 20 3d 20 6f 73 46 73 74 61 74 28 28 28 75 6e c = osFstat(((un
1d840 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 ixFile*)id)->h,
1d850 26 62 75 66 29 3b 0a 20 20 53 69 6d 75 6c 61 74 &buf);. Simulat
1d860 65 49 4f 45 72 72 6f 72 28 20 72 63 3d 31 20 29 eIOError( rc=1 )
1d870 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b ;. if( rc!=0 ){
1d880 0a 20 20 20 20 73 74 6f 72 65 4c 61 73 74 45 72 . storeLastEr
1d890 72 6e 6f 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 rno((unixFile*)i
1d8a0 64 2c 20 65 72 72 6e 6f 29 3b 0a 20 20 20 20 72 d, errno);. r
1d8b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
1d8c0 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 20 20 RR_FSTAT;. }.
1d8d0 2a 70 53 69 7a 65 20 3d 20 62 75 66 2e 73 74 5f *pSize = buf.st_
1d8e0 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 57 68 65 6e size;.. /* When
1d8f0 20 6f 70 65 6e 69 6e 67 20 61 20 7a 65 72 6f 2d opening a zero-
1d900 73 69 7a 65 20 64 61 74 61 62 61 73 65 2c 20 74 size database, t
1d910 68 65 20 66 69 6e 64 49 6e 6f 64 65 49 6e 66 6f he findInodeInfo
1d920 28 29 20 70 72 6f 63 65 64 75 72 65 0a 20 20 2a () procedure. *
1d930 2a 20 77 72 69 74 65 73 20 61 20 73 69 6e 67 6c * writes a singl
1d940 65 20 62 79 74 65 20 69 6e 74 6f 20 74 68 61 74 e byte into that
1d950 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 74 file in order t
1d960 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 61 20 o work around a
1d970 62 75 67 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 bug. ** in the
1d980 4f 53 2d 58 20 6d 73 64 6f 73 20 66 69 6c 65 73 OS-X msdos files
1d990 79 73 74 65 6d 2e 20 20 49 6e 20 6f 72 64 65 72 ystem. In order
1d9a0 20 74 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 to avoid proble
1d9b0 6d 73 20 77 69 74 68 20 75 70 70 65 72 0a 20 20 ms with upper.
1d9c0 2a 2a 20 6c 61 79 65 72 73 2c 20 77 65 20 6e 65 ** layers, we ne
1d9d0 65 64 20 74 6f 20 72 65 70 6f 72 74 20 74 68 69 ed to report thi
1d9e0 73 20 66 69 6c 65 20 73 69 7a 65 20 61 73 20 7a s file size as z
1d9f0 65 72 6f 20 65 76 65 6e 20 74 68 6f 75 67 68 20 ero even though
1da00 69 74 20 69 73 0a 20 20 2a 2a 20 72 65 61 6c 6c it is. ** reall
1da10 79 20 31 2e 20 20 20 54 69 63 6b 65 74 20 23 33 y 1. Ticket #3
1da20 32 36 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 260.. */. if(
1da30 2a 70 53 69 7a 65 3d 3d 31 20 29 20 2a 70 53 69 *pSize==1 ) *pSi
1da40 7a 65 20 3d 20 30 3b 0a 0a 0a 20 20 72 65 74 75 ze = 0;... retu
1da50 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1da60 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 .#if SQLITE_ENAB
1da70 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
1da80 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 && defined(__AP
1da90 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a 20 48 61 6e PLE__)./*.** Han
1daa0 64 6c 65 72 20 66 6f 72 20 70 72 6f 78 79 2d 6c dler for proxy-l
1dab0 6f 63 6b 69 6e 67 20 66 69 6c 65 2d 63 6f 6e 74 ocking file-cont
1dac0 72 6f 6c 20 76 65 72 62 73 2e 20 20 44 65 66 69 rol verbs. Defi
1dad0 6e 65 64 20 62 65 6c 6f 77 20 69 6e 20 74 68 65 ned below in the
1dae0 0a 2a 2a 20 70 72 6f 78 79 69 6e 67 20 6c 6f 63 .** proxying loc
1daf0 6b 69 6e 67 20 64 69 76 69 73 69 6f 6e 2e 0a 2a king division..*
1db00 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f /.static int pro
1db10 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 xyFileControl(sq
1db20 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69 6e 74 2c lite3_file*,int,
1db30 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a void*);.#endif..
1db40 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 /* .** This func
1db50 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 tion is called t
1db60 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 53 51 4c o handle the SQL
1db70 49 54 45 5f 46 43 4e 54 4c 5f 53 49 5a 45 5f 48 ITE_FCNTL_SIZE_H
1db80 49 4e 54 20 0a 2a 2a 20 66 69 6c 65 2d 63 6f 6e INT .** file-con
1db90 74 72 6f 6c 20 6f 70 65 72 61 74 69 6f 6e 2e 20 trol operation.
1dba0 20 45 6e 6c 61 72 67 65 20 74 68 65 20 64 61 74 Enlarge the dat
1dbb0 61 62 61 73 65 20 74 6f 20 6e 42 79 74 65 73 20 abase to nBytes
1dbc0 69 6e 20 73 69 7a 65 0a 2a 2a 20 28 72 6f 75 6e in size.** (roun
1dbd0 64 65 64 20 75 70 20 74 6f 20 74 68 65 20 6e 65 ded up to the ne
1dbe0 78 74 20 63 68 75 6e 6b 2d 73 69 7a 65 29 2e 20 xt chunk-size).
1dbf0 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 If the database
1dc00 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 6e is already.** n
1dc10 42 79 74 65 73 20 6f 72 20 6c 61 72 67 65 72 2c Bytes or larger,
1dc20 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
1dc30 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 a no-op..*/.sta
1dc40 74 69 63 20 69 6e 74 20 66 63 6e 74 6c 53 69 7a tic int fcntlSiz
1dc50 65 48 69 6e 74 28 75 6e 69 78 46 69 6c 65 20 2a eHint(unixFile *
1dc60 70 46 69 6c 65 2c 20 69 36 34 20 6e 42 79 74 65 pFile, i64 nByte
1dc70 29 7b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e ){. if( pFile->
1dc80 73 7a 43 68 75 6e 6b 3e 30 20 29 7b 0a 20 20 20 szChunk>0 ){.
1dc90 20 69 36 34 20 6e 53 69 7a 65 3b 20 20 20 20 20 i64 nSize;
1dca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1dcb0 2a 20 52 65 71 75 69 72 65 64 20 66 69 6c 65 20 * Required file
1dcc0 73 69 7a 65 20 2a 2f 0a 20 20 20 20 73 74 72 75 size */. stru
1dcd0 63 74 20 73 74 61 74 20 62 75 66 3b 20 20 20 20 ct stat buf;
1dce0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 /* Use
1dcf0 64 20 74 6f 20 68 6f 6c 64 20 72 65 74 75 72 6e d to hold return
1dd00 20 76 61 6c 75 65 73 20 6f 66 20 66 73 74 61 74 values of fstat
1dd10 28 29 20 2a 2f 0a 20 20 20 0a 20 20 20 20 69 66 () */. . if
1dd20 28 20 6f 73 46 73 74 61 74 28 70 46 69 6c 65 2d ( osFstat(pFile-
1dd30 3e 68 2c 20 26 62 75 66 29 20 29 7b 0a 20 20 20 >h, &buf) ){.
1dd40 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1dd50 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 _IOERR_FSTAT;.
1dd60 20 20 7d 0a 0a 20 20 20 20 6e 53 69 7a 65 20 3d }.. nSize =
1dd70 20 28 28 6e 42 79 74 65 2b 70 46 69 6c 65 2d 3e ((nByte+pFile->
1dd80 73 7a 43 68 75 6e 6b 2d 31 29 20 2f 20 70 46 69 szChunk-1) / pFi
1dd90 6c 65 2d 3e 73 7a 43 68 75 6e 6b 29 20 2a 20 70 le->szChunk) * p
1dda0 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b 3b 0a 20 File->szChunk;.
1ddb0 20 20 20 69 66 28 20 6e 53 69 7a 65 3e 28 69 36 if( nSize>(i6
1ddc0 34 29 62 75 66 2e 73 74 5f 73 69 7a 65 20 29 7b 4)buf.st_size ){
1ddd0 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 48 41 ..#if defined(HA
1dde0 56 45 5f 50 4f 53 49 58 5f 46 41 4c 4c 4f 43 41 VE_POSIX_FALLOCA
1ddf0 54 45 29 20 26 26 20 48 41 56 45 5f 50 4f 53 49 TE) && HAVE_POSI
1de00 58 5f 46 41 4c 4c 4f 43 41 54 45 0a 20 20 20 20 X_FALLOCATE.
1de10 20 20 2f 2a 20 54 68 65 20 63 6f 64 65 20 62 65 /* The code be
1de20 6c 6f 77 20 69 73 20 68 61 6e 64 6c 69 6e 67 20 low is handling
1de30 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 the return value
1de40 20 6f 66 20 6f 73 46 61 6c 6c 6f 63 61 74 65 28 of osFallocate(
1de50 29 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 72 72 ) . ** corr
1de60 65 63 74 6c 79 2e 20 70 6f 73 69 78 5f 66 61 6c ectly. posix_fal
1de70 6c 6f 63 61 74 65 28 29 20 69 73 20 64 65 66 69 locate() is defi
1de80 6e 65 64 20 74 6f 20 22 72 65 74 75 72 6e 73 20 ned to "returns
1de90 7a 65 72 6f 20 6f 6e 20 73 75 63 63 65 73 73 2c zero on success,
1dea0 20 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 61 6e . ** or an
1deb0 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 20 6f 6e error number on
1dec0 20 20 66 61 69 6c 75 72 65 22 2e 20 53 65 65 20 failure". See
1ded0 74 68 65 20 6d 61 6e 70 61 67 65 20 66 6f 72 20 the manpage for
1dee0 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 20 20 20 20 details. */.
1def0 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 20 20 20 int err;.
1df00 20 64 6f 7b 0a 20 20 20 20 20 20 20 20 65 72 72 do{. err
1df10 20 3d 20 6f 73 46 61 6c 6c 6f 63 61 74 65 28 70 = osFallocate(p
1df20 46 69 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f File->h, buf.st_
1df30 73 69 7a 65 2c 20 6e 53 69 7a 65 2d 62 75 66 2e size, nSize-buf.
1df40 73 74 5f 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 st_size);.
1df50 7d 77 68 69 6c 65 28 20 65 72 72 3d 3d 45 49 4e }while( err==EIN
1df60 54 52 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 TR );. if(
1df70 65 72 72 20 29 20 72 65 74 75 72 6e 20 53 51 4c err ) return SQL
1df80 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b ITE_IOERR_WRITE;
1df90 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 2f 2a 20 .#else. /*
1dfa0 49 66 20 74 68 65 20 4f 53 20 64 6f 65 73 20 6e If the OS does n
1dfb0 6f 74 20 68 61 76 65 20 70 6f 73 69 78 5f 66 61 ot have posix_fa
1dfc0 6c 6c 6f 63 61 74 65 28 29 2c 20 66 61 6b 65 20 llocate(), fake
1dfd0 69 74 2e 20 57 72 69 74 65 20 61 20 0a 20 20 20 it. Write a .
1dfe0 20 20 20 2a 2a 20 73 69 6e 67 6c 65 20 62 79 74 ** single byt
1dff0 65 20 74 6f 20 74 68 65 20 6c 61 73 74 20 62 79 e to the last by
1e000 74 65 20 69 6e 20 65 61 63 68 20 62 6c 6f 63 6b te in each block
1e010 20 74 68 61 74 20 66 61 6c 6c 73 20 65 6e 74 69 that falls enti
1e020 72 65 6c 79 0a 20 20 20 20 20 20 2a 2a 20 77 69 rely. ** wi
1e030 74 68 69 6e 20 74 68 65 20 65 78 74 65 6e 64 65 thin the extende
1e040 64 20 72 65 67 69 6f 6e 2e 20 54 68 65 6e 2c 20 d region. Then,
1e050 69 66 20 72 65 71 75 69 72 65 64 2c 20 61 20 73 if required, a s
1e060 69 6e 67 6c 65 20 62 79 74 65 0a 20 20 20 20 20 ingle byte.
1e070 20 2a 2a 20 61 74 20 6f 66 66 73 65 74 20 28 6e ** at offset (n
1e080 53 69 7a 65 2d 31 29 2c 20 74 6f 20 73 65 74 20 Size-1), to set
1e090 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
1e0a0 66 69 6c 65 20 63 6f 72 72 65 63 74 6c 79 2e 0a file correctly..
1e0b0 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 ** This is
1e0c0 20 61 20 73 69 6d 69 6c 61 72 20 74 65 63 68 6e a similar techn
1e0d0 69 71 75 65 20 74 6f 20 74 68 61 74 20 75 73 65 ique to that use
1e0e0 64 20 62 79 20 67 6c 69 62 63 20 6f 6e 20 73 79 d by glibc on sy
1e0f0 73 74 65 6d 73 0a 20 20 20 20 20 20 2a 2a 20 74 stems. ** t
1e100 68 61 74 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 hat do not have
1e110 61 20 72 65 61 6c 20 66 61 6c 6c 6f 63 61 74 65 a real fallocate
1e120 28 29 20 63 61 6c 6c 2e 0a 20 20 20 20 20 20 2a () call.. *
1e130 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 42 6c 6b /. int nBlk
1e140 20 3d 20 62 75 66 2e 73 74 5f 62 6c 6b 73 69 7a = buf.st_blksiz
1e150 65 3b 20 20 2f 2a 20 46 69 6c 65 2d 73 79 73 74 e; /* File-syst
1e160 65 6d 20 62 6c 6f 63 6b 20 73 69 7a 65 20 2a 2f em block size */
1e170 0a 20 20 20 20 20 20 69 6e 74 20 6e 57 72 69 74 . int nWrit
1e180 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 e = 0;
1e190 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1e1a0 62 79 74 65 73 20 77 72 69 74 74 65 6e 20 62 79 bytes written by
1e1b0 20 73 65 65 6b 41 6e 64 57 72 69 74 65 20 2a 2f seekAndWrite */
1e1c0 0a 20 20 20 20 20 20 69 36 34 20 69 57 72 69 74 . i64 iWrit
1e1d0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
1e1e0 20 20 20 2f 2a 20 4e 65 78 74 20 6f 66 66 73 65 /* Next offse
1e1f0 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 2a 2f t to write to */
1e200 0a 0a 20 20 20 20 20 20 69 57 72 69 74 65 20 3d .. iWrite =
1e210 20 28 62 75 66 2e 73 74 5f 73 69 7a 65 2f 6e 42 (buf.st_size/nB
1e220 6c 6b 29 2a 6e 42 6c 6b 20 2b 20 6e 42 6c 6b 20 lk)*nBlk + nBlk
1e230 2d 20 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 - 1;. asser
1e240 74 28 20 69 57 72 69 74 65 3e 3d 62 75 66 2e 73 t( iWrite>=buf.s
1e250 74 5f 73 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 t_size );.
1e260 61 73 73 65 72 74 28 20 28 28 69 57 72 69 74 65 assert( ((iWrite
1e270 2b 31 29 25 6e 42 6c 6b 29 3d 3d 30 20 29 3b 0a +1)%nBlk)==0 );.
1e280 20 20 20 20 20 20 66 6f 72 28 2f 2a 6e 6f 2d 6f for(/*no-o
1e290 70 2a 2f 3b 20 69 57 72 69 74 65 3c 6e 53 69 7a p*/; iWrite<nSiz
1e2a0 65 2b 6e 42 6c 6b 2d 31 3b 20 69 57 72 69 74 65 e+nBlk-1; iWrite
1e2b0 2b 3d 6e 42 6c 6b 20 29 7b 0a 20 20 20 20 20 20 +=nBlk ){.
1e2c0 20 20 69 66 28 20 69 57 72 69 74 65 3e 3d 6e 53 if( iWrite>=nS
1e2d0 69 7a 65 20 29 20 69 57 72 69 74 65 20 3d 20 6e ize ) iWrite = n
1e2e0 53 69 7a 65 20 2d 20 31 3b 0a 20 20 20 20 20 20 Size - 1;.
1e2f0 20 20 6e 57 72 69 74 65 20 3d 20 73 65 65 6b 41 nWrite = seekA
1e300 6e 64 57 72 69 74 65 28 70 46 69 6c 65 2c 20 69 ndWrite(pFile, i
1e310 57 72 69 74 65 2c 20 22 22 2c 20 31 29 3b 0a 20 Write, "", 1);.
1e320 20 20 20 20 20 20 20 69 66 28 20 6e 57 72 69 74 if( nWrit
1e330 65 21 3d 31 20 29 20 72 65 74 75 72 6e 20 53 51 e!=1 ) return SQ
1e340 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 LITE_IOERR_WRITE
1e350 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 ;. }.#endif
1e360 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 20 . }. }..#if
1e370 53 51 4c 49 54 45 5f 4d 41 58 5f 4d 4d 41 50 5f SQLITE_MAX_MMAP_
1e380 53 49 5a 45 3e 30 0a 20 20 69 66 28 20 70 46 69 SIZE>0. if( pFi
1e390 6c 65 2d 3e 6d 6d 61 70 53 69 7a 65 4d 61 78 3e le->mmapSizeMax>
1e3a0 30 20 26 26 20 6e 42 79 74 65 3e 70 46 69 6c 65 0 && nByte>pFile
1e3b0 2d 3e 6d 6d 61 70 53 69 7a 65 20 29 7b 0a 20 20 ->mmapSize ){.
1e3c0 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 69 66 int rc;. if
1e3d0 28 20 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b ( pFile->szChunk
1e3e0 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 <=0 ){. if(
1e3f0 20 72 6f 62 75 73 74 5f 66 74 72 75 6e 63 61 74 robust_ftruncat
1e400 65 28 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 e(pFile->h, nByt
1e410 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 e) ){. st
1e420 6f 72 65 4c 61 73 74 45 72 72 6e 6f 28 70 46 69 oreLastErrno(pFi
1e430 6c 65 2c 20 65 72 72 6e 6f 29 3b 0a 20 20 20 20 le, errno);.
1e440 20 20 20 20 72 65 74 75 72 6e 20 75 6e 69 78 4c return unixL
1e450 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 49 ogError(SQLITE_I
1e460 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 2c 20 22 OERR_TRUNCATE, "
1e470 66 74 72 75 6e 63 61 74 65 22 2c 20 70 46 69 6c ftruncate", pFil
1e480 65 2d 3e 7a 50 61 74 68 29 3b 0a 20 20 20 20 20 e->zPath);.
1e490 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 }. }.. rc
1e4a0 20 3d 20 75 6e 69 78 4d 61 70 66 69 6c 65 28 70 = unixMapfile(p
1e4b0 46 69 6c 65 2c 20 6e 42 79 74 65 29 3b 0a 20 20 File, nByte);.
1e4c0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
1e4d0 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 .#endif.. retur
1e4e0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1e4f0 2f 2a 0a 2a 2a 20 49 66 20 2a 70 41 72 67 20 69 /*.** If *pArg i
1e500 73 20 69 6e 69 74 69 61 6c 6c 79 20 6e 65 67 61 s initially nega
1e510 74 69 76 65 20 74 68 65 6e 20 74 68 69 73 20 69 tive then this i
1e520 73 20 61 20 71 75 65 72 79 2e 20 20 53 65 74 20 s a query. Set
1e530 2a 70 41 72 67 20 74 6f 0a 2a 2a 20 31 20 6f 72 *pArg to.** 1 or
1e540 20 30 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 0 depending on
1e550 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 62 whether or not b
1e560 69 74 20 6d 61 73 6b 20 6f 66 20 70 46 69 6c 65 it mask of pFile
1e570 2d 3e 63 74 72 6c 46 6c 61 67 73 20 69 73 20 73 ->ctrlFlags is s
1e580 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 2a 70 41 et..**.** If *pA
1e590 72 67 20 69 73 20 30 20 6f 72 20 31 2c 20 74 68 rg is 0 or 1, th
1e5a0 65 6e 20 63 6c 65 61 72 20 6f 72 20 73 65 74 20 en clear or set
1e5b0 74 68 65 20 6d 61 73 6b 20 62 69 74 20 6f 66 20 the mask bit of
1e5c0 70 46 69 6c 65 2d 3e 63 74 72 6c 46 6c 61 67 73 pFile->ctrlFlags
1e5d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1e5e0 20 75 6e 69 78 4d 6f 64 65 42 69 74 28 75 6e 69 unixModeBit(uni
1e5f0 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 75 6e xFile *pFile, un
1e600 73 69 67 6e 65 64 20 63 68 61 72 20 6d 61 73 6b signed char mask
1e610 2c 20 69 6e 74 20 2a 70 41 72 67 29 7b 0a 20 20 , int *pArg){.
1e620 69 66 28 20 2a 70 41 72 67 3c 30 20 29 7b 0a 20 if( *pArg<0 ){.
1e630 20 20 20 2a 70 41 72 67 20 3d 20 28 70 46 69 6c *pArg = (pFil
1e640 65 2d 3e 63 74 72 6c 46 6c 61 67 73 20 26 20 6d e->ctrlFlags & m
1e650 61 73 6b 29 21 3d 30 3b 0a 20 20 7d 65 6c 73 65 ask)!=0;. }else
1e660 20 69 66 28 20 28 2a 70 41 72 67 29 3d 3d 30 20 if( (*pArg)==0
1e670 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 63 74 ){. pFile->ct
1e680 72 6c 46 6c 61 67 73 20 26 3d 20 7e 6d 61 73 6b rlFlags &= ~mask
1e690 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
1e6a0 46 69 6c 65 2d 3e 63 74 72 6c 46 6c 61 67 73 20 File->ctrlFlags
1e6b0 7c 3d 20 6d 61 73 6b 3b 0a 20 20 7d 0a 7d 0a 0a |= mask;. }.}..
1e6c0 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 /* Forward decla
1e6d0 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 ration */.static
1e6e0 20 69 6e 74 20 75 6e 69 78 47 65 74 54 65 6d 70 int unixGetTemp
1e6f0 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 name(int nBuf, c
1e700 68 61 72 20 2a 7a 42 75 66 29 3b 0a 0a 2f 2a 0a har *zBuf);../*.
1e710 2a 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 ** Information a
1e720 6e 64 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 61 6e nd control of an
1e730 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c open file handl
1e740 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1e750 20 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c unixFileControl
1e760 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
1e770 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 d, int op, void
1e780 2a 70 41 72 67 29 7b 0a 20 20 75 6e 69 78 46 69 *pArg){. unixFi
1e790 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
1e7a0 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 73 77 69 xFile*)id;. swi
1e7b0 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 tch( op ){. c
1e7c0 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c ase SQLITE_FCNTL
1e7d0 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 _LOCKSTATE: {.
1e7e0 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 *(int*)pArg
1e7f0 3d 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f = pFile->eFileLo
1e800 63 6b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ck;. return
1e810 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
1e820 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 }. case SQLIT
1e830 45 5f 46 43 4e 54 4c 5f 4c 41 53 54 5f 45 52 52 E_FCNTL_LAST_ERR
1e840 4e 4f 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e NO: {. *(in
1e850 74 2a 29 70 41 72 67 20 3d 20 70 46 69 6c 65 2d t*)pArg = pFile-
1e860 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 >lastErrno;.
1e870 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1e880 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 OK;. }. ca
1e890 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f se SQLITE_FCNTL_
1e8a0 43 48 55 4e 4b 5f 53 49 5a 45 3a 20 7b 0a 20 20 CHUNK_SIZE: {.
1e8b0 20 20 20 20 70 46 69 6c 65 2d 3e 73 7a 43 68 75 pFile->szChu
1e8c0 6e 6b 20 3d 20 2a 28 69 6e 74 20 2a 29 70 41 72 nk = *(int *)pAr
1e8d0 67 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 g;. return
1e8e0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
1e8f0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
1e900 5f 46 43 4e 54 4c 5f 53 49 5a 45 5f 48 49 4e 54 _FCNTL_SIZE_HINT
1e910 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 : {. int rc
1e920 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 ;. Simulate
1e930 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 IOErrorBenign(1)
1e940 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 63 6e ;. rc = fcn
1e950 74 6c 53 69 7a 65 48 69 6e 74 28 70 46 69 6c 65 tlSizeHint(pFile
1e960 2c 20 2a 28 69 36 34 20 2a 29 70 41 72 67 29 3b , *(i64 *)pArg);
1e970 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 . SimulateI
1e980 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b OErrorBenign(0);
1e990 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
1e9a0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1e9b0 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 45 SQLITE_FCNTL_PE
1e9c0 52 53 49 53 54 5f 57 41 4c 3a 20 7b 0a 20 20 20 RSIST_WAL: {.
1e9d0 20 20 20 75 6e 69 78 4d 6f 64 65 42 69 74 28 70 unixModeBit(p
1e9e0 46 69 6c 65 2c 20 55 4e 49 58 46 49 4c 45 5f 50 File, UNIXFILE_P
1e9f0 45 52 53 49 53 54 5f 57 41 4c 2c 20 28 69 6e 74 ERSIST_WAL, (int
1ea00 2a 29 70 41 72 67 29 3b 0a 20 20 20 20 20 20 72 *)pArg);. r
1ea10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1ea20 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1ea30 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 4f 57 SQLITE_FCNTL_POW
1ea40 45 52 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 ERSAFE_OVERWRITE
1ea50 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 4d 6f : {. unixMo
1ea60 64 65 42 69 74 28 70 46 69 6c 65 2c 20 55 4e 49 deBit(pFile, UNI
1ea70 58 46 49 4c 45 5f 50 53 4f 57 2c 20 28 69 6e 74 XFILE_PSOW, (int
1ea80 2a 29 70 41 72 67 29 3b 0a 20 20 20 20 20 20 72 *)pArg);. r
1ea90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1eaa0 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1eab0 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 56 46 53 SQLITE_FCNTL_VFS
1eac0 4e 41 4d 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28 NAME: {. *(
1ead0 63 68 61 72 2a 2a 29 70 41 72 67 20 3d 20 73 71 char**)pArg = sq
1eae0 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 lite3_mprintf("%
1eaf0 73 22 2c 20 70 46 69 6c 65 2d 3e 70 56 66 73 2d s", pFile->pVfs-
1eb00 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 >zName);. r
1eb10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1eb20 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1eb30 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 54 45 4d SQLITE_FCNTL_TEM
1eb40 50 46 49 4c 45 4e 41 4d 45 3a 20 7b 0a 20 20 20 PFILENAME: {.
1eb50 20 20 20 63 68 61 72 20 2a 7a 54 46 69 6c 65 20 char *zTFile
1eb60 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 = sqlite3_malloc
1eb70 36 34 28 20 70 46 69 6c 65 2d 3e 70 56 66 73 2d 64( pFile->pVfs-
1eb80 3e 6d 78 50 61 74 68 6e 61 6d 65 20 29 3b 0a 20 >mxPathname );.
1eb90 20 20 20 20 20 69 66 28 20 7a 54 46 69 6c 65 20 if( zTFile
1eba0 29 7b 0a 20 20 20 20 20 20 20 20 75 6e 69 78 47 ){. unixG
1ebb0 65 74 54 65 6d 70 6e 61 6d 65 28 70 46 69 6c 65 etTempname(pFile
1ebc0 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 ->pVfs->mxPathna
1ebd0 6d 65 2c 20 7a 54 46 69 6c 65 29 3b 0a 20 20 20 me, zTFile);.
1ebe0 20 20 20 20 20 2a 28 63 68 61 72 2a 2a 29 70 41 *(char**)pA
1ebf0 72 67 20 3d 20 7a 54 46 69 6c 65 3b 0a 20 20 20 rg = zTFile;.
1ec00 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 }. retur
1ec10 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 n SQLITE_OK;.
1ec20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 }. case SQLI
1ec30 54 45 5f 46 43 4e 54 4c 5f 48 41 53 5f 4d 4f 56 TE_FCNTL_HAS_MOV
1ec40 45 44 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e ED: {. *(in
1ec50 74 2a 29 70 41 72 67 20 3d 20 66 69 6c 65 48 61 t*)pArg = fileHa
1ec60 73 4d 6f 76 65 64 28 70 46 69 6c 65 29 3b 0a 20 sMoved(pFile);.
1ec70 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
1ec80 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69 66 TE_OK;. }.#if
1ec90 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4d 4d 41 50 SQLITE_MAX_MMAP
1eca0 5f 53 49 5a 45 3e 30 0a 20 20 20 20 63 61 73 65 _SIZE>0. case
1ecb0 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4d 4d SQLITE_FCNTL_MM
1ecc0 41 50 5f 53 49 5a 45 3a 20 7b 0a 20 20 20 20 20 AP_SIZE: {.
1ecd0 20 69 36 34 20 6e 65 77 4c 69 6d 69 74 20 3d 20 i64 newLimit =
1ece0 2a 28 69 36 34 2a 29 70 41 72 67 3b 0a 20 20 20 *(i64*)pArg;.
1ecf0 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 int rc = SQLI
1ed00 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 69 66 28 TE_OK;. if(
1ed10 20 6e 65 77 4c 69 6d 69 74 3e 73 71 6c 69 74 65 newLimit>sqlite
1ed20 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 78 3GlobalConfig.mx
1ed30 4d 6d 61 70 20 29 7b 0a 20 20 20 20 20 20 20 20 Mmap ){.
1ed40 6e 65 77 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 newLimit = sqlit
1ed50 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
1ed60 78 4d 6d 61 70 3b 0a 20 20 20 20 20 20 7d 0a 20 xMmap;. }.
1ed70 20 20 20 20 20 2a 28 69 36 34 2a 29 70 41 72 67 *(i64*)pArg
1ed80 20 3d 20 70 46 69 6c 65 2d 3e 6d 6d 61 70 53 69 = pFile->mmapSi
1ed90 7a 65 4d 61 78 3b 0a 20 20 20 20 20 20 69 66 28 zeMax;. if(
1eda0 20 6e 65 77 4c 69 6d 69 74 3e 3d 30 20 26 26 20 newLimit>=0 &&
1edb0 6e 65 77 4c 69 6d 69 74 21 3d 70 46 69 6c 65 2d newLimit!=pFile-
1edc0 3e 6d 6d 61 70 53 69 7a 65 4d 61 78 20 26 26 20 >mmapSizeMax &&
1edd0 70 46 69 6c 65 2d 3e 6e 46 65 74 63 68 4f 75 74 pFile->nFetchOut
1ede0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ==0 ){. p
1edf0 46 69 6c 65 2d 3e 6d 6d 61 70 53 69 7a 65 4d 61 File->mmapSizeMa
1ee00 78 20 3d 20 6e 65 77 4c 69 6d 69 74 3b 0a 20 20 x = newLimit;.
1ee10 20 20 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d if( pFile-
1ee20 3e 6d 6d 61 70 53 69 7a 65 3e 30 20 29 7b 0a 20 >mmapSize>0 ){.
1ee30 20 20 20 20 20 20 20 20 20 75 6e 69 78 55 6e 6d unixUnm
1ee40 61 70 66 69 6c 65 28 70 46 69 6c 65 29 3b 0a 20 apfile(pFile);.
1ee50 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 75 6e rc = un
1ee60 69 78 4d 61 70 66 69 6c 65 28 70 46 69 6c 65 2c ixMapfile(pFile,
1ee70 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a -1);. }.
1ee80 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 }. re
1ee90 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 23 turn rc;. }.#
1eea0 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
1eeb0 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 2f 2a ITE_DEBUG. /*
1eec0 20 54 68 65 20 70 61 67 65 72 20 63 61 6c 6c 73 The pager calls
1eed0 20 74 68 69 73 20 6d 65 74 68 6f 64 20 74 6f 20 this method to
1eee0 73 69 67 6e 61 6c 20 74 68 61 74 20 69 74 20 68 signal that it h
1eef0 61 73 20 64 6f 6e 65 0a 20 20 20 20 2a 2a 20 61 as done. ** a
1ef00 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 74 68 rollback and th
1ef10 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 at the database
1ef20 69 73 20 74 68 65 72 65 66 6f 72 65 20 75 6e 63 is therefore unc
1ef30 68 61 6e 67 65 64 20 61 6e 64 0a 20 20 20 20 2a hanged and. *
1ef40 2a 20 69 74 20 68 65 6e 63 65 20 69 74 20 69 73 * it hence it is
1ef50 20 4f 4b 20 66 6f 72 20 74 68 65 20 74 72 61 6e OK for the tran
1ef60 73 61 63 74 69 6f 6e 20 63 68 61 6e 67 65 20 63 saction change c
1ef70 6f 75 6e 74 65 72 20 74 6f 20 62 65 0a 20 20 20 ounter to be.
1ef80 20 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20 ** unchanged..
1ef90 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 */. case S
1efa0 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 QLITE_FCNTL_DB_U
1efb0 4e 43 48 41 4e 47 45 44 3a 20 7b 0a 20 20 20 20 NCHANGED: {.
1efc0 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 ((unixFile*)id
1efd0 29 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30 3b )->dbUpdate = 0;
1efe0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
1eff0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 LITE_OK;. }.#
1f000 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 endif.#if SQLITE
1f010 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
1f020 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 STYLE && defined
1f030 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20 (__APPLE__).
1f040 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 case SQLITE_FCNT
1f050 4c 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 L_SET_LOCKPROXYF
1f060 49 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 53 51 ILE:. case SQ
1f070 4c 49 54 45 5f 46 43 4e 54 4c 5f 47 45 54 5f 4c LITE_FCNTL_GET_L
1f080 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 20 7b 0a OCKPROXYFILE: {.
1f090 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f return pro
1f0a0 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64 xyFileControl(id
1f0b0 2c 6f 70 2c 70 41 72 67 29 3b 0a 20 20 20 20 7d ,op,pArg);. }
1f0c0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1f0d0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
1f0e0 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 _STYLE && define
1f0f0 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 2a 2f 0a d(__APPLE__) */.
1f100 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
1f110 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 3b 0a 7d 0a ITE_NOTFOUND;.}.
1f120 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
1f130 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 6e e sector size in
1f140 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 75 6e bytes of the un
1f150 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 64 derlying block d
1f160 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 evice for.** the
1f170 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 2e specified file.
1f180 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 74 20 This is almost
1f190 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 73 always 512 bytes
1f1a0 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a 20 , but may be.**
1f1b0 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d 65 20 larger for some
1f1c0 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 devices..**.** S
1f1d0 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73 75 6d QLite code assum
1f1e0 65 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e es this function
1f1f0 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 74 cannot fail. It
1f200 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 74 68 also assumes th
1f210 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69 6c at.** if two fil
1f220 65 73 20 61 72 65 20 63 72 65 61 74 65 64 20 69 es are created i
1f230 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2d n the same file-
1f240 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f 72 79 system directory
1f250 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 61 (i.e..** a data
1f260 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a 6f 75 base and its jou
1f270 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 74 20 rnal file) that
1f280 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 the sector size
1f290 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20 73 will be the.** s
1f2a0 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f ame for both..*/
1f2b0 0a 23 69 66 6e 64 65 66 20 5f 5f 51 4e 58 4e 54 .#ifndef __QNXNT
1f2c0 4f 5f 5f 20 0a 73 74 61 74 69 63 20 69 6e 74 20 O__ .static int
1f2d0 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 28 73 unixSectorSize(s
1f2e0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 qlite3_file *Not
1f2f0 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f Used){. UNUSED_
1f300 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
1f310 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c d);. return SQL
1f320 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 ITE_DEFAULT_SECT
1f330 4f 52 5f 53 49 5a 45 3b 0a 7d 0a 23 65 6e 64 69 OR_SIZE;.}.#endi
1f340 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c f../*.** The fol
1f350 6c 6f 77 69 6e 67 20 76 65 72 73 69 6f 6e 20 6f lowing version o
1f360 66 20 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 f unixSectorSize
1f370 28 29 20 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 () is optimized
1f380 66 6f 72 20 51 4e 58 2e 0a 2a 2f 0a 23 69 66 64 for QNX..*/.#ifd
1f390 65 66 20 5f 5f 51 4e 58 4e 54 4f 5f 5f 0a 23 69 ef __QNXNTO__.#i
1f3a0 6e 63 6c 75 64 65 20 3c 73 79 73 2f 64 63 6d 64 nclude <sys/dcmd
1f3b0 5f 62 6c 6b 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 _blk.h>.#include
1f3c0 20 3c 73 79 73 2f 73 74 61 74 76 66 73 2e 68 3e <sys/statvfs.h>
1f3d0 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
1f3e0 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 SectorSize(sqlit
1f3f0 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 e3_file *id){.
1f400 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
1f410 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
1f420 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 73 65 . if( pFile->se
1f430 63 74 6f 72 53 69 7a 65 20 3d 3d 20 30 20 29 7b ctorSize == 0 ){
1f440 0a 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74 . struct stat
1f450 76 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20 20 20 vfs fsInfo;.
1f460 20 20 20 0a 20 20 20 20 2f 2a 20 53 65 74 20 64 . /* Set d
1f470 65 66 61 75 6c 74 73 20 66 6f 72 20 6e 6f 6e 2d efaults for non-
1f480 73 75 70 70 6f 72 74 65 64 20 66 69 6c 65 73 79 supported filesy
1f490 73 74 65 6d 73 20 2a 2f 0a 20 20 20 20 70 46 69 stems */. pFi
1f4a0 6c 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d le->sectorSize =
1f4b0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
1f4c0 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 20 SECTOR_SIZE;.
1f4d0 20 70 46 69 6c 65 2d 3e 64 65 76 69 63 65 43 68 pFile->deviceCh
1f4e0 61 72 61 63 74 65 72 69 73 74 69 63 73 20 3d 20 aracteristics =
1f4f0 30 3b 0a 20 20 20 20 69 66 28 20 66 73 74 61 74 0;. if( fstat
1f500 76 66 73 28 70 46 69 6c 65 2d 3e 68 2c 20 26 66 vfs(pFile->h, &f
1f510 73 49 6e 66 6f 29 20 3d 3d 20 2d 31 20 29 20 7b sInfo) == -1 ) {
1f520 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 46 . return pF
1f530 69 6c 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3b ile->sectorSize;
1f540 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
1f550 21 73 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66 !strcmp(fsInfo.f
1f560 5f 62 61 73 65 74 79 70 65 2c 20 22 74 6d 70 22 _basetype, "tmp"
1f570 29 20 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c ) ) {. pFil
1f580 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 e->sectorSize =
1f590 66 73 49 6e 66 6f 2e 66 5f 62 73 69 7a 65 3b 0a fsInfo.f_bsize;.
1f5a0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 64 65 76 pFile->dev
1f5b0 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
1f5c0 63 73 20 3d 0a 20 20 20 20 20 20 20 20 53 51 4c cs =. SQL
1f5d0 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
1f5e0 34 4b 20 7c 20 20 20 20 20 20 20 2f 2a 20 41 6c 4K | /* Al
1f5f0 6c 20 72 61 6d 20 66 69 6c 65 73 79 73 74 65 6d l ram filesystem
1f600 20 77 72 69 74 65 73 20 61 72 65 20 61 74 6f 6d writes are atom
1f610 69 63 20 2a 2f 0a 20 20 20 20 20 20 20 20 53 51 ic */. SQ
1f620 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f LITE_IOCAP_SAFE_
1f630 41 50 50 45 4e 44 20 7c 20 20 20 20 2f 2a 20 67 APPEND | /* g
1f640 72 6f 77 69 6e 67 20 74 68 65 20 66 69 6c 65 20 rowing the file
1f650 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 20 75 does not occur u
1f660 6e 74 69 6c 0a 20 20 20 20 20 20 20 20 20 20 20 ntil.
1f670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f680 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 ** th
1f690 65 20 77 72 69 74 65 20 73 75 63 63 65 65 64 73 e write succeeds
1f6a0 20 2a 2f 0a 20 20 20 20 20 20 20 20 53 51 4c 49 */. SQLI
1f6b0 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 TE_IOCAP_SEQUENT
1f6c0 49 41 4c 20 7c 20 20 20 20 20 2f 2a 20 54 68 65 IAL | /* The
1f6d0 20 72 61 6d 20 66 69 6c 65 73 79 73 74 65 6d 20 ram filesystem
1f6e0 68 61 73 20 6e 6f 20 77 72 69 74 65 20 62 65 68 has no write beh
1f6f0 69 6e 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 ind.
1f700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f710 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 20 ** so
1f720 69 74 20 69 73 20 6f 72 64 65 72 65 64 20 2a 2f it is ordered */
1f730 0a 20 20 20 20 20 20 20 20 30 3b 0a 20 20 20 20 . 0;.
1f740 7d 65 6c 73 65 20 69 66 28 20 73 74 72 73 74 72 }else if( strstr
1f750 28 66 73 49 6e 66 6f 2e 66 5f 62 61 73 65 74 79 (fsInfo.f_basety
1f760 70 65 2c 20 22 65 74 66 73 22 29 20 29 7b 0a 20 pe, "etfs") ){.
1f770 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 65 63 74 pFile->sect
1f780 6f 72 53 69 7a 65 20 3d 20 66 73 49 6e 66 6f 2e orSize = fsInfo.
1f790 66 5f 62 73 69 7a 65 3b 0a 20 20 20 20 20 20 70 f_bsize;. p
1f7a0 46 69 6c 65 2d 3e 64 65 76 69 63 65 43 68 61 72 File->deviceChar
1f7b0 61 63 74 65 72 69 73 74 69 63 73 20 3d 0a 20 20 acteristics =.
1f7c0 20 20 20 20 20 20 2f 2a 20 65 74 66 73 20 63 6c /* etfs cl
1f7d0 75 73 74 65 72 20 73 69 7a 65 20 77 72 69 74 65 uster size write
1f7e0 73 20 61 72 65 20 61 74 6f 6d 69 63 20 2a 2f 0a s are atomic */.
1f7f0 20 20 20 20 20 20 20 20 28 70 46 69 6c 65 2d 3e (pFile->
1f800 73 65 63 74 6f 72 53 69 7a 65 20 2f 20 35 31 32 sectorSize / 512
1f810 20 2a 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f * SQLITE_IOCAP_
1f820 41 54 4f 4d 49 43 35 31 32 29 20 7c 0a 20 20 20 ATOMIC512) |.
1f830 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 43 41 SQLITE_IOCA
1f840 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 7c 20 P_SAFE_APPEND |
1f850 20 20 20 2f 2a 20 67 72 6f 77 69 6e 67 20 74 68 /* growing th
1f860 65 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 e file does not
1f870 6f 63 63 75 72 20 75 6e 74 69 6c 0a 20 20 20 20 occur until.
1f880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f8a0 20 20 2a 2a 20 74 68 65 20 77 72 69 74 65 20 73 ** the write s
1f8b0 75 63 63 65 65 64 73 20 2a 2f 0a 20 20 20 20 20 ucceeds */.
1f8c0 20 20 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f SQLITE_IOCAP_
1f8d0 53 45 51 55 45 4e 54 49 41 4c 20 7c 20 20 20 20 SEQUENTIAL |
1f8e0 20 2f 2a 20 54 68 65 20 72 61 6d 20 66 69 6c 65 /* The ram file
1f8f0 73 79 73 74 65 6d 20 68 61 73 20 6e 6f 20 77 72 system has no wr
1f900 69 74 65 20 62 65 68 69 6e 64 0a 20 20 20 20 20 ite behind.
1f910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f930 20 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f 72 64 ** so it is ord
1f940 65 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 ered */.
1f950 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 0;. }else if(
1f960 20 21 73 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e !strcmp(fsInfo.
1f970 66 5f 62 61 73 65 74 79 70 65 2c 20 22 71 6e 78 f_basetype, "qnx
1f980 36 22 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6") ){. pFi
1f990 6c 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d le->sectorSize =
1f9a0 20 66 73 49 6e 66 6f 2e 66 5f 62 73 69 7a 65 3b fsInfo.f_bsize;
1f9b0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 64 65 . pFile->de
1f9c0 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
1f9d0 69 63 73 20 3d 0a 20 20 20 20 20 20 20 20 53 51 ics =. SQ
1f9e0 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
1f9f0 43 20 7c 20 20 20 20 20 20 20 20 20 2f 2a 20 41 C | /* A
1fa00 6c 6c 20 66 69 6c 65 73 79 73 74 65 6d 20 77 72 ll filesystem wr
1fa10 69 74 65 73 20 61 72 65 20 61 74 6f 6d 69 63 20 ites are atomic
1fa20 2a 2f 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 */. SQLIT
1fa30 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 E_IOCAP_SAFE_APP
1fa40 45 4e 44 20 7c 20 20 20 20 2f 2a 20 67 72 6f 77 END | /* grow
1fa50 69 6e 67 20 74 68 65 20 66 69 6c 65 20 64 6f 65 ing the file doe
1fa60 73 20 6e 6f 74 20 6f 63 63 75 72 20 75 6e 74 69 s not occur unti
1fa70 6c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l.
1fa80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa90 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 77 ** the w
1faa0 72 69 74 65 20 73 75 63 63 65 65 64 73 20 2a 2f rite succeeds */
1fab0 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f . SQLITE_
1fac0 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c IOCAP_SEQUENTIAL
1fad0 20 7c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 61 | /* The ra
1fae0 6d 20 66 69 6c 65 73 79 73 74 65 6d 20 68 61 73 m filesystem has
1faf0 20 6e 6f 20 77 72 69 74 65 20 62 65 68 69 6e 64 no write behind
1fb00 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1fb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fb20 20 20 20 20 20 20 20 2a 2a 20 73 6f 20 69 74 20 ** so it
1fb30 69 73 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 is ordered */.
1fb40 20 20 20 20 20 20 30 3b 0a 20 20 20 20 7d 65 6c 0;. }el
1fb50 73 65 20 69 66 28 20 21 73 74 72 63 6d 70 28 66 se if( !strcmp(f
1fb60 73 49 6e 66 6f 2e 66 5f 62 61 73 65 74 79 70 65 sInfo.f_basetype
1fb70 2c 20 22 71 6e 78 34 22 29 20 29 7b 0a 20 20 20 , "qnx4") ){.
1fb80 20 20 20 70 46 69 6c 65 2d 3e 73 65 63 74 6f 72 pFile->sector
1fb90 53 69 7a 65 20 3d 20 66 73 49 6e 66 6f 2e 66 5f Size = fsInfo.f_
1fba0 62 73 69 7a 65 3b 0a 20 20 20 20 20 20 70 46 69 bsize;. pFi
1fbb0 6c 65 2d 3e 64 65 76 69 63 65 43 68 61 72 61 63 le->deviceCharac
1fbc0 74 65 72 69 73 74 69 63 73 20 3d 0a 20 20 20 20 teristics =.
1fbd0 20 20 20 20 2f 2a 20 66 75 6c 6c 20 62 69 74 73 /* full bits
1fbe0 65 74 20 6f 66 20 61 74 6f 6d 69 63 73 20 66 72 et of atomics fr
1fbf0 6f 6d 20 6d 61 78 20 73 65 63 74 6f 72 20 73 69 om max sector si
1fc00 7a 65 20 61 6e 64 20 73 6d 61 6c 6c 65 72 20 2a ze and smaller *
1fc10 2f 0a 20 20 20 20 20 20 20 20 28 28 70 46 69 6c /. ((pFil
1fc20 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 2f 20 e->sectorSize /
1fc30 35 31 32 20 2a 20 53 51 4c 49 54 45 5f 49 4f 43 512 * SQLITE_IOC
1fc40 41 50 5f 41 54 4f 4d 49 43 35 31 32 29 20 3c 3c AP_ATOMIC512) <<
1fc50 20 31 29 20 2d 20 32 20 7c 0a 20 20 20 20 20 20 1) - 2 |.
1fc60 20 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 SQLITE_IOCAP_S
1fc70 45 51 55 45 4e 54 49 41 4c 20 7c 20 20 20 20 20 EQUENTIAL |
1fc80 2f 2a 20 54 68 65 20 72 61 6d 20 66 69 6c 65 73 /* The ram files
1fc90 79 73 74 65 6d 20 68 61 73 20 6e 6f 20 77 72 69 ystem has no wri
1fca0 74 65 20 62 65 68 69 6e 64 0a 20 20 20 20 20 20 te behind.
1fcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fcd0 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f 72 64 65 ** so it is orde
1fce0 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 30 red */. 0
1fcf0 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
1fd00 73 74 72 73 74 72 28 66 73 49 6e 66 6f 2e 66 5f strstr(fsInfo.f_
1fd10 62 61 73 65 74 79 70 65 2c 20 22 64 6f 73 22 29 basetype, "dos")
1fd20 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d ){. pFile-
1fd30 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 66 73 >sectorSize = fs
1fd40 49 6e 66 6f 2e 66 5f 62 73 69 7a 65 3b 0a 20 20 Info.f_bsize;.
1fd50 20 20 20 20 70 46 69 6c 65 2d 3e 64 65 76 69 63 pFile->devic
1fd60 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
1fd70 20 3d 0a 20 20 20 20 20 20 20 20 2f 2a 20 66 75 =. /* fu
1fd80 6c 6c 20 62 69 74 73 65 74 20 6f 66 20 61 74 6f ll bitset of ato
1fd90 6d 69 63 73 20 66 72 6f 6d 20 6d 61 78 20 73 65 mics from max se
1fda0 63 74 6f 72 20 73 69 7a 65 20 61 6e 64 20 73 6d ctor size and sm
1fdb0 61 6c 6c 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 aller */.
1fdc0 20 28 28 70 46 69 6c 65 2d 3e 73 65 63 74 6f 72 ((pFile->sector
1fdd0 53 69 7a 65 20 2f 20 35 31 32 20 2a 20 53 51 4c Size / 512 * SQL
1fde0 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
1fdf0 35 31 32 29 20 3c 3c 20 31 29 20 2d 20 32 20 7c 512) << 1) - 2 |
1fe00 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f . SQLITE_
1fe10 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c IOCAP_SEQUENTIAL
1fe20 20 7c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 61 | /* The ra
1fe30 6d 20 66 69 6c 65 73 79 73 74 65 6d 20 68 61 73 m filesystem has
1fe40 20 6e 6f 20 77 72 69 74 65 20 62 65 68 69 6e 64 no write behind
1fe50 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1fe60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fe70 20 20 20 20 20 20 20 2a 2a 20 73 6f 20 69 74 20 ** so it
1fe80 69 73 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 is ordered */.
1fe90 20 20 20 20 20 20 30 3b 0a 20 20 20 20 7d 65 6c 0;. }el
1fea0 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d se{. pFile-
1feb0 3e 64 65 76 69 63 65 43 68 61 72 61 63 74 65 72 >deviceCharacter
1fec0 69 73 74 69 63 73 20 3d 0a 20 20 20 20 20 20 20 istics =.
1fed0 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 SQLITE_IOCAP_AT
1fee0 4f 4d 49 43 35 31 32 20 7c 20 20 20 20 20 20 2f OMIC512 | /
1fef0 2a 20 62 6c 6f 63 6b 73 20 61 72 65 20 61 74 6f * blocks are ato
1ff00 6d 69 63 20 2a 2f 0a 20 20 20 20 20 20 20 20 53 mic */. S
1ff10 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 QLITE_IOCAP_SAFE
1ff20 5f 41 50 50 45 4e 44 20 7c 20 20 20 20 2f 2a 20 _APPEND | /*
1ff30 67 72 6f 77 69 6e 67 20 74 68 65 20 66 69 6c 65 growing the file
1ff40 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 20 does not occur
1ff50 75 6e 74 69 6c 0a 20 20 20 20 20 20 20 20 20 20 until.
1ff60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ff70 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 ** t
1ff80 68 65 20 77 72 69 74 65 20 73 75 63 63 65 65 64 he write succeed
1ff90 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 30 3b 0a s */. 0;.
1ffa0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 4c }. }. /* L
1ffb0 61 73 74 20 63 68 61 6e 63 65 20 76 65 72 69 66 ast chance verif
1ffc0 69 63 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 ication. If the
1ffd0 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73 6e sector size isn
1ffe0 27 74 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 't a multiple of
1fff0 20 35 31 32 0a 20 20 2a 2a 20 74 68 65 6e 20 69 512. ** then i
20000 74 20 69 73 6e 27 74 20 76 61 6c 69 64 2e 2a 2f t isn't valid.*/
20010 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 73 65 . if( pFile->se
20020 63 74 6f 72 53 69 7a 65 20 25 20 35 31 32 20 21 ctorSize % 512 !
20030 3d 20 30 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 = 0 ){. pFile
20040 2d 3e 64 65 76 69 63 65 43 68 61 72 61 63 74 65 ->deviceCharacte
20050 72 69 73 74 69 63 73 20 3d 20 30 3b 0a 20 20 20 ristics = 0;.
20060 20 70 46 69 6c 65 2d 3e 73 65 63 74 6f 72 53 69 pFile->sectorSi
20070 7a 65 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 ze = SQLITE_DEFA
20080 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b ULT_SECTOR_SIZE;
20090 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 46 . }. return pF
200a0 69 6c 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3b ile->sectorSize;
200b0 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 5f 51 .}.#endif /* __Q
200c0 4e 58 4e 54 4f 5f 5f 20 2a 2f 0a 0a 2f 2a 0a 2a NXNTO__ */../*.*
200d0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 65 76 * Return the dev
200e0 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74 ice characterist
200f0 69 63 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65 ics for the file
20100 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 56 46 53 ..**.** This VFS
20110 20 69 73 20 73 65 74 20 75 70 20 74 6f 20 72 65 is set up to re
20120 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 43 41 turn SQLITE_IOCA
20130 50 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 P_POWERSAFE_OVER
20140 57 52 49 54 45 20 62 79 20 64 65 66 61 75 6c 74 WRITE by default
20150 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 ..** However, th
20160 61 74 20 63 68 6f 69 63 65 20 69 73 20 63 6f 6e at choice is con
20170 74 72 6f 76 65 72 73 69 61 6c 20 73 69 6e 63 65 troversial since
20180 20 74 65 63 68 6e 69 63 61 6c 6c 79 20 74 68 65 technically the
20190 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 66 underlying.** f
201a0 69 6c 65 20 73 79 73 74 65 6d 20 64 6f 65 73 20 ile system does
201b0 6e 6f 74 20 61 6c 77 61 79 73 20 70 72 6f 76 69 not always provi
201c0 64 65 20 70 6f 77 65 72 73 61 66 65 20 6f 76 65 de powersafe ove
201d0 72 77 72 69 74 65 73 2e 20 20 28 49 6e 20 6f 74 rwrites. (In ot
201e0 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 61 66 her.** words, af
201f0 74 65 72 20 61 20 70 6f 77 65 72 2d 6c 6f 73 73 ter a power-loss
20200 20 65 76 65 6e 74 2c 20 70 61 72 74 73 20 6f 66 event, parts of
20210 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 20 77 the file that w
20220 65 72 65 20 6e 65 76 65 72 0a 2a 2a 20 77 72 69 ere never.** wri
20230 74 74 65 6e 20 6d 69 67 68 74 20 65 6e 64 20 75 tten might end u
20240 70 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2e p being altered.
20250 29 20 20 48 6f 77 65 76 65 72 2c 20 6e 6f 6e 2d ) However, non-
20260 50 53 4f 57 20 62 65 68 61 76 69 6f 72 20 69 73 PSOW behavior is
20270 20 76 65 72 79 2c 0a 2a 2a 20 76 65 72 79 20 72 very,.** very r
20280 61 72 65 2e 20 20 41 6e 64 20 61 73 73 65 72 74 are. And assert
20290 69 6e 67 20 50 53 4f 57 20 6d 61 6b 65 73 20 61 ing PSOW makes a
202a0 20 6c 61 72 67 65 20 72 65 64 75 63 74 69 6f 6e large reduction
202b0 20 69 6e 20 74 68 65 20 61 6d 6f 75 6e 74 0a 2a in the amount.*
202c0 2a 20 6f 66 20 72 65 71 75 69 72 65 64 20 49 2f * of required I/
202d0 4f 20 66 6f 72 20 6a 6f 75 72 6e 61 6c 69 6e 67 O for journaling
202e0 2c 20 73 69 6e 63 65 20 61 20 6c 6f 74 20 6f 66 , since a lot of
202f0 20 70 61 64 64 69 6e 67 20 69 73 20 65 6c 69 6d padding is elim
20300 69 6e 61 74 65 64 2e 0a 2a 2a 20 20 48 65 6e 63 inated..** Henc
20310 65 2c 20 77 68 69 6c 65 20 50 4f 57 45 52 53 41 e, while POWERSA
20320 46 45 5f 4f 56 45 52 57 52 49 54 45 20 69 73 20 FE_OVERWRITE is
20330 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 2c 20 74 on by default, t
20340 68 65 72 65 20 69 73 20 61 20 66 69 6c 65 2d 63 here is a file-c
20350 6f 6e 74 72 6f 6c 0a 2a 2a 20 61 76 61 69 6c 61 ontrol.** availa
20360 62 6c 65 20 74 6f 20 74 75 72 6e 20 69 74 20 6f ble to turn it o
20370 66 66 20 61 6e 64 20 55 52 49 20 71 75 65 72 79 ff and URI query
20380 20 70 61 72 61 6d 65 74 65 72 20 61 76 61 69 6c parameter avail
20390 61 62 6c 65 20 74 6f 20 74 75 72 6e 20 69 74 20 able to turn it
203a0 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 off..*/.static i
203b0 6e 74 20 75 6e 69 78 44 65 76 69 63 65 43 68 61 nt unixDeviceCha
203c0 72 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c racteristics(sql
203d0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a ite3_file *id){.
203e0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 20 3d 20 unixFile *p =
203f0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
20400 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 23 69 66 int rc = 0;.#if
20410 64 65 66 20 5f 5f 51 4e 58 4e 54 4f 5f 5f 0a 20 def __QNXNTO__.
20420 20 69 66 28 20 70 2d 3e 73 65 63 74 6f 72 53 69 if( p->sectorSi
20430 7a 65 3d 3d 30 20 29 20 75 6e 69 78 53 65 63 74 ze==0 ) unixSect
20440 6f 72 53 69 7a 65 28 69 64 29 3b 0a 20 20 72 63 orSize(id);. rc
20450 20 3d 20 70 2d 3e 64 65 76 69 63 65 43 68 61 72 = p->deviceChar
20460 61 63 74 65 72 69 73 74 69 63 73 3b 0a 23 65 6e acteristics;.#en
20470 64 69 66 0a 20 20 69 66 28 20 70 2d 3e 63 74 72 dif. if( p->ctr
20480 6c 46 6c 61 67 73 20 26 20 55 4e 49 58 46 49 4c lFlags & UNIXFIL
20490 45 5f 50 53 4f 57 20 29 7b 0a 20 20 20 20 72 63 E_PSOW ){. rc
204a0 20 7c 3d 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 |= SQLITE_IOCAP
204b0 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57 _POWERSAFE_OVERW
204c0 52 49 54 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 RITE;. }. retu
204d0 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 64 rn rc;.}..#if !d
204e0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
204f0 49 54 5f 57 41 4c 29 20 7c 7c 20 53 51 4c 49 54 IT_WAL) || SQLIT
20500 45 5f 4d 41 58 5f 4d 4d 41 50 5f 53 49 5a 45 3e E_MAX_MMAP_SIZE>
20510 30 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 0../*.** Return
20520 74 68 65 20 73 79 73 74 65 6d 20 70 61 67 65 20 the system page
20530 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 size..**.** This
20540 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 function should
20550 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 64 not be called d
20560 69 72 65 63 74 6c 79 20 62 79 20 6f 74 68 65 72 irectly by other
20570 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 code in this fi
20580 6c 65 2e 20 0a 2a 2a 20 49 6e 73 74 65 61 64 2c le. .** Instead,
20590 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20 63 61 it should be ca
205a0 6c 6c 65 64 20 76 69 61 20 6d 61 63 72 6f 20 6f lled via macro o
205b0 73 47 65 74 70 61 67 65 73 69 7a 65 28 29 2e 0a sGetpagesize()..
205c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
205d0 69 78 47 65 74 70 61 67 65 73 69 7a 65 28 76 6f ixGetpagesize(vo
205e0 69 64 29 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f id){.#if OS_VXWO
205f0 52 4b 53 0a 20 20 72 65 74 75 72 6e 20 31 30 32 RKS. return 102
20600 34 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 4;.#elif defined
20610 28 5f 42 53 44 5f 53 4f 55 52 43 45 29 0a 20 20 (_BSD_SOURCE).
20620 72 65 74 75 72 6e 20 67 65 74 70 61 67 65 73 69 return getpagesi
20630 7a 65 28 29 3b 0a 23 65 6c 73 65 0a 20 20 72 65 ze();.#else. re
20640 74 75 72 6e 20 28 69 6e 74 29 73 79 73 63 6f 6e turn (int)syscon
20650 66 28 5f 53 43 5f 50 41 47 45 53 49 5a 45 29 3b f(_SC_PAGESIZE);
20660 0a 23 65 6e 64 69 66 0a 7d 0a 0a 23 65 6e 64 69 .#endif.}..#endi
20670 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 f /* !defined(SQ
20680 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 29 20 7c LITE_OMIT_WAL) |
20690 7c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4d 4d 41 | SQLITE_MAX_MMA
206a0 50 5f 53 49 5a 45 3e 30 20 2a 2f 0a 0a 23 69 66 P_SIZE>0 */..#if
206b0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
206c0 5f 57 41 4c 0a 0a 2f 2a 0a 2a 2a 20 4f 62 6a 65 _WAL../*.** Obje
206d0 63 74 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 ct used to repre
206e0 73 65 6e 74 20 61 6e 20 73 68 61 72 65 64 20 6d sent an shared m
206f0 65 6d 6f 72 79 20 62 75 66 66 65 72 2e 20 20 0a emory buffer. .
20700 2a 2a 0a 2a 2a 20 57 68 65 6e 20 6d 75 6c 74 69 **.** When multi
20710 70 6c 65 20 74 68 72 65 61 64 73 20 61 6c 6c 20 ple threads all
20720 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 73 61 reference the sa
20730 6d 65 20 77 61 6c 2d 69 6e 64 65 78 2c 20 65 61 me wal-index, ea
20740 63 68 20 74 68 72 65 61 64 0a 2a 2a 20 68 61 73 ch thread.** has
20750 20 69 74 73 20 6f 77 6e 20 75 6e 69 78 53 68 6d its own unixShm
20760 20 6f 62 6a 65 63 74 2c 20 62 75 74 20 74 68 65 object, but the
20770 79 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20 61 y all point to a
20780 20 73 69 6e 67 6c 65 20 69 6e 73 74 61 6e 63 65 single instance
20790 0a 2a 2a 20 6f 66 20 74 68 69 73 20 75 6e 69 78 .** of this unix
207a0 53 68 6d 4e 6f 64 65 20 6f 62 6a 65 63 74 2e 20 ShmNode object.
207b0 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
207c0 20 65 61 63 68 20 77 61 6c 2d 69 6e 64 65 78 20 each wal-index
207d0 69 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e 6c is opened.** onl
207e0 79 20 6f 6e 63 65 20 70 65 72 20 70 72 6f 63 65 y once per proce
207f0 73 73 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 75 ss..**.** Each u
20800 6e 69 78 53 68 6d 4e 6f 64 65 20 6f 62 6a 65 63 nixShmNode objec
20810 74 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 74 t is connected t
20820 6f 20 61 20 73 69 6e 67 6c 65 20 75 6e 69 78 49 o a single unixI
20830 6e 6f 64 65 49 6e 66 6f 20 6f 62 6a 65 63 74 2e nodeInfo object.
20840 0a 2a 2a 20 57 65 20 63 6f 75 6c 64 20 63 6f 61 .** We could coa
20850 6c 65 73 63 65 20 74 68 69 73 20 6f 62 6a 65 63 lesce this objec
20860 74 20 69 6e 74 6f 20 75 6e 69 78 49 6e 6f 64 65 t into unixInode
20870 49 6e 66 6f 2c 20 62 75 74 20 74 68 61 74 20 77 Info, but that w
20880 6f 75 6c 64 20 6d 65 61 6e 0a 2a 2a 20 65 76 65 ould mean.** eve
20890 72 79 20 6f 70 65 6e 20 66 69 6c 65 20 74 68 61 ry open file tha
208a0 74 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 73 t does not use s
208b0 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 28 69 6e hared memory (in
208c0 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 6d 6f other words, mo
208d0 73 74 0a 2a 2a 20 6f 70 65 6e 20 66 69 6c 65 73 st.** open files
208e0 29 20 77 6f 75 6c 64 20 68 61 76 65 20 74 6f 20 ) would have to
208f0 63 61 72 72 79 20 61 72 6f 75 6e 64 20 74 68 69 carry around thi
20900 73 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 s extra informat
20910 69 6f 6e 2e 20 20 53 6f 0a 2a 2a 20 74 68 65 20 ion. So.** the
20920 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 6f 62 unixInodeInfo ob
20930 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 ject contains a
20940 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 pointer to this
20950 75 6e 69 78 53 68 6d 4e 6f 64 65 20 6f 62 6a 65 unixShmNode obje
20960 63 74 0a 2a 2a 20 61 6e 64 20 74 68 65 20 75 6e ct.** and the un
20970 69 78 53 68 6d 4e 6f 64 65 20 6f 62 6a 65 63 74 ixShmNode object
20980 20 69 73 20 63 72 65 61 74 65 64 20 6f 6e 6c 79 is created only
20990 20 77 68 65 6e 20 6e 65 65 64 65 64 2e 0a 2a 2a when needed..**
209a0 0a 2a 2a 20 75 6e 69 78 4d 75 74 65 78 48 65 6c .** unixMutexHel
209b0 64 28 29 20 6d 75 73 74 20 62 65 20 74 72 75 65 d() must be true
209c0 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20 6f when creating o
209d0 72 20 64 65 73 74 72 6f 79 69 6e 67 0a 2a 2a 20 r destroying.**
209e0 74 68 69 73 20 6f 62 6a 65 63 74 20 6f 72 20 77 this object or w
209f0 68 69 6c 65 20 72 65 61 64 69 6e 67 20 6f 72 20 hile reading or
20a00 77 72 69 74 69 6e 67 20 74 68 65 20 66 6f 6c 6c writing the foll
20a10 6f 77 69 6e 67 20 66 69 65 6c 64 73 3a 0a 2a 2a owing fields:.**
20a20 0a 2a 2a 20 20 20 20 20 20 6e 52 65 66 0a 2a 2a .** nRef.**
20a30 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
20a40 67 20 66 69 65 6c 64 73 20 61 72 65 20 72 65 61 g fields are rea
20a50 64 2d 6f 6e 6c 79 20 61 66 74 65 72 20 74 68 65 d-only after the
20a60 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74 object is creat
20a70 65 64 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 20 ed:.** .**
20a80 66 69 64 0a 2a 2a 20 20 20 20 20 20 7a 46 69 6c fid.** zFil
20a90 65 6e 61 6d 65 0a 2a 2a 0a 2a 2a 20 45 69 74 68 ename.**.** Eith
20aa0 65 72 20 75 6e 69 78 53 68 6d 4e 6f 64 65 2e 6d er unixShmNode.m
20ab0 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c utex must be hel
20ac0 64 20 6f 72 20 75 6e 69 78 53 68 6d 4e 6f 64 65 d or unixShmNode
20ad0 2e 6e 52 65 66 3d 3d 30 20 61 6e 64 0a 2a 2a 20 .nRef==0 and.**
20ae0 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28 29 20 unixMutexHeld()
20af0 69 73 20 74 72 75 65 20 77 68 65 6e 20 72 65 61 is true when rea
20b00 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 ding or writing
20b10 61 6e 79 20 6f 74 68 65 72 20 66 69 65 6c 64 0a any other field.
20b20 2a 2a 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 ** in this struc
20b30 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ture..*/.struct
20b40 75 6e 69 78 53 68 6d 4e 6f 64 65 20 7b 0a 20 20 unixShmNode {.
20b50 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 unixInodeInfo *p
20b60 49 6e 6f 64 65 3b 20 20 20 20 20 2f 2a 20 75 6e Inode; /* un
20b70 69 78 49 6e 6f 64 65 49 6e 66 6f 20 74 68 61 74 ixInodeInfo that
20b80 20 6f 77 6e 73 20 74 68 69 73 20 53 48 4d 20 6e owns this SHM n
20b90 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ode */. sqlite3
20ba0 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 _mutex *mutex;
20bb0 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 6f 20 /* Mutex to
20bc0 61 63 63 65 73 73 20 74 68 69 73 20 6f 62 6a 65 access this obje
20bd0 63 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 ct */. char *zF
20be0 69 6c 65 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 ilename;
20bf0 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
20c00 65 20 6d 6d 61 70 70 65 64 20 66 69 6c 65 20 2a e mmapped file *
20c10 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 /. int h;
20c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
20c30 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 * Open file desc
20c40 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 riptor */. int
20c50 73 7a 52 65 67 69 6f 6e 3b 20 20 20 20 20 20 20 szRegion;
20c60 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
20c70 66 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 f shared-memory
20c80 72 65 67 69 6f 6e 73 20 2a 2f 0a 20 20 75 31 36 regions */. u16
20c90 20 6e 52 65 67 69 6f 6e 3b 20 20 20 20 20 20 20 nRegion;
20ca0 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
20cb0 6f 66 20 61 72 72 61 79 20 61 70 52 65 67 69 6f of array apRegio
20cc0 6e 20 2a 2f 0a 20 20 75 38 20 69 73 52 65 61 64 n */. u8 isRead
20cd0 6f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20 only;
20ce0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65 61 /* True if rea
20cf0 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 63 68 61 72 d-only */. char
20d00 20 2a 2a 61 70 52 65 67 69 6f 6e 3b 20 20 20 20 **apRegion;
20d10 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 /* Array
20d20 6f 66 20 6d 61 70 70 65 64 20 73 68 61 72 65 64 of mapped shared
20d30 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e 73 20 -memory regions
20d40 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 */. int nRef;
20d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d60 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 6e 69 /* Number of uni
20d70 78 53 68 6d 20 6f 62 6a 65 63 74 73 20 70 6f 69 xShm objects poi
20d80 6e 74 69 6e 67 20 74 6f 20 74 68 69 73 20 2a 2f nting to this */
20d90 0a 20 20 75 6e 69 78 53 68 6d 20 2a 70 46 69 72 . unixShm *pFir
20da0 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a st; /*
20db0 20 41 6c 6c 20 75 6e 69 78 53 68 6d 20 6f 62 6a All unixShm obj
20dc0 65 63 74 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f ects pointing to
20dd0 20 74 68 69 73 20 2a 2f 0a 23 69 66 64 65 66 20 this */.#ifdef
20de0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 75 SQLITE_DEBUG. u
20df0 38 20 65 78 63 6c 4d 61 73 6b 3b 20 20 20 20 20 8 exclMask;
20e00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 /* Mas
20e10 6b 20 6f 66 20 65 78 63 6c 75 73 69 76 65 20 6c k of exclusive l
20e20 6f 63 6b 73 20 68 65 6c 64 20 2a 2f 0a 20 20 75 ocks held */. u
20e30 38 20 73 68 61 72 65 64 4d 61 73 6b 3b 20 20 20 8 sharedMask;
20e40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 /* Mas
20e50 6b 20 6f 66 20 73 68 61 72 65 64 20 6c 6f 63 6b k of shared lock
20e60 73 20 68 65 6c 64 20 2a 2f 0a 20 20 75 38 20 6e s held */. u8 n
20e70 65 78 74 53 68 6d 49 64 3b 20 20 20 20 20 20 20 extShmId;
20e80 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 61 /* Next a
20e90 76 61 69 6c 61 62 6c 65 20 75 6e 69 78 53 68 6d vailable unixShm
20ea0 2e 69 64 20 76 61 6c 75 65 20 2a 2f 0a 23 65 6e .id value */.#en
20eb0 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74 dif.};../*.** St
20ec0 72 75 63 74 75 72 65 20 75 73 65 64 20 69 6e 74 ructure used int
20ed0 65 72 6e 61 6c 6c 79 20 62 79 20 74 68 69 73 20 ernally by this
20ee0 56 46 53 20 74 6f 20 72 65 63 6f 72 64 20 74 68 VFS to record th
20ef0 65 20 73 74 61 74 65 20 6f 66 20 61 6e 0a 2a 2a e state of an.**
20f00 20 6f 70 65 6e 20 73 68 61 72 65 64 20 6d 65 6d open shared mem
20f10 6f 72 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a ory connection..
20f20 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 **.** The follow
20f30 69 6e 67 20 66 69 65 6c 64 73 20 61 72 65 20 69 ing fields are i
20f40 6e 69 74 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 nitialized when
20f50 74 68 69 73 20 6f 62 6a 65 63 74 20 69 73 20 63 this object is c
20f60 72 65 61 74 65 64 20 61 6e 64 0a 2a 2a 20 61 72 reated and.** ar
20f70 65 20 72 65 61 64 2d 6f 6e 6c 79 20 74 68 65 72 e read-only ther
20f80 65 61 66 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 eafter:.**.**
20f90 20 75 6e 69 78 53 68 6d 2e 70 46 69 6c 65 0a 2a unixShm.pFile.*
20fa0 2a 20 20 20 20 75 6e 69 78 53 68 6d 2e 69 64 0a * unixShm.id.
20fb0 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 **.** All other
20fc0 66 69 65 6c 64 73 20 61 72 65 20 72 65 61 64 2f fields are read/
20fd0 77 72 69 74 65 2e 20 20 54 68 65 20 75 6e 69 78 write. The unix
20fe0 53 68 6d 2e 70 46 69 6c 65 2d 3e 6d 75 74 65 78 Shm.pFile->mutex
20ff0 20 6d 75 73 74 20 62 65 20 68 65 6c 64 0a 2a 2a must be held.**
21000 20 77 68 69 6c 65 20 61 63 63 65 73 73 69 6e 67 while accessing
21010 20 61 6e 79 20 72 65 61 64 2f 77 72 69 74 65 20 any read/write
21020 66 69 65 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 fields..*/.struc
21030 74 20 75 6e 69 78 53 68 6d 20 7b 0a 20 20 75 6e t unixShm {. un
21040 69 78 53 68 6d 4e 6f 64 65 20 2a 70 53 68 6d 4e ixShmNode *pShmN
21050 6f 64 65 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 ode; /* The
21060 75 6e 64 65 72 6c 79 69 6e 67 20 75 6e 69 78 53 underlying unixS
21070 68 6d 4e 6f 64 65 20 6f 62 6a 65 63 74 20 2a 2f hmNode object */
21080 0a 20 20 75 6e 69 78 53 68 6d 20 2a 70 4e 65 78 . unixShm *pNex
21090 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
210a0 20 4e 65 78 74 20 75 6e 69 78 53 68 6d 20 77 69 Next unixShm wi
210b0 74 68 20 74 68 65 20 73 61 6d 65 20 75 6e 69 78 th the same unix
210c0 53 68 6d 4e 6f 64 65 20 2a 2f 0a 20 20 75 38 20 ShmNode */. u8
210d0 68 61 73 4d 75 74 65 78 3b 20 20 20 20 20 20 20 hasMutex;
210e0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
210f0 69 66 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 75 if holding the u
21100 6e 69 78 53 68 6d 4e 6f 64 65 20 6d 75 74 65 78 nixShmNode mutex
21110 20 2a 2f 0a 20 20 75 38 20 69 64 3b 20 20 20 20 */. u8 id;
21120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21130 20 2f 2a 20 49 64 20 6f 66 20 74 68 69 73 20 63 /* Id of this c
21140 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 69 6e onnection within
21150 20 69 74 73 20 75 6e 69 78 53 68 6d 4e 6f 64 65 its unixShmNode
21160 20 2a 2f 0a 20 20 75 31 36 20 73 68 61 72 65 64 */. u16 shared
21170 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 Mask;
21180 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 73 68 61 72 /* Mask of shar
21190 65 64 20 6c 6f 63 6b 73 20 68 65 6c 64 20 2a 2f ed locks held */
211a0 0a 20 20 75 31 36 20 65 78 63 6c 4d 61 73 6b 3b . u16 exclMask;
211b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
211c0 20 4d 61 73 6b 20 6f 66 20 65 78 63 6c 75 73 69 Mask of exclusi
211d0 76 65 20 6c 6f 63 6b 73 20 68 65 6c 64 20 2a 2f ve locks held */
211e0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 .};../*.** Const
211f0 61 6e 74 73 20 75 73 65 64 20 66 6f 72 20 6c 6f ants used for lo
21200 63 6b 69 6e 67 0a 2a 2f 0a 23 64 65 66 69 6e 65 cking.*/.#define
21210 20 55 4e 49 58 5f 53 48 4d 5f 42 41 53 45 20 20 UNIX_SHM_BASE
21220 20 28 28 32 32 2b 53 51 4c 49 54 45 5f 53 48 4d ((22+SQLITE_SHM
21230 5f 4e 4c 4f 43 4b 29 2a 34 29 20 20 20 20 20 20 _NLOCK)*4)
21240 20 20 20 2f 2a 20 66 69 72 73 74 20 6c 6f 63 6b /* first lock
21250 20 62 79 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65 byte */.#define
21260 20 55 4e 49 58 5f 53 48 4d 5f 44 4d 53 20 20 20 UNIX_SHM_DMS
21270 20 28 55 4e 49 58 5f 53 48 4d 5f 42 41 53 45 2b (UNIX_SHM_BASE+
21280 53 51 4c 49 54 45 5f 53 48 4d 5f 4e 4c 4f 43 4b SQLITE_SHM_NLOCK
21290 29 20 20 2f 2a 20 64 65 61 64 6d 61 6e 20 73 77 ) /* deadman sw
212a0 69 74 63 68 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 itch */../*.** A
212b0 70 70 6c 79 20 70 6f 73 69 78 20 61 64 76 69 73 pply posix advis
212c0 6f 72 79 20 6c 6f 63 6b 73 20 66 6f 72 20 61 6c ory locks for al
212d0 6c 20 62 79 74 65 73 20 66 72 6f 6d 20 6f 66 73 l bytes from ofs
212e0 74 20 74 68 72 6f 75 67 68 20 6f 66 73 74 2b 6e t through ofst+n
212f0 2d 31 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 73 20 -1..**.** Locks
21300 62 6c 6f 63 6b 20 69 66 20 74 68 65 20 6d 61 73 block if the mas
21310 6b 20 69 73 20 65 78 61 63 74 6c 79 20 55 4e 49 k is exactly UNI
21320 58 5f 53 48 4d 5f 43 20 61 6e 64 20 61 72 65 20 X_SHM_C and are
21330 6e 6f 6e 2d 62 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 non-blocking.**
21340 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 otherwise..*/.st
21350 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53 68 6d atic int unixShm
21360 53 79 73 74 65 6d 4c 6f 63 6b 28 0a 20 20 75 6e SystemLock(. un
21370 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 ixFile *pFile,
21380 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 63 6f 6e /* Open con
21390 6e 65 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 57 nection to the W
213a0 41 4c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 AL file */. int
213b0 20 6c 6f 63 6b 54 79 70 65 2c 20 20 20 20 20 20 lockType,
213c0 20 20 20 20 2f 2a 20 46 5f 55 4e 4c 43 4b 2c 20 /* F_UNLCK,
213d0 46 5f 52 44 4c 43 4b 2c 20 6f 72 20 46 5f 57 52 F_RDLCK, or F_WR
213e0 4c 43 4b 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 73 LCK */. int ofs
213f0 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t,
21400 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 /* First byte of
21410 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e the locking ran
21420 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 20 20 20 ge */. int n
21430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
21440 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
21450 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a s to lock */.){.
21460 20 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20 2a 70 unixShmNode *p
21470 53 68 6d 4e 6f 64 65 3b 20 2f 2a 20 41 70 70 6c ShmNode; /* Appl
21480 79 20 6c 6f 63 6b 73 20 74 6f 20 74 68 69 73 20 y locks to this
21490 6f 70 65 6e 20 73 68 61 72 65 64 2d 6d 65 6d 6f open shared-memo
214a0 72 79 20 73 65 67 6d 65 6e 74 20 2a 2f 0a 20 20 ry segment */.
214b0 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 66 3b 20 struct flock f;
214c0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 6f /* The po
214d0 73 69 78 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 six advisory loc
214e0 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 2a king structure *
214f0 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c /. int rc = SQL
21500 49 54 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20 52 65 ITE_OK; /* Re
21510 73 75 6c 74 20 63 6f 64 65 20 66 6f 72 6d 20 66 sult code form f
21520 63 6e 74 6c 28 29 20 2a 2f 0a 0a 20 20 2f 2a 20 cntl() */.. /*
21530 41 63 63 65 73 73 20 74 6f 20 74 68 65 20 75 6e Access to the un
21540 69 78 53 68 6d 4e 6f 64 65 20 6f 62 6a 65 63 74 ixShmNode object
21550 20 69 73 20 73 65 72 69 61 6c 69 7a 65 64 20 62 is serialized b
21560 79 20 74 68 65 20 63 61 6c 6c 65 72 20 2a 2f 0a y the caller */.
21570 20 20 70 53 68 6d 4e 6f 64 65 20 3d 20 70 46 69 pShmNode = pFi
21580 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 70 53 68 6d le->pInode->pShm
21590 4e 6f 64 65 3b 0a 20 20 61 73 73 65 72 74 28 20 Node;. assert(
215a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
215b0 6c 64 28 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 ld(pShmNode->mut
215c0 65 78 29 20 7c 7c 20 70 53 68 6d 4e 6f 64 65 2d ex) || pShmNode-
215d0 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 0a 20 20 2f >nRef==0 );.. /
215e0 2a 20 53 68 61 72 65 64 20 6c 6f 63 6b 73 20 6e * Shared locks n
215f0 65 76 65 72 20 73 70 61 6e 20 6d 6f 72 65 20 74 ever span more t
21600 68 61 6e 20 6f 6e 65 20 62 79 74 65 20 2a 2f 0a han one byte */.
21610 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 31 20 7c assert( n==1 |
21620 7c 20 6c 6f 63 6b 54 79 70 65 21 3d 46 5f 52 44 | lockType!=F_RD
21630 4c 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 LCK );.. /* Loc
21640 6b 73 20 61 72 65 20 77 69 74 68 69 6e 20 72 61 ks are within ra
21650 6e 67 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 nge */. assert(
21660 20 6e 3e 3d 31 20 26 26 20 6e 3c 3d 53 51 4c 49 n>=1 && n<=SQLI
21670 54 45 5f 53 48 4d 5f 4e 4c 4f 43 4b 20 29 3b 0a TE_SHM_NLOCK );.
21680 0a 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d . if( pShmNode-
21690 3e 68 3e 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 >h>=0 ){. /*
216a0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6c Initialize the l
216b0 6f 63 6b 69 6e 67 20 70 61 72 61 6d 65 74 65 72 ocking parameter
216c0 73 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 s */. memset(
216d0 26 66 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 29 &f, 0, sizeof(f)
216e0 29 3b 0a 20 20 20 20 66 2e 6c 5f 74 79 70 65 20 );. f.l_type
216f0 3d 20 6c 6f 63 6b 54 79 70 65 3b 0a 20 20 20 20 = lockType;.
21700 66 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 f.l_whence = SEE
21710 4b 5f 53 45 54 3b 0a 20 20 20 20 66 2e 6c 5f 73 K_SET;. f.l_s
21720 74 61 72 74 20 3d 20 6f 66 73 74 3b 0a 20 20 20 tart = ofst;.
21730 20 66 2e 6c 5f 6c 65 6e 20 3d 20 6e 3b 0a 0a 20 f.l_len = n;..
21740 20 20 20 72 63 20 3d 20 6f 73 46 63 6e 74 6c 28 rc = osFcntl(
21750 70 53 68 6d 4e 6f 64 65 2d 3e 68 2c 20 46 5f 53 pShmNode->h, F_S
21760 45 54 4c 4b 2c 20 26 66 29 3b 0a 20 20 20 20 72 ETLK, &f);. r
21770 63 20 3d 20 28 72 63 21 3d 28 2d 31 29 29 20 3f c = (rc!=(-1)) ?
21780 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c SQLITE_OK : SQL
21790 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 0a 20 ITE_BUSY;. }..
217a0 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 67 /* Update the g
217b0 6c 6f 62 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 lobal lock state
217c0 20 61 6e 64 20 64 6f 20 64 65 62 75 67 20 74 72 and do debug tr
217d0 61 63 69 6e 67 20 2a 2f 0a 23 69 66 64 65 66 20 acing */.#ifdef
217e0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 7b SQLITE_DEBUG. {
217f0 20 75 31 36 20 6d 61 73 6b 3b 0a 20 20 4f 53 54 u16 mask;. OST
21800 52 41 43 45 28 28 22 53 48 4d 2d 4c 4f 43 4b 20 RACE(("SHM-LOCK
21810 22 29 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 6f 66 "));. mask = of
21820 73 74 3e 33 31 20 3f 20 30 78 66 66 66 66 20 3a st>31 ? 0xffff :
21830 20 28 31 3c 3c 28 6f 66 73 74 2b 6e 29 29 20 2d (1<<(ofst+n)) -
21840 20 28 31 3c 3c 6f 66 73 74 29 3b 0a 20 20 69 66 (1<<ofst);. if
21850 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
21860 29 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 54 ){. if( lockT
21870 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a ype==F_UNLCK ){.
21880 20 20 20 20 20 20 4f 53 54 52 41 43 45 28 28 22 OSTRACE(("
21890 75 6e 6c 6f 63 6b 20 25 64 20 6f 6b 22 2c 20 6f unlock %d ok", o
218a0 66 73 74 29 29 3b 0a 20 20 20 20 20 20 70 53 68 fst));. pSh
218b0 6d 4e 6f 64 65 2d 3e 65 78 63 6c 4d 61 73 6b 20 mNode->exclMask
218c0 26 3d 20 7e 6d 61 73 6b 3b 0a 20 20 20 20 20 20 &= ~mask;.
218d0 70 53 68 6d 4e 6f 64 65 2d 3e 73 68 61 72 65 64 pShmNode->shared
218e0 4d 61 73 6b 20 26 3d 20 7e 6d 61 73 6b 3b 0a 20 Mask &= ~mask;.
218f0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 }else if( loc
21900 6b 54 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 29 kType==F_RDLCK )
21910 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 28 {. OSTRACE(
21920 28 22 72 65 61 64 2d 6c 6f 63 6b 20 25 64 20 6f ("read-lock %d o
21930 6b 22 2c 20 6f 66 73 74 29 29 3b 0a 20 20 20 20 k", ofst));.
21940 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 65 78 63 6c pShmNode->excl
21950 4d 61 73 6b 20 26 3d 20 7e 6d 61 73 6b 3b 0a 20 Mask &= ~mask;.
21960 20 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 73 pShmNode->s
21970 68 61 72 65 64 4d 61 73 6b 20 7c 3d 20 6d 61 73 haredMask |= mas
21980 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 k;. }else{.
21990 20 20 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b assert( lock
219a0 54 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 3b Type==F_WRLCK );
219b0 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 28 28 . OSTRACE((
219c0 22 77 72 69 74 65 2d 6c 6f 63 6b 20 25 64 20 6f "write-lock %d o
219d0 6b 22 2c 20 6f 66 73 74 29 29 3b 0a 20 20 20 20 k", ofst));.
219e0 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 65 78 63 6c pShmNode->excl
219f0 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 Mask |= mask;.
21a00 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 73 68 pShmNode->sh
21a10 61 72 65 64 4d 61 73 6b 20 26 3d 20 7e 6d 61 73 aredMask &= ~mas
21a20 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 k;. }. }else
21a30 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 54 79 {. if( lockTy
21a40 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 pe==F_UNLCK ){.
21a50 20 20 20 20 20 4f 53 54 52 41 43 45 28 28 22 75 OSTRACE(("u
21a60 6e 6c 6f 63 6b 20 25 64 20 66 61 69 6c 65 64 22 nlock %d failed"
21a70 2c 20 6f 66 73 74 29 29 3b 0a 20 20 20 20 7d 65 , ofst));. }e
21a80 6c 73 65 20 69 66 28 20 6c 6f 63 6b 54 79 70 65 lse if( lockType
21a90 3d 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 ==F_RDLCK ){.
21aa0 20 20 20 4f 53 54 52 41 43 45 28 28 22 72 65 61 OSTRACE(("rea
21ab0 64 2d 6c 6f 63 6b 20 66 61 69 6c 65 64 22 29 29 d-lock failed"))
21ac0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
21ad0 20 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 54 assert( lockT
21ae0 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 3b 0a ype==F_WRLCK );.
21af0 20 20 20 20 20 20 4f 53 54 52 41 43 45 28 28 22 OSTRACE(("
21b00 77 72 69 74 65 2d 6c 6f 63 6b 20 25 64 20 66 61 write-lock %d fa
21b10 69 6c 65 64 22 2c 20 6f 66 73 74 29 29 3b 0a 20 iled", ofst));.
21b20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41 }. }. OSTRA
21b30 43 45 28 28 22 20 2d 20 61 66 74 65 72 77 61 72 CE((" - afterwar
21b40 64 73 20 25 30 33 78 2c 25 30 33 78 5c 6e 22 2c ds %03x,%03x\n",
21b50 0a 20 20 20 20 20 20 20 20 20 20 20 70 53 68 6d . pShm
21b60 4e 6f 64 65 2d 3e 73 68 61 72 65 64 4d 61 73 6b Node->sharedMask
21b70 2c 20 70 53 68 6d 4e 6f 64 65 2d 3e 65 78 63 6c , pShmNode->excl
21b80 4d 61 73 6b 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 Mask));. }.#end
21b90 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b if.. return rc;
21ba0 20 20 20 20 20 20 20 20 0a 7d 0a 0a 2f 2a 0a 2a .}../*.*
21bb0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d 69 6e * Return the min
21bc0 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 33 imum number of 3
21bd0 32 4b 42 20 73 68 6d 20 72 65 67 69 6f 6e 73 20 2KB shm regions
21be0 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 6d that should be m
21bf0 61 70 70 65 64 20 61 74 0a 2a 2a 20 61 20 74 69 apped at.** a ti
21c00 6d 65 2c 20 61 73 73 75 6d 69 6e 67 20 74 68 61 me, assuming tha
21c10 74 20 65 61 63 68 20 6d 61 70 70 69 6e 67 20 6d t each mapping m
21c20 75 73 74 20 62 65 20 61 6e 20 69 6e 74 65 67 65 ust be an intege
21c30 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 74 68 r multiple of th
21c40 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 73 79 73 e.** current sys
21c50 74 65 6d 20 70 61 67 65 2d 73 69 7a 65 2e 0a 2a tem page-size..*
21c60 2a 0a 2a 2a 20 55 73 75 61 6c 6c 79 2c 20 74 68 *.** Usually, th
21c70 69 73 20 69 73 20 31 2e 20 54 68 65 20 65 78 63 is is 1. The exc
21c80 65 70 74 69 6f 6e 20 73 65 65 6d 73 20 74 6f 20 eption seems to
21c90 62 65 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 be systems that
21ca0 61 72 65 20 63 6f 6e 66 69 67 75 72 65 64 0a 2a are configured.*
21cb0 2a 20 74 6f 20 75 73 65 20 36 34 4b 42 20 70 61 * to use 64KB pa
21cc0 67 65 73 20 2d 20 69 6e 20 74 68 69 73 20 63 61 ges - in this ca
21cd0 73 65 20 65 61 63 68 20 6d 61 70 70 69 6e 67 20 se each mapping
21ce0 6d 75 73 74 20 63 6f 76 65 72 20 61 74 20 6c 65 must cover at le
21cf0 61 73 74 20 74 77 6f 0a 2a 2a 20 73 68 6d 20 72 ast two.** shm r
21d00 65 67 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 egions..*/.stati
21d10 63 20 69 6e 74 20 75 6e 69 78 53 68 6d 52 65 67 c int unixShmReg
21d20 69 6f 6e 50 65 72 4d 61 70 28 76 6f 69 64 29 7b ionPerMap(void){
21d30 0a 20 20 69 6e 74 20 73 68 6d 73 7a 20 3d 20 33 . int shmsz = 3
21d40 32 2a 31 30 32 34 3b 20 20 20 20 20 20 20 20 20 2*1024;
21d50 20 20 20 2f 2a 20 53 48 4d 20 72 65 67 69 6f 6e /* SHM region
21d60 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 70 size */. int p
21d70 67 73 7a 20 3d 20 6f 73 47 65 74 70 61 67 65 73 gsz = osGetpages
21d80 69 7a 65 28 29 3b 20 20 20 2f 2a 20 53 79 73 74 ize(); /* Syst
21d90 65 6d 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a em page size */.
21da0 20 20 61 73 73 65 72 74 28 20 28 28 70 67 73 7a assert( ((pgsz
21db0 2d 31 29 26 70 67 73 7a 29 3d 3d 30 20 29 3b 20 -1)&pgsz)==0 );
21dc0 20 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 6d /* Page size m
21dd0 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f ust be a power o
21de0 66 20 32 20 2a 2f 0a 20 20 69 66 28 20 70 67 73 f 2 */. if( pgs
21df0 7a 3c 73 68 6d 73 7a 20 29 20 72 65 74 75 72 6e z<shmsz ) return
21e00 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 70 67 73 1;. return pgs
21e10 7a 2f 73 68 6d 73 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a z/shmsz;.}../*.*
21e20 2a 20 50 75 72 67 65 20 74 68 65 20 75 6e 69 78 * Purge the unix
21e30 53 68 6d 4e 6f 64 65 4c 69 73 74 20 6c 69 73 74 ShmNodeList list
21e40 20 6f 66 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 of all entries
21e50 77 69 74 68 20 75 6e 69 78 53 68 6d 4e 6f 64 65 with unixShmNode
21e60 2e 6e 52 65 66 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 .nRef==0..**.**
21e70 54 68 69 73 20 69 73 20 6e 6f 74 20 61 20 56 46 This is not a VF
21e80 53 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 S shared-memory
21e90 6d 65 74 68 6f 64 3b 20 69 74 20 69 73 20 61 20 method; it is a
21ea0 75 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e utility function
21eb0 20 63 61 6c 6c 65 64 0a 2a 2a 20 62 79 20 56 46 called.** by VF
21ec0 53 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 S shared-memory
21ed0 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 73 74 61 74 methods..*/.stat
21ee0 69 63 20 76 6f 69 64 20 75 6e 69 78 53 68 6d 50 ic void unixShmP
21ef0 75 72 67 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 urge(unixFile *p
21f00 46 64 29 7b 0a 20 20 75 6e 69 78 53 68 6d 4e 6f Fd){. unixShmNo
21f10 64 65 20 2a 70 20 3d 20 70 46 64 2d 3e 70 49 6e de *p = pFd->pIn
21f20 6f 64 65 2d 3e 70 53 68 6d 4e 6f 64 65 3b 0a 20 ode->pShmNode;.
21f30 20 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74 assert( unixMut
21f40 65 78 48 65 6c 64 28 29 20 29 3b 0a 20 20 69 66 exHeld() );. if
21f50 28 20 70 20 26 26 20 41 4c 57 41 59 53 28 70 2d ( p && ALWAYS(p-
21f60 3e 6e 52 65 66 3d 3d 30 29 20 29 7b 0a 20 20 20 >nRef==0) ){.
21f70 20 69 6e 74 20 6e 53 68 6d 50 65 72 4d 61 70 20 int nShmPerMap
21f80 3d 20 75 6e 69 78 53 68 6d 52 65 67 69 6f 6e 50 = unixShmRegionP
21f90 65 72 4d 61 70 28 29 3b 0a 20 20 20 20 69 6e 74 erMap();. int
21fa0 20 69 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 i;. assert(
21fb0 70 2d 3e 70 49 6e 6f 64 65 3d 3d 70 46 64 2d 3e p->pInode==pFd->
21fc0 70 49 6e 6f 64 65 20 29 3b 0a 20 20 20 20 73 71 pInode );. sq
21fd0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 lite3_mutex_free
21fe0 28 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 (p->mutex);.
21ff0 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 52 for(i=0; i<p->nR
22000 65 67 69 6f 6e 3b 20 69 2b 3d 6e 53 68 6d 50 65 egion; i+=nShmPe
22010 72 4d 61 70 29 7b 0a 20 20 20 20 20 20 69 66 28 rMap){. if(
22020 20 70 2d 3e 68 3e 3d 30 20 29 7b 0a 20 20 20 20 p->h>=0 ){.
22030 20 20 20 20 6f 73 4d 75 6e 6d 61 70 28 70 2d 3e osMunmap(p->
22040 61 70 52 65 67 69 6f 6e 5b 69 5d 2c 20 70 2d 3e apRegion[i], p->
22050 73 7a 52 65 67 69 6f 6e 29 3b 0a 20 20 20 20 20 szRegion);.
22060 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
22070 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e sqlite3_free(p->
22080 61 70 52 65 67 69 6f 6e 5b 69 5d 29 3b 0a 20 20 apRegion[i]);.
22090 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
220a0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e sqlite3_free(p->
220b0 61 70 52 65 67 69 6f 6e 29 3b 0a 20 20 20 20 69 apRegion);. i
220c0 66 28 20 70 2d 3e 68 3e 3d 30 20 29 7b 0a 20 20 f( p->h>=0 ){.
220d0 20 20 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 robust_close
220e0 28 70 46 64 2c 20 70 2d 3e 68 2c 20 5f 5f 4c 49 (pFd, p->h, __LI
220f0 4e 45 5f 5f 29 3b 0a 20 20 20 20 20 20 70 2d 3e NE__);. p->
22100 68 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 h = -1;. }.
22110 20 20 70 2d 3e 70 49 6e 6f 64 65 2d 3e 70 53 68 p->pInode->pSh
22120 6d 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 73 mNode = 0;. s
22130 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
22140 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 }.}../*.** Ope
22150 6e 20 61 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 n a shared-memor
22160 79 20 61 72 65 61 20 61 73 73 6f 63 69 61 74 65 y area associate
22170 64 20 77 69 74 68 20 6f 70 65 6e 20 64 61 74 61 d with open data
22180 62 61 73 65 20 66 69 6c 65 20 70 44 62 46 64 2e base file pDbFd.
22190 20 20 0a 2a 2a 20 54 68 69 73 20 70 61 72 74 69 .** This parti
221a0 63 75 6c 61 72 20 69 6d 70 6c 65 6d 65 6e 74 61 cular implementa
221b0 74 69 6f 6e 20 75 73 65 73 20 6d 6d 61 70 70 65 tion uses mmappe
221c0 64 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 d files..**.** T
221d0 68 65 20 66 69 6c 65 20 75 73 65 64 20 74 6f 20 he file used to
221e0 69 6d 70 6c 65 6d 65 6e 74 20 73 68 61 72 65 64 implement shared
221f0 2d 6d 65 6d 6f 72 79 20 69 73 20 69 6e 20 74 68 -memory is in th
22200 65 20 73 61 6d 65 20 64 69 72 65 63 74 6f 72 79 e same directory
22210 0a 2a 2a 20 61 73 20 74 68 65 20 6f 70 65 6e 20 .** as the open
22220 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e database file an
22230 64 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 6e d has the same n
22240 61 6d 65 20 61 73 20 74 68 65 20 6f 70 65 6e 20 ame as the open
22250 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 database.** file
22260 20 77 69 74 68 20 74 68 65 20 22 2d 73 68 6d 22 with the "-shm"
22270 20 73 75 66 66 69 78 20 61 64 64 65 64 2e 20 20 suffix added.
22280 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 For example, if
22290 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
222a0 65 0a 2a 2a 20 69 73 20 22 2f 68 6f 6d 65 2f 75 e.** is "/home/u
222b0 73 65 72 31 2f 63 6f 6e 66 69 67 2e 64 62 22 20 ser1/config.db"
222c0 74 68 65 6e 20 74 68 65 20 66 69 6c 65 20 74 68 then the file th
222d0 61 74 20 69 73 20 63 72 65 61 74 65 64 20 61 6e at is created an
222e0 64 20 6d 6d 61 70 70 65 64 0a 2a 2a 20 66 6f 72 d mmapped.** for
222f0 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 77 shared memory w
22300 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 22 2f ill be called "/
22310 68 6f 6d 65 2f 75 73 65 72 31 2f 63 6f 6e 66 69 home/user1/confi
22320 67 2e 64 62 2d 73 68 6d 22 2e 20 20 0a 2a 2a 0a g.db-shm". .**.
22330 2a 2a 20 41 6e 6f 74 68 65 72 20 61 70 70 72 6f ** Another appro
22340 61 63 68 20 74 6f 20 69 73 20 74 6f 20 75 73 65 ach to is to use
22350 20 66 69 6c 65 73 20 69 6e 20 2f 64 65 76 2f 73 files in /dev/s
22360 68 6d 20 6f 72 20 2f 64 65 76 2f 74 6d 70 20 6f hm or /dev/tmp o
22370 72 20 61 6e 0a 2a 2a 20 73 6f 6d 65 20 6f 74 68 r an.** some oth
22380 65 72 20 74 6d 70 66 73 20 6d 6f 75 6e 74 2e 20 er tmpfs mount.
22390 42 75 74 20 69 66 20 61 20 66 69 6c 65 20 69 6e But if a file in
223a0 20 61 20 64 69 66 66 65 72 65 6e 74 20 64 69 72 a different dir
223b0 65 63 74 6f 72 79 0a 2a 2a 20 66 72 6f 6d 20 74 ectory.** from t
223c0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
223d0 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 64 is used, then d
223e0 69 66 66 65 72 69 6e 67 20 61 63 63 65 73 73 20 iffering access
223f0 70 65 72 6d 69 73 73 69 6f 6e 73 0a 2a 2a 20 6f permissions.** o
22400 72 20 61 20 63 68 72 6f 6f 74 28 29 20 6d 69 67 r a chroot() mig
22410 68 74 20 63 61 75 73 65 20 74 77 6f 20 64 69 66 ht cause two dif
22420 66 65 72 65 6e 74 20 70 72 6f 63 65 73 73 65 73 ferent processes
22430 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 on the same.**
22440 64 61 74 61 62 61 73 65 20 74 6f 20 65 6e 64 20 database to end
22450 75 70 20 75 73 69 6e 67 20 64 69 66 66 65 72 65 up using differe
22460 6e 74 20 66 69 6c 65 73 20 66 6f 72 20 73 68 61 nt files for sha
22470 72 65 64 20 6d 65 6d 6f 72 79 20 2d 20 0a 2a 2a red memory - .**
22480 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 74 68 meaning that th
22490 65 69 72 20 6d 65 6d 6f 72 79 20 77 6f 75 6c 64 eir memory would
224a0 20 6e 6f 74 20 72 65 61 6c 6c 79 20 62 65 20 73 not really be s
224b0 68 61 72 65 64 20 2d 20 72 65 73 75 6c 74 69 6e hared - resultin
224c0 67 0a 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 65 g.** in database
224d0 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 20 4e 65 corruption. Ne
224e0 76 65 72 74 68 65 6c 65 73 73 2c 20 74 68 69 73 vertheless, this
224f0 20 74 6d 70 66 73 20 66 69 6c 65 20 75 73 61 67 tmpfs file usag
22500 65 0a 2a 2a 20 63 61 6e 20 62 65 20 65 6e 61 62 e.** can be enab
22510 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 led at compile-t
22520 69 6d 65 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 ime using -DSQLI
22530 54 45 5f 53 48 4d 5f 44 49 52 45 43 54 4f 52 59 TE_SHM_DIRECTORY
22540 3d 22 2f 64 65 76 2f 73 68 6d 22 0a 2a 2a 20 6f ="/dev/shm".** o
22550 72 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74 r the equivalent
22560 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20 74 68 . The use of th
22570 65 20 53 51 4c 49 54 45 5f 53 48 4d 5f 44 49 52 e SQLITE_SHM_DIR
22580 45 43 54 4f 52 59 20 63 6f 6d 70 69 6c 65 2d 74 ECTORY compile-t
22590 69 6d 65 0a 2a 2a 20 6f 70 74 69 6f 6e 20 72 65 ime.** option re
225a0 73 75 6c 74 73 20 69 6e 20 61 6e 20 69 6e 63 6f sults in an inco
225b0 6d 70 61 74 69 62 6c 65 20 62 75 69 6c 64 20 6f mpatible build o
225c0 66 20 53 51 4c 69 74 65 3b 20 20 62 75 69 6c 64 f SQLite; build
225d0 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 74 s of SQLite.** t
225e0 68 61 74 20 77 69 74 68 20 64 69 66 66 65 72 69 hat with differi
225f0 6e 67 20 53 51 4c 49 54 45 5f 53 48 4d 5f 44 49 ng SQLITE_SHM_DI
22600 52 45 43 54 4f 52 59 20 73 65 74 74 69 6e 67 73 RECTORY settings
22610 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 attempt to use
22620 74 68 65 0a 2a 2a 20 73 61 6d 65 20 64 61 74 61 the.** same data
22630 62 61 73 65 20 66 69 6c 65 20 61 74 20 74 68 65 base file at the
22640 20 73 61 6d 65 20 74 69 6d 65 2c 20 64 61 74 61 same time, data
22650 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 base corruption
22660 77 69 6c 6c 20 6c 69 6b 65 6c 79 0a 2a 2a 20 72 will likely.** r
22670 65 73 75 6c 74 2e 20 54 68 65 20 53 51 4c 49 54 esult. The SQLIT
22680 45 5f 53 48 4d 5f 44 49 52 45 43 54 4f 52 59 20 E_SHM_DIRECTORY
22690 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 compile-time opt
226a0 69 6f 6e 20 69 73 20 63 6f 6e 73 69 64 65 72 65 ion is considere
226b0 64 0a 2a 2a 20 22 75 6e 73 75 70 70 6f 72 74 65 d.** "unsupporte
226c0 64 22 20 61 6e 64 20 6d 61 79 20 67 6f 20 61 77 d" and may go aw
226d0 61 79 20 69 6e 20 61 20 66 75 74 75 72 65 20 53 ay in a future S
226e0 51 4c 69 74 65 20 72 65 6c 65 61 73 65 2e 0a 2a QLite release..*
226f0 2a 0a 2a 2a 20 57 68 65 6e 20 6f 70 65 6e 69 6e *.** When openin
22700 67 20 61 20 6e 65 77 20 73 68 61 72 65 64 2d 6d g a new shared-m
22710 65 6d 6f 72 79 20 66 69 6c 65 2c 20 69 66 20 6e emory file, if n
22720 6f 20 6f 74 68 65 72 20 69 6e 73 74 61 6e 63 65 o other instance
22730 73 20 6f 66 20 74 68 61 74 0a 2a 2a 20 66 69 6c s of that.** fil
22740 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 e are currently
22750 6f 70 65 6e 2c 20 69 6e 20 74 68 69 73 20 70 72 open, in this pr
22760 6f 63 65 73 73 20 6f 72 20 69 6e 20 6f 74 68 65 ocess or in othe
22770 72 20 70 72 6f 63 65 73 73 65 73 2c 20 74 68 65 r processes, the
22780 6e 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 6d 75 n.** the file mu
22790 73 74 20 62 65 20 74 72 75 6e 63 61 74 65 64 20 st be truncated
227a0 74 6f 20 7a 65 72 6f 20 6c 65 6e 67 74 68 20 6f to zero length o
227b0 72 20 68 61 76 65 20 69 74 73 20 68 65 61 64 65 r have its heade
227c0 72 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a r cleared..**.**
227d0 20 49 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c If the original
227e0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 database file (
227f0 70 44 62 46 64 29 20 69 73 20 75 73 69 6e 67 20 pDbFd) is using
22800 74 68 65 20 22 75 6e 69 78 2d 65 78 63 6c 22 20 the "unix-excl"
22810 56 46 53 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e VFS.** that mean
22820 73 20 74 68 61 74 20 61 6e 20 65 78 63 6c 75 73 s that an exclus
22830 69 76 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 ive lock is held
22840 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
22850 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 74 68 61 file and.** tha
22860 74 20 6e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 t no other proce
22870 73 73 65 73 20 61 72 65 20 61 62 6c 65 20 74 6f sses are able to
22880 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 read or write t
22890 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 6e he database. In
228a0 0a 2a 2a 20 74 68 61 74 20 63 61 73 65 2c 20 77 .** that case, w
228b0 65 20 64 6f 20 6e 6f 74 20 72 65 61 6c 6c 79 20 e do not really
228c0 6e 65 65 64 20 73 68 61 72 65 64 20 6d 65 6d 6f need shared memo
228d0 72 79 2e 20 20 4e 6f 20 73 68 61 72 65 64 20 6d ry. No shared m
228e0 65 6d 6f 72 79 0a 2a 2a 20 66 69 6c 65 20 69 73 emory.** file is
228f0 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 73 created. The s
22900 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 77 69 6c hared memory wil
22910 6c 20 62 65 20 73 69 6d 75 6c 61 74 65 64 20 77 l be simulated w
22920 69 74 68 20 68 65 61 70 20 6d 65 6d 6f 72 79 2e ith heap memory.
22930 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
22940 6e 69 78 4f 70 65 6e 53 68 61 72 65 64 4d 65 6d nixOpenSharedMem
22950 6f 72 79 28 75 6e 69 78 46 69 6c 65 20 2a 70 44 ory(unixFile *pD
22960 62 46 64 29 7b 0a 20 20 73 74 72 75 63 74 20 75 bFd){. struct u
22970 6e 69 78 53 68 6d 20 2a 70 20 3d 20 30 3b 20 20 nixShm *p = 0;
22980 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 /* The c
22990 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 62 65 20 onnection to be
229a0 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 73 74 72 75 opened */. stru
229b0 63 74 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20 2a ct unixShmNode *
229c0 70 53 68 6d 4e 6f 64 65 3b 20 20 20 2f 2a 20 54 pShmNode; /* T
229d0 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d 6d he underlying mm
229e0 61 70 70 65 64 20 66 69 6c 65 20 2a 2f 0a 20 20 apped file */.
229f0 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
22a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22a10 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 2a /* Result code *
22a20 2f 0a 20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 /. unixInodeInf
22a30 6f 20 2a 70 49 6e 6f 64 65 3b 20 20 20 20 20 20 o *pInode;
22a40 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 6f 64 65 /* The inode
22a50 20 6f 66 20 66 64 20 2a 2f 0a 20 20 63 68 61 72 of fd */. char
22a60 20 2a 7a 53 68 6d 46 69 6c 65 6e 61 6d 65 3b 20 *zShmFilename;
22a70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
22a80 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ame of the file
22a90 75 73 65 64 20 66 6f 72 20 53 48 4d 20 2a 2f 0a used for SHM */.
22aa0 20 20 69 6e 74 20 6e 53 68 6d 46 69 6c 65 6e 61 int nShmFilena
22ab0 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 me;
22ac0 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
22ad0 20 53 48 4d 20 66 69 6c 65 6e 61 6d 65 20 69 6e SHM filename in
22ae0 20 62 79 74 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 bytes */.. /*
22af0 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 Allocate space f
22b00 6f 72 20 74 68 65 20 6e 65 77 20 75 6e 69 78 53 or the new unixS
22b10 68 6d 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 hm object. */.
22b20 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c p = sqlite3_mall
22b30 6f 63 36 34 28 20 73 69 7a 65 6f 66 28 2a 70 29 oc64( sizeof(*p)
22b40 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 );. if( p==0 )
22b50 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
22b60 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 20 6d 65 6d OMEM_BKPT;. mem
22b70 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 set(p, 0, sizeof
22b80 28 2a 70 29 29 3b 0a 20 20 61 73 73 65 72 74 28 (*p));. assert(
22b90 20 70 44 62 46 64 2d 3e 70 53 68 6d 3d 3d 30 20 pDbFd->pShm==0
22ba0 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 );.. /* Check t
22bb0 6f 20 73 65 65 20 69 66 20 61 20 75 6e 69 78 53 o see if a unixS
22bc0 68 6d 4e 6f 64 65 20 6f 62 6a 65 63 74 20 61 6c hmNode object al
22bd0 72 65 61 64 79 20 65 78 69 73 74 73 2e 20 52 65 ready exists. Re
22be0 75 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a use an existing.
22bf0 20 20 2a 2a 20 6f 6e 65 20 69 66 20 70 72 65 73 ** one if pres
22c00 65 6e 74 2e 20 43 72 65 61 74 65 20 61 20 6e 65 ent. Create a ne
22c10 77 20 6f 6e 65 20 69 66 20 6e 65 63 65 73 73 61 w one if necessa
22c20 72 79 2e 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45 ry.. */. unixE
22c30 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 nterMutex();. p
22c40 49 6e 6f 64 65 20 3d 20 70 44 62 46 64 2d 3e 70 Inode = pDbFd->p
22c50 49 6e 6f 64 65 3b 0a 20 20 70 53 68 6d 4e 6f 64 Inode;. pShmNod
22c60 65 20 3d 20 70 49 6e 6f 64 65 2d 3e 70 53 68 6d e = pInode->pShm
22c70 4e 6f 64 65 3b 0a 20 20 69 66 28 20 70 53 68 6d Node;. if( pShm
22c80 4e 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 73 Node==0 ){. s
22c90 74 72 75 63 74 20 73 74 61 74 20 73 53 74 61 74 truct stat sStat
22ca0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
22cb0 20 20 2f 2a 20 66 73 74 61 74 28 29 20 69 6e 66 /* fstat() inf
22cc0 6f 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 66 o for database f
22cd0 69 6c 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ile */.#ifndef S
22ce0 51 4c 49 54 45 5f 53 48 4d 5f 44 49 52 45 43 54 QLITE_SHM_DIRECT
22cf0 4f 52 59 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 ORY. const ch
22d00 61 72 20 2a 7a 42 61 73 65 50 61 74 68 20 3d 20 ar *zBasePath =
22d10 70 44 62 46 64 2d 3e 7a 50 61 74 68 3b 0a 23 65 pDbFd->zPath;.#e
22d20 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 43 61 6c ndif.. /* Cal
22d30 6c 20 66 73 74 61 74 28 29 20 74 6f 20 66 69 67 l fstat() to fig
22d40 75 72 65 20 6f 75 74 20 74 68 65 20 70 65 72 6d ure out the perm
22d50 69 73 73 69 6f 6e 73 20 6f 6e 20 74 68 65 20 64 issions on the d
22d60 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 atabase file. If
22d70 0a 20 20 20 20 2a 2a 20 61 20 6e 65 77 20 2a 2d . ** a new *-
22d80 73 68 6d 20 66 69 6c 65 20 69 73 20 63 72 65 61 shm file is crea
22d90 74 65 64 2c 20 61 6e 20 61 74 74 65 6d 70 74 20 ted, an attempt
22da0 77 69 6c 6c 20 62 65 20 6d 61 64 65 20 74 6f 20 will be made to
22db0 63 72 65 61 74 65 20 69 74 0a 20 20 20 20 2a 2a create it. **
22dc0 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 70 with the same p
22dd0 65 72 6d 69 73 73 69 6f 6e 73 2e 0a 20 20 20 20 ermissions..
22de0 2a 2f 0a 20 20 20 20 69 66 28 20 6f 73 46 73 74 */. if( osFst
22df0 61 74 28 70 44 62 46 64 2d 3e 68 2c 20 26 73 53 at(pDbFd->h, &sS
22e00 74 61 74 29 20 29 7b 0a 20 20 20 20 20 20 72 63 tat) ){. rc
22e10 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
22e20 46 53 54 41 54 3b 0a 20 20 20 20 20 20 67 6f 74 FSTAT;. got
22e30 6f 20 73 68 6d 5f 6f 70 65 6e 5f 65 72 72 3b 0a o shm_open_err;.
22e40 20 20 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 }..#ifdef SQ
22e50 4c 49 54 45 5f 53 48 4d 5f 44 49 52 45 43 54 4f LITE_SHM_DIRECTO
22e60 52 59 0a 20 20 20 20 6e 53 68 6d 46 69 6c 65 6e RY. nShmFilen
22e70 61 6d 65 20 3d 20 73 69 7a 65 6f 66 28 53 51 4c ame = sizeof(SQL
22e80 49 54 45 5f 53 48 4d 5f 44 49 52 45 43 54 4f 52 ITE_SHM_DIRECTOR
22e90 59 29 20 2b 20 33 31 3b 0a 23 65 6c 73 65 0a 20 Y) + 31;.#else.
22ea0 20 20 20 6e 53 68 6d 46 69 6c 65 6e 61 6d 65 20 nShmFilename
22eb0 3d 20 36 20 2b 20 28 69 6e 74 29 73 74 72 6c 65 = 6 + (int)strle
22ec0 6e 28 7a 42 61 73 65 50 61 74 68 29 3b 0a 23 65 n(zBasePath);.#e
22ed0 6e 64 69 66 0a 20 20 20 20 70 53 68 6d 4e 6f 64 ndif. pShmNod
22ee0 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c e = sqlite3_mall
22ef0 6f 63 36 34 28 20 73 69 7a 65 6f 66 28 2a 70 53 oc64( sizeof(*pS
22f00 68 6d 4e 6f 64 65 29 20 2b 20 6e 53 68 6d 46 69 hmNode) + nShmFi
22f10 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 20 20 69 66 lename );. if
22f20 28 20 70 53 68 6d 4e 6f 64 65 3d 3d 30 20 29 7b ( pShmNode==0 ){
22f30 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
22f40 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 TE_NOMEM_BKPT;.
22f50 20 20 20 20 20 67 6f 74 6f 20 73 68 6d 5f 6f 70 goto shm_op
22f60 65 6e 5f 65 72 72 3b 0a 20 20 20 20 7d 0a 20 20 en_err;. }.
22f70 20 20 6d 65 6d 73 65 74 28 70 53 68 6d 4e 6f 64 memset(pShmNod
22f80 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 53 e, 0, sizeof(*pS
22f90 68 6d 4e 6f 64 65 29 2b 6e 53 68 6d 46 69 6c 65 hmNode)+nShmFile
22fa0 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 53 68 6d 46 name);. zShmF
22fb0 69 6c 65 6e 61 6d 65 20 3d 20 70 53 68 6d 4e 6f ilename = pShmNo
22fc0 64 65 2d 3e 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 de->zFilename =
22fd0 28 63 68 61 72 2a 29 26 70 53 68 6d 4e 6f 64 65 (char*)&pShmNode
22fe0 5b 31 5d 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 [1];.#ifdef SQLI
22ff0 54 45 5f 53 48 4d 5f 44 49 52 45 43 54 4f 52 59 TE_SHM_DIRECTORY
23000 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
23010 72 69 6e 74 66 28 6e 53 68 6d 46 69 6c 65 6e 61 rintf(nShmFilena
23020 6d 65 2c 20 7a 53 68 6d 46 69 6c 65 6e 61 6d 65 me, zShmFilename
23030 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
23040 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 SQLITE_S
23050 48 4d 5f 44 49 52 45 43 54 4f 52 59 20 22 2f 73 HM_DIRECTORY "/s
23060 71 6c 69 74 65 2d 73 68 6d 2d 25 78 2d 25 78 22 qlite-shm-%x-%x"
23070 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
23080 20 20 20 20 20 20 20 28 75 33 32 29 73 53 74 61 (u32)sSta
23090 74 2e 73 74 5f 69 6e 6f 2c 20 28 75 33 32 29 73 t.st_ino, (u32)s
230a0 53 74 61 74 2e 73 74 5f 64 65 76 29 3b 0a 23 65 Stat.st_dev);.#e
230b0 6c 73 65 0a 20 20 20 20 73 71 6c 69 74 65 33 5f lse. sqlite3_
230c0 73 6e 70 72 69 6e 74 66 28 6e 53 68 6d 46 69 6c snprintf(nShmFil
230d0 65 6e 61 6d 65 2c 20 7a 53 68 6d 46 69 6c 65 6e ename, zShmFilen
230e0 61 6d 65 2c 20 22 25 73 2d 73 68 6d 22 2c 20 7a ame, "%s-shm", z
230f0 42 61 73 65 50 61 74 68 29 3b 0a 20 20 20 20 73 BasePath);. s
23100 71 6c 69 74 65 33 46 69 6c 65 53 75 66 66 69 78 qlite3FileSuffix
23110 33 28 70 44 62 46 64 2d 3e 7a 50 61 74 68 2c 20 3(pDbFd->zPath,
23120 7a 53 68 6d 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 zShmFilename);.#
23130 65 6e 64 69 66 0a 20 20 20 20 70 53 68 6d 4e 6f endif. pShmNo
23140 64 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20 20 20 20 de->h = -1;.
23150 70 44 62 46 64 2d 3e 70 49 6e 6f 64 65 2d 3e 70 pDbFd->pInode->p
23160 53 68 6d 4e 6f 64 65 20 3d 20 70 53 68 6d 4e 6f ShmNode = pShmNo
23170 64 65 3b 0a 20 20 20 20 70 53 68 6d 4e 6f 64 65 de;. pShmNode
23180 2d 3e 70 49 6e 6f 64 65 20 3d 20 70 44 62 46 64 ->pInode = pDbFd
23190 2d 3e 70 49 6e 6f 64 65 3b 0a 20 20 20 20 69 66 ->pInode;. if
231a0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
231b0 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 onfig.bCoreMutex
231c0 20 29 7b 0a 20 20 20 20 20 20 70 53 68 6d 4e 6f ){. pShmNo
231d0 64 65 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c 69 de->mutex = sqli
231e0 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 te3_mutex_alloc(
231f0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
23200 54 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 53 T);. if( pS
23210 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 3d 3d 30 hmNode->mutex==0
23220 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
23230 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5f 42 4b SQLITE_NOMEM_BK
23240 50 54 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f PT;. goto
23250 20 73 68 6d 5f 6f 70 65 6e 5f 65 72 72 3b 0a 20 shm_open_err;.
23260 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 }. }..
23270 20 20 69 66 28 20 70 49 6e 6f 64 65 2d 3e 62 50 if( pInode->bP
23280 72 6f 63 65 73 73 4c 6f 63 6b 3d 3d 30 20 29 7b rocessLock==0 ){
23290 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 65 6e 46 . int openF
232a0 6c 61 67 73 20 3d 20 4f 5f 52 44 57 52 20 7c 20 lags = O_RDWR |
232b0 4f 5f 43 52 45 41 54 3b 0a 20 20 20 20 20 20 69 O_CREAT;. i
232c0 66 28 20 73 71 6c 69 74 65 33 5f 75 72 69 5f 62 f( sqlite3_uri_b
232d0 6f 6f 6c 65 61 6e 28 70 44 62 46 64 2d 3e 7a 50 oolean(pDbFd->zP
232e0 61 74 68 2c 20 22 72 65 61 64 6f 6e 6c 79 5f 73 ath, "readonly_s
232f0 68 6d 22 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 hm", 0) ){.
23300 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 3d 20 4f openFlags = O
23310 5f 52 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 20 _RDONLY;.
23320 20 70 53 68 6d 4e 6f 64 65 2d 3e 69 73 52 65 61 pShmNode->isRea
23330 64 6f 6e 6c 79 20 3d 20 31 3b 0a 20 20 20 20 20 donly = 1;.
23340 20 7d 0a 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 }. pShmNod
23350 65 2d 3e 68 20 3d 20 72 6f 62 75 73 74 5f 6f 70 e->h = robust_op
23360 65 6e 28 7a 53 68 6d 46 69 6c 65 6e 61 6d 65 2c en(zShmFilename,
23370 20 6f 70 65 6e 46 6c 61 67 73 2c 20 28 73 53 74 openFlags, (sSt
23380 61 74 2e 73 74 5f 6d 6f 64 65 26 30 37 37 37 29 at.st_mode&0777)
23390 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 53 68 );. if( pSh
233a0 6d 4e 6f 64 65 2d 3e 68 3c 30 20 29 7b 0a 20 20 mNode->h<0 ){.
233b0 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69 78 4c rc = unixL
233c0 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 43 ogError(SQLITE_C
233d0 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 2c 20 22 6f ANTOPEN_BKPT, "o
233e0 70 65 6e 22 2c 20 7a 53 68 6d 46 69 6c 65 6e 61 pen", zShmFilena
233f0 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 me);. got
23400 6f 20 73 68 6d 5f 6f 70 65 6e 5f 65 72 72 3b 0a o shm_open_err;.
23410 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f }.. /
23420 2a 20 49 66 20 74 68 69 73 20 70 72 6f 63 65 73 * If this proces
23430 73 20 69 73 20 72 75 6e 6e 69 6e 67 20 61 73 20 s is running as
23440 72 6f 6f 74 2c 20 6d 61 6b 65 20 73 75 72 65 20 root, make sure
23450 74 68 61 74 20 74 68 65 20 53 48 4d 20 66 69 6c that the SHM fil
23460 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 6f 77 e. ** is ow
23470 6e 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 ned by the same
23480 75 73 65 72 20 74 68 61 74 20 6f 77 6e 73 20 74 user that owns t
23490 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 he original data
234a0 62 61 73 65 2e 20 20 4f 74 68 65 72 77 69 73 65 base. Otherwise
234b0 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6f ,. ** the o
234c0 72 69 67 69 6e 61 6c 20 6f 77 6e 65 72 20 77 69 riginal owner wi
234d0 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 ll not be able t
234e0 6f 20 63 6f 6e 6e 65 63 74 2e 0a 20 20 20 20 20 o connect..
234f0 20 2a 2f 0a 20 20 20 20 20 20 72 6f 62 75 73 74 */. robust
23500 46 63 68 6f 77 6e 28 70 53 68 6d 4e 6f 64 65 2d Fchown(pShmNode-
23510 3e 68 2c 20 73 53 74 61 74 2e 73 74 5f 75 69 64 >h, sStat.st_uid
23520 2c 20 73 53 74 61 74 2e 73 74 5f 67 69 64 29 3b , sStat.st_gid);
23530 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 . . /* Che
23540 63 6b 20 74 6f 20 73 65 65 20 69 66 20 61 6e 6f ck to see if ano
23550 74 68 65 72 20 70 72 6f 63 65 73 73 20 69 73 20 ther process is
23560 68 6f 6c 64 69 6e 67 20 74 68 65 20 64 65 61 64 holding the dead
23570 2d 6d 61 6e 20 73 77 69 74 63 68 2e 0a 20 20 20 -man switch..
23580 20 20 20 2a 2a 20 49 66 20 6e 6f 74 2c 20 74 72 ** If not, tr
23590 75 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 20 uncate the file
235a0 74 6f 20 7a 65 72 6f 20 6c 65 6e 67 74 68 2e 20 to zero length.
235b0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
235c0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
235d0 20 20 20 20 20 20 69 66 28 20 75 6e 69 78 53 68 if( unixSh
235e0 6d 53 79 73 74 65 6d 4c 6f 63 6b 28 70 44 62 46 mSystemLock(pDbF
235f0 64 2c 20 46 5f 57 52 4c 43 4b 2c 20 55 4e 49 58 d, F_WRLCK, UNIX
23600 5f 53 48 4d 5f 44 4d 53 2c 20 31 29 3d 3d 53 51 _SHM_DMS, 1)==SQ
23610 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
23620 20 20 20 69 66 28 20 72 6f 62 75 73 74 5f 66 74 if( robust_ft
23630 72 75 6e 63 61 74 65 28 70 53 68 6d 4e 6f 64 65 runcate(pShmNode
23640 2d 3e 68 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 ->h, 0) ){.
23650 20 20 20 20 20 72 63 20 3d 20 75 6e 69 78 4c 6f rc = unixLo
23660 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 49 4f gError(SQLITE_IO
23670 45 52 52 5f 53 48 4d 4f 50 45 4e 2c 20 22 66 74 ERR_SHMOPEN, "ft
23680 72 75 6e 63 61 74 65 22 2c 20 7a 53 68 6d 46 69 runcate", zShmFi
23690 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 lename);.
236a0 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
236b0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
236c0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
236d0 20 3d 20 75 6e 69 78 53 68 6d 53 79 73 74 65 6d = unixShmSystem
236e0 4c 6f 63 6b 28 70 44 62 46 64 2c 20 46 5f 52 44 Lock(pDbFd, F_RD
236f0 4c 43 4b 2c 20 55 4e 49 58 5f 53 48 4d 5f 44 4d LCK, UNIX_SHM_DM
23700 53 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 S, 1);. }.
23710 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f if( rc ) go
23720 74 6f 20 73 68 6d 5f 6f 70 65 6e 5f 65 72 72 3b to shm_open_err;
23730 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
23740 20 4d 61 6b 65 20 74 68 65 20 6e 65 77 20 63 6f Make the new co
23750 6e 6e 65 63 74 69 6f 6e 20 61 20 63 68 69 6c 64 nnection a child
23760 20 6f 66 20 74 68 65 20 75 6e 69 78 53 68 6d 4e of the unixShmN
23770 6f 64 65 20 2a 2f 0a 20 20 70 2d 3e 70 53 68 6d ode */. p->pShm
23780 4e 6f 64 65 20 3d 20 70 53 68 6d 4e 6f 64 65 3b Node = pShmNode;
23790 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
237a0 45 42 55 47 0a 20 20 70 2d 3e 69 64 20 3d 20 70 EBUG. p->id = p
237b0 53 68 6d 4e 6f 64 65 2d 3e 6e 65 78 74 53 68 6d ShmNode->nextShm
237c0 49 64 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 70 Id++;.#endif. p
237d0 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 66 2b 2b 3b ShmNode->nRef++;
237e0 0a 20 20 70 44 62 46 64 2d 3e 70 53 68 6d 20 3d . pDbFd->pShm =
237f0 20 70 3b 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d p;. unixLeaveM
23800 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 54 68 utex();.. /* Th
23810 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e e reference coun
23820 74 20 6f 6e 20 70 53 68 6d 4e 6f 64 65 20 68 61 t on pShmNode ha
23830 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 s already been i
23840 6e 63 72 65 6d 65 6e 74 65 64 20 75 6e 64 65 72 ncremented under
23850 0a 20 20 2a 2a 20 74 68 65 20 63 6f 76 65 72 20 . ** the cover
23860 6f 66 20 74 68 65 20 75 6e 69 78 45 6e 74 65 72 of the unixEnter
23870 4d 75 74 65 78 28 29 20 6d 75 74 65 78 20 61 6e Mutex() mutex an
23880 64 20 74 68 65 20 70 6f 69 6e 74 65 72 20 66 72 d the pointer fr
23890 6f 6d 20 74 68 65 0a 20 20 2a 2a 20 6e 65 77 20 om the. ** new
238a0 28 73 74 72 75 63 74 20 75 6e 69 78 53 68 6d 29 (struct unixShm)
238b0 20 6f 62 6a 65 63 74 20 74 6f 20 74 68 65 20 70 object to the p
238c0 53 68 6d 4e 6f 64 65 20 68 61 73 20 62 65 65 6e ShmNode has been
238d0 20 73 65 74 2e 20 41 6c 6c 20 74 68 61 74 20 69 set. All that i
238e0 73 0a 20 20 2a 2a 20 6c 65 66 74 20 74 6f 20 64 s. ** left to d
238f0 6f 20 69 73 20 74 6f 20 6c 69 6e 6b 20 74 68 65 o is to link the
23900 20 6e 65 77 20 6f 62 6a 65 63 74 20 69 6e 74 6f new object into
23910 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 the linked list
23920 20 73 74 61 72 74 69 6e 67 0a 20 20 2a 2a 20 61 starting. ** a
23930 74 20 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 t pShmNode->pFir
23940 73 74 2e 20 54 68 69 73 20 6d 75 73 74 20 62 65 st. This must be
23950 20 64 6f 6e 65 20 77 68 69 6c 65 20 68 6f 6c 64 done while hold
23960 69 6e 67 20 74 68 65 20 70 53 68 6d 4e 6f 64 65 ing the pShmNode
23970 2d 3e 6d 75 74 65 78 20 0a 20 20 2a 2a 20 6d 75 ->mutex . ** mu
23980 74 65 78 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 tex.. */. sqli
23990 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
239a0 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 pShmNode->mutex)
239b0 3b 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 ;. p->pNext = p
239c0 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73 74 3b ShmNode->pFirst;
239d0 0a 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 . pShmNode->pFi
239e0 72 73 74 20 3d 20 70 3b 0a 20 20 73 71 6c 69 74 rst = p;. sqlit
239f0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
23a00 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b ShmNode->mutex);
23a10 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
23a20 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 _OK;.. /* Jump
23a30 68 65 72 65 20 6f 6e 20 61 6e 79 20 65 72 72 6f here on any erro
23a40 72 20 2a 2f 0a 73 68 6d 5f 6f 70 65 6e 5f 65 72 r */.shm_open_er
23a50 72 3a 0a 20 20 75 6e 69 78 53 68 6d 50 75 72 67 r:. unixShmPurg
23a60 65 28 70 44 62 46 64 29 3b 20 20 20 20 20 20 20 e(pDbFd);
23a70 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 66 72 65 /* This call fre
23a80 65 73 20 70 53 68 6d 4e 6f 64 65 20 69 66 20 72 es pShmNode if r
23a90 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 73 71 6c equired */. sql
23aa0 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 ite3_free(p);.
23ab0 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 unixLeaveMutex()
23ac0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
23ad0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
23ae0 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
23af0 74 6f 20 6f 62 74 61 69 6e 20 61 20 70 6f 69 6e to obtain a poin
23b00 74 65 72 20 74 6f 20 72 65 67 69 6f 6e 20 69 52 ter to region iR
23b10 65 67 69 6f 6e 20 6f 66 20 74 68 65 20 0a 2a 2a egion of the .**
23b20 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 61 shared-memory a
23b30 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
23b40 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
23b50 20 66 64 2e 20 53 68 61 72 65 64 2d 6d 65 6d 6f fd. Shared-memo
23b60 72 79 20 72 65 67 69 6f 6e 73 20 0a 2a 2a 20 61 ry regions .** a
23b70 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72 re numbered star
23b80 74 69 6e 67 20 66 72 6f 6d 20 7a 65 72 6f 2e 20 ting from zero.
23b90 45 61 63 68 20 73 68 61 72 65 64 2d 6d 65 6d 6f Each shared-memo
23ba0 72 79 20 72 65 67 69 6f 6e 20 69 73 20 73 7a 52 ry region is szR
23bb0 65 67 69 6f 6e 20 0a 2a 2a 20 62 79 74 65 73 20 egion .** bytes
23bc0 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 in size..**.** I
23bd0 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
23be0 73 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 s, an error code
23bf0 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
23c00 20 2a 70 70 20 69 73 20 73 65 74 20 74 6f 20 4e *pp is set to N
23c10 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 ULL..**.** Other
23c20 77 69 73 65 2c 20 69 66 20 74 68 65 20 62 45 78 wise, if the bEx
23c30 74 65 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 tend parameter i
23c40 73 20 30 20 61 6e 64 20 74 68 65 20 72 65 71 75 s 0 and the requ
23c50 65 73 74 65 64 20 73 68 61 72 65 64 2d 6d 65 6d ested shared-mem
23c60 6f 72 79 0a 2a 2a 20 72 65 67 69 6f 6e 20 68 61 ory.** region ha
23c70 73 20 6e 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 s not been alloc
23c80 61 74 65 64 20 28 62 79 20 61 6e 79 20 63 6c 69 ated (by any cli
23c90 65 6e 74 2c 20 69 6e 63 6c 75 64 69 6e 67 20 6f ent, including o
23ca0 6e 65 20 72 75 6e 6e 69 6e 67 20 69 6e 20 61 0a ne running in a.
23cb0 2a 2a 20 73 65 70 61 72 61 74 65 20 70 72 6f 63 ** separate proc
23cc0 65 73 73 29 2c 20 74 68 65 6e 20 2a 70 70 20 69 ess), then *pp i
23cd0 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20 61 6e s set to NULL an
23ce0 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 d SQLITE_OK retu
23cf0 72 6e 65 64 2e 20 49 66 20 0a 2a 2a 20 62 45 78 rned. If .** bEx
23d00 74 65 6e 64 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f tend is non-zero
23d10 20 61 6e 64 20 74 68 65 20 72 65 71 75 65 73 74 and the request
23d20 65 64 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 ed shared-memory
23d30 20 72 65 67 69 6f 6e 20 68 61 73 20 6e 6f 74 20 region has not
23d40 79 65 74 20 0a 2a 2a 20 62 65 65 6e 20 61 6c 6c yet .** been all
23d50 6f 63 61 74 65 64 2c 20 69 74 20 69 73 20 61 6c ocated, it is al
23d60 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 20 located by this
23d70 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
23d80 49 66 20 74 68 65 20 73 68 61 72 65 64 2d 6d 65 If the shared-me
23d90 6d 6f 72 79 20 72 65 67 69 6f 6e 20 68 61 73 20 mory region has
23da0 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c already been all
23db0 6f 63 61 74 65 64 20 6f 72 20 69 73 20 61 6c 6c ocated or is all
23dc0 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 69 ocated by.** thi
23dd0 73 20 63 61 6c 6c 20 61 73 20 64 65 73 63 72 69 s call as descri
23de0 62 65 64 20 61 62 6f 76 65 2c 20 74 68 65 6e 20 bed above, then
23df0 69 74 20 69 73 20 6d 61 70 70 65 64 20 69 6e 74 it is mapped int
23e00 6f 20 74 68 69 73 20 70 72 6f 63 65 73 73 65 73 o this processes
23e10 20 0a 2a 2a 20 61 64 64 72 65 73 73 20 73 70 61 .** address spa
23e20 63 65 20 28 69 66 20 69 74 20 69 73 20 6e 6f 74 ce (if it is not
23e30 20 61 6c 72 65 61 64 79 29 2c 20 2a 70 70 20 69 already), *pp i
23e40 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 s set to point t
23e50 6f 20 74 68 65 20 6d 61 70 70 65 64 20 0a 2a 2a o the mapped .**
23e60 20 6d 65 6d 6f 72 79 20 61 6e 64 20 53 51 4c 49 memory and SQLI
23e70 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a TE_OK returned..
23e80 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
23e90 69 78 53 68 6d 4d 61 70 28 0a 20 20 73 71 6c 69 ixShmMap(. sqli
23ea0 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 20 20 te3_file *fd,
23eb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 /* H
23ec0 61 6e 64 6c 65 20 6f 70 65 6e 20 6f 6e 20 64 61 andle open on da
23ed0 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 tabase file */.
23ee0 20 69 6e 74 20 69 52 65 67 69 6f 6e 2c 20 20 20 int iRegion,
23ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
23f00 20 2f 2a 20 52 65 67 69 6f 6e 20 74 6f 20 72 65 /* Region to re
23f10 74 72 69 65 76 65 20 2a 2f 0a 20 20 69 6e 74 20 trieve */. int
23f20 73 7a 52 65 67 69 6f 6e 2c 20 20 20 20 20 20 20 szRegion,
23f30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
23f40 69 7a 65 20 6f 66 20 72 65 67 69 6f 6e 73 20 2a ize of regions *
23f50 2f 0a 20 20 69 6e 74 20 62 45 78 74 65 6e 64 2c /. int bExtend,
23f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
23f70 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 /* True to e
23f80 78 74 65 6e 64 20 66 69 6c 65 20 69 66 20 6e 65 xtend file if ne
23f90 63 65 73 73 61 72 79 20 2a 2f 0a 20 20 76 6f 69 cessary */. voi
23fa0 64 20 76 6f 6c 61 74 69 6c 65 20 2a 2a 70 70 20 d volatile **pp
23fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
23fc0 4f 55 54 3a 20 4d 61 70 70 65 64 20 6d 65 6d 6f OUT: Mapped memo
23fd0 72 79 20 2a 2f 0a 29 7b 0a 20 20 75 6e 69 78 46 ry */.){. unixF
23fe0 69 6c 65 20 2a 70 44 62 46 64 20 3d 20 28 75 6e ile *pDbFd = (un
23ff0 69 78 46 69 6c 65 2a 29 66 64 3b 0a 20 20 75 6e ixFile*)fd;. un
24000 69 78 53 68 6d 20 2a 70 3b 0a 20 20 75 6e 69 78 ixShm *p;. unix
24010 53 68 6d 4e 6f 64 65 20 2a 70 53 68 6d 4e 6f 64 ShmNode *pShmNod
24020 65 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 e;. int rc = SQ
24030 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e LITE_OK;. int n
24040 53 68 6d 50 65 72 4d 61 70 20 3d 20 75 6e 69 78 ShmPerMap = unix
24050 53 68 6d 52 65 67 69 6f 6e 50 65 72 4d 61 70 28 ShmRegionPerMap(
24060 29 3b 0a 20 20 69 6e 74 20 6e 52 65 71 52 65 67 );. int nReqReg
24070 69 6f 6e 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 ion;.. /* If th
24080 65 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 e shared-memory
24090 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 79 65 74 file has not yet
240a0 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c 20 6f 70 been opened, op
240b0 65 6e 20 69 74 20 6e 6f 77 2e 20 2a 2f 0a 20 20 en it now. */.
240c0 69 66 28 20 70 44 62 46 64 2d 3e 70 53 68 6d 3d if( pDbFd->pShm=
240d0 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 75 =0 ){. rc = u
240e0 6e 69 78 4f 70 65 6e 53 68 61 72 65 64 4d 65 6d nixOpenSharedMem
240f0 6f 72 79 28 70 44 62 46 64 29 3b 0a 20 20 20 20 ory(pDbFd);.
24100 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
24110 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 K ) return rc;.
24120 20 7d 0a 0a 20 20 70 20 3d 20 70 44 62 46 64 2d }.. p = pDbFd-
24130 3e 70 53 68 6d 3b 0a 20 20 70 53 68 6d 4e 6f 64 >pShm;. pShmNod
24140 65 20 3d 20 70 2d 3e 70 53 68 6d 4e 6f 64 65 3b e = p->pShmNode;
24150 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
24160 5f 65 6e 74 65 72 28 70 53 68 6d 4e 6f 64 65 2d _enter(pShmNode-
24170 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72 >mutex);. asser
24180 74 28 20 73 7a 52 65 67 69 6f 6e 3d 3d 70 53 68 t( szRegion==pSh
24190 6d 4e 6f 64 65 2d 3e 73 7a 52 65 67 69 6f 6e 20 mNode->szRegion
241a0 7c 7c 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 || pShmNode->nRe
241b0 67 69 6f 6e 3d 3d 30 20 29 3b 0a 20 20 61 73 73 gion==0 );. ass
241c0 65 72 74 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 70 ert( pShmNode->p
241d0 49 6e 6f 64 65 3d 3d 70 44 62 46 64 2d 3e 70 49 Inode==pDbFd->pI
241e0 6e 6f 64 65 20 29 3b 0a 20 20 61 73 73 65 72 74 node );. assert
241f0 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 68 3e 3d 30 ( pShmNode->h>=0
24200 20 7c 7c 20 70 44 62 46 64 2d 3e 70 49 6e 6f 64 || pDbFd->pInod
24210 65 2d 3e 62 50 72 6f 63 65 73 73 4c 6f 63 6b 3d e->bProcessLock=
24220 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =1 );. assert(
24230 70 53 68 6d 4e 6f 64 65 2d 3e 68 3c 30 20 7c 7c pShmNode->h<0 ||
24240 20 70 44 62 46 64 2d 3e 70 49 6e 6f 64 65 2d 3e pDbFd->pInode->
24250 62 50 72 6f 63 65 73 73 4c 6f 63 6b 3d 3d 30 20 bProcessLock==0
24260 29 3b 0a 0a 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d );.. /* Minimum
24270 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 67 69 6f number of regio
24280 6e 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 62 ns required to b
24290 65 20 6d 61 70 70 65 64 2e 20 2a 2f 0a 20 20 6e e mapped. */. n
242a0 52 65 71 52 65 67 69 6f 6e 20 3d 20 28 28 69 52 ReqRegion = ((iR
242b0 65 67 69 6f 6e 2b 6e 53 68 6d 50 65 72 4d 61 70 egion+nShmPerMap
242c0 29 20 2f 20 6e 53 68 6d 50 65 72 4d 61 70 29 20 ) / nShmPerMap)
242d0 2a 20 6e 53 68 6d 50 65 72 4d 61 70 3b 0a 0a 20 * nShmPerMap;..
242e0 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e if( pShmNode->n
242f0 52 65 67 69 6f 6e 3c 6e 52 65 71 52 65 67 69 6f Region<nReqRegio
24300 6e 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a n ){. char **
24310 61 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 apNew;
24320 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
24330 65 77 20 61 70 52 65 67 69 6f 6e 5b 5d 20 61 72 ew apRegion[] ar
24340 72 61 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e ray */. int n
24350 42 79 74 65 20 3d 20 6e 52 65 71 52 65 67 69 6f Byte = nReqRegio
24360 6e 2a 73 7a 52 65 67 69 6f 6e 3b 20 20 20 2f 2a n*szRegion; /*
24370 20 4d 69 6e 69 6d 75 6d 20 72 65 71 75 69 72 65 Minimum require
24380 64 20 66 69 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 d file size */.
24390 20 20 20 73 74 72 75 63 74 20 73 74 61 74 20 73 struct stat s
243a0 53 74 61 74 3b 20 20 20 20 20 20 20 20 20 20 20 Stat;
243b0 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 /* Used by
243c0 20 66 73 74 61 74 28 29 20 2a 2f 0a 0a 20 20 20 fstat() */..
243d0 20 70 53 68 6d 4e 6f 64 65 2d 3e 73 7a 52 65 67 pShmNode->szReg
243e0 69 6f 6e 20 3d 20 73 7a 52 65 67 69 6f 6e 3b 0a ion = szRegion;.
243f0 0a 20 20 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 . if( pShmNod
24400 65 2d 3e 68 3e 3d 30 20 29 7b 0a 20 20 20 20 20 e->h>=0 ){.
24410 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74 65 /* The requeste
24420 64 20 72 65 67 69 6f 6e 20 69 73 20 6e 6f 74 20 d region is not
24430 6d 61 70 70 65 64 20 69 6e 74 6f 20 74 68 69 73 mapped into this
24440 20 70 72 6f 63 65 73 73 65 73 20 61 64 64 72 65 processes addre
24450 73 73 20 73 70 61 63 65 2e 0a 20 20 20 20 20 20 ss space..
24460 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 ** Check to see
24470 69 66 20 69 74 20 68 61 73 20 62 65 65 6e 20 61 if it has been a
24480 6c 6c 6f 63 61 74 65 64 20 28 69 2e 65 2e 20 69 llocated (i.e. i
24490 66 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 f the wal-index
244a0 66 69 6c 65 20 69 73 0a 20 20 20 20 20 20 2a 2a file is. **
244b0 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f large enough to
244c0 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 72 65 71 contain the req
244d0 75 65 73 74 65 64 20 72 65 67 69 6f 6e 29 2e 0a uested region)..
244e0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
244f0 66 28 20 6f 73 46 73 74 61 74 28 70 53 68 6d 4e f( osFstat(pShmN
24500 6f 64 65 2d 3e 68 2c 20 26 73 53 74 61 74 29 20 ode->h, &sStat)
24510 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
24520 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d SQLITE_IOERR_SHM
24530 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 20 67 6f SIZE;. go
24540 74 6f 20 73 68 6d 70 61 67 65 5f 6f 75 74 3b 0a to shmpage_out;.
24550 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 }. .
24560 20 69 66 28 20 73 53 74 61 74 2e 73 74 5f 73 69 if( sStat.st_si
24570 7a 65 3c 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 ze<nByte ){.
24580 20 20 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 /* The reque
24590 73 74 65 64 20 6d 65 6d 6f 72 79 20 72 65 67 69 sted memory regi
245a0 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 on does not exis
245b0 74 2e 20 49 66 20 62 45 78 74 65 6e 64 20 69 73 t. If bExtend is
245c0 20 73 65 74 20 74 6f 0a 20 20 20 20 20 20 20 20 set to.
245d0 2a 2a 20 66 61 6c 73 65 2c 20 65 78 69 74 20 65 ** false, exit e
245e0 61 72 6c 79 2e 20 2a 70 70 20 77 69 6c 6c 20 62 arly. *pp will b
245f0 65 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20 61 6e e set to NULL an
24600 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 d SQLITE_OK retu
24610 72 6e 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f rned.. */
24620 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 62 45 . if( !bE
24630 78 74 65 6e 64 20 29 7b 0a 20 20 20 20 20 20 20 xtend ){.
24640 20 20 20 67 6f 74 6f 20 73 68 6d 70 61 67 65 5f goto shmpage_
24650 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a out;. }..
24660 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 74 65 72 /* Alter
24670 6e 61 74 69 76 65 6c 79 2c 20 69 66 20 62 45 78 natively, if bEx
24680 74 65 6e 64 20 69 73 20 74 72 75 65 2c 20 65 78 tend is true, ex
24690 74 65 6e 64 20 74 68 65 20 66 69 6c 65 2e 20 44 tend the file. D
246a0 6f 20 74 68 69 73 20 62 79 0a 20 20 20 20 20 20 o this by.
246b0 20 20 2a 2a 20 77 72 69 74 69 6e 67 20 61 20 73 ** writing a s
246c0 69 6e 67 6c 65 20 62 79 74 65 20 74 6f 20 74 68 ingle byte to th
246d0 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20 28 4f e end of each (O
246e0 53 29 20 70 61 67 65 20 62 65 69 6e 67 0a 20 20 S) page being.
246f0 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 ** allocat
24700 65 64 20 6f 72 20 65 78 74 65 6e 64 65 64 2e 20 ed or extended.
24710 54 65 63 68 6e 69 63 61 6c 6c 79 2c 20 77 65 20 Technically, we
24720 6e 65 65 64 20 6f 6e 6c 79 20 77 72 69 74 65 20 need only write
24730 74 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a to the. *
24740 2a 20 6c 61 73 74 20 70 61 67 65 20 69 6e 20 6f * last page in o
24750 72 64 65 72 20 74 6f 20 65 78 74 65 6e 64 20 74 rder to extend t
24760 68 65 20 66 69 6c 65 2e 20 42 75 74 20 77 72 69 he file. But wri
24770 74 69 6e 67 20 74 6f 20 61 6c 6c 20 6e 65 77 0a ting to all new.
24780 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 73 ** pages
24790 20 66 6f 72 63 65 73 20 74 68 65 20 4f 53 20 74 forces the OS t
247a0 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 6d 20 o allocate them
247b0 69 6d 6d 65 64 69 61 74 65 6c 79 2c 20 77 68 69 immediately, whi
247c0 63 68 20 72 65 64 75 63 65 73 0a 20 20 20 20 20 ch reduces.
247d0 20 20 20 2a 2a 20 74 68 65 20 63 68 61 6e 63 65 ** the chance
247e0 73 20 6f 66 20 53 49 47 42 55 53 20 77 68 69 6c s of SIGBUS whil
247f0 65 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 e accessing the
24800 6d 61 70 70 65 64 20 72 65 67 69 6f 6e 20 6c 61 mapped region la
24810 74 65 72 20 6f 6e 2e 0a 20 20 20 20 20 20 20 20 ter on..
24820 2a 2f 0a 20 20 20 20 20 20 20 20 65 6c 73 65 7b */. else{
24830 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69 . stati
24840 63 20 63 6f 6e 73 74 20 69 6e 74 20 70 67 73 7a c const int pgsz
24850 20 3d 20 34 30 39 36 3b 0a 20 20 20 20 20 20 20 = 4096;.
24860 20 20 20 69 6e 74 20 69 50 67 3b 0a 0a 20 20 20 int iPg;..
24870 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
24880 74 6f 20 74 68 65 20 6c 61 73 74 20 62 79 74 65 to the last byte
24890 20 6f 66 20 65 61 63 68 20 6e 65 77 6c 79 20 61 of each newly a
248a0 6c 6c 6f 63 61 74 65 64 20 6f 72 20 65 78 74 65 llocated or exte
248b0 6e 64 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 20 nded page */.
248c0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 assert( (
248d0 6e 42 79 74 65 20 25 20 70 67 73 7a 29 3d 3d 30 nByte % pgsz)==0
248e0 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f );. fo
248f0 72 28 69 50 67 3d 28 73 53 74 61 74 2e 73 74 5f r(iPg=(sStat.st_
24900 73 69 7a 65 2f 70 67 73 7a 29 3b 20 69 50 67 3c size/pgsz); iPg<
24910 28 6e 42 79 74 65 2f 70 67 73 7a 29 3b 20 69 50 (nByte/pgsz); iP
24920 67 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 g++){.
24930 20 20 69 6e 74 20 78 20 3d 20 30 3b 0a 20 20 20 int x = 0;.
24940 20 20 20 20 20 20 20 20 20 69 66 28 20 73 65 65 if( see
24950 6b 41 6e 64 57 72 69 74 65 46 64 28 70 53 68 6d kAndWriteFd(pShm
24960 4e 6f 64 65 2d 3e 68 2c 20 69 50 67 2a 70 67 73 Node->h, iPg*pgs
24970 7a 20 2b 20 70 67 73 7a 2d 31 2c 20 22 22 2c 20 z + pgsz-1, "",
24980 31 2c 20 26 78 29 21 3d 31 20 29 7b 0a 20 20 20 1, &x)!=1 ){.
24990 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 const
249a0 20 63 68 61 72 20 2a 7a 46 69 6c 65 20 3d 20 70 char *zFile = p
249b0 53 68 6d 4e 6f 64 65 2d 3e 7a 46 69 6c 65 6e 61 ShmNode->zFilena
249c0 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 me;.
249d0 20 20 72 63 20 3d 20 75 6e 69 78 4c 6f 67 45 72 rc = unixLogEr
249e0 72 6f 72 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 ror(SQLITE_IOERR
249f0 5f 53 48 4d 53 49 5a 45 2c 20 22 77 72 69 74 65 _SHMSIZE, "write
24a00 22 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 20 20 20 ", zFile);.
24a10 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73 68 goto sh
24a20 6d 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 20 mpage_out;.
24a30 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
24a40 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
24a50 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 }. }..
24a60 20 20 2f 2a 20 4d 61 70 20 74 68 65 20 72 65 71 /* Map the req
24a70 75 65 73 74 65 64 20 6d 65 6d 6f 72 79 20 72 65 uested memory re
24a80 67 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 70 gion into this p
24a90 72 6f 63 65 73 73 65 73 20 61 64 64 72 65 73 73 rocesses address
24aa0 20 73 70 61 63 65 2e 20 2a 2f 0a 20 20 20 20 61 space. */. a
24ab0 70 4e 65 77 20 3d 20 28 63 68 61 72 20 2a 2a 29 pNew = (char **)
24ac0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
24ad0 0a 20 20 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 . pShmNod
24ae0 65 2d 3e 61 70 52 65 67 69 6f 6e 2c 20 6e 52 65 e->apRegion, nRe
24af0 71 52 65 67 69 6f 6e 2a 73 69 7a 65 6f 66 28 63 qRegion*sizeof(c
24b00 68 61 72 20 2a 29 0a 20 20 20 20 29 3b 0a 20 20 har *). );.
24b10 20 20 69 66 28 20 21 61 70 4e 65 77 20 29 7b 0a if( !apNew ){.
24b20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
24b30 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 5f 42 4b E_IOERR_NOMEM_BK
24b40 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 PT;. goto s
24b50 68 6d 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 hmpage_out;.
24b60 7d 0a 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e }. pShmNode->
24b70 61 70 52 65 67 69 6f 6e 20 3d 20 61 70 4e 65 77 apRegion = apNew
24b80 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 53 68 ;. while( pSh
24b90 6d 4e 6f 64 65 2d 3e 6e 52 65 67 69 6f 6e 3c 6e mNode->nRegion<n
24ba0 52 65 71 52 65 67 69 6f 6e 20 29 7b 0a 20 20 20 ReqRegion ){.
24bb0 20 20 20 69 6e 74 20 6e 4d 61 70 20 3d 20 73 7a int nMap = sz
24bc0 52 65 67 69 6f 6e 2a 6e 53 68 6d 50 65 72 4d 61 Region*nShmPerMa
24bd0 70 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a p;. int i;.
24be0 20 20 20 20 20 20 76 6f 69 64 20 2a 70 4d 65 6d void *pMem
24bf0 3b 0a 20 20 20 20 20 20 69 66 28 20 70 53 68 6d ;. if( pShm
24c00 4e 6f 64 65 2d 3e 68 3e 3d 30 20 29 7b 0a 20 20 Node->h>=0 ){.
24c10 20 20 20 20 20 20 70 4d 65 6d 20 3d 20 6f 73 4d pMem = osM
24c20 6d 61 70 28 30 2c 20 6e 4d 61 70 2c 0a 20 20 20 map(0, nMap,.
24c30 20 20 20 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 pShmNod
24c40 65 2d 3e 69 73 52 65 61 64 6f 6e 6c 79 20 3f 20 e->isReadonly ?
24c50 50 52 4f 54 5f 52 45 41 44 20 3a 20 50 52 4f 54 PROT_READ : PROT
24c60 5f 52 45 41 44 7c 50 52 4f 54 5f 57 52 49 54 45 _READ|PROT_WRITE
24c70 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 4d , . M
24c80 41 50 5f 53 48 41 52 45 44 2c 20 70 53 68 6d 4e AP_SHARED, pShmN
24c90 6f 64 65 2d 3e 68 2c 20 73 7a 52 65 67 69 6f 6e ode->h, szRegion
24ca0 2a 28 69 36 34 29 70 53 68 6d 4e 6f 64 65 2d 3e *(i64)pShmNode->
24cb0 6e 52 65 67 69 6f 6e 0a 20 20 20 20 20 20 20 20 nRegion.
24cc0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
24cd0 4d 65 6d 3d 3d 4d 41 50 5f 46 41 49 4c 45 44 20 Mem==MAP_FAILED
24ce0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
24cf0 3d 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 28 53 = unixLogError(S
24d00 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d 4d QLITE_IOERR_SHMM
24d10 41 50 2c 20 22 6d 6d 61 70 22 2c 20 70 53 68 6d AP, "mmap", pShm
24d20 4e 6f 64 65 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 Node->zFilename)
24d30 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f ;. goto
24d40 20 73 68 6d 70 61 67 65 5f 6f 75 74 3b 0a 20 20 shmpage_out;.
24d50 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 }. }e
24d60 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 lse{. pMe
24d70 6d 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c m = sqlite3_mall
24d80 6f 63 36 34 28 73 7a 52 65 67 69 6f 6e 29 3b 0a oc64(szRegion);.
24d90 20 20 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d if( pMem
24da0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
24db0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
24dc0 45 4d 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 EM_BKPT;.
24dd0 20 20 20 67 6f 74 6f 20 73 68 6d 70 61 67 65 5f goto shmpage_
24de0 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 out;. }.
24df0 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 4d memset(pM
24e00 65 6d 2c 20 30 2c 20 73 7a 52 65 67 69 6f 6e 29 em, 0, szRegion)
24e10 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
24e20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53 68 6d for(i=0; i<nShm
24e30 50 65 72 4d 61 70 3b 20 69 2b 2b 29 7b 0a 20 20 PerMap; i++){.
24e40 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e pShmNode->
24e50 61 70 52 65 67 69 6f 6e 5b 70 53 68 6d 4e 6f 64 apRegion[pShmNod
24e60 65 2d 3e 6e 52 65 67 69 6f 6e 2b 69 5d 20 3d 20 e->nRegion+i] =
24e70 26 28 28 63 68 61 72 2a 29 70 4d 65 6d 29 5b 73 &((char*)pMem)[s
24e80 7a 52 65 67 69 6f 6e 2a 69 5d 3b 0a 20 20 20 20 zRegion*i];.
24e90 20 20 7d 0a 20 20 20 20 20 20 70 53 68 6d 4e 6f }. pShmNo
24ea0 64 65 2d 3e 6e 52 65 67 69 6f 6e 20 2b 3d 20 6e de->nRegion += n
24eb0 53 68 6d 50 65 72 4d 61 70 3b 0a 20 20 20 20 7d ShmPerMap;. }
24ec0 0a 20 20 7d 0a 0a 73 68 6d 70 61 67 65 5f 6f 75 . }..shmpage_ou
24ed0 74 3a 0a 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 t:. if( pShmNod
24ee0 65 2d 3e 6e 52 65 67 69 6f 6e 3e 69 52 65 67 69 e->nRegion>iRegi
24ef0 6f 6e 20 29 7b 0a 20 20 20 20 2a 70 70 20 3d 20 on ){. *pp =
24f00 70 53 68 6d 4e 6f 64 65 2d 3e 61 70 52 65 67 69 pShmNode->apRegi
24f10 6f 6e 5b 69 52 65 67 69 6f 6e 5d 3b 0a 20 20 7d on[iRegion];. }
24f20 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 20 3d 20 else{. *pp =
24f30 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 68 0;. }. if( pSh
24f40 6d 4e 6f 64 65 2d 3e 69 73 52 65 61 64 6f 6e 6c mNode->isReadonl
24f50 79 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f y && rc==SQLITE_
24f60 4f 4b 20 29 20 72 63 20 3d 20 53 51 4c 49 54 45 OK ) rc = SQLITE
24f70 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 73 71 6c _READONLY;. sql
24f80 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
24f90 28 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 (pShmNode->mutex
24fa0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
24fb0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 }../*.** Change
24fc0 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 66 the lock state f
24fd0 6f 72 20 61 20 73 68 61 72 65 64 2d 6d 65 6d 6f or a shared-memo
24fe0 72 79 20 73 65 67 6d 65 6e 74 2e 0a 2a 2a 0a 2a ry segment..**.*
24ff0 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 * Note that the
25000 72 65 6c 61 74 69 6f 6e 73 68 69 70 20 62 65 74 relationship bet
25010 77 65 65 6e 20 53 48 41 52 45 64 20 61 6e 64 20 ween SHAREd and
25020 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 20 EXCLUSIVE locks
25030 69 73 20 61 20 6c 69 74 74 6c 65 0a 2a 2a 20 64 is a little.** d
25040 69 66 66 65 72 65 6e 74 20 68 65 72 65 20 74 68 ifferent here th
25050 61 6e 20 69 6e 20 70 6f 73 69 78 2e 20 20 49 6e an in posix. In
25060 20 78 53 68 6d 4c 6f 63 6b 28 29 2c 20 6f 6e 65 xShmLock(), one
25070 20 63 61 6e 20 67 6f 20 66 72 6f 6d 20 75 6e 6c can go from unl
25080 6f 63 6b 65 64 0a 2a 2a 20 74 6f 20 73 68 61 72 ocked.** to shar
25090 65 64 20 61 6e 64 20 62 61 63 6b 20 6f 72 20 66 ed and back or f
250a0 72 6f 6d 20 75 6e 6c 6f 63 6b 65 64 20 74 6f 20 rom unlocked to
250b0 65 78 63 6c 75 73 69 76 65 20 61 6e 64 20 62 61 exclusive and ba
250c0 63 6b 2e 20 20 42 75 74 20 6f 6e 65 20 6d 61 79 ck. But one may
250d0 0a 2a 2a 20 6e 6f 74 20 67 6f 20 66 72 6f 6d 20 .** not go from
250e0 73 68 61 72 65 64 20 74 6f 20 65 78 63 6c 75 73 shared to exclus
250f0 69 76 65 20 6f 72 20 66 72 6f 6d 20 65 78 63 6c ive or from excl
25100 75 73 69 76 65 20 74 6f 20 73 68 61 72 65 64 2e usive to shared.
25110 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
25120 6e 69 78 53 68 6d 4c 6f 63 6b 28 0a 20 20 73 71 nixShmLock(. sq
25130 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 lite3_file *fd,
25140 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
25150 62 61 73 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e base file holdin
25160 67 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d g the shared mem
25170 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 73 ory */. int ofs
25180 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t,
25190 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6c 6f 63 /* First loc
251a0 6b 20 74 6f 20 61 63 71 75 69 72 65 20 6f 72 20 k to acquire or
251b0 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 release */. int
251c0 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 n,
251d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
251e0 72 20 6f 66 20 6c 6f 63 6b 73 20 74 6f 20 61 63 r of locks to ac
251f0 71 75 69 72 65 20 6f 72 20 72 65 6c 65 61 73 65 quire or release
25200 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 */. int flags
25210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
25220 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 /* What to do w
25230 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a ith the lock */.
25240 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 ){. unixFile *p
25250 44 62 46 64 20 3d 20 28 75 6e 69 78 46 69 6c 65 DbFd = (unixFile
25260 2a 29 66 64 3b 20 20 20 20 20 20 2f 2a 20 43 6f *)fd; /* Co
25270 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 69 6e 67 nnection holding
25280 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 2a shared memory *
25290 2f 0a 20 20 75 6e 69 78 53 68 6d 20 2a 70 20 3d /. unixShm *p =
252a0 20 70 44 62 46 64 2d 3e 70 53 68 6d 3b 20 20 20 pDbFd->pShm;
252b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
252c0 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 62 shared memory b
252d0 65 69 6e 67 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 eing locked */.
252e0 20 75 6e 69 78 53 68 6d 20 2a 70 58 3b 20 20 20 unixShm *pX;
252f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
25300 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f /* For lo
25310 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 73 oping over all s
25320 69 62 6c 69 6e 67 73 20 2a 2f 0a 20 20 75 6e 69 iblings */. uni
25330 78 53 68 6d 4e 6f 64 65 20 2a 70 53 68 6d 4e 6f xShmNode *pShmNo
25340 64 65 20 3d 20 70 2d 3e 70 53 68 6d 4e 6f 64 65 de = p->pShmNode
25350 3b 20 20 2f 2a 20 54 68 65 20 75 6e 64 65 72 6c ; /* The underl
25360 79 69 6e 67 20 66 69 6c 65 20 69 4e 6f 64 65 20 ying file iNode
25370 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 */. int rc = SQ
25380 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 LITE_OK;
25390 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
253a0 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 20 20 75 sult code */. u
253b0 31 36 20 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 16 mask;
253c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
253d0 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 /* Mask of
253e0 6c 6f 63 6b 73 20 74 6f 20 74 61 6b 65 20 6f 72 locks to take or
253f0 20 72 65 6c 65 61 73 65 20 2a 2f 0a 0a 20 20 61 release */.. a
25400 73 73 65 72 74 28 20 70 53 68 6d 4e 6f 64 65 3d ssert( pShmNode=
25410 3d 70 44 62 46 64 2d 3e 70 49 6e 6f 64 65 2d 3e =pDbFd->pInode->
25420 70 53 68 6d 4e 6f 64 65 20 29 3b 0a 20 20 61 73 pShmNode );. as
25430 73 65 72 74 28 20 70 53 68 6d 4e 6f 64 65 2d 3e sert( pShmNode->
25440 70 49 6e 6f 64 65 3d 3d 70 44 62 46 64 2d 3e 70 pInode==pDbFd->p
25450 49 6e 6f 64 65 20 29 3b 0a 20 20 61 73 73 65 72 Inode );. asser
25460 74 28 20 6f 66 73 74 3e 3d 30 20 26 26 20 6f 66 t( ofst>=0 && of
25470 73 74 2b 6e 3c 3d 53 51 4c 49 54 45 5f 53 48 4d st+n<=SQLITE_SHM
25480 5f 4e 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 _NLOCK );. asse
25490 72 74 28 20 6e 3e 3d 31 20 29 3b 0a 20 20 61 73 rt( n>=1 );. as
254a0 73 65 72 74 28 20 66 6c 61 67 73 3d 3d 28 53 51 sert( flags==(SQ
254b0 4c 49 54 45 5f 53 48 4d 5f 4c 4f 43 4b 20 7c 20 LITE_SHM_LOCK |
254c0 53 51 4c 49 54 45 5f 53 48 4d 5f 53 48 41 52 45 SQLITE_SHM_SHARE
254d0 44 29 0a 20 20 20 20 20 20 20 7c 7c 20 66 6c 61 D). || fla
254e0 67 73 3d 3d 28 53 51 4c 49 54 45 5f 53 48 4d 5f gs==(SQLITE_SHM_
254f0 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53 48 LOCK | SQLITE_SH
25500 4d 5f 45 58 43 4c 55 53 49 56 45 29 0a 20 20 20 M_EXCLUSIVE).
25510 20 20 20 20 7c 7c 20 66 6c 61 67 73 3d 3d 28 53 || flags==(S
25520 51 4c 49 54 45 5f 53 48 4d 5f 55 4e 4c 4f 43 4b QLITE_SHM_UNLOCK
25530 20 7c 20 53 51 4c 49 54 45 5f 53 48 4d 5f 53 48 | SQLITE_SHM_SH
25540 41 52 45 44 29 0a 20 20 20 20 20 20 20 7c 7c 20 ARED). ||
25550 66 6c 61 67 73 3d 3d 28 53 51 4c 49 54 45 5f 53 flags==(SQLITE_S
25560 48 4d 5f 55 4e 4c 4f 43 4b 20 7c 20 53 51 4c 49 HM_UNLOCK | SQLI
25570 54 45 5f 53 48 4d 5f 45 58 43 4c 55 53 49 56 45 TE_SHM_EXCLUSIVE
25580 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e ) );. assert( n
25590 3d 3d 31 20 7c 7c 20 28 66 6c 61 67 73 20 26 20 ==1 || (flags &
255a0 53 51 4c 49 54 45 5f 53 48 4d 5f 45 58 43 4c 55 SQLITE_SHM_EXCLU
255b0 53 49 56 45 29 21 3d 30 20 29 3b 0a 20 20 61 73 SIVE)!=0 );. as
255c0 73 65 72 74 28 20 70 53 68 6d 4e 6f 64 65 2d 3e sert( pShmNode->
255d0 68 3e 3d 30 20 7c 7c 20 70 44 62 46 64 2d 3e 70 h>=0 || pDbFd->p
255e0 49 6e 6f 64 65 2d 3e 62 50 72 6f 63 65 73 73 4c Inode->bProcessL
255f0 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 ock==1 );. asse
25600 72 74 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 68 3c rt( pShmNode->h<
25610 30 20 7c 7c 20 70 44 62 46 64 2d 3e 70 49 6e 6f 0 || pDbFd->pIno
25620 64 65 2d 3e 62 50 72 6f 63 65 73 73 4c 6f 63 6b de->bProcessLock
25630 3d 3d 30 20 29 3b 0a 0a 20 20 6d 61 73 6b 20 3d ==0 );.. mask =
25640 20 28 31 3c 3c 28 6f 66 73 74 2b 6e 29 29 20 2d (1<<(ofst+n)) -
25650 20 28 31 3c 3c 6f 66 73 74 29 3b 0a 20 20 61 73 (1<<ofst);. as
25660 73 65 72 74 28 20 6e 3e 31 20 7c 7c 20 6d 61 73 sert( n>1 || mas
25670 6b 3d 3d 28 31 3c 3c 6f 66 73 74 29 20 29 3b 0a k==(1<<ofst) );.
25680 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
25690 65 6e 74 65 72 28 70 53 68 6d 4e 6f 64 65 2d 3e enter(pShmNode->
256a0 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 66 6c mutex);. if( fl
256b0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 48 4d ags & SQLITE_SHM
256c0 5f 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 75 _UNLOCK ){. u
256d0 31 36 20 61 6c 6c 4d 61 73 6b 20 3d 20 30 3b 20 16 allMask = 0;
256e0 2f 2a 20 4d 61 73 6b 20 6f 66 20 6c 6f 63 6b 73 /* Mask of locks
256f0 20 68 65 6c 64 20 62 79 20 73 69 62 6c 69 6e 67 held by sibling
25700 73 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 65 s */.. /* See
25710 20 69 66 20 61 6e 79 20 73 69 62 6c 69 6e 67 73 if any siblings
25720 20 68 6f 6c 64 20 74 68 69 73 20 73 61 6d 65 20 hold this same
25730 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 66 6f 72 28 lock */. for(
25740 70 58 3d 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 pX=pShmNode->pFi
25750 72 73 74 3b 20 70 58 3b 20 70 58 3d 70 58 2d 3e rst; pX; pX=pX->
25760 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 pNext){. if
25770 28 20 70 58 3d 3d 70 20 29 20 63 6f 6e 74 69 6e ( pX==p ) contin
25780 75 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ue;. assert
25790 28 20 28 70 58 2d 3e 65 78 63 6c 4d 61 73 6b 20 ( (pX->exclMask
257a0 26 20 28 70 2d 3e 65 78 63 6c 4d 61 73 6b 7c 70 & (p->exclMask|p
257b0 2d 3e 73 68 61 72 65 64 4d 61 73 6b 29 29 3d 3d ->sharedMask))==
257c0 30 20 29 3b 0a 20 20 20 20 20 20 61 6c 6c 4d 61 0 );. allMa
257d0 73 6b 20 7c 3d 20 70 58 2d 3e 73 68 61 72 65 64 sk |= pX->shared
257e0 4d 61 73 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 Mask;. }..
257f0 20 2f 2a 20 55 6e 6c 6f 63 6b 20 74 68 65 20 73 /* Unlock the s
25800 79 73 74 65 6d 2d 6c 65 76 65 6c 20 6c 6f 63 6b ystem-level lock
25810 73 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 6d 61 s */. if( (ma
25820 73 6b 20 26 20 61 6c 6c 4d 61 73 6b 29 3d 3d 30 sk & allMask)==0
25830 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 75 ){. rc = u
25840 6e 69 78 53 68 6d 53 79 73 74 65 6d 4c 6f 63 6b nixShmSystemLock
25850 28 70 44 62 46 64 2c 20 46 5f 55 4e 4c 43 4b 2c (pDbFd, F_UNLCK,
25860 20 6f 66 73 74 2b 55 4e 49 58 5f 53 48 4d 5f 42 ofst+UNIX_SHM_B
25870 41 53 45 2c 20 6e 29 3b 0a 20 20 20 20 7d 65 6c ASE, n);. }el
25880 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 se{. rc = S
25890 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
258a0 0a 20 20 20 20 2f 2a 20 55 6e 64 6f 20 74 68 65 . /* Undo the
258b0 20 6c 6f 63 61 6c 20 6c 6f 63 6b 73 20 2a 2f 0a local locks */.
258c0 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
258d0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 TE_OK ){. p
258e0 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 3d 20 7e 6d ->exclMask &= ~m
258f0 61 73 6b 3b 0a 20 20 20 20 20 20 70 2d 3e 73 68 ask;. p->sh
25900 61 72 65 64 4d 61 73 6b 20 26 3d 20 7e 6d 61 73 aredMask &= ~mas
25910 6b 3b 0a 20 20 20 20 7d 20 0a 20 20 7d 65 6c 73 k;. } . }els
25920 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 e if( flags & SQ
25930 4c 49 54 45 5f 53 48 4d 5f 53 48 41 52 45 44 20 LITE_SHM_SHARED
25940 29 7b 0a 20 20 20 20 75 31 36 20 61 6c 6c 53 68 ){. u16 allSh
25950 61 72 65 64 20 3d 20 30 3b 20 20 2f 2a 20 55 6e ared = 0; /* Un
25960 69 6f 6e 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c ion of locks hel
25970 64 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 d by connections
25980 20 6f 74 68 65 72 20 74 68 61 6e 20 22 70 22 20 other than "p"
25990 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 */.. /* Find
259a0 6f 75 74 20 77 68 69 63 68 20 73 68 61 72 65 64 out which shared
259b0 20 6c 6f 63 6b 73 20 61 72 65 20 61 6c 72 65 61 locks are alrea
259c0 64 79 20 68 65 6c 64 20 62 79 20 73 69 62 6c 69 dy held by sibli
259d0 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a ng connections..
259e0 20 20 20 20 2a 2a 20 49 66 20 61 6e 79 20 73 69 ** If any si
259f0 62 6c 69 6e 67 20 61 6c 72 65 61 64 79 20 68 6f bling already ho
25a00 6c 64 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 lds an exclusive
25a10 20 6c 6f 63 6b 2c 20 67 6f 20 61 68 65 61 64 20 lock, go ahead
25a20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 20 20 2a and return. *
25a30 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a 20 * SQLITE_BUSY..
25a40 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 58 */. for(pX
25a50 3d 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73 =pShmNode->pFirs
25a60 74 3b 20 70 58 3b 20 70 58 3d 70 58 2d 3e 70 4e t; pX; pX=pX->pN
25a70 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ext){. if(
25a80 28 70 58 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 20 (pX->exclMask &
25a90 6d 61 73 6b 29 21 3d 30 20 29 7b 0a 20 20 20 20 mask)!=0 ){.
25aa0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
25ab0 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 62 72 BUSY;. br
25ac0 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
25ad0 20 20 20 61 6c 6c 53 68 61 72 65 64 20 7c 3d 20 allShared |=
25ae0 70 58 2d 3e 73 68 61 72 65 64 4d 61 73 6b 3b 0a pX->sharedMask;.
25af0 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 47 65 }.. /* Ge
25b00 74 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 61 t shared locks a
25b10 74 20 74 68 65 20 73 79 73 74 65 6d 20 6c 65 76 t the system lev
25b20 65 6c 2c 20 69 66 20 6e 65 63 65 73 73 61 72 79 el, if necessary
25b30 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d */. if( rc==
25b40 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
25b50 20 20 20 69 66 28 20 28 61 6c 6c 53 68 61 72 65 if( (allShare
25b60 64 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a d & mask)==0 ){.
25b70 20 20 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69 rc = uni
25b80 78 53 68 6d 53 79 73 74 65 6d 4c 6f 63 6b 28 70 xShmSystemLock(p
25b90 44 62 46 64 2c 20 46 5f 52 44 4c 43 4b 2c 20 6f DbFd, F_RDLCK, o
25ba0 66 73 74 2b 55 4e 49 58 5f 53 48 4d 5f 42 41 53 fst+UNIX_SHM_BAS
25bb0 45 2c 20 6e 29 3b 0a 20 20 20 20 20 20 7d 65 6c E, n);. }el
25bc0 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d se{. rc =
25bd0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
25be0 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f }. }.. /
25bf0 2a 20 47 65 74 20 74 68 65 20 6c 6f 63 61 6c 20 * Get the local
25c00 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 2a 2f 0a shared locks */.
25c10 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
25c20 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 TE_OK ){. p
25c30 2d 3e 73 68 61 72 65 64 4d 61 73 6b 20 7c 3d 20 ->sharedMask |=
25c40 6d 61 73 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 mask;. }. }e
25c50 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 lse{. /* Make
25c60 20 73 75 72 65 20 6e 6f 20 73 69 62 6c 69 6e 67 sure no sibling
25c70 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 68 6f 6c connections hol
25c80 64 20 6c 6f 63 6b 73 20 74 68 61 74 20 77 69 6c d locks that wil
25c90 6c 20 62 6c 6f 63 6b 20 74 68 69 73 0a 20 20 20 l block this.
25ca0 20 2a 2a 20 6c 6f 63 6b 2e 20 20 49 66 20 61 6e ** lock. If an
25cb0 79 20 64 6f 2c 20 72 65 74 75 72 6e 20 53 51 4c y do, return SQL
25cc0 49 54 45 5f 42 55 53 59 20 72 69 67 68 74 20 61 ITE_BUSY right a
25cd0 77 61 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 way.. */.
25ce0 66 6f 72 28 70 58 3d 70 53 68 6d 4e 6f 64 65 2d for(pX=pShmNode-
25cf0 3e 70 46 69 72 73 74 3b 20 70 58 3b 20 70 58 3d >pFirst; pX; pX=
25d00 70 58 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 pX->pNext){.
25d10 20 20 69 66 28 20 28 70 58 2d 3e 65 78 63 6c 4d if( (pX->exclM
25d20 61 73 6b 20 26 20 6d 61 73 6b 29 21 3d 30 20 7c ask & mask)!=0 |
25d30 7c 20 28 70 58 2d 3e 73 68 61 72 65 64 4d 61 73 | (pX->sharedMas
25d40 6b 20 26 20 6d 61 73 6b 29 21 3d 30 20 29 7b 0a k & mask)!=0 ){.
25d50 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
25d60 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 ITE_BUSY;.
25d70 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
25d80 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a . }. . /*
25d90 20 47 65 74 20 74 68 65 20 65 78 63 6c 75 73 69 Get the exclusi
25da0 76 65 20 6c 6f 63 6b 73 20 61 74 20 74 68 65 20 ve locks at the
25db0 73 79 73 74 65 6d 20 6c 65 76 65 6c 2e 20 20 54 system level. T
25dc0 68 65 6e 20 69 66 20 73 75 63 63 65 73 73 66 75 hen if successfu
25dd0 6c 0a 20 20 20 20 2a 2a 20 61 6c 73 6f 20 6d 61 l. ** also ma
25de0 72 6b 20 74 68 65 20 6c 6f 63 61 6c 20 63 6f 6e rk the local con
25df0 6e 65 63 74 69 6f 6e 20 61 73 20 62 65 69 6e 67 nection as being
25e00 20 6c 6f 63 6b 65 64 2e 0a 20 20 20 20 2a 2f 0a locked.. */.
25e10 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
25e20 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
25e30 63 20 3d 20 75 6e 69 78 53 68 6d 53 79 73 74 65 c = unixShmSyste
25e40 6d 4c 6f 63 6b 28 70 44 62 46 64 2c 20 46 5f 57 mLock(pDbFd, F_W
25e50 52 4c 43 4b 2c 20 6f 66 73 74 2b 55 4e 49 58 5f RLCK, ofst+UNIX_
25e60 53 48 4d 5f 42 41 53 45 2c 20 6e 29 3b 0a 20 20 SHM_BASE, n);.
25e70 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
25e80 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
25e90 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73 68 61 assert( (p->sha
25ea0 72 65 64 4d 61 73 6b 20 26 20 6d 61 73 6b 29 3d redMask & mask)=
25eb0 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d =0 );. p-
25ec0 3e 65 78 63 6c 4d 61 73 6b 20 7c 3d 20 6d 61 73 >exclMask |= mas
25ed0 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
25ee0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d . }. sqlite3_m
25ef0 75 74 65 78 5f 6c 65 61 76 65 28 70 53 68 6d 4e utex_leave(pShmN
25f00 6f 64 65 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 4f ode->mutex);. O
25f10 53 54 52 41 43 45 28 28 22 53 48 4d 2d 4c 4f 43 STRACE(("SHM-LOC
25f20 4b 20 73 68 6d 69 64 2d 25 64 2c 20 70 69 64 2d K shmid-%d, pid-
25f30 25 64 20 67 6f 74 20 25 30 33 78 2c 25 30 33 78 %d got %03x,%03x
25f40 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
25f50 70 2d 3e 69 64 2c 20 6f 73 47 65 74 70 69 64 28 p->id, osGetpid(
25f60 30 29 2c 20 70 2d 3e 73 68 61 72 65 64 4d 61 73 0), p->sharedMas
25f70 6b 2c 20 70 2d 3e 65 78 63 6c 4d 61 73 6b 29 29 k, p->exclMask))
25f80 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
25f90 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
25fa0 74 20 61 20 6d 65 6d 6f 72 79 20 62 61 72 72 69 t a memory barri
25fb0 65 72 20 6f 72 20 6d 65 6d 6f 72 79 20 66 65 6e er or memory fen
25fc0 63 65 20 6f 6e 20 73 68 61 72 65 64 20 6d 65 6d ce on shared mem
25fd0 6f 72 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 6c 6c ory. .**.** All
25fe0 20 6c 6f 61 64 73 20 61 6e 64 20 73 74 6f 72 65 loads and store
25ff0 73 20 62 65 67 75 6e 20 62 65 66 6f 72 65 20 74 s begun before t
26000 68 65 20 62 61 72 72 69 65 72 20 6d 75 73 74 20 he barrier must
26010 63 6f 6d 70 6c 65 74 65 20 62 65 66 6f 72 65 0a complete before.
26020 2a 2a 20 61 6e 79 20 6c 6f 61 64 20 6f 72 20 73 ** any load or s
26030 74 6f 72 65 20 62 65 67 75 6e 20 61 66 74 65 72 tore begun after
26040 20 74 68 65 20 62 61 72 72 69 65 72 2e 0a 2a 2f the barrier..*/
26050 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 .static void uni
26060 78 53 68 6d 42 61 72 72 69 65 72 28 0a 20 20 73 xShmBarrier(. s
26070 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 20 qlite3_file *fd
26080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
26090 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 20 * Database file
260a0 68 6f 6c 64 69 6e 67 20 74 68 65 20 73 68 61 72 holding the shar
260b0 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 29 7b 0a ed memory */.){.
260c0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
260d0 45 52 28 66 64 29 3b 0a 20 20 73 71 6c 69 74 65 ER(fd);. sqlite
260e0 33 4d 65 6d 6f 72 79 42 61 72 72 69 65 72 28 29 3MemoryBarrier()
260f0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6d ; /* com
26100 70 69 6c 65 72 2d 64 65 66 69 6e 65 64 20 6d 65 piler-defined me
26110 6d 6f 72 79 20 62 61 72 72 69 65 72 20 2a 2f 0a mory barrier */.
26120 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 unixEnterMutex
26130 28 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ();
26140 20 20 2f 2a 20 41 6c 73 6f 20 6d 75 74 65 78 2c /* Also mutex,
26150 20 66 6f 72 20 72 65 64 75 6e 64 61 6e 63 79 20 for redundancy
26160 2a 2f 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 */. unixLeaveMu
26170 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 tex();.}../*.**
26180 43 6c 6f 73 65 20 61 20 63 6f 6e 6e 65 63 74 69 Close a connecti
26190 6f 6e 20 74 6f 20 73 68 61 72 65 64 2d 6d 65 6d on to shared-mem
261a0 6f 72 79 2e 20 20 44 65 6c 65 74 65 20 74 68 65 ory. Delete the
261b0 20 75 6e 64 65 72 6c 79 69 6e 67 20 0a 2a 2a 20 underlying .**
261c0 73 74 6f 72 61 67 65 20 69 66 20 64 65 6c 65 74 storage if delet
261d0 65 46 6c 61 67 20 69 73 20 74 72 75 65 2e 0a 2a eFlag is true..*
261e0 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 *.** If there is
261f0 20 6e 6f 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 no shared memor
26200 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 y associated wit
26210 68 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e h the connection
26220 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f then this.** ro
26230 75 74 69 6e 65 20 69 73 20 61 20 68 61 72 6d 6c utine is a harml
26240 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 ess no-op..*/.st
26250 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53 68 6d atic int unixShm
26260 55 6e 6d 61 70 28 0a 20 20 73 71 6c 69 74 65 33 Unmap(. sqlite3
26270 5f 66 69 6c 65 20 2a 66 64 2c 20 20 20 20 20 20 _file *fd,
26280 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
26290 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62 underlying datab
262a0 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e ase file */. in
262b0 74 20 64 65 6c 65 74 65 46 6c 61 67 20 20 20 20 t deleteFlag
262c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
262d0 20 44 65 6c 65 74 65 20 73 68 61 72 65 64 2d 6d Delete shared-m
262e0 65 6d 6f 72 79 20 69 66 20 74 72 75 65 20 2a 2f emory if true */
262f0 0a 29 7b 0a 20 20 75 6e 69 78 53 68 6d 20 2a 70 .){. unixShm *p
26300 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
26310 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e /* The con
26320 6e 65 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 6c nection to be cl
26330 6f 73 65 64 20 2a 2f 0a 20 20 75 6e 69 78 53 68 osed */. unixSh
26340 6d 4e 6f 64 65 20 2a 70 53 68 6d 4e 6f 64 65 3b mNode *pShmNode;
26350 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
26360 20 75 6e 64 65 72 6c 79 69 6e 67 20 73 68 61 72 underlying shar
26370 65 64 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 2a ed-memory file *
26380 2f 0a 20 20 75 6e 69 78 53 68 6d 20 2a 2a 70 70 /. unixShm **pp
26390 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
263a0 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 /* For loopi
263b0 6e 67 20 6f 76 65 72 20 73 69 62 6c 69 6e 67 20 ng over sibling
263c0 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 20 connections */.
263d0 20 75 6e 69 78 46 69 6c 65 20 2a 70 44 62 46 64 unixFile *pDbFd
263e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
263f0 20 2f 2a 20 54 68 65 20 75 6e 64 65 72 6c 79 69 /* The underlyi
26400 6e 67 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ng database file
26410 20 2a 2f 0a 0a 20 20 70 44 62 46 64 20 3d 20 28 */.. pDbFd = (
26420 75 6e 69 78 46 69 6c 65 2a 29 66 64 3b 0a 20 20 unixFile*)fd;.
26430 70 20 3d 20 70 44 62 46 64 2d 3e 70 53 68 6d 3b p = pDbFd->pShm;
26440 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
26450 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
26460 20 20 70 53 68 6d 4e 6f 64 65 20 3d 20 70 2d 3e pShmNode = p->
26470 70 53 68 6d 4e 6f 64 65 3b 0a 0a 20 20 61 73 73 pShmNode;.. ass
26480 65 72 74 28 20 70 53 68 6d 4e 6f 64 65 3d 3d 70 ert( pShmNode==p
26490 44 62 46 64 2d 3e 70 49 6e 6f 64 65 2d 3e 70 53 DbFd->pInode->pS
264a0 68 6d 4e 6f 64 65 20 29 3b 0a 20 20 61 73 73 65 hmNode );. asse
264b0 72 74 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 70 49 rt( pShmNode->pI
264c0 6e 6f 64 65 3d 3d 70 44 62 46 64 2d 3e 70 49 6e node==pDbFd->pIn
264d0 6f 64 65 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 6d ode );.. /* Rem
264e0 6f 76 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 ove connection p
264f0 20 66 72 6f 6d 20 74 68 65 20 73 65 74 20 6f 66 from the set of
26500 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 73 73 connections ass
26510 6f 63 69 61 74 65 64 0a 20 20 2a 2a 20 77 69 74 ociated. ** wit
26520 68 20 70 53 68 6d 4e 6f 64 65 20 2a 2f 0a 20 20 h pShmNode */.
26530 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
26540 74 65 72 28 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 ter(pShmNode->mu
26550 74 65 78 29 3b 0a 20 20 66 6f 72 28 70 70 3d 26 tex);. for(pp=&
26560 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73 74 pShmNode->pFirst
26570 3b 20 28 2a 70 70 29 21 3d 70 3b 20 70 70 20 3d ; (*pp)!=p; pp =
26580 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 7b &(*pp)->pNext){
26590 7d 0a 20 20 2a 70 70 20 3d 20 70 2d 3e 70 4e 65 }. *pp = p->pNe
265a0 78 74 3b 0a 0a 20 20 2f 2a 20 46 72 65 65 20 74 xt;.. /* Free t
265b0 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 20 he connection p
265c0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 */. sqlite3_fre
265d0 65 28 70 29 3b 0a 20 20 70 44 62 46 64 2d 3e 70 e(p);. pDbFd->p
265e0 53 68 6d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 Shm = 0;. sqlit
265f0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
26600 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b ShmNode->mutex);
26610 0a 0a 20 20 2f 2a 20 49 66 20 70 53 68 6d 4e 6f .. /* If pShmNo
26620 64 65 2d 3e 6e 52 65 66 20 68 61 73 20 72 65 61 de->nRef has rea
26630 63 68 65 64 20 30 2c 20 74 68 65 6e 20 63 6c 6f ched 0, then clo
26640 73 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e se the underlyin
26650 67 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 6d 65 g. ** shared-me
26660 6d 6f 72 79 20 66 69 6c 65 2c 20 74 6f 6f 20 2a mory file, too *
26670 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 /. unixEnterMut
26680 65 78 28 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ex();. assert(
26690 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 66 3e 30 pShmNode->nRef>0
266a0 20 29 3b 0a 20 20 70 53 68 6d 4e 6f 64 65 2d 3e );. pShmNode->
266b0 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 53 nRef--;. if( pS
266c0 68 6d 4e 6f 64 65 2d 3e 6e 52 65 66 3d 3d 30 20 hmNode->nRef==0
266d0 29 7b 0a 20 20 20 20 69 66 28 20 64 65 6c 65 74 ){. if( delet
266e0 65 46 6c 61 67 20 26 26 20 70 53 68 6d 4e 6f 64 eFlag && pShmNod
266f0 65 2d 3e 68 3e 3d 30 20 29 7b 0a 20 20 20 20 20 e->h>=0 ){.
26700 20 6f 73 55 6e 6c 69 6e 6b 28 70 53 68 6d 4e 6f osUnlink(pShmNo
26710 64 65 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a de->zFilename);.
26720 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 53 68 }. unixSh
26730 6d 50 75 72 67 65 28 70 44 62 46 64 29 3b 0a 20 mPurge(pDbFd);.
26740 20 7d 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 }. unixLeaveMu
26750 74 65 78 28 29 3b 0a 0a 20 20 72 65 74 75 72 6e tex();.. return
26760 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a SQLITE_OK;.}...
26770 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 75 #else.# define u
26780 6e 69 78 53 68 6d 4d 61 70 20 20 20 20 20 30 0a nixShmMap 0.
26790 23 20 64 65 66 69 6e 65 20 75 6e 69 78 53 68 6d # define unixShm
267a0 4c 6f 63 6b 20 20 20 20 30 0a 23 20 64 65 66 69 Lock 0.# defi
267b0 6e 65 20 75 6e 69 78 53 68 6d 42 61 72 72 69 65 ne unixShmBarrie
267c0 72 20 30 0a 23 20 64 65 66 69 6e 65 20 75 6e 69 r 0.# define uni
267d0 78 53 68 6d 55 6e 6d 61 70 20 20 20 30 0a 23 65 xShmUnmap 0.#e
267e0 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 ndif /* #ifndef
267f0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 20 SQLITE_OMIT_WAL
26800 2a 2f 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d */..#if SQLITE_M
26810 41 58 5f 4d 4d 41 50 5f 53 49 5a 45 3e 30 0a 2f AX_MMAP_SIZE>0./
26820 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 63 75 *.** If it is cu
26830 72 72 65 6e 74 6c 79 20 6d 65 6d 6f 72 79 20 6d rrently memory m
26840 61 70 70 65 64 2c 20 75 6e 6d 61 70 20 66 69 6c apped, unmap fil
26850 65 20 70 46 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 e pFd..*/.static
26860 20 76 6f 69 64 20 75 6e 69 78 55 6e 6d 61 70 66 void unixUnmapf
26870 69 6c 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 ile(unixFile *pF
26880 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 46 d){. assert( pF
26890 64 2d 3e 6e 46 65 74 63 68 4f 75 74 3d 3d 30 20 d->nFetchOut==0
268a0 29 3b 0a 20 20 69 66 28 20 70 46 64 2d 3e 70 4d );. if( pFd->pM
268b0 61 70 52 65 67 69 6f 6e 20 29 7b 0a 20 20 20 20 apRegion ){.
268c0 6f 73 4d 75 6e 6d 61 70 28 70 46 64 2d 3e 70 4d osMunmap(pFd->pM
268d0 61 70 52 65 67 69 6f 6e 2c 20 70 46 64 2d 3e 6d apRegion, pFd->m
268e0 6d 61 70 53 69 7a 65 41 63 74 75 61 6c 29 3b 0a mapSizeActual);.
268f0 20 20 20 20 70 46 64 2d 3e 70 4d 61 70 52 65 67 pFd->pMapReg
26900 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 70 46 64 ion = 0;. pFd
26910 2d 3e 6d 6d 61 70 53 69 7a 65 20 3d 20 30 3b 0a ->mmapSize = 0;.
26920 20 20 20 20 70 46 64 2d 3e 6d 6d 61 70 53 69 7a pFd->mmapSiz
26930 65 41 63 74 75 61 6c 20 3d 20 30 3b 0a 20 20 7d eActual = 0;. }
26940 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 .}../*.** Attemp
26950 74 20 74 6f 20 73 65 74 20 74 68 65 20 73 69 7a t to set the siz
26960 65 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 e of the memory
26970 6d 61 70 70 69 6e 67 20 6d 61 69 6e 74 61 69 6e mapping maintain
26980 65 64 20 62 79 20 66 69 6c 65 20 0a 2a 2a 20 64 ed by file .** d
26990 65 73 63 72 69 70 74 6f 72 20 70 46 64 20 74 6f escriptor pFd to
269a0 20 6e 4e 65 77 20 62 79 74 65 73 2e 20 41 6e 79 nNew bytes. Any
269b0 20 65 78 69 73 74 69 6e 67 20 6d 61 70 70 69 6e existing mappin
269c0 67 20 69 73 20 64 69 73 63 61 72 64 65 64 2e 0a g is discarded..
269d0 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 **.** If success
269e0 66 75 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69 ful, this functi
269f0 6f 6e 20 73 65 74 73 20 74 68 65 20 66 6f 6c 6c on sets the foll
26a00 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 73 3a owing variables:
26a10 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 75 6e 69 .**.** uni
26a20 78 46 69 6c 65 2e 70 4d 61 70 52 65 67 69 6f 6e xFile.pMapRegion
26a30 0a 2a 2a 20 20 20 20 20 20 20 75 6e 69 78 46 69 .** unixFi
26a40 6c 65 2e 6d 6d 61 70 53 69 7a 65 0a 2a 2a 20 20 le.mmapSize.**
26a50 20 20 20 20 20 75 6e 69 78 46 69 6c 65 2e 6d 6d unixFile.mm
26a60 61 70 53 69 7a 65 41 63 74 75 61 6c 0a 2a 2a 0a apSizeActual.**.
26a70 2a 2a 20 49 66 20 75 6e 73 75 63 63 65 73 73 66 ** If unsuccessf
26a80 75 6c 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 ul, an error mes
26a90 73 61 67 65 20 69 73 20 6c 6f 67 67 65 64 20 76 sage is logged v
26aa0 69 61 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29 ia sqlite3_log()
26ab0 20 61 6e 64 0a 2a 2a 20 74 68 65 20 74 68 72 65 and.** the thre
26ac0 65 20 76 61 72 69 61 62 6c 65 73 20 61 62 6f 76 e variables abov
26ad0 65 20 61 72 65 20 7a 65 72 6f 65 64 2e 20 49 6e e are zeroed. In
26ae0 20 74 68 69 73 20 63 61 73 65 20 53 51 4c 69 74 this case SQLit
26af0 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 63 6f 6e 74 e should.** cont
26b00 69 6e 75 65 20 61 63 63 65 73 73 69 6e 67 20 74 inue accessing t
26b10 68 65 20 64 61 74 61 62 61 73 65 20 75 73 69 6e he database usin
26b20 67 20 74 68 65 20 78 52 65 61 64 28 29 20 61 6e g the xRead() an
26b30 64 20 78 57 72 69 74 65 28 29 0a 2a 2a 20 6d 65 d xWrite().** me
26b40 74 68 6f 64 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 thods..*/.static
26b50 20 76 6f 69 64 20 75 6e 69 78 52 65 6d 61 70 66 void unixRemapf
26b60 69 6c 65 28 0a 20 20 75 6e 69 78 46 69 6c 65 20 ile(. unixFile
26b70 2a 70 46 64 2c 20 20 20 20 20 20 20 20 20 20 20 *pFd,
26b80 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 /* File d
26b90 65 73 63 72 69 70 74 6f 72 20 6f 62 6a 65 63 74 escriptor object
26ba0 20 2a 2f 0a 20 20 69 36 34 20 6e 4e 65 77 20 20 */. i64 nNew
26bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
26bc0 20 20 20 20 20 20 2f 2a 20 52 65 71 75 69 72 65 /* Require
26bd0 64 20 6d 61 70 70 69 6e 67 20 73 69 7a 65 20 2a d mapping size *
26be0 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 /.){. const cha
26bf0 72 20 2a 7a 45 72 72 20 3d 20 22 6d 6d 61 70 22 r *zErr = "mmap"
26c00 3b 0a 20 20 69 6e 74 20 68 20 3d 20 70 46 64 2d ;. int h = pFd-
26c10 3e 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 >h;
26c20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 /* File
26c30 20 64 65 73 63 72 69 70 74 6f 72 20 6f 70 65 6e descriptor open
26c40 20 6f 6e 20 64 62 20 66 69 6c 65 20 2a 2f 0a 20 on db file */.
26c50 20 75 38 20 2a 70 4f 72 69 67 20 3d 20 28 75 38 u8 *pOrig = (u8
26c60 20 2a 29 70 46 64 2d 3e 70 4d 61 70 52 65 67 69 *)pFd->pMapRegi
26c70 6f 6e 3b 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 on; /* Pointer
26c80 20 74 6f 20 63 75 72 72 65 6e 74 20 66 69 6c 65 to current file
26c90 20 6d 61 70 70 69 6e 67 20 2a 2f 0a 20 20 69 36 mapping */. i6
26ca0 34 20 6e 4f 72 69 67 20 3d 20 70 46 64 2d 3e 6d 4 nOrig = pFd->m
26cb0 6d 61 70 53 69 7a 65 41 63 74 75 61 6c 3b 20 20 mapSizeActual;
26cc0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 4f /* Size of pO
26cd0 72 69 67 20 72 65 67 69 6f 6e 20 69 6e 20 62 79 rig region in by
26ce0 74 65 73 20 2a 2f 0a 20 20 75 38 20 2a 70 4e 65 tes */. u8 *pNe
26cf0 77 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 w = 0;
26d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
26d10 20 4c 6f 63 61 74 69 6f 6e 20 6f 66 20 6e 65 77 Location of new
26d20 20 6d 61 70 70 69 6e 67 20 2a 2f 0a 20 20 69 6e mapping */. in
26d30 74 20 66 6c 61 67 73 20 3d 20 50 52 4f 54 5f 52 t flags = PROT_R
26d40 45 41 44 3b 20 20 20 20 20 20 20 20 20 20 20 20 EAD;
26d50 20 20 20 2f 2a 20 46 6c 61 67 73 20 74 6f 20 70 /* Flags to p
26d60 61 73 73 20 74 6f 20 6d 6d 61 70 28 29 20 2a 2f ass to mmap() */
26d70 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 64 2d .. assert( pFd-
26d80 3e 6e 46 65 74 63 68 4f 75 74 3d 3d 30 20 29 3b >nFetchOut==0 );
26d90 0a 20 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3e . assert( nNew>
26da0 70 46 64 2d 3e 6d 6d 61 70 53 69 7a 65 20 29 3b pFd->mmapSize );
26db0 0a 20 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3c . assert( nNew<
26dc0 3d 70 46 64 2d 3e 6d 6d 61 70 53 69 7a 65 4d 61 =pFd->mmapSizeMa
26dd0 78 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e x );. assert( n
26de0 4e 65 77 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 New>0 );. asser
26df0 74 28 20 70 46 64 2d 3e 6d 6d 61 70 53 69 7a 65 t( pFd->mmapSize
26e00 41 63 74 75 61 6c 3e 3d 70 46 64 2d 3e 6d 6d 61 Actual>=pFd->mma
26e10 70 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 pSize );. asser
26e20 74 28 20 4d 41 50 5f 46 41 49 4c 45 44 21 3d 30 t( MAP_FAILED!=0
26e30 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 );..#ifdef SQLI
26e40 54 45 5f 4d 4d 41 50 5f 52 45 41 44 57 52 49 54 TE_MMAP_READWRIT
26e50 45 0a 20 20 69 66 28 20 28 70 46 64 2d 3e 63 74 E. if( (pFd->ct
26e60 72 6c 46 6c 61 67 73 20 26 20 55 4e 49 58 46 49 rlFlags & UNIXFI
26e70 4c 45 5f 52 44 4f 4e 4c 59 29 3d 3d 30 20 29 20 LE_RDONLY)==0 )
26e80 66 6c 61 67 73 20 7c 3d 20 50 52 4f 54 5f 57 52 flags |= PROT_WR
26e90 49 54 45 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 ITE;.#endif.. i
26ea0 66 28 20 70 4f 72 69 67 20 29 7b 0a 23 69 66 20 f( pOrig ){.#if
26eb0 48 41 56 45 5f 4d 52 45 4d 41 50 0a 20 20 20 20 HAVE_MREMAP.
26ec0 69 36 34 20 6e 52 65 75 73 65 20 3d 20 70 46 64 i64 nReuse = pFd
26ed0 2d 3e 6d 6d 61 70 53 69 7a 65 3b 0a 23 65 6c 73 ->mmapSize;.#els
26ee0 65 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 e. const int
26ef0 73 7a 53 79 73 70 61 67 65 20 3d 20 6f 73 47 65 szSyspage = osGe
26f00 74 70 61 67 65 73 69 7a 65 28 29 3b 0a 20 20 20 tpagesize();.
26f10 20 69 36 34 20 6e 52 65 75 73 65 20 3d 20 28 70 i64 nReuse = (p
26f20 46 64 2d 3e 6d 6d 61 70 53 69 7a 65 20 26 20 7e Fd->mmapSize & ~
26f30 28 73 7a 53 79 73 70 61 67 65 2d 31 29 29 3b 0a (szSyspage-1));.
26f40 23 65 6e 64 69 66 0a 20 20 20 20 75 38 20 2a 70 #endif. u8 *p
26f50 52 65 71 20 3d 20 26 70 4f 72 69 67 5b 6e 52 65 Req = &pOrig[nRe
26f60 75 73 65 5d 3b 0a 0a 20 20 20 20 2f 2a 20 55 6e use];.. /* Un
26f70 6d 61 70 20 61 6e 79 20 70 61 67 65 73 20 6f 66 map any pages of
26f80 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 6d 61 the existing ma
26f90 70 70 69 6e 67 20 74 68 61 74 20 63 61 6e 6e 6f pping that canno
26fa0 74 20 62 65 20 72 65 75 73 65 64 2e 20 2a 2f 0a t be reused. */.
26fb0 20 20 20 20 69 66 28 20 6e 52 65 75 73 65 21 3d if( nReuse!=
26fc0 6e 4f 72 69 67 20 29 7b 0a 20 20 20 20 20 20 6f nOrig ){. o
26fd0 73 4d 75 6e 6d 61 70 28 70 52 65 71 2c 20 6e 4f sMunmap(pReq, nO
26fe0 72 69 67 2d 6e 52 65 75 73 65 29 3b 0a 20 20 20 rig-nReuse);.
26ff0 20 7d 0a 0a 23 69 66 20 48 41 56 45 5f 4d 52 45 }..#if HAVE_MRE
27000 4d 41 50 0a 20 20 20 20 70 4e 65 77 20 3d 20 6f MAP. pNew = o
27010 73 4d 72 65 6d 61 70 28 70 4f 72 69 67 2c 20 6e sMremap(pOrig, n
27020 52 65 75 73 65 2c 20 6e 4e 65 77 2c 20 4d 52 45 Reuse, nNew, MRE
27030 4d 41 50 5f 4d 41 59 4d 4f 56 45 29 3b 0a 20 20 MAP_MAYMOVE);.
27040 20 20 7a 45 72 72 20 3d 20 22 6d 72 65 6d 61 70 zErr = "mremap
27050 22 3b 0a 23 65 6c 73 65 0a 20 20 20 20 70 4e 65 ";.#else. pNe
27060 77 20 3d 20 6f 73 4d 6d 61 70 28 70 52 65 71 2c w = osMmap(pReq,
27070 20 6e 4e 65 77 2d 6e 52 65 75 73 65 2c 20 66 6c nNew-nReuse, fl
27080 61 67 73 2c 20 4d 41 50 5f 53 48 41 52 45 44 2c ags, MAP_SHARED,
27090 20 68 2c 20 6e 52 65 75 73 65 29 3b 0a 20 20 20 h, nReuse);.
270a0 20 69 66 28 20 70 4e 65 77 21 3d 4d 41 50 5f 46 if( pNew!=MAP_F
270b0 41 49 4c 45 44 20 29 7b 0a 20 20 20 20 20 20 69 AILED ){. i
270c0 66 28 20 70 4e 65 77 21 3d 70 52 65 71 20 29 7b f( pNew!=pReq ){
270d0 0a 20 20 20 20 20 20 20 20 6f 73 4d 75 6e 6d 61 . osMunma
270e0 70 28 70 4e 65 77 2c 20 6e 4e 65 77 20 2d 20 6e p(pNew, nNew - n
270f0 52 65 75 73 65 29 3b 0a 20 20 20 20 20 20 20 20 Reuse);.
27100 70 4e 65 77 20 3d 20 30 3b 0a 20 20 20 20 20 20 pNew = 0;.
27110 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
27120 4e 65 77 20 3d 20 70 4f 72 69 67 3b 0a 20 20 20 New = pOrig;.
27130 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 }. }.#endi
27140 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 61 74 f.. /* The at
27150 74 65 6d 70 74 20 74 6f 20 65 78 74 65 6e 64 20 tempt to extend
27160 74 68 65 20 65 78 69 73 74 69 6e 67 20 6d 61 70 the existing map
27170 70 69 6e 67 20 66 61 69 6c 65 64 2e 20 46 72 65 ping failed. Fre
27180 65 20 69 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28 e it. */. if(
27190 20 70 4e 65 77 3d 3d 4d 41 50 5f 46 41 49 4c 45 pNew==MAP_FAILE
271a0 44 20 7c 7c 20 70 4e 65 77 3d 3d 30 20 29 7b 0a D || pNew==0 ){.
271b0 20 20 20 20 20 20 6f 73 4d 75 6e 6d 61 70 28 70 osMunmap(p
271c0 4f 72 69 67 2c 20 6e 52 65 75 73 65 29 3b 0a 20 Orig, nReuse);.
271d0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
271e0 66 20 70 4e 65 77 20 69 73 20 73 74 69 6c 6c 20 f pNew is still
271f0 4e 55 4c 4c 2c 20 74 72 79 20 74 6f 20 63 72 65 NULL, try to cre
27200 61 74 65 20 61 6e 20 65 6e 74 69 72 65 6c 79 20 ate an entirely
27210 6e 65 77 20 6d 61 70 70 69 6e 67 2e 20 2a 2f 0a new mapping. */.
27220 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b if( pNew==0 ){
27230 0a 20 20 20 20 70 4e 65 77 20 3d 20 6f 73 4d 6d . pNew = osMm
27240 61 70 28 30 2c 20 6e 4e 65 77 2c 20 66 6c 61 67 ap(0, nNew, flag
27250 73 2c 20 4d 41 50 5f 53 48 41 52 45 44 2c 20 68 s, MAP_SHARED, h
27260 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 , 0);. }.. if(
27270 20 70 4e 65 77 3d 3d 4d 41 50 5f 46 41 49 4c 45 pNew==MAP_FAILE
27280 44 20 29 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20 D ){. pNew =
27290 30 3b 0a 20 20 20 20 6e 4e 65 77 20 3d 20 30 3b 0;. nNew = 0;
272a0 0a 20 20 20 20 75 6e 69 78 4c 6f 67 45 72 72 6f . unixLogErro
272b0 72 28 53 51 4c 49 54 45 5f 4f 4b 2c 20 7a 45 72 r(SQLITE_OK, zEr
272c0 72 2c 20 70 46 64 2d 3e 7a 50 61 74 68 29 3b 0a r, pFd->zPath);.
272d0 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6d . /* If the m
272e0 6d 61 70 28 29 20 61 62 6f 76 65 20 66 61 69 6c map() above fail
272f0 65 64 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20 ed, assume that
27300 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 6d all subsequent m
27310 6d 61 70 28 29 20 63 61 6c 6c 73 0a 20 20 20 20 map() calls.
27320 2a 2a 20 77 69 6c 6c 20 70 72 6f 62 61 62 6c 79 ** will probably
27330 20 66 61 69 6c 20 74 6f 6f 2e 20 46 61 6c 6c 20 fail too. Fall
27340 62 61 63 6b 20 74 6f 20 75 73 69 6e 67 20 78 52 back to using xR
27350 65 61 64 2f 78 57 72 69 74 65 20 65 78 63 6c 75 ead/xWrite exclu
27360 73 69 76 65 6c 79 0a 20 20 20 20 2a 2a 20 69 6e sively. ** in
27370 20 74 68 69 73 20 63 61 73 65 2e 20 20 2a 2f 0a this case. */.
27380 20 20 20 20 70 46 64 2d 3e 6d 6d 61 70 53 69 7a pFd->mmapSiz
27390 65 4d 61 78 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 eMax = 0;. }.
273a0 70 46 64 2d 3e 70 4d 61 70 52 65 67 69 6f 6e 20 pFd->pMapRegion
273b0 3d 20 28 76 6f 69 64 20 2a 29 70 4e 65 77 3b 0a = (void *)pNew;.
273c0 20 20 70 46 64 2d 3e 6d 6d 61 70 53 69 7a 65 20 pFd->mmapSize
273d0 3d 20 70 46 64 2d 3e 6d 6d 61 70 53 69 7a 65 41 = pFd->mmapSizeA
273e0 63 74 75 61 6c 20 3d 20 6e 4e 65 77 3b 0a 7d 0a ctual = nNew;.}.
273f0 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 6d 61 ./*.** Memory ma
27400 70 20 6f 72 20 72 65 6d 61 70 20 74 68 65 20 66 p or remap the f
27410 69 6c 65 20 6f 70 65 6e 65 64 20 62 79 20 66 69 ile opened by fi
27420 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 20 70 46 le-descriptor pF
27430 64 20 28 69 66 20 74 68 65 20 66 69 6c 65 0a 2a d (if the file.*
27440 2a 20 69 73 20 61 6c 72 65 61 64 79 20 6d 61 70 * is already map
27450 70 65 64 2c 20 74 68 65 20 65 78 69 73 74 69 6e ped, the existin
27460 67 20 6d 61 70 70 69 6e 67 20 69 73 20 72 65 70 g mapping is rep
27470 6c 61 63 65 64 20 62 79 20 74 68 65 20 6e 65 77 laced by the new
27480 29 2e 20 4f 72 2c 20 69 66 20 0a 2a 2a 20 74 68 ). Or, if .** th
27490 65 72 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 ere already exis
274a0 74 73 20 61 20 6d 61 70 70 69 6e 67 20 66 6f 72 ts a mapping for
274b0 20 74 68 69 73 20 66 69 6c 65 2c 20 61 6e 64 20 this file, and
274c0 74 68 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 there are still
274d0 0a 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 .** outstanding
274e0 78 46 65 74 63 68 28 29 20 72 65 66 65 72 65 6e xFetch() referen
274f0 63 65 73 20 74 6f 20 69 74 2c 20 74 68 69 73 20 ces to it, this
27500 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f function is a no
27510 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 -op..**.** If pa
27520 72 61 6d 65 74 65 72 20 6e 42 79 74 65 20 69 73 rameter nByte is
27530 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 non-negative, t
27540 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 72 65 hen it is the re
27550 71 75 65 73 74 65 64 20 73 69 7a 65 20 6f 66 20 quested size of
27560 0a 2a 2a 20 74 68 65 20 6d 61 70 70 69 6e 67 20 .** the mapping
27570 74 6f 20 63 72 65 61 74 65 2e 20 4f 74 68 65 72 to create. Other
27580 77 69 73 65 2c 20 69 66 20 6e 42 79 74 65 20 69 wise, if nByte i
27590 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f s less than zero
275a0 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 72 , then the .** r
275b0 65 71 75 65 73 74 65 64 20 73 69 7a 65 20 69 73 equested size is
275c0 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
275d0 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 54 file on disk. T
275e0 68 65 20 61 63 74 75 61 6c 20 73 69 7a 65 20 6f he actual size o
275f0 66 20 74 68 65 0a 2a 2a 20 63 72 65 61 74 65 64 f the.** created
27600 20 6d 61 70 70 69 6e 67 20 69 73 20 65 69 74 68 mapping is eith
27610 65 72 20 74 68 65 20 72 65 71 75 65 73 74 65 64 er the requested
27620 20 73 69 7a 65 20 6f 72 20 74 68 65 20 76 61 6c size or the val
27630 75 65 20 63 6f 6e 66 69 67 75 72 65 64 20 0a 2a ue configured .*
27640 2a 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 46 * using SQLITE_F
27650 43 4e 54 4c 5f 4d 4d 41 50 5f 4c 49 4d 49 54 2c CNTL_MMAP_LIMIT,
27660 20 77 68 69 63 68 65 76 65 72 20 69 73 20 73 6d whichever is sm
27670 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c aller..**.** SQL
27680 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
27690 65 64 20 69 66 20 6e 6f 20 65 72 72 6f 72 20 6f ed if no error o
276a0 63 63 75 72 73 20 28 65 76 65 6e 20 69 66 20 74 ccurs (even if t
276b0 68 65 20 6d 61 70 70 69 6e 67 20 69 73 20 6e 6f he mapping is no
276c0 74 0a 2a 2a 20 72 65 63 72 65 61 74 65 64 20 61 t.** recreated a
276d0 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 6f 75 s a result of ou
276e0 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 tstanding refere
276f0 6e 63 65 73 29 20 6f 72 20 61 6e 20 53 51 4c 69 nces) or an SQLi
27700 74 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 te error.** code
27710 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 otherwise..*/.s
27720 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 4d 61 tatic int unixMa
27730 70 66 69 6c 65 28 75 6e 69 78 46 69 6c 65 20 2a pfile(unixFile *
27740 70 46 64 2c 20 69 36 34 20 6e 4d 61 70 29 7b 0a pFd, i64 nMap){.
27750 20 20 61 73 73 65 72 74 28 20 6e 4d 61 70 3e 3d assert( nMap>=
27760 30 20 7c 7c 20 70 46 64 2d 3e 6e 46 65 74 63 68 0 || pFd->nFetch
27770 4f 75 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 Out==0 );. asse
27780 72 74 28 20 6e 4d 61 70 3e 30 20 7c 7c 20 28 70 rt( nMap>0 || (p
27790 46 64 2d 3e 6d 6d 61 70 53 69 7a 65 3d 3d 30 20 Fd->mmapSize==0
277a0 26 26 20 70 46 64 2d 3e 70 4d 61 70 52 65 67 69 && pFd->pMapRegi
277b0 6f 6e 3d 3d 30 29 20 29 3b 0a 20 20 69 66 28 20 on==0) );. if(
277c0 70 46 64 2d 3e 6e 46 65 74 63 68 4f 75 74 3e 30 pFd->nFetchOut>0
277d0 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
277e0 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 6e 4d 61 70 _OK;.. if( nMap
277f0 3c 30 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 <0 ){. struct
27800 20 73 74 61 74 20 73 74 61 74 62 75 66 3b 20 20 stat statbuf;
27810 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c /* Low-l
27820 65 76 65 6c 20 66 69 6c 65 20 69 6e 66 6f 72 6d evel file inform
27830 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 ation */. if(
27840 20 6f 73 46 73 74 61 74 28 70 46 64 2d 3e 68 2c osFstat(pFd->h,
27850 20 26 73 74 61 74 62 75 66 29 20 29 7b 0a 20 20 &statbuf) ){.
27860 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
27870 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 E_IOERR_FSTAT;.
27880 20 20 20 7d 0a 20 20 20 20 6e 4d 61 70 20 3d 20 }. nMap =
27890 73 74 61 74 62 75 66 2e 73 74 5f 73 69 7a 65 3b statbuf.st_size;
278a0 0a 20 20 7d 0a 20 20 69 66 28 20 6e 4d 61 70 3e . }. if( nMap>
278b0 70 46 64 2d 3e 6d 6d 61 70 53 69 7a 65 4d 61 78 pFd->mmapSizeMax
278c0 20 29 7b 0a 20 20 20 20 6e 4d 61 70 20 3d 20 70 ){. nMap = p
278d0 46 64 2d 3e 6d 6d 61 70 53 69 7a 65 4d 61 78 3b Fd->mmapSizeMax;
278e0 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 . }.. assert(
278f0 6e 4d 61 70 3e 30 20 7c 7c 20 28 70 46 64 2d 3e nMap>0 || (pFd->
27900 6d 6d 61 70 53 69 7a 65 3d 3d 30 20 26 26 20 70 mmapSize==0 && p
27910 46 64 2d 3e 70 4d 61 70 52 65 67 69 6f 6e 3d 3d Fd->pMapRegion==
27920 30 29 20 29 3b 0a 20 20 69 66 28 20 6e 4d 61 70 0) );. if( nMap
27930 21 3d 70 46 64 2d 3e 6d 6d 61 70 53 69 7a 65 20 !=pFd->mmapSize
27940 29 7b 0a 20 20 20 20 75 6e 69 78 52 65 6d 61 70 ){. unixRemap
27950 66 69 6c 65 28 70 46 64 2c 20 6e 4d 61 70 29 3b file(pFd, nMap);
27960 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 . }.. return S
27970 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 QLITE_OK;.}.#end
27980 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 41 58 if /* SQLITE_MAX
27990 5f 4d 4d 41 50 5f 53 49 5a 45 3e 30 20 2a 2f 0a _MMAP_SIZE>0 */.
279a0 0a 2f 2a 0a 2a 2a 20 49 66 20 70 6f 73 73 69 62 ./*.** If possib
279b0 6c 65 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 le, return a poi
279c0 6e 74 65 72 20 74 6f 20 61 20 6d 61 70 70 69 6e nter to a mappin
279d0 67 20 6f 66 20 66 69 6c 65 20 66 64 20 73 74 61 g of file fd sta
279e0 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 74 0a rting at offset.
279f0 2a 2a 20 69 4f 66 66 2e 20 54 68 65 20 6d 61 70 ** iOff. The map
27a00 70 69 6e 67 20 6d 75 73 74 20 62 65 20 76 61 6c ping must be val
27a10 69 64 20 66 6f 72 20 61 74 20 6c 65 61 73 74 20 id for at least
27a20 6e 41 6d 74 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a nAmt bytes..**.*
27a30 2a 20 49 66 20 73 75 63 68 20 61 20 70 6f 69 6e * If such a poin
27a40 74 65 72 20 63 61 6e 20 62 65 20 6f 62 74 61 69 ter can be obtai
27a50 6e 65 64 2c 20 73 74 6f 72 65 20 69 74 20 69 6e ned, store it in
27a60 20 2a 70 70 20 61 6e 64 20 72 65 74 75 72 6e 20 *pp and return
27a70 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 4f 72 SQLITE_OK..** Or
27a80 2c 20 69 66 20 6f 6e 65 20 63 61 6e 6e 6f 74 20 , if one cannot
27a90 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 but no error occ
27aa0 75 72 73 2c 20 73 65 74 20 2a 70 70 20 74 6f 20 urs, set *pp to
27ab0 30 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 0 and return SQL
27ac0 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 46 69 6e 61 6c ITE_OK..** Final
27ad0 6c 79 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 ly, if an error
27ae0 64 6f 65 73 20 6f 63 63 75 72 2c 20 72 65 74 75 does occur, retu
27af0 72 6e 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 rn an SQLite err
27b00 6f 72 20 63 6f 64 65 2e 20 54 68 65 20 66 69 6e or code. The fin
27b10 61 6c 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 2a al.** value of *
27b20 70 70 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 pp is undefined
27b30 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a in this case..**
27b40 0a 2a 2a 20 49 66 20 74 68 69 73 20 66 75 6e 63 .** If this func
27b50 74 69 6f 6e 20 64 6f 65 73 20 72 65 74 75 72 6e tion does return
27b60 20 61 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 20 a pointer, the
27b70 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 76 65 6e caller must even
27b80 74 75 61 6c 6c 79 20 0a 2a 2a 20 72 65 6c 65 61 tually .** relea
27b90 73 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 se the reference
27ba0 20 62 79 20 63 61 6c 6c 69 6e 67 20 75 6e 69 78 by calling unix
27bb0 55 6e 66 65 74 63 68 28 29 2e 0a 2a 2f 0a 73 74 Unfetch()..*/.st
27bc0 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 65 74 atic int unixFet
27bd0 63 68 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ch(sqlite3_file
27be0 2a 66 64 2c 20 69 36 34 20 69 4f 66 66 2c 20 69 *fd, i64 iOff, i
27bf0 6e 74 20 6e 41 6d 74 2c 20 76 6f 69 64 20 2a 2a nt nAmt, void **
27c00 70 70 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f pp){.#if SQLITE_
27c10 4d 41 58 5f 4d 4d 41 50 5f 53 49 5a 45 3e 30 0a MAX_MMAP_SIZE>0.
27c20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 64 20 unixFile *pFd
27c30 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 66 64 = (unixFile *)fd
27c40 3b 20 20 20 2f 2a 20 54 68 65 20 75 6e 64 65 72 ; /* The under
27c50 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 20 66 lying database f
27c60 69 6c 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 ile */.#endif.
27c70 2a 70 70 20 3d 20 30 3b 0a 0a 23 69 66 20 53 51 *pp = 0;..#if SQ
27c80 4c 49 54 45 5f 4d 41 58 5f 4d 4d 41 50 5f 53 49 LITE_MAX_MMAP_SI
27c90 5a 45 3e 30 0a 20 20 69 66 28 20 70 46 64 2d 3e ZE>0. if( pFd->
27ca0 6d 6d 61 70 53 69 7a 65 4d 61 78 3e 30 20 29 7b mmapSizeMax>0 ){
27cb0 0a 20 20 20 20 69 66 28 20 70 46 64 2d 3e 70 4d . if( pFd->pM
27cc0 61 70 52 65 67 69 6f 6e 3d 3d 30 20 29 7b 0a 20 apRegion==0 ){.
27cd0 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 75 6e int rc = un
27ce0 69 78 4d 61 70 66 69 6c 65 28 70 46 64 2c 20 2d ixMapfile(pFd, -
27cf0 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 1);. if( rc
27d00 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 !=SQLITE_OK ) re
27d10 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
27d20 20 20 20 69 66 28 20 70 46 64 2d 3e 6d 6d 61 70 if( pFd->mmap
27d30 53 69 7a 65 20 3e 3d 20 69 4f 66 66 2b 6e 41 6d Size >= iOff+nAm
27d40 74 20 29 7b 0a 20 20 20 20 20 20 2a 70 70 20 3d t ){. *pp =
27d50 20 26 28 28 75 38 20 2a 29 70 46 64 2d 3e 70 4d &((u8 *)pFd->pM
27d60 61 70 52 65 67 69 6f 6e 29 5b 69 4f 66 66 5d 3b apRegion)[iOff];
27d70 0a 20 20 20 20 20 20 70 46 64 2d 3e 6e 46 65 74 . pFd->nFet
27d80 63 68 4f 75 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 chOut++;. }.
27d90 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 }.#endif. retu
27da0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
27db0 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74 68 ./*.** If the th
27dc0 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ird argument is
27dd0 6e 6f 6e 2d 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 non-NULL, then t
27de0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 6c his function rel
27df0 65 61 73 65 73 20 61 20 0a 2a 2a 20 72 65 66 65 eases a .** refe
27e00 72 65 6e 63 65 20 6f 62 74 61 69 6e 65 64 20 62 rence obtained b
27e10 79 20 61 6e 20 65 61 72 6c 69 65 72 20 63 61 6c y an earlier cal
27e20 6c 20 74 6f 20 75 6e 69 78 46 65 74 63 68 28 29 l to unixFetch()
27e30 2e 20 54 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 . The second.**
27e40 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 argument passed
27e50 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
27e60 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d must be the sam
27e70 65 20 61 73 20 74 68 65 20 63 6f 72 72 65 73 70 e as the corresp
27e80 6f 6e 64 69 6e 67 0a 2a 2a 20 61 72 67 75 6d 65 onding.** argume
27e90 6e 74 20 74 68 61 74 20 77 61 73 20 70 61 73 73 nt that was pass
27ea0 65 64 20 74 6f 20 74 68 65 20 75 6e 69 78 46 65 ed to the unixFe
27eb0 74 63 68 28 29 20 69 6e 76 6f 63 61 74 69 6f 6e tch() invocation
27ec0 2e 20 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 66 20 . .**.** Or, if
27ed0 74 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 the third argume
27ee0 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e nt is NULL, then
27ef0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
27f00 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 0a s being called .
27f10 2a 2a 20 74 6f 20 69 6e 66 6f 72 6d 20 74 68 65 ** to inform the
27f20 20 56 46 53 20 6c 61 79 65 72 20 74 68 61 74 2c VFS layer that,
27f30 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 50 4f according to PO
27f40 53 49 58 2c 20 61 6e 79 20 65 78 69 73 74 69 6e SIX, any existin
27f50 67 20 6d 61 70 70 69 6e 67 20 0a 2a 2a 20 6d 61 g mapping .** ma
27f60 79 20 6e 6f 77 20 62 65 20 69 6e 76 61 6c 69 64 y now be invalid
27f70 20 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 75 and should be u
27f80 6e 6d 61 70 70 65 64 2e 0a 2a 2f 0a 73 74 61 74 nmapped..*/.stat
27f90 69 63 20 69 6e 74 20 75 6e 69 78 55 6e 66 65 74 ic int unixUnfet
27fa0 63 68 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ch(sqlite3_file
27fb0 2a 66 64 2c 20 69 36 34 20 69 4f 66 66 2c 20 76 *fd, i64 iOff, v
27fc0 6f 69 64 20 2a 70 29 7b 0a 23 69 66 20 53 51 4c oid *p){.#if SQL
27fd0 49 54 45 5f 4d 41 58 5f 4d 4d 41 50 5f 53 49 5a ITE_MAX_MMAP_SIZ
27fe0 45 3e 30 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a E>0. unixFile *
27ff0 70 46 64 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 pFd = (unixFile
28000 2a 29 66 64 3b 20 20 20 2f 2a 20 54 68 65 20 75 *)fd; /* The u
28010 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62 61 nderlying databa
28020 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 55 4e 55 se file */. UNU
28030 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 4f SED_PARAMETER(iO
28040 66 66 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 3d ff);.. /* If p=
28050 3d 30 20 28 75 6e 6d 61 70 20 74 68 65 20 65 6e =0 (unmap the en
28060 74 69 72 65 20 66 69 6c 65 29 20 74 68 65 6e 20 tire file) then
28070 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 6e 6f there must be no
28080 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 0a 20 20 outstanding .
28090 2a 2a 20 78 46 65 74 63 68 20 72 65 66 65 72 65 ** xFetch refere
280a0 6e 63 65 73 2e 20 4f 72 2c 20 69 66 20 70 21 3d nces. Or, if p!=
280b0 30 20 28 6d 65 61 6e 69 6e 67 20 69 74 20 69 73 0 (meaning it is
280c0 20 61 6e 20 78 46 65 74 63 68 20 72 65 66 65 72 an xFetch refer
280d0 65 6e 63 65 29 2c 0a 20 20 2a 2a 20 74 68 65 6e ence),. ** then
280e0 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 there must be a
280f0 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 75 74 73 t least one outs
28100 74 61 6e 64 69 6e 67 2e 20 20 2a 2f 0a 20 20 61 tanding. */. a
28110 73 73 65 72 74 28 20 28 70 3d 3d 30 29 3d 3d 28 ssert( (p==0)==(
28120 70 46 64 2d 3e 6e 46 65 74 63 68 4f 75 74 3d 3d pFd->nFetchOut==
28130 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 0) );.. /* If p
28140 21 3d 30 2c 20 69 74 20 6d 75 73 74 20 6d 61 74 !=0, it must mat
28150 63 68 20 74 68 65 20 69 4f 66 66 20 76 61 6c 75 ch the iOff valu
28160 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 e. */. assert(
28170 70 3d 3d 30 20 7c 7c 20 70 3d 3d 26 28 28 75 38 p==0 || p==&((u8
28180 20 2a 29 70 46 64 2d 3e 70 4d 61 70 52 65 67 69 *)pFd->pMapRegi
28190 6f 6e 29 5b 69 4f 66 66 5d 20 29 3b 0a 0a 20 20 on)[iOff] );..
281a0 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 46 64 if( p ){. pFd
281b0 2d 3e 6e 46 65 74 63 68 4f 75 74 2d 2d 3b 0a 20 ->nFetchOut--;.
281c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 }else{. unix
281d0 55 6e 6d 61 70 66 69 6c 65 28 70 46 64 29 3b 0a Unmapfile(pFd);.
281e0 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 }.. assert( p
281f0 46 64 2d 3e 6e 46 65 74 63 68 4f 75 74 3e 3d 30 Fd->nFetchOut>=0
28200 20 29 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 );.#else. UNUS
28210 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 64 29 ED_PARAMETER(fd)
28220 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
28230 45 54 45 52 28 70 29 3b 0a 20 20 55 4e 55 53 45 ETER(p);. UNUSE
28240 44 5f 50 41 52 41 4d 45 54 45 52 28 69 4f 66 66 D_PARAMETER(iOff
28250 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 );.#endif. retu
28260 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
28270 0a 2f 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 ./*.** Here ends
28280 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 the implementat
28290 69 6f 6e 20 6f 66 20 61 6c 6c 20 73 71 6c 69 74 ion of all sqlit
282a0 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 2e e3_file methods.
282b0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
282c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 73 ********** End s
282d0 71 6c 69 74 65 33 5f 66 69 6c 65 20 4d 65 74 68 qlite3_file Meth
282e0 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ods ************
282f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
28300 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***.************
28310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
28320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
28330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
28340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
28350 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 **/../*.** This
28360 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e division contain
28370 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 s definitions of
28380 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
28390 6f 64 73 20 6f 62 6a 65 63 74 73 20 74 68 61 74 ods objects that
283a0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 76 61 .** implement va
283b0 72 69 6f 75 73 20 66 69 6c 65 20 6c 6f 63 6b 69 rious file locki
283c0 6e 67 20 73 74 72 61 74 65 67 69 65 73 2e 20 20 ng strategies.
283d0 49 74 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 It also contains
283e0 20 64 65 66 69 6e 69 74 69 6f 6e 73 0a 2a 2a 20 definitions.**
283f0 6f 66 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 of "finder" func
28400 74 69 6f 6e 73 2e 20 20 41 20 66 69 6e 64 65 72 tions. A finder
28410 2d 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 -function is use
28420 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 d to locate the
28430 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 73 appropriate.** s
28440 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
28450 73 20 6f 62 6a 65 63 74 20 66 6f 72 20 61 20 70 s object for a p
28460 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 articular databa
28470 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 41 se file. The pA
28480 70 70 44 61 74 61 0a 2a 2a 20 66 69 65 6c 64 20 ppData.** field
28490 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 of the sqlite3_v
284a0 66 73 20 56 46 53 20 6f 62 6a 65 63 74 73 20 61 fs VFS objects a
284b0 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 re initialized t
284c0 6f 20 62 65 20 70 6f 69 6e 74 65 72 73 20 74 6f o be pointers to
284d0 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 63 74 20 .** the correct
284e0 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 finder-function
284f0 66 6f 72 20 74 68 61 74 20 56 46 53 2e 0a 2a 2a for that VFS..**
28500 0a 2a 2a 20 4d 6f 73 74 20 66 69 6e 64 65 72 20 .** Most finder
28510 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e functions return
28520 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
28530 66 69 78 65 64 20 73 71 6c 69 74 65 33 5f 69 6f fixed sqlite3_io
28540 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62 6a 65 _methods.** obje
28550 63 74 2e 20 20 54 68 65 20 6f 6e 6c 79 20 69 6e ct. The only in
28560 74 65 72 65 73 74 69 6e 67 20 66 69 6e 64 65 72 teresting finder
28570 2d 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 75 74 -function is aut
28580 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 77 olockIoFinder, w
28590 68 69 63 68 0a 2a 2a 20 6c 6f 6f 6b 73 20 61 74 hich.** looks at
285a0 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20 the filesystem
285b0 74 79 70 65 20 61 6e 64 20 74 72 69 65 73 20 74 type and tries t
285c0 6f 20 67 75 65 73 73 20 74 68 65 20 62 65 73 74 o guess the best
285d0 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 72 61 locking.** stra
285e0 74 65 67 79 20 66 72 6f 6d 20 74 68 61 74 2e 0a tegy from that..
285f0 2a 2a 0a 2a 2a 20 46 6f 72 20 66 69 6e 64 65 72 **.** For finder
28600 2d 66 75 6e 63 74 69 6f 6e 20 46 2c 20 74 77 6f -function F, two
28610 20 6f 62 6a 65 63 74 73 20 61 72 65 20 63 72 65 objects are cre
28620 61 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 ated:.**.** (
28630 31 29 20 54 68 65 20 72 65 61 6c 20 66 69 6e 64 1) The real find
28640 65 72 2d 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 er-function name
28650 64 20 22 46 49 6d 70 74 28 29 22 2e 0a 2a 2a 0a d "FImpt()"..**.
28660 2a 2a 20 20 20 20 28 32 29 20 41 20 63 6f 6e 73 ** (2) A cons
28670 74 61 6e 74 20 70 6f 69 6e 74 65 72 20 74 6f 20 tant pointer to
28680 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6e 61 this function na
28690 6d 65 64 20 6a 75 73 74 20 22 46 22 2e 0a 2a 2a med just "F"..**
286a0 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 .**.** A pointer
286b0 20 74 6f 20 74 68 65 20 46 20 70 6f 69 6e 74 65 to the F pointe
286c0 72 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65 r is used as the
286d0 20 70 41 70 70 44 61 74 61 20 76 61 6c 75 65 20 pAppData value
286e0 66 6f 72 20 56 46 53 0a 2a 2a 20 6f 62 6a 65 63 for VFS.** objec
286f0 74 73 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 ts. We have to
28700 64 6f 20 74 68 69 73 20 69 6e 73 74 65 61 64 20 do this instead
28710 6f 66 20 6c 65 74 74 69 6e 67 20 70 41 70 70 44 of letting pAppD
28720 61 74 61 20 70 6f 69 6e 74 0a 2a 2a 20 64 69 72 ata point.** dir
28730 65 63 74 6c 79 20 61 74 20 74 68 65 20 66 69 6e ectly at the fin
28740 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 73 69 6e der-function sin
28750 63 65 20 43 39 30 20 72 75 6c 65 73 20 70 72 65 ce C90 rules pre
28760 76 65 6e 74 20 61 20 76 6f 69 64 2a 0a 2a 2a 20 vent a void*.**
28770 66 72 6f 6d 20 62 65 20 63 61 73 74 20 69 6e 74 from be cast int
28780 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 o a function poi
28790 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 45 nter..**.**.** E
287a0 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 ach instance of
287b0 74 68 69 73 20 6d 61 63 72 6f 20 67 65 6e 65 72 this macro gener
287c0 61 74 65 73 20 74 77 6f 20 6f 62 6a 65 63 74 73 ates two objects
287d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 20 63 :.**.** * A c
287e0 6f 6e 73 74 61 6e 74 20 73 71 6c 69 74 65 33 5f onstant sqlite3_
287f0 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 io_methods objec
28800 74 20 63 61 6c 6c 20 4d 45 54 48 4f 44 20 74 68 t call METHOD th
28810 61 74 20 68 61 73 20 6c 6f 63 6b 69 6e 67 0a 2a at has locking.*
28820 2a 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20 43 * methods C
28830 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f LOSE, LOCK, UNLO
28840 43 4b 2c 20 43 4b 52 45 53 4c 4f 43 4b 2e 0a 2a CK, CKRESLOCK..*
28850 2a 0a 2a 2a 20 20 20 2a 20 20 41 6e 20 49 2f 4f *.** * An I/O
28860 20 6d 65 74 68 6f 64 20 66 69 6e 64 65 72 20 66 method finder f
28870 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 65 64 20 46 unction called F
28880 49 4e 44 45 52 20 74 68 61 74 20 72 65 74 75 72 INDER that retur
28890 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 ns a pointer.**
288a0 20 20 20 20 20 74 6f 20 74 68 65 20 4d 45 54 48 to the METH
288b0 4f 44 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 OD object in the
288c0 20 70 72 65 76 69 6f 75 73 20 62 75 6c 6c 65 74 previous bullet
288d0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 49 4f 4d ..*/.#define IOM
288e0 45 54 48 4f 44 53 28 46 49 4e 44 45 52 2c 4d 45 ETHODS(FINDER,ME
288f0 54 48 4f 44 2c 56 45 52 53 49 4f 4e 2c 43 4c 4f THOD,VERSION,CLO
28900 53 45 2c 4c 4f 43 4b 2c 55 4e 4c 4f 43 4b 2c 43 SE,LOCK,UNLOCK,C
28910 4b 4c 4f 43 4b 2c 53 48 4d 4d 41 50 29 20 20 20 KLOCK,SHMMAP)
28920 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 \.static const
28930 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
28940 6f 64 73 20 4d 45 54 48 4f 44 20 3d 20 7b 20 20 ods METHOD = {
28950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28970 20 5c 0a 20 20 20 56 45 52 53 49 4f 4e 2c 20 20 \. VERSION,
28980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28990 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f /* iVersion */
289a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
289b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
289c0 5c 0a 20 20 20 43 4c 4f 53 45 2c 20 20 20 20 20 \. CLOSE,
289d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
289e0 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 20 20 20 /* xClose */
289f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
28a10 0a 20 20 20 75 6e 69 78 52 65 61 64 2c 20 20 20 . unixRead,
28a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28a30 2f 2a 20 78 52 65 61 64 20 2a 2f 20 20 20 20 20 /* xRead */
28a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
28a60 20 20 20 75 6e 69 78 57 72 69 74 65 2c 20 20 20 unixWrite,
28a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
28a80 2a 20 78 57 72 69 74 65 20 2a 2f 20 20 20 20 20 * xWrite */
28a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
28ab0 20 20 75 6e 69 78 54 72 75 6e 63 61 74 65 2c 20 unixTruncate,
28ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
28ad0 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 20 20 20 xTruncate */
28ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28af0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
28b00 20 75 6e 69 78 53 79 6e 63 2c 20 20 20 20 20 20 unixSync,
28b10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
28b20 78 53 79 6e 63 20 2a 2f 20 20 20 20 20 20 20 20 xSync */
28b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28b40 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
28b50 75 6e 69 78 46 69 6c 65 53 69 7a 65 2c 20 20 20 unixFileSize,
28b60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
28b70 46 69 6c 65 53 69 7a 65 20 2a 2f 20 20 20 20 20 FileSize */
28b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28b90 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 4c \. L
28ba0 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20 OCK,
28bb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c /* xL
28bc0 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ock */
28bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28be0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 55 4e \. UN
28bf0 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20 LOCK,
28c00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e /* xUn
28c10 6c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 lock */
28c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28c30 20 20 20 20 20 20 20 20 5c 0a 20 20 20 43 4b 4c \. CKL
28c40 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20 OCK,
28c50 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 68 65 /* xChe
28c60 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 2a ckReservedLock *
28c70 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
28c80 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 \. unix
28c90 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 20 20 20 20 FileControl,
28ca0 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 /* xFile
28cb0 43 6f 6e 74 72 6f 6c 20 2a 2f 20 20 20 20 20 20 Control */
28cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28cd0 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 53 \. unixS
28ce0 65 63 74 6f 72 53 69 7a 65 2c 20 20 20 20 20 20 ectorSize,
28cf0 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 74 6f /* xSecto
28d00 72 53 69 7a 65 20 2a 2f 20 20 20 20 20 20 20 20 rSize */
28d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28d20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 44 65 \. unixDe
28d30 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
28d40 69 63 73 2c 20 20 2f 2a 20 78 44 65 76 69 63 65 ics, /* xDevice
28d50 43 61 70 61 62 69 6c 69 74 69 65 73 20 2a 2f 20 Capabilities */
28d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28d70 20 20 20 20 5c 0a 20 20 20 53 48 4d 4d 41 50 2c \. SHMMAP,
28d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28d90 20 20 20 20 20 2f 2a 20 78 53 68 6d 4d 61 70 20 /* xShmMap
28da0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
28db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28dc0 20 20 20 5c 0a 20 20 20 75 6e 69 78 53 68 6d 4c \. unixShmL
28dd0 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 ock,
28de0 20 20 20 20 2f 2a 20 78 53 68 6d 4c 6f 63 6b 20 /* xShmLock
28df0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
28e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28e10 20 20 5c 0a 20 20 20 75 6e 69 78 53 68 6d 42 61 \. unixShmBa
28e20 72 72 69 65 72 2c 20 20 20 20 20 20 20 20 20 20 rrier,
28e30 20 20 20 2f 2a 20 78 53 68 6d 42 61 72 72 69 65 /* xShmBarrie
28e40 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 r */
28e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28e60 20 5c 0a 20 20 20 75 6e 69 78 53 68 6d 55 6e 6d \. unixShmUnm
28e70 61 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ap,
28e80 20 20 2f 2a 20 78 53 68 6d 55 6e 6d 61 70 20 2a /* xShmUnmap *
28e90 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
28ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28eb0 5c 0a 20 20 20 75 6e 69 78 46 65 74 63 68 2c 20 \. unixFetch,
28ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28ed0 20 2f 2a 20 78 46 65 74 63 68 20 2a 2f 20 20 20 /* xFetch */
28ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
28f00 0a 20 20 20 75 6e 69 78 55 6e 66 65 74 63 68 2c . unixUnfetch,
28f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28f20 2f 2a 20 78 55 6e 66 65 74 63 68 20 2a 2f 20 20 /* xUnfetch */
28f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
28f50 7d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 };
28f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28f90 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 73 \.s
28fa0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 tatic const sqli
28fb0 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a te3_io_methods *
28fc0 46 49 4e 44 45 52 23 23 49 6d 70 6c 28 63 6f 6e FINDER##Impl(con
28fd0 73 74 20 63 68 61 72 20 2a 7a 2c 20 75 6e 69 78 st char *z, unix
28fe0 46 69 6c 65 20 2a 70 29 7b 20 20 20 5c 0a 20 20 File *p){ \.
28ff0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
29000 28 7a 29 3b 20 55 4e 55 53 45 44 5f 50 41 52 41 (z); UNUSED_PARA
29010 4d 45 54 45 52 28 70 29 3b 20 20 20 20 20 20 20 METER(p);
29020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
29030 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 72 \. r
29040 65 74 75 72 6e 20 26 4d 45 54 48 4f 44 3b 20 20 eturn &METHOD;
29050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
29060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
29070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
29080 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 20 20 20 \.}
29090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
290a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
290b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
290c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
290d0 20 20 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 \.stati
290e0 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
290f0 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 63 6f io_methods *(*co
29100 6e 73 74 20 46 49 4e 44 45 52 29 28 63 6f 6e 73 nst FINDER)(cons
29110 74 20 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 t char*,unixFile
29120 20 2a 70 29 20 20 20 20 5c 0a 20 20 20 20 3d 20 *p) \. =
29130 46 49 4e 44 45 52 23 23 49 6d 70 6c 3b 0a 0a 2f FINDER##Impl;../
29140 2a 0a 2a 2a 20 48 65 72 65 20 61 72 65 20 61 6c *.** Here are al
29150 6c 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 l of the sqlite3
29160 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
29170 63 74 73 20 66 6f 72 20 65 61 63 68 20 6f 66 20 cts for each of
29180 74 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 the.** locking s
29190 74 72 61 74 65 67 69 65 73 2e 20 20 46 75 6e 63 trategies. Func
291a0 74 69 6f 6e 73 20 74 68 61 74 20 72 65 74 75 72 tions that retur
291b0 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 n pointers to th
291c0 65 73 65 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 61 ese methods.** a
291d0 72 65 20 61 6c 73 6f 20 63 72 65 61 74 65 64 2e re also created.
291e0 0a 2a 2f 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 .*/.IOMETHODS(.
291f0 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 2c 20 posixIoFinder,
29200 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
29210 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 nder function na
29220 6d 65 20 2a 2f 0a 20 20 70 6f 73 69 78 49 6f 4d me */. posixIoM
29230 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 ethods,
29240 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f /* sqlite3_io_
29250 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e methods object n
29260 61 6d 65 20 2a 2f 0a 20 20 33 2c 20 20 20 20 20 ame */. 3,
29270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
29280 20 20 20 2f 2a 20 73 68 61 72 65 64 20 6d 65 6d /* shared mem
29290 6f 72 79 20 61 6e 64 20 6d 6d 61 70 20 61 72 65 ory and mmap are
292a0 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 75 6e enabled */. un
292b0 69 78 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 ixClose,
292c0 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 /* xClos
292d0 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 75 6e e method */. un
292e0 69 78 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 ixLock,
292f0 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b /* xLock
29300 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 75 6e 69 method */. uni
29310 78 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 xUnlock,
29320 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 /* xUnloc
29330 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 75 6e k method */. un
29340 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c ixCheckReservedL
29350 6f 63 6b 2c 20 20 20 20 2f 2a 20 78 43 68 65 63 ock, /* xChec
29360 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 kReservedLock me
29370 74 68 6f 64 20 2a 2f 0a 20 20 75 6e 69 78 53 68 thod */. unixSh
29380 6d 4d 61 70 20 20 20 20 20 20 20 20 20 20 20 20 mMap
29390 20 20 20 20 2f 2a 20 78 53 68 6d 4d 61 70 20 6d /* xShmMap m
293a0 65 74 68 6f 64 20 2a 2f 0a 29 0a 49 4f 4d 45 54 ethod */.).IOMET
293b0 48 4f 44 53 28 0a 20 20 6e 6f 6c 6f 63 6b 49 6f HODS(. nolockIo
293c0 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 Finder,
293d0 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 /* Finder func
293e0 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 6e tion name */. n
293f0 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20 olockIoMethods,
29400 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 /* sqli
29410 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f te3_io_methods o
29420 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 bject name */.
29430 33 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3,
29440 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61 /* sha
29450 72 65 64 20 6d 65 6d 6f 72 79 20 69 73 20 64 69 red memory is di
29460 73 61 62 6c 65 64 20 2a 2f 0a 20 20 6e 6f 6c 6f sabled */. nolo
29470 63 6b 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 ckClose,
29480 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 /* xClose
29490 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f method */. nolo
294a0 63 6b 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 ckLock,
294b0 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d /* xLock m
294c0 65 74 68 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 ethod */. noloc
294d0 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 kUnlock,
294e0 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 /* xUnlock
294f0 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f method */. nolo
29500 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c ckCheckReservedL
29510 6f 63 6b 2c 20 20 2f 2a 20 78 43 68 65 63 6b 52 ock, /* xCheckR
29520 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 eservedLock meth
29530 6f 64 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20 od */. 0
29540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
29550 20 20 2f 2a 20 78 53 68 6d 4d 61 70 20 6d 65 74 /* xShmMap met
29560 68 6f 64 20 2a 2f 0a 29 0a 49 4f 4d 45 54 48 4f hod */.).IOMETHO
29570 44 53 28 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 DS(. dotlockIoF
29580 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 inder,
29590 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 /* Finder functi
295a0 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 64 6f 74 on name */. dot
295b0 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20 20 lockIoMethods,
295c0 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 /* sqlite
295d0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 3_io_methods obj
295e0 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 31 2c ect name */. 1,
295f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
29600 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61 72 65 /* share
29610 64 20 6d 65 6d 6f 72 79 20 69 73 20 64 69 73 61 d memory is disa
29620 62 6c 65 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 bled */. dotloc
29630 6b 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 kClose,
29640 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 /* xClose me
29650 74 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 thod */. dotloc
29660 6b 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 kLock,
29670 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 /* xLock met
29680 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b hod */. dotlock
29690 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 Unlock,
296a0 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 /* xUnlock me
296b0 74 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 thod */. dotloc
296c0 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f kCheckReservedLo
296d0 63 6b 2c 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 ck, /* xCheckRes
296e0 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 ervedLock method
296f0 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20 20 20 */. 0
29700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
29710 2f 2a 20 78 53 68 6d 4d 61 70 20 6d 65 74 68 6f /* xShmMap metho
29720 64 20 2a 2f 0a 29 0a 0a 23 69 66 20 53 51 4c 49 d */.)..#if SQLI
29730 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
29740 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54 48 4f 44 G_STYLE.IOMETHOD
29750 53 28 0a 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 S(. flockIoFind
29760 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f er, /
29770 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f * Finder functio
29780 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6c 6f 63 n name */. floc
29790 6b 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 kIoMethods,
297a0 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 /* sqlite3
297b0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
297c0 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 31 2c 20 ct name */. 1,
297d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
297e0 20 20 20 20 20 20 20 2f 2a 20 73 68 61 72 65 64 /* shared
297f0 20 6d 65 6d 6f 72 79 20 69 73 20 64 69 73 61 62 memory is disab
29800 6c 65 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 6c led */. flockCl
29810 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ose,
29820 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 /* xClose met
29830 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c 6f hod */. flockLo
29840 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ck,
29850 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 /* xLock meth
29860 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e 6c od */. flockUnl
29870 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 ock,
29880 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 /* xUnlock met
29890 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 68 hod */. flockCh
298a0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c eckReservedLock,
298b0 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 /* xCheckRese
298c0 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 rvedLock method
298d0 2a 2f 0a 20 20 30 20 20 20 20 20 20 20 20 20 20 */. 0
298e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
298f0 2a 20 78 53 68 6d 4d 61 70 20 6d 65 74 68 6f 64 * xShmMap method
29900 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 */.).#endif..#i
29910 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d f OS_VXWORKS.IOM
29920 45 54 48 4f 44 53 28 0a 20 20 73 65 6d 49 6f 46 ETHODS(. semIoF
29930 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 inder,
29940 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 /* Finder fu
29950 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 nction name */.
29960 20 73 65 6d 49 6f 4d 65 74 68 6f 64 73 2c 20 20 semIoMethods,
29970 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 /* sq
29980 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
29990 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a object name */.
299a0 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 1,
299b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
299c0 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 69 73 20 hared memory is
299d0 64 69 73 61 62 6c 65 64 20 2a 2f 0a 20 20 73 65 disabled */. se
299e0 6d 58 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 mXClose,
299f0 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 /* xClos
29a00 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 e method */. se
29a10 6d 58 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 mXLock,
29a20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b /* xLock
29a30 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 6d method */. sem
29a40 58 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 XUnlock,
29a50 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 /* xUnloc
29a60 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 k method */. se
29a70 6d 58 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c mXCheckReservedL
29a80 6f 63 6b 2c 20 20 20 20 2f 2a 20 78 43 68 65 63 ock, /* xChec
29a90 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 kReservedLock me
29aa0 74 68 6f 64 20 2a 2f 0a 20 20 30 20 20 20 20 20 thod */. 0
29ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
29ac0 20 20 20 20 2f 2a 20 78 53 68 6d 4d 61 70 20 6d /* xShmMap m
29ad0 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 ethod */.).#endi
29ae0 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f f..#if defined(_
29af0 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c _APPLE__) && SQL
29b00 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
29b10 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54 48 4f NG_STYLE.IOMETHO
29b20 44 53 28 0a 20 20 61 66 70 49 6f 46 69 6e 64 65 DS(. afpIoFinde
29b30 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r,
29b40 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 /* Finder functi
29b50 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70 on name */. afp
29b60 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 IoMethods,
29b70 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 /* sqlite
29b80 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 3_io_methods obj
29b90 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 31 2c ect name */. 1,
29ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
29bb0 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61 72 65 /* share
29bc0 64 20 6d 65 6d 6f 72 79 20 69 73 20 64 69 73 61 d memory is disa
29bd0 62 6c 65 64 20 2a 2f 0a 20 20 61 66 70 43 6c 6f bled */. afpClo
29be0 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 se,
29bf0 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 /* xClose me
29c00 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 4c 6f 63 thod */. afpLoc
29c10 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k,
29c20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 /* xLock met
29c30 68 6f 64 20 2a 2f 0a 20 20 61 66 70 55 6e 6c 6f hod */. afpUnlo
29c40 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ck,
29c50 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 /* xUnlock me
29c60 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 43 68 65 thod */. afpChe
29c70 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 20 ckReservedLock,
29c80 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 /* xCheckRes
29c90 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 ervedLock method
29ca0 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20 20 20 */. 0
29cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
29cc0 2f 2a 20 78 53 68 6d 4d 61 70 20 6d 65 74 68 6f /* xShmMap metho
29cd0 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 2f d */.).#endif../
29ce0 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 6c *.** The proxy l
29cf0 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 69 73 ocking method is
29d00 20 61 20 22 73 75 70 65 72 2d 6d 65 74 68 6f 64 a "super-method
29d10 22 20 69 6e 20 74 68 65 20 73 65 6e 73 65 20 74 " in the sense t
29d20 68 61 74 20 69 74 0a 2a 2a 20 6f 70 65 6e 73 20 hat it.** opens
29d30 73 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 20 64 secondary file d
29d40 65 73 63 72 69 70 74 6f 72 73 20 66 6f 72 20 74 escriptors for t
29d50 68 65 20 63 6f 6e 63 68 20 61 6e 64 20 6c 6f 63 he conch and loc
29d60 6b 20 66 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 69 k files and.** i
29d70 74 20 75 73 65 73 20 70 72 6f 78 79 2c 20 64 6f t uses proxy, do
29d80 74 2d 66 69 6c 65 2c 20 41 46 50 2c 20 61 6e 64 t-file, AFP, and
29d90 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 flock() locking
29da0 20 6d 65 74 68 6f 64 73 20 6f 6e 20 74 68 6f 73 methods on thos
29db0 65 0a 2a 2a 20 73 65 63 6f 6e 64 61 72 79 20 66 e.** secondary f
29dc0 69 6c 65 73 2e 20 20 46 6f 72 20 74 68 69 73 20 iles. For this
29dd0 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 reason, the divi
29de0 73 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d sion that implem
29df0 65 6e 74 73 0a 2a 2a 20 70 72 6f 78 79 20 6c 6f ents.** proxy lo
29e00 63 6b 69 6e 67 20 69 73 20 6c 6f 63 61 74 65 64 cking is located
29e10 20 6d 75 63 68 20 66 75 72 74 68 65 72 20 64 6f much further do
29e20 77 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 wn in the file.
29e30 20 42 75 74 20 77 65 20 6e 65 65 64 0a 2a 2a 20 But we need.**
29e40 74 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 to go ahead and
29e50 64 65 66 69 6e 65 20 74 68 65 20 73 71 6c 69 74 define the sqlit
29e60 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 61 6e e3_io_methods an
29e70 64 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f d finder functio
29e80 6e 0a 2a 2a 20 66 6f 72 20 70 72 6f 78 79 20 6c n.** for proxy l
29e90 6f 63 6b 69 6e 67 20 68 65 72 65 2e 20 20 53 6f ocking here. So
29ea0 20 77 65 20 66 6f 72 77 61 72 64 20 64 65 63 6c we forward decl
29eb0 61 72 65 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 are the I/O meth
29ec0 6f 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 ods..*/.#if defi
29ed0 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 ned(__APPLE__) &
29ee0 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f & SQLITE_ENABLE_
29ef0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 73 74 LOCKING_STYLE.st
29f00 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 6c atic int proxyCl
29f10 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ose(sqlite3_file
29f20 2a 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 70 *);.static int p
29f30 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 65 33 roxyLock(sqlite3
29f40 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 _file*, int);.st
29f50 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 55 6e atic int proxyUn
29f60 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c lock(sqlite3_fil
29f70 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69 63 e*, int);.static
29f80 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63 6b 52 int proxyCheckR
29f90 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 eservedLock(sqli
29fa0 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2a 29 te3_file*, int*)
29fb0 3b 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 ;.IOMETHODS(. p
29fc0 72 6f 78 79 49 6f 46 69 6e 64 65 72 2c 20 20 20 roxyIoFinder,
29fd0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 /* Find
29fe0 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 er function name
29ff0 20 2a 2f 0a 20 20 70 72 6f 78 79 49 6f 4d 65 74 */. proxyIoMet
2a000 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 hods,
2a010 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 /* sqlite3_io_me
2a020 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d thods object nam
2a030 65 20 2a 2f 0a 20 20 31 2c 20 20 20 20 20 20 20 e */. 1,
2a040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2a050 20 2f 2a 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 /* shared memor
2a060 79 20 69 73 20 64 69 73 61 62 6c 65 64 20 2a 2f y is disabled */
2a070 0a 20 20 70 72 6f 78 79 43 6c 6f 73 65 2c 20 20 . proxyClose,
2a080 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
2a090 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f xClose method */
2a0a0 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 2c 20 20 20 . proxyLock,
2a0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
2a0c0 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a xLock method */.
2a0d0 20 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 2c 20 20 proxyUnlock,
2a0e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
2a0f0 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f Unlock method */
2a100 0a 20 20 70 72 6f 78 79 43 68 65 63 6b 52 65 73 . proxyCheckRes
2a110 65 72 76 65 64 4c 6f 63 6b 2c 20 20 20 2f 2a 20 ervedLock, /*
2a120 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
2a130 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 30 ck method */. 0
2a140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2a150 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 68 6d /* xShm
2a160 4d 61 70 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a Map method */.).
2a170 23 65 6e 64 69 66 0a 0a 2f 2a 20 6e 66 73 20 6c #endif../* nfs l
2a180 6f 63 6b 64 20 6f 6e 20 4f 53 58 20 31 30 2e 33 ockd on OSX 10.3
2a190 2b 20 64 6f 65 73 6e 27 74 20 63 6c 65 61 72 20 + doesn't clear
2a1a0 77 72 69 74 65 20 6c 6f 63 6b 73 20 77 68 65 6e write locks when
2a1b0 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 a read lock is
2a1c0 73 65 74 20 2a 2f 0a 23 69 66 20 64 65 66 69 6e set */.#if defin
2a1d0 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 ed(__APPLE__) &&
2a1e0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
2a1f0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d OCKING_STYLE.IOM
2a200 45 54 48 4f 44 53 28 0a 20 20 6e 66 73 49 6f 46 ETHODS(. nfsIoF
2a210 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 inder,
2a220 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 /* Finder f
2a230 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a unction name */.
2a240 20 20 6e 66 73 49 6f 4d 65 74 68 6f 64 73 2c 20 nfsIoMethods,
2a250 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
2a260 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
2a270 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a ds object name *
2a280 2f 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 /. 1,
2a290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2a2a0 2a 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 * shared memory
2a2b0 69 73 20 64 69 73 61 62 6c 65 64 20 2a 2f 0a 20 is disabled */.
2a2c0 20 75 6e 69 78 43 6c 6f 73 65 2c 20 20 20 20 20 unixClose,
2a2d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
2a2e0 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a Close method */.
2a2f0 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 20 20 20 20 unixLock,
2a300 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
2a310 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a xLock method */.
2a320 20 20 6e 66 73 55 6e 6c 6f 63 6b 2c 20 20 20 20 nfsUnlock,
2a330 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
2a340 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a xUnlock method *
2a350 2f 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73 /. unixCheckRes
2a360 65 72 76 65 64 4c 6f 63 6b 2c 20 20 20 20 20 2f ervedLock, /
2a370 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 * xCheckReserved
2a380 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 Lock method */.
2a390 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
2a3a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
2a3b0 53 68 6d 4d 61 70 20 6d 65 74 68 6f 64 20 2a 2f ShmMap method */
2a3c0 0a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 64 .).#endif..#if d
2a3d0 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f efined(__APPLE__
2a3e0 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 ) && SQLITE_ENAB
2a3f0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
2a400 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 ./* .** This "fi
2a410 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 nder" function a
2a420 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 ttempts to deter
2a430 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f mine the best lo
2a440 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a cking strategy .
2a450 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 ** for the datab
2a460 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61 ase file "filePa
2a470 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65 th". It then re
2a480 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65 turns the sqlite
2a490 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 3_io_methods.**
2a4a0 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c object that impl
2a4b0 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74 72 61 ements that stra
2a4c0 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tegy..**.** This
2a4d0 20 69 73 20 66 6f 72 20 4d 61 63 4f 53 58 20 6f is for MacOSX o
2a4e0 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 nly..*/.static c
2a4f0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f onst sqlite3_io_
2a500 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63 methods *autoloc
2a510 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20 kIoFinderImpl(.
2a520 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c const char *fil
2a530 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d ePath, /* nam
2a540 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
2a550 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 e file */. unix
2a560 46 69 6c 65 20 2a 70 4e 65 77 20 20 20 20 20 20 File *pNew
2a570 20 20 20 20 20 2f 2a 20 6f 70 65 6e 20 66 69 6c /* open fil
2a580 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 e object for the
2a590 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
2a5a0 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f /.){. static co
2a5b0 6e 73 74 20 73 74 72 75 63 74 20 4d 61 70 70 69 nst struct Mappi
2a5c0 6e 67 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 ng {. const c
2a5d0 68 61 72 20 2a 7a 46 69 6c 65 73 79 73 74 65 6d har *zFilesystem
2a5e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
2a5f0 2a 20 46 69 6c 65 73 79 73 74 65 6d 20 74 79 70 * Filesystem typ
2a600 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 63 6f e name */. co
2a610 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d nst sqlite3_io_m
2a620 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 ethods *pMethods
2a630 3b 20 20 20 2f 2a 20 41 70 70 72 6f 70 72 69 61 ; /* Appropria
2a640 74 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f te locking metho
2a650 64 20 2a 2f 0a 20 20 7d 20 61 4d 61 70 5b 5d 20 d */. } aMap[]
2a660 3d 20 7b 0a 20 20 20 20 7b 20 22 68 66 73 22 2c = {. { "hfs",
2a670 20 20 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 &posixIoMeth
2a680 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22 75 66 ods },. { "uf
2a690 73 22 2c 20 20 20 20 26 70 6f 73 69 78 49 6f 4d s", &posixIoM
2a6a0 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 ethods },. {
2a6b0 22 61 66 70 66 73 22 2c 20 20 26 61 66 70 49 6f "afpfs", &afpIo
2a6c0 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b Methods },. {
2a6d0 20 22 73 6d 62 66 73 22 2c 20 20 26 61 66 70 49 "smbfs", &afpI
2a6e0 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 oMethods },.
2a6f0 7b 20 22 77 65 62 64 61 76 22 2c 20 26 6e 6f 6c { "webdav", &nol
2a700 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a ockIoMethods },.
2a710 20 20 20 20 7b 20 30 2c 20 30 20 7d 0a 20 20 7d { 0, 0 }. }
2a720 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 ;. int i;. str
2a730 75 63 74 20 73 74 61 74 66 73 20 66 73 49 6e 66 uct statfs fsInf
2a740 6f 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 o;. struct floc
2a750 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 20 69 k lockInfo;.. i
2a760 66 28 20 21 66 69 6c 65 50 61 74 68 20 29 7b 0a f( !filePath ){.
2a770 20 20 20 20 2f 2a 20 49 66 20 66 69 6c 65 50 61 /* If filePa
2a780 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 6d 65 th==NULL that me
2a790 61 6e 73 20 77 65 20 61 72 65 20 64 65 61 6c 69 ans we are deali
2a7a0 6e 67 20 77 69 74 68 20 61 20 74 72 61 6e 73 69 ng with a transi
2a7b0 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 ent file. **
2a7c0 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 that does not ne
2a7d0 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 2e ed to be locked.
2a7e0 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 26 */. return &
2a7f0 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b nolockIoMethods;
2a800 0a 20 20 7d 0a 20 20 69 66 28 20 73 74 61 74 66 . }. if( statf
2a810 73 28 66 69 6c 65 50 61 74 68 2c 20 26 66 73 49 s(filePath, &fsI
2a820 6e 66 6f 29 20 21 3d 20 2d 31 20 29 7b 0a 20 20 nfo) != -1 ){.
2a830 20 20 69 66 28 20 66 73 49 6e 66 6f 2e 66 5f 66 if( fsInfo.f_f
2a840 6c 61 67 73 20 26 20 4d 4e 54 5f 52 44 4f 4e 4c lags & MNT_RDONL
2a850 59 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 Y ){. retur
2a860 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f n &nolockIoMetho
2a870 64 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f ds;. }. fo
2a880 72 28 69 3d 30 3b 20 61 4d 61 70 5b 69 5d 2e 7a r(i=0; aMap[i].z
2a890 46 69 6c 65 73 79 73 74 65 6d 3b 20 69 2b 2b 29 Filesystem; i++)
2a8a0 7b 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63 {. if( strc
2a8b0 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79 mp(fsInfo.f_fsty
2a8c0 70 65 6e 61 6d 65 2c 20 61 4d 61 70 5b 69 5d 2e pename, aMap[i].
2a8d0 7a 46 69 6c 65 73 79 73 74 65 6d 29 3d 3d 30 20 zFilesystem)==0
2a8e0 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
2a8f0 6e 20 61 4d 61 70 5b 69 5d 2e 70 4d 65 74 68 6f n aMap[i].pMetho
2a900 64 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ds;. }.
2a910 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 66 61 }. }.. /* Defa
2a920 75 6c 74 20 63 61 73 65 2e 20 48 61 6e 64 6c 65 ult case. Handle
2a930 73 2c 20 61 6d 6f 6e 67 73 74 20 6f 74 68 65 72 s, amongst other
2a940 73 2c 20 22 6e 66 73 22 2e 0a 20 20 2a 2a 20 54 s, "nfs".. ** T
2a950 65 73 74 20 62 79 74 65 2d 72 61 6e 67 65 20 6c est byte-range l
2a960 6f 63 6b 20 75 73 69 6e 67 20 66 63 6e 74 6c 28 ock using fcntl(
2a970 29 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 20 73 ). If the call s
2a980 75 63 63 65 65 64 73 2c 20 0a 20 20 2a 2a 20 61 ucceeds, . ** a
2a990 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 20 66 ssume that the f
2a9a0 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 70 6f ile-system suppo
2a9b0 72 74 73 20 50 4f 53 49 58 20 73 74 79 6c 65 20 rts POSIX style
2a9c0 6c 6f 63 6b 73 2e 20 0a 20 20 2a 2f 0a 20 20 6c locks. . */. l
2a9d0 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 ockInfo.l_len =
2a9e0 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 1;. lockInfo.l_
2a9f0 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 start = 0;. loc
2aa00 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d kInfo.l_whence =
2aa10 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 SEEK_SET;. loc
2aa20 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 kInfo.l_type = F
2aa30 5f 52 44 4c 43 4b 3b 0a 20 20 69 66 28 20 6f 73 _RDLCK;. if( os
2aa40 46 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 2c 20 46 Fcntl(pNew->h, F
2aa50 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 6e 66 _GETLK, &lockInf
2aa60 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 20 20 69 o)!=-1 ) {. i
2aa70 66 28 20 73 74 72 63 6d 70 28 66 73 49 6e 66 6f f( strcmp(fsInfo
2aa80 2e 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20 22 .f_fstypename, "
2aa90 6e 66 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 nfs")==0 ){.
2aaa0 20 20 72 65 74 75 72 6e 20 26 6e 66 73 49 6f 4d return &nfsIoM
2aab0 65 74 68 6f 64 73 3b 0a 20 20 20 20 7d 20 65 6c ethods;. } el
2aac0 73 65 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 se {. retur
2aad0 6e 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 n &posixIoMethod
2aae0 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 s;. }. }else
2aaf0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64 6f {. return &do
2ab00 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a tlockIoMethods;.
2ab10 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e }.}.static con
2ab20 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
2ab30 74 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 thods . *(*cons
2ab40 74 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 t autolockIoFind
2ab50 65 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c er)(const char*,
2ab60 75 6e 69 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 unixFile*) = aut
2ab70 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 olockIoFinderImp
2ab80 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 l;..#endif /* de
2ab90 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
2aba0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
2abb0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
2abc0 2a 2f 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 */..#if OS_VXWOR
2abd0 4b 53 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 22 66 KS./*.** This "f
2abe0 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 inder" function
2abf0 66 6f 72 20 56 78 57 6f 72 6b 73 20 63 68 65 63 for VxWorks chec
2ac00 6b 73 20 74 6f 20 73 65 65 20 69 66 20 70 6f 73 ks to see if pos
2ac10 69 78 20 61 64 76 69 73 6f 72 79 0a 2a 2a 20 6c ix advisory.** l
2ac20 6f 63 6b 69 6e 67 20 77 6f 72 6b 73 2e 20 20 49 ocking works. I
2ac30 66 20 69 74 20 64 6f 65 73 2c 20 74 68 65 6e 20 f it does, then
2ac40 74 68 61 74 20 69 73 20 77 68 61 74 20 69 73 20 that is what is
2ac50 75 73 65 64 2e 20 20 49 66 20 69 74 20 64 6f 65 used. If it doe
2ac60 73 20 6e 6f 74 0a 2a 2a 20 77 6f 72 6b 2c 20 74 s not.** work, t
2ac70 68 65 6e 20 66 61 6c 6c 62 61 63 6b 20 74 6f 20 hen fallback to
2ac80 6e 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 named semaphore
2ac90 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 locking..*/.stat
2aca0 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
2acb0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 76 78 77 _io_methods *vxw
2acc0 6f 72 6b 73 49 6f 46 69 6e 64 65 72 49 6d 70 6c orksIoFinderImpl
2acd0 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a (. const char *
2ace0 66 69 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 filePath, /*
2acf0 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 name of the data
2ad00 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 base file */. u
2ad10 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 20 20 nixFile *pNew
2ad20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 6f /* the o
2ad30 70 65 6e 20 66 69 6c 65 20 6f 62 6a 65 63 74 20 pen file object
2ad40 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 66 */.){. struct f
2ad50 6c 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a lock lockInfo;..
2ad60 20 20 69 66 28 20 21 66 69 6c 65 50 61 74 68 20 if( !filePath
2ad70 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c ){. /* If fil
2ad80 65 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 ePath==NULL that
2ad90 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 means we are de
2ada0 61 6c 69 6e 67 20 77 69 74 68 20 61 20 74 72 61 aling with a tra
2adb0 6e 73 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 nsient file.
2adc0 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 ** that does not
2add0 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b need to be lock
2ade0 65 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 ed. */. retur
2adf0 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f n &nolockIoMetho
2ae00 64 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 ds;. }.. /* Te
2ae10 73 74 20 69 66 20 66 63 6e 74 6c 28 29 20 69 73 st if fcntl() is
2ae20 20 73 75 70 70 6f 72 74 65 64 20 61 6e 64 20 75 supported and u
2ae30 73 65 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c se POSIX style l
2ae40 6f 63 6b 73 2e 0a 20 20 2a 2a 20 4f 74 68 65 72 ocks.. ** Other
2ae50 77 69 73 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 wise fall back t
2ae60 6f 20 74 68 65 20 6e 61 6d 65 64 20 73 65 6d 61 o the named sema
2ae70 70 68 6f 72 65 20 6d 65 74 68 6f 64 2e 0a 20 20 phore method..
2ae80 2a 2f 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f */. lockInfo.l_
2ae90 6c 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 len = 1;. lockI
2aea0 6e 66 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b nfo.l_start = 0;
2aeb0 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 . lockInfo.l_wh
2aec0 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b ence = SEEK_SET;
2aed0 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 . lockInfo.l_ty
2aee0 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 pe = F_RDLCK;.
2aef0 69 66 28 20 6f 73 46 63 6e 74 6c 28 70 4e 65 77 if( osFcntl(pNew
2af00 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c ->h, F_GETLK, &l
2af10 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b ockInfo)!=-1 ) {
2af20 0a 20 20 20 20 72 65 74 75 72 6e 20 26 70 6f 73 . return &pos
2af30 69 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d ixIoMethods;. }
2af40 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
2af50 20 26 73 65 6d 49 6f 4d 65 74 68 6f 64 73 3b 0a &semIoMethods;.
2af60 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e }.}.static con
2af70 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
2af80 74 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 thods . *(*cons
2af90 74 20 76 78 77 6f 72 6b 73 49 6f 46 69 6e 64 65 t vxworksIoFinde
2afa0 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 r)(const char*,u
2afb0 6e 69 78 46 69 6c 65 2a 29 20 3d 20 76 78 77 6f nixFile*) = vxwo
2afc0 72 6b 73 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b rksIoFinderImpl;
2afd0 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 ..#endif /* OS_V
2afe0 58 57 4f 52 4b 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a XWORKS */../*.**
2aff0 20 41 6e 20 61 62 73 74 72 61 63 74 20 74 79 70 An abstract typ
2b000 65 20 66 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 e for a pointer
2b010 74 6f 20 61 6e 20 49 4f 20 6d 65 74 68 6f 64 20 to an IO method
2b020 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 3a finder function:
2b030 0a 2a 2f 0a 74 79 70 65 64 65 66 20 63 6f 6e 73 .*/.typedef cons
2b040 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
2b050 68 6f 64 73 20 2a 28 2a 66 69 6e 64 65 72 5f 74 hods *(*finder_t
2b060 79 70 65 29 28 63 6f 6e 73 74 20 63 68 61 72 2a ype)(const char*
2b070 2c 75 6e 69 78 46 69 6c 65 2a 29 3b 0a 0a 0a 2f ,unixFile*);.../
2b080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2b090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2b0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2b0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2b0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
2b0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2b0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 ********* sqlite
2b0f0 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 2a 2a 3_vfs methods **
2b100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2b110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
2b120 20 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 63 This division c
2b130 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c ontains the impl
2b140 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 65 ementation of me
2b150 74 68 6f 64 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 thods on the.**
2b160 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 sqlite3_vfs obje
2b170 63 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e ct..*/../*.** In
2b180 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e itialize the con
2b190 74 65 6e 74 73 20 6f 66 20 74 68 65 20 75 6e 69 tents of the uni
2b1a0 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 xFile structure
2b1b0 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 49 pointed to by pI
2b1c0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
2b1d0 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 fillInUnixFile(
2b1e0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
2b1f0 70 56 66 73 2c 20 20 20 20 20 20 2f 2a 20 50 6f pVfs, /* Po
2b200 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a inter to vfs obj
2b210 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 ect */. int h,
2b220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2b230 20 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 /* Open file de
2b240 73 63 72 69 70 74 6f 72 20 6f 66 20 66 69 6c 65 scriptor of file
2b250 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f being opened */
2b260 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
2b270 2a 70 49 64 2c 20 20 20 20 20 20 2f 2a 20 57 72 *pId, /* Wr
2b280 69 74 65 20 74 6f 20 74 68 65 20 75 6e 69 78 46 ite to the unixF
2b290 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 68 65 ile structure he
2b2a0 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 re */. const ch
2b2b0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 ar *zFilename,
2b2c0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 /* Name of the f
2b2d0 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 ile being opened
2b2e0 20 2a 2f 0a 20 20 69 6e 74 20 63 74 72 6c 46 6c */. int ctrlFl
2b2f0 61 67 73 20 20 20 20 20 20 20 20 20 20 20 2f 2a ags /*
2b300 20 5a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 55 4e Zero or more UN
2b310 49 58 46 49 4c 45 5f 2a 20 76 61 6c 75 65 73 20 IXFILE_* values
2b320 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 */.){. const sq
2b330 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
2b340 20 2a 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b *pLockingStyle;
2b350 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 . unixFile *pNe
2b360 77 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 w = (unixFile *)
2b370 70 49 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 pId;. int rc =
2b380 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 SQLITE_OK;.. as
2b390 73 65 72 74 28 20 70 4e 65 77 2d 3e 70 49 6e 6f sert( pNew->pIno
2b3a0 64 65 3d 3d 4e 55 4c 4c 20 29 3b 0a 0a 20 20 2f de==NULL );.. /
2b3b0 2a 20 55 73 75 61 6c 6c 79 20 74 68 65 20 70 61 * Usually the pa
2b3c0 74 68 20 7a 46 69 6c 65 6e 61 6d 65 20 73 68 6f th zFilename sho
2b3d0 75 6c 64 20 6e 6f 74 20 62 65 20 61 20 72 65 6c uld not be a rel
2b3e0 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 2e 20 ative pathname.
2b3f0 54 68 65 0a 20 20 2a 2a 20 65 78 63 65 70 74 69 The. ** excepti
2b400 6f 6e 20 69 73 20 77 68 65 6e 20 6f 70 65 6e 69 on is when openi
2b410 6e 67 20 74 68 65 20 70 72 6f 78 79 20 22 63 6f ng the proxy "co
2b420 6e 63 68 22 20 66 69 6c 65 20 69 6e 20 62 75 69 nch" file in bui
2b430 6c 64 73 20 74 68 61 74 0a 20 20 2a 2a 20 69 6e lds that. ** in
2b440 63 6c 75 64 65 20 74 68 65 20 73 70 65 63 69 61 clude the specia
2b450 6c 20 41 70 70 6c 65 20 6c 6f 63 6b 69 6e 67 20 l Apple locking
2b460 73 74 79 6c 65 73 2e 0a 20 20 2a 2f 0a 23 69 66 styles.. */.#if
2b470 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 defined(__APPLE
2b480 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e __) && SQLITE_EN
2b490 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
2b4a0 4c 45 0a 20 20 61 73 73 65 72 74 28 20 7a 46 69 LE. assert( zFi
2b4b0 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 lename==0 || zFi
2b4c0 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20 0a lename[0]=='/' .
2b4d0 20 20 20 20 7c 7c 20 70 56 66 73 2d 3e 70 41 70 || pVfs->pAp
2b4e0 70 44 61 74 61 3d 3d 28 76 6f 69 64 2a 29 26 61 pData==(void*)&a
2b4f0 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 utolockIoFinder
2b500 29 3b 0a 23 65 6c 73 65 0a 20 20 61 73 73 65 72 );.#else. asser
2b510 74 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 t( zFilename==0
2b520 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d || zFilename[0]=
2b530 3d 27 2f 27 20 29 3b 0a 23 65 6e 64 69 66 0a 0a ='/' );.#endif..
2b540 20 20 2f 2a 20 4e 6f 20 6c 6f 63 6b 69 6e 67 20 /* No locking
2b550 6f 63 63 75 72 73 20 69 6e 20 74 65 6d 70 6f 72 occurs in tempor
2b560 61 72 79 20 66 69 6c 65 73 20 2a 2f 0a 20 20 61 ary files */. a
2b570 73 73 65 72 74 28 20 7a 46 69 6c 65 6e 61 6d 65 ssert( zFilename
2b580 21 3d 30 20 7c 7c 20 28 63 74 72 6c 46 6c 61 67 !=0 || (ctrlFlag
2b590 73 20 26 20 55 4e 49 58 46 49 4c 45 5f 4e 4f 4c s & UNIXFILE_NOL
2b5a0 4f 43 4b 29 21 3d 30 20 29 3b 0a 0a 20 20 4f 53 OCK)!=0 );.. OS
2b5b0 54 52 41 43 45 28 28 22 4f 50 45 4e 20 20 20 20 TRACE(("OPEN
2b5c0 25 2d 33 64 20 25 73 5c 6e 22 2c 20 68 2c 20 7a %-3d %s\n", h, z
2b5d0 46 69 6c 65 6e 61 6d 65 29 29 3b 0a 20 20 70 4e Filename));. pN
2b5e0 65 77 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 4e 65 ew->h = h;. pNe
2b5f0 77 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a w->pVfs = pVfs;.
2b600 20 20 70 4e 65 77 2d 3e 7a 50 61 74 68 20 3d 20 pNew->zPath =
2b610 7a 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 70 4e 65 zFilename;. pNe
2b620 77 2d 3e 63 74 72 6c 46 6c 61 67 73 20 3d 20 28 w->ctrlFlags = (
2b630 75 38 29 63 74 72 6c 46 6c 61 67 73 3b 0a 23 69 u8)ctrlFlags;.#i
2b640 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4d 4d 41 f SQLITE_MAX_MMA
2b650 50 5f 53 49 5a 45 3e 30 0a 20 20 70 4e 65 77 2d P_SIZE>0. pNew-
2b660 3e 6d 6d 61 70 53 69 7a 65 4d 61 78 20 3d 20 73 >mmapSizeMax = s
2b670 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
2b680 69 67 2e 73 7a 4d 6d 61 70 3b 0a 23 65 6e 64 69 ig.szMmap;.#endi
2b690 66 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f f. if( sqlite3_
2b6a0 75 72 69 5f 62 6f 6f 6c 65 61 6e 28 28 28 63 74 uri_boolean(((ct
2b6b0 72 6c 46 6c 61 67 73 20 26 20 55 4e 49 58 46 49 rlFlags & UNIXFI
2b6c0 4c 45 5f 55 52 49 29 20 3f 20 7a 46 69 6c 65 6e LE_URI) ? zFilen
2b6d0 61 6d 65 20 3a 20 30 29 2c 0a 20 20 20 20 20 20 ame : 0),.
2b6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2b6f0 20 20 20 20 20 22 70 73 6f 77 22 2c 20 53 51 4c "psow", SQL
2b700 49 54 45 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 ITE_POWERSAFE_OV
2b710 45 52 57 52 49 54 45 29 20 29 7b 0a 20 20 20 20 ERWRITE) ){.
2b720 70 4e 65 77 2d 3e 63 74 72 6c 46 6c 61 67 73 20 pNew->ctrlFlags
2b730 7c 3d 20 55 4e 49 58 46 49 4c 45 5f 50 53 4f 57 |= UNIXFILE_PSOW
2b740 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 74 72 63 ;. }. if( strc
2b750 6d 70 28 70 56 66 73 2d 3e 7a 4e 61 6d 65 2c 22 mp(pVfs->zName,"
2b760 75 6e 69 78 2d 65 78 63 6c 22 29 3d 3d 30 20 29 unix-excl")==0 )
2b770 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 63 74 72 6c {. pNew->ctrl
2b780 46 6c 61 67 73 20 7c 3d 20 55 4e 49 58 46 49 4c Flags |= UNIXFIL
2b790 45 5f 45 58 43 4c 3b 0a 20 20 7d 0a 0a 23 69 66 E_EXCL;. }..#if
2b7a0 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 70 4e OS_VXWORKS. pN
2b7b0 65 77 2d 3e 70 49 64 20 3d 20 76 78 77 6f 72 6b ew->pId = vxwork
2b7c0 73 46 69 6e 64 46 69 6c 65 49 64 28 7a 46 69 6c sFindFileId(zFil
2b7d0 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 4e ename);. if( pN
2b7e0 65 77 2d 3e 70 49 64 3d 3d 30 20 29 7b 0a 20 20 ew->pId==0 ){.
2b7f0 20 20 63 74 72 6c 46 6c 61 67 73 20 7c 3d 20 55 ctrlFlags |= U
2b800 4e 49 58 46 49 4c 45 5f 4e 4f 4c 4f 43 4b 3b 0a NIXFILE_NOLOCK;.
2b810 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
2b820 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 20 7d 0a NOMEM_BKPT;. }.
2b830 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 63 74 #endif.. if( ct
2b840 72 6c 46 6c 61 67 73 20 26 20 55 4e 49 58 46 49 rlFlags & UNIXFI
2b850 4c 45 5f 4e 4f 4c 4f 43 4b 20 29 7b 0a 20 20 20 LE_NOLOCK ){.
2b860 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d pLockingStyle =
2b870 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 &nolockIoMethod
2b880 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 s;. }else{.
2b890 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 pLockingStyle =
2b8a0 28 2a 2a 28 66 69 6e 64 65 72 5f 74 79 70 65 2a (**(finder_type*
2b8b0 29 70 56 66 73 2d 3e 70 41 70 70 44 61 74 61 29 )pVfs->pAppData)
2b8c0 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 4e 65 77 (zFilename, pNew
2b8d0 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e );.#if SQLITE_EN
2b8e0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
2b8f0 4c 45 0a 20 20 20 20 2f 2a 20 43 61 63 68 65 20 LE. /* Cache
2b900 7a 46 69 6c 65 6e 61 6d 65 20 69 6e 20 74 68 65 zFilename in the
2b910 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 locking context
2b920 20 28 41 46 50 20 61 6e 64 20 64 6f 74 6c 6f 63 (AFP and dotloc
2b930 6b 20 6f 76 65 72 72 69 64 65 29 20 66 6f 72 0a k override) for.
2b940 20 20 20 20 2a 2a 20 70 72 6f 78 79 4c 6f 63 6b ** proxyLock
2b950 20 61 63 74 69 76 61 74 69 6f 6e 20 69 73 20 70 activation is p
2b960 6f 73 73 69 62 6c 65 20 28 72 65 6d 6f 74 65 20 ossible (remote
2b970 70 72 6f 78 79 20 69 73 20 62 61 73 65 64 20 6f proxy is based o
2b980 6e 20 64 62 20 6e 61 6d 65 29 0a 20 20 20 20 2a n db name). *
2b990 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 72 65 6d 61 * zFilename rema
2b9a0 69 6e 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 ins valid until
2b9b0 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 2c 20 file is closed,
2b9c0 74 6f 20 73 75 70 70 6f 72 74 20 2a 2f 0a 20 20 to support */.
2b9d0 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 pNew->lockingC
2b9e0 6f 6e 74 65 78 74 20 3d 20 28 76 6f 69 64 2a 29 ontext = (void*)
2b9f0 7a 46 69 6c 65 6e 61 6d 65 3b 0a 23 65 6e 64 69 zFilename;.#endi
2ba00 66 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4c 6f f. }.. if( pLo
2ba10 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 70 ckingStyle == &p
2ba20 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 0a 23 69 osixIoMethods.#i
2ba30 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c f defined(__APPL
2ba40 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 E__) && SQLITE_E
2ba50 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
2ba60 59 4c 45 0a 20 20 20 20 7c 7c 20 70 4c 6f 63 6b YLE. || pLock
2ba70 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 6e 66 73 ingStyle == &nfs
2ba80 49 6f 4d 65 74 68 6f 64 73 0a 23 65 6e 64 69 66 IoMethods.#endif
2ba90 0a 20 20 29 7b 0a 20 20 20 20 75 6e 69 78 45 6e . ){. unixEn
2baa0 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 terMutex();.
2bab0 72 63 20 3d 20 66 69 6e 64 49 6e 6f 64 65 49 6e rc = findInodeIn
2bac0 66 6f 28 70 4e 65 77 2c 20 26 70 4e 65 77 2d 3e fo(pNew, &pNew->
2bad0 70 49 6e 6f 64 65 29 3b 0a 20 20 20 20 69 66 28 pInode);. if(
2bae0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
2baf0 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6e {. /* If an
2bb00 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 error occurred
2bb10 69 6e 20 66 69 6e 64 49 6e 6f 64 65 49 6e 66 6f in findInodeInfo
2bb20 28 29 2c 20 63 6c 6f 73 65 20 74 68 65 20 66 69 (), close the fi
2bb30 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 20 20 le descriptor.
2bb40 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 ** immediate
2bb50 6c 79 2c 20 62 65 66 6f 72 65 20 72 65 6c 65 61 ly, before relea
2bb60 73 69 6e 67 20 74 68 65 20 6d 75 74 65 78 2e 20 sing the mutex.
2bb70 66 69 6e 64 49 6e 6f 64 65 49 6e 66 6f 28 29 20 findInodeInfo()
2bb80 6d 61 79 20 66 61 69 6c 0a 20 20 20 20 20 20 2a may fail. *
2bb90 2a 20 69 6e 20 74 77 6f 20 73 63 65 6e 61 72 69 * in two scenari
2bba0 6f 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 os:. **.
2bbb0 20 20 20 2a 2a 20 20 20 28 61 29 20 41 20 63 61 ** (a) A ca
2bbc0 6c 6c 20 74 6f 20 66 73 74 61 74 28 29 20 66 61 ll to fstat() fa
2bbd0 69 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 20 iled.. **
2bbe0 20 28 62 29 20 41 20 6d 61 6c 6c 6f 63 20 66 61 (b) A malloc fa
2bbf0 69 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 iled.. **.
2bc00 20 20 20 20 20 2a 2a 20 53 63 65 6e 61 72 69 6f ** Scenario
2bc10 20 28 62 29 20 6d 61 79 20 6f 6e 6c 79 20 6f 63 (b) may only oc
2bc20 63 75 72 20 69 66 20 74 68 65 20 70 72 6f 63 65 cur if the proce
2bc30 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 6e 6f ss is holding no
2bc40 20 6f 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 other. **
2bc50 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 file descriptors
2bc60 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d open on the sam
2bc70 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 e file. If there
2bc80 20 77 65 72 65 20 6f 74 68 65 72 20 66 69 6c 65 were other file
2bc90 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 . ** descri
2bca0 70 74 6f 72 73 20 6f 6e 20 74 68 69 73 20 66 69 ptors on this fi
2bcb0 6c 65 2c 20 74 68 65 6e 20 6e 6f 20 6d 61 6c 6c le, then no mall
2bcc0 6f 63 20 77 6f 75 6c 64 20 62 65 20 72 65 71 75 oc would be requ
2bcd0 69 72 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a ired by. **
2bce0 20 66 69 6e 64 49 6e 6f 64 65 49 6e 66 6f 28 29 findInodeInfo()
2bcf0 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 . If this is the
2bd00 20 63 61 73 65 2c 20 69 74 20 69 73 20 71 75 69 case, it is qui
2bd10 74 65 20 73 61 66 65 20 74 6f 20 63 6c 6f 73 65 te safe to close
2bd20 0a 20 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 . ** handle
2bd30 20 68 20 2d 20 61 73 20 69 74 20 69 73 20 67 75 h - as it is gu
2bd40 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e 6f aranteed that no
2bd50 20 70 6f 73 69 78 20 6c 6f 63 6b 73 20 77 69 6c posix locks wil
2bd60 6c 20 62 65 20 72 65 6c 65 61 73 65 64 0a 20 20 l be released.
2bd70 20 20 20 20 2a 2a 20 62 79 20 64 6f 69 6e 67 20 ** by doing
2bd80 73 6f 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 so.. **.
2bd90 20 20 20 2a 2a 20 49 66 20 73 63 65 6e 61 72 69 ** If scenari
2bda0 6f 20 28 61 29 20 63 61 75 73 65 64 20 74 68 65 o (a) caused the
2bdb0 20 65 72 72 6f 72 20 74 68 65 6e 20 74 68 69 6e error then thin
2bdc0 67 73 20 61 72 65 20 6e 6f 74 20 73 6f 20 73 61 gs are not so sa
2bdd0 66 65 2e 20 54 68 65 0a 20 20 20 20 20 20 2a 2a fe. The. **
2bde0 20 69 6d 70 6c 69 63 69 74 20 61 73 73 75 6d 70 implicit assump
2bdf0 74 69 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61 tion here is tha
2be00 74 20 69 66 20 66 73 74 61 74 28 29 20 66 61 69 t if fstat() fai
2be10 6c 73 2c 20 74 68 69 6e 67 73 20 61 72 65 20 69 ls, things are i
2be20 6e 0a 20 20 20 20 20 20 2a 2a 20 73 75 63 68 20 n. ** such
2be30 62 61 64 20 73 68 61 70 65 20 74 68 61 74 20 64 bad shape that d
2be40 72 6f 70 70 69 6e 67 20 61 20 6c 6f 63 6b 20 6f ropping a lock o
2be50 72 20 74 77 6f 20 64 6f 65 73 6e 27 74 20 6d 61 r two doesn't ma
2be60 74 74 65 72 20 6d 75 63 68 2e 0a 20 20 20 20 20 tter much..
2be70 20 2a 2f 0a 20 20 20 20 20 20 72 6f 62 75 73 74 */. robust
2be80 5f 63 6c 6f 73 65 28 70 4e 65 77 2c 20 68 2c 20 _close(pNew, h,
2be90 5f 5f 4c 49 4e 45 5f 5f 29 3b 0a 20 20 20 20 20 __LINE__);.
2bea0 20 68 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 h = -1;. }.
2beb0 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 unixLeaveMute
2bec0 78 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 20 53 51 x();. }..#if SQ
2bed0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
2bee0 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 ING_STYLE && def
2bef0 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a ined(__APPLE__).
2bf00 20 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63 6b else if( pLock
2bf10 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 61 66 70 ingStyle == &afp
2bf20 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 IoMethods ){.
2bf30 20 2f 2a 20 41 46 50 20 6c 6f 63 6b 69 6e 67 20 /* AFP locking
2bf40 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 uses the file pa
2bf50 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 th so it needs t
2bf60 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e o be included in
2bf70 0a 20 20 20 20 2a 2a 20 74 68 65 20 61 66 70 4c . ** the afpL
2bf80 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2e 0a 20 ockingContext..
2bf90 20 20 20 2a 2f 0a 20 20 20 20 61 66 70 4c 6f 63 */. afpLoc
2bfa0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 kingContext *pCt
2bfb0 78 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 x;. pNew->loc
2bfc0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 kingContext = pC
2bfd0 74 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c tx = sqlite3_mal
2bfe0 6c 6f 63 36 34 28 20 73 69 7a 65 6f 66 28 2a 70 loc64( sizeof(*p
2bff0 43 74 78 29 20 29 3b 0a 20 20 20 20 69 66 28 20 Ctx) );. if(
2c000 70 43 74 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 pCtx==0 ){.
2c010 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
2c020 45 4d 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 65 6c EM_BKPT;. }el
2c030 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 42 3a se{. /* NB:
2c040 20 7a 46 69 6c 65 6e 61 6d 65 20 65 78 69 73 74 zFilename exist
2c050 73 20 61 6e 64 20 72 65 6d 61 69 6e 73 20 76 61 s and remains va
2c060 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 66 69 lid until the fi
2c070 6c 65 20 69 73 20 63 6c 6f 73 65 64 0a 20 20 20 le is closed.
2c080 20 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20 ** according
2c090 74 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20 46 to requirement F
2c0a0 31 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64 6f 11141. So we do
2c0b0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b not need to mak
2c0c0 65 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70 e a. ** cop
2c0d0 79 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61 6d y of the filenam
2c0e0 65 2e 20 2a 2f 0a 20 20 20 20 20 20 70 43 74 78 e. */. pCtx
2c0f0 2d 3e 64 62 50 61 74 68 20 3d 20 7a 46 69 6c 65 ->dbPath = zFile
2c100 6e 61 6d 65 3b 0a 20 20 20 20 20 20 70 43 74 78 name;. pCtx
2c110 2d 3e 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a ->reserved = 0;.
2c120 20 20 20 20 20 20 73 72 61 6e 64 6f 6d 64 65 76 srandomdev
2c130 28 29 3b 0a 20 20 20 20 20 20 75 6e 69 78 45 6e ();. unixEn
2c140 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 terMutex();.
2c150 20 20 72 63 20 3d 20 66 69 6e 64 49 6e 6f 64 65 rc = findInode
2c160 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70 4e 65 77 Info(pNew, &pNew
2c170 2d 3e 70 49 6e 6f 64 65 29 3b 0a 20 20 20 20 20 ->pInode);.
2c180 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
2c190 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 OK ){. sq
2c1a0 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 65 77 2d lite3_free(pNew-
2c1b0 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 >lockingContext)
2c1c0 3b 0a 20 20 20 20 20 20 20 20 72 6f 62 75 73 74 ;. robust
2c1d0 5f 63 6c 6f 73 65 28 70 4e 65 77 2c 20 68 2c 20 _close(pNew, h,
2c1e0 5f 5f 4c 49 4e 45 5f 5f 29 3b 0a 20 20 20 20 20 __LINE__);.
2c1f0 20 20 20 68 20 3d 20 2d 31 3b 0a 20 20 20 20 20 h = -1;.
2c200 20 7d 0a 20 20 20 20 20 20 75 6e 69 78 4c 65 61 }. unixLea
2c210 76 65 4d 75 74 65 78 28 29 3b 20 20 20 20 20 20 veMutex();
2c220 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e . }. }.#en
2c230 64 69 66 0a 0a 20 20 65 6c 73 65 20 69 66 28 20 dif.. else if(
2c240 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d pLockingStyle ==
2c250 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f &dotlockIoMetho
2c260 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 44 6f 74 ds ){. /* Dot
2c270 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 file locking use
2c280 73 20 74 68 65 20 66 69 6c 65 20 70 61 74 68 20 s the file path
2c290 73 6f 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 so it needs to b
2c2a0 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 20 20 e included in.
2c2b0 20 20 2a 2a 20 74 68 65 20 64 6f 74 6c 6f 63 6b ** the dotlock
2c2c0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 0a LockingContext .
2c2d0 20 20 20 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 */. char
2c2e0 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20 20 20 *zLockFile;.
2c2f0 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65 3b 0a 20 int nFilename;.
2c300 20 20 20 61 73 73 65 72 74 28 20 7a 46 69 6c 65 assert( zFile
2c310 6e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 20 20 6e name!=0 );. n
2c320 46 69 6c 65 6e 61 6d 65 20 3d 20 28 69 6e 74 29 Filename = (int)
2c330 73 74 72 6c 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 strlen(zFilename
2c340 29 20 2b 20 36 3b 0a 20 20 20 20 7a 4c 6f 63 6b ) + 6;. zLock
2c350 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 73 File = (char *)s
2c360 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 36 34 28 qlite3_malloc64(
2c370 6e 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 nFilename);.
2c380 69 66 28 20 7a 4c 6f 63 6b 46 69 6c 65 3d 3d 30 if( zLockFile==0
2c390 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
2c3a0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 QLITE_NOMEM_BKPT
2c3b0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
2c3c0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
2c3d0 6e 74 66 28 6e 46 69 6c 65 6e 61 6d 65 2c 20 7a ntf(nFilename, z
2c3e0 4c 6f 63 6b 46 69 6c 65 2c 20 22 25 73 22 20 44 LockFile, "%s" D
2c3f0 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 2c 20 7a OTLOCK_SUFFIX, z
2c400 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7d Filename);. }
2c410 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 . pNew->locki
2c420 6e 67 43 6f 6e 74 65 78 74 20 3d 20 7a 4c 6f 63 ngContext = zLoc
2c430 6b 46 69 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 20 kFile;. }..#if
2c440 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 65 6c 73 OS_VXWORKS. els
2c450 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 e if( pLockingSt
2c460 79 6c 65 20 3d 3d 20 26 73 65 6d 49 6f 4d 65 74 yle == &semIoMet
2c470 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4e hods ){. /* N
2c480 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c amed semaphore l
2c490 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 ocking uses the
2c4a0 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 file path so it
2c4b0 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 needs to be.
2c4c0 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 ** included in t
2c4d0 68 65 20 73 65 6d 4c 6f 63 6b 69 6e 67 43 6f 6e he semLockingCon
2c4e0 74 65 78 74 0a 20 20 20 20 2a 2f 0a 20 20 20 20 text. */.
2c4f0 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
2c500 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 49 ;. rc = findI
2c510 6e 6f 64 65 49 6e 66 6f 28 70 4e 65 77 2c 20 26 nodeInfo(pNew, &
2c520 70 4e 65 77 2d 3e 70 49 6e 6f 64 65 29 3b 0a 20 pNew->pInode);.
2c530 20 20 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 if( (rc==SQLI
2c540 54 45 5f 4f 4b 29 20 26 26 20 28 70 4e 65 77 2d TE_OK) && (pNew-
2c550 3e 70 49 6e 6f 64 65 2d 3e 70 53 65 6d 3d 3d 4e >pInode->pSem==N
2c560 55 4c 4c 29 20 29 7b 0a 20 20 20 20 20 20 63 68 ULL) ){. ch
2c570 61 72 20 2a 7a 53 65 6d 4e 61 6d 65 20 3d 20 70 ar *zSemName = p
2c580 4e 65 77 2d 3e 70 49 6e 6f 64 65 2d 3e 61 53 65 New->pInode->aSe
2c590 6d 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 6e 74 mName;. int
2c5a0 20 6e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 n;. sqlite
2c5b0 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 3_snprintf(MAX_P
2c5c0 41 54 48 4e 41 4d 45 2c 20 7a 53 65 6d 4e 61 6d ATHNAME, zSemNam
2c5d0 65 2c 20 22 2f 25 73 2e 73 65 6d 22 2c 0a 20 20 e, "/%s.sem",.
2c5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2c5f0 20 20 20 20 20 70 4e 65 77 2d 3e 70 49 64 2d 3e pNew->pId->
2c600 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 29 3b zCanonicalName);
2c610 0a 20 20 20 20 20 20 66 6f 72 28 20 6e 3d 31 3b . for( n=1;
2c620 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 3b 20 6e 2b zSemName[n]; n+
2c630 2b 20 29 0a 20 20 20 20 20 20 20 20 69 66 28 20 + ). if(
2c640 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 3d 3d 27 2f 27 zSemName[n]=='/'
2c650 20 29 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 20 3d ) zSemName[n] =
2c660 20 27 5f 27 3b 0a 20 20 20 20 20 20 70 4e 65 77 '_';. pNew
2c670 2d 3e 70 49 6e 6f 64 65 2d 3e 70 53 65 6d 20 3d ->pInode->pSem =
2c680 20 73 65 6d 5f 6f 70 65 6e 28 7a 53 65 6d 4e 61 sem_open(zSemNa
2c690 6d 65 2c 20 4f 5f 43 52 45 41 54 2c 20 30 36 36 me, O_CREAT, 066
2c6a0 36 2c 20 31 29 3b 0a 20 20 20 20 20 20 69 66 28 6, 1);. if(
2c6b0 20 70 4e 65 77 2d 3e 70 49 6e 6f 64 65 2d 3e 70 pNew->pInode->p
2c6c0 53 65 6d 20 3d 3d 20 53 45 4d 5f 46 41 49 4c 45 Sem == SEM_FAILE
2c6d0 44 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 D ){. rc
2c6e0 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5f 42 = SQLITE_NOMEM_B
2c6f0 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 KPT;. pNe
2c700 77 2d 3e 70 49 6e 6f 64 65 2d 3e 61 53 65 6d 4e w->pInode->aSemN
2c710 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 20 ame[0] = '\0';.
2c720 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
2c730 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 unixLeaveMutex(
2c740 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 );. }.#endif.
2c750 0a 20 20 73 74 6f 72 65 4c 61 73 74 45 72 72 6e . storeLastErrn
2c760 6f 28 70 4e 65 77 2c 20 30 29 3b 0a 23 69 66 20 o(pNew, 0);.#if
2c770 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 OS_VXWORKS. if(
2c780 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
2c790 7b 0a 20 20 20 20 69 66 28 20 68 3e 3d 30 20 29 {. if( h>=0 )
2c7a0 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 70 4e robust_close(pN
2c7b0 65 77 2c 20 68 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 ew, h, __LINE__)
2c7c0 3b 0a 20 20 20 20 68 20 3d 20 2d 31 3b 0a 20 20 ;. h = -1;.
2c7d0 20 20 6f 73 55 6e 6c 69 6e 6b 28 7a 46 69 6c 65 osUnlink(zFile
2c7e0 6e 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65 77 2d name);. pNew-
2c7f0 3e 63 74 72 6c 46 6c 61 67 73 20 7c 3d 20 55 4e >ctrlFlags |= UN
2c800 49 58 46 49 4c 45 5f 44 45 4c 45 54 45 3b 0a 20 IXFILE_DELETE;.
2c810 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 }.#endif. if(
2c820 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
2c830 0a 20 20 20 20 69 66 28 20 68 3e 3d 30 20 29 20 . if( h>=0 )
2c840 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 70 4e 65 robust_close(pNe
2c850 77 2c 20 68 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b w, h, __LINE__);
2c860 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e . }else{. pN
2c870 65 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70 4c ew->pMethod = pL
2c880 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 20 ockingStyle;.
2c890 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 OpenCounter(+1)
2c8a0 3b 0a 20 20 20 20 76 65 72 69 66 79 44 62 46 69 ;. verifyDbFi
2c8b0 6c 65 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 20 20 le(pNew);. }.
2c8c0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
2c8d0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
2c8e0 61 6d 65 20 6f 66 20 61 20 64 69 72 65 63 74 6f ame of a directo
2c8f0 72 79 20 69 6e 20 77 68 69 63 68 20 74 6f 20 70 ry in which to p
2c900 75 74 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c ut temporary fil
2c910 65 73 2e 0a 2a 2a 20 49 66 20 6e 6f 20 73 75 69 es..** If no sui
2c920 74 61 62 6c 65 20 74 65 6d 70 6f 72 61 72 79 20 table temporary
2c930 66 69 6c 65 20 64 69 72 65 63 74 6f 72 79 20 63 file directory c
2c940 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20 72 65 74 an be found, ret
2c950 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 urn NULL..*/.sta
2c960 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tic const char *
2c970 75 6e 69 78 54 65 6d 70 46 69 6c 65 44 69 72 28 unixTempFileDir(
2c980 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
2c990 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a 44 69 const char *azDi
2c9a0 72 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 30 2c rs[] = {. 0,
2c9b0 0a 20 20 20 20 20 30 2c 0a 20 20 20 20 20 22 2f . 0,. "/
2c9c0 76 61 72 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22 var/tmp",. "
2c9d0 2f 75 73 72 2f 74 6d 70 22 2c 0a 20 20 20 20 20 /usr/tmp",.
2c9e0 22 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22 2e 22 "/tmp",. "."
2c9f0 0a 20 20 7d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 . };. unsigned
2ca00 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 73 74 int i = 0;. st
2ca10 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 ruct stat buf;.
2ca20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 69 const char *zDi
2ca30 72 20 3d 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 r = sqlite3_temp
2ca40 5f 64 69 72 65 63 74 6f 72 79 3b 0a 0a 20 20 69 _directory;.. i
2ca50 66 28 20 21 61 7a 44 69 72 73 5b 30 5d 20 29 20 f( !azDirs[0] )
2ca60 61 7a 44 69 72 73 5b 30 5d 20 3d 20 67 65 74 65 azDirs[0] = gete
2ca70 6e 76 28 22 53 51 4c 49 54 45 5f 54 4d 50 44 49 nv("SQLITE_TMPDI
2ca80 52 22 29 3b 0a 20 20 69 66 28 20 21 61 7a 44 69 R");. if( !azDi
2ca90 72 73 5b 31 5d 20 29 20 61 7a 44 69 72 73 5b 31 rs[1] ) azDirs[1
2caa0 5d 20 3d 20 67 65 74 65 6e 76 28 22 54 4d 50 44 ] = getenv("TMPD
2cab0 49 52 22 29 3b 0a 20 20 77 68 69 6c 65 28 31 29 IR");. while(1)
2cac0 7b 0a 20 20 20 20 69 66 28 20 7a 44 69 72 21 3d {. if( zDir!=
2cad0 30 0a 20 20 20 20 20 26 26 20 6f 73 53 74 61 74 0. && osStat
2cae0 28 7a 44 69 72 2c 20 26 62 75 66 29 3d 3d 30 0a (zDir, &buf)==0.
2caf0 20 20 20 20 20 26 26 20 53 5f 49 53 44 49 52 28 && S_ISDIR(
2cb00 62 75 66 2e 73 74 5f 6d 6f 64 65 29 0a 20 20 20 buf.st_mode).
2cb10 20 20 26 26 20 6f 73 41 63 63 65 73 73 28 7a 44 && osAccess(zD
2cb20 69 72 2c 20 30 33 29 3d 3d 30 0a 20 20 20 20 29 ir, 03)==0. )
2cb30 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 7a {. return z
2cb40 44 69 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 Dir;. }. i
2cb50 66 28 20 69 3e 3d 73 69 7a 65 6f 66 28 61 7a 44 f( i>=sizeof(azD
2cb60 69 72 73 29 2f 73 69 7a 65 6f 66 28 61 7a 44 69 irs)/sizeof(azDi
2cb70 72 73 5b 30 5d 29 20 29 20 62 72 65 61 6b 3b 0a rs[0]) ) break;.
2cb80 20 20 20 20 7a 44 69 72 20 3d 20 61 7a 44 69 72 zDir = azDir
2cb90 73 5b 69 2b 2b 5d 3b 0a 20 20 7d 0a 20 20 72 65 s[i++];. }. re
2cba0 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
2cbb0 20 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 Create a tempor
2cbc0 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e ary file name in
2cbd0 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 zBuf. zBuf mus
2cbe0 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a t be allocated.*
2cbf0 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 * by the calling
2cc00 20 70 72 6f 63 65 73 73 20 61 6e 64 20 6d 75 73 process and mus
2cc10 74 20 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 t be big enough
2cc20 74 6f 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 to hold at least
2cc30 0a 2a 2a 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 .** pVfs->mxPath
2cc40 6e 61 6d 65 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 name bytes..*/.s
2cc50 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 47 65 tatic int unixGe
2cc60 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 tTempname(int nB
2cc70 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b uf, char *zBuf){
2cc80 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
2cc90 44 69 72 3b 0a 20 20 69 6e 74 20 69 4c 69 6d 69 Dir;. int iLimi
2cca0 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 49 74 27 t = 0;.. /* It'
2ccb0 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74 s odd to simulat
2ccc0 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65 e an io-error he
2ccd0 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74 re, but really t
2cce0 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a his is just. **
2ccf0 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72 using the io-er
2cd00 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75 ror infrastructu
2cd10 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20 re to test that
2cd20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74 SQLite handles t
2cd30 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f his. ** functio
2cd40 6e 20 66 61 69 6c 69 6e 67 2e 20 0a 20 20 2a 2f n failing. . */
2cd50 0a 20 20 7a 42 75 66 5b 30 5d 20 3d 20 30 3b 0a . zBuf[0] = 0;.
2cd60 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
2cd70 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
2cd80 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 20 7a 44 69 _IOERR );.. zDi
2cd90 72 20 3d 20 75 6e 69 78 54 65 6d 70 46 69 6c 65 r = unixTempFile
2cda0 44 69 72 28 29 3b 0a 20 20 69 66 28 20 7a 44 69 Dir();. if( zDi
2cdb0 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 r==0 ) return SQ
2cdc0 4c 49 54 45 5f 49 4f 45 52 52 5f 47 45 54 54 45 LITE_IOERR_GETTE
2cdd0 4d 50 50 41 54 48 3b 0a 20 20 64 6f 7b 0a 20 20 MPPATH;. do{.
2cde0 20 20 75 36 34 20 72 3b 0a 20 20 20 20 73 71 6c u64 r;. sql
2cdf0 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 ite3_randomness(
2ce00 73 69 7a 65 6f 66 28 72 29 2c 20 26 72 29 3b 0a sizeof(r), &r);.
2ce10 20 20 20 20 61 73 73 65 72 74 28 20 6e 42 75 66 assert( nBuf
2ce20 3e 32 20 29 3b 0a 20 20 20 20 7a 42 75 66 5b 6e >2 );. zBuf[n
2ce30 42 75 66 2d 32 5d 20 3d 20 30 3b 0a 20 20 20 20 Buf-2] = 0;.
2ce40 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
2ce50 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 25 73 (nBuf, zBuf, "%s
2ce60 2f 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 /"SQLITE_TEMP_FI
2ce70 4c 45 5f 50 52 45 46 49 58 22 25 6c 6c 78 25 63 LE_PREFIX"%llx%c
2ce80 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
2ce90 20 20 20 20 20 20 20 20 7a 44 69 72 2c 20 72 2c zDir, r,
2cea0 20 30 29 3b 0a 20 20 20 20 69 66 28 20 7a 42 75 0);. if( zBu
2ceb0 66 5b 6e 42 75 66 2d 32 5d 21 3d 30 20 7c 7c 20 f[nBuf-2]!=0 ||
2cec0 28 69 4c 69 6d 69 74 2b 2b 29 3e 31 30 20 29 20 (iLimit++)>10 )
2ced0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
2cee0 52 4f 52 3b 0a 20 20 7d 77 68 69 6c 65 28 20 6f ROR;. }while( o
2cef0 73 41 63 63 65 73 73 28 7a 42 75 66 2c 30 29 3d sAccess(zBuf,0)=
2cf00 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 =0 );. return S
2cf10 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 QLITE_OK;.}..#if
2cf20 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
2cf30 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 OCKING_STYLE &&
2cf40 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f defined(__APPLE_
2cf50 5f 29 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 _)./*.** Routine
2cf60 20 74 6f 20 74 72 61 6e 73 66 6f 72 6d 20 61 20 to transform a
2cf70 75 6e 69 78 46 69 6c 65 20 69 6e 74 6f 20 61 20 unixFile into a
2cf80 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 75 6e proxy-locking un
2cf90 69 78 46 69 6c 65 2e 0a 2a 2a 20 49 6d 70 6c 65 ixFile..** Imple
2cfa0 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74 68 65 mentation in the
2cfb0 20 70 72 6f 78 79 2d 6c 6f 63 6b 20 64 69 76 69 proxy-lock divi
2cfc0 73 69 6f 6e 2c 20 62 75 74 20 75 73 65 64 20 62 sion, but used b
2cfd0 79 20 75 6e 69 78 4f 70 65 6e 28 29 0a 2a 2a 20 y unixOpen().**
2cfe0 69 66 20 53 51 4c 49 54 45 5f 50 52 45 46 45 52 if SQLITE_PREFER
2cff0 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 20 69 _PROXY_LOCKING i
2d000 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 s defined..*/.st
2d010 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 54 72 atic int proxyTr
2d020 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28 ansformUnixFile(
2d030 75 6e 69 78 46 69 6c 65 2a 2c 20 63 6f 6e 73 74 unixFile*, const
2d040 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a char*);.#endif.
2d050 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 66 6f ./*.** Search fo
2d060 72 20 61 6e 20 75 6e 75 73 65 64 20 66 69 6c 65 r an unused file
2d070 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 61 74 descriptor that
2d080 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 74 was opened on t
2d090 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 he database .**
2d0a0 66 69 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75 72 file (not a jour
2d0b0 6e 61 6c 20 6f 72 20 6d 61 73 74 65 72 2d 6a 6f nal or master-jo
2d0c0 75 72 6e 61 6c 20 66 69 6c 65 29 20 69 64 65 6e urnal file) iden
2d0d0 74 69 66 69 65 64 20 62 79 20 70 61 74 68 6e 61 tified by pathna
2d0e0 6d 65 0a 2a 2a 20 7a 50 61 74 68 20 77 69 74 68 me.** zPath with
2d0f0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 58 58 58 SQLITE_OPEN_XXX
2d100 20 66 6c 61 67 73 20 6d 61 74 63 68 69 6e 67 20 flags matching
2d110 74 68 6f 73 65 20 70 61 73 73 65 64 20 61 73 20 those passed as
2d120 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 61 72 the second.** ar
2d130 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 gument to this f
2d140 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 unction..**.** S
2d150 75 63 68 20 61 20 66 69 6c 65 20 64 65 73 63 72 uch a file descr
2d160 69 70 74 6f 72 20 6d 61 79 20 65 78 69 73 74 20 iptor may exist
2d170 69 66 20 61 20 64 61 74 61 62 61 73 65 20 63 6f if a database co
2d180 6e 6e 65 63 74 69 6f 6e 20 77 61 73 20 63 6c 6f nnection was clo
2d190 73 65 64 0a 2a 2a 20 62 75 74 20 74 68 65 20 61 sed.** but the a
2d1a0 73 73 6f 63 69 61 74 65 64 20 66 69 6c 65 20 64 ssociated file d
2d1b0 65 73 63 72 69 70 74 6f 72 20 63 6f 75 6c 64 20 escriptor could
2d1c0 6e 6f 74 20 62 65 20 63 6c 6f 73 65 64 20 62 65 not be closed be
2d1d0 63 61 75 73 65 20 73 6f 6d 65 0a 2a 2a 20 6f 74 cause some.** ot
2d1e0 68 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 70 her file descrip
2d1f0 74 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 tor open on the
2d200 73 61 6d 65 20 66 69 6c 65 20 69 73 20 68 6f 6c same file is hol
2d210 64 69 6e 67 20 61 20 66 69 6c 65 2d 6c 6f 63 6b ding a file-lock
2d220 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 63 6f ..** Refer to co
2d230 6d 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 75 6e mments in the un
2d240 69 78 43 6c 6f 73 65 28 29 20 66 75 6e 63 74 69 ixClose() functi
2d250 6f 6e 20 61 6e 64 20 74 68 65 20 6c 65 6e 67 74 on and the lengt
2d260 68 79 20 63 6f 6d 6d 65 6e 74 0a 2a 2a 20 64 65 hy comment.** de
2d270 73 63 72 69 62 69 6e 67 20 22 50 6f 73 69 78 20 scribing "Posix
2d280 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b 69 6e 67 Advisory Locking
2d290 22 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f " at the start o
2d2a0 66 20 74 68 69 73 20 66 69 6c 65 20 66 6f 72 20 f this file for
2d2b0 0a 2a 2a 20 66 75 72 74 68 65 72 20 64 65 74 61 .** further deta
2d2c0 69 6c 73 2e 20 41 6c 73 6f 2c 20 74 69 63 6b 65 ils. Also, ticke
2d2d0 74 20 23 34 30 31 38 2e 0a 2a 2a 0a 2a 2a 20 49 t #4018..**.** I
2d2e0 66 20 61 20 73 75 69 74 61 62 6c 65 20 66 69 6c f a suitable fil
2d2f0 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 e descriptor is
2d300 66 6f 75 6e 64 2c 20 74 68 65 6e 20 69 74 20 69 found, then it i
2d310 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 6e s returned. If n
2d320 6f 0a 2a 2a 20 73 75 63 68 20 66 69 6c 65 20 64 o.** such file d
2d330 65 73 63 72 69 70 74 6f 72 20 69 73 20 6c 6f 63 escriptor is loc
2d340 61 74 65 64 2c 20 2d 31 20 69 73 20 72 65 74 75 ated, -1 is retu
2d350 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rned..*/.static
2d360 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 66 69 UnixUnusedFd *fi
2d370 6e 64 52 65 75 73 61 62 6c 65 46 64 28 63 6f 6e ndReusableFd(con
2d380 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 st char *zPath,
2d390 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 55 6e int flags){. Un
2d3a0 69 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 ixUnusedFd *pUnu
2d3b0 73 65 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 44 sed = 0;.. /* D
2d3c0 6f 20 6e 6f 74 20 73 65 61 72 63 68 20 66 6f 72 o not search for
2d3d0 20 61 6e 20 75 6e 75 73 65 64 20 66 69 6c 65 20 an unused file
2d3e0 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 76 78 descriptor on vx
2d3f0 77 6f 72 6b 73 2e 20 4e 6f 74 20 62 65 63 61 75 works. Not becau
2d400 73 65 0a 20 20 2a 2a 20 76 78 77 6f 72 6b 73 20 se. ** vxworks
2d410 77 6f 75 6c 64 20 6e 6f 74 20 62 65 6e 65 66 69 would not benefi
2d420 74 20 66 72 6f 6d 20 74 68 65 20 63 68 61 6e 67 t from the chang
2d430 65 20 28 69 74 20 6d 69 67 68 74 2c 20 77 65 27 e (it might, we'
2d440 72 65 20 6e 6f 74 20 73 75 72 65 29 2c 0a 20 20 re not sure),.
2d450 2a 2a 20 62 75 74 20 62 65 63 61 75 73 65 20 6e ** but because n
2d460 6f 20 77 61 79 20 74 6f 20 74 65 73 74 20 69 74 o way to test it
2d470 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 76 is currently av
2d480 61 69 6c 61 62 6c 65 2e 20 49 74 20 69 73 20 62 ailable. It is b
2d490 65 74 74 65 72 20 0a 20 20 2a 2a 20 6e 6f 74 20 etter . ** not
2d4a0 74 6f 20 72 69 73 6b 20 62 72 65 61 6b 69 6e 67 to risk breaking
2d4b0 20 76 78 77 6f 72 6b 73 20 73 75 70 70 6f 72 74 vxworks support
2d4c0 20 66 6f 72 20 74 68 65 20 73 61 6b 65 20 6f 66 for the sake of
2d4d0 20 73 75 63 68 20 61 6e 20 6f 62 73 63 75 72 65 such an obscure
2d4e0 20 0a 20 20 2a 2a 20 66 65 61 74 75 72 65 2e 20 . ** feature.
2d4f0 20 2a 2f 0a 23 69 66 20 21 4f 53 5f 56 58 57 4f */.#if !OS_VXWO
2d500 52 4b 53 0a 20 20 73 74 72 75 63 74 20 73 74 61 RKS. struct sta
2d510 74 20 73 53 74 61 74 3b 20 20 20 20 20 20 20 20 t sStat;
2d520 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
2d530 73 75 6c 74 73 20 6f 66 20 73 74 61 74 28 29 20 sults of stat()
2d540 63 61 6c 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 41 20 call */.. /* A
2d550 73 74 61 74 28 29 20 63 61 6c 6c 20 6d 61 79 20 stat() call may
2d560 66 61 69 6c 20 66 6f 72 20 76 61 72 69 6f 75 73 fail for various
2d570 20 72 65 61 73 6f 6e 73 2e 20 49 66 20 74 68 69 reasons. If thi
2d580 73 20 68 61 70 70 65 6e 73 2c 20 69 74 20 69 73 s happens, it is
2d590 0a 20 20 2a 2a 20 61 6c 6d 6f 73 74 20 63 65 72 . ** almost cer
2d5a0 74 61 69 6e 20 74 68 61 74 20 61 6e 20 6f 70 65 tain that an ope
2d5b0 6e 28 29 20 63 61 6c 6c 20 6f 6e 20 74 68 65 20 n() call on the
2d5c0 73 61 6d 65 20 70 61 74 68 20 77 69 6c 6c 20 61 same path will a
2d5d0 6c 73 6f 20 66 61 69 6c 2e 0a 20 20 2a 2a 20 46 lso fail.. ** F
2d5e0 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 or this reason,
2d5f0 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 if an error occu
2d600 72 73 20 69 6e 20 74 68 65 20 73 74 61 74 28 29 rs in the stat()
2d610 20 63 61 6c 6c 20 68 65 72 65 2c 20 69 74 20 69 call here, it i
2d620 73 0a 20 20 2a 2a 20 69 67 6e 6f 72 65 64 20 61 s. ** ignored a
2d630 6e 64 20 2d 31 20 69 73 20 72 65 74 75 72 6e 65 nd -1 is returne
2d640 64 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69 d. The caller wi
2d650 6c 6c 20 74 72 79 20 74 6f 20 6f 70 65 6e 20 61 ll try to open a
2d660 20 6e 65 77 20 66 69 6c 65 0a 20 20 2a 2a 20 64 new file. ** d
2d670 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 74 68 65 escriptor on the
2d680 20 73 61 6d 65 20 70 61 74 68 2c 20 66 61 69 6c same path, fail
2d690 2c 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 , and return an
2d6a0 65 72 72 6f 72 20 74 6f 20 53 51 4c 69 74 65 2e error to SQLite.
2d6b0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 76 65 6e 20 . **. ** Even
2d6c0 69 66 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 if a subsequent
2d6d0 6f 70 65 6e 28 29 20 63 61 6c 6c 20 64 6f 65 73 open() call does
2d6e0 20 73 75 63 63 65 65 64 2c 20 74 68 65 20 63 6f succeed, the co
2d6f0 6e 73 65 71 75 65 6e 63 65 73 20 6f 66 0a 20 20 nsequences of.
2d700 2a 2a 20 6e 6f 74 20 73 65 61 72 63 68 69 6e 67 ** not searching
2d710 20 66 6f 72 20 61 20 72 65 75 73 61 62 6c 65 20 for a reusable
2d720 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
2d730 61 72 65 20 6e 6f 74 20 64 69 72 65 2e 20 20 2a are not dire. *
2d740 2f 0a 20 20 69 66 28 20 30 3d 3d 6f 73 53 74 61 /. if( 0==osSta
2d750 74 28 7a 50 61 74 68 2c 20 26 73 53 74 61 74 29 t(zPath, &sStat)
2d760 20 29 7b 0a 20 20 20 20 75 6e 69 78 49 6e 6f 64 ){. unixInod
2d770 65 49 6e 66 6f 20 2a 70 49 6e 6f 64 65 3b 0a 0a eInfo *pInode;..
2d780 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 unixEnterMut
2d790 65 78 28 29 3b 0a 20 20 20 20 70 49 6e 6f 64 65 ex();. pInode
2d7a0 20 3d 20 69 6e 6f 64 65 4c 69 73 74 3b 0a 20 20 = inodeList;.
2d7b0 20 20 77 68 69 6c 65 28 20 70 49 6e 6f 64 65 20 while( pInode
2d7c0 26 26 20 28 70 49 6e 6f 64 65 2d 3e 66 69 6c 65 && (pInode->file
2d7d0 49 64 2e 64 65 76 21 3d 73 53 74 61 74 2e 73 74 Id.dev!=sStat.st
2d7e0 5f 64 65 76 0a 20 20 20 20 20 20 20 20 20 20 20 _dev.
2d7f0 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 49 6e || pIn
2d800 6f 64 65 2d 3e 66 69 6c 65 49 64 2e 69 6e 6f 21 ode->fileId.ino!
2d810 3d 73 53 74 61 74 2e 73 74 5f 69 6e 6f 29 20 29 =sStat.st_ino) )
2d820 7b 0a 20 20 20 20 20 20 20 70 49 6e 6f 64 65 20 {. pInode
2d830 3d 20 70 49 6e 6f 64 65 2d 3e 70 4e 65 78 74 3b = pInode->pNext;
2d840 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
2d850 49 6e 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 55 Inode ){. U
2d860 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 2a 70 70 nixUnusedFd **pp
2d870 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 70 3d 26 ;. for(pp=&
2d880 70 49 6e 6f 64 65 2d 3e 70 55 6e 75 73 65 64 3b pInode->pUnused;
2d890 20 2a 70 70 20 26 26 20 28 2a 70 70 29 2d 3e 66 *pp && (*pp)->f
2d8a0 6c 61 67 73 21 3d 66 6c 61 67 73 3b 20 70 70 3d lags!=flags; pp=
2d8b0 26 28 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 &((*pp)->pNext))
2d8c0 3b 0a 20 20 20 20 20 20 70 55 6e 75 73 65 64 20 ;. pUnused
2d8d0 3d 20 2a 70 70 3b 0a 20 20 20 20 20 20 69 66 28 = *pp;. if(
2d8e0 20 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 pUnused ){.
2d8f0 20 20 20 20 2a 70 70 20 3d 20 70 55 6e 75 73 65 *pp = pUnuse
2d900 64 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 d->pNext;.
2d910 7d 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 }. }. unix
2d920 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
2d930 7d 0a 23 65 6e 64 69 66 20 20 20 20 2f 2a 20 69 }.#endif /* i
2d940 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f f !OS_VXWORKS */
2d950 0a 20 20 72 65 74 75 72 6e 20 70 55 6e 75 73 65 . return pUnuse
2d960 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 d;.}../*.** Find
2d970 20 74 68 65 20 6d 6f 64 65 2c 20 75 69 64 20 61 the mode, uid a
2d980 6e 64 20 67 69 64 20 6f 66 20 66 69 6c 65 20 7a nd gid of file z
2d990 46 69 6c 65 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 File. .*/.static
2d9a0 20 69 6e 74 20 67 65 74 46 69 6c 65 4d 6f 64 65 int getFileMode
2d9b0 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a (. const char *
2d9c0 7a 46 69 6c 65 2c 20 20 20 20 20 20 20 20 20 20 zFile,
2d9d0 20 20 20 20 2f 2a 20 46 69 6c 65 20 6e 61 6d 65 /* File name
2d9e0 20 2a 2f 0a 20 20 6d 6f 64 65 5f 74 20 2a 70 4d */. mode_t *pM
2d9f0 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ode,
2da00 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 65 /* OUT: Pe
2da10 72 6d 69 73 73 69 6f 6e 73 20 6f 66 20 7a 46 69 rmissions of zFi
2da20 6c 65 20 2a 2f 0a 20 20 75 69 64 5f 74 20 2a 70 le */. uid_t *p
2da30 55 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 Uid,
2da40 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 /* OUT:
2da50 75 69 64 20 6f 66 20 7a 46 69 6c 65 2e 20 2a 2f uid of zFile. */
2da60 0a 20 20 67 69 64 5f 74 20 2a 70 47 69 64 20 20 . gid_t *pGid
2da70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2da80 20 20 20 2f 2a 20 4f 55 54 3a 20 67 69 64 20 6f /* OUT: gid o
2da90 66 20 7a 46 69 6c 65 2e 20 2a 2f 0a 29 7b 0a 20 f zFile. */.){.
2daa0 20 73 74 72 75 63 74 20 73 74 61 74 20 73 53 74 struct stat sSt
2dab0 61 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 at;
2dac0 20 2f 2a 20 4f 75 74 70 75 74 20 6f 66 20 73 74 /* Output of st
2dad0 61 74 28 29 20 6f 6e 20 64 61 74 61 62 61 73 65 at() on database
2dae0 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 file */. int r
2daf0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
2db00 20 69 66 28 20 30 3d 3d 6f 73 53 74 61 74 28 7a if( 0==osStat(z
2db10 46 69 6c 65 2c 20 26 73 53 74 61 74 29 20 29 7b File, &sStat) ){
2db20 0a 20 20 20 20 2a 70 4d 6f 64 65 20 3d 20 73 53 . *pMode = sS
2db30 74 61 74 2e 73 74 5f 6d 6f 64 65 20 26 20 30 37 tat.st_mode & 07
2db40 37 37 3b 0a 20 20 20 20 2a 70 55 69 64 20 3d 20 77;. *pUid =
2db50 73 53 74 61 74 2e 73 74 5f 75 69 64 3b 0a 20 20 sStat.st_uid;.
2db60 20 20 2a 70 47 69 64 20 3d 20 73 53 74 61 74 2e *pGid = sStat.
2db70 73 74 5f 67 69 64 3b 0a 20 20 7d 65 6c 73 65 7b st_gid;. }else{
2db80 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
2db90 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 _IOERR_FSTAT;.
2dba0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
2dbb0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
2dbc0 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
2dbd0 62 79 20 75 6e 69 78 4f 70 65 6e 28 29 20 74 6f by unixOpen() to
2dbe0 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 75 determine the u
2dbf0 6e 69 78 20 70 65 72 6d 69 73 73 69 6f 6e 73 0a nix permissions.
2dc00 2a 2a 20 74 6f 20 63 72 65 61 74 65 20 6e 65 77 ** to create new
2dc10 20 66 69 6c 65 73 20 77 69 74 68 2e 20 49 66 20 files with. If
2dc20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c no error occurs,
2dc30 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 then SQLITE_OK
2dc40 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 is returned.** a
2dc50 6e 64 20 61 20 76 61 6c 75 65 20 73 75 69 74 61 nd a value suita
2dc60 62 6c 65 20 66 6f 72 20 70 61 73 73 69 6e 67 20 ble for passing
2dc70 61 73 20 74 68 65 20 74 68 69 72 64 20 61 72 67 as the third arg
2dc80 75 6d 65 6e 74 20 74 6f 20 6f 70 65 6e 28 32 29 ument to open(2)
2dc90 20 69 73 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74 is.** written t
2dca0 6f 20 2a 70 4d 6f 64 65 2e 20 49 66 20 61 6e 20 o *pMode. If an
2dcb0 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c IO error occurs,
2dcc0 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 an SQLite error
2dcd0 20 63 6f 64 65 20 69 73 20 0a 2a 2a 20 72 65 74 code is .** ret
2dce0 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 76 61 urned and the va
2dcf0 6c 75 65 20 6f 66 20 2a 70 4d 6f 64 65 20 69 73 lue of *pMode is
2dd00 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64 2e 0a 2a not modified..*
2dd10 2a 0a 2a 2a 20 49 6e 20 6d 6f 73 74 20 63 61 73 *.** In most cas
2dd20 65 73 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 es, this routine
2dd30 20 73 65 74 73 20 2a 70 4d 6f 64 65 20 74 6f 20 sets *pMode to
2dd40 30 2c 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65 0, which will be
2dd50 63 6f 6d 65 0a 2a 2a 20 61 6e 20 69 6e 64 69 63 come.** an indic
2dd60 61 74 69 6f 6e 20 74 6f 20 72 6f 62 75 73 74 5f ation to robust_
2dd70 6f 70 65 6e 28 29 20 74 6f 20 63 72 65 61 74 65 open() to create
2dd80 20 74 68 65 20 66 69 6c 65 20 75 73 69 6e 67 0a the file using.
2dd90 2a 2a 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ** SQLITE_DEFAUL
2dda0 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f T_FILE_PERMISSIO
2ddb0 4e 53 20 61 64 6a 75 73 74 65 64 20 62 79 20 74 NS adjusted by t
2ddc0 68 65 20 75 6d 61 73 6b 2e 0a 2a 2a 20 42 75 74 he umask..** But
2ddd0 20 69 66 20 74 68 65 20 66 69 6c 65 20 62 65 69 if the file bei
2dde0 6e 67 20 6f 70 65 6e 65 64 20 69 73 20 61 20 57 ng opened is a W
2ddf0 41 4c 20 6f 72 20 72 65 67 75 6c 61 72 20 6a 6f AL or regular jo
2de00 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e urnal file, then
2de10 20 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 .** this functi
2de20 6f 6e 20 71 75 65 72 69 65 73 20 74 68 65 20 66 on queries the f
2de30 69 6c 65 2d 73 79 73 74 65 6d 20 66 6f 72 20 74 ile-system for t
2de40 68 65 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 6f he permissions o
2de50 6e 20 74 68 65 20 0a 2a 2a 20 63 6f 72 72 65 73 n the .** corres
2de60 70 6f 6e 64 69 6e 67 20 64 61 74 61 62 61 73 65 ponding database
2de70 20 66 69 6c 65 20 61 6e 64 20 73 65 74 73 20 2a file and sets *
2de80 70 4d 6f 64 65 20 74 6f 20 74 68 69 73 20 76 61 pMode to this va
2de90 6c 75 65 2e 20 57 68 65 6e 65 76 65 72 20 0a 2a lue. Whenever .*
2dea0 2a 20 70 6f 73 73 69 62 6c 65 2c 20 57 41 4c 20 * possible, WAL
2deb0 61 6e 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 and journal file
2dec0 73 20 61 72 65 20 63 72 65 61 74 65 64 20 75 73 s are created us
2ded0 69 6e 67 20 74 68 65 20 73 61 6d 65 20 70 65 72 ing the same per
2dee0 6d 69 73 73 69 6f 6e 73 20 0a 2a 2a 20 61 73 20 missions .** as
2def0 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 64 the associated d
2df00 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
2df10 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 .** If the SQLIT
2df20 45 5f 45 4e 41 42 4c 45 5f 38 5f 33 5f 4e 41 4d E_ENABLE_8_3_NAM
2df30 45 53 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 ES option is ena
2df40 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 0a 2a bled, then the.*
2df50 2a 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 6e * original filen
2df60 61 6d 65 20 69 73 20 75 6e 61 76 61 69 6c 61 62 ame is unavailab
2df70 6c 65 2e 20 20 42 75 74 20 38 5f 33 5f 4e 41 4d le. But 8_3_NAM
2df80 45 53 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 ES is only used
2df90 66 6f 72 0a 2a 2a 20 46 41 54 20 66 69 6c 65 73 for.** FAT files
2dfa0 79 73 74 65 6d 73 20 61 6e 64 20 70 65 72 6d 69 ystems and permi
2dfb0 73 73 69 6f 6e 73 20 64 6f 20 6e 6f 74 20 6d 61 ssions do not ma
2dfc0 74 74 65 72 20 74 68 65 72 65 2c 20 73 6f 20 6a tter there, so j
2dfd0 75 73 74 20 75 73 65 0a 2a 2a 20 74 68 65 20 64 ust use.** the d
2dfe0 65 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69 6f efault permissio
2dff0 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ns..*/.static in
2e000 74 20 66 69 6e 64 43 72 65 61 74 65 46 69 6c 65 t findCreateFile
2e010 4d 6f 64 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 Mode(. const ch
2e020 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 ar *zPath,
2e030 20 20 20 20 20 20 20 20 2f 2a 20 50 61 74 68 20 /* Path
2e040 6f 66 20 66 69 6c 65 20 28 70 6f 73 73 69 62 6c of file (possibl
2e050 79 29 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 y) being created
2e060 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c */. int flags,
2e070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2e080 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 /* Flags p
2e090 61 73 73 65 64 20 61 73 20 34 74 68 20 61 72 67 assed as 4th arg
2e0a0 75 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e 28 29 ument to xOpen()
2e0b0 20 2a 2f 0a 20 20 6d 6f 64 65 5f 74 20 2a 70 4d */. mode_t *pM
2e0c0 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ode,
2e0d0 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 65 /* OUT: Pe
2e0e0 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20 6f 70 65 rmissions to ope
2e0f0 6e 20 66 69 6c 65 20 77 69 74 68 20 2a 2f 0a 20 n file with */.
2e100 20 75 69 64 5f 74 20 2a 70 55 69 64 2c 20 20 20 uid_t *pUid,
2e110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2e120 20 2f 2a 20 4f 55 54 3a 20 75 69 64 20 74 6f 20 /* OUT: uid to
2e130 73 65 74 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 set on the file
2e140 2a 2f 0a 20 20 67 69 64 5f 74 20 2a 70 47 69 64 */. gid_t *pGid
2e150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2e160 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 67 69 64 /* OUT: gid
2e170 20 74 6f 20 73 65 74 20 6f 6e 20 74 68 65 20 66 to set on the f
2e180 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ile */.){. int
2e190 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
2e1a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
2e1b0 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 eturn Code */.
2e1c0 2a 70 4d 6f 64 65 20 3d 20 30 3b 0a 20 20 2a 70 *pMode = 0;. *p
2e1d0 55 69 64 20 3d 20 30 3b 0a 20 20 2a 70 47 69 64 Uid = 0;. *pGid
2e1e0 20 3d 20 30 3b 0a 20 20 69 66 28 20 66 6c 61 67 = 0;. if( flag
2e1f0 73 20 26 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e s & (SQLITE_OPEN
2e200 5f 57 41 4c 7c 53 51 4c 49 54 45 5f 4f 50 45 4e _WAL|SQLITE_OPEN
2e210 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 20 29 _MAIN_JOURNAL) )
2e220 7b 0a 20 20 20 20 63 68 61 72 20 7a 44 62 5b 4d {. char zDb[M
2e230 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d 3b 20 AX_PATHNAME+1];
2e240 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 /* Database
2e250 66 69 6c 65 20 70 61 74 68 20 2a 2f 0a 20 20 20 file path */.
2e260 20 69 6e 74 20 6e 44 62 3b 20 20 20 20 20 20 20 int nDb;
2e270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2e280 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 61 6c 69 * Number of vali
2e290 64 20 62 79 74 65 73 20 69 6e 20 7a 44 62 20 2a d bytes in zDb *
2e2a0 2f 0a 0a 20 20 20 20 2f 2a 20 7a 50 61 74 68 20 /.. /* zPath
2e2b0 69 73 20 61 20 70 61 74 68 20 74 6f 20 61 20 57 is a path to a W
2e2c0 41 4c 20 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 AL or journal fi
2e2d0 6c 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e le. The followin
2e2e0 67 20 62 6c 6f 63 6b 20 64 65 72 69 76 65 73 0a g block derives.
2e2f0 20 20 20 20 2a 2a 20 74 68 65 20 70 61 74 68 20 ** the path
2e300 74 6f 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 to the associate
2e310 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 d database file
2e320 66 72 6f 6d 20 7a 50 61 74 68 2e 20 54 68 69 73 from zPath. This
2e330 20 62 6c 6f 63 6b 20 68 61 6e 64 6c 65 73 0a 20 block handles.
2e340 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 ** the follow
2e350 69 6e 67 20 6e 61 6d 69 6e 67 20 63 6f 6e 76 65 ing naming conve
2e360 6e 74 69 6f 6e 73 3a 0a 20 20 20 20 2a 2a 0a 20 ntions:. **.
2e370 20 20 20 2a 2a 20 20 20 22 3c 70 61 74 68 20 74 ** "<path t
2e380 6f 20 64 62 3e 2d 6a 6f 75 72 6e 61 6c 22 0a 20 o db>-journal".
2e390 20 20 20 2a 2a 20 20 20 22 3c 70 61 74 68 20 74 ** "<path t
2e3a0 6f 20 64 62 3e 2d 77 61 6c 22 0a 20 20 20 20 2a o db>-wal". *
2e3b0 2a 20 20 20 22 3c 70 61 74 68 20 74 6f 20 64 62 * "<path to db
2e3c0 3e 2d 6a 6f 75 72 6e 61 6c 4e 4e 22 0a 20 20 20 >-journalNN".
2e3d0 20 2a 2a 20 20 20 22 3c 70 61 74 68 20 74 6f 20 ** "<path to
2e3e0 64 62 3e 2d 77 61 6c 4e 4e 22 0a 20 20 20 20 2a db>-walNN". *
2e3f0 2a 0a 20 20 20 20 2a 2a 20 77 68 65 72 65 20 4e *. ** where N
2e400 4e 20 69 73 20 61 20 64 65 63 69 6d 61 6c 20 6e N is a decimal n
2e410 75 6d 62 65 72 2e 20 54 68 65 20 4e 4e 20 6e 61 umber. The NN na
2e420 6d 69 6e 67 20 73 63 68 65 6d 65 73 20 61 72 65 ming schemes are
2e430 20 0a 20 20 20 20 2a 2a 20 75 73 65 64 20 62 79 . ** used by
2e440 20 74 68 65 20 74 65 73 74 5f 6d 75 6c 74 69 70 the test_multip
2e450 6c 65 78 2e 63 20 6d 6f 64 75 6c 65 2e 0a 20 20 lex.c module..
2e460 20 20 2a 2f 0a 20 20 20 20 6e 44 62 20 3d 20 73 */. nDb = s
2e470 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
2e480 50 61 74 68 29 20 2d 20 31 3b 20 0a 20 20 20 20 Path) - 1; .
2e490 77 68 69 6c 65 28 20 7a 50 61 74 68 5b 6e 44 62 while( zPath[nDb
2e4a0 5d 21 3d 27 2d 27 20 29 7b 0a 23 69 66 6e 64 65 ]!='-' ){.#ifnde
2e4b0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
2e4c0 38 5f 33 5f 4e 41 4d 45 53 0a 20 20 20 20 20 20 8_3_NAMES.
2e4d0 2f 2a 20 49 6e 20 74 68 65 20 6e 6f 72 6d 61 6c /* In the normal
2e4e0 20 63 61 73 65 20 28 38 2b 33 20 66 69 6c 65 6e case (8+3 filen
2e4f0 61 6d 65 73 20 64 69 73 61 62 6c 65 64 29 20 74 ames disabled) t
2e500 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e he journal filen
2e510 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 ame. ** is
2e520 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 63 6f guaranteed to co
2e530 6e 74 61 69 6e 20 61 20 27 2d 27 20 63 68 61 72 ntain a '-' char
2e540 61 63 74 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 acter. */.
2e550 61 73 73 65 72 74 28 20 6e 44 62 3e 30 20 29 3b assert( nDb>0 );
2e560 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 . assert( s
2e570 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 7a 50 qlite3Isalnum(zP
2e580 61 74 68 5b 6e 44 62 5d 29 20 29 3b 0a 23 65 6c ath[nDb]) );.#el
2e590 73 65 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 38 se. /* If 8
2e5a0 2b 33 20 6e 61 6d 65 73 20 61 72 65 20 70 6f 73 +3 names are pos
2e5b0 73 69 62 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 sible, then the
2e5c0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 69 67 journal file mig
2e5d0 68 74 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 0a 20 ht not contain.
2e5e0 20 20 20 20 20 2a 2a 20 61 20 27 2d 27 20 63 68 ** a '-' ch
2e5f0 61 72 61 63 74 65 72 2e 20 20 53 6f 20 63 68 65 aracter. So che
2e600 63 6b 20 66 6f 72 20 74 68 61 74 20 63 61 73 65 ck for that case
2e610 20 61 6e 64 20 72 65 74 75 72 6e 20 65 61 72 6c and return earl
2e620 79 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 y. */. if(
2e630 6e 44 62 3d 3d 30 20 7c 7c 20 7a 50 61 74 68 5b nDb==0 || zPath[
2e640 6e 44 62 5d 3d 3d 27 2e 27 20 29 20 72 65 74 75 nDb]=='.' ) retu
2e650 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 rn SQLITE_OK;.#e
2e660 6e 64 69 66 0a 20 20 20 20 20 20 6e 44 62 2d 2d ndif. nDb--
2e670 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 ;. }. memc
2e680 70 79 28 7a 44 62 2c 20 7a 50 61 74 68 2c 20 6e py(zDb, zPath, n
2e690 44 62 29 3b 0a 20 20 20 20 7a 44 62 5b 6e 44 62 Db);. zDb[nDb
2e6a0 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 20 20 72 ] = '\0';.. r
2e6b0 63 20 3d 20 67 65 74 46 69 6c 65 4d 6f 64 65 28 c = getFileMode(
2e6c0 7a 44 62 2c 20 70 4d 6f 64 65 2c 20 70 55 69 64 zDb, pMode, pUid
2e6d0 2c 20 70 47 69 64 29 3b 0a 20 20 7d 65 6c 73 65 , pGid);. }else
2e6e0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c if( flags & SQL
2e6f0 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f ITE_OPEN_DELETEO
2e700 4e 43 4c 4f 53 45 20 29 7b 0a 20 20 20 20 2a 70 NCLOSE ){. *p
2e710 4d 6f 64 65 20 3d 20 30 36 30 30 3b 0a 20 20 7d Mode = 0600;. }
2e720 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 else if( flags &
2e730 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 55 52 49 SQLITE_OPEN_URI
2e740 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 ){. /* If th
2e750 69 73 20 69 73 20 61 20 6d 61 69 6e 20 64 61 74 is is a main dat
2e760 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 74 abase file and t
2e770 68 65 20 66 69 6c 65 20 77 61 73 20 6f 70 65 6e he file was open
2e780 65 64 20 75 73 69 6e 67 20 61 20 55 52 49 0a 20 ed using a URI.
2e790 20 20 20 2a 2a 20 66 69 6c 65 6e 61 6d 65 2c 20 ** filename,
2e7a0 63 68 65 63 6b 20 66 6f 72 20 74 68 65 20 22 6d check for the "m
2e7b0 6f 64 65 6f 66 22 20 70 61 72 61 6d 65 74 65 72 odeof" parameter
2e7c0 2e 20 49 66 20 70 72 65 73 65 6e 74 2c 20 69 6e . If present, in
2e7d0 74 65 72 70 72 65 74 0a 20 20 20 20 2a 2a 20 69 terpret. ** i
2e7e0 74 73 20 76 61 6c 75 65 20 61 73 20 61 20 66 69 ts value as a fi
2e7f0 6c 65 6e 61 6d 65 20 61 6e 64 20 74 72 79 20 74 lename and try t
2e800 6f 20 63 6f 70 79 20 74 68 65 20 6d 6f 64 65 2c o copy the mode,
2e810 20 75 69 64 20 61 6e 64 20 67 69 64 20 66 72 6f uid and gid fro
2e820 6d 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 66 69 m. ** that fi
2e830 6c 65 2e 20 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 le. */. cons
2e840 74 20 63 68 61 72 20 2a 7a 20 3d 20 73 71 6c 69 t char *z = sqli
2e850 74 65 33 5f 75 72 69 5f 70 61 72 61 6d 65 74 65 te3_uri_paramete
2e860 72 28 7a 50 61 74 68 2c 20 22 6d 6f 64 65 6f 66 r(zPath, "modeof
2e870 22 29 3b 0a 20 20 20 20 69 66 28 20 7a 20 29 7b ");. if( z ){
2e880 0a 20 20 20 20 20 20 72 63 20 3d 20 67 65 74 46 . rc = getF
2e890 69 6c 65 4d 6f 64 65 28 7a 2c 20 70 4d 6f 64 65 ileMode(z, pMode
2e8a0 2c 20 70 55 69 64 2c 20 70 47 69 64 29 3b 0a 20 , pUid, pGid);.
2e8b0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
2e8c0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f n rc;.}../*.** O
2e8d0 70 65 6e 20 74 68 65 20 66 69 6c 65 20 7a 50 61 pen the file zPa
2e8e0 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 50 72 65 76 69 th..** .** Previ
2e8f0 6f 75 73 6c 79 2c 20 74 68 65 20 53 51 4c 69 74 ously, the SQLit
2e900 65 20 4f 53 20 6c 61 79 65 72 20 75 73 65 64 20 e OS layer used
2e910 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 20 three functions
2e920 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 69 73 in place of this
2e930 0a 2a 2a 20 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 .** one:.**.**
2e940 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e sqlite3OsOpen
2e950 52 65 61 64 57 72 69 74 65 28 29 3b 0a 2a 2a 20 ReadWrite();.**
2e960 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 sqlite3OsOpe
2e970 6e 52 65 61 64 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 nReadOnly();.**
2e980 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 sqlite3OsOpe
2e990 6e 45 78 63 6c 75 73 69 76 65 28 29 3b 0a 2a 2a nExclusive();.**
2e9a0 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c 6c 73 20 .** These calls
2e9b0 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 correspond to th
2e9c0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 e following comb
2e9d0 69 6e 61 74 69 6f 6e 73 20 6f 66 20 66 6c 61 67 inations of flag
2e9e0 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 52 65 61 s:.**.** Rea
2e9f0 64 57 72 69 74 65 28 29 20 2d 3e 20 20 20 20 20 dWrite() ->
2ea00 28 52 45 41 44 57 52 49 54 45 20 7c 20 43 52 45 (READWRITE | CRE
2ea10 41 54 45 29 0a 2a 2a 20 20 20 20 20 52 65 61 64 ATE).** Read
2ea20 4f 6e 6c 79 28 29 20 20 2d 3e 20 20 20 20 20 28 Only() -> (
2ea30 52 45 41 44 4f 4e 4c 59 29 20 0a 2a 2a 20 20 20 READONLY) .**
2ea40 20 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 OpenExclusive(
2ea50 29 20 2d 3e 20 28 52 45 41 44 57 52 49 54 45 20 ) -> (READWRITE
2ea60 7c 20 43 52 45 41 54 45 20 7c 20 45 58 43 4c 55 | CREATE | EXCLU
2ea70 53 49 56 45 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 SIVE).**.** The
2ea80 6f 6c 64 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 old OpenExclusiv
2ea90 65 28 29 20 61 63 63 65 70 74 65 64 20 61 20 62 e() accepted a b
2eaa0 6f 6f 6c 65 61 6e 20 61 72 67 75 6d 65 6e 74 20 oolean argument
2eab0 2d 20 22 64 65 6c 46 6c 61 67 22 2e 20 49 66 0a - "delFlag". If.
2eac0 2a 2a 20 74 72 75 65 2c 20 74 68 65 20 66 69 6c ** true, the fil
2ead0 65 20 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 e was configured
2eae0 20 74 6f 20 62 65 20 61 75 74 6f 6d 61 74 69 63 to be automatic
2eaf0 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 77 68 65 ally deleted whe
2eb00 6e 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 68 61 n the.** file ha
2eb10 6e 64 6c 65 20 63 6c 6f 73 65 64 2e 20 54 6f 20 ndle closed. To
2eb20 61 63 68 69 65 76 65 20 74 68 65 20 73 61 6d 65 achieve the same
2eb30 20 65 66 66 65 63 74 20 75 73 69 6e 67 20 74 68 effect using th
2eb40 69 73 20 6e 65 77 20 0a 2a 2a 20 69 6e 74 65 72 is new .** inter
2eb50 66 61 63 65 2c 20 61 64 64 20 74 68 65 20 44 45 face, add the DE
2eb60 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 66 6c 61 67 LETEONCLOSE flag
2eb70 20 74 6f 20 74 68 6f 73 65 20 73 70 65 63 69 66 to those specif
2eb80 69 65 64 20 61 62 6f 76 65 20 66 6f 72 20 0a 2a ied above for .*
2eb90 2a 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 * OpenExclusive(
2eba0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
2ebb0 20 75 6e 69 78 4f 70 65 6e 28 0a 20 20 73 71 6c unixOpen(. sql
2ebc0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
2ebd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
2ebe0 20 56 46 53 20 66 6f 72 20 77 68 69 63 68 20 74 VFS for which t
2ebf0 68 69 73 20 69 73 20 74 68 65 20 78 4f 70 65 6e his is the xOpen
2ec00 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e method */. con
2ec10 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 st char *zPath,
2ec20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 74 /* Pat
2ec30 68 6e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f hname of file to
2ec40 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 be opened */.
2ec50 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 sqlite3_file *pF
2ec60 69 6c 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 ile, /*
2ec70 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 The file descrip
2ec80 74 6f 72 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 tor to be filled
2ec90 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 in */. int fla
2eca0 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 gs,
2ecb0 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 66 /* Input f
2ecc0 6c 61 67 73 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 lags to control
2ecd0 74 68 65 20 6f 70 65 6e 69 6e 67 20 2a 2f 0a 20 the opening */.
2ece0 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 int *pOutFlags
2ecf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
2ed00 20 4f 75 74 70 75 74 20 66 6c 61 67 73 20 72 65 Output flags re
2ed10 74 75 72 6e 65 64 20 74 6f 20 53 51 4c 69 74 65 turned to SQLite
2ed20 20 63 6f 72 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e core */.){. un
2ed30 69 78 46 69 6c 65 20 2a 70 20 3d 20 28 75 6e 69 ixFile *p = (uni
2ed40 78 46 69 6c 65 20 2a 29 70 46 69 6c 65 3b 0a 20 xFile *)pFile;.
2ed50 20 69 6e 74 20 66 64 20 3d 20 2d 31 3b 20 20 20 int fd = -1;
2ed60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ed70 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 /* File descript
2ed80 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 6f or returned by o
2ed90 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6f pen() */. int o
2eda0 70 65 6e 46 6c 61 67 73 20 3d 20 30 3b 20 20 20 penFlags = 0;
2edb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 /* Fla
2edc0 67 73 20 74 6f 20 70 61 73 73 20 74 6f 20 6f 70 gs to pass to op
2edd0 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 65 54 en() */. int eT
2ede0 79 70 65 20 3d 20 66 6c 61 67 73 26 30 78 46 46 ype = flags&0xFF
2edf0 46 46 46 46 30 30 3b 20 20 2f 2a 20 54 79 70 65 FFFF00; /* Type
2ee00 20 6f 66 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e of file to open
2ee10 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b */. int noLock
2ee20 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
2ee30 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 /* True to
2ee40 6f 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 70 72 69 omit locking pri
2ee50 6d 69 74 69 76 65 73 20 2a 2f 0a 20 20 69 6e 74 mitives */. int
2ee60 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
2ee70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
2ee80 75 6e 63 74 69 6f 6e 20 52 65 74 75 72 6e 20 43 unction Return C
2ee90 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 63 74 72 ode */. int ctr
2eea0 6c 46 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20 lFlags = 0;
2eeb0 20 20 20 20 20 20 20 20 2f 2a 20 55 4e 49 58 46 /* UNIXF
2eec0 49 4c 45 5f 2a 20 66 6c 61 67 73 20 2a 2f 0a 0a ILE_* flags */..
2eed0 20 20 69 6e 74 20 69 73 45 78 63 6c 75 73 69 76 int isExclusiv
2eee0 65 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 e = (flags & SQ
2eef0 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 LITE_OPEN_EXCLUS
2ef00 49 56 45 29 3b 0a 20 20 69 6e 74 20 69 73 44 65 IVE);. int isDe
2ef10 6c 65 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 lete = (flag
2ef20 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
2ef30 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 29 3b 0a DELETEONCLOSE);.
2ef40 20 20 69 6e 74 20 69 73 43 72 65 61 74 65 20 20 int isCreate
2ef50 20 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 = (flags & SQ
2ef60 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 LITE_OPEN_CREATE
2ef70 29 3b 0a 20 20 69 6e 74 20 69 73 52 65 61 64 6f );. int isReado
2ef80 6e 6c 79 20 20 20 3d 20 28 66 6c 61 67 73 20 26 nly = (flags &
2ef90 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
2efa0 44 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 DONLY);. int is
2efb0 52 65 61 64 57 72 69 74 65 20 20 3d 20 28 66 6c ReadWrite = (fl
2efc0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
2efd0 4e 5f 52 45 41 44 57 52 49 54 45 29 3b 0a 23 69 N_READWRITE);.#i
2efe0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
2eff0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 LOCKING_STYLE.
2f000 69 6e 74 20 69 73 41 75 74 6f 50 72 6f 78 79 20 int isAutoProxy
2f010 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 = (flags & SQLI
2f020 54 45 5f 4f 50 45 4e 5f 41 55 54 4f 50 52 4f 58 TE_OPEN_AUTOPROX
2f030 59 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 Y);.#endif.#if d
2f040 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f efined(__APPLE__
2f050 29 20 7c 7c 20 53 51 4c 49 54 45 5f 45 4e 41 42 ) || SQLITE_ENAB
2f060 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
2f070 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 66 73 . struct statfs
2f080 20 66 73 49 6e 66 6f 3b 0a 23 65 6e 64 69 66 0a fsInfo;.#endif.
2f090 0a 20 20 2f 2a 20 49 66 20 63 72 65 61 74 69 6e . /* If creatin
2f0a0 67 20 61 20 6d 61 73 74 65 72 20 6f 72 20 6d 61 g a master or ma
2f0b0 69 6e 2d 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2c in-file journal,
2f0c0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 this function w
2f0d0 69 6c 6c 20 6f 70 65 6e 0a 20 20 2a 2a 20 61 20 ill open. ** a
2f0e0 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 20 file-descriptor
2f0f0 6f 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 on the directory
2f100 20 74 6f 6f 2e 20 54 68 65 20 66 69 72 73 74 20 too. The first
2f110 74 69 6d 65 20 75 6e 69 78 53 79 6e 63 28 29 0a time unixSync().
2f120 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 74 ** is called t
2f130 68 65 20 64 69 72 65 63 74 6f 72 79 20 66 69 6c he directory fil
2f140 65 20 64 65 73 63 72 69 70 74 6f 72 20 77 69 6c e descriptor wil
2f150 6c 20 62 65 20 66 73 79 6e 63 28 29 65 64 20 61 l be fsync()ed a
2f160 6e 64 20 63 6c 6f 73 65 28 29 64 2e 0a 20 20 2a nd close()d.. *
2f170 2f 0a 20 20 69 6e 74 20 73 79 6e 63 44 69 72 20 /. int syncDir
2f180 3d 20 28 69 73 43 72 65 61 74 65 20 26 26 20 28 = (isCreate && (
2f190 0a 20 20 20 20 20 20 20 20 65 54 79 70 65 3d 3d . eType==
2f1a0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 SQLITE_OPEN_MAST
2f1b0 45 52 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20 ER_JOURNAL .
2f1c0 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 || eType==SQLIT
2f1d0 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 E_OPEN_MAIN_JOUR
2f1e0 4e 41 4c 20 0a 20 20 20 20 20 7c 7c 20 65 54 79 NAL . || eTy
2f1f0 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f pe==SQLITE_OPEN_
2f200 57 41 4c 0a 20 20 29 29 3b 0a 0a 20 20 2f 2a 20 WAL. ));.. /*
2f210 49 66 20 61 72 67 75 6d 65 6e 74 20 7a 50 61 74 If argument zPat
2f220 68 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e h is a NULL poin
2f230 74 65 72 2c 20 74 68 69 73 20 66 75 6e 63 74 69 ter, this functi
2f240 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 20 74 on is required t
2f250 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 61 20 74 65 o open. ** a te
2f260 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 55 73 mporary file. Us
2f270 65 20 74 68 69 73 20 62 75 66 66 65 72 20 74 6f e this buffer to
2f280 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c 65 20 store the file
2f290 6e 61 6d 65 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 name in.. */.
2f2a0 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 char zTmpname[MA
2f2b0 58 5f 50 41 54 48 4e 41 4d 45 2b 32 5d 3b 0a 20 X_PATHNAME+2];.
2f2c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
2f2d0 6d 65 20 3d 20 7a 50 61 74 68 3b 0a 0a 20 20 2f me = zPath;.. /
2f2e0 2a 20 43 68 65 63 6b 20 74 68 65 20 66 6f 6c 6c * Check the foll
2f2f0 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 owing statements
2f300 20 61 72 65 20 74 72 75 65 3a 20 0a 20 20 2a 2a are true: . **
2f310 0a 20 20 2a 2a 20 20 20 28 61 29 20 45 78 61 63 . ** (a) Exac
2f320 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 52 tly one of the R
2f330 45 41 44 57 52 49 54 45 20 61 6e 64 20 52 45 41 EADWRITE and REA
2f340 44 4f 4e 4c 59 20 66 6c 61 67 73 20 6d 75 73 74 DONLY flags must
2f350 20 62 65 20 73 65 74 2c 20 61 6e 64 20 0a 20 20 be set, and .
2f360 2a 2a 20 20 20 28 62 29 20 69 66 20 43 52 45 41 ** (b) if CREA
2f370 54 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 TE is set, then
2f380 52 45 41 44 57 52 49 54 45 20 6d 75 73 74 20 61 READWRITE must a
2f390 6c 73 6f 20 62 65 20 73 65 74 2c 20 61 6e 64 0a lso be set, and.
2f3a0 20 20 2a 2a 20 20 20 28 63 29 20 69 66 20 45 58 ** (c) if EX
2f3b0 43 4c 55 53 49 56 45 20 69 73 20 73 65 74 2c 20 CLUSIVE is set,
2f3c0 74 68 65 6e 20 43 52 45 41 54 45 20 6d 75 73 74 then CREATE must
2f3d0 20 61 6c 73 6f 20 62 65 20 73 65 74 2e 0a 20 20 also be set..
2f3e0 2a 2a 20 20 20 28 64 29 20 69 66 20 44 45 4c 45 ** (d) if DELE
2f3f0 54 45 4f 4e 43 4c 4f 53 45 20 69 73 20 73 65 74 TEONCLOSE is set
2f400 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d 75 , then CREATE mu
2f410 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e 0a st also be set..
2f420 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 28 69 */. assert((i
2f430 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 20 7c 7c 20 sReadonly==0 ||
2f440 69 73 52 65 61 64 57 72 69 74 65 3d 3d 30 29 20 isReadWrite==0)
2f450 26 26 20 28 69 73 52 65 61 64 57 72 69 74 65 20 && (isReadWrite
2f460 7c 7c 20 69 73 52 65 61 64 6f 6e 6c 79 29 29 3b || isReadonly));
2f470 0a 20 20 61 73 73 65 72 74 28 69 73 43 72 65 61 . assert(isCrea
2f480 74 65 3d 3d 30 20 7c 7c 20 69 73 52 65 61 64 57 te==0 || isReadW
2f490 72 69 74 65 29 3b 0a 20 20 61 73 73 65 72 74 28 rite);. assert(
2f4a0 69 73 45 78 63 6c 75 73 69 76 65 3d 3d 30 20 7c isExclusive==0 |
2f4b0 7c 20 69 73 43 72 65 61 74 65 29 3b 0a 20 20 61 | isCreate);. a
2f4c0 73 73 65 72 74 28 69 73 44 65 6c 65 74 65 3d 3d ssert(isDelete==
2f4d0 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b 0a 0 || isCreate);.
2f4e0 0a 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 44 . /* The main D
2f4f0 42 2c 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 2c B, main journal,
2f500 20 57 41 4c 20 66 69 6c 65 20 61 6e 64 20 6d 61 WAL file and ma
2f510 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61 72 65 ster journal are
2f520 20 6e 65 76 65 72 20 0a 20 20 2a 2a 20 61 75 74 never . ** aut
2f530 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 omatically delet
2f540 65 64 2e 20 4e 6f 72 20 61 72 65 20 74 68 65 79 ed. Nor are they
2f550 20 65 76 65 72 20 74 65 6d 70 6f 72 61 72 79 20 ever temporary
2f560 66 69 6c 65 73 2e 20 20 2a 2f 0a 20 20 61 73 73 files. */. ass
2f570 65 72 74 28 20 28 21 69 73 44 65 6c 65 74 65 20 ert( (!isDelete
2f580 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 && zName) || eTy
2f590 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f pe!=SQLITE_OPEN_
2f5a0 4d 41 49 4e 5f 44 42 20 29 3b 0a 20 20 61 73 73 MAIN_DB );. ass
2f5b0 65 72 74 28 20 28 21 69 73 44 65 6c 65 74 65 20 ert( (!isDelete
2f5c0 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 && zName) || eTy
2f5d0 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f pe!=SQLITE_OPEN_
2f5e0 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0a MAIN_JOURNAL );.
2f5f0 20 20 61 73 73 65 72 74 28 20 28 21 69 73 44 65 assert( (!isDe
2f600 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65 29 20 7c lete && zName) |
2f610 7c 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f | eType!=SQLITE_
2f620 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 OPEN_MASTER_JOUR
2f630 4e 41 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 NAL );. assert(
2f640 20 28 21 69 73 44 65 6c 65 74 65 20 26 26 20 7a (!isDelete && z
2f650 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 3d Name) || eType!=
2f660 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 57 41 4c 20 SQLITE_OPEN_WAL
2f670 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 );.. /* Assert
2f680 74 68 61 74 20 74 68 65 20 75 70 70 65 72 20 6c that the upper l
2f690 61 79 65 72 20 68 61 73 20 73 65 74 20 6f 6e 65 ayer has set one
2f6a0 20 6f 66 20 74 68 65 20 22 66 69 6c 65 2d 74 79 of the "file-ty
2f6b0 70 65 22 20 66 6c 61 67 73 2e 20 2a 2f 0a 20 20 pe" flags. */.
2f6c0 61 73 73 65 72 74 28 20 65 54 79 70 65 3d 3d 53 assert( eType==S
2f6d0 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
2f6e0 44 42 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 DB || eType
2f6f0 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 ==SQLITE_OPEN_TE
2f700 4d 50 5f 44 42 20 0a 20 20 20 20 20 20 20 7c 7c MP_DB . ||
2f710 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f eType==SQLITE_O
2f720 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c PEN_MAIN_JOURNAL
2f730 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 || eType==SQLIT
2f740 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 E_OPEN_TEMP_JOUR
2f750 4e 41 4c 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 NAL . || e
2f760 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 Type==SQLITE_OPE
2f770 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 20 20 7c N_SUBJOURNAL |
2f780 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f | eType==SQLITE_
2f790 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 OPEN_MASTER_JOUR
2f7a0 4e 41 4c 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 NAL . || e
2f7b0 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 Type==SQLITE_OPE
2f7c0 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 20 7c N_TRANSIENT_DB |
2f7d0 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f | eType==SQLITE_
2f7e0 4f 50 45 4e 5f 57 41 4c 0a 20 20 29 3b 0a 0a 20 OPEN_WAL. );..
2f7f0 20 2f 2a 20 44 65 74 65 63 74 20 61 20 70 69 64 /* Detect a pid
2f800 20 63 68 61 6e 67 65 20 61 6e 64 20 72 65 73 65 change and rese
2f810 74 20 74 68 65 20 50 52 4e 47 2e 20 20 54 68 65 t the PRNG. The
2f820 72 65 20 69 73 20 61 20 72 61 63 65 20 63 6f 6e re is a race con
2f830 64 69 74 69 6f 6e 0a 20 20 2a 2a 20 68 65 72 65 dition. ** here
2f840 20 73 75 63 68 20 74 68 61 74 20 74 77 6f 20 6f such that two o
2f850 72 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20 61 r more threads a
2f860 6c 6c 20 74 72 79 69 6e 67 20 74 6f 20 6f 70 65 ll trying to ope
2f870 6e 20 64 61 74 61 62 61 73 65 73 20 61 74 0a 20 n databases at.
2f880 20 2a 2a 20 74 68 65 20 73 61 6d 65 20 69 6e 73 ** the same ins
2f890 74 61 6e 74 20 6d 69 67 68 74 20 61 6c 6c 20 72 tant might all r
2f8a0 65 73 65 74 20 74 68 65 20 50 52 4e 47 2e 20 20 eset the PRNG.
2f8b0 42 75 74 20 6d 75 6c 74 69 70 6c 65 20 72 65 73 But multiple res
2f8c0 65 74 73 0a 20 20 2a 2a 20 61 72 65 20 68 61 72 ets. ** are har
2f8d0 6d 6c 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 mless.. */. if
2f8e0 28 20 72 61 6e 64 6f 6d 6e 65 73 73 50 69 64 21 ( randomnessPid!
2f8f0 3d 6f 73 47 65 74 70 69 64 28 30 29 20 29 7b 0a =osGetpid(0) ){.
2f900 20 20 20 20 72 61 6e 64 6f 6d 6e 65 73 73 50 69 randomnessPi
2f910 64 20 3d 20 6f 73 47 65 74 70 69 64 28 30 29 3b d = osGetpid(0);
2f920 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e . sqlite3_ran
2f930 64 6f 6d 6e 65 73 73 28 30 2c 30 29 3b 0a 20 20 domness(0,0);.
2f940 7d 0a 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 }.. memset(p, 0
2f950 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c , sizeof(unixFil
2f960 65 29 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 e));.. if( eTyp
2f970 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d e==SQLITE_OPEN_M
2f980 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 55 6e AIN_DB ){. Un
2f990 69 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 ixUnusedFd *pUnu
2f9a0 73 65 64 3b 0a 20 20 20 20 70 55 6e 75 73 65 64 sed;. pUnused
2f9b0 20 3d 20 66 69 6e 64 52 65 75 73 61 62 6c 65 46 = findReusableF
2f9c0 64 28 7a 4e 61 6d 65 2c 20 66 6c 61 67 73 29 3b d(zName, flags);
2f9d0 0a 20 20 20 20 69 66 28 20 70 55 6e 75 73 65 64 . if( pUnused
2f9e0 20 29 7b 0a 20 20 20 20 20 20 66 64 20 3d 20 70 ){. fd = p
2f9f0 55 6e 75 73 65 64 2d 3e 66 64 3b 0a 20 20 20 20 Unused->fd;.
2fa00 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 55 6e }else{. pUn
2fa10 75 73 65 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d used = sqlite3_m
2fa20 61 6c 6c 6f 63 36 34 28 73 69 7a 65 6f 66 28 2a alloc64(sizeof(*
2fa30 70 55 6e 75 73 65 64 29 29 3b 0a 20 20 20 20 20 pUnused));.
2fa40 20 69 66 28 20 21 70 55 6e 75 73 65 64 20 29 7b if( !pUnused ){
2fa50 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
2fa60 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 SQLITE_NOMEM_BKP
2fa70 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d T;. }. }
2fa80 0a 20 20 20 20 70 2d 3e 70 55 6e 75 73 65 64 20 . p->pUnused
2fa90 3d 20 70 55 6e 75 73 65 64 3b 0a 0a 20 20 20 20 = pUnused;..
2faa0 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 /* Database file
2fab0 6e 61 6d 65 73 20 61 72 65 20 64 6f 75 62 6c 65 names are double
2fac0 2d 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 -zero terminated
2fad0 20 69 66 20 74 68 65 79 20 61 72 65 20 6e 6f 74 if they are not
2fae0 0a 20 20 20 20 2a 2a 20 55 52 49 73 20 77 69 74 . ** URIs wit
2faf0 68 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 48 h parameters. H
2fb00 65 6e 63 65 2c 20 74 68 65 79 20 63 61 6e 20 61 ence, they can a
2fb10 6c 77 61 79 73 20 62 65 20 70 61 73 73 65 64 20 lways be passed
2fb20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 into. ** sqli
2fb30 74 65 33 5f 75 72 69 5f 70 61 72 61 6d 65 74 65 te3_uri_paramete
2fb40 72 28 29 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 r(). */. asse
2fb50 72 74 28 20 28 66 6c 61 67 73 20 26 20 53 51 4c rt( (flags & SQL
2fb60 49 54 45 5f 4f 50 45 4e 5f 55 52 49 29 20 7c 7c ITE_OPEN_URI) ||
2fb70 20 7a 4e 61 6d 65 5b 73 74 72 6c 65 6e 28 7a 4e zName[strlen(zN
2fb80 61 6d 65 29 2b 31 5d 3d 3d 30 20 29 3b 0a 0a 20 ame)+1]==0 );..
2fb90 20 7d 65 6c 73 65 20 69 66 28 20 21 7a 4e 61 6d }else if( !zNam
2fba0 65 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 7a e ){. /* If z
2fbb0 4e 61 6d 65 20 69 73 20 4e 55 4c 4c 2c 20 74 68 Name is NULL, th
2fbc0 65 20 75 70 70 65 72 20 6c 61 79 65 72 20 69 73 e upper layer is
2fbd0 20 72 65 71 75 65 73 74 69 6e 67 20 61 20 74 65 requesting a te
2fbe0 6d 70 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 mp file. */.
2fbf0 61 73 73 65 72 74 28 69 73 44 65 6c 65 74 65 20 assert(isDelete
2fc00 26 26 20 21 73 79 6e 63 44 69 72 29 3b 0a 20 20 && !syncDir);.
2fc10 20 20 72 63 20 3d 20 75 6e 69 78 47 65 74 54 65 rc = unixGetTe
2fc20 6d 70 6e 61 6d 65 28 70 56 66 73 2d 3e 6d 78 50 mpname(pVfs->mxP
2fc30 61 74 68 6e 61 6d 65 2c 20 7a 54 6d 70 6e 61 6d athname, zTmpnam
2fc40 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
2fc50 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
2fc60 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
2fc70 20 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 }. zName =
2fc80 7a 54 6d 70 6e 61 6d 65 3b 0a 0a 20 20 20 20 2f zTmpname;.. /
2fc90 2a 20 47 65 6e 65 72 61 74 65 64 20 74 65 6d 70 * Generated temp
2fca0 6f 72 61 72 79 20 66 69 6c 65 6e 61 6d 65 73 20 orary filenames
2fcb0 61 72 65 20 61 6c 77 61 79 73 20 64 6f 75 62 6c are always doubl
2fcc0 65 2d 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 e-zero terminate
2fcd0 64 0a 20 20 20 20 2a 2a 20 66 6f 72 20 75 73 65 d. ** for use
2fce0 20 62 79 20 73 71 6c 69 74 65 33 5f 75 72 69 5f by sqlite3_uri_
2fcf0 70 61 72 61 6d 65 74 65 72 28 29 2e 20 2a 2f 0a parameter(). */.
2fd00 20 20 20 20 61 73 73 65 72 74 28 20 7a 4e 61 6d assert( zNam
2fd10 65 5b 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 2b e[strlen(zName)+
2fd20 31 5d 3d 3d 30 20 29 3b 0a 20 20 7d 0a 0a 20 20 1]==0 );. }..
2fd30 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 /* Determine the
2fd40 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6c value of the fl
2fd50 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 70 61 ags parameter pa
2fd60 73 73 65 64 20 74 6f 20 50 4f 53 49 58 20 66 75 ssed to POSIX fu
2fd70 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e nction. ** open
2fd80 28 29 2e 20 54 68 65 73 65 20 6d 75 73 74 20 62 (). These must b
2fd90 65 20 63 61 6c 63 75 6c 61 74 65 64 20 65 76 65 e calculated eve
2fda0 6e 20 69 66 20 6f 70 65 6e 28 29 20 69 73 20 6e n if open() is n
2fdb0 6f 74 20 63 61 6c 6c 65 64 2c 20 61 73 0a 20 20 ot called, as.
2fdc0 2a 2a 20 74 68 65 79 20 6d 61 79 20 62 65 20 73 ** they may be s
2fdd0 74 6f 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 tored as part of
2fde0 20 74 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 the file handle
2fdf0 20 61 6e 64 20 75 73 65 64 20 62 79 20 74 68 65 and used by the
2fe00 20 0a 20 20 2a 2a 20 27 63 6f 6e 63 68 20 66 69 . ** 'conch fi
2fe10 6c 65 27 20 6c 6f 63 6b 69 6e 67 20 66 75 6e 63 le' locking func
2fe20 74 69 6f 6e 73 20 6c 61 74 65 72 20 6f 6e 2e 20 tions later on.
2fe30 20 2a 2f 0a 20 20 69 66 28 20 69 73 52 65 61 64 */. if( isRead
2fe40 6f 6e 6c 79 20 29 20 20 6f 70 65 6e 46 6c 61 67 only ) openFlag
2fe50 73 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 s |= O_RDONLY;.
2fe60 20 69 66 28 20 69 73 52 65 61 64 57 72 69 74 65 if( isReadWrite
2fe70 20 29 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 ) openFlags |=
2fe80 4f 5f 52 44 57 52 3b 0a 20 20 69 66 28 20 69 73 O_RDWR;. if( is
2fe90 43 72 65 61 74 65 20 29 20 20 20 20 6f 70 65 6e Create ) open
2fea0 46 6c 61 67 73 20 7c 3d 20 4f 5f 43 52 45 41 54 Flags |= O_CREAT
2feb0 3b 0a 20 20 69 66 28 20 69 73 45 78 63 6c 75 73 ;. if( isExclus
2fec0 69 76 65 20 29 20 6f 70 65 6e 46 6c 61 67 73 20 ive ) openFlags
2fed0 7c 3d 20 28 4f 5f 45 58 43 4c 7c 4f 5f 4e 4f 46 |= (O_EXCL|O_NOF
2fee0 4f 4c 4c 4f 57 29 3b 0a 20 20 6f 70 65 6e 46 6c OLLOW);. openFl
2fef0 61 67 73 20 7c 3d 20 28 4f 5f 4c 41 52 47 45 46 ags |= (O_LARGEF
2ff00 49 4c 45 7c 4f 5f 42 49 4e 41 52 59 29 3b 0a 0a ILE|O_BINARY);..
2ff10 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 if( fd<0 ){.
2ff20 20 20 6d 6f 64 65 5f 74 20 6f 70 65 6e 4d 6f 64 mode_t openMod
2ff30 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
2ff40 2f 2a 20 50 65 72 6d 69 73 73 69 6f 6e 73 20 74 /* Permissions t
2ff50 6f 20 63 72 65 61 74 65 20 66 69 6c 65 20 77 69 o create file wi
2ff60 74 68 20 2a 2f 0a 20 20 20 20 75 69 64 5f 74 20 th */. uid_t
2ff70 75 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 uid;
2ff80 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 72 69 /* Useri
2ff90 64 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 2a d for the file *
2ffa0 2f 0a 20 20 20 20 67 69 64 5f 74 20 67 69 64 3b /. gid_t gid;
2ffb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ffc0 20 20 20 20 2f 2a 20 47 72 6f 75 70 69 64 20 66 /* Groupid f
2ffd0 6f 72 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 or the file */.
2ffe0 20 20 20 72 63 20 3d 20 66 69 6e 64 43 72 65 61 rc = findCrea
2fff0 74 65 46 69 6c 65 4d 6f 64 65 28 7a 4e 61 6d 65 teFileMode(zName
30000 2c 20 66 6c 61 67 73 2c 20 26 6f 70 65 6e 4d 6f , flags, &openMo
30010 64 65 2c 20 26 75 69 64 2c 20 26 67 69 64 29 3b de, &uid, &gid);
30020 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
30030 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
30040 61 73 73 65 72 74 28 20 21 70 2d 3e 70 55 6e 75 assert( !p->pUnu
30050 73 65 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 sed );. ass
30060 65 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 ert( eType==SQLI
30070 54 45 5f 4f 50 45 4e 5f 57 41 4c 20 7c 7c 20 65 TE_OPEN_WAL || e
30080 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 Type==SQLITE_OPE
30090 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 29 N_MAIN_JOURNAL )
300a0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
300b0 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 64 20 c;. }. fd
300c0 3d 20 72 6f 62 75 73 74 5f 6f 70 65 6e 28 7a 4e = robust_open(zN
300d0 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20 ame, openFlags,
300e0 6f 70 65 6e 4d 6f 64 65 29 3b 0a 20 20 20 20 4f openMode);. O
300f0 53 54 52 41 43 45 28 28 22 4f 50 45 4e 58 20 20 STRACE(("OPENX
30100 20 25 2d 33 64 20 25 73 20 30 25 6f 5c 6e 22 2c %-3d %s 0%o\n",
30110 20 66 64 2c 20 7a 4e 61 6d 65 2c 20 6f 70 65 6e fd, zName, open
30120 46 6c 61 67 73 29 29 3b 0a 20 20 20 20 61 73 73 Flags));. ass
30130 65 72 74 28 20 21 69 73 45 78 63 6c 75 73 69 76 ert( !isExclusiv
30140 65 20 7c 7c 20 28 6f 70 65 6e 46 6c 61 67 73 20 e || (openFlags
30150 26 20 4f 5f 43 52 45 41 54 29 21 3d 30 20 29 3b & O_CREAT)!=0 );
30160 0a 20 20 20 20 69 66 28 20 66 64 3c 30 20 26 26 . if( fd<0 &&
30170 20 65 72 72 6e 6f 21 3d 45 49 53 44 49 52 20 26 errno!=EISDIR &
30180 26 20 69 73 52 65 61 64 57 72 69 74 65 20 29 7b & isReadWrite ){
30190 0a 20 20 20 20 20 20 2f 2a 20 46 61 69 6c 65 64 . /* Failed
301a0 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 66 69 6c to open the fil
301b0 65 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 e for read/write
301c0 20 61 63 63 65 73 73 2e 20 54 72 79 20 72 65 61 access. Try rea
301d0 64 2d 6f 6e 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 d-only. */.
301e0 20 66 6c 61 67 73 20 26 3d 20 7e 28 53 51 4c 49 flags &= ~(SQLI
301f0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
30200 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 E|SQLITE_OPEN_CR
30210 45 41 54 45 29 3b 0a 20 20 20 20 20 20 6f 70 65 EATE);. ope
30220 6e 46 6c 61 67 73 20 26 3d 20 7e 28 4f 5f 52 44 nFlags &= ~(O_RD
30230 57 52 7c 4f 5f 43 52 45 41 54 29 3b 0a 20 20 20 WR|O_CREAT);.
30240 20 20 20 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 flags |= SQLI
30250 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
30260 3b 0a 20 20 20 20 20 20 6f 70 65 6e 46 6c 61 67 ;. openFlag
30270 73 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 s |= O_RDONLY;.
30280 20 20 20 20 20 69 73 52 65 61 64 6f 6e 6c 79 20 isReadonly
30290 3d 20 31 3b 0a 20 20 20 20 20 20 66 64 20 3d 20 = 1;. fd =
302a0 72 6f 62 75 73 74 5f 6f 70 65 6e 28 7a 4e 61 6d robust_open(zNam
302b0 65 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70 e, openFlags, op
302c0 65 6e 4d 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 enMode);. }.
302d0 20 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 if( fd<0 ){.
302e0 20 20 20 20 20 72 63 20 3d 20 75 6e 69 78 4c 6f rc = unixLo
302f0 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 43 41 gError(SQLITE_CA
30300 4e 54 4f 50 45 4e 5f 42 4b 50 54 2c 20 22 6f 70 NTOPEN_BKPT, "op
30310 65 6e 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 en", zName);.
30320 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e goto open_fin
30330 69 73 68 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 ished;. }..
30340 20 20 2f 2a 20 49 66 20 74 68 69 73 20 70 72 6f /* If this pro
30350 63 65 73 73 20 69 73 20 72 75 6e 6e 69 6e 67 20 cess is running
30360 61 73 20 72 6f 6f 74 20 61 6e 64 20 69 66 20 63 as root and if c
30370 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 72 6f reating a new ro
30380 6c 6c 62 61 63 6b 0a 20 20 20 20 2a 2a 20 6a 6f llback. ** jo
30390 75 72 6e 61 6c 20 6f 72 20 57 41 4c 20 66 69 6c urnal or WAL fil
303a0 65 2c 20 73 65 74 20 74 68 65 20 6f 77 6e 65 72 e, set the owner
303b0 73 68 69 70 20 6f 66 20 74 68 65 20 6a 6f 75 72 ship of the jour
303c0 6e 61 6c 20 6f 72 20 57 41 4c 20 74 6f 20 62 65 nal or WAL to be
303d0 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 61 6d 65 . ** the same
303e0 20 61 73 20 74 68 65 20 6f 72 69 67 69 6e 61 6c as the original
303f0 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a database.. *
30400 2f 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 /. if( flags
30410 26 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 57 & (SQLITE_OPEN_W
30420 41 4c 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d AL|SQLITE_OPEN_M
30430 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 20 29 7b 0a AIN_JOURNAL) ){.
30440 20 20 20 20 20 20 72 6f 62 75 73 74 46 63 68 6f robustFcho
30450 77 6e 28 66 64 2c 20 75 69 64 2c 20 67 69 64 29 wn(fd, uid, gid)
30460 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 ;. }. }. as
30470 73 65 72 74 28 20 66 64 3e 3d 30 20 29 3b 0a 20 sert( fd>=0 );.
30480 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73 20 29 if( pOutFlags )
30490 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 {. *pOutFlags
304a0 20 3d 20 66 6c 61 67 73 3b 0a 20 20 7d 0a 0a 20 = flags;. }..
304b0 20 69 66 28 20 70 2d 3e 70 55 6e 75 73 65 64 20 if( p->pUnused
304c0 29 7b 0a 20 20 20 20 70 2d 3e 70 55 6e 75 73 65 ){. p->pUnuse
304d0 64 2d 3e 66 64 20 3d 20 66 64 3b 0a 20 20 20 20 d->fd = fd;.
304e0 70 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 6c 61 67 p->pUnused->flag
304f0 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 7d 0a 0a s = flags;. }..
30500 20 20 69 66 28 20 69 73 44 65 6c 65 74 65 20 29 if( isDelete )
30510 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 {.#if OS_VXWORKS
30520 0a 20 20 20 20 7a 50 61 74 68 20 3d 20 7a 4e 61 . zPath = zNa
30530 6d 65 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 me;.#elif define
30540 64 28 53 51 4c 49 54 45 5f 55 4e 4c 49 4e 4b 5f d(SQLITE_UNLINK_
30550 41 46 54 45 52 5f 43 4c 4f 53 45 29 0a 20 20 20 AFTER_CLOSE).
30560 20 7a 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 zPath = sqlite3
30570 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20 7a _mprintf("%s", z
30580 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 7a Name);. if( z
30590 50 61 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Path==0 ){.
305a0 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 70 2c robust_close(p,
305b0 20 66 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0a fd, __LINE__);.
305c0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
305d0 49 54 45 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a ITE_NOMEM_BKPT;.
305e0 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 }.#else.
305f0 6f 73 55 6e 6c 69 6e 6b 28 7a 4e 61 6d 65 29 3b osUnlink(zName);
30600 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23 69 66 20 .#endif. }.#if
30610 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
30620 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 65 6c CKING_STYLE. el
30630 73 65 7b 0a 20 20 20 20 70 2d 3e 6f 70 65 6e 46 se{. p->openF
30640 6c 61 67 73 20 3d 20 6f 70 65 6e 46 6c 61 67 73 lags = openFlags
30650 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0a ;. }.#endif. .
30660 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 #if defined(__AP
30670 50 4c 45 5f 5f 29 20 7c 7c 20 53 51 4c 49 54 45 PLE__) || SQLITE
30680 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
30690 53 54 59 4c 45 0a 20 20 69 66 28 20 66 73 74 61 STYLE. if( fsta
306a0 74 66 73 28 66 64 2c 20 26 66 73 49 6e 66 6f 29 tfs(fd, &fsInfo)
306b0 20 3d 3d 20 2d 31 20 29 7b 0a 20 20 20 20 73 74 == -1 ){. st
306c0 6f 72 65 4c 61 73 74 45 72 72 6e 6f 28 70 2c 20 oreLastErrno(p,
306d0 65 72 72 6e 6f 29 3b 0a 20 20 20 20 72 6f 62 75 errno);. robu
306e0 73 74 5f 63 6c 6f 73 65 28 70 2c 20 66 64 2c 20 st_close(p, fd,
306f0 5f 5f 4c 49 4e 45 5f 5f 29 3b 0a 20 20 20 20 72 __LINE__);. r
30700 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
30710 52 52 5f 41 43 43 45 53 53 3b 0a 20 20 7d 0a 20 RR_ACCESS;. }.
30720 20 69 66 20 28 30 20 3d 3d 20 73 74 72 6e 63 6d if (0 == strncm
30730 70 28 22 6d 73 64 6f 73 22 2c 20 66 73 49 6e 66 p("msdos", fsInf
30740 6f 2e 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20 o.f_fstypename,
30750 35 29 29 20 7b 0a 20 20 20 20 28 28 75 6e 69 78 5)) {. ((unix
30760 46 69 6c 65 2a 29 70 46 69 6c 65 29 2d 3e 66 73 File*)pFile)->fs
30770 46 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f Flags |= SQLITE_
30780 46 53 46 4c 41 47 53 5f 49 53 5f 4d 53 44 4f 53 FSFLAGS_IS_MSDOS
30790 3b 0a 20 20 7d 0a 20 20 69 66 20 28 30 20 3d 3d ;. }. if (0 ==
307a0 20 73 74 72 6e 63 6d 70 28 22 65 78 66 61 74 22 strncmp("exfat"
307b0 2c 20 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79 70 , fsInfo.f_fstyp
307c0 65 6e 61 6d 65 2c 20 35 29 29 20 7b 0a 20 20 20 ename, 5)) {.
307d0 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69 ((unixFile*)pFi
307e0 6c 65 29 2d 3e 66 73 46 6c 61 67 73 20 7c 3d 20 le)->fsFlags |=
307f0 53 51 4c 49 54 45 5f 46 53 46 4c 41 47 53 5f 49 SQLITE_FSFLAGS_I
30800 53 5f 4d 53 44 4f 53 3b 0a 20 20 7d 0a 23 65 6e S_MSDOS;. }.#en
30810 64 69 66 0a 0a 20 20 2f 2a 20 53 65 74 20 75 70 dif.. /* Set up
30820 20 61 70 70 72 6f 70 72 69 61 74 65 20 63 74 72 appropriate ctr
30830 6c 46 6c 61 67 73 20 2a 2f 0a 20 20 69 66 28 20 lFlags */. if(
30840 69 73 44 65 6c 65 74 65 20 29 20 20 20 20 20 20 isDelete )
30850 20 20 20 20 20 20 20 20 20 20 63 74 72 6c 46 6c ctrlFl
30860 61 67 73 20 7c 3d 20 55 4e 49 58 46 49 4c 45 5f ags |= UNIXFILE_
30870 44 45 4c 45 54 45 3b 0a 20 20 69 66 28 20 69 73 DELETE;. if( is
30880 52 65 61 64 6f 6e 6c 79 20 29 20 20 20 20 20 20 Readonly )
30890 20 20 20 20 20 20 20 20 63 74 72 6c 46 6c 61 67 ctrlFlag
308a0 73 20 7c 3d 20 55 4e 49 58 46 49 4c 45 5f 52 44 s |= UNIXFILE_RD
308b0 4f 4e 4c 59 3b 0a 20 20 6e 6f 4c 6f 63 6b 20 3d ONLY;. noLock =
308c0 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f eType!=SQLITE_O
308d0 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b 0a 20 20 69 PEN_MAIN_DB;. i
308e0 66 28 20 6e 6f 4c 6f 63 6b 20 29 20 20 20 20 20 f( noLock )
308f0 20 20 20 20 20 20 20 20 20 20 20 20 20 63 74 72 ctr
30900 6c 46 6c 61 67 73 20 7c 3d 20 55 4e 49 58 46 49 lFlags |= UNIXFI
30910 4c 45 5f 4e 4f 4c 4f 43 4b 3b 0a 20 20 69 66 28 LE_NOLOCK;. if(
30920 20 73 79 6e 63 44 69 72 20 29 20 20 20 20 20 20 syncDir )
30930 20 20 20 20 20 20 20 20 20 20 20 63 74 72 6c 46 ctrlF
30940 6c 61 67 73 20 7c 3d 20 55 4e 49 58 46 49 4c 45 lags |= UNIXFILE
30950 5f 44 49 52 53 59 4e 43 3b 0a 20 20 69 66 28 20 _DIRSYNC;. if(
30960 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
30970 50 45 4e 5f 55 52 49 20 29 20 63 74 72 6c 46 6c PEN_URI ) ctrlFl
30980 61 67 73 20 7c 3d 20 55 4e 49 58 46 49 4c 45 5f ags |= UNIXFILE_
30990 55 52 49 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 URI;..#if SQLITE
309a0 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
309b0 53 54 59 4c 45 0a 23 69 66 20 53 51 4c 49 54 45 STYLE.#if SQLITE
309c0 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f _PREFER_PROXY_LO
309d0 43 4b 49 4e 47 0a 20 20 69 73 41 75 74 6f 50 72 CKING. isAutoPr
309e0 6f 78 79 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a oxy = 1;.#endif.
309f0 20 20 69 66 28 20 69 73 41 75 74 6f 50 72 6f 78 if( isAutoProx
30a00 79 20 26 26 20 28 7a 50 61 74 68 21 3d 4e 55 4c y && (zPath!=NUL
30a10 4c 29 20 26 26 20 28 21 6e 6f 4c 6f 63 6b 29 20 L) && (!noLock)
30a20 26 26 20 70 56 66 73 2d 3e 78 4f 70 65 6e 20 29 && pVfs->xOpen )
30a30 7b 0a 20 20 20 20 63 68 61 72 20 2a 65 6e 76 66 {. char *envf
30a40 6f 72 63 65 20 3d 20 67 65 74 65 6e 76 28 22 53 orce = getenv("S
30a50 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 QLITE_FORCE_PROX
30a60 59 5f 4c 4f 43 4b 49 4e 47 22 29 3b 0a 20 20 20 Y_LOCKING");.
30a70 20 69 6e 74 20 75 73 65 50 72 6f 78 79 20 3d 20 int useProxy =
30a80 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 0;.. /* SQLIT
30a90 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f E_FORCE_PROXY_LO
30aa0 43 4b 49 4e 47 3d 3d 31 20 6d 65 61 6e 73 20 66 CKING==1 means f
30ab0 6f 72 63 65 20 61 6c 77 61 79 73 20 75 73 65 20 orce always use
30ac0 70 72 6f 78 79 2c 20 30 20 6d 65 61 6e 73 20 0a proxy, 0 means .
30ad0 20 20 20 20 2a 2a 20 6e 65 76 65 72 20 75 73 65 ** never use
30ae0 20 70 72 6f 78 79 2c 20 4e 55 4c 4c 20 6d 65 61 proxy, NULL mea
30af0 6e 73 20 75 73 65 20 70 72 6f 78 79 20 66 6f 72 ns use proxy for
30b00 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 73 non-local files
30b10 20 6f 6e 6c 79 2e 20 20 2a 2f 0a 20 20 20 20 69 only. */. i
30b20 66 28 20 65 6e 76 66 6f 72 63 65 21 3d 4e 55 4c f( envforce!=NUL
30b30 4c 20 29 7b 0a 20 20 20 20 20 20 75 73 65 50 72 L ){. usePr
30b40 6f 78 79 20 3d 20 61 74 6f 69 28 65 6e 76 66 6f oxy = atoi(envfo
30b50 72 63 65 29 3e 30 3b 0a 20 20 20 20 7d 65 6c 73 rce)>0;. }els
30b60 65 7b 0a 20 20 20 20 20 20 75 73 65 50 72 6f 78 e{. useProx
30b70 79 20 3d 20 21 28 66 73 49 6e 66 6f 2e 66 5f 66 y = !(fsInfo.f_f
30b80 6c 61 67 73 26 4d 4e 54 5f 4c 4f 43 41 4c 29 3b lags&MNT_LOCAL);
30b90 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 . }. if( u
30ba0 73 65 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 20 seProxy ){.
30bb0 20 72 63 20 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 rc = fillInUnix
30bc0 46 69 6c 65 28 70 56 66 73 2c 20 66 64 2c 20 70 File(pVfs, fd, p
30bd0 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 63 74 72 File, zPath, ctr
30be0 6c 46 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 69 lFlags);. i
30bf0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
30c00 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
30c10 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 proxyTransformU
30c20 6e 69 78 46 69 6c 65 28 28 75 6e 69 78 46 69 6c nixFile((unixFil
30c30 65 2a 29 70 46 69 6c 65 2c 20 22 3a 61 75 74 6f e*)pFile, ":auto
30c40 3a 22 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 :");. if(
30c50 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
30c60 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 {. /* U
30c70 73 65 20 75 6e 69 78 43 6c 6f 73 65 20 74 6f 20 se unixClose to
30c80 63 6c 65 61 6e 20 75 70 20 74 68 65 20 72 65 73 clean up the res
30c90 6f 75 72 63 65 73 20 61 64 64 65 64 20 69 6e 20 ources added in
30ca0 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 20 0a fillInUnixFile .
30cb0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 ** and
30cc0 20 63 6c 65 61 72 20 61 6c 6c 20 74 68 65 20 73 clear all the s
30cd0 74 72 75 63 74 75 72 65 27 73 20 72 65 66 65 72 tructure's refer
30ce0 65 6e 63 65 73 2e 20 20 53 70 65 63 69 66 69 63 ences. Specific
30cf0 61 6c 6c 79 2c 20 0a 20 20 20 20 20 20 20 20 20 ally, .
30d00 20 2a 2a 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 ** pFile->pMeth
30d10 6f 64 73 20 77 69 6c 6c 20 62 65 20 4e 55 4c 4c ods will be NULL
30d20 20 73 6f 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f so sqlite3OsClo
30d30 73 65 20 77 69 6c 6c 20 62 65 20 61 20 6e 6f 2d se will be a no-
30d40 6f 70 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2f op . */
30d50 0a 20 20 20 20 20 20 20 20 20 20 75 6e 69 78 43 . unixC
30d60 6c 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20 20 20 lose(pFile);.
30d70 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
30d80 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
30d90 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f }. goto o
30da0 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20 pen_finished;.
30db0 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }. }.#endif.
30dc0 20 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 55 . rc = fillInU
30dd0 6e 69 78 46 69 6c 65 28 70 56 66 73 2c 20 66 64 nixFile(pVfs, fd
30de0 2c 20 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 , pFile, zPath,
30df0 63 74 72 6c 46 6c 61 67 73 29 3b 0a 0a 6f 70 65 ctrlFlags);..ope
30e00 6e 5f 66 69 6e 69 73 68 65 64 3a 0a 20 20 69 66 n_finished:. if
30e10 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
30e20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
30e30 72 65 65 28 70 2d 3e 70 55 6e 75 73 65 64 29 3b ree(p->pUnused);
30e40 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
30e50 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 ;.}.../*.** Dele
30e60 74 65 20 74 68 65 20 66 69 6c 65 20 61 74 20 7a te the file at z
30e70 50 61 74 68 2e 20 49 66 20 74 68 65 20 64 69 72 Path. If the dir
30e80 53 79 6e 63 20 61 72 67 75 6d 65 6e 74 20 69 73 Sync argument is
30e90 20 74 72 75 65 2c 20 66 73 79 6e 63 28 29 0a 2a true, fsync().*
30ea0 2a 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 * the directory
30eb0 61 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20 74 after deleting t
30ec0 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 he file..*/.stat
30ed0 69 63 20 69 6e 74 20 75 6e 69 78 44 65 6c 65 74 ic int unixDelet
30ee0 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 e(. sqlite3_vfs
30ef0 20 2a 4e 6f 74 55 73 65 64 2c 20 20 20 20 20 2f *NotUsed, /
30f00 2a 20 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 * VFS containing
30f10 20 74 68 69 73 20 61 73 20 74 68 65 20 78 44 65 this as the xDe
30f20 6c 65 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 lete method */.
30f30 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 const char *zPa
30f40 74 68 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 th, /* Na
30f50 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 me of file to be
30f60 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e deleted */. in
30f70 74 20 64 69 72 53 79 6e 63 20 20 20 20 20 20 20 t dirSync
30f80 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 /* If tr
30f90 75 65 2c 20 66 73 79 6e 63 28 29 20 64 69 72 65 ue, fsync() dire
30fa0 63 74 6f 72 79 20 61 66 74 65 72 20 64 65 6c 65 ctory after dele
30fb0 74 69 6e 67 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a ting file */.){.
30fc0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
30fd0 45 5f 4f 4b 3b 0a 20 20 55 4e 55 53 45 44 5f 50 E_OK;. UNUSED_P
30fe0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
30ff0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
31000 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 rror(return SQLI
31010 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 TE_IOERR_DELETE)
31020 3b 0a 20 20 69 66 28 20 6f 73 55 6e 6c 69 6e 6b ;. if( osUnlink
31030 28 7a 50 61 74 68 29 3d 3d 28 2d 31 29 20 29 7b (zPath)==(-1) ){
31040 0a 20 20 20 20 69 66 28 20 65 72 72 6e 6f 3d 3d . if( errno==
31050 45 4e 4f 45 4e 54 0a 23 69 66 20 4f 53 5f 56 58 ENOENT.#if OS_VX
31060 57 4f 52 4b 53 0a 20 20 20 20 20 20 20 20 7c 7c WORKS. ||
31070 20 6f 73 41 63 63 65 73 73 28 7a 50 61 74 68 2c osAccess(zPath,
31080 30 29 21 3d 30 0a 23 65 6e 64 69 66 0a 20 20 20 0)!=0.#endif.
31090 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
310a0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 QLITE_IOERR_DELE
310b0 54 45 5f 4e 4f 45 4e 54 3b 0a 20 20 20 20 7d 65 TE_NOENT;. }e
310c0 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
310d0 75 6e 69 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c unixLogError(SQL
310e0 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 ITE_IOERR_DELETE
310f0 2c 20 22 75 6e 6c 69 6e 6b 22 2c 20 7a 50 61 74 , "unlink", zPat
31100 68 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 h);. }. re
31110 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 69 66 turn rc;. }.#if
31120 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 ndef SQLITE_DISA
31130 42 4c 45 5f 44 49 52 53 59 4e 43 0a 20 20 69 66 BLE_DIRSYNC. if
31140 28 20 28 64 69 72 53 79 6e 63 20 26 20 31 29 21 ( (dirSync & 1)!
31150 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 64 =0 ){. int fd
31160 3b 0a 20 20 20 20 72 63 20 3d 20 6f 73 4f 70 65 ;. rc = osOpe
31170 6e 44 69 72 65 63 74 6f 72 79 28 7a 50 61 74 68 nDirectory(zPath
31180 2c 20 26 66 64 29 3b 0a 20 20 20 20 69 66 28 20 , &fd);. if(
31190 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
311a0 0a 20 20 20 20 20 20 69 66 28 20 66 75 6c 6c 5f . if( full_
311b0 66 73 79 6e 63 28 66 64 2c 30 2c 30 29 20 29 7b fsync(fd,0,0) ){
311c0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 75 6e . rc = un
311d0 69 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 ixLogError(SQLIT
311e0 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e E_IOERR_DIR_FSYN
311f0 43 2c 20 22 66 73 79 6e 63 22 2c 20 7a 50 61 74 C, "fsync", zPat
31200 68 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 h);. }.
31210 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 30 robust_close(0
31220 2c 20 66 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b , fd, __LINE__);
31230 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
31240 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
31250 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 29 3b LITE_CANTOPEN );
31260 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
31270 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d TE_OK;. }. }
31280 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e .#endif. return
31290 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 rc;.}../*.** Te
312a0 73 74 20 74 68 65 20 65 78 69 73 74 65 6e 63 65 st the existence
312b0 20 6f 66 20 6f 72 20 61 63 63 65 73 73 20 70 65 of or access pe
312c0 72 6d 69 73 73 69 6f 6e 73 20 6f 66 20 66 69 6c rmissions of fil
312d0 65 20 7a 50 61 74 68 2e 20 54 68 65 0a 2a 2a 20 e zPath. The.**
312e0 74 65 73 74 20 70 65 72 66 6f 72 6d 65 64 20 64 test performed d
312f0 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 epends on the va
31300 6c 75 65 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a lue of flags:.**
31310 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 .** SQLITE_A
31320 43 43 45 53 53 5f 45 58 49 53 54 53 3a 20 52 65 CCESS_EXISTS: Re
31330 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 66 69 turn 1 if the fi
31340 6c 65 20 65 78 69 73 74 73 0a 2a 2a 20 20 20 20 le exists.**
31350 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 SQLITE_ACCESS_R
31360 45 41 44 57 52 49 54 45 3a 20 52 65 74 75 72 6e EADWRITE: Return
31370 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 1 if the file i
31380 73 20 72 65 61 64 20 61 6e 64 20 77 72 69 74 61 s read and writa
31390 62 6c 65 2e 0a 2a 2a 20 20 20 20 20 53 51 4c 49 ble..** SQLI
313a0 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 4f 4e TE_ACCESS_READON
313b0 4c 59 3a 20 52 65 74 75 72 6e 20 31 20 69 66 20 LY: Return 1 if
313c0 74 68 65 20 66 69 6c 65 20 69 73 20 72 65 61 64 the file is read
313d0 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 able..**.** Othe
313e0 72 77 69 73 65 20 72 65 74 75 72 6e 20 30 2e 0a rwise return 0..
313f0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
31400 69 78 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 ixAccess(. sqli
31410 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 te3_vfs *NotUsed
31420 2c 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 63 , /* The VFS c
31430 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73 20 78 ontaining this x
31440 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 2a 2f Access method */
31450 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
31460 50 61 74 68 2c 20 20 20 20 20 20 2f 2a 20 50 61 Path, /* Pa
31470 74 68 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 th of the file t
31480 6f 20 65 78 61 6d 69 6e 65 20 2a 2f 0a 20 20 69 o examine */. i
31490 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 nt flags,
314a0 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 64 /* What d
314b0 6f 20 77 65 20 77 61 6e 74 20 74 6f 20 6c 65 61 o we want to lea
314c0 72 6e 20 61 62 6f 75 74 20 74 68 65 20 7a 50 61 rn about the zPa
314d0 74 68 20 66 69 6c 65 3f 20 2a 2f 0a 20 20 69 6e th file? */. in
314e0 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20 20 20 t *pResOut
314f0 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 72 /* Write r
31500 65 73 75 6c 74 20 62 6f 6f 6c 65 61 6e 20 68 65 esult boolean he
31510 72 65 20 2a 2f 0a 29 7b 0a 20 20 55 4e 55 53 45 re */.){. UNUSE
31520 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
31530 73 65 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 sed);. Simulate
31540 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 IOError( return
31550 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 SQLITE_IOERR_ACC
31560 45 53 53 3b 20 29 3b 0a 20 20 61 73 73 65 72 74 ESS; );. assert
31570 28 20 70 52 65 73 4f 75 74 21 3d 30 20 29 3b 0a ( pResOut!=0 );.
31580 0a 20 20 2f 2a 20 54 68 65 20 73 70 65 63 20 73 . /* The spec s
31590 61 79 73 20 74 68 65 72 65 20 61 72 65 20 74 68 ays there are th
315a0 72 65 65 20 70 6f 73 73 69 62 6c 65 20 76 61 6c ree possible val
315b0 75 65 73 20 66 6f 72 20 66 6c 61 67 73 2e 20 20 ues for flags.
315c0 42 75 74 20 6f 6e 6c 79 0a 20 20 2a 2a 20 74 77 But only. ** tw
315d0 6f 20 6f 66 20 74 68 65 6d 20 61 72 65 20 61 63 o of them are ac
315e0 74 75 61 6c 6c 79 20 75 73 65 64 20 2a 2f 0a 20 tually used */.
315f0 20 61 73 73 65 72 74 28 20 66 6c 61 67 73 3d 3d assert( flags==
31600 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
31610 49 53 54 53 20 7c 7c 20 66 6c 61 67 73 3d 3d 53 ISTS || flags==S
31620 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 QLITE_ACCESS_REA
31630 44 57 52 49 54 45 20 29 3b 0a 0a 20 20 69 66 28 DWRITE );.. if(
31640 20 66 6c 61 67 73 3d 3d 53 51 4c 49 54 45 5f 41 flags==SQLITE_A
31650 43 43 45 53 53 5f 45 58 49 53 54 53 20 29 7b 0a CCESS_EXISTS ){.
31660 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74 20 struct stat
31670 62 75 66 3b 0a 20 20 20 20 2a 70 52 65 73 4f 75 buf;. *pResOu
31680 74 20 3d 20 28 30 3d 3d 6f 73 53 74 61 74 28 7a t = (0==osStat(z
31690 50 61 74 68 2c 20 26 62 75 66 29 20 26 26 20 62 Path, &buf) && b
316a0 75 66 2e 73 74 5f 73 69 7a 65 3e 30 29 3b 0a 20 uf.st_size>0);.
316b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 52 65 }else{. *pRe
316c0 73 4f 75 74 20 3d 20 6f 73 41 63 63 65 73 73 28 sOut = osAccess(
316d0 7a 50 61 74 68 2c 20 57 5f 4f 4b 7c 52 5f 4f 4b zPath, W_OK|R_OK
316e0 29 3d 3d 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 )==0;. }. retu
316f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
31700 0a 2f 2a 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 ./*.**.*/.static
31710 20 69 6e 74 20 6d 6b 46 75 6c 6c 50 61 74 68 6e int mkFullPathn
31720 61 6d 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 ame(. const cha
31730 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 20 r *zPath,
31740 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 /* Input
31750 70 61 74 68 20 2a 2f 0a 20 20 63 68 61 72 20 2a path */. char *
31760 7a 4f 75 74 2c 20 20 20 20 20 20 20 20 20 20 20 zOut,
31770 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 /* Out
31780 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 put buffer */.
31790 69 6e 74 20 6e 4f 75 74 20 20 20 20 20 20 20 20 int nOut
317a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
317b0 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a /* Allocated siz
317c0 65 20 6f 66 20 62 75 66 66 65 72 20 7a 4f 75 74 e of buffer zOut
317d0 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 50 61 */.){. int nPa
317e0 74 68 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c th = sqlite3Strl
317f0 65 6e 33 30 28 7a 50 61 74 68 29 3b 0a 20 20 69 en30(zPath);. i
31800 6e 74 20 69 4f 66 66 20 3d 20 30 3b 0a 20 20 69 nt iOff = 0;. i
31810 66 28 20 7a 50 61 74 68 5b 30 5d 21 3d 27 2f 27 f( zPath[0]!='/'
31820 20 29 7b 0a 20 20 20 20 69 66 28 20 6f 73 47 65 ){. if( osGe
31830 74 63 77 64 28 7a 4f 75 74 2c 20 6e 4f 75 74 2d tcwd(zOut, nOut-
31840 32 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 2)==0 ){. r
31850 65 74 75 72 6e 20 75 6e 69 78 4c 6f 67 45 72 72 eturn unixLogErr
31860 6f 72 28 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 or(SQLITE_CANTOP
31870 45 4e 5f 42 4b 50 54 2c 20 22 67 65 74 63 77 64 EN_BKPT, "getcwd
31880 22 2c 20 7a 50 61 74 68 29 3b 0a 20 20 20 20 7d ", zPath);. }
31890 0a 20 20 20 20 69 4f 66 66 20 3d 20 73 71 6c 69 . iOff = sqli
318a0 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4f 75 74 te3Strlen30(zOut
318b0 29 3b 0a 20 20 20 20 7a 4f 75 74 5b 69 4f 66 66 );. zOut[iOff
318c0 2b 2b 5d 20 3d 20 27 2f 27 3b 0a 20 20 7d 0a 20 ++] = '/';. }.
318d0 20 69 66 28 20 28 69 4f 66 66 2b 6e 50 61 74 68 if( (iOff+nPath
318e0 2b 31 29 3e 6e 4f 75 74 20 29 7b 0a 20 20 20 20 +1)>nOut ){.
318f0 2f 2a 20 53 51 4c 69 74 65 20 61 73 73 75 6d 65 /* SQLite assume
31900 73 20 74 68 61 74 20 78 46 75 6c 6c 50 61 74 68 s that xFullPath
31910 6e 61 6d 65 28 29 20 6e 75 6c 2d 74 65 72 6d 69 name() nul-termi
31920 6e 61 74 65 73 20 74 68 65 20 6f 75 74 70 75 74 nates the output
31930 20 62 75 66 66 65 72 0a 20 20 20 20 2a 2a 20 65 buffer. ** e
31940 76 65 6e 20 69 66 20 69 74 20 72 65 74 75 72 6e ven if it return
31950 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 2a 2f 0a s an error. */.
31960 20 20 20 20 7a 4f 75 74 5b 69 4f 66 66 5d 20 3d zOut[iOff] =
31970 20 27 5c 30 27 3b 0a 20 20 20 20 72 65 74 75 72 '\0';. retur
31980 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 n SQLITE_CANTOPE
31990 4e 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 73 71 N_BKPT;. }. sq
319a0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
319b0 4f 75 74 2d 69 4f 66 66 2c 20 26 7a 4f 75 74 5b Out-iOff, &zOut[
319c0 69 4f 66 66 5d 2c 20 22 25 73 22 2c 20 7a 50 61 iOff], "%s", zPa
319d0 74 68 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 th);. return SQ
319e0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
319f0 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76 * Turn a relativ
31a00 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 e pathname into
31a10 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e a full pathname.
31a20 20 54 68 65 20 72 65 6c 61 74 69 76 65 20 70 61 The relative pa
31a30 74 68 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 th.** is stored
31a40 61 73 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 as a nul-termina
31a50 74 65 64 20 73 74 72 69 6e 67 20 69 6e 20 74 68 ted string in th
31a60 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 e buffer pointed
31a70 20 74 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68 2e to by.** zPath.
31a80 20 0a 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 .**.** zOut poi
31a90 6e 74 73 20 74 6f 20 61 20 62 75 66 66 65 72 20 nts to a buffer
31aa0 6f 66 20 61 74 20 6c 65 61 73 74 20 73 71 6c 69 of at least sqli
31ab0 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 te3_vfs.mxPathna
31ac0 6d 65 20 62 79 74 65 73 20 0a 2a 2a 20 28 69 6e me bytes .** (in
31ad0 20 74 68 69 73 20 63 61 73 65 2c 20 4d 41 58 5f this case, MAX_
31ae0 50 41 54 48 4e 41 4d 45 20 62 79 74 65 73 29 2e PATHNAME bytes).
31af0 20 54 68 65 20 66 75 6c 6c 2d 70 61 74 68 20 69 The full-path i
31b00 73 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 s written to.**
31b10 74 68 69 73 20 62 75 66 66 65 72 20 62 65 66 6f this buffer befo
31b20 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f re returning..*/
31b30 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
31b40 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 FullPathname(.
31b50 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
31b60 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a s, /*
31b70 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 Pointer to vfs
31b80 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 object */. cons
31b90 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 t char *zPath,
31ba0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 /* Pos
31bb0 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 sibly relative i
31bc0 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 nput path */. i
31bd0 6e 74 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 20 nt nOut,
31be0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
31bf0 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 Size of output b
31c00 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a uffer in bytes *
31c10 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 20 /. char *zOut
31c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
31c30 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 /* Output buff
31c40 65 72 20 2a 2f 0a 29 7b 0a 23 69 66 20 21 64 65 er */.){.#if !de
31c50 66 69 6e 65 64 28 48 41 56 45 5f 52 45 41 44 4c fined(HAVE_READL
31c60 49 4e 4b 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 INK) || !defined
31c70 28 48 41 56 45 5f 4c 53 54 41 54 29 0a 20 20 72 (HAVE_LSTAT). r
31c80 65 74 75 72 6e 20 6d 6b 46 75 6c 6c 50 61 74 68 eturn mkFullPath
31c90 6e 61 6d 65 28 7a 50 61 74 68 2c 20 7a 4f 75 74 name(zPath, zOut
31ca0 2c 20 6e 4f 75 74 29 3b 0a 23 65 6c 73 65 0a 20 , nOut);.#else.
31cb0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
31cc0 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 _OK;. int nByte
31cd0 3b 0a 20 20 69 6e 74 20 6e 4c 69 6e 6b 20 3d 20 ;. int nLink =
31ce0 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
31cf0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 /* Number of s
31d00 79 6d 62 6f 6c 69 63 20 6c 69 6e 6b 73 20 66 6f ymbolic links fo
31d10 6c 6c 6f 77 65 64 20 73 6f 20 66 61 72 20 2a 2f llowed so far */
31d20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
31d30 49 6e 20 3d 20 7a 50 61 74 68 3b 20 20 20 20 20 In = zPath;
31d40 20 2f 2a 20 49 6e 70 75 74 20 70 61 74 68 20 66 /* Input path f
31d50 6f 72 20 65 61 63 68 20 69 74 65 72 61 74 69 6f or each iteratio
31d60 6e 20 6f 66 20 6c 6f 6f 70 20 2a 2f 0a 20 20 63 n of loop */. c
31d70 68 61 72 20 2a 7a 44 65 6c 20 3d 20 30 3b 0a 0a har *zDel = 0;..
31d80 20 20 61 73 73 65 72 74 28 20 70 56 66 73 2d 3e assert( pVfs->
31d90 6d 78 50 61 74 68 6e 61 6d 65 3d 3d 4d 41 58 5f mxPathname==MAX_
31da0 50 41 54 48 4e 41 4d 45 20 29 3b 0a 20 20 55 4e PATHNAME );. UN
31db0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 USED_PARAMETER(p
31dc0 56 66 73 29 3b 0a 0a 20 20 2f 2a 20 49 74 27 73 Vfs);.. /* It's
31dd0 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74 65 odd to simulate
31de0 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65 72 an io-error her
31df0 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74 68 e, but really th
31e00 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a 20 is is just. **
31e10 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72 72 using the io-err
31e20 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75 72 or infrastructur
31e30 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20 53 e to test that S
31e40 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74 68 QLite handles th
31e50 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e is. ** function
31e60 20 66 61 69 6c 69 6e 67 2e 20 54 68 69 73 20 66 failing. This f
31e70 75 6e 63 74 69 6f 6e 20 63 6f 75 6c 64 20 66 61 unction could fa
31e80 69 6c 20 69 66 2c 20 66 6f 72 20 65 78 61 6d 70 il if, for examp
31e90 6c 65 2c 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 le, the. ** cur
31ea0 72 65 6e 74 20 77 6f 72 6b 69 6e 67 20 64 69 72 rent working dir
31eb0 65 63 74 6f 72 79 20 68 61 73 20 62 65 65 6e 20 ectory has been
31ec0 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20 2a 2f 0a 20 unlinked.. */.
31ed0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
31ee0 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
31ef0 45 52 52 4f 52 20 29 3b 0a 0a 20 20 64 6f 20 7b ERROR );.. do {
31f00 0a 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 73 74 .. /* Call st
31f10 61 74 28 29 20 6f 6e 20 70 61 74 68 20 7a 49 6e at() on path zIn
31f20 2e 20 53 65 74 20 62 4c 69 6e 6b 20 74 6f 20 74 . Set bLink to t
31f30 72 75 65 20 69 66 20 74 68 65 20 70 61 74 68 20 rue if the path
31f40 69 73 20 61 20 73 79 6d 62 6f 6c 69 63 0a 20 20 is a symbolic.
31f50 20 20 2a 2a 20 6c 69 6e 6b 2c 20 6f 72 20 66 61 ** link, or fa
31f60 6c 73 65 20 6f 74 68 65 72 77 69 73 65 2e 20 20 lse otherwise.
31f70 2a 2f 0a 20 20 20 20 69 6e 74 20 62 4c 69 6e 6b */. int bLink
31f80 20 3d 20 30 3b 0a 20 20 20 20 73 74 72 75 63 74 = 0;. struct
31f90 20 73 74 61 74 20 62 75 66 3b 0a 20 20 20 20 69 stat buf;. i
31fa0 66 28 20 6f 73 4c 73 74 61 74 28 7a 49 6e 2c 20 f( osLstat(zIn,
31fb0 26 62 75 66 29 21 3d 30 20 29 7b 0a 20 20 20 20 &buf)!=0 ){.
31fc0 20 20 69 66 28 20 65 72 72 6e 6f 21 3d 45 4e 4f if( errno!=ENO
31fd0 45 4e 54 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ENT ){. r
31fe0 63 20 3d 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 c = unixLogError
31ff0 28 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e (SQLITE_CANTOPEN
32000 5f 42 4b 50 54 2c 20 22 6c 73 74 61 74 22 2c 20 _BKPT, "lstat",
32010 7a 49 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 zIn);. }.
32020 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 62 }else{. b
32030 4c 69 6e 6b 20 3d 20 53 5f 49 53 4c 4e 4b 28 62 Link = S_ISLNK(b
32040 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 20 20 20 uf.st_mode);.
32050 20 7d 0a 0a 20 20 20 20 69 66 28 20 62 4c 69 6e }.. if( bLin
32060 6b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a k ){. if( z
32070 44 65 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Del==0 ){.
32080 20 20 7a 44 65 6c 20 3d 20 73 71 6c 69 74 65 33 zDel = sqlite3
32090 5f 6d 61 6c 6c 6f 63 28 6e 4f 75 74 29 3b 0a 20 _malloc(nOut);.
320a0 20 20 20 20 20 20 20 69 66 28 20 7a 44 65 6c 3d if( zDel=
320b0 3d 30 20 29 20 72 63 20 3d 20 53 51 4c 49 54 45 =0 ) rc = SQLITE
320c0 5f 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 20 20 _NOMEM_BKPT;.
320d0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 2b 2b 6e }else if( ++n
320e0 4c 69 6e 6b 3e 53 51 4c 49 54 45 5f 4d 41 58 5f Link>SQLITE_MAX_
320f0 53 59 4d 4c 49 4e 4b 53 20 29 7b 0a 20 20 20 20 SYMLINKS ){.
32100 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
32110 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 3b 0a 20 CANTOPEN_BKPT;.
32120 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 }.. if
32130 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
32140 29 7b 0a 20 20 20 20 20 20 20 20 6e 42 79 74 65 ){. nByte
32150 20 3d 20 6f 73 52 65 61 64 6c 69 6e 6b 28 7a 49 = osReadlink(zI
32160 6e 2c 20 7a 44 65 6c 2c 20 6e 4f 75 74 2d 31 29 n, zDel, nOut-1)
32170 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 42 ;. if( nB
32180 79 74 65 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 yte<0 ){.
32190 20 20 20 72 63 20 3d 20 75 6e 69 78 4c 6f 67 45 rc = unixLogE
321a0 72 72 6f 72 28 53 51 4c 49 54 45 5f 43 41 4e 54 rror(SQLITE_CANT
321b0 4f 50 45 4e 5f 42 4b 50 54 2c 20 22 72 65 61 64 OPEN_BKPT, "read
321c0 6c 69 6e 6b 22 2c 20 7a 49 6e 29 3b 0a 20 20 20 link", zIn);.
321d0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
321e0 20 20 20 20 20 20 69 66 28 20 7a 44 65 6c 5b 30 if( zDel[0
321f0 5d 21 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 ]!='/' ){.
32200 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 int n;.
32210 20 20 20 20 20 20 20 20 20 66 6f 72 28 6e 20 3d for(n =
32220 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
32230 28 7a 49 6e 29 3b 20 6e 3e 30 20 26 26 20 7a 49 (zIn); n>0 && zI
32240 6e 5b 6e 2d 31 5d 21 3d 27 2f 27 3b 20 6e 2d 2d n[n-1]!='/'; n--
32250 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 );. i
32260 66 28 20 6e 42 79 74 65 2b 6e 2b 31 3e 6e 4f 75 f( nByte+n+1>nOu
32270 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 t ){.
32280 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 rc = SQLITE_C
32290 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 3b 0a 20 20 ANTOPEN_BKPT;.
322a0 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
322b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d . m
322c0 65 6d 6d 6f 76 65 28 26 7a 44 65 6c 5b 6e 5d 2c emmove(&zDel[n],
322d0 20 7a 44 65 6c 2c 20 6e 42 79 74 65 2b 31 29 3b zDel, nByte+1);
322e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d . m
322f0 65 6d 63 70 79 28 7a 44 65 6c 2c 20 7a 49 6e 2c emcpy(zDel, zIn,
32300 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 n);.
32310 20 20 20 6e 42 79 74 65 20 2b 3d 20 6e 3b 0a 20 nByte += n;.
32320 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
32330 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
32340 20 20 20 7a 44 65 6c 5b 6e 42 79 74 65 5d 20 3d zDel[nByte] =
32350 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 7d '\0';. }
32360 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
32370 7a 49 6e 20 3d 20 7a 44 65 6c 3b 0a 20 20 20 20 zIn = zDel;.
32380 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 }.. assert( r
32390 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c!=SQLITE_OK ||
323a0 7a 49 6e 21 3d 7a 4f 75 74 20 7c 7c 20 7a 49 6e zIn!=zOut || zIn
323b0 5b 30 5d 3d 3d 27 2f 27 20 29 3b 0a 20 20 20 20 [0]=='/' );.
323c0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
323d0 4b 20 26 26 20 7a 49 6e 21 3d 7a 4f 75 74 20 29 K && zIn!=zOut )
323e0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6b 46 {. rc = mkF
323f0 75 6c 6c 50 61 74 68 6e 61 6d 65 28 7a 49 6e 2c ullPathname(zIn,
32400 20 7a 4f 75 74 2c 20 6e 4f 75 74 29 3b 0a 20 20 zOut, nOut);.
32410 20 20 7d 0a 20 20 20 20 69 66 28 20 62 4c 69 6e }. if( bLin
32420 6b 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 k==0 ) break;.
32430 20 20 7a 49 6e 20 3d 20 7a 4f 75 74 3b 0a 20 20 zIn = zOut;.
32440 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 }while( rc==SQLI
32450 54 45 5f 4f 4b 20 29 3b 0a 0a 20 20 73 71 6c 69 TE_OK );.. sqli
32460 74 65 33 5f 66 72 65 65 28 7a 44 65 6c 29 3b 0a te3_free(zDel);.
32470 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e return rc;.#en
32480 64 69 66 20 20 20 2f 2a 20 48 41 56 45 5f 52 45 dif /* HAVE_RE
32490 41 44 4c 49 4e 4b 20 26 26 20 48 41 56 45 5f 4c ADLINK && HAVE_L
324a0 53 54 41 54 20 2a 2f 0a 7d 0a 0a 0a 23 69 66 6e STAT */.}...#ifn
324b0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
324c0 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f LOAD_EXTENSION./
324d0 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 *.** Interfaces
324e0 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 for opening a sh
324f0 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 ared library, fi
32500 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e nding entry poin
32510 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 ts.** within the
32520 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c shared library,
32530 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 and closing the
32540 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e shared library.
32550 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 64 6c .*/.#include <dl
32560 66 63 6e 2e 68 3e 0a 73 74 61 74 69 63 20 76 6f fcn.h>.static vo
32570 69 64 20 2a 75 6e 69 78 44 6c 4f 70 65 6e 28 73 id *unixDlOpen(s
32580 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 qlite3_vfs *NotU
32590 73 65 64 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 sed, const char
325a0 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 55 *zFilename){. U
325b0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
325c0 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 NotUsed);. retu
325d0 72 6e 20 64 6c 6f 70 65 6e 28 7a 46 69 6c 65 6e rn dlopen(zFilen
325e0 61 6d 65 2c 20 52 54 4c 44 5f 4e 4f 57 20 7c 20 ame, RTLD_NOW |
325f0 52 54 4c 44 5f 47 4c 4f 42 41 4c 29 3b 0a 7d 0a RTLD_GLOBAL);.}.
32600 0a 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 61 ./*.** SQLite ca
32610 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f lls this functio
32620 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 n immediately af
32630 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 75 6e ter a call to un
32640 69 78 44 6c 53 79 6d 28 29 20 6f 72 0a 2a 2a 20 ixDlSym() or.**
32650 75 6e 69 78 44 6c 4f 70 65 6e 28 29 20 66 61 69 unixDlOpen() fai
32660 6c 73 20 28 72 65 74 75 72 6e 73 20 61 20 6e 75 ls (returns a nu
32670 6c 6c 20 70 6f 69 6e 74 65 72 29 2e 20 49 66 20 ll pointer). If
32680 61 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 20 a more detailed
32690 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 error.** message
326a0 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 69 is available, i
326b0 74 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 t is written to
326c0 7a 42 75 66 4f 75 74 2e 20 49 66 20 6e 6f 20 65 zBufOut. If no e
326d0 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 rror message.**
326e0 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 7a 42 is available, zB
326f0 75 66 4f 75 74 20 69 73 20 6c 65 66 74 20 75 6e ufOut is left un
32700 6d 6f 64 69 66 69 65 64 20 61 6e 64 20 53 51 4c modified and SQL
32710 69 74 65 20 75 73 65 73 20 61 20 64 65 66 61 75 ite uses a defau
32720 6c 74 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 lt.** error mess
32730 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 age..*/.static v
32740 6f 69 64 20 75 6e 69 78 44 6c 45 72 72 6f 72 28 oid unixDlError(
32750 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 sqlite3_vfs *Not
32760 55 73 65 64 2c 20 69 6e 74 20 6e 42 75 66 2c 20 Used, int nBuf,
32770 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a char *zBufOut){.
32780 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 const char *zE
32790 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 rr;. UNUSED_PAR
327a0 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
327b0 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 . unixEnterMute
327c0 78 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20 64 6c x();. zErr = dl
327d0 65 72 72 6f 72 28 29 3b 0a 20 20 69 66 28 20 7a error();. if( z
327e0 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Err ){. sqlit
327f0 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 e3_snprintf(nBuf
32800 2c 20 7a 42 75 66 4f 75 74 2c 20 22 25 73 22 2c , zBufOut, "%s",
32810 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20 75 6e zErr);. }. un
32820 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a ixLeaveMutex();.
32830 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 28 2a }.static void (*
32840 75 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69 74 65 unixDlSym(sqlite
32850 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 3_vfs *NotUsed,
32860 76 6f 69 64 20 2a 70 2c 20 63 6f 6e 73 74 20 63 void *p, const c
32870 68 61 72 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29 har*zSym))(void)
32880 7b 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 47 43 43 {. /* . ** GCC
32890 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d with -pedantic-
328a0 65 72 72 6f 72 73 20 73 61 79 73 20 74 68 61 74 errors says that
328b0 20 43 39 30 20 64 6f 65 73 20 6e 6f 74 20 61 6c C90 does not al
328c0 6c 6f 77 20 61 20 76 6f 69 64 2a 20 74 6f 20 62 low a void* to b
328d0 65 0a 20 20 2a 2a 20 63 61 73 74 20 69 6e 74 6f e. ** cast into
328e0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
328f0 66 75 6e 63 74 69 6f 6e 2e 20 20 41 6e 64 20 79 function. And y
32900 65 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 64 et the library d
32910 6c 73 79 6d 28 29 20 72 6f 75 74 69 6e 65 0a 20 lsym() routine.
32920 20 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76 6f ** returns a vo
32930 69 64 2a 20 77 68 69 63 68 20 69 73 20 72 65 61 id* which is rea
32940 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74 6f lly a pointer to
32950 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 6f a function. So
32960 20 68 6f 77 20 64 6f 20 77 65 0a 20 20 2a 2a 20 how do we. **
32970 75 73 65 20 64 6c 73 79 6d 28 29 20 77 69 74 68 use dlsym() with
32980 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f 72 -pedantic-error
32990 73 3f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72 s?. **. ** Var
329a0 69 61 62 6c 65 20 78 20 62 65 6c 6f 77 20 69 73 iable x below is
329b0 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20 61 defined to be a
329c0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 pointer to a fu
329d0 6e 63 74 69 6f 6e 20 74 61 6b 69 6e 67 0a 20 20 nction taking.
329e0 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 76 6f ** parameters vo
329f0 69 64 2a 20 61 6e 64 20 63 6f 6e 73 74 20 63 68 id* and const ch
32a00 61 72 2a 20 61 6e 64 20 72 65 74 75 72 6e 69 6e ar* and returnin
32a10 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 g a pointer to a
32a20 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 function.. **
32a30 57 65 20 69 6e 69 74 69 61 6c 69 7a 65 20 78 20 We initialize x
32a40 62 79 20 61 73 73 69 67 6e 69 6e 67 20 69 74 20 by assigning it
32a50 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
32a60 20 64 6c 73 79 6d 28 29 20 66 75 6e 63 74 69 6f dlsym() functio
32a70 6e 2e 0a 20 20 2a 2a 20 28 54 68 61 74 20 61 73 n.. ** (That as
32a80 73 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 65 signment require
32a90 73 20 61 20 63 61 73 74 2e 29 20 20 54 68 65 6e s a cast.) Then
32aa0 20 77 65 20 63 61 6c 6c 20 74 68 65 20 66 75 6e we call the fun
32ab0 63 74 69 6f 6e 20 74 68 61 74 0a 20 20 2a 2a 20 ction that. **
32ac0 78 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 0a 20 x points to. .
32ad0 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 77 6f **. ** This wo
32ae0 72 6b 2d 61 72 6f 75 6e 64 20 69 73 20 75 6e 6c rk-around is unl
32af0 69 6b 65 6c 79 20 74 6f 20 77 6f 72 6b 20 63 6f ikely to work co
32b00 72 72 65 63 74 6c 79 20 6f 6e 20 61 6e 79 20 73 rrectly on any s
32b10 79 73 74 65 6d 20 77 68 65 72 65 0a 20 20 2a 2a ystem where. **
32b20 20 79 6f 75 20 72 65 61 6c 6c 79 20 63 61 6e 6e you really cann
32b30 6f 74 20 63 61 73 74 20 61 20 66 75 6e 63 74 69 ot cast a functi
32b40 6f 6e 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 on pointer into
32b50 76 6f 69 64 2a 2e 20 20 42 75 74 20 74 68 65 6e void*. But then
32b60 2c 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 6f 74 , on the. ** ot
32b70 68 65 72 20 68 61 6e 64 2c 20 64 6c 73 79 6d 28 her hand, dlsym(
32b80 29 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 ) will not work
32b90 6f 6e 20 73 75 63 68 20 61 20 73 79 73 74 65 6d on such a system
32ba0 20 65 69 74 68 65 72 2c 20 73 6f 20 77 65 20 68 either, so we h
32bb0 61 76 65 0a 20 20 2a 2a 20 6e 6f 74 20 72 65 61 ave. ** not rea
32bc0 6c 6c 79 20 6c 6f 73 74 20 61 6e 79 74 68 69 6e lly lost anythin
32bd0 67 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64 20 28 g.. */. void (
32be0 2a 28 2a 78 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 *(*x)(void*,cons
32bf0 74 20 63 68 61 72 2a 29 29 28 76 6f 69 64 29 3b t char*))(void);
32c00 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
32c10 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
32c20 78 20 3d 20 28 76 6f 69 64 28 2a 28 2a 29 28 76 x = (void(*(*)(v
32c30 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a oid*,const char*
32c40 29 29 28 76 6f 69 64 29 29 64 6c 73 79 6d 3b 0a ))(void))dlsym;.
32c50 20 20 72 65 74 75 72 6e 20 28 2a 78 29 28 70 2c return (*x)(p,
32c60 20 7a 53 79 6d 29 3b 0a 7d 0a 73 74 61 74 69 63 zSym);.}.static
32c70 20 76 6f 69 64 20 75 6e 69 78 44 6c 43 6c 6f 73 void unixDlClos
32c80 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e e(sqlite3_vfs *N
32c90 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 48 otUsed, void *pH
32ca0 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 44 andle){. UNUSED
32cb0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
32cc0 65 64 29 3b 0a 20 20 64 6c 63 6c 6f 73 65 28 70 ed);. dlclose(p
32cd0 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 Handle);.}.#else
32ce0 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d /* if SQLITE_OM
32cf0 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f IT_LOAD_EXTENSIO
32d00 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f N is defined: */
32d10 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 . #define unixD
32d20 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64 65 66 69 lOpen 0. #defi
32d30 6e 65 20 75 6e 69 78 44 6c 45 72 72 6f 72 20 30 ne unixDlError 0
32d40 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 . #define unixD
32d50 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66 69 lSym 0. #defi
32d60 6e 65 20 75 6e 69 78 44 6c 43 6c 6f 73 65 20 30 ne unixDlClose 0
32d70 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 .#endif../*.** W
32d80 72 69 74 65 20 6e 42 75 66 20 62 79 74 65 73 20 rite nBuf bytes
32d90 6f 66 20 72 61 6e 64 6f 6d 20 64 61 74 61 20 74 of random data t
32da0 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 62 o the supplied b
32db0 75 66 66 65 72 20 7a 42 75 66 2e 0a 2a 2f 0a 73 uffer zBuf..*/.s
32dc0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 52 61 tatic int unixRa
32dd0 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 ndomness(sqlite3
32de0 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 _vfs *NotUsed, i
32df0 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a nt nBuf, char *z
32e00 42 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 Buf){. UNUSED_P
32e10 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
32e20 29 3b 0a 20 20 61 73 73 65 72 74 28 28 73 69 7a );. assert((siz
32e30 65 5f 74 29 6e 42 75 66 3e 3d 28 73 69 7a 65 6f e_t)nBuf>=(sizeo
32e40 66 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65 6f 66 f(time_t)+sizeof
32e50 28 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a 20 57 (int)));.. /* W
32e60 65 20 68 61 76 65 20 74 6f 20 69 6e 69 74 69 61 e have to initia
32e70 6c 69 7a 65 20 7a 42 75 66 20 74 6f 20 70 72 65 lize zBuf to pre
32e80 76 65 6e 74 20 76 61 6c 67 72 69 6e 64 20 66 72 vent valgrind fr
32e90 6f 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20 20 2a om reporting. *
32ea0 2a 20 65 72 72 6f 72 73 2e 20 20 54 68 65 20 72 * errors. The r
32eb0 65 70 6f 72 74 73 20 69 73 73 75 65 64 20 62 79 eports issued by
32ec0 20 76 61 6c 67 72 69 6e 64 20 61 72 65 20 69 6e valgrind are in
32ed0 63 6f 72 72 65 63 74 20 2d 20 77 65 20 77 6f 75 correct - we wou
32ee0 6c 64 0a 20 20 2a 2a 20 70 72 65 66 65 72 20 74 ld. ** prefer t
32ef0 68 61 74 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 hat the randomne
32f00 73 73 20 62 65 20 69 6e 63 72 65 61 73 65 64 20 ss be increased
32f10 62 79 20 6d 61 6b 69 6e 67 20 75 73 65 20 6f 66 by making use of
32f20 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e 69 74 the. ** uninit
32f30 69 61 6c 69 7a 65 64 20 73 70 61 63 65 20 69 6e ialized space in
32f40 20 7a 42 75 66 20 2d 20 62 75 74 20 76 61 6c 67 zBuf - but valg
32f50 72 69 6e 64 20 65 72 72 6f 72 73 20 74 65 6e 64 rind errors tend
32f60 20 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a 20 73 to worry. ** s
32f70 6f 6d 65 20 75 73 65 72 73 2e 20 20 52 61 74 68 ome users. Rath
32f80 65 72 20 74 68 61 6e 20 61 72 67 75 65 2c 20 69 er than argue, i
32f90 74 20 73 65 65 6d 73 20 65 61 73 69 65 72 20 6a t seems easier j
32fa0 75 73 74 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a ust to initializ
32fb0 65 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f 6c 65 e. ** the whole
32fc0 20 61 72 72 61 79 20 61 6e 64 20 73 69 6c 65 6e array and silen
32fd0 63 65 20 76 61 6c 67 72 69 6e 64 2c 20 65 76 65 ce valgrind, eve
32fe0 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73 20 n if that means
32ff0 6c 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73 73 0a less randomness.
33000 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 61 6e 64 ** in the rand
33010 6f 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a 20 20 om seed.. **.
33020 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c ** When testing,
33030 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 7a 42 initializing zB
33040 75 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 73 20 uf[] to zero is
33050 61 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68 61 74 all we do. That
33060 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68 61 74 means. ** that
33070 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 74 we always use t
33080 68 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d 20 6e he same random n
33090 75 6d 62 65 72 20 73 65 71 75 65 6e 63 65 2e 20 umber sequence.
330a0 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 0a This makes the.
330b0 20 20 2a 2a 20 74 65 73 74 73 20 72 65 70 65 61 ** tests repea
330c0 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 6d 65 table.. */. me
330d0 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 mset(zBuf, 0, nB
330e0 75 66 29 3b 0a 20 20 72 61 6e 64 6f 6d 6e 65 73 uf);. randomnes
330f0 73 50 69 64 20 3d 20 6f 73 47 65 74 70 69 64 28 sPid = osGetpid(
33100 30 29 3b 20 20 0a 23 69 66 20 21 64 65 66 69 6e 0); .#if !defin
33110 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 ed(SQLITE_TEST)
33120 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
33130 54 45 5f 4f 4d 49 54 5f 52 41 4e 44 4f 4d 4e 45 TE_OMIT_RANDOMNE
33140 53 53 29 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 SS). {. int
33150 66 64 2c 20 67 6f 74 3b 0a 20 20 20 20 66 64 20 fd, got;. fd
33160 3d 20 72 6f 62 75 73 74 5f 6f 70 65 6e 28 22 2f = robust_open("/
33170 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f dev/urandom", O_
33180 52 44 4f 4e 4c 59 2c 20 30 29 3b 0a 20 20 20 20 RDONLY, 0);.
33190 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 if( fd<0 ){.
331a0 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 20 20 time_t t;.
331b0 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 time(&t);.
331c0 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c 20 26 memcpy(zBuf, &
331d0 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b 0a 20 t, sizeof(t));.
331e0 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 memcpy(&zBu
331f0 66 5b 73 69 7a 65 6f 66 28 74 29 5d 2c 20 26 72 f[sizeof(t)], &r
33200 61 6e 64 6f 6d 6e 65 73 73 50 69 64 2c 20 73 69 andomnessPid, si
33210 7a 65 6f 66 28 72 61 6e 64 6f 6d 6e 65 73 73 50 zeof(randomnessP
33220 69 64 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 id));. asse
33230 72 74 28 20 73 69 7a 65 6f 66 28 74 29 2b 73 69 rt( sizeof(t)+si
33240 7a 65 6f 66 28 72 61 6e 64 6f 6d 6e 65 73 73 50 zeof(randomnessP
33250 69 64 29 3c 3d 28 73 69 7a 65 5f 74 29 6e 42 75 id)<=(size_t)nBu
33260 66 20 29 3b 0a 20 20 20 20 20 20 6e 42 75 66 20 f );. nBuf
33270 3d 20 73 69 7a 65 6f 66 28 74 29 20 2b 20 73 69 = sizeof(t) + si
33280 7a 65 6f 66 28 72 61 6e 64 6f 6d 6e 65 73 73 50 zeof(randomnessP
33290 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a id);. }else{.
332a0 20 20 20 20 20 20 64 6f 7b 20 67 6f 74 20 3d 20 do{ got =
332b0 6f 73 52 65 61 64 28 66 64 2c 20 7a 42 75 66 2c osRead(fd, zBuf,
332c0 20 6e 42 75 66 29 3b 20 7d 77 68 69 6c 65 28 20 nBuf); }while(
332d0 67 6f 74 3c 30 20 26 26 20 65 72 72 6e 6f 3d 3d got<0 && errno==
332e0 45 49 4e 54 52 20 29 3b 0a 20 20 20 20 20 20 72 EINTR );. r
332f0 6f 62 75 73 74 5f 63 6c 6f 73 65 28 30 2c 20 66 obust_close(0, f
33300 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0a 20 20 d, __LINE__);.
33310 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }. }.#endif.
33320 20 72 65 74 75 72 6e 20 6e 42 75 66 3b 0a 7d 0a return nBuf;.}.
33330 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f ../*.** Sleep fo
33340 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 r a little while
33350 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d . Return the am
33360 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 ount of time sle
33370 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d pt..** The argum
33380 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 ent is the numbe
33390 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 r of microsecond
333a0 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 s we want to sle
333b0 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 ep..** The retur
333c0 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e n value is the n
333d0 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 umber of microse
333e0 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 conds of sleep a
333f0 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 ctually.** reque
33400 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e sted from the un
33410 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 derlying operati
33420 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d ng system, a num
33430 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 ber which.** mig
33440 68 74 20 62 65 20 67 72 65 61 74 65 72 20 74 68 ht be greater th
33450 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 an or equal to t
33460 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 he argument, but
33470 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 not less.** tha
33480 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a n the argument..
33490 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
334a0 69 78 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f ixSleep(sqlite3_
334b0 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e vfs *NotUsed, in
334c0 74 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 7b t microseconds){
334d0 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a .#if OS_VXWORKS.
334e0 20 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 struct timespe
334f0 63 20 73 70 3b 0a 0a 20 20 73 70 2e 74 76 5f 73 c sp;.. sp.tv_s
33500 65 63 20 3d 20 6d 69 63 72 6f 73 65 63 6f 6e 64 ec = microsecond
33510 73 20 2f 20 31 30 30 30 30 30 30 3b 0a 20 20 73 s / 1000000;. s
33520 70 2e 74 76 5f 6e 73 65 63 20 3d 20 28 6d 69 63 p.tv_nsec = (mic
33530 72 6f 73 65 63 6f 6e 64 73 20 25 20 31 30 30 30 roseconds % 1000
33540 30 30 30 29 20 2a 20 31 30 30 30 3b 0a 20 20 6e 000) * 1000;. n
33550 61 6e 6f 73 6c 65 65 70 28 26 73 70 2c 20 4e 55 anosleep(&sp, NU
33560 4c 4c 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 LL);. UNUSED_PA
33570 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
33580 3b 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f ;. return micro
33590 73 65 63 6f 6e 64 73 3b 0a 23 65 6c 69 66 20 64 seconds;.#elif d
335a0 65 66 69 6e 65 64 28 48 41 56 45 5f 55 53 4c 45 efined(HAVE_USLE
335b0 45 50 29 20 26 26 20 48 41 56 45 5f 55 53 4c 45 EP) && HAVE_USLE
335c0 45 50 0a 20 20 75 73 6c 65 65 70 28 6d 69 63 72 EP. usleep(micr
335d0 6f 73 65 63 6f 6e 64 73 29 3b 0a 20 20 55 4e 55 oseconds);. UNU
335e0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
335f0 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e tUsed);. return
33600 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23 microseconds;.#
33610 65 6c 73 65 0a 20 20 69 6e 74 20 73 65 63 6f 6e else. int secon
33620 64 73 20 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e ds = (microsecon
33630 64 73 2b 39 39 39 39 39 39 29 2f 31 30 30 30 30 ds+999999)/10000
33640 30 30 3b 0a 20 20 73 6c 65 65 70 28 73 65 63 6f 00;. sleep(seco
33650 6e 64 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 nds);. UNUSED_P
33660 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
33670 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 65 63 6f );. return seco
33680 6e 64 73 2a 31 30 30 30 30 30 30 3b 0a 23 65 6e nds*1000000;.#en
33690 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 dif.}../*.** The
336a0 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 following varia
336b0 62 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61 ble, if set to a
336c0 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c non-zero value,
336d0 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 is interpreted
336e0 61 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 as.** the number
336f0 20 6f 66 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 of seconds sinc
33700 65 20 31 39 37 30 20 61 6e 64 20 69 73 20 75 73 e 1970 and is us
33710 65 64 20 74 6f 20 73 65 74 20 74 68 65 20 72 65 ed to set the re
33720 73 75 6c 74 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 sult of.** sqlit
33730 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 e3OsCurrentTime(
33740 29 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 ) during testing
33750 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
33760 54 45 5f 54 45 53 54 0a 69 6e 74 20 73 71 6c 69 TE_TEST.int sqli
33770 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 te3_current_time
33780 20 3d 20 30 3b 20 20 2f 2a 20 46 61 6b 65 20 73 = 0; /* Fake s
33790 79 73 74 65 6d 20 74 69 6d 65 20 69 6e 20 73 65 ystem time in se
337a0 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 conds since 1970
337b0 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a . */.#endif../*.
337c0 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 72 72 ** Find the curr
337d0 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 ent time (in Uni
337e0 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 versal Coordinat
337f0 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 74 65 ed Time). Write
33800 20 69 6e 74 6f 20 2a 70 69 4e 6f 77 0a 2a 2a 20 into *piNow.**
33810 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 the current time
33820 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 4a and date as a J
33830 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 ulian Day number
33840 20 74 69 6d 65 73 20 38 36 5f 34 30 30 5f 30 30 times 86_400_00
33850 30 2e 20 20 49 6e 0a 2a 2a 20 6f 74 68 65 72 20 0. In.** other
33860 77 6f 72 64 73 2c 20 77 72 69 74 65 20 69 6e 74 words, write int
33870 6f 20 2a 70 69 4e 6f 77 20 74 68 65 20 6e 75 6d o *piNow the num
33880 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f ber of milliseco
33890 6e 64 73 20 73 69 6e 63 65 20 74 68 65 20 4a 75 nds since the Ju
338a0 6c 69 61 6e 0a 2a 2a 20 65 70 6f 63 68 20 6f 66 lian.** epoch of
338b0 20 6e 6f 6f 6e 20 69 6e 20 47 72 65 65 6e 77 69 noon in Greenwi
338c0 63 68 20 6f 6e 20 4e 6f 76 65 6d 62 65 72 20 32 ch on November 2
338d0 34 2c 20 34 37 31 34 20 42 2e 43 20 61 63 63 6f 4, 4714 B.C acco
338e0 72 64 69 6e 67 20 74 6f 20 74 68 65 0a 2a 2a 20 rding to the.**
338f0 70 72 6f 6c 65 70 74 69 63 20 47 72 65 67 6f 72 proleptic Gregor
33900 69 61 6e 20 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a ian calendar..**
33910 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 .** On success,
33920 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
33930 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 . Return SQLITE
33940 5f 45 52 52 4f 52 20 69 66 20 74 68 65 20 74 69 _ERROR if the ti
33950 6d 65 20 61 6e 64 20 64 61 74 65 20 0a 2a 2a 20 me and date .**
33960 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e cannot be found.
33970 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
33980 6e 69 78 43 75 72 72 65 6e 74 54 69 6d 65 49 6e nixCurrentTimeIn
33990 74 36 34 28 73 71 6c 69 74 65 33 5f 76 66 73 20 t64(sqlite3_vfs
339a0 2a 4e 6f 74 55 73 65 64 2c 20 73 71 6c 69 74 65 *NotUsed, sqlite
339b0 33 5f 69 6e 74 36 34 20 2a 70 69 4e 6f 77 29 7b 3_int64 *piNow){
339c0 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
339d0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 6e sqlite3_int64 un
339e0 69 78 45 70 6f 63 68 20 3d 20 32 34 34 30 35 38 ixEpoch = 244058
339f0 37 35 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 75*(sqlite3_int6
33a00 34 29 38 36 34 30 30 30 30 3b 0a 20 20 69 6e 74 4)8640000;. int
33a10 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
33a20 0a 23 69 66 20 64 65 66 69 6e 65 64 28 4e 4f 5f .#if defined(NO_
33a30 47 45 54 54 4f 44 29 0a 20 20 74 69 6d 65 5f 74 GETTOD). time_t
33a40 20 74 3b 0a 20 20 74 69 6d 65 28 26 74 29 3b 0a t;. time(&t);.
33a50 20 20 2a 70 69 4e 6f 77 20 3d 20 28 28 73 71 6c *piNow = ((sql
33a60 69 74 65 33 5f 69 6e 74 36 34 29 74 29 2a 31 30 ite3_int64)t)*10
33a70 30 30 20 2b 20 75 6e 69 78 45 70 6f 63 68 3b 0a 00 + unixEpoch;.
33a80 23 65 6c 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 #elif OS_VXWORKS
33a90 0a 20 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 . struct timesp
33aa0 65 63 20 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63 6b ec sNow;. clock
33ab0 5f 67 65 74 74 69 6d 65 28 43 4c 4f 43 4b 5f 52 _gettime(CLOCK_R
33ac0 45 41 4c 54 49 4d 45 2c 20 26 73 4e 6f 77 29 3b EALTIME, &sNow);
33ad0 0a 20 20 2a 70 69 4e 6f 77 20 3d 20 75 6e 69 78 . *piNow = unix
33ae0 45 70 6f 63 68 20 2b 20 31 30 30 30 2a 28 73 71 Epoch + 1000*(sq
33af0 6c 69 74 65 33 5f 69 6e 74 36 34 29 73 4e 6f 77 lite3_int64)sNow
33b00 2e 74 76 5f 73 65 63 20 2b 20 73 4e 6f 77 2e 74 .tv_sec + sNow.t
33b10 76 5f 6e 73 65 63 2f 31 30 30 30 30 30 30 3b 0a v_nsec/1000000;.
33b20 23 65 6c 73 65 0a 20 20 73 74 72 75 63 74 20 74 #else. struct t
33b30 69 6d 65 76 61 6c 20 73 4e 6f 77 3b 0a 20 20 28 imeval sNow;. (
33b40 76 6f 69 64 29 67 65 74 74 69 6d 65 6f 66 64 61 void)gettimeofda
33b50 79 28 26 73 4e 6f 77 2c 20 30 29 3b 20 20 2f 2a y(&sNow, 0); /*
33b60 20 43 61 6e 6e 6f 74 20 66 61 69 6c 20 67 69 76 Cannot fail giv
33b70 65 6e 20 76 61 6c 69 64 20 61 72 67 75 6d 65 6e en valid argumen
33b80 74 73 20 2a 2f 0a 20 20 2a 70 69 4e 6f 77 20 3d ts */. *piNow =
33b90 20 75 6e 69 78 45 70 6f 63 68 20 2b 20 31 30 30 unixEpoch + 100
33ba0 30 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 0*(sqlite3_int64
33bb0 29 73 4e 6f 77 2e 74 76 5f 73 65 63 20 2b 20 73 )sNow.tv_sec + s
33bc0 4e 6f 77 2e 74 76 5f 75 73 65 63 2f 31 30 30 30 Now.tv_usec/1000
33bd0 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 ;.#endif..#ifdef
33be0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 SQLITE_TEST. i
33bf0 66 28 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 f( sqlite3_curre
33c00 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20 20 2a nt_time ){. *
33c10 70 69 4e 6f 77 20 3d 20 31 30 30 30 2a 28 73 71 piNow = 1000*(sq
33c20 6c 69 74 65 33 5f 69 6e 74 36 34 29 73 71 6c 69 lite3_int64)sqli
33c30 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 te3_current_time
33c40 20 2b 20 75 6e 69 78 45 70 6f 63 68 3b 0a 20 20 + unixEpoch;.
33c50 7d 0a 23 65 6e 64 69 66 0a 20 20 55 4e 55 53 45 }.#endif. UNUSE
33c60 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
33c70 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 sed);. return r
33c80 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 c;.}..#ifndef SQ
33c90 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 LITE_OMIT_DEPREC
33ca0 41 54 45 44 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 ATED./*.** Find
33cb0 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 the current time
33cc0 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 (in Universal C
33cd0 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 oordinated Time)
33ce0 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 . Write the.**
33cf0 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 current time and
33d00 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 date as a Julia
33d10 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 n Day number int
33d20 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 o *prNow and.**
33d30 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 return 0. Retur
33d40 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d 65 20 n 1 if the time
33d50 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 and date cannot
33d60 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 be found..*/.sta
33d70 74 69 63 20 69 6e 74 20 75 6e 69 78 43 75 72 72 tic int unixCurr
33d80 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f entTime(sqlite3_
33d90 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 64 6f vfs *NotUsed, do
33da0 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 20 20 uble *prNow){.
33db0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 20 sqlite3_int64 i
33dc0 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 = 0;. int rc;.
33dd0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
33de0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 63 R(NotUsed);. rc
33df0 20 3d 20 75 6e 69 78 43 75 72 72 65 6e 74 54 69 = unixCurrentTi
33e00 6d 65 49 6e 74 36 34 28 30 2c 20 26 69 29 3b 0a meInt64(0, &i);.
33e10 20 20 2a 70 72 4e 6f 77 20 3d 20 69 2f 38 36 34 *prNow = i/864
33e20 30 30 30 30 30 2e 30 3b 0a 20 20 72 65 74 75 72 00000.0;. retur
33e30 6e 20 72 63 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 n rc;.}.#else.#
33e40 64 65 66 69 6e 65 20 75 6e 69 78 43 75 72 72 65 define unixCurre
33e50 6e 74 54 69 6d 65 20 30 0a 23 65 6e 64 69 66 0a ntTime 0.#endif.
33e60 0a 2f 2a 0a 2a 2a 20 54 68 65 20 78 47 65 74 4c ./*.** The xGetL
33e70 61 73 74 45 72 72 6f 72 28 29 20 6d 65 74 68 6f astError() metho
33e80 64 20 69 73 20 64 65 73 69 67 6e 65 64 20 74 6f d is designed to
33e90 20 72 65 74 75 72 6e 20 61 20 62 65 74 74 65 72 return a better
33ea0 0a 2a 2a 20 6c 6f 77 2d 6c 65 76 65 6c 20 65 72 .** low-level er
33eb0 72 6f 72 20 6d 65 73 73 61 67 65 20 77 68 65 6e ror message when
33ec0 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73 74 65 operating-syste
33ed0 6d 20 70 72 6f 62 6c 65 6d 73 20 63 6f 6d 65 20 m problems come
33ee0 75 70 0a 2a 2a 20 64 75 72 69 6e 67 20 53 51 4c up.** during SQL
33ef0 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 ite operation.
33f00 4f 6e 6c 79 20 74 68 65 20 69 6e 74 65 67 65 72 Only the integer
33f10 20 72 65 74 75 72 6e 20 63 6f 64 65 20 69 73 20 return code is
33f20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 75 73 65 currently.** use
33f30 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
33f40 20 75 6e 69 78 47 65 74 4c 61 73 74 45 72 72 6f unixGetLastErro
33f50 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e r(sqlite3_vfs *N
33f60 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 otUsed, int NotU
33f70 73 65 64 32 2c 20 63 68 61 72 20 2a 4e 6f 74 55 sed2, char *NotU
33f80 73 65 64 33 29 7b 0a 20 20 55 4e 55 53 45 44 5f sed3){. UNUSED_
33f90 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
33fa0 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d);. UNUSED_PAR
33fb0 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 32 29 AMETER(NotUsed2)
33fc0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
33fd0 45 54 45 52 28 4e 6f 74 55 73 65 64 33 29 3b 0a ETER(NotUsed3);.
33fe0 20 20 72 65 74 75 72 6e 20 65 72 72 6e 6f 3b 0a return errno;.
33ff0 7d 0a 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a }.../*.*********
34000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
34010 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 5f 76 End of sqlite3_v
34020 66 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a fs methods *****
34030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
34040 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
34050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
34060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
34070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
34080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
34090 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a *****/../*******
340a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
340b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
340c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
340d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
340e0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
340f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
34100 2a 2a 20 42 65 67 69 6e 20 50 72 6f 78 79 20 4c ** Begin Proxy L
34110 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ocking *********
34120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
34130 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 72 *******.**.** Pr
34140 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 oxy locking is a
34150 20 22 75 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d 6d "uber-locking-m
34160 65 74 68 6f 64 22 20 69 6e 20 74 68 69 73 20 73 ethod" in this s
34170 65 6e 73 65 3a 20 20 49 74 20 75 73 65 73 20 74 ense: It uses t
34180 68 65 0a 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 6b he.** other lock
34190 69 6e 67 20 6d 65 74 68 6f 64 73 20 6f 6e 20 73 ing methods on s
341a0 65 63 6f 6e 64 61 72 79 20 6c 6f 63 6b 20 66 69 econdary lock fi
341b0 6c 65 73 2e 20 20 50 72 6f 78 79 20 6c 6f 63 6b les. Proxy lock
341c0 69 6e 67 20 69 73 20 61 0a 2a 2a 20 6d 65 74 61 ing is a.** meta
341d0 2d 6c 61 79 65 72 20 6f 76 65 72 20 74 6f 70 20 -layer over top
341e0 6f 66 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 of the primitive
341f0 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 locking impleme
34200 6e 74 65 64 20 61 62 6f 76 65 2e 20 20 46 6f 72 nted above. For
34210 0a 2a 2a 20 74 68 69 73 20 72 65 61 73 6f 6e 2c .** this reason,
34220 20 74 68 65 20 64 69 76 69 73 69 6f 6e 20 74 68 the division th
34230 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 6f 66 at implements of
34240 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 proxy locking i
34250 73 20 64 65 66 65 72 72 65 64 0a 2a 2a 20 75 6e s deferred.** un
34260 74 69 6c 20 6c 61 74 65 20 69 6e 20 74 68 65 20 til late in the
34270 66 69 6c 65 20 28 68 65 72 65 29 20 61 66 74 65 file (here) afte
34280 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74 68 r all of the oth
34290 65 72 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 68 er I/O methods h
342a0 61 76 65 0a 2a 2a 20 62 65 65 6e 20 64 65 66 69 ave.** been defi
342b0 6e 65 64 20 2d 20 73 6f 20 74 68 61 74 20 74 68 ned - so that th
342c0 65 20 70 72 69 6d 69 74 69 76 65 20 6c 6f 63 6b e primitive lock
342d0 69 6e 67 20 6d 65 74 68 6f 64 73 20 61 72 65 20 ing methods are
342e0 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 available.** as
342f0 73 65 72 76 69 63 65 73 20 74 6f 20 68 65 6c 70 services to help
34300 20 77 69 74 68 20 74 68 65 20 69 6d 70 6c 65 6d with the implem
34310 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70 72 6f 78 entation of prox
34320 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a y locking..**.**
34330 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 **.**.** The def
34340 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 73 63 68 ault locking sch
34350 65 6d 65 73 20 69 6e 20 53 51 4c 69 74 65 20 75 emes in SQLite u
34360 73 65 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f se byte-range lo
34370 63 6b 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61 cks on the.** da
34380 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 tabase file to c
34390 6f 6f 72 64 69 6e 61 74 65 20 73 61 66 65 2c 20 oordinate safe,
343a0 63 6f 6e 63 75 72 72 65 6e 74 20 61 63 63 65 73 concurrent acces
343b0 73 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 72 65 s by multiple re
343c0 61 64 65 72 73 0a 2a 2a 20 61 6e 64 20 77 72 69 aders.** and wri
343d0 74 65 72 73 20 5b 68 74 74 70 3a 2f 2f 73 71 6c ters [http://sql
343e0 69 74 65 2e 6f 72 67 2f 6c 6f 63 6b 69 6e 67 76 ite.org/lockingv
343f0 33 2e 68 74 6d 6c 5d 2e 20 20 54 68 65 20 66 69 3.html]. The fi
34400 76 65 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a ve file locking.
34410 2a 2a 20 73 74 61 74 65 73 20 28 55 4e 4c 4f 43 ** states (UNLOC
34420 4b 45 44 2c 20 50 45 4e 44 49 4e 47 2c 20 53 48 KED, PENDING, SH
34430 41 52 45 44 2c 20 52 45 53 45 52 56 45 44 2c 20 ARED, RESERVED,
34440 45 58 43 4c 55 53 49 56 45 29 20 61 72 65 20 69 EXCLUSIVE) are i
34450 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 61 73 mplemented.** as
34460 20 50 4f 53 49 58 20 72 65 61 64 20 26 20 77 72 POSIX read & wr
34470 69 74 65 20 6c 6f 63 6b 73 20 6f 76 65 72 20 66 ite locks over f
34480 69 78 65 64 20 73 65 74 20 6f 66 20 6c 6f 63 61 ixed set of loca
34490 74 69 6f 6e 73 20 28 76 69 61 20 66 73 63 74 6c tions (via fsctl
344a0 29 2c 0a 2a 2a 20 6f 6e 20 41 46 50 20 61 6e 64 ),.** on AFP and
344b0 20 53 4d 42 20 6f 6e 6c 79 20 65 78 63 6c 75 73 SMB only exclus
344c0 69 76 65 20 62 79 74 65 2d 72 61 6e 67 65 20 6c ive byte-range l
344d0 6f 63 6b 73 20 61 72 65 20 61 76 61 69 6c 61 62 ocks are availab
344e0 6c 65 20 76 69 61 20 66 73 63 74 6c 0a 2a 2a 20 le via fsctl.**
344f0 77 69 74 68 20 5f 49 4f 57 52 28 27 7a 27 2c 20 with _IOWR('z',
34500 32 33 2c 20 73 74 72 75 63 74 20 42 79 74 65 52 23, struct ByteR
34510 61 6e 67 65 4c 6f 63 6b 50 42 32 29 20 74 6f 20 angeLockPB2) to
34520 74 72 61 63 6b 20 74 68 65 20 73 61 6d 65 20 35 track the same 5
34530 20 73 74 61 74 65 73 2e 0a 2a 2a 20 54 6f 20 73 states..** To s
34540 69 6d 75 6c 61 74 65 20 61 20 46 5f 52 44 4c 43 imulate a F_RDLC
34550 4b 20 6f 6e 20 74 68 65 20 73 68 61 72 65 64 20 K on the shared
34560 72 61 6e 67 65 2c 20 6f 6e 20 41 46 50 20 61 20 range, on AFP a
34570 72 61 6e 64 6f 6d 6c 79 20 73 65 6c 65 63 74 65 randomly selecte
34580 64 0a 2a 2a 20 61 64 64 72 65 73 73 20 69 6e 20 d.** address in
34590 74 68 65 20 73 68 61 72 65 64 20 72 61 6e 67 65 the shared range
345a0 20 69 73 20 74 61 6b 65 6e 20 66 6f 72 20 61 20 is taken for a
345b0 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 SHARED lock, the
345c0 20 65 6e 74 69 72 65 0a 2a 2a 20 73 68 61 72 65 entire.** share
345d0 64 20 72 61 6e 67 65 20 69 73 20 74 61 6b 65 6e d range is taken
345e0 20 66 6f 72 20 61 6e 20 45 58 43 4c 55 53 49 56 for an EXCLUSIV
345f0 45 20 6c 6f 63 6b 29 3a 0a 2a 2a 0a 2a 2a 20 20 E lock):.**.**
34600 20 20 20 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 PENDING_BYTE
34610 20 20 20 20 20 20 20 20 30 78 34 30 30 30 30 30 0x400000
34620 30 30 0a 2a 2a 20 20 20 20 20 20 52 45 53 45 52 00.** RESER
34630 56 45 44 5f 42 59 54 45 20 20 20 20 20 20 20 30 VED_BYTE 0
34640 78 34 30 30 30 30 30 30 31 0a 2a 2a 20 20 20 20 x40000001.**
34650 20 20 53 48 41 52 45 44 5f 52 41 4e 47 45 20 20 SHARED_RANGE
34660 20 20 20 20 20 20 30 78 34 30 30 30 30 30 30 32 0x40000002
34670 20 2d 3e 20 30 78 34 30 30 30 30 32 30 30 0a 2a -> 0x40000200.*
34680 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 *.** This works
34690 77 65 6c 6c 20 6f 6e 20 74 68 65 20 6c 6f 63 61 well on the loca
346a0 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 2c 20 62 l file system, b
346b0 75 74 20 73 68 6f 77 73 20 61 20 6e 65 61 72 6c ut shows a nearl
346c0 79 20 31 30 30 78 0a 2a 2a 20 73 6c 6f 77 64 6f y 100x.** slowdo
346d0 77 6e 20 69 6e 20 72 65 61 64 20 70 65 72 66 6f wn in read perfo
346e0 72 6d 61 6e 63 65 20 6f 6e 20 41 46 50 20 62 65 rmance on AFP be
346f0 63 61 75 73 65 20 74 68 65 20 41 46 50 20 63 6c cause the AFP cl
34700 69 65 6e 74 20 64 69 73 61 62 6c 65 73 0a 2a 2a ient disables.**
34710 20 74 68 65 20 72 65 61 64 20 63 61 63 68 65 20 the read cache
34720 77 68 65 6e 20 62 79 74 65 2d 72 61 6e 67 65 20 when byte-range
34730 6c 6f 63 6b 73 20 61 72 65 20 70 72 65 73 65 6e locks are presen
34740 74 2e 20 20 45 6e 61 62 6c 69 6e 67 20 74 68 65 t. Enabling the
34750 20 72 65 61 64 0a 2a 2a 20 63 61 63 68 65 20 65 read.** cache e
34760 78 70 6f 73 65 73 20 61 20 63 61 63 68 65 20 63 xposes a cache c
34770 6f 68 65 72 65 6e 63 79 20 70 72 6f 62 6c 65 6d oherency problem
34780 20 74 68 61 74 20 69 73 20 70 72 65 73 65 6e 74 that is present
34790 20 6f 6e 20 61 6c 6c 20 4f 53 20 58 0a 2a 2a 20 on all OS X.**
347a0 73 75 70 70 6f 72 74 65 64 20 6e 65 74 77 6f 72 supported networ
347b0 6b 20 66 69 6c 65 20 73 79 73 74 65 6d 73 2e 20 k file systems.
347c0 20 4e 46 53 20 61 6e 64 20 41 46 50 20 62 6f 74 NFS and AFP bot
347d0 68 20 6f 62 73 65 72 76 65 20 74 68 65 0a 2a 2a h observe the.**
347e0 20 63 6c 6f 73 65 2d 74 6f 2d 6f 70 65 6e 20 73 close-to-open s
347f0 65 6d 61 6e 74 69 63 73 20 66 6f 72 20 65 6e 73 emantics for ens
34800 75 72 69 6e 67 20 63 61 63 68 65 20 63 6f 68 65 uring cache cohe
34810 72 65 6e 63 79 0a 2a 2a 20 5b 68 74 74 70 3a 2f rency.** [http:/
34820 2f 6e 66 73 2e 73 6f 75 72 63 65 66 6f 72 67 65 /nfs.sourceforge
34830 2e 6e 65 74 2f 23 66 61 71 5f 61 38 5d 2c 20 77 .net/#faq_a8], w
34840 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 65 66 hich does not ef
34850 66 65 63 74 69 76 65 6c 79 0a 2a 2a 20 61 64 64 fectively.** add
34860 72 65 73 73 20 74 68 65 20 72 65 71 75 69 72 65 ress the require
34870 6d 65 6e 74 73 20 66 6f 72 20 63 6f 6e 63 75 72 ments for concur
34880 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 61 63 rent database ac
34890 63 65 73 73 20 62 79 20 6d 75 6c 74 69 70 6c 65 cess by multiple
348a0 0a 2a 2a 20 72 65 61 64 65 72 73 20 61 6e 64 20 .** readers and
348b0 77 72 69 74 65 72 73 0a 2a 2a 20 5b 68 74 74 70 writers.** [http
348c0 3a 2f 2f 77 77 77 2e 6e 61 62 62 6c 65 2e 63 6f ://www.nabble.co
348d0 6d 2f 53 51 4c 69 74 65 2d 6f 6e 2d 4e 46 53 2d m/SQLite-on-NFS-
348e0 63 61 63 68 65 2d 63 6f 68 65 72 65 6e 63 79 2d cache-coherency-
348f0 74 64 31 35 36 35 35 37 30 31 2e 68 74 6d 6c 5d td15655701.html]
34900 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 64 64 72 65 ..**.** To addre
34910 73 73 20 74 68 65 20 70 65 72 66 6f 72 6d 61 6e ss the performan
34920 63 65 20 61 6e 64 20 63 61 63 68 65 20 63 6f 68 ce and cache coh
34930 65 72 65 6e 63 79 20 69 73 73 75 65 73 2c 20 70 erency issues, p
34940 72 6f 78 79 20 66 69 6c 65 20 6c 6f 63 6b 69 6e roxy file lockin
34950 67 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 68 65 g.** changes the
34960 20 77 61 79 20 64 61 74 61 62 61 73 65 20 61 63 way database ac
34970 63 65 73 73 20 69 73 20 63 6f 6e 74 72 6f 6c 6c cess is controll
34980 65 64 20 62 79 20 6c 69 6d 69 74 69 6e 67 20 61 ed by limiting a
34990 63 63 65 73 73 20 74 6f 20 61 0a 2a 2a 20 73 69 ccess to a.** si
349a0 6e 67 6c 65 20 68 6f 73 74 20 61 74 20 61 20 74 ngle host at a t
349b0 69 6d 65 20 61 6e 64 20 6d 6f 76 69 6e 67 20 66 ime and moving f
349c0 69 6c 65 20 6c 6f 63 6b 73 20 6f 66 66 20 6f 66 ile locks off of
349d0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
349e0 6c 65 0a 2a 2a 20 61 6e 64 20 6f 6e 74 6f 20 61 le.** and onto a
349f0 20 70 72 6f 78 79 20 66 69 6c 65 20 6f 6e 20 74 proxy file on t
34a00 68 65 20 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 he local file sy
34a10 73 74 65 6d 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2a stem. .**.**.**
34a20 20 55 73 69 6e 67 20 70 72 6f 78 79 20 6c 6f 63 Using proxy loc
34a30 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ks.** ----------
34a40 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 43 20 -------.**.** C
34a50 41 50 49 73 0a 2a 2a 0a 2a 2a 20 20 73 71 6c 69 APIs.**.** sqli
34a60 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c te3_file_control
34a70 28 64 62 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c (db, dbname, SQL
34a80 49 54 45 5f 46 43 4e 54 4c 5f 53 45 54 5f 4c 4f ITE_FCNTL_SET_LO
34a90 43 4b 50 52 4f 58 59 46 49 4c 45 2c 0a 2a 2a 20 CKPROXYFILE,.**
34aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
34ab0 20 20 20 20 20 20 3c 70 72 6f 78 79 5f 70 61 74 <proxy_pat
34ac0 68 3e 20 7c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a h> | ":auto:");.
34ad0 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ** sqlite3_file
34ae0 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 64 62 6e _control(db, dbn
34af0 61 6d 65 2c 20 53 51 4c 49 54 45 5f 46 43 4e 54 ame, SQLITE_FCNT
34b00 4c 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 L_GET_LOCKPROXYF
34b10 49 4c 45 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 ILE,.**
34b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 3c &<
34b30 70 72 6f 78 79 5f 70 61 74 68 3e 29 3b 0a 2a 2a proxy_path>);.**
34b40 0a 2a 2a 0a 2a 2a 20 53 51 4c 20 70 72 61 67 6d .**.** SQL pragm
34b50 61 73 0a 2a 2a 0a 2a 2a 20 20 50 52 41 47 4d 41 as.**.** PRAGMA
34b60 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b [database.]lock
34b70 5f 70 72 6f 78 79 5f 66 69 6c 65 3d 3c 70 72 6f _proxy_file=<pro
34b80 78 79 5f 70 61 74 68 3e 20 7c 20 3a 61 75 74 6f xy_path> | :auto
34b90 3a 0a 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 :.** PRAGMA [da
34ba0 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f tabase.]lock_pro
34bb0 78 79 5f 66 69 6c 65 0a 2a 2a 0a 2a 2a 20 53 70 xy_file.**.** Sp
34bc0 65 63 69 66 79 69 6e 67 20 22 3a 61 75 74 6f 3a ecifying ":auto:
34bd0 22 20 6d 65 61 6e 73 20 74 68 61 74 20 69 66 20 " means that if
34be0 74 68 65 72 65 20 69 73 20 61 20 63 6f 6e 63 68 there is a conch
34bf0 20 66 69 6c 65 20 77 69 74 68 20 61 20 6d 61 74 file with a mat
34c00 63 68 69 6e 67 0a 2a 2a 20 68 6f 73 74 20 49 44 ching.** host ID
34c10 20 69 6e 20 69 74 2c 20 74 68 65 20 70 72 6f 78 in it, the prox
34c20 79 20 70 61 74 68 20 69 6e 20 74 68 65 20 63 6f y path in the co
34c30 6e 63 68 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 nch file will be
34c40 20 75 73 65 64 2c 20 6f 74 68 65 72 77 69 73 65 used, otherwise
34c50 0a 2a 2a 20 61 20 70 72 6f 78 79 20 70 61 74 68 .** a proxy path
34c60 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 75 73 based on the us
34c70 65 72 27 73 20 74 65 6d 70 20 64 69 72 0a 2a 2a er's temp dir.**
34c80 20 28 76 69 61 20 63 6f 6e 66 73 74 72 28 5f 43 (via confstr(_C
34c90 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45 S_DARWIN_USER_TE
34ca0 4d 50 5f 44 49 52 2c 2e 2e 2e 29 29 20 77 69 6c MP_DIR,...)) wil
34cb0 6c 20 62 65 20 75 73 65 64 20 61 6e 64 20 74 68 l be used and th
34cc0 65 0a 2a 2a 20 61 63 74 75 61 6c 20 70 72 6f 78 e.** actual prox
34cd0 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 67 y file name is g
34ce0 65 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 74 68 enerated from th
34cf0 65 20 6e 61 6d 65 20 61 6e 64 20 70 61 74 68 20 e name and path
34d00 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 of the.** databa
34d10 73 65 20 66 69 6c 65 2e 20 20 46 6f 72 20 65 78 se file. For ex
34d20 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ample:.**.**
34d30 20 20 20 46 6f 72 20 64 61 74 61 62 61 73 65 20 For database
34d40 70 61 74 68 20 22 2f 55 73 65 72 73 2f 6d 65 2f path "/Users/me/
34d50 66 6f 6f 2e 64 62 22 20 0a 2a 2a 20 20 20 20 20 foo.db" .**
34d60 20 20 54 68 65 20 6c 6f 63 6b 20 70 61 74 68 20 The lock path
34d70 77 69 6c 6c 20 62 65 20 22 3c 74 6d 70 64 69 72 will be "<tmpdir
34d80 3e 2f 73 71 6c 69 74 65 70 6c 6f 63 6b 73 2f 5f >/sqliteplocks/_
34d90 55 73 65 72 73 5f 6d 65 5f 66 6f 6f 2e 64 62 3a Users_me_foo.db:
34da0 61 75 74 6f 3a 22 29 0a 2a 2a 0a 2a 2a 20 4f 6e auto:").**.** On
34db0 63 65 20 61 20 6c 6f 63 6b 20 70 72 6f 78 79 20 ce a lock proxy
34dc0 69 73 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f is configured fo
34dd0 72 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e r a database con
34de0 6e 65 63 74 69 6f 6e 2c 20 69 74 20 63 61 6e 20 nection, it can
34df0 6e 6f 74 0a 2a 2a 20 62 65 20 72 65 6d 6f 76 65 not.** be remove
34e00 64 2c 20 68 6f 77 65 76 65 72 20 69 74 20 6d 61 d, however it ma
34e10 79 20 62 65 20 73 77 69 74 63 68 65 64 20 74 6f y be switched to
34e20 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 72 6f a different pro
34e30 78 79 20 70 61 74 68 20 76 69 61 0a 2a 2a 20 74 xy path via.** t
34e40 68 65 20 61 62 6f 76 65 20 41 50 49 73 20 28 61 he above APIs (a
34e50 73 73 75 6d 69 6e 67 20 74 68 65 20 63 6f 6e 63 ssuming the conc
34e60 68 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 62 65 h file is not be
34e70 69 6e 67 20 68 65 6c 64 20 62 79 20 61 6e 6f 74 ing held by anot
34e80 68 65 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f her.** connectio
34e90 6e 20 6f 72 20 70 72 6f 63 65 73 73 29 2e 20 0a n or process). .
34ea0 2a 2a 0a 2a 2a 0a 2a 2a 20 48 6f 77 20 70 72 6f **.**.** How pro
34eb0 78 79 20 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73 xy locking works
34ec0 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d .** ------------
34ed0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a -----------.**.*
34ee0 2a 20 50 72 6f 78 79 20 66 69 6c 65 20 6c 6f 63 * Proxy file loc
34ef0 6b 69 6e 67 20 72 65 6c 69 65 73 20 70 72 69 6d king relies prim
34f00 61 72 69 6c 79 20 6f 6e 20 74 77 6f 20 6e 65 77 arily on two new
34f10 20 73 75 70 70 6f 72 74 69 6e 67 20 66 69 6c 65 supporting file
34f20 73 3a 20 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 63 s: .**.** * c
34f30 6f 6e 63 68 20 66 69 6c 65 20 74 6f 20 6c 69 6d onch file to lim
34f40 69 74 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 it access to the
34f50 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
34f60 6f 20 61 20 73 69 6e 67 6c 65 20 68 6f 73 74 0a o a single host.
34f70 2a 2a 20 20 20 20 20 20 61 74 20 61 20 74 69 6d ** at a tim
34f80 65 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 70 72 6f e.**.** * pro
34f90 78 79 20 66 69 6c 65 20 74 6f 20 61 63 74 20 61 xy file to act a
34fa0 73 20 61 20 70 72 6f 78 79 20 66 6f 72 20 74 68 s a proxy for th
34fb0 65 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 e advisory locks
34fc0 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 20 20 20 normally.**
34fd0 20 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 64 taken on the d
34fe0 61 74 61 62 61 73 65 0a 2a 2a 0a 2a 2a 20 54 68 atabase.**.** Th
34ff0 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 2d 20 74 e conch file - t
35000 6f 20 75 73 65 20 61 20 70 72 6f 78 79 20 66 69 o use a proxy fi
35010 6c 65 2c 20 73 71 6c 69 74 65 20 6d 75 73 74 20 le, sqlite must
35020 66 69 72 73 74 20 22 68 6f 6c 64 20 74 68 65 20 first "hold the
35030 63 6f 6e 63 68 22 0a 2a 2a 20 62 79 20 74 61 6b conch".** by tak
35040 69 6e 67 20 61 6e 20 73 71 6c 69 74 65 2d 73 74 ing an sqlite-st
35050 79 6c 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 yle shared lock
35060 6f 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c on the conch fil
35070 65 2c 20 72 65 61 64 69 6e 67 20 74 68 65 0a 2a e, reading the.*
35080 2a 20 63 6f 6e 74 65 6e 74 73 20 61 6e 64 20 63 * contents and c
35090 6f 6d 70 61 72 69 6e 67 20 74 68 65 20 68 6f 73 omparing the hos
350a0 74 27 73 20 75 6e 69 71 75 65 20 68 6f 73 74 20 t's unique host
350b0 49 44 20 28 73 65 65 20 62 65 6c 6f 77 29 20 61 ID (see below) a
350c0 6e 64 20 6c 6f 63 6b 0a 2a 2a 20 70 72 6f 78 79 nd lock.** proxy
350d0 20 70 61 74 68 20 61 67 61 69 6e 73 74 20 74 68 path against th
350e0 65 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20 e values stored
350f0 69 6e 20 74 68 65 20 63 6f 6e 63 68 2e 20 20 54 in the conch. T
35100 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73 he conch file is
35110 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 74 68 .** stored in th
35120 65 20 73 61 6d 65 20 64 69 72 65 63 74 6f 72 79 e same directory
35130 20 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65 as the database
35140 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 66 69 file and the fi
35150 6c 65 20 6e 61 6d 65 0a 2a 2a 20 69 73 20 70 61 le name.** is pa
35160 74 74 65 72 6e 65 64 20 61 66 74 65 72 20 74 68 tterned after th
35170 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
35180 6e 61 6d 65 20 61 73 20 22 2e 3c 64 61 74 61 62 name as ".<datab
35190 61 73 65 6e 61 6d 65 3e 2d 63 6f 6e 63 68 22 2e asename>-conch".
351a0 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6e 63 68 .** If the conch
351b0 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 file does not e
351c0 78 69 73 74 2c 20 6f 72 20 69 74 73 20 63 6f 6e xist, or its con
351d0 74 65 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61 74 tents do not mat
351e0 63 68 20 74 68 65 0a 2a 2a 20 68 6f 73 74 20 49 ch the.** host I
351f0 44 20 61 6e 64 2f 6f 72 20 70 72 6f 78 79 20 70 D and/or proxy p
35200 61 74 68 2c 20 74 68 65 6e 20 74 68 65 20 6c 6f ath, then the lo
35210 63 6b 20 69 73 20 65 73 63 61 6c 61 74 65 64 20 ck is escalated
35220 74 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 0a to an exclusive.
35230 2a 2a 20 6c 6f 63 6b 20 61 6e 64 20 74 68 65 20 ** lock and the
35240 63 6f 6e 63 68 20 66 69 6c 65 20 63 6f 6e 74 65 conch file conte
35250 6e 74 73 20 69 73 20 75 70 64 61 74 65 64 20 77 nts is updated w
35260 69 74 68 20 74 68 65 20 68 6f 73 74 20 49 44 20 ith the host ID
35270 61 6e 64 20 70 72 6f 78 79 0a 2a 2a 20 70 61 74 and proxy.** pat
35280 68 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 69 h and the lock i
35290 73 20 64 6f 77 6e 67 72 61 64 65 64 20 74 6f 20 s downgraded to
352a0 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 67 a shared lock ag
352b0 61 69 6e 2e 20 20 49 66 20 74 68 65 20 63 6f 6e ain. If the con
352c0 63 68 0a 2a 2a 20 69 73 20 68 65 6c 64 20 62 79 ch.** is held by
352d0 20 61 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 73 another process
352e0 20 28 77 69 74 68 20 61 20 73 68 61 72 65 64 20 (with a shared
352f0 6c 6f 63 6b 29 2c 20 74 68 65 20 65 78 63 6c 75 lock), the exclu
35300 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 77 69 6c sive lock.** wil
35310 6c 20 66 61 69 6c 20 61 6e 64 20 53 51 4c 49 54 l fail and SQLIT
35320 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e E_BUSY is return
35330 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 ed..**.** The pr
35340 6f 78 79 20 66 69 6c 65 20 2d 20 61 20 73 69 6e oxy file - a sin
35350 67 6c 65 2d 62 79 74 65 20 66 69 6c 65 20 75 73 gle-byte file us
35360 65 64 20 66 6f 72 20 61 6c 6c 20 61 64 76 69 73 ed for all advis
35370 6f 72 79 20 66 69 6c 65 20 6c 6f 63 6b 73 0a 2a ory file locks.*
35380 2a 20 6e 6f 72 6d 61 6c 6c 79 20 74 61 6b 65 6e * normally taken
35390 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
353a0 20 66 69 6c 65 2e 20 20 20 54 68 69 73 20 61 6c file. This al
353b0 6c 6f 77 73 20 66 6f 72 20 73 61 66 65 20 73 68 lows for safe sh
353c0 61 72 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 20 aring.** of the
353d0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f database file fo
353e0 72 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64 65 r multiple reade
353f0 72 73 20 61 6e 64 20 77 72 69 74 65 72 73 20 6f rs and writers o
35400 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 68 6f n the same.** ho
35410 73 74 20 28 74 68 65 20 63 6f 6e 63 68 20 65 6e st (the conch en
35420 73 75 72 65 73 20 74 68 61 74 20 74 68 65 79 20 sures that they
35430 61 6c 6c 20 75 73 65 20 74 68 65 20 73 61 6d 65 all use the same
35440 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 local lock file
35450 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 65 73 74 )..**.** Request
35460 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 70 72 6f ing the lock pro
35470 78 79 20 64 6f 65 73 20 6e 6f 74 20 69 6d 6d 65 xy does not imme
35480 64 69 61 74 65 6c 79 20 74 61 6b 65 20 74 68 65 diately take the
35490 20 63 6f 6e 63 68 2c 20 69 74 20 69 73 0a 2a 2a conch, it is.**
354a0 20 6f 6e 6c 79 20 74 61 6b 65 6e 20 77 68 65 6e only taken when
354b0 20 74 68 65 20 66 69 72 73 74 20 72 65 71 75 65 the first reque
354c0 73 74 20 74 6f 20 6c 6f 63 6b 20 64 61 74 61 62 st to lock datab
354d0 61 73 65 20 66 69 6c 65 20 69 73 20 6d 61 64 65 ase file is made
354e0 2e 20 20 0a 2a 2a 20 54 68 69 73 20 6d 61 74 63 . .** This matc
354f0 68 65 73 20 74 68 65 20 73 65 6d 61 6e 74 69 63 hes the semantic
35500 73 20 6f 66 20 74 68 65 20 74 72 61 64 69 74 69 s of the traditi
35510 6f 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 62 65 68 onal locking beh
35520 61 76 69 6f 72 2c 20 77 68 65 72 65 0a 2a 2a 20 avior, where.**
35530 6f 70 65 6e 69 6e 67 20 61 20 63 6f 6e 6e 65 63 opening a connec
35540 74 69 6f 6e 20 74 6f 20 61 20 64 61 74 61 62 61 tion to a databa
35550 73 65 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 se file does not
35560 20 74 61 6b 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 take a lock on
35570 69 74 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72 65 it..** The share
35580 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6e 20 6f 70 d lock and an op
35590 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 en file descript
355a0 6f 72 20 61 72 65 20 6d 61 69 6e 74 61 69 6e 65 or are maintaine
355b0 64 20 75 6e 74 69 6c 20 0a 2a 2a 20 74 68 65 20 d until .** the
355c0 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74 68 connection to th
355d0 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6c e database is cl
355e0 6f 73 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 osed. .**.** The
355f0 20 70 72 6f 78 79 20 66 69 6c 65 20 61 6e 64 20 proxy file and
35600 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 72 the lock file ar
35610 65 20 6e 65 76 65 72 20 64 65 6c 65 74 65 64 20 e never deleted
35620 73 6f 20 74 68 65 79 20 6f 6e 6c 79 20 6e 65 65 so they only nee
35630 64 0a 2a 2a 20 74 6f 20 62 65 20 63 72 65 61 74 d.** to be creat
35640 65 64 20 74 68 65 20 66 69 72 73 74 20 74 69 6d ed the first tim
35650 65 20 74 68 65 79 20 61 72 65 20 75 73 65 64 2e e they are used.
35660 0a 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 61 .**.** Configura
35670 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 tion options.**
35680 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
35690 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 53 51 4c -----.**.** SQL
356a0 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 ITE_PREFER_PROXY
356b0 5f 4c 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20 20 _LOCKING.**.**
356c0 20 20 20 20 20 44 61 74 61 62 61 73 65 20 66 69 Database fi
356d0 6c 65 73 20 61 63 63 65 73 73 65 64 20 6f 6e 20 les accessed on
356e0 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 20 73 non-local file s
356f0 79 73 74 65 6d 73 20 61 72 65 0a 2a 2a 20 20 20 ystems are.**
35700 20 20 20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c automaticall
35710 79 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 y configured for
35720 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2c 20 proxy locking,
35730 6c 6f 63 6b 20 66 69 6c 65 73 20 61 72 65 0a 2a lock files are.*
35740 2a 20 20 20 20 20 20 20 6e 61 6d 65 64 20 61 75 * named au
35750 74 6f 6d 61 74 69 63 61 6c 6c 79 20 75 73 69 6e tomatically usin
35760 67 20 74 68 65 20 73 61 6d 65 20 6c 6f 67 69 63 g the same logic
35770 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 50 52 41 as.** PRA
35780 47 4d 41 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 GMA lock_proxy_f
35790 69 6c 65 3d 22 3a 61 75 74 6f 3a 22 0a 2a 2a 20 ile=":auto:".**
357a0 20 20 20 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 50 .** SQLITE_P
357b0 52 4f 58 59 5f 44 45 42 55 47 0a 2a 2a 0a 2a 2a ROXY_DEBUG.**.**
357c0 20 20 20 20 20 20 20 45 6e 61 62 6c 65 73 20 74 Enables t
357d0 68 65 20 6c 6f 67 67 69 6e 67 20 6f 66 20 65 72 he logging of er
357e0 72 6f 72 20 6d 65 73 73 61 67 65 73 20 64 75 72 ror messages dur
357f0 69 6e 67 20 68 6f 73 74 20 69 64 20 66 69 6c 65 ing host id file
35800 0a 2a 2a 20 20 20 20 20 20 20 72 65 74 72 69 65 .** retrie
35810 76 61 6c 20 61 6e 64 20 63 72 65 61 74 69 6f 6e val and creation
35820 0a 2a 2a 0a 2a 2a 20 20 4c 4f 43 4b 50 52 4f 58 .**.** LOCKPROX
35830 59 44 49 52 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 YDIR.**.**
35840 20 4f 76 65 72 72 69 64 65 73 20 74 68 65 20 64 Overrides the d
35850 65 66 61 75 6c 74 20 64 69 72 65 63 74 6f 72 79 efault directory
35860 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 20 70 used for lock p
35870 72 6f 78 79 20 66 69 6c 65 73 20 74 68 61 74 0a roxy files that.
35880 2a 2a 20 20 20 20 20 20 20 61 72 65 20 6e 61 6d ** are nam
35890 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ed automatically
358a0 20 76 69 61 20 74 68 65 20 22 3a 61 75 74 6f 3a via the ":auto:
358b0 22 20 73 65 74 74 69 6e 67 0a 2a 2a 0a 2a 2a 20 " setting.**.**
358c0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
358d0 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 PROXYDIR_PERMISS
358e0 49 4f 4e 53 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 IONS.**.**
358f0 20 50 65 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20 Permissions to
35900 75 73 65 20 77 68 65 6e 20 63 72 65 61 74 69 6e use when creatin
35910 67 20 61 20 64 69 72 65 63 74 6f 72 79 20 66 6f g a directory fo
35920 72 20 73 74 6f 72 69 6e 67 20 74 68 65 0a 2a 2a r storing the.**
35930 20 20 20 20 20 20 20 6c 6f 63 6b 20 70 72 6f 78 lock prox
35940 79 20 66 69 6c 65 73 2c 20 6f 6e 6c 79 20 75 73 y files, only us
35950 65 64 20 77 68 65 6e 20 4c 4f 43 4b 50 52 4f 58 ed when LOCKPROX
35960 59 44 49 52 20 69 73 20 6e 6f 74 20 73 65 74 2e YDIR is not set.
35970 0a 2a 2a 20 20 20 20 0a 2a 2a 20 20 20 20 0a 2a .** .** .*
35980 2a 20 41 73 20 6d 65 6e 74 69 6f 6e 65 64 20 61 * As mentioned a
35990 62 6f 76 65 2c 20 77 68 65 6e 20 63 6f 6d 70 69 bove, when compi
359a0 6c 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f led with SQLITE_
359b0 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 PREFER_PROXY_LOC
359c0 4b 49 4e 47 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 KING,.** setting
359d0 20 74 68 65 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 the environment
359e0 20 76 61 72 69 61 62 6c 65 20 53 51 4c 49 54 45 variable SQLITE
359f0 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 _FORCE_PROXY_LOC
35a00 4b 49 4e 47 20 74 6f 20 31 20 77 69 6c 6c 0a 2a KING to 1 will.*
35a10 2a 20 66 6f 72 63 65 20 70 72 6f 78 79 20 6c 6f * force proxy lo
35a20 63 6b 69 6e 67 20 74 6f 20 62 65 20 75 73 65 64 cking to be used
35a30 20 66 6f 72 20 65 76 65 72 79 20 64 61 74 61 62 for every datab
35a40 61 73 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c ase file opened,
35a50 20 61 6e 64 20 30 0a 2a 2a 20 77 69 6c 6c 20 66 and 0.** will f
35a60 6f 72 63 65 20 61 75 74 6f 6d 61 74 69 63 20 70 orce automatic p
35a70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 roxy locking to
35a80 62 65 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 be disabled for
35a90 61 6c 6c 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 all database.**
35aa0 66 69 6c 65 73 20 28 65 78 70 6c 69 63 69 74 6c files (explicitl
35ab0 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 53 51 y calling the SQ
35ac0 4c 49 54 45 5f 46 43 4e 54 4c 5f 53 45 54 5f 4c LITE_FCNTL_SET_L
35ad0 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 70 72 61 OCKPROXYFILE pra
35ae0 67 6d 61 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 gma or.** sqlite
35af0 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 20 41 50 _file_control AP
35b00 49 20 69 73 20 6e 6f 74 20 61 66 66 65 63 74 65 I is not affecte
35b10 64 20 62 79 20 53 51 4c 49 54 45 5f 46 4f 52 43 d by SQLITE_FORC
35b20 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 29 E_PROXY_LOCKING)
35b30 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 78 ..*/../*.** Prox
35b40 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c y locking is onl
35b50 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d y available on M
35b60 61 63 4f 53 58 20 0a 2a 2f 0a 23 69 66 20 64 65 acOSX .*/.#if de
35b70 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
35b80 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
35b90 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
35ba0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 ./*.** The proxy
35bb0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 68 LockingContext h
35bc0 61 73 20 74 68 65 20 70 61 74 68 20 61 6e 64 20 as the path and
35bd0 66 69 6c 65 20 73 74 72 75 63 74 75 72 65 73 20 file structures
35be0 66 6f 72 20 74 68 65 20 72 65 6d 6f 74 65 20 0a for the remote .
35bf0 2a 2a 20 61 6e 64 20 6c 6f 63 61 6c 20 70 72 6f ** and local pro
35c00 78 79 20 66 69 6c 65 73 20 69 6e 20 69 74 0a 2a xy files in it.*
35c10 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
35c20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
35c30 74 65 78 74 20 70 72 6f 78 79 4c 6f 63 6b 69 6e text proxyLockin
35c40 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 gContext;.struct
35c50 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
35c60 74 65 78 74 20 7b 0a 20 20 75 6e 69 78 46 69 6c text {. unixFil
35c70 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b 20 20 20 e *conchFile;
35c80 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 63 6f /* Open co
35c90 6e 63 68 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 nch file */. ch
35ca0 61 72 20 2a 63 6f 6e 63 68 46 69 6c 65 50 61 74 ar *conchFilePat
35cb0 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 h; /* Na
35cc0 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 63 68 20 me of the conch
35cd0 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69 file */. unixFi
35ce0 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 3b 20 20 le *lockProxy;
35cf0 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 /* Open p
35d00 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a roxy lock file *
35d10 2f 0a 20 20 63 68 61 72 20 2a 6c 6f 63 6b 50 72 /. char *lockPr
35d20 6f 78 79 50 61 74 68 3b 20 20 20 20 20 20 20 20 oxyPath;
35d30 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
35d40 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 20 proxy lock file
35d50 2a 2f 0a 20 20 63 68 61 72 20 2a 64 62 50 61 74 */. char *dbPat
35d60 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
35d70 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
35d80 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 open file */.
35d90 69 6e 74 20 63 6f 6e 63 68 48 65 6c 64 3b 20 20 int conchHeld;
35da0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
35db0 31 20 69 66 20 74 68 65 20 63 6f 6e 63 68 20 69 1 if the conch i
35dc0 73 20 68 65 6c 64 2c 20 2d 31 20 69 66 20 6c 6f s held, -1 if lo
35dd0 63 6b 6c 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20 ckless */. int
35de0 6e 46 61 69 6c 73 3b 20 20 20 20 20 20 20 20 20 nFails;
35df0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
35e00 65 72 20 6f 66 20 63 6f 6e 63 68 20 74 61 6b 69 er of conch taki
35e10 6e 67 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a 20 ng failures */.
35e20 20 76 6f 69 64 20 2a 6f 6c 64 4c 6f 63 6b 69 6e void *oldLockin
35e30 67 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 2f 2a gContext; /*
35e40 20 4f 72 69 67 69 6e 61 6c 20 6c 6f 63 6b 69 6e Original lockin
35e50 67 63 6f 6e 74 65 78 74 20 74 6f 20 72 65 73 74 gcontext to rest
35e60 6f 72 65 20 6f 6e 20 63 6c 6f 73 65 20 2a 2f 0a ore on close */.
35e70 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 sqlite3_io_met
35e80 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 4f 6c 64 hods const *pOld
35e90 4d 65 74 68 6f 64 3b 20 20 20 20 20 2f 2a 20 4f Method; /* O
35ea0 72 69 67 69 6e 61 6c 20 49 2f 4f 20 6d 65 74 68 riginal I/O meth
35eb0 6f 64 73 20 66 6f 72 20 63 6c 6f 73 65 20 2a 2f ods for close */
35ec0 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20 .};../* .** The
35ed0 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 20 proxy lock file
35ee0 70 61 74 68 20 66 6f 72 20 74 68 65 20 64 61 74 path for the dat
35ef0 61 62 61 73 65 20 61 74 20 64 62 50 61 74 68 20 abase at dbPath
35f00 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 is written into
35f10 6c 50 61 74 68 2c 20 0a 2a 2a 20 77 68 69 63 68 lPath, .** which
35f20 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 76 must point to v
35f30 61 6c 69 64 2c 20 77 72 69 74 61 62 6c 65 20 6d alid, writable m
35f40 65 6d 6f 72 79 20 6c 61 72 67 65 20 65 6e 6f 75 emory large enou
35f50 67 68 20 66 6f 72 20 61 20 6d 61 78 4c 65 6e 20 gh for a maxLen
35f60 6c 65 6e 67 74 68 0a 2a 2a 20 66 69 6c 65 20 70 length.** file p
35f70 61 74 68 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 ath. .*/.static
35f80 69 6e 74 20 70 72 6f 78 79 47 65 74 4c 6f 63 6b int proxyGetLock
35f90 50 61 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20 Path(const char
35fa0 2a 64 62 50 61 74 68 2c 20 63 68 61 72 20 2a 6c *dbPath, char *l
35fb0 50 61 74 68 2c 20 73 69 7a 65 5f 74 20 6d 61 78 Path, size_t max
35fc0 4c 65 6e 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b Len){. int len;
35fd0 0a 20 20 69 6e 74 20 64 62 4c 65 6e 3b 0a 20 20 . int dbLen;.
35fe0 69 6e 74 20 69 3b 0a 0a 23 69 66 64 65 66 20 4c int i;..#ifdef L
35ff0 4f 43 4b 50 52 4f 58 59 44 49 52 0a 20 20 6c 65 OCKPROXYDIR. le
36000 6e 20 3d 20 73 74 72 6c 63 70 79 28 6c 50 61 74 n = strlcpy(lPat
36010 68 2c 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52 2c h, LOCKPROXYDIR,
36020 20 6d 61 78 4c 65 6e 29 3b 0a 23 65 6c 73 65 0a maxLen);.#else.
36030 23 20 69 66 64 65 66 20 5f 43 53 5f 44 41 52 57 # ifdef _CS_DARW
36040 49 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 IN_USER_TEMP_DIR
36050 0a 20 20 7b 0a 20 20 20 20 69 66 28 20 21 63 6f . {. if( !co
36060 6e 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49 4e nfstr(_CS_DARWIN
36070 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c 20 _USER_TEMP_DIR,
36080 6c 50 61 74 68 2c 20 6d 61 78 4c 65 6e 29 20 29 lPath, maxLen) )
36090 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 28 {. OSTRACE(
360a0 28 22 47 45 54 4c 4f 43 4b 50 41 54 48 20 20 66 ("GETLOCKPATH f
360b0 61 69 6c 65 64 20 25 73 20 65 72 72 6e 6f 3d 25 ailed %s errno=%
360c0 64 20 70 69 64 3d 25 64 5c 6e 22 2c 0a 20 20 20 d pid=%d\n",.
360d0 20 20 20 20 20 20 20 20 20 20 20 20 6c 50 61 74 lPat
360e0 68 2c 20 65 72 72 6e 6f 2c 20 6f 73 47 65 74 70 h, errno, osGetp
360f0 69 64 28 30 29 29 29 3b 0a 20 20 20 20 20 20 72 id(0)));. r
36100 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
36110 52 52 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 RR_LOCK;. }.
36120 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 61 74 len = strlcat
36130 28 6c 50 61 74 68 2c 20 22 73 71 6c 69 74 65 70 (lPath, "sqlitep
36140 6c 6f 63 6b 73 22 2c 20 6d 61 78 4c 65 6e 29 3b locks", maxLen);
36150 20 20 20 20 0a 20 20 7d 0a 23 20 65 6c 73 65 0a . }.# else.
36160 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 70 79 28 len = strlcpy(
36170 6c 50 61 74 68 2c 20 22 2f 74 6d 70 2f 22 2c 20 lPath, "/tmp/",
36180 6d 61 78 4c 65 6e 29 3b 0a 23 20 65 6e 64 69 66 maxLen);.# endif
36190 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6c .#endif.. if( l
361a0 50 61 74 68 5b 6c 65 6e 2d 31 5d 21 3d 27 2f 27 Path[len-1]!='/'
361b0 20 29 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 ){. len = st
361c0 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 2f 22 rlcat(lPath, "/"
361d0 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 7d 0a 20 , maxLen);. }.
361e0 20 0a 20 20 2f 2a 20 74 72 61 6e 73 66 6f 72 6d . /* transform
361f0 20 74 68 65 20 64 62 20 70 61 74 68 20 74 6f 20 the db path to
36200 61 20 75 6e 69 71 75 65 20 63 61 63 68 65 20 6e a unique cache n
36210 61 6d 65 20 2a 2f 0a 20 20 64 62 4c 65 6e 20 3d ame */. dbLen =
36220 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 64 62 50 (int)strlen(dbP
36230 61 74 68 29 3b 0a 20 20 66 6f 72 28 20 69 3d 30 ath);. for( i=0
36240 3b 20 69 3c 64 62 4c 65 6e 20 26 26 20 28 69 2b ; i<dbLen && (i+
36250 6c 65 6e 2b 37 29 3c 28 69 6e 74 29 6d 61 78 4c len+7)<(int)maxL
36260 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 en; i++){. ch
36270 61 72 20 63 20 3d 20 64 62 50 61 74 68 5b 69 5d ar c = dbPath[i]
36280 3b 0a 20 20 20 20 6c 50 61 74 68 5b 69 2b 6c 65 ;. lPath[i+le
36290 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27 29 3f 27 5f n] = (c=='/')?'_
362a0 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c 50 61 74 68 ':c;. }. lPath
362b0 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27 3b 0a 20 20 [i+len]='\0';.
362c0 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 strlcat(lPath, "
362d0 3a 61 75 74 6f 3a 22 2c 20 6d 61 78 4c 65 6e 29 :auto:", maxLen)
362e0 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 47 45 ;. OSTRACE(("GE
362f0 54 4c 4f 43 4b 50 41 54 48 20 20 70 72 6f 78 79 TLOCKPATH proxy
36300 20 6c 6f 63 6b 20 70 61 74 68 3d 25 73 20 70 69 lock path=%s pi
36310 64 3d 25 64 5c 6e 22 2c 20 6c 50 61 74 68 2c 20 d=%d\n", lPath,
36320 6f 73 47 65 74 70 69 64 28 30 29 29 29 3b 0a 20 osGetpid(0)));.
36330 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
36340 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 20 2a 2a 20 43 72 K;.}../* . ** Cr
36350 65 61 74 65 73 20 74 68 65 20 6c 6f 63 6b 20 66 eates the lock f
36360 69 6c 65 20 61 6e 64 20 61 6e 79 20 6d 69 73 73 ile and any miss
36370 69 6e 67 20 64 69 72 65 63 74 6f 72 69 65 73 20 ing directories
36380 69 6e 20 6c 6f 63 6b 50 61 74 68 0a 20 2a 2f 0a in lockPath. */.
36390 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
363a0 43 72 65 61 74 65 4c 6f 63 6b 50 61 74 68 28 63 CreateLockPath(c
363b0 6f 6e 73 74 20 63 68 61 72 20 2a 6c 6f 63 6b 50 onst char *lockP
363c0 61 74 68 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6c ath){. int i, l
363d0 65 6e 3b 0a 20 20 63 68 61 72 20 62 75 66 5b 4d en;. char buf[M
363e0 41 58 50 41 54 48 4c 45 4e 5d 3b 0a 20 20 69 6e AXPATHLEN];. in
363f0 74 20 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 0a t start = 0;. .
36400 20 20 61 73 73 65 72 74 28 6c 6f 63 6b 50 61 74 assert(lockPat
36410 68 21 3d 4e 55 4c 4c 29 3b 0a 20 20 2f 2a 20 74 h!=NULL);. /* t
36420 72 79 20 74 6f 20 63 72 65 61 74 65 20 61 6c 6c ry to create all
36430 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 the intermediat
36440 65 20 64 69 72 65 63 74 6f 72 69 65 73 20 2a 2f e directories */
36450 0a 20 20 6c 65 6e 20 3d 20 28 69 6e 74 29 73 74 . len = (int)st
36460 72 6c 65 6e 28 6c 6f 63 6b 50 61 74 68 29 3b 0a rlen(lockPath);.
36470 20 20 62 75 66 5b 30 5d 20 3d 20 6c 6f 63 6b 50 buf[0] = lockP
36480 61 74 68 5b 30 5d 3b 0a 20 20 66 6f 72 28 20 69 ath[0];. for( i
36490 3d 31 3b 20 69 3c 6c 65 6e 3b 20 69 2b 2b 20 29 =1; i<len; i++ )
364a0 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 50 61 {. if( lockPa
364b0 74 68 5b 69 5d 20 3d 3d 20 27 2f 27 20 26 26 20 th[i] == '/' &&
364c0 28 69 20 2d 20 73 74 61 72 74 20 3e 20 30 29 20 (i - start > 0)
364d0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 6f 6e 6c 79 ){. /* only
364e0 20 6d 6b 64 69 72 20 69 66 20 6c 65 61 66 20 64 mkdir if leaf d
364f0 69 72 20 21 3d 20 22 2e 22 20 6f 72 20 22 2f 22 ir != "." or "/"
36500 20 6f 72 20 22 2e 2e 22 20 2a 2f 0a 20 20 20 20 or ".." */.
36510 20 20 69 66 28 20 69 2d 73 74 61 72 74 3e 32 20 if( i-start>2
36520 7c 7c 20 28 69 2d 73 74 61 72 74 3d 3d 31 20 26 || (i-start==1 &
36530 26 20 62 75 66 5b 73 74 61 72 74 5d 20 21 3d 20 & buf[start] !=
36540 27 2e 27 20 26 26 20 62 75 66 5b 73 74 61 72 74 '.' && buf[start
36550 5d 20 21 3d 20 27 2f 27 29 20 0a 20 20 20 20 20 ] != '/') .
36560 20 20 20 20 7c 7c 20 28 69 2d 73 74 61 72 74 3d || (i-start=
36570 3d 32 20 26 26 20 62 75 66 5b 73 74 61 72 74 5d =2 && buf[start]
36580 20 21 3d 20 27 2e 27 20 26 26 20 62 75 66 5b 73 != '.' && buf[s
36590 74 61 72 74 2b 31 5d 20 21 3d 20 27 2e 27 29 20 tart+1] != '.')
365a0 29 7b 0a 20 20 20 20 20 20 20 20 62 75 66 5b 69 ){. buf[i
365b0 5d 3d 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 ]='\0';.
365c0 69 66 28 20 6f 73 4d 6b 64 69 72 28 62 75 66 2c if( osMkdir(buf,
365d0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
365e0 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 PROXYDIR_PERMISS
365f0 49 4f 4e 53 29 20 29 7b 0a 20 20 20 20 20 20 20 IONS) ){.
36600 20 20 20 69 6e 74 20 65 72 72 3d 65 72 72 6e 6f int err=errno
36610 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
36620 65 72 72 21 3d 45 45 58 49 53 54 20 29 20 7b 0a err!=EEXIST ) {.
36630 20 20 20 20 20 20 20 20 20 20 20 20 4f 53 54 52 OSTR
36640 41 43 45 28 28 22 43 52 45 41 54 45 4c 4f 43 4b ACE(("CREATELOCK
36650 50 41 54 48 20 20 46 41 49 4c 45 44 20 63 72 65 PATH FAILED cre
36660 61 74 69 6e 67 20 25 73 2c 20 22 0a 20 20 20 20 ating %s, ".
36670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
36680 20 22 27 25 73 27 20 70 72 6f 78 79 20 6c 6f 63 "'%s' proxy loc
36690 6b 20 70 61 74 68 3d 25 73 20 70 69 64 3d 25 64 k path=%s pid=%d
366a0 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
366b0 20 20 20 20 20 20 20 20 20 20 62 75 66 2c 20 73 buf, s
366c0 74 72 65 72 72 6f 72 28 65 72 72 29 2c 20 6c 6f trerror(err), lo
366d0 63 6b 50 61 74 68 2c 20 6f 73 47 65 74 70 69 64 ckPath, osGetpid
366e0 28 30 29 29 29 3b 0a 20 20 20 20 20 20 20 20 20 (0)));.
366f0 20 20 20 72 65 74 75 72 6e 20 65 72 72 3b 0a 20 return err;.
36700 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
36710 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
36720 20 20 20 73 74 61 72 74 3d 69 2b 31 3b 0a 20 20 start=i+1;.
36730 20 20 7d 0a 20 20 20 20 62 75 66 5b 69 5d 20 3d }. buf[i] =
36740 20 6c 6f 63 6b 50 61 74 68 5b 69 5d 3b 0a 20 20 lockPath[i];.
36750 7d 0a 20 20 4f 53 54 52 41 43 45 28 28 22 43 52 }. OSTRACE(("CR
36760 45 41 54 45 4c 4f 43 4b 50 41 54 48 20 20 70 72 EATELOCKPATH pr
36770 6f 78 79 20 6c 6f 63 6b 20 70 61 74 68 3d 25 73 oxy lock path=%s
36780 20 70 69 64 3d 25 64 5c 6e 22 2c 6c 6f 63 6b 50 pid=%d\n",lockP
36790 61 74 68 2c 6f 73 47 65 74 70 69 64 28 30 29 29 ath,osGetpid(0))
367a0 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d );. return 0;.}
367b0 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
367c0 20 6e 65 77 20 56 46 53 20 66 69 6c 65 20 64 65 new VFS file de
367d0 73 63 72 69 70 74 6f 72 20 28 73 74 6f 72 65 64 scriptor (stored
367e0 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 in memory obtai
367f0 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 ned from.** sqli
36800 74 65 33 5f 6d 61 6c 6c 6f 63 29 20 61 6e 64 20 te3_malloc) and
36810 6f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 6e 61 open the file na
36820 6d 65 64 20 22 70 61 74 68 22 20 69 6e 20 74 68 med "path" in th
36830 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
36840 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c r..**.** The cal
36850 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 ler is responsib
36860 6c 65 20 6e 6f 74 20 6f 6e 6c 79 20 66 6f 72 20 le not only for
36870 63 6c 6f 73 69 6e 67 20 74 68 65 20 66 69 6c 65 closing the file
36880 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20 62 descriptor.** b
36890 75 74 20 61 6c 73 6f 20 66 6f 72 20 66 72 65 65 ut also for free
368a0 69 6e 67 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 ing the memory a
368b0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
368c0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
368d0 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e or..*/.static in
368e0 74 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e 69 t proxyCreateUni
368f0 78 46 69 6c 65 28 0a 20 20 20 20 63 6f 6e 73 74 xFile(. const
36900 20 63 68 61 72 20 2a 70 61 74 68 2c 20 20 20 20 char *path,
36910 20 20 20 20 2f 2a 20 70 61 74 68 20 66 6f 72 20 /* path for
36920 74 68 65 20 6e 65 77 20 75 6e 69 78 46 69 6c 65 the new unixFile
36930 20 2a 2f 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 */. unixFile
36940 20 2a 2a 70 70 46 69 6c 65 2c 20 20 20 20 20 20 **ppFile,
36950 20 2f 2a 20 75 6e 69 78 46 69 6c 65 20 63 72 65 /* unixFile cre
36960 61 74 65 64 20 61 6e 64 20 72 65 74 75 72 6e 65 ated and returne
36970 64 20 62 79 20 72 65 66 20 2a 2f 0a 20 20 20 20 d by ref */.
36980 69 6e 74 20 69 73 6c 6f 63 6b 66 69 6c 65 20 20 int islockfile
36990 20 20 20 20 20 20 20 20 20 2f 2a 20 69 66 20 6e /* if n
369a0 6f 6e 20 7a 65 72 6f 20 6d 69 73 73 69 6e 67 20 on zero missing
369b0 64 69 72 73 20 77 69 6c 6c 20 62 65 20 63 72 65 dirs will be cre
369c0 61 74 65 64 20 2a 2f 0a 29 20 7b 0a 20 20 69 6e ated */.) {. in
369d0 74 20 66 64 20 3d 20 2d 31 3b 0a 20 20 75 6e 69 t fd = -1;. uni
369e0 78 46 69 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 69 xFile *pNew;. i
369f0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
36a00 4b 3b 0a 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61 K;. int openFla
36a10 67 73 20 3d 20 4f 5f 52 44 57 52 20 7c 20 4f 5f gs = O_RDWR | O_
36a20 43 52 45 41 54 3b 0a 20 20 73 71 6c 69 74 65 33 CREAT;. sqlite3
36a30 5f 76 66 73 20 64 75 6d 6d 79 56 66 73 3b 0a 20 _vfs dummyVfs;.
36a40 20 69 6e 74 20 74 65 72 72 6e 6f 20 3d 20 30 3b int terrno = 0;
36a50 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 . UnixUnusedFd
36a60 2a 70 55 6e 75 73 65 64 20 3d 20 4e 55 4c 4c 3b *pUnused = NULL;
36a70 0a 0a 20 20 2f 2a 20 31 2e 20 66 69 72 73 74 20 .. /* 1. first
36a80 74 72 79 20 74 6f 20 6f 70 65 6e 2f 63 72 65 61 try to open/crea
36a90 74 65 20 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a te the file. **
36aa0 20 32 2e 20 69 66 20 74 68 61 74 20 66 61 69 6c 2. if that fail
36ab0 73 2c 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 s, and this is a
36ac0 20 6c 6f 63 6b 20 66 69 6c 65 20 28 6e 6f 74 2d lock file (not-
36ad0 63 6f 6e 63 68 29 2c 20 74 72 79 20 63 72 65 61 conch), try crea
36ae0 74 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 70 61 ting. ** the pa
36af0 72 65 6e 74 20 64 69 72 65 63 74 6f 72 69 65 73 rent directories
36b00 20 61 6e 64 20 74 68 65 6e 20 74 72 79 20 61 67 and then try ag
36b10 61 69 6e 2e 0a 20 20 2a 2a 20 33 2e 20 69 66 20 ain.. ** 3. if
36b20 74 68 61 74 20 66 61 69 6c 73 2c 20 74 72 79 20 that fails, try
36b30 74 6f 20 6f 70 65 6e 20 74 68 65 20 66 69 6c 65 to open the file
36b40 20 72 65 61 64 2d 6f 6e 6c 79 0a 20 20 2a 2a 20 read-only. **
36b50 6f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 6e otherwise return
36b60 20 42 55 53 59 20 28 69 66 20 6c 6f 63 6b 20 66 BUSY (if lock f
36b70 69 6c 65 29 20 6f 72 20 43 41 4e 54 4f 50 45 4e ile) or CANTOPEN
36b80 20 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 66 for the conch f
36b90 69 6c 65 0a 20 20 2a 2f 0a 20 20 70 55 6e 75 73 ile. */. pUnus
36ba0 65 64 20 3d 20 66 69 6e 64 52 65 75 73 61 62 6c ed = findReusabl
36bb0 65 46 64 28 70 61 74 68 2c 20 6f 70 65 6e 46 6c eFd(path, openFl
36bc0 61 67 73 29 3b 0a 20 20 69 66 28 20 70 55 6e 75 ags);. if( pUnu
36bd0 73 65 64 20 29 7b 0a 20 20 20 20 66 64 20 3d 20 sed ){. fd =
36be0 70 55 6e 75 73 65 64 2d 3e 66 64 3b 0a 20 20 7d pUnused->fd;. }
36bf0 65 6c 73 65 7b 0a 20 20 20 20 70 55 6e 75 73 65 else{. pUnuse
36c00 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c d = sqlite3_mall
36c10 6f 63 36 34 28 73 69 7a 65 6f 66 28 2a 70 55 6e oc64(sizeof(*pUn
36c20 75 73 65 64 29 29 3b 0a 20 20 20 20 69 66 28 20 used));. if(
36c30 21 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 !pUnused ){.
36c40 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
36c50 4e 4f 4d 45 4d 5f 42 4b 50 54 3b 0a 20 20 20 20 NOMEM_BKPT;.
36c60 7d 0a 20 20 7d 0a 20 20 69 66 28 20 66 64 3c 30 }. }. if( fd<0
36c70 20 29 7b 0a 20 20 20 20 66 64 20 3d 20 72 6f 62 ){. fd = rob
36c80 75 73 74 5f 6f 70 65 6e 28 70 61 74 68 2c 20 6f ust_open(path, o
36c90 70 65 6e 46 6c 61 67 73 2c 20 30 29 3b 0a 20 20 penFlags, 0);.
36ca0 20 20 74 65 72 72 6e 6f 20 3d 20 65 72 72 6e 6f terrno = errno
36cb0 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30 20 26 ;. if( fd<0 &
36cc0 26 20 65 72 72 6e 6f 3d 3d 45 4e 4f 45 4e 54 20 & errno==ENOENT
36cd0 26 26 20 69 73 6c 6f 63 6b 66 69 6c 65 20 29 7b && islockfile ){
36ce0 0a 20 20 20 20 20 20 69 66 28 20 70 72 6f 78 79 . if( proxy
36cf0 43 72 65 61 74 65 4c 6f 63 6b 50 61 74 68 28 70 CreateLockPath(p
36d00 61 74 68 29 20 3d 3d 20 53 51 4c 49 54 45 5f 4f ath) == SQLITE_O
36d10 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 66 64 20 K ){. fd
36d20 3d 20 72 6f 62 75 73 74 5f 6f 70 65 6e 28 70 61 = robust_open(pa
36d30 74 68 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20 30 th, openFlags, 0
36d40 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
36d50 0a 20 20 7d 0a 20 20 69 66 28 20 66 64 3c 30 20 . }. if( fd<0
36d60 29 7b 0a 20 20 20 20 6f 70 65 6e 46 6c 61 67 73 ){. openFlags
36d70 20 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20 20 = O_RDONLY;.
36d80 20 66 64 20 3d 20 72 6f 62 75 73 74 5f 6f 70 65 fd = robust_ope
36d90 6e 28 70 61 74 68 2c 20 6f 70 65 6e 46 6c 61 67 n(path, openFlag
36da0 73 2c 20 30 29 3b 0a 20 20 20 20 74 65 72 72 6e s, 0);. terrn
36db0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a 20 o = errno;. }.
36dc0 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 if( fd<0 ){.
36dd0 20 69 66 28 20 69 73 6c 6f 63 6b 66 69 6c 65 20 if( islockfile
36de0 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
36df0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 SQLITE_BUSY;.
36e00 20 7d 0a 20 20 20 20 73 77 69 74 63 68 20 28 74 }. switch (t
36e10 65 72 72 6e 6f 29 20 7b 0a 20 20 20 20 20 20 63 errno) {. c
36e20 61 73 65 20 45 41 43 43 45 53 3a 0a 20 20 20 20 ase EACCES:.
36e30 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
36e40 45 5f 50 45 52 4d 3b 0a 20 20 20 20 20 20 63 61 E_PERM;. ca
36e50 73 65 20 45 49 4f 3a 20 0a 20 20 20 20 20 20 20 se EIO: .
36e60 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
36e70 4f 45 52 52 5f 4c 4f 43 4b 3b 20 2f 2a 20 65 76 OERR_LOCK; /* ev
36e80 65 6e 20 74 68 6f 75 67 68 20 69 74 20 69 73 20 en though it is
36e90 74 68 65 20 63 6f 6e 63 68 20 2a 2f 0a 20 20 20 the conch */.
36ea0 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 default:.
36eb0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
36ec0 45 5f 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 3b E_CANTOPEN_BKPT;
36ed0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 . }. }. .
36ee0 70 4e 65 77 20 3d 20 28 75 6e 69 78 46 69 6c 65 pNew = (unixFile
36ef0 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f *)sqlite3_mallo
36f00 63 36 34 28 73 69 7a 65 6f 66 28 2a 70 4e 65 77 c64(sizeof(*pNew
36f10 29 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d ));. if( pNew==
36f20 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 72 63 20 3d NULL ){. rc =
36f30 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5f 42 4b SQLITE_NOMEM_BK
36f40 50 54 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 PT;. goto end
36f50 5f 63 72 65 61 74 65 5f 70 72 6f 78 79 3b 0a 20 _create_proxy;.
36f60 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 4e 65 77 }. memset(pNew
36f70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 , 0, sizeof(unix
36f80 46 69 6c 65 29 29 3b 0a 20 20 70 4e 65 77 2d 3e File));. pNew->
36f90 6f 70 65 6e 46 6c 61 67 73 20 3d 20 6f 70 65 6e openFlags = open
36fa0 46 6c 61 67 73 3b 0a 20 20 6d 65 6d 73 65 74 28 Flags;. memset(
36fb0 26 64 75 6d 6d 79 56 66 73 2c 20 30 2c 20 73 69 &dummyVfs, 0, si
36fc0 7a 65 6f 66 28 64 75 6d 6d 79 56 66 73 29 29 3b zeof(dummyVfs));
36fd0 0a 20 20 64 75 6d 6d 79 56 66 73 2e 70 41 70 70 . dummyVfs.pApp
36fe0 44 61 74 61 20 3d 20 28 76 6f 69 64 2a 29 26 61 Data = (void*)&a
36ff0 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 3b utolockIoFinder;
37000 0a 20 20 64 75 6d 6d 79 56 66 73 2e 7a 4e 61 6d . dummyVfs.zNam
37010 65 20 3d 20 22 64 75 6d 6d 79 22 3b 0a 20 20 70 e = "dummy";. p
37020 55 6e 75 73 65 64 2d 3e 66 64 20 3d 20 66 64 3b Unused->fd = fd;
37030 0a 20 20 70 55 6e 75 73 65 64 2d 3e 66 6c 61 67 . pUnused->flag
37040 73 20 3d 20 6f 70 65 6e 46 6c 61 67 73 3b 0a 20 s = openFlags;.
37050 20 70 4e 65 77 2d 3e 70 55 6e 75 73 65 64 20 3d pNew->pUnused =
37060 20 70 55 6e 75 73 65 64 3b 0a 20 20 0a 20 20 72 pUnused;. . r
37070 63 20 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 c = fillInUnixFi
37080 6c 65 28 26 64 75 6d 6d 79 56 66 73 2c 20 66 64 le(&dummyVfs, fd
37090 2c 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a , (sqlite3_file*
370a0 29 70 4e 65 77 2c 20 70 61 74 68 2c 20 30 29 3b )pNew, path, 0);
370b0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
370c0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 70 46 E_OK ){. *ppF
370d0 69 6c 65 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 ile = pNew;.
370e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
370f0 3b 0a 20 20 7d 0a 65 6e 64 5f 63 72 65 61 74 65 ;. }.end_create
37100 5f 70 72 6f 78 79 3a 20 20 20 20 0a 20 20 72 6f _proxy: . ro
37110 62 75 73 74 5f 63 6c 6f 73 65 28 70 4e 65 77 2c bust_close(pNew,
37120 20 66 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0a fd, __LINE__);.
37130 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
37140 4e 65 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f New);. sqlite3_
37150 66 72 65 65 28 70 55 6e 75 73 65 64 29 3b 0a 20 free(pUnused);.
37160 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 return rc;.}..#
37170 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
37180 54 0a 2f 2a 20 73 69 6d 75 6c 61 74 65 20 6d 75 T./* simulate mu
37190 6c 74 69 70 6c 65 20 68 6f 73 74 73 20 62 79 20 ltiple hosts by
371a0 63 72 65 61 74 69 6e 67 20 75 6e 69 71 75 65 20 creating unique
371b0 68 6f 73 74 69 64 20 66 69 6c 65 20 70 61 74 68 hostid file path
371c0 73 20 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 s */.int sqlite3
371d0 5f 68 6f 73 74 69 64 5f 6e 75 6d 20 3d 20 30 3b _hostid_num = 0;
371e0 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 .#endif..#define
371f0 20 50 52 4f 58 59 5f 48 4f 53 54 49 44 4c 45 4e PROXY_HOSTIDLEN
37200 20 20 20 20 31 36 20 20 2f 2a 20 63 6f 6e 63 68 16 /* conch
37210 20 66 69 6c 65 20 68 6f 73 74 20 69 64 20 6c 65 file host id le
37220 6e 67 74 68 20 2a 2f 0a 0a 23 69 66 64 65 66 20 ngth */..#ifdef
37230 48 41 56 45 5f 47 45 54 48 4f 53 54 55 55 49 44 HAVE_GETHOSTUUID
37240 0a 2f 2a 20 4e 6f 74 20 61 6c 77 61 79 73 20 64 ./* Not always d
37250 65 66 69 6e 65 64 20 69 6e 20 74 68 65 20 68 65 efined in the he
37260 61 64 65 72 73 20 61 73 20 69 74 20 6f 75 67 68 aders as it ough
37270 74 20 74 6f 20 62 65 20 2a 2f 0a 65 78 74 65 72 t to be */.exter
37280 6e 20 69 6e 74 20 67 65 74 68 6f 73 74 75 75 69 n int gethostuui
37290 64 28 75 75 69 64 5f 74 20 69 64 2c 20 63 6f 6e d(uuid_t id, con
372a0 73 74 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 st struct timesp
372b0 65 63 20 2a 77 61 69 74 29 3b 0a 23 65 6e 64 69 ec *wait);.#endi
372c0 66 0a 0a 2f 2a 20 67 65 74 20 74 68 65 20 68 6f f../* get the ho
372d0 73 74 20 49 44 20 76 69 61 20 67 65 74 68 6f 73 st ID via gethos
372e0 74 75 75 69 64 28 29 2c 20 70 48 6f 73 74 49 44 tuuid(), pHostID
372f0 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 50 must point to P
37300 52 4f 58 59 5f 48 4f 53 54 49 44 4c 45 4e 20 0a ROXY_HOSTIDLEN .
37310 2a 2a 20 62 79 74 65 73 20 6f 66 20 77 72 69 74 ** bytes of writ
37320 61 62 6c 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a able memory..*/.
37330 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
37340 47 65 74 48 6f 73 74 49 44 28 75 6e 73 69 67 6e GetHostID(unsign
37350 65 64 20 63 68 61 72 20 2a 70 48 6f 73 74 49 44 ed char *pHostID
37360 2c 20 69 6e 74 20 2a 70 45 72 72 6f 72 29 7b 0a , int *pError){.
37370 20 20 61 73 73 65 72 74 28 50 52 4f 58 59 5f 48 assert(PROXY_H
37380 4f 53 54 49 44 4c 45 4e 20 3d 3d 20 73 69 7a 65 OSTIDLEN == size
37390 6f 66 28 75 75 69 64 5f 74 29 29 3b 0a 20 20 6d of(uuid_t));. m
373a0 65 6d 73 65 74 28 70 48 6f 73 74 49 44 2c 20 30 emset(pHostID, 0
373b0 2c 20 50 52 4f 58 59 5f 48 4f 53 54 49 44 4c 45 , PROXY_HOSTIDLE
373c0 4e 29 3b 0a 23 69 66 64 65 66 20 48 41 56 45 5f N);.#ifdef HAVE_
373d0 47 45 54 48 4f 53 54 55 55 49 44 0a 20 20 7b 0a GETHOSTUUID. {.
373e0 20 20 20 20 73 74 72 75 63 74 20 74 69 6d 65 73 struct times
373f0 70 65 63 20 74 69 6d 65 6f 75 74 20 3d 20 7b 31 pec timeout = {1
37400 2c 20 30 7d 3b 20 2f 2a 20 31 20 73 65 63 20 74 , 0}; /* 1 sec t
37410 69 6d 65 6f 75 74 20 2a 2f 0a 20 20 20 20 69 66 imeout */. if
37420 28 20 67 65 74 68 6f 73 74 75 75 69 64 28 70 48 ( gethostuuid(pH
37430 6f 73 74 49 44 2c 20 26 74 69 6d 65 6f 75 74 29 ostID, &timeout)
37440 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 72 ){. int er
37450 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 r = errno;.
37460 20 69 66 28 20 70 45 72 72 6f 72 20 29 7b 0a 20 if( pError ){.
37470 20 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d *pError =
37480 20 65 72 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 err;. }.
37490 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
374a0 45 5f 49 4f 45 52 52 3b 0a 20 20 20 20 7d 0a 20 E_IOERR;. }.
374b0 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 }.#else. UNUSE
374c0 44 5f 50 41 52 41 4d 45 54 45 52 28 70 45 72 72 D_PARAMETER(pErr
374d0 6f 72 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 or);.#endif.#ifd
374e0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 ef SQLITE_TEST.
374f0 20 2f 2a 20 73 69 6d 75 6c 61 74 65 20 6d 75 6c /* simulate mul
37500 74 69 70 6c 65 20 68 6f 73 74 73 20 62 79 20 63 tiple hosts by c
37510 72 65 61 74 69 6e 67 20 75 6e 69 71 75 65 20 68 reating unique h
37520 6f 73 74 69 64 20 66 69 6c 65 20 70 61 74 68 73 ostid file paths
37530 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 */. if( sqlite
37540 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 20 21 3d 20 3_hostid_num !=
37550 30 29 7b 0a 20 20 20 20 70 48 6f 73 74 49 44 5b 0){. pHostID[
37560 30 5d 20 3d 20 28 63 68 61 72 29 28 70 48 6f 73 0] = (char)(pHos
37570 74 49 44 5b 30 5d 20 2b 20 28 63 68 61 72 29 28 tID[0] + (char)(
37580 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e sqlite3_hostid_n
37590 75 6d 20 26 20 30 78 46 46 29 29 3b 0a 20 20 7d um & 0xFF));. }
375a0 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 72 65 74 .#endif. . ret
375b0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
375c0 0a 0a 2f 2a 20 54 68 65 20 63 6f 6e 63 68 20 66 ../* The conch f
375d0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ile contains the
375e0 20 68 65 61 64 65 72 2c 20 68 6f 73 74 20 69 64 header, host id
375f0 20 61 6e 64 20 6c 6f 63 6b 20 66 69 6c 65 20 70 and lock file p
37600 61 74 68 0a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ath. */.#define
37610 50 52 4f 58 59 5f 43 4f 4e 43 48 56 45 52 53 49 PROXY_CONCHVERSI
37620 4f 4e 20 32 20 20 20 2f 2a 20 31 2d 62 79 74 65 ON 2 /* 1-byte
37630 20 68 65 61 64 65 72 2c 20 31 36 2d 62 79 74 65 header, 16-byte
37640 20 68 6f 73 74 20 69 64 2c 20 70 61 74 68 20 2a host id, path *
37650 2f 0a 23 64 65 66 69 6e 65 20 50 52 4f 58 59 5f /.#define PROXY_
37660 48 45 41 44 45 52 4c 45 4e 20 20 20 20 31 20 20 HEADERLEN 1
37670 20 2f 2a 20 63 6f 6e 63 68 20 66 69 6c 65 20 68 /* conch file h
37680 65 61 64 65 72 20 6c 65 6e 67 74 68 20 2a 2f 0a eader length */.
37690 23 64 65 66 69 6e 65 20 50 52 4f 58 59 5f 50 41 #define PROXY_PA
376a0 54 48 49 4e 44 45 58 20 20 20 20 28 50 52 4f 58 THINDEX (PROX
376b0 59 5f 48 45 41 44 45 52 4c 45 4e 2b 50 52 4f 58 Y_HEADERLEN+PROX
376c0 59 5f 48 4f 53 54 49 44 4c 45 4e 29 0a 23 64 65 Y_HOSTIDLEN).#de
376d0 66 69 6e 65 20 50 52 4f 58 59 5f 4d 41 58 43 4f fine PROXY_MAXCO
376e0 4e 43 48 4c 45 4e 20 20 28 50 52 4f 58 59 5f 48 NCHLEN (PROXY_H
376f0 45 41 44 45 52 4c 45 4e 2b 50 52 4f 58 59 5f 48 EADERLEN+PROXY_H
37700 4f 53 54 49 44 4c 45 4e 2b 4d 41 58 50 41 54 48 OSTIDLEN+MAXPATH
37710 4c 45 4e 29 0a 0a 2f 2a 20 0a 2a 2a 20 54 61 6b LEN)../* .** Tak
37720 65 73 20 61 6e 20 6f 70 65 6e 20 63 6f 6e 63 68 es an open conch
37730 20 66 69 6c 65 2c 20 63 6f 70 69 65 73 20 74 68 file, copies th
37740 65 20 63 6f 6e 74 65 6e 74 73 20 74 6f 20 61 20 e contents to a
37750 6e 65 77 20 70 61 74 68 20 61 6e 64 20 74 68 65 new path and the
37760 6e 20 6d 6f 76 65 73 20 0a 2a 2a 20 69 74 20 62 n moves .** it b
37770 61 63 6b 2e 20 20 54 68 65 20 6e 65 77 6c 79 20 ack. The newly
37780 63 72 65 61 74 65 64 20 66 69 6c 65 27 73 20 66 created file's f
37790 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 ile descriptor i
377a0 73 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 s assigned to th
377b0 65 0a 2a 2a 20 63 6f 6e 63 68 20 66 69 6c 65 20 e.** conch file
377c0 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 66 69 structure and fi
377d0 6e 61 6c 6c 79 20 74 68 65 20 6f 72 69 67 69 6e nally the origin
377e0 61 6c 20 63 6f 6e 63 68 20 66 69 6c 65 20 64 65 al conch file de
377f0 73 63 72 69 70 74 6f 72 20 69 73 20 0a 2a 2a 20 scriptor is .**
37800 63 6c 6f 73 65 64 2e 20 20 52 65 74 75 72 6e 73 closed. Returns
37810 20 7a 65 72 6f 20 69 66 20 73 75 63 63 65 73 73 zero if success
37820 66 75 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ful..*/.static i
37830 6e 74 20 70 72 6f 78 79 42 72 65 61 6b 43 6f 6e nt proxyBreakCon
37840 63 68 4c 6f 63 6b 28 75 6e 69 78 46 69 6c 65 20 chLock(unixFile
37850 2a 70 46 69 6c 65 2c 20 75 75 69 64 5f 74 20 6d *pFile, uuid_t m
37860 79 48 6f 73 74 49 44 29 7b 0a 20 20 70 72 6f 78 yHostID){. prox
37870 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
37880 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f *pCtx = (proxyLo
37890 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 ckingContext *)p
378a0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
378b0 74 65 78 74 3b 20 0a 20 20 75 6e 69 78 46 69 6c text; . unixFil
378c0 65 20 2a 63 6f 6e 63 68 46 69 6c 65 20 3d 20 70 e *conchFile = p
378d0 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a Ctx->conchFile;.
378e0 20 20 63 68 61 72 20 74 50 61 74 68 5b 4d 41 58 char tPath[MAX
378f0 50 41 54 48 4c 45 4e 5d 3b 0a 20 20 63 68 61 72 PATHLEN];. char
37900 20 62 75 66 5b 50 52 4f 58 59 5f 4d 41 58 43 4f buf[PROXY_MAXCO
37910 4e 43 48 4c 45 4e 5d 3b 0a 20 20 63 68 61 72 20 NCHLEN];. char
37920 2a 63 50 61 74 68 20 3d 20 70 43 74 78 2d 3e 63 *cPath = pCtx->c
37930 6f 6e 63 68 46 69 6c 65 50 61 74 68 3b 0a 20 20 onchFilePath;.
37940 73 69 7a 65 5f 74 20 72 65 61 64 4c 65 6e 20 3d size_t readLen =
37950 20 30 3b 0a 20 20 73 69 7a 65 5f 74 20 70 61 74 0;. size_t pat
37960 68 4c 65 6e 20 3d 20 30 3b 0a 20 20 63 68 61 72 hLen = 0;. char
37970 20 65 72 72 6d 73 67 5b 36 34 5d 20 3d 20 22 22 errmsg[64] = ""
37980 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d 31 3b ;. int fd = -1;
37990 0a 20 20 69 6e 74 20 72 63 20 3d 20 2d 31 3b 0a . int rc = -1;.
379a0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
379b0 45 52 28 6d 79 48 6f 73 74 49 44 29 3b 0a 0a 20 ER(myHostID);..
379c0 20 2f 2a 20 63 72 65 61 74 65 20 61 20 6e 65 77 /* create a new
379d0 20 70 61 74 68 20 62 79 20 72 65 70 6c 61 63 65 path by replace
379e0 20 74 68 65 20 74 72 61 69 6c 69 6e 67 20 27 2d the trailing '-
379f0 63 6f 6e 63 68 27 20 77 69 74 68 20 27 2d 62 72 conch' with '-br
37a00 65 61 6b 27 20 2a 2f 0a 20 20 70 61 74 68 4c 65 eak' */. pathLe
37a10 6e 20 3d 20 73 74 72 6c 63 70 79 28 74 50 61 74 n = strlcpy(tPat
37a20 68 2c 20 63 50 61 74 68 2c 20 4d 41 58 50 41 54 h, cPath, MAXPAT
37a30 48 4c 45 4e 29 3b 0a 20 20 69 66 28 20 70 61 74 HLEN);. if( pat
37a40 68 4c 65 6e 3e 4d 41 58 50 41 54 48 4c 45 4e 20 hLen>MAXPATHLEN
37a50 7c 7c 20 70 61 74 68 4c 65 6e 3c 36 20 7c 7c 20 || pathLen<6 ||
37a60 0a 20 20 20 20 20 28 73 74 72 6c 63 70 79 28 26 . (strlcpy(&
37a70 74 50 61 74 68 5b 70 61 74 68 4c 65 6e 2d 35 5d tPath[pathLen-5]
37a80 2c 20 22 62 72 65 61 6b 22 2c 20 36 29 20 21 3d , "break", 6) !=
37a90 20 35 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 5) ){. sqlit
37aa0 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 e3_snprintf(size
37ab0 6f 66 28 65 72 72 6d 73 67 29 2c 65 72 72 6d 73 of(errmsg),errms
37ac0 67 2c 22 70 61 74 68 20 65 72 72 6f 72 20 28 6c g,"path error (l
37ad0 65 6e 20 25 64 29 22 2c 28 69 6e 74 29 70 61 74 en %d)",(int)pat
37ae0 68 4c 65 6e 29 3b 0a 20 20 20 20 67 6f 74 6f 20 hLen);. goto
37af0 65 6e 64 5f 62 72 65 61 6b 6c 6f 63 6b 3b 0a 20 end_breaklock;.
37b00 20 7d 0a 20 20 2f 2a 20 72 65 61 64 20 74 68 65 }. /* read the
37b10 20 63 6f 6e 63 68 20 63 6f 6e 74 65 6e 74 20 2a conch content *
37b20 2f 0a 20 20 72 65 61 64 4c 65 6e 20 3d 20 6f 73 /. readLen = os
37b30 50 72 65 61 64 28 63 6f 6e 63 68 46 69 6c 65 2d Pread(conchFile-
37b40 3e 68 2c 20 62 75 66 2c 20 50 52 4f 58 59 5f 4d >h, buf, PROXY_M
37b50 41 58 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a AXCONCHLEN, 0);.
37b60 20 20 69 66 28 20 72 65 61 64 4c 65 6e 3c 50 52 if( readLen<PR
37b70 4f 58 59 5f 50 41 54 48 49 4e 44 45 58 20 29 7b OXY_PATHINDEX ){
37b80 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
37b90 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 65 72 72 rintf(sizeof(err
37ba0 6d 73 67 29 2c 65 72 72 6d 73 67 2c 22 72 65 61 msg),errmsg,"rea
37bb0 64 20 65 72 72 6f 72 20 28 6c 65 6e 20 25 64 29 d error (len %d)
37bc0 22 2c 28 69 6e 74 29 72 65 61 64 4c 65 6e 29 3b ",(int)readLen);
37bd0 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 62 72 . goto end_br
37be0 65 61 6b 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 2f eaklock;. }. /
37bf0 2a 20 77 72 69 74 65 20 69 74 20 6f 75 74 20 74 * write it out t
37c00 6f 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 o the temporary
37c10 62 72 65 61 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 break file */.
37c20 66 64 20 3d 20 72 6f 62 75 73 74 5f 6f 70 65 6e fd = robust_open
37c30 28 74 50 61 74 68 2c 20 28 4f 5f 52 44 57 52 7c (tPath, (O_RDWR|
37c40 4f 5f 43 52 45 41 54 7c 4f 5f 45 58 43 4c 29 2c O_CREAT|O_EXCL),
37c50 20 30 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 0);. if( fd<0
37c60 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 ){. sqlite3_s
37c70 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 65 nprintf(sizeof(e
37c80 72 72 6d 73 67 29 2c 20 65 72 72 6d 73 67 2c 20 rrmsg), errmsg,
37c90 22 63 72 65 61 74 65 20 66 61 69 6c 65 64 20 28 "create failed (
37ca0 25 64 29 22 2c 20 65 72 72 6e 6f 29 3b 0a 20 20 %d)", errno);.
37cb0 20 20 67 6f 74 6f 20 65 6e 64 5f 62 72 65 61 6b goto end_break
37cc0 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 lock;. }. if(
37cd0 6f 73 50 77 72 69 74 65 28 66 64 2c 20 62 75 66 osPwrite(fd, buf
37ce0 2c 20 72 65 61 64 4c 65 6e 2c 20 30 29 20 21 3d , readLen, 0) !=
37cf0 20 28 73 73 69 7a 65 5f 74 29 72 65 61 64 4c 65 (ssize_t)readLe
37d00 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 n ){. sqlite3
37d10 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 _snprintf(sizeof
37d20 28 65 72 72 6d 73 67 29 2c 20 65 72 72 6d 73 67 (errmsg), errmsg
37d30 2c 20 22 77 72 69 74 65 20 66 61 69 6c 65 64 20 , "write failed
37d40 28 25 64 29 22 2c 20 65 72 72 6e 6f 29 3b 0a 20 (%d)", errno);.
37d50 20 20 20 67 6f 74 6f 20 65 6e 64 5f 62 72 65 61 goto end_brea
37d60 6b 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 klock;. }. if(
37d70 20 72 65 6e 61 6d 65 28 74 50 61 74 68 2c 20 63 rename(tPath, c
37d80 50 61 74 68 29 20 29 7b 0a 20 20 20 20 73 71 6c Path) ){. sql
37d90 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 ite3_snprintf(si
37da0 7a 65 6f 66 28 65 72 72 6d 73 67 29 2c 20 65 72 zeof(errmsg), er
37db0 72 6d 73 67 2c 20 22 72 65 6e 61 6d 65 20 66 61 rmsg, "rename fa
37dc0 69 6c 65 64 20 28 25 64 29 22 2c 20 65 72 72 6e iled (%d)", errn
37dd0 6f 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 o);. goto end
37de0 5f 62 72 65 61 6b 6c 6f 63 6b 3b 0a 20 20 7d 0a _breaklock;. }.
37df0 20 20 72 63 20 3d 20 30 3b 0a 20 20 66 70 72 69 rc = 0;. fpri
37e00 6e 74 66 28 73 74 64 65 72 72 2c 20 22 62 72 6f ntf(stderr, "bro
37e10 6b 65 20 73 74 61 6c 65 20 6c 6f 63 6b 20 6f 6e ke stale lock on
37e20 20 25 73 5c 6e 22 2c 20 63 50 61 74 68 29 3b 0a %s\n", cPath);.
37e30 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 70 robust_close(p
37e40 46 69 6c 65 2c 20 63 6f 6e 63 68 46 69 6c 65 2d File, conchFile-
37e50 3e 68 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0a 20 >h, __LINE__);.
37e60 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 20 3d 20 conchFile->h =
37e70 66 64 3b 0a 20 20 63 6f 6e 63 68 46 69 6c 65 2d fd;. conchFile-
37e80 3e 6f 70 65 6e 46 6c 61 67 73 20 3d 20 4f 5f 52 >openFlags = O_R
37e90 44 57 52 20 7c 20 4f 5f 43 52 45 41 54 3b 0a 0a DWR | O_CREAT;..
37ea0 65 6e 64 5f 62 72 65 61 6b 6c 6f 63 6b 3a 0a 20 end_breaklock:.
37eb0 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 69 if( rc ){. i
37ec0 66 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 f( fd>=0 ){.
37ed0 20 20 6f 73 55 6e 6c 69 6e 6b 28 74 50 61 74 68 osUnlink(tPath
37ee0 29 3b 0a 20 20 20 20 20 20 72 6f 62 75 73 74 5f );. robust_
37ef0 63 6c 6f 73 65 28 70 46 69 6c 65 2c 20 66 64 2c close(pFile, fd,
37f00 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0a 20 20 20 20 __LINE__);.
37f10 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 }. fprintf(st
37f20 64 65 72 72 2c 20 22 66 61 69 6c 65 64 20 74 6f derr, "failed to
37f30 20 62 72 65 61 6b 20 73 74 61 6c 65 20 6c 6f 63 break stale loc
37f40 6b 20 6f 6e 20 25 73 2c 20 25 73 5c 6e 22 2c 20 k on %s, %s\n",
37f50 63 50 61 74 68 2c 20 65 72 72 6d 73 67 29 3b 0a cPath, errmsg);.
37f60 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
37f70 0a 7d 0a 0a 2f 2a 20 54 61 6b 65 20 74 68 65 20 .}../* Take the
37f80 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 6f requested lock o
37f90 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 n the conch file
37fa0 20 61 6e 64 20 62 72 65 61 6b 20 61 20 73 74 61 and break a sta
37fb0 6c 65 20 6c 6f 63 6b 20 69 66 20 74 68 65 20 0a le lock if the .
37fc0 2a 2a 20 68 6f 73 74 20 69 64 20 6d 61 74 63 68 ** host id match
37fd0 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e es..*/.static in
37fe0 74 20 70 72 6f 78 79 43 6f 6e 63 68 4c 6f 63 6b t proxyConchLock
37ff0 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 (unixFile *pFile
38000 2c 20 75 75 69 64 5f 74 20 6d 79 48 6f 73 74 49 , uuid_t myHostI
38010 44 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29 D, int lockType)
38020 7b 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 {. proxyLocking
38030 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 Context *pCtx =
38040 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e (proxyLockingCon
38050 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f text *)pFile->lo
38060 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 0a 20 ckingContext; .
38070 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 unixFile *conch
38080 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 6f 6e File = pCtx->con
38090 63 68 46 69 6c 65 3b 0a 20 20 69 6e 74 20 72 63 chFile;. int rc
380a0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
380b0 69 6e 74 20 6e 54 72 69 65 73 20 3d 20 30 3b 0a int nTries = 0;.
380c0 20 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 struct timespe
380d0 63 20 63 6f 6e 63 68 4d 6f 64 54 69 6d 65 3b 0a c conchModTime;.
380e0 20 20 0a 20 20 6d 65 6d 73 65 74 28 26 63 6f 6e . memset(&con
380f0 63 68 4d 6f 64 54 69 6d 65 2c 20 30 2c 20 73 69 chModTime, 0, si
38100 7a 65 6f 66 28 63 6f 6e 63 68 4d 6f 64 54 69 6d zeof(conchModTim
38110 65 29 29 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 20 e));. do {.
38120 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e rc = conchFile->
38130 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 pMethod->xLock((
38140 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f sqlite3_file*)co
38150 6e 63 68 46 69 6c 65 2c 20 6c 6f 63 6b 54 79 70 nchFile, lockTyp
38160 65 29 3b 0a 20 20 20 20 6e 54 72 69 65 73 20 2b e);. nTries +
38170 2b 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 +;. if( rc==S
38180 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 QLITE_BUSY ){.
38190 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6c 6f /* If the lo
381a0 63 6b 20 66 61 69 6c 65 64 20 28 62 75 73 79 29 ck failed (busy)
381b0 3a 0a 20 20 20 20 20 20 20 2a 20 31 73 74 20 74 :. * 1st t
381c0 72 79 3a 20 67 65 74 20 74 68 65 20 6d 6f 64 20 ry: get the mod
381d0 74 69 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 63 time of the conc
381e0 68 2c 20 77 61 69 74 20 30 2e 35 73 20 61 6e 64 h, wait 0.5s and
381f0 20 74 72 79 20 61 67 61 69 6e 2e 20 0a 20 20 20 try again. .
38200 20 20 20 20 2a 20 32 6e 64 20 74 72 79 3a 20 66 * 2nd try: f
38210 61 69 6c 20 69 66 20 74 68 65 20 6d 6f 64 20 74 ail if the mod t
38220 69 6d 65 20 63 68 61 6e 67 65 64 20 6f 72 20 68 ime changed or h
38230 6f 73 74 20 69 64 20 69 73 20 64 69 66 66 65 72 ost id is differ
38240 65 6e 74 2c 20 77 61 69 74 20 0a 20 20 20 20 20 ent, wait .
38250 20 20 2a 20 20 20 20 20 20 20 20 20 20 20 31 30 * 10
38260 20 73 65 63 20 61 6e 64 20 74 72 79 20 61 67 61 sec and try aga
38270 69 6e 0a 20 20 20 20 20 20 20 2a 20 33 72 64 20 in. * 3rd
38280 74 72 79 3a 20 62 72 65 61 6b 20 74 68 65 20 6c try: break the l
38290 6f 63 6b 20 75 6e 6c 65 73 73 20 74 68 65 20 6d ock unless the m
382a0 6f 64 20 74 69 6d 65 20 68 61 73 20 63 68 61 6e od time has chan
382b0 67 65 64 2e 0a 20 20 20 20 20 20 20 2a 2f 0a 20 ged.. */.
382c0 20 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74 struct stat
382d0 20 62 75 66 3b 0a 20 20 20 20 20 20 69 66 28 20 buf;. if(
382e0 6f 73 46 73 74 61 74 28 63 6f 6e 63 68 46 69 6c osFstat(conchFil
382f0 65 2d 3e 68 2c 20 26 62 75 66 29 20 29 7b 0a 20 e->h, &buf) ){.
38300 20 20 20 20 20 20 20 73 74 6f 72 65 4c 61 73 74 storeLast
38310 45 72 72 6e 6f 28 70 46 69 6c 65 2c 20 65 72 72 Errno(pFile, err
38320 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 no);. ret
38330 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
38340 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 _LOCK;. }.
38350 20 20 20 20 20 0a 20 20 20 20 20 20 69 66 28 20 . if(
38360 6e 54 72 69 65 73 3d 3d 31 20 29 7b 0a 20 20 20 nTries==1 ){.
38370 20 20 20 20 20 63 6f 6e 63 68 4d 6f 64 54 69 6d conchModTim
38380 65 20 3d 20 62 75 66 2e 73 74 5f 6d 74 69 6d 65 e = buf.st_mtime
38390 73 70 65 63 3b 0a 20 20 20 20 20 20 20 20 75 73 spec;. us
383a0 6c 65 65 70 28 35 30 30 30 30 30 29 3b 20 2f 2a leep(500000); /*
383b0 20 77 61 69 74 20 30 2e 35 20 73 65 63 20 61 6e wait 0.5 sec an
383c0 64 20 74 72 79 20 74 68 65 20 6c 6f 63 6b 20 61 d try the lock a
383d0 67 61 69 6e 2a 2f 0a 20 20 20 20 20 20 20 20 63 gain*/. c
383e0 6f 6e 74 69 6e 75 65 3b 20 20 0a 20 20 20 20 20 ontinue; .
383f0 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 }.. assert
38400 28 20 6e 54 72 69 65 73 3e 31 20 29 3b 0a 20 20 ( nTries>1 );.
38410 20 20 20 20 69 66 28 20 63 6f 6e 63 68 4d 6f 64 if( conchMod
38420 54 69 6d 65 2e 74 76 5f 73 65 63 20 21 3d 20 62 Time.tv_sec != b
38430 75 66 2e 73 74 5f 6d 74 69 6d 65 73 70 65 63 2e uf.st_mtimespec.
38440 74 76 5f 73 65 63 20 7c 7c 20 0a 20 20 20 20 20 tv_sec || .
38450 20 20 20 20 63 6f 6e 63 68 4d 6f 64 54 69 6d 65 conchModTime
38460 2e 74 76 5f 6e 73 65 63 20 21 3d 20 62 75 66 2e .tv_nsec != buf.
38470 73 74 5f 6d 74 69 6d 65 73 70 65 63 2e 74 76 5f st_mtimespec.tv_
38480 6e 73 65 63 20 29 7b 0a 20 20 20 20 20 20 20 20 nsec ){.
38490 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 return SQLITE_BU
384a0 53 59 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 SY;. }.
384b0 20 20 0a 20 20 20 20 20 20 69 66 28 20 6e 54 72 . if( nTr
384c0 69 65 73 3d 3d 32 20 29 7b 20 20 0a 20 20 20 20 ies==2 ){ .
384d0 20 20 20 20 63 68 61 72 20 74 42 75 66 5b 50 52 char tBuf[PR
384e0 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c 45 4e 5d OXY_MAXCONCHLEN]
384f0 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c 65 ;. int le
38500 6e 20 3d 20 6f 73 50 72 65 61 64 28 63 6f 6e 63 n = osPread(conc
38510 68 46 69 6c 65 2d 3e 68 2c 20 74 42 75 66 2c 20 hFile->h, tBuf,
38520 50 52 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c 45 PROXY_MAXCONCHLE
38530 4e 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 N, 0);. i
38540 66 28 20 6c 65 6e 3c 30 20 29 7b 0