Introduce struct xbps_handle and use it for xbps_init().

This structure sets up function callbacks for fetching files and
unpacking binary packages, as well as setting the debug boolean.

This way the affected functions (xbps_fetch_file() and
xbps_unpack_binary_pkg()) do not need to accept the fn cb pointers
and data as arguments.

Bump XBPS_RELVER.
This commit is contained in:
Juan RP
2011-02-21 13:38:44 +01:00
parent 0bd533f8a9
commit 22ae7aa2e8
11 changed files with 212 additions and 204 deletions

View File

@@ -94,7 +94,6 @@ static int
download_package_list(prop_object_iterator_t iter, bool only_show)
{
prop_object_t obj;
struct xbps_fetch_progress_data xfpd;
const char *pkgver, *repoloc, *filename, *cachedir, *sha256;
char *binfile;
int rv = 0;
@@ -147,8 +146,7 @@ again:
return errno;
}
printf("Downloading %s binary package ...\n", pkgver);
rv = xbps_fetch_file(binfile, cachedir, false, NULL,
fetch_file_progress_cb, &xfpd);
rv = xbps_fetch_file(binfile, cachedir, false, NULL);
if (rv == -1) {
xbps_error_printf("xbps-bin: couldn't download `%s'\n",
filename);
@@ -373,45 +371,11 @@ xbps_update_pkg(const char *pkgname)
return 0;
}
static void
unpack_progress_cb_verbose(void *data)
{
struct xbps_unpack_progress_data *xpd = data;
if (xpd->entry == NULL || xpd->entry_is_metadata)
return;
else if (xpd->entry_size <= 0)
return;
fprintf(stderr, "Extracted %sfile `%s' (%" PRIi64 " bytes)\n",
xpd->entry_is_conf ? "configuration " : "", xpd->entry,
xpd->entry_size);
}
static void
unpack_progress_cb_percentage(void *data)
{
struct xbps_unpack_progress_data *xpd = data;
double percent = 0;
if (xpd->entry_is_metadata)
return;
percent =
(double)((xpd->entry_extract_count * 100.0) / xpd->entry_total_count);
if (percent > 100.0 ||
xpd->entry_extract_count >= xpd->entry_total_count)
percent = 100.0;
printf("\033[s(%3.2f%%)\033[u", percent);
}
static int
exec_transaction(struct transaction *trans)
{
prop_dictionary_t instpkgd;
prop_object_t obj;
struct xbps_unpack_progress_data xpd;
const char *pkgname, *version, *pkgver, *instver, *filen, *tract;
int flags = xbps_get_flags(), rv = 0;
bool update, preserve, autoinst;
@@ -528,15 +492,7 @@ exec_transaction(struct transaction *trans)
* Unpack binary package.
*/
printf("Unpacking `%s' (from ../%s) ... ", pkgver, filen);
if (flags & XBPS_FLAG_VERBOSE) {
rv = xbps_unpack_binary_pkg(obj,
unpack_progress_cb_verbose, &xpd);
printf("\n");
} else {
rv = xbps_unpack_binary_pkg(obj,
unpack_progress_cb_percentage, &xpd);
}
if (rv != 0) {
if ((rv = xbps_unpack_binary_pkg(obj)) != 0) {
xbps_error_printf("xbps-bin: error unpacking %s "
"(%s)\n", pkgver, strerror(rv));
return rv;

View File

@@ -149,10 +149,46 @@ cleanup(int signum)
exit(signum);
}
static void
unpack_progress_cb_verbose(void *data)
{
struct xbps_unpack_progress_data *xpd = data;
if (xpd->entry == NULL || xpd->entry_is_metadata)
return;
else if (xpd->entry_size <= 0)
return;
fprintf(stderr, "Extracted %sfile `%s' (%" PRIi64 " bytes)\n",
xpd->entry_is_conf ? "configuration " : "", xpd->entry,
xpd->entry_size);
}
static void
unpack_progress_cb_percentage(void *data)
{
struct xbps_unpack_progress_data *xpd = data;
double percent = 0;
if (xpd->entry_is_metadata)
return;
percent =
(double)((xpd->entry_extract_count * 100.0) / xpd->entry_total_count);
if (percent > 100.0 ||
xpd->entry_extract_count >= xpd->entry_total_count)
percent = 100.0;
printf("\033[s(%3.2f%%)\033[u", percent);
}
int
main(int argc, char **argv)
{
prop_dictionary_t dict;
struct xbps_handle xh;
struct xbps_unpack_progress_data xupd;
struct xbps_fetch_progress_data xfpd;
struct list_pkgver_cb lpc;
struct sigaction sa;
int i , c, flags, rv;
@@ -225,7 +261,16 @@ main(int argc, char **argv)
/*
* Initialize stuff for libxbps.
*/
xbps_init(with_debug);
memset(&xh, 0, sizeof(xh));
xh.with_debug = with_debug;
xh.xbps_fetch_cb = fetch_file_progress_cb;
xh.xfpd = &xfpd;
if (flags & XBPS_FLAG_VERBOSE)
xh.xbps_unpack_cb = unpack_progress_cb_verbose;
else
xh.xbps_unpack_cb = unpack_progress_cb_percentage;
xh.xupd = &xupd;
xbps_init(&xh);
if ((dict = xbps_regpkgdb_dictionary_get()) == NULL) {
if (errno && errno != ENOENT) {

View File

@@ -77,6 +77,8 @@ repo_search_pkgs_cb(struct repository_pool_index *rpi, void *arg, bool *done)
int
main(int argc, char **argv)
{
struct xbps_handle xh;
struct xbps_fetch_progress_data xfpd;
prop_dictionary_t pkgd;
char *root;
int c, rv = 0;
@@ -110,7 +112,14 @@ main(int argc, char **argv)
if (argc < 1)
usage();
xbps_init(with_debug);
/*
* Initialize the function callbacks and debug in libxbps.
*/
memset(&xh, 0, sizeof(xh));
xh.with_debug = with_debug;
xh.xbps_fetch_cb = fetch_file_progress_cb;
xh.xfpd = &xfpd;
xbps_init(&xh);
if ((rv = xbps_repository_pool_init()) != 0) {
if (rv != ENOENT) {

View File

@@ -164,7 +164,6 @@ int
register_repository(const char *uri)
{
struct repoinfo *rpi = NULL;
struct xbps_fetch_progress_data xfpd;
const char *idxstr = NULL;
char *metadir, *plist;
int rv = 0;
@@ -174,8 +173,7 @@ register_repository(const char *uri)
if (xbps_check_is_repository_uri_remote(idxstr)) {
printf("Fetching remote package index at %s...\n", idxstr);
rv = xbps_repository_sync_pkg_index(idxstr,
fetch_file_progress_cb, &xfpd);
rv = xbps_repository_sync_pkg_index(idxstr);
if (rv == -1) {
xbps_error_printf("xbps-repo: couldn't fetch pkg-index"
"file: %s.\n", xbps_fetch_error_string());
@@ -295,7 +293,6 @@ show_pkg_deps_from_repolist(const char *pkgname)
static int
repo_sync_pkg_index_cb(struct repository_pool_index *rpi, void *arg, bool *done)
{
struct xbps_fetch_progress_data xfpd;
struct repoinfo *rp;
char *plist;
int rv = 0;
@@ -307,8 +304,7 @@ repo_sync_pkg_index_cb(struct repository_pool_index *rpi, void *arg, bool *done)
return 0;
printf("Syncing package index from: %s\n", rpi->rpi_uri);
rv = xbps_repository_sync_pkg_index(rpi->rpi_uri,
fetch_file_progress_cb, &xfpd);
rv = xbps_repository_sync_pkg_index(rpi->rpi_uri);
if (rv == -1) {
xbps_error_printf("xbps-repo: failed to sync `%s': %s\n",
rpi->rpi_uri, xbps_fetch_error_string());

View File

@@ -106,6 +106,7 @@ usage(void)
int
main(int argc, char **argv)
{
struct xbps_handle xh;
struct xbps_fetch_progress_data xfpd;
prop_dictionary_t dict;
const char *version;
@@ -137,7 +138,14 @@ main(int argc, char **argv)
if (argc < 1)
usage();
xbps_init(debug);
/*
* Initialize the callbacks and debug in libxbps.
*/
memset(&xh, 0, sizeof(xh));
xh.with_debug = debug;
xh.xbps_fetch_cb = fetch_file_progress_cb;
xh.xfpd = &xfpd;
xbps_init(&xh);
plist = xbps_xasprintf("%s/%s/%s", xbps_get_rootdir(),
XBPS_META_PATH, XBPS_REGPKGDB);
@@ -336,8 +344,7 @@ main(int argc, char **argv)
usage();
for (i = 1; i < argc; i++) {
rv = xbps_fetch_file(argv[i], ".", false, "v",
fetch_file_progress_cb, &xfpd);
rv = xbps_fetch_file(argv[i], ".", false, "v");
if (rv == -1) {
printf("%s: %s\n", argv[1],
xbps_fetch_error_string());