X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Fopen.c;h=e88c9e694c531b67bf4a6ff33d113a3b4078ad59;hp=37e20907fedfc1a9f512c449dea8b8e727ebcf9a;hb=5802573130c841d10734e1b0dcdb0b13167f9c86;hpb=66ead2bcb732aa53bde5092b34c16d780df779ea diff --git a/ccan/tdb2/open.c b/ccan/tdb2/open.c index 37e20907..e88c9e69 100644 --- a/ccan/tdb2/open.c +++ b/ccan/tdb2/open.c @@ -1,3 +1,20 @@ + /* + Trivial Database 2: opening and closing TDBs + 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 @@ -203,6 +220,12 @@ enum TDB_ERROR tdb_set_attribute(struct tdb_context *tdb, : attr->base.attr == TDB_ATTRIBUTE_SEED ? "TDB_ATTRIBUTE_SEED" : "TDB_ATTRIBUTE_OPENHOOK"); + case TDB_ATTRIBUTE_STATS: + return tdb->last_error + = tdb_logerr(tdb, TDB_ERR_EINVAL, + TDB_LOG_USE_ERROR, + "tdb_set_attribute:" + " cannot set TDB_ATTRIBUTE_STATS"); case TDB_ATTRIBUTE_FLOCK: tdb->lock_fn = attr->flock.lock; tdb->unlock_fn = attr->flock.unlock; @@ -252,9 +275,13 @@ enum TDB_ERROR tdb_get_attribute(struct tdb_context *tdb, TDB_LOG_USE_ERROR, "tdb_get_attribute:" " cannot get TDB_ATTRIBUTE_OPENHOOK"); - case TDB_ATTRIBUTE_STATS: - /* FIXME */ - return TDB_ERR_EINVAL; + case TDB_ATTRIBUTE_STATS: { + size_t size = attr->stats.size; + if (size > tdb->stats.size) + size = tdb->stats.size; + memcpy(&attr->stats, &tdb->stats, size); + break; + } case TDB_ATTRIBUTE_FLOCK: attr->flock.lock = tdb->lock_fn; attr->flock.unlock = tdb->unlock_fn; @@ -291,7 +318,10 @@ void tdb_unset_attribute(struct tdb_context *tdb, : "TDB_ATTRIBUTE_OPENHOOK"); break; case TDB_ATTRIBUTE_STATS: - /* FIXME */ + tdb_logerr(tdb, TDB_ERR_EINVAL, + TDB_LOG_USE_ERROR, + "tdb_unset_attribute:" + "cannot unset TDB_ATTRIBUTE_STATS"); break; case TDB_ATTRIBUTE_FLOCK: tdb->lock_fn = tdb_fcntl_lock; @@ -320,6 +350,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, struct tdb_attribute_openhook *openhook = NULL; tdb_bool_err berr; enum TDB_ERROR ecode; + int openlock; tdb = malloc(sizeof(*tdb) + (name ? strlen(name) + 1 : 0)); if (!tdb) { @@ -337,13 +368,15 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, tdb->flags = tdb_flags; tdb->log_fn = NULL; tdb->transaction = NULL; - tdb->stats = NULL; tdb->access = NULL; tdb->last_error = TDB_SUCCESS; tdb->file = NULL; tdb->lock_fn = tdb_fcntl_lock; tdb->unlock_fn = tdb_fcntl_unlock; tdb->hash_fn = jenkins_hash; + memset(&tdb->stats, 0, sizeof(tdb->stats)); + tdb->stats.base.attr = TDB_ATTRIBUTE_STATS; + tdb->stats.size = sizeof(tdb->stats); tdb_io_init(tdb); while (attr) { @@ -355,12 +388,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, case TDB_ATTRIBUTE_SEED: seed = &attr->seed; break; - case TDB_ATTRIBUTE_STATS: - tdb->stats = &attr->stats; - /* They have stats we don't know about? Tell them. */ - if (tdb->stats->size > sizeof(attr->stats)) - tdb->stats->size = sizeof(attr->stats); - break; case TDB_ATTRIBUTE_OPENHOOK: openhook = &attr->openhook; break; @@ -374,7 +401,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, } if (tdb_flags & ~(TDB_INTERNAL | TDB_NOLOCK | TDB_NOMMAP | TDB_CONVERT - | TDB_NOSYNC | TDB_SEQNUM)) { + | TDB_NOSYNC | TDB_SEQNUM | TDB_ALLOW_NESTING)) { ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR, "tdb_open: unknown flags %u", tdb_flags); goto fail; @@ -390,9 +417,11 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, if ((open_flags & O_ACCMODE) == O_RDONLY) { tdb->read_only = true; tdb->mmap_flags = PROT_READ; + openlock = F_RDLCK; } else { tdb->read_only = false; tdb->mmap_flags = PROT_READ | PROT_WRITE; + openlock = F_WRLCK; } /* internal databases don't need any of the rest. */ @@ -407,16 +436,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, if (ecode != TDB_SUCCESS) { goto fail; } - if (name) { - tdb->name = strdup(name); - if (!tdb->name) { - ecode = tdb_logerr(tdb, TDB_ERR_OOM, - TDB_LOG_ERROR, - "tdb_open: failed to" - " allocate name"); - goto fail; - } - } tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed)); tdb->hash_seed = hdr.hash_seed; tdb_ftable_init(tdb); @@ -447,12 +466,15 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, "tdb_open: could not stat open %s: %s", name, strerror(errno)); + close(fd); goto fail_errno; } ecode = tdb_new_file(tdb); - if (ecode != TDB_SUCCESS) + if (ecode != TDB_SUCCESS) { + close(fd); goto fail; + } tdb->file->next = files; tdb->file->fd = fd; @@ -463,7 +485,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, } /* ensure there is only one process initialising at once */ - ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK); + ecode = tdb_lock_open(tdb, openlock, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK); if (ecode != TDB_SUCCESS) { saved_errno = errno; goto fail_errno; @@ -525,13 +547,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, goto fail; } - tdb->name = strdup(name); - if (!tdb->name) { - ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR, - "tdb_open: failed to allocate name"); - goto fail; - } - /* Clear any features we don't understand. */ if ((open_flags & O_ACCMODE) != O_RDONLY) { hdr.features_used &= TDB_FEATURE_MASK; @@ -542,7 +557,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, goto fail; } - tdb_unlock_open(tdb); + tdb_unlock_open(tdb, openlock); /* This make sure we have current map_size and mmap. */ tdb->methods->oob(tdb, tdb->file->map_size + 1, true);