Module lsh.path
Create and manipulate filesystem paths.
Functions
| new (...) | Constructs a new path instance. |
| cwd () | Returns the current working directory. |
| home () | Returns the home directory. |
| __call (_M, ...) | Shorthand for new. |
Path attributes
| name () | A string representing the final path component. |
| parent () | The logical parent of the path. |
| parents () | An array providing access to the logical ancestors of the path. |
| suffix () | The file extension of the final component, with the leading dot (if any). |
| suffixes () | An array of the file extensions of the path, from last to first. |
| stem () | The final path component, without its suffix. |
| parts () | An array of the components of the path. |
Path methods
| type () | Returns the type of the instance. |
| join (self, ...) | Joins the path with each of the arguments in turn. |
| relative_to (self, other) | Computes a version of this path relative to other. |
| chdir (self) | Changes the working directory to this path. |
| open (self, flags, mode) | Calls lsh.fio.open on the path and returns a lsh.fio.fh file handle. |
| resolve (self) | Makes the path absolute and resolves symlinks. |
| glob (self, pattern) | Globs the given relative pattern in the directory of this path, and returns all matching files (of any kind). |
| stat (self) | Returns the file metadata. |
| with_slash (self) | Returns the path with a trailing slash. |
| exists (self) | Returns self if the path points to an existing file or directory,
false otherwise. |
| is_file (self) | Returns self if the path points to a regular file,
false otherwise. |
| is_dir (self) | Returns self if the path points to a directory,
false otherwise. |
| is_link (self) | Returns self if the path points to a symbolic link,
false otherwise. |
| is_sock (self) | Returns self if the path points to a socket,
false otherwise. |
| is_fifo (self) | Returns self if the path points to a FIFO,
false otherwise. |
| is_blk (self) | Returns self if the path points to a block device,
false otherwise. |
| is_char (self) | Returns self if the path points to a character device,
false otherwise. |
| setxattr (self, name, value[, flag]) | Sets an extended attribute. |
| getxattr (self, name) | Gets an extended attribute. |
| removexattr (self, name) | Removes an extended attribute. |
| listxattr (self) | Lists the extended attributes. |
| mkdir (self[, mode[, parents[, exists]]]) | Creates a new directory at this path. |
| rmdir (self) | Removes this directory. |
| lsdir (self) | Returns an iterator over the entries of the directory. |
| touch (self[, mode[, exists]]) | Creates the file, or updates its modification time. |
| unlink (self[, missing]) | Removes this file or symbolic link. |
| __len (self) | Returns the number of components in the path. |
| __eq (l, r) | Checks if the paths are identical. |
| __div (l, r) | Joins a path with a string, or a string with a path, and returns the new joined path instance. |
Functions
- new (...)
-
Constructs a new path instance.
Parameters:
Returns:
-
lsh.path
new path instance
Usage:
local sh = require 'lsh' sh.path.new('/', 'var')
- cwd ()
-
Returns the current working directory.
Returns:
-
lsh.path
path of the working directory
Or
- nil
- string error message
Usage:
local sh = require 'lsh' sh.path.cwd()
- home ()
-
Returns the home directory.
Returns:
-
lsh.path
path pointing to
HOMEdirectoryOr
- nil
- string error message
Usage:
local sh = require 'lsh' sh.path.home()
- __call (_M, ...)
-
Shorthand for new.
Parameters:
Returns:
-
lsh.path
new path instance
Usage:
local sh = require 'lsh' sh.path('/', 'var')
Path attributes
- name ()
-
A string representing the final path component.
Returns:
-
string
final path component
Usage:
local sh = require 'lsh' local name = sh.path('dev/init.lua').name assert(name == 'init.lua')
- parent ()
-
The logical parent of the path.
Returns:
-
lsh.path
parent
Usage:
local sh = require 'lsh' local p = sh.path('a/b/c/d/e') assert(p.parent == sh.path('a/b/c/d')) p = sh.path('/') assert(p.parent == sh.path('/')) p = sh.path('.') assert(p.parent == sh.path('.'))
- parents ()
-
An array providing access to the logical ancestors of the path.
Returns:
-
{lsh.path,...}
array of logical ancestors
Usage:
local sh = require 'lsh' local p = sh.path('/usr/local/bin/lua') assert(p.parents[1] == sh.path('/usr/local/bin')) assert(p.parents[2] == sh.path('/usr/local')) assert(p.parents[3] == sh.path('/usr')) assert(p.parents[4] == sh.path('/'))
- suffix ()
-
The file extension of the final component, with the leading dot (if any).
Returns:
-
string
suffix
Usage:
local sh = require 'lsh' assert(sh.path('dev/init.lua').suffix == '.lua') assert(sh.path('my/lib.tar.gz').suffix == '.gz') assert(sh.path('my/lib').suffix == '')
- suffixes ()
-
An array of the file extensions of the path, from last to first.
Returns:
-
{string,...}
array of extensions
Usage:
local sh = require 'lsh' local sx = sh.path('my/lib.tar.gar').suffixes assert(sx[1] == '.gar') assert(sx[2] == '.tar') sx = sh.path('my/lib').suffixes assert(#sx == 0)
- stem ()
-
The final path component, without its suffix.
Returns:
Usage:
local sh = require 'lsh' assert(sh.path('my/lib.tar.gz').stem == 'lib.tar') assert(sh.path('my/lib.tar').stem == 'lib') assert(sh.path('my/lib').stem == 'lib')
- parts ()
-
An array of the components of the path.
Returns:
Usage:
local sh = require 'lsh' local p = sh.path('/usr/local/bin/lua') assert(p.parts[1] == '/') assert(p.parts[2] == 'usr') assert(p.parts[3] == 'local') assert(p.parts[4] == 'bin') assert(p.parts[5] == 'lua')
Path methods
- type ()
-
Returns the type of the instance.
Returns:
-
the string
"path"Usage:
local sh = require 'lsh' assert(sh.path('/'):type() == 'path')
- join (self, ...)
-
Joins the path with each of the arguments in turn.
Parameters:
Returns:
-
lsh.path
new joined path instance
Or
- nil
- string error message
Usage:
local sh = require 'lsh' local p = sh.path('/etc') assert(p:join('passwd') == sh.path('/etc/passwd')) assert(p:join(sh.path('passwd')) == sh.path('/etc/passwd')) assert(p:join('nginx', 'nginx.conf') == sh.path('/etc/nginx/nginx.conf'))
- relative_to (self, other)
-
Computes a version of this path relative to
other. If the path does not start withother, it returnsniland an error message.Parameters:
- self lsh.path
- other lsh.path or string
Returns:
-
lsh.path
new relative path instance
Or
- nil
- string error message
Usage:
local sh = require 'lsh' local p = sh.path('/etc/passwd') assert(p:relative_to('/') == sh.path('etc/passwd')) assert(p:relative_to('/etc') == sh.path('passwd'))
- chdir (self)
-
Changes the working directory to this path.
Parameters:
- self lsh.path
Returns:
-
lsh.path
self
Or
- nil
- string error message
Usage:
local sh = require 'lsh' sh.path('/home/user'):chdir()
- open (self, flags, mode)
-
Calls lsh.fio.open on the path and returns
a lsh.fio.fh file handle.
This interface is not final. It will change in incompatible ways.
Parameters:
- self lsh.path
- flags table lsh.fio.open flags
- mode table lsh.fio.open mode
Returns:
-
lsh.fio.fh
file handle
Or
- nil
- string error message
Usage:
local sh = require 'lsh' local fh = sh.path('/etc/hostname'):open('rdonly')
- resolve (self)
-
Makes the path absolute and resolves symlinks.
Symlink resolution is not implemented yet.
Parameters:
- self lsh.path
Returns:
-
lsh.path
resolved path instance
Or
- nil
- string error message
Usage:
local sh = require 'lsh' local p = sh.path('.') assert(p:resolve() == sh.path.cwd())
- glob (self, pattern)
-
Globs the given relative pattern in the directory of this path,
and returns all matching files (of any kind).
Parameters:
- self lsh.path
- pattern string pattern to match
Returns:
Or
- nil
- string error message
Usage:
local sh = require 'lsh' sh.path('.'):glob('*.lua') local res, err = sh.path('/'):glob('*/*') assert(type(res) == 'table') if err then for k,v in pairs(err) do print(k,v) end end --> /root Permission denied
- stat (self)
-
Returns the file metadata.
This interface is not final. It will change in incompatible ways.
Parameters:
- self lsh.path
Returns:
-
cdata
ljsyscall stat_t
Or
-
nil
Usage:
local sh = require 'lsh' local st = sh.path('.'):stat()
- with_slash (self)
-
Returns the path with a trailing slash.
Parameters:
- self lsh.path
Returns:
-
string
path with trailing slash
Usage:
local sh = require 'lsh' assert(sh.path('/var'):with_slash() == '/var/') assert(sh.path('log.txt'):with_slash() == 'log.txt/') assert(sh.path('/'):with_slash() == '/')
- exists (self)
-
Returns
selfif the path points to an existing file or directory,falseotherwise.Parameters:
- self lsh.path
Returns:
-
lsh.path
selfOr
-
false
Usage:
local sh = require 'lsh' assert(sh.path('.'):exists())
- is_file (self)
-
Returns
selfif the path points to a regular file,falseotherwise.It also returns
falseif the path does not exist or is a broken symlink.Parameters:
- self lsh.path
Returns:
-
lsh.path
selfOr
-
false
Usage:
local sh = require 'lsh' assert(sh.path('/etc/resolv.conf'):is_file())
- is_dir (self)
-
Returns
selfif the path points to a directory,falseotherwise.It also returns
falseif the path does not exist or is a broken symlink.Parameters:
- self lsh.path
Returns:
-
lsh.path
selfOr
-
false
- is_link (self)
-
Returns
selfif the path points to a symbolic link,falseotherwise.It also returns
falseif the path does not exist.Parameters:
- self lsh.path
Returns:
-
lsh.path
selfOr
-
false
- is_sock (self)
-
Returns
selfif the path points to a socket,falseotherwise.It also returns
falseif the path does not exist or is a broken symlink.Parameters:
- self lsh.path
Returns:
-
lsh.path
selfOr
-
false
- is_fifo (self)
-
Returns
selfif the path points to a FIFO,falseotherwise.It also returns
falseif the path does not exist or is a broken symlink.Parameters:
- self lsh.path
Returns:
-
lsh.path
selfOr
-
false
- is_blk (self)
-
Returns
selfif the path points to a block device,falseotherwise.It also returns
falseif the path does not exist or is a broken symlink.Parameters:
- self lsh.path
Returns:
-
lsh.path
selfOr
-
false
- is_char (self)
-
Returns
selfif the path points to a character device,falseotherwise.It also returns
falseif the path does not exist or is a broken symlink.Parameters:
- self lsh.path
Returns:
-
lsh.path
selfOr
-
false
- setxattr (self, name, value[, flag])
-
Sets an extended attribute.
Parameters:
- self lsh.path
- name string name of the attribute
- value string value of the attribute
- flag
string
"CREATE"or"REPLACE"(optional)
Returns:
-
bool
trueOr
- nil
- string error
- getxattr (self, name)
-
Gets an extended attribute.
Parameters:
- self lsh.path
- name string name of the attribute
Returns:
-
string
value of the attribute
Or
- nil
- string error
- removexattr (self, name)
-
Removes an extended attribute.
Parameters:
- self lsh.path
- name string name of the attribute
Returns:
-
bool
trueOr
- nil
- string error
- listxattr (self)
-
Lists the extended attributes.
Parameters:
- self lsh.path
Returns:
-
{string,...}
array of attribute names
Or
- nil
- string error
- mkdir (self[, mode[, parents[, exists]]])
-
Creates a new directory at this path.
If
modeis given, theumaskof the process is applied to it to get the file mode and access flags.If parents is
true, it creates the missing parents of this path. The parents get the default permissions and do not usemode(the same as the POSIXmkdir -pcommand). If parents isfalse(the default) and a parent is missing, it returnsniland an error message.If exists is
false(the default) and the directory already exists, it returnsniland an error message. If exists istrue, it ignores this error (the same as the POSIXmkdir -pcommand).Parameters:
- self lsh.path
- mode
string
set mode (default is
0755) (optional) - parents bool whether to create parent directories (optional)
- exists bool whether to ignore file exist errors (optional)
Returns:
-
bool
trueOr
- nil
- string error
- rmdir (self)
-
Removes this directory. The directory must be empty.
Parameters:
- self lsh.path
Returns:
-
bool
trueOr
- nil
- string error
- lsdir (self)
-
Returns an iterator over the entries of the directory.
Each call of the iterator with
dir_objreturns a directory entry, ornilif there are no more entries. You can also iterate withdir_obj:next(). To close the directory before the iteration ends, calldir_obj:close(). If the path is not a directory, it returnsniland an error message.Parameters:
- self lsh.path
Returns:
- func iterator that returns directory entries
- table dir_obj directory object
Or
- nil
- string error
Usage:
local sh = require 'lsh' for name in sh.path('/etc'):lsdir() do print(name) end
- touch (self[, mode[, exists]])
-
Creates the file, or updates its modification time.
If
modeis given, theumaskof the process is applied to it to get the file mode and access flags.If the file already exists and exists is
true, it sets the modification time to the current time. If the file already exists and exists isfalse, it returnsniland an error message.Parameters:
- self lsh.path
- mode
string
set mode (default is
0666) (optional) - exists bool whether to ignore file exist errors (optional)
Returns:
-
bool
trueOr
- nil
- string error
- unlink (self[, missing])
-
Removes this file or symbolic link.
If the path points to a directory, use rmdir.
If
missingisfalse(the default) and the path does not exist, it returnsniland an error message. Ifmissingistrue, it ignores this error (the same as the POSIXrm -fcommand).Parameters:
- self lsh.path
- missing bool whether to ignore a missing file (optional)
Returns:
-
bool
trueOr
- nil
- string error
- __len (self)
-
Returns the number of components in the path.
Parameters:
- self lsh.path
Returns:
-
number
number of components
Usage:
local sh = require 'lsh' assert(#sh.path('/etc', 'resolv.conf') == 3)
- __eq (l, r)
-
Checks if the paths are identical.
Parameters:
- l lsh.path left value
- r lsh.path right value
Returns:
-
bool
Or
- nil
- string error message
Usage:
local sh = require 'lsh' assert(sh.path('/etc', 'resolv.conf') == sh.path('/etc', 'resolv.conf'))
- __div (l, r)
-
Joins a path with a string, or a string with a path,
and returns the new joined path instance.
Parameters:
Returns:
-
lsh.path
new joined path instance
Or
- nil
- string error message
Usage:
local sh = require 'lsh' assert(sh.path('/etc'):join('resolv.conf') == sh.path('/etc') / 'resolv.conf')