nupp.io.files
Portable filesystem access accepts native paths on native hosts and application-owned paths on browser hosts. Set an application identity before resolving the portable configuration, data, or cache directory.
local files = nupp.io.files
files.setApplicationIdentity("example", "notes")
local data = assert(files.dataPath())
assert(files.write(data:join("notes.txt"), "hello"))Native programs may also pass ordinary strings or nupp.io.path.Path values. Browser programs refuse raw path text and use the opaque ApplicationPath values returned by configurationPath, dataPath, or cachePath.
Browser application data lives in the Origin Private File System. It remains evictable unless requestPersistentStorage succeeds. Capability flags describe the operations whose native semantics have no browser equivalent.
Submodules
| Module | Description |
|---|---|
nupp.io.files.messages |
Module contents
Types
| Type | Kind | Description |
|---|---|---|
ApplicationPath | interface | An opaque portable path rooted in this application's platform storage. |
Capabilities | record | The exact filesystem guarantees supplied by the selected provider. |
Entry | record | An immediate child returned by a directory listing. |
File | interface | An affine open file, which is itself a reader and a writer over its one cursor. |
Info | record | Metadata about one filesystem object. |
Kind | type | The kind of filesystem object described by metadata or a directory entry. |
Mode | type | A file open mode: read, write, append, or an updating variant. |
Origin | type | The base used by a file seek. |
Path | type | A native path or an opaque application-owned path. |
TemporaryOptions | record | Naming and placement options for a native temporary path. |
TemporaryPath | interface | A native temporary path removed when its owner closes unless persisted. |
UserFolder | type | A portable name for a conventional user directory. |
Functions
| Function | Kind | Description |
|---|---|---|
append | function | Appends bytes to a file, creating it when absent. |
applicationIdentity | function | Returns the selected application identity, or nil values before one is set. |
cachePath | function | Resolves and creates this application's platform cache directory. |
configurationPath | function | Resolves and creates this application's platform configuration directory. |
copy | function | Copies a file over another. |
createDirectory | function | Creates a directory and all missing parents. |
createSymlink | function | Creates a symbolic link when the provider supports one. |
createTemporaryDirectory | function | Creates a native temporary directory owner when the provider supports one. |
createTemporaryFile | function | Creates a native temporary file owner when the provider supports one. |
dataPath | function | Resolves and creates this application's platform data directory. |
executableDirectory | function | Returns the native running executable's directory. |
exists | function | Reports whether a path resolves. |
glob | function | Expands a native filesystem glob. |
info | function | Describes the filesystem object at a path. |
isDirectory | function | Reports whether a path resolves to a directory. |
isFile | function | Reports whether a path resolves to a file. |
isSymlink | function | Reports whether a path itself is a symbolic link. |
lines | function | Opens a file as a line reader, which takes the file and closes it with itself. |
list | function | Lists a directory's immediate children. |
open | function | Opens a file in one of the six standard read, write, append, or update modes. |
read | function | Reads an entire file as bytes. |
readLink | function | Reads a symbolic link target without resolving it. |
remove | function | Removes a file, link, or directory. |
rename | function | Renames a path, replacing an existing destination when supported. |
requestPersistentStorage | function | Requests non-evictable browser storage; native providers succeed without work. |
setApplicationIdentity | function | Selects the identity used to resolve configuration, data, and cache roots. |
setReadOnly | function | Sets or clears a path's native read-only attribute. |
userFolder | function | Resolves a conventional native user folder. |
write | function | Replaces a file with the supplied bytes. |
writeAtomic | function | Atomically replaces a file when the provider supports it. |
Values
| Value | Kind | Description |
|---|---|---|
capabilities | variable | Reports the exact guarantees of the selected filesystem provider. |
pendingTransfers | variable | Reports whole-file transfers that have not settled. |
Types#
ApplicationPathinterface#
sealed interface ApplicationPath ...An opaque portable path rooted in this application's platform storage.
Members
| Name | Kind | Description |
|---|---|---|
join | method | Appends portable path components without allowing an escape from the root. |
toString | method | Returns a display or native-interoperability form that does not retain authority. |
joinmethod#
join: function(self, ...: string): ApplicationPathAppends portable path components without allowing an escape from the root.
Arguments
| Name | Type | Description |
|---|---|---|
? | self | |
... | string |
Returns
| Type | Description |
|---|---|
ApplicationPath |
toStringmethod#
toString: function(self): stringReturns a display or native-interoperability form that does not retain authority.
Arguments
| Name | Type | Description |
|---|---|---|
? | self |
Returns
| Type | Description |
|---|---|
string |
Capabilitiesrecord#
record Capabilities ...The exact filesystem guarantees supplied by the selected provider.
Members
| Name | Kind | Description |
|---|---|---|
available | field | Whether filesystem access is available at all. |
arbitraryPaths | field | Whether ordinary native paths are accepted. |
symbolicLinks | field | Whether symbolic links can be queried and created. |
readOnlyAttributes | field | Whether read-only attributes can be changed. |
glob | field | Whether filesystem globbing is supported. |
rename | field | Whether paths can be renamed. |
atomicWrite | field | Whether atomic replacement writes are supported. |
temporaryPaths | field | Whether temporary path owners can be created. |
concurrentOpenHandles | field | Whether a |
currentDirectory | field | Whether a process working directory exists. |
userFolders | field | Whether conventional user folders can be resolved. |
executableDirectory | field | Whether the running executable's directory can be resolved. |
persistentStorageRequest | field | Whether persistent browser storage can be requested. |
executableDirectoryfield#
executableDirectory: booleanWhether the running executable's directory can be resolved.
persistentStorageRequestfield#
persistentStorageRequest: booleanWhether persistent browser storage can be requested.
Entryrecord#
record Entry ...An immediate child returned by a directory listing.
Fileinterface#
interface File is io.Reader, io.Writer ...An affine open file, which is itself a reader and a writer over its one cursor.
Members
| Name | Kind | Description |
|---|---|---|
size | method | Returns the file's byte length without moving its cursor. |
seek | method | Moves the cursor relative to an origin and returns its new position. |
position | method | Returns the shared cursor position. |
flush | method | Publishes buffered file writes, and reports a write that failed to land. |
isReleased | method | Reports whether the file has already been closed. |
close | method | Closes the file and spends its ownership. |
sizemethod#
size: function(borrows self: File): (integer?, io.Error?)Returns the file's byte length without moving its cursor.
Arguments
| Name | Type | Description |
|---|---|---|
borrows self | File |
Returns
| Type | Description |
|---|---|
integer? | |
io.Error? |
seekmethod#
seek: function(exclusive self: File, offset: integer?, origin: messages.Origin?): (integer?, io.Error?)Moves the cursor relative to an origin and returns its new position.
Arguments
| Name | Type | Description |
|---|---|---|
exclusive self | File | |
offset | integer? | |
origin | messages.Origin? |
Returns
| Type | Description |
|---|---|
integer? | |
io.Error? |
positionmethod#
position: function(exclusive self: File): (integer?, io.Error?)Returns the shared cursor position.
Arguments
| Name | Type | Description |
|---|---|---|
exclusive self | File |
Returns
| Type | Description |
|---|---|
integer? | |
io.Error? |
flushmethod#
flush: function(borrows self: File): (boolean, string?)Publishes buffered file writes, and reports a write that failed to land.
Arguments
| Name | Type | Description |
|---|---|---|
borrows self | File |
Returns
| Type | Description |
|---|---|
boolean | |
string? |
Inforecord#
record Info ...Metadata about one filesystem object.
Members
| Name | Kind | Description |
|---|---|---|
kind | field | The object's filesystem kind. |
size | field | Its byte size, or zero for a directory. |
modified | field | Its modification time in milliseconds since the Unix epoch, the unit nupp.time.wallTime returns in, with whatever... |
readOnly | field | Whether the native platform refuses writes. |
modifiedfield#
modified: numberIts modification time in milliseconds since the Unix epoch, the unit nupp.time.wallTime returns in, with whatever fraction the platform records.
Kindtype#
type Kind = "file" | "directory" | "symlink" | "other"The kind of filesystem object described by metadata or a directory entry.
Modetype#
type Mode = "r" | "w" | "a" | "r+" | "w+" | "a+"A file open mode: read, write, append, or an updating variant.
Origintype#
type Origin = "set" | "current" | "end"The base used by a file seek.
Pathtype#
type Path = string | paths.Path | appPaths.ApplicationPathA native path or an opaque application-owned path.
TemporaryOptionsrecord#
record TemporaryOptions ...Naming and placement options for a native temporary path.
Members
| Name | Kind | Description |
|---|---|---|
directory | field | The native directory to create the path in. |
prefix | field | A separator-free fragment placed before the random portion of the name. |
suffix | field | A separator-free fragment placed after the random portion of the name. |
prefixfield#
prefix: string?A separator-free fragment placed before the random portion of the name.
TemporaryPathinterface#
A native temporary path removed when its owner closes unless persisted.
Members
| Name | Kind | Description |
|---|---|---|
toString | method | Returns the native path text. |
path | method | Returns the created path as a path value. |
persist | method | Moves the temporary path to a durable destination. |
isReleased | method | Reports whether the temporary owner has been closed or persisted. |
close | method | Removes the temporary path and spends its ownership. |
toStringmethod#
toString: function(self: TemporaryPath): stringReturns the native path text.
Arguments
| Name | Type | Description |
|---|---|---|
self | TemporaryPath |
Returns
| Type | Description |
|---|---|
string |
pathmethod#
path: function(self: TemporaryPath): paths.PathReturns the created path as a path value.
Arguments
| Name | Type | Description |
|---|---|---|
self | TemporaryPath |
Returns
| Type | Description |
|---|---|
paths.Path |
persistmethod#
persist: function(self: TemporaryPath, destination: any): (boolean, io.Error?)Moves the temporary path to a durable destination.
Arguments
| Name | Type | Description |
|---|---|---|
self | TemporaryPath | |
destination | any |
Returns
| Type | Description |
|---|---|
boolean | |
io.Error? |
isReleasedmethod#
isReleased: function(self: TemporaryPath): booleanReports whether the temporary owner has been closed or persisted.
Arguments
| Name | Type | Description |
|---|---|---|
self | TemporaryPath |
Returns
| Type | Description |
|---|---|
boolean |
closemethod#
close: function(takes self: TemporaryPath): nilRemoves the temporary path and spends its ownership. Repeated calls, and a call after persist, are safe and do nothing.
Arguments
| Name | Type | Description |
|---|---|---|
takes self | TemporaryPath |
Returns
| Type | Description |
|---|---|
nil |
UserFoldertype#
type UserFolder = "home" | "documents" | "downloads" | "desktop" | "pictures" | "music" | "videos"A portable name for a conventional user directory.
Functions#
appendfunction#
Appends bytes to a file, creating it when absent.
Arguments
| Name | Type | Description |
|---|---|---|
path | Path | the file to append to |
borrows bytes | string | io.ByteView | the byte string or view to append |
Returns
| Type | Description |
|---|---|
boolean | whether the bytes were written |
io.Error? | a failure reason, when unsuccessful |
applicationIdentityfunction#
function applicationIdentity(): string?, string?Returns the selected application identity, or nil values before one is set.
Returns
| Type | Description |
|---|---|
string? | the organization component |
string? | the application component |
cachePathfunction#
function cachePath(): appPaths.ApplicationPath?, io.Error?Resolves and creates this application's platform cache directory.
Returns
| Type | Description |
|---|---|
appPaths.ApplicationPath? | the cache path, or nil on failure |
io.Error? | a failure reason, when unsuccessful |
configurationPathfunction#
function configurationPath(): appPaths.ApplicationPath?, io.Error?Resolves and creates this application's platform configuration directory.
Configuration, data and cache are always three distinct directories, so clearing one with a recursive remove never reaches another.
Returns
| Type | Description |
|---|---|
appPaths.ApplicationPath? | the configuration path, or nil on failure |
io.Error? | a failure reason, when unsuccessful |
copyfunction#
Copies a file over another.
Arguments
| Name | Type | Description |
|---|---|---|
from | Path | the source file |
to | Path | the destination file |
Returns
| Type | Description |
|---|---|
boolean | whether the file was copied |
io.Error? | a failure reason, when unsuccessful |
createDirectoryfunction#
Creates a directory and all missing parents.
Arguments
| Name | Type | Description |
|---|---|---|
path | Path | the directory to create |
Returns
| Type | Description |
|---|---|
boolean | whether the directory exists afterward |
io.Error? | a failure reason, when unsuccessful |
createSymlinkfunction#
function createSymlink(targetPath: Path, link: Path, kind: ("file" | "directory")?): boolean, io.Error?Creates a symbolic link when the provider supports one.
Arguments
| Name | Type | Description |
|---|---|---|
targetPath | Path | the path the link names |
link | Path | where to create the link |
kind | ("file" | "directory")? | whether a Windows link names a file or directory |
Returns
| Type | Description |
|---|---|
boolean | whether the link was created |
io.Error? | a failure reason, when unsuccessful |
createTemporaryDirectoryfunction#
function createTemporaryDirectory(options: messages.TemporaryOptions?): affine(contracts.TemporaryPath?), io.Error?Creates a native temporary directory owner when the provider supports one.
Arguments
| Name | Type | Description |
|---|---|---|
options | messages.TemporaryOptions? | naming and placement options |
Returns
| Type | Description |
|---|---|
affine(contracts.TemporaryPath?) | the temporary path owner, or nil on failure |
io.Error? | a failure reason, when unsuccessful |
createTemporaryFilefunction#
function createTemporaryFile(options: messages.TemporaryOptions?): affine(contracts.TemporaryPath?), io.Error?Creates a native temporary file owner when the provider supports one.
Arguments
| Name | Type | Description |
|---|---|---|
options | messages.TemporaryOptions? | naming and placement options |
Returns
| Type | Description |
|---|---|
affine(contracts.TemporaryPath?) | the temporary path owner, or nil on failure |
io.Error? | a failure reason, when unsuccessful |
dataPathfunction#
function dataPath(): appPaths.ApplicationPath?, io.Error?Resolves and creates this application's platform data directory.
Returns
| Type | Description |
|---|---|
appPaths.ApplicationPath? | the data path, or nil on failure |
io.Error? | a failure reason, when unsuccessful |
executableDirectoryfunction#
function executableDirectory(): paths.Path?, io.Error?Returns the native running executable's directory.
Returns
| Type | Description |
|---|---|
paths.Path? | the executable directory, or nil when unavailable |
io.Error? | a failure reason, when unsuccessful |
existsfunction#
Reports whether a path resolves.
Any failure reads as false, a refused permission or an I/O error as much as a missing name. Ask info when the reason matters.
Arguments
| Name | Type | Description |
|---|---|---|
path | Path | the path to inspect |
Returns
| Type | Description |
|---|---|
boolean | whether it exists |
globfunction#
function glob(pattern: string): {paths.Path}?, io.Error?Expands a native filesystem glob.
Arguments
| Name | Type | Description |
|---|---|---|
pattern | string | the glob pattern |
Returns
| Type | Description |
|---|---|
{paths.Path}? | sorted matching paths, or nil on failure |
io.Error? | a failure reason, when unsuccessful |
infofunction#
Describes the filesystem object at a path.
Arguments
| Name | Type | Description |
|---|---|---|
path | Path | the path to inspect |
Returns
| Type | Description |
|---|---|
messages.Info? | its metadata, or nil on failure |
io.Error? | a failure reason, when unsuccessful |
isDirectoryfunction#
Reports whether a path resolves to a directory.
Any failure reads as false; ask info when the reason matters.
Arguments
| Name | Type | Description |
|---|---|---|
path | Path | the path to inspect |
Returns
| Type | Description |
|---|---|
boolean | whether it is a directory |
isFilefunction#
Reports whether a path resolves to a file.
Any failure reads as false; ask info when the reason matters.
Arguments
| Name | Type | Description |
|---|---|---|
path | Path | the path to inspect |
Returns
| Type | Description |
|---|---|
boolean | whether it is a file |
isSymlinkfunction#
Reports whether a path itself is a symbolic link.
Any failure reads as false, and so does a provider with no links at all; capabilities().symbolicLinks says which that is.
Arguments
| Name | Type | Description |
|---|---|---|
path | Path | the path to inspect |
Returns
| Type | Description |
|---|---|
boolean | whether it is a symbolic link |
linesfunction#
Opens a file as a line reader, which takes the file and closes it with itself.
A line is read without its terminator, \n or \r\n. A read that fails partway through returns nil and the reason from Lines:read, which is what tells it apart from the end of the file, where the reason is nil.
local log = assert(nupp.io.files.lines("access.log"))
while true do
local line, reason = log:read()
if line == nil then
assert(reason == nil, reason)
break
end
print(line)
endArguments
| Name | Type | Description |
|---|---|---|
path | Path | the file to read |
limit | integer? | the most bytes one line may hold, or 65536 |
Returns
| Type | Description |
|---|---|
affine(io.Lines)? | the line reader, or nil when the file cannot be opened |
io.Error? | a failure reason, when unsuccessful |
Raises
| Type | Condition |
|---|---|
string | when limit is not a positive integer |
listfunction#
Lists a directory's immediate children.
Arguments
| Name | Type | Description |
|---|---|---|
path | Path | the directory to list |
Returns
| Type | Description |
|---|---|
{messages.Entry}? | its entries, or nil on failure |
io.Error? | a failure reason, when unsuccessful |
openfunction#
Opens a file in one of the six standard read, write, append, or update modes.
Arguments
| Name | Type | Description |
|---|---|---|
path | Path | the native path or browser application path to open |
mode | messages.Mode? | the access mode, defaulting to read |
Returns
| Type | Description |
|---|---|
affine(contracts.File?) | the open file owner, or nil on failure |
io.Error? | a failure reason, when unsuccessful |
readfunction#
Reads an entire file as bytes.
Arguments
| Name | Type | Description |
|---|---|---|
path | Path | the file to read |
Returns
| Type | Description |
|---|---|
string? | its bytes, or nil on failure |
io.Error? | a failure reason, when unsuccessful |
readLinkfunction#
Reads a symbolic link target without resolving it.
Arguments
| Name | Type | Description |
|---|---|---|
path | Path | the link to read |
Returns
| Type | Description |
|---|---|
paths.Path? | its target, or nil on failure |
io.Error? | a failure reason, when unsuccessful |
removefunction#
Removes a file, link, or directory.
Arguments
| Name | Type | Description |
|---|---|---|
path | Path | the path to remove |
recursive | boolean? | whether to remove a directory's contents |
Returns
| Type | Description |
|---|---|
boolean | whether the path was removed |
io.Error? | a failure reason, when unsuccessful |
renamefunction#
Renames a path, replacing an existing destination when supported.
Arguments
| Name | Type | Description |
|---|---|---|
from | Path | the source path |
to | Path | the destination path |
Returns
| Type | Description |
|---|---|
boolean | whether the path was renamed |
io.Error? | a failure reason, when unsuccessful |
requestPersistentStoragefunction#
function requestPersistentStorage(): boolean, io.Error?Requests non-evictable browser storage; native providers succeed without work.
Returns
| Type | Description |
|---|---|
boolean | whether persistence is guaranteed |
io.Error? | a failure reason, when unsuccessful |
setApplicationIdentityfunction#
function setApplicationIdentity(newOrganization: string, newApplication: string): nilSelects the identity used to resolve configuration, data, and cache roots.
Arguments
| Name | Type | Description |
|---|---|---|
newOrganization | string | the organization component |
newApplication | string | the application component |
Returns
| Type | Description |
|---|---|
nil | nil after selecting the identity |
setReadOnlyfunction#
Sets or clears a path's native read-only attribute.
Arguments
| Name | Type | Description |
|---|---|---|
path | Path | the path to change |
readOnly | boolean | whether writes should be refused |
Returns
| Type | Description |
|---|---|
boolean | whether the attribute was changed |
io.Error? | a failure reason, when unsuccessful |
userFolderfunction#
function userFolder(which: messages.UserFolder): paths.Path?, io.Error?Resolves a conventional native user folder.
Arguments
| Name | Type | Description |
|---|---|---|
which | messages.UserFolder | the folder to resolve |
Returns
| Type | Description |
|---|---|
paths.Path? | its native path, or nil when unavailable |
io.Error? | a failure reason, when unsuccessful |
writefunction#
Replaces a file with the supplied bytes.
Arguments
| Name | Type | Description |
|---|---|---|
path | Path | the file to write |
borrows bytes | string | io.ByteView | the byte string or view to write |
Returns
| Type | Description |
|---|---|
boolean | whether the bytes were written |
io.Error? | a failure reason, when unsuccessful |
writeAtomicfunction#
Atomically replaces a file when the provider supports it.
Arguments
| Name | Type | Description |
|---|---|---|
path | Path | the destination file |
borrows bytes | string | io.ByteView | the byte string or view to write |
Returns
| Type | Description |
|---|---|
boolean | whether the file was replaced |
io.Error? | a failure reason, when unsuccessful |
Values#
capabilitiesvariable#
const capabilitiesReports the exact guarantees of the selected filesystem provider.
pendingTransfersvariable#
const pendingTransfersReports whole-file transfers that have not settled.