Improved callback/states support to make libxbps std{err,out} printf free.
This is a major API/ABI change, documentation has been updated accordingly.
This commit is contained in:
@@ -85,14 +85,14 @@ bool yesno(const char *, ...);
|
||||
bool noyes(const char *, ...);
|
||||
|
||||
/* from fetch_cb.c */
|
||||
void fetch_file_progress_cb(struct xbps_fetch_cb_data *);
|
||||
void fetch_file_progress_cb(const struct xbps_fetch_cb_data *, void *);
|
||||
|
||||
/* from state_cb.c */
|
||||
void state_cb(struct xbps_state_cb_data *);
|
||||
void state_cb(const struct xbps_state_cb_data *, void *);
|
||||
|
||||
/* from unpack_cb.c */
|
||||
void unpack_progress_cb_verbose(struct xbps_unpack_cb_data *);
|
||||
void unpack_progress_cb(struct xbps_unpack_cb_data *);
|
||||
void unpack_progress_cb_verbose(const struct xbps_unpack_cb_data *, void *);
|
||||
void unpack_progress_cb(const struct xbps_unpack_cb_data *, void *);
|
||||
|
||||
/* From util.c */
|
||||
int show_pkg_files(prop_dictionary_t);
|
||||
|
@@ -47,14 +47,14 @@
|
||||
* Compute and display ETA
|
||||
*/
|
||||
static const char *
|
||||
stat_eta(struct xbps_fetch_cb_data *xfpd)
|
||||
stat_eta(const struct xbps_fetch_cb_data *xfpd, void *cbdata)
|
||||
{
|
||||
struct xferstat *xsp = xfpd->cookie;
|
||||
struct xferstat *xfer = cbdata;
|
||||
static char str[16];
|
||||
long elapsed, eta;
|
||||
off_t received, expected;
|
||||
|
||||
elapsed = xsp->last.tv_sec - xsp->start.tv_sec;
|
||||
elapsed = xfer->last.tv_sec - xfer->start.tv_sec;
|
||||
received = xfpd->file_dloaded - xfpd->file_offset;
|
||||
expected = xfpd->file_size - xfpd->file_dloaded;
|
||||
eta = (long)(elapsed * expected / received);
|
||||
@@ -68,11 +68,6 @@ stat_eta(struct xbps_fetch_cb_data *xfpd)
|
||||
return str;
|
||||
}
|
||||
|
||||
/** High precision double comparison
|
||||
* \param[in] a The first double to compare
|
||||
* \param[in] b The second double to compare
|
||||
* \return true on equal within precison, false if not equal within defined precision.
|
||||
*/
|
||||
static inline bool
|
||||
compare_double(const double a, const double b)
|
||||
{
|
||||
@@ -88,15 +83,15 @@ compare_double(const double a, const double b)
|
||||
* Compute and display transfer rate
|
||||
*/
|
||||
static const char *
|
||||
stat_bps(struct xbps_fetch_cb_data *xfpd)
|
||||
stat_bps(const struct xbps_fetch_cb_data *xfpd, void *cbdata)
|
||||
{
|
||||
struct xferstat *xsp = xfpd->cookie;
|
||||
struct xferstat *xfer = cbdata;
|
||||
static char str[16];
|
||||
char size[8];
|
||||
double delta, bps;
|
||||
|
||||
delta = (xsp->last.tv_sec + (xsp->last.tv_usec / 1.e6))
|
||||
- (xsp->start.tv_sec + (xsp->start.tv_usec / 1.e6));
|
||||
delta = (xfer->last.tv_sec + (xfer->last.tv_usec / 1.e6))
|
||||
- (xfer->start.tv_sec + (xfer->start.tv_usec / 1.e6));
|
||||
if (compare_double(delta, 0.0001)) {
|
||||
snprintf(str, sizeof str, "-- stalled --");
|
||||
} else {
|
||||
@@ -112,73 +107,48 @@ stat_bps(struct xbps_fetch_cb_data *xfpd)
|
||||
* Update the stats display
|
||||
*/
|
||||
static void
|
||||
stat_display(struct xbps_fetch_cb_data *xfpd)
|
||||
stat_display(const struct xbps_fetch_cb_data *xfpd, void *cbdata)
|
||||
{
|
||||
struct xferstat *xsp = xfpd->cookie;
|
||||
struct xferstat *xfer = cbdata;
|
||||
struct timeval now;
|
||||
char totsize[8], recvsize[8];
|
||||
|
||||
gettimeofday(&now, NULL);
|
||||
if (now.tv_sec <= xsp->last.tv_sec)
|
||||
if (now.tv_sec <= xfer->last.tv_sec)
|
||||
return;
|
||||
xsp->last = now;
|
||||
xfer->last = now;
|
||||
|
||||
(void)xbps_humanize_number(totsize, (int64_t)xfpd->file_size);
|
||||
(void)xbps_humanize_number(recvsize, (int64_t)xfpd->file_dloaded);
|
||||
fprintf(stderr,"\r%s: %s [%d%% of %s]", xfpd->file_name, recvsize,
|
||||
(int)((double)(100.0 *
|
||||
(double)xfpd->file_dloaded) / (double)xfpd->file_size), totsize);
|
||||
fprintf(stderr," %s", stat_bps(xfpd));
|
||||
fprintf(stderr," %s", stat_bps(xfpd, xfer));
|
||||
if (xfpd->file_size > 0 && xfpd->file_dloaded > 0 &&
|
||||
xsp->last.tv_sec >= xsp->start.tv_sec + 10)
|
||||
fprintf(stderr," ETA: %s", stat_eta(xfpd));
|
||||
xfer->last.tv_sec >= xfer->start.tv_sec + 10)
|
||||
fprintf(stderr," ETA: %s", stat_eta(xfpd, xfer));
|
||||
|
||||
fprintf(stderr,"\033[K");
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize the transfer statistics
|
||||
*/
|
||||
static void
|
||||
stat_start(struct xferstat *xsp)
|
||||
{
|
||||
gettimeofday(&xsp->start, NULL);
|
||||
xsp->last.tv_sec = xsp->last.tv_usec = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Update the transfer statistics
|
||||
*/
|
||||
static void
|
||||
stat_update(struct xbps_fetch_cb_data *xfpd)
|
||||
{
|
||||
xfpd->file_dloaded += xfpd->file_offset;
|
||||
stat_display(xfpd);
|
||||
}
|
||||
|
||||
/*
|
||||
* Finalize the transfer statistics
|
||||
*/
|
||||
static void
|
||||
stat_end(struct xbps_fetch_cb_data *xfpd)
|
||||
void
|
||||
fetch_file_progress_cb(const struct xbps_fetch_cb_data *xfpd, void *cbdata)
|
||||
{
|
||||
struct xferstat *xfer = cbdata;
|
||||
char size[8];
|
||||
|
||||
(void)xbps_humanize_number(size, (int64_t)xfpd->file_size);
|
||||
fprintf(stderr,"\rDownloaded %s for %s [avg rate: %s]",
|
||||
size, xfpd->file_name, stat_bps(xfpd));
|
||||
fprintf(stderr,"\033[K\n");
|
||||
}
|
||||
|
||||
void
|
||||
fetch_file_progress_cb(struct xbps_fetch_cb_data *xfpd)
|
||||
{
|
||||
struct xferstat *xsp = xfpd->cookie;
|
||||
|
||||
if (xfpd->cb_start)
|
||||
stat_start(xsp);
|
||||
else if (xfpd->cb_update)
|
||||
stat_update(xfpd);
|
||||
else if (xfpd->cb_end)
|
||||
stat_end(xfpd);
|
||||
if (xfpd->cb_start) {
|
||||
/* start transfer stats */
|
||||
gettimeofday(&xfer->start, NULL);
|
||||
xfer->last.tv_sec = xfer->last.tv_usec = 0;
|
||||
} else if (xfpd->cb_update) {
|
||||
/* update transfer stats */
|
||||
stat_display(xfpd, xfer);
|
||||
} else if (xfpd->cb_end) {
|
||||
/* end transfer stats */
|
||||
(void)xbps_humanize_number(size, (int64_t)xfpd->file_size);
|
||||
fprintf(stderr,"\rDownloaded %s for %s [avg rate: %s]",
|
||||
size, xfpd->file_name, stat_bps(xfpd, xfer));
|
||||
fprintf(stderr,"\033[K\n");
|
||||
}
|
||||
}
|
||||
|
@@ -161,13 +161,13 @@ main(int argc, char **argv)
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
xhp->debug = debug;
|
||||
xhp->xbps_state_cb = state_cb;
|
||||
xhp->xbps_fetch_cb = fetch_file_progress_cb;
|
||||
xhp->xfcd->cookie = &xfer;
|
||||
xhp->state_cb = state_cb;
|
||||
xhp->fetch_cb = fetch_file_progress_cb;
|
||||
xhp->fetch_cb_data = &xfer;
|
||||
if (flags & XBPS_FLAG_VERBOSE)
|
||||
xhp->xbps_unpack_cb = unpack_progress_cb_verbose;
|
||||
xhp->unpack_cb = unpack_progress_cb_verbose;
|
||||
else
|
||||
xhp->xbps_unpack_cb = unpack_progress_cb;
|
||||
xhp->unpack_cb = unpack_progress_cb;
|
||||
|
||||
if (rootdir)
|
||||
xhp->rootdir = prop_string_create_cstring(rootdir);
|
||||
|
@@ -38,24 +38,12 @@ pkg_remove_and_purge(const char *pkgname, const char *version, bool purge)
|
||||
{
|
||||
int rv = 0;
|
||||
|
||||
printf("Removing package %s-%s ...\n", pkgname, version);
|
||||
|
||||
if ((rv = xbps_remove_pkg(pkgname, version, false)) != 0) {
|
||||
xbps_error_printf("xbps-bin: unable to remove %s-%s (%s).\n",
|
||||
pkgname, version, strerror(errno));
|
||||
if ((rv = xbps_remove_pkg(pkgname, version, false)) != 0)
|
||||
return rv;
|
||||
}
|
||||
if (purge) {
|
||||
printf(" Purging ... ");
|
||||
(void)fflush(stdout);
|
||||
if ((rv = xbps_purge_pkg(pkgname, false)) != 0) {
|
||||
xbps_error_printf("xbps-bin: unable to purge %s-%s "
|
||||
"(%s).\n", pkgname, version,
|
||||
strerror(errno));
|
||||
|
||||
if (purge)
|
||||
if ((rv = xbps_purge_pkg(pkgname, false)) != 0)
|
||||
return rv;
|
||||
}
|
||||
printf("done.\n");
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
@@ -183,7 +171,7 @@ remove_installed_pkgs(int argc, char **argv, bool yes, bool purge,
|
||||
continue;
|
||||
}
|
||||
|
||||
xbps_printf("WARNING: %s IS REQUIRED BY %u PACKAGE%s:\n\n",
|
||||
printf("WARNING: %s IS REQUIRED BY %u PACKAGE%s:\n\n",
|
||||
pkgver, prop_array_count(reqby),
|
||||
prop_array_count(reqby) > 1 ? "S" : "");
|
||||
for (x = 0; x < prop_array_count(reqby); x++) {
|
||||
|
@@ -25,98 +25,73 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <xbps_api.h>
|
||||
#include "defs.h"
|
||||
|
||||
void
|
||||
state_cb(struct xbps_state_cb_data *xscd)
|
||||
state_cb(const struct xbps_state_cb_data *xscd, void *cbdata)
|
||||
{
|
||||
const struct xbps_handle *xhp = xbps_handle_get();
|
||||
prop_dictionary_t pkgd;
|
||||
const char *opkgver, *state_descr = NULL, *fetchstr;
|
||||
char *pkgname;
|
||||
const char *pkgver;
|
||||
|
||||
if (xscd->desc != NULL && xscd->pkgver == NULL && xscd->err == 0) {
|
||||
printf("\n%s ...\n", xscd->desc);
|
||||
return;
|
||||
}
|
||||
(void)cbdata;
|
||||
|
||||
switch (xscd->state) {
|
||||
case XBPS_STATE_DOWNLOAD:
|
||||
printf("Downloading `%s' (from %s) ...\n",
|
||||
xscd->pkgver, xscd->repourl);
|
||||
break;
|
||||
case XBPS_STATE_DOWNLOAD_FAIL:
|
||||
state_descr = "failed to download binary package";
|
||||
break;
|
||||
case XBPS_STATE_VERIFY:
|
||||
printf("Checking `%s' integrity ...\n", xscd->binpkg_fname);
|
||||
break;
|
||||
case XBPS_STATE_VERIFY_FAIL:
|
||||
state_descr = "failed to verify binary package SHA256";
|
||||
break;
|
||||
case XBPS_STATE_REMOVE:
|
||||
printf("Removing `%s' ...\n", xscd->pkgver);
|
||||
break;
|
||||
case XBPS_STATE_REMOVE_FAIL:
|
||||
state_descr = "failed to remove package";
|
||||
break;
|
||||
case XBPS_STATE_PURGE:
|
||||
printf("Purging `%s' ...\n", xscd->pkgver);
|
||||
break;
|
||||
case XBPS_STATE_PURGE_FAIL:
|
||||
state_descr = "failed to purge package";
|
||||
break;
|
||||
case XBPS_STATE_CONFIGURE:
|
||||
printf("Configuring `%s' ...\n", xscd->pkgver);
|
||||
break;
|
||||
case XBPS_STATE_CONFIGURE_FAIL:
|
||||
state_descr = "failed to configure package";
|
||||
break;
|
||||
case XBPS_STATE_REGISTER_FAIL:
|
||||
state_descr = "failed to register package";
|
||||
break;
|
||||
case XBPS_STATE_REGISTER:
|
||||
case XBPS_STATE_UNREGISTER:
|
||||
case XBPS_STATE_INSTALL:
|
||||
case XBPS_STATE_UNPACK:
|
||||
case XBPS_STATE_REPOSYNC:
|
||||
case XBPS_STATE_CONFIG_FILE:
|
||||
printf("%s\n", xscd->desc);
|
||||
break;
|
||||
case XBPS_STATE_UPDATE:
|
||||
pkgname = xbps_pkg_name(xscd->pkgver);
|
||||
if (pkgname == NULL) {
|
||||
xbps_error_printf("%s: failed to alloc pkgname!\n",
|
||||
__func__);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
pkgd = xbps_find_pkg_dict_installed(pkgname, false);
|
||||
prop_dictionary_get_cstring_nocopy(pkgd, "pkgver", &opkgver);
|
||||
pkgd = xbps_find_pkg_dict_installed(xscd->pkgname, false);
|
||||
prop_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
|
||||
prop_object_release(pkgd);
|
||||
free(pkgname);
|
||||
printf("Updating `%s' to `%s'...\n", opkgver, xscd->pkgver);
|
||||
printf("Updating `%s' to `%s-%s'...\n", pkgver,
|
||||
xscd->pkgname, xscd->version);
|
||||
break;
|
||||
case XBPS_STATE_UPDATE_FAIL:
|
||||
state_descr = "failed to update package";
|
||||
break;
|
||||
case XBPS_STATE_UNPACK:
|
||||
printf("Unpacking `%s' (from ../%s) ...\n",
|
||||
xscd->pkgver, xscd->binpkg_fname);
|
||||
case XBPS_STATE_REMOVE_FILE:
|
||||
case XBPS_STATE_REMOVE_FILE_OBSOLETE:
|
||||
if (xhp->flags & XBPS_FLAG_VERBOSE)
|
||||
printf("%s\n", xscd->desc);
|
||||
else {
|
||||
printf("%s\n", xscd->desc);
|
||||
printf("\033[1A\033[K");
|
||||
}
|
||||
break;
|
||||
case XBPS_STATE_UNPACK_FAIL:
|
||||
state_descr = "failed to unpack binary package";
|
||||
break;
|
||||
case XBPS_STATE_REPOSYNC:
|
||||
printf("Synchronizing index for `%s' ...\n",
|
||||
xscd->repourl);
|
||||
break;
|
||||
case XBPS_STATE_UPDATE_FAIL:
|
||||
case XBPS_STATE_CONFIGURE_FAIL:
|
||||
case XBPS_STATE_REGISTER_FAIL:
|
||||
case XBPS_STATE_UNREGISTER_FAIL:
|
||||
case XBPS_STATE_PURGE_FAIL:
|
||||
case XBPS_STATE_REMOVE_FAIL:
|
||||
case XBPS_STATE_VERIFY_FAIL:
|
||||
case XBPS_STATE_DOWNLOAD_FAIL:
|
||||
case XBPS_STATE_REPOSYNC_FAIL:
|
||||
fetchstr = xbps_fetch_error_string();
|
||||
xbps_error_printf("Failed to sync index: %s\n",
|
||||
fetchstr ? fetchstr : strerror(xscd->err));
|
||||
return;
|
||||
case XBPS_STATE_CONFIG_FILE_FAIL:
|
||||
xbps_error_printf("%s\n", xscd->desc);
|
||||
break;
|
||||
case XBPS_STATE_REMOVE_FILE_FAIL:
|
||||
case XBPS_STATE_REMOVE_FILE_HASH_FAIL:
|
||||
case XBPS_STATE_REMOVE_FILE_OBSOLETE_FAIL:
|
||||
/* Ignore errors due to not empty directories */
|
||||
if (xscd->err == ENOTEMPTY)
|
||||
return;
|
||||
|
||||
xbps_error_printf("%s\n", xscd->desc);
|
||||
break;
|
||||
default:
|
||||
xbps_dbg_printf("%s: unknown state %d %s\n",
|
||||
xscd->pkgver, xscd->state, xscd->desc);
|
||||
xbps_dbg_printf("unknown state %d\n", xscd->state);
|
||||
break;
|
||||
}
|
||||
|
||||
if (state_descr != NULL && xscd->err != 0)
|
||||
xbps_error_printf("%s: %s: %s\n",
|
||||
xscd->pkgver, state_descr, strerror(xscd->err));
|
||||
}
|
||||
|
@@ -30,8 +30,10 @@
|
||||
#include "defs.h"
|
||||
|
||||
void
|
||||
unpack_progress_cb_verbose(struct xbps_unpack_cb_data *xpd)
|
||||
unpack_progress_cb_verbose(const struct xbps_unpack_cb_data *xpd, void *cbdata)
|
||||
{
|
||||
(void)cbdata;
|
||||
|
||||
if (xpd->entry == NULL || xpd->entry_is_metadata)
|
||||
return;
|
||||
else if (xpd->entry_size <= 0)
|
||||
@@ -43,8 +45,10 @@ unpack_progress_cb_verbose(struct xbps_unpack_cb_data *xpd)
|
||||
}
|
||||
|
||||
void
|
||||
unpack_progress_cb(struct xbps_unpack_cb_data *xpd)
|
||||
unpack_progress_cb(const struct xbps_unpack_cb_data *xpd, void *cbdata)
|
||||
{
|
||||
(void)cbdata;
|
||||
|
||||
if (xpd->entry == NULL || xpd->entry_is_metadata)
|
||||
return;
|
||||
else if (xpd->entry_size <= 0)
|
||||
|
@@ -138,9 +138,9 @@ main(int argc, char **argv)
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
xhp->debug = debug;
|
||||
xhp->xbps_state_cb = state_cb;
|
||||
xhp->xbps_fetch_cb = fetch_file_progress_cb;
|
||||
xhp->xfcd->cookie = &xfer;
|
||||
xhp->state_cb = state_cb;
|
||||
xhp->fetch_cb = fetch_file_progress_cb;
|
||||
xhp->fetch_cb_data = &xfer;
|
||||
if (rootdir)
|
||||
xhp->rootdir = prop_string_create_cstring(rootdir);
|
||||
if (cachedir)
|
||||
@@ -190,7 +190,7 @@ main(int argc, char **argv)
|
||||
|
||||
rv = show_pkg_info_from_repolist(argv[1], option);
|
||||
if (rv == ENOENT) {
|
||||
xbps_printf("Unable to locate package "
|
||||
printf("Unable to locate package "
|
||||
"`%s' in repository pool.\n", argv[1]);
|
||||
} else if (rv == ENOTSUP) {
|
||||
xbps_error_printf("xbps-repo: no repositories "
|
||||
@@ -207,7 +207,7 @@ main(int argc, char **argv)
|
||||
|
||||
rv = show_pkg_deps_from_repolist(argv[1]);
|
||||
if (rv == ENOENT) {
|
||||
xbps_printf("Unable to locate package "
|
||||
printf("Unable to locate package "
|
||||
"`%s' in repository pool.\n", argv[1]);
|
||||
} else if (rv == ENOTSUP) {
|
||||
xbps_error_printf("xbps-repo: no repositories "
|
||||
@@ -229,7 +229,7 @@ main(int argc, char **argv)
|
||||
xbps_error_printf("xbps-repo: no repositories "
|
||||
"currently registered!\n");
|
||||
} else if (errno == ENOENT) {
|
||||
xbps_printf("Unable to locate package `%s' "
|
||||
printf("Unable to locate package `%s' "
|
||||
"in repository pool.\n", argv[1]);
|
||||
} else {
|
||||
xbps_error_printf("xbps-repo: unexpected "
|
||||
|
@@ -35,7 +35,10 @@
|
||||
#include <xbps_api.h>
|
||||
#include "../xbps-bin/defs.h"
|
||||
#include "defs.h"
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
int
|
||||
show_pkg_info_from_repolist(const char *pkgname, const char *option)
|
||||
|
@@ -158,8 +158,8 @@ main(int argc, char **argv)
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
xhp->debug = debug;
|
||||
xhp->xbps_fetch_cb = fetch_file_progress_cb;
|
||||
xhp->xfcd->cookie = &xfer;
|
||||
xhp->fetch_cb = fetch_file_progress_cb;
|
||||
xhp->fetch_cb_data = &xfer;
|
||||
if (rootdir)
|
||||
xhp->rootdir = prop_string_create_cstring(rootdir);
|
||||
if (confdir)
|
||||
|
Reference in New Issue
Block a user