Use xfcd->cookie to pass user-supplied data to the fetch cb.
This commit is contained in:
		| @@ -30,8 +30,14 @@ | |||||||
| #define __UNCONST(a)    ((void *)(unsigned long)(const void *)(a)) | #define __UNCONST(a)    ((void *)(unsigned long)(const void *)(a)) | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  | #include <sys/time.h> | ||||||
| #include <xbps_api.h> | #include <xbps_api.h> | ||||||
|  |  | ||||||
|  | struct xferstat { | ||||||
|  | 	struct timeval start; | ||||||
|  | 	struct timeval last; | ||||||
|  | }; | ||||||
|  |  | ||||||
| /* from install.c */ | /* from install.c */ | ||||||
| int	install_new_pkg(const char *); | int	install_new_pkg(const char *); | ||||||
| int	update_pkg(const char *); | int	update_pkg(const char *); | ||||||
|   | |||||||
| @@ -43,17 +43,13 @@ | |||||||
| #include <xbps_api.h> | #include <xbps_api.h> | ||||||
| #include "defs.h" | #include "defs.h" | ||||||
|  |  | ||||||
| struct xferstat { |  | ||||||
| 	struct timeval start; |  | ||||||
| 	struct timeval last; |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| /* | /* | ||||||
|  * Compute and display ETA |  * Compute and display ETA | ||||||
|  */ |  */ | ||||||
| static const char * | static const char * | ||||||
| stat_eta(struct xbps_fetch_cb_data *xfpd, struct xferstat *xsp) | stat_eta(struct xbps_fetch_cb_data *xfpd) | ||||||
| { | { | ||||||
|  | 	struct xferstat *xsp = xfpd->cookie; | ||||||
| 	static char str[16]; | 	static char str[16]; | ||||||
| 	long elapsed, eta; | 	long elapsed, eta; | ||||||
| 	off_t received, expected; | 	off_t received, expected; | ||||||
| @@ -92,8 +88,9 @@ compare_double(const double a, const double b) | |||||||
|  * Compute and display transfer rate |  * Compute and display transfer rate | ||||||
|  */ |  */ | ||||||
| static const char * | static const char * | ||||||
| stat_bps(struct xbps_fetch_cb_data *xfpd, struct xferstat *xsp) | stat_bps(struct xbps_fetch_cb_data *xfpd) | ||||||
| { | { | ||||||
|  | 	struct xferstat *xsp = xfpd->cookie; | ||||||
| 	static char str[16]; | 	static char str[16]; | ||||||
| 	char size[8]; | 	char size[8]; | ||||||
| 	double delta, bps; | 	double delta, bps; | ||||||
| @@ -115,8 +112,9 @@ stat_bps(struct xbps_fetch_cb_data *xfpd, struct xferstat *xsp) | |||||||
|  * Update the stats display |  * Update the stats display | ||||||
|  */ |  */ | ||||||
| static void | static void | ||||||
| stat_display(struct xbps_fetch_cb_data *xfpd, struct xferstat *xsp) | stat_display(struct xbps_fetch_cb_data *xfpd) | ||||||
| { | { | ||||||
|  | 	struct xferstat *xsp = xfpd->cookie; | ||||||
| 	struct timeval now; | 	struct timeval now; | ||||||
| 	char totsize[8], recvsize[8]; | 	char totsize[8], recvsize[8]; | ||||||
|  |  | ||||||
| @@ -130,10 +128,10 @@ stat_display(struct xbps_fetch_cb_data *xfpd, struct xferstat *xsp) | |||||||
| 	fprintf(stderr,"\r%s: %s [%d%% of %s]", xfpd->file_name, recvsize, | 	fprintf(stderr,"\r%s: %s [%d%% of %s]", xfpd->file_name, recvsize, | ||||||
| 	    (int)((double)(100.0 * | 	    (int)((double)(100.0 * | ||||||
| 	    (double)xfpd->file_dloaded) / (double)xfpd->file_size), totsize); | 	    (double)xfpd->file_dloaded) / (double)xfpd->file_size), totsize); | ||||||
| 	fprintf(stderr," %s", stat_bps(xfpd, xsp)); | 	fprintf(stderr," %s", stat_bps(xfpd)); | ||||||
| 	if (xfpd->file_size > 0 && xfpd->file_dloaded > 0 && | 	if (xfpd->file_size > 0 && xfpd->file_dloaded > 0 && | ||||||
| 	    xsp->last.tv_sec >= xsp->start.tv_sec + 10) | 	    xsp->last.tv_sec >= xsp->start.tv_sec + 10) | ||||||
| 		fprintf(stderr," ETA: %s", stat_eta(xfpd, xsp)); | 		fprintf(stderr," ETA: %s", stat_eta(xfpd)); | ||||||
|  |  | ||||||
| 	fprintf(stderr,"\033[K"); | 	fprintf(stderr,"\033[K"); | ||||||
| } | } | ||||||
| @@ -152,35 +150,35 @@ stat_start(struct xferstat *xsp) | |||||||
|  * Update the transfer statistics |  * Update the transfer statistics | ||||||
|  */ |  */ | ||||||
| static void | static void | ||||||
| stat_update(struct xbps_fetch_cb_data *xfpd, struct xferstat *xsp) | stat_update(struct xbps_fetch_cb_data *xfpd) | ||||||
| { | { | ||||||
| 	xfpd->file_dloaded += xfpd->file_offset; | 	xfpd->file_dloaded += xfpd->file_offset; | ||||||
| 	stat_display(xfpd, xsp); | 	stat_display(xfpd); | ||||||
| } | } | ||||||
|  |  | ||||||
| /* | /* | ||||||
|  * Finalize the transfer statistics |  * Finalize the transfer statistics | ||||||
|  */ |  */ | ||||||
| static void | static void | ||||||
| stat_end(struct xbps_fetch_cb_data *xfpd, struct xferstat *xsp) | stat_end(struct xbps_fetch_cb_data *xfpd) | ||||||
| { | { | ||||||
| 	char size[8]; | 	char size[8]; | ||||||
|  |  | ||||||
| 	(void)xbps_humanize_number(size, (int64_t)xfpd->file_size); | 	(void)xbps_humanize_number(size, (int64_t)xfpd->file_size); | ||||||
| 	fprintf(stderr,"\rDownloaded %s for %s [avg rate: %s]", | 	fprintf(stderr,"\rDownloaded %s for %s [avg rate: %s]", | ||||||
| 	    size, xfpd->file_name, stat_bps(xfpd, xsp)); | 	    size, xfpd->file_name, stat_bps(xfpd)); | ||||||
| 	fprintf(stderr,"\033[K\n"); | 	fprintf(stderr,"\033[K\n"); | ||||||
| } | } | ||||||
|  |  | ||||||
| void | void | ||||||
| fetch_file_progress_cb(struct xbps_fetch_cb_data *xfpd) | fetch_file_progress_cb(struct xbps_fetch_cb_data *xfpd) | ||||||
| { | { | ||||||
| 	static struct xferstat xs; | 	struct xferstat *xsp = xfpd->cookie; | ||||||
|  |  | ||||||
| 	if (xfpd->cb_start) | 	if (xfpd->cb_start) | ||||||
| 		stat_start(&xs); | 		stat_start(xsp); | ||||||
| 	else if (xfpd->cb_update) | 	else if (xfpd->cb_update) | ||||||
| 		stat_update(xfpd, &xs); | 		stat_update(xfpd); | ||||||
| 	else if (xfpd->cb_end) | 	else if (xfpd->cb_end) | ||||||
| 		stat_end(xfpd, &xs); | 		stat_end(xfpd); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -180,6 +180,7 @@ int | |||||||
| main(int argc, char **argv) | main(int argc, char **argv) | ||||||
| { | { | ||||||
| 	struct xbps_handle *xhp; | 	struct xbps_handle *xhp; | ||||||
|  | 	struct xferstat xfer; | ||||||
| 	struct list_pkgver_cb lpc; | 	struct list_pkgver_cb lpc; | ||||||
| 	struct sigaction sa; | 	struct sigaction sa; | ||||||
| 	const char *rootdir, *cachedir, *conffile; | 	const char *rootdir, *cachedir, *conffile; | ||||||
| @@ -277,6 +278,7 @@ main(int argc, char **argv) | |||||||
| 	xhp->xbps_transaction_cb = transaction_cb; | 	xhp->xbps_transaction_cb = transaction_cb; | ||||||
| 	xhp->xbps_transaction_err_cb = transaction_err_cb; | 	xhp->xbps_transaction_err_cb = transaction_err_cb; | ||||||
| 	xhp->xbps_fetch_cb = fetch_file_progress_cb; | 	xhp->xbps_fetch_cb = fetch_file_progress_cb; | ||||||
|  | 	xhp->xfcd->cookie = &xfer; | ||||||
| 	if (flags & XBPS_FLAG_VERBOSE) | 	if (flags & XBPS_FLAG_VERBOSE) | ||||||
| 		xhp->xbps_unpack_cb = unpack_progress_cb_verbose; | 		xhp->xbps_unpack_cb = unpack_progress_cb_verbose; | ||||||
| 	else | 	else | ||||||
|   | |||||||
| @@ -88,6 +88,7 @@ int | |||||||
| main(int argc, char **argv) | main(int argc, char **argv) | ||||||
| { | { | ||||||
| 	struct xbps_handle *xhp; | 	struct xbps_handle *xhp; | ||||||
|  | 	struct xferstat xfer; | ||||||
| 	prop_dictionary_t pkgd; | 	prop_dictionary_t pkgd; | ||||||
| 	const char *rootdir, *cachedir, *conffile; | 	const char *rootdir, *cachedir, *conffile; | ||||||
| 	int c, rv = 0; | 	int c, rv = 0; | ||||||
| @@ -135,6 +136,7 @@ main(int argc, char **argv) | |||||||
| 	} | 	} | ||||||
| 	xhp->debug = debug; | 	xhp->debug = debug; | ||||||
| 	xhp->xbps_fetch_cb = fetch_file_progress_cb; | 	xhp->xbps_fetch_cb = fetch_file_progress_cb; | ||||||
|  | 	xhp->xfcd->cookie = &xfer; | ||||||
| 	xhp->rootdir = rootdir; | 	xhp->rootdir = rootdir; | ||||||
| 	xhp->cachedir = cachedir; | 	xhp->cachedir = cachedir; | ||||||
| 	xhp->conffile = conffile; | 	xhp->conffile = conffile; | ||||||
|   | |||||||
| @@ -110,6 +110,7 @@ int | |||||||
| main(int argc, char **argv) | main(int argc, char **argv) | ||||||
| { | { | ||||||
| 	struct xbps_handle *xhp; | 	struct xbps_handle *xhp; | ||||||
|  | 	struct xferstat xfer; | ||||||
| 	prop_dictionary_t dict; | 	prop_dictionary_t dict; | ||||||
| 	const char *version, *rootdir = NULL, *conffile = NULL; | 	const char *version, *rootdir = NULL, *conffile = NULL; | ||||||
| 	char *plist, *pkgname, *pkgver, *in_chroot_env, *hash; | 	char *plist, *pkgname, *pkgver, *in_chroot_env, *hash; | ||||||
| @@ -153,6 +154,7 @@ main(int argc, char **argv) | |||||||
| 	} | 	} | ||||||
| 	xhp->debug = debug; | 	xhp->debug = debug; | ||||||
| 	xhp->xbps_fetch_cb = fetch_file_progress_cb; | 	xhp->xbps_fetch_cb = fetch_file_progress_cb; | ||||||
|  | 	xhp->xfcd->cookie = &xfer; | ||||||
| 	xhp->rootdir = rootdir; | 	xhp->rootdir = rootdir; | ||||||
| 	xhp->conffile = conffile; | 	xhp->conffile = conffile; | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user