File: file.3

package info (click to toggle)
erlang-manpages 1%3A12.b.3-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,188 kB
  • ctags: 2
  • sloc: makefile: 68; perl: 30; sh: 15
file content (1971 lines) | stat: -rw-r--r-- 49,528 bytes parent folder | download
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
.TH file 3 "kernel  2.12.3" "Ericsson AB" "ERLANG MODULE DEFINITION"
.SH MODULE
file \- File Interface Module
.SH DESCRIPTION
.LP
The module \fIfile\fR provides an interface to the file system\&.
.LP
On operating systems with thread support, it is possible to let file operations be performed in threads of their own, allowing other Erlang processes to continue executing in parallel with the file operations\&. See the command line flag \fI+A\fR in erl(1)\&.

.SH DATA TYPES

.nf
iodata() = iolist() | binary()
  iolist() = [char() | binary() | iolist()]

io_device()
  as returned by file:open/2, a process handling IO protocols

name() = string() | atom() | DeepList
  DeepList = [char() | atom() | DeepList]

posix()
  an atom which is named from the Posix error codes used in
  Unix, and in the runtime libraries of most C compilers

ext_posix() = posix() | badarg

time() = {{Year, Month, Day}, {Hour, Minute, Second}}
  Year = Month = Day = Hour = Minute = Second = int()
  Must denote a valid date and time
.fi
.SH EXPORTS
.LP
.B
change_group(Filename, Gid) -> ok | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
Gid = int()
.br
Reason = ext_posix()
.br
.RE
.RS
.LP
Changes group of a file\&. See write_file_info/2\&.
.RE
.LP
.B
change_owner(Filename, Uid) -> ok | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
Uid = int()
.br
Reason = ext_posix()
.br
.RE
.RS
.LP
Changes owner of a file\&. See write_file_info/2\&.
.RE
.LP
.B
change_owner(Filename, Uid, Gid) -> ok | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
Uid = int()
.br
Gid = int()
.br
Reason = ext_posix()
.br
.RE
.RS
.LP
Changes owner and group of a file\&. See write_file_info/2\&.
.RE
.LP
.B
change_time(Filename, Mtime) -> ok | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
Mtime = time()
.br
Reason = ext_posix()
.br
.RE
.RS
.LP
Changes the modification and access times of a file\&. See write_file_info/2\&.
.RE
.LP
.B
change_time(Filename, Mtime, Atime) -> ok | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
Mtime = Atime = time()
.br
Reason = ext_posix()
.br
.RE
.RS
.LP
Changes the modification and last access times of a file\&. See write_file_info/2\&.
.RE
.LP
.B
close(IoDevice) -> ok | {error, Reason}
.br
.RS
.TP
Types
IoDevice = io_device()
.br
Reason = ext_posix() | terminated
.br
.RE
.RS
.LP
Closes the file referenced by \fIIoDevice\fR\&. It mostly returns \fIok\fR, expect for some severe errors such as out of memory\&.
.LP
Note that if the option \fIdelayed_write\fR was used when opening the file, \fIclose/1\fR might return an old write error and not even try to close the file\&. See open/2\&.
.RE
.LP
.B
consult(Filename) -> {ok, Terms} | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
Terms = [term()]
.br
Reason = ext_posix() | terminated | system_limit | {Line, Mod, Term}
.br
Line, Mod, Term -- see below
.br
.RE
.RS
.LP
Reads Erlang terms, separated by \&'\&.\&', from \fIFilename\fR\&. Returns one of the following:
.RS 2
.TP 4
.B
\fI{ok, Terms}\fR:
The file was successfully read\&.
.TP 4
.B
\fI{error, atom()}\fR:
An error occurred when opening the file or reading it\&. See open/2 for a list of typical error codes\&.
.TP 4
.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
Example:

.nf
f\&.txt:  {person, "kalle", 25}\&.
        {person, "pelle", 30}\&.
.fi

.nf
1> file:consult("f\&.txt")\&.

{ok,[{person,"kalle",25},{person,"pelle",30}]}
.fi
.RE
.LP
.B
copy(Source, Destination) ->
.br
.B
copy(Source, Destination, ByteCount) -> {ok, BytesCopied} | {error, Reason}
.br
.RS
.TP
Types
Source = Destination = io_device() | Filename | {Filename, Modes}
.br
Filename = name()
.br
Modes = [Mode] -- see open/2
.br
ByteCount = int() >= 0 | infinity
.br
BytesCopied = int()
.br
.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 e\&.g\&. \fIopen/2\fR\&. \fIByteCount\fR defaults \fIinfinity\fR, denoting an infinite number of bytes\&.
.LP
The argument \fIModes\fR is a list of possible modes, see open/2, and defaults to []\&.
.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 actually was copied, which may 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
.B
del_dir(Dir) -> ok | {error, Reason}
.br
.RS
.TP
Types
Dir = name()
.br
Reason = ext_posix()
.br
.RE
.RS
.LP
Tries to delete the directory \fIDir\fR\&. The directory must be empty before it can be deleted\&. Returns \fIok\fR if successful\&.
.LP
Typical error reasons are:
.RS 2
.TP 4
.B
\fIeacces\fR:
Missing search or write permissions for the parent directories of \fIDir\fR\&.
.TP 4
.B
\fIeexist\fR:
The directory is not empty\&.
.TP 4
.B
\fIenoent\fR:
The directory does not exist\&.
.TP 4
.B
\fIenotdir\fR:
A component of \fIDir\fR is not a directory\&. On some platforms, \fIenoent\fR is returned instead\&.
.TP 4
.B
\fIeinval\fR:
Attempt to delete the current directory\&. On some platforms, \fIeacces\fR is returned instead\&.
.RE
.RE
.LP
.B
delete(Filename) -> ok | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
Reason = ext_posix()
.br
.RE
.RS
.LP
Tries to delete the file \fIFilename\fR\&. Returns \fIok\fR if successful\&.
.LP
Typical error reasons are:
.RS 2
.TP 4
.B
\fIenoent\fR:
The file does not exist\&.
.TP 4
.B
\fIeacces\fR:
Missing permission for the file or one of its parents\&.
.TP 4
.B
\fIeperm\fR:
The file is a directory and the user is not super-user\&.
.TP 4
.B
\fIenotdir\fR:
A component of the file name is not a directory\&. On some platforms, \fIenoent\fR is returned instead\&.
.TP 4
.B
\fIeinval\fR:
\fIFilename\fR had an improper type, such as tuple\&.
.RE
.SS Warning:
.LP
In a future release, a bad type for the \fIFilename\fR argument will probably generate an exception\&.
.LP


