-
Notifications
You must be signed in to change notification settings - Fork 3
/
LibFS.c
353 lines (284 loc) · 7.5 KB
/
LibFS.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
#include "LibFS.h"
#include "LibDisk.h"
#include "builder.h"
#include "parameters.h"
#include "directory.h"
#include "inode.h"
#include "file.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
// global errno value here
int osErrno;
// a temp variable for reading buffers
char* read_buffer;
char diskPath[256];
int FS_Boot(char *path)
{
printf("FS_Boot %s\n", path);
// oops, check for errors
if (Disk_Init() == -1)
{
printf("Disk_Init() failed\n");
osErrno = E_GENERAL;
return -1;
}
// do all of the other stuff needed...
// check for path and read the existing file
strcpy(diskPath, path);
if (Disk_Load(path)==-1)
{
if (diskErrno==E_OPENING_FILE)
{
printf("This filename does not exist\n");
}
else if(diskErrno==E_READING_FILE)
{
printf("There is some errors with reading from disk image\n");
}
// do all of the other stuff needed...
if (BuildMetadataBlocks() == -1)
{
osErrno = E_GENERAL;
}
else if(CreateFileTable() == -1)
{
osErrno = E_GENERAL;
return -1;
}
}
else
{
// check for magic number of block
// handling errors
if (CheckFileSystemSuperBlock() == -1)
{
osErrno = E_GENERAL;
return -1;
}
else if (CreateFileTable() == -1)
{
osErrno = E_GENERAL;
return -1;
}
}
return 0;
}
int FS_Sync()
{
printf("FS_Sync %s\n", diskPath);
Disk_Save(diskPath);
return 0;
}
int File_Create(char *file)
{
printf("FS_Create\n");
// allocate memory for storing string...
char* array[128];
char myPath[256];
// make a copy of path to modify
strcpy(myPath, file);
// tokenize path and make array of path elements...
int i = BreakPathName(myPath, array);
int parent;
int current;
if(findLeafInodeNumber(myPath, array, i, &parent, ¤t, 1) != 0)
{
if(findLeafInodeNumber(myPath, array, i, &parent, ¤t, 0) == 0)
{
osErrno = E_GENERAL;
return -1;
}
osErrno = E_CREATE;
return -1;
}
if(addFile(parent, array[i-1]) != 0)
{
osErrno = E_CREATE;
return -1;
}
return 0;
}
int File_Open(char *file)
{
printf("FS_Open : %s\n", file);
int fd = openFileDescriptor(file);
if(fd != 0)
return -1;
printFileTable();
return fd;
}
int File_Read(int fd, void *buffer, int size)
{
printf("FS_Read\n");
// Check file is open or not
if ( isFileOpen(fd)==-1)
{
osErrno=E_BAD_FD;
printf("File is not open\n");
return -1;
}
int sizeRead = FileRead( fd, buffer, size);
if(sizeRead==-1)
{
printf("Error happen in Reading\n");
return -1;
}
return sizeRead;
}
int File_Write(int fd, void *buffer, int size)
{
printf("FS_Write\n");
// Check file is open or not
if ( isFileOpen(fd)==-1)
{
osErrno=E_BAD_FD;
printf("File is not open\n");
return -1;
}
int newSizeOfFile=FileWrite( fd, buffer, size);
if(newSizeOfFile==-1)
{
printf("Error happen while Writing\n");
return -1;
}
return newSizeOfFile;
}
int File_Seek(int fd, int offset)
{
printf("FS_Seek\n");
if(offset > SizeOfFile(getInodePointerOfFileEntry(fd)))
{
osErrno= E_SEEK_OUT_OF_BOUNDS;
return -1;
}
if(isFileOpen(fd))
{
osErrno = E_BAD_FD;
return -1;
}
updateFilePointerOfFileEntry(fd, offset);
return 0;
}
int File_Close(int fd)
{
printf("FS_Close\n");
if(isFileOpen(fd))
{
osErrno = E_BAD_FD;
return -1;
}
if(removeFileTableEntry(fd) != 0)
return -1;
return 0;
}
int File_Unlink(char *file)
{
printf("FS_Unlink\n");
// allocate memory for storing string...
char* array[128];
char myPath[256];
// make a copy of path to modify
strcpy(myPath, file);
// tokenize path and make array of path elements...
int i = BreakPathName(myPath, array);
int parent;
int current;
if(findLeafInodeNumber(myPath, array, i, &parent, ¤t, 0) != 0)
return -1;
printf("DeleteEntryFromDirectory( %d, %d ) ", parent, current);
DeleteEntryFromDirectory(parent, current);
int DataBlocksOccupied[30];
int j = DataBlocksOccupiedByFile(current, DataBlocksOccupied);
for (i = 0; i < j; i++)
{
ChangeDataBitmapStatus(DataBlocksOccupied[i], AVAILIBLE);
}
ChangeInodeBitmapStatus(current, AVAILIBLE);
return 0;
}
// directory ops
int Dir_Create(char *path)
{
printf("Dir_Create %s\n", path);
// allocate memory for storing string...
char* array[128];
char myPath[256];
// make a copy of path to modify
strcpy(myPath, path);
// tokenize path and make array of path elements...
int i = BreakPathName(myPath, array);
if(addDirectory(myPath, array, i) != 0)
{
osErrno = E_CREATE;
return -1;
}
return 0;
}
int Dir_Size(char *path)
{
printf("Dir_Size\n");
// allocate memory for storing string...
char* array[128];
char myPath[256];
// make a copy of path to modify
strcpy(myPath, path);
// tokenize path and make array of path elements...
int i = BreakPathName(myPath, array);
int parent;
int current;
if(findLeafInodeNumber(myPath, array, i, &parent, ¤t, 0) != 0)
return -1;
int size = DirSizeFromInode(current);
printf("Size : %d\n", size);
return size;
}
int Dir_Read(char *path, void *buffer, int size)
{
buffer = calloc(sizeof(char), size);
printf("Dir_Read ( %s, %d)\n", path, size);
// allocate memory for storing string...
char* array[128];
char myPath[256];
if(size < Dir_Size(path))
{
osErrno = E_BUFFER_TOO_SMALL;
return -1;
}
// make a copy of path to modify
strcpy(myPath, path);
// tokenize path and make array of path elements...
int i = BreakPathName(myPath, array);
int parent;
int current;
if(findLeafInodeNumber(myPath, array, i, &parent, ¤t, 0) != 0)
return -1;
DirReadFromInode(current, buffer, size);
printBlockHex(buffer, size);
return 0;
}
int Dir_Unlink(char *path)
{
printf("Dir_Unlink\n");
// allocate memory for storing string...
char* array[128];
char myPath[256];
// make a copy of path to modify
strcpy(myPath, path);
// tokenize path and make array of path elements...
int i = BreakPathName(myPath, array);
int parent;
int current;
if(findLeafInodeNumber(myPath, array, i, &parent, ¤t, 0) != 0)
return -1;
printf("DeleteEntryFromDirectory( %d, %d ) ", parent, current);
DeleteEntryFromDirectory(parent, current);
int DataBlocksOccupied[30];
int j = DataBlocksOccupiedByDirectory(current, DataBlocksOccupied);
for (i = 0; i < j; i++)
{
ChangeDataBitmapStatus(DataBlocksOccupied[i], AVAILIBLE);
}
ChangeInodeBitmapStatus(current, AVAILIBLE);
return 0;
}