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