+ if (!obj)
+ yaffsfs_SetError(-EBADF);
+ else if (obj->my_dev->read_only)
+ yaffsfs_SetError(-EROFS);
+ else if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
+ !(list_empty(&obj->variant.dir_variant.children)))
+ yaffsfs_SetError(-ENOTEMPTY);
+ else if (obj == obj->my_dev->root_dir)
+ yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
+ else if (yaffs_unlink_obj(obj) == YAFFS_OK)
+ retVal = 0;
+
+ yaffsfs_Unlock();
+
+ return retVal;
+}
+
+int yaffs_fgetfl(int fd, int *flags)
+{
+ struct yaffsfs_FileDes *fdp = yaffsfs_HandleToFileDes(fd);
+ int retVal;
+
+ yaffsfs_Lock();
+
+ if(!flags || !fdp) {
+ yaffsfs_SetError(-EINVAL);
+ retVal = -1;
+ } else {
+ if (fdp->reading && fdp->writing)
+ *flags = O_RDWR;
+ else if (fdp->writing)
+ *flags = O_WRONLY;
+ else
+ *flags = O_RDONLY;
+ retVal = 0;
+ }
+
+ yaffsfs_Unlock();
+ return retVal;
+}
+
+
+static int rename_file_over_dir(struct yaffs_obj *obj, struct yaffs_obj *newobj)
+{
+ if (obj && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY &&
+ newobj && newobj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
+ return 1;
+ else
+ return 0;
+}
+
+static int rename_dir_over_file(struct yaffs_obj *obj, struct yaffs_obj *newobj)
+{
+ if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
+ newobj && newobj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
+ return 1;
+ else
+ return 0;
+}
+
+int yaffs_rename_reldir(struct yaffs_obj *reldir,
+ 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 (yaffsfs_CheckMemRegion(oldPath, 0, 0) < 0 ||
+ yaffsfs_CheckMemRegion(newPath, 0, 0) < 0) {
+ 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(reldir, oldPath, &oldname, 0,
+ ¬OldDir, &oldLoop);
+ newdir = yaffsfs_FindDirectory(reldir, newPath, &newname, 0,
+ ¬NewDir, &newLoop);
+ obj = yaffsfs_FindObject(reldir, oldPath, 0, 0, NULL, NULL, NULL);
+ newobj = yaffsfs_FindObject(reldir, 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.
+ */