2007-05-09 08:04:03 +08:00
|
|
|
/****************************************************************************
|
2014-09-29 01:06:21 +08:00
|
|
|
* fs/inode/fs_inoderemove.c
|
2007-03-15 06:41:09 +08:00
|
|
|
*
|
2009-12-14 02:01:46 +08:00
|
|
|
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
|
2012-07-15 05:05:40 +08:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-03-15 06:41:09 +08: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 22:46:44 +08:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-03-15 06:41:09 +08: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.
|
|
|
|
*
|
2007-05-09 08:04:03 +08:00
|
|
|
****************************************************************************/
|
2007-03-15 06:41:09 +08:00
|
|
|
|
2007-05-09 08:04:03 +08:00
|
|
|
/****************************************************************************
|
2007-03-15 06:41:09 +08:00
|
|
|
* Included Files
|
2007-05-09 08:04:03 +08:00
|
|
|
****************************************************************************/
|
2007-03-15 06:41:09 +08:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-15 08:18:32 +08:00
|
|
|
|
2007-03-15 06:41:09 +08:00
|
|
|
#include <errno.h>
|
2011-04-06 04:54:00 +08:00
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2012-03-22 02:01:07 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
2009-12-15 08:18:32 +08:00
|
|
|
|
2014-09-29 21:14:38 +08:00
|
|
|
#include "inode/inode.h"
|
2007-03-15 06:41:09 +08:00
|
|
|
|
2014-02-20 03:14:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-05-09 08:04:03 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: inode_unlink
|
2014-02-20 03:14:39 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Given a path, remove a the node from the in-memory, inode tree that the
|
|
|
|
* path refers to. This is normally done in preparation to removing or
|
|
|
|
* moving an inode.
|
|
|
|
*
|
2017-02-03 09:37:58 +08:00
|
|
|
* In symbolic links in the pseduo file system are enabled, then this
|
|
|
|
* logic will follow the symbolic links up until the terminal node. Then
|
|
|
|
* that link in removed. So if this the terminal node is a symbolic link,
|
|
|
|
* the symbolic link node will be removed, not the target of the link.
|
|
|
|
*
|
2014-02-20 03:14:39 +08:00
|
|
|
* Assumptions/Limitations:
|
|
|
|
* The caller must hold the inode semaphore
|
|
|
|
*
|
2007-05-09 08:04:03 +08:00
|
|
|
****************************************************************************/
|
2007-03-15 06:41:09 +08:00
|
|
|
|
2014-02-20 03:14:39 +08:00
|
|
|
FAR struct inode *inode_unlink(FAR const char *path)
|
2007-03-15 06:41:09 +08:00
|
|
|
{
|
2014-02-20 03:14:39 +08:00
|
|
|
const char *name = path;
|
|
|
|
FAR struct inode *node;
|
|
|
|
FAR struct inode *peer;
|
|
|
|
FAR struct inode *parent;
|
|
|
|
|
|
|
|
/* Verify parameters. Ignore null paths and relative paths */
|
2007-03-15 06:41:09 +08:00
|
|
|
|
2015-06-11 21:47:26 +08:00
|
|
|
if (path == NULL || path[0] != '/')
|
2012-07-15 05:05:40 +08:00
|
|
|
{
|
2014-02-20 03:14:39 +08:00
|
|
|
return NULL;
|
2012-07-15 05:05:40 +08:00
|
|
|
}
|
2007-03-15 06:41:09 +08:00
|
|
|
|
2014-02-20 03:14:39 +08:00
|
|
|
/* Find the node to unlink */
|
2007-03-15 06:41:09 +08:00
|
|
|
|
2017-02-03 09:37:58 +08:00
|
|
|
node = inode_search_nofollow(&name, &peer, &parent, (const char **)NULL);
|
2014-02-20 03:14:39 +08:00
|
|
|
if (node)
|
2012-07-15 05:05:40 +08:00
|
|
|
{
|
2014-02-20 03:14:39 +08:00
|
|
|
/* If peer is non-null, then remove the node from the right of
|
|
|
|
* of that peer node.
|
|
|
|
*/
|
2007-03-15 06:41:09 +08:00
|
|
|
|
2014-02-20 03:14:39 +08:00
|
|
|
if (peer)
|
|
|
|
{
|
|
|
|
peer->i_peer = node->i_peer;
|
|
|
|
}
|
2007-03-15 06:41:09 +08:00
|
|
|
|
2014-02-20 03:14:39 +08:00
|
|
|
/* If parent is non-null, then remove the node from head of
|
|
|
|
* of the list of children.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (parent)
|
|
|
|
{
|
|
|
|
parent->i_child = node->i_peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, we must be removing the root inode. */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2015-06-12 01:24:20 +08:00
|
|
|
g_root_inode = node->i_peer;
|
2014-02-20 03:14:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
node->i_peer = NULL;
|
2012-07-15 05:05:40 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 03:14:39 +08:00
|
|
|
return node;
|
2007-03-15 06:41:09 +08:00
|
|
|
}
|
|
|
|
|
2007-05-09 08:04:03 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: inode_remove
|
2007-05-13 04:07:51 +08:00
|
|
|
*
|
2012-08-04 06:04:14 +08:00
|
|
|
* Description:
|
2014-02-20 03:14:39 +08:00
|
|
|
* Given a path, remove a the node from the in-memory, inode tree that the
|
|
|
|
* path refers to and free all resources related to the inode. If the
|
|
|
|
* inode is in-use, then it will be unlinked, but will not be freed until
|
|
|
|
* the last reference to the inode is released.
|
2012-08-04 06:04:14 +08:00
|
|
|
*
|
2014-02-20 03:14:39 +08:00
|
|
|
* Assumptions/Limitations:
|
|
|
|
* The caller must hold the inode semaphore
|
2012-07-15 05:05:40 +08:00
|
|
|
*
|
2007-05-09 08:04:03 +08:00
|
|
|
****************************************************************************/
|
2007-03-15 06:41:09 +08:00
|
|
|
|
2012-08-04 06:04:14 +08:00
|
|
|
int inode_remove(FAR const char *path)
|
2007-03-15 06:41:09 +08:00
|
|
|
{
|
|
|
|
FAR struct inode *node;
|
|
|
|
|
2014-02-20 03:14:39 +08:00
|
|
|
/* Find the inode and unlink it from the in-memory inode tree */
|
2007-03-15 06:41:09 +08:00
|
|
|
|
2014-02-20 03:14:39 +08:00
|
|
|
node = inode_unlink(path);
|
2007-03-15 06:41:09 +08:00
|
|
|
if (node)
|
|
|
|
{
|
2014-02-20 03:14:39 +08:00
|
|
|
/* Found it! But we cannot delete the inode if there are references
|
|
|
|
* to it
|
|
|
|
*/
|
2007-03-15 06:41:09 +08:00
|
|
|
|
|
|
|
if (node->i_crefs)
|
|
|
|
{
|
2014-02-20 03:14:39 +08:00
|
|
|
/* In that case, we will mark it deleted, when the filesystem
|
|
|
|
* releases the inode, we will then, finally delete the subtree
|
2012-07-15 05:05:40 +08:00
|
|
|
*/
|
2007-03-15 06:41:09 +08:00
|
|
|
|
|
|
|
node->i_flags |= FSNODEFLAG_DELETED;
|
2007-05-28 03:10:40 +08:00
|
|
|
return -EBUSY;
|
2012-07-15 05:05:40 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-12 01:24:20 +08:00
|
|
|
/* And delete it now -- recursively to delete all of its children.
|
|
|
|
* Since it has been unlinked, then the peer pointer should be NULL.
|
|
|
|
*/
|
2007-03-15 06:41:09 +08:00
|
|
|
|
2015-06-12 01:24:20 +08:00
|
|
|
DEBUGASSERT(node->i_peer == NULL);
|
|
|
|
inode_free(node);
|
2007-03-15 06:41:09 +08:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 03:14:39 +08:00
|
|
|
/* The node does not exist */
|
2007-03-15 06:41:09 +08:00
|
|
|
|
2007-05-28 03:10:40 +08:00
|
|
|
return -ENOENT;
|
2007-03-15 06:41:09 +08:00
|
|
|
}
|