diff options
author | Andreas Schneider <mail@cynapses.org> | 2009-04-24 12:57:11 +0000 |
---|---|---|
committer | Andreas Schneider <mail@cynapses.org> | 2009-04-24 12:57:11 +0000 |
commit | 50616076c73d6c9c6f4ade6bcf21e73b59f47c89 (patch) | |
tree | 95b5f9a977d0b705f2ff483b574b2fb7d8cb7ac7 /libssh/sftp.c | |
parent | 4a383ab3cfeb39e011eed4c3b786a9c25681265b (diff) | |
download | libssh-50616076c73d6c9c6f4ade6bcf21e73b59f47c89.tar.gz libssh-50616076c73d6c9c6f4ade6bcf21e73b59f47c89.tar.xz libssh-50616076c73d6c9c6f4ade6bcf21e73b59f47c89.zip |
Improve sftp_unlink().
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@607 7dcaeef0-15fb-0310-b436-a5af3365683c
Diffstat (limited to 'libssh/sftp.c')
-rw-r--r-- | libssh/sftp.c | 95 |
1 files changed, 60 insertions, 35 deletions
diff --git a/libssh/sftp.c b/libssh/sftp.c index dbdd57df..763fd645 100644 --- a/libssh/sftp.c +++ b/libssh/sftp.c @@ -1781,46 +1781,71 @@ int sftp_rm(SFTP_SESSION *sftp, const char *file) { /* code written by Nick */ int sftp_unlink(SFTP_SESSION *sftp, const char *file) { - u32 id = sftp_get_new_id(sftp); - BUFFER *buffer = buffer_new(); - STRING *filename = string_from_char(file); - SFTP_MESSAGE *msg = NULL; - STATUS_MESSAGE *status = NULL; + STATUS_MESSAGE *status = NULL; + SFTP_MESSAGE *msg = NULL; + STRING *filename; + BUFFER *buffer; + u32 id; - buffer_add_u32(buffer, id); - buffer_add_ssh_string(buffer, filename); - free(filename); - sftp_packet_write(sftp, SSH_FXP_REMOVE, buffer); + buffer = buffer_new(); + if (buffer == NULL) { + return -1; + } + + filename = string_from_char(file); + if (filename == NULL) { buffer_free(buffer); - while (!msg) { - if (sftp_read_and_dispatch(sftp)) { - return -1; - } - msg = sftp_dequeue(sftp, id); + return -1; + } + + id = sftp_get_new_id(sftp); + if (buffer_add_u32(buffer, id) < 0 || + buffer_add_ssh_string(buffer, filename) < 0 || + sftp_packet_write(sftp, SSH_FXP_REMOVE, buffer) < 0) { + buffer_free(buffer); + string_free(filename); + } + string_free(filename); + buffer_free(buffer); + + while (msg == NULL) { + if (sftp_read_and_dispatch(sftp)) { + return -1; } - if (msg->packet_type == SSH_FXP_STATUS) { - /* by specification, this command's only supposed to return SSH_FXP_STATUS */ - status = parse_status_msg(msg); - sftp_message_free(msg); - if (!status) - return -1; - sftp_set_error(sftp, status->status); - switch (status->status) { - case SSH_FX_OK: - status_msg_free(status); - return 0; - default: - break; - } - /* status should be SSH_FX_OK if the command was successful, if it didn't, then there was an error */ - ssh_set_error(sftp->session,SSH_REQUEST_DENIED, "sftp server: %s", status->errormsg); - status_msg_free(status); - return -1; - } else { - ssh_set_error(sftp->session,SSH_FATAL, "Received message %d when attempting to remove file", msg->packet_type); - sftp_message_free(msg); + msg = sftp_dequeue(sftp, id); + } + + if (msg->packet_type == SSH_FXP_STATUS) { + /* by specification, this command's only supposed to return SSH_FXP_STATUS */ + status = parse_status_msg(msg); + sftp_message_free(msg); + if (status == NULL) { + return -1; + } + sftp_set_error(sftp, status->status); + switch (status->status) { + case SSH_FX_OK: + status_msg_free(status); + return 0; + default: + break; } + + /* + * The status should be SSH_FX_OK if the command was successful, if it + * didn't, then there was an error + */ + ssh_set_error(sftp->session, SSH_REQUEST_DENIED, + "SFTP server: %s", status->errormsg); + status_msg_free(status); return -1; + } else { + ssh_set_error(sftp->session,SSH_FATAL, + "Received message %d when attempting to remove file", msg->packet_type); + sftp_message_free(msg); + } + + return -1; } /* code written by Nick */ |