[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

sftp_read receives EOF on second call for a 24kb file


I have built and am running libssh 0.5.5 for windows x32 and am trying to
pull a 24kb file over.  I have my buffer size set to 1024 (but have tried
4096 with the same result).  The first call to sftp_read returns back 1024
as the count of bytes read but the subsequent call returns 0.  When I
stepped through sftp_read I found that sftp_read was receiving SSH_FX_EOF
even though there is still 23kb left to read.  Am I opening the ssh or sftp
session incorrectly?  I have attached the relevant code.

 

Thanks,

 

Kevin

 

int SshHelper::InitFtpSession(ssh_session &sshSession, sftp_session &sftpSession)
{  
  int port = 22;
  int timeout = 10;  //seconds
  int rc;

  sshSession = ssh_new();

  if ( sshSession == NULL ) {
    TRACE("SshHelper::InitFtpSession - Unable to obtain SSH session");
    return SSH_ERROR;
  }

  ssh_options_set(sshSession, SSH_OPTIONS_HOST, mIpAddress.c_str());
  ssh_options_set(sshSession, SSH_OPTIONS_USER, mUsername.c_str());
  ssh_options_set(sshSession, SSH_OPTIONS_PORT, &port);
  ssh_options_set(sshSession, SSH_OPTIONS_TIMEOUT, &timeout);

  try {
    rc = ssh_connect(sshSession);
    if ( rc != SSH_OK ) {      
      ssh_free(sshSession);
      TRACE((char*)String::F("SshHelper::InitFtpSession error connecting to modem.  Address %s:%d, Username %s, Timeout: %d", mIpAddress.c_str(), port, mUsername.c_str(), timeout).c_str());
      return SSH_ERROR;
    }
  } catch (...) {
    TRACE("SshHelper::InitFtpSession Crash");
  }

  sftpSession = sftp_new(sshSession);
  if ( sftpSession == NULL ) {
    TRACE("SshHelper::InitFtpSession - Unable to obtaion SFTP session");

    return SSH_ERROR;
  }

  rc = sftp_init(sftpSession);
  if ( rc != SSH_OK ) {
    TRACE("SshHelper::InitFtpSession - Unable to init sftp session");
    return rc;
  }

  return SSH_OK;
}

int SshHelper::GetFile(String source, String destination)
{
  sftp_session sftp = NULL;
  ssh_session sshSession = NULL;
  int rc = SSH_OK;
  char buffer[1024];
  size_t length = sizeof(buffer);
  size_t totalLength = 0;
  std::ofstream destFile;

  rc = InitFtpSession( sshSession, sftp );
  if ( rc != SSH_OK ) {
    TRACE("SshHelper::GetFile - Failure to init sftp session");
  }

  destFile.open(destination.c_str(), std::ios::out);

  //read a file
  int access_type = O_RDONLY;
  sftp_file file = sftp_open(sftp, source.c_str(), access_type, 0);
  if ( file == NULL ) {
    TRACE("SshHelper::GetFile - Failure to open file for writing");
  } else {    
    size_t count = 0;

    count = sftp_read(file, buffer, length);

    while ( count > 0 ) {

      if ( destFile.is_open() ) {
        destFile.write(buffer, length);
      }

      totalLength += count;

      count = sftp_read(file, buffer, length);
    } 
  }

  destFile.close();

  // Cleanup
  try {
    sftp_free(sftp);
    ssh_disconnect(sshSession);
    ssh_free(sshSession);
  } catch (...) {
    TRACE("SshHelper::GetFile - Exception during cleanup");
  }

  return rc;
}

Archive administrator: postmaster@lists.cynapses.org