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
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
|
/* This file is part of the KDE libraries
Copyright (C) 2000 David Faure <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
Recommended reading explaining FTP details and quirks:
http://cr.yp.to/ftp.html (by D.J. Bernstein)
*/
// fix type conversion error josswern 23.02.2011
#define TDEIO_FTP_PRIVATE_INCLUDE
#include "ftp.h"
#include <sys/stat.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#if TIME_WITH_SYS_TIME
#include <time.h>
#endif
#include <ntqdir.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <kinstance.h>
#include <kmimemagic.h>
#include <kmimetype.h>
#include <ksockaddr.h>
#include <tdeio/ioslave_defaults.h>
#include <tdeio/slaveconfig.h>
#include <kremoteencoding.h>
#include <klargefile.h>
#ifdef HAVE_STRTOLL
#define charToLongLong(a) strtoll(a, 0, 10)
#else
#define charToLongLong(a) strtol(a, 0, 10)
#endif
// JPF: a remark on coding style (2004-03-06):
// Some calls to TQString::fromLatin1() were removed from the code. In most places
// the KDE code relies on implicit creation of TQStrings. Also TQt has a lot of
// const char* overloads, so that using TQString::fromLatin1() can be ineffectient!
#define FTP_LOGIN "anonymous"
#define FTP_PASSWD "anonymous@"
//#undef kdDebug
#define ENABLE_CAN_RESUME
// JPF: somebody should find a better solution for this or move this to TDEIO
// JPF: anyhow, in KDE 3.2.0 I found diffent MAX_IPC_SIZE definitions!
namespace TDEIO {
enum buffersizes
{ /**
* largest buffer size that should be used to transfer data between
* TDEIO slaves using the data() function
*/
maximumIpcSize = 32 * 1024,
/**
* this is a reasonable value for an initial read() that a TDEIO slave
* can do to obtain data via a slow network connection.
*/
initialIpcSize = 2 * 1024,
/**
* recommended size of a data block passed to findBufferFileType()
*/
mimimumMimeSize = 1024
};
// JPF: this helper was derived from write_all in file.cpp (FileProtocol).
static // JPF: in ftp.cpp we make it static
/**
* This helper handles some special issues (blocking and interrupted
* system call) when writing to a file handle.
*
* @return 0 on success or an error code on failure (ERR_COULD_NOT_WRITE,
* ERR_DISK_FULL, ERR_CONNECTION_BROKEN).
*/
int WriteToFile(int fd, const char *buf, size_t len)
{
while (len > 0)
{ // JPF: shouldn't there be a KDE_write?
ssize_t written = write(fd, buf, len);
if (written >= 0)
{ buf += written;
len -= written;
continue;
}
switch(errno)
{ case EINTR: continue;
case EPIPE: return ERR_CONNECTION_BROKEN;
case ENOSPC: return ERR_DISK_FULL;
default: return ERR_COULD_NOT_WRITE;
}
}
return 0;
}
}
TDEIO::filesize_t Ftp::UnknownSize = (TDEIO::filesize_t)-1;
using namespace TDEIO;
extern "C" { KDE_EXPORT int kdemain(int argc, char **argv); }
int kdemain( int argc, char **argv )
{
TDELocale::setMainCatalogue("tdelibs");
TDEInstance instance( "tdeio_ftp" );
( void ) TDEGlobal::locale();
kdDebug(7102) << "Starting " << getpid() << endl;
if (argc != 4)
{
fprintf(stderr, "Usage: tdeio_ftp protocol domain-socket1 domain-socket2\n");
exit(-1);
}
Ftp slave(argv[1], argv[2], argv[3]);
slave.dispatchLoop();
kdDebug(7102) << "Done" << endl;
return 0;
}
//===============================================================================
// FtpTextReader Read Text lines from a file (or socket)
//===============================================================================
void FtpTextReader::textClear()
{ m_iTextLine = m_iTextBuff = 0;
m_szText[0] = 0;
m_bTextEOF = m_bTextTruncated = false;
}
int FtpTextReader::textRead(FtpSocket *pSock)
{
// if we have still buffered data then move it to the left
char* pEOL;
if(m_iTextLine < m_iTextBuff)
{ m_iTextBuff -= m_iTextLine;
memmove(m_szText, m_szText+m_iTextLine, m_iTextBuff);
pEOL = (char*)memchr(m_szText, '\n', m_iTextBuff); // have a complete line?
}
else
{ m_iTextBuff = 0;
pEOL = NULL;
}
m_bTextEOF = m_bTextTruncated = false;
// read data from the control socket until a complete line is read
int nBytes;
while(pEOL == NULL)
{
if(m_iTextBuff > textReadLimit)
{ m_bTextTruncated = true;
m_iTextBuff = textReadLimit;
}
nBytes = pSock->read(m_szText+m_iTextBuff, sizeof(m_szText)-m_iTextBuff);
if(nBytes <= 0)
{
// This error can occur after the server closed the connection (after a timeout)
if(nBytes < 0)
pSock->debugMessage("textRead failed");
m_bTextEOF = true;
pEOL = m_szText + m_iTextBuff;
}
else
{
m_iTextBuff += nBytes;
pEOL = (char*)memchr(m_szText, '\n', m_iTextBuff);
}
}
nBytes = pEOL - m_szText;
m_iTextLine = nBytes + 1;
if(nBytes > textReadLimit)
{ m_bTextTruncated = true;
nBytes = textReadLimit;
}
if(nBytes && m_szText[nBytes-1] == '\r')
nBytes--;
m_szText[nBytes] = 0;
return nBytes;
}
//===============================================================================
// FtpSocket Helper Class for Data or Control Connections
//===============================================================================
void FtpSocket::debugMessage(const char* pszMsg) const
{
kdDebug(7102) << m_pszName << ": " << pszMsg << endl;
}
int FtpSocket::errorMessage(int iErrorCode, const char* pszMsg) const
{
kdError(7102) << m_pszName << ": " << pszMsg << endl;
return iErrorCode;
}
int FtpSocket::connectSocket(int iTimeOutSec, bool bControl)
{
closeSocket();
int iOpt = bControl ? KExtendedSocket::inetSocket
: KExtendedSocket::noResolve;
setSocketFlags(iOpt | socketFlags());
setTimeout(iTimeOutSec);
int iCon = KExtendedSocket::connect();
if(iCon < 0)
{ int iErrorCode = (status() == IO_LookupError)
? ERR_UNKNOWN_HOST : ERR_COULD_NOT_CONNECT;
TQString strMsg = KExtendedSocket::strError(status(), systemError());
strMsg.prepend("connect failed (code %1): ");
return errorMessage(iErrorCode, strMsg.arg(iCon).latin1());
}
if( !setAddressReusable(true) )
return errorMessage(ERR_COULD_NOT_CREATE_SOCKET, "setAddressReusable failed");
if(!bControl)
{ int on=1;
if( !setSocketOption(SO_KEEPALIVE, (char *)&on, sizeof(on)) )
errorMessage(0, "Keepalive not allowed");
struct linger lng = { 1, 120 };
if( !setSocketOption(SO_LINGER, (char *)&lng, sizeof (lng)) )
errorMessage(0, "Linger mode was not allowed.");
}
debugMessage("connected");
return 0;
}
void FtpSocket::closeSocket()
{
if(m_server != -1 || fd() != -1)
debugMessage("disconnected");
if(m_server != -1)
{
::shutdown(m_server, SHUT_RDWR);
::close(m_server);
m_server = -1;
}
if(socketStatus() > nothing)
reset();
textClear();
}
bool FtpSocket::setSocketOption(int opt, char*arg, socklen_t len) const
{
return (setsockopt(sock(), SOL_SOCKET, opt, arg, len) != -1);
}
bool FtpSocket::sslConnect()
{
int connSucc = m_ssl.connect(sock());
if (connSucc != 1) return false;
m_sslUsed = true;
return true;
}
//===============================================================================
// Ftp
//===============================================================================
Ftp::Ftp( const TQCString &prot, const TQCString &pool, const TQCString &app )
: SlaveBase( prot, pool, app )
{
// init the socket data
m_data = m_control = NULL;
ftpCloseControlConnection();
// set the protocol
m_prot = prot;
// init other members
m_port = 0;
kdDebug(7102) << "Ftp::Ftp()" << endl;
}
Ftp::~Ftp()
{
kdDebug(7102) << "Ftp::~Ftp()" << endl;
closeConnection();
}
/**
* This closes a data connection opened by ftpOpenDataConnection().
*/
void Ftp::ftpCloseDataConnection()
{
if(m_data != NULL)
{ delete m_data;
m_data = NULL;
}
}
/**
* This closes a control connection opened by ftpOpenControlConnection() and reinits the
* related states. This method gets called from the constructor with m_control = NULL.
*/
void Ftp::ftpCloseControlConnection()
{
m_extControl = 0;
if(m_control)
delete m_control;
m_control = NULL;
m_cDataMode = 0;
m_bLoggedOn = false; // logon needs control connction
m_bTextMode = false;
m_bBusy = false;
}
/**
* Returns the last response from the server (iOffset >= 0) -or- reads a new response
* (iOffset < 0). The result is returned (with iOffset chars skipped for iOffset > 0).
*/
const char* Ftp::ftpResponse(int iOffset)
{
assert(m_control != NULL); // must have control connection socket
const char *pTxt = m_control->textLine();
// read the next line ...
if(iOffset < 0)
{
int iMore = 0;
m_iRespCode = 0;
// If the server sends multiline responses "nnn-text" we loop here until
// a final "nnn text" line is reached. Only data from the final line will
// be stored. Some servers (OpenBSD) send a single "nnn-" followed by
// optional lines that start with a space and a final "nnn text" line.
do {
int nBytes = m_control->textRead();
int iCode = atoi(pTxt);
if(iCode > 0) m_iRespCode = iCode;
// ignore lines starting with a space in multiline response
if(iMore != 0 && pTxt[0] == 32)
;
// otherwise the line should start with "nnn-" or "nnn "
else if(nBytes < 4 || iCode < 100)
iMore = 0;
// we got a valid line, now check for multiline responses ...
else if(iMore == 0 && pTxt[3] == '-')
iMore = iCode;
// "nnn " ends multiline mode ...
else if(iMore != 0 && (iMore != iCode || pTxt[3] != '-'))
iMore = 0;
if(iMore != 0)
kdDebug(7102) << " > " << pTxt << endl;
} while(iMore != 0);
kdDebug(7102) << "resp> " << pTxt << endl;
m_iRespType = (m_iRespCode > 0) ? m_iRespCode / 100 : 0;
}
// return text with offset ...
while(iOffset-- > 0 && pTxt[0])
pTxt++;
return pTxt;
}
void Ftp::closeConnection()
{
if(m_control != NULL || m_data != NULL)
kdDebug(7102) << "Ftp::closeConnection m_bLoggedOn=" << m_bLoggedOn << " m_bBusy=" << m_bBusy << endl;
if(m_bBusy) // ftpCloseCommand not called
{
kdWarning(7102) << "Ftp::closeConnection Abandoned data stream" << endl;
ftpCloseDataConnection();
}
if(m_bLoggedOn) // send quit
{
if( !ftpSendCmd( "quit", 0 ) || (m_iRespType != 2) )
kdWarning(7102) << "Ftp::closeConnection QUIT returned error: " << m_iRespCode << endl;
}
// close the data and control connections ...
ftpCloseDataConnection();
ftpCloseControlConnection();
}
void Ftp::setHost( const TQString& _host, int _port, const TQString& _user,
const TQString& _pass )
{
kdDebug(7102) << "Ftp::setHost (" << getpid() << "): " << _host << endl;
m_proxyURL = metaData("UseProxy");
m_bUseProxy = (m_proxyURL.isValid() && m_proxyURL.protocol() == m_prot);
if ( m_host != _host || m_port != _port ||
m_user != _user || m_pass != _pass )
closeConnection();
m_host = _host;
m_port = _port;
m_user = _user;
m_pass = _pass;
}
void Ftp::openConnection()
{
ftpOpenConnection(loginExplicit);
}
bool Ftp::ftpOpenConnection (LoginMode loginMode)
{
// check for implicit login if we are already logged on ...
if(loginMode == loginImplicit && m_bLoggedOn)
{
assert(m_control != NULL); // must have control connection socket
return true;
}
kdDebug(7102) << "ftpOpenConnection " << m_host << ":" << m_port << " "
<< m_user << " [password hidden]" << endl;
infoMessage( i18n("Opening connection to host %1").arg(m_host) );
if ( m_host.isEmpty() )
{
error( ERR_UNKNOWN_HOST, TQString::null );
return false;
}
assert( !m_bLoggedOn );
m_initialPath = TQString::null;
m_currentPath = TQString::null;
TQString host = m_bUseProxy ? m_proxyURL.host() : m_host;
unsigned short int port = m_bUseProxy ? m_proxyURL.port() : m_port;
if (!ftpOpenControlConnection(host, port) )
return false; // error emitted by ftpOpenControlConnection
infoMessage( i18n("Connected to host %1").arg(m_host) );
if(loginMode != loginDefered)
{
m_bLoggedOn = ftpLogin();
if( !m_bLoggedOn )
return false; // error emitted by ftpLogin
}
m_bTextMode = config()->readBoolEntry("textmode", false);
connected();
return true;
}
/**
* Called by @ref openConnection. It opens the control connection to the ftp server.
*
* @return true on success.
*/
bool Ftp::ftpOpenControlConnection( const TQString &host, unsigned short int port )
{
if ( port == 0 ) {
struct servent *pse;
if ( ( pse = getservbyname( "ftp", "tcp" ) ) == NULL )
port = 21;
else
port = ntohs(pse->s_port);
}
// implicitly close, then try to open a new connection ...
closeConnection();
int iErrorCode = ERR_OUT_OF_MEMORY;
TQString sErrorMsg;
m_control = new FtpSocket("CNTL");
if(m_control != NULL)
{
// now connect to the server and read the login message ...
m_control->setAddress(host, port);
iErrorCode = m_control->connectSocket(connectTimeout(), true);
sErrorMsg = host;
// on connect success try to read the server message...
if(iErrorCode == 0)
{
const char* psz = ftpResponse(-1);
if(m_iRespType != 2)
{ // login not successful, do we have an message text?
if(psz[0])
sErrorMsg = i18n("%1.\n\nReason: %2").arg(host).arg(psz);
iErrorCode = ERR_COULD_NOT_CONNECT;
}
}
if( (iErrorCode == 0) && (m_prot == "ftps") )
{
// ask whether ftps encryption is supported...
bool authSucc = ( ftpSendCmd("AUTH TLS") && (m_iRespCode == 234) );
if (!authSucc)
{
sErrorMsg = i18n("The FTP-server does not seem to support ftps-encryption.");
iErrorCode = ERR_SLAVE_DEFINED;
}
}
if( (iErrorCode == 0) && (m_prot == "ftps") )
{
iErrorCode = sslConnect(m_control);
sErrorMsg = i18n("SSL-connecting error occured");
}
}
// if there was a problem - report it ...
if(iErrorCode == 0) // OK, return success
return true;
closeConnection(); // clean-up on error
error(iErrorCode, sErrorMsg);
return false;
}
/**
* SSL connect
*/
int Ftp::sslConnect(FtpSocket* socket)
{
bool sslSucc = socket->sslConnect();
if (!sslSucc) return ERR_SLAVE_DEFINED;
else return 0;
}
/**
* Called by @ref openConnection. It logs us in.
* @ref m_initialPath is set to the current working directory
* if logging on was successful.
*
* @return true on success.
*/
bool Ftp::ftpLogin()
{
infoMessage( i18n("Sending login information") );
assert( !m_bLoggedOn );
TQString user = m_user;
TQString pass = m_pass;
if ( config()->readBoolEntry("EnableAutoLogin") )
{
TQString au = config()->readEntry("autoLoginUser");
if ( !au.isEmpty() )
{
user = au;
pass = config()->readEntry("autoLoginPass");
}
}
// Try anonymous login if both username/password
// information is blank.
if (user.isEmpty() && pass.isEmpty())
{
user = FTP_LOGIN;
pass = FTP_PASSWD;
}
AuthInfo info;
info.url.setProtocol( m_prot );
info.url.setHost( m_host );
info.url.setPort( m_port );
info.url.setUser( user );
TQCString tempbuf;
int failedAuth = 0;
do
{
// Check the cache and/or prompt user for password if 1st
// login attempt failed OR the user supplied a login name,
// but no password.
if ( failedAuth > 0 || (!user.isEmpty() && pass.isEmpty()) )
{
TQString errorMsg;
kdDebug(7102) << "Prompting user for login info..." << endl;
// Ask user if we should retry after when login fails!
if( failedAuth > 0 )
{
errorMsg = i18n("Message sent:\nLogin using username=%1 and "
"password=[hidden]\n\nServer replied:\n%2\n\n"
).arg(user).arg(ftpResponse(0));
}
if ( user != FTP_LOGIN )
info.username = user;
info.prompt = i18n("You need to supply a username and a password "
"to access this site.");
info.commentLabel = i18n( "Site:" );
info.comment = i18n("<b>%1</b>").arg( m_host );
info.keepPassword = true; // Prompt the user for persistence as well.
info.readOnly = (!m_user.isEmpty() && m_user != FTP_LOGIN);
bool disablePassDlg = config()->readBoolEntry( "DisablePassDlg", false );
if ( disablePassDlg || !openPassDlg( info, errorMsg ) )
{
error( ERR_USER_CANCELED, m_host );
return false;
}
else
{
user = info.username;
pass = info.password;
}
}
tempbuf = "user ";
tempbuf += user.latin1();
if ( m_bUseProxy )
{
tempbuf += '@';
tempbuf += m_host.latin1();
if ( m_port > 0 && m_port != DEFAULT_FTP_PORT )
{
tempbuf += ':';
tempbuf += TQString::number(m_port).latin1();
}
}
kdDebug(7102) << "Sending Login name: " << tempbuf << endl;
bool loggedIn = ( ftpSendCmd(tempbuf) && (m_iRespCode == 230) );
bool needPass = (m_iRespCode == 331);
// Prompt user for login info if we do not
// get back a "230" or "331".
if ( !loggedIn && !needPass )
{
kdDebug(7102) << "Login failed: " << ftpResponse(0) << endl;
++failedAuth;
continue; // Well we failed, prompt the user please!!
}
if( needPass )
{
tempbuf = "pass ";
tempbuf += pass.latin1();
kdDebug(7102) << "Sending Login password: " << "[protected]" << endl;
loggedIn = ( ftpSendCmd(tempbuf) && (m_iRespCode == 230) );
}
if ( loggedIn )
{
// Do not cache the default login!!
if( user != FTP_LOGIN && pass != FTP_PASSWD )
cacheAuthentication( info );
failedAuth = -1;
}
} while( ++failedAuth );
kdDebug(7102) << "Login OK" << endl;
infoMessage( i18n("Login OK") );
// Okay, we're logged in. If this is IIS 4, switch dir listing style to Unix:
// Thanks to [email protected] (Jens Kristian Sgaard) for this hint
if( ftpSendCmd("syst") && (m_iRespType == 2) )
{
if( !strncmp( ftpResponse(0), "215 Windows_NT", 14 ) ) // should do for any version
{
ftpSendCmd( "site dirstyle" );
// Check if it was already in Unix style
// Patch from Keith Refson <[email protected]>
if( !strncmp( ftpResponse(0), "200 MSDOS-like directory output is on", 37 ))
//It was in Unix style already!
ftpSendCmd( "site dirstyle" );
// windows won't support chmod before KDE konquers their desktop...
m_extControl |= chmodUnknown;
}
}
else
kdWarning(7102) << "syst failed" << endl;
if ( config()->readBoolEntry ("EnableAutoLoginMacro") )
ftpAutoLoginMacro ();
// Get the current working directory
kdDebug(7102) << "Searching for pwd" << endl;
if( !ftpSendCmd("pwd") || (m_iRespType != 2) )
{
kdDebug(7102) << "Couldn't issue pwd command" << endl;
error( ERR_COULD_NOT_LOGIN, i18n("Could not login to %1.").arg(m_host) ); // or anything better ?
return false;
}
TQString sTmp = remoteEncoding()->decode( ftpResponse(3) );
int iBeg = sTmp.find('"');
int iEnd = sTmp.findRev('"');
if(iBeg > 0 && iBeg < iEnd)
{
m_initialPath = sTmp.mid(iBeg+1, iEnd-iBeg-1);
if(m_initialPath[0] != '/') m_initialPath.prepend('/');
kdDebug(7102) << "Initial path set to: " << m_initialPath << endl;
m_currentPath = m_initialPath;
}
return true;
}
void Ftp::ftpAutoLoginMacro ()
{
TQString macro = metaData( "autoLoginMacro" );
if ( macro.isEmpty() )
return;
TQStringList list = TQStringList::split('\n', macro);
for(TQStringList::Iterator it = list.begin() ; it != list.end() ; ++it )
{
if ( (*it).startsWith("init") )
{
list = TQStringList::split( '\\', macro);
it = list.begin();
++it; // ignore the macro name
for( ; it != list.end() ; ++it )
{
// TODO: Add support for arbitrary commands
// besides simply changing directory!!
if ( (*it).startsWith( "cwd" ) )
ftpFolder( (*it).mid(4).stripWhiteSpace(), false );
}
break;
}
}
}
/**
* ftpSendCmd - send a command (@p cmd) and read response
*
* @param maxretries number of time it should retry. Since it recursively
* calls itself if it can't read the answer (this happens especially after
* timeouts), we need to limit the recursiveness ;-)
*
* return true if any response received, false on error
*/
bool Ftp::ftpSendCmd( const TQCString& cmd, int maxretries )
{
assert(m_control != NULL); // must have control connection socket
if ( cmd.find( '\r' ) != -1 || cmd.find( '\n' ) != -1)
{
kdWarning(7102) << "Invalid command received (contains CR or LF):"
<< cmd.data() << endl;
error( ERR_UNSUPPORTED_ACTION, m_host );
return false;
}
// Don't print out the password...
bool isPassCmd = (cmd.left(4).lower() == "pass");
if ( !isPassCmd )
kdDebug(7102) << "send> " << cmd.data() << endl;
else
kdDebug(7102) << "send> pass [protected]" << endl;
// Send the message...
TQCString buf = cmd;
buf += "\r\n"; // Yes, must use CR/LF - see http://cr.yp.to/ftp/request.html
int num = m_control->write(buf.data(), buf.length());
// If we were able to successfully send the command, then we will
// attempt to read the response. Otherwise, take action to re-attempt
// the login based on the maximum number of retires specified...
if( num > 0 )
ftpResponse(-1);
else
{ m_iRespType = m_iRespCode = 0;
m_control->textClear();
}
// If respCh is NULL or the response is 421 (Timed-out), we try to re-send
// the command based on the value of maxretries.
if( (m_iRespType <= 0) || (m_iRespCode == 421) )
{
// We have not yet logged on...
if (!m_bLoggedOn)
{
// The command was sent from the ftpLogin function, i.e. we are actually
// attempting to login in. NOTE: If we already sent the username, we
// return false and let the user decide whether (s)he wants to start from
// the beginning...
if (maxretries > 0 && !isPassCmd)
{
closeConnection ();
if( ftpOpenConnection(loginDefered) )
ftpSendCmd ( cmd, maxretries - 1 );
}
return false;
}
else
{
if ( maxretries < 1 )
return false;
else
{
kdDebug(7102) << "Was not able to communicate with " << m_host << endl
<< "Attempting to re-establish connection." << endl;
closeConnection(); // Close the old connection...
openConnection(); // Attempt to re-establish a new connection...
if (!m_bLoggedOn)
{
if (m_control != NULL) // if openConnection succeeded ...
{
kdDebug(7102) << "Login failure, aborting" << endl;
error (ERR_COULD_NOT_LOGIN, m_host);
closeConnection ();
}
return false;
}
kdDebug(7102) << "Logged back in, re-issuing command" << endl;
// If we were able to login, resend the command...
if (maxretries)
maxretries--;
return ftpSendCmd( cmd, maxretries );
}
}
}
return true;
}
/*
* ftpOpenPASVDataConnection - set up data connection, using PASV mode
*
* return 1 if successful, 0 otherwise
* doesn't set error message, since non-pasv mode will always be tried if
* this one fails
*/
int Ftp::ftpOpenPASVDataConnection()
{
assert(m_control != NULL); // must have control connection socket
assert(m_data == NULL); // ... but no data connection
// Check that we can do PASV
const TDESocketAddress *sa = m_control->peerAddress();
if (sa != NULL && sa->family() != PF_INET)
return ERR_INTERNAL; // no PASV for non-PF_INET connections
if (m_extControl & pasvUnknown)
return ERR_INTERNAL; // already tried and got "unknown command"
m_bPasv = true;
/* Let's PASsiVe*/
if( !ftpSendCmd("PASV") || (m_iRespType != 2) )
{
kdDebug(7102) << "PASV attempt failed" << endl;
// unknown command?
if( m_iRespType == 5 )
{
kdDebug(7102) << "disabling use of PASV" << endl;
m_extControl |= pasvUnknown;
}
return ERR_INTERNAL;
}
// The usual answer is '227 Entering Passive Mode. (160,39,200,55,6,245)'
// but anonftpd gives '227 =160,39,200,55,6,245'
int i[6];
// fix type conversion error josswern 23.02.2011
const char *start; // statt char *start 23.02.11
start = strchr(ftpResponse(3), '(');
if ( !start )
start = strchr(ftpResponse(3), '=');
if ( !start ||
( sscanf(start, "(%d,%d,%d,%d,%d,%d)",&i[0], &i[1], &i[2], &i[3], &i[4], &i[5]) != 6 &&
sscanf(start, "=%d,%d,%d,%d,%d,%d", &i[0], &i[1], &i[2], &i[3], &i[4], &i[5]) != 6 ) )
{
kdError(7102) << "parsing IP and port numbers failed. String parsed: " << start << endl;
return ERR_INTERNAL;
}
// Make hostname and port number ...
TQString host;
host.sprintf("%d.%d.%d.%d", i[0], i[1], i[2], i[3]);
int port = i[4] << 8 | i[5];
// now connect the data socket ...
m_data = new FtpSocket("PASV");
m_data->setAddress(host, port);
kdDebug(7102) << "Connecting to " << host << " on port " << port << endl;
return m_data->connectSocket(connectTimeout(), false);
}
/*
* ftpOpenEPSVDataConnection - opens a data connection via EPSV
*/
int Ftp::ftpOpenEPSVDataConnection()
{
assert(m_control != NULL); // must have control connection socket
assert(m_data == NULL); // ... but no data connection
const TDESocketAddress *sa = m_control->peerAddress();
int portnum;
// we are sure sa is a KInetSocketAddress, because we asked for KExtendedSocket::inetSocket
// when we connected
const KInetSocketAddress *sin = static_cast<const KInetSocketAddress*>(sa);
if (m_extControl & epsvUnknown || sa == NULL)
return ERR_INTERNAL;
m_bPasv = true;
if( !ftpSendCmd("EPSV") || (m_iRespType != 2) )
{
// unknown command?
if( m_iRespType == 5 )
{
kdDebug(7102) << "disabling use of EPSV" << endl;
m_extControl |= epsvUnknown;
}
return ERR_INTERNAL;
}
// fix type conversion error josswern 23.02.2011
const char *start = strchr(ftpResponse(3), '|'); // statt char *start = strchr(ftpResponse(3), '|'); josswern
if ( !start || sscanf(start, "|||%d|", &portnum) != 1)
return ERR_INTERNAL;
m_data = new FtpSocket("EPSV");
m_data->setAddress(sin->nodeName(), portnum);
return m_data->connectSocket(connectTimeout(), false) != 0;
}
/*
* ftpOpenEPRTDataConnection
* @return 0 on success, ERR_INTERNAL if mode not acceptable -or- a fatal error code
*/
int Ftp::ftpOpenEPRTDataConnection()
{
assert(m_control != NULL); // must have control connection socket
assert(m_data == NULL); // ... but no data connection
// yes, we are sure this is a KInetSocketAddress
const KInetSocketAddress *sin = static_cast<const KInetSocketAddress*>(m_control->localAddress());
m_bPasv = false;
if (m_extControl & eprtUnknown || sin == NULL)
return ERR_INTERNAL;
m_data = new FtpSocket("EPRT");
m_data->setHost(sin->nodeName());
m_data->setPort(0); // setting port to 0 will make us bind to a random, free port
m_data->setSocketFlags(KExtendedSocket::noResolve | KExtendedSocket::passiveSocket |
KExtendedSocket::inetSocket);
if (m_data->listen(1) < 0)
return ERR_COULD_NOT_LISTEN;
sin = static_cast<const KInetSocketAddress*>(m_data->localAddress());
if (sin == NULL)
return ERR_INTERNAL;
// TQString command = TQString::fromLatin1("eprt |%1|%2|%3|").arg(sin->ianaFamily())
// .arg(sin->nodeName())
// .arg(sin->port());
TQCString command;
command.sprintf("eprt |%d|%s|%d|", sin->ianaFamily(),
sin->nodeName().latin1(), sin->port());
// FIXME! Encoding for hostnames?
if( ftpSendCmd(command) && (m_iRespType == 2) )
return 0;
// unknown command?
if( m_iRespType == 5 )
{
kdDebug(7102) << "disabling use of EPRT" << endl;
m_extControl |= eprtUnknown;
}
return ERR_INTERNAL;
}
/*
* ftpOpenDataConnection - set up data connection
*
* The routine calls several ftpOpenXxxxConnection() helpers to find
* the best connection mode. If a helper cannot connect if returns
* ERR_INTERNAL - so this is not really an error! All other error
* codes are treated as fatal, e.g. they are passed back to the caller
* who is responsible for calling error(). ftpOpenPortDataConnection
* can be called as last try and it does never return ERR_INTERNAL.
*
* @return 0 if successful, err code otherwise
*/
int Ftp::ftpOpenDataConnection()
{
// make sure that we are logged on and have no data connection...
assert( m_bLoggedOn );
ftpCloseDataConnection();
int iErrCode = 0;
int iErrCodePASV = 0; // Remember error code from PASV
// First try passive (EPSV & PASV) modes
if( !config()->readBoolEntry("DisablePassiveMode", false) )
{
iErrCode = ftpOpenPASVDataConnection();
if(iErrCode == 0)
return 0; // success
iErrCodePASV = iErrCode;
ftpCloseDataConnection();
if( !config()->readBoolEntry("DisableEPSV", false) )
{
iErrCode = ftpOpenEPSVDataConnection();
if(iErrCode == 0)
return 0; // success
ftpCloseDataConnection();
}
// if we sent EPSV ALL already and it was accepted, then we can't
// use active connections any more
if (m_extControl & epsvAllSent)
return iErrCodePASV ? iErrCodePASV : iErrCode;
}
if( !config()->readBoolEntry("DisableEPRT", false) )
{
iErrCode = ftpOpenEPRTDataConnection();
if(iErrCode == 0)
return 0; // success
ftpCloseDataConnection();
}
// fall back to port mode
iErrCode = ftpOpenPortDataConnection();
if(iErrCode == 0)
return 0; // success
ftpCloseDataConnection();
// prefer to return the error code from PASV if any, since that's what should have worked in the first place
return iErrCodePASV ? iErrCodePASV : iErrCode;
}
/*
* ftpOpenPortDataConnection - set up data connection
*
* @return 0 if successfull, err code otherwise (but never ERR_INTERNAL
* because this is the last connection mode that is tried)
*/
int Ftp::ftpOpenPortDataConnection()
{
assert(m_control != NULL); // must have control connection socket
assert(m_data == NULL); // ... but no data connection
m_bPasv = false;
// create a socket, bind it and let it listen ...
m_data = new FtpSocket("PORT");
m_data->setSocketFlags(KExtendedSocket::noResolve | KExtendedSocket::passiveSocket |
KExtendedSocket::inetSocket);
// yes, we are sure this is a KInetSocketAddress
const KInetSocketAddress* pAddr = static_cast<const KInetSocketAddress*>(m_control->localAddress());
m_data->setAddress(pAddr->nodeName(), "0");
m_data->setAddressReusable(true);
if(m_data->listen(1) < 0)
return ERR_COULD_NOT_LISTEN;
struct linger lng = { 0, 0 };
if ( !m_data->setSocketOption(SO_LINGER, (char*)&lng, sizeof(lng)) )
return ERR_COULD_NOT_CREATE_SOCKET;
// send the PORT command ...
pAddr = static_cast<const KInetSocketAddress*>(m_data->localAddress());
struct sockaddr* psa = (struct sockaddr*)pAddr->addressV4();
unsigned char* pData = (unsigned char*)(psa->sa_data);
TQCString portCmd;
portCmd.sprintf("port %d,%d,%d,%d,%d,%d",
pData[2], pData[3], pData[4], pData[5], pData[0], pData[1]);
if( ftpSendCmd(portCmd) && (m_iRespType == 2) )
return 0;
return ERR_COULD_NOT_CONNECT;
}
/*
* ftpAcceptConnect - wait for incoming connection
* Used by @ref ftpOpenCommand
*
* return false on error or timeout
*/
int Ftp::ftpAcceptConnect()
{
assert(m_data != NULL);
if ( m_bPasv )
{
m_data->setServer(-1);
return true;
}
int sSock = m_data->fd();
struct sockaddr addr;
for(;;)
{
fd_set mask;
FD_ZERO(&mask);
FD_SET(sSock,&mask);
int r = KSocks::self()->select(sSock + 1, &mask, NULL, NULL, 0L);
if( r < 0 && errno != EINTR && errno != EAGAIN )
continue;
if( r > 0 )
break;
}
ksocklen_t l = sizeof(addr);
m_data->setServer( KSocks::self()->accept(sSock, &addr, &l) );
return (m_data->server() != -1);
}
bool Ftp::ftpOpenCommand( const char *_command, const TQString & _path, char _mode,
int errorcode, TDEIO::fileoffset_t _offset )
{
bool encSucc = false;
if (m_prot == "ftps")
{
// Check if data-encryption is supported. If the server doesn't know PBSZ 0 the whole
// thing is skipped, otherwise we check for a successful answer to PROT P. if there is
// none, we issue the PROT C command.
encSucc = ( ftpSendCmd("PBSZ 0") && (m_iRespCode == 200) );
if (encSucc)
{
encSucc = ( ftpSendCmd("PROT P") && (m_iRespCode == 200) );
if (!encSucc)
{
ftpSendCmd("PBSZ 0");
ftpSendCmd("PROT C");
}
}
}
int errCode = 0;
if( !ftpDataMode(_mode) )
errCode = ERR_COULD_NOT_CONNECT;
else
errCode = ftpOpenDataConnection();
if(errCode != 0)
{
error(errCode, m_host);
return false;
}
if ( _offset > 0 ) {
// send rest command if offset > 0, this applies to retr and stor commands
char buf[100];
sprintf(buf, "rest %lld", _offset);
if ( !ftpSendCmd( buf ) )
return false;
if( m_iRespType != 3 )
{
error( ERR_CANNOT_RESUME, _path ); // should never happen
return false;
}
}
TQCString tmp = _command;
TQString errormessage;
if ( !_path.isEmpty() ) {
tmp += " ";
tmp += remoteEncoding()->encode(_path);
}
if( !ftpSendCmd( tmp ) || (m_iRespType != 1) )
{
if( _offset > 0 && strcmp(_command, "retr") == 0 && (m_iRespType == 4) )
errorcode = ERR_CANNOT_RESUME;
// The error here depends on the command
errormessage = _path;
}
else
{
// Only now we know for sure that we can resume
if ( _offset > 0 && strcmp(_command, "retr") == 0 )
canResume();
if( ftpAcceptConnect() )
{
m_bBusy = true; // cleared in ftpCloseCommand
if ((encSucc) && (m_prot == "ftps") )
{
errorcode = sslConnect(m_data);
if (errorcode == 0) return true;
errormessage = i18n("SSL connection failed on data channel.");
}
else return true;
}
errorcode = ERR_COULD_NOT_ACCEPT;
}
error(errorcode, errormessage);
return false;
}
bool Ftp::ftpCloseCommand()
{
// first close data sockets (if opened), then read response that
// we got for whatever was used in ftpOpenCommand ( should be 226 )
if(m_data)
{
delete m_data;
m_data = NULL;
}
if(!m_bBusy)
return true;
kdDebug(7102) << "ftpCloseCommand: reading command result" << endl;
m_bBusy = false;
if(!ftpResponse(-1) || (m_iRespType != 2) )
{
kdDebug(7102) << "ftpCloseCommand: no transfer complete message" << endl;
return false;
}
return true;
}
void Ftp::mkdir( const KURL & url, int permissions )
{
if( !ftpOpenConnection(loginImplicit) )
return;
TQString path = remoteEncoding()->encode(url);
TQCString buf = "mkd ";
buf += remoteEncoding()->encode(path);
if( !ftpSendCmd( buf ) || (m_iRespType != 2) )
{
TQString currentPath( m_currentPath );
// Check whether or not mkdir failed because
// the directory already exists...
if( ftpFolder( path, false ) )
{
error( ERR_DIR_ALREADY_EXIST, path );
// Change the directory back to what it was...
(void) ftpFolder( currentPath, false );
return;
}
error( ERR_COULD_NOT_MKDIR, path );
return;
}
if ( permissions != -1 )
{
// chmod the dir we just created, ignoring errors.
(void) ftpChmod( path, permissions );
}
finished();
}
void Ftp::rename( const KURL& src, const KURL& dst, bool overwrite )
{
if( !ftpOpenConnection(loginImplicit) )
return;
// The actual functionality is in ftpRename because put needs it
if ( ftpRename( src.path(), dst.path(), overwrite ) )
finished();
else
error( ERR_CANNOT_RENAME, src.path() );
}
bool Ftp::ftpRename( const TQString & src, const TQString & dst, bool /* overwrite */ )
{
// TODO honor overwrite
assert( m_bLoggedOn );
int pos = src.findRev("/");
if( !ftpFolder(src.left(pos+1), false) )
return false;
TQCString from_cmd = "RNFR ";
from_cmd += remoteEncoding()->encode(src.mid(pos+1));
if( !ftpSendCmd( from_cmd ) || (m_iRespType != 3) )
return false;
TQCString to_cmd = "RNTO ";
to_cmd += remoteEncoding()->encode(dst);
if( !ftpSendCmd( to_cmd ) || (m_iRespType != 2) )
return false;
return true;
}
void Ftp::del( const KURL& url, bool isfile )
{
if( !ftpOpenConnection(loginImplicit) )
return;
// When deleting a directory, we must exit from it first
// The last command probably went into it (to stat it)
if ( !isfile )
ftpFolder(remoteEncoding()->directory(url), false); // ignore errors
TQCString cmd = isfile ? "DELE " : "RMD ";
cmd += remoteEncoding()->encode(url);
if( !ftpSendCmd( cmd ) || (m_iRespType != 2) )
error( ERR_CANNOT_DELETE, url.path() );
else
finished();
}
bool Ftp::ftpChmod( const TQString & path, int permissions )
{
assert( m_bLoggedOn );
if(m_extControl & chmodUnknown) // previous errors?
return false;
// we need to do bit AND 777 to get permissions, in case
// we were sent a full mode (unlikely)
TQCString cmd;
cmd.sprintf("SITE CHMOD %o ", permissions & 511 );
cmd += remoteEncoding()->encode(path);
ftpSendCmd(cmd);
if(m_iRespType == 2)
return true;
if(m_iRespCode == 500)
{
m_extControl |= chmodUnknown;
kdDebug(7102) << "ftpChmod: CHMOD not supported - disabling";
}
return false;
}
void Ftp::chmod( const KURL & url, int permissions )
{
if( !ftpOpenConnection(loginImplicit) )
return;
if ( !ftpChmod( url.path(), permissions ) )
error( ERR_CANNOT_CHMOD, url.path() );
else
finished();
}
void Ftp::ftpCreateUDSEntry( const TQString & filename, FtpEntry& ftpEnt, UDSEntry& entry, bool isDir )
{
assert(entry.count() == 0); // by contract :-)
UDSAtom atom;
atom.m_uds = UDS_NAME;
atom.m_str = filename;
entry.append( atom );
atom.m_uds = UDS_SIZE;
atom.m_long = ftpEnt.size;
entry.append( atom );
atom.m_uds = UDS_MODIFICATION_TIME;
atom.m_long = ftpEnt.date;
entry.append( atom );
atom.m_uds = UDS_ACCESS;
atom.m_long = ftpEnt.access;
entry.append( atom );
atom.m_uds = UDS_USER;
atom.m_str = ftpEnt.owner;
entry.append( atom );
if ( !ftpEnt.group.isEmpty() )
{
atom.m_uds = UDS_GROUP;
atom.m_str = ftpEnt.group;
entry.append( atom );
}
if ( !ftpEnt.link.isEmpty() )
{
atom.m_uds = UDS_LINK_DEST;
atom.m_str = ftpEnt.link;
entry.append( atom );
KMimeType::Ptr mime = KMimeType::findByURL( KURL(m_prot + "://host/" + filename ) );
// Links on ftp sites are often links to dirs, and we have no way to check
// that. Let's do like Netscape : assume dirs generally.
// But we do this only when the mimetype can't be known from the filename.
// --> we do better than Netscape :-)
if ( mime->name() == KMimeType::defaultMimeType() )
{
kdDebug(7102) << "Setting guessed mime type to inode/directory for " << filename << endl;
atom.m_uds = UDS_GUESSED_MIME_TYPE;
atom.m_str = "inode/directory";
entry.append( atom );
isDir = true;
}
}
atom.m_uds = UDS_FILE_TYPE;
atom.m_long = isDir ? S_IFDIR : ftpEnt.type;
entry.append( atom );
/* atom.m_uds = UDS_ACCESS_TIME;
atom.m_long = buff.st_atime;
entry.append( atom );
atom.m_uds = UDS_CREATION_TIME;
atom.m_long = buff.st_ctime;
entry.append( atom ); */
}
void Ftp::ftpShortStatAnswer( const TQString& filename, bool isDir )
{
UDSEntry entry;
UDSAtom atom;
atom.m_uds = TDEIO::UDS_NAME;
atom.m_str = filename;
entry.append( atom );
atom.m_uds = TDEIO::UDS_FILE_TYPE;
atom.m_long = isDir ? S_IFDIR : S_IFREG;
entry.append( atom );
atom.m_uds = TDEIO::UDS_ACCESS;
atom.m_long = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
entry.append( atom );
// No details about size, ownership, group, etc.
statEntry(entry);
finished();
}
void Ftp::ftpStatAnswerNotFound( const TQString & path, const TQString & filename )
{
// Only do the 'hack' below if we want to download an existing file (i.e. when looking at the "source")
// When e.g. uploading a file, we still need stat() to return "not found"
// when the file doesn't exist.
TQString statSide = metaData("statSide");
kdDebug(7102) << "Ftp::stat statSide=" << statSide << endl;
if ( statSide == "source" )
{
kdDebug(7102) << "Not found, but assuming found, because some servers don't allow listing" << endl;
// MS Server is incapable of handling "list <blah>" in a case insensitive way
// But "retr <blah>" works. So lie in stat(), to get going...
//
// There's also the case of ftp://ftp2.3ddownloads.com/90380/linuxgames/loki/patches/ut/ut-patch-436.run
// where listing permissions are denied, but downloading is still possible.
ftpShortStatAnswer( filename, false /*file, not dir*/ );
return;
}
error( ERR_DOES_NOT_EXIST, path );
}
void Ftp::stat( const KURL &url)
{
kdDebug(7102) << "Ftp::stat : path='" << url.path() << "'" << endl;
if( !ftpOpenConnection(loginImplicit) )
return;
TQString path = TQDir::cleanDirPath( url.path() );
kdDebug(7102) << "Ftp::stat : cleaned path='" << path << "'" << endl;
// We can't stat root, but we know it's a dir.
if( path.isEmpty() || path == "/" )
{
UDSEntry entry;
UDSAtom atom;
atom.m_uds = TDEIO::UDS_NAME;
atom.m_str = TQString::null;
entry.append( atom );
atom.m_uds = TDEIO::UDS_FILE_TYPE;
atom.m_long = S_IFDIR;
entry.append( atom );
atom.m_uds = TDEIO::UDS_ACCESS;
atom.m_long = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
entry.append( atom );
atom.m_uds = TDEIO::UDS_USER;
atom.m_str = "root";
entry.append( atom );
atom.m_uds = TDEIO::UDS_GROUP;
entry.append( atom );
// no size
statEntry( entry );
finished();
return;
}
KURL tempurl( url );
tempurl.setPath( path ); // take the clean one
TQString listarg; // = tempurl.directory(false /*keep trailing slash*/);
TQString parentDir;
TQString filename = tempurl.fileName();
Q_ASSERT(!filename.isEmpty());
TQString search = filename;
// Try cwd into it, if it works it's a dir (and then we'll list the parent directory to get more info)
// if it doesn't work, it's a file (and then we'll use dir filename)
bool isDir = ftpFolder(path, false);
// if we're only interested in "file or directory", we should stop here
TQString sDetails = metaData("details");
int details = sDetails.isEmpty() ? 2 : sDetails.toInt();
kdDebug(7102) << "Ftp::stat details=" << details << endl;
if ( details == 0 )
{
if ( !isDir && !ftpSize( path, 'I' ) ) // ok, not a dir -> is it a file ?
{ // no -> it doesn't exist at all
ftpStatAnswerNotFound( path, filename );
return;
}
ftpShortStatAnswer( filename, isDir ); // successfully found a dir or a file -> done
return;
}
if (!isDir)
{
// It is a file or it doesn't exist, try going to parent directory
parentDir = tempurl.directory(false /*keep trailing slash*/);
// With files we can do "LIST <filename>" to avoid listing the whole dir
listarg = filename;
}
else
{
// --- New implementation:
// Don't list the parent dir. Too slow, might not show it, etc.
// Just return that it's a dir.
UDSEntry entry;
UDSAtom atom;
atom.m_uds = TDEIO::UDS_NAME;
atom.m_str = filename;
entry.append( atom );
atom.m_uds = TDEIO::UDS_FILE_TYPE;
atom.m_long = S_IFDIR;
entry.append( atom );
atom.m_uds = TDEIO::UDS_ACCESS;
atom.m_long = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
entry.append( atom );
// No clue about size, ownership, group, etc.
statEntry(entry);
finished();
return;
// --- Old implementation:
#if 0
// It's a dir, remember that
// Reason: it could be a symlink to a dir, in which case ftpReadDir
// in the parent dir will have no idea about that. But we know better.
isDir = true;
// If the dir starts with '.', we'll need '-a' to see it in the listing.
if ( search[0] == '.' )
listarg = "-a";
parentDir = "..";
#endif
}
// Now cwd the parent dir, to prepare for listing
if( !ftpFolder(parentDir, true) )
return;
if( !ftpOpenCommand( "list", listarg, 'I', ERR_DOES_NOT_EXIST ) )
{
kdError(7102) << "COULD NOT LIST" << endl;
return;
}
kdDebug(7102) << "Starting of list was ok" << endl;
Q_ASSERT( !search.isEmpty() && search != "/" );
bool bFound = false;
KURL linkURL;
FtpEntry ftpEnt;
while( ftpReadDir(ftpEnt) )
{
// We look for search or filename, since some servers (e.g. ftp.tuwien.ac.at)
// return only the filename when doing "dir /full/path/to/file"
if ( !bFound )
{
if ( ( search == ftpEnt.name || filename == ftpEnt.name ) ) {
if ( !filename.isEmpty() ) {
bFound = true;
UDSEntry entry;
ftpCreateUDSEntry( filename, ftpEnt, entry, isDir );
statEntry( entry );
}
} else if ( isDir && ( ftpEnt.name == listarg || ftpEnt.name+'/' == listarg ) ) {
// Damn, the dir we're trying to list is in fact a symlink
// Follow it and try again
if ( ftpEnt.link.isEmpty() )
kdWarning(7102) << "Got " << listarg << " as answer, but empty link!" << endl;
else
{
linkURL = url;
kdDebug(7102) << "ftpEnt.link=" << ftpEnt.link << endl;
if ( ftpEnt.link[0] == '/' )
linkURL.setPath( ftpEnt.link ); // Absolute link
else
{
// Relative link (stat will take care of cleaning ../.. etc.)
linkURL.setPath( listarg ); // this is what we were listing (the link)
linkURL.setPath( linkURL.directory() ); // go up one dir
linkURL.addPath( ftpEnt.link ); // replace link by its destination
kdDebug(7102) << "linkURL now " << linkURL.prettyURL() << endl;
}
// Re-add the filename we're looking for
linkURL.addPath( filename );
}
bFound = true;
}
}
// kdDebug(7102) << ftpEnt.name << endl;
}
ftpCloseCommand(); // closes the data connection only
if ( !bFound )
{
ftpStatAnswerNotFound( path, filename );
return;
}
if ( !linkURL.isEmpty() )
{
if ( linkURL == url || linkURL == tempurl )
{
error( ERR_CYCLIC_LINK, linkURL.prettyURL() );
return;
}
stat( linkURL );
return;
}
kdDebug(7102) << "stat : finished successfully" << endl;
finished();
}
void Ftp::listDir( const KURL &url )
{
kdDebug(7102) << "Ftp::listDir " << url.prettyURL() << endl;
if( !ftpOpenConnection(loginImplicit) )
return;
// No path specified ?
TQString path = url.path();
if ( path.isEmpty() )
{
KURL realURL;
realURL.setProtocol( m_prot );
if ( m_user != FTP_LOGIN )
realURL.setUser( m_user );
// We set the password, so that we don't ask for it if it was given
if ( m_pass != FTP_PASSWD )
realURL.setPass( m_pass );
realURL.setHost( m_host );
realURL.setPort( m_port );
if ( m_initialPath.isEmpty() )
m_initialPath = "/";
realURL.setPath( m_initialPath );
kdDebug(7102) << "REDIRECTION to " << realURL.prettyURL() << endl;
redirection( realURL );
finished();
return;
}
kdDebug(7102) << "hunting for path '" << path << "'" << endl;
if (!ftpOpenDir( path ) )
{
if ( ftpSize( path, 'I' ) ) // is it a file ?
{
error( ERR_IS_FILE, path );
return;
}
// not sure which to emit
//error( ERR_DOES_NOT_EXIST, path );
error( ERR_CANNOT_ENTER_DIRECTORY, path );
return;
}
UDSEntry entry;
FtpEntry ftpEnt;
while( ftpReadDir(ftpEnt) )
{
//kdDebug(7102) << ftpEnt.name << endl;
//Q_ASSERT( !ftpEnt.name.isEmpty() );
if ( !ftpEnt.name.isEmpty() )
{
//if ( S_ISDIR( (mode_t)ftpEnt.type ) )
// kdDebug(7102) << "is a dir" << endl;
//if ( !ftpEnt.link.isEmpty() )
// kdDebug(7102) << "is a link to " << ftpEnt.link << endl;
entry.clear();
ftpCreateUDSEntry( ftpEnt.name, ftpEnt, entry, false );
listEntry( entry, false );
}
}
listEntry( entry, true ); // ready
ftpCloseCommand(); // closes the data connection only
finished();
}
void Ftp::slave_status()
{
kdDebug(7102) << "Got slave_status host = " << (m_host.ascii() ? m_host.ascii() : "[None]") << " [" << (m_bLoggedOn ? "Connected" : "Not connected") << "]" << endl;
slaveStatus( m_host, m_bLoggedOn );
}
bool Ftp::ftpOpenDir( const TQString & path )
{
//TQString path( _url.path(-1) );
// We try to change to this directory first to see whether it really is a directory.
// (And also to follow symlinks)
TQString tmp = path.isEmpty() ? TQString("/") : path;
// We get '550', whether it's a file or doesn't exist...
if( !ftpFolder(tmp, false) )
return false;
// Don't use the path in the list command:
// We changed into this directory anyway - so it's enough just to send "list".
// We use '-a' because the application MAY be interested in dot files.
// The only way to really know would be to have a metadata flag for this...
// Since some windows ftp server seems not to support the -a argument, we use a fallback here.
// In fact we have to use -la otherwise -a removes the default -l (e.g. ftp.trolltech.com)
if( !ftpOpenCommand( "list -la", TQString::null, 'I', ERR_CANNOT_ENTER_DIRECTORY ) )
{
if ( !ftpOpenCommand( "list", TQString::null, 'I', ERR_CANNOT_ENTER_DIRECTORY ) )
{
kdWarning(7102) << "Can't open for listing" << endl;
return false;
}
}
kdDebug(7102) << "Starting of list was ok" << endl;
return true;
}
bool Ftp::ftpReadDir(FtpEntry& de)
{
assert(m_data != NULL);
// get a line from the data connecetion ...
while( !m_data->textEOF() )
{
if(m_data->textRead() <= 0)
continue;
if(m_data->textTooLong())
kdWarning(7102) << "ftpReadDir line too long - truncated" << endl;
const char* buffer = m_data->textLine();
kdDebug(7102) << "dir > " << buffer << endl;
//Normally the listing looks like
// -rw-r--r-- 1 dfaure dfaure 102 Nov 9 12:30 log
// but on Netware servers like ftp://ci-1.ci.pwr.wroc.pl/ it looks like (#76442)
// d [RWCEAFMS] Admin 512 Oct 13 2004 PSI
// we should always get the following 5 fields ...
const char *p_access, *p_junk, *p_owner, *p_group, *p_size;
if( (p_access = strtok((char*)buffer," ")) == 0) continue;
if( (p_junk = strtok(NULL," ")) == 0) continue;
if( (p_owner = strtok(NULL," ")) == 0) continue;
if( (p_group = strtok(NULL," ")) == 0) continue;
if( (p_size = strtok(NULL," ")) == 0) continue;
//kdDebug(7102) << "p_access=" << p_access << " p_junk=" << p_junk << " p_owner=" << p_owner << " p_group=" << p_group << " p_size=" << p_size << endl;
de.access = 0;
if ( strlen( p_access ) == 1 && p_junk[0] == '[' ) { // Netware
de.access = S_IRWXU | S_IRWXG | S_IRWXO; // unknown -> give all permissions
}
const char *p_date_1, *p_date_2, *p_date_3, *p_name;
// A special hack for "/dev". A listing may look like this:
// crw-rw-rw- 1 root root 1, 5 Jun 29 1997 zero
// So we just ignore the number in front of the ",". Ok, its a hack :-)
if ( strchr( p_size, ',' ) != 0L )
{
//kdDebug(7102) << "Size contains a ',' -> reading size again (/dev hack)" << endl;
if ((p_size = strtok(NULL," ")) == 0)
continue;
}
// Check whether the size we just read was really the size
// or a month (this happens when the server lists no group)
// Used to be the case on sunsite.uio.no, but not anymore
// This is needed for the Netware case, too.
if ( !isdigit( *p_size ) )
{
p_date_1 = p_size;
p_size = p_group;
p_group = 0;
//kdDebug(7102) << "Size didn't have a digit -> size=" << p_size << " date_1=" << p_date_1 << endl;
}
else
{
p_date_1 = strtok(NULL," ");
//kdDebug(7102) << "Size has a digit -> ok. p_date_1=" << p_date_1 << endl;
}
if ( p_date_1 != 0 &&
(p_date_2 = strtok(NULL," ")) != 0 &&
(p_date_3 = strtok(NULL," ")) != 0 &&
(p_name = strtok(NULL,"\r\n")) != 0 )
{
{
TQCString tmp( p_name );
if ( p_access[0] == 'l' )
{
int i = tmp.findRev( " -> " );
if ( i != -1 ) {
de.link = remoteEncoding()->decode(p_name + i + 4);
tmp.truncate( i );
}
else
de.link = TQString::null;
}
else
de.link = TQString::null;
if ( tmp[0] == '/' ) // listing on ftp://ftp.gnupg.org/ starts with '/'
tmp.remove( 0, 1 );
if (tmp.find('/') != -1)
continue; // Don't trick us!
// Some sites put more than one space between the date and the name
// e.g. ftp://ftp.uni-marburg.de/mirror/
de.name = remoteEncoding()->decode(tmp.stripWhiteSpace());
}
de.type = S_IFREG;
switch ( p_access[0] ) {
case 'd':
de.type = S_IFDIR;
break;
case 's':
de.type = S_IFSOCK;
break;
case 'b':
de.type = S_IFBLK;
break;
case 'c':
de.type = S_IFCHR;
break;
case 'l':
de.type = S_IFREG;
// we don't set S_IFLNK here. de.link says it.
break;
default:
break;
}
if ( p_access[1] == 'r' )
de.access |= S_IRUSR;
if ( p_access[2] == 'w' )
de.access |= S_IWUSR;
if ( p_access[3] == 'x' || p_access[3] == 's' )
de.access |= S_IXUSR;
if ( p_access[4] == 'r' )
de.access |= S_IRGRP;
if ( p_access[5] == 'w' )
de.access |= S_IWGRP;
if ( p_access[6] == 'x' || p_access[6] == 's' )
de.access |= S_IXGRP;
if ( p_access[7] == 'r' )
de.access |= S_IROTH;
if ( p_access[8] == 'w' )
de.access |= S_IWOTH;
if ( p_access[9] == 'x' || p_access[9] == 't' )
de.access |= S_IXOTH;
if ( p_access[3] == 's' || p_access[3] == 'S' )
de.access |= S_ISUID;
if ( p_access[6] == 's' || p_access[6] == 'S' )
de.access |= S_ISGID;
if ( p_access[9] == 't' || p_access[9] == 'T' )
de.access |= S_ISVTX;
de.owner = remoteEncoding()->decode(p_owner);
de.group = remoteEncoding()->decode(p_group);
de.size = charToLongLong(p_size);
// Parsing the date is somewhat tricky
// Examples : "Oct 6 22:49", "May 13 1999"
// First get current time - we need the current month and year
time_t currentTime = time( 0L );
struct tm * tmptr = gmtime( ¤tTime );
int currentMonth = tmptr->tm_mon;
//kdDebug(7102) << "Current time :" << asctime( tmptr ) << endl;
// Reset time fields
tmptr->tm_isdst = -1; // We do not know anything about day saving time (of any random day of the year)
tmptr->tm_sec = 0;
tmptr->tm_min = 0;
tmptr->tm_hour = 0;
// Get day number (always second field)
tmptr->tm_mday = atoi( p_date_2 );
// Get month from first field
// NOTE : no, we don't want to use TDELocale here
// It seems all FTP servers use the English way
//kdDebug(7102) << "Looking for month " << p_date_1 << endl;
static const char * s_months[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
for ( int c = 0 ; c < 12 ; c ++ )
if ( !strcmp( p_date_1, s_months[c]) )
{
//kdDebug(7102) << "Found month " << c << " for " << p_date_1 << endl;
tmptr->tm_mon = c;
break;
}
// Parse third field
if ( strlen( p_date_3 ) == 4 ) // 4 digits, looks like a year
tmptr->tm_year = atoi( p_date_3 ) - 1900;
else
{
// otherwise, the year is implicit
// according to man ls, this happens when it is between than 6 months
// old and 1 hour in the future.
// So the year is : current year if tm_mon <= currentMonth+1
// otherwise current year minus one
// (The +1 is a security for the "+1 hour" at the end of the month issue)
if ( tmptr->tm_mon > currentMonth + 1 )
tmptr->tm_year--;
// and p_date_3 contains probably a time
char * semicolon;
if ( ( semicolon = (char*)strchr( p_date_3, ':' ) ) )
{
*semicolon = '\0';
tmptr->tm_min = atoi( semicolon + 1 );
tmptr->tm_hour = atoi( p_date_3 );
}
else
kdWarning(7102) << "Can't parse third field " << p_date_3 << endl;
}
//kdDebug(7102) << asctime( tmptr ) << endl;
de.date = mktime( tmptr );
return true;
}
} // line invalid, loop to get another line
return false;
}
//===============================================================================
// public: get download file from server
// helper: ftpGet called from get() and copy()
//===============================================================================
void Ftp::get( const KURL & url )
{
kdDebug(7102) << "Ftp::get " << url.url() << endl;
int iError = 0;
ftpGet(iError, -1, url, 0); // iError gets status
if(iError) // can have only server side errs
error(iError, url.path());
ftpCloseCommand(); // must close command!
}
Ftp::StatusCode Ftp::ftpGet(int& iError, int iCopyFile, const KURL& url, TDEIO::fileoffset_t llOffset)
{
// Calls error() by itself!
if( !ftpOpenConnection(loginImplicit) )
return statusServerError;
// Try to find the size of the file (and check that it exists at
// the same time). If we get back a 550, "File does not exist"
// or "not a plain file", check if it is a directory. If it is a
// directory, return an error; otherwise simply try to retrieve
// the request...
if ( !ftpSize( url.path(), '?' ) && (m_iRespCode == 550) &&
ftpFolder(url.path(), false) )
{
// Ok it's a dir in fact
kdDebug(7102) << "ftpGet: it is a directory in fact" << endl;
iError = ERR_IS_DIRECTORY;
return statusServerError;
}
TQString resumeOffset = metaData("resume");
if ( !resumeOffset.isEmpty() )
{
llOffset = resumeOffset.toLongLong();
kdDebug(7102) << "ftpGet: got offset from metadata : " << llOffset << endl;
}
if( !ftpOpenCommand("retr", url.path(), '?', ERR_CANNOT_OPEN_FOR_READING, llOffset) )
{
kdWarning(7102) << "ftpGet: Can't open for reading" << endl;
return statusServerError;
}
// Read the size from the response string
if(m_size == UnknownSize)
{
const char* psz = strrchr( ftpResponse(4), '(' );
if(psz) m_size = charToLongLong(psz+1);
}
TDEIO::filesize_t bytesLeft = 0;
if ( m_size != UnknownSize )
bytesLeft = m_size - llOffset;
kdDebug(7102) << "ftpGet: starting with offset=" << llOffset << endl;
TDEIO::fileoffset_t processed_size = llOffset;
TQByteArray array;
bool mimetypeEmitted = false;
char buffer[maximumIpcSize];
// start whith small data chunks in case of a slow data source (modem)
// - unfortunately this has a negative impact on performance for large
// - files - so we will increase the block size after a while ...
int iBlockSize = initialIpcSize;
int iBufferCur = 0;
while(m_size == UnknownSize || bytesLeft > 0)
{ // let the buffer size grow if the file is larger 64kByte ...
if(processed_size-llOffset > 1024 * 64)
iBlockSize = maximumIpcSize;
// read the data and detect EOF or error ...
if(iBlockSize+iBufferCur > (int)sizeof(buffer))
iBlockSize = sizeof(buffer) - iBufferCur;
int n = m_data->read( buffer+iBufferCur, iBlockSize );
if(n <= 0)
{ // this is how we detect EOF in case of unknown size
if( m_size == UnknownSize && n == 0 )
break;
// unexpected eof. Happens when the daemon gets killed.
iError = ERR_COULD_NOT_READ;
return statusServerError;
}
processed_size += n;
// collect very small data chunks in buffer before processing ...
if(m_size != UnknownSize)
{
bytesLeft -= n;
iBufferCur += n;
if(iBufferCur < mimimumMimeSize && bytesLeft > 0)
{
processedSize( processed_size );
continue;
}
n = iBufferCur;
iBufferCur = 0;
}
// get the mime type and set the total size ...
if(!mimetypeEmitted)
{
mimetypeEmitted = true;
array.setRawData(buffer, n);
KMimeMagicResult * result = KMimeMagic::self()->findBufferFileType(array, url.fileName());
array.resetRawData(buffer, n);
kdDebug(7102) << "ftpGet: Emitting mimetype " << result->mimeType() << endl;
mimeType( result->mimeType() );
if( m_size != UnknownSize ) // Emit total size AFTER mimetype
totalSize( m_size );
}
// write output file or pass to data pump ...
if(iCopyFile == -1)
{
array.setRawData(buffer, n);
data( array );
array.resetRawData(buffer, n);
}
else if( (iError = WriteToFile(iCopyFile, buffer, n)) != 0)
return statusClientError; // client side error
processedSize( processed_size );
}
kdDebug(7102) << "ftpGet: done" << endl;
if(iCopyFile == -1) // must signal EOF to data pump ...
data(array); // array is empty and must be empty!
processedSize( m_size == UnknownSize ? processed_size : m_size );
kdDebug(7102) << "ftpGet: emitting finished()" << endl;
finished();
return statusSuccess;
}
/*
void Ftp::mimetype( const KURL& url )
{
if( !ftpOpenConnection(loginImplicit) )
return;
if ( !ftpOpenCommand( "retr", url.path(), 'I', ERR_CANNOT_OPEN_FOR_READING, 0 ) ) {
kdWarning(7102) << "Can't open for reading" << endl;
return;
}
char buffer[ 2048 ];
TQByteArray array;
// Get one chunk of data only and send it, TDEIO::Job will determine the
// mimetype from it using KMimeMagic
int n = m_data->read( buffer, 2048 );
array.setRawData(buffer, n);
data( array );
array.resetRawData(buffer, n);
kdDebug(7102) << "aborting" << endl;
ftpAbortTransfer();
kdDebug(7102) << "finished" << endl;
finished();
kdDebug(7102) << "after finished" << endl;
}
void Ftp::ftpAbortTransfer()
{
// RFC 959, page 34-35
// IAC (interpret as command) = 255 ; IP (interrupt process) = 254
// DM = 242 (data mark)
char msg[4];
// 1. User system inserts the Telnet "Interrupt Process" (IP) signal
// in the Telnet stream.
msg[0] = (char) 255; //IAC
msg[1] = (char) 254; //IP
(void) send(sControl, msg, 2, 0);
// 2. User system sends the Telnet "Sync" signal.
msg[0] = (char) 255; //IAC
msg[1] = (char) 242; //DM
if (send(sControl, msg, 2, MSG_OOB) != 2)
; // error...
// Send ABOR
kdDebug(7102) << "send ABOR" << endl;
TQCString buf = "ABOR\r\n";
if ( KSocks::self()->write( sControl, buf.data(), buf.length() ) <= 0 ) {
error( ERR_COULD_NOT_WRITE, TQString::null );
return;
}
//
kdDebug(7102) << "read resp" << endl;
if ( readresp() != '2' )
{
error( ERR_COULD_NOT_READ, TQString::null );
return;
}
kdDebug(7102) << "close sockets" << endl;
closeSockets();
}
*/
//===============================================================================
// public: put upload file to server
// helper: ftpPut called from put() and copy()
//===============================================================================
void Ftp::put(const KURL& url, int permissions, bool overwrite, bool resume)
{
kdDebug(7102) << "Ftp::put " << url.url() << endl;
int iError = 0; // iError gets status
ftpPut(iError, -1, url, permissions, overwrite, resume);
if(iError) // can have only server side errs
error(iError, url.path());
ftpCloseCommand(); // must close command!
}
Ftp::StatusCode Ftp::ftpPut(int& iError, int iCopyFile, const KURL& dest_url,
int permissions, bool overwrite, bool resume)
{
if( !ftpOpenConnection(loginImplicit) )
return statusServerError;
// Don't use mark partial over anonymous FTP.
// My incoming dir allows put but not rename...
bool bMarkPartial;
if (m_user.isEmpty () || m_user == FTP_LOGIN)
bMarkPartial = false;
else
bMarkPartial = config()->readBoolEntry("MarkPartial", true);
TQString dest_orig = dest_url.path();
TQString dest_part( dest_orig );
dest_part += ".part";
if ( ftpSize( dest_orig, 'I' ) )
{
if ( m_size == 0 )
{ // delete files with zero size
TQCString cmd = "DELE ";
cmd += remoteEncoding()->encode(dest_orig);
if( !ftpSendCmd( cmd ) || (m_iRespType != 2) )
{
iError = ERR_CANNOT_DELETE_PARTIAL;
return statusServerError;
}
}
else if ( !overwrite && !resume )
{
iError = ERR_FILE_ALREADY_EXIST;
return statusServerError;
}
else if ( bMarkPartial )
{ // when using mark partial, append .part extension
if ( !ftpRename( dest_orig, dest_part, true ) )
{
iError = ERR_CANNOT_RENAME_PARTIAL;
return statusServerError;
}
}
// Don't chmod an existing file
permissions = -1;
}
else if ( bMarkPartial && ftpSize( dest_part, 'I' ) )
{ // file with extension .part exists
if ( m_size == 0 )
{ // delete files with zero size
TQCString cmd = "DELE ";
cmd += remoteEncoding()->encode(dest_part);
if ( !ftpSendCmd( cmd ) || (m_iRespType != 2) )
{
iError = ERR_CANNOT_DELETE_PARTIAL;
return statusServerError;
}
}
else if ( !overwrite && !resume )
{
resume = canResume (m_size);
if (!resume)
{
iError = ERR_FILE_ALREADY_EXIST;
return statusServerError;
}
}
}
else
m_size = 0;
TQString dest;
// if we are using marking of partial downloads -> add .part extension
if ( bMarkPartial ) {
kdDebug(7102) << "Adding .part extension to " << dest_orig << endl;
dest = dest_part;
} else
dest = dest_orig;
TDEIO::fileoffset_t offset = 0;
// set the mode according to offset
if( resume && m_size > 0 )
{
offset = m_size;
if(iCopyFile != -1)
{
if( KDE_lseek(iCopyFile, offset, SEEK_SET) < 0 )
{
iError = ERR_CANNOT_RESUME;
return statusClientError;
}
}
}
if (! ftpOpenCommand( "stor", dest, '?', ERR_COULD_NOT_WRITE, offset ) )
return statusServerError;
kdDebug(7102) << "ftpPut: starting with offset=" << offset << endl;
TDEIO::fileoffset_t processed_size = offset;
TQByteArray buffer;
int result;
int iBlockSize = initialIpcSize;
// Loop until we got 'dataEnd'
do
{
if(iCopyFile == -1)
{
dataReq(); // Request for data
result = readData( buffer );
}
else
{ // let the buffer size grow if the file is larger 64kByte ...
if(processed_size-offset > 1024 * 64)
iBlockSize = maximumIpcSize;
buffer.resize(iBlockSize);
result = ::read(iCopyFile, buffer.data(), buffer.size());
if(result < 0)
iError = ERR_COULD_NOT_WRITE;
else
buffer.resize(result);
}
if (result > 0)
{
m_data->write( buffer.data(), buffer.size() );
processed_size += result;
processedSize (processed_size);
}
}
while ( result > 0 );
if (result != 0) // error
{
ftpCloseCommand(); // don't care about errors
kdDebug(7102) << "Error during 'put'. Aborting." << endl;
if (bMarkPartial)
{
// Remove if smaller than minimum size
if ( ftpSize( dest, 'I' ) &&
( processed_size < (unsigned long) config()->readNumEntry("MinimumKeepSize", DEFAULT_MINIMUM_KEEP_SIZE) ) )
{
TQCString cmd = "DELE ";
cmd += remoteEncoding()->encode(dest);
(void) ftpSendCmd( cmd );
}
}
return statusServerError;
}
if ( !ftpCloseCommand() )
{
iError = ERR_COULD_NOT_WRITE;
return statusServerError;
}
// after full download rename the file back to original name
if ( bMarkPartial )
{
kdDebug(7102) << "renaming dest (" << dest << ") back to dest_orig (" << dest_orig << ")" << endl;
if ( !ftpRename( dest, dest_orig, true ) )
{
iError = ERR_CANNOT_RENAME_PARTIAL;
return statusServerError;
}
}
// set final permissions
if ( permissions != -1 )
{
if ( m_user == FTP_LOGIN )
kdDebug(7102) << "Trying to chmod over anonymous FTP ???" << endl;
// chmod the file we just put
if ( ! ftpChmod( dest_orig, permissions ) )
{
// To be tested
//if ( m_user != FTP_LOGIN )
// warning( i18n( "Could not change permissions for\n%1" ).arg( dest_orig ) );
}
}
// We have done our job => finish
finished();
return statusSuccess;
}
/** Use the SIZE command to get the file size.
Warning : the size depends on the transfer mode, hence the second arg. */
bool Ftp::ftpSize( const TQString & path, char mode )
{
m_size = UnknownSize;
if( !ftpDataMode(mode) )
return false;
TQCString buf;
buf = "SIZE ";
buf += remoteEncoding()->encode(path);
if( !ftpSendCmd( buf ) || (m_iRespType != 2) )
return false;
// skip leading "213 " (response code)
const char* psz = ftpResponse(4);
if(!psz)
return false;
m_size = charToLongLong(psz);
return true;
}
// Today the differences between ASCII and BINARY are limited to
// CR or CR/LF line terminators. Many servers ignore ASCII (like
// win2003 -or- vsftp with default config). In the early days of
// computing, when even text-files had structure, this stuff was
// more important.
// Theoretically "list" could return different results in ASCII
// and BINARY mode. But again, most servers ignore ASCII here.
bool Ftp::ftpDataMode(char cMode)
{
if(cMode == '?') cMode = m_bTextMode ? 'A' : 'I';
else if(cMode == 'a') cMode = 'A';
else if(cMode != 'A') cMode = 'I';
kdDebug(7102) << "ftpDataMode: want '" << cMode << "' has '" << m_cDataMode << "'" << endl;
if(m_cDataMode == cMode)
return true;
TQCString buf;
buf.sprintf("TYPE %c", cMode);
if( !ftpSendCmd(buf) || (m_iRespType != 2) )
return false;
m_cDataMode = cMode;
return true;
}
bool Ftp::ftpFolder(const TQString& path, bool bReportError)
{
TQString newPath = path;
int iLen = newPath.length();
if(iLen > 1 && newPath[iLen-1] == '/') newPath.truncate(iLen-1);
//kdDebug(7102) << "ftpFolder: want '" << newPath << "' has '" << m_currentPath << "'" << endl;
if(m_currentPath == newPath)
return true;
TQCString tmp = "cwd ";
tmp += remoteEncoding()->encode(newPath);
if( !ftpSendCmd(tmp) )
return false; // connection failure
if(m_iRespType != 2)
{
if(bReportError)
error(ERR_CANNOT_ENTER_DIRECTORY, path);
return false; // not a folder
}
m_currentPath = newPath;
return true;
}
//===============================================================================
// public: copy don't use tdeio data pump if one side is a local file
// helper: ftpCopyPut called from copy() on upload
// helper: ftpCopyGet called from copy() on download
//===============================================================================
void Ftp::copy( const KURL &src, const KURL &dest, int permissions, bool overwrite )
{
int iError = 0;
int iCopyFile = -1;
StatusCode cs = statusSuccess;
bool bSrcLocal = src.isLocalFile();
bool bDestLocal = dest.isLocalFile();
TQString sCopyFile;
if(bSrcLocal && !bDestLocal) // File -> Ftp
{
sCopyFile = src.path();
kdDebug(7102) << "Ftp::copy local file '" << sCopyFile << "' -> ftp '" << dest.path() << "'" << endl;
cs = ftpCopyPut(iError, iCopyFile, sCopyFile, dest, permissions, overwrite);
if( cs == statusServerError) sCopyFile = dest.url();
}
else if(!bSrcLocal && bDestLocal) // Ftp -> File
{
sCopyFile = dest.path();
kdDebug(7102) << "Ftp::copy ftp '" << src.path() << "' -> local file '" << sCopyFile << "'" << endl;
cs = ftpCopyGet(iError, iCopyFile, sCopyFile, src, permissions, overwrite);
if( cs == statusServerError ) sCopyFile = src.url();
}
else {
error( ERR_UNSUPPORTED_ACTION, TQString::null );
return;
}
// perform clean-ups and report error (if any)
if(iCopyFile != -1)
::close(iCopyFile);
if(iError)
error(iError, sCopyFile);
ftpCloseCommand(); // must close command!
}
Ftp::StatusCode Ftp::ftpCopyPut(int& iError, int& iCopyFile, TQString sCopyFile,
const KURL& url, int permissions, bool overwrite)
{
// check if source is ok ...
KDE_struct_stat buff;
TQCString sSrc( TQFile::encodeName(sCopyFile) );
bool bSrcExists = (KDE_stat( sSrc.data(), &buff ) != -1);
if(bSrcExists)
{ if(S_ISDIR(buff.st_mode))
{
iError = ERR_IS_DIRECTORY;
return statusClientError;
}
}
else
{
iError = ERR_DOES_NOT_EXIST;
return statusClientError;
}
iCopyFile = KDE_open( sSrc.data(), O_RDONLY );
if(iCopyFile == -1)
{
iError = ERR_CANNOT_OPEN_FOR_READING;
return statusClientError;
}
// delegate the real work (iError gets status) ...
totalSize(buff.st_size);
#ifdef ENABLE_CAN_RESUME
return ftpPut(iError, iCopyFile, url, permissions, overwrite, false);
#else
return ftpPut(iError, iCopyFile, url, permissions, overwrite, true);
#endif
}
Ftp::StatusCode Ftp::ftpCopyGet(int& iError, int& iCopyFile, const TQString sCopyFile,
const KURL& url, int permissions, bool overwrite)
{
// check if destination is ok ...
KDE_struct_stat buff;
TQCString sDest( TQFile::encodeName(sCopyFile) );
bool bDestExists = (KDE_stat( sDest.data(), &buff ) != -1);
if(bDestExists)
{ if(S_ISDIR(buff.st_mode))
{
iError = ERR_IS_DIRECTORY;
return statusClientError;
}
if(!overwrite)
{
iError = ERR_FILE_ALREADY_EXIST;
return statusClientError;
}
}
// do we have a ".part" file?
TQCString sPart = TQFile::encodeName(sCopyFile + ".part");
bool bResume = false;
bool bPartExists = (KDE_stat( sPart.data(), &buff ) != -1);
const bool bMarkPartial = config()->readBoolEntry("MarkPartial", true);
if(!bMarkPartial)
{
sPart = TQFile::encodeName(sCopyFile);
}
else if(bPartExists && buff.st_size > 0)
{ // must not be a folder! please fix a similar bug in tdeio_file!!
if(S_ISDIR(buff.st_mode))
{
iError = ERR_DIR_ALREADY_EXIST;
return statusClientError; // client side error
}
//doesn't work for copy? -> design flaw?
#ifdef ENABLE_CAN_RESUME
bResume = canResume( buff.st_size );
#else
bResume = true;
#endif
}
if(bPartExists && !bResume) // get rid of an unwanted ".part" file
remove(sPart.data());
// JPF: in tdeio_file overwrite disables ".part" operations. I do not believe
// JPF: that this is a good behaviour!
if(bDestExists) // must delete for overwrite
remove(sDest.data());
// WABA: Make sure that we keep writing permissions ourselves,
// otherwise we can be in for a surprise on NFS.
mode_t initialMode;
if (permissions != -1)
initialMode = permissions | S_IWUSR;
else
initialMode = 0666;
// open the output file ...
TDEIO::fileoffset_t hCopyOffset = 0;
if(bResume)
{
iCopyFile = KDE_open( sPart.data(), O_RDWR ); // append if resuming
hCopyOffset = KDE_lseek(iCopyFile, 0, SEEK_END);
if(hCopyOffset < 0)
{
iError = ERR_CANNOT_RESUME;
return statusClientError; // client side error
}
kdDebug(7102) << "copy: resuming at " << hCopyOffset << endl;
}
else
iCopyFile = KDE_open(sPart.data(), O_CREAT | O_TRUNC | O_WRONLY, initialMode);
if(iCopyFile == -1)
{
kdDebug(7102) << "copy: ### COULD NOT WRITE " << sCopyFile << endl;
iError = (errno == EACCES) ? ERR_WRITE_ACCESS_DENIED
: ERR_CANNOT_OPEN_FOR_WRITING;
return statusClientError;
}
// delegate the real work (iError gets status) ...
StatusCode iRes = ftpGet(iError, iCopyFile, url, hCopyOffset);
if( ::close(iCopyFile) && iRes == statusSuccess )
{
iError = ERR_COULD_NOT_WRITE;
iRes = statusClientError;
}
// handle renaming or deletion of a partial file ...
if(bMarkPartial)
{
if(iRes == statusSuccess)
{ // rename ".part" on success
if ( ::rename( sPart.data(), sDest.data() ) )
{
kdDebug(7102) << "copy: cannot rename " << sPart << " to " << sDest << endl;
iError = ERR_CANNOT_RENAME_PARTIAL;
iRes = statusClientError;
}
}
else if(KDE_stat( sPart.data(), &buff ) == 0)
{ // should a very small ".part" be deleted?
int size = config()->readNumEntry("MinimumKeepSize", DEFAULT_MINIMUM_KEEP_SIZE);
if (buff.st_size < size)
remove(sPart.data());
}
}
return iRes;
}
|