]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Sort the downloaded log just before using it
authorFrédéric Marchal <fmarchal@users.sourceforge.net>
Fri, 3 Feb 2012 20:49:33 +0000 (21:49 +0100)
committerFrédéric Marchal <fmarchal@users.sourceforge.net>
Fri, 3 Feb 2012 20:49:33 +0000 (21:49 +0100)
It is cleaner to group the download stuff in the same file and at the same
place.

Moreover, it frees the sorting routine up to be included where it belong
too.

The documentation of download.c has been merged within the source code.

documentation/download.txt [deleted file]
download.c
sort.c

diff --git a/documentation/download.txt b/documentation/download.txt
deleted file mode 100644 (file)
index 7b500b1..0000000
+++ /dev/null
@@ -1,72 +0,0 @@
-/*!\file download.c
-\brief Report the downloaded files
-*/
-
-
-/*! \var static char *DownloadSuffix=NULL;
-The buffer to store the list of the suffixes to take into account when generating
-the report of the downloaded files. The suffixes in the list are separated by the ASCII
-null.
-*/
-
-
-
-/*! \var static char **DownloadSuffixIndex=NULL;
-The index of all the suffixes stored in ::DownloadSuffix. The list is sorted alphabetically.
-to speed up the search.
-*/
-
-
-/*! \var static int NDownloadSuffix=0;
-The number of suffixes in ::DownloadSuffixIndex.
-*/
-
-
-
-/*! \fn void download_report(void)
-Generate the report of the downloaded files. The list of the suffixes to take into account
-is set with set_download_suffix().
-*/
-
-
-
-/*! \fn void free_download(void)
-Free the memory allocated by set_download_suffix().
-*/
-
-
-
-/*! \fn void set_download_suffix(const char *list)
-Set the list of the suffixes corresponding to the download of files you want to detect with
-is_download_suffix(). The list is sorted to make the search faster.
-
-\param list A comma separated list of the suffixes to set in ::DownloadSuffix.
-
-\note The memory allocated by this function must be freed by free_download().
-*/
-
-
-
-
-
-/*! \fn int is_download_suffix(const char *url)
-Tell if the URL correspond to a downloaded file. The function takes the extension at the end of the
-URL with a maximum of 9 characters and compare it to the list of the download suffix in
-::DownloadSuffix. If the suffix is found in the list, the function reports the URL as the download
-of a file.
-
-\param url The URL to test.
-
-\retval 1 The URL matches a suffix of a download.
-\retval 0 The URL is not a known download.
-
-\note A downloaded file cannot be detected if the file name is embedded in a GET or POST request. Only requests
-that ends with the file name can be detected.
-
-\note A URL embedding another web site's address ending by .com at the end of the URL will match the download
-extension com if it is defined in the ::DownloadSuffix.
-*/
-
-
-
-
index 75421a6d20529278bef6df5564b5eac77b88a576..5cb0aea378398179a93c26ce6dc25b10c20ed364 100644 (file)
 #include "include/conf.h"
 #include "include/defs.h"
 
+/*!
+The buffer to store the list of the suffixes to take into account when generating
+the report of the downloaded files. The suffixes in the list are separated by the ASCII
+null.
+*/
 /*@null@*/static char *DownloadSuffix=NULL;
+
+/*!
+The index of all the suffixes stored in ::DownloadSuffix. The list is sorted alphabetically.
+to speed up the search.
+*/
 /*@null@*/static char **DownloadSuffixIndex=NULL;
+
+/*!
+The number of suffixes in ::DownloadSuffixIndex.
+*/
 static int NDownloadSuffix=0;
 
+/*!
+Sort the raw log file with the downloaded files.
+
+\param report_in The name of the file where to store the sorted entries.
+*/
+static void download_sort(const char *report_in)
+{
+       int clen;
+       char csort[MAXLEN];
+       int cstatus;
+       
+       clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" -k 3,3 -k 1,1 -k 2,2 -k 5,5 -o \"%s\" \"%s/download.unsort\"",
+                       tmp, report_in, tmp);
+       if (clen>=sizeof(csort)) {
+               debuga(_("Path too long to sort the file: %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(csort,sizeof(csort),"%s/download.unsort",tmp)>=sizeof(csort)) {
+               debuga(_("Path too long for %s/download.unsort\n"),tmp);
+               exit(EXIT_FAILURE);
+       }
+       if (unlink(csort)) {
+               debuga(_("Cannot delete %s - %s\n"),csort,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+}
+
+/*!
+Generate the report of the downloaded files. The list of the suffixes to take into account
+is set with set_download_suffix().
+*/
 void download_report(void)
 {
        FILE *fp_in = NULL, *fp_ou = NULL;
@@ -59,12 +110,15 @@ void download_report(void)
        ouser[0]='\0';
        ouser2[0]='\0';
 
+       // sort the raw file
        snprintf(report_in,sizeof(report_in),"%s/download.log",tmp);
+       download_sort(report_in);
        if(access(report_in, R_OK) != 0) {
                if (debugz) debugaz(_("Downloaded files report not generated as it is empty\n"));
                return;
        }
 
+       // produce the report.
        snprintf(report,sizeof(report),"%s/download.html",outdirname);
 
        if((fp_in=MY_FOPEN(report_in,"r"))==NULL) {
@@ -176,6 +230,9 @@ void download_report(void)
        return;
 }
 
+/*!
+Free the memory allocated by set_download_suffix().
+*/
 void free_download(void)
 {
        if (DownloadSuffix) {
@@ -189,6 +246,14 @@ void free_download(void)
        NDownloadSuffix=0;
 }
 
+/*!
+Set the list of the suffixes corresponding to the download of files you want to detect with
+is_download_suffix(). The list is sorted to make the search faster.
+
+\param list A comma separated list of the suffixes to set in ::DownloadSuffix.
+
+\note The memory allocated by this function must be freed by free_download().
+*/
 void set_download_suffix(const char *list)
 {
        char *str;
@@ -241,6 +306,23 @@ void set_download_suffix(const char *list)
        }
 }
 
+/*!
+Tell if the URL correspond to a downloaded file. The function takes the extension at the end of the
+URL with a maximum of 9 characters and compare it to the list of the download suffix in
+::DownloadSuffix. If the suffix is found in the list, the function reports the URL as the download
+of a file.
+
+\param url The URL to test.
+
+\retval 1 The URL matches a suffix of a download.
+\retval 0 The URL is not a known download.
+
+\note A downloaded file cannot be detected if the file name is embedded in a GET or POST request. Only requests
+that ends with the file name can be detected.
+
+\note A URL embedding another web site's address ending by .com at the end of the URL will match the download
+extension com if it is defined in the ::DownloadSuffix.
+*/
 bool is_download_suffix(const char *url)
 {
        int urllen;
diff --git a/sort.c b/sort.c
index 83e190f467674815f1288ce68a99479ad0757349..f0823474d25e31dd8d590389a6751404937c1c8e 100644 (file)
--- a/sort.c
+++ b/sort.c
@@ -114,6 +114,8 @@ void sort_users_log(const char *tmp, int debug)
                        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;
@@ -122,12 +124,8 @@ void sort_users_log(const char *tmp, int debug)
                        user[0]='\0';
                }
 
-               if(strcmp(direntp->d_name,"download.unsort") == 0)
-                       clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" -k 3,3 -k 1,1 -k 2,2 -k 5,5 -o \"%s/%s.log\" \"%s/%s.unsort\"",
-                           tmp, tmp, user, tmp, user);
-               else
-                       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);
+               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);