Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 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 | */ |
| 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 | */ |
| 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_mem(len, &dblist->list); |
| 89 | if (retval) |
| 90 | goto cleanup; |
| 91 | |
| 92 | if (list) |
| 93 | memcpy(dblist->list, list, len); |
| 94 | else |
| 95 | memset(dblist->list, 0, len); |
| 96 | if (ret_dblist) |
| 97 | *ret_dblist = dblist; |
| 98 | else |
| 99 | fs->dblist = dblist; |
| 100 | return 0; |
| 101 | cleanup: |
| 102 | if (dblist) |
| 103 | ext2fs_free_mem(&dblist); |
| 104 | return retval; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Initialize a directory block list |
| 109 | */ |
| 110 | errcode_t ext2fs_init_dblist(ext2_filsys fs, ext2_dblist *ret_dblist) |
| 111 | { |
| 112 | ext2_dblist dblist; |
| 113 | errcode_t retval; |
| 114 | |
| 115 | retval = make_dblist(fs, 0, 0, 0, &dblist); |
| 116 | if (retval) |
| 117 | return retval; |
| 118 | |
| 119 | dblist->sorted = 1; |
| 120 | if (ret_dblist) |
| 121 | *ret_dblist = dblist; |
| 122 | else |
| 123 | fs->dblist = dblist; |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Copy a directory block list |
| 130 | */ |
| 131 | errcode_t ext2fs_copy_dblist(ext2_dblist src, ext2_dblist *dest) |
| 132 | { |
| 133 | ext2_dblist dblist; |
| 134 | errcode_t retval; |
| 135 | |
| 136 | retval = make_dblist(src->fs, src->size, src->count, src->list, |
| 137 | &dblist); |
| 138 | if (retval) |
| 139 | return retval; |
| 140 | dblist->sorted = src->sorted; |
| 141 | *dest = dblist; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Close a directory block list |
| 147 | * |
| 148 | * (moved to closefs.c) |
| 149 | */ |
| 150 | |
| 151 | |
| 152 | /* |
| 153 | * Add a directory block to the directory block list |
| 154 | */ |
| 155 | errcode_t ext2fs_add_dir_block(ext2_dblist dblist, ext2_ino_t ino, blk_t blk, |
| 156 | int blockcnt) |
| 157 | { |
| 158 | struct ext2_db_entry *new_entry; |
| 159 | errcode_t retval; |
| 160 | unsigned long old_size; |
| 161 | |
| 162 | EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST); |
| 163 | |
| 164 | if (dblist->count >= dblist->size) { |
| 165 | old_size = dblist->size * sizeof(struct ext2_db_entry); |
| 166 | dblist->size += 100; |
| 167 | retval = ext2fs_resize_mem(old_size, (size_t) dblist->size * |
| 168 | sizeof(struct ext2_db_entry), |
| 169 | &dblist->list); |
| 170 | if (retval) { |
| 171 | dblist->size -= 100; |
| 172 | return retval; |
| 173 | } |
| 174 | } |
| 175 | new_entry = dblist->list + ( (int) dblist->count++); |
| 176 | new_entry->blk = blk; |
| 177 | new_entry->ino = ino; |
| 178 | new_entry->blockcnt = blockcnt; |
| 179 | |
| 180 | dblist->sorted = 0; |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Change the directory block to the directory block list |
| 187 | */ |
| 188 | errcode_t ext2fs_set_dir_block(ext2_dblist dblist, ext2_ino_t ino, blk_t blk, |
| 189 | int blockcnt) |
| 190 | { |
| 191 | dgrp_t i; |
| 192 | |
| 193 | EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST); |
| 194 | |
| 195 | for (i=0; i < dblist->count; i++) { |
| 196 | if ((dblist->list[i].ino != ino) || |
| 197 | (dblist->list[i].blockcnt != blockcnt)) |
| 198 | continue; |
| 199 | dblist->list[i].blk = blk; |
| 200 | dblist->sorted = 0; |
| 201 | return 0; |
| 202 | } |
| 203 | return EXT2_ET_DB_NOT_FOUND; |
| 204 | } |
| 205 | |
| 206 | void ext2fs_dblist_sort(ext2_dblist dblist, |
| 207 | EXT2_QSORT_TYPE (*sortfunc)(const void *, |
| 208 | const void *)) |
| 209 | { |
| 210 | if (!sortfunc) |
| 211 | sortfunc = dir_block_cmp; |
| 212 | qsort(dblist->list, (size_t) dblist->count, |
| 213 | sizeof(struct ext2_db_entry), sortfunc); |
| 214 | dblist->sorted = 1; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * This function iterates over the directory block list |
| 219 | */ |
| 220 | errcode_t ext2fs_dblist_iterate(ext2_dblist dblist, |
| 221 | int (*func)(ext2_filsys fs, |
| 222 | struct ext2_db_entry *db_info, |
| 223 | void *priv_data), |
| 224 | void *priv_data) |
| 225 | { |
| 226 | ext2_ino_t i; |
| 227 | int ret; |
| 228 | |
| 229 | EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST); |
| 230 | |
| 231 | if (!dblist->sorted) |
| 232 | ext2fs_dblist_sort(dblist, 0); |
| 233 | for (i=0; i < dblist->count; i++) { |
| 234 | ret = (*func)(dblist->fs, &dblist->list[(int)i], priv_data); |
| 235 | if (ret & DBLIST_ABORT) |
| 236 | return 0; |
| 237 | } |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static EXT2_QSORT_TYPE dir_block_cmp(const void *a, const void *b) |
| 242 | { |
| 243 | const struct ext2_db_entry *db_a = |
| 244 | (const struct ext2_db_entry *) a; |
| 245 | const struct ext2_db_entry *db_b = |
| 246 | (const struct ext2_db_entry *) b; |
| 247 | |
| 248 | if (db_a->blk != db_b->blk) |
| 249 | return (int) (db_a->blk - db_b->blk); |
| 250 | |
| 251 | if (db_a->ino != db_b->ino) |
| 252 | return (int) (db_a->ino - db_b->ino); |
| 253 | |
| 254 | return (int) (db_a->blockcnt - db_b->blockcnt); |
| 255 | } |
| 256 | |
| 257 | int ext2fs_dblist_count(ext2_dblist dblist) |
| 258 | { |
| 259 | return (int) dblist->count; |
| 260 | } |