-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfs_api.c
516 lines (399 loc) · 14.2 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#include "sfs_api.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "disk_emu.h"
#include "disk_emu.c"
#define MAX_FIZE_NUM 256;
#define MAX_FILE_NAME 16;
#define SUPERBLOCK 0;
#define BLOCKSIZE 2048;
#define SYSTEMNAME "OS.sfs";
DirectoryEntry rootDirectory[500];
FileDescriptorTable fdtable[500];
typedef struct {
int magic;
int blockSize;
int fileSystemSize;
int iNodeTable;
int rootDirectory;
} superBlock;
typedef struct {
int size;
int mode;
int linkCount;
int uid;
int gid;
int pointer1;
int pointer2;
int pointer3;
int pointer4;
int pointer5;
int pointer6;
int pointer7;
int pointer8;
int pointer9;
int pointer10;
int pointer11;
int pointer12;
int indPointer;
} iNode;
typedef struct {
int i_node;
char file_name[MAX_FILE_NAME];
} DirectoryEntry;
typedef struct {
int inodeNumber;
iNode* inode;
int rwpointer;
} FileDescriptorTable;
void mksfs(int) {
if(fresh == 1) { // create file system
for (int i = 0; i < 500; i++) { // populate FDTable
fdtable[i].inodeNumber = -1;
fdtable[i].rwpointer = -1;
}
for (int i = 0; i < 500; i++) {
iNodeTable[i].mode = -1;
iNodeTable[i].linkCount = -1;
iNodeTable[i].uid = -1;
iNodeTable[i].gid = -1;
iNodeTable[i].size = -1;
iNodeTable[i].pointer1 = -1;
iNodeTable[i].pointer2 = -1;
iNodeTable[i].pointer3 = -1;
iNodeTable[i].pointer4 = -1;
iNodeTable[i].pointer5 = -1;
iNodeTable[i].pointer6 = -1;
iNodeTable[i].pointer7 = -1;
iNodeTable[i].pointer8 = -1;
iNodeTable[i].pointer9 = -1;
iNodeTable[i].pointer10 = -1;
iNodeTable[i].pointer11 = -1;
iNodeTable[i].pointer12 = -1;
iNodeTable[i].indPointer = -1;
}
for (int i = 0; i < 500; i++) { // populate root directory
rootDirectory[i].iNode = -1;
}
init_fresh_disk(SYSTEMNAME, BLOCKSIZE, 2048); // creating new file system
int *superBuffer = malloc(BLOCKSIZE);
write_blocks(SUPERBLOCK, 1, superBuffer);
free(superBuffer);
superBlock.magic = 0;
superBlock.blockSize = BLOCK_SIZE;
superBlock.fileSystemSize = 2048;
superBlock.iNodeTable = 500;
superBlock.rootDirectory = *rootDirectory;
}
else {
int error = init_disk(SYSTEMNAME, BLOCKSIZE, 2048);
if (error != 0) {
fprintf(stderr, "Error loading file system");
}
}
}
int sfs_fseek(int fileID, int loc) {
if (fileID < 0 || loc < 0) {
fprintf(stderr, "Bad seek argument");
return -1;
}
fdtable[fileID].rwpointer = loc;
return 0;
}
int sfs_getnextfilename(char* fname) {
int next = 0;
for (int i = 0; i < 500; i++) {
if(rootDirectory[positionInDir].name == fname) {
next = i + 1;
break;
}
}
return next;
}
int sfs_fclose(int fileID) {
if (fdt[fileID].inodeNumber == -1) { // file already not being read, cannot close an already closed file
return -1;
} else { // else set read/write pointer to invalid numbers
fdt[fileID].inodeNumber = -1;
fdt[fileID].rwpointer = -1;
fdtable[fileID] = NULL;
}
return 0;
}
int sfs_fopen(char* name) {
if(name == NULL){ // throw error on invalid argument
return -1;
}
}
int sfs_remove(char *file) {
int inode = -1;
int i;
int directory_table_index;
for(i = 0; i < sizeof(rootDirectory)/sizeof(rootDirectory[0]); i++) {
if(rootDirectory[i].used == 1) {
if (strcmp(rootDirectory[i].name, file) == 0) {
inode = rootDirectory[i].inode;
directory_table_index = i;
}
}
}
if(inode == -1) {
printf("File not found!\n");
return -1;
}
// free bitmap
inode_t* n = &iNodeTable[inode];
int j = 0;
while(n->data_ptrs[j] != -1 && j< 12){
rm_index(n->data_ptrs[j]);
j++;
}
j = 0;
if(n->indirect_ptrs != -1){
indirect_t* indirect_pointer = malloc(sizeof(indirect_t));
read_blocks(n->indirect_ptrs, 1, (void*) indirect_pointer);
while(indirect_pointer->data_ptr[j] != -1 && j< NUM_INDIRECT){
rm_index(indirect_pointer->data_ptr[j]);
j++;
}
}
// remove from directory_table
rootDirectory[directory_table_index].used = 0;
// remove from inode_table
iNodeTable[inode].used = 0;
// update disk
uint8_t* free_bit_map = get_bitmap();
write_blocks(NUM_BLOCKS - 1, 1, (void*) free_bit_map);
write_blocks(1, sb.inode_table_len, (void*) inode_table);
write_blocks(sb.inode_table_len + 1, NUM_DIR_BLOCKS, (void*) directory_table);
return 0;
}
int sfs_fread(int fileID, char *buf, int length) {
char *temp_buf = NULL;
int len_temp_buf = 0;
if (fdt[fileID].used == 0) {
return 0;
}
file_descriptor *f = &fdt[fileID];
inode_t *n = &inode_table[f->inode];
if (f->rwptr+length > n->size){
length = n->size - f->rwptr;
}
int num_block_read = ((f->rwptr % BLOCK_SZ) + length) / BLOCK_SZ + 1;
char buffer[num_block_read][BLOCK_SZ];
uint64_t data_ptr_index = f->rwptr / BLOCK_SZ;
int i;
for (i = 0; i < num_block_read; i++) {
if (data_ptr_index < 12) {
read_blocks(n->data_ptrs[data_ptr_index], 1, buffer[i]);
}
else {
if (n->indirect_ptrs != -1) {
indirect_t* indirect_pointer = malloc(sizeof(indirect_t));
read_blocks(n->indirect_ptrs, 1, (void*) indirect_pointer);
read_blocks(indirect_pointer->data_ptr[data_ptr_index - 12], 1, buffer[i]);
} else {
printf("ERROR While reading the file\n");
}
}
data_ptr_index++;
}
int start_ptr = f->rwptr % BLOCK_SZ;
int end_ptr = (start_ptr + length) % BLOCK_SZ;
int j;
if (num_block_read == 1) {
for (j = start_ptr; j < end_ptr; j++) {
temp_buf = appendCharToCharArray(temp_buf, buffer[0][j], len_temp_buf);
len_temp_buf++;
}
} else if (num_block_read == 2) {
for (j = start_ptr; j < BLOCK_SZ; j++) {
temp_buf = appendCharToCharArray(temp_buf, buffer[0][j], len_temp_buf);
len_temp_buf++;
}
for (j = 0; j < end_ptr; j++) {
temp_buf = appendCharToCharArray(temp_buf, buffer[1][j], len_temp_buf);
len_temp_buf++;
}
} else {
for (j = start_ptr; j < BLOCK_SZ; j++) {
temp_buf = appendCharToCharArray(temp_buf, buffer[0][j], len_temp_buf);
len_temp_buf++;
}
for (j = 1; j < num_block_read - 1; j++) {
int k;
for (k = 0; k < BLOCK_SZ; k++) {
temp_buf = appendCharToCharArray(temp_buf, buffer[j][k], len_temp_buf);
len_temp_buf++;
}
}
for (j = 0; j < end_ptr; j++) {
temp_buf = appendCharToCharArray(temp_buf, buffer[num_block_read - 1][j], len_temp_buf);
len_temp_buf++;
}
}
f->rwptr += length;
for(i = 0; i < length; i++) {
buf[i] = temp_buf[i];
}
return len_temp_buf;
}
int sfs_fwrite(int fileID, const char *buf, int length){
int count = 0;
int length_left = length;
file_descriptor* f = &fdt[fileID];
inode_t* n = &inode_table[f->inode];
int ptr_index = 0;
while (n->data_ptrs[ptr_index] != -1 && ptr_index < 12) {
ptr_index++;
}
int ind_ptr_index = 0;
if (ptr_index == 12 && n->indirect_ptrs != -1) {
indirect_t *indirect_pointer = malloc(sizeof(indirect_t));
read_blocks(n->indirect_ptrs, 1, (void *) indirect_pointer);
while (indirect_pointer->data_ptr[ind_ptr_index] != -1) {
ind_ptr_index++;
if(ind_ptr_index == NUM_INDIRECT){
return 0;
}
}
} else if (ptr_index == 12) {
indirect_t *indirect_pointer = malloc(sizeof(indirect_t));
for (ind_ptr_index = 0; ind_ptr_index < NUM_INDIRECT; ind_ptr_index++) {
indirect_pointer->data_ptr[ind_ptr_index] = -1;
}
n->indirect_ptrs = get_index();
write_blocks(n->indirect_ptrs, 1, (void *) indirect_pointer);
ind_ptr_index = 0;
}
if(f->rwptr%BLOCK_SZ != 0) {
char *read_buf = malloc(sizeof(char)*BLOCK_SZ);
uint64_t init_rwptr = f->rwptr;
sfs_fseek(fileID, (f->rwptr - (f->rwptr % BLOCK_SZ)));
sfs_fread(fileID, read_buf, (init_rwptr % BLOCK_SZ));
int len_read_buf = (init_rwptr % BLOCK_SZ);
int i;
if (length_left > BLOCK_SZ - (f->rwptr % BLOCK_SZ)) {
for (i = 0; i < BLOCK_SZ - (f->rwptr % BLOCK_SZ); i++) {
read_buf = appendCharToCharArray(read_buf, buf[i], len_read_buf);
len_read_buf++;
count++;
}
} else {
for (i = 0; i < length_left; i++) {
read_buf = appendCharToCharArray(read_buf, buf[i], len_read_buf);
len_read_buf++;
count++;
}
}
if (ptr_index < 12) {
write_blocks(n->data_ptrs[ptr_index-1], 1, (void *) read_buf);
}
else if(ind_ptr_index == 0){
write_blocks(n->data_ptrs[ptr_index-1], 1, (void *) read_buf);
}
else {
if(n->indirect_ptrs != -1) {
indirect_t *indirect_pointer = malloc(sizeof(indirect_t));
read_blocks(n->indirect_ptrs, 1, (void *) indirect_pointer);
write_blocks(indirect_pointer->data_ptr[ind_ptr_index-1], 1, (void *) read_buf);
}
}
length_left -= BLOCK_SZ - (f->rwptr % BLOCK_SZ);
}
int k;
char *write_buf;
int len_write_buf;
int num_block_left;
if(length_left > 0) {
num_block_left = (length_left - 1) / BLOCK_SZ + 1;
} else if (length_left == 1) {
num_block_left = 1;
} else {
num_block_left = 0;
}
for(k = 0; k < num_block_left; k++) {
write_buf = "";
len_write_buf = 0;
if(length_left > BLOCK_SZ) {
int w;
for(w = length - length_left; w < length - length_left + BLOCK_SZ; w++){
write_buf = appendCharToCharArray(write_buf, buf[w], len_write_buf);
len_write_buf++;
count++;
}
if(ptr_index < 12) {
n->data_ptrs[ptr_index] = get_index();
write_blocks(n->data_ptrs[ptr_index], 1, (void*) write_buf);
ptr_index++;
} else {
if(n->indirect_ptrs != -1) {
indirect_t* indirect_pointer = malloc(sizeof(indirect_t));
read_blocks(n->indirect_ptrs, 1, (void*) indirect_pointer);
indirect_pointer->data_ptr[ind_ptr_index] = get_index();
write_blocks(n->indirect_ptrs, 1, (void*) indirect_pointer);
write_blocks(indirect_pointer->data_ptr[ind_ptr_index], 1, (void*) write_buf);
ind_ptr_index++;
} else {
indirect_t* indirect_pointer = malloc(sizeof(indirect_t));
for(ind_ptr_index = 0; ind_ptr_index < NUM_INDIRECT; ind_ptr_index++){
indirect_pointer->data_ptr[ind_ptr_index] = -1;
}
n->indirect_ptrs = get_index();
write_blocks(n->indirect_ptrs, 1, (void*) indirect_pointer);
ind_ptr_index = 0;
indirect_pointer->data_ptr[ind_ptr_index] = get_index();
write_blocks(n->indirect_ptrs, 1, (void*) indirect_pointer);
write_blocks(indirect_pointer->data_ptr[ind_ptr_index], 1, (void*) write_buf);
ind_ptr_index++;
}
}
length_left -= BLOCK_SZ;
}
else {
int w;
for(w = length - length_left; w < length; w++){
write_buf = appendCharToCharArray(write_buf, buf[w], len_write_buf);
len_write_buf++;
count++;
}
if(ptr_index < 12) {
n->data_ptrs[ptr_index] = get_index();
write_blocks(n->data_ptrs[ptr_index], 1, (void*) write_buf);
ptr_index++;
} else {
if(n->indirect_ptrs != -1) {
indirect_t* indirect_pointer = malloc(sizeof(indirect_t));
read_blocks(n->indirect_ptrs, 1, (void*) indirect_pointer);
indirect_pointer->data_ptr[ind_ptr_index] = get_index();
write_blocks(n->indirect_ptrs, 1, (void*) indirect_pointer);
write_blocks(indirect_pointer->data_ptr[ind_ptr_index], 1, (void*) write_buf);
ind_ptr_index++;
}
else{
indirect_t* indirect_pointer = malloc(sizeof(indirect_t));
for(ind_ptr_index = 0; ind_ptr_index < NUM_INDIRECT; ind_ptr_index++){
indirect_pointer->data_ptr[ind_ptr_index] = -1;
}
n->indirect_ptrs = get_index();
write_blocks(n->indirect_ptrs, 1, (void*) indirect_pointer);
ind_ptr_index = 0;
indirect_pointer->data_ptr[ind_ptr_index] = get_index();
write_blocks(n->indirect_ptrs, 1, (void*) indirect_pointer);
write_blocks(indirect_pointer->data_ptr[ind_ptr_index], 1, (void*) write_buf);
ind_ptr_index++;
}
}
}
}
f->rwptr += length;
// update bitmap
uint8_t* free_bit_map = get_bitmap();
write_blocks(NUM_BLOCKS - 1, 1, (void*) free_bit_map);
// update inode
n->size += length;
write_blocks(1, (int) sb.inode_table_len, (void*) inode_table);
return count;
}