list widget selection
[oftp] / src / ftp.c
index 6a29a77..806c766 100644 (file)
--- a/src/ftp.c
+++ b/src/ftp.c
@@ -20,7 +20,7 @@
 #define fcntlsocket            fcntl
 #endif
 
-#define TIMEOUT        5
+#define TIMEOUT        15
 
 static int newconn(struct ftp *ftp);
 static int sendcmd(struct ftp *ftp, const char *fmt, ...);
@@ -187,7 +187,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 +195,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;
 }
 
@@ -268,9 +276,11 @@ static int ftp_active(struct ftp *ftp)
                return -1;
        }
 
+       len = sizeof sa;
+       getsockname(ftp->ctl, (struct sockaddr*)&sa, &len);
+
        sa.sin_family = AF_INET;
        sa.sin_port = 0;
-       sa.sin_addr.s_addr = htonl(INADDR_ANY);
 
        if(bind(ftp->lis, (struct sockaddr*)&sa, sizeof sa) == -1) {
                errmsg("ftp_active: failed to bind listening socket\n");
@@ -281,7 +291,7 @@ static int ftp_active(struct ftp *ftp)
        listen(ftp->lis, 1);
 
        len = sizeof sa;
-       if(getsockname(ftp->ctl, (struct sockaddr*)&sa, &len) == -1) {
+       if(getsockname(ftp->lis, (struct sockaddr*)&sa, &len) == -1) {
                errmsg("ftp_active: failed to retrieve listening socket address\n");
                closesocket(ftp->lis);
                ftp->lis = -1;
@@ -290,6 +300,7 @@ static int ftp_active(struct ftp *ftp)
 
        addr = ntohl(sa.sin_addr.s_addr);
        port = ntohs(sa.sin_port);
+       infomsg("listening on %s port %d\n", inet_ntoa(sa.sin_addr), port);
 
        sendcmd(ftp, "PORT %d,%d,%d,%d,%d,%d", addr >> 24, (addr >> 16) & 0xff,
                        (addr >> 8) & 0xff, addr & 0xff, port >> 8, port & 0xff);
@@ -327,11 +338,13 @@ 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);
+       infomsg("send: %s\n", buf);
        strcat(buf, "\r\n");
-
        return send(ftp->ctl, buf, strlen(buf), 0);
 }
 
@@ -418,8 +431,14 @@ static int respcode(const char *resp)
 static void proc_control(struct ftp *ftp, const char *buf)
 {
        int code;
+       char *end;
 
        while(*buf && isspace(*buf)) buf++;
+       if((end = strchr(buf, '\r'))) {
+               *end = 0;
+       }
+
+       infomsg("recv: %s\n", buf);
 
        if((code = respcode(buf)) == 0) {
                warnmsg("ignoring invalid response: %s\n", buf);
@@ -434,6 +453,7 @@ 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);
@@ -459,6 +479,7 @@ static void proc_control(struct ftp *ftp, const char *buf)
                errmsg("login failed\n");
                break;
        }
+       ftp->busy = 0;
 }
 
 static int newconn(struct ftp *ftp)
@@ -612,15 +633,93 @@ static int cproc_list(struct ftp *ftp, int code, const char *buf, void *cls)
        return 0;
 }
 
+static void free_dirlist(struct ftp_dirent *list)
+{
+       struct ftp_dirent *tmp;
+
+       while(list) {
+               tmp = list;
+               list = list->next;
+
+               free(tmp->name);
+               free(tmp);
+       }
+}
+
+#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 void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls)
 {
        struct recvbuf *rbuf = cls;
 
        if(sz == 0) {
                /* EOF condition, we got the whole list, update directory entries */
-               /* TODO */
-               rbuf->buf[rbuf->size] = 0;
-               fprintf(stderr, "%s\n", rbuf->buf);
+               char *ptr = rbuf->buf;
+               char *end = rbuf->buf + rbuf->size;
+               struct ftp_dirent *tail = 0;
+               struct ftp_dirent *ent;
+
+               free_dirlist(ftp->dent_rem);
+               ftp->dent_rem = 0;
+
+               while(ptr < end) {
+                       ent = malloc_nf(sizeof *ent);
+                       if(parse_dirent(ent, ptr) != -1) {
+                               ent->next = 0;
+                               if(!tail) {
+                                       ftp->dent_rem = tail = ent;
+                               } else {
+                                       tail->next = ent;
+                                       tail = 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);