drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2004 May 22 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
| 13 | ** This file contains code that is specific to windows. |
danielk1977 | 822a516 | 2008-05-16 04:51:54 +0000 | [diff] [blame] | 14 | ** |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame^] | 15 | ** $Id: os_win.c,v 1.141 2008/12/08 18:19:18 drh Exp $ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 16 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 17 | #include "sqliteInt.h" |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 18 | #if SQLITE_OS_WIN /* This file is used for windows only */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 19 | |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | ** A Note About Memory Allocation: |
| 23 | ** |
| 24 | ** This driver uses malloc()/free() directly rather than going through |
| 25 | ** the SQLite-wrappers sqlite3_malloc()/sqlite3_free(). Those wrappers |
| 26 | ** are designed for use on embedded systems where memory is scarce and |
| 27 | ** malloc failures happen frequently. Win32 does not typically run on |
| 28 | ** embedded systems, and when it does the developers normally have bigger |
| 29 | ** problems to worry about than running out of memory. So there is not |
| 30 | ** a compelling need to use the wrappers. |
| 31 | ** |
| 32 | ** But there is a good reason to not use the wrappers. If we use the |
| 33 | ** wrappers then we will get simulated malloc() failures within this |
| 34 | ** driver. And that causes all kinds of problems for our tests. We |
| 35 | ** could enhance SQLite to deal with simulated malloc failures within |
| 36 | ** the OS driver, but the code to deal with those failure would not |
| 37 | ** be exercised on Linux (which does not need to malloc() in the driver) |
| 38 | ** and so we would have difficulty writing coverage tests for that |
| 39 | ** code. Better to leave the code out, we think. |
| 40 | ** |
| 41 | ** The point of this discussion is as follows: When creating a new |
| 42 | ** OS layer for an embedded system, if you use this file as an example, |
| 43 | ** avoid the use of malloc()/free(). Those routines work ok on windows |
| 44 | ** desktops but not so well in embedded systems. |
| 45 | */ |
| 46 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 47 | #include <winbase.h> |
| 48 | |
drh | 09bf0e8 | 2005-03-21 00:36:08 +0000 | [diff] [blame] | 49 | #ifdef __CYGWIN__ |
| 50 | # include <sys/cygwin.h> |
| 51 | #endif |
| 52 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 53 | /* |
| 54 | ** Macros used to determine whether or not to use threads. |
| 55 | */ |
| 56 | #if defined(THREADSAFE) && THREADSAFE |
| 57 | # define SQLITE_W32_THREADS 1 |
| 58 | #endif |
| 59 | |
| 60 | /* |
| 61 | ** Include code that is common to all os_*.c files |
| 62 | */ |
| 63 | #include "os_common.h" |
| 64 | |
| 65 | /* |
shane | 171fa29 | 2008-09-01 22:15:18 +0000 | [diff] [blame] | 66 | ** Some microsoft compilers lack this definition. |
| 67 | */ |
| 68 | #ifndef INVALID_FILE_ATTRIBUTES |
| 69 | # define INVALID_FILE_ATTRIBUTES ((DWORD)-1) |
| 70 | #endif |
| 71 | |
| 72 | /* |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 73 | ** Determine if we are dealing with WindowsCE - which has a much |
| 74 | ** reduced API. |
| 75 | */ |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 76 | #if SQLITE_OS_WINCE |
drh | d2832bf | 2007-01-10 18:56:15 +0000 | [diff] [blame] | 77 | # define AreFileApisANSI() 1 |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 78 | #endif |
| 79 | |
| 80 | /* |
drh | 72aead8 | 2006-01-23 15:54:25 +0000 | [diff] [blame] | 81 | ** WinCE lacks native support for file locking so we have to fake it |
| 82 | ** with some code of our own. |
| 83 | */ |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 84 | #if SQLITE_OS_WINCE |
drh | 72aead8 | 2006-01-23 15:54:25 +0000 | [diff] [blame] | 85 | typedef struct winceLock { |
| 86 | int nReaders; /* Number of reader locks obtained */ |
| 87 | BOOL bPending; /* Indicates a pending lock has been obtained */ |
| 88 | BOOL bReserved; /* Indicates a reserved lock has been obtained */ |
| 89 | BOOL bExclusive; /* Indicates an exclusive lock has been obtained */ |
| 90 | } winceLock; |
| 91 | #endif |
| 92 | |
| 93 | /* |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 94 | ** The winFile structure is a subclass of sqlite3_file* specific to the win32 |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 95 | ** portability layer. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 96 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 97 | typedef struct winFile winFile; |
| 98 | struct winFile { |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 99 | const sqlite3_io_methods *pMethod;/* Must be first */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 100 | HANDLE h; /* Handle for accessing the file */ |
| 101 | unsigned char locktype; /* Type of lock currently held on this file */ |
| 102 | short sharedLockByte; /* Randomly chosen byte used as a shared lock */ |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 103 | #if SQLITE_OS_WINCE |
drh | 72aead8 | 2006-01-23 15:54:25 +0000 | [diff] [blame] | 104 | WCHAR *zDeleteOnClose; /* Name of file to delete when closing */ |
| 105 | HANDLE hMutex; /* Mutex used to control access to shared lock */ |
| 106 | HANDLE hShared; /* Shared memory segment used for locking */ |
| 107 | winceLock local; /* Locks obtained by this instance of winFile */ |
| 108 | winceLock *shared; /* Global shared lock memory for the file */ |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 109 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 113 | /* |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 114 | ** The following variable is (normally) set once and never changes |
| 115 | ** thereafter. It records whether the operating system is Win95 |
| 116 | ** or WinNT. |
| 117 | ** |
| 118 | ** 0: Operating system unknown. |
| 119 | ** 1: Operating system is Win95. |
| 120 | ** 2: Operating system is WinNT. |
| 121 | ** |
| 122 | ** In order to facilitate testing on a WinNT system, the test fixture |
| 123 | ** can manually set this value to 1 to emulate Win98 behavior. |
| 124 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 125 | #ifdef SQLITE_TEST |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 126 | int sqlite3_os_type = 0; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 127 | #else |
| 128 | static int sqlite3_os_type = 0; |
| 129 | #endif |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 130 | |
| 131 | /* |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 132 | ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP, |
| 133 | ** or WinCE. Return false (zero) for Win95, Win98, or WinME. |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 134 | ** |
| 135 | ** Here is an interesting observation: Win95, Win98, and WinME lack |
| 136 | ** the LockFileEx() API. But we can still statically link against that |
| 137 | ** API as long as we don't call it win running Win95/98/ME. A call to |
| 138 | ** this routine is used to determine if the host is Win95/98/ME or |
| 139 | ** WinNT/2K/XP so that we will know whether or not we can safely call |
| 140 | ** the LockFileEx() API. |
| 141 | */ |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 142 | #if SQLITE_OS_WINCE |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 143 | # define isNT() (1) |
| 144 | #else |
| 145 | static int isNT(void){ |
| 146 | if( sqlite3_os_type==0 ){ |
| 147 | OSVERSIONINFO sInfo; |
| 148 | sInfo.dwOSVersionInfoSize = sizeof(sInfo); |
| 149 | GetVersionEx(&sInfo); |
| 150 | sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1; |
| 151 | } |
| 152 | return sqlite3_os_type==2; |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 153 | } |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 154 | #endif /* SQLITE_OS_WINCE */ |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 155 | |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 156 | /* |
drh | 584c094 | 2006-12-21 03:20:40 +0000 | [diff] [blame] | 157 | ** Convert a UTF-8 string to microsoft unicode (UTF-16?). |
| 158 | ** |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 159 | ** Space to hold the returned string is obtained from malloc. |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 160 | */ |
| 161 | static WCHAR *utf8ToUnicode(const char *zFilename){ |
drh | e3dd8bb | 2006-02-27 23:44:35 +0000 | [diff] [blame] | 162 | int nChar; |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 163 | WCHAR *zWideFilename; |
| 164 | |
drh | e3dd8bb | 2006-02-27 23:44:35 +0000 | [diff] [blame] | 165 | nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0); |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 166 | zWideFilename = malloc( nChar*sizeof(zWideFilename[0]) ); |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 167 | if( zWideFilename==0 ){ |
| 168 | return 0; |
| 169 | } |
drh | e3dd8bb | 2006-02-27 23:44:35 +0000 | [diff] [blame] | 170 | nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar); |
| 171 | if( nChar==0 ){ |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 172 | free(zWideFilename); |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 173 | zWideFilename = 0; |
| 174 | } |
| 175 | return zWideFilename; |
| 176 | } |
| 177 | |
| 178 | /* |
drh | 584c094 | 2006-12-21 03:20:40 +0000 | [diff] [blame] | 179 | ** Convert microsoft unicode to UTF-8. Space to hold the returned string is |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 180 | ** obtained from malloc(). |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 181 | */ |
| 182 | static char *unicodeToUtf8(const WCHAR *zWideFilename){ |
| 183 | int nByte; |
| 184 | char *zFilename; |
| 185 | |
| 186 | nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0); |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 187 | zFilename = malloc( nByte ); |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 188 | if( zFilename==0 ){ |
| 189 | return 0; |
| 190 | } |
| 191 | nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte, |
| 192 | 0, 0); |
| 193 | if( nByte == 0 ){ |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 194 | free(zFilename); |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 195 | zFilename = 0; |
| 196 | } |
| 197 | return zFilename; |
| 198 | } |
| 199 | |
drh | 371de5a | 2006-10-30 13:37:22 +0000 | [diff] [blame] | 200 | /* |
drh | 584c094 | 2006-12-21 03:20:40 +0000 | [diff] [blame] | 201 | ** Convert an ansi string to microsoft unicode, based on the |
| 202 | ** current codepage settings for file apis. |
| 203 | ** |
| 204 | ** Space to hold the returned string is obtained |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 205 | ** from malloc. |
drh | 371de5a | 2006-10-30 13:37:22 +0000 | [diff] [blame] | 206 | */ |
| 207 | static WCHAR *mbcsToUnicode(const char *zFilename){ |
| 208 | int nByte; |
| 209 | WCHAR *zMbcsFilename; |
drh | 584c094 | 2006-12-21 03:20:40 +0000 | [diff] [blame] | 210 | int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP; |
drh | 371de5a | 2006-10-30 13:37:22 +0000 | [diff] [blame] | 211 | |
drh | 584c094 | 2006-12-21 03:20:40 +0000 | [diff] [blame] | 212 | nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR); |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 213 | zMbcsFilename = malloc( nByte*sizeof(zMbcsFilename[0]) ); |
drh | 371de5a | 2006-10-30 13:37:22 +0000 | [diff] [blame] | 214 | if( zMbcsFilename==0 ){ |
| 215 | return 0; |
| 216 | } |
drh | 584c094 | 2006-12-21 03:20:40 +0000 | [diff] [blame] | 217 | nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte); |
drh | 371de5a | 2006-10-30 13:37:22 +0000 | [diff] [blame] | 218 | if( nByte==0 ){ |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 219 | free(zMbcsFilename); |
drh | 371de5a | 2006-10-30 13:37:22 +0000 | [diff] [blame] | 220 | zMbcsFilename = 0; |
| 221 | } |
| 222 | return zMbcsFilename; |
| 223 | } |
| 224 | |
| 225 | /* |
drh | 584c094 | 2006-12-21 03:20:40 +0000 | [diff] [blame] | 226 | ** Convert microsoft unicode to multibyte character string, based on the |
| 227 | ** user's Ansi codepage. |
| 228 | ** |
| 229 | ** Space to hold the returned string is obtained from |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 230 | ** malloc(). |
drh | 371de5a | 2006-10-30 13:37:22 +0000 | [diff] [blame] | 231 | */ |
| 232 | static char *unicodeToMbcs(const WCHAR *zWideFilename){ |
| 233 | int nByte; |
| 234 | char *zFilename; |
drh | 584c094 | 2006-12-21 03:20:40 +0000 | [diff] [blame] | 235 | int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP; |
drh | 371de5a | 2006-10-30 13:37:22 +0000 | [diff] [blame] | 236 | |
drh | 584c094 | 2006-12-21 03:20:40 +0000 | [diff] [blame] | 237 | nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0); |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 238 | zFilename = malloc( nByte ); |
drh | 371de5a | 2006-10-30 13:37:22 +0000 | [diff] [blame] | 239 | if( zFilename==0 ){ |
| 240 | return 0; |
| 241 | } |
drh | 584c094 | 2006-12-21 03:20:40 +0000 | [diff] [blame] | 242 | nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, nByte, |
drh | 371de5a | 2006-10-30 13:37:22 +0000 | [diff] [blame] | 243 | 0, 0); |
| 244 | if( nByte == 0 ){ |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 245 | free(zFilename); |
drh | 371de5a | 2006-10-30 13:37:22 +0000 | [diff] [blame] | 246 | zFilename = 0; |
| 247 | } |
| 248 | return zFilename; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | ** Convert multibyte character string to UTF-8. Space to hold the |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 253 | ** returned string is obtained from malloc(). |
drh | 371de5a | 2006-10-30 13:37:22 +0000 | [diff] [blame] | 254 | */ |
drh | 1d29885 | 2008-11-18 19:18:52 +0000 | [diff] [blame] | 255 | char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){ |
drh | 371de5a | 2006-10-30 13:37:22 +0000 | [diff] [blame] | 256 | char *zFilenameUtf8; |
| 257 | WCHAR *zTmpWide; |
| 258 | |
| 259 | zTmpWide = mbcsToUnicode(zFilename); |
| 260 | if( zTmpWide==0 ){ |
| 261 | return 0; |
| 262 | } |
| 263 | zFilenameUtf8 = unicodeToUtf8(zTmpWide); |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 264 | free(zTmpWide); |
drh | 371de5a | 2006-10-30 13:37:22 +0000 | [diff] [blame] | 265 | return zFilenameUtf8; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | ** Convert UTF-8 to multibyte character string. Space to hold the |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 270 | ** returned string is obtained from malloc(). |
drh | 371de5a | 2006-10-30 13:37:22 +0000 | [diff] [blame] | 271 | */ |
| 272 | static char *utf8ToMbcs(const char *zFilename){ |
| 273 | char *zFilenameMbcs; |
| 274 | WCHAR *zTmpWide; |
| 275 | |
| 276 | zTmpWide = utf8ToUnicode(zFilename); |
| 277 | if( zTmpWide==0 ){ |
| 278 | return 0; |
| 279 | } |
| 280 | zFilenameMbcs = unicodeToMbcs(zTmpWide); |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 281 | free(zTmpWide); |
drh | 371de5a | 2006-10-30 13:37:22 +0000 | [diff] [blame] | 282 | return zFilenameMbcs; |
| 283 | } |
| 284 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 285 | #if SQLITE_OS_WINCE |
drh | 72aead8 | 2006-01-23 15:54:25 +0000 | [diff] [blame] | 286 | /************************************************************************* |
| 287 | ** This section contains code for WinCE only. |
| 288 | */ |
| 289 | /* |
| 290 | ** WindowsCE does not have a localtime() function. So create a |
| 291 | ** substitute. |
| 292 | */ |
| 293 | #include <time.h> |
| 294 | struct tm *__cdecl localtime(const time_t *t) |
| 295 | { |
| 296 | static struct tm y; |
| 297 | FILETIME uTm, lTm; |
| 298 | SYSTEMTIME pTm; |
drh | c51250a | 2007-09-20 14:39:23 +0000 | [diff] [blame] | 299 | sqlite3_int64 t64; |
drh | 72aead8 | 2006-01-23 15:54:25 +0000 | [diff] [blame] | 300 | t64 = *t; |
| 301 | t64 = (t64 + 11644473600)*10000000; |
| 302 | uTm.dwLowDateTime = t64 & 0xFFFFFFFF; |
| 303 | uTm.dwHighDateTime= t64 >> 32; |
| 304 | FileTimeToLocalFileTime(&uTm,&lTm); |
| 305 | FileTimeToSystemTime(&lTm,&pTm); |
| 306 | y.tm_year = pTm.wYear - 1900; |
| 307 | y.tm_mon = pTm.wMonth - 1; |
| 308 | y.tm_wday = pTm.wDayOfWeek; |
| 309 | y.tm_mday = pTm.wDay; |
| 310 | y.tm_hour = pTm.wHour; |
| 311 | y.tm_min = pTm.wMinute; |
| 312 | y.tm_sec = pTm.wSecond; |
| 313 | return &y; |
| 314 | } |
| 315 | |
| 316 | /* This will never be called, but defined to make the code compile */ |
| 317 | #define GetTempPathA(a,b) |
| 318 | |
| 319 | #define LockFile(a,b,c,d,e) winceLockFile(&a, b, c, d, e) |
| 320 | #define UnlockFile(a,b,c,d,e) winceUnlockFile(&a, b, c, d, e) |
| 321 | #define LockFileEx(a,b,c,d,e,f) winceLockFileEx(&a, b, c, d, e, f) |
| 322 | |
| 323 | #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-offsetof(winFile,h)] |
| 324 | |
| 325 | /* |
| 326 | ** Acquire a lock on the handle h |
| 327 | */ |
| 328 | static void winceMutexAcquire(HANDLE h){ |
| 329 | DWORD dwErr; |
| 330 | do { |
| 331 | dwErr = WaitForSingleObject(h, INFINITE); |
| 332 | } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED); |
| 333 | } |
| 334 | /* |
| 335 | ** Release a lock acquired by winceMutexAcquire() |
| 336 | */ |
| 337 | #define winceMutexRelease(h) ReleaseMutex(h) |
| 338 | |
| 339 | /* |
| 340 | ** Create the mutex and shared memory used for locking in the file |
| 341 | ** descriptor pFile |
| 342 | */ |
| 343 | static BOOL winceCreateLock(const char *zFilename, winFile *pFile){ |
| 344 | WCHAR *zTok; |
| 345 | WCHAR *zName = utf8ToUnicode(zFilename); |
| 346 | BOOL bInit = TRUE; |
| 347 | |
| 348 | /* Initialize the local lockdata */ |
| 349 | ZeroMemory(&pFile->local, sizeof(pFile->local)); |
| 350 | |
| 351 | /* Replace the backslashes from the filename and lowercase it |
| 352 | ** to derive a mutex name. */ |
| 353 | zTok = CharLowerW(zName); |
| 354 | for (;*zTok;zTok++){ |
| 355 | if (*zTok == '\\') *zTok = '_'; |
| 356 | } |
| 357 | |
| 358 | /* Create/open the named mutex */ |
| 359 | pFile->hMutex = CreateMutexW(NULL, FALSE, zName); |
| 360 | if (!pFile->hMutex){ |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 361 | free(zName); |
drh | 72aead8 | 2006-01-23 15:54:25 +0000 | [diff] [blame] | 362 | return FALSE; |
| 363 | } |
| 364 | |
| 365 | /* Acquire the mutex before continuing */ |
| 366 | winceMutexAcquire(pFile->hMutex); |
| 367 | |
| 368 | /* Since the names of named mutexes, semaphores, file mappings etc are |
| 369 | ** case-sensitive, take advantage of that by uppercasing the mutex name |
| 370 | ** and using that as the shared filemapping name. |
| 371 | */ |
| 372 | CharUpperW(zName); |
| 373 | pFile->hShared = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, |
| 374 | PAGE_READWRITE, 0, sizeof(winceLock), |
| 375 | zName); |
| 376 | |
| 377 | /* Set a flag that indicates we're the first to create the memory so it |
| 378 | ** must be zero-initialized */ |
| 379 | if (GetLastError() == ERROR_ALREADY_EXISTS){ |
| 380 | bInit = FALSE; |
| 381 | } |
| 382 | |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 383 | free(zName); |
drh | 72aead8 | 2006-01-23 15:54:25 +0000 | [diff] [blame] | 384 | |
| 385 | /* If we succeeded in making the shared memory handle, map it. */ |
| 386 | if (pFile->hShared){ |
| 387 | pFile->shared = (winceLock*)MapViewOfFile(pFile->hShared, |
| 388 | FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock)); |
| 389 | /* If mapping failed, close the shared memory handle and erase it */ |
| 390 | if (!pFile->shared){ |
| 391 | CloseHandle(pFile->hShared); |
| 392 | pFile->hShared = NULL; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /* If shared memory could not be created, then close the mutex and fail */ |
| 397 | if (pFile->hShared == NULL){ |
| 398 | winceMutexRelease(pFile->hMutex); |
| 399 | CloseHandle(pFile->hMutex); |
| 400 | pFile->hMutex = NULL; |
| 401 | return FALSE; |
| 402 | } |
| 403 | |
| 404 | /* Initialize the shared memory if we're supposed to */ |
| 405 | if (bInit) { |
| 406 | ZeroMemory(pFile->shared, sizeof(winceLock)); |
| 407 | } |
| 408 | |
| 409 | winceMutexRelease(pFile->hMutex); |
| 410 | return TRUE; |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | ** Destroy the part of winFile that deals with wince locks |
| 415 | */ |
| 416 | static void winceDestroyLock(winFile *pFile){ |
| 417 | if (pFile->hMutex){ |
| 418 | /* Acquire the mutex */ |
| 419 | winceMutexAcquire(pFile->hMutex); |
| 420 | |
| 421 | /* The following blocks should probably assert in debug mode, but they |
| 422 | are to cleanup in case any locks remained open */ |
| 423 | if (pFile->local.nReaders){ |
| 424 | pFile->shared->nReaders --; |
| 425 | } |
| 426 | if (pFile->local.bReserved){ |
| 427 | pFile->shared->bReserved = FALSE; |
| 428 | } |
| 429 | if (pFile->local.bPending){ |
| 430 | pFile->shared->bPending = FALSE; |
| 431 | } |
| 432 | if (pFile->local.bExclusive){ |
| 433 | pFile->shared->bExclusive = FALSE; |
| 434 | } |
| 435 | |
| 436 | /* De-reference and close our copy of the shared memory handle */ |
| 437 | UnmapViewOfFile(pFile->shared); |
| 438 | CloseHandle(pFile->hShared); |
| 439 | |
| 440 | /* Done with the mutex */ |
| 441 | winceMutexRelease(pFile->hMutex); |
| 442 | CloseHandle(pFile->hMutex); |
| 443 | pFile->hMutex = NULL; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | ** An implementation of the LockFile() API of windows for wince |
| 449 | */ |
| 450 | static BOOL winceLockFile( |
| 451 | HANDLE *phFile, |
| 452 | DWORD dwFileOffsetLow, |
| 453 | DWORD dwFileOffsetHigh, |
| 454 | DWORD nNumberOfBytesToLockLow, |
| 455 | DWORD nNumberOfBytesToLockHigh |
| 456 | ){ |
| 457 | winFile *pFile = HANDLE_TO_WINFILE(phFile); |
| 458 | BOOL bReturn = FALSE; |
| 459 | |
| 460 | if (!pFile->hMutex) return TRUE; |
| 461 | winceMutexAcquire(pFile->hMutex); |
| 462 | |
| 463 | /* Wanting an exclusive lock? */ |
| 464 | if (dwFileOffsetLow == SHARED_FIRST |
| 465 | && nNumberOfBytesToLockLow == SHARED_SIZE){ |
| 466 | if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){ |
| 467 | pFile->shared->bExclusive = TRUE; |
| 468 | pFile->local.bExclusive = TRUE; |
| 469 | bReturn = TRUE; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | /* Want a read-only lock? */ |
| 474 | else if ((dwFileOffsetLow >= SHARED_FIRST && |
| 475 | dwFileOffsetLow < SHARED_FIRST + SHARED_SIZE) && |
| 476 | nNumberOfBytesToLockLow == 1){ |
| 477 | if (pFile->shared->bExclusive == 0){ |
| 478 | pFile->local.nReaders ++; |
| 479 | if (pFile->local.nReaders == 1){ |
| 480 | pFile->shared->nReaders ++; |
| 481 | } |
| 482 | bReturn = TRUE; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /* Want a pending lock? */ |
| 487 | else if (dwFileOffsetLow == PENDING_BYTE && nNumberOfBytesToLockLow == 1){ |
| 488 | /* If no pending lock has been acquired, then acquire it */ |
| 489 | if (pFile->shared->bPending == 0) { |
| 490 | pFile->shared->bPending = TRUE; |
| 491 | pFile->local.bPending = TRUE; |
| 492 | bReturn = TRUE; |
| 493 | } |
| 494 | } |
| 495 | /* Want a reserved lock? */ |
| 496 | else if (dwFileOffsetLow == RESERVED_BYTE && nNumberOfBytesToLockLow == 1){ |
| 497 | if (pFile->shared->bReserved == 0) { |
| 498 | pFile->shared->bReserved = TRUE; |
| 499 | pFile->local.bReserved = TRUE; |
| 500 | bReturn = TRUE; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | winceMutexRelease(pFile->hMutex); |
| 505 | return bReturn; |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | ** An implementation of the UnlockFile API of windows for wince |
| 510 | */ |
| 511 | static BOOL winceUnlockFile( |
| 512 | HANDLE *phFile, |
| 513 | DWORD dwFileOffsetLow, |
| 514 | DWORD dwFileOffsetHigh, |
| 515 | DWORD nNumberOfBytesToUnlockLow, |
| 516 | DWORD nNumberOfBytesToUnlockHigh |
| 517 | ){ |
| 518 | winFile *pFile = HANDLE_TO_WINFILE(phFile); |
| 519 | BOOL bReturn = FALSE; |
| 520 | |
| 521 | if (!pFile->hMutex) return TRUE; |
| 522 | winceMutexAcquire(pFile->hMutex); |
| 523 | |
| 524 | /* Releasing a reader lock or an exclusive lock */ |
| 525 | if (dwFileOffsetLow >= SHARED_FIRST && |
| 526 | dwFileOffsetLow < SHARED_FIRST + SHARED_SIZE){ |
| 527 | /* Did we have an exclusive lock? */ |
| 528 | if (pFile->local.bExclusive){ |
| 529 | pFile->local.bExclusive = FALSE; |
| 530 | pFile->shared->bExclusive = FALSE; |
| 531 | bReturn = TRUE; |
| 532 | } |
| 533 | |
| 534 | /* Did we just have a reader lock? */ |
| 535 | else if (pFile->local.nReaders){ |
| 536 | pFile->local.nReaders --; |
| 537 | if (pFile->local.nReaders == 0) |
| 538 | { |
| 539 | pFile->shared->nReaders --; |
| 540 | } |
| 541 | bReturn = TRUE; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /* Releasing a pending lock */ |
| 546 | else if (dwFileOffsetLow == PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){ |
| 547 | if (pFile->local.bPending){ |
| 548 | pFile->local.bPending = FALSE; |
| 549 | pFile->shared->bPending = FALSE; |
| 550 | bReturn = TRUE; |
| 551 | } |
| 552 | } |
| 553 | /* Releasing a reserved lock */ |
| 554 | else if (dwFileOffsetLow == RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){ |
| 555 | if (pFile->local.bReserved) { |
| 556 | pFile->local.bReserved = FALSE; |
| 557 | pFile->shared->bReserved = FALSE; |
| 558 | bReturn = TRUE; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | winceMutexRelease(pFile->hMutex); |
| 563 | return bReturn; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | ** An implementation of the LockFileEx() API of windows for wince |
| 568 | */ |
| 569 | static BOOL winceLockFileEx( |
| 570 | HANDLE *phFile, |
| 571 | DWORD dwFlags, |
| 572 | DWORD dwReserved, |
| 573 | DWORD nNumberOfBytesToLockLow, |
| 574 | DWORD nNumberOfBytesToLockHigh, |
| 575 | LPOVERLAPPED lpOverlapped |
| 576 | ){ |
| 577 | /* If the caller wants a shared read lock, forward this call |
| 578 | ** to winceLockFile */ |
| 579 | if (lpOverlapped->Offset == SHARED_FIRST && |
| 580 | dwFlags == 1 && |
| 581 | nNumberOfBytesToLockLow == SHARED_SIZE){ |
| 582 | return winceLockFile(phFile, SHARED_FIRST, 0, 1, 0); |
| 583 | } |
| 584 | return FALSE; |
| 585 | } |
| 586 | /* |
| 587 | ** End of the special code for wince |
| 588 | *****************************************************************************/ |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 589 | #endif /* SQLITE_OS_WINCE */ |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 590 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 591 | /***************************************************************************** |
| 592 | ** The next group of routines implement the I/O methods specified |
| 593 | ** by the sqlite3_io_methods object. |
| 594 | ******************************************************************************/ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 595 | |
| 596 | /* |
| 597 | ** Close a file. |
drh | 59e63a6 | 2006-06-04 23:31:48 +0000 | [diff] [blame] | 598 | ** |
| 599 | ** It is reported that an attempt to close a handle might sometimes |
| 600 | ** fail. This is a very unreasonable result, but windows is notorious |
| 601 | ** for being unreasonable so I do not doubt that it might happen. If |
| 602 | ** the close fails, we pause for 100 milliseconds and try again. As |
| 603 | ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before |
| 604 | ** giving up and returning an error. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 605 | */ |
drh | 59e63a6 | 2006-06-04 23:31:48 +0000 | [diff] [blame] | 606 | #define MX_CLOSE_ATTEMPT 3 |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 607 | static int winClose(sqlite3_file *id){ |
| 608 | int rc, cnt = 0; |
| 609 | winFile *pFile = (winFile*)id; |
| 610 | OSTRACE2("CLOSE %d\n", pFile->h); |
| 611 | do{ |
| 612 | rc = CloseHandle(pFile->h); |
shane | d94b055 | 2008-09-30 04:20:07 +0000 | [diff] [blame] | 613 | }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (Sleep(100), 1) ); |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 614 | #if SQLITE_OS_WINCE |
drh | d641d51 | 2008-02-20 00:00:00 +0000 | [diff] [blame] | 615 | #define WINCE_DELETION_ATTEMPTS 3 |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 616 | winceDestroyLock(pFile); |
drh | 7229ed4 | 2007-10-08 12:29:17 +0000 | [diff] [blame] | 617 | if( pFile->zDeleteOnClose ){ |
drh | d641d51 | 2008-02-20 00:00:00 +0000 | [diff] [blame] | 618 | int cnt = 0; |
| 619 | while( |
| 620 | DeleteFileW(pFile->zDeleteOnClose)==0 |
| 621 | && GetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff |
| 622 | && cnt++ < WINCE_DELETION_ATTEMPTS |
| 623 | ){ |
| 624 | Sleep(100); /* Wait a little before trying again */ |
| 625 | } |
drh | 7229ed4 | 2007-10-08 12:29:17 +0000 | [diff] [blame] | 626 | free(pFile->zDeleteOnClose); |
| 627 | } |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 628 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 629 | OpenCounter(-1); |
drh | eb4fa52 | 2006-06-04 23:02:20 +0000 | [diff] [blame] | 630 | return rc ? SQLITE_OK : SQLITE_IOERR; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /* |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 634 | ** Some microsoft compilers lack this definition. |
| 635 | */ |
| 636 | #ifndef INVALID_SET_FILE_POINTER |
| 637 | # define INVALID_SET_FILE_POINTER ((DWORD)-1) |
| 638 | #endif |
| 639 | |
| 640 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 641 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 642 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 643 | ** wrong. |
| 644 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 645 | static int winRead( |
| 646 | sqlite3_file *id, /* File to read from */ |
| 647 | void *pBuf, /* Write content into this buffer */ |
| 648 | int amt, /* Number of bytes to read */ |
| 649 | sqlite3_int64 offset /* Begin reading at this offset */ |
| 650 | ){ |
| 651 | LONG upperBits = (offset>>32) & 0x7fffffff; |
| 652 | LONG lowerBits = offset & 0xffffffff; |
| 653 | DWORD rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 654 | DWORD got; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 655 | winFile *pFile = (winFile*)id; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 656 | assert( id!=0 ); |
drh | 9cce710 | 2007-01-09 17:18:19 +0000 | [diff] [blame] | 657 | SimulateIOError(return SQLITE_IOERR_READ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 658 | OSTRACE3("READ %d lock=%d\n", pFile->h, pFile->locktype); |
| 659 | rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN); |
| 660 | if( rc==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ){ |
| 661 | return SQLITE_FULL; |
| 662 | } |
| 663 | if( !ReadFile(pFile->h, pBuf, amt, &got, 0) ){ |
drh | aedd892 | 2007-01-05 14:38:54 +0000 | [diff] [blame] | 664 | return SQLITE_IOERR_READ; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 665 | } |
| 666 | if( got==(DWORD)amt ){ |
| 667 | return SQLITE_OK; |
| 668 | }else{ |
drh | 4c17c3f | 2008-11-07 00:06:18 +0000 | [diff] [blame] | 669 | /* Unread parts of the buffer must be zero-filled */ |
drh | bafda09 | 2007-01-03 23:36:22 +0000 | [diff] [blame] | 670 | memset(&((char*)pBuf)[got], 0, amt-got); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 671 | return SQLITE_IOERR_SHORT_READ; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 677 | ** or some other error code on failure. |
| 678 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 679 | static int winWrite( |
| 680 | sqlite3_file *id, /* File to write into */ |
| 681 | const void *pBuf, /* The bytes to be written */ |
| 682 | int amt, /* Number of bytes to write */ |
| 683 | sqlite3_int64 offset /* Offset into the file to begin writing at */ |
| 684 | ){ |
| 685 | LONG upperBits = (offset>>32) & 0x7fffffff; |
| 686 | LONG lowerBits = offset & 0xffffffff; |
| 687 | DWORD rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 688 | DWORD wrote; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 689 | winFile *pFile = (winFile*)id; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 690 | assert( id!=0 ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 691 | SimulateIOError(return SQLITE_IOERR_WRITE); |
drh | 5968593 | 2006-09-14 13:47:11 +0000 | [diff] [blame] | 692 | SimulateDiskfullError(return SQLITE_FULL); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 693 | OSTRACE3("WRITE %d lock=%d\n", pFile->h, pFile->locktype); |
| 694 | rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN); |
| 695 | if( rc==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ){ |
| 696 | return SQLITE_FULL; |
| 697 | } |
drh | 4c7f941 | 2005-02-03 00:29:47 +0000 | [diff] [blame] | 698 | assert( amt>0 ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 699 | while( |
| 700 | amt>0 |
| 701 | && (rc = WriteFile(pFile->h, pBuf, amt, &wrote, 0))!=0 |
| 702 | && wrote>0 |
| 703 | ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 704 | amt -= wrote; |
| 705 | pBuf = &((char*)pBuf)[wrote]; |
| 706 | } |
| 707 | if( !rc || amt>(int)wrote ){ |
| 708 | return SQLITE_FULL; |
| 709 | } |
| 710 | return SQLITE_OK; |
| 711 | } |
| 712 | |
| 713 | /* |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 714 | ** Truncate an open file to a specified size |
drh | bbdc2b9 | 2005-09-19 12:53:18 +0000 | [diff] [blame] | 715 | */ |
drh | c51250a | 2007-09-20 14:39:23 +0000 | [diff] [blame] | 716 | static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){ |
shane | a3465f2 | 2008-10-12 02:27:38 +0000 | [diff] [blame] | 717 | DWORD rc; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 718 | LONG upperBits = (nByte>>32) & 0x7fffffff; |
| 719 | LONG lowerBits = nByte & 0xffffffff; |
| 720 | winFile *pFile = (winFile*)id; |
| 721 | OSTRACE3("TRUNCATE %d %lld\n", pFile->h, nByte); |
| 722 | SimulateIOError(return SQLITE_IOERR_TRUNCATE); |
shane | a3465f2 | 2008-10-12 02:27:38 +0000 | [diff] [blame] | 723 | rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN); |
| 724 | if( INVALID_SET_FILE_POINTER != rc ){ |
| 725 | /* SetEndOfFile will fail if nByte is negative */ |
| 726 | if( SetEndOfFile(pFile->h) ){ |
| 727 | return SQLITE_OK; |
| 728 | } |
| 729 | } |
| 730 | return SQLITE_IOERR_TRUNCATE; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 731 | } |
| 732 | |
drh | dec6fae | 2007-09-03 17:02:50 +0000 | [diff] [blame] | 733 | #ifdef SQLITE_TEST |
| 734 | /* |
| 735 | ** Count the number of fullsyncs and normal syncs. This is used to test |
| 736 | ** that syncs and fullsyncs are occuring at the right times. |
| 737 | */ |
| 738 | int sqlite3_sync_count = 0; |
| 739 | int sqlite3_fullsync_count = 0; |
| 740 | #endif |
| 741 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 742 | /* |
| 743 | ** Make sure all writes to a particular file are committed to disk. |
| 744 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 745 | static int winSync(sqlite3_file *id, int flags){ |
| 746 | winFile *pFile = (winFile*)id; |
| 747 | OSTRACE3("SYNC %d lock=%d\n", pFile->h, pFile->locktype); |
drh | dec6fae | 2007-09-03 17:02:50 +0000 | [diff] [blame] | 748 | #ifdef SQLITE_TEST |
| 749 | if( flags & SQLITE_SYNC_FULL ){ |
| 750 | sqlite3_fullsync_count++; |
| 751 | } |
| 752 | sqlite3_sync_count++; |
| 753 | #endif |
shane | 84ca383 | 2008-11-13 18:20:43 +0000 | [diff] [blame] | 754 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 755 | ** no-op |
| 756 | */ |
| 757 | #ifdef SQLITE_NO_SYNC |
| 758 | return SQLITE_OK; |
| 759 | #else |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 760 | if( FlushFileBuffers(pFile->h) ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 761 | return SQLITE_OK; |
| 762 | }else{ |
| 763 | return SQLITE_IOERR; |
| 764 | } |
shane | 84ca383 | 2008-11-13 18:20:43 +0000 | [diff] [blame] | 765 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 769 | ** Determine the current size of a file in bytes |
| 770 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 771 | static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){ |
| 772 | winFile *pFile = (winFile*)id; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 773 | DWORD upperBits, lowerBits; |
drh | 9cce710 | 2007-01-09 17:18:19 +0000 | [diff] [blame] | 774 | SimulateIOError(return SQLITE_IOERR_FSTAT); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 775 | lowerBits = GetFileSize(pFile->h, &upperBits); |
| 776 | *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 777 | return SQLITE_OK; |
| 778 | } |
| 779 | |
| 780 | /* |
drh | 602bbd3 | 2006-01-06 20:22:29 +0000 | [diff] [blame] | 781 | ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems. |
| 782 | */ |
| 783 | #ifndef LOCKFILE_FAIL_IMMEDIATELY |
| 784 | # define LOCKFILE_FAIL_IMMEDIATELY 1 |
| 785 | #endif |
| 786 | |
| 787 | /* |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 788 | ** Acquire a reader lock. |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 789 | ** Different API routines are called depending on whether or not this |
| 790 | ** is Win95 or WinNT. |
| 791 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 792 | static int getReadLock(winFile *pFile){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 793 | int res; |
| 794 | if( isNT() ){ |
| 795 | OVERLAPPED ovlp; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 796 | ovlp.Offset = SHARED_FIRST; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 797 | ovlp.OffsetHigh = 0; |
| 798 | ovlp.hEvent = 0; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 799 | res = LockFileEx(pFile->h, LOCKFILE_FAIL_IMMEDIATELY, |
| 800 | 0, SHARED_SIZE, 0, &ovlp); |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 801 | /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. |
| 802 | */ |
| 803 | #if SQLITE_OS_WINCE==0 |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 804 | }else{ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 805 | int lk; |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 806 | sqlite3_randomness(sizeof(lk), &lk); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 807 | pFile->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1); |
| 808 | res = LockFile(pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0); |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 809 | #endif |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 810 | } |
| 811 | return res; |
| 812 | } |
| 813 | |
| 814 | /* |
| 815 | ** Undo a readlock |
| 816 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 817 | static int unlockReadLock(winFile *pFile){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 818 | int res; |
| 819 | if( isNT() ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 820 | res = UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 821 | /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. |
| 822 | */ |
| 823 | #if SQLITE_OS_WINCE==0 |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 824 | }else{ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 825 | res = UnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0); |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 826 | #endif |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 827 | } |
| 828 | return res; |
| 829 | } |
| 830 | |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 831 | /* |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 832 | ** Lock the file with the lock specified by parameter locktype - one |
| 833 | ** of the following: |
| 834 | ** |
| 835 | ** (1) SHARED_LOCK |
| 836 | ** (2) RESERVED_LOCK |
| 837 | ** (3) PENDING_LOCK |
| 838 | ** (4) EXCLUSIVE_LOCK |
| 839 | ** |
| 840 | ** Sometimes when requesting one lock state, additional lock states |
| 841 | ** are inserted in between. The locking might fail on one of the later |
| 842 | ** transitions leaving the lock state different from what it started but |
| 843 | ** still short of its goal. The following chart shows the allowed |
| 844 | ** transitions and the inserted intermediate states: |
| 845 | ** |
| 846 | ** UNLOCKED -> SHARED |
| 847 | ** SHARED -> RESERVED |
| 848 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 849 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 850 | ** PENDING -> EXCLUSIVE |
| 851 | ** |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 852 | ** This routine will only increase a lock. The winUnlock() routine |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 853 | ** erases all locks at once and returns us immediately to locking level 0. |
| 854 | ** It is not possible to lower the locking level one step at a time. You |
| 855 | ** must go straight to locking level 0. |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 856 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 857 | static int winLock(sqlite3_file *id, int locktype){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 858 | int rc = SQLITE_OK; /* Return code from subroutines */ |
| 859 | int res = 1; /* Result of a windows lock call */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 860 | int newLocktype; /* Set pFile->locktype to this value before exiting */ |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 861 | int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 862 | winFile *pFile = (winFile*)id; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 863 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 864 | assert( pFile!=0 ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 865 | OSTRACE5("LOCK %d %d was %d(%d)\n", |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 866 | pFile->h, locktype, pFile->locktype, pFile->sharedLockByte); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 867 | |
| 868 | /* If there is already a lock of this type or more restrictive on the |
| 869 | ** OsFile, do nothing. Don't use the end_lock: exit path, as |
| 870 | ** sqlite3OsEnterMutex() hasn't been called yet. |
| 871 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 872 | if( pFile->locktype>=locktype ){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 873 | return SQLITE_OK; |
| 874 | } |
| 875 | |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 876 | /* Make sure the locking sequence is correct |
| 877 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 878 | assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 879 | assert( locktype!=PENDING_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 880 | assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 881 | |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 882 | /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or |
| 883 | ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of |
| 884 | ** the PENDING_LOCK byte is temporary. |
| 885 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 886 | newLocktype = pFile->locktype; |
| 887 | if( pFile->locktype==NO_LOCK |
| 888 | || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK) |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 889 | ){ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 890 | int cnt = 3; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 891 | while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 892 | /* Try 3 times to get the pending lock. The pending lock might be |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 893 | ** held by another reader process who will release it momentarily. |
| 894 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 895 | OSTRACE2("could not get a PENDING lock. cnt=%d\n", cnt); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 896 | Sleep(1); |
| 897 | } |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 898 | gotPendingLock = res; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | /* Acquire a shared lock |
| 902 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 903 | if( locktype==SHARED_LOCK && res ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 904 | assert( pFile->locktype==NO_LOCK ); |
| 905 | res = getReadLock(pFile); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 906 | if( res ){ |
| 907 | newLocktype = SHARED_LOCK; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
| 911 | /* Acquire a RESERVED lock |
| 912 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 913 | if( locktype==RESERVED_LOCK && res ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 914 | assert( pFile->locktype==SHARED_LOCK ); |
| 915 | res = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 916 | if( res ){ |
| 917 | newLocktype = RESERVED_LOCK; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | /* Acquire a PENDING lock |
| 922 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 923 | if( locktype==EXCLUSIVE_LOCK && res ){ |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 924 | newLocktype = PENDING_LOCK; |
| 925 | gotPendingLock = 0; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | /* Acquire an EXCLUSIVE lock |
| 929 | */ |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 930 | if( locktype==EXCLUSIVE_LOCK && res ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 931 | assert( pFile->locktype>=SHARED_LOCK ); |
| 932 | res = unlockReadLock(pFile); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 933 | OSTRACE2("unreadlock = %d\n", res); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 934 | res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 935 | if( res ){ |
| 936 | newLocktype = EXCLUSIVE_LOCK; |
| 937 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 938 | OSTRACE2("error-code = %d\n", GetLastError()); |
drh | 8fea128 | 2007-05-14 12:12:11 +0000 | [diff] [blame] | 939 | getReadLock(pFile); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | |
| 943 | /* If we are holding a PENDING lock that ought to be released, then |
| 944 | ** release it now. |
| 945 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 946 | if( gotPendingLock && locktype==SHARED_LOCK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 947 | UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /* Update the state of the lock has held in the file descriptor then |
| 951 | ** return the appropriate result code. |
| 952 | */ |
| 953 | if( res ){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 954 | rc = SQLITE_OK; |
| 955 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 956 | OSTRACE4("LOCK FAILED %d trying for %d but got %d\n", pFile->h, |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 957 | locktype, newLocktype); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 958 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 959 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 960 | pFile->locktype = newLocktype; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 961 | return rc; |
| 962 | } |
| 963 | |
| 964 | /* |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 965 | ** This routine checks if there is a RESERVED lock held on the specified |
| 966 | ** file by this or any other process. If such a lock is held, return |
| 967 | ** non-zero, otherwise zero. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 968 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 969 | static int winCheckReservedLock(sqlite3_file *id, int *pResOut){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 970 | int rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 971 | winFile *pFile = (winFile*)id; |
| 972 | assert( pFile!=0 ); |
| 973 | if( pFile->locktype>=RESERVED_LOCK ){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 974 | rc = 1; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 975 | OSTRACE3("TEST WR-LOCK %d %d (local)\n", pFile->h, rc); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 976 | }else{ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 977 | rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 978 | if( rc ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 979 | UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 980 | } |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 981 | rc = !rc; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 982 | OSTRACE3("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 983 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 984 | *pResOut = rc; |
| 985 | return SQLITE_OK; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 989 | ** Lower the locking level on file descriptor id to locktype. locktype |
| 990 | ** must be either NO_LOCK or SHARED_LOCK. |
| 991 | ** |
| 992 | ** If the locking level of the file descriptor is already at or below |
| 993 | ** the requested locking level, this routine is a no-op. |
| 994 | ** |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 995 | ** It is not possible for this routine to fail if the second argument |
| 996 | ** is NO_LOCK. If the second argument is SHARED_LOCK then this routine |
| 997 | ** might return SQLITE_IOERR; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 998 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 999 | static int winUnlock(sqlite3_file *id, int locktype){ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1000 | int type; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1001 | winFile *pFile = (winFile*)id; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1002 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1003 | assert( pFile!=0 ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1004 | assert( locktype<=SHARED_LOCK ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1005 | OSTRACE5("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1006 | pFile->locktype, pFile->sharedLockByte); |
| 1007 | type = pFile->locktype; |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 1008 | if( type>=EXCLUSIVE_LOCK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1009 | UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); |
| 1010 | if( locktype==SHARED_LOCK && !getReadLock(pFile) ){ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1011 | /* This should never happen. We should always be able to |
| 1012 | ** reacquire the read lock */ |
drh | 9cce710 | 2007-01-09 17:18:19 +0000 | [diff] [blame] | 1013 | rc = SQLITE_IOERR_UNLOCK; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1014 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1015 | } |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 1016 | if( type>=RESERVED_LOCK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1017 | UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1018 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1019 | if( locktype==NO_LOCK && type>=SHARED_LOCK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1020 | unlockReadLock(pFile); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1021 | } |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1022 | if( type>=PENDING_LOCK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1023 | UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0); |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1024 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1025 | pFile->locktype = locktype; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1026 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 1030 | ** Control and query of the open file handle. |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 1031 | */ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 1032 | static int winFileControl(sqlite3_file *id, int op, void *pArg){ |
| 1033 | switch( op ){ |
| 1034 | case SQLITE_FCNTL_LOCKSTATE: { |
| 1035 | *(int*)pArg = ((winFile*)id)->locktype; |
| 1036 | return SQLITE_OK; |
| 1037 | } |
| 1038 | } |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1039 | return SQLITE_ERROR; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 1043 | ** Return the sector size in bytes of the underlying block device for |
| 1044 | ** the specified file. This is almost always 512 bytes, but may be |
| 1045 | ** larger for some devices. |
| 1046 | ** |
| 1047 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 1048 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 1049 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 1050 | ** same for both. |
| 1051 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1052 | static int winSectorSize(sqlite3_file *id){ |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 1053 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | /* |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1057 | ** Return a vector of device characteristics. |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1058 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1059 | static int winDeviceCharacteristics(sqlite3_file *id){ |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
| 1063 | /* |
| 1064 | ** This vector defines all the methods that can operate on an |
| 1065 | ** sqlite3_file for win32. |
| 1066 | */ |
| 1067 | static const sqlite3_io_methods winIoMethod = { |
| 1068 | 1, /* iVersion */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1069 | winClose, |
| 1070 | winRead, |
| 1071 | winWrite, |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1072 | winTruncate, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1073 | winSync, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1074 | winFileSize, |
| 1075 | winLock, |
| 1076 | winUnlock, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1077 | winCheckReservedLock, |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 1078 | winFileControl, |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 1079 | winSectorSize, |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1080 | winDeviceCharacteristics |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1081 | }; |
| 1082 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1083 | /*************************************************************************** |
| 1084 | ** Here ends the I/O methods that form the sqlite3_io_methods object. |
| 1085 | ** |
| 1086 | ** The next block of code implements the VFS methods. |
| 1087 | ****************************************************************************/ |
| 1088 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1089 | /* |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1090 | ** Convert a UTF-8 filename into whatever form the underlying |
| 1091 | ** operating system wants filenames in. Space to hold the result |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 1092 | ** is obtained from malloc and must be freed by the calling |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1093 | ** function. |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1094 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1095 | static void *convertUtf8Filename(const char *zFilename){ |
| 1096 | void *zConverted = 0; |
| 1097 | if( isNT() ){ |
| 1098 | zConverted = utf8ToUnicode(zFilename); |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 1099 | /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. |
| 1100 | */ |
| 1101 | #if SQLITE_OS_WINCE==0 |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1102 | }else{ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1103 | zConverted = utf8ToMbcs(zFilename); |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 1104 | #endif |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1105 | } |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1106 | /* caller will handle out of memory */ |
| 1107 | return zConverted; |
| 1108 | } |
| 1109 | |
| 1110 | /* |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1111 | ** Create a temporary file name in zBuf. zBuf must be big enough to |
| 1112 | ** hold at pVfs->mxPathname characters. |
| 1113 | */ |
| 1114 | static int getTempname(int nBuf, char *zBuf){ |
| 1115 | static char zChars[] = |
| 1116 | "abcdefghijklmnopqrstuvwxyz" |
| 1117 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 1118 | "0123456789"; |
shane | 3582c5a | 2008-07-31 01:34:34 +0000 | [diff] [blame] | 1119 | size_t i, j; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1120 | char zTempPath[MAX_PATH+1]; |
| 1121 | if( sqlite3_temp_directory ){ |
| 1122 | sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory); |
| 1123 | }else if( isNT() ){ |
| 1124 | char *zMulti; |
| 1125 | WCHAR zWidePath[MAX_PATH]; |
| 1126 | GetTempPathW(MAX_PATH-30, zWidePath); |
| 1127 | zMulti = unicodeToUtf8(zWidePath); |
| 1128 | if( zMulti ){ |
| 1129 | sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti); |
| 1130 | free(zMulti); |
| 1131 | }else{ |
| 1132 | return SQLITE_NOMEM; |
| 1133 | } |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 1134 | /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. |
| 1135 | ** Since the ASCII version of these Windows API do not exist for WINCE, |
| 1136 | ** it's important to not reference them for WINCE builds. |
| 1137 | */ |
| 1138 | #if SQLITE_OS_WINCE==0 |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1139 | }else{ |
| 1140 | char *zUtf8; |
| 1141 | char zMbcsPath[MAX_PATH]; |
| 1142 | GetTempPathA(MAX_PATH-30, zMbcsPath); |
drh | 1d29885 | 2008-11-18 19:18:52 +0000 | [diff] [blame] | 1143 | zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1144 | if( zUtf8 ){ |
| 1145 | sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8); |
| 1146 | free(zUtf8); |
| 1147 | }else{ |
| 1148 | return SQLITE_NOMEM; |
| 1149 | } |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 1150 | #endif |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1151 | } |
| 1152 | for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){} |
| 1153 | zTempPath[i] = 0; |
| 1154 | sqlite3_snprintf(nBuf-30, zBuf, |
| 1155 | "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath); |
| 1156 | j = strlen(zBuf); |
| 1157 | sqlite3_randomness(20, &zBuf[j]); |
| 1158 | for(i=0; i<20; i++, j++){ |
| 1159 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 1160 | } |
| 1161 | zBuf[j] = 0; |
| 1162 | OSTRACE2("TEMP FILENAME: %s\n", zBuf); |
| 1163 | return SQLITE_OK; |
| 1164 | } |
| 1165 | |
shane | 820800d | 2008-07-22 05:32:03 +0000 | [diff] [blame] | 1166 | /* |
| 1167 | ** The return value of getLastErrorMsg |
| 1168 | ** is zero if the error message fits in the buffer, or non-zero |
| 1169 | ** otherwise (if the message was truncated). |
| 1170 | */ |
| 1171 | static int getLastErrorMsg(int nBuf, char *zBuf){ |
| 1172 | DWORD error = GetLastError(); |
| 1173 | |
| 1174 | #if SQLITE_OS_WINCE |
| 1175 | sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", error, error); |
| 1176 | #else |
| 1177 | /* FormatMessage returns 0 on failure. Otherwise it |
| 1178 | ** returns the number of TCHARs written to the output |
| 1179 | ** buffer, excluding the terminating null char. |
| 1180 | */ |
| 1181 | if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, |
| 1182 | NULL, |
| 1183 | error, |
| 1184 | 0, |
| 1185 | zBuf, |
| 1186 | nBuf-1, |
| 1187 | 0)) |
| 1188 | { |
| 1189 | sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", error, error); |
| 1190 | } |
| 1191 | #endif |
| 1192 | |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1196 | |
| 1197 | /* |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1198 | ** Open a file. |
| 1199 | */ |
| 1200 | static int winOpen( |
| 1201 | sqlite3_vfs *pVfs, /* Not used */ |
| 1202 | const char *zName, /* Name of the file (UTF-8) */ |
| 1203 | sqlite3_file *id, /* Write the SQLite file handle here */ |
| 1204 | int flags, /* Open mode flags */ |
| 1205 | int *pOutFlags /* Status return flags */ |
| 1206 | ){ |
| 1207 | HANDLE h; |
| 1208 | DWORD dwDesiredAccess; |
| 1209 | DWORD dwShareMode; |
| 1210 | DWORD dwCreationDisposition; |
| 1211 | DWORD dwFlagsAndAttributes = 0; |
shane | d94b055 | 2008-09-30 04:20:07 +0000 | [diff] [blame] | 1212 | #if SQLITE_OS_WINCE |
| 1213 | int isTemp = 0; |
| 1214 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1215 | winFile *pFile = (winFile*)id; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 1216 | void *zConverted; /* Filename in OS encoding */ |
| 1217 | const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */ |
| 1218 | char zTmpname[MAX_PATH+1]; /* Buffer used to create temp filename */ |
| 1219 | |
| 1220 | /* If the second argument to this function is NULL, generate a |
| 1221 | ** temporary file name to use |
| 1222 | */ |
| 1223 | if( !zUtf8Name ){ |
| 1224 | int rc = getTempname(MAX_PATH+1, zTmpname); |
| 1225 | if( rc!=SQLITE_OK ){ |
| 1226 | return rc; |
| 1227 | } |
| 1228 | zUtf8Name = zTmpname; |
| 1229 | } |
| 1230 | |
| 1231 | /* Convert the filename to the system encoding. */ |
| 1232 | zConverted = convertUtf8Filename(zUtf8Name); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1233 | if( zConverted==0 ){ |
| 1234 | return SQLITE_NOMEM; |
| 1235 | } |
| 1236 | |
| 1237 | if( flags & SQLITE_OPEN_READWRITE ){ |
| 1238 | dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; |
| 1239 | }else{ |
| 1240 | dwDesiredAccess = GENERIC_READ; |
| 1241 | } |
| 1242 | if( flags & SQLITE_OPEN_CREATE ){ |
| 1243 | dwCreationDisposition = OPEN_ALWAYS; |
| 1244 | }else{ |
| 1245 | dwCreationDisposition = OPEN_EXISTING; |
| 1246 | } |
| 1247 | if( flags & SQLITE_OPEN_MAIN_DB ){ |
| 1248 | dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; |
| 1249 | }else{ |
| 1250 | dwShareMode = 0; |
| 1251 | } |
drh | 64c1ea6 | 2007-12-10 21:11:31 +0000 | [diff] [blame] | 1252 | if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 1253 | #if SQLITE_OS_WINCE |
drh | 0cd1ea5 | 2007-10-08 15:06:03 +0000 | [diff] [blame] | 1254 | dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN; |
shane | d94b055 | 2008-09-30 04:20:07 +0000 | [diff] [blame] | 1255 | isTemp = 1; |
drh | 0cd1ea5 | 2007-10-08 15:06:03 +0000 | [diff] [blame] | 1256 | #else |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1257 | dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY |
| 1258 | | FILE_ATTRIBUTE_HIDDEN |
| 1259 | | FILE_FLAG_DELETE_ON_CLOSE; |
drh | 0cd1ea5 | 2007-10-08 15:06:03 +0000 | [diff] [blame] | 1260 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1261 | }else{ |
| 1262 | dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL; |
| 1263 | } |
drh | 496936c | 2007-10-08 12:21:10 +0000 | [diff] [blame] | 1264 | /* Reports from the internet are that performance is always |
| 1265 | ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */ |
shane | d94b055 | 2008-09-30 04:20:07 +0000 | [diff] [blame] | 1266 | #if SQLITE_OS_WINCE |
drh | 496936c | 2007-10-08 12:21:10 +0000 | [diff] [blame] | 1267 | dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS; |
shane | d94b055 | 2008-09-30 04:20:07 +0000 | [diff] [blame] | 1268 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1269 | if( isNT() ){ |
| 1270 | h = CreateFileW((WCHAR*)zConverted, |
| 1271 | dwDesiredAccess, |
| 1272 | dwShareMode, |
| 1273 | NULL, |
| 1274 | dwCreationDisposition, |
| 1275 | dwFlagsAndAttributes, |
| 1276 | NULL |
| 1277 | ); |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 1278 | /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. |
| 1279 | ** Since the ASCII version of these Windows API do not exist for WINCE, |
| 1280 | ** it's important to not reference them for WINCE builds. |
| 1281 | */ |
| 1282 | #if SQLITE_OS_WINCE==0 |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1283 | }else{ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1284 | h = CreateFileA((char*)zConverted, |
| 1285 | dwDesiredAccess, |
| 1286 | dwShareMode, |
| 1287 | NULL, |
| 1288 | dwCreationDisposition, |
| 1289 | dwFlagsAndAttributes, |
| 1290 | NULL |
| 1291 | ); |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 1292 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1293 | } |
| 1294 | if( h==INVALID_HANDLE_VALUE ){ |
drh | e1843af | 2007-08-30 16:46:04 +0000 | [diff] [blame] | 1295 | free(zConverted); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1296 | if( flags & SQLITE_OPEN_READWRITE ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1297 | return winOpen(0, zName, id, |
| 1298 | ((flags|SQLITE_OPEN_READONLY)&~SQLITE_OPEN_READWRITE), pOutFlags); |
| 1299 | }else{ |
| 1300 | return SQLITE_CANTOPEN; |
| 1301 | } |
| 1302 | } |
| 1303 | if( pOutFlags ){ |
| 1304 | if( flags & SQLITE_OPEN_READWRITE ){ |
| 1305 | *pOutFlags = SQLITE_OPEN_READWRITE; |
| 1306 | }else{ |
| 1307 | *pOutFlags = SQLITE_OPEN_READONLY; |
| 1308 | } |
| 1309 | } |
| 1310 | memset(pFile, 0, sizeof(*pFile)); |
| 1311 | pFile->pMethod = &winIoMethod; |
| 1312 | pFile->h = h; |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 1313 | #if SQLITE_OS_WINCE |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1314 | if( (flags & (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)) == |
| 1315 | (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB) |
drh | aab2e6d | 2007-10-08 12:22:57 +0000 | [diff] [blame] | 1316 | && !winceCreateLock(zName, pFile) |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1317 | ){ |
| 1318 | CloseHandle(h); |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 1319 | free(zConverted); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1320 | return SQLITE_CANTOPEN; |
| 1321 | } |
drh | fc3afb6 | 2007-10-09 15:36:10 +0000 | [diff] [blame] | 1322 | if( isTemp ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1323 | pFile->zDeleteOnClose = zConverted; |
| 1324 | }else |
| 1325 | #endif |
| 1326 | { |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 1327 | free(zConverted); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1328 | } |
drh | af5f040 | 2007-09-03 17:09:03 +0000 | [diff] [blame] | 1329 | OpenCounter(+1); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1330 | return SQLITE_OK; |
| 1331 | } |
| 1332 | |
| 1333 | /* |
| 1334 | ** Delete the named file. |
| 1335 | ** |
| 1336 | ** Note that windows does not allow a file to be deleted if some other |
| 1337 | ** process has it open. Sometimes a virus scanner or indexing program |
| 1338 | ** will open a journal file shortly after it is created in order to do |
shane | 3582c5a | 2008-07-31 01:34:34 +0000 | [diff] [blame] | 1339 | ** whatever it does. While this other process is holding the |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1340 | ** file open, we will be unable to delete it. To work around this |
| 1341 | ** problem, we delay 100 milliseconds and try to delete again. Up |
| 1342 | ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving |
| 1343 | ** up and returning an error. |
| 1344 | */ |
drh | f024f0b | 2007-11-07 01:19:07 +0000 | [diff] [blame] | 1345 | #define MX_DELETION_ATTEMPTS 5 |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1346 | static int winDelete( |
| 1347 | sqlite3_vfs *pVfs, /* Not used on win32 */ |
| 1348 | const char *zFilename, /* Name of file to delete */ |
| 1349 | int syncDir /* Not used on win32 */ |
| 1350 | ){ |
| 1351 | int cnt = 0; |
shane | d94b055 | 2008-09-30 04:20:07 +0000 | [diff] [blame] | 1352 | DWORD rc; |
shane | 3582c5a | 2008-07-31 01:34:34 +0000 | [diff] [blame] | 1353 | DWORD error; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1354 | void *zConverted = convertUtf8Filename(zFilename); |
| 1355 | if( zConverted==0 ){ |
| 1356 | return SQLITE_NOMEM; |
| 1357 | } |
| 1358 | SimulateIOError(return SQLITE_IOERR_DELETE); |
| 1359 | if( isNT() ){ |
| 1360 | do{ |
drh | f024f0b | 2007-11-07 01:19:07 +0000 | [diff] [blame] | 1361 | DeleteFileW(zConverted); |
shane | 3582c5a | 2008-07-31 01:34:34 +0000 | [diff] [blame] | 1362 | }while( ( ((rc = GetFileAttributesW(zConverted)) != INVALID_FILE_ATTRIBUTES) |
| 1363 | || ((error = GetLastError()) == ERROR_ACCESS_DENIED)) |
shane | d94b055 | 2008-09-30 04:20:07 +0000 | [diff] [blame] | 1364 | && (++cnt < MX_DELETION_ATTEMPTS) |
shane | 3582c5a | 2008-07-31 01:34:34 +0000 | [diff] [blame] | 1365 | && (Sleep(100), 1) ); |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 1366 | /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. |
| 1367 | ** Since the ASCII version of these Windows API do not exist for WINCE, |
| 1368 | ** it's important to not reference them for WINCE builds. |
| 1369 | */ |
| 1370 | #if SQLITE_OS_WINCE==0 |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1371 | }else{ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1372 | do{ |
drh | f024f0b | 2007-11-07 01:19:07 +0000 | [diff] [blame] | 1373 | DeleteFileA(zConverted); |
shane | 3582c5a | 2008-07-31 01:34:34 +0000 | [diff] [blame] | 1374 | }while( ( ((rc = GetFileAttributesA(zConverted)) != INVALID_FILE_ATTRIBUTES) |
| 1375 | || ((error = GetLastError()) == ERROR_ACCESS_DENIED)) |
shane | d94b055 | 2008-09-30 04:20:07 +0000 | [diff] [blame] | 1376 | && (++cnt < MX_DELETION_ATTEMPTS) |
shane | 3582c5a | 2008-07-31 01:34:34 +0000 | [diff] [blame] | 1377 | && (Sleep(100), 1) ); |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 1378 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1379 | } |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 1380 | free(zConverted); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1381 | OSTRACE2("DELETE \"%s\"\n", zFilename); |
shane | d94b055 | 2008-09-30 04:20:07 +0000 | [diff] [blame] | 1382 | return ( (rc == INVALID_FILE_ATTRIBUTES) |
shane | 3582c5a | 2008-07-31 01:34:34 +0000 | [diff] [blame] | 1383 | && (error == ERROR_FILE_NOT_FOUND)) ? SQLITE_OK : SQLITE_IOERR_DELETE; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | /* |
| 1387 | ** Check the existance and status of a file. |
| 1388 | */ |
| 1389 | static int winAccess( |
| 1390 | sqlite3_vfs *pVfs, /* Not used on win32 */ |
| 1391 | const char *zFilename, /* Name of file to check */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1392 | int flags, /* Type of test to make on this file */ |
| 1393 | int *pResOut /* OUT: Result */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1394 | ){ |
| 1395 | DWORD attr; |
| 1396 | int rc; |
| 1397 | void *zConverted = convertUtf8Filename(zFilename); |
| 1398 | if( zConverted==0 ){ |
| 1399 | return SQLITE_NOMEM; |
| 1400 | } |
| 1401 | if( isNT() ){ |
| 1402 | attr = GetFileAttributesW((WCHAR*)zConverted); |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 1403 | /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. |
| 1404 | ** Since the ASCII version of these Windows API do not exist for WINCE, |
| 1405 | ** it's important to not reference them for WINCE builds. |
| 1406 | */ |
| 1407 | #if SQLITE_OS_WINCE==0 |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1408 | }else{ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1409 | attr = GetFileAttributesA((char*)zConverted); |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 1410 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1411 | } |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 1412 | free(zConverted); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1413 | switch( flags ){ |
drh | 50d3f90 | 2007-08-27 21:10:36 +0000 | [diff] [blame] | 1414 | case SQLITE_ACCESS_READ: |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1415 | case SQLITE_ACCESS_EXISTS: |
shane | 820800d | 2008-07-22 05:32:03 +0000 | [diff] [blame] | 1416 | rc = attr!=INVALID_FILE_ATTRIBUTES; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1417 | break; |
| 1418 | case SQLITE_ACCESS_READWRITE: |
| 1419 | rc = (attr & FILE_ATTRIBUTE_READONLY)==0; |
| 1420 | break; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1421 | default: |
| 1422 | assert(!"Invalid flags argument"); |
| 1423 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1424 | *pResOut = rc; |
| 1425 | return SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1429 | /* |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1430 | ** Turn a relative pathname into a full pathname. Write the full |
| 1431 | ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname |
| 1432 | ** bytes in size. |
| 1433 | */ |
| 1434 | static int winFullPathname( |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1435 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 1436 | const char *zRelative, /* Possibly relative input path */ |
| 1437 | int nFull, /* Size of output buffer in bytes */ |
| 1438 | char *zFull /* Output buffer */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1439 | ){ |
| 1440 | |
| 1441 | #if defined(__CYGWIN__) |
| 1442 | cygwin_conv_to_full_win32_path(zRelative, zFull); |
danielk1977 | 076f1c0 | 2007-09-12 14:09:23 +0000 | [diff] [blame] | 1443 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1444 | #endif |
| 1445 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 1446 | #if SQLITE_OS_WINCE |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1447 | /* WinCE has no concept of a relative pathname, or so I am told. */ |
| 1448 | sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zRelative); |
drh | e825609 | 2007-10-09 15:20:39 +0000 | [diff] [blame] | 1449 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1450 | #endif |
| 1451 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 1452 | #if !SQLITE_OS_WINCE && !defined(__CYGWIN__) |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1453 | int nByte; |
| 1454 | void *zConverted; |
| 1455 | char *zOut; |
| 1456 | zConverted = convertUtf8Filename(zRelative); |
| 1457 | if( isNT() ){ |
| 1458 | WCHAR *zTemp; |
| 1459 | nByte = GetFullPathNameW((WCHAR*)zConverted, 0, 0, 0) + 3; |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 1460 | zTemp = malloc( nByte*sizeof(zTemp[0]) ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1461 | if( zTemp==0 ){ |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 1462 | free(zConverted); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1463 | return SQLITE_NOMEM; |
| 1464 | } |
| 1465 | GetFullPathNameW((WCHAR*)zConverted, nByte, zTemp, 0); |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 1466 | free(zConverted); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1467 | zOut = unicodeToUtf8(zTemp); |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 1468 | free(zTemp); |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 1469 | /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. |
| 1470 | ** Since the ASCII version of these Windows API do not exist for WINCE, |
| 1471 | ** it's important to not reference them for WINCE builds. |
| 1472 | */ |
| 1473 | #if SQLITE_OS_WINCE==0 |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1474 | }else{ |
| 1475 | char *zTemp; |
| 1476 | nByte = GetFullPathNameA((char*)zConverted, 0, 0, 0) + 3; |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 1477 | zTemp = malloc( nByte*sizeof(zTemp[0]) ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1478 | if( zTemp==0 ){ |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 1479 | free(zConverted); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1480 | return SQLITE_NOMEM; |
| 1481 | } |
| 1482 | GetFullPathNameA((char*)zConverted, nByte, zTemp, 0); |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 1483 | free(zConverted); |
drh | 1d29885 | 2008-11-18 19:18:52 +0000 | [diff] [blame] | 1484 | zOut = sqlite3_win32_mbcs_to_utf8(zTemp); |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 1485 | free(zTemp); |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 1486 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1487 | } |
| 1488 | if( zOut ){ |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 1489 | sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut); |
| 1490 | free(zOut); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1491 | return SQLITE_OK; |
| 1492 | }else{ |
| 1493 | return SQLITE_NOMEM; |
| 1494 | } |
| 1495 | #endif |
| 1496 | } |
| 1497 | |
| 1498 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 1499 | /* |
| 1500 | ** Interfaces for opening a shared library, finding entry points |
| 1501 | ** within the shared library, and closing the shared library. |
| 1502 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1503 | /* |
| 1504 | ** Interfaces for opening a shared library, finding entry points |
| 1505 | ** within the shared library, and closing the shared library. |
| 1506 | */ |
| 1507 | static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){ |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 1508 | HANDLE h; |
| 1509 | void *zConverted = convertUtf8Filename(zFilename); |
| 1510 | if( zConverted==0 ){ |
| 1511 | return 0; |
| 1512 | } |
| 1513 | if( isNT() ){ |
drh | 584c094 | 2006-12-21 03:20:40 +0000 | [diff] [blame] | 1514 | h = LoadLibraryW((WCHAR*)zConverted); |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 1515 | /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. |
| 1516 | ** Since the ASCII version of these Windows API do not exist for WINCE, |
| 1517 | ** it's important to not reference them for WINCE builds. |
| 1518 | */ |
| 1519 | #if SQLITE_OS_WINCE==0 |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 1520 | }else{ |
drh | 584c094 | 2006-12-21 03:20:40 +0000 | [diff] [blame] | 1521 | h = LoadLibraryA((char*)zConverted); |
shane | 891adea | 2008-10-22 16:55:47 +0000 | [diff] [blame] | 1522 | #endif |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 1523 | } |
drh | b11caac | 2007-08-24 17:52:21 +0000 | [diff] [blame] | 1524 | free(zConverted); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 1525 | return (void*)h; |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 1526 | } |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1527 | static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){ |
shane | 820800d | 2008-07-22 05:32:03 +0000 | [diff] [blame] | 1528 | getLastErrorMsg(nBuf, zBufOut); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1529 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame^] | 1530 | void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){ |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 1531 | #if SQLITE_OS_WINCE |
drh | 2a4d54b | 2006-12-21 02:21:56 +0000 | [diff] [blame] | 1532 | /* The GetProcAddressA() routine is only available on wince. */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame^] | 1533 | return (void(*)(void))GetProcAddressA((HANDLE)pHandle, zSymbol); |
drh | 2a4d54b | 2006-12-21 02:21:56 +0000 | [diff] [blame] | 1534 | #else |
| 1535 | /* All other windows platforms expect GetProcAddress() to take |
| 1536 | ** an Ansi string regardless of the _UNICODE setting */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame^] | 1537 | return (void(*)(void))GetProcAddress((HANDLE)pHandle, zSymbol); |
drh | 2a4d54b | 2006-12-21 02:21:56 +0000 | [diff] [blame] | 1538 | #endif |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 1539 | } |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1540 | void winDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
| 1541 | FreeLibrary((HANDLE)pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 1542 | } |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1543 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 1544 | #define winDlOpen 0 |
| 1545 | #define winDlError 0 |
| 1546 | #define winDlSym 0 |
| 1547 | #define winDlClose 0 |
| 1548 | #endif |
| 1549 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 1550 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 1551 | /* |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1552 | ** Write up to nBuf bytes of randomness into zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1553 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1554 | static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ |
drh | d1a7931 | 2007-09-03 13:06:11 +0000 | [diff] [blame] | 1555 | int n = 0; |
shane | c7b7f1a | 2008-11-19 21:35:46 +0000 | [diff] [blame] | 1556 | UNUSED_PARAMETER(pVfs); |
| 1557 | #if defined(SQLITE_TEST) |
| 1558 | n = nBuf; |
| 1559 | memset(zBuf, 0, nBuf); |
| 1560 | #else |
drh | 8674261 | 2007-09-05 17:06:03 +0000 | [diff] [blame] | 1561 | if( sizeof(SYSTEMTIME)<=nBuf-n ){ |
drh | d1a7931 | 2007-09-03 13:06:11 +0000 | [diff] [blame] | 1562 | SYSTEMTIME x; |
| 1563 | GetSystemTime(&x); |
| 1564 | memcpy(&zBuf[n], &x, sizeof(x)); |
| 1565 | n += sizeof(x); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1566 | } |
drh | d1a7931 | 2007-09-03 13:06:11 +0000 | [diff] [blame] | 1567 | if( sizeof(DWORD)<=nBuf-n ){ |
| 1568 | DWORD pid = GetCurrentProcessId(); |
| 1569 | memcpy(&zBuf[n], &pid, sizeof(pid)); |
| 1570 | n += sizeof(pid); |
| 1571 | } |
| 1572 | if( sizeof(DWORD)<=nBuf-n ){ |
| 1573 | DWORD cnt = GetTickCount(); |
| 1574 | memcpy(&zBuf[n], &cnt, sizeof(cnt)); |
| 1575 | n += sizeof(cnt); |
| 1576 | } |
| 1577 | if( sizeof(LARGE_INTEGER)<=nBuf-n ){ |
| 1578 | LARGE_INTEGER i; |
| 1579 | QueryPerformanceCounter(&i); |
| 1580 | memcpy(&zBuf[n], &i, sizeof(i)); |
| 1581 | n += sizeof(i); |
| 1582 | } |
shane | c7b7f1a | 2008-11-19 21:35:46 +0000 | [diff] [blame] | 1583 | #endif |
drh | d1a7931 | 2007-09-03 13:06:11 +0000 | [diff] [blame] | 1584 | return n; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1587 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1588 | /* |
| 1589 | ** Sleep for a little while. Return the amount of time slept. |
| 1590 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1591 | static int winSleep(sqlite3_vfs *pVfs, int microsec){ |
| 1592 | Sleep((microsec+999)/1000); |
| 1593 | return ((microsec+999)/1000)*1000; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1597 | ** The following variable, if set to a non-zero value, becomes the result |
| 1598 | ** returned from sqlite3OsCurrentTime(). This is used for testing. |
| 1599 | */ |
| 1600 | #ifdef SQLITE_TEST |
| 1601 | int sqlite3_current_time = 0; |
| 1602 | #endif |
| 1603 | |
| 1604 | /* |
| 1605 | ** Find the current time (in Universal Coordinated Time). Write the |
| 1606 | ** current time and date as a Julian Day number into *prNow and |
| 1607 | ** return 0. Return 1 if the time and date cannot be found. |
| 1608 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1609 | int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1610 | FILETIME ft; |
| 1611 | /* FILETIME structure is a 64-bit value representing the number of |
| 1612 | 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). |
| 1613 | */ |
| 1614 | double now; |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 1615 | #if SQLITE_OS_WINCE |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 1616 | SYSTEMTIME time; |
| 1617 | GetSystemTime(&time); |
shane | 820800d | 2008-07-22 05:32:03 +0000 | [diff] [blame] | 1618 | /* if SystemTimeToFileTime() fails, it returns zero. */ |
| 1619 | if (!SystemTimeToFileTime(&time,&ft)){ |
| 1620 | return 1; |
| 1621 | } |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 1622 | #else |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1623 | GetSystemTimeAsFileTime( &ft ); |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 1624 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1625 | now = ((double)ft.dwHighDateTime) * 4294967296.0; |
| 1626 | *prNow = (now + ft.dwLowDateTime)/864000000000.0 + 2305813.5; |
| 1627 | #ifdef SQLITE_TEST |
| 1628 | if( sqlite3_current_time ){ |
| 1629 | *prNow = sqlite3_current_time/86400.0 + 2440587.5; |
| 1630 | } |
| 1631 | #endif |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
shane | 820800d | 2008-07-22 05:32:03 +0000 | [diff] [blame] | 1635 | /* |
| 1636 | ** The idea is that this function works like a combination of |
| 1637 | ** GetLastError() and FormatMessage() on windows (or errno and |
| 1638 | ** strerror_r() on unix). After an error is returned by an OS |
| 1639 | ** function, SQLite calls this function with zBuf pointing to |
| 1640 | ** a buffer of nBuf bytes. The OS layer should populate the |
| 1641 | ** buffer with a nul-terminated UTF-8 encoded error message |
| 1642 | ** describing the last IO error to have occured within the calling |
| 1643 | ** thread. |
| 1644 | ** |
| 1645 | ** If the error message is too large for the supplied buffer, |
| 1646 | ** it should be truncated. The return value of xGetLastError |
| 1647 | ** is zero if the error message fits in the buffer, or non-zero |
| 1648 | ** otherwise (if the message was truncated). If non-zero is returned, |
| 1649 | ** then it is not necessary to include the nul-terminator character |
| 1650 | ** in the output buffer. |
| 1651 | ** |
| 1652 | ** Not supplying an error message will have no adverse effect |
| 1653 | ** on SQLite. It is fine to have an implementation that never |
| 1654 | ** returns an error message: |
| 1655 | ** |
| 1656 | ** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ |
| 1657 | ** assert(zBuf[0]=='\0'); |
| 1658 | ** return 0; |
| 1659 | ** } |
| 1660 | ** |
| 1661 | ** However if an error message is supplied, it will be incorporated |
| 1662 | ** by sqlite into the error message available to the user using |
| 1663 | ** sqlite3_errmsg(), possibly making IO errors easier to debug. |
| 1664 | */ |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 1665 | static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ |
shane | 820800d | 2008-07-22 05:32:03 +0000 | [diff] [blame] | 1666 | return getLastErrorMsg(nBuf, zBuf); |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 1667 | } |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 1668 | |
| 1669 | /* |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 1670 | ** Initialize and deinitialize the operating system interface. |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1671 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 1672 | int sqlite3_os_init(void){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1673 | static sqlite3_vfs winVfs = { |
| 1674 | 1, /* iVersion */ |
| 1675 | sizeof(winFile), /* szOsFile */ |
| 1676 | MAX_PATH, /* mxPathname */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1677 | 0, /* pNext */ |
| 1678 | "win32", /* zName */ |
| 1679 | 0, /* pAppData */ |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 1680 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1681 | winOpen, /* xOpen */ |
| 1682 | winDelete, /* xDelete */ |
| 1683 | winAccess, /* xAccess */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1684 | winFullPathname, /* xFullPathname */ |
| 1685 | winDlOpen, /* xDlOpen */ |
| 1686 | winDlError, /* xDlError */ |
| 1687 | winDlSym, /* xDlSym */ |
| 1688 | winDlClose, /* xDlClose */ |
| 1689 | winRandomness, /* xRandomness */ |
| 1690 | winSleep, /* xSleep */ |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 1691 | winCurrentTime, /* xCurrentTime */ |
| 1692 | winGetLastError /* xGetLastError */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1693 | }; |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 1694 | sqlite3_vfs_register(&winVfs, 1); |
| 1695 | return SQLITE_OK; |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1696 | } |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 1697 | int sqlite3_os_end(void){ |
| 1698 | return SQLITE_OK; |
| 1699 | } |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 1700 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 1701 | #endif /* SQLITE_OS_WIN */ |