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