]>
git.ipfire.org Git - thirdparty/sarg.git/blob - lastlog.c
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
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.
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.
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.
27 #include "include/conf.h"
28 #include "include/defs.h"
32 struct DirEntry
*Next
;
37 static void DeleteDirList(struct DirEntry
*List
)
39 struct DirEntry
*Next
;
44 if (List
->Name
) free(List
->Name
);
50 static struct DirEntry
*AppendDirEntry(struct DirEntry
*List
,time_t CreationTime
,const char *Name
,int NameLen
)
52 struct DirEntry
*entry
;
53 struct DirEntry
*prev
;
56 entry
=malloc(sizeof(*entry
));
58 debuga(_("Not enough memory to store a report to purge\n"));
62 entry
->Name
=malloc((NameLen
+1)*sizeof(char));
65 debuga(_("Not enough memory to store a report to purge\n"));
69 entry
->Time
=CreationTime
;
70 strcpy(entry
->Name
,Name
);
72 // store most recent file first
74 for (ptr
=List
; ptr
; ptr
=ptr
->Next
)
76 if (ptr
->Time
>CreationTime
) break;
88 static struct DirEntry
*BuildDirDateList(struct DirEntry
*List
,char *Path
,int PathSize
,int RootPos
,int Length
,int Level
)
91 struct dirent
*direntp
;
95 if ((dirp
= opendir(Path
)) == NULL
) {
96 debuga(_("Cannot open directory \"%s\": %s\n"),Path
,strerror(errno
));
99 while ((direntp
= readdir( dirp
)) != NULL
)
101 name_len
=strlen(direntp
->d_name
);
102 if (RootPos
+name_len
+1>=PathSize
) {
103 debuga(_("Directory entry too long to purge the old reports: %s%s\n"),Path
,direntp
->d_name
);
106 strcpy(Path
+Length
,direntp
->d_name
);
107 if (stat(Path
,&statb
) == -1) {
108 debuga(_("Failed to get the statistics of file \"%s\": %s\n"),Path
,strerror(errno
));
111 if (!S_ISDIR(statb
.st_mode
)) continue;
114 if (IsTreeMonthFileName(direntp
->d_name
))
116 Path
[Length
+name_len
]='/';
117 Path
[Length
+name_len
+1]='\0';
118 List
=BuildDirDateList(List
,Path
,PathSize
,RootPos
,Length
+name_len
+1,1);
121 debuga(_("Old reports deletion not undertaken due to previous error\n"));
128 if (IsTreeDayFileName(direntp
->d_name
))
130 List
=AppendDirEntry(List
,statb
.st_mtime
,Path
+RootPos
,Length
-RootPos
+name_len
);
133 debuga(_("Old reports deletion not undertaken due to previous error\n"));
144 static struct DirEntry
*BuildDirList(const char *Path
)
147 struct dirent
*direntp
;
152 struct DirEntry
*List
=NULL
;
154 name_pos
=strlen(Path
);
155 if (name_pos
>=sizeof(warea
)) {
156 debuga(_("The directory containing the old reports to purge is too long: %s\n"),Path
);
160 if ((dirp
= opendir(outdir
)) == NULL
) {
161 debuga(_("Cannot open directory \"%s\": %s\n"),outdir
,strerror(errno
));
164 while ((direntp
= readdir( dirp
)) != NULL
)
166 name_len
=strlen(direntp
->d_name
);
167 if (name_pos
+name_len
+1>=sizeof(warea
)) {
168 debuga(_("Directory entry too long to purge the old reports: %s%s\n"),Path
,direntp
->d_name
);
171 strcpy(warea
+name_pos
,direntp
->d_name
);
172 if (stat(warea
,&statb
) == -1) {
173 debuga(_("Failed to get the statistics of file \"%s\": %s\n"),warea
,strerror(errno
));
176 if (!S_ISDIR(statb
.st_mode
)) continue;
177 if (IsTreeFileDirName(direntp
->d_name
))
179 List
=AppendDirEntry(List
,statb
.st_mtime
,direntp
->d_name
,name_len
);
182 debuga(_("Old reports deletion not undertaken due to previous error\n"));
186 else if (IsTreeYearFileName(direntp
->d_name
))
188 warea
[name_pos
+name_len
]='/';
189 warea
[name_pos
+name_len
+1]='\0';
190 List
=BuildDirDateList(List
,warea
,sizeof(warea
),name_pos
,name_pos
+name_len
+1,0);
193 debuga(_("Old reports deletion not undertaken due to previous error\n"));
203 static void DeleteEmptyDirs(char *Path
,int PathSize
,int BasePos
)
207 struct dirent
*direntp
;
210 while ((Dir
=strrchr(Path
,'/'))!=NULL
)
212 if (Dir
-Path
<=BasePos
) break;
214 if ((dirp
= opendir(Path
)) == NULL
) {
215 debuga(_("Cannot open directory \"%s\": %s\n"),Path
,strerror(errno
));
219 while ((direntp
= readdir( dirp
)) != NULL
)
221 if (direntp
->d_name
[0]=='.' && (direntp
->d_name
[1]=='\0' || (direntp
->d_name
[1]=='.' && direntp
->d_name
[2]=='\0'))) continue;
222 if (!strcmp(direntp
->d_name
,INDEX_HTML_FILE
))
231 // at least one file exists in the directory, don't delete the directory
235 debuga(_("Deleting empty directory %s\n"),Path
);
237 if (strlen(Path
)+strlen(INDEX_HTML_FILE
)+2>=PathSize
) {
238 debuga(_("Buffer too small to delete index file %s/%s"),Path
,INDEX_HTML_FILE
);
241 strcat(Path
,"/"INDEX_HTML_FILE
);
242 if (unlink(Path
)==-1) {
243 debuga(_("Failed to delete \"%s\": %s\n"),Path
,strerror(errno
));
249 debuga(_("Cannot delete \"%s\": %s\n"),Path
,strerror(errno
));
253 //! \todo Rebuild the surviving index file
256 void mklastlog(const char *outdir
)
261 struct DirEntry
*List
;
262 struct DirEntry
*ptr
;
267 List
=BuildDirList(outdir
);
270 for (ptr
=List
; ptr
; ptr
=ptr
->Next
) ftot
++;
272 debuga(ngettext("%d report directory found\n","%d report directories found\n",ftot
),ftot
);
277 debuga(ngettext("No old reports to delete as only %d report currently exists\n",
278 "No old reports to delete as only %d reports currently exist\n",ftot
),ftot
);
285 debuga(ngettext("%d old report to delete\n","%d old reports to delete\n",ftot
),ftot
);
287 name_pos
=strlen(outdir
);
288 if (name_pos
>=sizeof(warea
)) {
290 debuga(_("The directory containing the old reports to purge is too long: %s\n"),outdir
);
293 strcpy(warea
,outdir
);
294 for (ptr
=List
; ptr
&& ftot
>0 ; ptr
=ptr
->Next
)
297 debuga(_("Removing old report file %s\n"),ptr
->Name
);
298 if (name_pos
+strlen(ptr
->Name
)+1>=sizeof(warea
)) {
300 debuga(_("Path too long: "));
301 debuga_more("%s%s\n",outdir
,ptr
->Name
);
304 strcpy(warea
+name_pos
,ptr
->Name
);
306 DeleteEmptyDirs(warea
,sizeof(warea
),name_pos
);