Hi Kevin,
I had a play and came up with this solution in C, I don't usually use C so I
hope this is of 'some' use.
-----Original Message-----
From: Kevin Lambert <[email protected]>
To: libssh <[email protected]>
Sent: Tue, 22 Oct 2013 20:11
Subject: 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
#include <libssh/libssh.h>
#include <libssh/sftp.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
ssh_session my_ssh_session = ssh_new();
int verbosity = SSH_LOG_PROTOCOL;
int port = 22;
int rc;
if (my_ssh_session == NULL)
{
exit(-1);
}
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "<host>");
ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
rc = ssh_connect(my_ssh_session);
rc = ssh_userauth_password(my_ssh_session, NULL, "<password>");
sftp_session sftp;
sftp = sftp_new(my_ssh_session);
rc = sftp_init(sftp);
sftp_file file;
int access_type;
char *buffer = malloc(1024);
//char *buffer[1024];
int nbytes;
sftp_attributes getFileSize;
int getRemainder = 0;
int getExcess = 0;
int buffersize = 1024;
int FCount = 0;
file = sftp_open(sftp, "/home/Owner/test.pdf", 0, 0);
getFileSize = sftp_stat(sftp, "/home/Owner/test.pdf");
getRemainder = getFileSize->size%buffersize;
getExcess = getFileSize->size - getRemainder;
FILE *fp;
fp = fopen("test.pdf", "w+b");
if(getFileSize < buffersize)
{
nbytes = sftp_read(file, buffer, buffersize);
fwrite(buffer, sizeof(buffer), nbytes, fp);
}
else
{
while(FCount != getExcess)
{
nbytes = sftp_read(file, buffer, buffersize);
FCount = FCount + buffersize;
fwrite(buffer, sizeof(buffer), FCount, fp);
sftp_seek(file, FCount);
fseek(fp, FCount, 0);
}
if(getRemainder > 0)
{
nbytes = sftp_read(file, buffer, getRemainder);
fwrite(buffer, sizeof(buffer), getRemainder, fp);
}
}
free(buffer);
fclose(fp);
// -------------------------------------
}