[syslinux:lwip] pxe, ftp: support readdir for FTP

syslinux-bot for H. Peter Anvin hpa at zytor.com
Tue May 3 23:57:39 PDT 2011


Commit-ID:  4c9b4b40238d6afda386d1d5fb088cbc9aa68c42
Gitweb:     http://syslinux.zytor.com/commit/4c9b4b40238d6afda386d1d5fb088cbc9aa68c42
Author:     H. Peter Anvin <hpa at zytor.com>
AuthorDate: Tue, 3 May 2011 23:37:10 -0700
Committer:  H. Peter Anvin <hpa at zytor.com>
CommitDate: Tue, 3 May 2011 23:37:10 -0700

pxe, ftp: support readdir for FTP

Support readdir for FTP.  This uses some heuristics for parsing LIST
output which (hopefully) works on most systems... there is no hope for
weird filenames with leading spaces or control characters, though.
That would require MLSD support, which doesn't seem to be very common.

Signed-off-by: H. Peter Anvin <hpa at zytor.com>


---
 core/fs/pxe/ftp.c         |   32 ++++++++---
 core/fs/pxe/ftp_readdir.c |  140 +++++++++++++++++++++++++++++++++++++++++++++
 core/fs/pxe/pxe.c         |    2 +-
 core/fs/pxe/pxe.h         |    3 +
 4 files changed, 168 insertions(+), 9 deletions(-)

diff --git a/core/fs/pxe/ftp.c b/core/fs/pxe/ftp.c
index 0079e32..10a9f5c 100644
--- a/core/fs/pxe/ftp.c
+++ b/core/fs/pxe/ftp.c
@@ -1,16 +1,28 @@
+/* ----------------------------------------------------------------------- *
+ *   
+ *   Copyright 2009-2011 Intel Corporation; author: H. Peter Anvin
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ *   Boston MA 02110-1301, USA; either version 2 of the License, or
+ *   (at your option) any later version; incorporated herein by reference.
+ *
+ * ----------------------------------------------------------------------- */
+
 /*
  * ftp.c
  */
-
 #include <ctype.h>
 #include <stdio.h>
 #include <string.h>
-#include <core.h>
-#include <fs.h>
+#include <fcntl.h>
 #include <minmax.h>
 #include <sys/cpu.h>
 #include <netinet/in.h>
 #include <lwip/api.h>
+#include "core.h"
+#include "fs.h"
 #include "pxe.h"
 #include "thread.h"
 #include "url.h"
