navigation
[oftp] / src / ftp.c
index 04a8ae4..7cdf8ff 100644 (file)
--- a/src/ftp.c
+++ b/src/ftp.c
@@ -20,7 +20,9 @@
 #define fcntlsocket            fcntl
 #endif
 
-#define TIMEOUT        5
+#define TIMEOUT        15
+
+static void free_dir(struct ftp_dirent *dir);
 
 static int newconn(struct ftp *ftp);
 static int sendcmd(struct ftp *ftp, const char *fmt, ...);
@@ -44,6 +46,9 @@ struct ftp *ftp_alloc(void)
                return 0;
        }
        ftp->ctl = ftp->data = ftp->lis = -1;
+
+       ftp->dirent[0] = darr_alloc(0, sizeof *ftp->dirent[0]);
+       ftp->dirent[1] = darr_alloc(0, sizeof *ftp->dirent[1]);
        return ftp;
 }
 
@@ -54,6 +59,18 @@ void ftp_free(struct ftp *ftp)
        if(ftp->ctl >= 0) {
                ftp_close(ftp);
        }
+
+       free_dir(ftp->dirent[0]);
+       free_dir(ftp->dirent[1]);
+}
+
+static void free_dir(struct ftp_dirent *dir)
+{
+       int i;
+       for(i=0; i<darr_size(dir); i++) {
+               free(dir[i].name);
+       }
+       darr_free(dir);
 }
 
 int ftp_connect(struct ftp *ftp, const char *hostname, int port)
@@ -133,6 +150,10 @@ static void exec_op(struct ftp *ftp, int op, const char *arg)
                ftp_chdir(ftp, arg);
                break;
 
+       case FTP_CDUP:
+               ftp_chdir(ftp, "..");
+               break;
+
        case FTP_MKDIR:
                ftp_mkdir(ftp, arg);
                break;
