mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-09 09:54:25 +09:00
sftp: Reformat sftp_setstat()
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
157
src/sftp.c
157
src/sftp.c
@@ -2406,91 +2406,92 @@ int sftp_rename(sftp_session sftp, const char *original, const char *newname) {
|
|||||||
|
|
||||||
/* Code written by Nick */
|
/* Code written by Nick */
|
||||||
/* Set file attributes on a file, directory or symbolic link. */
|
/* Set file attributes on a file, directory or symbolic link. */
|
||||||
int sftp_setstat(sftp_session sftp, const char *file, sftp_attributes attr) {
|
int sftp_setstat(sftp_session sftp, const char *file, sftp_attributes attr)
|
||||||
uint32_t id;
|
{
|
||||||
ssh_buffer buffer;
|
uint32_t id;
|
||||||
ssh_string path;
|
ssh_buffer buffer;
|
||||||
sftp_message msg = NULL;
|
ssh_string path;
|
||||||
sftp_status_message status = NULL;
|
sftp_message msg = NULL;
|
||||||
int rc;
|
sftp_status_message status = NULL;
|
||||||
|
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);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
path = ssh_string_from_char(file);
|
|
||||||
if (path == NULL) {
|
|
||||||
ssh_set_error_oom(sftp->session);
|
|
||||||
ssh_buffer_free(buffer);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = ssh_buffer_allocate_size(buffer,
|
|
||||||
sizeof(uint32_t) * 2 +
|
|
||||||
ssh_string_len(path));
|
|
||||||
if (rc < 0) {
|
|
||||||
ssh_set_error_oom(sftp->session);
|
|
||||||
ssh_buffer_free(buffer);
|
|
||||||
ssh_string_free(path);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
id = sftp_get_new_id(sftp);
|
|
||||||
if (ssh_buffer_add_u32(buffer, htonl(id)) < 0 ||
|
|
||||||
ssh_buffer_add_ssh_string(buffer, path) < 0 ||
|
|
||||||
buffer_add_attributes(buffer, attr) < 0) {
|
|
||||||
ssh_set_error_oom(sftp->session);
|
|
||||||
ssh_buffer_free(buffer);
|
|
||||||
ssh_string_free(path);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (sftp_packet_write(sftp, SSH_FXP_SETSTAT, buffer) < 0) {
|
|
||||||
ssh_buffer_free(buffer);
|
|
||||||
ssh_string_free(path);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
ssh_buffer_free(buffer);
|
|
||||||
ssh_string_free(path);
|
|
||||||
|
|
||||||
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 */
|
path = ssh_string_from_char(file);
|
||||||
if (msg->packet_type == SSH_FXP_STATUS) {
|
if (path == NULL) {
|
||||||
status = parse_status_msg(msg);
|
ssh_set_error_oom(sftp->session);
|
||||||
sftp_message_free(msg);
|
ssh_buffer_free(buffer);
|
||||||
if (status == NULL) {
|
return -1;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
sftp_set_error(sftp, status->status);
|
|
||||||
switch (status->status) {
|
rc = ssh_buffer_allocate_size(buffer,
|
||||||
case SSH_FX_OK:
|
sizeof(uint32_t) * 2 +
|
||||||
|
ssh_string_len(path));
|
||||||
|
if (rc < 0) {
|
||||||
|
ssh_set_error_oom(sftp->session);
|
||||||
|
ssh_buffer_free(buffer);
|
||||||
|
ssh_string_free(path);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
id = sftp_get_new_id(sftp);
|
||||||
|
if (ssh_buffer_add_u32(buffer, htonl(id)) < 0 ||
|
||||||
|
ssh_buffer_add_ssh_string(buffer, path) < 0 ||
|
||||||
|
buffer_add_attributes(buffer, attr) < 0) {
|
||||||
|
ssh_set_error_oom(sftp->session);
|
||||||
|
ssh_buffer_free(buffer);
|
||||||
|
ssh_string_free(path);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (sftp_packet_write(sftp, SSH_FXP_SETSTAT, buffer) < 0) {
|
||||||
|
ssh_buffer_free(buffer);
|
||||||
|
ssh_string_free(path);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
ssh_buffer_free(buffer);
|
||||||
|
ssh_string_free(path);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* 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);
|
status_msg_free(status);
|
||||||
return 0;
|
return -1;
|
||||||
default:
|
} else {
|
||||||
break;
|
ssh_set_error(sftp->session, SSH_FATAL,
|
||||||
|
"Received message %d when attempting to set stats", msg->packet_type);
|
||||||
|
sftp_message_free(msg);
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
* 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 set stats", msg->packet_type);
|
|
||||||
sftp_message_free(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Change the file owner and group */
|
/* Change the file owner and group */
|
||||||
|
|||||||
Reference in New Issue
Block a user