1
1

ssh_scp_push_file and ssh_scp_write

still needs tests
Этот коммит содержится в:
Aris Adamantiadis 2009-08-09 22:51:03 +02:00
родитель e4da8b99fe
Коммит f92e12c7b0
5 изменённых файлов: 62 добавлений и 3 удалений

Просмотреть файл

@ -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
}

Просмотреть файл

@ -347,6 +347,8 @@ struct ssh_scp_struct {
int mode;
ssh_channel channel;
char *location;
size_t filelen;
size_t processed;
};
struct ssh_message;

Просмотреть файл

@ -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

Просмотреть файл

@ -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;

Просмотреть файл

@ -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;
}