tdb2: add TDB_RDONLY flag, allow setting/unsetting it.
[ccan] / ccan / tdb2 / open.c
index fec3ef1596a562c90c6c40588265b0e05c93ed20..68f707b920ec8cf72bcf66b9788c1abd25bc26c0 100644 (file)
@@ -369,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->open_flags = open_flags;
        tdb->last_error = TDB_SUCCESS;
        tdb->file = NULL;
        tdb->lock_fn = tdb_fcntl_lock;
@@ -401,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
-                         | TDB_NOSYNC | TDB_SEQNUM | TDB_ALLOW_NESTING)) {
+                         | 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;
@@ -415,12 +417,16 @@ 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;
+               tdb->flags |= TDB_RDONLY;
        } 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;
        }
 
@@ -532,6 +538,12 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                                           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));