]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Sort the user's log just before using it
authorFrédéric Marchal <fmarchal@users.sourceforge.net>
Fri, 3 Feb 2012 21:02:55 +0000 (22:02 +0100)
committerFrédéric Marchal <fmarchal@users.sourceforge.net>
Fri, 3 Feb 2012 21:02:55 +0000 (22:02 +0100)
It is cleaner to group the sorting with the code using it. Moreover, it is
safer as we don't risk sorting a file that is not a user's file.

The doxygen documentation of sort.c was included into the source file
instead of keeping it in a separate file.

datafile.c
documentation/sort.txt [deleted file]
include/defs.h
log.c
report.c
sort.c

index 3d5dabe10c103b0d6fb9c76591d16bebedd91100..813966b55df7163b9f610dd90121206847a22f5e 100644 (file)
@@ -82,6 +82,7 @@ void data_file(char *tmp)
                user_find(uinfo->label,MAX_USER_LEN, u2);
                if(debug) debuga(_("Reading user file: %s/%s\n"),tmp,uinfo->filename);
 
+               sort_users_log(tmp,debug,uinfo);
                if (snprintf(tmp3,sizeof(tmp3),"%s/%s.log",tmp,uinfo->filename)>=sizeof(tmp3)) {
                        debuga(_("(datafile) directory path too long: %s/%s.log\n"),tmp,uinfo->filename);
                        exit(EXIT_FAILURE);
diff --git a/documentation/sort.txt b/documentation/sort.txt
deleted file mode 100644 (file)
index c72d19b..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/*! \file sort.c
-\brief Sort the temporary log files.
-*/
-
-
-
-
-/*! \fn void tmpsort(void)
-Sort all the \c utmp files form the temporary directory. The sort can be made according to the
-number of connections, the accessed sites or the time of the access depending on the value of
-::UserSortField. The sorting is either made in increasing or decreasing order as specified by
-the value of ::UserSortOrder.
-*/
-
-
-
-
-
-/*! \fn void sort_users_log(const char *tmp, int debug)
-The function sorts all the \c unsort files in the temporary directory. These files correspond
-to the format described in \ref UserUnsortLog.
-
-\param tmp The temorary directory of the sarg files.
-\param debug \c True to output debug information.
-
-The user's files are sorted by columns 5, 1 and 2 that are the columns of the number of bytes transfered,
-the date of the access and the time of the access.
-
-The file with the downloads is sorted by columns 3, 1, 2 and 5 that are the columns of the user's ID, the
-date, the time and the URL.
-
-In both cases, the sorted files are written in files with the extension \c log and the name of the unsorted
-file without the \c unsort extension. The unsorted file is deleted just after the sorting.
-*/
-
-
-
-
-
-/*! \fn void sort_labels(const char **label,const char **order)
-Get the internationalized text to display when reporting the sort criterion and order
-of a user list.
-
-\param label A pointer to set to the string of the sort criterion name.
-\param order A pointer to set to the string of the sort order name
-*/
index 2eac0613cbed5ed2ce79cc124897a49fdcd73d05..0d59741c44d7ad675b32061a3b1efc94a4ee357e 100755 (executable)
@@ -187,7 +187,7 @@ void siteuser(void);
 void smartfilter_report(void);
 
 // sort.c
-void sort_users_log(const char *tmp, int debug);
+void sort_users_log(const char *tmp, int debug,struct userinfostruct *uinfo);
 void tmpsort(const struct userinfostruct *uinfo);
 void sort_labels(const char **label,const char **order);
 
diff --git a/log.c b/log.c
index dda4dbea4f1a725b6d6c0ee78e6c754f70e0be5e..585603f9ee9dbe8fb8b8aecaa84d9bc3bd166ccd 100644 (file)
--- a/log.c
+++ b/log.c
@@ -1695,8 +1695,6 @@ int main(int argc,char *argv[])
                }
        }
 
-       sort_users_log(tmp, debug);
-
        if(DataFile[0] != '\0')
                data_file(tmp);
        else
