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>
|
2021-05-18 08:59:14 +02: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>
|
2023-08-08 00:11:20 +02:00
|
|
|
#include <nuttx/lib/lib.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;
|
|
|
|
int ret;
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2022-10-15 08:19:51 +02:00
|
|
|
/* According to POSIX, any new inode at this path should be removed
|
2017-02-11 17:08:23 +01:00
|
|
|
* 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-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)
|
|
|
|
{
|
2021-07-09 09:46:02 +02:00
|
|
|
inode_release(newinode);
|
2017-02-12 15:37:28 +01:00
|
|
|
ret = OK;
|
2022-10-15 08:19:51 +02:00
|
|
|
goto errout; /* Same name, this is not an error case. */
|
2017-02-12 15:37:28 +01:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2021-07-09 09:46:02 +02:00
|
|
|
inode_release(newinode);
|
|
|
|
|
2022-10-15 08:19:51 +02:00
|
|
|
/* Free memory may be allocated in previous loop */
|
|
|
|
|
|
|
|
if (subdir != NULL)
|
|
|
|
{
|
2023-08-08 00:11:20 +02:00
|
|
|
lib_free(subdir);
|
2022-10-15 08:19:51 +02:00
|
|
|
subdir = NULL;
|
|
|
|
}
|
|
|
|
|
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);
|
2023-04-12 14:32:35 +02:00
|
|
|
ret = asprintf(&subdir, "%s/%s", newpath, subdirname);
|
|
|
|
if (ret < 0)
|
2017-02-11 17:08:23 +01:00
|
|
|
{
|
2023-04-12 14:32:35 +02:00
|
|
|
subdir = NULL;
|
2017-02-11 17:08:23 +01:00
|
|
|
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
|
|
|
|
2020-09-15 20:54:44 +02:00
|
|
|
RELEASE_SEARCH(&newdesc);
|
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
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = inode_lock();
|
2020-03-30 01:36:19 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
2017-02-05 16:51:42 +01:00
|
|
|
|
2021-07-09 07:43:26 +02:00
|
|
|
ret = inode_reserve(newpath, 0777, &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;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
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 */
|
2021-07-09 07:43:26 +02:00
|
|
|
#ifdef CONFIG_PSEUDOFS_ATTRIBUTES
|
2017-02-11 17:08:23 +01:00
|
|
|
newinode->i_mode = oldinode->i_mode; /* Access mode flags */
|
2021-07-09 07:43:26 +02:00
|
|
|
newinode->i_owner = oldinode->i_owner; /* Owner */
|
|
|
|
newinode->i_group = oldinode->i_group; /* Group */
|
|
|
|
newinode->i_atime = oldinode->i_atime; /* Time of last access */
|
|
|
|
newinode->i_mtime = oldinode->i_mtime; /* Time of last modification */
|
|
|
|
newinode->i_ctime = oldinode->i_ctime; /* Time of last status change */
|
2017-02-11 17:08:23 +01:00
|
|
|
#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);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2017-02-11 17:08:23 +01:00
|
|
|
}
|
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
|
|
|
|
2021-07-08 11:01:15 +02:00
|
|
|
oldinode->i_child = NULL;
|
|
|
|
oldinode->i_parent = NULL;
|
2017-02-11 17:08:23 +01:00
|
|
|
ret = OK;
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
|
|
|
inode_unlock();
|
2017-02-05 16:51:42 +01:00
|
|
|
|
2017-02-11 17:08:23 +01:00
|
|
|
errout:
|
2020-09-15 20:54:44 +02:00
|
|
|
RELEASE_SEARCH(&newdesc);
|
2017-02-11 17:08:23 +01:00
|
|
|
if (subdir != NULL)
|
|
|
|
{
|
2023-08-07 23:14:41 +02:00
|
|
|
lib_free(subdir);
|
2017-02-11 17:08:23 +01:00
|
|
|
}
|
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
|
|
|
|
2022-10-15 08:55:16 +02:00
|
|
|
/* If oldrelpath and newrelpath are the same, then this is an attempt
|
|
|
|
* to move the directory entry onto itself. Let's not but say we did.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (strcmp(oldrelpath, newrelpath) == 0)
|
|
|
|
{
|
|
|
|
ret = OK;
|
|
|
|
goto errout_with_newinode; /* Same name, this is not an error case. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does a directory entry already exist at the 'newrelpath'? And is it
|
2017-02-12 15:37:28 +01:00
|
|
|
* 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
|
2022-10-15 08:55:16 +02:00
|
|
|
* directory, then rename(b,a) should use move the oldrelpath should be
|
2017-02-11 18:18:30 +01:00
|
|
|
* moved as if rename(b,a/basename(b)) had been called.
|
2017-02-11 17:08:23 +01:00
|
|
|
*/
|
|
|
|
|
2022-10-15 08:55:16 +02:00
|
|
|
if (oldinode->u.i_mops->stat != NULL)
|
2017-02-11 17:08:23 +01:00
|
|
|
{
|
2017-02-11 18:18:30 +01:00
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
next_subdir:
|
2022-10-15 08:55:16 +02:00
|
|
|
ret = oldinode->u.i_mops->stat(oldinode, newrelpath, &buf);
|
|
|
|
if (ret >= 0)
|
2017-02-12 15:37:28 +01:00
|
|
|
{
|
2022-10-15 08:55:16 +02:00
|
|
|
/* Is the directory entry a directory? */
|
2017-02-11 18:18:30 +01:00
|
|
|
|
2022-10-15 08:55:16 +02:00
|
|
|
if (S_ISDIR(buf.st_mode))
|
|
|
|
{
|
|
|
|
FAR char *subdirname;
|
2017-02-12 15:37:28 +01:00
|
|
|
|
2022-10-15 08:55:16 +02:00
|
|
|
/* Free memory may be allocated in previous loop */
|
2022-10-15 08:19:51 +02:00
|
|
|
|
2022-10-15 08:55:16 +02:00
|
|
|
if (subdir != NULL)
|
|
|
|
{
|
2023-08-08 00:11:20 +02:00
|
|
|
lib_free(subdir);
|
2022-10-15 08:55:16 +02:00
|
|
|
subdir = NULL;
|
|
|
|
}
|
2022-10-15 08:19:51 +02:00
|
|
|
|
2022-10-15 08:55:16 +02: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.
|
|
|
|
*/
|
2017-02-12 15:37:28 +01:00
|
|
|
|
2022-10-15 08:55:16 +02:00
|
|
|
subdirname = basename((FAR char *)oldrelpath);
|
2017-02-12 15:37:28 +01:00
|
|
|
|
2022-10-15 08:55:16 +02:00
|
|
|
/* Special case the root directory */
|
2017-02-12 15:37:28 +01:00
|
|
|
|
2022-10-15 08:55:16 +02:00
|
|
|
if (*newrelpath == '\0')
|
|
|
|
{
|
|
|
|
newrelpath = subdirname;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-12 14:32:35 +02:00
|
|
|
ret = asprintf(&subdir, "%s/%s", newrelpath,
|
|
|
|
subdirname);
|
|
|
|
if (ret < 0)
|
2017-02-12 15:37:28 +01:00
|
|
|
{
|
2023-04-12 14:32:35 +02:00
|
|
|
subdir = NULL;
|
2022-10-15 08:55:16 +02:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_newinode;
|
2017-02-12 15:37:28 +01:00
|
|
|
}
|
|
|
|
|
2022-10-15 08:55:16 +02:00
|
|
|
newrelpath = subdir;
|
|
|
|
}
|
2017-02-12 15:37:28 +01:00
|
|
|
|
2022-10-15 08:55:16 +02:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. newrelpath must refer to a regular file. Make sure
|
|
|
|
* that the file at the oldrelpath actually exists before
|
|
|
|
* performing any further actions with newrelpath
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = oldinode->u.i_mops->stat(oldinode, oldrelpath, &buf);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_newinode;
|
2017-02-11 18:18:30 +01:00
|
|
|
}
|
2022-10-15 08:55:16 +02:00
|
|
|
|
|
|
|
if (oldinode->u.i_mops->unlink)
|
2017-02-11 18:18:30 +01:00
|
|
|
{
|
2022-10-15 08:55:16 +02:00
|
|
|
/* 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.
|
2017-02-12 15:37:28 +01:00
|
|
|
*/
|
|
|
|
|
2022-10-15 08:55:16 +02: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
|
|
|
|
2022-10-15 08:55:16 +02:00
|
|
|
/* Perform the rename operation using the relative paths at the common
|
|
|
|
* mountpoint.
|
2017-02-11 18:18:30 +01:00
|
|
|
*/
|
2014-02-19 20:14:39 +01:00
|
|
|
|
2022-10-15 08:55:16 +02:00
|
|
|
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);
|
2017-02-11 18:18:30 +01:00
|
|
|
if (subdir != NULL)
|
|
|
|
{
|
2023-08-07 23:14:41 +02:00
|
|
|
lib_free(subdir);
|
2017-02-11 18:18:30 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2020-09-13 19:15:05 +02:00
|
|
|
if (!oldpath || *oldpath == '\0' ||
|
|
|
|
!newpath || *newpath == '\0')
|
2017-02-11 17:08:23 +01:00
|
|
|
{
|
|
|
|
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 */
|