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