@@ -187,7 +208,7 @@ int ftp_queue(struct ftp *ftp, int op, const char *arg)
 {
        struct ftp_op *fop;
 
-       if(!ftp->qhead) {
+       if(!ftp->busy && !ftp->qhead) {
                exec_op(ftp, op, arg);
                return 0;
        }
@@ -195,15 +216,23 @@ int ftp_queue(struct ftp *ftp, int op, const char *arg)
        if(!(fop = malloc(sizeof *fop))) {
                return -1;
        }
-       if(!(fop->arg = strdup(arg))) {
-               free(fop);
-               return -1;
+       if(arg) {
+               if(!(fop->arg = strdup(arg))) {
+                       free(fop);
+                       return -1;
+               }
+       } else {
+               fop->arg = 0;
        }
        fop->op = op;
        fop->next = 0;
 
-       ftp->qtail->next = fop;
-       ftp->qtail = fop;
+       if(ftp->qhead) {
+               ftp->qtail->next = fop;
+               ftp->qtail = fop;
+       } else {
+               ftp->qhead = ftp->qtail = fop;
+       }
        return 0;
 }
 
@@ -330,6 +359,8 @@ static int sendcmd(struct ftp *ftp, const char *fmt, ...)
                return -1;
        }
 
+       ftp->busy = 1;
+
        va_start(ap, fmt);
        vsprintf(buf, fmt, ap);
        va_end(ap);
@@ -343,7 +374,13 @@ static int handle_control(struct ftp *ftp)
        int i, sz, rd;
        char *buf, *start, *end;
 
-       while((sz = sizeof ftp->crecv - ftp->num_crecv) > 0) {
+       for(;;) {
+               if((sz = sizeof ftp->crecv - ftp->num_crecv) <= 0) {
+                       /* discard buffer */
+                       warnmsg("discard buffer\n");
+                       sz = sizeof ftp->crecv;
+                       ftp->num_crecv = 0;
+               }
                start = ftp->crecv + ftp->num_crecv;
                if((rd = recv(ftp->ctl, start, sz, 0)) == -1) {
                        if(errno == EINTR) continue;
@@ -358,7 +395,9 @@ static int handle_control(struct ftp *ftp)
                end = start + rd;
                buf = ftp->crecv;
                for(i=0; i<rd; i++) {
-                       if(start[i] == '\n') {
+                       if(start[i] == '\r') {
+                               start[i] = 0;
+                       } else if(start[i] == '\n') {
                                start[i] = 0;
                                proc_control(ftp, buf);
                                buf = start + i + 1;
@@ -367,6 +406,8 @@ static int handle_control(struct ftp *ftp)
                if(buf != ftp->crecv && buf < end) {
                        ftp->num_crecv = end - buf;
                        memmove(ftp->crecv, buf, ftp->num_crecv);
+               } else {
+                       ftp->num_crecv = 0;
                }
        }
        return 0;
@@ -443,12 +484,14 @@ static void proc_control(struct ftp *ftp, const char *buf)
        if(ftp->cproc) {
                if(ftp->cproc(ftp, code, buf, ftp->cproc_cls) <= 0) {
                        ftp->cproc = 0;
+                       ftp->busy = 0;
 
                        /* execute next operation if there's one queued */
                        exec_queued(ftp);
                }
                return;
        }
+       ftp->busy = 0;
 
        switch(code) {
        case 220:
@@ -494,7 +537,11 @@ int ftp_pwd(struct ftp *ftp)
 
 int ftp_chdir(struct ftp *ftp, const char *dirname)
 {
-       sendcmd(ftp, "cwd %s", dirname);
+       if(strcmp(dirname, "..") == 0) {
+               sendcmd(ftp, "cdup");
+       } else {
+               sendcmd(ftp, "cwd %s", dirname);
+       }
        ftp->cproc = cproc_cwd;
        return 0;
 }
@@ -597,15 +644,22 @@ static int cproc_pwd(struct ftp *ftp, int code, const char *buf, void *cls)
                return -1;
        }
 
-       free(ftp->curdir_rem);
-       ftp->curdir_rem = strdup_nf(dirname);
-       ftp->modified = 1;
+       free(ftp->curdir[FTP_REMOTE]);
+       ftp->curdir[FTP_REMOTE] = strdup_nf(dirname);
+       ftp->modified = FTP_MOD_REMDIR;
        return 0;
 }
 
 static int cproc_cwd(struct ftp *ftp, int code, const char *buf, void *cls)
 {
-       return -1;
+       if(code != 250) {
+               warnmsg("cwd failed\n");
+               return -1;
+       }
+
+       ftp_queue(ftp, FTP_PWD, 0);
+       ftp_queue(ftp, FTP_LIST, 0);
+       return 0;
 }
 
 static int cproc_list(struct ftp *ftp, int code, const char *buf, void *cls)
@@ -621,19 +675,89 @@ static int cproc_list(struct ftp *ftp, int code, const char *buf, void *cls)
        return 0;
 }
 
+#define SKIP_FIELD(p) \
+       do { \
+               while(*(p) && *(p) != '\n' && !isspace(*(p))) (p)++; \
+               while(*(p) && *(p) != '\n' && isspace(*(p))) (p)++; \
+       } while(0)
+
+static int parse_dirent(struct ftp_dirent *ent, const char *line)
+{
+       int len;
+       const char *ptr = line;
+       const char *end;
+
+       if(!(end = strchr(line, '\r')) && !(end = strchr(line, '\n'))) {
+               return -1;
+       }
+
+       if(line[0] == 'd') {
+               ent->type = FTP_DIR;
+       } else {
+               ent->type = FTP_FILE;
+       }
+
+       SKIP_FIELD(ptr);                /* skip mode */
+       SKIP_FIELD(ptr);                /* skip links */
+       SKIP_FIELD(ptr);                /* skip owner */
+       SKIP_FIELD(ptr);                /* skip group */
+
+       if(ent->type == FTP_FILE) {
+               ent->size = atoi(ptr);
+       }
+       SKIP_FIELD(ptr);                /* skip size */
+       SKIP_FIELD(ptr);                /* skip month */
+       SKIP_FIELD(ptr);                /* skip day */
+       SKIP_FIELD(ptr);                /* skip year */
+
+       if(ptr >= end) return -1;
+
+       len = end - ptr;
+       ent->name = malloc(len + 1);
+       memcpy(ent->name, ptr, len);
+       ent->name[len] = 0;
+
+       return 0;
+}
+
+static int direntcmp(const void *a, const void *b)
+{
+       const struct ftp_dirent *da = a, *db = b;
+
+       if(da->type == db->type) {
+               return strcmp(da->name, db->name);
+       }
+       return da->type == FTP_DIR ? -1 : 1;
+}
+
 static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls)
 {
+       int num;
        struct recvbuf *rbuf = cls;
 
        if(sz == 0) {
                /* EOF condition, we got the whole list, update directory entries */
-               /* TODO */
-               rbuf->buf[rbuf->size] = 0;
-               infomsg("%s\n", rbuf->buf);
+               char *ptr = rbuf->buf;
+               char *end = rbuf->buf + rbuf->size;
+               struct ftp_dirent ent;
+
+               darr_clear(ftp->dirent[FTP_REMOTE]);
+
+               while(ptr < end) {
+                       if(parse_dirent(&ent, ptr) != -1) {
+                               darr_push(ftp->dirent[FTP_REMOTE], &ent);
+                       }
+                       while(ptr < end && *ptr != '\n' && *ptr != '\r') ptr++;
+                       while(ptr < end && (*ptr == '\r' || *ptr == '\n')) ptr++;
+               }
+               ftp->modified |= FTP_MOD_REMDIR;
 
                free(rbuf->buf);
                free(rbuf);
                ftp->dproc = 0;
+
+               num = darr_size(ftp->dirent[FTP_REMOTE]);
+               qsort(ftp->dirent[FTP_REMOTE], num, sizeof *ftp->dirent[FTP_REMOTE], direntcmp);
                return;
        }
 
@@ -652,3 +776,18 @@ static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls)
        memcpy(rbuf->buf + rbuf->size, buf, sz);
        rbuf->size += sz;
 }
+
+const char *ftp_curdir(struct ftp *ftp, int whichdir)
+{
+       return ftp->curdir[whichdir];
+}
+
+int ftp_num_dirent(struct ftp *ftp, int whichdir)
+{
+       return darr_size(ftp->dirent[whichdir]);
+}
+
+struct ftp_dirent *ftp_dirent(struct ftp *ftp, int whichdir, int idx)
+{
+       return ftp->dirent[whichdir] + idx;
+}