vfs/stat: Make the flag defintion more confirm POSIX standard

specified here:
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2020-07-05 23:30:43 +08:00 committed by Abdelatif Guettouche
parent 1a523e5519
commit 91ed14c631
7 changed files with 130 additions and 78 deletions

View File

@ -66,36 +66,48 @@ static void host_stat_convert(struct stat *hostbuf, struct nuttx_stat_s *buf)
{ {
/* Map the return values */ /* Map the return values */
buf->st_mode = hostbuf->st_mode & 0777; buf->st_mode = hostbuf->st_mode & 07777;
if (hostbuf->st_mode & S_IFDIR) if (S_ISDIR(hostbuf->st_mode))
{ {
buf->st_mode |= NUTTX_S_IFDIR; buf->st_mode |= NUTTX_S_IFDIR;
} }
else if (hostbuf->st_mode & S_IFREG) else if (S_ISREG(hostbuf->st_mode))
{ {
buf->st_mode |= NUTTX_S_IFREG; buf->st_mode |= NUTTX_S_IFREG;
} }
else if (hostbuf->st_mode & S_IFCHR) else if (S_ISCHR(hostbuf->st_mode))
{ {
buf->st_mode |= NUTTX_S_IFCHR; buf->st_mode |= NUTTX_S_IFCHR;
} }
else if (hostbuf->st_mode & S_IFBLK) else if (S_ISBLK(hostbuf->st_mode))
{ {
buf->st_mode |= NUTTX_S_IFBLK; buf->st_mode |= NUTTX_S_IFBLK;
} }
else if (hostbuf->st_mode & S_IFLNK) else if (S_ISLNK(hostbuf->st_mode))
{ {
buf->st_mode |= NUTTX_S_IFLNK; buf->st_mode |= NUTTX_S_IFLNK;
} }
else if (hostbuf->st_mode & S_IFIFO) else if (S_ISFIFO(hostbuf->st_mode))
{ {
buf->st_mode |= NUTTX_S_IFIFO; buf->st_mode |= NUTTX_S_IFIFO;
} }
else if (hostbuf->st_mode & S_IFSOCK) else if (S_ISSOCK(hostbuf->st_mode))
{ {
buf->st_mode |= NUTTX_S_IFSOCK; buf->st_mode |= NUTTX_S_IFSOCK;
} }
else if (S_TYPEISSEM(hostbuf))
{
buf->st_mode |= NUTTX_S_IFSEM;
}
else if (S_TYPEISMQ(hostbuf))
{
buf->st_mode |= NUTTX_S_IFMQ;
}
else if (S_TYPEISSHM(hostbuf))
{
buf->st_mode |= NUTTX_S_IFSHM;
}
buf->st_dev = hostbuf->st_dev; buf->st_dev = hostbuf->st_dev;
buf->st_ino = hostbuf->st_ino; buf->st_ino = hostbuf->st_ino;

View File

@ -106,7 +106,8 @@ static uint32_t cromfs_addr2offset(FAR const struct cromfs_volume_s *fs,
FAR const void *addr); FAR const void *addr);
static int cromfs_foreach_node(FAR const struct cromfs_volume_s *fs, static int cromfs_foreach_node(FAR const struct cromfs_volume_s *fs,
FAR const struct cromfs_node_s *node, FAR const struct cromfs_node_s *node,
cromfs_foreach_t callback, FAR void *arg); cromfs_foreach_t callback,
FAR void *arg);
static uint16_t cromfs_seglen(FAR const char *relpath); static uint16_t cromfs_seglen(FAR const char *relpath);
static int cromfs_comparenode(FAR const struct cromfs_volume_s *fs, static int cromfs_comparenode(FAR const struct cromfs_volume_s *fs,
FAR const struct cromfs_node_s *node, FAR const struct cromfs_node_s *node,
@ -120,11 +121,15 @@ static int cromfs_findnode(FAR const struct cromfs_volume_s *fs,
static int cromfs_open(FAR struct file *filep, const char *relpath, static int cromfs_open(FAR struct file *filep, const char *relpath,
int oflags, mode_t mode); int oflags, mode_t mode);
static int cromfs_close(FAR struct file *filep); static int cromfs_close(FAR struct file *filep);
static ssize_t cromfs_read(FAR struct file *filep, char *buffer, size_t buflen); static ssize_t cromfs_read(FAR struct file *filep,
static int cromfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg); char *buffer, size_t buflen);
static int cromfs_ioctl(FAR struct file *filep,
int cmd, unsigned long arg);
static int cromfs_dup(FAR const struct file *oldp, FAR struct file *newp); static int cromfs_dup(FAR const struct file *oldp,
static int cromfs_fstat(FAR const struct file *filep, FAR struct stat *buf); FAR struct file *newp);
static int cromfs_fstat(FAR const struct file *filep,
FAR struct stat *buf);
static int cromfs_opendir(struct inode *mountpt, const char *relpath, static int cromfs_opendir(struct inode *mountpt, const char *relpath,
struct fs_dirent_s *dir); struct fs_dirent_s *dir);
@ -133,15 +138,15 @@ static int cromfs_readdir(FAR struct inode *mountpt,
static int cromfs_rewinddir(FAR struct inode *mountpt, static int cromfs_rewinddir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir); FAR struct fs_dirent_s *dir);
static int cromfs_bind(FAR struct inode *blkdriver, FAR const void *data, static int cromfs_bind(FAR struct inode *blkdriver,
FAR void **handle); FAR const void *data, FAR void **handle);
static int cromfs_unbind(FAR void *handle, FAR struct inode **blkdriver, static int cromfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
unsigned int flags); unsigned int flags);
static int cromfs_statfs(FAR struct inode *mountpt, static int cromfs_statfs(FAR struct inode *mountpt,
FAR struct statfs *buf); FAR struct statfs *buf);
static int cromfs_stat(FAR struct inode *mountpt, FAR const char *relpath, static int cromfs_stat(FAR struct inode *mountpt,
FAR struct stat *buf); FAR const char *relpath, FAR struct stat *buf);
/**************************************************************************** /****************************************************************************
* Public Data * Public Data
@ -521,7 +526,9 @@ static int cromfs_open(FAR struct file *filep, FAR const char *relpath,
ret = cromfs_findnode(fs, &node, relpath); ret = cromfs_findnode(fs, &node, relpath);
if (ret < 0) if (ret < 0)
{ {
/* Nothing exists at that relative path (or a really bad error occurred) */ /* Nothing exists at that relative path (or a really bad error
* occurred)
*/
return ret; return ret;
} }
@ -662,8 +669,8 @@ static ssize_t cromfs_read(FAR struct file *filep, FAR char *buffer,
while (remaining > 0) while (remaining > 0)
{ {
/* Search for the next block containing the fpos file offset. This is /* Search for the next block containing the fpos file offset. This is
* real search on the first time through but the remaining blocks should * real search on the first time through but the remaining blocks
* be contiguous so that the logic should not loop. * should be contiguous so that the logic should not loop.
* *
*/ */
@ -702,7 +709,9 @@ static ssize_t cromfs_read(FAR struct file *filep, FAR char *buffer,
} }
while (fpos >= (blkoffs + ulen)); while (fpos >= (blkoffs + ulen));
/* Check if we need to decompress the next block into the user buffer. */ /* Check if we need to decompress the next block into the user
* buffer.
*/
if (currhdr->lzf_type == LZF_TYPE0_HDR) if (currhdr->lzf_type == LZF_TYPE0_HDR)
{ {
@ -756,9 +765,10 @@ static ssize_t cromfs_read(FAR struct file *filep, FAR char *buffer,
ff->ff_ulen = decomplen; ff->ff_ulen = decomplen;
} }
finfo("voloffs=%lu blkoffs=%lu ulen=%u ff_offset=%u copysize=%u\n", finfo(
(unsigned long)voloffs, (unsigned long)blkoffs, ulen, "voloffs=%lu blkoffs=%lu ulen=%u ff_offset=%u copysize=%u\n",
ff->ff_offset, copysize); (unsigned long)voloffs, (unsigned long)blkoffs, ulen,
ff->ff_offset, copysize);
DEBUGASSERT(ff->ff_ulen >= copysize); DEBUGASSERT(ff->ff_ulen >= copysize);
} }
else else
@ -769,7 +779,8 @@ static ssize_t cromfs_read(FAR struct file *filep, FAR char *buffer,
* decompression buffer. * decompression buffer.
*/ */
copyoffs = (blkoffs >= filep->f_pos) ? 0 : filep->f_pos - blkoffs; copyoffs = (blkoffs >= filep->f_pos) ?
0 : filep->f_pos - blkoffs;
DEBUGASSERT(ulen > copyoffs); DEBUGASSERT(ulen > copyoffs);
copysize = ulen - copyoffs; copysize = ulen - copyoffs;
@ -863,7 +874,7 @@ static int cromfs_dup(FAR const struct file *oldp, FAR struct file *newp)
* same node. * same node.
*/ */
newff = (FAR struct cromfs_file_s *)kmm_zalloc(sizeof(struct cromfs_file_s)); newff = kmm_zalloc(sizeof(struct cromfs_file_s));
if (newff == NULL) if (newff == NULL)
{ {
return -ENOMEM; return -ENOMEM;
@ -966,7 +977,9 @@ static int cromfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
ret = cromfs_findnode(fs, &node, relpath); ret = cromfs_findnode(fs, &node, relpath);
if (ret < 0) if (ret < 0)
{ {
/* Nothing exists at that relative path (or a really bad error occurred) */ /* Nothing exists at that relative path (or a really bad error
* occurred)
*/
return ret; return ret;
} }
@ -1045,7 +1058,7 @@ static int cromfs_readdir(struct inode *mountpt, struct fs_dirent_s *dir)
finfo("Entry %lu: %s\n", (unsigned long)offset, name); finfo("Entry %lu: %s\n", (unsigned long)offset, name);
strncpy(dir->fd_dir.d_name, name, NAME_MAX + 1); strncpy(dir->fd_dir.d_name, name, NAME_MAX + 1);
switch (node->cn_mode & s_IFTGT) switch (node->cn_mode & S_IFMT)
{ {
case S_IFDIR: /* Directory */ case S_IFDIR: /* Directory */
dir->fd_dir.d_type = DTYPE_DIRECTORY; dir->fd_dir.d_type = DTYPE_DIRECTORY;

View File

@ -1248,11 +1248,20 @@ static int romfs_stat_common(uint8_t type, uint32_t size,
buf->st_mode = S_IFDIR | S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP | buf->st_mode = S_IFDIR | S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP |
S_IRUSR | S_IXUSR; S_IRUSR | S_IXUSR;
} }
else if (IS_FILE(type)) else if (IS_FILE(type) || IS_SOFTLINK(type))
{ {
if (IS_FILE(type))
{
buf->st_mode = S_IFREG;
}
else
{
buf->st_mode = S_IFLNK;
}
/* It's a read-only file name */ /* It's a read-only file name */
buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR; buf->st_mode |= S_IROTH | S_IRGRP | S_IRUSR;
if (IS_EXECUTABLE(type)) if (IS_EXECUTABLE(type))
{ {
/* It's a read-execute file name */ /* It's a read-execute file name */

View File

@ -100,6 +100,11 @@
#define IS_HARDLINK(rfn) IS_MODE(rfn,RFNEXT_HARDLINK) #define IS_HARDLINK(rfn) IS_MODE(rfn,RFNEXT_HARDLINK)
#define IS_DIRECTORY(rfn) IS_MODE(rfn,RFNEXT_DIRECTORY) #define IS_DIRECTORY(rfn) IS_MODE(rfn,RFNEXT_DIRECTORY)
#define IS_FILE(rfn) IS_MODE(rfn,RFNEXT_FILE) #define IS_FILE(rfn) IS_MODE(rfn,RFNEXT_FILE)
#define IS_SOFTLINK(rfn) IS_MODE(rfn,RFNEXT_SOFTLINK)
#define IS_BLOCKDEV(rfn) IS_MODE(rfn,RFNEXT_BLOCKDEV)
#define IS_CHARDEV(rfn) IS_MODE(rfn,RFNEXT_CHARDEV)
#define IS_SOCKET(rfn) IS_MODE(rfn,RFNEXT_SOCKET)
#define IS_FIFO(rfn) IS_MODE(rfn,RFNEXT_FIFO)
#define IS_EXECUTABLE(rfn) (((rfn) & RFNEXT_EXEC) != 0) #define IS_EXECUTABLE(rfn) (((rfn) & RFNEXT_EXEC) != 0)
/* RFNEXT_SOFTLINK, RFNEXT_BLOCKDEV, RFNEXT_CHARDEV, RFNEXT_SOCKET, and /* RFNEXT_SOFTLINK, RFNEXT_BLOCKDEV, RFNEXT_CHARDEV, RFNEXT_SOCKET, and

View File

@ -375,7 +375,7 @@ int inode_stat(FAR struct inode *inode, FAR struct stat *buf, int resolve)
{ {
/* Make sure the caller knows that this is a symbolic link. */ /* Make sure the caller knows that this is a symbolic link. */
buf->st_mode |= S_IFLNK; buf->st_mode = S_IRWXO | S_IRWXG | S_IRWXU | S_IFLNK;
} }
} }
else else

View File

@ -66,32 +66,37 @@
/* These must exactly match the definitions from include/sys/stat.h: */ /* These must exactly match the definitions from include/sys/stat.h: */
#define NUTTX_S_IFIFO (0 << 11) #define NUTTX_S_IFIFO (1 << 12)
#define NUTTX_S_IFCHR (1 << 11) #define NUTTX_S_IFCHR (2 << 12)
#define NUTTX_S_IFDIR (2 << 11) #define NUTTX_S_IFSEM (3 << 12)
#define NUTTX_S_IFBLK (3 << 11) #define NUTTX_S_IFDIR (4 << 12)
#define NUTTX_S_IFREG (4 << 11) #define NUTTX_S_IFMQ (5 << 12)
#define NUTTX_S_IFSOCK (8 << 11) #define NUTTX_S_IFBLK (6 << 12)
#define NUTTX_S_IFLNK (1 << 15) #define NUTTX_S_IFSHM (7 << 12)
#define NUTTX_S_IFREG (8 << 12)
#define NUTTX_S_IFMTD (9 << 12)
#define NUTTX_S_IFLNK (10 << 12)
#define NUTTX_S_IFSOCK (12 << 12)
#define NUTTX_S_IFMT (15 << 12)
/* These must exactly match the definitions from include/fcntl.h: */ /* These must exactly match the definitions from include/fcntl.h: */
#define NUTTX_O_RDONLY (1 << 0) /* Open for read access (only) */ #define NUTTX_O_RDONLY (1 << 0) /* Open for read access (only) */
#define NUTTX_O_WRONLY (1 << 1) /* Open for write access (only) */ #define NUTTX_O_WRONLY (1 << 1) /* Open for write access (only) */
#define NUTTX_O_CREAT (1 << 2) /* Create file/sem/mq object */ #define NUTTX_O_CREAT (1 << 2) /* Create file/sem/mq object */
#define NUTTX_O_EXCL (1 << 3) /* Name must not exist when opened */ #define NUTTX_O_EXCL (1 << 3) /* Name must not exist when opened */
#define NUTTX_O_APPEND (1 << 4) /* Keep contents, append to end */ #define NUTTX_O_APPEND (1 << 4) /* Keep contents, append to end */
#define NUTTX_O_TRUNC (1 << 5) /* Delete contents */ #define NUTTX_O_TRUNC (1 << 5) /* Delete contents */
#define NUTTX_O_NONBLOCK (1 << 6) /* Don't wait for data */ #define NUTTX_O_NONBLOCK (1 << 6) /* Don't wait for data */
#define NUTTX_O_SYNC (1 << 7) /* Synchronize output on write */ #define NUTTX_O_SYNC (1 << 7) /* Synchronize output on write */
#define NUTTX_O_BINARY (1 << 8) /* Open the file in binary mode. */ #define NUTTX_O_BINARY (1 << 8) /* Open the file in binary mode. */
#define NUTTX_O_DIRECT (1 << 9) /* Avoid caching, write directly to hardware */ #define NUTTX_O_DIRECT (1 << 9) /* Avoid caching, write directly to hardware */
#define NUTTX_O_RDWR (NUTTX_O_RDONLY | NUTTX_O_WRONLY) #define NUTTX_O_RDWR (NUTTX_O_RDONLY | NUTTX_O_WRONLY)
/* Should match definition in include/limits.h */ /* Should match definition in include/limits.h */
#define NUTTX_NAME_MAX CONFIG_NAME_MAX #define NUTTX_NAME_MAX CONFIG_NAME_MAX
#endif /* __SIM__ */ #endif /* __SIM__ */

View File

@ -63,38 +63,46 @@
#define S_IRUSR (1 << 8) #define S_IRUSR (1 << 8)
#define S_IRWXU (7 << 6) #define S_IRWXU (7 << 6)
#define S_ISVTX 0 /* "Sticky" bit (not used) */ #define S_ISVTX (1 << 9) /* "Sticky" bit (not used) */
#define S_ISGID 0 /* Set group ID bit (not used)*/ #define S_ISGID (1 << 10) /* Set group ID bit (not used)*/
#define S_ISUID 0 /* Set UID bit (not used) */ #define S_ISUID (1 << 11) /* Set UID bit (not used) */
#define S_IFIFO 0 /* Bits 11-14: File type bits (not all used) */ #define S_IFIFO (1 << 12) /* Bits 12-15: File type bits (not all used) */
#define S_IFCHR (1 << 11) #define S_IFCHR (2 << 12)
#define S_IFDIR (2 << 11) #define S_IFSEM (3 << 12)
#define S_IFBLK (3 << 11) #define S_IFDIR (4 << 12)
#define S_IFREG (4 << 11) #define S_IFMQ (5 << 12)
#define S_IFMQ (5 << 11) #define S_IFBLK (6 << 12)
#define S_IFSEM (6 << 11) #define S_IFSHM (7 << 12)
#define S_IFSHM (7 << 11) #define S_IFREG (8 << 12)
#define S_IFSOCK (8 << 11) #define S_IFMTD (9 << 12)
#define S_IFMTD (9 << 11) #define S_IFLNK (10 << 12)
#define s_IFTGT (15 << 11) /* May be the target of a symbolic link */ #define S_IFSOCK (12 << 12)
#define S_IFMT (15 << 12)
#define S_IFLNK (1 << 15) /* Bit 15: Symbolic link */
#define S_IFMT (31 << 11) /* Bits 11-15: Full file type */
/* File type macros that operate on an instance of mode_t */ /* File type macros that operate on an instance of mode_t */
#define S_ISFIFO(m) (0) #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
#define S_ISCHR(m) (((m) & s_IFTGT) == S_IFCHR) #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
#define S_ISDIR(m) (((m) & s_IFTGT) == S_IFDIR) #define S_ISSEM(m) (((m) & S_IFMT) == S_IFSEM)
#define S_ISBLK(m) (((m) & s_IFTGT) == S_IFBLK) #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#define S_ISREG(m) (((m) & s_IFTGT) == S_IFREG) #define S_ISMQ(m) (((m) & S_IFMT) == S_IFMQ)
#define S_ISMQ(m) (((m) & s_IFTGT) == S_IFMQ) #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
#define S_ISSEM(m) (((m) & s_IFTGT) == S_IFSEM) #define S_ISSHM(m) (((m) & S_IFMT) == S_IFSHM)
#define S_ISSHM(m) (((m) & s_IFTGT) == S_IFSHM) #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#define S_ISSOCK(m) (((m) & s_IFTGT) == S_IFSOCK) #define S_ISMTD(m) (((m) & S_IFMT) == S_IFMTD)
#define S_ISMTD(m) (((m) & s_IFTGT) == S_IFMTD) #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
#define S_ISLNK(m) (((m) & S_IFLNK) != 0) #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
/* These are from POSIX.1b. If the objects are not implemented using separate
* distinct file types, the macros always will evaluate to zero. Unlike the
* other S_* macros the following three take a pointer to a `struct stat'
* object as the argument.
*/
#define S_TYPEISSEM(buf) S_ISSEM((buf)->st_mode)
#define S_TYPEISMQ(buf) S_ISMQ((buf)->st_mode)
#define S_TYPEISSHM(buf) S_ISSHM((buf)->st_mode)
/* The following macros are required by POSIX to acheive backward /* The following macros are required by POSIX to acheive backward
* compatibility with earlier versions of struct stat. * compatibility with earlier versions of struct stat.