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