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:
parent
4c4f5eebef
commit
748cc9c6b1
@ -87,11 +87,11 @@ stat_bps(struct xferstat *xsp)
|
|||||||
delta = (xsp->last.tv_sec + (xsp->last.tv_usec / 1.e6))
|
delta = (xsp->last.tv_sec + (xsp->last.tv_usec / 1.e6))
|
||||||
- (xsp->start.tv_sec + (xsp->start.tv_usec / 1.e6));
|
- (xsp->start.tv_sec + (xsp->start.tv_usec / 1.e6));
|
||||||
if (delta == 0.0) {
|
if (delta == 0.0) {
|
||||||
snprintf(str, sizeof str, "stalled");
|
snprintf(str, sizeof str, "-- stalled --");
|
||||||
} else {
|
} else {
|
||||||
bps = ((double)(xsp->rcvd - xsp->offset) / delta);
|
bps = ((double)(xsp->rcvd - xsp->offset) / delta);
|
||||||
(void)xbps_humanize_number(size, 6, (int64_t)bps, "",
|
(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);
|
snprintf(str, sizeof str, "%sB/s", size);
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
@ -111,19 +111,19 @@ stat_display(struct xferstat *xsp)
|
|||||||
return;
|
return;
|
||||||
xsp->last = now;
|
xsp->last = now;
|
||||||
|
|
||||||
printf("Downloading %s ... ", xsp->name);
|
(void)xbps_humanize_number(totsize, 7, (int64_t)xsp->size, "",
|
||||||
(void)xbps_humanize_number(totsize, 8, (int64_t)xsp->size, "",
|
HN_AUTOSCALE, HN_NOSPACE|HN_DECIMAL);
|
||||||
HN_AUTOSCALE, HN_DECIMAL);
|
(void)xbps_humanize_number(recvsize, 7, (int64_t)xsp->rcvd, "",
|
||||||
(void)xbps_humanize_number(recvsize, 8, (int64_t)xsp->rcvd, "",
|
HN_AUTOSCALE, HN_NOSPACE|HN_DECIMAL);
|
||||||
HN_AUTOSCALE, HN_DECIMAL);
|
printf("Downloading %s ...", xsp->name);
|
||||||
printf("%sB [%d%% of %sB]", recvsize,
|
printf("\n\t%sB [%d%% of %sB]", recvsize,
|
||||||
(int)((double)(100.0 * (double)xsp->rcvd) / (double)xsp->size), totsize);
|
(int)((double)(100.0 * (double)xsp->rcvd) / (double)xsp->size),
|
||||||
|
totsize);
|
||||||
printf(" %s", stat_bps(xsp));
|
printf(" %s", stat_bps(xsp));
|
||||||
if (xsp->size > 0 && xsp->rcvd > 0 &&
|
if (xsp->size > 0 && xsp->rcvd > 0 &&
|
||||||
xsp->last.tv_sec >= xsp->start.tv_sec + 10)
|
xsp->last.tv_sec >= xsp->start.tv_sec + 10)
|
||||||
printf(" ETA: %s", stat_eta(xsp));
|
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];
|
char size[32];
|
||||||
|
|
||||||
(void)xbps_humanize_number(size, 8, (int64_t)xsp->size, "",
|
(void)xbps_humanize_number(size, 6, (int64_t)xsp->size, "",
|
||||||
HN_AUTOSCALE, HN_DECIMAL);
|
HN_AUTOSCALE, HN_NOSPACE|HN_DECIMAL);
|
||||||
|
printf("\033[1A\033[K");
|
||||||
printf("Downloaded %s successfully (%sB at %s)\n",
|
printf("Downloaded %s successfully (%sB at %s)\n",
|
||||||
xsp->name, size, stat_bps(xsp));
|
xsp->name, size, stat_bps(xsp));
|
||||||
|
printf("\033[J");
|
||||||
}
|
}
|
||||||
|
|
||||||
const char SYMEXPORT *
|
const char SYMEXPORT *
|
||||||
@ -170,6 +172,19 @@ xbps_fetch_error_string(void)
|
|||||||
return fetchLastErrString;
|
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
|
int SYMEXPORT
|
||||||
xbps_fetch_file(const char *uri, const char *outputdir, const char *flags)
|
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)
|
if (flags != NULL)
|
||||||
strcat(fetchflags, flags);
|
strcat(fetchflags, flags);
|
||||||
strcat(fetchflags, "i");
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* By default we assume that we want always restart a transfer,
|
* Establish connection to remote host.
|
||||||
* will be checked later.
|
|
||||||
*/
|
*/
|
||||||
url->offset = st.st_size;
|
url->offset = st.st_size;
|
||||||
url->last_modified = st.st_mtime;
|
|
||||||
|
|
||||||
fio = fetchXGet(url, &url_st, fetchflags);
|
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 (fio == NULL) {
|
||||||
/*
|
/* Local and remote size match, do nothing */
|
||||||
* If requested offset is the same than remote size,
|
if (url->length == 0)
|
||||||
* and If-Modified-Since is unchanged, we are done.
|
|
||||||
*/
|
|
||||||
if (url->offset == st.st_size &&
|
|
||||||
fetchLastErrCode == FETCH_UNCHANGED)
|
|
||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
rv = fetchLastErrCode;
|
rv = fetchLastErrCode;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (url_st.size != -1) {
|
if (url_st.size == -1) {
|
||||||
if (url_st.size == st.st_size && url_st.mtime == st.st_mtime) {
|
printf("Remote file size is unknown!\n");
|
||||||
/*
|
rv = EINVAL;
|
||||||
* Files are identical.
|
goto out;
|
||||||
*/
|
} else if (st.st_size > url_st.size) {
|
||||||
goto out;
|
printf("Local file %s is greater than remote file!\n",
|
||||||
} else if (st.st_size > url_st.size) {
|
filename);
|
||||||
/*
|
rv = EFBIG;
|
||||||
* Local file bigger, error out.
|
goto out;
|
||||||
*/
|
} else if (st.st_size == url_st.size && st.st_mtime == url_st.mtime) {
|
||||||
rv = EFBIG;
|
/* Local and remote size/mtime match, do nothing. */
|
||||||
goto out;
|
rv = 0;
|
||||||
}
|
goto out;
|
||||||
}
|
}
|
||||||
printf("Connected to %s.\n", url->host);
|
printf("Connected to %s.\n", url->host);
|
||||||
|
|
||||||
|
@ -12,10 +12,6 @@ ifdef WITH_SSL
|
|||||||
CPPFLAGS += -DWITH_SSL
|
CPPFLAGS += -DWITH_SSL
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifdef WITH_DEBUG
|
|
||||||
CPPFLAGS += -DDEBUG
|
|
||||||
endif
|
|
||||||
|
|
||||||
ifdef STATIC
|
ifdef STATIC
|
||||||
CFLAGS += -static
|
CFLAGS += -static
|
||||||
endif
|
endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user