-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk.cpp
1127 lines (1000 loc) · 38.1 KB
/
disk.cpp
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 "disk.h"
SuperBlock createDisk(char name[],double size_disk, int size_block){
SuperBlock SP;
double disksizebyte = size_disk*1024*1024;//*1024;
char block[size_block];
double cantofblock = disksizebyte / size_block;
//cout<<"Writing Blocks # "<<cantofblock << endl;
memset(block,'0',size_block);
string path = "VirtualDisks/"; //[28]
string name2 = name;
string format = ".data";
string name_file = path + name2 + format;
//strcpy(name_file,name);
//strcat(name_file,".data");
ofstream output_file(name_file.c_str(),ios::binary);
for(int i=0;i<cantofblock;i++){
output_file.write((char*)block,sizeof(block));
}
output_file.close();
output_file.open(name_file.c_str(), ios::in | ios::out | ios::binary);
SP.cantofblock = cantofblock;
SP.cantofinode = (disksizebyte*0.02)/sizeof(Inode);
int bitmapsize = cantofblock/8;
char bitmap [bitmapsize];
int bitmapsize_inodes = (SP.cantofinode)/8;
char bitmap_inodes [bitmapsize_inodes];
int inodes_size = sizeof(Inode);
int FileData_size = sizeof(FileData);
double total_size_fs = sizeof(SuperBlock) + sizeof(bitmap) + sizeof(bitmap_inodes) + inodes_size*(SP.cantofinode)
+ FileData_size*(SP.cantofinode);
double FS_blocks_WR = total_size_fs/size_block;
int FS_blocks = ceil(FS_blocks_WR);
SP.freeblock = cantofblock - FS_blocks;
SP.freeinode = SP.cantofinode - 1;
SP.freespace = disksizebyte - total_size_fs;//sizeof(SuperBlock);
strcpy(SP.name, name);
SP.size = disksizebyte;
SP.sizeofblock = size_block;
SP.FS_Blocks = FS_blocks;
strcpy(SP.endsb, "FIN");
//cout<<"writing Super Block with size: "<<sizeof(SuperBlock)<<endl;
//cout<<"cant blocks: "<<SP.cantofblock<<endl;
//cout<<"cant free-blocks: "<<SP.freeblock<<endl;
//cout<<"cant inodes: "<<SP.cantofinode<<endl;
//cout<<"inode_size: "<<inodes_size<<endl;
//cout<<"FileData_size: "<<FileData_size<<endl;
//cout<<"free Space: "<<SP.freespace<<endl;
//cout<<"FS_blocks: "<<FS_blocks<<endl;
output_file.write((char*)&SP,sizeof(SuperBlock));
// int bitmapsize = cantofblock/8;
// char bitmap [bitmapsize];
memset(bitmap,0,bitmapsize);
//cout<<"writing Bitmap"<<endl;
//seteando los bloques usados por el FS al bitmap
for (int i = 0; i < FS_blocks; ++i) {
setBlock_use(bitmap,i);
}
output_file.write(((char*)bitmap),sizeof(bitmap));
//output_file.write("end bm",6);
// int bitmapsize_inodes = SP.cantofinode/8;
// char bitmap_inodes [bitmapsize_inodes];
memset(bitmap_inodes,0,bitmapsize_inodes);
setBlock_use(bitmap_inodes,0); // seteando el primer inodo que sera usado para el root
//cout<<"writing Bitmap Inodes"<<endl;
output_file.write(((char*)bitmap_inodes),sizeof(bitmap_inodes));
//output_file.write("end bmi",7);
FileData root;
//setendo el primer FilaData que pertenece al root
strcpy(root.name,"/");
root.index_file = 0;
output_file.write(((char*)&root),FileData_size);
FileData file;
file.index_file = -1;
// strcpy(file.name,"");
memset(file.name,0,sizeof(char)*50); // 50 caracteres maximo para el nombre
//cout<<"writing File'sData of size: "<<FileData_size*SP.cantofinode<<endl;
for(int d =1;d<SP.cantofinode;d++){
output_file.write(((char*)&file),FileData_size);
// output_file.write("ENDFD",5);
}
Inode root_inodo;
root_inodo.filesize = 0;
root_inodo.blockuse = 0;
// memset(root_inodo.directos,-1,sizeof(int)*10);
for (int i = 0; i < 10; ++i) {
(root_inodo.directos)[i] = -1;
}
root_inodo.indirectossimples = -1;
root_inodo.indirectosdobles = -1;
root_inodo.indirectostriples = -1;
root_inodo.lastDataBlock = -1;
strcpy(root_inodo.permisos,"drwxrwxrwx");
output_file.write(((char*)&root_inodo),inodes_size);
Inode inodes;
inodes.filesize = -1;
inodes.blockuse = -1;
// memset(inodes.directos,-1,sizeof(double)*10); //10 inodos directos
for (int i = 0; i < 10; ++i) {
(inodes.directos)[i] = -1;
}
inodes.indirectossimples = -1;
inodes.indirectosdobles = -1;
inodes.indirectostriples = -1;
inodes.lastDataBlock = -1;
strcpy(inodes.permisos,"----------");
//cout<<"writing Inodes of size: "<<inodes_size*SP.cantofinode<<endl;
for(int d =1;d<SP.cantofinode;d++){
output_file.write(((char*)&inodes),inodes_size);
// output_file.write("ENDIN",5);
}
//cout<<"Total Size of FS: "<<total_size_fs<<endl;
output_file.close();
return SP;
}
bool is_block_in_use(char* bitmap, int blocknum)
{
int indexonbitmap = blocknum / 8;
int posinchar = blocknum%8;
if(bitmap[indexonbitmap] & 1<<posinchar){
return true;
}else{
return false;
}
}
void setBlock_use(char* bitmap, int blocknum){
int indexonbitmap = blocknum / 8;
int posinchar = blocknum%8;
bitmap[indexonbitmap] = bitmap[indexonbitmap] | 1 << posinchar;
}
void setBlock_unuse(char* bitmap, int blocknum){
int indexonbitmap = blocknum / 8;
int posinchar = blocknum%8;
bitmap[indexonbitmap]= bitmap[indexonbitmap] & ~(1<<posinchar);
}
double getNextFreeBlock(char *bitmap, double size_bitmap)
{
for (int i = 0; i < size_bitmap; ++i) {
if (!is_block_in_use(bitmap,i)) {
setBlock_use(bitmap,i);
return i;
}
}
return -1;
}
vector<double> getTotalBlocksToUse(double file_size, int block_size)
{
vector<double> T_blocks;
int x = block_size/8;
double blocks_data = ceil(file_size/block_size);
double extra_blocks = 0;
if(blocks_data <= 10){ //case: 0
extra_blocks = 0;
T_blocks.push_back(0);
T_blocks.push_back(ceil(file_size/block_size)); //cuantos bloques de data los Directos simples uso? index: 1
}else if(blocks_data <= (10 + x)){ //case: 1
extra_blocks = 1;
T_blocks.push_back(1);
T_blocks.push_back(blocks_data-10); //cuantos bloques de data va a usar el IS? index: 1
}else if(blocks_data <= (10 + x + pow(x,2))){ //case: 2
double blocks_temp = blocks_data - (10 + x);
extra_blocks = ceil(blocks_temp/x) + 2;
T_blocks.push_back(2);
T_blocks.push_back(x); //cuantos bloques de data va a usar el IS? index: 1
T_blocks.push_back(ceil(blocks_temp/x)); //cuantos IS va a usar el ID? index: 2
T_blocks.push_back(blocks_temp); //cuantos bloques de data van a usar los IS del ID? index: 3
}else if(blocks_data <= (10 + x + pow(x,2) + pow(x,3))){ //case: 3
double blocks_temp = blocks_data - (10 + x + pow(x,2));
extra_blocks = ceil(blocks_temp/(pow(x,2))) + ceil(blocks_temp/x) + 3 + x;//(blocks_temp*(1+x))/(pow(x,2)) + 3 + x;//blocks_temp/(pow(x,2)) + blocks_temp/x + 3;
T_blocks.push_back(3);
T_blocks.push_back(x); //cuantos bloques de data va a usar el IS? index: 1
T_blocks.push_back(x); //cuantos IS va a usar el ID? index: 2
T_blocks.push_back(pow(x,2)); //cuantos bloques de data van a usar los IS del ID? index: 3
T_blocks.push_back(ceil(blocks_temp/(pow(x,2)))); //cuantos ID va usar el IT? index: 4
T_blocks.push_back(ceil(blocks_temp/x)); //cuantos IS van a usar los ID del IT? index: 5
T_blocks.push_back(blocks_temp); //cuantos bloques de data van a usar los Is de los ID del IT? index: 6
}else{
// return -1;
T_blocks.push_back(-1);
}
// return ceil(blocks_data + extra_blocks);
T_blocks.push_back(ceil(blocks_data + extra_blocks));
return T_blocks;
}
void write(string disk_name, char *buffer, double start, double bytes_to_write)
{
ofstream out(disk_name.c_str(), ios::in | ios::out | ios::binary);
out.seekp(start);
out.write(buffer,bytes_to_write);
out.close();
}
void read(string disk_name, char *buffer, double start, double bytes_to_read)
{
ifstream in(disk_name.c_str(), ios::in | ios::out | ios::binary);
in.seekg(start);
in.read(buffer,bytes_to_read);
in.close();
}
vector<double> getFreeBlocks(char *bitmap, double size_bitmap, double cant_of_blocks)
{
vector<double> blocks;
for (double i = 0; i < size_bitmap; ++i) {
if(cant_of_blocks==0) break;
if(!is_block_in_use(bitmap,i))
{
blocks.push_back(i);
cant_of_blocks--;
setBlock_use(bitmap,i); // lo cambiamos a usado
}
}
if(blocks.size() < cant_of_blocks)
{
blocks.clear();
blocks.push_back(-1);
}
return blocks;
}
vector<double> getFreeSequentialBlocks(char *bitmap, double size_bitmap, double cant_of_blocks)
{
vector<double> blocks;
double T_blocks = cant_of_blocks; // Total de bloques a buscar
double start=0; //comienza a buscar en 0
while (T_blocks>0) { //mientras el total de bloques a buscar sea mayor que cero, sequira buscando
bool start_encontrado=false;
//recorremos el mapa de bits hasta encontrar el primer bloque libre
for (double i = start; i < size_bitmap; ++i) {
if(!is_block_in_use(bitmap,i)) //si el bloque no esta usado
{
start = i; //seteamos el comienzo en ese bloque
start_encontrado = true;
break;
}
}
if(start_encontrado)
{
//verificamos si la catidad de bloques pedida esta disponible secuencialmente desde el comienzo
//que encontramos prbiamente.
for (int i = 0; i < cant_of_blocks; ++i) {
if(!is_block_in_use(bitmap,start + i)) //si no esta usado
{
blocks.push_back(start + i); //lo vamos agregando a la lista
T_blocks--; //y lo restamos de la cantidad de bloques a buscar
}else{
start = start + i; // cambiamos el comienzo para volver a buscar el siguente primer bloque libre a partir de alli
T_blocks = cant_of_blocks; //volvemos a setear el total de bloques = cantidad de bloques a buscar
blocks.clear(); //limpiamos la lista de los bloques que teniamos hasta el momento
break; //paramos el ciclo para que todo comience de nuevo
}
}
}else{
blocks.clear(); //limpiamos la lista por si habian datos guardados
blocks.push_back(-1); //seteamos el primer valor: -1 para saber que no se pudo encontrar una secuencia de bloques libres de la cantidad especificada
break;
}
}
return blocks;
}
void set_blocks_in_use(char *bitmap, vector<int> blocks)
{
for (int i = 0; i < blocks.size(); ++i) {
setBlock_use(bitmap,blocks[i]);
}
}
void set_blocks_in_unuse(char *bitmap, vector<double> blocks)
{
for (int i = 0; i < blocks.size(); ++i) {
setBlock_unuse(bitmap,blocks[i]);
}
}
void writeInodesBlocks(string disk_name, vector<double> data_index, vector<double> inodes_index, vector<double> how_many, int size_block, Inode *inode, double start)
{
double iterate = 10;
int x = size_block/8;
double *IS = new double[x];
double *ID = new double[x];
double *ID_IS = new double[x];
double *IT = new double[x];
double *IT_ID = new double[x];
double *IT_ID_IS = new double[x];
for (int i = 0; i < x; ++i) {
IS[i] = -1;
ID[i] = -1;
ID_IS[i] = -1;
IT[i] = -1;
IT_ID[i] = -1;
IT_ID_IS[i] = -1;
}
// memset(IS,-1,size_block);
// memset(ID,-1,size_block);
// memset(ID_IS,-1,size_block);
// memset(IT,-1,size_block);
// memset(IT_ID,-1,size_block);
// memset(IT_ID_IS,-1,size_block);
if(how_many[0]>=1) //IS
{
inode->indirectossimples = inodes_index[0];
//cout<<"IS en: "<<inode->indirectossimples<<endl;
for (int i = 0; i < how_many[1]; ++i) {
IS[i] = data_index[iterate++];
}
write(disk_name,(char*)IS,inode->indirectossimples*size_block,size_block);
inodes_index.erase(inodes_index.begin()); //borramos del vector el que ya fue usado
}
if(how_many[0]>=2) //ID
{
inode->indirectosdobles = inodes_index[0];
//cout<<"ID en: "<<inode->indirectosdobles<<endl;
inodes_index.erase(inodes_index.begin());
//cout<<"seteando IS del ID en: "<<endl;
for (int i = 0; i < how_many[2]; ++i) {
ID[i] = inodes_index[i];
//cout<<"- "<<ID[i]<<endl;
}
for (int i = 0; i < how_many[2]; ++i) {
inodes_index.erase(inodes_index.begin());
}
int d_b = how_many[3];
for (int j = 0; j < how_many[2]; ++j) {
memset(ID_IS,-1,x);
//cout<<"seteando data para IS: "<<j<<endl;
for (int i = 0; i < x; ++i) {
if(d_b>0)
{
ID_IS[i] = data_index[iterate++];
d_b--;
//cout<<"- "<<ID_IS[i]<<endl;
}else{
// break;
ID_IS[i] = -1;
}
}
write(disk_name,(char*)ID_IS,ID[j]*size_block,size_block);
}
write(disk_name,(char*)ID,(inode->indirectosdobles)*size_block,size_block);
}
if(how_many[0]>=3) //IT
{
inode->indirectostriples = inodes_index[0];
//cout<<"IT en: "<<inode->indirectostriples<<endl;
inodes_index.erase(inodes_index.begin());
//cout<<"seteando ID del IT en: "<<"cant: "<<how_many[4]<<endl;
for (int i = 0; i < how_many[4]; ++i) {
IT[i] = inodes_index[i];
//cout<<"- "<<IT[i]<<endl;
}
for (int i = 0; i < how_many[4]; ++i) {
inodes_index.erase(inodes_index.begin());
}
int is_b = how_many[5];
int d_b = how_many[6];
for (int j = 0; j < how_many[4]; ++j) {
memset(IT_ID,-1,x);
//cout<<"seteando IS de ID de IT: "<<j<<endl;
for (int i = 0; i < x; ++i) {
if(is_b>0)
{
IT_ID[i] = inodes_index[0];
is_b--;
//cout<<"- "<<IT_ID[i]<<endl;
inodes_index.erase(inodes_index.begin());
}else{
// break;
IT_ID[i] = -1;
}
memset(IT_ID_IS,-1,x);
//cout<<"seteando data para IS de ID de IT: "<<j<<endl;
for (int k = 0; k < x; ++k) {
if(d_b>0)
{
IT_ID_IS[k] = data_index[iterate++];
d_b--;
//cout<<"- "<<IT_ID_IS[k]<<endl;
}else{
// break;
IT_ID_IS[k] = -1;
}
}
write(disk_name,(char*)IT_ID_IS,IT_ID[i]*size_block,size_block);
}
write(disk_name,(char*)IT_ID,IT[j]*size_block,size_block);
}
write(disk_name,(char*)IT,inode->indirectostriples*size_block,size_block);
}
// Inode ino;
//lee IS
double *buf = new double[x];
read(disk_name,(char*)buf,inode->indirectossimples*size_block,size_block);
// memcpy(&ino,buf,size_block);
if(inode->indirectossimples!=-1)
{
//cout<<"Leido del Disco IS en!: "<<inode->indirectossimples<<endl;
for (int i = 0; i < x; ++i) {
//cout<<"data en- "<<buf[i]<<endl;
// if(buf[i]==-1) //cout<<"-nan = -1"<<endl;
}
}
//lee ID
read(disk_name,(char*)buf,inode->indirectosdobles*size_block,size_block);
// memcpy(&ino,buf,size_block);
//cout<<"Leido del Disco ID en!: "<<inode->indirectosdobles<<endl;
if(inode->indirectosdobles!=-1)
{
for (int i = 0; i < x; ++i) {
//cout<<"IS en- "<<buf[i]<<endl;
double *buf2 = new double[x];
read(disk_name,(char*)buf2,buf[i]*size_block,size_block);
// //cout<<"IS del ID"<<endl;
if(buf[i]!=-1)
{
for (int j = 0; j < x; ++j) {
//cout<<"data en:- "<<buf2[j]<<endl;
}
}
}
}
//lee IT
read(disk_name,(char*)buf,inode->indirectostriples*size_block,size_block);
// memcpy(&ino,buf,size_block);
//cout<<"Leido del Disco IT en!: "<<inode->indirectostriples<<endl;
if(inode->indirectostriples!=-1)
{
for (int i = 0; i < x; ++i) {
//cout<<"ID en- "<<buf[i]<<endl;
double *buf2 = new double[x];
read(disk_name,(char*)buf2,buf[i]*size_block,size_block);
// //cout<<"ID del IT"<<endl;
if(buf[i]!=-1)
{
for (int j = 0; j < x; ++j) {
//cout<<"IS en- "<<buf2[j]<<endl;
double *buf3 = new double[x];
read(disk_name,(char*)buf3,buf2[j]*size_block,size_block);
// //cout<<"IS del ID del IT"<<endl;
if(buf2[j]!=-1)
{
for (int k = 0; k < x; ++k) {
//cout<<"data en- "<<buf3[k]<<endl;
}
}
}
}
}
}
}
void memcpybuffer(char *&dest, char *src, int sizeblock, double init , double size_src)
{
memset(dest,0,sizeblock*sizeof(char));
for(int i =0; i< sizeblock;i++){
if(size_src==init+i)
return;
dest[i]=src[(int)(init+i)];
}
}
void memtransbuffer(char *&dest, char *src, double init, double size_src)
{
for (int i = 0; i < size_src; ++i) {
dest[(int)(init++)] = src[i];
}
}
void readDataBlocksFrom(string disk,char *&buffer, Inode *inode, double sizeblock)
{
buffer = new char[(int)inode->filesize];
double sizefile = inode->filesize;
double size_to_write = sizeblock;
int iterate = 0;
//cout<<"Directos en: "<<endl;
for (int i = 0; i < 10; ++i) {
//cout<<i<<"- "<<(inode->directos)[i]<<endl;
if(sizefile<size_to_write)
size_to_write = sizefile;
sizefile-=size_to_write;
if((inode->directos)[i]!=-1)
{
char dataBlock[(int)sizeblock];
read(disk,(char*)&dataBlock,(inode->directos)[i]*sizeblock,size_to_write);
memtransbuffer(buffer,dataBlock,iterate,size_to_write);
iterate+=size_to_write;
//cout<<"iterate: "<<iterate<<endl;
}else{
return;
}
}
//lee IS
int x = sizeblock/8;
double *buf = new double[x];
read(disk,(char*)buf,inode->indirectossimples*sizeblock,sizeblock);
if(inode->indirectossimples!=-1)
{
//cout<<"Leido del Disco IS en!: "<<inode->indirectossimples<<endl;
for (int i = 0; i < x; ++i) {
//cout<<"data en- "<<buf[i]<<endl;
if(sizefile<size_to_write)
size_to_write = sizefile;
sizefile-=size_to_write;
if(buf[i]!=-1)
{
char *dataBlock = new char[(int)sizeblock];
read(disk,dataBlock,buf[i]*sizeblock,size_to_write);
memtransbuffer(buffer,dataBlock,iterate,size_to_write);
iterate+=size_to_write;
//cout<<"iterate: "<<iterate<<endl;
}else{
return;
}
}
}
//lee ID
read(disk,(char*)buf,inode->indirectosdobles*sizeblock,sizeblock);
//cout<<"Leido del Disco ID en!: "<<inode->indirectosdobles<<endl;
if(inode->indirectosdobles!=-1)
{
for (int i = 0; i < x; ++i) {
//cout<<"IS en- "<<buf[i]<<endl;
double *buf2 = new double[x];
read(disk,(char*)buf2,buf[i]*sizeblock,sizeblock);
if(buf[i]!=-1)
{
for (int j = 0; j < x; ++j) {
//cout<<"data en:- "<<buf2[j]<<endl;
if(sizefile<size_to_write)
size_to_write = sizefile;
sizefile-=size_to_write;
if(buf2[j]!=-1)
{
char *dataBlock = new char[(int)sizeblock];
read(disk,dataBlock,buf2[j]*sizeblock,size_to_write);
memtransbuffer(buffer,dataBlock,iterate,size_to_write);
iterate+=size_to_write;
//cout<<"iterate: "<<iterate<<endl;
}else{
return;
}
}
}
}
}
//lee IT
read(disk,(char*)buf,inode->indirectostriples*sizeblock,sizeblock);
//cout<<"Leido del Disco IT en!: "<<inode->indirectostriples<<endl;
if(inode->indirectostriples!=-1)
{
for (int i = 0; i < x; ++i) {
//cout<<"ID en- "<<buf[i]<<endl;
double *buf2 = new double[x];
read(disk,(char*)buf2,buf[i]*sizeblock,sizeblock);
if(buf[i]!=-1)
{
for (int j = 0; j < x; ++j) {
//cout<<"IS en- "<<buf2[j]<<endl;
double *buf3 = new double[x];
read(disk,(char*)buf3,buf2[j]*sizeblock,sizeblock);
if(buf2[j]!=-1)
{
for (int k = 0; k < x; ++k) {
//cout<<"data en- "<<buf3[k]<<endl;
if(sizefile<size_to_write)
size_to_write = sizefile;
sizefile-=size_to_write;
if(buf3[k]!=-1)
{
char *dataBlock = new char[(int)sizeblock];
read(disk,dataBlock,buf3[k]*sizeblock,size_to_write);
memtransbuffer(buffer,dataBlock,iterate,size_to_write);
iterate+=size_to_write;
//cout<<"iterate: "<<iterate<<endl;
}else{
return;
}
}
}
}
}
}
}
}
void ExportFile(string disk, string file_to_export, Inode *inode, double sizeblock)
{
double sizefile = inode->filesize;
double size_to_write = sizeblock;
int iterate = 0;
ofstream out(file_to_export.c_str(),ios::binary);
//cout<<"Directos en: "<<endl;
for (int i = 0; i < 10; ++i) {
//cout<<i<<"- "<<(inode->directos)[i]<<endl;
if(sizefile<size_to_write)
size_to_write = sizefile;
sizefile-=size_to_write;
if((inode->directos)[i]!=-1)
{
char *dataBlock = new char[(int)sizeblock];
read(disk,dataBlock,(inode->directos)[i]*sizeblock,size_to_write);
out.write(dataBlock,size_to_write);
out.flush();
// write(file_to_export,dataBlock,iterate,size_to_write);
// memtransbuffer(buffer,dataBlock,iterate,size_to_write);
iterate+=size_to_write;
//cout<<"iterate: "<<iterate<<endl;
}else{
return;
}
}
//lee IS
int x = sizeblock/8;
double *buf = new double[x];
read(disk,(char*)buf,inode->indirectossimples*sizeblock,sizeblock);
if(inode->indirectossimples!=-1)
{
//cout<<"Leido del Disco IS en!: "<<inode->indirectossimples<<endl;
for (int i = 0; i < x; ++i) {
//cout<<"data en- "<<buf[i]<<endl;
if(sizefile<size_to_write)
size_to_write = sizefile;
sizefile-=size_to_write;
if(buf[i]!=-1)
{
char *dataBlock = new char[(int)sizeblock];
read(disk,dataBlock,buf[i]*sizeblock,size_to_write);
out.write(dataBlock,size_to_write);
out.flush();
// write(file_to_export,dataBlock,iterate,size_to_write);
// memtransbuffer(buffer,dataBlock,iterate,size_to_write);
iterate+=size_to_write;
//cout<<"iterate: "<<iterate<<endl;
}else{
return;
}
}
}
//lee ID
read(disk,(char*)buf,inode->indirectosdobles*sizeblock,sizeblock);
//cout<<"Leido del Disco ID en!: "<<inode->indirectosdobles<<endl;
if(inode->indirectosdobles!=-1)
{
for (int i = 0; i < x; ++i) {
//cout<<"IS en- "<<buf[i]<<endl;
double *buf2 = new double[x];
read(disk,(char*)buf2,buf[i]*sizeblock,sizeblock);
if(buf[i]!=-1)
{
for (int j = 0; j < x; ++j) {
//cout<<"data en:- "<<buf2[j]<<endl;
if(sizefile<size_to_write)
size_to_write = sizefile;
sizefile-=size_to_write;
if(buf2[j]!=-1)
{
char *dataBlock = new char[(int)sizeblock];
read(disk,dataBlock,buf2[j]*sizeblock,size_to_write);
out.write(dataBlock,size_to_write);
out.flush();
// write(file_to_export,dataBlock,iterate,size_to_write);
// memtransbuffer(buffer,dataBlock,iterate,size_to_write);
iterate+=size_to_write;
//cout<<"iterate: "<<iterate<<endl;
}else{
return;
}
}
}
}
}
//lee IT
read(disk,(char*)buf,inode->indirectostriples*sizeblock,sizeblock);
//cout<<"Leido del Disco IT en!: "<<inode->indirectostriples<<endl;
if(inode->indirectostriples!=-1)
{
for (int i = 0; i < x; ++i) {
//cout<<"ID en- "<<buf[i]<<endl;
double *buf2 = new double[x];
read(disk,(char*)buf2,buf[i]*sizeblock,sizeblock);
if(buf[i]!=-1)
{
for (int j = 0; j < x; ++j) {
//cout<<"IS en- "<<buf2[j]<<endl;
double *buf3 = new double[x];
read(disk,(char*)buf3,buf2[j]*sizeblock,sizeblock);
if(buf2[j]!=-1)
{
for (int k = 0; k < x; ++k) {
//cout<<"data en- "<<buf3[k]<<endl;
if(sizefile<size_to_write)
size_to_write = sizefile;
sizefile-=size_to_write;
if(buf3[k]!=-1)
{
char *dataBlock = new char[(int)sizeblock];
read(disk,dataBlock,buf3[k]*sizeblock,size_to_write);
out.write(dataBlock,size_to_write);
out.flush();
// write(file_to_export,dataBlock,iterate,size_to_write);
// memtransbuffer(buffer,dataBlock,iterate,size_to_write);
iterate+=size_to_write;
//cout<<"iterate: "<<iterate<<endl;
}else{
return;
}
}
}
}
}
}
}
out.close();
}
vector<FileData *> getFileTableFrom(Inode dir, char *buffer)
{
vector<FileData *> filetable;
int howMany = dir.filesize/(sizeof(FileData));
//cout<<"howMany: "<<howMany<<endl;
//cout<<"filesize: "<<dir.filesize<<endl;
//cout<<"sizeof(FileData): "<<sizeof(FileData)<<endl;
char *buf = new char[sizeof(FileData)];
for (int i = 0; i < howMany; ++i) {
FileData *fd = new FileData();
memset(buf,0,sizeof(FileData));
for (int j = i*sizeof(FileData),k=0; j < (i*sizeof(FileData) + sizeof(FileData)); ++j,k++) {
buf[k] = buffer[j];
}
memcpy(fd,buf,sizeof(FileData));
filetable.push_back(fd);
}
return filetable;
}
void copy(Inode *from, Inode *to)
{
strcpy(to->permisos,from->permisos);
to->blockuse = from->blockuse;
strcpy((char*)to->directos,(char*)from->directos);
to->filesize = from->filesize;
to->indirectossimples = from->indirectossimples;
to->indirectosdobles = from->indirectosdobles;
to->indirectostriples = from->indirectostriples;
to->lastDataBlock = from->lastDataBlock;
}
vector<double> getDataBlocksFrom(string disk, Inode *inode, int sizeblock)
{
vector<double> blocks;
double sizefile = inode->filesize;
double size_to_write = sizeblock;
int iterate = 0;
//cout<<"Directos en: "<<endl;
for (int i = 0; i < 10; ++i) {
//cout<<i<<"- "<<(inode->directos)[i]<<endl;
if((inode->directos)[i]!=-1)
blocks.push_back((inode->directos)[i]);
}
//lee IS
int x = sizeblock/8;
double buf[x];
read(disk,(char*)&buf,inode->indirectossimples*sizeblock,sizeblock);
if(inode->indirectossimples!=-1)
{
//cout<<"Leido del Disco IS en!: "<<inode->indirectossimples<<endl;
for (int i = 0; i < x; ++i) {
//cout<<"data en- "<<buf[i]<<endl;
if(buf[i]!=-1)
blocks.push_back(buf[i]);
}
}
//lee ID
read(disk,(char*)&buf,inode->indirectosdobles*sizeblock,sizeblock);
//cout<<"Leido del Disco ID en!: "<<inode->indirectosdobles<<endl;
if(inode->indirectosdobles!=-1)
{
for (int i = 0; i < x; ++i) {
//cout<<"IS en- "<<buf[i]<<endl;
double buf2[x];
read(disk,(char*)&buf2,buf[i]*sizeblock,sizeblock);
if(buf[i]!=-1)
{
for (int j = 0; j < x; ++j) {
//cout<<"data en:- "<<buf2[j]<<endl;
if(buf2[j]!=-1)
blocks.push_back(buf2[j]);
}
}
}
}
//lee IT
read(disk,(char*)&buf,inode->indirectostriples*sizeblock,sizeblock);
//cout<<"Leido del Disco IT en!: "<<inode->indirectostriples<<endl;
if(inode->indirectostriples!=-1)
{
for (int i = 0; i < x; ++i) {
//cout<<"ID en- "<<buf[i]<<endl;
double buf2[x];
read(disk,(char*)&buf2,buf[i]*sizeblock,sizeblock);
if(buf[i]!=-1)
{
for (int j = 0; j < x; ++j) {
//cout<<"IS en- "<<buf2[j]<<endl;
double buf3[x];
read(disk,(char*)&buf3,buf2[j]*sizeblock,sizeblock);
if(buf2[j]!=-1)
{
for (int k = 0; k < x; ++k) {
//cout<<"data en- "<<buf3[k]<<endl;
if(buf3[k]!=-1)
blocks.push_back(buf3[k]);
}
}
}
}
}
}
return blocks;
}
vector<double> getAllBlocksUsedFor(string disk, Inode *inode, int sizeblock)
{
vector<double> blocks;
//cout<<"Directos en: "<<endl;
for (int i = 0; i < 10; ++i) {
//cout<<i<<"- "<<(inode->directos)[i]<<endl;
if(inode->directos[i]!=-1)
blocks.push_back((inode->directos)[i]);
}
//lee IS
int x = sizeblock/8;
double buf[x];
read(disk,(char*)&buf,inode->indirectossimples*sizeblock,sizeblock);
if(inode->indirectossimples!=-1)
{
blocks.push_back(inode->indirectossimples);
//cout<<"Leido del Disco IS en!: "<<inode->indirectossimples<<endl;
for (int i = 0; i < x; ++i) {
//cout<<"data en- "<<buf[i]<<endl;
if(buf[i]!=-1)
blocks.push_back(buf[i]);
}
}
//lee ID
read(disk,(char*)&buf,inode->indirectosdobles*sizeblock,sizeblock);
//cout<<"Leido del Disco ID en!: "<<inode->indirectosdobles<<endl;
if(inode->indirectosdobles!=-1)
{
blocks.push_back(inode->indirectosdobles);
for (int i = 0; i < x; ++i) {
//cout<<"IS en- "<<buf[i]<<endl;
if(buf[i]!=-1)
{
blocks.push_back(buf[i]);
double buf2[x];
read(disk,(char*)&buf2,buf[i]*sizeblock,sizeblock);
for (int j = 0; j < x; ++j) {
//cout<<"data en:- "<<buf2[j]<<endl;
if(buf2[j]!=-1)
blocks.push_back(buf2[j]);
}
}
}
}
//lee IT
read(disk,(char*)buf,inode->indirectostriples*sizeblock,sizeblock);
//cout<<"Leido del Disco IT en!: "<<inode->indirectostriples<<endl;
if(inode->indirectostriples!=-1)
{
blocks.push_back(inode->indirectostriples);
for (int i = 0; i < x; ++i) {
//cout<<"ID en- "<<buf[i]<<endl;
double buf2[x];
read(disk,(char*)&buf2,buf[i]*sizeblock,sizeblock);
if(buf[i]!=-1)
{
blocks.push_back(buf[i]);
for (int j = 0; j < x; ++j) {
//cout<<"IS en- "<<buf2[j]<<endl;
double buf3[x];
read(disk,(char*)&buf3,buf2[j]*sizeblock,sizeblock);
if(buf2[j]!=-1)
{
blocks.push_back(buf2[j]);
for (int k = 0; k < x; ++k) {
//cout<<"data en- "<<buf3[k]<<endl;
if(buf3[k]!=-1)
blocks.push_back(buf3[k]);
}
}
}
}
}
}
return blocks;
}
vector<double> get_IBlocks_UsedFor(string disk, Inode *inode, int sizeblock)
{
vector<double> blocks;