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