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