Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * dblist.c -- directory block list functions |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 3 | * |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 4 | * Copyright 1997 by Theodore Ts'o |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 5 | * |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 6 | * %Begin-Header% |
Theodore Ts'o | 543547a | 2010-05-17 21:31:56 -0400 | [diff] [blame] | 7 | * This file may be redistributed under the terms of the GNU Library |
| 8 | * General Public License, version 2. |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 9 | * %End-Header% |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 12 | #include "config.h" |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 14 | #if HAVE_UNISTD_H |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 16 | #endif |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 17 | #include <string.h> |
| 18 | #include <time.h> |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 19 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 20 | #include "ext2_fs.h" |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 21 | #include "ext2fsP.h" |
| 22 | |
Theodore Ts'o | 4c77fe5 | 1998-04-30 17:35:59 +0000 | [diff] [blame] | 23 | static EXT2_QSORT_TYPE dir_block_cmp(const void *a, const void *b); |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 24 | static EXT2_QSORT_TYPE dir_block_cmp2(const void *a, const void *b); |
| 25 | static EXT2_QSORT_TYPE (*sortfunc32)(const void *a, const void *b); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | * Returns the number of directories in the filesystem as reported by |
| 29 | * the group descriptors. Of course, the group descriptors could be |
| 30 | * wrong! |
| 31 | */ |
Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 32 | errcode_t ext2fs_get_num_dirs(ext2_filsys fs, ext2_ino_t *ret_num_dirs) |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 33 | { |
Theodore Ts'o | 2eb374c | 1998-09-03 01:22:57 +0000 | [diff] [blame] | 34 | dgrp_t i; |
Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 35 | ext2_ino_t num_dirs, max_dirs; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 36 | |
| 37 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 38 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 39 | num_dirs = 0; |
Theodore Ts'o | 79b05db | 1998-02-21 05:01:19 +0000 | [diff] [blame] | 40 | max_dirs = fs->super->s_inodes_per_group; |
Theodore Ts'o | 5be8dc2 | 1997-12-01 18:24:10 +0000 | [diff] [blame] | 41 | for (i = 0; i < fs->group_desc_count; i++) { |
Valerie Aurora Henson | d7cca6b | 2009-10-25 21:43:47 -0400 | [diff] [blame] | 42 | if (ext2fs_bg_used_dirs_count(fs, i) > max_dirs) |
Theodore Ts'o | 79b05db | 1998-02-21 05:01:19 +0000 | [diff] [blame] | 43 | num_dirs += max_dirs / 8; |
Theodore Ts'o | 5be8dc2 | 1997-12-01 18:24:10 +0000 | [diff] [blame] | 44 | else |
Valerie Aurora Henson | d7cca6b | 2009-10-25 21:43:47 -0400 | [diff] [blame] | 45 | num_dirs += ext2fs_bg_used_dirs_count(fs, i); |
Theodore Ts'o | 5be8dc2 | 1997-12-01 18:24:10 +0000 | [diff] [blame] | 46 | } |
| 47 | if (num_dirs > fs->super->s_inodes_count) |
| 48 | num_dirs = fs->super->s_inodes_count; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 49 | |
| 50 | *ret_num_dirs = num_dirs; |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | /* |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 56 | * helper function for making a new directory block list (for |
| 57 | * initialize and copy). |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 58 | */ |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 59 | static errcode_t make_dblist(ext2_filsys fs, ext2_ino_t size, |
| 60 | ext2_ino_t count, |
| 61 | struct ext2_db_entry2 *list, |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 62 | ext2_dblist *ret_dblist) |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 63 | { |
Andreas Dilger | 00eb0ee | 2012-11-29 05:47:53 -0700 | [diff] [blame] | 64 | ext2_dblist dblist = NULL; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 65 | errcode_t retval; |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 66 | ext2_ino_t num_dirs; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 67 | size_t len; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 68 | |
| 69 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 70 | |
| 71 | if ((ret_dblist == 0) && fs->dblist && |
| 72 | (fs->dblist->magic == EXT2_ET_MAGIC_DBLIST)) |
| 73 | return 0; |
| 74 | |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 75 | retval = ext2fs_get_mem(sizeof(struct ext2_struct_dblist), &dblist); |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 76 | if (retval) |
Eric Sandeen | 624e8eb | 2011-09-16 15:49:28 -0500 | [diff] [blame] | 77 | goto cleanup; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 78 | memset(dblist, 0, sizeof(struct ext2_struct_dblist)); |
| 79 | |
| 80 | dblist->magic = EXT2_ET_MAGIC_DBLIST; |
| 81 | dblist->fs = fs; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 82 | if (size) |
| 83 | dblist->size = size; |
| 84 | else { |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 85 | retval = ext2fs_get_num_dirs(fs, &num_dirs); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 86 | if (retval) |
| 87 | goto cleanup; |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 88 | dblist->size = (num_dirs * 2) + 12; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 89 | } |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 90 | len = (size_t) sizeof(struct ext2_db_entry2) * dblist->size; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 91 | dblist->count = count; |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 92 | retval = ext2fs_get_array(dblist->size, sizeof(struct ext2_db_entry2), |
Theodore Ts'o | ee01079 | 2007-11-09 19:01:06 -0500 | [diff] [blame] | 93 | &dblist->list); |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 94 | if (retval) |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 95 | goto cleanup; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 96 | |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 97 | if (list) |
| 98 | memcpy(dblist->list, list, len); |
| 99 | else |
| 100 | memset(dblist->list, 0, len); |
Theodore Ts'o | 2eb374c | 1998-09-03 01:22:57 +0000 | [diff] [blame] | 101 | if (ret_dblist) |
| 102 | *ret_dblist = dblist; |
| 103 | else |
| 104 | fs->dblist = dblist; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 105 | return 0; |
| 106 | cleanup: |
| 107 | if (dblist) |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 108 | ext2fs_free_mem(&dblist); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 109 | return retval; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Initialize a directory block list |
| 114 | */ |
| 115 | errcode_t ext2fs_init_dblist(ext2_filsys fs, ext2_dblist *ret_dblist) |
| 116 | { |
| 117 | ext2_dblist dblist; |
| 118 | errcode_t retval; |
| 119 | |
| 120 | retval = make_dblist(fs, 0, 0, 0, &dblist); |
| 121 | if (retval) |
| 122 | return retval; |
| 123 | |
| 124 | dblist->sorted = 1; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 125 | if (ret_dblist) |
| 126 | *ret_dblist = dblist; |
| 127 | else |
| 128 | fs->dblist = dblist; |
| 129 | |
| 130 | return 0; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Copy a directory block list |
| 135 | */ |
| 136 | errcode_t ext2fs_copy_dblist(ext2_dblist src, ext2_dblist *dest) |
| 137 | { |
| 138 | ext2_dblist dblist; |
| 139 | errcode_t retval; |
| 140 | |
| 141 | retval = make_dblist(src->fs, src->size, src->count, src->list, |
| 142 | &dblist); |
| 143 | if (retval) |
| 144 | return retval; |
| 145 | dblist->sorted = src->sorted; |
| 146 | *dest = dblist; |
| 147 | return 0; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Close a directory block list |
| 152 | * |
| 153 | * (moved to closefs.c) |
| 154 | */ |
| 155 | |
| 156 | |
| 157 | /* |
| 158 | * Add a directory block to the directory block list |
| 159 | */ |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 160 | errcode_t ext2fs_add_dir_block2(ext2_dblist dblist, ext2_ino_t ino, |
| 161 | blk64_t blk, e2_blkcnt_t blockcnt) |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 162 | { |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 163 | struct ext2_db_entry2 *new_entry; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 164 | errcode_t retval; |
Theodore Ts'o | 76f875d | 1998-04-27 01:41:13 +0000 | [diff] [blame] | 165 | unsigned long old_size; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 166 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 167 | EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST); |
| 168 | |
| 169 | if (dblist->count >= dblist->size) { |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 170 | old_size = dblist->size * sizeof(struct ext2_db_entry2); |
Andreas Dilger | 9f9e5c3 | 2008-08-23 21:17:17 -0600 | [diff] [blame] | 171 | dblist->size += dblist->size > 200 ? dblist->size / 2 : 100; |
Theodore Ts'o | 76f875d | 1998-04-27 01:41:13 +0000 | [diff] [blame] | 172 | retval = ext2fs_resize_mem(old_size, (size_t) dblist->size * |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 173 | sizeof(struct ext2_db_entry2), |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 174 | &dblist->list); |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 175 | if (retval) { |
Theodore Ts'o | 8f36e69 | 2011-06-11 12:54:50 -0400 | [diff] [blame] | 176 | dblist->size = old_size / sizeof(struct ext2_db_entry2); |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 177 | return retval; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 178 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 179 | } |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 180 | new_entry = dblist->list + ( dblist->count++); |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 181 | new_entry->blk = blk; |
| 182 | new_entry->ino = ino; |
| 183 | new_entry->blockcnt = blockcnt; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 184 | |
| 185 | dblist->sorted = 0; |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 191 | * Change the directory block to the directory block list |
| 192 | */ |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 193 | errcode_t ext2fs_set_dir_block2(ext2_dblist dblist, ext2_ino_t ino, |
| 194 | blk64_t blk, e2_blkcnt_t blockcnt) |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 195 | { |
Theodore Ts'o | 2eb374c | 1998-09-03 01:22:57 +0000 | [diff] [blame] | 196 | dgrp_t i; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 197 | |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 198 | EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST); |
| 199 | |
| 200 | for (i=0; i < dblist->count; i++) { |
| 201 | if ((dblist->list[i].ino != ino) || |
| 202 | (dblist->list[i].blockcnt != blockcnt)) |
| 203 | continue; |
| 204 | dblist->list[i].blk = blk; |
| 205 | dblist->sorted = 0; |
| 206 | return 0; |
| 207 | } |
Theodore Ts'o | 1f0b6c1 | 1997-10-31 06:07:47 +0000 | [diff] [blame] | 208 | return EXT2_ET_DB_NOT_FOUND; |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 211 | void ext2fs_dblist_sort2(ext2_dblist dblist, |
| 212 | EXT2_QSORT_TYPE (*sortfunc)(const void *, |
| 213 | const void *)) |
Theodore Ts'o | ea1959f | 2002-08-31 02:32:41 -0400 | [diff] [blame] | 214 | { |
| 215 | if (!sortfunc) |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 216 | sortfunc = dir_block_cmp2; |
Theodore Ts'o | ea1959f | 2002-08-31 02:32:41 -0400 | [diff] [blame] | 217 | qsort(dblist->list, (size_t) dblist->count, |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 218 | sizeof(struct ext2_db_entry2), sortfunc); |
Theodore Ts'o | ea1959f | 2002-08-31 02:32:41 -0400 | [diff] [blame] | 219 | dblist->sorted = 1; |
| 220 | } |
| 221 | |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 222 | /* |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 223 | * This function iterates over the directory block list |
| 224 | */ |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 225 | errcode_t ext2fs_dblist_iterate2(ext2_dblist dblist, |
| 226 | int (*func)(ext2_filsys fs, |
| 227 | struct ext2_db_entry2 *db_info, |
| 228 | void *priv_data), |
| 229 | void *priv_data) |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 230 | { |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 231 | unsigned long long i; |
Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 232 | int ret; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 233 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 234 | EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST); |
| 235 | |
Theodore Ts'o | ea1959f | 2002-08-31 02:32:41 -0400 | [diff] [blame] | 236 | if (!dblist->sorted) |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 237 | ext2fs_dblist_sort2(dblist, 0); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 238 | for (i=0; i < dblist->count; i++) { |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 239 | ret = (*func)(dblist->fs, &dblist->list[i], priv_data); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 240 | if (ret & DBLIST_ABORT) |
| 241 | return 0; |
| 242 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 246 | static EXT2_QSORT_TYPE dir_block_cmp2(const void *a, const void *b) |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 247 | { |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 248 | const struct ext2_db_entry2 *db_a = |
| 249 | (const struct ext2_db_entry2 *) a; |
| 250 | const struct ext2_db_entry2 *db_b = |
| 251 | (const struct ext2_db_entry2 *) b; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 252 | |
| 253 | if (db_a->blk != db_b->blk) |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 254 | return (int) (db_a->blk - db_b->blk); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 255 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 256 | if (db_a->ino != db_b->ino) |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 257 | return (int) (db_a->ino - db_b->ino); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 258 | |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 259 | return (db_a->blockcnt - db_b->blockcnt); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 262 | blk64_t ext2fs_dblist_count2(ext2_dblist dblist) |
Theodore Ts'o | 549860c | 1997-06-17 03:55:00 +0000 | [diff] [blame] | 263 | { |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 264 | return dblist->count; |
Theodore Ts'o | 549860c | 1997-06-17 03:55:00 +0000 | [diff] [blame] | 265 | } |
Theodore Ts'o | 52b1dd5 | 2008-03-13 00:34:16 -0400 | [diff] [blame] | 266 | |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 267 | errcode_t ext2fs_dblist_get_last2(ext2_dblist dblist, |
| 268 | struct ext2_db_entry2 **entry) |
Theodore Ts'o | 52b1dd5 | 2008-03-13 00:34:16 -0400 | [diff] [blame] | 269 | { |
Theodore Ts'o | 52b1dd5 | 2008-03-13 00:34:16 -0400 | [diff] [blame] | 270 | EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST); |
| 271 | |
| 272 | if (dblist->count == 0) |
| 273 | return EXT2_ET_DBLIST_EMPTY; |
| 274 | |
| 275 | if (entry) |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 276 | *entry = dblist->list + ( dblist->count-1); |
Theodore Ts'o | 52b1dd5 | 2008-03-13 00:34:16 -0400 | [diff] [blame] | 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | errcode_t ext2fs_dblist_drop_last(ext2_dblist dblist) |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 281 | { |
Theodore Ts'o | 52b1dd5 | 2008-03-13 00:34:16 -0400 | [diff] [blame] | 282 | EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST); |
| 283 | |
| 284 | if (dblist->count == 0) |
| 285 | return EXT2_ET_DBLIST_EMPTY; |
| 286 | |
| 287 | dblist->count--; |
| 288 | return 0; |
| 289 | } |
Theodore Ts'o | ab13b5a | 2010-06-13 10:00:00 -0400 | [diff] [blame] | 290 | |
| 291 | /* |
| 292 | * Legacy 32-bit versions |
| 293 | */ |
| 294 | |
| 295 | /* |
| 296 | * Add a directory block to the directory block list |
| 297 | */ |
| 298 | errcode_t ext2fs_add_dir_block(ext2_dblist dblist, ext2_ino_t ino, blk_t blk, |
| 299 | int blockcnt) |
| 300 | { |
| 301 | return ext2fs_add_dir_block2(dblist, ino, blk, blockcnt); |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Change the directory block to the directory block list |
| 306 | */ |
| 307 | errcode_t ext2fs_set_dir_block(ext2_dblist dblist, ext2_ino_t ino, blk_t blk, |
| 308 | int blockcnt) |
| 309 | { |
| 310 | return ext2fs_set_dir_block2(dblist, ino, blk, blockcnt); |
| 311 | } |
| 312 | |
| 313 | void ext2fs_dblist_sort(ext2_dblist dblist, |
| 314 | EXT2_QSORT_TYPE (*sortfunc)(const void *, |
| 315 | const void *)) |
| 316 | { |
| 317 | if (sortfunc) { |
| 318 | sortfunc32 = sortfunc; |
| 319 | sortfunc = dir_block_cmp; |
| 320 | } else |
| 321 | sortfunc = dir_block_cmp2; |
| 322 | qsort(dblist->list, (size_t) dblist->count, |
| 323 | sizeof(struct ext2_db_entry2), sortfunc); |
| 324 | dblist->sorted = 1; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * This function iterates over the directory block list |
| 329 | */ |
| 330 | struct iterate_passthrough { |
| 331 | int (*func)(ext2_filsys fs, |
| 332 | struct ext2_db_entry *db_info, |
| 333 | void *priv_data); |
| 334 | void *priv_data; |
| 335 | }; |
| 336 | |
| 337 | static int passthrough_func(ext2_filsys fs, |
| 338 | struct ext2_db_entry2 *db_info, |
| 339 | void *priv_data) |
| 340 | { |
| 341 | struct iterate_passthrough *p = priv_data; |
| 342 | struct ext2_db_entry db; |
| 343 | int ret; |
| 344 | |
| 345 | db.ino = db_info->ino; |
| 346 | db.blk = (blk_t) db_info->blk; |
| 347 | db.blockcnt = (int) db_info->blockcnt; |
| 348 | ret = (p->func)(fs, &db, p->priv_data); |
| 349 | db_info->ino = db.ino; |
| 350 | db_info->blk = db.blk; |
| 351 | db_info->blockcnt = db.blockcnt; |
| 352 | return ret; |
| 353 | } |
| 354 | |
| 355 | errcode_t ext2fs_dblist_iterate(ext2_dblist dblist, |
| 356 | int (*func)(ext2_filsys fs, |
| 357 | struct ext2_db_entry *db_info, |
| 358 | void *priv_data), |
| 359 | void *priv_data) |
| 360 | { |
| 361 | struct iterate_passthrough pass; |
| 362 | |
| 363 | EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST); |
| 364 | pass.func = func; |
| 365 | pass.priv_data = priv_data; |
| 366 | |
| 367 | return ext2fs_dblist_iterate2(dblist, passthrough_func, &pass); |
| 368 | } |
| 369 | |
| 370 | static EXT2_QSORT_TYPE dir_block_cmp(const void *a, const void *b) |
| 371 | { |
| 372 | const struct ext2_db_entry2 *db_a = |
| 373 | (const struct ext2_db_entry2 *) a; |
| 374 | const struct ext2_db_entry2 *db_b = |
| 375 | (const struct ext2_db_entry2 *) b; |
| 376 | |
| 377 | struct ext2_db_entry a32, b32; |
| 378 | |
| 379 | a32.ino = db_a->ino; a32.blk = db_a->blk; |
| 380 | a32.blockcnt = db_a->blockcnt; |
| 381 | |
| 382 | b32.ino = db_b->ino; b32.blk = db_b->blk; |
| 383 | b32.blockcnt = db_b->blockcnt; |
| 384 | |
| 385 | return sortfunc32(&a32, &b32); |
| 386 | } |
| 387 | |
| 388 | int ext2fs_dblist_count(ext2_dblist dblist) |
| 389 | { |
| 390 | return dblist->count; |
| 391 | } |
| 392 | |
| 393 | errcode_t ext2fs_dblist_get_last(ext2_dblist dblist, |
| 394 | struct ext2_db_entry **entry) |
| 395 | { |
| 396 | EXT2_CHECK_MAGIC(dblist, EXT2_ET_MAGIC_DBLIST); |
| 397 | static struct ext2_db_entry ret_entry; |
| 398 | struct ext2_db_entry2 *last; |
| 399 | |
| 400 | if (dblist->count == 0) |
| 401 | return EXT2_ET_DBLIST_EMPTY; |
| 402 | |
| 403 | if (!entry) |
| 404 | return 0; |
| 405 | |
| 406 | last = dblist->list + dblist->count -1; |
| 407 | |
| 408 | ret_entry.ino = last->ino; |
| 409 | ret_entry.blk = last->blk; |
| 410 | ret_entry.blockcnt = last->blockcnt; |
| 411 | *entry = &ret_entry; |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |