-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfat32_console.c
More file actions
322 lines (293 loc) · 9.31 KB
/
fat32_console.c
File metadata and controls
322 lines (293 loc) · 9.31 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "stddef.h"
#include "common.h"
#include "fat32.h"
#include "keyboard.h"
#include "kheap.h"
#include "kernio.h"
#include "terminal.h"
int handle_commands(f32 *fs, struct directory *dir, char *buffer);
void fat32_console(f32 *fs) {
printf("\n\nStarting up Fat32 console...\n");
printf("Reading root directory... ");
struct directory dir;
populate_root_dir(fs, &dir);
printf("Done.\n");
uint32_t bufflen = 24;
if(dir.num_entries > 0) {
printf("Root directory:\n");
print_directory(fs, &dir);
}
else {
printf("Root directory empty.\n");
}
printf("Welcome to the Version 1 Fat32 console.\n");
printf("Type 'help' to see available commands.\n");
char buffer[bufflen + 1];
while(1) {
printf(">> ");
uint32_t i;
for(i = 0; i < bufflen; i++) {
char c = get_ascii_char();
if(c == BS) {
if(i == 0) {
i--;
continue;
}
printf("%c", c);
i-=2;
continue;
}
if(c == EOT || c == ESC) {
i--;
continue;
}
printf("%c", c);
buffer[i] = c;
if(c == '\n') break;
}
if(i == bufflen) {
printf("Input too long.\n");
while(get_ascii_char() != '\n');
continue;
}
buffer[i] = 0;
// If it's just a return, print the current directory.
if(strlen(buffer) == 0) {
print_directory(fs, &dir);
continue;
}
uint32_t x;
int scanned = coerce_int(buffer, &x);
if(scanned == 0) {
int command_ret = handle_commands(fs, &dir, buffer);
if(!command_ret) {
printf("Invalid input. Enter a number or command.\n");
}
else if(command_ret == -1) {
break;
}
continue;
}
if(dir.num_entries <= x) {
printf("Invalid selection.\n");
continue;
}
if(dir.entries[x].dir_attrs & DIRECTORY) {
// It's a directory. chdir to that one.
uint32_t cluster = dir.entries[x].first_cluster;
if(cluster == 0) cluster = 2;
free_directory(fs, &dir);
populate_dir(fs, &dir, cluster);
print_directory(fs, &dir);
continue;
}
else {
uint8_t *file = readFile(fs, &dir.entries[x]);
for(i = 0; i < dir.entries[x].file_size; i++) {
printf("%c", file[i]);
}
kfree(file);
}
}
printf("Shutting down filesystem.\n");
free_directory(fs, &dir);
destroyFilesystem(fs);
}
struct bytes {
char *buff;
size_t len;
char *err;
};
void do_delete(f32 *fs, struct directory *dir, char *filename) {
printf("do_delete(%s)\n", filename);
uint32_t i;
for(i = 0; i < dir->num_entries; i++) {
if(strcmp(filename, dir->entries[i].name) == 0) {
if(dir->entries[i].dir_attrs & DIRECTORY) {
struct directory subdir;
populate_dir(fs, &subdir, dir->entries[i].first_cluster);
uint32_t j;
for(j = 0; j < subdir.num_entries; j++) {
// Delete these last!
if(strcmp(subdir.entries[j].name, ".") == 0) {
// Don't recur on current directory!
continue;
}
if(strcmp(subdir.entries[j].name, "..") == 0) {
// Don't recur on parent directory.
continue;
}
do_delete(fs, &subdir, subdir.entries[j].name);
}
// Now delete '.' and '..'
for(j = 0; j < subdir.num_entries; j++) {
if(strcmp(subdir.entries[j].name, ".") == 0) {
// Don't recur on current directory!
delFile(fs, dir, subdir.entries[j].name);
continue;
}
if(strcmp(subdir.entries[j].name, "..") == 0) {
// Don't recur on parent directory.
delFile(fs, dir, subdir.entries[j].name);
continue;
}
}
free_directory(fs, &subdir);
// Finally, delete the directory itself.
delFile(fs, dir, filename);
}
else {
delFile(fs, dir, dir->entries[i].name);
}
}
}
}
void do_cat(f32 *fs, struct directory *dir, char *filename) {
uint32_t i;
for(i = 0; i < dir->num_entries; i++) {
if(strcmp(filename, dir->entries[i].name) == 0) {
printf("File already exists. del the file first.\n");
return;
}
}
printf("Ctrl+D to end file.\n");
uint32_t index = 0;
uint32_t buffsize = 1024;
uint32_t currlinelen = 0;
uint8_t *file = kmalloc(buffsize);
while(1) {
char c = get_ascii_char();
if(c == EOT) {
break;
}
printf("%c", c);
if(c == BS) {
if(currlinelen > 0) {
index--;
currlinelen--;
}
continue;
}
file[index++] = c;
currlinelen++;
if(c == '\n') {
currlinelen = 0;
}
if(index == buffsize) {
buffsize *= 2;
file = krealloc(file, buffsize);
}
}
writeFile(fs, dir, file, filename, index);
kfree(file);
}
void do_touch(f32 *fs, struct directory *dir, char *filename) {
writeFile(fs, dir, (uint8_t *)"", filename, 0);
}
int scan_command(char *buffer, char **comm, char **fname) {
char *buffscan = buffer;
if(!*buffscan) {
// There's nothing in the buffer.
return 0;
}
// skip any whitespace
while(*buffscan && *buffscan == ' ') {
buffscan++;
}
// Make sure there's something afterwards
if(!*buffscan) {
return 0;
}
// Point comm at the first non-whitespace character.
*comm = buffscan;
// Find a space.
while(*buffscan && *buffscan != ' ') {
buffscan++;
}
if(!*buffscan) {
// There's no more in the string
return 1;
}
// Terminate the string.
*buffscan = 0;
buffscan++;
// skip any whitespace
while(*buffscan && *buffscan == ' ') {
buffscan++;
}
// If there's no more string left, return 1.
if(!*buffscan) {
return 1;
}
*fname = buffscan;
// Chop any space off the end.
while(*buffscan && *buffscan != ' ') {
buffscan++;
}
*buffscan = 0;
return 2;
}
int handle_commands(f32 *fs, struct directory *dir, char *buffer) {
char *command = NULL;
char *filename = NULL;
int scanned = scan_command(buffer, &command, &filename);
if(scanned == 0) { printf("Failed to parse command.\n"); return 0; }
int ret = 0;
if(strcmp(command, "mkdir") == 0) {
if(filename != NULL && strlen(filename) > 0) {
printf("Making directory [%s].\n", filename);
mkdir(fs, dir, filename);
}
else {
printf("Need a directory name.\n");
}
ret = 1;
}
if(strcmp(command, "touch") == 0) {
if(filename != NULL && strlen(filename) > 0) {
do_touch(fs, dir, filename);
}
ret = 1;
}
if(strcmp(command, "del") == 0) {
if(filename != NULL && strlen(filename) > 0) {
do_delete(fs, dir, filename);
}
else {
printf("Need a file/directory name.\n");
}
ret = 1;
}
if(strcmp(command, "cat") == 0) {
if(filename != NULL && strlen(filename) > 0) {
do_cat(fs, dir, filename);
}
else {
printf("Need a filename.\n");
}
ret = 1;
}
if(strcmp(command, "freeclusters") == 0) {
printf("Free clusters in FAT: %d\n", count_free_clusters(fs));
ret = 1;
}
if(strcmp(command, "exit") == 0) {
printf("See ya!\n");
ret = -1;
}
if(strcmp(command, "help") == 0) {
printf("Commands are: \n");
printf("\t[a number] -> a number corresponding with a file or directory will print that file or enter that directory.\n");
printf("\t(return) -> pressing return without entering a command will list the current directory. Entries marked with a 'D' next to their names are directories.\n");
printf("\tmkdir [dirname] -> Create a directory in the current directory.\n");
printf("\ttouch [filename] -> Create an empty file in the current directory.\n");
printf("\tcat [filename] -> Creates a file named 'filename' and lets you type into it. Ctrl+D ends the file.\n");
printf("\tdel [filename | dirname] -> Delete a file or (recursively) a directory.\n");
printf("\tfreeclusters -> Count the free clusters available in the filesystem.\n");
printf("\texit -> Exit the FAT32 shell. This gracefully closes the filesystem. Always do this before shutting down the kernel.\n");
ret = 1;
}
free_directory(fs, dir);
populate_dir(fs, dir, dir->cluster);
return ret;
}