From ab0b31fed3e057cc36b1adda614b6ed2c54196b2 Mon Sep 17 00:00:00 2001 From: Nicolas Saillant Date: Fri, 28 Apr 2023 13:58:49 +0200 Subject: [PATCH 1/2] Use SFTP API instead of SCP API --- Polyhedron/demo/Polyhedron/MainWindow.cpp | 4 +- Polyhedron/demo/Polyhedron/Use_ssh.cpp | 133 +++++++++++----------- 2 files changed, 68 insertions(+), 69 deletions(-) diff --git a/Polyhedron/demo/Polyhedron/MainWindow.cpp b/Polyhedron/demo/Polyhedron/MainWindow.cpp index b797ecf7a70..39c537dea5b 100644 --- a/Polyhedron/demo/Polyhedron/MainWindow.cpp +++ b/Polyhedron/demo/Polyhedron/MainWindow.cpp @@ -3191,8 +3191,8 @@ void MainWindow::on_actionSa_ve_Scene_as_Script_triggered() tr("Enter the name of your scene file.")); if(path.isEmpty()) return; - if(!path.contains("Polyhedron_demo_")) - path.prepend("Polyhedron_demo_"); + if(!path.contains("/tmp/Polyhedron_demo_")) + path.prepend("/tmp/Polyhedron_demo_"); try{ ssh_session session = nullptr; bool res = establish_ssh_session_from_agent(session, diff --git a/Polyhedron/demo/Polyhedron/Use_ssh.cpp b/Polyhedron/demo/Polyhedron/Use_ssh.cpp index 965abf23024..2fafe072444 100644 --- a/Polyhedron/demo/Polyhedron/Use_ssh.cpp +++ b/Polyhedron/demo/Polyhedron/Use_ssh.cpp @@ -26,6 +26,10 @@ #include #include + +#include +#include + bool test_result(int res) { switch(res){ @@ -240,21 +244,22 @@ bool push_file(ssh_session &session, const char* dest_path, const char* filepath) { + std::size_t processed = 0; + sftp_file sftpfile; //copy a file - ssh_scp scp = ssh_scp_new( - session, SSH_SCP_WRITE | SSH_SCP_RECURSIVE, "/tmp"); - if (scp == nullptr) + sftp_session sftp = sftp_new(session); + if (sftp == nullptr) { - std::cerr<<"Error allocating scp session: %s\n" + std::cerr<<"Error allocating sftp session:\n" << ssh_get_error(session)< timespan(size); - std::this_thread::sleep_for(timespan); - if (res != SSH_OK) + while ( size > 0 ) { - std::cerr<< "Can't write to remote file: %s\n" - << ssh_get_error(session)< 16384) + s = 16384; + res = sftp_write(sftpfile, buffer.data() + processed, s); + if ( res < 0) + { + std::cerr<< "Can't write data to file:\n" + << ssh_get_error(session)< buffer; - - ssh_scp scp = ssh_scp_new( - session, SSH_SCP_READ | SSH_SCP_RECURSIVE, from_path); - if (scp == nullptr) + sftp_file sftpfile; + sftp_session sftp = sftp_new(session); + if (sftp == nullptr) { - std::cerr<<"Error allocating scp session: %s\n" + std::cerr<<"Error allocating sftp session:\n" << ssh_get_error(session)<size; buffer.resize(size); - if(ssh_scp_accept_request(scp) != SSH_OK) + while ( size > 0 ) { - std::cerr<< "Could not accept request."< 16384) + s = 16384; + res = sftp_read(sftpfile, buffer.data() + processed, s); + if ( res < 0) { - std::cerr<< "Error receiving file data: %s\n"<< ssh_get_error(session)<size; std::ofstream file(to_path, std::ios::binary |std::ios::trunc); if(!file.write(buffer.data(), size)) { std::cerr<<"Error while writing file."< Date: Wed, 31 May 2023 14:27:53 +0200 Subject: [PATCH 2/2] fix warning --- Polyhedron/demo/Polyhedron/Use_ssh.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Polyhedron/demo/Polyhedron/Use_ssh.cpp b/Polyhedron/demo/Polyhedron/Use_ssh.cpp index 2fafe072444..a4495160fbc 100644 --- a/Polyhedron/demo/Polyhedron/Use_ssh.cpp +++ b/Polyhedron/demo/Polyhedron/Use_ssh.cpp @@ -319,7 +319,6 @@ bool pull_file(ssh_session &session, const char* from_path, const char* to_path) { - int rc; std::size_t size; std::size_t processed = 0; std::vector buffer; @@ -386,10 +385,9 @@ bool explore_the_galaxy(ssh_session &session, QStringList& files) { ssh_channel channel; - int rc; channel = ssh_channel_new(session); if (channel == nullptr) return false; - rc = ssh_channel_open_session(channel); + int rc = ssh_channel_open_session(channel); if (rc != SSH_OK) { ssh_channel_free(channel);