Subject: libssh2_sftp_write fails with return code -1

libssh2_sftp_write fails with return code -1

From: Mark Riordan <mriordan_at_ipswitch.com>
Date: Fri, 29 Oct 2010 13:59:25 -0500

Yesterday (2010-10-28) I did a "git" from the repository and rebuilt my test
program with the newest libssh2.
Unfortunately, uploads now fail on the very first call to
libssh2_sftp_write, whereas with 1.2.7, the test program generally worked.
Here's the snippet of code that fails:

   remotemode = LIBSSH2_FXF_READ;
   if ("put" == settings.action)
      remotemode = LIBSSH2_FXF_WRITE | LIBSSH2_FXF_CREAT;
   sftp_handle =
     libssh2_sftp_open(sftp_session, settings.remotefile.c_str(),
remotemode, 0777);

   if (!sftp_handle) {
      fprintf(stderr, "Unable to open file with SFTP\n");
      goto shutdown;
   }
   fprintf(stderr, "libssh2_sftp_open() succeeded.\n");
   dwmsStart = GetTickCount();
   if ("get" == settings.action) {
      hand = open(settings.localfile.c_str(), O_BINARY | O_CREAT | O_RDWR,
_S_IWRITE);
      // On Ubuntu to Solaris, get gets 9.87 MB/sec!
      do {
         char mem[65536];

         /* loop until we fail */
         // fprintf(stderr, "libssh2_sftp_read()!\n");
         rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
         if (rc > 0) {
            write(hand, mem, rc);
            filesize += rc;
         } else {
            printf("libssh2_sftp_read with code %d\n", rc);
            break;
         }
      } while (1);
   } else if ("put" == settings.action) {
      hand = open(settings.localfile.c_str(), O_BINARY | O_RDONLY,
_S_IREAD);
      if (-1 == hand) {
         perror("opening local file");
         exit(2);
      }
      int nwrites = 0;

      char mem[32500];

      printf("Using output buffer size of %d bytes\n", sizeof(mem));
      do {
         /* loop until we fail */
         int nbytes = read(hand, mem, sizeof(mem));

         if (nbytes > 0) {
            filesize += nbytes;
            int offset = 0, bytes_to_send = nbytes;

            do {
               // This is the code that fails with -1.
<<===========================<<
               rc = libssh2_sftp_write(sftp_handle, mem + offset,
bytes_to_send);
               if (rc < 0) {
                  printf("libssh2_sftp_write failed with code %d\n", rc);
                  break;
               }
               ++nwrites;
               if (0 == (nwrites % 100)) {
                  printf("libssh2_sftp_write %d sent %d bytes\n", nwrites,
rc);
               }
               offset += rc;
               bytes_to_send -= rc;
            } while (bytes_to_send > 0);
         } else {
            break;
         }
      } while (rc >= 0);
   } else {
      puts("action must be get or put.");
      exit(1);
   }

Thanks,

Mark R

_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2010-10-29