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