Improve xbps_fetch_file() and friends, one more time by Daft Punk.

--HG--
extra : convert_revision : xtraeme%40gmail.com-20091031140925-rjlkhq0v6fvmtawo
This commit is contained in:
Juan RP 2009-10-31 15:09:25 +01:00
parent 4c4f5eebef
commit 748cc9c6b1
3 changed files with 62 additions and 41 deletions

View File

@ -87,11 +87,11 @@ stat_bps(struct xferstat *xsp)
delta = (xsp->last.tv_sec + (xsp->last.tv_usec / 1.e6))
- (xsp->start.tv_sec + (xsp->start.tv_usec / 1.e6));
if (delta == 0.0) {
snprintf(str, sizeof str, "stalled");
snprintf(str, sizeof str, "-- stalled --");
} else {
bps = ((double)(xsp->rcvd - xsp->offset) / delta);
(void)xbps_humanize_number(size, 6, (int64_t)bps, "",
HN_AUTOSCALE, HN_DECIMAL);
HN_AUTOSCALE, HN_NOSPACE|HN_DECIMAL);
snprintf(str, sizeof str, "%sB/s", size);
}
return str;
@ -111,19 +111,19 @@ stat_display(struct xferstat *xsp)
return;
xsp->last = now;
(void)xbps_humanize_number(totsize, 7, (int64_t)xsp->size, "",
HN_AUTOSCALE, HN_NOSPACE|HN_DECIMAL);
(void)xbps_humanize_number(recvsize, 7, (int64_t)xsp->rcvd, "",
HN_AUTOSCALE, HN_NOSPACE|HN_DECIMAL);
printf("Downloading %s ...", xsp->name);
(void)xbps_humanize_number(totsize, 8, (int64_t)xsp->size, "",
HN_AUTOSCALE, HN_DECIMAL);
(void)xbps_humanize_number(recvsize, 8, (int64_t)xsp->rcvd, "",
HN_AUTOSCALE, HN_DECIMAL);
printf("%sB [%d%% of %sB]", recvsize,
(int)((double)(100.0 * (double)xsp->rcvd) / (double)xsp->size), totsize);
printf("\n\t%sB [%d%% of %sB]", recvsize,
(int)((double)(100.0 * (double)xsp->rcvd) / (double)xsp->size),
totsize);
printf(" %s", stat_bps(xsp));
if (xsp->size > 0 && xsp->rcvd > 0 &&
xsp->last.tv_sec >= xsp->start.tv_sec + 10)
printf(" ETA: %s", stat_eta(xsp));
printf("\n\033[1A\033[K");
printf("\n\033[2A\033[K");
}
/*
@ -158,10 +158,12 @@ stat_end(struct xferstat *xsp)
{
char size[32];
(void)xbps_humanize_number(size, 8, (int64_t)xsp->size, "",
HN_AUTOSCALE, HN_DECIMAL);
(void)xbps_humanize_number(size, 6, (int64_t)xsp->size, "",
HN_AUTOSCALE, HN_NOSPACE|HN_DECIMAL);
printf("\033[1A\033[K");
printf("Downloaded %s successfully (%sB at %s)\n",
xsp->name, size, stat_bps(xsp));
printf("\033[J");
}
const char SYMEXPORT *
@ -170,6 +172,19 @@ xbps_fetch_error_string(void)
return fetchLastErrString;
}
#ifdef DEBUG
static const char *
print_time(time_t *t)
{
struct tm tm;
static char buf[255];
localtime_r(t, &tm);
strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm);
return buf;
}
#endif
int SYMEXPORT
xbps_fetch_file(const char *uri, const char *outputdir, const char *flags)
{
@ -231,42 +246,51 @@ xbps_fetch_file(const char *uri, const char *outputdir, const char *flags)
*/
if (flags != NULL)
strcat(fetchflags, flags);
strcat(fetchflags, "i");
/*
* By default we assume that we want always restart a transfer,
* will be checked later.
* Establish connection to remote host.
*/
url->offset = st.st_size;
url->last_modified = st.st_mtime;
fio = fetchXGet(url, &url_st, fetchflags);
#ifdef DEBUG
printf("st.st_size: %zd\n", (ssize_t)st.st_size);
printf("st.st_atime: %s\n", print_time(&st.st_atime));
printf("st.st_mtime: %s\n", print_time(&st.st_mtime));
printf("url->scheme: %s\n", url->scheme);
printf("url->host: %s\n", url->host);
printf("url->port: %d\n", url->port);
printf("url->doc: %s\n", url->doc);
printf("url->offset: %zd\n", (ssize_t)url->offset);
printf("url->length: %zu\n", url->length);
printf("url->last_modified: %s\n", print_time(&url->last_modified));
printf("url_stat.size: %zd\n", (ssize_t)url_st.size);
printf("url_stat.atime: %s\n", print_time(&url_st.atime));
printf("url_stat.mtime: %s\n", print_time(&url_st.mtime));
#endif
if (fio == NULL) {
/*
* If requested offset is the same than remote size,
* and If-Modified-Since is unchanged, we are done.
*/
if (url->offset == st.st_size &&
fetchLastErrCode == FETCH_UNCHANGED)
/* Local and remote size match, do nothing */
if (url->length == 0)
goto out;
rv = fetchLastErrCode;
goto out;
}
if (url_st.size != -1) {
if (url_st.size == st.st_size && url_st.mtime == st.st_mtime) {
/*
* Files are identical.
*/
if (url_st.size == -1) {
printf("Remote file size is unknown!\n");
rv = EINVAL;
goto out;
} else if (st.st_size > url_st.size) {
/*
* Local file bigger, error out.
*/
printf("Local file %s is greater than remote file!\n",
filename);
rv = EFBIG;
goto out;
}
} else if (st.st_size == url_st.size && st.st_mtime == url_st.mtime) {
/* Local and remote size/mtime match, do nothing. */
rv = 0;
goto out;
}
printf("Connected to %s.\n", url->host);

View File

@ -12,10 +12,6 @@ ifdef WITH_SSL
CPPFLAGS += -DWITH_SSL
endif
ifdef WITH_DEBUG
CPPFLAGS += -DDEBUG
endif
ifdef STATIC
CFLAGS += -static
endif

View File

@ -13,6 +13,7 @@ INSTALL_STRIPPED ?= -s
ifdef DEBUG
INSTALL_STRIPPED=
DEBUG_FLAGS = -g
CPPFLAGS += -DDEBUG
endif
LDFLAGS = -L$(TOPDIR)/lib