LCOV - code coverage report
Current view: top level - src - stats.c (source / functions) Hit Total Coverage
Test: cov.info Lines: 0 53 0.0 %
Date: 2015-08-15 Functions: 0 2 0.0 %

          Line data    Source code
       1             : /*-
       2             :  * Copyright (c) 2011-2012 Marin Atanasov Nikolov <dnaeon@gmail.com>
       3             :  * Copyright (c) 2014 Matthew Seaman <matthew@FreeBSD.org>
       4             :  * All rights reserved.
       5             :  * 
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions
       8             :  * are met:
       9             :  * 1. Redistributions of source code must retain the above copyright
      10             :  *    notice, this list of conditions and the following disclaimer
      11             :  *    in this position and unchanged.
      12             :  * 2. Redistributions in binary form must reproduce the above copyright
      13             :  *    notice, this list of conditions and the following disclaimer in the
      14             :  *    documentation and/or other materials provided with the distribution.
      15             :  * 
      16             :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
      17             :  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      18             :  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
      19             :  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
      20             :  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      21             :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      22             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      23             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      24             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
      25             :  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      26             :  */
      27             : 
      28             : #ifdef HAVE_CONFIG_H
      29             : #include "pkg_config.h"
      30             : #endif
      31             : 
      32             : #include <err.h>
      33             : #include <getopt.h>
      34             : #include <inttypes.h>
      35             : #ifdef HAVE_LIBUTIL_H
      36             : #include <libutil.h>
      37             : #endif
      38             : #include <stdio.h>
      39             : #include <sysexits.h>
      40             : #include <unistd.h>
      41             : 
      42             : #include <pkg.h>
      43             : 
      44             : #include <bsd_compat.h>
      45             : 
      46             : #include "pkgcli.h"
      47             : 
      48             : void
      49           0 : usage_stats(void)
      50             : {
      51           0 :         fprintf(stderr, "Usage: pkg stats [-qlrb]\n\n");
      52           0 :         fprintf(stderr, "For more information see 'pkg help stats'.\n");
      53           0 : }
      54             : 
      55             : int
      56           0 : exec_stats(int argc, char **argv)
      57             : {
      58           0 :         struct pkgdb    *db = NULL;
      59           0 :         int64_t          flatsize = 0;
      60           0 :         unsigned int     opt = 0;
      61             :         char             size[8];
      62             :         int              ch;
      63           0 :         bool             show_bytes = false;
      64             : 
      65           0 :         struct option longopts[] = {
      66             :                 { "bytes",    no_argument,    NULL,   'b' },
      67             :                 { "local",    no_argument,    NULL,   'l' },
      68             :                 { "quiet",    no_argument,    NULL,   'q' },
      69             :                 { "remote",   no_argument,    NULL,   'r' },
      70             :                 { NULL,         0,              NULL,   0   },
      71             :         };
      72             :         
      73           0 :         while ((ch = getopt_long(argc, argv, "+blqr", longopts, NULL)) != -1) {
      74           0 :                 switch (ch) {
      75             :                 case 'b':
      76           0 :                         show_bytes = true;
      77           0 :                         break;
      78             :                 case 'l':
      79           0 :                         opt |= STATS_LOCAL;
      80           0 :                         break;
      81             :                 case 'q':
      82           0 :                         quiet = true;
      83           0 :                         break;
      84             :                 case 'r':
      85           0 :                         opt |= STATS_REMOTE;
      86           0 :                         break;
      87             :                 default:
      88           0 :                         usage_stats();
      89           0 :                         return (EX_USAGE);
      90             :                 }
      91             :         }
      92           0 :         argc -= optind;
      93           0 :         argv += optind;
      94             : 
      95             :         /* default is to show everything we have */
      96           0 :         if (opt == 0)
      97           0 :                 opt |= (STATS_LOCAL | STATS_REMOTE);
      98             : 
      99           0 :         if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {
     100           0 :                 return (EX_IOERR);
     101             :         }
     102             : 
     103           0 :         if (pkgdb_obtain_lock(db, PKGDB_LOCK_READONLY) != EPKG_OK) {
     104           0 :                 pkgdb_close(db);
     105           0 :                 warnx("Cannot get a read lock on a database, it is locked by another process");
     106           0 :                 return (EX_TEMPFAIL);
     107             :         }
     108             : 
     109           0 :         if (opt & STATS_LOCAL) {
     110           0 :                 printf("Local package database:\n");
     111           0 :                 printf("\tInstalled packages: %" PRId64 "\n", pkgdb_stats(db, PKG_STATS_LOCAL_COUNT));
     112             : 
     113           0 :                 flatsize = pkgdb_stats(db, PKG_STATS_LOCAL_SIZE);
     114             : 
     115           0 :                 if (show_bytes)
     116           0 :                         printf("\tDisk space occupied: %" PRId64 "\n\n", flatsize);
     117             :                 else {
     118           0 :                         humanize_number(size, sizeof(size), flatsize, "B",
     119             :                             HN_AUTOSCALE, HN_IEC_PREFIXES);
     120           0 :                         printf("\tDisk space occupied: %s\n\n", size);
     121             :                 }
     122             :         }
     123             : 
     124           0 :         if ((opt & STATS_REMOTE) && pkg_repos_total_count() > 0) {
     125           0 :                 printf("Remote package database(s):\n");
     126           0 :                 printf("\tNumber of repositories: %" PRId64 "\n", pkgdb_stats(db, PKG_STATS_REMOTE_REPOS));
     127           0 :                 printf("\tPackages available: %" PRId64 "\n", pkgdb_stats(db, PKG_STATS_REMOTE_COUNT));
     128           0 :                 printf("\tUnique packages: %" PRId64 "\n", pkgdb_stats(db, PKG_STATS_REMOTE_UNIQUE));
     129             : 
     130           0 :                 flatsize = pkgdb_stats(db, PKG_STATS_REMOTE_SIZE);
     131             : 
     132           0 :                 if (show_bytes)
     133           0 :                         printf("\tTotal size of packages: %" PRId64 "\n", flatsize);
     134             :                 else {
     135           0 :                         humanize_number(size, sizeof(size), flatsize, "B",
     136             :                             HN_AUTOSCALE, HN_IEC_PREFIXES);
     137           0 :                         printf("\tTotal size of packages: %s\n", size);
     138             :                 }
     139             :         }
     140             : 
     141           0 :         pkgdb_release_lock(db, PKGDB_LOCK_READONLY);
     142           0 :         pkgdb_close(db);
     143             : 
     144           0 :         return (EX_OK);
     145             : }

Generated by: LCOV version 1.10