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