]> git.ipfire.org Git - thirdparty/sarg.git/blob - lastlog.c
Update the Russian translation.
[thirdparty/sarg.git] / lastlog.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2013
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 void mklastlog(const char *outdir)
31 {
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
49 if (snprintf(temp,sizeof(temp),"%s/lastlog1",tmp)>=sizeof(temp)) {
50 debuga(_("Path too long: "));
51 debuga_more("%s/lastlog1\n",tmp);
52 exit(EXIT_FAILURE);
53 }
54 if((fp_ou=fopen(temp,"w"))==NULL) {
55 debugapos("lastlog",_("Cannot open file \"%s\": %s\n"),temp,strerror(errno));
56 exit(EXIT_FAILURE);
57 }
58
59 if ((dirp = opendir(outdir)) == NULL) {
60 debuga(_("Cannot open directory \"%s\": %s\n"),outdir,strerror(errno));
61 exit(EXIT_FAILURE);
62 }
63 while ((direntp = readdir( dirp )) != NULL ){
64 if(strchr(direntp->d_name,'-') == 0)
65 continue;
66
67 snprintf(warea,sizeof(warea),"%s%s",outdir,direntp->d_name);
68 if (stat(warea,&statb) == -1) {
69 debuga(_("Failed to get the creation time of \"%s\": %s\n"),warea,strerror(errno));
70 continue;
71 }
72 t=statb.st_ctime;
73 local = localtime(&t);
74 strftime(ftime, sizeof(ftime), "%Y%m%d%H%M%S", local);
75 fprintf(fp_ou,"%s\t%s\n",ftime,direntp->d_name);
76 ftot++;
77 }
78
79 closedir( dirp );
80 fclose(fp_ou);
81
82 if(ftot<=LastLog) {
83 if (debug) {
84 debuga(ngettext("No old reports to delete as only %d report currently exist\n",
85 "No old reports to delete as only %d reports currently exists\n",ftot),ftot);
86 }
87 if (!KeepTempLog && unlink(temp)) {
88 debuga(_("Cannot delete \"%s\": %s\n"),temp,strerror(errno));
89 exit(EXIT_FAILURE);
90 }
91 return;
92 }
93
94 snprintf(buf,sizeof(buf),"sort -n -t \"\t\" -k 1,1 -o \"%s/lastlog\" \"%s\"",tmp,temp);
95 cstatus=system(buf);
96 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
97 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
98 debuga(_("sort command: %s\n"),buf);
99 exit(EXIT_FAILURE);
100 }
101
102 if (!KeepTempLog && unlink(temp)) {
103 debuga(_("Cannot delete \"%s\": %s\n"),temp,strerror(errno));
104 exit(EXIT_FAILURE);
105 }
106
107 if (debug)
108 debuga(ngettext("%d report directory found\n","%d report directories found\n",ftot),ftot);
109 ftot-=LastLog;
110 if (debug)
111 debuga(ngettext("%d old report to delete\n","%d old reports to delete\n",ftot),ftot);
112
113 snprintf(temp,sizeof(temp),"%s/lastlog",tmp);
114 if((fp_in=fopen(temp,"r"))==NULL) {
115 debugapos("lastlog",_("Cannot open file \"%s\": %s\n"),temp,strerror(errno));
116 exit(EXIT_FAILURE);
117 }
118
119 while(ftot>0 && fgets(buf,sizeof(buf),fp_in)!=NULL) {
120 fixendofline(buf);
121 getword_start(&gwarea,buf);
122 if (getword(warea,sizeof(warea),&gwarea,'\t')<0) {
123 debuga(_("Invalid record in file \"%s\"\n"),temp);
124 exit(EXIT_FAILURE);
125 }
126
127 if(debug)
128 debuga(_("Removing old report file \"%s\"\n"),gwarea.current);
129 if (snprintf(temp,sizeof(temp),"%s%s",outdir,gwarea.current)>=sizeof(temp)) {
130 debuga(_("Path too long: "));
131 debuga_more("%s%s\n",outdir,gwarea.current);
132 exit(EXIT_FAILURE);
133 }
134 unlinkdir(temp,0);
135 ftot--;
136 }
137
138 fclose(fp_in);
139 if (!KeepTempLog) {
140 snprintf(temp,sizeof(temp),"%s/lastlog",tmp);
141 if (unlink(temp) == -1)
142 debuga(_("Cannot delete \"%s\": %s\n"),temp,strerror(errno));
143 }
144
145 return;
146 }