-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_partitions.cpp
801 lines (756 loc) · 24.2 KB
/
handler_partitions.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
#include "handler_partitions.h"
#include <vars.h>
Response createPartition(int size, Unit unit, char path[],char nameDisk[],TypePartition TypePartition, Fit fit, char name[]){
char full_path[200];
clearArray(full_path,sizeof(full_path));
getFullPathDisk(path,nameDisk,full_path);
//ESCRIBIR ARCHIVO
Response response = newPartition(size,unit,full_path,TypePartition,fit,name);
if(response == SUCCESS){
char nameMirror[50];
clearArray(nameMirror,50);
strcat(nameMirror,nameDisk);
strcat(nameMirror,"_mirror");
clearArray(full_path,sizeof(full_path));
getFullPathDisk(path,nameMirror,full_path);
//ESCRIBIR RAID
//Response response1 =
newPartition(size,unit,full_path,TypePartition,fit,name);
//if(response1 != SUCCESS)cout<<"Error al hacer cambio en el raid\n";
}
return response;
}
Response updatepartition(char path[],char nameDisk[],char namePart[],int sizeToMod,Unit unit){
char full_path[200];
clearArray(full_path,sizeof(full_path));
getFullPathDisk(path,nameDisk,full_path);
//ESCRIBIR ARCHIVO
Response response = modPartition(sizeToMod,unit,full_path,namePart);
if(response == SUCCESS){
char nameMirror[50];
clearArray(nameMirror,50);
strcat(nameMirror,nameDisk);
strcat(nameMirror,"_mirror");
clearArray(full_path,sizeof(full_path));
getFullPathDisk(path,nameMirror,full_path);
//ESCRIBIR RAID
//Response response1 =
modPartition(sizeToMod,unit,full_path,namePart);
//if(response1 != SUCCESS)cout<<"Error al hacer cambio en el raid\n";
}
return response;
}
Response newPartition(int size, Unit unit, char path[], TypePartition TypePartition, Fit fit, char name[]){
MBR *disco = openMBR(path);
if(disco==NULL){
return ERROR_UNHANDLED;
}
long final_size = getSize(size,unit);
if(final_size<=0){
return ERROR_SIZE_MIN;
}
switch (TypePartition) {
case Primaria:
return newPrimaryPart(final_size,fit,name,disco,path);
case Extendida:
return newExtendedPart(final_size,fit,name,disco,path);
case Logica:
return newLogicPart(final_size,fit,name,disco,path);
break;
}
delete disco;
return SUCCESS;
}
Response modPartition(int size, Unit unit, char path[], char name[]){
MBR *disco = openMBR(path);
if(disco==NULL){
return ERROR_UNHANDLED;
}
long final_size = getSize(size,unit);
virtualBlock *parts[4];
int contadorParticiones = 0;
int i;
for(i = 0;i<4;i++){
if(disco->particiones[i].part_status == Activo){
parts[contadorParticiones] = new virtualBlock(disco->particiones[i].part_size,disco->particiones[i].part_start,OCUPADO);
contadorParticiones++;
}
}
//ordenar
virtualBlock *temporal;
for (int i = 0;i < contadorParticiones; i++){
for (int j = 0; j< contadorParticiones-1; j++){
if (parts[j]->start > parts[j+1]->start){
temporal = parts[j];
parts[j] = parts[j+1];
parts[j+1] = temporal;
}
}
}
//buscar SIGUIENTE particion o espacio antes del fin del disco
Partition *part = NULL;
int indicePart = 0;
for(i = 0;i<4;i++){
if(disco->particiones[i].part_status == Activo){
if(strcmp(disco->particiones[i].part_name,name)==0){
part = &disco->particiones[i];
break;
}
indicePart++;
}
}
if(part==NULL){
//BUSCAR PARTICION LOGICA
//SI NO EXISTE, RETORNAR ERROR
return ERROR_PARTITION_NOT_EXIST;
}else{
//MODIFICAR TAMAÑO
if(part->part_size+final_size<=0){
return ERROR_SIZE_MIN;
}
if(indicePart == contadorParticiones-1){
int newt = part->part_start+part->part_size+final_size;
if(disco->mbr_tamanio < newt){
return ERROR_NOT_SPACE_ADD;
}
}else{
int newt = part->part_start+part->part_size+final_size;
if(parts[indicePart+1]->start < newt){
return ERROR_NOT_SPACE_ADD;
}
}
part->part_size = part->part_size+final_size;
replaceMBR(disco,path);
//ESCRIBIR MBR
}
return SUCCESS;
}
Response newPrimaryPart(long size,Fit fit,char name[],MBR *disco,char path[]){
int i;
int startPoint;
Response resp = getStartAddress(disco,(Fit)disco->disk_fit,size,&startPoint);
if(resp!=SUCCESS){
return resp;
}
//BUSCAR PARTICION
Partition *partition;
bool hayEspacio = false;
for(i=0;i<4;i++){
partition = &disco->particiones[i];
if(partition->part_status==Inactivo){
hayEspacio = true;
break;
}else{
if(strcmp(partition->part_name,name)==0){
return ERROR_PARTITION_EXIST;
}
}
}
if(hayEspacio){
partition = &disco->particiones[i];
partition->part_fit = fit;
strcpy(partition->part_name,name);
partition->part_size = size;
partition->part_start = startPoint;
partition->part_type = Primaria;
partition->part_status = Activo;
replaceMBR(disco,path);
return SUCCESS;
}else{
return ERROR_FULL_PARTITION_PRIMARY;
}
}
Response getStartAddress(MBR *disco,Fit fit,long size,int *startPoint){
int i;
*startPoint = -1;
int contadorParticiones = 0;
//VIRTUALIZANDO particiones
virtualBlock *parts[4];
bool empty = true;
for(i = 0;i<4;i++){
if(disco->particiones[i].part_status == Activo){
parts[contadorParticiones] = new virtualBlock(disco->particiones[i].part_size,disco->particiones[i].part_start,OCUPADO);
empty = false;
contadorParticiones++;
}
}
if(empty){
if(disco->mbr_tamanio>=size){
*startPoint = sizeof(MBR);
}
}else{
//ordenar
virtualBlock *temporal;
for (int i = 0;i < contadorParticiones; i++){
for (int j = 0; j< contadorParticiones-1; j++){
if (parts[j]->start > parts[j+1]->start){
temporal = parts[j];
parts[j] = parts[j+1];
parts[j+1] = temporal;
}
}
}
//obtener espacios libres
int inicio=sizeof(MBR);
int fin = sizeof(MB)-1;
virtualBlock *espaciosLibres[5];
int contadorEspacios = 0;
for(i=0;i<contadorParticiones;i++){
fin = parts[i]->start;
if(fin - inicio > 0){
espaciosLibres[contadorEspacios] = new virtualBlock(fin-inicio,inicio,LIBRE);
contadorEspacios++;
}
inicio = parts[i]->start+parts[i]->size;
}
espaciosLibres[contadorEspacios] = new virtualBlock(disco->mbr_tamanio-inicio+sizeof(MBR),inicio,LIBRE);
contadorEspacios++;
//ordenar espacios libres por tamaño
for (int i = 0;i < contadorEspacios; i++){
for (int j = 0; j< contadorEspacios-1; j++){
if (espaciosLibres[j]->size > espaciosLibres[j+1]->size){
temporal = espaciosLibres[j];
espaciosLibres[j] = espaciosLibres[j+1];
espaciosLibres[j+1] = temporal;
}
}
}
//BUSCAR SEGUN AJUSTE
switch (fit) {
case FirstFit:
for(i=0;i<contadorEspacios;i++){
if(size<=espaciosLibres[i]->size){
*startPoint = espaciosLibres[i]->start;
break;
}
}
break;
case WorstFit:
if(size<=espaciosLibres[contadorEspacios-1]->size){
*startPoint = espaciosLibres[contadorEspacios-1]->start;
}
break;
case BestFit:
int tams[contadorEspacios];
for(i=0;i<contadorEspacios;i++){
tams[i] = espaciosLibres[i]->size-size;
}
int best =0;
for(i=0;i<contadorEspacios;i++){
if(tams[i]<tams[best]){
best = i;
}
}
if(best == -1){
return ERROR_INSUFICIENT_SPACE;
}
*startPoint = espaciosLibres[best]->start;
break;
}
}
if(*startPoint == -1){
return ERROR_INSUFICIENT_SPACE;
}
return SUCCESS;
}
Response newExtendedPart(long size, Fit fit, char name[], MBR *disco, char path[]){
int i;
int startPoint;
Response resp = getStartAddress(disco,(Fit)disco->disk_fit,size,&startPoint);
if(resp!=SUCCESS){
return resp;
}
//BUSCAR PARTICION
Partition *partition;
bool noHayPartExt = true;
bool hayEspacio = false;
for(i=0;i<4;i++){
partition = &disco->particiones[i];
if(partition->part_type == Extendida){
noHayPartExt = false;
break;
}
}
if(noHayPartExt)
for(i=0;i<4;i++){
partition = &disco->particiones[i];
if(partition->part_status==Inactivo){
hayEspacio = true;
break;
}else{
if(strcmp(partition->part_name,name)==0){
return ERROR_PARTITION_EXIST;
}
}
}
if(noHayPartExt){
if(hayEspacio){
partition = &disco->particiones[i];
partition->part_fit = fit;
strcpy(partition->part_name,name);
partition->part_size = size;
partition->part_start=startPoint;
partition->part_type = Extendida;
partition->part_status = Activo;
replaceMBR(disco,path);
newEBR(partition,path);
fillSpaceWithZeros(path,partition->part_start+sizeof(EBR),size);
return SUCCESS;
}else{
return ERROR_FULL_PARTITION_PRIMARY;
}
}else{
return ERROR_EXISTS_EXTENDED_PARTITION;
}
}
void newEBR(Partition *partition, char path[]){
EBR *extendedPart = (EBR*)malloc(sizeof(EBR));
extendedPart->part_fit = partition->part_fit;
strcpy(extendedPart->part_name,partition->part_name);
extendedPart->part_next = -1;
extendedPart->part_size = partition->part_size;
extendedPart->part_start = partition->part_start+sizeof(EBR);
extendedPart->part_status = Inactivo;
writeEBR(extendedPart,path,partition->part_start);
}
void writeEBR(EBR *extendedPart,char path[],int point){
FILE * myFile;
myFile = fopen (path,"rb+");
if (myFile==NULL)
{
cout<<"Error al abrir el archivo\n";
return;
}
//escribir MBR en disco
fseek(myFile, point, SEEK_SET);
fwrite(extendedPart, sizeof(EBR), 1, myFile);
//cerrando stream
fclose (myFile);
}
Response newLogicPart(long size, Fit fit, char name[], MBR *disco, char path[]){
EBR *firstEBR = getFirstEBR(disco,path);
int newPosition = 0;
if(firstEBR==NULL){
return ERROR_NOT_EXIST_EXTENDED_PARTITION;
}
Response resp = SUCCESS;
//= getStartAddressLogic(disco,firstEBR,(Fit)firstEBR->part_fit,size,&newPosition,path);
if(resp!=SUCCESS){
return resp;
}
EBR *newEBR = (EBR*)malloc(sizeof(EBR));
newEBR->part_fit = fit;
strcpy(newEBR->part_name,name);
newEBR->part_next = -1;
newEBR->part_size = size;
newEBR->part_status = Activo;
//si es el unico que existe, reemplazar
if(firstEBR->part_next == -1 && firstEBR->part_status == Inactivo){
newEBR->part_start = firstEBR->part_start;
if(size > firstEBR->part_size){
return ERROR_INSUFICIENT_SPACE;
}
writeEBR(newEBR,path,firstEBR->part_start-sizeof(EBR));
}else{
//agregar al final de la lista
bool flag = true;
while(flag){//existenExtendidas
if(firstEBR->part_next!=-1){
firstEBR = readEBR(firstEBR->part_next,path);
}else{
flag = false;
}
}
newPosition = firstEBR->part_start+firstEBR->part_size;
firstEBR->part_next = newPosition;
writeEBR(firstEBR,path,firstEBR->part_start-sizeof(EBR));
newEBR->part_start = newPosition+sizeof(EBR);
writeEBR(newEBR,path,newPosition);
}
return SUCCESS;
}
Response deletePartition(char path[],char nameDisk[], char name[],TypePartition typePartition,TypeFormat dtype){
char full_path[200];
clearArray(full_path,sizeof(full_path));
getFullPathDisk(path,nameDisk,full_path);
//ESCRIBIR ARCHIVO
Response response = rmPartition(full_path,name,typePartition,dtype);
if(response == SUCCESS){
char nameMirror[50];
clearArray(nameMirror,50);
strcat(nameMirror,nameDisk);
strcat(nameMirror,"_mirror");
clearArray(full_path,sizeof(full_path));
getFullPathDisk(path,nameMirror,full_path);
//ESCRIBIR RAID
//Response response1 =
rmPartition(full_path,name,typePartition,dtype);
//if(response1 != SUCCESS)cout<<"Error al hacer cambio en el raid\n";
}
return response;
}
Response rmPartition(char path[], char name[], TypePartition typePartition,TypeFormat dtype){
MBR *disco = openMBR(path);
if(disco==NULL){
return ERROR_UNHANDLED;
}
/*if(!canDeletePart(path,name)){
return ERROR_PARTITION_MOUNTED_DEL;
}*/
switch (typePartition) {
case Primaria:
case Extendida:
return deletePrimaryPart(disco,name,dtype,path);
case Logica:
return deleteLogicPart(name,disco,path);
break;
}
return SUCCESS;
}
Response deletePrimaryPart(MBR *disco,char name[],TypeFormat dtype,char path[]){
int i;
for(i=0;i<4;i++){
if(disco->particiones[i].part_status == Activo){
if(strcmp(disco->particiones[i].part_name,name)==0){
//delete partition
disco->particiones[i].part_status = Inactivo;
clearArray(disco->particiones[i].part_name,16);
if(dtype == Full){
fillSpaceWithZeros(path,disco->particiones[i].part_start,disco->particiones[0].part_size);
}
disco->particiones[i].part_size = 0;
disco->particiones[i].part_start = 0;
replaceMBR(disco,path);
return SUCCESS;
}
}
}
return ERROR_PARTITION_NOT_EXIST;
}
Response deleteLogicPart(char name[], MBR *disco, char path[]){
EBR *firstEBR = getFirstEBR(disco,path);
Partition *extended = getExtendedPart(name,disco);
if(extended==NULL){
return ERROR_NOT_EXIST_EXTENDED_PARTITION;
}
if(firstEBR==NULL){
return ERROR_NOT_EXIST_EXTENDED_PARTITION;
}
EBR *backEBR = NULL;
bool flag = true;
while(flag){
if(strcmp(firstEBR->part_name,name)==0){
//particion encontrada
if(backEBR!=NULL){
backEBR->part_next = firstEBR->part_next;
//reescribir el EBR anterior
writeEBR(backEBR,path,backEBR->part_start);
}
}
backEBR = firstEBR;
firstEBR = readEBR(firstEBR->part_next,path);
if(firstEBR==NULL){
flag = false;
}
}
return SUCCESS;
}
Response mountPart(char path[], char name[]){
int contador = 0;
bool exist= false;
//MONTAR DISCO
while(partsMounted[contador]!=NULL){
if(strcmp(partsMounted[contador]->path,path)==0){
exist = true;
break;
}
contador++;
}
if(!exist){
partsMounted[contador] = new MountedDisk();
strcpy(partsMounted[contador]->path,path);
partsMounted[contador]->letter = 97+contador;
}
//MONTAR PARTICION
bool existPart = false;
MountedDisk *mdisk = partsMounted[contador];
int contador2=0;
//VALIDAR QUE YA ESTÉ MONTADA LA PARTICION
while(mdisk->parts[contador2]!=NULL){
if(strcmp(mdisk->parts[contador2]->name,name)==0){
return ERROR_PARTITION_MOUNTED;
}
contador2++;
}
MBR *disco = openMBR(mdisk->path);
if(disco==NULL){
return ERROR_UNHANDLED;
}
//BUSCAR PARTICION PRIMARIA/EXTENDIDA
int i;
int init;
for(i=0;i<4;i++){
if(strcmp(disco->particiones[i].part_name,name)==0){
existPart = true;
init = disco->particiones[i].part_start;
break;
}
}
if(existPart){
mdisk->parts[contador2] = new MountedPart();
strcpy(mdisk->parts[contador2]->id,getPartId(mdisk->letter,contador2));
strcpy(mdisk->parts[contador2]->name, name);
mdisk->parts[contador2]->start = init;
}else{
return ERROR_PARTITION_NOT_EXIST;
}
//MODIFICAR SUPERBLOCK
SuperBlock *sb =readSuperBlock(path,name);
if(sb==NULL){
return ERROR_UNHANDLED;
}
sb->s_mnt_count = sb->s_mnt_count+1;
getCurrentDate(sb->s_mtime);
writeSuperBlock(sb,path,init);
return SUCCESS;
}
void showMounts(){
int contador = 0;
int contador2=0;
while(partsMounted[contador]!=NULL){
cout<<"--------------------------------------------------\n";
cout<<"DISCO \""<<partsMounted[contador]->letter<<"\" \n";
cout<<partsMounted[contador]->path<<endl;
cout<<"Particiones: \n";
contador2 = 0;
while (partsMounted[contador]->parts[contador2]!=NULL) {
cout<<"Nombre: "<<partsMounted[contador]->parts[contador2]->name<<endl;
cout<<"Id: "<<partsMounted[contador]->parts[contador2]->id<<endl;
contador2++;
}
contador++;
}
cout<<"--------------------------------------------------\n";
}
char* getPartId(char letra, int numero){
string str("vd");
str+=letra;
str+=to_string(numero);
return &str[0];
}
Response unmountPart(char id[]){
string str(id);
if(strlen(id)<4){
return ERROR_ID_MALFORMED;
}
char letra = str.at(2);
//BUSCAR LETRA
int contadorDiscos = 0;
bool existeDisco= false;
while(partsMounted[contadorDiscos]!=NULL){
if(partsMounted[contadorDiscos]->letter == letra){
existeDisco = true;
break;
}
contadorDiscos++;
}
if(!existeDisco){
return ERROR_DISK_NOT_EXIST;
}
//BUSCAR NUMERO
int contadorPart = 0;
int sizeParts= 0;
bool existePart = false;
while((partsMounted[contadorDiscos])->parts[sizeParts]!=NULL){
sizeParts++;
}
while((partsMounted[contadorDiscos])->parts[contadorPart]!=NULL){
if(strcmp((partsMounted[contadorDiscos])->parts[contadorPart]->id,id)==0){
existePart = true;
break;
}
contadorPart++;
}
if(existePart){
delete (partsMounted[contadorDiscos])->parts[contadorPart];
(partsMounted[contadorDiscos])->parts[contadorPart] = NULL;
sizeParts--;
if(sizeParts == 0){
//desmontar disco
SuperBlock *sb =readSuperBlock(partsMounted[contadorDiscos]->path,
partsMounted[contadorDiscos]->parts[contadorPart]->name);
if(sb==NULL){
return ERROR_UNHANDLED;
}
sb->s_mnt_count = sb->s_mnt_count+1;
getCurrentDate(sb->s_mtime);
writeSuperBlock(sb,partsMounted[contadorDiscos]->path,partsMounted[contadorDiscos]->parts[contadorPart]->start);
while((partsMounted[contadorDiscos])!=NULL){
delete partsMounted[contadorDiscos];
(partsMounted[contadorDiscos]) = (partsMounted[contadorDiscos+1]);
}
(partsMounted[contadorDiscos]) = NULL;
}
}else{
return ERROR_PARTITION_NOT_MOUNTED;
}
return SUCCESS;
}
Response getStartAddressLogic(MBR *disco,EBR *part,Fit fit,long size,int *startPoint,char path[]){
//VIRTUALIZAR PARTICIONES
EBR *firstEBR = part;
virtualBlock *espaciosLibres = NULL;
bool flag = true;
int inicio = firstEBR->part_start;
int fin = firstEBR->part_start-1;
int contadorEspacios = 0;
while(flag){
if(firstEBR->part_next!=-1){
fin = firstEBR->part_start;
if(fin - inicio > 0){
if(espaciosLibres==NULL){
espaciosLibres = new virtualBlock(fin-inicio,inicio,LIBRE);
}else{
virtualBlock *newt = new virtualBlock(fin-inicio,inicio,LIBRE);
espaciosLibres->next = newt;
espaciosLibres = espaciosLibres->next;
}
contadorEspacios++;
}
inicio = firstEBR->part_start+firstEBR->part_size;
firstEBR = readEBR(firstEBR->part_next,path);
}else{
flag = false;
}
}
if(espaciosLibres==NULL){
espaciosLibres = new virtualBlock(disco->mbr_tamanio-inicio+sizeof(MBR),inicio,LIBRE);
}
contadorEspacios++;
//ORDENAR
/*
for (int i = 0;i < contadorEspacios; i++){
for (int j = 0; j< contadorEspacios-1; j++){
if (espaciosLibres[j]->size > espaciosLibres[j+1]->size){
temporal = espaciosLibres[j];
espaciosLibres[j] = espaciosLibres[j+1];
espaciosLibres[j+1] = temporal;
}
}
}
*/
//BUSCAR SEGUN AJUSTE
switch (fit) {
case FirstFit:
{
virtualBlock *it = espaciosLibres;
while(it!=NULL){
if(size<=it->size){
*startPoint = it->start;
break;
}
it = it->next;
}
}
break;
case WorstFit:
{
virtualBlock *it = espaciosLibres;
virtualBlock *ant;
while(it!=NULL){
ant = it;
it = it->next;
}
if(size<=ant->size){
*startPoint = it->start;
}
}
break;
case BestFit:
/*int tams[contadorEspacios];
for(i=0;i<contadorEspacios;i++){
tams[i] = espaciosLibres[i]->size-size;
}
int best =0;
for(i=0;i<contadorEspacios;i++){
if(tams[i]<tams[best]){
best = i;
}
}
if(best == -1){
return ERROR_INSUFICIENT_SPACE;
}
*startPoint = espaciosLibres[best]->start;*/
break;
}
if(*startPoint == -1){
return ERROR_INSUFICIENT_SPACE;
}
return SUCCESS;
}
MountedPart* getMountedPartition(char id[]){
string str(id);
if(strlen(id)<4){
showMessageError(ERROR_ID_MALFORMED);
return NULL;
}
char letra = str.at(2);
//BUSCAR LETRA
int contadorDiscos = 0;
bool existeDisco= false;
while(partsMounted[contadorDiscos]!=NULL){
if(partsMounted[contadorDiscos]->letter == letra){
existeDisco = true;
break;
}
contadorDiscos++;
}
if(!existeDisco){
showMessageError(ERROR_DISK_NOT_EXIST);
return NULL;
}
//BUSCAR NUMERO
int contadorPart = 0;
int sizeParts= 0;
bool existePart = false;
while((partsMounted[contadorDiscos])->parts[sizeParts]!=NULL){
sizeParts++;
}
while((partsMounted[contadorDiscos])->parts[contadorPart]!=NULL){
if(strcmp((partsMounted[contadorDiscos])->parts[contadorPart]->id,id)==0){
existePart = true;
break;
}
contadorPart++;
}
if(existePart){
return partsMounted[contadorDiscos]->parts[contadorPart];
}else{
showMessageError(ERROR_PARTITION_NOT_MOUNTED);
return NULL;
}
}
MountedDisk* getMountedDisk(char id[]){
if(id==NULL) return NULL;
string str(id);
if(strlen(id)<4){
showMessageError(ERROR_ID_MALFORMED);
return NULL;
}
char letra = str.at(2);
//BUSCAR LETRA
int contadorDiscos = 0;
bool existeDisco= false;
while(partsMounted[contadorDiscos]!=NULL){
if(partsMounted[contadorDiscos]->letter == letra){
existeDisco = true;
break;
}
contadorDiscos++;
}
if(!existeDisco){
showMessageError(ERROR_DISK_NOT_MOUNTED);
return NULL;
}
return partsMounted[contadorDiscos];
}