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