.RE
.LP
.B
eval(Filename) -> ok | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
Reason = ext_posix() | terminated | system_limit | {Line, Mod, Term}
.br
Line, Mod, Term -- see below
.br
.RE
.RS
.LP
Reads and evaluates Erlang expressions, separated by \&'\&.\&' (or \&',\&', a sequence of expressions is also an expression), from \fIFilename\fR\&. The actual result of the evaluation is not returned; any expression sequence in the file must be there for its side effect\&. Returns one of the following:
.RS 2
.TP 4
.B
\fIok\fR:
The file was read and evaluated\&.
.TP 4
.B
\fI{error, atom()}\fR:
An error occurred when opening the file or reading it\&. See \fIopen/2\fR for a list of typical error codes\&.
.TP 4
.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
.RE
.LP
.B
eval(Filename, Bindings) -> ok | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
Bindings -- see erl_eval(3)
.br
Reason = ext_posix() | terminated | system_limit | {Line, Mod, Term}
.br
Line, Mod, Term -- see eval/1
.br
.RE
.RS
.LP
The same as \fIeval/1\fR but the variable bindings \fIBindings\fR are used in the evaluation\&. See erl_eval(3) about variable bindings\&.
.RE
.LP
.B
file_info(Filename) -> {ok, FileInfo} | {error, Reason}
.br
.RS
.LP
This function is obsolete\&. Use \fIread_file_info/1\fR instead\&.
.RE
.LP
.B
format_error(Reason) -> Chars
.br
.RS
.TP
Types
Reason = atom() | {Line, Mod, Term}
.br
Line, Mod, Term -- see eval/1
.br
Chars = [char() | Chars]
.br
.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
.B
get_cwd() -> {ok, Dir} | {error, Reason}
.br
.RS
.TP
Types
Dir = string()
.br
Reason = posix()
.br
.RE
.RS
.LP
Returns \fI{ok, Dir}\fR, where \fIDir\fR is the current working directory of the file server\&.
.SS Note:
.LP
In rare circumstances, this function can fail on Unix\&. It may happen if read permission does not exist for the parent directories of the current directory\&.

.LP
Typical error reasons are:
.RS 2
.TP 4
.B
\fIeacces\fR:
Missing read permission for one of the parents of the current directory\&.
.RE
.RE
.LP
.B
get_cwd(Drive) -> {ok, Dir} | {error, Reason}
.br
.RS
.TP
Types
Drive = string() -- see below
.br
Dir = string()
.br
Reason = ext_posix()
.br
.RE
.RS
.LP
\fIDrive\fR should be of the form "\fILetter\fR\fI:\fR", for example "c:"\&. Returns \fI{ok, Dir}\fR or \fI{error, Reason}\fR, where \fIDir\fR is the current working directory of the drive specified\&.
.LP
This function returns \fI{error, enotsup}\fR on platforms which have no concept of current drive (Unix, for example)\&.
.LP
Typical error reasons are:
.RS 2
.TP 4
.B
\fIenotsup\fR:
The operating system have no concept of drives\&.
.TP 4
.B
\fIeacces\fR:
The drive does not exist\&.
.TP 4
.B
\fIeinval\fR:
The format of \fIDrive\fR is invalid\&.
.RE
.RE
.LP
.B
list_dir(Dir) -> {ok, Filenames} | {error, Reason}
.br
.RS
.TP
Types
Dir = name()
.br
Filenames = [Filename]
.br
Filename = string()
.br
Reason = ext_posix()
.br
.RE
.RS
.LP
Lists all the files in a directory\&. Returns \fI{ok, Filenames}\fR if successful\&. Otherwise, it returns \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 are:
.RS 2
.TP 4
.B
\fIeacces\fR:
Missing search or write permissions for \fIDir\fR or one of its parent directories\&.
.TP 4
.B
\fIenoent\fR:
The directory does not exist\&.
.RE
.RE
.LP
.B
make_dir(Dir) -> ok | {error, Reason}
.br
.RS
.TP
Types
Dir = name()
.br
Reason = ext_posix()
.br
.RE
.RS
.LP
Tries to create the directory \fIDir\fR\&. Missing parent directories are \fInot\fR created\&. Returns \fIok\fR if successful\&.
.LP
Typical error reasons are:
.RS 2
.TP 4
.B
\fIeacces\fR:
Missing search or write permissions for the parent directories of \fIDir\fR\&.
.TP 4
.B
\fIeexist\fR:
There is already a file or directory named \fIDir\fR\&.
.TP 4
.B
\fIenoent\fR:
A component of \fIDir\fR does not exist\&.
.TP 4
.B
\fIenospc\fR:
There is a no space left on the device\&.
.TP 4
.B
\fIenotdir\fR:
A component of \fIDir\fR is not a directory\&. On some platforms, \fIenoent\fR is returned instead\&.
.RE
.RE
.LP
.B
make_link(Existing, New) -> ok | {error, Reason}
.br
.RS
.TP
Types
Existing = New = name()
.br
Reason = ext_posix()
.br
.RE
.RS
.LP
Makes a hard link from \fIExisting\fR to \fINew\fR, on platforms that support links (Unix)\&. This function returns \fIok\fR if the link was successfully created, or \fI{error, Reason}\fR\&. On platforms that do not support links, \fI{error, enotsup}\fR is returned\&.
.LP
Typical error reasons:
.RS 2
.TP 4
.B
\fIeacces\fR:
Missing read or write permissions for the parent directories of \fIExisting\fR or \fINew\fR\&.
.TP 4
.B
\fIeexist\fR:
\fINew\fR already exists\&.
.TP 4
.B
\fIenotsup\fR:
Hard links are not supported on this platform\&.
.RE
.RE
.LP
.B
make_symlink(Name1, Name2) -> ok | {error, Reason}
.br
.RS
.TP
Types
Name1 = Name2 = name()
.br
Reason = ext_posix()
.br
.RE
.RS
.LP
This function creates a symbolic link \fIName2\fR to the file or directory \fIName1\fR, on platforms that support symbolic links (most Unix systems)\&. \fIName1\fR need not exist\&. This function returns \fIok\fR if the link was successfully created, or \fI{error, Reason}\fR\&. On platforms that do not support symbolic links, \fI{error, enotsup}\fR is returned\&.
.LP
Typical error reasons:
.RS 2
.TP 4
.B
\fIeacces\fR:
Missing read or write permissions for the parent directories of \fIName1\fR or \fIName2\fR\&.
.TP 4
.B
\fIeexist\fR:
\fIName2\fR already exists\&.
.TP 4
.B
\fIenotsup\fR:
Symbolic links are not supported on this platform\&.
.RE
.RE
.LP
.B
open(Filename, Modes) -> {ok, IoDevice} | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
Modes = [Mode]
.br
Mode = read | write | append | raw | binary | {delayed_write, Size, Delay} | delayed_write | {read_ahead, Size} | read_ahead | compressed
.br
Size = Delay = int()
.br
IoDevice = io_device()
.br
Reason = ext_posix() | system_limit
.br
.RE
.RS
.LP
Opens the file \fIFilename\fR in the mode determined by \fIModes\fR, which may contain one or more of the following items:
.RS 2
.TP 4
.B
\fIread\fR:
The file, which must exist, is opened for reading\&.
.TP 4
.B
\fIwrite\fR:
The file is opened for writing\&. It is created if it does not exist\&. If the file exists, and if \fIwrite\fR is not combined with \fIread\fR, the file will be truncated\&.
.TP 4
.B
\fIappend\fR:
The file will be opened for writing, and it will be created if it does not exist\&. Every write operation to a file opened with \fIappend\fR will take place at the end of the file\&.
.TP 4
.B
\fIraw\fR:
The \fIraw\fR option allows faster access to a file, because no Erlang process is needed to handle the file\&. However, a file opened in this way has the following limitations:
.RS 4
.LP

