Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/ftp_put_file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 28 - (show 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: 4276 byte(s)
marged ftp_append_buffer.c and ftp_put_buffer.c. added ftp_append_file() function.

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

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