Add basic fstat() support. Now all that is needed is to modify ALL of the file systems.

This commit is contained in:
Gregory Nutt 2017-02-12 12:48:24 -06:00
parent 9a3af1a3e0
commit c5a8e96dbc
6 changed files with 370 additions and 175 deletions

View File

@ -240,6 +240,29 @@ int inode_search(FAR struct inode_search_s *desc);
int inode_find(FAR struct inode_search_s *desc);
/****************************************************************************
* Name: inode_stat
*
* Description:
* 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 'buf' argument is a pointer to a stat structure, as defined in
* <sys/stat.h>, into which information is placed concerning the file.
*
* Input Parameters:
* inode - The indoe of interest
* buf - The caller provide location in which to return information about
* the inode.
*
* Returned Value:
* Zero (OK) returned on success. Otherwise, a negated errno value is
* returned to indicate the nature of the failure.
*
****************************************************************************/
int inode_stat(FAR struct inode *inode, FAR struct stat *buf);
/****************************************************************************
* Name: inode_free
*

View File

@ -72,9 +72,9 @@ else
# Common file/socket descriptor support
CSRCS += fs_close.c fs_dup.c fs_dup2.c fs_fcntl.c fs_dupfd.c fs_dupfd2.c
CSRCS += fs_epoll.c fs_getfilep.c fs_ioctl.c fs_lseek.c fs_mkdir.c fs_open.c
CSRCS += fs_poll.c fs_read.c fs_rename.c fs_rmdir.c fs_stat.c fs_statfs.c
CSRCS += fs_select.c fs_unlink.c fs_write.c
CSRCS += fs_epoll.c fs_fstat.c fs_getfilep.c fs_ioctl.c fs_lseek.c
CSRCS += fs_mkdir.c fs_open.c fs_poll.c fs_read.c fs_rename.c fs_rmdir.c fs_statfs.c
CSRCS += fs_stat.c fs_select.c fs_unlink.c fs_write.c
# Certain interfaces are not available if there is no mountpoint support

155
fs/vfs/fs_fstat.c Normal file
View File

@ -0,0 +1,155 @@
/****************************************************************************
* fs/vfs/fs_fstat.c
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <unistd.h>
#include <sched.h>
#include <errno.h>
#include <nuttx/fs/fs.h>
#include "inode/inode.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: fstat
*
* Description:
* The fstat() function will obtain information about an open file
* associated with the file descriptor 'fd', and will write it to the area
* pointed to by 'buf'.
*
* The 'buf' argument is a pointer to a stat structure, as defined in
* <sys/stat.h>, into which information is placed concerning the file.
*
* Input Parameters:
* fd - The file descriptor associated with the open file of interest
* buf - The caller provide location in which to return information about
* the open file.
*
* Returned Value:
* Upon successful completion, 0 shall be returned. Otherwise, -1 shall be
* returned and errno set to indicate the error.
*
****************************************************************************/
int fstat(int fd, FAR struct stat *buf);
{
FAR struct file *filep;
FAR struct inode *inode;
int ret;
/* Did we get a valid file descriptor? */
if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
{
/* No networking... it is a bad descriptor in any event */
set_errno(EBADF);
return ERROR;
}
/* The descriptor is in a valid range to file descriptor... do the
* read. First, get the file structure. Note that on failure,
* fs_getfilep() will set the errno variable.
*/
filep = fs_getfilep(fd);
if (filep == NULL)
{
/* The errno value has already been set */
return ERROR;
}
/* Get the inode from the file structure */
inode = filep->f_inode;
DEBUGASSERT(inode != NULL);
/* The way we handle the stat depends on the type of inode that we
* are dealing with.
*/
#ifndef CONFIG_DISABLE_MOUNTPOINT
if (INODE_IS_MOUNTPT(inode))
{
/* The inode is a file system mointpoint. Verify that the mountpoint
* supports the fstat() method
*/
if (inode->u.i_mops && inode->u.i_mops->fstat)
{
/* Perform the fstat() operation */
ret = inode->u.i_mops->fstat(filep, buf);
}
}
else
#endif
{
/* The inode is part of the root pseudo file system. */
ret = inode_stat(inode, buf);
}
/* Check if the fstat operation was successful */
if (ret < 0)
{
ret = -ret;
goto errout_with_inode;
}
/* Successfully fstat'ed the file */
inode_release(inode);
return OK;
/* Failure conditions always set the errno appropriately */
errout_with_inode:
inode_release(inode);
set_errno(ret);
return ERROR;
}

