Commit 3660fddf authored by Niels Möller's avatar Niels Möller
Browse files

Deleted old code.

(sftp_client_get_status): New function, for parsing -02 draft
status messages. Use it everywhere.

Rev: src/sftp/sftp-test-client.c:1.10
parent 88405a0c
......@@ -60,6 +60,24 @@ sftp_client_get_id(struct sftp_input *i, UINT32 expected)
&& (id == expected));
}
static int
sftp_client_get_status(struct sftp_input *i, UINT32 *status)
{
int res;
UINT8 *msg = NULL;
UINT8 *language = NULL;
UINT32 length;
res = (sftp_get_uint32(i, status)
&& (msg = sftp_get_string(i, &length))
&& (language = sftp_get_string(i, &length)));
sftp_free_string(msg); sftp_free_string(language);
return res;
}
static void
fork_server(char *name,
struct client_ctx *ctx)
......@@ -219,7 +237,9 @@ do_ls(struct client_ctx *ctx, const char *name)
else
if ( msg == SSH_FXP_STATUS )
{
sftp_get_uint32(ctx->i, &status);
if (!sftp_client_get_status(ctx->i, &status))
FATAL("Invalid SSH_FXP_STATUS message.");
lsloop=0; /* End of loop - EOF or failue */
if ( status != SSH_FX_EOF)
......@@ -246,7 +266,7 @@ do_ls(struct client_ctx *ctx, const char *name)
return (sftp_get_uint8(ctx->i, &msg)
&& (msg==SSH_FXP_STATUS)
&& sftp_client_get_id(ctx->i, id)
&& sftp_get_uint32(ctx->i, &status)
&& sftp_client_get_status(ctx->i, &status)
&& (status == SSH_FX_OK)
&& !failure);
}
......@@ -306,7 +326,7 @@ do_close(struct client_ctx *ctx,
return (sftp_get_uint8(ctx->i, &msg)
&& (msg==SSH_FXP_STATUS)
&& sftp_client_get_id(ctx->i, id)
&& sftp_get_uint32(ctx->i, &status)
&& sftp_client_get_status(ctx->i, &status)
&& (status == SSH_FX_OK));
}
......@@ -386,7 +406,9 @@ do_get(struct client_ctx *ctx,
}
case SSH_FXP_STATUS:
{
sftp_get_uint32(ctx->i, &status);
if (!sftp_client_get_status(ctx->i, &status))
FATAL("Invalid SSH_FXP_STATUS message.");
getloop=0; /* End of loop - EOF or failue */
if ( status != SSH_FX_EOF)
......@@ -409,196 +431,6 @@ do_get(struct client_ctx *ctx,
return ok;
}
#if 0
static int
do_get(struct client_ctx *ctx,
const char *lname,
const char *rname,
int cont,
off_t contat)
{
UINT32 id;
UINT32 status;
UINT8* handle;
UINT32 hlength;
off_t curpos=0;
UINT32 rdatal;
UINT8* rdata;
struct sftp_attrib a;
UINT8 msg;
int getloop=1;
int failure=0;
int fd=-1;
id=sftp_client_new_id();
sftp_clear_attrib(&a); /* Don't pass any information on how to open */
sftp_set_msg(ctx->o, SSH_FXP_OPEN); /* Send a OPEN message */
sftp_set_id(ctx->o, id);
sftp_put_string(ctx->o, strlen(rname), rname );
sftp_put_uint32(ctx->o, SSH_FXF_READ ); /* Read mode only */
sftp_put_attrib(ctx->o, &a);
if (!sftp_write_packet(ctx->o))
return 0;
if (sftp_read_packet(ctx->i) <= 0)
return 0;
if ( !(sftp_get_uint8(ctx->i, &msg) && /* None of these may fail */
msg==SSH_FXP_HANDLE &&
sftp_client_get_id(ctx->i, id) &&
(handle=sftp_get_string(ctx->i, &hlength))
))
return 0;
/* OK, we now have a successfull call and file handle */
id=sftp_client_new_id();
sftp_set_msg(ctx->o, SSH_FXP_FSTAT); /* Send a FSTAT message */
sftp_set_id(ctx->o, id);
sftp_put_string(ctx->o, hlength, handle );
if (!sftp_write_packet(ctx->o))
return 0;
if (sftp_read_packet(ctx->i) <= 0)
return 0;
if ( !(sftp_get_uint8(ctx->i, &msg) && /* None of these may fail */
msg==SSH_FXP_ATTRS &&
sftp_client_get_id(ctx->i, id) &&
sftp_get_attrib(ctx->i, &a)
))
{
getloop=0; /* fstat failed - skip the loop */
failure=1; /* failure (but we still need to close the file */
}
else
{
int openmode=0666; /* Default mode for open if server doesn't say
otherwise */
if ( a.flags & SSH_FILEXFER_ATTR_PERMISSIONS )
openmode=a.permissions & 0777;
if ( (fd=open( lname, O_CREAT | O_WRONLY, openmode )) < 0 )
{
getloop=0; /* Couldn't open local file - skip loop */
failure=1;
}
if ( !cont ) /* Continue an old transfer */
curpos=0;
else
{
if ( contat ) /* Position to continue at given? */
curpos=contat;
else
{
struct stat st;
if ( ! fstat(fd, &st ) ) /* Stat local file */
{
/* Failed (Fixme; is it plausible that the fstat fails but the
rest works?) */
failure=1;
getloop=0;
}
else
curpos=st.st_size; /* Continue at end of local file */
}
}
}
while ( getloop )
{
id=sftp_client_new_id();
sftp_set_msg(ctx->o, SSH_FXP_READ); /* Send a read request */
sftp_set_id(ctx->o, id);
sftp_put_string(ctx->o, hlength, handle);
sftp_put_uint64(ctx->o, curpos);
sftp_put_uint32(ctx->o, SFTP_XFER_BLOCKSIZE);
if (!sftp_write_packet(ctx->o))
return 0;
if (sftp_read_packet(ctx->i) <= 0)
return 0;
if( !(sftp_get_uint8(ctx->i, &msg) &&
sftp_client_get_id(ctx->i, id)
))
return 0;
if ( msg == SSH_FXP_DATA )
{
rdata=sftp_get_string_auto(ctx->i, &rdatal);
/* Fixme; display this somehow */
if ( ( -1 == lseek(fd,curpos, SEEK_SET) ) ||
( -1 == write(fd,rdata,rdatal) ) )
{
getloop=0;
failure=1;
}
else
curpos+=rdatal;
}
else
if ( msg == SSH_FXP_STATUS )
{
sftp_get_uint32(ctx->i, &status);
getloop=0; /* End of loop - EOF or failue */
if ( status != SSH_FX_EOF)
failure=1;
}
}
/* Time to close */
id=sftp_client_new_id();
sftp_set_msg(ctx->o, SSH_FXP_CLOSE); /* Send a close message */
sftp_set_id(ctx->o, id);
sftp_put_string(ctx->o, hlength, handle);
sftp_free_string(handle);
if (!sftp_write_packet(ctx->o))
return 0;
if (sftp_read_packet(ctx->i) <= 0)
return 0;
if ( fd >= 0 && close(fd) )
failure=1;
if ( !(
sftp_get_uint8(ctx->i, &msg) && /* None of these may fail */
msg==SSH_FXP_STATUS &&
sftp_client_get_id(ctx->i, id) &&
sftp_get_uint32(ctx->i, &status) &&
status == SSH_FX_OK &&
!failure
))
return 0;
return 1;
}
#endif
static int
do_put(struct client_ctx *ctx,
const char *name,
......@@ -668,7 +500,9 @@ do_put(struct client_ctx *ctx,
if ( msg == SSH_FXP_STATUS )
{
sftp_get_uint32(ctx->i, &status);
if (!sftp_client_get_status(ctx->i, &status))
FATAL("Invalid SSH_FXP_STATUS message.");
putloop=0; /* End of loop - EOF or failue */
if ( status != SSH_FX_OK )
......@@ -687,191 +521,6 @@ do_put(struct client_ctx *ctx,
return ok;
}
#if 0
static int
do_put(struct client_ctx *ctx,
const char *lname,
const char *rname,
int cont,
off_t contat)
{
UINT32 id;
UINT32 status;
UINT8* handle;
UINT32 hlength;
off_t curpos=0;
UINT8* wdata;
ssize_t rbytes;
struct stat st;
struct sftp_attrib a;
UINT8 msg;
int putloop=1;
int failure=0;
int fd=-1;
sftp_clear_attrib(&a);
if ( ((fd=open( lname, O_RDONLY)) < 0) ||
fstat(fd, &st) )
return 0; /* Open or stat failed */
wdata=xmalloc(SFTP_XFER_BLOCKSIZE);
a.flags= ( SSH_FILEXFER_ATTR_PERMISSIONS |
SSH_FILEXFER_ATTR_UIDGID ); /* Fixme; what should we pass? */
a.permissions=st.st_mode & 0777; /* Fixme; Macro for 0777? */
a.uid=st.st_uid;
a.gid=st.st_gid;
id=sftp_client_new_id();
sftp_set_msg(ctx->o, SSH_FXP_OPEN); /* Send a OPEN message */
sftp_set_id(ctx->o, id);
sftp_put_string(ctx->o, strlen(rname), rname );
sftp_put_uint32(ctx->o, SSH_FXF_CREAT | SSH_FXF_WRITE );
sftp_put_attrib(ctx->o, &a);
if (!sftp_write_packet(ctx->o))
return 0;
if (sftp_read_packet(ctx->i) <= 0)
return 0;
if ( !(
sftp_get_uint8(ctx->i, &msg) && /* None of these may fail */
msg==SSH_FXP_HANDLE &&
sftp_client_get_id(ctx->i, id) &&
(handle=sftp_get_string(ctx->i, &hlength))
))
return 0;
/* OK, we now have a successfull call and file handle */
id=sftp_client_new_id();
sftp_set_msg(ctx->o, SSH_FXP_FSTAT); /* Send a FSTAT message */
sftp_set_id(ctx->o, id);
sftp_put_string(ctx->o, hlength, handle );
if (!sftp_write_packet(ctx->o))
return 0;
if (sftp_read_packet(ctx->i) <= 0)
return 0;
if ( !(
sftp_get_uint8(ctx->i, &msg) && /* None of these may fail */
msg==SSH_FXP_ATTRS &&
sftp_client_get_id(ctx->i, id) &&
sftp_get_attrib(ctx->i, &a)
))
{
putloop=0; /* fstat failed - skip the loop */
failure=1; /* failure (but we still need to close the file) */
}
if ( !cont ) /* Continue an old transfer? */
curpos=0;
else
{
if ( contat ) /* Position to continue at given? */
curpos=contat;
else
{
if ( a.flags & SSH_FILEXFER_ATTR_SIZE )
curpos=a.size;
/* Continue at end of remote file */
}
}
while ( putloop )
{
if (
(-1 == ( lseek(fd, curpos ,SEEK_SET) ) ) ||
(-1 == (rbytes=read(fd, wdata, SFTP_XFER_BLOCKSIZE )) )
)
{
putloop=0;
failure=1;
break;
}
if ( !rbytes )
putloop=0;
else
{
id=sftp_client_new_id();
sftp_set_msg(ctx->o, SSH_FXP_WRITE); /* Send a read request */
sftp_set_id(ctx->o, id);
sftp_put_string(ctx->o, hlength, handle);
sftp_put_uint64(ctx->o, curpos);
sftp_put_string(ctx->o, rbytes, wdata);
if (!sftp_write_packet(ctx->o))
return 0;
curpos+=rbytes;
if (sftp_read_packet(ctx->i) <= 0)
return 0;
if( !(
sftp_get_uint8(ctx->i, &msg) &&
sftp_client_get_id(ctx->i, id)
))
return 0;
if ( msg == SSH_FXP_STATUS )
{
sftp_get_uint32(ctx->i, &status);
putloop=0; /* End of loop - EOF or failue */
if ( status != SSH_FX_OK )
failure=1;
}
}
}
/* Time to close */
id=sftp_client_new_id();
sftp_set_msg(ctx->o, SSH_FXP_CLOSE); /* Send a close message */
sftp_set_id(ctx->o, id);
sftp_put_string(ctx->o, hlength, handle);
sftp_free_string(handle);
if (!sftp_write_packet(ctx->o))
return 0;
if (sftp_read_packet(ctx->i) <= 0)
return 0;
if ( fd >= 0 && close(fd) )
failure=1;
free(wdata);
if ( !(
sftp_get_uint8(ctx->i, &msg) && /* None of these may fail */
msg==SSH_FXP_STATUS &&
sftp_client_get_id(ctx->i, id) &&
sftp_get_uint32(ctx->i, &status) &&
status == SSH_FX_OK &&
!failure
))
return 0;
return 1;
}
#endif
static int
do_stat(struct client_ctx *ctx, const char *name)
{
......
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