1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-08-05 20:55:46 +03:00

Reformat the test sftpserver

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
This commit is contained in:
Jakub Jelen
2023-01-17 14:25:02 +01:00
parent 13b2727023
commit f1f766f14f

View File

@@ -412,7 +412,8 @@ static int readdir_long_name(char* z_file_name, struct stat* z_st, char* z_long_
return SSH_OK;
}
static int process_open(sftp_client_message client_msg) {
static int process_open(sftp_client_message client_msg)
{
int ret = SSH_OK;
const char *filename = sftp_client_message_get_filename(client_msg);
uint32_t msg_flag = sftp_client_message_get_flags(client_msg);
@@ -421,6 +422,8 @@ static int process_open(sftp_client_message client_msg) {
int handle_ind = -1;
int status;
SSH_LOG(SSH_LOG_PROTOCOL, "try to open file: %s", filename);
if (( (msg_flag&(uint32_t)SSH_FXF_READ) == SSH_FXF_READ) &&
( (msg_flag&(uint32_t)SSH_FXF_WRITE) == SSH_FXF_WRITE)) {
file_flag = O_RDWR; //file must exist
@@ -437,40 +440,46 @@ static int process_open(sftp_client_message client_msg) {
}
fd = open(filename, file_flag, 0600);
if(fd == -1){
if (fd == -1) {
status = unix_errno_to_ssh_stat(errno);
printf("error open file with error: %d\n", errno);
SSH_LOG(SSH_LOG_PROTOCOL, "error open file with error: %d", errno);
sftp_reply_status(client_msg, status, "Write error");
ret = SSH_ERROR;
return ret;
}
handle_ind = add_handle(FILE_HANDLE, NULL, fd, filename, client_msg->sftp);
if(handle_ind >= 0){
if (handle_ind >= 0) {
void *handle_ptr = &s_handle_table[handle_ind];
ssh_string handle_s = sftp_handle_alloc(client_msg->sftp, handle_ptr);
sftp_reply_handle(client_msg, handle_s);
ssh_string_free(handle_s);
ret = SSH_OK;
}else {
} else {
close(fd);
printf("opening file failed");
SSH_LOG(SSH_LOG_PROTOCOL, "opening file failed", errno);
sftp_reply_status(client_msg, SSH_FX_FAILURE, "No handle available");
}
return ret;
}
static int process_read(sftp_client_message client_msg) {
static int process_read(sftp_client_message client_msg)
{
int ret = SSH_OK;
struct sftp_handle *client_handle = (struct sftp_handle*)sftp_handle(client_msg->sftp, client_msg->handle);
struct sftp_handle *client_handle;
uint32_t readn;
int fd;
char *buffer;
int rv;
client_handle = (struct sftp_handle*)sftp_handle(client_msg->sftp,
client_msg->handle);
SSH_LOG(SSH_LOG_PROTOCOL, "try to read a file from handle");
if (client_handle == NULL || client_handle->session_id != client_msg->sftp) {
printf("get wrong handle from msg\n");
SSH_LOG(SSH_LOG_PROTOCOL, "got wrong handle from msg");
sftp_reply_status(client_msg, SSH_FX_FAILURE, NULL);
return SSH_ERROR;
}
@@ -479,14 +488,15 @@ static int process_read(sftp_client_message client_msg) {
if (fd < 0) {
sftp_reply_status(client_msg, SSH_FX_INVALID_HANDLE, NULL);
printf("error reading file fd: %d\n", fd);
SSH_LOG(SSH_LOG_PROTOCOL, "error reading file fd: %d", fd);
ret = SSH_ERROR;
return ret;
}
rv = lseek(fd, client_msg->offset, SEEK_SET);
if (rv == -1) {
sftp_reply_status(client_msg, SSH_FX_FAILURE, NULL);
printf("error seeking file fd: %d at offset: %ld\n", fd, client_msg->offset);
SSH_LOG(SSH_LOG_PROTOCOL, "error seeking file fd: %d at offset: %ld",
fd, client_msg->offset);
ret = SSH_ERROR;
return ret;
}
@@ -494,28 +504,34 @@ static int process_read(sftp_client_message client_msg) {
buffer = malloc((client_msg->len)*sizeof(char));
readn = read(fd, buffer, client_msg->len);
if(readn > 0){
if (readn > 0) {
sftp_reply_data(client_msg, buffer, readn);
}else if(readn == 0) {
} else if (readn == 0) {
sftp_reply_status(client_msg, SSH_FX_EOF, "EOF encountered");
}else {
} else {
sftp_reply_status(client_msg, SSH_FX_FAILURE, NULL);
printf("read file error!\n");
SSH_LOG(SSH_LOG_PROTOCOL, "read file error!");
}
return ret;
}
static int process_write(sftp_client_message client_msg) {
static int process_write(sftp_client_message client_msg)
{
int ret = SSH_OK;
struct sftp_handle *client_handle = (struct sftp_handle*)sftp_handle(client_msg->sftp, client_msg->handle);
struct sftp_handle *client_handle;
uint32_t writen;
int fd;
const char *msg_data;
uint32_t len;
int rv;
client_handle = (struct sftp_handle*)sftp_handle(client_msg->sftp,
client_msg->handle);
SSH_LOG(SSH_LOG_PROTOCOL, "try to write a file from handle");
if (client_handle == NULL || client_handle->session_id != client_msg->sftp) {
printf("get wrong handle from msg\n");
SSH_LOG(SSH_LOG_PROTOCOL, "got wrong handle from msg");
sftp_reply_status(client_msg, SSH_FX_FAILURE, NULL);
return SSH_ERROR;
}
@@ -524,7 +540,7 @@ static int process_write(sftp_client_message client_msg) {
if (fd < 0) {
sftp_reply_status(client_msg, SSH_FX_INVALID_HANDLE, NULL);
printf("write file fd error!\n");
SSH_LOG(SSH_LOG_PROTOCOL, "write file fd error!");
ret = SSH_ERROR;
return ret;
}
@@ -535,7 +551,8 @@ static int process_write(sftp_client_message client_msg) {
rv = lseek(fd, client_msg->offset, SEEK_SET);
if (rv == -1) {
sftp_reply_status(client_msg, SSH_FX_FAILURE, NULL);
printf("error seeking file at offset: %ld\n", client_msg->offset);
SSH_LOG(SSH_LOG_PROTOCOL, "error seeking file at offset: %ld\n",
client_msg->offset);
}
writen = write(fd, msg_data, len);
if(writen == len) {
@@ -547,9 +564,15 @@ static int process_write(sftp_client_message client_msg) {
return ret;
}
static int process_close(sftp_client_message client_msg) {
static int process_close(sftp_client_message client_msg)
{
int ret = SSH_OK;
struct sftp_handle *client_handle = (struct sftp_handle*)sftp_handle(client_msg->sftp, client_msg->handle);
struct sftp_handle *client_handle;
client_handle = (struct sftp_handle*)sftp_handle(client_msg->sftp,
client_msg->handle);
SSH_LOG(SSH_LOG_PROTOCOL, "try to close handle");
if (client_handle == NULL) {
printf("get wrong handle from msg\n");
@@ -561,19 +584,22 @@ static int process_close(sftp_client_message client_msg) {
reinit_single_handle(client_handle);
sftp_reply_status(client_msg, SSH_FX_OK, NULL);
} else {
printf("closing file failed\n");
SSH_LOG(SSH_LOG_PROTOCOL, "closing file failed");
sftp_reply_status(client_msg, SSH_FX_BAD_MESSAGE, "Invalid handle");
}
return ret;
}
static int process_opendir(sftp_client_message client_msg) {
static int process_opendir(sftp_client_message client_msg)
{
int ret = SSH_OK;
DIR *dir = NULL;
const char *dir_name = sftp_client_message_get_filename(client_msg);
int handle_ind = -1;
SSH_LOG(SSH_LOG_PROTOCOL, "try to open dir: %s", dir_name);
dir = opendir(dir_name);
if (dir == NULL) {
sftp_reply_status(client_msg, SSH_FX_NO_SUCH_FILE, "No such directory");
@@ -740,7 +766,8 @@ static int process_lstat(sftp_client_message client_msg) {
return ret;
}
static int process_readlink(sftp_client_message client_msg) {
static int process_readlink(sftp_client_message client_msg)
{
int ret = SSH_OK;
const char *filename = sftp_client_message_get_filename(client_msg);
char buf[PATH_MAX];
@@ -748,16 +775,18 @@ static int process_readlink(sftp_client_message client_msg) {
const char *err_msg;
int status = SSH_FX_OK;
if (filename==NULL) {
if (filename == NULL) {
sftp_reply_status(client_msg, SSH_FX_NO_SUCH_FILE, "File name error");
return SSH_ERROR;
}
SSH_LOG(SSH_LOG_PROTOCOL, "read link : %s", filename);
len = readlink(filename, buf, sizeof(buf) - 1);
if (len < 0) {
printf("read link error with reason: %d\n", errno);
status = unix_errno_to_ssh_stat(errno);
err_msg = ssh_str_error(status);
SSH_LOG(SSH_LOG_PROTOCOL, "read link error with reason: %s", err_msg);
sftp_reply_status(client_msg, status, err_msg);
ret = SSH_ERROR;
} else {
@@ -768,13 +797,16 @@ static int process_readlink(sftp_client_message client_msg) {
return ret;
}
static int process_symlink(sftp_client_message client_msg) {
static int process_symlink(sftp_client_message client_msg)
{
int ret = SSH_OK;
const char *destpath = sftp_client_message_get_filename(client_msg);
const char *srcpath = ssh_string_get_char(client_msg->data);
int status = SSH_FX_OK;
int rv;
// printf("try to create link with src: %s and dest: %s \n", srcpath, destpath);
SSH_LOG(SSH_LOG_PROTOCOL, "try to create link with src: %s and dest: %s",
srcpath, destpath);
if (srcpath == NULL || destpath == NULL) {
sftp_reply_status(client_msg, SSH_FX_NO_SUCH_FILE, "File name error");
@@ -785,7 +817,9 @@ static int process_symlink(sftp_client_message client_msg) {
if (rv < 0) {
status = unix_errno_to_ssh_stat(errno);
printf("error symlink with error: %d\n", errno);
sftp_reply_status(client_msg, status, "Write error");
SSH_LOG(SSH_LOG_PROTOCOL, "error symlink with error: %s\n",
strerror(errno));
sftp_reply_status(client_msg, status, strerror(errno));
ret = SSH_ERROR;
} else {
sftp_reply_status(client_msg, SSH_FX_OK, "write success");
@@ -794,15 +828,19 @@ static int process_symlink(sftp_client_message client_msg) {
return ret;
}
static int process_remove(sftp_client_message client_msg) {
static int process_remove(sftp_client_message client_msg)
{
int ret = SSH_OK;
const char *filename = sftp_client_message_get_filename(client_msg);
int rv;
int status = SSH_FX_OK;
SSH_LOG(SSH_LOG_PROTOCOL, "try to remove: %s ", filename);
rv = unlink(filename);
if (rv < 0) {
printf("unlink error with reason: %d\n", errno);
SSH_LOG(SSH_LOG_PROTOCOL, "unlink error with reason: %s ",
strerror(errno));
status = unix_errno_to_ssh_stat(errno);
ret = SSH_ERROR;
}