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