FileSystem
ls /
mkdir /a/b/c
addContentToFile /a/b/c/d "hello"
ls /
readContentFromFile /a/b/c/dnull
[]
null
null
[a]
helloProblem contributed by @yuta_is_a_bum
Implement FileSystem, an in-memory hierarchical file system that supports basic UNIX-like file and directory operations.
Design an in-memory file system that stores everything in RAM and supports hierarchical directories and files identified by absolute paths (like /a/b/c).
This problem is divided into multiple parts. Each part builds on the previous one.
FileSystem()ls(path)path.path is a directory, return a list of names of files and directories directly under it.path is a file, return a list containing only the file name.mkdir(path)path.addContentToFile(filePath, content)filePath.content.content to its existing contents.filePath must be created automatically.\n for newlines, \t for tabs, \\ for backslash, and \" for quotes within the content.readContentFromFile(filePath)filePath.FileSystem()
ls("/") // returns []
mkdir("/a/b/c")
addContentToFile("/a/b/c/d", "hello")
ls("/") // returns ["a"]
readContentFromFile("/a/b/c/d") // returns "hello"
addContentToFile("/a/b/c/d", " world")
readContentFromFile("/a/b/c/d") // returns "hello world"Extend your implementation with the following operations:
rm(path)path.path refers to a file, delete the file.path refers to a directory, delete the directory and all of its contents recursively.mv(source, destination)source is an existing file or directory path.destination is the new path.destination already exists as a directory, move source inside it.mv("/a/x", "/b") moves /a/x to /b/x if /b is a directory.destination as a rename to that exact path.mv("/a/x", "/a/y") renames /a/x to /a/y.Extend the file system to track metadata for all files and directories.
getSize(path)path is a file, return the size of the file (number of characters in its content).path is a directory, return the total size of all files contained within it (recursively).getLastModified(path)For operations that modify the file system (in Part 3), an optional timestamp parameter may be provided:
addContentToFile(filePath, content, timestamp)mkdir(path, timestamp)rm(path, timestamp)mv(source, destination, timestamp)When a timestamp is provided, use it to update the lastModified metadata. If not provided, you may use any monotonically increasing value.
Track and store the creation time for all files and directories.
/.null in the test results.ls method should output lists in the format [name1, name2, name3] with unquoted names separated by commas and spaces, or [] for empty directories./.FileSystem
ls /
mkdir /a/b/c
addContentToFile /a/b/c/d "hello"
ls /
readContentFromFile /a/b/c/dnull
[]
null
null
[a]
helloFileSystem
ls /
mkdir /a/b/c
addContentToFile /a/b/c/d "hello"
ls /
readContentFromFile /a/b/c/dnull
[]
null
null
[a]
hello