-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfs_api.c
406 lines (310 loc) · 11.1 KB
/
sfs_api.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
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
#include "sfs_api.h"
/******* Setting things up in Memory *******/
SuperBlock SB;
char *fileName = "FileSystem";
INode JNode; //the root node
unsigned char *fileDesChars;
FileDesc *fdTable;
Block iNodesBlockBuffer[14];
RootDirectory rootDir;
RootDirectoryEntry *rootDirectoryEntries;
void mkssfs(int fresh){
if(fresh){
//making space for file descriptors
fileDesChars = calloc(1, 3584);
//casting that space into individual entries
fdTable = (FileDesc *)fileDesChars;
//setting all the free bits to zero
for(int i = 0; i < 224; i++){
fdTable[i].freeBit = 0;
fdTable[i].readPointer = 0;
}
//Initializing new file system
init_fresh_disk(fileName, _BLOCK_SIZE, _NUMBER_OF_BLOCKS);
//making space and casting for root directory entries
unsigned char *entriesBuff = calloc(5, _BLOCK_SIZE);
rootDirectoryEntries = (RootDirectoryEntry *)entriesBuff;
//setting up the root dir entries
for(int i = 0; i < 224; i++){
rootDirectoryEntries[i].iNodeNumber = -1;
}
write_blocks(1, 5, rootDirectoryEntries);
//setting up the superblock, done a bit inneficiently, change it
SB.magic[0] = 0xff;
SB.magic[1] = 0xff;
SB.magic[2] = 0xff;
SB.magic[3] = 0xff;
SB.blockSize = _BLOCK_SIZE;
SB.blocksCount = _NUMBER_OF_BLOCKS;
SB.iNodesCount = _NUMBER_OF_INODES;
SB.jNode = JNode;
Block *super = calloc(1, _BLOCK_SIZE);
memcpy(super, &SB, sizeof(SB));
Block blocksBuffer[1] = {*super};
/******* Writing to the disk *******/
write_blocks(0,1,blocksBuffer);
int counter = 20;
unsigned char *iBlocks = calloc(14, _BLOCK_SIZE);
INode *iNode = (INode *)iBlocks;
//iterating through all iNodes and setting them up
for(int j = 0; j < 224 ; j++){
iNode[j].size = -1;
for(int k = 0; k < 14; k++){
iNode[j].direct[k] = counter; //setting direct block pointers
counter++;
}
}
write_blocks(6,14,iBlocks);
free(iBlocks);
}else{
//if opening existing file system
fileDesChars = calloc(1, 3584);
fdTable = (FileDesc *)fileDesChars;
for(int i = 0; i < 224; i++){
fdTable[i].freeBit = 0;
fdTable[i].readPointer =0;
}
unsigned char *entriesBuff = calloc(5, _BLOCK_SIZE);
rootDirectoryEntries = (RootDirectoryEntry *)entriesBuff;
//setting up the root dir entries
for(int i = 0; i < 224; i++){
rootDirectoryEntries[i].iNodeNumber = -1;
}
write_blocks(1, 5, rootDirectoryEntries);
init_disk(fileName, _BLOCK_SIZE, _NUMBER_OF_BLOCKS);
}
}
int ssfs_fopen(char *name){
//making room for all inodes
unsigned char *iNodesBuff = calloc(14, _BLOCK_SIZE);
INode *iNodesBlocks = (INode *)iNodesBuff;
//setting up vars that we'll use
int iNodeNumber = -1;
int writePointer = 0;
/**** Reading the INodes *****/
read_blocks(6, 14,iNodesBlocks);
//reading the root dirs
read_blocks(1,5,rootDirectoryEntries);
//checking to see if file exists
for(int i = 0; i < 224; i++){
//if we find a file with the name
if(!strcmp(rootDirectoryEntries[i].fileName, name)){
printf("file name is : %s\n", rootDirectoryEntries[i].fileName);
iNodeNumber = rootDirectoryEntries[i].iNodeNumber;
writePointer = iNodesBlocks[iNodeNumber].size;
break;
}
//if no file with the same name and empy directory entry
}
//if we didnt find an existing entry
if(iNodeNumber == -1){
//finding the first free iNode
for(int i = 0; i < 224; i++){
if(iNodesBlocks[i].size == -1){
//setting up the first free iNode
iNodesBlocks[i].size = 0;
iNodeNumber = i;
//find a free location in root dirs
for(int j = 0; j < 224; j++){
if(rootDirectoryEntries[j].iNodeNumber == -1){
rootDirectoryEntries[j].iNodeNumber = iNodeNumber;
strcpy(rootDirectoryEntries[j].fileName, name);
break;
}
}
break;
}
}
}
//index to return
int index = -1;
for(int i = 0; i < 224; i++){
//finding the first free filedesc and setting it equal to the new file inode and fresh pointers
if (fdTable[i].freeBit == 0){
fdTable[i].iNodeNumber = iNodeNumber;
fdTable[i].readPointer = 0;
fdTable[i].writePointer = 0;
fdTable[i].freeBit = 1;
index = i;
break;
}
}
write_blocks(1,5,rootDirectoryEntries);
write_blocks(6,14,iNodesBuff);
free(iNodesBuff);
return index;
}
int ssfs_fclose(int fileID){
//negative check
if(fileID < 0){
printf("cant be negative");
return -1;
}
//closing the entry at that fileID
if(fdTable[fileID].freeBit == 1){
fdTable[fileID].iNodeNumber = -1;
fdTable[fileID].readPointer = 0;
fdTable[fileID].writePointer = 0;
fdTable[fileID].freeBit = 0;
return 0;
}
//if nothing there
if(fdTable[fileID].freeBit == 0){
return -1;
}
return -1;
}
int ssfs_frseek(int fileID, int loc){
//negative check
if(fileID <0){
printf("cant be negative");
return -1;
}
//negative location check, not sure if correct
if(loc < 0){
printf("Cannot seek negative location");
return -1;
}
//making room for all inodes
unsigned char *iNodesBuff = calloc(14, _BLOCK_SIZE);
INode *iNodesBlocks = (INode *)iNodesBuff;
/**** Reading the INodes *****/
read_blocks(6, 14,iNodesBlocks);
if(fdTable[fileID].freeBit == 0){
printf("No open file with that ID");
return -1;
}
//checking whether we're going past the size
if((fdTable[fileID].readPointer + loc) > iNodesBlocks[fdTable[fileID].iNodeNumber].size){
printf("Error, trying to read past end of the file");
return -1;
}
fdTable[fileID].readPointer = loc;
free(iNodesBuff);
return 0;
}
int ssfs_fwseek(int fileID, int loc){
//negative check
if(fileID <0){
printf("cant be negative");
return -1;
}
//making room for all inodes
unsigned char *iNodesBuff = calloc(14, _BLOCK_SIZE);
INode *iNodesBlocks = (INode *)iNodesBuff;
/**** Reading the INodes *****/
read_blocks(6, 14,iNodesBlocks);
if(fdTable[fileID].freeBit == 0){
printf("No open file with that ID");
return -1;
}
//checking whether we go past the size
if((fdTable[fileID].writePointer + loc) > iNodesBlocks[fdTable[fileID].iNodeNumber].size){
printf("Error, trying to read past end of the file");
return -1;
}
fdTable[fileID].writePointer = loc;
free(iNodesBuff);
return 0;
}
int ssfs_fwrite(int fileID, char *buf, int length){
//negative check
if(fileID <0){
printf("cant be negative");
return -1;
}
//making room for all inodes
unsigned char *iNodesBuff = calloc(14, _BLOCK_SIZE);
INode *iNodesBlocks = (INode *)iNodesBuff;
/**** Reading the INodes *****/
read_blocks(6, 14,iNodesBlocks);
printf("the iNode Number is : %d \n", fdTable[fileID].iNodeNumber);
unsigned char *blocksBuff = calloc(14, _BLOCK_SIZE);
//making sure it is open/ it is in the filedesc table
if(fdTable[fileID].freeBit == 0){
printf("No open file with that ID");
return -1;
}
int startingBlock = iNodesBlocks[fdTable[fileID].iNodeNumber].direct[0];
//todo check if read pointer + length is less than buff (idk how to check sizeof of buff cause that function is weird)
//reading one by one into blockBuff
read_blocks(startingBlock, 14, blocksBuff);
//if the buffer is less than the length, write up until the end of the buffer
if(((int)strlen(buf))<length){
length = (int)strlen(buf);
}
for(int i = 0; i < length; i++){
blocksBuff[i + fdTable[fileID].writePointer] = buf[i];
}
//simply to identify the hex entry in the file system
for(int i = 0; i<2; i++){
printf("%02x \n",blocksBuff[i]);
}
//updtating the iNode size, need to take into account if buf is smaller than length
iNodesBlocks[fdTable[fileID].iNodeNumber].size = iNodesBlocks[fdTable[fileID].iNodeNumber].size + length;
write_blocks(6, 14, iNodesBlocks);
printf("it starts at : %d \n", startingBlock);
write_blocks(startingBlock, 14, blocksBuff);
free(iNodesBuff);
free(blocksBuff);
return length;
}
int ssfs_fread(int fileID, char *buf, int length){
if(fileID <0){
printf("cant be negative");
return -1;
}
//making room for all inodes
unsigned char *iNodesBuff = calloc(14, _BLOCK_SIZE);
INode *iNodesBlocks = (INode *)iNodesBuff;
/**** Reading the INodes *****/
read_blocks(6, 14,iNodesBlocks);
if(fdTable[fileID].freeBit == 0){
printf("No open file with that ID");
return -1;
}
INode fileInode = iNodesBlocks[fdTable[fileID].iNodeNumber];
unsigned char *blocksBuff = calloc(14, _BLOCK_SIZE);
//making sure it is open/ it is in the filedesc table
//todo check if read pointer + length is less than buff (idk how to check sizeof of buff cause that function is weird)
//reading one by one into blockBuff
read_blocks(fileInode.direct[0], 14,blocksBuff);
for(int i = 0; i < length; i++){
buf[i] = blocksBuff[i + fdTable[fileID].readPointer];
}
printf("%s \n", buf);
free(iNodesBuff);
free(blocksBuff);
return length;
}
int ssfs_remove(char *file){
//making room for all inodes
unsigned char *iNodesBuff = calloc(14, _BLOCK_SIZE);
INode *iNodesBlocks = (INode *)iNodesBuff;
/**** Reading the INodes *****/
read_blocks(6, 14,iNodesBlocks);
int iNodeNumber = -1;
read_blocks(1,5,rootDirectoryEntries);
//checking to see if file exists
for(int i = 0; i < 224; i++){
//if we find a file with the name
if(!strcmp(rootDirectoryEntries[i].fileName, file)){
iNodeNumber = rootDirectoryEntries[i].iNodeNumber;
ssfs_fclose(rootDirectoryEntries[i].iNodeNumber);
//setting all the values of the blocks that the inode poined to, to zero
for(int i = 0; i < 14; i++){
Block *emptyBlock = calloc(1, _BLOCK_SIZE);
write_blocks(iNodesBlocks[iNodeNumber].direct[i], 1, emptyBlock);
}
//zeroing the directory entry and writing back changes
memset(rootDirectoryEntries[i].fileName, 0, 16);
rootDirectoryEntries[i].iNodeNumber = -1;
write_blocks(1, 5, rootDirectoryEntries);
return 0;
break;
}
//if no file with the same name and empy directory entry
}
free(iNodesBuff);
return -1;
}