1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* dirinfo.c --- maintains the directory information table for e2fsck.
*
* Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
* under the terms of the GNU Public License.
*/
#include <et/com_err.h>
#include "e2fsck.h"
static int dir_info_count = 0;
static int dir_info_size = 0;
static struct dir_info *dir_info = 0;
int get_num_dirs(ext2_filsys fs)
{
int i, num_dirs;
num_dirs = 0;
for (i = 0; i < fs->group_desc_count; i++)
num_dirs += fs->group_desc[i].bg_used_dirs_count;
return num_dirs;
}
/*
* This subroutine is called during pass1 to stash away the block
* numbers for the directory, which we will need later. The idea is
* to avoid reading directory inodes twice.
*/
void add_dir_info(ext2_filsys fs, ino_t ino, ino_t parent,
struct ext2_inode *inode)
{
struct dir_info *dir;
int i, j;
#if 0
printf("add_dir_info for inode %d...\n", ino);
#endif
if (!dir_info) {
dir_info_count = 0;
dir_info_size = get_num_dirs(fs) + 10;
dir_info = allocate_memory(dir_info_size *
sizeof (struct dir_info),
"directory map");
}
if (dir_info_count >= dir_info_size) {
dir_info_size += 10;
dir_info = realloc(dir_info,
dir_info_size * sizeof(struct dir_info));
}
/*
* Normally, add_dir_info is called with each inode in
* sequential order; but once in a while (like when pass 3
* needs to recreate the root directory or lost+found
* directory) it is called out of order. In those cases, we
* need to move the dir_info entries down to make room, since
* the dir_info array needs to be sorted by inode number for
* get_dir_info()'s sake.
*/
if (dir_info_count && dir_info[dir_info_count-1].ino > ino) {
for (i = dir_info_count-1; i > 0; i--)
if (dir_info[i-1].ino < ino)
break;
dir = &dir_info[i];
if (dir->ino != ino)
for (j = dir_info_count++; j > i; j--)
dir_info[j] = dir_info[j-1];
} else
dir = &dir_info[dir_info_count++];
dir->ino = ino;
dir->dotdot = parent;
dir->parent = parent;
}
/*
* get_dir_info() --- given an inode number, try to find the directory
* information entry for it.
*/
struct dir_info *get_dir_info(ino_t ino)
{
int low, high, mid;
low = 0;
high = dir_info_count-1;
if (ino == dir_info[low].ino)
return &dir_info[low];
if (ino == dir_info[high].ino)
return &dir_info[high];
while (low < high) {
mid = (low+high)/2;
if (mid == low || mid == high)
break;
if (ino == dir_info[mid].ino)
return &dir_info[mid];
if (ino < dir_info[mid].ino)
high = mid;
else
low = mid;
}
return 0;
}
/*
* Free the dir_info structure when it isn't needed any more.
*/
void free_dir_info(ext2_filsys fs)
{
if (dir_info) {
free(dir_info);
dir_info = 0;
}
dir_info_size = 0;
dir_info_count = 0;
}
|