Commit ef87a324 authored by Pontus Freyhult's avatar Pontus Freyhult

Use fds instead of FILE*s

Rev: src/sftp/buffer.c:1.12
Rev: src/sftp/buffer.h:1.8
Rev: src/sftp/sftp-test-client.c:1.11
Rev: src/sftp/sftp_bind.c:1.6
Rev: src/sftp/sftp_c.c:1.5
parent 992e2d00
......@@ -31,6 +31,8 @@
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#define FATAL(x) do { fputs("sftp-server: " x "\n", stderr); exit(EXIT_FAILURE); } while (0)
......@@ -55,7 +57,7 @@ do { \
struct sftp_input
{
FILE *f;
int fd;
UINT32 left;
/* Strings that we own */
......@@ -65,7 +67,7 @@ struct sftp_input
struct sftp_output
{
FILE *f;
int fd;
/* The message type is the first byte of a message, after the
* length. */
......@@ -93,10 +95,26 @@ sftp_get_data(struct sftp_input *i, UINT32 length, UINT8 *data)
{
if (sftp_check_input(i, length))
{
i->left -= length;
return (fread(data, 1, length, i->f) == length);
UINT8* buf = data;
int j;
while (length)
{
j = read(i->fd, buf, length);
while (-1==j && EINTR==errno) /* Loop over EINTR */
j = read(i->fd, buf, length);
if (-1==j) /* Error, and not EINTR */
return -1; /* Error */
buf += j; /* Move counters accordingly */
length -= j;
i->left -= j;
}
return 1; /* Success */
}
return 0;
return 0; /* FIXME: Return code? */
}
#define GET_DATA(i, buf) \
......@@ -172,11 +190,11 @@ sftp_get_eod(struct sftp_input *i)
/* Input */
struct sftp_input *
sftp_make_input(FILE *f)
sftp_make_input(int fd)
{
struct sftp_input *i = xmalloc(sizeof(struct sftp_input));
i->f = f;
i->fd = fd;
i->left = 0;
i->used_strings = 0;
......@@ -200,8 +218,9 @@ sftp_read_packet(struct sftp_input *i)
{
UINT8 buf[4];
int done;
int bytesread = 0;
if (i->left)
if (i->left) /* Unread data? */
{
UINT8 d;
......@@ -224,24 +243,23 @@ sftp_read_packet(struct sftp_input *i)
/* First, deallocate the strings. */
sftp_input_clear_strings(i);
done = fread(buf, 1, sizeof(buf), i->f);
switch (done)
while (bytesread < sizeof(buf))
{
case 0:
{
int err = feof(i->f) ? -1 : 0;
clearerr( i->f );
return err;
}
case 4:
i->left = READ_UINT32(buf);
return 1;
default:
return 0;
int j = read(i->fd, buf+bytesread, sizeof(buf)-bytesread);
while(-1==j && EINTR==errno) /* Loop over EINTR */
j = read(i->fd, buf+bytesread, sizeof(buf)-bytesread);
if (-1==j) /* Not EINTR but a real error */
return -1;
bytesread += j;
}
i->left = READ_UINT32(buf); /* Store packet size */
return 1;
}
/* Output */
......@@ -268,6 +286,7 @@ sftp_put_data(struct sftp_output *o, UINT32 length, const UINT8 *data)
o->i += length;
}
#define PUT_DATA(o, buf) \
(sftp_put_data((o), sizeof((buf)), (buf)))
......@@ -467,11 +486,11 @@ sftp_put_uint64(struct sftp_output *o, off_t value)
*/
struct sftp_output *
sftp_make_output(FILE *f)
sftp_make_output(int fd)
{
struct sftp_output *o = xmalloc(sizeof(struct sftp_output));
o->f = f;
o->fd = fd;
o->data = NULL;
o->size = 0;
o->i = 0;
......@@ -494,6 +513,8 @@ sftp_set_id(struct sftp_output *o, UINT32 id)
int
sftp_write_packet(struct sftp_output *o)
{
int j;
int written = 0;
UINT32 length = o->i + 5;
UINT8 buf[9];
......@@ -501,16 +522,39 @@ sftp_write_packet(struct sftp_output *o)
buf[4] = o->msg;
WRITE_UINT32(buf + 5, o->first);
if (fwrite(buf, 1, 9, o->f) != 9)
return 0;
if (fwrite(o->data, 1, o->i, o->f) != o->i)
return 0;
/* Write 9 bytes from buf */
o->i = 0;
while (written<9)
{
j = write(o->fd, buf+written, 9-written);
while (-1==j && errno==EINTR) /* Loop over EINTR */
j = write(o->fd, buf+written, 9-written);;
if (-1==j) /* Error, and not EINTR */
return -1; /* Error */
written += j; /* Move counters accordingly */
}
/* FIXME: Flushing after each packet is sub-optimal. */
if (fflush(o->f))
return 0;
/* Write o->i bytes from data */
written = 0; /* Reset counter */
while (written<o->i)
{
j = write(o->fd, o->data+written, o->i-written);
while (-1==j && errno==EINTR) /* Loop over EINTR */
j = write(o->fd, o->data+written, o->i-written);;
if (-1==j) /* Error, and not EINTR */
return -1; /* Error */
written += j; /* Move counters accordingly */
}
o->i = 0;
return 1;
}
......
......@@ -188,14 +188,14 @@ int
sftp_skip_extension(struct sftp_input *i);
/* Simple input and output objects based on FILE * */
/* Simple input and output objects */
#include <stdio.h>
struct sftp_input *
sftp_make_input(FILE *f);
sftp_make_input(int fd);
struct sftp_output *
sftp_make_output(FILE *f);
sftp_make_output(int fd);
int
sftp_packet_size(struct sftp_output* out);
......
......@@ -98,22 +98,11 @@ fork_server(char *name,
FATAL("fork failed.");
default: /* Parent */
{
FILE *i;
FILE *o;
close(stdin_pipe[0]);
close(stdout_pipe[1]);
i = fdopen(stdout_pipe[0], "r");
if (!i)
FATAL("fdopen stdout_pipe failed.");
o = fdopen(stdin_pipe[1], "w");
if (!o)
FATAL("fdopen stdin_pipe failed.");
ctx->i = sftp_make_input(i);
ctx->o = sftp_make_output(o);
ctx->i = sftp_make_input(stdout_pipe[0]);
ctx->o = sftp_make_output(stdin_pipe[1]);
return;
}
......
......@@ -29,9 +29,6 @@
#include "sftp_bind.h"
static FILE* to_transport;
static FILE* from_transport;
static int transport_pid = 0;
static int sftp_callbacks = 0;
......@@ -132,24 +129,11 @@ int lsftp_open_connection(char** argv, int argc)
FATAL("fork failed.");
default: /* Parent */
{
FILE *i;
FILE *o;
void* err;
close(stdin_pipe[0]); /* Close those ends of the pipe we won't use */
close(stdout_pipe[1]);
i = fdopen(stdout_pipe[0], "r");
if (!i)
FATAL("fdopen stdout_pipe failed.");
o = fdopen(stdin_pipe[1], "w");
if (!o)
FATAL("fdopen stdin_pipe failed.");
from_transport = i;
to_transport = o;
fd_from_transport = stdout_pipe[0];
fd_to_transport = stdin_pipe[1];
......@@ -368,12 +352,12 @@ int lsftp_close_connection()
signal( SIGCHLD, SIG_DFL ); /* Restore original signal handler before quitting */
i = fclose( to_transport ); /* Close outgoing */
i = close( fd_to_transport ); /* Close outgoing */
/* FIXME: Read what we can here */
/* FIXME: Should we report any errors here? */
i = i || fclose( from_transport );
i = i || close( fd_from_transport );
if( -1 != kill( transport_pid, SIGTERM ) ) /* Signal sent OK? */
{
......@@ -398,8 +382,8 @@ int lsftp_close_connection()
alarm( 0 ); /* Cancel any alarm */
transport_pid = 0; /* Make invalid */
to_transport = 0;
from_transport = 0;
fd_to_transport = 0;
fd_from_transport = 0;
/* FIXME: Free memory used by sftp_input in and sftp_output out? */
......@@ -420,8 +404,8 @@ int lsftp_handshake()
{
int failed;
in = sftp_make_input( from_transport );
out = sftp_make_output( to_transport );
in = sftp_make_input( fd_from_transport );
out = sftp_make_output( fd_to_transport );
failed = sftp_handshake( in, out );
......@@ -436,7 +420,7 @@ int lsftp_handshake()
while( readok )
{
char x;
readok = fread( &x, 1, 1, from_transport ); /* Try to read one byte */
readok = read( fd_from_transport, &x, 1 ); /* Try to read one byte */
fprintf( stderr, "%c", x);
}
......
......@@ -1614,12 +1614,32 @@ sftp_ls_main(struct sftp_callback *next,
/* Write string length before. Explicit casts */
sftp_store( &next->mem, &fnamelen, sizeof( UINT32 ) );
sftp_store( &next->mem, fname, fnamelen );
if( fname ) /* sftp_get_string succedeed */
{
sftp_store( &next->mem, &fnamelen, sizeof( UINT32 ) );
sftp_store( &next->mem, fname, fnamelen );
}
else
{
UINT32 zero = 0;
sftp_store( &next->mem, &zero, sizeof( UINT32 ) );
sftp_store( &next->mem, &zero, 0 );
}
if( longname ) /* sftp_get_string succedeed */
{
/* Write string length before */
sftp_store( &next->mem, &longnamelen, sizeof( UINT32 ) );
sftp_store( &next->mem, longname, longnamelen );
}
else
{
UINT32 zero = 0;
sftp_store( &next->mem, &zero, sizeof( UINT32 ) );
sftp_store( &next->mem, &zero, 0 );
}
/* Write string length before */
sftp_store( &next->mem, &longnamelen, sizeof( UINT32 ) );
sftp_store( &next->mem, longname, longnamelen );
/* Write length before */
sftp_store( &next->mem, &attriblen, sizeof( UINT32 ) );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment