-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcvfs.cpp
More file actions
657 lines (550 loc) · 15.8 KB
/
cvfs.cpp
File metadata and controls
657 lines (550 loc) · 15.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
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
#include "cvfs.h"
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Global variables or objects used in the project
//
/////////////////////////////////////////////////////////////////////////////////////////////////
BootBlock bootobj;
SuperBlock superobj;
UAREA ureaobj;
PINODE head = NULL;
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Function Name: InitializeUAREA
// Description: It is used to initialize UAREA members
// Author: Aditya Harishchandra Chavan
// Date: 13/01/2026
//
/////////////////////////////////////////////////////////////////////////////////////////////////
void InitializeUAREA()
{
strcpy(ureaobj.ProcessName, "MyExe");
int i = 0;
for(i = 0; i < MAXOPENFILES; i++)
{
ureaobj.UFDT[i] = NULL;
}
printf("CVFS: UAREA gets initialized successfully!\n");
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Function Name: InitializeSuperBlock
// Description: It is used to initialize super block member
// Author: Aditya Harishchandra Chavan
// Date: 13/01/2026
//
/////////////////////////////////////////////////////////////////////////////////////////////////
void InitializeSuperBlock()
{
superobj.TotalInodes = MAXINODE;
superobj.FreeInodes = MAXINODE;
printf("CVFS: Super block gets initialized successfully!\n");
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Function Name: CreateDILB
// Description: It is used to linked list of inodes
// Author: Aditya Harishchandra Chavan
// Date: 13/01/2026
//
/////////////////////////////////////////////////////////////////////////////////////////////////
void CreateDILB()
{
int i = 0;
PINODE newn = NULL;
PINODE temp = head;
for(i = 1; i <= MAXINODE; i++)
{
newn = (PINODE)malloc(sizeof(INODE));
strcpy(newn->FileName, "\0");
newn->InodeNumber = i;
newn->FileSize = 0;
newn->FileType = 0;
newn->ActualFileSize = 0;
newn->ReferenceCount = 0;
newn->Permission = 0;
newn->Buffer = NULL;
newn->next = NULL;
if(temp == NULL) // LL is empty
{
head = newn;
temp = head;
}
else // LL contains at least 1 node
{
temp->next = newn;
temp = temp->next;
}
}
printf("CVFS: DILB created successfully!\n");
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Function Name: StartAuxillaryDataInitialization
// Description: It is used to call all such functions which are used to initialize
// auxillary data
// Author: Aditya Harishchandra Chavan
// Date: 13/01/2026
//
/////////////////////////////////////////////////////////////////////////////////////////////////
void StartAuxillaryDataInitialization()
{
strcpy(bootobj.Information, "Booting process of CVFS is done!\n");
printf("%s", bootobj.Information);
InitializeSuperBlock();
CreateDILB();
InitializeUAREA();
printf("CVFS: Auxillary data initialized successfully!\n");
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Function Name: DisplayHelp
// Description: It is used to display the help
// Author: Aditya Harishchandra Chavan
// Date: 14/01/2026
//
/////////////////////////////////////////////////////////////////////////////////////////////////
void DisplayHelp()
{
printf("---------------------------------------------------------\n");
printf("-----------------------CVFS help-------------------------\n");
printf("---------------------------------------------------------\n");
printf("man: It is used to display the manual page\n");
printf("clear: It is used to clear the terminal\n");
printf("creat: It is used to create a new file\n");
printf("write: It is used to write data into the file\n");
printf("read: It is used to read the data from the file\n");
printf("stat: It is used to display the file information\n");
printf("unlink: It is used to delete the file\n");
printf("exit: It is used to exit the application\n");
printf("---------------------------------------------------------\n");
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Function Name: DisplayManPage
// Description: It is used to display the man page
// Author: Aditya Harishchandra Chavan
// Date: 14/01/2026
//
/////////////////////////////////////////////////////////////////////////////////////////////////
void DisplayManPage(char Name[20])
{
if(strcmp("ls", Name) == 0)
{
printf("About: Used to list names of all files\n");
printf("Usage: ls\n");
}
else if(strcmp("man", Name) == 0)
{
printf("About: Used to display man page\n");
printf("Usage: man [command]\n");
printf("command: The command written as parameter\n");
}
else if(strcmp("exit", Name) == 0)
{
printf("About: Used to terminate the shell\n");
printf("Usage: exit\n");
}
else if(strcmp("clear", Name) == 0)
{
printf("About: Used to clear the shell\n");
printf("Usage: clear\n");
}
else if(strcmp("help", Name) == 0)
{
printf("About: Used to display help page\n");
printf("Usage: help\n");
}
else if(strcmp("unlink", Name) == 0)
{
printf("About: Used to unlink/delete a file\n");
printf("Usage: unlink [filename]\n");
}
else if(strcmp("write", Name) == 0)
{
printf("About: Used to write data in file\n");
printf("Usage: write [fd] (file descriptor)\n");
}
else if(strcmp("creat", Name) == 0)
{
printf("About: Used to create a new file\n");
printf("Usage: creat [filename] [permissions] (rwx)\n");
printf("Returns file descriptor. Important for other operations.\n");
}
else if(strcmp("read", Name) == 0)
{
printf("About: Used to read data from a file\n");
printf("Usage: read [file descriptor] [number of bytes to be read]\n");
}
else if(strcmp("stat", Name) == 0)
{
printf("About: Used to display file metadata\n");
printf("Usage: stat [filename]\n");
}
else
{
printf("No manual entry for %s", Name);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Function Name: IsFileExist
// Description: It is used to check if file is already present or not
// Input: It accepts file name
// Output: It returns boolean
// Author: Aditya Harishchandra Chavan
// Date: 16/01/2026
//
/////////////////////////////////////////////////////////////////////////////////////////////////
bool IsFileExist
(
char *name // file name
)
{
PINODE temp = head;
bool bFlag = false;
while(temp != NULL)
{
if((strcmp(name, temp->FileName) == 0) && (temp->FileType == REGULARFILE))
{
bFlag = true;
break;
}
temp = temp->next;
}
return bFlag;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Function Name: CreateFile
// Description: It is used to create new regular file
// Input: It accepts file name and permissions
// Output: It returns the file descriptor
// Author: Aditya Harishchandra Chavan
// Date: 16/01/2026
//
/////////////////////////////////////////////////////////////////////////////////////////////////
int CreateFile
(
char *name, // name of new file
int permission // permission for that file
)
{
PINODE temp = head;
int i = 0;
printf("Total number of Inodes remaining: %d\n", superobj.FreeInodes);
// if name not given
if(NULL == name)
{
return ERR_INVALID_PARAMETER;
}
// if permission value is invalid
// permission 1 is read
// permission 2 is write
// permission 3 is read + write
if(permission < 1 || permission > 3)
{
return ERR_INVALID_PARAMETER;
}
// if inodes are full
if(superobj.FreeInodes == 0)
{
return ERR_NO_INODES;
}
// if file is already present
if(IsFileExist(name) == true)
{
return ERR_FILE_ALREADY_EXIST;
}
// search empty inode
while(temp != NULL)
{
if(temp->FileType == 0)
{
break;
}
temp = temp->next;
}
// inode which doesn't have regular file type as 0, rare
if(temp == NULL)
{
printf("There is no Inode.\n");
return ERR_NO_INODES;
}
// search for empty UDFT entry
// 0, 1, 2 are reserved just like the actual linux/unix
for(i = 3; i < MAXOPENFILES; i++)
{
if(NULL == ureaobj.UFDT[i])
{
break;
}
}
// UFDT is full
if(i == MAXOPENFILES)
{
return ERR_MAX_FILES_OPEN;
}
// allocate memory for file table
ureaobj.UFDT[i] = (PFILETABLE)malloc(sizeof(FILETABLE));
// initialize file table
ureaobj.UFDT[i]->ReadOffset = 0;
ureaobj.UFDT[i]->WriteOffset = 0;
ureaobj.UFDT[i]->Mode = permission;
// connect file table with inode
ureaobj.UFDT[i]->ptrinode = temp;
// initialize elements of inode
// can use temp
strcpy(ureaobj.UFDT[i]->ptrinode->FileName, name);
ureaobj.UFDT[i]->ptrinode->FileSize = MAXFILESIZE;
ureaobj.UFDT[i]->ptrinode->ActualFileSize = 0;
ureaobj.UFDT[i]->ptrinode->FileType = REGULARFILE;
ureaobj.UFDT[i]->ptrinode->ReferenceCount = 1;
ureaobj.UFDT[i]->ptrinode->Permission = permission;
// allocate memory for file data
ureaobj.UFDT[i]->ptrinode->Buffer = (char *)malloc(MAXFILESIZE);
// important to decrease this
superobj.FreeInodes--;
return i; // file descriptor
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Function Name: lsFile()
// Description: It is used to list all files
// Author: Aditya Harishchandra Chavan
// Date: 16/01/2026
//
/////////////////////////////////////////////////////////////////////////////////////////////////
// ls -l
void lsFile()
{
PINODE temp = head;
printf("---------------------------------------------------------\n");
printf("------------------CVFS files information-----------------\n");
printf("---------------------------------------------------------\n");
// todo add header
while(temp != NULL)
{
if(temp->FileType != 0)
{
printf("%d\t%s\t%d bytes\n", temp->InodeNumber, temp->FileName, temp->ActualFileSize);
}
temp = temp->next;
}
printf("---------------------------------------------------------\n");
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Function Name: UnlinkFile()
// Description: It is used to delete a file
// Input: File name
// Author: Aditya Harishchandra Chavan
// Date: 22/01/2026
//
/////////////////////////////////////////////////////////////////////////////////////////////////
int UnlinkFile
(
char *name
)
{
int i = 0;
if(NULL == name)
{
return ERR_INVALID_PARAMETER;
}
if(IsFileExist(name) == false)
{
return ERR_FILE_NOT_EXIST;
}
// travel the UFDT
for(i = 0; i < MAXOPENFILES; i++)
{
if(ureaobj.UFDT[i] != NULL)
{
if(strcmp(ureaobj.UFDT[i]->ptrinode->FileName, name) == 0)
{
// deallocate memory of Buffer
free(ureaobj.UFDT[i]->ptrinode->Buffer);
// for later use, should be NULL
ureaobj.UFDT[i]->ptrinode->Buffer = NULL;
// reset all values of inode
// do not deallocate memory of inode
ureaobj.UFDT[i]->ptrinode->FileSize = 0;
ureaobj.UFDT[i]->ptrinode->ActualFileSize = 0;
ureaobj.UFDT[i]->ptrinode->FileType = 0;
ureaobj.UFDT[i]->ptrinode->ReferenceCount = 0; // todo: might have to change this
ureaobj.UFDT[i]->ptrinode->Permission = 0;
memset(ureaobj.UFDT[i]->ptrinode->FileName, '\0', sizeof(ureaobj.UFDT[i]->ptrinode->FileName));
// deallocate memory of file table
free(ureaobj.UFDT[i]);
// set NULL to UDFT
ureaobj.UFDT[i] = NULL;
// free node increment
superobj.FreeInodes++;
// important to break
break;
} // end of if
} // end of if
} // end of for
return EXECUTE_SUCCESS;
} // end of function
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Function Name: WriteFile()
//
// Description: It is used to write the data into the file
//
// Input: File descriptor
// Address of Buffer which contains data
// Size of data that we want to write
//
// Output: Number of bytes successfully read
//
// Author: Aditya Harishchandra Chavan
// Date: 22/01/2026
//
/////////////////////////////////////////////////////////////////////////////////////////////////
int WriteFile
(
int fd,
char *data,
int size
)
{
printf("File descriptor: %d\n", fd);
printf("Data that we want to write: %s\n", data);
printf("Size of data: %d\n", size);
// invalid fd
if(fd < 0 || fd > MAXOPENFILES)
{
return ERR_INVALID_PARAMETER;
}
// fd points to NULL
if(ureaobj.UFDT[fd] == NULL)
{
return ERR_FILE_NOT_EXIST;
}
// no write permission
if(ureaobj.UFDT[fd]->ptrinode->Permission < WRITE)
{
return ERR_PERMISSION_DENIED;
}
// insufficient space
if(MAXFILESIZE - ureaobj.UFDT[fd]->WriteOffset < size)
{
return ERR_INSUFFICIENT_SPACE;
}
// write data into the file
strncpy(ureaobj.UFDT[fd]->ptrinode->Buffer + ureaobj.UFDT[fd]->WriteOffset, data, size);
// update write offset
ureaobj.UFDT[fd]->WriteOffset = ureaobj.UFDT[fd]->WriteOffset + size;
// update the actual file size
ureaobj.UFDT[fd]->ptrinode->ActualFileSize = ureaobj.UFDT[fd]->ptrinode->ActualFileSize + size;
return size;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Function Name: ReadFile()
//
// Description: It is used to read the data into the file
//
// Input: File descriptor
// Address of empty buffer
// Size of data that we want to read
//
// Output: Number of bytes successfully read
//
// Author: Aditya Harishchandra Chavan
// Date: 22/01/2026
//
/////////////////////////////////////////////////////////////////////////////////////////////////
int ReadFile
(
int fd,
char *data,
int size
)
{
// invalid FD
if(fd < 0 || fd > MAXOPENFILES)
{
return ERR_INVALID_PARAMETER;
}
// invalid parameter
if(data == NULL)
{
return ERR_INVALID_PARAMETER;
}
// invalid size
if(size <= 0)
{
return ERR_INVALID_PARAMETER;
}
// invalid fd
if(ureaobj.UFDT[fd] == NULL)
{
return ERR_FILE_NOT_EXIST;
}
// invalid permission
if(ureaobj.UFDT[fd]->ptrinode->Permission < READ)
{
return ERR_PERMISSION_DENIED;
}
// insufficient data
if((MAXFILESIZE - ureaobj.UFDT[fd]->ReadOffset) < size)
{
return ERR_INSUFFICIENT_DATA;
}
// read the data
strncpy(data, ureaobj.UFDT[fd]->ptrinode->Buffer + ureaobj.UFDT[fd]->ReadOffset, size);
// update the read offset
ureaobj.UFDT[fd]->ReadOffset = ureaobj.UFDT[fd]->ReadOffset + size;
return size;
}
int StatFile(char *name) // accept name and display file metadata
{
PINODE temp = head;
// if name not given
if(NULL == name)
{
return ERR_INVALID_PARAMETER;
}
// search empty inode
while(temp != NULL)
{
if(strcmp(name, temp->FileName) == 0)
{
break;
}
temp = temp->next;
}
if(temp == NULL)
{
return ERR_FILE_NOT_EXIST;
}
printf("\n----- File Information -----\n");
printf("File Name : %s\n", temp->FileName);
printf("Inode Number : %d\n", temp->InodeNumber);
printf("File Size : %d\n", temp->FileSize);
printf("Actual File Size : %d\n", temp->ActualFileSize);
printf("Reference Count : %d\n", temp->ReferenceCount);
if(temp->FileType == 1)
{
printf("File Type : Regular File\n");
}
printf("Permission : ");
if(temp->Permission == 1)
{
printf("r--\n");
}
else if(temp->Permission == 2)
{
printf("-w-\n");
}
else if(temp->Permission == 3)
{
printf("rw-\n");
}
printf("----------------------------\n");
return EXECUTE_SUCCESS;
}