]> git.ipfire.org Git - thirdparty/sarg.git/blob - lastlog.c
064cad611e8b5c4bb2edbbe5e964d20a5b05436c
[thirdparty/sarg.git] / lastlog.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2014
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 struct DirEntry
31 {
32 struct DirEntry *Next;
33 time_t Time;
34 char *Name;
35 };
36
37 static void DeleteDirList(struct DirEntry *List)
38 {
39 struct DirEntry *Next;
40
41 while (List)
42 {
43 Next=List->Next;
44 if (List->Name) free(List->Name);
45 free(List);
46 List=Next;
47 }
48 }
49
50 static struct DirEntry *AppendDirEntry(struct DirEntry *List,time_t CreationTime,const char *Name,int NameLen)
51 {
52 struct DirEntry *entry;
53 struct DirEntry *prev;
54 struct DirEntry *ptr;
55
56 entry=malloc(sizeof(*entry));
57 if (!entry) {
58 debuga(_("Not enough memory to store a report to purge\n"));
59 DeleteDirList(List);
60 return(NULL);
61 }
62 entry->Name=malloc((NameLen+1)*sizeof(char));
63 if (!entry->Name) {
64 free(entry);
65 debuga(_("Not enough memory to store a report to purge\n"));
66 DeleteDirList(List);
67 return(NULL);
68 }
69 entry->Time=CreationTime;
70 strcpy(entry->Name,Name);
71
72 // store most recent file first
73 prev=NULL;
74 for (ptr=List ; ptr ; ptr=ptr->Next)
75 {
76 if (ptr->Time>CreationTime) break;
77 prev=ptr;
78 }
79 entry->Next=ptr;
80 if (prev)
81 prev->Next=entry;
82 else
83 List=entry;
84
85 return(List);
86 }
87
88 static struct DirEntry *BuildDirList(const char *Path)
89 {
90 DIR *dirp;
91 struct dirent *direntp;
92 struct stat statb;
93 char warea[MAXLEN];
94 int name_pos;
95 int name_len;
96 struct DirEntry *List=NULL;
97
98 name_pos=strlen(Path);
99 if (name_pos>=sizeof(warea)) {
100 debuga(_("The directory containing the old reports to purge is too long: %s\n"),Path);
101 exit(EXIT_FAILURE);
102 }
103 strcpy(warea,Path);
104 if ((dirp = opendir(outdir)) == NULL) {
105 debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno));
106 exit(EXIT_FAILURE);
107 }
108 while ((direntp = readdir( dirp )) != NULL )
109 {
110 if (!IsTreeFileDirName(direntp->d_name)) continue;
111
112 name_len=strlen(direntp->d_name);
113 if (name_pos+name_len+1>=sizeof(warea)) {
114 debuga(_("Directory entry too long to purge the old reports: %s%s\n"),Path,direntp->d_name);
115 exit(EXIT_FAILURE);
116 }
117 strcpy(warea+name_pos,direntp->d_name);
118 if (stat(warea,&statb) == -1) {
119 debuga(_("Failed to get the last modification time of %s\n"),warea);
120 continue;
121 }
122 List=AppendDirEntry(List,statb.st_mtime,direntp->d_name,name_len);
123 if (!List)
124 {
125 debuga(_("Old reports deletion not undertaken due to previous error\n"));
126 break;
127 }
128 }
129
130 closedir(dirp);
131 return(List);
132 }
133
134 void mklastlog(const char *outdir)
135 {
136 char warea[MAXLEN];
137 int name_pos;
138 int ftot=0;
139 struct DirEntry *List;
140 struct DirEntry *ptr;
141
142 if(LastLog <= 0)
143 return;
144
145 List=BuildDirList(outdir);
146 if (!List) return;
147
148 for (ptr=List ; ptr ; ptr=ptr->Next) ftot++;
149 if (debug)
150 debuga(ngettext("%d report directory found\n","%d report directories found\n",ftot),ftot);
151
152 if(ftot<=LastLog) {
153 DeleteDirList(List);
154 if (debug) {
155 debuga(ngettext("No old reports to delete as only %d report currently exists\n",
156 "No old reports to delete as only %d reports currently exist\n",ftot),ftot);
157 }
158 return;
159 }
160
161 ftot-=LastLog;
162 if (debug)
163 debuga(ngettext("%d old report to delete\n","%d old reports to delete\n",ftot),ftot);
164
165 name_pos=strlen(outdir);
166 if (name_pos>=sizeof(warea)) {
167 DeleteDirList(List);
168 debuga(_("The directory containing the old reports to purge is too long: %s\n"),outdir);
169 exit(EXIT_FAILURE);
170 }
171 strcpy(warea,outdir);
172 for (ptr=List ; ptr && ftot>0 ; ptr=ptr->Next)
173 {
174 if(debug)
175 debuga(_("Removing old report file %s\n"),ptr->Name);
176 if (name_pos+strlen(ptr->Name)+1>=sizeof(warea)) {
177 DeleteDirList(List);
178 debuga(_("Directory name too long: %s%s\n"),outdir,ptr->Name);
179 exit(EXIT_FAILURE);
180 }
181 strcpy(warea+name_pos,ptr->Name);
182 //unlinkdir(warea,0);
183 ftot--;
184 }
185
186 DeleteDirList(List);
187 return;
188 }