487 lines
14 KiB
C
487 lines
14 KiB
C
/****************************************************************************
|
|
* 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 <string.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_linktarget
|
|
*
|
|
* 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 int inode_linktarget(FAR struct inode *node,
|
|
FAR struct inode_search_s *desc)
|
|
{
|
|
unsigned int count = 0;
|
|
int ret = -ENOENT;
|
|
|
|
DEBUGASSERT(desc != NULL && node != NULL);
|
|
|
|
/* 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 (INODE_IS_SOFTLINK(node))
|
|
{
|
|
/* Now, look-up the inode associated with the target path */
|
|
|
|
memset(desc, 0, sizeof(struct inode_search_s));
|
|
desc->path = (FAR const char *)node->u.i_link;
|
|
|
|
/* Look up the target of the symbolic link */
|
|
|
|
ret = inode_search_nofollow(desc);
|
|
if (ret < 0)
|
|
{
|
|
break;
|
|
}
|
|
|
|
/* Limit the number of symbolic links that we pass through */
|
|
|
|
if (++count > SYMLOOP_MAX)
|
|
{
|
|
ret = -ELOOP;
|
|
break;
|
|
}
|
|
|
|
/* Set up for the next time through the loop */
|
|
|
|
node = desc->node;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
#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.
|
|
*
|
|
* If a soft link is encountered that is not the terminal node in the path,
|
|
* that that WILL be deferenced and the mountpoint inode will be returned.
|
|
*
|
|
* Assumptions:
|
|
* The caller holds the g_inode_sem semaphore
|
|
*
|
|
****************************************************************************/
|
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
int inode_search_nofollow(FAR struct inode_search_s *desc)
|
|
#else
|
|
int inode_search(FAR struct inode_search_s *desc)
|
|
#endif
|
|
{
|
|
FAR const char *name;
|
|
FAR struct inode *node = g_root_inode;
|
|
FAR struct inode *left = NULL;
|
|
FAR struct inode *above = NULL;
|
|
FAR const char *relpath = NULL;
|
|
int ret = -ENOENT;
|
|
|
|
/* Get the search path, skipping over the leading '/'. The leading '/' is
|
|
* mandatory because only absolte paths are expected in this context.
|
|
*/
|
|
|
|
DEBUGASSERT(desc != NULL && desc->path != NULL);
|
|
name = desc->path;
|
|
|
|
DEBUGASSERT(*name == '/');
|
|
if (*name != '/')
|
|
{
|
|
return -EINVAL;
|
|
}
|
|
|
|
name++; /* Skip over the leading '/' */
|
|
|
|
/* 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)
|
|
{
|
|
/* Continue looking to the "right" of this inode. */
|
|
|
|
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
|
|
*/
|
|
|
|
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
|
|
ret = OK;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
/* More nodes to be examined in the path "below" this one. */
|
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
/* Was the node a soft link? If so, then we need need to
|
|
* continue below the target of the link, not the link itself.
|
|
*/
|
|
|
|
if (INODE_IS_SOFTLINK(node))
|
|
{
|
|
int status;
|
|
|
|
/* 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 soft
|
|
* link, and (3) continue searching with that inode instead.
|
|
*/
|
|
|
|
status = inode_linktarget(node, desc);
|
|
if (status < 0)
|
|
{
|
|
/* Probably means that the the target of the symbolic
|
|
* link does not exist.
|
|
*/
|
|
|
|
ret = status;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
FAR struct inode *newnode = desc->node;
|
|
|
|
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.
|
|
*
|
|
* Continue from this new inode.
|
|
*/
|
|
|
|
node = newnode;
|
|
|
|
/* Check if this took us to a mountpoint. */
|
|
|
|
if (INODE_IS_MOUNTPT(node))
|
|
{
|
|
/* Yes.. return the mountpoint information.
|
|
* REVISIT: The relpath is incorrect in this case.
|
|
* It is relative to symbolic link, not to the
|
|
* root of the mount.
|
|
*/
|
|
|
|
relpath = name;
|
|
above = NULL;
|
|
left = NULL;
|
|
|
|
ret = OK;
|
|
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
|
|
*/
|
|
|
|
desc->path = name;
|
|
desc->node = node;
|
|
desc->peer = left;
|
|
desc->parent = above;
|
|
desc->relpath = relpath;
|
|
return ret;
|
|
}
|
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
int inode_search(FAR struct inode_search_s *desc)
|
|
{
|
|
int ret;
|
|
|
|
/* Lookup the terminal inode */
|
|
|
|
ret = inode_search_nofollow(desc);
|
|
|
|
/* Did we find it? inode_search_nofollow() will terminate in one of
|
|
* three ways:
|
|
*
|
|
* 1. With an error (node == NULL)
|
|
* 2. With node referring to the terminal inode which may be a symbolic
|
|
* link, or
|
|
* 3. with node referring to an intermediate MOUNTPOINT inode with the
|
|
* residual path in relpath.
|
|
*
|
|
* REVISIT: The relpath is incorrect in the final case. It will be relative
|
|
* to symbolic link, not to the root of the mount.
|
|
*/
|
|
|
|
if (ret >= 0)
|
|
{
|
|
/* Sucessfully found inode */
|
|
|
|
FAR struct inode *node = desc->node;
|
|
DEBUGASSERT(node != NULL);
|
|
|
|
/* Is the terminal node a softlink? */
|
|
|
|
if (INODE_IS_SOFTLINK(node))
|
|
{
|
|
/* The terminating inode is a valid soft link: Return the inode,
|
|
* corresponding to link target.
|
|
*/
|
|
|
|
ret = inode_linktarget(node, desc);
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
#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;
|
|
}
|