Branch data 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 <unistd.h>
40 : :
41 : : #include <pkg.h>
42 : :
43 : : #include <bsd_compat.h>
44 : :
45 : : #include "pkgcli.h"
46 : :
47 : : void
48 : 0 : usage_stats(void)
49 : : {
50 : 0 : fprintf(stderr, "Usage: pkg stats [-qlrb]\n\n");
51 : 0 : fprintf(stderr, "For more information see 'pkg help stats'.\n");
52 : 0 : }
53 : :
54 : : int
55 : 0 : exec_stats(int argc, char **argv)
56 : : {
57 : 0 : struct pkgdb *db = NULL;
58 : 0 : int64_t flatsize = 0;
59 : 0 : unsigned int opt = 0;
60 : : char size[8];
61 : : int ch;
62 : 0 : bool show_bytes = false;
63 : :
64 : 0 : struct option longopts[] = {
65 : : { "bytes", no_argument, NULL, 'b' },
66 : : { "local", no_argument, NULL, 'l' },
67 : : { "quiet", no_argument, NULL, 'q' },
68 : : { "remote", no_argument, NULL, 'r' },
69 : : { NULL, 0, NULL, 0 },
70 : : };
71 : :
72 [ # # ]: 0 : while ((ch = getopt_long(argc, argv, "+blqr", longopts, NULL)) != -1) {
73 [ # # # # : 0 : switch (ch) {
# ]
74 : : case 'b':
75 : 0 : show_bytes = true;
76 : 0 : break;
77 : : case 'l':
78 : 0 : opt |= STATS_LOCAL;
79 : 0 : break;
80 : : case 'q':
81 : 0 : quiet = true;
82 : 0 : break;
83 : : case 'r':
84 : 0 : opt |= STATS_REMOTE;
85 : 0 : break;
86 : : default:
87 : 0 : usage_stats();
88 : 0 : return (EXIT_FAILURE);
89 : : }
90 : : }
91 : : //argv += optind;
92 : :
93 : : /* default is to show everything we have */
94 [ # # ]: 0 : if (opt == 0)
95 : 0 : opt |= (STATS_LOCAL | STATS_REMOTE);
96 : :
97 [ # # ]: 0 : if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {
98 : 0 : return (EXIT_FAILURE);
99 : : }
100 : :
101 [ # # ]: 0 : if (pkgdb_obtain_lock(db, PKGDB_LOCK_READONLY) != EPKG_OK) {
102 : 0 : pkgdb_close(db);
103 : 0 : warnx("Cannot get a read lock on a database, it is locked by another process");
104 : 0 : return (EXIT_FAILURE);
105 : : }
106 : :
107 [ # # ]: 0 : if (opt & STATS_LOCAL) {
108 : 0 : printf("Local package database:\n");
109 : 0 : printf("\tInstalled packages: %" PRId64 "\n", pkgdb_stats(db, PKG_STATS_LOCAL_COUNT));
110 : :
111 : 0 : flatsize = pkgdb_stats(db, PKG_STATS_LOCAL_SIZE);
112 : :
113 [ # # ]: 0 : if (show_bytes)
114 : 0 : printf("\tDisk space occupied: %" PRId64 "\n\n", flatsize);
115 : : else {
116 : 0 : humanize_number(size, sizeof(size), flatsize, "B",
117 : : HN_AUTOSCALE, HN_IEC_PREFIXES);
118 : 0 : printf("\tDisk space occupied: %s\n\n", size);
119 : : }
120 : 0 : }
121 : :
122 [ # # # # ]: 0 : if ((opt & STATS_REMOTE) && pkg_repos_total_count() > 0) {
123 : 0 : printf("Remote package database(s):\n");
124 : 0 : printf("\tNumber of repositories: %" PRId64 "\n", pkgdb_stats(db, PKG_STATS_REMOTE_REPOS));
125 : 0 : printf("\tPackages available: %" PRId64 "\n", pkgdb_stats(db, PKG_STATS_REMOTE_COUNT));
126 : 0 : printf("\tUnique packages: %" PRId64 "\n", pkgdb_stats(db, PKG_STATS_REMOTE_UNIQUE));
127 : :
128 : 0 : flatsize = pkgdb_stats(db, PKG_STATS_REMOTE_SIZE);
129 : :
130 [ # # ]: 0 : if (show_bytes)
131 : 0 : printf("\tTotal size of packages: %" PRId64 "\n", flatsize);
132 : : else {
133 : 0 : humanize_number(size, sizeof(size), flatsize, "B",
134 : : HN_AUTOSCALE, HN_IEC_PREFIXES);
135 : 0 : printf("\tTotal size of packages: %s\n", size);
136 : : }
137 : 0 : }
138 : :
139 : 0 : pkgdb_release_lock(db, PKGDB_LOCK_READONLY);
140 : 0 : pkgdb_close(db);
141 : :
142 : 0 : return (EXIT_SUCCESS);
143 : 0 : }
|