file(3)

MODULE
file - File Interface Module

DESCRIPTION

The module file provides an interface to the file system.

Most functions have a name argument such as a file name or directory name, which is either an atom, a string, or a deep list of characters and atoms. A path is a list of directory names. If the functions are successful, they return ok, or {ok, Value}.

If an error occurs, the return value has the format {error, Reason}. Reason is an atom which is named from the Posix error codes used in Unix, and in the runtime libraries of most C compilers. In the following descriptions of functions, the most typical error codes are listed. By matching the error code, applications can use this information for error recovery. To produce a readable error string, use format_error/1.

EXPORTS

read_file(Filename)

    Returns {ok, Binary}, where Binary is a binary data object that contains the contents of Filename, or {error, Reason} if an error occurs.

    Typical error reasons:

        enoent:
            The file does not exist.

        eacces:
            Missing permission for reading the file, or for searching one of the parent directories.

        eisdir:
            The named file is a directory.

        enotdir:
            A component of the path is not a directory. On some platforms, enoent is returned instead.

        enomem:
            There is not enough memory for the contents of the file.

write_file(Filename, Binary)

    Writes the contents of the binary data object Binary to the file Filename. The file is created if it does not exist already. If it exists, the previous contents are overwritten. Returns ok, or {error, Reason}.

    Typical error reasons are:

        enoent:
            A component of the file name does not exist.

        enotdir:
            A component of the path is not a directory. On some platforms, enoent is returned instead.

        enospc:
            There is a no space left on the device.

        eacces:
            Missing permission for writing the file or searching one of the parent directories.

        eisdir:
            The named file is a directory.

get_cwd()

    Returns {ok, CurDir}, where CurDir (a string) is the current working directory of the file server.

  Note:

In rare circumstances, this function can fail on Unix. It may happen if read permission does not exist for the parent directories of the current directory.

Typical error reasons are:

    eacces:
        Missing read permission for one of the parents of the current directory.

get_cwd(Drive)

    Drive should be of the form "Letter:", for example "c:". Returns {ok, CurDir} or {error, Reason}, where CurDir (a string) is the current working directory of the drive specified.

    This function returns {error, enotsupt} on platforms which have no concept of current drive (Unix, for example).

    Typical error reasons are:

        enotsup:
            The operating system have no concept of drives.

        eacces:
            The drive does not exist.

        einval:
            The format of Drive is invalid.

set_cwd(DirName)

    Sets the current working directory of the file server to DirName. Returns ok if successful.

    Typical error reasons are:

        enoent:
            The directory does not exist.

        enotdir:
            A component of DirName is not a directory. On some platforms, enoent is returned.

        eacces:
            Missing permission for the directory or one of its parents.

delete(Filename)

    Tries to delete the file Filename. Returns ok if successful.

    Typical error reasons are:

        enoent:
            The file does not exist.

        eacces:
            Missing permission for the file or one of its parents.

        eperm:
            The file is a directory and the user is not super-user.

        enotdir:
            A component of the file name is not a directory. On some platforms, enoent is returned instead.

rename(Source, Destination)

    Tries to rename the file Source toDestination. It can be used to move files (and directories) between directories, but it is not sufficient to specify the destination only. The destination file name must also be specified. For example, if bar is a normal file and foo and baz are directories, rename("foo/bar", "baz") returns an error, but rename("foo/bar", "baz/bar") succeeds. Returns ok if it is successful.

  Note:

Renaming of open files is not allowed on most platforms (see eacces below).

Typical error reasons:

    eacces:
        Missing read or write permissions for the parent directories of Source or Destination. On some platforms, this error is given if either Source or Destination is open.

    eexist:
        Destination is not an empty directory.

    einval:
        Source is a root directory, or Destination is a sub-directory of Source.

    eisdir:
        Destination is a directory, but Source is not.

    enoent:
        Source does not exist.

    enotdir:
        Source is a directory, but Destination is not.

    exdev:
        Source and Destination are on different file systems.

