📄 fsdb.cpp
字号:
if (ulFileSize > 4) { rc = HXR_OK; } else { LOGX((szDbgTemp, " Size too small: %lu [%s]", ulFileSize, pszFilename)); } pPnFile->Close(); } else { LOG ("*** Cannot open pPnFile"); } delete pPnFile; } // Check the sub-directory structure LOG (" Database verified"); return(rc);}intdb_func_close (DB *pDb){ LOG ("db_func_close()"); if (pDb) { CloseMutex (pDb); if (pDb->pDir) { free (pDb->pDir); } free (pDb); } return(HXR_OK);}intdb_func_del (const DB *pDb, const DBT *pKey, UINT32 ulFlags){ UINT8* pszFilename = 0; FILE* pFile = NULL; LOG ("db_func_del()"); // Grab control of the database if (GrabMutex(pDb) != HXR_OK) { return(HXR_FAIL); } GetFilename (pDb, pKey, &pszFilename, &pFile); if (pszFilename && pFile) { LOGX((szDbgTemp, " Deleting entry %s", pszFilename)); // We have found the entry, delete it fclose (pFile); pFile = NULL; remove ((char*)pszFilename); } else { LOG (" Nothing deleted"); } if (pFile) { fclose(pFile); } if (pszFilename) { free (pszFilename); } FreeMutex (pDb); // Return the result return(HXR_OK);}intdb_func_get (const DB *pDb, const DBT *pKey, DBT *pData, UINT32 ulFlags){ UINT32 ulEntryIndex = 0; char* pszFilename = NULL; FILE* pFile = NULL; static UINT8* pucData = NULL; LOG ("db_funct_get()"); pData->size = 0; pData->data = NULL; // Grab control of the database if (GrabMutex(pDb) != HXR_OK) { return(HXR_FAIL); } GetFilename (pDb, pKey, (UINT8**)&pszFilename, &pFile); if (pszFilename && pFile) { UINT32 ulFileSize = 0; UINT32 ulSize = 0; UINT8 pSizeStr[4] = { 0 }; LOGX((szDbgTemp, " Found entry %s", pszFilename)); // We have found the entry, get the data if (fread(pSizeStr, 1, sizeof pSizeStr, pFile) == sizeof pSizeStr) { ulSize = pSizeStr[0]; // Architecture independent ulSize = (ulSize << 8) | pSizeStr[1]; ulSize = (ulSize << 8) | pSizeStr[2]; ulSize = (ulSize << 8) | pSizeStr[3]; // Get the file's actual size to verify data length CHXDataFile* pPnFile = CHXDataFile::Construct(); HX_ASSERT(pFile); if (pPnFile != NULL) { if (pPnFile->Open (pszFilename, O_RDONLY) == HXR_OK) { ulFileSize = pPnFile->GetSize(); pPnFile->Close(); } delete pPnFile; } if (sizeof pKey->size + pKey->size + sizeof ulSize + ulSize != ulFileSize) { LOGX((szDbgTemp, "*** Incorrect file length (on get!): %lu + %lu + %lu + %lu vs %lu", sizeof pKey->size, pKey->size, sizeof ulSize, ulSize, ulFileSize)); fclose (pFile); pFile = NULL; remove (pszFilename); free (pszFilename); FreeMutex (pDb); return(HXR_FAIL); } LOGX((szDbgTemp, " Data size is %lu bytes", ulSize)); pData->size = ulSize; pData->data = malloc (ulSize); fread (pData->data, 1, ulSize, pFile); } else { LOGX((szDbgTemp, " fread failed [%s]", strerror(errno))); } fclose (pFile); pFile = NULL; free (pszFilename); pszFilename = NULL; } else { LOG (" Miss"); } if (pFile) { fclose(pFile); } if (pszFilename) { free (pszFilename); } pucData = (UINT8*)pData->data; FreeMutex (pDb); // Return the result LOG (pucData ? "...db_get succeeded" : "...db_get failed"); return(pucData ? HXR_OK : HXR_FAIL);}intdb_func_put (const DB *pDb, DBT *pKey, const DBT *pData, UINT32 ulFlags){ BOOL bFailed = FALSE; UINT32 ulBlockCount = 0; UINT32 i = 0; char* pszFilename = NULL; FILE* pFile = NULL; LOG ("db_funct_put()"); // Grab control of the database if (GrabMutex(pDb) != HXR_OK) { return(DB_PUT_ERROR); } // If overwrite mode selected, zap any existing entries if (!(ulFlags & DB_NOOVERWRITE)) db_func_del (pDb, pKey, 0); // Fail if DB_NOOVERWRITE and entry exists GetFilename (pDb, pKey, (UINT8**)&pszFilename, &pFile); if (pszFilename && pFile) { LOG (""); LOGX((szDbgTemp, " *** Target file exists, aborting put: '%s'", pszFilename)); fclose (pFile); free (pszFilename); FreeMutex (pDb); return(DB_KEYEXIST); } else { UINT32 ulSize = 0; LOGX((szDbgTemp, " Writing entry '%s'", pszFilename)); if (ulSize > MAX_CACHE_ENTRY_SIZE) { LOG ("*** Cache entry too big"); free (pszFilename); return(HXR_FAIL); } // Create the entry file if ((pFile = fopen (pszFilename, "w" BINARY_FILE)) != NULL) { WriteSection (pFile, pKey); WriteSection (pFile, pData); fclose (pFile); pFile = NULL; } else { LOG (" File creation failed"); } } FreeMutex (pDb); if (pszFilename) free(pszFilename); LOG (bFailed ? "...db_put failed" : "...db_put succeeded"); return(bFailed ? DB_PUT_ERROR : DB_PUT_SUCCESS);}static HX_RESULTWriteSection (FILE* pFile, const DBT* pDbThang){ UINT8 pSizeStr[4] = { 0 }; HX_RESULT rc = HXR_FAIL; if (pFile == NULL || pDbThang == NULL) { LOG ("WriteSection(): Null parameters"); return(HXR_FAIL); } LOGX((szDbgTemp, " WriteSection Size=%lu", pDbThang->size)); pSizeStr[0] = (pDbThang->size >> 24) & 0xFF; pSizeStr[1] = (pDbThang->size >> 16) & 0xFF; pSizeStr[2] = (pDbThang->size >> 8) & 0xFF; pSizeStr[3] = (pDbThang->size >> 0) & 0xFF; if (fwrite (pSizeStr, 1, sizeof pSizeStr, pFile) == sizeof pSizeStr) { if (fwrite (pDbThang->data, 1, pDbThang->size, pFile) == pDbThang->size) rc = HXR_OK; } if (rc != HXR_OK) { LOGX((szDbgTemp, " Write failed [%08X] [%s]", pFile, strerror(errno))); } LOGX((szDbgTemp, " File postion = %lu [%08X]", ftell(pFile), pFile)); fflush(NULL); return(rc);} intdb_func_seq (const DB *pDb, DBT *pKey, DBT *pHeader, UINT32 ulFlags){ static UINT32 ulNext = 0; static UINT8* pKeyData = NULL; static UINT8* pHeaderData = NULL; static BOOL bFirstTime = TRUE; HX_RESULT rc = HXR_FAIL; FILE* pFile = NULL; BOOL bKeyValid = FALSE; UINT32 ulSize = 0; UINT32 ulFileSize = 0; UINT8 pSizeStr[4] = { 0 }; char* pFilename = NULL; char pFullName[MAX_FILENAME]; /* Flawfinder: ignore */ static FileList* pTail = NULL; static FileList listHead = { NULL, NULL }; // A nice linked list of directory entries LOG ("db_func_seq()"); // data is valid until the routine is called again if (pKeyData) { free (pKeyData); pKeyData = NULL; } if (pHeaderData) { free (pHeaderData); pHeaderData = NULL; } if (bFirstTime == TRUE && ulFlags == R_NEXT) { bFirstTime = FALSE; ulFlags = R_FIRST; } switch (ulFlags) { case R_FIRST: // Create list of files, set index to zero CreateFileList ((DB*)pDb, &listHead); pTail = listHead.pNext; // Fall through to case 'R_NEXT' case R_NEXT: { // Get the key and data if (pTail == NULL) { FreeFileList (&listHead); return(DB_NOTFOUND); } // Get the entry SafeSprintf(pFullName, MAX_FILENAME, "%-.400s%c%-.400s", pDb->pDir, OS_SEPARATOR_CHAR, pTail->pFilename); // Get the file's actual size to verify data length CHXDataFile* pPnFile = CHXDataFile::Construct(); HX_ASSERT(pPnFile); if (pPnFile != NULL) { if (pPnFile->Open (pFullName, O_RDONLY) == HXR_OK) { ulFileSize = pPnFile->GetSize(); pPnFile->Close(); } delete pPnFile; } if ((pFile = fopen (pFullName, "r")) != NULL) { // Get the Key size if (fread(pSizeStr, 1, sizeof pSizeStr, pFile) == sizeof pSizeStr) { ulSize = pSizeStr[0]; // Architecture independent ulSize = (ulSize << 8) | pSizeStr[1]; ulSize = (ulSize << 8) | pSizeStr[2]; ulSize = (ulSize << 8) | pSizeStr[3]; if (sizeof ulSize + ulSize + sizeof (UINT32) > ulFileSize) { LOGX ((szDbgTemp, "*** Key size too large: %lu vs %lu", ulSize, ulFileSize)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -