Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/ftp_get_buffer.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (show annotations) (download) (as text)
Wed Feb 25 13:47:38 2009 UTC (15 years, 1 month ago) by hirohitohigashi
File MIME type: text/x-csrc
File size: 2928 byte(s)
Starting 2nd version of libOftp project. There are new designs.

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
16
17 /***** Local headers ********************************************************/
18 #include "liboftp.h"
19 #include "sub.h"
20
21
22 /***** Constat values *******************************************************/
23 #define TRANSFER_SEGMENT_SIZE 4096 /* 一度のrecv()で転送するバイト数 */
24
25
26 /***** Macros ***************************************************************/
27 /***** Typedefs *************************************************************/
28 /***** Function prototypes **************************************************/
29 /***** Local variables ******************************************************/
30 /***** Global variables *****************************************************/
31 /***** Signal catching functions ********************************************/
32 /***** Local functions ******************************************************/
33 /***** Global functions *****************************************************/
34
35 /****************************************************************************/
36 /*! バッファへファイル取得
37 *
38 *@param ftp LIBOFTPへのポインタ。
39 *@param fname サーバ上のファイル名
40 *@param buf バッファへのポインタ
41 *@param bufsiz バッファサイズ
42 *@retval int 0: no error. -1: OS level error. -2: ftp protocol error. -3: buffer too small.
43 *@note
44 */
45 int ftp_get_buffer( LIBOFTP *ftp, const char *fname, char *buf, int bufsiz )
46 {
47 int data_socket;
48 char *p = buf;
49 int n;
50
51 /*
52 * 受信準備
53 */
54 if( ftp->flag_passive ) {
55 data_socket = ftp_getready_pasv( ftp, fname, "RETR" );
56 if( data_socket < 0 ) {
57 return -1; /* XXX: mixed os level error and ftp protocol error. */
58 }
59 } else {
60 return -2; /* XXX: active mode not ready yet. */
61 }
62
63 /*
64 * タイムアウトが意図通りに働くように、分割してrecvする。
65 */
66 while( 1 ) {
67 int len = bufsiz;
68 if( len > TRANSFER_SEGMENT_SIZE ) {
69 len = TRANSFER_SEGMENT_SIZE;
70 }
71 n = recv( data_socket, p, len, 0 );
72 DEBUGPRINT1( "RECV: n=%d\n", n );
73 if( n < 0 ) {
74 close( data_socket );
75 return -1;
76 }
77 if( n == 0 ) {
78 break;
79 }
80 p += n;
81 bufsiz -= n;
82 if( bufsiz <= 0 ) {
83 break;
84 }
85 }
86 close( data_socket );
87
88 /*
89 * receive response.
90 */
91 if( ftp_receive_response( ftp, 0, 0 ) != 226 ) { /* 226: Closing data connection. */
92 return -2;
93 }
94
95 if( bufsiz <= 0 ) {
96 DEBUGPRINT1( "get_buffer: buffer too small. %s\n", "" );
97 return -3;
98 }
99 return 0;
100 }

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