View File

@ -70,7 +70,6 @@
* Private Function Prototypes
****************************************************************************/
static inline int statpseudo(FAR struct inode *inode, FAR struct stat *buf);
static inline int statroot(FAR struct stat *buf);
int stat_recursive(FAR const char *path, FAR struct stat *buf);
@ -79,11 +78,197 @@ int stat_recursive(FAR const char *path, FAR struct stat *buf);
****************************************************************************/
/****************************************************************************
* Name: statpseudo
* Name: statroot
****************************************************************************/
static inline int statpseudo(FAR struct inode *inode, FAR struct stat *buf)
static inline int statroot(FAR struct stat *buf)
{
/* There is no inode associated with the fake root directory */
RESET_BUF(buf);
buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR;
return OK;
}
/****************************************************************************
* Name: stat_recursive
*
* Returned Value:
* Zero on success; -1 on failure with errno set:
*
* EACCES Search permission is denied for one of the directories in the
* path prefix of path.
* EFAULT Bad address.
* ENOENT A component of the path path does not exist, or the path is an
* empty string.
* ENOMEM Out of memory
* ENOTDIR A component of the path is not a directory.
*
****************************************************************************/
int stat_recursive(FAR const char *path, FAR struct stat *buf)
{
struct inode_search_s desc;
FAR struct inode *inode;
int ret;
/* Get an inode for this path */
SETUP_SEARCH(&desc, path, true);
ret = inode_find(&desc);
if (ret < 0)
{
/* This name does not refer to an inode in the pseudo file system and
* there is no mountpoint that includes in this path.
*/
ret = -ret;
goto errout_with_search;
}
/* Get the search results */
inode = desc.node;
DEBUGASSERT(inode != NULL);
/* The way we handle the stat depends on the type of inode that we
* are dealing with.
*/
#ifndef CONFIG_DISABLE_MOUNTPOINT
if (INODE_IS_MOUNTPT(inode))
{
/* The node is a file system mointpoint. Verify that the mountpoint
* supports the stat() method
*/
if (inode->u.i_mops && inode->u.i_mops->stat)
{
/* Perform the stat() operation */
ret = inode->u.i_mops->stat(inode, desc.relpath, buf);
}
}
else
#endif
{
/* The node is part of the root pseudo file system. This path may
* recurse if soft links are supported in the pseudo file system.
*/
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);
RELEASE_SEARCH(&desc);
return OK;
/* Failure conditions always set the errno appropriately */
errout_with_inode:
inode_release(inode);
errout_with_search:
RELEASE_SEARCH(&desc);
set_errno(ret);
return ERROR;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: stat
*
* Returned Value:
* Zero on success; -1 on failure with errno set:
*
* EACCES Search permission is denied for one of the directories in the
* path prefix of path.
* EFAULT Bad address.
* ENOENT A component of the path path does not exist, or the path is an
* empty string.
* ENOMEM Out of memory
* ENOTDIR A component of the path is not a directory.
*
****************************************************************************/
int stat(FAR const char *path, FAR struct stat *buf)
{
int ret;
/* Sanity checks */
if (path == NULL || buf == NULL)
{
ret = EFAULT;
goto errout;
}
if (*path == '\0')
{
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;
}
/****************************************************************************
* Name: inode_stat
*
* Description:
* 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 'buf' argument is a pointer to a stat structure, as defined in
* <sys/stat.h>, into which information is placed concerning the file.
*
* Input Parameters:
* inode - The indoe of interest
* buf - The caller provide location in which to return information about
* the inode.
*
* Returned Value:
* Zero (OK) returned on success. Otherwise, a negated errno value is
* returned to indicate the nature of the failure.
*
****************************************************************************/
int inode_stat(FAR struct inode *inode, FAR struct stat *buf)
{
DEBUGASSERT(inode != NULL && buf != NULL);
/* Most of the stat entries just do not apply */
RESET_BUF(buf);
@ -210,170 +395,3 @@ static inline int statpseudo(FAR struct inode *inode, FAR struct stat *buf)
return OK;
}
/****************************************************************************
* Name: statroot
****************************************************************************/
static inline int statroot(FAR struct stat *buf)
{
/* There is no inode associated with the fake root directory */
RESET_BUF(buf);
buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR;
return OK;
}
/****************************************************************************
* Name: stat_recursive
*
* Returned Value:
* Zero on success; -1 on failure with errno set:
*
* EACCES Search permission is denied for one of the directories in the
* path prefix of path.
* EFAULT Bad address.
* ENOENT A component of the path path does not exist, or the path is an
* empty string.
* ENOMEM Out of memory
* ENOTDIR A component of the path is not a directory.
*
****************************************************************************/
int stat_recursive(FAR const char *path, FAR struct stat *buf)
{
struct inode_search_s desc;
FAR struct inode *inode;
int ret;
/* Get an inode for this path */
SETUP_SEARCH(&desc, path, true);
ret = inode_find(&desc);
if (ret < 0)
{
/* This name does not refer to a psudeo-inode and there is no
* mountpoint that includes in this path.
*/
ret = -ret;
goto errout_with_search;
}
/* Get the search results */
inode = desc.node;
DEBUGASSERT(inode != NULL);
/* The way we handle the stat depends on the type of inode that we
* are dealing with.
*/
#ifndef CONFIG_DISABLE_MOUNTPOINT
if (INODE_IS_MOUNTPT(inode))
{
/* The node is a file system mointpoint. Verify that the mountpoint
* supports the stat() method
*/
if (inode->u.i_mops && inode->u.i_mops->stat)
{
/* Perform the stat() operation */
ret = inode->u.i_mops->stat(inode, desc.relpath, buf);
}
}
else
#endif
{
/* The node is part of the root pseudo file system. This path may
* recurse if soft links are supported in the pseudo file system.
*/
ret = statpseudo(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);
RELEASE_SEARCH(&desc);
return OK;
/* Failure conditions always set the errno appropriately */
errout_with_inode:
inode_release(inode);
errout_with_search:
RELEASE_SEARCH(&desc);
set_errno(ret);
return ERROR;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: stat
*
* Returned Value:
* Zero on success; -1 on failure with errno set:
*
* EACCES Search permission is denied for one of the directories in the
* path prefix of path.
* EFAULT Bad address.
* ENOENT A component of the path path does not exist, or the path is an
* empty string.
* ENOMEM Out of memory
* ENOTDIR A component of the path is not a directory.
*
****************************************************************************/
int stat(FAR const char *path, FAR struct stat *buf)
{
int ret;
/* Sanity checks */
if (path == NULL || buf == NULL)
{
ret = EFAULT;
goto errout;
}
if (*path == '\0')
{
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;
}

View File

@ -265,6 +265,7 @@ struct mountpt_operations
int (*sync)(FAR struct file *filep);
int (*dup)(FAR const struct file *oldp, FAR struct file *newp);
int (*fstat)(FAR const struct file *filep, FAR struct stat *buf);
/* Directory operations */

View File

@ -155,9 +155,7 @@ extern "C"
int mkdir(FAR const char *pathname, mode_t mode);
int mkfifo(FAR const char *pathname, mode_t mode);
int stat(const char *path, FAR struct stat *buf);
#if 0 /* Not yet supported */
int fstat(int fd, FAR struct stat *buf);
#endif
#undef EXTERN
#if defined(__cplusplus)