*** ./kioslave/fish/fish.cpp.old	2009-09-07 03:58:21.000000000 +0900
--- ./kioslave/fish/fish.cpp	2009-09-07 03:58:25.000000000 +0900
***************
*** 30,36 ****
  #include "config-fish.h"
  #include <QFile>
  #include <QDateTime>
- #include <QBitArray>
  #include <QRegExp>
  #include <QCoreApplication>
  
--- 30,35 ----
***************
*** 518,527 ****
              myDebug( << "select failed, rc: " << rc << ", error: " << strerror(errno) << endl);
              return true;
          }
!         if (FD_ISSET(childFd,&wfds) && outBufPos >= 0) {
!             if (outBuf) rc = ::write(childFd,outBuf+outBufPos,outBufLen-outBufPos);
!             else rc = 0;
!             if (rc >= 0) outBufPos += rc;
              else {
                  if (errno == EINTR)
                      continue;
--- 517,530 ----
              myDebug( << "select failed, rc: " << rc << ", error: " << strerror(errno) << endl);
              return true;
          }
!         if (FD_ISSET(childFd, &wfds) && outBufPos >= 0) {
!             if (outBuf)
!                 rc = ::write(childFd, outBuf + outBufPos, outBufLen - outBufPos);
!             else
!                 rc = 0;
! 
!             if (rc >= 0)
!                 outBufPos += rc;
              else {
                  if (errno == EINTR)
                      continue;
***************
*** 534,546 ****
                  outBuf = NULL;
                  outBufLen = 0;
              }
!         }
!         else if (FD_ISSET(childFd,&rfds)) {
!             rc = ::read(childFd,buf+offset,32768-offset);
              if (rc > 0) {
!                 int noff = establishConnection(buf,rc+offset);
!                 if (noff < 0) return false;
!                 if (noff > 0) memmove(buf,buf+offset+rc-noff,noff);
                  offset = noff;
              } else {
                  if (errno == EINTR)
--- 537,550 ----
                  outBuf = NULL;
                  outBufLen = 0;
              }
!         } else if (FD_ISSET(childFd,&rfds)) {
!             rc = ::read(childFd, buf + offset, sizeof(buf) - offset);
              if (rc > 0) {
!                 int noff = establishConnection(buf, rc + offset);
!                 if (noff < 0)
!                     return false;
!                 if (noff > 0)
!                     memmove(buf, buf + offset + rc - noff, noff);
                  offset = noff;
              } else {
                  if (errno == EINTR)
***************
*** 1402,1407 ****
--- 1406,1412 ----
          isRunning = false;
      }
  }
+ 
  /** aborts command sequence and calls error() */
  void fishProtocol::error(int type, const QString &detail) {
      commandList.clear();
***************
*** 1410,1417 ****
      SlaveBase::error(type,detail);
      isRunning = false;
  }
  /** executes a chain of commands */
! void fishProtocol::run() 
  /* This function writes to childFd fish commands (like #STOR 0 /tmp/test ...) that are stored in outBuf
  and reads from childFd the remote host's response. ChildFd is the fd to a process that communicates
  with .fishsrv.pl typically running on another computer. */
--- 1415,1423 ----
      SlaveBase::error(type,detail);
      isRunning = false;
  }
+ 
  /** executes a chain of commands */
! void fishProtocol::run()
  /* This function writes to childFd fish commands (like #STOR 0 /tmp/test ...) that are stored in outBuf
  and reads from childFd the remote host's response. ChildFd is the fd to a process that communicates
  with .fishsrv.pl typically running on another computer. */
***************
*** 1443,1465 ****
                  shutdownConnection();
                  return;
              }
              if (FD_ISSET(childFd,&wfds) && outBufPos >= 0) {
  #else
              if (outBufPos >= 0) {
- #endif
- #if 0
-                 QString debug = QString::fromLatin1(outBuf+outBufPos,outBufLen-outBufPos);
-                 myDebug( << "now writing " << (outBufLen-outBufPos) << " " << debug.left(40) << "..." << endl);
- #endif
- #ifndef Q_WS_WIN
-                 if (outBufLen-outBufPos > 0) rc = ::write(childFd,outBuf+outBufPos,outBufLen-outBufPos);
- #else
                  if (outBufLen-outBufPos > 0) {
                      rc = childPid->write(outBuf);
                  }
  #endif
!                 else rc = 0;
!                 if (rc >= 0) outBufPos += rc;
                  else {
  #ifndef Q_WS_WIN
                      if (errno == EINTR)
--- 1449,1472 ----
                  shutdownConnection();
                  return;
              }
+ 	    // We first write the complete buffer, including all newlines.
+ 	    // Do: send command and newlines, expect response then
+ 	    // Do not: send commands, expect response, send newlines, expect response on newlines
+ 	    // Newlines do not trigger a response.
              if (FD_ISSET(childFd,&wfds) && outBufPos >= 0) {
+                 if (outBufLen-outBufPos > 0)
+                     rc = ::write(childFd, outBuf + outBufPos, outBufLen - outBufPos);
  #else
              if (outBufPos >= 0) {
                  if (outBufLen-outBufPos > 0) {
                      rc = childPid->write(outBuf);
                  }
  #endif
!                 else
!                     rc = 0;
! 
!                 if (rc >= 0)
!                     outBufPos += rc;
                  else {
  #ifndef Q_WS_WIN
                      if (errno == EINTR)
***************
*** 1480,1494 ****
              }
  #ifndef Q_WS_WIN
              else if (FD_ISSET(childFd,&rfds)) {
!                 rc = ::read(childFd,buf+offset,32768-offset);
  #else
              else if (childPid->waitForReadyRead(1000)) {
!                 rc = childPid->read(buf+offset,32768-offset);
  #endif
                  //myDebug( << "read " << rc << " bytes" << endl);
                  if (rc > 0) {
!                     int noff = received(buf,rc+offset);
!                     if (noff > 0) memmove(buf,buf+offset+rc-noff,noff);
                      //myDebug( << "left " << noff << " bytes: " << QString::fromLatin1(buf,offset) << endl);
                      offset = noff;
                  } else {
--- 1487,1501 ----
              }
  #ifndef Q_WS_WIN
              else if (FD_ISSET(childFd,&rfds)) {
!                 rc = ::read(childFd, buf + offset, sizeof(buf) - offset);
  #else
              else if (childPid->waitForReadyRead(1000)) {
!                 rc = childPid->read(buf + offset, sizeof(buf) - offset);
  #endif
                  //myDebug( << "read " << rc << " bytes" << endl);
                  if (rc > 0) {
!                     int noff = received(buf, rc + offset);
!                     if (noff > 0) memmove(buf, buf + offset + rc - noff, noff);
                      //myDebug( << "left " << noff << " bytes: " << QString::fromLatin1(buf,offset) << endl);
                      offset = noff;
                  } else {
***************
*** 1509,1514 ****
--- 1516,1522 ----
          }
      }
  }
+ 
  /** stat a file */
  void fishProtocol::stat(const KUrl& u){
      myDebug( << "@@@@@@@@@ stat " << u << endl);
