-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.c
More file actions
executable file
·945 lines (732 loc) · 27.1 KB
/
fs.c
File metadata and controls
executable file
·945 lines (732 loc) · 27.1 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
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
#include "fs.h"
#include "disk.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#define FS_MAGIC 0xf0f03410
#define INODES_PER_BLOCK 128
#define POINTERS_PER_INODE 5
#define POINTERS_PER_BLOCK 1024
// Returns the number of dedicated inode blocks given the disk size in blocks
#define NUM_INODE_BLOCKS(disk_size_in_blocks) (1 + (disk_size_in_blocks / 10))
struct fs_superblock
{
int magic; // Magic bytes
int nblocks; // Size of the disk in number of blocks
int ninodeblocks; // Number of blocks dedicated to inodes
int ninodes; // Number of dedicated inodes
};
struct fs_inode
{
int isvalid; // 1 if valid (in use), 0 otherwise
int size; // Size of file in bytes
int direct[POINTERS_PER_INODE]; // Direct data block numbers (0 if invalid)
int indirect; // Indirect data block number (0 if invalid)
};
union fs_block
{
struct fs_superblock super; // Superblock
struct fs_inode inode[INODES_PER_BLOCK]; // Block of inodes
int pointers[POINTERS_PER_BLOCK]; // Indirect block of direct data block numbers
char data[DISK_BLOCK_SIZE]; // Data block
};
typedef struct {
char *data; // the buffer (always null‐terminated)
int length; // number of chars in data (excluding ’\0’)
size_t capacity; // total allocated bytes in data
} StringBuilder;
union fs_block superblock;
/**
* + Freemap should be allocated in fs_mount (via malloc)
* based on the number of blocks (nblocks) in your super block
*
* + freemap can then be indexed like an array for determining whether or
* not a block is free... e.g.
* if(!freemap[123])
* printf("Block 123 is NOT free\n");
*/
static int *freemap = 0;
int freemapSize;
void fs_debug()
{
if (freemap) {
for (int i = 0; i < freemapSize; i++) {
printf("FreeMap[%d] = %d\n", i, freemap[i]);
}
}
union fs_block block;
disk_read(0, block.data);
superblock = block;
printf("superblock:\n");
if(superblock.super.magic == FS_MAGIC){
printf(" magic number is valid\n");
}else{
printf(" magic number is invalid\n");
}
printf(" %d blocks on disk\n", superblock.super.nblocks);
printf(" %d blocks for inodes\n", superblock.super.ninodeblocks);
printf(" %d inodes total\n", superblock.super.ninodes);
//Loop through inode blocks
for (int b = 1; b <= superblock.super.ninodeblocks; b++){
//Open inode block
union fs_block inodeblock;
disk_read(b, inodeblock.data);
printf("Block num: %d\n",b);
//Loop through inode list
for (int i = 0; i < INODES_PER_BLOCK; i++){
if(inodeblock.inode[i].isvalid){
int actualI = (INODES_PER_BLOCK*(b-1))+i;
printf("inode %d:\n",actualI);
printf(" size: %d bytes\n",inodeblock.inode[i].size);
//Loop and print through direct list of inode.
printf(" direct blocks:");
for (int ii = 0; ii < POINTERS_PER_INODE; ii++){
//Prevents the printing of many 0's
if (inodeblock.inode[i].direct[ii]!=0){
printf(" %d",inodeblock.inode[i].direct[ii]);
}
}
printf("\n");
if (inodeblock.inode[i].indirect != 0){
printf(" indirect block: %d\n",inodeblock.inode[i].indirect);
// Read in data block of the indirect block
union fs_block curr_ind_block;
disk_read(inodeblock.inode[i].indirect, curr_ind_block.data);
printf(" indirect data blocks: ");
for (int iii = 0; iii <POINTERS_PER_BLOCK; iii++){
if (curr_ind_block.pointers[iii]!=0){
printf(" %d",curr_ind_block.pointers[iii]);
}
}
printf("\n");
}
}
}
}
}
int fs_format()
{
// Clear out inode tables in file system
for (int i = 1; i <= superblock.super.ninodeblocks; i++) {
// superblock.inode =
memset(superblock.inode, 0, sizeof(superblock.inode));
disk_write(i, superblock.inode);
}
// Clear out data table in file system
for (int i = 2; i < superblock.super.nblocks; i++) {
memset(superblock.data, 0, sizeof(superblock.data));
disk_write(i, superblock.data);
}
// Initialize our superblock
superblock.super.magic = FS_MAGIC;
superblock.super.nblocks = disk_size();
superblock.super.ninodeblocks = NUM_INODE_BLOCKS(disk_size());
superblock.super.ninodes = NUM_INODE_BLOCKS(disk_size()) * INODES_PER_BLOCK;
disk_write(0, superblock.data);
// Write in the superblock
return 1;
}
int fs_mount()
{
union fs_block block;
union fs_block indirect;
disk_read(0, block.data);
superblock = block;
if (superblock.super.magic != FS_MAGIC) {
printf("Error\n");
return 0;
}
disk_read(0, superblock.data);
freemap = malloc(sizeof(int) * disk_size());
freemapSize = disk_size();
// Allocate superblock and inodes
freemap[0] = 1;
for (int i = 1; i <= superblock.super.ninodeblocks; i++) {
freemap[i] = 1;
}
for (int i = 0; i < freemapSize; i++) {
disk_read(i, block.data);
// Go through inodes and see which data blocks are being used
for (int b = 1; b <= superblock.super.ninodeblocks; b++){
for (int i = 0; i < INODES_PER_BLOCK; i++){
if (block.inode[i].isvalid) {
// Check direct
for (int j = 0; j < POINTERS_PER_INODE; j++) {
int position = block.inode[i].direct[j];
if (position < freemapSize) {
freemap[position] = 1;
}
}
int position = block.inode[i].indirect;
if (position < freemapSize) {
freemap[position] = 1;
}
// Check indirect
if (position < freemapSize && position != 0) {
disk_read(block.inode[i].indirect, indirect.data);
for (int j = 0; j < POINTERS_PER_BLOCK; j++) {
if (indirect.pointers[j] < freemapSize) {
freemap[indirect.pointers[j]] = 1;
}
}
}
}
}
}
}
return 1;
}
int fs_unmount()
{
if (freemap == NULL) {
return 0;
}
free(freemap);
freemap = NULL;
return 1;
}
int fs_create()
{
struct fs_inode *inode = malloc(sizeof(struct fs_inode));
if (inode == NULL) {
printf("failed here\n");
return -1;
}
inode->size = 0;
inode->isvalid = 0;
//inode->direct = 0;
//inode->indirect = 0;
//Loop through inode blocks
for (int b = 1; b <= superblock.super.ninodeblocks; b++){
//Open inode block
union fs_block inodeblock;
disk_read(b, inodeblock.data);
printf("Block num: %d\n",b);
//Loop through Inodes in a block
for (int in = 0; in < INODES_PER_BLOCK; in++){
//Try and find a not valid inode
if (inodeblock.inode[in].isvalid==0){
//Means its empty
int inumber = (b-1)*INODES_PER_BLOCK + in;
inodeblock.inode[in].isvalid=1;
// freemap[inumber] = 1;
disk_write(b,inodeblock.data);
return inumber;
}
}
}
return -1;
}
//Helper for finding retrieving inode with inumber
//Also retrieves the blocknum.
//Also retrieves the block.
//return 0 : failed
//return 1 : success
int get_inode(int inumber, struct fs_inode *inode, int *blocknum, union fs_block *block, int *in)
{
//Calculate the block
int blockIndex = (inumber / (INODES_PER_BLOCK))+1;
int blockOffset = (inumber % (INODES_PER_BLOCK));
union fs_block blocky;
disk_read(blockIndex, blocky.data);
*in = blockOffset;
*blocknum = blockIndex;
*block = blocky;
//Get inode of block
if (((*inode = blocky.inode[blockOffset]).isvalid)==1){
return 1;
}
printf("Invalid\n");
return 0;
}
int fs_delete(int inumber)
{
if (freemap == NULL) {
printf("Mount first\n");
return 0;
}
//Calculate the block that inode is apart of
//INODES_PER_BLOCK;
struct fs_inode inode;
int blocknum;
union fs_block block;
int in;
if (get_inode(inumber, &inode, &blocknum, &block, &in)){
if (inode.isvalid==0){
return 0;
}
// Clear out freemap of direct blocks so it can be used
for (int i = 0; i < 5; i++) {
if (block.inode[in].direct[i] != 0) {
freemap[block.inode[in].direct[i]] = 0;
}
}
union fs_block indirect;
int ind = block.inode[in].indirect;
// Clear out indirect
if (ind < freemapSize && ind != 0) {
disk_read(block.inode[in].indirect, indirect.data);
for (int i = 0; i < POINTERS_PER_BLOCK; i++) {
if (indirect.pointers[i] != 0) {
freemap[indirect.pointers[i]] = 0;
}
}
}
memset(&block.inode[in], 0, sizeof(superblock.inode[0]));
disk_write(blocknum, block.data);
return 1;
}
return 0;
}
int fs_getsize(int inumber)
{
struct fs_inode inode;
int blocknum;
union fs_block block;
int in;
if (get_inode(inumber, &inode, &blocknum, &block, &in)){
return inode.size;
}
return -1;
}
//Helper function for read/write to get a specific data block given a offset and the inode
//Returns data block and possible second data block.
int get_datablock(int offset, struct fs_inode *inode, int length, union fs_block *block){
int blockNum = offset/4096;
int blockOffset = offset%4096;
//Check if valid blockNum
if (blockNum-POINTERS_PER_INODE>=POINTERS_PER_BLOCK){
return 0;
}
//Check for direct
if (blockNum<POINTERS_PER_INODE) {
disk_read((*inode).direct[blockNum], (*block).data);
return 1;
}
//Leaving this else statments here for future implementation of more indirection.
else{
union fs_block indirectBlock;
//Indirect
disk_read((*inode).indirect, indirectBlock.data);
disk_read((indirectBlock).pointers[blockNum - POINTERS_PER_INODE], (*block).data);
return 1;
}
}
int addBlockIndex(struct fs_inode *inode2, int blockIndex, int inumber, int size) {
struct fs_inode inode;
int blocknum;
union fs_block block;
int in; // index of this inode within the block
printf("Block Index: %d\n", blockIndex);
freemap[blockIndex] = 1;
if (get_inode(inumber, &inode, &blocknum, &block, &in)) {
// Try all direct pointers first
for (int i = 0; i < POINTERS_PER_INODE; i++) {
if (block.inode[in].direct[i] == 0) {
block.inode[in].size = size;
block.inode[in].direct[i] = blockIndex;
disk_write(blocknum, block.data);
return 1;
}
}
// handle the indirect block if need be
// TODO still i believe need to allocate the block of indirect pointers.
union fs_block indir;
disk_read(inode.indirect, indir.data);
for (int i = 0; i < POINTERS_PER_BLOCK; i++) {
if (indir.pointers[i] == 0) {
block.inode[i].size = size;
indir.pointers[i] = blockIndex;
disk_write(inode.indirect, indir.data);
return 1;
}
}
}
return 0;
}
//Function listDataBlocks()
//Needs a datablocks to allocate list with max size of 5 + 1024 * sizeof(int)
//Retrieves int list of all datablocks that will be needed in the copy process
//Retrieves a fromEnd int that states what should be copied out from the end of last data block
//Returns 0 if error occurs
//Returns 1 if success
int listDataBlocks(int inumber, int length, int offset, int **datablocks, int *sizeOfDBs)
{
if (offset != 0 && offset < length) {
return 1;
}
struct fs_inode inode;
int blocknum;
union fs_block block;
int in;
if (get_inode(inumber, &inode, &blocknum, &block, &in)){
int totalBlocksNeeded = (length)/4096; //forumula is made complicated so it can round up with int math
if (totalBlocksNeeded == 0) {
totalBlocksNeeded = 1;
}
*sizeOfDBs = totalBlocksNeeded;
*datablocks = malloc((totalBlocksNeeded) * sizeof(int));
if (!*datablocks) {
return 0;
}
int startingBlockIndex = offset/4096;
int tempBlockIndex = startingBlockIndex;
//Loops until populated all
int i = 0;
union fs_block inodeindirectblock;
disk_read(inode.indirect, inodeindirectblock.data);
while(totalBlocksNeeded != 0){
//Populate datablocks list
if (tempBlockIndex < 5){
//Direct
(*datablocks)[i] = inode.direct[tempBlockIndex];
}
else if (tempBlockIndex < (1024+5)){
//Indirect
(*datablocks)[i] = inodeindirectblock.pointers[tempBlockIndex-5];
}
else{
//printf("HERE AT THE ELSE BAD BAD BAD\n");
}
tempBlockIndex +=1;
i +=1;
totalBlocksNeeded = totalBlocksNeeded - 1;
}
//*sizeOfDBs = tempBlockIndex;
return 1;
}
//Fail
return 0;
}
// Ensure we have room for at least new_len more chars (+1 for ’\0’)
static void ensure_capacity(StringBuilder *sb, size_t new_len) {
size_t needed = sb->length + new_len + 1;
if (needed > sb->capacity) {
// grow to max(needed, 2×old capacity)
size_t cap = sb->capacity ? sb->capacity * 2 : 16;
if (cap < needed) cap = needed;
sb->data = realloc(sb->data, cap);
if (!sb->data) { perror("realloc"); exit(1); }
sb->capacity = cap;
}
}
// Append a C-string
void sb_append_str(StringBuilder *sb, const char *s) {
size_t n = strlen(s);
ensure_capacity(sb, n);
memcpy(sb->data + sb->length, s, n);
sb->length += n;
sb->data[sb->length] = '\0';
}
// extract [start..end] (inclusive) from data[]
// returns a malloc’d, NUL-terminated string (or NULL on failure)
char *substr(const char *data, size_t start, size_t end) {
if (end < start) return NULL; // invalid
size_t len = end - start + 1; // number of chars to copy
char *out = malloc(len + 1); // +1 for '\0'
if (!out) return NULL; // alloc failed
memcpy(out, data + start, len); // copy the slice
out[len] = '\0'; // NUL-terminate
return out;
}
//Function copyCatList()
//Needs the int list from the listDataBlocks function an int to start in first block
//and an int fromEnd saying where to stop from second
//returns 1 if success
//returns 0 if fail
int copyCatList(int **DataBlocks,int DBLength, int offset, int fromEnd, int length, char **data, int *charsRead)
{
*data[length];
int *blocks = *DataBlocks;
//data[length]='\0'; TODO add this back.
int tempIndex = 0;
*charsRead = 0;
//Looping through datablocks in datablocks list
StringBuilder sb = { NULL, 0, 0};
while (tempIndex < DBLength){
int startAt = 0;
int endAt = 4096;
//Check if this is the last datablock
if (tempIndex == DBLength - 1){
//Last datablock
//Only go to length
endAt = 4096-fromEnd;
}
if (tempIndex == 0){
//First Datablock
startAt = offset % 4096;
}
union fs_block readDataBlock;
//Load in datablock and data string
if (blocks[tempIndex]!=0){
disk_read(blocks[tempIndex],readDataBlock.data);
char *dataToCopy = substr(readDataBlock.data,startAt,endAt);
//Append data to char list
sb_append_str(&sb, dataToCopy);
free(dataToCopy);
}
tempIndex+=1;
}
*charsRead = sb.length;
strncpy(*data,sb.data,sb.capacity);
return 1;
}
void print_list(const int *list, size_t length) {
for (size_t i = 0; i < length; i++) {
printf("list[%zu] = %d\n", i, list[i]);
}
}
//Function fs_read
//Retrieves data starting from offset (byte) in a specified inode and copies into the data variable
//Returns total number of bytes read in. Read in bytes can be smaller than requested.
//Returns 0 for any errors
int fs_read(int inumber, char *data, int length, int offset)
{
int **datablocks;
int *sizeOfDBs;
if (listDataBlocks(inumber, length, offset, &datablocks, &sizeOfDBs)){
//Success, datablocks have been retrieved
int fromEnd = 4096-((offset+length)%4096);
if (fromEnd == 4096){
fromEnd = 0;
}
int charsRead = 0;
if (copyCatList(&datablocks,sizeOfDBs, offset, fromEnd, length, &data,&charsRead)){
return charsRead;
}
}
return 0;
}
int listWriteDataBlocks(int inumber, int length, int offset, int **datablocks, int *sizeOfDBs)
{
struct fs_inode inode;
int blocknum;
union fs_block block;
int in;
if (get_inode(inumber, &inode, &blocknum, &block, &in)){
int totalBlocksNeeded = (offset+ length+ 4096-1)/4096; //forumula is made complicated so it can round up with int math
*sizeOfDBs = totalBlocksNeeded;
*datablocks = malloc((totalBlocksNeeded) * sizeof(int));
if (!*datablocks) {
return 0;
}
int startingBlockIndex = offset/4096;
int tempBlockIndex = startingBlockIndex;
//Loops until populated all
int i = 0;
union fs_block inodeindirectblock;
disk_read(inode.indirect, inodeindirectblock.data);
while(totalBlocksNeeded != 0){
//Populate datablocks list
if (tempBlockIndex < 5){
//Direct
(*datablocks)[i] = inode.direct[tempBlockIndex];
}
else if (tempBlockIndex < (1024+5)){
//Indirect
(*datablocks)[i] = inodeindirectblock.pointers[tempBlockIndex-5];
}
else{
}
tempBlockIndex +=1;
i +=1;
totalBlocksNeeded = totalBlocksNeeded - 1;
}
return 1;
}
printf("FAILED the get inode\n");
//Fail
return 0;
}
int splitDataToLists(int DBLength, int offset, char *data, int length, char ***dataSplit, int *dataListSplitLength){
char **splits = malloc(DBLength * sizeof(*splits));
int tempDataCopied = 0;
for (int i = 0; i < DBLength; i++){
int NumToSplit = 4096;
if (i == DBLength - 1){
//On last block
NumToSplit = length % 4096;
}
if (i == 0){
//First one so take into account offset
NumToSplit -= offset;
}
char *dataToCopy = substr(data, tempDataCopied, tempDataCopied+NumToSplit);
splits[i] = malloc(NumToSplit + 1);
if (!splits[i]) {
free(dataToCopy);
for (int j = 0; j < i; j++) free(splits[j]);
free(splits);
return 0;
}
memcpy(splits[i], dataToCopy, NumToSplit);
splits[i][NumToSplit] = '\0';
free(dataToCopy);
tempDataCopied += NumToSplit;
}
*dataSplit = splits;
*dataListSplitLength = DBLength;
return 1;
}
int writeCatList(int **DataBlocks,int DBLength, int offset, int fromEnd, int length, char *data, int *charsWrote, char ***dataSplit, int dataListSplitLength, struct fs_inode *inode, int inumber){
int tempIndex = 0;
int tempDataWritten = 0;
int dataToGo = length;
int foundFreeBlock = -1;
while (tempIndex < DBLength){
printf("Inside loop\n");
dataToGo = length - tempDataWritten;
int startAt = 0;
int endAt = 4096;
//Check if this is the last datablock
if (tempIndex == DBLength - 1){
//Last datablock
//Only go to length
endAt = length;
}
if (tempIndex == 0){
//First Datablock
startAt = offset % 4096;
}
//Check if dataToGo is smaller than endAt (IDK if actually needed)
if (dataToGo<endAt){
endAt = dataToGo;
}
StringBuilder sb = { NULL, 0, 0};
//First Read in current data if 4096-endAt-startAt != 4096
if (4096-endAt-startAt != 0){
printf("First if\n");
//Check if start or end needs to be saved
if (startAt == 0){
//save end
//Read in what is on the block already
union fs_block readInBlock;
disk_read(*DataBlocks[tempIndex],readInBlock.data);
char *readInData = readInBlock.data;
char *subData = substr(readInBlock.data,endAt,4096);
//Then write dataSplit[tempIndex] at the start index
sb_append_str(&sb,(*dataSplit)[tempIndex]);
sb_append_str(&sb,subData);
}
else{
printf("First else\n");
//save front
//Read in what is on the block already
union fs_block readInBlock;
disk_read(*DataBlocks[tempIndex],readInBlock.data);
char *readInData = readInBlock.data;
char *subData = substr(readInBlock.data,0,startAt);
//Then write dataSplit[tempIndex] at the startAt index
sb_append_str(&sb,subData);
sb_append_str(&sb,(*dataSplit)[tempIndex]);
}
if (*DataBlocks[tempIndex]==0){
printf("Second if\n");
//Means need to allocate a block for this.
//Consult bitmap
// int foundFreeBlock = 0;
for (int bmIndex = superblock.super.ninodeblocks+1; bmIndex < freemapSize; bmIndex++){
if (freemap[bmIndex]==0){
foundFreeBlock = bmIndex;
break;
}
}
if (foundFreeBlock == -1){
printf("Inode full if\n");
return 0;
//inode is full
//TODO
}else{
*DataBlocks[tempIndex]=foundFreeBlock;
addBlockIndex(inode,foundFreeBlock, inumber, length);
}
}
tempDataWritten+=endAt-startAt;
}else{
printf("Second else, tempIndex: %d\n", tempIndex);
// printf("DataBlocks[tempInd] = %d\n", *DataBlocks[tempIndex]);
//Write the dataSplit[tempIndex] straight to the block.
if (foundFreeBlock == -1){
printf("Here\n");
//Means need to allocate a block for this.
//Consult bitmap
// int foundFreeBlock = 0;
for (int bmIndex = 0; bmIndex < freemapSize; bmIndex++){
if (freemap[bmIndex]==0){
printf("Found free spot: %d\n", bmIndex);
foundFreeBlock = bmIndex;
// *DataBlocks[tempIndex] = foundFreeBlock;
sb_append_str(&sb,(*dataSplit)[tempIndex]);
break;
}
}
if (foundFreeBlock == -1){
printf("Inode full else\n");
//inode is full
//TODO
}
else{
// *DataBlocks[tempIndex]=foundFreeBlock;
addBlockIndex(inode, foundFreeBlock, inumber, length);
}
}
else {
printf("Returning 0\n");
return 0;
}
tempDataWritten+=4096;
}
// printf("tempIndex: %d, size of dataBlocks: %d", tempIndex, DBLength);
// printf("Writing here with index: %d\n", *DataBlocks[tempIndex]);
if (foundFreeBlock == -1) {
return 0;
}
disk_write(foundFreeBlock, sb.data);
foundFreeBlock = -1;
tempIndex +=1;
}
printf("Failure\n");
*charsWrote = tempDataWritten;
return 1;
}
//Function fs_write
int fs_write(int inumber, const char *data, int length, int offset)
{
int **datablocks;
int *sizeOfDBs;
// if (listWriteDataBlocks(inumber, length, offset, &datablocks, &sizeOfDBs)){
// Create a list of available data blocks to store this data
if (listDataBlocks(inumber, length, offset, &datablocks, &sizeOfDBs)){
//Success, datablocks have been retrieved
int fromEnd = 4096-((offset+length)%4096);
if (fromEnd == 4096){
fromEnd = 0;
}
int *charsWrote;
char *dataSplit;
int dataListSplitLength;
// dataSplit is an array of strings for each data block
// Split data into an array of strings in dataSplit
if (splitDataToLists(sizeOfDBs, offset, data, length, &dataSplit, &dataListSplitLength)){
struct fs_inode inode;
// inode block num
int blocknum;
// inode block
union fs_block block;
int in;
// Get the inode, blocknum, and block from inumber
if (get_inode(inumber, &inode, &blocknum, &block, &in)){
if (writeCatList(&datablocks,sizeOfDBs, offset, fromEnd, length, &data,&charsWrote, &dataSplit, dataListSplitLength,&inode, inumber)){
return charsWrote;
}
}
}
return 0;
}else{
printf("listDataBlocks returned 0\n");
}
return 0;
}