Introduction
LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution.
LuaFileSystem offers a portable way to access the underlying directory structure and file attributes.
Building
LuaFileSystem should be built with Lua 5.1 so the language library and header files for the target version must be installed properly.
LuaFileSystem offers a Makefile and a separate configuration file,
config,
which should be edited to suit your installation before running
make.
The file has some definitions like paths to the external libraries,
compiler options and the like.
On Windows, the C runtime used to compile LuaFileSystem must be the same runtime that Lua uses, or some LuaFileSystem functions will not work.
Installation
The easiest way to install LuaFileSystem is to use LuaRocks:
luarocks install luafilesystem
If you prefer to install LuaFileSystem manually, the compiled binary should be copied to a directory in your C path.
Reference
LuaFileSystem offers the following functions:
lfs.attributes (filepath [, aname])- Returns a table with the file attributes corresponding to
filepath(ornilfollowed by an error message in case of error). If the second optional argument is given, then only the value of the named attribute is returned (this use is equivalent tolfs.attributes(filepath).aname, but the table is not created and only one attribute is retrieved from the O.S.). The attributes are described as follows; attributemodeis a string, all the others are numbers, and the time related attributes use the same time reference ofos.time:dev- on Unix systems, this represents the device that the inode resides on. On Windows systems, represents the drive number of the disk containing the file
ino- on Unix systems, this represents the inode number. On Windows systems this has no meaning
mode- string representing the associated protection mode (the values could be
file,directory,link,socket,named pipe,char device,block deviceorother) nlink- number of hard links to the file
uid- user-id of owner (Unix only, always 0 on Windows)
gid- group-id of owner (Unix only, always 0 on Windows)
rdev- on Unix systems, represents the device type, for special file inodes.
On Windows systems represents the same as
dev access- time of last access
modification- time of last data modification
change- time of last file status change
size- file size, in bytes
blocks- block allocated for file; (Unix only)
blksize- optimal file system I/O blocksize; (Unix only)
statinternally thus if the givenfilepathis a symbolic link, it is followed (if it points to another link the chain is followed recursively) and the information is about the file it refers to. To obtain information about the link itself, see function lfs.symlinkattributes. lfs.chdir (path)- Changes the current working directory to the given
path.
Returnstruein case of success ornilplus an error string. lfs.currentdir ()- Returns a string with the current working directory or
nilplus an error string. lfs.dir (path)-
Lua iterator over the entries of a given directory.
Each time the iterator is called it returns a directory entry's name as a string, or
nilif there are no more entries. Raises an error ifpathis not a directory. lfs.lock (filehandle, mode[, start[, length]])- Locks a file or a part of it. This function works on open files; the
file handle should be specified as the first argument.
The string
modecould be eitherr(for a read/shared lock) orw(for a write/exclusive lock). The optional argumentsstartandlengthcan be used to specify a starting point and its length; both should be numbers.
Returnstrueif the operation was successful; in case of error, it returnsnilplus an error string. lfs.mkdir (dirname)- Creates a new directory. The argument is the name of the new
directory.
Returnstrueif the operation was successful; in case of error, it returnsnilplus an error string. lfs.rmdir (dirname)- Removes an existing directory. The argument is the name of the directory.
Returnstrueif the operation was successful; in case of error, it returnsnilplus an error string. lfs.setmode (file, mode)- Sets the writing mode for a file. The mode string can be either
binaryortext. Returns the previous mode string for the file. This function is only available in Windows, so you may want to make sure thatlfs.setmodeexists before using it. lfs.symlinkattributes (filepath [, aname])- Identical to lfs.attributes except that
it obtains information about the link itself (not the file it refers to).
This function is not available in Windows so you may want to make sure that
lfs.symlinkattributesexists before using it. lfs.touch (filepath [, atime [, mtime]])- Set access and modification times of a file. This function is
a bind to
utimefunction. The first argument is the filename, the second argument (atime) is the access time, and the third argument (mtime) is the modification time. Both times are provided in seconds (which should be generated with Lua standard functionos.time). If the modification time is omitted, the access time provided is used; if both times are omitted, the current time is used.
Returnstrueif the operation was successful; in case of error, it returnsnilplus an error string. lfs.unlock (filehandle[, start[, length]])- Unlocks a file or a part of it. This function works on
open files; the file handle should be specified as the first
argument. The optional arguments
startandlengthcan be used to specify a starting point and its length; both should be numbers.
Returnstrueif the operation was successful; in case of error, it returnsnilplus an error string.
