-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBTRFS Simulation_Source Code.cpp
More file actions
1257 lines (1103 loc) · 32.4 KB
/
BTRFS Simulation_Source Code.cpp
File metadata and controls
1257 lines (1103 loc) · 32.4 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
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
#include <bits/stdc++.h>
#include <iostream>
#include <ctime>
#include <string>
#include <stdlib.h>
#include <cstring>
#include <sstream>
#include <vector>
#define KEYS 9
using namespace std;
class BTreeNode //A class for nodes
{
private:
string data[KEYS]; //an array of strings (only 8byte strings are allocated to these array)
int min_degree;
int filled_key_amount;
int L_bit; // Is leaf? (the leaf bit)
int *keys_array;
BTreeNode **child_pointer_array;
public:
BTreeNode(int m_d, int l_b); // minimumm degree , leaf bit
BTreeNode *search(int val); //if key value is not present function returns null
void Deletion(int val);
void non_filled_node_insertion(int val, string str);
void merge(int index);
void split(int pos, BTreeNode *node);
int FindTheKey(int val);
void traverse();
friend void copyFile();
friend int main();
friend void modifyFile();
friend void viewFile();
friend class BTree;
};
class BTree //A class for trees
{
private:
BTreeNode *root;
int min_degree;
public:
BTree(int m_d) // Constructor
{
min_degree = m_d;
root = NULL;
}
void traverse()
{
if (root != NULL)
{
root->traverse();
}
}
BTreeNode* search(int val)
{
if(root == NULL)
{
return NULL;
}
else
{
return (root->search(val));
}
}
void insert(int val, string str);
void Deletion(int val);
};
BTreeNode::BTreeNode(int m_d, int l_b) //constructor
{
//initializing data members of node
filled_key_amount = 0;
min_degree = m_d;
L_bit = l_b;
keys_array = new int[2*min_degree-1];
child_pointer_array = new BTreeNode *[2*min_degree];
}
//used to find the index of the first key that is greater than or equal to a certain key value
int BTreeNode::FindTheKey(int val)
{
int index=0;
while ((index<filled_key_amount) && (keys_array[index] < val))
{
index=index+1;
}
return (index);
}
//used to traverse through the whole tree
void BTreeNode::traverse()
{
int i;
for (i = 0; i < filled_key_amount; i++)
{
if (L_bit == 0) //if it is not a leaf
{
child_pointer_array[i]->traverse(); //traverse recursively
}
cout<<keys_array[i]<<"-"<<data[i]<<endl;
}
if (L_bit == 0)
{
child_pointer_array[i]->traverse();
}
}
//used to merge two nodes
void BTreeNode::merge(int index)
{
BTreeNode *child1 = child_pointer_array[index]; //address of the node1 to merge
BTreeNode *child2 = child_pointer_array[index+1]; //address of the node2 to merge
child1->keys_array[min_degree-1] = keys_array[index];
child1->data[min_degree-1] = data[index];
for (int i=0; i<child2->filled_key_amount; ++i) //copying child2's keys to child1 and also strings
{
child1->keys_array[i+min_degree] = child2->keys_array[i];
child1->data[i+min_degree] = child2->data[i];
}
if (!child1->L_bit) //copying child2's child pointers to child1
{
for(int i=0; i<=child2->filled_key_amount; ++i)
{
child1->child_pointer_array[i+min_degree] = child2->child_pointer_array[i];
}
}
for (int i=index+1; i<filled_key_amount; ++i)
{
keys_array[i-1] = keys_array[i];
data[i-1] = data[i];
}
for (int i=index+2; i<=filled_key_amount; ++i)
{
child_pointer_array[i-1] = child_pointer_array[i];
}
child1->filled_key_amount += child2->filled_key_amount+1;
filled_key_amount--;
delete(child2);
}
//used to insert a key to the tree
void BTree::insert(int val, string str)
{
if (root == NULL) //initial situation (empty tree)
{
root = new BTreeNode(min_degree, 1);
root->filled_key_amount = 1; //just one key
root->keys_array[0] = val; //key value
(root->data[0]=str); //inserting the 5byte string to root
}
else
{
int temp= (root->filled_key_amount == 2*min_degree-1);
if (temp) //situation where the root is fully occupied by keys
{
BTreeNode *newRoot = new BTreeNode(min_degree,0); //new root node
newRoot->child_pointer_array[0] = root;
newRoot->split(0, root);
//adding the newly inserted key to the tree
int i = 0;
if (newRoot->keys_array[0] < val) //traverse to the position
{
i=i+1;
}
newRoot->child_pointer_array[i]->non_filled_node_insertion(val,str); //insert the key
root = newRoot;
}
else //situation where the root is not full
{
root->non_filled_node_insertion(val,str); //just add the key
}
}
}
//used to split a node
void BTreeNode::split(int pos, BTreeNode *node)
{
//initialize the new node
BTreeNode *newNode = new BTreeNode(node->min_degree, node->L_bit);
newNode->filled_key_amount = min_degree - 1;
//copy necessary key values to the new node
for (int i = 0; i < min_degree-1; i++)
{
newNode->keys_array[i] = node->keys_array[i+min_degree];
newNode->data[i]=node->data[i+min_degree];
}
//copy necessary children addresses
if (node->L_bit == false)
{
for (int i = 0; i < min_degree; i++)
{
newNode->child_pointer_array[i] = node->child_pointer_array[i+min_degree];
}
}
node->filled_key_amount = min_degree - 1; //update number of keys in old node
for (int i = filled_key_amount; i >= pos+1; i--)
{
child_pointer_array[i+1] = child_pointer_array[i];
}
child_pointer_array[pos+1] = newNode; //create the link between two nodes
for (int i = filled_key_amount-1; i >= pos; i--)
{
keys_array[i+1] = keys_array[i];
}
keys_array[pos] = node->keys_array[min_degree-1];
data[pos]=node->data[min_degree-1];
filled_key_amount++;
}
//used to insert a value to a non filled node (not fully filled)
void BTreeNode::non_filled_node_insertion(int val, string str)
{
int rmtIndex = filled_key_amount-1; //get the rightmost element's index
if (L_bit) //when current node is a leaf node
{
while (rmtIndex >= 0 && keys_array[rmtIndex] > val) //all key values those are greater than val is moved one position up in the key array
{
keys_array[rmtIndex+1] = keys_array[rmtIndex];
data[rmtIndex+1]=data[rmtIndex];
rmtIndex--;
}
keys_array[rmtIndex+1] = val; //insert the key element to suitable position
data[rmtIndex+1]=str; //insert the string to suitable position
filled_key_amount = filled_key_amount+1;
}
else //when current node is a non leaf node
{
while (rmtIndex >= 0 && keys_array[rmtIndex] > val) //search the child who is going to get the new key
{
rmtIndex--;
}
if (child_pointer_array[rmtIndex+1]->filled_key_amount == 2*min_degree-1) //if that child is full
{
split(rmtIndex+1, child_pointer_array[rmtIndex+1]); //split the node
if (keys_array[rmtIndex+1] < val)
{
rmtIndex++;
}
}
child_pointer_array[rmtIndex+1]->non_filled_node_insertion(val,str);
}
}
//used to search a key and returns the address of the particular node it belongs
BTreeNode *BTreeNode::search(int val)
{
int i = 0;
while (i < filled_key_amount && val > keys_array[i])
{
i++;
}
if (keys_array[i] == val) //key is found and exit
{
return this;
}
if (L_bit == 1) //key is not found and exit
{
return NULL;
}
return child_pointer_array[i]->search(val); //keep searching
}
//used to delete a key (utility)
void BTreeNode::Deletion(int val)
{
int index = FindTheKey(val);
if ((index < filled_key_amount) && (keys_array[index] == val)) //key is in current node
{
if (L_bit==1) //if current node is a leaf
{
int i;
for (i=index+1; i<filled_key_amount; i++) //keys those are located after the index is moved one position back
{
keys_array[i-1] = keys_array[i];
data[i-1]=data[i];
}
filled_key_amount=filled_key_amount-1; //update the key count
}
else //if current node is not a leaf
{
int val = keys_array[index];
if ((child_pointer_array[index+1]->filled_key_amount) >= min_degree) //leftmost
{
BTreeNode *current = child_pointer_array[index+1];
//get the left most value of the tree
while (!current->L_bit)
{
current = current->child_pointer_array[0];
}
int s=current->keys_array[0]; //add the first key of the leaf node to s
string s1=current->data[0];
data[index]=s1; //replacing
keys_array[index] = s;
child_pointer_array[index+1]->Deletion(s); //recursively delete
}
else if ((child_pointer_array[index]->filled_key_amount) >= min_degree) //rightmost
{
BTreeNode *current=child_pointer_array[index];
//get the right most value of the tree
while (!current->L_bit)
{
current = current->child_pointer_array[current->filled_key_amount];
}
int p= current->keys_array[current->filled_key_amount-1]; //add the first key of the leaf node to p
string s2=current->data[current->filled_key_amount-1];
data[index]=s2;
keys_array[index]=p; //replacing
child_pointer_array[index]->Deletion(p); //recursively delete
}
else //both of the children has less than min_degree keys
{
merge(index);
child_pointer_array[index]->Deletion(val);
}
}
}
else //key is not in the current node
{
if (L_bit==1) //if this is a leaf
{
cout<<"key is not present in the tree"<<endl;
return;
}
//if this is not a leaf
int flag;
if (index==filled_key_amount)
{
flag=1;
}
else
{
flag=0;
}
if ((child_pointer_array[index]->filled_key_amount) < min_degree) //child has less than minimumm keys
{
//fill the child
if (index!=0 && (child_pointer_array[index-1]->filled_key_amount)>=min_degree)
{
//borrow a key from previous child
BTreeNode *child2=child_pointer_array[index-1];
BTreeNode *child1=child_pointer_array[index];
int i;
for (i=child1->filled_key_amount-1; i>=0; i-=1)
{
child1->keys_array[i+1] = child1->keys_array[i];
child1->data[i+1] = child1->data[i];
}
if (!child1->L_bit)
{
for(i=child1->filled_key_amount; i>=0; i-=1)
{
child1->child_pointer_array[i+1] = child1->child_pointer_array[i];
}
}
child1->keys_array[0] = keys_array[index-1];
if(!child1->L_bit)
{
child1->child_pointer_array[0] = child2->child_pointer_array[child2->filled_key_amount];
}
keys_array[index-1] = child2->keys_array[child2->filled_key_amount-1];
data[index-1] = child2->data[child2->filled_key_amount-1];
child2->filled_key_amount--;
child1->filled_key_amount++;
}
else if (index!=filled_key_amount && child_pointer_array[index+1]->filled_key_amount>=min_degree)
{
//borrow a key from next child
BTreeNode *child2=child_pointer_array[index+1];
BTreeNode *child1=child_pointer_array[index];
child1->keys_array[(child1->filled_key_amount)] = keys_array[index];
child1->data[(child1->filled_key_amount)] = data[index];
if (!(child1->L_bit))
{
child1->child_pointer_array[(child1->filled_key_amount)+1] = child2->child_pointer_array[0];
}
keys_array[index] = child2->keys_array[0];
data[index] = child2->data[0];
for (int i=1; i<child2->filled_key_amount; ++i)
{
child2->keys_array[i-1] = child2->keys_array[i];
child2->data[i-1] = child2->data[i];
}
if (!child2->L_bit)
{
for(int i=1; i<=child2->filled_key_amount; ++i)
{
child2->child_pointer_array[i-1] = child2->child_pointer_array[i];
}
}
child2->filled_key_amount --;
child1->filled_key_amount ++;
}
else
{
if (index != filled_key_amount)
{
merge(index);
}
else
{
merge(index-1);
}
}
}
if ((flag && index) <= filled_key_amount)
{
child_pointer_array[index]->Deletion(val);
}
else
{
child_pointer_array[index-1]->Deletion(val);
}
}
}
//used to delete a key (main)
void BTree::Deletion(int val)
{
if (!root)
{
cout << "empty tree"<<endl;
return;
}
root->Deletion(val);
if (root->filled_key_amount==0)
{
BTreeNode *temp = root;
if (root->L_bit)
{
root = NULL;
}
else
root = root->child_pointer_array[0];
delete temp;
}
}
struct metaNode //to keep metadata of files
{
int ID; //starting ID of the file
int numOfBlocks; //number of blocks needed to store the file
string name;
int size;
long int checksum;
bool modified_bit;
};
static long int id_val=0; //initialize the static variable
vector<metaNode> metaList;
struct CoW_list_Node //CoW list (contains the updated file's data)
{
string modifiedFile;
int ID;
long int checksum;
};
vector<CoW_list_Node> CoW_List_Disk1; // disk 1 CoW list
vector<CoW_list_Node> CoW_List_Disk2; // disk 2 CoW list
BTree disk_1(5); //simulates the disk 1
BTree disk_2(5); //simulates the disk 2
//to create a text file and store it on Disks (B Trees)
void createFile()
{
/* algo
1.get the user input to a string
2.update metadata vector
3.store it block by block in both trees (RAID level 1)
*/
//get the input
string name;
while(1) //repeat until we get a valid name from the user
{
int flag=0;
cout<<"Enter the file name: ";
cin.ignore();
getline(cin, name);
//there cannot be two files with the same name
//search metadata vector for validity of the name
int var;
for (int i=0;i<metaList.size();i++)
{
var=name.compare(metaList[i].name);
if(!var)
{
cout<<endl<<"invalid name! The name you entered is already in use. please enter another name. "<<endl<<endl;
flag=1;
break;
}
}
if(!flag) //to check whether we need to repeat or not
{
break;
}
}
string text;
cout<<endl<<" -------- Enter the text file content-------- "<<endl<<endl;
getline(cin, text);
//update metadata vector
id_val++;
struct metaNode temp;
temp.ID=id_val;
temp.modified_bit=0;
temp.name=name;
int i=0,c_sum=0;
while(text[i]) //calculate check sum
{
c_sum+=text[i];
i++;
}
temp.checksum=c_sum;
double num_of_blocks=ceil(i/8.0); //to get the number of blocks needed
temp.numOfBlocks=num_of_blocks;
temp.size=num_of_blocks*8;
metaList.push_back(temp);
//store the file in both B trees (RAID level 1)
int remaining_characters=i%8; //get the charaters of the last block
string subStr;
int index=0;
//to split the text string to b byte sized substrings
for(int i=1;i<num_of_blocks;i++)
{
subStr = text.substr(index,8);
disk_1.insert(id_val,subStr); //insert the data to disk 1
disk_2.insert(id_val,subStr); //insert the data to disk 2
index+=8;
id_val++;
}
subStr=text.substr(index,remaining_characters);
disk_1.insert(id_val,subStr); //insert the last block of data to disk 1
disk_2.insert(id_val,subStr); //insert the last block of data to disk 2
}
//to modify a text file and store it.(CoW is used)
void modifyFile()
{
/*
algorithm
1.get the file name and , make modified bit 1 for that file
2.display the file to user and input the modified file (ask to copy paste it and modify)
3.get the modified string and store it to CoW_list vector for both disks (file id and the string)
note -
original file is there always unless you modify that file again or you delete it.
*/
string fName,modifiedText;
int file_index;
int flag=0;
while(1) //to get the file name from user
{
cout<<"Enter the name of the file you want to modify: ";
cin.ignore();
getline(cin, fName);
for(int i=0;i<metaList.size();i++) //find the corresponding file
{
if(!((metaList[i].name).compare(fName)))
{
cout<<"Copy and paste this file content to your editor space and modify it. press enter to save the modified text: "<<endl<<endl;
//get the file from the tree
string text; //we recollect the file to this string from tree blocks
BTreeNode *tempB;
int val=metaList[i].ID; //val is key
for(int h=0;h<metaList[i].numOfBlocks;h++)
{
tempB=disk_1.search(val);
for(int l=0;l<tempB->filled_key_amount;l++)
{
if(tempB->keys_array[l]==val)
{
text+=tempB->data[l];
break;
}
}
val++;
}
cout<<"Content Of the File: "<<text<<endl<<endl;
getline(cin, modifiedText); //get the modified text
metaList[i].modified_bit=1; //update the modified bit in metadata list
//pushing the modified file and its ID to Cow list vector
struct CoW_list_Node temp;
temp.ID=metaList[i].ID;
temp.modifiedFile=modifiedText;
int i=0,c_sum=0;
while(modifiedText[i]) //calculate check sum
{
c_sum+=modifiedText[i];
i++;
}
temp.checksum=c_sum;
CoW_List_Disk1.push_back(temp); //disk 1
CoW_List_Disk2.push_back(temp); //disk 2
flag=1;
break;
}
}
if(!flag)
{
cout<<"File name is invalid, please try again."<<endl;
}
else
{
break;
}
}
}
//to list all the available files
void listFiles()
{
/* idea is to list file names and sizes
Algorithm
1.if modified bit==1 then
don't print it
print relevant data from CoW - update file id accordingly
print the relevant data fromm metadata vector
*/
for(int i=0;i<metaList.size();i++)
{
if(metaList[i].modified_bit) //if file is modified
{
//access the CoW list to get that particular data
for(int j=0;j<CoW_List_Disk1.size();j++) //iterate through CoW List
{
if(CoW_List_Disk1[j].ID==metaList[i].ID) //find the CoW node for relvant ID
{
cout<<"File Name: "<<metaList[i].name<<" | ";
//calculate the size of the string
string tempS=CoW_List_Disk1[j].modifiedFile;
int count=0;
while(tempS[count]){count++;} //calculate number of characters
double num_of_blocks=ceil(count/8.0); //to get the number of blocks needed
int size_temp=num_of_blocks*8;
cout<<"File Size: "<<size_temp<<" Bytes"<<endl;
break; //loop is not needed beyond this point
}
}
continue; //to skip this value
}
cout<<"File Name: "<<metaList[i].name<<" | ";
cout<<"File Size: "<<metaList[i].size<<" Bytes"<<endl;
}
}
//to delete a text file
void deleteFile()
{
/*
algorithm
1.get the file name from the user
2.if file is modified then
remove from both CoW Lists
3.remove corresponding data from both b trees
4.remove metadata.
*/
string fName;
int flag=0;
while(1) //to get the file name from user
{
cout<<"Enter the name of the file you want to delete: ";
cin.ignore();
getline(cin, fName);
for(int i=0;i<metaList.size();i++) //find the corresponding file
{
if(!((metaList[i].name).compare(fName))) //file is there.!
{
if(metaList[i].modified_bit) //if the file is already modified
{
//get the files vector index
for (int j=0;j<CoW_List_Disk1.size();j++) //iterate through the CoW vector
{
if(CoW_List_Disk1[j].ID==metaList[i].ID) //find the corresponding file's CoW index
{
CoW_List_Disk1.erase(CoW_List_Disk1.begin() + j); //delete the record from disk 1 COW list
CoW_List_Disk2.erase(CoW_List_Disk2.begin() + j); //delete the record from disk 2 COW list
}
}
}
//remove data from B trees
disk_1.Deletion(metaList[i].ID); //from disk 1
disk_2.Deletion(metaList[i].ID); //from disk 2
//remove metadata
metaList.erase(metaList.begin() + i);
flag=1;
break;
}
}
if(!flag)
{
cout<<"File name is invalid, please try again. "<<endl;
}
else
{
break;
}
}
}
//to create a duplicate of a file
void copyFile()
{
/* idea is to give the copy a different name and a different ID but same content
algortihm
1.select the correct file
2.give a different name and id to the copy
3.if(file is a modified one) then
get the CoW string and make a copy of it
store that copy to both B Trees
update metadata list (add new copy's information to the metadata list)
else
get the file from B Tree and make a copy
store that copy to both B Trees
update metadata list (add new copy's information to the metadata list)
*/
string fName,tempName;
int flag=0;
while(1) //to get the file name from user
{
cout<<"Enter the name of the file you want to make a copy: ";
cin.ignore();
getline(cin, fName);
for(int i=0;i<metaList.size();i++) //find the corresponding file
{
if(!((metaList[i].name).compare(fName))) //file found.!
{
if(metaList[i].modified_bit) //if the file is already modified
{
//get the file's index from CoW List
for (int j=0;j<CoW_List_Disk1.size();j++) //iterate through the CoW vector
{
if(CoW_List_Disk1[j].ID==metaList[i].ID) //find the corresponding file's CoW index
{
int temp_id=CoW_List_Disk1[j].ID;
tempName=metaList[temp_id-1].name;
tempName=tempName+"_copy"; //name of that copy
//inserting the file to tree
string text=CoW_List_Disk1[j].modifiedFile; //file content
//update metadata vector
id_val++;
struct metaNode temp;
temp.ID=id_val;
temp.modified_bit=0;
temp.name=tempName;
int i=0,c_sum=0;
while(text[i]) //calculate check sum
{
c_sum+=text[i];
i++;
}
temp.checksum=c_sum;
double num_of_blocks=ceil(i/8.0); //to get the number of blocks needed
temp.numOfBlocks=num_of_blocks;
temp.size=num_of_blocks*8;
metaList.push_back(temp);
//store the file in both B trees (RAID level 1)
int remaining_characters=i%8; //get the charaters of the last block
string subStr;
int index=0;
//to split the text string to b byte sized substrings
for(int i=1;i<num_of_blocks;i++)
{
subStr = text.substr(index,8);
disk_1.insert(id_val,subStr); //insert the data to disk 1
disk_2.insert(id_val,subStr); //insert the data to disk 2
index+=8;
id_val++;
}
subStr=text.substr(index,remaining_characters);
disk_1.insert(id_val,subStr); //insert the last block of data to disk 1
disk_2.insert(id_val,subStr); //insert the last block of data to disk 2
}
}
}
else //not a modified file
{
tempName=metaList[i].name;
tempName=tempName+"_copy"; //name of that copy
//get the file from the tree
string text; //we recollect the file to this string from tree blocks
BTreeNode *tempB;
int val=metaList[i].ID; //val is key
for(int h=0;h<metaList[i].numOfBlocks;h++)
{
tempB=disk_1.search(val);
for(int l=0;l<tempB->filled_key_amount;l++)
{
if(tempB->keys_array[l]==val)
{
text+=tempB->data[l];
break;
}
}
val++;
}
//update metadata vector
id_val++;
struct metaNode temp;
temp.ID=id_val;
temp.modified_bit=0;
temp.name=tempName;
int i=0,c_sum=0;
while(text[i]) //calculate check sum
{
c_sum+=text[i];
i++;
}
temp.checksum=c_sum;
double num_of_blocks=ceil(i/8.0); //to get the number of blocks needed
temp.numOfBlocks=num_of_blocks;
temp.size=num_of_blocks*8;
metaList.push_back(temp);
//store the file in both B trees (RAID level 1)
int remaining_characters=i%8; //get the charaters of the last block
string subStr;
int index=0;
//to split the text string to b byte sized substrings
for(int i=1;i<num_of_blocks;i++)
{
subStr = text.substr(index,8);
disk_1.insert(id_val,subStr); //insert the data to disk 1
disk_2.insert(id_val,subStr); //insert the data to disk 2
index+=8;
id_val++;
}
subStr=text.substr(index,remaining_characters);
disk_1.insert(id_val,subStr); //insert the last block of data to disk 1
disk_2.insert(id_val,subStr); //insert the last block of data to disk 2
}
flag=1;
break;
}
}
if(!flag)
{
cout<<"File name is invalid, please try again."<<endl;
}
else
{
break;
}
}
}
//to view the content of a file
void viewFile()
{
/*
algorithm
1.select the corresponding file
2.get both file strings and calculate checksums and store those two values in temp varibles
3.IF (file is a modified one) then
{ get two original checksums from the CoW lists (actually getting one is enough both checksums are same, stored at same time.)
calculate two saved CoW string's checksums
if(original checksum 1 and calculated disk 1's checksum doesn't match)
if(original checksum 1 and calculated disk 2's checksum doesn't match) //both files are corrupted
error (both files are corrupted)
else //only file 1 corrupted
//UPDATE DISK 1 string
view disk2 file
else
if(original checksum 1 and calculated disk 2's checksum doesn't match) //only file 2 corrupted
//UPDATE DISK 2 string
view disk 1 file
else //both files are okay
view disk 1,2 file //it can be either 1 or 2
}
ELSE
{
get the original checksum from the metadata list
if(original checksum and calculated disk 1's checksum doesn't match)
if(original checksum and calculated disk 2's checksum doesn't match) //both files are corrupted
error (both files are corrupted)
else //only file 1 corrupted
//UPDATE DISK 1 string
view disk2 file
else
if(original checksum and calculated disk 2's checksum doesn't match) //only file 2 corrupted
//UPDATE DISK 2 string
view disk 1 file
else //both files are okay
view disk 1,2 file //it can be either 1 or 2
}
*/
//get the input
string fName;
int flag=0;