make_dir(DirName)

    Tries to create the directory DirName. Missing parent directories are NOT created. Returns ok if successful.

    Typical error reasons are:

        eacces:
            Missing search or write permissions for the parent directories of DirName.

        eexist:
            There is already a file or directory named DirName.

        enoent:
            A component of DirName does not exist.

        enospc:
            There is a no space left on the device.

        enotdir:
            A component of the path is not a directory. On some platforms, enoent is returned instead.

del_dir(DirName)

    Tries to delete the directory DirName. The directory must be empty before it can be deleted. Returns ok if successful.

    Typical error reasons are:

        eacces:
            Missing search or write permissions for the parent directories of DirName.

        eexist:
            The directory is not empty.

        enoent:
            The directory does not exist.

        enotdir:
            A component of the path is not a directory. On some platforms, enoent is returned instead.

        einval:
            Attempt to delete the current directory. On some platforms, eacces is returned instead.

list_dir(DirName)

    Lists all the files in a directory. Returns {ok, FilenameList} if successful. Otherwise, it returns {error, Reason}. FilenameList is a list of the names of all the files in the directory. Each name is a string. The names are not sorted.

    Typical error reasons are:

        eacces:
            Missing search or write permissions for DirName or one of its parent directories.

        enoent:
            The directory does not exist.

read_file_info(Filename)

    Retrieves information about a file. Returns {ok, FileInfo} if successful, otherwise {error, Reason}. FileInfo is a record. Its definition can be found by including file.hrl from the kernel application:
              -include_lib("kernel/include/file.hrl").

    The record contains the following fields.

        size:
            Size of file in bytes.

        type:
            The type of the file which can be device, directory, regular, or other.

        access:
            The current system access to the file, which is one of the atoms read, write, read_write, or none.

        atime:
            The last (local) time the file was read, in the format {{Year, Month, Day}, {Hour, Minute, Second}}.

        mtime:
            The last (local) time the file was written, in the format {{Year, Month, Day}, {Hour, Minute, Second}}.

        ctime:
            The interpreation of this time field depends on the operating system. On Unix, it is the last time the file or or the inode was changed. In Windows, it is the create time. The format is {{Year, Month, Day}, {Hour, Minute, Second}}.

        mode:
            An integer which gives the file permissions as a sum of the following bit values:

                8#00400:
                    read permission: owner

                8#00200:
                    write permission: owner

                8#00100:
                    execute permission: owner

                8#00040:
                    read permission: group

                8#00020:
                    write permission: group

                8#00010:
                    execute permission: group

                8#00004:
                    read permission: other

                8#00002:
                    write permission: other

                8#00001:
                    execute permission: other

                16#800:
                    set user id on execution

                16#400:
                    set group id on execution

            On Unix platforms, other bits than those listed above may be set.

        links:
            Number of links to the file (this will always be 1 for file systems which have no concept of links).

        major_device:
            An integer which identifies the file system where the file is located. In Windows, the number indicates a drive as follows: 0 means A:, 1 means B:, and so on.

        minor_device:
            Only valid for character devices on Unix. In all other cases, this field is zero.

        inode:
            An integer which gives the inode number. On non-Unix file systems, this field will be zero.

        uid:
            An integer which indicates the owner of the file. Will be zero for non-Unix file systems.

        gid:
            An integer which gives the group that the owner of the file belongs to. Will be zero for non-Unix file systems.

    Typical error reasons:

        eacces:
            Missing search permission for one of the parent directories of the file.

        enoent:
            The file does not exist.

        enotdir:
            A component of the path is not a directory. On some platforms, enoent is returned instead.

file_info(Filename)

  Note:

This function is obsolete. Use read_file_info instead.

