-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileSystem.c
More file actions
482 lines (409 loc) · 10.8 KB
/
fileSystem.c
File metadata and controls
482 lines (409 loc) · 10.8 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
** File: fileSystem.c
**
** Authors: Max Roth, Nicholas Jenis, Ernesto Soltero
**
** Description: Implementation of Simple File System
*/
#include "types.h"
#include "fileSystem.h"
#include "klib.h"
#include "user.h"
#include "c_io.h"
/*
** Create and initialize a file system, returning it's representative
** sfs_file_table structure
*/
void _sfs_init( void ) {
//Set file system to a point 1 gig into the RAM
fileSystem = (void *)RAM_START_ADDRESS;
//Make sure filesystem starts out clean
_memset((uint8_t *)fileSystem, 0, sizeof(&fileSystem));
sfs_entry* entry;
for(int i = 0; i < NUM_ENTRIES; i++) {
entry = &fileSystem->entries[i];
for(int j = 0; j < MAX_NAME_LENGTH; j++) {
entry->name[j] = '\0';
}
entry->size = 0;
entry->payload = 0;
entry->type = 0;
}
fileSystem->current_location = 0;
sfs_data* block;
for(int i = 0; i < NUM_BLOCKS; i++) {
block = &fileSystem->blocks[i];
block->next = 0;
for(int j = 0; j < DATA_BLOCK_SIZE - sizeof(uint8_t); j++) {
block->data[j] = 0;
}
}
for(int i = 0; i < MAX_NAME_LENGTH - MAX_FILE_NAME_LENGTH - 1; i++) {
fileSystem->directory[i] = '\0';
}
_sfs_create(ROOT, DIRECTORY);
_set_directory(ROOT);
// Set the location for the read buffer which is used to pass
// data back to the user from the file system
// Also, I have no idea where this is, but it is different than the fs
//sfs_read_buffer* readBuf = &fileSystem->read_buffer;
//readBuf->start = 0x30000000;
//readBuf->size = 0;
//readBuf->buf = (void *) readBuf->start;
//fileSystem->read_buffer.start = (void *)0x4000000;
//fileSystem->read_buffer->size = 0;
}
/*
** Create a new file with one data block or empty directory
** Return 0 on success, anything else on error
*/
uint8_t _sfs_create(char* name, uint8_t entry_type) {
//Special use case to create the root directory, since normal creation
//is dependant on the root existing
if(compare(name,ROOT) == 0 && entry_type == DIRECTORY) {
sfs_entry *entry = _sfs_exists(name, entry_type);
if(compare((char*)&entry->name,"") == 0) {
char* c = name;
for(int i = 0; i < MAX_FILE_NAME_LENGTH - 1 && *c != '\0'; i++) {
entry->name[i] = *c;
entry->name[i+1] = '\0';
c++;
}
entry->type = entry_type;
//c_puts("ROOT CREATED!\n");
return 7; //Return 7 on root creation
}
else {
//Root already exists...
//c_puts("ROOT DENIED!\n");
return -7; //Return -7 on root creation denial
}
}
if(len(name) <= 0) {
return -2; //Return -2 if no entry name is specified
}
char* entryName = _adjust_filename(name);
sfs_entry *entry = _sfs_exists(entryName, entry_type);
if(compare((char*)&entry->name,ROOT) != 0) {
return 1; //Return 1 if the entry already exists
}
//Go through the adjusted filename and make sure each directory exists
char* checkName = "";
char* checkNameTemp = checkName;
char* temp = entryName + 1;
*checkNameTemp = DIR_SEPERATOR;
checkNameTemp++;
*checkNameTemp = '\0';
c_puts("C:");
c_puts(entryName);
c_puts("|");
c_puts(checkName);
c_puts("\n");
while(*temp != '\0') {
// c_puts(temp);
// c_puts("|");
if(*temp == DIR_SEPERATOR) {
*checkNameTemp = '\0';
c_puts("\nT:");
c_puts(temp);
c_puts("|");
c_puts(checkName);
c_puts("\n");
entry = _sfs_exists(checkName, DIRECTORY);
if(compare((char*)&entry->name,ROOT) == 0) {
return 2; //Return 2 if the proper directories do not exist
}
}
*checkNameTemp = *temp;
checkNameTemp++;
*checkNameTemp = '\0';
temp++;
}
for(int i = 0; i < NUM_ENTRIES; ++i) {
sfs_entry* entry = &fileSystem->entries[i];
if( entry->name[0] != '\0' ) {
continue; //Entry in use
}
char* c = entryName;
for(int i = 0; i < MAX_NAME_LENGTH - 1 && *c != '\0'; i++) {
entry->name[i] = *c;
entry->name[i+1] = '\0';
c++;
}
if(entry_type == FILE) {
entry->payload = fileSystem->current_location;
fileSystem->current_location++;
}
else {
entry->payload = 0;
}
entry->size = 0;
entry->type = entry_type;
return 0;
}
return -1; //No open file entries
}
/*
** Delete an existing file or directory
*/
uint8_t _sfs_delete(char* f_name) {
char* filename = _adjust_filename(f_name);
sfs_entry *entry = _sfs_exists(filename, FILE);
if(compare((char*)&entry->name,ROOT) == 0) {
entry = _sfs_exists(filename, DIRECTORY);
if(compare((char*)&entry->name,ROOT) == 0) {
return 1; //No file entries with that name
}
}
/*if(file->payload == 0) {
return 0; //No data to delete, should never be the case
}*/
if(entry->type == FILE) {
sfs_data* ptr = &fileSystem->blocks[entry->payload];
while(ptr) {
sfs_data* current = ptr;
if(ptr->next == 0) {
ptr = 0;
}
else {
ptr = (sfs_data*) &fileSystem->blocks[ptr->next];
}
_memset((uint8_t *) current, 0, DATA_BLOCK_SIZE);
}
}
else if(entry->type == DIRECTORY) {
char* delete;
char* nameCheck;
sfs_entry *temp;
int flag;
//If the directory has any files/subfolders, do not delete
for(int j = 0; j< NUM_ENTRIES; ++j) {
temp = &fileSystem->entries[j];
nameCheck = (char*)&temp->name;
delete = (char*)&entry->name;
flag = 0;
if(len(nameCheck) > len(delete)) {
flag = 2;
while(*delete != '\0') {
if(*delete != *nameCheck) {
flag = 1;
}
nameCheck++;
delete++;
}
}
if(flag == 2) {
return 2; //Return 2 if there is a file or
//subfolder in this directory
}
}
}
_memset((uint8_t *)&entry->name, 0, sizeof(&entry->name));
entry->name[0] = '\0';
entry->size = 0;
return 0;
}
/*
** Read an existing file
*/
uint8_t* _sfs_read(char* f_name) {
uint8_t* result = 0;
*result = 0;
char* filename = _adjust_filename(f_name);
sfs_entry *entry = _sfs_exists(filename, FILE);
if(compare((char*)&entry->name,ROOT) == 0) {
//char* buf = "test";
//return (uint8_t *) buf;
return result;
}
// If there is no data here, return nothing
//if(entry->payload == 0) return 0;
//Impossible now, every file has at least one data block
//sfs_read_buffer* read_buffer = &fileSystem->read_buffer;
sfs_data* source = &fileSystem->blocks[entry->payload];
uint16_t totalToRead = entry->size;
uint8_t* buffer = result;
int emptyFile = 1;
while(source) {
uint16_t readSize = totalToRead;
if(readSize > DATA_BLOCK_SIZE - sizeof(uint8_t)) {
readSize = DATA_BLOCK_SIZE - sizeof(uint8_t);
}
// Copy the data out of the filesystem
for( int j=0; j < readSize; ++j ) {
emptyFile = 0;
*buffer = source->data[j];
buffer++;
//c_printf("\n%x", source->data[i]);
}
totalToRead -= readSize;
if(source->next) source = (sfs_data*) &fileSystem->blocks[source->next];
else source = 0;
}
// Use this for files that exist, but are empty. Using the
// space character means that the first character in the data
// can't be a space. We can use any other character though.
if(emptyFile) {
*buffer = ' ';
buffer++;
}
*buffer = '\0';
return result;
}
/*
** Write to an existing file
*/
uint8_t _sfs_write(char* f_name, uint16_t size, uint8_t* buffer, int doAppend) {
char* filename = _adjust_filename(f_name);
sfs_entry *entry = _sfs_exists(filename, FILE);
if(compare((char*)&entry->name,ROOT) == 0) {
return 1; //Return 1 if file DNE
}
int writeSpot = 0;
sfs_data* destination = &fileSystem->blocks[entry->payload];
if(doAppend) {
writeSpot = entry->size -1;
entry->size += size;
// Move to the right block
// We don't actually use multiple blocks yet
//uint8_t destination->next
}
else { // Overwrite what is there.
entry->size = size;
}
uint16_t totalToWrite = size;
while( totalToWrite ) {
uint16_t writeSize = totalToWrite;
if( writeSize + writeSpot > DATA_BLOCK_SIZE - sizeof(uint8_t))
writeSize = DATA_BLOCK_SIZE - sizeof(uint8_t);
// Copy the data from the buffer to our file system
for(int i=0; i < writeSize; ++i ) {
destination->data[writeSpot] = (uint8_t) *buffer;
buffer++;
writeSpot++;
}
writeSpot = 0;
totalToWrite -= writeSize;
if(totalToWrite) destination = &fileSystem->blocks[fileSystem->current_location++];
}
// We have finished writing
return 0;
}
/*
** List all files in the directory
** Weird thing in old SFS file, no directories???
*/
uint8_t* _sfs_list( void ) {
uint8_t* result = 0;
*result = 0;
uint8_t* buffer = result;
for(int i = 0; i < NUM_ENTRIES; ++i) {
sfs_entry *entry = &fileSystem->entries[i];
if(entry->name[0] != 0) {
// c_puts("-A");
c_printf("\n%x\n", entry->name[0]);
for(int j = 0; entry->name[j] != '\0'; j++) {
*buffer = entry->name[j];
buffer++;
}
*buffer = '\n';
buffer++;
}
}
*buffer = '\0';
return result;
// return (uint8_t *)1;
}
/*
** Checks if a file/directory exists
**
** Returns a pointer to the file/directory if it exists
** Returns a pointer to address 512 megabytes in RAM if it does not
** (This only seems to work at spaces in RAM that are empty...)
*/
sfs_entry* _sfs_exists( char* f_name, uint8_t filetype ) {
char* filename = _adjust_filename(f_name);
for(int i = 0; i < NUM_ENTRIES; ++i) {
sfs_entry *entry = &fileSystem->entries[i];
// Check for the right file
if(compare(filename, (char*)&entry->name) != 0 || \
entry->type != filetype)
continue;
return entry;
}
//No file found, return root
return &fileSystem->entries[0];
}
/*
** Return a pointer to the file system file table
*/
sfs_file_table* _get_fileSystem( void ) {
return fileSystem;
}
/*
** Returns a pointer to the string holding the current directory
*/
char* _get_directory( void ) {
return (char*)&fileSystem->directory;
}
/*
** Change the current directory
**
** Return 0 on success
** Return anything else on failure
*/
uint8_t _set_directory( char* new_dir ) {
if(compare(new_dir, ROOT) == 0) {
char* c = ROOT;
for(int i = 0; i < MAX_NAME_LENGTH - MAX_FILE_NAME_LENGTH - 2 \
&& *c != '\0'; i++) {
fileSystem->directory[i] = *c;
fileSystem->directory[i+1] = '\0';
c++;
}
return 0;
}
sfs_entry *entry = _sfs_exists(new_dir, DIRECTORY);
if(compare((char*)&entry->name,ROOT) == 0) {
return -1; //Directory doesn't exist
}
char* c = _adjust_filename(new_dir);
for(int i = 0; i < MAX_NAME_LENGTH - MAX_FILE_NAME_LENGTH - 2 \
&& *c != '\0'; i++) {
fileSystem->directory[i] = *c;
fileSystem->directory[i+1] = '\0';
c++;
}
return 0;
}
/**
** Update a filename to match the current directory
**/
char* _adjust_filename( char* filename ) {
if(*filename != DIR_SEPERATOR) {
char* result = "NO ISSUE";
char* tempResult = result;
char* dir = _get_directory();
char* tempDir = dir;
char* tempName = filename;
for(int i = 0; i < len(dir); i++) {
*tempResult = *tempDir;
tempResult++;
tempDir++;
}
if(compare(dir, ROOT) != 0) {
*tempResult = DIR_SEPERATOR;
tempResult++;
}
for(int j = 0; j < len(filename); j++) {
*tempResult = *tempName;
tempResult++;
tempName++;
}
*tempResult = '\0';
return result;
}
else {
return filename;
}
}