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
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
|
/*
Unix SMB/Netbios implementation.
Version 1.9.
Tar Extensions
Copyright (C) Ricky Poulten 1995-1998
Copyright (C) Richard Sharpe 1998
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* The following changes developed by Richard Sharpe for Canon Information
Systems Research Australia (CISRA)
1. Restore can now restore files with long file names
2. Save now saves directory information so that we can restore
directory creation times
3. tar now accepts both UNIX path names and DOS path names. I prefer
those lovely /'s to those UGLY \'s :-)
4. the files to exclude can be specified as a regular expression by adding
an r flag to the other tar flags. Eg:
-TcrX file.tar "*.(obj|exe)"
will skip all .obj and .exe files
*/
#include "includes.h"
#include "clitar.h"
typedef struct file_info_struct file_info2;
struct file_info_struct
{
int size;
int mode;
int uid;
int gid;
/* These times are normally kept in GMT */
time_t mtime;
time_t atime;
time_t ctime;
char *name; /* This is dynamically allocate */
file_info2 *next, *prev; /* Used in the stack ... */
};
typedef struct
{
file_info2 *top;
int items;
} stack;
stack dir_stack = {NULL, 0}; /* Want an empty stack */
extern BOOL recurse;
#define SEPARATORS " \t\n\r"
extern int DEBUGLEVEL;
extern int Client;
/* These defines are for the do_setrattr routine, to indicate
* setting and reseting of file attributes in the function call */
#define ATTRSET 1
#define ATTRRESET 0
static int attribute = aDIR | aSYSTEM | aHIDDEN;
#ifndef CLIENT_TIMEOUT
#define CLIENT_TIMEOUT (30*1000)
#endif
static char *tarbuf;
static int tp, ntarf, tbufsiz, ttarf;
/* Incremental mode */
BOOL tar_inc=False;
/* Reset archive bit */
BOOL tar_reset=False;
/* Include / exclude mode (true=include, false=exclude) */
BOOL tar_excl=True;
/* use regular expressions for search on file names */
BOOL tar_re_search=False;
#ifdef HAVE_REGEX_H
regex_t *preg;
#endif
/* Dump files with System attribute */
BOOL tar_system=True;
/* Dump files with Hidden attribute */
BOOL tar_hidden=True;
/* Be noisy - make a catalogue */
BOOL tar_noisy=True;
BOOL tar_real_noisy=True;
char tar_type='\0';
static char **cliplist=NULL;
static int clipn=0;
static BOOL must_free_cliplist = False;
extern file_info def_finfo;
extern BOOL lowercase;
extern int cnum;
extern BOOL readbraw_supported;
extern int max_xmit;
extern pstring cur_dir;
extern int get_total_time_ms;
extern int get_total_size;
extern int Protocol;
int blocksize=20;
int tarhandle;
static void writetarheader(int f, char *aname, int size, time_t mtime,
char *amode, unsigned char ftype);
static void do_atar(char *rname,char *lname,file_info *finfo1);
static void do_tar(file_info *finfo);
static void oct_it(long value, int ndgs, char *p);
static void fixtarname(char *tptr, char *fp, int l);
static int dotarbuf(int f, char *b, int n);
static void dozerobuf(int f, int n);
static void dotareof(int f);
static void initarbuf(void);
static int do_setrattr(char *fname, int attr, int setit);
/* restore functions */
static long readtarheader(union hblock *hb, file_info2 *finfo, char *prefix);
static long unoct(char *p, int ndgs);
static void do_tarput(void);
static void unfixtarname(char *tptr, char *fp, int l, BOOL first);
/*
* tar specific utitlities
*/
#if 0 /* Removed to get around gcc 'defined but not used' error. */
/*
* Stack routines, push_dir, pop_dir, top_dir_name
*/
static BOOL push_dir(stack *tar_dir_stack, file_info2 *dir)
{
dir -> next = tar_dir_stack -> top;
dir -> prev = NULL;
tar_dir_stack -> items++;
tar_dir_stack -> top = dir;
return(True);
}
static file_info2 *pop_dir(stack *tar_dir_stack)
{
file_info2 *ptr;
ptr = tar_dir_stack -> top;
if (tar_dir_stack -> top != NULL) {
tar_dir_stack -> top = tar_dir_stack -> top -> next;
tar_dir_stack -> items--;
}
return ptr;
}
static char *top_dir_name(stack *tar_dir_stack)
{
return(tar_dir_stack -> top != NULL?tar_dir_stack -> top -> name:NULL);
}
static BOOL sub_dir(char *dir1, char *dir2)
{
return(True);
}
#endif /* Removed to get around gcc 'defined but not used' error. */
/*******************************************************************
Create a string of size size+1 (for the null)
*******************************************************************/
static char *string_create_s(int size)
{
char *tmp;
tmp = (char *)malloc(size+1);
if (tmp == NULL) {
DEBUG(0, ("Out of memory in string_create_s\n"));
}
return(tmp);
}
/****************************************************************************
Write a tar header to buffer
****************************************************************************/
static void writetarheader(int f, char *aname, int size, time_t mtime,
char *amode, unsigned char ftype)
{
union hblock hb;
int i, chk, l;
char *jp;
DEBUG(5, ("WriteTarHdr, Type = %c, Size= %i, Name = %s\n", ftype, size, aname));
memset(hb.dummy, 0, sizeof(hb.dummy));
l=strlen(aname);
if (l >= NAMSIZ) {
/* write a GNU tar style long header */
char *b;
b = (char *)malloc(l+TBLOCK+100);
if (!b) {
DEBUG(0,("out of memory\n"));
exit(1);
}
writetarheader(f, "/./@LongLink", l+1, 0, " 0 \0", 'L');
memset(b, 0, l+TBLOCK+100);
fixtarname(b, aname, l);
i = strlen(b)+1;
DEBUG(5, ("File name in tar file: %s, size=%i, \n", b, strlen(b)));
dotarbuf(f, b, TBLOCK*(((i-1)/TBLOCK)+1));
free(b);
}
/* use l + 1 to do the null too */
fixtarname(hb.dbuf.name, aname, (l >= NAMSIZ) ? NAMSIZ : l + 1);
if (lowercase)
strlower(hb.dbuf.name);
/* write out a "standard" tar format header */
hb.dbuf.name[NAMSIZ-1]='\0';
safe_strcpy(hb.dbuf.mode, amode, strlen(amode));
oct_it(0L, 8, hb.dbuf.uid);
oct_it(0L, 8, hb.dbuf.gid);
oct_it((long) size, 13, hb.dbuf.size);
oct_it((long) mtime, 13, hb.dbuf.mtime);
memcpy(hb.dbuf.chksum, " ", sizeof(hb.dbuf.chksum));
memset(hb.dbuf.linkname, 0, NAMSIZ);
hb.dbuf.linkflag=ftype;
for (chk=0, i=sizeof(hb.dummy), jp=hb.dummy; --i>=0;) chk+=(0xFF & *jp++);
oct_it((long) chk, 8, hb.dbuf.chksum);
hb.dbuf.chksum[6] = '\0';
(void) dotarbuf(f, hb.dummy, sizeof(hb.dummy));
}
/****************************************************************************
Read a tar header into a hblock structure, and validate
***************************************************************************/
static long readtarheader(union hblock *hb, file_info2 *finfo, char *prefix)
{
long chk, fchk;
int i;
char *jp;
/*
* read in a "standard" tar format header - we're not that interested
* in that many fields, though
*/
/* check the checksum */
for (chk=0, i=sizeof(hb->dummy), jp=hb->dummy; --i>=0;) chk+=(0xFF & *jp++);
if (chk == 0)
return chk;
/* compensate for blanks in chksum header */
for (i=sizeof(hb->dbuf.chksum), jp=hb->dbuf.chksum; --i>=0;)
chk-=(0xFF & *jp++);
chk += ' ' * sizeof(hb->dbuf.chksum);
fchk=unoct(hb->dbuf.chksum, sizeof(hb->dbuf.chksum));
DEBUG(5, ("checksum totals chk=%d fchk=%d chksum=%s\n",
chk, fchk, hb->dbuf.chksum));
if (fchk != chk)
{
DEBUG(0, ("checksums don't match %d %d\n", fchk, chk));
for (i = 0; i < sizeof(hb -> dummy); i++) {
fprintf(stdout, "%2X ", hb -> dummy[i]);
}
fprintf(stdout, "\n");
fprintf(stdout, "%s\n", hb -> dummy);
fprintf(stdout, "Tarbuf = %X, hb = %X\n", (int)tarbuf, (int)hb);
return -1;
}
if ((finfo->name = string_create_s(strlen(prefix) + strlen(hb -> dbuf.name) + 3)) == NULL) {
DEBUG(0, ("Out of space creating file_info2 for %s\n", hb -> dbuf.name));
return(-1);
}
safe_strcpy(finfo->name, prefix, strlen(prefix) + strlen(hb -> dbuf.name) + 3);
/* use l + 1 to do the null too; do prefix - prefcnt to zap leading slash */
unfixtarname(finfo->name + strlen(prefix), hb->dbuf.name,
strlen(hb->dbuf.name) + 1, True);
/* can't handle some links at present */
if ((hb->dbuf.linkflag != '0') && (hb -> dbuf.linkflag != '5')) {
if (hb->dbuf.linkflag == 0) {
DEBUG(6, ("Warning: NULL link flag (gnu tar archive ?) %s\n",
finfo->name));
} else {
if (hb -> dbuf.linkflag == 'L') { /* We have a longlink */
/* Do nothing here at the moment. do_tarput will handle this
as long as the longlink gets back to it, as it has to advance
the buffer pointer, etc */
} else {
DEBUG(0, ("this tar file appears to contain some kind of link other than a GNUtar Longlink - ignoring\n"));
return -2;
}
}
}
if ((unoct(hb->dbuf.mode, sizeof(hb->dbuf.mode)) & S_IFDIR)
|| (*(finfo->name+strlen(finfo->name)-1) == '\\'))
{
finfo->mode=aDIR;
}
else
finfo->mode=0; /* we don't care about mode at the moment, we'll
* just make it a regular file */
/*
* Bug fix by richard@sj.co.uk
*
* REC: restore times correctly (as does tar)
* We only get the modification time of the file; set the creation time
* from the mod. time, and the access time to current time
*/
finfo->mtime = finfo->ctime = strtol(hb->dbuf.mtime, NULL, 8);
finfo->atime = time(NULL);
finfo->size = unoct(hb->dbuf.size, sizeof(hb->dbuf.size));
return True;
}
/****************************************************************************
Write out the tar buffer to tape or wherever
****************************************************************************/
static int dotarbuf(int f, char *b, int n)
{
int fail=1, writ=n;
/* This routine and the next one should be the only ones that do write()s */
if (tp + n >= tbufsiz)
{
int diff;
diff=tbufsiz-tp;
memcpy(tarbuf + tp, b, diff);
fail=fail && (1+write(f, tarbuf, tbufsiz));
n-=diff;
b+=diff;
tp=0;
while (n >= tbufsiz)
{
fail=fail && (1 + write(f, b, tbufsiz));
n-=tbufsiz;
b+=tbufsiz;
}
}
if (n>0) {
memcpy(tarbuf+tp, b, n);
tp+=n;
}
return(fail ? writ : 0);
}
/****************************************************************************
Write zeros to buffer / tape
****************************************************************************/
static void dozerobuf(int f, int n)
{
/* short routine just to write out n zeros to buffer -
* used to round files to nearest block
* and to do tar EOFs */
if (n+tp >= tbufsiz)
{
memset(tarbuf+tp, 0, tbufsiz-tp);
write(f, tarbuf, tbufsiz);
memset(tarbuf, 0, (tp+=n-tbufsiz));
}
else
{
memset(tarbuf+tp, 0, n);
tp+=n;
}
}
/****************************************************************************
Malloc tape buffer
****************************************************************************/
static void initarbuf()
{
/* initialize tar buffer */
tbufsiz=blocksize*TBLOCK;
tarbuf=malloc(tbufsiz); /* FIXME: We might not get the buffer */
/* reset tar buffer pointer and tar file counter and total dumped */
tp=0; ntarf=0; ttarf=0;
}
/****************************************************************************
Write two zero blocks at end of file
****************************************************************************/
static void dotareof(int f)
{
struct stat stbuf;
/* Two zero blocks at end of file, write out full buffer */
(void) dozerobuf(f, TBLOCK);
(void) dozerobuf(f, TBLOCK);
if (fstat(f, &stbuf) == -1)
{
DEBUG(0, ("Couldn't stat file handle\n"));
return;
}
/* Could be a pipe, in which case S_ISREG should fail,
* and we should write out at full size */
if (tp > 0) write(f, tarbuf, S_ISREG(stbuf.st_mode) ? tp : tbufsiz);
}
/****************************************************************************
(Un)mangle DOS pathname, make nonabsolute
****************************************************************************/
static void fixtarname(char *tptr, char *fp, int l)
{
/* add a '.' to start of file name, convert from ugly dos \'s in path
* to lovely unix /'s :-} */
*tptr++='.';
while (l > 0) {
int skip;
if((skip = skip_multibyte_char( *fp)) != 0) {
if (skip == 2) {
*tptr++ = *fp++;
*tptr++ = *fp++;
l -= 2;
} else if (skip == 1) {
*tptr++ = *fp++;
l--;
}
} else if (*fp == '\\') {
*tptr++ = '/';
fp++;
l--;
} else {
*tptr++ = *fp++;
l--;
}
}
}
/****************************************************************************
Convert from decimal to octal string
****************************************************************************/
static void oct_it (long value, int ndgs, char *p)
{
/* Converts long to octal string, pads with leading zeros */
/* skip final null, but do final space */
--ndgs;
p[--ndgs] = ' ';
/* Loop does at least one digit */
do {
p[--ndgs] = '0' + (char) (value & 7);
value >>= 3;
}
while (ndgs > 0 && value != 0);
/* Do leading zeros */
while (ndgs > 0)
p[--ndgs] = '0';
}
/****************************************************************************
Convert from octal string to long
***************************************************************************/
static long unoct(char *p, int ndgs)
{
long value=0;
/* Converts octal string to long, ignoring any non-digit */
while (--ndgs)
{
if (isdigit(*p))
value = (value << 3) | (long) (*p - '0');
p++;
}
return value;
}
/****************************************************************************
Compare two strings in a slash insensitive way, allowing s1 to match s2
if s1 is an "initial" string (up to directory marker). Thus, if s2 is
a file in any subdirectory of s1, declare a match.
***************************************************************************/
static int strslashcmp(char *s1, char *s2)
{
char *s1_0=s1;
while(*s1 && *s2 &&
(*s1 == *s2
|| tolower(*s1) == tolower(*s2)
|| (*s1 == '\\' && *s2=='/')
|| (*s1 == '/' && *s2=='\\'))) {
s1++; s2++;
}
/* if s1 has a trailing slash, it compared equal, so s1 is an "initial"
string of s2.
*/
if (!*s1 && s1 != s1_0 && (*(s1-1) == '/' || *(s1-1) == '\\')) return 0;
/* ignore trailing slash on s1 */
if (!*s2 && (*s1 == '/' || *s1 == '\\') && !*(s1+1)) return 0;
/* check for s1 is an "initial" string of s2 */
if (*s2 == '/' || *s2 == '\\') return 0;
return *s1-*s2;
}
/*
* general smb utility functions
*/
/**********************************************************************
do_setrtime, set time on a file or dir ...
**********************************************************************/
static int do_setrtime(char *fname, int mtime, BOOL err_silent)
{
char *inbuf, *outbuf, *p;
char *name;
DEBUG(5, ("Setting time on: %s, fnlen=%i.\n", fname, strlen(fname)));
name = (char *)malloc(strlen(fname) + 1 + 1);
if (name == NULL) {
DEBUG(0, ("Failed to allocate space while setting time on file: %s", fname));
return False;
}
if (*fname != '\\')
safe_strcpy(name, "\\", strlen(fname) + 1);
else
safe_strcpy(name, "", strlen(fname) + 1);
safe_strcat(name, fname, strlen(fname) + 1);
if (fname[strlen(name) - 1] == '\\')
name[strlen(name) - 1] = '\0';
inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
if (!inbuf || !outbuf) {
DEBUG(0, ("Could not allocate memory for inbuf or outbuf while changing time on: %s\n", fname));
free(name);
return False;
}
memset(outbuf, 0, smb_size);
set_message(outbuf, 8, 4 + strlen(name), True);
CVAL(outbuf, smb_com) = SMBsetatr;
SSVAL(outbuf, smb_tid, cnum);
cli_setup_pkt(outbuf);
SSVAL(outbuf, smb_vwv0, 0);
put_dos_date3(outbuf, smb_vwv1, mtime);
p = smb_buf(outbuf);
*p++ = 4;
safe_strcpy(p, name, strlen(name));
p+= (strlen(fname)+1);
*p++ = 4;
*p++ = 0;
send_smb(Client, outbuf);
client_receive_smb(Client, inbuf, CLIENT_TIMEOUT);
if (CVAL(inbuf,smb_rcls) != 0)
{
if (!err_silent) {
DEBUG(0,("%s setting attributes on file %s\n",
smb_errstr(inbuf), fname));
}
free(name);free(inbuf);free(outbuf);
return(False);
}
free(name);
free(inbuf);free(outbuf);
return(True);
}
/****************************************************************************
Set DOS file attributes
***************************************************************************/
static int do_setrattr(char *fname, int attr, int setit)
{
/*
* First get the existing attribs from existing file
*/
char *inbuf,*outbuf;
char *p;
char *name;
int fattr;
name = (char *)malloc(strlen(fname) + 1 + 1);
if (name == NULL) {
DEBUG(0, ("Failed to allocate space in do_setrattr while setting time on file: %s", fname));
return False;
}
safe_strcpy(name, "\\", strlen(fname) + 1);
safe_strcat(name, fname, strlen(fname) + 1);
inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
if (!inbuf || !outbuf)
{
DEBUG(0,("out of memory\n"));
free(name);
return False;
}
/* send an smb getatr message */
memset(outbuf,0,smb_size);
set_message(outbuf,0,2 + strlen(fname),True);
CVAL(outbuf,smb_com) = SMBgetatr;
SSVAL(outbuf,smb_tid,cnum);
cli_setup_pkt(outbuf);
p = smb_buf(outbuf);
*p++ = 4;
safe_strcpy(p,name, strlen(name));
p += (strlen(name)+1);
*p++ = 4;
*p++ = 0;
send_smb(Client,outbuf);
client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
if (CVAL(inbuf,smb_rcls) != 0)
DEBUG(5,("getatr: %s\n",smb_errstr(inbuf)));
else
{
DEBUG(5,("\nattr 0x%X time %d size %d\n",
(int)CVAL(inbuf,smb_vwv0),
SVAL(inbuf,smb_vwv1),
SVAL(inbuf,smb_vwv3)));
}
fattr=CVAL(inbuf,smb_vwv0);
/* combine found attributes with bits to be set or reset */
attr=setit ? (fattr | attr) : (fattr & ~attr);
/* now try and set attributes by sending smb reset message */
/* clear out buffer and start again */
memset(outbuf,0,smb_size);
set_message(outbuf,8,4 + strlen(name),True);
CVAL(outbuf,smb_com) = SMBsetatr;
SSVAL(outbuf,smb_tid,cnum);
cli_setup_pkt(outbuf);
SSVAL(outbuf,smb_vwv0,attr);
p = smb_buf(outbuf);
*p++ = 4;
safe_strcpy(p,name, strlen(name));
p += (strlen(name)+1);
*p++ = 4;
*p++ = 0;
send_smb(Client,outbuf);
client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
if (CVAL(inbuf,smb_rcls) != 0)
{
DEBUG(0,("%s setting attributes on file %s\n",
smb_errstr(inbuf), name));
free(name);free(inbuf);free(outbuf);
return(False);
}
free(name);
free(inbuf);free(outbuf);
return(True);
}
/****************************************************************************
Create a file on a share
***************************************************************************/
static BOOL smbcreat(file_info2 finfo, int *fnum, char *inbuf, char *outbuf)
{
char *p;
/* *must* be called with buffer ready malloc'ed */
/* open remote file */
memset(outbuf,0,smb_size);
set_message(outbuf,3,2 + strlen(finfo.name),True);
CVAL(outbuf,smb_com) = SMBcreate;
SSVAL(outbuf,smb_tid,cnum);
cli_setup_pkt(outbuf);
SSVAL(outbuf,smb_vwv0,finfo.mode);
put_dos_date3(outbuf,smb_vwv1,finfo.mtime);
p = smb_buf(outbuf);
*p++ = 4;
safe_strcpy(p,finfo.name, strlen(finfo.name));
send_smb(Client,outbuf);
client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
if (CVAL(inbuf,smb_rcls) != 0)
{
DEBUG(0,("%s opening remote file %s\n",smb_errstr(inbuf),
finfo.name));
return 0;
}
*fnum = SVAL(inbuf,smb_vwv0);
return True;
}
/****************************************************************************
Write a file to a share
***************************************************************************/
static BOOL smbwrite(int fnum, int n, int low, int high, int left,
char *bufferp, char *inbuf, char *outbuf)
{
/* *must* be called with buffer ready malloc'ed */
memset(outbuf,0,smb_size);
set_message(outbuf,5,n + 3,True);
memcpy(smb_buf(outbuf)+3, bufferp, n);
set_message(outbuf,5,n + 3, False);
CVAL(outbuf,smb_com) = SMBwrite;
SSVAL(outbuf,smb_tid,cnum);
cli_setup_pkt(outbuf);
SSVAL(outbuf,smb_vwv0,fnum);
SSVAL(outbuf,smb_vwv1,n);
SIVAL(outbuf,smb_vwv2,low);
SSVAL(outbuf,smb_vwv4,left);
CVAL(smb_buf(outbuf),0) = 1;
SSVAL(smb_buf(outbuf),1,n);
send_smb(Client,outbuf);
client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
if (CVAL(inbuf,smb_rcls) != 0)
{
DEBUG(0,("%s writing remote file\n",smb_errstr(inbuf)));
return False;
}
if (n != SVAL(inbuf,smb_vwv0))
{
DEBUG(0,("Error: only wrote %d bytes out of %d\n",
SVAL(inbuf,smb_vwv0), n));
return False;
}
return True;
}
/****************************************************************************
Close a file on a share
***************************************************************************/
static BOOL smbshut(file_info2 finfo, int fnum, char *inbuf, char *outbuf)
{
/* *must* be called with buffer ready malloc'ed */
memset(outbuf,0,smb_size);
set_message(outbuf,3,0,True);
CVAL(outbuf,smb_com) = SMBclose;
SSVAL(outbuf,smb_tid,cnum);
cli_setup_pkt(outbuf);
SSVAL(outbuf,smb_vwv0,fnum);
put_dos_date3(outbuf,smb_vwv1,finfo.mtime);
DEBUG(3,("Setting date to %s (0x%X)",
asctime(LocalTime(&finfo.mtime)),
finfo.mtime));
send_smb(Client,outbuf);
client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
if (CVAL(inbuf,smb_rcls) != 0)
{
DEBUG(0,("%s closing remote file %s\n",smb_errstr(inbuf),
finfo.name));
return False;
}
return True;
}
/****************************************************************************
Verify existence of path on share
***************************************************************************/
static BOOL smbchkpath(char *fname, char *inbuf, char *outbuf)
{
char *p;
memset(outbuf,0,smb_size);
set_message(outbuf,0,4 + strlen(fname),True);
CVAL(outbuf,smb_com) = SMBchkpth;
SSVAL(outbuf,smb_tid,cnum);
cli_setup_pkt(outbuf);
p = smb_buf(outbuf);
*p++ = 4;
safe_strcpy(p,fname, strlen(fname));
send_smb(Client,outbuf);
client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
DEBUG(5,("smbchkpath: %s\n",smb_errstr(inbuf)));
return(CVAL(inbuf,smb_rcls) == 0);
}
/****************************************************************************
Make a directory on share
***************************************************************************/
static BOOL smbmkdir(char *fname, char *inbuf, char *outbuf)
{
/* *must* be called with buffer ready malloc'ed */
char *p;
memset(outbuf,0,smb_size);
set_message(outbuf,0,2 + strlen(fname),True);
CVAL(outbuf,smb_com) = SMBmkdir;
SSVAL(outbuf,smb_tid,cnum);
cli_setup_pkt(outbuf);
p = smb_buf(outbuf);
*p++ = 4;
safe_strcpy(p,fname, strlen(fname));
send_smb(Client,outbuf);
client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
if (CVAL(inbuf,smb_rcls) != 0)
{
DEBUG(0,("%s making remote directory %s\n",
smb_errstr(inbuf),fname));
return(False);
}
return(True);
}
/****************************************************************************
Ensure a remote path exists (make if necessary)
***************************************************************************/
static BOOL ensurepath(char *fname, char *inbuf, char *outbuf)
{
/* *must* be called with buffer ready malloc'ed */
/* ensures path exists */
char *partpath, *ffname;
char *p=fname, *basehack;
DEBUG(5, ( "Ensurepath called with: %s\n", fname));
partpath = string_create_s(strlen(fname));
ffname = string_create_s(strlen(fname));
if ((partpath == NULL) || (ffname == NULL)){
DEBUG(0, ("Out of memory in ensurepath: %s\n", fname));
return(False);
}
*partpath = 0;
/* fname copied to ffname so can strtok */
safe_strcpy(ffname, fname, strlen(fname));
/* do a `basename' on ffname, so don't try and make file name directory */
if ((basehack=strrchr(ffname, '\\')) == NULL)
return True;
else
*basehack='\0';
p=strtok(ffname, "\\");
while (p)
{
safe_strcat(partpath, p, strlen(fname) + 1);
if (!smbchkpath(partpath, inbuf, outbuf)) {
if (!smbmkdir(partpath, inbuf, outbuf))
{
DEBUG(0, ("Error mkdirhiering\n"));
return False;
}
else
DEBUG(3, ("mkdirhiering %s\n", partpath));
}
safe_strcat(partpath, "\\", strlen(fname) + 1);
p = strtok(NULL,"/\\");
}
return True;
}
int padit(char *buf, int bufsize, int padsize)
{
int berr= 0;
int bytestowrite;
DEBUG(5, ("Padding with %d zeros\n", padsize));
memset(buf, 0, bufsize);
while( !berr && padsize > 0 ) {
bytestowrite= MIN(bufsize, padsize);
berr = dotarbuf(tarhandle, buf, bytestowrite) != bytestowrite;
padsize -= bytestowrite;
}
return berr;
}
/*
* smbclient functions
*/
/****************************************************************************
append one remote file to the tar file
***************************************************************************/
static void do_atar(char *rname,char *lname,file_info *finfo1)
{
int fnum;
uint32 nread=0;
char *p, ftype;
char *inbuf,*outbuf;
file_info2 finfo;
BOOL close_done = False;
BOOL shallitime=True;
BOOL ignore_close_error = False;
char *dataptr=NULL;
int datalen=0;
struct timeval tp_start;
GetTimeOfDay(&tp_start);
ftype = '0'; /* An ordinary file ... */
if (finfo1) {
finfo.size = finfo1 -> size;
finfo.mode = finfo1 -> mode;
finfo.uid = finfo1 -> uid;
finfo.gid = finfo1 -> gid;
finfo.mtime = finfo1 -> mtime;
finfo.atime = finfo1 -> atime;
finfo.ctime = finfo1 -> ctime;
}
else {
finfo.size = def_finfo.size;
finfo.mode = def_finfo.mode;
finfo.uid = def_finfo.uid;
finfo.gid = def_finfo.gid;
finfo.mtime = def_finfo.mtime;
finfo.atime = def_finfo.atime;
finfo.ctime = def_finfo.ctime;
}
inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
if (!inbuf || !outbuf)
{
DEBUG(0,("out of memory\n"));
return;
}
memset(outbuf,0,smb_size);
set_message(outbuf,15,1 + strlen(rname),True);
CVAL(outbuf,smb_com) = SMBopenX;
SSVAL(outbuf,smb_tid,cnum);
cli_setup_pkt(outbuf);
SSVAL(outbuf,smb_vwv0,0xFF);
SSVAL(outbuf,smb_vwv2,1);
SSVAL(outbuf,smb_vwv3,(DENY_NONE<<4));
SSVAL(outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
SSVAL(outbuf,smb_vwv5,aSYSTEM | aHIDDEN);
SSVAL(outbuf,smb_vwv8,1);
p = smb_buf(outbuf);
safe_strcpy(p, rname, strlen(rname));
p = skip_string(p,1);
dos_clean_name(rname);
/* do a chained openX with a readX? */
if (finfo.size > 0)
{
SSVAL(outbuf,smb_vwv0,SMBreadX);
SSVAL(outbuf,smb_vwv1,PTR_DIFF(p,outbuf) - 4);
memset(p,0,200);
p -= smb_wct;
SSVAL(p,smb_wct,10);
SSVAL(p,smb_vwv0,0xFF);
SSVAL(p,smb_vwv5,MIN(max_xmit-500,finfo.size));
SSVAL(p,smb_vwv9,MIN(0xFFFF,finfo.size));
smb_setlen(outbuf,smb_len(outbuf)+11*2+1);
}
send_smb(Client,outbuf);
client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
if (CVAL(inbuf,smb_rcls) != 0)
{
if (CVAL(inbuf,smb_rcls) == ERRSRV &&
SVAL(inbuf,smb_err) == ERRnoresource &&
cli_reopen_connection(inbuf,outbuf))
{
do_atar(rname,lname,finfo1);
free(inbuf);free(outbuf);
return;
}
DEBUG(0,("%s opening remote file %s\n",smb_errstr(inbuf),rname));
free(inbuf);free(outbuf);
return;
}
finfo.name = string_create_s(strlen(rname));
if (finfo.name == NULL) {
DEBUG(0, ("Unable to allocate space for finfo.name in do_atar\n"));
free(inbuf); free(outbuf);
return;
}
safe_strcpy(finfo.name,rname, strlen(rname));
if (!finfo1)
{
finfo.mode = SVAL(inbuf,smb_vwv3);
finfo.size = IVAL(inbuf,smb_vwv4);
finfo.mtime = make_unix_date3(inbuf+smb_vwv6);
finfo.atime = finfo.ctime = finfo.mtime;
}
DEBUG(3,("file %s attrib 0x%X\n",finfo.name,finfo.mode));
fnum = SVAL(inbuf,smb_vwv2);
if (tar_inc && !(finfo.mode & aARCH))
{
DEBUG(4, ("skipping %s - archive bit not set\n", finfo.name));
shallitime=0;
}
else if (!tar_system && (finfo.mode & aSYSTEM))
{
DEBUG(4, ("skipping %s - system bit is set\n", finfo.name));
shallitime=0;
}
else if (!tar_hidden && (finfo.mode & aHIDDEN))
{
DEBUG(4, ("skipping %s - hidden bit is set\n", finfo.name));
shallitime=0;
}
else
{
if (SVAL(inbuf,smb_vwv0) == SMBreadX)
{
p = (inbuf+4+SVAL(inbuf,smb_vwv1)) - smb_wct;
datalen = SVAL(p,smb_vwv5);
dataptr = inbuf + 4 + SVAL(p,smb_vwv6);
}
else
{
dataptr = NULL;
datalen = 0;
}
DEBUG(3,("getting file %s of size %d bytes as a tar file %s",
finfo.name,
finfo.size,
lname));
/* write a tar header, don't bother with mode - just set to 100644 */
writetarheader(tarhandle, rname, finfo.size, finfo.mtime, "100644 \0", ftype);
while (nread < finfo.size && !close_done)
{
int method = -1;
static BOOL can_chain_close=True;
p=NULL;
DEBUG(3,("nread=%d\n",nread));
/* 3 possible read types. readbraw if a large block is required.
readX + close if not much left and read if neither is supported */
/* we might have already read some data from a chained readX */
if (dataptr && datalen>0)
method=3;
/* if we can finish now then readX+close */
if (method<0 && can_chain_close && (Protocol >= PROTOCOL_LANMAN1) &&
((finfo.size - nread) <
(max_xmit - (2*smb_size + 13*SIZEOFWORD + 300))))
method = 0;
/* if we support readraw then use that */
if (method<0 && readbraw_supported)
method = 1;
/* if we can then use readX */
if (method<0 && (Protocol >= PROTOCOL_LANMAN1))
method = 2;
switch (method)
{
/* use readX */
case 0:
case 2:
if (method == 0)
close_done = True;
/* use readX + close */
memset(outbuf,0,smb_size);
set_message(outbuf,10,0,True);
CVAL(outbuf,smb_com) = SMBreadX;
SSVAL(outbuf,smb_tid,cnum);
cli_setup_pkt(outbuf);
if (close_done)
{
CVAL(outbuf,smb_vwv0) = SMBclose;
SSVAL(outbuf,smb_vwv1,PTR_DIFF(smb_buf(outbuf),outbuf) - 4);
}
else
CVAL(outbuf,smb_vwv0) = 0xFF;
SSVAL(outbuf,smb_vwv2,fnum);
SIVAL(outbuf,smb_vwv3,nread);
SSVAL(outbuf,smb_vwv5,MIN(max_xmit-200,finfo.size - nread));
SSVAL(outbuf,smb_vwv6,0);
SIVAL(outbuf,smb_vwv7,0);
SSVAL(outbuf,smb_vwv9,MIN(0xFFFF,finfo.size-nread));
if (close_done)
{
p = smb_buf(outbuf);
memset(p,0,9);
CVAL(p,0) = 3;
SSVAL(p,1,fnum);
SIVALS(p,3,-1);
/* now set the total packet length */
smb_setlen(outbuf,smb_len(outbuf)+9);
}
send_smb(Client,outbuf);
client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
if (CVAL(inbuf,smb_rcls) != 0)
{
DEBUG(0,("Error %s reading remote file\n",smb_errstr(inbuf)));
break;
}
if (close_done &&
SVAL(inbuf,smb_vwv0) != SMBclose)
{
/* NOTE: WfWg sometimes just ignores the chained
command! This seems to break the spec? */
DEBUG(3,("Rejected chained close?\n"));
close_done = False;
can_chain_close = False;
ignore_close_error = True;
}
datalen = SVAL(inbuf,smb_vwv5);
dataptr = inbuf + 4 + SVAL(inbuf,smb_vwv6);
break;
/* use readbraw */
case 1:
{
static int readbraw_size = 0xFFFF;
extern int Client;
memset(outbuf,0,smb_size);
set_message(outbuf,8,0,True);
CVAL(outbuf,smb_com) = SMBreadbraw;
SSVAL(outbuf,smb_tid,cnum);
cli_setup_pkt(outbuf);
SSVAL(outbuf,smb_vwv0,fnum);
SIVAL(outbuf,smb_vwv1,nread);
SSVAL(outbuf,smb_vwv3,MIN(finfo.size-nread,readbraw_size));
SSVAL(outbuf,smb_vwv4,0);
SIVALS(outbuf,smb_vwv5,-1);
send_smb(Client,outbuf);
/* Now read the raw data into the buffer and write it */
if(read_smb_length(Client,inbuf,0) == -1) {
DEBUG(0,("Failed to read length in readbraw\n"));
exit(1);
}
/* Even though this is not an smb message, smb_len
returns the generic length of an smb message */
datalen = smb_len(inbuf);
if (datalen == 0)
{
/* we got a readbraw error */
DEBUG(4,("readbraw error - reducing size\n"));
readbraw_size = (readbraw_size * 9) / 10;
if (readbraw_size < max_xmit)
{
DEBUG(0,("disabling readbraw\n"));
readbraw_supported = False;
}
dataptr=NULL;
continue;
}
if(read_data(Client,inbuf,datalen) != datalen) {
DEBUG(0,("Failed to read data in readbraw\n"));
exit(1);
}
dataptr = inbuf;
}
break;
case 3:
/* we've already read some data with a chained readX */
break;
default:
/* use plain read */
memset(outbuf,0,smb_size);
set_message(outbuf,5,0,True);
CVAL(outbuf,smb_com) = SMBread;
SSVAL(outbuf,smb_tid,cnum);
cli_setup_pkt(outbuf);
SSVAL(outbuf,smb_vwv0,fnum);
SSVAL(outbuf,smb_vwv1,MIN(max_xmit-200,finfo.size - nread));
SIVAL(outbuf,smb_vwv2,nread);
SSVAL(outbuf,smb_vwv4,finfo.size - nread);
send_smb(Client,outbuf);
client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
if (CVAL(inbuf,smb_rcls) != 0)
{
DEBUG(0,("Error %s reading remote file\n",smb_errstr(inbuf)));
break;
}
datalen = SVAL(inbuf,smb_vwv0);
dataptr = smb_buf(inbuf) + 3;
break;
}
/* add received bits of file to buffer - dotarbuf will
* write out in 512 byte intervals */
if (dotarbuf(tarhandle,dataptr,datalen) != datalen)
{
DEBUG(0,("Error writing to tar file - %s\n", strerror(errno)));
break;
}
nread += datalen;
if (datalen == 0)
{
DEBUG(0,("Error reading file %s. Got 0 bytes\n", rname));
break;
}
dataptr=NULL;
datalen=0;
}
/* pad tar file with zero's if we couldn't get entire file */
if (nread < finfo.size)
{
DEBUG(0, ("Didn't get entire file. size=%d, nread=%d\n", finfo.size, nread));
if (padit(inbuf, BUFFER_SIZE, finfo.size - nread))
DEBUG(0,("Error writing tar file - %s\n", strerror(errno)));
}
/* round tar file to nearest block */
if (finfo.size % TBLOCK)
dozerobuf(tarhandle, TBLOCK - (finfo.size % TBLOCK));
ttarf+=finfo.size + TBLOCK - (finfo.size % TBLOCK);
ntarf++;
}
if (!close_done)
{
memset(outbuf,0,smb_size);
set_message(outbuf,3,0,True);
CVAL(outbuf,smb_com) = SMBclose;
SSVAL(outbuf,smb_tid,cnum);
cli_setup_pkt(outbuf);
SSVAL(outbuf,smb_vwv0,fnum);
SIVALS(outbuf,smb_vwv1,-1);
send_smb(Client,outbuf);
client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
if (!ignore_close_error && CVAL(inbuf,smb_rcls) != 0)
{
DEBUG(0,("Error %s closing remote file\n",smb_errstr(inbuf)));
free(inbuf);free(outbuf);
return;
}
}
if (shallitime)
{
struct timeval tp_end;
int this_time;
/* if shallitime is true then we didn't skip */
if (tar_reset) (void) do_setrattr(finfo.name, aARCH, ATTRRESET);
GetTimeOfDay(&tp_end);
this_time =
(tp_end.tv_sec - tp_start.tv_sec)*1000 +
(tp_end.tv_usec - tp_start.tv_usec)/1000;
get_total_time_ms += this_time;
get_total_size += finfo.size;
/* Thanks to Carel-Jan Engel (ease@mail.wirehub.nl) for this one */
DEBUG(3,("(%g kb/s) (average %g kb/s)\n",
finfo.size / MAX(0.001, (1.024*this_time)),
get_total_size / MAX(0.001, (1.024*get_total_time_ms))));
if (tar_noisy)
{
printf("%10d (%7.1f kb/s) %s\n",
finfo.size, finfo.size / MAX(0.001, (1.024*this_time)),
finfo.name);
}
}
free(inbuf);free(outbuf);
}
/****************************************************************************
Append single file to tar file (or not)
***************************************************************************/
static void do_tar(file_info *finfo)
{
pstring rname;
if (strequal(finfo->name,".."))
return;
/* Is it on the exclude list ? */
if (!tar_excl && clipn) {
pstring exclaim;
DEBUG(5, ("Excl: strlen(cur_dir) = %i\n", strlen(cur_dir)));
safe_strcpy(exclaim, cur_dir, sizeof(pstring));
*(exclaim+strlen(exclaim)-1)='\0';
safe_strcat(exclaim, "\\", sizeof(pstring));
safe_strcat(exclaim, finfo->name, sizeof(exclaim));
DEBUG(5, ("...tar_re_search: %d\n", tar_re_search));
if ((!tar_re_search && clipfind(cliplist, clipn, exclaim)) ||
#ifdef HAVE_REGEX_H
(tar_re_search && !regexec(preg, exclaim, 0, NULL, 0))) {
#else
(tar_re_search && mask_match(exclaim, cliplist[0], True, False))) {
#endif
DEBUG(3,("Skipping file %s\n", exclaim));
return;
}
}
if (finfo->mode & aDIR)
{
pstring saved_curdir;
pstring mtar_mask;
char *inbuf,*outbuf;
inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
if (!inbuf || !outbuf)
{
DEBUG(0,("out of memory\n"));
return;
}
safe_strcpy(saved_curdir, cur_dir, sizeof(saved_curdir));
DEBUG(5, ("Sizeof(cur_dir)=%i, strlen(cur_dir)=%i, strlen(finfo->name)=%i\nname=%s,cur_dir=%s\n", sizeof(cur_dir), strlen(cur_dir), strlen(finfo->name), finfo->name, cur_dir));
safe_strcat(cur_dir,finfo->name, sizeof(cur_dir));
safe_strcat(cur_dir,"\\", sizeof(cur_dir));
DEBUG(5, ("Writing a dir, Name = %s\n", cur_dir));
/* write a tar directory, don't bother with mode - just set it to
* 40755 */
writetarheader(tarhandle, cur_dir, 0, finfo->mtime, "040755 \0", '5');
if (tar_noisy) {
printf(" directory %s\n", cur_dir);
}
ntarf++; /* Make sure we have a file on there */
safe_strcpy(mtar_mask,cur_dir, sizeof(pstring));
safe_strcat(mtar_mask,"*", sizeof(pstring));
/* do_dir((char *)inbuf,(char *)outbuf,mtar_mask,attribute,do_tar,recurse,True); */
safe_strcpy(cur_dir,saved_curdir, sizeof(pstring));
free(inbuf);free(outbuf);
}
else
{
safe_strcpy(rname,cur_dir, sizeof(pstring));
safe_strcat(rname,finfo->name, sizeof(pstring));
do_atar(rname,finfo->name,finfo);
}
}
/****************************************************************************
Convert from UNIX to DOS file names
***************************************************************************/
static void unfixtarname(char *tptr, char *fp, int l, BOOL first)
{
/* remove '.' from start of file name, convert from unix /'s to
* dos \'s in path. Kill any absolute path names. But only if first!
*/
DEBUG(5, ("firstb=%X, secondb=%X, len=%i\n", tptr, fp, l));
if (first) {
if (*fp == '.') {
fp++;
l--;
}
if (*fp == '\\' || *fp == '/') {
fp++;
l--;
}
}
while (l > 0) {
int skip;
if(( skip = skip_multibyte_char( *fp )) != 0) {
if (skip == 2) {
*tptr++ = *fp++;
*tptr++ = *fp++;
l -= 2;
} else if (skip == 1) {
*tptr++ = *fp++;
l--;
}
} else if (*fp == '/') {
*tptr++ = '\\';
fp++;
l--;
} else {
*tptr++ = *fp++;
l--;
}
}
}
#if 0 /* Removed to get around gcc 'defined but not used' error. */
/****************************************************************************
Move to the next block in the buffer, which may mean read in another set of
blocks.
****************************************************************************/
static int next_block(char *ltarbuf, char *bufferp, int bufsiz)
{
int bufread, total = 0;
if (bufferp >= (ltarbuf + bufsiz)) {
for (bufread = read(tarhandle, ltarbuf, bufsiz); total < bufsiz; total += bufread) {
if (bufread <= 0) { /* An error, return false */
return (total > 0 ? -2 : bufread);
}
}
bufferp = ltarbuf;
}
else {
bufferp += TBLOCK;
}
return(0);
}
static int skip_file(int skip)
{
return(0);
}
static int get_file(file_info2 finfo)
{
return(0);
}
static int get_dir(file_info2 finfo)
{
return(0);
}
static char * get_longfilename(file_info2 finfo)
{
return(NULL);
}
static char * bufferp;
static void do_tarput2(void)
{
file_info2 finfo, *finfo2;
struct timeval tp_start;
char *inbuf, *outbuf, *longfilename = NULL;
int skip = False;
GetTimeOfDay(&tp_start);
bufferp = tarbuf + tbufsiz; /* init this to force first read */
if (push_dir(&dir_stack, &finfo)) {
finfo2 = pop_dir(&dir_stack);
inbuf = top_dir_name(&dir_stack); /* FIXME */
if (sub_dir(inbuf, finfo2 -> name)){
DEBUG(0, (""));
}
}
inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
if (!inbuf || !outbuf) {
DEBUG(0, ("Out of memory during allocate of inbuf and outbuf!\n"));
return;
}
if (next_block(tarbuf, bufferp, tbufsiz) <= 0) {
DEBUG(0, ("Empty file or short tar file: %s\n", strerror(errno)));
}
/* Now read through those files ... */
while (True) {
switch (readtarheader((union hblock *) bufferp, &finfo, cur_dir)) {
case -2: /* Hmm, not good, but not fatal */
DEBUG(0, ("Skipping %s...\n", finfo.name));
if ((next_block(tarbuf, bufferp, tbufsiz) <= 0) &&
!skip_file(finfo.size)) {
DEBUG(0, ("Short file, bailing out...\n"));
free(inbuf); free(outbuf);
continue;
}
break;
case -1:
DEBUG(0, ("abandoning restore, -1 from read tar header\n"));
free(inbuf); free(outbuf);
return;
case 0: /* chksum is zero - looks like an EOF */
DEBUG(0, ("total of %d tar files restored to share\n", ntarf));
free(inbuf); free(outbuf);
return; /* Hmmm, bad here ... */
default:
break;
}
/* Now, do we have a long file name? */
if (longfilename != NULL) {
if (strlen(longfilename) < sizeof(finfo.name)) { /* if we have space */
strncpy(finfo.name, longfilename, sizeof(finfo.name) - 1);
free(longfilename);
longfilename = NULL;
}
else {
DEBUG(0, ("filename: %s too long, skipping\n", strlen(longfilename)));
skip = True;
}
}
/* Well, now we have a header, process the file ... */
/* Should we skip the file? */
if (skip) {
skip_file(finfo.size);
continue;
}
/* We only get this far if we should process the file */
switch (((union hblock *)bufferp) -> dbuf.linkflag) {
case '0': /* Should use symbolic names--FIXME */
get_file(finfo);
break;
case '5':
get_dir(finfo);
break;
case 'L':
longfilename = get_longfilename(finfo);
break;
default:
skip_file(finfo.size); /* Don't handle these yet */
break;
}
}
}
#endif /* Removed to get around gcc 'defined but not used' error. */
static void do_tarput()
{
file_info2 finfo;
int nread=0, bufread;
char *inbuf,*outbuf, *longname = NULL;
int fsize=0;
int fnum;
struct timeval tp_start;
BOOL tskip=False; /* We'll take each file as it comes */
finfo.name = NULL; /* No name in here ... */
GetTimeOfDay(&tp_start);
inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
if (!inbuf || !outbuf)
{
DEBUG(0,("out of memory\n"));
return;
}
/*
* Must read in tbufsiz dollops
*/
/* These should be the only reads in clitar.c */
while ((bufread=read(tarhandle, tarbuf, tbufsiz))>0) {
char *buffer_p, *endofbuffer;
int chunk;
/* Code to handle a short read.
* We always need a TBLOCK full of stuff
*/
if (bufread % TBLOCK) {
int lchunk=TBLOCK-(bufread % TBLOCK);
int lread;
/* It's a shorty - a short read that is */
DEBUG(3, ("Short read, read %d so far (need %d)\n", bufread, lchunk));
while ((lread=read(tarhandle, tarbuf+bufread, lchunk))>0) {
bufread+=lread;
if (!(lchunk-=lread)) break;
}
/* If we've reached EOF then that must be a short file */
if (lread<=0) break;
}
buffer_p=tarbuf;
endofbuffer=tarbuf+bufread;
if (tskip) {
if (fsize<bufread) {
tskip=False;
buffer_p+=fsize;
fsize=0;
} else {
if (fsize==bufread) tskip=False;
fsize-=bufread;
continue;
}
}
do {
if (!fsize)
{
int next_header = 1; /* Want at least one header */
while (next_header)
{
if (buffer_p >= endofbuffer) {
bufread = read(tarhandle, tarbuf, tbufsiz);
buffer_p = tarbuf;
}
next_header = 0; /* Don't want the next one ... */
if (finfo.name != NULL) { /* Free the space */
free(finfo.name);
finfo.name = NULL;
}
DEBUG(5, ("Tarbuf=%X, buffer=%X, endofbuf=%X\n", tarbuf, buffer_p, endofbuffer));
switch (readtarheader((union hblock *) buffer_p, &finfo, cur_dir))
{
case -2: /* something dodgy but not fatal about this */
DEBUG(0, ("skipping %s...\n", finfo.name));
buffer_p+=TBLOCK; /* header - like a link */
continue;
case -1:
DEBUG(0, ("abandoning restore, -1 from readtarheader\n"));
free(inbuf); free(outbuf);
return;
case 0: /* chksum is zero - we assume that one all zero
*header block will do for eof */
DEBUG(0,
("total of %d tar files restored to share\n", ntarf));
free(inbuf); free(outbuf);
return;
default:
break;
}
/* If we have a longname left from the last time through,
copy it into finfo.name and free it.
The size of a pstring is the limiting factor on filenames
and directory names now. The total pathname length must be
less than sizeof(pstring) - 1, which is currently 1023. */
if (longname != NULL) {
free(finfo.name); /* Free the name in the finfo */
finfo.name = string_create_s(strlen(longname) + 2);
strncpy(finfo.name, longname, strlen(longname) + 1);
DEBUG(5, ("Long name = \"%s\", filename=\"%s\"\n", longname, finfo.name));
free(longname);
longname = NULL;
}
/* Check if a long-link. We do this before the clip checking
because clip-checking should clip on real name - RJS */
if (((union hblock *)buffer_p) -> dbuf.linkflag == 'L') {
int file_len, first = 0; char *cp;
/* Skip this header, but pick up length, get the name and
fix the name and skip the name. Hmmm, what about end of
buffer??? */
longname = malloc(finfo.size + strlen(cur_dir) + 1);
if (longname == NULL) {
DEBUG(0, ("could not allocate buffer of size %d for longname\n",
finfo.size + strlen(cur_dir) + 1)
);
free(inbuf); free(outbuf);
return;
}
buffer_p += TBLOCK; /* Skip that longlink header */
/* This needs restructuring ... */
strncpy(longname, cur_dir, strlen(cur_dir) + 1);
cp = longname + strlen(cur_dir);
file_len = finfo.size;
while (file_len > 0) {
if (buffer_p >= endofbuffer) {
bufread = read(tarhandle, tarbuf, tbufsiz);
buffer_p = tarbuf;
}
unfixtarname(cp, buffer_p, file_len >= TBLOCK?TBLOCK:file_len, first == 0);
first++; /* Not the first anymore */
cp = cp + strlen(cp); /* Move to end of string */
buffer_p += TBLOCK;
file_len -= TBLOCK;
next_header = 1; /* Force read of next header */
}
}
}
tskip=clipn
&& ((!tar_re_search && clipfind(cliplist, clipn, finfo.name) ^ tar_excl)
#ifdef HAVE_REGEX_H
|| (tar_re_search && !regexec(preg, finfo.name, 0, NULL, 0)));
#else
|| (tar_re_search && mask_match(finfo.name, cliplist[0], True, False)));
#endif
if (tskip) {
buffer_p+=TBLOCK;
if (finfo.mode & aDIR)
continue;
else if ((fsize=finfo.size) % TBLOCK) {
fsize+=TBLOCK-(fsize%TBLOCK);
}
if (fsize<endofbuffer-buffer_p) {
buffer_p+=fsize;
fsize=0;
continue;
} else {
fsize-=endofbuffer-buffer_p;
break;
}
}
DEBUG(5, ("do_tarput: File is: %s\n", finfo.name));
if (finfo.mode & aDIR)
{
DEBUG(5, ("Creating directory: %s\n", finfo.name));
DEBUG(0, ("restore tar dir %s of size %d bytes\n",
finfo.name, finfo.size));
if (!ensurepath(finfo.name, inbuf, outbuf))
{
DEBUG(0, ("abandoning restore, problems ensuring path\n"));
free(inbuf); free(outbuf);
return;
}
else
{
/* Now we update the creation date ... */
DEBUG(5, ("Updating creation date on %s\n", finfo.name));
if (!do_setrtime(finfo.name, finfo.mtime, True)) {
if (tar_real_noisy) {
DEBUG(0, ("Could not set time on file: %s\n", finfo.name));
}
/*return; /* Win 95 does not like setting time on dirs */
}
ntarf++;
buffer_p+=TBLOCK;
continue;
}
}
fsize=finfo.size;
if (ensurepath(finfo.name, inbuf, outbuf)
&& !smbcreat(finfo, &fnum, inbuf, outbuf))
{
DEBUG(0, ("abandoning restore\n"));
free(inbuf);free(outbuf);
return;
}
DEBUG(0 ,("restore tar file %s of size %d bytes\n",
finfo.name, finfo.size));
/* if (!finfo.size) {
if (!smbshut(finfo, fnum, inbuf, outbuf)){
DEBUG(0, ("Error closing remote file of length 0: %s\n", finfo.name));
free(inbuf);free(outbuf);
return;
}
} */
nread=0;
if ((buffer_p+=TBLOCK) >= endofbuffer) break;
} /* if (!fsize) */
/* write out the file in chunk sized chunks - don't
* go past end of buffer though */
chunk=(fsize-nread < endofbuffer - buffer_p)
? fsize - nread : endofbuffer - buffer_p;
while (chunk > 0) {
int minichunk=MIN(chunk, max_xmit-200);
if (!smbwrite(fnum, /* file descriptor */
minichunk, /* n */
nread, /* offset low */
0, /* offset high - not implemented */
fsize-nread, /* left - only hint to server */
buffer_p,
inbuf,
outbuf))
{
DEBUG(0, ("Error writing remote file\n"));
free(inbuf); free(outbuf);
return;
}
DEBUG(5, ("chunk writing fname=%s fnum=%d nread=%d minichunk=%d chunk=%d size=%d\n", finfo.name, fnum, nread, minichunk, chunk, fsize));
buffer_p+=minichunk; nread+=minichunk;
chunk-=minichunk;
}
if (nread>=fsize)
{
if (!smbshut(finfo, fnum, inbuf, outbuf))
{
DEBUG(0, ("Error closing remote file\n"));
free(inbuf);free(outbuf);
return;
}
if (fsize % TBLOCK) buffer_p+=TBLOCK - (fsize % TBLOCK);
DEBUG(5, ("buffer_p is now %d (psn=%d)\n",
(long) buffer_p, (long)(buffer_p - tarbuf)));
ntarf++;
fsize=0;
}
} while (buffer_p < endofbuffer);
}
DEBUG(0, ("premature eof on tar file ?\n"));
DEBUG(0,("total of %d tar files restored to share\n", ntarf));
free(inbuf); free(outbuf);
}
/*
* samba interactive commands
*/
/****************************************************************************
Blocksize command
***************************************************************************/
void cmd_block(char *dum_in, char *dum_out)
{
fstring buf;
int block;
if (!next_token(NULL,buf,NULL))
{
DEBUG(0, ("blocksize <n>\n"));
return;
}
block=atoi(buf);
if (block < 0 || block > 65535)
{
DEBUG(0, ("blocksize out of range"));
return;
}
blocksize=block;
DEBUG(2,("blocksize is now %d\n", blocksize));
}
/****************************************************************************
command to set incremental / reset mode
***************************************************************************/
void cmd_tarmode(char *dum_in, char *dum_out)
{
fstring buf;
while (next_token(NULL,buf,NULL)) {
if (strequal(buf, "full"))
tar_inc=False;
else if (strequal(buf, "inc"))
tar_inc=True;
else if (strequal(buf, "reset"))
tar_reset=True;
else if (strequal(buf, "noreset"))
tar_reset=False;
else if (strequal(buf, "system"))
tar_system=True;
else if (strequal(buf, "nosystem"))
tar_system=False;
else if (strequal(buf, "hidden"))
tar_hidden=True;
else if (strequal(buf, "nohidden"))
tar_hidden=False;
else if (strequal(buf, "verbose") || strequal(buf, "noquiet"))
tar_noisy=True;
else if (strequal(buf, "quiet") || strequal(buf, "noverbose"))
tar_noisy=False;
else DEBUG(0, ("tarmode: unrecognised option %s\n", buf));
}
DEBUG(0, ("tarmode is now %s, %s, %s, %s, %s\n",
tar_inc ? "incremental" : "full",
tar_system ? "system" : "nosystem",
tar_hidden ? "hidden" : "nohidden",
tar_reset ? "reset" : "noreset",
tar_noisy ? "verbose" : "quiet"));
}
/****************************************************************************
Feeble attrib command
***************************************************************************/
void cmd_setmode(char *dum_in, char *dum_out)
{
char *q;
fstring buf;
pstring fname;
int attra[2];
int direct=1;
attra[0] = attra[1] = 0;
if (!next_token(NULL,buf,NULL))
{
DEBUG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
return;
}
safe_strcpy(fname, cur_dir, sizeof(pstring));
safe_strcat(fname, buf, sizeof(pstring));
while (next_token(NULL,buf,NULL)) {
q=buf;
while(*q)
switch (*q++) {
case '+': direct=1;
break;
case '-': direct=0;
break;
case 'r': attra[direct]|=aRONLY;
break;
case 'h': attra[direct]|=aHIDDEN;
break;
case 's': attra[direct]|=aSYSTEM;
break;
case 'a': attra[direct]|=aARCH;
break;
default: DEBUG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
return;
}
}
if (attra[ATTRSET]==0 && attra[ATTRRESET]==0)
{
DEBUG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
return;
}
DEBUG(2, ("\nperm set %d %d\n", attra[ATTRSET], attra[ATTRRESET]));
(void) do_setrattr(fname, attra[ATTRSET], ATTRSET);
(void) do_setrattr(fname, attra[ATTRRESET], ATTRRESET);
}
/****************************************************************************
Principal command for creating / extracting
***************************************************************************/
void cmd_tar(char *inbuf, char *outbuf)
{
fstring buf;
char **argl;
int argcl;
if (!next_token(NULL,buf,NULL))
{
DEBUG(0,("tar <c|x>[IXbga] <filename>\n"));
return;
}
argl=toktocliplist(&argcl, NULL);
if (!tar_parseargs(argcl, argl, buf, 0))
return;
process_tar(inbuf, outbuf);
free(argl);
}
/****************************************************************************
Command line (option) version
***************************************************************************/
int process_tar(char *inbuf, char *outbuf)
{
initarbuf();
switch(tar_type) {
case 'x':
#if 0
do_tarput2();
#else
do_tarput();
#endif
free(tarbuf);
close(tarhandle);
break;
case 'r':
case 'c':
if (clipn && tar_excl) {
int i;
pstring tarmac;
for (i=0; i<clipn; i++) {
DEBUG(5,("arg %d = %s\n", i, cliplist[i]));
if (*(cliplist[i]+strlen(cliplist[i])-1)=='\\') {
*(cliplist[i]+strlen(cliplist[i])-1)='\0';
}
if (strrchr(cliplist[i], '\\')) {
pstring saved_dir;
safe_strcpy(saved_dir, cur_dir, sizeof(pstring));
if (*cliplist[i]=='\\') {
safe_strcpy(tarmac, cliplist[i], sizeof(pstring));
} else {
safe_strcpy(tarmac, cur_dir, sizeof(pstring));
safe_strcat(tarmac, cliplist[i], sizeof(pstring));
}
safe_strcpy(cur_dir, tarmac, sizeof(pstring));
*(strrchr(cur_dir, '\\')+1)='\0';
do_dir((char *)inbuf,(char *)outbuf,tarmac,attribute,do_tar,recurse, True);
safe_strcpy(cur_dir,saved_dir, sizeof(pstring));
} else {
safe_strcpy(tarmac, cur_dir, sizeof(pstring));
safe_strcat(tarmac, cliplist[i], sizeof(pstring));
do_dir((char *)inbuf,(char *)outbuf,tarmac,attribute,do_tar,recurse, True);
}
}
} else {
pstring mask;
safe_strcpy(mask,cur_dir, sizeof(pstring));
safe_strcat(mask,"\\*", sizeof(pstring));
do_dir((char *)inbuf,(char *)outbuf,mask,attribute,do_tar,recurse, True);
}
if (ntarf) dotareof(tarhandle);
close(tarhandle);
free(tarbuf);
DEBUG(0, ("tar: dumped %d tar files\n", ntarf));
DEBUG(0, ("Total bytes written: %d\n", ttarf));
break;
}
if (must_free_cliplist) {
int i;
for (i = 0; i < clipn; ++i) {
free(cliplist[i]);
}
free(cliplist);
cliplist = NULL;
clipn = 0;
must_free_cliplist = False;
}
return(0);
}
/****************************************************************************
Find a token (filename) in a clip list
***************************************************************************/
int clipfind(char **aret, int ret, char *tok)
{
if (aret==NULL) return 0;
/* ignore leading slashes or dots in token */
while(strchr("/\\.", *tok)) tok++;
while(ret--) {
char *pkey=*aret++;
/* ignore leading slashes or dots in list */
while(strchr("/\\.", *pkey)) pkey++;
if (!strslashcmp(pkey, tok)) return 1;
}
return 0;
}
/****************************************************************************
Read list of files to include from the file and initialize cliplist
accordingly.
***************************************************************************/
static int read_inclusion_file(char *filename)
{
FILE *inclusion = NULL;
char buf[MAXPATHLEN + 1];
char *inclusion_buffer = NULL;
int inclusion_buffer_size = 0;
int inclusion_buffer_sofar = 0;
char *p;
char *tmpstr;
int i;
int error = 0;
clipn = 0;
buf[MAXPATHLEN] = '\0'; /* guarantee null-termination */
if ((inclusion = fopen(filename, "r")) == NULL) {
/* XXX It would be better to include a reason for failure, but without
* autoconf, it's hard to use strerror, sys_errlist, etc.
*/
DEBUG(0,("Unable to open inclusion file %s\n", filename));
return 0;
}
while ((! error) && (fgets(buf, sizeof(buf)-1, inclusion))) {
if (inclusion_buffer == NULL) {
inclusion_buffer_size = 1024;
if ((inclusion_buffer = malloc(inclusion_buffer_size)) == NULL) {
DEBUG(0,("failure allocating buffer to read inclusion file\n"));
error = 1;
break;
}
}
if (buf[strlen(buf)-1] == '\n') {
buf[strlen(buf)-1] = '\0';
}
if ((strlen(buf) + 1 + inclusion_buffer_sofar) >= inclusion_buffer_size) {
inclusion_buffer_size *= 2;
inclusion_buffer = Realloc(inclusion_buffer,inclusion_buffer_size);
if (! inclusion_buffer) {
DEBUG(0,("failure enlarging inclusion buffer to %d bytes\n",
inclusion_buffer_size));
error = 1;
break;
}
}
safe_strcpy(inclusion_buffer + inclusion_buffer_sofar, buf, inclusion_buffer_size - inclusion_buffer_sofar);
inclusion_buffer_sofar += strlen(buf) + 1;
clipn++;
}
fclose(inclusion);
if (! error) {
/* Allocate an array of clipn + 1 char*'s for cliplist */
cliplist = malloc((clipn + 1) * sizeof(char *));
if (cliplist == NULL) {
DEBUG(0,("failure allocating memory for cliplist\n"));
error = 1;
} else {
cliplist[clipn] = NULL;
p = inclusion_buffer;
for (i = 0; (! error) && (i < clipn); i++) {
/* set current item to NULL so array will be null-terminated even if
* malloc fails below. */
cliplist[i] = NULL;
if ((tmpstr = (char *)malloc(strlen(p)+1)) == NULL) {
DEBUG(0, ("Could not allocate space for a cliplist item, # %i\n", i));
error = 1;
} else {
unfixtarname(tmpstr, p, strlen(p) + 1, True);
cliplist[i] = tmpstr;
if ((p = strchr(p, '\000')) == NULL) {
DEBUG(0,("INTERNAL ERROR: inclusion_buffer is of unexpected contents.\n"));
abort();
}
}
++p;
}
must_free_cliplist = True;
}
}
if (inclusion_buffer) {
free(inclusion_buffer);
}
if (error) {
if (cliplist) {
char **pp;
/* We know cliplist is always null-terminated */
for (pp = cliplist; *pp; ++pp) {
free(*pp);
}
free(cliplist);
cliplist = NULL;
must_free_cliplist = False;
}
return 0;
}
/* cliplist and its elements are freed at the end of process_tar. */
return 1;
}
/****************************************************************************
Parse tar arguments. Sets tar_type, tar_excl, etc.
***************************************************************************/
int tar_parseargs(int argc, char *argv[], char *Optarg, int Optind)
{
char tar_clipfl='\0';
/* Reset back to defaults - could be from interactive version
* reset mode and archive mode left as they are though
*/
tar_type='\0';
tar_excl=True;
while (*Optarg)
switch(*Optarg++) {
case 'c':
tar_type='c';
break;
case 'x':
if (tar_type=='c') {
printf("Tar must be followed by only one of c or x.\n");
return 0;
}
tar_type='x';
break;
case 'b':
if (Optind>=argc || !(blocksize=atoi(argv[Optind]))) {
DEBUG(0,("Option b must be followed by valid blocksize\n"));
return 0;
} else {
Optind++;
}
break;
case 'g':
tar_inc=True;
break;
case 'N':
if (Optind>=argc) {
DEBUG(0,("Option N must be followed by valid file name\n"));
return 0;
} else {
struct stat stbuf;
extern time_t newer_than;
if (sys_stat(argv[Optind], &stbuf) == 0) {
newer_than = stbuf.st_mtime;
DEBUG(1,("Getting files newer than %s",
asctime(LocalTime(&newer_than))));
Optind++;
} else {
DEBUG(0,("Error setting newer-than time\n"));
return 0;
}
}
break;
case 'a':
tar_reset=True;
break;
case 'I':
if (tar_clipfl) {
DEBUG(0,("Only one of I,X,F must be specified\n"));
return 0;
}
tar_clipfl='I';
break;
case 'X':
if (tar_clipfl) {
DEBUG(0,("Only one of I,X,F must be specified\n"));
return 0;
}
tar_clipfl='X';
break;
case 'F':
if (tar_clipfl) {
DEBUG(0,("Only one of I,X,F must be specified\n"));
return 0;
}
tar_clipfl='F';
break;
case 'r':
DEBUG(0, ("tar_re_search set\n"));
tar_re_search = True;
break;
default:
DEBUG(0,("Unknown tar option\n"));
return 0;
}
if (!tar_type) {
printf("Option T must be followed by one of c or x.\n");
return 0;
}
/* tar_excl is true if cliplist lists files to be included.
* Both 'I' and 'F' mean include. */
tar_excl=tar_clipfl!='X';
if (tar_clipfl=='F') {
if (argc-Optind-1 != 1) {
DEBUG(0,("Option F must be followed by exactly one filename.\n"));
return 0;
}
if (! read_inclusion_file(argv[Optind+1])) {
return 0;
}
} else if (Optind+1<argc && !tar_re_search) { /* For backwards compatibility */
char *tmpstr;
char **tmplist;
int clipcount;
cliplist=argv+Optind+1;
clipn=argc-Optind-1;
clipcount = clipn;
if ((tmplist=malloc(clipn*sizeof(char *))) == NULL) {
DEBUG(0, ("Could not allocate space to process cliplist, count = %i\n",
clipn)
);
return 0;
}
for (clipcount = 0; clipcount < clipn; clipcount++) {
DEBUG(5, ("Processing an item, %s\n", cliplist[clipcount]));
if ((tmpstr = (char *)malloc(strlen(cliplist[clipcount])+1)) == NULL) {
DEBUG(0, ("Could not allocate space for a cliplist item, # %i\n",
clipcount)
);
return 0;
}
unfixtarname(tmpstr, cliplist[clipcount], strlen(cliplist[clipcount]) + 1, True);
tmplist[clipcount] = tmpstr;
DEBUG(5, ("Processed an item, %s\n", tmpstr));
DEBUG(5, ("Cliplist is: %s\n", cliplist[0]));
}
cliplist = tmplist;
must_free_cliplist = True;
}
if (Optind+1<argc && tar_re_search) { /* Doing regular expression seaches */
#ifdef HAVE_REGEX_H
int errcode;
if ((preg = (regex_t *)malloc(65536)) == NULL) {
DEBUG(0, ("Could not allocate buffer for regular expression search\n"));
return;
}
if (errcode = regcomp(preg, argv[Optind + 1], REG_EXTENDED)) {
char errstr[1024];
size_t errlen;
errlen = regerror(errcode, preg, errstr, sizeof(errstr) - 1);
DEBUG(0, ("Could not compile pattern buffer for re search: %s\n%s\n", argv[Optind + 1], errstr));
return;
}
#endif
clipn=argc-Optind-1;
cliplist=argv+Optind+1;
}
if (Optind>=argc || !strcmp(argv[Optind], "-")) {
/* Sets tar handle to either 0 or 1, as appropriate */
tarhandle=(tar_type=='c');
} else {
if ((tar_type=='x' && (tarhandle = open(argv[Optind], O_RDONLY)) == -1)
|| (tar_type=='c' && (tarhandle=creat(argv[Optind], 0644)) < 0))
{
DEBUG(0,("Error opening local file %s - %s\n",
argv[Optind], strerror(errno)));
return(0);
}
}
return 1;
}
|