0

I can successfully send files from one end to another using libssh2_scp_send(). When I try to receive files using libssh2_scp_recv(), though, it does not fill the struct stat parameter with data. The file size always returns "0".

struct stat    file_info;

LIBSSH2_CHANNEL *channel = libssh2_scp_recv( session, source_file, &file_info );

printf( "file_info.st_size = %lu.\n",       file_info.st_size );

Any other fields in file_info come empty too, even though channel is not NULL.

My session is opened correctly and works for any other SSH operation (running remote comands, sending files...)

I am using "libssh2-1.3.0".

Is there any clue, please?

Really thanks.


EDIT:

I changed the third line to:

fprintf( stderr, "** file_info { st_size = %lu; st_atime = %lu }. SSH last error = %d",
    file_info.st_size, file_info.st_atime,
    libssh2_session_last_errno( info->session ) );

And the result is:

** file_info { st_size = 0; st_atime = 0 }. SSH last error = 0`.

That's it. The returned channel is valid, I can read from it. libssh2_session_last_errno() gives no error, but st_size still comes empty.

JaxWR
  • 226
  • 2
  • 7

2 Answers2

0

Try to do some error checking, so we can see exactly what's going on:

channel = libssh2_scp_recv(session, path_to_file, &struct_stat);
if (!channel && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) 
{
    if ((struct_stat.st_mode == 0) && (struct_stat.st_atime == 0) && \
           (struct_stat.st_mtime == 0) && (struct_stat.st_size == 0)) 
    {
       fprintf(stderr, "No file found and the error %d\n", \
                     libssh2_session_last_errno(session));

       exit (EXIT_FAILURE);
   }
}

Hope this help.

Regards.

TOC
  • 4,326
  • 18
  • 21
  • This will allow me a more precise diagnostic, but does not explain why `st_size == 0` even knowing that the file exists and has data. If I try to read the file until no more bytes, I can read it correctly. – JaxWR Aug 01 '12 at 21:29
  • @JaxWR : what return this libssh2_session_last_errno(session)? – TOC Aug 01 '12 at 21:46
  • `** file_info { st_size = 0; st_atime = 0 }. SSH last error = 0`. Thats it. The returned channel is valid and there is no `errno` for `libssh2_session_last_errno()`. – JaxWR Aug 02 '12 at 21:55
0

Only Change Time and Permission modes of source_file are being populated by libssh2_scp_recv(session, source_file, &file_info) on my system. Therefore, file_info.st_ctim.tv_sec and file_info.st_mode have valid values.

If you look at the libssh2 API documentation for libssh2_scp_recv, http://libssh2.sourceforge.net/doc/#libssh2scprecv, it does not guarantee that all fields of file_info will be populated.

The end of file is indicated by a single NULL(0) byte being returned by libssh2_channel_read.

Vivek
  • 320
  • 1
  • 5