#K72832. In-Memory File System
In-Memory File System
In-Memory File System
Design and implement a simple in-memory file system that supports the following operations:
- mkdir(path: str): Create a directory at the specified path.
- addContentToFile(path: str, content: str): Append content to a file at the specified path. If the file does not exist, create it.
- readContentFromFile(path: str): Return the content of the file.
- ls(path: str): If
path
is a file, return a list containing only the file's name; if it is a directory, return the list of files and subdirectories in lexicographical order.
The file system path follows a Unix-like format with '/' as the directory separator. In mathematical terms, a valid path can be represented as: \( path = '/'(\text{dir}/)* (\text{file})? \).
Your solution should process input via stdin
and produce output on stdout
as described in the input/output sections.
inputFormat
The input is provided via stdin
. The first line contains an integer N representing the number of operations. Each of the next N lines contains one operation command in one of the following formats:
mkdir <path>
add <path> "<content>"
(the content is enclosed in double quotes)read <path>
ls <path>
Note: For operations that output a result (i.e. read
and ls
), each output should be printed on a separate line in the order the operations occur.
outputFormat
For each read
or ls
operation, output the result on a separate line to stdout
. For a read
command, print the content of the file. For a ls
command, print the names (sorted lexicographically if it is a directory) separated by a single space.
6
mkdir /a/b/c
add /a/b/c/d "hello"
read /a/b/c/d
add /a/b/c/d " world"
read /a/b/c/d
ls /a/b/c
hello
hello world
d
</p>