Retrieves information about a file. Returns {ok, FileInfo} if successful, otherwise {error, Reason}. FileInfo is a tuple with the following fields:
          {Size,Type,Access,AccessTime,ModifyTime,UnUsed1,UnUsed2}

    Size:
        The size of the file in bytes.

    Type:
        The type of file which is device, directory, regular, or other.

    Access:
        The current system access to the file, which is one of the atoms read, write, read_write, or none.

    AccessTime:
        The last time the file was read, shown in the format {Year, Month, Day, Hour, Minute, Second}.

    ModifyTime:
        The last time the file was written, shown in the format {Year, Month, Day, Hour, Minute, Second}.

    UnUsed1, UnUsed2:
        These fields are not used, but reserved for future expansion. They probably contain unused.

Typical error reasons: Same as for read_file_info/1.

write_file_info(Filename, FileInfo)

    Change file information. Returns ok if successful, otherwise {error, Reason}. FileInfo is a record. Its definition can be found by including file.hrl from the kernel application:
              -include_lib("kernel/include/file.hrl").

    The following fields are used from the record if they are given.

        atime:
            The last (local) time the file was read, in the format {{Year, Month, Day}, {Hour, Minute, Second}}.

        mtime:
            The last (local) time the file was written, in the format {{Year, Month, Day}, {Hour, Minute, Second}}.

        ctime:
            On Unix, any value give for this field will be ignored (the "ctime" for the file will be set to the current time). On Windows, this field is the new creation time to set for the file. The format is {{Year, Month, Day}, {Hour, Minute, Second}}.

        mode:
            An integer which gives the file permissions as a sum of the following bit values:

                8#00400:
                    read permission: owner

                8#00200:
                    write permission: owner

                8#00100:
                    execute permission: owner

                8#00040:
                    read permission: group

                8#00020:
                    write permission: group

                8#00010:
                    execute permission: group

                8#00004:
                    read permission: other

                8#00002:
                    write permission: other

                8#00001:
                    execute permission: other

                16#800:
                    set user id on execution

                16#400:
                    set group id on execution

            On Unix platforms, other bits than those listed above may be set.

        uid:
            An integer which indicates the owner of the file. Ignored for non-Unix file systems.

        gid:
            An integer which gives the group that the owner of the file belongs to. Ignored non-Unix file systems.

    Typical error reasons:

        eacces:
            Missing search permission for one of the parent directories of the file.

        enoent:
            The file does not exist.

        enotdir:
            A component of the path is not a directory. On some platforms, enoent is returned instead.

change_owner(Filename, Uid)

    Change owner of a file. See write_file_info/2.

change_owner(Filename, Uid, Gid)

    Change owner and group of a file. See write_file_info/2.

change_group(Filename, Gid)

    Change group of a file. See write_file_info/2.

change_time(Filename, Mtime)

    Change the modification and access times of a file. See write_file_info/2.

change_time(Filename, Mtime, Atime)

    Change the modification and access times of a file. See write_file_info/2.

consult(Filename)

    Opens file Filename and reads all the Erlang terms in it. Returns one of the following:

        {ok, TermList}:
            The file was successfully read.

        {error, Atom}:
            An error occurred when opening the file or reading it. The Atom is a Posix error code. See the description of open/2 for a list of typical error codes.

        {error, {Line, Mod, Term}}:
            An error occurred when interpreting the Erlang terms in the file. Use the format_error/1 function to convert the three-element tuple to an English description of the error.

path_consult(Path, Filename)

    Searches the path Path (a list of file names) until the file Filename is found. It is opened and all the terms in it are read. It returns one of the following:

        {ok, TermList, FullName}:
            The file was successfully read. FullName is the full name of the file which was opened and read.

        {error, enoent}:
            The file could not be found in any of the directories in Path.

        {error, Atom}:
            An error occurred when opening the file or reading it. The Atom is a Posix error code. See the description of open/2 for a list of typical error codes.

        {error, {Line, Mod, Term}}:
            An error occurred when interpreting the Erlang terms in the file. Use the format_error/1 function to convert the three-element tuple to an English description of the error.

