2008-09-12 16:46:44 +02:00
|
|
|
/****************************************************************************
|
2014-09-28 19:17:36 +02:00
|
|
|
* fs/dirent/fs_readdir.c
|
2007-03-14 23:41:09 +01:00
|
|
|
*
|
2017-02-03 18:23:57 +01:00
|
|
|
* Copyright (C) 2007-2009, 2011, 2017 Gregory Nutt. All rights reserved.
|
2012-07-14 23:05:40 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-03-14 23:41:09 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-09-12 16:46:44 +02:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-03-14 23:41:09 +01:00
|
|
|
* 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.
|
|
|
|
*
|
2008-09-12 16:46:44 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2008-09-12 16:46:44 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Included Files
|
2008-09-12 16:46:44 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-15 01:18:32 +01:00
|
|
|
|
2007-03-14 23:41:09 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
2011-03-28 02:05:58 +02:00
|
|
|
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/dirent.h>
|
2009-12-15 01:18:32 +01:00
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2008-09-12 16:46:44 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Private Functions
|
2008-09-12 16:46:44 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2008-09-12 16:46:44 +02:00
|
|
|
/****************************************************************************
|
2012-08-05 19:44:11 +02:00
|
|
|
* Name: readpseudodir
|
2008-09-12 16:46:44 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2012-08-05 19:44:11 +02:00
|
|
|
static inline int readpseudodir(struct fs_dirent_s *idir)
|
2007-03-14 23:41:09 +01:00
|
|
|
{
|
|
|
|
FAR struct inode *prev;
|
|
|
|
|
|
|
|
/* Check if we are at the end of the list */
|
|
|
|
|
2012-08-05 19:44:11 +02:00
|
|
|
if (!idir->u.pseudo.fd_next)
|
2007-03-14 23:41:09 +01:00
|
|
|
{
|
2007-05-26 21:22:34 +02:00
|
|
|
/* End of file and error conditions are not distinguishable
|
|
|
|
* with readdir. Here we return -ENOENT to signal the end
|
|
|
|
* of the directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -ENOENT;
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the inode name into the dirent structure */
|
|
|
|
|
2012-08-05 19:44:11 +02:00
|
|
|
strncpy(idir->fd_dir.d_name, idir->u.pseudo.fd_next->i_name, NAME_MAX+1);
|
2007-03-14 23:41:09 +01:00
|
|
|
|
|
|
|
/* If the node has file operations, we will say that it is
|
|
|
|
* a file.
|
|
|
|
*/
|
|
|
|
|
2007-05-26 21:22:34 +02:00
|
|
|
idir->fd_dir.d_type = 0;
|
2012-08-05 19:44:11 +02:00
|
|
|
if (idir->u.pseudo.fd_next->u.i_ops)
|
2007-03-14 23:41:09 +01:00
|
|
|
{
|
2007-05-27 00:37:57 +02:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
2014-02-19 16:21:38 +01:00
|
|
|
if (INODE_IS_BLOCK(idir->u.pseudo.fd_next))
|
2007-05-27 00:37:57 +02:00
|
|
|
{
|
|
|
|
idir->fd_dir.d_type |= DTYPE_BLK;
|
|
|
|
}
|
2017-02-03 18:23:57 +01:00
|
|
|
else if (INODE_IS_MOUNTPT(idir->u.pseudo.fd_next))
|
2007-05-27 00:37:57 +02:00
|
|
|
{
|
|
|
|
idir->fd_dir.d_type |= DTYPE_DIRECTORY;
|
|
|
|
}
|
|
|
|
else
|
2017-02-03 18:23:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
if (INODE_IS_SOFTLINK(idir->u.pseudo.fd_next))
|
|
|
|
{
|
|
|
|
idir->fd_dir.d_type |= DTYPE_LINK;
|
|
|
|
}
|
|
|
|
else
|
2007-05-27 00:37:57 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
idir->fd_dir.d_type |= DTYPE_CHR;
|
|
|
|
}
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
2014-02-19 16:21:38 +01:00
|
|
|
/* If the node has child node(s) or no operations, then we will say that
|
|
|
|
* it is a directory rather than a special file. NOTE: that the node can
|
|
|
|
* be both!
|
2007-03-14 23:41:09 +01:00
|
|
|
*/
|
|
|
|
|
2012-08-05 19:44:11 +02:00
|
|
|
if (idir->u.pseudo.fd_next->i_child || !idir->u.pseudo.fd_next->u.i_ops)
|
2007-03-14 23:41:09 +01:00
|
|
|
{
|
2007-05-26 21:22:34 +02:00
|
|
|
idir->fd_dir.d_type |= DTYPE_DIRECTORY;
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now get the inode to vist next time that readdir() is called */
|
|
|
|
|
|
|
|
inode_semtake();
|
|
|
|
|
2012-08-05 19:44:11 +02:00
|
|
|
prev = idir->u.pseudo.fd_next;
|
|
|
|
idir->u.pseudo.fd_next = prev->i_peer; /* The next node to visit */
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2012-08-05 19:44:11 +02:00
|
|
|
if (idir->u.pseudo.fd_next)
|
2007-03-14 23:41:09 +01:00
|
|
|
{
|
|
|
|
/* Increment the reference count on this next node */
|
|
|
|
|
2012-08-05 19:44:11 +02:00
|
|
|
idir->u.pseudo.fd_next->i_crefs++;
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inode_semgive();
|
|
|
|
|
|
|
|
if (prev)
|
|
|
|
{
|
|
|
|
inode_release(prev);
|
|
|
|
}
|
|
|
|
|
2007-05-26 21:22:34 +02:00
|
|
|
return OK;
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
2008-09-12 16:46:44 +02:00
|
|
|
/****************************************************************************
|
2007-05-26 18:05:59 +02:00
|
|
|
* Public Functions
|
2008-09-12 16:46:44 +02:00
|
|
|
****************************************************************************/
|
2007-05-26 18:05:59 +02:00
|
|
|
|
2008-09-12 16:46:44 +02:00
|
|
|
/****************************************************************************
|
2007-05-26 18:05:59 +02:00
|
|
|
* Name: readdir
|
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 23:05:40 +02:00
|
|
|
* The readdir() function returns a pointer to a dirent structure
|
|
|
|
* representing the next directory entry in the directory stream pointed
|
|
|
|
* to by dir. It returns NULL on reaching the end-of-file or if an error
|
|
|
|
* occurred.
|
2007-05-26 18:05:59 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2012-07-14 23:05:40 +02:00
|
|
|
* dirp -- An instance of type DIR created by a previous call to opendir();
|
2007-05-26 18:05:59 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2012-07-14 23:05:40 +02:00
|
|
|
* The readdir() function returns a pointer to a dirent structure, or NULL
|
|
|
|
* if an error occurs or end-of-file is reached. On error, errno is set
|
|
|
|
* appropriately.
|
2007-05-26 18:05:59 +02:00
|
|
|
*
|
|
|
|
* EBADF - Invalid directory stream descriptor dir
|
|
|
|
*
|
2008-09-12 16:46:44 +02:00
|
|
|
****************************************************************************/
|
2007-05-26 18:05:59 +02:00
|
|
|
|
|
|
|
FAR struct dirent *readdir(DIR *dirp)
|
|
|
|
{
|
2011-03-28 02:05:58 +02:00
|
|
|
FAR struct fs_dirent_s *idir = (struct fs_dirent_s *)dirp;
|
2007-05-26 21:22:34 +02:00
|
|
|
struct inode *inode;
|
|
|
|
int ret;
|
2007-05-26 18:05:59 +02:00
|
|
|
|
2014-02-19 16:21:38 +01:00
|
|
|
/* Verify that we were provided with a valid directory structure */
|
2007-05-26 18:05:59 +02:00
|
|
|
|
2014-02-19 16:21:38 +01:00
|
|
|
if (!idir)
|
2007-05-26 18:05:59 +02:00
|
|
|
{
|
2007-05-26 21:22:34 +02:00
|
|
|
ret = EBADF;
|
|
|
|
goto errout;
|
2007-05-26 18:05:59 +02:00
|
|
|
}
|
|
|
|
|
2014-02-19 16:21:38 +01:00
|
|
|
/* A special case is when we enumerate an "empty", unused inode. That is
|
|
|
|
* an inode in the pseudo-filesystem that has no operations and no children.
|
|
|
|
* This is a "dangling" directory entry that has lost its children.
|
|
|
|
*/
|
|
|
|
|
|
|
|
inode = idir->fd_root;
|
|
|
|
if (!inode)
|
|
|
|
{
|
|
|
|
/* End of file and error conditions are not distinguishable
|
|
|
|
* with readdir. We return NULL to signal either case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2007-05-26 18:05:59 +02:00
|
|
|
/* The way we handle the readdir depends on the type of inode
|
|
|
|
* that we are dealing with.
|
|
|
|
*/
|
|
|
|
|
2007-05-27 00:37:57 +02:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
2012-08-05 19:44:11 +02:00
|
|
|
if (INODE_IS_MOUNTPT(inode) && !DIRENT_ISPSEUDONODE(idir->fd_flags))
|
2007-05-26 18:05:59 +02:00
|
|
|
{
|
2007-05-26 21:22:34 +02:00
|
|
|
/* The node is a file system mointpoint. Verify that the mountpoint
|
|
|
|
* supports the readdir() method
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!inode->u.i_mops || !inode->u.i_mops->readdir)
|
2012-07-14 23:05:40 +02:00
|
|
|
{
|
|
|
|
ret = EACCES;
|
2014-02-19 16:21:38 +01:00
|
|
|
goto errout;
|
2012-07-14 23:05:40 +02:00
|
|
|
}
|
2007-05-26 18:05:59 +02:00
|
|
|
|
2007-05-26 21:22:34 +02:00
|
|
|
/* Perform the readdir() operation */
|
|
|
|
|
|
|
|
ret = inode->u.i_mops->readdir(inode, idir);
|
2007-05-26 18:05:59 +02:00
|
|
|
}
|
|
|
|
else
|
2007-05-27 00:37:57 +02:00
|
|
|
#endif
|
2007-05-26 18:05:59 +02:00
|
|
|
{
|
2012-08-05 19:44:11 +02:00
|
|
|
/* The node is part of the root pseudo file system */
|
2007-05-27 00:37:57 +02:00
|
|
|
|
2012-08-05 19:44:11 +02:00
|
|
|
ret = readpseudodir(idir);
|
2007-05-26 21:22:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ret < 0 is an error. Special case: ret = -ENOENT is end of file */
|
|
|
|
|
2014-02-19 16:21:38 +01:00
|
|
|
if (ret < 0)
|
2007-05-26 21:22:34 +02:00
|
|
|
{
|
|
|
|
if (ret == -ENOENT)
|
|
|
|
{
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ret;
|
|
|
|
}
|
2014-02-19 16:21:38 +01:00
|
|
|
|
2007-05-26 21:22:34 +02:00
|
|
|
goto errout;
|
2007-05-26 18:05:59 +02:00
|
|
|
}
|
2007-05-26 21:22:34 +02:00
|
|
|
|
|
|
|
/* Success */
|
|
|
|
|
|
|
|
idir->fd_position++;
|
|
|
|
return &idir->fd_dir;
|
|
|
|
|
|
|
|
errout:
|
2012-07-14 23:05:40 +02:00
|
|
|
set_errno(ret);
|
2007-05-26 21:22:34 +02:00
|
|
|
return NULL;
|
2007-05-26 18:05:59 +02:00
|
|
|
}
|
|
|
|
|