Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/src/stats.c
2065 views
1
/*-
2
* Copyright (c) 2011-2012 Marin Atanasov Nikolov <[email protected]>
3
* Copyright (c) 2014 Matthew Seaman <[email protected]>
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
usage_stats(void)
49
{
50
fprintf(stderr, "Usage: pkg stats [-qlrb]\n\n");
51
fprintf(stderr, "For more information see 'pkg help stats'.\n");
52
}
53
54
int
55
exec_stats(int argc, char **argv)
56
{
57
struct pkgdb *db = NULL;
58
int64_t flatsize = 0;
59
unsigned int opt = 0;
60
char size[8];
61
int ch;
62
bool show_bytes = false;
63
64
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
while ((ch = getopt_long(argc, argv, "+blqr", longopts, NULL)) != -1) {
73
switch (ch) {
74
case 'b':
75
show_bytes = true;
76
break;
77
case 'l':
78
opt |= STATS_LOCAL;
79
break;
80
case 'q':
81
quiet = true;
82
break;
83
case 'r':
84
opt |= STATS_REMOTE;
85
break;
86
default:
87
usage_stats();
88
return (EXIT_FAILURE);
89
}
90
}
91
//argv += optind;
92
pkgdb_t dbtype = PKGDB_DEFAULT;
93
94
/* default is to show everything we have */
95
if (opt == 0)
96
opt |= (STATS_LOCAL | STATS_REMOTE);
97
98
if ((opt & STATS_REMOTE) && pkg_repos_activated_count() > 0)
99
dbtype = PKGDB_REMOTE;
100
101
if (pkgdb_open(&db, dbtype) != EPKG_OK) {
102
return (EXIT_FAILURE);
103
}
104
105
if (pkgdb_obtain_lock(db, PKGDB_LOCK_READONLY) != EPKG_OK) {
106
pkgdb_close(db);
107
warnx("Cannot get a read lock on a database, it is locked by another process");
108
return (EXIT_FAILURE);
109
}
110
111
if (opt & STATS_LOCAL) {
112
printf("Local package database:\n");
113
printf("\tInstalled packages: %" PRId64 "\n", pkgdb_stats(db, PKG_STATS_LOCAL_COUNT));
114
115
flatsize = pkgdb_stats(db, PKG_STATS_LOCAL_SIZE);
116
117
if (show_bytes)
118
printf("\tDisk space occupied: %" PRId64 "\n\n", flatsize);
119
else {
120
humanize_number(size, sizeof(size), flatsize, "B",
121
HN_AUTOSCALE, HN_IEC_PREFIXES);
122
printf("\tDisk space occupied: %s\n\n", size);
123
}
124
}
125
126
if ((opt & STATS_REMOTE) && pkg_repos_activated_count() > 0) {
127
printf("Remote package database(s):\n");
128
printf("\tNumber of repositories: %" PRId64 "\n", pkgdb_stats(db, PKG_STATS_REMOTE_REPOS));
129
printf("\tPackages available: %" PRId64 "\n", pkgdb_stats(db, PKG_STATS_REMOTE_COUNT));
130
printf("\tUnique packages: %" PRId64 "\n", pkgdb_stats(db, PKG_STATS_REMOTE_UNIQUE));
131
132
flatsize = pkgdb_stats(db, PKG_STATS_REMOTE_SIZE);
133
134
if (show_bytes)
135
printf("\tTotal size of packages: %" PRId64 "\n", flatsize);
136
else {
137
humanize_number(size, sizeof(size), flatsize, "B",
138
HN_AUTOSCALE, HN_IEC_PREFIXES);
139
printf("\tTotal size of packages: %s\n", size);
140
}
141
}
142
143
pkgdb_release_lock(db, PKGDB_LOCK_READONLY);
144
pkgdb_close(db);
145
146
return (EXIT_SUCCESS);
147
}
148
149