0000: 65 78 74 65 72 6e 20 22 43 22 0a 7b 0a 2f 2a 0a extern "C".{./*.
0010: 2a 2a 20 32 30 30 35 20 41 70 72 69 6c 20 31 20 ** 2005 April 1
0020: 2d 20 4e 75 6e 6f 20 4c 75 63 61 73 0a 2a 2a 0a - Nuno Lucas.**.
0030: 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
0040: 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
0050: 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
0060: 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
0070: 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
0080: 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
0090: 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
00a0: 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
00b0: 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
00c0: 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
00d0: 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
00e0: 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
00f0: 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
0100: 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
0110: 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
0120: 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
0130: 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
0140: 2a 0a 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 2a 2a 2a 2a 2a 2a ****************
0180: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0190: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
01a0: 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 contains code t
01b0: 68 61 74 20 69 73 20 73 70 65 63 69 66 69 63 20 hat is specific
01c0: 74 6f 20 57 69 6e 64 6f 77 73 20 43 45 2e 0a 2a to Windows CE..*
01d0: 2f 0a 23 69 6e 63 6c 75 64 65 20 22 73 71 6c 69 /.#include "sqli
01e0: 74 65 49 6e 74 2e 68 22 0a 23 69 6e 63 6c 75 64 teInt.h".#includ
01f0: 65 20 22 6f 73 2e 68 22 20 20 20 20 20 20 20 20 e "os.h"
0200: 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 66 69 72 /* Must be fir
0210: 73 74 20 74 6f 20 65 6e 61 62 6c 65 20 6c 61 72 st to enable lar
0220: 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 ge file support
0230: 2a 2f 0a 23 69 66 64 65 66 20 5f 57 49 4e 33 32 */.#ifdef _WIN32
0240: 5f 57 43 45 20 20 20 20 20 20 20 20 2f 2a 20 54 _WCE /* T
0250: 68 69 73 20 66 69 6c 65 20 69 73 20 75 73 65 64 his file is used
0260: 20 66 6f 72 20 57 69 6e 64 6f 77 73 20 43 45 20 for Windows CE
0270: 6f 6e 6c 79 20 2a 2f 0a 23 69 6e 63 6c 75 64 65 only */.#include
0280: 20 3c 74 69 6d 65 2e 68 3e 0a 0a 2f 2a 0a 2a 2a <time.h>../*.**
0290: 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68 Include code th
02a0: 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 at is common to
02b0: 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 all os_*.c files
02c0: 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 22 6f 73 .*/.#include "os
02d0: 5f 63 6f 6d 6d 6f 6e 2e 68 22 0a 0a 0a 2f 2a 0a _common.h".../*.
02e0: 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 **.** Implementa
02f0: 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 73 73 65 tion of the asse
0300: 72 74 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 rt function for
0310: 73 79 73 74 65 6d 73 20 6e 6f 74 20 68 61 76 69 systems not havi
0320: 6e 67 20 69 74 0a 2a 2a 0a 2a 2a 20 56 65 72 79 ng it.**.** Very
0330: 20 62 61 73 69 63 2c 20 6a 75 73 74 20 6f 70 65 basic, just ope
0340: 6e 73 20 61 20 6d 65 73 73 61 67 65 20 62 6f 78 ns a message box
0350: 20 64 69 73 70 6c 61 79 69 6e 67 20 77 68 65 72 displaying wher
0360: 65 20 61 6e 64 20 77 68 61 74 20 66 69 72 65 64 e and what fired
0370: 20 74 68 65 0a 2a 2a 20 74 68 65 20 61 73 73 65 the.** the asse
0380: 72 74 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 76 rt failure..*/.v
0390: 6f 69 64 20 73 71 6c 69 74 65 77 63 65 5f 61 73 oid sqlitewce_as
03a0: 73 65 72 74 28 20 69 6e 74 20 78 2c 20 63 68 61 sert( int x, cha
03b0: 72 20 2a 20 74 65 73 74 2c 20 63 68 61 72 20 2a r * test, char *
03c0: 20 66 69 6c 65 2c 20 69 6e 74 20 6c 69 6e 65 20 file, int line
03d0: 29 0a 7b 0a 09 2f 2a 20 54 68 69 73 20 73 68 6f ).{../* This sho
03e0: 75 6c 64 20 62 65 20 66 69 78 65 64 20 73 6f 6d uld be fixed som
03f0: 65 68 6f 77 2c 20 74 6f 20 61 76 6f 69 64 20 6f ehow, to avoid o
0400: 76 65 72 66 6c 6f 77 73 2e 0a 09 20 2a 20 41 6c verflows... * Al
0410: 73 6f 2c 20 77 68 65 6e 20 61 6e 20 61 73 73 65 so, when an asse
0420: 72 74 20 69 73 20 63 61 75 73 65 64 20 62 79 20 rt is caused by
0430: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
0440: 6e 20 66 61 69 6c 6c 75 72 65 2c 20 74 68 69 73 n faillure, this
0450: 0a 09 20 2a 20 77 69 6c 6c 20 70 72 6f 62 61 62 .. * will probab
0460: 6c 79 20 66 61 69 6c 2e 0a 09 20 2a 2f 0a 09 57 ly fail... */..W
0470: 43 48 41 52 20 62 75 66 5b 32 30 34 38 5d 3b 0a CHAR buf[2048];.
0480: 09 69 66 20 28 78 29 20 72 65 74 75 72 6e 3b 0a .if (x) return;.
0490: 09 73 77 70 72 69 6e 74 66 28 20 62 75 66 2c 20 .swprintf( buf,
04a0: 4c 22 61 73 73 65 72 74 28 20 25 68 73 20 29 5c L"assert( %hs )\
04b0: 72 5c 6e 5c 72 5c 6e 46 69 6c 65 3a 20 27 25 68 r\n\r\nFile: '%h
04c0: 73 27 20 20 4c 69 6e 65 3a 20 25 64 22 2c 20 74 s' Line: %d", t
04d0: 65 73 74 2c 20 66 69 6c 65 2c 20 6c 69 6e 65 20 est, file, line
04e0: 29 3b 0a 09 4d 65 73 73 61 67 65 42 6f 78 57 28 );..MessageBoxW(
04f0: 20 30 2c 20 62 75 66 2c 20 4c 22 41 73 73 65 72 0, buf, L"Asser
0500: 74 69 6f 6e 20 45 72 72 6f 72 22 2c 20 4d 42 5f tion Error", MB_
0510: 49 43 4f 4e 45 52 52 4f 52 20 29 3b 0a 7d 0a 0a ICONERROR );.}..
0520: 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 /*.** Implementa
0530: 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 63 61 tion of the loca
0540: 6c 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e 20 66 ltime function f
0550: 6f 72 20 73 79 73 74 65 6d 73 20 6e 6f 74 20 68 or systems not h
0560: 61 76 69 6e 67 20 69 74 2e 0a 2a 2a 20 43 6f 6e aving it..** Con
0570: 76 65 72 74 20 74 69 6d 65 5f 74 20 74 6f 20 6c vert time_t to l
0580: 6f 63 61 6c 20 74 69 6d 65 20 69 6e 20 74 6d 20 ocal time in tm
0590: 73 74 72 75 63 74 20 66 6f 72 6d 61 74 2e 0a 2a struct format..*
05a0: 2f 0a 73 74 72 75 63 74 20 74 6d 20 2a 20 73 71 /.struct tm * sq
05b0: 6c 69 74 65 77 63 65 5f 6c 6f 63 61 6c 74 69 6d litewce_localtim
05c0: 65 28 20 63 6f 6e 73 74 20 74 69 6d 65 5f 74 20 e( const time_t
05d0: 2a 74 69 6d 65 72 20 29 0a 7b 0a 09 73 74 61 74 *timer ).{..stat
05e0: 69 63 20 73 74 72 75 63 74 20 74 6d 20 73 5f 74 ic struct tm s_t
05f0: 6d 3b 0a 09 46 49 4c 45 54 49 4d 45 09 75 66 2c m;..FILETIME.uf,
0600: 20 6c 66 3b 0a 09 53 59 53 54 45 4d 54 49 4d 45 lf;..SYSTEMTIME
0610: 09 6c 73 3b 0a 09 2f 2f 20 43 6f 6e 76 65 72 74 .ls;..// Convert
0620: 20 74 69 6d 65 5f 74 20 74 6f 20 46 49 4c 45 54 time_t to FILET
0630: 49 4d 45 0a 09 75 6e 73 69 67 6e 65 64 20 5f 5f IME..unsigned __
0640: 69 6e 74 36 34 20 69 36 34 20 3d 20 49 6e 74 33 int64 i64 = Int3
0650: 32 78 33 32 54 6f 36 34 28 74 69 6d 65 72 2c 20 2x32To64(timer,
0660: 31 30 30 30 30 30 30 30 29 20 2b 20 31 31 36 34 10000000) + 1164
0670: 34 34 37 33 36 30 30 30 30 30 30 30 30 30 3b 0a 44736000000000;.
0680: 09 75 66 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d .uf.dwLowDateTim
0690: 65 20 3d 20 28 44 57 4f 52 44 29 20 69 36 34 3b e = (DWORD) i64;
06a0: 0a 09 75 66 2e 64 77 48 69 67 68 44 61 74 65 54 ..uf.dwHighDateT
06b0: 69 6d 65 20 3d 20 28 44 57 4f 52 44 29 20 28 69 ime = (DWORD) (i
06c0: 36 34 20 3e 3e 20 33 32 29 3b 0a 09 2f 2f 20 43 64 >> 32);..// C
06d0: 6f 6e 76 65 72 74 20 55 54 43 28 47 4d 54 29 20 onvert UTC(GMT)
06e0: 46 49 4c 45 54 49 4d 45 20 74 6f 20 6c 6f 63 61 FILETIME to loca
06f0: 6c 20 46 49 4c 45 54 49 4d 45 0a 09 46 69 6c 65 l FILETIME..File
0700: 54 69 6d 65 54 6f 4c 6f 63 61 6c 46 69 6c 65 54 TimeToLocalFileT
0710: 69 6d 65 28 20 26 75 66 2c 20 26 6c 66 20 29 3b ime( &uf, &lf );
0720: 0a 09 2f 2f 20 43 6f 6e 76 65 72 74 20 46 49 4c ..// Convert FIL
0730: 45 54 49 4d 45 20 74 6f 20 53 59 53 54 45 4d 54 ETIME to SYSTEMT
0740: 49 4d 45 0a 09 46 69 6c 65 54 69 6d 65 54 6f 53 IME..FileTimeToS
0750: 79 73 74 65 6d 54 69 6d 65 28 20 26 6c 66 2c 20 ystemTime( &lf,
0760: 26 6c 73 20 29 3b 0a 09 2f 2f 20 43 6f 6e 76 65 &ls );..// Conve
0770: 72 74 20 53 59 53 54 45 4d 54 49 4d 45 20 74 6f rt SYSTEMTIME to
0780: 20 74 6d 0a 09 73 5f 74 6d 2e 74 6d 5f 73 65 63 tm..s_tm.tm_sec
0790: 20 20 3d 20 6c 73 2e 77 53 65 63 6f 6e 64 3b 0a = ls.wSecond;.
07a0: 09 73 5f 74 6d 2e 74 6d 5f 6d 69 6e 20 20 3d 20 .s_tm.tm_min =
07b0: 6c 73 2e 77 4d 69 6e 75 74 65 3b 0a 09 73 5f 74 ls.wMinute;..s_t
07c0: 6d 2e 74 6d 5f 68 6f 75 72 20 3d 20 6c 73 2e 77 m.tm_hour = ls.w
07d0: 48 6f 75 72 3b 0a 09 73 5f 74 6d 2e 74 6d 5f 6d Hour;..s_tm.tm_m
07e0: 64 61 79 20 3d 20 6c 73 2e 77 44 61 79 3b 0a 09 day = ls.wDay;..
07f0: 73 5f 74 6d 2e 74 6d 5f 6d 6f 6e 20 20 3d 20 6c s_tm.tm_mon = l
0800: 73 2e 77 4d 6f 6e 74 68 20 2d 31 3b 0a 09 73 5f s.wMonth -1;..s_
0810: 74 6d 2e 74 6d 5f 79 65 61 72 20 3d 20 6c 73 2e tm.tm_year = ls.
0820: 77 59 65 61 72 20 2d 20 31 39 30 30 3b 0a 09 73 wYear - 1900;..s
0830: 5f 74 6d 2e 74 6d 5f 77 64 61 79 20 3d 20 6c 73 _tm.tm_wday = ls
0840: 2e 77 44 61 79 4f 66 57 65 65 6b 3b 0a 09 2f 2f .wDayOfWeek;..//
0850: 20 52 65 74 75 72 6e 20 70 6f 69 6e 74 65 72 20 Return pointer
0860: 74 6f 20 73 74 61 74 69 63 20 64 61 74 61 0a 09 to static data..
0870: 72 65 74 75 72 6e 20 26 73 5f 74 6d 3b 0a 7d 0a return &s_tm;.}.
0880: 0a 2f 2a 0a 2a 2a 20 53 69 6d 69 6c 61 72 20 74 ./*.** Similar t
0890: 6f 20 73 74 72 64 75 70 2c 20 62 75 74 20 66 69 o strdup, but fi
08a0: 72 73 74 20 63 6f 6e 76 65 72 74 73 20 74 68 65 rst converts the
08b0: 20 4d 42 43 53 20 73 74 72 69 6e 67 20 74 6f 20 MBCS string to
08c0: 55 4e 49 43 4f 44 45 0a 2a 2a 20 61 6e 64 20 74 UNICODE.** and t
08d0: 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 hen returns the
08e0: 55 4e 49 43 4f 44 45 20 63 6c 6f 6e 65 2e 0a 2a UNICODE clone..*
08f0: 2a 20 44 6f 6e 27 74 20 66 6f 72 67 65 74 20 74 * Don't forget t
0900: 6f 20 66 72 65 65 28 29 20 74 68 65 20 72 65 74 o free() the ret
0910: 75 72 6e 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a urned string..**
0920: 20 49 20 61 73 73 75 6d 65 20 61 20 32 20 62 79 I assume a 2 by
0930: 74 65 20 73 69 7a 65 20 70 65 72 20 63 68 61 72 te size per char
0940: 61 63 74 65 72 20 66 6f 72 20 75 6e 69 63 6f 64 acter for unicod
0950: 65 2e 20 54 68 61 74 27 73 20 77 68 61 74 20 77 e. That's what w
0960: 69 6e 64 6f 77 73 0a 2a 2a 20 74 68 69 6e 6b 73 indows.** thinks
0970: 20 75 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 73 unicode strings
0980: 20 61 72 65 20 28 65 78 70 65 63 74 20 68 61 76 are (expect hav
0990: 69 6e 67 20 74 6f 20 63 68 61 6e 67 65 20 74 68 ing to change th
09a0: 69 73 20 69 6e 20 32 30 31 30 20 3b 29 0a 2a 2f is in 2010 ;).*/
09b0: 0a 73 74 61 74 69 63 20 57 43 48 41 52 20 2a 20 .static WCHAR *
09c0: 53 74 72 44 75 70 57 28 20 63 6f 6e 73 74 20 63 StrDupW( const c
09d0: 68 61 72 20 2a 20 73 74 72 20 29 0a 7b 0a 09 73 har * str ).{..s
09e0: 69 7a 65 5f 74 20 73 69 7a 65 20 3d 20 73 74 72 ize_t size = str
09f0: 6c 65 6e 28 73 74 72 29 20 2b 20 31 3b 20 2f 2f len(str) + 1; //
0a00: 20 2b 31 20 66 6f 72 20 74 65 72 6d 69 6e 61 74 +1 for terminat
0a10: 69 6e 67 20 27 5c 30 27 0a 09 57 43 48 41 52 20 ing '\0'..WCHAR
0a20: 2a 20 61 75 78 20 3d 20 28 57 43 48 41 52 20 2a * aux = (WCHAR *
0a30: 29 20 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 2a 73 ) malloc( size*s
0a40: 69 7a 65 6f 66 28 57 43 48 41 52 29 20 29 3b 0a izeof(WCHAR) );.
0a50: 09 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 .MultiByteToWide
0a60: 43 68 61 72 28 20 43 50 5f 41 43 50 2c 20 30 2c Char( CP_ACP, 0,
0a70: 20 73 74 72 2c 2d 31 2c 20 61 75 78 2c 20 73 69 str,-1, aux, si
0a80: 7a 65 20 29 3b 0a 09 72 65 74 75 72 6e 20 61 75 ze );..return au
0a90: 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 69 6e 64 x;.}../*.** Wind
0aa0: 6f 77 73 20 43 45 20 76 65 72 73 69 6f 6e 73 20 ows CE versions
0ab0: 70 72 69 6f 72 20 74 6f 20 33 2e 30 20 64 6f 6e prior to 3.0 don
0ac0: 27 74 20 69 6d 70 6c 65 6d 65 6e 74 20 61 74 6f 't implement ato
0ad0: 66 28 29 2c 20 73 6f 20 77 65 0a 2a 2a 20 69 6d f(), so we.** im
0ae0: 70 6c 65 6d 65 6e 74 20 69 74 20 68 65 72 65 20 plement it here
0af0: 61 73 20 61 20 77 72 61 70 70 65 72 20 74 6f 20 as a wrapper to
0b00: 77 63 73 74 6f 64 28 29 2e 0a 2a 2f 0a 64 6f 75 wcstod()..*/.dou
0b10: 62 6c 65 20 73 71 6c 69 74 65 77 63 65 5f 61 74 ble sqlitewce_at
0b20: 6f 66 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a of( const char *
0b30: 73 74 72 20 29 0a 7b 0a 09 77 63 68 61 72 5f 74 str ).{..wchar_t
0b40: 20 2a 20 61 75 78 20 3d 20 53 74 72 44 75 70 57 * aux = StrDupW
0b50: 28 20 73 74 72 20 29 3b 0a 09 64 6f 75 62 6c 65 ( str );..double
0b60: 20 64 20 3d 20 77 63 73 74 6f 64 28 20 61 75 78 d = wcstod( aux
0b70: 2c 20 4e 55 4c 4c 20 29 3b 0a 09 66 72 65 65 28 , NULL );..free(
0b80: 20 61 75 78 20 29 3b 0a 09 72 65 74 75 72 6e 20 aux );..return
0b90: 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 d;.}../*.** This
0ba0: 20 69 73 20 6e 65 65 64 65 64 20 66 6f 72 20 74 is needed for t
0bb0: 68 65 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 20 he command line
0bc0: 76 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 version of sqlit
0bd0: 65 20 74 6f 20 63 6f 6d 70 69 6c 65 2e 0a 2a 2a e to compile..**
0be0: 2f 0a 69 6e 74 20 69 73 61 74 74 79 28 20 69 6e /.int isatty( in
0bf0: 74 20 68 61 6e 64 6c 65 20 29 0a 7b 0a 09 55 4e t handle ).{..UN
0c00: 52 45 46 45 52 45 4e 43 45 44 5f 50 41 52 41 4d REFERENCED_PARAM
0c10: 45 54 45 52 28 68 61 6e 64 6c 65 29 3b 0a 09 72 ETER(handle);..r
0c20: 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 1;.}../*.*
0c30: 2a 20 43 6f 6e 76 65 72 74 73 20 61 20 72 65 6c * Converts a rel
0c40: 61 74 69 76 65 20 70 61 74 68 20 74 6f 20 61 6e ative path to an
0c50: 20 61 62 73 6f 6c 75 74 65 20 70 61 74 68 2e 0a absolute path..
0c60: 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 63 ** There is no c
0c70: 75 72 72 65 6e 74 20 64 69 72 65 63 74 6f 72 79 urrent directory
0c80: 20 63 6f 6e 63 65 70 74 20 6f 6e 20 57 69 6e 64 concept on Wind
0c90: 6f 77 73 20 43 45 2c 20 73 6f 20 77 65 20 61 73 ows CE, so we as
0ca0: 73 75 6d 65 0a 2a 2a 20 77 65 20 61 72 65 20 77 sume.** we are w
0cb0: 6f 72 6b 69 6e 67 20 61 6c 77 61 79 73 20 77 69 orking always wi
0cc0: 74 68 20 61 62 73 6f 6c 75 74 65 20 70 61 74 68 th absolute path
0cd0: 73 20 61 6e 64 20 73 69 6d 70 6c 79 20 63 6f 70 s and simply cop
0ce0: 79 0a 2a 2a 20 74 68 65 20 67 69 76 65 6e 20 70 y.** the given p
0cf0: 61 74 68 20 74 6f 20 74 68 65 20 70 72 6f 76 69 ath to the provi
0d00: 64 65 64 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 44 ded buffer..*/.D
0d10: 57 4f 52 44 20 73 71 6c 69 74 65 77 63 65 5f 47 WORD sqlitewce_G
0d20: 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 0a etFullPathNameA.
0d30: 09 28 0a 09 09 4c 50 43 53 54 52 09 6c 70 46 69 .(...LPCSTR.lpFi
0d40: 6c 65 4e 61 6d 65 2c 0a 09 09 44 57 4f 52 44 09 leName,...DWORD.
0d50: 6e 42 75 66 66 65 72 4c 65 6e 67 74 68 2c 0a 09 nBufferLength,..
0d60: 09 4c 50 53 54 52 09 6c 70 42 75 66 66 65 72 2c .LPSTR.lpBuffer,
0d70: 0a 09 09 4c 50 53 54 52 20 2a 09 6c 70 46 69 6c ...LPSTR *.lpFil
0d80: 65 50 61 72 74 0a 09 29 0a 7b 0a 09 44 57 4f 52 ePart..).{..DWOR
0d90: 44 20 69 20 3d 20 30 3b 0a 09 66 6f 72 20 28 20 D i = 0;..for (
0da0: 3b 20 69 20 3c 20 6e 42 75 66 66 65 72 4c 65 6e ; i < nBufferLen
0db0: 67 74 68 3b 20 2b 2b 69 20 29 0a 09 7b 0a 09 09 gth; ++i )..{...
0dc0: 6c 70 42 75 66 66 65 72 5b 69 5d 20 3d 20 6c 70 lpBuffer[i] = lp
0dd0: 46 69 6c 65 4e 61 6d 65 5b 69 5d 3b 0a 09 09 69 FileName[i];...i
0de0: 66 20 28 20 6c 70 42 75 66 66 65 72 5b 69 5d 20 f ( lpBuffer[i]
0df0: 3d 3d 20 27 5c 5c 27 20 7c 7c 20 6c 70 42 75 66 == '\\' || lpBuf
0e00: 66 65 72 5b 69 5d 20 3d 3d 20 27 2f 27 20 29 0a fer[i] == '/' ).
0e10: 09 09 09 2a 6c 70 46 69 6c 65 50 61 72 74 20 3d ...*lpFilePart =
0e20: 20 6c 70 42 75 66 66 65 72 20 2b 20 69 20 2b 20 lpBuffer + i +
0e30: 31 3b 0a 09 09 69 66 20 28 20 6c 70 42 75 66 66 1;...if ( lpBuff
0e40: 65 72 5b 69 5d 20 3d 3d 20 27 5c 30 27 20 29 0a er[i] == '\0' ).
0e50: 09 09 09 62 72 65 61 6b 3b 0a 09 7d 0a 09 72 65 ...break;..}..re
0e60: 74 75 72 6e 20 28 69 20 3e 3d 20 6e 42 75 66 66 turn (i >= nBuff
0e70: 65 72 4c 65 6e 67 74 68 29 3f 20 73 74 72 6c 65 erLength)? strle
0e80: 6e 28 6c 70 46 69 6c 65 4e 61 6d 65 29 20 2b 20 n(lpFileName) +
0e90: 31 20 3a 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 1 : i;.}../*.**
0ea0: 53 69 6d 70 6c 65 20 77 72 61 70 70 65 72 20 74 Simple wrapper t
0eb0: 6f 20 74 68 65 20 55 6e 69 63 6f 64 65 20 76 65 o the Unicode ve
0ec0: 72 73 69 6f 6e 20 6f 66 20 47 65 74 46 69 6c 65 rsion of GetFile
0ed0: 41 74 74 72 69 62 75 74 65 73 28 29 2e 0a 2a 2f Attributes()..*/
0ee0: 0a 44 57 4f 52 44 20 73 71 6c 69 74 65 77 63 65 .DWORD sqlitewce
0ef0: 5f 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 _GetFileAttribut
0f00: 65 73 41 28 20 4c 50 43 53 54 52 20 6c 70 46 69 esA( LPCSTR lpFi
0f10: 6c 65 4e 61 6d 65 20 29 0a 7b 0a 09 77 63 68 61 leName ).{..wcha
0f20: 72 5f 74 20 2a 20 61 75 78 20 3d 20 53 74 72 44 r_t * aux = StrD
0f30: 75 70 57 28 20 6c 70 46 69 6c 65 4e 61 6d 65 20 upW( lpFileName
0f40: 29 3b 0a 09 44 57 4f 52 44 20 72 65 74 20 3d 20 );..DWORD ret =
0f50: 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 GetFileAttribute
0f60: 73 57 28 20 61 75 78 20 29 3b 0a 09 66 72 65 65 sW( aux );..free
0f70: 28 20 61 75 78 20 29 3b 0a 09 72 65 74 75 72 6e ( aux );..return
0f80: 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 ret;.}../*.** W
0f90: 69 6e 43 45 20 64 6f 65 73 6e 27 74 20 69 6d 70 inCE doesn't imp
0fa0: 6c 65 6d 65 6e 74 20 47 65 74 53 79 73 74 65 6d lement GetSystem
0fb0: 54 69 6d 65 41 73 46 69 6c 65 54 69 6d 65 28 29 TimeAsFileTime()
0fc0: 2c 20 62 75 74 20 69 73 0a 2a 2a 20 74 72 69 76 , but is.** triv
0fd0: 69 61 6c 20 74 6f 20 63 6f 64 65 2e 0a 2a 2f 0a ial to code..*/.
0fe0: 76 6f 69 64 20 73 71 6c 69 74 65 77 63 65 5f 47 void sqlitewce_G
0ff0: 65 74 53 79 73 74 65 6d 54 69 6d 65 41 73 46 69 etSystemTimeAsFi
1000: 6c 65 54 69 6d 65 28 20 4c 50 46 49 4c 45 54 49 leTime( LPFILETI
1010: 4d 45 20 66 74 20 29 0a 7b 0a 09 53 59 53 54 45 ME ft ).{..SYSTE
1020: 4d 54 49 4d 45 20 73 74 3b 0a 09 47 65 74 53 79 MTIME st;..GetSy
1030: 73 74 65 6d 54 69 6d 65 28 20 26 73 74 20 29 3b stemTime( &st );
1040: 0a 09 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 ..SystemTimeToFi
1050: 6c 65 54 69 6d 65 28 20 26 73 74 2c 20 66 74 20 leTime( &st, ft
1060: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 6d 70 );.}../*.** Simp
1070: 6c 65 20 77 72 61 70 70 65 72 20 74 6f 20 74 68 le wrapper to th
1080: 65 20 55 6e 69 63 6f 64 65 20 76 65 72 73 69 6f e Unicode versio
1090: 6e 20 6f 66 20 44 65 6c 65 74 65 46 69 6c 65 28 n of DeleteFile(
10a0: 29 2e 0a 2a 2f 0a 42 4f 4f 4c 20 73 71 6c 69 74 )..*/.BOOL sqlit
10b0: 65 77 63 65 5f 44 65 6c 65 74 65 46 69 6c 65 41 ewce_DeleteFileA
10c0: 28 20 4c 50 43 53 54 52 20 7a 46 69 6c 65 6e 61 ( LPCSTR zFilena
10d0: 6d 65 20 29 0a 7b 0a 09 77 63 68 61 72 5f 74 20 me ).{..wchar_t
10e0: 2a 20 61 75 78 20 3d 20 53 74 72 44 75 70 57 28 * aux = StrDupW(
10f0: 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 09 42 zFilename );..B
1100: 4f 4f 4c 20 72 65 74 20 3d 20 44 65 6c 65 74 65 OOL ret = Delete
1110: 46 69 6c 65 57 28 20 61 75 78 20 29 3b 0a 09 66 FileW( aux );..f
1120: 72 65 65 28 20 61 75 78 20 29 3b 0a 09 72 65 74 ree( aux );..ret
1130: 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a urn ret;.}../*.*
1140: 2a 20 57 72 61 70 70 65 72 20 74 6f 20 74 68 65 * Wrapper to the
1150: 20 55 6e 69 63 6f 64 65 20 76 65 72 73 69 6f 6e Unicode version
1160: 20 6f 66 20 47 65 74 54 65 6d 70 50 61 74 68 28 of GetTempPath(
1170: 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 54 )..**.** NOTE: T
1180: 68 65 20 4d 53 44 4e 20 73 61 79 73 20 47 65 74 he MSDN says Get
1190: 54 65 6d 70 50 61 74 68 28 29 20 63 61 6e 20 66 TempPath() can f
11a0: 61 69 6c 20 69 66 20 6e 6f 20 74 65 6d 70 6f 72 ail if no tempor
11b0: 61 72 79 20 70 61 74 68 0a 2a 2a 20 20 20 20 20 ary path.**
11c0: 20 20 64 65 66 69 6e 65 64 2e 20 4e 6f 20 63 68 defined. No ch
11d0: 65 63 6b 20 66 6f 72 20 74 68 69 73 2c 20 61 73 eck for this, as
11e0: 20 6e 6f 77 20 69 73 20 70 6f 73 73 69 62 6c 65 now is possible
11f0: 20 74 6f 20 64 65 66 69 6e 65 0a 2a 2a 20 20 20 to define.**
1200: 20 20 20 20 61 6e 20 61 6c 74 65 72 6e 61 74 65 an alternate
1210: 20 74 65 6d 70 6f 72 61 72 79 20 70 61 74 68 20 temporary path
1220: 66 6f 72 20 73 71 6c 69 74 65 2e 0a 2a 2f 0a 44 for sqlite..*/.D
1230: 57 4f 52 44 20 73 71 6c 69 74 65 77 63 65 5f 47 WORD sqlitewce_G
1240: 65 74 54 65 6d 70 50 61 74 68 41 28 20 44 57 4f etTempPathA( DWO
1250: 52 44 20 62 75 66 4c 65 6e 2c 20 4c 50 53 54 52 RD bufLen, LPSTR
1260: 20 62 75 66 20 29 0a 7b 0a 09 69 6e 74 20 6c 65 buf ).{..int le
1270: 6e 20 3d 20 47 65 74 54 65 6d 70 50 61 74 68 57 n = GetTempPathW
1280: 28 20 30 2c 30 20 29 3b 0a 09 4c 50 57 53 54 52 ( 0,0 );..LPWSTR
1290: 20 77 54 65 6d 70 50 61 74 68 20 3d 20 28 4c 50 wTempPath = (LP
12a0: 57 53 54 52 29 20 6d 61 6c 6c 6f 63 28 20 28 6c WSTR) malloc( (l
12b0: 65 6e 2b 31 29 2a 73 69 7a 65 6f 66 28 57 43 48 en+1)*sizeof(WCH
12c0: 41 52 29 20 29 3b 0a 09 47 65 74 54 65 6d 70 50 AR) );..GetTempP
12d0: 61 74 68 57 28 20 6c 65 6e 2b 31 2c 20 77 54 65 athW( len+1, wTe
12e0: 6d 70 50 61 74 68 20 29 3b 0a 09 6c 65 6e 20 3d mpPath );..len =
12f0: 20 57 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 WideCharToMulti
1300: 42 79 74 65 28 20 43 50 5f 41 43 50 2c 20 30 2c Byte( CP_ACP, 0,
1310: 20 77 54 65 6d 70 50 61 74 68 2c 2d 31 2c 20 62 wTempPath,-1, b
1320: 75 66 2c 62 75 66 4c 65 6e 2c 20 30 2c 30 20 29 uf,bufLen, 0,0 )
1330: 3b 0a 09 66 72 65 65 28 20 77 54 65 6d 70 50 61 ;..free( wTempPa
1340: 74 68 20 29 3b 0a 09 72 65 74 75 72 6e 20 6c 65 th );..return le
1350: 6e 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a n;.}.../********
1360: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1370: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1380: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1390: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 **************.
13a0: 2a 20 46 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 68 * File locking h
13b0: 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 elper functions
13c0: 66 6f 72 20 57 69 6e 64 6f 77 73 20 43 45 0a 20 for Windows CE.
13d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1400: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1410: 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 *****/..#ifndef
1420: 53 51 4c 49 54 45 5f 57 43 45 5f 4f 4d 49 54 5f SQLITE_WCE_OMIT_
1430: 46 49 4c 45 4c 4f 43 4b 0a 0a 2f 2a 0a 2a 2a 20 FILELOCK../*.**
1440: 53 74 72 75 63 74 75 72 65 20 68 6f 6c 64 69 6e Structure holdin
1450: 67 20 74 68 65 20 67 6c 6f 62 61 6c 20 6c 6f 63 g the global loc
1460: 6b 69 6e 67 20 64 61 74 61 20 66 6f 72 20 65 61 king data for ea
1470: 63 68 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 ch open database
1480: 2f 66 69 6c 65 2e 0a 2a 2a 20 73 71 6c 69 74 65 /file..** sqlite
1490: 77 63 65 5f 4c 6f 63 6b 4d 75 74 65 78 28 29 20 wce_LockMutex()
14a0: 6d 75 73 74 20 62 65 20 75 73 65 64 20 62 65 66 must be used bef
14b0: 6f 72 65 20 75 73 69 6e 67 20 74 68 69 73 20 64 ore using this d
14c0: 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 3c 6c 6f 63 6b ata..**.** <lock
14d0: 3e 20 68 6f 6c 64 73 20 74 68 65 20 67 6c 6f 62 > holds the glob
14e0: 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 20 6f 66 al lock state of
14f0: 20 74 68 65 20 66 69 6c 65 2e 20 45 76 65 72 79 the file. Every
1500: 20 74 69 6d 65 20 61 20 70 72 6f 63 65 73 73 0a time a process.
1510: 2a 2a 09 09 09 68 6f 6c 64 73 20 61 20 6c 6f 63 **...holds a loc
1520: 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 2c 20 31 k on the file, 1
1530: 20 3c 3c 20 6c 6f 63 6b 74 79 70 65 20 69 73 20 << locktype is
1540: 73 65 74 2c 20 69 2e 65 2e 2c 20 62 69 74 20 31 set, i.e., bit 1
1550: 0a 2a 2a 09 09 09 69 66 20 61 6e 79 20 70 72 6f .**...if any pro
1560: 63 65 73 73 20 77 69 74 68 20 61 20 53 48 41 52 cess with a SHAR
1570: 45 44 20 6c 6f 63 6b 2c 20 62 69 74 20 32 20 69 ED lock, bit 2 i
1580: 73 20 73 65 74 20 69 66 20 61 6e 79 20 77 69 74 s set if any wit
1590: 68 0a 2a 2a 09 09 09 61 20 52 45 53 45 52 56 45 h.**...a RESERVE
15a0: 44 20 6c 6f 63 6b 2c 20 62 69 74 20 33 20 66 6f D lock, bit 3 fo
15b0: 72 20 74 68 65 20 50 45 4e 44 49 4e 47 20 6c 6f r the PENDING lo
15c0: 63 6b 20 61 6e 64 20 62 69 74 20 34 20 66 6f 72 ck and bit 4 for
15d0: 0a 2a 2a 09 09 09 74 68 65 20 45 58 43 4c 55 53 .**...the EXCLUS
15e0: 49 56 45 20 6c 6f 63 6b 2e 20 62 69 74 20 30 20 IVE lock. bit 0
15f0: 69 73 20 69 67 6e 6f 72 65 64 2c 20 61 6e 64 20 is ignored, and
1600: 6d 61 79 20 62 65 20 73 65 74 20 6f 72 20 6e 6f may be set or no
1610: 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e t.** in
1620: 20 62 65 74 77 65 65 6e 20 28 69 66 20 69 74 20 between (if it
1630: 73 69 6d 70 6c 69 66 69 65 73 20 74 68 65 20 61 simplifies the a
1640: 6c 67 6f 72 69 74 68 6d 29 2e 0a 2a 2a 20 3c 73 lgorithm)..** <s
1650: 68 61 72 65 64 3e 20 69 73 20 74 68 65 20 63 6f hared> is the co
1660: 75 6e 74 20 6f 66 20 70 72 6f 63 65 73 73 65 73 unt of processes
1670: 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 53 48 41 holding the SHA
1680: 52 45 44 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2f 0a RED lock..**.*/.
1690: 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 typedef struct s
16a0: 71 6c 69 74 65 77 63 65 5f 6c 6f 63 6b 64 61 74 qlitewce_lockdat
16b0: 61 5f 74 0a 7b 0a 09 75 6e 73 69 67 6e 65 64 09 a_t.{..unsigned.
16c0: 6c 6f 63 6b 3b 09 2f 2a 20 67 6c 6f 62 61 6c 20 lock;./* global
16d0: 6c 6f 63 6b 20 73 74 61 74 65 20 2a 2f 0a 09 75 lock state */..u
16e0: 6e 73 69 67 6e 65 64 09 73 68 61 72 65 64 3b 09 nsigned.shared;.
16f0: 2f 2a 20 67 6c 6f 62 61 6c 20 73 68 61 72 65 20 /* global share
1700: 63 6f 75 6e 74 20 2a 2f 0a 7d 20 73 71 6c 69 74 count */.} sqlit
1710: 65 77 63 65 5f 6c 6f 63 6b 64 61 74 61 5f 74 3b ewce_lockdata_t;
1720: 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 61 63 63 ../*.** Lock acc
1730: 65 73 73 20 74 6f 20 66 69 6c 65 20 6c 6f 63 6b ess to file lock
1740: 69 6e 67 20 64 61 74 61 2e 0a 2a 2a 20 4f 6e 6c ing data..** Onl
1750: 79 20 72 65 74 75 72 6e 73 20 6f 6e 20 73 75 63 y returns on suc
1760: 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 cess..**.** NOTE
1770: 3a 20 49 27 6d 20 6e 6f 74 20 73 75 72 65 20 69 : I'm not sure i
1780: 6e 20 77 68 61 74 20 63 6f 6e 64 69 74 69 6f 6e n what condition
1790: 73 20 74 68 69 73 20 63 61 6e 20 74 75 72 6e 20 s this can turn
17a0: 69 6e 74 6f 20 61 6e 20 69 6e 66 69 6e 69 74 65 into an infinite
17b0: 0a 2a 2a 20 20 20 20 20 20 20 6c 6f 6f 70 2e 20 .** loop.
17c0: 49 20 64 6f 6e 27 74 20 74 68 69 6e 6b 20 69 74 I don't think it
17d0: 20 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74 is possible wit
17e0: 68 6f 75 74 20 61 20 73 65 72 69 6f 75 73 20 62 hout a serious b
17f0: 75 67 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 73 ug in.** s
1800: 71 6c 69 74 65 20 6f 72 20 77 69 6e 64 6f 77 73 qlite or windows
1810: 2c 20 62 75 74 20 69 27 6d 20 6e 6f 74 20 73 75 , but i'm not su
1820: 72 65 20 6f 66 20 74 68 69 73 2e 0a 2a 2f 0a 73 re of this..*/.s
1830: 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 6b 5f tatic void lock_
1840: 66 69 6c 65 28 20 4f 73 46 69 6c 65 20 2a 69 64 file( OsFile *id
1850: 20 29 0a 7b 0a 09 44 57 4f 52 44 20 72 65 73 3b ).{..DWORD res;
1860: 0a 09 77 68 69 6c 65 20 28 20 31 20 29 0a 09 7b ..while ( 1 )..{
1870: 0a 09 09 72 65 73 20 3d 20 57 61 69 74 46 6f 72 ...res = WaitFor
1880: 53 69 6e 67 6c 65 4f 62 6a 65 63 74 28 20 69 64 SingleObject( id
1890: 2d 3e 68 4d 75 78 2c 20 49 4e 46 49 4e 49 54 45 ->hMux, INFINITE
18a0: 20 29 3b 0a 09 09 2f 2f 20 49 20 64 6f 6e 27 74 );...// I don't
18b0: 20 6b 6e 6f 77 20 76 65 72 79 20 77 65 6c 6c 20 know very well
18c0: 77 68 61 74 20 49 20 68 61 76 65 20 74 6f 20 64 what I have to d
18d0: 6f 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a o in this case..
18e0: 09 09 2f 2f 20 54 68 65 20 4d 53 44 4e 20 73 61 ..// The MSDN sa
18f0: 79 73 20 74 68 61 74 20 74 68 69 73 20 63 61 73 ys that this cas
1900: 65 20 69 73 20 77 68 65 6e 20 61 20 74 68 72 65 e is when a thre
1910: 61 64 20 74 65 72 6d 69 6e 61 74 65 73 20 77 69 ad terminates wi
1920: 74 68 6f 75 74 0a 09 09 2f 2f 20 72 65 6c 65 61 thout...// relea
1930: 73 69 6e 67 20 74 68 65 20 6d 75 74 65 78 2e 20 sing the mutex.
1940: 53 6f 20 49 20 68 61 76 65 20 74 6f 20 72 65 6c So I have to rel
1950: 65 61 73 65 20 69 74 20 61 6e 64 20 74 72 79 20 ease it and try
1960: 61 67 61 69 6e 0a 09 09 69 66 20 28 20 72 65 73 again...if ( res
1970: 20 3d 3d 20 57 41 49 54 5f 41 42 41 4e 44 4f 4e == WAIT_ABANDON
1980: 45 44 20 29 0a 09 09 7b 0a 09 09 09 52 65 6c 65 ED )...{....Rele
1990: 61 73 65 4d 75 74 65 78 28 20 69 64 2d 3e 68 4d aseMutex( id->hM
19a0: 75 78 20 29 3b 0a 09 09 09 63 6f 6e 74 69 6e 75 ux );....continu
19b0: 65 3b 0a 09 09 7d 0a 09 09 2f 2f 20 73 75 63 63 e;...}...// succ
19c0: 65 73 73 20 3f 0a 09 09 69 66 20 28 20 72 65 73 ess ?...if ( res
19d0: 20 3d 3d 20 57 41 49 54 5f 4f 42 4a 45 43 54 5f == WAIT_OBJECT_
19e0: 30 20 29 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 0 )....break;...
19f0: 2f 2f 20 4c 65 74 20 74 68 65 20 6f 77 6e 65 72 // Let the owner
1a00: 20 68 61 76 65 20 74 69 6d 65 20 74 6f 20 72 65 have time to re
1a10: 6c 65 61 73 65 20 69 74 0a 09 09 53 6c 65 65 70 lease it...Sleep
1a20: 28 20 31 20 29 3b 0a 09 7d 0a 7d 0a 0a 2f 2a 0a ( 1 );..}.}../*.
1a30: 2a 2a 20 52 65 6c 65 61 73 65 73 20 6f 77 6e 65 ** Releases owne
1a40: 72 73 68 69 70 20 6f 66 20 74 68 65 20 66 69 6c rship of the fil
1a50: 65 20 6d 75 74 65 78 2e 0a 2a 2a 20 41 6c 77 61 e mutex..** Alwa
1a60: 79 73 20 73 75 63 63 65 73 73 0a 2a 2f 0a 73 74 ys success.*/.st
1a70: 61 74 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b atic void unlock
1a80: 5f 66 69 6c 65 28 20 4f 73 46 69 6c 65 20 2a 69 _file( OsFile *i
1a90: 64 20 29 0a 7b 0a 09 52 65 6c 65 61 73 65 4d 75 d ).{..ReleaseMu
1aa0: 74 65 78 28 20 69 64 2d 3e 68 4d 75 78 20 29 3b tex( id->hMux );
1ab0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 .}../*.** Acquir
1ac0: 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 e a lock on the
1ad0: 66 69 6c 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 73 file..** Returns
1ae0: 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 6e 20 73 75 63 non-zero on suc
1af0: 63 65 73 73 2c 20 7a 65 72 6f 20 6f 6e 20 66 61 cess, zero on fa
1b00: 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 ilure..*/.static
1b10: 20 69 6e 74 20 67 65 74 4c 6f 63 6b 28 20 4f 73 int getLock( Os
1b20: 46 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f File *id, int lo
1b30: 63 6b 74 79 70 65 20 29 0a 7b 0a 09 69 6e 74 20 cktype ).{..int
1b40: 72 63 20 3d 20 30 3b 0a 09 6c 6f 63 6b 5f 66 69 rc = 0;..lock_fi
1b50: 6c 65 28 20 69 64 20 29 3b 0a 09 69 66 20 28 20 le( id );..if (
1b60: 6c 6f 63 6b 74 79 70 65 20 3d 3d 20 53 48 41 52 locktype == SHAR
1b70: 45 44 5f 4c 4f 43 4b 20 29 0a 09 7b 0a 09 09 61 ED_LOCK )..{...a
1b80: 73 73 65 72 74 28 20 69 64 2d 3e 6c 6f 63 6b 64 ssert( id->lockd
1b90: 61 74 61 2d 3e 73 68 61 72 65 64 20 3e 3d 20 30 ata->shared >= 0
1ba0: 20 29 3b 0a 09 09 2b 2b 69 64 2d 3e 6c 6f 63 6b );...++id->lock
1bb0: 64 61 74 61 2d 3e 73 68 61 72 65 64 3b 09 2f 2a data->shared;./*
1bc0: 20 49 6e 63 72 65 6d 65 6e 74 20 6e 75 6d 62 65 Increment numbe
1bd0: 72 20 6f 66 20 72 65 61 64 65 72 73 20 2a 2f 0a r of readers */.
1be0: 09 09 69 64 2d 3e 6c 6f 63 6b 64 61 74 61 2d 3e ..id->lockdata->
1bf0: 6c 6f 63 6b 20 7c 3d 20 31 20 3c 3c 20 53 48 41 lock |= 1 << SHA
1c00: 52 45 44 5f 4c 4f 43 4b 3b 0a 09 7d 0a 09 65 6c RED_LOCK;..}..el
1c10: 73 65 0a 09 7b 0a 09 09 69 66 20 28 20 69 64 2d se..{...if ( id-
1c20: 3e 6c 6f 63 6b 64 61 74 61 2d 3e 6c 6f 63 6b 20 >lockdata->lock
1c30: 26 20 28 31 20 3c 3c 20 6c 6f 63 6b 74 79 70 65 & (1 << locktype
1c40: 29 20 29 0a 09 09 7b 0a 09 09 09 75 6e 6c 6f 63 ) )...{....unloc
1c50: 6b 5f 66 69 6c 65 28 20 69 64 20 29 3b 0a 09 09 k_file( id );...
1c60: 09 72 65 74 75 72 6e 20 30 3b 09 2f 2a 20 41 6c .return 0;./* Al
1c70: 72 65 61 64 79 20 6c 6f 63 6b 65 64 20 62 79 20 ready locked by
1c80: 6f 74 68 65 72 73 20 2a 2f 0a 09 09 7d 0a 09 09 others */...}...
1c90: 69 64 2d 3e 6c 6f 63 6b 64 61 74 61 2d 3e 6c 6f id->lockdata->lo
1ca0: 63 6b 20 7c 3d 20 31 20 3c 3c 20 6c 6f 63 6b 74 ck |= 1 << lockt
1cb0: 79 70 65 3b 0a 09 7d 0a 09 75 6e 6c 6f 63 6b 5f ype;..}..unlock_
1cc0: 66 69 6c 65 28 20 69 64 20 29 3b 0a 09 72 65 74 file( id );..ret
1cd0: 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 1;.}../*.**
1ce0: 52 65 6c 65 61 73 65 73 20 6c 6f 63 6b 20 6f 6e Releases lock on
1cf0: 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 41 6c the file..** Al
1d00: 77 61 79 73 20 73 75 63 63 65 65 64 73 2c 20 73 ways succeeds, s
1d10: 6f 20 6e 6f 20 72 65 74 75 72 6e 20 76 61 6c 75 o no return valu
1d20: 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
1d30: 64 20 75 6e 73 65 74 4c 6f 63 6b 28 20 4f 73 46 d unsetLock( OsF
1d40: 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
1d50: 6b 74 79 70 65 20 29 0a 7b 0a 09 61 73 73 65 72 ktype ).{..asser
1d60: 74 28 20 6c 6f 63 6b 74 79 70 65 20 3e 3d 20 53 t( locktype >= S
1d70: 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 09 6c HARED_LOCK );..l
1d80: 6f 63 6b 5f 66 69 6c 65 28 20 69 64 20 29 3b 0a ock_file( id );.
1d90: 09 69 66 20 28 20 6c 6f 63 6b 74 79 70 65 20 3d .if ( locktype =
1da0: 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 0a = SHARED_LOCK ).
1db0: 09 7b 0a 09 09 61 73 73 65 72 74 28 20 69 64 2d .{...assert( id-
1dc0: 3e 6c 6f 63 6b 64 61 74 61 2d 3e 73 68 61 72 65 >lockdata->share
1dd0: 64 20 3e 20 30 20 29 3b 0a 09 09 2d 2d 69 64 2d d > 0 );...--id-
1de0: 3e 6c 6f 63 6b 64 61 74 61 2d 3e 73 68 61 72 65 >lockdata->share
1df0: 64 3b 09 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 d;./* Decrement
1e00: 6e 75 6d 62 65 72 20 6f 66 20 72 65 61 64 65 72 number of reader
1e10: 73 20 2a 2f 0a 09 09 69 66 20 28 20 69 64 2d 3e s */...if ( id->
1e20: 6c 6f 63 6b 64 61 74 61 2d 3e 73 68 61 72 65 64 lockdata->shared
1e30: 20 3d 3d 20 30 20 29 09 2f 2a 20 4c 61 73 74 20 == 0 )./* Last
1e40: 72 65 61 64 65 72 3f 20 2a 2f 0a 09 09 09 69 64 reader? */....id
1e50: 2d 3e 6c 6f 63 6b 64 61 74 61 2d 3e 6c 6f 63 6b ->lockdata->lock
1e60: 20 26 3d 20 7e 28 31 20 3c 3c 20 53 48 41 52 45 &= ~(1 << SHARE
1e70: 44 5f 4c 4f 43 4b 29 3b 0a 09 7d 0a 09 65 6c 73 D_LOCK);..}..els
1e80: 65 0a 09 7b 0a 09 09 69 64 2d 3e 6c 6f 63 6b 64 e..{...id->lockd
1e90: 61 74 61 2d 3e 6c 6f 63 6b 20 26 3d 20 7e 28 31 ata->lock &= ~(1
1ea0: 20 3c 3c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 09 << locktype);..
1eb0: 7d 0a 09 75 6e 6c 6f 63 6b 5f 66 69 6c 65 28 20 }..unlock_file(
1ec0: 69 64 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 id );.}../*.** I
1ed0: 6e 69 74 69 61 6c 69 7a 65 73 20 66 69 6c 65 20 nitializes file
1ee0: 6c 6f 63 6b 69 6e 67 20 73 74 72 75 74 75 72 65 locking struture
1ef0: 2e 0a 2a 2a 20 52 65 74 75 72 6e 73 20 6e 6f 6e ..** Returns non
1f00: 2d 7a 65 72 6f 20 6f 6e 20 73 75 63 63 65 73 73 -zero on success
1f10: 2c 20 7a 65 72 6f 20 6f 6e 20 65 72 72 6f 72 2e , zero on error.
1f20: 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 6f 70 65 6e .**.** Each open
1f30: 20 66 69 6c 65 20 77 69 6c 6c 20 68 61 76 65 20 file will have
1f40: 61 6e 20 61 73 73 6f 63 69 61 74 65 64 20 73 68 an associated sh
1f50: 61 72 65 64 20 6d 65 6d 6f 72 79 20 61 72 65 61 ared memory area
1f60: 2c 20 77 68 65 72 65 20 74 68 65 20 67 6c 6f 62 , where the glob
1f70: 61 6c 0a 2a 2a 20 6c 6f 63 6b 20 69 6e 66 6f 72 al.** lock infor
1f80: 6d 61 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 73 mation will be s
1f90: 74 6f 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 69 tored..** The fi
1fa0: 6c 65 20 70 61 74 68 20 69 73 20 75 73 65 64 20 le path is used
1fb0: 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 75 6e to generate a un
1fc0: 69 71 75 65 20 6e 61 6d 65 20 66 6f 72 20 65 61 ique name for ea
1fd0: 63 68 20 66 69 6c 65 2e 0a 2a 2a 20 41 6e 20 61 ch file..** An a
1fe0: 64 69 74 69 6f 6e 61 6c 20 67 6c 6f 62 61 6c 20 ditional global
1ff0: 6d 75 74 65 78 20 70 65 72 20 66 69 6c 65 20 69 mutex per file i
2000: 73 20 63 72 65 61 74 65 64 2c 20 66 6f 72 20 73 s created, for s
2010: 79 6e 63 72 6f 6e 69 7a 61 74 69 6f 6e 20 62 65 yncronization be
2020: 74 77 65 65 6e 0a 2a 2a 20 70 72 6f 63 65 73 73 tween.** process
2030: 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e es..*/.static in
2040: 74 20 73 71 6c 69 74 65 77 63 65 5f 49 6e 69 74 t sqlitewce_Init
2050: 4c 6f 63 6b 73 28 20 4f 73 46 69 6c 65 20 2a 69 Locks( OsFile *i
2060: 64 20 29 0a 7b 0a 09 57 43 48 41 52 20 2a 20 61 d ).{..WCHAR * a
2070: 75 78 3b 0a 09 57 43 48 41 52 20 6d 75 78 4e 61 ux;..WCHAR muxNa
2080: 6d 65 5b 32 35 36 5d 20 3d 20 4c 22 73 71 77 63 me[256] = L"sqwc
2090: 65 5f 6d 75 78 5f 22 3b 0a 09 57 43 48 41 52 20 e_mux_";..WCHAR
20a0: 6d 65 6d 4e 61 6d 65 5b 32 35 36 5d 20 3d 20 4c memName[256] = L
20b0: 22 73 71 77 63 65 5f 6d 65 6d 5f 22 3b 0a 09 69 "sqwce_mem_";..i
20c0: 6e 74 20 69 2c 20 65 78 69 73 74 73 3b 0a 0a 09 nt i, exists;...
20d0: 2f 2f 20 47 65 6e 65 72 61 74 65 20 72 65 73 6f // Generate reso
20e0: 75 72 63 65 20 6e 61 6d 65 73 20 73 75 66 66 69 urce names suffi
20f0: 78 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 20 x from the file
2100: 6e 61 6d 65 0a 09 61 75 78 20 3d 20 5f 77 63 73 name..aux = _wcs
2110: 64 75 70 28 20 69 64 2d 3e 77 46 69 6c 65 6e 61 dup( id->wFilena
2120: 6d 65 20 29 3b 0a 09 69 66 20 28 20 61 75 78 20 me );..if ( aux
2130: 3d 3d 20 4e 55 4c 4c 20 29 09 72 65 74 75 72 6e == NULL ).return
2140: 20 30 3b 20 2f 2f 20 4e 6f 20 6d 65 6d 0a 09 66 0; // No mem..f
2150: 6f 72 20 28 20 69 20 3d 20 30 3b 20 61 75 78 5b or ( i = 0; aux[
2160: 69 5d 3b 20 2b 2b 69 20 29 0a 09 7b 0a 09 09 69 i]; ++i )..{...i
2170: 66 20 28 20 61 75 78 5b 69 5d 20 3d 3d 20 27 5c f ( aux[i] == '\
2180: 5c 27 20 29 0a 09 09 09 61 75 78 5b 69 5d 20 3d \' )....aux[i] =
2190: 20 27 2f 27 3b 20 2f 2f 20 63 61 6e 27 74 20 75 '/'; // can't u
21a0: 73 65 20 27 5c 5c 27 20 69 6e 20 6e 61 6d 65 0a se '\\' in name.
21b0: 09 09 65 6c 73 65 0a 09 09 09 61 75 78 5b 69 5d ..else....aux[i]
21c0: 20 3d 20 74 6f 77 6c 6f 77 65 72 28 20 61 75 78 = towlower( aux
21d0: 5b 69 5d 20 29 3b 20 2f 2f 20 6e 61 6d 65 73 20 [i] ); // names
21e0: 61 72 65 20 63 61 73 65 20 73 65 6e 73 69 74 69 are case sensiti
21f0: 76 65 0a 09 7d 0a 09 77 63 73 6e 63 61 74 28 20 ve..}..wcsncat(
2200: 6d 75 78 4e 61 6d 65 2c 20 61 75 78 2c 20 32 35 muxName, aux, 25
2210: 36 20 29 3b 0a 09 77 63 73 6e 63 61 74 28 20 6d 6 );..wcsncat( m
2220: 65 6d 4e 61 6d 65 2c 20 61 75 78 2c 20 32 35 36 emName, aux, 256
2230: 20 29 3b 0a 09 66 72 65 65 28 20 61 75 78 20 29 );..free( aux )
2240: 3b 0a 0a 09 2f 2f 20 43 72 65 61 74 65 20 6e 61 ;...// Create na
2250: 6d 65 64 20 6d 75 74 65 78 20 28 6f 72 20 6f 70 med mutex (or op
2260: 65 6e 20 65 78 69 73 74 69 6e 67 29 0a 09 69 64 en existing)..id
2270: 2d 3e 68 4d 75 78 20 3d 20 43 72 65 61 74 65 4d ->hMux = CreateM
2280: 75 74 65 78 28 20 4e 55 4c 4c 2c 20 46 41 4c 53 utex( NULL, FALS
2290: 45 2c 20 6d 75 78 4e 61 6d 65 20 29 3b 0a 09 69 E, muxName );..i
22a0: 66 20 28 20 69 64 2d 3e 68 4d 75 78 20 3d 3d 20 f ( id->hMux ==
22b0: 4e 55 4c 4c 20 29 0a 09 09 72 65 74 75 72 6e 20 NULL )...return
22c0: 30 3b 20 2f 2f 20 4e 6f 20 6d 65 6d 20 6f 72 20 0; // No mem or
22d0: 73 6f 6d 65 74 68 69 6e 67 20 77 65 69 72 64 0a something weird.
22e0: 0a 09 2f 2f 20 4c 6f 63 6b 20 61 63 63 65 73 73 ..// Lock access
22f0: 20 74 6f 20 66 69 6c 65 20 64 61 74 61 20 28 61 to file data (a
2300: 76 6f 69 64 20 72 61 63 65 20 63 6f 6e 64 69 74 void race condit
2310: 69 6f 6e 20 6f 6e 20 63 72 65 61 74 65 2f 6f 70 ion on create/op
2320: 65 6e 29 0a 09 6c 6f 63 6b 5f 66 69 6c 65 28 20 en)..lock_file(
2330: 69 64 20 29 3b 0a 0a 09 2f 2f 20 43 72 65 61 74 id );...// Creat
2340: 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 e shared memory
2350: 6d 61 70 70 69 6e 67 20 6f 72 20 6f 70 65 6e 20 mapping or open
2360: 65 78 69 73 74 69 6e 67 0a 09 69 64 2d 3e 68 4d existing..id->hM
2370: 65 6d 20 3d 20 43 72 65 61 74 65 46 69 6c 65 4d em = CreateFileM
2380: 61 70 70 69 6e 67 28 0a 09 09 09 09 09 49 4e 56 apping(......INV
2390: 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 ALID_HANDLE_VALU
23a0: 45 2c 20 4e 55 4c 4c 2c 20 50 41 47 45 5f 52 45 E, NULL, PAGE_RE
23b0: 41 44 57 52 49 54 45 2c 0a 09 09 09 09 09 30 2c ADWRITE,......0,
23c0: 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 77 63 sizeof(sqlitewc
23d0: 65 5f 6c 6f 63 6b 64 61 74 61 5f 74 29 2c 0a 09 e_lockdata_t),..
23e0: 09 09 09 09 6d 65 6d 4e 61 6d 65 20 29 3b 0a 09 ....memName );..
23f0: 69 66 20 28 20 69 64 2d 3e 68 4d 65 6d 20 3d 3d if ( id->hMem ==
2400: 20 4e 55 4c 4c 20 29 0a 09 7b 0a 09 09 75 6e 6c NULL )..{...unl
2410: 6f 63 6b 5f 66 69 6c 65 28 20 69 64 20 29 3b 0a ock_file( id );.
2420: 09 09 43 6c 6f 73 65 48 61 6e 64 6c 65 28 20 69 ..CloseHandle( i
2430: 64 2d 3e 68 4d 75 78 20 29 3b 0a 09 09 72 65 74 d->hMux );...ret
2440: 75 72 6e 20 30 3b 09 2f 2f 20 4e 6f 20 6d 65 6d urn 0;.// No mem
2450: 20 6f 72 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 or something we
2460: 69 72 64 0a 09 7d 0a 0a 09 2f 2f 20 43 68 65 63 ird..}...// Chec
2470: 6b 20 69 66 20 61 6c 72 65 61 64 79 20 65 78 69 k if already exi
2480: 73 74 73 20 28 63 72 65 61 74 65 64 20 62 79 20 sts (created by
2490: 6f 74 68 65 72 20 70 72 6f 63 65 73 73 29 0a 09 other process)..
24a0: 65 78 69 73 74 73 20 3d 20 28 47 65 74 4c 61 73 exists = (GetLas
24b0: 74 45 72 72 6f 72 28 29 20 3d 3d 20 45 52 52 4f tError() == ERRO
24c0: 52 5f 41 4c 52 45 41 44 59 5f 45 58 49 53 54 53 R_ALREADY_EXISTS
24d0: 29 3b 0a 0a 09 2f 2f 20 4f 70 65 6e 20 76 69 65 );...// Open vie
24e0: 77 20 74 6f 20 74 68 65 20 64 61 74 61 0a 09 69 w to the data..i
24f0: 64 2d 3e 6c 6f 63 6b 64 61 74 61 20 3d 20 28 73 d->lockdata = (s
2500: 71 6c 69 74 65 77 63 65 5f 6c 6f 63 6b 64 61 74 qlitewce_lockdat
2510: 61 5f 74 20 2a 29 4d 61 70 56 69 65 77 4f 66 46 a_t *)MapViewOfF
2520: 69 6c 65 28 20 69 64 2d 3e 68 4d 65 6d 2c 20 46 ile( id->hMem, F
2530: 49 4c 45 5f 4d 41 50 5f 57 52 49 54 45 2c 20 30 ILE_MAP_WRITE, 0
2540: 2c 30 2c 20 30 20 29 3b 0a 09 69 66 20 28 20 69 ,0, 0 );..if ( i
2550: 64 2d 3e 6c 6f 63 6b 64 61 74 61 20 3d 3d 20 4e d->lockdata == N
2560: 55 4c 4c 20 29 0a 09 7b 0a 09 09 75 6e 6c 6f 63 ULL )..{...unloc
2570: 6b 5f 66 69 6c 65 28 20 69 64 20 29 3b 0a 09 09 k_file( id );...
2580: 43 6c 6f 73 65 48 61 6e 64 6c 65 28 20 69 64 2d CloseHandle( id-
2590: 3e 68 4d 65 6d 20 29 3b 0a 09 09 43 6c 6f 73 65 >hMem );...Close
25a0: 48 61 6e 64 6c 65 28 20 69 64 2d 3e 68 4d 75 78 Handle( id->hMux
25b0: 20 29 3b 0a 09 09 72 65 74 75 72 6e 20 30 3b 09 );...return 0;.
25c0: 2f 2f 20 4e 6f 20 6d 65 6d 20 6f 72 20 73 6f 6d // No mem or som
25d0: 65 74 68 69 6e 67 20 77 65 69 72 64 0a 09 7d 0a ething weird..}.
25e0: 0a 09 2f 2f 20 49 6e 69 74 69 61 6c 69 7a 65 20 ..// Initialize
25f0: 6c 6f 63 6b 64 61 74 61 2c 20 69 66 20 66 69 72 lockdata, if fir
2600: 73 74 20 74 69 6d 65 0a 09 69 66 20 28 20 21 20 st time..if ( !
2610: 65 78 69 73 74 73 20 29 0a 09 09 6d 65 6d 73 65 exists )...memse
2620: 74 28 20 69 64 2d 3e 6c 6f 63 6b 64 61 74 61 2c t( id->lockdata,
2630: 20 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 0, sizeof(sqlit
2640: 65 77 63 65 5f 6c 6f 63 6b 64 61 74 61 5f 74 29 ewce_lockdata_t)
2650: 20 29 3b 0a 0a 09 2f 2f 20 44 6f 6e 65 2c 20 72 );...// Done, r
2660: 65 6c 65 61 73 65 20 67 6c 6f 62 61 6c 20 6c 6f elease global lo
2670: 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0a ck on the file..
2680: 09 75 6e 6c 6f 63 6b 5f 66 69 6c 65 28 20 69 64 .unlock_file( id
2690: 20 29 3b 0a 0a 09 72 65 74 75 72 6e 20 31 3b 0a );...return 1;.
26a0: 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 }../*.** Release
26b0: 73 20 61 6e 79 20 6c 6f 63 6b 73 20 68 65 6c 64 s any locks held
26c0: 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 61 6e 64 on the file and
26d0: 20 72 65 6c 65 61 73 65 73 20 6c 6f 63 6b 69 6e releases lockin
26e0: 67 20 64 61 74 61 2e 0a 2a 2a 20 44 6f 65 73 6e g data..** Doesn
26f0: 27 74 20 72 65 74 75 72 6e 20 61 6e 79 74 68 69 't return anythi
2700: 6e 67 2c 20 62 65 63 61 75 73 65 20 74 68 65 72 ng, because ther
2710: 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 72 e is no way to r
2720: 65 63 6f 76 65 72 0a 2a 2a 20 66 72 6f 6d 20 61 ecover.** from a
2730: 20 66 61 69 6c 6c 75 72 65 20 74 6f 20 72 65 6d faillure to rem
2740: 6f 76 65 20 74 68 65 20 6c 6f 63 6b 20 28 61 6e ove the lock (an
2750: 64 20 61 20 66 61 69 6c 6c 75 72 65 20 74 6f 20 d a faillure to
2760: 64 6f 20 73 6f 0a 2a 2a 20 77 6f 75 6c 64 20 62 do so.** would b
2770: 65 20 61 20 62 75 67 20 65 69 74 68 65 72 20 69 e a bug either i
2780: 6e 20 77 69 6e 64 6f 77 73 20 6f 72 20 69 6e 20 n windows or in
2790: 73 71 6c 69 74 65 29 2e 0a 2a 2f 0a 73 74 61 74 sqlite)..*/.stat
27a0: 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 77 63 ic void sqlitewc
27b0: 65 5f 52 65 6c 65 61 73 65 4c 6f 63 6b 73 28 20 e_ReleaseLocks(
27c0: 4f 73 46 69 6c 65 20 2a 69 64 20 29 0a 7b 0a 09 OsFile *id ).{..
27d0: 69 66 20 28 20 69 64 2d 3e 6c 6f 63 6b 64 61 74 if ( id->lockdat
27e0: 61 20 29 0a 09 7b 0a 09 09 73 71 6c 69 74 65 33 a )..{...sqlite3
27f0: 4f 73 55 6e 6c 6f 63 6b 28 20 69 64 2c 20 4e 4f OsUnlock( id, NO
2800: 5f 4c 4f 43 4b 20 29 3b 0a 09 09 55 6e 6d 61 70 _LOCK );...Unmap
2810: 56 69 65 77 4f 66 46 69 6c 65 28 20 69 64 2d 3e ViewOfFile( id->
2820: 6c 6f 63 6b 64 61 74 61 20 29 3b 0a 09 09 43 6c lockdata );...Cl
2830: 6f 73 65 48 61 6e 64 6c 65 28 20 69 64 2d 3e 68 oseHandle( id->h
2840: 4d 65 6d 20 29 3b 0a 09 09 43 6c 6f 73 65 48 61 Mem );...CloseHa
2850: 6e 64 6c 65 28 20 69 64 2d 3e 68 4d 75 78 20 29 ndle( id->hMux )
2860: 3b 0a 09 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f ;..}.}..#endif /
2870: 2f 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 / !defined(SQLIT
2880: 45 5f 57 43 45 5f 4f 4d 49 54 5f 46 49 4c 45 4c E_WCE_OMIT_FILEL
2890: 4f 43 4b 29 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a OCK).../********
28a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
28b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
28c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
28d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 **************.
28e0: 2a 20 73 71 6c 69 74 65 33 4f 73 20 66 75 6e 63 * sqlite3Os func
28f0: 74 69 6f 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 65 tions implemente
2900: 64 20 73 70 65 63 69 66 69 63 61 6c 79 20 66 6f d specificaly fo
2910: 72 20 57 69 6e 43 45 0a 20 2a 2a 2a 2a 2a 2a 2a r WinCE. *******
2920: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2930: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2940: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2950: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
2960: 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 ./*.** Attempt t
2970: 6f 20 6f 70 65 6e 20 61 20 66 69 6c 65 20 66 6f o open a file fo
2980: 72 20 62 6f 74 68 20 72 65 61 64 69 6e 67 20 61 r both reading a
2990: 6e 64 20 77 72 69 74 69 6e 67 2e 20 20 49 66 20 nd writing. If
29a0: 74 68 61 74 0a 2a 2a 20 66 61 69 6c 73 2c 20 74 that.** fails, t
29b0: 72 79 20 6f 70 65 6e 69 6e 67 20 69 74 20 72 65 ry opening it re
29c0: 61 64 2d 6f 6e 6c 79 2e 20 20 49 66 20 74 68 65 ad-only. If the
29d0: 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 file does not e
29e0: 78 69 73 74 2c 0a 2a 2a 20 74 72 79 20 74 6f 20 xist,.** try to
29f0: 63 72 65 61 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a create it..**.**
2a00: 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 61 20 68 On success, a h
2a10: 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 6f 70 andle for the op
2a20: 65 6e 20 66 69 6c 65 20 69 73 20 77 72 69 74 74 en file is writt
2a30: 65 6e 20 74 6f 20 2a 69 64 0a 2a 2a 20 61 6e 64 en to *id.** and
2a40: 20 2a 70 52 65 61 64 6f 6e 6c 79 20 69 73 20 73 *pReadonly is s
2a50: 65 74 20 74 6f 20 30 20 69 66 20 74 68 65 20 66 et to 0 if the f
2a60: 69 6c 65 20 77 61 73 20 6f 70 65 6e 65 64 20 66 ile was opened f
2a70: 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 0a 2a or reading and.*
2a80: 2a 20 77 72 69 74 69 6e 67 20 6f 72 20 31 20 69 * writing or 1 i
2a90: 66 20 74 68 65 20 66 69 6c 65 20 77 61 73 20 6f f the file was o
2aa0: 70 65 6e 65 64 20 72 65 61 64 2d 6f 6e 6c 79 2e pened read-only.
2ab0: 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 The function r
2ac0: 65 74 75 72 6e 73 0a 2a 2a 20 53 51 4c 49 54 45 eturns.** SQLITE
2ad0: 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 66 61 _OK..**.** On fa
2ae0: 69 6c 75 72 65 2c 20 74 68 65 20 66 75 6e 63 74 ilure, the funct
2af0: 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ion returns SQLI
2b00: 54 45 5f 43 41 4e 54 4f 50 45 4e 20 61 6e 64 20 TE_CANTOPEN and
2b10: 6c 65 61 76 65 73 0a 2a 2a 20 2a 69 64 20 61 6e leaves.** *id an
2b20: 64 20 2a 70 52 65 61 64 6f 6e 6c 79 20 75 6e 63 d *pReadonly unc
2b30: 68 61 6e 67 65 64 2e 0a 2a 2f 0a 69 6e 74 20 73 hanged..*/.int s
2b40: 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 qlite3OsOpenRead
2b50: 57 72 69 74 65 28 20 63 6f 6e 73 74 20 63 68 61 Write( const cha
2b60: 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 4f 73 r *zFilename, Os
2b70: 46 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 File *id, int *p
2b80: 52 65 61 64 6f 6e 6c 79 20 29 0a 7b 0a 09 48 41 Readonly ).{..HA
2b90: 4e 44 4c 45 20 68 3b 0a 09 57 43 48 41 52 20 2a NDLE h;..WCHAR *
2ba0: 77 46 69 6c 65 6e 61 6d 65 20 3d 20 53 74 72 44 wFilename = StrD
2bb0: 75 70 57 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 upW( zFilename )
2bc0: 3b 0a 09 69 66 20 28 20 77 46 69 6c 65 6e 61 6d ;..if ( wFilenam
2bd0: 65 20 3d 3d 20 4e 55 4c 4c 20 29 0a 09 09 72 65 e == NULL )...re
2be0: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
2bf0: 4d 3b 0a 09 61 73 73 65 72 74 28 20 21 69 64 2d M;..assert( !id-
2c00: 3e 69 73 4f 70 65 6e 20 29 3b 0a 09 68 20 3d 20 >isOpen );..h =
2c10: 43 72 65 61 74 65 46 69 6c 65 57 28 20 77 46 69 CreateFileW( wFi
2c20: 6c 65 6e 61 6d 65 2c 20 47 45 4e 45 52 49 43 5f lename, GENERIC_
2c30: 52 45 41 44 20 7c 20 47 45 4e 45 52 49 43 5f 57 READ | GENERIC_W
2c40: 52 49 54 45 2c 0a 09 09 09 09 09 46 49 4c 45 5f RITE,......FILE_
2c50: 53 48 41 52 45 5f 52 45 41 44 20 7c 20 46 49 4c SHARE_READ | FIL
2c60: 45 5f 53 48 41 52 45 5f 57 52 49 54 45 2c 0a 09 E_SHARE_WRITE,..
2c70: 09 09 09 09 4e 55 4c 4c 2c 20 4f 50 45 4e 5f 41 ....NULL, OPEN_A
2c80: 4c 57 41 59 53 2c 0a 09 09 09 09 09 46 49 4c 45 LWAYS,......FILE
2c90: 5f 41 54 54 52 49 42 55 54 45 5f 4e 4f 52 4d 41 _ATTRIBUTE_NORMA
2ca0: 4c 20 7c 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 L | FILE_FLAG_RA
2cb0: 4e 44 4f 4d 5f 41 43 43 45 53 53 2c 0a 09 09 09 NDOM_ACCESS,....
2cc0: 09 09 4e 55 4c 4c 20 29 3b 0a 09 69 66 20 28 20 ..NULL );..if (
2cd0: 68 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 48 41 4e h == INVALID_HAN
2ce0: 44 4c 45 5f 56 41 4c 55 45 20 29 0a 09 7b 0a 09 DLE_VALUE )..{..
2cf0: 09 68 20 3d 20 43 72 65 61 74 65 46 69 6c 65 57 .h = CreateFileW
2d00: 28 20 77 46 69 6c 65 6e 61 6d 65 2c 20 47 45 4e ( wFilename, GEN
2d10: 45 52 49 43 5f 52 45 41 44 2c 0a 09 09 09 09 09 ERIC_READ,......
2d20: 46 49 4c 45 5f 53 48 41 52 45 5f 52 45 41 44 2c FILE_SHARE_READ,
2d30: 0a 09 09 09 09 09 4e 55 4c 4c 2c 20 4f 50 45 4e ......NULL, OPEN
2d40: 5f 41 4c 57 41 59 53 2c 0a 09 09 09 09 09 46 49 _ALWAYS,......FI
2d50: 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 4e 4f 52 LE_ATTRIBUTE_NOR
2d60: 4d 41 4c 20 7c 20 46 49 4c 45 5f 46 4c 41 47 5f MAL | FILE_FLAG_
2d70: 52 41 4e 44 4f 4d 5f 41 43 43 45 53 53 2c 0a 09 RANDOM_ACCESS,..
2d80: 09 09 09 09 4e 55 4c 4c 20 29 3b 0a 09 09 69 66 ....NULL );...if
2d90: 20 28 20 68 20 3d 3d 20 49 4e 56 41 4c 49 44 5f ( h == INVALID_
2da0: 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29 0a 09 HANDLE_VALUE )..
2db0: 09 7b 0a 09 09 09 66 72 65 65 28 20 77 46 69 6c .{....free( wFil
2dc0: 65 6e 61 6d 65 20 29 3b 0a 09 09 09 72 65 74 75 ename );....retu
2dd0: 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 rn SQLITE_CANTOP
2de0: 45 4e 3b 0a 09 09 7d 0a 09 09 2a 70 52 65 61 64 EN;...}...*pRead
2df0: 6f 6e 6c 79 20 3d 20 31 3b 0a 09 7d 0a 09 65 6c only = 1;..}..el
2e00: 73 65 0a 09 7b 0a 09 09 2a 70 52 65 61 64 6f 6e se..{...*pReadon
2e10: 6c 79 20 3d 20 30 3b 0a 09 7d 0a 09 2f 2f 20 46 ly = 0;..}..// F
2e20: 69 6c 6c 20 66 69 6c 65 20 63 6f 6e 74 65 78 74 ill file context
2e30: 20 64 61 74 61 0a 09 69 64 2d 3e 68 20 3d 20 68 data..id->h = h
2e40: 3b 0a 09 69 64 2d 3e 6c 6f 63 6b 74 79 70 65 20 ;..id->locktype
2e50: 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 09 69 64 2d 3e = NO_LOCK;..id->
2e60: 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d sharedLockByte =
2e70: 20 30 3b 0a 09 69 64 2d 3e 69 73 4f 70 65 6e 20 0;..id->isOpen
2e80: 3d 20 31 3b 0a 09 69 64 2d 3e 77 46 69 6c 65 6e = 1;..id->wFilen
2e90: 61 6d 65 20 3d 20 77 46 69 6c 65 6e 61 6d 65 3b ame = wFilename;
2ea0: 0a 09 69 64 2d 3e 64 65 6c 4f 6e 43 6c 6f 73 65 ..id->delOnClose
2eb0: 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 = 0;.#ifndef SQ
2ec0: 4c 49 54 45 5f 57 43 45 5f 4f 4d 49 54 5f 46 49 LITE_WCE_OMIT_FI
2ed0: 4c 45 4c 4f 43 4b 0a 09 69 66 20 28 20 21 20 73 LELOCK..if ( ! s
2ee0: 71 6c 69 74 65 77 63 65 5f 49 6e 69 74 4c 6f 63 qlitewce_InitLoc
2ef0: 6b 73 28 69 64 29 20 29 0a 09 7b 09 2f 2f 20 46 ks(id) )..{.// F
2f00: 61 69 6c 6c 65 64 20 74 6f 20 69 6e 69 74 69 61 ailled to initia
2f10: 6c 69 7a 65 20 66 69 6c 65 20 6c 6f 63 6b 20 6d lize file lock m
2f20: 65 63 68 61 6e 69 73 6d 0a 09 09 66 72 65 65 28 echanism...free(
2f30: 20 77 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 09 09 wFilename );...
2f40: 43 6c 6f 73 65 48 61 6e 64 6c 65 28 20 68 20 29 CloseHandle( h )
2f50: 3b 0a 09 09 72 65 74 75 72 6e 20 53 51 4c 49 54 ;...return SQLIT
2f60: 45 5f 4e 4f 4d 45 4d 3b 0a 09 7d 0a 23 65 6e 64 E_NOMEM;..}.#end
2f70: 69 66 0a 09 4f 70 65 6e 43 6f 75 6e 74 65 72 28 if..OpenCounter(
2f80: 2b 31 29 3b 0a 09 54 52 41 43 45 33 28 22 4f 50 +1);..TRACE3("OP
2f90: 45 4e 20 52 2f 57 20 25 64 20 5c 22 25 73 5c 22 EN R/W %d \"%s\"
2fa0: 5c 6e 22 2c 20 68 2c 20 7a 46 69 6c 65 6e 61 6d \n", h, zFilenam
2fb0: 65 29 3b 0a 09 72 65 74 75 72 6e 20 53 51 4c 49 e);..return SQLI
2fc0: 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
2fd0: 41 74 74 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20 Attempt to open
2fe0: 61 20 6e 65 77 20 66 69 6c 65 20 66 6f 72 20 65 a new file for e
2ff0: 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 20 xclusive access
3000: 62 79 20 74 68 69 73 20 70 72 6f 63 65 73 73 2e by this process.
3010: 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 77 69 6c .** The file wil
3020: 6c 20 62 65 20 6f 70 65 6e 65 64 20 66 6f 72 20 l be opened for
3030: 62 6f 74 68 20 72 65 61 64 69 6e 67 20 61 6e 64 both reading and
3040: 20 77 72 69 74 69 6e 67 2e 20 20 54 6f 20 61 76 writing. To av
3050: 6f 69 64 0a 2a 2a 20 61 20 70 6f 74 65 6e 74 69 oid.** a potenti
3060: 61 6c 20 73 65 63 75 72 69 74 79 20 70 72 6f 62 al security prob
3070: 6c 65 6d 2c 20 77 65 20 64 6f 20 6e 6f 74 20 61 lem, we do not a
3080: 6c 6c 6f 77 20 74 68 65 20 66 69 6c 65 20 74 6f llow the file to
3090: 20 68 61 76 65 0a 2a 2a 20 70 72 65 76 69 6f 75 have.** previou
30a0: 73 6c 79 20 65 78 69 73 74 65 64 2e 20 20 4e 6f sly existed. No
30b0: 72 20 64 6f 20 77 65 20 61 6c 6c 6f 77 20 74 68 r do we allow th
30c0: 65 20 66 69 6c 65 20 74 6f 20 62 65 20 61 20 73 e file to be a s
30d0: 79 6d 62 6f 6c 69 63 0a 2a 2a 20 6c 69 6e 6b 2e ymbolic.** link.
30e0: 0a 2a 2a 0a 2a 2a 20 49 66 20 64 65 6c 46 6c 61 .**.** If delFla
30f0: 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 g is true, then
3100: 6d 61 6b 65 20 61 72 72 61 6e 67 65 6d 65 6e 74 make arrangement
3110: 73 20 74 6f 20 61 75 74 6f 6d 61 74 69 63 61 6c s to automatical
3120: 6c 79 20 64 65 6c 65 74 65 0a 2a 2a 20 74 68 65 ly delete.** the
3130: 20 66 69 6c 65 20 77 68 65 6e 20 69 74 20 69 73 file when it is
3140: 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f closed..**.** O
3150: 6e 20 73 75 63 63 65 73 73 2c 20 77 72 69 74 65 n success, write
3160: 20 74 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 the file handle
3170: 20 69 6e 74 6f 20 2a 69 64 20 61 6e 64 20 72 65 into *id and re
3180: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a turn SQLITE_OK..
3190: 2a 2a 0a 2a 2a 20 4f 6e 20 66 61 69 6c 75 72 65 **.** On failure
31a0: 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
31b0: 43 41 4e 54 4f 50 45 4e 2e 0a 2a 2f 0a 69 6e 74 CANTOPEN..*/.int
31c0: 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 78 sqlite3OsOpenEx
31d0: 63 6c 75 73 69 76 65 28 20 63 6f 6e 73 74 20 63 clusive( const c
31e0: 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 har *zFilename,
31f0: 4f 73 46 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 OsFile *id, int
3200: 64 65 6c 46 6c 61 67 20 29 0a 7b 0a 09 48 41 4e delFlag ).{..HAN
3210: 44 4c 45 20 68 3b 0a 09 57 43 48 41 52 20 2a 20 DLE h;..WCHAR *
3220: 77 46 69 6c 65 6e 61 6d 65 20 3d 20 53 74 72 44 wFilename = StrD
3230: 75 70 57 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 upW( zFilename )
3240: 3b 0a 09 61 73 73 65 72 74 28 20 21 69 64 2d 3e ;..assert( !id->
3250: 69 73 4f 70 65 6e 20 29 3b 0a 09 68 20 3d 20 43 isOpen );..h = C
3260: 72 65 61 74 65 46 69 6c 65 57 28 20 77 46 69 6c reateFileW( wFil
3270: 65 6e 61 6d 65 2c 20 47 45 4e 45 52 49 43 5f 52 ename, GENERIC_R
3280: 45 41 44 20 7c 20 47 45 4e 45 52 49 43 5f 57 52 EAD | GENERIC_WR
3290: 49 54 45 2c 20 30 2c 0a 09 09 09 09 09 4e 55 4c ITE, 0,......NUL
32a0: 4c 2c 20 43 52 45 41 54 45 5f 41 4c 57 41 59 53 L, CREATE_ALWAYS
32b0: 2c 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 , FILE_FLAG_RAND
32c0: 4f 4d 5f 41 43 43 45 53 53 2c 20 4e 55 4c 4c 20 OM_ACCESS, NULL
32d0: 29 3b 0a 09 69 66 20 28 20 68 20 3d 3d 20 49 4e );..if ( h == IN
32e0: 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c VALID_HANDLE_VAL
32f0: 55 45 20 29 0a 09 7b 0a 09 09 66 72 65 65 28 20 UE )..{...free(
3300: 77 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 09 09 72 wFilename );...r
3310: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e eturn SQLITE_CAN
3320: 54 4f 50 45 4e 3b 0a 09 7d 0a 09 69 64 2d 3e 68 TOPEN;..}..id->h
3330: 20 3d 20 68 3b 0a 09 69 64 2d 3e 6c 6f 63 6b 74 = h;..id->lockt
3340: 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 09 ype = NO_LOCK;..
3350: 69 64 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 id->sharedLockBy
3360: 74 65 20 3d 20 30 3b 0a 09 69 64 2d 3e 69 73 4f te = 0;..id->isO
3370: 70 65 6e 20 3d 20 31 3b 0a 09 69 64 2d 3e 77 46 pen = 1;..id->wF
3380: 69 6c 65 6e 61 6d 65 20 3d 20 77 46 69 6c 65 6e ilename = wFilen
3390: 61 6d 65 3b 0a 09 69 64 2d 3e 64 65 6c 4f 6e 43 ame;..id->delOnC
33a0: 6c 6f 73 65 20 3d 20 64 65 6c 46 6c 61 67 3b 0a lose = delFlag;.
33b0: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 57 #ifndef SQLITE_W
33c0: 43 45 5f 4f 4d 49 54 5f 46 49 4c 45 4c 4f 43 4b CE_OMIT_FILELOCK
33d0: 0a 09 2f 2f 20 4e 6f 74 20 73 68 61 72 65 64 2c ..// Not shared,
33e0: 20 73 6f 20 6e 6f 20 6e 65 65 64 20 74 6f 20 6c so no need to l
33f0: 6f 63 6b 20 66 69 6c 65 20 28 69 74 20 77 6f 75 ock file (it wou
3400: 6c 64 20 66 61 69 6c 20 74 6f 20 6f 70 65 6e 29 ld fail to open)
3410: 0a 09 69 64 2d 3e 68 4d 75 78 20 3d 20 4e 55 4c ..id->hMux = NUL
3420: 4c 3b 0a 09 69 64 2d 3e 68 4d 65 6d 20 3d 20 4e L;..id->hMem = N
3430: 55 4c 4c 3b 0a 09 69 64 2d 3e 6c 6f 63 6b 64 61 ULL;..id->lockda
3440: 74 61 20 3d 20 4e 55 4c 4c 3b 0a 23 65 6e 64 69 ta = NULL;.#endi
3450: 66 0a 09 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b f..OpenCounter(+
3460: 31 29 3b 0a 09 54 52 41 43 45 33 28 22 4f 50 45 1);..TRACE3("OPE
3470: 4e 20 45 58 20 25 64 20 5c 22 25 73 5c 22 5c 6e N EX %d \"%s\"\n
3480: 22 2c 20 68 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 ", h, zFilename)
3490: 3b 0a 09 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ;..return SQLITE
34a0: 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 _OK;.}../*.** At
34b0: 74 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20 61 20 tempt to open a
34c0: 6e 65 77 20 66 69 6c 65 20 66 6f 72 20 72 65 61 new file for rea
34d0: 64 2d 6f 6e 6c 79 20 61 63 63 65 73 73 2e 0a 2a d-only access..*
34e0: 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c *.** On success,
34f0: 20 77 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 write the file
3500: 68 61 6e 64 6c 65 20 69 6e 74 6f 20 2a 69 64 20 handle into *id
3510: 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 and return SQLIT
3520: 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 66 E_OK..**.** On f
3530: 61 69 6c 75 72 65 2c 20 72 65 74 75 72 6e 20 53 ailure, return S
3540: 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 2e 0a QLITE_CANTOPEN..
3550: 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 */.int sqlite3Os
3560: 4f 70 65 6e 52 65 61 64 4f 6e 6c 79 28 20 63 6f OpenReadOnly( co
3570: 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
3580: 61 6d 65 2c 20 4f 73 46 69 6c 65 20 2a 69 64 20 ame, OsFile *id
3590: 29 0a 7b 0a 09 48 41 4e 44 4c 45 20 68 3b 0a 09 ).{..HANDLE h;..
35a0: 57 43 48 41 52 20 2a 20 77 46 69 6c 65 6e 61 6d WCHAR * wFilenam
35b0: 65 20 3d 20 53 74 72 44 75 70 57 28 20 7a 46 69 e = StrDupW( zFi
35c0: 6c 65 6e 61 6d 65 20 29 3b 0a 09 61 73 73 65 72 lename );..asser
35d0: 74 28 20 21 69 64 2d 3e 69 73 4f 70 65 6e 20 29 t( !id->isOpen )
35e0: 3b 0a 09 68 20 3d 20 43 72 65 61 74 65 46 69 6c ;..h = CreateFil
35f0: 65 57 28 20 77 46 69 6c 65 6e 61 6d 65 2c 20 47 eW( wFilename, G
3600: 45 4e 45 52 49 43 5f 52 45 41 44 2c 20 30 2c 20 ENERIC_READ, 0,
3610: 4e 55 4c 4c 2c 20 4f 50 45 4e 5f 45 58 49 53 54 NULL, OPEN_EXIST
3620: 49 4e 47 2c 0a 09 09 09 09 09 46 49 4c 45 5f 41 ING,......FILE_A
3630: 54 54 52 49 42 55 54 45 5f 4e 4f 52 4d 41 4c 20 TTRIBUTE_NORMAL
3640: 7c 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 | FILE_FLAG_RAND
3650: 4f 4d 5f 41 43 43 45 53 53 2c 20 4e 55 4c 4c 20 OM_ACCESS, NULL
3660: 29 3b 0a 09 69 66 20 28 20 68 20 3d 3d 20 49 4e );..if ( h == IN
3670: 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c VALID_HANDLE_VAL
3680: 55 45 20 29 0a 09 7b 0a 09 09 66 72 65 65 28 20 UE )..{...free(
3690: 77 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 09 09 72 wFilename );...r
36a0: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e eturn SQLITE_CAN
36b0: 54 4f 50 45 4e 3b 0a 09 7d 0a 09 69 64 2d 3e 68 TOPEN;..}..id->h
36c0: 20 3d 20 68 3b 0a 09 69 64 2d 3e 6c 6f 63 6b 74 = h;..id->lockt
36d0: 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 09 ype = NO_LOCK;..
36e0: 69 64 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 id->sharedLockBy
36f0: 74 65 20 3d 20 30 3b 0a 09 69 64 2d 3e 69 73 4f te = 0;..id->isO
3700: 70 65 6e 20 3d 20 31 3b 0a 09 69 64 2d 3e 77 46 pen = 1;..id->wF
3710: 69 6c 65 6e 61 6d 65 20 3d 20 77 46 69 6c 65 6e ilename = wFilen
3720: 61 6d 65 3b 0a 09 69 64 2d 3e 64 65 6c 4f 6e 43 ame;..id->delOnC
3730: 6c 6f 73 65 20 3d 20 30 3b 0a 23 69 66 6e 64 65 lose = 0;.#ifnde
3740: 66 20 53 51 4c 49 54 45 5f 57 43 45 5f 4f 4d 49 f SQLITE_WCE_OMI
3750: 54 5f 46 49 4c 45 4c 4f 43 4b 0a 09 2f 2f 20 4e T_FILELOCK..// N
3760: 6f 74 20 73 68 61 72 65 64 2c 20 73 6f 20 6e 6f ot shared, so no
3770: 20 6e 65 65 64 20 74 6f 20 6c 6f 63 6b 20 66 69 need to lock fi
3780: 6c 65 0a 09 69 64 2d 3e 68 4d 75 78 20 3d 20 4e le..id->hMux = N
3790: 55 4c 4c 3b 0a 09 69 64 2d 3e 68 4d 65 6d 20 3d ULL;..id->hMem =
37a0: 20 4e 55 4c 4c 3b 0a 09 69 64 2d 3e 6c 6f 63 6b NULL;..id->lock
37b0: 64 61 74 61 20 3d 20 4e 55 4c 4c 3b 0a 23 65 6e data = NULL;.#en
37c0: 64 69 66 0a 09 4f 70 65 6e 43 6f 75 6e 74 65 72 dif..OpenCounter
37d0: 28 2b 31 29 3b 0a 09 54 52 41 43 45 33 28 22 4f (+1);..TRACE3("O
37e0: 50 45 4e 20 52 4f 20 25 64 20 5c 22 25 73 5c 22 PEN RO %d \"%s\"
37f0: 5c 6e 22 2c 20 68 2c 20 7a 46 69 6c 65 6e 61 6d \n", h, zFilenam
3800: 65 29 3b 0a 09 72 65 74 75 72 6e 20 53 51 4c 49 e);..return SQLI
3810: 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
3820: 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f Close a file..*/
3830: 0a 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c .int sqlite3OsCl
3840: 6f 73 65 28 20 4f 73 46 69 6c 65 20 2a 69 64 20 ose( OsFile *id
3850: 29 0a 7b 0a 09 69 66 20 28 20 69 64 2d 3e 69 73 ).{..if ( id->is
3860: 4f 70 65 6e 20 29 0a 09 7b 0a 09 09 54 52 41 43 Open )..{...TRAC
3870: 45 32 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c E2("CLOSE %d\n",
3880: 20 69 64 2d 3e 68 29 3b 0a 23 69 66 6e 64 65 66 id->h);.#ifndef
3890: 20 53 51 4c 49 54 45 5f 57 43 45 5f 4f 4d 49 54 SQLITE_WCE_OMIT
38a0: 5f 46 49 4c 45 4c 4f 43 4b 0a 09 09 73 71 6c 69 _FILELOCK...sqli
38b0: 74 65 77 63 65 5f 52 65 6c 65 61 73 65 4c 6f 63 tewce_ReleaseLoc
38c0: 6b 73 28 20 69 64 20 29 3b 0a 23 65 6e 64 69 66 ks( id );.#endif
38d0: 0a 09 09 43 6c 6f 73 65 48 61 6e 64 6c 65 28 69 ...CloseHandle(i
38e0: 64 2d 3e 68 29 3b 0a 09 09 4f 70 65 6e 43 6f 75 d->h);...OpenCou
38f0: 6e 74 65 72 28 2d 31 29 3b 0a 09 09 69 64 2d 3e nter(-1);...id->
3900: 69 73 4f 70 65 6e 20 3d 20 30 3b 0a 09 09 69 66 isOpen = 0;...if
3910: 20 28 20 69 64 2d 3e 64 65 6c 4f 6e 43 6c 6f 73 ( id->delOnClos
3920: 65 20 29 0a 09 09 09 44 65 6c 65 74 65 46 69 6c e )....DeleteFil
3930: 65 57 28 20 69 64 2d 3e 77 46 69 6c 65 6e 61 6d eW( id->wFilenam
3940: 65 20 29 3b 0a 09 09 66 72 65 65 28 20 69 64 2d e );...free( id-
3950: 3e 77 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 09 7d >wFilename );..}
3960: 0a 09 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ..return SQLITE_
3970: 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 OK;.}../*.** Loc
3980: 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 k the file with
3990: 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 the lock specifi
39a0: 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 ed by parameter
39b0: 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a locktype - one.*
39c0: 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * of the followi
39d0: 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 ng:.**.** (1
39e0: 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a ) SHARED_LOCK.**
39f0: 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 (2) RESERVE
3a00: 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 D_LOCK.** (3
3a10: 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a ) PENDING_LOCK.*
3a20: 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 * (4) EXCLUS
3a30: 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 IVE_LOCK.**.** S
3a40: 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 ometimes when re
3a50: 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 questing one loc
3a60: 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f k state, additio
3a70: 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a nal lock states.
3a80: 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 ** are inserted
3a90: 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 in between. The
3aa0: 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 locking might f
3ab0: 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 ail on one of th
3ac0: 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 e later.** trans
3ad0: 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 itions leaving t
3ae0: 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 he lock state di
3af0: 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 fferent from wha
3b00: 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 t it started but
3b10: 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 .** still short
3b20: 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 of its goal. Th
3b30: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 e following char
3b40: 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f t shows the allo
3b50: 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f wed.** transitio
3b60: 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 ns and the inser
3b70: 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 ted intermediate
3b80: 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 states:.**.**
3b90: 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 UNLOCKED -> SH
3ba0: 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 ARED.** SHARE
3bb0: 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a D -> RESERVED.**
3bc0: 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 SHARED -> (P
3bd0: 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 ENDING) -> EXCLU
3be0: 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 SIVE.** RESER
3bf0: 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 VED -> (PENDING)
3c00: 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
3c10: 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 PENDING -> E
3c20: 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 XCLUSIVE.**.** T
3c30: 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
3c40: 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 only increase a
3c50: 20 6c 6f 63 6b 2e 20 20 54 68 65 20 73 71 6c 69 lock. The sqli
3c60: 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 20 72 6f te3OsUnlock() ro
3c70: 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73 20 utine.** erases
3c80: 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e 63 all locks at onc
3c90: 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20 75 73 e and returns us
3ca0: 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 immediately to
3cb0: 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e locking level 0.
3cc0: 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f .** It is not po
3cd0: 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72 20 ssible to lower
3ce0: 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
3cf0: 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 20 61 20 l one step at a
3d00: 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d 75 time. You.** mu
3d10: 73 74 20 67 6f 20 73 74 72 61 69 67 68 74 20 74 st go straight t
3d20: 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 o locking level
3d30: 30 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 0..*/.int sqlite
3d40: 33 4f 73 4c 6f 63 6b 28 20 4f 73 46 69 6c 65 20 3OsLock( OsFile
3d50: 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 *id, int locktyp
3d60: 65 20 29 0a 7b 0a 23 69 66 64 65 66 20 53 51 4c e ).{.#ifdef SQL
3d70: 49 54 45 5f 57 43 45 5f 4f 4d 49 54 5f 46 49 4c ITE_WCE_OMIT_FIL
3d80: 45 4c 4f 43 4b 0a 09 69 64 2d 3e 6c 6f 63 6b 74 ELOCK..id->lockt
3d90: 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a ype = locktype;.
3da0: 09 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f .return SQLITE_O
3db0: 4b 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 72 K;.#else. int r
3dc0: 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
3dd0: 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
3de0: 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65 from subroutine
3df0: 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20 3d s */. int res =
3e00: 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 1; /*
3e10: 20 52 65 73 75 6c 74 20 6f 66 20 61 20 77 69 6e Result of a win
3e20: 64 6f 77 73 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a dows lock call *
3e30: 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 /. int newLockt
3e40: 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 ype; /* Se
3e50: 74 20 69 64 2d 3e 6c 6f 63 6b 74 79 70 65 20 74 t id->locktype t
3e60: 6f 20 74 68 69 73 20 76 61 6c 75 65 20 62 65 66 o this value bef
3e70: 6f 72 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a 20 ore exiting */.
3e80: 20 69 6e 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c int gotPendingL
3e90: 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 65 20 ock = 0;/* True
3ea0: 69 66 20 77 65 20 61 63 71 75 69 72 65 64 20 61 if we acquired a
3eb0: 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 PENDING lock th
3ec0: 69 73 20 74 69 6d 65 20 2a 2f 0a 0a 20 20 61 73 is time */.. as
3ed0: 73 65 72 74 28 20 69 64 2d 3e 69 73 4f 70 65 6e sert( id->isOpen
3ee0: 20 29 3b 0a 20 20 54 52 41 43 45 35 28 22 4c 4f );. TRACE5("LO
3ef0: 43 4b 20 25 64 20 25 64 20 77 61 73 20 25 64 28 CK %d %d was %d(
3f00: 25 64 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 %d)\n",.
3f10: 20 20 69 64 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 id->h, locktyp
3f20: 65 2c 20 69 64 2d 3e 6c 6f 63 6b 74 79 70 65 2c e, id->locktype,
3f30: 20 69 64 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 id->sharedLockB
3f40: 79 74 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 yte);.. /* If t
3f50: 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 here is already
3f60: 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 a lock of this t
3f70: 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 ype or more rest
3f80: 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 rictive on the.
3f90: 20 2a 2a 20 4f 73 46 69 6c 65 2c 20 64 6f 20 6e ** OsFile, do n
3fa0: 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 othing. Don't us
3fb0: 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 e the end_lock:
3fc0: 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 exit path, as.
3fd0: 2a 2a 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65 ** sqlite3OsEnte
3fe0: 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 rMutex() hasn't
3ff0: 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e been called yet.
4000: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 64 2d 3e . */. if( id->
4010: 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 locktype>=lockty
4020: 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e pe ){. return
4030: 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
4040: 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 . /* Make sure
4050: 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 the locking sequ
4060: 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74 0a ence is correct.
4070: 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 */. assert( i
4080: 64 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f d->locktype!=NO_
4090: 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 LOCK || locktype
40a0: 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b ==SHARED_LOCK );
40b0: 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
40c0: 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 ype!=PENDING_LOC
40d0: 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c K );. assert( l
40e0: 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45 ocktype!=RESERVE
40f0: 44 5f 4c 4f 43 4b 20 7c 7c 20 69 64 2d 3e 6c 6f D_LOCK || id->lo
4100: 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
4110: 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 OCK );.. /* Loc
4120: 6b 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f k the PENDING_LO
4130: 43 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 CK byte if we ne
4140: 65 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 ed to acquire a
4150: 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a PENDING lock or.
4160: 20 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f ** a SHARED lo
4170: 63 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 ck. If we are a
4180: 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 cquiring a SHARE
4190: 44 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 D lock, the acqu
41a0: 69 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 isition of. **
41b0: 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b the PENDING_LOCK
41c0: 20 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 byte is tempora
41d0: 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f ry.. */. newLo
41e0: 63 6b 74 79 70 65 20 3d 20 69 64 2d 3e 6c 6f 63 cktype = id->loc
41f0: 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 69 64 2d ktype;. if( id-
4200: 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f >locktype==NO_LO
4210: 43 4b 0a 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 CK. || (lockty
4220: 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f pe==EXCLUSIVE_LO
4230: 43 4b 20 26 26 20 69 64 2d 3e 6c 6f 63 6b 74 79 CK && id->lockty
4240: 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 pe==RESERVED_LOC
4250: 4b 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 K). ){. int
4260: 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 77 68 69 cnt = 3;. whi
4270: 6c 65 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28 le( cnt-->0 && (
4280: 72 65 73 20 3d 20 67 65 74 4c 6f 63 6b 28 69 64 res = getLock(id
4290: 2c 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 29 , PENDING_LOCK))
42a0: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ==0 ){. /*
42b0: 54 72 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67 Try 3 times to g
42c0: 65 74 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c et the pending l
42d0: 6f 63 6b 2e 20 20 54 68 65 20 70 65 6e 64 69 6e ock. The pendin
42e0: 67 20 6c 6f 63 6b 20 6d 69 67 68 74 20 62 65 0a g lock might be.
42f0: 20 20 20 20 20 20 2a 2a 20 68 65 6c 64 20 62 79 ** held by
4300: 20 61 6e 6f 74 68 65 72 20 72 65 61 64 65 72 20 another reader
4310: 70 72 6f 63 65 73 73 20 77 68 6f 20 77 69 6c 6c process who will
4320: 20 72 65 6c 65 61 73 65 20 69 74 20 6d 6f 6d 65 release it mome
4330: 6e 74 61 72 69 6c 79 2e 0a 20 20 20 20 20 20 2a ntarily.. *
4340: 2f 0a 20 20 20 20 20 20 54 52 41 43 45 32 28 22 /. TRACE2("
4350: 63 6f 75 6c 64 20 6e 6f 74 20 67 65 74 20 61 20 could not get a
4360: 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 63 6e PENDING lock. cn
4370: 74 3d 25 64 5c 6e 22 2c 20 63 6e 74 29 3b 0a 20 t=%d\n", cnt);.
4380: 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a 20 Sleep(1);.
4390: 20 20 20 7d 0a 20 20 20 20 67 6f 74 50 65 6e 64 }. gotPend
43a0: 69 6e 67 4c 6f 63 6b 20 3d 20 72 65 73 3b 0a 20 ingLock = res;.
43b0: 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 }.. /* Acquire
43c0: 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 a shared lock.
43d0: 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 */. if( lockty
43e0: 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
43f0: 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 61 73 && res ){. as
4400: 73 65 72 74 28 20 69 64 2d 3e 6c 6f 63 6b 74 79 sert( id->lockty
4410: 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 pe==NO_LOCK );.
4420: 20 20 20 72 65 73 20 3d 20 67 65 74 4c 6f 63 6b res = getLock
4430: 28 20 69 64 2c 20 53 48 41 52 45 44 5f 4c 4f 43 ( id, SHARED_LOC
4440: 4b 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 K );. if( res
4450: 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 ){. newLoc
4460: 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c ktype = SHARED_L
4470: 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a OCK;. }. }..
4480: 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 52 /* Acquire a R
4490: 45 53 45 52 56 45 44 20 6c 6f 63 6b 0a 20 20 2a ESERVED lock. *
44a0: 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 /. if( locktype
44b0: 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 ==RESERVED_LOCK
44c0: 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 61 73 && res ){. as
44d0: 73 65 72 74 28 20 69 64 2d 3e 6c 6f 63 6b 74 79 sert( id->lockty
44e0: 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
44f0: 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 67 65 74 );. res = get
4500: 4c 6f 63 6b 28 20 69 64 2c 20 52 45 53 45 52 56 Lock( id, RESERV
4510: 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 69 ED_LOCK );. i
4520: 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 f( res ){.
4530: 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 52 45 newLocktype = RE
4540: 53 45 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 SERVED_LOCK;.
4550: 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 }. }.. /* Acq
4560: 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c uire a PENDING l
4570: 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c ock. */. if( l
4580: 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 ocktype==EXCLUSI
4590: 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 VE_LOCK && res )
45a0: 7b 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 {. newLocktyp
45b0: 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b e = PENDING_LOCK
45c0: 3b 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 ;. gotPending
45d0: 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 Lock = 0;. }..
45e0: 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45 /* Acquire an E
45f0: 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 0a 20 20 XCLUSIVE lock.
4600: 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 */. if( locktyp
4610: 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 e==EXCLUSIVE_LOC
4620: 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 K && res ){.
4630: 61 73 73 65 72 74 28 20 69 64 2d 3e 6c 6f 63 6b assert( id->lock
4640: 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 type>=SHARED_LOC
4650: 4b 20 29 3b 0a 2f 2f 20 20 72 65 73 20 3d 20 75 K );.// res = u
4660: 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 69 64 nlockReadLock(id
4670: 29 3b 0a 2f 2f 20 20 54 52 41 43 45 32 28 22 75 );.// TRACE2("u
4680: 6e 72 65 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e nreadlock = %d\n
4690: 22 2c 20 72 65 73 29 3b 0a 20 20 20 20 72 65 73 ", res);. res
46a0: 20 3d 20 67 65 74 4c 6f 63 6b 28 20 69 64 2c 20 = getLock( id,
46b0: 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 EXCLUSIVE_LOCK )
46c0: 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b ;. if( res ){
46d0: 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 . newLockty
46e0: 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c pe = EXCLUSIVE_L
46f0: 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a OCK;. }else{.
4700: 20 20 20 20 20 20 54 52 41 43 45 32 28 22 65 72 TRACE2("er
4710: 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e 22 ror-code = %d\n"
4720: 2c 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 , GetLastError()
4730: 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
4740: 2f 2a 20 49 66 20 77 65 20 61 72 65 20 68 6f 6c /* If we are hol
4750: 64 69 6e 67 20 61 20 50 45 4e 44 49 4e 47 20 6c ding a PENDING l
4760: 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 74 20 74 ock that ought t
4770: 6f 20 62 65 20 72 65 6c 65 61 73 65 64 2c 20 74 o be released, t
4780: 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65 hen. ** release
4790: 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 it now.. */.
47a0: 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f if( gotPendingLo
47b0: 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d ck && locktype==
47c0: 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SHARED_LOCK ){.
47d0: 20 20 20 75 6e 73 65 74 4c 6f 63 6b 28 20 69 64 unsetLock( id
47e0: 2c 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 , PENDING_LOCK )
47f0: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 ;. }.. /* Upda
4800: 74 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 te the state of
4810: 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c the lock has hel
4820: 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 d in the file de
4830: 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 scriptor then.
4840: 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 70 ** return the ap
4850: 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74 propriate result
4860: 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 code.. */. if
4870: 28 20 72 65 73 20 29 7b 0a 20 20 20 20 72 63 20 ( res ){. rc
4880: 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d = SQLITE_OK;. }
4890: 65 6c 73 65 7b 0a 20 20 20 20 54 52 41 43 45 34 else{. TRACE4
48a0: 28 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 ("LOCK FAILED %d
48b0: 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20 62 trying for %d b
48c0: 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 69 64 ut got %d\n", id
48d0: 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ->h,.
48e0: 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77 4c 6f 63 locktype, newLoc
48f0: 6b 74 79 70 65 29 3b 0a 20 20 20 20 72 63 20 3d ktype);. rc =
4900: 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
4910: 7d 0a 20 20 69 64 2d 3e 6c 6f 63 6b 74 79 70 65 }. id->locktype
4920: 20 3d 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 0a = newLocktype;.
4930: 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e return rc;.#en
4940: 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 dif.}../*.** Thi
4950: 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 s routine checks
4960: 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 if there is a R
4970: 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c ESERVED lock hel
4980: 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 d on the specifi
4990: 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 ed.** file by th
49a0: 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 is or any other
49b0: 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 process. If such
49c0: 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c a lock is held,
49d0: 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a return.** non-z
49e0: 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 7a ero, otherwise z
49f0: 65 72 6f 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 ero..*/.int sqli
4a00: 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 te3OsCheckReserv
4a10: 65 64 4c 6f 63 6b 28 20 4f 73 46 69 6c 65 20 2a edLock( OsFile *
4a20: 69 64 20 29 0a 7b 0a 20 20 69 6e 74 20 72 63 3b id ).{. int rc;
4a30: 0a 20 20 61 73 73 65 72 74 28 20 69 64 2d 3e 69 . assert( id->i
4a40: 73 4f 70 65 6e 20 29 3b 0a 20 20 69 66 28 20 69 sOpen );. if( i
4a50: 64 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53 d->locktype>=RES
4a60: 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 ERVED_LOCK ){.
4a70: 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 54 52 rc = 1;. TR
4a80: 41 43 45 33 28 22 54 45 53 54 20 57 52 2d 4c 4f ACE3("TEST WR-LO
4a90: 43 4b 20 25 64 20 25 64 20 28 6c 6f 63 61 6c 29 CK %d %d (local)
4aa0: 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 63 29 3b \n", id->h, rc);
4ab0: 0a 20 20 7d 65 6c 73 65 7b 0a 23 69 66 64 65 66 . }else{.#ifdef
4ac0: 20 53 51 4c 49 54 45 5f 57 43 45 5f 4f 4d 49 54 SQLITE_WCE_OMIT
4ad0: 5f 46 49 4c 45 4c 4f 43 4b 0a 09 72 63 20 3d 20 _FILELOCK..rc =
4ae0: 30 3b 0a 23 65 6c 73 65 0a 20 20 20 20 2f 2a 20 0;.#else. /*
4af0: 4f 6e 6c 79 20 61 6e 20 61 74 6f 6d 69 63 20 72 Only an atomic r
4b00: 65 61 64 2c 20 6e 6f 20 6e 65 65 64 20 74 6f 20 ead, no need to
4b10: 6c 6f 63 6b 5f 66 69 6c 65 28 29 20 2a 2f 0a 20 lock_file() */.
4b20: 20 20 20 72 63 20 3d 20 28 20 69 64 2d 3e 6c 6f rc = ( id->lo
4b30: 63 6b 64 61 74 61 2d 3e 6c 6f 63 6b 20 26 20 28 ckdata->lock & (
4b40: 31 3c 3c 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 1<<RESERVED_LOCK
4b50: 29 20 29 20 21 3d 20 30 3b 0a 20 20 20 20 54 52 ) ) != 0;. TR
4b60: 41 43 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c ACE3( "TEST WR-L
4b70: 4f 43 4b 20 25 64 20 25 64 20 28 72 65 6d 6f 74 OCK %d %d (remot
4b80: 65 29 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 63 e)\n", id->h, rc
4b90: 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 );.#endif. }.
4ba0: 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
4bb0: 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c *.** Lower the l
4bc0: 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 ocking level on
4bd0: 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
4be0: 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 id to locktype.
4bf0: 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 locktype.** mus
4c00: 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c t be either NO_L
4c10: 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f OCK or SHARED_LO
4c20: 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 CK..**.** If the
4c30: 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f locking level o
4c40: 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 f the file descr
4c50: 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 iptor is already
4c60: 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 at or below.**
4c70: 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f the requested lo
4c80: 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 cking level, thi
4c90: 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e s routine is a n
4ca0: 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 o-op..**.** It i
4cb0: 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 s not possible f
4cc0: 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 or this routine
4cd0: 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65 20 73 to fail if the s
4ce0: 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a econd argument.*
4cf0: 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49 * is NO_LOCK. I
4d00: 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 f the second arg
4d10: 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45 44 5f ument is SHARED_
4d20: 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73 20 72 LOCK then this r
4d30: 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 20 outine.** might
4d40: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
4d50: 45 52 52 3b 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 ERR;.*/.int sqli
4d60: 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 20 4f 73 46 te3OsUnlock( OsF
4d70: 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
4d80: 6b 74 79 70 65 20 29 0a 7b 0a 23 69 66 64 65 66 ktype ).{.#ifdef
4d90: 20 53 51 4c 49 54 45 5f 57 43 45 5f 4f 4d 49 54 SQLITE_WCE_OMIT
4da0: 5f 46 49 4c 45 4c 4f 43 4b 0a 20 20 72 65 74 75 _FILELOCK. retu
4db0: 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 rn SQLITE_OK;.#e
4dc0: 6c 73 65 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a lse. int type;.
4dd0: 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
4de0: 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 E_OK;. assert(
4df0: 69 64 2d 3e 69 73 4f 70 65 6e 20 29 3b 0a 20 20 id->isOpen );.
4e00: 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
4e10: 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b <=SHARED_LOCK );
4e20: 0a 20 20 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 . TRACE5("UNLOC
4e30: 4b 20 25 64 20 74 6f 20 25 64 20 77 61 73 20 25 K %d to %d was %
4e40: 64 28 25 64 29 5c 6e 22 2c 20 69 64 2d 3e 68 2c d(%d)\n", id->h,
4e50: 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 locktype,.
4e60: 20 20 20 20 20 69 64 2d 3e 6c 6f 63 6b 74 79 70 id->locktyp
4e70: 65 2c 20 69 64 2d 3e 73 68 61 72 65 64 4c 6f 63 e, id->sharedLoc
4e80: 6b 42 79 74 65 29 3b 0a 20 20 74 79 70 65 20 3d kByte);. type =
4e90: 20 69 64 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 id->locktype;.
4ea0: 20 69 66 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 if( type>=EXCLU
4eb0: 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 09 75 6e SIVE_LOCK ){..un
4ec0: 73 65 74 4c 6f 63 6b 28 20 69 64 2c 20 45 58 43 setLock( id, EXC
4ed0: 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 20 LUSIVE_LOCK );.
4ee0: 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 }. if( type>=R
4ef0: 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a ESERVED_LOCK ){.
4f00: 09 75 6e 73 65 74 4c 6f 63 6b 28 20 69 64 2c 20 .unsetLock( id,
4f10: 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 3b RESERVED_LOCK );
4f20: 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 . }. if( lockt
4f30: 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20 ype==NO_LOCK &&
4f40: 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 type>=SHARED_LOC
4f50: 4b 20 29 7b 0a 20 20 20 20 75 6e 73 65 74 4c 6f K ){. unsetLo
4f60: 63 6b 28 20 69 64 2c 20 53 48 41 52 45 44 5f 4c ck( id, SHARED_L
4f70: 4f 43 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 OCK );. }. if(
4f80: 20 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c type>=PENDING_L
4f90: 4f 43 4b 20 29 7b 0a 20 20 20 20 75 6e 73 65 74 OCK ){. unset
4fa0: 4c 6f 63 6b 28 20 69 64 2c 20 50 45 4e 44 49 4e Lock( id, PENDIN
4fb0: 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 7d 0a 20 20 G_LOCK );. }.
4fc0: 69 64 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c id->locktype = l
4fd0: 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 ocktype;. retur
4fe0: 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a n rc;.#endif.}..
4ff0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
5000: 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d OMIT_PAGER_PRAGM
5010: 41 53 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 AS./*.** Check t
5020: 68 61 74 20 61 20 67 69 76 65 6e 20 70 61 74 68 hat a given path
5030: 6e 61 6d 65 20 69 73 20 61 20 64 69 72 65 63 74 name is a direct
5040: 6f 72 79 20 61 6e 64 20 69 73 20 77 72 69 74 61 ory and is writa
5050: 62 6c 65 20 0a 2a 2a 0a 2a 2f 0a 69 6e 74 20 73 ble .**.*/.int s
5060: 71 6c 69 74 65 33 4f 73 49 73 44 69 72 57 72 69 qlite3OsIsDirWri
5070: 74 61 62 6c 65 28 63 68 61 72 20 2a 7a 42 75 66 table(char *zBuf
5080: 29 7b 0a 20 20 69 6e 74 20 66 69 6c 65 41 74 74 ){. int fileAtt
5090: 72 3b 0a 20 20 69 66 28 21 20 7a 42 75 66 20 29 r;. if(! zBuf )
50a0: 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 66 69 6c return 0;. fil
50b0: 65 41 74 74 72 20 3d 20 47 65 74 46 69 6c 65 41 eAttr = GetFileA
50c0: 74 74 72 69 62 75 74 65 73 41 28 7a 42 75 66 29 ttributesA(zBuf)
50d0: 3b 0a 20 20 69 66 28 20 66 69 6c 65 41 74 74 72 ;. if( fileAttr
50e0: 20 3d 3d 20 30 78 66 66 66 66 66 66 66 66 20 29 == 0xffffffff )
50f0: 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 return 0;. if(
5100: 20 28 66 69 6c 65 41 74 74 72 20 26 20 46 49 4c (fileAttr & FIL
5110: 45 5f 41 54 54 52 49 42 55 54 45 5f 44 49 52 45 E_ATTRIBUTE_DIRE
5120: 43 54 4f 52 59 29 20 21 3d 20 46 49 4c 45 5f 41 CTORY) != FILE_A
5130: 54 54 52 49 42 55 54 45 5f 44 49 52 45 43 54 4f TTRIBUTE_DIRECTO
5140: 52 59 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e RY ){. return
5150: 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0;. }. return
5160: 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 1;.}.#endif /*
5170: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 SQLITE_OMIT_PAGE
5180: 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 0a 23 R_PRAGMAS */...#
5190: 65 6e 64 69 66 20 2f 2a 20 5f 57 49 4e 33 32 5f endif /* _WIN32_
51a0: 57 43 45 20 2a 2f 0a 0a 7d 0a WCE */..}.