-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_disk.cpp
528 lines (487 loc) · 16.4 KB
/
handler_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
#include "handler_disk.h"
Response newDisk(int size,Fit fit,Unit unit,char path[],char name[]){
//VALIDAR TAMAÑO
long final_size = getSize(size,unit);
//MBR
MBR* disco = (MBR*)malloc(sizeof(MBR));
getCurrentDate(disco->mbr_fecha_creacion);
disco->disk_fit = fit;
disco->mbr_disk_signature=getSignature();
disco->mbr_tamanio = final_size;
//PARTITIONS
for(int i = 0; i < 4; i++){
disco->particiones[i].part_fit = fit;
string nombre = "partition_"+to_string(i+1);
clearArray(disco->particiones[i].part_name,16);
strcpy(disco->particiones[i].part_name,nombre.c_str());
disco->particiones[i].part_size = 0;
disco->particiones[i].part_start = 0;
disco->particiones[i].part_status = Inactivo;
disco->particiones[i].part_type = Primaria;
}
//CREAR FULL PATH
char full_path[200];
clearArray(full_path,sizeof(full_path));
getFullPathDisk(path,name,full_path);
//ESCRIBIR ARCHIVO
bool res= writeMBR(disco,path,full_path);
if(!res)return ERROR_DISK_NOT_EXIST;
//ESCRIBIR RAID
char nameMirror[50];
clearArray(nameMirror,50);
strcat(nameMirror,name);
strcat(nameMirror,"_mirror");
clearArray(full_path,sizeof(full_path));
getFullPathDisk(path,nameMirror,full_path);
writeMBR(disco,path,full_path);
delete disco;
return SUCCESS;
}
bool writeMBR(MBR *disco,char path[],char full_path[]){
//CREAR DIRECTORIO SI NO EXISTE
createPath(full_path);
FILE *myFile;
myFile = fopen(full_path,"wb+");
if (myFile==NULL)
{
cout<<"Error al crear el archivo\n";
return false;
}
//inicializando con ceros
fseek(myFile, disco->mbr_tamanio, SEEK_SET);
fwrite("\0", sizeof(char), 1, myFile);
//escribir MBR en disco
fseek(myFile, 0, SEEK_SET);
fwrite(disco, sizeof(MBR), 1, myFile);
//cerrando stream
fclose (myFile);
return true;
}
int getSignature(){
srand(time(NULL));
int num = rand();
num = 1 + rand() % (100000 - 1000000);
return num;
}
void getCurrentDate(char *date){
time_t tiempo = time(0);
struct tm *tlocal = localtime(&tiempo);
strftime(date, 16, "%x %H:%M", tlocal);
}
void deleteDisk(char path[]){
cout<<"Está seguro que desea eliminar el disco? \n"
<<"Presione S para eliminar o N para cancelar\n";
char response;
cin>>response;
if(response == 83 || response == 115){
if(canDeleteDisk(path)){
if(remove(path) != 0 )
cout<<"Error al borrar el disco\n";
else
cout<<"¡El disco se borro con exito!\n";
}else{
cout<<"El disco no se puede eliminar por qué tiene particiones montadas\n";
}
}else if(response!=78 && response!=110){
cout<<"Tecla inválida\n";
}
}
void reportMBR(char path[],char path_report[]){
MBR* disco = openMBR(path);
if(disco==NULL){
cout<<"Error al generar reporte de MBR\n";
return;
}
FILE *myFile;
myFile = fopen("report_mbr.dot","w+");
if (myFile==NULL)
{
cout<<"Error al crear el archivo\n";
return;
}
fseek(myFile, 0, SEEK_SET);
fputs("digraph {\ntbl [\nshape=plaintext\n label=<\n", myFile);
fputs("<table border='0' cellborder='1' cellspacing='0'>\n",myFile);
fputs("<tr><td colspan=\"3\">Disk1</td></tr>\n",myFile);
fputs("<th><td>Nombre</td><td>Valor</td></th>\n",myFile);
//DATA OF MBR
//TAMAÑO
fputs("<tr><td bgcolor=\"lightblue\">mbr_tamaño</td><td bgcolor=\"lightblue\">",myFile);
fputs(&to_string(disco->mbr_tamanio)[0],myFile);
fputs("</td></tr>\n",myFile);
//FECHA
fputs("<tr><td bgcolor=\"lightblue\">mbr_Fecha_creación</td><td bgcolor=\"lightblue\">",myFile);
fputs(disco->mbr_fecha_creacion,myFile);
fputs("</td></tr>\n",myFile);
//SIGNATURE
fputs("<tr><td bgcolor=\"lightblue\">mbr_disk_signature</td><td bgcolor=\"lightblue\">",myFile);
fprintf(myFile, "%d", disco->mbr_disk_signature);
fputs("</td></tr>\n",myFile);
//FIT
fputs("<tr><td bgcolor=\"lightblue\">mbr_disk_fit</td><td bgcolor=\"lightblue\">",myFile);
fprintf(myFile, "%c", disco->disk_fit);
fputs("</td></tr>\n",myFile);
int i;
Partition part;
char colors[4][10];
clearArray(colors[0],10);
strcat(colors[0],"#bcf7c1");
clearArray(colors[1],10);
strcat(colors[1],"#f8fc92");
clearArray(colors[2],10);
strcat(colors[2],"#fcc292");
clearArray(colors[3],10);
strcat(colors[3],"#dfbcf7");
for(i=0;i<4;i++){
part = disco->particiones[i];
if(part.part_status == Inactivo) continue;
//PART NAME
fputs("<tr><td colspan=\"2\" bgcolor=\"",myFile);
fputs(colors[i],myFile);
fputs("\">",myFile);
fputs(part.part_name,myFile);
fputs("</td></tr>\n",myFile);
//PART STATUS
fputs("<tr><td bgcolor=\"",myFile);
fputs(colors[i],myFile);
fputs("\">part_status</td><td bgcolor=\"",myFile);
fputs(colors[i],myFile);
fputs("\">",myFile);
fprintf(myFile, "%d", part.part_status);
fputs("</td></tr>\n",myFile);
//PART TYPE
fputs("<tr><td bgcolor=\"",myFile);
fputs(colors[i],myFile);
fputs("\">part_type</td><td bgcolor=\"",myFile);
fputs(colors[i],myFile);
fputs("\">",myFile);
fprintf(myFile, "%c", part.part_type);
fputs("</td></tr>\n",myFile);
//PART FIT
fputs("<tr><td bgcolor=\"",myFile);
fputs(colors[i],myFile);
fputs("\">part_fit</td><td bgcolor=\"",myFile);
fputs(colors[i],myFile);
fputs("\">",myFile);
fprintf(myFile, "%c", part.part_fit);
fputs("</td></tr>\n",myFile);
//PART START
fputs("<tr><td bgcolor=\"",myFile);
fputs(colors[i],myFile);
fputs("\">part_start</td><td bgcolor=\"",myFile);
fputs(colors[i],myFile);
fputs("\">",myFile);
fprintf(myFile, "%d", part.part_start);
fputs("</td></tr>\n",myFile);
//PART SIZE
fputs("<tr><td bgcolor=\"",myFile);
fputs(colors[i],myFile);
fputs("\">part_size</td><td bgcolor=\"",myFile);
fputs(colors[i],myFile);
fputs("\">",myFile);
fputs(&to_string(part.part_size)[0],myFile);
fputs("</td></tr>\n",myFile);
}
fputs("</table>\n",myFile);
fputs(">];\n\n", myFile);
//REPORTE DE EBR'S
i = 0;
EBR *ebr = getFirstEBR(disco,path);
while(ebr!=NULL){//existenExtendidas
addReportEBR(ebr,myFile,i);
if(ebr->part_next!=-1){
ebr = readEBR(ebr->part_next,path);
}else{
ebr = NULL;
}
i++;
}
fputs("}\n",myFile);
//cerrando stream
fclose (myFile);
string pathString(path_report);
string command = "dot -Tpng report_mbr.dot -o \""+pathString+"\"";//+"/report_mbr.png";
system(command.c_str());
cout<<"Reporte de MBR creado...\n";
}
void addReportEBR(EBR *ebr,FILE *myFile,int index){
string nombreNodo = "tbl"+to_string(index+1);
fputs(nombreNodo.c_str(),myFile);
fputs(" [\nshape=plaintext\n label=<\n", myFile);
fputs("<table border='0' cellborder='1' cellspacing='0'>\n",myFile);
fputs("<tr><td colspan=\"3\">",myFile);
fputs(ebr->part_name,myFile);
fputs("</td></tr>\n",myFile);
fputs("<th><td>Nombre</td><td>Valor</td></th>\n",myFile);
//PART STATUS
fputs("<tr><td bgcolor=\"#fcc8c8\">part_status</td><td bgcolor=\"#fcc8c8\">",myFile);
fprintf(myFile, "%d", ebr->part_status);
fputs("</td></tr>\n",myFile);
//PART FIT
fputs("<tr><td bgcolor=\"#fcc8c8\">part_fit</td><td bgcolor=\"#fcc8c8\">",myFile);
fprintf(myFile, "%c", ebr->part_fit);
fputs("</td></tr>\n",myFile);
//PART START
fputs("<tr><td bgcolor=\"#fcc8c8\">part_start</td><td bgcolor=\"#fcc8c8\">",myFile);
fprintf(myFile, "%d", ebr->part_start);
fputs("</td></tr>\n",myFile);
//PART SIZE
fputs("<tr><td bgcolor=\"#fcc8c8\">part_size</td><td bgcolor=\"#fcc8c8\">",myFile);
fputs(&to_string(ebr->part_size)[0],myFile);
fputs("</td></tr>\n",myFile);
//PART NEXT
fputs("<tr><td bgcolor=\"#fcc8c8\">part_next</td><td bgcolor=\"#fcc8c8\">",myFile);
fprintf(myFile, "%d", ebr->part_next);
fputs("</td></tr>\n",myFile);
fputs("</table>\n",myFile);
fputs(">];\n", myFile);
}
MBR* openMBR(char path[]){
FILE *myFile;
myFile = fopen(path,"rb+");
if(myFile==NULL){
cout<<"Error: No existe el disco\n";
return NULL;
}
fseek(myFile, 0, SEEK_SET);
MBR *mbr = (MBR*)malloc(sizeof(MBR));
fread(mbr,sizeof(MBR),1,myFile);
fclose(myFile);
return mbr;
}
void replaceMBR(MBR *disco,char path[]){
FILE *myFile;
myFile = fopen(path,"rb+");
if (myFile==NULL)
{
cout<<"Error al abrir el archivo\n";
return;
}
//escribir MBR en disco
fseek(myFile, 0, SEEK_SET);
fwrite(disco, sizeof(MBR), 1, myFile);
//cerrando stream
fclose (myFile);
}
EBR* getFirstEBR(MBR *disco,char path[]){
int i;
Partition *extended = NULL;
for(i=0;i<4;i++){
if(disco->particiones[i].part_status == Activo && disco->particiones[i].part_type == Extendida){
extended = &disco->particiones[i];
break;
}
}
if(extended!=NULL){
return readEBR(extended->part_start,path);
}
return NULL;
}
EBR* readEBR(int point, char path[]){
FILE *myFile = fopen(path,"rb+");
if(myFile==NULL){
cout<<"Error al abrir el archivo \n";
return NULL;
}
EBR *ebr = (EBR*)malloc(sizeof(EBR));
fseek(myFile, point, SEEK_SET);
fread(ebr, sizeof(EBR), 1, myFile);
fclose(myFile);
return ebr;
}
Partition* getExtendedPart(char name[], MBR *disco){
int i;
for(i=0;i<4;i++){
if(disco->particiones[i].part_status == Activo && disco->particiones[i].part_type == Extendida){
if(strcmp(disco->particiones[i].part_name,name)==0){
return &disco->particiones[i];
}
}
}
return NULL;
}
void reportDisk(char path[], char path_report[]){
MBR* disco = openMBR(path);
if(disco==NULL){
cout<<"Error al generar reporte de disco\n";
return;
}
FILE *myFile;
myFile = fopen("report_disk.dot","w+");
if (myFile==NULL)
{
cout<<"Error al crear el archivo de reporte\n";
return;
}
fseek(myFile, 0, SEEK_SET);
fputs("digraph G {\n", myFile);
fputs("parent [\n", myFile);
fputs("shape=plaintext\n", myFile);
fputs("label=<\n", myFile);
fputs("<table border='1' cellborder='1'>\n", myFile);
fputs("<tr>\n", myFile);
//MBR
fputs("<td rowspan=\"2\" bgcolor =\"#dd8703\" >MBR</td>\n", myFile);
int i;
int inicio = sizeof(MBR);
int fin = sizeof(MBR)-1;
for(i=0;i<4;i++){
if(disco->particiones[i].part_status == Activo){
switch(disco->particiones[i].part_type){
case Primaria:
fin = disco->particiones[i].part_start;
if(fin-inicio > 0){
//ESPACIO LIBRE
fputs("<td rowspan=\"2\" bgcolor = \"#3ac9da\">Libre<br/>", myFile);
fprintf(myFile, "%.2f",(getDecimal((float)((fin-inicio)*100)/disco->mbr_tamanio)));
fputs("%</td>\n", myFile);
}
inicio = disco->particiones[i].part_start+disco->particiones[i].part_size;
//PARTICION PRIMARIA
fputs("<td rowspan=\"2\" bgcolor =\"#50b104\" >Primaria <br/>",myFile);
fprintf(myFile, "%.2f",getDecimal((float)(disco->particiones[i].part_size*100)/disco->mbr_tamanio));
fputs("%</td>\n", myFile);
break;
case Extendida:
fin = disco->particiones[i].part_start;
if(fin-inicio > 0){
//ESPACIO LIBRE
fputs("<td rowspan=\"2\" bgcolor = \"#3ac9da\">Libre<br/>\n", myFile);
fprintf(myFile, "%.2f",getDecimal((float)((fin-inicio)*100)/disco->mbr_tamanio));
fputs("%</td>\n", myFile);
}
inicio = disco->particiones[i].part_start+disco->particiones[i].part_size;
//PARTICION EXTENDIDA
fputs("<td>\n", myFile);
writeExtendedReport(myFile,disco,path);
fputs("</td>\n", myFile);
break;
}
}
}
if(disco->mbr_tamanio-inicio > 0){
//ESPACIO LIBRE
fputs("<td rowspan=\"2\" bgcolor = \"#3ac9da\">Libre<br/>\n", myFile);
fprintf(myFile, "%.2f", getDecimal((float)((disco->mbr_tamanio-inicio)*100)/disco->mbr_tamanio));
fputs("%</td>\n", myFile);
}
fputs("</tr>\n", myFile);
fputs("</table>\n", myFile);
fputs(">];\n", myFile);
fputs("}\n",myFile);
//cerrando stream
fclose (myFile);
string pathString(path_report);
string command = "dot -Tpng report_disk.dot -o \""+pathString+"\"";//+"/report_disk.png";
system(command.c_str());
cout<<"Reporte de disco creado...\n";
}
void writeExtendedReport(FILE *myFile, MBR *disco, char path[]){
EBR *first = getFirstEBR(disco,path);
if(first == NULL){
return;
}
int i;
Partition *extended = NULL;
for(i=0;i<4;i++){
if(disco->particiones[i].part_status == Activo && disco->particiones[i].part_type == Extendida){
extended = &disco->particiones[i];
break;
}
}
if(extended==NULL){
return;
}
/* int contador = 0;
bool flag1 = true;
EBR *nodo = first;
while (flag1) {
if(nodo->part_next!=-1){
nodo = readEBR(nodo->part_next,path);
}else{
flag1 = false;
}
contador++;
}*/
fputs("<table border = \"1\" cellborder=\"1\" bgcolor=\"#da3a85\">\n", myFile);
//fputs("<tr><td colspan=\"", myFile);
//fputs(to_string(contador+2).c_str(), myFile);
//fputs("\">Extendida</td></tr>\n", myFile);
fputs("<tr>\n", myFile);
bool flag = true;
int inicio = extended->part_start;
int fin = 0;
while(flag){
fin = first->part_start-sizeof(EBR);
if(fin-inicio > 0){
//ESPACIO LIBRE
fputs("<td rowspan=\"2\" bgcolor = \"#3ac9da\">\n", myFile);
fprintf(myFile, "%.2f",getDecimal((float)((fin-inicio)*100)/disco->mbr_tamanio));
fputs("%</td>\n", myFile);
}
inicio = first->part_start+first->part_size;
if(first->part_status == Activo){
fputs("<td bgcolor=\"#eeeeee\">EBR</td>\n", myFile);
}
fputs("<td bgcolor=\"#eeeeee\">", myFile);
if(first->part_status == Inactivo){
fputs("Libre <br/>",myFile);
}else{
fputs("Lógica <br/>",myFile);
}
fprintf(myFile, "%.2f",getDecimal((float)(first->part_size*100)/disco->mbr_tamanio));
fputs("%</td>\n",myFile);
if(first->part_next!=-1){
first = readEBR(first->part_next,path);
}else{
flag = false;
}
}
if(disco->mbr_tamanio-inicio > 0){
//ESPACIO LIBRE
fputs("<td rowspan=\"2\" bgcolor = \"#3ac9da\">Libre<br/>\n", myFile);
fprintf(myFile, "%.2f",getDecimal((float)(((extended->part_start+extended->part_size)-inicio)*100)/disco->mbr_tamanio));
fputs("%</td>\n", myFile);
}
fputs("</tr>\n", myFile);
fputs("</table>\n", myFile);
}
bool canDeleteDisk(char path[]){
int contadorDiscos = 0;
bool exist= false;
//MONTAR DISCO
while(partsMounted[contadorDiscos]!=NULL){
if(strcmp(partsMounted[contadorDiscos]->path,path)==0){
exist = true;
break;
}
contadorDiscos++;
}
return !exist;
}
bool canDeletePart(char path[],char name[]){
int contadorDiscos = 0;
bool exist= false;
//MONTAR DISCO
while(partsMounted[contadorDiscos]!=NULL){
if(strcmp(partsMounted[contadorDiscos]->path,path)==0){
exist = true;
break;
}
contadorDiscos++;
}
//MONTAR PARTICION
bool existPart = false;
MountedDisk *mdisk = partsMounted[contadorDiscos];
int contador2=0;
//VALIDAR QUE YA ESTÉ MONTADA LA PARTICION
if(mdisk!=NULL){
while(mdisk->parts[contador2]!=NULL){
if(strcmp(mdisk->parts[contador2]->name,name)==0){
existPart = true;
}
contador2++;
}
}
return !false;
}