-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmontarFS.c
156 lines (127 loc) · 4.54 KB
/
montarFS.c
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
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include "montarFS.h"
t_fileSysADM * montarFileSys(char* path){
t_fileSysADM* cerebroDelFS = malloc(sizeof(fileSysADM));
t_config *fileSysConfig = config_create(path);
char *pathArchivos = enHome(config_get_string_value(fileSysConfig, "PATH_ARCHIVOS"));
char *pathNodos = enHome(config_get_string_value(fileSysConfig, "PATH_NODOS"));
char *pathDirectorios = enHome(config_get_string_value(fileSysConfig, "PATH_DIRECTORIOS"));
t_config *nodosConfig = config_create(pathNodos);
montarDirectorios(pathDirectorios, fileSysCerebro);
montarArchivos(pathArchivos, fileSysCerebro);
montarNodos(nodosConfig, fileSysCerebro);
// extraer info de configs y guardar todo en el cerebro
free(pathArchivos);
free(pathNodos);
return cerebroDelFS;
}
char * enHome(char * relPath){
char* pathHome = getenv("HOME");
char *finalPath = calloc(strlen(relPath)+strlen(pathHome)+1,1);
strcpy(finalPath, pathHome);
strcat(finalPath, relPath);
return finalPath;
}
void montarNodos(t_config *nodoConf, fileSysADM *fileSysCerebro){
char ** idNodos = config_get_array_value(nodoConf,"NODOS");
int i = 0;
t_list nodosCargados = fileSysCerebro->nodos;
t_nodo *nodoACargar;
char *keyNodo = malloc(20);
while(idNodos[i]] != NULL){
strcpy(keyNodo, idNodos[i]]);
nodoACargar = crearNodo(idNodos[i], config_get_int_value(nodoConf,strcat(keyNodo,"Total")), config_get_int_value(nodoConf, strcat(keyNodo,"Libre")));
list_add(nodosCargados, nodoACargar);
fileSysCerebro->cantNodos++;
}
fileSysCerebro->size = config_get_int_value(nodoConf,"TAMANIO");
fileSysCerebro->espacioLibreTotal = config_get_int_value(nodoConf,"LIBRE");
free(keyNodo);
}
t_nodo *crearNodo(char *id, int cantBloquesTotal, int cantBloquesLibres){
t_nodo *pNodo = malloc(sizeof(t_nodo));
pNodo->id = malloc(strlen(id)+1);
strcpy(pNodo->id,id);
pNodo->cantBloquesTotal = cantBloquesTotal;
pNodo->cantBloquesLibres = cantBloquesLibres;
pNodo->socketFS = -1;
return pNodo;
}
void montarDirectorios(char *pathDir, fileSysADM *fileSysCerebro){
FILE* fDir = fopen(pathDir,"r");
t_list dirs = fileSysCerebro->directorios;
fileSysCerebro->cantDir = 0;
while(!feof(fDir)){
t_directorio dir;
fread(&dir, sizeof(dir), 1, fDir);
list_add(dirs, dir);
fileSysCerebro->cantDir++;
}
}
void montarArchivos(char *archFolder, fileSysADM *fileSysCerebro){
int i = 1;
int longFoldPath = strlen(archFolder);
char *pathCompleto = malloc(longFoldPath+100);
strcpy(pathCompleto, archFolder);
char *pathParcial = pathCompleto + longFoldPath;
t_list *archivos = fileSysCerebro->archivos;
t_infoArchivo *arch = malloc(sizeof(t_infoArchivo));
DIR *dp;
struct dirent *ep;
t_config *fileInfo;
while( i <= fileSysCerebro->cantDir){
sprintf(pathParcial, "/%d", i);
dp = opendir(pathCompleto);
if(dp!=NULL){
while(ep = readdir(dp)){
arch->indexDirPadre = i;
arch->nombre = malloc(strlen(ep->d_name));
strcpy(arch->nombre, ep->d_name);
strcpy(pathParcial+strlen(pathParcial), arch->nombre);
fileInfo = config_create(pathCompleto);
arch->size = config_get_int_value(fileInfo, "TAMANIO");
arch->tipo = config_get_int_value(fileInfo, "TIPO");
arch->estado = DISPONIBLE;
arch->bloques = obtenerBloques(fileInfo);
closedir(dp);
list_add(archivos, arch);
}
}else
return -1;
}
free(archInf);
free(pathCompleto);
}
t_list *obtenerBloques(t_config *fileInfo){
t_list *bloques = list_create();
t_bloque *bloque;
char * keyBloque = malloc(100), **dupla;
int i = 0, j = 0, offset;
offset = sprintf(keyBloque, "%s%d", "BLOQUE", i);
sprintf(keyBloque+offset, "%s%d","COPIA", j);
while(config_has_property(fileInfo, keyBloque)){
while( config_has_property(fileInfo, keyBloque)){
bloque = malloc(sizeof(t_bloque));
bloque->posEnArch = i;
dupla = config_get_array_value(fileInfo, keyBloque);
bloque->id = malloc(strlen(dupla[0]));
strcpy(bloque->id, dupla[0]);
bloque->posEnNodo = dupla[1];
list_add(bloques, bloque);
j++;
sprintf(keyBloque+offset, "%s%d","COPIA", j);
}
sprintf(keyBloque+offset, "%s", "BYTES");
while(j >= 0){
bloque = list_get(bloques, i+j); /* ESTO DEPENDE DEL ORDEN EN QUE SE GUARDARON LOS BLOQUES EN
EL WHILE DE MAS ARRIBA */
bloque->finBloque = config_get_int_value(fileInfo, keyBloque);
}
i++;
offset = sprintf(keyBloque, "%s%d", "BLOQUE", i);
}
return bloques;
}