From: Robert K. <may...@us...> - 2001-10-10 23:25:51
|
Update of /cvsroot/bitcollider/bitcollider/lib In directory usw-pr-cvs1:/tmp/cvs-serv17734/lib Modified Files: Makefile.am main.c Added Files: cache.c Log Message: Added support for caching using the Berkeley DB. If the DB is not found, the cache functionality degrades nicely. Linux only so far -- support for windows will be done shortly. --- NEW FILE: cache.c --- /* (PD) 2001 The Bitzi Corporation * Please see file COPYING or http://bitzi.com/publicdomain * for more info. * * $Id: cache.c,v 1.1 2001/10/10 23:25:48 mayhemchaos Exp $ */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "cache.h" #define DB printf("%s:%d\n", __FILE__, __LINE__); #if USE_BDB cache_info *init_cache(void) { cache_info *info; info = malloc(sizeof(cache_info)); memset(info, 0, sizeof(cache_info)); return info; } b_bool open_cache(cache_info *info, const char *fileName) { int ret; ret = db_create(&info->dbp, NULL, 0); if (ret != 0) { info->errorString = strdup(db_strerror(ret)); return false; } ret = info->dbp->open(info->dbp, fileName, NULL, DB_BTREE, DB_CREATE, 0664); if (ret != 0) { info->errorString = strdup(db_strerror(ret)); return false; } return true; } void close_cache(cache_info *info) { info->dbp->close(info->dbp, 0); if (info->errorString) free(info->errorString); free(info); } b_bool get_cache_entry(cache_info *info, cache_entry *entry) { DBT key, data; int ret; char *str, *token; memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); key.data = entry->fileName; key.size = strlen(entry->fileName); ret = info->dbp->get(info->dbp, NULL, &key, &data, 0); if (ret == DB_NOTFOUND) { if (info->errorString) free(info->errorString); info->errorString = NULL; return false; } if (ret != 0) { info->errorString = strdup(db_strerror(ret)); return false; } str = malloc(data.size + 1); strncpy(str, data.data, data.size); str[data.size] = 0; token = strtok(str, "*"); if (token) { entry->lastModDate = atoi(token); token = strtok(NULL, "*"); if (token) strcpy(entry->bitprint, token); } else { entry->lastModDate = 0; entry->bitprint[0] = 0; } free(str); return true; } b_bool add_cache_entry(cache_info *info, cache_entry *entry) { DBT key, data; int ret; char strData[100]; memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); key.data = entry->fileName; key.size = strlen(entry->fileName); sprintf(strData, "%ld*%s", (long)entry->lastModDate, entry->bitprint); data.data = strData; data.size = strlen(strData); ret = info->dbp->put(info->dbp, NULL, &key, &data, DB_NOOVERWRITE); if (ret != 0) { info->errorString = strdup(db_strerror(ret)); return false; } return true; } b_bool remove_cache_entry(cache_info *info, cache_entry *entry) { DBT key; int ret; memset(&key, 0, sizeof(DBT)); key.data = entry->fileName; key.size = strlen(entry->fileName); ret = info->dbp->del(info->dbp, NULL, &key, 0); if (ret != 0) { info->errorString = strdup(db_strerror(ret)); return false; } return true; } #endif Index: Makefile.am =================================================================== RCS file: /cvsroot/bitcollider/bitcollider/lib/Makefile.am,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -r1.12 -r1.13 *** Makefile.am 2001/08/10 22:29:31 1.12 --- Makefile.am 2001/10/10 23:25:48 1.13 *************** *** 9,15 **** INCLUDES = $(INCLTDL) -I$(top_srcdir)/.. -I$(top_srcdir)/ver -I$(top_srcdir)/include lib_LTLIBRARIES = libbitcollider.la ! libbitcollider_la_SOURCES = main.c sha1.c tiger.c tigertree.c sboxes.c browser.c mp3.c id3.c plugin_man.c plugin_man.h dirsearch.c dir.h bitprint.c bitprint.h md5.c md5.h libbitcollider_la_LDFLAGS = -version-info 1:0:0 ! libbitcollider_la_LIBADD = $(LIBLTDL) noinst_HEADERS = browser.h mp3.h id3.h --- 9,15 ---- INCLUDES = $(INCLTDL) -I$(top_srcdir)/.. -I$(top_srcdir)/ver -I$(top_srcdir)/include lib_LTLIBRARIES = libbitcollider.la ! libbitcollider_la_SOURCES = main.c sha1.c tiger.c tigertree.c sboxes.c browser.c mp3.c id3.c plugin_man.c plugin_man.h dirsearch.c dir.h bitprint.c bitprint.h md5.c md5.h cache.c libbitcollider_la_LDFLAGS = -version-info 1:0:0 ! libbitcollider_la_LIBADD = $(LIBLTDL) $(BDB_LIBS) noinst_HEADERS = browser.h mp3.h id3.h Index: main.c =================================================================== RCS file: /cvsroot/bitcollider/bitcollider/lib/main.c,v retrieving revision 1.33 retrieving revision 1.34 diff -C2 -r1.33 -r1.34 *** main.c 2001/08/23 20:39:42 1.33 --- main.c 2001/10/10 23:25:48 1.34 *************** *** 22,25 **** --- 22,27 ---- #include "browser.h" #include <errno.h> + #include <unistd.h> + #include <sys/stat.h> #else #include <windows.h> *************** *** 91,97 **** DWORD type; #endif ! char path[MAX_PATH], *ptr; int total = 0; bc = init_plugins(); --- 93,100 ---- DWORD type; #endif ! char path[MAX_PATH], cacheFile[MAX_PATH], *ptr; int total = 0; + cacheFile[0] = 0; bc = init_plugins(); *************** *** 106,109 **** --- 109,113 ---- if (ptr) { + sprintf(cacheFile, "%s/.bitcollider/cache.db", ptr); sprintf(path, "%s/.bitcollider/plugins", ptr); if (printDebugInfo) *************** *** 111,114 **** --- 115,123 ---- total += load_plugins(bc, path, printDebugInfo); } + else + { + if (printDebugInfo) + fprintf(stderr, "HOME env var not set. Cannot find home.\n"); + } ptr = PREFIX"/lib/bitcollider/plugins"; *************** *** 129,135 **** DWORD size = MAX_PATH; ret = RegQueryValueEx(hKey, "PluginDir", NULL, &type, path, &size); - RegCloseKey(hKey); if (ret == ERROR_FILE_NOT_FOUND) --- 138,144 ---- DWORD size = MAX_PATH; + // Get the plugin dir, first ret = RegQueryValueEx(hKey, "PluginDir", NULL, &type, path, &size); if (ret == ERROR_FILE_NOT_FOUND) *************** *** 137,141 **** --- 146,159 ---- else if (ret != ERROR_SUCCESS) + { + RegCloseKey(hKey); return bc; + } + + // Now try to get the cache file + size = MAX_PATH; + ret = RegQueryValueEx(hKey, "CacheFile", NULL, &type, + cacheFile, &size); + RegCloseKey(hKey); } ptr = path; *************** *** 150,153 **** --- 168,189 ---- if (printDebugInfo) fprintf(stderr, "Loaded %d plugins total.\n\n", total); + + #if USE_BDB + if (cacheFile[0] != 0) + { + bc->cache = init_cache(); + if (bc->cache) + { + if (!open_cache(bc->cache, cacheFile)) + { + if (printDebugInfo) + fprintf(stderr, "Failed to open cache file: %s.\n\n", + cacheFile); + close_cache(bc->cache); + bc->cache = NULL; + } + } + } + #endif return bc; *************** *** 156,159 **** --- 192,199 ---- void bitcollider_shutdown(Bitcollider *bc) { + #if USE_BDB + if (bc->cache) + close_cache(bc->cache); + #endif unload_plugins(bc); shutdown_plugins(bc); *************** *** 188,191 **** --- 228,235 ---- Attribute *attrList = NULL, *attr; const char *err; + #if USE_BDB + cache_entry centry; + struct stat fileInfo; + #endif if (submission->bc->error) *************** *** 249,252 **** --- 293,322 ---- return true; + #if USE_BDB + /* Check to see if this bitprint has already been bitprinted */ + strcpy(centry.fileName, fileName); + if (get_cache_entry(submission->bc->cache, ¢ry)) + { + if (stat(submission->fileName, &fileInfo) == 0) + { + if (fileInfo.st_mtime == centry.lastModDate) + { + if (submission->bc->progressCallback && + !submission->bc->preview) + submission->bc->progressCallback(0, submission->fileName, + "found in cache, skipped."); + return true; + } + } + else + { + printf("Stat failed.\n"); + } + /* Its out of date or something else is wrong. Remove the entry + from the cache */ + remove_cache_entry(submission->bc->cache, ¢ry); + } + #endif + if (mp3Check) mp3Info = malloc(sizeof(mp3_info)); *************** *** 372,375 **** --- 442,459 ---- submission->numBitprints++; + + #if USE_BDB + strcpy(centry.fileName, fileName); + strcpy(centry.bitprint, bitprint); + + if (stat(submission->fileName, &fileInfo) == 0) + { + centry.lastModDate = fileInfo.st_mtime; + if (!add_cache_entry(submission->bc->cache, ¢ry)) + { + printf("Failed to insert bitprint into cache.\n"); + } + } + #endif return true; |