Revisión | 44 (tree) |
---|---|
Tiempo | 2009-03-04 00:06:37 |
Autor | hirohitohigashi |
Merge r42 (RB-2.0)
@@ -1,74 +0,0 @@ | ||
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 | -/***** Local headers ********************************************************/ | |
13 | -#include "liboftp.h" | |
14 | -#include "sub.h" | |
15 | - | |
16 | - | |
17 | -/***** Constat values *******************************************************/ | |
18 | -/***** Macros ***************************************************************/ | |
19 | -/***** Typedefs *************************************************************/ | |
20 | -/***** Function prototypes **************************************************/ | |
21 | -/***** Local variables ******************************************************/ | |
22 | -/***** Global variables *****************************************************/ | |
23 | -/***** Signal catching functions ********************************************/ | |
24 | -/***** Local functions ******************************************************/ | |
25 | - | |
26 | -/***** Global functions *****************************************************/ | |
27 | - | |
28 | -/****************************************************************************/ | |
29 | -/*! ディレクトリリスト(LIST) 取得 | |
30 | - * | |
31 | - *@param ftp LIBOFTPへのポインタ。 | |
32 | - *@param fglob ファイルリストグロブ (ex: *.txt) or NULL | |
33 | - *@param buf バッファへのポインタ | |
34 | - *@param bufsiz バッファサイズ | |
35 | - *@retval int エラーコード | |
36 | - *@note | |
37 | - */ | |
38 | -int ftp_list( LIBOFTP *ftp, const char *fglob, char *buf, int bufsiz ) | |
39 | -{ | |
40 | - int ret; | |
41 | - | |
42 | - ret = ftp_get_buffer_main( ftp, "LIST", fglob, buf, bufsiz ); | |
43 | - if( ret > 0 ) { | |
44 | - buf[ret] = 0; | |
45 | - return LIBOFTP_NOERROR; | |
46 | - } | |
47 | - | |
48 | - return ret; | |
49 | -} | |
50 | - | |
51 | - | |
52 | - | |
53 | -/****************************************************************************/ | |
54 | -/*! ディレクトリリスト(NLST) 取得 | |
55 | - * | |
56 | - *@param ftp LIBOFTPへのポインタ。 | |
57 | - *@param fglob ファイルリストグロブ (ex: *.txt) or NULL | |
58 | - *@param buf バッファへのポインタ | |
59 | - *@param bufsiz バッファサイズ | |
60 | - *@retval int エラーコード | |
61 | - *@note | |
62 | - */ | |
63 | -int ftp_nlist( LIBOFTP *ftp, const char *fglob, char *buf, int bufsiz ) | |
64 | -{ | |
65 | - int ret; | |
66 | - | |
67 | - ret = ftp_get_buffer_main( ftp, "NLST", fglob, buf, bufsiz ); | |
68 | - if( ret > 0 ) { | |
69 | - buf[ret] = 0; | |
70 | - return LIBOFTP_NOERROR; | |
71 | - } | |
72 | - | |
73 | - return ret; | |
74 | -} |
@@ -49,7 +49,6 @@ | ||
49 | 49 | int ftp_receive_response( LIBOFTP *ftp, char *res_buf, int bufsiz ); |
50 | 50 | int ftp_getready_active( LIBOFTP *ftp, const char *cmd, const char *fname ); |
51 | 51 | int ftp_getready_pasv( LIBOFTP *ftp, const char *cmd, const char *fname ); |
52 | -int ftp_get_buffer_main( LIBOFTP *ftp, const char *cmd, const char *fname, char *buf, int bufsiz ); | |
53 | 52 | |
54 | 53 | |
55 | 54 | #if defined( __QNX__ ) && !defined( __QNXNTO__ ) |
@@ -9,6 +9,12 @@ | ||
9 | 9 | |
10 | 10 | /***** Feature test switches ************************************************/ |
11 | 11 | /***** System headers *******************************************************/ |
12 | +#include <unistd.h> | |
13 | +#include <string.h> | |
14 | +#include <sys/types.h> | |
15 | +#include <sys/socket.h> | |
16 | + | |
17 | + | |
12 | 18 | /***** Local headers ********************************************************/ |
13 | 19 | #include "liboftp.h" |
14 | 20 | #include "sub.h" |
@@ -15,6 +21,7 @@ | ||
15 | 21 | |
16 | 22 | |
17 | 23 | /***** Constat values *******************************************************/ |
24 | +#define TRANSFER_SEGMENT_SIZE 4096 /* 一度のrecv()で転送するバイト数 */ | |
18 | 25 | |
19 | 26 | |
20 | 27 | /***** Macros ***************************************************************/ |
@@ -24,6 +31,113 @@ | ||
24 | 31 | /***** Global variables *****************************************************/ |
25 | 32 | /***** Signal catching functions ********************************************/ |
26 | 33 | /***** Local functions ******************************************************/ |
34 | + | |
35 | +/****************************************************************************/ | |
36 | +/*! バッファへリスト取得 作業ルーチン | |
37 | + * | |
38 | + *@param ftp LIBOFTPへのポインタ。 | |
39 | + *@param cmd コマンド (RETR|LIST|NLST) | |
40 | + *@param fname ファイル名または、グロブ。 | |
41 | + *@param buf バッファへのポインタ | |
42 | + *@param bufsiz バッファサイズ | |
43 | + *@retval int 取得したバイト数 マイナス値ならエラーコード | |
44 | + *@note | |
45 | + */ | |
46 | +static int ftp_get_buffer_main( LIBOFTP *ftp, const char *cmd, const char *fname, char *buf, int bufsiz ) | |
47 | +{ | |
48 | + int data_socket; | |
49 | + char *p = buf; | |
50 | + int res; | |
51 | + | |
52 | + /* | |
53 | + * 受信準備 | |
54 | + */ | |
55 | + if( ftp->flag_passive ) { | |
56 | + data_socket = ftp_getready_pasv( ftp, cmd, fname ); | |
57 | + } else { | |
58 | + data_socket = ftp_getready_active( ftp, cmd, fname ); | |
59 | + } | |
60 | + if( data_socket < 0 ) { | |
61 | + return data_socket; | |
62 | + } | |
63 | + | |
64 | + /* | |
65 | + * タイムアウトが意図通りに働くように、分割してrecvする。 | |
66 | + */ | |
67 | + while( 1 ) { | |
68 | + int n; | |
69 | + int len = bufsiz; | |
70 | + if( len > TRANSFER_SEGMENT_SIZE ) { | |
71 | + len = TRANSFER_SEGMENT_SIZE; | |
72 | + } | |
73 | + n = recv( data_socket, p, len, 0 ); | |
74 | + DEBUGPRINT1( "RECV: n=%d\n", n ); | |
75 | + if( n < 0 ) { | |
76 | + int ret; | |
77 | + DEBUGPRINT1( "recv error. %s\n", strerror(errno) ); | |
78 | + if( errno == EAGAIN ) { | |
79 | + ret = LIBOFTP_ERROR_TIMEOUT; | |
80 | + } else { | |
81 | + ret = LIBOFTP_ERROR_OS; | |
82 | + copy_strerror(); | |
83 | + } | |
84 | + close( data_socket ); | |
85 | + return ret; | |
86 | + } | |
87 | + if( n == 0 ) { | |
88 | + break; | |
89 | + } | |
90 | + p += n; | |
91 | + bufsiz -= n; | |
92 | + if( bufsiz <= 0 ) { | |
93 | + break; /* buffer too small */ | |
94 | + } | |
95 | + } | |
96 | + | |
97 | + if( bufsiz > 0 ) { /* バッファ不足だったか? */ | |
98 | + /* | |
99 | + * 不足していない | |
100 | + */ | |
101 | + close( data_socket ); | |
102 | + | |
103 | + /* receive response. */ | |
104 | + res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 ); | |
105 | + if( res != 226 ) { /* 226: Closing data connection. */ | |
106 | + DEBUGPRINT1( "got illegal response %d\n", res ); | |
107 | + return res < 0? res: LIBOFTP_ERROR_PROTOCOL; | |
108 | + } | |
109 | + | |
110 | + return p - buf; /* return with transfered bytes */ | |
111 | + | |
112 | + } else { | |
113 | + /* | |
114 | + * バッファ不足時 | |
115 | + */ | |
116 | + DEBUGPRINT1( "buffer too small. %s\n", "" ); | |
117 | + strncpy( ftp->error_message, "buffer too small", sizeof( ftp->error_message ) - 1 ); | |
118 | + | |
119 | + if( ftp_send_command( ftp, "ABOR\r\n" ) < 0 ) { | |
120 | + DEBUGPRINT1( "command sending error. %s\n", "ABOR" ); | |
121 | + close( data_socket ); | |
122 | + return LIBOFTP_ERROR_BUFFER; | |
123 | + } | |
124 | + | |
125 | + res = ftp_receive_response( ftp, 0, 0 ); | |
126 | + if( res == 426 ) { /* 426: Connection closed; transfer aborted. */ | |
127 | + res = ftp_receive_response( ftp, 0, 0 ); | |
128 | + } | |
129 | + close( data_socket ); | |
130 | + if( res != 226 ) { /* 226: Closing data connection. */ | |
131 | + DEBUGPRINT1( "got illegal response %d\n", res ); | |
132 | + return res < 0? res: LIBOFTP_ERROR_PROTOCOL; | |
133 | + } | |
134 | + | |
135 | + return LIBOFTP_ERROR_BUFFER; | |
136 | + } | |
137 | +} | |
138 | + | |
139 | + | |
140 | + | |
27 | 141 | /***** Global functions *****************************************************/ |
28 | 142 | |
29 | 143 | /****************************************************************************/ |
@@ -40,3 +154,53 @@ | ||
40 | 154 | { |
41 | 155 | return ftp_get_buffer_main( ftp, "RETR", fname, buf, bufsiz ); |
42 | 156 | } |
157 | + | |
158 | + | |
159 | + | |
160 | +/****************************************************************************/ | |
161 | +/*! ディレクトリリスト(LIST) 取得 | |
162 | + * | |
163 | + *@param ftp LIBOFTPへのポインタ。 | |
164 | + *@param fglob ファイルリストグロブ (ex: *.txt) or NULL | |
165 | + *@param buf バッファへのポインタ | |
166 | + *@param bufsiz バッファサイズ | |
167 | + *@retval int エラーコード | |
168 | + *@note | |
169 | + */ | |
170 | +int ftp_list( LIBOFTP *ftp, const char *fglob, char *buf, int bufsiz ) | |
171 | +{ | |
172 | + int ret; | |
173 | + | |
174 | + ret = ftp_get_buffer_main( ftp, "LIST", fglob, buf, bufsiz ); | |
175 | + if( ret > 0 ) { | |
176 | + buf[ret] = 0; | |
177 | + return LIBOFTP_NOERROR; | |
178 | + } | |
179 | + | |
180 | + return ret; | |
181 | +} | |
182 | + | |
183 | + | |
184 | + | |
185 | +/****************************************************************************/ | |
186 | +/*! ディレクトリリスト(NLST) 取得 | |
187 | + * | |
188 | + *@param ftp LIBOFTPへのポインタ。 | |
189 | + *@param fglob ファイルリストグロブ (ex: *.txt) or NULL | |
190 | + *@param buf バッファへのポインタ | |
191 | + *@param bufsiz バッファサイズ | |
192 | + *@retval int エラーコード | |
193 | + *@note | |
194 | + */ | |
195 | +int ftp_nlist( LIBOFTP *ftp, const char *fglob, char *buf, int bufsiz ) | |
196 | +{ | |
197 | + int ret; | |
198 | + | |
199 | + ret = ftp_get_buffer_main( ftp, "NLST", fglob, buf, bufsiz ); | |
200 | + if( ret > 0 ) { | |
201 | + buf[ret] = 0; | |
202 | + return LIBOFTP_NOERROR; | |
203 | + } | |
204 | + | |
205 | + return ret; | |
206 | +} |
@@ -26,9 +26,6 @@ | ||
26 | 26 | |
27 | 27 | |
28 | 28 | /***** Constat values *******************************************************/ |
29 | -#define TRANSFER_SEGMENT_SIZE 4096 /* 一度のrecv()で転送するバイト数 */ | |
30 | - | |
31 | - | |
32 | 29 | /***** Macros ***************************************************************/ |
33 | 30 | /***** Typedefs *************************************************************/ |
34 | 31 | /***** Function prototypes **************************************************/ |
@@ -474,109 +471,3 @@ | ||
474 | 471 | return LIBOFTP_ERROR_PROTOCOL; |
475 | 472 | } |
476 | 473 | } |
477 | - | |
478 | - | |
479 | - | |
480 | -/****************************************************************************/ | |
481 | -/*! バッファへリスト取得 | |
482 | - * | |
483 | - *@param ftp LIBOFTPへのポインタ。 | |
484 | - *@param cmd コマンド (RETR|LIST|NLST) | |
485 | - *@param fname ファイル名または、グロブ。 | |
486 | - *@param buf バッファへのポインタ | |
487 | - *@param bufsiz バッファサイズ | |
488 | - *@retval int 取得したバイト数 マイナス値ならエラーコード | |
489 | - *@note | |
490 | - */ | |
491 | -int ftp_get_buffer_main( LIBOFTP *ftp, const char *cmd, const char *fname, char *buf, int bufsiz ) | |
492 | -{ | |
493 | - int data_socket; | |
494 | - char *p = buf; | |
495 | - int res; | |
496 | - | |
497 | - /* | |
498 | - * 受信準備 | |
499 | - */ | |
500 | - if( ftp->flag_passive ) { | |
501 | - data_socket = ftp_getready_pasv( ftp, cmd, fname ); | |
502 | - } else { | |
503 | - data_socket = ftp_getready_active( ftp, cmd, fname ); | |
504 | - } | |
505 | - if( data_socket < 0 ) { | |
506 | - return data_socket; | |
507 | - } | |
508 | - | |
509 | - /* | |
510 | - * タイムアウトが意図通りに働くように、分割してrecvする。 | |
511 | - */ | |
512 | - while( 1 ) { | |
513 | - int n; | |
514 | - int len = bufsiz; | |
515 | - if( len > TRANSFER_SEGMENT_SIZE ) { | |
516 | - len = TRANSFER_SEGMENT_SIZE; | |
517 | - } | |
518 | - n = recv( data_socket, p, len, 0 ); | |
519 | - DEBUGPRINT1( "RECV: n=%d\n", n ); | |
520 | - if( n < 0 ) { | |
521 | - int ret; | |
522 | - DEBUGPRINT1( "recv error. %s\n", strerror(errno) ); | |
523 | - if( errno == EAGAIN ) { | |
524 | - ret = LIBOFTP_ERROR_TIMEOUT; | |
525 | - } else { | |
526 | - ret = LIBOFTP_ERROR_OS; | |
527 | - copy_strerror(); | |
528 | - } | |
529 | - close( data_socket ); | |
530 | - return ret; | |
531 | - } | |
532 | - if( n == 0 ) { | |
533 | - break; | |
534 | - } | |
535 | - p += n; | |
536 | - bufsiz -= n; | |
537 | - if( bufsiz <= 0 ) { | |
538 | - break; /* buffer too small */ | |
539 | - } | |
540 | - } | |
541 | - | |
542 | - if( bufsiz > 0 ) { /* バッファ不足だったか? */ | |
543 | - /* | |
544 | - * 不足していない | |
545 | - */ | |
546 | - close( data_socket ); | |
547 | - | |
548 | - /* receive response. */ | |
549 | - res = ftp_receive_response( ftp, ftp->error_message, sizeof(ftp->error_message)-1 ); | |
550 | - if( res != 226 ) { /* 226: Closing data connection. */ | |
551 | - DEBUGPRINT1( "got illegal response %d\n", res ); | |
552 | - return res < 0? res: LIBOFTP_ERROR_PROTOCOL; | |
553 | - } | |
554 | - | |
555 | - return p - buf; /* return with transfered bytes */ | |
556 | - | |
557 | - } else { | |
558 | - /* | |
559 | - * バッファ不足時 | |
560 | - */ | |
561 | - DEBUGPRINT1( "buffer too small. %s\n", "" ); | |
562 | - strncpy( ftp->error_message, "buffer too small", sizeof( ftp->error_message ) - 1 ); | |
563 | - | |
564 | - if( ftp_send_command( ftp, "ABOR\r\n" ) < 0 ) { | |
565 | - DEBUGPRINT1( "command sending error. %s\n", "ABOR" ); | |
566 | - close( data_socket ); | |
567 | - return LIBOFTP_ERROR_BUFFER; | |
568 | - } | |
569 | - | |
570 | - res = ftp_receive_response( ftp, 0, 0 ); | |
571 | - if( res == 426 ) { /* 426: Connection closed; transfer aborted. */ | |
572 | - res = ftp_receive_response( ftp, 0, 0 ); | |
573 | - } | |
574 | - close( data_socket ); | |
575 | - if( res != 226 ) { /* 226: Closing data connection. */ | |
576 | - DEBUGPRINT1( "got illegal response %d\n", res ); | |
577 | - return res < 0? res: LIBOFTP_ERROR_PROTOCOL; | |
578 | - } | |
579 | - | |
580 | - return LIBOFTP_ERROR_BUFFER; | |
581 | - } | |
582 | -} |
@@ -12,7 +12,7 @@ | ||
12 | 12 | CFLAGS = -Wall -O2 |
13 | 13 | ARFLAGS = -rs |
14 | 14 | SRCS = sub.c ftp_open.c ftp_user.c ftp_passive.c ftp_timeout.c ftp_type.c ftp_quit.c \ |
15 | - ftp_site.c ftp_delete.c ftp_rename.c ftp_list.c ftp_reset.c \ | |
15 | + ftp_site.c ftp_delete.c ftp_rename.c ftp_reset.c \ | |
16 | 16 | ftp_get_buffer.c ftp_put_buffer.c ftp_get_file.c ftp_put_file.c \ |
17 | 17 | ftp_get_descriptor.c ftp_put_descriptor.c \ |
18 | 18 | ftp_mkdir.c ftp_rmdir.c ftp_pwd.c ftp_cd.c |