Index: branches/prototype-v0/zoo-project/zoo-kernel/caching.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/caching.c	(revision 877)
+++ branches/prototype-v0/zoo-project/zoo-kernel/caching.c	(revision 880)
@@ -67,16 +67,18 @@
  */
 char* getMd5f(char* file){
-  EVP_MD_CTX md5ctx;
+  EVP_MD_CTX *md5ctx=EVP_MD_CTX_create();
   char* fresult=(char*)malloc((EVP_MAX_MD_SIZE+1)*sizeof(char));
   unsigned char result[EVP_MAX_MD_SIZE];
   unsigned int len;
   int bytes;
-  unsigned char data[1024];
+  int dlen=65536;
+  unsigned char data[dlen+1];
   FILE *inFile = fopen (file, "rb");
-  EVP_DigestInit(&md5ctx, EVP_md5());
-  while ((bytes = fread (data, 1, 1024, inFile)) != 0)
-    EVP_DigestUpdate(&md5ctx, data, bytes);
-  EVP_DigestFinal_ex(&md5ctx,result,&len);
-  EVP_MD_CTX_cleanup(&md5ctx);
+  EVP_DigestInit(md5ctx, EVP_md5());
+  while ((bytes = fread (data, sizeof(unsigned char), dlen, inFile)) != 0)
+    EVP_DigestUpdate(md5ctx, data, bytes);
+  EVP_DigestFinal_ex(md5ctx,result,&len);
+  EVP_MD_CTX_cleanup(md5ctx);
+  EVP_MD_CTX_destroy(md5ctx);
   int i;
   for(i = 0; i < len; i++){
@@ -162,4 +164,22 @@
 
 /**
+ * Store MD5 of the content of a file
+ * @file char* the full path of the file
+ */
+int storeMd5(char* file){
+  char* storage=zStrdup(file);
+  char* md5fstr=getMd5f(file);
+  storage[strlen(storage)-2]='m';
+  storage[strlen(storage)-1]='d';
+  FILE* fo=fopen(storage,"w+");
+  if(fo==NULL)
+    return 1;
+  fwrite(md5fstr,sizeof(char),strlen(md5fstr),fo);
+  free(md5fstr);
+  fclose(fo);
+  return 0;
+}
+
+/**
  * Cache a file for a given request.
  * For each cached file, the are two files stored, a .zca and a .zcm containing
@@ -177,5 +197,4 @@
   map* tmp=getMapFromMaps(conf,"main","cacheDir");
   char contentr[4096];
-  char* md5fstr=NULL;
   int cred=0;
   if(tmp!=NULL){
@@ -184,4 +203,16 @@
     free(myRequest);
     char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6));
+    // Store md5
+    char* md5fstr=getMd5f(filename);
+    sprintf(fname,"%s/%s.zmd",tmp->value,md5str);
+    FILE* fo=fopen(fname,"w+");
+#ifdef DEBUG
+    fprintf(stderr,"filename: %s\n",filename);
+    fprintf(stderr,"MD5: %s\n",md5fstr);
+#endif
+    fwrite(md5fstr,sizeof(char),strlen(md5fstr),fo);
+    free(md5fstr);
+    fclose(fo);
+    
     sprintf(fname,"%s/%s.zca",tmp->value,md5str);
     zooLock* lck=lockFile(conf,fname,'w');
@@ -192,5 +223,6 @@
 #endif
       FILE* fi=fopen(filename,"rb");
-      FILE* fo=fopen(fname,"w+");
+      sprintf(fname,"%s/%s.zca",tmp->value,md5str);
+      fo=fopen(fname,"w+");
       if(fo==NULL){
 #ifdef DEBUG
@@ -236,19 +268,8 @@
       fclose(fo);
 
-      // Store md5
-      sprintf(fname,"%s/%s.zca",tmp->value,md5str);
-      md5fstr=getMd5f(fname);
-      sprintf(fname,"%s/%s.zmd",tmp->value,md5str);
-      fo=fopen(fname,"w+");
-#ifdef DEBUG
-      fprintf(stderr,"MD5: %s\n",md5fstr);
-#endif
-      fwrite(md5fstr,sizeof(char),strlen(md5fstr),fo);
-      free(md5fstr);
-      fclose(fo);
-
       free(md5str);
-      free(fname);
-    }
+
+    }
+    free(fname);
   }
 }
@@ -342,5 +363,4 @@
     }
     else{
-
       char* myRequest=getFilenameForRequest(conf,request);
       char* md5str=getMd5(myRequest);
@@ -436,5 +456,5 @@
 
       if(getMap(content->content,icname)==NULL) {
-	if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){
+	if(memUse==NULL || strcasecmp(memUse->value,"load")==0){
 	  fcontent=(char*)malloc((hInternet->ihandle[*index].nDataLen+1)*sizeof(char));
 	  if(fcontent == NULL){
@@ -456,5 +476,5 @@
 	
 	map* tmpMap=getMapOrFill(&(*in)->content,vname,"");
-	if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){
+	if(memUse==NULL || strcasecmp(memUse->value,"load")==0){
 	  free(tmpMap->value);
 	  tmpMap->value=(char*)malloc((fsize+1)*sizeof(char));
@@ -518,5 +538,5 @@
 	addToMap((*in)->content,sname,ltmp1);
 	addToMap((*in)->content,mname,mimeType);
-	if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){
+	if(memUse==NULL || strcasecmp(memUse->value,"load")==0){
 	  addToCache(*m,request,fcontent,mimeType,fsize, NULL, 0);
 	  free(fcontent);
@@ -628,5 +648,5 @@
 	return -1;
       fsize=f_status.st_size;
-      if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){
+      if(memUse==NULL || strcasecmp(memUse->value,"load")==0){
 	fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
 	FILE* f=fopen(cached,"rb");
@@ -665,5 +685,5 @@
 
   map* tmpMap=getMapOrFill(content,"value","");
-  if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){
+  if(memUse==NULL || strcasecmp(memUse->value,"load")==0){
     free(tmpMap->value);
     tmpMap->value=(char*)malloc((fsize+1)*sizeof(char));
@@ -677,5 +697,5 @@
   addToMap(*content,"size",ltmp1);
   if(cached==NULL){
-    if(memUse!=NULL && strcasecmp(memUse->value,"load")==0)
+    if(memUse==NULL || strcasecmp(memUse->value,"load")==0)
       addToCache(*m,url,fcontent,mimeType,fsize, NULL, 0);
     else
