0e203cc0d87adcd34cd632ed0c3f21d60d75b74b
[bbaumbach/samba-autobuild/.git] / source4 / lib / tdb / common / dump.c
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 2 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, write to the Free Software
26    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27 */
28
29 #include "tdb_private.h"
30
31 static tdb_off_t tdb_dump_record(struct tdb_context *tdb, tdb_off_t offset)
32 {
33         struct list_struct rec;
34         tdb_off_t tailer_ofs, tailer;
35
36         if (tdb_read(tdb, offset, (char *)&rec, sizeof(rec), DOCONV()) == -1) {
37                 printf("ERROR: failed to read record at %u\n", offset);
38                 return 0;
39         }
40
41         printf(" rec: offset=0x%08x next=0x%08x rec_len=%d key_len=%d data_len=%d full_hash=0x%x magic=0x%x\n",
42                offset, rec.next, rec.rec_len, rec.key_len, rec.data_len, rec.full_hash, rec.magic);
43
44         tailer_ofs = offset + sizeof(rec) + rec.rec_len - sizeof(tdb_off_t);
45
46         if (tdb_ofs_read(tdb, tailer_ofs, &tailer) == -1) {
47                 printf("ERROR: failed to read tailer at %u\n", tailer_ofs);
48                 return rec.next;
49         }
50
51         if (tailer != rec.rec_len + sizeof(rec)) {
52                 printf("ERROR: tailer does not match record! tailer=%u totalsize=%u\n",
53                                 (unsigned int)tailer, (unsigned int)(rec.rec_len + sizeof(rec)));
54         }
55         return rec.next;
56 }
57
58 static int tdb_dump_chain(struct tdb_context *tdb, int i)
59 {
60         tdb_off_t rec_ptr, top;
61
62         top = TDB_HASH_TOP(i);
63
64         if (tdb_lock(tdb, i, F_WRLCK) != 0)
65                 return -1;
66
67         if (tdb_ofs_read(tdb, top, &rec_ptr) == -1)
68                 return tdb_unlock(tdb, i, F_WRLCK);
69
70         if (rec_ptr)
71                 printf("hash=%d\n", i);
72
73         while (rec_ptr) {
74                 rec_ptr = tdb_dump_record(tdb, rec_ptr);
75         }
76
77         return tdb_unlock(tdb, i, F_WRLCK);
78 }
79
80 void tdb_dump_all(struct tdb_context *tdb)
81 {
82         int i;
83         for (i=0;i<tdb->header.hash_size;i++) {
84                 tdb_dump_chain(tdb, i);
85         }
86         printf("freelist:\n");
87         tdb_dump_chain(tdb, -1);
88 }
89
90 int tdb_printfreelist(struct tdb_context *tdb)
91 {
92         int ret;
93         long total_free = 0;
94         tdb_off_t offset, rec_ptr;
95         struct list_struct rec;
96
97         if ((ret = tdb_lock(tdb, -1, F_WRLCK)) != 0)
98                 return ret;
99
100         offset = FREELIST_TOP;
101
102         /* read in the freelist top */
103         if (tdb_ofs_read(tdb, offset, &rec_ptr) == -1) {
104                 tdb_unlock(tdb, -1, F_WRLCK);
105                 return 0;
106         }
107
108         printf("freelist top=[0x%08x]\n", rec_ptr );
109         while (rec_ptr) {
110                 if (tdb_read(tdb, rec_ptr, (char *)&rec, sizeof(rec), DOCONV()) == -1) {
111                         tdb_unlock(tdb, -1, F_WRLCK);
112                         return -1;
113                 }
114
115                 if (rec.magic != TDB_FREE_MAGIC) {
116                         printf("bad magic 0x%08x in free list\n", rec.magic);
117                         tdb_unlock(tdb, -1, F_WRLCK);
118                         return -1;
119                 }
120
121                 printf("entry offset=[0x%08x], rec.rec_len = [0x%08x (%d)] (end = 0x%08x)\n", 
122                        rec_ptr, rec.rec_len, rec.rec_len, rec_ptr + rec.rec_len);
123                 total_free += rec.rec_len;
124
125                 /* move to the next record */
126                 rec_ptr = rec.next;
127         }
128         printf("total rec_len = [0x%08x (%d)]\n", (int)total_free, 
129                (int)total_free);
130
131         return tdb_unlock(tdb, -1, F_WRLCK);
132 }
133