-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
290 lines (264 loc) · 6.46 KB
/
Copy pathmain.cpp
File metadata and controls
290 lines (264 loc) · 6.46 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
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<iostream>
#include<string.h>
#include "cvfs.h"
void printError(int iErrCode)
{
if(iErrCode == 1)
{
printf("Error: Bad command");
}
else if(iErrCode == -1)
{
printf("Error: Incorrect Parameters");
}
}
int main()
{
char szStr[80];
char buffer[MAX_FILESIZE];
char *ptr = NULL;
char szCommand[4][80];
int iCount = 0, iRet = 0, iFD = 0;
FileSystem *objFS = new FileSystem();
while(1)
{
fflush(stdin);
strcpy(szStr,"");
printf("\nVFS : >");
fgets(szStr, 80, stdin);
iCount = sscanf(szStr,"%s %s %s %s",szCommand[0], szCommand[1], szCommand[2], szCommand[3]);
switch(iCount)
{
case 1: if(strcmp(szCommand[0], "ls") == 0)
{
objFS->ls_files();
}
else if(strcmp(szCommand[0], "exit") == 0)
{
printf("\nThank you for using CVFS\n");
exit(0);
}
else if(strcmp(szCommand[0], "clear") == 0)
{
system("clear");
}
else if(strcmp(szCommand[0], "help") == 0)
{
objFS->DisplayHelp();
}
else if(strcmp(szCommand[0], "closeall") == 0)
{
objFS->CloseAllFiles();
}
else
{
printError(1);
}
break;
case 2: if(strcmp(szCommand[0], "rm") == 0)
{
iRet = objFS->rm_file(szCommand[1]);
if(iRet == 0)
{
printf("\nSUCCESS : File %s deleted successfully\n",szCommand[1]);
}
else if(iRet == -1)
{
printf("\nERROR: Invalid parameters to rm\n Use man rm for usage information");
}
else if(iRet == -2)
{
printf("\nERROR: File %s not found", szCommand[1]);
}
}
else if(strcmp(szCommand[0], "man") == 0)
{
objFS->man(szCommand[1]);
}
else if(strcmp(szCommand[0], "write") == 0)
{
iFD = objFS->GetFDByName(szCommand[1]);
if(iFD == -1)
{
printf("\nError : Please open the file first\n");
}
else
{
printf("\nEnter data: ");
fgets(buffer, MAX_FILESIZE, stdin); // scanf(" %[^\n]",buffer);
iRet = strlen(buffer);
if(iRet == 0)
{
printf("\nError : No data provided to write\n");
}
else
{
iRet = objFS->WriteFile(iFD, buffer, iRet);
if(iRet == -1)
{
printf("\nError : Permission Denied\n");
}
}
}
}
else if(strcmp(szCommand[0], "close") == 0)
{
int iRet = objFS->CloseFile(szCommand[1]);
if(iRet == -1)
{
printf("\nERROR : File %s not found or not open", szCommand[1]);
}
else
{
printf("\nSUCCESS : File %s closed", szCommand[1]);
}
}
else if(strcmp(szCommand[0], "stat") == 0)
{
iRet = objFS->stat_file(szCommand[1]);
if(iRet == -1)
{
printf("\nERROR : Incorrect Parameters\n");
}
else if(iRet == -2)
{
printf("\nERROR : File %s not found\n",szCommand[1]);
}
}
else
{
printError(1);
}
break;
case 3:
if(strcmp(szCommand[0], "open") == 0)
{
// Syntax of open is open(fname, mode) so we passed fname and mode to our function
iRet = objFS->OpenFile(szCommand[1], atoi(szCommand[2]));
if(iRet >= 0)
{
printf("\nSUCCESS : File %s opened successfully with file descriptor: %d\n", szCommand[1], iRet);
}
else if(iRet == -1)
{
printf("\nERROR : Invalid parameter values\n");
}
else if(iRet == -2)
{
printf("\nERROR : File %s doesn't exists\n", szCommand[1]);
}
else if(iRet == -3)
{
printf("\nERROR : Permission Denied\n");
}
else if(iRet == -4)
{
printf("\nERROR : No file descriptor available\n");
}
else if(iRet == -5)
{
printf("\nERROR : Memory allocation failure\n");
}
else
{
printf("\nSome error occured\n");
}
}
else if(strcmp(szCommand[0], "create") == 0)
{
// Syntax of create is create(fname, mode) so we passed fname and mode to our function
iRet = objFS->CreateFile(szCommand[1], atoi(szCommand[2]));
if(iRet >= 0)
{
printf("\nSUCCESS :File %s created successfully with file descriptor: %d\n", szCommand[1], iRet);
}
else if(iRet == -1)
{
printf("\nERROR : Invalid parameter values\n");
}
else if(iRet == -2)
{
printf("\nERROR : There is no inodes\n");
}
else if(iRet == -3)
{
printf("\nERROR : File already exists\n");
}
else if(iRet == -4)
{
printf("\nERROR : No file descriptor available\n");
}
else if(iRet == -5)
{
printf("\nERROR : Memory allocation failure\n");
}
}
else if(strcmp(szCommand[0], "read") == 0)
{
iFD = objFS->GetFDByName(szCommand[1]);
if(iFD == -1)
{
printf("\nERROR : File does not exists\n");
}
else
{
ptr = (char *)malloc(sizeof(char) * atoi(szCommand[2])+1);
if(ptr == NULL)
{
printf("\nError : Memory allocation failure\n");
break;
}
// Syntax of read is read(fd, buff, size) so we passed fd, buff and size to our function
iRet = objFS->ReadFile(iFD, ptr, atoi(szCommand[2]));
if(iRet > 0)
{
// Write read data on stdout (monitor)
write(2,ptr,iRet);
}
else if(iRet == -1){
printf("\nERROR : File does not exists\n");
}
else if(iRet == -2){
printf("\nERROR : Permission denied\n");
}
else if(iRet == -3){
printf("\nERROR : Reached at end of file\n");
}
else if(iRet == -4){
printf("\nERROR : It is not regular file\n");
}
else if(iRet == 0){
printf("\nERROR : File empty\n");
}
}
}
break;
case 4:
if(strcmp(szCommand[0],"lseek") == 0)
{
iFD = objFS->GetFDByName(szCommand[1]);
if(iFD == -1)
{
printf("Error : Incorrect parameter\n");
continue;
}
iRet = objFS->LseekFile(iFD, atoi(szCommand[2]),atoi(szCommand[3]));
if(iRet == -1)
{
printf("ERROR : Unable to perform lseek\n");
}
}
else
{
printf("\nERROR : Command not found !!!\n”); continue");
}
break;
default: printf("Error: Bad command default");
break;
}
}
return 0;
}