.RS 2
.TP 2
*
The functions in the \fIio\fR module cannot be used, because they can only talk to an Erlang process\&. Instead, use the \fIread/2\fR and \fIwrite/2\fR functions\&.
.TP 2
*
Only the Erlang process which opened the file can use it\&.
.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)\&.
.RE
.RE
.TP 4
.B
\fIbinary\fR:
This option can only be used if the \fIraw\fR option is specified as well\&. When specified, read operations on the file using the \fIread/2\fR function will return binaries rather than lists\&.
.TP 4
.B
\fI{delayed_write, Size, Delay}\fR:
If this option is used, the data in subsequent \fIwrite/2\fR calls is buffered until there are at least \fISize\fR bytes 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 4
.LP

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

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

.LP
For example, when \fIdelayed_write\fR is used, after a number of \fIwrite/2\fR calls, \fIclose/1\fR might return \fI{error, enospc}\fR because there was not enough space on the disc for previously written data, and \fIclose/1\fR should probably be called again since the file is still open\&.
.RE
.TP 4
.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 KBytes, 2 seconds)
.TP 4
.B
\fI{read_ahead, Size}\fR:
This option activates read data buffering\&. If \fIread/2\fR calls are for significantly less than \fISize\fR bytes, read operations towards 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 since the number of operating system calls is reduced\&.
.RS 4
.LP

