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