Mercurial > gftp.yaz
annotate lib/protocols.c @ 168:c505d9ba9d53
2003-6-6 Brian Masney <masneyb@gftp.org>
* lib/gftp.h - if USE_SSL is defined, include the OpenSSL headers.
Added read_function, write_function and post_connect function pointers
to gftp_request structure. Added SSL object to gftp_request structure
if USE_SSL is defined. Added protocol number and init function
declarations for the HTTPS protocol
* lib/options.h - added HTTPS to the list of supported protocols
* lib/protocols.c lib/cache.c lib/rfc2068.c lib/rfc959.c lib/sshv2.c -
renamed gftp_read(), gftp_write() and gftp_set_sockblocking() to
gftp_fd_read(), gftp_fd_write() and gftp_fd_set_sockblocking()
respectively
* lib/bookmark.c lib/local.c
* lib/misc.c lib/rfc2068.c - moved base64_encode() to misc.c
* lib/protocols.c - improved parsing of URLs. Rather than calling
gftp_read() or gftp_write() directly, call the read_function or
write_function that is set in the request structure. Expanded tabs
to spaces. Cleanup for parsing of timestamps. In
gftp_connect_server(), if a post_connect function pointer is set,
call it after we are connected to the server. Improvements to
gftp_get_line ().
* lib/httpcommon.h lib/rfc2068.c - moved rfc2068_params structure to
httpcommon.h. Fix for chunked file transfers, they were not handled
at all before. Made the I/O calls a little more generic so that we can
read from either a socket or a SSL connection.
* lib/sslcommon.c - added generic SSL layer
* lib/https.c - added support for the HTTPS protocol. It piggy backs
off of the existing HTTP support and uses the generic SSL layer
* src/gtk/bookmarks.c src/gtk/chmod_dialog.c src/gtk/gftp-gtk.c
src/gtk/menu-items.c src/gtk/misc-gtk.c src/gtk/options_dialog.c
src/gtk/view_dialog.c - set the window icon name to the gFTP <version>
* configure.in - added lib back to SUBDIRS (oops)
* lib/Makefile.am - added https.c, sslcommon.c and httpcommon.h
author | masneyb |
---|---|
date | Sun, 08 Jun 2003 15:04:40 +0000 |
parents | 2f15b3000dbc |
children | d40f9db52cdf |
rev | line source |
---|---|
1 | 1 /*****************************************************************************/ |
2 /* protocols.c - Skeleton functions for the protocols gftp supports */ | |
122 | 3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */ |
1 | 4 /* */ |
5 /* This program is free software; you can redistribute it and/or modify */ | |
6 /* it under the terms of the GNU General Public License as published by */ | |
7 /* the Free Software Foundation; either version 2 of the License, or */ | |
8 /* (at your option) any later version. */ | |
9 /* */ | |
10 /* This program is distributed in the hope that it will be useful, */ | |
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | |
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | |
13 /* GNU General Public License for more details. */ | |
14 /* */ | |
15 /* You should have received a copy of the GNU General Public License */ | |
16 /* along with this program; if not, write to the Free Software */ | |
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */ | |
18 /*****************************************************************************/ | |
19 | |
20 #include "gftp.h" | |
33 | 21 static const char cvsid[] = "$Id$"; |
1 | 22 |
23 gftp_request * | |
24 gftp_request_new (void) | |
25 { | |
26 gftp_request *request; | |
27 | |
28 request = g_malloc0 (sizeof (*request)); | |
58 | 29 request->sockfd = -1; |
66 | 30 request->datafd = -1; |
31 request->cachefd = -1; | |
122 | 32 request->server_type = GFTP_DIRTYPE_OTHER; |
1 | 33 return (request); |
34 } | |
136 | 35 |
1 | 36 |
37 void | |
67 | 38 gftp_request_destroy (gftp_request * request, int free_request) |
1 | 39 { |
40 g_return_if_fail (request != NULL); | |
41 | |
42 gftp_disconnect (request); | |
43 | |
44 if (request->destroy != NULL) | |
45 request->destroy (request); | |
46 | |
47 if (request->hostname) | |
48 g_free (request->hostname); | |
49 if (request->username) | |
50 g_free (request->username); | |
51 if (request->password) | |
52 { | |
53 memset (request->password, 0, strlen (request->password)); | |
54 g_free (request->password); | |
55 } | |
56 if (request->account) | |
57 { | |
58 memset (request->account, 0, strlen (request->account)); | |
59 g_free (request->account); | |
60 } | |
61 if (request->directory) | |
62 g_free (request->directory); | |
63 if (request->last_ftp_response) | |
64 g_free (request->last_ftp_response); | |
65 if (request->protocol_data) | |
66 g_free (request->protocol_data); | |
67 | 67 |
1 | 68 memset (request, 0, sizeof (*request)); |
67 | 69 |
70 if (free_request) | |
71 g_free (request); | |
72 else | |
73 { | |
74 request->sockfd = -1; | |
75 request->datafd = -1; | |
76 request->cachefd = -1; | |
77 } | |
136 | 78 |
143 | 79 if (request->local_options_vars != NULL) |
80 { | |
81 g_free (request->local_options_vars); | |
82 g_hash_table_destroy (request->local_options_hash); | |
83 } | |
1 | 84 } |
85 | |
86 | |
87 void | |
88 gftp_file_destroy (gftp_file * file) | |
89 { | |
90 g_return_if_fail (file != NULL); | |
91 | |
92 if (file->file) | |
93 g_free (file->file); | |
94 if (file->user) | |
95 g_free (file->user); | |
96 if (file->group) | |
97 g_free (file->group); | |
98 if (file->attribs) | |
99 g_free (file->attribs); | |
100 if (file->destfile) | |
101 g_free (file->destfile); | |
102 memset (file, 0, sizeof (*file)); | |
103 } | |
104 | |
105 | |
106 int | |
107 gftp_connect (gftp_request * request) | |
108 { | |
84 | 109 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 110 |
111 if (request->connect == NULL) | |
84 | 112 return (GFTP_EFATAL); |
1 | 113 |
114 gftp_set_config_options (request); | |
115 | |
116 return (request->connect (request)); | |
117 } | |
118 | |
119 | |
120 void | |
121 gftp_disconnect (gftp_request * request) | |
122 { | |
123 g_return_if_fail (request != NULL); | |
124 | |
125 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
151 | 126 if (request->free_hostp && request->hostp != NULL) |
1 | 127 freeaddrinfo (request->hostp); |
128 #endif | |
129 request->hostp = NULL; | |
130 | |
168 | 131 #ifdef USE_SSL |
132 if (request->ssl != NULL) | |
133 { | |
134 SSL_free (request->ssl); | |
135 request->ssl = NULL; | |
136 } | |
137 #endif | |
138 | |
1 | 139 request->cached = 0; |
140 if (request->disconnect == NULL) | |
141 return; | |
142 request->disconnect (request); | |
143 } | |
144 | |
145 | |
58 | 146 off_t |
147 gftp_get_file (gftp_request * request, const char *filename, int fd, | |
1 | 148 size_t startsize) |
149 { | |
122 | 150 float maxkbs; |
151 | |
84 | 152 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 153 |
122 | 154 gftp_lookup_request_option (request, "maxkbs", &maxkbs); |
155 if (maxkbs > 0) | |
156 { | |
157 request->logging_function (gftp_logging_misc, request->user_data, | |
158 _("File transfer will be throttled to %.2f KB/s\n"), | |
159 maxkbs); | |
160 } | |
161 | |
1 | 162 request->cached = 0; |
163 if (request->get_file == NULL) | |
84 | 164 return (GFTP_EFATAL); |
1 | 165 return (request->get_file (request, filename, fd, startsize)); |
166 } | |
167 | |
168 | |
169 int | |
58 | 170 gftp_put_file (gftp_request * request, const char *filename, int fd, |
1 | 171 size_t startsize, size_t totalsize) |
172 { | |
122 | 173 float maxkbs; |
174 | |
84 | 175 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 176 |
122 | 177 gftp_lookup_request_option (request, "maxkbs", &maxkbs); |
178 | |
179 if (maxkbs > 0) | |
180 { | |
181 request->logging_function (gftp_logging_misc, request->user_data, | |
182 _("File transfer will be throttled to %.2f KB/s\n"), | |
183 maxkbs); | |
184 } | |
185 | |
1 | 186 request->cached = 0; |
187 if (request->put_file == NULL) | |
84 | 188 return (GFTP_EFATAL); |
1 | 189 return (request->put_file (request, filename, fd, startsize, totalsize)); |
190 } | |
191 | |
192 | |
193 long | |
194 gftp_transfer_file (gftp_request * fromreq, const char *fromfile, | |
58 | 195 int fromfd, size_t fromsize, |
1 | 196 gftp_request * toreq, const char *tofile, |
58 | 197 int tofd, size_t tosize) |
1 | 198 { |
199 long size; | |
84 | 200 int ret; |
1 | 201 |
84 | 202 g_return_val_if_fail (fromreq != NULL, GFTP_EFATAL); |
203 g_return_val_if_fail (fromfile != NULL, GFTP_EFATAL); | |
204 g_return_val_if_fail (toreq != NULL, GFTP_EFATAL); | |
205 g_return_val_if_fail (tofile != NULL, GFTP_EFATAL); | |
206 | |
87 | 207 if (fromreq->protonum == toreq->protonum && |
84 | 208 fromreq->transfer_file != NULL) |
209 return (fromreq->transfer_file (fromreq, fromfile, fromsize, toreq, | |
210 tofile, tosize)); | |
1 | 211 |
212 fromreq->cached = 0; | |
213 toreq->cached = 0; | |
214 if ((size = gftp_get_file (fromreq, fromfile, fromfd, tosize)) < 0) | |
84 | 215 return (size); |
1 | 216 |
84 | 217 if ((ret = gftp_put_file (toreq, tofile, tofd, tosize, size)) != 0) |
1 | 218 { |
40 | 219 if (gftp_abort_transfer (fromreq) != 0) |
220 gftp_end_transfer (fromreq); | |
221 | |
84 | 222 return (ret); |
1 | 223 } |
224 | |
225 return (size); | |
226 } | |
227 | |
228 | |
58 | 229 ssize_t |
1 | 230 gftp_get_next_file_chunk (gftp_request * request, char *buf, size_t size) |
231 { | |
84 | 232 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
233 g_return_val_if_fail (buf != NULL, GFTP_EFATAL); | |
1 | 234 |
235 if (request->get_next_file_chunk != NULL) | |
236 return (request->get_next_file_chunk (request, buf, size)); | |
237 | |
168 | 238 return (request->read_function (request, buf, size, request->datafd)); |
1 | 239 } |
240 | |
241 | |
58 | 242 ssize_t |
1 | 243 gftp_put_next_file_chunk (gftp_request * request, char *buf, size_t size) |
244 { | |
84 | 245 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
246 g_return_val_if_fail (buf != NULL, GFTP_EFATAL); | |
1 | 247 |
248 if (request->put_next_file_chunk != NULL) | |
249 return (request->put_next_file_chunk (request, buf, size)); | |
250 | |
251 if (size == 0) | |
252 return (0); | |
253 | |
168 | 254 return (request->write_function (request, buf, size, request->datafd)); |
1 | 255 } |
256 | |
257 | |
258 int | |
259 gftp_end_transfer (gftp_request * request) | |
260 { | |
261 int ret; | |
262 | |
84 | 263 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 264 |
40 | 265 if (!request->cached && |
266 request->end_transfer != NULL) | |
267 ret = request->end_transfer (request); | |
268 else | |
269 ret = 0; | |
1 | 270 |
58 | 271 if (request->cachefd > 0) |
1 | 272 { |
58 | 273 close (request->cachefd); |
274 request->cachefd = -1; | |
1 | 275 } |
276 | |
277 if (request->last_dir_entry) | |
278 { | |
279 g_free (request->last_dir_entry); | |
280 request->last_dir_entry = NULL; | |
281 request->last_dir_entry_len = 0; | |
282 } | |
283 | |
284 return (ret); | |
285 } | |
286 | |
287 | |
288 int | |
40 | 289 gftp_abort_transfer (gftp_request * request) |
290 { | |
84 | 291 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
40 | 292 |
293 if (request->abort_transfer == NULL) | |
84 | 294 return (GFTP_EFATAL); |
40 | 295 |
296 return (request->abort_transfer (request)); | |
297 } | |
298 | |
299 | |
300 int | |
1 | 301 gftp_list_files (gftp_request * request) |
302 { | |
58 | 303 int fd; |
1 | 304 |
84 | 305 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 306 |
307 request->cached = 0; | |
58 | 308 if (request->use_cache && (fd = gftp_find_cache_entry (request)) > 0) |
1 | 309 { |
310 request->logging_function (gftp_logging_misc, request->user_data, | |
311 _("Loading directory listing %s from cache\n"), | |
312 request->directory); | |
313 | |
314 request->cachefd = fd; | |
315 request->cached = 1; | |
316 return (0); | |
317 } | |
318 else if (request->use_cache) | |
319 { | |
320 request->cachefd = gftp_new_cache_entry (request); | |
321 request->cached = 0; | |
322 } | |
323 | |
324 if (request->list_files == NULL) | |
84 | 325 return (GFTP_EFATAL); |
1 | 326 return (request->list_files (request)); |
327 } | |
328 | |
329 | |
330 int | |
331 gftp_get_next_file (gftp_request * request, char *filespec, gftp_file * fle) | |
332 { | |
58 | 333 int fd, ret; |
45 | 334 #if GLIB_MAJOR_VERSION > 1 |
335 gsize bread, bwrite; | |
336 char *tempstr; | |
46 | 337 GError * error; |
45 | 338 #endif |
1 | 339 |
84 | 340 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 341 |
342 if (request->get_next_file == NULL) | |
84 | 343 return (GFTP_EFATAL); |
1 | 344 |
58 | 345 if (request->cached && request->cachefd > 0) |
1 | 346 fd = request->cachefd; |
347 else | |
348 fd = request->datafd; | |
349 | |
350 memset (fle, 0, sizeof (*fle)); | |
351 do | |
352 { | |
353 gftp_file_destroy (fle); | |
354 ret = request->get_next_file (request, fle, fd); | |
355 | |
45 | 356 #if GLIB_MAJOR_VERSION > 1 |
102 | 357 if (ret >= 0 && fle->file != NULL && !g_utf8_validate (fle->file, -1, NULL)) |
45 | 358 { |
46 | 359 error = NULL; |
45 | 360 if ((tempstr = g_locale_to_utf8 (fle->file, -1, &bread, |
46 | 361 &bwrite, &error)) != NULL) |
45 | 362 { |
363 g_free (fle->file); | |
364 fle->file = tempstr; | |
365 } | |
46 | 366 else |
367 g_warning ("Error when converting %s to UTF-8: %s\n", fle->file, | |
368 error->message); | |
45 | 369 } |
370 #endif | |
371 | |
60 | 372 if (ret >= 0 && !request->cached && request->cachefd > 0 && |
1 | 373 request->last_dir_entry != NULL) |
374 { | |
168 | 375 if (gftp_fd_write (request, request->last_dir_entry, |
60 | 376 request->last_dir_entry_len, request->cachefd) < 0) |
1 | 377 { |
378 request->logging_function (gftp_logging_error, request->user_data, | |
379 _("Error: Cannot write to cache: %s\n"), | |
380 g_strerror (errno)); | |
60 | 381 close (request->cachefd); |
382 request->cachefd = -1; | |
1 | 383 } |
384 } | |
385 } while (ret > 0 && !gftp_match_filespec (fle->file, filespec)); | |
386 | |
387 return (ret); | |
388 } | |
389 | |
390 | |
391 int | |
87 | 392 gftp_parse_bookmark (gftp_request * request, const char * bookmark) |
393 { | |
394 gftp_logging_func logging_function; | |
122 | 395 gftp_bookmarks_var * tempentry; |
396 char *default_protocol; | |
87 | 397 int i; |
398 | |
399 g_return_val_if_fail (request != NULL, GFTP_EFATAL); | |
400 g_return_val_if_fail (bookmark != NULL, GFTP_EFATAL); | |
401 | |
402 logging_function = request->logging_function; | |
403 gftp_request_destroy (request, 0); | |
404 request->logging_function = logging_function; | |
405 | |
122 | 406 if ((tempentry = g_hash_table_lookup (gftp_bookmarks_htable, |
407 bookmark)) == NULL) | |
87 | 408 { |
409 request->logging_function (gftp_logging_error, request->user_data, | |
410 _("Error: Could not find bookmark %s\n"), | |
411 bookmark); | |
412 return (GFTP_EFATAL); | |
413 } | |
414 else if (tempentry->hostname == NULL || *tempentry->hostname == '\0') | |
415 { | |
416 request->logging_function (gftp_logging_error, request->user_data, | |
417 _("Bookmarks Error: The bookmark entry %s does not have a hostname\n"), bookmark); | |
418 return (GFTP_EFATAL); | |
419 } | |
420 | |
421 if (tempentry->user != NULL) | |
422 gftp_set_username (request, tempentry->user); | |
423 | |
424 if (tempentry->pass != NULL) | |
122 | 425 gftp_set_password (request, tempentry->pass); |
87 | 426 |
427 if (tempentry->acct != NULL) | |
428 gftp_set_account (request, tempentry->acct); | |
429 | |
430 gftp_set_hostname (request, tempentry->hostname); | |
431 gftp_set_directory (request, tempentry->remote_dir); | |
432 gftp_set_port (request, tempentry->port); | |
433 | |
434 for (i = 0; gftp_protocols[i].name; i++) | |
435 { | |
436 if (strcmp (gftp_protocols[i].name, tempentry->protocol) == 0) | |
437 { | |
438 gftp_protocols[i].init (request); | |
439 break; | |
440 } | |
441 } | |
442 | |
122 | 443 if (gftp_protocols[i].name == NULL) |
87 | 444 { |
122 | 445 gftp_lookup_request_option (request, "default_protocol", |
446 &default_protocol); | |
447 | |
125 | 448 if (default_protocol != NULL && *default_protocol != '\0') |
87 | 449 { |
122 | 450 for (i = 0; gftp_protocols[i].url_prefix; i++) |
451 { | |
452 if (strcmp (gftp_protocols[i].name, default_protocol) == 0) | |
453 break; | |
454 } | |
87 | 455 } |
456 | |
457 if (gftp_protocols[i].url_prefix == NULL) | |
458 i = GFTP_FTP_NUM; | |
459 } | |
460 | |
461 gftp_protocols[i].init (request); | |
462 return (0); | |
463 } | |
464 | |
465 | |
466 int | |
1 | 467 gftp_parse_url (gftp_request * request, const char *url) |
468 { | |
168 | 469 char *pos, *endpos, *endhostpos, tempchar, *default_protocol, *stpos; |
67 | 470 gftp_logging_func logging_function; |
168 | 471 const char *cpos; |
1 | 472 int len, i; |
473 | |
84 | 474 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
475 g_return_val_if_fail (url != NULL, GFTP_EFATAL); | |
1 | 476 |
67 | 477 logging_function = request->logging_function; |
478 gftp_request_destroy (request, 0); | |
479 request->logging_function = logging_function; | |
480 | |
168 | 481 for (cpos = url; *cpos == ' '; cpos++); |
482 stpos = g_strdup (cpos); | |
483 for (pos = stpos + strlen (stpos) - 1; *pos == ' '; pos--) | |
484 *pos = '\0'; | |
1 | 485 |
122 | 486 i = GFTP_FTP_NUM; |
487 | |
1 | 488 if ((pos = strstr (stpos, "://")) != NULL) |
489 { | |
490 *pos = '\0'; | |
491 | |
492 for (i = 0; gftp_protocols[i].url_prefix; i++) | |
493 { | |
494 if (strcmp (gftp_protocols[i].url_prefix, stpos) == 0) | |
495 break; | |
496 } | |
497 | |
168 | 498 *pos = ':'; |
499 | |
1 | 500 if (gftp_protocols[i].url_prefix == NULL) |
168 | 501 { |
502 g_free (stpos); | |
503 return (GFTP_EFATAL); | |
504 } | |
1 | 505 } |
506 else | |
507 { | |
122 | 508 gftp_lookup_request_option (request, "default_protocol", |
509 &default_protocol); | |
510 | |
125 | 511 if (default_protocol != NULL && *default_protocol != '\0') |
1 | 512 { |
122 | 513 for (i = 0; gftp_protocols[i].url_prefix; i++) |
514 { | |
515 if (strcmp (gftp_protocols[i].name, default_protocol) == 0) | |
516 break; | |
517 } | |
1 | 518 } |
122 | 519 } |
1 | 520 |
122 | 521 if (gftp_protocols[i].url_prefix == NULL) |
522 i = GFTP_FTP_NUM; | |
1 | 523 |
524 gftp_protocols[i].init (request); | |
525 | |
526 if (request->parse_url != NULL) | |
168 | 527 { |
528 g_free (stpos); | |
529 return (request->parse_url (request, url)); | |
530 } | |
531 | |
532 pos = stpos; | |
533 len = strlen (request->url_prefix); | |
534 if (strncmp (pos, request->url_prefix, len) == 0 && | |
535 strncmp (pos + len, "://", 3) == 0) | |
536 pos += len + 3; | |
1 | 537 |
538 if (i == GFTP_LOCAL_NUM) | |
539 { | |
168 | 540 request->directory = g_strdup (pos); |
541 g_free (stpos); | |
1 | 542 return (0); |
543 } | |
544 | |
545 if ((endhostpos = strchr (pos, '@')) != NULL) | |
546 { | |
547 /* A user/password was entered */ | |
548 if ((endpos = strchr (pos, ':')) == NULL || endhostpos < endpos) | |
168 | 549 { |
550 /* No password was entered */ | |
551 gftp_set_password (request, ""); | |
552 endpos = endhostpos; | |
553 } | |
1 | 554 |
555 *endpos = '\0'; | |
556 gftp_set_username (request, pos); | |
557 | |
558 pos = endpos + 1; | |
559 if ((endpos = strchr (pos, '@')) != NULL) | |
168 | 560 { |
561 if (strchr (endpos + 1, '@') != NULL) | |
562 endpos = strchr (endpos + 1, '@'); | |
563 *endpos = '\0'; | |
564 gftp_set_password (request, pos); | |
1 | 565 |
168 | 566 pos = endpos + 1; |
567 } | |
1 | 568 } |
569 | |
570 /* Now get the hostname and an optional port and optional directory */ | |
571 endhostpos = pos + strlen (pos); | |
572 if ((endpos = strchr (pos, ':')) != NULL) | |
573 endhostpos = endpos; | |
574 else if ((endpos = strchr (pos, '/')) != NULL) | |
575 endhostpos = endpos; | |
576 tempchar = *endhostpos; | |
577 *endhostpos = '\0'; | |
578 gftp_set_hostname (request, pos); | |
579 *endhostpos = tempchar; | |
580 | |
581 if ((endpos = strchr (pos, ':')) != NULL) | |
582 { | |
583 /* A port was entered */ | |
584 pos = endpos + 1; | |
585 gftp_set_port (request, strtol (pos, NULL, 10)); | |
586 } | |
587 if ((endpos = strchr (pos, '/')) != NULL) | |
588 gftp_set_directory (request, endpos); | |
168 | 589 g_free (stpos); |
1 | 590 return (0); |
591 } | |
592 | |
593 | |
594 void | |
595 gftp_set_hostname (gftp_request * request, const char *hostname) | |
596 { | |
597 g_return_if_fail (request != NULL); | |
598 g_return_if_fail (hostname != NULL); | |
599 | |
600 if (request->hostname) | |
601 g_free (request->hostname); | |
124 | 602 request->hostname = g_strdup (hostname); |
1 | 603 } |
604 | |
605 | |
606 void | |
607 gftp_set_username (gftp_request * request, const char *username) | |
608 { | |
609 g_return_if_fail (request != NULL); | |
610 g_return_if_fail (username != NULL); | |
611 | |
612 if (request->username) | |
613 g_free (request->username); | |
124 | 614 request->username = g_strdup (username); |
1 | 615 } |
616 | |
617 | |
618 void | |
619 gftp_set_password (gftp_request * request, const char *password) | |
620 { | |
621 g_return_if_fail (request != NULL); | |
622 g_return_if_fail (password != NULL); | |
623 | |
624 if (request->password) | |
625 g_free (request->password); | |
124 | 626 request->password = g_strdup (password); |
1 | 627 } |
628 | |
629 | |
630 void | |
631 gftp_set_account (gftp_request * request, const char *account) | |
632 { | |
633 g_return_if_fail (request != NULL); | |
634 g_return_if_fail (account != NULL); | |
635 | |
636 if (request->account) | |
637 g_free (request->account); | |
124 | 638 request->account = g_strdup (account); |
1 | 639 } |
640 | |
641 | |
642 int | |
643 gftp_set_directory (gftp_request * request, const char *directory) | |
644 { | |
84 | 645 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
646 g_return_val_if_fail (directory != NULL, GFTP_EFATAL); | |
1 | 647 |
648 | |
58 | 649 if (request->sockfd <= 0 && !request->always_connected) |
1 | 650 { |
651 if (directory != request->directory) | |
168 | 652 { |
653 if (request->directory) | |
654 g_free (request->directory); | |
655 request->directory = g_strdup (directory); | |
656 } | |
1 | 657 return (0); |
658 } | |
659 else if (request->chdir == NULL) | |
84 | 660 return (GFTP_EFATAL); |
1 | 661 return (request->chdir (request, directory)); |
662 } | |
663 | |
664 | |
665 void | |
666 gftp_set_port (gftp_request * request, unsigned int port) | |
667 { | |
668 g_return_if_fail (request != NULL); | |
669 | |
670 request->port = port; | |
671 } | |
672 | |
673 | |
674 int | |
675 gftp_remove_directory (gftp_request * request, const char *directory) | |
676 { | |
84 | 677 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 678 |
679 if (request->rmdir == NULL) | |
84 | 680 return (GFTP_EFATAL); |
1 | 681 return (request->rmdir (request, directory)); |
682 } | |
683 | |
684 | |
685 int | |
686 gftp_remove_file (gftp_request * request, const char *file) | |
687 { | |
84 | 688 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 689 |
690 if (request->rmfile == NULL) | |
84 | 691 return (GFTP_EFATAL); |
1 | 692 return (request->rmfile (request, file)); |
693 } | |
694 | |
695 | |
696 int | |
697 gftp_make_directory (gftp_request * request, const char *directory) | |
698 { | |
84 | 699 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 700 |
701 if (request->mkdir == NULL) | |
84 | 702 return (GFTP_EFATAL); |
1 | 703 return (request->mkdir (request, directory)); |
704 } | |
705 | |
706 | |
707 int | |
708 gftp_rename_file (gftp_request * request, const char *oldname, | |
168 | 709 const char *newname) |
1 | 710 { |
84 | 711 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 712 |
713 if (request->rename == NULL) | |
84 | 714 return (GFTP_EFATAL); |
1 | 715 return (request->rename (request, oldname, newname)); |
716 } | |
717 | |
718 | |
719 int | |
720 gftp_chmod (gftp_request * request, const char *file, int mode) | |
721 { | |
84 | 722 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 723 |
724 if (request->chmod == NULL) | |
84 | 725 return (GFTP_EFATAL); |
1 | 726 return (request->chmod (request, file, mode)); |
727 } | |
728 | |
729 | |
730 int | |
731 gftp_set_file_time (gftp_request * request, const char *file, time_t datetime) | |
732 { | |
84 | 733 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 734 |
735 if (request->set_file_time == NULL) | |
84 | 736 return (GFTP_EFATAL); |
1 | 737 return (request->set_file_time (request, file, datetime)); |
738 } | |
739 | |
740 | |
741 char | |
742 gftp_site_cmd (gftp_request * request, const char *command) | |
743 { | |
84 | 744 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 745 |
746 if (request->site == NULL) | |
84 | 747 return (GFTP_EFATAL); |
1 | 748 return (request->site (request, command)); |
749 } | |
750 | |
751 | |
58 | 752 off_t |
1 | 753 gftp_get_file_size (gftp_request * request, const char *filename) |
754 { | |
755 g_return_val_if_fail (request != NULL, 0); | |
756 | |
757 if (request->get_file_size == NULL) | |
758 return (0); | |
759 return (request->get_file_size (request, filename)); | |
760 } | |
761 | |
762 | |
122 | 763 static int |
764 gftp_need_proxy (gftp_request * request, char *service, char *proxy_hostname, | |
765 int proxy_port) | |
1 | 766 { |
122 | 767 gftp_config_list_vars * proxy_hosts; |
1 | 768 gftp_proxy_hosts * hostname; |
769 unsigned char addy[4]; | |
770 struct sockaddr *addr; | |
771 GList * templist; | |
772 gint32 netaddr; | |
773 char *pos; | |
774 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
775 struct addrinfo hints; | |
776 int port, errnum; | |
777 char serv[8]; | |
122 | 778 #endif |
779 | |
780 gftp_lookup_global_option ("ext", &proxy_hosts); | |
781 | |
782 if (proxy_hostname == NULL || *proxy_hostname == '\0') | |
783 return (0); | |
784 else if (proxy_hosts->list == NULL) | |
785 return (proxy_hostname != NULL && | |
786 *proxy_hostname != '\0'); | |
1 | 787 |
788 request->hostp = NULL; | |
122 | 789 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) |
151 | 790 request->free_hostp = 1; |
1 | 791 memset (&hints, 0, sizeof (hints)); |
792 hints.ai_flags = AI_CANONNAME; | |
146 | 793 hints.ai_family = PF_UNSPEC; |
1 | 794 hints.ai_socktype = SOCK_STREAM; |
795 | |
122 | 796 port = request->use_proxy ? proxy_port : request->port; |
1 | 797 if (port == 0) |
798 strcpy (serv, service); | |
799 else | |
800 snprintf (serv, sizeof (serv), "%d", port); | |
801 | |
802 request->logging_function (gftp_logging_misc, request->user_data, | |
803 _("Looking up %s\n"), request->hostname); | |
804 | |
805 if ((errnum = getaddrinfo (request->hostname, serv, &hints, | |
806 &request->hostp)) != 0) | |
807 { | |
808 request->logging_function (gftp_logging_error, request->user_data, | |
809 _("Cannot look up hostname %s: %s\n"), | |
810 request->hostname, gai_strerror (errnum)); | |
84 | 811 return (GFTP_ERETRYABLE); |
1 | 812 } |
813 | |
814 addr = request->hostp->ai_addr; | |
815 | |
816 #else /* !HAVE_GETADDRINFO */ | |
817 request->logging_function (gftp_logging_misc, request->user_data, | |
818 _("Looking up %s\n"), request->hostname); | |
819 | |
820 if (!(request->hostp = r_gethostbyname (request->hostname, &request->host, | |
821 NULL))) | |
822 { | |
823 request->logging_function (gftp_logging_error, request->user_data, | |
824 _("Cannot look up hostname %s: %s\n"), | |
825 request->hostname, g_strerror (errno)); | |
84 | 826 return (GFTP_ERETRYABLE); |
1 | 827 } |
828 | |
829 addr = (struct sockaddr *) request->host.h_addr_list[0]; | |
830 | |
831 #endif /* HAVE_GETADDRINFO */ | |
832 | |
122 | 833 templist = proxy_hosts->list; |
1 | 834 while (templist != NULL) |
835 { | |
836 hostname = templist->data; | |
837 if (hostname->domain && | |
838 strlen (request->hostname) > strlen (hostname->domain)) | |
168 | 839 { |
840 pos = request->hostname + strlen (request->hostname) - | |
841 strlen (hostname->domain); | |
842 if (strcmp (hostname->domain, pos) == 0) | |
843 return (0); | |
844 } | |
1 | 845 |
846 if (hostname->ipv4_network_address != 0) | |
168 | 847 { |
848 memcpy (addy, addr, sizeof (addy)); | |
849 netaddr = | |
850 (((addy[0] & 0xff) << 24) | ((addy[1] & 0xff) << 16) | | |
851 ((addy[2] & 0xff) << 8) | (addy[3] & 0xff)) & | |
1 | 852 hostname->ipv4_netmask; |
168 | 853 if (netaddr == hostname->ipv4_network_address) |
854 return (0); | |
855 } | |
1 | 856 templist = templist->next; |
857 } | |
858 | |
122 | 859 return (proxy_hostname != NULL && *proxy_hostname != '\0'); |
1 | 860 } |
861 | |
862 | |
48 | 863 static char * |
864 copy_token (char **dest, char *source) | |
1 | 865 { |
48 | 866 /* This function is used internally by gftp_parse_ls () */ |
867 char *endpos, savepos; | |
1 | 868 |
48 | 869 endpos = source; |
870 while (*endpos != ' ' && *endpos != '\t' && *endpos != '\0') | |
871 endpos++; | |
872 if (*endpos == '\0') | |
873 return (NULL); | |
1 | 874 |
48 | 875 savepos = *endpos; |
876 *endpos = '\0'; | |
877 *dest = g_malloc (endpos - source + 1); | |
878 strcpy (*dest, source); | |
879 *endpos = savepos; | |
1 | 880 |
48 | 881 /* Skip the blanks till we get to the next entry */ |
882 source = endpos + 1; | |
883 while ((*source == ' ' || *source == '\t') && *source != '\0') | |
884 source++; | |
885 return (source); | |
886 } | |
1 | 887 |
888 | |
48 | 889 static char * |
890 goto_next_token (char *pos) | |
891 { | |
892 while (*pos != ' ' && *pos != '\t' && *pos != '\0') | |
893 pos++; | |
1 | 894 |
48 | 895 if (pos == '\0') |
896 return (pos); | |
1 | 897 |
48 | 898 while ((*pos == ' ' || *pos == '\t') && *pos != '\0') |
899 pos++; | |
900 | |
901 return (pos); | |
1 | 902 } |
903 | |
904 | |
102 | 905 time_t |
906 parse_time (char *str, char **endpos) | |
1 | 907 { |
102 | 908 struct tm curtime, *loctime; |
105 | 909 time_t t, ret; |
102 | 910 char *tmppos; |
168 | 911 int i; |
1 | 912 |
102 | 913 memset (&curtime, 0, sizeof (curtime)); |
914 curtime.tm_isdst = -1; | |
105 | 915 if (strlen (str) > 4 && isdigit ((int) str[0]) && str[2] == '-' && isdigit ((int) str[3])) |
1 | 916 { |
917 /* This is how DOS will return the date/time */ | |
918 /* 07-06-99 12:57PM */ | |
919 | |
102 | 920 tmppos = strptime (str, "%m-%d-%y %I:%M%p", &curtime); |
1 | 921 } |
105 | 922 else if (strlen (str) > 4 && isdigit ((int) str[0]) && str[2] == '-' && isalpha (str[3])) |
923 { | |
924 /* 10-Jan-2003 09:14 */ | |
925 tmppos = strptime (str, "%d-%h-%Y %H:%M", &curtime); | |
926 } | |
1 | 927 else |
928 { | |
929 /* This is how most UNIX, Novell, and MacOS ftp servers send their time */ | |
102 | 930 /* Jul 06 12:57 or Jul 6 1999 */ |
1 | 931 |
102 | 932 if (strchr (str, ':') != NULL) |
933 { | |
934 tmppos = strptime (str, "%h %d %H:%M", &curtime); | |
935 t = time (NULL); | |
936 loctime = localtime (&t); | |
937 curtime.tm_year = loctime->tm_year; | |
938 } | |
939 else | |
940 tmppos = strptime (str, "%h %d %Y", &curtime); | |
941 } | |
1 | 942 |
105 | 943 if (tmppos != NULL) |
944 ret = mktime (&curtime); | |
945 else | |
946 ret = 0; | |
947 | |
102 | 948 if (endpos != NULL) |
105 | 949 { |
950 if (tmppos == NULL) | |
951 { | |
952 /* We cannot parse this date format. So, just skip this date field and continue to the next | |
953 token. This is mainly for the HTTP support */ | |
954 | |
168 | 955 for (i=0, *endpos = str; (*endpos)[i] != ' ' && (*endpos)[i] != '\t' && (*endpos)[i] != '\0'; i++); |
956 endpos += i; | |
105 | 957 } |
958 else | |
959 *endpos = tmppos; | |
960 } | |
1 | 961 |
102 | 962 return (mktime (&curtime)); |
1 | 963 } |
964 | |
965 | |
107 | 966 static void |
967 gftp_parse_vms_attribs (char *dest, char **src) | |
968 { | |
969 char *endpos; | |
970 | |
971 if (*src == NULL) | |
972 return; | |
973 | |
974 if ((endpos = strchr (*src, ',')) != NULL) | |
975 *endpos = '\0'; | |
976 | |
977 if (strchr (*src, 'R') != NULL) | |
978 *dest = 'r'; | |
979 if (strchr (*src, 'W') != NULL) | |
980 *(dest + 1) = 'w'; | |
981 if (strchr (*src, 'E') != NULL) | |
982 *(dest + 2) = 'x'; | |
983 | |
984 *src = endpos + 1; | |
985 } | |
986 | |
987 | |
988 static int | |
989 gftp_parse_ls_vms (char *str, gftp_file * fle) | |
990 { | |
991 char *curpos, *endpos; | |
992 | |
993 /* .PINE-DEBUG1;1 9 21-AUG-2002 20:06 [MYERSRG] (RWED,RWED,,) */ | |
994 /* WWW.DIR;1 1 23-NOV-1999 05:47 [MYERSRG] (RWE,RWE,RE,E) */ | |
995 | |
996 if ((curpos = strchr (str, ';')) == NULL) | |
997 return (GFTP_EFATAL); | |
998 | |
999 *curpos = '\0'; | |
1000 fle->attribs = g_strdup ("----------"); | |
1001 if (strlen (str) > 4 && strcmp (curpos - 4, ".DIR") == 0) | |
1002 { | |
1003 fle->isdir = 1; | |
1004 *fle->attribs = 'd'; | |
1005 *(curpos - 4) = '\0'; | |
1006 } | |
1007 | |
1008 fle->file = g_strdup (str); | |
1009 | |
1010 curpos = goto_next_token (curpos + 1); | |
122 | 1011 fle->size = strtol (curpos, NULL, 10) * 512; /* Is this correct? */ |
107 | 1012 curpos = goto_next_token (curpos); |
1013 | |
1014 if ((fle->datetime = parse_time (curpos, &curpos)) == 0) | |
1015 return (GFTP_EFATAL); | |
1016 | |
1017 curpos = goto_next_token (curpos); | |
1018 | |
1019 if (*curpos != '[') | |
1020 return (GFTP_EFATAL); | |
1021 curpos++; | |
1022 if ((endpos = strchr (curpos, ']')) == NULL) | |
1023 return (GFTP_EFATAL); | |
1024 *endpos = '\0'; | |
1025 fle->user = g_strdup (curpos); | |
1026 fle->group = g_strdup (""); | |
1027 | |
1028 curpos = goto_next_token (endpos + 1); | |
1029 if ((curpos = strchr (curpos, ',')) == NULL) | |
1030 return (0); | |
1031 curpos++; | |
1032 | |
1033 gftp_parse_vms_attribs (fle->attribs + 1, &curpos); | |
1034 gftp_parse_vms_attribs (fle->attribs + 4, &curpos); | |
1035 gftp_parse_vms_attribs (fle->attribs + 7, &curpos); | |
1036 | |
1037 return (0); | |
1038 } | |
1039 | |
1040 | |
1 | 1041 static int |
1042 gftp_parse_ls_eplf (char *str, gftp_file * fle) | |
1043 { | |
1044 char *startpos; | |
1045 | |
1046 startpos = str; | |
124 | 1047 fle->attribs = g_strdup ("----------"); |
1 | 1048 while (startpos) |
1049 { | |
1050 startpos++; | |
1051 switch (*startpos) | |
168 | 1052 { |
1053 case '/': | |
1054 *fle->attribs = 'd'; | |
1055 break; | |
1056 case 's': | |
1057 fle->size = strtol (startpos + 1, NULL, 10); | |
1058 break; | |
1059 case 'm': | |
1060 fle->datetime = strtol (startpos + 1, NULL, 10); | |
1061 break; | |
1062 } | |
1 | 1063 startpos = strchr (startpos, ','); |
1064 } | |
1065 if ((startpos = strchr (str, 9)) == NULL) | |
84 | 1066 return (GFTP_EFATAL); |
124 | 1067 fle->file = g_strdup (startpos + 1); |
1068 fle->user = g_strdup (_("unknown")); | |
1069 fle->group = g_strdup (_("unknown")); | |
1 | 1070 return (0); |
1071 } | |
1072 | |
1073 | |
1074 static int | |
91 | 1075 gftp_parse_ls_unix (gftp_request * request, char *str, gftp_file * fle) |
1 | 1076 { |
91 | 1077 char *endpos, *startpos, *pos; |
1078 int cols; | |
1079 | |
1080 /* If there is no space between the attribs and links field, just make one */ | |
1081 if (strlen (str) > 10) | |
1082 str[10] = ' '; | |
1083 | |
1084 /* Determine the number of columns */ | |
1085 cols = 0; | |
1086 pos = str; | |
1087 while (*pos != '\0') | |
1088 { | |
1089 while (*pos != '\0' && *pos != ' ' && *pos != '\t') | |
1090 { | |
1091 if (*pos == ':') | |
1092 break; | |
1093 pos++; | |
1094 } | |
1095 | |
1096 cols++; | |
1097 | |
1098 if (*pos == ':') | |
1099 { | |
1100 cols++; | |
1101 break; | |
1102 } | |
1103 | |
1104 while (*pos == ' ' || *pos == '\t') | |
1105 pos++; | |
1106 } | |
1 | 1107 |
1108 startpos = str; | |
1109 /* Copy file attributes */ | |
1110 if ((startpos = copy_token (&fle->attribs, startpos)) == NULL) | |
84 | 1111 return (GFTP_EFATAL); |
1 | 1112 |
1113 if (cols >= 9) | |
1114 { | |
1115 /* Skip the number of links */ | |
1116 startpos = goto_next_token (startpos); | |
1117 | |
1118 /* Copy the user that owns this file */ | |
1119 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
168 | 1120 return (GFTP_EFATAL); |
1 | 1121 |
1122 /* Copy the group that owns this file */ | |
1123 if ((startpos = copy_token (&fle->group, startpos)) == NULL) | |
168 | 1124 return (GFTP_EFATAL); |
1 | 1125 } |
1126 else | |
1127 { | |
124 | 1128 fle->group = g_strdup (_("unknown")); |
1 | 1129 if (cols == 8) |
168 | 1130 { |
1131 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
1132 return (GFTP_EFATAL); | |
1133 } | |
1 | 1134 else |
124 | 1135 fle->user = g_strdup (_("unknown")); |
1 | 1136 startpos = goto_next_token (startpos); |
1137 } | |
1138 | |
136 | 1139 if (request->server_type != GFTP_DIRTYPE_CRAY) |
1 | 1140 { |
91 | 1141 /* See if this is a Cray directory listing. It has the following format: |
1142 drwx------ 2 feiliu g913 DK common 4096 Sep 24 2001 wv */ | |
1143 if (cols == 11 && strstr (str, "->") == NULL) | |
1144 { | |
1145 startpos = goto_next_token (startpos); | |
1146 startpos = goto_next_token (startpos); | |
1147 } | |
1 | 1148 } |
1149 | |
1150 /* See if this is a block or character device. We will store the major number | |
1151 in the high word and the minor number in the low word. */ | |
1152 if ((fle->attribs[0] == 'b' || fle->attribs[0] == 'u' || | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1153 fle->attribs[0] == 'c') && |
1 | 1154 ((endpos = strchr (startpos, ',')) != NULL)) |
1155 { | |
1156 fle->size = strtol (startpos, NULL, 10) << 16; | |
1157 | |
1158 startpos = endpos + 1; | |
1159 while (*startpos == ' ') | |
168 | 1160 startpos++; |
1 | 1161 |
1162 /* Get the minor number */ | |
1163 if ((endpos = strchr (startpos, ' ')) == NULL) | |
168 | 1164 return (GFTP_EFATAL); |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1165 fle->size |= strtol (startpos, NULL, 10) & 0xFF; |
1 | 1166 } |
1167 else | |
1168 { | |
1169 /* This is a regular file */ | |
1170 if ((endpos = strchr (startpos, ' ')) == NULL) | |
168 | 1171 return (GFTP_EFATAL); |
1 | 1172 fle->size = strtol (startpos, NULL, 10); |
1173 } | |
1174 | |
1175 /* Skip the blanks till we get to the next entry */ | |
1176 startpos = endpos + 1; | |
1177 while (*startpos == ' ') | |
1178 startpos++; | |
1179 | |
102 | 1180 if ((fle->datetime = parse_time (startpos, &startpos)) == 0) |
84 | 1181 return (GFTP_EFATAL); |
1 | 1182 |
1183 /* Skip the blanks till we get to the next entry */ | |
1184 startpos = goto_next_token (startpos); | |
1185 | |
1186 /* Parse the filename. If this file is a symbolic link, remove the -> part */ | |
1187 if (fle->attribs[0] == 'l' && ((endpos = strstr (startpos, "->")) != NULL)) | |
1188 *(endpos - 1) = '\0'; | |
1189 | |
124 | 1190 fle->file = g_strdup (startpos); |
1 | 1191 |
1192 /* Uncomment this if you want to strip the spaces off of the end of the file. | |
1193 I don't want to do this by default since there are valid filenames with | |
1194 spaces at the end of them. Some broken FTP servers like the Paradyne IPC | |
1195 DSLAMS append a bunch of spaces at the end of the file. | |
1196 for (endpos = fle->file + strlen (fle->file) - 1; | |
1197 *endpos == ' '; | |
1198 *endpos-- = '\0'); | |
1199 */ | |
1200 | |
1201 return (0); | |
1202 } | |
1203 | |
1204 | |
1205 static int | |
1206 gftp_parse_ls_nt (char *str, gftp_file * fle) | |
1207 { | |
1208 char *startpos; | |
1209 | |
1210 startpos = str; | |
102 | 1211 if ((fle->datetime = parse_time (startpos, &startpos)) == 0) |
84 | 1212 return (GFTP_EFATAL); |
1 | 1213 |
124 | 1214 fle->user = g_strdup (_("unknown")); |
1215 fle->group = g_strdup (_("unknown")); | |
1 | 1216 |
1217 startpos = goto_next_token (startpos); | |
1218 if (startpos[0] == '<') | |
124 | 1219 fle->attribs = g_strdup ("drwxrwxrwx"); |
1 | 1220 else |
1221 { | |
124 | 1222 fle->attribs = g_strdup ("-rw-rw-rw-"); |
1 | 1223 fle->size = strtol (startpos, NULL, 10); |
1224 } | |
124 | 1225 |
1 | 1226 startpos = goto_next_token (startpos); |
124 | 1227 fle->file = g_strdup (startpos); |
1 | 1228 return (0); |
1229 } | |
1230 | |
1231 | |
1232 static int | |
1233 gftp_parse_ls_novell (char *str, gftp_file * fle) | |
1234 { | |
1235 char *startpos; | |
1236 | |
1237 if (str[12] != ' ') | |
84 | 1238 return (GFTP_EFATAL); |
1 | 1239 str[12] = '\0'; |
124 | 1240 fle->attribs = g_strdup (str); |
1 | 1241 startpos = str + 13; |
1242 | |
1243 while ((*startpos == ' ' || *startpos == '\t') && *startpos != '\0') | |
1244 startpos++; | |
1245 | |
1246 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
84 | 1247 return (GFTP_EFATAL); |
1 | 1248 |
124 | 1249 fle->group = g_strdup (_("unknown")); |
1 | 1250 |
1251 fle->size = strtol (startpos, NULL, 10); | |
1252 | |
1253 startpos = goto_next_token (startpos); | |
102 | 1254 if ((fle->datetime = parse_time (startpos, &startpos)) == 0) |
84 | 1255 return (GFTP_EFATAL); |
1 | 1256 |
1257 startpos = goto_next_token (startpos); | |
124 | 1258 fle->file = g_strdup (startpos); |
1 | 1259 return (0); |
1260 } | |
1261 | |
1262 | |
48 | 1263 int |
91 | 1264 gftp_parse_ls (gftp_request * request, const char *lsoutput, gftp_file * fle) |
1 | 1265 { |
107 | 1266 char *str, *endpos, tmpchar; |
1267 int result, is_vms; | |
91 | 1268 size_t len; |
48 | 1269 |
84 | 1270 g_return_val_if_fail (lsoutput != NULL, GFTP_EFATAL); |
1271 g_return_val_if_fail (fle != NULL, GFTP_EFATAL); | |
48 | 1272 |
124 | 1273 str = g_strdup (lsoutput); |
48 | 1274 memset (fle, 0, sizeof (*fle)); |
1 | 1275 |
91 | 1276 len = strlen (str); |
107 | 1277 if (len > 0 && str[len - 1] == '\n') |
91 | 1278 str[--len] = '\0'; |
1279 if (len > 0 && str[len - 1] == '\r') | |
1280 str[--len] = '\0'; | |
39 | 1281 |
91 | 1282 switch (request->server_type) |
1283 { | |
122 | 1284 case GFTP_DIRTYPE_CRAY: |
1285 case GFTP_DIRTYPE_UNIX: | |
1286 result = gftp_parse_ls_unix (request, str, fle); | |
91 | 1287 break; |
122 | 1288 case GFTP_DIRTYPE_EPLF: |
91 | 1289 result = gftp_parse_ls_eplf (str, fle); |
1290 break; | |
122 | 1291 case GFTP_DIRTYPE_NOVELL: |
91 | 1292 result = gftp_parse_ls_novell (str, fle); |
1293 break; | |
122 | 1294 case GFTP_DIRTYPE_DOS: |
91 | 1295 result = gftp_parse_ls_nt (str, fle); |
1296 break; | |
122 | 1297 case GFTP_DIRTYPE_VMS: |
107 | 1298 result = gftp_parse_ls_vms (str, fle); |
1299 break; | |
91 | 1300 default: /* autodetect */ |
1301 if (*lsoutput == '+') | |
1302 result = gftp_parse_ls_eplf (str, fle); | |
1303 else if (isdigit ((int) str[0]) && str[2] == '-') | |
1304 result = gftp_parse_ls_nt (str, fle); | |
1305 else if (str[1] == ' ' && str[2] == '[') | |
1306 result = gftp_parse_ls_novell (str, fle); | |
1307 else | |
107 | 1308 { |
1309 if ((endpos = strchr (str, ' ')) != NULL) | |
1310 { | |
1311 /* If the first token in the string has a ; in it, then */ | |
1312 /* we'll assume that this is a VMS directory listing */ | |
1313 tmpchar = *endpos; | |
1314 *endpos = '\0'; | |
1315 is_vms = strchr (str, ';') != NULL; | |
1316 *endpos = tmpchar; | |
1317 } | |
1318 else | |
1319 is_vms = 0; | |
48 | 1320 |
107 | 1321 if (is_vms) |
1322 result = gftp_parse_ls_vms (str, fle); | |
1323 else | |
1324 result = gftp_parse_ls_unix (request, str, fle); | |
1325 } | |
91 | 1326 break; |
48 | 1327 } |
1328 g_free (str); | |
1329 | |
1330 if (fle->attribs == NULL) | |
1331 return (result); | |
1332 | |
1333 if (*fle->attribs == 'd') | |
1334 fle->isdir = 1; | |
1335 if (*fle->attribs == 'l') | |
1336 fle->islink = 1; | |
1337 if (strchr (fle->attribs, 'x') != NULL && !fle->isdir && !fle->islink) | |
1338 fle->isexe = 1; | |
1339 | |
1340 return (result); | |
1 | 1341 } |
1342 | |
1343 | |
48 | 1344 static GHashTable * |
1345 gftp_gen_dir_hash (gftp_request * request) | |
1 | 1346 { |
48 | 1347 unsigned long *newsize; |
1348 GHashTable * dirhash; | |
1349 gftp_file * fle; | |
1350 char * newname; | |
1351 | |
39 | 1352 |
48 | 1353 dirhash = g_hash_table_new (string_hash_function, string_hash_compare); |
1354 if (gftp_list_files (request) == 0) | |
1355 { | |
1356 fle = g_malloc0 (sizeof (*fle)); | |
1357 while (gftp_get_next_file (request, NULL, fle) > 0) | |
1358 { | |
1359 newname = fle->file; | |
1360 newsize = g_malloc (sizeof (unsigned long)); | |
1361 *newsize = fle->size; | |
1362 g_hash_table_insert (dirhash, newname, newsize); | |
1363 fle->file = NULL; | |
1364 gftp_file_destroy (fle); | |
1365 } | |
1366 gftp_end_transfer (request); | |
1367 g_free (fle); | |
1368 } | |
1369 else | |
1370 { | |
1371 g_hash_table_destroy (dirhash); | |
1372 dirhash = NULL; | |
1373 } | |
1374 return (dirhash); | |
1375 } | |
39 | 1376 |
48 | 1377 |
1378 static void | |
1379 destroy_hash_ent (gpointer key, gpointer value, gpointer user_data) | |
1380 { | |
39 | 1381 |
48 | 1382 g_free (key); |
1383 g_free (value); | |
1384 } | |
1385 | |
1386 | |
1387 static void | |
1388 gftp_destroy_dir_hash (GHashTable * dirhash) | |
1389 { | |
1390 g_hash_table_foreach (dirhash, destroy_hash_ent, NULL); | |
1391 g_hash_table_destroy (dirhash); | |
1 | 1392 } |
1393 | |
1394 | |
1395 static GList * | |
1396 gftp_get_dir_listing (gftp_transfer * transfer, int getothdir) | |
1397 { | |
1398 unsigned long *newsize; | |
1399 GHashTable * dirhash; | |
1400 GList * templist; | |
1401 gftp_file * fle; | |
1402 char *newname; | |
1403 | |
1404 if (getothdir && transfer->toreq) | |
1405 dirhash = gftp_gen_dir_hash (transfer->toreq); | |
1406 else | |
1407 dirhash = NULL; | |
1408 | |
1409 if (gftp_list_files (transfer->fromreq) != 0) | |
1410 return (NULL); | |
1411 | |
1412 fle = g_malloc (sizeof (*fle)); | |
1413 templist = NULL; | |
1414 while (gftp_get_next_file (transfer->fromreq, NULL, fle) > 0) | |
1415 { | |
1416 if (strcmp (fle->file, ".") == 0 || strcmp (fle->file, "..") == 0) | |
1417 { | |
1418 gftp_file_destroy (fle); | |
1419 continue; | |
1420 } | |
1421 | |
1422 if (dirhash && | |
1423 (newsize = g_hash_table_lookup (dirhash, fle->file)) != NULL) | |
1424 fle->startsize = *newsize; | |
1425 | |
1426 if (transfer->toreq) | |
1427 fle->destfile = g_strconcat (transfer->toreq->directory, "/", | |
1428 fle->file, NULL); | |
1429 newname = g_strconcat (transfer->fromreq->directory, "/", fle->file, NULL); | |
1430 g_free (fle->file); | |
1431 fle->file = newname; | |
1432 | |
1433 templist = g_list_append (templist, fle); | |
1434 | |
1435 fle = g_malloc (sizeof (*fle)); | |
1436 } | |
1437 gftp_end_transfer (transfer->fromreq); | |
1438 | |
1439 gftp_file_destroy (fle); | |
1440 g_free (fle); | |
1441 | |
1442 if (dirhash) | |
1443 gftp_destroy_dir_hash (dirhash); | |
1444 | |
1445 | |
1446 return (templist); | |
1447 } | |
1448 | |
1449 | |
1450 int | |
1451 gftp_get_all_subdirs (gftp_transfer * transfer, | |
1452 void (*update_func) (gftp_transfer * transfer)) | |
1453 { | |
1454 char *oldfromdir, *oldtodir, *newname; | |
1455 GList * templist, * lastlist; | |
1456 int forcecd, remotechanged; | |
1457 unsigned long *newsize; | |
1458 GHashTable * dirhash; | |
1459 gftp_file * curfle; | |
1460 | |
84 | 1461 g_return_val_if_fail (transfer != NULL, GFTP_EFATAL); |
1462 g_return_val_if_fail (transfer->fromreq != NULL, GFTP_EFATAL); | |
1463 g_return_val_if_fail (transfer->files != NULL, GFTP_EFATAL); | |
1 | 1464 |
1465 if (transfer->toreq != NULL) | |
1466 dirhash = gftp_gen_dir_hash (transfer->toreq); | |
1467 else | |
1468 dirhash = NULL; | |
1469 | |
1470 for (lastlist = transfer->files; ; lastlist = lastlist->next) | |
1471 { | |
1472 curfle = lastlist->data; | |
1473 if (dirhash && | |
1474 (newsize = g_hash_table_lookup (dirhash, curfle->file)) != NULL) | |
1475 curfle->startsize = *newsize; | |
1476 | |
1477 if (transfer->toreq) | |
1478 curfle->destfile = g_strconcat (transfer->toreq->directory, "/", | |
1479 curfle->file, NULL); | |
1480 newname = g_strconcat (transfer->fromreq->directory, transfer->fromreq->directory[strlen (transfer->fromreq->directory) - 1] == '/' ? "" : "/", curfle->file, NULL); | |
1481 g_free (curfle->file); | |
1482 curfle->file = newname; | |
1483 | |
1484 if (lastlist->next == NULL) | |
1485 break; | |
1486 } | |
1487 | |
1488 if (dirhash) | |
1489 gftp_destroy_dir_hash (dirhash); | |
1490 | |
1491 oldfromdir = oldtodir = NULL; | |
1492 remotechanged = 0; | |
1493 forcecd = 0; | |
1494 for (templist = transfer->files; templist != NULL; templist = templist->next) | |
1495 { | |
1496 curfle = templist->data; | |
1497 | |
1498 if (curfle->isdir) | |
1499 { | |
1500 oldfromdir = transfer->fromreq->directory; | |
1501 transfer->fromreq->directory = curfle->file; | |
1502 | |
1503 if (transfer->toreq) | |
1504 { | |
1505 oldtodir = transfer->toreq->directory; | |
1506 transfer->toreq->directory = curfle->destfile; | |
1507 } | |
1508 forcecd = 1; | |
1509 if (gftp_set_directory (transfer->fromreq, | |
1510 transfer->fromreq->directory) == 0) | |
1511 { | |
1512 if (curfle->startsize > 0 && transfer->toreq) | |
1513 { | |
1514 remotechanged = 1; | |
1515 if (gftp_set_directory (transfer->toreq, | |
1516 transfer->toreq->directory) != 0) | |
1517 curfle->startsize = 0; | |
1518 } | |
1519 | |
1520 lastlist->next = gftp_get_dir_listing (transfer, | |
1521 curfle->startsize > 0); | |
1522 if (lastlist->next != NULL) | |
1523 { | |
1524 lastlist->next->prev = lastlist; | |
1525 for (; lastlist->next != NULL; lastlist = lastlist->next); | |
1526 } | |
1527 transfer->numdirs++; | |
1528 if (update_func) | |
1529 update_func (transfer); | |
1530 } | |
1531 else | |
1532 curfle->transfer_action = GFTP_TRANS_ACTION_SKIP; | |
1533 | |
1534 transfer->fromreq->directory = oldfromdir; | |
1535 if (transfer->toreq) | |
1536 transfer->toreq->directory = oldtodir; | |
1537 } | |
1538 else | |
122 | 1539 transfer->numfiles++; |
1 | 1540 } |
1541 | |
1542 if (forcecd) | |
1543 gftp_set_directory (transfer->fromreq, transfer->fromreq->directory); | |
1544 if (remotechanged && transfer->toreq) | |
1545 gftp_set_directory (transfer->toreq, transfer->toreq->directory); | |
1546 | |
1547 if (update_func) | |
1548 { | |
1549 transfer->numfiles = transfer->numdirs = -1; | |
1550 update_func (transfer); | |
1551 } | |
1552 return (0); | |
1553 } | |
1554 | |
1555 | |
1556 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
122 | 1557 static int |
1 | 1558 get_port (struct addrinfo *addr) |
1559 { | |
1560 struct sockaddr_in * saddr; | |
1561 int port; | |
1562 | |
1563 if (addr->ai_family == AF_INET) | |
1564 { | |
1565 saddr = (struct sockaddr_in *) addr->ai_addr; | |
1566 port = ntohs (saddr->sin_port); | |
1567 } | |
1568 else | |
1569 port = 0; | |
1570 | |
1571 return (port); | |
1572 } | |
1573 #endif | |
1574 | |
1575 | |
1576 int | |
122 | 1577 gftp_connect_server (gftp_request * request, char *service, |
1578 char *proxy_hostname, int proxy_port) | |
1 | 1579 { |
1580 char *connect_host, *disphost; | |
1581 int port, sock; | |
1582 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
1583 struct addrinfo hints, *res; | |
1584 char serv[8]; | |
1585 int errnum; | |
1586 | |
122 | 1587 if ((request->use_proxy = gftp_need_proxy (request, service, |
1588 proxy_hostname, proxy_port)) < 0) | |
84 | 1589 return (request->use_proxy); |
1 | 1590 else if (request->use_proxy == 1) |
1591 request->hostp = NULL; | |
1592 | |
151 | 1593 request->free_hostp = 1; |
1 | 1594 memset (&hints, 0, sizeof (hints)); |
1595 hints.ai_flags = AI_CANONNAME; | |
146 | 1596 hints.ai_family = PF_UNSPEC; |
1 | 1597 hints.ai_socktype = SOCK_STREAM; |
1598 | |
122 | 1599 if (request->use_proxy) |
1600 { | |
1601 connect_host = proxy_hostname; | |
1602 port = proxy_port; | |
1603 } | |
1604 else | |
1605 { | |
1606 connect_host = request->hostname; | |
1607 port = request->port; | |
1608 } | |
1 | 1609 |
1610 if (request->hostp == NULL) | |
1611 { | |
1612 if (port == 0) | |
1613 strcpy (serv, service); | |
1614 else | |
1615 snprintf (serv, sizeof (serv), "%d", port); | |
1616 | |
1617 request->logging_function (gftp_logging_misc, request->user_data, | |
168 | 1618 _("Looking up %s\n"), connect_host); |
1 | 1619 if ((errnum = getaddrinfo (connect_host, serv, &hints, |
1620 &request->hostp)) != 0) | |
168 | 1621 { |
1622 request->logging_function (gftp_logging_error, request->user_data, | |
1623 _("Cannot look up hostname %s: %s\n"), | |
1624 connect_host, gai_strerror (errnum)); | |
1625 return (GFTP_ERETRYABLE); | |
1626 } | |
1 | 1627 } |
1628 | |
1629 disphost = connect_host; | |
1630 for (res = request->hostp; res != NULL; res = res->ai_next) | |
1631 { | |
1632 disphost = res->ai_canonname ? res->ai_canonname : connect_host; | |
1633 port = get_port (res); | |
56 | 1634 if (!request->use_proxy) |
1635 request->port = port; | |
1636 | |
1 | 1637 if ((sock = socket (res->ai_family, res->ai_socktype, |
1638 res->ai_protocol)) < 0) | |
1639 { | |
168 | 1640 request->logging_function (gftp_logging_error, request->user_data, |
1 | 1641 _("Failed to create a socket: %s\n"), |
1642 g_strerror (errno)); | |
1643 continue; | |
66 | 1644 } |
1 | 1645 |
1646 request->logging_function (gftp_logging_misc, request->user_data, | |
168 | 1647 _("Trying %s:%d\n"), disphost, port); |
1 | 1648 |
1649 if (connect (sock, res->ai_addr, res->ai_addrlen) == -1) | |
168 | 1650 { |
1651 request->logging_function (gftp_logging_error, request->user_data, | |
1652 _("Cannot connect to %s: %s\n"), | |
1653 disphost, g_strerror (errno)); | |
1 | 1654 close (sock); |
1655 continue; | |
168 | 1656 } |
1 | 1657 break; |
1658 } | |
1659 | |
1660 if (res == NULL) | |
1661 { | |
1662 if (request->hostp != NULL) | |
1663 { | |
1664 freeaddrinfo (request->hostp); | |
1665 request->hostp = NULL; | |
1666 } | |
84 | 1667 return (GFTP_ERETRYABLE); |
1 | 1668 } |
1669 | |
1670 #else /* !HAVE_GETADDRINFO */ | |
1671 struct sockaddr_in remote_address; | |
1672 struct servent serv_struct; | |
1673 int curhost; | |
1674 | |
122 | 1675 if ((request->use_proxy = gftp_need_proxy (request, service, |
1676 proxy_hostname, proxy_port)) < 0) | |
84 | 1677 return (request->use_proxy); |
1 | 1678 else if (request->use_proxy == 1) |
1679 request->hostp = NULL; | |
1680 | |
1681 if ((sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) | |
1682 { | |
1683 request->logging_function (gftp_logging_error, request->user_data, | |
1684 _("Failed to create a socket: %s\n"), | |
1685 g_strerror (errno)); | |
84 | 1686 return (GFTP_ERETRYABLE); |
1 | 1687 } |
1688 | |
1689 memset (&remote_address, 0, sizeof (remote_address)); | |
1690 remote_address.sin_family = AF_INET; | |
1691 | |
122 | 1692 if (request->use_proxy) |
1693 { | |
1694 connect_host = proxy_hostname; | |
1695 port = proxy_port; | |
1696 } | |
1697 else | |
1698 { | |
1699 connect_host = request->hostname; | |
1700 port = request->port; | |
1701 } | |
1 | 1702 |
1703 if (port == 0) | |
1704 { | |
1705 if (!r_getservbyname (service, "tcp", &serv_struct, NULL)) | |
1706 { | |
122 | 1707 request->logging_function (gftp_logging_error, request->user_data, |
1708 _("Cannot look up service name %s/tcp. Please check your services file\n"), | |
1709 service); | |
1710 close (sock); | |
1711 return (GFTP_EFATAL); | |
1 | 1712 } |
1713 else | |
1714 { | |
1715 port = serv_struct.s_port; | |
1716 request->port = ntohs (serv_struct.s_port); | |
1717 } | |
56 | 1718 |
1719 if (!request->use_proxy) | |
1720 request->port = ntohs (port); | |
1 | 1721 } |
1722 remote_address.sin_port = port; | |
1723 | |
1724 if (request->hostp == NULL) | |
1725 { | |
1726 request->logging_function (gftp_logging_misc, request->user_data, | |
168 | 1727 _("Looking up %s\n"), connect_host); |
1 | 1728 if (!(request->hostp = r_gethostbyname (connect_host, &request->host, |
1729 NULL))) | |
1730 { | |
1731 request->logging_function (gftp_logging_error, request->user_data, | |
1732 _("Cannot look up hostname %s: %s\n"), | |
1733 connect_host, g_strerror (errno)); | |
1734 close (sock); | |
84 | 1735 return (GFTP_ERETRYABLE); |
1 | 1736 } |
1737 } | |
1738 | |
1739 disphost = NULL; | |
1740 for (curhost = 0; request->host.h_addr_list[curhost] != NULL; curhost++) | |
1741 { | |
1742 disphost = request->host.h_name; | |
1743 memcpy (&remote_address.sin_addr, request->host.h_addr_list[curhost], | |
1744 request->host.h_length); | |
1745 request->logging_function (gftp_logging_misc, request->user_data, | |
1746 _("Trying %s:%d\n"), | |
1747 request->host.h_name, ntohs (port)); | |
1748 | |
1749 if (connect (sock, (struct sockaddr *) &remote_address, | |
1750 sizeof (remote_address)) == -1) | |
1751 { | |
1752 request->logging_function (gftp_logging_error, request->user_data, | |
1753 _("Cannot connect to %s: %s\n"), | |
1754 connect_host, g_strerror (errno)); | |
1755 } | |
1756 break; | |
1757 } | |
1758 | |
1759 if (request->host.h_addr_list[curhost] == NULL) | |
1760 { | |
1761 close (sock); | |
84 | 1762 return (GFTP_ERETRYABLE); |
1 | 1763 } |
1764 port = ntohs (port); | |
1765 #endif /* HAVE_GETADDRINFO */ | |
1766 | |
1767 request->logging_function (gftp_logging_misc, request->user_data, | |
168 | 1768 _("Connected to %s:%d\n"), connect_host, port); |
58 | 1769 |
168 | 1770 if (gftp_fd_set_sockblocking (request, sock, 1) < 0) |
58 | 1771 { |
1772 close (sock); | |
84 | 1773 return (GFTP_ERETRYABLE); |
58 | 1774 } |
1775 | |
168 | 1776 request->sockfd = sock; |
1777 | |
1778 if (request->post_connect != NULL) | |
1779 return (request->post_connect (request)); | |
1780 | |
1781 return (0); | |
1 | 1782 } |
1783 | |
1784 | |
1785 void | |
1786 gftp_set_config_options (gftp_request * request) | |
1787 { | |
58 | 1788 if (request->set_config_options != NULL) |
1789 request->set_config_options (request); | |
1 | 1790 } |
1791 | |
1792 | |
1793 void | |
1794 print_file_list (GList * list) | |
1795 { | |
1796 gftp_file * tempfle; | |
1797 GList * templist; | |
1798 | |
1799 printf ("--START OF FILE LISTING - TOP TO BOTTOM--\n"); | |
1800 for (templist = list; ; templist = templist->next) | |
1801 { | |
1802 tempfle = templist->data; | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1803 #if defined (_LARGEFILE_SOURCE) |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1804 printf ("%s:%s:%lld:%lld:%s:%s:%s\n", |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1805 #else |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1806 printf ("%s:%s:%ld:%ld:%s:%s:%s\n", |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1807 #endif |
16 | 1808 tempfle->file, tempfle->destfile, |
1809 tempfle->size, tempfle->startsize, | |
1810 tempfle->user, tempfle->group, tempfle->attribs); | |
1 | 1811 if (templist->next == NULL) |
1812 break; | |
1813 } | |
1814 | |
1815 printf ("--START OF FILE LISTING - BOTTOM TO TOP--\n"); | |
1816 for (; ; templist = templist->prev) | |
1817 { | |
1818 tempfle = templist->data; | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1819 #if defined (_LARGEFILE_SOURCE) |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1820 printf ("%s:%s:%lld:%lld:%s:%s:%s\n", |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1821 #else |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1822 printf ("%s:%s:%ld:%ld:%s:%s:%s\n", |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1823 #endif |
16 | 1824 tempfle->file, tempfle->destfile, |
1825 tempfle->size, tempfle->startsize, | |
1826 tempfle->user, tempfle->group, tempfle->attribs); | |
1 | 1827 if (templist == list) |
1828 break; | |
1829 } | |
1830 printf ("--END OF FILE LISTING--\n"); | |
1831 } | |
1832 | |
41 | 1833 |
58 | 1834 static void |
1835 gftp_free_getline_buffer (gftp_getline_buffer ** rbuf) | |
41 | 1836 { |
58 | 1837 g_free ((*rbuf)->buffer); |
1838 g_free (*rbuf); | |
1839 *rbuf = NULL; | |
41 | 1840 } |
1841 | |
1842 | |
58 | 1843 ssize_t |
1844 gftp_get_line (gftp_request * request, gftp_getline_buffer ** rbuf, | |
1845 char * str, size_t len, int fd) | |
41 | 1846 { |
168 | 1847 ssize_t ret, retval, rlen; |
58 | 1848 char *pos, *nextpos; |
168 | 1849 ssize_t (*read_function) (gftp_request * request, void *ptr, size_t size, |
1850 int fd); | |
1851 | |
1852 if (request == NULL || request->read_function == NULL) | |
1853 read_function = gftp_fd_read; | |
1854 else | |
1855 read_function = request->read_function; | |
58 | 1856 |
1857 if (*rbuf == NULL) | |
1858 { | |
1859 *rbuf = g_malloc0 (sizeof (**rbuf)); | |
1860 (*rbuf)->max_bufsize = len; | |
60 | 1861 (*rbuf)->buffer = g_malloc ((*rbuf)->max_bufsize + 1); |
168 | 1862 |
1863 if ((ret = read_function (request, (*rbuf)->buffer, | |
1864 (*rbuf)->max_bufsize, fd)) <= 0) | |
58 | 1865 { |
1866 gftp_free_getline_buffer (rbuf); | |
1867 return (ret); | |
1868 } | |
168 | 1869 |
60 | 1870 (*rbuf)->buffer[ret] = '\0'; |
58 | 1871 (*rbuf)->cur_bufsize = ret; |
1872 (*rbuf)->curpos = (*rbuf)->buffer; | |
1873 } | |
1874 | |
84 | 1875 retval = GFTP_ERETRYABLE; |
58 | 1876 do |
1877 { | |
1878 if ((*rbuf)->cur_bufsize > 0 && | |
1879 ((pos = strchr ((*rbuf)->curpos, '\n')) != NULL || | |
1880 ((*rbuf)->curpos == (*rbuf)->buffer && | |
1881 (*rbuf)->max_bufsize == (*rbuf)->cur_bufsize))) | |
1882 { | |
1883 if (pos != NULL) | |
168 | 1884 retval = pos - (*rbuf)->curpos + 1; |
1885 else | |
1886 retval = (*rbuf)->cur_bufsize; | |
1887 | |
1888 if (pos != NULL) | |
58 | 1889 { |
1890 nextpos = pos + 1; | |
1891 if (pos > (*rbuf)->curpos && *(pos - 1) == '\r') | |
1892 pos--; | |
1893 *pos = '\0'; | |
1894 } | |
1895 else | |
60 | 1896 nextpos = NULL; |
58 | 1897 |
1898 strncpy (str, (*rbuf)->curpos, len); | |
168 | 1899 (*rbuf)->cur_bufsize -= retval; |
58 | 1900 |
168 | 1901 if (nextpos != NULL) |
1902 (*rbuf)->curpos = nextpos; | |
1903 else | |
1904 (*rbuf)->cur_bufsize = 0; | |
58 | 1905 break; |
1906 } | |
1907 else | |
1908 { | |
1909 if ((*rbuf)->cur_bufsize == 0 || *(*rbuf)->curpos == '\0') | |
1910 { | |
1911 rlen = (*rbuf)->max_bufsize; | |
1912 pos = (*rbuf)->buffer; | |
1913 } | |
1914 else | |
1915 { | |
168 | 1916 memmove ((*rbuf)->buffer, (*rbuf)->curpos, (*rbuf)->cur_bufsize); |
1917 pos = (*rbuf)->buffer + (*rbuf)->cur_bufsize; | |
1918 rlen = (*rbuf)->max_bufsize - (*rbuf)->cur_bufsize; | |
58 | 1919 } |
168 | 1920 |
58 | 1921 (*rbuf)->curpos = (*rbuf)->buffer; |
1922 | |
168 | 1923 if ((ret = read_function (request, pos, rlen, fd)) < 0) |
58 | 1924 { |
1925 gftp_free_getline_buffer (rbuf); | |
1926 return (ret); | |
1927 } | |
168 | 1928 else if (ret == 0 && (*rbuf)->cur_bufsize == 0) |
1929 { | |
1930 gftp_free_getline_buffer (rbuf); | |
1931 return (ret); | |
1932 } | |
1933 | |
1934 (*rbuf)->buffer[ret + (*rbuf)->cur_bufsize] = '\0'; | |
1935 (*rbuf)->cur_bufsize += ret; | |
58 | 1936 } |
1937 } | |
84 | 1938 while (retval == GFTP_ERETRYABLE); |
58 | 1939 |
1940 return (retval); | |
1941 } | |
1942 | |
1943 | |
1944 ssize_t | |
168 | 1945 gftp_fd_read (gftp_request * request, void *ptr, size_t size, int fd) |
58 | 1946 { |
122 | 1947 long network_timeout; |
58 | 1948 struct timeval tv; |
1949 fd_set fset; | |
1950 ssize_t ret; | |
41 | 1951 |
122 | 1952 gftp_lookup_request_option (request, "network_timeout", &network_timeout); |
1953 | |
41 | 1954 errno = 0; |
1955 ret = 0; | |
1956 do | |
1957 { | |
58 | 1958 FD_ZERO (&fset); |
1959 FD_SET (fd, &fset); | |
122 | 1960 tv.tv_sec = network_timeout; |
58 | 1961 tv.tv_usec = 0; |
1962 ret = select (fd + 1, &fset, NULL, NULL, &tv); | |
1963 if (ret == -1 && errno == EINTR) | |
41 | 1964 { |
58 | 1965 if (request && request->cancel) |
1966 break; | |
1967 else | |
1968 continue; | |
1969 } | |
1970 else if (ret <= 0) | |
1971 { | |
1972 if (request != NULL) | |
41 | 1973 { |
58 | 1974 request->logging_function (gftp_logging_error, request->user_data, |
1975 _("Connection to %s timed out\n"), | |
1976 request->hostname); | |
1977 gftp_disconnect (request); | |
1978 } | |
84 | 1979 return (GFTP_ERETRYABLE); |
41 | 1980 } |
1981 | |
58 | 1982 if ((ret = read (fd, ptr, size)) < 0) |
41 | 1983 { |
1984 if (errno == EINTR) | |
1985 { | |
58 | 1986 if (request != NULL && request->cancel) |
41 | 1987 break; |
1988 else | |
58 | 1989 continue; |
41 | 1990 } |
1991 | |
58 | 1992 if (request != NULL) |
1993 { | |
1994 request->logging_function (gftp_logging_error, request->user_data, | |
1995 _("Error: Could not read from socket: %s\n"), | |
41 | 1996 g_strerror (errno)); |
58 | 1997 gftp_disconnect (request); |
1998 } | |
84 | 1999 return (GFTP_ERETRYABLE); |
41 | 2000 } |
2001 } | |
58 | 2002 while (errno == EINTR && !(request != NULL && request->cancel)); |
41 | 2003 |
58 | 2004 if (errno == EINTR && request != NULL && request->cancel) |
41 | 2005 { |
2006 gftp_disconnect (request); | |
84 | 2007 return (GFTP_ERETRYABLE); |
41 | 2008 } |
2009 | |
2010 return (ret); | |
2011 } | |
2012 | |
58 | 2013 |
2014 ssize_t | |
168 | 2015 gftp_fd_write (gftp_request * request, const char *ptr, size_t size, int fd) |
58 | 2016 { |
122 | 2017 long network_timeout; |
58 | 2018 struct timeval tv; |
2019 size_t ret, w_ret; | |
2020 fd_set fset; | |
2021 | |
122 | 2022 gftp_lookup_request_option (request, "network_timeout", &network_timeout); |
2023 | |
58 | 2024 errno = 0; |
2025 ret = 0; | |
2026 do | |
2027 { | |
2028 FD_ZERO (&fset); | |
2029 FD_SET (fd, &fset); | |
122 | 2030 tv.tv_sec = network_timeout; |
58 | 2031 tv.tv_usec = 0; |
2032 ret = select (fd + 1, NULL, &fset, NULL, &tv); | |
2033 if (ret == -1 && errno == EINTR) | |
2034 { | |
2035 if (request != NULL && request->cancel) | |
2036 break; | |
2037 else | |
2038 continue; | |
2039 } | |
2040 else if (ret <= 0) | |
2041 { | |
2042 if (request != NULL) | |
2043 { | |
2044 request->logging_function (gftp_logging_error, request->user_data, | |
2045 _("Connection to %s timed out\n"), | |
2046 request->hostname); | |
2047 gftp_disconnect (request); | |
2048 } | |
84 | 2049 return (GFTP_ERETRYABLE); |
58 | 2050 } |
2051 | |
2052 if ((w_ret = write (fd, ptr, size)) < 0) | |
2053 { | |
2054 if (errno == EINTR) | |
2055 { | |
2056 if (request != NULL && request->cancel) | |
2057 break; | |
2058 else | |
2059 continue; | |
2060 } | |
2061 | |
2062 if (request != NULL) | |
2063 { | |
2064 request->logging_function (gftp_logging_error, request->user_data, | |
2065 _("Error: Could not write to socket: %s\n"), | |
2066 g_strerror (errno)); | |
2067 gftp_disconnect (request); | |
2068 } | |
84 | 2069 return (GFTP_ERETRYABLE); |
58 | 2070 } |
2071 | |
2072 ptr += w_ret; | |
2073 size -= w_ret; | |
2074 ret += w_ret; | |
2075 } | |
2076 while (size > 0); | |
2077 | |
2078 if (errno == EINTR && request != NULL && request->cancel) | |
2079 { | |
2080 gftp_disconnect (request); | |
84 | 2081 return (GFTP_ERETRYABLE); |
58 | 2082 } |
2083 | |
2084 return (ret); | |
2085 } | |
2086 | |
2087 | |
2088 ssize_t | |
2089 gftp_writefmt (gftp_request * request, int fd, const char *fmt, ...) | |
2090 { | |
2091 char *tempstr; | |
2092 va_list argp; | |
2093 ssize_t ret; | |
2094 | |
2095 va_start (argp, fmt); | |
2096 tempstr = g_strdup_vprintf (fmt, argp); | |
2097 va_end (argp); | |
2098 | |
168 | 2099 ret = request->write_function (request, tempstr, strlen (tempstr), fd); |
58 | 2100 g_free (tempstr); |
2101 return (ret); | |
2102 } | |
2103 | |
2104 | |
2105 int | |
168 | 2106 gftp_fd_set_sockblocking (gftp_request * request, int fd, int non_blocking) |
58 | 2107 { |
2108 int flags; | |
2109 | |
84 | 2110 if ((flags = fcntl (fd, F_GETFL, 0)) < 0) |
58 | 2111 { |
2112 request->logging_function (gftp_logging_error, request->user_data, | |
2113 _("Cannot get socket flags: %s\n"), | |
2114 g_strerror (errno)); | |
2115 gftp_disconnect (request); | |
84 | 2116 return (GFTP_ERETRYABLE); |
58 | 2117 } |
2118 | |
2119 if (non_blocking) | |
2120 flags |= O_NONBLOCK; | |
2121 else | |
2122 flags &= ~O_NONBLOCK; | |
2123 | |
84 | 2124 if (fcntl (fd, F_SETFL, flags) < 0) |
58 | 2125 { |
2126 request->logging_function (gftp_logging_error, request->user_data, | |
2127 _("Cannot set socket to non-blocking: %s\n"), | |
2128 g_strerror (errno)); | |
2129 gftp_disconnect (request); | |
84 | 2130 return (GFTP_ERETRYABLE); |
58 | 2131 } |
2132 | |
2133 return (0); | |
2134 } | |
2135 | |
2136 | |
63 | 2137 void |
2138 gftp_swap_socks (gftp_request * dest, gftp_request * source) | |
2139 { | |
2140 g_return_if_fail (dest != NULL); | |
2141 g_return_if_fail (source != NULL); | |
2142 g_return_if_fail (dest->protonum == source->protonum); | |
2143 | |
2144 dest->sockfd = source->sockfd; | |
2145 dest->datafd = source->datafd; | |
2146 dest->cached = 0; | |
2147 if (!source->always_connected) | |
2148 { | |
2149 source->sockfd = -1; | |
2150 source->datafd = -1; | |
2151 source->cached = 1; | |
2152 } | |
2153 | |
2154 if (dest->swap_socks) | |
2155 dest->swap_socks (dest, source); | |
2156 } | |
2157 | |
122 | 2158 |
2159 void | |
2160 gftp_calc_kbs (gftp_transfer * tdata, ssize_t num_read) | |
2161 { | |
2162 unsigned long waitusecs; | |
2163 double difftime, curkbs; | |
2164 gftp_file * tempfle; | |
2165 unsigned long toadd; | |
2166 struct timeval tv; | |
2167 float maxkbs; | |
2168 | |
2169 gftp_lookup_request_option (tdata->fromreq, "maxkbs", &maxkbs); | |
2170 | |
2171 gettimeofday (&tv, NULL); | |
2172 if (g_thread_supported ()) | |
2173 g_static_mutex_lock (&tdata->statmutex); | |
2174 | |
2175 tempfle = tdata->curfle->data; | |
2176 tdata->trans_bytes += num_read; | |
2177 tdata->curtrans += num_read; | |
2178 tdata->stalled = 0; | |
2179 | |
2180 difftime = (tv.tv_sec - tdata->starttime.tv_sec) + ((double) (tv.tv_usec - tdata->starttime.tv_usec) / 1000000.0); | |
2181 if (difftime <= 0) | |
2182 tdata->kbs = (double) tdata->trans_bytes / 1024.0; | |
2183 else | |
2184 tdata->kbs = (double) tdata->trans_bytes / 1024.0 / difftime; | |
2185 | |
2186 difftime = (tv.tv_sec - tdata->lasttime.tv_sec) + ((double) (tv.tv_usec - tdata->lasttime.tv_usec) / 1000000.0); | |
2187 | |
2188 if (difftime <= 0) | |
2189 curkbs = (double) (num_read / 1024.0); | |
2190 else | |
2191 curkbs = (double) (num_read / 1024.0 / difftime); | |
2192 | |
2193 if (maxkbs > 0 && curkbs > maxkbs) | |
2194 { | |
2195 waitusecs = (double) num_read / 1024.0 / maxkbs * 1000000.0 - difftime; | |
2196 | |
2197 if (waitusecs > 0) | |
2198 { | |
2199 if (g_thread_supported ()) | |
2200 g_static_mutex_unlock (&tdata->statmutex); | |
2201 | |
2202 difftime += ((double) waitusecs / 1000000.0); | |
2203 usleep (waitusecs); | |
2204 | |
2205 if (g_thread_supported ()) | |
2206 g_static_mutex_lock (&tdata->statmutex); | |
2207 } | |
2208 } | |
2209 | |
2210 /* I don't call gettimeofday (&tdata->lasttime) here because this will use | |
2211 less system resources. This will be close enough for what we need */ | |
2212 difftime += tdata->lasttime.tv_usec / 1000000.0; | |
2213 toadd = (long) difftime; | |
2214 difftime -= toadd; | |
2215 tdata->lasttime.tv_sec += toadd; | |
2216 tdata->lasttime.tv_usec = difftime * 1000000.0; | |
2217 | |
2218 if (g_thread_supported ()) | |
2219 g_static_mutex_unlock (&tdata->statmutex); | |
2220 } | |
2221 | |
125 | 2222 |
2223 int | |
2224 gftp_get_transfer_status (gftp_transfer * tdata, ssize_t num_read) | |
2225 { | |
2226 int ret1, ret2, retries, sleep_time; | |
2227 gftp_file * tempfle; | |
2228 struct timeval tv; | |
2229 | |
2230 ret1 = ret2 = 0; | |
2231 gftp_lookup_request_option (tdata->fromreq, "retries", &retries); | |
2232 gftp_lookup_request_option (tdata->fromreq, "sleep_time", &sleep_time); | |
2233 | |
2234 if (g_thread_supported ()) | |
2235 g_static_mutex_lock (&tdata->structmutex); | |
2236 | |
2237 if (tdata->curfle == NULL) | |
2238 { | |
2239 if (g_thread_supported ()) | |
2240 g_static_mutex_unlock (&tdata->structmutex); | |
2241 | |
2242 return (GFTP_EFATAL); | |
2243 } | |
2244 | |
2245 tempfle = tdata->curfle->data; | |
2246 | |
2247 if (g_thread_supported ()) | |
2248 g_static_mutex_unlock (&tdata->structmutex); | |
2249 | |
2250 gftp_disconnect (tdata->fromreq); | |
2251 gftp_disconnect (tdata->toreq); | |
2252 | |
2253 if (num_read < 0 || tdata->skip_file) | |
2254 { | |
2255 if (num_read == GFTP_EFATAL) | |
2256 return (GFTP_EFATAL); | |
2257 else if (retries != 0 && | |
2258 tdata->current_file_retries >= retries) | |
2259 { | |
2260 tdata->fromreq->logging_function (gftp_logging_error, | |
2261 tdata->fromreq->user_data, | |
2262 _("Error: Remote site %s disconnected. Max retries reached...giving up\n"), | |
2263 tdata->fromreq->hostname != NULL ? | |
2264 tdata->fromreq->hostname : tdata->toreq->hostname); | |
2265 return (GFTP_EFATAL); | |
2266 } | |
2267 else | |
2268 { | |
2269 tdata->fromreq->logging_function (gftp_logging_error, | |
2270 tdata->fromreq->user_data, | |
2271 _("Error: Remote site %s disconnected. Will reconnect in %d seconds\n"), | |
2272 tdata->fromreq->hostname != NULL ? | |
2273 tdata->fromreq->hostname : tdata->toreq->hostname, | |
2274 sleep_time); | |
2275 } | |
2276 | |
2277 while (retries == 0 || | |
2278 tdata->current_file_retries <= retries) | |
2279 { | |
2280 if (!tdata->skip_file) | |
2281 { | |
2282 tv.tv_sec = sleep_time; | |
2283 tv.tv_usec = 0; | |
2284 select (0, NULL, NULL, NULL, &tv); | |
2285 } | |
2286 | |
2287 if ((ret1 = gftp_connect (tdata->fromreq)) == 0 && | |
2288 (ret2 = gftp_connect (tdata->toreq)) == 0) | |
2289 { | |
2290 if (g_thread_supported ()) | |
2291 g_static_mutex_lock (&tdata->structmutex); | |
2292 | |
2293 tdata->resumed_bytes = tdata->resumed_bytes + tdata->trans_bytes - tdata->curresumed - tdata->curtrans; | |
2294 tdata->trans_bytes = 0; | |
2295 if (tdata->skip_file) | |
2296 { | |
2297 tdata->total_bytes -= tempfle->size; | |
2298 tdata->curtrans = 0; | |
2299 | |
2300 tdata->curfle = tdata->curfle->next; | |
2301 tdata->next_file = 1; | |
2302 tdata->skip_file = 0; | |
2303 tdata->cancel = 0; | |
2304 tdata->fromreq->cancel = 0; | |
2305 tdata->toreq->cancel = 0; | |
2306 } | |
2307 else | |
2308 { | |
2309 tempfle->transfer_action = GFTP_TRANS_ACTION_RESUME; | |
2310 tempfle->startsize = tdata->curtrans + tdata->curresumed; | |
2311 /* We decrement this here because it will be incremented in | |
2312 the loop again */ | |
2313 tdata->curresumed = 0; | |
2314 tdata->current_file_number--; /* Decrement this because it | |
2315 will be incremented when we | |
2316 continue in the loop */ | |
2317 } | |
2318 | |
2319 gettimeofday (&tdata->starttime, NULL); | |
2320 | |
2321 if (g_thread_supported ()) | |
2322 g_static_mutex_unlock (&tdata->structmutex); | |
2323 | |
2324 return (GFTP_ERETRYABLE); | |
2325 } | |
2326 else if (ret1 == GFTP_EFATAL || ret2 == GFTP_EFATAL) | |
2327 { | |
2328 gftp_disconnect (tdata->fromreq); | |
2329 gftp_disconnect (tdata->toreq); | |
2330 return (GFTP_EFATAL); | |
2331 } | |
2332 else | |
2333 tdata->current_file_retries++; | |
2334 } | |
2335 } | |
2336 else if (tdata->cancel) | |
2337 return (GFTP_EFATAL); | |
2338 | |
2339 return (0); | |
2340 } | |
2341 |