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