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