-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmfs.c
More file actions
666 lines (542 loc) · 16.6 KB
/
mfs.c
File metadata and controls
666 lines (542 loc) · 16.6 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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/**************************************************************
* Class:: CSC-415-02 Summer 2025
* Name::Phillip Davis Igor Tello Jared Aung Preet Vithani
* Student IDs::923980431, 923043807, 922772159, 923575806
* GitHub-Name::R3plug
* Group-Name::Team Kentucky Kernels
* Project:: Basic File System
*
* File:: mfs.c
*
* Description::
* This is the file system interface implementation.
* This is the implementation of the interface needed
* by the driver to interact with your filesystem.
*
**************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fsPath.h"
#include "mfs.h"
#include "dirLow.h"
#include "fsFreeSpace.h"
VCB* getVCB();
int findFreeDE(DE* parent);
void safeFree(DE* dir);
int expandDirectory(DE* dir);
int freeBlocks(ExtentTable* mem);
void freePPI(ppInfo* info);
int fs_mkdir(const char *pathname, mode_t mode)
{
ppInfo ppi;
int retPP = parsePath(pathname, &ppi);
printf("parsePath completed\n");
if (retPP != 0)
{
return -1;
}
if (ppi.index != -1)
{
return -2;
}
DE *newDir = createDir(50, ppi.parent);
printf("createDir completed\n");
int index = findFreeDE(ppi.parent);
if(index<0){
printf("No valid index received %d",index);
return -1;
}
ppi.parent[index].size = newDir[0].size;
ppi.parent[index].isDir = newDir[0].isDir;
ppi.parent[index].creationTime = newDir[0].creationTime;
ppi.parent[index].lastAccessTime = newDir[0].lastAccessTime;
ppi.parent[index].modificationTime = newDir[0].creationTime;
for(int i =0;i<newDir[0].mem.extentCount;i++){
ppi.parent[index].mem.extents[i].block = newDir[0].mem.extents[i].block;
ppi.parent[index].mem.extents[i].count = newDir[0].mem.extents[i].count;
ppi.parent[index].mem.extents[i].used = newDir[0].mem.extents[i].used;
}
ppi.parent[index].mem.extentCount = newDir[0].mem.extentCount;
printf("In mkdir, extentCount of newDir: %d\n",newDir[0].mem.extentCount);
strncpy(ppi.parent[index].name, ppi.lastElementName, strlen(ppi.lastElementName));
if (writeDir(ppi.parent) != 0)
{
return -3; // failed write to save dir
};
safeFree(ppi.parent);
return 0;
}
/**
* Returns an int indicating the position of a free
* directory entry in parent.
* if directory is full it expands it.
*/
int findFreeDE(DE* parent){
if(parent == NULL){
printf("Parent is NULL\n");
return -1;
}
if(parent->isDir!=1){
printf("Parent is not a directory\n");
return -1;
}
int dirEntries = parent[0].size/sizeof(DE);
char emptyDirName ='\0';
int index;
for(index = 0; index<dirEntries;index++){
if(parent[index].name[0]=='\0'){
printf("Index: %d\n",index);
return index;
}
}
if(expandDirectory(parent)!=0){
return -1;
};
return index++;
}
int expandDirectory(DE* dir){
VCB* tempVCB =getVCB();
int blockSize = tempVCB->blockSize;
int numEntries = 50;
int memNeeded = numEntries*sizeof(DE);
int blocksNeeded = (memNeeded+blockSize-1)/blockSize;
memNeeded = blocksNeeded*blockSize; //Accounts for allocating memory in blocks
DE* newDir = malloc(memNeeded);//initialize directory array
int actualEntries = memNeeded/sizeof(DE);//calculate the number of entries that fit in
//allocated memory
for(int i =2; i<actualEntries; i++){//Mark unused entries
newDir[i].name[0] ='\0';
}
uint32_t blocksAllocated =0;
Extent* dirMem = allocateFreeBlocks(blocksNeeded,&blocksAllocated);//get memory for directory
//printf("\nExtent count: %d\n", dirMem->count);
if(dirMem ==NULL){
printf("No memory allocated for root dir\n");
return -1;
}
int nextFreeExt = dir->mem.extentCount;
dir[0].mem.extents[nextFreeExt].count = dirMem->count;
dir[0].mem.extents[nextFreeExt].block = dirMem->block;
dir[0].mem.extents[nextFreeExt].used = 1;
dir[0].mem.extentCount++;
dir[0].size+=memNeeded;
return 0;
}
int fs_delete(char* filename){
ppInfo ppi;
int parseResult = parsePath(filename, &ppi);
if(parseResult !=0){
return -1; // ParsePath failed
}
if(ppi.index == -1){
safeFree(ppi.parent);
return -1; // File not found
}
DE* fileEntry = &ppi.parent[ppi.index];
// Make sure file is a file, not a directory
if(fileEntry->isDir){
safeFree(ppi.parent);
return -1; // Cannot delete a directory
}
// Free file's data blocks
if(freeBlocks(&fileEntry->mem) != 0){
safeFree(ppi.parent);
return -1; // Failed to free blocks
}
// Mark directory entry as not used
memset(fileEntry, 0, sizeof(DE));
// Write updated directory to disk
if(writeDir(ppi.parent) != 0){
safeFree(ppi.parent);
return -1; // Failed to write directory
}
safeFree(ppi.parent);
return 0;
}
int freeBlocks(ExtentTable* mem){
if(!mem) return -1;
for(uint32_t i = 0; i < mem->extentCount; i++){
if(mem->extents[i].used){
releaseBlocks(mem->extents[i].block, mem->extents[i].count);
mem->extents[i].used = 0; // Change extent to not used
}
}
mem->extentCount = 0;
return 0;
}
// This function will set the current working directory.
int fs_setcwd(char *pathname){
//We need to allocate memory for return value of parsePath
ppInfo* ppi = malloc(sizeof(ppInfo));
char *pathCpy = strdup(pathname);
int retVal = parsePath(pathCpy, ppi);
printf("ParsePath Index %d\n", ppi->index);
free(pathCpy);
//We need to check for errors
if(retVal < 0){
freePPI(ppi);
printf("fs_setcwd:ERROR IN PARSE PATH: %d\n", retVal);
return retVal;
}
//We need to check for errors
if(ppi->index < 0){
freePPI(ppi);
return -1;
}
DE* entry = &(ppi->parent[ppi->index]);
//We need to make sure last value is a valid directory
printf("in setcwd Entry[ppi->index].isDir: %d\n",entry->isDir);
printf("In setwcd Entry[ppi->index].Extentcount: %d\n",entry->mem.extentCount);
printf("PPI name: %s",entry->name);
printf("PPI Entry: %d\n",ppi->index);
printf("setcwd isDir %d\n",entry->isDir);
if(entry->isDir!=1){
printf("fs_setcwd: is not a valid path\n");
freePPI(ppi);
return -1;
}
//We need to load the directory to memory
DE* cwd = loadDir(entry);
printf("Current Dir extent count: %d\n",cwd[0].mem.extentCount);
if(!cwd){
printf("setcwd loadDir() failed\n");
freePPI(ppi);
return -1;
}
//We need to set current directory
freePPI(ppi);
setCwdDir(cwd);
//We need to update str value of CWD
pathCleaner(pathname);
return 0;
}
//This function gets the current working directory.
char* fs_getcwd(char *pathname, size_t size){
char* retVal = strncpy(pathname, getCWDStr(), size);
return retVal;
}
//this function (fs_isFile) returns 1 if it is a file, 0 if it not
int fs_isFile(char * filename){
//We need to allocate memory for return value of parsePath
ppInfo* ppi = malloc(sizeof(ppInfo));
if (ppi == NULL) {
return -1;
}
char *pathCpy = strdup(filename);
if (pathCpy == NULL) {
free(ppi);
return -1;
}
int retVal = parsePath(pathCpy, ppi);
free(pathCpy);
//We need to check for errors (does not exist)
if(ppi->index == -1){
freePPI(ppi);
return 0;
}
//We need to check for parse errors
if(retVal < 0){
freePPI(ppi);
printf("fs_isFile: ERROR IN PARSE PATH: %d\n", retVal);
return 0;
}
//We need to check whether it is not a directory
int isFile = !entryIsDir(ppi);
freePPI(ppi);
return isFile;
}
// This Function returns if the directory is file or directory.
int fs_isDir(char * filename){
// We need to allocate memory for return value of parsePath
//printf("Checking if is directory\n");
ppInfo* ppi = malloc(sizeof(ppInfo));
if (ppi == NULL) {
return -1;
}
char *pathCpy = strdup(filename);
if (pathCpy == NULL) {
freePPI(ppi);
return -1;
}
int retVal = parsePath(pathCpy, ppi);
free(pathCpy);
//We need to check for errors
if (ppi->index == -1) {
freePPI(ppi);
return 0;
}
//We need to check for parse errors
if (retVal < 0) {
freePPI(ppi);
printf("fs_isFile: ERROR IN PARSE PATH: %d\n", retVal);
return 0;
}
// We need to check whether it is a directory
retVal = entryIsDir(ppi);
freePPI(ppi);
return retVal;
}
// helper: return 1 if this loaded directory has only "." and ".."
static int isDirEmpty(DE *entries) {
int total = entries[0].size / sizeof(DE);
for (int i = 2; i < total; i++) {
if (entries[i].name[0] != '\0') {
return 0;
}
}
return 1;
}
int fs_rmdir(const char *pathname) {
// 1) parsePath
ppInfo *ppi = malloc(sizeof *ppi);
if (!ppi) return -1;
char *pathCpy = strdup(pathname);
if (!pathCpy) { free(ppi); return -1; }
int ret = parsePath(pathCpy, ppi);
free(pathCpy);
if (ppi->index < 0 || ret < 0) {
freePPI(ppi);
return -1;
}
// 2) must be a directory
if (!entryIsDir(ppi)) {
freePPI(ppi);
return -1;
}
DE *parent = ppi->parent;
int idx = ppi->index;
// 3) forbid removing root or cwd
if (parent == getRootDir() && idx == 0) {
freePPI(ppi);
return -1;
}
if (&parent[idx] == getCwd()) {
freePPI(ppi);
return -1;
}
// 4) load its contents and check empty
DE *child = loadDir(&parent[idx]);
if (!child) {
freePPI(ppi);
return -1;
}
if (!isDirEmpty(child)) {
safeFree(child);
freePPI(ppi);
return -1;
}
safeFree(child);
// 5) free its data blocks (extents)
freeBlocks(&parent[idx].mem);
// 6) remove the entry from parent
memset(&parent[idx], 0, sizeof(DE));
parent[0].modificationTime = time(NULL);
// 7) write updated parent directory back to disk
writeDir(parent);
freePPI(ppi);
return 0;
}
/*
int entryIsDir(ppInfo *ppi){
if(ppi == NULL || ppi->parent == NULL || ppi->index < 0){
return -1;
}
DE *entries = ppi->parent;
int index = ppi->index;
DE entry = entries[index];
return (entry.isDir != 0) ? 1 : 0;
}
*/
/**
* Check that the dir is not the cwd or root dir
* then free it
*/
void safeFree(DE* dir){
if(dir!=getCwd() && dir!=getRootDir()){
free(dir);
}
}
// fs_opendir opens a directory by loading all its DE from disk and return fdDir*
// handle that can be used to iterate through the entries
fdDir * fs_opendir (const char *pathname){
//printf("opening directory: %s\n", pathname);
ppInfo info;
// Parse the path into parent directory and target entry
memset(&info, 0, sizeof(ppInfo));
int result = parsePath(pathname, &info);
if(result != 0){
printf("Parse path failed\n");
return NULL;
}
DE* dir;
// check if the path is root directory
if(info.index > 0){
// get the DE at the desired index inside parent directory
dir = &info.parent[info.index];
}
else if (info.index == -2){
// root directory
dir = info.parent;
} else{
safeFree(info.parent);
printf("Directory invalid.\n");
return NULL;
}
//printf("directory name: %s\n", dir->name);
//printf("directory index: %d\n", info.index);
//printf("directory isDir: %d\n", dir->isDir);
// check directory exists and is a valid directory
if(!dir || dir->isDir != 1){
printf("Not a directory or null entry\n");
safeFree(info.parent);
return NULL;
}
fdDir* dirp = malloc(sizeof(fdDir));
if (!dirp){
printf("could not malloc fdDir dirp\n");
return NULL;
}
// allocate structure to hold directory entry information
DirHandle* handle = malloc(sizeof(DirHandle));
if (!handle){
printf("could not allocate DirHandle\n");
free(dirp);
return NULL;
}
// Compute total size and number of DEs
int totalBytes = dir->size;
int totalDEs = totalBytes / sizeof(DE);
handle->entries = malloc(totalBytes);
if (!handle->entries){
printf("could not malloc handle->entries\n");
free(handle);
free(dirp);
return NULL;
}
int currentOffset = 0;
// loop through each extent and load blocks into memory
for (int i = 0; i < dir->mem.extentCount; i++){
int startBlock = dir->mem.extents[i].block;
int numBlocks = dir->mem.extents[i].count;
void *buffer = malloc(numBlocks * BLOCK_SIZE);
if (!buffer){
printf("Failed to allocate buffer\n");
free(handle->entries);
free(handle);
free(dirp);
return NULL;
}
if (LBAread(buffer, numBlocks, startBlock) != numBlocks){
printf("LBAread failed\n");
free(buffer);
free(handle->entries);
free(handle);
free(dirp);
return NULL;
}
// Copy only the required bytes into entries array
int copySize ;
if (totalBytes - currentOffset > numBlocks * BLOCK_SIZE) {
copySize = numBlocks * BLOCK_SIZE;
}
else {
copySize = totalBytes - currentOffset;
}
memcpy((char *)handle->entries + currentOffset, buffer, copySize);
currentOffset += copySize;
free(buffer);
}
handle->totalEntries = totalBytes / sizeof(DE);
handle->currentIndex = 0;
dirp->d_reclen = sizeof(fdDir);
dirp->dirEntryPosition = 0;
dirp->di = NULL;
dirp->handle = handle;
safeFree(info.parent);
//printf("Handle total entries: %d\n", handle->totalEntries);
return dirp;
}
// read the next entry in directory
struct fs_diriteminfo *fs_readdir(fdDir *dirp){
//printf("Reading dir\n");
// validate directory and handle
if (!dirp || !dirp->handle){
printf("Invalid directory, fs_readdir\n");
return NULL;
}
if (dirp->di){
free(dirp->di);
dirp->di =NULL;
}
// Cast to DirHandle for access
DirHandle* handle = dirp->handle;
printf("cwd size = %d\n", handle->totalEntries);
// iterate through all the DEs until a valid entry is found or end is reacted
// for valid entry, fill the details in diriteminfo structure
while (handle->currentIndex < handle->totalEntries)
{
DE *entry = &handle->entries[handle->currentIndex++];
if (entry->name[0] == '\0')
continue; // skip empty entries
//printf("Reading entry: %s : %d\n", entry->name,entry->isDir);
struct fs_diriteminfo *info = malloc(sizeof(struct fs_diriteminfo));
if(!info) return NULL;
info->d_reclen = sizeof(struct fs_diriteminfo);
info->fileType = entry->isDir ? FT_DIRECTORY : FT_REGFILE;
strncpy(info->d_name, entry->name, 255);
info->d_name[255] = '\0';
dirp->di = info;
return dirp->di;
}
return NULL;
}
int fs_stat(const char *path, struct fs_stat *buf) {
char *pathCpy = strdup(path);
ppInfo info;
int ret = parsePath(pathCpy, &info);
free(pathCpy);
if (ret < 0) {
return -1; // not found or error
}
DE *entry = (info.index >= 0)
? &info.parent[info.index]
: info.parent; // “/” itself
VCB *vcb = getVCB();
if (!vcb) return -1;
buf->st_size = entry->size;
buf->st_blksize = vcb->blockSize;
buf->st_blocks = (entry->size + vcb->blockSize - 1)
/ vcb->blockSize;
buf->st_accesstime = entry->lastAccessTime;
buf->st_modtime = entry->modificationTime;
buf->st_createtime = entry->creationTime;
free(vcb);
return 0;
}
int fs_closedir(fdDir *dirp){
// Check if pointer is valid
if(dirp == NULL){
return -1;
}
memset(dirp,0,sizeof(fdDir));
// Free directory entry via handle
if(dirp->handle != NULL){
if(dirp->handle->entries != NULL){
free(dirp->handle->entries);
dirp->handle->entries = NULL;
}
free(dirp->handle);
dirp->handle = NULL;
}
if(dirp->di != NULL){
free(dirp->di);
dirp->di = NULL;
}
free(dirp);
return 0;
}
char* mc(int size){
char* temp = malloc(size);
return temp;
}