From: mistachkin Date: Mon, 21 Nov 2011 00:54:37 +0000 (+0000) Subject: Cleanup the semantics surrounding use of the GetLastError function on Windows. X-Git-Tag: version-3.7.10~19^2~81^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d1ef9b6da105a27d42a500beb3a44484bb58a85d;p=thirdparty%2Fsqlite.git Cleanup the semantics surrounding use of the GetLastError function on Windows. FossilOrigin-Name: 7e657bbb800107c992a6ee7a3b35bc0a073bf3e4 --- diff --git a/manifest b/manifest index bf28f8bbf8..d104ec1889 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Change\sthe\smultiplexor\sto\suse\sa\s3-digit\ssuffix. -D 2011-11-18T13:10:51.946 +C Cleanup\sthe\ssemantics\ssurrounding\suse\sof\sthe\sGetLastError\sfunction\son\sWindows. +D 2011-11-21T00:54:37.897 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 5b4a3e12a850b021547e43daf886b25133b44c07 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -167,7 +167,7 @@ F src/os.h 9dbed8c2b9c1f2f2ebabc09e49829d4777c26bf9 F src/os_common.h 92815ed65f805560b66166e3583470ff94478f04 F src/os_os2.c 4a75888ba3dfc820ad5e8177025972d74d7f2440 F src/os_unix.c 4fbb91726165e105c1679a2660f49a3f4c376e4f -F src/os_win.c a22b88d2c088c09a678a471abafa8d60dbf56803 +F src/os_win.c 6efe66a38215c38eaa7603ee5f76848159f8669d F src/pager.c d981f3bfcc0e4460537d983899620700ccf8f539 F src/pager.h 5cd760857707529b403837d813d86b68938d6183 F src/parse.y 12b7ebd61ea54f0e1b1083ff69cc2c8ce9353d58 @@ -976,7 +976,10 @@ F tool/tostr.awk e75472c2f98dd76e06b8c9c1367f4ab07e122d06 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f F tool/warnings-clang.sh 9f406d66e750e8ac031c63a9ef3248aaa347ef2a F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381 -P 1c45b2a0c055f6fc5da9d00ae2e9171099d904d4 06e0cdaf9112f722c23692e25c5b1f99b61c2d78 -R d62e550a3406a1fdeec14ac20b144781 -U drh -Z d184ad6817e342a5dbefa36cc13c5962 +P 0b7edc44757660c8a5ae3b91cbcc3e6afd419b28 +R 99aff9fc0229e676974ede97756eda75 +T *branch * winGetLastError +T *sym-winGetLastError * +T -sym-trunk * +U mistachkin +Z 7ff767419ae0d15068d14c36ad0bea1d diff --git a/manifest.uuid b/manifest.uuid index deaed3cb30..5343d856e0 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -0b7edc44757660c8a5ae3b91cbcc3e6afd419b28 \ No newline at end of file +7e657bbb800107c992a6ee7a3b35bc0a073bf3e4 \ No newline at end of file diff --git a/src/os_win.c b/src/os_win.c index 62868ba504..7e89a8cc02 100644 --- a/src/os_win.c +++ b/src/os_win.c @@ -1170,12 +1170,14 @@ static int win32IoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY; ** to see if it should be retried. Return TRUE to retry. Return FALSE ** to give up with an error. */ -static int retryIoerr(int *pnRetry){ - DWORD e; +static int retryIoerr(int *pnRetry, DWORD *pError){ + DWORD e = osGetLastError(); if( *pnRetry>=win32IoerrRetry ){ + if( pError ){ + *pError = e; + } return 0; } - e = osGetLastError(); if( e==ERROR_ACCESS_DENIED || e==ERROR_LOCK_VIOLATION || e==ERROR_SHARING_VIOLATION ){ @@ -1183,6 +1185,9 @@ static int retryIoerr(int *pnRetry){ ++*pnRetry; return 1; } + if( pError ){ + *pError = e; + } return 0; } @@ -1539,6 +1544,7 @@ static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){ LONG upperBits; /* Most sig. 32 bits of new offset */ LONG lowerBits; /* Least sig. 32 bits of new offset */ DWORD dwRet; /* Value returned by SetFilePointer() */ + DWORD lastErrno; /* Value returned by GetLastError() */ upperBits = (LONG)((iOffset>>32) & 0x7fffffff); lowerBits = (LONG)(iOffset & 0xffffffff); @@ -1551,8 +1557,10 @@ static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){ ** GetLastError(). */ dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN); - if( (dwRet==INVALID_SET_FILE_POINTER && osGetLastError()!=NO_ERROR) ){ - pFile->lastErrno = osGetLastError(); + + if( (dwRet==INVALID_SET_FILE_POINTER + && ((lastErrno = osGetLastError())!=NO_ERROR)) ){ + pFile->lastErrno = lastErrno; winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno, "seekWinFile", pFile->zPath); return 1; @@ -1628,8 +1636,9 @@ static int winRead( return SQLITE_FULL; } while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){ - if( retryIoerr(&nRetry) ) continue; - pFile->lastErrno = osGetLastError(); + DWORD lastErrno; + if( retryIoerr(&nRetry, &lastErrno) ) continue; + pFile->lastErrno = lastErrno; return winLogError(SQLITE_IOERR_READ, pFile->lastErrno, "winRead", pFile->zPath); } @@ -1669,10 +1678,11 @@ static int winWrite( u8 *aRem = (u8 *)pBuf; /* Data yet to be written */ int nRem = amt; /* Number of bytes yet to be written */ DWORD nWrite; /* Bytes written by each WriteFile() call */ + DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */ while( nRem>0 ){ if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){ - if( retryIoerr(&nRetry) ) continue; + if( retryIoerr(&nRetry, &lastErrno) ) continue; break; } if( nWrite<=0 ) break; @@ -1680,7 +1690,7 @@ static int winWrite( nRem -= nWrite; } if( nRem>0 ){ - pFile->lastErrno = osGetLastError(); + pFile->lastErrno = lastErrno; rc = 1; } } @@ -1810,15 +1820,15 @@ static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){ DWORD upperBits; DWORD lowerBits; winFile *pFile = (winFile*)id; - DWORD error; + DWORD lastErrno; assert( id!=0 ); SimulateIOError(return SQLITE_IOERR_FSTAT); lowerBits = osGetFileSize(pFile->h, &upperBits); if( (lowerBits == INVALID_FILE_SIZE) - && ((error = osGetLastError()) != NO_ERROR) ) + && ((lastErrno = osGetLastError())!=NO_ERROR) ) { - pFile->lastErrno = error; + pFile->lastErrno = lastErrno; return winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno, "winFileSize", pFile->zPath); } @@ -1869,6 +1879,7 @@ static int getReadLock(winFile *pFile){ */ static int unlockReadLock(winFile *pFile){ int res; + DWORD lastErrno; if( isNT() ){ res = osUnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. @@ -1878,8 +1889,8 @@ static int unlockReadLock(winFile *pFile){ res = osUnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0); #endif } - if( res==0 && osGetLastError()!=ERROR_NOT_LOCKED ){ - pFile->lastErrno = osGetLastError(); + if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){ + pFile->lastErrno = lastErrno; winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno, "unlockReadLock", pFile->zPath); } @@ -1918,7 +1929,7 @@ static int winLock(sqlite3_file *id, int locktype){ int newLocktype; /* Set pFile->locktype to this value before exiting */ int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */ winFile *pFile = (winFile*)id; - DWORD error = NO_ERROR; + DWORD lastErrno = NO_ERROR; assert( id!=0 ); OSTRACE(("LOCK %d %d was %d(%d)\n", @@ -1960,7 +1971,7 @@ static int winLock(sqlite3_file *id, int locktype){ } gotPendingLock = res; if( !res ){ - error = osGetLastError(); + lastErrno = osGetLastError(); } } @@ -1972,7 +1983,7 @@ static int winLock(sqlite3_file *id, int locktype){ if( res ){ newLocktype = SHARED_LOCK; }else{ - error = osGetLastError(); + lastErrno = osGetLastError(); } } @@ -1984,7 +1995,7 @@ static int winLock(sqlite3_file *id, int locktype){ if( res ){ newLocktype = RESERVED_LOCK; }else{ - error = osGetLastError(); + lastErrno = osGetLastError(); } } @@ -2005,8 +2016,8 @@ static int winLock(sqlite3_file *id, int locktype){ if( res ){ newLocktype = EXCLUSIVE_LOCK; }else{ - error = osGetLastError(); - OSTRACE(("error-code = %d\n", error)); + lastErrno = osGetLastError(); + OSTRACE(("error-code = %d\n", lastErrno)); getReadLock(pFile); } } @@ -2026,7 +2037,7 @@ static int winLock(sqlite3_file *id, int locktype){ }else{ OSTRACE(("LOCK FAILED %d trying for %d but got %d\n", pFile->h, locktype, newLocktype)); - pFile->lastErrno = error; + pFile->lastErrno = lastErrno; rc = SQLITE_BUSY; } pFile->locktype = (u8)newLocktype; @@ -2964,6 +2975,7 @@ static int winOpen( int *pOutFlags /* Status return flags */ ){ HANDLE h; + DWORD lastErrno; DWORD dwDesiredAccess; DWORD dwShareMode; DWORD dwCreationDisposition; @@ -3100,7 +3112,7 @@ static int winOpen( dwCreationDisposition, dwFlagsAndAttributes, NULL))==INVALID_HANDLE_VALUE && - retryIoerr(&cnt) ){} + retryIoerr(&cnt, &lastErrno) ){} /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. ** Since the ANSI version of these Windows API do not exist for WINCE, ** it's important to not reference them for WINCE builds. @@ -3113,7 +3125,7 @@ static int winOpen( dwCreationDisposition, dwFlagsAndAttributes, NULL))==INVALID_HANDLE_VALUE && - retryIoerr(&cnt) ){} + retryIoerr(&cnt, &lastErrno) ){} #endif } @@ -3124,7 +3136,7 @@ static int winOpen( h==INVALID_HANDLE_VALUE ? "failed" : "ok")); if( h==INVALID_HANDLE_VALUE ){ - pFile->lastErrno = osGetLastError(); + pFile->lastErrno = lastErrno; winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name); sqlite3_free(zConverted); if( isReadWrite && !isExclusive ){ @@ -3191,6 +3203,7 @@ static int winDelete( ){ int cnt = 0; int rc; + DWORD lastErrno; void *zConverted; UNUSED_PARAMETER(pVfs); UNUSED_PARAMETER(syncDir); @@ -3203,7 +3216,7 @@ static int winDelete( if( isNT() ){ rc = 1; while( osGetFileAttributesW(zConverted)!=INVALID_FILE_ATTRIBUTES && - (rc = osDeleteFileW(zConverted))==0 && retryIoerr(&cnt) ){} + (rc = osDeleteFileW(zConverted))==0 && retryIoerr(&cnt, &lastErrno) ){} rc = rc ? SQLITE_OK : SQLITE_ERROR; /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. ** Since the ANSI version of these Windows API do not exist for WINCE, @@ -3213,12 +3226,12 @@ static int winDelete( }else{ rc = 1; while( osGetFileAttributesA(zConverted)!=INVALID_FILE_ATTRIBUTES && - (rc = osDeleteFileA(zConverted))==0 && retryIoerr(&cnt) ){} + (rc = osDeleteFileA(zConverted))==0 && retryIoerr(&cnt, &lastErrno) ){} rc = rc ? SQLITE_OK : SQLITE_ERROR; #endif } if( rc ){ - rc = winLogError(SQLITE_IOERR_DELETE, osGetLastError(), + rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename); }else{ logIoerr(cnt); @@ -3239,6 +3252,7 @@ static int winAccess( ){ DWORD attr; int rc = 0; + DWORD lastErrno; void *zConverted; UNUSED_PARAMETER(pVfs); @@ -3253,7 +3267,7 @@ static int winAccess( memset(&sAttrData, 0, sizeof(sAttrData)); while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted, GetFileExInfoStandard, - &sAttrData)) && retryIoerr(&cnt) ){} + &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){} if( rc ){ /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file ** as if it does not exist. @@ -3266,7 +3280,6 @@ static int winAccess( attr = sAttrData.dwFileAttributes; } }else{ - DWORD lastErrno = osGetLastError(); logIoerr(cnt); if( lastErrno!=ERROR_FILE_NOT_FOUND ){ winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", zFilename);