2007-05-21 16:36:00 +02:00
|
|
|
/****************************************************************************
|
2014-09-28 19:46:11 +02:00
|
|
|
* fs/vfs/fs_unlink.c
|
2007-05-21 16:36:00 +02:00
|
|
|
*
|
2020-03-21 17:48:14 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-05-21 16:36:00 +02:00
|
|
|
*
|
2020-03-21 17:48:14 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-05-21 16:36:00 +02:00
|
|
|
*
|
2020-03-21 17:48:14 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-05-21 16:36:00 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-15 01:18:32 +01:00
|
|
|
|
2017-02-04 18:46:54 +01:00
|
|
|
#include <stdbool.h>
|
2007-05-21 16:36:00 +02:00
|
|
|
#include <unistd.h>
|
2017-02-05 16:51:42 +01:00
|
|
|
#include <assert.h>
|
2007-05-21 16:36:00 +02:00
|
|
|
#include <errno.h>
|
2017-02-04 18:46:54 +01:00
|
|
|
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2007-05-21 16:36:00 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2009-12-15 01:18:32 +01:00
|
|
|
* Pre-processor Definitions
|
2007-05-21 16:36:00 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-21 01:14:02 +01:00
|
|
|
#undef FS_HAVE_UNLINK
|
2020-08-13 15:53:59 +02:00
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) || !defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS)
|
2014-02-21 01:14:02 +01:00
|
|
|
# define FS_HAVE_UNLINK 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef FS_HAVE_UNLINK
|
|
|
|
|
2007-05-21 16:36:00 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-12-24 11:22:51 +01:00
|
|
|
* Name: nx_unlink
|
2007-05-21 16:36:00 +02:00
|
|
|
*
|
|
|
|
* Description: Remove a file managed a mountpoint
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-12-24 11:22:51 +01:00
|
|
|
int nx_unlink(FAR const char *pathname)
|
2007-05-21 16:36:00 +02:00
|
|
|
{
|
2017-02-05 16:51:42 +01:00
|
|
|
struct inode_search_s desc;
|
2007-05-21 16:36:00 +02:00
|
|
|
FAR struct inode *inode;
|
2017-02-05 16:51:42 +01:00
|
|
|
int ret;
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-02 20:01:21 +01:00
|
|
|
/* Get an inode for this file (without deference the final node in the path
|
|
|
|
* which may be a symbolic link)
|
|
|
|
*/
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
SETUP_SEARCH(&desc, pathname, true);
|
2017-02-05 16:51:42 +01:00
|
|
|
|
|
|
|
ret = inode_find(&desc);
|
|
|
|
if (ret < 0)
|
2007-05-21 16:36:00 +02:00
|
|
|
{
|
2014-02-21 01:14:02 +01:00
|
|
|
/* There is no inode that includes in this path */
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
goto errout_with_search;
|
2007-05-21 16:36:00 +02:00
|
|
|
}
|
|
|
|
|
2017-02-05 16:51:42 +01:00
|
|
|
/* Get the search results */
|
|
|
|
|
|
|
|
inode = desc.node;
|
|
|
|
DEBUGASSERT(inode != NULL);
|
|
|
|
|
2014-02-21 01:14:02 +01:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
/* Check if the inode is a valid mountpoint. */
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2014-02-21 01:14:02 +01:00
|
|
|
if (INODE_IS_MOUNTPT(inode) && inode->u.i_mops)
|
2007-05-21 16:36:00 +02:00
|
|
|
{
|
2014-02-21 01:14:02 +01:00
|
|
|
/* Perform the unlink operation using the relative path at the
|
|
|
|
* mountpoint.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (inode->u.i_mops->unlink)
|
|
|
|
{
|
2017-02-05 16:51:42 +01:00
|
|
|
ret = inode->u.i_mops->unlink(inode, desc.relpath);
|
2014-02-21 01:14:02 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-24 11:22:51 +01:00
|
|
|
ret = -ENOSYS;
|
2014-02-21 01:14:02 +01:00
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
2007-05-21 16:36:00 +02:00
|
|
|
}
|
2014-02-21 01:14:02 +01:00
|
|
|
else
|
|
|
|
#endif
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2020-12-24 15:53:32 +01:00
|
|
|
{
|
2014-02-21 01:14:02 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2020-12-24 15:53:32 +01:00
|
|
|
/* Refuse to unlink the inode if it has children. I.e., if it is
|
|
|
|
* functioning as a directory and the directory is not empty.
|
2014-02-21 01:14:02 +01:00
|
|
|
*/
|
|
|
|
|
2020-12-24 15:53:32 +01:00
|
|
|
if (inode->i_child != NULL)
|
2007-05-21 16:36:00 +02:00
|
|
|
{
|
2020-12-24 15:53:32 +01:00
|
|
|
ret = -ENOTEMPTY;
|
2022-07-08 06:25:58 +02:00
|
|
|
goto errout_with_inode;
|
2020-12-24 15:53:32 +01:00
|
|
|
}
|
2014-02-21 01:14:02 +01:00
|
|
|
|
2020-12-24 15:53:32 +01:00
|
|
|
/* Notify the driver that it has been unlinked. If there are no
|
|
|
|
* open references to the driver instance, then the driver should
|
|
|
|
* release all resources because it is no longer accessible.
|
|
|
|
*/
|
2014-02-21 01:14:02 +01:00
|
|
|
|
2020-12-24 15:53:32 +01:00
|
|
|
if (INODE_IS_DRIVER(inode) && inode->u.i_ops->unlink)
|
|
|
|
{
|
|
|
|
/* Notify the character driver that it has been unlinked */
|
2015-01-31 18:31:34 +01:00
|
|
|
|
2020-12-24 15:53:32 +01:00
|
|
|
ret = inode->u.i_ops->unlink(inode);
|
|
|
|
if (ret < 0)
|
2015-01-31 18:31:34 +01:00
|
|
|
{
|
2022-07-08 06:25:58 +02:00
|
|
|
goto errout_with_inode;
|
2015-01-31 20:19:23 +01:00
|
|
|
}
|
2020-12-24 15:53:32 +01:00
|
|
|
}
|
2015-02-01 00:16:30 +01:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
2020-12-24 15:53:32 +01:00
|
|
|
else if (INODE_IS_BLOCK(inode) && inode->u.i_bops->unlink)
|
|
|
|
{
|
|
|
|
/* Notify the block driver that it has been unlinked */
|
2015-01-31 20:19:23 +01:00
|
|
|
|
2020-12-24 15:53:32 +01:00
|
|
|
ret = inode->u.i_bops->unlink(inode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-07-08 06:25:58 +02:00
|
|
|
goto errout_with_inode;
|
2015-01-31 18:31:34 +01:00
|
|
|
}
|
2020-12-24 15:53:32 +01:00
|
|
|
}
|
2015-02-01 00:16:30 +01:00
|
|
|
#endif
|
2020-12-24 15:53:32 +01:00
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
else if (INODE_IS_PSEUDODIR(inode) || INODE_IS_SOFTLINK(inode))
|
|
|
|
#else
|
|
|
|
else if (INODE_IS_PSEUDODIR(inode))
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* If this is a "dangling" pseudo-file node
|
|
|
|
* (i.e., it has no operations) or a soft link,
|
|
|
|
* then rm should remove the node.
|
2014-02-21 01:14:02 +01:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-24 15:53:32 +01:00
|
|
|
ret = -ENXIO;
|
2022-07-08 06:25:58 +02:00
|
|
|
goto errout_with_inode;
|
2020-12-24 15:53:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the old inode. Because we hold a reference count on the
|
|
|
|
* inode, it will not be deleted now. It will be deleted when all
|
|
|
|
* of the references to the inode have been released (perhaps
|
|
|
|
* when inode_release() is called below). inode_remove() will
|
|
|
|
* return -EBUSY to indicate that the inode was not deleted now.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = inode_lock();
|
2022-07-08 06:25:58 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
|
|
|
|
2020-12-24 15:53:32 +01:00
|
|
|
ret = inode_remove(pathname);
|
2022-09-06 08:18:45 +02:00
|
|
|
inode_unlock();
|
2020-12-24 15:53:32 +01:00
|
|
|
|
|
|
|
if (ret < 0 && ret != -EBUSY)
|
|
|
|
{
|
2007-05-21 16:36:00 +02:00
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
2014-09-29 01:30:42 +02:00
|
|
|
#endif
|
2007-05-21 16:36:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Successfully unlinked */
|
|
|
|
|
|
|
|
inode_release(inode);
|
2017-02-05 21:25:45 +01:00
|
|
|
RELEASE_SEARCH(&desc);
|
2007-05-21 16:36:00 +02:00
|
|
|
return OK;
|
|
|
|
|
2020-12-31 00:53:01 +01:00
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) || !defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS)
|
2015-10-04 23:04:00 +02:00
|
|
|
errout_with_inode:
|
2007-05-21 16:36:00 +02:00
|
|
|
inode_release(inode);
|
2020-12-31 00:53:01 +01:00
|
|
|
#endif
|
2017-02-05 21:25:45 +01:00
|
|
|
|
|
|
|
errout_with_search:
|
|
|
|
RELEASE_SEARCH(&desc);
|
2020-12-24 11:22:51 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unlink
|
|
|
|
*
|
|
|
|
* Description: Remove a file managed a mountpoint
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int unlink(FAR const char *pathname)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = nx_unlink(pathname);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
2007-05-21 16:36:00 +02:00
|
|
|
}
|
|
|
|
|
2014-02-21 01:14:02 +01:00
|
|
|
#endif /* FS_HAVE_UNLINK */
|