Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : trivial database library
5 :
6 : Copyright (C) Andrew Tridgell 1999-2005
7 : Copyright (C) Paul `Rusty' Russell 2000
8 : Copyright (C) Jeremy Allison 2000-2003
9 :
10 : ** NOTE! The following LGPL license applies to the tdb
11 : ** library. This does NOT imply that all of Samba is released
12 : ** under the LGPL
13 :
14 : This library is free software; you can redistribute it and/or
15 : modify it under the terms of the GNU Lesser General Public
16 : License as published by the Free Software Foundation; either
17 : version 3 of the License, or (at your option) any later version.
18 :
19 : This library is distributed in the hope that it will be useful,
20 : but WITHOUT ANY WARRANTY; without even the implied warranty of
21 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 : Lesser General Public License for more details.
23 :
24 : You should have received a copy of the GNU Lesser General Public
25 : License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 : */
27 :
28 : #include "tdb_private.h"
29 :
30 0 : static tdb_off_t tdb_dump_record(struct tdb_context *tdb, int hash,
31 : tdb_off_t offset)
32 : {
33 0 : struct tdb_record rec;
34 0 : tdb_off_t tailer_ofs, tailer;
35 :
36 0 : if (tdb->methods->tdb_read(tdb, offset, (char *)&rec,
37 0 : sizeof(rec), DOCONV()) == -1) {
38 0 : printf("ERROR: failed to read record at %u\n", offset);
39 0 : return 0;
40 : }
41 :
42 0 : printf(" rec: hash=%d offset=0x%08x next=0x%08x rec_len=%u "
43 : "key_len=%u data_len=%u full_hash=0x%08x magic=0x%08x\n",
44 : hash, offset, rec.next, rec.rec_len, rec.key_len, rec.data_len,
45 : rec.full_hash, rec.magic);
46 :
47 0 : tailer_ofs = offset + sizeof(rec) + rec.rec_len - sizeof(tdb_off_t);
48 :
49 0 : if (tdb_ofs_read(tdb, tailer_ofs, &tailer) == -1) {
50 0 : printf("ERROR: failed to read tailer at %u\n", tailer_ofs);
51 0 : return rec.next;
52 : }
53 :
54 0 : if (tailer != rec.rec_len + sizeof(rec)) {
55 0 : printf("ERROR: tailer does not match record! tailer=%u totalsize=%u\n",
56 0 : (unsigned int)tailer, (unsigned int)(rec.rec_len + sizeof(rec)));
57 : }
58 0 : return rec.next;
59 : }
60 :
61 0 : static int tdb_dump_chain(struct tdb_context *tdb, int i)
62 : {
63 0 : struct tdb_chainwalk_ctx chainwalk;
64 0 : tdb_off_t rec_ptr, top;
65 :
66 0 : if (i == -1) {
67 0 : top = FREELIST_TOP;
68 : } else {
69 0 : top = TDB_HASH_TOP(i);
70 : }
71 :
72 0 : if (tdb_lock(tdb, i, F_WRLCK) != 0)
73 0 : return -1;
74 :
75 0 : if (tdb_ofs_read(tdb, top, &rec_ptr) == -1)
76 0 : return tdb_unlock(tdb, i, F_WRLCK);
77 :
78 0 : tdb_chainwalk_init(&chainwalk, rec_ptr);
79 :
80 0 : if (rec_ptr)
81 0 : printf("hash=%d\n", i);
82 :
83 0 : while (rec_ptr) {
84 0 : bool ok;
85 0 : rec_ptr = tdb_dump_record(tdb, i, rec_ptr);
86 0 : ok = tdb_chainwalk_check(tdb, &chainwalk, rec_ptr);
87 0 : if (!ok) {
88 0 : printf("circular hash chain %d\n", i);
89 0 : break;
90 : }
91 : }
92 :
93 0 : return tdb_unlock(tdb, i, F_WRLCK);
94 : }
95 :
96 0 : _PUBLIC_ void tdb_dump_all(struct tdb_context *tdb)
97 : {
98 0 : uint32_t i;
99 0 : for (i=0;i<tdb->hash_size;i++) {
100 0 : tdb_dump_chain(tdb, i);
101 : }
102 0 : printf("freelist:\n");
103 0 : tdb_dump_chain(tdb, -1);
104 0 : }
105 :
106 0 : _PUBLIC_ int tdb_printfreelist(struct tdb_context *tdb)
107 : {
108 0 : int ret;
109 0 : long total_free = 0;
110 0 : tdb_off_t offset, rec_ptr;
111 0 : struct tdb_record rec;
112 :
113 0 : if ((ret = tdb_lock(tdb, -1, F_WRLCK)) != 0)
114 0 : return ret;
115 :
116 0 : offset = FREELIST_TOP;
117 :
118 : /* read in the freelist top */
119 0 : if (tdb_ofs_read(tdb, offset, &rec_ptr) == -1) {
120 0 : tdb_unlock(tdb, -1, F_WRLCK);
121 0 : return 0;
122 : }
123 :
124 0 : printf("freelist top=[0x%08x]\n", rec_ptr );
125 0 : while (rec_ptr) {
126 0 : if (tdb->methods->tdb_read(tdb, rec_ptr, (char *)&rec,
127 0 : sizeof(rec), DOCONV()) == -1) {
128 0 : tdb_unlock(tdb, -1, F_WRLCK);
129 0 : return -1;
130 : }
131 :
132 0 : if (rec.magic != TDB_FREE_MAGIC) {
133 0 : printf("bad magic 0x%08x in free list\n", rec.magic);
134 0 : tdb_unlock(tdb, -1, F_WRLCK);
135 0 : return -1;
136 : }
137 :
138 0 : printf("entry offset=[0x%08x], rec.rec_len = [0x%08x (%u)] (end = 0x%08x)\n",
139 0 : rec_ptr, rec.rec_len, rec.rec_len, rec_ptr + rec.rec_len);
140 0 : total_free += rec.rec_len;
141 :
142 : /* move to the next record */
143 0 : rec_ptr = rec.next;
144 : }
145 0 : printf("total rec_len = [0x%08lx (%lu)]\n", total_free, total_free);
146 :
147 0 : return tdb_unlock(tdb, -1, F_WRLCK);
148 : }
149 :
|