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