Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File tree maxdepth #587

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
52 changes: 47 additions & 5 deletions tavern/tomes/file_tree/main.eldritch
Original file line number Diff line number Diff line change
@@ -1,33 +1,75 @@
block_list = ["/proc","/sys","/lib","/libx32","/lib32","/lib64","/boot","/srv","/usr","/snap","/run","/dev","/cores"]

usernfo = sys.get_user()
windows = sys.get_os().get("platform", "") == "PLATFORM_WINDOWS"

def file_list(path,tree):
def can_read(f):
"""Return true if the user can read this dir/file
"""
# Bypass until windows perms are implemented
if windows:
return True

PERM_READ = 4
f_user = int(f["permissions"][-3]) # User byte
f_group = int(f["permissions"][-2]) # Group byte

# Check world byte first so it hopefully is fast
if int(f["permissions"][-1]) & PERM_READ:
return True

# Are we root?
root = usernfo["euid"]["uid"] == 0

# If the user isnt root and the user doesnt own the file, clear the user byte
if not root and f["owner"] not in (usernfo["euid"]["name"], usernfo["uid"]["name"]):
f_user = 0

# TODO: https://github.com/spellshift/realm/issues/570
# Will NOT match any group other than primary until #570 is fixed

# If the user isnt root and the group doesnt own the file, clear the group byte
if not root and f["group"] not in (str(usernfo["egid"]), str(usernfo["gid"])):
f_group = 0

if (f_group & PERM_READ) | (f_user & PERM_READ):
return True
return False

def file_list(path,tree, depth=-1):
tree="|\t"+tree
if depth == 0:
return
files = file.list(path)
for f in files:
if path+f['file_name'] in block_list:
print("Skipping: "+path+f['file_name']+"\n")
continue
if f['type'] == "Directory":
print(tree+"|---"+path+"/"+f['file_name']+"\n")
file_list(path+"/"+f['file_name'],tree)
if can_read(f):
file_list(path+"/"+f['file_name'],tree, depth=depth-1)
if f['type'] == "Link":
print(tree+"|---"+f['file_name']+"\n")
if f['type'] == "File":
print(tree+"|---"+f['file_name']+"\n")

def main(path):
def main(path, depth=-1):
tree=""
if isinstance(depth, str) and not depth.lstrip("-").isdigit():
hulto marked this conversation as resolved.
Show resolved Hide resolved
print("ERROR: Invalid depth specified, must be a valid number")
return
depth=int(depth)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seem redundant with the above

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean redundant? -1 is not handled properly by the is digit function. This also gracefully exits rather than just crashing

if file.is_dir(path):
print(path+"\n")
if path == "/":
print("It looks like you're trying to list every file on the system.\n")
print("This generates a lot of data so I'm going to exclude less helpful directories\n")
print("If you really really want everything including /proc and /sys specify \"//\"\n")
file_list(path,tree)
file_list(path,tree, depth=depth)
elif file.is_file(path):
print("Error: Invalid Path ("+path+")\n")

main(input_params['path'])
main(input_params['path'], input_params["depth"])
nullmonk marked this conversation as resolved.
Show resolved Hide resolved
nullmonk marked this conversation as resolved.
Show resolved Hide resolved
print("\n")
print("\n")
4 changes: 4 additions & 0 deletions tavern/tomes/file_tree/metadata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ paramdefs:
type: string
label: File path
placeholder: "/etc/"
- name: depth
type: int
nullmonk marked this conversation as resolved.
Show resolved Hide resolved
label: Recurse Depth
placeholder: "3, or -1 for infinite recursion"