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.

On operating systems with thread support (Solaris and Windows), it is possible to let file operations be performed in threads of their own, allowing other Erlang processes to continue executing in parallel with the file operations. See the command-line option +A in the manual page for erl.

EXPORTS

change_group(Filename, Gid)

    Change group of a file. See write_file_info/2.

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_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.

close(IoDevice)

    Closes the file referenced by IoDevice. It mostly returns ok, expect for some severe errors such as out of memory.

    Note that if the option delayed_write was used when opening the file, close/1 might return an old write error and not even try to close the file. See open/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.

copy(Source, Destination)

    Copies the contents of Source to Destination. Source and Destination are either filenames or open file references from e.g open/2.

    The same as copy/3 but with infinite byte count.

copy(Source, Destination, ByteCount)

    Copies ByteCount bytes from Source to Destination. Source and Destination are either filenames or open file references from e.g open/2.

    If Source is a tuple {Filename, ModeList} where ModeList is a mode list as for open/2, the source is opened with read mode prepended to the mode list before the copy, and closed when done.

    If Source is a filename, it is interpreted as {Source, []}. The file is thereby read from the beginning.

    If Destination is a tuple {Filename, ModeList} where ModeList is a mode list as for open/2, the destination is opened with write mode prepended to the mode list before the copy, and closed when done.

    If Destination is a filename, it is interpreted as {Destination, []}. This implies that the previous file contents are overwritten.

    If both Source and Destination are filenames or {Filename, ModeList} tuples, the files are opened with [raw, read, binary] and [raw, write, binary] prepended to their mode lists, respectively, to optimize the copy.

    Returns {ok, BytesCopied} where BytesCopied is the number of bytes that actually was copied, which may be less than ByteCount if end of file was encountered on the source. If the operation fails, {error, Reason} is returned.

    Typical error reasons: As for open/2 if a file had to be opened, and as for read/2 and write/2.

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 DirName 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.

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.

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.

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.

format_error(ErrorDescriptor)

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

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, enotsup} 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.

ipread_s32bu_p32bu(IoDevice, Location, MaxSize)

    Specialised indirect read function for Dets. Equivalent to pread/3 of a header from Location followed by another pread/3 of the buffer specified by the header.

  Warning:

This function is not intended to be used by others than Dets. It is therefore not well documented.

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.

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 DirName is not a directory. On some platforms, enoent is returned instead.

make_link(Existing, New)

    Makes a hard link from Existing to New, on platforms that support links (Unix). This function returns ok if the link was successfully created, or {error, Reason}. On platforms that do not support links, {error, enotsup} will be returned.

    Typical error reasons:

        eacces:
            Missing read or write permissions for the parent directories of Existing or New.

        eexist:
            new already exists.

        enotsup:
            Hard links are not supported on this platform.

make_symlink(Name1, Name2)

    This function creates a symbolic link Name2 to the file or directory Name1, on platforms that support symbolic links (most Unix systems). Name1 need not exist. This function returns ok if the link was successfully created, or {error, Reason}. On platforms that do not support symbolic links, {error, enotsup} will be returned.

    Typical error reasons:

        eacces:
            Missing read or write permissions for the parent directories of Existing or New.

        eexist:
            new already exists.

        enotsup:
            Symbolic links are not supported on this platform.

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 (unless combined with read).

        append:
            The file will be opened for writing, and it will be created it does not exist. Every write operation to a file openeded with append will take place at the end of the file.

        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.

        {delayed_write, Size, Delay}:
            If this option is used, the data in subsequent write/2 calls is buffered until there are at least Size bytes buffered, or until the oldest buffered data is Delay milliseconds old. Then all buffered data is written in one operating system call. The buffered data is also flushed before some other file operation than write/2 is executed.

            The purpose of this option is to increase performance by reducing the number of operating system calls, so the write/2 calls should be for sizes significantly less than Size, and not interspersed by to many other file operations, for this to happen.

            When this option is used, the result of write/2 calls may prematurely be reported as successful, and if a write error should actually occur the error is reported as the result of the next file operation, which is not executed.

            E.g when delayed_write is used, after a number of write/2 calls, close/1 might return {error, enospc} because there was not enough space on the disc for previously written data, and close/1 should probably be called again since the file is still open.

        delayed_write:
            The same as {delayed_write, Size, Delay} with reasonable default values for Size and Delay. (Roughly some 64 KBytes, 2 seconds)

        {read_ahead, Size}:
            This option activates read data buffering. If read/2 calls are for significantly less than Size bytes, read operations towards the operating system are still performed for blocks of Size bytes. The extra data is buffered and returned in subsequent read/2 calls, giving a performance gain since the number of operating system calls is reduced.

            If read/2 calls are for sizes not significantly less than, or even greater than Size bytes, no performance gain can be expected.

        read_ahead:
            The same as {read_ahead, Size} with a reasonable default value for Size. (Roughly some 64 KBytes)

    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 file name 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_consult(Path, Filename)

    Searches the path Path (a list of directory names) until the file Filename is found. If Filename is an absolute file name, Path is ignored. The file is opened and all the terms in it are read. The function 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.

