PageRenderTime 780ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/erlang_otp/_sources/lib/kernel-2.13.3/doc/html/file.txt

https://bitbucket.org/docs/docs.bitbucket.org
Plain Text | 1623 lines | 1114 code | 509 blank | 0 comment | 0 complexity | 85b7ef4fe58eb6859ca23f50325d08d9 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. =======================================
  2. Lib - Kernel-2.13.3 - Doc - Html - File
  3. =======================================
  4. ` <http://www.ericsson.com/technology/opensource/erlang>`__
  5. file
  6. ====
  7. MODULE
  8. ~~~~~~
  9. file
  10. MODULE SUMMARY
  11. ~~~~~~~~~~~~~~
  12. File Interface Module
  13. DESCRIPTION
  14. ~~~~~~~~~~~
  15. The module file provides an interface to the file system.
  16. On operating systems with thread support, it is possible to let file
  17. operations be performed in threads of their own, allowing other Erlang
  18. processes to continue executing in parallel with the file operations.
  19. See the command line flag+A in `erl(1)
  20. <javascript:erlhref('../../../../',%20'erts',%20'erl.html');>`__.
  21. DATA TYPES
  22. ~~~~~~~~~~
  23. .. sourcecode:: erlang
  24. iodata() = iolist() | binary()
  25. iolist() = [char() | binary() | iolist()]
  26. io_device()
  27. as returned by file:open/2, a process handling IO protocols
  28. name() = string() | atom() | DeepList
  29. DeepList = [char() | atom() | DeepList]
  30. posix()
  31. an atom which is named from the Posix error codes used in
  32. Unix, and in the runtime libraries of most C compilers
  33. ext_posix() = posix() | badarg
  34. time() = {{Year, Month, Day}, {Hour, Minute, Second}}
  35. Year = Month = Day = Hour = Minute = Second = int()
  36. Must denote a valid date and time
  37. EXPORTS
  38. ~~~~~~~
  39. change_group(Filename, Gid) -> ok | {error, Reason}
  40. Types:
  41. Filename = name()
  42. Gid = int()
  43. Reason = ext_posix()
  44. Changes group of a file. Seewrite_file_info/2.
  45. change_owner(Filename, Uid) -> ok | {error, Reason}
  46. Types:
  47. Filename = name()
  48. Uid = int()
  49. Reason = ext_posix()
  50. Changes owner of a file. Seewrite_file_info/2.
  51. change_owner(Filename, Uid, Gid) -> ok | {error, Reason}
  52. Types:
  53. Filename = name()
  54. Uid = int()
  55. Gid = int()
  56. Reason = ext_posix()
  57. Changes owner and group of a file. Seewrite_file_info/2.
  58. change_time(Filename, Mtime) -> ok | {error, Reason}
  59. Types:
  60. Filename = name()
  61. Mtime = time()
  62. Reason = ext_posix()
  63. Changes the modification and access times of a file.
  64. Seewrite_file_info/2.
  65. change_time(Filename, Mtime, Atime) -> ok | {error, Reason}
  66. Types:
  67. Filename = name()
  68. Mtime = Atime = time()
  69. Reason = ext_posix()
  70. Changes the modification and last access times of a file.
  71. Seewrite_file_info/2.
  72. close(IoDevice) -> ok | {error, Reason}
  73. Types:
  74. IoDevice = io_device()
  75. Reason = ext_posix() | terminated
  76. Closes the file referenced by IoDevice. It mostly returns ok, expect
  77. for some severe errors such as out of memory.
  78. Note that if the option delayed_write was used when opening the file,
  79. close/1 might return an old write error and not even try to close the
  80. file. Seeopen/2.
  81. consult(Filename) -> {ok, Terms} | {error, Reason}
  82. Types:
  83. Filename = name()
  84. Terms = [term()]
  85. Reason = ext_posix() | terminated | system_limit | {Line, Mod, Term}
  86. Line, Mod, Term -- see below
  87. Reads Erlang terms, separated by '.', from Filename. Returns one of
  88. the following:
  89. :{ok, Terms}: The file was successfully read.
  90. :{error, atom()}: An error occurred when opening the file or reading
  91. it. See open/2 for a list of typical error codes.
  92. :{error, {Line, Mod, Term}}: An error occurred when interpreting the
  93. Erlang terms in the file. Use format_error/1 to convert the three-
  94. element tuple to an English description of the error.
  95. Example:
  96. .. sourcecode:: erlang
  97. f.txt: {person, "kalle", 25}.
  98. {person, "pelle", 30}.
  99. .. sourcecode:: erlang
  100. 1> file:consult("f.txt").
  101. {ok,[{person,"kalle",25},{person,"pelle",30}]}
  102. copy(Source, Destination) ->
  103. copy(Source, Destination, ByteCount) -> {ok, BytesCopied} | {error,
  104. Reason}
  105. Types:
  106. Source = Destination = io_device() | Filename | {Filename, Modes}
  107. Filename = name()
  108. Modes = [Mode] -- see open/2
  109. ByteCount = int() >= 0 | infinity
  110. BytesCopied = int()
  111. Copies ByteCount bytes from Source toDestination. Source and
  112. Destination refer to either filenames or IO devices from e.g.
  113. open/2.ByteCount defaults infinity, denoting an infinite number of
  114. bytes.
  115. The argument Modes is a list of possible modes, seeopen/2, and
  116. defaults to [].
  117. If both Source and Destination refer to filenames, the files are
  118. opened with [read, binary] and [write, binary] prepended to their mode
  119. lists, respectively, to optimize the copy.
  120. If Source refers to a filename, it is opened withread mode prepended
  121. to the mode list before the copy, and closed when done.
  122. If Destination refers to a filename, it is opened with write mode
  123. prepended to the mode list before the copy, and closed when done.
  124. Returns {ok, BytesCopied} where BytesCopied is the number of bytes
  125. that actually was copied, which may be less than ByteCount if end of
  126. file was encountered on the source. If the operation fails, {error,
  127. Reason} is returned.
  128. Typical error reasons: As for open/2 if a file had to be opened, and
  129. as for read/2 and write/2.
  130. del_dir(Dir) -> ok | {error, Reason}
  131. Types:
  132. Dir = name()
  133. Reason = ext_posix()
  134. Tries to delete the directory Dir. The directory must be empty before
  135. it can be deleted. Returns ok if successful.
  136. Typical error reasons are:
  137. :eacces: Missing search or write permissions for the parent
  138. directories of Dir.
  139. :eexist: The directory is not empty.
  140. :enoent: The directory does not exist.
  141. :enotdir: A component of Dir is not a directory. On some platforms,
  142. enoent is returned instead.
  143. :einval: Attempt to delete the current directory. On some platforms,
  144. eacces is returned instead.
  145. delete(Filename) -> ok | {error, Reason}
  146. Types:
  147. Filename = name()
  148. Reason = ext_posix()
  149. Tries to delete the file Filename. Returns ok if successful.
  150. Typical error reasons are:
  151. :enoent: The file does not exist.
  152. :eacces: Missing permission for the file or one of its parents.
  153. :eperm: The file is a directory and the user is not super-user.
  154. :enotdir: A component of the file name is not a directory. On some
  155. platforms, enoent is returned instead.
  156. :einval: Filename had an improper type, such as tuple.
  157. Warning
  158. In a future release, a bad type for the Filename argument will
  159. probably generate an exception.
  160. eval(Filename) -> ok | {error, Reason}
  161. Types:
  162. Filename = name()
  163. Reason = ext_posix() | terminated | system_limit | {Line, Mod, Term}
  164. Line, Mod, Term -- see below
  165. Reads and evaluates Erlang expressions, separated by '.' (or ',', a
  166. sequence of expressions is also an expression), fromFilename. The
  167. actual result of the evaluation is not returned; any expression
  168. sequence in the file must be there for its side effect. Returns one of
  169. the following:
  170. :ok: The file was read and evaluated.
  171. :{error, atom()}: An error occurred when opening the file or reading
  172. it. See open/2 for a list of typical error codes.
  173. :{error, {Line, Mod, Term}}: An error occurred when interpreting the
  174. Erlang expressions in the file. Use format_error/1 to convert the
  175. three-element tuple to an English description of the error.
  176. eval(Filename, Bindings) -> ok | {error, Reason}
  177. Types:
  178. Filename = name()
  179. Bindings -- see erl_eval(3)
  180. Reason = ext_posix() | terminated | system_limit | {Line, Mod, Term}
  181. Line, Mod, Term -- see eval/1
  182. The same as eval/1 but the variable bindingsBindings are used in the
  183. evaluation. See `erl_eval(3) <javascript:erlhref('../../../../',%20'st
  184. dlib',%20'erl_eval.html');>`__ about variable bindings.
  185. file_info(Filename) -> {ok, FileInfo} | {error, Reason}
  186. This function is obsolete. Use read_file_info/1 instead.
  187. format_error(Reason) -> Chars
  188. Types:
  189. Reason = atom() | {Line, Mod, Term}
  190. Line, Mod, Term -- see eval/1
  191. Chars = [char() | Chars]
  192. Given the error reason returned by any function in this module,
  193. returns a descriptive string of the error in English.
  194. get_cwd() -> {ok, Dir} | {error, Reason}
  195. Types:
  196. Dir = string()
  197. Reason = posix()
  198. Returns {ok, Dir}, where Dir is the current working directory of the
  199. file server.
  200. Note
  201. In rare circumstances, this function can fail on Unix. It may happen
  202. if read permission does not exist for the parent directories of the
  203. current directory.
  204. Typical error reasons are:
  205. :eacces: Missing read permission for one of the parents of the current
  206. directory.
  207. get_cwd(Drive) -> {ok, Dir} | {error, Reason}
  208. Types:
  209. Drive = string() -- see below
  210. Dir = string()
  211. Reason = ext_posix()
  212. Drive should be of the form "Letter:", for example "c:". Returns {ok,
  213. Dir} or{error, Reason}, where Dir is the current working directory of
  214. the drive specified.
  215. This function returns {error, enotsup} on platforms which have no
  216. concept of current drive (Unix, for example).
  217. Typical error reasons are:
  218. :enotsup: The operating system have no concept of drives.
  219. :eacces: The drive does not exist.
  220. :einval: The format of Drive is invalid.
  221. list_dir(Dir) -> {ok, Filenames} | {error, Reason}
  222. Types:
  223. Dir = name()
  224. Filenames = [Filename]
  225. Filename = string()
  226. Reason = ext_posix()
  227. Lists all the files in a directory. Returns{ok, Filenames} if
  228. successful. Otherwise, it returns{error, Reason}. Filenames is a list
  229. of the names of all the files in the directory. The names are not
  230. sorted.
  231. Typical error reasons are:
  232. :eacces: Missing search or write permissions for Dir or one of its
  233. parent directories.
  234. :enoent: The directory does not exist.
  235. make_dir(Dir) -> ok | {error, Reason}
  236. Types:
  237. Dir = name()
  238. Reason = ext_posix()
  239. Tries to create the directory Dir. Missing parent directories are not
  240. created. Returns ok if successful.
  241. Typical error reasons are:
  242. :eacces: Missing search or write permissions for the parent
  243. directories of Dir.
  244. :eexist: There is already a file or directory named Dir.
  245. :enoent: A component of Dir does not exist.
  246. :enospc: There is a no space left on the device.
  247. :enotdir: A component of Dir is not a directory. On some platforms,
  248. enoent is returned instead.
  249. make_link(Existing, New) -> ok | {error, Reason}
  250. Types:
  251. Existing = New = name()
  252. Reason = ext_posix()
  253. Makes a hard link from Existing to New, on platforms that support
  254. links (Unix). This function returnsok if the link was successfully
  255. created, or{error, Reason}. On platforms that do not support links,
  256. {error,enotsup} is returned.
  257. Typical error reasons:
  258. :eacces: Missing read or write permissions for the parent directories
  259. of Existing or New.
  260. :eexist: New already exists.
  261. :enotsup: Hard links are not supported on this platform.
  262. make_symlink(Name1, Name2) -> ok | {error, Reason}
  263. Types:
  264. Name1 = Name2 = name()
  265. Reason = ext_posix()
  266. This function creates a symbolic link Name2 to the file or directory
  267. Name1, on platforms that support symbolic links (most Unix systems).
  268. Name1 need not exist. This function returns ok if the link was
  269. successfully created, or {error, Reason}. On platforms that do not
  270. support symbolic links, {error, enotsup} is returned.
  271. Typical error reasons:
  272. :eacces: Missing read or write permissions for the parent directories
  273. of Name1 or Name2.
  274. :eexist: Name2 already exists.
  275. :enotsup: Symbolic links are not supported on this platform.
  276. open(Filename, Modes) -> {ok, IoDevice} | {error, Reason}
  277. Types:
  278. Filename = name()
  279. Modes = [Mode]
  280. Mode = read | write | append | raw | binary | {delayed_write, Size,
  281. Delay} | delayed_write | {read_ahead, Size} | read_ahead | compressed
  282. Size = Delay = int()
  283. IoDevice = io_device()
  284. Reason = ext_posix() | system_limit
  285. Opens the file Filename in the mode determined byModes, which may
  286. contain one or more of the following items:
  287. :read: The file, which must exist, is opened for reading.
  288. :write: The file is opened for writing. It is created if it does not
  289. exist. If the file exists, and if write is not combined with read, the
  290. file will be truncated.
  291. :append: The file will be opened for writing, and it will be created
  292. if it does not exist. Every write operation to a file opened with
  293. append will take place at the end of the file.
  294. :raw: The raw option allows faster access to a file, because no Erlang
  295. process is needed to handle the file. However, a file opened in this
  296. way has the following limitations:
  297. + The functions in the io module cannot be used, because they can only
  298. talk to an Erlang process. Instead, use the read/2, read_line/1 and
  299. write/2 functions.
  300. + Especially if read_line/1 is to be used on a raw file, it is
  301. recommended to combine this option with the {read_ahead, Size} option
  302. as line oriented I/O is inefficient without buffering.
  303. + Only the Erlang process which opened the file can use it.
  304. + A remote Erlang file server cannot be used; the computer on which
  305. the Erlang node is running must have access to the file system
  306. (directly or through NFS).
  307. :binary: When this option has been given, read operations on the file
  308. will return binaries rather than lists.
  309. :{delayed_write, Size, Delay}: If this option is used, the data in
  310. subsequentwrite/2 calls is buffered until there are at leastSize bytes
  311. buffered, or until the oldest buffered data is Delay milliseconds old.
  312. Then all buffered data is written in one operating system call. The
  313. buffered data is also flushed before some other file operation than
  314. write/2 is executed. The purpose of this option is to increase
  315. performance by reducing the number of operating system calls, so
  316. thewrite/2 calls should be for sizes significantly less than Size, and
  317. not interspersed by to many other file operations, for this to happen.
  318. When this option is used, the result of write/2 calls may prematurely
  319. be reported as successful, and if a write error should actually occur
  320. the error is reported as the result of the next file operation, which
  321. is not executed. For example, when delayed_write is used, after a
  322. number of write/2 calls, close/1 might return {error, enospc} because
  323. there was not enough space on the disc for previously written data,
  324. andclose/1 should probably be called again since the file is still
  325. open.
  326. :delayed_write: The same as {delayed_write, Size, Delay} with
  327. reasonable default values for Size andDelay. (Roughly some 64 KBytes,
  328. 2 seconds)
  329. :{read_ahead, Size}: This option activates read data buffering.
  330. Ifread/2 calls are for significantly less thanSize bytes, read
  331. operations towards the operating system are still performed for blocks
  332. of Size bytes. The extra data is buffered and returned in subsequent
  333. read/2 calls, giving a performance gain since the number of operating
  334. system calls is reduced. The read_ahead buffer is also highly utilized
  335. by the read_line/1 function in raw mode, why this option is
  336. recommended (for performance reasons) when accessing raw files using
  337. that function. If read/2 calls are for sizes not significantly less
  338. than, or even greater than Size bytes, no performance gain can be
  339. expected.
  340. :read_ahead: The same as {read_ahead, Size} with a reasonable default
  341. value for Size. (Roughly some 64 KBytes)
  342. :compressed: Makes it possible to read or write gzip compressed files.
  343. The compressesed option must be combined with either read or write,
  344. but not both. Note that the file size obtained withread_file_info/1
  345. will most probably not match the number of bytes that can be read from
  346. a compressed file.
  347. :{encoding, Encoding}: Makes the file perform automatic translation of
  348. characters to and from a specific (Unicode) encoding. Note that the
  349. data supplied to file:write or returned by file:read still is byte
  350. oriented, this option only denotes how data is actually stored in the
  351. disk file. Depending on the encoding, different methods of reading and
  352. writing data is preferred. The default encoding of latin1 implies
  353. using this (the file) module for reading and writing data, as the
  354. interfaces provided here work with byte-oriented data, while using
  355. other (Unicode) encodings makes the `io(3)
  356. <javascript:erlhref('../../../../',%20'stdlib',%20'io.html');>`__
  357. module's get_chars, get_line and put_chars functions more suitable, as
  358. they can work with the full Unicode range. If data is sent to an
  359. io_device() in a format that cannot be converted to the specified
  360. encoding, or if data is read by a function that returns data in a
  361. format that cannot cope with the character range of the data, an error
  362. occurs and the file will be closed. The allowed values for Encoding
  363. are:
  364. :latin1: The default encoding. Bytes supplied to i.e. file:write are
  365. written as is on the file, likewise bytes read from the file are
  366. returned to i.e. file:read as is. If the `io(3)
  367. <javascript:erlhref('../../../../',%20'stdlib',%20'io.html');>`__
  368. module is used for writing, the file can only cope with Unicode
  369. characters up to codepoint 255 (the ISO-latin-1 range).
  370. :unicode or utf8: Characters are translated to and from the UTF-8
  371. encoding before being written to or read from the file. A file opened
  372. in this way might be readable using the file:read function, as long as
  373. no data stored on the file lies beyond the ISO-latin-1 range (0..255),
  374. but failure will occur if the data contains Unicode codepoints beyond
  375. that range. The file is best read with the functions in the Unicode
  376. aware `io(3)
  377. <javascript:erlhref('../../../../',%20'stdlib',%20'io.html');>`__
  378. module. Bytes written to the file by any means are translated to UTF-8
  379. encoding before actually being stored on the disk file.
  380. :utf16 or {utf16,big}: Works like unicode, but translation is done to
  381. and from big endian UTF-16 instead of UTF-8.
  382. :{utf16,little}: Works like unicode, but translation is done to and
  383. from little endian UTF-16 instead of UTF-8.
  384. :utf32 or {utf32,big}: Works like unicode, but translation is done to
  385. and from big endian UTF-32 instead of UTF-8.
  386. :{utf32,little}: Works like unicode, but translation is done to and
  387. from little endian UTF-32 instead of UTF-8.
  388. The Encoding can be changed for a file "on the fly" by using the
  389. `io:setopts/2 <javascript:erlhref('../../../../',%20'stdlib',%20'io.ht
  390. ml#setopts-2');>`__ function, why a file can be analyzed in latin1
  391. encoding for i.e. a BOM, positioned beyond the BOM and then be set for
  392. the right encoding before further reading.See the `unicode(3)
  393. <javascript:erlhref('../../../../',%20'stdlib',%20'unicode.html');>`__
  394. module for functions identifying BOM's. This option is not allowed on
  395. raw files.
  396. Returns:
  397. :{ok, IoDevice}: The file has been opened in the requested
  398. mode.IoDevice is a reference to the file.
  399. :{error, Reason}: The file could not be opened.
  400. IoDevice is really the pid of the process which handles the file. This
  401. process is linked to the process which originally opened the file. If
  402. any process to which the IoDevice is linked terminates, the file will
  403. be closed and the process itself will be terminated. An IoDevice
  404. returned from this call can be used as an argument to the IO functions
  405. (see `io(3)
  406. <javascript:erlhref('../../../../',%20'stdlib',%20'io.html');>`__).
  407. Note
  408. In previous versions of file, modes were given as one of the atoms
  409. read, write, orread_write instead of a list. This is still allowed for
  410. reasons of backwards compatibility, but should not be used for new
  411. code. Also note that read_write is not allowed in a mode list.
  412. Typical error reasons:
  413. :enoent: The file does not exist.
  414. :eacces: Missing permission for reading the file or searching one of
  415. the parent directories.
  416. :eisdir: The named file is not a regular file. It may be a directory,
  417. a fifo, or a device.
  418. :enotdir: A component of the file name is not a directory. On some
  419. platforms, enoent is returned instead.
  420. :enospc: There is a no space left on the device (if write access was
  421. specified).
  422. path_consult(Path, Filename) -> {ok, Terms, FullName} | {error,
  423. Reason}
  424. Types:
  425. Path = [Dir]
  426. Dir = name()
  427. Filename = name()
  428. Terms = [term()]
  429. FullName = string()
  430. Reason = ext_posix() | terminated | system_limit | {Line, Mod, Term}
  431. Line, Mod, Term -- see below
  432. Searches the path Path (a list of directory names) until the file
  433. Filename is found. If Filename is an absolute filename, Path is
  434. ignored. Then reads Erlang terms, separated by '.', from the file.
  435. Returns one of the following:
  436. :{ok, Terms, FullName}: The file was successfully read. FullName is
  437. the full name of the file.
  438. :{error, enoent}: The file could not be found in any of the
  439. directories inPath.
  440. :{error, atom()}: An error occurred when opening the file or reading
  441. it. See open/2 for a list of typical error codes.
  442. :{error, {Line, Mod, Term}}: An error occurred when interpreting the
  443. Erlang terms in the file. Use format_error/1 to convert the three-
  444. element tuple to an English description of the error.
  445. path_eval(Path, Filename) -> {ok, FullName} | {error, Reason}
  446. Types:
  447. Path = [Dir]
  448. Dir = name()
  449. Filename = name()
  450. FullName = string()
  451. Reason = ext_posix() | terminated | system_limit | {Line, Mod, Term}
  452. Line, Mod, Term -- see below
  453. Searches the path Path (a list of directory names) until the file
  454. Filename is found. If Filename is an absolute file name, Path is
  455. ignored. Then reads and evaluates Erlang expressions, separated by '.'
  456. (or ',', a sequence of expressions is also an expression), from the
  457. file. The actual result of evaluation is not returned; any expression
  458. sequence in the file must be there for its side effect. Returns one of
  459. the following:
  460. :{ok, FullName}: The file was read and evaluated. FullName is the full
  461. name of the file.
  462. :{error, enoent}: The file could not be found in any of the
  463. directories inPath.
  464. :{error, atom()}: An error occurred when opening the file or reading
  465. it. See open/2 for a list of typical error codes.
  466. :{error, {Line, Mod, Term}}: An error occurred when interpreting the
  467. Erlang expressions in the file. Use format_error/1 to convert the
  468. three-element tuple to an English description of the error.
  469. path_open(Path, Filename, Modes) -> {ok, IoDevice, FullName} | {error,
  470. Reason}
  471. Types:
  472. Path = [Dir]
  473. Dir = name()
  474. Filename = name()
  475. Modes = [Mode] -- see open/2
  476. IoDevice = io_device()
  477. FullName = string()
  478. Reason = ext_posix() | system_limit
  479. Searches the path Path (a list of directory names) until the file
  480. Filename is found. If Filename is an absolute file name, Path is
  481. ignored. Then opens the file in the mode determined by Modes. Returns
  482. one of the following:
  483. :{ok, IoDevice, FullName}: The file has been opened in the requested
  484. mode.IoDevice is a reference to the file andFullName is the full name
  485. of the file.
  486. :{error, enoent}: The file could not be found in any of the
  487. directories inPath.
  488. :{error, atom()}: The file could not be opened.
  489. path_script(Path, Filename) -> {ok, Value, FullName} | {error, Reason}
  490. Types:
  491. Path = [Dir]
  492. Dir = name()
  493. Filename = name()
  494. Value = term()
  495. FullName = string()
  496. Reason = ext_posix() | terminated | system_limit | {Line, Mod, Term}
  497. Line, Mod, Term -- see below
  498. Searches the path Path (a list of directory names) until the file
  499. Filename is found. If Filename is an absolute file name, Path is
  500. ignored. Then reads and evaluates Erlang expressions, separated by '.'
  501. (or ',', a sequence of expressions is also an expression), from the
  502. file. Returns one of the following:
  503. :{ok, Value, FullName}: The file was read and evaluated. FullName is
  504. the full name of the file and Value the value of the last expression.
  505. :{error, enoent}: The file could not be found in any of the
  506. directories inPath.
  507. :{error, atom()}: An error occurred when opening the file or reading
  508. it. See open/2 for a list of typical error codes.
  509. :{error, {Line, Mod, Term}}: An error occurred when interpreting the
  510. Erlang expressions in the file. Use format_error/1 to convert the
  511. three-element tuple to an English description of the error.
  512. path_script(Path, Filename, Bindings) -> {ok, Value, FullName} |
  513. {error, Reason}
  514. Types:
  515. Path = [Dir]
  516. Dir = name()
  517. Filename = name()
  518. Bindings -- see erl_eval(3)
  519. Value = term()
  520. FullName = string()
  521. Reason = posix() | terminated | system_limit | {Line, Mod, Term}
  522. Line, Mod, Term -- see path_script/2
  523. The same as path_script/2 but the variable bindingsBindings are used
  524. in the evaluation. See `erl_eval(3) <javascript:erlhref('../../../../'
  525. ,%20'stdlib',%20'erl_eval.html');>`__ about variable bindings.
  526. pid2name(Pid) -> string() | undefined
  527. Types:
  528. Pid = pid()
  529. If Pid is an IO device, that is, a pid returned fromopen/2, this
  530. function returns the filename, or rather:
  531. :{ok, Filename}: If this node's file server is not a slave, the file
  532. was opened by this node's file server, (this implies thatPid must be a
  533. local pid) and the file is not closed. Filename is the filename in
  534. flat string format.
  535. :undefined: In all other cases.
  536. Warning
  537. This function is intended for debugging only.
  538. position(IoDevice, Location) -> {ok, NewPosition} | {error, Reason}
  539. Types:
  540. IoDevice = io_device()
  541. Location = Offset | {bof, Offset} | {cur, Offset} | {eof, Offset} |
  542. bof | cur | eof
  543. Offset = int()
  544. NewPosition = int()
  545. Reason = ext_posix() | terminated
  546. Sets the position of the file referenced by IoDevice to Location.
  547. Returns {ok, NewPosition} (as absolute offset) if successful,
  548. otherwise{error, Reason}. Location is one of the following:
  549. :Offset: The same as {bof, Offset}.
  550. :{bof, Offset}: Absolute offset.
  551. :{cur, Offset}: Offset from the current position.
  552. :{eof, Offset}: Offset from the end of file.
  553. :bof | cur | eof: The same as above with Offset 0.
  554. Note that offsets are counted in bytes, not in characters. If the file
  555. is opened using some other encoding than latin1, one byte does not
  556. correspond to one character. Positioning in such a file can only be
  557. done to known character boundaries, i.e. to a position earlier
  558. retrieved by getting a current position, to the beginning/end of the
  559. file or to some other position known to be on a correct character
  560. boundary by some other means (typically beyond a byte order mark in
  561. the file, which has a known byte-size).
  562. Typical error reasons are:
  563. :einval: Either Location was illegal, or it evaluated to a negative
  564. offset in the file. Note that if the resulting position is a negative
  565. value, the result is an error, and after the call the file position is
  566. undefined.
  567. pread(IoDevice, LocNums) -> {ok, DataL} | eof | {error, Reason}
  568. Types:
  569. IoDevice = io_device()
  570. LocNums = [{Location, Number}]
  571. Location -- see position/2
  572. Number = int()
  573. DataL = [Data]
  574. Data = [char()] | binary()
  575. Reason = ext_posix() | terminated
  576. Performs a sequence of pread/3 in one operation, which is more
  577. efficient than calling them one at a time. Returns {ok, [Data, ...]}
  578. or {error, Reason}, where each Data, the result of the
  579. correspondingpread, is either a list or a binary depending on the mode
  580. of the file, or eof if the requested position was beyond end of file.
  581. As the position is given as a byte-offset, special caution has to be
  582. taken when working with files where encoding is set to something else
  583. than latin1, as not every byte position will be a valid character
  584. boundary on such a file.
  585. pread(IoDevice, Location, Number) -> {ok, Data} | eof | {error,
  586. Reason}
  587. Types:
  588. IoDevice = io_device()
  589. Location -- see position/2
  590. Number = int()
  591. Data = [char()] | binary()
  592. Reason = ext_posix() | terminated
  593. Combines position/2 and read/2 in one operation, which is more
  594. efficient than calling them one at a time. If IoDevice has been opened
  595. in raw mode, some restrictions apply: Location is only allowed to be
  596. an integer; and the current position of the file is undefined after
  597. the operation.
  598. As the position is given as a byte-offset, special caution has to be
  599. taken when working with files where encoding is set to something else
  600. than latin1, as not every byte position will be a valid character
  601. boundary on such a file.
  602. pwrite(IoDevice, LocBytes) -> ok | {error, {N, Reason}}
  603. Types:
  604. IoDevice = io_device()
  605. LocBytes = [{Location, Bytes}]
  606. Location -- see position/2
  607. Bytes = iodata()
  608. N = int()
  609. Reason = ext_posix() | terminated
  610. Performs a sequence of pwrite/3 in one operation, which is more
  611. efficient than calling them one at a time. Returns ok or {error, {N,
  612. Reason}}, whereN is the number of successful writes that was done
  613. before the failure.
  614. When positioning in a file with other encoding than latin1, caution
  615. must be taken to set the position on a correct character boundary, see
  616. position/2 for details.
  617. pwrite(IoDevice, Location, Bytes) -> ok | {error, Reason}
  618. Types:
  619. IoDevice = io_device()
  620. Location -- see position/2
  621. Bytes = iodata()
  622. Reason = ext_posix() | terminated
  623. Combines position/2 and write/2 in one operation, which is more
  624. efficient than calling them one at a time. If IoDevice has been opened
  625. in raw mode, some restrictions apply: Location is only allowed to be
  626. an integer; and the current position of the file is undefined after
  627. the operation.
  628. When positioning in a file with other encoding than latin1, caution
  629. must be taken to set the position on a correct character boundary, see
  630. position/2 for details.
  631. read(IoDevice, Number) -> {ok, Data} | eof | {error, Reason}
  632. Types:
  633. IoDevice = io_device()
  634. Number = int()
  635. Data = [char()] | binary()
  636. Reason = ext_posix() | terminated
  637. Reads Number bytes/characters from the file referenced byIoDevice. The
  638. functions read/2, pread/3 and read_line/1 are the only ways to read
  639. from a file opened in raw mode (although they work for normally opened
  640. files, too).
  641. For files where encoding is set to something else than latin1, one
  642. character might be represented by more than one byte on the file. The
  643. parameter Number always denotes the number of characters read from the
  644. file, why the position in the file might be moved a lot more than this
  645. number when reading a Unicode file.
  646. Also if encoding is set to something else than latin1, the read/3 call
  647. will fail if the data contains characters larger than 255, why the
  648. `io(3)
  649. <javascript:erlhref('../../../../',%20'stdlib',%20'io.html');>`__
  650. module is to be preferred when reading such a file.
  651. The function returns:
  652. :{ok, Data}: If the file was opened in binary mode, the read bytes are
  653. returned in a binary, otherwise in a list. The list or binary will be
  654. shorter than the number of bytes requested if end of file was reached.
  655. :eof: Returned if Number>0 and end of file was reached before anything
  656. at all could be read.
  657. :{error, Reason}: An error occurred.
  658. Typical error reasons:
  659. :ebadf: The file is not opened for reading.
  660. :{no_translation, unicode, latin1}: The file is was opened with
  661. another encoding than latin1 and the data on the file can not be
  662. translated to the byte-oriented data that this function returns.
  663. read_file(Filename) -> {ok, Binary} | {error, Reason}
  664. Types:
  665. Filename = name()
  666. Binary = binary()
  667. Reason = ext_posix() | terminated | system_limit
  668. Returns {ok, Binary}, where Binary is a binary data object that
  669. contains the contents of Filename, or{error, Reason} if an error
  670. occurs.
  671. Typical error reasons:
  672. :enoent: The file does not exist.
  673. :eacces: Missing permission for reading the file, or for searching one
  674. of the parent directories.
  675. :eisdir: The named file is a directory.
  676. :enotdir: A component of the file name is not a directory. On some
  677. platforms, enoent is returned instead.
  678. :enomem: There is not enough memory for the contents of the file.
  679. read_file_info(Filename) -> {ok, FileInfo} | {error, Reason}
  680. Types:
  681. Filename = name()
  682. FileInfo = #file_info{}
  683. Reason = ext_posix()
  684. Retrieves information about a file. Returns{ok, FileInfo} if
  685. successful, otherwise{error, Reason}. FileInfo is a recordfile_info,
  686. defined in the Kernel include filefile.hrl. Include the following
  687. directive in the module from which the function is called:
  688. .. sourcecode:: erlang
  689. -include_lib("kernel/include/file.hrl").
  690. The record file_info contains the following fields.
  691. :size = int(): Size of file in bytes.
  692. :type = device | directory | regular | other: The type of the file.
  693. :access = read | write | read_write | none: The current system access
  694. to the file.
  695. :atime = time(): The last (local) time the file was read.
  696. :mtime = time(): The last (local) time the file was written.
  697. :ctime = time(): The interpretation of this time field depends on the
  698. operating system. On Unix, it is the last time the file or the inode
  699. was changed. In Windows, it is the create time.
  700. :mode = int(): The file permissions as the sum of the following bit
  701. values:
  702. : 8#00400: read permission: owner
  703. : 8#00200: write permission: owner
  704. : 8#00100: execute permission: owner
  705. : 8#00040: read permission: group
  706. : 8#00020: write permission: group
  707. : 8#00010: execute permission: group
  708. : 8#00004: read permission: other
  709. : 8#00002: write permission: other
  710. : 8#00001: execute permission: other
  711. : 16#800: set user id on execution
  712. : 16#400: set group id on execution
  713. On Unix platforms, other bits than those listed above may be set.
  714. :links = int(): Number of links to the file (this will always be 1 for
  715. file systems which have no concept of links).
  716. :major_device = int(): Identifies the file system where the file is
  717. located. In Windows, the number indicates a drive as follows: 0 means
  718. A:, 1 means B:, and so on.
  719. :minor_device = int(): Only valid for character devices on Unix. In
  720. all other cases, this field is zero.
  721. :inode = int(): Gives the inode number. On non-Unix file systems, this
  722. field will be zero.
  723. :uid = int(): Indicates the owner of the file. Will be zero for non-
  724. Unix file systems.
  725. :gid = int(): Gives the group that the owner of the file belongs to.
  726. Will be zero for non-Unix file systems.
  727. Typical error reasons:
  728. :eacces: Missing search permission for one of the parent directories
  729. of the file.
  730. :enoent: The file does not exist.
  731. :enotdir: A component of the file name is not a directory. On some
  732. platforms, enoent is returned instead.
  733. read_line(IoDevice) -> {ok, Data} | eof | {error, Reason}
  734. Types:
  735. IoDevice = io_device()
  736. Data = [char()] | binary()
  737. Reason = ext_posix() | terminated
  738. Reads a line of bytes/characters from the file referenced byIoDevice.
  739. Lines are defined to be delimited by the linefeed (LF, \n) character,
  740. but any carriage return (CR, \r) followed by a newline is also treated
  741. as a single LF character (the carriage return is silently ignored).
  742. The line is returned including the LF, but excluding any CR
  743. immediately followed by a LF. This behaviour is consistent with the
  744. behaviour of `io:get_line/2 <javascript:erlhref('../../../../',%20'std
  745. lib',%20'io.html#get_line-2');>`__. If end of file is reached without
  746. any LF ending the last line, a line with no trailing LF is returned.
  747. The function can be used on files opened in raw mode. It is however
  748. inefficient to use it on raw files if the file is not opened with the
  749. option {read_ahead, Size} specified, why combining raw and
  750. {read_ahead, Size} is highly recommended when opening a text file for
  751. raw line oriented reading.
  752. If encoding is set to something else than latin1, the read_line/1 call
  753. will fail if the data contains characters larger than 255, why the
  754. `io(3)
  755. <javascript:erlhref('../../../../',%20'stdlib',%20'io.html');>`__
  756. module is to be preferred when reading such a file.
  757. The function returns:
  758. :{ok, Data}: One line from the file is returned, including the
  759. trailing LF, but with CRLF sequences replaced by a single LF (see
  760. above). If the file was opened in binary mode, the read bytes are
  761. returned in a binary, otherwise in a list.
  762. :eof: Returned if end of file was reached before anything at all could
  763. be read.
  764. :{error, Reason}: An error occurred.
  765. Typical error reasons:
  766. :ebadf: The file is not opened for reading.
  767. :{no_translation, unicode, latin1}: The file is was opened with
  768. another encoding than latin1 and the data on the file can not be
  769. translated to the byte-oriented data that this function returns.
  770. read_link(Name) -> {ok, Filename} | {error, Reason}
  771. Types:
  772. Name = name()
  773. Filename = string()
  774. Reason = ext_posix()
  775. This function returns {ok, Filename} if Name refers to a symbolic link
  776. or {error, Reason} otherwise. On platforms that do not support
  777. symbolic links, the return value will be {error,enotsup}.
  778. Typical error reasons:
  779. :einval: Linkname does not refer to a symbolic link.
  780. :enoent: The file does not exist.
  781. :enotsup: Symbolic links are not supported on this platform.
  782. read_link_info(Name) -> {ok, FileInfo} | {error, Reason}
  783. Types:
  784. Name = name()
  785. FileInfo = #file_info{}, see read_file_info/1
  786. Reason = ext_posix()
  787. This function works like read_file_info/1, except that if Name is a
  788. symbolic link, information about the link will be returned in the
  789. file_info record and the type field of the record will be set
  790. tosymlink.
  791. If Name is not a symbolic link, this function returns exactly the same
  792. result as read_file_info/1. On platforms that do not support symbolic
  793. links, this function is always equivalent to read_file_info/1.
  794. rename(Source, Destination) -> ok | {error, Reason}
  795. Types:
  796. Source = Destination = name()
  797. Reason = ext_posix()
  798. Tries to rename the file Source to Destination. It can be used to move
  799. files (and directories) between directories, but it is not sufficient
  800. to specify the destination only. The destination file name must also
  801. be specified. For example, if bar is a normal file andfoo and baz are
  802. directories,rename("foo/bar", "baz") returns an error,
  803. butrename("foo/bar", "baz/bar") succeeds. Returnsok if it is
  804. successful.
  805. Note
  806. Renaming of open files is not allowed on most platforms (see eacces
  807. below).
  808. Typical error reasons:
  809. :eacces: Missing read or write permissions for the parent directories
  810. of Source or Destination. On some platforms, this error is given if
  811. eitherSource or Destination is open.
  812. :eexist: Destination is not an empty directory. On some platforms,
  813. also given when Source andDestination are not of the same type.
  814. :einval: Source is a root directory, or Destination is a sub-directory
  815. of Source.
  816. :eisdir: Destination is a directory, but Source is not.
  817. :enoent: Source does not exist.
  818. :enotdir: Source is a directory, but Destination is not.
  819. :exdev: Source and Destination are on different file systems.
  820. script(Filename) -> {ok, Value} | {error, Reason}
  821. Types:
  822. Filename = name()
  823. Value = term()
  824. Reason = ext_posix() | terminated | system_limit | {Line, Mod, Term}
  825. Line, Mod, Term -- see below
  826. Reads and evaluates Erlang expressions, separated by '.' (or ',', a
  827. sequence of expressions is also an expression), from the file. Returns
  828. one of the following:
  829. :{ok, Value}: The file was read and evaluated. Value is the value of
  830. the last expression.
  831. :{error, atom()}: An error occurred when opening the file or reading
  832. it. See open/2 for a list of typical error codes.
  833. :{error, {Line, Mod, Term}}: An error occurred when interpreting the
  834. Erlang expressions in the file. Use format_error/1 to convert the
  835. three-element tuple to an English description of the error.
  836. script(Filename, Bindings) -> {ok, Value} | {error, Reason}
  837. Types:
  838. Filename = name()
  839. Bindings -- see erl_eval(3)
  840. Value = term()
  841. Reason = ext_posix() | terminated | system_limit | {Line, Mod, Term}
  842. Line, Mod, Term -- see below
  843. The same as script/1 but the variable bindingsBindings are used in the
  844. evaluation. See `erl_eval(3) <javascript:erlhref('../../../../',%20'st
  845. dlib',%20'erl_eval.html');>`__ about variable bindings.
  846. set_cwd(Dir) -> ok | {error,Reason}
  847. Types:
  848. Dir = name()
  849. Reason = ext_posix()
  850. Sets the current working directory of the file server toDir. Returns
  851. ok if successful.
  852. Typical error reasons are:
  853. :enoent: The directory does not exist.
  854. :enotdir: A component of Dir is not a directory. On some platforms,
  855. enoent is returned.
  856. :eacces: Missing permission for the directory or one of its parents.
  857. :badarg: Filename had an improper type, such as tuple.
  858. Warning
  859. In a future release, a bad type for the Filename argument will
  860. probably generate an exception.
  861. sync(IoDevice) -> ok | {error, Reason}
  862. Types:
  863. IoDevice = io_device()
  864. Reason = ext_posix() | terminated
  865. Makes sure that any buffers kept by the operating system (not by the
  866. Erlang runtime system) are written to disk. On some platforms, this
  867. function might have no effect.
  868. Typical error reasons are:
  869. :enospc: Not enough space left to write the file.
  870. truncate(IoDevice) -> ok | {error, Reason}
  871. Types:
  872. IoDevice = io_device()
  873. Reason = ext_posix() | terminated
  874. Truncates the file referenced by IoDevice at the current position.
  875. Returns ok if successful, otherwise {error, Reason}.
  876. write(IoDevice, Bytes) -> ok | {error, Reason}
  877. Types:
  878. IoDevice = io_device()
  879. Bytes = iodata()
  880. Reason = ext_posix() | terminated
  881. Writes Bytes to the file referenced byIoDevice. This function is the
  882. only way to write to a file opened in raw mode (although it works for
  883. normally opened files, too). Returns ok if successful, and{error,
  884. Reason} otherwise.
  885. If the file is opened with encoding set to something else than latin1,
  886. each byte written might result in several bytes actually being written
  887. to the file, as the byte range 0..255 might represent anything between
  888. one and four bytes depending on value and UTF encoding type.
  889. Typical error reasons are:
  890. :ebadf: The file is not opened for writing.
  891. :enospc: There is a no space left on the device.
  892. write_file(Filename, Bytes) -> ok | {error, Reason}
  893. Types:
  894. Filename = name()
  895. Bytes = iodata()
  896. Reason = ext_posix() | terminated | system_limit
  897. Writes the contents of the iodata term Bytes to the file Filename. The
  898. file is created if it does not exist. If it exists, the previous
  899. contents are overwritten. Returns ok, or {error, Reason}.
  900. Typical error reasons are:
  901. :enoent: A component of the file name does not exist.
  902. :enotdir: A component of the file name is not a directory. On some
  903. platforms, enoent is returned instead.
  904. :enospc: There is a no space left on the device.
  905. :eacces: Missing permission for writing the file or searching one of
  906. the parent directories.
  907. :eisdir: The named file is a directory.
  908. write_file(Filename, Bytes, Modes) -> ok | {error, Reason}
  909. Types:
  910. Filename = name()
  911. Bytes = iodata()
  912. Modes = [Mode] -- see open/2
  913. Reason = ext_posix() | terminated | system_limit
  914. Same as write_file/2, but takes a third argumentModes, a list of
  915. possible modes, seeopen/2. The mode flagsbinary and write are
  916. implicit, so they should not be used.
  917. write_file_info(Filename, FileInfo) -> ok | {error, Reason}
  918. Types:
  919. Filename = name()
  920. FileInfo = #file_info{} -- see also read_file_info/1
  921. Reason = ext_posix()
  922. Change file information. Returns ok if successful, otherwise {error,
  923. Reason}. FileInfo is a recordfile_info, defined in the Kernel include
  924. filefile.hrl. Include the following directive in the module from which
  925. the function is called:
  926. .. sourcecode:: erlang
  927. -include_lib("kernel/include/file.hrl").
  928. The following fields are used from the record, if they are given.
  929. :atime = time(): The last (local) time the file was read.
  930. :mtime = time(): The last (local) time the file was written.
  931. :ctime = time(): On Unix, any value give for this field will be
  932. ignored (the "ctime" for the file will be set to the current time). On
  933. Windows, this field is the new creation time to set for the file.
  934. :mode = int(): The file permissions as the sum of the following bit
  935. values:
  936. : 8#00400: read permission: owner
  937. : 8#00200: write permission: owner
  938. : 8#00100: execute permission: owner
  939. : 8#00040: read permission: group
  940. : 8#00020: write permission: group
  941. : 8#00010: execute permission: group
  942. : 8#00004: read permission: other
  943. : 8#00002: write permission: other
  944. : 8#00001: execute permission: other
  945. : 16#800: set user id on execution
  946. : 16#400: set group id on execution
  947. On Unix platforms, other bits than those listed above may be set.
  948. :uid = int(): Indicates the owner of the file. Ignored for non-Unix
  949. file systems.
  950. :gid = int(): Gives the group that the owner of the file belongs to.
  951. Ignored non-Unix file systems.
  952. Typical error reasons:
  953. :eacces: Missing search permission for one of the parent directories
  954. of the file.
  955. :enoent: The file does not exist.
  956. :enotdir: A component of the file name is not a directory. On some
  957. platforms, enoent is returned instead.
  958. POSIX Error Codes
  959. ~~~~~~~~~~~~~~~~~
  960. + eacces - permission denied
  961. + eagain - resource temporarily unavailable
  962. + ebadf - bad file number
  963. + ebusy - file busy
  964. + edquot - disk quota exceeded
  965. + eexist - file already exists
  966. + efault - bad address in system call argument
  967. + efbig - file too large
  968. + eintr - interrupted system call
  969. + einval - invalid argument
  970. + eio - IO error
  971. + eisdir - illegal operation on a directory
  972. + eloop - too many levels of symbolic links
  973. + emfile - too many open files
  974. + emlink - too many links
  975. + enametoolong - file name too long
  976. + enfile - file table overflow
  977. + enodev - no such device
  978. + enoent - no such file or directory
  979. + enomem - not enough memory
  980. + enospc - no space left on device
  981. + enotblk - block device required
  982. + enotdir - not a directory
  983. + enotsup - operation not supported
  984. + enxio - no such device or address
  985. + eperm - not owner
  986. + epipe - broken pipe
  987. + erofs - read-only file system
  988. + espipe - invalid seek
  989. + esrch - no such process
  990. + estale - stale remote file handle
  991. + exdev - cross-domain link
  992. Performance
  993. ~~~~~~~~~~~
  994. Some operating system file operations, for example async/1 or close/1
  995. on a huge file, may block their calling thread for seconds. If this
  996. befalls the emulator main thread, the response time is no longer in
  997. the order of milliseconds, depending on the definition of "soft" in
  998. soft real-time system.
  999. If the device driver thread pool is active, file operations are done
  1000. through those threads instead, so the emulator can go on executing
  1001. Erlang processes. Unfortunately, the time for serving a file operation
  1002. increases due to the extra scheduling required from the operating
  1003. system.
  1004. If the device driver thread pool is disabled or of size 0, large file
  1005. reads and writes are segmented into several smaller, which enables the
  1006. emulator so server other processes during the file operation. This
  1007. gives the same effect as when using the thread pool, but with larger
  1008. overhead. Other file operations, for example sync/1 or close/1 on a
  1009. huge file, still are a problem.
  1010. For increased performance, raw files are recommended. Raw files uses
  1011. the file system of the node's host machine. For normal files (non-
  1012. raw), the file server is used to find the files, and if the node is
  1013. running its file server as slave to another node's, and the other node
  1014. runs on some other host machine, they may have different file systems.
  1015. This is seldom a problem, but you have now been warned.
  1016. A normal file is really a process so it can be used as an IO device
  1017. (see io). Therefore when data is written to a normal file, the sending
  1018. of the data to the file process, copies all data that are not
  1019. binaries. Opening the file in binary mode and writing binaries is
  1020. therefore recommended. If the file is opened on another node, or if
  1021. the file server runs as slave to another node's, also binaries are
  1022. copied.
  1023. Caching data to reduce the number of file operations, or rather the
  1024. number of calls to the file driver, will generally increase
  1025. performance. The following function writes 4 MBytes in 23 seconds when
  1026. tested:
  1027. .. sourcecode:: erlang
  1028. create_file_slow(Name, N) when integer(N), N >= 0 ->
  1029. {ok, FD} = file:open(Name, [raw, write, delayed_write, binary]),
  1030. ok = create_file_slow(FD, 0, N),
  1031. ok = ?FILE_MODULE:close(FD),
  1032. ok.
  1033. create_file_slow(FD, M, M) ->
  1034. ok;
  1035. create_file_slow(FD, M, N) ->
  1036. ok = file:write(FD, <<M:32/unsigned>>),
  1037. create_file_slow(FD, M+1, N).
  1038. The following, functionally equivalent, function collects 1024 entries
  1039. into a list of 128 32-byte binaries before each call tofile:write/2
  1040. and so does the same work in 0.52 seconds, which is 44 times faster.
  1041. .. sourcecode:: erlang
  1042. create_file(Name, N) when integer(N), N >= 0 ->
  1043. {ok, FD} = file:open(Name, [raw, write, delayed_write, binary]),
  1044. ok = create_file(FD, 0, N),
  1045. ok = ?FILE_MODULE:close(FD),
  1046. ok.
  1047. create_file(FD, M, M) ->
  1048. ok;
  1049. create_file(FD, M, N) when M + 1024 =< N ->
  1050. create_file(FD, M, M + 1024, []),
  1051. create_file(FD, M + 1024, N);
  1052. create_file(FD, M, N) ->
  1053. create_file(FD, M, N, []).
  1054. create_file(FD, M, M, R) ->
  1055. ok = file:write(FD, R);
  1056. create_file(FD, M, N0, R) when M + 8 =< N0 ->
  1057. N1 = N0-1, N2 = N0-2, N3 = N0-3, N4 = N0-4,
  1058. N5 = N0-5, N6 = N0-6, N7 = N0-7, N8 = N0-8,
  1059. create_file(FD, M, N8,
  1060. [<<N8:32/unsigned, N7:32/unsigned,
  1061. N6:32/unsigned, N5:32/unsigned,
  1062. N4:32/unsigned, N3:32/unsigned,
  1063. N2:32/unsigned, N1:32/unsigned>> | R]);
  1064. create_file(FD, M, N0, R) ->
  1065. N1 = N0-1,
  1066. create_file(FD, M, N1, [<<N1:32/unsigned>> | R]).
  1067. Note
  1068. Trust only your own benchmarks. If the list length in create_file/2
  1069. above is increased, it will run slightly faster, but consume more
  1070. memory and cause more memory fragmentation. How much this affects your
  1071. application is something that this simple benchmark can not predict.
  1072. If the size of each binary is increased to 64 bytes, it will also run
  1073. slightly faster, but the code will be twice as clumsy. In the current
  1074. implementation are binaries larger than 64 bytes stored in memory
  1075. common to all processes and not copied when sent between processes,
  1076. while these smaller binaries are stored on the process heap and copied
  1077. when sent like any other term.
  1078. So, with a binary size of 68 bytes create_file/2 runs 30 percent
  1079. slower then with 64 bytes, and will cause much more memory
  1080. fragmentation. Note that if the binaries were to be sent between
  1081. processes (for example a non-raw file) the results would probably be
  1082. completely different.
  1083. A raw file is really a port. When writing data to a port, it is
  1084. efficient to write a list of binaries. There is no need to flatten a
  1085. deep list before writing. On Unix hosts, scatter output, which writes
  1086. a set of buffers in one operation, is used when possible. In this way
  1087. file:write(FD, [Bin1, Bin2 | Bin3]) will write the contents of the
  1088. binaries without copying the data at all except for perhaps deep down
  1089. in the operating syste

Large files files are truncated, but you can click here to view the full file