📄 fsdb.cpp
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: fsdb.cpp,v 1.7.8.1 2004/07/09 02:04:03 hubbe Exp $ * * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved. * * The contents of this file, and the files included with this file, * are subject to the current version of the RealNetworks Public * Source License (the "RPSL") available at * http://www.helixcommunity.org/content/rpsl unless you have licensed * the file under the current version of the RealNetworks Community * Source License (the "RCSL") available at * http://www.helixcommunity.org/content/rcsl, in which case the RCSL * will apply. You may also obtain the license terms directly from * RealNetworks. You may not use this file except in compliance with * the RPSL or, if you have a valid RCSL with RealNetworks applicable * to this file, the RCSL. Please see the applicable RPSL or RCSL for * the rights, obligations and limitations governing use of the * contents of the file. * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL") in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your version of * this file only under the terms of the GPL, and not to allow others * to use your version of this file under the terms of either the RPSL * or RCSL, indicate your decision by deleting the provisions above * and replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient may * use your version of this file under the terms of any one of the * RPSL, the RCSL or the GPL. * * This file is part of the Helix DNA Technology. RealNetworks is the * developer of the Original Code and owns the copyrights in the * portions it created. * * This file, and the files included with this file, is distributed * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET * ENJOYMENT OR NON-INFRINGEMENT. * * Technology Compatibility Kit Test Suite(s) Location: * http://www.helixcommunity.org/content/tck * * Contributor(s): * * ***** END LICENSE BLOCK ***** */#include "hxtypes.h"#ifndef _MACINTOSH# include "hlxclib/sys/types.h"//# include <sys/stat.h>#endif#include "hlxclib/stdio.h"#include "hlxclib/stdlib.h"#include "safestring.h"#include "hxdir.h" // for mkdir()#include "hxcom.h"#include "db.h"#include "findfile.h"#include "chxdataf.h"#include "hxstrutl.h"#if defined( _WIN32) && !defined(_WINCE)# include <io.h>#endif#include "hxheap.h"#ifdef _DEBUG#undef HX_THIS_FILEstatic const char HX_THIS_FILE[] = __FILE__;#endif#define LOG_FILE "C:/Temp/db.log"#include "http_debug.h"#define MAX_CACHE_ENTRY_SIZE (8 * 1024 * 1024) /* 8 MBytes limit on entry size */#ifdef _WIN32# define BINARY_FILE "b"#else# define BINARY_FILE ""#endifint db_func_close (DB *);int db_func_del (const DB *, const DBT *, UINT32);int db_func_get (const DB *, const DBT *, DBT *, UINT32);int db_func_put (const DB *, DBT *, const DBT *, UINT32);int db_func_seq (const DB *, DBT *, DBT *, UINT32);int db_func_sync (const DB *, UINT32);int db_func_fd (const DB *);typedef struct _FileList { char* pFilename; struct _FileList* pNext;} FileList; static HX_RESULT GrabMutex (const DB* pDb);static HX_RESULT FreeMutex (const DB* pDb);static HX_RESULT CloseMutex (const DB* pDb);static HX_RESULT createDatabase (DB* pDb, const char* pszFilename);static void createPath (const char* pszFilename);static HX_RESULT destroyDatabase (const DB* pDb, const char* pszFilename);static HX_RESULT verifyDatabase (const char* pszFilename);static HX_RESULT GetFilename (const DB* pDb, const DBT* pKey, UINT8** ppszFilename, FILE** ppFile);static HX_RESULT GetKeyAndData (char* pFilename, DBT* pKey, DBT* pData);static BOOL CompareKey (const DBT* pKey, FILE* pFile);static HX_RESULT WriteSection (FILE* pFile, const DBT* pDbThang);static void CreateFileList (const DB* pDb, FileList* pListHead);static void FreeFileList (FileList* pList);extern UINT32 GetFreeMbyteCount (const char *pDirectoryName);#if defined (_WINDOWS ) || defined (_WIN32)#define OS_SEPARATOR_CHAR '\\'#define OS_SEPARATOR_STRING "\\"#elif defined (_UNIX)#define OS_SEPARATOR_CHAR '/'#define OS_SEPARATOR_STRING "/"#elif defined (__MWERKS__)#define OS_SEPARATOR_CHAR ':'#define OS_SEPARATOR_STRING ":"#endif // defined (_WINDOWS ) || defined (WIN32)UINT32get_hash (const DBT* pKey){ UINT32 hash = 5381; UINT8* pStr = (UINT8*) pKey->data; UINT32 ulSize = pKey->size; while (ulSize--) hash = ((hash << 5) + hash) + *pStr++; return hash;}#ifdef __cplusplusextern "C" {#endif#ifndef O_RDONLY#define O_RDONLY 001#endifDB *dbopen(const char* pszFilename, int ulFlags, int nMode, DBTYPE type, DB_INFO* pInfo){ DB* pDb = (DB*) calloc (1, sizeof (DB)); UINT32 rc = 0; UINT32 i = 0; char* pLastSlash = NULL; LOG ("dbopen()"); if (pDb == NULL) { LOG ("*** Cannot allocate space for database reference"); return(NULL); } if (type != DB_HASH) { LOG ("*** Only hashed database entries are supported\n"); // Go ahead and treat it as a hashed database anyway } // Record the directory path pDb->pDir = (char*)malloc (strlen(pszFilename) + 1); strcpy (pDb->pDir, pszFilename); /* Flawfinder: ignore */ pLastSlash = strrchr (pDb->pDir, OS_SEPARATOR_CHAR); if (pLastSlash) { *pLastSlash = '\0'; } else { *pDb->pDir = '\0'; } LOGX ((szDbgTemp, " Base directory = '%s'", pDb->pDir)); // Set up the function references pDb->close = db_func_close; pDb->del = db_func_del; pDb->get = db_func_get; pDb->put = db_func_put; pDb->seq = db_func_seq; pDb->sync = db_func_sync; pDb->fd = db_func_fd; // Make sure we are the only ones accessing the database if (GrabMutex(pDb) == HXR_FAIL) { LOG (" Could not get mutex to open file"); return(NULL); } if (ulFlags & DB_TRUNCATE) { LOG (" DB Truncate flag, destroying existing database"); destroyDatabase(pDb, pszFilename); } // Verify the database structure rc = verifyDatabase (pszFilename); if ((rc != HXR_OK) && !(ulFlags & DB_CREATE)) { LOG (" DB does not exist, create flag not present"); FreeMutex (pDb); return (NULL); } #if 0 if ((rc == HXR_OK) && (ulFlags & DB_NOOVERWRITE)) { LOG (" DB exists: DB_NOOVERWRITE flag prohibits success"); FreeMutex (pDb); return (NULL); }#endif // 0 if ((rc != HXR_OK) && (ulFlags & DB_CREATE)) { LOG (" Creating new database"); UINT32 ulMbytesFree = 0; ulMbytesFree = GetFreeMbyteCount(pDb->pDir); if (ulMbytesFree < DISK_FREE_LOW) { LOG ("*** Not enough disk space to safely create cache"); FreeMutex (pDb); return(NULL); } rc = createDatabase(pDb, pszFilename); if (rc == HXR_OK) { rc = verifyDatabase (pszFilename); } } if (rc != HXR_OK) { LOG ("*** Verify failed, closing database"); db_func_close (pDb); pDb = NULL; } FreeMutex (pDb); return(pDb);}#ifdef __cplusplus}#endifstatic HX_RESULTcreateDatabase(DB* pDb, const char* pszFilename){ FILE* pDbFile = NULL; createPath (pszFilename); #ifdef _WINCE ::DeleteFile(OS_STRING(pszFilename));#else unlink (pszFilename);#endif /* _WINCE */ pDbFile = fopen (pszFilename, "w"); if (pDbFile) { UINT32 ulTime = time(NULL); fprintf (pDbFile, "REALFSDB\n" "%s" "Partitioning=%d\n", ctime ((time_t *)&ulTime), 0); fclose (pDbFile); } return(pDbFile ? HXR_OK : HXR_FAIL);}static voidcreatePath (const char* pszFilename){ UINT32 ulIndex = 0; UINT8* pDir = NULL; CHXDirectory Dir; if (pszFilename == NULL) return; pDir = (UINT8*)calloc (strlen(pszFilename) + 1, 1); for (ulIndex = 0; ulIndex < MAX_FILENAME; ulIndex++) { pDir[ulIndex] = pszFilename[ulIndex]; if (pszFilename[ulIndex] == '\0') break; if (pszFilename[ulIndex + 1] == OS_SEPARATOR_CHAR) { Dir.SetPath((char*)pDir); if(!Dir.IsValid()) { Dir.Create(); } } } free (pDir);}static HX_RESULTdestroyDatabase(const DB* pDb, const char* pszFilename){ char pBaseDir[MAX_FILENAME] = { 0 }; /* Flawfinder: ignore */ FileList fileList = { 0, 0 }; FileList* pEntry = NULL; FileList* pNext = NULL; #ifdef _WINCE ::DeleteFile(OS_STRING(pszFilename));#else unlink (pszFilename);#endif /* _WINCE */ // Delete all of the hash filenames CreateFileList (pDb, &fileList); pEntry = fileList.pNext; while (pEntry) { pNext = pEntry->pNext; remove (pEntry->pFilename); free (pEntry->pFilename); free (pEntry); pEntry = pNext; } return(HXR_OK);}static HX_RESULTverifyDatabase (const char* pszFilename){ HX_RESULT rc = HXR_FAIL; CHXDataFile* pPnFile = CHXDataFile::Construct(); HX_ASSERT(pPnFile); if (pPnFile == NULL) { LOG (" HXR_OUTOFMEMORY"); rc = HXR_OUTOFMEMORY; } else { if (pPnFile->Open (pszFilename, O_RDONLY) == HXR_OK) { ULONG32 ulFileSize = 0; ulFileSize = pPnFile->GetSize();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -