mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-12-08 03:42:12 +03:00
sftp: Reformat read/write functions
Signed-off-by: Jakub Jelen <jjelen@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
113
src/sftp.c
113
src/sftp.c
@@ -1131,13 +1131,15 @@ void sftp_file_set_blocking(sftp_file handle)
|
||||
}
|
||||
|
||||
/* Read from a file using an opened sftp file handle. */
|
||||
ssize_t sftp_read(sftp_file handle, void *buf, size_t count) {
|
||||
sftp_session sftp;
|
||||
ssize_t
|
||||
sftp_read(sftp_file handle, void *buf, size_t count)
|
||||
{
|
||||
sftp_session sftp = NULL;
|
||||
sftp_message msg = NULL;
|
||||
sftp_status_message status;
|
||||
ssh_string datastring;
|
||||
ssh_string datastring = NULL;
|
||||
size_t datalen;
|
||||
ssh_buffer buffer;
|
||||
ssh_buffer buffer = NULL;
|
||||
uint32_t id;
|
||||
int rc;
|
||||
|
||||
@@ -1179,7 +1181,7 @@ ssize_t sftp_read(sftp_file handle, void *buf, size_t count) {
|
||||
handle->handle,
|
||||
handle->offset,
|
||||
count);
|
||||
if (rc != SSH_OK){
|
||||
if (rc != SSH_OK) {
|
||||
ssh_set_error_oom(sftp->session);
|
||||
SSH_BUFFER_FREE(buffer);
|
||||
sftp_set_error(sftp, SSH_FX_FAILURE);
|
||||
@@ -1198,8 +1200,8 @@ ssize_t sftp_read(sftp_file handle, void *buf, size_t count) {
|
||||
|
||||
if (rc == SSH_AGAIN) {
|
||||
/*
|
||||
* file opened in non blocking mode and the response has not arrived yet.
|
||||
* Since we cannot block, return 0 as the number of bytes read.
|
||||
* file opened in non blocking mode and the response has not arrived
|
||||
* yet. Since we cannot block, return 0 as the number of bytes read.
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
@@ -1220,25 +1222,30 @@ ssize_t sftp_read(sftp_file handle, void *buf, size_t count) {
|
||||
default:
|
||||
break;
|
||||
}
|
||||
ssh_set_error(sftp->session,SSH_REQUEST_DENIED,
|
||||
"SFTP server: %s", status->errormsg);
|
||||
ssh_set_error(sftp->session,
|
||||
SSH_REQUEST_DENIED,
|
||||
"SFTP server: %s",
|
||||
status->errormsg);
|
||||
status_msg_free(status);
|
||||
return -1;
|
||||
case SSH_FXP_DATA:
|
||||
datastring = ssh_buffer_get_ssh_string(msg->payload);
|
||||
sftp_message_free(msg);
|
||||
if (datastring == NULL) {
|
||||
ssh_set_error(sftp->session, SSH_FATAL,
|
||||
ssh_set_error(sftp->session,
|
||||
SSH_FATAL,
|
||||
"Received invalid DATA packet from sftp server");
|
||||
return -1;
|
||||
}
|
||||
|
||||
datalen = ssh_string_len(datastring);
|
||||
if (datalen > count) {
|
||||
ssh_set_error(sftp->session, SSH_FATAL,
|
||||
ssh_set_error(sftp->session,
|
||||
SSH_FATAL,
|
||||
"Received a too big DATA packet from sftp server: "
|
||||
"%zu and asked for %zu",
|
||||
datalen, count);
|
||||
datalen,
|
||||
count);
|
||||
SSH_STRING_FREE(datastring);
|
||||
return -1;
|
||||
}
|
||||
@@ -1247,8 +1254,10 @@ ssize_t sftp_read(sftp_file handle, void *buf, size_t count) {
|
||||
SSH_STRING_FREE(datastring);
|
||||
return datalen;
|
||||
default:
|
||||
ssh_set_error(sftp->session, SSH_FATAL,
|
||||
"Received message %d during read!", msg->packet_type);
|
||||
ssh_set_error(sftp->session,
|
||||
SSH_FATAL,
|
||||
"Received message %d during read!",
|
||||
msg->packet_type);
|
||||
sftp_message_free(msg);
|
||||
sftp_set_error(sftp, SSH_FX_BAD_MESSAGE);
|
||||
return -1;
|
||||
@@ -1258,9 +1267,11 @@ ssize_t sftp_read(sftp_file handle, void *buf, size_t count) {
|
||||
}
|
||||
|
||||
/* Start an asynchronous read from a file using an opened sftp file handle. */
|
||||
int sftp_async_read_begin(sftp_file file, uint32_t len){
|
||||
int
|
||||
sftp_async_read_begin(sftp_file file, uint32_t len)
|
||||
{
|
||||
sftp_session sftp = file->sftp;
|
||||
ssh_buffer buffer;
|
||||
ssh_buffer buffer = NULL;
|
||||
uint32_t id;
|
||||
int rc;
|
||||
|
||||
@@ -1273,12 +1284,7 @@ int sftp_async_read_begin(sftp_file file, uint32_t len){
|
||||
|
||||
id = sftp_get_new_id(sftp);
|
||||
|
||||
rc = ssh_buffer_pack(buffer,
|
||||
"dSqd",
|
||||
id,
|
||||
file->handle,
|
||||
file->offset,
|
||||
len);
|
||||
rc = ssh_buffer_pack(buffer, "dSqd", id, file->handle, file->offset, len);
|
||||
if (rc != SSH_OK) {
|
||||
ssh_set_error_oom(sftp->session);
|
||||
SSH_BUFFER_FREE(buffer);
|
||||
@@ -1297,11 +1303,13 @@ int sftp_async_read_begin(sftp_file file, uint32_t len){
|
||||
}
|
||||
|
||||
/* Wait for an asynchronous read to complete and save the data. */
|
||||
int sftp_async_read(sftp_file file, void *data, uint32_t size, uint32_t id){
|
||||
sftp_session sftp;
|
||||
int
|
||||
sftp_async_read(sftp_file file, void *data, uint32_t size, uint32_t id)
|
||||
{
|
||||
sftp_session sftp = NULL;
|
||||
sftp_message msg = NULL;
|
||||
sftp_status_message status;
|
||||
ssh_string datastring;
|
||||
ssh_string datastring = NULL;
|
||||
int rc, err = SSH_OK;
|
||||
size_t len;
|
||||
|
||||
@@ -1329,8 +1337,10 @@ int sftp_async_read(sftp_file file, void *data, uint32_t size, uint32_t id){
|
||||
}
|
||||
sftp_set_error(sftp, status->status);
|
||||
if (status->status != SSH_FX_EOF) {
|
||||
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
||||
"SFTP server : %s", status->errormsg);
|
||||
ssh_set_error(sftp->session,
|
||||
SSH_REQUEST_DENIED,
|
||||
"SFTP server : %s",
|
||||
status->errormsg);
|
||||
err = SSH_ERROR;
|
||||
} else {
|
||||
file->eof = 1;
|
||||
@@ -1341,15 +1351,18 @@ int sftp_async_read(sftp_file file, void *data, uint32_t size, uint32_t id){
|
||||
datastring = ssh_buffer_get_ssh_string(msg->payload);
|
||||
sftp_message_free(msg);
|
||||
if (datastring == NULL) {
|
||||
ssh_set_error(sftp->session, SSH_FATAL,
|
||||
ssh_set_error(sftp->session,
|
||||
SSH_FATAL,
|
||||
"Received invalid DATA packet from sftp server");
|
||||
return SSH_ERROR;
|
||||
}
|
||||
if (ssh_string_len(datastring) > size) {
|
||||
ssh_set_error(sftp->session, SSH_FATAL,
|
||||
ssh_set_error(sftp->session,
|
||||
SSH_FATAL,
|
||||
"Received a too big DATA packet from sftp server: "
|
||||
"%zu and asked for %" PRIu32,
|
||||
ssh_string_len(datastring), size);
|
||||
ssh_string_len(datastring),
|
||||
size);
|
||||
SSH_STRING_FREE(datastring);
|
||||
return SSH_ERROR;
|
||||
}
|
||||
@@ -1360,7 +1373,10 @@ int sftp_async_read(sftp_file file, void *data, uint32_t size, uint32_t id){
|
||||
SSH_STRING_FREE(datastring);
|
||||
return (int)len;
|
||||
default:
|
||||
ssh_set_error(sftp->session,SSH_FATAL,"Received message %d during read!",msg->packet_type);
|
||||
ssh_set_error(sftp->session,
|
||||
SSH_FATAL,
|
||||
"Received message %d during read!",
|
||||
msg->packet_type);
|
||||
sftp_message_free(msg);
|
||||
sftp_set_error(sftp, SSH_FX_BAD_MESSAGE);
|
||||
return SSH_ERROR;
|
||||
@@ -1369,11 +1385,13 @@ int sftp_async_read(sftp_file file, void *data, uint32_t size, uint32_t id){
|
||||
return SSH_ERROR;
|
||||
}
|
||||
|
||||
ssize_t sftp_write(sftp_file file, const void *buf, size_t count) {
|
||||
sftp_session sftp;
|
||||
ssize_t
|
||||
sftp_write(sftp_file file, const void *buf, size_t count)
|
||||
{
|
||||
sftp_session sftp = NULL;
|
||||
sftp_message msg = NULL;
|
||||
sftp_status_message status;
|
||||
ssh_buffer buffer;
|
||||
ssh_buffer buffer = NULL;
|
||||
uint32_t id;
|
||||
ssize_t len;
|
||||
size_t packetlen;
|
||||
@@ -1415,20 +1433,19 @@ ssize_t sftp_write(sftp_file file, const void *buf, size_t count) {
|
||||
file->offset,
|
||||
count, /* len of datastring */
|
||||
(size_t)count, buf);
|
||||
if (rc != SSH_OK){
|
||||
if (rc != SSH_OK) {
|
||||
ssh_set_error_oom(sftp->session);
|
||||
SSH_BUFFER_FREE(buffer);
|
||||
sftp_set_error(sftp, SSH_FX_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
len = sftp_packet_write(file->sftp, SSH_FXP_WRITE, buffer);
|
||||
packetlen=ssh_buffer_get_len(buffer);
|
||||
packetlen = ssh_buffer_get_len(buffer);
|
||||
SSH_BUFFER_FREE(buffer);
|
||||
if (len < 0) {
|
||||
return -1;
|
||||
} else if ((size_t)len != packetlen) {
|
||||
SSH_LOG(SSH_LOG_PACKET,
|
||||
"Could not write as much data as expected");
|
||||
SSH_LOG(SSH_LOG_PACKET, "Could not write as much data as expected");
|
||||
}
|
||||
|
||||
/* Wait for the response in blocking mode */
|
||||
@@ -1453,14 +1470,18 @@ ssize_t sftp_write(sftp_file file, const void *buf, size_t count) {
|
||||
default:
|
||||
break;
|
||||
}
|
||||
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
||||
"SFTP server: %s", status->errormsg);
|
||||
ssh_set_error(sftp->session,
|
||||
SSH_REQUEST_DENIED,
|
||||
"SFTP server: %s",
|
||||
status->errormsg);
|
||||
file->offset += count;
|
||||
status_msg_free(status);
|
||||
return -1;
|
||||
default:
|
||||
ssh_set_error(sftp->session, SSH_FATAL,
|
||||
"Received message %d during write!", msg->packet_type);
|
||||
ssh_set_error(sftp->session,
|
||||
SSH_FATAL,
|
||||
"Received message %d during write!",
|
||||
msg->packet_type);
|
||||
sftp_message_free(msg);
|
||||
sftp_set_error(sftp, SSH_FX_BAD_MESSAGE);
|
||||
return -1;
|
||||
@@ -1470,7 +1491,9 @@ ssize_t sftp_write(sftp_file file, const void *buf, size_t count) {
|
||||
}
|
||||
|
||||
/* Seek to a specific location in a file. */
|
||||
int sftp_seek(sftp_file file, uint32_t new_offset) {
|
||||
int
|
||||
sftp_seek(sftp_file file, uint32_t new_offset)
|
||||
{
|
||||
if (file == NULL) {
|
||||
return -1;
|
||||
}
|
||||
@@ -1481,7 +1504,9 @@ int sftp_seek(sftp_file file, uint32_t new_offset) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sftp_seek64(sftp_file file, uint64_t new_offset) {
|
||||
int
|
||||
sftp_seek64(sftp_file file, uint64_t new_offset)
|
||||
{
|
||||
if (file == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user