2007-05-21 16:36:00 +02:00
|
|
|
/****************************************************************************
|
2014-09-28 19:46:11 +02:00
|
|
|
* fs/vfs/fs_rmdir.c
|
2007-05-21 16:36:00 +02:00
|
|
|
*
|
2017-02-04 18:46:54 +01:00
|
|
|
* Copyright (C) 2007-2009, 2014, 2017 Gregory Nutt. All rights reserved.
|
2012-07-14 23:05:40 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-05-21 16:36:00 +02: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-01-29 01:50:16 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-05-21 16:36:00 +02: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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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_WRITABLE_MOUNTPOINT
|
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_WRITABLE) && \
|
|
|
|
CONFIG_NFILE_STREAMS > 0
|
|
|
|
# define FS_HAVE_WRITABLE_MOUNTPOINT 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef FS_HAVE_PSEUDOFS_OPERATIONS
|
|
|
|
#if !defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS) && CONFIG_NFILE_STREAMS > 0
|
|
|
|
# define FS_HAVE_PSEUDOFS_OPERATIONS 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef FS_HAVE_RMDIR
|
|
|
|
#if defined(FS_HAVE_WRITABLE_MOUNTPOINT) || defined(FS_HAVE_PSEUDOFS_OPERATIONS)
|
|
|
|
# define FS_HAVE_RMDIR 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef FS_HAVE_RMDIR
|
|
|
|
|
2007-05-21 16:36:00 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: rmdir
|
|
|
|
*
|
|
|
|
* Description: Remove a file managed a mountpoint
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-01-29 01:50:16 +01:00
|
|
|
int rmdir(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 errcode;
|
|
|
|
int ret;
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-03 14:42:19 +01:00
|
|
|
/* Get an inode for the directory (or for the mountpoint containing the
|
|
|
|
* directory). inode_find() automatically increments the reference count
|
|
|
|
* on the inode if one is found.
|
2014-02-19 17:01:26 +01:00
|
|
|
*/
|
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-19 17:01:26 +01:00
|
|
|
/* There is no inode that includes in this path */
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-05 16:51:42 +01:00
|
|
|
errcode = -ret;
|
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-19 17:01:26 +01:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
/* Check if the inode is a valid mountpoint. */
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2014-02-19 17:01:26 +01:00
|
|
|
if (INODE_IS_MOUNTPT(inode) && inode->u.i_mops)
|
2007-05-21 16:36:00 +02:00
|
|
|
{
|
2014-02-19 17:01:26 +01:00
|
|
|
/* Perform the rmdir operation using the relative path
|
|
|
|
* from the mountpoint.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (inode->u.i_mops->rmdir)
|
|
|
|
{
|
2017-02-05 16:51:42 +01:00
|
|
|
ret = inode->u.i_mops->rmdir(inode, desc.relpath);
|
2014-02-19 17:01:26 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errcode = ENOSYS;
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
2007-05-21 16:36:00 +02:00
|
|
|
}
|
2014-02-19 17:01:26 +01:00
|
|
|
else
|
|
|
|
#endif
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2014-02-21 01:14:02 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
/* If this is a "dangling" pseudo-directory node (i.e., it has no operations)
|
2014-02-19 17:01:26 +01:00
|
|
|
* then rmdir should remove the node.
|
2007-05-21 16:36:00 +02:00
|
|
|
*/
|
|
|
|
|
2014-02-19 17:01:26 +01:00
|
|
|
if (!inode->u.i_ops)
|
2007-05-21 16:36:00 +02:00
|
|
|
{
|
2014-02-19 17:01:26 +01:00
|
|
|
/* If the directory inode has children, however, then it cannot be
|
|
|
|
* removed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (inode->i_child)
|
|
|
|
{
|
|
|
|
errcode = ENOTEMPTY;
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the inode. NOTE: Because we hold a reference count on the
|
|
|
|
* inode, it will not be deleted now. But probably when inode_release()
|
|
|
|
* is called below. inode_remove should return -EBUSY to indicate that
|
|
|
|
* the inode was not deleted now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
inode_semtake();
|
|
|
|
ret = inode_remove(pathname);
|
|
|
|
inode_semgive();
|
|
|
|
|
|
|
|
if (ret < 0 && ret != -EBUSY)
|
2007-05-21 16:36:00 +02:00
|
|
|
{
|
2014-02-19 17:01:26 +01:00
|
|
|
errcode = -ret;
|
2007-05-21 16:36:00 +02:00
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2014-02-21 01:14:02 +01:00
|
|
|
{
|
|
|
|
errcode = ENOTDIR;
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
|
|
|
#else
|
2014-02-19 16:21:38 +01:00
|
|
|
{
|
2014-02-19 17:01:26 +01:00
|
|
|
errcode = ENXIO;
|
2007-05-21 16:36:00 +02:00
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
2014-02-21 01:14:02 +01:00
|
|
|
#endif
|
2007-05-21 16:36:00 +02:00
|
|
|
|
|
|
|
/* Successfully removed the directory */
|
|
|
|
|
|
|
|
inode_release(inode);
|
2017-02-05 21:25:45 +01:00
|
|
|
RELEASE_SEARCH(&desc);
|
2007-05-21 16:36:00 +02:00
|
|
|
return OK;
|
|
|
|
|
2014-02-19 17:01:26 +01:00
|
|
|
errout_with_inode:
|
2007-05-21 16:36:00 +02:00
|
|
|
inode_release(inode);
|
2017-02-05 21:25:45 +01:00
|
|
|
errout_with_search:
|
|
|
|
RELEASE_SEARCH(&desc);
|
2014-02-19 17:01:26 +01:00
|
|
|
set_errno(errcode);
|
2007-05-21 16:36:00 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2014-02-21 01:14:02 +01:00
|
|
|
#endif /* FS_HAVE_RMDIR */
|