#include "include/conf.h"
#include "include/defs.h"
+static void file_index_to_date_index(const char *entry);
+static void date_index_to_file_index(const char *entry);
+
void make_index(void)
{
-
FILE *fp_ou, *fp_ou2, *fp_ou3, *fp_tmp, *fp_tmp2, *fp_tmp3;
DIR *dirp, *dirp2, *dirp3;
struct dirent *direntp;
char ftime[128];
char day[16], mon[16], year[40], hour[10];
char *str;
+ char tmp2[MAXLEN];
int cstatus;
int iyear, imonth, iday, ihour, iminute, isecond, idst;
- char y1[5], y2[5];
- char d1[3], d2[3];
- char m1[4], m2[4];
struct getwordstruct gwarea;
if(LastLog[0] != '\0') mklastlog(outdir);
if(debug) debuga("%s",text[53]);
- // Root dir
+ // convert any old report hierarchy
dirp = opendir(outdir);
while ((direntp = readdir( dirp )) != NULL) {
- if(strcmp(IndexTree,"date") == 0) {
- if(!isdigit(direntp->d_name[0]) && !isdigit(direntp->d_name[1])) continue;
- if(strlen(direntp->d_name) > 4) {
- bzero(y1,5);
- bzero(y2,5);
- bzero(m1,4);
- bzero(m2,4);
- bzero(d1,3);
- bzero(d2,3);
- if(strcmp(df,"u") == 0) {
- strncpy(y1,direntp->d_name,4);
- strncpy(m1,direntp->d_name+4,3);
- strncpy(d1,direntp->d_name+7,2);
- strncpy(y2,direntp->d_name+10,4);
- strncpy(m2,direntp->d_name+14,3);
- strncpy(d2,direntp->d_name+17,2);
- } else if(strcmp(df,"e") == 0) {
- strncpy(y1,direntp->d_name+5,4);
- strncpy(m1,direntp->d_name+2,3);
- strncpy(d1,direntp->d_name,2);
- strncpy(y2,direntp->d_name+15,4);
- strncpy(m2,direntp->d_name+12,3);
- strncpy(d2,direntp->d_name+10,2);
- }
- conv_month(m1);
- conv_month(m2);
- }
- sprintf(val1,"%s%s",outdir,y1);
- if(access(val1, R_OK) != 0) mkdir(val1,0755);
- if(strcmp(m1,m2) != 0) sprintf(val2,"%s/%s-%s",val1,m1,m2);
- else sprintf(val2,"%s/%s",val1,m1);
- if(access(val2, R_OK) != 0) mkdir(val2,0755);
- if(strcmp(d1,d2) != 0) sprintf(val3,"%s/%s-%s",val2,d1,d2);
- else sprintf(val3,"%s/%s",val2,d1);
- sprintf(val4,"%s%s",outdir,direntp->d_name);
- rename(val4,val3);
- sprintf(val5,"%s/images",val2);
- if(access(val5, R_OK) != 0) {
- sprintf(val5,"ln -s \"%simages\" \"%s/images\"",outdir,val2);
- cstatus=system(val5);
- if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
- fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
- fprintf(stderr, "SARG: command: %s\n",val5);
- exit(1);
- }
- }
- } else {
- if(!isdigit(direntp->d_name[0]) && !isdigit(direntp->d_name[1])) continue;
- if(strlen(direntp->d_name) == 4) {
- strcpy(y1,direntp->d_name);
- sprintf(val1,"%s%s",outdir,direntp->d_name);
- dirp2 = opendir(val1);
- while ((direntp2 = readdir( dirp2 )) != NULL) {
- if(!isdigit(direntp2->d_name[0]) && !isdigit(direntp2->d_name[1])) continue;
- sprintf(val2,"%s/%s",val1,direntp2->d_name);
- dirp3 = opendir(val2);
- while ((direntp3 = readdir( dirp3 )) != NULL) {
- if(!isdigit(direntp3->d_name[0]) && !isdigit(direntp3->d_name[1])) continue;
- bzero(newname,512);
- strcpy(warea,direntp2->d_name);
- if((str = strchr(warea,'-')) != 0) {
- *str++ = '\0';
- strcpy(m1,warea);
- strcpy(m2,str);
- conv_month_name(m1);
- conv_month_name(m2);
- } else {
- strcpy(m1,warea);
- conv_month_name(m1);
- strcpy(m2,m1);
- }
- strcpy(warea,direntp3->d_name);
- if((str = strchr(warea,'-')) != 0) {
- *str++ = '\0';
- strcpy(d1,warea);
- strcpy(d2,warea);
- } else {
- strcpy(d1,warea);
- strcpy(d2,warea);
- }
- if(strcmp(df,"u") == 0) sprintf(val4,"%s%s%s%s-%s%s%s",outdir,y1,m1,d1,y1,m2,d2);
- else if(strcmp(df,"e") == 0) sprintf(val4,"%s%s%s%s-%s%s%s",outdir,d1,m1,y1,d2,m2,y1);
- sprintf(val5,"%s%s/%s/%s",outdir,y1,direntp2->d_name,direntp3->d_name);
- if(rename(val5,val4)) {
- fprintf(stderr, "SARG: (index) rename error - %s\n",strerror(errno));
- exit(1);
- }
- }
- (void)rewinddir( dirp3 );
- (void)closedir( dirp3 );
- }
- (void)rewinddir( dirp2 );
- (void)closedir( dirp2 );
- }
-// sprintf(cmd,"rm -rf %s%s\n",outdir,direntp->d_name);
-// system(cmd);
+ if(isdigit(direntp->d_name[0]) && isdigit(direntp->d_name[1])) {
+ if(strcmp(IndexTree,"date") == 0)
+ file_index_to_date_index(direntp->d_name);
+ else
+ date_index_to_file_index(direntp->d_name);
}
}
- (void)rewinddir( dirp );
- (void)closedir( dirp );
if((fp_tmp=fopen(wdir_tmp,"w"))==NULL) {
fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp);
exit(1);
}
- dirp = opendir(outdir);
+ rewinddir(dirp);
while ((direntp = readdir( dirp )) != NULL) {
if(strcmp(IndexTree,"date") == 0) {
- if(strlen(direntp->d_name) > 4 || (!isdigit(direntp->d_name[0]) && !isdigit(direntp->d_name[1]))) continue;
+ if(strlen(direntp->d_name) > 4 || !isdigit(direntp->d_name[0]) || !isdigit(direntp->d_name[1]) ||
+ !isdigit(direntp->d_name[2]) || !isdigit(direntp->d_name[3])) continue;
fprintf(fp_tmp,"%s\t%s\n",direntp->d_name,get_size(outdir,direntp->d_name));
- continue;
} else {
- if(strstr(direntp->d_name,"-") == 0) continue;
- bzero(newname, 512);
+ if(strchr(direntp->d_name,'-') == 0) continue;
+ bzero(newname, sizeof(newname));
if(strcmp(df,"u") == 0) {
strncat(newname,direntp->d_name,4);
strncpy(month,direntp->d_name+4,3);
buildymd(day,mon,year,ftime);
fprintf(fp_tmp,"%s%02d%02d%02d;%s;%s;%s;%s;%s;%s\n",ftime, ihour, iminute, isecond, direntp->d_name, data, tuser, tbytes, media,newname);
}
- continue;
}
}
- if(fp_tmp) fclose(fp_tmp);
- if(strcmp(IndexTree,"file") == 0) {
- (void)rewinddir( dirp );
- (void)closedir( dirp );
- }
+ fclose(fp_tmp);
+ (void)closedir( dirp );
if(strcmp(IndexTree,"date") == 0) {
if(strcmp(IndexSortOrder,"A") == 0) sprintf(warea,"sort -k 1,1 \"%s\" -o \"%s\"", wdir_tmp, wdir_tmp2);
}
dirp2 = opendir(tmp2);
while ((direntp2 = readdir( dirp2 )) != NULL) {
- if(!isdigit(direntp2->d_name[0]) && !isdigit(direntp2->d_name[1])) continue;
+ if(!isdigit(direntp2->d_name[0]) || !isdigit(direntp2->d_name[1])) continue;
fprintf(fp_ou2,"%s\n",direntp2->d_name);
}
if(fp_ou2) fclose(fp_ou2);
- (void)rewinddir(dirp2);
(void)closedir(dirp2);
sprintf(wdir_tmp3,"%s%s/index.sort",outdir,tmp4);
if(strcmp(IndexSortOrder,"A") == 0) sprintf(csort,"sort -n \"%s\" -o \"%s\"", tmp3, wdir_tmp3);
write_html_trailer(fp_ou2);
if(fp_ou2) fclose(fp_ou2);
}
- (void)rewinddir(dirp);
- (void)closedir(dirp);
if(fp_tmp) fclose(fp_tmp);
unlink(tmp6);
unlink(wdir_tmp2);
write_html_trailer(fp_ou);
if(fp_ou) fclose(fp_ou);
}
+
+static void file_index_to_date_index(const char *entry)
+{
+ int y1, y2, d1, d2;
+ int i, j;
+ int ndirlen;
+ char m1[8], m2[8];
+ char olddir[MAXLEN], newdir[MAXLEN];
+
+ if(strlen(entry) < 19) return;
+
+ y1=0;
+ y2=0;
+ memset(m1,0,sizeof(m1));
+ memset(m2,0,sizeof(m2));
+ d1=0;
+ d2=0;
+ i=0;
+ if(strcmp(df,"u") == 0) {
+ for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
+ y1=y1*10+(entry[i++]-'0');
+ if (j!=4) return;
+ for (j=0 ; j<sizeof(m1)-1 && entry[i] && isalpha(entry[i]) ; j++)
+ m1[j]=entry[i++];
+ if (j!=3) return;
+ m1[j]='\0';
+ for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
+ d1=d1*10+(entry[i++]-'0');
+ if (j!=2) return;
+
+ if (entry[i++]!='-') return;
+
+ for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
+ y2=y2*10+(entry[i++]-'0');
+ if (j!=4) return;
+ for (j=0 ; j<sizeof(m2)-1 && entry[i] && isalpha(entry[i]) ; j++)
+ m2[j]=entry[i++];
+ if (j!=3) return;
+ m2[j]='\0';
+ for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
+ d2=d2*10+(entry[i++]-'0');
+ if (j!=2) return;
+ } else if(strcmp(df,"e") == 0) {
+ for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
+ d1=d1*10+(entry[i++]-'0');
+ if (j!=2) return;
+ for (j=0 ; j<sizeof(m1)-1 && entry[i] && isalpha(entry[i]) ; j++)
+ m1[j]=entry[i++];
+ if (j!=3) return;
+ m1[j]='\0';
+ for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
+ y1=y1*10+(entry[i++]-'0');
+ if (j!=4) return;
+
+ if (entry[i++]!='-') return;
+
+ for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
+ d2=d2*10+(entry[i++]-'0');
+ if (j!=2) return;
+ for (j=0 ; j<sizeof(m2)-1 && entry[i] && isalpha(entry[i]) ; j++)
+ m2[j]=entry[i++];
+ if (j!=3) return;
+ m2[j]='\0';
+ for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
+ y2=y2*10+(entry[i++]-'0');
+ if (j!=4) return;
+ } else
+ return;
+
+ conv_month(m1);
+ conv_month(m2);
+ ndirlen=sprintf(newdir,"%s%04d",outdir,y1);
+ if(access(newdir, R_OK) != 0) mkdir(newdir,0755);
+ if(strcmp(m1,m2) != 0) ndirlen+=sprintf(newdir+ndirlen,"/%s-%s",m1,m2);
+ else ndirlen+=sprintf(newdir+ndirlen,"/%s",m1);
+ if(access(newdir, R_OK) != 0) mkdir(newdir,0755);
+ if(d1!=d2) ndirlen+=sprintf(newdir+ndirlen,"/%02d-%02d",d1,d2);
+ else ndirlen+=sprintf(newdir+ndirlen,"/%02d",d1);
+
+ sprintf(olddir,"%s%s",outdir,entry);
+ if (rename(olddir,newdir)) {
+ fprintf(stderr, "SARG: (index) rename error from \"%s\" to \"%s\" - %s\n",olddir,newdir,strerror(errno));
+ exit(1);
+ }
+
+ /*! \bug The links to the images in the reports are broken after moving the directories
+ as the the HTML files are not at the right level for the images any more.
+ */
+}
+
+static void date_index_to_file_index(const char *entry)
+{
+ int y1, next;
+ int val1len;
+ int d1, d2;
+ int i, j;
+ char val1[MAXLEN];
+ char m1[8], m2[8];
+ char *str;
+ char newdir[MAXLEN], olddir[MAXLEN];
+ DIR *dirp2, *dirp3;
+ struct dirent *direntp2;
+ struct dirent *direntp3;
+
+ if(strlen(entry) != 4) return;
+
+ next=-1;
+ if (sscanf(entry,"%d%n",&y1,&next)!=1 || next<0 || entry[next]) return;
+
+ val1len=sprintf(val1,"%s%s",outdir,entry);
+ dirp2 = opendir(val1);
+ if (!dirp2) return;
+ while ((direntp2 = readdir( dirp2 )) != NULL) {
+ if(!isdigit(direntp2->d_name[0]) || !isdigit(direntp2->d_name[1])) continue;
+ i=0;
+ str=direntp2->d_name;
+ for (j=0 ; j<sizeof(m1) && str[i] && isdigit(str[i]) ; j++)
+ m1[j]=str[i++];
+ if (j>=sizeof(m1)) continue;
+ m1[j]='\0';
+ conv_month_name(m1);
+ if (str[i]=='-') {
+ i++;
+ for (j=0 ; j<sizeof(m2) && str[i] && isdigit(str[i]) ; j++)
+ m2[j]=str[i++];
+ if (j>=sizeof(m2)) continue;
+ m2[j]='\0';
+ conv_month_name(m2);
+ } else if (!str[i]) {
+ strcpy(m2,m1);
+ } else {
+ continue;
+ }
+
+ sprintf(val1+val1len,"/%s",direntp2->d_name);
+ dirp3 = opendir(val1);
+ if (!dirp3) continue;
+ while ((direntp3 = readdir( dirp3 )) != NULL) {
+ if(!isdigit(direntp3->d_name[0]) || !isdigit(direntp3->d_name[1])) continue;
+ i=0;
+ str=direntp3->d_name;
+ d1=0;
+ for (j=0 ; str[i] && isdigit(str[i]) ; j++)
+ d1=d1*10+(str[i++]-'0');
+ if (j!=2) continue;
+ if (str[i]=='-') {
+ i++;
+ d2=0;
+ for (j=0 ; str[i] && isdigit(str[i]) ; j++)
+ d2=d2*10+(str[i++]-'0');
+ if (j!=2) continue;
+ } else if (!str[i]) {
+ d2=d1;
+ } else {
+ continue;
+ }
+
+ if(strcmp(df,"u") == 0) sprintf(newdir,"%s%04d%s%02d-%04d%s%02d",outdir,y1,m1,d1,y1,m2,d2);
+ else if(strcmp(df,"e") == 0) sprintf(newdir,"%s%02d%s%04d-%02d%s%04d",outdir,d1,m1,y1,d2,m2,y1);
+ else continue;
+ sprintf(olddir,"%s%04d/%s/%s",outdir,y1,direntp2->d_name,direntp3->d_name);
+ if(rename(olddir,newdir)) {
+ fprintf(stderr, "SARG: (index) rename error from \"%s\" to \"%s\" - %s\n",olddir,newdir,strerror(errno));
+ exit(1);
+ }
+ }
+ (void)closedir( dirp3 );
+ }
+ (void)closedir( dirp2 );
+}
+
for(x=0; x<12 && strcmp(mtab1[x],month)!=0; x++);
sprintf(month,"%02d",x+1);
-
}
}
-void vrfydir(const char *per1, const char *addr, const char *site, const char *us, const char *form)
+static void copy_images(void)
{
FILE *img_in, *img_ou;
+ char images[512];
+ char imgdir[MAXLEN];
+ char srcfile[MAXLEN];
+ char dstfile[MAXLEN];
+ DIR *dirp;
+ struct dirent *direntp;
+ char buffer[MAXLEN];
+ size_t nread;
+ struct stat info;
+
+ if (snprintf(images,sizeof(images),"%simages",outdir)>=sizeof(images)) {
+ fprintf(stderr,"SARG: Cannot copy images to target directory %simages\n",outdir);
+ exit(1);
+ }
+ if (access(images,R_OK)!=0) {
+ mkdir(images,0755);
+ }
+
+ strcpy(imgdir,IMAGEDIR);
+ dirp = opendir(imgdir);
+ if(dirp==NULL) {
+ fprintf(stderr, "SARG: (util) %s %s: %s\n","Can't open directory", imgdir,strerror(errno));
+ return;
+ }
+ while ((direntp = readdir( dirp )) != NULL ){
+ if(direntp->d_name[0]=='.')
+ continue;
+ sprintf(srcfile,"%s/%s",imgdir,direntp->d_name);
+ if (stat(srcfile,&info)) {
+ fprintf(stderr,"SARG: Cannot stat \"%s\" - %s\n",srcfile,strerror(errno));
+ continue;
+ }
+ if (S_ISREG(info.st_mode)) {
+ sprintf(dstfile,"%s/%s",images,direntp->d_name);
+ img_in = fopen(srcfile, "rb");
+ if(img_in!=NULL) {
+ img_ou = fopen(dstfile, "wb");
+ if(img_ou!=NULL) {
+ while ((nread = fread(buffer,1,sizeof(buffer),img_in))>0) {
+ fwrite(buffer,1,nread,img_ou);
+ }
+ fclose(img_ou);
+ } else
+ fprintf(stderr,"SARG: (util): %s %s: %s\n", text[45]?text[45]:"Can't open/create file", dstfile, strerror(errno));
+ fclose(img_in);
+ } else
+ fprintf(stderr,"SARG: (util): %s %s: %s\n", text[45]?text[45]:"Can't open file", srcfile, strerror(errno));
+ }
+ }
+ (void) closedir(dirp);
+
+ return;
+}
+
+void vrfydir(const char *per1, const char *addr, const char *site, const char *us, const char *form)
+{
FILE *fp_ou;
int num=1, count=0;
- int c;
char wdir[MAXLEN];
char per2[MAXLEN];
char dirname2[MAXLEN];
- char images[512];
- DIR *dirp;
- struct dirent *direntp;
char y1[5], y2[5];
char d1[3], d2[3];
char m1[8], m2[8];
}
strcpy(dirname2,wdir);
- sprintf(images,"%simages",outdir);
- mkdir(images,0755);
sprintf(wdir,"%s/sarg-date",dirname);
if ((fp_ou = fopen(wdir, "wt")) == 0) {
fprintf(fp_ou,"%s %d\n",wdir,loctm->tm_isdst);
fclose(fp_ou);
- strcpy(per2,IMAGEDIR);
-
- dirp = opendir(per2);
- if(dirp==NULL) {
- fprintf(stderr, "SARG: (util) %s %s: %s\n","Can't open directory", per2,strerror(errno));
- return;
- }
- while ((direntp = readdir( dirp )) != NULL ){
- if(strncmp(direntp->d_name,".",1) == 0)
- continue;
- sprintf(val10,"%s/%s",per2,direntp->d_name);
- sprintf(val11,"%s/%s",images,direntp->d_name);
- img_in = fopen(val10, "rb");
- if(img_in!=NULL) {
- img_ou = fopen(val11, "wb");
- if(img_ou!=NULL) {
- while ((c = fgetc(img_in))!=EOF) {
- fputc(c,img_ou);
- }
- fclose(img_ou);
- } else
- fprintf(stderr,"SARG: (util): %s %s: %s\n", text[45]?text[45]:"Can't open/create file", val11, strerror(errno));
- fclose(img_in);
- } else
- fprintf(stderr,"SARG: (util): %s %s: %s\n", text[45]?text[45]:"Can't open file", val10, strerror(errno));
- }
- (void) rewinddir(dirp);
- (void) closedir(dirp);
-
- return;
-
-
+ copy_images();
}
-
void strip_latin(char *line)
{
int i,j;