Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/ftp_put_buffer.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 50 - (hide annotations) (download) (as text)
Wed Dec 2 15:13:30 2009 UTC (14 years, 4 months ago) by hirohitohigashi
File MIME type: text/x-csrc
File size: 4105 byte(s)
add error check
1 hirohitohigashi 17 /*
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    
34    
35     /****************************************************************************/
36     /*! バッファからファイル送信
37     *
38     *@param ftp LIBOFTPへのポインタ。
39     *@param buf バッファへのポインタ
40     *@param bufsiz バッファサイズ
41 hirohitohigashi 27 *@param fname サーバ上のファイル名
42 hirohitohigashi 28 *@param cmd 送信するFTPコマンド
43 hirohitohigashi 30 *@retval int エラーコード
44 hirohitohigashi 17 *@note
45     */
46 hirohitohigashi 45 static int ftp_put_buffer_main( LIBOFTP *ftp, const char *buf, int bufsiz, const char *fname, const char *cmd )
47 hirohitohigashi 17 {
48     int data_socket;
49 hirohitohigashi 45 const char *p = buf;
50 hirohitohigashi 29 int n, res;
51 hirohitohigashi 17
52 hirohitohigashi 50 if( ftp->socket < 0 ) return LIBOFTP_ERROR;
53    
54 hirohitohigashi 17 /*
55     * 送信準備
56     */
57     if( ftp->flag_passive ) {
58 hirohitohigashi 28 data_socket = ftp_getready_pasv( ftp, cmd, fname );
59 hirohitohigashi 17 } else {
60 hirohitohigashi 28 data_socket = ftp_getready_active( ftp, cmd, fname );
61 hirohitohigashi 17 }
62 hirohitohigashi 20 if( data_socket < 0 ) {
63     return data_socket;
64     }
65 hirohitohigashi 17
66     /*
67     * タイムアウトが意図通りに働くように、分割してsendする。
68     */
69     while( 1 ) {
70     int len = bufsiz;
71     if( len > TRANSFER_SEGMENT_SIZE ) {
72     len = TRANSFER_SEGMENT_SIZE;
73     }
74     n = sendn( data_socket, p, len, 0 );
75     DEBUGPRINT1( "SEND: n=%d\n", n );
76     if( n < 0 ) {
77 hirohitohigashi 29 DEBUGPRINT1( "recv error. %s\n", strerror(errno) );
78     copy_strerror();
79 hirohitohigashi 17 close( data_socket );
80 hirohitohigashi 29 return LIBOFTP_ERROR_OS;
81 hirohitohigashi 17 }
82 hirohitohigashi 28
83 hirohitohigashi 17 p += n;
84     bufsiz -= n;
85     if( bufsiz <= 0 ) {
86     break;
87     }
88     }
89     close( data_socket );
90    
91     /*
92     * receive response.
93     */
94 hirohitohigashi 29 res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 );
95     if( res != 226 ) { /* 226: Closing data connection. */
96     DEBUGPRINT1( "got illegal response %d\n", res );
97     return res < 0? res: LIBOFTP_ERROR_PROTOCOL;
98 hirohitohigashi 17 }
99    
100 hirohitohigashi 29 return LIBOFTP_NOERROR;
101 hirohitohigashi 17 }
102 hirohitohigashi 28
103    
104    
105     /***** Global functions *****************************************************/
106    
107     /****************************************************************************/
108     /*! バッファからファイル送信
109     *
110     *@param ftp LIBOFTPへのポインタ。
111     *@param buf バッファへのポインタ
112     *@param bufsiz バッファサイズ
113     *@param fname サーバ上のファイル名
114 hirohitohigashi 30 *@retval int エラーコード
115 hirohitohigashi 28 *@note
116     */
117 hirohitohigashi 45 int ftp_put_buffer( LIBOFTP *ftp, const char *buf, int bufsiz, const char *fname )
118 hirohitohigashi 28 {
119     return ftp_put_buffer_main( ftp, buf, bufsiz, fname, "STOR" );
120     }
121    
122    
123    
124     /****************************************************************************/
125     /*! バッファからファイル送信 アペンドモード
126     *
127     *@param ftp LIBOFTPへのポインタ。
128     *@param buf バッファへのポインタ
129     *@param bufsiz バッファサイズ
130     *@param fname サーバ上のファイル名
131 hirohitohigashi 30 *@retval int エラーコード
132 hirohitohigashi 28 *@note
133     */
134 hirohitohigashi 45 int ftp_append_buffer( LIBOFTP *ftp, const char *buf, int bufsiz, const char *fname )
135 hirohitohigashi 28 {
136     return ftp_put_buffer_main( ftp, buf, bufsiz, fname, "APPE" );
137     }

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