-
Notifications
You must be signed in to change notification settings - Fork 1
/
dir.c
195 lines (175 loc) · 6.18 KB
/
dir.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>
#include "structs.h"
char* dir_pth(dir_t* dir) {
char *p = (dir->parent == NULL ? NULL : dir->parent->pth);
int plen = (p == NULL ? 0 : strlen(p));
int len = plen + 1 + entry_namelen(dir->entry) + 1;
char *pth = (char*) malloc(len * sizeof(char));
assert(pth != NULL);
if (plen)
strncpy(pth, p, plen);
pth[plen] = '/';
strncpy(pth + plen + 1, (const char *) dir->entry->fname, entry_namelen(dir->entry));
pth[len - 1] = '\0';
return pth;
}
void print_dir(dir_t* dir) {
printf("directory: %s\n", dir->pth + 6); // skip root label
printf("size: %d bytes, ", dir->byte_size);
printf("%d clusters\n", dir->clust_sz);
printf("modify time: %02d-%02d-%02d %02d:%02d:%02d\n\n", dir->entry->days + 1,
dir->entry->months + 1, dir->entry->years + 1980, dir->entry->hrs,
dir->entry->mins, dir->entry->secs);
int i;
for (i = 0; i < (int) dir->file_cnt; i++)
print_file(dir->files[i]);
for (i = 0; i < (int) dir->subdir_cnt; i++)
print_dir(dir->subdirs[i]);
}
dir_t* init_dir(fat_desc_t* fat, entry_t* entry, dir_t* parent) {
dir_t* dir = (dir_t*) malloc(sizeof(dir_t));
assert(dir != NULL);
memset(dir, 0, sizeof(dir_t));
dir->byte_size = 0;
dir->parent = parent;
dir->entry = entry;
dir->max_files = 100;
dir->max_subdirs = 100;
dir->subdirs = malloc(dir->max_subdirs * sizeof(dir_t*));
dir->files = malloc(dir->max_files * sizeof(file_t*));
assert(dir->subdirs != NULL);
assert(dir->files != NULL);
dir->file_cnt = 0;
dir->subdir_cnt = 0;
// finally get the path
dir->clust_sz = get_cluster_size(fat, entry->cluster);
dir->cluster = entry->cluster;
dir->pth = dir_pth(dir);
return dir;
}
void free_dir(dir_t* dir) {
int i;
for (i = 0; i < (int) dir->subdir_cnt; i++)
free_dir(dir->subdirs[i]);
free(dir->subdirs);
for (i = 0; i < (int) dir->file_cnt; i++) {
free(dir->files[i]->pth);
free(dir->files[i]->entry);
free(dir->files[i]);
}
free(dir->files);
free(dir->entry);
free(dir->pth);
free(dir);
dir = NULL;
}
dir_t* process_root(fat_desc_t* fat, int root_cluster) {
entry_t* root_entry = malloc(sizeof(entry_t));
assert(root_entry != NULL);
memset(root_entry, 0, sizeof(entry_t));
memcpy(root_entry->fname, "ROOT", sizeof(char) * 4);
root_entry->cluster = root_cluster;
dir_t* dir = init_dir(fat, root_entry, NULL);
assert(dir != NULL);
int i;
int root_dirs = (32 * fat->sector_size) / sizeof(entry_t);
int root_ptr = get_sector(fat, fat->rootdir_offset);
for (i = 0; i < root_dirs; i++) {
entry_t* cur = (entry_t*) malloc(sizeof(entry_t));
assert(cur != NULL);
memset(cur, 0, sizeof(entry_t));
fseek(fat->in, root_ptr + i * sizeof(entry_t), SEEK_SET);
if (fread(cur, sizeof(entry_t), 1, fat->in) != 1) {
fprintf(stderr, "Error: failed to read root dir entry\n");
exit(1);
}
if (cur->fname[0] == 0x0) {
free(cur);
break; // empty
}
// extended name or deleted file
if (cur->fname[0] == 0xe5 || cur->atr == 0x0f) {
free(cur);
continue;
}
// . and ..
if (cur->fname[0] == '.' && (cur->fname[1] == '.' || !isalpha(cur->fname[1])) && !isalpha(cur->fname[1])) {
free(cur);
continue;
}
if (cur->atr & 0x10) { // directory
dir_t* subdir = init_dir(fat, cur, dir);
dir_process(fat, subdir);
add_subdir(dir, subdir);
} else {
file_t* file = init_file(fat, cur, dir);
add_file(dir, file);
}
}
return dir;
}
void dir_process(fat_desc_t* fat, dir_t* dir) {
int cluster = dir->cluster;
int dirs_per_cluster = (fat->clust_sz * fat->sector_size) / sizeof(entry_t);
while (cluster != 0) {
int cluster_ptr = get_sector(fat, fat->cluster_offset + (cluster - 2) * fat->clust_sz);
int i;
for (i = 0; i < dirs_per_cluster; i++) {
entry_t* cur = (entry_t*) malloc(sizeof(entry_t));
assert(cur != NULL);
memset(cur, 0, sizeof(entry_t));
fseek(fat->in, cluster_ptr + i * sizeof(entry_t), SEEK_SET);
if (fread(cur, sizeof(entry_t), 1, fat->in) != 1) {
fprintf(stderr, "Error: failed to read entry\n");
exit(1);
}
if (cur->fname[0] == 0x0) {
free(cur);
break; // empty one
}
// extended name or deleted file
if (cur->fname[0] == 0xe5 || cur->atr == 0x0f) {
free(cur);
continue;
}
if (cur->fname[0] == '.' && (cur->fname[1] == '.' || !isalpha(cur->fname[1])) && !isalpha(cur->fname[1])) {
free(cur);
continue;
}
if (cur->atr & 0x10) { // directory
dir_t* subdir = init_dir(fat, cur, dir);
dir_process(fat, subdir);
add_subdir(dir, subdir);
} else {
file_t* file = init_file(fat, cur, dir);
add_file(dir, file);
}
}
cluster = get_next_cluster(fat, cluster);
}
}
void add_subdir(dir_t* dir, dir_t* subdir) {
// if we ran out of space...
if (dir->max_subdirs < dir->subdir_cnt) {
dir->max_subdirs += 100;
dir->subdirs = realloc(dir->subdirs, dir->max_subdirs * sizeof(dir_t*));
assert(dir->subdirs != NULL);
}
dir->subdirs[dir->subdir_cnt] = subdir;
dir->subdir_cnt += 1;
}
void add_file(dir_t* dir, file_t* file) {
// if we ran out of space...
if (dir->max_files < dir->file_cnt) {
dir->max_files += 100;
dir->files = realloc(dir->files, dir->max_files * sizeof(file_t*));
assert(dir->files != NULL);
}
dir->files[dir->file_cnt] = file;
dir->file_cnt += 1;
dir->byte_size += file->byte_size;
}