eval(Filename)

    Opens the file Filename and evaluates all the expression sequences in it. It returns one of the following:

        ok: The file was read and evaluated. The actual result of the evaluation is not returned; any expression sequence in the file must be there for its side effect.

        {error, Atom}:
            An error occurred when opening the file or reading it. The Atom is a Posix error code. See the description of open/2 for a list of typical error codes.

        {error, {Line, Mod, Term}}:
            An error occurred when interpreting the Erlang terms in the file. Use the format_error/1 function to convert the three-element tuple to an English description of the error.

path_eval(Path, Filename)

    Searches the path Path until the file Filename is found. It is opened and all the expression sequences in it are evaluated. It returns one of the following:

        {ok, FullName}:
            The file was read. FullName is the full name of the file which was opened and evaluated.

        {error, enoent}:
            The file could not be found in any of the directories in Path.

        {error, Atom}:
            An error occurred when opening the file or reading it. The Atom is a Posix error code. See the description of open/2 for a list of typical error codes.

        {error, {Line, Mod, Term}}:
            An error occurred when interpreting the Erlang terms in the file. Use the format_error/1 function to convert the three-element tuple to an English description of the error.

open(Filename, ModeList)

    Opens the file Filename in the mode determined by ModeList. ModeList may contain one or more of the following items:

        read:
            The file, which must exist, is opened for reading.

        write:
            The file is opened for writing. It is created if it does not exist. Otherwise, it is truncated.

        raw:
            The raw option allows faster access to a file, because no Erlang process is needed to handle the file. However, a file opened in this way has the following limitations:

                *   The functions in the io module cannot be used, because they can only talk to an Erlang process. Instead, use the read/2 and write/2 functions.

                *   Only the Erlang process which opened the file can use it.

                *   A remote Erlang file server cannot be used; the computer on which the Erlang node is running must have access to the file system (directly or through NFS).

        binary:
            This option can only be used if the raw option is specified as well. When specified, read operations on the file using the read/2 function will return binaries rather than lists.

    If both read and write are specified, the file is created if it does not exists. It is not truncated if it exists.

    Returns:

        {ok, IoDevice}:
            The file has been opened in the requested mode. IoDevice is a reference to the file.

        {error, Reason}:
            The file could not be opened.

    A file descriptor is the Pid of the process which handles the file. The file process is linked to the process which originally opened the file. If any process to which the file process is linked terminates, the file will be closed by the file process and the process itself will be terminated. The file descriptor returned from this call can be used as an argument to the I/O functions (see io).

  Note:

In previous versions of file, modes were given as on of the atoms read, write, or read_write instead of a list. This is still allowed for reasons of backwards compatibility, but should not be used for new code. Also note that read_write is not allowed in a mode list.

Typical error reasons:

    enoent:
        The file does not exist.

    eacces:
        Missing permission for reading the file or searching one of the parent directories.

    eisdir:
        The named file is a directory.

    enotdir:
        A component of the path is not a directory. On some platforms, enoent is returned instead.

    enospc:
        There is a no space left on the device (if write access was specified).

path_open(Path, Filename, Mode)

    Searches the path Path for the file Filename. If the path of Filename is absolute, Path is ignored. It returns one of the following:

        {ok, IoDevice, FullName}:
            The file was opened in the requested mode. IoDevice is a reference to the file and FullName is the full name of the file which was opened.

        {error, enoent}:
            Filename was not found in the path.

        {error, Reason}:
            There was an error opening Filename.

close(IoDevice)

    Closes the file referenced by IoDevice. It returns ok.

