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