Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

fo_cleanup.c

Go to the documentation of this file.
00001 
00010 /* {{{ Initial comments */
00011 /*
00012  * $LastChangedDate: 2004-04-01 18:34:17 +0200 (Thu, 01 Apr 2004) $
00013  * $LastChangedRevision: 50 $
00014  * $LastChangedBy: ckruse $
00015  *
00016  */
00017 /* }}} */
00018 
00019 /* {{{ Includes */
00020 #include "config.h"
00021 #include "defines.h"
00022 
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <dlfcn.h>
00026 
00027 #include <dirent.h>
00028 #include <sys/types.h>
00029 #include <sys/stat.h>
00030 
00031 /* socket includes */
00032 #include <sys/socket.h>
00033 #include <netdb.h>
00034 #include <unistd.h>
00035 #include <sys/un.h>
00036 
00037 #include "readline.h"
00038 #include "hashlib.h"
00039 #include "utils.h"
00040 #include "configparser.h"
00041 #include "cfcgi.h"
00042 #include "template.h"
00043 #include "clientlib.h"
00044 #include "readline.h"
00045 /* }}} */
00046 
00047 /* {{{ cleanup_deleted_file */
00054 int cleanup_deleted_file(int sock,struct stat *st,t_string *str) {
00055   FILE *fd;
00056   u_int64_t *field;
00057   u_int32_t i,nmem = 0,len;
00058   u_char buff[512];
00059   rline_t tsd;
00060   char *line;
00061   t_array ary;
00062 
00063   memset(&tsd,0,sizeof(tsd));
00064 
00065   array_init(&ary,sizeof(*field),NULL);
00066 
00067   nmem  = st->st_size / sizeof(*field);
00068   field = fo_alloc(NULL,nmem,sizeof(*field),FO_ALLOC_MALLOC);
00069 
00070   if((fd = fopen(str->content,"r")) == NULL) {
00071     free(field);
00072     perror("fopen");
00073     array_destroy(&ary);
00074     return EXIT_FAILURE;
00075   }
00076 
00077   fread(field,sizeof(*field),nmem,fd);
00078   fclose(fd);
00079 
00080   for(i=0;i<nmem;i++) {
00081     len = snprintf(buff,512,"STAT THREAD t%lld\n",field[i]);
00082     writen(sock,buff,len);
00083 
00084     if((line = readline(sock,&tsd)) == NULL) {
00085       free(field);
00086       array_destroy(&ary);
00087       perror("readline");
00088       return EXIT_FAILURE;
00089     }
00090 
00091     if(cf_strncmp(line,"404",3) != 0) {
00092       array_push(&ary,&field[i]);
00093     }
00094 
00095     free(line);
00096   }
00097 
00098   if((fd = fopen(str->content,"w")) == NULL) {
00099     perror("fopen");
00100     array_destroy(&ary);
00101     return EXIT_FAILURE;
00102   }
00103 
00104   fwrite(ary.array,ary.element_size,ary.elements,fd);
00105   fclose(fd);
00106 
00107   return EXIT_SUCCESS;
00108 }
00109 /* }}} */
00110 
00111 /* {{{ compare */
00112 #ifndef DOXYGEN
00113 int compare(t_cf_tree_dataset *a,t_cf_tree_dataset *b) {
00114   if(*((u_int64_t *)a->key) < *((u_int64_t *)b->key)) return -1;
00115   if(*((u_int64_t *)a->key) > *((u_int64_t *)b->key)) return 1;
00116 
00117   return 0;
00118 }
00119 #endif
00120 /* }}} */
00121 
00122 /* {{{ cleanup_visited_file */
00129 int cleanup_visited_file(int sock,struct stat *st,t_string *str) {
00130   t_cf_tree tree;
00131   u_char *line;
00132   rline_t rsd;
00133   u_int64_t mid;
00134   t_cf_tree_dataset data = { NULL, NULL };
00135 
00136   FILE *fd;
00137   u_int64_t *field;
00138   u_int32_t i,nmem = 0;
00139 
00140   t_array ary;
00141 
00142   cf_tree_init(&tree,compare,NULL);
00143   memset(&rsd,0,sizeof(rsd));
00144 
00145   data.data = NULL;
00146 
00147   writen(sock,"GET MIDLIST\n",12);
00148   line = readline(sock,&rsd);
00149 
00150   if(!line || cf_strncmp(line,"200",3)) {
00151     if(line) free(line);
00152     cf_tree_destroy(&tree);
00153     fprintf(stderr,"server returned not 200\n");
00154     return EXIT_FAILURE;
00155   }
00156 
00157   while((line = readline(sock,&rsd)) != NULL) {
00158     if(*line == '\n') {
00159       free(line);
00160       break;
00161     }
00162 
00163     mid       = strtoull(line+1,NULL,10);
00164     data.key  = memdup(&mid,sizeof(mid));
00165     data.data = NULL;
00166 
00167     cf_tree_insert(&tree,NULL,&data);
00168 
00169     free(line);
00170   }
00171 
00172   nmem  = st->st_size / sizeof(*field);
00173   field = fo_alloc(NULL,nmem,sizeof(*field),FO_ALLOC_MALLOC);
00174 
00175   if((fd = fopen(str->content,"r")) == NULL) {
00176     perror("fopen");
00177     cf_tree_destroy(&tree);
00178     free(field);
00179     return EXIT_FAILURE;
00180   }
00181 
00182   fread(field,sizeof(*field),nmem,fd);
00183   fclose(fd);
00184 
00185   array_init(&ary,sizeof(*field),NULL);
00186 
00187   for(i=0;i<nmem;i++) {
00188     data.key  = memdup(&field[i],sizeof(*field));
00189     data.data = NULL;
00190 
00191     if(cf_tree_find(&tree,tree.root,&data)) {
00192       array_push(&ary,&field[i]);
00193     }
00194   }
00195 
00196   if((fd = fopen(str->content,"w")) == NULL) {
00197     perror("fopen");
00198     cf_tree_destroy(&tree);
00199     array_destroy(&ary);
00200     free(field);
00201     return EXIT_FAILURE;
00202   }
00203   fwrite(ary.array,ary.element_size,ary.elements,fd);
00204   fclose(fd);
00205 
00206   free(field);
00207   cf_tree_destroy(&tree);
00208   array_destroy(&ary);
00209 
00210   return EXIT_SUCCESS;
00211 }
00212 /* }}} */
00213 
00214 /* {{{ cleanup_files */
00219 int cleanup_files(int sock) {
00220   t_name_value *v = cfg_get_value(&fo_default_conf,"ConfigDirectory");
00221   DIR *udir = opendir(v->values[0]);
00222   struct dirent *dp;
00223   t_string str;
00224   u_int32_t len = strlen(v->values[0]);
00225   int ret;
00226   struct stat st;
00227 
00228   if(!udir) {
00229     perror("opendir");
00230     return EXIT_FAILURE;
00231   }
00232 
00233   str_init(&str);
00234 
00235   while((dp = readdir(udir)) != NULL) {
00236     /* we don't want . and .. */
00237     if(*dp->d_name == '.') continue;
00238 
00239     str_chars_append(&str,v->values[0],len);
00240     str_char_append(&str,'/');
00241     str_chars_append(&str,dp->d_name,strlen(dp->d_name));
00242 
00243     /* we only want it if it is a directory */
00244     if(stat(str.content,&st) == -1) {
00245       str_cleanup(&str);
00246       continue;
00247     }
00248     if(!S_ISDIR(st.st_mode)) {
00249       str_cleanup(&str);
00250       continue;
00251     }
00252 
00253     str_chars_append(&str,"/deleted.dat",12);
00254 
00255     /* we have to check if there is a deleted.dat. If not, it is no valid user directory */
00256     if(stat(str.content,&st) == -1) {
00257       str_cleanup(&str);
00258       continue;
00259     }
00260 
00261     if((ret = cleanup_deleted_file(sock,&st,&str)) != EXIT_SUCCESS) {
00262       closedir(udir);
00263       str_cleanup(&str);
00264       return ret;
00265     }
00266 
00267     str.len -= 11;
00268     str_chars_append(&str,"/visited.dat",12);
00269 
00270     /* we have to check if there is a visited.dat. If not, it is no valid user directory */
00271     if(stat(str.content,&st) == -1) {
00272       str_cleanup(&str);
00273       continue;
00274     }
00275 
00276     if((ret = cleanup_visited_file(sock,&st,&str)) != EXIT_SUCCESS) {
00277       closedir(udir);
00278       str_cleanup(&str);
00279       return ret;
00280     }
00281 
00282     str_cleanup(&str);
00283   }
00284 
00285   closedir(udir);
00286 
00287   return EXIT_SUCCESS;
00288 }
00289 /* }}} */
00290 
00291 /* {{{ main */
00299 int main(int argc,char *argv[],char *envp[]) {
00300   int    sock,ret;
00301   t_array *cfgfiles;
00302   u_char *file;
00303   t_configfile conf,dconf;
00304 
00305   static const u_char *wanted[] = {
00306     "fo_default","fo_view"
00307   };
00308 
00309   if((cfgfiles = get_conf_file(wanted,2)) == NULL) {
00310     fprintf(stderr,"coult not get config files\n");
00311     return EXIT_FAILURE;
00312   }
00313 
00314   sock = 0;
00315 
00316   file = *((u_char **)array_element_at(cfgfiles,0));
00317   cfg_init_file(&dconf,file);
00318   free(file);
00319 
00320   file = *((u_char **)array_element_at(cfgfiles,1));
00321   cfg_init_file(&conf,file);
00322   free(file);
00323 
00324   cfg_register_options(&dconf,default_options);
00325   cfg_register_options(&conf,fo_view_options);
00326 
00327   if(read_config(&dconf,NULL) != 0 || read_config(&conf,NULL) != 0) {
00328     fprintf(stderr,"config file error!\n");
00329 
00330     cfg_cleanup_file(&conf);
00331     cfg_cleanup_file(&dconf);
00332 
00333     return EXIT_FAILURE;
00334   }
00335 
00336   if((sock = set_us_up_the_socket()) <= 0) {
00337     perror("sock");
00338     return EXIT_FAILURE;
00339   }
00340 
00341   ret = cleanup_files(sock);
00342 
00343   /* cleanup source */
00344   writen(sock,"QUIT\n",5);
00345   close(sock);
00346 
00347   cfg_cleanup(&fo_default_conf);
00348   cfg_cleanup_file(&dconf);
00349   cfg_cleanup(&fo_view_conf);
00350   cfg_cleanup_file(&conf);
00351 
00352   array_destroy(cfgfiles);
00353   free(cfgfiles);
00354 
00355   return ret;
00356 }
00357 /* }}} */
00358 
00359 /* eof */

Generated on Sun Apr 25 16:37:39 2004 for Classic Forum by doxygen 1.3.5