tdb2: add TDB_RDONLY flag, allow setting/unsetting it.
[ccan] / ccan / tdb2 / open.c
index 1292fcc1ee32ee59431284527706461696096211..68f707b920ec8cf72bcf66b9788c1abd25bc26c0 100644 (file)
@@ -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 <http://www.gnu.org/licenses/>.
+*/
 #include "private.h"
 #include <ccan/hash/hash.h>
 #include <assert.h>
 #include "private.h"
 #include <ccan/hash/hash.h>
 #include <assert.h>
@@ -333,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;
        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) {
 
        tdb = malloc(sizeof(*tdb) + (name ? strlen(name) + 1 : 0));
        if (!tdb) {
@@ -351,6 +369,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        tdb->log_fn = NULL;
        tdb->transaction = NULL;
        tdb->access = NULL;
        tdb->log_fn = NULL;
        tdb->transaction = NULL;
        tdb->access = NULL;
+       tdb->open_flags = open_flags;
        tdb->last_error = TDB_SUCCESS;
        tdb->file = NULL;
        tdb->lock_fn = tdb_fcntl_lock;
        tdb->last_error = TDB_SUCCESS;
        tdb->file = NULL;
        tdb->lock_fn = tdb_fcntl_lock;
@@ -383,7 +402,8 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        }
 
        if (tdb_flags & ~(TDB_INTERNAL | TDB_NOLOCK | TDB_NOMMAP | TDB_CONVERT
        }
 
        if (tdb_flags & ~(TDB_INTERNAL | TDB_NOLOCK | TDB_NOMMAP | TDB_CONVERT
-                         | TDB_NOSYNC | TDB_SEQNUM)) {
+                         | TDB_NOSYNC | TDB_SEQNUM | TDB_ALLOW_NESTING
+                         | TDB_RDONLY)) {
                ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
                                   "tdb_open: unknown flags %u", tdb_flags);
                goto fail;
                ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
                                   "tdb_open: unknown flags %u", tdb_flags);
                goto fail;
@@ -397,11 +417,17 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        }
 
        if ((open_flags & O_ACCMODE) == O_RDONLY) {
        }
 
        if ((open_flags & O_ACCMODE) == O_RDONLY) {
-               tdb->read_only = true;
-               tdb->mmap_flags = PROT_READ;
+               openlock = F_RDLCK;
+               tdb->flags |= TDB_RDONLY;
        } else {
        } else {
-               tdb->read_only = false;
-               tdb->mmap_flags = PROT_READ | PROT_WRITE;
+               if (tdb_flags & TDB_RDONLY) {
+                       ecode = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                          TDB_LOG_USE_ERROR,
+                                          "tdb_open: can't use TDB_RDONLY"
+                                          " without O_RDONLY");
+                       goto fail;
+               }
+               openlock = F_WRLCK;
        }
 
        /* internal databases don't need any of the rest. */
        }
 
        /* internal databases don't need any of the rest. */
@@ -446,12 +472,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));
                        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);
                        goto fail_errno;
                }
 
                ecode = tdb_new_file(tdb);
-               if (ecode != TDB_SUCCESS)
+               if (ecode != TDB_SUCCESS) {
+                       close(fd);
                        goto fail;
                        goto fail;
+               }
 
                tdb->file->next = files;
                tdb->file->fd = fd;
 
                tdb->file->next = files;
                tdb->file->fd = fd;
@@ -462,7 +491,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        }
 
        /* ensure there is only one process initialising at once */
        }
 
        /* 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;
        if (ecode != TDB_SUCCESS) {
                saved_errno = errno;
                goto fail_errno;
@@ -509,6 +538,12 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                                           name, (long long)hdr.version);
                        goto fail;
                }
                                           name, (long long)hdr.version);
                        goto fail;
                }
+       } else if (tdb->flags & TDB_CONVERT) {
+               ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
+                                  "tdb_open:"
+                                  " %s does not need TDB_CONVERT",
+                                  name);
+               goto fail;
        }
 
        tdb_convert(tdb, &hdr, sizeof(hdr));
        }
 
        tdb_convert(tdb, &hdr, sizeof(hdr));
@@ -527,17 +562,20 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        /* Clear any features we don't understand. */
        if ((open_flags & O_ACCMODE) != O_RDONLY) {
                hdr.features_used &= TDB_FEATURE_MASK;
        /* Clear any features we don't understand. */
        if ((open_flags & O_ACCMODE) != O_RDONLY) {
                hdr.features_used &= TDB_FEATURE_MASK;
-               if (tdb_write_convert(tdb, offsetof(struct tdb_header,
-                                                   features_used),
-                                     &hdr.features_used,
-                                     sizeof(hdr.features_used)) == -1)
+               ecode = tdb_write_convert(tdb, offsetof(struct tdb_header,
+                                                       features_used),
+                                         &hdr.features_used,
+                                         sizeof(hdr.features_used));
+               if (ecode != TDB_SUCCESS)
                        goto fail;
        }
 
                        goto fail;
        }
 
-       tdb_unlock_open(tdb);
+       tdb_unlock_open(tdb, openlock);
 
        /* This make sure we have current map_size and mmap. */
 
        /* This make sure we have current map_size and mmap. */
-       tdb->methods->oob(tdb, tdb->file->map_size + 1, true);
+       ecode = tdb->methods->oob(tdb, tdb->file->map_size + 1, true);
+       if (unlikely(ecode != TDB_SUCCESS))
+               goto fail;
 
        /* Now it's fully formed, recover if necessary. */
        berr = tdb_needs_recovery(tdb);
 
        /* Now it's fully formed, recover if necessary. */
        berr = tdb_needs_recovery(tdb);