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