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
|
/*-
* Copyright (c) 2003-2009 Tim Kientzle
* Copyright (c) 2010,2011 Michihiro NAKAJIMA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer
* in this position and unchanged.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* This is the tree-walking code for POSIX systems. */
#if !defined(_WIN32) || defined(__CYGWIN__)
#include "archive_platform.h"
__FBSDID("$FreeBSD$");
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#endif
#ifdef HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_LINUX_MAGIC_H
#include <linux/magic.h>
#endif
#ifdef HAVE_DIRECT_H
#include <direct.h>
#endif
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "archive.h"
#include "archive_string.h"
#include "archive_entry.h"
#include "archive_private.h"
#include "archive_read_disk_private.h"
#ifndef HAVE_FCHDIR
#error fchdir function required.
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
/*-
* This is a new directory-walking system that addresses a number
* of problems I've had with fts(3). In particular, it has no
* pathname-length limits (other than the size of 'int'), handles
* deep logical traversals, uses considerably less memory, and has
* an opaque interface (easier to modify in the future).
*
* Internally, it keeps a single list of "tree_entry" items that
* represent filesystem objects that require further attention.
* Non-directories are not kept in memory: they are pulled from
* readdir(), returned to the client, then freed as soon as possible.
* Any directory entry to be traversed gets pushed onto the stack.
*
* There is surprisingly little information that needs to be kept for
* each item on the stack. Just the name, depth (represented here as the
* string length of the parent directory's pathname), and some markers
* indicating how to get back to the parent (via chdir("..") for a
* regular dir or via fchdir(2) for a symlink).
*/
/*
* TODO:
* 1) Loop checking.
* 3) Arbitrary logical traversals by closing/reopening intermediate fds.
*/
struct restore_time {
const char *name;
time_t mtime;
long mtime_nsec;
time_t atime;
long atime_nsec;
mode_t filetype;
int noatime;
};
struct tree_entry {
int depth;
struct tree_entry *next;
struct tree_entry *parent;
struct archive_string name;
size_t dirname_length;
int64_t dev;
int64_t ino;
int flags;
int filesystem_id;
/* How to return back to the parent of a symlink. */
int symlink_parent_fd;
/* How to restore time of a directory. */
struct restore_time restore_time;
};
struct filesystem {
int64_t dev;
int synthetic;
int remote;
int noatime;
#if defined(HAVE_READDIR_R)
size_t name_max;
#endif
long incr_xfer_size;
long max_xfer_size;
long min_xfer_size;
long xfer_align;
/*
* Buffer used for reading file contents.
*/
/* Exactly allocated memory pointer. */
unsigned char *allocation_ptr;
/* Pointer adjusted to the filesystem alignment . */
unsigned char *buff;
size_t buff_size;
};
/* Definitions for tree_entry.flags bitmap. */
#define isDir 1 /* This entry is a regular directory. */
#define isDirLink 2 /* This entry is a symbolic link to a directory. */
#define needsFirstVisit 4 /* This is an initial entry. */
#define needsDescent 8 /* This entry needs to be previsited. */
#define needsOpen 16 /* This is a directory that needs to be opened. */
#define needsAscent 32 /* This entry needs to be postvisited. */
/*
* Local data for this package.
*/
struct tree {
struct tree_entry *stack;
struct tree_entry *current;
DIR *d;
#define INVALID_DIR_HANDLE NULL
struct dirent *de;
#if defined(HAVE_READDIR_R)
struct dirent *dirent;
size_t dirent_allocated;
#endif
int flags;
int visit_type;
/* Error code from last failed operation. */
int tree_errno;
/* Dynamically-sized buffer for holding path */
struct archive_string path;
/* Last path element */
const char *basename;
/* Leading dir length */
size_t dirname_length;
int depth;
int openCount;
int maxOpenCount;
int initial_dir_fd;
int working_dir_fd;
struct stat lst;
struct stat st;
int descend;
int nlink;
/* How to restore time of a file. */
struct restore_time restore_time;
struct entry_sparse {
int64_t length;
int64_t offset;
} *sparse_list, *current_sparse;
int sparse_count;
int sparse_list_size;
char initial_symlink_mode;
char symlink_mode;
struct filesystem *current_filesystem;
struct filesystem *filesystem_table;
int current_filesystem_id;
int max_filesystem_id;
int allocated_filesytem;
int entry_fd;
int entry_eof;
int64_t entry_remaining_bytes;
int64_t entry_total;
unsigned char *entry_buff;
size_t entry_buff_size;
};
/* Definitions for tree.flags bitmap. */
#define hasStat 16 /* The st entry is valid. */
#define hasLstat 32 /* The lst entry is valid. */
#define onWorkingDir 64 /* We are on the working dir where we are
* reading directory entry at this time. */
#define needsRestoreTimes 128
static int
tree_dir_next_posix(struct tree *t);
#ifdef HAVE_DIRENT_D_NAMLEN
/* BSD extension; avoids need for a strlen() call. */
#define D_NAMELEN(dp) (dp)->d_namlen
#else
#define D_NAMELEN(dp) (strlen((dp)->d_name))
#endif
/* Initiate/terminate a tree traversal. */
static struct tree *tree_open(const char *, int, int);
static struct tree *tree_reopen(struct tree *, const char *, int);
static void tree_close(struct tree *);
static void tree_free(struct tree *);
static void tree_push(struct tree *, const char *, int, int64_t, int64_t,
struct restore_time *);
static int tree_enter_initial_dir(struct tree *);
static int tree_enter_working_dir(struct tree *);
static int tree_current_dir_fd(struct tree *);
/*
* tree_next() returns Zero if there is no next entry, non-zero if
* there is. Note that directories are visited three times.
* Directories are always visited first as part of enumerating their
* parent; that is a "regular" visit. If tree_descend() is invoked at
* that time, the directory is added to a work list and will
* subsequently be visited two more times: once just after descending
* into the directory ("postdescent") and again just after ascending
* back to the parent ("postascent").
*
* TREE_ERROR_DIR is returned if the descent failed (because the
* directory couldn't be opened, for instance). This is returned
* instead of TREE_POSTDESCENT/TREE_POSTASCENT. TREE_ERROR_DIR is not a
* fatal error, but it does imply that the relevant subtree won't be
* visited. TREE_ERROR_FATAL is returned for an error that left the
* traversal completely hosed. Right now, this is only returned for
* chdir() failures during ascent.
*/
#define TREE_REGULAR 1
#define TREE_POSTDESCENT 2
#define TREE_POSTASCENT 3
#define TREE_ERROR_DIR -1
#define TREE_ERROR_FATAL -2
static int tree_next(struct tree *);
/*
* Return information about the current entry.
*/
/*
* The current full pathname, length of the full pathname, and a name
* that can be used to access the file. Because tree does use chdir
* extensively, the access path is almost never the same as the full
* current path.
*
* TODO: On platforms that support it, use openat()-style operations
* to eliminate the chdir() operations entirely while still supporting
* arbitrarily deep traversals. This makes access_path troublesome to
* support, of course, which means we'll need a rich enough interface
* that clients can function without it. (In particular, we'll need
* tree_current_open() that returns an open file descriptor.)
*
*/
static const char *tree_current_path(struct tree *);
static const char *tree_current_access_path(struct tree *);
/*
* Request the lstat() or stat() data for the current path. Since the
* tree package needs to do some of this anyway, and caches the
* results, you should take advantage of it here if you need it rather
* than make a redundant stat() or lstat() call of your own.
*/
static const struct stat *tree_current_stat(struct tree *);
static const struct stat *tree_current_lstat(struct tree *);
static int tree_current_is_symblic_link_target(struct tree *);
/* The following functions use tricks to avoid a certain number of
* stat()/lstat() calls. */
/* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */
static int tree_current_is_physical_dir(struct tree *);
/* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */
static int tree_current_is_dir(struct tree *);
static int update_current_filesystem(struct archive_read_disk *a,
int64_t dev);
static int setup_current_filesystem(struct archive_read_disk *);
static int tree_target_is_same_as_parent(struct tree *, const struct stat *);
static int _archive_read_disk_open(struct archive *, const char *);
static int _archive_read_free(struct archive *);
static int _archive_read_close(struct archive *);
static int _archive_read_data_block(struct archive *,
const void **, size_t *, int64_t *);
static int _archive_read_next_header2(struct archive *,
struct archive_entry *);
static const char *trivial_lookup_gname(void *, int64_t gid);
static const char *trivial_lookup_uname(void *, int64_t uid);
static int setup_sparse(struct archive_read_disk *, struct archive_entry *);
static int close_and_restore_time(int fd, struct tree *,
struct restore_time *);
static struct archive_vtable *
archive_read_disk_vtable(void)
{
static struct archive_vtable av;
static int inited = 0;
if (!inited) {
av.archive_free = _archive_read_free;
av.archive_close = _archive_read_close;
av.archive_read_data_block = _archive_read_data_block;
av.archive_read_next_header2 = _archive_read_next_header2;
inited = 1;
}
return (&av);
}
const char *
archive_read_disk_gname(struct archive *_a, int64_t gid)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_gname"))
return (NULL);
if (a->lookup_gname == NULL)
return (NULL);
return ((*a->lookup_gname)(a->lookup_gname_data, gid));
}
const char *
archive_read_disk_uname(struct archive *_a, int64_t uid)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_uname"))
return (NULL);
if (a->lookup_uname == NULL)
return (NULL);
return ((*a->lookup_uname)(a->lookup_uname_data, uid));
}
int
archive_read_disk_set_gname_lookup(struct archive *_a,
void *private_data,
const char * (*lookup_gname)(void *private, int64_t gid),
void (*cleanup_gname)(void *private))
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_set_gname_lookup");
if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
(a->cleanup_gname)(a->lookup_gname_data);
a->lookup_gname = lookup_gname;
a->cleanup_gname = cleanup_gname;
a->lookup_gname_data = private_data;
return (ARCHIVE_OK);
}
int
archive_read_disk_set_uname_lookup(struct archive *_a,
void *private_data,
const char * (*lookup_uname)(void *private, int64_t uid),
void (*cleanup_uname)(void *private))
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_set_uname_lookup");
if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
(a->cleanup_uname)(a->lookup_uname_data);
a->lookup_uname = lookup_uname;
a->cleanup_uname = cleanup_uname;
a->lookup_uname_data = private_data;
return (ARCHIVE_OK);
}
/*
* Create a new archive_read_disk object and initialize it with global state.
*/
struct archive *
archive_read_disk_new(void)
{
struct archive_read_disk *a;
a = (struct archive_read_disk *)malloc(sizeof(*a));
if (a == NULL)
return (NULL);
memset(a, 0, sizeof(*a));
a->archive.magic = ARCHIVE_READ_DISK_MAGIC;
a->archive.state = ARCHIVE_STATE_NEW;
a->archive.vtable = archive_read_disk_vtable();
a->lookup_uname = trivial_lookup_uname;
a->lookup_gname = trivial_lookup_gname;
a->entry_wd_fd = -1;
return (&a->archive);
}
static int
_archive_read_free(struct archive *_a)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
int r;
if (_a == NULL)
return (ARCHIVE_OK);
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
if (a->archive.state != ARCHIVE_STATE_CLOSED)
r = _archive_read_close(&a->archive);
else
r = ARCHIVE_OK;
tree_free(a->tree);
if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
(a->cleanup_gname)(a->lookup_gname_data);
if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
(a->cleanup_uname)(a->lookup_uname_data);
archive_string_free(&a->archive.error_string);
a->archive.magic = 0;
__archive_clean(&a->archive);
free(a);
return (r);
}
static int
_archive_read_close(struct archive *_a)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
if (a->archive.state != ARCHIVE_STATE_FATAL)
a->archive.state = ARCHIVE_STATE_CLOSED;
tree_close(a->tree);
return (ARCHIVE_OK);
}
static void
setup_symlink_mode(struct archive_read_disk *a, char symlink_mode,
int follow_symlinks)
{
a->symlink_mode = symlink_mode;
a->follow_symlinks = follow_symlinks;
if (a->tree != NULL) {
a->tree->initial_symlink_mode = a->symlink_mode;
a->tree->symlink_mode = a->symlink_mode;
}
}
int
archive_read_disk_set_symlink_logical(struct archive *_a)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_logical");
setup_symlink_mode(a, 'L', 1);
return (ARCHIVE_OK);
}
int
archive_read_disk_set_symlink_physical(struct archive *_a)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_physical");
setup_symlink_mode(a, 'P', 0);
return (ARCHIVE_OK);
}
int
archive_read_disk_set_symlink_hybrid(struct archive *_a)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_hybrid");
setup_symlink_mode(a, 'H', 1);/* Follow symlinks initially. */
return (ARCHIVE_OK);
}
int
archive_read_disk_set_atime_restored(struct archive *_a)
{
#ifndef HAVE_UTIMES
static int warning_done = 0;
#endif
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_restore_atime");
#ifdef HAVE_UTIMES
a->restore_time = 1;
if (a->tree != NULL)
a->tree->flags |= needsRestoreTimes;
return (ARCHIVE_OK);
#else
if (warning_done)
/* Warning was already emitted; suppress further warnings. */
return (ARCHIVE_OK);
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
"Cannot restore access time on this system");
warning_done = 1;
return (ARCHIVE_WARN);
#endif
}
/*
* Trivial implementations of gname/uname lookup functions.
* These are normally overridden by the client, but these stub
* versions ensure that we always have something that works.
*/
static const char *
trivial_lookup_gname(void *private_data, int64_t gid)
{
(void)private_data; /* UNUSED */
(void)gid; /* UNUSED */
return (NULL);
}
static const char *
trivial_lookup_uname(void *private_data, int64_t uid)
{
(void)private_data; /* UNUSED */
(void)uid; /* UNUSED */
return (NULL);
}
/*
* Allocate memory for the reading buffer adjusted to the filesystem
* alignment.
*/
static int
setup_suitable_read_buffer(struct archive_read_disk *a)
{
struct tree *t = a->tree;
struct filesystem *cf = t->current_filesystem;
size_t asize;
size_t s;
if (cf->allocation_ptr == NULL) {
/* If we couldn't get a filesystem alignment,
* we use 4096 as default value but we won't use
* O_DIRECT to open() and openat() operations. */
long xfer_align = (cf->xfer_align == -1)?4096:cf->xfer_align;
if (cf->max_xfer_size != -1)
asize = cf->max_xfer_size + xfer_align;
else {
long incr = cf->incr_xfer_size;
/* Some platform does not set a proper value to
* incr_xfer_size.*/
if (incr < 0)
incr = cf->min_xfer_size;
if (cf->min_xfer_size < 0) {
incr = xfer_align;
asize = xfer_align;
} else
asize = cf->min_xfer_size;
/* Increase a buffer size up to 64K bytes in
* a proper incremant size. */
while (asize < 1024*64)
asize += incr;
/* Take a margin to adjust to the filesystem
* alignment. */
asize += xfer_align;
}
cf->allocation_ptr = malloc(asize);
if (cf->allocation_ptr == NULL) {
archive_set_error(&a->archive, ENOMEM,
"Couldn't allocate memory");
a->archive.state = ARCHIVE_STATE_FATAL;
return (ARCHIVE_FATAL);
}
/*
* Calculate proper address for the filesystem.
*/
s = (uintptr_t)cf->allocation_ptr;
s %= xfer_align;
if (s > 0)
s = xfer_align - s;
/*
* Set a read buffer pointer in the proper alignment of
* the current filesystem.
*/
cf->buff = cf->allocation_ptr + s;
cf->buff_size = asize - xfer_align;
}
return (ARCHIVE_OK);
}
static int
_archive_read_data_block(struct archive *_a, const void **buff,
size_t *size, int64_t *offset)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
struct tree *t = a->tree;
int r;
ssize_t bytes;
size_t buffbytes;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
"archive_read_data_block");
if (t->entry_eof || t->entry_remaining_bytes <= 0) {
r = ARCHIVE_EOF;
goto abort_read_data;
}
/*
* Open the current file.
*/
if (t->entry_fd < 0) {
int flags = O_RDONLY | O_BINARY;
/*
* Eliminate or reduce cache effects if we can.
*
* Carefully consider this to be enabled.
*/
#if defined(O_DIRECT) && 0/* Disabled for now */
if (t->current_filesystem->xfer_align != -1 &&
t->nlink == 1)
flags |= O_DIRECT;
#endif
#if defined(O_NOATIME)
/*
* Linux has O_NOATIME flag; use it if we need.
*/
if ((t->flags & needsRestoreTimes) != 0 &&
t->restore_time.noatime == 0)
flags |= O_NOATIME;
do {
#endif
#ifdef HAVE_OPENAT
t->entry_fd = openat(tree_current_dir_fd(t),
tree_current_access_path(t), flags);
#else
tree_enter_working_dir(t);
t->entry_fd = open(tree_current_access_path(t), flags);
#endif
#if defined(O_NOATIME)
/*
* When we did open the file with O_NOATIME flag,
* if successful, set 1 to t->restore_time.noatime
* not to restore an atime of the file later.
* if failed by EPERM, retry it without O_NOATIME flag.
*/
if (flags & O_NOATIME) {
if (t->entry_fd >= 0)
t->restore_time.noatime = 1;
else if (errno == EPERM) {
flags &= ~O_NOATIME;
continue;
}
}
} while (0);
#endif
if (t->entry_fd < 0) {
archive_set_error(&a->archive, errno,
"Couldn't open %s", tree_current_path(t));
r = ARCHIVE_FAILED;
tree_enter_initial_dir(t);
goto abort_read_data;
}
tree_enter_initial_dir(t);
}
/*
* Allocate read buffer if not allocated.
*/
if (t->current_filesystem->allocation_ptr == NULL) {
r = setup_suitable_read_buffer(a);
if (r != ARCHIVE_OK) {
a->archive.state = ARCHIVE_STATE_FATAL;
goto abort_read_data;
}
}
t->entry_buff = t->current_filesystem->buff;
t->entry_buff_size = t->current_filesystem->buff_size;
buffbytes = t->entry_buff_size;
if (buffbytes > t->current_sparse->length)
buffbytes = t->current_sparse->length;
/*
* Skip hole.
* TODO: Should we consider t->current_filesystem->xfer_align?
*/
if (t->current_sparse->offset > t->entry_total) {
if (lseek(t->entry_fd,
(off_t)t->current_sparse->offset, SEEK_SET) < 0) {
archive_set_error(&a->archive, errno, "Seek error");
r = ARCHIVE_FATAL;
a->archive.state = ARCHIVE_STATE_FATAL;
goto abort_read_data;
}
bytes = t->current_sparse->offset - t->entry_total;
t->entry_remaining_bytes -= bytes;
t->entry_total += bytes;
}
/*
* Read file contents.
*/
if (buffbytes > 0) {
bytes = read(t->entry_fd, t->entry_buff, buffbytes);
if (bytes < 0) {
archive_set_error(&a->archive, errno, "Read error");
r = ARCHIVE_FATAL;
a->archive.state = ARCHIVE_STATE_FATAL;
goto abort_read_data;
}
} else
bytes = 0;
if (bytes == 0) {
/* Get EOF */
t->entry_eof = 1;
r = ARCHIVE_EOF;
goto abort_read_data;
}
*buff = t->entry_buff;
*size = bytes;
*offset = t->entry_total;
t->entry_total += bytes;
t->entry_remaining_bytes -= bytes;
if (t->entry_remaining_bytes == 0) {
/* Close the current file descriptor */
close_and_restore_time(t->entry_fd, t, &t->restore_time);
t->entry_fd = -1;
t->entry_eof = 1;
}
t->current_sparse->offset += bytes;
t->current_sparse->length -= bytes;
if (t->current_sparse->length == 0 && !t->entry_eof)
t->current_sparse++;
return (ARCHIVE_OK);
abort_read_data:
*buff = NULL;
*size = 0;
*offset = t->entry_total;
if (t->entry_fd >= 0) {
/* Close the current file descriptor */
close_and_restore_time(t->entry_fd, t, &t->restore_time);
t->entry_fd = -1;
}
return (r);
}
static int
_archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
struct tree *t;
const struct stat *st; /* info to use for this entry */
const struct stat *lst;/* lstat() information */
int descend, fd = -1, r;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
"archive_read_next_header2");
t = a->tree;
if (t->entry_fd >= 0) {
close_and_restore_time(t->entry_fd, t, &t->restore_time);
t->entry_fd = -1;
}
#if !(defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR))
/* Restore working directory. */
tree_enter_working_dir(t);
#endif
st = NULL;
lst = NULL;
do {
switch (tree_next(t)) {
case TREE_ERROR_FATAL:
archive_set_error(&a->archive, t->tree_errno,
"%s: Unable to continue traversing directory tree",
tree_current_path(t));
a->archive.state = ARCHIVE_STATE_FATAL;
tree_enter_initial_dir(t);
return (ARCHIVE_FATAL);
case TREE_ERROR_DIR:
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
"%s: Couldn't visit directory",
tree_current_path(t));
tree_enter_initial_dir(t);
return (ARCHIVE_FAILED);
case 0:
tree_enter_initial_dir(t);
return (ARCHIVE_EOF);
case TREE_POSTDESCENT:
case TREE_POSTASCENT:
break;
case TREE_REGULAR:
lst = tree_current_lstat(t);
if (lst == NULL) {
archive_set_error(&a->archive, errno,
"%s: Cannot stat",
tree_current_path(t));
tree_enter_initial_dir(t);
return (ARCHIVE_FAILED);
}
break;
}
} while (lst == NULL);
/*
* Distinguish 'L'/'P'/'H' symlink following.
*/
switch(t->symlink_mode) {
case 'H':
/* 'H': After the first item, rest like 'P'. */
t->symlink_mode = 'P';
/* 'H': First item (from command line) like 'L'. */
/* FALLTHROUGH */
case 'L':
/* 'L': Do descend through a symlink to dir. */
descend = tree_current_is_dir(t);
/* 'L': Follow symlinks to files. */
a->symlink_mode = 'L';
a->follow_symlinks = 1;
/* 'L': Archive symlinks as targets, if we can. */
st = tree_current_stat(t);
if (st != NULL && !tree_target_is_same_as_parent(t, st))
break;
/* If stat fails, we have a broken symlink;
* in that case, don't follow the link. */
/* FALLTHROUGH */
default:
/* 'P': Don't descend through a symlink to dir. */
descend = tree_current_is_physical_dir(t);
/* 'P': Don't follow symlinks to files. */
a->symlink_mode = 'P';
a->follow_symlinks = 0;
/* 'P': Archive symlinks as symlinks. */
st = lst;
break;
}
if (update_current_filesystem(a, st->st_dev) != ARCHIVE_OK) {
a->archive.state = ARCHIVE_STATE_FATAL;
tree_enter_initial_dir(t);
return (ARCHIVE_FATAL);
}
t->descend = descend;
archive_entry_set_pathname(entry, tree_current_path(t));
archive_entry_copy_sourcepath(entry, tree_current_access_path(t));
archive_entry_copy_stat(entry, st);
/* Save the times to be restored. */
t->restore_time.mtime = archive_entry_mtime(entry);
t->restore_time.mtime_nsec = archive_entry_mtime_nsec(entry);
t->restore_time.atime = archive_entry_atime(entry);
t->restore_time.atime_nsec = archive_entry_atime_nsec(entry);
t->restore_time.filetype = archive_entry_filetype(entry);
t->restore_time.noatime = t->current_filesystem->noatime;
#if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR)
/*
* Open the current file to freely gather its metadata anywhere in
* working directory.
* Note: A symbolic link file cannot be opened with O_NOFOLLOW.
*/
if (a->follow_symlinks || archive_entry_filetype(entry) != AE_IFLNK)
fd = openat(tree_current_dir_fd(t), tree_current_access_path(t),
O_RDONLY | O_NONBLOCK);
/* Restore working directory if openat() operation failed or
* the file is a symbolic link. */
if (fd < 0)
tree_enter_working_dir(t);
/* The current direcotry fd is needed at
* archive_read_disk_entry_from_file() function to read link data
* with readlinkat(). */
a->entry_wd_fd = tree_current_dir_fd(t);
#endif
/*
* Populate the archive_entry with metadata from the disk.
*/
r = archive_read_disk_entry_from_file(&(a->archive), entry, fd, st);
/* Close the file descriptor used for reding the current file
* metadata at archive_read_disk_entry_from_file(). */
if (fd >= 0)
close(fd);
/* Return to the initial directory. */
tree_enter_initial_dir(t);
archive_entry_copy_sourcepath(entry, tree_current_path(t));
/*
* EOF and FATAL are persistent at this layer. By
* modifying the state, we guarantee that future calls to
* read a header or read data will fail.
*/
switch (r) {
case ARCHIVE_EOF:
a->archive.state = ARCHIVE_STATE_EOF;
break;
case ARCHIVE_OK:
case ARCHIVE_WARN:
t->entry_total = 0;
if (archive_entry_filetype(entry) == AE_IFREG) {
t->nlink = archive_entry_nlink(entry);
t->entry_remaining_bytes = archive_entry_size(entry);
t->entry_eof = (t->entry_remaining_bytes == 0)? 1: 0;
if (!t->entry_eof &&
setup_sparse(a, entry) != ARCHIVE_OK)
return (ARCHIVE_FATAL);
} else {
t->entry_remaining_bytes = 0;
t->entry_eof = 1;
}
a->archive.state = ARCHIVE_STATE_DATA;
break;
case ARCHIVE_RETRY:
break;
case ARCHIVE_FATAL:
a->archive.state = ARCHIVE_STATE_FATAL;
break;
}
return (r);
}
static int
setup_sparse(struct archive_read_disk *a, struct archive_entry *entry)
{
struct tree *t = a->tree;
int64_t length, offset;
int i;
t->sparse_count = archive_entry_sparse_reset(entry);
if (t->sparse_count+1 > t->sparse_list_size) {
free(t->sparse_list);
t->sparse_list_size = t->sparse_count + 1;
t->sparse_list = malloc(sizeof(t->sparse_list[0]) *
t->sparse_list_size);
if (t->sparse_list == NULL) {
t->sparse_list_size = 0;
archive_set_error(&a->archive, ENOMEM,
"Can't allocate data");
a->archive.state = ARCHIVE_STATE_FATAL;
return (ARCHIVE_FATAL);
}
}
for (i = 0; i < t->sparse_count; i++) {
archive_entry_sparse_next(entry, &offset, &length);
t->sparse_list[i].offset = offset;
t->sparse_list[i].length = length;
}
if (i == 0) {
t->sparse_list[i].offset = 0;
t->sparse_list[i].length = archive_entry_size(entry);
} else {
t->sparse_list[i].offset = archive_entry_size(entry);
t->sparse_list[i].length = 0;
}
t->current_sparse = t->sparse_list;
return (ARCHIVE_OK);
}
/*
* Called by the client to mark the directory just returned from
* tree_next() as needing to be visited.
*/
int
archive_read_disk_descend(struct archive *_a)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
struct tree *t = a->tree;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
"archive_read_disk_descend");
if (t->visit_type != TREE_REGULAR || !t->descend) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
"Ignored the request descending the current object");
return (ARCHIVE_WARN);
}
if (tree_current_is_physical_dir(t)) {
tree_push(t, t->basename, t->current_filesystem_id,
t->lst.st_dev, t->lst.st_ino, &t->restore_time);
t->stack->flags |= isDir;
} else if (tree_current_is_dir(t)) {
tree_push(t, t->basename, t->current_filesystem_id,
t->st.st_dev, t->st.st_ino, &t->restore_time);
t->stack->flags |= isDirLink;
}
t->descend = 0;
return (ARCHIVE_OK);
}
int
archive_read_disk_open(struct archive *_a, const char *pathname)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
"archive_read_disk_open");
archive_clear_error(&a->archive);
return (_archive_read_disk_open(_a, pathname));
}
int
archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
struct archive_string path;
int ret;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
"archive_read_disk_open_w");
archive_clear_error(&a->archive);
/* Make a char string from a wchar_t string. */
archive_string_init(&path);
if (archive_string_append_from_wcs(&path, pathname,
wcslen(pathname)) != 0) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
"Can't convert a path to a char string");
a->archive.state = ARCHIVE_STATE_FATAL;
ret = ARCHIVE_FATAL;
} else
ret = _archive_read_disk_open(_a, path.s);
archive_string_free(&path);
return (ret);
}
static int
_archive_read_disk_open(struct archive *_a, const char *pathname)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
if (a->tree != NULL)
a->tree = tree_reopen(a->tree, pathname, a->restore_time);
else
a->tree = tree_open(pathname, a->symlink_mode,
a->restore_time);
if (a->tree == NULL) {
archive_set_error(&a->archive, ENOMEM,
"Can't allocate tar data");
a->archive.state = ARCHIVE_STATE_FATAL;
return (ARCHIVE_FATAL);
}
a->archive.state = ARCHIVE_STATE_HEADER;
return (ARCHIVE_OK);
}
/*
* Return a current filesystem ID which is index of the filesystem entry
* you've visited through archive_read_disk.
*/
int
archive_read_disk_current_filesystem(struct archive *_a)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
"archive_read_disk_current_filesystem");
return (a->tree->current_filesystem_id);
}
static int
update_current_filesystem(struct archive_read_disk *a, int64_t dev)
{
struct tree *t = a->tree;
int i, fid;
if (t->current_filesystem != NULL &&
t->current_filesystem->dev == dev)
return (ARCHIVE_OK);
for (i = 0; i < t->max_filesystem_id; i++) {
if (t->filesystem_table[i].dev == dev) {
/* There is the filesytem ID we've already generated. */
t->current_filesystem_id = i;
t->current_filesystem = &(t->filesystem_table[i]);
return (ARCHIVE_OK);
}
}
/*
* This is the new filesytem which we have to generate a new ID for.
*/
fid = t->max_filesystem_id++;
if (t->max_filesystem_id > t->allocated_filesytem) {
size_t s;
s = t->max_filesystem_id * 2;
t->filesystem_table = realloc(t->filesystem_table,
s * sizeof(*t->filesystem_table));
if (t->filesystem_table == NULL) {
archive_set_error(&a->archive, ENOMEM,
"Can't allocate tar data");
return (ARCHIVE_FATAL);
}
t->allocated_filesytem = s;
}
t->current_filesystem_id = fid;
t->current_filesystem = &(t->filesystem_table[fid]);
t->current_filesystem->dev = dev;
t->current_filesystem->allocation_ptr = NULL;
t->current_filesystem->buff = NULL;
/* Setup the current filesystem properties which depend on
* platform specific. */
return (setup_current_filesystem(a));
}
/*
* Returns 1 if current filesystem is generated filesystem, 0 if it is not
* or -1 if it is unknown.
*/
int
archive_read_disk_current_filesystem_is_synthetic(struct archive *_a)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
"archive_read_disk_current_filesystem");
return (a->tree->current_filesystem->synthetic);
}
/*
* Returns 1 if current filesystem is remote filesystem, 0 if it is not
* or -1 if it is unknown.
*/
int
archive_read_disk_current_filesystem_is_remote(struct archive *_a)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
"archive_read_disk_current_filesystem");
return (a->tree->current_filesystem->remote);
}
#if defined(_PC_REC_INCR_XFER_SIZE) && defined(_PC_REC_MAX_XFER_SIZE) &&\
defined(_PC_REC_MIN_XFER_SIZE) && defined(_PC_REC_XFER_ALIGN)
static int
get_xfer_size(struct tree *t, int fd, const char *path)
{
t->current_filesystem->xfer_align = -1;
errno = 0;
if (fd >= 0) {
t->current_filesystem->incr_xfer_size =
fpathconf(fd, _PC_REC_INCR_XFER_SIZE);
t->current_filesystem->max_xfer_size =
fpathconf(fd, _PC_REC_MAX_XFER_SIZE);
t->current_filesystem->min_xfer_size =
fpathconf(fd, _PC_REC_MIN_XFER_SIZE);
t->current_filesystem->xfer_align =
fpathconf(fd, _PC_REC_XFER_ALIGN);
} else if (path != NULL) {
t->current_filesystem->incr_xfer_size =
pathconf(path, _PC_REC_INCR_XFER_SIZE);
t->current_filesystem->max_xfer_size =
pathconf(path, _PC_REC_MAX_XFER_SIZE);
t->current_filesystem->min_xfer_size =
pathconf(path, _PC_REC_MIN_XFER_SIZE);
t->current_filesystem->xfer_align =
pathconf(path, _PC_REC_XFER_ALIGN);
}
/* At least we need an alignment size. */
if (t->current_filesystem->xfer_align == -1)
return ((errno == EINVAL)?1:-1);
else
return (0);
}
#else
static int
get_xfer_size(struct tree *t, int fd, const char *path)
{
(void)t; /* UNUSED */
(void)fd; /* UNUSED */
(void)path; /* UNUSED */
return (1);/* Not supported */
}
#endif
#if defined(HAVE_STATFS) && defined(HAVE_FSTATFS) && defined(MNT_LOCAL) \
&& !defined(ST_LOCAL)
/*
* Gather current filesystem properties on FreeBSD, OpenBSD and Mac OS X.
*/
static int
setup_current_filesystem(struct archive_read_disk *a)
{
struct tree *t = a->tree;
struct statfs sfs;
#if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
struct xvfsconf vfc;
#endif
int r, xr = 0;
#if !defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
long nm;
#endif
t->current_filesystem->synthetic = -1;
t->current_filesystem->remote = -1;
if (tree_current_is_symblic_link_target(t)) {
#if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR)
/*
* Get file system statistics on any directory
* where current is.
*/
int fd = openat(tree_current_dir_fd(t),
tree_current_access_path(t), O_RDONLY);
if (fd < 0) {
archive_set_error(&a->archive, errno,
"openat failed");
return (ARCHIVE_FAILED);
}
r = fstatfs(fd, &sfs);
if (r == 0)
xr = get_xfer_size(t, fd, NULL);
close(fd);
#else
r = statfs(tree_current_access_path(t), &sfs);
if (r == 0)
xr = get_xfer_size(t, -1, tree_current_access_path(t));
#endif
} else {
r = fstatfs(tree_current_dir_fd(t), &sfs);
if (r == 0)
xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
}
if (r == -1 || xr == -1) {
archive_set_error(&a->archive, errno, "statfs failed");
return (ARCHIVE_FAILED);
} else if (xr == 1) {
/* pathconf(_PC_REX_*) operations are not supported. */
t->current_filesystem->xfer_align = sfs.f_bsize;
t->current_filesystem->max_xfer_size = -1;
t->current_filesystem->min_xfer_size = sfs.f_iosize;
t->current_filesystem->incr_xfer_size = sfs.f_iosize;
}
if (sfs.f_flags & MNT_LOCAL)
t->current_filesystem->remote = 0;
else
t->current_filesystem->remote = 1;
#if defined(HAVE_GETVFSBYNAME) && defined(VFCF_SYNTHETIC)
r = getvfsbyname(sfs.f_fstypename, &vfc);
if (r == -1) {
archive_set_error(&a->archive, errno, "getvfsbyname failed");
return (ARCHIVE_FAILED);
}
if (vfc.vfc_flags & VFCF_SYNTHETIC)
t->current_filesystem->synthetic = 1;
else
t->current_filesystem->synthetic = 0;
#endif
#if defined(MNT_NOATIME)
if (sfs.f_flags & MNT_NOATIME)
t->current_filesystem->noatime = 1;
else
#endif
t->current_filesystem->noatime = 0;
#if defined(HAVE_READDIR_R)
/* Set maximum filename length. */
#if defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
t->current_filesystem->name_max = sfs.f_namemax;
#else
/* Mac OS X does not have f_namemax in struct statfs. */
if (tree_current_is_symblic_link_target(t))
nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
else
nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
if (nm == -1)
t->current_filesystem->name_max = NAME_MAX;
else
t->current_filesystem->name_max = nm;
#endif
#endif /* HAVE_READDIR_R */
return (ARCHIVE_OK);
}
#elif (defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS)) && defined(ST_LOCAL)
/*
* Gather current filesystem properties on NetBSD
*/
static int
setup_current_filesystem(struct archive_read_disk *a)
{
struct tree *t = a->tree;
struct statvfs sfs;
int r, xr = 0;
t->current_filesystem->synthetic = -1;
if (tree_current_is_symblic_link_target(t)) {
r = statvfs(tree_current_access_path(t), &sfs);
if (r == 0)
xr = get_xfer_size(t, -1, tree_current_access_path(t));
} else {
#ifdef HAVE_FSTATVFS
r = fstatvfs(tree_current_dir_fd(t), &sfs);
if (r == 0)
xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
#else
r = statvfs(".", &sfs);
if (r == 0)
xr = get_xfer_size(t, -1, ".");
#endif
}
if (r == -1 || xr == -1) {
t->current_filesystem->remote = -1;
archive_set_error(&a->archive, errno, "statvfs failed");
return (ARCHIVE_FAILED);
} else if (xr == 1) {
/* Usuall come here unless NetBSD supports _PC_REC_XFER_ALIGN
* for pathconf() function. */
t->current_filesystem->xfer_align = sfs.f_frsize;
t->current_filesystem->max_xfer_size = -1;
#if defined(HAVE_STRUCT_STATVFS_F_IOSIZE)
t->current_filesystem->min_xfer_size = sfs.f_iosize;
t->current_filesystem->incr_xfer_size = sfs.f_iosize;
#else
t->current_filesystem->min_xfer_size = sfs.f_bsize;
t->current_filesystem->incr_xfer_size = sfs.f_bsize;
#endif
}
if (sfs.f_flag & ST_LOCAL)
t->current_filesystem->remote = 0;
else
t->current_filesystem->remote = 1;
#if defined(ST_NOATIME)
if (sfs.f_flag & ST_NOATIME)
t->current_filesystem->noatime = 1;
else
#endif
t->current_filesystem->noatime = 0;
/* Set maximum filename length. */
t->current_filesystem->name_max = sfs.f_namemax;
return (ARCHIVE_OK);
}
#elif defined(HAVE_SYS_STATFS_H) && defined(HAVE_LINUX_MAGIC_H) &&\
defined(HAVE_STATFS) && defined(HAVE_FSTATFS)
/*
* Note: statfs is deprecated since LSB 3.2
*/
#ifndef CIFS_SUPER_MAGIC
#define CIFS_SUPER_MAGIC 0xFF534D42
#endif
#ifndef DEVFS_SUPER_MAGIC
#define DEVFS_SUPER_MAGIC 0x1373
#endif
/*
* Gather current filesystem properties on Linux
*/
static int
setup_current_filesystem(struct archive_read_disk *a)
{
struct tree *t = a->tree;
struct statfs sfs;
struct statvfs svfs;
int r, vr = 0, xr = 0;
if (tree_current_is_symblic_link_target(t)) {
#if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR)
/*
* Get file system statistics on any directory
* where current is.
*/
int fd = openat(tree_current_dir_fd(t),
tree_current_access_path(t), O_RDONLY);
if (fd < 0) {
archive_set_error(&a->archive, errno,
"openat failed");
return (ARCHIVE_FAILED);
}
vr = fstatvfs(fd, &svfs);/* for f_flag, mount flags */
r = fstatfs(fd, &sfs);
if (r == 0)
xr = get_xfer_size(t, fd, NULL);
close(fd);
#else
vr = statvfs(tree_current_access_path(t), &svfs);
r = statfs(tree_current_access_path(t), &sfs);
if (r == 0)
xr = get_xfer_size(t, -1, tree_current_access_path(t));
#endif
} else {
#ifdef HAVE_FSTATFS
vr = fstatvfs(tree_current_dir_fd(t), &svfs);
r = fstatfs(tree_current_dir_fd(t), &sfs);
if (r == 0)
xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
#elif defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR)
#error "Unexpected case. Please tell us about this error."
#else
vr = statvfs(".", &svfs);
r = statfs(".", &sfs);
if (r == 0)
xr = get_xfer_size(t, -1, ".");
#endif
}
if (r == -1 || xr == -1 || vr == -1) {
t->current_filesystem->synthetic = -1;
t->current_filesystem->remote = -1;
archive_set_error(&a->archive, errno, "statfs failed");
return (ARCHIVE_FAILED);
} else if (xr == 1) {
/* pathconf(_PC_REX_*) operations are not supported. */
t->current_filesystem->xfer_align = svfs.f_frsize;
t->current_filesystem->max_xfer_size = -1;
t->current_filesystem->min_xfer_size = svfs.f_bsize;
t->current_filesystem->incr_xfer_size = svfs.f_bsize;
}
switch (sfs.f_type) {
case AFS_SUPER_MAGIC:
case CIFS_SUPER_MAGIC:
case CODA_SUPER_MAGIC:
case NCP_SUPER_MAGIC:/* NetWare */
case NFS_SUPER_MAGIC:
case SMB_SUPER_MAGIC:
t->current_filesystem->remote = 1;
t->current_filesystem->synthetic = 0;
break;
case DEVFS_SUPER_MAGIC:
case PROC_SUPER_MAGIC:
case USBDEVICE_SUPER_MAGIC:
t->current_filesystem->remote = 0;
t->current_filesystem->synthetic = 1;
break;
default:
t->current_filesystem->remote = 0;
t->current_filesystem->synthetic = 0;
break;
}
#if defined(ST_NOATIME)
if (svfs.f_flag & ST_NOATIME)
t->current_filesystem->noatime = 1;
else
#endif
t->current_filesystem->noatime = 0;
#if defined(HAVE_READDIR_R)
/* Set maximum filename length. */
t->current_filesystem->name_max = sfs.f_namelen;
#endif
return (ARCHIVE_OK);
}
#elif defined(HAVE_SYS_STATVFS_H) &&\
(defined(HAVE_STATVFS) || defined(HAVE_FSTATVFS))
/*
* Gather current filesystem properties on other posix platform.
*/
static int
setup_current_filesystem(struct archive_read_disk *a)
{
struct tree *t = a->tree;
struct statvfs sfs;
int r, xr = 0;
t->current_filesystem->synthetic = -1;/* Not supported */
t->current_filesystem->remote = -1;/* Not supported */
if (tree_current_is_symblic_link_target(t)) {
#if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR)
/*
* Get file system statistics on any directory
* where current is.
*/
int fd = openat(tree_current_dir_fd(t),
tree_current_access_path(t), O_RDONLY);
if (fd < 0) {
archive_set_error(&a->archive, errno,
"openat failed");
return (ARCHIVE_FAILED);
}
r = fstatvfs(fd, &sfs);
if (r == 0)
xr = get_xfer_size(t, fd, NULL);
close(fd);
#else
r = statvfs(tree_current_access_path(t), &sfs);
if (r == 0)
xr = get_xfer_size(t, -1, tree_current_access_path(t));
#endif
} else {
#ifdef HAVE_FSTATVFS
r = fstatvfs(tree_current_dir_fd(t), &sfs);
if (r == 0)
xr = get_xfer_size(t, tree_current_dir_fd(t), NULL);
#elif defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR)
#error "Unexpected case. Please tell us about this error."
#else
r = statvfs(".", &sfs);
if (r == 0)
xr = get_xfer_size(t, -1, ".");
#endif
}
if (r == -1 || xr == -1) {
t->current_filesystem->synthetic = -1;
t->current_filesystem->remote = -1;
archive_set_error(&a->archive, errno, "statvfs failed");
return (ARCHIVE_FAILED);
} else if (xr == 1) {
/* pathconf(_PC_REX_*) operations are not supported. */
t->current_filesystem->xfer_align = sfs.f_frsize;
t->current_filesystem->max_xfer_size = -1;
t->current_filesystem->min_xfer_size = sfs.f_bsize;
t->current_filesystem->incr_xfer_size = sfs.f_bsize;
}
#if defined(ST_NOATIME)
if (sfs.f_flag & ST_NOATIME)
t->current_filesystem->noatime = 1;
else
#endif
t->current_filesystem->noatime = 0;
#if defined(HAVE_READDIR_R)
/* Set maximum filename length. */
t->current_filesystem->name_max = sfs.f_namemax;
#endif
return (ARCHIVE_OK);
}
#else
/*
* Generic: Gather current filesystem properties.
* TODO: Is this generic function really needed?
*/
static int
setup_current_filesystem(struct archive_read_disk *a)
{
struct tree *t = a->tree;
#if defined(_PC_NAME_MAX) && defined(HAVE_READDIR_R)
long nm;
#endif
t->current_filesystem->synthetic = -1;/* Not supported */
t->current_filesystem->remote = -1;/* Not supported */
t->current_filesystem->noatime = 0;
(void)get_xfer_size(t, -1, ".");/* Dummy call to avoid build error. */
t->current_filesystem->xfer_align = -1;/* Unknown */
t->current_filesystem->max_xfer_size = -1;
t->current_filesystem->min_xfer_size = -1;
t->current_filesystem->incr_xfer_size = -1;
#if defined(HAVE_READDIR_R)
/* Set maximum filename length. */
# if defined(_PC_NAME_MAX)
if (tree_current_is_symblic_link_target(t))
nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
else
nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
if (nm == -1)
# endif /* _PC_NAME_MAX */
/*
* Some sysmtes (HP-UX or others?) incorrectly defined
* NAME_MAX macro to be a smaller value.
*/
# if defined(NAME_MAX) && NAME_MAX >= 255
t->current_filesystem->name_max = NAME_MAX;
# else
/* No way to get a trusted value of maximum filename
* length. */
t->current_filesystem->name_max = PATH_MAX;
# endif /* NAME_MAX */
# if defined(_PC_NAME_MAX)
else
t->current_filesystem->name_max = nm;
# endif /* _PC_NAME_MAX */
#endif /* HAVE_READDIR_R */
return (ARCHIVE_OK);
}
#endif
static int
close_and_restore_time(int fd, struct tree *t, struct restore_time *rt)
{
#ifndef HAVE_UTIMES
(void)a; /* UNUSED */
return (close(fd));
#else
#if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
struct timespec timespecs[2];
#endif
struct timeval times[2];
if ((t->flags & needsRestoreTimes) == 0 || rt->noatime) {
if (fd >= 0)
return (close(fd));
else
return (0);
}
#if defined(HAVE_FUTIMENS) && !defined(__CYGWIN__)
timespecs[1].tv_sec = rt->mtime;
timespecs[1].tv_nsec = rt->mtime_nsec;
timespecs[0].tv_sec = rt->atime;
timespecs[0].tv_nsec = rt->atime_nsec;
/* futimens() is defined in POSIX.1-2008. */
if (futimens(fd, timespecs) == 0)
return (close(fd));
#endif
times[1].tv_sec = rt->mtime;
times[1].tv_usec = rt->mtime_nsec / 1000;
times[0].tv_sec = rt->atime;
times[0].tv_usec = rt->atime_nsec / 1000;
#if !defined(HAVE_FUTIMENS) && defined(HAVE_FUTIMES) && !defined(__CYGWIN__)
if (futimes(fd, times) == 0)
return (close(fd));
#endif
close(fd);
#if defined(HAVE_FUTIMESAT)
if (futimesat(tree_current_dir_fd(t), rt->name, times) == 0)
return (0);
#endif
#ifdef HAVE_LUTIMES
if (lutimes(rt->name, times) != 0)
#else
if (AE_IFLNK != rt->filetype && utimes(rt->name, times) != 0)
#endif
return (-1);
#endif
return (0);
}
/*
* Add a directory path to the current stack.
*/
static void
tree_push(struct tree *t, const char *path, int filesystem_id,
int64_t dev, int64_t ino, struct restore_time *rt)
{
struct tree_entry *te;
te = malloc(sizeof(*te));
memset(te, 0, sizeof(*te));
te->next = t->stack;
te->parent = t->current;
if (te->parent)
te->depth = te->parent->depth + 1;
t->stack = te;
archive_string_init(&te->name);
te->symlink_parent_fd = -1;
archive_strcpy(&te->name, path);
te->flags = needsDescent | needsOpen | needsAscent;
te->filesystem_id = filesystem_id;
te->dev = dev;
te->ino = ino;
te->dirname_length = t->dirname_length;
te->restore_time.name = te->name.s;
if (rt != NULL) {
te->restore_time.mtime = rt->mtime;
te->restore_time.mtime_nsec = rt->mtime_nsec;
te->restore_time.atime = rt->atime;
te->restore_time.atime_nsec = rt->atime_nsec;
te->restore_time.filetype = rt->filetype;
te->restore_time.noatime = rt->noatime;
}
}
/*
* Append a name to the current dir path.
*/
static void
tree_append(struct tree *t, const char *name, size_t name_length)
{
size_t size_needed;
t->path.s[t->dirname_length] = '\0';
t->path.length = t->dirname_length;
/* Strip trailing '/' from name, unless entire name is "/". */
while (name_length > 1 && name[name_length - 1] == '/')
name_length--;
/* Resize pathname buffer as needed. */
size_needed = name_length + t->dirname_length + 2;
archive_string_ensure(&t->path, size_needed);
/* Add a separating '/' if it's needed. */
if (t->dirname_length > 0 && t->path.s[archive_strlen(&t->path)-1] != '/')
archive_strappend_char(&t->path, '/');
t->basename = t->path.s + archive_strlen(&t->path);
archive_strncat(&t->path, name, name_length);
t->restore_time.name = t->basename;
}
/*
* Open a directory tree for traversal.
*/
static struct tree *
tree_open(const char *path, int symlink_mode, int restore_time)
{
struct tree *t;
if ((t = malloc(sizeof(*t))) == NULL)
return (NULL);
memset(t, 0, sizeof(*t));
archive_string_init(&t->path);
archive_string_ensure(&t->path, 31);
t->initial_symlink_mode = symlink_mode;
return (tree_reopen(t, path, restore_time));
}
static struct tree *
tree_reopen(struct tree *t, const char *path, int restore_time)
{
t->flags = (restore_time)?needsRestoreTimes:0;
t->visit_type = 0;
t->tree_errno = 0;
t->dirname_length = 0;
t->depth = 0;
t->descend = 0;
t->current = NULL;
t->d = INVALID_DIR_HANDLE;
t->symlink_mode = t->initial_symlink_mode;
archive_string_empty(&t->path);
t->entry_fd = -1;
t->entry_eof = 0;
t->entry_remaining_bytes = 0;
/* First item is set up a lot like a symlink traversal. */
tree_push(t, path, 0, 0, 0, NULL);
t->stack->flags = needsFirstVisit;
t->maxOpenCount = t->openCount = 1;
t->initial_dir_fd = open(".", O_RDONLY);
t->working_dir_fd = dup(t->initial_dir_fd);
return (t);
}
static int
tree_descent(struct tree *t)
{
int r = 0;
#if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR)
int new_fd;
t->dirname_length = archive_strlen(&t->path);
new_fd = openat(t->working_dir_fd, t->stack->name.s, O_RDONLY);
if (new_fd < 0) {
t->tree_errno = errno;
r = TREE_ERROR_DIR;
} else {
t->depth++;
/* If it is a link, set up fd for the ascent. */
if (t->stack->flags & isDirLink) {
t->stack->symlink_parent_fd = t->working_dir_fd;
t->openCount++;
if (t->openCount > t->maxOpenCount)
t->maxOpenCount = t->openCount;
} else
close(t->working_dir_fd);
t->working_dir_fd = new_fd;
}
#else
/* If it is a link, set up fd for the ascent. */
if (t->stack->flags & isDirLink)
t->stack->symlink_parent_fd = t->working_dir_fd;
else {
close(t->working_dir_fd);
t->openCount--;
}
t->working_dir_fd = -1;
t->dirname_length = archive_strlen(&t->path);
if (chdir(t->stack->name.s) != 0)
{
t->tree_errno = errno;
r = TREE_ERROR_DIR;
} else {
t->depth++;
t->working_dir_fd = open(".", O_RDONLY);
t->openCount++;
if (t->openCount > t->maxOpenCount)
t->maxOpenCount = t->openCount;
}
#endif
return (r);
}
/*
* We've finished a directory; ascend back to the parent.
*/
static int
tree_ascend(struct tree *t)
{
struct tree_entry *te;
int r = 0, prev_dir_fd;
te = t->stack;
prev_dir_fd = t->working_dir_fd;
#if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_FDOPENDIR)
if (te->flags & isDirLink)
t->working_dir_fd = te->symlink_parent_fd;
else {
int new_fd = openat(t->working_dir_fd, "..", O_RDONLY);
if (new_fd < 0) {
t->tree_errno = errno;
r = TREE_ERROR_FATAL;
} else
t->working_dir_fd = new_fd;
}
#else
if (te->flags & isDirLink) {
if (fchdir(te->symlink_parent_fd) != 0) {
t->tree_errno = errno;
r = TREE_ERROR_FATAL;
} else
t->working_dir_fd = te->symlink_parent_fd;
} else {
if (chdir("..") != 0) {
t->tree_errno = errno;
r = TREE_ERROR_FATAL;
} else
t->working_dir_fd = open(".", O_RDONLY);
}
#endif
if (r == 0) {
/* Current directory has been changed, we should
* close an fd of previous working directory. */
close_and_restore_time(prev_dir_fd, t, &te->restore_time);
if (te->flags & isDirLink) {
t->openCount--;
te->symlink_parent_fd = -1;
}
t->depth--;
}
return (r);
}
/*
* Return to the initial directory where tree_open() was performed.
*/
static int
tree_enter_initial_dir(struct tree *t)
{
int r = 0;
if (t->flags & onWorkingDir) {
r = fchdir(t->initial_dir_fd);
if (r == 0)
t->flags &= ~onWorkingDir;
}
return (r);
}
/*
* Restore working directory of directory traversals.
*/
static int
tree_enter_working_dir(struct tree *t)
{
int r = 0;
/*
* Change the current directory if really needed.
* Sometimes this is unneeded when we did not do
* descent.
*/
if (t->depth > 0 && (t->flags & onWorkingDir) == 0) {
r = fchdir(t->working_dir_fd);
if (r == 0)
t->flags |= onWorkingDir;
}
return (r);
}
static int
tree_current_dir_fd(struct tree *t)
{
return (t->working_dir_fd);
}
/*
* Pop the working stack.
*/
static void
tree_pop(struct tree *t)
{
struct tree_entry *te;
t->path.s[t->dirname_length] = '\0';
t->path.length = t->dirname_length;
if (t->stack == t->current && t->current != NULL)
t->current = t->current->parent;
te = t->stack;
t->stack = te->next;
t->dirname_length = te->dirname_length;
t->basename = t->path.s + t->dirname_length;
while (t->basename[0] == '/')
t->basename++;
archive_string_free(&te->name);
free(te);
}
/*
* Get the next item in the tree traversal.
*/
static int
tree_next(struct tree *t)
{
int r;
while (t->stack != NULL) {
/* If there's an open dir, get the next entry from there. */
if (t->d != INVALID_DIR_HANDLE) {
r = tree_dir_next_posix(t);
if (r == 0)
continue;
return (r);
}
if (t->stack->flags & needsFirstVisit) {
/* Top stack item needs a regular visit. */
t->current = t->stack;
tree_append(t, t->stack->name.s,
archive_strlen(&(t->stack->name)));
/* t->dirname_length = t->path_length; */
/* tree_pop(t); */
t->stack->flags &= ~needsFirstVisit;
return (t->visit_type = TREE_REGULAR);
} else if (t->stack->flags & needsDescent) {
/* Top stack item is dir to descend into. */
t->current = t->stack;
tree_append(t, t->stack->name.s,
archive_strlen(&(t->stack->name)));
t->stack->flags &= ~needsDescent;
r = tree_descent(t);
if (r != 0) {
tree_pop(t);
t->visit_type = r;
} else
t->visit_type = TREE_POSTDESCENT;
return (t->visit_type);
} else if (t->stack->flags & needsOpen) {
t->stack->flags &= ~needsOpen;
r = tree_dir_next_posix(t);
if (r == 0)
continue;
return (r);
} else if (t->stack->flags & needsAscent) {
/* Top stack item is dir and we're done with it. */
r = tree_ascend(t);
tree_pop(t);
t->visit_type = r != 0 ? r : TREE_POSTASCENT;
return (t->visit_type);
} else {
/* Top item on stack is dead. */
tree_pop(t);
t->flags &= ~hasLstat;
t->flags &= ~hasStat;
}
}
return (t->visit_type = 0);
}
static int
tree_dir_next_posix(struct tree *t)
{
int r;
const char *name;
size_t namelen;
if (t->d == NULL) {
#if defined(HAVE_READDIR_R)
size_t dirent_size;
#endif
#if defined(HAVE_FDOPENDIR)
if ((t->d = fdopendir(dup(t->working_dir_fd))) == NULL) {
#else
if ((t->d = opendir(".")) == NULL) {
#endif
r = tree_ascend(t); /* Undo "chdir" */
tree_pop(t);
t->tree_errno = errno;
t->visit_type = r != 0 ? r : TREE_ERROR_DIR;
return (t->visit_type);
}
#if defined(HAVE_READDIR_R)
dirent_size = offsetof(struct dirent, d_name) +
t->filesystem_table[t->current->filesystem_id].name_max + 1;
if (t->dirent == NULL || t->dirent_allocated < dirent_size) {
free(t->dirent);
t->dirent = malloc(dirent_size);
if (t->dirent == NULL) {
closedir(t->d);
t->d = INVALID_DIR_HANDLE;
(void)tree_ascend(t);
tree_pop(t);
t->tree_errno = ENOMEM;
t->visit_type = TREE_ERROR_DIR;
return (t->visit_type);
}
t->dirent_allocated = dirent_size;
}
#endif /* HAVE_READDIR_R */
}
for (;;) {
#if defined(HAVE_READDIR_R)
r = readdir_r(t->d, t->dirent, &t->de);
if (r != 0 || t->de == NULL) {
#else
errno = 0;
t->de = readdir(t->d);
if (t->de == NULL) {
r = errno;
#endif
closedir(t->d);
t->d = INVALID_DIR_HANDLE;
if (r != 0) {
t->tree_errno = r;
t->visit_type = TREE_ERROR_DIR;
return (t->visit_type);
} else
return (0);
}
name = t->de->d_name;
namelen = D_NAMELEN(t->de);
t->flags &= ~hasLstat;
t->flags &= ~hasStat;
if (name[0] == '.' && name[1] == '\0')
continue;
if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
continue;
tree_append(t, name, namelen);
return (t->visit_type = TREE_REGULAR);
}
}
/*
* Get the stat() data for the entry just returned from tree_next().
*/
static const struct stat *
tree_current_stat(struct tree *t)
{
if (!(t->flags & hasStat)) {
#ifdef HAVE_FSTATAT
if (fstatat(tree_current_dir_fd(t),
tree_current_access_path(t), &t->st, 0) != 0)
#else
if (stat(tree_current_access_path(t), &t->st) != 0)
#endif
return NULL;
t->flags |= hasStat;
}
return (&t->st);
}
/*
* Get the lstat() data for the entry just returned from tree_next().
*/
static const struct stat *
tree_current_lstat(struct tree *t)
{
if (!(t->flags & hasLstat)) {
#ifdef HAVE_FSTATAT
if (fstatat(tree_current_dir_fd(t),
tree_current_access_path(t), &t->lst,
AT_SYMLINK_NOFOLLOW) != 0)
#else
if (lstat(tree_current_access_path(t), &t->lst) != 0)
#endif
return NULL;
t->flags |= hasLstat;
}
return (&t->lst);
}
/*
* Test whether current entry is a dir or link to a dir.
*/
static int
tree_current_is_dir(struct tree *t)
{
const struct stat *st;
/*
* If we already have lstat() info, then try some
* cheap tests to determine if this is a dir.
*/
if (t->flags & hasLstat) {
/* If lstat() says it's a dir, it must be a dir. */
if (S_ISDIR(tree_current_lstat(t)->st_mode))
return 1;
/* Not a dir; might be a link to a dir. */
/* If it's not a link, then it's not a link to a dir. */
if (!S_ISLNK(tree_current_lstat(t)->st_mode))
return 0;
/*
* It's a link, but we don't know what it's a link to,
* so we'll have to use stat().
*/
}
st = tree_current_stat(t);
/* If we can't stat it, it's not a dir. */
if (st == NULL)
return 0;
/* Use the definitive test. Hopefully this is cached. */
return (S_ISDIR(st->st_mode));
}
/*
* Test whether current entry is a physical directory. Usually, we
* already have at least one of stat() or lstat() in memory, so we
* use tricks to try to avoid an extra trip to the disk.
*/
static int
tree_current_is_physical_dir(struct tree *t)
{
const struct stat *st;
/*
* If stat() says it isn't a dir, then it's not a dir.
* If stat() data is cached, this check is free, so do it first.
*/
if ((t->flags & hasStat)
&& (!S_ISDIR(tree_current_stat(t)->st_mode)))
return 0;
/*
* Either stat() said it was a dir (in which case, we have
* to determine whether it's really a link to a dir) or
* stat() info wasn't available. So we use lstat(), which
* hopefully is already cached.
*/
st = tree_current_lstat(t);
/* If we can't stat it, it's not a dir. */
if (st == NULL)
return 0;
/* Use the definitive test. Hopefully this is cached. */
return (S_ISDIR(st->st_mode));
}
/*
* Test whether the same file has been in the tree as its parent.
*/
static int
tree_target_is_same_as_parent(struct tree *t, const struct stat *st)
{
struct tree_entry *te;
for (te = t->current->parent; te != NULL; te = te->parent) {
if (te->dev == st->st_dev && te->ino == st->st_ino)
return (1);
}
return (0);
}
/*
* Test whether the current file is symbolic link target and
* on the other filesystem.
*/
static int
tree_current_is_symblic_link_target(struct tree *t)
{
static const struct stat *lst, *st;
lst = tree_current_lstat(t);
st = tree_current_stat(t);
return (st != NULL && st->st_dev == t->current_filesystem->dev &&
st->st_dev != lst->st_dev);
}
/*
* Return the access path for the entry just returned from tree_next().
*/
static const char *
tree_current_access_path(struct tree *t)
{
return (t->basename);
}
/*
* Return the full path for the entry just returned from tree_next().
*/
static const char *
tree_current_path(struct tree *t)
{
return (t->path.s);
}
/*
* Terminate the traversal.
*/
static void
tree_close(struct tree *t)
{
if (t == NULL)
return;
if (t->entry_fd >= 0) {
close_and_restore_time(t->entry_fd, t, &t->restore_time);
t->entry_fd = -1;
}
/* Close the handle of readdir(). */
if (t->d != INVALID_DIR_HANDLE) {
closedir(t->d);
t->d = INVALID_DIR_HANDLE;
}
/* Release anything remaining in the stack. */
while (t->stack != NULL) {
if (t->stack->flags & isDirLink)
close(t->stack->symlink_parent_fd);
tree_pop(t);
}
if (t->working_dir_fd >= 0) {
close(t->working_dir_fd);
t->working_dir_fd = -1;
}
if (t->initial_dir_fd >= 0) {
close(t->initial_dir_fd);
t->initial_dir_fd = -1;
}
}
/*
* Release any resources.
*/
static void
tree_free(struct tree *t)
{
int i;
if (t == NULL)
return;
archive_string_free(&t->path);
#if defined(HAVE_READDIR_R)
free(t->dirent);
#endif
free(t->sparse_list);
for (i = 0; i < t->max_filesystem_id; i++)
free(t->filesystem_table[i].allocation_ptr);
free(t->filesystem_table);
free(t);
}
#endif
|