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