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 34 - (show annotations) (download) (as text)
Sun Mar 1 12:43:14 2009 UTC (15 years, 1 month ago) by hirohitohigashi
Original Path: trunk/ftp_get_buffer.c
File MIME type: text/x-csrc
File size: 3300 byte(s)
added COPYRIGHT file. and fix some bugs.

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 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 n;
70 int len = bufsiz;
71 if( len > TRANSFER_SEGMENT_SIZE ) {
72 len = TRANSFER_SEGMENT_SIZE;
73 }
74 n = recv( data_socket, p, len, 0 );
75 DEBUGPRINT1( "RECV: n=%d\n", n );
76 if( n < 0 ) {
77 DEBUGPRINT1( "recv error. %s\n", strerror(errno) );
78 copy_strerror();
79 close( data_socket );
80 return LIBOFTP_ERROR_OS;
81 }
82 if( n == 0 ) {
83 break;
84 }
85 p += n;
86 bufsiz -= n;
87 if( bufsiz <= 0 ) {
88 break;
89 }
90 }
91 close( data_socket );
92
93 /*
94 * receive response.
95 */
96 res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
97 if( res != 226 ) { /* 226: Closing data connection. */
98 DEBUGPRINT1( "got illegal response %d\n", res );
99 return res<0? res: LIBOFTP_ERROR_PROTOCOL;
100 }
101
102 if( bufsiz <= 0 ) {
103 DEBUGPRINT1( "buffer too small. %s\n", "" );
104 strncpy( ftp->error_message, "buffer too small", sizeof( ftp->error_message ) - 1 );
105 return LIBOFTP_ERROR_BUFFER;
106 }
107
108 return p - buf; /* return with transfered bytes */
109 }

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