smbd: Remove a confusing comment
[Samba.git] / lib / tdb / test / run-3G-file.c
blob748c972284a788059ec7e615a84726b884f2768d
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"
14 #include <stdlib.h>
15 #include "logging.h"
17 static int tdb_expand_file_sparse(struct tdb_context *tdb,
18 tdb_off_t size,
19 tdb_off_t addition)
21 if (tdb->read_only || tdb->traverse_read) {
22 tdb->ecode = TDB_ERR_RDONLY;
23 return -1;
26 if (tdb_ftruncate(tdb, size+addition) == -1) {
27 char b = 0;
28 ssize_t written = tdb_pwrite(tdb, &b, 1, (size+addition) - 1);
29 if (written == 0) {
30 /* try once more, potentially revealing errno */
31 written = tdb_pwrite(tdb, &b, 1, (size+addition) - 1);
33 if (written == 0) {
34 /* again - give up, guessing errno */
35 errno = ENOSPC;
37 if (written != 1) {
38 TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file to %d failed (%s)\n",
39 size+addition, strerror(errno)));
40 return -1;
44 return 0;
47 static const struct tdb_methods large_io_methods = {
48 tdb_read,
49 tdb_write,
50 tdb_next_hash_chain,
51 tdb_oob,
52 tdb_expand_file_sparse
55 static int test_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
56 void *_data)
58 TDB_DATA *expect = _data;
59 ok1(key.dsize == strlen("hi"));
60 ok1(memcmp(key.dptr, "hi", strlen("hi")) == 0);
61 ok1(data.dsize == expect->dsize);
62 ok1(memcmp(data.dptr, expect->dptr, data.dsize) == 0);
63 return 0;
66 int main(int argc, char *argv[])
68 struct tdb_context *tdb;
69 TDB_DATA key, orig_data, data;
70 uint32_t hashval;
71 tdb_off_t rec_ptr;
72 struct tdb_record rec;
73 int ret;
75 plan_tests(24);
76 tdb = tdb_open_ex("run-36-file.tdb", 1024, TDB_CLEAR_IF_FIRST,
77 O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
79 ok1(tdb);
80 tdb->methods = &large_io_methods;
82 key.dsize = strlen("hi");
83 key.dptr = discard_const_p(uint8_t, "hi");
84 orig_data.dsize = strlen("world");
85 orig_data.dptr = discard_const_p(uint8_t, "world");
87 /* Enlarge the file (internally multiplies by 2). */
88 ret = tdb_expand(tdb, 1500000000);
89 #ifdef HAVE_INCOHERENT_MMAP
90 /* This can fail due to mmap failure on 32 bit systems. */
91 if (ret == -1) {
92 /* These should now fail. */
93 ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == -1);
94 data = tdb_fetch(tdb, key);
95 ok1(data.dptr == NULL);
96 ok1(tdb_traverse(tdb, test_traverse, &orig_data) == -1);
97 ok1(tdb_delete(tdb, key) == -1);
98 ok1(tdb_traverse(tdb, test_traverse, NULL) == -1);
99 /* Skip the rest... */
100 for (ret = 0; ret < 24 - 6; ret++)
101 ok1(1);
102 tdb_close(tdb);
103 return exit_status();
105 #endif
106 ok1(ret == 0);
108 /* Put an entry in, and check it. */
109 ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == 0);
111 data = tdb_fetch(tdb, key);
112 ok1(data.dsize == strlen("world"));
113 ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
114 free(data.dptr);
116 /* That currently fills at the end, make sure that's true. */
117 hashval = tdb->hash_fn(&key);
118 rec_ptr = tdb_find_lock_hash(tdb, key, hashval, F_RDLCK, &rec);
119 ok1(rec_ptr);
120 ok1(rec_ptr > 2U*1024*1024*1024);
121 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
123 /* Traverse must work. */
124 ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1);
126 /* Delete should work. */
127 ok1(tdb_delete(tdb, key) == 0);
129 ok1(tdb_traverse(tdb, test_traverse, NULL) == 0);
131 /* Transactions should work. */
132 ok1(tdb_transaction_start(tdb) == 0);
133 ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == 0);
135 data = tdb_fetch(tdb, key);
136 ok1(data.dsize == strlen("world"));
137 ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
138 free(data.dptr);
139 ok1(tdb_transaction_commit(tdb) == 0);
141 ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1);
142 tdb_close(tdb);
144 return exit_status();