Develop and Download Open Source Software

Browse Subversion Repository

Contents of /tags/REL-2.2/ftp_get_buffer.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 30 - (show annotations) (download) (as text)
Sat Feb 28 13:03:02 2009 UTC (15 years, 1 month ago) by hirohitohigashi
Original Path: trunk/ftp_get_buffer.c
File MIME type: text/x-csrc
File size: 3219 byte(s)
revised comments.

1 /*
2 liboftp: this is an FTP library to simplify the work to a Developer
3 who want to work with FTP servers (RFC 959).
4
5 Copyright (c) 2009 hirohito higashi. All rights reserved.
6 This file is distributed under BSD license.
7 */
8
9
10 /***** Feature test switches ************************************************/
11 /***** System headers *******************************************************/
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <string.h>
16 #include <errno.h>
17
18
19 /***** Local headers ********************************************************/
20 #include "liboftp.h"
21 #include "sub.h"
22
23
24 /***** Constat values *******************************************************/
25 #define TRANSFER_SEGMENT_SIZE 4096 /* 一度のrecv()で転送するバイト数 */
26
27
28 /***** Macros ***************************************************************/
29 /***** Typedefs *************************************************************/
30 /***** Function prototypes **************************************************/
31 /***** Local variables ******************************************************/
32 /***** Global variables *****************************************************/
33 /***** Signal catching functions ********************************************/
34 /***** Local functions ******************************************************/
35 /***** Global functions *****************************************************/
36
37 /****************************************************************************/
38 /*! バッファへファイル取得
39 *
40 *@param ftp LIBOFTPへのポインタ。
41 *@param fname サーバ上のファイル名
42 *@param buf バッファへのポインタ
43 *@param bufsiz バッファサイズ
44 *@retval int エラーコード
45 *@note
46 */
47 int ftp_get_buffer( LIBOFTP *ftp, const char *fname, char *buf, int bufsiz )
48 {
49 int data_socket;
50 char *p = buf;
51 int n, res;
52
53 /*
54 * 受信準備
55 */
56 if( ftp->flag_passive ) {
57 data_socket = ftp_getready_pasv( ftp, "RETR", fname );
58 } else {
59 data_socket = ftp_getready_active( ftp, "RETR", fname );
60 }
61 if( data_socket < 0 ) {
62 return data_socket;
63 }
64
65 /*
66 * タイムアウトが意図通りに働くように、分割してrecvする。
67 */
68 while( 1 ) {
69 int len = bufsiz;
70 if( len > TRANSFER_SEGMENT_SIZE ) {
71 len = TRANSFER_SEGMENT_SIZE;
72 }
73 n = recv( data_socket, p, len, 0 );
74 DEBUGPRINT1( "RECV: n=%d\n", n );
75 if( n < 0 ) {
76 DEBUGPRINT1( "recv error. %s\n", strerror(errno) );
77 copy_strerror();
78 close( data_socket );
79 return LIBOFTP_ERROR_OS;
80 }
81 if( n == 0 ) {
82 break;
83 }
84 p += n;
85 bufsiz -= n;
86 if( bufsiz <= 0 ) {
87 break;
88 }
89 }
90 close( data_socket );
91
92 /*
93 * receive response.
94 */
95 res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
96 if( res != 226 ) { /* 226: Closing data connection. */
97 DEBUGPRINT1( "got illegal response %d\n", res );
98 return res<0? res: LIBOFTP_ERROR_PROTOCOL;
99 }
100
101 if( bufsiz <= 0 ) {
102 DEBUGPRINT1( "buffer too small. %s\n", "" );
103 strncpy( ftp->error_message, "buffer too small", sizeof( ftp->error_message ) - 1 );
104 return LIBOFTP_ERROR_BUFFER;
105 }
106
107 return LIBOFTP_NOERROR;
108 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26