diff 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
line wrap: on
line diff
--- a/lib/misc.c	Tue Nov 12 00:04:44 2002 +0000
+++ b/lib/misc.c	Thu Nov 21 00:33:51 2002 +0000
@@ -259,24 +259,43 @@
 int
 copyfile (char *source, char *dest)
 {
-  FILE *srcfd, *destfd;
+  int srcfd, destfd;
   char buf[8192];
-  size_t n;
+  ssize_t n;
 
-  if ((srcfd = fopen (source, "rb")) == NULL)
-    return (0);
+  if ((srcfd = open (source, O_RDONLY)) == -1)
+    {
+      printf (_("Error: Cannot open local file %s: %s\n"),
+              source, g_strerror (errno));
+      exit (1);
+    }
 
-  if ((destfd = fopen (dest, "wb")) == NULL)
+  if ((destfd = open (dest, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
     {
-      fclose (srcfd);
-      return (0);
+      printf (_("Error: Cannot open local file %s: %s\n"),
+              dest, g_strerror (errno));
+      close (srcfd);
+      exit (1);
     }
 
-  while ((n = fread (buf, 1, sizeof (buf), srcfd)) > 0)
-    fwrite (buf, 1, n, destfd);
+  while ((n = read (srcfd, buf, sizeof (buf))) > 0)
+    {
+      if (write (destfd, buf, n) == -1)
+        {
+          printf (_("Error: Could not write to socket: %s\n"), 
+                  g_strerror (errno));
+          exit (1);
+        }
+    }
 
-  fclose (srcfd);
-  fclose (destfd);
+  if (n == -1)
+    {
+      printf (_("Error: Could not read from socket: %s\n"), g_strerror (errno));
+      exit (1);
+    }
+
+  close (srcfd);
+  close (destfd);
 
   return (1);
 }
@@ -429,8 +448,8 @@
     g_free (fle->attribs);
   if (fle->destfile)
     g_free (fle->destfile);
-  if (fle->fd)
-    fclose (fle->fd);
+  if (fle->fd > 0)
+    close (fle->fd);
   g_free (fle);
 }
 
@@ -481,13 +500,11 @@
 {
   dest->sockfd = source->sockfd;
   dest->datafd = source->datafd;
-  dest->sockfd_write = source->sockfd_write;
   dest->cached = 0;
   if (!source->always_connected)
     {
-      source->sockfd = NULL;
-      source->datafd = NULL;
-      source->sockfd_write = NULL;
+      source->sockfd = -1;
+      source->datafd = -1;
       source->cached = 1;
     }
 }
@@ -584,12 +601,10 @@
   newreq->proxy_account = NULL;
   newreq->last_ftp_response = NULL;
   newreq->last_dir_entry = NULL;
-  newreq->sockfd = NULL;
-  newreq->sockfd_write = NULL;
-  newreq->datafd = NULL;
-  newreq->cachefd = NULL;
+  newreq->sockfd = -1;
+  newreq->datafd = -1;
+  newreq->cachefd = -1;
   newreq->hostp = NULL;
-  newreq->protocol_data = NULL;
   
   if (req->proxy_config != NULL)
     newreq->proxy_config = g_strdup (req->proxy_config);
@@ -848,10 +863,8 @@
 ssh_start_login_sequence (gftp_request * request, int fd)
 {
   size_t rem, len, diff, lastdiff;
-  int flags, wrotepw, ok;
-  struct timeval tv;
-  char *tempstr;
-  fd_set rdfds;
+  int wrotepw, ok;
+  char *tempstr, *pwstr;
   ssize_t rd;
 
   rem = len = 100;
@@ -860,42 +873,16 @@
   wrotepw = 0;
   ok = 1;
 
-  if ((flags = fcntl (fd, F_GETFL, 0)) < 0) 
-    {
-      g_free (tempstr);
-      return (NULL);
-    }
+  if (gftp_set_sockblocking (request, request->datafd, 1) == -1)
+    return (NULL);
 
-  if (fcntl (fd, F_SETFL, flags | O_NONBLOCK) < 0)
-    {
-      g_free (tempstr);
-      return (NULL);
-    }
+  pwstr = g_strconcat (request->password, "\n", NULL);
 
   errno = 0;
   while (1)
     {
-      FD_ZERO (&rdfds);
-      FD_SET (fd, &rdfds);
-      tv.tv_sec = 5;
-      tv.tv_usec = 0;
-      if (select (fd + 1, &rdfds, NULL, NULL, &tv) < 0)
+      if ((rd = gftp_read (request, tempstr + diff, rem -1, fd)) <= 0)
         {
-          if (errno == EINTR && !request->cancel)
-            continue;
-          ok = 0;
-          break;
-        }
-
-      if ((rd = read (fd, tempstr + diff, rem - 1)) < 0)
-        {
-          if (errno == EINTR && !request->cancel)
-            continue;
-          ok = 0;
-          break;
-        }
-      else if (rd == 0)
-        { 
           ok = 0;
           break;
         }
@@ -918,8 +905,11 @@
                                            "password: ") == 0)
         {
           wrotepw = 1;
-          write (fd, request->password, strlen (request->password));
-          write (fd, "\n", 1);
+          if (gftp_write (request, pwstr, strlen (pwstr), fd) < 0)
+            {
+              ok = 0;
+              break;
+            }
         }
 
       else if (!wrotepw && 
@@ -927,21 +917,22 @@
                strstr (tempstr, "Enter passphrase for key '") != NULL))
         {
           wrotepw = 1;
-          write (fd, request->password, strlen (request->password));
-          write (fd, "\n", 1);
+          if (gftp_write (request, pwstr, strlen (pwstr), fd) < 0)
+            {
+              ok = 0;
+              break;
+            }
         }
       else if (strlen (tempstr) >= 5 && 
                strcmp (tempstr + strlen (tempstr) - 5, "xsftp") == 0)
         break;
     }
 
+  g_free (pwstr);
   tempstr[diff] = '\0';
   request->logging_function (gftp_logging_recv, request->user_data,
                              "%s\n", tempstr + lastdiff);
 
-  if (ok && fcntl (fd, F_SETFL, flags) < 0)
-    ok = 0;
-
   if (!ok)
     {
       g_free (tempstr);