]> git.ipfire.org Git - thirdparty/sarg.git/blame - lastlog.c
Check the release date when archiving the project
[thirdparty/sarg.git] / lastlog.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
e99bf0c0 3 * 1998, 2013
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
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"
5f3cfd1d 28#include "include/defs.h"
25697a35 29
32e71fa4 30void mklastlog(const char *outdir)
25697a35 31{
9bd92830
FM
32 FILE *fp_in, *fp_ou;
33 DIR *dirp;
34 struct dirent *direntp;
35 char buf[MAXLEN];
36 char temp[MAXLEN];
37 char warea[MAXLEN];
38 char ftime[128];
39 int ftot=0;
40 time_t t;
41 struct tm *local;
42 struct stat statb;
43 int cstatus;
44 struct getwordstruct gwarea;
45
46 if(LastLog <= 0)
47 return;
48
f293c727
FM
49 if (snprintf(temp,sizeof(temp),"%s/lastlog1",tmp)>=sizeof(temp)) {
50 debuga(_("File name too long: %s/lastlog1\n"),tmp);
51 exit(EXIT_FAILURE);
52 }
9bd92830 53 if((fp_ou=fopen(temp,"w"))==NULL) {
007905af
FM
54 debuga(_("(lastlog) Cannot open temporary file %s\n"),temp);
55 exit(EXIT_FAILURE);
9bd92830
FM
56 }
57
58 if ((dirp = opendir(outdir)) == NULL) {
59 debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno));
60 exit(EXIT_FAILURE);
61 }
62 while ((direntp = readdir( dirp )) != NULL ){
63 if(strchr(direntp->d_name,'-') == 0)
64 continue;
65
66 snprintf(warea,sizeof(warea),"%s%s",outdir,direntp->d_name);
67 if (stat(warea,&statb) == -1) {
68 debuga(_("Failed to get the creation time of %s\n"),warea);
69 continue;
70 }
71 t=statb.st_ctime;
72 local = localtime(&t);
73 strftime(ftime, sizeof(ftime), "%Y%m%d%H%M%S", local);
74 fprintf(fp_ou,"%s\t%s\n",ftime,direntp->d_name);
75 ftot++;
76 }
77
78 closedir( dirp );
79 fclose(fp_ou);
80
f293c727
FM
81 if(ftot<=LastLog) {
82 if (debug) {
83 debuga(ngettext("No old reports to delete as only %d report currently exist\n",
84 "No old reports to delete as only %d reports currently exists\n",ftot),ftot);
85 }
86 if (!KeepTempLog && unlink(temp)) {
87 debuga(_("Cannot delete \"%s\": %s\n"),temp,strerror(errno));
88 exit(EXIT_FAILURE);
89 }
90 return;
91 }
92
93 snprintf(buf,sizeof(buf),"sort -n -t \"\t\" -k 1,1 -o \"%s/lastlog\" \"%s\"",tmp,temp);
9bd92830
FM
94 cstatus=system(buf);
95 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
96 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
97 debuga(_("sort command: %s\n"),buf);
98 exit(EXIT_FAILURE);
99 }
100
f293c727 101 if (!KeepTempLog && unlink(temp)) {
7c4264ec 102 debuga(_("Cannot delete \"%s\": %s\n"),temp,strerror(errno));
08f9b029
FM
103 exit(EXIT_FAILURE);
104 }
9bd92830 105
f293c727
FM
106 if (debug)
107 debuga(ngettext("%d report directory found\n","%d report directories found\n",ftot),ftot);
9bd92830 108 ftot-=LastLog;
f293c727
FM
109 if (debug)
110 debuga(ngettext("%d old report to delete\n","%d old reports to delete\n",ftot),ftot);
9bd92830 111
f293c727 112 snprintf(temp,sizeof(temp),"%s/lastlog",tmp);
9bd92830 113 if((fp_in=fopen(temp,"r"))==NULL) {
007905af
FM
114 debuga(_("(lastlog) Cannot open temporary file %s\n"),temp);
115 exit(EXIT_FAILURE);
9bd92830
FM
116 }
117
118 while(ftot>0 && fgets(buf,sizeof(buf),fp_in)!=NULL) {
119 fixendofline(buf);
120 getword_start(&gwarea,buf);
121 if (getword(warea,sizeof(warea),&gwarea,'\t')<0) {
122 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),temp);
123 exit(EXIT_FAILURE);
124 }
125
126 if(debug)
127 debuga(_("Removing old report file %s\n"),gwarea.current);
128 if (snprintf(temp,sizeof(temp),"%s%s",outdir,gwarea.current)>=sizeof(temp)) {
129 debuga(_("Directory name too long: %s%s\n"),outdir,gwarea.current);
130 exit(EXIT_FAILURE);
131 }
132 unlinkdir(temp,0);
133 ftot--;
134 }
135
136 fclose(fp_in);
f293c727
FM
137 if (!KeepTempLog) {
138 snprintf(temp,sizeof(temp),"%s/lastlog",tmp);
139 if (unlink(temp) == -1)
140 debuga(_("Cannot delete \"%s\": %s\n"),temp,strerror(errno));
9bd92830
FM
141 }
142
143 return;
25697a35 144}