📄 os_os2.c
字号:
return SQLITE_CANTOPEN; } } if( pOutFlags ){ *pOutFlags = flags & SQLITE_OPEN_READWRITE ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY; } pFile->pMethod = &os2IoMethod; pFile->h = h; OpenCounter(+1); OSTRACE3( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags ); return SQLITE_OK;}/*** Delete the named file.*/int os2Delete( sqlite3_vfs *pVfs, /* Not used on os2 */ const char *zFilename, /* Name of file to delete */ int syncDir /* Not used on os2 */){ APIRET rc = NO_ERROR; SimulateIOError(return SQLITE_IOERR_DELETE); rc = DosDelete( (PSZ)zFilename ); OSTRACE2( "DELETE \"%s\"\n", zFilename ); return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;}/*** Check the existance and status of a file.*/static int os2Access( sqlite3_vfs *pVfs, /* Not used on os2 */ const char *zFilename, /* Name of file to check */ int flags /* Type of test to make on this file */){ FILESTATUS3 fsts3ConfigInfo; APIRET rc = NO_ERROR; memset(&fsts3ConfigInfo, 0, sizeof(fsts3ConfigInfo)); rc = DosQueryPathInfo( (PSZ)zFilename, FIL_STANDARD, &fsts3ConfigInfo, sizeof(FILESTATUS3) ); OSTRACE4( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n", fsts3ConfigInfo.attrFile, flags, rc ); switch( flags ){ case SQLITE_ACCESS_READ: case SQLITE_ACCESS_EXISTS: rc = (rc == NO_ERROR); OSTRACE3( "ACCESS %s access of read and exists rc=%d\n", zFilename, rc ); break; case SQLITE_ACCESS_READWRITE: rc = (fsts3ConfigInfo.attrFile & FILE_READONLY) == 0; OSTRACE3( "ACCESS %s access of read/write rc=%d\n", zFilename, rc ); break; default: assert( !"Invalid flags argument" ); } return rc;}/*** Create a temporary file name in zBuf. zBuf must be big enough to** hold at pVfs->mxPathname characters.*/static int os2GetTempname( sqlite3_vfs *pVfs, int nBuf, char *zBuf ){ static const unsigned char zChars[] = "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789"; int i, j; PSZ zTempPath = ""; if( DosScanEnv( (PSZ)"TEMP", &zTempPath ) ){ if( DosScanEnv( (PSZ)"TMP", &zTempPath ) ){ if( DosScanEnv( (PSZ)"TMPDIR", &zTempPath ) ){ ULONG ulDriveNum = 0, ulDriveMap = 0; DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap ); sprintf( (char*)zTempPath, "%c:", (char)( 'A' + ulDriveNum - 1 ) ); } } } /* strip off a trailing slashes or backslashes, otherwise we would get * * multiple (back)slashes which causes DosOpen() to fail */ j = strlen(zTempPath); while( j > 0 && ( zTempPath[j-1] == '\\' || zTempPath[j-1] == '/' ) ){ j--; } zTempPath[j] = '\0'; assert( nBuf>=pVfs->mxPathname ); sqlite3_snprintf( pVfs->mxPathname-30, zBuf, "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath ); j = strlen( zBuf ); sqlite3Randomness( 20, &zBuf[j] ); for( i = 0; i < 20; i++, j++ ){ zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; } zBuf[j] = 0; OSTRACE2( "TEMP FILENAME: %s\n", zBuf ); return SQLITE_OK;}/*** Turn a relative pathname into a full pathname. Write the full** pathname into zFull[]. zFull[] will be at least pVfs->mxPathname** bytes in size.*/static int os2FullPathname( sqlite3_vfs *pVfs, /* Pointer to vfs object */ const char *zRelative, /* Possibly relative input path */ int nFull, /* Size of output buffer in bytes */ char *zFull /* Output buffer */){ if( strchr(zRelative, ':') ){ sqlite3SetString( &zFull, zRelative, (char*)0 ); }else{ ULONG ulDriveNum = 0; ULONG ulDriveMap = 0; ULONG cbzBufLen = SQLITE_TEMPNAME_SIZE; char zDrive[2]; char *zBuff = (char*)malloc( cbzBufLen ); if( zBuff == 0 ){ return SQLITE_NOMEM; } DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap ); if( DosQueryCurrentDir( ulDriveNum, (PBYTE)zBuff, &cbzBufLen ) == NO_ERROR ){ sprintf( zDrive, "%c", (char)('A' + ulDriveNum - 1) ); sqlite3SetString( &zFull, zDrive, ":\\", zBuff, "\\", zRelative, (char*)0 ); } free( zBuff ); } return SQLITE_OK;}#ifndef SQLITE_OMIT_LOAD_EXTENSION/*** Interfaces for opening a shared library, finding entry points** within the shared library, and closing the shared library.*//*** Interfaces for opening a shared library, finding entry points** within the shared library, and closing the shared library.*/static void *os2DlOpen(sqlite3_vfs *pVfs, const char *zFilename){ UCHAR loadErr[256]; HMODULE hmod; APIRET rc; rc = DosLoadModule((PSZ)loadErr, sizeof(loadErr), zFilename, &hmod); return rc != NO_ERROR ? 0 : (void*)hmod;}/*** A no-op since the error code is returned on the DosLoadModule call.** os2Dlopen returns zero if DosLoadModule is not successful.*/static void os2DlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){/* no-op */}void *os2DlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){ PFN pfn; APIRET rc; rc = DosQueryProcAddr((HMODULE)pHandle, 0L, zSymbol, &pfn); if( rc != NO_ERROR ){ /* if the symbol itself was not found, search again for the same * symbol with an extra underscore, that might be needed depending * on the calling convention */ char _zSymbol[256] = "_"; strncat(_zSymbol, zSymbol, 255); rc = DosQueryProcAddr((HMODULE)pHandle, 0L, _zSymbol, &pfn); } return rc != NO_ERROR ? 0 : (void*)pfn;}void os2DlClose(sqlite3_vfs *pVfs, void *pHandle){ DosFreeModule((HMODULE)pHandle);}#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ #define os2DlOpen 0 #define os2DlError 0 #define os2DlSym 0 #define os2DlClose 0#endif/*** Write up to nBuf bytes of randomness into zBuf.*/static int os2Randomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf ){ ULONG sizeofULong = sizeof(ULONG); int n = 0; if( sizeof(DATETIME) <= nBuf - n ){ DATETIME x; DosGetDateTime(&x); memcpy(&zBuf[n], &x, sizeof(x)); n += sizeof(x); } if( sizeofULong <= nBuf - n ){ PPIB ppib; DosGetInfoBlocks(NULL, &ppib); memcpy(&zBuf[n], &ppib->pib_ulpid, sizeofULong); n += sizeofULong; } if( sizeofULong <= nBuf - n ){ PTIB ptib; DosGetInfoBlocks(&ptib, NULL); memcpy(&zBuf[n], &ptib->tib_ptib2->tib2_ultid, sizeofULong); n += sizeofULong; } /* if we still haven't filled the buffer yet the following will */ /* grab everything once instead of making several calls for a single item */ if( sizeofULong <= nBuf - n ){ ULONG ulSysInfo[QSV_MAX]; DosQuerySysInfo(1L, QSV_MAX, ulSysInfo, sizeofULong * QSV_MAX); memcpy(&zBuf[n], &ulSysInfo[QSV_MS_COUNT - 1], sizeofULong); n += sizeofULong; if( sizeofULong <= nBuf - n ){ memcpy(&zBuf[n], &ulSysInfo[QSV_TIMER_INTERVAL - 1], sizeofULong); n += sizeofULong; } if( sizeofULong <= nBuf - n ){ memcpy(&zBuf[n], &ulSysInfo[QSV_TIME_LOW - 1], sizeofULong); n += sizeofULong; } if( sizeofULong <= nBuf - n ){ memcpy(&zBuf[n], &ulSysInfo[QSV_TIME_HIGH - 1], sizeofULong); n += sizeofULong; } if( sizeofULong <= nBuf - n ){ memcpy(&zBuf[n], &ulSysInfo[QSV_TOTAVAILMEM - 1], sizeofULong); n += sizeofULong; } } return n;}/*** Sleep for a little while. Return the amount of time slept.** The argument is the number of microseconds we want to sleep.** The return value is the number of microseconds of sleep actually** requested from the underlying operating system, a number which** might be greater than or equal to the argument, but not less** than the argument.*/static int os2Sleep( sqlite3_vfs *pVfs, int microsec ){ DosSleep( (microsec/1000) ); return microsec;}/*** The following variable, if set to a non-zero value, becomes the result** returned from sqlite3OsCurrentTime(). This is used for testing.*/#ifdef SQLITE_TESTint sqlite3_current_time = 0;#endif/*** Find the current time (in Universal Coordinated Time). Write the** current time and date as a Julian Day number into *prNow and** return 0. Return 1 if the time and date cannot be found.*/int os2CurrentTime( sqlite3_vfs *pVfs, double *prNow ){ double now; SHORT minute; /* needs to be able to cope with negative timezone offset */ USHORT second, hour, day, month, year; DATETIME dt; DosGetDateTime( &dt ); second = (USHORT)dt.seconds; minute = (SHORT)dt.minutes + dt.timezone; hour = (USHORT)dt.hours; day = (USHORT)dt.day; month = (USHORT)dt.month; year = (USHORT)dt.year; /* Calculations from http://www.astro.keele.ac.uk/~rno/Astronomy/hjd.html http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c */ /* Calculate the Julian days */ now = day - 32076 + 1461*(year + 4800 + (month - 14)/12)/4 + 367*(month - 2 - (month - 14)/12*12)/12 - 3*((year + 4900 + (month - 14)/12)/100)/4; /* Add the fractional hours, mins and seconds */ now += (hour + 12.0)/24.0; now += minute/1440.0; now += second/86400.0; *prNow = now;#ifdef SQLITE_TEST if( sqlite3_current_time ){ *prNow = sqlite3_current_time/86400.0 + 2440587.5; }#endif return 0;}/*** Return a pointer to the sqlite3DefaultVfs structure. We use** a function rather than give the structure global scope because** some compilers (MSVC) do not allow forward declarations of** initialized structures.*/sqlite3_vfs *sqlite3OsDefaultVfs(void){ static sqlite3_vfs os2Vfs = { 1, /* iVersion */ sizeof(os2File), /* szOsFile */ CCHMAXPATH, /* mxPathname */ 0, /* pNext */ "os2", /* zName */ 0, /* pAppData */ os2Open, /* xOpen */ os2Delete, /* xDelete */ os2Access, /* xAccess */ os2GetTempname, /* xGetTempname */ os2FullPathname, /* xFullPathname */ os2DlOpen, /* xDlOpen */ os2DlError, /* xDlError */ os2DlSym, /* xDlSym */ os2DlClose, /* xDlClose */ os2Randomness, /* xRandomness */ os2Sleep, /* xSleep */ os2CurrentTime /* xCurrentTime */ }; return &os2Vfs;}#endif /* OS_OS2 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -