Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

fs.lib

fs.lib communicates with the FS (File System) using syscalls and wraps those functions


Importing

local fs = import("fs.lib")

Methods

write

dir

function fs.write.dir(path: string): number
  • Creates a directory at specified path
  • Returns a return code:
Return CodeNumerical ValueMeaning
SUCCESS0The operation has completed successfully
INVALID_PATH1The specified path is invalid
INVALID_PARENT_TYPE2The parent is a file and not a directory
DIR_EXISTS3The directory already exists
NO_PERMS4No permission to write directory at the specified path
STORAGE_OVERFLOW5The operation would cause the storage to fill up
  • Example usage:
local fs = import("fs.lib")

local success = fs.write.dir("root/user/Hello")
if success == 0 then
    -- the operation completed successfully
end

file

function fs.write.file(path: string, content: string): number
  • Creates a new file at specified path
  • File will contain given content
  • Returns a return code:
Return CodeNumerical ValueMeaning
SUCCESS0The operation has completed successfully
INVALID_PATH1The specified path is invalid
INVALID_PARENT_TYPE2The parent is a file and not a directory
OVERWRITE_DIR3fs.write.file() can not overwrite a directory
NO_PERMS4No permission to write file at the specified path
STORAGE_OVERFLOW5The operation would cause the storage to fill up
  • Example usage:
local fs = import("fs.lib")

local success = fs.write.file("root/user/hello.lua", "Hello World!")
if success = 0 then
    -- the operation completed successfully
end

read

function fs.read(path: string): (number, string)
  • Reads the content of a file at specified path
  • Returns the return code (ret1) and the content (ret2)
Return CodeNumerical ValueMeaning
SUCCESS0The operation has completed successfully
INVALID_FILE1The file that was attempted to be read is invalid
ATTEMPT_READ_DIR2fs.read() can not read a directory
NO_PERMS3No permission to read the file
  • Example usage:
local fs = import("fs.lib")

local success, content = fs.read("root/user/hello.lua")
if success = 0 then
    -- the operation completed successfully
end

remove

dir

function fs.remove.dir(path: string): number
  • Removes a directory and its children
  • Returns a return code:
Return CodeNumerical ValueMeaning
SUCCESS0The operation completed successfully
INVALID_DIR1The folder that should’ve been removed was not found
NO_PERMS2No permission to remove directory
FAILED3The operation has failed on the directory children - Data loss is possible
INVALID_FS_STATE0xDEADBEEFAn invalid file system state. The system will panic when this is returned
  • Example usage:
local fs = import("fs.lib")

local success = fs.remove.dir("root/user")
if success = 0 then
    -- the operation completed successfully
    -- this operation will not cause there are no permissions to remove root/user
end

file

function fs.remove.file(path: string): number
  • Removes a file
  • Returns a return code:
Return CodeNumerical ValueMeaning
SUCCESS0The operation completed successfully
INVALID_FILE1The specified file is invalid
NO_PERMS2No permission to complete operation
  • Example usage:
local fs = import("fs.lib")

local success = fs.remove.file("root/bin/console.exe")
if success = 0 then
    -- the operation completed successfully
    -- this operation will not cause there are no permissions to remove root/bin/console.exe
end

dir_children

function fs.remove.dir_children(path: string): number
  • Removes children of a directory excluding the directory

  • Returns a return code: | Return Code | Numerical Value | Meaning | | :———: | :———––: | :—–: | | SUCCESS | 0 | The operation completed successfully | | INVALID_DIR | 1 | The folder whose children should’ve been removed was not found | | NO_PERMS | 2 | No permission to remove children of directory | | FAILED | 3 | The operation has failed on the directory children - Data loss is possible | | INVALID_FS_STATE | 0xDEADBEEF | An invalid file system state. The system will panic when this is returned |

  • Example usage:

local fs = import("fs.lib")

local success = fs.remove.dir_children("root")
if success = 0 then
    -- the operation completed successfully
    -- this operation will not cause there are no permissions to remove root
end

get_dir_children

function fs.get_dir_children(path: string): (number, {string})
  • Gets all children of a directory
  • Returns a return code (ret1) and the children (ret2) | Return Code | Numerical Value | Meaning | | :———: | :———––: | :—–: | | SUCCESS | 0 | The operation completed successfully | | INVALID_PATH | 1 | The directory to get children of is invalid | | INVALID_PROP | 2 | This should never be returned as it is logically impossible to achieve |

get_ext

function fs.get_ext(path_or_fname: string): string
  • Returns the extension of a path or filename
  • Returns "" (an empty string) if filename has no extension

get_basename

function fs.get_basename(path_or_fname: string): string
  • Returns only the name, strips the extension and the path expression from earlier

get_name

function fs.get_name(path_or_fname: string): string
  • Returns the filename, removes previous slashes and keeps the name and extension

get_path_breakdown

function fs.get_path_breakdown(path_or_fname: string): {string}
  • Returns the path breakdown, a table of all elements from the path

get_parent

function fs.get_parent(path_or_fname: string): string
  • Returns the parent of a path

exists

function fs.exists(path): boolean
  • Returns whether the object exists or not in the filesystem

get_used_storage

function fs.get_used_storage(): number
  • Returns the used storage

get_max_storage

function fs.get_max_storage(): number
  • Returns the whole available storage

get_final_path

function fs.get_final_path(path, optional_start): string
  • Starts in the working directory or optional_start, then normalizes the given path and returns it
  • Example usage:
local fs = import("fs.lib")

local normalized = fs.get_final_path("../lib/../bin", "root/bin")
--[[
normalized is now "root/bin", because:
1. we start in root/bin
2. we go back (..) into root
3. we go in root/lib
4. we go back (..) into root
5. we go in root/bin
]]

get_final_path_ignore_existence

function fs.get_final_path_ignore_existence(path, optional_start): string
  • Starts in the working directory or optional_start, then normalizes the given path and returns it
  • Can go one file deeper (inexistent)
  • Example usage:
local fs = import("fs.lib")

local normalized = fs.get_final_path("../lib/../bin/inexistent_file.exe", "root/bin")
--[[
normalized is now "root/bin/inexistent_file.exe", because:
1. we start in root/bin
2. we go back (..) into root
3. we go in root/lib
4. we go back (..) into root
5. we go in root/bin
6. we go in root/bin/inexistent_file.exe
this is useful for programs such as mkdir or touch