tdb2: save open_flags instead of mmap_flags.
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 31 Aug 2011 04:26:15 +0000 (13:56 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 31 Aug 2011 04:26:15 +0000 (13:56 +0930)
It's more consistent with what tdb1 does, and slightly more encapsulated.

ccan/tdb2/io.c
ccan/tdb2/open.c
ccan/tdb2/private.h

index 4166cd4c67caf0475684fc03a0e9de86e714500e..d422d9632618fd47653878cd904a90b15645e878 100644 (file)
@@ -42,16 +42,23 @@ void tdb_munmap(struct tdb_file *file)
 
 void tdb_mmap(struct tdb_context *tdb)
 {
+       int mmap_flags;
+
        if (tdb->flags & TDB_INTERNAL)
                return;
 
        if (tdb->flags & TDB_NOMMAP)
                return;
 
+       if ((tdb->open_flags & O_ACCMODE) == O_RDONLY)
+               mmap_flags = PROT_READ;
+       else
+               mmap_flags = PROT_READ | PROT_WRITE;
+
        /* size_t can be smaller than off_t. */
        if ((size_t)tdb->file->map_size == tdb->file->map_size) {
                tdb->file->map_ptr = mmap(NULL, tdb->file->map_size,
-                                         tdb->mmap_flags,
+                                         mmap_flags,
                                          MAP_SHARED, tdb->file->fd, 0);
        } else
                tdb->file->map_ptr = MAP_FAILED;
index 96b5a5ea3f6a2b73b265e0f69ef7826aff27d207..83a83e0ba578b75e873c7900a140f08b7cf00b22 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;
@@ -416,11 +417,9 @@ 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;
        }
 
index 4da73723e3fe77e9e3fbf611dd9b36ef70dedfd5..3904442574a34b66d58bac1871e8d860ad18f2aa 100644 (file)
@@ -334,8 +334,8 @@ struct tdb_context {
        /* Operating read-only? (Opened O_RDONLY, or in traverse_read) */
        bool read_only;
 
-       /* mmap read only? */
-       int mmap_flags;
+       /* Open flags passed to tdb_open. */
+       int open_flags;
 
        /* the flags passed to tdb_open, for tdb_reopen. */
        uint32_t flags;