Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/ftp_put_buffer.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 50 - (show annotations) (download) (as text)
Wed Dec 2 15:13:30 2009 UTC (14 years, 3 months ago) by hirohitohigashi
File MIME type: text/x-csrc
File size: 4105 byte(s)
add error check
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
34
35 /****************************************************************************/
36 /*! バッファからファイル送信
37 *
38 *@param ftp LIBOFTPへのポインタ。
39 *@param buf バッファへのポインタ
40 *@param bufsiz バッファサイズ
41 *@param fname サーバ上のファイル名
42 *@param cmd 送信するFTPコマンド
43 *@retval int エラーコード
44 *@note
45 */
46 static int ftp_put_buffer_main( LIBOFTP *ftp, const char *buf, int bufsiz, const char *fname, const char *cmd )
47 {
48 int data_socket;
49 const char *p = buf;
50 int n, res;
51
52 if( ftp->socket < 0 ) return LIBOFTP_ERROR;
53
54 /*
55 * 送信準備
56 */
57 if( ftp->flag_passive ) {
58 data_socket = ftp_getready_pasv( ftp, cmd, fname );
59 } else {
60 data_socket = ftp_getready_active( ftp, cmd, fname );
61 }
62 if( data_socket < 0 ) {
63 return data_socket;
64 }
65
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 DEBUGPRINT1( "recv error. %s\n", strerror(errno) );
78 copy_strerror();
79 close( data_socket );
80 return LIBOFTP_ERROR_OS;
81 }
82
83 p += n;
84 bufsiz -= n;
85 if( bufsiz <= 0 ) {
86 break;
87 }
88 }
89 close( data_socket );
90
91 /*
92 * receive response.
93 */
94 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 }
99
100 return LIBOFTP_NOERROR;
101 }
102
103
104
105 /***** Global functions *****************************************************/
106
107 /****************************************************************************/
108 /*! バッファからファイル送信
109 *
110 *@param ftp LIBOFTPへのポインタ。
111 *@param buf バッファへのポインタ
112 *@param bufsiz バッファサイズ
113 *@param fname サーバ上のファイル名
114 *@retval int エラーコード
115 *@note
116 */
117 int ftp_put_buffer( LIBOFTP *ftp, const char *buf, int bufsiz, const char *fname )
118 {
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 *@retval int エラーコード
132 *@note
133 */
134 int ftp_append_buffer( LIBOFTP *ftp, const char *buf, int bufsiz, const char *fname )
135 {
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