Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add pkg-plugins-stats plugin to the repository for displaying pkg stats
Marin Atanasov Nikolov committed 13 years ago
commit 3a35daea089cd17f4d74e96b5abdd342e25c80bd
parent ffe16cc
5 files changed +201 -0
added plugins/pkg-plugin-stats/Makefile
@@ -0,0 +1,18 @@
+
.include <bsd.own.mk>
+

+
LIB=		pkg-plugin-stats
+
INCS=		stats.h
+
#WARNS=		6
+
PREFIX?=	/usr/local
+
LIBDIR=		${PREFIX}/lib
+
INCLUDEDIR=	${PREFIX}/include
+
SHLIB_MAJOR=	0
+

+
SRCS=		stats.c
+

+
CFLAGS+=	-std=c99 -fPIC -shared
+
CFLAGS+=	-I${INCLUDEDIR}
+

+
DEBUG_FLAGS+=  -g -O0
+

+
.include <bsd.lib.mk>
added plugins/pkg-plugin-stats/README.md
@@ -0,0 +1,43 @@
+
## General Information
+

+
The *pkg-plugin-stats* plugin is used for displaying package stats during:
+

+
* pre-install
+
* post-install
+
* pre-deinstall
+
* post-deinstall
+

+
## How to build the plugin?
+

+
In order to build the plugin enter into the plugin's directory and run make(1), e.g.:
+

+
	$ cd /path/to/pkg-plugins-stats
+
	$ make
+
	
+
Once the plugin is built you can install it using the following command:
+

+
	$ make install 
+
	
+
The plugin will be installed as a shared library in ${PREFIX}/lib/libpkg-plugin-stats.so
+

+
## Configuring the plugin
+

+
In order to configure the plugin simply copy the *stats.conf* file to the pkgng plugins directory,
+
which by default is set to */usr/local/etc/pkg/plugins*, unless you've specified it elsewhere by 
+
using the *PKG\_PLUGINS\_DIR* option.
+

+
	$ cp /path/to/pkg-plugins-stats/stats.conf /usr/local/etc/pkg/plugins/
+
	
+
## Testing the plugin
+

+
To test the plugin, first check that it is recongnized and
+
loaded by pkgng by executing the `pkg plugins` command:
+

+
	$ pkg plugins
+
	NAME       DESC                                VERSION    LOADED    
+
	stats      Plugin for displaying package stats 1.0        YES     
+

+
If the plugin shows up correctly then you are good to go!
+

+
Now go ahead and install/deintall a package and see it in action! :)
+

added plugins/pkg-plugin-stats/stats.c
@@ -0,0 +1,97 @@
+
/*
+
 * Copyright (c) 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 <assert.h>
+
#include <stdio.h>
+
#include <unistd.h>
+
#include <inttypes.h>
+
#include <libutil.h>
+

+
#include <pkg.h>
+

+
#include "stats.h"
+

+
#define PLUGIN_NAME "stats"
+

+
int
+
pkg_plugins_init_stats(void)
+
{
+
	/*
+
	 * Hook into the library and provide package stats for the following actions:
+
	 *
+
	 * - pre-install 
+
	 * - post-install
+
	 * - pre-deinstall
+
	 * - post-deinstall
+
	 */
+
	
+
	if (pkg_plugins_hook(PLUGIN_NAME, PKG_PLUGINS_HOOK_PRE_INSTALL, &plugin_stats_callback) != EPKG_OK) {
+
		fprintf(stderr, "Plugin '%s' failed to hook into the library\n", PLUGIN_NAME);
+
		return (EPKG_FATAL);
+
	}
+

+
	if (pkg_plugins_hook(PLUGIN_NAME, PKG_PLUGINS_HOOK_POST_INSTALL, &plugin_stats_callback) != EPKG_OK) {
+
		fprintf(stderr, "Plugin '%s' failed to hook into the library\n", PLUGIN_NAME);
+
		return (EPKG_FATAL);
+
	}
+
	if (pkg_plugins_hook(PLUGIN_NAME, PKG_PLUGINS_HOOK_PRE_DEINSTALL, &plugin_stats_callback) != EPKG_OK) {
+
		fprintf(stderr, "Plugin '%s' failed to hook into the library\n", PLUGIN_NAME);
+
		return (EPKG_FATAL);
+
	}
+
	
+
	if (pkg_plugins_hook(PLUGIN_NAME, PKG_PLUGINS_HOOK_POST_DEINSTALL, &plugin_stats_callback) != EPKG_OK) {
+
		fprintf(stderr, "Plugin '%s' failed to hook into the library\n", PLUGIN_NAME);
+
		return (EPKG_FATAL);
+
	}
+
	
+
	return (EPKG_OK);
+
}
+

+
int
+
pkg_plugins_shutdown_template(void)
+
{
+
	/* nothing to be done here */
+

+
	return (EPKG_OK);
+
}
+

+
int
+
plugin_stats_callback(void *data, struct pkgdb *db)
+
{
+
        int64_t flatsize = 0;
+
        char size[7];
+

+
	assert(db != NULL);
+
	/* assert(data != NULL); */
+

+
	flatsize = pkgdb_stats(db, PKG_STATS_LOCAL_SIZE);
+
	humanize_number(size, sizeof(flatsize), flatsize, "B", HN_AUTOSCALE, 0);
+
	printf(">>> Installed packages : %" PRId64 " | Disk space: %s <<<\n",
+
	       pkgdb_stats(db, PKG_STATS_LOCAL_COUNT),
+
	       size);
+

+
	return (EPKG_OK);
+
}
added plugins/pkg-plugin-stats/stats.conf
@@ -0,0 +1,6 @@
+
# Configuration file for pkg-plugin-stats
+
enabled=YES
+
name=stats
+
description=Plugin for displaying package stats
+
version=1.0
+
plugin=/usr/local/lib/libpkg-plugin-stats.so
added plugins/pkg-plugin-stats/stats.h
@@ -0,0 +1,37 @@
+
/*
+
 * Copyright (c) 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.
+
 */
+

+
#ifndef _PKG_PLUGINS_STATS_H
+
#define _PKG_PLUGINS_STATS_H
+

+
/* callback functions */
+
int plugin_stats_callback(void *data, struct pkgdb *db);
+

+
/* plugin init and shutdown functions */
+
int pkg_plugins_init_stats(void);
+
int pkg_plugins_shutdown_stats(void);
+

+
#endif /* !_PKG_PLUGINS_STATS_H */