2009-10-27 06:16:00 +05:30
|
|
|
/*-
|
|
|
|
* Copyright (c) 2009 Juan Romero Pardines
|
|
|
|
* Copyright (c) 2000-2004 Dag-Erling Coïdan Smørgrav
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer
|
|
|
|
* in this position and unchanged.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* From FreeBSD fetch(8):
|
|
|
|
* $FreeBSD: src/usr.bin/fetch/fetch.c,v 1.84.2.1 2009/08/03 08:13:06 kensmith Exp $
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <xbps_api.h>
|
|
|
|
#include "fetch.h"
|
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
/**
|
|
|
|
* @file lib/download.c
|
|
|
|
* @brief Download routines
|
|
|
|
* @defgroup download Internal download functions
|
|
|
|
*
|
|
|
|
* These functions allow you to download files.
|
|
|
|
*/
|
2009-10-27 06:16:00 +05:30
|
|
|
struct xferstat {
|
|
|
|
struct timeval start;
|
|
|
|
struct timeval last;
|
|
|
|
off_t size;
|
|
|
|
off_t offset;
|
|
|
|
off_t rcvd;
|
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute and display ETA
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
stat_eta(struct xferstat *xsp)
|
|
|
|
{
|
|
|
|
static char str[16];
|
|
|
|
long elapsed, eta;
|
|
|
|
off_t received, expected;
|
|
|
|
|
|
|
|
elapsed = xsp->last.tv_sec - xsp->start.tv_sec;
|
|
|
|
received = xsp->rcvd - xsp->offset;
|
|
|
|
expected = xsp->size - xsp->rcvd;
|
|
|
|
eta = (long)(elapsed * expected / received);
|
|
|
|
if (eta > 3600)
|
|
|
|
snprintf(str, sizeof str, "%02ldh%02ldm",
|
|
|
|
eta / 3600, (eta % 3600) / 60);
|
|
|
|
else
|
|
|
|
snprintf(str, sizeof str, "%02ldm%02lds",
|
|
|
|
eta / 60, eta % 60);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute and display transfer rate
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
stat_bps(struct xferstat *xsp)
|
|
|
|
{
|
|
|
|
static char str[16];
|
|
|
|
char size[32];
|
|
|
|
double delta, bps;
|
|
|
|
|
|
|
|
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) {
|
2009-10-31 19:39:25 +05:30
|
|
|
snprintf(str, sizeof str, "-- stalled --");
|
2009-10-27 06:16:00 +05:30
|
|
|
} else {
|
|
|
|
bps = ((double)(xsp->rcvd - xsp->offset) / delta);
|
|
|
|
(void)xbps_humanize_number(size, 6, (int64_t)bps, "",
|
2009-10-31 19:39:25 +05:30
|
|
|
HN_AUTOSCALE, HN_NOSPACE|HN_DECIMAL);
|
2009-10-27 06:16:00 +05:30
|
|
|
snprintf(str, sizeof str, "%sB/s", size);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the stats display
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
stat_display(struct xferstat *xsp)
|
|
|
|
{
|
|
|
|
struct timeval now;
|
|
|
|
char totsize[32], recvsize[32];
|
|
|
|
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
if (now.tv_sec <= xsp->last.tv_sec)
|
|
|
|
return;
|
|
|
|
xsp->last = now;
|
|
|
|
|
2009-10-31 19:39:25 +05:30
|
|
|
(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);
|
2009-11-18 11:58:14 +05:30
|
|
|
fprintf(stderr, "\r%s: %sB [%d%% of %sB]",
|
|
|
|
xsp->name, recvsize,
|
2009-10-31 19:39:25 +05:30
|
|
|
(int)((double)(100.0 * (double)xsp->rcvd) / (double)xsp->size),
|
|
|
|
totsize);
|
2009-11-18 11:58:14 +05:30
|
|
|
fprintf(stderr, " %s", stat_bps(xsp));
|
2009-10-27 06:16:00 +05:30
|
|
|
if (xsp->size > 0 && xsp->rcvd > 0 &&
|
|
|
|
xsp->last.tv_sec >= xsp->start.tv_sec + 10)
|
2009-11-18 11:58:14 +05:30
|
|
|
fprintf(stderr, " ETA: %s", stat_eta(xsp));
|
2009-10-27 06:16:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the transfer statistics
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
stat_start(struct xferstat *xsp, const char *name, off_t *size, off_t *offset)
|
|
|
|
{
|
|
|
|
gettimeofday(&xsp->start, NULL);
|
|
|
|
xsp->last.tv_sec = xsp->last.tv_usec = 0;
|
|
|
|
xsp->name = name;
|
|
|
|
xsp->size = *size;
|
|
|
|
xsp->offset = *offset;
|
|
|
|
xsp->rcvd = *offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the transfer statistics
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
stat_update(struct xferstat *xsp, off_t rcvd)
|
|
|
|
{
|
|
|
|
xsp->rcvd = rcvd + xsp->offset;
|
|
|
|
stat_display(xsp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finalize the transfer statistics
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
stat_end(struct xferstat *xsp)
|
|
|
|
{
|
|
|
|
char size[32];
|
|
|
|
|
2009-10-31 19:39:25 +05:30
|
|
|
(void)xbps_humanize_number(size, 6, (int64_t)xsp->size, "",
|
|
|
|
HN_AUTOSCALE, HN_NOSPACE|HN_DECIMAL);
|
2009-11-18 11:58:14 +05:30
|
|
|
fprintf(stderr, "\rDownloaded %sB for %s [avg rate: %s]",
|
|
|
|
size, xsp->name, stat_bps(xsp));
|
|
|
|
fprintf(stderr, "\033[K\n");
|
2009-10-27 06:16:00 +05:30
|
|
|
}
|
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
const char *
|
2009-10-30 07:43:03 +05:30
|
|
|
xbps_fetch_error_string(void)
|
|
|
|
{
|
|
|
|
return fetchLastErrString;
|
|
|
|
}
|
|
|
|
|
2009-10-31 19:39:25 +05:30
|
|
|
#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
|
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
int
|
2009-11-18 11:58:14 +05:30
|
|
|
xbps_fetch_file(const char *uri, const char *outputdir, bool refetch,
|
|
|
|
const char *flags)
|
2009-10-27 06:16:00 +05:30
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
struct xferstat xs;
|
|
|
|
struct url *url = NULL;
|
|
|
|
struct url_stat url_st;
|
|
|
|
struct fetchIO *fio = NULL;
|
|
|
|
struct timeval tv[2];
|
|
|
|
ssize_t bytes_read, bytes_written;
|
|
|
|
off_t bytes_dld = -1;
|
2009-11-18 11:58:14 +05:30
|
|
|
char buf[4096], *filename, *destfile;
|
2009-10-27 06:16:00 +05:30
|
|
|
int fd = -1, rv = 0;
|
|
|
|
bool restart = false;
|
|
|
|
|
2009-11-18 11:58:14 +05:30
|
|
|
filename = destfile = NULL;
|
2009-10-27 06:16:00 +05:30
|
|
|
bytes_read = bytes_written = -1;
|
2009-10-30 21:50:44 +05:30
|
|
|
fetchLastErrCode = 0;
|
2009-10-27 06:16:00 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the filename specified in URI argument.
|
|
|
|
*/
|
|
|
|
filename = strrchr(uri, '/');
|
2009-11-24 10:33:26 +05:30
|
|
|
if (filename == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-10-27 06:16:00 +05:30
|
|
|
filename++;
|
|
|
|
/*
|
|
|
|
* Compute destination file path.
|
|
|
|
*/
|
|
|
|
destfile = xbps_xasprintf("%s/%s", outputdir, filename);
|
|
|
|
if (destfile == NULL) {
|
2009-11-24 10:33:26 +05:30
|
|
|
rv = -1;
|
2009-10-27 06:16:00 +05:30
|
|
|
goto out;
|
|
|
|
}
|
2009-10-30 21:50:44 +05:30
|
|
|
/*
|
|
|
|
* Check if we have to resume a transfer.
|
|
|
|
*/
|
|
|
|
memset(&st, 0, sizeof(st));
|
2009-11-18 11:58:14 +05:30
|
|
|
if (stat(destfile, &st) == 0) {
|
|
|
|
if (st.st_size > 0)
|
|
|
|
restart = true;
|
|
|
|
} else {
|
2009-10-27 06:16:00 +05:30
|
|
|
if (errno != ENOENT) {
|
2009-11-24 10:33:26 +05:30
|
|
|
rv = -1;
|
2009-10-27 06:16:00 +05:30
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Prepare stuff for libfetch.
|
|
|
|
*/
|
|
|
|
if ((url = fetchParseURL(uri)) == NULL) {
|
2009-11-24 10:33:26 +05:30
|
|
|
rv = -1;
|
2009-10-27 06:16:00 +05:30
|
|
|
goto out;
|
|
|
|
|
|
|
|
}
|
|
|
|
/*
|
2009-11-18 11:58:14 +05:30
|
|
|
* Check if we want to refetch from scratch a file.
|
2009-10-27 06:16:00 +05:30
|
|
|
*/
|
2009-11-18 11:58:14 +05:30
|
|
|
if (refetch) {
|
|
|
|
/*
|
|
|
|
* Issue a HEAD request to know size and mtime.
|
|
|
|
*/
|
2009-11-24 10:33:26 +05:30
|
|
|
if ((rv = fetchStat(url, &url_st, NULL)) == -1)
|
2009-11-18 11:58:14 +05:30
|
|
|
goto out;
|
2009-11-24 10:33:26 +05:30
|
|
|
|
2009-11-18 11:58:14 +05:30
|
|
|
/*
|
|
|
|
* If mtime and size match do nothing.
|
|
|
|
*/
|
|
|
|
if (restart && url_st.size && url_st.mtime &&
|
|
|
|
url_st.size == st.st_size &&
|
|
|
|
url_st.mtime == st.st_mtime)
|
|
|
|
goto out;
|
|
|
|
|
2009-11-24 09:24:40 +05:30
|
|
|
/*
|
|
|
|
* If size match do nothing.
|
|
|
|
*/
|
|
|
|
if (restart && url_st.size && url_st.size == st.st_size)
|
|
|
|
goto out;
|
|
|
|
|
2009-11-18 11:58:14 +05:30
|
|
|
/*
|
|
|
|
* Remove current file (if exists).
|
|
|
|
*/
|
|
|
|
if (restart && remove(destfile) == -1) {
|
2009-11-24 10:33:26 +05:30
|
|
|
rv = -1;
|
2009-11-18 11:58:14 +05:30
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
restart = false;
|
|
|
|
url->offset = 0;
|
|
|
|
/*
|
|
|
|
* Issue the GET request to refetch.
|
|
|
|
*/
|
|
|
|
fio = fetchGet(url, flags);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Issue a GET and skip the HEAD request, some servers
|
|
|
|
* (googlecode.com) return a 404 in HEAD requests!
|
|
|
|
*/
|
|
|
|
url->offset = st.st_size;
|
|
|
|
fio = fetchXGet(url, &url_st, flags);
|
|
|
|
}
|
2009-10-31 19:39:25 +05:30
|
|
|
#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
|
2009-11-18 11:58:14 +05:30
|
|
|
if (fio == NULL && fetchLastErrCode != FETCH_OK) {
|
2009-11-22 11:34:46 +05:30
|
|
|
if (!refetch && restart && fetchLastErrCode == FETCH_UNAVAIL) {
|
2009-11-18 11:58:14 +05:30
|
|
|
/*
|
|
|
|
* In HTTP when 416 is returned and length==0
|
|
|
|
* means that local and remote file size match.
|
|
|
|
* Because we are requesting offset==st_size! grr,
|
|
|
|
* stupid http servers...
|
|
|
|
*/
|
|
|
|
if (url->length == 0)
|
|
|
|
goto out;
|
2009-11-07 10:51:01 +05:30
|
|
|
}
|
2009-11-24 10:33:26 +05:30
|
|
|
rv = -1;
|
2009-10-27 06:16:00 +05:30
|
|
|
goto out;
|
|
|
|
}
|
2009-10-31 19:39:25 +05:30
|
|
|
if (url_st.size == -1) {
|
|
|
|
printf("Remote file size is unknown!\n");
|
2009-11-24 10:33:26 +05:30
|
|
|
errno = EINVAL;
|
|
|
|
rv = -1;
|
2009-10-31 19:39:25 +05:30
|
|
|
goto out;
|
|
|
|
} else if (st.st_size > url_st.size) {
|
|
|
|
printf("Local file %s is greater than remote file!\n",
|
|
|
|
filename);
|
2009-11-24 10:33:26 +05:30
|
|
|
errno = EFBIG;
|
|
|
|
rv = -1;
|
2009-10-31 19:39:25 +05:30
|
|
|
goto out;
|
2009-11-18 11:58:14 +05:30
|
|
|
} else if (restart && url_st.mtime && url_st.size &&
|
|
|
|
url_st.size == st.st_size && url_st.mtime == st.st_mtime) {
|
2009-10-31 19:39:25 +05:30
|
|
|
/* Local and remote size/mtime match, do nothing. */
|
|
|
|
goto out;
|
2009-10-30 21:50:44 +05:30
|
|
|
}
|
2009-11-18 11:58:14 +05:30
|
|
|
fprintf(stderr, "Connected to %s.\n", url->host);
|
2009-10-27 06:16:00 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* If restarting, open the file for appending otherwise create it.
|
|
|
|
*/
|
|
|
|
if (restart)
|
|
|
|
fd = open(destfile, O_WRONLY|O_APPEND);
|
|
|
|
else
|
2009-11-18 11:58:14 +05:30
|
|
|
fd = open(destfile, O_WRONLY|O_CREAT|O_TRUNC, 0644);
|
2009-10-27 06:16:00 +05:30
|
|
|
|
|
|
|
if (fd == -1) {
|
2009-11-24 10:33:26 +05:30
|
|
|
rv = -1;
|
2009-10-27 06:16:00 +05:30
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start fetching requested file.
|
|
|
|
*/
|
2009-11-18 11:58:14 +05:30
|
|
|
stat_start(&xs, filename, &url_st.size, &url->offset);
|
2009-10-27 06:16:00 +05:30
|
|
|
while ((bytes_read = fetchIO_read(fio, buf, sizeof(buf))) > 0) {
|
|
|
|
bytes_written = write(fd, buf, (size_t)bytes_read);
|
|
|
|
if (bytes_written != bytes_read) {
|
2009-11-18 11:58:14 +05:30
|
|
|
fprintf(stderr, "Couldn't write to %s!\n", destfile);
|
2009-11-24 10:33:26 +05:30
|
|
|
rv = -1;
|
2009-10-27 06:16:00 +05:30
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
bytes_dld += bytes_read;
|
2009-11-18 11:58:14 +05:30
|
|
|
stat_update(&xs, bytes_dld);
|
2009-10-27 06:16:00 +05:30
|
|
|
}
|
2009-10-31 16:10:55 +05:30
|
|
|
if (bytes_read == -1) {
|
2009-11-18 11:58:14 +05:30
|
|
|
fprintf(stderr, "IO error while fetching %s: %s\n", filename,
|
2009-10-31 16:10:55 +05:30
|
|
|
fetchLastErrString);
|
2009-11-24 10:33:26 +05:30
|
|
|
errno = EIO;
|
|
|
|
rv = -1;
|
2009-10-27 06:16:00 +05:30
|
|
|
goto out;
|
|
|
|
}
|
2009-11-18 11:58:14 +05:30
|
|
|
stat_end(&xs);
|
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
goto out;
|
2009-10-27 06:16:00 +05:30
|
|
|
|
|
|
|
/*
|
2009-11-18 11:58:14 +05:30
|
|
|
* Update mtime in local file to match remote file if transfer
|
|
|
|
* was successful.
|
2009-10-27 06:16:00 +05:30
|
|
|
*/
|
|
|
|
tv[0].tv_sec = url_st.atime ? url_st.atime : url_st.mtime;
|
|
|
|
tv[1].tv_sec = url_st.mtime;
|
|
|
|
tv[0].tv_usec = tv[1].tv_usec = 0;
|
2009-11-18 11:58:14 +05:30
|
|
|
if (utimes(destfile, tv) == -1)
|
2009-11-24 10:33:26 +05:30
|
|
|
rv = -1;
|
|
|
|
else {
|
|
|
|
/* File downloaded successfully */
|
|
|
|
rv = 1;
|
|
|
|
}
|
2009-10-27 06:16:00 +05:30
|
|
|
|
|
|
|
out:
|
|
|
|
if (fd != -1)
|
|
|
|
(void)close(fd);
|
2009-11-07 10:51:01 +05:30
|
|
|
if (fio != NULL)
|
2009-10-27 06:16:00 +05:30
|
|
|
fetchIO_close(fio);
|
2009-11-07 10:51:01 +05:30
|
|
|
if (url != NULL)
|
2009-10-27 06:16:00 +05:30
|
|
|
fetchFreeURL(url);
|
2009-11-07 10:51:01 +05:30
|
|
|
if (destfile != NULL)
|
2009-10-27 06:16:00 +05:30
|
|
|
free(destfile);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|