Changeset 790 for trunk/zoo-project/zoo-kernel/caching.c
- Timestamp:
- Dec 19, 2016, 6:01:06 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/zoo-project/zoo-kernel/caching.c
r781 r790 148 148 149 149 /** 150 * Read the downloaded file for a specific input 151 * 152 * @param m the maps containing the settings of the main.cfg file 153 * @param in the input 154 * @param index the input index 155 * @param hInternet the internet connection 156 * @return 0 in case of success, 4 in case of failure 157 */ 158 int readCurrentInput(maps** m,maps** in,int* index,HINTERNET* hInternet){ 159 map* tmp1; 160 char sindex[5]; 161 maps* content=*in; 162 map* length=getMap(content->content,"length"); 163 int shouldClean=-1; 164 if(length==NULL){ 165 length=createMap("length","1"); 166 shouldClean=1; 167 } 168 for(int i=0;i<atoi(length->value);i++){ 169 char* fcontent; 170 char *mimeType=NULL; 171 int fsize=0; 172 char cname[15]; 173 char vname[11]; 174 char vname1[11]; 175 char sname[9]; 176 char mname[15]; 177 char icname[14]; 178 char xname[16]; 179 char oname[12]; 180 if(*index>0) 181 sprintf(vname1,"value_%d",*index); 182 else 183 sprintf(vname1,"value"); 184 185 if(i>0){ 186 tmp1=getMap(content->content,cname); 187 sprintf(cname,"cache_file_%d",i); 188 sprintf(vname,"value_%d",i); 189 sprintf(sname,"size_%d",i); 190 sprintf(mname,"mimeType_%d",i); 191 sprintf(icname,"isCached_%d",i); 192 sprintf(xname,"Reference_%d",i); 193 sprintf(oname,"Order_%d",i); 194 }else{ 195 sprintf(cname,"cache_file"); 196 sprintf(vname,"value"); 197 sprintf(sname,"size"); 198 sprintf(mname,"mimeType"); 199 sprintf(icname,"isCached"); 200 sprintf(xname,"Reference"); 201 sprintf(oname,"Order"); 202 } 203 204 map* tmap=getMap(content->content,oname); 205 sprintf(sindex,"%d",*index+1); 206 if((tmp1=getMap(content->content,xname))!=NULL && tmap!=NULL && strcasecmp(tmap->value,sindex)==0){ 207 208 if(getMap(content->content,icname)==NULL){ 209 210 fcontent=(char*)malloc((hInternet->ihandle[*index].nDataLen+1)*sizeof(char)); 211 if(fcontent == NULL){ 212 return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); 213 } 214 size_t dwRead; 215 InternetReadFile(hInternet->ihandle[*index], 216 (LPVOID)fcontent, 217 hInternet->ihandle[*index].nDataLen, 218 &dwRead); 219 fcontent[hInternet->ihandle[*index].nDataLen]=0; 220 fsize=hInternet->ihandle[*index].nDataLen; 221 if(hInternet->ihandle[*index].mimeType==NULL) 222 mimeType=zStrdup("none"); 223 else 224 mimeType=zStrdup(hInternet->ihandle[*index].mimeType); 225 226 map* tmpMap=getMapOrFill(&(*in)->content,vname,""); 227 free(tmpMap->value); 228 tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); 229 if(tmpMap->value==NULL){ 230 return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); 231 } 232 memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); 233 234 char ltmp1[256]; 235 sprintf(ltmp1,"%d",fsize); 236 map* tmp=getMapFromMaps(*m,"main","cacheDir"); 237 if(tmp!=NULL){ 238 char* md5str=getMd5(tmp1->value); 239 char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); 240 sprintf(fname,"%s/%s.zca",tmp->value,md5str); 241 addToMap((*in)->content,cname,fname); 242 free(fname); 243 } 244 addToMap((*in)->content,sname,ltmp1); 245 addToMap((*in)->content,mname,mimeType); 246 addToCache(*m,tmp1->value,fcontent,mimeType,fsize, NULL, 0); 247 free(fcontent); 248 free(mimeType); 249 *index++; 250 251 } 252 } 253 } 254 if(shouldClean>0){ 255 freeMap(&length); 256 free(length); 257 } 258 return 0; 259 } 260 261 /** 150 262 * Effectively run all the HTTP requests in the queue 151 263 * … … 157 269 */ 158 270 int runHttpRequests(maps** m,maps** inputs,HINTERNET* hInternet){ 159 if(hInternet ->nb>0){271 if(hInternet!=NULL && hInternet->nb>0){ 160 272 processDownloads(hInternet); 161 273 maps* content=*inputs; 162 map* tmp1;163 274 int index=0; 164 char sindex[5];165 275 while(content!=NULL){ 166 167 map* length=getMap(content->content,"length"); 168 int shouldClean=-1; 169 if(length==NULL){ 170 length=createMap("length","1"); 171 shouldClean=1; 172 } 173 for(int i=0;i<atoi(length->value);i++){ 174 char* fcontent; 175 char *mimeType=NULL; 176 int fsize=0; 177 char cname[15]; 178 char vname[11]; 179 char vname1[11]; 180 char sname[9]; 181 char mname[15]; 182 char icname[14]; 183 char xname[16]; 184 char oname[12]; 185 if(index>0) 186 sprintf(vname1,"value_%d",index); 187 else 188 sprintf(vname1,"value"); 189 190 if(i>0){ 191 tmp1=getMap(content->content,cname); 192 sprintf(cname,"cache_file_%d",i); 193 sprintf(vname,"value_%d",i); 194 sprintf(sname,"size_%d",i); 195 sprintf(mname,"mimeType_%d",i); 196 sprintf(icname,"isCached_%d",i); 197 sprintf(xname,"Reference_%d",i); 198 sprintf(oname,"Order_%d",i); 199 }else{ 200 sprintf(cname,"cache_file"); 201 sprintf(vname,"value"); 202 sprintf(sname,"size"); 203 sprintf(mname,"mimeType"); 204 sprintf(icname,"isCached"); 205 sprintf(xname,"Reference"); 206 sprintf(oname,"Order"); 207 } 208 209 map* tmap=getMap(content->content,oname); 210 sprintf(sindex,"%d",index+1); 211 if((tmp1=getMap(content->content,xname))!=NULL && tmap!=NULL && strcasecmp(tmap->value,sindex)==0){ 212 213 if(getMap(content->content,icname)==NULL){ 214 215 fcontent=(char*)malloc((hInternet->ihandle[index].nDataLen+1)*sizeof(char)); 216 if(fcontent == NULL){ 217 return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); 218 } 219 size_t dwRead; 220 InternetReadFile(hInternet->ihandle[index], 221 (LPVOID)fcontent, 222 hInternet->ihandle[index].nDataLen, 223 &dwRead); 224 fcontent[hInternet->ihandle[index].nDataLen]=0; 225 fsize=hInternet->ihandle[index].nDataLen; 226 if(hInternet->ihandle[index].mimeType==NULL) 227 mimeType=strdup("none"); 228 else 229 mimeType=strdup(hInternet->ihandle[index].mimeType); 230 231 map* tmpMap=getMapOrFill(&content->content,vname,""); 232 free(tmpMap->value); 233 tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); 234 if(tmpMap->value==NULL){ 235 return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); 236 } 237 memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); 238 239 char ltmp1[256]; 240 sprintf(ltmp1,"%d",fsize); 241 map* tmp=getMapFromMaps(*m,"main","cacheDir"); 242 if(tmp!=NULL){ 243 char* md5str=getMd5(tmp1->value); 244 char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); 245 sprintf(fname,"%s/%s.zca",tmp->value,md5str); 246 addToMap(content->content,cname,fname); 247 free(fname); 248 } 249 addToMap(content->content,sname,ltmp1); 250 addToMap(content->content,mname,mimeType); 251 addToCache(*m,tmp1->value,fcontent,mimeType,fsize, NULL, 0); 252 free(fcontent); 253 free(mimeType); 254 index++; 255 256 } 276 if(content->child!=NULL){ 277 maps* cursor=content->child; 278 while(cursor!=NULL){ 279 readCurrentInput(m,&cursor,&index,hInternet); 280 cursor=cursor->next; 257 281 } 258 282 } 259 if(shouldClean>0){ 260 freeMap(&length); 261 free(length); 262 } 263 283 else 284 readCurrentInput(m,&content,&index,hInternet); 264 285 content=content->next; 265 } 266 286 } 267 287 } 268 288 return 0; … … 281 301 maps *oreq=getMaps(*m,"orequests"); 282 302 if(oreq==NULL){ 283 oreq=(maps*)malloc(MAPS_SIZE); 284 oreq->name=zStrdup("orequests"); 303 oreq=createMaps("orequests"); 285 304 oreq->content=createMap("value",url); 286 oreq->next=NULL;287 305 addMapsToMaps(m,oreq); 288 306 freeMaps(&oreq); … … 351 369 352 370 free(tmpMap->value); 353 354 371 tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); 355 372 if(tmpMap->value==NULL || fcontent == NULL)
Note: See TracChangeset
for help on using the changeset viewer.