Skip to content

l0.DependencyControl.file-ops

file_ops = require "l0.DependencyControl.file-ops"
local file_ops = require("l0.DependencyControl.file-ops")

Functions

createTempDir

FileOps.createTempDir!
FileOps.createTempDir()

Creates a unique temporary directory and returns its path.

Returns:

  • tempDirPath string? — Absolute path to the created temporary directory, or nil if it couldn't be created.
  • err string? — Error message if the directory couldn't be created.

getTempDir

FileOps.getTempDir!
FileOps.getTempDir()

Generates a unique temporary directory path that does not exist yet.

Returns:

  • tempDirPath string — Absolute path to a unique, not-yet-existing temporary directory.

remove

FileOps.remove paths, recurse, reSchedule
FileOps.remove(paths, recurse, reSchedule)

Removes one or more files/directories and optionally reschedules failed removals.

param type description
paths string|(string|string[])[] Path, or list of paths (each a string or an array of path segments).
recurse? boolean Recurse into directories (default false, so a non-empty directory is not removed).
reSchedule? boolean Reschedule failed removals for the next restart.

Returns:

  • overallSuccess boolean? — True if all succeeded, false if any were rescheduled, nil on hard failure.
  • details table — Per-path result tables keyed by path.
  • firstErr string? — The first error encountered.

runScheduledRemoval

FileOps.runScheduledRemoval configDir
FileOps.runScheduledRemoval(configDir)

Replays removals previously scheduled by remove().

param type description
configDir? string Directory holding the FileOps config (defaults to the configured dir).

Returns:

  • success boolean?
  • err string?

copy

FileOps.copy source, target, clobber
FileOps.copy(source, target, clobber)

Copies a file to a target path.

param type description
source string
target string
clobber? boolean Overwrite an existing target file.

Returns:

  • success boolean
  • err string?

listDir

FileOps.listDir dirPath
FileOps.listDir(dirPath)

Lists the names of a directory's entries, excluding . and ...

param type description
dirPath string|string[] Path or path segments of the directory.

Returns:

  • entries string[]? — The entry names, or nil when the path isn't a directory.
  • err string?

listFilesRecursive

FileOps.listFilesRecursive dirPath
FileOps.listFilesRecursive(dirPath)

Recursively collects all files below a directory.

param type description
dirPath string|string[] Path or path segments of the directory to walk.

Returns:

  • files string[]? — Full paths of every file below the directory (joined onto the given path), or nil when it can't be listed.
  • err string?

joinPath

FileOps.joinPath ...
FileOps.joinPath(...)

Joins and resolves multiple path segments into a single path string.

param type description
... string|string[] One or more path segments, or arrays of path segments.

Returns:

  • joinedPath string? — The path segments joined by OS-specific separators, or nil on error.
  • err string?

pathSegments

FileOps.pathSegments path
FileOps.pathSegments(path)

Returns an iterator over the non-empty components of a path, split on any separator.

param type description
path string

Returns:

  • iterator fun(): string?

move

FileOps.move source, target, overwrite
FileOps.move(source, target, overwrite)

Moves a file to a target path, optionally replacing existing targets.

param type description
source string
target string
overwrite? boolean Replace an existing target file.

Returns:

  • success boolean
  • err string?

readFile

FileOps.readFile path
FileOps.readFile(path)

Reads and returns the full contents of a file.

param type description
path string|string[] Path or path segments to the file to read.

Returns:

  • data string? — The contents of the file, or nil if an error occurred.
  • err string? — An error message if an error occurred.

writeFile

FileOps.writeFile path, data, clobber
FileOps.writeFile(path, data, clobber)

Writes data to a file, creating the file if it doesn't exist and optionally overwriting existing files.

param type description
path string|string[] Path or path segments to the file to write.
data string The data to write to the file.
clobber? boolean Overwrite the file if it already exists (default false).

Returns:

  • success boolean — True if the file was written successfully.
  • err string?

getHash

FileOps.getHash fileName, hashType
FileOps.getHash(fileName, hashType)

Reads a file and computes the hash of its contents.

param type description
fileName string|string[] Path or path segments to the file to hash.
hashType? HashType The hash algorithm to use (default Sha1).

Returns:

  • hexDigest string? — The lowercase hex digest, or nil if an error occurred.
  • err string? — An error message if an error occurred.

verifyHash

FileOps.verifyHash fileName, hash, hashType
FileOps.verifyHash(fileName, hash, hashType)

