cURL / Mailing Lists / curl-library / Single Mail

curl-library

AW: AW: SCP/SFTP and persistency, bug #1823487

From: Enrico Stark <Enrico.Stark_at_bsgclearing.com>
Date: Thu, 29 Nov 2007 11:17:52 -0600

I just adopted the ftp upload example. The source directory contains 512 files that should be uploaded.
Here is the code:

#include <stdio.h>

#include <curl/curl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h> /* lstat, S_IFDIR */
#include <errno.h> /* errno */
#include <string.h> /* strcmp, ... */

#define REMOTE_URL "scp://user@host/curl/upload/"

int main(int argc, char **argv)
{
  CURL *curl;
  CURLcode res;
  FILE * hd_src ;
  char localFile[256]="";
  char remoteUrl[256]="";
  DIR* p_directory=NULL; /* handle on opened directory */
  struct dirent* p_direntry = NULL; /* handle on file in directory */
  struct stat localFileStat; /* file struct structure */
  char srcdir[256]="/curl/someDir/";
  int i = 0;
  double speed_upload=0;
  double total_time=0;

  /* In windows, this will init the winsock stuff */
  curl_global_init(CURL_GLOBAL_ALL);

  /* get a curl handle */
  curl = curl_easy_init();
  if (!curl) {
    fprintf(stderr, "Error initialising curl\n");
  }
  /* set debug output */
  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1) ;

  curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60) ;

  curl_easy_setopt(curl, CURLOPT_SSH_PUBLIC_KEYFILE, "/keyDir/.ssh/dsa-key.pub");
  curl_easy_setopt(curl, CURLOPT_SSH_PRIVATE_KEYFILE, "/keyDir/.ssh/dsa-key");

  curl_easy_setopt(curl, CURLOPT_KEYPASSWD, "passwd") ;

  /* enable uploading */
  curl_easy_setopt(curl, CURLOPT_UPLOAD, 1) ;

  if (NULL==(p_directory=opendir(srcdir))) {
    fprintf(stderr, "Cannot open directory <%s> errno=<%d>",srcdir,errno);
    return(1) ;
  }

  while ((p_direntry=readdir(p_directory)) != NULL) {
    if ( (strncmp(p_direntry->d_name,".",1)==0) ||
         (strncmp(p_direntry->d_name,"..",2)==0) ) {
         continue ; /* nothing to do with this files */
    }

    snprintf(localFile, sizeof(localFile), "%s/%s", srcdir,p_direntry->d_name);
    if (lstat(localFile,&localFileStat)<0) {
      fprintf(stderr, "Cannot detect file status of <%s>",localFile);
      continue ;
    }

    /* check if it is a regular file */
    if (!S_ISREG(localFileStat.st_mode)) {
      continue;
    }

    curl_easy_setopt(curl, CURLOPT_INFILESIZE,
                     (long)localFileStat.st_size);

  /* get a FILE * of the same file, could also be made with
     fdopen() from the previous descriptor, but hey this is just
     an example! */
    hd_src = fopen(localFile, "rb");

    snprintf(remoteUrl, sizeof(remoteUrl), "%s%s.tmp", REMOTE_URL, p_direntry->d_name);

    /* specify target */
    curl_easy_setopt(curl,CURLOPT_URL, remoteUrl);

    /* now specify which file to upload */
    curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);

    /* Now run off and do what you've been told! */
    res = curl_easy_perform(curl);

    /* now extract transfer info */
    curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
    curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);

    fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",
            speed_upload, total_time);

    fclose(hd_src); /* close the local file */
  }

  closedir(p_directory);

  /* always cleanup */
  curl_easy_cleanup(curl);
  curl_global_cleanup();
  return 0;
}

-----Ursprüngliche Nachricht-----
Von: curl-library-bounces_at_cool.haxx.se [mailto:curl-library-bounces_at_cool.haxx.se] Im Auftrag von Daniel Stenberg
Gesendet: Donnerstag, 29. November 2007 12:56
An: libcurl development
Betreff: Re: AW: SCP/SFTP and persistency, bug #1823487

On Wed, 28 Nov 2007, Enrico Stark wrote:

> I tried yesterday snapshot (libcurl 7.17.2-20071127) for a short test
> for protocols ftp, sftp and scp for uploading. Ftp and sftp work fine,
> but I have a question on scp, sometimes the connection is reused and
> sometimes not, see below ...

Thanks for the report, but I can't repeat it. I did this command line:

curl -T CHANGES scp://localhost/~/tmp/removeme -T README
scp://localhost/~/tmp/removeme2 -T CHANGES scp://localhost/~/tmp/removeme3 -u user:password -v

... and it re-used the same connection for all transfers!

How did you do it?

> * Channel failed to close

I found out that this particular error message (which really should be shown since it isn't an error) is due to a bug in libssh2 that I have fixed now...

--
  Commercial curl and libcurl Technical Support: http://haxx.se/curl.html
Received on 2007-11-29