-
Notifications
You must be signed in to change notification settings - Fork 0
/
CVFS.cpp
278 lines (268 loc) · 9.18 KB
/
CVFS.cpp
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<iostream>
#include<io.h>
#include "CVFS.h"
int main()
{
char *ptr = NULL;
int ret = 0,fd = 0,count = 0;
char command[4][80],str[80],arr[1024];
InitialiseSuperBlock();
CreateDILB();
while (1)
{
fflush(stdin);
strcpy(str,"");
printf("\nMarvellous Customized Virtual File System(CVFS):> ");
fgets(str,80,stdin);//scanf("%[^\n]s,str");
count = sscanf(str," %s %s %s",command[0],command[1],command[2],command[3]);
if (count == 1)
{
if (strcmp(command[0],"ls") == 0)
{
ls_file();
}
else if (strcmp(command[0],"closeall") == 0)
{
CloseAllFile();
printf("All files closed sucessfully\n");
continue;
}
else if (strcmp(command[0],"clear") == 0)
{
system("cls");
continue;
}
else if (strcmp(command[0],"help") == 0)
{
DisplayHelp();
continue;
}
else if (strcmp(command[0],"exit") == 0)
{
printf("Terminating the Virtual File System\n");
break;
}
else
{
printf("\nError: Command not found !!!\n");
continue;
}
}
else if (count == 2)
{
if (strcmp(command[0],"stat") == 0)
{
ret = stat_file(command[1]);
if (ret == -1)
{
printf("ERROR: Incorrect parameters\n");
}
if (ret == -2)
{
printf("ERROR:There is no such file\n");
}
continue;
}
else if (strcmp(command[0],"fstat") == 0)
{
ret = fstat_file(atoi(command[1]));
if (ret == -1)
{
printf("ERROR: Incorrect Parameters\n");
}
if (ret == -2)
{
printf("ERROR: There is no such file\n");
}
continue;
}
else if (strcmp(command[0],"close") == 0)
{
ret = CloseFileByName(command[1]);
if (ret == -1)
{
printf("ERROR: There is no such file\n");
}
continue;
}
else if (strcmp(command[0],"rm") == 0)
{
ret = rm_File(command[1]);
if (ret == -1)
{
printf("ERROR: There is no such file\n");
}
continue;
}
else if (strcmp(command[0],"man") == 0)
{
man(command[1]);
}
else if (strcmp(command[0],"write") == 0)
{
fd = GetFDFromName(command[1]);
if (fd == -1)
{
printf("ERROR: Incorrect parameter\n");
continue;
}
printf("Enter the data: \n");
scanf("%[^\n]",arr);
ret = strlen(arr);
if (ret == 0)
{
printf("ERROR: Incorrect parameter\n");
continue;
}
ret = WriteFile(fd,arr,ret);
if (ret == -1)
{
printf("ERROR: Permission denied\n");
}
if (ret == -2)
{
printf("ERROR: There is no sufficient memory to write\n");
}
if (ret == -3)
{
printf("ERROR: It is not regular file\n");
}
}
else if (strcmp(command[0],"truncate") == 0)
{
ret = truncate_File(command[1]);
if (ret == -1)
{
printf("ERROR: Incorrect parameter\n");
}
}
else
{
printf("\nERROR: Command not found !!!\n");
continue;
}
}
else if (count == 3)
{
if (strcmp(command[0],"create") == 0)
{
ret = CreateFile(command[1],atoi(command[2]));
if (ret >= 0)
{
printf("File is sucessfully created with file descriptor: %d\n",ret);
}
if (ret == -1)
{
printf("ERROR: Incorrect parameters\n");
}
if (ret == -2)
{
printf("ERROR: There are no inodes\n");
}
if (ret == -3)
{
printf("ERROR: File already exists\n");
}
if (ret == -4)
{
printf("ERROR: Memory allocation failure\n");
}
continue;
}
else if (strcmp(command[0],"open") == 0)
{
ret = OpenFile(command[1],atoi(command[2]));
if (ret >= 0)
{
printf("File is sucessfully opened with file descriptor: %d\n",ret);
}
if (ret == -1)
{
printf("ERROR: Incorrect parameters\n");
}
if (ret == -2)
{
printf("ERROR: File not present\n");
}
if (ret == -3)
{
printf("ERROR:permission denied\n");
}
continue;
}
else if (strcmp(command[0],"read") == 0)
{
fd = GetFDFromName(command[1]);
if (fd == -1)
{
printf("ERROR: Incorrect parameter\n");
continue;
}
ptr = (char*)malloc(sizeof(atoi(command[2]))+1);
if (ptr == NULL)
{
printf("ERROR: Memory allocation failure\n");
continue;
}
ret = ReadFile(fd,ptr,atoi(command[2]));
if (ret == -1)
{
printf("ERROR: File not existing\n");
}
if (ret == -2)
{
printf("ERROR: Permission denied\n");
}
if (ret == -3)
{
printf("ERROR: Reached at end of file\n");
}
if (ret == -4)
{
printf("ERROR: It is not regular file \n");
}
if (ret == 0)
{
printf("ERROR: File empty\n");
}
if (ret>0)
{
write(2,ptr,ret);
}
continue;
}
else if (count == 4)
{
if (strcmp(command[0],"lseek") == 0)
{
fd = GetFDFromName(command[1]);
if (fd == -1)
{
printf("ERROR: Incorrect parameter\n");
continue;
}
ret = LseekFile(fd,atoi(command[2]),atoi(command[3]));
if (ret == -1)
{
printf("ERROR: Unable to perform lseek\n");
}
}
else
{
printf("\nERROR: Command not found !!!\n");
continue;
}
}
else
{
printf("\nERROR: Command not found !!!\n");
continue;
}
}
}
return 0;
}