📄 fsdb.cpp
字号:
else if (ulSize < 5) { LOG ("*** Key size too small"); // "http:" is the shortest } else { pKey->size = ulSize; pKey->data = malloc (ulSize); if (fread (pKey->data, 1, ulSize, pFile) != ulSize) { LOGX ((szDbgTemp, "*** Bad read on key data")); } else { LOGX ((szDbgTemp, " Got Key[%04X]=%1.*s", pKey->size, pKey->size, pKey->data)); bKeyValid = TRUE; } pKeyData = (UINT8*)pKey->data; } } pSizeStr[0] = 0; pSizeStr[1] = 0; pSizeStr[2] = 0; pSizeStr[3] = 0; // Get the Data size if (bKeyValid && (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 (ulSize > MAX_CACHE_ENTRY_SIZE) { LOG ("*** Data size too large"); } else { INT32 nReadBytes = 0; pHeader->size = ulSize; pHeader->data = malloc (ulSize); if ((nReadBytes = fread (pHeader->data, 1, ulSize, pFile)) == (INT32) ulSize) { rc = HXR_OK; } else { LOGX ((szDbgTemp, " Bad read on data data: %d vs %d", nReadBytes, ulSize)); rc = HXR_FAIL; } LOGX ((szDbgTemp, " Got Data[%ld]", pHeader->size)); pHeaderData = (UINT8*)pHeader->data; } } fclose (pFile); } else { LOG ("*** Cannot open file for sequential access"); } pTail = pTail->pNext; break; } default: LOGX ((szDbgTemp, "*** Invalid flag = %lu", ulFlags)); break; } return(rc);}static voidFreeFileList (FileList* pList){ FileList* pTemp = pList; pList = pList->pNext; // the first entry is not freeable pTemp->pNext = NULL; // ...but it does need to be pruned while (pList) { pTemp = pList->pNext; if (pList->pFilename) free (pList->pFilename); free (pList); pList = pTemp; } }static voidCreateFileList (const DB* pDb, FileList* pList){// HANDLE hSearch = NULL;// WIN32_FIND_DATA findData = { 0 };// char pCheckFilename[MAX_FILENAME]; FileList* pTail = pList; char* pszDllName = NULL; CFindFile* pFileFinder = NULL; FreeFileList (pList); LOG ("CreateFileList()"); pFileFinder = CFindFile::CreateFindFile(pDb->pDir, 0, "????????.???"); if (NULL == pFileFinder) { return; } pszDllName = pFileFinder->FindFirst(); while (pszDllName) { pszDllName = pFileFinder->GetCurFilePath(); if (pszDllName != NULL) { HX_ASSERT (strlen(pDb->pDir) < strlen(pszDllName)); char *pszFileOnly = pszDllName + strlen(pDb->pDir) + 1; if (strlen(pszFileOnly) != 12) // 8.3 filename {// LOGX ((szDbgTemp, " Skipping '%s' [Length]", pszDllName)); } else if (strspn (pszFileOnly, "0123456789abcdefABCDEF.") != strlen(pszFileOnly)) {// LOGX ((szDbgTemp, " Skipping '%s' [Alphabet]", pszFileOnly)); } else {// LOGX ((szDbgTemp, " Adding '%s'", pszDllName)); // Add file to list pTail->pNext = (FileList*)malloc (sizeof (FileList)); pTail = pTail->pNext; pTail->pNext = NULL; pTail->pFilename = (char*)malloc (strlen (pszFileOnly) + 1); strcpy (pTail->pFilename, pszFileOnly); /* Flawfinder: ignore */ } } pszDllName = pFileFinder->FindNext(); } HX_DELETE(pFileFinder);} intdb_func_sync (const DB *pDb, UINT32 ulFlags){ LOG ("db_func_sync()"); return(HXR_OK); }intdb_func_fd (const DB *pDb){ return(HXR_FAIL);}/* * GetFilename (pDb, pszUrl, ppszFilename, ppFile) * * This is passed a URL and returns the filename to be used for that URL. * The file handle ppFile is set if an entry for the URL already exists. * The position of the file pointer is to the start of the data section * of the file when this function returns. * * If no file exists for a given key, a suitable filename is returned * and the value ppFile is set to NULL. * * The caller is responsible for freeing ppszFilename and closing ppFile. */#define HASHED_FILENAME_OVERFLOW_MAX 1000static HX_RESULTGetFilename (const DB* pDb, const DBT* pKey, UINT8** ppszFilename, FILE** ppFile){ UINT32 hash = 0; char pCheckFilename[MAX_FILENAME]; /* Flawfinder: ignore */ char rgucUsed[HASHED_FILENAME_OVERFLOW_MAX] = { 0 }; /* Flawfinder: ignore */ // Flag to indicate filename is used // HANDLE hSearch = NULL;// WIN32_FIND_DATA findData = { 0 }; FILE* pFile = NULL; BOOL bMatch = FALSE; char* pszDllName = NULL; CFindFile* pFileFinder = NULL; LOG ("GetFilename()"); // Make sure the return values are initially zero *ppszFilename = NULL; *ppFile = NULL; // get the has value hash = get_hash (pKey); // Search for alternate extensions// sprintf (pCheckFilename, "%s%c%08X.???", pDb->pDir, OS_SEPARATOR_CHAR, hash); // Get list of files which match the URL's hash value SafeSprintf(pCheckFilename, MAX_FILENAME, "%08X.???", hash); pFileFinder = CFindFile::CreateFindFile(pDb->pDir, 0, pCheckFilename); if (NULL == pFileFinder) { return HXR_FAIL; } pszDllName = pFileFinder->FindFirst(); while (pszDllName) { pszDllName = pFileFinder->GetCurFilePath(); if (pszDllName != NULL) { UINT32 ulIndex = 0; LOGX ((szDbgTemp, " Looking at [%s]", pszDllName)); if (strlen(pszDllName) < 4) break; // Mark name as in use ulIndex = atol (pszDllName + strlen(pszDllName) - 4); if (ulIndex < HASHED_FILENAME_OVERFLOW_MAX) rgucUsed[ulIndex] = 1; // Get the URL from the file SafeSprintf(pCheckFilename, MAX_FILENAME, "%-.400s%c%-.400s", pDb->pDir, OS_SEPARATOR_CHAR, pszDllName); pFile = fopen (pszDllName, "r" BINARY_FILE "+"); if (pFile == NULL) { LOGX ((szDbgTemp, " Cannot open file '%s' [%s]", pCheckFilename, strerror(errno))); break; } if (CompareKey (pKey, pFile)) { bMatch = TRUE; break; } fclose (pFile); pszDllName = pFileFinder->FindNext(); } } if (bMatch == TRUE) { LOG (" Match"); if (ppszFilename) { int lenFilename = strlen(pszDllName) + 2; *ppszFilename = (UINT8*)malloc (lenFilename); SafeSprintf ((char*)*ppszFilename, lenFilename, "%s", pszDllName); /* Flawfinder: ignore */ } if (ppFile) { *ppFile = pFile; } } else { // No match, return first valid name if (ppszFilename) { UINT32 ulIndex = 0; for (ulIndex = 0; ulIndex < HASHED_FILENAME_OVERFLOW_MAX; ulIndex++) { if (rgucUsed[ulIndex] == 0) { int lenFilename = strlen (pDb->pDir) + 16; *ppszFilename = (UINT8*)malloc (lenFilename); SafeSprintf ((char*)*ppszFilename, lenFilename, "%s%c%08X.%03lu", pDb->pDir, OS_SEPARATOR_CHAR, hash, ulIndex); /* Flawfinder: ignore */ LOGX ((szDbgTemp, " No match, return first valid name [%s]", *ppszFilename)); break; } } } } HX_DELETE(pFileFinder); return (pFile ? HXR_OK : HXR_FAIL); }/* * CompareKey() -- The hash matches the key's hash, verify by reading the * key string from the file and doing a full compare. */ static BOOLCompareKey (const DBT* pKey, FILE* pFile){ UINT32 ulSize = 0; UINT8 pSizeStr[4] = { 0 }; char* pKeyStr = NULL; BOOL bMatch = FALSE; 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 (ulSize != pKey->size) { LOG (" Size check fails"); fclose (pFile); return (FALSE); } pKeyStr = (char*)malloc (ulSize); if (pKeyStr) { if (fread (pKeyStr, 1, ulSize, pFile) != ulSize) { LOG (" Error reading key string"); } else { LOGX ((szDbgTemp, " '%1.*s' vs '%s' [%lu]", ulSize, pKeyStr, pKey->data, ulSize)); if (strncmp (pKeyStr, (char*)pKey->data, ulSize) == 0) { bMatch = TRUE; } } } if (pKeyStr) free (pKeyStr); return(bMatch); } else { LOG ("*** Could not read size bytes"); } return(FALSE);}static HX_RESULTGetKeyAndData (char* pFilename, DBT* pKey, DBT* pData){ return(FALSE);} /* * The following inclusion of C code will be replaced by a change * in the UMAKE file to add a platform dependent target for 'pndbsupp'. */ #ifdef _WIN32# include "./platform/win/hxdbsupp.c"#endif#if defined(_UNIX) || defined(__TCS__)# include "./platform/unix/hxdbsupp.c"#endif#ifdef _MACINTOSH# include "./platform/mac/hxdbsupp.cpp"#endif#ifdef _OPENWAVE# include "./platform/openwave/hxdbsupp.c"#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -