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