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
|
|
|
*
|
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_RMDIR
|
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_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
|
2019-03-01 22:00:00 +01:00
|
|
|
/* If this is a "dangling" pseudo-directory node (i.e., it has no
|
|
|
|
* operations) 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
|
2020-03-21 20:54:03 +01:00
|
|
|
* 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.
|
2014-02-19 17:01:26 +01:00
|
|
|
*/
|
|
|
|
|
2020-03-30 01:36:19 +02:00
|
|
|
ret = inode_semtake();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
|
|
|
|
2014-02-19 17:01:26 +01:00
|
|
|
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 */
|