0000: 2f 2a 0a 2a 2a 20 32 30 30 35 20 4e 6f 76 65 6d /*.** 2005 Novem
0010: 62 65 72 20 32 39 0a 2a 2a 0a 2a 2a 20 54 68 65 ber 29.**.** The
0020: 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
0030: 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
0040: 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
0050: 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
0060: 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
0070: 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
0080: 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
0090: 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
00a0: 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
00b0: 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
00c0: 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
00d0: 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
00e0: 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
00f0: 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
0100: 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
0110: 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
0120: 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0170: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
0180: 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
0190: 69 6e 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65 ins OS interface
01a0: 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f code that is co
01b0: 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 61 mmon to all.** a
01c0: 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a 2a 2f rchitectures..*/
01d0: 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 .#define _SQLITE
01e0: 5f 4f 53 5f 43 5f 20 31 0a 23 69 6e 63 6c 75 64 _OS_C_ 1.#includ
01f0: 65 20 22 73 71 6c 69 74 65 49 6e 74 2e 68 22 0a e "sqliteInt.h".
0200: 23 75 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f #undef _SQLITE_O
0210: 53 5f 43 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 S_C_../*.** The
0220: 64 65 66 61 75 6c 74 20 53 51 4c 69 74 65 20 73 default SQLite s
0230: 71 6c 69 74 65 33 5f 76 66 73 20 69 6d 70 6c 65 qlite3_vfs imple
0240: 6d 65 6e 74 61 74 69 6f 6e 73 20 64 6f 20 6e 6f mentations do no
0250: 74 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 6d 65 t allocate.** me
0260: 6d 6f 72 79 20 28 61 63 74 75 61 6c 6c 79 2c 20 mory (actually,
0270: 6f 73 5f 75 6e 69 78 2e 63 20 61 6c 6c 6f 63 61 os_unix.c alloca
0280: 74 65 73 20 61 20 73 6d 61 6c 6c 20 61 6d 6f 75 tes a small amou
0290: 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 nt of memory.**
02a0: 66 72 6f 6d 20 77 69 74 68 69 6e 20 4f 73 4f 70 from within OsOp
02b0: 65 6e 28 29 29 2c 20 62 75 74 20 73 6f 6d 65 20 en()), but some
02c0: 74 68 69 72 64 2d 70 61 72 74 79 20 69 6d 70 6c third-party impl
02d0: 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6d 61 79 2e ementations may.
02e0: 0a 2a 2a 20 53 6f 20 77 65 20 74 65 73 74 20 74 .** So we test t
02f0: 68 65 20 65 66 66 65 63 74 73 20 6f 66 20 61 20 he effects of a
0300: 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 69 6e 67 malloc() failing
0310: 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 and the sqlite3
0320: 4f 73 58 58 58 28 29 0a 2a 2a 20 66 75 6e 63 74 OsXXX().** funct
0330: 69 6f 6e 20 72 65 74 75 72 6e 69 6e 67 20 53 51 ion returning SQ
0340: 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d LITE_IOERR_NOMEM
0350: 20 75 73 69 6e 67 20 74 68 65 20 44 4f 5f 4f 53 using the DO_OS
0360: 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 20 6d 61 63 _MALLOC_TEST mac
0370: 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f ro..**.** The fo
0380: 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e llowing function
0390: 73 20 61 72 65 20 69 6e 73 74 72 75 6d 65 6e 74 s are instrument
03a0: 65 64 20 66 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 ed for malloc()
03b0: 66 61 69 6c 75 72 65 20 0a 2a 2a 20 74 65 73 74 failure .** test
03c0: 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 ing:.**.** s
03d0: 71 6c 69 74 65 33 4f 73 52 65 61 64 28 29 0a 2a qlite3OsRead().*
03e0: 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 57 * sqlite3OsW
03f0: 72 69 74 65 28 29 0a 2a 2a 20 20 20 20 20 73 71 rite().** sq
0400: 6c 69 74 65 33 4f 73 53 79 6e 63 28 29 0a 2a 2a lite3OsSync().**
0410: 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69 sqlite3OsFi
0420: 6c 65 53 69 7a 65 28 29 0a 2a 2a 20 20 20 20 20 leSize().**
0430: 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 0a sqlite3OsLock().
0440: 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 ** sqlite3Os
0450: 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
0460: 6b 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 k().** sqlit
0470: 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 e3OsFileControl(
0480: 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 ).** sqlite3
0490: 4f 73 53 68 6d 4d 61 70 28 29 0a 2a 2a 20 20 20 OsShmMap().**
04a0: 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 sqlite3OsOpen(
04b0: 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 ).** sqlite3
04c0: 4f 73 44 65 6c 65 74 65 28 29 0a 2a 2a 20 20 20 OsDelete().**
04d0: 20 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 sqlite3OsAcces
04e0: 73 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 s().** sqlit
04f0: 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 e3OsFullPathname
0500: 28 29 0a 2a 2a 0a 2a 2f 0a 23 69 66 20 64 65 66 ().**.*/.#if def
0510: 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
0520: 29 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 ).int sqlite3_me
0530: 6d 64 65 62 75 67 5f 76 66 73 5f 6f 6f 6d 5f 74 mdebug_vfs_oom_t
0540: 65 73 74 20 3d 20 31 3b 0a 20 20 23 64 65 66 69 est = 1;. #defi
0550: 6e 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f ne DO_OS_MALLOC_
0560: 54 45 53 54 28 78 29 20 20 20 20 20 20 20 20 20 TEST(x)
0570: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0580: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
0590: 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 6d 65 if (sqlite3_me
05a0: 6d 64 65 62 75 67 5f 76 66 73 5f 6f 6f 6d 5f 74 mdebug_vfs_oom_t
05b0: 65 73 74 20 26 26 20 28 21 78 20 7c 7c 20 21 73 est && (!x || !s
05c0: 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e qlite3IsMemJourn
05d0: 61 6c 28 78 29 29 29 20 7b 20 20 5c 0a 20 20 20 al(x))) { \.
05e0: 20 76 6f 69 64 20 2a 70 54 73 74 41 6c 6c 6f 63 void *pTstAlloc
05f0: 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
0600: 28 31 30 29 3b 20 20 20 20 20 20 20 20 20 20 20 (10);
0610: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0620: 20 20 5c 0a 20 20 20 20 69 66 20 28 21 70 54 73 \. if (!pTs
0630: 74 41 6c 6c 6f 63 29 20 72 65 74 75 72 6e 20 53 tAlloc) return S
0640: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 QLITE_IOERR_NOME
0650: 4d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 M;
0660: 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 73 \. s
0670: 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 73 74 qlite3_free(pTst
0680: 41 6c 6c 6f 63 29 3b 20 20 20 20 20 20 20 20 20 Alloc);
0690: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
06a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
06b0: 5c 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 23 64 \. }.#else. #d
06c0: 65 66 69 6e 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c efine DO_OS_MALL
06d0: 4f 43 5f 54 45 53 54 28 78 29 0a 23 65 6e 64 69 OC_TEST(x).#endi
06e0: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c f../*.** The fol
06f0: 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 lowing routines
0700: 61 72 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 are convenience
0710: 77 72 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20 wrappers around
0720: 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 68 methods.** of th
0730: 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f e sqlite3_file o
0740: 62 6a 65 63 74 2e 20 20 54 68 69 73 20 69 73 20 bject. This is
0750: 6d 6f 73 74 6c 79 20 6a 75 73 74 20 73 79 6e 74 mostly just synt
0760: 61 63 74 69 63 20 73 75 67 61 72 2e 20 41 6c 6c actic sugar. All
0770: 0a 2a 2a 20 6f 66 20 74 68 69 73 20 77 6f 75 6c .** of this woul
0780: 64 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 d be completely
0790: 61 75 74 6f 6d 61 74 69 63 20 69 66 20 53 51 4c automatic if SQL
07a0: 69 74 65 20 77 65 72 65 20 63 6f 64 65 64 20 75 ite were coded u
07b0: 73 69 6e 67 0a 2a 2a 20 43 2b 2b 20 69 6e 73 74 sing.** C++ inst
07c0: 65 61 64 20 6f 66 20 70 6c 61 69 6e 20 6f 6c 64 ead of plain old
07d0: 20 43 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 C..*/.int sqlit
07e0: 65 33 4f 73 43 6c 6f 73 65 28 73 71 6c 69 74 65 e3OsClose(sqlite
07f0: 33 5f 66 69 6c 65 20 2a 70 49 64 29 7b 0a 20 20 3_file *pId){.
0800: 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
0810: 4f 4b 3b 0a 20 20 69 66 28 20 70 49 64 2d 3e 70 OK;. if( pId->p
0820: 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 72 Methods ){. r
0830: 63 20 3d 20 70 49 64 2d 3e 70 4d 65 74 68 6f 64 c = pId->pMethod
0840: 73 2d 3e 78 43 6c 6f 73 65 28 70 49 64 29 3b 0a s->xClose(pId);.
0850: 20 20 20 20 70 49 64 2d 3e 70 4d 65 74 68 6f 64 pId->pMethod
0860: 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 s = 0;. }. ret
0870: 75 72 6e 20 72 63 3b 0a 7d 0a 69 6e 74 20 73 71 urn rc;.}.int sq
0880: 6c 69 74 65 33 4f 73 52 65 61 64 28 73 71 6c 69 lite3OsRead(sqli
0890: 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 76 6f te3_file *id, vo
08a0: 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d id *pBuf, int am
08b0: 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b 0a t, i64 offset){.
08c0: 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 DO_OS_MALLOC_T
08d0: 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 EST(id);. retur
08e0: 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e n id->pMethods->
08f0: 78 52 65 61 64 28 69 64 2c 20 70 42 75 66 2c 20 xRead(id, pBuf,
0900: 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a amt, offset);.}.
0910: 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69 int sqlite3OsWri
0920: 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 te(sqlite3_file
0930: 2a 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 *id, const void
0940: 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 *pBuf, int amt,
0950: 69 36 34 20 6f 66 66 73 65 74 29 7b 0a 20 20 44 i64 offset){. D
0960: 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
0970: 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 (id);. return i
0980: 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 57 72 d->pMethods->xWr
0990: 69 74 65 28 69 64 2c 20 70 42 75 66 2c 20 61 6d ite(id, pBuf, am
09a0: 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 69 6e t, offset);.}.in
09b0: 74 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 t sqlite3OsTrunc
09c0: 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ate(sqlite3_file
09d0: 20 2a 69 64 2c 20 69 36 34 20 73 69 7a 65 29 7b *id, i64 size){
09e0: 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d . return id->pM
09f0: 65 74 68 6f 64 73 2d 3e 78 54 72 75 6e 63 61 74 ethods->xTruncat
0a00: 65 28 69 64 2c 20 73 69 7a 65 29 3b 0a 7d 0a 69 e(id, size);.}.i
0a10: 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 nt sqlite3OsSync
0a20: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
0a30: 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 d, int flags){.
0a40: 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 DO_OS_MALLOC_TE
0a50: 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e ST(id);. return
0a60: 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 id->pMethods->x
0a70: 53 79 6e 63 28 69 64 2c 20 66 6c 61 67 73 29 3b Sync(id, flags);
0a80: 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 .}.int sqlite3Os
0a90: 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 FileSize(sqlite3
0aa0: 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 2a _file *id, i64 *
0ab0: 70 53 69 7a 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f pSize){. DO_OS_
0ac0: 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b MALLOC_TEST(id);
0ad0: 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d . return id->pM
0ae0: 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 53 69 7a ethods->xFileSiz
0af0: 65 28 69 64 2c 20 70 53 69 7a 65 29 3b 0a 7d 0a e(id, pSize);.}.
0b00: 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 int sqlite3OsLoc
0b10: 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
0b20: 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 id, int lockType
0b30: 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f ){. DO_OS_MALLO
0b40: 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 C_TEST(id);. re
0b50: 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 turn id->pMethod
0b60: 73 2d 3e 78 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 s->xLock(id, loc
0b70: 6b 54 79 70 65 29 3b 0a 7d 0a 69 6e 74 20 73 71 kType);.}.int sq
0b80: 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 lite3OsUnlock(sq
0b90: 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
0ba0: 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 int lockType){.
0bb0: 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 return id->pMet
0bc0: 68 6f 64 73 2d 3e 78 55 6e 6c 6f 63 6b 28 69 64 hods->xUnlock(id
0bd0: 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 69 , lockType);.}.i
0be0: 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 nt sqlite3OsChec
0bf0: 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 kReservedLock(sq
0c00: 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
0c10: 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 int *pResOut){.
0c20: 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 DO_OS_MALLOC_TE
0c30: 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e ST(id);. return
0c40: 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 id->pMethods->x
0c50: 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
0c60: 6b 28 69 64 2c 20 70 52 65 73 4f 75 74 29 3b 0a k(id, pResOut);.
0c70: 7d 0a 0a 2f 2a 0a 2a 2a 20 55 73 65 20 73 71 6c }../*.** Use sql
0c80: 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f ite3OsFileContro
0c90: 6c 28 29 20 77 68 65 6e 20 77 65 20 61 72 65 20 l() when we are
0ca0: 64 6f 69 6e 67 20 73 6f 6d 65 74 68 69 6e 67 20 doing something
0cb0: 74 68 61 74 20 6d 69 67 68 74 20 66 61 69 6c 0a that might fail.
0cc0: 2a 2a 20 61 6e 64 20 77 65 20 6e 65 65 64 20 74 ** and we need t
0cd0: 6f 20 6b 6e 6f 77 20 61 62 6f 75 74 20 74 68 65 o know about the
0ce0: 20 66 61 69 6c 75 72 65 73 2e 20 20 55 73 65 20 failures. Use
0cf0: 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e sqlite3OsFileCon
0d00: 74 72 6f 6c 48 69 6e 74 28 29 0a 2a 2a 20 77 68 trolHint().** wh
0d10: 65 6e 20 73 69 6d 70 6c 79 20 74 6f 73 73 69 6e en simply tossin
0d20: 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 76 g information ov
0d30: 65 72 20 74 68 65 20 77 61 6c 6c 20 74 6f 20 74 er the wall to t
0d40: 68 65 20 56 46 53 20 61 6e 64 20 77 65 20 64 6f he VFS and we do
0d50: 20 6e 6f 74 0a 2a 2a 20 72 65 61 6c 6c 79 20 63 not.** really c
0d60: 61 72 65 20 69 66 20 74 68 65 20 56 46 53 20 72 are if the VFS r
0d70: 65 63 65 69 76 65 73 20 61 6e 64 20 75 6e 64 65 eceives and unde
0d80: 72 73 74 61 6e 64 73 20 74 68 65 20 69 6e 66 6f rstands the info
0d90: 72 6d 61 74 69 6f 6e 20 73 69 6e 63 65 20 69 74 rmation since it
0da0: 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 61 20 68 69 .** is only a hi
0db0: 6e 74 20 61 6e 64 20 63 61 6e 20 62 65 20 73 61 nt and can be sa
0dc0: 66 65 6c 79 20 69 67 6e 6f 72 65 64 2e 20 20 54 fely ignored. T
0dd0: 68 65 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 he sqlite3OsFile
0de0: 43 6f 6e 74 72 6f 6c 48 69 6e 74 28 29 0a 2a 2a ControlHint().**
0df0: 20 72 6f 75 74 69 6e 65 20 68 61 73 20 6e 6f 20 routine has no
0e00: 72 65 74 75 72 6e 20 76 61 6c 75 65 20 73 69 6e return value sin
0e10: 63 65 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 ce the return va
0e20: 6c 75 65 20 77 6f 75 6c 64 20 62 65 20 6d 65 61 lue would be mea
0e30: 6e 69 6e 67 6c 65 73 73 2e 0a 2a 2f 0a 69 6e 74 ningless..*/.int
0e40: 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f sqlite3OsFileCo
0e50: 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 ntrol(sqlite3_fi
0e60: 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 le *id, int op,
0e70: 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 23 69 66 void *pArg){.#if
0e80: 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
0e90: 20 20 69 66 28 20 6f 70 21 3d 53 51 4c 49 54 45 if( op!=SQLITE
0ea0: 5f 46 43 4e 54 4c 5f 43 4f 4d 4d 49 54 5f 50 48 _FCNTL_COMMIT_PH
0eb0: 41 53 45 54 57 4f 20 29 7b 0a 20 20 20 20 2f 2a ASETWO ){. /*
0ec0: 20 46 61 75 6c 74 73 20 61 72 65 20 6e 6f 74 20 Faults are not
0ed0: 69 6e 6a 65 63 74 65 64 20 69 6e 74 6f 20 43 4f injected into CO
0ee0: 4d 4d 49 54 5f 50 48 41 53 45 54 57 4f 20 62 65 MMIT_PHASETWO be
0ef0: 63 61 75 73 65 2c 20 61 73 73 75 6d 69 6e 67 20 cause, assuming
0f00: 53 51 4c 69 74 65 0a 20 20 20 20 2a 2a 20 69 73 SQLite. ** is
0f10: 20 75 73 69 6e 67 20 61 20 72 65 67 75 6c 61 72 using a regular
0f20: 20 56 46 53 2c 20 69 74 20 69 73 20 63 61 6c 6c VFS, it is call
0f30: 65 64 20 61 66 74 65 72 20 74 68 65 20 63 6f 72 ed after the cor
0f40: 72 65 73 70 6f 6e 64 69 6e 67 20 0a 20 20 20 20 responding .
0f50: 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 ** transaction h
0f60: 61 73 20 62 65 65 6e 20 63 6f 6d 6d 69 74 74 65 as been committe
0f70: 64 2e 20 49 6e 6a 65 63 74 69 6e 67 20 61 20 66 d. Injecting a f
0f80: 61 75 6c 74 20 61 74 20 74 68 69 73 20 70 6f 69 ault at this poi
0f90: 6e 74 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 66 75 nt . ** confu
0fa0: 73 65 73 20 74 68 65 20 74 65 73 74 20 73 63 72 ses the test scr
0fb0: 69 70 74 73 20 2d 20 74 68 65 20 43 4f 4d 4d 49 ipts - the COMMI
0fc0: 54 20 63 6f 6d 61 6e 64 20 72 65 74 75 72 6e 73 T comand returns
0fd0: 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 SQLITE_NOMEM.
0fe0: 20 20 2a 2a 20 62 75 74 20 74 68 65 20 74 72 61 ** but the tra
0ff0: 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d nsaction is comm
1000: 69 74 74 65 64 20 61 6e 79 77 61 79 2e 0a 20 20 itted anyway..
1010: 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 **. ** The
1020: 63 6f 72 65 20 6d 75 73 74 20 63 61 6c 6c 20 4f core must call O
1030: 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 74 sFileControl() t
1040: 68 6f 75 67 68 2c 20 6e 6f 74 20 4f 73 46 69 6c hough, not OsFil
1050: 65 43 6f 6e 74 72 6f 6c 48 69 6e 74 28 29 2c 0a eControlHint(),.
1060: 20 20 20 20 2a 2a 20 61 73 20 69 66 20 61 20 63 ** as if a c
1070: 75 73 74 6f 6d 20 56 46 53 20 28 65 2e 67 2e 20 ustom VFS (e.g.
1080: 7a 69 70 76 66 73 29 20 72 65 74 75 72 6e 73 20 zipvfs) returns
1090: 61 6e 20 65 72 72 6f 72 20 68 65 72 65 2c 20 69 an error here, i
10a0: 74 20 70 72 6f 62 61 62 6c 79 0a 20 20 20 20 2a t probably. *
10b0: 2a 20 6d 65 61 6e 73 20 74 68 65 20 63 6f 6d 6d * means the comm
10c0: 69 74 20 72 65 61 6c 6c 79 20 68 61 73 20 66 61 it really has fa
10d0: 69 6c 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f iled and an erro
10e0: 72 20 73 68 6f 75 6c 64 20 62 65 20 72 65 74 75 r should be retu
10f0: 72 6e 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 74 rned. ** to t
1100: 68 65 20 75 73 65 72 2e 20 20 2a 2f 0a 20 20 20 he user. */.
1110: 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 DO_OS_MALLOC_TE
1120: 53 54 28 69 64 29 3b 0a 20 20 7d 0a 23 65 6e 64 ST(id);. }.#end
1130: 69 66 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e if. return id->
1140: 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 43 pMethods->xFileC
1150: 6f 6e 74 72 6f 6c 28 69 64 2c 20 6f 70 2c 20 70 ontrol(id, op, p
1160: 41 72 67 29 3b 0a 7d 0a 76 6f 69 64 20 73 71 6c Arg);.}.void sql
1170: 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f ite3OsFileContro
1180: 6c 48 69 6e 74 28 73 71 6c 69 74 65 33 5f 66 69 lHint(sqlite3_fi
1190: 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 le *id, int op,
11a0: 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 28 void *pArg){. (
11b0: 76 6f 69 64 29 69 64 2d 3e 70 4d 65 74 68 6f 64 void)id->pMethod
11c0: 73 2d 3e 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 s->xFileControl(
11d0: 69 64 2c 20 6f 70 2c 20 70 41 72 67 29 3b 0a 7d id, op, pArg);.}
11e0: 0a 0a 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 ..int sqlite3OsS
11f0: 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 ectorSize(sqlite
1200: 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 3_file *id){. i
1210: 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 69 7a 65 nt (*xSectorSize
1220: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 )(sqlite3_file*)
1230: 20 3d 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d = id->pMethods-
1240: 3e 78 53 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 >xSectorSize;.
1250: 72 65 74 75 72 6e 20 28 78 53 65 63 74 6f 72 53 return (xSectorS
1260: 69 7a 65 20 3f 20 78 53 65 63 74 6f 72 53 69 7a ize ? xSectorSiz
1270: 65 28 69 64 29 20 3a 20 53 51 4c 49 54 45 5f 44 e(id) : SQLITE_D
1280: 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 EFAULT_SECTOR_SI
1290: 5a 45 29 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 ZE);.}.int sqlit
12a0: 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 e3OsDeviceCharac
12b0: 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 teristics(sqlite
12c0: 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72 3_file *id){. r
12d0: 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f eturn id->pMetho
12e0: 64 73 2d 3e 78 44 65 76 69 63 65 43 68 61 72 61 ds->xDeviceChara
12f0: 63 74 65 72 69 73 74 69 63 73 28 69 64 29 3b 0a cteristics(id);.
1300: 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 }.int sqlite3OsS
1310: 68 6d 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 hmLock(sqlite3_f
1320: 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 66 66 ile *id, int off
1330: 73 65 74 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 set, int n, int
1340: 66 6c 61 67 73 29 7b 0a 20 20 72 65 74 75 72 6e flags){. return
1350: 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 id->pMethods->x
1360: 53 68 6d 4c 6f 63 6b 28 69 64 2c 20 6f 66 66 73 ShmLock(id, offs
1370: 65 74 2c 20 6e 2c 20 66 6c 61 67 73 29 3b 0a 7d et, n, flags);.}
1380: 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 53 .void sqlite3OsS
1390: 68 6d 42 61 72 72 69 65 72 28 73 71 6c 69 74 65 hmBarrier(sqlite
13a0: 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 3_file *id){. i
13b0: 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 68 d->pMethods->xSh
13c0: 6d 42 61 72 72 69 65 72 28 69 64 29 3b 0a 7d 0a mBarrier(id);.}.
13d0: 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 68 6d int sqlite3OsShm
13e0: 55 6e 6d 61 70 28 73 71 6c 69 74 65 33 5f 66 69 Unmap(sqlite3_fi
13f0: 6c 65 20 2a 69 64 2c 20 69 6e 74 20 64 65 6c 65 le *id, int dele
1400: 74 65 46 6c 61 67 29 7b 0a 20 20 72 65 74 75 72 teFlag){. retur
1410: 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e n id->pMethods->
1420: 78 53 68 6d 55 6e 6d 61 70 28 69 64 2c 20 64 65 xShmUnmap(id, de
1430: 6c 65 74 65 46 6c 61 67 29 3b 0a 7d 0a 69 6e 74 leteFlag);.}.int
1440: 20 73 71 6c 69 74 65 33 4f 73 53 68 6d 4d 61 70 sqlite3OsShmMap
1450: 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 (. sqlite3_file
1460: 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 *id,
1470: 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 /* Database
1480: 66 69 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 file handle */.
1490: 20 69 6e 74 20 69 50 61 67 65 2c 0a 20 20 69 6e int iPage,. in
14a0: 74 20 70 67 73 7a 2c 0a 20 20 69 6e 74 20 62 45 t pgsz,. int bE
14b0: 78 74 65 6e 64 2c 20 20 20 20 20 20 20 20 20 20 xtend,
14c0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
14d0: 65 20 74 6f 20 65 78 74 65 6e 64 20 66 69 6c 65 e to extend file
14e0: 20 69 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f if necessary */
14f0: 0a 20 20 76 6f 69 64 20 76 6f 6c 61 74 69 6c 65 . void volatile
1500: 20 2a 2a 70 70 20 20 20 20 20 20 20 20 20 20 20 **pp
1510: 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 /* OUT: Point
1520: 65 72 20 74 6f 20 6d 61 70 70 69 6e 67 20 2a 2f er to mapping */
1530: 0a 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c .){. DO_OS_MALL
1540: 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 OC_TEST(id);. r
1550: 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f eturn id->pMetho
1560: 64 73 2d 3e 78 53 68 6d 4d 61 70 28 69 64 2c 20 ds->xShmMap(id,
1570: 69 50 61 67 65 2c 20 70 67 73 7a 2c 20 62 45 78 iPage, pgsz, bEx
1580: 74 65 6e 64 2c 20 70 70 29 3b 0a 7d 0a 0a 23 69 tend, pp);.}..#i
1590: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4d 4d 41 f SQLITE_MAX_MMA
15a0: 50 5f 53 49 5a 45 3e 30 0a 2f 2a 20 54 68 65 20 P_SIZE>0./* The
15b0: 72 65 61 6c 20 69 6d 70 6c 65 6d 65 6e 74 61 74 real implementat
15c0: 69 6f 6e 20 6f 66 20 78 46 65 74 63 68 20 61 6e ion of xFetch an
15d0: 64 20 78 55 6e 66 65 74 63 68 20 2a 2f 0a 69 6e d xUnfetch */.in
15e0: 74 20 73 71 6c 69 74 65 33 4f 73 46 65 74 63 68 t sqlite3OsFetch
15f0: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
1600: 64 2c 20 69 36 34 20 69 4f 66 66 2c 20 69 6e 74 d, i64 iOff, int
1610: 20 69 41 6d 74 2c 20 76 6f 69 64 20 2a 2a 70 70 iAmt, void **pp
1620: 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f ){. DO_OS_MALLO
1630: 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 C_TEST(id);. re
1640: 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 turn id->pMethod
1650: 73 2d 3e 78 46 65 74 63 68 28 69 64 2c 20 69 4f s->xFetch(id, iO
1660: 66 66 2c 20 69 41 6d 74 2c 20 70 70 29 3b 0a 7d ff, iAmt, pp);.}
1670: 0a 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 6e .int sqlite3OsUn
1680: 66 65 74 63 68 28 73 71 6c 69 74 65 33 5f 66 69 fetch(sqlite3_fi
1690: 6c 65 20 2a 69 64 2c 20 69 36 34 20 69 4f 66 66 le *id, i64 iOff
16a0: 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65 , void *p){. re
16b0: 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 turn id->pMethod
16c0: 73 2d 3e 78 55 6e 66 65 74 63 68 28 69 64 2c 20 s->xUnfetch(id,
16d0: 69 4f 66 66 2c 20 70 29 3b 0a 7d 0a 23 65 6c 73 iOff, p);.}.#els
16e0: 65 0a 2f 2a 20 4e 6f 2d 6f 70 20 73 74 75 62 73 e./* No-op stubs
16f0: 20 74 6f 20 75 73 65 20 77 68 65 6e 20 6d 65 6d to use when mem
1700: 6f 72 79 2d 6d 61 70 70 65 64 20 49 2f 4f 20 69 ory-mapped I/O i
1710: 73 20 64 69 73 61 62 6c 65 64 20 2a 2f 0a 69 6e s disabled */.in
1720: 74 20 73 71 6c 69 74 65 33 4f 73 46 65 74 63 68 t sqlite3OsFetch
1730: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
1740: 64 2c 20 69 36 34 20 69 4f 66 66 2c 20 69 6e 74 d, i64 iOff, int
1750: 20 69 41 6d 74 2c 20 76 6f 69 64 20 2a 2a 70 70 iAmt, void **pp
1760: 29 7b 0a 20 20 2a 70 70 20 3d 20 30 3b 0a 20 20 ){. *pp = 0;.
1770: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1780: 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33 4f ;.}.int sqlite3O
1790: 73 55 6e 66 65 74 63 68 28 73 71 6c 69 74 65 33 sUnfetch(sqlite3
17a0: 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 69 _file *id, i64 i
17b0: 4f 66 66 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 Off, void *p){.
17c0: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
17d0: 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a K;.}.#endif../*.
17e0: 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 ** The next grou
17f0: 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 72 p of routines ar
1800: 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 e convenience wr
1810: 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20 74 68 appers around th
1820: 65 0a 2a 2a 20 56 46 53 20 6d 65 74 68 6f 64 73 e.** VFS methods
1830: 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
1840: 4f 73 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 OsOpen(. sqlite
1850: 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 3_vfs *pVfs, .
1860: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 const char *zPat
1870: 68 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 h, . sqlite3_fi
1880: 6c 65 20 2a 70 46 69 6c 65 2c 20 0a 20 20 69 6e le *pFile, . in
1890: 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20 t flags, . int
18a0: 2a 70 46 6c 61 67 73 4f 75 74 0a 29 7b 0a 20 20 *pFlagsOut.){.
18b0: 69 6e 74 20 72 63 3b 0a 20 20 44 4f 5f 4f 53 5f int rc;. DO_OS_
18c0: 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 30 29 3b 0a MALLOC_TEST(0);.
18d0: 20 20 2f 2a 20 30 78 38 37 66 37 66 20 69 73 20 /* 0x87f7f is
18e0: 61 20 6d 61 73 6b 20 6f 66 20 53 51 4c 49 54 45 a mask of SQLITE
18f0: 5f 4f 50 45 4e 5f 20 66 6c 61 67 73 20 74 68 61 _OPEN_ flags tha
1900: 74 20 61 72 65 20 76 61 6c 69 64 20 74 6f 20 62 t are valid to b
1910: 65 20 70 61 73 73 65 64 0a 20 20 2a 2a 20 64 6f e passed. ** do
1920: 77 6e 20 69 6e 74 6f 20 74 68 65 20 56 46 53 20 wn into the VFS
1930: 6c 61 79 65 72 2e 20 20 53 6f 6d 65 20 53 51 4c layer. Some SQL
1940: 49 54 45 5f 4f 50 45 4e 5f 20 66 6c 61 67 73 20 ITE_OPEN_ flags
1950: 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 20 20 (for example,.
1960: 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 ** SQLITE_OPEN_F
1970: 55 4c 4c 4d 55 54 45 58 20 6f 72 20 53 51 4c 49 ULLMUTEX or SQLI
1980: 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 TE_OPEN_SHAREDCA
1990: 43 48 45 29 20 61 72 65 20 62 6c 6f 63 6b 65 64 CHE) are blocked
19a0: 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 72 65 61 before. ** rea
19b0: 63 68 69 6e 67 20 74 68 65 20 56 46 53 2e 20 2a ching the VFS. *
19c0: 2f 0a 20 20 72 63 20 3d 20 70 56 66 73 2d 3e 78 /. rc = pVfs->x
19d0: 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 Open(pVfs, zPath
19e0: 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 20 26 , pFile, flags &
19f0: 20 30 78 38 37 66 37 66 2c 20 70 46 6c 61 67 73 0x87f7f, pFlags
1a00: 4f 75 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Out);. assert(
1a10: 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
1a20: 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 73 pFile->pMethods
1a30: 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ==0 );. return
1a40: 72 63 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 65 rc;.}.int sqlite
1a50: 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69 74 65 3OsDelete(sqlite
1a60: 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 3_vfs *pVfs, con
1a70: 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 st char *zPath,
1a80: 69 6e 74 20 64 69 72 53 79 6e 63 29 7b 0a 20 20 int dirSync){.
1a90: 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 DO_OS_MALLOC_TES
1aa0: 54 28 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 T(0);. assert(
1ab0: 64 69 72 53 79 6e 63 3d 3d 30 20 7c 7c 20 64 69 dirSync==0 || di
1ac0: 72 53 79 6e 63 3d 3d 31 20 29 3b 0a 20 20 72 65 rSync==1 );. re
1ad0: 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 65 6c 65 turn pVfs->xDele
1ae0: 74 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 te(pVfs, zPath,
1af0: 64 69 72 53 79 6e 63 29 3b 0a 7d 0a 69 6e 74 20 dirSync);.}.int
1b00: 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 sqlite3OsAccess(
1b10: 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
1b20: 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 pVfs, . const c
1b30: 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 69 har *zPath, . i
1b40: 6e 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 nt flags, . int
1b50: 20 2a 70 52 65 73 4f 75 74 0a 29 7b 0a 20 20 44 *pResOut.){. D
1b60: 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
1b70: 28 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 (0);. return pV
1b80: 66 73 2d 3e 78 41 63 63 65 73 73 28 70 56 66 73 fs->xAccess(pVfs
1b90: 2c 20 7a 50 61 74 68 2c 20 66 6c 61 67 73 2c 20 , zPath, flags,
1ba0: 70 52 65 73 4f 75 74 29 3b 0a 7d 0a 69 6e 74 20 pResOut);.}.int
1bb0: 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 sqlite3OsFullPat
1bc0: 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 hname(. sqlite3
1bd0: 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 _vfs *pVfs, . c
1be0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 onst char *zPath
1bf0: 2c 20 0a 20 20 69 6e 74 20 6e 50 61 74 68 4f 75 , . int nPathOu
1c00: 74 2c 20 0a 20 20 63 68 61 72 20 2a 7a 50 61 74 t, . char *zPat
1c10: 68 4f 75 74 0a 29 7b 0a 20 20 44 4f 5f 4f 53 5f hOut.){. DO_OS_
1c20: 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 30 29 3b 0a MALLOC_TEST(0);.
1c30: 20 20 7a 50 61 74 68 4f 75 74 5b 30 5d 20 3d 20 zPathOut[0] =
1c40: 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 0;. return pVfs
1c50: 2d 3e 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 ->xFullPathname(
1c60: 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 6e 50 61 pVfs, zPath, nPa
1c70: 74 68 4f 75 74 2c 20 7a 50 61 74 68 4f 75 74 29 thOut, zPathOut)
1c80: 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ;.}.#ifndef SQLI
1c90: 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
1ca0: 45 4e 53 49 4f 4e 0a 76 6f 69 64 20 2a 73 71 6c ENSION.void *sql
1cb0: 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 6c ite3OsDlOpen(sql
1cc0: 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
1cd0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 const char *zPat
1ce0: 68 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 h){. return pVf
1cf0: 73 2d 3e 78 44 6c 4f 70 65 6e 28 70 56 66 73 2c s->xDlOpen(pVfs,
1d00: 20 7a 50 61 74 68 29 3b 0a 7d 0a 76 6f 69 64 20 zPath);.}.void
1d10: 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 sqlite3OsDlError
1d20: 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
1d30: 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 fs, int nByte, c
1d40: 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 har *zBufOut){.
1d50: 20 70 56 66 73 2d 3e 78 44 6c 45 72 72 6f 72 28 pVfs->xDlError(
1d60: 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75 pVfs, nByte, zBu
1d70: 66 4f 75 74 29 3b 0a 7d 0a 76 6f 69 64 20 28 2a fOut);.}.void (*
1d80: 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 sqlite3OsDlSym(s
1d90: 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
1da0: 2c 20 76 6f 69 64 20 2a 70 48 64 6c 65 2c 20 63 , void *pHdle, c
1db0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 29 onst char *zSym)
1dc0: 29 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 )(void){. retur
1dd0: 6e 20 70 56 66 73 2d 3e 78 44 6c 53 79 6d 28 70 n pVfs->xDlSym(p
1de0: 56 66 73 2c 20 70 48 64 6c 65 2c 20 7a 53 79 6d Vfs, pHdle, zSym
1df0: 29 3b 0a 7d 0a 76 6f 69 64 20 73 71 6c 69 74 65 );.}.void sqlite
1e00: 33 4f 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 3OsDlClose(sqlit
1e10: 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f e3_vfs *pVfs, vo
1e20: 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 id *pHandle){.
1e30: 70 56 66 73 2d 3e 78 44 6c 43 6c 6f 73 65 28 70 pVfs->xDlClose(p
1e40: 56 66 73 2c 20 70 48 61 6e 64 6c 65 29 3b 0a 7d Vfs, pHandle);.}
1e50: 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1e60: 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
1e70: 4e 53 49 4f 4e 20 2a 2f 0a 69 6e 74 20 73 71 6c NSION */.int sql
1e80: 69 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 ite3OsRandomness
1e90: 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
1ea0: 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 fs, int nByte, c
1eb0: 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 har *zBufOut){.
1ec0: 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 52 return pVfs->xR
1ed0: 61 6e 64 6f 6d 6e 65 73 73 28 70 56 66 73 2c 20 andomness(pVfs,
1ee0: 6e 42 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b nByte, zBufOut);
1ef0: 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 .}.int sqlite3Os
1f00: 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 Sleep(sqlite3_vf
1f10: 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 4d 69 s *pVfs, int nMi
1f20: 63 72 6f 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 cro){. return p
1f30: 56 66 73 2d 3e 78 53 6c 65 65 70 28 70 56 66 73 Vfs->xSleep(pVfs
1f40: 2c 20 6e 4d 69 63 72 6f 29 3b 0a 7d 0a 69 6e 74 , nMicro);.}.int
1f50: 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e sqlite3OsCurren
1f60: 74 54 69 6d 65 49 6e 74 36 34 28 73 71 6c 69 74 tTimeInt64(sqlit
1f70: 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 73 71 e3_vfs *pVfs, sq
1f80: 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 54 69 lite3_int64 *pTi
1f90: 6d 65 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 meOut){. int rc
1fa0: 3b 0a 20 20 2f 2a 20 49 4d 50 4c 45 4d 45 4e 54 ;. /* IMPLEMENT
1fb0: 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d 34 39 30 34 ATION-OF: R-4904
1fc0: 35 2d 34 32 34 39 33 20 53 51 4c 69 74 65 20 77 5-42493 SQLite w
1fd0: 69 6c 6c 20 75 73 65 20 74 68 65 20 78 43 75 72 ill use the xCur
1fe0: 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 29 0a rentTimeInt64().
1ff0: 20 20 2a 2a 20 6d 65 74 68 6f 64 20 74 6f 20 67 ** method to g
2000: 65 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 et the current d
2010: 61 74 65 20 61 6e 64 20 74 69 6d 65 20 69 66 20 ate and time if
2020: 74 68 61 74 20 6d 65 74 68 6f 64 20 69 73 20 61 that method is a
2030: 76 61 69 6c 61 62 6c 65 0a 20 20 2a 2a 20 28 69 vailable. ** (i
2040: 66 20 69 56 65 72 73 69 6f 6e 20 69 73 20 32 20 f iVersion is 2
2050: 6f 72 20 67 72 65 61 74 65 72 20 61 6e 64 20 74 or greater and t
2060: 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e he function poin
2070: 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 29 ter is not NULL)
2080: 20 61 6e 64 0a 20 20 2a 2a 20 77 69 6c 6c 20 66 and. ** will f
2090: 61 6c 6c 20 62 61 63 6b 20 74 6f 20 78 43 75 72 all back to xCur
20a0: 72 65 6e 74 54 69 6d 65 28 29 20 69 66 20 78 43 rentTime() if xC
20b0: 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 urrentTimeInt64(
20c0: 29 20 69 73 0a 20 20 2a 2a 20 75 6e 61 76 61 69 ) is. ** unavai
20d0: 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 lable.. */. if
20e0: 28 20 70 56 66 73 2d 3e 69 56 65 72 73 69 6f 6e ( pVfs->iVersion
20f0: 3e 3d 32 20 26 26 20 70 56 66 73 2d 3e 78 43 75 >=2 && pVfs->xCu
2100: 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 20 29 rrentTimeInt64 )
2110: 7b 0a 20 20 20 20 72 63 20 3d 20 70 56 66 73 2d {. rc = pVfs-
2120: 3e 78 43 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 >xCurrentTimeInt
2130: 36 34 28 70 56 66 73 2c 20 70 54 69 6d 65 4f 75 64(pVfs, pTimeOu
2140: 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 t);. }else{.
2150: 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20 20 20 72 double r;. r
2160: 63 20 3d 20 70 56 66 73 2d 3e 78 43 75 72 72 65 c = pVfs->xCurre
2170: 6e 74 54 69 6d 65 28 70 56 66 73 2c 20 26 72 29 ntTime(pVfs, &r)
2180: 3b 0a 20 20 20 20 2a 70 54 69 6d 65 4f 75 74 20 ;. *pTimeOut
2190: 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 = (sqlite3_int64
21a0: 29 28 72 2a 38 36 34 30 30 30 30 30 2e 30 29 3b )(r*86400000.0);
21b0: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
21c0: 3b 0a 7d 0a 0a 69 6e 74 20 73 71 6c 69 74 65 33 ;.}..int sqlite3
21d0: 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 0a 20 20 OsOpenMalloc(.
21e0: 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
21f0: 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 s, . const char
2200: 20 2a 7a 46 69 6c 65 2c 20 0a 20 20 73 71 6c 69 *zFile, . sqli
2210: 74 65 33 5f 66 69 6c 65 20 2a 2a 70 70 46 69 6c te3_file **ppFil
2220: 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c e, . int flags,
2230: 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 . int *pOutFlag
2240: 73 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 s.){. int rc =
2250: 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
2260: 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 sqlite3_file *pF
2270: 69 6c 65 3b 0a 20 20 70 46 69 6c 65 20 3d 20 28 ile;. pFile = (
2280: 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 73 sqlite3_file *)s
2290: 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
22a0: 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 (pVfs->szOsFile)
22b0: 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 20 29 7b ;. if( pFile ){
22c0: 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
22d0: 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 46 3OsOpen(pVfs, zF
22e0: 69 6c 65 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 ile, pFile, flag
22f0: 73 2c 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 s, pOutFlags);.
2300: 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
2310: 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 E_OK ){. sq
2320: 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 lite3_free(pFile
2330: 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
2340: 20 20 20 20 2a 70 70 46 69 6c 65 20 3d 20 70 46 *ppFile = pF
2350: 69 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ile;. }. }.
2360: 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 69 6e return rc;.}.in
2370: 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 t sqlite3OsClose
2380: 46 72 65 65 28 73 71 6c 69 74 65 33 5f 66 69 6c Free(sqlite3_fil
2390: 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 e *pFile){. int
23a0: 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
23b0: 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
23c0: 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 );. rc = sqlit
23d0: 65 33 4f 73 43 6c 6f 73 65 28 70 46 69 6c 65 29 e3OsClose(pFile)
23e0: 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
23f0: 28 70 46 69 6c 65 29 3b 0a 20 20 72 65 74 75 72 (pFile);. retur
2400: 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
2410: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
2420: 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 a wrapper around
2430: 20 74 68 65 20 4f 53 20 73 70 65 63 69 66 69 63 the OS specific
2440: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
2450: 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 of.** sqlite3_os
2460: 5f 69 6e 69 74 28 29 2e 20 54 68 65 20 70 75 72 _init(). The pur
2470: 70 6f 73 65 20 6f 66 20 74 68 65 20 77 72 61 70 pose of the wrap
2480: 70 65 72 20 69 73 20 74 6f 20 70 72 6f 76 69 64 per is to provid
2490: 65 20 74 68 65 0a 2a 2a 20 61 62 69 6c 69 74 79 e the.** ability
24a0: 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 6d to simulate a m
24b0: 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 2c 20 73 alloc failure, s
24c0: 6f 20 74 68 61 74 20 74 68 65 20 68 61 6e 64 6c o that the handl
24d0: 69 6e 67 20 6f 66 20 61 6e 0a 2a 2a 20 65 72 72 ing of an.** err
24e0: 6f 72 20 69 6e 20 73 71 6c 69 74 65 33 5f 6f 73 or in sqlite3_os
24f0: 5f 69 6e 69 74 28 29 20 62 79 20 74 68 65 20 75 _init() by the u
2500: 70 70 65 72 20 6c 61 79 65 72 73 20 63 61 6e 20 pper layers can
2510: 62 65 20 74 65 73 74 65 64 2e 0a 2a 2f 0a 69 6e be tested..*/.in
2520: 74 20 73 71 6c 69 74 65 33 4f 73 49 6e 69 74 28 t sqlite3OsInit(
2530: 76 6f 69 64 29 7b 0a 20 20 76 6f 69 64 20 2a 70 void){. void *p
2540: 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f = sqlite3_mallo
2550: 63 28 31 30 29 3b 0a 20 20 69 66 28 20 70 3d 3d c(10);. if( p==
2560: 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 0 ) return SQLIT
2570: 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69 74 E_NOMEM;. sqlit
2580: 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 72 65 e3_free(p);. re
2590: 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6f 73 5f turn sqlite3_os_
25a0: 69 6e 69 74 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a init();.}../*.**
25b0: 20 54 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c The list of all
25c0: 20 72 65 67 69 73 74 65 72 65 64 20 56 46 53 20 registered VFS
25d0: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e implementations.
25e0: 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 .*/.static sqlit
25f0: 65 33 5f 76 66 73 20 2a 20 53 51 4c 49 54 45 5f e3_vfs * SQLITE_
2600: 57 53 44 20 76 66 73 4c 69 73 74 20 3d 20 30 3b WSD vfsList = 0;
2610: 0a 23 64 65 66 69 6e 65 20 76 66 73 4c 69 73 74 .#define vfsList
2620: 20 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 5f GLOBAL(sqlite3_
2630: 76 66 73 20 2a 2c 20 76 66 73 4c 69 73 74 29 0a vfs *, vfsList).
2640: 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 20 ./*.** Locate a
2650: 56 46 53 20 62 79 20 6e 61 6d 65 2e 20 20 49 66 VFS by name. If
2660: 20 6e 6f 20 6e 61 6d 65 20 69 73 20 67 69 76 65 no name is give
2670: 6e 2c 20 73 69 6d 70 6c 79 20 72 65 74 75 72 6e n, simply return
2680: 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 56 46 the.** first VF
2690: 53 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a S on the list..*
26a0: 2f 0a 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 /.sqlite3_vfs *s
26b0: 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 qlite3_vfs_find(
26c0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 const char *zVfs
26d0: 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 ){. sqlite3_vfs
26e0: 20 2a 70 56 66 73 20 3d 20 30 3b 0a 23 69 66 20 *pVfs = 0;.#if
26f0: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
2700: 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 E. sqlite3_mute
2710: 78 20 2a 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 x *mutex;.#endif
2720: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
2730: 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 OMIT_AUTOINIT.
2740: 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 int rc = sqlite3
2750: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 _initialize();.
2760: 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
2770: 20 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 0;.#endif.#if S
2780: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
2790: 0a 20 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 . mutex = sqlit
27a0: 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
27b0: 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
27c0: 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 _MASTER);.#endif
27d0: 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
27e0: 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 _enter(mutex);.
27f0: 20 66 6f 72 28 70 56 66 73 20 3d 20 76 66 73 4c for(pVfs = vfsL
2800: 69 73 74 3b 20 70 56 66 73 3b 20 70 56 66 73 3d ist; pVfs; pVfs=
2810: 70 56 66 73 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 pVfs->pNext){.
2820: 20 20 69 66 28 20 7a 56 66 73 3d 3d 30 20 29 20 if( zVfs==0 )
2830: 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 break;. if( s
2840: 74 72 63 6d 70 28 7a 56 66 73 2c 20 70 56 66 73 trcmp(zVfs, pVfs
2850: 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 ->zName)==0 ) br
2860: 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 eak;. }. sqlit
2870: 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
2880: 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 utex);. return
2890: 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 pVfs;.}../*.** U
28a0: 6e 6c 69 6e 6b 20 61 20 56 46 53 20 66 72 6f 6d nlink a VFS from
28b0: 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 the linked list
28c0: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
28d0: 76 66 73 55 6e 6c 69 6e 6b 28 73 71 6c 69 74 65 vfsUnlink(sqlite
28e0: 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 3_vfs *pVfs){.
28f0: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
2900: 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 mutex_held(sqlit
2910: 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
2920: 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
2930: 5f 4d 41 53 54 45 52 29 29 20 29 3b 0a 20 20 69 _MASTER)) );. i
2940: 66 28 20 70 56 66 73 3d 3d 30 20 29 7b 0a 20 20 f( pVfs==0 ){.
2950: 20 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 20 20 /* No-op */.
2960: 7d 65 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73 }else if( vfsLis
2970: 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 76 t==pVfs ){. v
2980: 66 73 4c 69 73 74 20 3d 20 70 56 66 73 2d 3e 70 fsList = pVfs->p
2990: 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 Next;. }else if
29a0: 28 20 76 66 73 4c 69 73 74 20 29 7b 0a 20 20 20 ( vfsList ){.
29b0: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 20 sqlite3_vfs *p
29c0: 3d 20 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 77 = vfsList;. w
29d0: 68 69 6c 65 28 20 70 2d 3e 70 4e 65 78 74 20 26 hile( p->pNext &
29e0: 26 20 70 2d 3e 70 4e 65 78 74 21 3d 70 56 66 73 & p->pNext!=pVfs
29f0: 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d ){. p = p-
2a00: 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 >pNext;. }.
2a10: 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 3d 3d if( p->pNext==
2a20: 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70 2d pVfs ){. p-
2a30: 3e 70 4e 65 78 74 20 3d 20 70 56 66 73 2d 3e 70 >pNext = pVfs->p
2a40: 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Next;. }. }.
2a50: 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 }../*.** Registe
2a60: 72 20 61 20 56 46 53 20 77 69 74 68 20 74 68 65 r a VFS with the
2a70: 20 73 79 73 74 65 6d 2e 20 20 49 74 20 69 73 20 system. It is
2a80: 68 61 72 6d 6c 65 73 73 20 74 6f 20 72 65 67 69 harmless to regi
2a90: 73 74 65 72 20 74 68 65 20 73 61 6d 65 0a 2a 2a ster the same.**
2aa0: 20 56 46 53 20 6d 75 6c 74 69 70 6c 65 20 74 69 VFS multiple ti
2ab0: 6d 65 73 2e 20 20 54 68 65 20 6e 65 77 20 56 46 mes. The new VF
2ac0: 53 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 S becomes the de
2ad0: 66 61 75 6c 74 20 69 66 20 6d 61 6b 65 44 66 6c fault if makeDfl
2ae0: 74 20 69 73 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f t is.** true..*/
2af0: 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 .int sqlite3_vfs
2b00: 5f 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 _register(sqlite
2b10: 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
2b20: 20 6d 61 6b 65 44 66 6c 74 29 7b 0a 20 20 4d 55 makeDflt){. MU
2b30: 54 45 58 5f 4c 4f 47 49 43 28 73 71 6c 69 74 65 TEX_LOGIC(sqlite
2b40: 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 29 3_mutex *mutex;)
2b50: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
2b60: 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 OMIT_AUTOINIT.
2b70: 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 int rc = sqlite3
2b80: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 _initialize();.
2b90: 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
2ba0: 20 72 63 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 rc;.#endif.#ifd
2bb0: 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
2bc0: 5f 41 50 49 5f 41 52 4d 4f 52 0a 20 20 69 66 28 _API_ARMOR. if(
2bd0: 20 70 56 66 73 3d 3d 30 20 29 20 72 65 74 75 72 pVfs==0 ) retur
2be0: 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f n SQLITE_MISUSE_
2bf0: 42 4b 50 54 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 BKPT;.#endif..
2c00: 4d 55 54 45 58 5f 4c 4f 47 49 43 28 20 6d 75 74 MUTEX_LOGIC( mut
2c10: 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ex = sqlite3Mute
2c20: 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
2c30: 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
2c40: 52 29 3b 20 29 0a 20 20 73 71 6c 69 74 65 33 5f R); ). sqlite3_
2c50: 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 mutex_enter(mute
2c60: 78 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 x);. vfsUnlink(
2c70: 70 56 66 73 29 3b 0a 20 20 69 66 28 20 6d 61 6b pVfs);. if( mak
2c80: 65 44 66 6c 74 20 7c 7c 20 76 66 73 4c 69 73 74 eDflt || vfsList
2c90: 3d 3d 30 20 29 7b 0a 20 20 20 20 70 56 66 73 2d ==0 ){. pVfs-
2ca0: 3e 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 >pNext = vfsList
2cb0: 3b 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 ;. vfsList =
2cc0: 70 56 66 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 pVfs;. }else{.
2cd0: 20 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d pVfs->pNext =
2ce0: 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 3b vfsList->pNext;
2cf0: 0a 20 20 20 20 76 66 73 4c 69 73 74 2d 3e 70 4e . vfsList->pN
2d00: 65 78 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 0a ext = pVfs;. }.
2d10: 20 20 61 73 73 65 72 74 28 76 66 73 4c 69 73 74 assert(vfsList
2d20: 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 );. sqlite3_mut
2d30: 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b ex_leave(mutex);
2d40: 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
2d50: 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e _OK;.}../*.** Un
2d60: 72 65 67 69 73 74 65 72 20 61 20 56 46 53 20 73 register a VFS s
2d70: 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 20 o that it is no
2d80: 6c 6f 6e 67 65 72 20 61 63 63 65 73 73 69 62 6c longer accessibl
2d90: 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 e..*/.int sqlite
2da0: 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 3_vfs_unregister
2db0: 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
2dc0: 66 73 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f fs){.#if SQLITE_
2dd0: 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c THREADSAFE. sql
2de0: 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
2df0: 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 x = sqlite3Mutex
2e00: 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
2e10: 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
2e20: 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 );.#endif. sqli
2e30: 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
2e40: 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 55 6e 6c mutex);. vfsUnl
2e50: 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 73 71 6c ink(pVfs);. sql
2e60: 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
2e70: 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 (mutex);. retur
2e80: 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a n SQLITE_OK;.}.