Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 9 Jan 2010 22:33:34 +0000 (UTC)
From:      Marcel Moolenaar <marcel@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r201937 - head/lib/libstand
Message-ID:  <201001092233.o09MXY5K035076@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: marcel
Date: Sat Jan  9 22:33:34 2010
New Revision: 201937
URL: http://svn.freebsd.org/changeset/base/201937

Log:
  Implement the fo_readdir method. This does not support long
  file names.
  
  Obtained from:	Juniper Networks, Inc.
  MFC after:	1 week

Modified:
  head/lib/libstand/dosfs.c

Modified: head/lib/libstand/dosfs.c
==============================================================================
--- head/lib/libstand/dosfs.c	Sat Jan  9 22:31:11 2010	(r201936)
+++ head/lib/libstand/dosfs.c	Sat Jan  9 22:33:34 2010	(r201937)
@@ -47,6 +47,7 @@ static int	dos_close(struct open_file *f
 static int	dos_read(struct open_file *fd, void *buf, size_t size, size_t *resid);
 static off_t	dos_seek(struct open_file *fd, off_t offset, int whence);
 static int	dos_stat(struct open_file *fd, struct stat *sb);
+static int	dos_readdir(struct open_file *fd, struct dirent *d);
 
 struct fs_ops dosfs_fsops = {
 	"dosfs",
@@ -56,7 +57,7 @@ struct fs_ops dosfs_fsops = {
 	null_write,
 	dos_seek,
 	dos_stat,
-	null_readdir
+	dos_readdir
 };
 
 #define SECSIZ  512             /* sector size */
@@ -354,6 +355,72 @@ dos_stat(struct open_file *fd, struct st
     return (0);
 }
 
+static int
+dos_readdir(struct open_file *fd, struct dirent *d)
+{
+    DOS_FILE *f = (DOS_FILE *)fd->f_fsdata;
+    u_char fn[261];
+    DOS_DIR dd;
+    size_t res;
+    u_int chk, i, x, xdn;
+    int err;
+
+    x = chk = 0;
+    while (1) {
+	xdn = x;
+	x = 0;
+	err = dos_read(fd, &dd, sizeof(dd), &res);
+	if (err)
+	    return (err);
+	if (res == sizeof(dd))
+	    return (ENOENT);
+	if (dd.de.name[0] == 0)
+	    return (ENOENT);
+
+	/* Skip deleted entries */
+	if (dd.de.name[0] == 0xe5)
+	    continue;
+
+	/* Skip volume labels */
+	if (dd.de.attr & FA_LABEL)
+	    continue;
+
+	if ((dd.de.attr & FA_MASK) == FA_XDE) {
+	    if (dd.xde.seq & 0x40)
+		chk = dd.xde.chk;
+	    else if (dd.xde.seq != xdn - 1 || dd.xde.chk != chk)
+		continue;
+	    x = dd.xde.seq & ~0x40;
+	    if (x < 1 || x > 20) {
+		x = 0;
+		continue;
+	    }
+	    cp_xdnm(fn, &dd.xde);
+	} else {
+	    if (xdn == 1) {
+		x = 0;
+		for (i = 0; i < 11; i++) {
+		    x = ((x & 1) << 7) | (x >> 1);
+		    x += dd.de.name[i];
+		    x &= 0xff;
+		}
+		if (x == chk)
+		    break;
+	    } else {
+		cp_sfn(fn, &dd.de);
+		break;
+	    }
+	    x = 0;
+	}
+    }
+
+    d->d_fileno = dd.de.clus[1] << 8 + dd.de.clus[0];
+    d->d_reclen = sizeof(*d);
+    d->d_type = (dd.de.attr & FA_DIR) ? DT_DIR : DT_REG;
+    memcpy(d->d_name, fn, sizeof(d->d_name));
+    return(0);
+}
+
 /*
  * Parse DOS boot sector
  */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201001092233.o09MXY5K035076>