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 */ 15 #include "sqliteInt.h" 16 #include "os.h" 17 #if OS_WIN /* This file is used for windows only */ 18 19 #include <winbase.h> 20 21 #ifdef __CYGWIN__ 22 # include <sys/cygwin.h> 23 #endif 24 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 /* 38 ** Do not include any of the File I/O interface procedures if the 39 ** SQLITE_OMIT_DISKIO macro is defined (indicating that there database 40 ** will be in-memory only) 41 */ 42 #ifndef SQLITE_OMIT_DISKIO 43 44 /* 45 ** Delete the named file 46 */ 47 int sqlite3OsDelete(const char *zFilename){ 48 DeleteFileA(zFilename); 49 TRACE2("DELETE \"%s\"\n", zFilename); 50 return SQLITE_OK; 51 } 52 53 /* 54 ** Return TRUE if the named file exists. 55 */ 56 int sqlite3OsFileExists(const char *zFilename){ 57 return GetFileAttributesA(zFilename) != 0xffffffff; 58 } 59 60 /* 61 ** Attempt to open a file for both reading and writing. If that 62 ** fails, try opening it read-only. If the file does not exist, 63 ** try to create it. 64 ** 65 ** On success, a handle for the open file is written to *id 66 ** and *pReadonly is set to 0 if the file was opened for reading and 67 ** writing or 1 if the file was opened read-only. The function returns 68 ** SQLITE_OK. 69 ** 70 ** On failure, the function returns SQLITE_CANTOPEN and leaves 71 ** *id and *pReadonly unchanged. 72 */ 73 int sqlite3OsOpenReadWrite( 74 const char *zFilename, 75 OsFile *id, 76 int *pReadonly 77 ){ 78 HANDLE h; 79 assert( !id->isOpen ); 80 h = CreateFileA(zFilename, 81 GENERIC_READ | GENERIC_WRITE, 82 FILE_SHARE_READ | FILE_SHARE_WRITE, 83 NULL, 84 OPEN_ALWAYS, 85 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, 86 NULL 87 ); 88 if( h==INVALID_HANDLE_VALUE ){ 89 h = CreateFileA(zFilename, 90 GENERIC_READ, 91 FILE_SHARE_READ, 92 NULL, 93 OPEN_ALWAYS, 94 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, 95 NULL 96 ); 97 if( h==INVALID_HANDLE_VALUE ){ 98 return SQLITE_CANTOPEN; 99 } 100 *pReadonly = 1; 101 }else{ 102 *pReadonly = 0; 103 } 104 id->h = h; 105 id->locktype = NO_LOCK; 106 id->sharedLockByte = 0; 107 id->isOpen = 1; 108 OpenCounter(+1); 109 TRACE3("OPEN R/W %d \"%s\"\n", h, zFilename); 110 return SQLITE_OK; 111 } 112 113 114 /* 115 ** Attempt to open a new file for exclusive access by this process. 116 ** The file will be opened for both reading and writing. To avoid 117 ** a potential security problem, we do not allow the file to have 118 ** previously existed. Nor do we allow the file to be a symbolic 119 ** link. 120 ** 121 ** If delFlag is true, then make arrangements to automatically delete 122 ** the file when it is closed. 123 ** 124 ** On success, write the file handle into *id and return SQLITE_OK. 125 ** 126 ** On failure, return SQLITE_CANTOPEN. 127 */ 128 int sqlite3OsOpenExclusive(const char *zFilename, OsFile *id, int delFlag){ 129 HANDLE h; 130 int fileflags; 131 assert( !id->isOpen ); 132 if( delFlag ){ 133 fileflags = FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_RANDOM_ACCESS 134 | FILE_FLAG_DELETE_ON_CLOSE; 135 }else{ 136 fileflags = FILE_FLAG_RANDOM_ACCESS; 137 } 138 h = CreateFileA(zFilename, 139 GENERIC_READ | GENERIC_WRITE, 140 0, 141 NULL, 142 CREATE_ALWAYS, 143 fileflags, 144 NULL 145 ); 146 if( h==INVALID_HANDLE_VALUE ){ 147 return SQLITE_CANTOPEN; 148 } 149 id->h = h; 150 id->locktype = NO_LOCK; 151 id->sharedLockByte = 0; 152 id->isOpen = 1; 153 OpenCounter(+1); 154 TRACE3("OPEN EX %d \"%s\"\n", h, zFilename); 155 return SQLITE_OK; 156 } 157 158 /* 159 ** Attempt to open a new file for read-only access. 160 ** 161 ** On success, write the file handle into *id and return SQLITE_OK. 162 ** 163 ** On failure, return SQLITE_CANTOPEN. 164 */ 165 int sqlite3OsOpenReadOnly(const char *zFilename, OsFile *id){ 166 HANDLE h; 167 assert( !id->isOpen ); 168 h = CreateFileA(zFilename, 169 GENERIC_READ, 170 0, 171 NULL, 172 OPEN_EXISTING, 173 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, 174 NULL 175 ); 176 if( h==INVALID_HANDLE_VALUE ){ 177 return SQLITE_CANTOPEN; 178 } 179 id->h = h; 180 id->locktype = NO_LOCK; 181 id->sharedLockByte = 0; 182 id->isOpen = 1; 183 OpenCounter(+1); 184 TRACE3("OPEN RO %d \"%s\"\n", h, zFilename); 185 return SQLITE_OK; 186 } 187 188 /* 189 ** Attempt to open a file descriptor for the directory that contains a 190 ** file. This file descriptor can be used to fsync() the directory 191 ** in order to make sure the creation of a new file is actually written 192 ** to disk. 193 ** 194 ** This routine is only meaningful for Unix. It is a no-op under 195 ** windows since windows does not support hard links. 196 ** 197 ** On success, a handle for a previously open file is at *id is 198 ** updated with the new directory file descriptor and SQLITE_OK is 199 ** returned. 200 ** 201 ** On failure, the function returns SQLITE_CANTOPEN and leaves 202 ** *id unchanged. 203 */ 204 int sqlite3OsOpenDirectory( 205 const char *zDirname, 206 OsFile *id 207 ){ 208 return SQLITE_OK; 209 } 210 211 /* 212 ** If the following global variable points to a string which is the 213 ** name of a directory, then that directory will be used to store 214 ** temporary files. 215 */ 216 char *sqlite3_temp_directory = 0; 217 218 /* 219 ** Create a temporary file name in zBuf. zBuf must be big enough to 220 ** hold at least SQLITE_TEMPNAME_SIZE characters. 221 */ 222 int sqlite3OsTempFileName(char *zBuf){ 223 static char zChars[] = 224 "abcdefghijklmnopqrstuvwxyz" 225 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 226 "0123456789"; 227 int i, j; 228 char zTempPath[SQLITE_TEMPNAME_SIZE]; 229 if( sqlite3_temp_directory ){ 230 strncpy(zTempPath, sqlite3_temp_directory, SQLITE_TEMPNAME_SIZE-30); 231 zTempPath[SQLITE_TEMPNAME_SIZE-30] = 0; 232 }else{ 233 GetTempPathA(SQLITE_TEMPNAME_SIZE-30, zTempPath); 234 } 235 for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){} 236 zTempPath[i] = 0; 237 for(;;){ 238 sprintf(zBuf, "%s\\"TEMP_FILE_PREFIX, zTempPath); 239 j = strlen(zBuf); 240 sqlite3Randomness(15, &zBuf[j]); 241 for(i=0; i<15; i++, j++){ 242 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; 243 } 244 zBuf[j] = 0; 245 if( !sqlite3OsFileExists(zBuf) ) break; 246 } 247 TRACE2("TEMP FILENAME: %s\n", zBuf); 248 return SQLITE_OK; 249 } 250 251 /* 252 ** Close a file. 253 */ 254 int sqlite3OsClose(OsFile *id){ 255 if( id->isOpen ){ 256 TRACE2("CLOSE %d\n", id->h); 257 CloseHandle(id->h); 258 OpenCounter(-1); 259 id->isOpen = 0; 260 } 261 return SQLITE_OK; 262 } 263 264 /* 265 ** Read data from a file into a buffer. Return SQLITE_OK if all 266 ** bytes were read successfully and SQLITE_IOERR if anything goes 267 ** wrong. 268 */ 269 int sqlite3OsRead(OsFile *id, void *pBuf, int amt){ 270 DWORD got; 271 assert( id->isOpen ); 272 SimulateIOError(SQLITE_IOERR); 273 TRACE3("READ %d lock=%d\n", id->h, id->locktype); 274 if( !ReadFile(id->h, pBuf, amt, &got, 0) ){ 275 got = 0; 276 } 277 if( got==(DWORD)amt ){ 278 return SQLITE_OK; 279 }else{ 280 return SQLITE_IOERR; 281 } 282 } 283 284 /* 285 ** Write data from a buffer into a file. Return SQLITE_OK on success 286 ** or some other error code on failure. 287 */ 288 int sqlite3OsWrite(OsFile *id, const void *pBuf, int amt){ 289 int rc = 0; 290 DWORD wrote; 291 assert( id->isOpen ); 292 SimulateIOError(SQLITE_IOERR); 293 SimulateDiskfullError; 294 TRACE3("WRITE %d lock=%d\n", id->h, id->locktype); 295 assert( amt>0 ); 296 while( amt>0 && (rc = WriteFile(id->h, pBuf, amt, &wrote, 0))!=0 && wrote>0 ){ 297 amt -= wrote; 298 pBuf = &((char*)pBuf)[wrote]; 299 } 300 if( !rc || amt>(int)wrote ){ 301 return SQLITE_FULL; 302 } 303 return SQLITE_OK; 304 } 305 306 /* 307 ** Move the read/write pointer in a file. 308 */ 309 int sqlite3OsSeek(OsFile *id, i64 offset){ 310 LONG upperBits = offset>>32; 311 LONG lowerBits = offset & 0xffffffff; 312 DWORD rc; 313 assert( id->isOpen ); 314 SEEK(offset/1024 + 1); 315 rc = SetFilePointer(id->h, lowerBits, &upperBits, FILE_BEGIN); 316 TRACE3("SEEK %d %lld\n", id->h, offset); 317 return SQLITE_OK; 318 } 319 320 /* 321 ** Make sure all writes to a particular file are committed to disk. 322 */ 323 int sqlite3OsSync(OsFile *id){ 324 assert( id->isOpen ); 325 TRACE3("SYNC %d lock=%d\n", id->h, id->locktype); 326 if( FlushFileBuffers(id->h) ){ 327 return SQLITE_OK; 328 }else{ 329 return SQLITE_IOERR; 330 } 331 } 332 333 /* 334 ** Sync the directory zDirname. This is a no-op on operating systems other 335 ** than UNIX. 336 */ 337 int sqlite3OsSyncDirectory(const char *zDirname){ 338 SimulateIOError(SQLITE_IOERR); 339 return SQLITE_OK; 340 } 341 342 /* 343 ** Truncate an open file to a specified size 344 */ 345 int sqlite3OsTruncate(OsFile *id, i64 nByte){ 346 LONG upperBits = nByte>>32; 347 assert( id->isOpen ); 348 TRACE3("TRUNCATE %d %lld\n", id->h, nByte); 349 SimulateIOError(SQLITE_IOERR); 350 SetFilePointer(id->h, nByte, &upperBits, FILE_BEGIN); 351 SetEndOfFile(id->h); 352 return SQLITE_OK; 353 } 354 355 /* 356 ** Determine the current size of a file in bytes 357 */ 358 int sqlite3OsFileSize(OsFile *id, i64 *pSize){ 359 DWORD upperBits, lowerBits; 360 assert( id->isOpen ); 361 SimulateIOError(SQLITE_IOERR); 362 lowerBits = GetFileSize(id->h, &upperBits); 363 *pSize = (((i64)upperBits)<<32) + lowerBits; 364 return SQLITE_OK; 365 } 366 367 /* 368 ** Return true (non-zero) if we are running under WinNT, Win2K or WinXP. 369 ** Return false (zero) for Win95, Win98, or WinME. 370 ** 371 ** Here is an interesting observation: Win95, Win98, and WinME lack 372 ** the LockFileEx() API. But we can still statically link against that 373 ** API as long as we don't call it win running Win95/98/ME. A call to 374 ** this routine is used to determine if the host is Win95/98/ME or 375 ** WinNT/2K/XP so that we will know whether or not we can safely call 376 ** the LockFileEx() API. 377 */ 378 static int isNT(void){ 379 static int osType = 0; /* 0=unknown 1=win95 2=winNT */ 380 if( osType==0 ){ 381 OSVERSIONINFO sInfo; 382 sInfo.dwOSVersionInfoSize = sizeof(sInfo); 383 GetVersionEx(&sInfo); 384 osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1; 385 } 386 return osType==2; 387 } 388 389 /* 390 ** Acquire a reader lock. 391 ** Different API routines are called depending on whether or not this 392 ** is Win95 or WinNT. 393 */ 394 static int getReadLock(OsFile *id){ 395 int res; 396 if( isNT() ){ 397 OVERLAPPED ovlp; 398 ovlp.Offset = SHARED_FIRST; 399 ovlp.OffsetHigh = 0; 400 ovlp.hEvent = 0; 401 res = LockFileEx(id->h, LOCKFILE_FAIL_IMMEDIATELY, 0, SHARED_SIZE,0,&ovlp); 402 }else{ 403 int lk; 404 sqlite3Randomness(sizeof(lk), &lk); 405 id->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1); 406 res = LockFile(id->h, SHARED_FIRST+id->sharedLockByte, 0, 1, 0); 407 } 408 return res; 409 } 410 411 /* 412 ** Undo a readlock 413 */ 414 static int unlockReadLock(OsFile *id){ 415 int res; 416 if( isNT() ){ 417 res = UnlockFile(id->h, SHARED_FIRST, 0, SHARED_SIZE, 0); 418 }else{ 419 res = UnlockFile(id->h, SHARED_FIRST + id->sharedLockByte, 0, 1, 0); 420 } 421 return res; 422 } 423 424 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 425 /* 426 ** Check that a given pathname is a directory and is writable 427 ** 428 */ 429 int sqlite3OsIsDirWritable(char *zBuf){ 430 int fileAttr; 431 if(! zBuf ) return 0; 432 if(! isNT() && strlen(zBuf) > MAX_PATH ) return 0; 433 fileAttr = GetFileAttributesA(zBuf); 434 if( fileAttr == 0xffffffff ) return 0; 435 if( (fileAttr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY ){ 436 return 0; 437 } 438 return 1; 439 } 440 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ 441 442 /* 443 ** Lock the file with the lock specified by parameter locktype - one 444 ** of the following: 445 ** 446 ** (1) SHARED_LOCK 447 ** (2) RESERVED_LOCK 448 ** (3) PENDING_LOCK 449 ** (4) EXCLUSIVE_LOCK 450 ** 451 ** Sometimes when requesting one lock state, additional lock states 452 ** are inserted in between. The locking might fail on one of the later 453 ** transitions leaving the lock state different from what it started but 454 ** still short of its goal. The following chart shows the allowed 455 ** transitions and the inserted intermediate states: 456 ** 457 ** UNLOCKED -> SHARED 458 ** SHARED -> RESERVED 459 ** SHARED -> (PENDING) -> EXCLUSIVE 460 ** RESERVED -> (PENDING) -> EXCLUSIVE 461 ** PENDING -> EXCLUSIVE 462 ** 463 ** This routine will only increase a lock. The sqlite3OsUnlock() routine 464 ** erases all locks at once and returns us immediately to locking level 0. 465 ** It is not possible to lower the locking level one step at a time. You 466 ** must go straight to locking level 0. 467 */ 468 int sqlite3OsLock(OsFile *id, int locktype){ 469 int rc = SQLITE_OK; /* Return code from subroutines */ 470 int res = 1; /* Result of a windows lock call */ 471 int newLocktype; /* Set id->locktype to this value before exiting */ 472 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */ 473 474 assert( id->isOpen ); 475 TRACE5("LOCK %d %d was %d(%d)\n", 476 id->h, locktype, id->locktype, id->sharedLockByte); 477 478 /* If there is already a lock of this type or more restrictive on the 479 ** OsFile, do nothing. Don't use the end_lock: exit path, as 480 ** sqlite3OsEnterMutex() hasn't been called yet. 481 */ 482 if( id->locktype>=locktype ){ 483 return SQLITE_OK; 484 } 485 486 /* Make sure the locking sequence is correct 487 */ 488 assert( id->locktype!=NO_LOCK || locktype==SHARED_LOCK ); 489 assert( locktype!=PENDING_LOCK ); 490 assert( locktype!=RESERVED_LOCK || id->locktype==SHARED_LOCK ); 491 492 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or 493 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of 494 ** the PENDING_LOCK byte is temporary. 495 */ 496 newLocktype = id->locktype; 497 if( id->locktype==NO_LOCK 498 || (locktype==EXCLUSIVE_LOCK && id->locktype==RESERVED_LOCK) 499 ){ 500 int cnt = 3; 501 while( cnt-->0 && (res = LockFile(id->h, PENDING_BYTE, 0, 1, 0))==0 ){ 502 /* Try 3 times to get the pending lock. The pending lock might be 503 ** held by another reader process who will release it momentarily. 504 */ 505 TRACE2("could not get a PENDING lock. cnt=%d\n", cnt); 506 Sleep(1); 507 } 508 gotPendingLock = res; 509 } 510 511 /* Acquire a shared lock 512 */ 513 if( locktype==SHARED_LOCK && res ){ 514 assert( id->locktype==NO_LOCK ); 515 res = getReadLock(id); 516 if( res ){ 517 newLocktype = SHARED_LOCK; 518 } 519 } 520 521 /* Acquire a RESERVED lock 522 */ 523 if( locktype==RESERVED_LOCK && res ){ 524 assert( id->locktype==SHARED_LOCK ); 525 res = LockFile(id->h, RESERVED_BYTE, 0, 1, 0); 526 if( res ){ 527 newLocktype = RESERVED_LOCK; 528 } 529 } 530 531 /* Acquire a PENDING lock 532 */ 533 if( locktype==EXCLUSIVE_LOCK && res ){ 534 newLocktype = PENDING_LOCK; 535 gotPendingLock = 0; 536 } 537 538 /* Acquire an EXCLUSIVE lock 539 */ 540 if( locktype==EXCLUSIVE_LOCK && res ){ 541 assert( id->locktype>=SHARED_LOCK ); 542 res = unlockReadLock(id); 543 TRACE2("unreadlock = %d\n", res); 544 res = LockFile(id->h, SHARED_FIRST, 0, SHARED_SIZE, 0); 545 if( res ){ 546 newLocktype = EXCLUSIVE_LOCK; 547 }else{ 548 TRACE2("error-code = %d\n", GetLastError()); 549 } 550 } 551 552 /* If we are holding a PENDING lock that ought to be released, then 553 ** release it now. 554 */ 555 if( gotPendingLock && locktype==SHARED_LOCK ){ 556 UnlockFile(id->h, PENDING_BYTE, 0, 1, 0); 557 } 558 559 /* Update the state of the lock has held in the file descriptor then 560 ** return the appropriate result code. 561 */ 562 if( res ){ 563 rc = SQLITE_OK; 564 }else{ 565 TRACE4("LOCK FAILED %d trying for %d but got %d\n", id->h, 566 locktype, newLocktype); 567 rc = SQLITE_BUSY; 568 } 569 id->locktype = newLocktype; 570 return rc; 571 } 572 573 /* 574 ** This routine checks if there is a RESERVED lock held on the specified 575 ** file by this or any other process. If such a lock is held, return 576 ** non-zero, otherwise zero. 577 */ 578 int sqlite3OsCheckReservedLock(OsFile *id){ 579 int rc; 580 assert( id->isOpen ); 581 if( id->locktype>=RESERVED_LOCK ){ 582 rc = 1; 583 TRACE3("TEST WR-LOCK %d %d (local)\n", id->h, rc); 584 }else{ 585 rc = LockFile(id->h, RESERVED_BYTE, 0, 1, 0); 586 if( rc ){ 587 UnlockFile(id->h, RESERVED_BYTE, 0, 1, 0); 588 } 589 rc = !rc; 590 TRACE3("TEST WR-LOCK %d %d (remote)\n", id->h, rc); 591 } 592 return rc; 593 } 594 595 /* 596 ** Lower the locking level on file descriptor id to locktype. locktype 597 ** must be either NO_LOCK or SHARED_LOCK. 598 ** 599 ** If the locking level of the file descriptor is already at or below 600 ** the requested locking level, this routine is a no-op. 601 ** 602 ** It is not possible for this routine to fail if the second argument 603 ** is NO_LOCK. If the second argument is SHARED_LOCK then this routine 604 ** might return SQLITE_IOERR; 605 */ 606 int sqlite3OsUnlock(OsFile *id, int locktype){ 607 int type; 608 int rc = SQLITE_OK; 609 assert( id->isOpen ); 610 assert( locktype<=SHARED_LOCK ); 611 TRACE5("UNLOCK %d to %d was %d(%d)\n", id->h, locktype, 612 id->locktype, id->sharedLockByte); 613 type = id->locktype; 614 if( type>=EXCLUSIVE_LOCK ){ 615 UnlockFile(id->h, SHARED_FIRST, 0, SHARED_SIZE, 0); 616 if( locktype==SHARED_LOCK && !getReadLock(id) ){ 617 /* This should never happen. We should always be able to 618 ** reacquire the read lock */ 619 rc = SQLITE_IOERR; 620 } 621 } 622 if( type>=RESERVED_LOCK ){ 623 UnlockFile(id->h, RESERVED_BYTE, 0, 1, 0); 624 } 625 if( locktype==NO_LOCK && type>=SHARED_LOCK ){ 626 unlockReadLock(id); 627 } 628 if( type>=PENDING_LOCK ){ 629 UnlockFile(id->h, PENDING_BYTE, 0, 1, 0); 630 } 631 id->locktype = locktype; 632 return rc; 633 } 634 635 /* 636 ** Turn a relative pathname into a full pathname. Return a pointer 637 ** to the full pathname stored in space obtained from sqliteMalloc(). 638 ** The calling function is responsible for freeing this space once it 639 ** is no longer needed. 640 */ 641 char *sqlite3OsFullPathname(const char *zRelative){ 642 char *zNotUsed; 643 char *zFull; 644 int nByte; 645 #ifdef __CYGWIN__ 646 nByte = strlen(zRelative) + MAX_PATH + 1001; 647 zFull = sqliteMalloc( nByte ); 648 if( zFull==0 ) return 0; 649 if( cygwin_conv_to_full_win32_path(zRelative, zFull) ) return 0; 650 #else 651 nByte = GetFullPathNameA(zRelative, 0, 0, &zNotUsed) + 1; 652 zFull = sqliteMalloc( nByte ); 653 if( zFull==0 ) return 0; 654 GetFullPathNameA(zRelative, nByte, zFull, &zNotUsed); 655 #endif 656 return zFull; 657 } 658 659 #endif /* SQLITE_OMIT_DISKIO */ 660 /*************************************************************************** 661 ** Everything above deals with file I/O. Everything that follows deals 662 ** with other miscellanous aspects of the operating system interface 663 ****************************************************************************/ 664 665 /* 666 ** Get information to seed the random number generator. The seed 667 ** is written into the buffer zBuf[256]. The calling function must 668 ** supply a sufficiently large buffer. 669 */ 670 int sqlite3OsRandomSeed(char *zBuf){ 671 /* We have to initialize zBuf to prevent valgrind from reporting 672 ** errors. The reports issued by valgrind are incorrect - we would 673 ** prefer that the randomness be increased by making use of the 674 ** uninitialized space in zBuf - but valgrind errors tend to worry 675 ** some users. Rather than argue, it seems easier just to initialize 676 ** the whole array and silence valgrind, even if that means less randomness 677 ** in the random seed. 678 ** 679 ** When testing, initializing zBuf[] to zero is all we do. That means 680 ** that we always use the same random number sequence.* This makes the 681 ** tests repeatable. 682 */ 683 memset(zBuf, 0, 256); 684 GetSystemTime((LPSYSTEMTIME)zBuf); 685 return SQLITE_OK; 686 } 687 688 /* 689 ** Sleep for a little while. Return the amount of time slept. 690 */ 691 int sqlite3OsSleep(int ms){ 692 Sleep(ms); 693 return ms; 694 } 695 696 /* 697 ** Static variables used for thread synchronization 698 */ 699 static int inMutex = 0; 700 #ifdef SQLITE_W32_THREADS 701 static CRITICAL_SECTION cs; 702 #endif 703 704 /* 705 ** The following pair of routine implement mutual exclusion for 706 ** multi-threaded processes. Only a single thread is allowed to 707 ** executed code that is surrounded by EnterMutex() and LeaveMutex(). 708 ** 709 ** SQLite uses only a single Mutex. There is not much critical 710 ** code and what little there is executes quickly and without blocking. 711 */ 712 void sqlite3OsEnterMutex(){ 713 #ifdef SQLITE_W32_THREADS 714 static int isInit = 0; 715 while( !isInit ){ 716 static long lock = 0; 717 if( InterlockedIncrement(&lock)==1 ){ 718 InitializeCriticalSection(&cs); 719 isInit = 1; 720 }else{ 721 Sleep(1); 722 } 723 } 724 EnterCriticalSection(&cs); 725 #endif 726 assert( !inMutex ); 727 inMutex = 1; 728 } 729 void sqlite3OsLeaveMutex(){ 730 assert( inMutex ); 731 inMutex = 0; 732 #ifdef SQLITE_W32_THREADS 733 LeaveCriticalSection(&cs); 734 #endif 735 } 736 737 /* 738 ** The following variable, if set to a non-zero value, becomes the result 739 ** returned from sqlite3OsCurrentTime(). This is used for testing. 740 */ 741 #ifdef SQLITE_TEST 742 int sqlite3_current_time = 0; 743 #endif 744 745 /* 746 ** Find the current time (in Universal Coordinated Time). Write the 747 ** current time and date as a Julian Day number into *prNow and 748 ** return 0. Return 1 if the time and date cannot be found. 749 */ 750 int sqlite3OsCurrentTime(double *prNow){ 751 FILETIME ft; 752 /* FILETIME structure is a 64-bit value representing the number of 753 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). 754 */ 755 double now; 756 GetSystemTimeAsFileTime( &ft ); 757 now = ((double)ft.dwHighDateTime) * 4294967296.0; 758 *prNow = (now + ft.dwLowDateTime)/864000000000.0 + 2305813.5; 759 #ifdef SQLITE_TEST 760 if( sqlite3_current_time ){ 761 *prNow = sqlite3_current_time/86400.0 + 2440587.5; 762 } 763 #endif 764 return 0; 765 } 766 767 #endif /* OS_WIN */ 768