Module minifs
MiniFS is a minimal file system module for Lua, licensed under MIT.
It depends on LFS and provides easy access to functions such as copying files, checking the type of a file or working with temporary files. You can get it from the GitLab repository: https://gitlab.com/zatherz/minifs.
Functions
fs.write (file, data) | Write data to a file, overwriting the previous contents (and creating the file if needed). |
fs.append (file, data) | Append data to a file (similiar to write, but will not overwrite the data inside the file). |
fs.read (file) | Read all data from a file. |
fs.copy (fromfile, tofile) | Copy a file to a destination. |
fs.move (fromfile, tofile) | Move a file. |
fs.appendcopy (fromfile, tofile) | Append a copy of a file to another file. |
fs.appendmove (fromfile, tofile) | Move and append a file. |
fs.remove (file) | Remove a file. |
fs.size (file) | Get the size of a file. |
fs.permissions (file) | Get the permission string of a file |
fs.update (file, accesstime, modificationtime) | Update the access/modification time of a file. |
fs.create (file) | Create an empty, zero byte file. |
fs.rename (file, newname) | Rename a file. |
fs.exists (file) | Check if a file exists. |
fs.mkdir (dir) | Create a new directory. |
fs.rmdir (dir, recursive) | Remove a directory. |
fs.device (file) | Get the device of the file. |
fs.type (file) | Get the type of the file. |
fs.uid (file) | Get the user ID of the file. |
fs.gid (file) | Get the group ID of the file. |
fs.accesstime (file) | Get the last access time of the file. |
fs.modificationtime (file) | Get the last modification time of the file. |
fs.changetime (file) | Get the last status change time of the file. |
fs.separator () | Get the directory separator that the system uses. |
fs.system (unixtype) | Get the operating system the script is running on (Mac/Linux/Windows). |
fs.randomstring (length) | Create a random string containing lowercase, uppercase letters and numbers. |
fs.tmpname (length, prefix, suffix) | Create a valid path to a temporary file name in the default temporary directory of the operating system. |
fs.usetmp (length, prefix, suffix, call, dir) | Create a temporary file, then call a defined function and remove the file afterwards. |
fs.usetmpdir (length, prefix, suffix, call) | Create a temporary directory, then call a defined function and recursively remove the directory afterwards. |
fs.files (dir, fullpath) | Basic iterator over files in a directory. |
fs.link (file, link) | Create a hardlink to a file. |
fs.symlink (file, link) | Create a symlink to a file or directory. |
fs.run (command, ...) | Run a console comand. |
fs.call (command, ...) | Call a command and return all of its results. |
Functions
- fs.write (file, data)
-
Write data to a file, overwriting the previous contents (and creating the file if needed).
Parameters:
- file The file to write to.
- data The data that will be written to the file.
Returns:
-
boolean
- fs.append (file, data)
-
Append data to a file (similiar to write, but will not overwrite the data inside the file).
Parameters:
- file The file to append to.
- data The data that will be appended to the file.
Returns:
-
boolean
- fs.read (file)
-
Read all data from a file.
Parameters:
- file The file to be read.
Returns:
-
string
- fs.copy (fromfile, tofile)
-
Copy a file to a destination.
Parameters:
- fromfile The file to be copied.
- tofile The target path.
Returns:
-
boolean
- fs.move (fromfile, tofile)
-
Move a file. Similiar to copying, but will remove the original file after copying.
Parameters:
- fromfile The file to be moved.
- tofile The destination of the file.
Returns:
-
nil
- fs.appendcopy (fromfile, tofile)
-
Append a copy of a file to another file. Like copying, but if the target file already exists the contents will not be overwritten but instead added after the existing data.
Parameters:
- fromfile The file to be copied.
- tofile The target file.
Returns:
-
boolean
- fs.appendmove (fromfile, tofile)
-
Move and append a file. As opposed to the normal move function, the contents of the moved file will be appended to the target file instead of overwriting it.
Parameters:
- fromfile The file to be moved.
- tofile The destination of the file.
Returns:
-
nil
- fs.remove (file)
-
Remove a file.
Parameters:
- file The file to be removed.
Returns:
-
boolean
- fs.size (file)
-
Get the size of a file.
Parameters:
- file The file to get the size of.
Returns:
-
number
- fs.permissions (file)
-
Get the permission string of a file
Parameters:
- file The file to get the permission string of.
Returns:
-
string
- fs.update (file, accesstime, modificationtime)
-
Update the access/modification time of a file.
Parameters:
- file The file to be updated.
- accesstime The access time to set, if nil will be set to the current time.
- modificationtime The modification time to set, if nil will be set to the accesstime, and if accesstime is nil, will be set to the current time.
Returns:
-
boolean, string [error message]
- fs.create (file)
-
Create an empty, zero byte file.
Parameters:
- file The file to be created.
Returns:
-
boolean
- fs.rename (file, newname)
-
Rename a file. The function will error
Parameters:
- file The name of the file.
- newname The new name of the file.
Returns:
-
boolean
- fs.exists (file)
-
Check if a file exists.
Parameters:
- file The path to be checked.
Returns:
-
boolean [whether the file exists]
- fs.mkdir (dir)
-
Create a new directory.
Parameters:
- dir The path to the directory.
Returns:
-
boolean, string [error message]
- fs.rmdir (dir, recursive)
-
Remove a directory.
Parameters:
- dir The path to the directory.
- recursive If true, the directory will be recursively removed. If false, the function will error on non-empty directories.
Returns:
-
boolean, string [error message]
- fs.device (file)
-
Get the device of the file. On Linux, this will be the inode. On Windows, this will be the drive number.
Parameters:
- file The path to the file.
Returns:
-
number (string on Windows?)
- fs.type (file)
-
Get the type of the file. This can be 'file', 'directory', 'link', 'socket', 'named pipe', 'char device', 'block device' or 'other'.
Parameters:
- file The path to the file.
Returns:
-
string
- fs.uid (file)
-
Get the user ID of the file. Always 0 on Windows.
Parameters:
- file The path to the file.
Returns:
-
number
- fs.gid (file)
-
Get the group ID of the file. Always 0 on Windows.
Parameters:
- file The path to the file.
Returns:
-
number
- fs.accesstime (file)
-
Get the last access time of the file.
Parameters:
- file The path to the file.
Returns:
-
number
- fs.modificationtime (file)
-
Get the last modification time of the file.
Parameters:
- file The path to the file.
Returns:
-
number
- fs.changetime (file)
-
Get the last status change time of the file.
Parameters:
- file The path to the file.
Returns:
-
number
- fs.separator ()
-
Get the directory separator that the system uses. Forward slash on Unix-likes, Backward slash on Windows.
Returns:
-
string
- fs.system (unixtype)
-
Get the operating system the script is running on (Mac/Linux/Windows).
Parameters:
- unixtype If set to true, the type of the Unix-like will be detected. Note that this uses an io.popen call and might be more expensive.
Returns:
-
string
- fs.randomstring (length)
-
Create a random string containing lowercase, uppercase letters and numbers.
Parameters:
- length Length of the name, 6 by default.
Returns:
-
string
- fs.tmpname (length, prefix, suffix)
-
Create a valid path to a temporary file name in the default temporary directory of the operating system.
Parameters:
- length Length of the filename.
- prefix Prefix of the filename, does not count for length.
- suffix Suffix of the filename, does not count for length.
Returns:
-
string
- fs.usetmp (length, prefix, suffix, call, dir)
-
Create a temporary file, then call a defined function and remove the file afterwards.
Note that you can also provide only one argument, the callback, and keep the rest of the values as defaults.
Parameters:
- length Length of the filename.
- prefix Prefix of the filename
- suffix Suffix of the filename.
- call The callback function to call inbetween creating a temporary file and removing the file. One argument, the path to the file, is provided to the function.
- dir If this is true, then the temporary file will be a temporary directory. See {fs.usetmpdir}.
- fs.usetmpdir (length, prefix, suffix, call)
-
Create a temporary directory, then call a defined function and recursively remove the directory afterwards.
Note that you can also provide only one argument, the callback, and keep the rest of the values as defaults.
Parameters:
- length Length of the filename.
- prefix Prefix of the filename
- suffix Suffix of the filename.
- call The callback function to call inbetween creating a temporary file and removing the file. One argument, the path to the file, is provided to the function.
- fs.files (dir, fullpath)
-
Basic iterator over files in a directory.
Parameters:
- dir The directory to iterate through.
- fullpath If this is set to true, then the entries returned by the iterator will contain the directory name along with the file name.
Returns:
-
function [iterator returns: string]
- fs.link (file, link)
-
Create a hardlink to a file.
Parameters:
- file The file to be linked to.
- link The path to the link.
- fs.symlink (file, link)
-
Create a symlink to a file or directory.
Parameters:
- file The file to be linked to.
- link The path to the link.
- fs.run (command, ...)
-
Run a console comand.
Parameters:
- command The command to be ran.
- ... Additional arguments.
- fs.call (command, ...)
-
Call a command and return all of its results.
Parameters:
- command The command to be called.
- ... Additional arguments.