LCOV - code coverage report
Current view: top level - lib/formats - summary.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 34 43 79.1 %
Date: 2015-09-30 14:09:30 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  This file is part of rmlint.
       3             :  *
       4             :  *  rmlint is free software: you can redistribute it and/or modify
       5             :  *  it under the terms of the GNU General Public License as published by
       6             :  *  the Free Software Foundation, either version 3 of the License, or
       7             :  *  (at your option) any later version.
       8             :  *
       9             :  *  rmlint is distributed in the hope that it will be useful,
      10             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  *  GNU General Public License for more details.
      13             :  *
      14             :  *  You should have received a copy of the GNU General Public License
      15             :  *  along with rmlint.  If not, see <http://www.gnu.org/licenses/>.
      16             :  *
      17             :  * Authors:
      18             :  *
      19             :  *  - Christopher <sahib> Pahl 2010-2015 (https://github.com/sahib)
      20             :  *  - Daniel <SeeSpotRun> T.   2014-2015 (https://github.com/SeeSpotRun)
      21             :  *
      22             :  * Hosted on http://github.com/sahib/rmlint
      23             :  *
      24             :  */
      25             : 
      26             : #include "../formats.h"
      27             : 
      28             : #include <glib.h>
      29             : #include <stdio.h>
      30             : #include <string.h>
      31             : #include <search.h>
      32             : 
      33             : typedef struct RmFmtHandlerSummary {
      34             :     /* must be first */
      35             :     RmFmtHandler parent;
      36             : } RmFmtHandlerSummary;
      37             : 
      38             : #define ARROW \
      39             :     fprintf(out, "%s==>%s ", MAYBE_YELLOW(out, session), MAYBE_RESET(out, session));
      40             : 
      41        2422 : static int rm_fmt_summary_cmp(gconstpointer key, gconstpointer value) {
      42        2422 :     return strcmp((char *)key, *(char **)value);
      43             : }
      44             : 
      45        1830 : static void rm_fmt_prog(RmSession *session,
      46             :                         _U RmFmtHandler *parent,
      47             :                         _U FILE *out,
      48             :                         RmFmtProgressState state) {
      49        1830 :     if(state != RM_PROGRESS_STATE_SUMMARY) {
      50        3416 :         return;
      51             :     }
      52             : 
      53         122 :     if(session->total_files <= 1) {
      54           0 :         ARROW fprintf(out, "%s%d%s", MAYBE_RED(out, session), session->total_files,
      55           0 :                       MAYBE_RESET(out, session));
      56           0 :         fprintf(out, _(" file(s) after investigation, nothing to search through.\n"));
      57           0 :         return;
      58             :     }
      59             : 
      60         122 :     if(rm_session_was_aborted(session)) {
      61           0 :         ARROW fprintf(out, _("Early shutdown, probably not all lint was found.\n"));
      62             :     }
      63             : 
      64             :     char numbers[3][512];
      65         122 :     snprintf(numbers[0], sizeof(numbers[0]), "%s%d%s", MAYBE_RED(out, session),
      66         122 :              session->total_files, MAYBE_RESET(out, session));
      67         122 :     snprintf(numbers[1], sizeof(numbers[1]), "%s%" LLU "%s", MAYBE_RED(out, session),
      68         122 :              session->dup_counter, MAYBE_RESET(out, session));
      69         122 :     snprintf(numbers[2], sizeof(numbers[2]), "%s%" LLU "%s", MAYBE_RED(out, session),
      70         122 :              session->dup_group_counter, MAYBE_RESET(out, session));
      71             : 
      72         122 :     ARROW fprintf(out, _("In total %s files, whereof %s are duplicates in %s groups.\n"),
      73             :                   numbers[0], numbers[1], numbers[2]);
      74             : 
      75             :     /* log10(2 ** 64) + 2 = 21; */
      76         122 :     char size_string_buf[22] = {0};
      77         122 :     rm_util_size_to_human_readable(session->total_lint_size, size_string_buf,
      78             :                                    sizeof(size_string_buf));
      79             : 
      80         366 :     ARROW fprintf(out, _("This equals %s%s%s of duplicates which could be removed.\n"),
      81         244 :                   MAYBE_RED(out, session), size_string_buf, MAYBE_RESET(out, session));
      82             : 
      83         122 :     if(session->other_lint_cnt > 0) {
      84           0 :         ARROW fprintf(out, "%s%" LLU "%s ", MAYBE_RED(out, session),
      85           0 :                       session->other_lint_cnt, MAYBE_RESET(out, session));
      86             : 
      87           0 :         fprintf(out, _("other suspicious item(s) found, which may vary in size.\n"));
      88             :     }
      89             : 
      90         122 :     bool first_print_flag = true;
      91             :     GHashTableIter iter;
      92         122 :     char *path = NULL;
      93         122 :     RmFmtHandler *handler = NULL;
      94         122 :     rm_fmt_get_pair_iter(session->formats, &iter);
      95             : 
      96        1058 :     while(g_hash_table_iter_next(&iter, (gpointer *)&path, (gpointer *)&handler)) {
      97             :         static const char *forbidden[] = {"stdout", "stderr", "stdin"};
      98         814 :         gsize forbidden_len = sizeof(forbidden) / sizeof(forbidden[0]);
      99             : 
     100         814 :         if(lfind(path, forbidden, &forbidden_len, sizeof(const char *),
     101             :                  rm_fmt_summary_cmp)) {
     102          20 :             continue;
     103             :         }
     104             : 
     105             :         /* Check if the file really exists, so we can print it for sure */
     106         804 :         if(access(path, R_OK) == -1) {
     107           0 :             continue;
     108             :         }
     109             : 
     110         804 :         if(first_print_flag) {
     111         122 :             fprintf(out, "\n");
     112         122 :             first_print_flag = false;
     113             :         }
     114             : 
     115        3216 :         fprintf(out, _("Wrote a %s%s%s file to: %s%s%s\n"), MAYBE_BLUE(out, session),
     116        2412 :                 handler->name, MAYBE_RESET(out, session), MAYBE_GREEN(out, session), path,
     117         804 :                 MAYBE_RESET(out, session));
     118             :     }
     119             : }
     120             : 
     121             : static RmFmtHandlerSummary SUMMARY_HANDLER_IMPL = {
     122             :     /* Initialize parent */
     123             :     .parent =
     124             :         {
     125             :          .size = sizeof(SUMMARY_HANDLER_IMPL),
     126             :          .name = "summary",
     127             :          .head = NULL,
     128             :          .elem = NULL,
     129             :          .prog = rm_fmt_prog,
     130             :          .foot = NULL,
     131             :          .valid_keys = {NULL},
     132             :         },
     133             : };
     134             : 
     135             : RmFmtHandler *SUMMARY_HANDLER = (RmFmtHandler *)&SUMMARY_HANDLER_IMPL;

Generated by: LCOV version 1.11