• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 #include <stdio.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <string.h>
18 #include <time.h>
19 
20 #include "ext2_fs.h"
21 #include "ext2fsP.h"
22 
23 static EXT2_QSORT_TYPE dir_block_cmp(const void *a, const void *b);
24 
25 /*
26  * Returns the number of directories in the filesystem as reported by
27  * the group descriptors.  Of course, the group descriptors could be
28  * wrong!
29  */
ext2fs_get_num_dirs(ext2_filsys fs,ext2_ino_t * ret_num_dirs)30 errcode_t ext2fs_get_num_dirs(ext2_filsys fs, ext2_ino_t *ret_num_dirs)
31 {
32 	dgrp_t	i;
33 	ext2_ino_t	num_dirs, max_dirs;
34 
35 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
36 
37 	num_dirs = 0;
38 	max_dirs = fs->super->s_inodes_per_group;
39 	for (i = 0; i < fs->group_desc_count; i++) {
40 		if (fs->group_desc[i].bg_used_dirs_count > max_dirs)
41 			num_dirs += max_dirs / 8;
42 		else
43 			num_dirs += fs->group_desc[i].bg_used_dirs_count;
44 	}
45 	if (num_dirs > fs->super->s_inodes_count)
46 		num_dirs = fs->super->s_inodes_count;
47 
48 	*ret_num_dirs = num_dirs;
49 
50 	return 0;
51 }
52 
53 /*
54  * helper function for making a new directory block list (for
55  * initialize and copy).
56  */
make_dblist(ext2_filsys fs,ext2_ino_t size,ext2_ino_t count,struct ext2_db_entry * list,ext2_dblist * ret_dblist)57 static errcode_t make_dblist(ext2_filsys fs, ext2_ino_t size, ext2_ino_t count,
58 			     struct ext2_db_entry *list,
59 			     ext2_dblist *ret_dblist)
60 {
61 	ext2_dblist	dblist;
62 	errcode_t	retval;
63 	size_t		len;
64 
65 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
66 
67 	if ((ret_dblist == 0) && fs->dblist &&
68 	    (fs->dblist->magic == EXT2_ET_MAGIC_DBLIST))
69 		return 0;
70 
71 	retval = ext2fs_get_mem(sizeof(struct ext2_struct_dblist), &dblist);
72 	if (retval)
73 		return retval;
74 	memset(dblist, 0, sizeof(struct ext2_struct_dblist));
75 
76 	dblist->magic = EXT2_ET_MAGIC_DBLIST;
77 	dblist->fs = fs;
78 	if (size)
79 		dblist->size = size;
80 	else {
81 		retval = ext2fs_get_num_dirs(fs, &dblist->size);
82 		if (retval)
83 			goto cleanup;
84 		dblist->size = (dblist->size * 2) + 12;
85 	}
86 	len = (size_t) sizeof(struct ext2_db_entry) * dblist->size;
87 	dblist->count = count;
88 	retval = ext2fs_get_array(dblist->size, sizeof(struct ext2_db_entry),
89 		&dblist->list);
90 	if (retval)
91 		goto cleanup;
92 
93 	if (list)
94 		memcpy(dblist->list, list, len);
95 	else
96 		memset(dblist->list, 0, len);
97 	if (ret_dblist)
98 		*ret_dblist = dblist;
99 	else
100 		fs->dblist = dblist;
101 	return 0;
102 cleanup:
103 	if (dblist)
104 		ext2fs_free_mem(&dblist);
105 	return retval;
106 }
107 
108 /*
109  * Initialize a directory block list
110  */
ext2fs_init_dblist(ext2_filsys fs,ext2_dblist * ret_dblist)111 errcode_t ext2fs_init_dblist(ext2_filsys fs, ext2_dblist *ret_dblist)
112 {
113 	ext2_dblist	dblist;
114 	errcode_t	retval;
115 
116 	retval = make_dblist(fs, 0, 0, 0, &dblist);
117 	if (retval)
118 		return retval;
119 
120 	dblist->sorted = 1;
121 	if (ret_dblist)
122 		*ret_dblist = dblist;
123 	else
124 		fs->dblist = dblist;
125 
126 	return 0;
127 }
128 
129 /*
130  * Copy a directory block list
131  */
ext2fs_copy_dblist(ext2_dblist src,ext2_dblist * dest)132 errcode_t ext2fs_copy_dblist(ext2_dblist src, ext2_dblist *dest)
133 {
134 	ext2_dblist	dblist;
135 	errcode_t	retval;
136 
137 	retval = make_dblist(src->fs, src->size, src->count, src->list,
138 			     &dblist);
139 	if (retval)
140 		return retval;
141 	dblist->sorted = src->sorted;
142 	*dest = dblist;
143 	return 0;
144 }
145 
146 /*
147  * Close a directory block list
148  *
149  * (moved to closefs.c)
150  */
151 
152 
153 /*
154  * Add a directory block to the directory block list
155  */
ext2fs_add_dir_block(ext2_dblist dblist,ext2_ino_t ino,blk_t blk,int blockcnt)156 errcode_t ext2fs_add_dir_block(ext2_dblist dblist, ext2_ino_t ino, blk_t blk,
157 			       int blockcnt)
158 {
159 	struct ext2_db_entry 	*new_entry;
160 	errcode_t		retval;
161 	unsigned long		old_size;
162 
163 	EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
164 
165 	if (dblist->count >= dblist->size) {
166 		old_size = dblist->size * sizeof(struct ext2_db_entry);
167 		dblist->size += dblist->size > 200 ? dblist->size / 2 : 100;
168 		retval = ext2fs_resize_mem(old_size, (size_t) dblist->size *
169 					   sizeof(struct ext2_db_entry),
170 					   &dblist->list);
171 		if (retval) {
172 			dblist->size -= 100;
173 			return retval;
174 		}
175 	}
176 	new_entry = dblist->list + ( (int) dblist->count++);
177 	new_entry->blk = blk;
178 	new_entry->ino = ino;
179 	new_entry->blockcnt = blockcnt;
180 
181 	dblist->sorted = 0;
182 
183 	return 0;
184 }
185 
186 /*
187  * Change the directory block to the directory block list
188  */
ext2fs_set_dir_block(ext2_dblist dblist,ext2_ino_t ino,blk_t blk,int blockcnt)189 errcode_t ext2fs_set_dir_block(ext2_dblist dblist, ext2_ino_t ino, blk_t blk,
190 			       int blockcnt)
191 {
192 	dgrp_t			i;
193 
194 	EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
195 
196 	for (i=0; i < dblist->count; i++) {
197 		if ((dblist->list[i].ino != ino) ||
198 		    (dblist->list[i].blockcnt != blockcnt))
199 			continue;
200 		dblist->list[i].blk = blk;
201 		dblist->sorted = 0;
202 		return 0;
203 	}
204 	return EXT2_ET_DB_NOT_FOUND;
205 }
206 
ext2fs_dblist_sort(ext2_dblist dblist,EXT2_QSORT_TYPE (* sortfunc)(const void *,const void *))207 void ext2fs_dblist_sort(ext2_dblist dblist,
208 			EXT2_QSORT_TYPE (*sortfunc)(const void *,
209 						    const void *))
210 {
211 	if (!sortfunc)
212 		sortfunc = dir_block_cmp;
213 	qsort(dblist->list, (size_t) dblist->count,
214 	      sizeof(struct ext2_db_entry), sortfunc);
215 	dblist->sorted = 1;
216 }
217 
218 /*
219  * This function iterates over the directory block list
220  */
ext2fs_dblist_iterate(ext2_dblist dblist,int (* func)(ext2_filsys fs,struct ext2_db_entry * db_info,void * priv_data),void * priv_data)221 errcode_t ext2fs_dblist_iterate(ext2_dblist dblist,
222 				int (*func)(ext2_filsys fs,
223 					    struct ext2_db_entry *db_info,
224 					    void	*priv_data),
225 				void *priv_data)
226 {
227 	ext2_ino_t	i;
228 	int		ret;
229 
230 	EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
231 
232 	if (!dblist->sorted)
233 		ext2fs_dblist_sort(dblist, 0);
234 	for (i=0; i < dblist->count; i++) {
235 		ret = (*func)(dblist->fs, &dblist->list[(int)i], priv_data);
236 		if (ret & DBLIST_ABORT)
237 			return 0;
238 	}
239 	return 0;
240 }
241 
dir_block_cmp(const void * a,const void * b)242 static EXT2_QSORT_TYPE dir_block_cmp(const void *a, const void *b)
243 {
244 	const struct ext2_db_entry *db_a =
245 		(const struct ext2_db_entry *) a;
246 	const struct ext2_db_entry *db_b =
247 		(const struct ext2_db_entry *) b;
248 
249 	if (db_a->blk != db_b->blk)
250 		return (int) (db_a->blk - db_b->blk);
251 
252 	if (db_a->ino != db_b->ino)
253 		return (int) (db_a->ino - db_b->ino);
254 
255 	return (int) (db_a->blockcnt - db_b->blockcnt);
256 }
257 
ext2fs_dblist_count(ext2_dblist dblist)258 int ext2fs_dblist_count(ext2_dblist dblist)
259 {
260 	return (int) dblist->count;
261 }
262 
ext2fs_dblist_get_last(ext2_dblist dblist,struct ext2_db_entry ** entry)263 errcode_t ext2fs_dblist_get_last(ext2_dblist dblist,
264 				 struct ext2_db_entry **entry)
265 {
266 	EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
267 
268 	if (dblist->count == 0)
269 		return EXT2_ET_DBLIST_EMPTY;
270 
271 	if (entry)
272 		*entry = dblist->list + ( (int) dblist->count-1);
273 	return 0;
274 }
275 
ext2fs_dblist_drop_last(ext2_dblist dblist)276 errcode_t ext2fs_dblist_drop_last(ext2_dblist dblist)
277 {
278 	EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST);
279 
280 	if (dblist->count == 0)
281 		return EXT2_ET_DBLIST_EMPTY;
282 
283 	dblist->count--;
284 	return 0;
285 }
286