comparison lib/misc.c @ 58:c01d91c10f6c

2002-11-20 Brian Masney <masneyb@gftp.org> * lib/protocols.c lib/gftp.h - added gftp_get_line(), gftp_read(), gftp_write(), gftp_writefmt(), and gftp_set_sockblocking() functions. Added struct_gftp_getline_buffer for gftp_get_line function() * lib/cache.c lib/gftp.h lib/local.c lib/misc.c lib/protocols.c lib/rfc2068.c lib/rfc959.c lib/ssh.c lib/sshv2.c - *_get_file() returns off_t instead of long. *_{get,put}_next_file_chunk returns ssize_t instead of size_t. Added *_set_config_options function to gftp_request structure and protocol files. Use the new network functions documented above. Convert usage of ANSI C IO (FILE *) to standard BSD sockets so that I can use timeouts properly with select * lib/misc.c (ssh_start_login_sequence) - use gftp_set_sockblock(), gftp_read() and gftp_write() functions * lib/protocols.c - move some protocol specific code to the protocol specific files * lib/local.c - log succesful messages to gftp_logging_misc instead of gftp_logging_error * lib/cache.c - log some more error conditions to the user * lib/rfc959.c - added rfc959_getcwd(). In, rfc959_accept_active_connection(), set set socket to blocking mode before calling accept() * src/text/gftk-text.c - If we get no files in gftp_text_ls(), return instead of segfaulting * src/gtk/gftp-gtk.c - expand the port field in the toolbar to be 45 pixels wide * src/text/gftp-text.c src/gtk/misc-gtk.c src/gtk/transfer.c src/gtk/view_dialog.c - changes for conversion of request->{sock,data} from ANSI C IO (FILE *) to standard BSD sockets
author masneyb
date Thu, 21 Nov 2002 00:33:51 +0000
parents a12bcbc2fce4
children 618423504fe0
comparison
equal deleted inserted replaced
57:72f6ca02c83a 58:c01d91c10f6c
257 257
258 258
259 int 259 int
260 copyfile (char *source, char *dest) 260 copyfile (char *source, char *dest)
261 { 261 {
262 FILE *srcfd, *destfd; 262 int srcfd, destfd;
263 char buf[8192]; 263 char buf[8192];
264 size_t n; 264 ssize_t n;
265 265
266 if ((srcfd = fopen (source, "rb")) == NULL) 266 if ((srcfd = open (source, O_RDONLY)) == -1)
267 return (0); 267 {
268 268 printf (_("Error: Cannot open local file %s: %s\n"),
269 if ((destfd = fopen (dest, "wb")) == NULL) 269 source, g_strerror (errno));
270 { 270 exit (1);
271 fclose (srcfd); 271 }
272 return (0); 272
273 } 273 if ((destfd = open (dest, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
274 274 {
275 while ((n = fread (buf, 1, sizeof (buf), srcfd)) > 0) 275 printf (_("Error: Cannot open local file %s: %s\n"),
276 fwrite (buf, 1, n, destfd); 276 dest, g_strerror (errno));
277 277 close (srcfd);
278 fclose (srcfd); 278 exit (1);
279 fclose (destfd); 279 }
280
281 while ((n = read (srcfd, buf, sizeof (buf))) > 0)
282 {
283 if (write (destfd, buf, n) == -1)
284 {
285 printf (_("Error: Could not write to socket: %s\n"),
286 g_strerror (errno));
287 exit (1);
288 }
289 }
290
291 if (n == -1)
292 {
293 printf (_("Error: Could not read from socket: %s\n"), g_strerror (errno));
294 exit (1);
295 }
296
297 close (srcfd);
298 close (destfd);
280 299
281 return (1); 300 return (1);
282 } 301 }
283 302
284 303
427 g_free (fle->group); 446 g_free (fle->group);
428 if (fle->attribs) 447 if (fle->attribs)
429 g_free (fle->attribs); 448 g_free (fle->attribs);
430 if (fle->destfile) 449 if (fle->destfile)
431 g_free (fle->destfile); 450 g_free (fle->destfile);
432 if (fle->fd) 451 if (fle->fd > 0)
433 fclose (fle->fd); 452 close (fle->fd);
434 g_free (fle); 453 g_free (fle);
435 } 454 }
436 455
437 456
438 gftp_file * 457 gftp_file *
479 void 498 void
480 swap_socks (gftp_request * dest, gftp_request * source) 499 swap_socks (gftp_request * dest, gftp_request * source)
481 { 500 {
482 dest->sockfd = source->sockfd; 501 dest->sockfd = source->sockfd;
483 dest->datafd = source->datafd; 502 dest->datafd = source->datafd;
484 dest->sockfd_write = source->sockfd_write;
485 dest->cached = 0; 503 dest->cached = 0;
486 if (!source->always_connected) 504 if (!source->always_connected)
487 { 505 {
488 source->sockfd = NULL; 506 source->sockfd = -1;
489 source->datafd = NULL; 507 source->datafd = -1;
490 source->sockfd_write = NULL;
491 source->cached = 1; 508 source->cached = 1;
492 } 509 }
493 } 510 }
494 511
495 512
582 newreq->proxy_username = NULL; 599 newreq->proxy_username = NULL;
583 newreq->proxy_password = NULL; 600 newreq->proxy_password = NULL;
584 newreq->proxy_account = NULL; 601 newreq->proxy_account = NULL;
585 newreq->last_ftp_response = NULL; 602 newreq->last_ftp_response = NULL;
586 newreq->last_dir_entry = NULL; 603 newreq->last_dir_entry = NULL;
587 newreq->sockfd = NULL; 604 newreq->sockfd = -1;
588 newreq->sockfd_write = NULL; 605 newreq->datafd = -1;
589 newreq->datafd = NULL; 606 newreq->cachefd = -1;
590 newreq->cachefd = NULL;
591 newreq->hostp = NULL; 607 newreq->hostp = NULL;
592 newreq->protocol_data = NULL;
593 608
594 if (req->proxy_config != NULL) 609 if (req->proxy_config != NULL)
595 newreq->proxy_config = g_strdup (req->proxy_config); 610 newreq->proxy_config = g_strdup (req->proxy_config);
596 611
597 req->init (newreq); 612 req->init (newreq);
846 861
847 char * 862 char *
848 ssh_start_login_sequence (gftp_request * request, int fd) 863 ssh_start_login_sequence (gftp_request * request, int fd)
849 { 864 {
850 size_t rem, len, diff, lastdiff; 865 size_t rem, len, diff, lastdiff;
851 int flags, wrotepw, ok; 866 int wrotepw, ok;
852 struct timeval tv; 867 char *tempstr, *pwstr;
853 char *tempstr;
854 fd_set rdfds;
855 ssize_t rd; 868 ssize_t rd;
856 869
857 rem = len = 100; 870 rem = len = 100;
858 tempstr = g_malloc0 (len); 871 tempstr = g_malloc0 (len);
859 diff = lastdiff = 0; 872 diff = lastdiff = 0;
860 wrotepw = 0; 873 wrotepw = 0;
861 ok = 1; 874 ok = 1;
862 875
863 if ((flags = fcntl (fd, F_GETFL, 0)) < 0) 876 if (gftp_set_sockblocking (request, request->datafd, 1) == -1)
864 { 877 return (NULL);
865 g_free (tempstr); 878
866 return (NULL); 879 pwstr = g_strconcat (request->password, "\n", NULL);
867 }
868
869 if (fcntl (fd, F_SETFL, flags | O_NONBLOCK) < 0)
870 {
871 g_free (tempstr);
872 return (NULL);
873 }
874 880
875 errno = 0; 881 errno = 0;
876 while (1) 882 while (1)
877 { 883 {
878 FD_ZERO (&rdfds); 884 if ((rd = gftp_read (request, tempstr + diff, rem -1, fd)) <= 0)
879 FD_SET (fd, &rdfds);
880 tv.tv_sec = 5;
881 tv.tv_usec = 0;
882 if (select (fd + 1, &rdfds, NULL, NULL, &tv) < 0)
883 { 885 {
884 if (errno == EINTR && !request->cancel)
885 continue;
886 ok = 0;
887 break;
888 }
889
890 if ((rd = read (fd, tempstr + diff, rem - 1)) < 0)
891 {
892 if (errno == EINTR && !request->cancel)
893 continue;
894 ok = 0;
895 break;
896 }
897 else if (rd == 0)
898 {
899 ok = 0; 886 ok = 0;
900 break; 887 break;
901 } 888 }
902 tempstr[diff + rd] = '\0'; 889 tempstr[diff + rd] = '\0';
903 rem -= rd; 890 rem -= rd;
916 if (!wrotepw && 903 if (!wrotepw &&
917 strlen (tempstr) > 11 && strcmp (tempstr + strlen (tempstr) - 10, 904 strlen (tempstr) > 11 && strcmp (tempstr + strlen (tempstr) - 10,
918 "password: ") == 0) 905 "password: ") == 0)
919 { 906 {
920 wrotepw = 1; 907 wrotepw = 1;
921 write (fd, request->password, strlen (request->password)); 908 if (gftp_write (request, pwstr, strlen (pwstr), fd) < 0)
922 write (fd, "\n", 1); 909 {
910 ok = 0;
911 break;
912 }
923 } 913 }
924 914
925 else if (!wrotepw && 915 else if (!wrotepw &&
926 (strstr (tempstr, "Enter passphrase for RSA key") != NULL || 916 (strstr (tempstr, "Enter passphrase for RSA key") != NULL ||
927 strstr (tempstr, "Enter passphrase for key '") != NULL)) 917 strstr (tempstr, "Enter passphrase for key '") != NULL))
928 { 918 {
929 wrotepw = 1; 919 wrotepw = 1;
930 write (fd, request->password, strlen (request->password)); 920 if (gftp_write (request, pwstr, strlen (pwstr), fd) < 0)
931 write (fd, "\n", 1); 921 {
922 ok = 0;
923 break;
924 }
932 } 925 }
933 else if (strlen (tempstr) >= 5 && 926 else if (strlen (tempstr) >= 5 &&
934 strcmp (tempstr + strlen (tempstr) - 5, "xsftp") == 0) 927 strcmp (tempstr + strlen (tempstr) - 5, "xsftp") == 0)
935 break; 928 break;
936 } 929 }
937 930
931 g_free (pwstr);
938 tempstr[diff] = '\0'; 932 tempstr[diff] = '\0';
939 request->logging_function (gftp_logging_recv, request->user_data, 933 request->logging_function (gftp_logging_recv, request->user_data,
940 "%s\n", tempstr + lastdiff); 934 "%s\n", tempstr + lastdiff);
941
942 if (ok && fcntl (fd, F_SETFL, flags) < 0)
943 ok = 0;
944 935
945 if (!ok) 936 if (!ok)
946 { 937 {
947 g_free (tempstr); 938 g_free (tempstr);
948 return (NULL); 939 return (NULL);