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 "../common/mutex.c"
13 #include "tap-interface.h"
16 static unsigned int tdb_dumb_hash(TDB_DATA
*key
)
21 static void log_fn(struct tdb_context
*tdb
, enum tdb_debug_level level
, const char *fmt
, ...)
23 unsigned int *count
= tdb_get_logging_private(tdb
);
24 if (strstr(fmt
, "hash"))
28 static unsigned int hdr_rwlocks(const char *fname
)
30 struct tdb_header hdr
;
32 int fd
= open(fname
, O_RDONLY
);
36 if (read(fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
43 int main(int argc
, char *argv
[])
45 struct tdb_context
*tdb
;
46 unsigned int log_count
, flags
;
48 struct tdb_logging_context log_ctx
= { log_fn
, &log_count
};
52 for (flags
= 0; flags
<= TDB_CONVERT
; flags
+= TDB_CONVERT
) {
53 unsigned int rwmagic
= TDB_HASH_RWLOCK_MAGIC
;
55 if (flags
& TDB_CONVERT
)
56 tdb_convert(&rwmagic
, sizeof(rwmagic
));
58 /* Create an old-style hash. */
60 tdb
= tdb_open_ex("run-incompatible.tdb", 0, flags
,
61 O_CREAT
|O_RDWR
|O_TRUNC
, 0600, &log_ctx
,
65 d
.dptr
= discard_const_p(uint8_t, "Hello");
67 ok1(tdb_store(tdb
, d
, d
, TDB_INSERT
) == 0);
70 /* Should not have marked rwlocks field. */
71 ok1(hdr_rwlocks("run-incompatible.tdb") == 0);
73 /* We can still open any old-style with incompat flag. */
75 tdb
= tdb_open_ex("run-incompatible.tdb", 0,
76 TDB_INCOMPATIBLE_HASH
,
77 O_RDWR
, 0600, &log_ctx
, NULL
);
80 r
= tdb_fetch(tdb
, d
);
83 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
87 tdb
= tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDONLY
,
88 0, &log_ctx
, tdb_jenkins_hash
);
91 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
95 tdb
= tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDONLY
,
96 0, &log_ctx
, tdb_jenkins_hash
);
99 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
102 /* OK, now create with incompatible flag, default hash. */
104 tdb
= tdb_open_ex("run-incompatible.tdb", 0,
105 flags
|TDB_INCOMPATIBLE_HASH
,
106 O_CREAT
|O_RDWR
|O_TRUNC
, 0600, &log_ctx
,
110 d
.dptr
= discard_const_p(uint8_t, "Hello");
112 ok1(tdb_store(tdb
, d
, d
, TDB_INSERT
) == 0);
115 /* Should have marked rwlocks field. */
116 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic
);
118 /* Cannot open with old hash. */
120 tdb
= tdb_open_ex("run-incompatible.tdb", 0, 0,
121 O_RDWR
, 0600, &log_ctx
, tdb_old_hash
);
125 /* Can open with jenkins hash. */
127 tdb
= tdb_open_ex("run-incompatible.tdb", 0, 0,
128 O_RDWR
, 0600, &log_ctx
, tdb_jenkins_hash
);
131 r
= tdb_fetch(tdb
, d
);
134 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
137 /* Can open by letting it figure it out itself. */
139 tdb
= tdb_open_ex("run-incompatible.tdb", 0, 0,
140 O_RDWR
, 0600, &log_ctx
, NULL
);
143 r
= tdb_fetch(tdb
, d
);
146 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
149 /* We can also use incompatible hash with other hashes. */
151 tdb
= tdb_open_ex("run-incompatible.tdb", 0,
152 flags
|TDB_INCOMPATIBLE_HASH
,
153 O_CREAT
|O_RDWR
|O_TRUNC
, 0600, &log_ctx
,
157 d
.dptr
= discard_const_p(uint8_t, "Hello");
159 ok1(tdb_store(tdb
, d
, d
, TDB_INSERT
) == 0);
162 /* Should have marked rwlocks field. */
163 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic
);
165 /* It should not open if we don't specify. */
167 tdb
= tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR
, 0,
172 /* Should reopen with correct hash. */
174 tdb
= tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR
, 0,
175 &log_ctx
, tdb_dumb_hash
);
178 r
= tdb_fetch(tdb
, d
);
181 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
185 return exit_status();