aboutsummaryrefslogtreecommitdiff
path: root/libssh/sftp.c
diff options
context:
space:
mode:
authorAndreas Schneider <mail@cynapses.org>2009-04-27 10:20:54 +0000
committerAndreas Schneider <mail@cynapses.org>2009-04-27 10:20:54 +0000
commit320c70a170abd426efcc2fba8c4e5a61a2e54132 (patch)
tree1d51aa7f8312b78740cccbf5e91d86aa240d6260 /libssh/sftp.c
parent387f6473f72e710e7ca7fde14df52b1d7a597683 (diff)
downloadlibssh-320c70a170abd426efcc2fba8c4e5a61a2e54132.tar.gz
libssh-320c70a170abd426efcc2fba8c4e5a61a2e54132.tar.xz
libssh-320c70a170abd426efcc2fba8c4e5a61a2e54132.zip
Improve sftp_rename().
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@611 7dcaeef0-15fb-0310-b436-a5af3365683c
Diffstat (limited to 'libssh/sftp.c')
-rw-r--r--libssh/sftp.c111
1 files changed, 73 insertions, 38 deletions
diff --git a/libssh/sftp.c b/libssh/sftp.c
index 965161f1..3c4bee98 100644
--- a/libssh/sftp.c
+++ b/libssh/sftp.c
@@ -2007,50 +2007,85 @@ int sftp_mkdir(SFTP_SESSION *sftp, const char *directory, mode_t mode) {
/* code written by nick */
int sftp_rename(SFTP_SESSION *sftp, const char *original, const char *newname) {
- u32 id = sftp_get_new_id(sftp);
- BUFFER *buffer = buffer_new();
- STRING *oldpath = string_from_char(original);
- STRING *newpath = string_from_char(newname);
- SFTP_MESSAGE *msg = NULL;
- STATUS_MESSAGE *status = NULL;
+ STATUS_MESSAGE *status = NULL;
+ SFTP_MESSAGE *msg = NULL;
+ BUFFER *buffer;
+ STRING *oldpath;
+ STRING *newpath;
+ u32 id;
- buffer_add_u32(buffer, id);
- buffer_add_ssh_string(buffer, oldpath);
- free(oldpath);
- buffer_add_ssh_string(buffer, newpath);
- free(newpath);
- /* POSIX rename atomically replaces newpath, we should do the same */
- buffer_add_u32(buffer, SSH_FXF_RENAME_OVERWRITE);
- sftp_packet_write(sftp, SSH_FXP_RENAME, buffer);
+ buffer = buffer_new();
+ if (buffer == NULL) {
+ return -1;
+ }
+
+ oldpath = string_from_char(original);
+ if (oldpath == NULL) {
buffer_free(buffer);
- while (!msg) {
- if (sftp_read_and_dispatch(sftp))
- return -1;
- msg = sftp_dequeue(sftp, id);
+ return -1;
+ }
+
+ newpath = string_from_char(newname);
+ if (newpath == NULL) {
+ buffer_free(buffer);
+ string_free(oldpath);
+ return -1;
+ }
+
+ id = sftp_get_new_id(sftp);
+ if (buffer_add_u32(buffer, id) < 0 ||
+ buffer_add_ssh_string(buffer, oldpath) < 0 ||
+ buffer_add_ssh_string(buffer, newpath) < 0 ||
+ /* POSIX rename atomically replaces newpath, we should do the same */
+ buffer_add_u32(buffer, SSH_FXF_RENAME_OVERWRITE) < 0 ||
+ sftp_packet_write(sftp, SSH_FXP_RENAME, buffer) < 0) {
+ buffer_free(buffer);
+ string_free(oldpath);
+ string_free(newpath);
+ return -1;
+ }
+ buffer_free(buffer);
+ string_free(oldpath);
+ string_free(newpath);
+
+ while (msg == NULL) {
+ if (sftp_read_and_dispatch(sftp) < 0) {
+ 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);
+ msg = sftp_dequeue(sftp, id);
+ }
+
+ /* By specification, this command only returns SSH_FXP_STATUS */
+ if (msg->packet_type == 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 -1;
- } else {
- ssh_set_error(sftp->session,SSH_FATAL, "Received message %d when attempting to rename", msg->packet_type);
- sftp_message_free(msg);
+ 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 rename",
+ msg->packet_type);
+ sftp_message_free(msg);
+ }
+
+ return -1;
}
/* Code written by Nick */