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 2016/03/30 06:13:32 UTC

incubator-mynewt-core git commit: MYNEWT-65 NFFS - Don't move ancestor into desc.

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 1c20ff3dc -> 0b90abf71


MYNEWT-65 NFFS - Don't move ancestor into desc.

MYNEWT-65 NFFS - Don't allow a parent directory to be moved into a child directory

This operation will cause a loop in the inode tree. An attempt to perform this operation should fail with an FS_EINVAL error code.


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

Branch: refs/heads/develop
Commit: 0b90abf712659378fa6137dd6cfcb025ed0a5664
Parents: 1c20ff3
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 29 21:11:30 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 29 21:13:16 2016 -0700

----------------------------------------------------------------------
 fs/nffs/src/nffs_inode.c              | 56 ++++++++++++++++++++++++++++--
 fs/nffs/src/test/arch/sim/nffs_test.c | 10 ++++++
 2 files changed, 64 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0b90abf7/fs/nffs/src/nffs_inode.c
----------------------------------------------------------------------
diff --git a/fs/nffs/src/nffs_inode.c b/fs/nffs/src/nffs_inode.c
index aa7accf..083b5e8 100644
--- a/fs/nffs/src/nffs_inode.c
+++ b/fs/nffs/src/nffs_inode.c
@@ -412,18 +412,70 @@ nffs_inode_delete_from_disk(struct nffs_inode *inode)
     return 0;
 }
 
+/**
+ * Determines if an inode is an ancestor of another.
+ *
+ * NOTE: If the two inodes are equal, a positive result is indicated.
+ *
+ * @param anc                   The potential ancestor.
+ * @param des                   The potential descendent.
+ * @param out_is_ancestor       On success, the result gets written here
+ *                                  (1 if an ancestor relationship is
+ *                                  detected).
+ *
+ * @return                      0 on success; FS_E[...] on error.
+ */
+static int
+nffs_inode_is_ancestor(struct nffs_inode_entry *anc,
+                       struct nffs_inode_entry *des,
+                       int *out_is_ancestor)
+{
+    struct nffs_inode_entry *cur;
+    struct nffs_inode inode;
+    int rc;
+
+    /* Only directories can be ancestors. */
+    if (!nffs_hash_id_is_dir(anc->nie_hash_entry.nhe_id)) {
+        *out_is_ancestor = 0;
+        return 0;
+    }
+
+    /* Trace des's heritage until we hit anc or there are no more parents. */
+    cur = des;
+    while (cur != NULL && cur != anc) {
+        rc = nffs_inode_from_entry(&inode, cur);
+        if (rc != 0) {
+            return rc;
+        }
+        cur = inode.ni_parent;
+    }
+
+    *out_is_ancestor = cur == anc;
+    return 0;
+}
+
 int
 nffs_inode_rename(struct nffs_inode_entry *inode_entry,
-                 struct nffs_inode_entry *new_parent,
-                 const char *new_filename)
+                  struct nffs_inode_entry *new_parent,
+                  const char *new_filename)
 {
     struct nffs_disk_inode disk_inode;
     struct nffs_inode inode;
     uint32_t area_offset;
     uint8_t area_idx;
     int filename_len;
+    int ancestor;
     int rc;
 
+    /* Don't allow a directory to be moved into a descendent directory. */
+    rc = nffs_inode_is_ancestor(inode_entry, new_parent, &ancestor);
+    if (rc != 0) {
+        return rc;
+    }
+    if (ancestor) {
+        return FS_EINVAL;
+    }
+
     rc = nffs_inode_from_entry(&inode, inode_entry);
     if (rc != 0) {
         return rc;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/0b90abf7/fs/nffs/src/test/arch/sim/nffs_test.c
----------------------------------------------------------------------
diff --git a/fs/nffs/src/test/arch/sim/nffs_test.c b/fs/nffs/src/test/arch/sim/nffs_test.c
index 5e2d462..42a7623 100644
--- a/fs/nffs/src/test/arch/sim/nffs_test.c
+++ b/fs/nffs/src/test/arch/sim/nffs_test.c
@@ -827,6 +827,9 @@ TEST_CASE(nffs_test_rename)
     rc = fs_mkdir("/mydir");
     TEST_ASSERT(rc == 0);
 
+    rc = fs_mkdir("/mydir/leafdir");
+    TEST_ASSERT(rc == 0);
+
     rc = fs_rename("/myfile2.txt", "/mydir/myfile2.txt");
     TEST_ASSERT(rc == 0);
 
@@ -837,6 +840,10 @@ TEST_CASE(nffs_test_rename)
     rc = fs_rename("/mydir", "badname");
     TEST_ASSERT(rc == FS_EINVAL);
 
+    /* Don't allow a directory to be moved into a descendent directory. */
+    rc = fs_rename("/mydir", "/mydir/leafdir/a");
+    TEST_ASSERT(rc == FS_EINVAL);
+
     rc = fs_rename("/mydir", "/mydir2");
     TEST_ASSERT(rc == 0);
 
@@ -851,6 +858,9 @@ TEST_CASE(nffs_test_rename)
                 .filename = "mydir2",
                 .is_dir = 1,
                 .children = (struct nffs_test_file_desc[]) { {
+                    .filename = "leafdir",
+                    .is_dir = 1,
+                }, {
                     .filename = "myfile2.txt",
                     .contents = "contents",
                     .contents_len = 9,