You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2015/12/02 20:07:01 UTC

incubator-mynewt-larva git commit: 2 nffs bug fixes: open() and opendir()

Repository: incubator-mynewt-larva
Updated Branches:
  refs/heads/master 770f4f28d -> d2bbd08a3


2 nffs bug fixes: open() and opendir()

Both of these functions incorrectly reported success when fed a path of the
wrong file type.  Now:
    * nffs_open() returns NFFS_EINVAL if passed a directory path.
    * nffs_opendir() returns NFFS_EINVAL if passed a file path.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/d2bbd08a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/d2bbd08a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/d2bbd08a

Branch: refs/heads/master
Commit: d2bbd08a3a9ef4af3b05c5df1ef9db4696f02f26
Parents: 770f4f2
Author: Christopher Collins <cc...@gmail.com>
Authored: Wed Dec 2 11:06:49 2015 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Wed Dec 2 11:06:49 2015 -0800

----------------------------------------------------------------------
 libs/nffs/src/nffs_dir.c                | 16 ++++++---
 libs/nffs/src/nffs_file.c               | 11 +++---
 libs/nffs/src/test/arch/sim/nffs_test.c | 53 +++++++++++++++++++++++++++-
 3 files changed, 70 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/d2bbd08a/libs/nffs/src/nffs_dir.c
----------------------------------------------------------------------
diff --git a/libs/nffs/src/nffs_dir.c b/libs/nffs/src/nffs_dir.c
index cc69133..ea2817c 100644
--- a/libs/nffs/src/nffs_dir.c
+++ b/libs/nffs/src/nffs_dir.c
@@ -50,19 +50,25 @@ nffs_dir_free(struct nffs_dir *dir)
 int
 nffs_dir_open(const char *path, struct nffs_dir **out_dir)
 {
+    struct nffs_inode_entry *parent_inode_entry;
     struct nffs_dir *dir;
     int rc;
 
+    rc = nffs_path_find_inode_entry(path, &parent_inode_entry);
+    if (rc != 0) {
+        return rc;
+    }
+
+    if (!nffs_hash_id_is_dir(parent_inode_entry->nie_hash_entry.nhe_id)) {
+        return NFFS_EINVAL;
+    }
+
     dir = nffs_dir_alloc();
     if (dir == NULL) {
         return NFFS_ENOMEM;
     }
 
-    rc = nffs_path_find_inode_entry(path, &dir->nd_parent_inode_entry);
-    if (rc != 0) {
-        return rc;
-    }
-
+    dir->nd_parent_inode_entry = parent_inode_entry;
     dir->nd_parent_inode_entry->nie_refcnt++;
     memset(&dir->nd_dirent, 0, sizeof dir->nd_dirent);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/d2bbd08a/libs/nffs/src/nffs_file.c
----------------------------------------------------------------------
diff --git a/libs/nffs/src/nffs_file.c b/libs/nffs/src/nffs_file.c
index c01753c..f7a1247 100644
--- a/libs/nffs/src/nffs_file.c
+++ b/libs/nffs/src/nffs_file.c
@@ -191,15 +191,15 @@ nffs_file_open(struct nffs_file **out_file, const char *path,
 
         /* Create a new file at the specified path. */
         rc = nffs_file_new(parent, parser.npp_token, parser.npp_token_len, 0,
-                          &file->nf_inode_entry);
+                           &file->nf_inode_entry);
         if (rc != 0) {
             goto err;
         }
-    } else {
+    } else if (rc == 0) {
         /* The file already exists. */
 
         /* Reject an attempt to open a directory. */
-        if (parser.npp_token_type != NFFS_PATH_TOKEN_LEAF) {
+        if (nffs_hash_id_is_dir(inode->nie_hash_entry.nhe_id)) {
             rc = NFFS_EINVAL;
             goto err;
         }
@@ -210,7 +210,7 @@ nffs_file_open(struct nffs_file **out_file, const char *path,
              */
             nffs_path_unlink(path);
             rc = nffs_file_new(parent, parser.npp_token, parser.npp_token_len,
-                              0, &file->nf_inode_entry);
+                               0, &file->nf_inode_entry);
             if (rc != 0) {
                 goto err;
             }
@@ -220,6 +220,9 @@ nffs_file_open(struct nffs_file **out_file, const char *path,
              */
             file->nf_inode_entry = inode;
         }
+    } else {
+        /* Invalid path. */
+        goto err;
     }
 
     if (access_flags & NFFS_ACCESS_APPEND) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/d2bbd08a/libs/nffs/src/test/arch/sim/nffs_test.c
----------------------------------------------------------------------
diff --git a/libs/nffs/src/test/arch/sim/nffs_test.c b/libs/nffs/src/test/arch/sim/nffs_test.c
index 40bef79..324fbdd 100644
--- a/libs/nffs/src/test/arch/sim/nffs_test.c
+++ b/libs/nffs/src/test/arch/sim/nffs_test.c
@@ -993,11 +993,57 @@ TEST_CASE(nffs_test_read)
     TEST_ASSERT(rc == 0);
 }
 
-TEST_CASE(nffs_test_overwrite_one)
+TEST_CASE(nffs_test_open)
 {
     struct nffs_file *file;
     int rc;
 
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    /*** Fail to open an invalid path (not rooted). */
+    rc = nffs_open("file", NFFS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == NFFS_EINVAL);
+
+    /*** Fail to open a directory (root directory). */
+    rc = nffs_open("/", NFFS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == NFFS_EINVAL);
+
+    /*** Fail to open a nonexistent file for reading. */
+    rc = nffs_open("/1234", NFFS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == NFFS_ENOENT);
+
+    rc = nffs_mkdir("/dir");
+    TEST_ASSERT(rc == 0);
+
+    /*** Fail to open a directory. */
+    rc = nffs_open("/dir", NFFS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == NFFS_EINVAL);
+
+    /*** Successfully open an existing file for reading. */
+    nffs_test_util_create_file("/dir/file.txt", "1234567890", 10);
+    rc = nffs_open("/dir/file.txt", NFFS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == 0);
+    rc = nffs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    /*** Successfully open an nonexistent file for writing. */
+    rc = nffs_open("/dir/file2.txt", NFFS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    rc = nffs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    /*** Ensure the file can be reopened. */
+    rc = nffs_open("/dir/file.txt", NFFS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == 0);
+    rc = nffs_close(file);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(nffs_test_overwrite_one)
+{
+    struct nffs_file *file;
+    int rc;
 
     /*** Setup. */
     rc = nffs_format(nffs_area_descs);
@@ -2201,6 +2247,10 @@ TEST_CASE(nffs_test_readdir)
     rc = nffs_opendir("/asdf", &dir);
     TEST_ASSERT(rc == NFFS_ENOENT);
 
+    /* Fail to opendir a file. */
+    rc = nffs_opendir("/mydir/a", &dir);
+    TEST_ASSERT(rc == NFFS_EINVAL);
+
     /* Real directory (with trailing slash). */
     rc = nffs_opendir("/mydir/", &dir);
     TEST_ASSERT_FATAL(rc == 0);
@@ -2307,6 +2357,7 @@ nffs_test_gen(void)
     nffs_test_truncate();
     nffs_test_append();
     nffs_test_read();
+    nffs_test_open();
     nffs_test_overwrite_one();
     nffs_test_overwrite_two();
     nffs_test_overwrite_three();