[PATCH] fs: Add struct dirent::d_type support

Sebastian Huber sebastian.huber at embedded-brains.de
Thu Jan 31 13:01:49 UTC 2019


---
 cpukit/libfs/src/dosfs/msdos_dir.c        | 11 ++++
 cpukit/libfs/src/imfs/imfs_dir_default.c  |  3 ++
 cpukit/libfs/src/jffs2/include/linux/fs.h |  5 ++
 cpukit/libfs/src/jffs2/src/fs-rtems.c     | 11 ++--
 cpukit/libfs/src/nfsclient/src/nfs.c      |  3 ++
 testsuites/fstests/fsscandir01/init.c     | 89 +++++++++++++++++++++----------
 6 files changed, 91 insertions(+), 31 deletions(-)

diff --git a/cpukit/libfs/src/dosfs/msdos_dir.c b/cpukit/libfs/src/dosfs/msdos_dir.c
index a13caafa7e..fee6491c7d 100644
--- a/cpukit/libfs/src/dosfs/msdos_dir.c
+++ b/cpukit/libfs/src/dosfs/msdos_dir.c
@@ -236,6 +236,17 @@ msdos_dir_read(rtems_libio_t *iop, void *buffer, size_t count)
                     continue;
                 }
 
+#ifdef DT_DIR
+                if ((*MSDOS_DIR_ATTR(entry)) & MSDOS_ATTR_DIRECTORY)
+                {
+                    tmp_dirent.d_type = DT_DIR;
+                }
+                else
+                {
+                    tmp_dirent.d_type = DT_REG;
+                }
+#endif
+
                 /*
                  * Move the entry to the return buffer
                  *
diff --git a/cpukit/libfs/src/imfs/imfs_dir_default.c b/cpukit/libfs/src/imfs/imfs_dir_default.c
index 03ef115301..7bb4f378e1 100644
--- a/cpukit/libfs/src/imfs/imfs_dir_default.c
+++ b/cpukit/libfs/src/imfs/imfs_dir_default.c
@@ -74,6 +74,9 @@ static ssize_t IMFS_dir_read(
          dir_ent->d_off = current_entry;
          dir_ent->d_reclen = sizeof( *dir_ent );
          dir_ent->d_ino = IMFS_node_to_ino( imfs_node );
+#ifdef DT_DIR
+         dir_ent->d_type = IFTODT( imfs_node->st_mode );
+#endif
          dir_ent->d_namlen =
            MIN( imfs_node->namelen, sizeof( dir_ent->d_name ) - 1 );
          dir_ent->d_name[ dir_ent->d_namlen ] = '\0';
diff --git a/cpukit/libfs/src/jffs2/include/linux/fs.h b/cpukit/libfs/src/jffs2/include/linux/fs.h
index 8da9880b13..a638e7b6bf 100644
--- a/cpukit/libfs/src/jffs2/include/linux/fs.h
+++ b/cpukit/libfs/src/jffs2/include/linux/fs.h
@@ -3,12 +3,17 @@
 
 #include <linux/stat.h>
 #include <sys/types.h>
+#include <sys/dirent.h>
 #include <sys/time.h>
 
+#ifdef DT_DIR
+#define RTEMS_JFFS2_HAVE_D_TYPE
+#else
 #define DT_UNKNOWN	0
 #define DT_DIR		4
 #define DT_REG		8
 #define DT_LNK		10
+#endif
 
 #define ATTR_MODE	(1U << 0)
 #define ATTR_UID	(1U << 1)
diff --git a/cpukit/libfs/src/jffs2/src/fs-rtems.c b/cpukit/libfs/src/jffs2/src/fs-rtems.c
index 17a4985607..aae208ccef 100644
--- a/cpukit/libfs/src/jffs2/src/fs-rtems.c
+++ b/cpukit/libfs/src/jffs2/src/fs-rtems.c
@@ -423,7 +423,7 @@ static int rtems_jffs2_fstat(
 	return 0;
 }
 
-static int rtems_jffs2_fill_dirent(struct dirent *de, off_t off, uint32_t ino, const char *name)
+static int rtems_jffs2_fill_dirent(struct dirent *de, off_t off, uint32_t ino, const char *name, unsigned char type)
 {
 	int eno = 0;
 	size_t len;
@@ -433,6 +433,9 @@ static int rtems_jffs2_fill_dirent(struct dirent *de, off_t off, uint32_t ino, c
 	de->d_off = off * sizeof(*de);
 	de->d_reclen = sizeof(*de);
 	de->d_ino = ino;
+#ifdef RTEMS_JFFS2_HAVE_D_TYPE
+	de->d_type = type;
+#endif
 
 	len = strlen(name);
 	de->d_namlen = len;
@@ -466,14 +469,14 @@ static ssize_t rtems_jffs2_dir_read(rtems_libio_t *iop, void *buf, size_t len)
 	off = begin;
 
 	if (off == 0 && off < end) {
-		eno = rtems_jffs2_fill_dirent(de, off, inode->i_ino, ".");
+		eno = rtems_jffs2_fill_dirent(de, off, inode->i_ino, ".", DT_DIR);
 		assert(eno == 0);
 		++off;
 		++de;
 	}
 
 	if (off == 1 && off < end) {
-		eno = rtems_jffs2_fill_dirent(de, off, inode->i_parent->i_ino, "..");
+		eno = rtems_jffs2_fill_dirent(de, off, inode->i_parent->i_ino, "..", DT_DIR);
 		assert(eno == 0);
 		++off;
 		++de;
@@ -482,7 +485,7 @@ static ssize_t rtems_jffs2_dir_read(rtems_libio_t *iop, void *buf, size_t len)
 	while (eno == 0 && off < end && fd != NULL) {
 		if (fd->ino != 0) {
 			if (off == fd_off) {
-				eno = rtems_jffs2_fill_dirent(de, off, fd->ino, fd->name);
+				eno = rtems_jffs2_fill_dirent(de, off, fd->ino, fd->name, fd->type);
 				++off;
 				++de;
 			}
diff --git a/cpukit/libfs/src/nfsclient/src/nfs.c b/cpukit/libfs/src/nfsclient/src/nfs.c
index ddb4dda313..bb338d58ca 100644
--- a/cpukit/libfs/src/nfsclient/src/nfs.c
+++ b/cpukit/libfs/src/nfsclient/src/nfs.c
@@ -327,6 +327,9 @@ nfscookie		*pcookie;
 		pde->d_ino    = fileid;
 		pde->d_namlen = nlen;
 		pde->d_off	  = di->ptr - di->buf;
+#ifdef DT_UNKNOWN
+		pde->d_type = DT_UNKNOWN;
+#endif
 		if (name == dummy.nambuf) {
 			memcpy(pde->d_name, dummy.nambuf, nlen + 1);
 		}
diff --git a/testsuites/fstests/fsscandir01/init.c b/testsuites/fstests/fsscandir01/init.c
index e92d2feceb..d7fb43c26b 100644
--- a/testsuites/fstests/fsscandir01/init.c
+++ b/testsuites/fstests/fsscandir01/init.c
@@ -8,47 +8,82 @@
  */
 
 #ifdef HAVE_CONFIG_H
