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