.TH file 3erl "kernel 7.2" "Ericsson AB" "Erlang Module Definition" .SH NAME file \- File interface module. .SH DESCRIPTION .LP This module provides an interface to the file system\&. .LP .RS -4 .B Warning: .RE File operations are only guaranteed to appear atomic when going through the same file server\&. A NIF or other OS process may observe intermediate steps on certain operations on some operating systems, eg\&. renaming an existing file on Windows, or \fIwrite_file_info/2\fR\& on any OS at the time of writing\&. .LP Regarding filename encoding, the Erlang VM can operate in two modes\&. The current mode can be queried using function \fInative_name_encoding/0\fR\&\&. It returns \fIlatin1\fR\& or \fIutf8\fR\&\&. .LP In \fIlatin1\fR\& mode, the Erlang VM does not change the encoding of filenames\&. In \fIutf8\fR\& mode, filenames can contain Unicode characters greater than 255 and the VM converts filenames back and forth to the native filename encoding (usually UTF-8, but UTF-16 on Windows)\&. .LP The default mode depends on the operating system\&. Windows, MacOS X and Android enforce consistent filename encoding and therefore the VM uses \fIutf8\fR\& mode\&. .LP On operating systems with transparent naming (for example, all Unix systems except MacOS X), default is \fIutf8\fR\& if the terminal supports UTF-8, otherwise \fIlatin1\fR\&\&. The default can be overridden using \fI+fnl\fR\& (to force \fIlatin1\fR\& mode) or \fI+fnu\fR\& (to force \fIutf8\fR\& mode) when starting \fIerl\fR\&\&. .LP On operating systems with transparent naming, files can be inconsistently named, for example, some files are encoded in UTF-8 while others are encoded in ISO Latin-1\&. The concept of \fIraw filenames\fR\& is introduced to handle file systems with inconsistent naming when running in \fIutf8\fR\& mode\&. .LP A \fIraw filename\fR\& is a filename specified as a binary\&. The Erlang VM does not translate a filename specified as a binary on systems with transparent naming\&. .LP When running in \fIutf8\fR\& mode, functions \fIlist_dir/1\fR\& and \fIread_link/1\fR\& never return raw filenames\&. To return all filenames including raw filenames, use functions \fIlist_dir_all/1\fR\& and \fIread_link_all/1\fR\&\&. .LP See also section Notes About Raw Filenames in the STDLIB User\&'s Guide\&. .LP .RS -4 .B Note: .RE File operations used to accept filenames containing null characters (integer value zero)\&. This caused the name to be truncated and in some cases arguments to primitive operations to be mixed up\&. Filenames containing null characters inside the filename are now \fIrejected\fR\& and will cause primitive file operations fail\&. .SH DATA TYPES .nf \fBdeep_list()\fR\& = [char() | atom() | deep_list()] .br .fi .nf .B fd() .br .fi .RS .LP A file descriptor representing a file opened in \fIraw\fR\& mode\&. .RE .nf \fBfilename()\fR\& = string() .br .fi .RS .LP See also the documentation of the \fIname_all()\fR\& type\&. .RE .nf \fBfilename_all()\fR\& = string() | binary() .br .fi .RS .LP See also the documentation of the \fIname_all()\fR\& type\&. .RE .nf \fBio_device()\fR\& = pid() | fd() .br .fi .RS .LP As returned by \fIopen/2\fR\&; \fIpid()\fR\& is a process handling I/O-protocols\&. .RE .nf \fBname()\fR\& = string() | atom() | deep_list() .br .fi .RS .LP If VM is in Unicode filename mode, \fIstring()\fR\& and \fIchar()\fR\& are allowed to be > 255\&. See also the documentation of the \fIname_all()\fR\& type\&. .RE .nf \fBname_all()\fR\& = .br string() | atom() | deep_list() | (RawFilename :: binary()) .br .fi .RS .LP If VM is in Unicode filename mode, characters are allowed to be > 255\&. \fIRawFilename\fR\& is a filename not subject to Unicode translation, meaning that it can contain characters not conforming to the Unicode encoding expected from the file system (that is, non-UTF-8 characters although the VM is started in Unicode filename mode)\&. Null characters (integer value zero) are \fInot\fR\& allowed in filenames (not even at the end)\&. .RE .nf \fBposix()\fR\& = .br eacces | eagain | ebadf | ebadmsg | ebusy | edeadlk | .br edeadlock | edquot | eexist | efault | efbig | eftype | .br eintr | einval | eio | eisdir | eloop | emfile | emlink | .br emultihop | enametoolong | enfile | enobufs | enodev | .br enolck | enolink | enoent | enomem | enospc | enosr | enostr | .br enosys | enotblk | enotdir | enotsup | enxio | eopnotsupp | .br eoverflow | eperm | epipe | erange | erofs | espipe | esrch | .br estale | etxtbsy | exdev .br .fi .RS .LP An atom that is named from the POSIX error codes used in Unix, and in the runtime libraries of most C compilers\&. .RE .nf \fBdate_time()\fR\& = calendar:datetime() .br .fi .RS .LP Must denote a valid date and time\&. .RE .nf \fBfile_info()\fR\& = .br #file_info{size = integer() >= 0 | undefined, .br type = .br device | directory | other | regular | .br symlink | undefined, .br access = .br read | write | read_write | none | undefined, .br atime = .br file:date_time() | .br integer() >= 0 | .br undefined, .br mtime = .br file:date_time() | .br integer() >= 0 | .br undefined, .br ctime = .br file:date_time() | .br integer() >= 0 | .br undefined, .br mode = integer() >= 0 | undefined, .br links = integer() >= 0 | undefined, .br major_device = integer() >= 0 | undefined, .br minor_device = integer() >= 0 | undefined, .br inode = integer() >= 0 | undefined, .br uid = integer() >= 0 | undefined, .br gid = integer() >= 0 | undefined} .br .fi .nf \fBlocation()\fR\& = .br integer() | .br {bof, Offset :: integer()} | .br {cur, Offset :: integer()} | .br {eof, Offset :: integer()} | .br bof | cur | eof .br .fi .nf \fBmode()\fR\& = .br read | write | append | exclusive | raw | binary | .br {delayed_write, .br Size :: integer() >= 0, .br Delay :: integer() >= 0} | .br delayed_write | .br {read_ahead, Size :: integer() >= 1} | .br read_ahead | compressed | .br {encoding, unicode:encoding()} | .br sync .br .fi .nf \fBfile_info_option()\fR\& = .br {time, local} | {time, universal} | {time, posix} | raw .br .fi .SH EXPORTS .LP .nf .B advise(IoDevice, Offset, Length, Advise) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 IoDevice = io_device() .br Offset = Length = integer() .br Advise = posix_file_advise() .br Reason = posix() | badarg .br .nf \fBposix_file_advise()\fR\& = .br normal | sequential | random | no_reuse | will_need | .br dont_need .fi .br .RE .RE .RS .LP \fIadvise/4\fR\& can be used to announce an intention to access file data in a specific pattern in the future, thus allowing the operating system to perform appropriate optimizations\&. .LP On some platforms, this function might have no effect\&. .RE .LP .nf .B allocate(File, Offset, Length) -> ok | {error, posix()} .br .fi .br .RS .LP Types: .RS 3 File = io_device() .br Offset = Length = integer() >= 0 .br .RE .RE .RS .LP \fIallocate/3\fR\& can be used to preallocate space for a file\&. .LP This function only succeeds in platforms that provide this feature\&. .RE .LP .nf .B change_group(Filename, Gid) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Gid = integer() .br Reason = posix() | badarg .br .RE .RE .RS .LP Changes group of a file\&. See \fIwrite_file_info/2\fR\&\&. .RE .LP .nf .B change_mode(Filename, Mode) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Mode = integer() .br Reason = posix() | badarg .br .RE .RE .RS .LP Changes permissions of a file\&. See \fIwrite_file_info/2\fR\&\&. .RE .LP .nf .B change_owner(Filename, Uid) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Uid = integer() .br Reason = posix() | badarg .br .RE .RE .RS .LP Changes owner of a file\&. See \fIwrite_file_info/2\fR\&\&. .RE .LP .nf .B change_owner(Filename, Uid, Gid) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Uid = Gid = integer() .br Reason = posix() | badarg .br .RE .RE .RS .LP Changes owner and group of a file\&. See \fIwrite_file_info/2\fR\&\&. .RE .LP .nf .B change_time(Filename, Mtime) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Mtime = date_time() .br Reason = posix() | badarg .br .RE .RE .RS .LP Changes the modification and access times of a file\&. See \fIwrite_file_info/2\fR\&\&. .RE .LP .nf .B change_time(Filename, Atime, Mtime) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Atime = Mtime = date_time() .br Reason = posix() | badarg .br .RE .RE .RS .LP Changes the modification and last access times of a file\&. See \fIwrite_file_info/2\fR\&\&. .RE .LP .nf .B close(IoDevice) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 IoDevice = io_device() .br Reason = posix() | badarg | terminated .br .RE .RE .RS .LP Closes the file referenced by \fIIoDevice\fR\&\&. It mostly returns \fIok\fR\&, except for some severe errors such as out of memory\&. .LP Notice that if option \fIdelayed_write\fR\& was used when opening the file, \fIclose/1\fR\& can return an old write error and not even try to close the file\&. See \fIopen/2\fR\&\&. .RE .LP .nf .B consult(Filename) -> {ok, Terms} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Terms = [term()] .br Reason = .br posix() | .br badarg | terminated | system_limit | .br {Line :: integer(), Mod :: module(), Term :: term()} .br .RE .RE .RS .LP Reads Erlang terms, separated by \&'\&.\&', from \fIFilename\fR\&\&. Returns one of the following: .RS 2 .TP 2 .B \fI{ok, Terms}\fR\&: The file was successfully read\&. .TP 2 .B \fI{error, atom()}\fR\&: An error occurred when opening the file or reading it\&. For a list of typical error codes, see \fIopen/2\fR\&\&. .TP 2 .B \fI{error, {Line, Mod, Term}}\fR\&: An error occurred when interpreting the Erlang terms in the file\&. To convert the three-element tuple to an English description of the error, use \fIformat_error/1\fR\&\&. .RE .LP \fIExample:\fR\& .LP .nf f.txt: {person, "kalle", 25}. {person, "pelle", 30}. .fi .LP .nf 1> file:consult("f\&.txt")\&. {ok,[{person,"kalle",25},{person,"pelle",30}]} .fi .LP The encoding of \fIFilename\fR\& can be set by a comment, as described in \fIepp(3erl)\fR\&\&. .RE .LP .nf .B copy(Source, Destination) -> {ok, BytesCopied} | {error, Reason} .br .fi .br .nf .B copy(Source, Destination, ByteCount) -> .B {ok, BytesCopied} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Source = Destination = io_device() | Filename | {Filename, Modes} .br Filename = name_all() .br Modes = [mode()] .br ByteCount = integer() >= 0 | infinity .br BytesCopied = integer() >= 0 .br Reason = posix() | badarg | terminated .br .RE .RE .RS .LP Copies \fIByteCount\fR\& bytes from \fISource\fR\& to \fIDestination\fR\&\&. \fISource\fR\& and \fIDestination\fR\& refer to either filenames or IO devices from, for example, \fIopen/2\fR\&\&. \fIByteCount\fR\& defaults to \fIinfinity\fR\&, denoting an infinite number of bytes\&. .LP Argument \fIModes\fR\& is a list of possible modes, see \fIopen/2\fR\&, and defaults to \fI[]\fR\&\&. .LP If both \fISource\fR\& and \fIDestination\fR\& refer to filenames, the files are opened with \fI[read, binary]\fR\& and \fI[write, binary]\fR\& prepended to their mode lists, respectively, to optimize the copy\&. .LP If \fISource\fR\& refers to a filename, it is opened with \fIread\fR\& mode prepended to the mode list before the copy, and closed when done\&. .LP If \fIDestination\fR\& refers to a filename, it is opened with \fIwrite\fR\& mode prepended to the mode list before the copy, and closed when done\&. .LP Returns \fI{ok, BytesCopied}\fR\&, where \fIBytesCopied\fR\& is the number of bytes that was copied, which can be less than \fIByteCount\fR\& if end of file was encountered on the source\&. If the operation fails, \fI{error, Reason}\fR\& is returned\&. .LP Typical error reasons: as for \fIopen/2\fR\& if a file had to be opened, and as for \fIread/2\fR\& and \fIwrite/2\fR\&\&. .RE .LP .nf .B datasync(IoDevice) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 IoDevice = io_device() .br Reason = posix() | badarg | terminated .br .RE .RE .RS .LP Ensures that any buffers kept by the operating system (not by the Erlang runtime system) are written to disk\&. In many ways it resembles \fIfsync\fR\& but it does not update some of the metadata of the file, such as the access time\&. On some platforms this function has no effect\&. .LP Applications that access databases or log files often write a tiny data fragment (for example, one line in a log file) and then call \fIfsync()\fR\& immediately to ensure that the written data is physically stored on the hard disk\&. Unfortunately, \fIfsync()\fR\& always initiates two write operations: one for the newly written data and another one to update the modification time stored in the \fIinode\fR\&\&. If the modification time is not a part of the transaction concept, \fIfdatasync()\fR\& can be used to avoid unnecessary \fIinode\fR\& disk write operations\&. .LP Available only in some POSIX systems, this call results in a call to \fIfsync()\fR\&, or has no effect in systems not providing the \fIfdatasync()\fR\& syscall\&. .RE .LP .nf .B del_dir(Dir) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Dir = name_all() .br Reason = posix() | badarg .br .RE .RE .RS .LP Tries to delete directory \fIDir\fR\&\&. The directory must be empty before it can be deleted\&. Returns \fIok\fR\& if successful\&. .LP Typical error reasons: .RS 2 .TP 2 .B \fIeacces\fR\&: Missing search or write permissions for the parent directories of \fIDir\fR\&\&. .TP 2 .B \fIeexist\fR\&: The directory is not empty\&. .TP 2 .B \fIenoent\fR\&: The directory does not exist\&. .TP 2 .B \fIenotdir\fR\&: A component of \fIDir\fR\& is not a directory\&. On some platforms, \fIenoent\fR\& is returned instead\&. .TP 2 .B \fIeinval\fR\&: Attempt to delete the current directory\&. On some platforms, \fIeacces\fR\& is returned instead\&. .RE .RE .LP .nf .B del_dir_r(File) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 File = name_all() .br Reason = posix() | badarg .br .RE .RE .RS .LP Deletes file or directory \fIFile\fR\&\&. If \fIFile\fR\& is a directory, its contents is first recursively deleted\&. Returns: .RS 2 .TP 2 .B \fIok\fR\&: The operation completed without errors\&. .TP 2 .B \fI{error, posix()}\fR\&: An error occurred when accessing or deleting \fIFile\fR\&\&. If some file or directory under \fIFile\fR\& could not be deleted, \fIFile\fR\& cannot be deleted as it is non-empty, and \fI{error, eexist}\fR\& is returned\&. .RE .RE .LP .nf .B delete(Filename) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Reason = posix() | badarg .br .RE .RE .RS .LP Tries to delete file \fIFilename\fR\&\&. Returns \fIok\fR\& if successful\&. .LP Typical error reasons: .RS 2 .TP 2 .B \fIenoent\fR\&: The file does not exist\&. .TP 2 .B \fIeacces\fR\&: Missing permission for the file or one of its parents\&. .TP 2 .B \fIeperm\fR\&: The file is a directory and the user is not superuser\&. .TP 2 .B \fIenotdir\fR\&: A component of the filename is not a directory\&. On some platforms, \fIenoent\fR\& is returned instead\&. .TP 2 .B \fIeinval\fR\&: \fIFilename\fR\& has an improper type, such as tuple\&. .RE .LP .RS -4 .B Warning: .RE In a future release, a bad type for argument \fIFilename\fR\& will probably generate an exception\&. .RE .LP .nf .B eval(Filename) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Reason = .br posix() | .br badarg | terminated | system_limit | .br {Line :: integer(), Mod :: module(), Term :: term()} .br .RE .RE .RS .LP Reads and evaluates Erlang expressions, separated by \&'\&.\&' (or \&',\&', a sequence of expressions is also an expression) from \fIFilename\fR\&\&. The 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: .RS 2 .TP 2 .B \fIok\fR\&: The file was read and evaluated\&. .TP 2 .B \fI{error, atom()}\fR\&: An error occurred when opening the file or reading it\&. For a list of typical error codes, see \fIopen/2\fR\&\&. .TP 2 .B \fI{error, {Line, Mod, Term}}\fR\&: An error occurred when interpreting the Erlang expressions in the file\&. To convert the three-element tuple to an English description of the error, use \fIformat_error/1\fR\&\&. .RE .LP The encoding of \fIFilename\fR\& can be set by a comment, as described in \fIepp(3erl)\fR\&\&. .RE .LP .nf .B eval(Filename, Bindings) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Bindings = erl_eval:binding_struct() .br Reason = .br posix() | .br badarg | terminated | system_limit | .br {Line :: integer(), Mod :: module(), Term :: term()} .br .RE .RE .RS .LP The same as \fIeval/1\fR\&, but the variable bindings \fIBindings\fR\& are used in the evaluation\&. For information about the variable bindings, see \fIerl_eval(3erl)\fR\&\&. .RE .LP .nf .B format_error(Reason) -> Chars .br .fi .br .RS .LP Types: .RS 3 Reason = .br posix() | .br badarg | terminated | system_limit | .br {Line :: integer(), Mod :: module(), Term :: term()} .br Chars = string() .br .RE .RE .RS .LP Given the error reason returned by any function in this module, returns a descriptive string of the error in English\&. .RE .LP .nf .B get_cwd() -> {ok, Dir} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Dir = filename() .br Reason = posix() .br .RE .RE .RS .LP Returns \fI{ok, Dir}\fR\&, where \fIDir\fR\& is the current working directory of the file server\&. .LP .RS -4 .B Note: .RE In rare circumstances, this function can fail on Unix\&. It can occur if read permission does not exist for the parent directories of the current directory\&. .LP A typical error reason: .RS 2 .TP 2 .B \fIeacces\fR\&: Missing read permission for one of the parents of the current directory\&. .RE .RE .LP .nf .B get_cwd(Drive) -> {ok, Dir} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Drive = string() .br Dir = filename() .br Reason = posix() | badarg .br .RE .RE .RS .LP Returns \fI{ok, Dir}\fR\& or \fI{error, Reason}\fR\&, where \fIDir\fR\& is the current working directory of the specified drive\&. .LP \fIDrive\fR\& is to be of the form "\fILetter\fR\&\fI:\fR\&", for example, "c:"\&. .LP Returns \fI{error, enotsup}\fR\& on platforms that have no concept of current drive (Unix, for example)\&. .LP Typical error reasons: .RS 2 .TP 2 .B \fIenotsup\fR\&: The operating system has no concept of drives\&. .TP 2 .B \fIeacces\fR\&: The drive does not exist\&. .TP 2 .B \fIeinval\fR\&: The format of \fIDrive\fR\& is invalid\&. .RE .RE .LP .nf .B list_dir(Dir) -> {ok, Filenames} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Dir = name_all() .br Filenames = [filename()] .br Reason = .br posix() | .br badarg | .br {no_translation, Filename :: unicode:latin1_binary()} .br .RE .RE .RS .LP Lists all files in a directory, \fIexcept\fR\& files with raw filenames\&. Returns \fI{ok, Filenames}\fR\& if successful, otherwise \fI{error, Reason}\fR\&\&. \fIFilenames\fR\& is a list of the names of all the files in the directory\&. The names are not sorted\&. .LP Typical error reasons: .RS 2 .TP 2 .B \fIeacces\fR\&: Missing search or write permissions for \fIDir\fR\& or one of its parent directories\&. .TP 2 .B \fIenoent\fR\&: The directory does not exist\&. .TP 2 .B \fI{no_translation, Filename}\fR\&: \fIFilename\fR\& is a \fIbinary()\fR\& with characters coded in ISO Latin-1 and the VM was started with parameter \fI+fnue\fR\&\&. .RE .RE .LP .nf .B list_dir_all(Dir) -> {ok, Filenames} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Dir = name_all() .br Filenames = [filename_all()] .br Reason = posix() | badarg .br .RE .RE .RS .LP Lists all the files in a directory, including files with raw filenames\&. Returns \fI{ok, Filenames}\fR\& if successful, otherwise \fI{error, Reason}\fR\&\&. \fIFilenames\fR\& is a list of the names of all the files in the directory\&. The names are not sorted\&. .LP Typical error reasons: .RS 2 .TP 2 .B \fIeacces\fR\&: Missing search or write permissions for \fIDir\fR\& or one of its parent directories\&. .TP 2 .B \fIenoent\fR\&: The directory does not exist\&. .RE .RE .LP .nf .B make_dir(Dir) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Dir = name_all() .br Reason = posix() | badarg .br .RE .RE .RS .LP Tries to create directory \fIDir\fR\&\&. Missing parent directories are \fInot\fR\& created\&. Returns \fIok\fR\& if successful\&. .LP Typical error reasons: .RS 2 .TP 2 .B \fIeacces\fR\&: Missing search or write permissions for the parent directories of \fIDir\fR\&\&. .TP 2 .B \fIeexist\fR\&: A file or directory named \fIDir\fR\& exists already\&. .TP 2 .B \fIenoent\fR\&: A component of \fIDir\fR\& does not exist\&. .TP 2 .B \fIenospc\fR\&: No space is left on the device\&. .TP 2 .B \fIenotdir\fR\&: A component of \fIDir\fR\& is not a directory\&. On some platforms, \fIenoent\fR\& is returned instead\&. .RE .RE .LP .nf .B make_link(Existing, New) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Existing = New = name_all() .br Reason = posix() | badarg .br .RE .RE .RS .LP Makes a hard link from \fIExisting\fR\& to \fINew\fR\& on platforms supporting links (Unix and Windows)\&. This function returns \fIok\fR\& if the link was successfully created, otherwise \fI{error, Reason}\fR\&\&. On platforms not supporting links, \fI{error,enotsup}\fR\& is returned\&. .LP Typical error reasons: .RS 2 .TP 2 .B \fIeacces\fR\&: Missing read or write permissions for the parent directories of \fIExisting\fR\& or \fINew\fR\&\&. .TP 2 .B \fIeexist\fR\&: \fINew\fR\& already exists\&. .TP 2 .B \fIenotsup\fR\&: Hard links are not supported on this platform\&. .RE .RE .LP .nf .B make_symlink(Existing, New) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Existing = New = name_all() .br Reason = posix() | badarg .br .RE .RE .RS .LP Creates a symbolic link \fINew\fR\& to the file or directory \fIExisting\fR\& on platforms supporting symbolic links (most Unix systems and Windows, beginning with Vista)\&. \fIExisting\fR\& does not need to exist\&. Returns \fIok\fR\& if the link is successfully created, otherwise \fI{error, Reason}\fR\&\&. On platforms not supporting symbolic links, \fI{error, enotsup}\fR\& is returned\&. .LP Typical error reasons: .RS 2 .TP 2 .B \fIeacces\fR\&: Missing read or write permissions for the parent directories of \fIExisting\fR\& or \fINew\fR\&\&. .TP 2 .B \fIeexist\fR\&: \fINew\fR\& already exists\&. .TP 2 .B \fIenotsup\fR\&: Symbolic links are not supported on this platform\&. .TP 2 .B \fIeperm\fR\&: User does not have privileges to create symbolic links (\fISeCreateSymbolicLinkPrivilege\fR\& on Windows)\&. .RE .RE .LP .nf .B native_name_encoding() -> latin1 | utf8 .br .fi .br .RS .LP Returns the filename encoding mode\&. If it is \fIlatin1\fR\&, the system translates no filenames\&. If it is \fIutf8\fR\&, filenames are converted back and forth to the native filename encoding (usually UTF-8, but UTF-16 on Windows)\&. .RE .LP .nf .B open(File, Modes) -> {ok, IoDevice} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 File = Filename | iodata() .br Filename = name_all() .br Modes = [mode() | ram | directory] .br IoDevice = io_device() .br Reason = posix() | badarg | system_limit .br .RE .RE .RS .LP Opens file \fIFile\fR\& in the mode determined by \fIModes\fR\&, which can contain one or more of the following options: .RS 2 .TP 2 .B \fIread\fR\&: The file, which must exist, is opened for reading\&. .TP 2 .B \fIwrite\fR\&: The file is opened for writing\&. It is created if it does not exist\&. If the file exists and \fIwrite\fR\& is not combined with \fIread\fR\&, the file is truncated\&. .TP 2 .B \fIappend\fR\&: The file is opened for writing\&. It is created if it does not exist\&. Every write operation to a file opened with \fIappend\fR\& takes place at the end of the file\&. .TP 2 .B \fIexclusive\fR\&: The file is opened for writing\&. It is created if it does not exist\&. If the file exists, \fI{error, eexist}\fR\& is returned\&. .LP .RS -4 .B Warning: .RE This option does not guarantee exclusiveness on file systems not supporting \fIO_EXCL\fR\& properly, such as NFS\&. Do not depend on this option unless you know that the file system supports it (in general, local file systems are safe)\&. .TP 2 .B \fIraw\fR\&: Allows faster access to a file, as no Erlang process is needed to handle the file\&. However, a file opened in this way has the following limitations: .RS 2 .TP 2 * The functions in the \fIio\fR\& module cannot be used, as they can only talk to an Erlang process\&. Instead, use functions \fIread/2\fR\&, \fIread_line/1\fR\&, and \fIwrite/2\fR\&\&. .LP .TP 2 * Especially if \fIread_line/1\fR\& is to be used on a \fIraw\fR\& file, it is recommended to combine this option with option \fI{read_ahead, Size}\fR\& as line-oriented I/O is inefficient without buffering\&. .LP .TP 2 * Only the Erlang process that opened the file can use it\&. .LP .TP 2 * 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)\&. .LP .RE .TP 2 .B \fIbinary\fR\&: Read operations on the file return binaries rather than lists\&. .TP 2 .B \fI{delayed_write, Size, Delay}\fR\&: Data in subsequent \fIwrite/2\fR\& calls is buffered until at least \fISize\fR\& bytes are buffered, or until the oldest buffered data is \fIDelay\fR\& 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 \fIwrite/2\fR\& is executed\&. .RS 2 .LP The purpose of this option is to increase performance by reducing the number of operating system calls\&. Thus, the \fIwrite/2\fR\& calls must be for sizes significantly less than \fISize\fR\&, and not interspersed by too many other file operations\&. .RE .RS 2 .LP When this option is used, the result of \fIwrite/2\fR\& calls can prematurely be reported as successful, and if a write error occurs, the error is reported as the result of the next file operation, which is not executed\&. .RE .RS 2 .LP For example, when \fIdelayed_write\fR\& is used, after a number of \fIwrite/2\fR\& calls, \fIclose/1\fR\& can return \fI{error, enospc}\fR\&, as there is not enough space on the disc for previously written data\&. \fIclose/1\fR\& must probably be called again, as the file is still open\&. .RE .TP 2 .B \fIdelayed_write\fR\&: The same as \fI{delayed_write, Size, Delay}\fR\& with reasonable default values for \fISize\fR\& and \fIDelay\fR\& (roughly some 64 KB, 2 seconds)\&. .TP 2 .B \fI{read_ahead, Size}\fR\&: Activates read data buffering\&. If \fIread/2\fR\& calls are for significantly less than \fISize\fR\& bytes, read operations to the operating system are still performed for blocks of \fISize\fR\& bytes\&. The extra data is buffered and returned in subsequent \fIread/2\fR\& calls, giving a performance gain as the number of operating system calls is reduced\&. .RS 2 .LP The \fIread_ahead\fR\& buffer is also highly used by function \fIread_line/1\fR\& in \fIraw\fR\& mode, therefore this option is recommended (for performance reasons) when accessing raw files using that function\&. .RE .RS 2 .LP If \fIread/2\fR\& calls are for sizes not significantly less than, or even greater than \fISize\fR\& bytes, no performance gain can be expected\&. .RE .TP 2 .B \fIread_ahead\fR\&: The same as \fI{read_ahead, Size}\fR\& with a reasonable default value for \fISize\fR\& (roughly some 64 KB)\&. .TP 2 .B \fIcompressed\fR\&: Makes it possible to read or write gzip compressed files\&. Option \fIcompressed\fR\& must be combined with \fIread\fR\& or \fIwrite\fR\&, but not both\&. Notice that the file size obtained with \fIread_file_info/1\fR\& does probably not match the number of bytes that can be read from a compressed file\&. .TP 2 .B \fI{encoding, Encoding}\fR\&: Makes the file perform automatic translation of characters to and from a specific (Unicode) encoding\&. Notice that the data supplied to \fIwrite/2\fR\& or returned by \fIread/2\fR\& still is byte-oriented; this option denotes only how data is stored in the disk file\&. .RS 2 .LP Depending on the encoding, different methods of reading and writing data is preferred\&. The default encoding of \fIlatin1\fR\& implies using this module (\fIfile\fR\&) for reading and writing data as the interfaces provided here work with byte-oriented data\&. Using other (Unicode) encodings makes the \fIio(3erl)\fR\& functions \fIget_chars\fR\&, \fIget_line\fR\&, and \fIput_chars\fR\& more suitable, as they can work with the full Unicode range\&. .RE .RS 2 .LP If data is sent to an \fIio_device()\fR\& in a format that cannot be converted to the specified encoding, or if data is read by a function that returns data in a format that cannot cope with the character range of the data, an error occurs and the file is closed\&. .RE .RS 2 .LP Allowed values for \fIEncoding\fR\&: .RE .RS 2 .TP 2 .B \fIlatin1\fR\&: The default encoding\&. Bytes supplied to the file, that is, \fIwrite/2\fR\& are written "as is" on the file\&. Likewise, bytes read from the file, that is, \fIread/2\fR\& are returned "as is"\&. If module \fIio(3erl)\fR\& is used for writing, the file can only cope with Unicode characters up to code point 255 (the ISO Latin-1 range)\&. .TP 2 .B \fIunicode or utf8\fR\&: Characters are translated to and from UTF-8 encoding before they are written to or read from the file\&. A file opened in this way can be readable using function \fIread/2\fR\&, as long as no data stored on the file lies beyond the ISO Latin-1 range (0\&.\&.255), but failure occurs if the data contains Unicode code points beyond that range\&. The file is best read with the functions in the Unicode aware module \fIio(3erl)\fR\&\&. .RS 2 .LP Bytes written to the file by any means are translated to UTF-8 encoding before being stored on the disk file\&. .RE .TP 2 .B \fIutf16 or {utf16,big}\fR\&: Works like \fIunicode\fR\&, but translation is done to and from big endian UTF-16 instead of UTF-8\&. .TP 2 .B \fI{utf16,little}\fR\&: Works like \fIunicode\fR\&, but translation is done to and from little endian UTF-16 instead of UTF-8\&. .TP 2 .B \fIutf32 or {utf32,big}\fR\&: Works like \fIunicode\fR\&, but translation is done to and from big endian UTF-32 instead of UTF-8\&. .TP 2 .B \fI{utf32,little}\fR\&: Works like \fIunicode\fR\&, but translation is done to and from little endian UTF-32 instead of UTF-8\&. .RE .RS 2 .LP The Encoding can be changed for a file "on the fly" by using function \fIio:setopts/2\fR\&\&. So a file can be analyzed in latin1 encoding for, for example, a BOM, positioned beyond the BOM and then be set for the right encoding before further reading\&. For functions identifying BOMs, see module \fIunicode(3erl)\fR\&\&. .RE .RS 2 .LP This option is not allowed on \fIraw\fR\& files\&. .RE .TP 2 .B \fIram\fR\&: \fIFile\fR\& must be \fIiodata()\fR\&\&. Returns an \fIfd()\fR\&, which lets module \fIfile\fR\& operate on the data in-memory as if it is a file\&. .TP 2 .B \fIsync\fR\&: On platforms supporting it, enables the POSIX \fIO_SYNC\fR\& synchronous I/O flag or its platform-dependent equivalent (for example, \fIFILE_FLAG_WRITE_THROUGH\fR\& on Windows) so that writes to the file block until the data is physically written to disk\&. However, be aware that the exact semantics of this flag differ from platform to platform\&. For example, none of Linux or Windows guarantees that all file metadata are also written before the call returns\&. For precise semantics, check the details of your platform documentation\&. On platforms with no support for POSIX \fIO_SYNC\fR\& or equivalent, use of the \fIsync\fR\& flag causes \fIopen\fR\& to return \fI{error, enotsup}\fR\&\&. .TP 2 .B \fIdirectory\fR\&: Allows \fIopen\fR\& to work on directories\&. .RE .LP Returns: .RS 2 .TP 2 .B \fI{ok, IoDevice}\fR\&: The file is opened in the requested mode\&. \fIIoDevice\fR\& is a reference to the file\&. .TP 2 .B \fI{error, Reason}\fR\&: The file cannot be opened\&. .RE .LP \fIIoDevice\fR\& is really the pid of the process that handles the file\&. This process monitors the process that originally opened the file (the owner process)\&. If the owner process terminates, the file is closed and the process itself terminates too\&. An \fIIoDevice\fR\& returned from this call can be used as an argument to the I/O functions (see \fIio(3erl)\fR\&)\&. .LP .RS -4 .B Note: .RE In previous versions of \fIfile\fR\&, modes were specified as one of the atoms \fIread\fR\&, \fIwrite\fR\&, or \fIread_write\fR\& instead of a list\&. This is still allowed for reasons of backwards compatibility, but is not to be used for new code\&. Also note that \fIread_write\fR\& is not allowed in a mode list\&. .LP Typical error reasons: .RS 2 .TP 2 .B \fIenoent\fR\&: The file does not exist\&. .TP 2 .B \fIeacces\fR\&: Missing permission for reading the file or searching one of the parent directories\&. .TP 2 .B \fIeisdir\fR\&: The named file is a directory\&. .TP 2 .B \fIenotdir\fR\&: A component of the filename is not a directory, or the filename itself is not a directory if \fIdirectory\fR\& mode was specified\&. On some platforms, \fIenoent\fR\& is returned instead\&. .TP 2 .B \fIenospc\fR\&: There is no space left on the device (if \fIwrite\fR\& access was specified)\&. .RE .RE .LP .nf .B path_consult(Path, Filename) -> .B {ok, Terms, FullName} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Path = [Dir] .br Dir = Filename = name_all() .br Terms = [term()] .br FullName = filename_all() .br Reason = .br posix() | .br badarg | terminated | system_limit | .br {Line :: integer(), Mod :: module(), Term :: term()} .br .RE .RE .RS .LP Searches the path \fIPath\fR\& (a list of directory names) until the file \fIFilename\fR\& is found\&. If \fIFilename\fR\& is an absolute filename, \fIPath\fR\& is ignored\&. Then reads Erlang terms, separated by \&'\&.\&', from the file\&. .LP Returns one of the following: .RS 2 .TP 2 .B \fI{ok, Terms, FullName}\fR\&: The file is successfully read\&. \fIFullName\fR\& is the full name of the file\&. .TP 2 .B \fI{error, enoent}\fR\&: The file cannot be found in any of the directories in \fIPath\fR\&\&. .TP 2 .B \fI{error, atom()}\fR\&: An error occurred when opening the file or reading it\&. For a list of typical error codes, see \fIopen/2\fR\&\&. .TP 2 .B \fI{error, {Line, Mod, Term}}\fR\&: An error occurred when interpreting the Erlang terms in the file\&. Use \fIformat_error/1\fR\& to convert the three-element tuple to an English description of the error\&. .RE .LP The encoding of \fIFilename\fR\& can be set by a comment as described in \fIepp(3erl)\fR\&\&. .RE .LP .nf .B path_eval(Path, Filename) -> {ok, FullName} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Path = [Dir :: name_all()] .br Filename = name_all() .br FullName = filename_all() .br Reason = .br posix() | .br badarg | terminated | system_limit | .br {Line :: integer(), Mod :: module(), Term :: term()} .br .RE .RE .RS .LP Searches the path \fIPath\fR\& (a list of directory names) until the file \fIFilename\fR\& is found\&. If \fIFilename\fR\& is an absolute filename, \fIPath\fR\& is ignored\&. Then reads and evaluates Erlang expressions, separated by \&'\&.\&' (or \&',\&', a sequence of expressions is also an expression), from the file\&. The result of evaluation is not returned; any expression sequence in the file must be there for its side effect\&. .LP Returns one of the following: .RS 2 .TP 2 .B \fI{ok, FullName}\fR\&: The file is read and evaluated\&. \fIFullName\fR\& is the full name of the file\&. .TP 2 .B \fI{error, enoent}\fR\&: The file cannot be found in any of the directories in \fIPath\fR\&\&. .TP 2 .B \fI{error, atom()}\fR\&: An error occurred when opening the file or reading it\&. For a list of typical error codes, see \fIopen/2\fR\&\&. .TP 2 .B \fI{error, {Line, Mod, Term}}\fR\&: An error occurred when interpreting the Erlang expressions in the file\&. Use \fIformat_error/1\fR\& to convert the three-element tuple to an English description of the error\&. .RE .LP The encoding of \fIFilename\fR\& can be set by a comment as described in \fIepp(3erl)\fR\&\&. .RE .LP .nf .B path_open(Path, Filename, Modes) -> .B {ok, IoDevice, FullName} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Path = [Dir :: name_all()] .br Filename = name_all() .br Modes = [mode() | directory] .br IoDevice = io_device() .br FullName = filename_all() .br Reason = posix() | badarg | system_limit .br .RE .RE .RS .LP Searches the path \fIPath\fR\& (a list of directory names) until the file \fIFilename\fR\& is found\&. If \fIFilename\fR\& is an absolute filename, \fIPath\fR\& is ignored\&. Then opens the file in the mode determined by \fIModes\fR\&\&. .LP Returns one of the following: .RS 2 .TP 2 .B \fI{ok, IoDevice, FullName}\fR\&: The file is opened in the requested mode\&. \fIIoDevice\fR\& is a reference to the file and \fIFullName\fR\& is the full name of the file\&. .TP 2 .B \fI{error, enoent}\fR\&: The file cannot be found in any of the directories in \fIPath\fR\&\&. .TP 2 .B \fI{error, atom()}\fR\&: The file cannot be opened\&. .RE .RE .LP .nf .B path_script(Path, Filename) -> .B {ok, Value, FullName} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Path = [Dir :: name_all()] .br Filename = name_all() .br Value = term() .br FullName = filename_all() .br Reason = .br posix() | .br badarg | terminated | system_limit | .br {Line :: integer(), Mod :: module(), Term :: term()} .br .RE .RE .RS .LP Searches the path \fIPath\fR\& (a list of directory names) until the file \fIFilename\fR\& is found\&. If \fIFilename\fR\& is an absolute filename, \fIPath\fR\& is ignored\&. Then reads and evaluates Erlang expressions, separated by \&'\&.\&' (or \&',\&', a sequence of expressions is also an expression), from the file\&. .LP Returns one of the following: .RS 2 .TP 2 .B \fI{ok, Value, FullName}\fR\&: The file is read and evaluated\&. \fIFullName\fR\& is the full name of the file and \fIValue\fR\& the value of the last expression\&. .TP 2 .B \fI{error, enoent}\fR\&: The file cannot be found in any of the directories in \fIPath\fR\&\&. .TP 2 .B \fI{error, atom()}\fR\&: An error occurred when opening the file or reading it\&. For a list of typical error codes, see \fIopen/2\fR\&\&. .TP 2 .B \fI{error, {Line, Mod, Term}}\fR\&: An error occurred when interpreting the Erlang expressions in the file\&. Use \fIformat_error/1\fR\& to convert the three-element tuple to an English description of the error\&. .RE .LP The encoding of \fIFilename\fR\& can be set by a comment as described in \fIepp(3erl)\fR\&\&. .RE .LP .nf .B path_script(Path, Filename, Bindings) -> .B {ok, Value, FullName} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Path = [Dir :: name_all()] .br Filename = name_all() .br Bindings = erl_eval:binding_struct() .br Value = term() .br FullName = filename_all() .br Reason = .br posix() | .br badarg | terminated | system_limit | .br {Line :: integer(), Mod :: module(), Term :: term()} .br .RE .RE .RS .LP The same as \fIpath_script/2\fR\& but the variable bindings \fIBindings\fR\& are used in the evaluation\&. See \fIerl_eval(3erl)\fR\& about variable bindings\&. .RE .LP .nf .B pid2name(Pid) -> {ok, Filename} | undefined .br .fi .br .RS .LP Types: .RS 3 Filename = filename_all() .br Pid = pid() .br .RE .RE .RS .LP If \fIPid\fR\& is an I/O device, that is, a pid returned from \fIopen/2\fR\&, this function returns the filename, or rather: .RS 2 .TP 2 .B \fI{ok, Filename}\fR\&: If the file server of this node is not a slave, the file was opened by the file server of this node (this implies that \fIPid\fR\& must be a local pid) and the file is not closed\&. \fIFilename\fR\& is the filename in flat string format\&. .TP 2 .B \fIundefined\fR\&: In all other cases\&. .RE .LP .RS -4 .B Warning: .RE This function is intended for debugging only\&. .RE .LP .nf .B position(IoDevice, Location) -> .B {ok, NewPosition} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 IoDevice = io_device() .br Location = location() .br NewPosition = integer() .br Reason = posix() | badarg | terminated .br .RE .RE .RS .LP Sets the position of the file referenced by \fIIoDevice\fR\& to \fILocation\fR\&\&. Returns \fI{ok, NewPosition}\fR\& (as absolute offset) if successful, otherwise \fI{error, Reason}\fR\&\&. \fILocation\fR\& is one of the following: .RS 2 .TP 2 .B \fIOffset\fR\&: The same as \fI{bof, Offset}\fR\&\&. .TP 2 .B \fI{bof, Offset}\fR\&: Absolute offset\&. .TP 2 .B \fI{cur, Offset}\fR\&: Offset from the current position\&. .TP 2 .B \fI{eof, Offset}\fR\&: Offset from the end of file\&. .TP 2 .B \fIbof | cur | eof\fR\&: The same as above with \fIOffset\fR\& 0\&. .RE .LP Notice that offsets are counted in bytes, not in characters\&. If the file is opened using some other \fIencoding\fR\& than \fIlatin1\fR\&, one byte does not correspond to one character\&. Positioning in such a file can only be done to known character boundaries\&. That is, to a position earlier retrieved by getting a current position, to the beginning/end of the file or to some other position \fIknown\fR\& to be on a correct character boundary by some other means (typically beyond a byte order mark in the file, which has a known byte-size)\&. .LP A typical error reason is: .RS 2 .TP 2 .B \fIeinval\fR\&: Either \fILocation\fR\& is illegal, or it is evaluated to a negative offset in the file\&. Notice that if the resulting position is a negative value, the result is an error, and after the call the file position is undefined\&. .RE .RE .LP .nf .B pread(IoDevice, LocNums) -> {ok, DataL} | eof | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 IoDevice = io_device() .br LocNums = .br [{Location :: location(), Number :: integer() >= 0}] .br DataL = [Data] .br Data = string() | binary() | eof .br Reason = posix() | badarg | terminated .br .RE .RE .RS .LP Performs a sequence of \fIpread/3\fR\& in one operation, which is more efficient than calling them one at a time\&. Returns \fI{ok, [Data, \&.\&.\&.]}\fR\& or \fI{error, Reason}\fR\&, where each \fIData\fR\&, the result of the corresponding \fIpread\fR\&, is either a list or a binary depending on the mode of the file, or \fIeof\fR\& if the requested position is beyond end of file\&. .LP As the position is specified as a byte-offset, take special caution when working with files where \fIencoding\fR\& is set to something else than \fIlatin1\fR\&, as not every byte position is a valid character boundary on such a file\&. .RE .LP .nf .B pread(IoDevice, Location, Number) -> .B {ok, Data} | eof | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 IoDevice = io_device() .br Location = location() .br Number = integer() >= 0 .br Data = string() | binary() .br Reason = posix() | badarg | terminated .br .RE .RE .RS .LP Combines \fIposition/2\fR\& and \fIread/2\fR\& in one operation, which is more efficient than calling them one at a time\&. .LP \fILocation\fR\& is only allowed to be an integer for \fIraw\fR\& and \fIram\fR\& modes\&. .LP The current position of the file after the operation is undefined for \fIraw\fR\& mode and unchanged for \fIram\fR\& mode\&. .LP As the position is specified as a byte-offset, take special caution when working with files where \fIencoding\fR\& is set to something else than \fIlatin1\fR\&, as not every byte position is a valid character boundary on such a file\&. .RE .LP .nf .B pwrite(IoDevice, LocBytes) -> ok | {error, {N, Reason}} .br .fi .br .RS .LP Types: .RS 3 IoDevice = io_device() .br LocBytes = [{Location :: location(), Bytes :: iodata()}] .br N = integer() >= 0 .br Reason = posix() | badarg | terminated .br .RE .RE .RS .LP Performs a sequence of \fIpwrite/3\fR\& in one operation, which is more efficient than calling them one at a time\&. Returns \fIok\fR\& or \fI{error, {N, Reason}}\fR\&, where \fIN\fR\& is the number of successful writes done before the failure\&. .LP When positioning in a file with other \fIencoding\fR\& than \fIlatin1\fR\&, caution must be taken to set the position on a correct character boundary\&. For details, see \fIposition/2\fR\&\&. .RE .LP .nf .B pwrite(IoDevice, Location, Bytes) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 IoDevice = io_device() .br Location = location() .br Bytes = iodata() .br Reason = posix() | badarg | terminated .br .RE .RE .RS .LP Combines \fIposition/2\fR\& and \fIwrite/2\fR\& in one operation, which is more efficient than calling them one at a time\&. .LP \fILocation\fR\& is only allowed to be an integer for \fIraw\fR\& and \fIram\fR\& modes\&. .LP The current position of the file after the operation is undefined for \fIraw\fR\& mode and unchanged for \fIram\fR\& mode\&. .LP When positioning in a file with other \fIencoding\fR\& than \fIlatin1\fR\&, caution must be taken to set the position on a correct character boundary\&. For details, see \fIposition/2\fR\&\&. .RE .LP .nf .B read(IoDevice, Number) -> {ok, Data} | eof | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 IoDevice = io_device() | atom() .br Number = integer() >= 0 .br Data = string() | binary() .br Reason = .br posix() | .br badarg | terminated | .br {no_translation, unicode, latin1} .br .RE .RE .RS .LP Reads \fINumber\fR\& bytes/characters from the file referenced by \fIIoDevice\fR\&\&. The functions \fIread/2\fR\&, \fIpread/3\fR\&, and \fIread_line/1\fR\& are the only ways to read from a file opened in \fIraw\fR\& mode (although they work for normally opened files, too)\&. .LP For files where \fIencoding\fR\& is set to something else than \fIlatin1\fR\&, one character can be represented by more than one byte on the file\&. The parameter \fINumber\fR\& always denotes the number of \fIcharacters\fR\& read from the file, while the position in the file can be moved much more than this number when reading a Unicode file\&. .LP Also, if \fIencoding\fR\& is set to something else than \fIlatin1\fR\&, the \fIread/3\fR\& call fails if the data contains characters larger than 255, which is why module \fIio(3erl)\fR\& is to be preferred when reading such a file\&. .LP The function returns: .RS 2 .TP 2 .B \fI{ok, Data}\fR\&: If the file was opened in binary mode, the read bytes are returned in a binary, otherwise in a list\&. The list or binary is shorter than the number of bytes requested if end of file was reached\&. .TP 2 .B \fIeof\fR\&: Returned if \fINumber>0\fR\& and end of file was reached before anything at all could be read\&. .TP 2 .B \fI{error, Reason}\fR\&: An error occurred\&. .RE .LP Typical error reasons: .RS 2 .TP 2 .B \fIebadf\fR\&: The file is not opened for reading\&. .TP 2 .B \fI{no_translation, unicode, latin1}\fR\&: The file is opened with another \fIencoding\fR\& than \fIlatin1\fR\& and the data in the file cannot be translated to the byte-oriented data that this function returns\&. .RE .RE .LP .nf .B read_file(Filename) -> {ok, Binary} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Binary = binary() .br Reason = posix() | badarg | terminated | system_limit .br .RE .RE .RS .LP Returns \fI{ok, Binary}\fR\&, where \fIBinary\fR\& is a binary data object that contains the contents of \fIFilename\fR\&, or \fI{error, Reason}\fR\& if an error occurs\&. .LP Typical error reasons: .RS 2 .TP 2 .B \fIenoent\fR\&: The file does not exist\&. .TP 2 .B \fIeacces\fR\&: Missing permission for reading the file, or for searching one of the parent directories\&. .TP 2 .B \fIeisdir\fR\&: The named file is a directory\&. .TP 2 .B \fIenotdir\fR\&: A component of the filename is not a directory\&. On some platforms, \fIenoent\fR\& is returned instead\&. .TP 2 .B \fIenomem\fR\&: There is not enough memory for the contents of the file\&. .RE .RE .LP .nf .B read_file_info(File) -> {ok, FileInfo} | {error, Reason} .br .fi .br .nf .B read_file_info(File, Opts) -> {ok, FileInfo} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 File = name_all() | io_device() .br Opts = [file_info_option()] .br FileInfo = file_info() .br Reason = posix() | badarg .br .RE .RE .RS .LP Retrieves information about a file\&. Returns \fI{ok, FileInfo}\fR\& if successful, otherwise \fI{error, Reason}\fR\&\&. \fIFileInfo\fR\& is a record \fIfile_info\fR\&, defined in the Kernel include file \fIfile\&.hrl\fR\&\&. Include the following directive in the module from which the function is called: .LP .nf -include_lib("kernel/include/file.hrl"). .fi .LP The time type returned in \fIatime\fR\&, \fImtime\fR\&, and \fIctime\fR\& is dependent on the time type set in \fIOpts :: {time, Type}\fR\& as follows: .RS 2 .TP 2 .B \fIlocal\fR\&: Returns local time\&. .TP 2 .B \fIuniversal\fR\&: Returns universal time\&. .TP 2 .B \fIposix\fR\&: Returns seconds since or before Unix time epoch, which is 1970-01-01 00:00 UTC\&. .RE .LP Default is \fI{time, local}\fR\&\&. .LP If the option \fIraw\fR\& is set, the file server is not called and only information about local files is returned\&. Note that this will break this module\&'s atomicity guarantees as it can race with a concurrent call to \fIwrite_file_info/1,2\fR\&\&. .LP This option has no effect when the function is given an I/O device instead of a file name\&. Use \fIopen/2\fR\& with the \fIraw\fR\& mode to obtain a file descriptor first\&. .LP .RS -4 .B Note: .RE As file times are stored in POSIX time on most OS, it is faster to query file information with option \fIposix\fR\&\&. .LP The record \fIfile_info\fR\& contains the following fields: .RS 2 .TP 2 .B \fIsize = integer() >= 0\fR\&: Size of file in bytes\&. .TP 2 .B \fItype = device | directory | other | regular | symlink\fR\&: The type of the file\&. .TP 2 .B \fIaccess = read | write | read_write | none\fR\&: The current system access to the file\&. .TP 2 .B \fIatime = \fR\&\fIdate_time()\fR\&\fI | integer() >= 0\fR\&: The last time the file was read\&. .TP 2 .B \fImtime = \fR\&\fIdate_time()\fR\&\fI | integer() >= 0\fR\&: The last time the file was written\&. .TP 2 .B \fIctime = \fR\&\fIdate_time()\fR\&\fI | integer() >=0\fR\&: The interpretation of this time field depends on the operating system\&. On Unix, it is the last time the file or the \fIinode\fR\& was changed\&. In Windows, it is the create time\&. .TP 2 .B \fImode = integer() >= 0\fR\&: The file permissions as the sum of the following bit values: .RS 2 .TP 2 .B \fI8#00400\fR\&: read permission: owner .TP 2 .B \fI8#00200\fR\&: write permission: owner .TP 2 .B \fI8#00100\fR\&: execute permission: owner .TP 2 .B \fI8#00040\fR\&: read permission: group .TP 2 .B \fI8#00020\fR\&: write permission: group .TP 2 .B \fI8#00010\fR\&: execute permission: group .TP 2 .B \fI8#00004\fR\&: read permission: other .TP 2 .B \fI8#00002\fR\&: write permission: other .TP 2 .B \fI8#00001\fR\&: execute permission: other .TP 2 .B \fI16#800\fR\&: set user id on execution .TP 2 .B \fI16#400\fR\&: set group id on execution .RE .RS 2 .LP On Unix platforms, other bits than those listed above may be set\&. .RE .TP 2 .B \fIlinks = integer() >= 0\fR\&: Number of links to the file (this is always 1 for file systems that have no concept of links)\&. .TP 2 .B \fImajor_device = integer() >= 0\fR\&: 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\&. .TP 2 .B \fIminor_device = integer() >= 0\fR\&: Only valid for character devices on Unix\&. In all other cases, this field is zero\&. .TP 2 .B \fIinode = integer() >= 0\fR\&: Gives the \fIinode\fR\& number\&. On non-Unix file systems, this field is zero\&. .TP 2 .B \fIuid = integer() >= 0\fR\&: Indicates the owner of the file\&. On non-Unix file systems, this field is zero\&. .TP 2 .B \fIgid = integer() >= 0\fR\&: Gives the group that the owner of the file belongs to\&. On non-Unix file systems, this field is zero\&. .RE .LP Typical error reasons: .RS 2 .TP 2 .B \fIeacces\fR\&: Missing search permission for one of the parent directories of the file\&. .TP 2 .B \fIenoent\fR\&: The file does not exist\&. .TP 2 .B \fIenotdir\fR\&: A component of the filename is not a directory\&. On some platforms, \fIenoent\fR\& is returned instead\&. .RE .RE .LP .nf .B read_line(IoDevice) -> {ok, Data} | eof | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 IoDevice = io_device() | atom() .br Data = string() | binary() .br Reason = .br posix() | .br badarg | terminated | .br {no_translation, unicode, latin1} .br .RE .RE .RS .LP Reads a line of bytes/characters from the file referenced by \fIIoDevice\fR\&\&. Lines are defined to be delimited by the linefeed (LF, \fI\\n\fR\&) character, but any carriage return (CR, \fI\\r\fR\&) followed by a newline is also treated as a single LF character (the carriage return is silently ignored)\&. The line is returned \fIincluding\fR\& the LF, but excluding any CR immediately followed by an LF\&. This behaviour is consistent with the behaviour of \fIio:get_line/2\fR\&\&. If end of file is reached without any LF ending the last line, a line with no trailing LF is returned\&. .LP The function can be used on files opened in \fIraw\fR\& mode\&. However, it is inefficient to use it on \fIraw\fR\& files if the file is not opened with option \fI{read_ahead, Size}\fR\& specified\&. Thus, combining \fIraw\fR\& and \fI{read_ahead, Size}\fR\& is highly recommended when opening a text file for raw line-oriented reading\&. .LP If \fIencoding\fR\& is set to something else than \fIlatin1\fR\&, the \fIread_line/1\fR\& call fails if the data contains characters larger than 255, why module \fIio(3erl)\fR\& is to be preferred when reading such a file\&. .LP The function returns: .RS 2 .TP 2 .B \fI{ok, Data}\fR\&: One line from the file is returned, including the trailing LF, but with CRLF sequences replaced by a single LF (see above)\&. .RS 2 .LP If the file is opened in binary mode, the read bytes are returned in a binary, otherwise in a list\&. .RE .TP 2 .B \fIeof\fR\&: Returned if end of file was reached before anything at all could be read\&. .TP 2 .B \fI{error, Reason}\fR\&: An error occurred\&. .RE .LP Typical error reasons: .RS 2 .TP 2 .B \fIebadf\fR\&: The file is not opened for reading\&. .TP 2 .B \fI{no_translation, unicode, latin1}\fR\&: The file is opened with another \fIencoding\fR\& than \fIlatin1\fR\& and the data on the file cannot be translated to the byte-oriented data that this function returns\&. .RE .RE .LP .nf .B read_link(Name) -> {ok, Filename} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Name = name_all() .br Filename = filename() .br Reason = posix() | badarg .br .RE .RE .RS .LP Returns \fI{ok, Filename}\fR\& if \fIName\fR\& refers to a symbolic link that is not a raw filename, or \fI{error, Reason}\fR\& otherwise\&. On platforms that do not support symbolic links, the return value is \fI{error,enotsup}\fR\&\&. .LP Typical error reasons: .RS 2 .TP 2 .B \fIeinval\fR\&: \fIName\fR\& does not refer to a symbolic link or the name of the file that it refers to does not conform to the expected encoding\&. .TP 2 .B \fIenoent\fR\&: The file does not exist\&. .TP 2 .B \fIenotsup\fR\&: Symbolic links are not supported on this platform\&. .RE .RE .LP .nf .B read_link_all(Name) -> {ok, Filename} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Name = name_all() .br Filename = filename_all() .br Reason = posix() | badarg .br .RE .RE .RS .LP Returns \fI{ok, Filename}\fR\& if \fIName\fR\& refers to a symbolic link or \fI{error, Reason}\fR\& otherwise\&. On platforms that do not support symbolic links, the return value is \fI{error,enotsup}\fR\&\&. .LP Notice that \fIFilename\fR\& can be either a list or a binary\&. .LP Typical error reasons: .RS 2 .TP 2 .B \fIeinval\fR\&: \fIName\fR\& does not refer to a symbolic link\&. .TP 2 .B \fIenoent\fR\&: The file does not exist\&. .TP 2 .B \fIenotsup\fR\&: Symbolic links are not supported on this platform\&. .RE .RE .LP .nf .B read_link_info(Name) -> {ok, FileInfo} | {error, Reason} .br .fi .br .nf .B read_link_info(Name, Opts) -> {ok, FileInfo} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Name = name_all() .br Opts = [file_info_option()] .br FileInfo = file_info() .br Reason = posix() | badarg .br .RE .RE .RS .LP Works like \fIread_file_info/1,2\fR\& except that if \fIName\fR\& is a symbolic link, information about the link is returned in the \fIfile_info\fR\& record and the \fItype\fR\& field of the record is set to \fIsymlink\fR\&\&. .LP If the option \fIraw\fR\& is set, the file server is not called and only information about local files is returned\&. Note that this will break this module\&'s atomicity guarantees as it can race with a concurrent call to \fIwrite_file_info/1,2\fR\& .LP If \fIName\fR\& is not a symbolic link, this function returns the same result as \fIread_file_info/1\fR\&\&. On platforms that do not support symbolic links, this function is always equivalent to \fIread_file_info/1\fR\&\&. .RE .LP .nf .B rename(Source, Destination) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Source = Destination = name_all() .br Reason = posix() | badarg .br .RE .RE .RS .LP Tries to rename the file \fISource\fR\& to \fIDestination\fR\&\&. It can be used to move files (and directories) between directories, but it is not sufficient to specify the destination only\&. The destination filename must also be specified\&. For example, if \fIbar\fR\& is a normal file and \fIfoo\fR\& and \fIbaz\fR\& are directories, \fIrename("foo/bar", "baz")\fR\& returns an error, but \fIrename("foo/bar", "baz/bar")\fR\& succeeds\&. Returns \fIok\fR\& if it is successful\&. .LP .RS -4 .B Note: .RE Renaming of open files is not allowed on most platforms (see \fIeacces\fR\& below)\&. .LP Typical error reasons: .RS 2 .TP 2 .B \fIeacces\fR\&: Missing read or write permissions for the parent directories of \fISource\fR\& or \fIDestination\fR\&\&. On some platforms, this error is given if either \fISource\fR\& or \fIDestination\fR\& is open\&. .TP 2 .B \fIeexist\fR\&: \fIDestination\fR\& is not an empty directory\&. On some platforms, also given when \fISource\fR\& and \fIDestination\fR\& are not of the same type\&. .TP 2 .B \fIeinval\fR\&: \fISource\fR\& is a root directory, or \fIDestination\fR\& is a subdirectory of \fISource\fR\&\&. .TP 2 .B \fIeisdir\fR\&: \fIDestination\fR\& is a directory, but \fISource\fR\& is not\&. .TP 2 .B \fIenoent\fR\&: \fISource\fR\& does not exist\&. .TP 2 .B \fIenotdir\fR\&: \fISource\fR\& is a directory, but \fIDestination\fR\& is not\&. .TP 2 .B \fIexdev\fR\&: \fISource\fR\& and \fIDestination\fR\& are on different file systems\&. .RE .RE .LP .nf .B script(Filename) -> {ok, Value} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Value = term() .br Reason = .br posix() | .br badarg | terminated | system_limit | .br {Line :: integer(), Mod :: module(), Term :: term()} .br .RE .RE .RS .LP Reads and evaluates Erlang expressions, separated by \&'\&.\&' (or \&',\&', a sequence of expressions is also an expression), from the file\&. .LP Returns one of the following: .RS 2 .TP 2 .B \fI{ok, Value}\fR\&: The file is read and evaluated\&. \fIValue\fR\& is the value of the last expression\&. .TP 2 .B \fI{error, atom()}\fR\&: An error occurred when opening the file or reading it\&. For a list of typical error codes, see \fIopen/2\fR\&\&. .TP 2 .B \fI{error, {Line, Mod, Term}}\fR\&: An error occurred when interpreting the Erlang expressions in the file\&. Use \fIformat_error/1\fR\& to convert the three-element tuple to an English description of the error\&. .RE .LP The encoding of \fIFilename\fR\& can be set by a comment as described in \fIepp(3erl)\fR\&\&. .RE .LP .nf .B script(Filename, Bindings) -> {ok, Value} | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Bindings = erl_eval:binding_struct() .br Value = term() .br Reason = .br posix() | .br badarg | terminated | system_limit | .br {Line :: integer(), Mod :: module(), Term :: term()} .br .RE .RE .RS .LP The same as \fIscript/1\fR\& but the variable bindings \fIBindings\fR\& are used in the evaluation\&. See \fIerl_eval(3erl)\fR\& about variable bindings\&. .RE .LP .nf .B sendfile(Filename, Socket) -> .B {ok, integer() >= 0} | .B {error, inet:posix() | closed | badarg | not_owner} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Socket = inet:socket() .br .RE .RE .RS .LP Sends the file \fIFilename\fR\& to \fISocket\fR\&\&. Returns \fI{ok, BytesSent}\fR\& if successful, otherwise \fI{error, Reason}\fR\&\&. .RE .LP .nf .B sendfile(RawFile, Socket, Offset, Bytes, Opts) -> .B {ok, integer() >= 0} | .B {error, inet:posix() | closed | badarg | not_owner} .br .fi .br .RS .LP Types: .RS 3 RawFile = fd() .br Socket = inet:socket() .br Offset = Bytes = integer() >= 0 .br Opts = [sendfile_option()] .br .nf \fBsendfile_option()\fR\& = .br {chunk_size, integer() >= 0} | {use_threads, boolean()} .fi .br .RE .RE .RS .LP Sends \fIBytes\fR\& from the file referenced by \fIRawFile\fR\& beginning at \fIOffset\fR\& to \fISocket\fR\&\&. Returns \fI{ok, BytesSent}\fR\& if successful, otherwise \fI{error, Reason}\fR\&\&. If \fIBytes\fR\& is set to \fI0\fR\& all data after the specified \fIOffset\fR\& is sent\&. .LP The file used must be opened using the \fIraw\fR\& flag, and the process calling \fIsendfile\fR\& must be the controlling process of the socket\&. See \fIgen_tcp:controlling_process/2\fR\&\&. .LP If the OS used does not support non-blocking \fIsendfile\fR\&, an Erlang fallback using \fIread/2\fR\& and \fIgen_tcp:send/2\fR\& is used\&. .LP The option list can contain the following options: .RS 2 .TP 2 .B \fIchunk_size\fR\&: The chunk size used by the Erlang fallback to send data\&. If using the fallback, set this to a value that comfortably fits in the systems memory\&. Default is 20 MB\&. .RE .RE .LP .nf .B set_cwd(Dir) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Dir = name() | EncodedBinary .br EncodedBinary = binary() .br Reason = posix() | badarg | no_translation .br .RE .RE .RS .LP Sets the current working directory of the file server to \fIDir\fR\&\&. Returns \fIok\fR\& if successful\&. .LP The functions in the module \fIfile\fR\& usually treat binaries as raw filenames, that is, they are passed "as is" even when the encoding of the binary does not agree with \fInative_name_encoding()\fR\&\&. However, this function expects binaries to be encoded according to the value returned by \fInative_name_encoding()\fR\&\&. .LP Typical error reasons are: .RS 2 .TP 2 .B \fIenoent\fR\&: The directory does not exist\&. .TP 2 .B \fIenotdir\fR\&: A component of \fIDir\fR\& is not a directory\&. On some platforms, \fIenoent\fR\& is returned\&. .TP 2 .B \fIeacces\fR\&: Missing permission for the directory or one of its parents\&. .TP 2 .B \fIbadarg\fR\&: \fIDir\fR\& has an improper type, such as tuple\&. .TP 2 .B \fIno_translation\fR\&: \fIDir\fR\& is a \fIbinary()\fR\& with characters coded in ISO-latin-1 and the VM is operating with unicode filename encoding\&. .RE .LP .RS -4 .B Warning: .RE In a future release, a bad type for argument \fIDir\fR\& will probably generate an exception\&. .RE .LP .nf .B sync(IoDevice) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 IoDevice = io_device() .br Reason = posix() | badarg | terminated .br .RE .RE .RS .LP Ensures 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\&. .LP A typical error reason is: .RS 2 .TP 2 .B \fIenospc\fR\&: Not enough space left to write the file\&. .RE .RE .LP .nf .B truncate(IoDevice) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 IoDevice = io_device() .br Reason = posix() | badarg | terminated .br .RE .RE .RS .LP Truncates the file referenced by \fIIoDevice\fR\& at the current position\&. Returns \fIok\fR\& if successful, otherwise \fI{error, Reason}\fR\&\&. .RE .LP .nf .B write(IoDevice, Bytes) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 IoDevice = io_device() | atom() .br Bytes = iodata() .br Reason = posix() | badarg | terminated .br .RE .RE .RS .LP Writes \fIBytes\fR\& to the file referenced by \fIIoDevice\fR\&\&. This function is the only way to write to a file opened in \fIraw\fR\& mode (although it works for normally opened files too)\&. Returns \fIok\fR\& if successful, and \fI{error, Reason}\fR\& otherwise\&. .LP If the file is opened with \fIencoding\fR\& set to something else than \fIlatin1\fR\&, each byte written can result in many bytes being written to the file, as the byte range 0\&.\&.255 can represent anything between one and four bytes depending on value and UTF encoding type\&. .LP Typical error reasons: .RS 2 .TP 2 .B \fIebadf\fR\&: The file is not opened for writing\&. .TP 2 .B \fIenospc\fR\&: No space is left on the device\&. .RE .RE .LP .nf .B write_file(Filename, Bytes) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Bytes = iodata() .br Reason = posix() | badarg | terminated | system_limit .br .RE .RE .RS .LP Writes the contents of the \fIiodata\fR\& term \fIBytes\fR\& to file \fIFilename\fR\&\&. The file is created if it does not exist\&. If it exists, the previous contents are overwritten\&. Returns \fIok\fR\& if successful, otherwise \fI{error, Reason}\fR\&\&. .LP Typical error reasons: .RS 2 .TP 2 .B \fIenoent\fR\&: A component of the filename does not exist\&. .TP 2 .B \fIenotdir\fR\&: A component of the filename is not a directory\&. On some platforms, \fIenoent\fR\& is returned instead\&. .TP 2 .B \fIenospc\fR\&: No space is left on the device\&. .TP 2 .B \fIeacces\fR\&: Missing permission for writing the file or searching one of the parent directories\&. .TP 2 .B \fIeisdir\fR\&: The named file is a directory\&. .RE .RE .LP .nf .B write_file(Filename, Bytes, Modes) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Bytes = iodata() .br Modes = [mode()] .br Reason = posix() | badarg | terminated | system_limit .br .RE .RE .RS .LP Same as \fIwrite_file/2\fR\&, but takes a third argument \fIModes\fR\&, a list of possible modes, see \fIopen/2\fR\&\&. The mode flags \fIbinary\fR\& and \fIwrite\fR\& are implicit, so they are not to be used\&. .RE .LP .nf .B write_file_info(Filename, FileInfo) -> ok | {error, Reason} .br .fi .br .nf .B write_file_info(Filename, FileInfo, Opts) -> ok | {error, Reason} .br .fi .br .RS .LP Types: .RS 3 Filename = name_all() .br Opts = [file_info_option()] .br FileInfo = file_info() .br Reason = posix() | badarg .br .RE .RE .RS .LP Changes file information\&. Returns \fIok\fR\& if successful, otherwise \fI{error, Reason}\fR\&\&. \fIFileInfo\fR\& is a record \fIfile_info\fR\&, defined in the Kernel include file \fIfile\&.hrl\fR\&\&. Include the following directive in the module from which the function is called: .LP .nf -include_lib("kernel/include/file.hrl"). .fi .LP The time type set in \fIatime\fR\&, \fImtime\fR\&, and \fIctime\fR\& depends on the time type set in \fIOpts :: {time, Type}\fR\& as follows: .RS 2 .TP 2 .B \fIlocal\fR\&: Interprets the time set as local\&. .TP 2 .B \fIuniversal\fR\&: Interprets it as universal time\&. .TP 2 .B \fIposix\fR\&: Must be seconds since or before Unix time epoch, which is 1970-01-01 00:00 UTC\&. .RE .LP Default is \fI{time, local}\fR\&\&. .LP If the option \fIraw\fR\& is set, the file server is not called and only information about local files is returned\&. .LP The following fields are used from the record, if they are specified: .RS 2 .TP 2 .B \fIatime = \fR\&\fIdate_time()\fR\&\fI | integer() >= 0\fR\&: The last time the file was read\&. .TP 2 .B \fImtime = \fR\&\fIdate_time()\fR\&\fI | integer() >= 0\fR\&: The last time the file was written\&. .TP 2 .B \fIctime = \fR\&\fIdate_time()\fR\&\fI | integer() >= 0\fR\&: On Unix, any value specified for this field is ignored (the "ctime" for the file is set to the current time)\&. On Windows, this field is the new creation time to set for the file\&. .TP 2 .B \fImode = integer() >= 0\fR\&: The file permissions as the sum of the following bit values: .RS 2 .TP 2 .B \fI8#00400\fR\&: Read permission: owner .TP 2 .B \fI8#00200\fR\&: Write permission: owner .TP 2 .B \fI8#00100\fR\&: Execute permission: owner .TP 2 .B \fI8#00040\fR\&: Read permission: group .TP 2 .B \fI8#00020\fR\&: Write permission: group .TP 2 .B \fI8#00010\fR\&: Execute permission: group .TP 2 .B \fI8#00004\fR\&: Read permission: other .TP 2 .B \fI8#00002\fR\&: Write permission: other .TP 2 .B \fI8#00001\fR\&: Execute permission: other .TP 2 .B \fI16#800\fR\&: Set user id on execution .TP 2 .B \fI16#400\fR\&: Set group id on execution .RE .RS 2 .LP On Unix platforms, other bits than those listed above may be set\&. .RE .TP 2 .B \fIuid = integer() >= 0\fR\&: Indicates the file owner\&. Ignored for non-Unix file systems\&. .TP 2 .B \fIgid = integer() >= 0\fR\&: Gives the group that the file owner belongs to\&. Ignored for non-Unix file systems\&. .RE .LP Typical error reasons: .RS 2 .TP 2 .B \fIeacces\fR\&: Missing search permission for one of the parent directories of the file\&. .TP 2 .B \fIenoent\fR\&: The file does not exist\&. .TP 2 .B \fIenotdir\fR\&: A component of the filename is not a directory\&. On some platforms, \fIenoent\fR\& is returned instead\&. .RE .RE .SH "POSIX ERROR CODES" .RS 2 .TP 2 * \fIeacces\fR\& - Permission denied .LP .TP 2 * \fIeagain\fR\& - Resource temporarily unavailable .LP .TP 2 * \fIebadf\fR\& - Bad file number .LP .TP 2 * \fIebusy\fR\& - File busy .LP .TP 2 * \fIedquot\fR\& - Disk quota exceeded .LP .TP 2 * \fIeexist\fR\& - File already exists .LP .TP 2 * \fIefault\fR\& - Bad address in system call argument .LP .TP 2 * \fIefbig\fR\& - File too large .LP .TP 2 * \fIeintr\fR\& - Interrupted system call .LP .TP 2 * \fIeinval\fR\& - Invalid argument .LP .TP 2 * \fIeio\fR\& - I/O error .LP .TP 2 * \fIeisdir\fR\& - Illegal operation on a directory .LP .TP 2 * \fIeloop\fR\& - Too many levels of symbolic links .LP .TP 2 * \fIemfile\fR\& - Too many open files .LP .TP 2 * \fIemlink\fR\& - Too many links .LP .TP 2 * \fIenametoolong\fR\& - Filename too long .LP .TP 2 * \fIenfile\fR\& - File table overflow .LP .TP 2 * \fIenodev\fR\& - No such device .LP .TP 2 * \fIenoent\fR\& - No such file or directory .LP .TP 2 * \fIenomem\fR\& - Not enough memory .LP .TP 2 * \fIenospc\fR\& - No space left on device .LP .TP 2 * \fIenotblk\fR\& - Block device required .LP .TP 2 * \fIenotdir\fR\& - Not a directory .LP .TP 2 * \fIenotsup\fR\& - Operation not supported .LP .TP 2 * \fIenxio\fR\& - No such device or address .LP .TP 2 * \fIeperm\fR\& - Not owner .LP .TP 2 * \fIepipe\fR\& - Broken pipe .LP .TP 2 * \fIerofs\fR\& - Read-only file system .LP .TP 2 * \fIespipe\fR\& - Invalid seek .LP .TP 2 * \fIesrch\fR\& - No such process .LP .TP 2 * \fIestale\fR\& - Stale remote file handle .LP .TP 2 * \fIexdev\fR\& - Cross-domain link .LP .RE .SH "PERFORMANCE" .LP For increased performance, raw files are recommended\&. .LP A normal file is really a process so it can be used as an I/O device (see \fIio\fR\&)\&. 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 the file server of another node, also binaries are copied\&. .LP .RS -4 .B Note: .RE Raw files use the file system of the host machine of the node\&. 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 the file server of another node, and the other node runs on some other host machine, they can have different file systems\&. However, this is seldom a problem\&. .LP \fIopen/2\fR\& can be given the options \fIdelayed_write\fR\& and \fIread_ahead\fR\& to turn on caching, which will reduce the number of operating system calls and greatly improve performance for small reads and writes\&. However, the overhead won\&'t disappear completely and it\&'s best to keep the number of file operations to a minimum\&. As a contrived example, the following function writes 4MB in 2\&.5 seconds when tested: .LP .nf create_file_slow(Name) -> {ok, Fd} = file:open(Name, [raw, write, delayed_write, binary]), create_file_slow_1(Fd, 4 bsl 20), file:close(Fd). create_file_slow_1(_Fd, 0) -> ok; create_file_slow_1(Fd, M) -> ok = file:write(Fd, <<0>>), create_file_slow_1(Fd, M - 1). .fi .LP The following functionally equivalent code writes 128 bytes per call to \fIwrite/2\fR\& and so does the same work in 0\&.08 seconds, which is roughly 30 times faster: .LP .nf create_file(Name) -> {ok, Fd} = file:open(Name, [raw, write, delayed_write, binary]), create_file_1(Fd, 4 bsl 20), file:close(Fd), ok. create_file_1(_Fd, 0) -> ok; create_file_1(Fd, M) when M >= 128 -> ok = file:write(Fd, <<0:(128)/unit:8>>), create_file_1(Fd, M - 128); create_file_1(Fd, M) -> ok = file:write(Fd, <<0:(M)/unit:8>>), create_file_1(Fd, M - 1). .fi .LP When writing data it\&'s generally more efficient to write a list of binaries rather than a list of integers\&. It is not needed 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 \fIwrite(FD, [Bin1, Bin2 | Bin3])\fR\& writes the contents of the binaries without copying the data at all, except for perhaps deep down in the operating system kernel\&. .LP .RS -4 .B Warning: .RE If an error occurs when accessing an open file with module \fIio\fR\&, the process handling the file exits\&. The dead file process can hang if a process tries to access it later\&. This will be fixed in a future release\&. .SH "SEE ALSO" .LP \fIfilename(3erl)\fR\&