2 * dirinfo.c --- maintains the directory information table for e2fsck.
4 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
8 #include <et/com_err.h>
12 * This subroutine is called during pass1 to create a directory info
13 * entry. During pass1, the passed-in parent is 0; it will get filled
16 void e2fsck_add_dir_info(e2fsck_t ctx, ino_t ino, ino_t parent)
24 printf("add_dir_info for inode %lu...\n", ino);
27 ctx->dir_info_count = 0;
28 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
30 num_dirs = 1024; /* Guess */
31 ctx->dir_info_size = num_dirs + 10;
32 ctx->dir_info = e2fsck_allocate_memory(ctx,
33 ctx->dir_info_size * sizeof (struct dir_info),
37 if (ctx->dir_info_count >= ctx->dir_info_size) {
38 ctx->dir_info_size += 10;
39 retval = ext2fs_resize_mem(ctx->dir_info_size *
40 sizeof(struct dir_info),
41 (void **) &ctx->dir_info);
43 ctx->dir_info_size -= 10;
49 * Normally, add_dir_info is called with each inode in
50 * sequential order; but once in a while (like when pass 3
51 * needs to recreate the root directory or lost+found
52 * directory) it is called out of order. In those cases, we
53 * need to move the dir_info entries down to make room, since
54 * the dir_info array needs to be sorted by inode number for
55 * get_dir_info()'s sake.
57 if (ctx->dir_info_count &&
58 ctx->dir_info[ctx->dir_info_count-1].ino >= ino) {
59 for (i = ctx->dir_info_count-1; i > 0; i--)
60 if (ctx->dir_info[i-1].ino < ino)
62 dir = &ctx->dir_info[i];
64 for (j = ctx->dir_info_count++; j > i; j--)
65 ctx->dir_info[j] = ctx->dir_info[j-1];
67 dir = &ctx->dir_info[ctx->dir_info_count++];
75 * get_dir_info() --- given an inode number, try to find the directory
76 * information entry for it.
78 struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ino_t ino)
83 high = ctx->dir_info_count-1;
86 if (ino == ctx->dir_info[low].ino)
87 return &ctx->dir_info[low];
88 if (ino == ctx->dir_info[high].ino)
89 return &ctx->dir_info[high];
93 if (mid == low || mid == high)
95 if (ino == ctx->dir_info[mid].ino)
96 return &ctx->dir_info[mid];
97 if (ino < ctx->dir_info[mid].ino)
106 * Free the dir_info structure when it isn't needed any more.
108 void e2fsck_free_dir_info(e2fsck_t ctx)
111 ext2fs_free_mem((void **) &ctx->dir_info);
114 ctx->dir_info_size = 0;
115 ctx->dir_info_count = 0;
119 * Return the count of number of directories in the dir_info structure
121 int e2fsck_get_num_dirinfo(e2fsck_t ctx)
123 return ctx->dir_info_count;
127 * A simple interator function
129 struct dir_info *e2fsck_dir_info_iter(e2fsck_t ctx, int *control)
131 if (*control >= ctx->dir_info_count)
134 return(ctx->dir_info + (*control)++);