]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - indexonly.c
Generate redirector log even if -d is not given
[thirdparty/sarg.git] / indexonly.c
index eb05d94acdec66caf84cccf7e8c560706f35db39..b6d44bb00915f0f977b5b202f90110ff247be5b7 100644 (file)
@@ -1,10 +1,11 @@
 /*
- * AUTHOR: Pedro Lineu Orso                         pedro.orso@gmail.com
- *                                                            1998, 2010
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
+ *                                                            1998, 2015
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
+ * Support:
+ *     http://sourceforge.net/projects/sarg/forums/forum/363374
  * ---------------------------------------------------------------------
  *
  *  This program is free software; you can redistribute it and/or modify
 
 void index_only(const char *dirname,int debug)
 {
-   DIR *dirp;
-   struct dirent *direntp;
-   char remove[MAXLEN];
-  
-   dirp = opendir(dirname);
-   while ( (direntp = readdir( dirp )) != NULL ){
-      if(strcmp(direntp->d_name,".") == 0 || strcmp(direntp->d_name,"..") == 0 || strcmp(direntp->d_name, "index.html") == 0)
-         continue;
-       
-      sprintf(remove,"%s/%s",dirname,direntp->d_name);
-      unlink(remove);
-   }
+       DIR *dirp;
+       struct dirent *direntp;
+       char remove[MAXLEN];
 
-   (void)closedir( dirp );
+       if ((dirp = opendir(dirname)) == NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open directory \"%s\": %s\n"),dirname,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+       while ( (direntp = readdir( dirp )) != NULL ){
+               if(strcmp(direntp->d_name,".") == 0 || strcmp(direntp->d_name,"..") == 0 || strcmp(direntp->d_name, INDEX_HTML_FILE) == 0)
+                       continue;
 
-   return;
+               if (snprintf(remove,sizeof(remove),"%s/%s",dirname,direntp->d_name)>=sizeof(remove)) {
+                       debuga(__FILE__,__LINE__,_("Name of the file to remove is too long. File name is \"%s/%s\"\n"),dirname,direntp->d_name);
+                       continue;
+               }
+               if (unlink(remove) == -1) {
+                       debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),remove,strerror(errno));
+               }
+       }
+
+       (void)closedir( dirp );
+
+       return;
 }