.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 4
.B
\fIread_ahead\fR:
The same as \fI{read_ahead, Size}\fR with a reasonable default value for \fISize\fR\&. (Roughly some 64 KBytes)
.TP 4
.B
\fIcompressed\fR:
Makes it possible to read and write gzip compressed files\&. Note that the file size obtained with \fIread_file_info/1\fR will most probably not match the number of bytes that can be read from a compressed file\&.
.RE
.LP
Returns:
.RS 2
.TP 4
.B
\fI{ok, IoDevice}\fR:
The file has been opened in the requested mode\&. \fIIoDevice\fR is a reference to the file\&.
.TP 4
.B
\fI{error, Reason}\fR:
The file could not be opened\&.
.RE
.LP
\fIIoDevice\fR is really the pid of the process which handles the file\&. This process is linked to the process which originally opened the file\&. If any process to which the \fIIoDevice\fR is linked terminates, the file will be closed and the process itself will be terminated\&. An \fIIoDevice\fR returned from this call can be used as an argument to the IO functions (see io(3))\&.
.SS Note:
.LP
In previous versions of \fIfile\fR, modes were given 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 should not 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 4
.B
\fIenoent\fR:
The file does not exist\&.
.TP 4
.B
\fIeacces\fR:
Missing permission for reading the file or searching one of the parent directories\&.
.TP 4
.B
\fIeisdir\fR:
The named file is not a regular file\&. It may be a directory, a fifo, or a device\&.
.TP 4
.B
\fIenotdir\fR:
A component of the file name is not a directory\&. On some platforms, \fIenoent\fR is returned instead\&.
.TP 4
.B
\fIenospc\fR:
There is a no space left on the device (if \fIwrite\fR access was specified)\&.
.RE
.RE
.LP
.B
path_consult(Path, Filename) -> {ok, Terms, FullName} | {error, Reason}
.br
.RS
.TP
Types
Path = [Dir]
.br
Dir = name()
.br
Filename = name()
.br
Terms = [term()]
.br
FullName = string()
.br
Reason = ext_posix() | terminated | system_limit | {Line, Mod, Term}
.br
Line, Mod, Term -- see below
.br
.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\&. Returns one of the following:
.RS 2
.TP 4
.B
\fI{ok, Terms, FullName}\fR:
The file was successfully read\&. \fIFullName\fR is the full name of the file\&.
.TP 4
.B
\fI{error, enoent}\fR:
The file could not be found in any of the directories in \fIPath\fR\&.
.TP 4
.B
\fI{error, atom()}\fR:
An error occurred when opening the file or reading it\&. See open/2 for a list of typical error codes\&.
.TP 4
.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
.RE
.LP
.B
path_eval(Path, Filename) -> {ok, FullName} | {error, Reason}
.br
.RS
.TP
Types
Path = [Dir]
.br
Dir = name()
.br
Filename = name()
.br
FullName = string()
.br
Reason = ext_posix() | terminated | system_limit | {Line, Mod, Term}
.br
Line, Mod, Term -- see below
.br
.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 file name, \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 actual result of evaluation is not returned; any expression sequence in the file must be there for its side effect\&. Returns one of the following:
.RS 2
.TP 4
.B
\fI{ok, FullName}\fR:
The file was read and evaluated\&. \fIFullName\fR is the full name of the file\&.
.TP 4
.B
\fI{error, enoent}\fR:
The file could not be found in any of the directories in \fIPath\fR\&.
.TP 4
.B
\fI{error, atom()}\fR:
An error occurred when opening the file or reading it\&. See open/2 for a list of typical error codes\&.
.TP 4
.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
.RE
.LP
.B
path_open(Path, Filename, Modes) -> {ok, IoDevice, FullName} | {error, Reason}
.br
.RS
.TP
Types
Path = [Dir]
.br
Dir = name()
.br
Filename = name()
.br
Modes = [Mode] -- see open/2
.br
IoDevice = io_device()
.br
FullName = string()
.br
Reason = ext_posix() | system_limit
.br
.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 file name, \fIPath\fR is ignored\&. Then opens the file in the mode determined by \fIModes\fR\&. Returns one of the following:
.RS 2
.TP 4
.B
\fI{ok, IoDevice, FullName}\fR:
The file has been opened in the requested mode\&. \fIIoDevice\fR is a reference to the file and \fIFullName\fR is the full name of the file\&.
.TP 4
.B
\fI{error, enoent}\fR:
The file could not be found in any of the directories in \fIPath\fR\&.
.TP 4
.B
\fI{error, atom()}\fR:
The file could not be opened\&.
.RE
.RE
.LP
.B
path_script(Path, Filename) -> {ok, Value, FullName} | {error, Reason}
.br
.RS
.TP
Types
Path = [Dir]
.br
Dir = name()
.br
Filename = name()
.br
Value = term()
.br
FullName = string()
.br
Reason = ext_posix() | terminated | system_limit | {Line, Mod, Term}
.br
Line, Mod, Term -- see below
.br
.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 file name, \fIPath\fR is ignored\&. Then reads and evaluates Erlang expressions, separated by \&'\&.\&' (or \&',\&', a sequence of expressions is also an expression), from the file\&. Returns one of the following:
.RS 2
.TP 4
.B
\fI{ok, Value, FullName}\fR:
The file was read and evaluated\&. \fIFullName\fR is the full name of the file and \fIValue\fR the value of the last expression\&.
.TP 4
.B
\fI{error, enoent}\fR:
The file could not be found in any of the directories in \fIPath\fR\&.
.TP 4
.B
\fI{error, atom()}\fR:
An error occurred when opening the file or reading it\&. See open/2 for a list of typical error codes\&.
.TP 4
.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
.RE
.LP
.B
path_script(Path, Filename, Bindings) -> {ok, Value, FullName} | {error, Reason}
.br
.RS
.TP
Types
Path = [Dir]
.br
Dir = name()
.br
Filename = name()
.br
Bindings -- see erl_eval(3)
.br
Value = term()
.br
FullName = string()
.br
Reason = posix() | terminated | system_limit | {Line, Mod, Term}
.br
Line, Mod, Term -- see path_script/2
.br
.RE
.RS
.LP
The same as \fIpath_script/2\fR but the variable bindings \fIBindings\fR are used in the evaluation\&. See erl_eval(3) about variable bindings\&.
.RE
.LP
.B
pid2name(Pid) -> string() | undefined
.br
.RS
.TP
Types
Pid = pid()
.br
.RE
.RS
.LP
If \fIPid\fR is an IO device, that is, a pid returned from \fIopen/2\fR, this function returns the filename, or rather:
.RS 2
.TP 4
.B
\fI{ok, Filename}\fR:
If this node\&'s file server is not a slave, the file was opened by this node\&'s file server, (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 4
.B
\fIundefined\fR:
In all other cases\&.
.RE
.SS Warning:
.LP
This function is intended for debugging only\&.

.RE
.LP
.B
position(IoDevice, Location) -> {ok, NewPosition} | {error, Reason}
.br
.RS
.TP
Types
IoDevice = io_device()
.br
Location = Offset | {bof, Offset} | {cur, Offset} | {eof, Offset} | bof | cur | eof
.br
Offset = int()
.br
NewPosition = int()
.br
Reason = ext_posix() | terminated
.br
.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 4
.B
\fIOffset\fR:
The same as \fI{bof, Offset}\fR\&.
.TP 4
.B
\fI{bof, Offset}\fR:
Absolute offset\&.
.TP 4
.B
\fI{cur, Offset}\fR:
Offset from the current position\&.
.TP 4
.B
\fI{eof, Offset}\fR:
Offset from the end of file\&.
.TP 4
.B
\fIbof | cur | eof\fR:
The same as above with \fIOffset\fR 0\&.
.RE
.LP
Typical error reasons are:
.RS 2
.TP 4
.B
\fIeinval\fR:
Either \fILocation\fR was illegal, or it evaluated to a negative offset in the file\&. Note that if the resulting position is a negative value, the result is an error, and after the call the file position is undefined\&.
.RE
.RE
.LP
.B
pread(IoDevice, LocNums) -> {ok, DataL} | eof | {error, Reason}
.br
.RS
.TP
Types
IoDevice = io_device()
.br
LocNums = [{Location, Number}]
.br
Location -- see position/2
.br
Number = int()
.br
DataL = [Data]
.br
Data = [char()] | binary()
.br
Reason = ext_posix() | terminated
.br
.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 was beyond end of file\&.
.RE
.LP
.B
pread(IoDevice, Location, Number) -> {ok, Data} | eof | {error, Reason}
.br
.RS
.TP
Types
IoDevice = io_device()
.br
Location -- see position/2
.br
Number = int()
.br
Data = [char()] | binary()
.br
Reason = ext_posix() | terminated
.br
.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\&. If \fIIoDevice\fR has been opened in raw mode, some restrictions apply: \fILocation\fR is only allowed to be an integer; and the current position of the file is undefined after the operation\&.
.RE
.LP
.B
pwrite(IoDevice, LocBytes) -> ok | {error, {N, Reason}}
.br
.RS
.TP
Types
IoDevice = io_device()
.br
LocBytes = [{Location, Bytes}]
.br
Location -- see position/2
.br
Bytes = iodata()
.br
N = int()
.br
Reason = ext_posix() | terminated
.br
.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 that was done before the failure\&.
.RE
.LP
.B
pwrite(IoDevice, Location, Bytes) -> ok | {error, Reason}
.br
.RS
.TP
Types
IoDevice = io_device()
.br
Location -- see position/2
.br
Bytes = iodata()
.br
Reason = ext_posix() | terminated
.br
.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\&. If \fIIoDevice\fR has been opened in raw mode, some restrictions apply: \fILocation\fR is only allowed to be an integer; and the current position of the file is undefined after the operation\&.
.RE
.LP
.B
read(IoDevice, Number) -> {ok, Data} | eof | {error, Reason}
.br
.RS
.TP
Types
IoDevice = io_device()
.br
Number = int()
.br
Data = [char()] | binary()
.br
Reason = ext_posix() | terminated
.br
.RE
.RS
.LP
Reads \fINumber\fR bytes from the file referenced by \fIIoDevice\fR\&. This function is the only way to read from a file opened in raw mode (although it works for normally opened files, too)\&. Returns:
.RS 2
.TP 4
.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 will be shorter than the number of bytes requested if end of file was reached\&.
.TP 4
.B
\fIeof\fR:
Returned if \fINumber>0\fR and end of file was reached before anything at all could be read\&.
.TP 4
.B
\fI{error, Reason}\fR:
An error occurred\&.
.RE
.LP
Typical error reasons:
.RS 2
.TP 4
.B
\fIebadf\fR:
The file is not opened for reading\&.
.RE
.RE
.LP
.B
read_file(Filename) -> {ok, Binary} | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
Binary = binary()
.br
Reason = ext_posix() | terminated | system_limit
.br
.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 4
.B
\fIenoent\fR:
The file does not exist\&.
.TP 4
.B
\fIeacces\fR:
Missing permission for reading the file, or for searching one of the parent directories\&.
.TP 4
.B
\fIeisdir\fR:
The named file is a directory\&.
.TP 4
.B
\fIenotdir\fR:
A component of the file name is not a directory\&. On some platforms, \fIenoent\fR is returned instead\&.
.TP 4
.B
\fIenomem\fR:
There is not enough memory for the contents of the file\&.
.RE
.RE
.LP
.B
read_file_info(Filename) -> {ok, FileInfo} | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
FileInfo = #file_info{}
.br
Reason = ext_posix()
.br
.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:

.nf
-include_lib("kernel/include/file\&.hrl")\&.
.fi
.LP
The record \fIfile_info\fR contains the following fields\&.
.RS 2
.TP 4
.B
\fIsize = int()\fR:
Size of file in bytes\&.
.TP 4
.B
\fItype = device | directory | regular | other\fR:
The type of the file\&.
.TP 4
.B
\fIaccess = read | write | read_write | none\fR:
The current system access to the file\&.
.TP 4
.B
\fIatime = time()\fR:
The last (local) time the file was read\&.
.TP 4
.B
\fImtime = time()\fR:
The last (local) time the file was written\&.
.TP 4
.B
\fIctime = time()\fR:
The interpretation of this time field depends on the operating system\&. On Unix, it is the last time the file or the inode was changed\&. In Windows, it is the create time\&.
.TP 4
.B
\fImode = int()\fR:
The file permissions as the sum of the following bit values:
.RS 4
.LP

.RS 2
.TP 4
.B
8#00400:
read permission: owner
.TP 4
.B
8#00200:
write permission: owner
.TP 4
.B
8#00100:
execute permission: owner
.TP 4
.B
8#00040:
read permission: group
.TP 4
.B
8#00020:
write permission: group
.TP 4
.B
8#00010:
execute permission: group
.TP 4
.B
8#00004:
read permission: other
.TP 4
.B
8#00002:
write permission: other
.TP 4
.B
8#00001:
execute permission: other
.TP 4
.B
16#800:
set user id on execution
.TP 4
.B
16#400:
set group id on execution
.RE
.LP

.LP
On Unix platforms, other bits than those listed above may be set\&.
.RE
.TP 4
.B
\fIlinks = int()\fR:
Number of links to the file (this will always be 1 for file systems which have no concept of links)\&.
.TP 4
.B
\fImajor_device = int()\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 4
.B
\fIminor_device = int()\fR:
Only valid for character devices on Unix\&. In all other cases, this field is zero\&.
.TP 4
.B
\fIinode = int()\fR:
Gives the \fIinode\fR number\&. On non-Unix file systems, this field will be zero\&.
.TP 4
.B
\fIuid = int()\fR:
Indicates the owner of the file\&. Will be zero for non-Unix file systems\&.
.TP 4
.B
\fIgid = int()\fR:
Gives the group that the owner of the file belongs to\&. Will be zero for non-Unix file systems\&.
.RE
.LP
Typical error reasons:
.RS 2
.TP 4
.B
\fIeacces\fR:
Missing search permission for one of the parent directories of the file\&.
.TP 4
.B
\fIenoent\fR:
The file does not exist\&.
.TP 4
.B
\fIenotdir\fR:
A component of the file name is not a directory\&. On some platforms, \fIenoent\fR is returned instead\&.
.RE
.RE
.LP
.B
read_link(Name) -> {ok, Filename} | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
Filename = string()
.br
Reason = ext_posix()
.br
.RE
.RS
.LP
This function 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 will be \fI{error, enotsup}\fR\&.
.LP
Typical error reasons:
.RS 2
.TP 4
.B
\fIeinval\fR:
\fILinkname\fR does not refer to a symbolic link\&.
.TP 4
.B
\fIenoent\fR:
The file does not exist\&.
.TP 4
.B
\fIenotsup\fR:
Symbolic links are not supported on this platform\&.
.RE
.RE
.LP
.B
read_link_info(Name) -> {ok, FileInfo} | {error, Reason}
.br
.RS
.TP
Types
Name = name()
.br
FileInfo = #file_info{}, see read_file_info/1
.br
Reason = ext_posix()
.br
.RE
.RS
.LP
This function works like \fIread_file_info/1\fR, except that if \fIName\fR is a symbolic link, information about the link will be returned in the \fIfile_info\fR record and the \fItype\fR field of the record will be set to \fIsymlink\fR\&.
.LP
If \fIName\fR is not a symbolic link, this function returns exactly 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
.B
rename(Source, Destination) -> ok | {error, Reason}
.br
.RS
.TP
Types
Source = Destination = name()
.br
Reason = ext_posix()
.br
.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 file name 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\&.
.SS Note:
.LP
Renaming of open files is not allowed on most platforms (see \fIeacces\fR below)\&.

.LP
Typical error reasons:
.RS 2
.TP 4
.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 4
.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 4
.B
\fIeinval\fR:
\fISource\fR is a root directory, or \fIDestination\fR is a sub-directory of \fISource\fR\&.
.TP 4
.B
\fIeisdir\fR:
\fIDestination\fR is a directory, but \fISource\fR is not\&.
.TP 4
.B
\fIenoent\fR:
\fISource\fR does not exist\&.
.TP 4
.B
\fIenotdir\fR:
\fISource\fR is a directory, but \fIDestination\fR is not\&.
.TP 4
.B
\fIexdev\fR:
\fISource\fR and \fIDestination\fR are on different file systems\&.
.RE
.RE
.LP
.B
script(Filename) -> {ok, Value} | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
Value = term()
.br
Reason = ext_posix() | terminated | system_limit | {Line, Mod, Term}
.br
Line, Mod, Term -- see below
.br
.RE
.RS
.LP
Reads and evaluates Erlang expressions, separated by \&'\&.\&' (or \&',\&', a sequence of expressions is also an expression), from the file\&. Returns one of the following:
.RS 2
.TP 4
.B
\fI{ok, Value}\fR:
The file was read and evaluated\&. \fIValue\fR is the value of the last expression\&.
.TP 4
.B
\fI{error, atom()}\fR:
An error occurred when opening the file or reading it\&. See open/2 for a list of typical error codes\&.
.TP 4
.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
.RE
.LP
.B
script(Filename, Bindings) -> {ok, Value} | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
Bindings -- see erl_eval(3)
.br
Value = term()
.br
Reason = ext_posix() | terminated | system_limit | {Line, Mod, Term}
.br
Line, Mod, Term -- see below
.br
.RE
.RS
.LP
The same as \fIscript/1\fR but the variable bindings \fIBindings\fR are used in the evaluation\&. See erl_eval(3) about variable bindings\&.
.RE
.LP
.B
set_cwd(Dir) -> ok | {error,Reason}
.br
.RS
.TP
Types
Dir = name()
.br
Reason = ext_posix()
.br
.RE
.RS
.LP
Sets the current working directory of the file server to \fIDir\fR\&. Returns \fIok\fR if successful\&.
.LP
Typical error reasons are:
.RS 2
.TP 4
.B
\fIenoent\fR:
The directory does not exist\&.
.TP 4
.B
\fIenotdir\fR:
A component of \fIDir\fR is not a directory\&. On some platforms, \fIenoent\fR is returned\&.
.TP 4
.B
\fIeacces\fR:
Missing permission for the directory or one of its parents\&.
.TP 4
.B
\fIbadarg\fR:
\fIFilename\fR had an improper type, such as tuple\&.
.RE
.SS Warning:
.LP
In a future release, a bad type for the \fIFilename\fR argument will probably generate an exception\&.
.LP


.RE
.LP
.B
sync(IoDevice) -> ok | {error, Reason}
.br
.RS
.TP
Types
IoDevice = io_device()
.br
Reason = ext_posix() | terminated
.br
.RE
.RS
.LP
Makes sure that any buffers kept by the operating system (not by the Erlang runtime system) are written to disk\&. On some platforms, this function might have no effect\&.
.LP
Typical error reasons are:
.RS 2
.TP 4
.B
\fIenospc\fR:
Not enough space left to write the file\&.
.RE
.RE
.LP
.B
truncate(IoDevice) -> ok | {error, Reason}
.br
.RS
.TP
Types
IoDevice = io_device()
.br
Reason = ext_posix() | terminated
.br
.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
.B
write(IoDevice, Bytes) -> ok | {error, Reason}
.br
.RS
.TP
Types
IoDevice = io_device()
.br
Bytes = iodata()
.br
Reason = ext_posix() | terminated
.br
.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 raw mode (although it works for normally opened files, too)\&. Returns \fIok\fR if successful, and \fI{error, Reason}\fR otherwise\&.
.LP
Typical error reasons are:
.RS 2
.TP 4
.B
\fIebadf\fR:
The file is not opened for writing\&.
.TP 4
.B
\fIenospc\fR:
There is a no space left on the device\&.
.RE
.RE
.LP
.B
write_file(Filename, Bytes) -> ok | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
Bytes = iodata()
.br
Reason = ext_posix() | terminated | system_limit
.br
.RE
.RS
.LP
Writes the contents of the iodata term \fIBytes\fR to the file \fIFilename\fR\&. The file is created if it does not exist\&. If it exists, the previous contents are overwritten\&. Returns \fIok\fR, or \fI{error, Reason}\fR\&.
.LP
Typical error reasons are:
.RS 2
.TP 4
.B
\fIenoent\fR:
A component of the file name does not exist\&.
.TP 4
.B
\fIenotdir\fR:
A component of the file name is not a directory\&. On some platforms, \fIenoent\fR is returned instead\&.
.TP 4
.B
\fIenospc\fR:
There is a no space left on the device\&.
.TP 4
.B
\fIeacces\fR:
Missing permission for writing the file or searching one of the parent directories\&.
.TP 4
.B
\fIeisdir\fR:
The named file is a directory\&.
.RE
.RE
.LP
.B
write_file(Filename, Binary, Bytes) -> ok | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
Bytes = iodata()
.br
Modes = [Mode] -- see open/2
.br
Reason = ext_posix() | terminated | system_limit
.br
.RE
.RS
.LP
Same as \fIwrite_file/2\fR, but takes a third argument \fIModes\fR, a list of possible modes, see open/2\&. The mode flags \fIbinary\fR and \fIwrite\fR are implicit, so they should not be used\&.
.RE
.LP
.B
write_file_info(Filename, FileInfo) -> ok | {error, Reason}
.br
.RS
.TP
Types
Filename = name()
.br
FileInfo = #file_info{} -- see also read_file_info/1
.br
Reason = ext_posix()
.br
.RE
.RS
.LP
Change 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:

.nf
-include_lib("kernel/include/file\&.hrl")\&.
.fi
.LP
The following fields are used from the record, if they are given\&.
.RS 2
.TP 4
.B
\fIatime = time()\fR:
The last (local) time the file was read\&.
.TP 4
.B
\fImtime = time()\fR:
The last (local) time the file was written\&.
.TP 4
.B
\fIctime = time()\fR:
On Unix, any value give for this field will be ignored (the "ctime" for the file will be set to the current time)\&. On Windows, this field is the new creation time to set for the file\&.
.TP 4
.B
\fImode = int()\fR:
The file permissions as the sum of the following bit values:
.RS 4
.LP

.RS 2
.TP 4
.B
8#00400:
read permission: owner
.TP 4
.B
8#00200:
write permission: owner
.TP 4
.B
8#00100:
execute permission: owner
.TP 4
.B
8#00040:
read permission: group
.TP 4
.B
8#00020:
write permission: group
.TP 4
.B
8#00010:
execute permission: group
.TP 4
.B
8#00004:
read permission: other
.TP 4
.B
8#00002:
write permission: other
.TP 4
.B
8#00001:
execute permission: other
.TP 4
.B
16#800:
set user id on execution
.TP 4
.B
16#400:
set group id on execution
.RE
.LP

.LP
On Unix platforms, other bits than those listed above may be set\&.
.RE
.TP 4
.B
\fIuid = int()\fR:
Indicates the owner of the file\&. Ignored for non-Unix file systems\&.
.TP 4
.B
\fIgid = int()\fR:
Gives the group that the owner of the file belongs to\&. Ignored non-Unix file systems\&.
.RE
.LP
Typical error reasons:
.RS 2
.TP 4
.B
\fIeacces\fR:
Missing search permission for one of the parent directories of the file\&.
.TP 4
.B
\fIenoent\fR:
The file does not exist\&.
.TP 4
.B
\fIenotdir\fR:
A component of the file name 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
.TP 2
*
\fIeagain\fR - resource temporarily unavailable
.TP 2
*
\fIebadf\fR - bad file number
.TP 2
*
\fIebusy\fR - file busy
.TP 2
*
\fIedquot\fR - disk quota exceeded
.TP 2
*
\fIeexist\fR - file already exists
.TP 2
*
\fIefault\fR - bad address in system call argument
.TP 2
*
\fIefbig\fR - file too large
.TP 2
*
\fIeintr\fR - interrupted system call
.TP 2
*
\fIeinval\fR - invalid argument
.TP 2
*
\fIeio\fR - IO error
.TP 2
*
\fIeisdir\fR - illegal operation on a directory
.TP 2
*
\fIeloop\fR - too many levels of symbolic links
.TP 2
*
\fIemfile\fR - too many open files
.TP 2
*
\fIemlink\fR - too many links
.TP 2
*
\fIenametoolong\fR - file name too long
.TP 2
*
\fIenfile\fR - file table overflow
.TP 2
*
\fIenodev\fR - no such device
.TP 2
*
\fIenoent\fR - no such file or directory
.TP 2
*
\fIenomem\fR - not enough memory
.TP 2
*
\fIenospc\fR - no space left on device
.TP 2
*
\fIenotblk\fR - block device required
.TP 2
*
\fIenotdir\fR - not a directory
.TP 2
*
\fIenotsup\fR - operation not supported
.TP 2
*
\fIenxio\fR - no such device or address
.TP 2
*
\fIeperm\fR - not owner
.TP 2
*
\fIepipe\fR - broken pipe
.TP 2
*
\fIerofs\fR - read-only file system
.TP 2
*
\fIespipe\fR - invalid seek
.TP 2
*
\fIesrch\fR - no such process
.TP 2
*
\fIestale\fR - stale remote file handle
.TP 2
*
\fIexdev\fR - cross-domain link
.RE
.SH PERFORMANCE
.LP
Some operating system file operations, for example a \fIsync/1\fR or \fIclose/1\fR on a huge file, may block their calling thread for seconds\&. If this befalls the emulator main thread, the response time is no longer in the order of milliseconds, depending on the definition of "soft" in soft real-time system\&.
.LP
If the device driver thread pool is active, file operations are done through those threads instead, so the emulator can go on executing Erlang processes\&. Unfortunately, the time for serving a file operation increases due to the extra scheduling required from the operating system\&.
.LP
If the device driver thread pool is disabled or of size 0, large file reads and writes are segmented into several smaller, which enables the emulator so server other processes during the file operation\&. This gives the same effect as when using the thread pool, but with larger overhead\&. Other file operations, for example \fIsync/1\fR or \fIclose/1\fR on a huge file, still are a problem\&.
.LP
For increased performance, raw files are recommended\&. Raw files uses the file system of the node\&'s host machine\&. For normal files (non-raw), the file server is used to find the files, and if the node is running its file server as slave to another node\&'s, and the other node runs on some other host machine, they may have different file systems\&. This is seldom a problem, but you have now been warned\&.
.LP
A normal file is really a process so it can be used as an IO 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 another node\&'s, also binaries are copied\&.
.LP
Caching data to reduce the number of file operations, or rather the number of calls to the file driver, will generally increase performance\&. The following function writes 4 MBytes in 23 seconds when tested:

.nf
create_file_slow(Name, N) when integer(N), N >= 0 ->
    {ok, FD} = file:open(Name, [raw, write, delayed_write, binary]),
    ok = create_file_slow(FD, 0, N),
    ok = ?FILE_MODULE:close(FD),
    ok\&.
      
create_file_slow(FD, M, M) ->
    ok;
create_file_slow(FD, M, N) ->
    ok = file:write(FD, <<M:32/unsigned>>),
    create_file_slow(FD, M+1, N)\&.
.fi
.LP
The following, functionally equivalent, function collects 1024 entries into a list of 128 32-byte binaries before each call to \fIfile:write/2\fR and so does the same work in 0\&.52 seconds, which is 44 times faster\&.

.nf
create_file(Name, N) when integer(N), N >= 0 ->
    {ok, FD} = file:open(Name, [raw, write, delayed_write, binary]),
    ok = create_file(FD, 0, N),
    ok = ?FILE_MODULE:close(FD),
    ok\&.
      
create_file(FD, M, M) ->
    ok;
create_file(FD, M, N) when M + 1024 =&lt; N ->
    create_file(FD, M, M + 1024, []),
    create_file(FD, M + 1024, N);
create_file(FD, M, N) ->
    create_file(FD, M, N, [])\&.
      
create_file(FD, M, M, R) ->
    ok = file:write(FD, R);
create_file(FD, M, N0, R) when M + 8 =&lt; N0 ->
    N1  = N0-1,  N2  = N0-2,  N3  = N0-3,  N4  = N0-4, 
    N5  = N0-5,  N6  = N0-6,  N7  = N0-7,  N8  = N0-8, 
    create_file(FD, M, N8, 
                [<<N8:32/unsigned,  N7:32/unsigned, 
                   N6:32/unsigned,  N5:32/unsigned, 
                   N4:32/unsigned,  N3:32/unsigned, 
                   N2:32/unsigned,  N1:32/unsigned>> | R]);
create_file(FD, M, N0, R) ->
    N1 = N0-1,
    create_file(FD, M, N1, [<<N1:32/unsigned>> | R])\&.
.fi
.SS Note:
.LP
Trust only your own benchmarks\&. If the list length in \fIcreate_file/2\fR above is increased, it will run slightly faster, but consume more memory and cause more memory fragmentation\&. How much this affects your application is something that this simple benchmark can not predict\&.
.LP
If the size of each binary is increased to 64 bytes, it will also run slightly faster, but the code will be twice as clumsy\&. In the current implementation are binaries larger than 64 bytes stored in memory common to all processes and not copied when sent between processes, while these smaller binaries are stored on the process heap and copied when sent like any other term\&.
.LP
So, with a binary size of 68 bytes \fIcreate_file/2\fR runs 30 percent slower then with 64 bytes, and will cause much more memory fragmentation\&. Note that if the binaries were to be sent between processes (for example a non-raw file) the results would probably be completely different\&.

.LP
A raw file is really a port\&. When writing data to a port, it is efficient to write a list of binaries\&. There is no need to flatten a deep list before writing\&. On Unix hosts, scatter output, which writes a set of buffers in one operation, is used when possible\&. In this way \fIfile:write(FD, [Bin1, Bin2 | Bin3])\fR will write the contents of the binaries without copying the data at all except for perhaps deep down in the operating system kernel\&.
.LP
For raw files, \fIpwrite/2\fR and \fIpread/2\fR are efficiently implemented\&. The file driver is called only once for the whole operation, and the list iteration is done in the file driver\&.
.LP
The options \fIdelayed_write\fR and \fIread_ahead\fR to \fIfile:open/2\fR makes the file driver cache data to reduce the number of operating system calls\&. The function \fIcreate_file/2\fR in the example above takes 60 seconds seconds without the \fIdelayed_write\fR option, which is 2\&.6 times slower\&.
.LP
And, as a really bad example, \fIcreate_file_slow/2\fR above without the \fIraw\fR, \fIbinary\fR and \fIdelayed_write\fR options, that is it calls \fIfile:open(Name, [write])\fR, needs 1 min 20 seconds for the job, which is 3\&.5 times slower than the first example, and 150 times slower than the optimized \fIcreate_file/2\fR\&. 
.SH WARNINGS
.LP
If an error occurs when accessing an open file with the \fIio\fR module, the process which handles the file will exit\&. The dead file process might hang if a process tries to access it later\&. This will be fixed in a future release\&.
.SH SEE ALSO
.LP
filename(3)