fs/vfs: fix case when file to rename does not exist

Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
This commit is contained in:
Petro Karashchenko 2022-10-14 16:44:26 +02:00 committed by Xiang Xiao
parent 4e3aa83706
commit 969a06331f

View File

@ -397,14 +397,26 @@ next_subdir:
goto next_subdir; goto next_subdir;
} }
else if (oldinode->u.i_mops->unlink) else
{ {
/* No.. newrelpath must refer to a regular file. Attempt /* No.. newrelpath must refer to a regular file. Make sure
* to remove the file before doing the rename. * that the file at the olrelpath 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;
}
if (oldinode->u.i_mops->unlink)
{
/* Attempt to remove the file before doing the rename.
* *
* NOTE that errors are not handled here. If we failed to * NOTE that errors are not handled here. If we failed
* remove the file, then the file system 'rename' method * to remove the file, then the file system 'rename'
* should check that. * method should check that.
*/ */
oldinode->u.i_mops->unlink(oldinode, newrelpath); oldinode->u.i_mops->unlink(oldinode, newrelpath);
@ -412,6 +424,7 @@ next_subdir:
} }
} }
} }
}
/* Just declare success of the oldrepath and the newrelpath point to /* Just declare success of the oldrepath and the newrelpath point to
* the same directory entry. That directory entry should have been * the same directory entry. That directory entry should have been