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