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
22 #include <linux/ext2_fs.h>
26 static int dir_block_cmp(const void *a, const void *b);
29 * Returns the number of directories in the filesystem as reported by
30 * the group descriptors. Of course, the group descriptors could be
33 errcode_t ext2fs_get_num_dirs(ext2_filsys fs, ino_t *ret_num_dirs)
38 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
41 for (i = 0; i < fs->group_desc_count; i++)
42 num_dirs += fs->group_desc[i].bg_used_dirs_count;
44 *ret_num_dirs = num_dirs;
50 * helper function for making a new directory block list (for
51 * initialize and copy).
53 static errcode_t make_dblist(ext2_filsys fs, ino_t size, ino_t count,
54 struct ext2_db_entry *list,
55 ext2_dblist *ret_dblist)
61 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
63 if ((ret_dblist == 0) && fs->dblist &&
64 (fs->dblist->magic == EXT2_ET_MAGIC_DBLIST))
67 dblist = malloc(sizeof(struct ext2_struct_dblist));
70 memset(dblist, 0, sizeof(struct ext2_struct_dblist));
72 dblist->magic = EXT2_ET_MAGIC_DBLIST;
77 retval = ext2fs_get_num_dirs(fs, &dblist->size);
80 dblist->size = (dblist->size * 2) + 12;
82 len = sizeof(struct ext2_db_entry) * dblist->size;
83 dblist->count = count;
84 dblist->list = malloc(len);
85 if (dblist->list == NULL) {
90 memcpy(dblist->list, list, len);
92 memset(dblist->list, 0, len);
102 * Initialize a directory block list
104 errcode_t ext2fs_init_dblist(ext2_filsys fs, ext2_dblist *ret_dblist)
109 retval = make_dblist(fs, 0, 0, 0, &dblist);
115 *ret_dblist = dblist;
123 * Copy a directory block list
125 errcode_t ext2fs_copy_dblist(ext2_dblist src, ext2_dblist *dest)
130 retval = make_dblist(src->fs, src->size, src->count, src->list,
134 dblist->sorted = src->sorted;
140 * Close a directory block list
142 * (moved to closefs.c)
147 * Add a directory block to the directory block list
149 errcode_t ext2fs_add_dir_block(ext2_dblist dblist, ino_t ino, blk_t blk,
152 struct ext2_db_entry *nlist, *new;
154 EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
156 if (dblist->count >= dblist->size) {
158 nlist = realloc(dblist->list,
159 dblist->size * sizeof(struct ext2_db_entry));
164 dblist->list = nlist;
166 new = dblist->list + dblist->count++;
169 new->blockcnt = blockcnt;
177 * Change the directory block to the directory block list
179 errcode_t ext2fs_set_dir_block(ext2_dblist dblist, ino_t ino, blk_t blk,
184 EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
186 for (i=0; i < dblist->count; i++) {
187 if ((dblist->list[i].ino != ino) ||
188 (dblist->list[i].blockcnt != blockcnt))
190 dblist->list[i].blk = blk;
198 * This function iterates over the directory block list
200 errcode_t ext2fs_dblist_iterate(ext2_dblist dblist,
201 int (*func)(ext2_filsys fs,
202 struct ext2_db_entry *db_info,
209 EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
211 if (!dblist->sorted) {
212 qsort(dblist->list, dblist->count,
213 sizeof(struct ext2_db_entry), dir_block_cmp);
216 for (i=0; i < dblist->count; i++) {
217 ret = (*func)(dblist->fs, &dblist->list[i], private);
218 if (ret & DBLIST_ABORT)
225 static int dir_block_cmp(const void *a, const void *b)
227 const struct ext2_db_entry *db_a =
228 (const struct ext2_db_entry *) a;
229 const struct ext2_db_entry *db_b =
230 (const struct ext2_db_entry *) b;
232 if (db_a->blk != db_b->blk)
233 return (db_a->blk - db_b->blk);
235 if (db_a->ino != db_b->ino)
236 return (db_a->ino - db_b->ino);
238 return (db_a->blockcnt - db_b->blockcnt);
241 int ext2fs_dblist_count(ext2_dblist dblist)
243 return (int) dblist->count;