wget: add (configurable) large file support. Run tested.

This commit is contained in:
Denis Vlasenko 2006-09-26 09:22:12 +00:00
parent 3312c989e6
commit a552eeb498
3 changed files with 99 additions and 53 deletions

View File

@ -248,7 +248,9 @@ extern char *safe_strncpy(char *dst, const char *src, size_t size);
extern int safe_strtoi(char *arg, int* value); extern int safe_strtoi(char *arg, int* value);
extern int safe_strtod(char *arg, double* value); extern int safe_strtod(char *arg, double* value);
extern int safe_strtol(char *arg, long* value); extern int safe_strtol(char *arg, long* value);
extern int safe_strtoll(char *arg, long long* value);
extern int safe_strtoul(char *arg, unsigned long* value); extern int safe_strtoul(char *arg, unsigned long* value);
extern int safe_strtoull(char *arg, unsigned long long* value);
struct suffix_mult { struct suffix_mult {
const char *suffix; const char *suffix;

View File

@ -74,3 +74,37 @@ int safe_strtoul(char *arg, unsigned long* value)
} }
#endif #endif
#ifdef L_safe_strtoll
int safe_strtoll(char *arg, long long* value)
{
char *endptr;
int errno_save = errno;
assert(arg!=NULL);
errno = 0;
*value = strtoll(arg, &endptr, 0);
if (errno != 0 || *endptr!='\0' || endptr==arg) {
return 1;
}
errno = errno_save;
return 0;
}
#endif
#ifdef L_safe_strtoull
int safe_strtoull(char *arg, unsigned long long* value)
{
char *endptr;
int errno_save = errno;
assert(arg!=NULL);
errno = 0;
*value = strtoull(arg, &endptr, 0);
if (errno != 0 || *endptr!='\0' || endptr==arg) {
return 1;
}
errno = errno_save;
return 0;
}
#endif

View File

