-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfs.c
1313 lines (1110 loc) · 36.8 KB
/
sfs.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
------------------------------------------
Assignment 3
------------------------------------------
subject: Advanced Operating Systems
year: 2021 - 2022
------------------------------------------
Name: Debajyoti Dasgupta
Roll No: 18CS30051
------------------------------------------
Kernel Version: 5.11.0-37-generic
System: Ubuntu 20.04.3 LTS
------------------------------------------
File Name sfs.c
------------------------------------------
*/
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include "disk.h"
#include "sfs.h"
static disk *mounted_disk;
static super_block *sb;
int root_file_inode = 0;
int format(disk *diskptr)
{
if (diskptr == NULL)
{
printf("{SFS} -- [ERROR] Disk is not initialized -- Disk is not formatted\n");
return -1;
}
int N = diskptr->blocks;
int M = N - 1;
int I = (int)(0.1 * M);
int n_inodes = I * 128;
int IB = (int)ceil(n_inodes / (8. * BLOCKSIZE));
int R = M - I - IB;
int DBB = (int)ceil(R / (8. * BLOCKSIZE));
int DB = R - DBB;
// Initialize the super block
if (sb == NULL)
sb = (super_block *)malloc(sizeof(super_block));
sb->magic_number = MAGIC;
sb->blocks = M;
sb->inode_blocks = I;
sb->inodes = n_inodes;
sb->inode_bitmap_block_idx = 1;
sb->inode_block_idx = 1 + IB + DBB;
sb->data_block_bitmap_idx = 1 + IB;
sb->data_block_idx = 1 + IB + DBB + I;
sb->data_blocks = DB;
// Write the super block
if (write_block(diskptr, 0, sb))
{
printf("{SFS} -- [ERROR] Failed to write super block -- Disk is not formatted\n");
return -1;
}
// Initialize the inode bit map
bitset *inode_bitmap = (bitset *)malloc(IB * BLOCKSIZE);
memset(inode_bitmap, 0, IB * BLOCKSIZE);
// Write inode bit map to disk
for (int i = 0; i < IB; i++)
{
int ret = write_block(diskptr, sb->inode_bitmap_block_idx + i, inode_bitmap + i * BLOCKSIZE);
if (ret < 0)
{
printf("{SFS} -- [ERROR]: Error writing inode bitmap to disk -- Disk is not formatted\n");
return -1;
}
}
// Initialize the data block bit map
bitset *data_bitmap = (char *)malloc(DBB * BLOCKSIZE);
memset(data_bitmap, 0, DBB * BLOCKSIZE);
// Write data block bit map to disk
for (int i = 0; i < DBB; i++)
{
int ret = write_block(diskptr, sb->data_block_bitmap_idx + i, data_bitmap + i);
if (ret < 0)
{
printf("{SFS} -- [ERROR]: Error writing data bitmap to disk -- Disk is not formatted\n");
return -1;
}
}
// Initialize the inodes
inode *inodes = (inode *)malloc(128 * sizeof(inode));
for (int i = 0; i < 128; i++)
{
inodes[i].valid = 0;
}
// Write the inodes
for (int i = 0; i < sb->inode_blocks; i++)
{
write_block(diskptr, sb->inode_block_idx + i, inodes);
}
// Free the memory
free(inodes);
free(inode_bitmap);
free(data_bitmap);
return 0;
}
int mount(disk *diskptr)
{
if (diskptr == NULL)
{
printf("{SFS} -- [ERROR] Disk is not initialized -- Disk is not mounted\n");
return -1;
}
// Read the super block
if (sb == NULL)
sb = (super_block *)malloc(sizeof(super_block));
if (read_block(diskptr, 0, sb) < 0)
{
printf("{SFS} -- [ERROR] Failed to read super block -- Disk is not mounted\n");
return -1;
}
// Check the magic number
if (sb->magic_number != MAGIC)
{
printf("{SFS} -- [ERROR] Magic number is incorrect -- Disk is not mounted\n");
return -1;
}
mounted_disk = diskptr;
// Create the root directory
root_file_inode = create_file();
return 0;
}
int create_file()
{
if (mounted_disk == NULL)
{
printf("{SFS} -- [ERROR] Disk is not mounted -- File not created\n");
return -1;
}
// Find the first free inode
int inode_idx = find_free_inode();
if (inode_idx < 0)
{
printf("{SFS} -- [ERROR] No free inodes -- File not created\n");
return -1;
}
// Initialize the inode
int inode_block_idx = sb->inode_block_idx + (inode_idx / 128);
int inode_offset = (inode_idx % 128);
inode *inode_ptr = (inode *)malloc(128 * sizeof(inode));
read_block(mounted_disk, inode_block_idx, inode_ptr);
inode_ptr[inode_offset].valid = 1;
inode_ptr[inode_offset].size = 0;
// Write the inode to disk
if (write_block(mounted_disk, inode_block_idx, inode_ptr))
{
printf("{SFS} -- [ERROR] Failed to write inode to disk -- File not created\n");
return -1;
}
return inode_idx;
}
int remove_file(int inumber)
{
if (mounted_disk == NULL)
{
printf("{SFS} -- [ERROR] Disk is not mounted -- File not removed\n");
return -1;
}
// Read the inode
int inode_block_idx = sb->inode_block_idx + (inumber / 128);
int inode_offset = (inumber % 128);
inode *inode_ptr = (inode *)malloc(128 * sizeof(inode));
read_block(mounted_disk, inode_block_idx, inode_ptr);
// Check if the inode is valid
if (inode_ptr[inode_offset].valid == 0)
{
printf("{SFS} -- [ERROR] Inode is not valid -- File not removed\n");
return -1;
}
// Clear the data bitmap
int data_block_bitmap_idx = sb->data_block_bitmap_idx;
int file_blocks = (int)ceil((double)inode_ptr[inode_offset].size / BLOCKSIZE);
uint32_t *indirect_ptr = NULL;
for (int i = 0; i < file_blocks; i++)
{
if (i < 5)
{
if (clear_bitmap(inode_ptr[inode_offset].direct[i], data_block_bitmap_idx))
{
printf("{SFS} -- [ERROR] Failed to clear data bitmap -- File not removed\n");
return -1;
}
}
else
{
if (indirect_ptr == NULL)
{
indirect_ptr = (uint32_t *)malloc(BLOCKSIZE);
if (read_block(mounted_disk, sb->data_block_idx + inode_ptr[inode_offset].indirect, indirect_ptr) < 0)
{
printf("{SFS} -- [ERROR] Failed to read indirect block -- File not removed\n");
return -1;
}
if (clear_bitmap(inode_ptr[inode_offset].indirect, data_block_bitmap_idx))
{
printf("{SFS} -- [ERROR] Failed to clear data bitmap -- File not removed\n");
return -1;
}
}
if (clear_bitmap(indirect_ptr[i - 5], data_block_bitmap_idx))
{
printf("{SFS} -- [ERROR] Failed to clear data bitmap -- File not removed\n");
return -1;
}
}
}
// Free the inode
inode_ptr[inode_offset].valid = 0;
inode_ptr[inode_offset].size = 0;
// Write the inode to disk
if (write_block(mounted_disk, inode_block_idx, inode_ptr))
{
printf("{SFS} -- [ERROR] Failed to write inode to disk -- File not removed\n");
return -1;
}
// Clere the inode bitmap
if (clear_bitmap(inumber, sb->inode_bitmap_block_idx))
{
printf("{SFS} -- [ERROR] Failed to clear inode bitmap -- File not removed\n");
return -1;
}
return 0;
}
int stat(int inumber)
{
if (mounted_disk == NULL)
{
printf("{SFS} -- [ERROR] Disk is not mounted -- Stat not printed\n");
return -1;
}
// Read the inode
int inode_block_idx = sb->inode_block_idx + (inumber / 128);
int inode_offset = (inumber % 128);
inode *inode_ptr = (inode *)malloc(128 * sizeof(inode));
read_block(mounted_disk, inode_block_idx, inode_ptr);
// Check if the inode is valid
if (inode_ptr[inode_offset].valid == 0)
{
inode_ptr[inode_offset].size = 0;
}
int blocks = (int)ceil((double)inode_ptr[inode_offset].size / BLOCKSIZE);
int direct_blocks = (blocks < 5) ? blocks : 5;
int indirect_blocks = blocks - direct_blocks;
printf("\n\
+---------------------[FILE STAT]---------------------+\n\
| |\n\
| Valid: %8d |\n\
| File Size (Logical): %8d |\n\
| Number of Data Blocks in use: %8d |\n\
| Number of Direct Pointers in use: %8d |\n\
| Number of Indirect Pointers in use: %8d |\n\
| |\n\
+-----------------------------------------------------+\n\n",
inode_ptr[inode_offset].valid,
inode_ptr[inode_offset].size,
blocks,
direct_blocks,
indirect_blocks);
return 0;
}
int read_i(int inumber, char *data, int length, int offset)
{
if (mounted_disk == NULL)
{
printf("{SFS} -- [ERROR] Disk is not mounted -- File not read\n");
return -1;
}
// Read the inode
int inode_block_idx = sb->inode_block_idx + (inumber / 128);
int inode_offset = (inumber % 128);
inode *inode_ptr = (inode *)malloc(128 * sizeof(inode));
read_block(mounted_disk, inode_block_idx, inode_ptr);
// Check if the inode is valid
if (inode_ptr[inode_offset].valid == 0)
{
printf("{SFS} -- [ERROR] Inode is not valid -- File not read\n");
return -1;
}
if (length == 0)
{
return 0;
}
// Check if the offset is valid
if (offset < 0 || offset >= inode_ptr[inode_offset].size)
{
printf("{SFS} -- [ERROR] Offset is not valid -- File not read\n");
return -1;
}
// Read the data
int start_block = offset / BLOCKSIZE;
//truncate the length if it is too long
int final_length = (length + offset > inode_ptr[inode_offset].size ? inode_ptr[inode_offset].size : length + offset);
int end_block = (final_length + BLOCKSIZE - 1) / BLOCKSIZE;
length = final_length - offset;
int bytes_read = 0;
char *temp_data = (char *)malloc(BLOCKSIZE);
int32_t *indirect_ptr = (int32_t *)malloc(BLOCKSIZE);
int file_blocks = (int)ceil((double)inode_ptr[inode_offset].size / BLOCKSIZE);
if (file_blocks > 5)
read_block(mounted_disk, sb->data_block_idx + inode_ptr[inode_offset].indirect, (char *)indirect_ptr);
int data_offset = 0;
for (int i = start_block; i < end_block; i++)
{
if (i < 5)
{
if (read_block(mounted_disk, sb->data_block_idx + inode_ptr[inode_offset].direct[i], temp_data) < 0)
{
printf("{SFS} -- [ERROR] Failed to read data block -- File not read\n");
return -1;
}
}
else
{
if (read_block(mounted_disk, sb->data_block_idx + indirect_ptr[i - 5], temp_data) < 0)
{
printf("{SFS} -- [ERROR] Failed to read data block -- File not read\n");
return -1;
}
}
int copy_length = (i == end_block - 1 ? length : BLOCKSIZE - offset % BLOCKSIZE);
memcpy(data + data_offset, temp_data + offset % BLOCKSIZE, copy_length);
data_offset += copy_length;
offset += copy_length;
bytes_read += copy_length;
length -= copy_length;
}
free(temp_data);
free(indirect_ptr);
return bytes_read;
}
int write_i(int inumber, char *data, int length, int offset)
{
// if (length == 24)
// {
// // print dir_entry
// dir_entry *dir_entry_ptr = (dir_entry *)data;
// printf("+----------[DIR ENTRY]--------------+\n");
// printf("| Name: %20s |\n", dir_entry_ptr->name);
// printf("| Inode: %20d |\n", dir_entry_ptr->inode_idx);
// printf("| Valid: %20d |\n", dir_entry_ptr->valid);
// printf("| Type: %20d |\n", dir_entry_ptr->type);
// printf("+-----------------------------------+\n");
// }
if (mounted_disk == NULL)
{
printf("{SFS} -- [ERROR] Disk is not mounted -- File not write\n");
return -1;
}
if (length == 0)
{
return 0;
}
// Read the inode
int inode_block_idx = sb->inode_block_idx + (inumber / 128);
int inode_offset = (inumber % 128);
inode *inode_ptr = (inode *)malloc(128 * sizeof(inode));
read_block(mounted_disk, inode_block_idx, inode_ptr);
// Check if the inode is valid
if (inode_ptr[inode_offset].valid == 0)
{
printf("{SFS} -- [ERROR] Inode is not valid -- File not write\n");
return -1;
}
// Check if the offset is valid
if (offset < 0)
{
printf("{SFS} -- [ERROR] Offset is not valid -- File not write\n");
return -1;
}
// Read the data
int start_block = offset / BLOCKSIZE;
//truncate the length if it is too long
int final_length = (length + offset > MAX_FILE_SIZE ? MAX_FILE_SIZE : length + offset);
int end_block = (final_length + BLOCKSIZE - 1) / BLOCKSIZE;
length = final_length - offset;
int32_t *indirect_ptr = (int32_t *)malloc(BLOCKSIZE);
int file_blocks = (inode_ptr[inode_offset].size + BLOCKSIZE - 1) / BLOCKSIZE;
int req_blocks = (final_length + BLOCKSIZE - 1) / BLOCKSIZE;
int bytes_written = 0, data_offset = 0;
if (file_blocks < req_blocks)
{
int empty_blocks = start_block - file_blocks - 1;
if (empty_blocks < 0)
empty_blocks = 0;
bytes_written += empty_blocks * BLOCKSIZE;
bytes_written += offset % BLOCKSIZE;
for (int i = file_blocks + (inode_ptr[inode_offset].size != 0); i < req_blocks; i++)
{
if (i < 5)
inode_ptr[inode_offset].direct[i] = find_free_data_block();
else
{
if (i == 5)
{
inode_ptr[inode_offset].indirect = find_free_data_block();
write_block(mounted_disk, inode_block_idx, inode_ptr);
}
read_block(mounted_disk, sb->data_block_idx + inode_ptr[inode_offset].indirect, (char *)indirect_ptr);
indirect_ptr[i - 5] = find_free_data_block();
write_block(mounted_disk, sb->data_block_idx + inode_ptr[inode_offset].indirect, (char *)indirect_ptr);
}
}
}
char *temp_data = (char *)malloc(BLOCKSIZE);
read_block(mounted_disk, sb->data_block_idx + inode_ptr[inode_offset].indirect, (char *)indirect_ptr);
for (int i = start_block; i < end_block; i++)
{
if (i < 5)
{
if (read_block(mounted_disk, sb->data_block_idx + inode_ptr[inode_offset].direct[i], temp_data) < 0)
{
printf("{SFS} -- [ERROR] Failed to read direct data block -- File not written\n");
return -1;
}
}
else
{
if (read_block(mounted_disk, indirect_ptr[i - 5], temp_data) < 0)
{
printf("{SFS} -- [ERROR] Failed to read indirect data block -- File not written\n");
return -1;
}
}
int copy_length = (i == end_block - 1 ? length : BLOCKSIZE - offset % BLOCKSIZE);
memcpy(temp_data + offset % BLOCKSIZE, data + data_offset, copy_length);
if (i < 5)
write_block(mounted_disk, sb->data_block_idx + inode_ptr[inode_offset].direct[i], temp_data);
else
write_block(mounted_disk, sb->data_block_idx + indirect_ptr[i - 5], temp_data);
// printf("\033[1;35m[DEBUG]\033[0m Written [%5d] bytes to block [%2d] at offset [%5d]\n", copy_length, i, offset % BLOCKSIZE);
data_offset += copy_length;
offset += copy_length;
bytes_written += copy_length;
length -= copy_length;
}
// Update the inode
inode_ptr[inode_offset].size = final_length;
write_block(mounted_disk, inode_block_idx, inode_ptr);
free(temp_data);
free(indirect_ptr);
free(inode_ptr);
return bytes_written;
}
int fit_to_size(int inumber, int size)
{
if (mounted_disk == NULL)
{
printf("{SFS} -- [ERROR] Disk is not mounted -- File not fit_to_size\n");
return -1;
}
// Read the inode
int inode_block_idx = sb->inode_block_idx + (inumber / 128);
int inode_offset = (inumber % 128);
inode *inode_ptr = (inode *)malloc(128 * sizeof(inode));
if (read_block(mounted_disk, inode_block_idx, inode_ptr) < 0)
{
printf("{SFS} -- [ERROR] Failed to read inode from disk -- File not fit_to_size\n");
return -1;
}
// Check if the inode is valid
if (inode_ptr[inode_offset].valid == 0)
{
printf("{SFS} -- [ERROR] Inode is not valid -- File not fit_to_size\n");
return -1;
}
int file_blocks = (inode_ptr[inode_offset].size + BLOCKSIZE - 1) / BLOCKSIZE;
if (inode_ptr[inode_offset].size > size)
{
inode_ptr[inode_offset].size = size;
if (write_block(mounted_disk, inode_block_idx, inode_ptr))
{
printf("{SFS} -- [ERROR] Failed to write inode to disk -- File not fit_to_size\n");
return -1;
}
}
int req_blocks = (size + BLOCKSIZE - 1) / BLOCKSIZE;
int free_indirect = 0;
for (int i = req_blocks + 1; i <= file_blocks; i++)
{
if (i < 5)
free_data_block(inode_ptr[inode_offset].direct[i]);
else
{
if (i == 5)
free_indirect = 1;
int32_t *indirect_ptr = (int32_t *)malloc(BLOCKSIZE);
read_block(mounted_disk, sb->data_block_idx + inode_ptr[inode_offset].indirect, (char *)indirect_ptr);
free_data_block(indirect_ptr[i - 5]);
write_block(mounted_disk, sb->data_block_idx + inode_ptr[inode_offset].indirect, (char *)indirect_ptr);
}
}
if (free_indirect == 1)
{
free_data_block(inode_ptr[inode_offset].indirect);
inode_ptr[inode_offset].indirect = find_free_data_block();
write_block(mounted_disk, inode_block_idx, inode_ptr);
}
return 0;
}
int create_dir(char *dirpath)
{
if (mounted_disk == NULL)
{
printf("{SFS} -- [ERROR] Disk is not mounted -- Directory not created\n");
return -1;
}
// Get the name of the directory to be created
int *n_parts = (int *)malloc(sizeof(int));
char **parts = path_parse(dirpath, n_parts);
if (!validate_path(parts, *n_parts))
{
printf("{SFS} -- [ERROR] Invalid path -- Directory not created\n");
return -1;
}
int new_inumber = -1;
if ((new_inumber = recursive_create(parts, *n_parts)) == -1)
{
printf("{SFS} -- [ERROR] Failed to create directory -- Directory not created \n");
return -1;
}
free(n_parts);
free(parts);
return new_inumber;
}
int remove_dir(char *dirpath)
{
if (mounted_disk == NULL)
{
printf("{SFS} -- [ERROR] Disk is not mounted -- Directory not removed\n");
return -1;
}
int parent_inumber = get_inumber(dirpath, 1);
if (parent_inumber == -1)
{
printf("{SFS} -- [ERROR] Directory not found -- Directory not removed\n");
return -1;
}
// Get the name of the directory to be removed
int *n_parts = (int *)malloc(sizeof(int));
char **parts = path_parse(dirpath, n_parts);
char *dirname = parts[*n_parts - 1];
if (!validate_path(parts, *n_parts))
{
printf("{SFS} -- [ERROR] Invalid path -- Directory not removed\n");
return -1;
}
// Check if the directory already exists
if (find_file(parent_inumber, dirname) < 0)
{
printf("{SFS} -- [ERROR] Directory does not exists -- Directory not removed\n");
return -1;
}
// read parent inode to compute offset
inode *parent_inode = (inode *)malloc(BLOCKSIZE);
int offset = parent_inumber % 128;
if (read_block(mounted_disk, sb->inode_block_idx + parent_inumber / 128, parent_inode) < 0)
{
printf("{SFS} -- [ERROR] Failed to read inode -- Directory not removed\n");
return -1;
}
int file_size = parent_inode[offset].size;
dir_entry *dir_files = (dir_entry *)malloc(file_size);
if (read_i(parent_inumber, (char *)dir_files, file_size, 0) < 0)
{
printf("{SFS} -- [ERROR] Failed to read data block -- Directory not removed\n");
return -1;
}
for (int i = 0; i < file_size / sizeof(dir_entry); i++)
{
if (strcmp(dirname, dir_files[i].name) == 0)
{
dir_files[i].valid = 0;
if (recursive_remove(dir_files[i].inode_idx, dir_files[i].type) < 0)
{
printf("{SFS} -- [ERROR] Failed to recursively remove directory -- Directory not removed\n");
return -1;
}
break;
}
}
// Write the directory entry
if (write_i(parent_inumber, (char *)dir_files, file_size, 0) < 0)
{
printf("{SFS} -- [ERROR] Failed to write data block -- Directory not removed\n");
free(dir_files);
free(parent_inode);
return -1;
}
free(dir_files);
free(parent_inode);
return 0;
}
int read_file(char *filepath, char *data, int length, int offset)
{
int inumber = get_inumber(filepath, 0);
if (inumber == -1)
{
printf("{SFS} -- [ERROR] File not found -- File not read\n");
return -1;
}
if (data == NULL)
{
printf("{SFS} -- [ERROR] Invalid data pointer -- Failed Read File\n");
return -1;
}
// Read data from the inode in data buffer
int bytes_read = read_i(inumber, data, length, offset);
if (bytes_read == -1)
{
printf("{SFS} -- [ERROR] Failed to read file -- File not read\n");
return -1;
}
return bytes_read;
}
int write_file(char *filepath, char *data, int length, int offset)
{
int parent_inumber = get_inumber(filepath, 1);
if (parent_inumber == -1)
{
printf("{SFS} -- [ERROR] File not found -- File not written\n");
return -1;
}
if (data == NULL)
{
printf("{SFS} -- [ERROR] Invalid data pointer -- Failed Write File\n");
return -1;
}
// Get the name of the File to be created
int *n_parts = (int *)malloc(sizeof(int));
char **parts = path_parse(filepath, n_parts);
char *filename = parts[*n_parts - 1];
if (!validate_path(parts, *n_parts))
{
printf("{SFS} -- [ERROR] Invalid path -- File not written\n");
return -1;
}
// Check if the File already exists
int inumber = -1;
if ((inumber = find_file(parent_inumber, filename)) < 0)
{
if ((inumber = create_file()) < 0)
{
printf("{SFS} -- [ERROR] Failed to write file -- File not written\n");
return -1;
}
}
// Write data to the inode in data buffer
int bytes_written = write_i(inumber, data, length, offset);
if (bytes_written == -1)
{
printf("{SFS} -- [ERROR] Failed to write file -- File not written\n");
return -1;
}
dir_entry *e = (dir_entry *)malloc(sizeof(dir_entry));
e->inode_idx = inumber;
e->name = (char *)malloc(strlen(filename) + 1);
strcpy(e->name, filename);
e->name_len = strlen(filename);
e->type = 0;
e->valid = 1;
// Write the directory entry
inode *parent_inode = (inode *)malloc(BLOCKSIZE);
int off = parent_inumber % 128;
if (read_block(mounted_disk, sb->inode_block_idx + parent_inumber / 128, parent_inode))
{
printf("{SFS} -- [ERROR] Failed to read inode -- File not written\n");
return -1;
}
int file_size = parent_inode[off].size;
dir_entry *dir_files = (dir_entry *)malloc(file_size);
if (read_i(parent_inumber, (char *)dir_files, file_size, 0) < 0)
{
printf("{SFS} -- [ERROR] Failed to read data block -- File not written\n");
return -1;
}
for (int i = 0; i < file_size / sizeof(dir_entry); i++)
{
if (strcmp(filename, dir_files[i].name) == 0)
{
dir_files[i] = *e;
if (write_i(parent_inumber, (char *)dir_files, file_size, 0) < 0)
{
printf("{SFS} -- [ERROR] Failed to write data block -- File not written\n");
return -1;
}
goto write_file_end;
}
}
// Add the directory entry
if (write_i(parent_inumber, (char *)e, sizeof(dir_entry), file_size) < 0)
{
printf("{SFS} -- [ERROR] Failed to write data block -- File not written\n");
return -1;
}
write_file_end:
free(dir_files);
free(parent_inode);
return bytes_written;
}
void set(bitset *bitmap, int index)
{
int byte_index = index >> 3;
int bit_index = index & 7;
bitmap[byte_index] |= (1 << bit_index);
}
void unset(bitset *bitmap, int index)
{
int byte_index = index >> 3;
int bit_index = index & 7;
bitmap[byte_index] &= ~(1 << bit_index);
}
int is_set(bitset *bitmap, int index)
{
int byte_index = index >> 3;
int bit_index = index & 7;
return (bitmap[byte_index] & (1 << bit_index)) != 0;
}
int clear_bitmap(int block, int bitmap_start)
{
int block_number = block / (8 * BLOCKSIZE);
int bit_number = block % (8 * BLOCKSIZE);
bitset *bitmap = (bitset *)malloc(BLOCKSIZE);
if (read_block(mounted_disk, bitmap_start + block_number, bitmap))
{
printf("{SFS} -- [ERROR] Failed to read bitmap -- Failed to clear bitmap\n");
free(bitmap);
return -1;
}
unset(bitmap, bit_number);
if (write_block(mounted_disk, bitmap_start + block_number, bitmap))
{
printf("{SFS} -- [ERROR] Failed to write bitmap -- Failed to clear bitmap\n");
free(bitmap);
return -1;
}
free(bitmap);
return 0;
}
int find_free_inode()
{
if (mounted_disk == NULL)
{
printf("{SFS} -- [ERROR] Disk is not mounted -- Failed to find free inode\n");
return -1;
}
// Read the inode bit map
int IB = (int)ceil((double)sb->inodes / (BLOCKSIZE * 8));
bitset *inode_bitmap = (bitset *)malloc(IB * BLOCKSIZE);
for (int i = 0; i < IB; i++)
{
if (read_block(mounted_disk, sb->inode_bitmap_block_idx + i, inode_bitmap + i * BLOCKSIZE))
{
printf("{SFS} -- [ERROR] Failed to read inode bitmap -- Failed to find free inode\n");
free(inode_bitmap);
return -1;
}
}
// Find the first free inode
for (int i = 0; i < sb->inodes; i++)
{
int index = (i / BLOCKSIZE) >> 3;
if (is_set(inode_bitmap + index * BLOCKSIZE, i % (BLOCKSIZE * 8)))
{
continue;
}
else
{
// Update the inode bitmap
int bit_idx = (i / BLOCKSIZE) >> 3;
int bit_offset = i % (BLOCKSIZE * 8);
set(inode_bitmap + bit_idx, bit_offset);
if (write_block(mounted_disk, sb->inode_bitmap_block_idx + bit_idx, inode_bitmap + bit_idx))
{
printf("{SFS} -- [ERROR] Failed to write inode bitmap to disk\n");
return -1;
}
free(inode_bitmap);
return i;
}
}
free(inode_bitmap);
return -1;
}
int find_free_data_block()
{
if (mounted_disk == NULL)
{
printf("{SFS} -- [ERROR] Disk is not mounted -- Failed to find free data block\n");
return -1;
}
// Read the data block bit map
int DB = sb->data_blocks;
int DBB = (int)ceil((double)DB / (BLOCKSIZE * 8));
bitset *data_block_bitmap = (bitset *)malloc(DBB * BLOCKSIZE);
for (int i = 0; i < DBB; i++)
{
if (read_block(mounted_disk, sb->data_block_bitmap_idx + i, data_block_bitmap + i * BLOCKSIZE))
{
printf("{SFS} -- [ERROR] Failed to read data block bitmap -- Failed to find free data block\n");
free(data_block_bitmap);
return -1;
}
}
// Find the first free data block
for (int i = 0; i < DB; i++)
{
int index = (i / BLOCKSIZE) >> 3;
if (is_set(data_block_bitmap + index * BLOCKSIZE, i % (BLOCKSIZE * 8)))
{
continue;
}
else
{
// Update the data bitmap
int bit_idx = (i / BLOCKSIZE) >> 3;
int bit_offset = i % (BLOCKSIZE * 8);
set(data_block_bitmap + bit_idx, bit_offset);
if (write_block(mounted_disk, sb->data_block_bitmap_idx + bit_idx, data_block_bitmap + bit_idx))
{
printf("{SFS} -- [ERROR] Failed to write data block bitmap to disk\n");
return -1;
}
free(data_block_bitmap);
return i;
}
}
free(data_block_bitmap);
return -1;
}
int find_file(int inumber, char *filename)
{
// Read the inode
inode *in = (inode *)malloc(BLOCKSIZE);
int off = inumber % 128;
if (read_block(mounted_disk, sb->inode_block_idx + inumber / 128, in))
{
printf("{SFS} -- [ERROR] Failed to read inode -- Failed to get inumber\n");
return -1;
}