path_eval(Path, Filename)

    Searches the path Path (a list of directory names) until the file Filename is found. If Filename is an absolute file name, Path is ignored. The file is opened and all the expression sequences in it are evaluated. The function 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.

path_open(Path, Filename, Mode)

    Searches the path Path (a list of directory names) until the file Filename is found. If Filename is an absolute file name, Path is ignored. The function 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.

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}, ...])

    Performs a sequence of pread/3 in one operation, which is more efficient than calling them one at a time. Returns {ok, [Data, ...]} or {error, Reason}, where Data is either a list or a binary depending on the mode of the file, or eof if the requested position was beyond end of file.

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}, ...])

    Performs a sequence of pwrite/3 in one operation, which is more efficient than calling them one at a time. Returns ok or {error, {NumberWritten, Reason}}, where NumberWritten is the number of successful writes that was done before the failure.

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.

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}:
            A Posix error code will be returned if an error occurred.

            Typical error reasons:

                ebadf:
                    The file is not opened for reading.

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 file name is not a directory. On some platforms, enoent is returned instead.

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

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 interpretation 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 file name is not a directory. On some platforms, enoent is returned instead.

read_link(Linkname)

    This function returns {ok, Filename} if Linkname refers to a symbolic link or {error, Reason} otherwise. On platforms that do not support symbolic links, the return value will be {error, enotsup}.

    Typical error reasons:

        einval:
            Linkname does not refer to a symbolic link.

        enoent:
            The file does not exist.

        enotsup:
            Symbolic links are not supported on this platform.

read_link_info(Filename)

    This function works like read_file_info/1, except that if Filename is a symbolic link, information about the link will be returned in the file_info record and the type field of the record will be set to symlink. If Filename is not a symbolic link, this function returns exactly the same result as read_file_info/1. On platforms that do not support symbolic link, this function is always equvivalent to read_file_info/1.

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. On some platforms, also given when Source and Destination are not of the same type.

    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.

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.

sync(IoDevice)

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

    Typical error reasons are:

        enospc:
            Not enough space left to write the file.

truncate(IoDevice)

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

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.

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 file name 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.

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 file name is not a directory. On some platforms, enoent is returned instead.

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

Performance

Some operating system file operations, for example a sync/1 or close/1 on a huge file, may block their calling thread for seconds. If this befalls the emulator main thread the response time is no longer in the order of milliseconds, depending on the definition of "soft" in soft real-time system.

If the device driver thread pool is active, file operations are done through those threads instead, so the emulator can go on executing erlang processes. Unfortunately, the time for serving a file operation increases due to the extra scheduling required from the operating system.

If the device driver thread pool is disabled or of size 0, large file reads and writes are segmented into several smaller, which enables the emulator so server other processes during the file operation. This gives the same effect as when using the thread pool, but with larger overhead. Other file operations, for example sync/1 or close/1 on a huge file, still is a problem.

For increased performance, raw files are recommended. Raw files, uses the file system of the node's host machine. For normal files (non-raw) the file server is used to find the files, and if the node is running its file server as slave to another node's, and the other node runs on some other host machine, they may have different file systems. This is seldom a problem, but you have now been warned.