-  #include "config.h"
+#include "config.h"
 #endif
 
-#include "fstest.h"
-#include "fs_config.h"
-#include "fstest_support.h"
-#include "pmacros.h"
-
+#include <sys/stat.h>
 #include <dirent.h>
+#include <fcntl.h>
+#include <limits.h>
 #include <stdio.h>
-#include <string.h>
 #include <unistd.h>
-#include <errno.h>
-#include <limits.h>
+
+#include <tmacros.h>
+
+#include "fstest.h"
+#include "fs_config.h"
 
 const char rtems_test_name[] = "FSSCANDIR " FILESYSTEM;
 
-/*
- * This code is from the scandir() man page.
- */
-static void test_scandir(void)
+#define FILE_NAME "aaa"
+
+#define DIR_NAME "bbb"
+
+void test(void)
 {
   struct dirent **namelist;
+  struct dirent *d;
+  int rv;
   int n;
+  int i;
+
+  rtems_test_assert(MAXNAMLEN == NAME_MAX);
+
+  rv = mknod(FILE_NAME, S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO, 0);
+  rtems_test_assert(rv == 0);
+
+  rv = mkdir(DIR_NAME, S_IRWXU | S_IRWXG | S_IRWXO );
+  rtems_test_assert(rv == 0);
+
+  n = scandir(".", &namelist, NULL, alphasort);
+  rtems_test_assert(2 <= n || n == 4);
 
-  n = scandir(".", &namelist, 0, NULL);
-  if (n < 0) {
-    perror("scandir");
-  } else {
-    while(n--) {
-      printf("%s\n", namelist[n]->d_name);
-      free(namelist[n]);
-    }
-    free(namelist);
+  i = 0;
+  d = namelist[i];
+
+  if (n >= 3) {
+    rtems_test_assert(strcmp(d->d_name, ".") == 0);
+#ifdef DT_UNKNOWN
+    rtems_test_assert(d->d_type == DT_DIR || d->d_type == DT_UNKNOWN);
+#endif
+    free(d);
+    ++i;
+    d = namelist[i];
   }
 
-  rtems_test_assert(MAXNAMLEN == NAME_MAX);
-}
+  if (n == 4) {
+    rtems_test_assert(strcmp(d->d_name, "..") == 0);
+#ifdef DT_UNKNOWN
+    rtems_test_assert(d->d_type == DT_DIR || d->d_type == DT_UNKNOWN);
+#endif
+    free(d);
+    ++i;
+    d = namelist[i];
+  }
 
+  rtems_test_assert(strcmp(d->d_name, FILE_NAME) == 0);
+#ifdef DT_UNKNOWN
+  rtems_test_assert(d->d_type == DT_REG || d->d_type == DT_UNKNOWN);
+#endif
+  free(d);
+  ++i;
+  d = namelist[i];
 
-void test (void)
-{
-  test_scandir();
+  rtems_test_assert(strcmp(d->d_name, DIR_NAME) == 0);
+#ifdef DT_UNKNOWN
+  rtems_test_assert(d->d_type == DT_DIR || d->d_type == DT_UNKNOWN);
+#endif
+  free(d);
+
+  free(namelist);
 }
-- 
2.16.4



More information about the devel mailing list