]> git.ipfire.org Git - thirdparty/sarg.git/blob - index.c
Add option to hide index column
[thirdparty/sarg.git] / index.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2010
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
24 *
25 */
26
27 #include "include/conf.h"
28 #include "include/defs.h"
29
30 static void make_date_index(void);
31 static void make_file_index(void);
32 static void file_index_to_date_index(const char *entry);
33 static void date_index_to_file_index(const char *entry);
34
35 void make_index(void)
36 {
37 DIR *dirp;
38 struct dirent *direntp;
39 char wdir[MAXLEN];
40
41 if(LastLog > 0) mklastlog(outdir);
42
43 if(Index == INDEX_NO) {
44 sprintf(wdir,"%sindex.html",outdir);
45 if(access(wdir, R_OK) == 0) unlink(wdir);
46 return;
47 }
48
49 if(debug) debuga(_("Making index.html\n"));
50
51 // convert any old report hierarchy
52 if ((dirp = opendir(outdir)) == NULL) {
53 debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno));
54 exit(EXIT_FAILURE);
55 }
56 while ((direntp = readdir( dirp )) != NULL) {
57 if(isdigit(direntp->d_name[0]) && isdigit(direntp->d_name[1])) {
58 if(IndexTree == INDEX_TREE_DATE)
59 file_index_to_date_index(direntp->d_name);
60 else
61 date_index_to_file_index(direntp->d_name);
62 }
63 }
64 closedir(dirp);
65
66 if(IndexTree == INDEX_TREE_DATE) {
67 make_date_index();
68 } else {
69 make_file_index();
70 }
71 }
72
73 static void make_date_index(void)
74 {
75 FILE *fp_ou, *fp_ou2, *fp_ou3;
76 DIR *dirp, *dirp2, *dirp3;
77 struct dirent *direntp;
78 struct dirent *direntp2;
79 struct dirent *direntp3;
80 char yearindex[MAXLEN];
81 char yeardir[MAXLEN];
82 char yearnum[10];
83 char monthindex[MAXLEN];
84 char monthdir[MAXLEN];
85 char monthname1[9], monthname2[9];
86 char nmonth[30];
87 char monthnum[10];
88 char dayindex[MAXLEN];
89 char daynum[10];
90 char title[80];
91 int yearsort[150];
92 int nyears;
93 int year;
94 int monthsort[144];
95 int nmonths;
96 int m1, m2, month;
97 int daysort[31*31];
98 int ndays;
99 int d1, d2, day;
100 int i, y, m, d;
101 int order;
102
103 sprintf(yearindex,"%sindex.html",outdir);
104
105 nyears=0;
106 if ((dirp = opendir(outdir)) == NULL) {
107 debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno));
108 exit(EXIT_FAILURE);
109 }
110 while ((direntp = readdir( dirp )) != NULL) {
111 if(strlen(direntp->d_name) > 4 || !isdigit(direntp->d_name[0]) || !isdigit(direntp->d_name[1]) ||
112 !isdigit(direntp->d_name[2]) || !isdigit(direntp->d_name[3])) continue;
113 year=atoi(direntp->d_name);
114 if (nyears>=sizeof(yearsort)/sizeof(yearsort[0])) {
115 /*
116 If too many years are listed in the directory, we ignore the earliest years. The yearsort array
117 is big enough to accomodate the most ambitious use of sarg but this safety is added to prevent
118 a crash should the directory be polluted by other entries.
119 */
120 if (year>yearsort[0]) {
121 for (i=1 ; i<nyears && year>yearsort[i] ; i++)
122 yearsort[i-1]=yearsort[i];
123 yearsort[i-1]=year;
124 }
125 } else {
126 for (i=nyears ; i>0 && year<yearsort[i-1] ; i--) {
127 yearsort[i]=yearsort[i-1];
128 }
129 yearsort[i]=year;
130 nyears++;
131 }
132 }
133 closedir( dirp );
134
135 order=(strcmp(IndexSortOrder,"A") == 0) ? 1 : -1;
136
137 if((fp_ou=fopen(yearindex,"w"))==NULL) {
138 debuga(_("(index) Cannot open file %s - %s\n"),yearindex,strerror(errno));
139 exit(EXIT_FAILURE);
140 }
141 write_html_header(fp_ou,0,ngettext("SARG report","SARG reports",nyears),HTML_JS_NONE);
142 close_html_header(fp_ou);
143 fputs("<div class=\"index\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_ou);
144 fprintf(fp_ou,"<tr><th class=\"header_l\">%s</th>",_("YEAR"));
145 if (IndexFields & INDEXFIELDS_DIRSIZE)
146 fprintf(fp_ou,"<th class=\"header_l\">%s</th>",_("SIZE"));
147 fputs("</tr>\n",fp_ou);
148 for (y=0 ; y<nyears ; y++) {
149 if (order>0)
150 year=yearsort[y];
151 else
152 year=yearsort[nyears-1-y];
153 sprintf(yearnum,"%04d",year);
154 fprintf(fp_ou,"<tr><td class=\"data2\"><a href=\"%s/index.html\">%s</a></td>",yearnum,yearnum);
155 if (IndexFields & INDEXFIELDS_DIRSIZE)
156 fprintf(fp_ou,"<td class=\"data2\">%s</td>",get_size(outdir,yearnum));
157 fputs("</tr>\n",fp_ou);
158 sprintf(yeardir,"%s%s",outdir,yearnum);
159 // Year dir
160 nmonths=0;
161 if ((dirp2 = opendir(yeardir)) == NULL) {
162 debuga(_("Failed to open directory %s - %s\n"),yeardir,strerror(errno));
163 exit(EXIT_FAILURE);
164 }
165 while ((direntp2 = readdir( dirp2 )) != NULL) {
166 if(!isdigit(direntp2->d_name[0]) || !isdigit(direntp2->d_name[1])) continue;
167 i=-1;
168 if (sscanf(direntp2->d_name,"%d%n",&m1,&i)!=1 || m1<=0 || m1>12 || i<0) continue;
169 if (direntp2->d_name[i]=='-') {
170 if (sscanf(direntp2->d_name+i+1,"%d",&m2)!=1 || m2<m1 || m2>12) continue;
171 } else if (direntp2->d_name[i]!='\0') {
172 continue;
173 } else {
174 m2=0;
175 }
176 if (nmonths>=sizeof(monthsort)/sizeof(monthsort[0])) {
177 debuga(_("Too many month directories in %s\nSupernumerary entries are ignored\n"),yeardir);
178 break;
179 }
180 month=m1*16+m2;
181 for (i=nmonths ; i>0 && month<monthsort[i-1] ; i--) {
182 monthsort[i]=monthsort[i-1];
183 }
184 monthsort[i]=month;
185 nmonths++;
186 }
187 closedir(dirp2);
188 sprintf(monthindex,"%s/index.html",yeardir);
189 if((fp_ou2=fopen(monthindex,"w"))==NULL) {
190 debuga(_("(index) Cannot open file %s - %s\n"),monthindex,strerror(errno));
191 exit(EXIT_FAILURE);
192 }
193 snprintf(title,sizeof(title),ngettext("SARG: report for %04d","SARG: reports for %04d",nmonths),year);
194 write_html_header(fp_ou2,1,title,HTML_JS_NONE);
195 close_html_header(fp_ou2);
196 fputs("<div class=\"index\"><table cellpadding=\"1\" cellspacing=\"2\">\n<tr><td></td><td></td></tr>\n",fp_ou2);
197 fprintf(fp_ou2,"<tr><th class=\"header_l\">%s/%s</th></tr>\n",_("YEAR"),_("MONTH"));
198 for (m=0 ; m<nmonths ; m++) {
199 if (order>0)
200 month=monthsort[m];
201 else
202 month=monthsort[nmonths-1-m];
203 m1=month / 16;
204 if(month % 16 != 0) {
205 m2=month % 16;
206 sprintf(monthnum,"%02d-%02d",m1,m2);
207 sprintf(monthname1,"%02d",m1);
208 sprintf(monthname2,"%02d",m2);
209 name_month(monthname1,sizeof(monthname1));
210 name_month(monthname2,sizeof(monthname2));
211 sprintf(nmonth,"%s-%s",monthname1,monthname2);
212 } else {
213 sprintf(nmonth,"%02d",m1);
214 sprintf(monthnum,"%02d",m1);
215 name_month(nmonth,sizeof(nmonth));
216 }
217 fprintf(fp_ou2,"<tr><td class=\"data2\"><a href=\"%s/index.html\">%s %s</a></td></tr>\n",monthnum,yearnum,nmonth);
218
219 sprintf(monthdir,"%s/%s",yeardir,monthnum);
220 // month dir
221 ndays=0;
222 if ((dirp3 = opendir(monthdir)) == NULL) {
223 debuga(_("Failed to open directory %s - %s\n"),monthdir,strerror(errno));
224 exit(EXIT_FAILURE);
225 }
226 while ((direntp3 = readdir( dirp3 )) != NULL) {
227 if(!isdigit(direntp3->d_name[0]) && !isdigit(direntp3->d_name[1])) continue;
228 i=-1;
229 if (sscanf(direntp3->d_name,"%d%n",&d1,&i)!=1 || d1<=0 || d1>31 || i<0) continue;
230 if (direntp3->d_name[i]=='-') {
231 if (sscanf(direntp3->d_name+i+1,"%d",&d2)!=1 || d2<d1 || d2>31) continue;
232 } else if (direntp3->d_name[i]!='\0') {
233 continue;
234 } else {
235 d2=0;
236 }
237 if (ndays>=sizeof(daysort)/sizeof(daysort[0])) {
238 debuga(_("Too many day directories in %s\nSupernumerary entries are ignored\n"),monthdir);
239 break;
240 }
241 day=d1*32+d2;
242 for (i=ndays ; i>0 && day<daysort[i-1] ; i--) {
243 daysort[i]=daysort[i-1];
244 }
245 daysort[i]=day;
246 ndays++;
247 }
248 closedir(dirp3);
249 sprintf(dayindex,"%s/index.html",monthdir);
250 if((fp_ou3=fopen(dayindex,"w"))==NULL) {
251 debuga(_("(index) Cannot open file %s - %s\n"),dayindex,strerror(errno));
252 exit(EXIT_FAILURE);
253 }
254 snprintf(title,sizeof(title),ngettext("SARG: report for %04d/%02d","SARG: reports for %04d/%02d",ndays),year,month);
255 write_html_header(fp_ou3,2,title,HTML_JS_NONE);
256 close_html_header(fp_ou3);
257 fputs("<div class=\"index\"><table cellpadding=\"1\" cellspacing=\"2\">\n<tr><td></td><td></td></tr>\n",fp_ou3);
258 fprintf(fp_ou3,"<tr><th class=\"header_l\">%s/%s/%s</th></tr>\n",_("YEAR"),_("MONTH"),_("DAYS"));
259 for (d=0 ; d<ndays ; d++) {
260 if (order>0)
261 day=daysort[d];
262 else
263 day=daysort[ndays-1-d];
264 d1=day / 32;
265 if(day % 32 != 0) {
266 d2=day % 32;
267 sprintf(daynum,"%02d-%02d",d1,d2);
268 } else {
269 sprintf(daynum,"%02d",d1);
270 }
271 fprintf(fp_ou3,"<tr><td class=\"data2\"><a href=\"%s/index.html\">%s %s %s</a></td></tr>\n",daynum,yearnum,nmonth,daynum);
272 }
273 fputs("</table></div>\n",fp_ou3);
274 if (write_html_trailer(fp_ou3)<0)
275 debuga(_("Write error in the index %s\n"),dayindex);
276 if (fclose(fp_ou3)==EOF)
277 debuga(_("Failed to close the index file %s - %s\n"),dayindex,strerror(errno));
278 }
279 fputs("</table></div>\n",fp_ou2);
280 if (write_html_trailer(fp_ou2)<0)
281 debuga(_("Write error in the index %s\n"),monthindex);
282 if (fclose(fp_ou2)==EOF)
283 debuga(_("Failed to close the index file %s - %s\n"),monthindex,strerror(errno));
284 }
285
286 fputs("</table></div>\n",fp_ou);
287 if (write_html_trailer(fp_ou)<0)
288 debuga(_("Write error in the index %s\n"),yearindex);
289 if (fclose(fp_ou)==EOF)
290 debuga(_("Failed to close the index file %s - %s\n"),yearindex,strerror(errno));
291 }
292
293 static void make_file_index(void)
294 {
295 #define MAX_CREATION_DATE 15
296 FILE *fp_ou;
297 DIR *dirp;
298 struct dirent *direntp;
299 char wdir[MAXLEN];
300 char data[80];
301 char ftime[128];
302 char day[6], mon[8], year[40], hour[10];
303 long long int tbytes;
304 long long int media;
305 int iyear, imonth, iday, ihour, iminute, isecond, idst;
306 int nsort;
307 int nallocated;
308 int order;
309 int i;
310 int tuser;
311 struct getwordstruct gwarea;
312 struct sortstruct
313 {
314 int year, month, day, sortnum;
315 char creationdate[MAX_CREATION_DATE];
316 char *dirname;
317 char date[60];
318 } **sortlist, *item, **tempsort;
319
320 sprintf(wdir,"%sindex.html",outdir);
321
322 order=(strcmp(IndexSortOrder,"A") == 0) ? 1 : -1;
323
324 if ((dirp = opendir(outdir)) == NULL) {
325 debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno));
326 exit(EXIT_FAILURE);
327 }
328
329 nsort=0;
330 nallocated=0;
331 sortlist=NULL;
332 while ((direntp = readdir( dirp )) != NULL) {
333 if (strchr(direntp->d_name,'-') == 0) continue;
334 item=malloc(sizeof(*item));
335 if (!item) {
336 debuga(_("not enough memory to sort the index\n"));
337 exit(EXIT_FAILURE);
338 }
339 if(strcmp(df,"u") == 0) {
340 item->year=atoi(direntp->d_name);
341 item->month=conv_month(direntp->d_name+4);
342 item->day=atoi(direntp->d_name+7);
343 } else {
344 item->year=atoi(direntp->d_name+5);
345 item->month=conv_month(direntp->d_name+2);
346 item->day=atoi(direntp->d_name);
347 }
348 item->sortnum=(item->year*16+item->month)*32+item->day;
349 obtdate(outdir,direntp->d_name,data);
350 if (sscanf(data,"%d-%d-%d %d:%d:%d %d",&iyear,&imonth,&iday,&ihour,&iminute,&isecond,&idst)==7) {
351 formatdate(data,sizeof(data),iyear,imonth,iday,ihour,iminute,isecond,idst);
352 snprintf(item->creationdate,sizeof(item->creationdate),"%04d%02d%02d%02d%02d%02d",iyear,imonth,iday,ihour,iminute,isecond);
353 } else {
354 /*
355 Old code to parse a date stored by sarg before 2.2.6.1 in the sarg-date file of each report directory.
356 */
357 getword_start(&gwarea,data);
358 if (getword_skip(16,&gwarea,' ')<0) {
359 debuga(_("Maybe you have a broken week day in your %s%s/sarg-date file\n"),outdir,direntp->d_name);
360 exit(EXIT_FAILURE);
361 }
362 if (getword_multisep(mon,sizeof(mon),&gwarea,' ')<0) {
363 debuga(_("Maybe you have a broken month in your %s%s/sarg-date file\n"),outdir,direntp->d_name);
364 exit(EXIT_FAILURE);
365 }
366 if (getword_multisep(day,sizeof(day),&gwarea,' ')<0) {
367 debuga(_("Maybe you have a broken day in your %s%s/sarg-date file\n"),outdir,direntp->d_name);
368 exit(EXIT_FAILURE);
369 }
370 if (getword_multisep(hour,sizeof(hour),&gwarea,' ')<0) {
371 debuga(_("Maybe you have a broken time in your %s%s/sarg-date file\n"),outdir,direntp->d_name);
372 exit(EXIT_FAILURE);
373 }
374 do {
375 if (getword_multisep(year,sizeof(year),&gwarea,' ')<0) {
376 debuga(_("Maybe you have a broken year in your %s%s/sarg-date file\n"),outdir,direntp->d_name);
377 exit(EXIT_FAILURE);
378 }
379 } while (year[0] && !isdigit(year[0])); //skip time zone information with spaces until the year is found
380 if (sscanf(hour,"%d:%d:%d",&ihour,&iminute,&isecond)!=3) {
381 debuga(_("Maybe you have a broken time in your %s%s/sarg-date file\n"),outdir,direntp->d_name);
382 exit(EXIT_FAILURE);
383 }
384 buildymd(day,mon,year,ftime);
385 snprintf(item->creationdate,sizeof(item->creationdate),"%s%02d%02d%02d",ftime, ihour, iminute, isecond);
386 }
387 item->dirname=strdup(direntp->d_name);
388 if (!item->dirname) {
389 debuga(_("Not enough memory to store the directory name \"%s\" in the index\n"),direntp->d_name);
390 exit(EXIT_FAILURE);
391 }
392 strncpy(item->date,data,sizeof(item->date));
393 if (nsort+1>nallocated) {
394 nallocated+=10;
395 tempsort=realloc(sortlist,nallocated*sizeof(*item));
396 if (!tempsort) {
397 debuga(_("not enough memory to sort the index\n"));
398 exit(EXIT_FAILURE);
399 }
400 sortlist=tempsort;
401 }
402 for (i=nsort ; i>0 ; i--) {
403 if (item->sortnum>sortlist[i-1]->sortnum) break;
404 if (item->sortnum==sortlist[i-1]->sortnum) {
405 if (strcmp(item->creationdate,sortlist[i-1]->creationdate)>=0) break;
406 }
407 sortlist[i]=sortlist[i-1];
408 }
409 sortlist[i]=item;
410 nsort++;
411 }
412
413 closedir( dirp );
414
415 if((fp_ou=fopen(wdir,"w"))==NULL) {
416 debuga(_("(index) Cannot open file %s\n"),wdir);
417 exit(EXIT_FAILURE);
418 }
419 write_html_header(fp_ou,0,ngettext("SARG report","SARG reports",nsort),HTML_JS_SORTTABLE);
420 close_html_header(fp_ou);
421 fputs("<div class=\"index\"><table cellpadding=\"1\" cellspacing=\"2\"",fp_ou);
422 if (SortTableJs[0]) fputs(" class=\"sortable\"",fp_ou);
423 fputs(">\n",fp_ou);
424 fprintf(fp_ou,"<thead><tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr></thead>\n",_("FILE/PERIOD"),_("CREATION DATE"),_("USERS"),_("BYTES"),_("AVERAGE"));
425 for (i=0 ; i<nsort ; i++) {
426 if (order>0)
427 item=sortlist[i];
428 else
429 item=sortlist[nsort-i-1];
430 tuser=obtuser(outdir,item->dirname);
431 obttotal(outdir,item->dirname,tuser,&tbytes,&media);
432 fputs("<tr><td class=\"data2\"",fp_ou);
433 if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%d\"",item->sortnum);
434 fprintf(fp_ou,"><a href='%s/%s'>%s</a></td>",item->dirname,ReplaceIndex,item->dirname);
435 fputs("<td class=\"data2\"",fp_ou);
436 if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%s\"",item->creationdate);
437 fprintf(fp_ou,">%s</td>",item->date);
438 fprintf(fp_ou,"<td class=\"data\">%d</td>",tuser);
439 fputs("<td class=\"data\"",fp_ou);
440 if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(int64_t)tbytes);
441 fprintf(fp_ou,">%s</td>",fixnum(tbytes,1));
442 fputs("<td class=\"data\"",fp_ou);
443 if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(int64_t)media);
444 fprintf(fp_ou,">%s</td></tr>\n",fixnum(media,1));
445 }
446 fputs("</table></div>\n",fp_ou);
447 if (write_html_trailer(fp_ou)<0)
448 debuga(_("Write error in the index %s\n"),wdir);
449 if (fclose(fp_ou)==EOF)
450 debuga(_("Failed to close the index file %s - %s\n"),wdir,strerror(errno));
451
452 if (sortlist) {
453 for (i=0 ; i<nsort ; i++) {
454 free(sortlist[i]->dirname);
455 free(sortlist[i]);
456 }
457 free(sortlist);
458 }
459 }
460
461 static void file_index_to_date_index(const char *entry)
462 {
463 int y1, y2, m1, m2, d1, d2;
464 int i, j;
465 int ndirlen;
466 int monthlen;
467 char sm1[8], sm2[8];
468 char olddir[MAXLEN], newdir[MAXLEN];
469
470 if(strlen(entry) < 19) return;
471
472 y1=0;
473 y2=0;
474 memset(sm1,0,sizeof(sm1));
475 memset(sm2,0,sizeof(sm2));
476 d1=0;
477 d2=0;
478 i=0;
479 if(strcmp(df,"u") == 0) {
480 for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
481 y1=y1*10+(entry[i++]-'0');
482 if (j!=4) return;
483 for (j=0 ; j<sizeof(sm1)-1 && entry[i] && isalpha(entry[i]) ; j++)
484 sm1[j]=entry[i++];
485 if (j!=3) return;
486 sm1[j]='\0';
487 for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
488 d1=d1*10+(entry[i++]-'0');
489 if (j!=2) return;
490
491 if (entry[i++]!='-') return;
492
493 for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
494 y2=y2*10+(entry[i++]-'0');
495 if (j!=4) return;
496 for (j=0 ; j<sizeof(sm2)-1 && entry[i] && isalpha(entry[i]) ; j++)
497 sm2[j]=entry[i++];
498 if (j!=3) return;
499 sm2[j]='\0';
500 for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
501 d2=d2*10+(entry[i++]-'0');
502 if (j!=2) return;
503 } else if(strcmp(df,"e") == 0) {
504 for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
505 d1=d1*10+(entry[i++]-'0');
506 if (j!=2) return;
507 for (j=0 ; j<sizeof(sm1)-1 && entry[i] && isalpha(entry[i]) ; j++)
508 sm1[j]=entry[i++];
509 if (j!=3) return;
510 sm1[j]='\0';
511 for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
512 y1=y1*10+(entry[i++]-'0');
513 if (j!=4) return;
514
515 if (entry[i++]!='-') return;
516
517 for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
518 d2=d2*10+(entry[i++]-'0');
519 if (j!=2) return;
520 for (j=0 ; j<sizeof(sm2)-1 && entry[i] && isalpha(entry[i]) ; j++)
521 sm2[j]=entry[i++];
522 if (j!=3) return;
523 sm2[j]='\0';
524 for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
525 y2=y2*10+(entry[i++]-'0');
526 if (j!=4) return;
527 } else
528 return;
529
530 m1=conv_month(sm1);
531 m2=conv_month(sm2);
532 ndirlen=sprintf(newdir,"%s%04d",outdir,y1);
533 if(access(newdir, R_OK) != 0) mkdir(newdir,0755);
534 if(m1 != m2) ndirlen+=sprintf(newdir+ndirlen,"/%02d-%02d",m1,m2);
535 else ndirlen+=sprintf(newdir+ndirlen,"/%02d",m1);
536 if(access(newdir, R_OK) != 0) mkdir(newdir,0755);
537 monthlen=ndirlen;
538 if(d1!=d2) ndirlen+=sprintf(newdir+ndirlen,"/%02d-%02d",d1,d2);
539 else ndirlen+=sprintf(newdir+ndirlen,"/%02d",d1);
540
541 sprintf(olddir,"%s%s",outdir,entry);
542 if (rename(olddir,newdir)) {
543 debuga(_("(index) rename error from \"%s\" to \"%s\" - %s\n"),olddir,newdir,strerror(errno));
544 exit(EXIT_FAILURE);
545 }
546
547 strcpy(newdir+monthlen,"/images");
548 if(access(newdir, R_OK) != 0) {
549 #ifdef HAVE_SYMLINK
550 char linkdir[MAXLEN];
551
552 sprintf(linkdir,"%simages",outdir);
553 if (symlink(linkdir,newdir)) {
554 debuga(_("failed to create link \"%s\" to \"%s\" - %s\n"),linkdir,newdir,strerror(errno));
555 exit(EXIT_FAILURE);
556 }
557 #else
558 char cmd[MAXLEN];
559 int cstatus;
560
561 sprintf(cmd,"ln -s \"%simages\" \"%s/images\"",outdir,newdir);
562 cstatus=system(cmd);
563 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
564 debuga(_("command return status %d\n"),WEXITSTATUS(cstatus));
565 debuga(_("command: %s\n"),cmd);
566 exit(EXIT_FAILURE);
567 }
568 #endif
569 }
570 }
571
572 static void date_index_to_file_index(const char *entry)
573 {
574 int y1, next;
575 int m1, m2;
576 int d1, d2;
577 int val1len;
578 int i, j;
579 char val1[MAXLEN];
580 const char *sm1, *sm2;
581 char *str;
582 char newdir[MAXLEN], olddir[MAXLEN];
583 DIR *dirp2, *dirp3;
584 struct dirent *direntp2;
585 struct dirent *direntp3;
586
587 if(strlen(entry) != 4) return;
588
589 next=-1;
590 if (sscanf(entry,"%d%n",&y1,&next)!=1 || next<0 || entry[next]) return;
591
592 val1len=snprintf(val1,sizeof(val1),"%s%s",outdir,entry);
593 dirp2 = opendir(val1);
594 if (!dirp2) return;
595 while ((direntp2 = readdir( dirp2 )) != NULL) {
596 if(!isdigit(direntp2->d_name[0]) || !isdigit(direntp2->d_name[1])) continue;
597 i=0;
598 str=direntp2->d_name;
599 m1=0;
600 for (j=0 ; j<2 && str[i] && isdigit(str[i]) ; j++)
601 m1=(m1*10)+(str[i++]-'0');
602 if (j>=2) continue;
603 sm1=conv_month_name(m1);
604 if (str[i]=='-') {
605 i++;
606 m2=0;
607 for (j=0 ; j<2 && str[i] && isdigit(str[i]) ; j++)
608 m2=(m2*10)+(str[i++]-'0');
609 if (j>=2) continue;
610 sm2=conv_month_name(m2);
611 } else if (!str[i]) {
612 sm2=sm1;
613 } else {
614 continue;
615 }
616
617 sprintf(val1+val1len,"/%s",direntp2->d_name);
618 dirp3 = opendir(val1);
619 if (!dirp3) continue;
620 while ((direntp3 = readdir( dirp3 )) != NULL) {
621 if(!isdigit(direntp3->d_name[0]) || !isdigit(direntp3->d_name[1])) continue;
622 i=0;
623 str=direntp3->d_name;
624 d1=0;
625 for (j=0 ; str[i] && isdigit(str[i]) ; j++)
626 d1=d1*10+(str[i++]-'0');
627 if (j!=2) continue;
628 if (str[i]=='-') {
629 i++;
630 d2=0;
631 for (j=0 ; str[i] && isdigit(str[i]) ; j++)
632 d2=d2*10+(str[i++]-'0');
633 if (j!=2) continue;
634 } else if (!str[i]) {
635 d2=d1;
636 } else {
637 continue;
638 }
639
640 if(strcmp(df,"u") == 0) sprintf(newdir,"%s%04d%s%02d-%04d%s%02d",outdir,y1,sm1,d1,y1,sm2,d2);
641 else if(strcmp(df,"e") == 0) sprintf(newdir,"%s%02d%s%04d-%02d%s%04d",outdir,d1,sm1,y1,d2,sm2,y1);
642 else continue;
643 sprintf(olddir,"%s%04d/%s/%s",outdir,y1,direntp2->d_name,direntp3->d_name);
644 if(rename(olddir,newdir)) {
645 debuga(_("(index) rename error from \"%s\" to \"%s\" - %s\n"),olddir,newdir,strerror(errno));
646 exit(EXIT_FAILURE);
647 }
648 }
649 closedir( dirp3 );
650 }
651 closedir( dirp2 );
652
653 /*!
654 \bug The links to the images in the reports are broken after moving the directories
655 as the the HTML files are not at the right level for the images any more.
656 */
657 }
658