Module: nupp.io.files

Filesystem metadata, directory contents, and the operations that move names rather than bytes.

Module contents

Functions

FunctionKindDescription
appendfunctionAdds to the end of a file, creating it when it does not exist.
copyfunctionCopies a file's contents over a destination.
createDirectoryfunctionCreates a directory and every missing parent.
createSymlinkfunctionCreates a symbolic link.
createTemporaryDirectoryfunction
createTemporaryFilefunction
currentDirectoryfunctionReads the process's current working directory.
existsfunctionWhether a path resolves to anything at all.
globfunctionExpands a filesystem pattern into matching paths.
infofunctionDescribes one path, following symbolic links.
isDirectoryfunctionWhether a path resolves to a directory.
isFilefunctionWhether a path resolves to a regular file.
isSymlinkfunctionWhether a path is itself a symbolic link, without following it.
linesfunctionIterates a file's lines, closing it at the end.
listfunctionLists a directory's immediate children.
openfunction
pendingTransfersfunctionHow many whole-file transfers this program is still holding.
readfunctionReads a whole file.
readLinkfunctionReads a symbolic link's target without resolving it.
removefunctionRemoves a file, a symbolic link, or a directory.
renamefunctionRenames a path, replacing an existing destination.
setReadOnlyfunctionSets or clears a path's read-only attribute.
userFolderfunctionAnswers a well-known user folder.
writefunctionWrites a whole file, replacing its contents.
writeAtomicfunctionWrites a whole file through a temporary beside it, so an interrupted write leaves the destination as it was rather...

Functions#

appendfunction#

Adds to the end of a file, creating it when it does not exist.

local append: function(path: string | nupp.Path, bytes: string | nupp.ByteView): (boolean, string?)

Arguments

NameTypeDescription
pathstring | nupp.Path

the file to extend

bytesstring | nupp.ByteView

what to add

Returns

TypeDescription
boolean

whether the write succeeded

string?

a failure reason, when unsuccessful

copyfunction#

Copies a file's contents over a destination.

local copy: function(from: string | nupp.Path, to: string | nupp.Path): (boolean, string?)

Arguments

NameTypeDescription
fromstring | nupp.Path

the file to copy

tostring | nupp.Path

where to copy it

Returns

TypeDescription
boolean

whether the copy succeeded

string?

a failure reason, when unsuccessful

createDirectoryfunction#

Creates a directory and every missing parent. An existing directory succeeds.

local createDirectory: nosuspend function(path: string | nupp.Path): (boolean, string?)

Arguments

NameTypeDescription
pathstring | nupp.Path

the directory to create

Returns

TypeDescription
boolean

whether the directory exists afterwards

string?

a failure reason, when unsuccessful

createTemporaryDirectoryfunction#

local createTemporaryDirectory: nosuspend function(
            options: nupp.Files.TemporaryOptions?
        ): (nupp.Files.TemporaryPath?, string?)

Arguments

NameTypeDescription
optionsnupp.Files.TemporaryOptions?

Returns

TypeDescription
nupp.Files.TemporaryPath?
string?

createTemporaryFilefunction#

local createTemporaryFile: nosuspend function(
            options: nupp.Files.TemporaryOptions?
        ): (nupp.Files.TemporaryPath?, string?)

Arguments

NameTypeDescription
optionsnupp.Files.TemporaryOptions?

Returns

TypeDescription
nupp.Files.TemporaryPath?
string?

currentDirectoryfunction#

Reads the process's current working directory.

local currentDirectory: nosuspend function(): (string?, string?)

Returns

TypeDescription
string?

the current directory, or nil on failure

string?

a failure reason, when unsuccessful

existsfunction#

Whether a path resolves to anything at all.

local exists: nosuspend function(path: string | nupp.Path): boolean

Arguments

NameTypeDescription
pathstring | nupp.Path

the path to test

Returns

TypeDescription
boolean

whether it resolves

globfunction#

Expands a filesystem pattern into matching paths.

* and ? match within one path component, character classes use [abc] or [!abc], and ** crosses directory boundaries. The returned paths are sorted, and no matches is an empty list.

local glob: nosuspend function(pattern: string | nupp.Path): ({string}?, string?)

Arguments

NameTypeDescription
patternstring | nupp.Path

the filesystem pattern to expand

Returns

TypeDescription
{string}?

the matching paths, or nil when the pattern or walk fails

string?

a failure reason, when unsuccessful

infofunction#

Describes one path, following symbolic links.

Examples#

Read a file's size:

local info: nosuspend function(path: string | nupp.Path): (nupp.Files.Info?, string?)

Arguments

NameTypeDescription
pathstring | nupp.Path

the path to describe

Returns

TypeDescription
nupp.Files.Info?

the attributes, or nil when the path cannot be read

string?

a failure reason, when unsuccessful

