summaryrefslogtreecommitdiff
path: root/lib/stdlib/test/shell_docs_SUITE_data/kernel_file_open_2_func.txt
blob: b89e725d7b73bc34ce9cee1ad17613c8f8693895 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255

  -spec open(File, Modes) -> {ok, IoDevice} | {error, Reason}
                when
                    File :: Filename | iodata(),
                    Filename :: name_all(),
                    Modes :: [mode() | ram | directory],
                    IoDevice :: io_device(),
                    Reason :: posix() | badarg | system_limit.

  Opens file File in the mode determined by Modes, which can
  contain one or more of the following options:

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

  write:
    The file is opened for writing. It is created if it does not
    exist. If the file exists and write is not combined with 
    read, the file is truncated.

  append:
    The file is opened for writing. It is created if it does not
    exist. Every write operation to a file opened with append
    takes place at the end of the file.

  exclusive:
    The file is opened for writing. It is created if it does not
    exist. If the file exists, {error, eexist} is returned.

    Warning:
      This option does not guarantee exclusiveness on file
      systems not supporting O_EXCL 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).

  raw:
    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:

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

     • Especially if read_line/1 is to be used on a raw
       file, it is recommended to combine this option with
       option {read_ahead, Size} as line-oriented I/O is
       inefficient without buffering.

     • Only the Erlang process that opened the file can use it.

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

  binary:
    Read operations on the file return binaries rather than lists.

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

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

    When this option is used, the result of write/2 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.

    For example, when delayed_write is used, after a number of 
    write/2 calls, close/1 can return {error, enospc}, as
    there is not enough space on the disc for previously written
    data. close/1 must probably be called again, as the file is
    still open.

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

  {read_ahead, Size}:
    Activates read data buffering. If read/2 calls are for
    significantly less than Size bytes, read operations to 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 as the number of
    operating system calls is reduced.

    The read_ahead buffer is also highly used by function 
    read_line/1 in raw mode, therefore this option is
    recommended (for performance reasons) when accessing raw files
    using that function.

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

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

  compressed:
    Makes it possible to read or write gzip compressed files.
    Option compressed must be combined with read or write,
    but not both. Notice that the file size obtained with 
    read_file_info/1 does probably not match the number of bytes
    that can be read from a compressed file.

  {encoding, Encoding}:
    Makes the file perform automatic translation of characters to
    and from a specific (Unicode) encoding. Notice that the data
    supplied to write/2 or returned by read/2 still is
    byte-oriented; this option denotes only how data is stored in
    the disk file.

    Depending on the encoding, different methods of reading and
    writing data is preferred. The default encoding of latin1
    implies using this module (file) for reading and writing
    data as the interfaces provided here work with byte-oriented
    data. Using other (Unicode) encodings makes the io(3)
    functions get_chars, get_line, and put_chars more
    suitable, as they can work with the full Unicode range.

    If data is sent to an io_device() 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.

    Allowed values for Encoding:

    latin1:
      The default encoding. Bytes supplied to the file, that is, 
      write/2 are written "as is" on the file. Likewise, bytes
      read from the file, that is, read/2 are returned "as
      is". If module io(3) is used for writing, the file can
      only cope with Unicode characters up to code point 255
      (the ISO Latin-1 range).

    unicode or utf8:
      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 read/2,
      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 io(3).

      Bytes written to the file by any means are translated to
      UTF-8 encoding before being stored on the disk file.

    utf16 or {utf16,big}:
      Works like unicode, but translation is done to and from
      big endian UTF-16 instead of UTF-8.

    {utf16,little}:
      Works like unicode, but translation is done to and from
      little endian UTF-16 instead of UTF-8.

    utf32 or {utf32,big}:
      Works like unicode, but translation is done to and from
      big endian UTF-32 instead of UTF-8.

    {utf32,little}:
      Works like unicode, but translation is done to and from
      little endian UTF-32 instead of UTF-8.

    The Encoding can be changed for a file "on the fly" by using
    function io:setopts/2. 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 unicode(3).

    This option is not allowed on raw files.

  ram:
    File must be iodata(). Returns an fd(), which lets module 
    file operate on the data in-memory as if it is a file.

  sync:
    On platforms supporting it, enables the POSIX O_SYNC
    synchronous I/O flag or its platform-dependent equivalent (for
    example, FILE_FLAG_WRITE_THROUGH 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 O_SYNC or equivalent, use of the sync
    flag causes open to return {error, enotsup}.

  directory:
    Allows open to work on directories.

  Returns:

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

  {error, Reason}:
    The file cannot be opened.

  IoDevice 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 IoDevice
  returned from this call can be used as an argument to the I/O
  functions (see io(3)).

  Warning:
    While this function can be used to open any file, we recommend
    against using it for NFS-mounted files, FIFOs, devices, or
    similar since they can cause IO threads to hang forever.

    If your application needs to interact with these kinds of
    files we recommend breaking out those parts to a port program
    instead.

  Note:
    In previous versions of file, modes were specified as one of
    the atoms read, write, or read_write 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 read_write
    is not allowed in a mode list.

  Typical error reasons:

  enoent:
    The file does not exist.

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

  eisdir:
    The named file is a directory.

  enotdir:
    A component of the filename is not a directory, or the
    filename itself is not a directory if directory mode was
    specified. On some platforms, enoent is returned instead.

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