2007-05-21 16:36:00 +02:00
|
|
|
/****************************************************************************
|
2014-09-28 19:46:11 +02:00
|
|
|
* fs/vfs/fs_rename.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-11 18:18:30 +01:00
|
|
|
#include <sys/stat.h>
|
2017-02-04 18:46:54 +01:00
|
|
|
#include <stdbool.h>
|
2007-05-21 16:36:00 +02:00
|
|
|
#include <stdio.h>
|
2017-02-12 15:37:28 +01:00
|
|
|
#include <string.h>
|
2017-02-11 17:08:23 +01:00
|
|
|
#include <libgen.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_RENAME
|
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_RENAME 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef FS_HAVE_RENAME
|
|
|
|
|
2007-05-21 16:36:00 +02:00
|
|
|
/****************************************************************************
|
2017-02-11 17:08:23 +01:00
|
|
|
* Private Functions
|
2007-05-21 16:36:00 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-02-11 17:08:23 +01:00
|
|
|
* Name: pseudorename
|
2007-05-21 16:36:00 +02:00
|
|
|
*
|
2017-02-11 17:08:23 +01:00
|
|
|
* Description:
|
|
|
|
* Rename an inode in the pseudo file system
|
2007-05-21 16:36:00 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int pseudorename(FAR const char *oldpath, FAR struct inode *oldinode,
|
|
|
|
FAR const char *newpath)
|
2007-05-21 16:36:00 +02:00
|
|
|
{
|
2017-02-05 16:51:42 +01:00
|
|
|
struct inode_search_s newdesc;
|
2014-02-21 01:14:02 +01:00
|
|
|
FAR struct inode *newinode;
|
2017-02-11 17:08:23 +01:00
|
|
|
FAR char *subdir = NULL;
|
2017-02-12 15:37:28 +01:00
|
|
|
FAR const char *name;
|
2017-02-11 17:08:23 +01:00
|
|
|
int ret;
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-12 15:37:28 +01:00
|
|
|
/* Special case the root directory. There is no root inode and there is
|
|
|
|
* no name for the root. inode_find() will fail to the find the root
|
|
|
|
* inode -- because there isn't one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
name = newpath;
|
|
|
|
while (*name == '/')
|
|
|
|
{
|
|
|
|
name++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*name == '\0')
|
|
|
|
{
|
|
|
|
FAR char *subdirname;
|
|
|
|
|
|
|
|
/* In the newpath is the root directory, the target of the rename must
|
|
|
|
* be a directory entry under the root.
|
|
|
|
*/
|
|
|
|
|
|
|
|
subdirname = basename((FAR char *)oldpath);
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
asprintf(&subdir, "/%s", subdirname);
|
2017-02-12 15:37:28 +01:00
|
|
|
if (subdir == NULL)
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
newpath = subdir;
|
|
|
|
}
|
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
/* According to POSIX, any old inode at this path should be removed
|
|
|
|
* first, provided that it is not a directory.
|
2014-02-19 20:14:39 +01:00
|
|
|
*/
|
|
|
|
|
2017-02-11 18:18:30 +01:00
|
|
|
next_subdir:
|
2017-02-12 15:37:28 +01:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
SETUP_SEARCH(&newdesc, newpath, true);
|
|
|
|
ret = inode_find(&newdesc);
|
|
|
|
if (ret >= 0)
|
2014-02-19 20:14:39 +01:00
|
|
|
{
|
2017-02-11 17:08:23 +01:00
|
|
|
/* We found it. Get the search results */
|
|
|
|
|
|
|
|
newinode = newdesc.node;
|
|
|
|
DEBUGASSERT(newinode != NULL);
|
|
|
|
|
2017-02-12 15:37:28 +01:00
|
|
|
/* If the old and new inodes are the same, then this is an attempt to
|
|
|
|
* move the directory entry onto itself. Let's not but say we did.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (oldinode == newinode)
|
|
|
|
{
|
|
|
|
ret = OK;
|
|
|
|
goto errout; /* Bad naming, this is not an error case. */
|
|
|
|
}
|
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
/* Make sure that the old path does not lie on a mounted volume. */
|
|
|
|
|
|
|
|
if (INODE_IS_MOUNTPT(newinode))
|
|
|
|
{
|
|
|
|
inode_release(newinode);
|
|
|
|
ret = -EXDEV;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
2019-10-27 18:48:14 +01:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
/* We found it and it appears to be a "normal" inode. Is it a
|
2017-02-11 19:02:50 +01:00
|
|
|
* directory (i.e, an operation-less inode or an inode with children)?
|
2017-02-11 17:08:23 +01:00
|
|
|
*/
|
|
|
|
|
2017-02-11 19:02:50 +01:00
|
|
|
if (newinode->u.i_ops == NULL || newinode->i_child != NULL)
|
2017-02-11 17:08:23 +01:00
|
|
|
{
|
|
|
|
FAR char *subdirname;
|
2017-02-11 18:18:30 +01:00
|
|
|
FAR char *tmp;
|
2017-02-11 17:08:23 +01:00
|
|
|
|
|
|
|
/* Yes.. In this case, the target of the rename must be a
|
|
|
|
* subdirectory of newinode, not the newinode itself. For
|
|
|
|
* example: mv b a/ must move b to a/b.
|
|
|
|
*/
|
|
|
|
|
|
|
|
subdirname = basename((FAR char *)oldpath);
|
2017-02-11 18:18:30 +01:00
|
|
|
tmp = subdir;
|
|
|
|
subdir = NULL;
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
asprintf(&subdir, "%s/%s", newpath, subdirname);
|
2017-02-11 18:18:30 +01:00
|
|
|
|
|
|
|
if (tmp != NULL)
|
|
|
|
{
|
|
|
|
kmm_free(tmp);
|
|
|
|
}
|
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
if (subdir == NULL)
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
newpath = subdir;
|
|
|
|
|
2017-02-11 18:18:30 +01:00
|
|
|
/* This can be a recursive case, another inode may already exist
|
|
|
|
* at oldpth/subdirname. In that case, we need to do this all
|
|
|
|
* over again. A nasty goto is used because I am lazy.
|
2017-02-11 17:08:23 +01:00
|
|
|
*/
|
2017-02-11 18:18:30 +01:00
|
|
|
|
|
|
|
goto next_subdir;
|
2017-02-11 17:08:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Not a directory... remove it. It may still be something
|
|
|
|
* important (like a driver), but we will just have to suffer
|
|
|
|
* the consequences.
|
|
|
|
*
|
|
|
|
* NOTE (1) that we not bother to check the error. If we
|
|
|
|
* failed to remove the inode for some reason, then
|
|
|
|
* inode_reserve() will complain below, and (2) the inode
|
|
|
|
* won't really be removed until we call inode_release();
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
inode_remove(newpath);
|
2017-02-11 17:08:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inode_release(newinode);
|
2014-02-19 20:14:39 +01:00
|
|
|
}
|
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
/* Create a new, empty inode at the destination location.
|
|
|
|
* NOTE that the new inode will be created with a reference count
|
|
|
|
* of zero.
|
|
|
|
*/
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2020-03-30 01:36:19 +02:00
|
|
|
ret = inode_semtake();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
2017-02-05 16:51:42 +01:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
ret = inode_reserve(newpath, &newinode);
|
2017-02-05 16:51:42 +01:00
|
|
|
if (ret < 0)
|
2007-05-21 16:36:00 +02:00
|
|
|
{
|
2017-02-11 17:08:23 +01:00
|
|
|
/* It is an error if a node at newpath already exists in the tree
|
|
|
|
* OR if we fail to allocate memory for the new inode (and possibly
|
|
|
|
* any new intermediate path segments).
|
|
|
|
*/
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
ret = -EEXIST;
|
|
|
|
goto errout_with_sem;
|
2007-05-21 16:36:00 +02:00
|
|
|
}
|
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
/* Copy the inode state from the old inode to the newly allocated inode */
|
2017-02-05 16:51:42 +01:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
newinode->i_child = oldinode->i_child; /* Link to lower level inode */
|
|
|
|
newinode->i_flags = oldinode->i_flags; /* Flags for inode */
|
|
|
|
newinode->u.i_ops = oldinode->u.i_ops; /* Inode operations */
|
|
|
|
#ifdef CONFIG_FILE_MODE
|
|
|
|
newinode->i_mode = oldinode->i_mode; /* Access mode flags */
|
|
|
|
#endif
|
|
|
|
newinode->i_private = oldinode->i_private; /* Per inode driver private data */
|
2017-02-05 16:51:42 +01:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
/* Prevent the link target string from being deallocated. The pointer to
|
|
|
|
* the allocated link target path was copied above (under the guise of
|
|
|
|
* u.i_ops). Now we must nullify the u.i_link pointer so that it is not
|
|
|
|
* deallocated when inode_free() is (eventually called.
|
|
|
|
*/
|
|
|
|
|
|
|
|
oldinode->u.i_link = NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* We now have two copies of the inode. One with a reference count of
|
|
|
|
* zero (the new one), and one that may have multiple references
|
|
|
|
* including one by this logic (the old one)
|
|
|
|
*
|
|
|
|
* 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
|
2019-09-29 20:52:20 +02:00
|
|
|
* the references to the inode have been released (perhaps when
|
2017-02-11 17:08:23 +01:00
|
|
|
* inode_release() is called in remove()). inode_remove() should return
|
|
|
|
* -EBUSY to indicate that the inode was not deleted now.
|
|
|
|
*/
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
ret = inode_remove(oldpath);
|
|
|
|
if (ret < 0 && ret != -EBUSY)
|
2007-05-21 16:36:00 +02:00
|
|
|
{
|
2017-02-11 17:08:23 +01:00
|
|
|
/* Remove the new node we just recreated */
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
inode_remove(newpath);
|
2017-02-11 17:08:23 +01:00
|
|
|
goto errout_with_sem;
|
|
|
|
}
|
2017-02-05 16:51:42 +01:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
/* Remove all of the children from the unlinked inode */
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
oldinode->i_child = NULL;
|
|
|
|
ret = OK;
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
errout_with_sem:
|
|
|
|
inode_semgive();
|
2017-02-05 16:51:42 +01:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
errout:
|
|
|
|
if (subdir != NULL)
|
|
|
|
{
|
|
|
|
kmm_free(subdir);
|
|
|
|
}
|
2017-02-05 16:51:42 +01:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
#endif /* CONFIG_DISABLE_PSEUDOFS_OPERATIONS */
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mountptrename
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Rename a file residing on a mounted volume.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
static int mountptrename(FAR const char *oldpath, FAR struct inode *oldinode,
|
|
|
|
FAR const char *oldrelpath, FAR const char *newpath)
|
|
|
|
{
|
|
|
|
struct inode_search_s newdesc;
|
|
|
|
FAR struct inode *newinode;
|
2017-02-11 18:18:30 +01:00
|
|
|
FAR const char *newrelpath;
|
|
|
|
FAR char *subdir = NULL;
|
2017-02-11 17:08:23 +01:00
|
|
|
int ret;
|
2014-02-19 20:14:39 +01:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
DEBUGASSERT(oldinode->u.i_mops);
|
2014-02-19 20:14:39 +01:00
|
|
|
|
2017-02-12 01:55:13 +01:00
|
|
|
/* If the file system does not support the rename() method, then bail now.
|
|
|
|
* As of this writing, only NXFFS does not support the rename method. A
|
|
|
|
* good fallback might be to copy the oldrelpath to the correct location,
|
|
|
|
* then unlink it.
|
|
|
|
*/
|
2017-02-11 18:18:30 +01:00
|
|
|
|
|
|
|
if (oldinode->u.i_mops->rename == NULL)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
/* Get an inode for the new relpath -- it should lie on the same
|
|
|
|
* mountpoint
|
|
|
|
*/
|
|
|
|
|
|
|
|
SETUP_SEARCH(&newdesc, newpath, true);
|
|
|
|
|
|
|
|
ret = inode_find(&newdesc);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* There is no mountpoint that includes in this path */
|
|
|
|
|
|
|
|
goto errout_with_newsearch;
|
2007-05-21 16:36:00 +02:00
|
|
|
}
|
2017-02-11 17:08:23 +01:00
|
|
|
|
|
|
|
/* Get the search results */
|
|
|
|
|
2017-02-11 18:18:30 +01:00
|
|
|
newinode = newdesc.node;
|
|
|
|
newrelpath = newdesc.relpath;
|
2017-02-12 15:37:28 +01:00
|
|
|
DEBUGASSERT(newinode != NULL && newrelpath != NULL);
|
2017-02-11 17:08:23 +01:00
|
|
|
|
|
|
|
/* Verify that the two paths lie on the same mountpoint inode */
|
|
|
|
|
|
|
|
if (oldinode != newinode)
|
2014-02-19 20:14:39 +01:00
|
|
|
{
|
2017-02-11 17:08:23 +01:00
|
|
|
ret = -EXDEV;
|
|
|
|
goto errout_with_newinode;
|
|
|
|
}
|
2014-02-19 20:14:39 +01:00
|
|
|
|
2017-02-12 15:37:28 +01:00
|
|
|
/* Does a directory entry already exist at the 'rewrelpath'? And is it
|
|
|
|
* not the same directory entry that we are moving?
|
2017-02-11 18:18:30 +01:00
|
|
|
*
|
2017-02-12 15:37:28 +01:00
|
|
|
* If the directory entry at the newrelpath is a regular file, then that
|
|
|
|
* file should be removed first.
|
2017-02-11 18:18:30 +01:00
|
|
|
*
|
|
|
|
* If the directory entry at the target is a directory, then the source
|
|
|
|
* file should be moved "under" the directory, i.e., if newrelpath is a
|
|
|
|
* directory, then rename(b,a) should use move the olrelpath should be
|
|
|
|
* moved as if rename(b,a/basename(b)) had been called.
|
2017-02-11 17:08:23 +01:00
|
|
|
*/
|
|
|
|
|
2017-02-12 15:37:28 +01:00
|
|
|
if (oldinode->u.i_mops->stat != NULL &&
|
|
|
|
strcmp(oldrelpath, newrelpath) != 0)
|
2017-02-11 17:08:23 +01:00
|
|
|
{
|
2017-02-11 18:18:30 +01:00
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
next_subdir:
|
|
|
|
|
2017-02-12 15:37:28 +01:00
|
|
|
/* Something exists for this directory entry. Do nothing in the
|
|
|
|
* degenerate case where a directory or file is being moved to
|
|
|
|
* itself.
|
|
|
|
*/
|
2017-02-11 18:18:30 +01:00
|
|
|
|
2017-02-12 15:37:28 +01:00
|
|
|
if (strcmp(oldrelpath, newrelpath) != 0)
|
|
|
|
{
|
|
|
|
ret = oldinode->u.i_mops->stat(oldinode, newrelpath, &buf);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Is the directory entry a directory? */
|
2017-02-11 18:18:30 +01:00
|
|
|
|
2017-02-12 15:37:28 +01:00
|
|
|
if (S_ISDIR(buf.st_mode))
|
2017-02-11 18:18:30 +01:00
|
|
|
{
|
2017-02-12 15:37:28 +01:00
|
|
|
FAR char *subdirname;
|
|
|
|
|
2019-03-01 22:00:00 +01:00
|
|
|
/* Yes.. In this case, the target of the rename must be a
|
2017-02-12 15:37:28 +01:00
|
|
|
* subdirectory of newinode, not the newinode itself. For
|
|
|
|
* example: mv b a/ must move b to a/b.
|
|
|
|
*/
|
|
|
|
|
|
|
|
subdirname = basename((FAR char *)oldrelpath);
|
|
|
|
|
|
|
|
/* Special case the root directory */
|
|
|
|
|
|
|
|
if (*newrelpath == '\0')
|
|
|
|
{
|
|
|
|
if (subdir != NULL)
|
|
|
|
{
|
|
|
|
kmm_free(subdir);
|
|
|
|
subdir = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
newrelpath = subdirname;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR char *tmp = subdir;
|
|
|
|
|
|
|
|
subdir = NULL;
|
2020-01-02 17:49:34 +01:00
|
|
|
asprintf(&subdir, "%s/%s", newrelpath,
|
|
|
|
subdirname);
|
2017-02-12 15:37:28 +01:00
|
|
|
|
|
|
|
if (tmp != NULL)
|
|
|
|
{
|
|
|
|
kmm_free(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (subdir == NULL)
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_newinode;
|
|
|
|
}
|
|
|
|
|
|
|
|
newrelpath = subdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This can be a recursive, another directory may already
|
|
|
|
* exist at the newrelpath. In that case, we need to
|
|
|
|
* do this all over again. A nasty goto is used because
|
|
|
|
* I am lazy.
|
|
|
|
*/
|
|
|
|
|
|
|
|
goto next_subdir;
|
2017-02-11 18:18:30 +01:00
|
|
|
}
|
2017-02-12 15:37:28 +01:00
|
|
|
else if (oldinode->u.i_mops->unlink)
|
2017-02-11 18:18:30 +01:00
|
|
|
{
|
2017-02-12 15:37:28 +01:00
|
|
|
/* No.. newrelpath must refer to a regular file. Attempt
|
|
|
|
* to remove the file before doing the rename.
|
|
|
|
*
|
|
|
|
* NOTE that errors are not handled here. If we failed to
|
|
|
|
* remove the file, then the file system 'rename' method
|
|
|
|
* should check that.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
oldinode->u.i_mops->unlink(oldinode, newrelpath);
|
2017-02-11 18:18:30 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-19 20:14:39 +01:00
|
|
|
}
|
2017-02-11 17:08:23 +01:00
|
|
|
}
|
2014-02-19 20:14:39 +01:00
|
|
|
|
2017-02-12 15:37:28 +01:00
|
|
|
/* Just declare success of the oldrepath and the newrelpath point to
|
|
|
|
* the same directory entry. That directory entry should have been
|
|
|
|
* stat'ed above to assure that it exists.
|
2017-02-11 18:18:30 +01:00
|
|
|
*/
|
2014-02-19 20:14:39 +01:00
|
|
|
|
2017-02-12 15:37:28 +01:00
|
|
|
ret = OK;
|
|
|
|
if (strcmp(oldrelpath, newrelpath) != 0)
|
|
|
|
{
|
|
|
|
/* Perform the rename operation using the relative paths at the common
|
|
|
|
* mountpoint.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = oldinode->u.i_mops->rename(oldinode, oldrelpath, newrelpath);
|
|
|
|
}
|
2014-02-19 20:14:39 +01:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
errout_with_newinode:
|
|
|
|
inode_release(newinode);
|
2014-02-19 20:14:39 +01:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
errout_with_newsearch:
|
|
|
|
RELEASE_SEARCH(&newdesc);
|
2014-02-19 20:14:39 +01:00
|
|
|
|
2017-02-11 18:18:30 +01:00
|
|
|
if (subdir != NULL)
|
|
|
|
{
|
|
|
|
kmm_free(subdir);
|
|
|
|
}
|
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_DISABLE_MOUNTPOINT */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
2014-02-19 20:14:39 +01:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: rename
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Rename a file or directory.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2014-02-19 20:14:39 +01:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
int rename(FAR const char *oldpath, FAR const char *newpath)
|
|
|
|
{
|
|
|
|
struct inode_search_s olddesc;
|
|
|
|
FAR struct inode *oldinode;
|
|
|
|
int ret;
|
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
/* Ignore paths that are interpreted as the root directory which has no
|
|
|
|
* name and cannot be moved
|
2017-02-11 17:08:23 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!oldpath || *oldpath == '\0' || oldpath[0] != '/' ||
|
|
|
|
!newpath || *newpath == '\0' || newpath[0] != '/')
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto errout;
|
2007-05-21 16:36:00 +02:00
|
|
|
}
|
2017-02-11 17:08:23 +01:00
|
|
|
|
|
|
|
/* Get an inode that includes the oldpath */
|
|
|
|
|
|
|
|
SETUP_SEARCH(&olddesc, oldpath, true);
|
|
|
|
|
|
|
|
ret = inode_find(&olddesc);
|
|
|
|
if (ret < 0)
|
2014-02-21 01:14:02 +01:00
|
|
|
{
|
2017-02-11 17:08:23 +01:00
|
|
|
/* There is no inode that includes in this path */
|
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
goto errout_with_oldsearch;
|
2014-02-21 01:14:02 +01:00
|
|
|
}
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
/* Get the search results */
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
oldinode = olddesc.node;
|
|
|
|
DEBUGASSERT(oldinode != NULL);
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2014-02-19 20:14:39 +01:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
2017-02-11 17:08:23 +01:00
|
|
|
/* Verify that the old inode is a valid mountpoint. */
|
2017-02-05 21:25:45 +01:00
|
|
|
|
2020-09-15 17:13:53 +02:00
|
|
|
if (INODE_IS_MOUNTPT(oldinode) && *olddesc.relpath != '\0')
|
2017-02-11 17:08:23 +01:00
|
|
|
{
|
|
|
|
ret = mountptrename(oldpath, oldinode, olddesc.relpath, newpath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* CONFIG_DISABLE_MOUNTPOINT */
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
{
|
|
|
|
ret = pseudorename(oldpath, oldinode, newpath);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
ret = -ENXIO;
|
|
|
|
}
|
2014-02-19 20:14:39 +01:00
|
|
|
#endif
|
2017-02-05 21:25:45 +01:00
|
|
|
|
2007-05-21 16:36:00 +02:00
|
|
|
inode_release(oldinode);
|
2017-02-05 21:25:45 +01:00
|
|
|
|
|
|
|
errout_with_oldsearch:
|
|
|
|
RELEASE_SEARCH(&olddesc);
|
2017-02-11 17:08:23 +01:00
|
|
|
|
|
|
|
errout:
|
|
|
|
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_RENAME */
|