1 #include "../common/tdb_private.h"
2 #include "../common/io.c"
3 #include "../common/tdb.c"
4 #include "../common/lock.c"
5 #include "../common/freelist.c"
6 #include "../common/traverse.c"
7 #include "../common/transaction.c"
8 #include "../common/error.c"
9 #include "../common/open.c"
10 #include "../common/check.c"
11 #include "../common/hash.c"
12 #include "tap-interface.h"
15 static unsigned int tdb_dumb_hash(TDB_DATA
*key
)
20 static void log_fn(struct tdb_context
*tdb
, enum tdb_debug_level level
, const char *fmt
, ...)
22 unsigned int *count
= tdb_get_logging_private(tdb
);
23 if (strstr(fmt
, "hash"))
27 static unsigned int hdr_rwlocks(const char *fname
)
29 struct tdb_header hdr
;
31 int fd
= open(fname
, O_RDONLY
);
35 if (read(fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
42 int main(int argc
, char *argv
[])
44 struct tdb_context
*tdb
;
45 unsigned int log_count
, flags
;
47 struct tdb_logging_context log_ctx
= { log_fn
, &log_count
};
51 for (flags
= 0; flags
<= TDB_CONVERT
; flags
+= TDB_CONVERT
) {
52 unsigned int rwmagic
= TDB_HASH_RWLOCK_MAGIC
;
54 if (flags
& TDB_CONVERT
)
55 tdb_convert(&rwmagic
, sizeof(rwmagic
));
57 /* Create an old-style hash. */
59 tdb
= tdb_open_ex("run-incompatible.tdb", 0, flags
,
60 O_CREAT
|O_RDWR
|O_TRUNC
, 0600, &log_ctx
,
64 d
.dptr
= (void *)"Hello";
66 ok1(tdb_store(tdb
, d
, d
, TDB_INSERT
) == 0);
69 /* Should not have marked rwlocks field. */
70 ok1(hdr_rwlocks("run-incompatible.tdb") == 0);
72 /* We can still open any old-style with incompat flag. */
74 tdb
= tdb_open_ex("run-incompatible.tdb", 0,
75 TDB_INCOMPATIBLE_HASH
,
76 O_RDWR
, 0600, &log_ctx
, NULL
);
79 r
= tdb_fetch(tdb
, d
);
82 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
86 tdb
= tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDONLY
,
87 0, &log_ctx
, tdb_jenkins_hash
);
90 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
94 tdb
= tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDONLY
,
95 0, &log_ctx
, tdb_jenkins_hash
);
98 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
101 /* OK, now create with incompatible flag, default hash. */
103 tdb
= tdb_open_ex("run-incompatible.tdb", 0,
104 flags
|TDB_INCOMPATIBLE_HASH
,
105 O_CREAT
|O_RDWR
|O_TRUNC
, 0600, &log_ctx
,
109 d
.dptr
= (void *)"Hello";
111 ok1(tdb_store(tdb
, d
, d
, TDB_INSERT
) == 0);
114 /* Should have marked rwlocks field. */
115 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic
);
117 /* Cannot open with old hash. */
119 tdb
= tdb_open_ex("run-incompatible.tdb", 0, 0,
120 O_RDWR
, 0600, &log_ctx
, tdb_old_hash
);
124 /* Can open with jenkins hash. */
126 tdb
= tdb_open_ex("run-incompatible.tdb", 0, 0,
127 O_RDWR
, 0600, &log_ctx
, tdb_jenkins_hash
);
130 r
= tdb_fetch(tdb
, d
);
133 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
136 /* Can open by letting it figure it out itself. */
138 tdb
= tdb_open_ex("run-incompatible.tdb", 0, 0,
139 O_RDWR
, 0600, &log_ctx
, NULL
);
142 r
= tdb_fetch(tdb
, d
);
145 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
148 /* We can also use incompatible hash with other hashes. */
150 tdb
= tdb_open_ex("run-incompatible.tdb", 0,
151 flags
|TDB_INCOMPATIBLE_HASH
,
152 O_CREAT
|O_RDWR
|O_TRUNC
, 0600, &log_ctx
,
156 d
.dptr
= (void *)"Hello";
158 ok1(tdb_store(tdb
, d
, d
, TDB_INSERT
) == 0);
161 /* Should have marked rwlocks field. */
162 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic
);
164 /* It should not open if we don't specify. */
166 tdb
= tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR
, 0,
171 /* Should reopen with correct hash. */
173 tdb
= tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR
, 0,
174 &log_ctx
, tdb_dumb_hash
);
177 r
= tdb_fetch(tdb
, d
);
180 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
184 return exit_status();