Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /tags/REL-2.2/ftp_get_file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 55 - (hide annotations) (download) (as text)
Wed Mar 24 08:04:56 2010 UTC (14 years ago) by hirohitohigashi
File MIME type: text/x-csrc
File size: 3594 byte(s)
Tag release 2.2
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 hirohitohigashi 30 *@retval int エラーコード
46 hirohitohigashi 28 *@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 hirohitohigashi 50 if( ftp->socket < 0 ) return LIBOFTP_ERROR;
56 hirohitohigashi 28
57     /*
58     * ローカルファイルオープン
59     */
60     fd = open( local_fname, O_WRONLY|O_CREAT, 0644 );
61     if( fd < 0 ) {
62 hirohitohigashi 29 DEBUGPRINT1( "local file open error. %s\n", strerror(errno) );
63     copy_strerror();
64     return LIBOFTP_ERROR_OS;
65 hirohitohigashi 28 }
66    
67     /*
68     * 受信準備
69     */
70     if( ftp->flag_passive ) {
71     data_socket = ftp_getready_pasv( ftp, "RETR", fname );
72     } else {
73     data_socket = ftp_getready_active( ftp, "RETR", fname );
74     }
75     if( data_socket < 0 ) {
76     close( fd );
77     return data_socket;
78     }
79     /*
80     * タイムアウトが意図通りに働くように、分割してrecvする。
81     */
82     while( 1 ) {
83     n = recv( data_socket, buf, TRANSFER_SEGMENT_SIZE, 0 );
84     DEBUGPRINT1( "RECV: n=%d\n", n );
85     if( n < 0 ) {
86 hirohitohigashi 36 int ret;
87 hirohitohigashi 29 DEBUGPRINT1( "recv error. %s\n", strerror(errno) );
88 hirohitohigashi 36 if( errno == EAGAIN ) {
89     ret = LIBOFTP_ERROR_TIMEOUT;
90     } else {
91     ret = LIBOFTP_ERROR_OS;
92     copy_strerror();
93     }
94 hirohitohigashi 28 close( fd );
95     close( data_socket );
96 hirohitohigashi 36 return ret;
97 hirohitohigashi 28 }
98     if( n == 0 ) {
99     break;
100     }
101    
102     if( write( fd, buf, n ) != n ) {
103 hirohitohigashi 29 DEBUGPRINT1( "write error. %s\n", strerror(errno) );
104     copy_strerror();
105 hirohitohigashi 28 close( fd );
106     close( data_socket );
107 hirohitohigashi 29 return LIBOFTP_ERROR_OS;
108 hirohitohigashi 28 }
109     }
110     close( fd );
111     close( data_socket );
112    
113     /*
114     * receive response.
115     */
116 hirohitohigashi 29 res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
117     if( res != 226 ) { /* 226: Closing data connection. */
118     DEBUGPRINT1( "got illegal response %d\n", res );
119     return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
120 hirohitohigashi 28 }
121    
122 hirohitohigashi 29 return LIBOFTP_NOERROR;
123 hirohitohigashi 28 }

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