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