]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/tdb.c
tdb2: add TDB_ATTRIBUTE_TDB1_HASHSIZE
[ccan] / ccan / tdb2 / tdb.c
index d7b5163be95eba66ac204f1be75dc239350c41f9..329a643f5145ac6c452fd2c08e56359a52e405dc 100644 (file)
@@ -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 <http://www.gnu.org/licenses/>.
+*/
 #include "private.h"
 #include <ccan/asprintf/asprintf.h>
 #include <stdarg.h>
@@ -42,7 +59,7 @@ static enum TDB_ERROR replace_data(struct tdb_context *tdb,
                ecode = add_free_record(tdb, old_off,
                                        sizeof(struct tdb_used_record)
                                        + key.dsize + old_room,
-                                       TDB_LOCK_WAIT);
+                                       TDB_LOCK_WAIT, true);
                if (ecode == TDB_SUCCESS)
                        ecode = replace_in_hash(tdb, h, new_off);
        } else {
@@ -53,13 +70,13 @@ static enum TDB_ERROR replace_data(struct tdb_context *tdb,
        }
 
        new_off += sizeof(struct tdb_used_record);
-       ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize);
+       ecode = tdb->tdb2.io->twrite(tdb, new_off, key.dptr, key.dsize);
        if (ecode != TDB_SUCCESS) {
                return ecode;
        }
 
        new_off += key.dsize;
-       ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
+       ecode = tdb->tdb2.io->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
        if (ecode != TDB_SUCCESS) {
                return ecode;
        }
@@ -77,10 +94,10 @@ static enum TDB_ERROR update_data(struct tdb_context *tdb,
 {
        enum TDB_ERROR ecode;
 
-       ecode = tdb->methods->twrite(tdb, off, dbuf.dptr, dbuf.dsize);
+       ecode = tdb->tdb2.io->twrite(tdb, off, dbuf.dptr, dbuf.dsize);
        if (ecode == TDB_SUCCESS && extra) {
                /* Put a zero in; future versions may append other data. */
-               ecode = tdb->methods->twrite(tdb, off + dbuf.dsize, "", 1);
+               ecode = tdb->tdb2.io->twrite(tdb, off + dbuf.dsize, "", 1);
        }
        if (tdb->flags & TDB_SEQNUM)
                tdb_inc_seqnum(tdb);
@@ -194,7 +211,7 @@ enum TDB_ERROR tdb_append(struct tdb_context *tdb,
                                                    + dbuf.dsize));
                        goto out;
                }
-               ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
+               ecode = tdb->tdb2.io->tread(tdb, off + sizeof(rec) + key.dsize,
                                            newdata, old_dlen);
                if (ecode != TDB_SUCCESS) {
                        goto out_free_newdata;
@@ -292,7 +309,7 @@ enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
                                + rec_key_length(&rec)
                                + rec_data_length(&rec)
                                + rec_extra_padding(&rec),
-                               TDB_LOCK_WAIT);
+                               TDB_LOCK_WAIT, true);
 
        if (tdb->flags & TDB_SEQNUM)
                tdb_inc_seqnum(tdb);
@@ -307,6 +324,37 @@ unsigned int tdb_get_flags(struct tdb_context *tdb)
        return tdb->flags;
 }
 
+static bool inside_transaction(const struct tdb_context *tdb)
+{
+       if (tdb->flags & TDB_VERSION1)
+               return tdb->tdb1.transaction != NULL;
+       else
+               return tdb->tdb2.transaction != NULL;
+}
+
+static bool readonly_changable(struct tdb_context *tdb, const char *caller)
+{
+       if (inside_transaction(tdb)) {
+               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) {
@@ -332,6 +380,10 @@ void tdb_add_flag(struct tdb_context *tdb, unsigned flag)
        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,
@@ -365,6 +417,18 @@ void tdb_remove_flag(struct tdb_context *tdb, unsigned flag)
        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,
@@ -414,11 +478,11 @@ enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
        va_end(ap);
 
        if (len < 0) {
-               tdb->log_fn(tdb, TDB_LOG_ERROR,
+               tdb->log_fn(tdb, TDB_LOG_ERROR, TDB_ERR_OOM,
                            "out of memory formatting message:", tdb->log_data);
-               tdb->log_fn(tdb, level, fmt, tdb->log_data);
+               tdb->log_fn(tdb, level, ecode, fmt, tdb->log_data);
        } else {
-               tdb->log_fn(tdb, level, message, tdb->log_data);
+               tdb->log_fn(tdb, level, ecode, message, tdb->log_data);
                free(message);
        }
        errno = saved_errno;