2 * dblist.c -- directory block list functions
4 * Copyright 1997 by Theodore Ts'o
7 * This file may be redistributed under the terms of the GNU Public
24 #include <linux/ext2_fs.h>
28 static int dir_block_cmp(const void *a, const void *b);
31 * Returns the number of directories in the filesystem as reported by
32 * the group descriptors. Of course, the group descriptors could be
35 errcode_t ext2fs_get_num_dirs(ext2_filsys fs, ino_t *ret_num_dirs)
40 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
43 for (i = 0; i < fs->group_desc_count; i++)
44 num_dirs += fs->group_desc[i].bg_used_dirs_count;
46 *ret_num_dirs = num_dirs;
52 * helper function for making a new directory block list (for
53 * initialize and copy).
55 static errcode_t make_dblist(ext2_filsys fs, ino_t size, ino_t count,
56 struct ext2_db_entry *list,
57 ext2_dblist *ret_dblist)
63 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
65 if ((ret_dblist == 0) && fs->dblist &&
66 (fs->dblist->magic == EXT2_ET_MAGIC_DBLIST))
69 dblist = malloc(sizeof(struct ext2_struct_dblist));
72 memset(dblist, 0, sizeof(struct ext2_struct_dblist));
74 dblist->magic = EXT2_ET_MAGIC_DBLIST;
79 retval = ext2fs_get_num_dirs(fs, &dblist->size);
82 dblist->size = (dblist->size * 2) + 12;
84 len = (size_t) sizeof(struct ext2_db_entry) * dblist->size;
85 dblist->count = count;
86 dblist->list = malloc(len);
87 if (dblist->list == NULL) {
92 memcpy(dblist->list, list, len);
94 memset(dblist->list, 0, len);
104 * Initialize a directory block list
106 errcode_t ext2fs_init_dblist(ext2_filsys fs, ext2_dblist *ret_dblist)
111 retval = make_dblist(fs, 0, 0, 0, &dblist);
117 *ret_dblist = dblist;
125 * Copy a directory block list
127 errcode_t ext2fs_copy_dblist(ext2_dblist src, ext2_dblist *dest)
132 retval = make_dblist(src->fs, src->size, src->count, src->list,
136 dblist->sorted = src->sorted;
142 * Close a directory block list
144 * (moved to closefs.c)
149 * Add a directory block to the directory block list
151 errcode_t ext2fs_add_dir_block(ext2_dblist dblist, ino_t ino, blk_t blk,
154 struct ext2_db_entry *nlist, *new;
156 EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
158 if (dblist->count >= dblist->size) {
160 nlist = realloc(dblist->list, (size_t) dblist->size *
161 sizeof(struct ext2_db_entry));
166 dblist->list = nlist;
168 new = dblist->list + ( (int) dblist->count++);
171 new->blockcnt = blockcnt;
179 * Change the directory block to the directory block list
181 errcode_t ext2fs_set_dir_block(ext2_dblist dblist, ino_t ino, blk_t blk,
186 EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
188 for (i=0; i < dblist->count; i++) {
189 if ((dblist->list[i].ino != ino) ||
190 (dblist->list[i].blockcnt != blockcnt))
192 dblist->list[i].blk = blk;
200 * This function iterates over the directory block list
202 errcode_t ext2fs_dblist_iterate(ext2_dblist dblist,
203 int (*func)(ext2_filsys fs,
204 struct ext2_db_entry *db_info,
211 EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
213 if (!dblist->sorted) {
214 qsort(dblist->list, (size_t) dblist->count,
215 sizeof(struct ext2_db_entry), dir_block_cmp);
218 for (i=0; i < dblist->count; i++) {
219 ret = (*func)(dblist->fs, &dblist->list[(int)i], private);
220 if (ret & DBLIST_ABORT)
227 static int dir_block_cmp(const void *a, const void *b)
229 const struct ext2_db_entry *db_a =
230 (const struct ext2_db_entry *) a;
231 const struct ext2_db_entry *db_b =
232 (const struct ext2_db_entry *) b;
234 if (db_a->blk != db_b->blk)
235 return (int) (db_a->blk - db_b->blk);
237 if (db_a->ino != db_b->ino)
238 return (int) (db_a->ino - db_b->ino);
240 return (int) (db_a->blockcnt - db_b->blockcnt);
243 int ext2fs_dblist_count(ext2_dblist dblist)
245 return (int) dblist->count;