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