lib/tdb: don't overwrite TDBs with different version numbers.
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 28 Aug 2009 02:26:34 +0000 (11:56 +0930)
committerAndrew Tridgell <tridge@samba.org>
Fri, 28 Aug 2009 03:43:05 +0000 (13:43 +1000)
In future, this may happen, and we don't want to clobber them.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
lib/tdb/common/open.c

index 2e6a707497b85003159d7a02a7fa278859e64df8..141e6fe517da72cbcb39c20ce3cb03c56854e81d 100644 (file)
@@ -240,17 +240,19 @@ struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
 
        errno = 0;
        if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
-           || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0
-           || (tdb->header.version != TDB_VERSION
-               && !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION))))) {
-               /* its not a valid database - possibly initialise it */
+           || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0) {
                if (!(open_flags & O_CREAT) || tdb_new_database(tdb, hash_size) == -1) {
                        if (errno == 0) {
-                       errno = EIO; /* ie bad format or something */
+                               errno = EIO; /* ie bad format or something */
                        }
                        goto fail;
                }
                rev = (tdb->flags & TDB_CONVERT);
+       } else if (tdb->header.version != TDB_VERSION
+                  && !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION)))) {
+               /* wrong version */
+               errno = EIO;
+               goto fail;
        }
        vp = (unsigned char *)&tdb->header.version;
        vertest = (((uint32_t)vp[0]) << 24) | (((uint32_t)vp[1]) << 16) |