aboutsummaryrefslogtreecommitdiff
path: root/lib/cache.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/cache.c')
-rw-r--r--lib/cache.c106
1 files changed, 54 insertions, 52 deletions
diff --git a/lib/cache.c b/lib/cache.c
index 0abd7141..b15cbf9a 100644
--- a/lib/cache.c
+++ b/lib/cache.c
@@ -22,15 +22,14 @@
#include <mailutils/mailutils.h>
#include "libmf.h"
#include "mfdb.h"
-#include "mf-dbm.h"
#include "filenames.h"
time_t negative_expire_interval;//FIXME
time_t tempfail_expire_interval = 37*60;/* FIXME: Equals to the sum of the
``hard'' timeouts */
-static void cache_print_item(const char *email, size_t size,
- const void *content, size_t contsize);
+static void cache_print_item(struct mu_dbm_datum const *key,
+ struct mu_dbm_datum const *val);
static int cache_expire_item(const void *content);
static struct db_format cache_format_struct = {
@@ -69,36 +68,33 @@ mf_status
cache_get(const char *email)
{
mf_status rc;
- MF_DBM_FILE db;
- MF_DBM_DATUM key;
- MF_DBM_DATUM contents;
+ mu_dbm_file_t db;
+ struct mu_dbm_datum key;
+ struct mu_dbm_datum contents;
+ int result;
int readonly = 0;
- int res;
-
+
if (!cache_format->enabled)
return mf_failure;
-
+
mu_debug(cache_format->debug_handle, MU_DEBUG_TRACE5,
("getting cache info for %s", email));
- if (mf_dbm_open(cache_format->dbname, &db, MU_STREAM_RDWR, 0600,
- &readonly)) {
- mu_error(_("mf_dbm_open(%s) failed: %s"), cache_format->dbname,
- mf_dbm_strerror());
- return mf_failure;
+ db = mf_dbm_open(cache_format->dbname, MU_STREAM_RDWR, 0600);
+ if (!db) {
+ db = mf_dbm_open(cache_format->dbname, MU_STREAM_READ, 0600);
+ if (!db)
+ return mf_failure;
+ readonly = 1;
}
- if (readonly)
- mu_debug(cache_format->debug_handle, MU_DEBUG_TRACE0,
- ("cannot lock %s: switching to read-only mode",
- cache_format->dbname));
memset (&key, 0, sizeof key);
memset (&contents, 0, sizeof contents);
- MF_DATUM_PTR (key) = (void*) email;
- MF_DATUM_SIZE (key) = strlen (email) + 1;
- if ((res = mf_dbm_fetch(&db, key, &contents)) == 0) {
+ key.mu_dptr = (void*) email;
+ key.mu_dsize = strlen (email) + 1;
+ if ((result = mu_dbm_fetch(db, &key, &contents)) == 0) {
char timebuf[80];
struct cache_result *res = (struct cache_result *)
- MF_DATUM_PTR(contents);
+ contents.mu_dptr;
time_t t = time(NULL);
mu_debug(cache_format->debug_handle, MU_DEBUG_TRACE5,
("found status: %s (%d), time: %s",
@@ -112,34 +108,40 @@ cache_get(const char *email)
MU_DEBUG_TRACE5,
("removing expired entry for %s",
email));
- if (mf_dbm_delete(&db, key))
+ result = mu_dbm_delete(db, &key);
+ if (result)
mu_error(_("cannot remove record `%s' from `%s': %s"),
- email, db.name,
- mf_dbm_strerror());
+ email, cache_format->dbname,
+ result == MU_ERR_FAILURE ?
+ mu_dbm_strerror(db) :
+ mu_strerror(result));
+
}
rc = mf_failure;
} else {
rc = res->status;
}
} else {
- if (res != MU_ERR_NOENT)
+ if (result == MU_ERR_FAILURE)
mu_error(_("cannot fetch record `%s' from `%s': %s"),
- email, db.name, mf_dbm_strerror());
+ email, cache_format->dbname,
+ mu_dbm_strerror(db));
rc = mf_failure;
}
- mf_dbm_datum_free(&contents);
- mf_dbm_close(&db);
+ mu_dbm_datum_free(&contents);
+ mu_dbm_destroy(&db);
return rc;
}
void
cache_insert(const char *email, mf_status rc)
{
- MF_DBM_FILE db;
- MF_DBM_DATUM key;
- MF_DBM_DATUM contents;
+ mu_dbm_file_t db;
+ struct mu_dbm_datum key;
+ struct mu_dbm_datum contents;
struct cache_result res;
char timebuf[80];
+ int result;
if (!cache_format->enabled)
return;
@@ -150,34 +152,34 @@ cache_insert(const char *email, mf_status rc)
("inserting cache info for %s. status=%s (%d), time=%s",
email, mf_status_str(rc), rc,
format_timestr(res.timestamp, timebuf, sizeof timebuf)));
- if (mf_dbm_open(cache_format->dbname, &db, MU_STREAM_RDWR,
- 0600, NULL)) {
- mu_error(_("mf_dbm_open(%s) failed: %s"), cache_format->dbname,
- mf_dbm_strerror());
+ db = mf_dbm_open(cache_format->dbname, MU_STREAM_RDWR, 0600);
+ if (!db)
return;
- }
-
+
memset(&key, 0, sizeof key);
memset(&contents, 0, sizeof contents);
- MF_DATUM_PTR(key) = (void*) email;
- MF_DATUM_SIZE(key) = strlen (email) + 1;
- MF_DATUM_PTR(contents) = (void*)&res;
- MF_DATUM_SIZE(contents) = sizeof(res);
- if (mf_dbm_insert(&db, key, contents, 1))
- mu_error(_("cannot insert datum `%s' into `%s': %s"),
- email, db.name, mf_dbm_strerror());
+ key.mu_dptr = (void*) email;
+ key.mu_dsize = strlen (email) + 1;
+ contents.mu_dptr = (void*)&res;
+ contents.mu_dsize = sizeof(res);
+ result = mu_dbm_store(db, &key, &contents, 1);
+ if (result)
+ mu_error(_("cannot store datum `%s' into `%s': %s"),
+ email, cache_format->dbname,
+ result == MU_ERR_FAILURE ?
+ mu_dbm_strerror(db) : mu_strerror(result));
- mf_dbm_close(&db);
+ mu_dbm_destroy(&db);
}
static void
-cache_print_item(const char *email, size_t size, const void *content,
- size_t contsize)
+cache_print_item(struct mu_dbm_datum const *key,
+ struct mu_dbm_datum const *val)
{
- const struct cache_result *res = content;
-
- size--; /* Size includes the trailing nul */
- printf("%*.*s", size, size, email);
+ const struct cache_result *res = (const struct cache_result *)
+ val->mu_dptr;
+ int size = key->mu_dsize - 1; /* Size includes the trailing nul */
+ printf("%*.*s", size, size, key->mu_dptr);
printf(" %10.10s ", mf_status_str(res->status));
format_time_str(stdout, res->timestamp);
putchar('\n');

Return to:

Send suggestions and report system problems to the System administrator.