diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2006-09-23 17:49:09 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2006-09-23 17:49:09 +0000 |
commit | a94554d010844e17ecf674fe738679f09cbc6c21 (patch) | |
tree | f84d358b4a6f174f8ee891203d9da39f0eea50b4 | |
parent | 01a74f96492e9e9995b7fcb54013d00a840a3d5f (diff) | |
download | busybox-w32-a94554d010844e17ecf674fe738679f09cbc6c21.tar.gz busybox-w32-a94554d010844e17ecf674fe738679f09cbc6c21.tar.bz2 busybox-w32-a94554d010844e17ecf674fe738679f09cbc6c21.zip |
wget: fix bug where wget creates null file if there is no remote one.
add O_LARGEFILE support (not run tested :).
-rw-r--r-- | libbb/xconnect.c | 2 | ||||
-rw-r--r-- | networking/wget.c | 116 |
2 files changed, 39 insertions, 79 deletions
diff --git a/libbb/xconnect.c b/libbb/xconnect.c index ce1081d59..f88136a1a 100644 --- a/libbb/xconnect.c +++ b/libbb/xconnect.c | |||
@@ -55,7 +55,7 @@ int xconnect(struct sockaddr_in *s_addr) | |||
55 | if (connect(s, (struct sockaddr *)s_addr, sizeof(struct sockaddr_in)) < 0) | 55 | if (connect(s, (struct sockaddr *)s_addr, sizeof(struct sockaddr_in)) < 0) |
56 | { | 56 | { |
57 | if (ENABLE_FEATURE_CLEAN_UP) close(s); | 57 | if (ENABLE_FEATURE_CLEAN_UP) close(s); |
58 | bb_perror_msg_and_die("Unable to connect to remote host (%s)", | 58 | bb_perror_msg_and_die("unable to connect to remote host (%s)", |
59 | inet_ntoa(s_addr->sin_addr)); | 59 | inet_ntoa(s_addr->sin_addr)); |
60 | } | 60 | } |
61 | return s; | 61 | return s; |
diff --git a/networking/wget.c b/networking/wget.c index 1a80972fc..4b55b93f4 100644 --- a/networking/wget.c +++ b/networking/wget.c | |||
@@ -9,7 +9,6 @@ | |||
9 | #include "busybox.h" | 9 | #include "busybox.h" |
10 | #include <getopt.h> | 10 | #include <getopt.h> |
11 | 11 | ||
12 | |||
13 | struct host_info { | 12 | struct host_info { |
14 | char *host; | 13 | char *host; |
15 | int port; | 14 | int port; |
@@ -39,14 +38,6 @@ enum { | |||
39 | static void progressmeter(int flag) {} | 38 | static void progressmeter(int flag) {} |
40 | #endif | 39 | #endif |
41 | 40 | ||
42 | static void close_and_delete_outfile(FILE* output, char *fname_out, int do_continue) | ||
43 | { | ||
44 | if (output != stdout && do_continue == 0) { | ||
45 | fclose(output); | ||
46 | unlink(fname_out); | ||
47 | } | ||
48 | } | ||
49 | |||
50 | /* Read NMEMB elements of SIZE bytes into PTR from STREAM. Returns the | 41 | /* Read NMEMB elements of SIZE bytes into PTR from STREAM. Returns the |
51 | * number of elements read, and a short count if an eof or non-interrupt | 42 | * number of elements read, and a short count if an eof or non-interrupt |
52 | * error is encountered. */ | 43 | * error is encountered. */ |
@@ -62,21 +53,6 @@ static size_t safe_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) | |||
62 | return ret; | 53 | return ret; |
63 | } | 54 | } |
64 | 55 | ||
65 | /* Write NMEMB elements of SIZE bytes from PTR to STREAM. Returns the | ||
66 | * number of elements written, and a short count if an eof or non-interrupt | ||
67 | * error is encountered. */ | ||
68 | static size_t safe_fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream) | ||
69 | { | ||
70 | size_t ret = 0; | ||
71 | |||
72 | do { | ||
73 | clearerr(stream); | ||
74 | ret += fwrite((char *)ptr + (ret * size), size, nmemb - ret, stream); | ||
75 | } while (ret < nmemb && ferror(stream) && errno == EINTR); | ||
76 | |||
77 | return ret; | ||
78 | } | ||
79 | |||
80 | /* Read a line or SIZE - 1 bytes into S, whichever is less, from STREAM. | 56 | /* Read a line or SIZE - 1 bytes into S, whichever is less, from STREAM. |
81 | * Returns S, or NULL if an eof or non-interrupt error is encountered. */ | 57 | * Returns S, or NULL if an eof or non-interrupt error is encountered. */ |
82 | static char *safe_fgets(char *s, int size, FILE *stream) | 58 | static char *safe_fgets(char *s, int size, FILE *stream) |
@@ -91,11 +67,6 @@ static char *safe_fgets(char *s, int size, FILE *stream) | |||
91 | return ret; | 67 | return ret; |
92 | } | 68 | } |
93 | 69 | ||
94 | #define close_delete_and_die(s...) { \ | ||
95 | close_and_delete_outfile(output, fname_out, do_continue); \ | ||
96 | bb_error_msg_and_die(s); } | ||
97 | |||
98 | |||
99 | #ifdef CONFIG_FEATURE_WGET_AUTHENTICATION | 70 | #ifdef CONFIG_FEATURE_WGET_AUTHENTICATION |
100 | /* | 71 | /* |
101 | * Base64-encode character string and return the string. | 72 | * Base64-encode character string and return the string. |
@@ -138,7 +109,6 @@ int wget_main(int argc, char **argv) | |||
138 | char *proxy = 0; | 109 | char *proxy = 0; |
139 | char *dir_prefix=NULL; | 110 | char *dir_prefix=NULL; |
140 | char *s, buf[512]; | 111 | char *s, buf[512]; |
141 | struct stat sbuf; | ||
142 | char extra_headers[1024]; | 112 | char extra_headers[1024]; |
143 | char *extra_headers_ptr = extra_headers; | 113 | char *extra_headers_ptr = extra_headers; |
144 | int extra_headers_left = sizeof(extra_headers); | 114 | int extra_headers_left = sizeof(extra_headers); |
@@ -150,9 +120,9 @@ int wget_main(int argc, char **argv) | |||
150 | FILE *dfp = NULL; /* socket to ftp server (data) */ | 120 | FILE *dfp = NULL; /* socket to ftp server (data) */ |
151 | char *fname_out = NULL; /* where to direct output (-O) */ | 121 | char *fname_out = NULL; /* where to direct output (-O) */ |
152 | int do_continue = 0; /* continue a prev transfer (-c) */ | 122 | int do_continue = 0; /* continue a prev transfer (-c) */ |
153 | long beg_range = 0L; /* range at which continue begins */ | 123 | off64_t beg_range = 0; /* range at which continue begins */ |
154 | int got_clen = 0; /* got content-length: from server */ | 124 | int got_clen = 0; /* got content-length: from server */ |
155 | FILE *output; /* socket to web server */ | 125 | int output_fd = -1; |
156 | int quiet_flag = FALSE; /* Be verry, verry quiet... */ | 126 | int quiet_flag = FALSE; /* Be verry, verry quiet... */ |
157 | int use_proxy = 1; /* Use proxies if env vars are set */ | 127 | int use_proxy = 1; /* Use proxies if env vars are set */ |
158 | char *proxy_flag = "on"; /* Use proxies if env vars are set */ | 128 | char *proxy_flag = "on"; /* Use proxies if env vars are set */ |
@@ -221,7 +191,7 @@ int wget_main(int argc, char **argv) | |||
221 | #endif | 191 | #endif |
222 | bb_get_last_path_component(target.path); | 192 | bb_get_last_path_component(target.path); |
223 | } | 193 | } |
224 | if (fname_out == NULL || strlen(fname_out) < 1) { | 194 | if (!fname_out || !fname_out[0]) { |
225 | fname_out = | 195 | fname_out = |
226 | #ifdef CONFIG_FEATURE_WGET_STATUSBAR | 196 | #ifdef CONFIG_FEATURE_WGET_STATUSBAR |
227 | curfile = | 197 | curfile = |
@@ -238,27 +208,22 @@ int wget_main(int argc, char **argv) | |||
238 | if (do_continue && !fname_out) | 208 | if (do_continue && !fname_out) |
239 | bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); | 209 | bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); |
240 | 210 | ||
241 | |||
242 | /* | ||
243 | * Open the output file stream. | ||
244 | */ | ||
245 | if (strcmp(fname_out, "-") == 0) { | ||
246 | output = stdout; | ||
247 | quiet_flag = TRUE; | ||
248 | } else { | ||
249 | output = xfopen(fname_out, (do_continue ? "a" : "w")); | ||
250 | } | ||
251 | |||
252 | /* | 211 | /* |
253 | * Determine where to start transfer. | 212 | * Determine where to start transfer. |
254 | */ | 213 | */ |
255 | if (do_continue) { | 214 | if (!strcmp(fname_out, "-")) { |
256 | if (fstat(fileno(output), &sbuf) < 0) | 215 | output_fd = 1; |
257 | bb_perror_msg_and_die("fstat"); | 216 | quiet_flag = TRUE; |
258 | if (sbuf.st_size > 0) | 217 | do_continue = 0; |
259 | beg_range = sbuf.st_size; | 218 | } else if (do_continue) { |
260 | else | 219 | output_fd = open(fname_out, O_WRONLY|O_LARGEFILE); |
261 | do_continue = 0; | 220 | if (output_fd >= 0) { |
221 | beg_range = lseek64(output_fd, 0, SEEK_END); | ||
222 | if (beg_range == (off64_t)-1) | ||
223 | bb_perror_msg_and_die("lseek64"); | ||
224 | } | ||
225 | /* File doesn't exist. We do not create file here yet. | ||
226 | We are not sure it exists on remove side */ | ||
262 | } | 227 | } |
263 | 228 | ||
264 | /* We want to do exactly _one_ DNS lookup, since some | 229 | /* We want to do exactly _one_ DNS lookup, since some |
@@ -279,7 +244,7 @@ int wget_main(int argc, char **argv) | |||
279 | got_clen = chunked = 0; | 244 | got_clen = chunked = 0; |
280 | 245 | ||
281 | if (!--try) | 246 | if (!--try) |
282 | close_delete_and_die("too many redirections"); | 247 | bb_error_msg_and_die("too many redirections"); |
283 | 248 | ||
284 | /* | 249 | /* |
285 | * Open socket to http server | 250 | * Open socket to http server |
@@ -317,8 +282,8 @@ int wget_main(int argc, char **argv) | |||
317 | } | 282 | } |
318 | #endif | 283 | #endif |
319 | 284 | ||
320 | if (do_continue) | 285 | if (beg_range) |
321 | fprintf(sfp, "Range: bytes=%ld-\r\n", beg_range); | 286 | fprintf(sfp, "Range: bytes=%lld-\r\n", (long long)beg_range); |
322 | if(extra_headers_left < sizeof(extra_headers)) | 287 | if(extra_headers_left < sizeof(extra_headers)) |
323 | fputs(extra_headers,sfp); | 288 | fputs(extra_headers,sfp); |
324 | fprintf(sfp,"Connection: close\r\n\r\n"); | 289 | fprintf(sfp,"Connection: close\r\n\r\n"); |
@@ -328,7 +293,7 @@ int wget_main(int argc, char **argv) | |||
328 | */ | 293 | */ |
329 | read_response: | 294 | read_response: |
330 | if (fgets(buf, sizeof(buf), sfp) == NULL) | 295 | if (fgets(buf, sizeof(buf), sfp) == NULL) |
331 | close_delete_and_die("no response from server"); | 296 | bb_error_msg_and_die("no response from server"); |
332 | 297 | ||
333 | for (s = buf ; *s != '\0' && !isspace(*s) ; ++s) | 298 | for (s = buf ; *s != '\0' && !isspace(*s) ; ++s) |
334 | ; | 299 | ; |
@@ -340,9 +305,6 @@ read_response: | |||
340 | while (gethdr(buf, sizeof(buf), sfp, &n) != NULL); | 305 | while (gethdr(buf, sizeof(buf), sfp, &n) != NULL); |
341 | goto read_response; | 306 | goto read_response; |
342 | case 200: | 307 | case 200: |
343 | if (do_continue && output != stdout) | ||
344 | output = freopen(fname_out, "w", output); | ||
345 | do_continue = 0; | ||
346 | break; | 308 | break; |
347 | case 300: /* redirection */ | 309 | case 300: /* redirection */ |
348 | case 301: | 310 | case 301: |
@@ -350,12 +312,12 @@ read_response: | |||
350 | case 303: | 312 | case 303: |
351 | break; | 313 | break; |
352 | case 206: | 314 | case 206: |
353 | if (do_continue) | 315 | if (beg_range) |
354 | break; | 316 | break; |
355 | /*FALLTHRU*/ | 317 | /*FALLTHRU*/ |
356 | default: | 318 | default: |
357 | chomp(buf); | 319 | chomp(buf); |
358 | close_delete_and_die("server returned error %d: %s", atoi(s), buf); | 320 | bb_error_msg_and_die("server returned error %d: %s", atoi(s), buf); |
359 | } | 321 | } |
360 | 322 | ||
361 | /* | 323 | /* |
@@ -365,7 +327,7 @@ read_response: | |||
365 | if (strcasecmp(buf, "content-length") == 0) { | 327 | if (strcasecmp(buf, "content-length") == 0) { |
366 | unsigned long value; | 328 | unsigned long value; |
367 | if (safe_strtoul(s, &value)) { | 329 | if (safe_strtoul(s, &value)) { |
368 | close_delete_and_die("content-length %s is garbage", s); | 330 | bb_error_msg_and_die("content-length %s is garbage", s); |
369 | } | 331 | } |
370 | filesize = value; | 332 | filesize = value; |
371 | got_clen = 1; | 333 | got_clen = 1; |
@@ -375,7 +337,7 @@ read_response: | |||
375 | if (strcasecmp(s, "chunked") == 0) { | 337 | if (strcasecmp(s, "chunked") == 0) { |
376 | chunked = got_clen = 1; | 338 | chunked = got_clen = 1; |
377 | } else { | 339 | } else { |
378 | close_delete_and_die("server wants to do %s transfer encoding", s); | 340 | bb_error_msg_and_die("server wants to do %s transfer encoding", s); |
379 | } | 341 | } |
380 | } | 342 | } |
381 | if (strcasecmp(buf, "location") == 0) { | 343 | if (strcasecmp(buf, "location") == 0) { |
@@ -407,7 +369,7 @@ read_response: | |||
407 | 369 | ||
408 | sfp = open_socket(&s_in); | 370 | sfp = open_socket(&s_in); |
409 | if (ftpcmd(NULL, NULL, sfp, buf) != 220) | 371 | if (ftpcmd(NULL, NULL, sfp, buf) != 220) |
410 | close_delete_and_die("%s", buf+4); | 372 | bb_error_msg_and_die("%s", buf+4); |
411 | 373 | ||
412 | /* | 374 | /* |
413 | * Splitting username:password pair, | 375 | * Splitting username:password pair, |
@@ -424,7 +386,7 @@ read_response: | |||
424 | break; | 386 | break; |
425 | /* FALLTHRU (failed login) */ | 387 | /* FALLTHRU (failed login) */ |
426 | default: | 388 | default: |
427 | close_delete_and_die("ftp login: %s", buf+4); | 389 | bb_error_msg_and_die("ftp login: %s", buf+4); |
428 | } | 390 | } |
429 | 391 | ||
430 | ftpcmd("TYPE I", NULL, sfp, buf); | 392 | ftpcmd("TYPE I", NULL, sfp, buf); |
@@ -435,7 +397,7 @@ read_response: | |||
435 | if (ftpcmd("SIZE ", target.path, sfp, buf) == 213) { | 397 | if (ftpcmd("SIZE ", target.path, sfp, buf) == 213) { |
436 | unsigned long value; | 398 | unsigned long value; |
437 | if (safe_strtoul(buf+4, &value)) { | 399 | if (safe_strtoul(buf+4, &value)) { |
438 | close_delete_and_die("SIZE value is garbage"); | 400 | bb_error_msg_and_die("SIZE value is garbage"); |
439 | } | 401 | } |
440 | filesize = value; | 402 | filesize = value; |
441 | got_clen = 1; | 403 | got_clen = 1; |
@@ -445,7 +407,7 @@ read_response: | |||
445 | * Entering passive mode | 407 | * Entering passive mode |
446 | */ | 408 | */ |
447 | if (ftpcmd("PASV", NULL, sfp, buf) != 227) | 409 | if (ftpcmd("PASV", NULL, sfp, buf) != 227) |
448 | close_delete_and_die("PASV: %s", buf+4); | 410 | bb_error_msg_and_die("PASV: %s", buf+4); |
449 | s = strrchr(buf, ','); | 411 | s = strrchr(buf, ','); |
450 | *s = 0; | 412 | *s = 0; |
451 | port = atoi(s+1); | 413 | port = atoi(s+1); |
@@ -454,18 +416,14 @@ read_response: | |||
454 | s_in.sin_port = htons(port); | 416 | s_in.sin_port = htons(port); |
455 | dfp = open_socket(&s_in); | 417 | dfp = open_socket(&s_in); |
456 | 418 | ||
457 | if (do_continue) { | 419 | if (beg_range) { |
458 | sprintf(buf, "REST %ld", beg_range); | 420 | sprintf(buf, "REST %lld", (long long)beg_range); |
459 | if (ftpcmd(buf, NULL, sfp, buf) != 350) { | 421 | if (ftpcmd(buf, NULL, sfp, buf) == 350) |
460 | if (output != stdout) | ||
461 | output = freopen(fname_out, "w", output); | ||
462 | do_continue = 0; | ||
463 | } else | ||
464 | filesize -= beg_range; | 422 | filesize -= beg_range; |
465 | } | 423 | } |
466 | 424 | ||
467 | if (ftpcmd("RETR ", target.path, sfp, buf) > 150) | 425 | if (ftpcmd("RETR ", target.path, sfp, buf) > 150) |
468 | close_delete_and_die("RETR: %s", buf+4); | 426 | bb_error_msg_and_die("RETR: %s", buf+4); |
469 | } | 427 | } |
470 | 428 | ||
471 | 429 | ||
@@ -488,7 +446,9 @@ read_response: | |||
488 | n = safe_fread(buf, 1, rdsz, dfp); | 446 | n = safe_fread(buf, 1, rdsz, dfp); |
489 | if (n <= 0) | 447 | if (n <= 0) |
490 | break; | 448 | break; |
491 | if (safe_fwrite(buf, 1, n, output) != n) { | 449 | if (output_fd < 0) |
450 | output_fd = xopen3(fname_out, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC|O_LARGEFILE, 0666); | ||
451 | if (full_write(output_fd, buf, n) != n) { | ||
492 | bb_perror_msg_and_die(bb_msg_write_error); | 452 | bb_perror_msg_and_die(bb_msg_write_error); |
493 | } | 453 | } |
494 | #ifdef CONFIG_FEATURE_WGET_STATUSBAR | 454 | #ifdef CONFIG_FEATURE_WGET_STATUSBAR |
@@ -526,7 +486,7 @@ read_response: | |||
526 | } | 486 | } |
527 | 487 | ||
528 | 488 | ||
529 | void parse_url(char *url, struct host_info *h) | 489 | static void parse_url(char *url, struct host_info *h) |
530 | { | 490 | { |
531 | char *cp, *sp, *up, *pp; | 491 | char *cp, *sp, *up, *pp; |
532 | 492 | ||
@@ -581,7 +541,7 @@ void parse_url(char *url, struct host_info *h) | |||
581 | } | 541 | } |
582 | 542 | ||
583 | 543 | ||
584 | FILE *open_socket(struct sockaddr_in *s_in) | 544 | static FILE *open_socket(struct sockaddr_in *s_in) |
585 | { | 545 | { |
586 | FILE *fp; | 546 | FILE *fp; |
587 | 547 | ||
@@ -593,7 +553,7 @@ FILE *open_socket(struct sockaddr_in *s_in) | |||
593 | } | 553 | } |
594 | 554 | ||
595 | 555 | ||
596 | char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc) | 556 | static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc) |
597 | { | 557 | { |
598 | char *s, *hdrval; | 558 | char *s, *hdrval; |
599 | int c; | 559 | int c; |