On 2/1/07, Daniel Stenberg <[EMAIL PROTECTED]> wrote: > Hey friends, > > I committed example/simple/scp.c a few days ago. It seems it doesn't work to > download files with SCP the way I thought it should. Can someone please tell > me in what way I abuse the API?
I did not look at your code, but here is what we are doing and it seems to work: Chris /****************************************************************************** * * file_copy.c * * Copyright (C) 2006 Chris Nystrom * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lessor General Public License as published * by the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but without any warranty whatsoever. * * For more license information see: * * http://www.gnu.org/copyleft/lessor.html * * Contact Author at: * * Chris Nystrom * 11013 Prairie Dove Circle * Austin, Texas 78758 * * E-Mail: [EMAIL PROTECTED] * Blog: http://newio.blogspot.com * AIM: nystromchris * * Soli Deo Gloria * */ #include "ssh2_lib.h" #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #ifndef WIN32 #include <unistd.h> #endif /* */ #ifdef WIN32 #include <io.h> #define S_IWUSR 0 #define S_IRUSR 0 #endif /* */ #ifndef BUFSIZ #define BUFSIZ (1024) #endif /* */ #define BLOCK (1) #define NO_BLOCK (0) LIBSSH2_SESSION *_get_session(void); PRIVATE LIBSSH2_CHANNEL *channel; PRIVATE int get_file_size(const char *path) { struct stat sb; stat(path, &sb); return (sb.st_size); } PRIVATE int scp_write_file(const char *local_path) { int file, n; char buf[BUFSIZ]; if ((file = open(local_path, O_RDWR #ifdef WIN32 | _O_BINARY #endif /* */ , 0)) == -1) { fprintf(stderr, "scp_write_file(): unable to open file %s.\n", local_path); return (-1); } libssh2_channel_set_blocking(channel, BLOCK); while ((n = read(file, buf, BUFSIZ)) > 0) { if (libssh2_channel_write(channel, buf, n) != n) { fprintf(stderr, "scp_write_file(): write error on file %s\n", local_path); } } close(file); return (0); } PRIVATE int scp_read_file(const char *local_path, int filesize) { int file, n; char buf[BUFSIZ]; int count = 0; if ((file = open(local_path, O_RDWR | O_CREAT | O_TRUNC #ifdef WIN32 | _O_BINARY #endif /* */ , S_IRUSR | S_IWUSR)) == -1) { fprintf(stderr, "scp_read_file(): unable to create file %s.\n", local_path); return (-1); } libssh2_channel_set_blocking(channel, BLOCK); while (count < filesize) { n = libssh2_channel_read(channel, buf, BUFSIZ); count += n; if (write(file, buf, n) != n) { fprintf(stderr, "scp_read_file(): write error on file %s\n", local_path); } } close(file); return (0); } PRIVATE void free_scp_channel(void) { if (channel) { libssh2_channel_free(channel); channel = NULL; } } PUBLIC int send_file(const char *local_path, const char *remote_path, int remote_mode) { int f_size; /* does file exist? */ f_size = get_file_size(local_path); if (f_size < 0) { fprintf(stderr, "send_file(): unable to get file size for file %s\n", local_path); return (-1); } printf("file size = %d\n", f_size); printf("path = %s\n", local_path); printf("path = %s\n", remote_path); printf("mode = %o\n", remote_mode); /* open the channel */ channel = libssh2_scp_send(_get_session(), remote_path, remote_mode, f_size); if (channel == NULL) { fprintf(stderr, "send_file(): unable to open channel\n"); return (-1); } if (scp_write_file(local_path) < 0) { fprintf(stderr, "send_file(): unable to write file\n"); free_scp_channel(); return (-1); } free_scp_channel(); return (0); } PUBLIC int recv_file(const char *remote_path, const char *local_path) { struct stat sb; channel = libssh2_scp_recv(_get_session(), remote_path, &sb); if (channel == NULL) { fprintf(stderr, "recv_file(): unable to open channel\n"); return (-1); } if (scp_read_file(local_path, sb.st_size) < 0) { fprintf(stderr, "recv_file(): unable to read file\n"); free_scp_channel(); return (-1); } free_scp_channel(); return (0); } -- E-Mail: Chris Nystrom <[EMAIL PROTECTED]> Saving the world from web programming. http://www.newio.org/ - AIM: nystromchris ------------------------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier. Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 _______________________________________________ libssh2-devel mailing list libssh2-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/libssh2-devel