Ejemplo n.º 1
0
void FSSftp::CloseHandle( LIBSSH2_SFTP_HANDLE* h, FSCInfo* info )
{
	int ret;
	WHILE_EAGAIN_( ret, libssh2_sftp_close_handle( h ) );

	if ( ret ) { throw int( ret - 1000 ); }
}
Ejemplo n.º 2
0
void
PJSSH::GetFile(const char* aRemoteFileName, std::ostream& aStream) const
{
  LIBSSH2_SFTP* sftp = libssh2_sftp_init(mSession);

  if( NULL == sftp )
  {
    throw std::runtime_error("Failed to open a sftp session.");
  }

  LIBSSH2_SFTP_HANDLE* file_handle
    = libssh2_sftp_open(sftp,aRemoteFileName,LIBSSH2_FXF_READ,0);

  if( NULL == file_handle )
  {
    std::ostringstream o;
    o<<"Failed to open remote file for reading. Last error code="
      <<libssh2_sftp_last_error(sftp);
    throw std::runtime_error(o.str());
  }

  // Read the whole file and write the read data on the supplied stream.
  char buffer[1024];
  size_t num_of_read_bytes(0);
  do
  {
    num_of_read_bytes = libssh2_sftp_read(file_handle,buffer,1024);
    aStream.write(buffer,num_of_read_bytes);
  } while( num_of_read_bytes == 1024 );

  // Close sftp file handle and end SFTP session.
  libssh2_sftp_close_handle(file_handle);
  libssh2_sftp_shutdown(sftp);
}
Ejemplo n.º 3
0
static PyObject *
SFTP_handle_close(SSH2_SFTP_handleObj *self)
{
	int ret;

	Py_BEGIN_ALLOW_THREADS
	ret = libssh2_sftp_close_handle(self->sftphandle);
	Py_END_ALLOW_THREADS

	CHECK_RETURN_CODE(ret, self->session)

	Py_RETURN_NONE;
}
Ejemplo n.º 4
0
bool SFTPChannel::updateWriteFile()
{
    int rc;

    if (mRequestState == Beginning)
    {
        QByteArray path = mCurrentRequest->getPath().toUtf8();
        mOperationHandle = libssh2_sftp_open_ex(mHandle, path, path.length(), LIBSSH2_FXF_WRITE | LIBSSH2_FXF_TRUNC | LIBSSH2_FXF_CREAT, 0644, LIBSSH2_SFTP_OPENFILE);
        if (mOperationHandle)
            mRequestState = Writing;
        else if ((rc = libssh2_session_last_errno(mSession->sessionHandle())) == LIBSSH2_ERROR_EAGAIN)
            return true;	// try again
        else
        {
            criticalError(tr("Failed to open remote file for writing: %1").arg(rc));
            return false;
        }

        mOperationCursor = 0;
    }

    if (mRequestState == Writing)
    {
        const QByteArray& content = mCurrentRequest->getContent();
        rc = libssh2_sftp_write(mOperationHandle, content.constData() + mOperationCursor, content.length() - mOperationCursor);
        if (rc == LIBSSH2_ERROR_EAGAIN)
            return true;	//	Try again
        else if (rc < 0)
        {
            criticalError(tr("Error while writing file contents: %1").arg(rc));
            return false;
        }
        else	//	Wrote some data
        {
            mOperationCursor += rc;
            mCurrentRequest->triggerProgress((mOperationCursor * 100) / content.length());
        }

        if (mOperationCursor >= content.length())
            mRequestState = Finishing;
    }

    if (mRequestState == Finishing)
    {
        rc = libssh2_sftp_close_handle(mOperationHandle);
        if (rc == LIBSSH2_ERROR_EAGAIN)
            return true;
        else if (rc < 0)
        {
            criticalError(tr("Failed to cleanly close SFTP file: %1").arg(rc));
            return false;
        }

        //	Success! Send a response and finish up.
        QVariantMap finalResult;
        finalResult.insert("revision", mCurrentRequest->getRevision());
        finalResult.insert("undoLength", mCurrentRequest->getUndoLength());
        finalResult.insert("checksum", BaseFile::getChecksum(mCurrentRequest->getContent()));
        mCurrentRequest->triggerSuccess(finalResult);
        return false;
    }

    return true;
}
Ejemplo n.º 5
0
bool SFTPChannel::updateReadFile()
{
    int rc;

    if (mRequestState == Beginning)
    {
        QByteArray path = mCurrentRequest->getPath().toUtf8();
        mOperationHandle = libssh2_sftp_open_ex(mHandle, path, path.length(), LIBSSH2_FXF_READ, 0, LIBSSH2_SFTP_OPENFILE);
        if (mOperationHandle)
            mRequestState = Sizing;
        else if ((rc = libssh2_session_last_errno(mSession->sessionHandle())) == LIBSSH2_ERROR_EAGAIN)
            return true;	// try again
        else
        {
            criticalError(tr("Failed to open remote file for reading: %1").arg(rc));
            return false;
        }
    }

    if (mRequestState == Sizing)
    {
        LIBSSH2_SFTP_ATTRIBUTES attr;
        int rc = libssh2_sftp_fstat_ex(mOperationHandle, &attr, 0);
        if (rc == LIBSSH2_ERROR_EAGAIN)
            return true;
        else if (rc < 0)
        {
            criticalError(tr("Failed to stat remote file: %1").arg(rc));
            return false;
        }

        mOperationSize = attr.filesize;
        if (mOperationSize == 0)
            mOperationSize = 1;
        mRequestState = Reading;
    }

    if (mRequestState == Reading)
    {
        char buffer[4096];

        rc = libssh2_sftp_read(mOperationHandle, buffer, sizeof(buffer));
        if (rc == LIBSSH2_ERROR_EAGAIN)
            return true;	//	Try again
        else if (rc == 0)
            mRequestState = Finishing;
        else if (rc < 0)
        {
            criticalError(tr("Error while reading file contents: %1").arg(rc));
            return false;
        }
        else	//	Got some data
        {
            mCurrentRequest->addContent(buffer, rc);
            mCurrentRequest->triggerProgress((mCurrentRequest->getContent().length() * 100) / mOperationSize);
        }
    }

    if (mRequestState == Finishing)
    {
        rc = libssh2_sftp_close_handle(mOperationHandle);
        if (rc == LIBSSH2_ERROR_EAGAIN)
            return true;
        else if (rc < 0)
        {
            criticalError(tr("Failed to cleanly close SFTP file: %1").arg(rc));
            return false;
        }

        //	Success! Send a response and finish up.
        QVariantMap finalResult;
        finalResult.insert("content", mCurrentRequest->getContent());
        mCurrentRequest->triggerSuccess(finalResult);
        return false;
    }

    return true;
}
Ejemplo n.º 6
0
void
PJSSH::PutStream(std::istream & aStream, const char* aRemoteFileName) const
{
   //Check if local file exists
    if(!aStream)
    {
        std::string error("The local file does not exist");
        throw std::runtime_error(error);
    }
   LIBSSH2_SFTP* sftp = libssh2_sftp_init(mSession);

   if( NULL == sftp )
   {
      throw std::runtime_error("Failed to open a sftp session.");
   }
   //Check if remote file exitst
   LIBSSH2_SFTP_ATTRIBUTES fileinfo;
   int status = libssh2_sftp_stat(sftp, aRemoteFileName , &fileinfo);
   if(status == 0)
   {
       //File exitsts
       std::string error("The file " + std::string(aRemoteFileName) + " alerady exists");
       throw std::runtime_error(error);

   }

   LIBSSH2_SFTP_HANDLE* file_handle
      = libssh2_sftp_open(sftp,aRemoteFileName,
            LIBSSH2_FXF_TRUNC | LIBSSH2_FXF_WRITE | LIBSSH2_FXF_CREAT,0);

   if( NULL == file_handle )
   {
      std::ostringstream o;
      o<<"Failed to write on remote file. Last error code="
         <<libssh2_sftp_last_error(sftp);
       throw std::runtime_error(o.str());
   }
   char buffer[1024];
   do
   {
      aStream.read(buffer,1024);
      const std::streamsize num_of_read_characters(aStream.gcount());
      if( num_of_read_characters > 0 )
      {
         const size_t num_of_bytes_written
            = libssh2_sftp_write( file_handle, buffer, num_of_read_characters);
         if( num_of_bytes_written == ((size_t)-1) )
         {
            throw std::runtime_error("Failed to write to the remote file.");
         }
         else if( static_cast<std::streamsize>(num_of_bytes_written)
               != num_of_read_characters )
         {
            throw std::runtime_error("Failed to write all bytes to remote file.");
         }
         else if(num_of_read_characters <= 0)
         {
            throw std::runtime_error("Failed to read characters from the input "
                  "stream to be written to the remote file.");
         }

      }
   } while( aStream );
   // Close sftp file handle and end SFTP session.
   libssh2_sftp_close_handle(file_handle);
   libssh2_sftp_shutdown(sftp);
}