From cccaa868f535f063c5a4e7f4f4a5f17c54a4e5cc Mon Sep 17 00:00:00 2001
From: Daniel Stenberg <daniel@haxx.se>
Date: Fri, 7 Aug 2009 11:33:59 +0200
Subject: [PATCH] revert parts of the b95fe985af3c80a2babcaaaf7da69a15b1237c49
 commit

Alexander Lamaison tracked down that my previous commit broke SFTP
reads in some aspects. The reversion now gets back to always recv()
until EAGAIN is returned so that the code no longer treats a short
read as an indication that it is "enough for now".

The bad commit in particular had another independent change included,
which is to clear the direction-bits first in the transport read
and write functions, but this reversion does not revert that change.
Clearing those bits first is a good thing.
---
 src/transport.c | 33 ++++++++++++---------------------
 1 file changed, 12 insertions(+), 21 deletions(-)

diff --git a/src/transport.c b/src/transport.c
index 119c007..ade3826 100644
--- a/src/transport.c
+++ b/src/transport.c
@@ -272,7 +272,6 @@ _libssh2_transport_read(LIBSSH2_SESSION * session)
     unsigned char block[MAX_BLOCKSIZE];
     int blocksize;
     int encrypted = 1;
-    int loop = 1;
     int status;
 
     /* default clear the bit */
@@ -297,17 +296,17 @@ _libssh2_transport_read(LIBSSH2_SESSION * session)
          * is done!
          */
         _libssh2_debug(session, LIBSSH2_DBG_TRANS, "Redirecting into the"
-            " key re-exchange");
+                       " key re-exchange");
         status = libssh2_kex_exchange(session, 1, &session->startup_key_state);
         if (status == PACKET_EAGAIN) {
             libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
-                "Would block exchanging encryption keys", 0);
+                          "Would block exchanging encryption keys", 0);
             return PACKET_EAGAIN;
-      } else if (status) {
-          libssh2_error(session, LIBSSH2_ERROR_KEX_FAILURE,
-                      "Unable to exchange encryption keys",0);
-          return LIBSSH2_ERROR_KEX_FAILURE;
-      }
+        } else if (status) {
+            libssh2_error(session, LIBSSH2_ERROR_KEX_FAILURE,
+                          "Unable to exchange encryption keys",0);
+            return LIBSSH2_ERROR_KEX_FAILURE;
+        }
     }
 
     /*
@@ -351,7 +350,6 @@ _libssh2_transport_read(LIBSSH2_SESSION * session)
             /* If we have less than a blocksize left, it is too
                little data to deal with, read more */
             ssize_t nread;
-            size_t recv_amount;
 
             /* move any remainder to the start of the buffer so
                that we can do a full refill */
@@ -364,12 +362,11 @@ _libssh2_transport_read(LIBSSH2_SESSION * session)
                 p->readidx = p->writeidx = 0;
             }
 
-            recv_amount = PACKETBUFSIZE - remainbuf;
-
             /* now read a big chunk from the network into the temp buffer */
             nread =
                 _libssh2_recv(session->socket_fd, &p->buf[remainbuf],
-                              recv_amount, LIBSSH2_SOCKET_RECV_FLAGS(session));
+                              PACKETBUFSIZE - remainbuf,
+                              LIBSSH2_SOCKET_RECV_FLAGS(session));
             if (nread <= 0) {
                 /* check if this is due to EAGAIN and return the special
                    return code if so, error out normally otherwise */
@@ -380,10 +377,6 @@ _libssh2_transport_read(LIBSSH2_SESSION * session)
                 }
                 return PACKET_FAIL;
             }
-            else if(nread != (ssize_t)recv_amount) {
-                /* we're done for this time! */
-                loop = 0;
-            }
 
             debugdump(session, "libssh2_transport_read() raw",
                       &p->buf[remainbuf], nread);
@@ -581,11 +574,12 @@ _libssh2_transport_read(LIBSSH2_SESSION * session)
             }
 
             p->total_num = 0;   /* no packet buffer available */
+
             return rc;
         }
-    } while (loop); /* loop until done */
+    } while (1);                /* loop */
 
-    return rc;
+    return PACKET_FAIL;         /* we never reach this point */
 }
 
 static libssh2pack_t
@@ -815,6 +809,3 @@ _libssh2_transport_write(LIBSSH2_SESSION * session, unsigned char *data,
 
     return PACKET_NONE;         /* all is good */
 }
-
-
-