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