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