ftp.c (97856) | ftp.c (97866) |
---|---|
1/*- 2 * Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 13 unchanged lines hidden (view full) --- 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 13 unchanged lines hidden (view full) --- 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> |
30__FBSDID("$FreeBSD: head/lib/libfetch/ftp.c 97856 2002-06-05 10:05:03Z des $"); | 30__FBSDID("$FreeBSD: head/lib/libfetch/ftp.c 97866 2002-06-05 12:19:08Z des $"); |
31 32/* 33 * Portions of this code were taken from or based on ftpio.c: 34 * 35 * ---------------------------------------------------------------------------- 36 * "THE BEER-WARE LICENSE" (Revision 42): 37 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you 38 * can do whatever you want with this stuff. If we meet some day, and you think --- 267 unchanged lines hidden (view full) --- 306 tm.tm_hour, tm.tm_min, tm.tm_sec)); 307 return (0); 308} 309 310/* 311 * I/O functions for FTP 312 */ 313struct ftpio { | 31 32/* 33 * Portions of this code were taken from or based on ftpio.c: 34 * 35 * ---------------------------------------------------------------------------- 36 * "THE BEER-WARE LICENSE" (Revision 42): 37 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you 38 * can do whatever you want with this stuff. If we meet some day, and you think --- 267 unchanged lines hidden (view full) --- 306 tm.tm_hour, tm.tm_min, tm.tm_sec)); 307 return (0); 308} 309 310/* 311 * I/O functions for FTP 312 */ 313struct ftpio { |
314 conn_t *conn; /* Control connection */ 315 int dsd; /* Data socket descriptor */ | 314 conn_t *cconn; /* Control connection */ 315 conn_t *dconn; /* Data connection */ |
316 int dir; /* Direction */ 317 int eof; /* EOF reached */ 318 int err; /* Error code */ 319}; 320 321static int _ftp_readfn(void *, char *, int); 322static int _ftp_writefn(void *, const char *, int); 323static fpos_t _ftp_seekfn(void *, fpos_t, int); --- 5 unchanged lines hidden (view full) --- 329 struct ftpio *io; 330 int r; 331 332 io = (struct ftpio *)v; 333 if (io == NULL) { 334 errno = EBADF; 335 return (-1); 336 } | 316 int dir; /* Direction */ 317 int eof; /* EOF reached */ 318 int err; /* Error code */ 319}; 320 321static int _ftp_readfn(void *, char *, int); 322static int _ftp_writefn(void *, const char *, int); 323static fpos_t _ftp_seekfn(void *, fpos_t, int); --- 5 unchanged lines hidden (view full) --- 329 struct ftpio *io; 330 int r; 331 332 io = (struct ftpio *)v; 333 if (io == NULL) { 334 errno = EBADF; 335 return (-1); 336 } |
337 if (io->conn == NULL || io->dsd == -1 || io->dir == O_WRONLY) { | 337 if (io->cconn == NULL || io->dconn == NULL || io->dir == O_WRONLY) { |
338 errno = EBADF; 339 return (-1); 340 } 341 if (io->err) { 342 errno = io->err; 343 return (-1); 344 } 345 if (io->eof) 346 return (0); | 338 errno = EBADF; 339 return (-1); 340 } 341 if (io->err) { 342 errno = io->err; 343 return (-1); 344 } 345 if (io->eof) 346 return (0); |
347 r = read(io->dsd, buf, len); | 347 r = _fetch_read(io->dconn, buf, len); |
348 if (r > 0) 349 return (r); 350 if (r == 0) { 351 io->eof = 1; 352 return (0); 353 } 354 if (errno != EINTR) 355 io->err = errno; --- 6 unchanged lines hidden (view full) --- 362 struct ftpio *io; 363 int w; 364 365 io = (struct ftpio *)v; 366 if (io == NULL) { 367 errno = EBADF; 368 return (-1); 369 } | 348 if (r > 0) 349 return (r); 350 if (r == 0) { 351 io->eof = 1; 352 return (0); 353 } 354 if (errno != EINTR) 355 io->err = errno; --- 6 unchanged lines hidden (view full) --- 362 struct ftpio *io; 363 int w; 364 365 io = (struct ftpio *)v; 366 if (io == NULL) { 367 errno = EBADF; 368 return (-1); 369 } |
370 if (io->conn == NULL || io->dsd == -1 || io->dir == O_RDONLY) { | 370 if (io->cconn == NULL || io->dconn == NULL || io->dir == O_RDONLY) { |
371 errno = EBADF; 372 return (-1); 373 } 374 if (io->err) { 375 errno = io->err; 376 return (-1); 377 } | 371 errno = EBADF; 372 return (-1); 373 } 374 if (io->err) { 375 errno = io->err; 376 return (-1); 377 } |
378 w = write(io->dsd, buf, len); | 378 w = _fetch_write(io->dconn, buf, len); |
379 if (w >= 0) 380 return (w); 381 if (errno != EINTR) 382 io->err = errno; 383 return (-1); 384} 385 386static fpos_t --- 18 unchanged lines hidden (view full) --- 405 406 io = (struct ftpio *)v; 407 if (io == NULL) { 408 errno = EBADF; 409 return (-1); 410 } 411 if (io->dir == -1) 412 return (0); | 379 if (w >= 0) 380 return (w); 381 if (errno != EINTR) 382 io->err = errno; 383 return (-1); 384} 385 386static fpos_t --- 18 unchanged lines hidden (view full) --- 405 406 io = (struct ftpio *)v; 407 if (io == NULL) { 408 errno = EBADF; 409 return (-1); 410 } 411 if (io->dir == -1) 412 return (0); |
413 if (io->conn == NULL || io->dsd == -1) { | 413 if (io->cconn == NULL || io->dconn == NULL) { |
414 errno = EBADF; 415 return (-1); 416 } | 414 errno = EBADF; 415 return (-1); 416 } |
417 close(io->dsd); | 417 _fetch_close(io->dconn); |
418 io->dir = -1; | 418 io->dir = -1; |
419 io->dsd = -1; | 419 io->dconn = NULL; |
420 DEBUG(fprintf(stderr, "Waiting for final status\n")); | 420 DEBUG(fprintf(stderr, "Waiting for final status\n")); |
421 r = _ftp_chkerr(io->conn); 422 _fetch_close(io->conn); | 421 r = _ftp_chkerr(io->cconn); 422 _fetch_close(io->cconn); |
423 free(io); 424 return (r == FTP_TRANSFER_COMPLETE) ? 0 : -1; 425} 426 427static FILE * | 423 free(io); 424 return (r == FTP_TRANSFER_COMPLETE) ? 0 : -1; 425} 426 427static FILE * |
428_ftp_setup(conn_t *conn, int dsd, int mode) | 428_ftp_setup(conn_t *cconn, conn_t *dconn, int mode) |
429{ 430 struct ftpio *io; 431 FILE *f; 432 | 429{ 430 struct ftpio *io; 431 FILE *f; 432 |
433 if (cconn == NULL || dconn == NULL) 434 return (NULL); |
|
433 if ((io = malloc(sizeof *io)) == NULL) 434 return (NULL); | 435 if ((io = malloc(sizeof *io)) == NULL) 436 return (NULL); |
435 io->conn = conn; 436 io->dsd = dsd; | 437 io->cconn = cconn; 438 io->dconn = dconn; |
437 io->dir = mode; 438 io->eof = io->err = 0; 439 f = funopen(io, _ftp_readfn, _ftp_writefn, _ftp_seekfn, _ftp_closefn); 440 if (f == NULL) 441 free(io); 442 return (f); 443} 444 --- 244 unchanged lines hidden (view full) --- 689 690 /* accept the incoming connection and go to town */ 691 if ((d = accept(sd, NULL, NULL)) == -1) 692 goto sysouch; 693 close(sd); 694 sd = d; 695 } 696 | 439 io->dir = mode; 440 io->eof = io->err = 0; 441 f = funopen(io, _ftp_readfn, _ftp_writefn, _ftp_seekfn, _ftp_closefn); 442 if (f == NULL) 443 free(io); 444 return (f); 445} 446 --- 244 unchanged lines hidden (view full) --- 691 692 /* accept the incoming connection and go to town */ 693 if ((d = accept(sd, NULL, NULL)) == -1) 694 goto sysouch; 695 close(sd); 696 sd = d; 697 } 698 |
697 if ((df = _ftp_setup(conn, sd, mode)) == NULL) | 699 if ((df = _ftp_setup(conn, _fetch_reopen(sd), mode)) == NULL) |
698 goto sysouch; 699 return (df); 700 701sysouch: 702 _fetch_syserr(); 703 if (sd >= 0) 704 close(sd); 705 return (NULL); --- 303 unchanged lines hidden --- | 700 goto sysouch; 701 return (df); 702 703sysouch: 704 _fetch_syserr(); 705 if (sd >= 0) 706 close(sd); 707 return (NULL); --- 303 unchanged lines hidden --- |