From 67ef70d460db4695b950208d861ff47d4a40bdb3 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Mon, 6 Jul 2020 00:34:32 +0800 Subject: [PATCH] vfs/dirread: Should return the same file type as lstat by extend the possible value of d_type for the special file Signed-off-by: Xiang Xiao --- arch/sim/src/sim/up_hostfs.c | 13 +++++++- fs/cromfs/fs_cromfs.c | 31 ++++++++++++++++++-- fs/dirent/fs_readdir.c | 32 +++++++++++++++----- fs/romfs/fs_romfs.c | 5 ++++ include/dirent.h | 57 +++++++++++++++++++++++------------- include/nuttx/fs/hostfs.h | 17 +++++++---- 6 files changed, 118 insertions(+), 37 deletions(-) diff --git a/arch/sim/src/sim/up_hostfs.c b/arch/sim/src/sim/up_hostfs.c index c3926a49f7..f6f55fd732 100644 --- a/arch/sim/src/sim/up_hostfs.c +++ b/arch/sim/src/sim/up_hostfs.c @@ -389,11 +389,14 @@ int host_readdir(void *dirp, struct nuttx_dirent_s *entry) /* Map the type */ - entry->d_type = 0; if (ent->d_type == DT_REG) { entry->d_type = NUTTX_DTYPE_FILE; } + else if (ent->d_type == DT_FIFO) + { + entry->d_type = NUTTX_DTYPE_FIFO; + } else if (ent->d_type == DT_CHR) { entry->d_type = NUTTX_DTYPE_CHR; @@ -410,6 +413,14 @@ int host_readdir(void *dirp, struct nuttx_dirent_s *entry) { entry->d_type = NUTTX_DTYPE_LINK; } + else if (ent->d_type == DT_SOCK) + { + entry->d_type = NUTTX_DTYPE_SOCK; + } + else + { + entry->d_type = NUTTX_DTYPE_UNKNOWN; + } return 0; } diff --git a/fs/cromfs/fs_cromfs.c b/fs/cromfs/fs_cromfs.c index 39bef9cc77..140ae87df7 100644 --- a/fs/cromfs/fs_cromfs.c +++ b/fs/cromfs/fs_cromfs.c @@ -1069,14 +1069,41 @@ static int cromfs_readdir(struct inode *mountpt, struct fs_dirent_s *dir) break; case S_IFIFO: /* FIFO */ + dir->fd_dir.d_type = DTYPE_FIFO; + break; + case S_IFCHR: /* Character driver */ + dir->fd_dir.d_type = DTYPE_CHR; + break; + case S_IFBLK: /* Block driver */ -#if 0 + dir->fd_dir.d_type = DTYPE_BLK; + break; + case S_IFSOCK: /* Socket */ -#endif + dir->fd_dir.d_type = DTYPE_SOCK; + break; + case S_IFMQ: /* Message queue */ + dir->fd_dir.d_type = DTYPE_MQ; + break; + case S_IFSEM: /* Semaphore */ + dir->fd_dir.d_type = DTYPE_SEM; + break; + case S_IFSHM: /* Shared memory */ + dir->fd_dir.d_type = DTYPE_SHM; + break; + + case S_IFMTD: /* MTD driver */ + dir->fd_dir.d_type = DTYPE_MTD; + break; + + case S_IFSOCK: /* Socket */ + dir->fd_dir.d_type = DTYPE_SOCK; + break; + default: DEBUGPANIC(); dir->fd_dir.d_type = DTYPE_UNKNOWN; diff --git a/fs/dirent/fs_readdir.c b/fs/dirent/fs_readdir.c index bb2620fbbb..2e3f025503 100644 --- a/fs/dirent/fs_readdir.c +++ b/fs/dirent/fs_readdir.c @@ -65,30 +65,46 @@ static inline int readpseudodir(struct fs_dirent_s *idir) /* If the node has file operations, we will say that it is a file. */ - idir->fd_dir.d_type = 0; + idir->fd_dir.d_type = DTYPE_UNKNOWN; if (idir->u.pseudo.fd_next->u.i_ops) { #ifndef CONFIG_DISABLE_MOUNTPOINT - if (INODE_IS_BLOCK(idir->u.pseudo.fd_next) || - INODE_IS_MTD(idir->u.pseudo.fd_next)) + if (INODE_IS_BLOCK(idir->u.pseudo.fd_next)) { - idir->fd_dir.d_type |= DTYPE_BLK; + idir->fd_dir.d_type = DTYPE_BLK; + } + else if (INODE_IS_MTD(idir->u.pseudo.fd_next)) + { + idir->fd_dir.d_type = DTYPE_MTD; } else if (INODE_IS_MOUNTPT(idir->u.pseudo.fd_next)) { - idir->fd_dir.d_type |= DTYPE_DIRECTORY; + idir->fd_dir.d_type = DTYPE_DIRECTORY; } else #endif #ifdef CONFIG_PSEUDOFS_SOFTLINKS if (INODE_IS_SOFTLINK(idir->u.pseudo.fd_next)) { - idir->fd_dir.d_type |= DTYPE_LINK; + idir->fd_dir.d_type = DTYPE_LINK; } else #endif + if (INODE_IS_DRIVER(idir->u.pseudo.fd_next)) { - idir->fd_dir.d_type |= DTYPE_CHR; + idir->fd_dir.d_type = DTYPE_CHR; + } + else if (INODE_IS_NAMEDSEM(idir->u.pseudo.fd_next)) + { + idir->fd_dir.d_type = DTYPE_SEM; + } + else if (INODE_IS_MQUEUE(idir->u.pseudo.fd_next)) + { + idir->fd_dir.d_type = DTYPE_MQ; + } + else if (INODE_IS_SHM(idir->u.pseudo.fd_next)) + { + idir->fd_dir.d_type = DTYPE_SHM; } } @@ -99,7 +115,7 @@ static inline int readpseudodir(struct fs_dirent_s *idir) if (idir->u.pseudo.fd_next->i_child || !idir->u.pseudo.fd_next->u.i_ops) { - idir->fd_dir.d_type |= DTYPE_DIRECTORY; + idir->fd_dir.d_type = DTYPE_DIRECTORY; } /* Now get the inode to visit next time that readdir() is called */ diff --git a/fs/romfs/fs_romfs.c b/fs/romfs/fs_romfs.c index 2c54930562..a2c49a5b9a 100644 --- a/fs/romfs/fs_romfs.c +++ b/fs/romfs/fs_romfs.c @@ -943,6 +943,11 @@ static int romfs_readdir(FAR struct inode *mountpt, dir->fd_dir.d_type = DTYPE_FILE; break; } + else if (IS_SOFTLINK(next)) + { + dir->fd_dir.d_type = DTYPE_LINK; + break; + } } errout_with_semaphore: diff --git a/include/dirent.h b/include/dirent.h index 5f5f9987c1..28d006a52d 100644 --- a/include/dirent.h +++ b/include/dirent.h @@ -55,20 +55,31 @@ * top-level, pseudo-file system, an inode can be BOTH a file and a directory */ -#define DTYPE_UNKNOWN 0 /* The file type could not be determined */ -#define DTYPE_FILE (1 << 0) /* Bit 0: Regular file */ -#define DTYPE_CHR (1 << 1) /* Bit 1: Character device */ -#define DTYPE_BLK (1 << 2) /* Bit 2: Block device */ -#define DTYPE_DIRECTORY (1 << 3) /* Bit 3: Directory */ -#define DTYPE_LINK (1 << 4) /* Bit 4: Symbolic link */ -#define DTYPE_FIFO (1 << 5) /* Bit 5: Named Pipe (FIFO) */ -#define DTYPE_SOCK (1 << 6) /* Bit 6: UNIX domain socket */ +#define DTYPE_UNKNOWN 0 +#define DTYPE_FIFO 1 +#define DTYPE_CHR 2 +#define DTYPE_SEM 3 +#define DTYPE_DIRECTORY 4 +#define DTYPE_MQ 5 +#define DTYPE_BLK 6 +#define DTYPE_SHM 7 +#define DTYPE_FILE 8 +#define DTYPE_MTD 9 +#define DTYPE_LINK 10 +#define DTYPE_SOCK 12 -#define DIRENT_ISFILE(dtype) (((dtype) & DTYPE_FILE) != 0) -#define DIRENT_ISCHR(dtype) (((dtype) & DTYPE_CHR) != 0) -#define DIRENT_ISBLK(dtype) (((dtype) & DTYPE_BLK) != 0) -#define DIRENT_ISDIRECTORY(dtype) (((dtype) & DTYPE_DIRECTORY) != 0) -#define DIRENT_ISLINK(dtype) (((dtype) & DTYPE_LINK) != 0) +#define DIRENT_ISUNKNOWN(dtype) ((dtype) == DTYPE_UNKNOWN) +#define DIRENT_ISFIFO(dtype) ((dtype) == DTYPE_FIFO) +#define DIRENT_ISCHR(dtype) ((dtype) == DTYPE_CHR) +#define DIRENT_ISSEM(dtype) ((dtype) == DTYPE_SEM) +#define DIRENT_ISDIRECTORY(dtype) ((dtype) == DTYPE_DIRECTORY) +#define DIRENT_ISMQ(dtype) ((dtype) == DTYPE_MQ) +#define DIRENT_ISBLK(dtype) ((dtype) == DTYPE_BLK) +#define DIRENT_ISSHM(dtype) ((dtype) == DTYPE_SHM) +#define DIRENT_ISFILE(dtype) ((dtype) == DTYPE_FILE) +#define DIRENT_ISMTD(dtype) ((dtype) == DTYPE_MTD) +#define DIRENT_ISLINK(dtype) ((dtype) == DTYPE_LINK) +#define DIRENT_ISSOCK(dtype) ((dtype) == DTYPE_SOCK) /* The d_type field of the dirent structure is not specified by POSIX. It * is a non-standard, 4.5BSD extension that is implemented by most OSs. A @@ -77,14 +88,18 @@ * type names: */ -#define DT_UNKNOWN DTYPE_UNKNOWN -#define DT_FIFO DTYPE_FIFO -#define DT_CHR DTYPE_CHR -#define DT_DIR DTYPE_DIRECTORY -#define DT_BLK DTYPE_BLK -#define DT_REG DTYPE_FILE -#define DT_LNK DTYPE_LINK -#define DT_SOCK DTYPE_SOCK +#define DT_UNKNOWN DTYPE_UNKNOWN +#define DT_FIFO DTYPE_FIFO +#define DT_CHR DTYPE_CHR +#define DT_SEM DTYPE_SEM +#define DT_DIR DTYPE_DIRECTORY +#define DT_MQ DTYPE_MQ +#define DT_BLK DTYPE_BLK +#define DT_SHM DTYPE_SHM +#define DT_REG DTYPE_FILE +#define DT_MTD DTYPE_MTD +#define DT_LNK DTYPE_LINK +#define DT_SOCK DTYPE_SOCK /**************************************************************************** * Public Type Definitions diff --git a/include/nuttx/fs/hostfs.h b/include/nuttx/fs/hostfs.h index 744b80c84b..55aef962f2 100644 --- a/include/nuttx/fs/hostfs.h +++ b/include/nuttx/fs/hostfs.h @@ -58,11 +58,18 @@ /* These must exactly match the definitions from include/dirent.h: */ -#define NUTTX_DTYPE_FILE 0x01 -#define NUTTX_DTYPE_CHR 0x02 -#define NUTTX_DTYPE_BLK 0x04 -#define NUTTX_DTYPE_DIRECTORY 0x08 -#define NUTTX_DTYPE_LINK 0x10 +#define NUTTX_DTYPE_UNKNOWN 0 +#define NUTTX_DTYPE_FIFO 1 +#define NUTTX_DTYPE_CHR 2 +#define NUTTX_DTYPE_SEM 3 +#define NUTTX_DTYPE_DIRECTORY 4 +#define NUTTX_DTYPE_MQ 5 +#define NUTTX_DTYPE_BLK 6 +#define NUTTX_DTYPE_SHM 7 +#define NUTTX_DTYPE_FILE 8 +#define NUTTX_DTYPE_MTD 9 +#define NUTTX_DTYPE_LINK 10 +#define NUTTX_DTYPE_SOCK 12 /* These must exactly match the definitions from include/sys/stat.h: */