isDirectoryfunction#

Whether a path resolves to a directory.

local isDirectory: nosuspend function(path: string | nupp.Path): boolean

Arguments

NameTypeDescription
pathstring | nupp.Path

the path to test

Returns

TypeDescription
boolean

whether it is a directory

isFilefunction#

Whether a path resolves to a regular file.

local isFile: nosuspend function(path: string | nupp.Path): boolean

Arguments

NameTypeDescription
pathstring | nupp.Path

the path to test

Returns

TypeDescription
boolean

whether it is a file

linesfunction#

Iterates a file's lines, closing it at the end.

A trailing carriage return is removed, so a file written on either platform reads the same. The iterator stops at the end of the file; abandoning it early leaves the file open until it is collected.

local lines: function(path: string | nupp.Path): (nupp.Files.LineIterator?, string?)

Arguments

NameTypeDescription
pathstring | nupp.Path

the file to read

Returns

TypeDescription
nupp.Files.LineIterator?

an iterator answering each line, or nil on failure

string?

a failure reason, when unsuccessful

listfunction#

Lists a directory's immediate children.

Examples#

Count the modules beside a file:

local list: nosuspend function(path: string | nupp.Path): ({nupp.Files.Entry}?, string?)

Arguments

NameTypeDescription
pathstring | nupp.Path

the directory to list

Returns

TypeDescription
{nupp.Files.Entry}?

the children in the platform's order, or nil on failure

string?

a failure reason, when unsuccessful

openfunction#

local open: function(path: string | nupp.Path, mode: nupp.Files.Mode?): (nupp.Files.File?, string?)

Arguments

NameTypeDescription
pathstring | nupp.Path
modenupp.Files.Mode?

Returns

TypeDescription
nupp.Files.File?
string?

pendingTransfersfunction#

How many whole-file transfers this program is still holding.

Whole-file reads, writes and copies settle on worker threads, and the lane that runs them is bounded. This answers what it holds, which is what a program that submitted more than it consumed needs to see.

local pendingTransfers: nosuspend function(): integer

Returns

TypeDescription
integer

the number of live transfers

readfunction#

Reads a whole file.

Examples#

Read a file's bytes, or report why not:

local read: function(path: string | nupp.Path): (string?, string?)

Arguments

NameTypeDescription
pathstring | nupp.Path

the file to read

Returns

TypeDescription
string?

the contents, or nil on failure

string?

a failure reason, when unsuccessful

removefunction#

Removes a file, a symbolic link, or a directory.

local remove: nosuspend function(path: string | nupp.Path, recursive: boolean?): (boolean, string?)

Arguments

NameTypeDescription
pathstring | nupp.Path

the path to remove

recursiveboolean?

whether to remove a directory's contents with it

Returns

TypeDescription
boolean

whether the path was removed

string?

a failure reason, when unsuccessful

renamefunction#

Renames a path, replacing an existing destination.

local rename: nosuspend function(from: string | nupp.Path, to: string | nupp.Path): (boolean, string?)

Arguments

NameTypeDescription
fromstring | nupp.Path

the path to rename

tostring | nupp.Path

the new path

Returns

TypeDescription
boolean

whether the rename happened

string?

a failure reason, when unsuccessful

setReadOnlyfunction#

Sets or clears a path's read-only attribute.

local setReadOnly: nosuspend function(path: string | nupp.Path, readOnly: boolean): (boolean, string?)

Arguments

NameTypeDescription
pathstring | nupp.Path

the path to change

readOnlyboolean

whether to refuse writes

Returns

TypeDescription
boolean

whether the attribute was set

string?

a failure reason, when unsuccessful

userFolderfunction#

Answers a well-known user folder.

local userFolder: nosuspend function(which: nupp.Files.UserFolder): (string?, string?)

Arguments

NameTypeDescription
whichnupp.Files.UserFolder

the folder to locate

Returns

TypeDescription
string?

the folder, or nil when the platform has no such folder

string?

a failure reason, when unsuccessful

writefunction#

Writes a whole file, replacing its contents.

local write: function(path: string | nupp.Path, bytes: string | nupp.ByteView): (boolean, string?)

Arguments

NameTypeDescription
pathstring | nupp.Path

the file to write

bytesstring | nupp.ByteView

the contents

Returns

TypeDescription
boolean

whether the write succeeded

string?

a failure reason, when unsuccessful

writeAtomicfunction#

Writes a whole file through a temporary beside it, so an interrupted write leaves the destination as it was rather than half replaced.

local writeAtomic: function(path: string | nupp.Path, bytes: string | nupp.ByteView): (boolean, string?)

Arguments

NameTypeDescription
pathstring | nupp.Path

the file to write

bytesstring | nupp.ByteView

the contents

Returns

TypeDescription
boolean

whether the write succeeded

string?

a failure reason, when unsuccessful