+ yaffsfs_Lock();
+
+ obj = yaffsfs_FindObject(NULL,path,0,0,NULL,NULL,NULL);
+ dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
+
+ if(!dir && notDir)
+ yaffsfs_SetError(-ENOTDIR);
+ else if(loop)
+ yaffsfs_SetError(-ELOOP);
+ else if(!dir)
+ yaffsfs_SetError(-ENOENT);
+ else if(strncmp(name,_Y("."),2) == 0)
+ yaffsfs_SetError(-EINVAL);
+ else if(!obj)
+ yaffsfs_SetError(-ENOENT);
+ else if(obj->my_dev->read_only)
+ yaffsfs_SetError(-EROFS);
+ else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
+ yaffsfs_SetError(-EISDIR);
+ else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
+ yaffsfs_SetError(-ENOTDIR);
+ else if(isDirectory && obj == obj->my_dev->root_dir)
+ yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
+ else {
+ result = yaffs_unlinker(dir,name);
+
+ if(result == YAFFS_FAIL && isDirectory)
+ yaffsfs_SetError(-ENOTEMPTY);
+ }
+
+ yaffsfs_Unlock();
+
+ return (result == YAFFS_FAIL) ? -1 : 0;
+}
+
+
+int yaffs_unlink(const YCHAR *path)
+{
+ return yaffsfs_DoUnlink(path,0);
+}
+
+int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
+{
+ struct yaffs_obj *olddir = NULL;
+ struct yaffs_obj *newdir = NULL;
+ struct yaffs_obj *obj = NULL;
+ struct yaffs_obj *newobj = NULL;
+ YCHAR *oldname;
+ YCHAR *newname;
+ int result= YAFFS_FAIL;
+ int rename_allowed = 1;
+ int notOldDir = 0;
+ int notNewDir = 0;
+ int oldLoop = 0;
+ int newLoop = 0;
+
+ YCHAR *alt_newpath=NULL;
+
+ if(!oldPath || !newPath){
+ yaffsfs_SetError(-EFAULT);
+ return -1;
+ }
+
+ if(yaffsfs_CheckPath(oldPath) < 0 ||
+ yaffsfs_CheckPath(newPath) < 0){
+ yaffsfs_SetError(-ENAMETOOLONG);
+ return -1;
+ }
+
+ if(yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0){
+ yaffsfs_SetError(-ENOMEM);
+ return -1;
+ }
+ if(alt_newpath)
+ newPath = alt_newpath;
+
+ yaffsfs_Lock();
+
+
+ olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0,¬OldDir,&oldLoop);
+ newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0,¬NewDir,&newLoop);
+ obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL,NULL,NULL);
+ newobj = yaffsfs_FindObject(NULL,newPath,0,0,NULL,NULL,NULL);
+
+ /* If the object being renamed is a directory and the
+ * path ended with a "/" then the olddir == obj.
+ * We pass through NULL for the old name to tell the lower layers
+ * to use olddir as the object.
+ */
+
+ if(olddir == obj)
+ oldname = NULL;
+
+ if((!olddir && notOldDir) || (!newdir && notNewDir)) {
+ yaffsfs_SetError(-ENOTDIR);
+ rename_allowed = 0;
+ } else if(oldLoop || newLoop) {
+ yaffsfs_SetError(-ELOOP);
+ rename_allowed = 0;
+ } else if (olddir && oldname && strncmp(oldname, _Y("."),2) == 0){
+ yaffsfs_SetError(-EINVAL);
+ rename_allowed = 0;
+ }else if(!olddir || !newdir || !obj) {
+ yaffsfs_SetError(-ENOENT);
+ rename_allowed = 0;
+ } else if(obj->my_dev->read_only){
+ yaffsfs_SetError(-EROFS);
+ rename_allowed = 0;
+ } else if(yaffs_is_non_empty_dir(newobj)){
+ yaffsfs_SetError(-ENOTEMPTY);
+ rename_allowed = 0;
+ } else if(olddir->my_dev != newdir->my_dev) {
+ /* Rename must be on same device */
+ yaffsfs_SetError(-EXDEV);
+ rename_allowed = 0;
+ } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
+ /*
+ * It is a directory, check that it is not being renamed to
+ * being its own decendent.
+ * Do this by tracing from the new directory back to the root,
+ * checking for obj
+ */
+
+ struct yaffs_obj *xx = newdir;
+
+ while( rename_allowed && xx){
+ if(xx == obj)
+ rename_allowed = 0;
+ xx = xx->parent;