annotate sqlite/os_win.c @ 1474:6d690e50811f trunk

[svn] - remove dependency on the client library.
author nenolod
date Wed, 02 Aug 2006 22:41:00 -0700
parents b6b61becdf4e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1434
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
2 ** 2004 May 22
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
3 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
4 ** The author disclaims copyright to this source code. In place of
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
5 ** a legal notice, here is a blessing:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
6 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
7 ** May you do good and not evil.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
8 ** May you find forgiveness for yourself and forgive others.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
9 ** May you share freely, never taking more than you give.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
10 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
11 ******************************************************************************
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
12 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
13 ** This file contains code that is specific to windows.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
14 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
15 #include "sqliteInt.h"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
16 #include "os.h"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
17 #if OS_WIN /* This file is used for windows only */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
18
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
19 #include <winbase.h>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
20
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
21 #ifdef __CYGWIN__
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
22 # include <sys/cygwin.h>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
23 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
24
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
25 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
26 ** Macros used to determine whether or not to use threads.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
27 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
28 #if defined(THREADSAFE) && THREADSAFE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
29 # define SQLITE_W32_THREADS 1
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
30 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
31
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
32 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
33 ** Include code that is common to all os_*.c files
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
34 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
35 #include "os_common.h"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
36
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
37 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
38 ** Determine if we are dealing with WindowsCE - which has a much
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
39 ** reduced API.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
40 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
41 #if defined(_WIN32_WCE)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
42 # define OS_WINCE 1
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
43 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
44 # define OS_WINCE 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
45 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
46
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
47 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
48 ** WinCE lacks native support for file locking so we have to fake it
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
49 ** with some code of our own.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
50 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
51 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
52 typedef struct winceLock {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
53 int nReaders; /* Number of reader locks obtained */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
54 BOOL bPending; /* Indicates a pending lock has been obtained */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
55 BOOL bReserved; /* Indicates a reserved lock has been obtained */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
56 BOOL bExclusive; /* Indicates an exclusive lock has been obtained */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
57 } winceLock;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
58 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
59
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
60 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
61 ** The winFile structure is a subclass of OsFile specific to the win32
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
62 ** portability layer.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
63 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
64 typedef struct winFile winFile;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
65 struct winFile {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
66 IoMethod const *pMethod;/* Must be first */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
67 HANDLE h; /* Handle for accessing the file */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
68 unsigned char locktype; /* Type of lock currently held on this file */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
69 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
70 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
71 WCHAR *zDeleteOnClose; /* Name of file to delete when closing */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
72 HANDLE hMutex; /* Mutex used to control access to shared lock */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
73 HANDLE hShared; /* Shared memory segment used for locking */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
74 winceLock local; /* Locks obtained by this instance of winFile */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
75 winceLock *shared; /* Global shared lock memory for the file */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
76 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
77 };
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
78
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
79
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
80 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
81 ** Do not include any of the File I/O interface procedures if the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
82 ** SQLITE_OMIT_DISKIO macro is defined (indicating that there database
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
83 ** will be in-memory only)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
84 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
85 #ifndef SQLITE_OMIT_DISKIO
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
86
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
87 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
88 ** The following variable is (normally) set once and never changes
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
89 ** thereafter. It records whether the operating system is Win95
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
90 ** or WinNT.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
91 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
92 ** 0: Operating system unknown.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
93 ** 1: Operating system is Win95.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
94 ** 2: Operating system is WinNT.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
95 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
96 ** In order to facilitate testing on a WinNT system, the test fixture
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
97 ** can manually set this value to 1 to emulate Win98 behavior.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
98 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
99 int sqlite3_os_type = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
100
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
101 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
102 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
103 ** or WinCE. Return false (zero) for Win95, Win98, or WinME.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
104 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
105 ** Here is an interesting observation: Win95, Win98, and WinME lack
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
106 ** the LockFileEx() API. But we can still statically link against that
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
107 ** API as long as we don't call it win running Win95/98/ME. A call to
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
108 ** this routine is used to determine if the host is Win95/98/ME or
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
109 ** WinNT/2K/XP so that we will know whether or not we can safely call
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
110 ** the LockFileEx() API.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
111 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
112 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
113 # define isNT() (1)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
114 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
115 static int isNT(void){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
116 if( sqlite3_os_type==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
117 OSVERSIONINFO sInfo;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
118 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
119 GetVersionEx(&sInfo);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
120 sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
121 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
122 return sqlite3_os_type==2;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
123 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
124 #endif /* OS_WINCE */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
125
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
126 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
127 ** Convert a UTF-8 string to UTF-32. Space to hold the returned string
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
128 ** is obtained from sqliteMalloc.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
129 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
130 static WCHAR *utf8ToUnicode(const char *zFilename){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
131 int nChar;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
132 WCHAR *zWideFilename;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
133
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
134 if( !isNT() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
135 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
136 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
137 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
138 zWideFilename = sqliteMalloc( nChar*sizeof(zWideFilename[0]) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
139 if( zWideFilename==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
140 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
141 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
142 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
143 if( nChar==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
144 sqliteFree(zWideFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
145 zWideFilename = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
146 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
147 return zWideFilename;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
148 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
149
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
150 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
151 ** Convert UTF-32 to UTF-8. Space to hold the returned string is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
152 ** obtained from sqliteMalloc().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
153 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
154 static char *unicodeToUtf8(const WCHAR *zWideFilename){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
155 int nByte;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
156 char *zFilename;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
157
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
158 nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
159 zFilename = sqliteMalloc( nByte );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
160 if( zFilename==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
161 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
162 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
163 nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
164 0, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
165 if( nByte == 0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
166 sqliteFree(zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
167 zFilename = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
168 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
169 return zFilename;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
170 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
171
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
172 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
173 /*************************************************************************
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
174 ** This section contains code for WinCE only.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
175 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
176 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
177 ** WindowsCE does not have a localtime() function. So create a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
178 ** substitute.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
179 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
180 #include <time.h>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
181 struct tm *__cdecl localtime(const time_t *t)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
182 {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
183 static struct tm y;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
184 FILETIME uTm, lTm;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
185 SYSTEMTIME pTm;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
186 i64 t64;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
187 t64 = *t;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
188 t64 = (t64 + 11644473600)*10000000;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
189 uTm.dwLowDateTime = t64 & 0xFFFFFFFF;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
190 uTm.dwHighDateTime= t64 >> 32;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
191 FileTimeToLocalFileTime(&uTm,&lTm);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
192 FileTimeToSystemTime(&lTm,&pTm);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
193 y.tm_year = pTm.wYear - 1900;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
194 y.tm_mon = pTm.wMonth - 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
195 y.tm_wday = pTm.wDayOfWeek;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
196 y.tm_mday = pTm.wDay;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
197 y.tm_hour = pTm.wHour;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
198 y.tm_min = pTm.wMinute;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
199 y.tm_sec = pTm.wSecond;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
200 return &y;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
201 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
202
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
203 /* This will never be called, but defined to make the code compile */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
204 #define GetTempPathA(a,b)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
205
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
206 #define LockFile(a,b,c,d,e) winceLockFile(&a, b, c, d, e)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
207 #define UnlockFile(a,b,c,d,e) winceUnlockFile(&a, b, c, d, e)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
208 #define LockFileEx(a,b,c,d,e,f) winceLockFileEx(&a, b, c, d, e, f)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
209
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
210 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-offsetof(winFile,h)]
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
211
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
212 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
213 ** Acquire a lock on the handle h
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
214 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
215 static void winceMutexAcquire(HANDLE h){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
216 DWORD dwErr;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
217 do {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
218 dwErr = WaitForSingleObject(h, INFINITE);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
219 } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
220 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
221 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
222 ** Release a lock acquired by winceMutexAcquire()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
223 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
224 #define winceMutexRelease(h) ReleaseMutex(h)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
225
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
226 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
227 ** Create the mutex and shared memory used for locking in the file
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
228 ** descriptor pFile
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
229 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
230 static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
231 WCHAR *zTok;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
232 WCHAR *zName = utf8ToUnicode(zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
233 BOOL bInit = TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
234
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
235 /* Initialize the local lockdata */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
236 ZeroMemory(&pFile->local, sizeof(pFile->local));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
237
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
238 /* Replace the backslashes from the filename and lowercase it
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
239 ** to derive a mutex name. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
240 zTok = CharLowerW(zName);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
241 for (;*zTok;zTok++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
242 if (*zTok == '\\') *zTok = '_';
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
243 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
244
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
245 /* Create/open the named mutex */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
246 pFile->hMutex = CreateMutexW(NULL, FALSE, zName);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
247 if (!pFile->hMutex){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
248 sqliteFree(zName);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
249 return FALSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
250 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
251
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
252 /* Acquire the mutex before continuing */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
253 winceMutexAcquire(pFile->hMutex);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
254
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
255 /* Since the names of named mutexes, semaphores, file mappings etc are
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
256 ** case-sensitive, take advantage of that by uppercasing the mutex name
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
257 ** and using that as the shared filemapping name.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
258 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
259 CharUpperW(zName);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
260 pFile->hShared = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
261 PAGE_READWRITE, 0, sizeof(winceLock),
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
262 zName);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
263
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
264 /* Set a flag that indicates we're the first to create the memory so it
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
265 ** must be zero-initialized */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
266 if (GetLastError() == ERROR_ALREADY_EXISTS){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
267 bInit = FALSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
268 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
269
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
270 sqliteFree(zName);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
271
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
272 /* If we succeeded in making the shared memory handle, map it. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
273 if (pFile->hShared){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
274 pFile->shared = (winceLock*)MapViewOfFile(pFile->hShared,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
275 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
276 /* If mapping failed, close the shared memory handle and erase it */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
277 if (!pFile->shared){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
278 CloseHandle(pFile->hShared);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
279 pFile->hShared = NULL;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
280 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
281 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
282
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
283 /* If shared memory could not be created, then close the mutex and fail */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
284 if (pFile->hShared == NULL){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
285 winceMutexRelease(pFile->hMutex);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
286 CloseHandle(pFile->hMutex);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
287 pFile->hMutex = NULL;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
288 return FALSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
289 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
290
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
291 /* Initialize the shared memory if we're supposed to */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
292 if (bInit) {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
293 ZeroMemory(pFile->shared, sizeof(winceLock));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
294 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
295
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
296 winceMutexRelease(pFile->hMutex);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
297 return TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
298 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
299
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
300 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
301 ** Destroy the part of winFile that deals with wince locks
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
302 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
303 static void winceDestroyLock(winFile *pFile){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
304 if (pFile->hMutex){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
305 /* Acquire the mutex */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
306 winceMutexAcquire(pFile->hMutex);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
307
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
308 /* The following blocks should probably assert in debug mode, but they
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
309 are to cleanup in case any locks remained open */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
310 if (pFile->local.nReaders){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
311 pFile->shared->nReaders --;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
312 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
313 if (pFile->local.bReserved){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
314 pFile->shared->bReserved = FALSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
315 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
316 if (pFile->local.bPending){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
317 pFile->shared->bPending = FALSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
318 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
319 if (pFile->local.bExclusive){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
320 pFile->shared->bExclusive = FALSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
321 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
322
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
323 /* De-reference and close our copy of the shared memory handle */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
324 UnmapViewOfFile(pFile->shared);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
325 CloseHandle(pFile->hShared);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
326
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
327 /* Done with the mutex */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
328 winceMutexRelease(pFile->hMutex);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
329 CloseHandle(pFile->hMutex);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
330 pFile->hMutex = NULL;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
331 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
332 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
333
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
334 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
335 ** An implementation of the LockFile() API of windows for wince
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
336 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
337 static BOOL winceLockFile(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
338 HANDLE *phFile,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
339 DWORD dwFileOffsetLow,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
340 DWORD dwFileOffsetHigh,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
341 DWORD nNumberOfBytesToLockLow,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
342 DWORD nNumberOfBytesToLockHigh
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
343 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
344 winFile *pFile = HANDLE_TO_WINFILE(phFile);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
345 BOOL bReturn = FALSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
346
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
347 if (!pFile->hMutex) return TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
348 winceMutexAcquire(pFile->hMutex);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
349
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
350 /* Wanting an exclusive lock? */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
351 if (dwFileOffsetLow == SHARED_FIRST
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
352 && nNumberOfBytesToLockLow == SHARED_SIZE){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
353 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
354 pFile->shared->bExclusive = TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
355 pFile->local.bExclusive = TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
356 bReturn = TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
357 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
358 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
359
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
360 /* Want a read-only lock? */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
361 else if ((dwFileOffsetLow >= SHARED_FIRST &&
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
362 dwFileOffsetLow < SHARED_FIRST + SHARED_SIZE) &&
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
363 nNumberOfBytesToLockLow == 1){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
364 if (pFile->shared->bExclusive == 0){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
365 pFile->local.nReaders ++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
366 if (pFile->local.nReaders == 1){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
367 pFile->shared->nReaders ++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
368 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
369 bReturn = TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
370 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
371 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
372
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
373 /* Want a pending lock? */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
374 else if (dwFileOffsetLow == PENDING_BYTE && nNumberOfBytesToLockLow == 1){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
375 /* If no pending lock has been acquired, then acquire it */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
376 if (pFile->shared->bPending == 0) {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
377 pFile->shared->bPending = TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
378 pFile->local.bPending = TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
379 bReturn = TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
380 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
381 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
382 /* Want a reserved lock? */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
383 else if (dwFileOffsetLow == RESERVED_BYTE && nNumberOfBytesToLockLow == 1){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
384 if (pFile->shared->bReserved == 0) {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
385 pFile->shared->bReserved = TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
386 pFile->local.bReserved = TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
387 bReturn = TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
388 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
389 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
390
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
391 winceMutexRelease(pFile->hMutex);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
392 return bReturn;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
393 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
394
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
395 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
396 ** An implementation of the UnlockFile API of windows for wince
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
397 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
398 static BOOL winceUnlockFile(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
399 HANDLE *phFile,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
400 DWORD dwFileOffsetLow,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
401 DWORD dwFileOffsetHigh,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
402 DWORD nNumberOfBytesToUnlockLow,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
403 DWORD nNumberOfBytesToUnlockHigh
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
404 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
405 winFile *pFile = HANDLE_TO_WINFILE(phFile);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
406 BOOL bReturn = FALSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
407
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
408 if (!pFile->hMutex) return TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
409 winceMutexAcquire(pFile->hMutex);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
410
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
411 /* Releasing a reader lock or an exclusive lock */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
412 if (dwFileOffsetLow >= SHARED_FIRST &&
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
413 dwFileOffsetLow < SHARED_FIRST + SHARED_SIZE){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
414 /* Did we have an exclusive lock? */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
415 if (pFile->local.bExclusive){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
416 pFile->local.bExclusive = FALSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
417 pFile->shared->bExclusive = FALSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
418 bReturn = TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
419 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
420
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
421 /* Did we just have a reader lock? */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
422 else if (pFile->local.nReaders){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
423 pFile->local.nReaders --;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
424 if (pFile->local.nReaders == 0)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
425 {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
426 pFile->shared->nReaders --;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
427 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
428 bReturn = TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
429 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
430 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
431
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
432 /* Releasing a pending lock */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
433 else if (dwFileOffsetLow == PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
434 if (pFile->local.bPending){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
435 pFile->local.bPending = FALSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
436 pFile->shared->bPending = FALSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
437 bReturn = TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
438 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
439 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
440 /* Releasing a reserved lock */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
441 else if (dwFileOffsetLow == RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
442 if (pFile->local.bReserved) {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
443 pFile->local.bReserved = FALSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
444 pFile->shared->bReserved = FALSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
445 bReturn = TRUE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
446 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
447 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
448
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
449 winceMutexRelease(pFile->hMutex);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
450 return bReturn;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
451 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
452
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
453 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
454 ** An implementation of the LockFileEx() API of windows for wince
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
455 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
456 static BOOL winceLockFileEx(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
457 HANDLE *phFile,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
458 DWORD dwFlags,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
459 DWORD dwReserved,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
460 DWORD nNumberOfBytesToLockLow,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
461 DWORD nNumberOfBytesToLockHigh,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
462 LPOVERLAPPED lpOverlapped
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
463 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
464 /* If the caller wants a shared read lock, forward this call
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
465 ** to winceLockFile */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
466 if (lpOverlapped->Offset == SHARED_FIRST &&
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
467 dwFlags == 1 &&
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
468 nNumberOfBytesToLockLow == SHARED_SIZE){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
469 return winceLockFile(phFile, SHARED_FIRST, 0, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
470 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
471 return FALSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
472 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
473 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
474 ** End of the special code for wince
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
475 *****************************************************************************/
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
476 #endif /* OS_WINCE */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
477
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
478 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
479 ** Delete the named file.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
480 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
481 ** Note that windows does not allow a file to be deleted if some other
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
482 ** process has it open. Sometimes a virus scanner or indexing program
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
483 ** will open a journal file shortly after it is created in order to do
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
484 ** whatever it is it does. While this other process is holding the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
485 ** file open, we will be unable to delete it. To work around this
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
486 ** problem, we delay 100 milliseconds and try to delete again. Up
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
487 ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
488 ** up and returning an error.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
489 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
490 #define MX_DELETION_ATTEMPTS 3
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
491 int sqlite3WinDelete(const char *zFilename){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
492 WCHAR *zWide = utf8ToUnicode(zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
493 int cnt = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
494 int rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
495 if( zWide ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
496 do{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
497 rc = DeleteFileW(zWide);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
498 }while( rc==0 && cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
499 sqliteFree(zWide);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
500 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
501 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
502 return SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
503 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
504 do{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
505 rc = DeleteFileA(zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
506 }while( rc==0 && cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
507 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
508 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
509 TRACE2("DELETE \"%s\"\n", zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
510 return rc==0 ? SQLITE_OK : SQLITE_IOERR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
511 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
512
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
513 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
514 ** Return TRUE if the named file exists.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
515 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
516 int sqlite3WinFileExists(const char *zFilename){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
517 int exists = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
518 WCHAR *zWide = utf8ToUnicode(zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
519 if( zWide ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
520 exists = GetFileAttributesW(zWide) != 0xffffffff;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
521 sqliteFree(zWide);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
522 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
523 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
524 return SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
525 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
526 exists = GetFileAttributesA(zFilename) != 0xffffffff;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
527 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
528 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
529 return exists;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
530 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
531
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
532 /* Forward declaration */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
533 static int allocateWinFile(winFile *pInit, OsFile **pId);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
534
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
535 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
536 ** Attempt to open a file for both reading and writing. If that
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
537 ** fails, try opening it read-only. If the file does not exist,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
538 ** try to create it.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
539 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
540 ** On success, a handle for the open file is written to *id
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
541 ** and *pReadonly is set to 0 if the file was opened for reading and
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
542 ** writing or 1 if the file was opened read-only. The function returns
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
543 ** SQLITE_OK.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
544 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
545 ** On failure, the function returns SQLITE_CANTOPEN and leaves
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
546 ** *id and *pReadonly unchanged.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
547 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
548 int sqlite3WinOpenReadWrite(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
549 const char *zFilename,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
550 OsFile **pId,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
551 int *pReadonly
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
552 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
553 winFile f;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
554 HANDLE h;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
555 WCHAR *zWide = utf8ToUnicode(zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
556 assert( *pId==0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
557 if( zWide ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
558 h = CreateFileW(zWide,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
559 GENERIC_READ | GENERIC_WRITE,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
560 FILE_SHARE_READ | FILE_SHARE_WRITE,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
561 NULL,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
562 OPEN_ALWAYS,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
563 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
564 NULL
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
565 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
566 if( h==INVALID_HANDLE_VALUE ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
567 h = CreateFileW(zWide,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
568 GENERIC_READ,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
569 FILE_SHARE_READ | FILE_SHARE_WRITE,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
570 NULL,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
571 OPEN_ALWAYS,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
572 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
573 NULL
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
574 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
575 if( h==INVALID_HANDLE_VALUE ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
576 sqliteFree(zWide);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
577 return SQLITE_CANTOPEN;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
578 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
579 *pReadonly = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
580 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
581 *pReadonly = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
582 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
583 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
584 if (!winceCreateLock(zFilename, &f)){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
585 CloseHandle(h);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
586 sqliteFree(zWide);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
587 return SQLITE_CANTOPEN;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
588 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
589 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
590 sqliteFree(zWide);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
591 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
592 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
593 return SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
594 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
595 h = CreateFileA(zFilename,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
596 GENERIC_READ | GENERIC_WRITE,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
597 FILE_SHARE_READ | FILE_SHARE_WRITE,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
598 NULL,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
599 OPEN_ALWAYS,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
600 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
601 NULL
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
602 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
603 if( h==INVALID_HANDLE_VALUE ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
604 h = CreateFileA(zFilename,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
605 GENERIC_READ,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
606 FILE_SHARE_READ | FILE_SHARE_WRITE,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
607 NULL,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
608 OPEN_ALWAYS,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
609 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
610 NULL
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
611 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
612 if( h==INVALID_HANDLE_VALUE ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
613 return SQLITE_CANTOPEN;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
614 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
615 *pReadonly = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
616 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
617 *pReadonly = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
618 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
619 #endif /* OS_WINCE */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
620 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
621 f.h = h;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
622 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
623 f.zDeleteOnClose = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
624 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
625 TRACE3("OPEN R/W %d \"%s\"\n", h, zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
626 return allocateWinFile(&f, pId);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
627 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
628
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
629
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
630 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
631 ** Attempt to open a new file for exclusive access by this process.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
632 ** The file will be opened for both reading and writing. To avoid
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
633 ** a potential security problem, we do not allow the file to have
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
634 ** previously existed. Nor do we allow the file to be a symbolic
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
635 ** link.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
636 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
637 ** If delFlag is true, then make arrangements to automatically delete
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
638 ** the file when it is closed.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
639 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
640 ** On success, write the file handle into *id and return SQLITE_OK.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
641 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
642 ** On failure, return SQLITE_CANTOPEN.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
643 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
644 ** Sometimes if we have just deleted a prior journal file, windows
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
645 ** will fail to open a new one because there is a "pending delete".
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
646 ** To work around this bug, we pause for 100 milliseconds and attempt
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
647 ** a second open after the first one fails. The whole operation only
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
648 ** fails if both open attempts are unsuccessful.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
649 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
650 int sqlite3WinOpenExclusive(const char *zFilename, OsFile **pId, int delFlag){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
651 winFile f;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
652 HANDLE h;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
653 int fileflags;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
654 WCHAR *zWide = utf8ToUnicode(zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
655 assert( *pId == 0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
656 fileflags = FILE_FLAG_RANDOM_ACCESS;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
657 #if !OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
658 if( delFlag ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
659 fileflags |= FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
660 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
661 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
662 if( zWide ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
663 int cnt = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
664 do{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
665 h = CreateFileW(zWide,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
666 GENERIC_READ | GENERIC_WRITE,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
667 0,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
668 NULL,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
669 CREATE_ALWAYS,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
670 fileflags,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
671 NULL
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
672 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
673 }while( h==INVALID_HANDLE_VALUE && cnt++ < 2 && (Sleep(100), 1) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
674 sqliteFree(zWide);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
675 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
676 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
677 return SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
678 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
679 int cnt = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
680 do{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
681 h = CreateFileA(zFilename,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
682 GENERIC_READ | GENERIC_WRITE,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
683 0,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
684 NULL,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
685 CREATE_ALWAYS,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
686 fileflags,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
687 NULL
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
688 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
689 }while( h==INVALID_HANDLE_VALUE && cnt++ < 2 && (Sleep(100), 1) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
690 #endif /* OS_WINCE */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
691 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
692 if( h==INVALID_HANDLE_VALUE ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
693 return SQLITE_CANTOPEN;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
694 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
695 f.h = h;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
696 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
697 f.zDeleteOnClose = delFlag ? utf8ToUnicode(zFilename) : 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
698 f.hMutex = NULL;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
699 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
700 TRACE3("OPEN EX %d \"%s\"\n", h, zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
701 return allocateWinFile(&f, pId);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
702 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
703
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
704 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
705 ** Attempt to open a new file for read-only access.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
706 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
707 ** On success, write the file handle into *id and return SQLITE_OK.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
708 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
709 ** On failure, return SQLITE_CANTOPEN.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
710 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
711 int sqlite3WinOpenReadOnly(const char *zFilename, OsFile **pId){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
712 winFile f;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
713 HANDLE h;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
714 WCHAR *zWide = utf8ToUnicode(zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
715 assert( *pId==0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
716 if( zWide ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
717 h = CreateFileW(zWide,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
718 GENERIC_READ,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
719 0,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
720 NULL,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
721 OPEN_EXISTING,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
722 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
723 NULL
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
724 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
725 sqliteFree(zWide);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
726 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
727 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
728 return SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
729 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
730 h = CreateFileA(zFilename,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
731 GENERIC_READ,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
732 0,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
733 NULL,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
734 OPEN_EXISTING,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
735 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
736 NULL
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
737 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
738 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
739 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
740 if( h==INVALID_HANDLE_VALUE ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
741 return SQLITE_CANTOPEN;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
742 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
743 f.h = h;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
744 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
745 f.zDeleteOnClose = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
746 f.hMutex = NULL;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
747 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
748 TRACE3("OPEN RO %d \"%s\"\n", h, zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
749 return allocateWinFile(&f, pId);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
750 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
751
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
752 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
753 ** Attempt to open a file descriptor for the directory that contains a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
754 ** file. This file descriptor can be used to fsync() the directory
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
755 ** in order to make sure the creation of a new file is actually written
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
756 ** to disk.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
757 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
758 ** This routine is only meaningful for Unix. It is a no-op under
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
759 ** windows since windows does not support hard links.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
760 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
761 ** On success, a handle for a previously open file is at *id is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
762 ** updated with the new directory file descriptor and SQLITE_OK is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
763 ** returned.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
764 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
765 ** On failure, the function returns SQLITE_CANTOPEN and leaves
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
766 ** *id unchanged.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
767 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
768 static int winOpenDirectory(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
769 OsFile *id,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
770 const char *zDirname
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
771 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
772 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
773 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
774
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
775 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
776 ** If the following global variable points to a string which is the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
777 ** name of a directory, then that directory will be used to store
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
778 ** temporary files.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
779 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
780 char *sqlite3_temp_directory = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
781
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
782 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
783 ** Create a temporary file name in zBuf. zBuf must be big enough to
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
784 ** hold at least SQLITE_TEMPNAME_SIZE characters.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
785 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
786 int sqlite3WinTempFileName(char *zBuf){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
787 static char zChars[] =
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
788 "abcdefghijklmnopqrstuvwxyz"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
789 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
790 "0123456789";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
791 int i, j;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
792 char zTempPath[SQLITE_TEMPNAME_SIZE];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
793 if( sqlite3_temp_directory ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
794 strncpy(zTempPath, sqlite3_temp_directory, SQLITE_TEMPNAME_SIZE-30);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
795 zTempPath[SQLITE_TEMPNAME_SIZE-30] = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
796 }else if( isNT() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
797 char *zMulti;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
798 WCHAR zWidePath[SQLITE_TEMPNAME_SIZE];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
799 GetTempPathW(SQLITE_TEMPNAME_SIZE-30, zWidePath);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
800 zMulti = unicodeToUtf8(zWidePath);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
801 if( zMulti ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
802 strncpy(zTempPath, zMulti, SQLITE_TEMPNAME_SIZE-30);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
803 zTempPath[SQLITE_TEMPNAME_SIZE-30] = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
804 sqliteFree(zMulti);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
805 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
806 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
807 GetTempPathA(SQLITE_TEMPNAME_SIZE-30, zTempPath);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
808 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
809 for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
810 zTempPath[i] = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
811 for(;;){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
812 sprintf(zBuf, "%s\\"TEMP_FILE_PREFIX, zTempPath);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
813 j = strlen(zBuf);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
814 sqlite3Randomness(15, &zBuf[j]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
815 for(i=0; i<15; i++, j++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
816 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
817 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
818 zBuf[j] = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
819 if( !sqlite3OsFileExists(zBuf) ) break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
820 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
821 TRACE2("TEMP FILENAME: %s\n", zBuf);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
822 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
823 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
824
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
825 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
826 ** Close a file.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
827 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
828 ** It is reported that an attempt to close a handle might sometimes
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
829 ** fail. This is a very unreasonable result, but windows is notorious
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
830 ** for being unreasonable so I do not doubt that it might happen. If
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
831 ** the close fails, we pause for 100 milliseconds and try again. As
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
832 ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
833 ** giving up and returning an error.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
834 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
835 #define MX_CLOSE_ATTEMPT 3
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
836 static int winClose(OsFile **pId){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
837 winFile *pFile;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
838 int rc = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
839 if( pId && (pFile = (winFile*)*pId)!=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
840 int rc, cnt = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
841 TRACE2("CLOSE %d\n", pFile->h);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
842 do{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
843 rc = CloseHandle(pFile->h);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
844 }while( rc==0 && cnt++ < MX_CLOSE_ATTEMPT && (Sleep(100), 1) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
845 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
846 winceDestroyLock(pFile);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
847 if( pFile->zDeleteOnClose ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
848 DeleteFileW(pFile->zDeleteOnClose);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
849 sqliteFree(pFile->zDeleteOnClose);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
850 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
851 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
852 OpenCounter(-1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
853 sqliteFree(pFile);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
854 *pId = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
855 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
856 return rc ? SQLITE_OK : SQLITE_IOERR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
857 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
858
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
859 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
860 ** Read data from a file into a buffer. Return SQLITE_OK if all
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
861 ** bytes were read successfully and SQLITE_IOERR if anything goes
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
862 ** wrong.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
863 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
864 static int winRead(OsFile *id, void *pBuf, int amt){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
865 DWORD got;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
866 assert( id!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
867 SimulateIOError(SQLITE_IOERR);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
868 TRACE3("READ %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
869 if( !ReadFile(((winFile*)id)->h, pBuf, amt, &got, 0) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
870 got = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
871 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
872 if( got==(DWORD)amt ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
873 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
874 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
875 return SQLITE_IOERR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
876 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
877 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
878
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
879 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
880 ** Write data from a buffer into a file. Return SQLITE_OK on success
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
881 ** or some other error code on failure.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
882 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
883 static int winWrite(OsFile *id, const void *pBuf, int amt){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
884 int rc = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
885 DWORD wrote;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
886 assert( id!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
887 SimulateIOError(SQLITE_IOERR);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
888 SimulateDiskfullError;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
889 TRACE3("WRITE %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
890 assert( amt>0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
891 while( amt>0 && (rc = WriteFile(((winFile*)id)->h, pBuf, amt, &wrote, 0))!=0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
892 && wrote>0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
893 amt -= wrote;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
894 pBuf = &((char*)pBuf)[wrote];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
895 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
896 if( !rc || amt>(int)wrote ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
897 return SQLITE_FULL;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
898 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
899 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
900 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
901
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
902 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
903 ** Some microsoft compilers lack this definition.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
904 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
905 #ifndef INVALID_SET_FILE_POINTER
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
906 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
907 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
908
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
909 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
910 ** Move the read/write pointer in a file.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
911 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
912 static int winSeek(OsFile *id, i64 offset){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
913 LONG upperBits = offset>>32;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
914 LONG lowerBits = offset & 0xffffffff;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
915 DWORD rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
916 assert( id!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
917 #ifdef SQLITE_TEST
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
918 if( offset ) SimulateDiskfullError
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
919 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
920 SEEK(offset/1024 + 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
921 rc = SetFilePointer(((winFile*)id)->h, lowerBits, &upperBits, FILE_BEGIN);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
922 TRACE3("SEEK %d %lld\n", ((winFile*)id)->h, offset);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
923 if( rc==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
924 return SQLITE_FULL;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
925 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
926 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
927 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
928
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
929 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
930 ** Make sure all writes to a particular file are committed to disk.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
931 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
932 static int winSync(OsFile *id, int dataOnly){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
933 assert( id!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
934 TRACE3("SYNC %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
935 if( FlushFileBuffers(((winFile*)id)->h) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
936 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
937 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
938 return SQLITE_IOERR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
939 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
940 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
941
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
942 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
943 ** Sync the directory zDirname. This is a no-op on operating systems other
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
944 ** than UNIX.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
945 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
946 int sqlite3WinSyncDirectory(const char *zDirname){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
947 SimulateIOError(SQLITE_IOERR);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
948 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
949 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
950
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
951 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
952 ** Truncate an open file to a specified size
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
953 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
954 static int winTruncate(OsFile *id, i64 nByte){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
955 LONG upperBits = nByte>>32;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
956 assert( id!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
957 TRACE3("TRUNCATE %d %lld\n", ((winFile*)id)->h, nByte);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
958 SimulateIOError(SQLITE_IOERR);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
959 SetFilePointer(((winFile*)id)->h, nByte, &upperBits, FILE_BEGIN);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
960 SetEndOfFile(((winFile*)id)->h);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
961 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
962 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
963
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
964 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
965 ** Determine the current size of a file in bytes
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
966 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
967 static int winFileSize(OsFile *id, i64 *pSize){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
968 DWORD upperBits, lowerBits;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
969 assert( id!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
970 SimulateIOError(SQLITE_IOERR);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
971 lowerBits = GetFileSize(((winFile*)id)->h, &upperBits);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
972 *pSize = (((i64)upperBits)<<32) + lowerBits;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
973 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
974 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
975
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
976 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
977 ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
978 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
979 #ifndef LOCKFILE_FAIL_IMMEDIATELY
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
980 # define LOCKFILE_FAIL_IMMEDIATELY 1
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
981 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
982
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
983 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
984 ** Acquire a reader lock.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
985 ** Different API routines are called depending on whether or not this
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
986 ** is Win95 or WinNT.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
987 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
988 static int getReadLock(winFile *id){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
989 int res;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
990 if( isNT() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
991 OVERLAPPED ovlp;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
992 ovlp.Offset = SHARED_FIRST;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
993 ovlp.OffsetHigh = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
994 ovlp.hEvent = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
995 res = LockFileEx(id->h, LOCKFILE_FAIL_IMMEDIATELY, 0, SHARED_SIZE,0,&ovlp);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
996 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
997 int lk;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
998 sqlite3Randomness(sizeof(lk), &lk);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
999 id->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1000 res = LockFile(id->h, SHARED_FIRST+id->sharedLockByte, 0, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1001 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1002 return res;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1003 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1004
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1005 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1006 ** Undo a readlock
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1007 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1008 static int unlockReadLock(winFile *pFile){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1009 int res;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1010 if( isNT() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1011 res = UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1012 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1013 res = UnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1014 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1015 return res;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1016 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1017
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1018 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1019 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1020 ** Check that a given pathname is a directory and is writable
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1021 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1022 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1023 int sqlite3WinIsDirWritable(char *zDirname){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1024 int fileAttr;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1025 WCHAR *zWide;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1026 if( zDirname==0 ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1027 if( !isNT() && strlen(zDirname)>MAX_PATH ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1028 zWide = utf8ToUnicode(zDirname);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1029 if( zWide ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1030 fileAttr = GetFileAttributesW(zWide);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1031 sqliteFree(zWide);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1032 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1033 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1034 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1035 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1036 fileAttr = GetFileAttributesA(zDirname);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1037 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1038 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1039 if( fileAttr == 0xffffffff ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1040 if( (fileAttr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1041 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1042 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1043 return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1044 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1045 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1046
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1047 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1048 ** Lock the file with the lock specified by parameter locktype - one
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1049 ** of the following:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1050 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1051 ** (1) SHARED_LOCK
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1052 ** (2) RESERVED_LOCK
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1053 ** (3) PENDING_LOCK
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1054 ** (4) EXCLUSIVE_LOCK
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1055 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1056 ** Sometimes when requesting one lock state, additional lock states
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1057 ** are inserted in between. The locking might fail on one of the later
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1058 ** transitions leaving the lock state different from what it started but
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1059 ** still short of its goal. The following chart shows the allowed
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1060 ** transitions and the inserted intermediate states:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1061 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1062 ** UNLOCKED -> SHARED
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1063 ** SHARED -> RESERVED
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1064 ** SHARED -> (PENDING) -> EXCLUSIVE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1065 ** RESERVED -> (PENDING) -> EXCLUSIVE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1066 ** PENDING -> EXCLUSIVE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1067 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1068 ** This routine will only increase a lock. The winUnlock() routine
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1069 ** erases all locks at once and returns us immediately to locking level 0.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1070 ** It is not possible to lower the locking level one step at a time. You
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1071 ** must go straight to locking level 0.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1072 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1073 static int winLock(OsFile *id, int locktype){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1074 int rc = SQLITE_OK; /* Return code from subroutines */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1075 int res = 1; /* Result of a windows lock call */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1076 int newLocktype; /* Set id->locktype to this value before exiting */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1077 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1078 winFile *pFile = (winFile*)id;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1079
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1080 assert( pFile!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1081 TRACE5("LOCK %d %d was %d(%d)\n",
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1082 pFile->h, locktype, pFile->locktype, pFile->sharedLockByte);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1083
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1084 /* If there is already a lock of this type or more restrictive on the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1085 ** OsFile, do nothing. Don't use the end_lock: exit path, as
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1086 ** sqlite3OsEnterMutex() hasn't been called yet.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1087 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1088 if( pFile->locktype>=locktype ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1089 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1090 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1091
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1092 /* Make sure the locking sequence is correct
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1093 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1094 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1095 assert( locktype!=PENDING_LOCK );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1096 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1097
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1098 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1099 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1100 ** the PENDING_LOCK byte is temporary.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1101 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1102 newLocktype = pFile->locktype;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1103 if( pFile->locktype==NO_LOCK
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1104 || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1105 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1106 int cnt = 3;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1107 while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1108 /* Try 3 times to get the pending lock. The pending lock might be
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1109 ** held by another reader process who will release it momentarily.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1110 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1111 TRACE2("could not get a PENDING lock. cnt=%d\n", cnt);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1112 Sleep(1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1113 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1114 gotPendingLock = res;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1115 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1116
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1117 /* Acquire a shared lock
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1118 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1119 if( locktype==SHARED_LOCK && res ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1120 assert( pFile->locktype==NO_LOCK );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1121 res = getReadLock(pFile);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1122 if( res ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1123 newLocktype = SHARED_LOCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1124 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1125 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1126
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1127 /* Acquire a RESERVED lock
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1128 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1129 if( locktype==RESERVED_LOCK && res ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1130 assert( pFile->locktype==SHARED_LOCK );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1131 res = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1132 if( res ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1133 newLocktype = RESERVED_LOCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1134 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1135 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1136
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1137 /* Acquire a PENDING lock
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1138 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1139 if( locktype==EXCLUSIVE_LOCK && res ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1140 newLocktype = PENDING_LOCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1141 gotPendingLock = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1142 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1143
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1144 /* Acquire an EXCLUSIVE lock
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1145 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1146 if( locktype==EXCLUSIVE_LOCK && res ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1147 assert( pFile->locktype>=SHARED_LOCK );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1148 res = unlockReadLock(pFile);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1149 TRACE2("unreadlock = %d\n", res);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1150 res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1151 if( res ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1152 newLocktype = EXCLUSIVE_LOCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1153 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1154 TRACE2("error-code = %d\n", GetLastError());
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1155 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1156 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1157
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1158 /* If we are holding a PENDING lock that ought to be released, then
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1159 ** release it now.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1160 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1161 if( gotPendingLock && locktype==SHARED_LOCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1162 UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1163 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1164
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1165 /* Update the state of the lock has held in the file descriptor then
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1166 ** return the appropriate result code.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1167 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1168 if( res ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1169 rc = SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1170 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1171 TRACE4("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1172 locktype, newLocktype);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1173 rc = SQLITE_BUSY;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1174 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1175 pFile->locktype = newLocktype;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1176 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1177 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1178
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1179 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1180 ** This routine checks if there is a RESERVED lock held on the specified
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1181 ** file by this or any other process. If such a lock is held, return
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1182 ** non-zero, otherwise zero.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1183 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1184 static int winCheckReservedLock(OsFile *id){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1185 int rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1186 winFile *pFile = (winFile*)id;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1187 assert( pFile!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1188 if( pFile->locktype>=RESERVED_LOCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1189 rc = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1190 TRACE3("TEST WR-LOCK %d %d (local)\n", pFile->h, rc);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1191 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1192 rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1193 if( rc ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1194 UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1195 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1196 rc = !rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1197 TRACE3("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1198 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1199 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1200 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1201
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1202 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1203 ** Lower the locking level on file descriptor id to locktype. locktype
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1204 ** must be either NO_LOCK or SHARED_LOCK.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1205 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1206 ** If the locking level of the file descriptor is already at or below
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1207 ** the requested locking level, this routine is a no-op.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1208 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1209 ** It is not possible for this routine to fail if the second argument
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1210 ** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1211 ** might return SQLITE_IOERR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1212 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1213 static int winUnlock(OsFile *id, int locktype){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1214 int type;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1215 int rc = SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1216 winFile *pFile = (winFile*)id;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1217 assert( pFile!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1218 assert( locktype<=SHARED_LOCK );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1219 TRACE5("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1220 pFile->locktype, pFile->sharedLockByte);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1221 type = pFile->locktype;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1222 if( type>=EXCLUSIVE_LOCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1223 UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1224 if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1225 /* This should never happen. We should always be able to
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1226 ** reacquire the read lock */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1227 rc = SQLITE_IOERR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1228 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1229 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1230 if( type>=RESERVED_LOCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1231 UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1232 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1233 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1234 unlockReadLock(pFile);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1235 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1236 if( type>=PENDING_LOCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1237 UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1238 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1239 pFile->locktype = locktype;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1240 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1241 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1242
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1243 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1244 ** Turn a relative pathname into a full pathname. Return a pointer
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1245 ** to the full pathname stored in space obtained from sqliteMalloc().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1246 ** The calling function is responsible for freeing this space once it
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1247 ** is no longer needed.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1248 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1249 char *sqlite3WinFullPathname(const char *zRelative){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1250 char *zFull;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1251 #if defined(__CYGWIN__)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1252 int nByte;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1253 nByte = strlen(zRelative) + MAX_PATH + 1001;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1254 zFull = sqliteMalloc( nByte );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1255 if( zFull==0 ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1256 if( cygwin_conv_to_full_win32_path(zRelative, zFull) ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1257 #elif OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1258 /* WinCE has no concept of a relative pathname, or so I am told. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1259 zFull = sqliteStrDup(zRelative);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1260 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1261 char *zNotUsed;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1262 WCHAR *zWide;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1263 int nByte;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1264 zWide = utf8ToUnicode(zRelative);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1265 if( zWide ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1266 WCHAR *zTemp, *zNotUsedW;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1267 nByte = GetFullPathNameW(zWide, 0, 0, &zNotUsedW) + 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1268 zTemp = sqliteMalloc( nByte*sizeof(zTemp[0]) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1269 if( zTemp==0 ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1270 GetFullPathNameW(zWide, nByte, zTemp, &zNotUsedW);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1271 sqliteFree(zWide);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1272 zFull = unicodeToUtf8(zTemp);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1273 sqliteFree(zTemp);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1274 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1275 nByte = GetFullPathNameA(zRelative, 0, 0, &zNotUsed) + 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1276 zFull = sqliteMalloc( nByte*sizeof(zFull[0]) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1277 if( zFull==0 ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1278 GetFullPathNameA(zRelative, nByte, zFull, &zNotUsed);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1279 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1280 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1281 return zFull;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1282 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1283
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1284 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1285 ** The fullSync option is meaningless on windows. This is a no-op.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1286 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1287 static void winSetFullSync(OsFile *id, int v){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1288 return;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1289 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1290
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1291 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1292 ** Return the underlying file handle for an OsFile
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1293 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1294 static int winFileHandle(OsFile *id){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1295 return (int)((winFile*)id)->h;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1296 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1297
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1298 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1299 ** Return an integer that indices the type of lock currently held
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1300 ** by this handle. (Used for testing and analysis only.)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1301 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1302 static int winLockState(OsFile *id){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1303 return ((winFile*)id)->locktype;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1304 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1305
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1306 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1307 ** This vector defines all the methods that can operate on an OsFile
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1308 ** for win32.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1309 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1310 static const IoMethod sqlite3WinIoMethod = {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1311 winClose,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1312 winOpenDirectory,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1313 winRead,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1314 winWrite,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1315 winSeek,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1316 winTruncate,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1317 winSync,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1318 winSetFullSync,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1319 winFileHandle,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1320 winFileSize,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1321 winLock,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1322 winUnlock,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1323 winLockState,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1324 winCheckReservedLock,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1325 };
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1326
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1327 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1328 ** Allocate memory for an OsFile. Initialize the new OsFile
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1329 ** to the value given in pInit and return a pointer to the new
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1330 ** OsFile. If we run out of memory, close the file and return NULL.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1331 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1332 static int allocateWinFile(winFile *pInit, OsFile **pId){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1333 winFile *pNew;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1334 pNew = sqliteMalloc( sizeof(*pNew) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1335 if( pNew==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1336 CloseHandle(pInit->h);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1337 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1338 sqliteFree(pInit->zDeleteOnClose);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1339 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1340 *pId = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1341 return SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1342 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1343 *pNew = *pInit;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1344 pNew->pMethod = &sqlite3WinIoMethod;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1345 pNew->locktype = NO_LOCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1346 pNew->sharedLockByte = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1347 *pId = (OsFile*)pNew;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1348 OpenCounter(+1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1349 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1350 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1351 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1352
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1353
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1354 #endif /* SQLITE_OMIT_DISKIO */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1355 /***************************************************************************
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1356 ** Everything above deals with file I/O. Everything that follows deals
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1357 ** with other miscellanous aspects of the operating system interface
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1358 ****************************************************************************/
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1359
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1360 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1361 ** Get information to seed the random number generator. The seed
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1362 ** is written into the buffer zBuf[256]. The calling function must
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1363 ** supply a sufficiently large buffer.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1364 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1365 int sqlite3WinRandomSeed(char *zBuf){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1366 /* We have to initialize zBuf to prevent valgrind from reporting
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1367 ** errors. The reports issued by valgrind are incorrect - we would
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1368 ** prefer that the randomness be increased by making use of the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1369 ** uninitialized space in zBuf - but valgrind errors tend to worry
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1370 ** some users. Rather than argue, it seems easier just to initialize
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1371 ** the whole array and silence valgrind, even if that means less randomness
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1372 ** in the random seed.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1373 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1374 ** When testing, initializing zBuf[] to zero is all we do. That means
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1375 ** that we always use the same random number sequence.* This makes the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1376 ** tests repeatable.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1377 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1378 memset(zBuf, 0, 256);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1379 GetSystemTime((LPSYSTEMTIME)zBuf);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1380 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1381 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1382
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1383 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1384 ** Sleep for a little while. Return the amount of time slept.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1385 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1386 int sqlite3WinSleep(int ms){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1387 Sleep(ms);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1388 return ms;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1389 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1390
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1391 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1392 ** Static variables used for thread synchronization
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1393 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1394 static int inMutex = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1395 #ifdef SQLITE_W32_THREADS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1396 static DWORD mutexOwner;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1397 static CRITICAL_SECTION cs;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1398 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1399
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1400 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1401 ** The following pair of routines implement mutual exclusion for
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1402 ** multi-threaded processes. Only a single thread is allowed to
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1403 ** executed code that is surrounded by EnterMutex() and LeaveMutex().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1404 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1405 ** SQLite uses only a single Mutex. There is not much critical
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1406 ** code and what little there is executes quickly and without blocking.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1407 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1408 ** Version 3.3.1 and earlier used a simple mutex. Beginning with
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1409 ** version 3.3.2, a recursive mutex is required.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1410 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1411 void sqlite3WinEnterMutex(){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1412 #ifdef SQLITE_W32_THREADS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1413 static int isInit = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1414 while( !isInit ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1415 static long lock = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1416 if( InterlockedIncrement(&lock)==1 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1417 InitializeCriticalSection(&cs);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1418 isInit = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1419 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1420 Sleep(1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1421 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1422 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1423 EnterCriticalSection(&cs);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1424 mutexOwner = GetCurrentThreadId();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1425 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1426 inMutex++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1427 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1428 void sqlite3WinLeaveMutex(){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1429 assert( inMutex );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1430 inMutex--;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1431 #ifdef SQLITE_W32_THREADS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1432 assert( mutexOwner==GetCurrentThreadId() );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1433 LeaveCriticalSection(&cs);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1434 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1435 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1436
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1437 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1438 ** Return TRUE if the mutex is currently held.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1439 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1440 ** If the thisThreadOnly parameter is true, return true if and only if the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1441 ** calling thread holds the mutex. If the parameter is false, return
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1442 ** true if any thread holds the mutex.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1443 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1444 int sqlite3WinInMutex(int thisThreadOnly){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1445 #ifdef SQLITE_W32_THREADS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1446 return inMutex>0 && (thisThreadOnly==0 || mutexOwner==GetCurrentThreadId());
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1447 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1448 return inMutex>0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1449 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1450 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1451
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1452
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1453 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1454 ** The following variable, if set to a non-zero value, becomes the result
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1455 ** returned from sqlite3OsCurrentTime(). This is used for testing.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1456 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1457 #ifdef SQLITE_TEST
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1458 int sqlite3_current_time = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1459 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1460
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1461 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1462 ** Find the current time (in Universal Coordinated Time). Write the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1463 ** current time and date as a Julian Day number into *prNow and
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1464 ** return 0. Return 1 if the time and date cannot be found.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1465 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1466 int sqlite3WinCurrentTime(double *prNow){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1467 FILETIME ft;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1468 /* FILETIME structure is a 64-bit value representing the number of
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1469 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1470 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1471 double now;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1472 #if OS_WINCE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1473 SYSTEMTIME time;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1474 GetSystemTime(&time);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1475 SystemTimeToFileTime(&time,&ft);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1476 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1477 GetSystemTimeAsFileTime( &ft );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1478 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1479 now = ((double)ft.dwHighDateTime) * 4294967296.0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1480 *prNow = (now + ft.dwLowDateTime)/864000000000.0 + 2305813.5;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1481 #ifdef SQLITE_TEST
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1482 if( sqlite3_current_time ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1483 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1484 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1485 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1486 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1487 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1488
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1489 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1490 ** Remember the number of thread-specific-data blocks allocated.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1491 ** Use this to verify that we are not leaking thread-specific-data.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1492 ** Ticket #1601
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1493 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1494 #ifdef SQLITE_TEST
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1495 int sqlite3_tsd_count = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1496 # define TSD_COUNTER_INCR InterlockedIncrement(&sqlite3_tsd_count)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1497 # define TSD_COUNTER_DECR InterlockedDecrement(&sqlite3_tsd_count)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1498 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1499 # define TSD_COUNTER_INCR /* no-op */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1500 # define TSD_COUNTER_DECR /* no-op */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1501 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1502
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1503
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1504
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1505 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1506 ** If called with allocateFlag>1, then return a pointer to thread
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1507 ** specific data for the current thread. Allocate and zero the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1508 ** thread-specific data if it does not already exist necessary.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1509 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1510 ** If called with allocateFlag==0, then check the current thread
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1511 ** specific data. Return it if it exists. If it does not exist,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1512 ** then return NULL.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1513 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1514 ** If called with allocateFlag<0, check to see if the thread specific
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1515 ** data is allocated and is all zero. If it is then deallocate it.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1516 ** Return a pointer to the thread specific data or NULL if it is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1517 ** unallocated or gets deallocated.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1518 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1519 ThreadData *sqlite3WinThreadSpecificData(int allocateFlag){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1520 static int key;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1521 static int keyInit = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1522 static const ThreadData zeroData = {0};
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1523 ThreadData *pTsd;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1524
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1525 if( !keyInit ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1526 sqlite3OsEnterMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1527 if( !keyInit ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1528 key = TlsAlloc();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1529 if( key==0xffffffff ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1530 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1531 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1532 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1533 keyInit = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1534 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1535 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1536 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1537 pTsd = TlsGetValue(key);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1538 if( allocateFlag>0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1539 if( !pTsd ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1540 pTsd = sqlite3OsMalloc( sizeof(zeroData) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1541 if( pTsd ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1542 *pTsd = zeroData;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1543 TlsSetValue(key, pTsd);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1544 TSD_COUNTER_INCR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1545 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1546 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1547 }else if( pTsd!=0 && allocateFlag<0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1548 && memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1549 sqlite3OsFree(pTsd);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1550 TlsSetValue(key, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1551 TSD_COUNTER_DECR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1552 pTsd = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1553 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1554 return pTsd;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1555 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1556 #endif /* OS_WIN */