📄 test_onefile.c
字号:
int iBuf = 0; int ii = iOfst; while( iRem>0 && rc==SQLITE_OK ){ int iRealOff = pReal->nBlob - BLOCKSIZE*((ii/BLOCKSIZE)+1) + ii%BLOCKSIZE; int iRealAmt = MIN(iRem, BLOCKSIZE - (iRealOff%BLOCKSIZE)); rc = pF->pMethods->xRead(pF, &((char *)zBuf)[iBuf], iRealAmt, iRealOff); ii += iRealAmt; iBuf += iRealAmt; iRem -= iRealAmt; } } return rc;}/*** Write data to an fs-file.*/static int fsWrite( sqlite3_file *pFile, const void *zBuf, int iAmt, sqlite_int64 iOfst){ int rc = SQLITE_OK; fs_file *p = (fs_file *)pFile; fs_real_file *pReal = p->pReal; sqlite3_file *pF = pReal->pFile; if( p->eType==DATABASE_FILE ){ if( (iAmt+iOfst+BLOCKSIZE)>(pReal->nBlob-pReal->nJournal) ){ rc = SQLITE_FULL; }else{ rc = pF->pMethods->xWrite(pF, zBuf, iAmt, iOfst+BLOCKSIZE); if( rc==SQLITE_OK ){ pReal->nDatabase = MAX(pReal->nDatabase, iAmt+iOfst); } } }else{ /* Journal file. */ int iRem = iAmt; int iBuf = 0; int ii = iOfst; while( iRem>0 && rc==SQLITE_OK ){ int iRealOff = pReal->nBlob - BLOCKSIZE*((ii/BLOCKSIZE)+1) + ii%BLOCKSIZE; int iRealAmt = MIN(iRem, BLOCKSIZE - (iRealOff%BLOCKSIZE)); if( iRealOff<(pReal->nDatabase+BLOCKSIZE) ){ rc = SQLITE_FULL; }else{ rc = pF->pMethods->xWrite(pF, &((char *)zBuf)[iBuf], iRealAmt,iRealOff); ii += iRealAmt; iBuf += iRealAmt; iRem -= iRealAmt; } } if( rc==SQLITE_OK ){ pReal->nJournal = MAX(pReal->nJournal, iAmt+iOfst); } } return rc;}/*** Truncate an fs-file.*/static int fsTruncate(sqlite3_file *pFile, sqlite_int64 size){ fs_file *p = (fs_file *)pFile; fs_real_file *pReal = p->pReal; if( p->eType==DATABASE_FILE ){ pReal->nDatabase = MIN(pReal->nDatabase, size); }else{ pReal->nJournal = MIN(pReal->nJournal, size); } return SQLITE_OK;}/*** Sync an fs-file.*/static int fsSync(sqlite3_file *pFile, int flags){ fs_file *p = (fs_file *)pFile; fs_real_file *pReal = p->pReal; sqlite3_file *pRealFile = pReal->pFile; int rc = SQLITE_OK; if( p->eType==DATABASE_FILE ){ unsigned char zSize[4]; zSize[0] = (pReal->nDatabase&0xFF000000)>>24; zSize[1] = (pReal->nDatabase&0x00FF0000)>>16; zSize[2] = (pReal->nDatabase&0x0000FF00)>>8; zSize[3] = (pReal->nDatabase&0x000000FF); rc = pRealFile->pMethods->xWrite(pRealFile, zSize, 4, 0); } if( rc==SQLITE_OK ){ rc = pRealFile->pMethods->xSync(pRealFile, flags&(~SQLITE_SYNC_DATAONLY)); } return rc;}/*** Return the current file-size of an fs-file.*/static int fsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ fs_file *p = (fs_file *)pFile; fs_real_file *pReal = p->pReal; if( p->eType==DATABASE_FILE ){ *pSize = pReal->nDatabase; }else{ *pSize = pReal->nJournal; } return SQLITE_OK;}/*** Lock an fs-file.*/static int fsLock(sqlite3_file *pFile, int eLock){ return SQLITE_OK;}/*** Unlock an fs-file.*/static int fsUnlock(sqlite3_file *pFile, int eLock){ return SQLITE_OK;}/*** Check if another file-handle holds a RESERVED lock on an fs-file.*/static int fsCheckReservedLock(sqlite3_file *pFile, int *pResOut){ *pResOut = 0; return SQLITE_OK;}/*** File control method. For custom operations on an fs-file.*/static int fsFileControl(sqlite3_file *pFile, int op, void *pArg){ return SQLITE_OK;}/*** Return the sector-size in bytes for an fs-file.*/static int fsSectorSize(sqlite3_file *pFile){ return BLOCKSIZE;}/*** Return the device characteristic flags supported by an fs-file.*/static int fsDeviceCharacteristics(sqlite3_file *pFile){ return 0;}/*** Open an fs file handle.*/static int fsOpen( sqlite3_vfs *pVfs, const char *zName, sqlite3_file *pFile, int flags, int *pOutFlags){ fs_vfs_t *pFsVfs = (fs_vfs_t *)pVfs; fs_file *p = (fs_file *)pFile; fs_real_file *pReal = 0; int eType; int nName; int rc = SQLITE_OK; if( 0==(flags&(SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_MAIN_JOURNAL)) ){ tmp_file *p = (tmp_file *)pFile; memset(p, 0, sizeof(*p)); p->base.pMethods = &tmp_io_methods; return SQLITE_OK; } eType = ((flags&(SQLITE_OPEN_MAIN_DB))?DATABASE_FILE:JOURNAL_FILE); p->base.pMethods = &fs_io_methods; p->eType = eType; assert(strlen("-journal")==8); nName = strlen(zName)-((eType==JOURNAL_FILE)?8:0); pReal=pFsVfs->pFileList; for(; pReal && strncmp(pReal->zName, zName, nName); pReal=pReal->pNext); if( !pReal ){ sqlite3_int64 size; sqlite3_file *pRealFile; sqlite3_vfs *pParent = pFsVfs->pParent; assert(eType==DATABASE_FILE); pReal = (fs_real_file *)sqlite3_malloc(sizeof(*pReal)+pParent->szOsFile); if( !pReal ){ rc = SQLITE_NOMEM; goto open_out; } memset(pReal, 0, sizeof(*pReal)+pParent->szOsFile); pReal->zName = zName; pReal->pFile = (sqlite3_file *)(&pReal[1]); rc = pParent->xOpen(pParent, zName, pReal->pFile, flags, pOutFlags); if( rc!=SQLITE_OK ){ goto open_out; } pRealFile = pReal->pFile; rc = pRealFile->pMethods->xFileSize(pRealFile, &size); if( rc!=SQLITE_OK ){ goto open_out; } if( size==0 ){ rc = pRealFile->pMethods->xWrite(pRealFile, "\0", 1, BLOBSIZE-1); pReal->nBlob = BLOBSIZE; }else{ unsigned char zS[4]; pReal->nBlob = size; rc = pRealFile->pMethods->xRead(pRealFile, zS, 4, 0); pReal->nDatabase = (zS[0]<<24)+(zS[1]<<16)+(zS[2]<<8)+zS[3]; if( rc==SQLITE_OK ){ rc = pRealFile->pMethods->xRead(pRealFile, zS, 4, pReal->nBlob-4); if( zS[0] || zS[1] || zS[2] || zS[3] ){ pReal->nJournal = pReal->nBlob; } } } if( rc==SQLITE_OK ){ pReal->pNext = pFsVfs->pFileList; if( pReal->pNext ){ pReal->pNext->ppThis = &pReal->pNext; } pReal->ppThis = &pFsVfs->pFileList; pFsVfs->pFileList = pReal; } }open_out: if( pReal ){ if( rc==SQLITE_OK ){ p->pReal = pReal; pReal->nRef++; }else{ if( pReal->pFile->pMethods ){ pReal->pFile->pMethods->xClose(pReal->pFile); } sqlite3_free(pReal); } } return rc;}/*** Delete the file located at zPath. If the dirSync argument is true,** ensure the file-system modifications are synced to disk before** returning.*/static int fsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ int rc = SQLITE_OK; fs_vfs_t *pFsVfs = (fs_vfs_t *)pVfs; fs_real_file *pReal; sqlite3_file *pF; int nName = strlen(zPath) - 8; assert(strlen("-journal")==8); assert(strcmp("-journal", &zPath[nName])==0); pReal = pFsVfs->pFileList; for(; pReal && strncmp(pReal->zName, zPath, nName); pReal=pReal->pNext); if( pReal ){ pF = pReal->pFile; rc = pF->pMethods->xWrite(pF, "\0\0\0\0", 4, pReal->nBlob-BLOCKSIZE); if( rc==SQLITE_OK ){ pReal->nJournal = 0; } } return rc;}/*** Test for access permissions. Return true if the requested permission** is available, or false otherwise.*/static int fsAccess( sqlite3_vfs *pVfs, const char *zPath, int flags, int *pResOut){ fs_vfs_t *pFsVfs = (fs_vfs_t *)pVfs; fs_real_file *pReal; int isJournal = 0; int nName = strlen(zPath); if( flags!=SQLITE_ACCESS_EXISTS ){ sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; return pParent->xAccess(pParent, zPath, flags, pResOut); } assert(strlen("-journal")==8); if( nName>8 && strcmp("-journal", &zPath[nName-8])==0 ){ nName -= 8; isJournal = 1; } pReal = pFsVfs->pFileList; for(; pReal && strncmp(pReal->zName, zPath, nName); pReal=pReal->pNext); *pResOut = (pReal && (!isJournal || pReal->nJournal>0)); return SQLITE_OK;}/*** Populate buffer zOut with the full canonical pathname corresponding** to the pathname in zPath. zOut is guaranteed to point to a buffer** of at least (FS_MAX_PATHNAME+1) bytes.*/static int fsFullPathname( sqlite3_vfs *pVfs, /* Pointer to vfs object */ const char *zPath, /* Possibly relative input path */ int nOut, /* Size of output buffer in bytes */ char *zOut /* Output buffer */){ sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; return pParent->xFullPathname(pParent, zPath, nOut, zOut);}/*** Open the dynamic library located at zPath and return a handle.*/static void *fsDlOpen(sqlite3_vfs *pVfs, const char *zPath){ sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; return pParent->xDlOpen(pParent, zPath);}/*** Populate the buffer zErrMsg (size nByte bytes) with a human readable** utf-8 string describing the most recent error encountered associated ** with dynamic libraries.*/static void fsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; pParent->xDlError(pParent, nByte, zErrMsg);}/*** Return a pointer to the symbol zSymbol in the dynamic library pHandle.*/static void *fsDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){ sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; return pParent->xDlSym(pParent, pHandle, zSymbol);}/*** Close the dynamic library handle pHandle.*/static void fsDlClose(sqlite3_vfs *pVfs, void *pHandle){ sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; pParent->xDlClose(pParent, pHandle);}/*** Populate the buffer pointed to by zBufOut with nByte bytes of ** random data.*/static int fsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; return pParent->xRandomness(pParent, nByte, zBufOut);}/*** Sleep for nMicro microseconds. Return the number of microseconds ** actually slept.*/static int fsSleep(sqlite3_vfs *pVfs, int nMicro){ sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; return pParent->xSleep(pParent, nMicro);}/*** Return the current time as a Julian Day number in *pTimeOut.*/static int fsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; return pParent->xCurrentTime(pParent, pTimeOut);}/*** This procedure registers the fs vfs with SQLite. If the argument is** true, the fs vfs becomes the new default vfs. It is the only publicly** available function in this file.*/int fs_register(){ if( fs_vfs.pParent ) return SQLITE_OK; fs_vfs.pParent = sqlite3_vfs_find(0); fs_vfs.base.mxPathname = fs_vfs.pParent->mxPathname; fs_vfs.base.szOsFile = MAX(sizeof(tmp_file), sizeof(fs_file)); return sqlite3_vfs_register(&fs_vfs.base, 0);}#ifdef SQLITE_TEST int SqlitetestOnefile_Init() {return fs_register();}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -