dblist.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * dblist.c -- directory block list functions
  4. *
  5. * Copyright 1997 by Theodore Ts'o
  6. *
  7. * %Begin-Header%
  8. * This file may be redistributed under the terms of the GNU Public
  9. * License.
  10. * %End-Header%
  11. *
  12. */
  13. #include <stdio.h>
  14. #if HAVE_UNISTD_H
  15. #include <unistd.h>
  16. #endif
  17. #include <string.h>
  18. #include <time.h>
  19. #include "ext2_fs.h"
  20. #include "ext2fsP.h"
  21. static int dir_block_cmp(const void *a, const void *b);
  22. /*
  23. * Returns the number of directories in the filesystem as reported by
  24. * the group descriptors. Of course, the group descriptors could be
  25. * wrong!
  26. */
  27. errcode_t ext2fs_get_num_dirs(ext2_filsys fs, ext2_ino_t *ret_num_dirs)
  28. {
  29. dgrp_t i;
  30. ext2_ino_t num_dirs, max_dirs;
  31. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  32. num_dirs = 0;
  33. max_dirs = fs->super->s_inodes_per_group;
  34. for (i = 0; i < fs->group_desc_count; i++) {
  35. if (fs->group_desc[i].bg_used_dirs_count > max_dirs)
  36. num_dirs += max_dirs / 8;
  37. else
  38. num_dirs += fs->group_desc[i].bg_used_dirs_count;
  39. }
  40. if (num_dirs > fs->super->s_inodes_count)
  41. num_dirs = fs->super->s_inodes_count;
  42. *ret_num_dirs = num_dirs;
  43. return 0;
  44. }
  45. /*
  46. * helper function for making a new directory block list (for
  47. * initialize and copy).
  48. */
  49. static errcode_t make_dblist(ext2_filsys fs, ext2_ino_t size, ext2_ino_t count,
  50. struct ext2_db_entry *list,
  51. ext2_dblist *ret_dblist)
  52. {
  53. ext2_dblist dblist;
  54. errcode_t retval;
  55. size_t len;
  56. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  57. if ((ret_dblist == 0) && fs->dblist &&
  58. (fs->dblist->magic == EXT2_ET_MAGIC_DBLIST))
  59. return 0;
  60. retval = ext2fs_get_mem(sizeof(struct ext2_struct_dblist), &dblist);
  61. if (retval)
  62. return retval;
  63. memset(dblist, 0, sizeof(struct ext2_struct_dblist));
  64. dblist->magic = EXT2_ET_MAGIC_DBLIST;
  65. dblist->fs = fs;
  66. if (size)
  67. dblist->size = size;
  68. else {
  69. retval = ext2fs_get_num_dirs(fs, &dblist->size);
  70. if (retval)
  71. goto cleanup;
  72. dblist->size = (dblist->size * 2) + 12;
  73. }
  74. len = (size_t) sizeof(struct ext2_db_entry) * dblist->size;
  75. dblist->count = count;
  76. retval = ext2fs_get_mem(len, &dblist->list);
  77. if (retval)
  78. goto cleanup;
  79. if (list)
  80. memcpy(dblist->list, list, len);
  81. else
  82. memset(dblist->list, 0, len);
  83. if (ret_dblist)
  84. *ret_dblist = dblist;
  85. else
  86. fs->dblist = dblist;
  87. return 0;
  88. cleanup:
  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. int (*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 int 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. }