From 7690b174f121007c511f40d69c70d72e6953b5a9 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Fri, 27 Jan 2023 13:30:33 +0200 Subject: [PATCH] retrieve --- GNUmakefile | 2 +- src/ftp.c | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----- src/ftp.h | 20 +++++++- src/main.c | 71 +++++++++++++++++++++++++- 4 files changed, 238 insertions(+), 14 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index 734fb67..8997898 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -3,7 +3,7 @@ obj = $(src:.c=.o) dep = $(src:.c=.d) bin = oftp -warn = -pedantic -Wall +warn = -pedantic -Wall -Wno-pointer-sign dbg = -g incdir = -Isrc diff --git a/src/ftp.c b/src/ftp.c index cf76a6b..f62a3a4 100644 --- a/src/ftp.c +++ b/src/ftp.c @@ -50,6 +50,9 @@ static int cproc_cwd(struct ftp *ftp, int code, const char *buf, void *cls); static int cproc_list(struct ftp *ftp, int code, const char *buf, void *cls); static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls); +static int cproc_xfer(struct ftp *ftp, int code, const char *buf, void *cls); +static void dproc_xfer(struct ftp *ftp, const char *buf, int sz, void *cls); + struct ftp *ftp_alloc(void) { @@ -151,15 +154,19 @@ int ftp_sockets(struct ftp *ftp, int *sockv, int maxsize) return sptr - sockv; } -static void exec_op(struct ftp *ftp, int op, const char *arg) +static void exec_op(struct ftp *ftp, struct ftp_op *fop) { - switch(op) { + switch(fop->op) { + case FTP_TYPE: + ftp_type(ftp, fop->arg); + break; + case FTP_PWD: ftp_pwd(ftp); break; case FTP_CHDIR: - ftp_chdir(ftp, arg); + ftp_chdir(ftp, fop->arg); break; case FTP_CDUP: @@ -167,15 +174,15 @@ static void exec_op(struct ftp *ftp, int op, const char *arg) break; case FTP_MKDIR: - ftp_mkdir(ftp, arg); + ftp_mkdir(ftp, fop->arg); break; case FTP_RMDIR: - ftp_rmdir(ftp, arg); + ftp_rmdir(ftp, fop->arg); break; case FTP_DEL: - ftp_delete(ftp, arg); + ftp_delete(ftp, fop->arg); break; case FTP_LIST: @@ -183,11 +190,15 @@ static void exec_op(struct ftp *ftp, int op, const char *arg) break; case FTP_RETR: - ftp_retrieve(ftp, arg); + ftp_retrieve(ftp, fop->arg); break; case FTP_STORE: - ftp_store(ftp, arg); + ftp_store(ftp, fop->arg); + break; + + case FTP_XFER: + ftp_transfer(ftp, fop->data); break; default: @@ -209,7 +220,7 @@ static void exec_queued(struct ftp *ftp) ftp->qhead = ftp->qhead->next; } - exec_op(ftp, fop->op, fop->arg); + exec_op(ftp, fop); free(fop->arg); free(fop); @@ -221,7 +232,10 @@ int ftp_queue(struct ftp *ftp, int op, const char *arg) struct ftp_op *fop; if(!ftp->busy && !ftp->qhead) { - exec_op(ftp, op, arg); + struct ftp_op tmp = {0}; + tmp.op = op; + tmp.arg = (char*)arg; + exec_op(ftp, &tmp); return 0; } @@ -248,6 +262,35 @@ int ftp_queue(struct ftp *ftp, int op, const char *arg) return 0; } +int ftp_queue_transfer(struct ftp *ftp, struct ftp_transfer *xfer) +{ + struct ftp_op *fop; + + if(!ftp->busy && !ftp->qhead) { + struct ftp_op tmp = {0}; + tmp.op = FTP_XFER; + tmp.data = xfer; + exec_op(ftp, &tmp); + return 0; + } + + if(!(fop = malloc(sizeof *fop))) { + return -1; + } + fop->op = FTP_XFER; + fop->arg = 0; + fop->data = xfer; + fop->next = 0; + + if(ftp->qhead) { + ftp->qtail->next = fop; + ftp->qtail = fop; + } else { + ftp->qhead = ftp->qtail = fop; + } + return 0; +} + int ftp_waitresp(struct ftp *ftp, long timeout) { fd_set rdset; @@ -538,11 +581,18 @@ static void proc_control(struct ftp *ftp, const char *buf) ftp->status = 0; errmsg("login failed\n"); break; + + default: + ftp->busy = 0; + + /* execute next operation if there's one queued */ + exec_queued(ftp); } } static int newconn(struct ftp *ftp) { + ftp_queue(ftp, FTP_TYPE, "I"); ftp_queue(ftp, FTP_PWD, 0); ftp_queue(ftp, FTP_LIST, 0); return 0; @@ -553,6 +603,16 @@ int ftp_update(struct ftp *ftp) return -1; } +int ftp_type(struct ftp *ftp, const char *type) +{ + int tc = toupper(type[0]); + if(tc != 'I' && tc != 'A') { + return -1; + } + sendcmd(ftp, "type %c", tc); + return 0; +} + int ftp_pwd(struct ftp *ftp) { sendcmd(ftp, "pwd"); @@ -615,7 +675,7 @@ int ftp_list(struct ftp *ftp) { struct recvbuf *rbuf; - if(prepare_data(ftp)) { + if(prepare_data(ftp) == -1) { return -1; } @@ -648,6 +708,25 @@ int ftp_store(struct ftp *ftp, const char *fname) return -1; } +#define XFER_BUF_SIZE 1024 + +int ftp_transfer(struct ftp *ftp, struct ftp_transfer *xfer) +{ + if(prepare_data(ftp) == -1) { + return -1; + } + + if(xfer->op == FTP_RETR) { + sendcmd(ftp, "retr %s", xfer->rname); + ftp->cproc = cproc_xfer; + ftp->dproc = dproc_xfer; + ftp->cproc_cls = ftp->dproc_cls = xfer; + } else { + /* TODO */ + } + return 0; +} + static int get_quoted_text(const char *str, char *buf) { int len; @@ -883,6 +962,64 @@ static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls) rbuf->size += sz; } +static int cproc_xfer(struct ftp *ftp, int code, const char *buf, void *cls) +{ + char *ptr; + struct ftp_transfer *xfer = cls; + + if(code < 200) { + /* expect more */ + if(code == 150 && (ptr = strchr(buf, '('))) { + /* update total size */ + sscanf(ptr, "(%ld bytes)", &xfer->total); + } + return 1; + } + + if(code >= 400) { + errmsg("failed to retrieve file\n"); + } + return 0; +} + +static void dproc_xfer(struct ftp *ftp, const char *buf, int sz, void *cls) +{ + struct ftp_transfer *xfer = cls; + + if(xfer->op == FTP_RETR) { + if(sz == 0) { + /* EOF condition, got the whole file */ + if(xfer->fp) { + fclose(xfer->fp); + xfer->fp = 0; + } + + if(xfer->done) { + xfer->done(ftp, xfer); + } + return; + } + + if(xfer->fp) { + ((char*)buf)[sz] = 0; + fwrite(buf, 1, sz, xfer->fp); + + } else if(xfer->mem) { + int prevsz = darr_size(xfer->mem); + darr_resize(xfer->mem, prevsz + sz); + memcpy(xfer->mem + prevsz, buf, sz); + } + + xfer->count += sz; + if(xfer->count > xfer->total) { + xfer->count = xfer->total; + } + + } else { /* FTP_STOR */ + /* TODO */ + } +} + const char *ftp_curdir(struct ftp *ftp) { return ftp->curdir; diff --git a/src/ftp.h b/src/ftp.h index d588e56..a09f0f1 100644 --- a/src/ftp.h +++ b/src/ftp.h @@ -1,9 +1,12 @@ #ifndef FTP_H_ #define FTP_H_ +#include + enum {FTP_DIR, FTP_FILE}; /* ftp_dirent type */ enum { + FTP_TYPE, FTP_PWD, FTP_CHDIR, FTP_CDUP, @@ -12,7 +15,8 @@ enum { FTP_DEL, FTP_LIST, FTP_RETR, - FTP_STORE + FTP_STORE, + FTP_XFER }; enum { @@ -28,6 +32,7 @@ enum { struct ftp_op { int op; char *arg; + void *data; struct ftp_op *next; }; @@ -63,6 +68,16 @@ struct ftp { int modified; }; +struct ftp_transfer { + int op; + char *rname; + FILE *fp; /* option: file */ + char *mem; /* option: darray */ + long total, count; + + void (*done)(struct ftp*, struct ftp_transfer*); +}; + struct ftp *ftp_alloc(void); void ftp_free(struct ftp *ftp); @@ -76,9 +91,11 @@ int ftp_sockets(struct ftp *ftp, int *sockv, int maxsize); int ftp_handle(struct ftp *ftp, int s); int ftp_queue(struct ftp *ftp, int op, const char *arg); +int ftp_queue_transfer(struct ftp *ftp, struct ftp_transfer *xfer); int ftp_waitresp(struct ftp *ftp, long timeout); int ftp_update(struct ftp *ftp); +int ftp_type(struct ftp *ftp, const char *type); int ftp_pwd(struct ftp *ftp); int ftp_chdir(struct ftp *ftp, const char *dirname); int ftp_mkdir(struct ftp *ftp, const char *dirname); @@ -87,6 +104,7 @@ int ftp_delete(struct ftp *ftp, const char *fname); int ftp_list(struct ftp *ftp); int ftp_retrieve(struct ftp *ftp, const char *fname); int ftp_store(struct ftp *ftp, const char *fname); +int ftp_transfer(struct ftp *ftp, struct ftp_transfer *xfer); const char *ftp_curdir(struct ftp *ftp); int ftp_num_dirent(struct ftp *ftp); diff --git a/src/main.c b/src/main.c index 5fdacc6..0334910 100644 --- a/src/main.c +++ b/src/main.c @@ -26,6 +26,8 @@ void updateui(void); int update_localdir(void); int proc_input(void); int keypress(int key); +static void fixname(char *dest, const char *src); +static void xfer_done(struct ftp *ftp, struct ftp_transfer *xfer); int parse_args(int argc, char **argv); static struct ftp *ftp; @@ -38,6 +40,7 @@ static int port = 21; static char curdir[PATH_MAX + 1]; static struct ftp_dirent *localdir; +static int local_modified; int main(int argc, char **argv) @@ -164,7 +167,7 @@ void updateui(void) upd |= 1; } - if(strcmp(tui_get_title(uilist[1]), curdir) != 0) { + if(local_modified || strcmp(tui_get_title(uilist[1]), curdir) != 0) { tui_clear_list(uilist[1]); num = darr_size(localdir); for(i=0; iname) + 1); + + fixname(lname, ent->name); + + xfer = malloc_nf(sizeof *xfer); + if(!(xfer->fp = fopen(lname, "wb"))) { + errmsg("failed to open %s: %s\n", lname, strerror(errno)); + free(xfer); + break; + } + + xfer->op = FTP_RETR; + xfer->rname = strdup_nf(ent->name); + xfer->total = ent->size; + xfer->done = xfer_done; + + ftp_queue_transfer(ftp, xfer); + local_modified = 1; + } else { + /* TODO */ + } + break; + default: break; } return 0; } +static void fixname(char *dest, const char *src) +{ + strcpy(dest, src); + +#ifdef __DOS__ + { + char *suffix; + if((suffix = strrchr(dest, '.'))) { + *suffix++ = 0; + if(strlen(suffix) > 3) { + suffix[3] = 0; + } + } + if(strlen(dest) > 8) { + dest[8] = 0; + } + + if(suffix) { + strcat(dest, "."); + strcat(dest, suffix); + } + } +#endif +} + +static void xfer_done(struct ftp *ftp, struct ftp_transfer *xfer) +{ + if(xfer->fp) { + fclose(xfer->fp); + } + free(xfer->rname); + free(xfer); + update_localdir(); +} + static const char *usage = "Usage: %s [options] [hostname] [port]\n" "Options:\n" " -h: print usage information and exit\n"; -- 1.7.10.4