FS: Separate inode_search() and inode_free() from fs_inode.c and put in separate files. Flesh out symbolic link logic in stat() and readdir(). There are still some issues with stat().
This commit is contained in:
parent
5f4a26fee6
commit
7c59e05305
@ -1,7 +1,7 @@
|
||||
/****************************************************************************
|
||||
* fs/dirent/fs_readdir.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Copyright (C) 2007-2009, 2011, 2017 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@ -88,11 +88,18 @@ static inline int readpseudodir(struct fs_dirent_s *idir)
|
||||
{
|
||||
idir->fd_dir.d_type |= DTYPE_BLK;
|
||||
}
|
||||
if (INODE_IS_MOUNTPT(idir->u.pseudo.fd_next))
|
||||
else if (INODE_IS_MOUNTPT(idir->u.pseudo.fd_next))
|
||||
{
|
||||
idir->fd_dir.d_type |= DTYPE_DIRECTORY;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
if (INODE_IS_SOFTLINK(idir->u.pseudo.fd_next))
|
||||
{
|
||||
idir->fd_dir.d_type |= DTYPE_LINK;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
idir->fd_dir.d_type |= DTYPE_CHR;
|
||||
|
@ -1,7 +1,7 @@
|
||||
############################################################################
|
||||
# fs/inode/Make.defs
|
||||
#
|
||||
# Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
||||
# Copyright (C) 2014, 2017 Gregory Nutt. All rights reserved.
|
||||
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
@ -38,8 +38,9 @@
|
||||
ifneq ($(CONFIG_NFILE_DESCRIPTORS),0)
|
||||
|
||||
CSRCS += fs_files.c fs_foreachinode.c fs_inode.c fs_inodeaddref.c
|
||||
CSRCS += fs_inodebasename.c fs_inodefind.c fs_inoderelease.c
|
||||
CSRCS += fs_inoderemove.c fs_inodereserve.c fs_filedetach.c
|
||||
CSRCS += fs_inodebasename.c fs_inodefind.c fs_inodefree.c fs_inoderelease.c
|
||||
CSRCS += fs_inoderemove.c fs_inodereserve.c fs_inodesearch.c
|
||||
CSRCS += fs_filedetach.c
|
||||
|
||||
# Include inode/utils build support
|
||||
|
||||
|
@ -39,13 +39,11 @@
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <unistd.h>
|
||||
#include <semaphore.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/kmalloc.h>
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
#include "inode/inode.h"
|
||||
@ -81,139 +79,6 @@ struct inode_sem_s
|
||||
|
||||
static struct inode_sem_s g_inode_sem;
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
FAR struct inode *g_root_inode = NULL;
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: _inode_compare
|
||||
*
|
||||
* Description:
|
||||
* Compare two inode names
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static int _inode_compare(FAR const char *fname,
|
||||
FAR struct inode *node)
|
||||
{
|
||||
char *nname = node->i_name;
|
||||
|
||||
if (!nname)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!fname)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (; ; )
|
||||
{
|
||||
/* At end of node name? */
|
||||
|
||||
if (!*nname)
|
||||
{
|
||||
/* Yes.. also end of find name? */
|
||||
|
||||
if (!*fname || *fname == '/')
|
||||
{
|
||||
/* Yes.. return match */
|
||||
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* No... return find name > node name */
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* At end of find name? */
|
||||
|
||||
else if (!*fname || *fname == '/')
|
||||
{
|
||||
/* Yes... return find name < node name */
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Check for non-matching characters */
|
||||
|
||||
else if (*fname > *nname)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (*fname < *nname)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Not at the end of either string and all of the
|
||||
* characters still match. keep looking.
|
||||
*/
|
||||
|
||||
else
|
||||
{
|
||||
fname++;
|
||||
nname++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: _inode_dereference
|
||||
*
|
||||
* Description:
|
||||
* If the inode is a soft link, then (1) get the name of the full path of
|
||||
* the soft link, (2) recursively look-up the inode referenced by the soft
|
||||
* link, and (3) return the inode referenced by the soft link.
|
||||
*
|
||||
* Assumptions:
|
||||
* The caller holds the g_inode_sem semaphore
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
static inline FAR struct inode *
|
||||
_inode_dereference(FAR struct inode *node, FAR struct inode **peer,
|
||||
FAR struct inode **parent, FAR const char **relpath)
|
||||
{
|
||||
FAR const char *copy;
|
||||
unsigned int count = 0;
|
||||
|
||||
/* An infinite loop is avoided only by the loop count.
|
||||
*
|
||||
* REVISIT: The ELOOP error should be reported to the application in that
|
||||
* case but there is no simple mechanism to do that.
|
||||
*/
|
||||
|
||||
while (node != NULL && INODE_IS_SOFTLINK(node))
|
||||
{
|
||||
/* Careful: inode_search_nofollow overwrites the input string pointer */
|
||||
|
||||
copy = (FAR const char *)node->u.i_link;
|
||||
|
||||
/* Now, look-up the inode associated with the target path */
|
||||
|
||||
node = inode_search_nofollow(©, peer, parent, relpath);
|
||||
if (node == NULL && ++count > SYMLOOP_MAX)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
@ -320,337 +185,3 @@ void inode_semgive(void)
|
||||
sem_post(&g_inode_sem.sem);
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: inode_search and inode_search_nofollow
|
||||
*
|
||||
* Description:
|
||||
* Find the inode associated with 'path' returning the inode references
|
||||
* and references to its companion nodes.
|
||||
*
|
||||
* Both versions will follow soft links in path leading up to the terminal
|
||||
* node. inode_search() will deference that terminal node,
|
||||
* inode_search_nofollow will not.
|
||||
*
|
||||
* Assumptions:
|
||||
* The caller holds the g_inode_sem semaphore
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
|
||||
FAR struct inode *inode_search_nofollow(FAR const char **path,
|
||||
FAR struct inode **peer,
|
||||
FAR struct inode **parent,
|
||||
FAR const char **relpath)
|
||||
#else
|
||||
FAR struct inode *inode_search(FAR const char **path,
|
||||
FAR struct inode **peer,
|
||||
FAR struct inode **parent,
|
||||
FAR const char **relpath)
|
||||
#endif
|
||||
{
|
||||
FAR const char *name = *path + 1; /* Skip over leading '/' */
|
||||
FAR struct inode *node = g_root_inode;
|
||||
FAR struct inode *left = NULL;
|
||||
FAR struct inode *above = NULL;
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
FAR struct inode *newnode;
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
/* Handle the case were the root node is a symbolic link */
|
||||
|
||||
#warning Missing logic
|
||||
#endif
|
||||
|
||||
/* Traverse the pseudo file system node tree until either (1) all nodes
|
||||
* have been examined without finding the matching node, or (2) the
|
||||
* matching node is found.
|
||||
*/
|
||||
|
||||
while (node != NULL)
|
||||
{
|
||||
int result = _inode_compare(name, node);
|
||||
|
||||
/* Case 1: The name is less than the name of the node.
|
||||
* Since the names are ordered, these means that there
|
||||
* is no peer node with this name and that there can be
|
||||
* no match in the fileystem.
|
||||
*/
|
||||
|
||||
if (result < 0)
|
||||
{
|
||||
node = NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Case 2: the name is greater than the name of the node.
|
||||
* In this case, the name may still be in the list to the
|
||||
* "right"
|
||||
*/
|
||||
|
||||
else if (result > 0)
|
||||
{
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
/* If the inode in the is a soft link and this is the inode at
|
||||
* at the head of the peer list and not the final node in the
|
||||
* path), then (1) get the name of the full path of the soft
|
||||
* link, (2) recursively look-up the inode referenced by the
|
||||
* soft link, and (3) use the peer of that inode instead.
|
||||
*/
|
||||
|
||||
FAR const char *nextname = inode_nextname(name);
|
||||
if (*nextname != '\0')
|
||||
{
|
||||
newnode = _inode_dereference(node, NULL, &above, relpath);
|
||||
if (newnode == NULL)
|
||||
{
|
||||
/* Probably means that the node is a symbolic link, but
|
||||
* that the target of the symbolic link does not exist.
|
||||
*/
|
||||
|
||||
break;
|
||||
}
|
||||
else if (newnode != node)
|
||||
{
|
||||
/* The node was a valid symbolic link and we have jumped to a
|
||||
* different, spot in the the pseudo file system tree. Reset
|
||||
* everything and continue looking at the next level "down"
|
||||
* from that new spot in the tree.
|
||||
*/
|
||||
|
||||
above = newnode;
|
||||
left = NULL;
|
||||
node = newnode->i_child;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
/* Continue looking to the left */
|
||||
|
||||
left = node;
|
||||
node = node->i_peer;
|
||||
}
|
||||
|
||||
/* The names match */
|
||||
|
||||
else
|
||||
{
|
||||
/* Now there are three remaining possibilities:
|
||||
* (1) This is the node that we are looking for.
|
||||
* (2) The node we are looking for is "below" this one.
|
||||
* (3) This node is a mountpoint and will absorb all request
|
||||
* below this one
|
||||
*/
|
||||
|
||||
name = inode_nextname(name);
|
||||
if (*name == '\0' || INODE_IS_MOUNTPT(node))
|
||||
{
|
||||
/* Either (1) we are at the end of the path, so this must be the
|
||||
* node we are looking for or else (2) this node is a mountpoint
|
||||
* and will handle the remaining part of the pathname
|
||||
*/
|
||||
|
||||
if (relpath)
|
||||
{
|
||||
*relpath = name;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
/* NOTE that if the terminal inode is a soft link, it is not
|
||||
* deferenced in this case. The raw inode is returned.
|
||||
*
|
||||
* In that case a wrapper function will perform that operation.
|
||||
*/
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* More nodes to be examined in the path... */
|
||||
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
/* If this intermediate inode in the is a soft link, then (1)
|
||||
* get the name of the full path of the soft link, (2) recursively
|
||||
* look-up the inode referenced by the sof link, and (3)
|
||||
* continue searching with that inode instead.
|
||||
*/
|
||||
|
||||
newnode = _inode_dereference(node, NULL, NULL, relpath);
|
||||
if (newnode == NULL)
|
||||
{
|
||||
/* Probably means that the node is a symbolic link, but
|
||||
* that the target of the symbolic link does not exist.
|
||||
*/
|
||||
|
||||
break;
|
||||
}
|
||||
else if (newnode != node)
|
||||
{
|
||||
/* The node was a valid symbolic link and we have jumped to a
|
||||
* different, spot in the the pseudo file system tree. Reset
|
||||
* everything and continue looking to the right (if possible)
|
||||
* otherwise at the next level "down" from that new spot in
|
||||
* the tree.
|
||||
*/
|
||||
|
||||
if (newnode->i_peer != NULL)
|
||||
{
|
||||
above = NULL; /* REVISIT: This can't be right */
|
||||
left = newnode;
|
||||
node = newnode->i_peer;
|
||||
|
||||
/* Did the symbolic link take us to a mountpoint? */
|
||||
|
||||
if (INODE_IS_MOUNTPT(newnode))
|
||||
{
|
||||
/* Yes.. return the mountpoint information */
|
||||
|
||||
if (relpath)
|
||||
{
|
||||
*relpath = name;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
/* Keep looking at the next level "down" */
|
||||
|
||||
above = node;
|
||||
left = NULL;
|
||||
node = node->i_child;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* node is null. This can happen in one of four cases:
|
||||
* With node = NULL
|
||||
* (1) We went left past the final peer: The new node
|
||||
* name is larger than any existing node name at
|
||||
* that level.
|
||||
* (2) We broke out in the middle of the list of peers
|
||||
* because the name was not found in the ordered
|
||||
* list.
|
||||
* (3) We went down past the final parent: The new node
|
||||
* name is "deeper" than anything that we currently
|
||||
* have in the tree.
|
||||
* with node != NULL
|
||||
* (4) When the node matching the full path is found
|
||||
*/
|
||||
|
||||
if (peer)
|
||||
{
|
||||
*peer = left;
|
||||
}
|
||||
|
||||
if (parent)
|
||||
{
|
||||
*parent = above;
|
||||
}
|
||||
|
||||
*path = name;
|
||||
return node;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
FAR struct inode *inode_search(FAR const char **path,
|
||||
FAR struct inode **peer,
|
||||
FAR struct inode **parent,
|
||||
FAR const char **relpath)
|
||||
{
|
||||
/* Lookup the terminal inode */
|
||||
|
||||
FAR struct inode *node = inode_search_nofollow(path, peer, parent, relpath);
|
||||
|
||||
/* Did we find it? */
|
||||
|
||||
if (node != NULL)
|
||||
{
|
||||
/* Yes.. If the terminal inode in the is a soft link, then (1) get
|
||||
* the name of the full path of the soft link, (2) recursively
|
||||
* look-up the inode referenced by the soft link, and (3)
|
||||
* return that inode instead.
|
||||
*/
|
||||
|
||||
return _inode_dereference(node, peer, parent, relpath);
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: inode_free
|
||||
*
|
||||
* Description:
|
||||
* Free resources used by an inode
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void inode_free(FAR struct inode *node)
|
||||
{
|
||||
/* Verify that we were passed valid pointer to an inode */
|
||||
|
||||
if (node != NULL)
|
||||
{
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
/* Symbol links should never have peers or children */
|
||||
|
||||
DEBUGASSERT(!INODE_IS_SOFTLINK(node) ||
|
||||
(node->i_peer == NULL && node->i_child == NULL));
|
||||
#endif
|
||||
|
||||
/* Free all peers and children of this i_node */
|
||||
|
||||
inode_free(node->i_peer);
|
||||
inode_free(node->i_child);
|
||||
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
/* If the inode is a symbolic link, the free the path to the linked
|
||||
* entity.
|
||||
*/
|
||||
|
||||
if (INODE_IS_SOFTLINK(node) && node->u.i_link != NULL)
|
||||
{
|
||||
kmm_free(node->u.i_link);
|
||||
}
|
||||
#endif
|
||||
|
||||
kmm_free(node);
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: inode_nextname
|
||||
*
|
||||
* Description:
|
||||
* Given a path with node names separated by '/', return the next path
|
||||
* segment name.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR const char *inode_nextname(FAR const char *name)
|
||||
{
|
||||
/* Search for the '/' delimiter or the NUL terminator at the end of the
|
||||
* path segment.
|
||||
*/
|
||||
|
||||
while (*name && *name != '/')
|
||||
{
|
||||
name++;
|
||||
}
|
||||
|
||||
/* If we found the '/' delimiter, then the path segment we want begins at
|
||||
* the next character (which might also be the NUL terminator).
|
||||
*/
|
||||
|
||||
if (*name)
|
||||
{
|
||||
name++;
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
93
fs/inode/fs_inodefree.c
Normal file
93
fs/inode/fs_inodefree.c
Normal file
@ -0,0 +1,93 @@
|
||||
/****************************************************************************
|
||||
* fs/inode/fs_inodefree.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012, 2016-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 <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/kmalloc.h>
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
#include "inode/inode.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: inode_free
|
||||
*
|
||||
* Description:
|
||||
* Free resources used by an inode
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void inode_free(FAR struct inode *node)
|
||||
{
|
||||
/* Verify that we were passed valid pointer to an inode */
|
||||
|
||||
if (node != NULL)
|
||||
{
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
/* Symbol links should never have peers or children */
|
||||
|
||||
DEBUGASSERT(!INODE_IS_SOFTLINK(node) ||
|
||||
(node->i_peer == NULL && node->i_child == NULL));
|
||||
#endif
|
||||
|
||||
/* Free all peers and children of this i_node */
|
||||
|
||||
inode_free(node->i_peer);
|
||||
inode_free(node->i_child);
|
||||
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
/* If the inode is a symbolic link, the free the path to the linked
|
||||
* entity.
|
||||
*/
|
||||
|
||||
if (INODE_IS_SOFTLINK(node) && node->u.i_link != NULL)
|
||||
{
|
||||
kmm_free(node->u.i_link);
|
||||
}
|
||||
#endif
|
||||
|
||||
kmm_free(node);
|
||||
}
|
||||
}
|
478
fs/inode/fs_inodesearch.c
Normal file
478
fs/inode/fs_inodesearch.c
Normal file
@ -0,0 +1,478 @@
|
||||
/****************************************************************************
|
||||
* fs/inode/fs_inodesearch.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011-2012, 2016-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 <limits.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
#include "inode/inode.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
FAR struct inode *g_root_inode = NULL;
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: _inode_compare
|
||||
*
|
||||
* Description:
|
||||
* Compare two inode names
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static int _inode_compare(FAR const char *fname,
|
||||
FAR struct inode *node)
|
||||
{
|
||||
char *nname = node->i_name;
|
||||
|
||||
if (!nname)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!fname)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (; ; )
|
||||
{
|
||||
/* At end of node name? */
|
||||
|
||||
if (!*nname)
|
||||
{
|
||||
/* Yes.. also end of find name? */
|
||||
|
||||
if (!*fname || *fname == '/')
|
||||
{
|
||||
/* Yes.. return match */
|
||||
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* No... return find name > node name */
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* At end of find name? */
|
||||
|
||||
else if (!*fname || *fname == '/')
|
||||
{
|
||||
/* Yes... return find name < node name */
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Check for non-matching characters */
|
||||
|
||||
else if (*fname > *nname)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (*fname < *nname)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Not at the end of either string and all of the
|
||||
* characters still match. keep looking.
|
||||
*/
|
||||
|
||||
else
|
||||
{
|
||||
fname++;
|
||||
nname++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: _inode_dereference
|
||||
*
|
||||
* Description:
|
||||
* If the inode is a soft link, then (1) get the name of the full path of
|
||||
* the soft link, (2) recursively look-up the inode referenced by the soft
|
||||
* link, and (3) return the inode referenced by the soft link.
|
||||
*
|
||||
* Assumptions:
|
||||
* The caller holds the g_inode_sem semaphore
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
static inline FAR struct inode *
|
||||
_inode_dereference(FAR struct inode *node, FAR struct inode **peer,
|
||||
FAR struct inode **parent, FAR const char **relpath)
|
||||
{
|
||||
FAR const char *copy;
|
||||
unsigned int count = 0;
|
||||
|
||||
/* An infinite loop is avoided only by the loop count.
|
||||
*
|
||||
* REVISIT: The ELOOP error should be reported to the application in that
|
||||
* case but there is no simple mechanism to do that.
|
||||
*/
|
||||
|
||||
while (node != NULL && INODE_IS_SOFTLINK(node))
|
||||
{
|
||||
/* Careful: inode_search_nofollow overwrites the input string pointer */
|
||||
|
||||
copy = (FAR const char *)node->u.i_link;
|
||||
|
||||
/* Now, look-up the inode associated with the target path */
|
||||
|
||||
node = inode_search_nofollow(©, peer, parent, relpath);
|
||||
if (node == NULL && ++count > SYMLOOP_MAX)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: inode_search and inode_search_nofollow
|
||||
*
|
||||
* Description:
|
||||
* Find the inode associated with 'path' returning the inode references
|
||||
* and references to its companion nodes.
|
||||
*
|
||||
* Both versions will follow soft links in path leading up to the terminal
|
||||
* node. inode_search() will deference that terminal node,
|
||||
* inode_search_nofollow will not.
|
||||
*
|
||||
* Assumptions:
|
||||
* The caller holds the g_inode_sem semaphore
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
|
||||
FAR struct inode *inode_search_nofollow(FAR const char **path,
|
||||
FAR struct inode **peer,
|
||||
FAR struct inode **parent,
|
||||
FAR const char **relpath)
|
||||
#else
|
||||
FAR struct inode *inode_search(FAR const char **path,
|
||||
FAR struct inode **peer,
|
||||
FAR struct inode **parent,
|
||||
FAR const char **relpath)
|
||||
#endif
|
||||
{
|
||||
FAR const char *name = *path + 1; /* Skip over leading '/' */
|
||||
FAR struct inode *node = g_root_inode;
|
||||
FAR struct inode *left = NULL;
|
||||
FAR struct inode *above = NULL;
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
FAR struct inode *newnode;
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
/* Handle the case were the root node is a symbolic link */
|
||||
|
||||
#warning Missing logic
|
||||
#endif
|
||||
|
||||
/* Traverse the pseudo file system node tree until either (1) all nodes
|
||||
* have been examined without finding the matching node, or (2) the
|
||||
* matching node is found.
|
||||
*/
|
||||
|
||||
while (node != NULL)
|
||||
{
|
||||
int result = _inode_compare(name, node);
|
||||
|
||||
/* Case 1: The name is less than the name of the node.
|
||||
* Since the names are ordered, these means that there
|
||||
* is no peer node with this name and that there can be
|
||||
* no match in the fileystem.
|
||||
*/
|
||||
|
||||
if (result < 0)
|
||||
{
|
||||
node = NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Case 2: the name is greater than the name of the node.
|
||||
* In this case, the name may still be in the list to the
|
||||
* "right"
|
||||
*/
|
||||
|
||||
else if (result > 0)
|
||||
{
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
/* If the inode in the is a soft link and this is the inode at
|
||||
* at the head of the peer list and not the final node in the
|
||||
* path), then (1) get the name of the full path of the soft
|
||||
* link, (2) recursively look-up the inode referenced by the
|
||||
* soft link, and (3) use the peer of that inode instead.
|
||||
*/
|
||||
|
||||
FAR const char *nextname = inode_nextname(name);
|
||||
if (*nextname != '\0')
|
||||
{
|
||||
newnode = _inode_dereference(node, NULL, &above, relpath);
|
||||
if (newnode == NULL)
|
||||
{
|
||||
/* Probably means that the node is a symbolic link, but
|
||||
* that the target of the symbolic link does not exist.
|
||||
*/
|
||||
|
||||
break;
|
||||
}
|
||||
else if (newnode != node)
|
||||
{
|
||||
/* The node was a valid symbolic link and we have jumped to a
|
||||
* different, spot in the the pseudo file system tree. Reset
|
||||
* everything and continue looking at the next level "down"
|
||||
* from that new spot in the tree.
|
||||
*/
|
||||
|
||||
above = newnode;
|
||||
left = NULL;
|
||||
node = newnode->i_child;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
/* Continue looking to the left */
|
||||
|
||||
left = node;
|
||||
node = node->i_peer;
|
||||
}
|
||||
|
||||
/* The names match */
|
||||
|
||||
else
|
||||
{
|
||||
/* Now there are three remaining possibilities:
|
||||
* (1) This is the node that we are looking for.
|
||||
* (2) The node we are looking for is "below" this one.
|
||||
* (3) This node is a mountpoint and will absorb all request
|
||||
* below this one
|
||||
*/
|
||||
|
||||
name = inode_nextname(name);
|
||||
if (*name == '\0' || INODE_IS_MOUNTPT(node))
|
||||
{
|
||||
/* Either (1) we are at the end of the path, so this must be the
|
||||
* node we are looking for or else (2) this node is a mountpoint
|
||||
* and will handle the remaining part of the pathname
|
||||
*/
|
||||
|
||||
if (relpath)
|
||||
{
|
||||
*relpath = name;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
/* NOTE that if the terminal inode is a soft link, it is not
|
||||
* deferenced in this case. The raw inode is returned.
|
||||
*
|
||||
* In that case a wrapper function will perform that operation.
|
||||
*/
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* More nodes to be examined in the path... */
|
||||
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
/* If this intermediate inode in the is a soft link, then (1)
|
||||
* get the name of the full path of the soft link, (2) recursively
|
||||
* look-up the inode referenced by the sof link, and (3)
|
||||
* continue searching with that inode instead.
|
||||
*/
|
||||
|
||||
newnode = _inode_dereference(node, NULL, NULL, relpath);
|
||||
if (newnode == NULL)
|
||||
{
|
||||
/* Probably means that the node is a symbolic link, but
|
||||
* that the target of the symbolic link does not exist.
|
||||
*/
|
||||
|
||||
break;
|
||||
}
|
||||
else if (newnode != node)
|
||||
{
|
||||
/* The node was a valid symbolic link and we have jumped to a
|
||||
* different, spot in the the pseudo file system tree. Reset
|
||||
* everything and continue looking to the right (if possible)
|
||||
* otherwise at the next level "down" from that new spot in
|
||||
* the tree.
|
||||
*/
|
||||
|
||||
if (newnode->i_peer != NULL)
|
||||
{
|
||||
above = NULL; /* REVISIT: This can't be right */
|
||||
left = newnode;
|
||||
node = newnode->i_peer;
|
||||
|
||||
/* Did the symbolic link take us to a mountpoint? */
|
||||
|
||||
if (INODE_IS_MOUNTPT(newnode))
|
||||
{
|
||||
/* Yes.. return the mountpoint information */
|
||||
|
||||
if (relpath)
|
||||
{
|
||||
*relpath = name;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
/* Keep looking at the next level "down" */
|
||||
|
||||
above = node;
|
||||
left = NULL;
|
||||
node = node->i_child;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* node is null. This can happen in one of four cases:
|
||||
* With node = NULL
|
||||
* (1) We went left past the final peer: The new node
|
||||
* name is larger than any existing node name at
|
||||
* that level.
|
||||
* (2) We broke out in the middle of the list of peers
|
||||
* because the name was not found in the ordered
|
||||
* list.
|
||||
* (3) We went down past the final parent: The new node
|
||||
* name is "deeper" than anything that we currently
|
||||
* have in the tree.
|
||||
* with node != NULL
|
||||
* (4) When the node matching the full path is found
|
||||
*/
|
||||
|
||||
if (peer)
|
||||
{
|
||||
*peer = left;
|
||||
}
|
||||
|
||||
if (parent)
|
||||
{
|
||||
*parent = above;
|
||||
}
|
||||
|
||||
*path = name;
|
||||
return node;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
||||
FAR struct inode *inode_search(FAR const char **path,
|
||||
FAR struct inode **peer,
|
||||
FAR struct inode **parent,
|
||||
FAR const char **relpath)
|
||||
{
|
||||
/* Lookup the terminal inode */
|
||||
|
||||
FAR struct inode *node = inode_search_nofollow(path, peer, parent, relpath);
|
||||
|
||||
/* Did we find it? */
|
||||
|
||||
if (node != NULL)
|
||||
{
|
||||
/* Yes.. If the terminal inode in the is a soft link, then (1) get
|
||||
* the name of the full path of the soft link, (2) recursively
|
||||
* look-up the inode referenced by the soft link, and (3)
|
||||
* return that inode instead.
|
||||
*/
|
||||
|
||||
return _inode_dereference(node, peer, parent, relpath);
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: inode_nextname
|
||||
*
|
||||
* Description:
|
||||
* Given a path with node names separated by '/', return the next path
|
||||
* segment name.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR const char *inode_nextname(FAR const char *name)
|
||||
{
|
||||
/* Search for the '/' delimiter or the NUL terminator at the end of the
|
||||
* path segment.
|
||||
*/
|
||||
|
||||
while (*name && *name != '/')
|
||||
{
|
||||
name++;
|
||||
}
|
||||
|
||||
/* If we found the '/' delimiter, then the path segment we want begins at
|
||||
* the next character (which might also be the NUL terminator).
|
||||
*/
|
||||
|
||||
if (*name)
|
||||
{
|
||||
name++;
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
/****************************************************************************
|
||||
* fs/vfs/fs_stat.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2012 Gregory Nutt. All rights reserved.
|
||||
* Copyright (C) 2007-2009, 2012, 2017 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@ -104,10 +104,18 @@ static inline int statpseudo(FAR struct inode *inode, FAR struct stat *buf)
|
||||
}
|
||||
else if (INODE_IS_BLOCK(inode))
|
||||
{
|
||||
/* What is it also has child inodes? */
|
||||
/* What is if also has child inodes? */
|
||||
|
||||
buf->st_mode |= S_IFBLK;
|
||||
}
|
||||
#if 0 /* ifdef CONFIG_PSEUDOFS_SOFTLINKS See REVISIT in stat() */
|
||||
else if (INODE_IS_SOFTLINK(inode))
|
||||
{
|
||||
/* Should not have child inodes. */
|
||||
|
||||
buf->st_mode |= S_IFLNK;
|
||||
}
|
||||
#endif
|
||||
else /* if (INODE_IS_DRIVER(inode)) */
|
||||
{
|
||||
/* What is it if it also has child inodes? */
|
||||
@ -191,7 +199,11 @@ int stat(FAR const char *path, FAR struct stat *buf)
|
||||
|
||||
/* Get an inode for this file */
|
||||
|
||||
#if 0 /* REVISIT: inode_find_nofollow needed for symlinks but conflicts with other usage */
|
||||
inode = inode_find_nofollow(path, &relpath);
|
||||
#else
|
||||
inode = inode_find(path, &relpath);
|
||||
#endif
|
||||
if (!inode)
|
||||
{
|
||||
/* This name does not refer to a psudeo-inode and there is no
|
||||
|
@ -55,15 +55,18 @@
|
||||
* of NuttX, an inode can be BOTH a file and a directory
|
||||
*/
|
||||
|
||||
#define DTYPE_FILE 0x01
|
||||
#define DTYPE_CHR 0x02
|
||||
#define DTYPE_BLK 0x04
|
||||
#define DTYPE_DIRECTORY 0x08
|
||||
#define DTYPE_UNKNOWN 0 /* The file type could not be determined */
|
||||
#define DTYPE_FILE (1 << 0) /* Bit 0: Regular file */
|
||||
#define DTYPE_CHR (1 << 1) /* Bit 1: Character device */
|
||||
#define DTYPE_BLK (1 << 2) /* Bit 2: Block device */
|
||||
#define DTYPE_DIRECTORY (1 << 3) /* Bit 3: Directory */
|
||||
#define DTYPE_LINK (1 << 4) /* Bit 4: Symbolic link */
|
||||
|
||||
#define DIRENT_ISFILE(dtype) (((dtype) & DTYPE_FILE) != 0 )
|
||||
#define DIRENT_ISCHR(dtype) (((dtype) & DTYPE_CHR) != 0 )
|
||||
#define DIRENT_ISBLK(dtype) (((dtype) & DTYPE_BLK) != 0 )
|
||||
#define DIRENT_ISDIRECTORY(dtype) (((dtype) & DTYPE_DIRECTORY) != 0 )
|
||||
#define DIRENT_ISLINK(dtype) (((dtype) & DTYPE_LINK) != 0 )
|
||||
|
||||
/****************************************************************************
|
||||
* Public Type Definitions
|
||||
|
Loading…
Reference in New Issue
Block a user