@ -6,8 +6,28 @@
* *
*/ */
/* We want libc to give us xxx64 functions also */
/* http://www.unix.org/version2/whatsnew/lfs20mar.html */
#define _LARGEFILE64_SOURCE 1
#include "busybox.h" #include "busybox.h"
#include <getopt.h> #include <getopt.h> /* for struct option */
#ifdef CONFIG_LFS
# define FILEOFF_TYPE off64_t
# define FILEOFF_FMT "%lld"
# define LSEEK lseek64
# define STRTOOFF strtoll
# define SAFE_STRTOOFF safe_strtoll
/* stat64 etc as needed... */
#else
# define FILEOFF_TYPE off_t
# define FILEOFF_FMT "%ld"
# define LSEEK lseek
# define STRTOOFF strtol
# define SAFE_STRTOOFF safe_strtol
/* Do we need to undefine O_LARGEFILE? */
#endif
struct host_info { struct host_info {
char *host; char *host;
@ -23,13 +43,13 @@ static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc);
static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf); static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf);
/* Globals (can be accessed from signal handlers */ /* Globals (can be accessed from signal handlers */
static off_t filesize; /* content-length of the file */ static FILEOFF_TYPE filesize; /* content-length of the file */
static int chunked; /* chunked transfer encoding */ static int chunked; /* chunked transfer encoding */
#ifdef CONFIG_FEATURE_WGET_STATUSBAR #ifdef CONFIG_FEATURE_WGET_STATUSBAR
static void progressmeter(int flag); static void progressmeter(int flag);
static char *curfile; /* Name of current file being transferred. */ static char *curfile; /* Name of current file being transferred. */
static struct timeval start; /* Time a transfer started. */ static struct timeval start; /* Time a transfer started. */
static off_t transferred; /* Number of bytes transferred so far. */ static FILEOFF_TYPE transferred; /* Number of bytes transferred so far. */
/* For progressmeter() -- number of seconds before xfer considered "stalled" */ /* For progressmeter() -- number of seconds before xfer considered "stalled" */
enum { enum {
STALLTIME = 5 STALLTIME = 5
@ -91,7 +111,7 @@ static char *base64enc(unsigned char *p, char *buf, int len)
static const struct option wget_long_options[] = { static const struct option wget_long_options[] = {
{ "continue", 0, NULL, 'c' }, { "continue", 0, NULL, 'c' },
{ "quiet", 0, NULL, 'q' }, { "quiet", 0, NULL, 'q' },
{ "passive-ftp", 0, NULL, 139 }, { "passive-ftp", 0, NULL, 139 }, /* FIXME: what is this - 139?? */
{ "output-document", 1, NULL, 'O' }, { "output-document", 1, NULL, 'O' },
{ "header", 1, NULL, 131 }, { "header", 1, NULL, 131 },
{ "directory-prefix",1, NULL, 'P' }, { "directory-prefix",1, NULL, 'P' },
@ -119,11 +139,9 @@ int wget_main(int argc, char **argv)
FILE *sfp = NULL; /* socket to web/ftp server */ FILE *sfp = NULL; /* socket to web/ftp server */
FILE *dfp = NULL; /* socket to ftp server (data) */ FILE *dfp = NULL; /* socket to ftp server (data) */
char *fname_out = NULL; /* where to direct output (-O) */ char *fname_out = NULL; /* where to direct output (-O) */
int do_continue = 0; /* continue a prev transfer (-c) */ FILEOFF_TYPE beg_range = 0; /* range at which continue begins */
off64_t beg_range = 0; /* range at which continue begins */
int got_clen = 0; /* got content-length: from server */ int got_clen = 0; /* got content-length: from server */
int output_fd = -1; int output_fd = -1;
int quiet_flag = FALSE; /* Be verry, verry quiet... */
int use_proxy = 1; /* Use proxies if env vars are set */ int use_proxy = 1; /* Use proxies if env vars are set */
char *proxy_flag = "on"; /* Use proxies if env vars are set */ char *proxy_flag = "on"; /* Use proxies if env vars are set */
char *user_agent = "Wget"; /* Content of the "User-Agent" header field */ char *user_agent = "Wget"; /* Content of the "User-Agent" header field */
@ -138,12 +156,6 @@ int wget_main(int argc, char **argv)
opt = bb_getopt_ulflags(argc, argv, "cq\213O:\203:P:Y:U:", opt = bb_getopt_ulflags(argc, argv, "cq\213O:\203:P:Y:U:",
&fname_out, &headers_llist, &fname_out, &headers_llist,
&dir_prefix, &proxy_flag, &user_agent); &dir_prefix, &proxy_flag, &user_agent);
if (opt & WGET_OPT_CONTINUE) {
++do_continue;
}
if (opt & WGET_OPT_QUIET) {
quiet_flag = TRUE;
}
if (strcmp(proxy_flag, "off") == 0) { if (strcmp(proxy_flag, "off") == 0) {
/* Use the proxy if necessary. */ /* Use the proxy if necessary. */
use_proxy = 0; use_proxy = 0;
@ -205,7 +217,7 @@ int wget_main(int argc, char **argv)
curfile = bb_get_last_path_component(fname_out); curfile = bb_get_last_path_component(fname_out);
#endif #endif
} }
if (do_continue && !fname_out) if ((opt & WGET_OPT_CONTINUE) && !fname_out)
bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)");
/* /*
@ -213,13 +225,13 @@ int wget_main(int argc, char **argv)
*/ */
if (!strcmp(fname_out, "-")) { if (!strcmp(fname_out, "-")) {
output_fd = 1; output_fd = 1;
quiet_flag = TRUE; opt |= WGET_OPT_QUIET;
do_continue = 0; opt &= ~WGET_OPT_CONTINUE;
} else if (do_continue) { } else if (opt & WGET_OPT_CONTINUE) {
output_fd = open(fname_out, O_WRONLY|O_LARGEFILE); output_fd = open(fname_out, O_WRONLY|O_LARGEFILE);
if (output_fd >= 0) { if (output_fd >= 0) {
beg_range = lseek64(output_fd, 0, SEEK_END); beg_range = LSEEK(output_fd, 0, SEEK_END);
if (beg_range == (off64_t)-1) if (beg_range == (FILEOFF_TYPE)-1)
bb_perror_msg_and_die("lseek64"); bb_perror_msg_and_die("lseek64");
} }
/* File doesn't exist. We do not create file here yet. /* File doesn't exist. We do not create file here yet.
@ -231,7 +243,7 @@ int wget_main(int argc, char **argv)
* and we want to connect to only one IP... */ * and we want to connect to only one IP... */
bb_lookup_host(&s_in, server.host); bb_lookup_host(&s_in, server.host);
s_in.sin_port = server.port; s_in.sin_port = server.port;
if (quiet_flag == FALSE) { if (!(opt & WGET_OPT_QUIET)) {
printf("Connecting to %s[%s]:%d\n", printf("Connecting to %s[%s]:%d\n",
server.host, inet_ntoa(s_in.sin_addr), ntohs(server.port)); server.host, inet_ntoa(s_in.sin_addr), ntohs(server.port));
} }
@ -283,7 +295,7 @@ int wget_main(int argc, char **argv)
#endif #endif
if (beg_range) if (beg_range)
fprintf(sfp, "Range: bytes=%lld-\r\n", (long long)beg_range); fprintf(sfp, "Range: bytes="FILEOFF_FMT"-\r\n", beg_range);
if(extra_headers_left < sizeof(extra_headers)) if(extra_headers_left < sizeof(extra_headers))
fputs(extra_headers,sfp); fputs(extra_headers,sfp);
fprintf(sfp,"Connection: close\r\n\r\n"); fprintf(sfp,"Connection: close\r\n\r\n");
@ -325,11 +337,9 @@ read_response:
*/ */
while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) { while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
if (strcasecmp(buf, "content-length") == 0) { if (strcasecmp(buf, "content-length") == 0) {
unsigned long value; if (SAFE_STRTOOFF(s, &filesize) || filesize < 0) {
if (safe_strtoul(s, &value)) {
bb_error_msg_and_die("content-length %s is garbage", s); bb_error_msg_and_die("content-length %s is garbage", s);
} }
filesize = value;
got_clen = 1; got_clen = 1;
continue; continue;
} }
@ -395,11 +405,9 @@ read_response:
* Querying file size * Querying file size
*/ */
if (ftpcmd("SIZE ", target.path, sfp, buf) == 213) { if (ftpcmd("SIZE ", target.path, sfp, buf) == 213) {
unsigned long value; if (SAFE_STRTOOFF(buf+4, &filesize) || filesize < 0) {
if (safe_strtoul(buf+4, &value)) {
bb_error_msg_and_die("SIZE value is garbage"); bb_error_msg_and_die("SIZE value is garbage");
} }
filesize = value;
got_clen = 1; got_clen = 1;
} }
@ -417,7 +425,7 @@ read_response:
dfp = open_socket(&s_in); dfp = open_socket(&s_in);
if (beg_range) { if (beg_range) {
sprintf(buf, "REST %lld", (long long)beg_range); sprintf(buf, "REST "FILEOFF_FMT, beg_range);
if (ftpcmd(buf, NULL, sfp, buf) == 350) if (ftpcmd(buf, NULL, sfp, buf) == 350)
filesize -= beg_range; filesize -= beg_range;
} }
@ -432,17 +440,18 @@ read_response:
*/ */
if (chunked) { if (chunked) {
fgets(buf, sizeof(buf), dfp); fgets(buf, sizeof(buf), dfp);
filesize = strtol(buf, (char **) NULL, 16); filesize = STRTOOFF(buf, (char **) NULL, 16);
/* FIXME: error check?? */
} }
if (quiet_flag == FALSE) if (!(opt & WGET_OPT_QUIET))
progressmeter(-1); progressmeter(-1);
do { do {
while (filesize > 0 || !got_clen) { while (filesize > 0 || !got_clen) {
unsigned rdsz = sizeof(buf); unsigned rdsz = sizeof(buf);
if (filesize < sizeof(buf) && (chunked || got_clen)) if (filesize < sizeof(buf) && (chunked || got_clen))
rdsz = filesize; rdsz = (unsigned)filesize;
n = safe_fread(buf, 1, rdsz, dfp); n = safe_fread(buf, 1, rdsz, dfp);
if (n <= 0) if (n <= 0)
break; break;
@ -462,7 +471,8 @@ read_response:
if (chunked) { if (chunked) {
safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */ safe_fgets(buf, sizeof(buf), dfp); /* This is a newline */
safe_fgets(buf, sizeof(buf), dfp); safe_fgets(buf, sizeof(buf), dfp);
filesize = strtol(buf, (char **) NULL, 16); filesize = STRTOOFF(buf, (char **) NULL, 16);
/* FIXME: error check? */
if (filesize == 0) { if (filesize == 0) {
chunked = 0; /* all done! */ chunked = 0; /* all done! */
} }
@ -473,7 +483,7 @@ read_response:
} }
} while (chunked); } while (chunked);
if (quiet_flag == FALSE) if (!(opt & WGET_OPT_QUIET))
progressmeter(1); progressmeter(1);
if ((use_proxy == 0) && target.is_ftp) { if ((use_proxy == 0) && target.is_ftp) {
@ -662,10 +672,10 @@ static void
progressmeter(int flag) progressmeter(int flag)
{ {
static struct timeval lastupdate; static struct timeval lastupdate;
static off_t lastsize, totalsize; static FILEOFF_TYPE lastsize, totalsize;
struct timeval now, td, tvwait; struct timeval now, td, tvwait;
off_t abbrevsize; FILEOFF_TYPE abbrevsize;
int elapsed, ratio, barlength, i; int elapsed, ratio, barlength, i;
char buf[256]; char buf[256];