metis.fs

Extends the CC's fs library with additional utility functions.

This module re-exports any function defined in fs, and so may shadow the fs global.

local fs = require "metis.fs"
print(fs.exists("rom"))
read_file(filename [, binary])Read a file into a string.
write_file(filename, contents [, binary])Write a file to a string.
walk(root, callback)Walk a directory tree recursively, invoking a callback for every path found.
read_dirs(root)Recursively read all files and folders in a directory.
read_file(filename [, binary])Source

Read a file into a string.

Parameters

  1. filename string The file to read.
  2. binary? boolean Whether to read this as a binary file.

Returns

  1. string The contents of the file.

Or

  1. nil If the file could not be read.
  2. string The error which occurred when reading.

Usage

  • Read from example.txt.

    local fs = require "metis.fs"
    print(fs.read_file("example.txt"))
write_file(filename, contents [, binary])Source

Write a file to a string.

Parameters

  1. filename string The file to read.
  2. contents string The contents of the file.
  3. binary? boolean Whether to write this as a binary file.

Throws

  • If the file could not be written to.

Usage

  • Write to example.txt.

    local fs = require "metis.fs"
    fs.write_file("example.txt", "Hello, world!")
walk(root, callback)Source

Walk a directory tree recursively, invoking a callback for every path found.

Parameters

  1. root string The directory to start walking from.
  2. callback function(name: string, attributes: table):boolean | nil

    The callback to invoke. This will be passed the file's absolute path, and the result of fs.attributes.

    When given a directory, the function may return false in order to skip visiting its children.

Usage

  • Print the size of every file on the computer, skipping .git directories and the rom.

    local fs = require "metis.fs"
    fs.walk("/", function(path, info)
      if info.isDir then
        if fs.getName(path) == ".git" or path == "rom" then return false end
      else
        print(path .. " => " .. info.size)
      end
    end)
read_dirs(root)Source

Recursively read all files and folders in a directory.

Parameters

  1. root string The directory to start walking from.

Returns

  1. { [string] = table } A mapping of absolute file/folder names to their attributes.

See also

  • walk For finer-grain control.