diff options
author | Aris Adamantiadis <aris@0xbadc0de.be> | 2009-08-09 22:51:03 +0200 |
---|---|---|
committer | Aris Adamantiadis <aris@0xbadc0de.be> | 2009-08-09 22:51:03 +0200 |
commit | f92e12c7b0950666fbbe8a8fabc7258a831dbab1 (patch) | |
tree | 1761957f42b3138ee316aaf6833713a3820e2ab4 | |
parent | e4da8b99feb82f4b86ec158b8056d24c3a2dde7d (diff) | |
download | libssh-f92e12c7b0950666fbbe8a8fabc7258a831dbab1.tar.gz libssh-f92e12c7b0950666fbbe8a8fabc7258a831dbab1.tar.xz libssh-f92e12c7b0950666fbbe8a8fabc7258a831dbab1.zip |
ssh_scp_push_file and ssh_scp_write
still needs tests
-rw-r--r-- | include/libssh/libssh.h | 3 | ||||
-rw-r--r-- | include/libssh/priv.h | 2 | ||||
-rw-r--r-- | libssh/libssh.def | 2 | ||||
-rw-r--r-- | libssh/libssh.map | 6 | ||||
-rw-r--r-- | libssh/scp.c | 52 |
5 files changed, 62 insertions, 3 deletions
diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h index b86eaf26..bf984448 100644 --- a/include/libssh/libssh.h +++ b/include/libssh/libssh.h @@ -436,7 +436,8 @@ ssh_scp ssh_scp_new(ssh_session session, int mode, const char *location); int ssh_scp_init(ssh_scp scp); int ssh_scp_close(ssh_scp scp); void ssh_scp_free(ssh_scp scp); - +int ssh_scp_push_file(ssh_scp scp, const char *filename, size_t size, const char *perms); +int ssh_scp_write(ssh_scp scp, const void *buffer, size_t len); #ifdef __cplusplus } diff --git a/include/libssh/priv.h b/include/libssh/priv.h index e5c102a8..4d65652d 100644 --- a/include/libssh/priv.h +++ b/include/libssh/priv.h @@ -347,6 +347,8 @@ struct ssh_scp_struct { int mode; ssh_channel channel; char *location; + size_t filelen; + size_t processed; }; struct ssh_message; diff --git a/libssh/libssh.def b/libssh/libssh.def index b2848ce8..7b0ea799 100644 --- a/libssh/libssh.def +++ b/libssh/libssh.def @@ -171,6 +171,8 @@ ssh_scp_close ssh_scp_free ssh_scp_init ssh_scp_new +ssh_scp_push_file +ssh_scp_write ssh_select ssh_service_request ssh_set_blocking diff --git a/libssh/libssh.map b/libssh/libssh.map index b3e22065..e689c2bd 100644 --- a/libssh/libssh.map +++ b/libssh/libssh.map @@ -188,10 +188,12 @@ SSH_0.4 { channel_write_stderr; channel_request_x11; channel_accept_x11; - ssh_scp_new; + ssh_scp_close; ssh_scp_free; ssh_scp_init; - ssh_scp_close; + ssh_scp_new; + ssh_scp_push_file + ssh_scp_write sftp_extensions_get_count; sftp_extensions_get_data; sftp_extensions_get_name; diff --git a/libssh/scp.c b/libssh/scp.c index 84db3b51..b2fc2c91 100644 --- a/libssh/scp.c +++ b/libssh/scp.c @@ -92,3 +92,55 @@ void ssh_scp_free(ssh_scp scp){ SAFE_FREE(scp->location); SAFE_FREE(scp); } + +/** @brief initializes the sending of a file to a scp in sink mode + * @param filename Name of the file being sent. It should not contain any path indicator + * @param size Exact size in bytes of the file being sent. + * @param perms Text form of the unix permissions for the new file, e.g. "0644" + * @returns SSH_OK if the file is ready to be sent. + * @returns SSH_ERROR if an error happened. + */ +int ssh_scp_push_file(ssh_scp scp, const char *filename, size_t size, const char *perms){ + char buffer[1024]; + int r; + u_int8_t code; + snprintf(buffer,sizeof(buffer),"C%s %ld %s\n",perms, size, filename); + r=channel_write(scp->channel,buffer,strlen(buffer)); + if(r==SSH_ERROR) + return SSH_ERROR; + r=channel_read(scp->channel,&code,1,0); + if(code != 0){ + ssh_set_error(scp->session,SSH_FATAL, "scp status code %ud not valid", code); + return SSH_ERROR; + } + scp->filelen = size; + scp->processed = 0; + return SSH_OK; +} + +/** @brief Write into a remote scp file + * @param buffer the buffer to write + * @param len the number of bytes to write + * @returns SSH_OK the write was successful + * @returns SSH_ERROR an error happened while writing + */ +int ssh_scp_write(ssh_scp scp, const void *buffer, size_t len){ + int w,r; + u_int8_t code; + if(scp->processed + len > scp->filelen) + len = scp->filelen - scp->processed; + w=channel_write(scp->channel,buffer,len); + if(w != SSH_ERROR) + scp->processed += w; + else + return w; + if(scp->processed == scp->filelen) { + r=channel_read(scp->channel,&code,1,0); + if(code != 0){ + ssh_set_error(scp->session,SSH_FATAL, "scp status code %ud not valid", code); + return SSH_ERROR; + } + scp->processed=scp->filelen=0; + } + return SSH_OK; +} |