+ 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.
+ */
+
+ 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 &&
+ yaffs_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 (rename_file_over_dir(obj, newobj)) {
+ yaffsfs_SetError(-EISDIR);
+ rename_allowed = 0;
+ } else if (rename_dir_over_file(obj, newobj)) {
+ yaffsfs_SetError(-ENOTDIR);
+ 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;