Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 49 additions & 10 deletions tavern/tomes/file_tree/main.eldritch
Original file line number Diff line number Diff line change
@@ -1,26 +1,65 @@
block_list = ["/proc", "/sys", "/lib", "/libx32", "/lib32", "/lib64",
"/boot", "/srv", "/usr", "/snap", "/run", "/dev", "/cores"]

usernfo = sys.get_user()
windows = sys.is_windows()

def can_read(f):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Instead of adding can_read lets make file.list fail in a recoverable way similar to the new file.find function

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We'll move forward with it as is and then remove the can_read here once the eldritch change has been made.

"""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

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

print(tree+"|---"+f['file_name'])

def main(path):
tree = ""
def main(path, depth=-1):
tree=""
depth=int(depth)
Copy link
Copy Markdown
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
Copy Markdown
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+"")
if path == "/":
Expand Down
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
label: Recurse Depth
placeholder: "3, or -1 for infinite recursion"
Loading