]> git.ipfire.org Git - thirdparty/sarg.git/blame - index.c
Restore the creation of the link to the image directory lost during the rewritting...
[thirdparty/sarg.git] / index.c
CommitLineData
25697a35 1/*
c37945ed
FM
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
94ff9470 4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
25697a35
GS
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26#include "include/conf.h"
5f3cfd1d 27#include "include/defs.h"
25697a35 28
06ced858
FM
29static void file_index_to_date_index(const char *entry);
30static void date_index_to_file_index(const char *entry);
31
32e71fa4 32void make_index(void)
25697a35 33{
491b862f
GS
34 FILE *fp_ou, *fp_ou2, *fp_ou3, *fp_tmp, *fp_tmp2, *fp_tmp3;
35 DIR *dirp, *dirp2, *dirp3;
25697a35 36 struct dirent *direntp;
491b862f
GS
37 struct dirent *direntp2;
38 struct dirent *direntp3;
25697a35
GS
39 char html[MAXLEN];
40 char wdir[MAXLEN];
41 char wdir_tmp[MAXLEN];
42 char wdir_tmp2[MAXLEN];
491b862f 43 char wdir_tmp3[MAXLEN];
25697a35
GS
44 char newname[512];
45 char month[4];
46 char period[80];
47 char data[80];
48 char tuser[20];
49 char tbytes[20];
50 char media[20];
51 char ftime[128];
fd4dbc54 52 char day[16], mon[16], year[40], hour[10];
9c7c6346 53 char *str;
06ced858 54 char tmp2[MAXLEN];
456d78a5 55 int cstatus;
0a4e18e1 56 int iyear, imonth, iday, ihour, iminute, isecond, idst;
9c7c6346 57 struct getwordstruct gwarea;
25697a35 58
32e71fa4 59 if(LastLog[0] != '\0') mklastlog(outdir);
25697a35
GS
60
61 sprintf(wdir,"%sindex.html",outdir);
62 sprintf(wdir_tmp,"%sindex.unsort",outdir);
63 sprintf(wdir_tmp2,"%sindex.sort",outdir);
64 strcpy(hbc1,"class=\"header\"");
65
66 if(strcmp(Index,"no") == 0) {
d6e703cc 67 if(access(wdir, R_OK) == 0) unlink(wdir);
25697a35
GS
68 return;
69 }
70
d2fe0c32 71 if(debug) debuga("%s",text[53]);
25697a35 72
06ced858 73 // convert any old report hierarchy
491b862f
GS
74 dirp = opendir(outdir);
75 while ((direntp = readdir( dirp )) != NULL) {
06ced858
FM
76 if(isdigit(direntp->d_name[0]) && isdigit(direntp->d_name[1])) {
77 if(strcmp(IndexTree,"date") == 0)
78 file_index_to_date_index(direntp->d_name);
79 else
80 date_index_to_file_index(direntp->d_name);
491b862f 81 }
25697a35
GS
82 }
83
84 if((fp_tmp=fopen(wdir_tmp,"w"))==NULL) {
85 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp);
86 exit(1);
87 }
88
06ced858 89 rewinddir(dirp);
491b862f
GS
90 while ((direntp = readdir( dirp )) != NULL) {
91 if(strcmp(IndexTree,"date") == 0) {
06ced858
FM
92 if(strlen(direntp->d_name) > 4 || !isdigit(direntp->d_name[0]) || !isdigit(direntp->d_name[1]) ||
93 !isdigit(direntp->d_name[2]) || !isdigit(direntp->d_name[3])) continue;
120d768c 94 fprintf(fp_tmp,"%s\t%s\n",direntp->d_name,get_size(outdir,direntp->d_name));
491b862f 95 } else {
06ced858
FM
96 if(strchr(direntp->d_name,'-') == 0) continue;
97 bzero(newname, sizeof(newname));
491b862f
GS
98 if(strcmp(df,"u") == 0) {
99 strncat(newname,direntp->d_name,4);
100 strncpy(month,direntp->d_name+4,3);
101 } else {
102 strncat(newname,direntp->d_name+5,4);
103 strncpy(month,direntp->d_name+2,3);
104 }
105 month[3]='\0';
106 conv_month(month);
107 strcat(newname,month);
d6e703cc
FM
108 if(strcmp(df,"u") == 0) strncat(newname,direntp->d_name+7,2);
109 else strncat(newname,direntp->d_name,2);
491b862f
GS
110 obtdate(outdir,direntp->d_name,data);
111 obtuser(outdir,direntp->d_name,tuser);
112 obttotal(outdir,direntp->d_name,tbytes,tuser,media);
a1de61fe
FM
113 if (sscanf(data,"%d-%d-%d %d:%d:%d %d",&iyear,&imonth,&iday,&ihour,&iminute,&isecond,&idst)==7) {
114 formatdate(data,sizeof(data),iyear,imonth,iday,ihour,iminute,isecond,idst);
fd4dbc54
FM
115 fprintf(fp_tmp,"%04d%02d%02d%02d%02d%02d;%s;%s;%s;%s;%s;%s\n",iyear,imonth,iday,ihour,iminute,isecond, direntp->d_name, data, tuser, tbytes, media,newname);
116 } else {
117 /*
118 Old code to parse a date stored by sarg before 2.2.6.1 in the sarg-date file of each report directory.
119 */
9c7c6346
FM
120 getword_start(&gwarea,data);
121 if (getword_multisep(mon,sizeof(mon),&gwarea,' ')<0) {
fd4dbc54
FM
122 printf("SARG: Maybe you have a broken week day in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
123 exit(1);
124 }
9c7c6346 125 if (getword_multisep(mon,sizeof(mon),&gwarea,' ')<0) {
fd4dbc54
FM
126 printf("SARG: Maybe you have a broken month in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
127 exit(1);
128 }
9c7c6346 129 if (getword_multisep(day,sizeof(day),&gwarea,' ')<0) {
fd4dbc54
FM
130 printf("SARG: Maybe you have a broken day in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
131 exit(1);
132 }
9c7c6346 133 if (getword_multisep(hour,sizeof(hour),&gwarea,' ')<0) {
fd4dbc54
FM
134 printf("SARG: Maybe you have a broken time in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
135 exit(1);
136 }
137 do {
9c7c6346 138 if (getword_multisep(year,sizeof(year),&gwarea,' ')<0) {
fd4dbc54
FM
139 printf("SARG: Maybe you have a broken year in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
140 exit(1);
141 }
142 } while (year[0] && !isdigit(year[0])); //skip time zone information with spaces until the year is found
143 strcpy(html,hour);
9c7c6346
FM
144 if (sscanf(hour,"%d:%d:%d",&ihour,&iminute,&isecond)!=3) {
145 printf("SARG: Maybe you have a broken time in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
fd4dbc54
FM
146 exit(1);
147 }
148 buildymd(day,mon,year,ftime);
9c7c6346 149 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);
4bcb77cf 150 }
491b862f 151 }
25697a35 152 }
94ff9470 153
06ced858
FM
154 fclose(fp_tmp);
155 (void)closedir( dirp );
25697a35 156
491b862f 157 if(strcmp(IndexTree,"date") == 0) {
9a2efbd0
FM
158 if(strcmp(IndexSortOrder,"A") == 0) sprintf(warea,"sort -k 1,1 \"%s\" -o \"%s\"", wdir_tmp, wdir_tmp2);
159 else sprintf(warea,"sort -r -k 1,1 \"%s\" -o \"%s\"", wdir_tmp, wdir_tmp2);
456d78a5
FM
160 cstatus=system(warea);
161 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
162 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
163 fprintf(stderr, "SARG: sort command: %s\n",warea);
164 exit(1);
165 }
491b862f
GS
166 if((fp_tmp=fopen(wdir_tmp2,"r"))==NULL) {
167 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp2);
456d78a5 168 fprintf(stderr, "SARG: sort command: %s\n",warea);
491b862f
GS
169 exit(1);
170 }
456d78a5 171 unlink(wdir_tmp);
491b862f
GS
172 if((fp_ou=fopen(wdir,"w"))==NULL) {
173 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir);
174 exit(1);
175 }
176 write_html_header(fp_ou, ".");
d6e703cc 177 fprintf(fp_ou,"<tr><th %s>%s</th><th %s>%s</th></tr>\n",hbc1,text[130],hbc1,text[132]);
4157aa09 178 while(fgets(wwork1,sizeof(wwork1),fp_tmp)!=NULL) {
9c7c6346
FM
179 getword_start(&gwarea,wwork1);
180 if (getword(tmp4,sizeof(tmp4),&gwarea,'\t')<0) {
4157aa09 181 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
4bcb77cf
FM
182 exit(1);
183 }
e6414a9d 184 fprintf(fp_ou,"<tr><td class=\"data2\"><a href=\"%s/index.html\">%s</a></td><td class=\"data2\">%s</td></tr>\n",tmp4,tmp4,gwarea.current);
491b862f
GS
185 sprintf(tmp2,"%s%s",outdir,tmp4);
186 sprintf(tmp3,"%s%s/index.unsort",outdir,tmp4);
187 // Year dir
188 if((fp_ou2=fopen(tmp3,"w"))==NULL) {
189 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],tmp3);
190 exit(1);
191 }
192 dirp2 = opendir(tmp2);
193 while ((direntp2 = readdir( dirp2 )) != NULL) {
06ced858 194 if(!isdigit(direntp2->d_name[0]) || !isdigit(direntp2->d_name[1])) continue;
d6e703cc 195 fprintf(fp_ou2,"%s\n",direntp2->d_name);
491b862f
GS
196 }
197 if(fp_ou2) fclose(fp_ou2);
491b862f
GS
198 (void)closedir(dirp2);
199 sprintf(wdir_tmp3,"%s%s/index.sort",outdir,tmp4);
9a2efbd0
FM
200 if(strcmp(IndexSortOrder,"A") == 0) sprintf(csort,"sort -n \"%s\" -o \"%s\"", tmp3, wdir_tmp3);
201 else sprintf(csort,"sort -n -r \"%s\" -o \"%s\"", tmp3, wdir_tmp3);
456d78a5
FM
202 cstatus=system(csort);
203 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
204 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
205 fprintf(stderr, "SARG: sort command: %s\n",csort);
206 exit(1);
207 }
491b862f
GS
208 unlink(tmp3);
209 if((fp_tmp2=fopen(wdir_tmp3,"r"))==NULL) {
210 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp3);
211 exit(1);
212 }
213 sprintf(tmp3,"%s%s/index.html",outdir,tmp4);
214 if((fp_ou2=fopen(tmp3,"w"))==NULL) {
215 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir);
216 exit(1);
217 }
218 write_html_header(fp_ou2,"..");
d6e703cc 219 fprintf(fp_ou2,"<tr><th %s>%s/%s</th></tr>\n",hbc1,text[130],text[131]);
9c7c6346
FM
220 while(fgets(wwork1,sizeof(wwork1),fp_tmp2)!=NULL) {
221 fixendofline(wwork1);
491b862f 222 strcpy(tmp5,wwork1);
9c7c6346
FM
223 if((str = strchr(tmp5,'-')) != 0) {
224 *str++ = '\0';
225 strcpy(tmp6,str);
4bcb77cf 226 name_month(tmp5,sizeof(tmp5));
9c7c6346
FM
227 name_month(tmp6,sizeof(tmp6));
228 sprintf(nmonth,"%s-%s",tmp5,tmp6);
491b862f
GS
229 } else {
230 strcpy(nmonth,tmp5);
4bcb77cf 231 name_month(nmonth,sizeof(nmonth));
491b862f 232 }
e6414a9d 233 fprintf(fp_ou2,"<tr><td class=\"data2\"><a href=\"%s/index.html\">%s %s</a></td></tr>\n",wwork1,tmp4,nmonth);
4157aa09 234
491b862f
GS
235 sprintf(val1,"%s%s/%s",outdir,tmp4,wwork1);
236 sprintf(tmp5,"%s%s/%s/index.unsort",outdir,tmp4,wwork1);
237 if((fp_ou3=fopen(tmp5,"w"))==NULL) {
238 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],tmp5);
239 exit(1);
240 }
241 // month dir
242 dirp3 = opendir(val1);
243 while ((direntp3 = readdir( dirp3 )) != NULL) {
d6e703cc
FM
244 if(!isdigit(direntp3->d_name[0]) && !isdigit(direntp3->d_name[1])) continue;
245 fprintf(fp_ou3,"%s\n",direntp3->d_name);
491b862f
GS
246 }
247 if(fp_ou3) fclose(fp_ou3);
248 (void)rewinddir(dirp3);
249 (void)closedir(dirp3);
250 unlink(wdir_tmp3);
251 sprintf(tmp6,"%s%s/%s/index.sort",outdir,tmp4,wwork1);
9a2efbd0
FM
252 if(strcmp(IndexSortOrder,"A") == 0) sprintf(csort,"sort -n \"%s\" -o \"%s\"", tmp5, tmp6);
253 else sprintf(csort,"sort -n -r \"%s\" -o \"%s\"", tmp5, tmp6);
456d78a5
FM
254 cstatus=system(csort);
255 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
256 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
257 fprintf(stderr, "SARG: sort command: %s\n",csort);
258 exit(1);
259 }
491b862f
GS
260 unlink(tmp5);
261 sprintf(val2,"%s%s/%s/index.html",outdir,tmp4,wwork1);
262 sprintf(val3,"%s/%s",tmp4,wwork1);
263 unlink(val2);
264 if((fp_ou3=fopen(val2,"w"))==NULL) {
265 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],val2);
266 exit(1);
267 }
268 if((fp_tmp3=fopen(tmp6,"r"))==NULL) {
269 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],tmp6);
270 exit(1);
271 }
272 write_html_header(fp_ou3,"../..");
d6e703cc 273 fprintf(fp_ou3,"<tr><th %s>%s/%s/%s</th></tr>\n",hbc1,text[130],text[131],text[127]);
e6414a9d 274 while(fgets(warea,sizeof(warea),fp_tmp3)!=NULL) {
491b862f 275 warea[strlen(warea)-1]='\0';
e6414a9d 276 fprintf(fp_ou3,"<tr><td class=\"data2\"><a href=\"%s/index.html\">%s %s %s</a></td></tr>\n",warea,tmp4,nmonth,warea);
491b862f
GS
277 }
278 if(fp_tmp3) fclose(fp_tmp3);
491b862f 279 write_html_trailer(fp_ou3);
121c2a89 280 if(fp_ou3) fclose(fp_ou3);
491b862f
GS
281 unlink(tmp6);
282 }
283 write_html_trailer(fp_ou2);
284 if(fp_ou2) fclose(fp_ou2);
285 }
491b862f
GS
286 if(fp_tmp) fclose(fp_tmp);
287 unlink(tmp6);
288 unlink(wdir_tmp2);
289 } else {
9a2efbd0
FM
290 if(strcmp(IndexSortOrder,"A") == 0) sprintf(warea,"sort -t\";\" -k 7,7 -k 1,1 \"%s\" -o \"%s\"", wdir_tmp, wdir_tmp2);
291 else sprintf(warea,"sort -r -t\";\" -k 7,7 -k 1,1 \"%s\" -o \"%s\"", wdir_tmp, wdir_tmp2);
456d78a5
FM
292 cstatus=system(warea);
293 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
294 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
295 fprintf(stderr, "SARG: sort command: %s\n",warea);
296 exit(1);
297 }
298 if((fp_tmp2=fopen(wdir_tmp2,"r"))==NULL) {
299 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp2);
300 fprintf(stderr, "SARG: sort command: %s\n",warea);
301 exit(1);
302 }
491b862f
GS
303 unlink(wdir_tmp);
304 if((fp_ou=fopen(wdir,"w"))==NULL) {
305 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir);
306 exit(1);
307 }
308 write_html_header(fp_ou,".");
d6e703cc 309 fprintf(fp_ou,"<tr><th %s>%s</th><th %s>%s</th><th %s>%s</th><th %s>%s</th><th %s>%s</th></tr>\n",hbc1,text[101],hbc1,text[102],hbc1,text[103],hbc1,text[93],hbc1,text[96]);
9c7c6346
FM
310 while(fgets(buf,sizeof(buf),fp_tmp2)!=NULL) {
311 getword_start(&gwarea,buf);
312 if (getword_multisep(period,sizeof(period),&gwarea,';')<0) {
4157aa09 313 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
4bcb77cf
FM
314 exit(1);
315 }
9c7c6346 316 if (getword_multisep(period,sizeof(period),&gwarea,';')<0) {
4157aa09 317 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
4bcb77cf
FM
318 exit(1);
319 }
9c7c6346 320 if (getword_multisep(data,sizeof(data),&gwarea,';')<0) {
4157aa09 321 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
4bcb77cf
FM
322 exit(1);
323 }
9c7c6346 324 if (getword_multisep(tuser,sizeof(tuser),&gwarea,';')<0) {
4157aa09 325 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
4bcb77cf
FM
326 exit(1);
327 }
9c7c6346 328 if (getword_multisep(tbytes,sizeof(tbytes),&gwarea,';')<0) {
4157aa09 329 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
4bcb77cf
FM
330 exit(1);
331 }
9c7c6346 332 if (getword_multisep(media,sizeof(media),&gwarea,';')<0) {
4157aa09 333 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
4bcb77cf
FM
334 exit(1);
335 }
d6e703cc 336 fprintf(fp_ou,"<tr><td class=\"data2\"><a href='%s/%s'>%s</a></td><td class=\"data2\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",period,ReplaceIndex,period,data,tuser,tbytes,media);
491b862f
GS
337 }
338 if(fp_tmp2) fclose(fp_tmp2);
339 unlink(wdir_tmp2);
25697a35 340 }
4157aa09 341
21fe6e2d
FM
342 write_html_trailer(fp_ou);
343 if(fp_ou) fclose(fp_ou);
25697a35 344}
06ced858
FM
345
346static void file_index_to_date_index(const char *entry)
347{
348 int y1, y2, d1, d2;
349 int i, j;
350 int ndirlen;
b5f13803 351 int monthlen;
06ced858
FM
352 char m1[8], m2[8];
353 char olddir[MAXLEN], newdir[MAXLEN];
354
355 if(strlen(entry) < 19) return;
356
357 y1=0;
358 y2=0;
359 memset(m1,0,sizeof(m1));
360 memset(m2,0,sizeof(m2));
361 d1=0;
362 d2=0;
363 i=0;
364 if(strcmp(df,"u") == 0) {
365 for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
366 y1=y1*10+(entry[i++]-'0');
367 if (j!=4) return;
368 for (j=0 ; j<sizeof(m1)-1 && entry[i] && isalpha(entry[i]) ; j++)
369 m1[j]=entry[i++];
370 if (j!=3) return;
371 m1[j]='\0';
372 for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
373 d1=d1*10+(entry[i++]-'0');
374 if (j!=2) return;
375
376 if (entry[i++]!='-') return;
377
378 for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
379 y2=y2*10+(entry[i++]-'0');
380 if (j!=4) return;
381 for (j=0 ; j<sizeof(m2)-1 && entry[i] && isalpha(entry[i]) ; j++)
382 m2[j]=entry[i++];
383 if (j!=3) return;
384 m2[j]='\0';
385 for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
386 d2=d2*10+(entry[i++]-'0');
387 if (j!=2) return;
388 } else if(strcmp(df,"e") == 0) {
389 for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
390 d1=d1*10+(entry[i++]-'0');
391 if (j!=2) return;
392 for (j=0 ; j<sizeof(m1)-1 && entry[i] && isalpha(entry[i]) ; j++)
393 m1[j]=entry[i++];
394 if (j!=3) return;
395 m1[j]='\0';
396 for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
397 y1=y1*10+(entry[i++]-'0');
398 if (j!=4) return;
399
400 if (entry[i++]!='-') return;
401
402 for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
403 d2=d2*10+(entry[i++]-'0');
404 if (j!=2) return;
405 for (j=0 ; j<sizeof(m2)-1 && entry[i] && isalpha(entry[i]) ; j++)
406 m2[j]=entry[i++];
407 if (j!=3) return;
408 m2[j]='\0';
409 for (j=0 ; entry[i] && isdigit(entry[i]) ; j++)
410 y2=y2*10+(entry[i++]-'0');
411 if (j!=4) return;
412 } else
413 return;
414
415 conv_month(m1);
416 conv_month(m2);
417 ndirlen=sprintf(newdir,"%s%04d",outdir,y1);
418 if(access(newdir, R_OK) != 0) mkdir(newdir,0755);
419 if(strcmp(m1,m2) != 0) ndirlen+=sprintf(newdir+ndirlen,"/%s-%s",m1,m2);
420 else ndirlen+=sprintf(newdir+ndirlen,"/%s",m1);
421 if(access(newdir, R_OK) != 0) mkdir(newdir,0755);
b5f13803 422 monthlen=ndirlen;
06ced858
FM
423 if(d1!=d2) ndirlen+=sprintf(newdir+ndirlen,"/%02d-%02d",d1,d2);
424 else ndirlen+=sprintf(newdir+ndirlen,"/%02d",d1);
425
426 sprintf(olddir,"%s%s",outdir,entry);
427 if (rename(olddir,newdir)) {
428 fprintf(stderr, "SARG: (index) rename error from \"%s\" to \"%s\" - %s\n",olddir,newdir,strerror(errno));
429 exit(1);
430 }
431
b5f13803
FM
432 strcpy(newdir+monthlen,"/images");
433 if(access(newdir, R_OK) != 0) {
434#ifdef HAVE_SYMLINK
435 char linkdir[MAXLEN];
436
437 sprintf(linkdir,"%simages",outdir);
438 if (symlink(linkdir,newdir)) {
439 fprintf(stderr, "SARG: failed to create link \"%s\" to \"%s\" - %s\n",linkdir,newdir,strerror(errno));
440 exit(1);
441 }
442#else
443 char cmd[MAXLEN];
444 int cstatus;
445
446 sprintf(cmd,"ln -s \"%simages\" \"%s/images\"",outdir,newdir);
447 cstatus=system(cmd);
448 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
449 fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
450 fprintf(stderr, "SARG: command: %s\n",cmd);
451 exit(1);
452 }
453#endif
454 }
06ced858
FM
455}
456
457static void date_index_to_file_index(const char *entry)
458{
459 int y1, next;
460 int val1len;
461 int d1, d2;
462 int i, j;
463 char val1[MAXLEN];
464 char m1[8], m2[8];
465 char *str;
466 char newdir[MAXLEN], olddir[MAXLEN];
467 DIR *dirp2, *dirp3;
468 struct dirent *direntp2;
469 struct dirent *direntp3;
470
471 if(strlen(entry) != 4) return;
472
473 next=-1;
474 if (sscanf(entry,"%d%n",&y1,&next)!=1 || next<0 || entry[next]) return;
475
476 val1len=sprintf(val1,"%s%s",outdir,entry);
477 dirp2 = opendir(val1);
478 if (!dirp2) return;
479 while ((direntp2 = readdir( dirp2 )) != NULL) {
480 if(!isdigit(direntp2->d_name[0]) || !isdigit(direntp2->d_name[1])) continue;
481 i=0;
482 str=direntp2->d_name;
483 for (j=0 ; j<sizeof(m1) && str[i] && isdigit(str[i]) ; j++)
484 m1[j]=str[i++];
485 if (j>=sizeof(m1)) continue;
486 m1[j]='\0';
487 conv_month_name(m1);
488 if (str[i]=='-') {
489 i++;
490 for (j=0 ; j<sizeof(m2) && str[i] && isdigit(str[i]) ; j++)
491 m2[j]=str[i++];
492 if (j>=sizeof(m2)) continue;
493 m2[j]='\0';
494 conv_month_name(m2);
495 } else if (!str[i]) {
496 strcpy(m2,m1);
497 } else {
498 continue;
499 }
500
501 sprintf(val1+val1len,"/%s",direntp2->d_name);
502 dirp3 = opendir(val1);
503 if (!dirp3) continue;
504 while ((direntp3 = readdir( dirp3 )) != NULL) {
505 if(!isdigit(direntp3->d_name[0]) || !isdigit(direntp3->d_name[1])) continue;
506 i=0;
507 str=direntp3->d_name;
508 d1=0;
509 for (j=0 ; str[i] && isdigit(str[i]) ; j++)
510 d1=d1*10+(str[i++]-'0');
511 if (j!=2) continue;
512 if (str[i]=='-') {
513 i++;
514 d2=0;
515 for (j=0 ; str[i] && isdigit(str[i]) ; j++)
516 d2=d2*10+(str[i++]-'0');
517 if (j!=2) continue;
518 } else if (!str[i]) {
519 d2=d1;
520 } else {
521 continue;
522 }
523
524 if(strcmp(df,"u") == 0) sprintf(newdir,"%s%04d%s%02d-%04d%s%02d",outdir,y1,m1,d1,y1,m2,d2);
525 else if(strcmp(df,"e") == 0) sprintf(newdir,"%s%02d%s%04d-%02d%s%04d",outdir,d1,m1,y1,d2,m2,y1);
526 else continue;
527 sprintf(olddir,"%s%04d/%s/%s",outdir,y1,direntp2->d_name,direntp3->d_name);
528 if(rename(olddir,newdir)) {
529 fprintf(stderr, "SARG: (index) rename error from \"%s\" to \"%s\" - %s\n",olddir,newdir,strerror(errno));
530 exit(1);
531 }
532 }
533 (void)closedir( dirp3 );
534 }
535 (void)closedir( dirp2 );
b5f13803
FM
536
537 /*!
538 \bug The links to the images in the reports are broken after moving the directories
539 as the the HTML files are not at the right level for the images any more.
540 */
06ced858
FM
541}
542