index 0ed0c9f1e34fe35b96262584be1eb54245ea8bb5..5249576e92c3ca4700a1fced402fddf9ec357b9d 100644 (file)
--- a/report.c
+++ b/report.c
@@ -115,6 +115,7 @@ void gerarel(void)
                exit(EXIT_FAILURE);
        }
        while ((uinfo = userinfo_advancescan(uscan)) != NULL ) {
+               sort_users_log(tmp,debug,uinfo);
                if (snprintf(tmp3,sizeof(tmp3),"%s/%s.log",tmp,uinfo->filename)>=sizeof(tmp3)) {
                        debuga(_("(report) directory entry too long: %s/%s.log\n"),tmp,uinfo->filename);
                        exit(EXIT_FAILURE);
diff --git a/sort.c b/sort.c
index f0823474d25e31dd8d590389a6751404937c1c8e..be8c71ce4d42bfc1152e77f4ecb08c019800bf52 100644 (file)
--- a/sort.c
+++ b/sort.c
 #include "include/conf.h"
 #include "include/defs.h"
 
+/*!
+Sort all the \c utmp files form the temporary directory. The sort can be made according to the
+number of connections, the accessed sites or the time of the access depending on the value of
+::UserSortField. The sorting is either made in increasing or decreasing order as specified by
+the value of ::UserSortOrder.
+*/
 void tmpsort(const struct userinfostruct *uinfo)
 {
        int cstatus;
@@ -87,69 +93,63 @@ void tmpsort(const struct userinfostruct *uinfo)
        return;
 }
 
-void sort_users_log(const char *tmp, int debug)
+/*!
+The function sorts the \c unsort file in the temporary directory. These files correspond
+to the format described in \ref UserUnsortLog.
+
+\param tmp The temorary directory of the sarg files.
+\param debug \c True to output debug information.
+\param uinfo The user whose log must be sorted.
+
+The user's files are sorted by columns 5, 1 and 2 that are the columns of the number of bytes transfered,
+the date of the access and the time of the access.
+
+The sorted files are written in files with the extension \c log and the name of the unsorted
+file without the \c unsort extension. The unsorted file is deleted just after the sorting.
+*/
+void sort_users_log(const char *tmp, int debug,struct userinfostruct *uinfo)
 {
-       DIR *dirp;
-       struct dirent *direntp;
        char csort[MAXLEN];
-       char user[MAXLEN];
-       char wdname[MAXLEN];
+       const char *user;
        int cstatus;
-       int dlen;
        int clen;
-       const char unsortext[]=".unsort";
 
        if(debug) {
-               debuga(_("pre-sorting files\n"));
+               debuga(_("Sorting log %s/%s.unsort\n"),tmp,uinfo->filename);
        }
 
-       if ((dirp = opendir(tmp)) == NULL) {
-               debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno));
+       user=uinfo->filename;
+       clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" -k 4,4 -k 1,1 -k 2,2 -o \"%s/%s.log\" \"%s/%s.unsort\"",
+                       tmp, tmp, user, tmp, user);
+       if (clen>=sizeof(csort)) {
+               debuga(_("user name too long to sort %s\n"),csort);
                exit(EXIT_FAILURE);
        }
-       while ( (direntp = readdir( dirp )) != NULL ){
-               dlen=strlen(direntp->d_name)-(sizeof(unsortext)-1);
-               if (dlen<0) continue;
-               if(strcmp(direntp->d_name+dlen,unsortext) != 0)
-                       continue;
-               if(strcmp(direntp->d_name,"authfail.log.unsort") == 0)
-                       continue;
-               if(strcmp(direntp->d_name,"download.unsort") == 0)
-                       continue;
-
-               if (dlen>0) {
-                       if (dlen>=sizeof(user)) continue;
-                       safe_strcpy(user,direntp->d_name,dlen+1);
-               } else {
-                       user[0]='\0';
-               }
-
-               clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" -k 4,4 -k 1,1 -k 2,2 -o \"%s/%s.log\" \"%s/%s.unsort\"",
-                   tmp, tmp, user, tmp, user);
-               if (clen>=sizeof(csort)) {
-                       debuga(_("user name too long to sort %s\n"),csort);
-                       exit(EXIT_FAILURE);
-               }
-               cstatus=system(csort);
-               if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-                       debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
-                       debuga(_("sort command: %s\n"),csort);
-                       exit(EXIT_FAILURE);
-               }
-               if (snprintf(wdname,sizeof(wdname),"%s/%s.unsort",tmp,user)>=sizeof(wdname)) {
-                       debuga(_("user name too long for %s/%s.unsort\n"),tmp,user);
-                       exit(EXIT_FAILURE);
-               }
-               if (unlink(wdname)) {
-                       debuga(_("Cannot delete %s - %s\n"),wdname,strerror(errno));
-                       exit(EXIT_FAILURE);
-               }
+       cstatus=system(csort);
+       if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
+               debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+               debuga(_("sort command: %s\n"),csort);
+               exit(EXIT_FAILURE);
+       }
+       if (snprintf(csort,sizeof(csort),"%s/%s.unsort",tmp,user)>=sizeof(csort)) {
+               debuga(_("user name too long for %s/%s.unsort\n"),tmp,user);
+               exit(EXIT_FAILURE);
+       }
+       if (unlink(csort)) {
+               debuga(_("Cannot delete %s - %s\n"),csort,strerror(errno));
+               exit(EXIT_FAILURE);
        }
-       (void)closedir( dirp );
 
        return;
 }
 
+/*!
+Get the internationalized text to display when reporting the sort criterion and order
+of a user list.
+
+\param label A pointer to set to the string of the sort criterion name.
+\param order A pointer to set to the string of the sort order name
+*/
 void sort_labels(const char **label,const char **order)
 {
        if((UserSort & USER_SORT_CONNECT) != 0) {