feat: owloysius - dynamic init function invocator
[lunaix-os.git] / lunaix-os / kernel / fs / path_walk.c
index c80d7134d22596d89a7f53addcfb59929c3da8a5..e1b11578145ed1f47d1ddd12cdf7246cf8207247 100644 (file)
@@ -1,5 +1,6 @@
 #include <lunaix/fs.h>
 #include <lunaix/mm/valloc.h>
+#include <lunaix/process.h>
 #include <lunaix/spike.h>
 
 #include <klibc/string.h>
@@ -42,8 +43,8 @@ __vfs_walk(struct v_dnode* start,
     assert(start);
 
     struct v_dnode* dnode;
-    struct v_inode* current_inode;
     struct v_dnode* current_level = start;
+    struct v_inode* current_inode = current_level->inode;
 
     struct hstr name = HSTR(fname_buffer, 0);
 
@@ -79,38 +80,6 @@ __vfs_walk(struct v_dnode* start,
             break;
         }
 
-        current_inode = current_level->inode;
-
-        if ((current_inode->itype & VFS_IFSYMLINK) &&
-            !(walk_options & VFS_WALK_NOFOLLOW)) {
-            const char* link;
-
-            lock_inode(current_inode);
-            if ((errno =
-                   current_inode->ops->read_symlink(current_inode, &link))) {
-                unlock_inode(current_inode);
-                goto error;
-            }
-            unlock_inode(current_inode);
-
-            errno = __vfs_walk(current_level->parent,
-                               link,
-                               &dnode,
-                               NULL,
-                               0,
-                               depth + 1,
-                               fname_buffer + name.len + 1);
-
-            if (errno) {
-                goto error;
-            }
-
-            // reposition the resolved subtree pointed by symlink
-            vfs_dcache_rehash(current_level->parent, dnode);
-            current_level = dnode;
-            current_inode = dnode->inode;
-        }
-
         lock_dnode(current_level);
 
         dnode = vfs_dcache_lookup(current_level, &name);
@@ -148,6 +117,41 @@ __vfs_walk(struct v_dnode* start,
 
         j = 0;
         current_level = dnode;
+        current_inode = current_level->inode;
+
+        if ((current_inode->itype & F_MSLNK) &&
+            !(walk_options & VFS_WALK_NOFOLLOW)) {
+            const char* link;
+
+            if (!current_inode->ops->read_symlink) {
+                errno = ENOTSUP;
+                goto error;
+            }
+
+            lock_inode(current_inode);
+            if ((errno =
+                   current_inode->ops->read_symlink(current_inode, &link))) {
+                unlock_inode(current_inode);
+                goto error;
+            }
+            unlock_inode(current_inode);
+
+            errno = __vfs_walk(current_level->parent,
+                               link,
+                               &dnode,
+                               NULL,
+                               0,
+                               depth + 1,
+                               fname_buffer + name.len + 1);
+
+            if (errno) {
+                goto error;
+            }
+
+            current_level = dnode;
+            current_inode = dnode->inode;
+        }
+
     cont:
         current = lookahead;
     };
@@ -169,6 +173,11 @@ vfs_walk(struct v_dnode* start,
          struct hstr* component,
          int options)
 {
+    if (!path) {
+        *dentry = NULL;
+        return 0;
+    }
+
     // allocate a file name stack for path walking and recursion to resolve
     // symlink
     char* name_buffer = valloc(2048);