|
| 1 | +# Virtual filesystem definition |
| 2 | +virtual_fs = { |
| 3 | + "/": {"bin": {}, "boot": {}, "dev": {}, "etc": {}, "home": {}, "lib": {}, "media": {}, "mnt": {}, "opt": {}, "sbin": {}, "srv": {}, "tmp": {}, "usr": {}, "proc": {}}, |
| 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 | + return |
| 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 | +def ls(): |
| 29 | + """List the contents of the current directory.""" |
| 30 | + contents = virtual_fs.get(current_path, {}) |
| 31 | + for item in contents: |
| 32 | + print(item) |
| 33 | + |
| 34 | +def cat(filename): |
| 35 | + """Simulate opening and reading a file.""" |
| 36 | + global current_path |
| 37 | + current_dir_contents = virtual_fs.get(current_path, {}) |
| 38 | + |
| 39 | + if filename in current_dir_contents: |
| 40 | + if isinstance(current_dir_contents[filename], str): |
| 41 | + print(current_dir_contents[filename]) |
| 42 | + else: |
| 43 | + print(f"cat: {filename}: Is a directory") |
| 44 | + else: |
| 45 | + print(f"cat: {filename}: No such file or directory") |
| 46 | + |
| 47 | +def edit(filename, new_content): |
| 48 | + """Simulate editing the content of a file.""" |
| 49 | + global current_path |
| 50 | + current_dir_contents = virtual_fs.get(current_path, {}) |
| 51 | + |
| 52 | + if filename in current_dir_contents: |
| 53 | + if isinstance(current_dir_contents[filename], str): |
| 54 | + # Update the content of the file |
| 55 | + current_dir_contents[filename] = new_content |
| 56 | + print(f"File '{filename}' has been updated.") |
| 57 | + else: |
| 58 | + print(f"edit: {filename}: Is a directory") |
| 59 | + else: |
| 60 | + print(f"edit: {filename}: No such file or directory") |
| 61 | + |
| 62 | +def pwd(): |
| 63 | + print(current_path) |
| 64 | + |
| 65 | +def mkdir(directory_name): |
| 66 | + """Create a new directory in the current directory.""" |
| 67 | + global current_path |
| 68 | + |
| 69 | + # Get the current directory's contents |
| 70 | + current_dir_contents = virtual_fs.get(current_path, {}) |
| 71 | + |
| 72 | + # Check if the directory already exists |
| 73 | + if directory_name in current_dir_contents: |
| 74 | + print(f"mkdir: cannot create directory '{directory_name}': File exists") |
| 75 | + else: |
| 76 | + # Create the new directory as an empty dictionary |
| 77 | + current_dir_contents[directory_name] = {} |
| 78 | + print(f"Directory '{directory_name}' created.") |
| 79 | + |
| 80 | +def touch(filename): |
| 81 | + """Create a new empty text file in the current directory.""" |
| 82 | + global current_path |
| 83 | + |
| 84 | + # Get the current directory's contents |
| 85 | + current_dir_contents = virtual_fs.get(current_path, {}) |
| 86 | + |
| 87 | + # Check if the file already exists |
| 88 | + if filename in current_dir_contents: |
| 89 | + print(f"touch: cannot create file '{filename}': File exists") |
| 90 | + else: |
| 91 | + # Create the new file with empty content (represented as an empty string) |
| 92 | + current_dir_contents[filename] = "" |
| 93 | + print(f"File '{filename}' created.") |
0 commit comments