mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-08-17 14:21:05 +03:00
Reformat of sftp_rename() to match the current coding style
Signed-off-by: Eshan Kelkar <eshankelkar@galorithm.com> Reviewed-by: Jakub Jelen <jjelen@redhat.com>
This commit is contained in:
committed by
Jakub Jelen
parent
73c3d8965d
commit
b067d7a123
147
src/sftp.c
147
src/sftp.c
@@ -2514,85 +2514,88 @@ int sftp_mkdir(sftp_session sftp, const char *directory, mode_t mode)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* code written by nick */
|
/* code written by nick */
|
||||||
int sftp_rename(sftp_session sftp, const char *original, const char *newname) {
|
int sftp_rename(sftp_session sftp, const char *original, const char *newname)
|
||||||
sftp_status_message status = NULL;
|
{
|
||||||
sftp_message msg = NULL;
|
sftp_status_message status = NULL;
|
||||||
ssh_buffer buffer;
|
sftp_message msg = NULL;
|
||||||
uint32_t id;
|
ssh_buffer buffer = NULL;
|
||||||
int rc;
|
uint32_t id;
|
||||||
|
int rc;
|
||||||
|
|
||||||
buffer = ssh_buffer_new();
|
buffer = ssh_buffer_new();
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
ssh_set_error_oom(sftp->session);
|
ssh_set_error_oom(sftp->session);
|
||||||
sftp_set_error(sftp, SSH_FX_FAILURE);
|
sftp_set_error(sftp, SSH_FX_FAILURE);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
id = sftp_get_new_id(sftp);
|
|
||||||
|
|
||||||
rc = ssh_buffer_pack(buffer,
|
|
||||||
"dss",
|
|
||||||
id,
|
|
||||||
original,
|
|
||||||
newname);
|
|
||||||
if (rc != SSH_OK) {
|
|
||||||
ssh_set_error_oom(sftp->session);
|
|
||||||
SSH_BUFFER_FREE(buffer);
|
|
||||||
sftp_set_error(sftp, SSH_FX_FAILURE);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sftp->version >= 4){
|
|
||||||
/* POSIX rename atomically replaces newpath, we should do the same
|
|
||||||
* only available on >=v4 */
|
|
||||||
ssh_buffer_add_u32(buffer, SSH_FXF_RENAME_OVERWRITE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sftp_packet_write(sftp, SSH_FXP_RENAME, buffer) < 0) {
|
|
||||||
SSH_BUFFER_FREE(buffer);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
SSH_BUFFER_FREE(buffer);
|
|
||||||
|
|
||||||
while (msg == NULL) {
|
|
||||||
if (sftp_read_and_dispatch(sftp) < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
msg = sftp_dequeue(sftp, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* By specification, this command only returns SSH_FXP_STATUS */
|
id = sftp_get_new_id(sftp);
|
||||||
if (msg->packet_type == SSH_FXP_STATUS) {
|
|
||||||
status = parse_status_msg(msg);
|
rc = ssh_buffer_pack(buffer,
|
||||||
sftp_message_free(msg);
|
"dss",
|
||||||
if (status == NULL) {
|
id,
|
||||||
return -1;
|
original,
|
||||||
|
newname);
|
||||||
|
if (rc != SSH_OK) {
|
||||||
|
ssh_set_error_oom(sftp->session);
|
||||||
|
SSH_BUFFER_FREE(buffer);
|
||||||
|
sftp_set_error(sftp, SSH_FX_FAILURE);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
sftp_set_error(sftp, status->status);
|
|
||||||
switch (status->status) {
|
if (sftp->version >= 4) {
|
||||||
case SSH_FX_OK:
|
/*
|
||||||
|
* POSIX rename atomically replaces newpath,
|
||||||
|
* we should do the same only available on >=v4
|
||||||
|
*/
|
||||||
|
ssh_buffer_add_u32(buffer, SSH_FXF_RENAME_OVERWRITE);
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = sftp_packet_write(sftp, SSH_FXP_RENAME, buffer);
|
||||||
|
SSH_BUFFER_FREE(buffer);
|
||||||
|
if (rc < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (msg == NULL) {
|
||||||
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
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 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);
|
status_msg_free(status);
|
||||||
return 0;
|
return -1;
|
||||||
default:
|
} else {
|
||||||
break;
|
ssh_set_error(sftp->session, SSH_FATAL,
|
||||||
|
"Received message %d when attempting to rename",
|
||||||
|
msg->packet_type);
|
||||||
|
sftp_message_free(msg);
|
||||||
|
sftp_set_error(sftp, SSH_FX_BAD_MESSAGE);
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
* 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);
|
|
||||||
sftp_set_error(sftp, SSH_FX_BAD_MESSAGE);
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Code written by Nick */
|
/* Code written by Nick */
|
||||||
|
Reference in New Issue
Block a user