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