forked from ParasharaRamesh/mySimpleFilesystem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiletable.c
86 lines (79 loc) · 1.75 KB
/
filetable.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
#include "globalConstants.h"
#include "utility.h"
#include<stdlib.h>
#include<string.h>
#include<sys/time.h>
filetable FileTable[5];
extern int currentshellpid;
filetable* getEntry(int fd,int who)
{
//traverse the filetable and search for the entry with file's inode id and the who and
for(int i=0;i<5;i++)
{
if(FileTable[i].used==1)
{
if((FileTable[i].who==who)&&(FileTable[i].filedescriptor==fd))
{
return &FileTable[i];
}
}
}
return NULL;
}
int addEntry(inode *file,int who)
{
if(getEntry(file->id,who)!=NULL)
{
//this process already has this file open!
return 1;
}
int fd=file->id;
int count=0;
for(int i=0;i<5;i++)
{
if(FileTable[i].used==0)
{
FileTable[i].used=1;
FileTable[i].filedescriptor=fd;
FileTable[i].who=who;
FileTable[i].currfilepointer=0;
return 1;
}
}
return 0;
}
int removeEntry(inode *file,int who)
{
for(int i=0;i<5;i++)
{
if( (FileTable[i].filedescriptor == file->id) && (FileTable[i].who == who) )
{
for(int k=i;k<4;k++)
{
FileTable[i].who = FileTable[i + 1].who;
FileTable[i].filedescriptor = FileTable[i + 1].filedescriptor;
FileTable[i].used = FileTable[i + 1].used;
FileTable[i].currfilepointer = FileTable[i + 1].currfilepointer;
}
FileTable[4].used = 0;
FileTable[4].who = 0;
FileTable[4].filedescriptor = 0;
FileTable[4].currfilepointer = 0;
return 1;
}
}
return 0;
}
int flushfiletable(inode *file)
{
//traverse this table and remove all the entries with the filedescriptor as this file
int fd=file->id;
for(int i=0;i<5;i++)
{
if( FileTable[i].filedescriptor == fd )
{
FileTable[i].used=0;
}
}
return 1;
}