The module file
provides an interface to the file system.
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 flag
+A
in erl(1).
iodata() = iolist() | binary() iolist() = [char() | binary() | iolist()] io_device() as returned by file:open/2, a process handling IO protocols name() = string() | atom() | DeepList DeepList = [char() | atom() | DeepList] posix() an atom which is named from the Posix error codes used in Unix, and in the runtime libraries of most C compilers time() = {{Year, Month, Day}, {Hour, Minute, Second}} Year = Month = Day = Hour = Minute = Second = int() Must denote a valid date and time
change_group(Filename, Gid) -> ok | {error, Reason}
Types:
Filename = name()
Gid = int()
Reason = posix()
Changes group of a file. See write_file_info/2.
change_owner(Filename, Uid) -> ok | {error, Reason}
Types:
Filename = name()
Uid = int()
Reason = posix()
Changes owner of a file. See write_file_info/2.
change_owner(Filename, Uid, Gid) -> ok | {error, Reason}
Types:
Filename = name()
Uid = int()
Gid = int()
Reason = posix()
Changes owner and group of a file. See write_file_info/2.
change_time(Filename, Mtime) -> ok | {error, Reason}
Types:
Filename = name()
Mtime = time()
Reason = posix()
Changes the modification and access times of a file. See write_file_info/2.
change_time(Filename, Mtime, Atime) -> ok | {error, Reason}
Types:
Filename = name()
Mtime = Atime = time()
Reason = posix()
Changes the modification and last access times of a file. See write_file_info/2.
close(IoDevice) -> ok | {error, Reason}
Types:
IoDevice = io_device()
Reason = posix()
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) -> {ok, Terms} | {error, Reason}
Types:
Filename = name()
Terms = [term()]
Reason = posix() | {Line, Mod, Term}
Line, Mod, Term -- see below
Reads Erlang terms, separated by '.', from Filename
.
Returns one of the following:
{ok, Terms}
{error, Posix}
{error, {Line, Mod, Term}}
format_error/1
to convert
the three-element tuple to an English description of
the error.Example:
f.txt: {person, "kalle", 25}. {person, "pelle", 30}.
1> file:consult("f.txt"). {ok,[{person,"kalle",25},{person,"pelle",30}]}
copy(Source, Destination) ->
copy(Source, Destination, ByteCount) -> {ok, BytesCopied}
| {error, Reason}
Types:
Source = Destination = io_device() | Filename
| {Filename, Modes}
Filename = name()
Modes = [Mode] -- see open/2
ByteCount = int() >= 0 | infinity
BytesCopied = int()
Copies ByteCount
bytes from Source
to
Destination
. Source
and Destination
refer
to either filenames or IO devices from e.g. open/2
.
ByteCount
defaults infinity
, denoting an
infinite number of bytes.
The argument Modes
is a list of possible modes, see
open/2, and defaults to
[].
If both Source
and Destination
refer to
filenames, the files are opened with [read, binary]
and [write, binary]
prepended to their mode lists,
respectively, to optimize the copy.
If Source
refers to a filename, it is opened with
read
mode prepended to the mode list before the copy,
and closed when done.
If Destination
refers to a filename, it is opened
with write
mode prepended to the mode list before
the copy, and closed when done.
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(Dir) -> ok | {error, Reason}
Types:
Dir = name()
Reason = posix()
Tries to delete the directory Dir
. The directory must
be empty before it can be deleted. Returns ok
if
successful.
Typical error reasons are:
eacces
Dir
.eexist
enoent
enotdir
Dir
is not a directory. On some
platforms, enoent
is returned instead.einval
eacces
is returned instead.delete(Filename) -> ok | {error, Reason}
Types:
Filename = name()
Reason = posix()
Tries to delete the file Filename
. Returns ok
if successful.
Typical error reasons are:
enoent
eacces
eperm
enotdir
enoent
is returned instead.eval(Filename) -> ok | {error, Reason}
Types:
Filename = name()
Reason = posix() | {Line, Mod, Term}
Line, Mod, Term -- see below
Reads and evaluates Erlang expressions, separated by '.' (or
',', a sequence of expressions is also an expression), from
Filename
. The actual result of the evaluation is not
returned; any expression sequence in the file must be there
for its side effect. Returns one of the following:
ok
{error, Posix}
open/2
for a list of typical error codes.{error, {Line, Mod, Term}}
format_error/1
to
convert the three-element tuple to an English description
of the error.eval(Filename, Bindings) -> ok | {error, Reason}
Types:
Filename = name()
Bindings -- see erl_eval(3)
Reason = posix() | {Line, Mod, Term}
Line, Mod, Term -- see eval/1
The same as eval/1
but the variable bindings
Bindings
are used in the evaluation. See
erl_eval(3) about
variable bindings.
file_info(Filename) -> {ok, FileInfo} | {error, Reason}
This function is obsolete. Use read_file_info/1
instead.
Types:
Reason = posix() | {Line, Mod, Term}
Line, Mod, Term -- see eval/1
Chars = [char() | Chars]
Given the error reason returned by any function in this module, returns a descriptive string of the error in English.
get_cwd() -> {ok, Dir} | {error, Reason}
Types:
Dir = string()
Reason = posix()
Returns {ok, Dir}
, where Dir
is the current
working directory of the file server.
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
get_cwd(Drive) -> {ok, Dir} | {error, Reason}
Types:
Drive = string() -- see below
Dir = string()
Reason = posix()
Drive
should be of the form "Letter
:
",
for example "c:". Returns {ok, Dir}
or
{error, Reason}
, where Dir
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
eacces
einval
Drive
is invalid.list_dir(Dir) -> {ok, Filenames} | {error, Reason}
Types:
Dir = name()
Filenames = [Filename]
Filename = string()
Reason = posix()
Lists all the files in a directory. Returns
{ok, Filenames}
if successful. Otherwise, it returns
{error, Reason}
. Filenames
is a list of
the names of all the files in the directory. The names are
not sorted.
Typical error reasons are:
eacces
Dir
or
one of its parent directories.enoent
make_dir(Dir) -> ok | {error, Reason}
Types:
Dir = name()
Reason = posix()
Tries to create the directory Dir
. Missing parent
directories are not created. Returns ok
if
successful.
Typical error reasons are:
eacces
Dir
.eexist
Dir
.
enoent
Dir
does not exist.enospc
enotdir
Dir
is not a directory. On some
platforms, enoent
is returned instead.make_link(Existing, New) -> ok | {error, Reason}
Types:
Existing = New = name()
Reason = posix()
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}
is returned.
Typical error reasons:
eacces
Existing
or New
.eexist
New
already exists.enotsup
make_symlink(Name1, Name2) -> ok | {error, Reason}
Types:
Name1 = Name2 = name()
Reason = posix()
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}
is returned.
Typical error reasons:
eacces
Name1
or Name2
.eexist
Name2
already exists.enotsup
open(Filename, Modes) -> {ok, IoDevice} | {error, Reason}
Types:
Filename = name()
Modes = [Mode]
Mode = read | write | append | raw | binary
| {delayed_write, Size, Delay} | delayed_write
| {read_ahead, Size} | read_ahead | compressed
Size = Delay = int()
IoDevice = io_device()
Reason = posix()
Opens the file Filename
in the mode determined by
Modes
, which may contain one or more of the following
items:
read
write
write
is not
combined with read
, the file will be truncated.append
append
will take place at
the end of the file.raw
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:io
module cannot be used,
because they can only talk to an Erlang process.
Instead, use the read/2
and write/2
functions.
binary
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}
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.write/2
calls should be for sizes significantly
less than Size
, and not interspersed by to many
other file operations, for this to happen.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.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
{delayed_write, Size, Delay}
with
reasonable default values for Size
and
Delay
. (Roughly some 64 KBytes, 2 seconds){read_ahead, Size}
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.read/2
calls are for sizes not significantly
less than, or even greater than Size
bytes, no
performance gain can be expected.read_ahead
{read_ahead, Size}
with a reasonable
default value for Size
. (Roughly some 64 KBytes)
compressed
read_file_info/1
will most probably not match the
number of bytes that can be read from a compressed file.
Returns:
{ok, IoDevice}
IoDevice
is a reference to the file.{error, Reason}
IoDevice
is really the pid of the process which
handles the file. This process is linked to the process
which originally opened the file. If any process to which
the IoDevice
is linked terminates, the file will be
closed and the process itself will be terminated.
An IoDevice
returned from this call can be used as an
argument to the IO functions (see
io(3)).
In previous versions of |
Typical error reasons:
enoent
eacces
eisdir
enotdir
enoent
is returned instead.enospc
write
access was specified).path_consult(Path, Filename) -> {ok, Terms, FullName}
| {error, Reason}
Types:
Path = [Dir]
Dir = name()
Filename = name()
Terms = [term()]
FullName = string()
Reason = posix() | {Line, Mod, Term}
Line, Mod, Term -- see below
Searches the path Path
(a list of directory names)
until the file Filename
is found. If Filename
is an absolute filename, Path
is ignored.
Then reads Erlang terms, separated by '.', from the file.
Returns one of the following:
{ok, Terms, FullName}
FullName
is
the full name of the file.{error, enoent}
Path
.{error, Posix}
{error, {Line, Mod, Term}}
format_error/1
to convert
the three-element tuple to an English description of
the error.path_eval(Path, Filename) -> {ok, FullName}
| {error, Reason}
Types:
Path = [Dir]
Dir = name()
Filename = name()
FullName = string()
Reason = posix() | {Line, Mod, Term}
Line, Mod, Term -- see below
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. Then reads
and evaluates Erlang expressions, separated by '.' (or ',', a
sequence of expressions is also an expression), from the file.
The actual result of evaluation is not returned; any
expression sequence in the file must be there for its side
effect. Returns one of the following:
{ok, FullName}
FullName
is
the full name of the file.{error, enoent}
Path
.{error, Posix}
{error, {Line, Mod, Term}}
format_error/1
to
convert the three-element tuple to an English description
of the error.path_open(Path, Filename, Modes) -> {ok, IoDevice, FullName}
| {error, Reason}
Types:
Path = [Dir]
Dir = name()
Filename = name()
Modes = [Mode] -- see open/2
IoDevice = io_device()
FullName = string()
Reason = posix()
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.
Then opens the file in the mode determined by Modes
.
Returns one of the following:
{ok, IoDevice, FullName}
IoDevice
is a reference to the file and
FullName
is the full name of the file.{error, enoent}
Path
.{error, Posix}
path_script(Path, Filename) -> {ok, Value, FullName}
| {error, Reason}
Types:
Path = [Dir]
Dir = name()
Filename = name()
Value = term()
FullName = string()
Reason = posix() | {Line, Mod, Term}
Line, Mod, Term -- see below
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. Then reads
and evalutates Erlang expressions, separated by '.' (or ',', a
sequence of expressions is also an expression), from the file.
Returns one of the following:
{ok, Value, FullName}
FullName
is
the full name of the file and Value
the value of
the last expression.{error, enoent}
Path
.{error, Posix}
{error, {Line, Mod, Term}}
format_error/1
to
convert the three-element tuple to an English description
of the error.path_script(Path, Filename, Bindings) ->
{ok, Value, FullName} | {error, Reason}
Types:
Path = [Dir]
Dir = name()
Filename = name()
Bindings -- see erl_eval(3)
Value = term()
FullName = string()
Reason = posix() | {Line, Mod, Term}
Line, Mod, Term -- see path_script/2
The same as path_script/2
but the variable bindings
Bindings
are used in the evaluation. See
erl_eval(3) about
variable bindings.
pid2name(Pid) -> string() | undefined
Types:
Pid = pid()
If Pid
is an IO device, that is, a pid returned from
open/2
, this function returns the filename, or rather:
{ok, Filename}
Pid
must be a local pid) and the file is not
closed. Filename
is the filename in flat string
format.undefined
This function is intended for debugging only. |
position(IoDevice, Location) -> {ok, NewPosition}
| {error, Reason}
Types:
IoDevice = io_device()
Location = Offset | {bof, Offset} | {cur, Offset}
| {eof, Offset} | bof | cur | eof
Offset = int()
NewPosition = int()
Reason = posix()
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:
Offset
{bof, Offset}
.{bof, Offset}
{cur, Offset}
{eof, Offset}
bof | cur | eof
Offset
0.Typical error reasons are:
einval
Location
was illegal, or it evaluated to a
negative offset in the file. Note that if the resulting
position is a negative value, the result is an error, and
after the call the file position is undefined.pread(IoDevice, LocNums) -> {ok, DataL} | {error, Reason}
Types:
IoDevice = io_device()
LocNums = [{Location, Number}]
Location -- see position/2
Number = int()
DataL = [Data]
Data = [char()] | binary() | eof
Reason = posix()
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 each Data
, the result of the corresponding
pread
, 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) -> {ok, Data}
| {error, Reason}
Types:
IoDevice = io_device()
Location -- see position/2
Number = int()
Data = [char()] | binary() | eof
Reason = posix()
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, LocBytes) -> ok | {error, {N, Reason}}
Types:
IoDevice = io_device()
LocBytes = [{Location, Bytes}]
Location -- see position/2
Bytes = iodata()
N = int()
Reason = posix()
Performs a sequence of pwrite/3
in one operation,
which is more efficient than calling them one at a time.
Returns ok
or {error, {N, Reason}}
, where
N
is the number of successful writes that was done
before the failure.
pwrite(IoDevice, Location, Bytes) -> ok | {error, Reason}
Types:
IoDevice = io_device()
Location -- see position/2
Bytes = iodata()
Reason = posix()
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) -> {ok, Data} | eof | {error, Reason}
Types:
IoDevice = io_device()
Number = int()
Data = [char()] | binary()
Reason = posix()
Reads Number
bytes from the file referenced 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, Data}
eof
Number>0
and end of file was reached
before anything at all could be read.{error, Reason}
Typical error reasons:
ebadf
read_file(Filename) -> {ok, Binary} | {error, Reason}
Types:
Filename = name()
Binary = binary()
Reason = posix()
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
eacces
eisdir
enotdir
enoent
is returned instead.enomem
read_file_info(Filename) -> {ok, FileInfo} | {error, Reason}
Types:
Filename = name()
FileInfo = #file_info{}
Reason = posix()
Retrieves information about a file. Returns
{ok, FileInfo}
if successful, otherwise
{error, Reason}
. FileInfo
is a record
file_info
, defined in the Kernel include file
file.hrl
. Include the following directive in the module
from which the function is called:
-include_lib("kernel/include/file.hrl").
The record file_info
contains the following fields.
size = int()
type = device | directory | regular | other
access = read | write | read_write | none
atime = time()
mtime = time()
ctime = time()
mode = int()
links = int()
major_device = int()
minor_device = int()
inode = int()
inode
number. On non-Unix file systems,
this field will be zero.uid = int()
gid = int()
Typical error reasons:
eacces
enoent
enotdir
enoent
is returned instead.read_link(Name) -> {ok, Filename} | {error, Reason}
Types:
Name = name()
Filename = string()
Reason = posix()
This function returns {ok, Filename}
if Name
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
enotsup
read_link_info(Name) -> {ok, FileInfo} | {error, Reason}
Types:
Name = name()
FileInfo = #file_info{}, see read_file_info/1
Reason = posix()
This function works like read_file_info/1
, except that
if Name
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 Name
is not a symbolic link, this function returns
exactly the same result as read_file_info/1
.
On platforms that do not support symbolic links, this function
is always equvivalent to read_file_info/1
.
rename(Source, Destination) -> ok | {error, Reason}
Types:
Source = Destination = name()
Reason = posix()
Tries to rename the file Source
to Destination
.
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.
Renaming of open files is not allowed on most platforms
(see |
Typical error reasons:
eacces
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.script(Filename) -> {ok, Value} | {error, Reason}
Types:
Filename = name()
Value = term()
Reason = posix() | {Line, Mod, Term}
Line, Mod, Term -- see below
Reads and evaluates Erlang expressions, separated by '.' (or ',', a sequence of expressions is also an expression), from the file. Returns one of the following:
{ok, Value}
Value
is
the value of the last expression.{error, Posix}
{error, {Line, Mod, Term}}
format_error/1
to
convert the three-element tuple to an English description
of the error.script(Filename, Bindings) -> {ok, Value} | {error, Reason}
Types:
Filename = name()
Bindings -- see erl_eval(3)
Value = term()
Reason = posix() | {Line, Mod, Term}
Line, Mod, Term -- see below
The same as script/1
but the variable bindings
Bindings
are used in the evaluation. See
erl_eval(3) about
variable bindings.
Types:
Dir = name()
Sets the current working directory of the file server to
Dir
. Returns ok
if successful.
Typical error reasons are:
enoent
enotdir
Dir
is not a directory. On some
platforms, enoent
is returned.eacces
sync(IoDevice) -> ok | {error, Reason}
Types:
IoDevice = io_device()
Reason = posix()
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
truncate(IoDevice) -> ok | {error, Reason}
Types:
IoDevice = io_device()
Reason = posix()
Truncates the file referenced by IoDevice
at
the current position. Returns ok
if successful,
otherwise {error, Reason}
.
write(IoDevice, Bytes) -> ok | {error, Reason}
Types:
IoDevice = io_device()
Bytes = iodata()
Reason = posix()
Writes Bytes
to the file referenced 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). Returns ok
if successful, and
{error, Reason}
otherwise.
Typical error reasons are:
ebadf
enospc
write_file(Filename, Binary) -> ok | {error, Reason}
Types:
Filename = name()
Binary = binary()
Reason = posix()
Writes the contents of the binary data object Binary
to the file Filename
. The file is created if it does
not exist. If it exists, the previous contents are
overwritten. Returns ok
, or {error, Reason}
.
Typical error reasons are:
enoent
enotdir
enoent
is returned instead.enospc
eacces
eisdir
write_file(Filename, Binary, Modes) -> ok | {error, Reason}
Types:
Filename = name()
Binary = binary()
Modes = [Mode] -- see open/2
Reason = posix()
Same as write_file/2
, but takes a third argument
Modes
, a list of possible modes, see
open/2. The mode flags
binary
and write
are implicit, so they should
not be used.
write_file_info(Filename, FileInfo) -> ok | {error, Reason}
Types:
Filename = name()
FileInfo = #file_info{} -- see also read_file_info/1
Reason = posix()
Change file information. Returns ok
if successful,
otherwise {error, Reason}
. FileInfo
is a record
file_info
, defined in the Kernel include file
file.hrl
. Include the following directive in the module
from which the function is called:
-include_lib("kernel/include/file.hrl").
The following fields are used from the record, if they are given.
atime = time()
mtime = time()
ctime = time()
mode = int()
uid = int()
gid = int()
Typical error reasons:
eacces
enoent
enotdir
enoent
is returned instead.
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
- IO 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
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 are
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 IO
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 are 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 when tested:
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, functionally equivalent, 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]).
Trust only your own benchmarks. If the list length in
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 |
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
.
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.