-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmfs.c
459 lines (383 loc) · 10.9 KB
/
mfs.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
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
/**************************************************************
* Class: CSC-415-03 Spring 2023
* Names: Danial Tahir, Chris Camano, Mahek Delawala, Savjot Dhillon
* Student IDs: 920838929, 921642160, 922968394, 918239054
* GitHub Name: DanTahir
* Group Name: Segfault
* Project: Basic File System
*
* File: mfs.c
*
* Description: Define functions from the file system interface
*
**************************************************************/
#include "mfs.h"
#include <string.h>
int fs_mkdir(const char *pathname, mode_t mode){
DirEntry * dir = dirInstance();
dirCopyWorking(&dir);
char dirToMake[NAMELEN];
int traverseReturn = dirTraversePath(&dir, pathname, dirToMake);
printf("dir to make - %s\n", dirToMake);
if(traverseReturn == -1){
printf("traverse path failed");
free(dir);
dir = NULL;
return -1;
}
int i;
uint64_t dirCount = dir[0].size / sizeof(DirEntry);
for(i = 0; i < dirCount; i++){
int strcmpVal = strncmp(dir[i].name, dirToMake, NAMELEN - 1);
if (strcmpVal == 0){
printf("dirToMake found in dir\n");
free(dir);
dir = NULL;
return -1;
}
}
uint64_t newLocation = dirInitNew(dir[0].location);
if (newLocation == 0){
printf("volume full\n");
return -1;
}
dirAddEntry(&dir, dirToMake, newLocation, sizeof(DirEntry) * 2, 1);
free(dir);
dir = NULL;
return 0;
}
int fs_setcwd(char *pathname){
char finalDirName[NAMELEN];
int traverseRet = dirTraversePath(&workingDir, pathname, finalDirName);
if (traverseRet == -1) {
return -1;
}
int i;
uint64_t dirCount = workingDir[0].size / sizeof(DirEntry);
for(i = 0; i < dirCount; i++){
int strcmpVal = strcmp(workingDir[i].name, finalDirName);
if(strcmpVal == 0){
break;
}
}
if (i == dirCount){
return -1;
}
if (workingDir[i].isDir != 1){
return -1;
}
dirSetWorking(workingDir[i].location);
return 0;
}
int fs_delete(char* path){
/*
load a directory current dir
*/
DirEntry * dir = dirInstance();
dirCopyWorking(&dir);
char fileName[NAMELEN];
/*
Traverse the file system to the exact location of the file and load the appropriate directory.
if the entries in the proper directory have a matching name to the targeted file name free the struct
*/
int traverseReturn = dirTraversePath(&dir, path, fileName);
if (traverseReturn == -1){
printf("path invalid\n");
return -1;
}
uint64_t dirCount = dir[0].size / sizeof(DirEntry);
for (int i = 0; i < dirCount; i++) {
DirEntry* entry = &dir[i];
if (strcmp(entry->name, fileName) == 0) {
if(entry->isDir == 1){
printf("The requested file is a directory");
free(dir);
return -1;
}
bitmapFreeFileSpace(entry->size,entry->location);
dirRemoveEntry(&dir, i);
free(dir);
return 0;
}
}
printf("Error file not found");
free(dir);
return -1;
}
int fs_stat(const char *path, struct fs_stat *buf){
/*
load a directory based on the working directory
*/
DirEntry * dir = dirInstance();
dirCopyWorking(&dir);
/*
Traverse the file system based on the path provided to the exact location of the file and load
the appropriate directory. if the entries in the proper directory have a matching name to the
targeted file name free the struct
*/
char fileName[NAMELEN];
DirEntry* entry;
int traverseReturn = dirTraversePath(&dir, path, fileName);
if (traverseReturn == -1){
printf("path invalid\n");
free(dir);
return -1;
}
uint64_t dirCount = dir[0].size / sizeof(DirEntry);
int i;
for (i = 0; i < dirCount; i++) {
entry = &dir[i];
if (strcmp(entry->name, fileName) == 0) {
break;
}
}
if(i == dirCount){
printf("file not found\n");
free(dir);
return -1;
}
VCB * vcb = getVCBG();
buf->st_size=entry->size; /* total size, in bytes */
buf->st_blksize=vcb->blockSize; /* blocksize for file system I/O */
buf->st_blocks=vcb->blockCount; /* number of 512B blocks allocated */
buf->st_accesstime=0; /* time of last access */
buf->st_modtime=0; /* time of last modification */
buf->st_createtime=0; /* time of last status change */
buf->fileType=entry->isDir;
free(dir);
free(vcb);
return 0;
}
// Returns a pointer to the "pathname" directory
fdDir * fs_opendir(const char *pathname){
VCB* vcb = getVCBG();
char dirToOpen[NAMELEN];
DirEntry * dir = dirInstance();
dirCopyWorking(&dir);
int traverseReturn = dirTraversePath(&dir,pathname,dirToOpen);
if(traverseReturn==-1){
//Error, free space and put pointer to NULL
printf("Traverse Failed");
free(dir);
free(vcb);
dir=NULL;
vcb=NULL;
return NULL;
}
if(dirToOpen[0]=='\0'){
fdDir* myDir = malloc(sizeof(fdDir));
myDir->d_reclen = sizeof(fdDir) ;
myDir->dirEntryPosition = 0 ;
myDir->directoryStartLocation =dir[0].location;
free(dir);
free(vcb);
return myDir;
}
uint64_t dirCount = dir[0].size / sizeof(DirEntry);
for(int i=0;i<dirCount;i++){
//Check if name matches and is it a directory
if(strncmp(dir[i].name, dirToOpen, NAMELEN-1) == 0 && dir[i].isDir){
//Dir found, Load it into memory
fdDir* myDir = malloc(sizeof(fdDir));
myDir->d_reclen = sizeof(fdDir);
myDir->dirEntryPosition = 0;
myDir->directoryStartLocation =dir[i].location;
free(dir);
free(vcb);
return myDir;
}
}
printf("dir not found or not a directory");
free(vcb);
free(dir);
return NULL;
}
int fs_closedir(fdDir* dirp) {
if (dirp == NULL) {
return -1;
}
free(dirp);
return 0;
}
struct fs_diriteminfo *fs_readdir(fdDir *dirp){
if(dirp==NULL) {
return NULL;
}
DirEntry * dir = dirInstance();
//dir read on the directory
//to the location pointed by dirp-> startlocation
//based on the number of entry position
dirRead(&dir,dirp->directoryStartLocation);
uint64_t dirCount = dir[0].size / sizeof(DirEntry);
int dep = dirp->dirEntryPosition;
if (dep >= dirCount){
free(dir);
dir = NULL;
return NULL;
}
//Read directory here
struct fs_diriteminfo* directory = malloc(sizeof(struct fs_diriteminfo));
strcpy(directory->d_name,dir[dep].name);
directory->d_reclen = sizeof(struct fs_diriteminfo);
if(dir[dep].isDir){
directory->fileType='d';
}
else{
directory->fileType='f';
}
free(dir);
dir = NULL;
dirp->dirEntryPosition++;
return directory;
}
int fs_isDir(char* pathname){
struct fs_stat fileStat;
if(fs_stat(pathname,&fileStat)<0){
printf("Error during fs_stat");
//Have to add errro codes in fs_stat method
}
else{
return fileStat.fileType;
}
}
int fs_isFile(char* filename){
struct fs_stat fileStat;
if(fs_stat(filename,&fileStat)<0){
printf("Error during fs_stat");
//Have to add errro codes in fs_stat method
}
else{
return !fileStat.fileType;
}
}
// This function will remove the directory
int fs_rmdir(const char *pathname)
{
int ret;
struct fs_stat fileStat;
bool DirEmpty;
DirEntry * dir = dirInstance();
dirCopyWorking(&dir);
char dirNameToDel[NAMELEN];
//DirEntry* entry = &dir[i];
// Check for Valid Path
int traverseReturn = dirTraversePath(&dir, pathname, dirNameToDel);
printf("Dir to Delete - %s\n", dirNameToDel);
if(traverseReturn != 0){
printf("Traverse path failed\n");
free(dir);
dir = NULL;
return PATH_NOT_FOUND;
}
uint64_t dirCount = dir[0].size / sizeof(DirEntry);
int i;
for(i = 2; i < dirCount; i++){
int strcmpVal = strcmp(dirNameToDel, dir[i].name);
if(strcmpVal == 0){
break;
}
}
if (i == dirCount){
printf("dir to delete not found\n");
free(dir);
dir=NULL;
return -1;
}
if(dir[i].isDir == 0){
printf("this is a file, not a directory\n");
free(dir);
dir=NULL;
return -1;
}
DirEntry * dirToDel = dirInstance();
dirRead(&dirToDel, dir[i].location);
dirCount = dirToDel[0].size / sizeof(DirEntry);
if (dirCount > 2){
printf("directory not empty\n");
free(dir);
dir=NULL;
free(dirToDel);
dirToDel = NULL;
return -1;
}
bitmapFreeFileSpace(dir[i].size, dir[i].location);
dirRemoveEntry(&dir, i);
free(dir);
dir=NULL;
free(dirToDel);
dirToDel = NULL;
return 0;
}
// This function will return the current working directory
// or the present working directory
char * fs_getcwd(char *pathname, size_t size)
{
//int maxDepth = 30;
DirEntry * dir = dirInstance();
//char tokens[maxDepth][NAMELEN];
dirCopyWorking(&dir);
pathname[0] = '\0';
/*
for(int i = 0; i < maxDepth; i++){
tokens[i][0] = '\0';
}
*/
int mySize = (int)size;
//int tokenCount = 0;
while(1){
if (dir[0].location == dir[1].location){
break;
}
// printf(". location - %lu\n", dir[0].location);
// printf(".. location - %lu\n", dir[1].location);
uint64_t lastLocation = dir[0].location;
dirRead(&dir,dir[1].location);
// printf("new . location - %lu\n", dir[0].location);
// printf("new .. location - %lu\n", dir[1].location);
uint64_t dirCount = dir[0].size / sizeof(DirEntry);
int i;
for(i = 0; i < dirCount; i++){
if(dir[i].location == lastLocation){
break;
}
}
if (i == dirCount){
printf("critical error, directory not found in parent\n");
free(dir);
dir = NULL;
return NULL;
}
mySize = mySize - strlen(dir[i].name);
if (mySize < 0){
break;
}
char * pathnameCopy = malloc(size);
strcpy(pathnameCopy, pathname);
sprintf(pathname, "/%s%s", dir[i].name, pathnameCopy);
free(pathnameCopy);
/*
strcpy(tokens[tokenCount], dir[i].name);
tokenCount++;
if (tokenCount == maxDepth){
break;
}
*/
}
/*
tokenCount--;
int mySize = (int)size;
for(tokenCount; tokenCount > -1; tokenCount--){
mySize = mySize - strlen(tokens[tokenCount]);
if (mySize < 0){
break;
}
sprintf(pathname, "%s/%s", pathname, tokens[tokenCount]);
}
*/
if(pathname[0] == '\0'){
strncpy(pathname, "/", size);
}
free(dir);
dir = NULL;
return pathname;
}