Reads a file and verifies its contents match an expected hash.

param type description
fileName string|string[] Path or path segments to the file to verify.
hash string The expected hex digest (compared case-insensitively).
hashType? HashType The hash algorithm to use (default Sha1).

Returns:

  • match boolean? — True on match, false on mismatch, or nil on error.
  • err string? — The mismatch detail or error message.

rmdir

FileOps.rmdir path, recurse
FileOps.rmdir(path, recurse)

Removes a directory, by default together with everything it contains.

param type description
path string|string[] Path or path segments to the directory to remove.
recurse? boolean Remove the directory's contents first (default true); when false, only an already-empty directory is removed.

Returns:

  • success boolean? — True on success, or nil on error.
  • err string? — An error message when the path is empty, doesn't exist, isn't a directory, or something couldn't be removed.

mkdir

FileOps.mkdir path, isFile, recurse
FileOps.mkdir(path, isFile, recurse)

Creates a directory.

param type description
path string|string[] Path or path segments to the directory to create.
isFile boolean Whether the path is a file path (discards the last segment when checking/creating the directory).
recurse? boolean Also create any missing parent directories (default false).

Returns:

  • created boolean? — True if created, false if it already existed, nil if an error occurred.
  • dirPathOrError string — The existing/created directory path, or an error message.

getAttributes

FileOps.getAttributes path, key
FileOps.getAttributes(path, key)

Retrieves file or directory attributes along with the parsed components of its path.

param type description
path string|string[] Either a path or an array of path segments.
key? string Attribute name to retrieve (e.g. "mode", "size", "modification"), or nil for the full attribute table.

Returns:

  • info FileOpsAttributesInfo? — The attributes and path components, or nil on a hard error (an invalid path or an lfs failure). A path that simply doesn't exist is not an error: info.attr is then false.
  • err string? — An error message, present only when info is nil.

attributes

Deprecated — Use getAttributes, which returns a single info table plus an error message.

FileOps.attributes path, key
FileOps.attributes(path, key)

Retrieves file or directory attributes.

param type description
path string|string[] Either a path or an array of path segments.
key? string Attribute name to retrieve (e.g. "mode", "size", "modification"), or nil for the full attribute table.

Returns:

  • attr table|string|number|boolean|nil — The requested attribute(s), false if absent, or nil on error.
  • fullPath string — The validated full path, or an error message if the path was invalid.
  • device string? — The device component of the path.
  • dir string? — The directory component of the path.
  • file string? — The file name component of the path.

exists

FileOps.exists path, expectedMode
FileOps.exists(path, expectedMode)

Checks whether a file or directory exists and optionally verifies its type.

param type description
path string|string[] Either a path or an array of path segments.
expectedMode? string If specified, the required type of the filesystem entry.

Returns:

  • exists boolean? — True if it exists and matches the expected type, false if not, nil on error.
  • err string? — An error message if the file doesn't exist or is of the wrong type.

validateFullPath

FileOps.validateFullPath path, checkFileExt, basePath
FileOps.validateFullPath(path, checkFileExt, basePath)

Validates and normalizes an absolute filesystem path.

param type description
path string|string[] Either a path or an array of path segments.
checkFileExt? boolean Require the path to have a file extension.
basePath? string|string[] Base path to resolve relative paths against; relative paths are rejected without it.

Returns:

  • normalizedPath string|false|nil — The normalized path, or false/nil on error.
  • deviceOrErr string? — The device/root component on success, or an error message on failure.
  • dir string? — The directory component (success only).
  • file string? — The file name component (success only).

getNamespacedPath

FileOps.getNamespacedPath basePath, namespace, ext, nested
FileOps.getNamespacedPath(basePath, namespace, ext, nested)

Converts a base path and namespace into a namespaced filesystem path. Dots in the namespace are converted to path separators when nested is true.

param type description
basePath string|string[] Base path (or segments) the namespaced path is created under.
namespace string
ext string File extension (including the dot).
nested? boolean Convert namespace dots to path separators (default true).

Returns:

  • path string?
  • err string?

Fields

field type description
pathSep string
pathMatch table
pathMaxLength integer
pathMaxSegmentLength integer
longPathsDisabled string
windowsRegistryLongPathsEnabled boolean

Types

FileOpsAttributesInfo

field type description
attr table|string|number|false The requested attribute(s), or false when the entry doesn't exist.
path string The validated full path.
dev string The device component of the path.
dir string The directory component of the path.
file string The file name component of the path.