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