mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-10 18:28:10 +09:00
sftp: Reformat sftp_readlink()
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
(cherry picked from commit bb4bdec184)
This commit is contained in:
177
src/sftp.c
177
src/sftp.c
@@ -2612,103 +2612,106 @@ int sftp_symlink(sftp_session sftp, const char *target, const char *dest) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *sftp_readlink(sftp_session sftp, const char *path) {
|
char *sftp_readlink(sftp_session sftp, const char *path)
|
||||||
sftp_status_message status = NULL;
|
{
|
||||||
sftp_message msg = NULL;
|
sftp_status_message status = NULL;
|
||||||
ssh_string path_s = NULL;
|
sftp_message msg = NULL;
|
||||||
ssh_string link_s = NULL;
|
ssh_string path_s = NULL;
|
||||||
ssh_buffer buffer;
|
ssh_string link_s = NULL;
|
||||||
char *lnk;
|
ssh_buffer buffer;
|
||||||
uint32_t ignored;
|
char *lnk;
|
||||||
uint32_t id;
|
uint32_t ignored;
|
||||||
int rc;
|
uint32_t id;
|
||||||
|
int rc;
|
||||||
|
|
||||||
if (sftp == NULL)
|
if (sftp == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
if (path == NULL) {
|
}
|
||||||
ssh_set_error_invalid(sftp);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (sftp->version < 3){
|
|
||||||
ssh_set_error(sftp,SSH_REQUEST_DENIED,"sftp version %d does not support sftp_readlink",sftp->version);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
buffer = ssh_buffer_new();
|
|
||||||
if (buffer == NULL) {
|
|
||||||
ssh_set_error_oom(sftp->session);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
path_s = ssh_string_from_char(path);
|
if (path == NULL) {
|
||||||
if (path_s == NULL) {
|
ssh_set_error_invalid(sftp);
|
||||||
ssh_set_error_oom(sftp->session);
|
return NULL;
|
||||||
ssh_buffer_free(buffer);
|
}
|
||||||
return NULL;
|
if (sftp->version < 3){
|
||||||
}
|
ssh_set_error(sftp,SSH_REQUEST_DENIED,"sftp version %d does not support sftp_readlink",sftp->version);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
buffer = ssh_buffer_new();
|
||||||
|
if (buffer == NULL) {
|
||||||
|
ssh_set_error_oom(sftp->session);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
rc = ssh_buffer_allocate_size(buffer,
|
path_s = ssh_string_from_char(path);
|
||||||
sizeof(uint32_t) * 2 +
|
if (path_s == NULL) {
|
||||||
ssh_string_len(path_s));
|
ssh_set_error_oom(sftp->session);
|
||||||
if (rc < 0) {
|
ssh_buffer_free(buffer);
|
||||||
ssh_set_error_oom(sftp->session);
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = ssh_buffer_allocate_size(buffer,
|
||||||
|
sizeof(uint32_t) * 2 +
|
||||||
|
ssh_string_len(path_s));
|
||||||
|
if (rc < 0) {
|
||||||
|
ssh_set_error_oom(sftp->session);
|
||||||
|
ssh_buffer_free(buffer);
|
||||||
|
ssh_string_free(path_s);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
id = sftp_get_new_id(sftp);
|
||||||
|
if (ssh_buffer_add_u32(buffer, htonl(id)) < 0 ||
|
||||||
|
ssh_buffer_add_ssh_string(buffer, path_s) < 0) {
|
||||||
|
ssh_set_error_oom(sftp->session);
|
||||||
|
ssh_buffer_free(buffer);
|
||||||
|
ssh_string_free(path_s);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (sftp_packet_write(sftp, SSH_FXP_READLINK, buffer) < 0) {
|
||||||
|
ssh_buffer_free(buffer);
|
||||||
|
ssh_string_free(path_s);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
ssh_buffer_free(buffer);
|
ssh_buffer_free(buffer);
|
||||||
ssh_string_free(path_s);
|
ssh_string_free(path_s);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
id = sftp_get_new_id(sftp);
|
while (msg == NULL) {
|
||||||
if (ssh_buffer_add_u32(buffer, htonl(id)) < 0 ||
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
||||||
ssh_buffer_add_ssh_string(buffer, path_s) < 0) {
|
return NULL;
|
||||||
ssh_set_error_oom(sftp->session);
|
}
|
||||||
ssh_buffer_free(buffer);
|
msg = sftp_dequeue(sftp, id);
|
||||||
ssh_string_free(path_s);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (sftp_packet_write(sftp, SSH_FXP_READLINK, buffer) < 0) {
|
|
||||||
ssh_buffer_free(buffer);
|
|
||||||
ssh_string_free(path_s);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
ssh_buffer_free(buffer);
|
|
||||||
ssh_string_free(path_s);
|
|
||||||
|
|
||||||
while (msg == NULL) {
|
|
||||||
if (sftp_read_and_dispatch(sftp) < 0) {
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
msg = sftp_dequeue(sftp, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg->packet_type == SSH_FXP_NAME) {
|
if (msg->packet_type == SSH_FXP_NAME) {
|
||||||
/* we don't care about "count" */
|
/* we don't care about "count" */
|
||||||
ssh_buffer_get_u32(msg->payload, &ignored);
|
ssh_buffer_get_u32(msg->payload, &ignored);
|
||||||
/* we only care about the file name string */
|
/* we only care about the file name string */
|
||||||
link_s = ssh_buffer_get_ssh_string(msg->payload);
|
link_s = ssh_buffer_get_ssh_string(msg->payload);
|
||||||
sftp_message_free(msg);
|
sftp_message_free(msg);
|
||||||
if (link_s == NULL) {
|
if (link_s == NULL) {
|
||||||
/* TODO: what error to set here? */
|
/* TODO: what error to set here? */
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
lnk = ssh_string_to_char(link_s);
|
||||||
|
ssh_string_free(link_s);
|
||||||
|
|
||||||
|
return lnk;
|
||||||
|
} else if (msg->packet_type == SSH_FXP_STATUS) { /* bad response (error) */
|
||||||
|
status = parse_status_msg(msg);
|
||||||
|
sftp_message_free(msg);
|
||||||
|
if (status == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
||||||
|
"SFTP server: %s", status->errormsg);
|
||||||
|
status_msg_free(status);
|
||||||
|
} else { /* this shouldn't happen */
|
||||||
|
ssh_set_error(sftp->session, SSH_FATAL,
|
||||||
|
"Received message %d when attempting to set stats", msg->packet_type);
|
||||||
|
sftp_message_free(msg);
|
||||||
}
|
}
|
||||||
lnk = ssh_string_to_char(link_s);
|
|
||||||
ssh_string_free(link_s);
|
|
||||||
|
|
||||||
return lnk;
|
return NULL;
|
||||||
} else if (msg->packet_type == SSH_FXP_STATUS) { /* bad response (error) */
|
|
||||||
status = parse_status_msg(msg);
|
|
||||||
sftp_message_free(msg);
|
|
||||||
if (status == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
|
||||||
"SFTP server: %s", status->errormsg);
|
|
||||||
status_msg_free(status);
|
|
||||||
} else { /* this shouldn't happen */
|
|
||||||
ssh_set_error(sftp->session, SSH_FATAL,
|
|
||||||
"Received message %d when attempting to set stats", msg->packet_type);
|
|
||||||
sftp_message_free(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static sftp_statvfs_t sftp_parse_statvfs(sftp_session sftp, ssh_buffer buf) {
|
static sftp_statvfs_t sftp_parse_statvfs(sftp_session sftp, ssh_buffer buf) {
|
||||||
|
|||||||
Reference in New Issue
Block a user