2

I'm try to use libssh2 to write a string to a file. And I can do it.

The problem is I only can write once to the file, I can't append to the file, and giving the arguments to the libssh2_scp_send I give the size, if the size is bigger then the real size of the string (ex. I give 50, but the string have 5 chars) the is created but with 0 bytes.

function I use:

int s7c_hardware::writeKey(char * filename, char * str)
{
    channel = libssh2_scp_send(session, filename, 0777, strlen(str));
    char buf[99999];
    memcpy(buf, str, strlen(str));
    buf[strlen(str)] = '\0';
    libssh2_channel_write(channel, buf, strlen(str));
    libssh2_channel_free(channel);
    channel = NULL;
    return 0;
}
jww
  • 97,681
  • 90
  • 411
  • 885
Samega 7Cattac
  • 253
  • 1
  • 3
  • 16
  • Why the `memcpy()`? Isn't the effect exactly what `strcpy()` does? When do you close the channel? Because `channel` seems to be a global variable ... and I see no calls to [libssh2_channel_close](https://www.libssh2.org/libssh2_channel_close.html). Is there an error returned from the channel write (you ignore it). – dhke Jul 06 '17 at 22:33
  • @dhke Sorry, I forget to put the code to close the channel. Why memcpy? because in my app that string can have "\0" zero terminator in the middle. – Samega 7Cattac Jul 06 '17 at 22:54
  • 1
    You should use the SFTP protocol rather than SCP if you can. SCP isn't very sophisticated. – Kenster Jul 06 '17 at 23:10
  • @Kenster But the difference between SCP and SFTP is: SCP is faster, don't use ACK flag, but with SFTP you can start and stop your download our upload – Samega 7Cattac Jul 07 '17 at 00:05
  • @Kenster But ok, I will try to use SFTP – Samega 7Cattac Jul 07 '17 at 00:06
  • If your string may contain null byte `\0` in the middle, `strlen()` is not the right function you need. – alvits Jul 07 '17 at 23:05
  • @alvits So what I use to have the size of a string with `\0` in the middle? – Samega 7Cattac Jul 09 '17 at 16:24

1 Answers1

1

Now I understood the why! SCP cannot append to a file because that's literally starting and stoping the download and SCP cannot do that, but SFTP can!

Samega 7Cattac
  • 253
  • 1
  • 3
  • 16