X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftdb2%2Ftdb.c;h=bb181732c90a541f832c5f6ea936d218ff7f1c1a;hb=444fade529f68eb2b0aebbc8de442478c5c6f916;hp=09333a26801380fb232a2fc444eb11473de8805e;hpb=f367a1dc8573db0a1f6f49fcda474eb51c192241;p=ccan
diff --git a/ccan/tdb2/tdb.c b/ccan/tdb2/tdb.c
index 09333a26..bb181732 100644
--- a/ccan/tdb2/tdb.c
+++ b/ccan/tdb2/tdb.c
@@ -1,3 +1,20 @@
+ /*
+ Trivial Database 2: fetch, store and misc routines.
+ Copyright (C) Rusty Russell 2010
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 3 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, see .
+*/
#include "private.h"
#include
#include
@@ -38,10 +55,11 @@ static enum TDB_ERROR replace_data(struct tdb_context *tdb,
/* We didn't like the existing one: remove it. */
if (old_off) {
- add_stat(tdb, frees, 1);
+ tdb->stats.frees++;
ecode = add_free_record(tdb, old_off,
sizeof(struct tdb_used_record)
- + key.dsize + old_room);
+ + key.dsize + old_room,
+ TDB_LOCK_WAIT, true);
if (ecode == TDB_SUCCESS)
ecode = replace_in_hash(tdb, h, new_off);
} else {
@@ -285,12 +303,13 @@ enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
}
/* Free the deleted entry. */
- add_stat(tdb, frees, 1);
+ tdb->stats.frees++;
ecode = add_free_record(tdb, off,
sizeof(struct tdb_used_record)
+ rec_key_length(&rec)
+ rec_data_length(&rec)
- + rec_extra_padding(&rec));
+ + rec_extra_padding(&rec),
+ TDB_LOCK_WAIT, true);
if (tdb->flags & TDB_SEQNUM)
tdb_inc_seqnum(tdb);
@@ -305,6 +324,29 @@ unsigned int tdb_get_flags(struct tdb_context *tdb)
return tdb->flags;
}
+static bool readonly_changable(struct tdb_context *tdb, const char *caller)
+{
+ if (tdb->transaction) {
+ tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+ TDB_LOG_USE_ERROR,
+ "%s: can't change"
+ " TDB_RDONLY inside transaction",
+ caller);
+ return false;
+ }
+
+ if (tdb->file->allrecord_lock.count != 0
+ || tdb->file->num_lockrecs != 0) {
+ tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+ TDB_LOG_USE_ERROR,
+ "%s: can't change"
+ " TDB_RDONLY holding locks",
+ caller);
+ return false;
+ }
+ return true;
+}
+
void tdb_add_flag(struct tdb_context *tdb, unsigned flag)
{
if (tdb->flags & TDB_INTERNAL) {
@@ -327,6 +369,13 @@ void tdb_add_flag(struct tdb_context *tdb, unsigned flag)
case TDB_SEQNUM:
tdb->flags |= TDB_SEQNUM;
break;
+ case TDB_ALLOW_NESTING:
+ tdb->flags |= TDB_ALLOW_NESTING;
+ break;
+ case TDB_RDONLY:
+ if (readonly_changable(tdb, "tdb_add_flag"))
+ tdb->flags |= TDB_RDONLY;
+ break;
default:
tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
TDB_LOG_USE_ERROR,
@@ -357,6 +406,21 @@ void tdb_remove_flag(struct tdb_context *tdb, unsigned flag)
case TDB_SEQNUM:
tdb->flags &= ~TDB_SEQNUM;
break;
+ case TDB_ALLOW_NESTING:
+ tdb->flags &= ~TDB_ALLOW_NESTING;
+ break;
+ case TDB_RDONLY:
+ if ((tdb->open_flags & O_ACCMODE) == O_RDONLY) {
+ tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+ TDB_LOG_USE_ERROR,
+ "tdb_remove_flag: can't"
+ " remove TDB_RDONLY on tdb"
+ " opened with O_RDONLY");
+ break;
+ }
+ if (readonly_changable(tdb, "tdb_remove_flag"))
+ tdb->flags &= ~TDB_RDONLY;
+ break;
default:
tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
TDB_LOG_USE_ERROR,
@@ -382,6 +446,11 @@ const char *tdb_errorstr(enum TDB_ERROR ecode)
return "Invalid error code";
}
+enum TDB_ERROR tdb_error(struct tdb_context *tdb)
+{
+ return tdb->last_error;
+}
+
enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
enum TDB_ERROR ecode,
enum tdb_log_level level,
@@ -393,7 +462,7 @@ enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
/* tdb_open paths care about errno, so save it. */
int saved_errno = errno;
- if (!tdb->logfn)
+ if (!tdb->log_fn)
return ecode;
va_start(ap, fmt);
@@ -401,11 +470,11 @@ enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
va_end(ap);
if (len < 0) {
- tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private,
- "out of memory formatting message:");
- tdb->logfn(tdb, level, tdb->log_private, fmt);
+ tdb->log_fn(tdb, TDB_LOG_ERROR, TDB_ERR_OOM,
+ "out of memory formatting message:", tdb->log_data);
+ tdb->log_fn(tdb, level, ecode, fmt, tdb->log_data);
} else {
- tdb->logfn(tdb, level, tdb->log_private, message);
+ tdb->log_fn(tdb, level, ecode, message, tdb->log_data);
free(message);
}
errno = saved_errno;
@@ -414,10 +483,10 @@ enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb,
TDB_DATA key,
- enum TDB_ERROR (*parse)(TDB_DATA key,
- TDB_DATA data,
- void *p),
- void *p)
+ enum TDB_ERROR (*parse)(TDB_DATA k,
+ TDB_DATA d,
+ void *data),
+ void *data)
{
tdb_off_t off;
struct tdb_used_record rec;
@@ -438,9 +507,9 @@ enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb,
if (TDB_PTR_IS_ERR(dptr)) {
ecode = TDB_PTR_ERR(dptr);
} else {
- TDB_DATA data = tdb_mkdata(dptr, rec_data_length(&rec));
+ TDB_DATA d = tdb_mkdata(dptr, rec_data_length(&rec));
- ecode = parse(key, data, p);
+ ecode = parse(key, d, data);
tdb_access_release(tdb, dptr);
}
}