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