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 <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2020-07-06 00:34:32 +08:00 committed by Abdelatif Guettouche
parent 91ed14c631
commit 67ef70d460
6 changed files with 118 additions and 37 deletions

View File

@ -389,11 +389,14 @@ int host_readdir(void *dirp, struct nuttx_dirent_s *entry)
/* Map the type */ /* Map the type */
entry->d_type = 0;
if (ent->d_type == DT_REG) if (ent->d_type == DT_REG)
{ {
entry->d_type = NUTTX_DTYPE_FILE; 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) else if (ent->d_type == DT_CHR)
{ {
entry->d_type = NUTTX_DTYPE_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; 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; return 0;
} }

View File

@ -1069,14 +1069,41 @@ static int cromfs_readdir(struct inode *mountpt, struct fs_dirent_s *dir)
break; break;
case S_IFIFO: /* FIFO */ case S_IFIFO: /* FIFO */
dir->fd_dir.d_type = DTYPE_FIFO;
break;
case S_IFCHR: /* Character driver */ case S_IFCHR: /* Character driver */
dir->fd_dir.d_type = DTYPE_CHR;
break;
case S_IFBLK: /* Block driver */ case S_IFBLK: /* Block driver */
#if 0 dir->fd_dir.d_type = DTYPE_BLK;
break;
case S_IFSOCK: /* Socket */ case S_IFSOCK: /* Socket */
#endif dir->fd_dir.d_type = DTYPE_SOCK;
break;
case S_IFMQ: /* Message queue */ case S_IFMQ: /* Message queue */
dir->fd_dir.d_type = DTYPE_MQ;
break;
case S_IFSEM: /* Semaphore */ case S_IFSEM: /* Semaphore */
dir->fd_dir.d_type = DTYPE_SEM;
break;
case S_IFSHM: /* Shared memory */ 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: default:
DEBUGPANIC(); DEBUGPANIC();
dir->fd_dir.d_type = DTYPE_UNKNOWN; dir->fd_dir.d_type = DTYPE_UNKNOWN;

View File

@ -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. */ /* 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) if (idir->u.pseudo.fd_next->u.i_ops)
{ {
#ifndef CONFIG_DISABLE_MOUNTPOINT #ifndef CONFIG_DISABLE_MOUNTPOINT
if (INODE_IS_BLOCK(idir->u.pseudo.fd_next) || if (INODE_IS_BLOCK(idir->u.pseudo.fd_next))
INODE_IS_MTD(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)) 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 else
#endif #endif
#ifdef CONFIG_PSEUDOFS_SOFTLINKS #ifdef CONFIG_PSEUDOFS_SOFTLINKS
if (INODE_IS_SOFTLINK(idir->u.pseudo.fd_next)) if (INODE_IS_SOFTLINK(idir->u.pseudo.fd_next))
{ {
idir->fd_dir.d_type |= DTYPE_LINK; idir->fd_dir.d_type = DTYPE_LINK;
} }
else else
#endif #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) 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 */ /* Now get the inode to visit next time that readdir() is called */

View File

@ -943,6 +943,11 @@ static int romfs_readdir(FAR struct inode *mountpt,
dir->fd_dir.d_type = DTYPE_FILE; dir->fd_dir.d_type = DTYPE_FILE;
break; break;
} }
else if (IS_SOFTLINK(next))
{
dir->fd_dir.d_type = DTYPE_LINK;
break;
}
} }
errout_with_semaphore: errout_with_semaphore:

View File

@ -55,20 +55,31 @@
* top-level, pseudo-file system, an inode can be BOTH a file and a directory * 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_UNKNOWN 0
#define DTYPE_FILE (1 << 0) /* Bit 0: Regular file */ #define DTYPE_FIFO 1
#define DTYPE_CHR (1 << 1) /* Bit 1: Character device */ #define DTYPE_CHR 2
#define DTYPE_BLK (1 << 2) /* Bit 2: Block device */ #define DTYPE_SEM 3
#define DTYPE_DIRECTORY (1 << 3) /* Bit 3: Directory */ #define DTYPE_DIRECTORY 4
#define DTYPE_LINK (1 << 4) /* Bit 4: Symbolic link */ #define DTYPE_MQ 5
#define DTYPE_FIFO (1 << 5) /* Bit 5: Named Pipe (FIFO) */ #define DTYPE_BLK 6
#define DTYPE_SOCK (1 << 6) /* Bit 6: UNIX domain socket */ #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_ISUNKNOWN(dtype) ((dtype) == DTYPE_UNKNOWN)
#define DIRENT_ISCHR(dtype) (((dtype) & DTYPE_CHR) != 0) #define DIRENT_ISFIFO(dtype) ((dtype) == DTYPE_FIFO)
#define DIRENT_ISBLK(dtype) (((dtype) & DTYPE_BLK) != 0) #define DIRENT_ISCHR(dtype) ((dtype) == DTYPE_CHR)
#define DIRENT_ISDIRECTORY(dtype) (((dtype) & DTYPE_DIRECTORY) != 0) #define DIRENT_ISSEM(dtype) ((dtype) == DTYPE_SEM)
#define DIRENT_ISLINK(dtype) (((dtype) & DTYPE_LINK) != 0) #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 /* 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 * is a non-standard, 4.5BSD extension that is implemented by most OSs. A
@ -77,14 +88,18 @@
* type names: * type names:
*/ */
#define DT_UNKNOWN DTYPE_UNKNOWN #define DT_UNKNOWN DTYPE_UNKNOWN
#define DT_FIFO DTYPE_FIFO #define DT_FIFO DTYPE_FIFO
#define DT_CHR DTYPE_CHR #define DT_CHR DTYPE_CHR
#define DT_DIR DTYPE_DIRECTORY #define DT_SEM DTYPE_SEM
#define DT_BLK DTYPE_BLK #define DT_DIR DTYPE_DIRECTORY
#define DT_REG DTYPE_FILE #define DT_MQ DTYPE_MQ
#define DT_LNK DTYPE_LINK #define DT_BLK DTYPE_BLK
#define DT_SOCK DTYPE_SOCK #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 * Public Type Definitions

View File

@ -58,11 +58,18 @@
/* These must exactly match the definitions from include/dirent.h: */ /* These must exactly match the definitions from include/dirent.h: */
#define NUTTX_DTYPE_FILE 0x01 #define NUTTX_DTYPE_UNKNOWN 0
#define NUTTX_DTYPE_CHR 0x02 #define NUTTX_DTYPE_FIFO 1
#define NUTTX_DTYPE_BLK 0x04 #define NUTTX_DTYPE_CHR 2
#define NUTTX_DTYPE_DIRECTORY 0x08 #define NUTTX_DTYPE_SEM 3
#define NUTTX_DTYPE_LINK 0x10 #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: */ /* These must exactly match the definitions from include/sys/stat.h: */