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
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
|
// operations.cpp --------------------------------------------------------------------//
// Copyright 2002-2009, 2014 Beman Dawes
// Copyright 2001 Dietmar Kuehl
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// See library home page at http://www.boost.org/libs/filesystem
//--------------------------------------------------------------------------------------//
#if defined(__ANDROID__) && defined(__ANDROID_API__) && __ANDROID_API__ < 24
// Android fully supports 64-bit file offsets only for API 24 and above.
#else
// define 64-bit offset macros BEFORE including boost/config.hpp (see ticket #5355)
#if defined(__ANDROID__) && defined(__ANDROID_API__) && __ANDROID_API__ < 24
// Android fully supports 64-bit file offsets only for API 24 and above.
//
// Trying to define _FILE_OFFSET_BITS=64 for APIs below 24
// leads to compilation failure for one or another reason,
// depending on target Android API level, Android NDK version,
// used STL, order of include paths and more.
// For more information, please see:
// - https://github.com/boostorg/filesystem/issues/65
// - https://github.com/boostorg/filesystem/pull/69
//
// Android NDK developers consider it the expected behavior.
// See their official position here:
// - https://github.com/android-ndk/ndk/issues/501#issuecomment-326447479
// - https://android.googlesource.com/platform/bionic/+/a34817457feee026e8702a1d2dffe9e92b51d7d1/docs/32-bit-abi.md#32_bit-abi-bugs
//
// Thus we do not define _FILE_OFFSET_BITS in such case.
#else
// Defining _FILE_OFFSET_BITS=64 should kick in 64-bit off_t's
// (and thus st_size) on 32-bit systems that provide the Large File
// Support (LFS) interface, such as Linux, Solaris, and IRIX.
//
// At the time of this comment writing (March 2018), on most systems
// _FILE_OFFSET_BITS=64 definition is harmless:
// either the definition is supported and enables 64-bit off_t,
// or the definition is not supported and is ignored, in which case
// off_t does not change its default size for the target system
// (which may be 32-bit or 64-bit already).
// Thus it makes sense to have _FILE_OFFSET_BITS=64 defined by default,
// instead of listing every system that supports the definition.
// Those few systems, on which _FILE_OFFSET_BITS=64 is harmful,
// for example this definition causes compilation failure on those systems,
// should be exempt from defining _FILE_OFFSET_BITS by adding
// an appropriate #elif block above with the appropriate comment.
//
// _FILE_OFFSET_BITS must be defined before any headers are included
// to ensure that the definition is available to all included headers.
// That is required at least on Solaris, and possibly on other
// systems as well.
#define _FILE_OFFSET_BITS 64
#endif
#endif
// define BOOST_FILESYSTEM_SOURCE so that <boost/filesystem/config.hpp> knows
// the library is being built (possibly exporting rather than importing code)
#define BOOST_FILESYSTEM_SOURCE
#ifndef BOOST_SYSTEM_NO_DEPRECATED
# define BOOST_SYSTEM_NO_DEPRECATED
#endif
#ifndef _POSIX_PTHREAD_SEMANTICS
# define _POSIX_PTHREAD_SEMANTICS // Sun readdir_r()needs this
#endif
#include <boost/filesystem/operations.hpp>
#include <boost/scoped_array.hpp>
#include <boost/detail/workaround.hpp>
#include <limits>
#include <vector>
#include <cstdlib> // for malloc, free
#include <cstring>
#include <cstdio> // for remove, rename
#if defined(__QNXNTO__) // see ticket #5355
# include <stdio.h>
#endif
#include <cerrno>
#ifdef BOOST_FILEYSTEM_INCLUDE_IOSTREAM
# include <iostream>
#endif
namespace fs = boost::filesystem;
using boost::filesystem::path;
using boost::filesystem::filesystem_error;
using boost::filesystem::perms;
using boost::system::error_code;
using boost::system::error_category;
using boost::system::system_category;
using std::string;
using std::wstring;
# ifdef BOOST_POSIX_API
# include <sys/types.h>
# include <sys/stat.h>
# if !defined(__APPLE__) && !defined(__OpenBSD__) && !defined(__ANDROID__) \
&& !defined(__VXWORKS__)
# include <sys/statvfs.h>
# define BOOST_STATVFS statvfs
# define BOOST_STATVFS_F_FRSIZE vfs.f_frsize
# else
# ifdef __OpenBSD__
# include <sys/param.h>
# elif defined(__ANDROID__)
# include <sys/vfs.h>
# endif
# if !defined(__VXWORKS__)
# include <sys/mount.h>
# endif
# define BOOST_STATVFS statfs
# define BOOST_STATVFS_F_FRSIZE static_cast<boost::uintmax_t>(vfs.f_bsize)
# endif
# include <dirent.h>
# include <unistd.h>
# include <fcntl.h>
# include <utime.h>
# include "limits.h"
# else // BOOST_WINDOW_API
# if (defined(__MINGW32__) || defined(__CYGWIN__)) && !defined(WINVER)
// Versions of MinGW or Cygwin that support Filesystem V3 support at least WINVER 0x501.
// See MinGW's windef.h
# define WINVER 0x501
# endif
# include <cwchar>
# include <io.h>
# include <windows.h>
# include <winnt.h>
# if !defined(_WIN32_WINNT)
# define _WIN32_WINNT 0x0500
# endif
# if defined(__BORLANDC__) || defined(__MWERKS__)
# if defined(__BORLANDC__)
using std::time_t;
# endif
# include <utime.h>
# else
# include <sys/utime.h>
# endif
// REPARSE_DATA_BUFFER related definitions are found in ntifs.h, which is part of the
// Windows Device Driver Kit. Since that's inconvenient, the definitions are provided
// here. See http://msdn.microsoft.com/en-us/library/ms791514.aspx
#if !defined(REPARSE_DATA_BUFFER_HEADER_SIZE) // mingw winnt.h does provide the defs
#define SYMLINK_FLAG_RELATIVE 1
typedef struct _REPARSE_DATA_BUFFER {
ULONG ReparseTag;
USHORT ReparseDataLength;
USHORT Reserved;
union {
struct {
USHORT SubstituteNameOffset;
USHORT SubstituteNameLength;
USHORT PrintNameOffset;
USHORT PrintNameLength;
ULONG Flags;
WCHAR PathBuffer[1];
/* Example of distinction between substitute and print names:
mklink /d ldrive c:\
SubstituteName: c:\\??\
PrintName: c:\
*/
} SymbolicLinkReparseBuffer;
struct {
USHORT SubstituteNameOffset;
USHORT SubstituteNameLength;
USHORT PrintNameOffset;
USHORT PrintNameLength;
WCHAR PathBuffer[1];
} MountPointReparseBuffer;
struct {
UCHAR DataBuffer[1];
} GenericReparseBuffer;
};
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
#define REPARSE_DATA_BUFFER_HEADER_SIZE \
FIELD_OFFSET(REPARSE_DATA_BUFFER, GenericReparseBuffer)
#endif
#ifndef MAXIMUM_REPARSE_DATA_BUFFER_SIZE
#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
#endif
# ifndef FSCTL_GET_REPARSE_POINT
# define FSCTL_GET_REPARSE_POINT 0x900a8
# endif
# ifndef IO_REPARSE_TAG_SYMLINK
# define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
# endif
inline std::wstring wgetenv(const wchar_t* name)
{
// use vector since for C++03 basic_string is not required to be contiguous
std::vector<wchar_t> buf(::GetEnvironmentVariableW(name, NULL, 0));
// C++03 vector does not have data() so use &buf[0]
return (buf.empty()
|| ::GetEnvironmentVariableW(name, &buf[0], static_cast<DWORD>(buf.size())) == 0)
? std::wstring() : std::wstring(&buf[0]);
}
# endif // BOOST_WINDOWS_API
// BOOST_FILESYSTEM_STATUS_CACHE enables file_status cache in
// dir_itr_increment. The config tests are placed here because some of the
// macros being tested come from dirent.h.
//
// TODO: find out what macros indicate dirent::d_type present in more libraries
# if defined(BOOST_WINDOWS_API)\
|| defined(_DIRENT_HAVE_D_TYPE)// defined by GNU C library if d_type present
# define BOOST_FILESYSTEM_STATUS_CACHE
# endif
// POSIX/Windows macros ----------------------------------------------------//
// Portions of the POSIX and Windows API's are very similar, except for name,
// order of arguments, and meaning of zero/non-zero returns. The macros below
// abstract away those differences. They follow Windows naming and order of
// arguments, and return true to indicate no error occurred. [POSIX naming,
// order of arguments, and meaning of return were followed initially, but
// found to be less clear and cause more coding errors.]
# if defined(BOOST_POSIX_API)
typedef int err_t;
// POSIX uses a 0 return to indicate success
# define BOOST_ERRNO errno
# define BOOST_SET_CURRENT_DIRECTORY(P)(::chdir(P)== 0)
# define BOOST_CREATE_DIRECTORY(P)(::mkdir(P, S_IRWXU|S_IRWXG|S_IRWXO)== 0)
# define BOOST_CREATE_HARD_LINK(F,T)(::link(T, F)== 0)
# define BOOST_CREATE_SYMBOLIC_LINK(F,T,Flag)(::symlink(T, F)== 0)
# define BOOST_REMOVE_DIRECTORY(P)(::rmdir(P)== 0)
# define BOOST_DELETE_FILE(P)(::unlink(P)== 0)
# define BOOST_COPY_DIRECTORY(F,T)(!(::stat(from.c_str(), &from_stat)!= 0\
|| ::mkdir(to.c_str(),from_stat.st_mode)!= 0))
# define BOOST_COPY_FILE(F,T,FailIfExistsBool)copy_file_api(F, T, FailIfExistsBool)
# define BOOST_MOVE_FILE(OLD,NEW)(::rename(OLD, NEW)== 0)
# define BOOST_RESIZE_FILE(P,SZ)(::truncate(P, SZ)== 0)
# define BOOST_ERROR_NOT_SUPPORTED ENOSYS
# define BOOST_ERROR_ALREADY_EXISTS EEXIST
# else // BOOST_WINDOWS_API
typedef DWORD err_t;
// Windows uses a non-0 return to indicate success
# define BOOST_ERRNO ::GetLastError()
# define BOOST_SET_CURRENT_DIRECTORY(P)(::SetCurrentDirectoryW(P)!= 0)
# define BOOST_CREATE_DIRECTORY(P)(::CreateDirectoryW(P, 0)!= 0)
# define BOOST_CREATE_HARD_LINK(F,T)(create_hard_link_api(F, T, 0)!= 0)
# define BOOST_CREATE_SYMBOLIC_LINK(F,T,Flag)(create_symbolic_link_api(F, T, Flag)!= 0)
# define BOOST_REMOVE_DIRECTORY(P)(::RemoveDirectoryW(P)!= 0)
# define BOOST_DELETE_FILE(P)(::DeleteFileW(P)!= 0)
# define BOOST_COPY_DIRECTORY(F,T)(::CreateDirectoryExW(F, T, 0)!= 0)
# define BOOST_COPY_FILE(F,T,FailIfExistsBool)(::CopyFileW(F, T, FailIfExistsBool)!= 0)
# define BOOST_MOVE_FILE(OLD,NEW)(::MoveFileExW(OLD, NEW, MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED)!= 0)
# define BOOST_RESIZE_FILE(P,SZ)(resize_file_api(P, SZ)!= 0)
# define BOOST_READ_SYMLINK(P,T)
# define BOOST_ERROR_ALREADY_EXISTS ERROR_ALREADY_EXISTS
# define BOOST_ERROR_NOT_SUPPORTED ERROR_NOT_SUPPORTED
# endif
//--------------------------------------------------------------------------------------//
// //
// helpers (all operating systems) //
// //
//--------------------------------------------------------------------------------------//
namespace
{
fs::file_type query_file_type(const path& p, error_code* ec);
boost::filesystem::directory_iterator end_dir_itr;
// error handling helpers ----------------------------------------------------------//
bool error(err_t error_num, error_code* ec, const char* message);
bool error(err_t error_num, const path& p, error_code* ec, const char* message);
bool error(err_t error_num, const path& p1, const path& p2, error_code* ec,
const char* message);
const error_code ok;
// error_num is value of errno on POSIX, error code (from ::GetLastError()) on Windows.
// Interface changed 30 Jan 15 to have caller supply error_num as ::SetLastError()
// values were apparently getting cleared before they could be retrieved by error().
bool error(err_t error_num, error_code* ec, const char* message)
{
if (!error_num)
{
if (ec != 0) ec->clear();
}
else
{ // error
if (ec == 0)
BOOST_FILESYSTEM_THROW(filesystem_error(message,
error_code(error_num, system_category())));
else
ec->assign(error_num, system_category());
}
return error_num != 0;
}
bool error(err_t error_num, const path& p, error_code* ec, const char* message)
{
if (!error_num)
{
if (ec != 0) ec->clear();
}
else
{ // error
if (ec == 0)
BOOST_FILESYSTEM_THROW(filesystem_error(message,
p, error_code(error_num, system_category())));
else
ec->assign(error_num, system_category());
}
return error_num != 0;
}
bool error(err_t error_num, const path& p1, const path& p2, error_code* ec,
const char* message)
{
if (!error_num)
{
if (ec != 0) ec->clear();
}
else
{ // error
if (ec == 0)
BOOST_FILESYSTEM_THROW(filesystem_error(message,
p1, p2, error_code(error_num, system_category())));
else
ec->assign(error_num, system_category());
}
return error_num != 0;
}
// general helpers -----------------------------------------------------------------//
bool is_empty_directory(const path& p, error_code* ec)
{
return (ec != 0 ? fs::directory_iterator(p, *ec) : fs::directory_iterator(p))
== end_dir_itr;
}
bool not_found_error(int errval); // forward declaration
// only called if directory exists
bool remove_directory(const path& p) // true if succeeds or not found
{
return BOOST_REMOVE_DIRECTORY(p.c_str())
|| not_found_error(BOOST_ERRNO); // mitigate possible file system race. See #11166
}
// only called if file exists
bool remove_file(const path& p) // true if succeeds or not found
{
return BOOST_DELETE_FILE(p.c_str())
|| not_found_error(BOOST_ERRNO); // mitigate possible file system race. See #11166
}
// called by remove and remove_all_aux
bool remove_file_or_directory(const path& p, fs::file_type type, error_code* ec)
// return true if file removed, false if not removed
{
if (type == fs::file_not_found)
{
if (ec != 0) ec->clear();
return false;
}
if (type == fs::directory_file
# ifdef BOOST_WINDOWS_API
|| type == fs::_detail_directory_symlink
# endif
)
{
if (error(!remove_directory(p) ? BOOST_ERRNO : 0, p, ec,
"boost::filesystem::remove"))
return false;
}
else
{
if (error(!remove_file(p) ? BOOST_ERRNO : 0, p, ec,
"boost::filesystem::remove"))
return false;
}
return true;
}
boost::uintmax_t remove_all_aux(const path& p, fs::file_type type,
error_code* ec)
{
boost::uintmax_t count = 1;
if (type == fs::directory_file) // but not a directory symlink
{
fs::directory_iterator itr;
if (ec != 0)
{
itr = fs::directory_iterator(p, *ec);
if (*ec)
return count;
}
else
itr = fs::directory_iterator(p);
for (; itr != end_dir_itr; ++itr)
{
fs::file_type tmp_type = query_file_type(itr->path(), ec);
if (ec != 0 && *ec)
return count;
count += remove_all_aux(itr->path(), tmp_type, ec);
if (ec != 0 && *ec)
return count;
}
}
remove_file_or_directory(p, type, ec);
return count;
}
#ifdef BOOST_POSIX_API
//--------------------------------------------------------------------------------------//
// //
// POSIX-specific helpers //
// //
//--------------------------------------------------------------------------------------//
const char dot = '.';
bool not_found_error(int errval)
{
return errno == ENOENT || errno == ENOTDIR;
}
bool // true if ok
copy_file_api(const std::string& from_p,
const std::string& to_p, bool fail_if_exists)
{
const std::size_t buf_sz = 32768;
boost::scoped_array<char> buf(new char [buf_sz]);
int infile=-1, outfile=-1; // -1 means not open
// bug fixed: code previously did a stat()on the from_file first, but that
// introduced a gratuitous race condition; the stat()is now done after the open()
if ((infile = ::open(from_p.c_str(), O_RDONLY))< 0)
{ return false; }
struct stat from_stat;
if (::stat(from_p.c_str(), &from_stat)!= 0)
{
::close(infile);
return false;
}
int oflag = O_CREAT | O_WRONLY | O_TRUNC;
if (fail_if_exists)
oflag |= O_EXCL;
if ((outfile = ::open(to_p.c_str(), oflag, from_stat.st_mode))< 0)
{
int open_errno = errno;
BOOST_ASSERT(infile >= 0);
::close(infile);
errno = open_errno;
return false;
}
ssize_t sz, sz_read=1, sz_write;
while (sz_read > 0
&& (sz_read = ::read(infile, buf.get(), buf_sz)) > 0)
{
// Allow for partial writes - see Advanced Unix Programming (2nd Ed.),
// Marc Rochkind, Addison-Wesley, 2004, page 94
sz_write = 0;
do
{
BOOST_ASSERT(sz_read - sz_write > 0); // #1
// ticket 4438 claimed possible infinite loop if write returns 0. My analysis
// is that POSIX specifies 0 return only if 3rd arg is 0, and that will never
// happen due to loop entry and coninuation conditions. BOOST_ASSERT #1 above
// and #2 below added to verify that analysis.
if ((sz = ::write(outfile, buf.get() + sz_write,
sz_read - sz_write)) < 0)
{
sz_read = sz; // cause read loop termination
break; // and error reported after closes
}
BOOST_ASSERT(sz > 0); // #2
sz_write += sz;
} while (sz_write < sz_read);
}
if (::close(infile)< 0)
sz_read = -1;
if (::close(outfile)< 0)
sz_read = -1;
return sz_read >= 0;
}
inline fs::file_type query_file_type(const path& p, error_code* ec)
{
return fs::detail::symlink_status(p, ec).type();
}
# else
//--------------------------------------------------------------------------------------//
// //
// Windows-specific helpers //
// //
//--------------------------------------------------------------------------------------//
const std::size_t buf_size=128;
const wchar_t dot = L'.';
bool not_found_error(int errval)
{
return errval == ERROR_FILE_NOT_FOUND
|| errval == ERROR_PATH_NOT_FOUND
|| errval == ERROR_INVALID_NAME // "tools/jam/src/:sys:stat.h", "//foo"
|| errval == ERROR_INVALID_DRIVE // USB card reader with no card inserted
|| errval == ERROR_NOT_READY // CD/DVD drive with no disc inserted
|| errval == ERROR_INVALID_PARAMETER // ":sys:stat.h"
|| errval == ERROR_BAD_PATHNAME // "//nosuch" on Win64
|| errval == ERROR_BAD_NETPATH; // "//nosuch" on Win32
}
static bool equal_extension( wchar_t const* p, wchar_t const (&x1)[ 5 ], wchar_t const (&x2)[ 5 ] )
{
return
(p[0] == x1[0] || p[0] == x2[0]) &&
(p[1] == x1[1] || p[1] == x2[1]) &&
(p[2] == x1[2] || p[2] == x2[2]) &&
(p[3] == x1[3] || p[3] == x2[3]) &&
p[4] == 0;
}
perms make_permissions(const path& p, DWORD attr)
{
perms prms = fs::owner_read | fs::group_read | fs::others_read;
if ((attr & FILE_ATTRIBUTE_READONLY) == 0)
prms |= fs::owner_write | fs::group_write | fs::others_write;
path ext = p.extension();
wchar_t const* q = ext.c_str();
if (equal_extension(q, L".exe", L".EXE")
|| equal_extension(q, L".com", L".COM")
|| equal_extension(q, L".bat", L".BAT")
|| equal_extension(q, L".cmd", L".CMD"))
prms |= fs::owner_exe | fs::group_exe | fs::others_exe;
return prms;
}
// these constants come from inspecting some Microsoft sample code
std::time_t to_time_t(const FILETIME & ft)
{
__int64 t = (static_cast<__int64>(ft.dwHighDateTime)<< 32)
+ ft.dwLowDateTime;
# if !defined(BOOST_MSVC) || BOOST_MSVC > 1300 // > VC++ 7.0
t -= 116444736000000000LL;
# else
t -= 116444736000000000;
# endif
t /= 10000000;
return static_cast<std::time_t>(t);
}
void to_FILETIME(std::time_t t, FILETIME & ft)
{
__int64 temp = t;
temp *= 10000000;
# if !defined(BOOST_MSVC) || BOOST_MSVC > 1300 // > VC++ 7.0
temp += 116444736000000000LL;
# else
temp += 116444736000000000;
# endif
ft.dwLowDateTime = static_cast<DWORD>(temp);
ft.dwHighDateTime = static_cast<DWORD>(temp >> 32);
}
// Thanks to Jeremy Maitin-Shepard for much help and for permission to
// base the equivalent()implementation on portions of his
// file-equivalence-win32.cpp experimental code.
struct handle_wrapper
{
HANDLE handle;
handle_wrapper(HANDLE h)
: handle(h){}
~handle_wrapper()
{
if (handle != INVALID_HANDLE_VALUE)
::CloseHandle(handle);
}
};
HANDLE create_file_handle(const path& p, DWORD dwDesiredAccess,
DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile)
{
return ::CreateFileW(p.c_str(), dwDesiredAccess, dwShareMode,
lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
hTemplateFile);
}
bool is_reparse_point_a_symlink(const path& p)
{
handle_wrapper h(create_file_handle(p, FILE_READ_EA,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL));
if (h.handle == INVALID_HANDLE_VALUE)
return false;
boost::scoped_array<char> buf(new char [MAXIMUM_REPARSE_DATA_BUFFER_SIZE]);
// Query the reparse data
DWORD dwRetLen;
BOOL result = ::DeviceIoControl(h.handle, FSCTL_GET_REPARSE_POINT, NULL, 0, buf.get(),
MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dwRetLen, NULL);
if (!result) return false;
return reinterpret_cast<const REPARSE_DATA_BUFFER*>(buf.get())->ReparseTag
== IO_REPARSE_TAG_SYMLINK
// Issue 9016 asked that NTFS directory junctions be recognized as directories.
// That is equivalent to recognizing them as symlinks, and then the normal symlink
// mechanism will take care of recognizing them as directories.
//
// Directory junctions are very similar to symlinks, but have some performance
// and other advantages over symlinks. They can be created from the command line
// with "mklink /j junction-name target-path".
|| reinterpret_cast<const REPARSE_DATA_BUFFER*>(buf.get())->ReparseTag
== IO_REPARSE_TAG_MOUNT_POINT; // aka "directory junction" or "junction"
}
inline std::size_t get_full_path_name(
const path& src, std::size_t len, wchar_t* buf, wchar_t** p)
{
return static_cast<std::size_t>(
::GetFullPathNameW(src.c_str(), static_cast<DWORD>(len), buf, p));
}
fs::file_status process_status_failure(const path& p, error_code* ec)
{
int errval(::GetLastError());
if (ec != 0) // always report errval, even though some
ec->assign(errval, system_category()); // errval values are not status_errors
if (not_found_error(errval))
{
return fs::file_status(fs::file_not_found, fs::no_perms);
}
else if (errval == ERROR_SHARING_VIOLATION)
{
return fs::file_status(fs::type_unknown);
}
if (ec == 0)
BOOST_FILESYSTEM_THROW(filesystem_error("boost::filesystem::status",
p, error_code(errval, system_category())));
return fs::file_status(fs::status_error);
}
// differs from symlink_status() in that directory symlinks are reported as
// _detail_directory_symlink, as required on Windows by remove() and its helpers.
fs::file_type query_file_type(const path& p, error_code* ec)
{
DWORD attr(::GetFileAttributesW(p.c_str()));
if (attr == 0xFFFFFFFF)
{
return process_status_failure(p, ec).type();
}
if (ec != 0) ec->clear();
if (attr & FILE_ATTRIBUTE_REPARSE_POINT)
{
if (is_reparse_point_a_symlink(p))
return (attr & FILE_ATTRIBUTE_DIRECTORY)
? fs::_detail_directory_symlink
: fs::symlink_file;
return fs::reparse_file;
}
return (attr & FILE_ATTRIBUTE_DIRECTORY)
? fs::directory_file
: fs::regular_file;
}
BOOL resize_file_api(const wchar_t* p, boost::uintmax_t size)
{
handle_wrapper h(CreateFileW(p, GENERIC_WRITE, 0, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0));
LARGE_INTEGER sz;
sz.QuadPart = size;
return h.handle != INVALID_HANDLE_VALUE
&& ::SetFilePointerEx(h.handle, sz, 0, FILE_BEGIN)
&& ::SetEndOfFile(h.handle);
}
// Windows kernel32.dll functions that may or may not be present
// must be accessed through pointers
typedef BOOL (WINAPI *PtrCreateHardLinkW)(
/*__in*/ LPCWSTR lpFileName,
/*__in*/ LPCWSTR lpExistingFileName,
/*__reserved*/ LPSECURITY_ATTRIBUTES lpSecurityAttributes
);
PtrCreateHardLinkW create_hard_link_api = PtrCreateHardLinkW(
::GetProcAddress(
::GetModuleHandleW(L"kernel32.dll"), "CreateHardLinkW"));
typedef BOOLEAN (WINAPI *PtrCreateSymbolicLinkW)(
/*__in*/ LPCWSTR lpSymlinkFileName,
/*__in*/ LPCWSTR lpTargetFileName,
/*__in*/ DWORD dwFlags
);
PtrCreateSymbolicLinkW create_symbolic_link_api = PtrCreateSymbolicLinkW(
::GetProcAddress(
::GetModuleHandleW(L"kernel32.dll"), "CreateSymbolicLinkW"));
#endif
//#ifdef BOOST_WINDOWS_API
//
//
// inline bool get_free_disk_space(const std::wstring& ph,
// PULARGE_INTEGER avail, PULARGE_INTEGER total, PULARGE_INTEGER free)
// { return ::GetDiskFreeSpaceExW(ph.c_str(), avail, total, free)!= 0; }
//
//#endif
} // unnamed namespace
//--------------------------------------------------------------------------------------//
// //
// operations functions declared in operations.hpp //
// in alphabetic order //
// //
//--------------------------------------------------------------------------------------//
namespace boost
{
namespace filesystem
{
BOOST_FILESYSTEM_DECL
path absolute(const path& p, const path& base)
{
// if ( p.empty() || p.is_absolute() )
// return p;
// // recursively calling absolute is sub-optimal, but is simple
// path abs_base(base.is_absolute() ? base : absolute(base));
//# ifdef BOOST_WINDOWS_API
// if (p.has_root_directory())
// return abs_base.root_name() / p;
// // !p.has_root_directory
// if (p.has_root_name())
// return p.root_name()
// / abs_base.root_directory() / abs_base.relative_path() / p.relative_path();
// // !p.has_root_name()
//# endif
// return abs_base / p;
// recursively calling absolute is sub-optimal, but is sure and simple
path abs_base(base.is_absolute() ? base : absolute(base));
// store expensive to compute values that are needed multiple times
path p_root_name (p.root_name());
path base_root_name (abs_base.root_name());
path p_root_directory (p.root_directory());
if (p.empty())
return abs_base;
if (!p_root_name.empty()) // p.has_root_name()
{
if (p_root_directory.empty()) // !p.has_root_directory()
return p_root_name / abs_base.root_directory()
/ abs_base.relative_path() / p.relative_path();
// p is absolute, so fall through to return p at end of block
}
else if (!p_root_directory.empty()) // p.has_root_directory()
{
# ifdef BOOST_POSIX_API
// POSIX can have root name it it is a network path
if (base_root_name.empty()) // !abs_base.has_root_name()
return p;
# endif
return base_root_name / p;
}
else
{
return abs_base / p;
}
return p; // p.is_absolute() is true
}
namespace detail
{
BOOST_FILESYSTEM_DECL bool possible_large_file_size_support()
{
# ifdef BOOST_POSIX_API
struct stat lcl_stat;
return sizeof(lcl_stat.st_size)> 4;
# else
return true;
# endif
}
BOOST_FILESYSTEM_DECL
path canonical(const path& p, const path& base, system::error_code* ec)
{
path source (p.is_absolute() ? p : absolute(p, base));
path root(source.root_path());
path result;
system::error_code local_ec;
file_status stat (status(source, local_ec));
if (stat.type() == fs::file_not_found)
{
if (ec == 0)
BOOST_FILESYSTEM_THROW(filesystem_error(
"boost::filesystem::canonical", source,
error_code(system::errc::no_such_file_or_directory, system::generic_category())));
ec->assign(system::errc::no_such_file_or_directory, system::generic_category());
return result;
}
else if (local_ec)
{
if (ec == 0)
BOOST_FILESYSTEM_THROW(filesystem_error(
"boost::filesystem::canonical", source, local_ec));
*ec = local_ec;
return result;
}
bool scan (true);
while (scan)
{
scan = false;
result.clear();
for (path::iterator itr = source.begin(); itr != source.end(); ++itr)
{
if (*itr == dot_path())
continue;
if (*itr == dot_dot_path())
{
if (result != root)
result.remove_filename();
continue;
}
result /= *itr;
bool is_sym (is_symlink(detail::symlink_status(result, ec)));
if (ec && *ec)
return path();
if (is_sym)
{
path link(detail::read_symlink(result, ec));
if (ec && *ec)
return path();
result.remove_filename();
if (link.is_absolute())
{
for (++itr; itr != source.end(); ++itr)
link /= *itr;
source = link;
}
else // link is relative
{
path new_source(result);
new_source /= link;
for (++itr; itr != source.end(); ++itr)
new_source /= *itr;
source = new_source;
}
scan = true; // symlink causes scan to be restarted
break;
}
}
}
if (ec != 0)
ec->clear();
BOOST_ASSERT_MSG(result.is_absolute(), "canonical() implementation error; please report");
return result;
}
BOOST_FILESYSTEM_DECL
void copy(const path& from, const path& to, system::error_code* ec)
{
file_status s(symlink_status(from, ec));
if (ec != 0 && *ec) return;
if(is_symlink(s))
{
copy_symlink(from, to, *ec);
}
else if(is_directory(s))
{
copy_directory(from, to, *ec);
}
else if(is_regular_file(s))
{
copy_file(from, to, fs::copy_option::fail_if_exists, *ec);
}
else
{
if (ec == 0)
BOOST_FILESYSTEM_THROW(filesystem_error("boost::filesystem::copy",
from, to, error_code(BOOST_ERROR_NOT_SUPPORTED, system_category())));
ec->assign(BOOST_ERROR_NOT_SUPPORTED, system_category());
}
}
BOOST_FILESYSTEM_DECL
void copy_directory(const path& from, const path& to, system::error_code* ec)
{
# ifdef BOOST_POSIX_API
struct stat from_stat;
# endif
error(!BOOST_COPY_DIRECTORY(from.c_str(), to.c_str()) ? BOOST_ERRNO : 0,
from, to, ec, "boost::filesystem::copy_directory");
}
BOOST_FILESYSTEM_DECL
void copy_file(const path& from, const path& to, copy_option option, error_code* ec)
{
error(!BOOST_COPY_FILE(from.c_str(), to.c_str(),
option == fail_if_exists) ? BOOST_ERRNO : 0,
from, to, ec, "boost::filesystem::copy_file");
}
BOOST_FILESYSTEM_DECL
void copy_symlink(const path& existing_symlink, const path& new_symlink,
system::error_code* ec)
{
# if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600
error(BOOST_ERROR_NOT_SUPPORTED, new_symlink, existing_symlink, ec,
"boost::filesystem::copy_symlink");
# else // modern Windows or BOOST_POSIX_API
path p(read_symlink(existing_symlink, ec));
if (ec != 0 && *ec) return;
create_symlink(p, new_symlink, ec);
# endif
}
BOOST_FILESYSTEM_DECL
bool create_directories(const path& p, system::error_code* ec)
{
if (p.empty())
{
if (ec == 0)
BOOST_FILESYSTEM_THROW(filesystem_error(
"boost::filesystem::create_directories", p,
system::errc::make_error_code(system::errc::invalid_argument)));
else
ec->assign(system::errc::invalid_argument, system::generic_category());
return false;
}
if (p.filename_is_dot() || p.filename_is_dot_dot())
return create_directories(p.parent_path(), ec);
error_code local_ec;
file_status p_status = status(p, local_ec);
if (p_status.type() == directory_file)
{
if (ec != 0)
ec->clear();
return false;
}
path parent = p.parent_path();
BOOST_ASSERT_MSG(parent != p, "internal error: p == p.parent_path()");
if (!parent.empty())
{
// determine if the parent exists
file_status parent_status = status(parent, local_ec);
// if the parent does not exist, create the parent
if (parent_status.type() == file_not_found)
{
create_directories(parent, local_ec);
if (local_ec)
{
if (ec == 0)
BOOST_FILESYSTEM_THROW(filesystem_error(
"boost::filesystem::create_directories", parent, local_ec));
else
*ec = local_ec;
return false;
}
}
}
// create the directory
return create_directory(p, ec);
}
BOOST_FILESYSTEM_DECL
bool create_directory(const path& p, error_code* ec)
{
if (BOOST_CREATE_DIRECTORY(p.c_str()))
{
if (ec != 0)
ec->clear();
return true;
}
// attempt to create directory failed
int errval(BOOST_ERRNO); // save reason for failure
error_code dummy;
if (is_directory(p, dummy))
{
if (ec != 0)
ec->clear();
return false;
}
// attempt to create directory failed && it doesn't already exist
if (ec == 0)
BOOST_FILESYSTEM_THROW(filesystem_error("boost::filesystem::create_directory",
p, error_code(errval, system_category())));
else
ec->assign(errval, system_category());
return false;
}
BOOST_FILESYSTEM_DECL
void create_directory_symlink(const path& to, const path& from,
system::error_code* ec)
{
# if defined(BOOST_WINDOWS_API) && _WIN32_WINNT < 0x0600 // SDK earlier than Vista and Server 2008
error(BOOST_ERROR_NOT_SUPPORTED, to, from, ec,
"boost::filesystem::create_directory_symlink");
# else
# if defined(BOOST_WINDOWS_API) && _WIN32_WINNT >= 0x0600
// see if actually supported by Windows runtime dll
if (error(!create_symbolic_link_api ? BOOST_ERROR_NOT_SUPPORTED : 0, to, from, ec,
"boost::filesystem::create_directory_symlink"))
return;
# endif
error(!BOOST_CREATE_SYMBOLIC_LINK(from.c_str(), to.c_str(),
SYMBOLIC_LINK_FLAG_DIRECTORY) ? BOOST_ERRNO : 0,
to, from, ec, "boost::filesystem::create_directory_symlink");
# endif
}
BOOST_FILESYSTEM_DECL
void create_hard_link(const path& to, const path& from, error_code* ec)
{
# if defined(BOOST_WINDOWS_API) && _WIN32_WINNT < 0x0500 // SDK earlier than Win 2K
error(BOOST_ERROR_NOT_SUPPORTED, to, from, ec,
"boost::filesystem::create_hard_link");
# else
# if defined(BOOST_WINDOWS_API) && _WIN32_WINNT >= 0x0500
// see if actually supported by Windows runtime dll
if (error(!create_hard_link_api ? BOOST_ERROR_NOT_SUPPORTED : 0, to, from, ec,
"boost::filesystem::create_hard_link"))
return;
# endif
error(!BOOST_CREATE_HARD_LINK(from.c_str(), to.c_str()) ? BOOST_ERRNO : 0, to, from, ec,
"boost::filesystem::create_hard_link");
# endif
}
BOOST_FILESYSTEM_DECL
void create_symlink(const path& to, const path& from, error_code* ec)
{
# if defined(BOOST_WINDOWS_API) && _WIN32_WINNT < 0x0600 // SDK earlier than Vista and Server 2008
error(BOOST_ERROR_NOT_SUPPORTED, to, from, ec,
"boost::filesystem::create_directory_symlink");
# else
# if defined(BOOST_WINDOWS_API) && _WIN32_WINNT >= 0x0600
// see if actually supported by Windows runtime dll
if (error(!create_symbolic_link_api ? BOOST_ERROR_NOT_SUPPORTED : 0, to, from, ec,
"boost::filesystem::create_symlink"))
return;
# endif
error(!BOOST_CREATE_SYMBOLIC_LINK(from.c_str(), to.c_str(), 0) ? BOOST_ERRNO : 0,
to, from, ec, "boost::filesystem::create_symlink");
# endif
}
BOOST_FILESYSTEM_DECL
path current_path(error_code* ec)
{
# ifdef BOOST_POSIX_API
path cur;
for (long path_max = 128;; path_max *=2)// loop 'til buffer large enough
{
boost::scoped_array<char>
buf(new char[static_cast<std::size_t>(path_max)]);
if (::getcwd(buf.get(), static_cast<std::size_t>(path_max))== 0)
{
if (error(errno != ERANGE ? errno : 0
// bug in some versions of the Metrowerks C lib on the Mac: wrong errno set
# if defined(__MSL__) && (defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__))
&& errno != 0
# endif
, ec, "boost::filesystem::current_path"))
{
break;
}
}
else
{
cur = buf.get();
if (ec != 0) ec->clear();
break;
}
}
return cur;
# else
DWORD sz;
if ((sz = ::GetCurrentDirectoryW(0, NULL)) == 0)sz = 1;
boost::scoped_array<path::value_type> buf(new path::value_type[sz]);
error(::GetCurrentDirectoryW(sz, buf.get()) == 0 ? BOOST_ERRNO : 0, ec,
"boost::filesystem::current_path");
return path(buf.get());
# endif
}
BOOST_FILESYSTEM_DECL
void current_path(const path& p, system::error_code* ec)
{
error(!BOOST_SET_CURRENT_DIRECTORY(p.c_str()) ? BOOST_ERRNO : 0,
p, ec, "boost::filesystem::current_path");
}
BOOST_FILESYSTEM_DECL
bool equivalent(const path& p1, const path& p2, system::error_code* ec)
{
# ifdef BOOST_POSIX_API
struct stat s2;
int e2(::stat(p2.c_str(), &s2));
struct stat s1;
int e1(::stat(p1.c_str(), &s1));
if (e1 != 0 || e2 != 0)
{
// if one is invalid and the other isn't then they aren't equivalent,
// but if both are invalid then it is an error
error (e1 != 0 && e2 != 0, p1, p2, ec, "boost::filesystem::equivalent");
return false;
}
// both stats now known to be valid
return s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino
// According to the POSIX stat specs, "The st_ino and st_dev fields
// taken together uniquely identify the file within the system."
// Just to be sure, size and mod time are also checked.
&& s1.st_size == s2.st_size && s1.st_mtime == s2.st_mtime;
# else // Windows
// Note well: Physical location on external media is part of the
// equivalence criteria. If there are no open handles, physical location
// can change due to defragmentation or other relocations. Thus handles
// must be held open until location information for both paths has
// been retrieved.
// p2 is done first, so any error reported is for p1
handle_wrapper h2(
create_file_handle(
p2.c_str(),
0,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
0));
handle_wrapper h1(
create_file_handle(
p1.c_str(),
0,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
0));
if (h1.handle == INVALID_HANDLE_VALUE
|| h2.handle == INVALID_HANDLE_VALUE)
{
// if one is invalid and the other isn't, then they aren't equivalent,
// but if both are invalid then it is an error
error((h1.handle == INVALID_HANDLE_VALUE
&& h2.handle == INVALID_HANDLE_VALUE) ? BOOST_ERROR_NOT_SUPPORTED : 0, p1, p2, ec,
"boost::filesystem::equivalent");
return false;
}
// at this point, both handles are known to be valid
BY_HANDLE_FILE_INFORMATION info1, info2;
if (error(!::GetFileInformationByHandle(h1.handle, &info1) ? BOOST_ERRNO : 0,
p1, p2, ec, "boost::filesystem::equivalent"))
return false;
if (error(!::GetFileInformationByHandle(h2.handle, &info2) ? BOOST_ERRNO : 0,
p1, p2, ec, "boost::filesystem::equivalent"))
return false;
// In theory, volume serial numbers are sufficient to distinguish between
// devices, but in practice VSN's are sometimes duplicated, so last write
// time and file size are also checked.
return
info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
&& info1.nFileIndexHigh == info2.nFileIndexHigh
&& info1.nFileIndexLow == info2.nFileIndexLow
&& info1.nFileSizeHigh == info2.nFileSizeHigh
&& info1.nFileSizeLow == info2.nFileSizeLow
&& info1.ftLastWriteTime.dwLowDateTime
== info2.ftLastWriteTime.dwLowDateTime
&& info1.ftLastWriteTime.dwHighDateTime
== info2.ftLastWriteTime.dwHighDateTime;
# endif
}
BOOST_FILESYSTEM_DECL
boost::uintmax_t file_size(const path& p, error_code* ec)
{
# ifdef BOOST_POSIX_API
struct stat path_stat;
if (error(::stat(p.c_str(), &path_stat)!= 0 ? BOOST_ERRNO : 0,
p, ec, "boost::filesystem::file_size"))
return static_cast<boost::uintmax_t>(-1);
if (error(!S_ISREG(path_stat.st_mode) ? EPERM : 0,
p, ec, "boost::filesystem::file_size"))
return static_cast<boost::uintmax_t>(-1);
return static_cast<boost::uintmax_t>(path_stat.st_size);
# else // Windows
// assume uintmax_t is 64-bits on all Windows compilers
WIN32_FILE_ATTRIBUTE_DATA fad;
if (error(::GetFileAttributesExW(p.c_str(), ::GetFileExInfoStandard, &fad)== 0
? BOOST_ERRNO : 0, p, ec, "boost::filesystem::file_size"))
return static_cast<boost::uintmax_t>(-1);
if (error((fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)!= 0
? ERROR_NOT_SUPPORTED : 0, p, ec, "boost::filesystem::file_size"))
return static_cast<boost::uintmax_t>(-1);
return (static_cast<boost::uintmax_t>(fad.nFileSizeHigh)
<< (sizeof(fad.nFileSizeLow)*8)) + fad.nFileSizeLow;
# endif
}
BOOST_FILESYSTEM_DECL
boost::uintmax_t hard_link_count(const path& p, system::error_code* ec)
{
# ifdef BOOST_POSIX_API
struct stat path_stat;
return error(::stat(p.c_str(), &path_stat)!= 0 ? BOOST_ERRNO : 0,
p, ec, "boost::filesystem::hard_link_count")
? 0
: static_cast<boost::uintmax_t>(path_stat.st_nlink);
# else // Windows
// Link count info is only available through GetFileInformationByHandle
BY_HANDLE_FILE_INFORMATION info;
handle_wrapper h(
create_file_handle(p.c_str(), 0,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0));
return
!error(h.handle == INVALID_HANDLE_VALUE ? BOOST_ERRNO : 0,
p, ec, "boost::filesystem::hard_link_count")
&& !error(::GetFileInformationByHandle(h.handle, &info)== 0 ? BOOST_ERRNO : 0,
p, ec, "boost::filesystem::hard_link_count")
? info.nNumberOfLinks
: 0;
# endif
}
BOOST_FILESYSTEM_DECL
path initial_path(error_code* ec)
{
static path init_path;
if (init_path.empty())
init_path = current_path(ec);
else if (ec != 0) ec->clear();
return init_path;
}
BOOST_FILESYSTEM_DECL
bool is_empty(const path& p, system::error_code* ec)
{
# ifdef BOOST_POSIX_API
struct stat path_stat;
if (error(::stat(p.c_str(), &path_stat)!= 0,
p, ec, "boost::filesystem::is_empty"))
return false;
return S_ISDIR(path_stat.st_mode)
? is_empty_directory(p, ec)
: path_stat.st_size == 0;
# else
WIN32_FILE_ATTRIBUTE_DATA fad;
if (error(::GetFileAttributesExW(p.c_str(), ::GetFileExInfoStandard, &fad)== 0
? BOOST_ERRNO : 0, p, ec, "boost::filesystem::is_empty"))
return false;
if (ec != 0) ec->clear();
return
(fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
? is_empty_directory(p, ec)
: (!fad.nFileSizeHigh && !fad.nFileSizeLow);
# endif
}
BOOST_FILESYSTEM_DECL
std::time_t last_write_time(const path& p, system::error_code* ec)
{
# ifdef BOOST_POSIX_API
struct stat path_stat;
if (error(::stat(p.c_str(), &path_stat)!= 0 ? BOOST_ERRNO : 0,
p, ec, "boost::filesystem::last_write_time"))
return std::time_t(-1);
return path_stat.st_mtime;
# else
handle_wrapper hw(
create_file_handle(p.c_str(), 0,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0));
if (error(hw.handle == INVALID_HANDLE_VALUE ? BOOST_ERRNO : 0,
p, ec, "boost::filesystem::last_write_time"))
return std::time_t(-1);
FILETIME lwt;
if (error(::GetFileTime(hw.handle, 0, 0, &lwt)== 0 ? BOOST_ERRNO : 0,
p, ec, "boost::filesystem::last_write_time"))
return std::time_t(-1);
return to_time_t(lwt);
# endif
}
BOOST_FILESYSTEM_DECL
void last_write_time(const path& p, const std::time_t new_time,
system::error_code* ec)
{
# ifdef BOOST_POSIX_API
struct stat path_stat;
if (error(::stat(p.c_str(), &path_stat)!= 0,
p, ec, "boost::filesystem::last_write_time"))
return;
::utimbuf buf;
buf.actime = path_stat.st_atime; // utime()updates access time too:-(
buf.modtime = new_time;
error(::utime(p.c_str(), &buf)!= 0 ? BOOST_ERRNO : 0,
p, ec, "boost::filesystem::last_write_time");
# else
handle_wrapper hw(
create_file_handle(p.c_str(), FILE_WRITE_ATTRIBUTES,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0));
if (error(hw.handle == INVALID_HANDLE_VALUE ? BOOST_ERRNO : 0,
p, ec, "boost::filesystem::last_write_time"))
return;
FILETIME lwt;
to_FILETIME(new_time, lwt);
error(::SetFileTime(hw.handle, 0, 0, &lwt)== 0 ? BOOST_ERRNO : 0,
p, ec, "boost::filesystem::last_write_time");
# endif
}
# ifdef BOOST_POSIX_API
const perms active_bits(all_all | set_uid_on_exe | set_gid_on_exe | sticky_bit);
inline mode_t mode_cast(perms prms) { return prms & active_bits; }
# endif
BOOST_FILESYSTEM_DECL
void permissions(const path& p, perms prms, system::error_code* ec)
{
BOOST_ASSERT_MSG(!((prms & add_perms) && (prms & remove_perms)),
"add_perms and remove_perms are mutually exclusive");
if ((prms & add_perms) && (prms & remove_perms)) // precondition failed
return;
# ifdef BOOST_POSIX_API
error_code local_ec;
file_status current_status((prms & symlink_perms)
? fs::symlink_status(p, local_ec)
: fs::status(p, local_ec));
if (local_ec)
{
if (ec == 0)
BOOST_FILESYSTEM_THROW(filesystem_error(
"boost::filesystem::permissions", p, local_ec));
else
*ec = local_ec;
return;
}
if (prms & add_perms)
prms |= current_status.permissions();
else if (prms & remove_perms)
prms = current_status.permissions() & ~prms;
// OS X <10.10, iOS <8.0 and some other platforms don't support fchmodat().
// Solaris (SunPro and gcc) only support fchmodat() on Solaris 11 and higher,
// and a runtime check is too much trouble.
// Linux does not support permissions on symbolic links and has no plans to
// support them in the future. The chmod() code is thus more practical,
// rather than always hitting ENOTSUP when sending in AT_SYMLINK_NO_FOLLOW.
// - See the 3rd paragraph of
// "Symbolic link ownership, permissions, and timestamps" at:
// "http://man7.org/linux/man-pages/man7/symlink.7.html"
// - See the fchmodat() Linux man page:
// "http://man7.org/linux/man-pages/man2/fchmodat.2.html"
# if defined(AT_FDCWD) && defined(AT_SYMLINK_NOFOLLOW) \
&& !(defined(__SUNPRO_CC) || defined(__sun) || defined(sun)) \
&& !(defined(linux) || defined(__linux) || defined(__linux__)) \
&& !(defined(__MAC_OS_X_VERSION_MIN_REQUIRED) \
&& __MAC_OS_X_VERSION_MIN_REQUIRED < 101000) \
&& !(defined(__IPHONE_OS_VERSION_MIN_REQUIRED) \
&& __IPHONE_OS_VERSION_MIN_REQUIRED < 80000)
if (::fchmodat(AT_FDCWD, p.c_str(), mode_cast(prms),
!(prms & symlink_perms) ? 0 : AT_SYMLINK_NOFOLLOW))
# else // fallback if fchmodat() not supported
if (::chmod(p.c_str(), mode_cast(prms)))
# endif
{
if (ec == 0)
BOOST_FILESYSTEM_THROW(filesystem_error(
"boost::filesystem::permissions", p,
error_code(errno, system::generic_category())));
else
ec->assign(errno, system::generic_category());
}
# else // Windows
// if not going to alter FILE_ATTRIBUTE_READONLY, just return
if (!(!((prms & (add_perms | remove_perms)))
|| (prms & (owner_write|group_write|others_write))))
return;
DWORD attr = ::GetFileAttributesW(p.c_str());
if (error(attr == 0 ? BOOST_ERRNO : 0, p, ec, "boost::filesystem::permissions"))
return;
if (prms & add_perms)
attr &= ~FILE_ATTRIBUTE_READONLY;
else if (prms & remove_perms)
attr |= FILE_ATTRIBUTE_READONLY;
else if (prms & (owner_write|group_write|others_write))
attr &= ~FILE_ATTRIBUTE_READONLY;
else
attr |= FILE_ATTRIBUTE_READONLY;
error(::SetFileAttributesW(p.c_str(), attr) == 0 ? BOOST_ERRNO : 0,
p, ec, "boost::filesystem::permissions");
# endif
}
BOOST_FILESYSTEM_DECL
path read_symlink(const path& p, system::error_code* ec)
{
path symlink_path;
# ifdef BOOST_POSIX_API
for (std::size_t path_max = 64;; path_max *= 2)// loop 'til buffer large enough
{
boost::scoped_array<char> buf(new char[path_max]);
ssize_t result;
if ((result=::readlink(p.c_str(), buf.get(), path_max))== -1)
{
if (ec == 0)
BOOST_FILESYSTEM_THROW(filesystem_error("boost::filesystem::read_symlink",
p, error_code(errno, system_category())));
else ec->assign(errno, system_category());
break;
}
else
{
if(result != static_cast<ssize_t>(path_max))
{
symlink_path.assign(buf.get(), buf.get() + result);
if (ec != 0) ec->clear();
break;
}
}
}
# elif _WIN32_WINNT < 0x0600 // SDK earlier than Vista and Server 2008
error(BOOST_ERROR_NOT_SUPPORTED, p, ec,
"boost::filesystem::read_symlink");
# else // Vista and Server 2008 SDK, or later
union info_t
{
char buf[REPARSE_DATA_BUFFER_HEADER_SIZE+MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
REPARSE_DATA_BUFFER rdb;
} info;
handle_wrapper h(
create_file_handle(p.c_str(), GENERIC_READ, 0, 0, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, 0));
if (error(h.handle == INVALID_HANDLE_VALUE ? BOOST_ERRNO : 0,
p, ec, "boost::filesystem::read_symlink"))
return symlink_path;
DWORD sz;
if (!error(::DeviceIoControl(h.handle, FSCTL_GET_REPARSE_POINT,
0, 0, info.buf, sizeof(info), &sz, 0) == 0 ? BOOST_ERRNO : 0, p, ec,
"boost::filesystem::read_symlink" ))
symlink_path.assign(
static_cast<wchar_t*>(info.rdb.SymbolicLinkReparseBuffer.PathBuffer)
+ info.rdb.SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(wchar_t),
static_cast<wchar_t*>(info.rdb.SymbolicLinkReparseBuffer.PathBuffer)
+ info.rdb.SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(wchar_t)
+ info.rdb.SymbolicLinkReparseBuffer.PrintNameLength/sizeof(wchar_t));
# endif
return symlink_path;
}
BOOST_FILESYSTEM_DECL
path relative(const path& p, const path& base, error_code* ec)
{
error_code tmp_ec;
path wc_base(weakly_canonical(base, &tmp_ec));
if (error(tmp_ec.value(), base, ec, "boost::filesystem::relative"))
return path();
path wc_p(weakly_canonical(p, &tmp_ec));
if (error(tmp_ec.value(), base, ec, "boost::filesystem::relative"))
return path();
return wc_p.lexically_relative(wc_base);
}
BOOST_FILESYSTEM_DECL
bool remove(const path& p, error_code* ec)
{
error_code tmp_ec;
file_type type = query_file_type(p, &tmp_ec);
if (error(type == status_error ? tmp_ec.value() : 0, p, ec,
"boost::filesystem::remove"))
return false;
// Since POSIX remove() is specified to work with either files or directories, in a
// perfect world it could just be called. But some important real-world operating
// systems (Windows, Mac OS X, for example) don't implement the POSIX spec. So
// remove_file_or_directory() is always called to keep it simple.
return remove_file_or_directory(p, type, ec);
}
BOOST_FILESYSTEM_DECL
boost::uintmax_t remove_all(const path& p, error_code* ec)
{
error_code tmp_ec;
file_type type = query_file_type(p, &tmp_ec);
if (error(type == status_error ? tmp_ec.value() : 0, p, ec,
"boost::filesystem::remove_all"))
return 0;
return (type != status_error && type != file_not_found) // exists
? remove_all_aux(p, type, ec)
: 0;
}
BOOST_FILESYSTEM_DECL
void rename(const path& old_p, const path& new_p, error_code* ec)
{
error(!BOOST_MOVE_FILE(old_p.c_str(), new_p.c_str()) ? BOOST_ERRNO : 0, old_p, new_p,
ec, "boost::filesystem::rename");
}
BOOST_FILESYSTEM_DECL
void resize_file(const path& p, uintmax_t size, system::error_code* ec)
{
# if defined(BOOST_POSIX_API)
if (BOOST_UNLIKELY(size > static_cast< uintmax_t >((std::numeric_limits< off_t >::max)()))) {
error(system::errc::file_too_large, p, ec, "boost::filesystem::resize_file");
return;
}
# endif
error(!BOOST_RESIZE_FILE(p.c_str(), size) ? BOOST_ERRNO : 0, p, ec,
"boost::filesystem::resize_file");
}
BOOST_FILESYSTEM_DECL
space_info space(const path& p, error_code* ec)
{
# ifdef BOOST_POSIX_API
struct BOOST_STATVFS vfs;
space_info info;
if (!error(::BOOST_STATVFS(p.c_str(), &vfs) ? BOOST_ERRNO : 0,
p, ec, "boost::filesystem::space"))
{
info.capacity
= static_cast<boost::uintmax_t>(vfs.f_blocks)* BOOST_STATVFS_F_FRSIZE;
info.free
= static_cast<boost::uintmax_t>(vfs.f_bfree)* BOOST_STATVFS_F_FRSIZE;
info.available
= static_cast<boost::uintmax_t>(vfs.f_bavail)* BOOST_STATVFS_F_FRSIZE;
}
# else
ULARGE_INTEGER avail, total, free;
space_info info;
if (!error(::GetDiskFreeSpaceExW(p.c_str(), &avail, &total, &free)== 0,
p, ec, "boost::filesystem::space"))
{
info.capacity
= (static_cast<boost::uintmax_t>(total.HighPart)<< 32)
+ total.LowPart;
info.free
= (static_cast<boost::uintmax_t>(free.HighPart)<< 32)
+ free.LowPart;
info.available
= (static_cast<boost::uintmax_t>(avail.HighPart)<< 32)
+ avail.LowPart;
}
# endif
else
{
info.capacity = info.free = info.available = 0;
}
return info;
}
BOOST_FILESYSTEM_DECL
file_status status(const path& p, error_code* ec)
{
# ifdef BOOST_POSIX_API
struct stat path_stat;
if (::stat(p.c_str(), &path_stat)!= 0)
{
if (ec != 0) // always report errno, even though some
ec->assign(errno, system_category()); // errno values are not status_errors
if (not_found_error(errno))
{
return fs::file_status(fs::file_not_found, fs::no_perms);
}
if (ec == 0)
BOOST_FILESYSTEM_THROW(filesystem_error("boost::filesystem::status",
p, error_code(errno, system_category())));
return fs::file_status(fs::status_error);
}
if (ec != 0) ec->clear();;
if (S_ISDIR(path_stat.st_mode))
return fs::file_status(fs::directory_file,
static_cast<perms>(path_stat.st_mode) & fs::perms_mask);
if (S_ISREG(path_stat.st_mode))
return fs::file_status(fs::regular_file,
static_cast<perms>(path_stat.st_mode) & fs::perms_mask);
if (S_ISBLK(path_stat.st_mode))
return fs::file_status(fs::block_file,
static_cast<perms>(path_stat.st_mode) & fs::perms_mask);
if (S_ISCHR(path_stat.st_mode))
return fs::file_status(fs::character_file,
static_cast<perms>(path_stat.st_mode) & fs::perms_mask);
if (S_ISFIFO(path_stat.st_mode))
return fs::file_status(fs::fifo_file,
static_cast<perms>(path_stat.st_mode) & fs::perms_mask);
if (S_ISSOCK(path_stat.st_mode))
return fs::file_status(fs::socket_file,
static_cast<perms>(path_stat.st_mode) & fs::perms_mask);
return fs::file_status(fs::type_unknown);
# else // Windows
DWORD attr(::GetFileAttributesW(p.c_str()));
if (attr == 0xFFFFFFFF)
{
return process_status_failure(p, ec);
}
// reparse point handling;
// since GetFileAttributesW does not resolve symlinks, try to open a file
// handle to discover if the file exists
if (attr & FILE_ATTRIBUTE_REPARSE_POINT)
{
handle_wrapper h(
create_file_handle(
p.c_str(),
0, // dwDesiredAccess; attributes only
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
0, // lpSecurityAttributes
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
0)); // hTemplateFile
if (h.handle == INVALID_HANDLE_VALUE)
{
return process_status_failure(p, ec);
}
if (!is_reparse_point_a_symlink(p))
return file_status(reparse_file, make_permissions(p, attr));
}
if (ec != 0) ec->clear();
return (attr & FILE_ATTRIBUTE_DIRECTORY)
? file_status(directory_file, make_permissions(p, attr))
: file_status(regular_file, make_permissions(p, attr));
# endif
}
BOOST_FILESYSTEM_DECL
file_status symlink_status(const path& p, error_code* ec)
{
# ifdef BOOST_POSIX_API
struct stat path_stat;
if (::lstat(p.c_str(), &path_stat)!= 0)
{
if (ec != 0) // always report errno, even though some
ec->assign(errno, system_category()); // errno values are not status_errors
if (errno == ENOENT || errno == ENOTDIR) // these are not errors
{
return fs::file_status(fs::file_not_found, fs::no_perms);
}
if (ec == 0)
BOOST_FILESYSTEM_THROW(filesystem_error("boost::filesystem::status",
p, error_code(errno, system_category())));
return fs::file_status(fs::status_error);
}
if (ec != 0) ec->clear();
if (S_ISREG(path_stat.st_mode))
return fs::file_status(fs::regular_file,
static_cast<perms>(path_stat.st_mode) & fs::perms_mask);
if (S_ISDIR(path_stat.st_mode))
return fs::file_status(fs::directory_file,
static_cast<perms>(path_stat.st_mode) & fs::perms_mask);
if (S_ISLNK(path_stat.st_mode))
return fs::file_status(fs::symlink_file,
static_cast<perms>(path_stat.st_mode) & fs::perms_mask);
if (S_ISBLK(path_stat.st_mode))
return fs::file_status(fs::block_file,
static_cast<perms>(path_stat.st_mode) & fs::perms_mask);
if (S_ISCHR(path_stat.st_mode))
return fs::file_status(fs::character_file,
static_cast<perms>(path_stat.st_mode) & fs::perms_mask);
if (S_ISFIFO(path_stat.st_mode))
return fs::file_status(fs::fifo_file,
static_cast<perms>(path_stat.st_mode) & fs::perms_mask);
if (S_ISSOCK(path_stat.st_mode))
return fs::file_status(fs::socket_file,
static_cast<perms>(path_stat.st_mode) & fs::perms_mask);
return fs::file_status(fs::type_unknown);
# else // Windows
DWORD attr(::GetFileAttributesW(p.c_str()));
if (attr == 0xFFFFFFFF)
{
return process_status_failure(p, ec);
}
if (ec != 0) ec->clear();
if (attr & FILE_ATTRIBUTE_REPARSE_POINT)
return is_reparse_point_a_symlink(p)
? file_status(symlink_file, make_permissions(p, attr))
: file_status(reparse_file, make_permissions(p, attr));
return (attr & FILE_ATTRIBUTE_DIRECTORY)
? file_status(directory_file, make_permissions(p, attr))
: file_status(regular_file, make_permissions(p, attr));
# endif
}
// contributed by Jeff Flinn
BOOST_FILESYSTEM_DECL
path temp_directory_path(system::error_code* ec)
{
# ifdef BOOST_POSIX_API
const char* val = 0;
(val = std::getenv("TMPDIR" )) ||
(val = std::getenv("TMP" )) ||
(val = std::getenv("TEMP" )) ||
(val = std::getenv("TEMPDIR"));
# ifdef __ANDROID__
const char* default_tmp = "/data/local/tmp";
# else
const char* default_tmp = "/tmp";
# endif
path p((val!=0) ? val : default_tmp);
if (p.empty() || (ec&&!is_directory(p, *ec))||(!ec&&!is_directory(p)))
{
error(ENOTDIR, p, ec, "boost::filesystem::temp_directory_path");
return p;
}
return p;
# else // Windows
const wchar_t* tmp_env = L"TMP";
const wchar_t* temp_env = L"TEMP";
const wchar_t* localappdata_env = L"LOCALAPPDATA";
const wchar_t* userprofile_env = L"USERPROFILE";
const wchar_t* env_list[]
= {tmp_env, temp_env, localappdata_env, userprofile_env, 0};
path p;
for (int i = 0; env_list[i]; ++i)
{
std::wstring env = wgetenv(env_list[i]);
if (!env.empty())
{
p = env;
if (i >= 2)
p /= L"Temp";
error_code lcl_ec;
if (exists(p, lcl_ec) && !lcl_ec && is_directory(p, lcl_ec) && !lcl_ec)
break;
p.clear();
}
}
if (p.empty())
{
// use vector since in C++03 a string is not required to be contiguous
std::vector<wchar_t> buf(::GetWindowsDirectoryW(NULL, 0));
if (buf.empty()
|| ::GetWindowsDirectoryW(&buf[0], static_cast<UINT>(buf.size())) == 0)
{
error(::GetLastError(), ec, "boost::filesystem::temp_directory_path");
return path();
}
p = &*buf.begin(); // do not depend on buf.size(); see ticket #10388
p /= L"Temp";
}
return p;
# endif
}
BOOST_FILESYSTEM_DECL
path system_complete(const path& p, system::error_code* ec)
{
# ifdef BOOST_POSIX_API
return (p.empty() || p.is_absolute())
? p : current_path()/ p;
# else
if (p.empty())
{
if (ec != 0) ec->clear();
return p;
}
wchar_t buf[buf_size];
wchar_t* pfn;
std::size_t len = get_full_path_name(p, buf_size, buf, &pfn);
if (error(len == 0 ? BOOST_ERRNO : 0, p, ec, "boost::filesystem::system_complete"))
return path();
if (len < buf_size)// len does not include null termination character
return path(&buf[0]);
boost::scoped_array<wchar_t> big_buf(new wchar_t[len]);
return error(get_full_path_name(p, len , big_buf.get(), &pfn)== 0 ? BOOST_ERRNO : 0,
p, ec, "boost::filesystem::system_complete")
? path()
: path(big_buf.get());
# endif
}
BOOST_FILESYSTEM_DECL
path weakly_canonical(const path& p, system::error_code* ec)
{
path head(p);
path tail;
system::error_code tmp_ec;
path::iterator itr = p.end();
for (; !head.empty(); --itr)
{
file_status head_status = status(head, tmp_ec);
if (error(head_status.type() == fs::status_error,
head, ec, "boost::filesystem::weakly_canonical"))
return path();
if (head_status.type() != fs::file_not_found)
break;
head.remove_filename();
}
bool tail_has_dots = false;
for (; itr != p.end(); ++itr)
{
tail /= *itr;
// for a later optimization, track if any dot or dot-dot elements are present
if (itr->native().size() <= 2
&& itr->native()[0] == dot
&& (itr->native().size() == 1 || itr->native()[1] == dot))
tail_has_dots = true;
}
if (head.empty())
return p.lexically_normal();
head = canonical(head, tmp_ec);
if (error(tmp_ec.value(), head, ec, "boost::filesystem::weakly_canonical"))
return path();
return tail.empty()
? head
: (tail_has_dots // optimization: only normalize if tail had dot or dot-dot element
? (head/tail).lexically_normal()
: head/tail);
}
} // namespace detail
//--------------------------------------------------------------------------------------//
// //
// directory_entry //
// //
//--------------------------------------------------------------------------------------//
file_status
directory_entry::m_get_status(system::error_code* ec) const
{
if (!status_known(m_status))
{
// optimization: if the symlink status is known, and it isn't a symlink,
// then status and symlink_status are identical so just copy the
// symlink status to the regular status.
if (status_known(m_symlink_status)
&& !is_symlink(m_symlink_status))
{
m_status = m_symlink_status;
if (ec != 0) ec->clear();
}
else m_status = detail::status(m_path, ec);
}
else if (ec != 0) ec->clear();
return m_status;
}
file_status
directory_entry::m_get_symlink_status(system::error_code* ec) const
{
if (!status_known(m_symlink_status))
m_symlink_status = detail::symlink_status(m_path, ec);
else if (ec != 0) ec->clear();
return m_symlink_status;
}
// dispatch directory_entry supplied here rather than in
// <boost/filesystem/path_traits.hpp>, thus avoiding header circularity.
// test cases are in operations_unit_test.cpp
namespace path_traits
{
void dispatch(const directory_entry & de,
# ifdef BOOST_WINDOWS_API
std::wstring& to,
# else
std::string& to,
# endif
const codecvt_type &)
{
to = de.path().native();
}
void dispatch(const directory_entry & de,
# ifdef BOOST_WINDOWS_API
std::wstring& to
# else
std::string& to
# endif
)
{
to = de.path().native();
}
} // namespace path_traits
} // namespace filesystem
} // namespace boost
//--------------------------------------------------------------------------------------//
// //
// directory_iterator //
// //
//--------------------------------------------------------------------------------------//
namespace
{
# ifdef BOOST_POSIX_API
error_code path_max(std::size_t & result)
// this code is based on Stevens and Rago, Advanced Programming in the
// UNIX envirnment, 2nd Ed., ISBN 0-201-43307-9, page 49
{
# ifdef PATH_MAX
static std::size_t max = PATH_MAX;
# else
static std::size_t max = 0;
# endif
if (max == 0)
{
errno = 0;
long tmp = ::pathconf("/", _PC_NAME_MAX);
if (tmp < 0)
{
if (errno == 0)// indeterminate
max = 4096; // guess
else return error_code(errno, system_category());
}
else max = static_cast<std::size_t>(tmp + 1); // relative root
}
result = max;
return ok;
}
#if defined(__PGI) && defined(__USE_FILE_OFFSET64)
#define dirent dirent64
#endif
error_code dir_itr_first(void *& handle, void *& buffer,
const char* dir, string& target,
fs::file_status &, fs::file_status &)
{
if ((handle = ::opendir(dir))== 0)
return error_code(errno, system_category());
target = string("."); // string was static but caused trouble
// when iteration called from dtor, after
// static had already been destroyed
std::size_t path_size (0); // initialization quiets gcc warning (ticket #3509)
error_code ec = path_max(path_size);
if (ec)return ec;
dirent de;
buffer = std::malloc((sizeof(dirent) - sizeof(de.d_name))
+ path_size + 1); // + 1 for "/0"
return ok;
}
// warning: the only dirent member updated is d_name
inline int readdir_r_simulator(DIR * dirp, struct dirent * entry,
struct dirent ** result)// *result set to 0 on end of directory
{
# if !defined(__CYGWIN__)\
&& defined(_POSIX_THREAD_SAFE_FUNCTIONS)\
&& defined(_SC_THREAD_SAFE_FUNCTIONS)\
&& (_POSIX_THREAD_SAFE_FUNCTIONS+0 >= 0)\
&& (!defined(__hpux) || defined(_REENTRANT)) \
&& (!defined(_AIX) || defined(__THREAD_SAFE))
errno = 0;
if (::sysconf(_SC_THREAD_SAFE_FUNCTIONS)>= 0)
{ return ::readdir_r(dirp, entry, result); }
# endif
errno = 0;
struct dirent * p;
*result = 0;
if ((p = ::readdir(dirp))== 0)
return errno;
std::strcpy(entry->d_name, p->d_name);
*result = entry;
return 0;
}
error_code dir_itr_increment(void *& handle, void *& buffer,
string& target, fs::file_status & sf, fs::file_status & symlink_sf)
{
BOOST_ASSERT(buffer != 0);
dirent * entry(static_cast<dirent *>(buffer));
dirent * result;
int return_code;
if ((return_code = readdir_r_simulator(static_cast<DIR*>(handle), entry, &result))!= 0)
return error_code(errno, system_category());
if (result == 0)
return fs::detail::dir_itr_close(handle, buffer);
target = entry->d_name;
# ifdef BOOST_FILESYSTEM_STATUS_CACHE
if (entry->d_type == DT_UNKNOWN) // filesystem does not supply d_type value
{
sf = symlink_sf = fs::file_status(fs::status_error);
}
else // filesystem supplies d_type value
{
if (entry->d_type == DT_DIR)
sf = symlink_sf = fs::file_status(fs::directory_file);
else if (entry->d_type == DT_REG)
sf = symlink_sf = fs::file_status(fs::regular_file);
else if (entry->d_type == DT_LNK)
{
sf = fs::file_status(fs::status_error);
symlink_sf = fs::file_status(fs::symlink_file);
}
else sf = symlink_sf = fs::file_status(fs::status_error);
}
# else
sf = symlink_sf = fs::file_status(fs::status_error);
# endif
return ok;
}
# else // BOOST_WINDOWS_API
error_code dir_itr_first(void *& handle, const fs::path& dir,
wstring& target, fs::file_status & sf, fs::file_status & symlink_sf)
// Note: an empty root directory has no "." or ".." entries, so this
// causes a ERROR_FILE_NOT_FOUND error which we do not considered an
// error. It is treated as eof instead.
{
// use a form of search Sebastian Martel reports will work with Win98
wstring dirpath(dir.wstring());
dirpath += (dirpath.empty()
|| (dirpath[dirpath.size()-1] != L'\\'
&& dirpath[dirpath.size()-1] != L'/'
&& dirpath[dirpath.size()-1] != L':'))? L"\\*" : L"*";
WIN32_FIND_DATAW data;
if ((handle = ::FindFirstFileW(dirpath.c_str(), &data))
== INVALID_HANDLE_VALUE)
{
handle = 0; // signal eof
return error_code( (::GetLastError() == ERROR_FILE_NOT_FOUND
// Windows Mobile returns ERROR_NO_MORE_FILES; see ticket #3551
|| ::GetLastError() == ERROR_NO_MORE_FILES)
? 0 : ::GetLastError(), system_category() );
}
target = data.cFileName;
if (data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
// reparse points are complex, so don't try to handle them here; instead just mark
// them as status_error which causes directory_entry caching to call status()
// and symlink_status() which do handle reparse points fully
{
sf.type(fs::status_error);
symlink_sf.type(fs::status_error);
}
else
{
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
sf.type(fs::directory_file);
symlink_sf.type(fs::directory_file);
}
else
{
sf.type(fs::regular_file);
symlink_sf.type(fs::regular_file);
}
sf.permissions(make_permissions(data.cFileName, data.dwFileAttributes));
symlink_sf.permissions(sf.permissions());
}
return error_code();
}
error_code dir_itr_increment(void *& handle, wstring& target,
fs::file_status & sf, fs::file_status & symlink_sf)
{
WIN32_FIND_DATAW data;
if (::FindNextFileW(handle, &data)== 0)// fails
{
int error = ::GetLastError();
fs::detail::dir_itr_close(handle);
return error_code(error == ERROR_NO_MORE_FILES ? 0 : error, system_category());
}
target = data.cFileName;
if (data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
// reparse points are complex, so don't try to handle them here; instead just mark
// them as status_error which causes directory_entry caching to call status()
// and symlink_status() which do handle reparse points fully
{
sf.type(fs::status_error);
symlink_sf.type(fs::status_error);
}
else
{
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
sf.type(fs::directory_file);
symlink_sf.type(fs::directory_file);
}
else
{
sf.type(fs::regular_file);
symlink_sf.type(fs::regular_file);
}
sf.permissions(make_permissions(data.cFileName, data.dwFileAttributes));
symlink_sf.permissions(sf.permissions());
}
return error_code();
}
#endif
const error_code not_found_error_code (
# ifdef BOOST_WINDOWS_API
ERROR_PATH_NOT_FOUND
# else
ENOENT
# endif
, system_category());
} // unnamed namespace
namespace boost
{
namespace filesystem
{
namespace detail
{
// dir_itr_close is called both from the ~dir_itr_imp()destructor
// and dir_itr_increment()
BOOST_FILESYSTEM_DECL
system::error_code dir_itr_close( // never throws
void *& handle
# if defined(BOOST_POSIX_API)
, void *& buffer
# endif
)
{
# ifdef BOOST_POSIX_API
std::free(buffer);
buffer = 0;
if (handle == 0)return ok;
DIR * h(static_cast<DIR*>(handle));
handle = 0;
return error_code(::closedir(h)== 0 ? 0 : errno, system_category());
# else
if (handle != 0)
{
::FindClose(handle);
handle = 0;
}
return ok;
# endif
}
void directory_iterator_construct(directory_iterator& it,
const path& p, system::error_code* ec)
{
if (error(p.empty() ? not_found_error_code.value() : 0, p, ec,
"boost::filesystem::directory_iterator::construct"))
return;
path::string_type filename;
file_status file_stat, symlink_file_stat;
error_code result = dir_itr_first(it.m_imp->handle,
# if defined(BOOST_POSIX_API)
it.m_imp->buffer,
# endif
p.c_str(), filename, file_stat, symlink_file_stat);
if (result)
{
it.m_imp.reset();
error(result.value(), p,
ec, "boost::filesystem::directory_iterator::construct");
return;
}
if (it.m_imp->handle == 0)
it.m_imp.reset(); // eof, so make end iterator
else // not eof
{
it.m_imp->dir_entry.assign(p / filename, file_stat, symlink_file_stat);
if (filename[0] == dot // dot or dot-dot
&& (filename.size()== 1
|| (filename[1] == dot
&& filename.size()== 2)))
{
detail::directory_iterator_increment(it, ec);
}
}
}
void directory_iterator_increment(directory_iterator& it,
system::error_code* ec)
{
BOOST_ASSERT_MSG(it.m_imp.get(), "attempt to increment end iterator");
BOOST_ASSERT_MSG(it.m_imp->handle != 0, "internal program error");
path::string_type filename;
file_status file_stat, symlink_file_stat;
system::error_code temp_ec;
for (;;)
{
temp_ec = dir_itr_increment(it.m_imp->handle,
# if defined(BOOST_POSIX_API)
it.m_imp->buffer,
# endif
filename, file_stat, symlink_file_stat);
if (temp_ec) // happens if filesystem is corrupt, such as on a damaged optical disc
{
path error_path(it.m_imp->dir_entry.path().parent_path()); // fix ticket #5900
it.m_imp.reset();
if (ec == 0)
BOOST_FILESYSTEM_THROW(
filesystem_error("boost::filesystem::directory_iterator::operator++",
error_path,
error_code(BOOST_ERRNO, system_category())));
ec->assign(BOOST_ERRNO, system_category());
return;
}
else if (ec != 0) ec->clear();
if (it.m_imp->handle == 0) // eof, make end
{
it.m_imp.reset();
return;
}
if (!(filename[0] == dot // !(dot or dot-dot)
&& (filename.size()== 1
|| (filename[1] == dot
&& filename.size()== 2))))
{
it.m_imp->dir_entry.replace_filename(
filename, file_stat, symlink_file_stat);
return;
}
}
}
} // namespace detail
} // namespace filesystem
} // namespace boost
|