|
| 1 | +# Virtual filesystem definition |
| 2 | +virtual_fs = { |
| 3 | + "/": {"home": {}, "var": {}, "tmp": {}}, |
| 4 | + "/home": {"user": {}}, |
| 5 | + "/home/user": {"file1.txt": "HelloWorld!", "documents": {}}, |
| 6 | + "/var": {}, |
| 7 | + "/tmp": {}, |
| 8 | +} |
| 9 | + |
| 10 | +current_path = "/" |
| 11 | + |
| 12 | +def cd(path=None): |
| 13 | + """Change the current directory in the virtual filesystem.""" |
| 14 | + global current_path |
| 15 | + if not path or path.strip() == "": |
| 16 | + print(current_path) |
| 17 | + elif path == "..": |
| 18 | + if current_path != "/": |
| 19 | + current_path = "/".join(current_path.rstrip("/").split("/")[:-1]) |
| 20 | + if current_path == "": |
| 21 | + current_path = "/" |
| 22 | + elif path in virtual_fs.get(current_path, {}): |
| 23 | + if isinstance(virtual_fs[current_path][path], dict): |
| 24 | + current_path = current_path.rstrip("/") + "/" + path |
| 25 | + else: |
| 26 | + print(f"cd: no such file or directory: {path}") |
| 27 | + |
| 28 | + |
| 29 | +def ls(): |
| 30 | + """List the contents of the current directory.""" |
| 31 | + contents = virtual_fs.get(current_path, {}) |
| 32 | + for item in contents: |
| 33 | + print(item) |
| 34 | + |
| 35 | +def cat(filename): |
| 36 | + """Simulate opening and reading a file.""" |
| 37 | + global current_path |
| 38 | + current_dir_contents = virtual_fs.get(current_path, {}) |
| 39 | + |
| 40 | + if filename in current_dir_contents: |
| 41 | + if isinstance(current_dir_contents[filename], str): |
| 42 | + print(current_dir_contents[filename]) |
| 43 | + else: |
| 44 | + print(f"cat: {filename}: Is a directory") |
| 45 | + else: |
| 46 | + print(f"cat: {filename}: No such file or directory") |
| 47 | + |
| 48 | +def edit(filename, new_content): |
| 49 | + """Simulate editing the content of a file.""" |
| 50 | + global current_path |
| 51 | + current_dir_contents = virtual_fs.get(current_path, {}) |
| 52 | + |
| 53 | + if filename in current_dir_contents: |
| 54 | + if isinstance(current_dir_contents[filename], str): |
| 55 | + # Update the content of the file |
| 56 | + current_dir_contents[filename] = new_content |
| 57 | + print(f"File '{filename}' has been updated.") |
| 58 | + else: |
| 59 | + print(f"edit: {filename}: Is a directory") |
| 60 | + else: |
| 61 | + print(f"edit: {filename}: No such file or directory") |
| 62 | + |
| 63 | + |
| 64 | +def print_tree(path, indent=""): |
| 65 | + """Recursively print the directory tree, listing files and subdirectories.""" |
| 66 | + # Get contents of the current path |
| 67 | + contents = virtual_fs.get(path, {}) |
| 68 | + |
| 69 | + # Iterate through the contents of the directory |
| 70 | + for item, content in contents.items(): |
| 71 | + # If the content is a dictionary, it's a directory, so print its name and recurse into it |
| 72 | + if isinstance(content, dict): |
| 73 | + print(f"{indent}{item}/") # Print directory with a slash at the end |
| 74 | + print_tree(f"{path}/{item}", indent + " ") # Recurse into the directory |
| 75 | + else: |
| 76 | + # If it's not a dictionary, it's a file, so print its name |
| 77 | + print(f"{indent}{item}") # Print file name |
| 78 | + |
| 79 | +def tree(): |
| 80 | + """Print the tree of the current directory starting from the root.""" |
| 81 | + print_tree(current_path) |
0 commit comments