Radish alpha
H
HardenedBSD Package Manager
Radicle
Git (anonymous pull)
Log in to clone via SSH
Initial commit of 'pkg stats' command
Marin Atanasov Nikolov committed 13 years ago
commit 6262427027a45d6c91dfb94a5a2aa1036d749b5c
parent d59f91c083c376c4341abad1632f397d4e5c53e8
6 files changed +130 -1
modified libpkg/pkg.h
@@ -265,6 +265,11 @@ typedef enum {
	PKG_CONFIG_KV_VALUE
} pkg_config_kv_t;

+
typedef enum _pkg_stats_t {
+
	PKG_STATS_INSTALLED = 0,
+
	PKG_STATS_INSTALLED_SIZE,
+
} pkg_stats_t;
+

/**
 * Error type used everywhere by libpkg.
 */
@@ -818,6 +823,14 @@ int pkg_create_staged(const char *, pkg_formats, const char *, const char *, cha
int pkg_update(const char *name, const char *packagesite);

/**
+
 * Get statistics information from the package database(s)
+
 * @param db A valid database object as returned by pkgdb_open()
+
 * @param type Type of statistics to be returned
+
 * @return The statistic information requested
+
 */
+
int64_t pkgdb_stats(struct pkgdb *db, pkg_stats_t type);
+

+
/**
 * Get the value of a configuration key
 */
int pkg_config_string(pkg_config_key key, const char **value);
modified libpkg/pkgdb.c
@@ -3303,3 +3303,35 @@ pkgdb_unlock(struct pkgdb *db)
{
	return sql_exec(db->sqlite, "PRAGMA main.locking_mode=NORMAL;BEGIN IMMEDIATE;COMMIT;");
}
+

+
int64_t
+
pkgdb_stats(struct pkgdb *db, pkg_stats_t type)
+
{
+
	sqlite3_stmt *stmt = NULL;
+
	const char *sql = NULL;
+
	int64_t stats = 0;
+

+
	assert(db != NULL);
+

+
	switch(type) {
+
	case PKG_STATS_INSTALLED:
+
		sql = "SELECT COUNT(p.id) FROM main.packages AS p;";
+
		break;
+
	case PKG_STATS_INSTALLED_SIZE:
+
		sql = "SELECT SUM(p.flatsize) FROM main.packages AS p;";
+
		break;
+
	}
+

+
	if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK) {
+
		ERROR_SQLITE(db->sqlite);
+
		return (-1);
+
	}
+
	
+
	while (sqlite3_step(stmt) != SQLITE_DONE) {
+
		stats = sqlite3_column_int64(stmt, 0);
+
	}
+

+
	sqlite3_finalize(stmt);
+

+
	return (stats);
+
}
modified pkg/Makefile
@@ -26,7 +26,8 @@ SRCS= add.c \
		version.c \
		which.c \
		fetch.c \
-
		shell.c
+
		shell.c \
+
		stats.c

PREFIX?=	/usr/local
BINDIR=		${PREFIX}/sbin
modified pkg/main.c
@@ -80,6 +80,7 @@ static struct commands {
	{ "rquery", "Query information from the remote repository", exec_rquery, usage_rquery},
	{ "shell", "Open a debug shell", exec_shell, usage_shell},
	{ "shlib", "Displays which package links against a specific shared library", exec_shlib, usage_shlib},
+
	{ "stats", "Display package database statistics", exec_stats, usage_stats},
	{ "update", "Updates remote package repository databases", exec_update, usage_update},
	{ "updating", "Displays UPDATING information for a package", exec_updating, usage_updating},
	{ "upgrade", "Performs upgrades of package software distributions", exec_upgrade, usage_upgrade},
modified pkg/pkgcli.h
@@ -99,6 +99,10 @@ int exec_shlib(int, char **);
void usage_shlib(void);
char *sanitize(char *, const char *, size_t);

+
/* pkg stats */
+
int exec_stats(int, char **);
+
void usage_stats(void);
+

/* pkg update */
int exec_update(int, char **);
void usage_update(void);
added pkg/stats.c
@@ -0,0 +1,78 @@
+
/*
+
 * Copyright (c) 2011-2012 Marin Atanasov Nikolov <dnaeon@gmail.com>
+
 * All rights reserved.
+
 * 
+
 * Redistribution and use in source and binary forms, with or without
+
 * modification, are permitted provided that the following conditions
+
 * are met:
+
 * 1. Redistributions of source code must retain the above copyright
+
 *    notice, this list of conditions and the following disclaimer
+
 *    in this position and unchanged.
+
 * 2. Redistributions in binary form must reproduce the above copyright
+
 *    notice, this list of conditions and the following disclaimer in the
+
 *    documentation and/or other materials provided with the distribution.
+
 * 
+
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
+
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
+
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
 */
+

+
#include <stdio.h>
+
#include <sysexits.h>
+
#include <unistd.h>
+
#include <inttypes.h>
+

+
#include <pkg.h>
+

+
#include "pkgcli.h"
+

+
void
+
usage_stats(void)
+
{
+
	fprintf(stderr, "usage: pkg stats [-q]\n\n");
+
	fprintf(stderr, "For more information see 'pkg help stats'.\n");
+
}
+

+
int
+
exec_stats(int argc, char **argv)
+
{
+
	struct pkgdb *db = NULL;
+
	int retcode = EX_OK;
+
	int ch;
+

+
	while ((ch = getopt(argc, argv, "q")) != -1) {
+
                switch (ch) {
+
		case 'q':
+
			quiet = true;
+
			break;
+
		default:
+
			usage_stats();
+
			return (EX_USAGE);
+
                }
+
        }
+
        argc -= optind;
+
        argv += optind;
+
	
+
	if (argc > 2) {
+
		usage_stats();
+
		return (EX_USAGE);
+
	}
+

+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
+
		return (EX_IOERR);
+
	}
+

+
       	printf("installed packages: %" PRId64 "\n", pkgdb_stats(db, PKG_STATS_INSTALLED));
+
	
+
	
+
	pkgdb_close(db);
+

+
	return (retcode);
+
}