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

fo_tid_index.c

Go to the documentation of this file.
00001 
00011 /* {{{ Initial comments */
00012 /*
00013  * $LastChangedDate$
00014  * $LastChangedRevision$
00015  * $LastChangedBy$
00016  *
00017  */
00018 /* }}} */
00019 
00020 /* {{{ Includes */
00021 #include "config.h"
00022 #include "defines.h"
00023 
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <dlfcn.h>
00027 #include <ctype.h>
00028 
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 
00032 #include <dirent.h>
00033 
00034 /* socket includes */
00035 #include <sys/socket.h>
00036 #include <netdb.h>
00037 #include <unistd.h>
00038 #include <sys/un.h>
00039 
00040 #include "hashlib.h"
00041 #include "utils.h"
00042 #include "configparser.h"
00043 #include "cfcgi.h"
00044 #include "template.h"
00045 #include "readline.h"
00046 #include "clientlib.h"
00047 #include "fo_tid_index.h"
00048 /* }}} */
00049 
00051 t_array idx;
00052 
00053 /* {{{ is_digit */
00059 int is_digit(struct dirent *ent) {
00060   register char *ptr = ent->d_name;
00061 
00062   for(;*ptr;ptr++) {
00063     if(!isdigit(*ptr)) return -1;
00064   }
00065 
00066   return 0;
00067 }
00068 /* }}} */
00069 
00070 /* {{{ is_thread */
00075 int is_thread(const char *path) {
00076   register char *ptr = (char *)path;
00077   register int dg = 0;
00078 
00079   if(*ptr++ != 't') return -1;
00080 
00081   for(;*ptr && isdigit(*ptr);ptr++) {
00082     dg = 1;
00083   }
00084 
00085   if(cf_strcmp(ptr,".xml") != 0) return -1;
00086   if(dg == 0) return -1;
00087 
00088   return 0;
00089 }
00090 /* }}} */
00091 
00092 /* {{{ index_month */
00098 void index_month(char *year,char *month) {
00099   t_name_value *apath = cfg_get_value(&fo_default_conf,"ArchivePath");
00100   char path[256],path1[256];
00101   u_int64_t min = (u_int64_t)-1,max = 0,tmp;
00102   t_tid_index midx;
00103   struct stat st;
00104 
00105   DIR *m;
00106   struct dirent *ent;
00107 
00108   (void)snprintf(path,256,"%s/%s/%s",apath->values[0],year,month);
00109   (void)snprintf(path,256,"%s/%s/%s/.leave",apath->values[0],year,month);
00110 
00111   if(stat(path,&st) == 0) return;
00112 
00113   if((m = opendir(path)) == NULL) {
00114     perror("opendir");
00115     exit(EXIT_FAILURE);
00116   }
00117 
00118   while((ent = readdir(m)) != NULL) {
00119     if(is_thread(ent->d_name) == -1) continue;
00120 
00121     tmp = strtoull(ent->d_name+1,NULL,10);
00122     if(tmp > max) max = tmp;
00123     if(tmp < min) min = tmp;
00124   }
00125 
00126   closedir(m);
00127 
00128   midx.year  = atoi(year);
00129   midx.month = atoi(month);
00130   midx.start = min;
00131   midx.end   = max;
00132 
00133   array_push(&idx,&midx);
00134 }
00135 /* }}} */
00136 
00137 /* {{{ do_year */
00142 void do_year(char *year) {
00143   t_name_value *apath = cfg_get_value(&fo_default_conf,"ArchivePath");
00144   char path[256];
00145 
00146   DIR *months;
00147   struct dirent *ent;
00148 
00149   (void)snprintf(path,256,"%s/%s",apath->values[0],year);
00150 
00151   if((months = opendir(path)) == NULL) {
00152     perror("opendir");
00153     exit(EXIT_FAILURE);
00154   }
00155 
00156   while((ent = readdir(months)) != NULL) {
00157     if(is_digit(ent) == -1) continue;
00158     index_month(year,ent->d_name);
00159   }
00160 
00161   closedir(months);
00162 
00163 }
00164 /* }}} */
00165 
00166 /* {{{ cmp */
00167 #ifndef DOXYGEN
00168 int cmp(const void *elem1,const void *elem2) {
00169   t_tid_index *id1 = (t_tid_index *)elem1;
00170   t_tid_index *id2 = (t_tid_index *)elem2;
00171 
00172   if(id1->start > id2->start) return 1;
00173   if(id1->start < id2->start) return -1;
00174 
00175   return 0;
00176 }
00177 #endif
00178 /* }}} */
00179 
00180 /* {{{ main */
00187 int main(int argc,char *argv[],char *envp[]) {
00188   t_array *cfgfiles;
00189   u_char *file;
00190   t_configfile dconf;
00191   t_name_value *ent,*idxfile;
00192   FILE *fd;
00193 
00194   DIR *years;
00195   struct dirent *year;
00196 
00197   static const u_char *wanted[] = {
00198     "fo_default"
00199   };
00200 
00201   if((cfgfiles = get_conf_file(wanted,1)) == NULL) {
00202     fprintf(stderr,"error getting config files\n");
00203     return EXIT_FAILURE;
00204   }
00205 
00206   file = array_element_at(cfgfiles,0);
00207   cfg_init_file(&dconf,file);
00208   cfg_register_options(&dconf,default_options);
00209   free(file);
00210 
00211   if(read_config(&dconf,NULL) != 0) {
00212     fprintf(stderr,"config file error!\n");
00213 
00214     cfg_cleanup_file(&dconf);
00215 
00216     return EXIT_FAILURE;
00217   }
00218 
00219   array_init(&idx,sizeof(t_tid_index),NULL);
00220 
00221   if((ent = cfg_get_value(&fo_default_conf,"ArchivePath")) == NULL) {
00222     fprintf(stderr,"error getting archive path\n");
00223     return EXIT_FAILURE;
00224   }
00225   if((idxfile = cfg_get_value(&fo_default_conf,"ThreadIndexFile")) == NULL) {
00226     fprintf(stderr,"error getting index file\n");
00227     return EXIT_FAILURE;
00228   }
00229 
00230   if((years = opendir(ent->values[0])) == NULL) {
00231     perror("opendir");
00232     return EXIT_FAILURE;
00233   }
00234 
00235   while((year = readdir(years)) != NULL) {
00236     if(is_digit(year) == -1) continue;
00237 
00238     do_year(year->d_name);
00239   }
00240 
00241   closedir(years);
00242 
00243   array_sort(&idx,cmp);
00244   if((fd = fopen(idxfile->values[0],"w")) == NULL) {
00245     perror("fopen(idxfile)");
00246     return EXIT_FAILURE;
00247   }
00248   fwrite(idx.array,idx.element_size,idx.elements,fd);
00249   fclose(fd);
00250 
00251   cfg_cleanup(&fo_default_conf);
00252   cfg_cleanup_file(&dconf);
00253 
00254   array_destroy(cfgfiles);
00255   free(cfgfiles);
00256 
00257   return EXIT_SUCCESS;
00258 }
00259 /* }}} */
00260 
00261 /* eof */

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