Improve sftp_rename().
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@611 7dcaeef0-15fb-0310-b436-a5af3365683c
Этот коммит содержится в:
родитель
387f6473f7
Коммит
320c70a170
119
libssh/sftp.c
119
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_free(buffer);
|
||||
while (!msg) {
|
||||
if (sftp_read_and_dispatch(sftp))
|
||||
return -1;
|
||||
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)
|
||||
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 rename", msg->packet_type);
|
||||
sftp_message_free(msg);
|
||||
}
|
||||
buffer = buffer_new();
|
||||
if (buffer == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
oldpath = string_from_char(original);
|
||||
if (oldpath == NULL) {
|
||||
buffer_free(buffer);
|
||||
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;
|
||||
}
|
||||
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);
|
||||
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 */
|
||||
|
Загрузка…
x
Ссылка в новой задаче
Block a user