Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/ftp_get_file.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
File MIME type: text/x-csrc
File size: 3335 byte(s)
marged ftp_append_buffer.c and ftp_put_buffer.c. added ftp_append_file() function.

1 hirohitohigashi 28 /*
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 <sys/uio.h>
16     #include <fcntl.h>
17     #include <string.h>
18     #include <errno.h>
19    
20    
21     /***** Local headers ********************************************************/
22     #include "liboftp.h"
23     #include "sub.h"
24    
25    
26     /***** Constat values *******************************************************/
27     #define TRANSFER_SEGMENT_SIZE 1024 /* 一度のrecv()で転送するバイト数 */
28    
29    
30     /***** Macros ***************************************************************/
31     /***** Typedefs *************************************************************/
32     /***** Function prototypes **************************************************/
33     /***** Local variables ******************************************************/
34     /***** Global variables *****************************************************/
35     /***** Signal catching functions ********************************************/
36     /***** Local functions ******************************************************/
37     /***** Global functions *****************************************************/
38    
39     /****************************************************************************/
40     /*! ファイル取得してローカルファイルへ
41     *
42     *@param ftp LIBOFTPへのポインタ。
43     *@param fname サーバ上のファイル名
44     *@param local_fname ローカルファイル名
45     *@retval int 0: no error. -1: OS level error. -2: ftp protocol error. -3: buffer too small.
46     *@note
47     */
48     int ftp_get_file( LIBOFTP *ftp, const char *fname, const char *local_fname )
49     {
50     int data_socket;
51     int n, res;
52     int fd;
53     char buf[TRANSFER_SEGMENT_SIZE];
54    
55    
56     /*
57     * ローカルファイルオープン
58     */
59     fd = open( local_fname, O_WRONLY|O_CREAT, 0644 );
60     if( fd < 0 ) {
61     DEBUGPRINT1( "get_file: local file open error. %s\n", strerror(errno) );
62     return -1;
63     }
64    
65     /*
66     * 受信準備
67     */
68     if( ftp->flag_passive ) {
69     data_socket = ftp_getready_pasv( ftp, "RETR", fname );
70     } else {
71     data_socket = ftp_getready_active( ftp, "RETR", fname );
72     }
73     if( data_socket < 0 ) {
74     close( fd );
75     return data_socket;
76     }
77     /*
78     * タイムアウトが意図通りに働くように、分割してrecvする。
79     */
80     while( 1 ) {
81     n = recv( data_socket, buf, TRANSFER_SEGMENT_SIZE, 0 );
82     DEBUGPRINT1( "RECV: n=%d\n", n );
83     if( n < 0 ) {
84     DEBUGPRINT1( "get_file: recv error. %s\n", strerror(errno) );
85     close( fd );
86     close( data_socket );
87     return -1;
88     }
89     if( n == 0 ) {
90     break;
91     }
92    
93     if( write( fd, buf, n ) != n ) {
94     DEBUGPRINT1( "get_file: write error. %s\n", strerror(errno) );
95     close( fd );
96     close( data_socket );
97     return -1;
98     }
99     }
100     close( fd );
101     close( data_socket );
102    
103     /*
104     * receive response.
105     */
106     if( (res = ftp_receive_response( ftp, 0, 0 )) != 226 ) { /* 226: Closing data connection. */
107     DEBUGPRINT1( "get_buffer: got illegal response %d\n", res );
108     return -2;
109     }
110    
111     return 0;
112     }

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