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 4f 70 65 6e 28 29 0a 2a qlite3OsOpen().*
03e0: 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 52 * sqlite3OsR
03f0: 65 61 64 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c ead().** sql
0400: 69 74 65 33 4f 73 57 72 69 74 65 28 29 0a 2a 2a ite3OsWrite().**
0410: 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53 79 sqlite3OsSy
0420: 6e 63 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 nc().** sqli
0430: 74 65 33 4f 73 4c 6f 63 6b 28 29 0a 2a 2a 0a 2a te3OsLock().**.*
0440: 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 /.#if defined(SQ
0450: 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 28 53 LITE_TEST) && (S
0460: 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 3d 3d 30 29 QLITE_OS_WIN==0)
0470: 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53 . #define DO_OS
0480: 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 78 29 20 _MALLOC_TEST(x)
0490: 69 66 20 28 21 78 20 7c 7c 20 21 73 71 6c 69 74 if (!x || !sqlit
04a0: 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 78 e3IsMemJournal(x
04b0: 29 29 20 7b 20 20 20 20 20 5c 0a 20 20 20 20 76 )) { \. v
04c0: 6f 69 64 20 2a 70 54 73 74 41 6c 6c 6f 63 20 3d oid *pTstAlloc =
04d0: 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 31 sqlite3Malloc(1
04e0: 30 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0);
04f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0500: 5c 0a 20 20 20 20 69 66 20 28 21 70 54 73 74 41 \. if (!pTstA
0510: 6c 6c 6f 63 29 20 72 65 74 75 72 6e 20 53 51 4c lloc) return SQL
0520: 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b ITE_IOERR_NOMEM;
0530: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0540: 20 20 20 20 20 20 20 5c 0a 20 20 20 20 73 71 6c \. sql
0550: 69 74 65 33 5f 66 72 65 65 28 70 54 73 74 41 6c ite3_free(pTstAl
0560: 6c 6f 63 29 3b 20 20 20 20 20 20 20 20 20 20 20 loc);
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 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 }.#else. #def
05a0: 69 6e 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 ine DO_OS_MALLOC
05b0: 5f 54 45 53 54 28 78 29 0a 23 65 6e 64 69 66 0a _TEST(x).#endif.
05c0: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
05d0: 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 72 wing routines ar
05e0: 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 e convenience wr
05f0: 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20 6d 65 appers around me
0600: 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 68 65 20 thods.** of the
0610: 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a sqlite3_file obj
0620: 65 63 74 2e 20 20 54 68 69 73 20 69 73 20 6d 6f ect. This is mo
0630: 73 74 6c 79 20 6a 75 73 74 20 73 79 6e 74 61 63 stly just syntac
0640: 74 69 63 20 73 75 67 61 72 2e 20 41 6c 6c 0a 2a tic sugar. All.*
0650: 2a 20 6f 66 20 74 68 69 73 20 77 6f 75 6c 64 20 * of this would
0660: 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 61 75 be completely au
0670: 74 6f 6d 61 74 69 63 20 69 66 20 53 51 4c 69 74 tomatic if SQLit
0680: 65 20 77 65 72 65 20 63 6f 64 65 64 20 75 73 69 e were coded usi
0690: 6e 67 0a 2a 2a 20 43 2b 2b 20 69 6e 73 74 65 61 ng.** C++ instea
06a0: 64 20 6f 66 20 70 6c 61 69 6e 20 6f 6c 64 20 43 d of plain old C
06b0: 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
06c0: 4f 73 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f OsClose(sqlite3_
06d0: 66 69 6c 65 20 2a 70 49 64 29 7b 0a 20 20 69 6e file *pId){. in
06e0: 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
06f0: 3b 0a 20 20 69 66 28 20 70 49 64 2d 3e 70 4d 65 ;. if( pId->pMe
0700: 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 72 63 20 thods ){. rc
0710: 3d 20 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 2d = pId->pMethods-
0720: 3e 78 43 6c 6f 73 65 28 70 49 64 29 3b 0a 20 20 >xClose(pId);.
0730: 20 20 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20 pId->pMethods
0740: 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 0;. }. retur
0750: 6e 20 72 63 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 n rc;.}.int sqli
0760: 74 65 33 4f 73 52 65 61 64 28 73 71 6c 69 74 65 te3OsRead(sqlite
0770: 33 5f 66 69 6c 65 20 2a 69 64 2c 20 76 6f 69 64 3_file *id, void
0780: 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c *pBuf, int amt,
0790: 20 69 36 34 20 6f 66 66 73 65 74 29 7b 0a 20 20 i64 offset){.
07a0: 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 DO_OS_MALLOC_TES
07b0: 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 T(id);. return
07c0: 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 52 id->pMethods->xR
07d0: 65 61 64 28 69 64 2c 20 70 42 75 66 2c 20 61 6d ead(id, pBuf, am
07e0: 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 69 6e t, offset);.}.in
07f0: 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 t sqlite3OsWrite
0800: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
0810: 64 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 d, const void *p
0820: 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 Buf, int amt, i6
0830: 34 20 6f 66 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4 offset){. DO_
0840: 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 OS_MALLOC_TEST(i
0850: 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d d);. return id-
0860: 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 57 72 69 74 >pMethods->xWrit
0870: 65 28 69 64 2c 20 70 42 75 66 2c 20 61 6d 74 2c e(id, pBuf, amt,
0880: 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 69 6e 74 20 offset);.}.int
0890: 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 sqlite3OsTruncat
08a0: 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
08b0: 69 64 2c 20 69 36 34 20 73 69 7a 65 29 7b 0a 20 id, i64 size){.
08c0: 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 return id->pMet
08d0: 68 6f 64 73 2d 3e 78 54 72 75 6e 63 61 74 65 28 hods->xTruncate(
08e0: 69 64 2c 20 73 69 7a 65 29 3b 0a 7d 0a 69 6e 74 id, size);.}.int
08f0: 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 sqlite3OsSync(s
0900: 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
0910: 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 44 int flags){. D
0920: 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
0930: 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 (id);. return i
0940: 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 79 d->pMethods->xSy
0950: 6e 63 28 69 64 2c 20 66 6c 61 67 73 29 3b 0a 7d nc(id, flags);.}
0960: 0a 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 .int sqlite3OsFi
0970: 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 leSize(sqlite3_f
0980: 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 ile *id, i64 *pS
0990: 69 7a 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 ize){. DO_OS_MA
09a0: 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 LLOC_TEST(id);.
09b0: 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 return id->pMet
09c0: 68 6f 64 73 2d 3e 78 46 69 6c 65 53 69 7a 65 28 hods->xFileSize(
09d0: 69 64 2c 20 70 53 69 7a 65 29 3b 0a 7d 0a 69 6e id, pSize);.}.in
09e0: 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 t sqlite3OsLock(
09f0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
0a00: 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b , int lockType){
0a10: 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f . DO_OS_MALLOC_
0a20: 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 TEST(id);. retu
0a30: 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d rn id->pMethods-
0a40: 3e 78 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 >xLock(id, lockT
0a50: 79 70 65 29 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 ype);.}.int sqli
0a60: 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 te3OsUnlock(sqli
0a70: 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
0a80: 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20 72 t lockType){. r
0a90: 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f eturn id->pMetho
0aa0: 64 73 2d 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 ds->xUnlock(id,
0ab0: 6c 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 69 6e 74 lockType);.}.int
0ac0: 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 sqlite3OsCheckR
0ad0: 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 eservedLock(sqli
0ae0: 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
0af0: 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 44 t *pResOut){. D
0b00: 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
0b10: 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 (id);. return i
0b20: 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 68 d->pMethods->xCh
0b30: 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
0b40: 69 64 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d 0a id, pResOut);.}.
0b50: 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c int sqlite3OsFil
0b60: 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 eControl(sqlite3
0b70: 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f _file *id, int o
0b80: 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a p, void *pArg){.
0b90: 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 return id->pMe
0ba0: 74 68 6f 64 73 2d 3e 78 46 69 6c 65 43 6f 6e 74 thods->xFileCont
0bb0: 72 6f 6c 28 69 64 2c 20 6f 70 2c 20 70 41 72 67 rol(id, op, pArg
0bc0: 29 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33 );.}.int sqlite3
0bd0: 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c OsSectorSize(sql
0be0: 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a ite3_file *id){.
0bf0: 20 20 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 int (*xSectorS
0c00: 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c ize)(sqlite3_fil
0c10: 65 2a 29 20 3d 20 69 64 2d 3e 70 4d 65 74 68 6f e*) = id->pMetho
0c20: 64 73 2d 3e 78 53 65 63 74 6f 72 53 69 7a 65 3b ds->xSectorSize;
0c30: 0a 20 20 72 65 74 75 72 6e 20 28 78 53 65 63 74 . return (xSect
0c40: 6f 72 53 69 7a 65 20 3f 20 78 53 65 63 74 6f 72 orSize ? xSector
0c50: 53 69 7a 65 28 69 64 29 20 3a 20 53 51 4c 49 54 Size(id) : SQLIT
0c60: 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 E_DEFAULT_SECTOR
0c70: 5f 53 49 5a 45 29 3b 0a 7d 0a 69 6e 74 20 73 71 _SIZE);.}.int sq
0c80: 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 lite3OsDeviceCha
0c90: 72 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c racteristics(sql
0ca0: 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a ite3_file *id){.
0cb0: 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 return id->pMe
0cc0: 74 68 6f 64 73 2d 3e 78 44 65 76 69 63 65 43 68 thods->xDeviceCh
0cd0: 61 72 61 63 74 65 72 69 73 74 69 63 73 28 69 64 aracteristics(id
0ce0: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 );.}../*.** The
0cf0: 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f next group of ro
0d00: 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 utines are conve
0d10: 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 73 20 nience wrappers
0d20: 61 72 6f 75 6e 64 20 74 68 65 0a 2a 2a 20 56 46 around the.** VF
0d30: 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 69 6e S methods..*/.in
0d40: 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 t sqlite3OsOpen(
0d50: 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
0d60: 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 pVfs, . const c
0d70: 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 73 har *zPath, . s
0d80: 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 qlite3_file *pFi
0d90: 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 le, . int flags
0da0: 2c 20 0a 20 20 69 6e 74 20 2a 70 46 6c 61 67 73 , . int *pFlags
0db0: 4f 75 74 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b Out.){. int rc;
0dc0: 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f . DO_OS_MALLOC_
0dd0: 54 45 53 54 28 30 29 3b 0a 20 20 2f 2a 20 30 78 TEST(0);. /* 0x
0de0: 37 66 33 66 20 69 73 20 61 20 6d 61 73 6b 20 6f 7f3f is a mask o
0df0: 66 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 20 66 f SQLITE_OPEN_ f
0e00: 6c 61 67 73 20 74 68 61 74 20 61 72 65 20 76 61 lags that are va
0e10: 6c 69 64 20 74 6f 20 62 65 20 70 61 73 73 65 64 lid to be passed
0e20: 0a 20 20 2a 2a 20 64 6f 77 6e 20 69 6e 74 6f 20 . ** down into
0e30: 74 68 65 20 56 46 53 20 6c 61 79 65 72 2e 20 20 the VFS layer.
0e40: 53 6f 6d 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e Some SQLITE_OPEN
0e50: 5f 20 66 6c 61 67 73 20 28 66 6f 72 20 65 78 61 _ flags (for exa
0e60: 6d 70 6c 65 2c 0a 20 20 2a 2a 20 53 51 4c 49 54 mple,. ** SQLIT
0e70: 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 E_OPEN_FULLMUTEX
0e80: 20 6f 72 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f or SQLITE_OPEN_
0e90: 53 48 41 52 45 44 43 41 43 48 45 29 20 61 72 65 SHAREDCACHE) are
0ea0: 20 62 6c 6f 63 6b 65 64 20 62 65 66 6f 72 65 0a blocked before.
0eb0: 20 20 2a 2a 20 72 65 61 63 68 69 6e 67 20 74 68 ** reaching th
0ec0: 65 20 56 46 53 2e 20 2a 2f 0a 20 20 72 63 20 3d e VFS. */. rc =
0ed0: 20 70 56 66 73 2d 3e 78 4f 70 65 6e 28 70 56 66 pVfs->xOpen(pVf
0ee0: 73 2c 20 7a 50 61 74 68 2c 20 70 46 69 6c 65 2c s, zPath, pFile,
0ef0: 20 66 6c 61 67 73 20 26 20 30 78 37 66 33 66 2c flags & 0x7f3f,
0f00: 20 70 46 6c 61 67 73 4f 75 74 29 3b 0a 20 20 61 pFlagsOut);. a
0f10: 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
0f20: 45 5f 4f 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 70 E_OK || pFile->p
0f30: 4d 65 74 68 6f 64 73 3d 3d 30 20 29 3b 0a 20 20 Methods==0 );.
0f40: 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 69 6e 74 return rc;.}.int
0f50: 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 sqlite3OsDelete
0f60: 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
0f70: 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a fs, const char *
0f80: 7a 50 61 74 68 2c 20 69 6e 74 20 64 69 72 53 79 zPath, int dirSy
0f90: 6e 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 nc){. return pV
0fa0: 66 73 2d 3e 78 44 65 6c 65 74 65 28 70 56 66 73 fs->xDelete(pVfs
0fb0: 2c 20 7a 50 61 74 68 2c 20 64 69 72 53 79 6e 63 , zPath, dirSync
0fc0: 29 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33 );.}.int sqlite3
0fd0: 4f 73 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 OsAccess(. sqli
0fe0: 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a te3_vfs *pVfs, .
0ff0: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 const char *zP
1000: 61 74 68 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 ath, . int flag
1010: 73 2c 20 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f s, . int *pResO
1020: 75 74 0a 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 ut.){. DO_OS_MA
1030: 4c 4c 4f 43 5f 54 45 53 54 28 30 29 3b 0a 20 20 LLOC_TEST(0);.
1040: 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 41 63 return pVfs->xAc
1050: 63 65 73 73 28 70 56 66 73 2c 20 7a 50 61 74 68 cess(pVfs, zPath
1060: 2c 20 66 6c 61 67 73 2c 20 70 52 65 73 4f 75 74 , flags, pResOut
1070: 29 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33 );.}.int sqlite3
1080: 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a OsFullPathname(.
1090: 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
10a0: 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 Vfs, . const ch
10b0: 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e ar *zPath, . in
10c0: 74 20 6e 50 61 74 68 4f 75 74 2c 20 0a 20 20 63 t nPathOut, . c
10d0: 68 61 72 20 2a 7a 50 61 74 68 4f 75 74 0a 29 7b har *zPathOut.){
10e0: 0a 20 20 7a 50 61 74 68 4f 75 74 5b 30 5d 20 3d . zPathOut[0] =
10f0: 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 0;. return pVf
1100: 73 2d 3e 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 s->xFullPathname
1110: 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 6e 50 (pVfs, zPath, nP
1120: 61 74 68 4f 75 74 2c 20 7a 50 61 74 68 4f 75 74 athOut, zPathOut
1130: 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c );.}.#ifndef SQL
1140: 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 ITE_OMIT_LOAD_EX
1150: 54 45 4e 53 49 4f 4e 0a 76 6f 69 64 20 2a 73 71 TENSION.void *sq
1160: 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 lite3OsDlOpen(sq
1170: 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
1180: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 const char *zPa
1190: 74 68 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 th){. return pV
11a0: 66 73 2d 3e 78 44 6c 4f 70 65 6e 28 70 56 66 73 fs->xDlOpen(pVfs
11b0: 2c 20 7a 50 61 74 68 29 3b 0a 7d 0a 76 6f 69 64 , zPath);.}.void
11c0: 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f sqlite3OsDlErro
11d0: 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 r(sqlite3_vfs *p
11e0: 56 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 Vfs, int nByte,
11f0: 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a char *zBufOut){.
1200: 20 20 70 56 66 73 2d 3e 78 44 6c 45 72 72 6f 72 pVfs->xDlError
1210: 28 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 (pVfs, nByte, zB
1220: 75 66 4f 75 74 29 3b 0a 7d 0a 76 6f 69 64 20 28 ufOut);.}.void (
1230: 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 *sqlite3OsDlSym(
1240: 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
1250: 73 2c 20 76 6f 69 64 20 2a 70 48 64 6c 65 2c 20 s, void *pHdle,
1260: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d const char *zSym
1270: 29 29 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 ))(void){. retu
1280: 72 6e 20 70 56 66 73 2d 3e 78 44 6c 53 79 6d 28 rn pVfs->xDlSym(
1290: 70 56 66 73 2c 20 70 48 64 6c 65 2c 20 7a 53 79 pVfs, pHdle, zSy
12a0: 6d 29 3b 0a 7d 0a 76 6f 69 64 20 73 71 6c 69 74 m);.}.void sqlit
12b0: 65 33 4f 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 e3OsDlClose(sqli
12c0: 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 te3_vfs *pVfs, v
12d0: 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 oid *pHandle){.
12e0: 20 70 56 66 73 2d 3e 78 44 6c 43 6c 6f 73 65 28 pVfs->xDlClose(
12f0: 70 56 66 73 2c 20 70 48 61 6e 64 6c 65 29 3b 0a pVfs, pHandle);.
1300: 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1310: 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
1320: 45 4e 53 49 4f 4e 20 2a 2f 0a 69 6e 74 20 73 71 ENSION */.int sq
1330: 6c 69 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 lite3OsRandomnes
1340: 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 s(sqlite3_vfs *p
1350: 56 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 Vfs, int nByte,
1360: 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a char *zBufOut){.
1370: 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 return pVfs->x
1380: 52 61 6e 64 6f 6d 6e 65 73 73 28 70 56 66 73 2c Randomness(pVfs,
1390: 20 6e 42 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 nByte, zBufOut)
13a0: 3b 0a 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33 4f ;.}.int sqlite3O
13b0: 73 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 sSleep(sqlite3_v
13c0: 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 4d fs *pVfs, int nM
13d0: 69 63 72 6f 29 7b 0a 20 20 72 65 74 75 72 6e 20 icro){. return
13e0: 70 56 66 73 2d 3e 78 53 6c 65 65 70 28 70 56 66 pVfs->xSleep(pVf
13f0: 73 2c 20 6e 4d 69 63 72 6f 29 3b 0a 7d 0a 69 6e s, nMicro);.}.in
1400: 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 t sqlite3OsCurre
1410: 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 ntTime(sqlite3_v
1420: 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 fs *pVfs, double
1430: 20 2a 70 54 69 6d 65 4f 75 74 29 7b 0a 20 20 72 *pTimeOut){. r
1440: 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 43 75 72 eturn pVfs->xCur
1450: 72 65 6e 74 54 69 6d 65 28 70 56 66 73 2c 20 70 rentTime(pVfs, p
1460: 54 69 6d 65 4f 75 74 29 3b 0a 7d 0a 0a 69 6e 74 TimeOut);.}..int
1470: 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 sqlite3OsOpenMa
1480: 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 74 65 33 5f lloc(. sqlite3_
1490: 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f vfs *pVfs, . co
14a0: 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c nst char *zFile,
14b0: 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 . sqlite3_file
14c0: 20 2a 2a 70 70 46 69 6c 65 2c 20 0a 20 20 69 6e **ppFile, . in
14d0: 74 20 66 6c 61 67 73 2c 0a 20 20 69 6e 74 20 2a t flags,. int *
14e0: 70 4f 75 74 46 6c 61 67 73 0a 29 7b 0a 20 20 69 pOutFlags.){. i
14f0: 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e nt rc = SQLITE_N
1500: 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69 74 65 33 5f OMEM;. sqlite3_
1510: 66 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20 70 file *pFile;. p
1520: 46 69 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 5f File = (sqlite3_
1530: 66 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d 61 file *)sqlite3Ma
1540: 6c 6c 6f 63 28 70 56 66 73 2d 3e 73 7a 4f 73 46 lloc(pVfs->szOsF
1550: 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46 69 6c ile);. if( pFil
1560: 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 e ){. rc = sq
1570: 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 lite3OsOpen(pVfs
1580: 2c 20 7a 46 69 6c 65 2c 20 70 46 69 6c 65 2c 20 , zFile, pFile,
1590: 66 6c 61 67 73 2c 20 70 4f 75 74 46 6c 61 67 73 flags, pOutFlags
15a0: 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
15b0: 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
15c0: 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
15d0: 46 69 6c 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 File);. }else
15e0: 7b 0a 20 20 20 20 20 20 2a 70 70 46 69 6c 65 20 {. *ppFile
15f0: 3d 20 70 46 69 6c 65 3b 0a 20 20 20 20 7d 0a 20 = pFile;. }.
1600: 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1610: 7d 0a 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 }.int sqlite3OsC
1620: 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74 65 33 loseFree(sqlite3
1630: 5f 66 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 _file *pFile){.
1640: 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1650: 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 _OK;. assert( p
1660: 46 69 6c 65 20 29 3b 0a 20 20 72 63 20 3d 20 73 File );. rc = s
1670: 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 46 qlite3OsClose(pF
1680: 69 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f ile);. sqlite3_
1690: 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20 20 72 free(pFile);. r
16a0: 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
16b0: 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
16c0: 20 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72 is a wrapper ar
16d0: 6f 75 6e 64 20 74 68 65 20 4f 53 20 73 70 65 63 ound the OS spec
16e0: 69 66 69 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74 ific implementat
16f0: 69 6f 6e 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 ion of.** sqlite
1700: 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 54 68 65 3_os_init(). The
1710: 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20 purpose of the
1720: 77 72 61 70 70 65 72 20 69 73 20 74 6f 20 70 72 wrapper is to pr
1730: 6f 76 69 64 65 20 74 68 65 0a 2a 2a 20 61 62 69 ovide the.** abi
1740: 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 lity to simulate
1750: 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 a malloc failur
1760: 65 2c 20 73 6f 20 74 68 61 74 20 74 68 65 20 68 e, so that the h
1770: 61 6e 64 6c 69 6e 67 20 6f 66 20 61 6e 0a 2a 2a andling of an.**
1780: 20 65 72 72 6f 72 20 69 6e 20 73 71 6c 69 74 65 error in sqlite
1790: 33 5f 6f 73 5f 69 6e 69 74 28 29 20 62 79 20 74 3_os_init() by t
17a0: 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 73 20 he upper layers
17b0: 63 61 6e 20 62 65 20 74 65 73 74 65 64 2e 0a 2a can be tested..*
17c0: 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 49 /.int sqlite3OsI
17d0: 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 76 6f 69 nit(void){. voi
17e0: 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d d *p = sqlite3_m
17f0: 61 6c 6c 6f 63 28 31 30 29 3b 0a 20 20 69 66 28 alloc(10);. if(
1800: 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 p==0 ) return S
1810: 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 QLITE_NOMEM;. s
1820: 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
1830: 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
1840: 5f 6f 73 5f 69 6e 69 74 28 29 3b 0a 7d 0a 0a 2f _os_init();.}../
1850: 2a 0a 2a 2a 20 54 68 65 20 6c 69 73 74 20 6f 66 *.** The list of
1860: 20 61 6c 6c 20 72 65 67 69 73 74 65 72 65 64 20 all registered
1870: 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 VFS implementati
1880: 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 ons..*/.static s
1890: 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 53 51 4c qlite3_vfs * SQL
18a0: 49 54 45 5f 57 53 44 20 76 66 73 4c 69 73 74 20 ITE_WSD vfsList
18b0: 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 76 66 73 = 0;.#define vfs
18c0: 4c 69 73 74 20 47 4c 4f 42 41 4c 28 73 71 6c 69 List GLOBAL(sqli
18d0: 74 65 33 5f 76 66 73 20 2a 2c 20 76 66 73 4c 69 te3_vfs *, vfsLi
18e0: 73 74 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 st)../*.** Locat
18f0: 65 20 61 20 56 46 53 20 62 79 20 6e 61 6d 65 2e e a VFS by name.
1900: 20 20 49 66 20 6e 6f 20 6e 61 6d 65 20 69 73 20 If no name is
1910: 67 69 76 65 6e 2c 20 73 69 6d 70 6c 79 20 72 65 given, simply re
1920: 74 75 72 6e 20 74 68 65 0a 2a 2a 20 66 69 72 73 turn the.** firs
1930: 74 20 56 46 53 20 6f 6e 20 74 68 65 20 6c 69 73 t VFS on the lis
1940: 74 2e 0a 2a 2f 0a 73 71 6c 69 74 65 33 5f 76 66 t..*/.sqlite3_vf
1950: 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 s *sqlite3_vfs_f
1960: 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ind(const char *
1970: 7a 56 66 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 zVfs){. sqlite3
1980: 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 30 3b 0a _vfs *pVfs = 0;.
1990: 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
19a0: 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f DSAFE. sqlite3_
19b0: 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 23 65 mutex *mutex;.#e
19c0: 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c ndif.#ifndef SQL
19d0: 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 ITE_OMIT_AUTOINI
19e0: 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c T. int rc = sql
19f0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
1a00: 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 );. if( rc ) re
1a10: 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 23 turn 0;.#endif.#
1a20: 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
1a30: 53 41 46 45 0a 20 20 6d 75 74 65 78 20 3d 20 73 SAFE. mutex = s
1a40: 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
1a50: 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
1a60: 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 ATIC_MASTER);.#e
1a70: 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d ndif. sqlite3_m
1a80: 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 utex_enter(mutex
1a90: 29 3b 0a 20 20 66 6f 72 28 70 56 66 73 20 3d 20 );. for(pVfs =
1aa0: 76 66 73 4c 69 73 74 3b 20 70 56 66 73 3b 20 70 vfsList; pVfs; p
1ab0: 56 66 73 3d 70 56 66 73 2d 3e 70 4e 65 78 74 29 Vfs=pVfs->pNext)
1ac0: 7b 0a 20 20 20 20 69 66 28 20 7a 56 66 73 3d 3d {. if( zVfs==
1ad0: 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 0 ) break;. i
1ae0: 66 28 20 73 74 72 63 6d 70 28 7a 56 66 73 2c 20 f( strcmp(zVfs,
1af0: 70 56 66 73 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 pVfs->zName)==0
1b00: 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 ) break;. }. s
1b10: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1b20: 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 ve(mutex);. ret
1b30: 75 72 6e 20 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a urn pVfs;.}../*.
1b40: 2a 2a 20 55 6e 6c 69 6e 6b 20 61 20 56 46 53 20 ** Unlink a VFS
1b50: 66 72 6f 6d 20 74 68 65 20 6c 69 6e 6b 65 64 20 from the linked
1b60: 6c 69 73 74 0a 2a 2f 0a 73 74 61 74 69 63 20 76 list.*/.static v
1b70: 6f 69 64 20 76 66 73 55 6e 6c 69 6e 6b 28 73 71 oid vfsUnlink(sq
1b80: 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 29 lite3_vfs *pVfs)
1b90: 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
1ba0: 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 te3_mutex_held(s
1bb0: 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
1bc0: 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
1bd0: 41 54 49 43 5f 4d 41 53 54 45 52 29 29 20 29 3b ATIC_MASTER)) );
1be0: 0a 20 20 69 66 28 20 70 56 66 73 3d 3d 30 20 29 . if( pVfs==0 )
1bf0: 7b 0a 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 20 2a {. /* No-op *
1c00: 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76 66 /. }else if( vf
1c10: 73 4c 69 73 74 3d 3d 70 56 66 73 20 29 7b 0a 20 sList==pVfs ){.
1c20: 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56 66 vfsList = pVf
1c30: 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 s->pNext;. }els
1c40: 65 20 69 66 28 20 76 66 73 4c 69 73 74 20 29 7b e if( vfsList ){
1c50: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 . sqlite3_vfs
1c60: 20 2a 70 20 3d 20 76 66 73 4c 69 73 74 3b 0a 20 *p = vfsList;.
1c70: 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 4e 65 while( p->pNe
1c80: 78 74 20 26 26 20 70 2d 3e 70 4e 65 78 74 21 3d xt && p->pNext!=
1c90: 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70 20 pVfs ){. p
1ca0: 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 = p->pNext;.
1cb0: 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4e 65 }. if( p->pNe
1cc0: 78 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 xt==pVfs ){.
1cd0: 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66 p->pNext = pVf
1ce0: 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a s->pNext;. }.
1cf0: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 }.}../*.** Reg
1d00: 69 73 74 65 72 20 61 20 56 46 53 20 77 69 74 68 ister a VFS with
1d10: 20 74 68 65 20 73 79 73 74 65 6d 2e 20 20 49 74 the system. It
1d20: 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f 20 is harmless to
1d30: 72 65 67 69 73 74 65 72 20 74 68 65 20 73 61 6d register the sam
1d40: 65 0a 2a 2a 20 56 46 53 20 6d 75 6c 74 69 70 6c e.** VFS multipl
1d50: 65 20 74 69 6d 65 73 2e 20 20 54 68 65 20 6e 65 e times. The ne
1d60: 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 68 w VFS becomes th
1d70: 65 20 64 65 66 61 75 6c 74 20 69 66 20 6d 61 6b e default if mak
1d80: 65 44 66 6c 74 20 69 73 0a 2a 2a 20 74 72 75 65 eDflt is.** true
1d90: 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
1da0: 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 73 71 _vfs_register(sq
1db0: 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
1dc0: 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 7b 0a int makeDflt){.
1dd0: 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
1de0: 2a 6d 75 74 65 78 20 3d 20 30 3b 0a 23 69 66 6e *mutex = 0;.#ifn
1df0: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1e00: 41 55 54 4f 49 4e 49 54 0a 20 20 69 6e 74 20 72 AUTOINIT. int r
1e10: 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 c = sqlite3_init
1e20: 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 ialize();. if(
1e30: 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
1e40: 23 65 6e 64 69 66 0a 20 20 6d 75 74 65 78 20 3d #endif. mutex =
1e50: 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
1e60: 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
1e70: 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a STATIC_MASTER);.
1e80: 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1e90: 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 enter(mutex);.
1ea0: 76 66 73 55 6e 6c 69 6e 6b 28 70 56 66 73 29 3b vfsUnlink(pVfs);
1eb0: 0a 20 20 69 66 28 20 6d 61 6b 65 44 66 6c 74 20 . if( makeDflt
1ec0: 7c 7c 20 76 66 73 4c 69 73 74 3d 3d 30 20 29 7b || vfsList==0 ){
1ed0: 0a 20 20 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 . pVfs->pNext
1ee0: 20 3d 20 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 = vfsList;.
1ef0: 76 66 73 4c 69 73 74 20 3d 20 70 56 66 73 3b 0a vfsList = pVfs;.
1f00: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 56 66 }else{. pVf
1f10: 73 2d 3e 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 s->pNext = vfsLi
1f20: 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 76 st->pNext;. v
1f30: 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 fsList->pNext =
1f40: 70 56 66 73 3b 0a 20 20 7d 0a 20 20 61 73 73 65 pVfs;. }. asse
1f50: 72 74 28 76 66 73 4c 69 73 74 29 3b 0a 20 20 73 rt(vfsList);. s
1f60: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1f70: 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 ve(mutex);. ret
1f80: 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1f90: 0a 0a 2f 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 74 ../*.** Unregist
1fa0: 65 72 20 61 20 56 46 53 20 73 6f 20 74 68 61 74 er a VFS so that
1fb0: 20 69 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 it is no longer
1fc0: 20 61 63 63 65 73 73 69 62 6c 65 2e 0a 2a 2f 0a accessible..*/.
1fd0: 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f int sqlite3_vfs_
1fe0: 75 6e 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 unregister(sqlit
1ff0: 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 23 e3_vfs *pVfs){.#
2000: 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
2010: 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d SAFE. sqlite3_m
2020: 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 utex *mutex = sq
2030: 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
2040: 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
2050: 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e TIC_MASTER);.#en
2060: 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 dif. sqlite3_mu
2070: 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 tex_enter(mutex)
2080: 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 ;. vfsUnlink(pV
2090: 66 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d fs);. sqlite3_m
20a0: 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 utex_leave(mutex
20b0: 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
20c0: 54 45 5f 4f 4b 3b 0a 7d 0a TE_OK;.}.