2008-01-01 00:52:53 +01:00
|
|
|
/****************************************************************************
|
2014-09-28 19:17:36 +02:00
|
|
|
* fs/dirent/fs_opendir.c
|
2007-03-14 23:41:09 +01:00
|
|
|
*
|
2014-02-19 16:21:38 +01:00
|
|
|
* Copyright (C) 2007-2009, 2011, 2013-2014 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-01-01 00:52:53 +01: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-01-01 00:52:53 +01:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2008-01-01 00:52:53 +01:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Included Files
|
2008-01-01 00:52:53 +01:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2008-05-31 22:14:15 +02:00
|
|
|
|
2009-12-15 01:18:32 +01:00
|
|
|
#include <stdbool.h>
|
2007-03-14 23:41:09 +01:00
|
|
|
#include <dirent.h>
|
2007-05-26 18:05:59 +02:00
|
|
|
#include <string.h>
|
2008-05-31 22:14:15 +02:00
|
|
|
#include <assert.h>
|
2007-03-14 23:41:09 +01:00
|
|
|
#include <errno.h>
|
2008-05-31 22:14:15 +02:00
|
|
|
|
2011-04-05 22:54:00 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/dirent.h>
|
2008-05-31 22:14:15 +02:00
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2008-01-01 00:52:53 +01:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Private Functions
|
2008-01-01 00:52:53 +01:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2011-03-28 19:43:34 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: open_mountpoint
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the case where the inode to be opened is within a mountpoint.
|
|
|
|
*
|
|
|
|
* Inputs:
|
|
|
|
* inode -- the inode of the mountpoint to open
|
|
|
|
* relpath -- the relative path within the mountpoint to open
|
|
|
|
* dir -- the dirent structure to be initialized
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* On success, OK is returned; Otherwise, a positive errno is returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
static inline int open_mountpoint(FAR struct inode *inode,
|
|
|
|
FAR const char *relpath,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* The inode itself as the 'root' of mounted volume. The actually
|
|
|
|
* directory is at relpath into the* mounted filesystem.
|
|
|
|
*
|
|
|
|
* Verify that the mountpoint inode supports the opendir() method
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!inode->u.i_mops || !inode->u.i_mops->opendir)
|
|
|
|
{
|
|
|
|
return ENOSYS;
|
|
|
|
}
|
|
|
|
|
2014-02-19 16:21:38 +01:00
|
|
|
/* Take reference to the mountpoint inode. Note that we do not use
|
|
|
|
* inode_addref() because we already hold the tree semaphore.
|
2011-03-28 19:43:34 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
inode->i_crefs++;
|
|
|
|
|
|
|
|
/* Perform the opendir() operation */
|
|
|
|
|
|
|
|
ret = inode->u.i_mops->opendir(inode, relpath, dir);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* We now need to back off our reference to the inode. We can't
|
|
|
|
* call inode_release() to do that unless we release the tree
|
|
|
|
* semaphore. The following should be safe because: (1) after the
|
|
|
|
* reference count was incremented above it should be >=1 so it should
|
|
|
|
* not decrement below zero, and (2) we hold the tree semaphore so no
|
|
|
|
* other thread should be able to change the reference count.
|
|
|
|
*/
|
|
|
|
|
|
|
|
inode->i_crefs--;
|
|
|
|
DEBUGASSERT(inode->i_crefs >= 0);
|
|
|
|
|
|
|
|
/* Negate the error value so that it can be used to set errno */
|
|
|
|
|
|
|
|
return -ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: open_pseudodir
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the case where the inode to be opened is within the top-level
|
|
|
|
* pseudo-file system.
|
|
|
|
*
|
|
|
|
* Inputs:
|
|
|
|
* inode -- the inode of the mountpoint to open
|
|
|
|
* dir -- the dirent structure to be initialized
|
|
|
|
*
|
|
|
|
* Return:
|
2014-02-19 16:21:38 +01:00
|
|
|
* None
|
2011-03-28 19:43:34 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void open_pseudodir(FAR struct inode *inode, FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
2012-08-05 19:44:11 +02:00
|
|
|
/* We have a valid pseudo-filesystem node. Take two references on the
|
2011-03-28 19:43:34 +02:00
|
|
|
* inode -- one for the parent (fd_root) and one for the child (fd_next).
|
|
|
|
* Note that we do not call inode_addref because we are holding the tree
|
|
|
|
* semaphore and that would result in deadlock.
|
|
|
|
*/
|
|
|
|
|
2014-02-19 16:21:38 +01:00
|
|
|
inode->i_crefs += 2;
|
|
|
|
dir->fd_root = inode; /* Save the inode where we start */
|
2012-08-05 19:44:11 +02:00
|
|
|
dir->u.pseudo.fd_next = inode; /* This is the next node to use for readdir() */
|
2011-03-28 19:43:34 +02:00
|
|
|
|
2012-08-05 19:44:11 +02:00
|
|
|
/* Flag the inode as belonging to the pseudo-filesystem */
|
2011-03-28 19:43:34 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
2012-08-05 19:44:11 +02:00
|
|
|
DIRENT_SETPSEUDONODE(dir->fd_flags);
|
2011-03-28 19:43:34 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-19 16:21:38 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: open_emptydir
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the case where the inode to be opened is an empty, directory node
|
|
|
|
* within the top-level pseudo-file system. That is, it has no operations
|
|
|
|
* and, therefore, it must be a directory node. But is has no children
|
|
|
|
* to be enumerated either.
|
|
|
|
*
|
|
|
|
* Inputs:
|
|
|
|
* dir -- the dirent structure to be initialized
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void open_emptydir(FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
|
|
|
/* We have a valid, but empty pseudo-filesystem node. fd_next is NULL
|
|
|
|
* meaning that we are already at the end of the list of its children.
|
|
|
|
* fd_root is NULL so that if the directory is rewound, it will still be
|
|
|
|
* at the end of the list.
|
|
|
|
*/
|
|
|
|
|
2014-09-01 00:24:24 +02:00
|
|
|
#if 0 /* Already nullified by kumm_zalloc */
|
2014-02-19 16:21:38 +01:00
|
|
|
dir->fd_root = NULL; /* Save the inode where we start */
|
|
|
|
dir->u.pseudo.fd_next = NULL; /* We are at the end of the list */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Flag the inode as belonging to the pseudo-filesystem */
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
DIRENT_SETPSEUDONODE(dir->fd_flags);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-01-01 00:52:53 +01:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Public Functions
|
2008-01-01 00:52:53 +01:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2008-01-01 00:52:53 +01:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Name: opendir
|
|
|
|
*
|
|
|
|
* Description:
|
2011-03-28 19:43:34 +02:00
|
|
|
* The opendir() function opens a directory stream corresponding to the
|
|
|
|
* directory name, and returns a pointer to the directory stream. The
|
|
|
|
* stream is positioned at the first entry in the directory.
|
2007-03-14 23:41:09 +01:00
|
|
|
*
|
|
|
|
* Inputs:
|
|
|
|
* path -- the directory to open
|
|
|
|
*
|
|
|
|
* Return:
|
2011-03-28 19:43:34 +02:00
|
|
|
* The opendir() function returns a pointer to the directory stream. On
|
|
|
|
* error, NULL is returned, and errno is set appropriately.
|
2007-03-14 23:41:09 +01:00
|
|
|
*
|
|
|
|
* EACCES - Permission denied.
|
|
|
|
* EMFILE - Too many file descriptors in use by process.
|
|
|
|
* ENFILE - Too many files are currently open in the
|
|
|
|
* system.
|
|
|
|
* ENOENT - Directory does not exist, or name is an empty
|
|
|
|
* string.
|
|
|
|
* ENOMEM - Insufficient memory to complete the operation.
|
|
|
|
* ENOTDIR - 'path' is not a directory.
|
|
|
|
*
|
2008-01-01 00:52:53 +01:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2008-01-01 00:52:53 +01:00
|
|
|
FAR DIR *opendir(FAR const char *path)
|
2007-03-14 23:41:09 +01:00
|
|
|
{
|
2007-05-27 00:37:57 +02:00
|
|
|
FAR struct inode *inode = NULL;
|
2011-03-28 02:05:58 +02:00
|
|
|
FAR struct fs_dirent_s *dir;
|
2008-01-01 00:52:53 +01:00
|
|
|
FAR const char *relpath;
|
2014-02-19 16:21:38 +01:00
|
|
|
bool isroot = false;
|
2007-05-26 18:05:59 +02:00
|
|
|
int ret;
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2007-05-27 00:37:57 +02:00
|
|
|
/* If we are given 'nothing' then we will interpret this as
|
|
|
|
* request for the root inode.
|
2007-03-14 23:41:09 +01:00
|
|
|
*/
|
|
|
|
|
2007-06-10 19:50:16 +02:00
|
|
|
inode_semtake();
|
2015-06-11 19:24:20 +02:00
|
|
|
if (path == NULL || *path == '\0' || strcmp(path, "/") == 0)
|
2007-05-27 00:37:57 +02:00
|
|
|
{
|
2015-06-11 19:24:20 +02:00
|
|
|
inode = g_root_inode;
|
2015-06-05 00:58:59 +02:00
|
|
|
isroot = true;
|
2012-07-14 23:05:40 +02:00
|
|
|
relpath = NULL;
|
2007-05-27 00:37:57 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We don't know what to do with relative pathes */
|
|
|
|
|
2012-07-14 23:05:40 +02:00
|
|
|
if (*path != '/')
|
2007-05-27 00:37:57 +02:00
|
|
|
{
|
2007-06-10 19:50:16 +02:00
|
|
|
ret = -ENOTDIR;
|
|
|
|
goto errout_with_semaphore;
|
2007-05-27 00:37:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the node matching the path. */
|
|
|
|
|
2015-06-05 00:58:59 +02:00
|
|
|
inode = inode_search(&path, (FAR struct inode**)NULL,
|
|
|
|
(FAR struct inode**)NULL, &relpath);
|
2007-05-27 00:37:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Did we get an inode? */
|
|
|
|
|
2007-03-14 23:41:09 +01:00
|
|
|
if (!inode)
|
|
|
|
{
|
2015-10-11 19:39:29 +02:00
|
|
|
/* 'path' is not a does not exist. */
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2007-05-26 18:05:59 +02:00
|
|
|
ret = ENOTDIR;
|
2007-05-27 00:37:57 +02:00
|
|
|
goto errout_with_semaphore;
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a type DIR -- which is little more than an inode
|
|
|
|
* container.
|
|
|
|
*/
|
|
|
|
|
2014-09-01 00:24:24 +02:00
|
|
|
dir = (FAR struct fs_dirent_s *)kumm_zalloc(sizeof(struct fs_dirent_s));
|
2007-03-14 23:41:09 +01:00
|
|
|
if (!dir)
|
|
|
|
{
|
2015-10-11 19:39:29 +02:00
|
|
|
/* Insufficient memory to complete the operation. */
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2007-05-26 18:05:59 +02:00
|
|
|
ret = ENOMEM;
|
2007-05-27 00:37:57 +02:00
|
|
|
goto errout_with_semaphore;
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
2007-05-26 18:05:59 +02:00
|
|
|
/* Populate the DIR structure and return it to the caller. The way that
|
2012-08-05 19:44:11 +02:00
|
|
|
* we do this depends on whenever this is a "normal" pseudo-file-system
|
2007-05-26 18:05:59 +02:00
|
|
|
* inode or a file system mountpoint.
|
|
|
|
*/
|
|
|
|
|
2007-05-26 21:22:34 +02:00
|
|
|
dir->fd_position = 0; /* This is the position in the read stream */
|
2007-05-26 18:05:59 +02:00
|
|
|
|
2011-03-28 19:43:34 +02:00
|
|
|
/* First, handle the special case of the root inode. This must be
|
|
|
|
* special-cased here because the root inode might ALSO be a mountpoint.
|
|
|
|
*/
|
2007-05-26 18:05:59 +02:00
|
|
|
|
2014-02-19 16:21:38 +01:00
|
|
|
if (isroot)
|
2011-03-28 19:43:34 +02:00
|
|
|
{
|
|
|
|
/* Whatever payload the root inode carries, the root inode is always
|
|
|
|
* a directory inode in the pseudo-file system
|
2008-05-31 22:14:15 +02:00
|
|
|
*/
|
2007-05-26 18:37:37 +02:00
|
|
|
|
2011-03-28 19:43:34 +02:00
|
|
|
open_pseudodir(inode, dir);
|
|
|
|
}
|
2007-05-27 00:37:57 +02:00
|
|
|
|
2012-08-05 19:44:11 +02:00
|
|
|
/* Is this a node in the pseudo filesystem? Or a mountpoint? If the node
|
2014-02-19 16:21:38 +01:00
|
|
|
* is the root (isroot == TRUE), then this is a special case.
|
2011-03-28 19:43:34 +02:00
|
|
|
*/
|
2007-05-26 18:37:37 +02:00
|
|
|
|
2011-03-28 19:43:34 +02:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
else if (INODE_IS_MOUNTPT(inode))
|
|
|
|
{
|
2014-02-19 16:21:38 +01:00
|
|
|
/* Yes, the node is a file system mountpoint */
|
|
|
|
|
|
|
|
dir->fd_root = inode; /* Save the inode where we start */
|
|
|
|
|
|
|
|
/* Open the directory at the relative path */
|
2007-05-26 18:05:59 +02:00
|
|
|
|
2011-03-28 19:43:34 +02:00
|
|
|
ret = open_mountpoint(inode, relpath, dir);
|
|
|
|
if (ret != OK)
|
2007-05-26 18:05:59 +02:00
|
|
|
{
|
2011-03-28 19:43:34 +02:00
|
|
|
goto errout_with_direntry;
|
2007-05-26 18:05:59 +02:00
|
|
|
}
|
|
|
|
}
|
2007-05-26 21:22:34 +02:00
|
|
|
#endif
|
2011-03-28 19:43:34 +02:00
|
|
|
else
|
2007-05-26 18:05:59 +02:00
|
|
|
{
|
2014-02-19 16:21:38 +01:00
|
|
|
/* The node is part of the root pseudo file system. Does the inode
|
|
|
|
* have a child? If so that the child would be the 'root' of a list
|
|
|
|
* of nodes under the directory.
|
2007-05-27 00:37:57 +02:00
|
|
|
*/
|
|
|
|
|
2014-02-19 16:21:38 +01:00
|
|
|
FAR struct inode *child = inode->i_child;
|
|
|
|
if (child)
|
|
|
|
{
|
|
|
|
/* It looks we have a valid pseudo-filesystem directory node. */
|
|
|
|
|
|
|
|
open_pseudodir(child, dir);
|
|
|
|
}
|
|
|
|
else if (!inode->u.i_ops)
|
|
|
|
{
|
|
|
|
/* This is a dangling node with no children and no operations. Set
|
|
|
|
* up to enumerate an empty directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
open_emptydir(dir);
|
|
|
|
}
|
|
|
|
else
|
2007-05-27 00:37:57 +02:00
|
|
|
{
|
2011-03-28 19:43:34 +02:00
|
|
|
ret = ENOTDIR;
|
|
|
|
goto errout_with_direntry;
|
2007-05-27 00:37:57 +02:00
|
|
|
}
|
2007-05-26 18:05:59 +02:00
|
|
|
}
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2007-05-27 00:37:57 +02:00
|
|
|
inode_semgive();
|
2015-10-11 19:39:29 +02:00
|
|
|
return ((FAR DIR *)dir);
|
2007-05-26 18:05:59 +02:00
|
|
|
|
|
|
|
/* Nasty goto's make error handling simpler */
|
|
|
|
|
2007-05-27 00:37:57 +02:00
|
|
|
errout_with_direntry:
|
2014-09-01 00:15:11 +02:00
|
|
|
kumm_free(dir);
|
2007-12-02 19:18:59 +01:00
|
|
|
|
2007-05-27 00:37:57 +02:00
|
|
|
errout_with_semaphore:
|
|
|
|
inode_semgive();
|
2012-07-14 23:05:40 +02:00
|
|
|
set_errno(ret);
|
2007-05-26 18:05:59 +02:00
|
|
|
return NULL;
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|