read(IoDevice, Number)

    Reads Number bytes from the file described by IoDevice. This function is the only way to read from a file opened in raw mode (although it works for normally opened files, too). Returns:

        {ok, ListOrBinary}:
            If the file was opened in binary mode, the read bytes are returned in a binary, otherwise in a list. The list or binary will be shorter than the the number of bytes requested if the end of the file is reached.

        eof:
            eof is returned if the Number was greater than zero and end of file was reached before anything at all could be read. .

        {error, Reason}:
            An posix error code will be returned if an error occurred.

            Typical error reasons:

                ebadf:
                    The file is not opened for reading.

write(IoDevice, Bytes)

    Writes Bytes (possibly a deep list of characters, or a binary) to the file described by IoDevice. This function is the only way to write to a file opened in raw mode (although it works for normally opened files, too).

    This function returns ok if successful, and {error, Reason} otherwise.

    Typical error reasons are:

        ebadf:
            The file is not opened for writing.

        enospc:
            There is a no space left on the device.

position(IoDevice, Location)

    Sets the position of the file referenced by IoDevice to Location. Returns {ok, NewPosition} (as absolute offset) if successful, otherwise {error, Reason}. Location is one of the following:

        {bof, Offset}:
            Absolute offset

        {cur, Offset}:
            Offset from the current position

        {eof, Offset}:
            Offset from the end of file

        Integer:
            The same as {bof, Integer}

        bof || cur || eof:
            The same as above with Offset 0.

    Typical error reasons are:

        einval:
            Either the Location was illegal, or it evaluated to a negative offset in the file. Note that if the resulting position is a negative value you will get an error but after the call it is undefined where the file position will be.

pread(IoDevice, Location, Number)

    Combines position/2 and read/2 in one operation, which is more efficient than calling them one at a time. If IoDevice has been opened in raw mode, some restrictions apply: Location is only allowed to be an integer; and the current position of the file is undefined after the operation.

pwrite(IoDevice, Location, Bytes)

    Combines position/2 and write/2 in one operation, which is more efficient than calling them one at a time. If IoDevice has been opened in raw mode, some restrictions apply: Location is only allowed to be an integer; and the current position of the file is undefined after the operation.

truncate(IoDevice)

    Truncates the file referenced by IoDevice at the current position. Returns ok if successful, otherwise {error, Reason}.

sync(IoDevice)

    Makes sure that any buffers kept by the operating system (not by the Erlang system) are written to disk. On some platforms, this function might have no effect .

format_error(ErrorDescriptor)

    Given the error reason returned by any function in this module, it returns a descriptive string of the error in English.

POSIX Error Codes

    eacces:
        permission denied

    eagain:
        resource temporarily unavailable

    ebadf:
        bad file number

    ebusy:
        file busy

    edquot:
        disk quota exceeded

    eexist:
        file already exists

    efault:
        bad address in system call argument

    efbig:
        file too large

    eintr:
        interrupted system call

    einval:
        invalid argument

    eio:
        I/O error

    eisdir:
        illegal operation on a directory

    eloop:
        too many levels of symbolic links

    emfile:
        too many open files

    emlink:
        too many links

    enametoolong:
        file name too long

    enfile:
        file table overflow

    enodev:
        no such device

    enoent:
        no such file or directory

    enomem:
        not enough memory

    enospc:
        no space left on device

    enotblk:
        block device required

    enotdir:
        not a directory

    enotsup:
        operation not supported

    enxio:
        no such device or address

    eperm:
        not owner

    epipe:
        broken pipe

    erofs:
        read-only file system

    espipe:
        invalid seek

    esrch:
        no such process

    estale:
        stale remote file handle

    exdev:
        cross-domain link

Warnings

If an error occurs when accessing an open file with the io module, the process which handles the file will exit. The dead file process might hang if a process tries to access it later. This will be fixed in a future release.

See Also

filename(3)

AUTHORS
Robert Virding - rv@erix.ericsson.se
Claes Wikstrom - klacke@erix.ericsson.se
Bjorn Gustavsson - bjorn@erix.ericsson.se