00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "tdb_private.h"
00030
00031 static tdb_off_t tdb_dump_record(struct tdb_context *tdb, int hash,
00032 tdb_off_t offset)
00033 {
00034 struct list_struct rec;
00035 tdb_off_t tailer_ofs, tailer;
00036
00037 if (tdb->methods->tdb_read(tdb, offset, (char *)&rec,
00038 sizeof(rec), DOCONV()) == -1) {
00039 printf("ERROR: failed to read record at %u\n", offset);
00040 return 0;
00041 }
00042
00043 printf(" rec: hash=%d offset=0x%08x next=0x%08x rec_len=%d "
00044 "key_len=%d data_len=%d full_hash=0x%x magic=0x%x\n",
00045 hash, offset, rec.next, rec.rec_len, rec.key_len, rec.data_len,
00046 rec.full_hash, rec.magic);
00047
00048 tailer_ofs = offset + sizeof(rec) + rec.rec_len - sizeof(tdb_off_t);
00049
00050 if (tdb_ofs_read(tdb, tailer_ofs, &tailer) == -1) {
00051 printf("ERROR: failed to read tailer at %u\n", tailer_ofs);
00052 return rec.next;
00053 }
00054
00055 if (tailer != rec.rec_len + sizeof(rec)) {
00056 printf("ERROR: tailer does not match record! tailer=%u totalsize=%u\n",
00057 (unsigned int)tailer, (unsigned int)(rec.rec_len + sizeof(rec)));
00058 }
00059 return rec.next;
00060 }
00061
00062 static int tdb_dump_chain(struct tdb_context *tdb, int i)
00063 {
00064 tdb_off_t rec_ptr, top;
00065
00066 top = TDB_HASH_TOP(i);
00067
00068 if (tdb_lock(tdb, i, F_WRLCK) != 0)
00069 return -1;
00070
00071 if (tdb_ofs_read(tdb, top, &rec_ptr) == -1)
00072 return tdb_unlock(tdb, i, F_WRLCK);
00073
00074 if (rec_ptr)
00075 printf("hash=%d\n", i);
00076
00077 while (rec_ptr) {
00078 rec_ptr = tdb_dump_record(tdb, i, rec_ptr);
00079 }
00080
00081 return tdb_unlock(tdb, i, F_WRLCK);
00082 }
00083
00084 void tdb_dump_all(struct tdb_context *tdb)
00085 {
00086 int i;
00087 for (i=0;i<tdb->header.hash_size;i++) {
00088 tdb_dump_chain(tdb, i);
00089 }
00090 printf("freelist:\n");
00091 tdb_dump_chain(tdb, -1);
00092 }
00093
00094 int tdb_printfreelist(struct tdb_context *tdb)
00095 {
00096 int ret;
00097 long total_free = 0;
00098 tdb_off_t offset, rec_ptr;
00099 struct list_struct rec;
00100
00101 if ((ret = tdb_lock(tdb, -1, F_WRLCK)) != 0)
00102 return ret;
00103
00104 offset = FREELIST_TOP;
00105
00106
00107 if (tdb_ofs_read(tdb, offset, &rec_ptr) == -1) {
00108 tdb_unlock(tdb, -1, F_WRLCK);
00109 return 0;
00110 }
00111
00112 printf("freelist top=[0x%08x]\n", rec_ptr );
00113 while (rec_ptr) {
00114 if (tdb->methods->tdb_read(tdb, rec_ptr, (char *)&rec,
00115 sizeof(rec), DOCONV()) == -1) {
00116 tdb_unlock(tdb, -1, F_WRLCK);
00117 return -1;
00118 }
00119
00120 if (rec.magic != TDB_FREE_MAGIC) {
00121 printf("bad magic 0x%08x in free list\n", rec.magic);
00122 tdb_unlock(tdb, -1, F_WRLCK);
00123 return -1;
00124 }
00125
00126 printf("entry offset=[0x%08x], rec.rec_len = [0x%08x (%d)] (end = 0x%08x)\n",
00127 rec_ptr, rec.rec_len, rec.rec_len, rec_ptr + rec.rec_len);
00128 total_free += rec.rec_len;
00129
00130
00131 rec_ptr = rec.next;
00132 }
00133 printf("total rec_len = [0x%08x (%d)]\n", (int)total_free,
00134 (int)total_free);
00135
00136 return tdb_unlock(tdb, -1, F_WRLCK);
00137 }
00138