Module system.filesystem¶
The filesystem module implements common operations for working with the filesystem, including wrappers for syscalls.
Index¶
Function
Gets the absolute path from a path string. |
|
Returns the file name for a path. |
|
Changes the permissions (mode) of the file at a path. |
|
Changes the owner of a file or directory. |
|
Changes the root directory of the current and future child processes. This function requires root. |
|
Combines the specified path components into a single path, canonicalizing any links and ./.. paths. |
|
Copies a file or directory. |
|
Returns the parent directory for a path. |
|
Returns the effective permissions on a file or stat entry for the selected user. |
|
Convenience function for determining whether a file exists. This simply checks that stat does not return nil. |
|
Searches the filesystem for paths matching a glob-style wildcard. |
|
Registers the process to receive filesystem events for a path. Note that this is not recursive. |
|
Returns whether the path exists and is a directory. |
|
Returns whether the path exists and is a file. |
|
Returns whether the path exists and is a link. |
|
Creates a (symbolic) link to a file. |
|
Returns a list of files in a directory. |
|
Creates a directory, making any parent paths that don't exist. |
|
Creates a FIFO. |
|
Mounts a filesystem of the specified type to a directory. |
|
Returns a list of mounts currently available. |
|
Moves a file or directory, allowing cross-filesystem operations. |
|
Opens a file for reading or writing. |
|
Deletes a file or directory at a path, removing any subentries if present. |
|
Moves a file or directory on the same filesystem. |
|
Returns a table with various information about a file or directory. |
|
Unmounts a mounted filesystem. |
Class
A table which stores file statistics. |
API Reference¶
-
system.filesystem.open(path:
string, mode:string): (
handle:system.filesystem.FileHandle|nil,
err:string|nil
)¶ Opens a file for reading or writing.
- Parameters:
path (
string) – The path to the file to openmode (
string) – The mode to open the file in: [rwa]b?
- Returns:
handle (
system.filesystem.FileHandle|nil) – The file handle, which has the same functions as CraftOS file handleserr (
string|nil) – An error message describing why the file couldn’t be opened
-
system.filesystem.list(path:
string): (result:table)¶ Returns a list of files in a directory.
- Parameters:
path (
string) – The path to query- Returns:
result (
table) – A list of files and folders in the directory
-
system.filesystem.stat(path:
string, nolink?:boolean): (result:system.filesystem.FileStat)¶ Returns a table with various information about a file or directory.
- Parameters:
path (
string) – The path to querynolink? (
boolean) – Whether to not resolve links to the file (defaults to false)
- Returns:
result (
system.filesystem.FileStat) – A table with information about the path
-
system.filesystem.remove(path:
string):unknown¶ Deletes a file or directory at a path, removing any subentries if present.
- Parameters:
path (
string) – The path to remove
-
system.filesystem.rename(from:
string, to:string):unknown¶ Moves a file or directory on the same filesystem.
- Parameters:
from (
string) – The original file to moveto (
string) – The new path for the file
-
system.filesystem.mkdir(path:
string):unknown¶ Creates a directory, making any parent paths that don’t exist.
- Parameters:
path (
string) – The directory to create
-
system.filesystem.link(path:
string, location:string):unknown¶ Creates a (symbolic) link to a file.
- Parameters:
path (
string) – The path of the new linklocation (
string) – The location to point the link to
-
system.filesystem.mkfifo(path:
string):unknown¶ Creates a FIFO.
- Parameters:
path (
string) – The FIFO to create
-
system.filesystem.chmod(
path:string,
user:string|nil,
mode:string|number| {read:boolean, write:boolean, execute:boolean}
):unknown¶ Changes the permissions (mode) of the file at a path.
- Parameters:
path (
string) – The path to modifyuser (
string|nil) – The user to modify, or nil to modify world permissionsmode (
string|number| {read:boolean, write:boolean, execute:boolean}) – The new permissions, as either an octal bitmask, a string in the format “[+-=][rwx]+” or “[r-][w-][x-]”, or a table with the permissions to set (anynilarguments are left unset).
-
system.filesystem.chown(path:
string, user:string):unknown¶ Changes the owner of a file or directory.
- Parameters:
path (
string) – The path to modifyuser (
string) – The new owner of the file
-
system.filesystem.chroot(path:
string):unknown¶ Changes the root directory of the current and future child processes. This function requires root.
- Parameters:
path (
string) – The new root path to change to
-
system.filesystem.mount(type:
string, src:string, dest:string, options?:table):unknown¶ Mounts a filesystem of the specified type to a directory.
- Parameters:
type (
string) – The type of filesystem to mountsrc (
string) – The source of the mount (depends on the FS type)dest (
string) – The destination directory to mount tooptions? (
table) – A table of options to pass to the filesystem
-
system.filesystem.unmount(path:
string):unknown¶ Unmounts a mounted filesystem.
- Parameters:
path (
string) – The filesystem to unmount
-
system.filesystem.mountlist(): (
result: {path:string, type:string, source:string, options:table}[]
)¶ Returns a list of mounts currently available.
- Returns:
result ({path:
string, type:string, source:string, options:table}[]) – A list of mounts and their properties.
-
system.filesystem.fsevent(path:
string, enabled?:boolean):unknown¶ Registers the process to receive filesystem events for a path. Note that this is not recursive.
- Parameters:
path (
string) – The path to register forenabled? (
boolean) – Whether to enable events (defaults to true)
-
system.filesystem.combine(...:
string): (result:string)¶ Combines the specified path components into a single path, canonicalizing any links and ./.. paths.
- Parameters:
... (
string) – The path components to combine- Returns:
result (
string) – The combined and canonicalized path
-
system.filesystem.absolute(path:
string): (result:string)¶ Gets the absolute path from a path string.
- Parameters:
path (
string) – The path to convert- Returns:
result (
string) – An absolute path pointing to the file
-
system.filesystem.copy(from:
string, to:string, preserve?:boolean)¶ Copies a file or directory.
- Parameters:
from (
string) – The path to copy fromto (
string) – The path to copy topreserve? (
boolean) – Whether to preserve permissions when copying
-
system.filesystem.move(from:
string, to:string)¶ Moves a file or directory, allowing cross-filesystem operations.
- Parameters:
from (
string) – The path to move fromto (
string) – The path to move to
-
system.filesystem.basename(path:
string): (result:string)¶ Returns the file name for a path.
- Parameters:
path (
string) – The path to use- Returns:
result (
string) – The file name of the path
-
system.filesystem.dirname(path:
string): (result:string)¶ Returns the parent directory for a path.
- Parameters:
path (
string) – The path to use- Returns:
result (
string) – The parent directory of the path
-
system.filesystem.find(wildcard:
string): (result:table)¶ Searches the filesystem for paths matching a glob-style wildcard.
- Parameters:
wildcard (
string) – The pathspec to match- Returns:
result (
table) – A list of matching file paths
-
system.filesystem.exists(path:
string): (result:boolean)¶ Convenience function for determining whether a file exists. This simply checks that
statdoes not returnnil.- Parameters:
path (
string) – The path to check- Returns:
result (
boolean) – Whether the path exists
-
system.filesystem.isFile(path:
string): (result:boolean)¶ Returns whether the path exists and is a file.
- Parameters:
path (
string) – The path to check- Returns:
result (
boolean) – Whether the path is a file
-
system.filesystem.isDir(path:
string): (result:boolean)¶ Returns whether the path exists and is a directory.
- Parameters:
path (
string) – The path to check- Returns:
result (
boolean) – Whether the path is a directory
-
system.filesystem.isLink(path:
string): (result:boolean)¶ Returns whether the path exists and is a link.
- Parameters:
path (
string) – The path to check- Returns:
result (
boolean) – Whether the path is a link
-
system.filesystem.effectivePermissions(
file:string|system.filesystem.FileStat,
user?:string
): (
result: {read:boolean, write:boolean, execute:boolean} |nil
)¶ Returns the effective permissions on a file or stat entry for the selected user.
- Parameters:
file (
string|system.filesystem.FileStat) – The file path or stat to checkuser? (
string) – The user to check for (defaults to the current user)
- Returns:
result ({read:
boolean, write:boolean, execute:boolean} |nil) – The permissions for the user, ornilif the file doesn’t exist
- class system.filesystem.FileStat¶
A table which stores file statistics.
-
type:
string¶ Stores the type of file: one of “file”, “directory”, “link”, “special”
-
size:
integer¶ The size of the file
-
created:
integer¶ The creation date of the file, in milliseconds since January 1, 1970
-
modified:
integer¶ The modification date of the file, in milliseconds since January 1, 1970
-
owner:
string¶ The owner of the file
-
permissions:
table¶ The permissions of the file for each user, indexed by user name
-
worldPermissions:
table¶ The permissions of the file for all users not in
FileStat.permissions
-
special:
table¶ Any additional data from the filesystem
-
type: