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

Re: 'Short sftp packet error' in libssh


Here is my code for copying files with SFTP in libssh.

Do you think reading the file contents into a string might be the issue?


void SSH::copyFile_SFTP(boost::filesystem::path source, boost::filesystem::path remote_dir)
{
    std::lock_guard<std::mutex> lock(lock_SSH_Session);

    ASSERT(SSH_session);
    ASSERT(SFTP_session);

    // make sure file at 'source' exists
    if (!boost::filesystem::exists(source))
    {
        sftp_free(SFTP_session);
        ssh_disconnect(SSH_session);
        ssh_free(SSH_session);
        throw omnetpp::cRuntimeError("File %s not found!", source.c_str());
    }

    // read file contents into a string
    std::ifstream ifs(source.c_str());
    std::string content( (std::istreambuf_iterator<char>(ifs) ),
            (std::istreambuf_iterator<char>()    ) );
    int length = content.size();

    boost::filesystem::path remoteFile = remote_dir / source.filename();
    int access_type = O_WRONLY | O_CREAT | O_TRUNC;
    sftp_file file = sftp_open(SFTP_session, remoteFile.c_str(), access_type, S_IRWXU);
    if (file == NULL)
    {
        std::string err = ssh_get_error(SSH_session);
        sftp_free(SFTP_session);
        ssh_disconnect(SSH_session);
        ssh_free(SSH_session);
        throw omnetpp::cRuntimeError("Can't open file for writing: %s", err.c_str());
    }

    int nwritten = sftp_write(file, content.c_str(), length);
    if (nwritten != length)
    {
        std::string err = ssh_get_error(SSH_session);
        sftp_free(SFTP_session);
        ssh_disconnect(SSH_session);
        ssh_free(SSH_session);
        throw omnetpp::cRuntimeError("Can't write data to file: %s", err.c_str());
    }

    int rc = sftp_close(file);
    if (rc != SSH_OK)
        throw omnetpp::cRuntimeError("Can't close the written file: %s", ssh_get_error(SSH_session));
}


- Mani



On 11/14/2016 04:29 PM, Mani Amoozadeh wrote:

Following the last email I don't see any options to change chunk size when copying a file to the remote computer:

#include <libssh/sftp.h>
#include <sys/stat.h>
#include <fcntl.h>
int sftp_helloworld(ssh_session session, sftp_session sftp)
{
int access_type = O_WRONLY | O_CREAT | O_TRUNC;
sftp_file file;
const char *helloworld = "Hello, World!\n";
int length = strlen(helloworld);
int rc, nwritten;
...
file = sftp_open(sftp, "helloworld/helloworld.txt",
access_type, S_IRWXU);
if (file == NULL)
{
fprintf(stderr, "Can't open file for writing: %s\n",
ssh_get_error(session));
return SSH_ERROR;
}
nwritten = sftp_write(file, helloworld, length);
if (nwritten != length)
{
fprintf(stderr, "Can't write data to file: %s\n",
ssh_get_error(session));
sftp_close(file);
return SSH_ERROR;
}
rc = sftp_close(file);
if (rc != SSH_OK)
{
fprintf(stderr, "Can't close the written file: %s\n",
ssh_get_error(session));
return rc;
}
return SSH_OK;
}

On 11/14/2016 08:28 AM, Mani Amoozadeh wrote:

I dont know. How can I check it?

I am using the exact same sample code provided in the documentation.

I am sending a file from x86-64 system to ARM system.


On Nov 14, 2016 6:58 AM, "Andreas Schneider" <asn@xxxxxxxxxxxxxx> wrote:
On Sunday, 13 November 2016 23:33:48 CET Mani Amoozadeh wrote:
> Hello,
>
> I write a simple C++ program to copy files from my local PC to remote PC
> using SFTP implemented in libssh library. I followed the sample code in
> section 'Copying a file to the remote computer' in
> 'http://api.libssh.org/master/libssh_tutor_sftp.html'. I can copy most of
> files using this code, but for some files (specifically large
> files), sftp_write method returns the following error:
>
> Can't write data to file: Short sftp packet!
> I am using the latest libssh library from the git repository.

Hey,

do you write in 16k chunks?


        Andreas

--
Andreas Schneider                   GPG-ID: CC014E3D
www.cryptomilk.org                asn@xxxxxxxxxxxxxx




References:
'Short sftp packet error' in libsshMani Amoozadeh <mani.amoozadeh2@xxxxxxxxx>
Re: 'Short sftp packet error' in libsshAndreas Schneider <asn@xxxxxxxxxxxxxx>
Re: 'Short sftp packet error' in libsshMani Amoozadeh <mani.amoozadeh2@xxxxxxxxx>
Re: 'Short sftp packet error' in libsshMani Amoozadeh <mani.amoozadeh2@xxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org