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
;
33 int fd
= open(fname
, O_RDONLY
);
37 nread
= read(fd
, &hdr
, sizeof(hdr
));
39 if (nread
!= sizeof(hdr
)) {
45 int main(int argc
, char *argv
[])
47 struct tdb_context
*tdb
;
48 unsigned int log_count
, flags
;
50 struct tdb_logging_context log_ctx
= { log_fn
, &log_count
};
54 for (flags
= 0; flags
<= TDB_CONVERT
; flags
+= TDB_CONVERT
) {
55 unsigned int rwmagic
= TDB_HASH_RWLOCK_MAGIC
;
57 if (flags
& TDB_CONVERT
)
58 tdb_convert(&rwmagic
, sizeof(rwmagic
));
60 /* Create an old-style hash. */
62 tdb
= tdb_open_ex("run-incompatible.tdb", 0, flags
,
63 O_CREAT
|O_RDWR
|O_TRUNC
, 0600, &log_ctx
,
67 d
.dptr
= discard_const_p(uint8_t, "Hello");
69 ok1(tdb_store(tdb
, d
, d
, TDB_INSERT
) == 0);
72 /* Should not have marked rwlocks field. */
73 ok1(hdr_rwlocks("run-incompatible.tdb") == 0);
75 /* We can still open any old-style with incompat flag. */
77 tdb
= tdb_open_ex("run-incompatible.tdb", 0,
78 TDB_INCOMPATIBLE_HASH
,
79 O_RDWR
, 0600, &log_ctx
, NULL
);
82 r
= tdb_fetch(tdb
, d
);
85 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
89 tdb
= tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDONLY
,
90 0, &log_ctx
, tdb_jenkins_hash
);
93 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
97 tdb
= tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDONLY
,
98 0, &log_ctx
, tdb_jenkins_hash
);
101 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
104 /* OK, now create with incompatible flag, default hash. */
106 tdb
= tdb_open_ex("run-incompatible.tdb", 0,
107 flags
|TDB_INCOMPATIBLE_HASH
,
108 O_CREAT
|O_RDWR
|O_TRUNC
, 0600, &log_ctx
,
112 d
.dptr
= discard_const_p(uint8_t, "Hello");
114 ok1(tdb_store(tdb
, d
, d
, TDB_INSERT
) == 0);
117 /* Should have marked rwlocks field. */
118 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic
);
120 /* Cannot open with old hash. */
122 tdb
= tdb_open_ex("run-incompatible.tdb", 0, 0,
123 O_RDWR
, 0600, &log_ctx
, tdb_old_hash
);
127 /* Can open with jenkins hash. */
129 tdb
= tdb_open_ex("run-incompatible.tdb", 0, 0,
130 O_RDWR
, 0600, &log_ctx
, tdb_jenkins_hash
);
133 r
= tdb_fetch(tdb
, d
);
136 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
139 /* Can open by letting it figure it out itself. */
141 tdb
= tdb_open_ex("run-incompatible.tdb", 0, 0,
142 O_RDWR
, 0600, &log_ctx
, NULL
);
145 r
= tdb_fetch(tdb
, d
);
148 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
151 /* We can also use incompatible hash with other hashes. */
153 tdb
= tdb_open_ex("run-incompatible.tdb", 0,
154 flags
|TDB_INCOMPATIBLE_HASH
,
155 O_CREAT
|O_RDWR
|O_TRUNC
, 0600, &log_ctx
,
159 d
.dptr
= discard_const_p(uint8_t, "Hello");
161 ok1(tdb_store(tdb
, d
, d
, TDB_INSERT
) == 0);
164 /* Should have marked rwlocks field. */
165 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic
);
167 /* It should not open if we don't specify. */
169 tdb
= tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR
, 0,
174 /* Should reopen with correct hash. */
176 tdb
= tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR
, 0,
177 &log_ctx
, tdb_dumb_hash
);
180 r
= tdb_fetch(tdb
, d
);
183 ok1(tdb_check(tdb
, NULL
, NULL
) == 0);
187 return exit_status();