Skip to content

Commit

Permalink
drgn.helpers.linux.kernfs: Add follow_symlinks support
Browse files Browse the repository at this point in the history
Extend kernfs_walk() to support symlink.

Signed-off-by: Kuan-Ying Lee <[email protected]>
  • Loading branch information
kylee0215 committed Oct 29, 2024
1 parent 393792d commit c1f24fa
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions drgn/helpers/linux/kernfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,33 @@ def kernfs_path(kn: Object) -> bytes:
return b"/".join(names)


def kernfs_walk(parent: Object, path: Path) -> Object:
def kernfs_walk(parent: Object, path: Path, follow_symlinks: bool = False) -> Object:
"""
Find the kernfs node with the given path from the given parent kernfs node.
:param parent: ``struct kernfs_node *``
:param path: Path name.
:param follow_symlinks: If follow_symlinks is ``False``, and the
last component of a path is a symlink, the function will
return ``struct kernfs_node *`` of the symbolic link.
:return: ``struct kernfs_node *`` (``NULL`` if not found)
"""
kernfs_nodep_type = parent.type_
kernfs_node_type = kernfs_nodep_type.type
link_flag = parent.prog_.constant("KERNFS_LINK")
for name in os.fsencode(path).split(b"/"):
if not name:
continue

for parent in rbtree_inorder_for_each_entry(
kernfs_node_type, parent.dir.children.address_of_(), "rb"
):
for parent in kernfs_children(parent):
if (
parent.name.string_() == name
and not parent.ns # For now, we don't bother with namespaced kernfs nodes.
):
break
else:
return NULL(parent.prog_, kernfs_nodep_type)
if parent.flags & link_flag and follow_symlinks:
parent = parent.symlink.target_kn
return parent


Expand Down

0 comments on commit c1f24fa

Please sign in to comment.