2007-05-09 02:04:03 +02:00
|
|
|
/****************************************************************************
|
2014-09-28 19:06:21 +02:00
|
|
|
* fs/inode/fs_inoderemove.c
|
2007-03-14 23:41:09 +01:00
|
|
|
*
|
2017-02-04 18:21:44 +01:00
|
|
|
* Copyright (C) 2007-2009, 2017 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-09-12 16:46:44 +02: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.
|
|
|
|
*
|
2007-05-09 02:04:03 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2007-05-09 02:04:03 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Included Files
|
2007-05-09 02:04:03 +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 <errno.h>
|
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>
|
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
|
|
|
|
2014-02-19 20:14:39 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-05-09 02:04:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: inode_unlink
|
2014-02-19 20:14:39 +01: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 02:37:58 +01: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-19 20:14:39 +01:00
|
|
|
* Assumptions/Limitations:
|
|
|
|
* The caller must hold the inode semaphore
|
|
|
|
*
|
2007-05-09 02:04:03 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2014-02-19 20:14:39 +01:00
|
|
|
FAR struct inode *inode_unlink(FAR const char *path)
|
2007-03-14 23:41:09 +01:00
|
|
|
{
|
2017-02-04 18:21:44 +01:00
|
|
|
struct inode_search_s desc;
|
2014-02-19 20:14:39 +01:00
|
|
|
FAR struct inode *node;
|
2017-02-04 18:21:44 +01:00
|
|
|
int ret;
|
2014-02-19 20:14:39 +01:00
|
|
|
|
|
|
|
/* Verify parameters. Ignore null paths and relative paths */
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2015-06-11 15:47:26 +02:00
|
|
|
if (path == NULL || path[0] != '/')
|
2012-07-14 23:05:40 +02:00
|
|
|
{
|
2014-02-19 20:14:39 +01:00
|
|
|
return NULL;
|
2012-07-14 23:05:40 +02:00
|
|
|
}
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2014-02-19 20:14:39 +01:00
|
|
|
/* Find the node to unlink */
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
SETUP_SEARCH(&desc, path, true);
|
2017-02-04 18:21:44 +01:00
|
|
|
|
2017-02-04 19:51:14 +01:00
|
|
|
ret = inode_search(&desc);
|
2017-02-04 18:21:44 +01:00
|
|
|
if (ret >= 0)
|
2012-07-14 23:05:40 +02:00
|
|
|
{
|
2017-02-04 18:21:44 +01:00
|
|
|
node = desc.node;
|
|
|
|
DEBUGASSERT(node != NULL);
|
|
|
|
|
2014-02-19 20:14:39 +01:00
|
|
|
/* If peer is non-null, then remove the node from the right of
|
|
|
|
* of that peer node.
|
|
|
|
*/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2017-02-04 18:21:44 +01:00
|
|
|
if (desc.peer != NULL)
|
2014-02-19 20:14:39 +01:00
|
|
|
{
|
2017-02-04 18:21:44 +01:00
|
|
|
desc.peer->i_peer = node->i_peer;
|
2014-02-19 20:14:39 +01:00
|
|
|
}
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2014-02-19 20:14:39 +01:00
|
|
|
/* If parent is non-null, then remove the node from head of
|
|
|
|
* of the list of children.
|
|
|
|
*/
|
|
|
|
|
2017-02-04 18:21:44 +01:00
|
|
|
else if (desc.parent)
|
2014-02-19 20:14:39 +01:00
|
|
|
{
|
2017-02-04 18:21:44 +01:00
|
|
|
desc.parent->i_child = node->i_peer;
|
2014-02-19 20:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, we must be removing the root inode. */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2015-06-11 19:24:20 +02:00
|
|
|
g_root_inode = node->i_peer;
|
2014-02-19 20:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
node->i_peer = NULL;
|
2012-07-14 23:05:40 +02:00
|
|
|
}
|
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
RELEASE_SEARCH(&desc);
|
2014-02-19 20:14:39 +01:00
|
|
|
return node;
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
2007-05-09 02:04:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: inode_remove
|
2007-05-12 22:07:51 +02:00
|
|
|
*
|
2012-08-04 00:04:14 +02:00
|
|
|
* Description:
|
2014-02-19 20:14:39 +01: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 00:04:14 +02:00
|
|
|
*
|
2014-02-19 20:14:39 +01:00
|
|
|
* Assumptions/Limitations:
|
|
|
|
* The caller must hold the inode semaphore
|
2012-07-14 23:05:40 +02:00
|
|
|
*
|
2007-05-09 02:04:03 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2012-08-04 00:04:14 +02:00
|
|
|
int inode_remove(FAR const char *path)
|
2007-03-14 23:41:09 +01:00
|
|
|
{
|
|
|
|
FAR struct inode *node;
|
|
|
|
|
2014-02-19 20:14:39 +01:00
|
|
|
/* Find the inode and unlink it from the in-memory inode tree */
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2014-02-19 20:14:39 +01:00
|
|
|
node = inode_unlink(path);
|
2007-03-14 23:41:09 +01:00
|
|
|
if (node)
|
|
|
|
{
|
2014-02-19 20:14:39 +01:00
|
|
|
/* Found it! But we cannot delete the inode if there are references
|
|
|
|
* to it
|
|
|
|
*/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
|
|
|
if (node->i_crefs)
|
|
|
|
{
|
2014-02-19 20:14:39 +01:00
|
|
|
/* In that case, we will mark it deleted, when the filesystem
|
|
|
|
* releases the inode, we will then, finally delete the subtree
|
2012-07-14 23:05:40 +02:00
|
|
|
*/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
|
|
|
node->i_flags |= FSNODEFLAG_DELETED;
|
2007-05-27 21:10:40 +02:00
|
|
|
return -EBUSY;
|
2012-07-14 23:05:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-11 19:24:20 +02: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-14 23:41:09 +01:00
|
|
|
|
2015-06-11 19:24:20 +02:00
|
|
|
DEBUGASSERT(node->i_peer == NULL);
|
|
|
|
inode_free(node);
|
2007-03-14 23:41:09 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 20:14:39 +01:00
|
|
|
/* The node does not exist */
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2007-05-27 21:10:40 +02:00
|
|
|
return -ENOENT;
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|