3
0

dblist.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * dblist.c -- directory block list functions
  3. *
  4. * Copyright 1997 by Theodore Ts'o
  5. *
  6. * %Begin-Header%
  7. * This file may be redistributed under the terms of the GNU Public
  8. * License.
  9. * %End-Header%
  10. *
  11. */
  12. #include <stdio.h>
  13. #if HAVE_UNISTD_H
  14. #include <unistd.h>
  15. #endif
  16. #include <string.h>
  17. #include <time.h>
  18. #include "ext2_fs.h"
  19. #include "ext2fsP.h"
  20. static EXT2_QSORT_TYPE dir_block_cmp(const void *a, const void *b);
  21. /*
  22. * Returns the number of directories in the filesystem as reported by
  23. * the group descriptors. Of course, the group descriptors could be
  24. * wrong!
  25. */
  26. errcode_t ext2fs_get_num_dirs(ext2_filsys fs, ext2_ino_t *ret_num_dirs)
  27. {
  28. dgrp_t i;
  29. ext2_ino_t num_dirs, max_dirs;
  30. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  31. num_dirs = 0;
  32. max_dirs = fs->super->s_inodes_per_group;
  33. for (i = 0; i < fs->group_desc_count; i++) {
  34. if (fs->group_desc[i].bg_used_dirs_count > max_dirs)
  35. num_dirs += max_dirs / 8;
  36. else
  37. num_dirs += fs->group_desc[i].bg_used_dirs_count;
  38. }
  39. if (num_dirs > fs->super->s_inodes_count)
  40. num_dirs = fs->super->s_inodes_count;
  41. *ret_num_dirs = num_dirs;
  42. return 0;
  43. }
  44. /*
  45. * helper function for making a new directory block list (for
  46. * initialize and copy).
  47. */
  48. static errcode_t make_dblist(ext2_filsys fs, ext2_ino_t size, ext2_ino_t count,
  49. struct ext2_db_entry *list,
  50. ext2_dblist *ret_dblist)
  51. {
  52. ext2_dblist dblist;
  53. errcode_t retval;
  54. size_t len;
  55. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  56. if ((ret_dblist == 0) && fs->dblist &&
  57. (fs->dblist->magic == EXT2_ET_MAGIC_DBLIST))
  58. return 0;
  59. retval = ext2fs_get_mem(sizeof(struct ext2_struct_dblist), &dblist);
  60. if (retval)
  61. return retval;
  62. memset(dblist, 0, sizeof(struct ext2_struct_dblist));
  63. dblist->magic = EXT2_ET_MAGIC_DBLIST;
  64. dblist->fs = fs;
  65. if (size)
  66. dblist->size = size;
  67. else {
  68. retval = ext2fs_get_num_dirs(fs, &dblist->size);
  69. if (retval)
  70. goto cleanup;
  71. dblist->size = (dblist->size * 2) + 12;
  72. }
  73. len = (size_t) sizeof(struct ext2_db_entry) * dblist->size;
  74. dblist->count = count;
  75. retval = ext2fs_get_mem(len, &dblist->list);
  76. if (retval)
  77. goto cleanup;
  78. if (list)
  79. memcpy(dblist->list, list, len);
  80. else
  81. memset(dblist->list, 0, len);
  82. if (ret_dblist)
  83. *ret_dblist = dblist;
  84. else
  85. fs->dblist = dblist;
  86. return 0;
  87. cleanup:
  88. if (dblist)
  89. ext2fs_free_mem(&dblist);
  90. return retval;
  91. }
  92. /*
  93. * Initialize a directory block list
  94. */
  95. errcode_t ext2fs_init_dblist(ext2_filsys fs, ext2_dblist *ret_dblist)
  96. {
  97. ext2_dblist dblist;
  98. errcode_t retval;
  99. retval = make_dblist(fs, 0, 0, 0, &dblist);
  100. if (retval)
  101. return retval;
  102. dblist->sorted = 1;
  103. if (ret_dblist)
  104. *ret_dblist = dblist;
  105. else
  106. fs->dblist = dblist;
  107. return 0;
  108. }
  109. /*
  110. * Copy a directory block list
  111. */
  112. errcode_t ext2fs_copy_dblist(ext2_dblist src, ext2_dblist *dest)
  113. {
  114. ext2_dblist dblist;
  115. errcode_t retval;
  116. retval = make_dblist(src->fs, src->size, src->count, src->list,
  117. &dblist);
  118. if (retval)
  119. return retval;
  120. dblist->sorted = src->sorted;
  121. *dest = dblist;
  122. return 0;
  123. }
  124. /*
  125. * Close a directory block list
  126. *
  127. * (moved to closefs.c)
  128. */
  129. /*
  130. * Add a directory block to the directory block list
  131. */
  132. errcode_t ext2fs_add_dir_block(ext2_dblist dblist, ext2_ino_t ino, blk_t blk,
  133. int blockcnt)
  134. {
  135. struct ext2_db_entry *new_entry;
  136. errcode_t retval;
  137. unsigned long old_size;
  138. EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
  139. if (dblist->count >= dblist->size) {
  140. old_size = dblist->size * sizeof(struct ext2_db_entry);
  141. dblist->size += 100;
  142. retval = ext2fs_resize_mem(old_size, (size_t) dblist->size *
  143. sizeof(struct ext2_db_entry),
  144. &dblist->list);
  145. if (retval) {
  146. dblist->size -= 100;
  147. return retval;
  148. }
  149. }
  150. new_entry = dblist->list + ( (int) dblist->count++);
  151. new_entry->blk = blk;
  152. new_entry->ino = ino;
  153. new_entry->blockcnt = blockcnt;
  154. dblist->sorted = 0;
  155. return 0;
  156. }
  157. /*
  158. * Change the directory block to the directory block list
  159. */
  160. errcode_t ext2fs_set_dir_block(ext2_dblist dblist, ext2_ino_t ino, blk_t blk,
  161. int blockcnt)
  162. {
  163. dgrp_t i;
  164. EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
  165. for (i=0; i < dblist->count; i++) {
  166. if ((dblist->list[i].ino != ino) ||
  167. (dblist->list[i].blockcnt != blockcnt))
  168. continue;
  169. dblist->list[i].blk = blk;
  170. dblist->sorted = 0;
  171. return 0;
  172. }
  173. return EXT2_ET_DB_NOT_FOUND;
  174. }
  175. void ext2fs_dblist_sort(ext2_dblist dblist,
  176. EXT2_QSORT_TYPE (*sortfunc)(const void *,
  177. const void *))
  178. {
  179. if (!sortfunc)
  180. sortfunc = dir_block_cmp;
  181. qsort(dblist->list, (size_t) dblist->count,
  182. sizeof(struct ext2_db_entry), sortfunc);
  183. dblist->sorted = 1;
  184. }
  185. /*
  186. * This function iterates over the directory block list
  187. */
  188. errcode_t ext2fs_dblist_iterate(ext2_dblist dblist,
  189. int (*func)(ext2_filsys fs,
  190. struct ext2_db_entry *db_info,
  191. void *priv_data),
  192. void *priv_data)
  193. {
  194. ext2_ino_t i;
  195. int ret;
  196. EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
  197. if (!dblist->sorted)
  198. ext2fs_dblist_sort(dblist, 0);
  199. for (i=0; i < dblist->count; i++) {
  200. ret = (*func)(dblist->fs, &dblist->list[(int)i], priv_data);
  201. if (ret & DBLIST_ABORT)
  202. return 0;
  203. }
  204. return 0;
  205. }
  206. static EXT2_QSORT_TYPE dir_block_cmp(const void *a, const void *b)
  207. {
  208. const struct ext2_db_entry *db_a =
  209. (const struct ext2_db_entry *) a;
  210. const struct ext2_db_entry *db_b =
  211. (const struct ext2_db_entry *) b;
  212. if (db_a->blk != db_b->blk)
  213. return (int) (db_a->blk - db_b->blk);
  214. if (db_a->ino != db_b->ino)
  215. return (int) (db_a->ino - db_b->ino);
  216. return (int) (db_a->blockcnt - db_b->blockcnt);
  217. }
  218. int ext2fs_dblist_count(ext2_dblist dblist)
  219. {
  220. return (int) dblist->count;
  221. }