Further extensions for VxWorks wrapper
[yaffs2.git] / direct / yaffsfs.c
index cdde7abe4a549df817287d64390c87c083a78fc9..3cb11a92a3c4675ec4220cad03e4378625168ec1 100644 (file)
@@ -346,8 +346,10 @@ static int yaffsfs_PutFileDes(int fdId)
                fd = &yaffsfs_fd[fdId];
                fd->handleCount--;
                if (fd->handleCount < 1) {
-                       if (fd->isDir)
+                       if (fd->isDir) {
                                yaffsfs_closedir_no_lock(fd->v.dir);
+                               fd->v.dir = NULL;
+                       }
                        if (fd->inodeId >= 0) {
                                yaffsfs_PutInode(fd->inodeId);
                                fd->inodeId = -1;
@@ -747,7 +749,20 @@ static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
        if (dirOut)
                *dirOut = dir;
 
-       if (dir && *name)
+       /* At this stage we have looked up directory part and have the name part
+        * in name if there is one.
+        *
+        *  eg /nand/x/ will give us a name of ""
+        *     /nand/x will give us a name of "x"
+        *
+        * Since the name part might be "." or ".." which need to be fixed.
+        */
+       if (dir && (yaffs_strcmp(name, _Y("..")) == 0)) {
+               dir = dir->parent;
+               obj = dir;
+       } else if (dir && (yaffs_strcmp(name, _Y(".")) == 0))
+               obj = dir;
+       else if (dir && *name)
                obj = yaffs_find_by_name(dir, name);
        else
                obj = dir;
@@ -823,7 +838,7 @@ int yaffs_open_sharing_reldir(struct yaffs_obj *reldir, const YCHAR *path,
        int notDir = 0;
        int loop = 0;
        int is_dir = 0;
-       yaffs_DIR *dsc;
+       yaffs_DIR *dsc = NULL;
 
        if (yaffsfs_CheckMemRegion(path, 0, 0)< 0) {
                yaffsfs_SetError(-EFAULT);
@@ -840,7 +855,7 @@ int yaffs_open_sharing_reldir(struct yaffs_obj *reldir, const YCHAR *path,
                oflag &= ~(O_EXCL);
 
        /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
-       if ((oflag & O_CREAT) & (oflag & O_EXCL))
+       if ((oflag & O_CREAT) && (oflag & O_EXCL))
                oflag &= ~(O_TRUNC);
 
        /* Todo: Are there any more flag combos to sanitise ? */
@@ -856,7 +871,7 @@ int yaffs_open_sharing_reldir(struct yaffs_obj *reldir, const YCHAR *path,
 
        if (handle < 0) {
                yaffsfs_SetError(-ENFILE);
-               errorReported = 1;
+               errorReported = __LINE__;
        } else {
 
                fd = yaffsfs_HandleToFileDes(handle);
@@ -879,19 +894,18 @@ int yaffs_open_sharing_reldir(struct yaffs_obj *reldir, const YCHAR *path,
 
                        /* A directory can't be opened except for read */
                        if ( is_dir &&
-                           (writeRequested || !readRequested ||
-                               (oflag & ~O_RDONLY))) {
-                               openDenied = 1;
+                           (writeRequested || !readRequested || rwflags != O_RDONLY)) {
+                               openDenied = __LINE__;
                                yaffsfs_SetError(-EISDIR);
-                               errorReported = 1;
+                               errorReported = __LINE__;
                        }
 
                        if(is_dir) {
                                dsc = yaffsfs_opendir_reldir_no_lock(reldir, path);
                                if (!dsc) {
-                                       openDenied = 1;
+                                       openDenied = __LINE__;
                                        yaffsfs_SetError(-ENFILE);
-                                       errorReported = 1;
+                                       errorReported = __LINE__;
                                }
                        }
 
@@ -900,28 +914,28 @@ int yaffs_open_sharing_reldir(struct yaffs_obj *reldir, const YCHAR *path,
                         */
                        if (!errorReported &&
                            (oflag & O_EXCL) && (oflag & O_CREAT)) {
-                               openDenied = 1;
+                               openDenied = __LINE__;
                                yaffsfs_SetError(-EEXIST);
-                               errorReported = 1;
+                               errorReported = __LINE__;
                        }
 
                        /* Check file permissions */
-                       if (readRequested && !(obj->yst_mode & S_IREAD))
-                               openDenied = 1;
+                       if (readRequested && !(obj->yst_mode & S_IRUSR))
+                               openDenied = __LINE__;
 
-                       if (writeRequested && !(obj->yst_mode & S_IWRITE))
-                               openDenied = 1;
+                       if (writeRequested && !(obj->yst_mode & S_IWUSR))
+                               openDenied = __LINE__;
 
                        if (!errorReported && writeRequested &&
                            obj->my_dev->read_only) {
-                               openDenied = 1;
+                               openDenied = __LINE__;
                                yaffsfs_SetError(-EROFS);
-                               errorReported = 1;
+                               errorReported = __LINE__;
                        }
 
                        if (openDenied && !errorReported) {
                                yaffsfs_SetError(-EACCES);
-                               errorReported = 1;
+                               errorReported = __LINE__;
                        }
 
                        /* Check sharing of an existing object. */
@@ -954,9 +968,9 @@ int yaffs_open_sharing_reldir(struct yaffs_obj *reldir, const YCHAR *path,
                                    (!shareRead && alreadyReading) ||
                                    (!sharedWriteAllowed && writeRequested) ||
                                    (!shareWrite && alreadyWriting)) {
-                                       openDenied = 1;
+                                       openDenied = __LINE__;
                                        yaffsfs_SetError(-EBUSY);
-                                       errorReported = 1;
+                                       errorReported = __LINE__;
                                }
                        }
 
@@ -970,13 +984,13 @@ int yaffs_open_sharing_reldir(struct yaffs_obj *reldir, const YCHAR *path,
                                                    &notDir, &loop);
                        if (!dir && notDir) {
                                yaffsfs_SetError(-ENOTDIR);
-                               errorReported = 1;
+                               errorReported = __LINE__;
                        } else if (loop) {
                                yaffsfs_SetError(-ELOOP);
-                               errorReported = 1;
+                               errorReported = __LINE__;
                        } else if (!dir) {
                                yaffsfs_SetError(-ENOENT);
-                               errorReported = 1;
+                               errorReported = __LINE__;
                        }
                }
 
@@ -984,22 +998,22 @@ int yaffs_open_sharing_reldir(struct yaffs_obj *reldir, const YCHAR *path,
                        /* Let's see if we can create this file */
                        if (dir->my_dev->read_only) {
                                yaffsfs_SetError(-EROFS);
-                               errorReported = 1;
+                               errorReported = __LINE__;
                        } else if (yaffsfs_TooManyObjects(dir->my_dev)) {
                                yaffsfs_SetError(-ENFILE);
-                               errorReported = 1;
+                               errorReported = __LINE__;
                        } else
                                obj = yaffs_create_file(dir, name, mode, 0, 0);
 
                        if (!obj && !errorReported) {
                                yaffsfs_SetError(-ENOSPC);
-                               errorReported = 1;
+                               errorReported = __LINE__;
                        }
                }
 
                if (!obj && dir && !errorReported && !(oflag & O_CREAT)) {
                        yaffsfs_SetError(-ENOENT);
-                       errorReported = 1;
+                       errorReported = __LINE__;
                }
 
                if (obj && !openDenied) {
@@ -1032,6 +1046,9 @@ int yaffs_open_sharing_reldir(struct yaffs_obj *reldir, const YCHAR *path,
                        if (!is_dir && (oflag & O_TRUNC) && fd->writing)
                                yaffs_resize_file(obj, 0);
                } else {
+                       if (dsc)
+                               yaffsfs_closedir_no_lock(dsc);
+                       dsc = NULL;
                        yaffsfs_PutHandle(handle);
                        if (!errorReported)
                                yaffsfs_SetError(0);    /* Problem */
@@ -1576,6 +1593,56 @@ int yaffs_unlink(const YCHAR *path)
        return yaffs_unlink_reldir(NULL, path);
 }
 
+int yaffs_funlink(int fd)
+{
+       struct yaffs_obj *obj;
+       int retVal = -1;
+
+       yaffsfs_Lock();
+       obj = yaffsfs_HandleToObject(fd);
+
+       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 &&
@@ -2540,11 +2607,11 @@ int yaffs_access_reldir(struct yaffs_obj *reldir, const YCHAR *path, int amode)
        else {
                int access_ok = 1;
 
-               if ((amode & R_OK) && !(obj->yst_mode & S_IREAD))
+               if ((amode & R_OK) && !(obj->yst_mode & S_IRUSR))
                        access_ok = 0;
-               if ((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
+               if ((amode & W_OK) && !(obj->yst_mode & S_IWUSR))
                        access_ok = 0;
-               if ((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
+               if ((amode & X_OK) && !(obj->yst_mode & S_IXUSR))
                        access_ok = 0;
 
                if (!access_ok)
@@ -2850,7 +2917,9 @@ int yaffs_mount(const YCHAR *path)
        return yaffs_mount_common(NULL, path, 0, 0);
 }
 
-int yaffs_sync_common(struct yaffs_dev *dev, const YCHAR *path)
+static int yaffs_sync_common(struct yaffs_dev *dev,
+                            const YCHAR *path,
+                            int do_checkpt)
 {
        int retVal = -1;
        YCHAR *dummy;
@@ -2879,7 +2948,8 @@ int yaffs_sync_common(struct yaffs_dev *dev, const YCHAR *path)
                else {
 
                        yaffs_flush_whole_cache(dev, 0);
-                       yaffs_checkpoint_save(dev);
+                       if (do_checkpt)
+                               yaffs_checkpoint_save(dev);
                        retVal = 0;
 
                }
@@ -2890,14 +2960,24 @@ int yaffs_sync_common(struct yaffs_dev *dev, const YCHAR *path)
        return retVal;
 }
 
+int yaffs_sync_files_reldev(struct yaffs_dev *dev)
+{
+       return yaffs_sync_common(dev, NULL, 0);
+}
+
+int yaffs_sync_files(const YCHAR *path)
+{
+       return yaffs_sync_common(NULL, path, 0);
+}
+
 int yaffs_sync_reldev(struct yaffs_dev *dev)
 {
-       return yaffs_sync_common(dev, NULL);
+       return yaffs_sync_common(dev, NULL, 1);
 }
 
 int yaffs_sync(const YCHAR *path)
 {
-       return yaffs_sync_common(NULL, path);
+       return yaffs_sync_common(NULL, path, 1);
 }
 
 
@@ -3588,6 +3668,7 @@ static int yaffsfs_closedir_no_lock(yaffs_DIR *dirp)
 
        return 0;
 }
+
 int yaffs_closedir(yaffs_DIR *dirp)
 {
        int ret;
@@ -3834,6 +3915,11 @@ int yaffs_set_error(int error)
        return 0;
 }
 
+struct yaffs_obj * yaffs_get_obj_from_fd(int handle)
+{
+       return yaffsfs_HandleToObject(handle);
+}
+
 int yaffs_dump_dev_reldir(struct yaffs_obj *reldir, const YCHAR *path)
 {
 #if 1