#C9532. In-Memory File System Simulation
In-Memory File System Simulation
In-Memory File System Simulation
You are required to implement a simple in-memory file system that supports the following operations:
ADD <path> <size>
: Adds a new file or directory. Ifsize = 0
, the path represents a directory; otherwise it represents a file with the given size.DELETE <path>
: Deletes an existing file or directory. If the path is a directory, all its contents are removed.LIST <path>
: Lists all immediate files and directories within the specified directory. The output should contain the full paths of the children, separated by a space.SIZE <path>
: Returns the total size of the directory, which is the sum of sizes of all nested files (recursively). For files, it simply returns the file size.
If an operation fails because the target path already exists (in case of ADD
) or does not exist (in case of DELETE
, LIST
, or SIZE
), output an error message exactly in the format:
Error: Path 'path' already exists.
for adding an existing path.Error: Path 'path' does not exist.
for operations on non-existing paths.
The file system uses Unix-style paths. All operations are provided via standard input (stdin) and any output or error messages should be printed to standard output (stdout).
Note that the operations and expected outputs follow the rules below. For example, if you add a directory /root
and then a file /root/a.txt
, listing /root
should output:
/root/a.txt followed by any other directory/file added under /root
.
There are no extra spaces or characters in the output.
For any formulas such as in complexity analysis, you can use LaTeX formatting like \(O(n)\) if needed.
inputFormat
The first line of input contains a single integer n indicating the number of operations.
The following n lines each represent an operation in one of the following formats:
ADD <path> <size>
DELETE <path>
LIST <path>
SIZE <path>
outputFormat
For each LIST
and SIZE
operation, output the required result on a new line. If an operation encounters an error, output the corresponding error message.
The results for LIST
should be the full paths of the immediate children of the given directory, separated by a space, and the result for SIZE
should be an integer.
5
ADD /root 0
ADD /root/a.txt 10
ADD /root/subdir 0
ADD /root/subdir/b.txt 20
LIST /root
/root/a.txt /root/subdir