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