A normal file is really a process so it can be used as an I/O device (see io). Therefore when data is written to a normal file, the sending of the data to the file process copies all data that is not binaries. Opening the file in binary mode and writing binaries is therefore recommended. If the file is opened on another node, or if the file server runs as slave to another node's, also binaries are copied.

Caching data to reduce the number of file operations, or rather the number of calls to the file driver, will generally increase performance. The following function writes 4 MBytes in 23 seconds on my machine:

      create_file_slow(Name, N) when integer(N), N >= 0 ->
          {ok, FD} =
              file:open(Name, [raw, write, delayed_write, binary]),
          ok = create_file_slow(FD, 0, N),
          ok = ?FILE_MODULE:close(FD),
          ok.

      create_file_slow(FD, M, M) ->
          ok;
      create_file_slow(FD, M, N) ->
          ok = file:write(FD, <<M:32/unsigned>>),
          create_file_slow(FD, M+1, N).

The following functionallyequivalent function collects 1024 entries into a list of 128 32-byte binaries before each call to file:write/2 and so does the same work in 0.52 seconds, which is 44 times faster.

      create_file(Name, N) when integer(N), N >= 0 ->
          {ok, FD} = file:open(Name, [raw, write, delayed_write, binary]),
          ok = create_file(FD, 0, N),
          ok = ?FILE_MODULE:close(FD),
          ok.

      create_file(FD, M, M) ->
          ok;
      create_file(FD, M, N) when M + 1024 =< N ->
          create_file(FD, M, M + 1024, []),
          create_file(FD, M + 1024, N);
      create_file(FD, M, N) ->
          create_file(FD, M, N, []).

      create_file(FD, M, M, R) ->
          ok = file:write(FD, R);
      create_file(FD, M, N0, R) when M + 8 =< N0 ->
          N1  = N0-1,  N2  = N0-2,  N3  = N0-3,  N4  = N0-4,
          N5  = N0-5,  N6  = N0-6,  N7  = N0-7,  N8  = N0-8,
          create_file(FD, M, N8,
                        [<<N8:32/unsigned,  N7:32/unsigned,
                           N6:32/unsigned,  N5:32/unsigned,
                           N4:32/unsigned,  N3:32/unsigned,
                           N2:32/unsigned,  N1:32/unsigned>> | R]);
      create_file(FD, M, N0, R) ->
          N1 = N0-1,
          create_file(FD, M, N1, [<<N1:32/unsigned>> | R]).

  Note:

Trust only your own benchmarks. If the list length in create_file/2 above is increased, it will run slightly faster, but consume more memory and cause more memory fragmentation. How much this affects your application is something that this simple benchmark can not predict.

If the size of each binary is increased to 64 bytes, it will also run slightly faster, but the code will be twice as clumsy. In the current implementation are binaries larger than 64 bytes stored in memory common to all processes and not copied when sent between processes, while these smaller binaries are stored on the process heap and copied when sent like any other term.

So, with a binary size of 68 bytes create_file/2 runs 30 percent slower then with 64 bytes, and will cause much more memory fragmentation. Note that if the binaries were to be sent between processes (for example a non-raw file) the results would probably be completely different.

A raw file is really a port. When writing data to a port, it is efficient to write a list of binaries. There is no need to flatten a deep list before writing. On Unix hosts, scatter output, which writes a set of buffers in one operation, is used when possible. In this way file:write(FD, [Bin1, Bin2 | Bin3]) will write the contents of the binaries without copying the data at all except for perhaps deep down in the operating system kernel.

For raw files, pwrite/2 and pread/2 are efficiently implemented. The file driver is called only once for the whole operation, and the list iteration is done in the file driver.

The options delayed_write and read_ahead to file:open/2 makes the file driver cache data to reduce the number of operating system calls. The function create_file/2 in the example above takes 60 seconds seconds without the delayed_write option, which is 2.6 times slower.

And, as a really bad example, create_file_slow/2 above without the raw, binary and delayed_write options, that is it calls file:open(Name, [write]), needs 1 min 20 seconds for the job, which is 3.5 times slower than the first example, and 150 times slower than the optimized create_file/2.

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 - support@erlang.ericsson.se
Claes Wikström - support@erlang.ericsson.se
Björn Gustavsson - support@erlang.ericsson.se