Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/ftp_put_file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 30 - (hide annotations) (download) (as text)
Sat Feb 28 13:03:02 2009 UTC (15 years, 1 month ago) by hirohitohigashi
File MIME type: text/x-csrc
File size: 4332 byte(s)
revised comments.

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     /****************************************************************************/
38     /*! ファイル送信メイン
39     *
40     *@param ftp LIBOFTPへのポインタ。
41     *@param local_fname ローカルファイル名
42     *@param fname サーバ上のファイル名
43     *@param cmd 送信するFTPコマンド
44 hirohitohigashi 30 *@retval int エラーコード
45 hirohitohigashi 28 *@note
46     */
47     static int ftp_put_file_main( LIBOFTP *ftp, const char *local_fname, const char *fname, const char *cmd )
48     {
49     int data_socket;
50     int fd;
51     char buf[TRANSFER_SEGMENT_SIZE];
52 hirohitohigashi 29 int res;
53 hirohitohigashi 28
54     /*
55     * ローカルファイルオープン
56     */
57     fd = open( local_fname, O_RDONLY );
58     if( fd < 0 ) {
59 hirohitohigashi 29 DEBUGPRINT1( "local file open error. %s\n", strerror(errno) );
60     return LIBOFTP_ERROR_OS;
61 hirohitohigashi 28 }
62    
63     /*
64     * 送信準備
65     */
66     if( ftp->flag_passive ) {
67     data_socket = ftp_getready_pasv( ftp, cmd, fname );
68     } else {
69     data_socket = ftp_getready_active( ftp, cmd, fname );
70     }
71     if( data_socket < 0 ) {
72     close( fd );
73     return data_socket;
74     }
75    
76     /*
77     * タイムアウトが意図通りに働くように、分割してsendする。
78     */
79     while( 1 ) {
80     int n_rd, n_wr;
81     n_rd = read( fd, buf, TRANSFER_SEGMENT_SIZE );
82     if( n_rd == 0 ) break;
83    
84     if( n_rd < 0 ) {
85     if( errno == EINTR ) continue;
86    
87 hirohitohigashi 29 DEBUGPRINT1( "local file read error. %s\n", strerror(errno) );
88     copy_strerror();
89 hirohitohigashi 28 close( data_socket );
90 hirohitohigashi 29 return LIBOFTP_ERROR_OS;
91 hirohitohigashi 28 }
92    
93     n_wr = sendn( data_socket, buf, n_rd, 0 );
94     DEBUGPRINT1( "SEND: n=%d\n", n_wr );
95     if( n_wr < 0 ) {
96 hirohitohigashi 29 copy_strerror();
97 hirohitohigashi 28 close( data_socket );
98 hirohitohigashi 29 return LIBOFTP_ERROR_OS;
99 hirohitohigashi 28 }
100     }
101     close( fd );
102     close( data_socket );
103    
104     /*
105     * receive response.
106     */
107 hirohitohigashi 29 res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
108     if( res != 226 ) { /* 226: Closing data connection. */
109     DEBUGPRINT1( "got illegal response %d\n", res );
110     return res<0? res: LIBOFTP_ERROR_PROTOCOL;
111 hirohitohigashi 28 }
112    
113 hirohitohigashi 29 return LIBOFTP_NOERROR;
114 hirohitohigashi 28 }
115    
116    
117    
118     /***** Global functions *****************************************************/
119    
120     /****************************************************************************/
121     /*! ファイル送信
122     *
123     *@param ftp LIBOFTPへのポインタ。
124     *@param local_fname ローカルファイル名
125     *@param fname サーバ上のファイル名
126 hirohitohigashi 30 *@retval int エラーコード
127 hirohitohigashi 28 *@note
128     */
129     int ftp_put_file( LIBOFTP *ftp, const char *local_fname, const char *fname )
130     {
131     return ftp_put_file_main( ftp, local_fname, fname, "STOR" );
132     }
133    
134    
135    
136     /****************************************************************************/
137     /*! ファイル送信 アペンドモード
138     *
139     *@param ftp LIBOFTPへのポインタ。
140     *@param local_fname ローカルファイル名
141     *@param fname サーバ上のファイル名
142 hirohitohigashi 30 *@retval int エラーコード
143 hirohitohigashi 28 *@note
144     */
145     int ftp_append_file( LIBOFTP *ftp, const char *local_fname, const char *fname )
146     {
147     return ftp_put_file_main( ftp, local_fname, fname, "APPE" );
148     }
149    

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