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
|
|
|
*
|
2020-03-30 01:36:19 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-03-14 23:41:09 +01:00
|
|
|
*
|
2020-03-30 01:36:19 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-03-14 23:41:09 +01:00
|
|
|
*
|
2020-03-30 01:36:19 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-03-14 23:41:09 +01:00
|
|
|
*
|
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;
|
2020-03-30 01:36:19 +02:00
|
|
|
int ret;
|
2007-03-14 23:41:09 +01:00
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
2018-11-08 16:59:18 +01:00
|
|
|
/* End of file and error conditions are not distinguishable with
|
|
|
|
* readdir. Here we return -ENOENT to signal the end of the
|
|
|
|
* directory.
|
2007-05-26 21:22:34 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
return -ENOENT;
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the inode name into the dirent structure */
|
|
|
|
|
2018-11-08 16:59:18 +01:00
|
|
|
strncpy(idir->fd_dir.d_name, idir->u.pseudo.fd_next->i_name,
|
2021-01-16 09:58:18 +01:00
|
|
|
NAME_MAX);
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2018-11-08 16:59:18 +01:00
|
|
|
/* If the node has file operations, we will say that it is a file. */
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2020-07-05 18:34:32 +02:00
|
|
|
idir->fd_dir.d_type = DTYPE_UNKNOWN;
|
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
|
2020-07-05 18:34:32 +02:00
|
|
|
if (INODE_IS_BLOCK(idir->u.pseudo.fd_next))
|
2007-05-27 00:37:57 +02:00
|
|
|
{
|
2020-07-05 18:34:32 +02:00
|
|
|
idir->fd_dir.d_type = DTYPE_BLK;
|
|
|
|
}
|
|
|
|
else if (INODE_IS_MTD(idir->u.pseudo.fd_next))
|
|
|
|
{
|
|
|
|
idir->fd_dir.d_type = DTYPE_MTD;
|
2007-05-27 00:37:57 +02:00
|
|
|
}
|
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
|
|
|
{
|
2020-07-05 18:34:32 +02:00
|
|
|
idir->fd_dir.d_type = DTYPE_DIRECTORY;
|
2007-05-27 00:37:57 +02:00
|
|
|
}
|
|
|
|
else
|
2017-02-03 18:23:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
if (INODE_IS_SOFTLINK(idir->u.pseudo.fd_next))
|
|
|
|
{
|
2020-07-05 18:34:32 +02:00
|
|
|
idir->fd_dir.d_type = DTYPE_LINK;
|
2017-02-03 18:23:57 +01:00
|
|
|
}
|
|
|
|
else
|
2007-05-27 00:37:57 +02:00
|
|
|
#endif
|
2020-07-05 18:34:32 +02:00
|
|
|
if (INODE_IS_DRIVER(idir->u.pseudo.fd_next))
|
|
|
|
{
|
|
|
|
idir->fd_dir.d_type = DTYPE_CHR;
|
|
|
|
}
|
|
|
|
else if (INODE_IS_NAMEDSEM(idir->u.pseudo.fd_next))
|
|
|
|
{
|
|
|
|
idir->fd_dir.d_type = DTYPE_SEM;
|
|
|
|
}
|
|
|
|
else if (INODE_IS_MQUEUE(idir->u.pseudo.fd_next))
|
|
|
|
{
|
|
|
|
idir->fd_dir.d_type = DTYPE_MQ;
|
|
|
|
}
|
|
|
|
else if (INODE_IS_SHM(idir->u.pseudo.fd_next))
|
2007-05-27 00:37:57 +02:00
|
|
|
{
|
2020-07-05 18:34:32 +02:00
|
|
|
idir->fd_dir.d_type = DTYPE_SHM;
|
2007-05-27 00:37:57 +02:00
|
|
|
}
|
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
|
|
|
{
|
2020-07-05 18:34:32 +02:00
|
|
|
idir->fd_dir.d_type = DTYPE_DIRECTORY;
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
2020-03-30 01:36:19 +02:00
|
|
|
/* Now get the inode to visit next time that readdir() is called */
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2020-03-30 01:36:19 +02:00
|
|
|
ret = inode_semtake();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2007-03-14 23:41:09 +01:00
|
|
|
|
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
|
2020-03-30 01:36:19 +02:00
|
|
|
* an inode in the pseudo-filesystem that has no operations and no
|
|
|
|
* children. This is a "dangling" directory entry that has lost its
|
|
|
|
* children.
|
2014-02-19 16:21:38 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
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
|
|
|
}
|