fs/vfs: Add nx_stat function

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2020-05-03 16:17:44 +08:00 committed by patacongo
parent 6418d13b92
commit 65308eabb4
2 changed files with 74 additions and 54 deletions

View File

@ -94,7 +94,7 @@ static inline int statroot(FAR struct stat *buf)
* Name: stat_recursive * Name: stat_recursive
* *
* Returned Value: * Returned Value:
* Zero on success; -1 on failure with errno set: * Zero on success; < 0 on failure:
* *
* EACCES Search permission is denied for one of the directories in the * EACCES Search permission is denied for one of the directories in the
* path prefix of path. * path prefix of path.
@ -123,7 +123,6 @@ int stat_recursive(FAR const char *path, FAR struct stat *buf)
* there is no mountpoint that includes in this path. * there is no mountpoint that includes in this path.
*/ */
ret = -ret;
goto errout_with_search; goto errout_with_search;
} }
@ -160,35 +159,62 @@ int stat_recursive(FAR const char *path, FAR struct stat *buf)
ret = inode_stat(inode, buf); ret = inode_stat(inode, buf);
} }
/* Check if the stat operation was successful */
if (ret < 0)
{
ret = -ret;
goto errout_with_inode;
}
/* Successfully stat'ed the file */
inode_release(inode); inode_release(inode);
RELEASE_SEARCH(&desc);
return OK;
/* Failure conditions always set the errno appropriately */
errout_with_inode:
inode_release(inode);
errout_with_search: errout_with_search:
RELEASE_SEARCH(&desc); RELEASE_SEARCH(&desc);
set_errno(ret); return ret;
return ERROR;
} }
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/****************************************************************************
* Name: nx_stat
*
* Description:
* nx_stat() is similar to the standard 'stat' interface except that is
* not a cancellation point and it does not modify the errno variable.
*
* nx_stat() is an internal NuttX interface and should not be called from
* applications.
*
* Returned Value:
* Zero is returned on success; a negated value is returned on any failure.
*
****************************************************************************/
int nx_stat(FAR const char *path, FAR struct stat *buf)
{
/* Sanity checks */
if (path == NULL || buf == NULL)
{
return -EFAULT;
}
if (*path == '\0')
{
return -ENOENT;
}
/* Check for the fake root directory (which has no inode) */
if (strcmp(path, "/") == 0)
{
return statroot(buf);
}
/* The perform the stat() operation on the path. This is potentially
* recursive if soft link support is enabled.
*/
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
buf->st_count = 0;
#endif
return stat_recursive(path, buf);
}
/**************************************************************************** /****************************************************************************
* Name: stat * Name: stat
* *
@ -209,39 +235,14 @@ int stat(FAR const char *path, FAR struct stat *buf)
{ {
int ret; int ret;
/* Sanity checks */ ret = nx_stat(path, buf);
if (ret < 0)
if (path == NULL || buf == NULL)
{ {
ret = EFAULT; set_errno(-ret);
goto errout; ret = ERROR;
} }
if (*path == '\0') return ret;
{
ret = ENOENT;
goto errout;
}
/* Check for the fake root directory (which has no inode) */
if (strcmp(path, "/") == 0)
{
return statroot(buf);
}
/* The perform the stat() operation on the path. This is potentially
* recursive if soft link support is enabled.
*/
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
buf->st_count = 0;
#endif
return stat_recursive(path, buf);
errout:
set_errno(ret);
return ERROR;
} }
/**************************************************************************** /****************************************************************************
@ -249,7 +250,7 @@ errout:
* *
* Description: * Description:
* The inode_stat() function will obtain information about an 'inode' in * The inode_stat() function will obtain information about an 'inode' in
* the pseudo file system and will write it to the area pointed to by 'buf'. * the pseudo file system and write it to the area pointed to by 'buf'.
* *
* The 'buf' argument is a pointer to a stat structure, as defined in * The 'buf' argument is a pointer to a stat structure, as defined in
* <sys/stat.h>, into which information is placed concerning the file. * <sys/stat.h>, into which information is placed concerning the file.
@ -347,7 +348,7 @@ int inode_stat(FAR struct inode *inode, FAR struct stat *buf)
RESET_BUF(buf); RESET_BUF(buf);
} }
/* Make sure that the caller knows that this really a symbolic link. */ /* Make sure the caller knows that this really a symbolic link. */
buf->st_mode |= S_IFLNK; buf->st_mode |= S_IFLNK;
} }

View File

@ -81,6 +81,7 @@
# define _NX_READ(f,b,s) nx_read(f,b,s) # define _NX_READ(f,b,s) nx_read(f,b,s)
# define _NX_WRITE(f,b,s) nx_write(f,b,s) # define _NX_WRITE(f,b,s) nx_write(f,b,s)
# define _NX_SEEK(f,o,w) nx_seek(f,o,w) # define _NX_SEEK(f,o,w) nx_seek(f,o,w)
# define _NX_STAT(p,s) nx_stat(p,s)
# define _NX_GETERRNO(r) (-(r)) # define _NX_GETERRNO(r) (-(r))
# define _NX_SETERRNO(r) set_errno(-(r)) # define _NX_SETERRNO(r) set_errno(-(r))
# define _NX_GETERRVAL(r) (r) # define _NX_GETERRVAL(r) (r)
@ -94,6 +95,7 @@
# define _NX_READ(f,b,s) read(f,b,s) # define _NX_READ(f,b,s) read(f,b,s)
# define _NX_WRITE(f,b,s) write(f,b,s) # define _NX_WRITE(f,b,s) write(f,b,s)
# define _NX_SEEK(f,o,w) lseek(f,o,w) # define _NX_SEEK(f,o,w) lseek(f,o,w)
# define _NX_STAT(p,s) stat(p,s)
# define _NX_GETERRNO(r) errno # define _NX_GETERRNO(r) errno
# define _NX_SETERRNO(r) # define _NX_SETERRNO(r)
# define _NX_GETERRVAL(r) (-errno) # define _NX_GETERRVAL(r) (-errno)
@ -1334,6 +1336,23 @@ int file_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup);
int file_fstat(FAR struct file *filep, FAR struct stat *buf); int file_fstat(FAR struct file *filep, FAR struct stat *buf);
/****************************************************************************
* Name: nx_stat
*
* Description:
* nx_stat() is similar to the standard 'stat' interface except that is
* not a cancellation point and it does not modify the errno variable.
*
* nx_stat() is an internal NuttX interface and should not be called from
* applications.
*
* Returned Value:
* Zero is returned on success; a negated value is returned on any failure.
*
****************************************************************************/
int nx_stat(FAR const char *path, FAR struct stat *buf);
/**************************************************************************** /****************************************************************************
* Name: fdesc_poll * Name: fdesc_poll
* *