@@ -169,6 +181,7 @@ static void ftp_close_file(struct inode *inode)
 static const struct pxe_conn_ops ftp_conn_ops = {
     .fill_buffer	= tcp_fill_buffer,
     .close		= ftp_close_file,
+    .readdir		= ftp_readdir,
 };
 
 void ftp_open(struct url_info *url, int flags, struct inode *inode,
@@ -183,7 +196,6 @@ void ftp_open(struct url_info *url, int flags, struct inode *inode,
     err_t err;
 
     (void)redir;		/* FTP does not redirect */
-    (void)flags;
 
     inode->size = 0;
 
@@ -229,9 +241,11 @@ void ftp_open(struct url_info *url, int flags, struct inode *inode,
 	    goto err_disconnect;
     }
 
-    resp = ftp_cmd_response(socket->ctl, "TYPE", "I", NULL, NULL);
-    if (resp != 200)
-	goto err_disconnect;
+    if (!(flags & O_DIRECTORY)) {
+	resp = ftp_cmd_response(socket->ctl, "TYPE", "I", NULL, NULL);
+	if (resp != 200)
+	    goto err_disconnect;
+    }
 
     resp = ftp_cmd_response(socket->ctl, "PASV", NULL, pasv_data, &pasv_bytes);
     //printf("%u PASV %u bytes %u,%u,%u,%u,%u,%u\n",
@@ -248,7 +262,9 @@ void ftp_open(struct url_info *url, int flags, struct inode *inode,
     if (err)
 	goto err_disconnect;
 
-    resp = ftp_cmd_response(socket->ctl, "RETR", url->path, NULL, NULL);
+    resp = ftp_cmd_response(socket->ctl,
+			    (flags & O_DIRECTORY) ? "LIST" : "RETR",
+			    url->path, NULL, NULL);
     if (resp != 125 && resp != 150)
 	goto err_disconnect;
 
diff --git a/core/fs/pxe/ftp_readdir.c b/core/fs/pxe/ftp_readdir.c
new file mode 100644
index 0000000..2eb7748
--- /dev/null
+++ b/core/fs/pxe/ftp_readdir.c
@@ -0,0 +1,140 @@
+/* ----------------------------------------------------------------------- *
+ *
+ *   Copyright 2011 Intel Corporation; author: H. Peter Anvin
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ *   Boston MA 02110-1301, USA; either version 2 of the License, or
+ *   (at your option) any later version; incorporated herein by reference.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * ftp_readdir.c
+ */
+#include <inttypes.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <dprintf.h>
+#include "pxe.h"
+
+static int dirtype(char type)
+{
+    switch (type) {
+    case 'f':
+	return DT_FIFO;
+    case 'c':
+	return DT_CHR;
+    case 'd':
+	return DT_DIR;
+    case 'b':
+	return DT_BLK;
+    case '-':
+	return DT_REG;
+    case 'l':
+	return DT_LNK;
+    case 's':
+	return DT_SOCK;
+    default:
+	return DT_UNKNOWN;
+    }
+}
+
+int ftp_readdir(struct inode *inode, struct dirent *dirent)
+{
+    char bufs[2][FILENAME_MAX + 1];
+    int nbuf = 0;
+    char *buf = bufs[nbuf];
+    char *p = buf;
+    char *name = NULL;
+    char type;
+    int c;
+    int dt;
+    bool was_cr = false;
+    bool first = true;
+
+    for (;;) {
+	type = 0;
+
+	for (;;) {
+	    c = pxe_getc(inode);
+	    if (c == -1)
+		return -1;	/* Nothing else there */
+
+	    if (c == '\r') {
+		was_cr = true;
+		continue;
+	    }
+	    if (was_cr) {
+		if (c == '\n') {
+		    if (!name) {
+			*p = '\0';
+			name = buf;
+		    }
+		    break;	/* End of line */
+		}
+		else if (c == '\0')
+		    c = '\r';
+	    }
+	    was_cr = false;
+
+	    if (c == ' ' || c == '\t') {
+		if (!name) {
+		    *p = '\0';
+		    if (first) {
+			if (p == buf) {
+			    /* Name started with whitespace - skip line */
+			    name = buf;
+			} else if ((p = strchr(buf, ';'))) {
+			    /* VMS/Multinet format */
+			    if (p > buf+4 && !memcmp(p-4, ".DIR", 4)) {
+				type = 'd';
+				p -= 4;
+			    } else {
+				type = 'f';
+			    }
+			    *p = '\0';
+			    name = buf;
+			} else {
+			    type = buf[0];
+			}
+			first = false;
+		    } else {
+			/* Not the first word */
+			if ((type >= '0' && type <= '9') &&
+			    !strcmp(buf, "<DIR>")) {
+			    /* Some DOS FTP servers */
+			    type = 'd';
+			} else if (type == 'l' && !strcmp(buf, "->")) {
+			    /* The name was the previous word */
+			    name = bufs[nbuf ^ 1];
+			}
+		    }
+		    nbuf ^= 1;
+		    p = buf = bufs[nbuf];
+		}
+	    } else {
+		if (!name && p < buf + FILENAME_MAX)
+		    *p++ = c;
+	    }
+	}
+
+	dt = dirtype(type);
+	if (dt != DT_UNKNOWN) {
+	    size_t len = strlen(name);
+
+	    if (len <= NAME_MAX) {
+		dirent->d_type = dt;
+		dirent->d_ino = 0;	/* Not applicable */
+		dirent->d_off = 0;	/* Not applicable */
+		dirent->d_reclen = offsetof(struct dirent, d_name) + len+1;
+		memcpy(dirent->d_name, name, len+1);
+		return 0;
+	    }
+	}
+
+	/* Otherwise try the next line... */
+    }
+}
diff --git a/core/fs/pxe/pxe.c b/core/fs/pxe/pxe.c
index acefac9..267b32f 100644
--- a/core/fs/pxe/pxe.c
+++ b/core/fs/pxe/pxe.c
@@ -35,7 +35,7 @@ static struct url_scheme {
 } url_schemes[] = {
     { "tftp", tftp_open, 0 },
     { "http", http_open, O_DIRECTORY },
-    { "ftp",  ftp_open,  0 },
+    { "ftp",  ftp_open,  O_DIRECTORY },
     { NULL, NULL, 0 },
 };
 #define OK_FLAGS_MASK	(O_DIRECTORY|O_WRONLY)
diff --git a/core/fs/pxe/pxe.h b/core/fs/pxe/pxe.h
index 4c7e8e6..47c233f 100644
--- a/core/fs/pxe/pxe.h
+++ b/core/fs/pxe/pxe.h
@@ -230,6 +230,9 @@ int http_readdir(struct inode *inode, struct dirent *dirent);
 void ftp_open(struct url_info *url, int flags, struct inode *inode,
 	      const char **redir);
 
+/* ftp_readdir.c */
+int ftp_readdir(struct inode *inode, struct dirent *dirent);
+
 /* tcp.c */
 void tcp_close_file(struct inode *inode);
 void tcp_fill_buffer(struct inode *inode);



More information about the Syslinux-commits mailing list