From 91ed14c631389ddfa709275acc1ac8d14afa9d87 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Sun, 5 Jul 2020 23:30:43 +0800 Subject: [PATCH] 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 --- arch/sim/src/sim/up_hostfs.c | 28 +++++++++++----- fs/cromfs/fs_cromfs.c | 53 ++++++++++++++++++----------- fs/romfs/fs_romfs.c | 13 ++++++-- fs/romfs/fs_romfs.h | 5 +++ fs/vfs/fs_stat.c | 2 +- include/nuttx/fs/hostfs.h | 43 +++++++++++++----------- include/sys/stat.h | 64 ++++++++++++++++++++---------------- 7 files changed, 130 insertions(+), 78 deletions(-) diff --git a/arch/sim/src/sim/up_hostfs.c b/arch/sim/src/sim/up_hostfs.c index 8e35559db0..c3926a49f7 100644 --- a/arch/sim/src/sim/up_hostfs.c +++ b/arch/sim/src/sim/up_hostfs.c @@ -66,36 +66,48 @@ static void host_stat_convert(struct stat *hostbuf, struct nuttx_stat_s *buf) { /* 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; } - else if (hostbuf->st_mode & S_IFREG) + else if (S_ISREG(hostbuf->st_mode)) { 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; } - else if (hostbuf->st_mode & S_IFBLK) + else if (S_ISBLK(hostbuf->st_mode)) { 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; } - else if (hostbuf->st_mode & S_IFIFO) + else if (S_ISFIFO(hostbuf->st_mode)) { 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; } + 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_ino = hostbuf->st_ino; diff --git a/fs/cromfs/fs_cromfs.c b/fs/cromfs/fs_cromfs.c index 28e4f305fb..39bef9cc77 100644 --- a/fs/cromfs/fs_cromfs.c +++ b/fs/cromfs/fs_cromfs.c @@ -106,7 +106,8 @@ static uint32_t cromfs_addr2offset(FAR const struct cromfs_volume_s *fs, FAR const void *addr); static int cromfs_foreach_node(FAR const struct cromfs_volume_s *fs, 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 int cromfs_comparenode(FAR const struct cromfs_volume_s *fs, 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, int oflags, mode_t mode); static int cromfs_close(FAR struct file *filep); -static ssize_t cromfs_read(FAR struct file *filep, char *buffer, size_t buflen); -static int cromfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg); +static ssize_t cromfs_read(FAR struct file *filep, + 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_fstat(FAR const struct file *filep, FAR struct stat *buf); +static int cromfs_dup(FAR const struct file *oldp, + 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, 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, FAR struct fs_dirent_s *dir); -static int cromfs_bind(FAR struct inode *blkdriver, FAR const void *data, - FAR void **handle); +static int cromfs_bind(FAR struct inode *blkdriver, + FAR const void *data, FAR void **handle); static int cromfs_unbind(FAR void *handle, FAR struct inode **blkdriver, unsigned int flags); static int cromfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf); -static int cromfs_stat(FAR struct inode *mountpt, FAR const char *relpath, - FAR struct stat *buf); +static int cromfs_stat(FAR struct inode *mountpt, + FAR const char *relpath, FAR struct stat *buf); /**************************************************************************** * Public Data @@ -521,7 +526,9 @@ static int cromfs_open(FAR struct file *filep, FAR const char *relpath, ret = cromfs_findnode(fs, &node, relpath); 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; } @@ -662,8 +669,8 @@ static ssize_t cromfs_read(FAR struct file *filep, FAR char *buffer, while (remaining > 0) { /* Search for the next block containing the fpos file offset. This is - * real search on the first time through but the remaining blocks should - * be contiguous so that the logic should not loop. + * real search on the first time through but the remaining blocks + * 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)); - /* 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) { @@ -756,9 +765,10 @@ static ssize_t cromfs_read(FAR struct file *filep, FAR char *buffer, ff->ff_ulen = decomplen; } - finfo("voloffs=%lu blkoffs=%lu ulen=%u ff_offset=%u copysize=%u\n", - (unsigned long)voloffs, (unsigned long)blkoffs, ulen, - ff->ff_offset, copysize); + finfo( + "voloffs=%lu blkoffs=%lu ulen=%u ff_offset=%u copysize=%u\n", + (unsigned long)voloffs, (unsigned long)blkoffs, ulen, + ff->ff_offset, copysize); DEBUGASSERT(ff->ff_ulen >= copysize); } else @@ -769,7 +779,8 @@ static ssize_t cromfs_read(FAR struct file *filep, FAR char *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); copysize = ulen - copyoffs; @@ -863,7 +874,7 @@ static int cromfs_dup(FAR const struct file *oldp, FAR struct file *newp) * 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) { return -ENOMEM; @@ -966,7 +977,9 @@ static int cromfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, ret = cromfs_findnode(fs, &node, relpath); 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; } @@ -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); 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 */ dir->fd_dir.d_type = DTYPE_DIRECTORY; diff --git a/fs/romfs/fs_romfs.c b/fs/romfs/fs_romfs.c index 5e8847ddab..2c54930562 100644 --- a/fs/romfs/fs_romfs.c +++ b/fs/romfs/fs_romfs.c @@ -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 | 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 */ - buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR; + buf->st_mode |= S_IROTH | S_IRGRP | S_IRUSR; if (IS_EXECUTABLE(type)) { /* It's a read-execute file name */ diff --git a/fs/romfs/fs_romfs.h b/fs/romfs/fs_romfs.h index 2db440d2f9..fd35bb3864 100644 --- a/fs/romfs/fs_romfs.h +++ b/fs/romfs/fs_romfs.h @@ -100,6 +100,11 @@ #define IS_HARDLINK(rfn) IS_MODE(rfn,RFNEXT_HARDLINK) #define IS_DIRECTORY(rfn) IS_MODE(rfn,RFNEXT_DIRECTORY) #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) /* RFNEXT_SOFTLINK, RFNEXT_BLOCKDEV, RFNEXT_CHARDEV, RFNEXT_SOCKET, and diff --git a/fs/vfs/fs_stat.c b/fs/vfs/fs_stat.c index 7889efaf7b..e8b9714c73 100644 --- a/fs/vfs/fs_stat.c +++ b/fs/vfs/fs_stat.c @@ -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. */ - buf->st_mode |= S_IFLNK; + buf->st_mode = S_IRWXO | S_IRWXG | S_IRWXU | S_IFLNK; } } else diff --git a/include/nuttx/fs/hostfs.h b/include/nuttx/fs/hostfs.h index 81f5eb0453..744b80c84b 100644 --- a/include/nuttx/fs/hostfs.h +++ b/include/nuttx/fs/hostfs.h @@ -66,32 +66,37 @@ /* These must exactly match the definitions from include/sys/stat.h: */ -#define NUTTX_S_IFIFO (0 << 11) -#define NUTTX_S_IFCHR (1 << 11) -#define NUTTX_S_IFDIR (2 << 11) -#define NUTTX_S_IFBLK (3 << 11) -#define NUTTX_S_IFREG (4 << 11) -#define NUTTX_S_IFSOCK (8 << 11) -#define NUTTX_S_IFLNK (1 << 15) +#define NUTTX_S_IFIFO (1 << 12) +#define NUTTX_S_IFCHR (2 << 12) +#define NUTTX_S_IFSEM (3 << 12) +#define NUTTX_S_IFDIR (4 << 12) +#define NUTTX_S_IFMQ (5 << 12) +#define NUTTX_S_IFBLK (6 << 12) +#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: */ -#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_CREAT (1 << 2) /* Create file/sem/mq object */ -#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_TRUNC (1 << 5) /* Delete contents */ -#define NUTTX_O_NONBLOCK (1 << 6) /* Don't wait for data */ -#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_DIRECT (1 << 9) /* Avoid caching, write directly to hardware */ +#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_CREAT (1 << 2) /* Create file/sem/mq object */ +#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_TRUNC (1 << 5) /* Delete contents */ +#define NUTTX_O_NONBLOCK (1 << 6) /* Don't wait for data */ +#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_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 */ -#define NUTTX_NAME_MAX CONFIG_NAME_MAX +#define NUTTX_NAME_MAX CONFIG_NAME_MAX #endif /* __SIM__ */ diff --git a/include/sys/stat.h b/include/sys/stat.h index 6483c46dcd..6ee05eb8c3 100644 --- a/include/sys/stat.h +++ b/include/sys/stat.h @@ -63,38 +63,46 @@ #define S_IRUSR (1 << 8) #define S_IRWXU (7 << 6) -#define S_ISVTX 0 /* "Sticky" bit (not used) */ -#define S_ISGID 0 /* Set group ID bit (not used)*/ -#define S_ISUID 0 /* Set UID bit (not used) */ +#define S_ISVTX (1 << 9) /* "Sticky" bit (not used) */ +#define S_ISGID (1 << 10) /* Set group ID 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_IFCHR (1 << 11) -#define S_IFDIR (2 << 11) -#define S_IFBLK (3 << 11) -#define S_IFREG (4 << 11) -#define S_IFMQ (5 << 11) -#define S_IFSEM (6 << 11) -#define S_IFSHM (7 << 11) -#define S_IFSOCK (8 << 11) -#define S_IFMTD (9 << 11) -#define s_IFTGT (15 << 11) /* May be the target of a symbolic link */ - -#define S_IFLNK (1 << 15) /* Bit 15: Symbolic link */ -#define S_IFMT (31 << 11) /* Bits 11-15: Full file type */ +#define S_IFIFO (1 << 12) /* Bits 12-15: File type bits (not all used) */ +#define S_IFCHR (2 << 12) +#define S_IFSEM (3 << 12) +#define S_IFDIR (4 << 12) +#define S_IFMQ (5 << 12) +#define S_IFBLK (6 << 12) +#define S_IFSHM (7 << 12) +#define S_IFREG (8 << 12) +#define S_IFMTD (9 << 12) +#define S_IFLNK (10 << 12) +#define S_IFSOCK (12 << 12) +#define S_IFMT (15 << 12) /* File type macros that operate on an instance of mode_t */ -#define S_ISFIFO(m) (0) -#define S_ISCHR(m) (((m) & s_IFTGT) == S_IFCHR) -#define S_ISDIR(m) (((m) & s_IFTGT) == S_IFDIR) -#define S_ISBLK(m) (((m) & s_IFTGT) == S_IFBLK) -#define S_ISREG(m) (((m) & s_IFTGT) == S_IFREG) -#define S_ISMQ(m) (((m) & s_IFTGT) == S_IFMQ) -#define S_ISSEM(m) (((m) & s_IFTGT) == S_IFSEM) -#define S_ISSHM(m) (((m) & s_IFTGT) == S_IFSHM) -#define S_ISSOCK(m) (((m) & s_IFTGT) == S_IFSOCK) -#define S_ISMTD(m) (((m) & s_IFTGT) == S_IFMTD) -#define S_ISLNK(m) (((m) & S_IFLNK) != 0) +#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) +#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) +#define S_ISSEM(m) (((m) & S_IFMT) == S_IFSEM) +#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) +#define S_ISMQ(m) (((m) & S_IFMT) == S_IFMQ) +#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) +#define S_ISSHM(m) (((m) & S_IFMT) == S_IFSHM) +#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) +#define S_ISMTD(m) (((m) & S_IFMT) == S_IFMTD) +#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) +#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 * compatibility with earlier versions of struct stat.