* fs/vfs/fs_rename.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <libgen.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/fs/fs.h>
#include <nuttx/lib/lib.h>
#include "inode/inode.h"
#include "fs_heap.h"
#include "vfs.h"
* Pre-processor Definitions
****************************************************************************/
#undef FS_HAVE_RENAME
#if !defined(CONFIG_DISABLE_MOUNTPOINT) || !defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS)
# define FS_HAVE_RENAME 1
#endif
#ifdef FS_HAVE_RENAME
* Private Functions
****************************************************************************/
* Name: pseudo_isparent
*
* Description:
* Check if 'parent' is an ancestor of 'child'
*
****************************************************************************/
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
static bool pseudo_isparent(FAR struct inode *parent,
FAR struct inode *child)
{
FAR struct inode *tmp;
for (tmp = child; tmp; tmp = tmp->i_parent)
{
if (tmp == parent)
{
return true;
}
}
return false;
}
* Name: pseudorename
*
* Description:
* Rename an inode in the pseudo file system
*
****************************************************************************/
static int pseudorename(FAR const char *oldpath, FAR struct inode *oldinode,
FAR const char *newpath)
{
struct inode_search_s newdesc;
FAR struct inode *newinode;
FAR char *subdir = NULL;
bool isdir = INODE_IS_PSEUDODIR(oldinode);
int ret;
* first, provided that it is not a directory.
*/
SETUP_SEARCH(&newdesc, newpath, true);
ret = inode_find(&newdesc);
if (ret >= 0)
{
FAR struct inode *oldlink = oldinode;
FAR struct inode *newlink = newdesc.node;
newinode = newlink;
DEBUGASSERT(newinode != NULL);
* move the directory entry onto itself. Let's not but say we did.
*/
if (INODE_IS_HARDLINK(oldinode))
{
oldlink = oldinode->i_private;
DEBUGASSERT(oldlink != NULL);
}
if (INODE_IS_HARDLINK(newinode))
{
newlink = newinode->i_private;
DEBUGASSERT(newlink != NULL);
}
if (oldinode == newinode || oldlink == newlink)
{
inode_release(newinode);
ret = OK;
goto errout;
}
#ifndef CONFIG_DISABLE_MOUNTPOINT
if (INODE_IS_MOUNTPT(newinode))
{
inode_release(newinode);
ret = -EXDEV;
goto errout;
}
#endif
* directory (i.e, an operation-less inode or an inode with children)?
*/
if ((newinode->u.i_ops == NULL || newinode->i_child != NULL)
#ifdef CONFIG_FS_LINKS
&& !INODE_IS_HARDLINK(newinode)
#endif
)
{
if (!INODE_IS_PSEUDODIR(oldinode))
{
ret = -EISDIR;
goto errout;
}
if (newinode->i_child != NULL)
{
* that is not empty.
*/
ret = -ENOTEMPTY;
goto errout;
}
if (pseudo_isparent(oldinode, newinode))
{
* children
*/
ret = -EINVAL;
goto errout;
}
inode_remove(newpath);
#ifdef CONFIG_FS_NOTIFY
notify_unlink(newpath);
#endif
}
else
{
* 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();
*/
if (isdir)
{
ret = -ENOTDIR;
goto errout;
}
inode_remove(newpath);
#ifdef CONFIG_FS_NOTIFY
notify_unlink(newpath);
#endif
}
inode_release(newinode);
}
* NOTE that the new inode will be created with a reference count
* of zero.
*/
inode_lock();
ret = inode_reserve(newpath, 0777, &newinode);
if (ret < 0)
{
* OR if we fail to allocate memory for the new inode (and possibly
* any new intermediate path segments).
*/
goto errout_with_lock;
}
if (pseudo_isparent(oldinode, newinode))
{
inode_remove(newpath);
ret = -EINVAL;
goto errout_with_lock;
}
newinode->i_child = oldinode->i_child;
newinode->i_flags = oldinode->i_flags;
newinode->u.i_ops = oldinode->u.i_ops;
newinode->i_ino = oldinode->i_ino;
#ifdef CONFIG_PSEUDOFS_ATTRIBUTES
newinode->i_mode = oldinode->i_mode;
newinode->i_owner = oldinode->i_owner;
newinode->i_group = oldinode->i_group;
newinode->i_atime = oldinode->i_atime;
newinode->i_mtime = oldinode->i_mtime;
newinode->i_ctime = oldinode->i_ctime;
clock_gettime(CLOCK_REALTIME, &newinode->i_parent->i_mtime);
newinode->i_parent->i_ctime = newinode->i_parent->i_mtime;
#endif
newinode->i_private = oldinode->i_private;
#ifdef CONFIG_FS_LINKS
* 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
* 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
* the references to the inode have been released (perhaps when
* inode_release() is called in remove()). inode_remove() should return
* -EBUSY to indicate that the inode was not deleted now.
*/
ret = inode_remove(oldpath);
if (ret < 0 && ret != -EBUSY)
{
inode_remove(newpath);
goto errout_with_lock;
}
oldinode->i_child = NULL;
oldinode->i_parent = NULL;
ret = OK;
errout_with_lock:
inode_unlock();
#ifdef CONFIG_FS_NOTIFY
if (ret >= 0)
{
notify_rename(oldpath, isdir, newpath, isdir);
}
#endif
errout:
RELEASE_SEARCH(&newdesc);
if (subdir != NULL)
{
fs_heap_free(subdir);
}
return ret;
}
#endif
* Name: mountptrename
*
* Description:
* Rename a file residing on a mounted volume.
*
****************************************************************************/
#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;
FAR const char *newrelpath;
FAR char *subdir = NULL;
bool newisdir = false;
bool oldisdir = false;
int ret;
DEBUGASSERT(oldinode->u.i_mops);
* 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.
*/
if (oldinode->u.i_mops->rename == NULL)
{
return -ENOSYS;
}
* mountpoint
*/
SETUP_SEARCH(&newdesc, newpath, true);
ret = inode_find(&newdesc);
if (ret < 0)
{
goto errout_with_newsearch;
}
newinode = newdesc.node;
newrelpath = newdesc.relpath;
DEBUGASSERT(newinode != NULL && newrelpath != NULL);
if (oldinode != newinode)
{
ret = -EXDEV;
goto errout_with_newinode;
}
* 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;
}
* not the same directory entry that we are moving?
*
* If the directory entry at the newrelpath is a regular file, then that
* file should be removed first.
*
* If the directory entry at the newrelpath is an empty directory, then it
* can be removed without issue.
*
* If the directory entry at the newrelpath is a non-empty directory,
* then the rename should fail with the error ENOTEMPTY.
*/
#ifdef CONFIG_FS_LINKS
if (oldinode->u.i_mops->lstat != NULL || oldinode->u.i_mops->stat != NULL)
#else
if (oldinode->u.i_mops->stat != NULL)
#endif
{
struct stat oldbuf;
struct stat newbuf;
#ifdef CONFIG_FS_LINKS
if (oldinode->u.i_mops->lstat)
{
ret = oldinode->u.i_mops->lstat(oldinode, oldrelpath, &oldbuf);
}
else
#endif
{
ret = oldinode->u.i_mops->stat(oldinode, oldrelpath, &oldbuf);
}
if (ret < 0)
{
goto errout_with_newinode;
}
oldisdir = S_ISDIR(oldbuf.st_mode);
#ifdef CONFIG_FS_LINKS
if (oldinode->u.i_mops->lstat)
{
ret = oldinode->u.i_mops->lstat(oldinode, newrelpath, &newbuf);
}
else
#endif
{
ret = oldinode->u.i_mops->stat(oldinode, newrelpath, &newbuf);
}
if (ret >= 0)
{
newisdir = S_ISDIR(newbuf.st_mode);
if (newisdir)
{
if (!oldisdir)
{
ret = -EISDIR;
goto errout_with_newinode;
}
* rmdir will handle the error cases.
*/
if (oldinode->u.i_mops->rmdir)
{
ret = oldinode->u.i_mops->rmdir(oldinode, newrelpath);
if (ret < 0)
{
goto errout_with_newinode;
}
}
}
else
{
if (oldisdir)
{
ret = -ENOTDIR;
goto errout_with_newinode;
}
if (oldinode->u.i_mops->unlink)
{
*
* NOTE that errors are not handled here. If we failed
* to remove the file, then the file system 'rename'
* method should check that.
*/
#ifdef CONFIG_FS_PATHCACHE
ret = oldinode->u.i_mops->unlink(oldinode, newrelpath);
if (ret >= 0 && INODE_IS_PATHCACHE(oldinode))
{
pathcache_remove(newpath);
}
#else
oldinode->u.i_mops->unlink(oldinode, newrelpath);
#endif
#ifdef CONFIG_FS_NOTIFY
notify_unlink(newrelpath);
#endif
}
}
}
}
* mountpoint.
*/
ret = oldinode->u.i_mops->rename(oldinode, oldrelpath, newrelpath);
#ifdef CONFIG_FS_NOTIFY
if (ret >= 0)
{
notify_rename(oldpath, oldisdir, newpath, newisdir);
}
#endif
#ifdef CONFIG_FS_PATHCACHE
if (ret >= 0 && INODE_IS_PATHCACHE(oldinode))
{
pathcache_remove(oldpath);
}
#endif
errout_with_newinode:
inode_release(newinode);
errout_with_newsearch:
RELEASE_SEARCH(&newdesc);
if (subdir != NULL)
{
fs_heap_free(subdir);
}
return ret;
}
#endif
* Public Functions
****************************************************************************/
* Name: rename
*
* Description:
* Rename a file or directory.
*
****************************************************************************/
int rename(FAR const char *oldpath, FAR const char *newpath)
{
struct inode_search_s olddesc;
FAR struct inode *oldinode;
int ret;
* name and cannot be moved
*/
if (!oldpath || *oldpath == '\0' ||
!newpath || *newpath == '\0')
{
ret = -ENOENT;
goto errout;
}
SETUP_SEARCH(&olddesc, oldpath, true);
ret = inode_find(&olddesc);
if (ret < 0)
{
goto errout_with_oldsearch;
}
oldinode = olddesc.node;
DEBUGASSERT(oldinode != NULL);
#ifndef CONFIG_DISABLE_MOUNTPOINT
if (INODE_IS_MOUNTPT(oldinode) && *olddesc.relpath != '\0')
{
ret = mountptrename(oldpath, oldinode, olddesc.relpath, newpath);
}
else
#endif
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
{
ret = pseudorename(oldpath, oldinode, newpath);
}
#else
{
ret = -ENXIO;
}
#endif
inode_release(oldinode);
errout_with_oldsearch:
RELEASE_SEARCH(&olddesc);
errout:
if (ret < 0)
{
set_errno(-ret);
return ERROR;
}
return OK;
}
#endif