dump.c 3.6 KB

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