usage: require("vfs")
allows you to work with paths and create virtual filesystems with mount support
it can be used in conjunction with the ramfs library to create a temporary directory
please note that this library always uses full paths when accessing file systems,
which means that the directory selected on the filesystem does not affect its operation through this library
however, this library itself implements a mechanism for changing directories, which replaces the one that is built into filesystems
methods:
- vfs.createHost():vfshost - creates a new root filesystem
- vfs.elements(path):table - divides the path into its component parts
- vfs.resolve(...):string - accepts a table paths consisting of individual elements, processes elements such as "." and ".."
- vfs.concat(...):string - combines many paths into one. supports returning to the previous directory using ".."
- vfs.safeConcat(rootpath, ...):string - it works the same way as concat, but the resulting path cannot be a directory lower than the first one, otherwise it returns nil
- vfs.isGlobalPath(path):boolean - returns true if the path is global
- vfs.path(path):path - returns the path without the last element
- vfs.name(path):name - returns the name of the last element of the path
- vfs.hideExtension(name):string - accepts the name (not path), hides the extension
- vfs.getExtension(name):string - accepts the name (not path), returns the extension
vfshost methods:
- vfshost:mount(path, fs) - mounts the filesystem using the specified path
- vfshost:get(path):fs|nil, path|nil - returns the filesystem mounted on this path and the global path to the target object inside the mounted file system
- vfshost:unmount(path|fs):boolean - unmounts the filesystem using the specified path or filesystem object. returns true if at least one mount point has been deleted
- vfshost:mounts():{{path, fs}, {path, fs}, ...} - return a list of all mount points
- vfshost:absolute(path):path - returns an absolute path from a relative one
- vfshost:equals(...):boolean - returns true if all paths refer to the same object
- vfshost:openFolder(path) - changes the currently selected folder. if the path starts with /, then the folder relative to the root is selected. if without / then relative to the current folder. ".." is also supported
- vfshost:getCurrentPath():string - returns the path to the currently selected folder
- vfshost:createFile(path) - creates a file, if the file has already been created, it will throw an exception
- vfshost:createFolder(path) - creates a directory with the specified name. if it already exists, it throws an exception
- vfshost:deleteFile(path) - deletes the file if the file does not exist throws an exception
- vfshost:deleteFolder(path) - deletes the directory if the file does not exist throws an exception
- vfshost:writeFile(path, string) - writes a file, throws an exception if the file does not exist
- vfshost:readFile(path):string - reads the file. if the file does not exist, it throws an exception
- vfshost:hasFile(path):boolean - checks if a file with the specified name exists
- vfshost:hasFolder(path):boolean - checks if a directory with the specified name exists
- vfshost:getFileSize(path):number - reads the file size in bytes. takes into account the length of the file name
- vfshost:getFolderSize(path):number - recursively reads how much space a folder takes up, taking into account all the names and contents of all files
- vfshost:getFileList(path):table - returns a list of files in the current directory
- vfshost:getFolderList(path):table - returns a list of directories in the current directory
- vfshost:hasMount(path):boolean - checks if a mount with the specified name exists
- vfshost:getMountList(path):table - returns a list of mount points in the current directory. returns only the mount points inside this directory, not the mount point of this directory
- vfshost:pCreateFile(path) - this works the same way as createFile, but it does not give an error if the file already exists and automatically creates the necessary directories
- vfshost:pCreateFolder(path) - it works the same way as createFolder, but it can create multiple directories at once and does not create exceptions if the directory already exists
- vfshost:pDeleteFile(path):boolean - deletes the file, returns true if successful
- vfshost:pDeleteFolder(path):boolean - deletes the directory, returns true if successful. cannot delete non-empty directories, use recursionDelete to do this
- vfshost:pWriteFile(path, string) - it works the same way as writeFile, but it creates a file itself if there is none and paves the path of directories if one or more of them are missing
- vfshost:pReadFile(path):string|nil - it works the same way as readFile, but it does not throw an exception if there is no file or directory, but returns nil
- vfshost:pHasFile(path):boolean - it works the same way as hasFile but returns false instead of an exception when a directory is missing from the path
- vfshost:pHasFolder(path):boolean - it works the same way as hasFolder but returns false instead of an exception when a directory is missing from the path
- vfshost:pGetFileSize(path):number|nil - it works the same way as getFileSize but returns nil instead of an exception
- vfshost:pGetFolderSize(path):number|nil - it works the same way as getFolderSize but returns nil instead of an exception
- vfshost:pGetFileList(path):table - it works the same way as getFileList, but if one of the path elements is missing, it simply returns an empty table instead of an exception
- vfshost:pGetFolderList(path):table - it works the same way as getFolderList, but if one of the path elements is missing, it simply returns an empty table instead of an exception
- vfshost:recursionDelete(path, deleteContentsMountPoints:boolean) - recursively deletes directories and files. it does not delete mount points, but it can delete the contents in them if the appropriate flag is used
- vfshost:recursionCopy(path, path2, copyContentsMountPoints:boolean) - recursively copy directories and files. you can pass the true value to also copy the contents of the mount points as directories
local ramfs = require("ramfs")
local vfs = require("vfs")
local vfshost = vfs.createHost()
vfshost:mount("/", getComponent("disk")) --you can mount something to the root, or you can choose not to. at your discretion
for i = 1, 3 do
vfshost:mount("/tmp" .. i, ramfs.create(1024 * 64).fs)
end
function onTick()
print("file list: ")
for k, v in ipairs(vfshost:getFileList(".")) do
print(v)
end
print("folders list: ")
for k, v in ipairs(vfshost:getFolderList(".")) do
print(v)
end
print("mounts list: ")
for k, v in ipairs(vfshost:getMountsList(".")) do
print(v)
end
end
_enableCallbacks = true