| 9 |
|
|
| 10 |
/***** Feature test switches ************************************************/ |
/***** Feature test switches ************************************************/ |
| 11 |
/***** System headers *******************************************************/ |
/***** System headers *******************************************************/ |
|
#include <unistd.h> |
|
|
#include <sys/types.h> |
|
|
#include <sys/socket.h> |
|
|
#include <string.h> |
|
|
#include <errno.h> |
|
|
|
|
|
|
|
| 12 |
/***** Local headers ********************************************************/ |
/***** Local headers ********************************************************/ |
| 13 |
#include "liboftp.h" |
#include "liboftp.h" |
| 14 |
#include "sub.h" |
#include "sub.h" |
| 15 |
|
|
| 16 |
|
|
| 17 |
/***** Constat values *******************************************************/ |
/***** Constat values *******************************************************/ |
|
#define TRANSFER_SEGMENT_SIZE 4096 /* 一度のrecv()で転送するバイト数 */ |
|
| 18 |
|
|
| 19 |
|
|
| 20 |
/***** Macros ***************************************************************/ |
/***** Macros ***************************************************************/ |
| 38 |
*/ |
*/ |
| 39 |
int ftp_get_buffer( LIBOFTP *ftp, const char *fname, char *buf, int bufsiz ) |
int ftp_get_buffer( LIBOFTP *ftp, const char *fname, char *buf, int bufsiz ) |
| 40 |
{ |
{ |
| 41 |
int data_socket; |
return ftp_get_buffer_main( ftp, "RETR", fname, buf, bufsiz ); |
|
char *p = buf; |
|
|
int res; |
|
|
|
|
|
/* |
|
|
* 受信準備 |
|
|
*/ |
|
|
if( ftp->flag_passive ) { |
|
|
data_socket = ftp_getready_pasv( ftp, "RETR", fname ); |
|
|
} else { |
|
|
data_socket = ftp_getready_active( ftp, "RETR", fname ); |
|
|
} |
|
|
if( data_socket < 0 ) { |
|
|
return data_socket; |
|
|
} |
|
|
|
|
|
/* |
|
|
* タイムアウトが意図通りに働くように、分割してrecvする。 |
|
|
*/ |
|
|
while( 1 ) { |
|
|
int n; |
|
|
int len = bufsiz; |
|
|
if( len > TRANSFER_SEGMENT_SIZE ) { |
|
|
len = TRANSFER_SEGMENT_SIZE; |
|
|
} |
|
|
n = recv( data_socket, p, len, 0 ); |
|
|
DEBUGPRINT1( "RECV: n=%d\n", n ); |
|
|
if( n < 0 ) { |
|
|
int ret; |
|
|
DEBUGPRINT1( "recv error. %s\n", strerror(errno) ); |
|
|
if( errno == EAGAIN ) { |
|
|
ret = LIBOFTP_ERROR_TIMEOUT; |
|
|
} else { |
|
|
ret = LIBOFTP_ERROR_OS; |
|
|
copy_strerror(); |
|
|
} |
|
|
close( data_socket ); |
|
|
return ret; |
|
|
} |
|
|
if( n == 0 ) { |
|
|
break; |
|
|
} |
|
|
p += n; |
|
|
bufsiz -= n; |
|
|
if( bufsiz <= 0 ) { |
|
|
break; /* buffer too small */ |
|
|
} |
|
|
} |
|
|
|
|
|
if( bufsiz > 0 ) { /* バッファ不足だったか? */ |
|
|
/* |
|
|
* 不足していない |
|
|
*/ |
|
|
close( data_socket ); |
|
|
|
|
|
/* receive response. */ |
|
|
res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 ); |
|
|
if( res != 226 ) { /* 226: Closing data connection. */ |
|
|
DEBUGPRINT1( "got illegal response %d\n", res ); |
|
|
return res < 0? res: LIBOFTP_ERROR_PROTOCOL; |
|
|
} |
|
|
|
|
|
return p - buf; /* return with transfered bytes */ |
|
|
|
|
|
} else { |
|
|
/* |
|
|
* バッファ不足時 |
|
|
*/ |
|
|
DEBUGPRINT1( "buffer too small. %s\n", "" ); |
|
|
strncpy( ftp->error_message, "buffer too small", sizeof( ftp->error_message ) - 1 ); |
|
|
|
|
|
if( ftp_send_command( ftp, "ABOR\r\n" ) < 0 ) { |
|
|
DEBUGPRINT1( "command sending error. %s\n", "ABOR" ); |
|
|
close( data_socket ); |
|
|
return LIBOFTP_ERROR_BUFFER; |
|
|
} |
|
|
|
|
|
res = ftp_receive_response( ftp, 0, 0 ); |
|
|
if( res == 426 ) { /* 426: Connection closed; transfer aborted. */ |
|
|
res = ftp_receive_response( ftp, 0, 0 ); |
|
|
} |
|
|
close( data_socket ); |
|
|
if( res != 226 ) { /* 226: Closing data connection. */ |
|
|
DEBUGPRINT1( "got illegal response %d\n", res ); |
|
|
return res < 0? res: LIBOFTP_ERROR_PROTOCOL; |
|
|
} |
|
|
|
|
|
return LIBOFTP_ERROR_BUFFER; |
|
|
} |
|
| 42 |
} |
} |