Module irccd.fs

Filesystem support.

Open, read and analyze directories.

Functions

basename (path) Get the file name of a full path.
dirname (path) Get the parent path of a file.
exists (path) Check if a file exists.
mkdir (path, mode) Create a directory recursively.
opendir (path, skipSpecial) Open a directory.
stat (path) Get file status.

Class Directory

Directory:count () Count the number of entries in the directory.
Directory:read () Iterate over the entries.

Directory metamethods

Directory:__eq (other) Test equality.
Directory:__tostring () Convert object to string.


Functions

basename (path)
Get the file name of a full path. Returns the file name only of a full path. For instance, returns "foo.lua" when calling with "/usr/local/foo.lua"

Parameters:

  • path the file path

Returns:

    the file name
dirname (path)
Get the parent path of a file. This function may be used to get the parent path of a file. Example: calling dirname on "/foo/bar/test.txt" will returns "/foo/bar".

Parameters:

  • path the full file path

Returns:

    the parent path
exists (path)
Check if a file exists. Tests if a directory or a file exists.

Parameters:

  • path the path to check

Returns:

    true if exists
mkdir (path, mode)
Create a directory recursively. This function acts like mkdir -p.

Parameters:

  • path the path
  • mode an optional octal mode

Returns:

  1. true on success or false plus an error
  2. the error message
opendir (path, skipSpecial)
Open a directory. Optional boolean skipSpecial may be passed to avoid ".", ".." for being included.

Parameters:

  • path the path
  • skipSpecial forget "." and "..", default: false

Returns:

    a Directory object

See also:

stat (path)
Get file status. Returns a table with the following information:
  • device: the device number
  • inode: the inode number
  • nlink: the number of hard links to the file
  • atime: the last time the file was accessed
  • mtime: time when file data last modified
  • ctime: time when file status was last changed
  • size: the file size in bytes
  • blocksize: the optional I/O block size
  • blocks: the number of blocks for the file

Note: not all fields are available.
Note: you must load the irccd.util library for fields atime, ctime and mtime.

Parameters:

  • path the path to the file

Returns:

  1. the table or nil
  2. the error message

See also:

Usage:

     local fs = require "irccd.fs"
     local util = require "irccd.util"
    
     local status = fs.stat("/path/to/file")
    
     if status.mtime then
             print(string.format("Last modified on: %s", status.mtime:format("%d %a %y %H:%M:%S")))
     end

Class Directory

Directory:count ()
Count the number of entries in the directory. Note that if you didn't passed true to fs.opendir() you will have "." and ".." included.

Returns:

    the number of entries in the directory

See also:

Directory:read ()
Iterate over the entries. This function returns 2 arguments the entry and a boolean telling if is a directory.

Returns:

  1. the entry name
  2. true if it is a directory

Usage:

     local d = fs.opendir("/")
    
     -- Read the directory "/"
     for e, isdir in d:read() do
             if isdir then
                     print(e .. " is a directory")
             else
                     print("=>" .. e)
             end
     end

Directory metamethods

The metamethods available for object Directory.
Directory:__eq (other)
Test equality.

Parameters:

  • other the other directory

Returns:

    true if they equals
Directory:__tostring ()
Convert object to string.

Returns:

    a string
generated by LDoc 1.4.0