Mercurial > gftp.yaz
annotate lib/protocols.c @ 182:33b394ebba68
2003-6-15 Brian Masney <masneyb@gftp.org>
* lib/cache.c lib/gftp.h - added gftp_generate_cache_description().
* lib/cache.c lib/gftp.h src/text/gftp-text.c src/gtk/delete-dialog.c
src/gtk/menu-items.c src/gtk/misc-gtk.c src/gtk/mkdir-dialog.c - Added
description parameter to gftp_delete_cache_entry().
* lib/protocols.c lib/gftp.h - added gftp_fd_open(). It will call
open() and then set the socket option close on exec
* lib/cache.c lib/local.c lib/misc.c - use gftp_fd_open() instead of
open()
* lib/rfc959.c lib/protocols.c - on newly created sockets, make sure
the close on exec socket option is set
* lib/options.h src/text/gftp-text.c src/gtk/transfer.c - added
preserve_permissions option
* lib/protocols.c (gftp_parse_url) - allow an @ to be in the username
* src/text/gftp-text.c - after transfering a file, honor
preserve_permissions if it is set
* src/gtk/delete-dialog.c - improvments to clearing the expired cache
entries
author | masneyb |
---|---|
date | Sun, 15 Jun 2003 21:28:02 +0000 |
parents | 8d933999bba6 |
children | 65eb40fb4f03 |
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 |
1 | 67 memset (request, 0, sizeof (*request)); |
67 | 68 |
69 if (free_request) | |
70 g_free (request); | |
71 else | |
72 { | |
73 request->datafd = -1; | |
74 request->cachefd = -1; | |
75 } | |
136 | 76 |
143 | 77 if (request->local_options_vars != NULL) |
78 { | |
79 g_free (request->local_options_vars); | |
80 g_hash_table_destroy (request->local_options_hash); | |
81 } | |
1 | 82 } |
83 | |
84 | |
85 void | |
86 gftp_file_destroy (gftp_file * file) | |
87 { | |
88 g_return_if_fail (file != NULL); | |
89 | |
90 if (file->file) | |
91 g_free (file->file); | |
92 if (file->user) | |
93 g_free (file->user); | |
94 if (file->group) | |
95 g_free (file->group); | |
96 if (file->attribs) | |
97 g_free (file->attribs); | |
98 if (file->destfile) | |
99 g_free (file->destfile); | |
100 memset (file, 0, sizeof (*file)); | |
101 } | |
102 | |
103 | |
104 int | |
105 gftp_connect (gftp_request * request) | |
106 { | |
177 | 107 int ret; |
108 | |
84 | 109 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 110 |
111 if (request->connect == NULL) | |
84 | 112 return (GFTP_EFATAL); |
1 | 113 |
177 | 114 if ((ret = gftp_set_config_options (request)) < 0) |
115 return (ret); | |
1 | 116 |
117 return (request->connect (request)); | |
118 } | |
119 | |
120 | |
121 void | |
122 gftp_disconnect (gftp_request * request) | |
123 { | |
124 g_return_if_fail (request != NULL); | |
125 | |
126 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
151 | 127 if (request->free_hostp && request->hostp != NULL) |
1 | 128 freeaddrinfo (request->hostp); |
129 #endif | |
130 request->hostp = NULL; | |
131 | |
168 | 132 #ifdef USE_SSL |
133 if (request->ssl != NULL) | |
134 { | |
135 SSL_free (request->ssl); | |
136 request->ssl = NULL; | |
137 } | |
138 #endif | |
139 | |
1 | 140 request->cached = 0; |
141 if (request->disconnect == NULL) | |
142 return; | |
143 request->disconnect (request); | |
144 } | |
145 | |
146 | |
58 | 147 off_t |
148 gftp_get_file (gftp_request * request, const char *filename, int fd, | |
1 | 149 size_t startsize) |
150 { | |
122 | 151 float maxkbs; |
152 | |
84 | 153 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 154 |
122 | 155 gftp_lookup_request_option (request, "maxkbs", &maxkbs); |
156 if (maxkbs > 0) | |
157 { | |
158 request->logging_function (gftp_logging_misc, request->user_data, | |
159 _("File transfer will be throttled to %.2f KB/s\n"), | |
160 maxkbs); | |
161 } | |
162 | |
1 | 163 request->cached = 0; |
164 if (request->get_file == NULL) | |
84 | 165 return (GFTP_EFATAL); |
1 | 166 return (request->get_file (request, filename, fd, startsize)); |
167 } | |
168 | |
169 | |
170 int | |
58 | 171 gftp_put_file (gftp_request * request, const char *filename, int fd, |
1 | 172 size_t startsize, size_t totalsize) |
173 { | |
122 | 174 float maxkbs; |
175 | |
84 | 176 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 177 |
122 | 178 gftp_lookup_request_option (request, "maxkbs", &maxkbs); |
179 | |
180 if (maxkbs > 0) | |
181 { | |
182 request->logging_function (gftp_logging_misc, request->user_data, | |
183 _("File transfer will be throttled to %.2f KB/s\n"), | |
184 maxkbs); | |
185 } | |
186 | |
1 | 187 request->cached = 0; |
188 if (request->put_file == NULL) | |
84 | 189 return (GFTP_EFATAL); |
1 | 190 return (request->put_file (request, filename, fd, startsize, totalsize)); |
191 } | |
192 | |
193 | |
194 long | |
195 gftp_transfer_file (gftp_request * fromreq, const char *fromfile, | |
58 | 196 int fromfd, size_t fromsize, |
1 | 197 gftp_request * toreq, const char *tofile, |
58 | 198 int tofd, size_t tosize) |
1 | 199 { |
200 long size; | |
84 | 201 int ret; |
1 | 202 |
84 | 203 g_return_val_if_fail (fromreq != NULL, GFTP_EFATAL); |
204 g_return_val_if_fail (fromfile != NULL, GFTP_EFATAL); | |
205 g_return_val_if_fail (toreq != NULL, GFTP_EFATAL); | |
206 g_return_val_if_fail (tofile != NULL, GFTP_EFATAL); | |
207 | |
87 | 208 if (fromreq->protonum == toreq->protonum && |
84 | 209 fromreq->transfer_file != NULL) |
210 return (fromreq->transfer_file (fromreq, fromfile, fromsize, toreq, | |
211 tofile, tosize)); | |
1 | 212 |
213 fromreq->cached = 0; | |
214 toreq->cached = 0; | |
215 if ((size = gftp_get_file (fromreq, fromfile, fromfd, tosize)) < 0) | |
84 | 216 return (size); |
1 | 217 |
84 | 218 if ((ret = gftp_put_file (toreq, tofile, tofd, tosize, size)) != 0) |
1 | 219 { |
40 | 220 if (gftp_abort_transfer (fromreq) != 0) |
221 gftp_end_transfer (fromreq); | |
222 | |
84 | 223 return (ret); |
1 | 224 } |
225 | |
226 return (size); | |
227 } | |
228 | |
229 | |
58 | 230 ssize_t |
1 | 231 gftp_get_next_file_chunk (gftp_request * request, char *buf, size_t size) |
232 { | |
84 | 233 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
234 g_return_val_if_fail (buf != NULL, GFTP_EFATAL); | |
1 | 235 |
236 if (request->get_next_file_chunk != NULL) | |
237 return (request->get_next_file_chunk (request, buf, size)); | |
238 | |
168 | 239 return (request->read_function (request, buf, size, request->datafd)); |
1 | 240 } |
241 | |
242 | |
58 | 243 ssize_t |
1 | 244 gftp_put_next_file_chunk (gftp_request * request, char *buf, size_t size) |
245 { | |
84 | 246 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
247 g_return_val_if_fail (buf != NULL, GFTP_EFATAL); | |
1 | 248 |
249 if (request->put_next_file_chunk != NULL) | |
250 return (request->put_next_file_chunk (request, buf, size)); | |
251 | |
252 if (size == 0) | |
253 return (0); | |
254 | |
168 | 255 return (request->write_function (request, buf, size, request->datafd)); |
1 | 256 } |
257 | |
258 | |
259 int | |
260 gftp_end_transfer (gftp_request * request) | |
261 { | |
262 int ret; | |
263 | |
84 | 264 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 265 |
40 | 266 if (!request->cached && |
267 request->end_transfer != NULL) | |
268 ret = request->end_transfer (request); | |
269 else | |
270 ret = 0; | |
1 | 271 |
58 | 272 if (request->cachefd > 0) |
1 | 273 { |
58 | 274 close (request->cachefd); |
275 request->cachefd = -1; | |
1 | 276 } |
277 | |
278 if (request->last_dir_entry) | |
279 { | |
280 g_free (request->last_dir_entry); | |
281 request->last_dir_entry = NULL; | |
282 request->last_dir_entry_len = 0; | |
283 } | |
284 | |
285 return (ret); | |
286 } | |
287 | |
288 | |
289 int | |
40 | 290 gftp_abort_transfer (gftp_request * request) |
291 { | |
84 | 292 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
40 | 293 |
294 if (request->abort_transfer == NULL) | |
84 | 295 return (GFTP_EFATAL); |
40 | 296 |
297 return (request->abort_transfer (request)); | |
298 } | |
299 | |
300 | |
301 int | |
1 | 302 gftp_list_files (gftp_request * request) |
303 { | |
58 | 304 int fd; |
1 | 305 |
84 | 306 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 307 |
308 request->cached = 0; | |
58 | 309 if (request->use_cache && (fd = gftp_find_cache_entry (request)) > 0) |
1 | 310 { |
311 request->logging_function (gftp_logging_misc, request->user_data, | |
312 _("Loading directory listing %s from cache\n"), | |
313 request->directory); | |
314 | |
315 request->cachefd = fd; | |
316 request->cached = 1; | |
317 return (0); | |
318 } | |
319 else if (request->use_cache) | |
320 { | |
321 request->cachefd = gftp_new_cache_entry (request); | |
322 request->cached = 0; | |
323 } | |
324 | |
325 if (request->list_files == NULL) | |
84 | 326 return (GFTP_EFATAL); |
1 | 327 return (request->list_files (request)); |
328 } | |
329 | |
330 | |
331 int | |
332 gftp_get_next_file (gftp_request * request, char *filespec, gftp_file * fle) | |
333 { | |
58 | 334 int fd, ret; |
45 | 335 #if GLIB_MAJOR_VERSION > 1 |
336 gsize bread, bwrite; | |
337 char *tempstr; | |
46 | 338 GError * error; |
45 | 339 #endif |
1 | 340 |
84 | 341 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 342 |
343 if (request->get_next_file == NULL) | |
84 | 344 return (GFTP_EFATAL); |
1 | 345 |
58 | 346 if (request->cached && request->cachefd > 0) |
1 | 347 fd = request->cachefd; |
348 else | |
349 fd = request->datafd; | |
350 | |
351 memset (fle, 0, sizeof (*fle)); | |
352 do | |
353 { | |
354 gftp_file_destroy (fle); | |
355 ret = request->get_next_file (request, fle, fd); | |
356 | |
45 | 357 #if GLIB_MAJOR_VERSION > 1 |
102 | 358 if (ret >= 0 && fle->file != NULL && !g_utf8_validate (fle->file, -1, NULL)) |
45 | 359 { |
46 | 360 error = NULL; |
45 | 361 if ((tempstr = g_locale_to_utf8 (fle->file, -1, &bread, |
46 | 362 &bwrite, &error)) != NULL) |
45 | 363 { |
364 g_free (fle->file); | |
365 fle->file = tempstr; | |
366 } | |
46 | 367 else |
368 g_warning ("Error when converting %s to UTF-8: %s\n", fle->file, | |
369 error->message); | |
45 | 370 } |
371 #endif | |
372 | |
60 | 373 if (ret >= 0 && !request->cached && request->cachefd > 0 && |
1 | 374 request->last_dir_entry != NULL) |
375 { | |
168 | 376 if (gftp_fd_write (request, request->last_dir_entry, |
60 | 377 request->last_dir_entry_len, request->cachefd) < 0) |
1 | 378 { |
379 request->logging_function (gftp_logging_error, request->user_data, | |
380 _("Error: Cannot write to cache: %s\n"), | |
381 g_strerror (errno)); | |
60 | 382 close (request->cachefd); |
383 request->cachefd = -1; | |
1 | 384 } |
385 } | |
386 } while (ret > 0 && !gftp_match_filespec (fle->file, filespec)); | |
387 | |
388 return (ret); | |
389 } | |
390 | |
391 | |
392 int | |
87 | 393 gftp_parse_bookmark (gftp_request * request, const char * bookmark) |
394 { | |
395 gftp_logging_func logging_function; | |
122 | 396 gftp_bookmarks_var * tempentry; |
397 char *default_protocol; | |
173 | 398 int i, init_ret; |
87 | 399 |
400 g_return_val_if_fail (request != NULL, GFTP_EFATAL); | |
401 g_return_val_if_fail (bookmark != NULL, GFTP_EFATAL); | |
402 | |
403 logging_function = request->logging_function; | |
404 gftp_request_destroy (request, 0); | |
405 request->logging_function = logging_function; | |
406 | |
122 | 407 if ((tempentry = g_hash_table_lookup (gftp_bookmarks_htable, |
408 bookmark)) == NULL) | |
87 | 409 { |
410 request->logging_function (gftp_logging_error, request->user_data, | |
411 _("Error: Could not find bookmark %s\n"), | |
412 bookmark); | |
413 return (GFTP_EFATAL); | |
414 } | |
415 else if (tempentry->hostname == NULL || *tempentry->hostname == '\0') | |
416 { | |
417 request->logging_function (gftp_logging_error, request->user_data, | |
418 _("Bookmarks Error: The bookmark entry %s does not have a hostname\n"), bookmark); | |
419 return (GFTP_EFATAL); | |
420 } | |
421 | |
422 if (tempentry->user != NULL) | |
423 gftp_set_username (request, tempentry->user); | |
424 | |
425 if (tempentry->pass != NULL) | |
122 | 426 gftp_set_password (request, tempentry->pass); |
87 | 427 |
428 if (tempentry->acct != NULL) | |
429 gftp_set_account (request, tempentry->acct); | |
430 | |
431 gftp_set_hostname (request, tempentry->hostname); | |
432 gftp_set_directory (request, tempentry->remote_dir); | |
433 gftp_set_port (request, tempentry->port); | |
434 | |
435 for (i = 0; gftp_protocols[i].name; i++) | |
436 { | |
437 if (strcmp (gftp_protocols[i].name, tempentry->protocol) == 0) | |
438 { | |
173 | 439 if ((init_ret = gftp_protocols[i].init (request)) < 0) |
440 { | |
441 gftp_request_destroy (request, 0); | |
442 return (init_ret); | |
443 } | |
87 | 444 break; |
445 } | |
446 } | |
447 | |
122 | 448 if (gftp_protocols[i].name == NULL) |
87 | 449 { |
122 | 450 gftp_lookup_request_option (request, "default_protocol", |
451 &default_protocol); | |
452 | |
125 | 453 if (default_protocol != NULL && *default_protocol != '\0') |
87 | 454 { |
122 | 455 for (i = 0; gftp_protocols[i].url_prefix; i++) |
456 { | |
457 if (strcmp (gftp_protocols[i].name, default_protocol) == 0) | |
458 break; | |
459 } | |
87 | 460 } |
461 | |
462 if (gftp_protocols[i].url_prefix == NULL) | |
463 i = GFTP_FTP_NUM; | |
464 } | |
465 | |
173 | 466 if ((init_ret = gftp_protocols[i].init (request)) < 0) |
467 { | |
468 gftp_request_destroy (request, 0); | |
469 return (init_ret); | |
470 } | |
471 | |
87 | 472 return (0); |
473 } | |
474 | |
475 | |
476 int | |
1 | 477 gftp_parse_url (gftp_request * request, const char *url) |
478 { | |
168 | 479 char *pos, *endpos, *endhostpos, tempchar, *default_protocol, *stpos; |
67 | 480 gftp_logging_func logging_function; |
173 | 481 int len, i, init_ret; |
168 | 482 const char *cpos; |
1 | 483 |
84 | 484 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
485 g_return_val_if_fail (url != NULL, GFTP_EFATAL); | |
1 | 486 |
67 | 487 logging_function = request->logging_function; |
488 gftp_request_destroy (request, 0); | |
489 request->logging_function = logging_function; | |
490 | |
168 | 491 for (cpos = url; *cpos == ' '; cpos++); |
492 stpos = g_strdup (cpos); | |
493 for (pos = stpos + strlen (stpos) - 1; *pos == ' '; pos--) | |
494 *pos = '\0'; | |
1 | 495 |
122 | 496 i = GFTP_FTP_NUM; |
497 | |
1 | 498 if ((pos = strstr (stpos, "://")) != NULL) |
499 { | |
500 *pos = '\0'; | |
501 | |
502 for (i = 0; gftp_protocols[i].url_prefix; i++) | |
503 { | |
504 if (strcmp (gftp_protocols[i].url_prefix, stpos) == 0) | |
505 break; | |
506 } | |
507 | |
508 if (gftp_protocols[i].url_prefix == NULL) | |
168 | 509 { |
173 | 510 request->logging_function (gftp_logging_misc, NULL, |
511 _("The protocol '%s' is currently not supported.\n"), | |
512 stpos); | |
168 | 513 g_free (stpos); |
514 return (GFTP_EFATAL); | |
515 } | |
173 | 516 |
517 *pos = ':'; | |
1 | 518 } |
519 else | |
520 { | |
122 | 521 gftp_lookup_request_option (request, "default_protocol", |
522 &default_protocol); | |
523 | |
125 | 524 if (default_protocol != NULL && *default_protocol != '\0') |
1 | 525 { |
122 | 526 for (i = 0; gftp_protocols[i].url_prefix; i++) |
527 { | |
528 if (strcmp (gftp_protocols[i].name, default_protocol) == 0) | |
529 break; | |
530 } | |
1 | 531 } |
122 | 532 } |
1 | 533 |
122 | 534 if (gftp_protocols[i].url_prefix == NULL) |
535 i = GFTP_FTP_NUM; | |
1 | 536 |
173 | 537 if ((init_ret = gftp_protocols[i].init (request)) < 0) |
538 { | |
539 gftp_request_destroy (request, 0); | |
540 return (init_ret); | |
541 } | |
1 | 542 |
543 if (request->parse_url != NULL) | |
168 | 544 { |
545 g_free (stpos); | |
546 return (request->parse_url (request, url)); | |
547 } | |
548 | |
549 pos = stpos; | |
550 len = strlen (request->url_prefix); | |
551 if (strncmp (pos, request->url_prefix, len) == 0 && | |
552 strncmp (pos + len, "://", 3) == 0) | |
553 pos += len + 3; | |
1 | 554 |
555 if (i == GFTP_LOCAL_NUM) | |
556 { | |
168 | 557 request->directory = g_strdup (pos); |
558 g_free (stpos); | |
1 | 559 return (0); |
560 } | |
561 | |
182 | 562 if ((endhostpos = strrchr (pos, '@')) != NULL) |
1 | 563 { |
564 /* A user/password was entered */ | |
565 if ((endpos = strchr (pos, ':')) == NULL || endhostpos < endpos) | |
168 | 566 { |
567 /* No password was entered */ | |
568 gftp_set_password (request, ""); | |
569 endpos = endhostpos; | |
570 } | |
1 | 571 |
572 *endpos = '\0'; | |
573 gftp_set_username (request, pos); | |
574 | |
575 pos = endpos + 1; | |
576 if ((endpos = strchr (pos, '@')) != NULL) | |
168 | 577 { |
578 if (strchr (endpos + 1, '@') != NULL) | |
579 endpos = strchr (endpos + 1, '@'); | |
580 *endpos = '\0'; | |
581 gftp_set_password (request, pos); | |
1 | 582 |
168 | 583 pos = endpos + 1; |
584 } | |
1 | 585 } |
586 | |
587 /* Now get the hostname and an optional port and optional directory */ | |
588 endhostpos = pos + strlen (pos); | |
589 if ((endpos = strchr (pos, ':')) != NULL) | |
590 endhostpos = endpos; | |
591 else if ((endpos = strchr (pos, '/')) != NULL) | |
592 endhostpos = endpos; | |
593 tempchar = *endhostpos; | |
594 *endhostpos = '\0'; | |
595 gftp_set_hostname (request, pos); | |
596 *endhostpos = tempchar; | |
597 | |
598 if ((endpos = strchr (pos, ':')) != NULL) | |
599 { | |
600 /* A port was entered */ | |
601 pos = endpos + 1; | |
602 gftp_set_port (request, strtol (pos, NULL, 10)); | |
603 } | |
604 if ((endpos = strchr (pos, '/')) != NULL) | |
605 gftp_set_directory (request, endpos); | |
168 | 606 g_free (stpos); |
1 | 607 return (0); |
608 } | |
609 | |
610 | |
611 void | |
612 gftp_set_hostname (gftp_request * request, const char *hostname) | |
613 { | |
614 g_return_if_fail (request != NULL); | |
615 g_return_if_fail (hostname != NULL); | |
616 | |
617 if (request->hostname) | |
618 g_free (request->hostname); | |
124 | 619 request->hostname = g_strdup (hostname); |
1 | 620 } |
621 | |
622 | |
623 void | |
624 gftp_set_username (gftp_request * request, const char *username) | |
625 { | |
626 g_return_if_fail (request != NULL); | |
627 g_return_if_fail (username != NULL); | |
628 | |
629 if (request->username) | |
630 g_free (request->username); | |
169 | 631 |
632 if (username != NULL) | |
633 request->username = g_strdup (username); | |
634 else | |
635 request->username = NULL; | |
1 | 636 } |
637 | |
638 | |
639 void | |
640 gftp_set_password (gftp_request * request, const char *password) | |
641 { | |
642 g_return_if_fail (request != NULL); | |
643 g_return_if_fail (password != NULL); | |
644 | |
645 if (request->password) | |
646 g_free (request->password); | |
169 | 647 |
648 if (password != NULL) | |
649 request->password = g_strdup (password); | |
650 else | |
651 request->password = NULL; | |
1 | 652 } |
653 | |
654 | |
655 void | |
656 gftp_set_account (gftp_request * request, const char *account) | |
657 { | |
658 g_return_if_fail (request != NULL); | |
659 g_return_if_fail (account != NULL); | |
660 | |
661 if (request->account) | |
662 g_free (request->account); | |
124 | 663 request->account = g_strdup (account); |
1 | 664 } |
665 | |
666 | |
667 int | |
668 gftp_set_directory (gftp_request * request, const char *directory) | |
669 { | |
84 | 670 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
671 g_return_val_if_fail (directory != NULL, GFTP_EFATAL); | |
1 | 672 |
673 | |
169 | 674 if (request->datafd <= 0 && !request->always_connected) |
1 | 675 { |
676 if (directory != request->directory) | |
168 | 677 { |
678 if (request->directory) | |
679 g_free (request->directory); | |
680 request->directory = g_strdup (directory); | |
681 } | |
1 | 682 return (0); |
683 } | |
684 else if (request->chdir == NULL) | |
84 | 685 return (GFTP_EFATAL); |
1 | 686 return (request->chdir (request, directory)); |
687 } | |
688 | |
689 | |
690 void | |
691 gftp_set_port (gftp_request * request, unsigned int port) | |
692 { | |
693 g_return_if_fail (request != NULL); | |
694 | |
695 request->port = port; | |
696 } | |
697 | |
698 | |
699 int | |
700 gftp_remove_directory (gftp_request * request, const char *directory) | |
701 { | |
84 | 702 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 703 |
704 if (request->rmdir == NULL) | |
84 | 705 return (GFTP_EFATAL); |
1 | 706 return (request->rmdir (request, directory)); |
707 } | |
708 | |
709 | |
710 int | |
711 gftp_remove_file (gftp_request * request, const char *file) | |
712 { | |
84 | 713 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 714 |
715 if (request->rmfile == NULL) | |
84 | 716 return (GFTP_EFATAL); |
1 | 717 return (request->rmfile (request, file)); |
718 } | |
719 | |
720 | |
721 int | |
722 gftp_make_directory (gftp_request * request, const char *directory) | |
723 { | |
84 | 724 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 725 |
726 if (request->mkdir == NULL) | |
84 | 727 return (GFTP_EFATAL); |
1 | 728 return (request->mkdir (request, directory)); |
729 } | |
730 | |
731 | |
732 int | |
733 gftp_rename_file (gftp_request * request, const char *oldname, | |
168 | 734 const char *newname) |
1 | 735 { |
84 | 736 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 737 |
738 if (request->rename == NULL) | |
84 | 739 return (GFTP_EFATAL); |
1 | 740 return (request->rename (request, oldname, newname)); |
741 } | |
742 | |
743 | |
744 int | |
745 gftp_chmod (gftp_request * request, const char *file, int mode) | |
746 { | |
84 | 747 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 748 |
749 if (request->chmod == NULL) | |
84 | 750 return (GFTP_EFATAL); |
1 | 751 return (request->chmod (request, file, mode)); |
752 } | |
753 | |
754 | |
755 int | |
756 gftp_set_file_time (gftp_request * request, const char *file, time_t datetime) | |
757 { | |
84 | 758 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 759 |
760 if (request->set_file_time == NULL) | |
84 | 761 return (GFTP_EFATAL); |
1 | 762 return (request->set_file_time (request, file, datetime)); |
763 } | |
764 | |
765 | |
766 char | |
767 gftp_site_cmd (gftp_request * request, const char *command) | |
768 { | |
84 | 769 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 770 |
771 if (request->site == NULL) | |
84 | 772 return (GFTP_EFATAL); |
1 | 773 return (request->site (request, command)); |
774 } | |
775 | |
776 | |
58 | 777 off_t |
1 | 778 gftp_get_file_size (gftp_request * request, const char *filename) |
779 { | |
780 g_return_val_if_fail (request != NULL, 0); | |
781 | |
782 if (request->get_file_size == NULL) | |
783 return (0); | |
784 return (request->get_file_size (request, filename)); | |
785 } | |
786 | |
787 | |
122 | 788 static int |
789 gftp_need_proxy (gftp_request * request, char *service, char *proxy_hostname, | |
790 int proxy_port) | |
1 | 791 { |
122 | 792 gftp_config_list_vars * proxy_hosts; |
1 | 793 gftp_proxy_hosts * hostname; |
794 unsigned char addy[4]; | |
795 struct sockaddr *addr; | |
796 GList * templist; | |
797 gint32 netaddr; | |
798 char *pos; | |
799 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
800 struct addrinfo hints; | |
801 int port, errnum; | |
802 char serv[8]; | |
122 | 803 #endif |
804 | |
805 gftp_lookup_global_option ("ext", &proxy_hosts); | |
806 | |
807 if (proxy_hostname == NULL || *proxy_hostname == '\0') | |
808 return (0); | |
809 else if (proxy_hosts->list == NULL) | |
810 return (proxy_hostname != NULL && | |
811 *proxy_hostname != '\0'); | |
1 | 812 |
813 request->hostp = NULL; | |
122 | 814 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) |
151 | 815 request->free_hostp = 1; |
1 | 816 memset (&hints, 0, sizeof (hints)); |
817 hints.ai_flags = AI_CANONNAME; | |
146 | 818 hints.ai_family = PF_UNSPEC; |
1 | 819 hints.ai_socktype = SOCK_STREAM; |
820 | |
122 | 821 port = request->use_proxy ? proxy_port : request->port; |
1 | 822 if (port == 0) |
823 strcpy (serv, service); | |
824 else | |
825 snprintf (serv, sizeof (serv), "%d", port); | |
826 | |
827 request->logging_function (gftp_logging_misc, request->user_data, | |
828 _("Looking up %s\n"), request->hostname); | |
829 | |
830 if ((errnum = getaddrinfo (request->hostname, serv, &hints, | |
831 &request->hostp)) != 0) | |
832 { | |
833 request->logging_function (gftp_logging_error, request->user_data, | |
834 _("Cannot look up hostname %s: %s\n"), | |
835 request->hostname, gai_strerror (errnum)); | |
84 | 836 return (GFTP_ERETRYABLE); |
1 | 837 } |
838 | |
839 addr = request->hostp->ai_addr; | |
840 | |
841 #else /* !HAVE_GETADDRINFO */ | |
842 request->logging_function (gftp_logging_misc, request->user_data, | |
843 _("Looking up %s\n"), request->hostname); | |
844 | |
845 if (!(request->hostp = r_gethostbyname (request->hostname, &request->host, | |
846 NULL))) | |
847 { | |
848 request->logging_function (gftp_logging_error, request->user_data, | |
849 _("Cannot look up hostname %s: %s\n"), | |
850 request->hostname, g_strerror (errno)); | |
84 | 851 return (GFTP_ERETRYABLE); |
1 | 852 } |
853 | |
854 addr = (struct sockaddr *) request->host.h_addr_list[0]; | |
855 | |
856 #endif /* HAVE_GETADDRINFO */ | |
857 | |
122 | 858 templist = proxy_hosts->list; |
1 | 859 while (templist != NULL) |
860 { | |
861 hostname = templist->data; | |
862 if (hostname->domain && | |
863 strlen (request->hostname) > strlen (hostname->domain)) | |
168 | 864 { |
865 pos = request->hostname + strlen (request->hostname) - | |
866 strlen (hostname->domain); | |
867 if (strcmp (hostname->domain, pos) == 0) | |
868 return (0); | |
869 } | |
1 | 870 |
871 if (hostname->ipv4_network_address != 0) | |
168 | 872 { |
873 memcpy (addy, addr, sizeof (addy)); | |
874 netaddr = | |
875 (((addy[0] & 0xff) << 24) | ((addy[1] & 0xff) << 16) | | |
876 ((addy[2] & 0xff) << 8) | (addy[3] & 0xff)) & | |
1 | 877 hostname->ipv4_netmask; |
168 | 878 if (netaddr == hostname->ipv4_network_address) |
879 return (0); | |
880 } | |
1 | 881 templist = templist->next; |
882 } | |
883 | |
122 | 884 return (proxy_hostname != NULL && *proxy_hostname != '\0'); |
1 | 885 } |
886 | |
887 | |
48 | 888 static char * |
889 copy_token (char **dest, char *source) | |
1 | 890 { |
48 | 891 /* This function is used internally by gftp_parse_ls () */ |
892 char *endpos, savepos; | |
1 | 893 |
48 | 894 endpos = source; |
895 while (*endpos != ' ' && *endpos != '\t' && *endpos != '\0') | |
896 endpos++; | |
897 if (*endpos == '\0') | |
898 return (NULL); | |
1 | 899 |
48 | 900 savepos = *endpos; |
901 *endpos = '\0'; | |
902 *dest = g_malloc (endpos - source + 1); | |
903 strcpy (*dest, source); | |
904 *endpos = savepos; | |
1 | 905 |
48 | 906 /* Skip the blanks till we get to the next entry */ |
907 source = endpos + 1; | |
908 while ((*source == ' ' || *source == '\t') && *source != '\0') | |
909 source++; | |
910 return (source); | |
911 } | |
1 | 912 |
913 | |
48 | 914 static char * |
915 goto_next_token (char *pos) | |
916 { | |
917 while (*pos != ' ' && *pos != '\t' && *pos != '\0') | |
918 pos++; | |
1 | 919 |
48 | 920 if (pos == '\0') |
921 return (pos); | |
1 | 922 |
48 | 923 while ((*pos == ' ' || *pos == '\t') && *pos != '\0') |
924 pos++; | |
925 | |
926 return (pos); | |
1 | 927 } |
928 | |
929 | |
102 | 930 time_t |
931 parse_time (char *str, char **endpos) | |
1 | 932 { |
102 | 933 struct tm curtime, *loctime; |
105 | 934 time_t t, ret; |
102 | 935 char *tmppos; |
168 | 936 int i; |
1 | 937 |
102 | 938 memset (&curtime, 0, sizeof (curtime)); |
939 curtime.tm_isdst = -1; | |
105 | 940 if (strlen (str) > 4 && isdigit ((int) str[0]) && str[2] == '-' && isdigit ((int) str[3])) |
1 | 941 { |
942 /* This is how DOS will return the date/time */ | |
943 /* 07-06-99 12:57PM */ | |
944 | |
102 | 945 tmppos = strptime (str, "%m-%d-%y %I:%M%p", &curtime); |
1 | 946 } |
105 | 947 else if (strlen (str) > 4 && isdigit ((int) str[0]) && str[2] == '-' && isalpha (str[3])) |
948 { | |
949 /* 10-Jan-2003 09:14 */ | |
950 tmppos = strptime (str, "%d-%h-%Y %H:%M", &curtime); | |
951 } | |
1 | 952 else |
953 { | |
954 /* This is how most UNIX, Novell, and MacOS ftp servers send their time */ | |
102 | 955 /* Jul 06 12:57 or Jul 6 1999 */ |
1 | 956 |
102 | 957 if (strchr (str, ':') != NULL) |
958 { | |
959 tmppos = strptime (str, "%h %d %H:%M", &curtime); | |
960 t = time (NULL); | |
961 loctime = localtime (&t); | |
962 curtime.tm_year = loctime->tm_year; | |
963 } | |
964 else | |
965 tmppos = strptime (str, "%h %d %Y", &curtime); | |
966 } | |
1 | 967 |
105 | 968 if (tmppos != NULL) |
969 ret = mktime (&curtime); | |
970 else | |
971 ret = 0; | |
972 | |
102 | 973 if (endpos != NULL) |
105 | 974 { |
975 if (tmppos == NULL) | |
976 { | |
977 /* We cannot parse this date format. So, just skip this date field and continue to the next | |
978 token. This is mainly for the HTTP support */ | |
979 | |
168 | 980 for (i=0, *endpos = str; (*endpos)[i] != ' ' && (*endpos)[i] != '\t' && (*endpos)[i] != '\0'; i++); |
981 endpos += i; | |
105 | 982 } |
983 else | |
984 *endpos = tmppos; | |
985 } | |
1 | 986 |
102 | 987 return (mktime (&curtime)); |
1 | 988 } |
989 | |
990 | |
107 | 991 static void |
992 gftp_parse_vms_attribs (char *dest, char **src) | |
993 { | |
994 char *endpos; | |
995 | |
996 if (*src == NULL) | |
997 return; | |
998 | |
999 if ((endpos = strchr (*src, ',')) != NULL) | |
1000 *endpos = '\0'; | |
1001 | |
1002 if (strchr (*src, 'R') != NULL) | |
1003 *dest = 'r'; | |
1004 if (strchr (*src, 'W') != NULL) | |
1005 *(dest + 1) = 'w'; | |
1006 if (strchr (*src, 'E') != NULL) | |
1007 *(dest + 2) = 'x'; | |
1008 | |
1009 *src = endpos + 1; | |
1010 } | |
1011 | |
1012 | |
1013 static int | |
1014 gftp_parse_ls_vms (char *str, gftp_file * fle) | |
1015 { | |
1016 char *curpos, *endpos; | |
1017 | |
1018 /* .PINE-DEBUG1;1 9 21-AUG-2002 20:06 [MYERSRG] (RWED,RWED,,) */ | |
1019 /* WWW.DIR;1 1 23-NOV-1999 05:47 [MYERSRG] (RWE,RWE,RE,E) */ | |
1020 | |
1021 if ((curpos = strchr (str, ';')) == NULL) | |
1022 return (GFTP_EFATAL); | |
1023 | |
1024 *curpos = '\0'; | |
1025 fle->attribs = g_strdup ("----------"); | |
1026 if (strlen (str) > 4 && strcmp (curpos - 4, ".DIR") == 0) | |
1027 { | |
1028 fle->isdir = 1; | |
1029 *fle->attribs = 'd'; | |
1030 *(curpos - 4) = '\0'; | |
1031 } | |
1032 | |
1033 fle->file = g_strdup (str); | |
1034 | |
1035 curpos = goto_next_token (curpos + 1); | |
122 | 1036 fle->size = strtol (curpos, NULL, 10) * 512; /* Is this correct? */ |
107 | 1037 curpos = goto_next_token (curpos); |
1038 | |
1039 if ((fle->datetime = parse_time (curpos, &curpos)) == 0) | |
1040 return (GFTP_EFATAL); | |
1041 | |
1042 curpos = goto_next_token (curpos); | |
1043 | |
1044 if (*curpos != '[') | |
1045 return (GFTP_EFATAL); | |
1046 curpos++; | |
1047 if ((endpos = strchr (curpos, ']')) == NULL) | |
1048 return (GFTP_EFATAL); | |
1049 *endpos = '\0'; | |
1050 fle->user = g_strdup (curpos); | |
1051 fle->group = g_strdup (""); | |
1052 | |
1053 curpos = goto_next_token (endpos + 1); | |
1054 if ((curpos = strchr (curpos, ',')) == NULL) | |
1055 return (0); | |
1056 curpos++; | |
1057 | |
1058 gftp_parse_vms_attribs (fle->attribs + 1, &curpos); | |
1059 gftp_parse_vms_attribs (fle->attribs + 4, &curpos); | |
1060 gftp_parse_vms_attribs (fle->attribs + 7, &curpos); | |
1061 | |
1062 return (0); | |
1063 } | |
1064 | |
1065 | |
1 | 1066 static int |
1067 gftp_parse_ls_eplf (char *str, gftp_file * fle) | |
1068 { | |
1069 char *startpos; | |
1070 | |
1071 startpos = str; | |
124 | 1072 fle->attribs = g_strdup ("----------"); |
1 | 1073 while (startpos) |
1074 { | |
1075 startpos++; | |
1076 switch (*startpos) | |
168 | 1077 { |
1078 case '/': | |
1079 *fle->attribs = 'd'; | |
1080 break; | |
1081 case 's': | |
1082 fle->size = strtol (startpos + 1, NULL, 10); | |
1083 break; | |
1084 case 'm': | |
1085 fle->datetime = strtol (startpos + 1, NULL, 10); | |
1086 break; | |
1087 } | |
1 | 1088 startpos = strchr (startpos, ','); |
1089 } | |
1090 if ((startpos = strchr (str, 9)) == NULL) | |
84 | 1091 return (GFTP_EFATAL); |
124 | 1092 fle->file = g_strdup (startpos + 1); |
1093 fle->user = g_strdup (_("unknown")); | |
1094 fle->group = g_strdup (_("unknown")); | |
1 | 1095 return (0); |
1096 } | |
1097 | |
1098 | |
1099 static int | |
91 | 1100 gftp_parse_ls_unix (gftp_request * request, char *str, gftp_file * fle) |
1 | 1101 { |
91 | 1102 char *endpos, *startpos, *pos; |
1103 int cols; | |
1104 | |
1105 /* If there is no space between the attribs and links field, just make one */ | |
1106 if (strlen (str) > 10) | |
1107 str[10] = ' '; | |
1108 | |
1109 /* Determine the number of columns */ | |
1110 cols = 0; | |
1111 pos = str; | |
1112 while (*pos != '\0') | |
1113 { | |
1114 while (*pos != '\0' && *pos != ' ' && *pos != '\t') | |
1115 { | |
1116 if (*pos == ':') | |
1117 break; | |
1118 pos++; | |
1119 } | |
1120 | |
1121 cols++; | |
1122 | |
1123 if (*pos == ':') | |
1124 { | |
1125 cols++; | |
1126 break; | |
1127 } | |
1128 | |
1129 while (*pos == ' ' || *pos == '\t') | |
1130 pos++; | |
1131 } | |
1 | 1132 |
1133 startpos = str; | |
1134 /* Copy file attributes */ | |
1135 if ((startpos = copy_token (&fle->attribs, startpos)) == NULL) | |
84 | 1136 return (GFTP_EFATAL); |
1 | 1137 |
1138 if (cols >= 9) | |
1139 { | |
1140 /* Skip the number of links */ | |
1141 startpos = goto_next_token (startpos); | |
1142 | |
1143 /* Copy the user that owns this file */ | |
1144 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
168 | 1145 return (GFTP_EFATAL); |
1 | 1146 |
1147 /* Copy the group that owns this file */ | |
1148 if ((startpos = copy_token (&fle->group, startpos)) == NULL) | |
168 | 1149 return (GFTP_EFATAL); |
1 | 1150 } |
1151 else | |
1152 { | |
124 | 1153 fle->group = g_strdup (_("unknown")); |
1 | 1154 if (cols == 8) |
168 | 1155 { |
1156 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
1157 return (GFTP_EFATAL); | |
1158 } | |
1 | 1159 else |
124 | 1160 fle->user = g_strdup (_("unknown")); |
1 | 1161 startpos = goto_next_token (startpos); |
1162 } | |
1163 | |
136 | 1164 if (request->server_type != GFTP_DIRTYPE_CRAY) |
1 | 1165 { |
91 | 1166 /* See if this is a Cray directory listing. It has the following format: |
1167 drwx------ 2 feiliu g913 DK common 4096 Sep 24 2001 wv */ | |
1168 if (cols == 11 && strstr (str, "->") == NULL) | |
1169 { | |
1170 startpos = goto_next_token (startpos); | |
1171 startpos = goto_next_token (startpos); | |
1172 } | |
1 | 1173 } |
1174 | |
1175 /* See if this is a block or character device. We will store the major number | |
1176 in the high word and the minor number in the low word. */ | |
1177 if ((fle->attribs[0] == 'b' || fle->attribs[0] == 'u' || | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1178 fle->attribs[0] == 'c') && |
1 | 1179 ((endpos = strchr (startpos, ',')) != NULL)) |
1180 { | |
1181 fle->size = strtol (startpos, NULL, 10) << 16; | |
1182 | |
1183 startpos = endpos + 1; | |
1184 while (*startpos == ' ') | |
168 | 1185 startpos++; |
1 | 1186 |
1187 /* Get the minor number */ | |
1188 if ((endpos = strchr (startpos, ' ')) == NULL) | |
168 | 1189 return (GFTP_EFATAL); |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1190 fle->size |= strtol (startpos, NULL, 10) & 0xFF; |
1 | 1191 } |
1192 else | |
1193 { | |
1194 /* This is a regular file */ | |
1195 if ((endpos = strchr (startpos, ' ')) == NULL) | |
168 | 1196 return (GFTP_EFATAL); |
1 | 1197 fle->size = strtol (startpos, NULL, 10); |
1198 } | |
1199 | |
1200 /* Skip the blanks till we get to the next entry */ | |
1201 startpos = endpos + 1; | |
1202 while (*startpos == ' ') | |
1203 startpos++; | |
1204 | |
102 | 1205 if ((fle->datetime = parse_time (startpos, &startpos)) == 0) |
84 | 1206 return (GFTP_EFATAL); |
1 | 1207 |
1208 /* Skip the blanks till we get to the next entry */ | |
1209 startpos = goto_next_token (startpos); | |
1210 | |
1211 /* Parse the filename. If this file is a symbolic link, remove the -> part */ | |
1212 if (fle->attribs[0] == 'l' && ((endpos = strstr (startpos, "->")) != NULL)) | |
1213 *(endpos - 1) = '\0'; | |
1214 | |
124 | 1215 fle->file = g_strdup (startpos); |
1 | 1216 |
1217 /* Uncomment this if you want to strip the spaces off of the end of the file. | |
1218 I don't want to do this by default since there are valid filenames with | |
1219 spaces at the end of them. Some broken FTP servers like the Paradyne IPC | |
1220 DSLAMS append a bunch of spaces at the end of the file. | |
1221 for (endpos = fle->file + strlen (fle->file) - 1; | |
1222 *endpos == ' '; | |
1223 *endpos-- = '\0'); | |
1224 */ | |
1225 | |
1226 return (0); | |
1227 } | |
1228 | |
1229 | |
1230 static int | |
1231 gftp_parse_ls_nt (char *str, gftp_file * fle) | |
1232 { | |
1233 char *startpos; | |
1234 | |
1235 startpos = str; | |
102 | 1236 if ((fle->datetime = parse_time (startpos, &startpos)) == 0) |
84 | 1237 return (GFTP_EFATAL); |
1 | 1238 |
124 | 1239 fle->user = g_strdup (_("unknown")); |
1240 fle->group = g_strdup (_("unknown")); | |
1 | 1241 |
1242 startpos = goto_next_token (startpos); | |
1243 if (startpos[0] == '<') | |
124 | 1244 fle->attribs = g_strdup ("drwxrwxrwx"); |
1 | 1245 else |
1246 { | |
124 | 1247 fle->attribs = g_strdup ("-rw-rw-rw-"); |
1 | 1248 fle->size = strtol (startpos, NULL, 10); |
1249 } | |
124 | 1250 |
1 | 1251 startpos = goto_next_token (startpos); |
124 | 1252 fle->file = g_strdup (startpos); |
1 | 1253 return (0); |
1254 } | |
1255 | |
1256 | |
1257 static int | |
1258 gftp_parse_ls_novell (char *str, gftp_file * fle) | |
1259 { | |
1260 char *startpos; | |
1261 | |
1262 if (str[12] != ' ') | |
84 | 1263 return (GFTP_EFATAL); |
1 | 1264 str[12] = '\0'; |
124 | 1265 fle->attribs = g_strdup (str); |
1 | 1266 startpos = str + 13; |
1267 | |
1268 while ((*startpos == ' ' || *startpos == '\t') && *startpos != '\0') | |
1269 startpos++; | |
1270 | |
1271 if ((startpos = copy_token (&fle->user, startpos)) == NULL) | |
84 | 1272 return (GFTP_EFATAL); |
1 | 1273 |
124 | 1274 fle->group = g_strdup (_("unknown")); |
1 | 1275 |
1276 fle->size = strtol (startpos, NULL, 10); | |
1277 | |
1278 startpos = goto_next_token (startpos); | |
102 | 1279 if ((fle->datetime = parse_time (startpos, &startpos)) == 0) |
84 | 1280 return (GFTP_EFATAL); |
1 | 1281 |
1282 startpos = goto_next_token (startpos); | |
124 | 1283 fle->file = g_strdup (startpos); |
1 | 1284 return (0); |
1285 } | |
1286 | |
1287 | |
48 | 1288 int |
91 | 1289 gftp_parse_ls (gftp_request * request, const char *lsoutput, gftp_file * fle) |
1 | 1290 { |
107 | 1291 char *str, *endpos, tmpchar; |
1292 int result, is_vms; | |
91 | 1293 size_t len; |
48 | 1294 |
84 | 1295 g_return_val_if_fail (lsoutput != NULL, GFTP_EFATAL); |
1296 g_return_val_if_fail (fle != NULL, GFTP_EFATAL); | |
48 | 1297 |
124 | 1298 str = g_strdup (lsoutput); |
48 | 1299 memset (fle, 0, sizeof (*fle)); |
1 | 1300 |
91 | 1301 len = strlen (str); |
107 | 1302 if (len > 0 && str[len - 1] == '\n') |
91 | 1303 str[--len] = '\0'; |
1304 if (len > 0 && str[len - 1] == '\r') | |
1305 str[--len] = '\0'; | |
39 | 1306 |
91 | 1307 switch (request->server_type) |
1308 { | |
122 | 1309 case GFTP_DIRTYPE_CRAY: |
1310 case GFTP_DIRTYPE_UNIX: | |
1311 result = gftp_parse_ls_unix (request, str, fle); | |
91 | 1312 break; |
122 | 1313 case GFTP_DIRTYPE_EPLF: |
91 | 1314 result = gftp_parse_ls_eplf (str, fle); |
1315 break; | |
122 | 1316 case GFTP_DIRTYPE_NOVELL: |
91 | 1317 result = gftp_parse_ls_novell (str, fle); |
1318 break; | |
122 | 1319 case GFTP_DIRTYPE_DOS: |
91 | 1320 result = gftp_parse_ls_nt (str, fle); |
1321 break; | |
122 | 1322 case GFTP_DIRTYPE_VMS: |
107 | 1323 result = gftp_parse_ls_vms (str, fle); |
1324 break; | |
91 | 1325 default: /* autodetect */ |
1326 if (*lsoutput == '+') | |
1327 result = gftp_parse_ls_eplf (str, fle); | |
1328 else if (isdigit ((int) str[0]) && str[2] == '-') | |
1329 result = gftp_parse_ls_nt (str, fle); | |
1330 else if (str[1] == ' ' && str[2] == '[') | |
1331 result = gftp_parse_ls_novell (str, fle); | |
1332 else | |
107 | 1333 { |
1334 if ((endpos = strchr (str, ' ')) != NULL) | |
1335 { | |
1336 /* If the first token in the string has a ; in it, then */ | |
1337 /* we'll assume that this is a VMS directory listing */ | |
1338 tmpchar = *endpos; | |
1339 *endpos = '\0'; | |
1340 is_vms = strchr (str, ';') != NULL; | |
1341 *endpos = tmpchar; | |
1342 } | |
1343 else | |
1344 is_vms = 0; | |
48 | 1345 |
107 | 1346 if (is_vms) |
1347 result = gftp_parse_ls_vms (str, fle); | |
1348 else | |
1349 result = gftp_parse_ls_unix (request, str, fle); | |
1350 } | |
91 | 1351 break; |
48 | 1352 } |
1353 g_free (str); | |
1354 | |
1355 if (fle->attribs == NULL) | |
1356 return (result); | |
1357 | |
1358 if (*fle->attribs == 'd') | |
1359 fle->isdir = 1; | |
1360 if (*fle->attribs == 'l') | |
1361 fle->islink = 1; | |
1362 if (strchr (fle->attribs, 'x') != NULL && !fle->isdir && !fle->islink) | |
1363 fle->isexe = 1; | |
1364 | |
1365 return (result); | |
1 | 1366 } |
1367 | |
1368 | |
48 | 1369 static GHashTable * |
1370 gftp_gen_dir_hash (gftp_request * request) | |
1 | 1371 { |
48 | 1372 unsigned long *newsize; |
1373 GHashTable * dirhash; | |
1374 gftp_file * fle; | |
1375 char * newname; | |
1376 | |
39 | 1377 |
48 | 1378 dirhash = g_hash_table_new (string_hash_function, string_hash_compare); |
1379 if (gftp_list_files (request) == 0) | |
1380 { | |
1381 fle = g_malloc0 (sizeof (*fle)); | |
1382 while (gftp_get_next_file (request, NULL, fle) > 0) | |
1383 { | |
1384 newname = fle->file; | |
1385 newsize = g_malloc (sizeof (unsigned long)); | |
1386 *newsize = fle->size; | |
1387 g_hash_table_insert (dirhash, newname, newsize); | |
1388 fle->file = NULL; | |
1389 gftp_file_destroy (fle); | |
1390 } | |
1391 gftp_end_transfer (request); | |
1392 g_free (fle); | |
1393 } | |
1394 else | |
1395 { | |
1396 g_hash_table_destroy (dirhash); | |
1397 dirhash = NULL; | |
1398 } | |
1399 return (dirhash); | |
1400 } | |
39 | 1401 |
48 | 1402 |
1403 static void | |
1404 destroy_hash_ent (gpointer key, gpointer value, gpointer user_data) | |
1405 { | |
39 | 1406 |
48 | 1407 g_free (key); |
1408 g_free (value); | |
1409 } | |
1410 | |
1411 | |
1412 static void | |
1413 gftp_destroy_dir_hash (GHashTable * dirhash) | |
1414 { | |
1415 g_hash_table_foreach (dirhash, destroy_hash_ent, NULL); | |
1416 g_hash_table_destroy (dirhash); | |
1 | 1417 } |
1418 | |
1419 | |
1420 static GList * | |
1421 gftp_get_dir_listing (gftp_transfer * transfer, int getothdir) | |
1422 { | |
1423 unsigned long *newsize; | |
1424 GHashTable * dirhash; | |
1425 GList * templist; | |
1426 gftp_file * fle; | |
1427 char *newname; | |
1428 | |
1429 if (getothdir && transfer->toreq) | |
1430 dirhash = gftp_gen_dir_hash (transfer->toreq); | |
1431 else | |
1432 dirhash = NULL; | |
1433 | |
1434 if (gftp_list_files (transfer->fromreq) != 0) | |
1435 return (NULL); | |
1436 | |
1437 fle = g_malloc (sizeof (*fle)); | |
1438 templist = NULL; | |
1439 while (gftp_get_next_file (transfer->fromreq, NULL, fle) > 0) | |
1440 { | |
1441 if (strcmp (fle->file, ".") == 0 || strcmp (fle->file, "..") == 0) | |
1442 { | |
1443 gftp_file_destroy (fle); | |
1444 continue; | |
1445 } | |
1446 | |
1447 if (dirhash && | |
1448 (newsize = g_hash_table_lookup (dirhash, fle->file)) != NULL) | |
1449 fle->startsize = *newsize; | |
1450 | |
1451 if (transfer->toreq) | |
1452 fle->destfile = g_strconcat (transfer->toreq->directory, "/", | |
1453 fle->file, NULL); | |
1454 newname = g_strconcat (transfer->fromreq->directory, "/", fle->file, NULL); | |
1455 g_free (fle->file); | |
1456 fle->file = newname; | |
1457 | |
1458 templist = g_list_append (templist, fle); | |
1459 | |
1460 fle = g_malloc (sizeof (*fle)); | |
1461 } | |
1462 gftp_end_transfer (transfer->fromreq); | |
1463 | |
1464 gftp_file_destroy (fle); | |
1465 g_free (fle); | |
1466 | |
1467 if (dirhash) | |
1468 gftp_destroy_dir_hash (dirhash); | |
1469 | |
1470 | |
1471 return (templist); | |
1472 } | |
1473 | |
1474 | |
1475 int | |
1476 gftp_get_all_subdirs (gftp_transfer * transfer, | |
1477 void (*update_func) (gftp_transfer * transfer)) | |
1478 { | |
1479 char *oldfromdir, *oldtodir, *newname; | |
1480 GList * templist, * lastlist; | |
1481 int forcecd, remotechanged; | |
1482 unsigned long *newsize; | |
1483 GHashTable * dirhash; | |
1484 gftp_file * curfle; | |
1485 | |
84 | 1486 g_return_val_if_fail (transfer != NULL, GFTP_EFATAL); |
1487 g_return_val_if_fail (transfer->fromreq != NULL, GFTP_EFATAL); | |
1488 g_return_val_if_fail (transfer->files != NULL, GFTP_EFATAL); | |
1 | 1489 |
1490 if (transfer->toreq != NULL) | |
1491 dirhash = gftp_gen_dir_hash (transfer->toreq); | |
1492 else | |
1493 dirhash = NULL; | |
1494 | |
1495 for (lastlist = transfer->files; ; lastlist = lastlist->next) | |
1496 { | |
1497 curfle = lastlist->data; | |
1498 if (dirhash && | |
1499 (newsize = g_hash_table_lookup (dirhash, curfle->file)) != NULL) | |
1500 curfle->startsize = *newsize; | |
1501 | |
1502 if (transfer->toreq) | |
1503 curfle->destfile = g_strconcat (transfer->toreq->directory, "/", | |
1504 curfle->file, NULL); | |
1505 newname = g_strconcat (transfer->fromreq->directory, transfer->fromreq->directory[strlen (transfer->fromreq->directory) - 1] == '/' ? "" : "/", curfle->file, NULL); | |
1506 g_free (curfle->file); | |
1507 curfle->file = newname; | |
1508 | |
1509 if (lastlist->next == NULL) | |
1510 break; | |
1511 } | |
1512 | |
1513 if (dirhash) | |
1514 gftp_destroy_dir_hash (dirhash); | |
1515 | |
1516 oldfromdir = oldtodir = NULL; | |
1517 remotechanged = 0; | |
1518 forcecd = 0; | |
1519 for (templist = transfer->files; templist != NULL; templist = templist->next) | |
1520 { | |
1521 curfle = templist->data; | |
1522 | |
1523 if (curfle->isdir) | |
1524 { | |
1525 oldfromdir = transfer->fromreq->directory; | |
1526 transfer->fromreq->directory = curfle->file; | |
1527 | |
1528 if (transfer->toreq) | |
1529 { | |
1530 oldtodir = transfer->toreq->directory; | |
1531 transfer->toreq->directory = curfle->destfile; | |
1532 } | |
1533 forcecd = 1; | |
1534 if (gftp_set_directory (transfer->fromreq, | |
1535 transfer->fromreq->directory) == 0) | |
1536 { | |
1537 if (curfle->startsize > 0 && transfer->toreq) | |
1538 { | |
1539 remotechanged = 1; | |
1540 if (gftp_set_directory (transfer->toreq, | |
1541 transfer->toreq->directory) != 0) | |
1542 curfle->startsize = 0; | |
1543 } | |
1544 | |
1545 lastlist->next = gftp_get_dir_listing (transfer, | |
1546 curfle->startsize > 0); | |
1547 if (lastlist->next != NULL) | |
1548 { | |
1549 lastlist->next->prev = lastlist; | |
1550 for (; lastlist->next != NULL; lastlist = lastlist->next); | |
1551 } | |
1552 transfer->numdirs++; | |
1553 if (update_func) | |
1554 update_func (transfer); | |
1555 } | |
1556 else | |
1557 curfle->transfer_action = GFTP_TRANS_ACTION_SKIP; | |
1558 | |
1559 transfer->fromreq->directory = oldfromdir; | |
1560 if (transfer->toreq) | |
1561 transfer->toreq->directory = oldtodir; | |
1562 } | |
1563 else | |
122 | 1564 transfer->numfiles++; |
1 | 1565 } |
1566 | |
1567 if (forcecd) | |
1568 gftp_set_directory (transfer->fromreq, transfer->fromreq->directory); | |
1569 if (remotechanged && transfer->toreq) | |
1570 gftp_set_directory (transfer->toreq, transfer->toreq->directory); | |
1571 | |
1572 if (update_func) | |
1573 { | |
1574 transfer->numfiles = transfer->numdirs = -1; | |
1575 update_func (transfer); | |
1576 } | |
1577 return (0); | |
1578 } | |
1579 | |
1580 | |
1581 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
122 | 1582 static int |
1 | 1583 get_port (struct addrinfo *addr) |
1584 { | |
1585 struct sockaddr_in * saddr; | |
1586 int port; | |
1587 | |
1588 if (addr->ai_family == AF_INET) | |
1589 { | |
1590 saddr = (struct sockaddr_in *) addr->ai_addr; | |
1591 port = ntohs (saddr->sin_port); | |
1592 } | |
1593 else | |
1594 port = 0; | |
1595 | |
1596 return (port); | |
1597 } | |
1598 #endif | |
1599 | |
1600 | |
1601 int | |
122 | 1602 gftp_connect_server (gftp_request * request, char *service, |
1603 char *proxy_hostname, int proxy_port) | |
1 | 1604 { |
1605 char *connect_host, *disphost; | |
1606 int port, sock; | |
1607 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GAI_STRERROR) | |
1608 struct addrinfo hints, *res; | |
1609 char serv[8]; | |
1610 int errnum; | |
1611 | |
122 | 1612 if ((request->use_proxy = gftp_need_proxy (request, service, |
1613 proxy_hostname, proxy_port)) < 0) | |
84 | 1614 return (request->use_proxy); |
1 | 1615 else if (request->use_proxy == 1) |
1616 request->hostp = NULL; | |
1617 | |
151 | 1618 request->free_hostp = 1; |
1 | 1619 memset (&hints, 0, sizeof (hints)); |
1620 hints.ai_flags = AI_CANONNAME; | |
146 | 1621 hints.ai_family = PF_UNSPEC; |
1 | 1622 hints.ai_socktype = SOCK_STREAM; |
1623 | |
122 | 1624 if (request->use_proxy) |
1625 { | |
1626 connect_host = proxy_hostname; | |
1627 port = proxy_port; | |
1628 } | |
1629 else | |
1630 { | |
1631 connect_host = request->hostname; | |
1632 port = request->port; | |
1633 } | |
1 | 1634 |
1635 if (request->hostp == NULL) | |
1636 { | |
1637 if (port == 0) | |
1638 strcpy (serv, service); | |
1639 else | |
1640 snprintf (serv, sizeof (serv), "%d", port); | |
1641 | |
1642 request->logging_function (gftp_logging_misc, request->user_data, | |
168 | 1643 _("Looking up %s\n"), connect_host); |
1 | 1644 if ((errnum = getaddrinfo (connect_host, serv, &hints, |
1645 &request->hostp)) != 0) | |
168 | 1646 { |
1647 request->logging_function (gftp_logging_error, request->user_data, | |
1648 _("Cannot look up hostname %s: %s\n"), | |
1649 connect_host, gai_strerror (errnum)); | |
1650 return (GFTP_ERETRYABLE); | |
1651 } | |
1 | 1652 } |
1653 | |
1654 disphost = connect_host; | |
1655 for (res = request->hostp; res != NULL; res = res->ai_next) | |
1656 { | |
1657 disphost = res->ai_canonname ? res->ai_canonname : connect_host; | |
1658 port = get_port (res); | |
56 | 1659 if (!request->use_proxy) |
1660 request->port = port; | |
1661 | |
1 | 1662 if ((sock = socket (res->ai_family, res->ai_socktype, |
1663 res->ai_protocol)) < 0) | |
1664 { | |
168 | 1665 request->logging_function (gftp_logging_error, request->user_data, |
1 | 1666 _("Failed to create a socket: %s\n"), |
1667 g_strerror (errno)); | |
1668 continue; | |
66 | 1669 } |
1 | 1670 |
1671 request->logging_function (gftp_logging_misc, request->user_data, | |
168 | 1672 _("Trying %s:%d\n"), disphost, port); |
1 | 1673 |
1674 if (connect (sock, res->ai_addr, res->ai_addrlen) == -1) | |
168 | 1675 { |
1676 request->logging_function (gftp_logging_error, request->user_data, | |
1677 _("Cannot connect to %s: %s\n"), | |
1678 disphost, g_strerror (errno)); | |
1 | 1679 close (sock); |
1680 continue; | |
168 | 1681 } |
1 | 1682 break; |
1683 } | |
1684 | |
1685 if (res == NULL) | |
1686 { | |
1687 if (request->hostp != NULL) | |
1688 { | |
1689 freeaddrinfo (request->hostp); | |
1690 request->hostp = NULL; | |
1691 } | |
84 | 1692 return (GFTP_ERETRYABLE); |
1 | 1693 } |
1694 | |
1695 #else /* !HAVE_GETADDRINFO */ | |
1696 struct sockaddr_in remote_address; | |
1697 struct servent serv_struct; | |
1698 int curhost; | |
1699 | |
122 | 1700 if ((request->use_proxy = gftp_need_proxy (request, service, |
1701 proxy_hostname, proxy_port)) < 0) | |
84 | 1702 return (request->use_proxy); |
1 | 1703 else if (request->use_proxy == 1) |
1704 request->hostp = NULL; | |
1705 | |
1706 if ((sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) | |
1707 { | |
1708 request->logging_function (gftp_logging_error, request->user_data, | |
1709 _("Failed to create a socket: %s\n"), | |
1710 g_strerror (errno)); | |
84 | 1711 return (GFTP_ERETRYABLE); |
1 | 1712 } |
1713 | |
1714 memset (&remote_address, 0, sizeof (remote_address)); | |
1715 remote_address.sin_family = AF_INET; | |
1716 | |
122 | 1717 if (request->use_proxy) |
1718 { | |
1719 connect_host = proxy_hostname; | |
1720 port = proxy_port; | |
1721 } | |
1722 else | |
1723 { | |
1724 connect_host = request->hostname; | |
1725 port = request->port; | |
1726 } | |
1 | 1727 |
1728 if (port == 0) | |
1729 { | |
1730 if (!r_getservbyname (service, "tcp", &serv_struct, NULL)) | |
1731 { | |
122 | 1732 request->logging_function (gftp_logging_error, request->user_data, |
1733 _("Cannot look up service name %s/tcp. Please check your services file\n"), | |
1734 service); | |
1735 close (sock); | |
1736 return (GFTP_EFATAL); | |
1 | 1737 } |
1738 else | |
1739 { | |
1740 port = serv_struct.s_port; | |
1741 request->port = ntohs (serv_struct.s_port); | |
1742 } | |
56 | 1743 |
1744 if (!request->use_proxy) | |
1745 request->port = ntohs (port); | |
1 | 1746 } |
1747 remote_address.sin_port = port; | |
1748 | |
1749 if (request->hostp == NULL) | |
1750 { | |
1751 request->logging_function (gftp_logging_misc, request->user_data, | |
168 | 1752 _("Looking up %s\n"), connect_host); |
1 | 1753 if (!(request->hostp = r_gethostbyname (connect_host, &request->host, |
1754 NULL))) | |
1755 { | |
1756 request->logging_function (gftp_logging_error, request->user_data, | |
1757 _("Cannot look up hostname %s: %s\n"), | |
1758 connect_host, g_strerror (errno)); | |
1759 close (sock); | |
84 | 1760 return (GFTP_ERETRYABLE); |
1 | 1761 } |
1762 } | |
1763 | |
1764 disphost = NULL; | |
1765 for (curhost = 0; request->host.h_addr_list[curhost] != NULL; curhost++) | |
1766 { | |
1767 disphost = request->host.h_name; | |
1768 memcpy (&remote_address.sin_addr, request->host.h_addr_list[curhost], | |
1769 request->host.h_length); | |
1770 request->logging_function (gftp_logging_misc, request->user_data, | |
1771 _("Trying %s:%d\n"), | |
1772 request->host.h_name, ntohs (port)); | |
1773 | |
1774 if (connect (sock, (struct sockaddr *) &remote_address, | |
1775 sizeof (remote_address)) == -1) | |
1776 { | |
1777 request->logging_function (gftp_logging_error, request->user_data, | |
1778 _("Cannot connect to %s: %s\n"), | |
1779 connect_host, g_strerror (errno)); | |
1780 } | |
1781 break; | |
1782 } | |
1783 | |
1784 if (request->host.h_addr_list[curhost] == NULL) | |
1785 { | |
1786 close (sock); | |
84 | 1787 return (GFTP_ERETRYABLE); |
1 | 1788 } |
1789 port = ntohs (port); | |
1790 #endif /* HAVE_GETADDRINFO */ | |
1791 | |
182 | 1792 if (fcntl (sock, F_SETFD, 1) == -1) |
1793 { | |
1794 request->logging_function (gftp_logging_error, request->user_data, | |
1795 _("Error: Cannot set close on exec flag: %s\n"), | |
1796 g_strerror (errno)); | |
1797 | |
1798 return (GFTP_ERETRYABLE); | |
1799 } | |
1800 | |
1 | 1801 request->logging_function (gftp_logging_misc, request->user_data, |
168 | 1802 _("Connected to %s:%d\n"), connect_host, port); |
58 | 1803 |
168 | 1804 if (gftp_fd_set_sockblocking (request, sock, 1) < 0) |
58 | 1805 { |
1806 close (sock); | |
84 | 1807 return (GFTP_ERETRYABLE); |
58 | 1808 } |
1809 | |
169 | 1810 request->datafd = sock; |
168 | 1811 |
1812 if (request->post_connect != NULL) | |
1813 return (request->post_connect (request)); | |
1814 | |
1815 return (0); | |
1 | 1816 } |
1817 | |
1818 | |
177 | 1819 int |
1 | 1820 gftp_set_config_options (gftp_request * request) |
1821 { | |
58 | 1822 if (request->set_config_options != NULL) |
177 | 1823 return (request->set_config_options (request)); |
1824 else | |
1825 return (0); | |
1 | 1826 } |
1827 | |
1828 | |
1829 void | |
1830 print_file_list (GList * list) | |
1831 { | |
1832 gftp_file * tempfle; | |
1833 GList * templist; | |
1834 | |
1835 printf ("--START OF FILE LISTING - TOP TO BOTTOM--\n"); | |
1836 for (templist = list; ; templist = templist->next) | |
1837 { | |
1838 tempfle = templist->data; | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1839 #if defined (_LARGEFILE_SOURCE) |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1840 printf ("%s:%s:%lld:%lld:%s:%s:%s\n", |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1841 #else |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1842 printf ("%s:%s:%ld:%ld:%s:%s:%s\n", |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1843 #endif |
16 | 1844 tempfle->file, tempfle->destfile, |
1845 tempfle->size, tempfle->startsize, | |
1846 tempfle->user, tempfle->group, tempfle->attribs); | |
1 | 1847 if (templist->next == NULL) |
1848 break; | |
1849 } | |
1850 | |
1851 printf ("--START OF FILE LISTING - BOTTOM TO TOP--\n"); | |
1852 for (; ; templist = templist->prev) | |
1853 { | |
1854 tempfle = templist->data; | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1855 #if defined (_LARGEFILE_SOURCE) |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1856 printf ("%s:%s:%lld:%lld:%s:%s:%s\n", |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1857 #else |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1858 printf ("%s:%s:%ld:%ld:%s:%s:%s\n", |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
1859 #endif |
16 | 1860 tempfle->file, tempfle->destfile, |
1861 tempfle->size, tempfle->startsize, | |
1862 tempfle->user, tempfle->group, tempfle->attribs); | |
1 | 1863 if (templist == list) |
1864 break; | |
1865 } | |
1866 printf ("--END OF FILE LISTING--\n"); | |
1867 } | |
1868 | |
41 | 1869 |
58 | 1870 static void |
1871 gftp_free_getline_buffer (gftp_getline_buffer ** rbuf) | |
41 | 1872 { |
58 | 1873 g_free ((*rbuf)->buffer); |
1874 g_free (*rbuf); | |
1875 *rbuf = NULL; | |
41 | 1876 } |
1877 | |
1878 | |
58 | 1879 ssize_t |
1880 gftp_get_line (gftp_request * request, gftp_getline_buffer ** rbuf, | |
1881 char * str, size_t len, int fd) | |
41 | 1882 { |
168 | 1883 ssize_t ret, retval, rlen; |
58 | 1884 char *pos, *nextpos; |
168 | 1885 ssize_t (*read_function) (gftp_request * request, void *ptr, size_t size, |
1886 int fd); | |
179 | 1887 int end_of_buffer; |
168 | 1888 |
1889 if (request == NULL || request->read_function == NULL) | |
1890 read_function = gftp_fd_read; | |
1891 else | |
1892 read_function = request->read_function; | |
58 | 1893 |
1894 if (*rbuf == NULL) | |
1895 { | |
1896 *rbuf = g_malloc0 (sizeof (**rbuf)); | |
1897 (*rbuf)->max_bufsize = len; | |
60 | 1898 (*rbuf)->buffer = g_malloc ((*rbuf)->max_bufsize + 1); |
168 | 1899 |
1900 if ((ret = read_function (request, (*rbuf)->buffer, | |
1901 (*rbuf)->max_bufsize, fd)) <= 0) | |
58 | 1902 { |
1903 gftp_free_getline_buffer (rbuf); | |
1904 return (ret); | |
1905 } | |
60 | 1906 (*rbuf)->buffer[ret] = '\0'; |
58 | 1907 (*rbuf)->cur_bufsize = ret; |
1908 (*rbuf)->curpos = (*rbuf)->buffer; | |
1909 } | |
1910 | |
84 | 1911 retval = GFTP_ERETRYABLE; |
58 | 1912 do |
1913 { | |
179 | 1914 pos = strchr ((*rbuf)->curpos, '\n'); |
1915 end_of_buffer = (*rbuf)->curpos == (*rbuf)->buffer && | |
1916 ((*rbuf)->max_bufsize == (*rbuf)->cur_bufsize || (*rbuf)->eof); | |
1917 | |
1918 if ((*rbuf)->cur_bufsize > 0 && (pos != NULL || end_of_buffer)) | |
58 | 1919 { |
1920 if (pos != NULL) | |
168 | 1921 retval = pos - (*rbuf)->curpos + 1; |
1922 else | |
1923 retval = (*rbuf)->cur_bufsize; | |
1924 | |
1925 if (pos != NULL) | |
58 | 1926 { |
1927 nextpos = pos + 1; | |
1928 if (pos > (*rbuf)->curpos && *(pos - 1) == '\r') | |
1929 pos--; | |
1930 *pos = '\0'; | |
1931 } | |
1932 else | |
60 | 1933 nextpos = NULL; |
58 | 1934 |
1935 strncpy (str, (*rbuf)->curpos, len); | |
168 | 1936 (*rbuf)->cur_bufsize -= retval; |
58 | 1937 |
168 | 1938 if (nextpos != NULL) |
1939 (*rbuf)->curpos = nextpos; | |
1940 else | |
1941 (*rbuf)->cur_bufsize = 0; | |
58 | 1942 break; |
1943 } | |
1944 else | |
1945 { | |
1946 if ((*rbuf)->cur_bufsize == 0 || *(*rbuf)->curpos == '\0') | |
1947 { | |
1948 rlen = (*rbuf)->max_bufsize; | |
1949 pos = (*rbuf)->buffer; | |
1950 } | |
1951 else | |
1952 { | |
168 | 1953 memmove ((*rbuf)->buffer, (*rbuf)->curpos, (*rbuf)->cur_bufsize); |
1954 pos = (*rbuf)->buffer + (*rbuf)->cur_bufsize; | |
1955 rlen = (*rbuf)->max_bufsize - (*rbuf)->cur_bufsize; | |
58 | 1956 } |
168 | 1957 |
58 | 1958 (*rbuf)->curpos = (*rbuf)->buffer; |
1959 | |
168 | 1960 if ((ret = read_function (request, pos, rlen, fd)) < 0) |
58 | 1961 { |
1962 gftp_free_getline_buffer (rbuf); | |
1963 return (ret); | |
1964 } | |
179 | 1965 else if (ret == 0) |
168 | 1966 { |
179 | 1967 if ((*rbuf)->cur_bufsize == 0) |
1968 { | |
1969 gftp_free_getline_buffer (rbuf); | |
1970 return (ret); | |
1971 } | |
1972 | |
1973 (*rbuf)->eof = 1; | |
168 | 1974 } |
1975 | |
1976 (*rbuf)->buffer[ret + (*rbuf)->cur_bufsize] = '\0'; | |
1977 (*rbuf)->cur_bufsize += ret; | |
58 | 1978 } |
1979 } | |
84 | 1980 while (retval == GFTP_ERETRYABLE); |
58 | 1981 |
1982 return (retval); | |
1983 } | |
1984 | |
1985 | |
1986 ssize_t | |
168 | 1987 gftp_fd_read (gftp_request * request, void *ptr, size_t size, int fd) |
58 | 1988 { |
122 | 1989 long network_timeout; |
58 | 1990 struct timeval tv; |
1991 fd_set fset; | |
1992 ssize_t ret; | |
41 | 1993 |
122 | 1994 gftp_lookup_request_option (request, "network_timeout", &network_timeout); |
1995 | |
41 | 1996 errno = 0; |
1997 ret = 0; | |
1998 do | |
1999 { | |
58 | 2000 FD_ZERO (&fset); |
2001 FD_SET (fd, &fset); | |
122 | 2002 tv.tv_sec = network_timeout; |
58 | 2003 tv.tv_usec = 0; |
2004 ret = select (fd + 1, &fset, NULL, NULL, &tv); | |
2005 if (ret == -1 && errno == EINTR) | |
41 | 2006 { |
58 | 2007 if (request && request->cancel) |
2008 break; | |
2009 else | |
2010 continue; | |
2011 } | |
2012 else if (ret <= 0) | |
2013 { | |
2014 if (request != NULL) | |
41 | 2015 { |
58 | 2016 request->logging_function (gftp_logging_error, request->user_data, |
2017 _("Connection to %s timed out\n"), | |
2018 request->hostname); | |
2019 gftp_disconnect (request); | |
2020 } | |
84 | 2021 return (GFTP_ERETRYABLE); |
41 | 2022 } |
2023 | |
58 | 2024 if ((ret = read (fd, ptr, size)) < 0) |
41 | 2025 { |
2026 if (errno == EINTR) | |
2027 { | |
58 | 2028 if (request != NULL && request->cancel) |
41 | 2029 break; |
2030 else | |
58 | 2031 continue; |
41 | 2032 } |
2033 | |
58 | 2034 if (request != NULL) |
2035 { | |
2036 request->logging_function (gftp_logging_error, request->user_data, | |
2037 _("Error: Could not read from socket: %s\n"), | |
41 | 2038 g_strerror (errno)); |
58 | 2039 gftp_disconnect (request); |
2040 } | |
84 | 2041 return (GFTP_ERETRYABLE); |
41 | 2042 } |
2043 } | |
58 | 2044 while (errno == EINTR && !(request != NULL && request->cancel)); |
41 | 2045 |
58 | 2046 if (errno == EINTR && request != NULL && request->cancel) |
41 | 2047 { |
2048 gftp_disconnect (request); | |
84 | 2049 return (GFTP_ERETRYABLE); |
41 | 2050 } |
2051 | |
2052 return (ret); | |
2053 } | |
2054 | |
58 | 2055 |
2056 ssize_t | |
168 | 2057 gftp_fd_write (gftp_request * request, const char *ptr, size_t size, int fd) |
58 | 2058 { |
122 | 2059 long network_timeout; |
58 | 2060 struct timeval tv; |
2061 size_t ret, w_ret; | |
2062 fd_set fset; | |
2063 | |
122 | 2064 gftp_lookup_request_option (request, "network_timeout", &network_timeout); |
2065 | |
58 | 2066 errno = 0; |
2067 ret = 0; | |
2068 do | |
2069 { | |
2070 FD_ZERO (&fset); | |
2071 FD_SET (fd, &fset); | |
122 | 2072 tv.tv_sec = network_timeout; |
58 | 2073 tv.tv_usec = 0; |
2074 ret = select (fd + 1, NULL, &fset, NULL, &tv); | |
2075 if (ret == -1 && errno == EINTR) | |
2076 { | |
2077 if (request != NULL && request->cancel) | |
2078 break; | |
2079 else | |
2080 continue; | |
2081 } | |
2082 else if (ret <= 0) | |
2083 { | |
2084 if (request != NULL) | |
2085 { | |
2086 request->logging_function (gftp_logging_error, request->user_data, | |
2087 _("Connection to %s timed out\n"), | |
2088 request->hostname); | |
2089 gftp_disconnect (request); | |
2090 } | |
84 | 2091 return (GFTP_ERETRYABLE); |
58 | 2092 } |
2093 | |
2094 if ((w_ret = write (fd, ptr, size)) < 0) | |
2095 { | |
2096 if (errno == EINTR) | |
2097 { | |
2098 if (request != NULL && request->cancel) | |
2099 break; | |
2100 else | |
2101 continue; | |
2102 } | |
2103 | |
2104 if (request != NULL) | |
2105 { | |
2106 request->logging_function (gftp_logging_error, request->user_data, | |
2107 _("Error: Could not write to socket: %s\n"), | |
2108 g_strerror (errno)); | |
2109 gftp_disconnect (request); | |
2110 } | |
84 | 2111 return (GFTP_ERETRYABLE); |
58 | 2112 } |
2113 | |
2114 ptr += w_ret; | |
2115 size -= w_ret; | |
2116 ret += w_ret; | |
2117 } | |
2118 while (size > 0); | |
2119 | |
2120 if (errno == EINTR && request != NULL && request->cancel) | |
2121 { | |
2122 gftp_disconnect (request); | |
84 | 2123 return (GFTP_ERETRYABLE); |
58 | 2124 } |
2125 | |
2126 return (ret); | |
2127 } | |
2128 | |
2129 | |
2130 ssize_t | |
2131 gftp_writefmt (gftp_request * request, int fd, const char *fmt, ...) | |
2132 { | |
2133 char *tempstr; | |
2134 va_list argp; | |
2135 ssize_t ret; | |
2136 | |
2137 va_start (argp, fmt); | |
2138 tempstr = g_strdup_vprintf (fmt, argp); | |
2139 va_end (argp); | |
2140 | |
168 | 2141 ret = request->write_function (request, tempstr, strlen (tempstr), fd); |
58 | 2142 g_free (tempstr); |
2143 return (ret); | |
2144 } | |
2145 | |
2146 | |
2147 int | |
168 | 2148 gftp_fd_set_sockblocking (gftp_request * request, int fd, int non_blocking) |
58 | 2149 { |
2150 int flags; | |
2151 | |
84 | 2152 if ((flags = fcntl (fd, F_GETFL, 0)) < 0) |
58 | 2153 { |
2154 request->logging_function (gftp_logging_error, request->user_data, | |
2155 _("Cannot get socket flags: %s\n"), | |
2156 g_strerror (errno)); | |
2157 gftp_disconnect (request); | |
84 | 2158 return (GFTP_ERETRYABLE); |
58 | 2159 } |
2160 | |
2161 if (non_blocking) | |
2162 flags |= O_NONBLOCK; | |
2163 else | |
2164 flags &= ~O_NONBLOCK; | |
2165 | |
84 | 2166 if (fcntl (fd, F_SETFL, flags) < 0) |
58 | 2167 { |
2168 request->logging_function (gftp_logging_error, request->user_data, | |
2169 _("Cannot set socket to non-blocking: %s\n"), | |
2170 g_strerror (errno)); | |
2171 gftp_disconnect (request); | |
84 | 2172 return (GFTP_ERETRYABLE); |
58 | 2173 } |
2174 | |
2175 return (0); | |
2176 } | |
2177 | |
2178 | |
63 | 2179 void |
2180 gftp_swap_socks (gftp_request * dest, gftp_request * source) | |
2181 { | |
2182 g_return_if_fail (dest != NULL); | |
2183 g_return_if_fail (source != NULL); | |
2184 g_return_if_fail (dest->protonum == source->protonum); | |
2185 | |
2186 dest->datafd = source->datafd; | |
2187 dest->cached = 0; | |
2188 if (!source->always_connected) | |
2189 { | |
2190 source->datafd = -1; | |
2191 source->cached = 1; | |
2192 } | |
2193 | |
2194 if (dest->swap_socks) | |
2195 dest->swap_socks (dest, source); | |
2196 } | |
2197 | |
122 | 2198 |
2199 void | |
2200 gftp_calc_kbs (gftp_transfer * tdata, ssize_t num_read) | |
2201 { | |
2202 unsigned long waitusecs; | |
2203 double difftime, curkbs; | |
2204 gftp_file * tempfle; | |
2205 unsigned long toadd; | |
2206 struct timeval tv; | |
2207 float maxkbs; | |
2208 | |
2209 gftp_lookup_request_option (tdata->fromreq, "maxkbs", &maxkbs); | |
2210 | |
2211 gettimeofday (&tv, NULL); | |
2212 if (g_thread_supported ()) | |
2213 g_static_mutex_lock (&tdata->statmutex); | |
2214 | |
2215 tempfle = tdata->curfle->data; | |
2216 tdata->trans_bytes += num_read; | |
2217 tdata->curtrans += num_read; | |
2218 tdata->stalled = 0; | |
2219 | |
2220 difftime = (tv.tv_sec - tdata->starttime.tv_sec) + ((double) (tv.tv_usec - tdata->starttime.tv_usec) / 1000000.0); | |
2221 if (difftime <= 0) | |
2222 tdata->kbs = (double) tdata->trans_bytes / 1024.0; | |
2223 else | |
2224 tdata->kbs = (double) tdata->trans_bytes / 1024.0 / difftime; | |
2225 | |
2226 difftime = (tv.tv_sec - tdata->lasttime.tv_sec) + ((double) (tv.tv_usec - tdata->lasttime.tv_usec) / 1000000.0); | |
2227 | |
2228 if (difftime <= 0) | |
2229 curkbs = (double) (num_read / 1024.0); | |
2230 else | |
2231 curkbs = (double) (num_read / 1024.0 / difftime); | |
2232 | |
2233 if (maxkbs > 0 && curkbs > maxkbs) | |
2234 { | |
2235 waitusecs = (double) num_read / 1024.0 / maxkbs * 1000000.0 - difftime; | |
2236 | |
2237 if (waitusecs > 0) | |
2238 { | |
2239 if (g_thread_supported ()) | |
2240 g_static_mutex_unlock (&tdata->statmutex); | |
2241 | |
2242 difftime += ((double) waitusecs / 1000000.0); | |
2243 usleep (waitusecs); | |
2244 | |
2245 if (g_thread_supported ()) | |
2246 g_static_mutex_lock (&tdata->statmutex); | |
2247 } | |
2248 } | |
2249 | |
2250 /* I don't call gettimeofday (&tdata->lasttime) here because this will use | |
2251 less system resources. This will be close enough for what we need */ | |
2252 difftime += tdata->lasttime.tv_usec / 1000000.0; | |
2253 toadd = (long) difftime; | |
2254 difftime -= toadd; | |
2255 tdata->lasttime.tv_sec += toadd; | |
2256 tdata->lasttime.tv_usec = difftime * 1000000.0; | |
2257 | |
2258 if (g_thread_supported ()) | |
2259 g_static_mutex_unlock (&tdata->statmutex); | |
2260 } | |
2261 | |
125 | 2262 |
2263 int | |
2264 gftp_get_transfer_status (gftp_transfer * tdata, ssize_t num_read) | |
2265 { | |
2266 int ret1, ret2, retries, sleep_time; | |
2267 gftp_file * tempfle; | |
2268 struct timeval tv; | |
2269 | |
2270 ret1 = ret2 = 0; | |
2271 gftp_lookup_request_option (tdata->fromreq, "retries", &retries); | |
2272 gftp_lookup_request_option (tdata->fromreq, "sleep_time", &sleep_time); | |
2273 | |
2274 if (g_thread_supported ()) | |
2275 g_static_mutex_lock (&tdata->structmutex); | |
2276 | |
2277 if (tdata->curfle == NULL) | |
2278 { | |
2279 if (g_thread_supported ()) | |
2280 g_static_mutex_unlock (&tdata->structmutex); | |
2281 | |
2282 return (GFTP_EFATAL); | |
2283 } | |
2284 | |
2285 tempfle = tdata->curfle->data; | |
2286 | |
2287 if (g_thread_supported ()) | |
2288 g_static_mutex_unlock (&tdata->structmutex); | |
2289 | |
2290 gftp_disconnect (tdata->fromreq); | |
2291 gftp_disconnect (tdata->toreq); | |
2292 | |
2293 if (num_read < 0 || tdata->skip_file) | |
2294 { | |
2295 if (num_read == GFTP_EFATAL) | |
2296 return (GFTP_EFATAL); | |
2297 else if (retries != 0 && | |
2298 tdata->current_file_retries >= retries) | |
2299 { | |
2300 tdata->fromreq->logging_function (gftp_logging_error, | |
2301 tdata->fromreq->user_data, | |
2302 _("Error: Remote site %s disconnected. Max retries reached...giving up\n"), | |
2303 tdata->fromreq->hostname != NULL ? | |
2304 tdata->fromreq->hostname : tdata->toreq->hostname); | |
2305 return (GFTP_EFATAL); | |
2306 } | |
2307 else | |
2308 { | |
2309 tdata->fromreq->logging_function (gftp_logging_error, | |
2310 tdata->fromreq->user_data, | |
2311 _("Error: Remote site %s disconnected. Will reconnect in %d seconds\n"), | |
2312 tdata->fromreq->hostname != NULL ? | |
2313 tdata->fromreq->hostname : tdata->toreq->hostname, | |
2314 sleep_time); | |
2315 } | |
2316 | |
2317 while (retries == 0 || | |
2318 tdata->current_file_retries <= retries) | |
2319 { | |
2320 if (!tdata->skip_file) | |
2321 { | |
2322 tv.tv_sec = sleep_time; | |
2323 tv.tv_usec = 0; | |
2324 select (0, NULL, NULL, NULL, &tv); | |
2325 } | |
2326 | |
2327 if ((ret1 = gftp_connect (tdata->fromreq)) == 0 && | |
2328 (ret2 = gftp_connect (tdata->toreq)) == 0) | |
2329 { | |
2330 if (g_thread_supported ()) | |
2331 g_static_mutex_lock (&tdata->structmutex); | |
2332 | |
2333 tdata->resumed_bytes = tdata->resumed_bytes + tdata->trans_bytes - tdata->curresumed - tdata->curtrans; | |
2334 tdata->trans_bytes = 0; | |
2335 if (tdata->skip_file) | |
2336 { | |
2337 tdata->total_bytes -= tempfle->size; | |
2338 tdata->curtrans = 0; | |
2339 | |
2340 tdata->curfle = tdata->curfle->next; | |
2341 tdata->next_file = 1; | |
2342 tdata->skip_file = 0; | |
2343 tdata->cancel = 0; | |
2344 tdata->fromreq->cancel = 0; | |
2345 tdata->toreq->cancel = 0; | |
2346 } | |
2347 else | |
2348 { | |
2349 tempfle->transfer_action = GFTP_TRANS_ACTION_RESUME; | |
2350 tempfle->startsize = tdata->curtrans + tdata->curresumed; | |
2351 /* We decrement this here because it will be incremented in | |
2352 the loop again */ | |
2353 tdata->curresumed = 0; | |
2354 tdata->current_file_number--; /* Decrement this because it | |
2355 will be incremented when we | |
2356 continue in the loop */ | |
2357 } | |
2358 | |
2359 gettimeofday (&tdata->starttime, NULL); | |
2360 | |
2361 if (g_thread_supported ()) | |
2362 g_static_mutex_unlock (&tdata->structmutex); | |
2363 | |
2364 return (GFTP_ERETRYABLE); | |
2365 } | |
2366 else if (ret1 == GFTP_EFATAL || ret2 == GFTP_EFATAL) | |
2367 { | |
2368 gftp_disconnect (tdata->fromreq); | |
2369 gftp_disconnect (tdata->toreq); | |
2370 return (GFTP_EFATAL); | |
2371 } | |
2372 else | |
2373 tdata->current_file_retries++; | |
2374 } | |
2375 } | |
2376 else if (tdata->cancel) | |
2377 return (GFTP_EFATAL); | |
2378 | |
2379 return (0); | |
2380 } | |
2381 | |
182 | 2382 |
2383 int | |
2384 gftp_fd_open (gftp_request * request, const char *pathname, int flags, mode_t mode) | |
2385 { | |
2386 mode_t mask; | |
2387 int fd; | |
2388 | |
2389 if (mode == 0 && (flags & O_CREAT)) | |
2390 { | |
2391 mask = umask (0); /* FIXME - improve */ | |
2392 umask (mask); | |
2393 mode = 0666 & ~mask; | |
2394 } | |
2395 | |
2396 if ((fd = open (pathname, flags, mode)) < 0) | |
2397 { | |
2398 if (request != NULL) | |
2399 request->logging_function (gftp_logging_error, request->user_data, | |
2400 _("Error: Cannot open local file %s: %s\n"), | |
2401 pathname, g_strerror (errno)); | |
2402 return (GFTP_ERETRYABLE); | |
2403 } | |
2404 | |
2405 if (fcntl (fd, F_SETFD, 1) == -1) | |
2406 { | |
2407 if (request != NULL) | |
2408 request->logging_function (gftp_logging_error, request->user_data, | |
2409 _("Error: Cannot set close on exec flag: %s\n"), | |
2410 g_strerror (errno)); | |
2411 | |
2412 return (-1); | |
2413 } | |
2414 | |
2415 return (fd); | |
2416 } | |
2417 |