Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /tags/REL-2.1/ftp_get_buffer.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 28 - (hide annotations) (download) (as text)
Sat Feb 28 04:14:38 2009 UTC (15 years, 1 month ago) by hirohitohigashi
Original Path: trunk/ftp_get_buffer.c
File MIME type: text/x-csrc
File size: 3072 byte(s)
marged ftp_append_buffer.c and ftp_put_buffer.c. added ftp_append_file() function.

1 hirohitohigashi 17 /*
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 hirohitohigashi 28 #include <string.h>
16     #include <errno.h>
17 hirohitohigashi 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 0: no error. -1: OS level error. -2: ftp protocol error. -3: buffer too small.
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 hirohitohigashi 20 int n, res;
52 hirohitohigashi 17
53     /*
54     * 受信準備
55     */
56     if( ftp->flag_passive ) {
57 hirohitohigashi 26 data_socket = ftp_getready_pasv( ftp, "RETR", fname );
58 hirohitohigashi 17 } else {
59 hirohitohigashi 26 data_socket = ftp_getready_active( ftp, "RETR", fname );
60 hirohitohigashi 17 }
61 hirohitohigashi 20 if( data_socket < 0 ) {
62     return data_socket;
63     }
64 hirohitohigashi 17
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 hirohitohigashi 28 DEBUGPRINT1( "get_buffer: recv error. %s\n", strerror(errno) );
77 hirohitohigashi 17 close( data_socket );
78     return -1;
79     }
80     if( n == 0 ) {
81     break;
82     }
83     p += n;
84     bufsiz -= n;
85     if( bufsiz <= 0 ) {
86     break;
87     }
88     }
89     close( data_socket );
90    
91     /*
92     * receive response.
93     */
94 hirohitohigashi 20 if( (res = ftp_receive_response( ftp, 0, 0 )) != 226 ) { /* 226: Closing data connection. */
95     DEBUGPRINT1( "get_buffer: got illegal response %d\n", res );
96 hirohitohigashi 17 return -2;
97     }
98    
99     if( bufsiz <= 0 ) {
100     DEBUGPRINT1( "get_buffer: buffer too small. %s\n", "" );
101     return -3;
102     }
103     return 0;
104     }

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